gnat1drv.adb 58.3 KB
Newer Older
Richard Kenner committed
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                             G N A T 1 D R V                              --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 1992-2018, Free Software Foundation, Inc.         --
Richard Kenner committed
10 11 12
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- 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- --
Richard Kenner committed
14 15 16 17
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
18 19
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license.          --
Richard Kenner committed
20 21
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
22
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
Richard Kenner committed
23 24 25
--                                                                          --
------------------------------------------------------------------------------

26 27
with Atree;     use Atree;
with Back_End;  use Back_End;
Arnaud Charlet committed
28
with Checks;
Richard Kenner committed
29
with Comperr;
30
with Csets;
31
with Debug;     use Debug;
Richard Kenner committed
32
with Elists;
33
with Errout;    use Errout;
34
with Exp_CG;
35
with Fmap;
36 37
with Fname;     use Fname;
with Fname.UF;  use Fname.UF;
Richard Kenner committed
38
with Frontend;
39 40
with Ghost;     use Ghost;
with Gnatvsn;   use Gnatvsn;
Richard Kenner committed
41
with Inline;
42 43
with Lib;       use Lib;
with Lib.Writ;  use Lib.Writ;
44
with Lib.Xref;
45
with Namet;     use Namet;
Richard Kenner committed
46
with Nlists;
47 48 49 50
with Opt;       use Opt;
with Osint;     use Osint;
with Osint.C;   use Osint.C;
with Output;    use Output;
Arnaud Charlet committed
51
with Par_SCO;
52
with Prepcomp;
53
with Repinfo;   use Repinfo;
54
with Restrict;
55
with Rident;    use Rident;
56
with Rtsfind;
Arnaud Charlet committed
57
with SCOs;
Richard Kenner committed
58
with Sem;
59 60
with Sem_Ch8;
with Sem_Ch12;
Richard Kenner committed
61
with Sem_Ch13;
Arnaud Charlet committed
62
with Sem_Elim;
63
with Sem_Eval;
Arnaud Charlet committed
64
with Sem_SPARK; use Sem_SPARK;
65
with Sem_Type;
Arnaud Charlet committed
66
with Set_Targ;
67 68 69 70
with Sinfo;     use Sinfo;
with Sinput.L;  use Sinput.L;
with Snames;    use Snames;
with Sprint;    use Sprint;
Richard Kenner committed
71
with Stringt;
72 73
with Stylesw;   use Stylesw;
with Targparm;  use Targparm;
Arnaud Charlet committed
74
with Tbuild;
Richard Kenner committed
75
with Tree_Gen;
76
with Treepr;    use Treepr;
Richard Kenner committed
77
with Ttypes;
78
with Types;     use Types;
79
with Uintp;
80
with Uname;     use Uname;
Richard Kenner committed
81 82
with Urealp;
with Usage;
83
with Validsw;   use Validsw;
Richard Kenner committed
84 85

with System.Assertions;
Arnaud Charlet committed
86
with System.OS_Lib;
Richard Kenner committed
87

Arnaud Charlet committed
88 89 90 91
--------------
-- Gnat1drv --
--------------

Richard Kenner committed
92
procedure Gnat1drv is
Arnaud Charlet committed
93
   procedure Adjust_Global_Switches;
Arnaud Charlet committed
94
   --  There are various interactions between front-end switch settings,
Arnaud Charlet committed
95 96
   --  including debug switch settings and target dependent parameters.
   --  This procedure takes care of properly handling these interactions.
97
   --  We do it after scanning out all the switches, so that we are not
Arnaud Charlet committed
98 99
   --  depending on the order in which switches appear.

Arnaud Charlet committed
100 101 102
   procedure Check_Bad_Body (Unit_Node : Node_Id; Unit_Kind : Node_Kind);
   --  Called to check whether a unit described by its compilation unit node
   --  and kind has a bad body.
103 104 105 106 107

   procedure Check_Rep_Info;
   --  Called when we are not generating code, to check if -gnatR was requested
   --  and if so, explain that we will not be honoring the request.

Arnaud Charlet committed
108 109 110 111 112 113 114
   procedure Post_Compilation_Validation_Checks;
   --  This procedure performs various validation checks that have to be left
   --  to the end of the compilation process, after generating code but before
   --  issuing error messages. In particular, these checks generally require
   --  the information provided by the back end in back annotation of declared
   --  entities (e.g. actual size and alignment values chosen by the back end).

Arnaud Charlet committed
115 116 117 118 119
   ----------------------------
   -- Adjust_Global_Switches --
   ----------------------------

   procedure Adjust_Global_Switches is
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
      procedure SPARK_Library_Warning (Kind : String);
      --  Issue a warning in GNATprove mode if the run-time library does not
      --  fully support IEEE-754 floating-point semantics.

      ---------------------------
      -- SPARK_Library_Warning --
      ---------------------------

      procedure SPARK_Library_Warning (Kind : String) is
      begin
         Write_Line
           ("warning: run-time library may be configured incorrectly");
         Write_Line
           ("warning: (SPARK analysis requires support for " & Kind & ')');
      end SPARK_Library_Warning;

   --  Start of processing for Adjust_Global_Switches

Arnaud Charlet committed
138
   begin
139 140 141
      --  Define pragma GNAT_Annotate as an alias of pragma Annotate, to be
      --  able to work around bootstrap limitations with the old syntax of
      --  pragma Annotate, and use pragma GNAT_Annotate in compiler sources
142 143 144 145
      --  when needed.

      Map_Pragma_Name (From => Name_Gnat_Annotate, To => Name_Annotate);

Arnaud Charlet committed
146
      --  -gnatd.M enables Relaxed_RM_Semantics
Arnaud Charlet committed
147

Arnaud Charlet committed
148 149
      if Debug_Flag_Dot_MM then
         Relaxed_RM_Semantics := True;
Arnaud Charlet committed
150
      end if;
151 152 153 154 155 156

      --  -gnatd.1 enables unnesting of subprograms

      if Debug_Flag_Dot_1 then
         Unnest_Subprogram_Mode := True;
      end if;
Arnaud Charlet committed
157

158
      --  -gnatd.u enables special C expansion mode
Arnaud Charlet committed
159

160
      if Debug_Flag_Dot_U then
Arnaud Charlet committed
161 162 163
         Modify_Tree_For_C := True;
      end if;

164 165 166
      --  Set all flags required when generating C code

      if Generate_C_Code then
167
         Modify_Tree_For_C := True;
168
         Unnest_Subprogram_Mode := True;
169
         Building_Static_Dispatch_Tables := False;
Arnaud Charlet committed
170
         Minimize_Expression_With_Actions := True;
171
         Expand_Nonbinary_Modular_Ops := True;
172 173 174 175 176

         --  Set operating mode to Generate_Code to benefit from full front-end
         --  expansion (e.g. generics).

         Operating_Mode := Generate_Code;
Arnaud Charlet committed
177 178

         --  Suppress alignment checks since we do not have access to alignment
Arnaud Charlet committed
179
         --  info on the target.
Arnaud Charlet committed
180 181

         Suppress_Options.Suppress (Alignment_Check) := False;
182 183
      end if;

Arnaud Charlet committed
184 185 186 187 188 189 190
      --  -gnatd.E sets Error_To_Warning mode, causing selected error messages
      --  to be treated as warnings instead of errors.

      if Debug_Flag_Dot_EE then
         Error_To_Warning := True;
      end if;

Arnaud Charlet committed
191 192 193 194 195 196 197
      --  -gnatdJ sets Include_Subprogram_In_Messages, adding the related
      --  subprogram as part of the error and warning messages.

      if Debug_Flag_JJ then
         Include_Subprogram_In_Messages := True;
      end if;

Arnaud Charlet committed
198 199 200 201 202 203 204
      --  Disable CodePeer_Mode in Check_Syntax, since we need front-end
      --  expansion.

      if Operating_Mode = Check_Syntax then
         CodePeer_Mode := False;
      end if;

Arnaud Charlet committed
205 206 207 208 209
      --  Set ASIS mode if -gnatt and -gnatc are set

      if Operating_Mode = Check_Semantics and then Tree_Output then
         ASIS_Mode := True;

Arnaud Charlet committed
210 211 212 213 214 215
         --  Set ASIS GNSA mode if -gnatd.H is set

         if Debug_Flag_Dot_HH then
            ASIS_GNSA_Mode := True;
         end if;

Arnaud Charlet committed
216 217 218
         --  Turn off inlining in ASIS mode, since ASIS cannot handle the extra
         --  information in the trees caused by inlining being active.

219 220
         --  More specifically, the tree seems to be malformed from the ASIS
         --  point of view if -gnatc and -gnatn appear together???
Arnaud Charlet committed
221 222 223

         Inline_Active := False;

Arnaud Charlet committed
224 225
         --  Turn off SCIL generation and CodePeer mode in semantics mode,
         --  since SCIL requires front-end expansion.
Arnaud Charlet committed
226 227

         Generate_SCIL := False;
Arnaud Charlet committed
228
         CodePeer_Mode := False;
Arnaud Charlet committed
229 230 231 232 233 234
      end if;

      --  SCIL mode needs to disable front-end inlining since the generated
      --  trees (in particular order and consistency between specs compiled
      --  as part of a main unit or as part of a with-clause) are causing
      --  troubles.
Arnaud Charlet committed
235

Arnaud Charlet committed
236 237
      if Generate_SCIL then
         Front_End_Inlining := False;
Arnaud Charlet committed
238 239
      end if;

Arnaud Charlet committed
240
      --  Tune settings for optimal SCIL generation in CodePeer mode
Arnaud Charlet committed
241 242 243

      if CodePeer_Mode then

Arnaud Charlet committed
244 245
         --  Turn off gnatprove mode (which can be set via e.g. -gnatd.F), not
         --  compatible with CodePeer mode.
Arnaud Charlet committed
246 247

         GNATprove_Mode := False;
Arnaud Charlet committed
248
         Debug_Flag_Dot_FF := False;
Arnaud Charlet committed
249

250 251
         --  Turn off length expansion. CodePeer has its own mechanism to
         --  handle length attribute.
252

253 254
         Debug_Flag_Dot_PP := True;

255 256 257
         --  Turn off C tree generation, not compatible with CodePeer mode. We
         --  do not expect this to happen in normal use, since both modes are
         --  enabled by special tools, but it is useful to turn off these flags
Arnaud Charlet committed
258
         --  this way when we are doing CodePeer tests on existing test suites
Arnaud Charlet committed
259
         --  that may have -gnateg set, to avoid the need for special casing.
260

261 262
         Modify_Tree_For_C      := False;
         Generate_C_Code        := False;
263 264
         Unnest_Subprogram_Mode := False;

Arnaud Charlet committed
265
         --  Turn off inlining, confuses CodePeer output and gains nothing
Arnaud Charlet committed
266 267

         Front_End_Inlining := False;
Arnaud Charlet committed
268 269
         Inline_Active      := False;

Arnaud Charlet committed
270 271 272 273 274 275
         --  Disable front-end optimizations, to keep the tree as close to the
         --  source code as possible, and also to avoid inconsistencies between
         --  trees when using different optimization switches.

         Optimization_Level := 0;

Arnaud Charlet committed
276 277
         --  Enable some restrictions systematically to simplify the generated
         --  code (and ease analysis). Note that restriction checks are also
Arnaud Charlet committed
278 279 280
         --  disabled in CodePeer mode, see Restrict.Check_Restriction, and
         --  user specified Restrictions pragmas are ignored, see
         --  Sem_Prag.Process_Restrictions_Or_Restriction_Warnings.
Arnaud Charlet committed
281

282
         Restrict.Restrictions.Set   (No_Exception_Registration)       := True;
Arnaud Charlet committed
283
         Restrict.Restrictions.Set   (No_Initialize_Scalars)           := True;
Arnaud Charlet committed
284 285 286
         Restrict.Restrictions.Set   (No_Task_Hierarchy)               := True;
         Restrict.Restrictions.Set   (No_Abort_Statements)             := True;
         Restrict.Restrictions.Set   (Max_Asynchronous_Select_Nesting) := True;
Arnaud Charlet committed
287
         Restrict.Restrictions.Value (Max_Asynchronous_Select_Nesting) := 0;
Arnaud Charlet committed
288

Arnaud Charlet committed
289 290 291 292
         --  Enable pragma Ignore_Pragma (Global) to support legacy code. As a
         --  consequence, Refined_Global pragma should be ignored as well, as
         --  it is only allowed on a body when pragma Global is given for the
         --  spec.
Arnaud Charlet committed
293

Arnaud Charlet committed
294 295
         Set_Name_Table_Boolean3 (Name_Global, True);
         Set_Name_Table_Boolean3 (Name_Refined_Global, True);
Arnaud Charlet committed
296 297

         --  Suppress division by zero checks since they are handled
Arnaud Charlet committed
298
         --  implicitly by CodePeer.
Arnaud Charlet committed
299 300

         --  Turn off dynamic elaboration checks: generates inconsistencies in
Arnaud Charlet committed
301 302 303
         --  trees between specs compiled as part of a main unit or as part of
         --  a with-clause.

Arnaud Charlet committed
304 305 306
         --  Turn off alignment checks: these cannot be proved statically by
         --  CodePeer and generate false positives.

Arnaud Charlet committed
307
         --  Enable all other language checks
Arnaud Charlet committed
308

309
         Suppress_Options.Suppress :=
Arnaud Charlet committed
310
           (Alignment_Check   => True,
311 312 313
            Division_Check    => True,
            Elaboration_Check => True,
            others            => False);
Arnaud Charlet committed
314

Arnaud Charlet committed
315
         Dynamic_Elaboration_Checks := False;
Arnaud Charlet committed
316

Arnaud Charlet committed
317 318 319
         --  Set STRICT mode for overflow checks if not set explicitly. This
         --  prevents suppressing of overflow checks by default, in code down
         --  below.
Arnaud Charlet committed
320

321 322 323
         if Suppress_Options.Overflow_Mode_General = Not_Set then
            Suppress_Options.Overflow_Mode_General    := Strict;
            Suppress_Options.Overflow_Mode_Assertions := Strict;
Arnaud Charlet committed
324 325
         end if;

Arnaud Charlet committed
326 327 328 329 330 331 332
         --  CodePeer handles division and overflow checks directly, based on
         --  the marks set by the frontend, hence no special expansion should
         --  be performed in the frontend for division and overflow checks.

         Backend_Divide_Checks_On_Target   := True;
         Backend_Overflow_Checks_On_Target := True;

Arnaud Charlet committed
333 334 335 336
         --  Kill debug of generated code, since it messes up sloc values

         Debug_Generated_Code := False;

337 338 339 340 341
         --  Ditto for -gnateG which interacts badly with handling of pragma
         --  Annotate in gnat2scil.

         Generate_Processed_File := False;

Arnaud Charlet committed
342 343 344 345 346
         --  Disable Exception_Extra_Info (-gnateE) which generates more
         --  complex trees with no added value, and may confuse CodePeer.

         Exception_Extra_Info := False;

Arnaud Charlet committed
347
         --  Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
Arnaud Charlet committed
348
         --  to support source navigation.
Arnaud Charlet committed
349 350 351 352 353 354 355

         Xref_Active := True;

         --  Polling mode forced off, since it generates confusing junk

         Polling_Required := False;

Arnaud Charlet committed
356 357
         --  Set operating mode to Generate_Code to benefit from full front-end
         --  expansion (e.g. generics).
Arnaud Charlet committed
358

Arnaud Charlet committed
359
         Operating_Mode := Generate_Code;
Arnaud Charlet committed
360 361 362 363 364

         --  We need SCIL generation of course

         Generate_SCIL := True;

Arnaud Charlet committed
365
         --  Enable assertions, since they give CodePeer valuable extra info
Arnaud Charlet committed
366

367
         Assertions_Enabled := True;
Arnaud Charlet committed
368

Arnaud Charlet committed
369
         --  Set normal RM validity checking and checking of copies (to catch
Arnaud Charlet committed
370
         --  e.g. wrong values used in unchecked conversions).
Arnaud Charlet committed
371 372 373
         --  All other validity checking is turned off, since this can generate
         --  very complex trees that only confuse CodePeer and do not bring
         --  enough useful info.
Arnaud Charlet committed
374 375 376

         Reset_Validity_Check_Options;
         Validity_Check_Default       := True;
Arnaud Charlet committed
377
         Validity_Check_Copies        := True;
378
         Check_Validity_Of_Parameters := False;
Arnaud Charlet committed
379

Arnaud Charlet committed
380 381 382
         --  Turn off style check options and ignore any style check pragmas
         --  since we are not interested in any front-end warnings when we are
         --  getting CodePeer output.
Arnaud Charlet committed
383 384

         Reset_Style_Check_Options;
Arnaud Charlet committed
385
         Ignore_Style_Checks_Pragmas := True;
Arnaud Charlet committed
386 387 388 389 390 391

         --  Always perform semantics and generate ali files in CodePeer mode,
         --  so that a gnatmake -c -k will proceed further when possible.

         Force_ALI_Tree_File := True;
         Try_Semantics := True;
Arnaud Charlet committed
392

Arnaud Charlet committed
393
         --  Make the Ada front end more liberal so that the compiler will
Arnaud Charlet committed
394
         --  allow illegal code that is allowed by other compilers. CodePeer
395
         --  is in the business of finding problems, not enforcing rules.
Arnaud Charlet committed
396 397
         --  This is useful when using CodePeer mode with other compilers.

Arnaud Charlet committed
398
         Relaxed_RM_Semantics := True;
Arnaud Charlet committed
399

400
         if not Generate_CodePeer_Messages then
401

402
            --  Suppress compiler warnings by default when generating SCIL for
403 404
            --  CodePeer, except when combined with -gnateC where we do want to
            --  emit GNAT warnings.
405 406 407 408

            Warning_Mode := Suppress;
         end if;

Arnaud Charlet committed
409 410 411 412 413
         --  Disable all simple value propagation. This is an optimization
         --  which is valuable for code optimization, and also for generation
         --  of compiler warnings, but these are being turned off by default,
         --  and CodePeer generates better messages (referencing original
         --  variables) this way.
Arnaud Charlet committed
414
         --  Do this only if -gnatws is set (the default with -gnatcC), so that
Arnaud Charlet committed
415 416 417 418 419
         --  if warnings are enabled, we'll get better messages from GNAT.

         if Warning_Mode = Suppress then
            Debug_Flag_MM := True;
         end if;
Arnaud Charlet committed
420 421
      end if;

Arnaud Charlet committed
422 423 424
      --  Enable some individual switches that are implied by relaxed RM
      --  semantics mode.

Arnaud Charlet committed
425
      if Relaxed_RM_Semantics then
Arnaud Charlet committed
426
         Opt.Allow_Integer_Address := True;
Arnaud Charlet committed
427
         Overriding_Renamings := True;
Arnaud Charlet committed
428
         Treat_Categorization_Errors_As_Warnings := True;
Arnaud Charlet committed
429 430
      end if;

431
      --  Enable GNATprove_Mode when using -gnatd.F switch
Arnaud Charlet committed
432

Arnaud Charlet committed
433
      if Debug_Flag_Dot_FF then
434
         GNATprove_Mode := True;
Arnaud Charlet committed
435 436
      end if;

437 438
      --  GNATprove_Mode is also activated by default in the gnat2why
      --  executable.
Arnaud Charlet committed
439

440
      if GNATprove_Mode then
Arnaud Charlet committed
441

Arnaud Charlet committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
         --  Turn off CodePeer mode (which can be set via e.g. -gnatC or
         --  -gnateC), not compatible with GNATprove mode.

         CodePeer_Mode := False;
         Generate_SCIL := False;

         --  Turn off C tree generation, not compatible with GNATprove mode. We
         --  do not expect this to happen in normal use, since both modes are
         --  enabled by special tools, but it is useful to turn off these flags
         --  this way when we are doing GNATprove tests on existing test suites
         --  that may have -gnateg set, to avoid the need for special casing.

         Modify_Tree_For_C := False;
         Generate_C_Code := False;
         Unnest_Subprogram_Mode := False;

Arnaud Charlet committed
458 459 460 461 462 463
         --  Turn off inlining, which would confuse formal verification output
         --  and gain nothing.

         Front_End_Inlining := False;
         Inline_Active      := False;

464 465 466 467 468 469
         --  Issue warnings for failure to inline subprograms, as otherwise
         --  expected in GNATprove mode for the local subprograms without
         --  contracts.

         Ineffective_Inline_Warnings := True;

Arnaud Charlet committed
470 471 472 473 474 475 476
         --  Disable front-end optimizations, to keep the tree as close to the
         --  source code as possible, and also to avoid inconsistencies between
         --  trees when using different optimization switches.

         Optimization_Level := 0;

         --  Enable some restrictions systematically to simplify the generated
477
         --  code (and ease analysis).
Arnaud Charlet committed
478 479 480 481 482 483 484 485 486

         Restrict.Restrictions.Set (No_Initialize_Scalars) := True;

         --  Note: at this point we used to suppress various checks, but that
         --  is not what we want. We need the semantic processing for these
         --  checks (which will set flags like Do_Overflow_Check, showing the
         --  points at which potential checks are required semantically). We
         --  don't want the expansion associated with these checks, but that
         --  happens anyway because this expansion is simply not done in the
487
         --  SPARK version of the expander.
Arnaud Charlet committed
488

Arnaud Charlet committed
489
         --  On the contrary, we need to enable explicitly all language checks,
Arnaud Charlet committed
490
         --  as they may have been suppressed by the use of switch -gnatp.
Arnaud Charlet committed
491 492 493

         Suppress_Options.Suppress := (others => False);

Arnaud Charlet committed
494 495
         --  Detect overflow on unconstrained floating-point types, such as
         --  the predefined types Float, Long_Float and Long_Long_Float from
Arnaud Charlet committed
496 497 498
         --  package Standard. Not necessary if float overflows are checked
         --  (Machine_Overflow true), since appropriate Do_Overflow_Check flags
         --  will be set in any case.
Arnaud Charlet committed
499

Arnaud Charlet committed
500
         Check_Float_Overflow := not Machine_Overflows_On_Target;
Arnaud Charlet committed
501

Arnaud Charlet committed
502 503 504 505
         --  Set STRICT mode for overflow checks if not set explicitly. This
         --  prevents suppressing of overflow checks by default, in code down
         --  below.

506 507 508
         if Suppress_Options.Overflow_Mode_General = Not_Set then
            Suppress_Options.Overflow_Mode_General    := Strict;
            Suppress_Options.Overflow_Mode_Assertions := Strict;
Arnaud Charlet committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
         end if;

         --  Kill debug of generated code, since it messes up sloc values

         Debug_Generated_Code := False;

         --  Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
         --  as it is needed for computing effects of subprograms in the formal
         --  verification backend.

         Xref_Active := True;

         --  Polling mode forced off, since it generates confusing junk

         Polling_Required := False;

525 526
         --  Set operating mode to Check_Semantics, but a light front-end
         --  expansion is still performed.
Arnaud Charlet committed
527

528
         Operating_Mode := Check_Semantics;
Arnaud Charlet committed
529

Arnaud Charlet committed
530 531
         --  Enable assertions, since they give valuable extra information for
         --  formal verification.
Arnaud Charlet committed
532

Arnaud Charlet committed
533
         Assertions_Enabled := True;
Arnaud Charlet committed
534

Arnaud Charlet committed
535 536 537 538 539
         --  Disable validity checks, since it generates code raising
         --  exceptions for invalid data, which confuses GNATprove. Invalid
         --  data is directly detected by GNATprove's flow analysis.

         Validity_Checks_On := False;
540
         Check_Validity_Of_Parameters := False;
Arnaud Charlet committed
541

Arnaud Charlet committed
542
         --  Turn off style check options since we are not interested in any
543
         --  front-end warnings when we are getting SPARK output.
Arnaud Charlet committed
544 545 546 547

         Reset_Style_Check_Options;

         --  Suppress the generation of name tables for enumerations, which are
548
         --  not needed for formal verification, and fall outside the SPARK
Arnaud Charlet committed
549 550 551 552 553
         --  subset (use of pointers).

         Global_Discard_Names := True;

         --  Suppress the expansion of tagged types and dispatching calls,
554
         --  which lead to the generation of non-SPARK code (use of pointers),
Arnaud Charlet committed
555 556 557
         --  which is more complex to formally verify than the original source.

         Tagged_Type_Expansion := False;
Arnaud Charlet committed
558 559 560 561

         --  Detect that the runtime library support for floating-point numbers
         --  may not be compatible with SPARK analysis of IEEE-754 floats.

562 563 564 565 566 567 568 569 570
         if Denorm_On_Target = False then
            SPARK_Library_Warning ("float subnormals");

         elsif Machine_Rounds_On_Target = False then
            SPARK_Library_Warning ("float rounding");

         elsif Signed_Zeros_On_Target = False then
            SPARK_Library_Warning ("signed zeros");
         end if;
Arnaud Charlet committed
571 572
      end if;

573 574
      --  Set Configurable_Run_Time mode if system.ads flag set or if the
      --  special debug flag -gnatdY is set.
Arnaud Charlet committed
575 576 577 578 579

      if Targparm.Configurable_Run_Time_On_Target or Debug_Flag_YY then
         Configurable_Run_Time_Mode := True;
      end if;

Arnaud Charlet committed
580
      --  Set -gnatRm mode if debug flag A set
Arnaud Charlet committed
581 582 583 584 585 586 587 588 589 590 591 592 593

      if Debug_Flag_AA then
         Back_Annotate_Rep_Info := True;
         List_Representation_Info := 1;
         List_Representation_Info_Mechanisms := True;
      end if;

      --  Force Target_Strict_Alignment true if debug flag -gnatd.a is set

      if Debug_Flag_Dot_A then
         Ttypes.Target_Strict_Alignment := True;
      end if;

Arnaud Charlet committed
594 595 596 597 598 599
      --  Increase size of allocated entities if debug flag -gnatd.N is set

      if Debug_Flag_Dot_NN then
         Atree.Num_Extension_Nodes := Atree.Num_Extension_Nodes + 1;
      end if;

600 601 602
      --  Disable static allocation of dispatch tables if -gnatd.t is enabled.
      --  The front end's layout phase currently treats types that have
      --  discriminant-dependent arrays as not being static even when a
Arnaud Charlet committed
603 604 605
      --  discriminant constraint on the type is static, and this leads to
      --  problems with subtypes of type Ada.Tags.Dispatch_Table_Wrapper. ???

606
      if Debug_Flag_Dot_T then
607
         Building_Static_Dispatch_Tables := False;
Arnaud Charlet committed
608 609 610 611 612 613 614 615
      end if;

      --  Flip endian mode if -gnatd8 set

      if Debug_Flag_8 then
         Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
      end if;

Arnaud Charlet committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
      --  Set and check exception mechanism. This is only meaningful when
      --  compiling, and in particular not meaningful for special modes used
      --  for program analysis rather than compilation: ASIS mode, CodePeer
      --  mode and GNATprove mode.

      if Operating_Mode = Generate_Code
        and then not (ASIS_Mode or CodePeer_Mode or GNATprove_Mode)
      then
         case Targparm.Frontend_Exceptions_On_Target is
            when True =>
               case Targparm.ZCX_By_Default_On_Target is
                  when True =>
                     Write_Line
                       ("Run-time library configured incorrectly");
                     Write_Line
                       ("(requesting support for Frontend ZCX exceptions)");
                     raise Unrecoverable_Error;
633

Arnaud Charlet committed
634 635 636
                  when False =>
                     Exception_Mechanism := Front_End_SJLJ;
               end case;
637

Arnaud Charlet committed
638 639 640 641 642 643 644 645 646
            when False =>
               case Targparm.ZCX_By_Default_On_Target is
                  when True =>
                     Exception_Mechanism := Back_End_ZCX;
                  when False =>
                     Exception_Mechanism := Back_End_SJLJ;
               end case;
         end case;
      end if;
Arnaud Charlet committed
647

648
      --  Set proper status for overflow check mechanism
Arnaud Charlet committed
649

650
      --  If already set (by -gnato or above in SPARK or CodePeer mode) then we
Arnaud Charlet committed
651
      --  have nothing to do.
Arnaud Charlet committed
652

653
      if Opt.Suppress_Options.Overflow_Mode_General /= Not_Set then
Arnaud Charlet committed
654 655
         null;

656
      --  Otherwise set overflow mode defaults
Arnaud Charlet committed
657

658
      else
659 660 661 662 663
         --  Overflow checks are on by default (Suppress set False) except in
         --  GNAT_Mode, where we want them off by default (we are not ready to
         --  enable overflow checks in the compiler yet, for one thing the case
         --  of 64-bit checks needs System.Arith_64 which is not a compiler
         --  unit and it is a pain to try to include it in the compiler.
Arnaud Charlet committed
664

665
         Suppress_Options.Suppress (Overflow_Check) := GNAT_Mode;
Arnaud Charlet committed
666

667 668 669
         --  Set appropriate default overflow handling mode. Note: at present
         --  we set STRICT in all three of the following cases. They are
         --  separated because in the future we may make different choices.
Arnaud Charlet committed
670

671
         --  By default set STRICT mode if -gnatg in effect
Arnaud Charlet committed
672

673
         if GNAT_Mode then
674 675
            Suppress_Options.Overflow_Mode_General    := Strict;
            Suppress_Options.Overflow_Mode_Assertions := Strict;
Arnaud Charlet committed
676

677 678 679 680 681 682
         --  If we have backend divide and overflow checks, then by default
         --  overflow checks are STRICT. Historically this code used to also
         --  activate overflow checks, although no target currently has these
         --  flags set, so this was dead code anyway.

         elsif Targparm.Backend_Divide_Checks_On_Target
683 684
                 and
               Targparm.Backend_Overflow_Checks_On_Target
685
         then
686 687
            Suppress_Options.Overflow_Mode_General    := Strict;
            Suppress_Options.Overflow_Mode_Assertions := Strict;
688 689 690 691 692 693

         --  Otherwise for now, default is STRICT mode. This may change in the
         --  future, but for now this is the compatible behavior with previous
         --  versions of GNAT.

         else
694 695
            Suppress_Options.Overflow_Mode_General    := Strict;
            Suppress_Options.Overflow_Mode_Assertions := Strict;
696
         end if;
Arnaud Charlet committed
697
      end if;
Arnaud Charlet committed
698

Arnaud Charlet committed
699 700 701 702 703
      --  Set default for atomic synchronization. As this synchronization
      --  between atomic accesses can be expensive, and not typically needed
      --  on some targets, an optional target parameter can turn the option
      --  off. Note Atomic Synchronization is implemented as check.

Arnaud Charlet committed
704
      Suppress_Options.Suppress (Atomic_Synchronization) :=
Arnaud Charlet committed
705
        not Atomic_Sync_Default_On_Target;
Arnaud Charlet committed
706

707 708 709 710 711 712 713 714
      --  Set default for Alignment_Check, if we are on a machine with non-
      --  strict alignment, then we suppress this check, since it is over-
      --  zealous for such machines.

      if not Ttypes.Target_Strict_Alignment then
         Suppress_Options.Suppress (Alignment_Check) := True;
      end if;

Arnaud Charlet committed
715 716
      --  Set switch indicating if back end can handle limited types, and
      --  guarantee that no incorrect copies are made (e.g. in the context
717
      --  of an if or case expression).
Arnaud Charlet committed
718 719 720

      --  Debug flag -gnatd.L decisively sets usage on

721
      if Debug_Flag_Dot_LL then
Arnaud Charlet committed
722 723
         Back_End_Handles_Limited_Types := True;

Arnaud Charlet committed
724
      --  If no debug flag, usage off for SCIL cases
Arnaud Charlet committed
725

Arnaud Charlet committed
726
      elsif Generate_SCIL then
Arnaud Charlet committed
727 728
         Back_End_Handles_Limited_Types := False;

Arnaud Charlet committed
729
      --  Otherwise normal gcc back end, for now still turn flag off by
Arnaud Charlet committed
730
      --  default, since there are unresolved problems in the front end.
Arnaud Charlet committed
731 732 733 734

      else
         Back_End_Handles_Limited_Types := False;
      end if;
735

Arnaud Charlet committed
736 737 738 739 740 741 742 743 744 745
      --  If the inlining level has not been set by the user, compute it from
      --  the optimization level: 1 at -O1/-O2 (and -Os), 2 at -O3 and above.

      if Inline_Level = 0 then
         if Optimization_Level < 3 then
            Inline_Level := 1;
         else
            Inline_Level := 2;
         end if;
      end if;
Arnaud Charlet committed
746

Arnaud Charlet committed
747 748
      --  Treat -gnatn as equivalent to -gnatN for non-GCC targets

Arnaud Charlet committed
749 750 751 752 753 754
      if Inline_Active and not Front_End_Inlining then

         --  We really should have a tag for this, what if we added a new
         --  back end some day, it would not be true for this test, but it
         --  would be non-GCC, so this is a bit troublesome ???

Arnaud Charlet committed
755
         Front_End_Inlining := Generate_C_Code;
Arnaud Charlet committed
756 757
      end if;

Arnaud Charlet committed
758
      --  Set back-end inlining indication
Arnaud Charlet committed
759 760

      Back_End_Inlining :=
761

Arnaud Charlet committed
762
        --  No back-end inlining available on C generation
Arnaud Charlet committed
763

Arnaud Charlet committed
764
        not Generate_C_Code
Arnaud Charlet committed
765

Arnaud Charlet committed
766
        --  No back-end inlining in GNATprove mode, since it just confuses
767 768 769 770
        --  the formal verification process.

        and then not GNATprove_Mode

Arnaud Charlet committed
771
        --  No back-end inlining if front-end inlining explicitly enabled.
772 773 774
        --  Done to minimize the output differences to customers still using
        --  this deprecated switch; in addition, this behavior reduces the
        --  output differences in old tests.
775 776 777

        and then not Front_End_Inlining

Arnaud Charlet committed
778
        --  Back-end inlining is disabled if debug flag .z is set
779

780
        and then not Debug_Flag_Dot_Z;
Arnaud Charlet committed
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
      --  Output warning if -gnateE specified and cannot be supported

      if Exception_Extra_Info
        and then Restrict.No_Exception_Handlers_Set
      then
         Set_Standard_Error;
         Write_Str
           ("warning: extra exception information (-gnateE) was specified");
         Write_Eol;
         Write_Str
           ("warning: this capability is not available in this configuration");
         Write_Eol;
         Set_Standard_Output;
      end if;

Arnaud Charlet committed
797 798 799 800 801
      --  Finally capture adjusted value of Suppress_Options as the initial
      --  value for Scope_Suppress, which will be modified as we move from
      --  scope to scope (by Suppress/Unsuppress/Overflow_Checks pragmas).

      Sem.Scope_Suppress := Opt.Suppress_Options;
Arnaud Charlet committed
802 803
   end Adjust_Global_Switches;

804 805 806 807
   --------------------
   -- Check_Bad_Body --
   --------------------

Arnaud Charlet committed
808 809
   procedure Check_Bad_Body (Unit_Node : Node_Id; Unit_Kind : Node_Kind) is
      Fname : File_Name_Type;
810 811 812 813 814 815 816 817 818 819

      procedure Bad_Body_Error (Msg : String);
      --  Issue message for bad body found

      --------------------
      -- Bad_Body_Error --
      --------------------

      procedure Bad_Body_Error (Msg : String) is
      begin
Arnaud Charlet committed
820
         Error_Msg_N (Msg, Unit_Node);
821
         Error_Msg_File_1 := Fname;
Arnaud Charlet committed
822
         Error_Msg_N ("remove incorrect body in file{!", Unit_Node);
823 824
      end Bad_Body_Error;

Arnaud Charlet committed
825 826 827 828 829
      --  Local variables

      Sname   : Unit_Name_Type;
      Src_Ind : Source_File_Index;

Arnaud Charlet committed
830
   --  Start of processing for Check_Bad_Body
831 832 833 834 835 836 837 838 839 840 841

   begin
      --  Nothing to do if we are only checking syntax, because we don't know
      --  enough to know if we require or forbid a body in this case.

      if Operating_Mode = Check_Syntax then
         return;
      end if;

      --  Check for body not allowed

Arnaud Charlet committed
842 843 844 845 846 847 848
      if (Unit_Kind = N_Package_Declaration
           and then not Body_Required (Unit_Node))
        or else (Unit_Kind = N_Generic_Package_Declaration
                  and then not Body_Required (Unit_Node))
        or else Unit_Kind = N_Package_Renaming_Declaration
        or else Unit_Kind = N_Subprogram_Renaming_Declaration
        or else Nkind (Original_Node (Unit (Unit_Node)))
849 850 851 852 853 854 855 856 857 858 859 860 861
                         in N_Generic_Instantiation
      then
         Sname := Unit_Name (Main_Unit);

         --  If we do not already have a body name, then get the body name

         if not Is_Body_Name (Sname) then
            Sname := Get_Body_Name (Sname);
         end if;

         Fname := Get_File_Name (Sname, Subunit => False);
         Src_Ind := Load_Source_File (Fname);

Arnaud Charlet committed
862 863 864 865 866 867 868 869 870
         --  Case where body is present and it is not a subunit. Exclude the
         --  subunit case, because it has nothing to do with the package we are
         --  compiling. It is illegal for a child unit and a subunit with the
         --  same expanded name (RM 10.2(9)) to appear together in a partition,
         --  but there is nothing to stop a compilation environment from having
         --  both, and the test here simply allows that. If there is an attempt
         --  to include both in a partition, this is diagnosed at bind time. In
         --  Ada 83 mode this is not a warning case.

Arnaud Charlet committed
871 872 873 874 875 876
         --  Note that in general we do not give the message if the file in
         --  question does not look like a body. This includes weird cases,
         --  but in particular means that if the file is just a No_Body pragma,
         --  then we won't give the message (that's the whole point of this
         --  pragma, to be used this way and to cause the body file to be
         --  ignored in this context).
877

878
         if Src_Ind > No_Source_File
Arnaud Charlet committed
879
           and then Source_File_Is_Body (Src_Ind)
880
         then
881
            Errout.Finalize (Last_Call => False);
882 883 884 885 886 887 888 889 890 891

            Error_Msg_Unit_1 := Sname;

            --  Ada 83 case of a package body being ignored. This is not an
            --  error as far as the Ada 83 RM is concerned, but it is almost
            --  certainly not what is wanted so output a warning. Give this
            --  message only if there were no errors, since otherwise it may
            --  be incorrect (we may have misinterpreted a junk spec as not
            --  needing a body when it really does).

Arnaud Charlet committed
892
            if Unit_Kind = N_Package_Declaration
893 894 895 896 897 898
              and then Ada_Version = Ada_83
              and then Operating_Mode = Generate_Code
              and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
              and then not Compilation_Errors
            then
               Error_Msg_N
Arnaud Charlet committed
899
                 ("package $$ does not require a body??", Unit_Node);
900
               Error_Msg_File_1 := Fname;
Arnaud Charlet committed
901
               Error_Msg_N ("body in file{ will be ignored??", Unit_Node);
902 903 904 905 906 907 908

               --  Ada 95 cases of a body file present when no body is
               --  permitted. This we consider to be an error.

            else
               --  For generic instantiations, we never allow a body

Arnaud Charlet committed
909
               if Nkind (Original_Node (Unit (Unit_Node))) in
Arnaud Charlet committed
910
                                                    N_Generic_Instantiation
911 912 913 914
               then
                  Bad_Body_Error
                    ("generic instantiation for $$ does not allow a body");

Arnaud Charlet committed
915
               --  A library unit that is a renaming never allows a body
916

Arnaud Charlet committed
917
               elsif Unit_Kind in N_Renaming_Declaration then
918 919 920 921 922 923
                  Bad_Body_Error
                    ("renaming declaration for $$ does not allow a body!");

                  --  Remaining cases are packages and generic packages. Here
                  --  we only do the test if there are no previous errors,
                  --  because if there are errors, they may lead us to
924 925
                  --  incorrectly believe that a package does not allow a
                  --  body when in fact it does.
926 927

               elsif not Compilation_Errors then
Arnaud Charlet committed
928
                  if Unit_Kind = N_Package_Declaration then
929 930 931
                     Bad_Body_Error
                       ("package $$ does not allow a body!");

Arnaud Charlet committed
932
                  elsif Unit_Kind = N_Generic_Package_Declaration then
933 934 935 936 937 938 939 940 941 942
                     Bad_Body_Error
                       ("generic package $$ does not allow a body!");
                  end if;
               end if;

            end if;
         end if;
      end if;
   end Check_Bad_Body;

Arnaud Charlet committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
   --------------------
   -- Check_Rep_Info --
   --------------------

   procedure Check_Rep_Info is
   begin
      if List_Representation_Info /= 0
        or else List_Representation_Info_Mechanisms
      then
         Set_Standard_Error;
         Write_Eol;
         Write_Str
           ("cannot generate representation information, no code generated");
         Write_Eol;
         Write_Eol;
         Set_Standard_Output;
      end if;
   end Check_Rep_Info;

Arnaud Charlet committed
962 963 964 965 966 967 968 969 970 971 972 973 974
   ----------------------------------------
   -- Post_Compilation_Validation_Checks --
   ----------------------------------------

   procedure Post_Compilation_Validation_Checks is
   begin
      --  Validate alignment check warnings. In some cases we generate warnings
      --  about possible alignment errors because we don't know the alignment
      --  that will be chosen by the back end. This routine is in charge of
      --  getting rid of those warnings if we can tell they are not needed.

      Checks.Validate_Alignment_Check_Warnings;

975 976 977 978
      --  Validate compile time warnings and errors (using the values for size
      --  and alignment annotated by the backend where possible). We need to
      --  unlock temporarily these tables to reanalyze their expression.

Arnaud Charlet committed
979 980 981 982 983 984 985
      Atree.Unlock;
      Nlists.Unlock;
      Sem.Unlock;
      Sem_Ch13.Validate_Compile_Time_Warning_Errors;
      Sem.Lock;
      Nlists.Lock;
      Atree.Lock;
986

Arnaud Charlet committed
987 988 989 990 991 992 993 994 995 996
      --  Validate unchecked conversions (using the values for size and
      --  alignment annotated by the backend where possible).

      Sem_Ch13.Validate_Unchecked_Conversions;

      --  Validate address clauses (again using alignment values annotated
      --  by the backend where possible).

      Sem_Ch13.Validate_Address_Clauses;

Arnaud Charlet committed
997 998 999
      --  Validate independence pragmas (again using values annotated by the
      --  back end for component layout where possible) but only for non-GCC
      --  back ends, as this is done a priori for GCC back ends.
1000 1001 1002 1003 1004
      --  ??? We use to test for AAMP_On_Target which is now gone, consider
      --
      --  if AAMP_On_Target then
      --     Sem_Ch13.Validate_Independence;
      --  end if;
Arnaud Charlet committed
1005 1006
   end Post_Compilation_Validation_Checks;

Arnaud Charlet committed
1007 1008 1009
   --  Local variables

   Back_End_Mode : Back_End.Back_End_Mode_Type;
1010
   Ecode         : Exit_Code_Type;
Arnaud Charlet committed
1011 1012 1013 1014 1015 1016 1017

   Main_Unit_Kind : Node_Kind;
   --  Kind of main compilation unit node

   Main_Unit_Node : Node_Id;
   --  Compilation unit node for main unit

1018 1019
--  Start of processing for Gnat1drv

Richard Kenner committed
1020 1021 1022 1023 1024 1025 1026
begin
   --  This inner block is set up to catch assertion errors and constraint
   --  errors. Since the code for handling these errors can cause another
   --  exception to be raised (namely Unrecoverable_Error), we need two
   --  nested blocks, so that the outer one handles unrecoverable error.

   begin
Arnaud Charlet committed
1027 1028 1029
      --  Initialize all packages. For the most part, these initialization
      --  calls can be made in any order. Exceptions are as follows:

1030
      --  Lib.Initialize need to be called before Scan_Compiler_Arguments,
1031
      --  because it initializes a table filled by Scan_Compiler_Arguments.
1032

1033 1034
      Osint.Initialize;
      Fmap.Reset_Tables;
1035
      Lib.Initialize;
1036
      Lib.Xref.Initialize;
Richard Kenner committed
1037 1038
      Scan_Compiler_Arguments;
      Osint.Add_Default_Search_Dirs;
Arnaud Charlet committed
1039
      Atree.Initialize;
1040
      Nlists.Initialize;
Richard Kenner committed
1041 1042
      Sinput.Initialize;
      Sem.Initialize;
1043
      Exp_CG.Initialize;
Richard Kenner committed
1044 1045 1046 1047
      Csets.Initialize;
      Uintp.Initialize;
      Urealp.Initialize;
      Errout.Initialize;
Arnaud Charlet committed
1048
      SCOs.Initialize;
Richard Kenner committed
1049 1050
      Snames.Initialize;
      Stringt.Initialize;
1051
      Ghost.Initialize;
Richard Kenner committed
1052
      Inline.Initialize;
Arnaud Charlet committed
1053
      Par_SCO.Initialize;
1054 1055
      Sem_Ch8.Initialize;
      Sem_Ch12.Initialize;
Richard Kenner committed
1056
      Sem_Ch13.Initialize;
Arnaud Charlet committed
1057
      Sem_Elim.Initialize;
1058 1059 1060
      Sem_Eval.Initialize;
      Sem_Type.Init_Interp_Tables;

Arnaud Charlet committed
1061 1062 1063 1064
      --  Capture compilation date and time

      Opt.Compilation_Time := System.OS_Lib.Current_Time_String;

Arnaud Charlet committed
1065 1066
      --  Get the target parameters only when -gnats is not used, to avoid
      --  failing when there is no default runtime.
1067

Arnaud Charlet committed
1068
      if Operating_Mode /= Check_Syntax then
1069

Arnaud Charlet committed
1070
         --  Acquire target parameters from system.ads (package System source)
1071

Arnaud Charlet committed
1072 1073
         Targparm_Acquire : declare
            use Sinput;
1074

Arnaud Charlet committed
1075 1076
            S : Source_File_Index;
            N : File_Name_Type;
1077

Arnaud Charlet committed
1078 1079 1080 1081 1082
         begin
            Name_Buffer (1 .. 10) := "system.ads";
            Name_Len := 10;
            N := Name_Find;
            S := Load_Source_File (N);
1083

Arnaud Charlet committed
1084
            --  Failed to read system.ads, fatal error
Richard Kenner committed
1085

Arnaud Charlet committed
1086 1087 1088 1089 1090
            if S = No_Source_File then
               Write_Line
                 ("fatal error, run-time library not installed correctly");
               Write_Line ("cannot locate file system.ads");
               raise Unrecoverable_Error;
1091

1092 1093 1094 1095 1096
            elsif S = No_Access_To_Source_File then
               Write_Line
                 ("fatal error, run-time library not installed correctly");
               Write_Line ("no read access for file system.ads");
               raise Unrecoverable_Error;
1097

Arnaud Charlet committed
1098 1099 1100 1101 1102
            --  Read system.ads successfully, remember its source index

            else
               System_Source_File_Index := S;
            end if;
1103

Arnaud Charlet committed
1104
            --  Call to get target parameters. Note that the actual interface
Arnaud Charlet committed
1105 1106
            --  routines are in Tbuild. They can't be in this procedure because
            --  of accessibility issues.
Arnaud Charlet committed
1107

Arnaud Charlet committed
1108 1109 1110 1111 1112 1113
            Targparm.Get_Target_Parameters
              (System_Text  => Source_Text  (S),
               Source_First => Source_First (S),
               Source_Last  => Source_Last  (S),
               Make_Id      => Tbuild.Make_Id'Access,
               Make_SC      => Tbuild.Make_SC'Access,
Arnaud Charlet committed
1114 1115 1116 1117
               Set_NOD      => Tbuild.Set_NOD'Access,
               Set_NSA      => Tbuild.Set_NSA'Access,
               Set_NUA      => Tbuild.Set_NUA'Access,
               Set_NUP      => Tbuild.Set_NUP'Access);
Arnaud Charlet committed
1118 1119 1120 1121 1122 1123

            --  Acquire configuration pragma information from Targparm

            Restrict.Restrictions := Targparm.Restrictions_On_Target;
         end Targparm_Acquire;
      end if;
Arnaud Charlet committed
1124 1125

      --  Perform various adjustments and settings of global switches
1126

Arnaud Charlet committed
1127
      Adjust_Global_Switches;
1128

1129
      --  Output copyright notice if full list mode unless we have a list
Arnaud Charlet committed
1130
      --  file, in which case we defer this so that it is output in the file.
Richard Kenner committed
1131

1132
      if (Verbose_Mode or else (Full_List and then Full_List_File_Name = null))
Arnaud Charlet committed
1133 1134 1135

        --  Debug flag gnatd7 suppresses this copyright notice

1136
        and then not Debug_Flag_7
Richard Kenner committed
1137 1138 1139
      then
         Write_Eol;
         Write_Str ("GNAT ");
1140
         Write_Str (Gnat_Version_String);
1141
         Write_Eol;
Arnaud Charlet committed
1142 1143
         Write_Str ("Copyright 1992-" & Current_Year
                    & ", Free Software Foundation, Inc.");
1144
         Write_Eol;
Richard Kenner committed
1145 1146
      end if;

1147 1148 1149
      --  Check we do not have more than one source file, this happens only in
      --  the case where the driver is called directly, it cannot happen when
      --  gnat1 is invoked from gcc in the normal case.
Richard Kenner committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

      if Osint.Number_Of_Files /= 1 then
         Usage;
         Write_Eol;
         Osint.Fail ("you must provide one source file");

      elsif Usage_Requested then
         Usage;
      end if;

Arnaud Charlet committed
1160 1161
      --  Generate target dependent output file if requested

1162
      if Target_Dependent_Info_Write_Name /= null then
Arnaud Charlet committed
1163 1164 1165 1166 1167
         Set_Targ.Write_Target_Dependent_Values;
      end if;

      --  Call the front end

Richard Kenner committed
1168 1169 1170
      Original_Operating_Mode := Operating_Mode;
      Frontend;

Arnaud Charlet committed
1171
      --  Exit with errors if the main source could not be parsed
Arnaud Charlet committed
1172

1173
      if Sinput.Main_Source_File <= No_Source_File then
1174
         Errout.Finalize (Last_Call => True);
1175 1176
         Errout.Output_Messages;
         Exit_Program (E_Errors);
Richard Kenner committed
1177 1178
      end if;

1179
      Main_Unit_Node := Cunit (Main_Unit);
Arnaud Charlet committed
1180 1181 1182
      Main_Unit_Kind := Nkind (Unit (Main_Unit_Node));

      Check_Bad_Body (Main_Unit_Node, Main_Unit_Kind);
1183

Arnaud Charlet committed
1184 1185 1186 1187 1188 1189 1190 1191
      --  In CodePeer mode we always delete old SCIL files before regenerating
      --  new ones, in case of e.g. errors, and also to remove obsolete scilx
      --  files generated by CodePeer itself.

      if CodePeer_Mode then
         Comperr.Delete_SCIL_Files;
      end if;

Arnaud Charlet committed
1192 1193 1194 1195 1196 1197 1198
      --  Ditto for old C files before regenerating new ones

      if Generate_C_Code then
         Delete_C_File;
         Delete_H_File;
      end if;

Richard Kenner committed
1199 1200
      --  Exit if compilation errors detected

1201
      Errout.Finalize (Last_Call => False);
1202

Richard Kenner committed
1203 1204
      if Compilation_Errors then
         Treepr.Tree_Dump;
Arnaud Charlet committed
1205
         Post_Compilation_Validation_Checks;
1206
         Errout.Finalize (Last_Call => True);
1207
         Errout.Output_Messages;
Richard Kenner committed
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
         Namet.Finalize;

         --  Generate ALI file if specially requested

         if Opt.Force_ALI_Tree_File then
            Write_ALI (Object => False);
            Tree_Gen;
         end if;

         Exit_Program (E_Errors);
      end if;

1220 1221 1222
      --  Set Generate_Code on main unit and its spec. We do this even if are
      --  not generating code, since Lib-Writ uses this to determine which
      --  units get written in the ali file.
Richard Kenner committed
1223 1224 1225

      Set_Generate_Code (Main_Unit);

Arnaud Charlet committed
1226 1227 1228
      --  If we have a corresponding spec, and it comes from source or it is
      --  not a generated spec for a child subprogram body, then we need object
      --  code for the spec unit as well.
Richard Kenner committed
1229 1230 1231 1232

      if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
        and then not Acts_As_Spec (Main_Unit_Node)
      then
Arnaud Charlet committed
1233
         if Nkind (Unit (Main_Unit_Node)) = N_Subprogram_Body
1234 1235 1236 1237 1238 1239 1240
           and then not Comes_From_Source (Library_Unit (Main_Unit_Node))
         then
            null;
         else
            Set_Generate_Code
              (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
         end if;
Richard Kenner committed
1241 1242 1243 1244 1245 1246
      end if;

      --  Case of no code required to be generated, exit indicating no error

      if Original_Operating_Mode = Check_Syntax then
         Treepr.Tree_Dump;
1247
         Errout.Finalize (Last_Call => True);
1248
         Errout.Output_Messages;
Richard Kenner committed
1249 1250
         Tree_Gen;
         Namet.Finalize;
1251
         Check_Rep_Info;
1252 1253 1254 1255 1256

         --  Use a goto instead of calling Exit_Program so that finalization
         --  occurs normally.

         goto End_Of_Program;
Richard Kenner committed
1257 1258 1259 1260 1261

      elsif Original_Operating_Mode = Check_Semantics then
         Back_End_Mode := Declarations_Only;

      --  All remaining cases are cases in which the user requested that code
Arnaud Charlet committed
1262 1263
      --  be generated (i.e. no -gnatc or -gnats switch was used). Check if we
      --  can in fact satisfy this request.
Richard Kenner committed
1264

1265 1266
      --  Cannot generate code if someone has turned off code generation for
      --  any reason at all. We will try to figure out a reason below.
Richard Kenner committed
1267 1268 1269 1270

      elsif Operating_Mode /= Generate_Code then
         Back_End_Mode := Skip;

1271 1272 1273
      --  We can generate code for a subprogram body unless there were missing
      --  subunits. Note that we always generate code for all generic units (a
      --  change from some previous versions of GNAT).
Richard Kenner committed
1274

Arnaud Charlet committed
1275 1276 1277
      elsif Main_Unit_Kind = N_Subprogram_Body
        and then not Subunits_Missing
      then
Richard Kenner committed
1278 1279
         Back_End_Mode := Generate_Object;

1280 1281 1282
      --  We can generate code for a package body unless there are subunits
      --  missing (note that we always generate code for generic units, which
      --  is a change from some earlier versions of GNAT).
Richard Kenner committed
1283

Arnaud Charlet committed
1284
      elsif Main_Unit_Kind = N_Package_Body and then not Subunits_Missing then
Richard Kenner committed
1285 1286 1287 1288 1289
         Back_End_Mode := Generate_Object;

      --  We can generate code for a package declaration or a subprogram
      --  declaration only if it does not required a body.

Arnaud Charlet committed
1290 1291
      elsif Nkind_In (Main_Unit_Kind, N_Package_Declaration,
                                      N_Subprogram_Declaration)
Richard Kenner committed
1292 1293
        and then
          (not Body_Required (Main_Unit_Node)
Arnaud Charlet committed
1294
             or else Distribution_Stub_Mode = Generate_Caller_Stub_Body)
Richard Kenner committed
1295 1296 1297 1298
      then
         Back_End_Mode := Generate_Object;

      --  We can generate code for a generic package declaration of a generic
1299
      --  subprogram declaration only if does not require a body.
Richard Kenner committed
1300

Arnaud Charlet committed
1301 1302
      elsif Nkind_In (Main_Unit_Kind, N_Generic_Package_Declaration,
                                      N_Generic_Subprogram_Declaration)
Richard Kenner committed
1303 1304 1305 1306
        and then not Body_Required (Main_Unit_Node)
      then
         Back_End_Mode := Generate_Object;

Arnaud Charlet committed
1307 1308
      --  Compilation units that are renamings do not require bodies, so we can
      --  generate code for them.
Richard Kenner committed
1309

Arnaud Charlet committed
1310 1311
      elsif Nkind_In (Main_Unit_Kind, N_Package_Renaming_Declaration,
                                      N_Subprogram_Renaming_Declaration)
Richard Kenner committed
1312 1313 1314 1315
      then
         Back_End_Mode := Generate_Object;

      --  Compilation units that are generic renamings do not require bodies
1316
      --  so we can generate code for them.
Richard Kenner committed
1317

Arnaud Charlet committed
1318
      elsif Main_Unit_Kind in N_Generic_Renaming_Declaration then
Richard Kenner committed
1319 1320
         Back_End_Mode := Generate_Object;

Arnaud Charlet committed
1321 1322
      --  It is not an error to analyze in CodePeer mode a spec which requires
      --  a body, in order to generate SCIL for this spec.
Arnaud Charlet committed
1323
      --  Ditto for Generate_C_Code mode and generate a C header for a spec.
Arnaud Charlet committed
1324

Arnaud Charlet committed
1325
      elsif CodePeer_Mode or Generate_C_Code then
Arnaud Charlet committed
1326 1327
         Back_End_Mode := Generate_Object;

1328 1329
      --  It is not an error to analyze in GNATprove mode a spec which requires
      --  a body, when the body is not available. During frame condition
Arnaud Charlet committed
1330
      --  generation, the corresponding ALI file is generated. During
Arnaud Charlet committed
1331
      --  analysis, the spec is analyzed.
Arnaud Charlet committed
1332

1333 1334
      elsif GNATprove_Mode then
         Back_End_Mode := Declarations_Only;
Arnaud Charlet committed
1335

Richard Kenner committed
1336 1337 1338 1339 1340 1341 1342 1343 1344
      --  In all other cases (specs which have bodies, generics, and bodies
      --  where subunits are missing), we cannot generate code and we generate
      --  a warning message. Note that generic instantiations are gone at this
      --  stage since they have been replaced by their instances.

      else
         Back_End_Mode := Skip;
      end if;

Arnaud Charlet committed
1345 1346 1347
      --  At this stage Back_End_Mode is set to indicate if the backend should
      --  be called to generate code. If it is Skip, then code generation has
      --  been turned off, even though code was requested by the original
1348 1349 1350
      --  command. This is not an error from the user point of view, but it is
      --  an error from the point of view of the gcc driver, so we must exit
      --  with an error status.
Richard Kenner committed
1351

1352 1353 1354 1355
      --  We generate an informative message (from the gcc point of view, it
      --  is an error message, but from the users point of view this is not an
      --  error, just a consequence of compiling something that cannot
      --  generate code).
Richard Kenner committed
1356 1357 1358

      if Back_End_Mode = Skip then

Arnaud Charlet committed
1359 1360 1361
         --  An ignored Ghost unit is rewritten into a null statement because
         --  it must not produce an ALI or object file. Do not emit any errors
         --  related to code generation because the unit does not exist.
Richard Kenner committed
1362

1363 1364 1365 1366 1367 1368 1369 1370
         if Is_Ignored_Ghost_Unit (Main_Unit_Node) then

            --  Exit the gnat driver with success, otherwise external builders
            --  such as gnatmake and gprbuild will treat the compilation of an
            --  ignored Ghost unit as a failure. Note that this will produce
            --  an empty object file for the unit.

            Ecode := E_Success;
Arnaud Charlet committed
1371

Arnaud Charlet committed
1372 1373
         --  Otherwise the unit is missing a crucial piece that prevents code
         --  generation.
Arnaud Charlet committed
1374

Arnaud Charlet committed
1375
         else
1376 1377
            Ecode := E_No_Code;

Arnaud Charlet committed
1378 1379 1380
            Set_Standard_Error;
            Write_Str ("cannot generate code for file ");
            Write_Name (Unit_File_Name (Main_Unit));
Richard Kenner committed
1381

Arnaud Charlet committed
1382 1383 1384
            if Subunits_Missing then
               Write_Str (" (missing subunits)");
               Write_Eol;
Arnaud Charlet committed
1385

Arnaud Charlet committed
1386
               --  Force generation of ALI file, for backward compatibility
Arnaud Charlet committed
1387

Arnaud Charlet committed
1388
               Opt.Force_ALI_Tree_File := True;
1389

Arnaud Charlet committed
1390 1391 1392
            elsif Main_Unit_Kind = N_Subunit then
               Write_Str (" (subunit)");
               Write_Eol;
1393

Arnaud Charlet committed
1394 1395 1396
               --  Do not generate an ALI file in this case, because it would
               --  become obsolete when the parent is compiled, and thus
               --  confuse tools such as gnatfind.
Arnaud Charlet committed
1397

Arnaud Charlet committed
1398 1399 1400
            elsif Main_Unit_Kind = N_Subprogram_Declaration then
               Write_Str (" (subprogram spec)");
               Write_Eol;
Arnaud Charlet committed
1401

Arnaud Charlet committed
1402
            --  Generic package body in GNAT implementation mode
Richard Kenner committed
1403

Arnaud Charlet committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
            elsif Main_Unit_Kind = N_Package_Body and then GNAT_Mode then
               Write_Str (" (predefined generic)");
               Write_Eol;

               --  Force generation of ALI file, for backward compatibility

               Opt.Force_ALI_Tree_File := True;

            --  Only other case is a package spec

            else
               Write_Str (" (package spec)");
               Write_Eol;
            end if;
1418 1419
         end if;

1420
         Set_Standard_Output;
Richard Kenner committed
1421

Arnaud Charlet committed
1422
         Post_Compilation_Validation_Checks;
1423
         Errout.Finalize (Last_Call => True);
1424
         Errout.Output_Messages;
Richard Kenner committed
1425 1426
         Treepr.Tree_Dump;
         Tree_Gen;
Arnaud Charlet committed
1427 1428 1429 1430 1431 1432 1433 1434

         --  Generate ALI file if specially requested, or for missing subunits,
         --  subunits or predefined generic.

         if Opt.Force_ALI_Tree_File then
            Write_ALI (Object => False);
         end if;

Richard Kenner committed
1435
         Namet.Finalize;
1436
         Check_Rep_Info;
Richard Kenner committed
1437

1438 1439 1440
         --  Exit the driver with an appropriate status indicator. This will
         --  generate an empty object file for ignored Ghost units, otherwise
         --  no object file will be generated.
Richard Kenner committed
1441

1442
         Exit_Program (Ecode);
Richard Kenner committed
1443 1444
      end if;

Arnaud Charlet committed
1445 1446
      --  In -gnatc mode, we only do annotation if -gnatt or -gnatR is also set
      --  as indicated by Back_Annotate_Rep_Info being set to True.
Richard Kenner committed
1447 1448

      --  We don't call for annotations on a subunit, because to process those
Arnaud Charlet committed
1449
      --  the back end requires that the parent(s) be properly compiled.
Richard Kenner committed
1450

1451 1452 1453
      --  Annotation is suppressed for targets where front-end layout is
      --  enabled, because the front end determines representations.

Arnaud Charlet committed
1454 1455
      --  The back end is not invoked in ASIS mode with GNSA because all type
      --  representation information will be provided by the GNSA back end, not
Arnaud Charlet committed
1456 1457
      --  gigi.

Richard Kenner committed
1458
      if Back_End_Mode = Declarations_Only
Arnaud Charlet committed
1459 1460
        and then
          (not (Back_Annotate_Rep_Info or Generate_SCIL or GNATprove_Mode)
Arnaud Charlet committed
1461
            or else Main_Unit_Kind = N_Subunit
Arnaud Charlet committed
1462
            or else ASIS_GNSA_Mode)
Richard Kenner committed
1463
      then
Arnaud Charlet committed
1464
         Post_Compilation_Validation_Checks;
1465
         Errout.Finalize (Last_Call => True);
1466
         Errout.Output_Messages;
Richard Kenner committed
1467 1468 1469 1470
         Write_ALI (Object => False);
         Tree_Dump;
         Tree_Gen;
         Namet.Finalize;
1471
         Check_Rep_Info;
Richard Kenner committed
1472 1473 1474
         return;
      end if;

1475 1476 1477
      --  Ensure that we properly register a dependency on system.ads, since
      --  even if we do not semantically depend on this, Targparm has read
      --  system parameters from the system.ads file.
Richard Kenner committed
1478 1479 1480

      Lib.Writ.Ensure_System_Dependency;

1481 1482 1483 1484 1485
      --  Add dependencies, if any, on preprocessing data file and on
      --  preprocessing definition file(s).

      Prepcomp.Add_Dependencies;

Arnaud Charlet committed
1486 1487
      if GNATprove_Mode then

Arnaud Charlet committed
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
         --  Perform the new SPARK checking rules for pointer aliasing. This is
         --  only activated in GNATprove mode and on SPARK code.

         if Debug_Flag_FF then
            Check_Safe_Pointers (Main_Unit_Node);
         end if;

         --  In GNATprove mode we're writing the ALI much earlier than usual
         --  as flow analysis needs the file present in order to append its
         --  own globals to it.

Arnaud Charlet committed
1499 1500 1501 1502 1503 1504 1505
         --  Note: In GNATprove mode, an "object" file is always generated as
         --  the result of calling gnat1 or gnat2why, although this is not the
         --  same as the object file produced for compilation.

         Write_ALI (Object => True);
      end if;

Arnaud Charlet committed
1506 1507 1508 1509 1510 1511 1512
      --  Some back ends (for instance Gigi) are known to rely on SCOs for code
      --  generation. Make sure they are available.

      if Generate_SCO then
         Par_SCO.SCO_Record_Filtered;
      end if;

Richard Kenner committed
1513 1514 1515 1516 1517
      --  Back end needs to explicitly unlock tables it needs to touch

      Atree.Lock;
      Elists.Lock;
      Fname.UF.Lock;
1518
      Ghost.Lock;
Richard Kenner committed
1519 1520
      Inline.Lock;
      Lib.Lock;
1521
      Namet.Lock;
Richard Kenner committed
1522 1523 1524 1525 1526
      Nlists.Lock;
      Sem.Lock;
      Sinput.Lock;
      Stringt.Lock;

1527
      --  Here we call the back end to generate the output code
Richard Kenner committed
1528

1529
      Generating_Code := True;
Richard Kenner committed
1530 1531
      Back_End.Call_Back_End (Back_End_Mode);

1532
      --  Once the backend is complete, we unlock the names table. This call
Arnaud Charlet committed
1533 1534
      --  allows a few extra entries, needed for example for the file name
      --  for the library file output.
Richard Kenner committed
1535 1536 1537

      Namet.Unlock;

1538 1539 1540 1541
      --  Generate the call-graph output of dispatching calls

      Exp_CG.Generate_CG_Output;

Arnaud Charlet committed
1542
      --  Perform post compilation validation checks
1543

Arnaud Charlet committed
1544
      Post_Compilation_Validation_Checks;
Arnaud Charlet committed
1545

1546 1547 1548 1549
      --  Now we complete output of errors, rep info and the tree info. These
      --  are delayed till now, since it is perfectly possible for gigi to
      --  generate errors, modify the tree (in particular by setting flags
      --  indicating that elaboration is required, and also to back annotate
Arnaud Charlet committed
1550
      --  representation information for List_Rep_Info).
Richard Kenner committed
1551

1552
      Errout.Finalize (Last_Call => True);
1553
      Errout.Output_Messages;
Arnaud Charlet committed
1554
      List_Rep_Info (Ttypes.Bytes_Big_Endian);
Arnaud Charlet committed
1555
      Inline.List_Inlining_Info;
Richard Kenner committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565

      --  Only write the library if the backend did not generate any error
      --  messages. Otherwise signal errors to the driver program so that
      --  there will be no attempt to generate an object file.

      if Compilation_Errors then
         Treepr.Tree_Dump;
         Exit_Program (E_Errors);
      end if;

Arnaud Charlet committed
1566 1567 1568
      if not GNATprove_Mode then
         Write_ALI (Object => (Back_End_Mode = Generate_Object));
      end if;
Richard Kenner committed
1569

Arnaud Charlet committed
1570
      if not Compilation_Errors then
Arnaud Charlet committed
1571

Arnaud Charlet committed
1572
         --  In case of ada backends, we need to make sure that the generated
Arnaud Charlet committed
1573 1574 1575 1576
         --  object file has a timestamp greater than the ALI file. We do this
         --  to make gnatmake happy when checking the ALI and obj timestamps,
         --  where it expects the object file being written after the ali file.

Arnaud Charlet committed
1577 1578 1579 1580
         --  Gnatmake's assumption is true for gcc platforms where the gcc
         --  wrapper needs to call the assembler after calling gnat1, but is
         --  not true for ada backends, where the object files are created
         --  directly by gnat1 (so are created before the ali file).
Arnaud Charlet committed
1581

Arnaud Charlet committed
1582 1583 1584
         Back_End.Gen_Or_Update_Object_File;
      end if;

Arnaud Charlet committed
1585 1586 1587 1588
      --  Generate ASIS tree after writing the ALI file, since in ASIS mode,
      --  Write_ALI may in fact result in further tree decoration from the
      --  original tree file. Note that we dump the tree just before generating
      --  it, so that the dump will exactly reflect what is written out.
Richard Kenner committed
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

      Treepr.Tree_Dump;
      Tree_Gen;

      --  Finalize name table and we are all done

      Namet.Finalize;

   exception
      --  Handle fatal internal compiler errors

1600 1601 1602
      when Rtsfind.RE_Not_Available =>
         Comperr.Compiler_Abort ("RE_Not_Available");

Richard Kenner committed
1603 1604 1605 1606 1607 1608 1609 1610 1611
      when System.Assertions.Assert_Failure =>
         Comperr.Compiler_Abort ("Assert_Failure");

      when Constraint_Error =>
         Comperr.Compiler_Abort ("Constraint_Error");

      when Program_Error =>
         Comperr.Compiler_Abort ("Program_Error");

Arnaud Charlet committed
1612 1613
      --  Assume this is a bug. If it is real, the message will in any case
      --  say Storage_Error, giving a strong hint.
Richard Kenner committed
1614

Arnaud Charlet committed
1615
      when Storage_Error =>
Richard Kenner committed
1616
         Comperr.Compiler_Abort ("Storage_Error");
Arnaud Charlet committed
1617 1618 1619 1620 1621 1622

      when Unrecoverable_Error =>
         raise;

      when others =>
         Comperr.Compiler_Abort ("exception");
Richard Kenner committed
1623 1624
   end;

1625 1626 1627
   <<End_Of_Program>>
   null;

Arnaud Charlet committed
1628
--  The outer exception handler handles an unrecoverable error
Richard Kenner committed
1629 1630 1631

exception
   when Unrecoverable_Error =>
1632
      Errout.Finalize (Last_Call => True);
1633
      Errout.Output_Messages;
Richard Kenner committed
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644

      Set_Standard_Error;
      Write_Str ("compilation abandoned");
      Write_Eol;

      Set_Standard_Output;
      Source_Dump;
      Tree_Dump;
      Exit_Program (E_Errors);

end Gnat1drv;