OSDN Git Service

2010-06-17 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnat1drv.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             G N A T 1 D R V                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Back_End; use Back_End;
28 with Comperr;
29 with Csets;    use Csets;
30 with Debug;    use Debug;
31 with Elists;
32 with Errout;   use Errout;
33 with Fmap;
34 with Fname;    use Fname;
35 with Fname.UF; use Fname.UF;
36 with Frontend;
37 with Gnatvsn;  use Gnatvsn;
38 with Hostparm;
39 with Inline;
40 with Lib;      use Lib;
41 with Lib.Writ; use Lib.Writ;
42 with Lib.Xref;
43 with Namet;    use Namet;
44 with Nlists;
45 with Opt;      use Opt;
46 with Osint;    use Osint;
47 with Output;   use Output;
48 with Par_SCO;
49 with Prepcomp;
50 with Repinfo;  use Repinfo;
51 with Restrict;
52 with Rtsfind;
53 with SCOs;
54 with Sem;
55 with Sem_Ch8;
56 with Sem_Ch12;
57 with Sem_Ch13;
58 with Sem_Elim;
59 with Sem_Eval;
60 with Sem_Type;
61 with Sinfo;    use Sinfo;
62 with Sinput.L; use Sinput.L;
63 with Snames;
64 with Sprint;   use Sprint;
65 with Stringt;
66 with Stylesw;  use Stylesw;
67 with Targparm; use Targparm;
68 with Tree_Gen;
69 with Treepr;   use Treepr;
70 with Ttypes;
71 with Types;    use Types;
72 with Uintp;    use Uintp;
73 with Uname;    use Uname;
74 with Urealp;
75 with Usage;
76 with Validsw;  use Validsw;
77
78 with System.Assertions;
79
80 procedure Gnat1drv is
81    Main_Unit_Node : Node_Id;
82    --  Compilation unit node for main unit
83
84    Main_Kind : Node_Kind;
85    --  Kind of main compilation unit node
86
87    Back_End_Mode : Back_End.Back_End_Mode_Type;
88    --  Record back end mode
89
90    procedure Adjust_Global_Switches;
91    --  There are various interactions between front end switch settings,
92    --  including debug switch settings and target dependent parameters.
93    --  This procedure takes care of properly handling these interactions.
94    --  We do it after scanning out all the switches, so that we are not
95    --  depending on the order in which switches appear.
96
97    procedure Check_Bad_Body;
98    --  Called to check if the unit we are compiling has a bad body
99
100    procedure Check_Rep_Info;
101    --  Called when we are not generating code, to check if -gnatR was requested
102    --  and if so, explain that we will not be honoring the request.
103
104    procedure Check_Library_Items;
105    --  For debugging -- checks the behavior of Walk_Library_Items
106    pragma Warnings (Off, Check_Library_Items);
107    --  In case the call below is commented out
108
109    ----------------------------
110    -- Adjust_Global_Switches --
111    ----------------------------
112
113    procedure Adjust_Global_Switches is
114    begin
115       --  Debug flag -gnatd.I is a synonym for Generate_SCIL and requires code
116       --  generation.
117
118       if Debug_Flag_Dot_II
119         and then Operating_Mode = Generate_Code
120       then
121          Generate_SCIL := True;
122       end if;
123
124       --  Set ASIS mode if -gnatt and -gnatc are set
125
126       if Operating_Mode = Check_Semantics and then Tree_Output then
127          ASIS_Mode := True;
128
129          --  Turn off inlining in ASIS mode, since ASIS cannot handle the extra
130          --  information in the trees caused by inlining being active.
131
132          --  More specifically, the tree seems to be malformed from the ASIS
133          --  point of view if -gnatc and -gnatn appear together???
134
135          Inline_Active := False;
136
137          --  Turn off SCIL generation in ASIS mode, since SCIL requires front-
138          --  end expansion.
139
140          Generate_SCIL := False;
141       end if;
142
143       --  SCIL mode needs to disable front-end inlining since the generated
144       --  trees (in particular order and consistency between specs compiled
145       --  as part of a main unit or as part of a with-clause) are causing
146       --  troubles.
147
148       if Generate_SCIL then
149          Front_End_Inlining := False;
150       end if;
151
152       --  Tune settings for optimal SCIL generation in CodePeer_Mode
153
154       if CodePeer_Mode then
155
156          --  Turn off inlining, confuses CodePeer output and gains nothing
157
158          Front_End_Inlining := False;
159          Inline_Active      := False;
160
161          --  Turn off ASIS mode: incompatible with front-end expansion
162
163          ASIS_Mode := False;
164
165          --  Disable front-end optimizations, to keep the tree as close to the
166          --  source code as possible, and also to avoid inconsistencies between
167          --  trees when using different optimization switches.
168
169          Optimization_Level := 0;
170
171          --  Disable specific expansions for Restrictions pragmas to avoid
172          --  tree inconsistencies between compilations with different pragmas
173          --  that will cause different SCIL files to be generated for the
174          --  same Ada spec.
175
176          Treat_Restrictions_As_Warnings := True;
177
178          --  Suppress overflow, division by zero and access checks since they
179          --  are handled implicitly by CodePeer.
180
181          --  Turn off dynamic elaboration checks: generates inconsistencies in
182          --  trees between specs compiled as part of a main unit or as part of
183          --  a with-clause.
184
185          --  Turn off alignment checks: these cannot be proved statically by
186          --  CodePeer and generate false positives.
187
188          --  Enable all other language checks
189
190          Suppress_Options :=
191            (Access_Check      => True,
192             Alignment_Check   => True,
193             Division_Check    => True,
194             Elaboration_Check => True,
195             Overflow_Check    => True,
196             others            => False);
197          Enable_Overflow_Checks := False;
198          Dynamic_Elaboration_Checks := False;
199
200          --  Kill debug of generated code, since it messes up sloc values
201
202          Debug_Generated_Code := False;
203
204          --  Turn cross-referencing on in case it was disabled (by e.g. -gnatD)
205          --  Do we really need to spend time generating xref in CodePeer
206          --  mode??? Consider setting Xref_Active to False.
207
208          Xref_Active := True;
209
210          --  Polling mode forced off, since it generates confusing junk
211
212          Polling_Required := False;
213
214          --  Set operating mode to Generate_Code to benefit from full
215          --  front-end expansion (e.g. generics).
216
217          Operating_Mode := Generate_Code;
218
219          --  We need SCIL generation of course
220
221          Generate_SCIL := True;
222
223          --  Enable assertions and debug pragmas, since they give CodePeer
224          --  valuable extra information.
225
226          Assertions_Enabled     := True;
227          Debug_Pragmas_Enabled  := True;
228
229          --  Suppress compiler warnings, since what we are interested in here
230          --  is what CodePeer can find out. Also disable all simple value
231          --  propagation. This is an optimization which is valuable for code
232          --  optimization, and also for generation of compiler warnings, but
233          --  these are being turned off anyway, and CodePeer understands
234          --  things more clearly if references are not optimized in this way.
235
236          Warning_Mode  := Suppress;
237          Debug_Flag_MM := True;
238
239          --  Set normal RM validity checking, and checking of IN OUT parameters
240          --  (this might give CodePeer more useful checks to analyze, to be
241          --  confirmed???). All other validity checking is turned off, since
242          --  this can generate very complex trees that only confuse CodePeer
243          --  and do not bring enough useful info.
244
245          Reset_Validity_Check_Options;
246          Validity_Check_Default       := True;
247          Validity_Check_In_Out_Params := True;
248          Validity_Check_In_Params     := True;
249
250          --  Turn off style check options since we are not interested in any
251          --  front-end warnings when we are getting CodePeer output.
252
253          Reset_Style_Check_Options;
254       end if;
255
256       --  Set Configurable_Run_Time mode if system.ads flag set
257
258       if Targparm.Configurable_Run_Time_On_Target or Debug_Flag_YY then
259          Configurable_Run_Time_Mode := True;
260       end if;
261
262       --  Set -gnatR3m mode if debug flag A set
263
264       if Debug_Flag_AA then
265          Back_Annotate_Rep_Info := True;
266          List_Representation_Info := 1;
267          List_Representation_Info_Mechanisms := True;
268       end if;
269
270       --  Force Target_Strict_Alignment true if debug flag -gnatd.a is set
271
272       if Debug_Flag_Dot_A then
273          Ttypes.Target_Strict_Alignment := True;
274       end if;
275
276       --  Disable static allocation of dispatch tables if -gnatd.t or if layout
277       --  is enabled. The front end's layout phase currently treats types that
278       --  have discriminant-dependent arrays as not being static even when a
279       --  discriminant constraint on the type is static, and this leads to
280       --  problems with subtypes of type Ada.Tags.Dispatch_Table_Wrapper. ???
281
282       if Debug_Flag_Dot_T or else Frontend_Layout_On_Target then
283          Static_Dispatch_Tables := False;
284       end if;
285
286       --  Flip endian mode if -gnatd8 set
287
288       if Debug_Flag_8 then
289          Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
290       end if;
291
292       --  Deal with forcing OpenVMS switches True if debug flag M is set, but
293       --  record the setting of Targparm.Open_VMS_On_Target in True_VMS_Target
294       --  before doing this, so we know if we are in real OpenVMS or not!
295
296       Opt.True_VMS_Target := Targparm.OpenVMS_On_Target;
297
298       if Debug_Flag_M then
299          Targparm.OpenVMS_On_Target := True;
300          Hostparm.OpenVMS := True;
301       end if;
302
303       --  Activate front end layout if debug flag -gnatdF is set
304
305       if Debug_Flag_FF then
306          Targparm.Frontend_Layout_On_Target := True;
307       end if;
308
309       --  Set and check exception mechnism
310
311       if Targparm.ZCX_By_Default_On_Target then
312          if Targparm.GCC_ZCX_Support_On_Target then
313             Exception_Mechanism := Back_End_Exceptions;
314          else
315             Osint.Fail ("Zero Cost Exceptions not supported on this target");
316          end if;
317       end if;
318
319       --  Set proper status for overflow checks. We turn on overflow checks
320       --  if -gnatp was not specified, and either -gnato is set or the back
321       --  end takes care of overflow checks. Otherwise we suppress overflow
322       --  checks by default (since front end checks are expensive).
323
324       if not Opt.Suppress_Checks
325         and then (Opt.Enable_Overflow_Checks
326                     or else
327                       (Targparm.Backend_Divide_Checks_On_Target
328                         and
329                        Targparm.Backend_Overflow_Checks_On_Target))
330       then
331          Suppress_Options (Overflow_Check) := False;
332       else
333          Suppress_Options (Overflow_Check) := True;
334       end if;
335
336       --  Set switch indicating if we can use N_Expression_With_Actions
337
338       --  Debug flag -gnatd.X decisively sets usage on
339
340       if Debug_Flag_Dot_XX then
341          Use_Expression_With_Actions := True;
342
343       --  Debug flag -gnatd.Y decisively sets usage off
344
345       elsif Debug_Flag_Dot_YY then
346          Use_Expression_With_Actions := False;
347
348       --  If no debug flags, usage off for AAMP, VM, SCIL cases
349
350       elsif AAMP_On_Target
351         or else VM_Target /= No_VM
352         or else Generate_SCIL
353       then
354          Use_Expression_With_Actions := False;
355
356          --  Otherwise normal gcc back end, for now still turn usage off by
357          --  default.
358
359       else
360          Use_Expression_With_Actions := False;
361       end if;
362    end Adjust_Global_Switches;
363
364    --------------------
365    -- Check_Bad_Body --
366    --------------------
367
368    procedure Check_Bad_Body is
369       Sname   : Unit_Name_Type;
370       Src_Ind : Source_File_Index;
371       Fname   : File_Name_Type;
372
373       procedure Bad_Body_Error (Msg : String);
374       --  Issue message for bad body found
375
376       --------------------
377       -- Bad_Body_Error --
378       --------------------
379
380       procedure Bad_Body_Error (Msg : String) is
381       begin
382          Error_Msg_N (Msg, Main_Unit_Node);
383          Error_Msg_File_1 := Fname;
384          Error_Msg_N ("remove incorrect body in file{!", Main_Unit_Node);
385       end Bad_Body_Error;
386
387       --  Start of processing for Check_Bad_Body
388
389    begin
390       --  Nothing to do if we are only checking syntax, because we don't know
391       --  enough to know if we require or forbid a body in this case.
392
393       if Operating_Mode = Check_Syntax then
394          return;
395       end if;
396
397       --  Check for body not allowed
398
399       if (Main_Kind = N_Package_Declaration
400            and then not Body_Required (Main_Unit_Node))
401         or else (Main_Kind = N_Generic_Package_Declaration
402                   and then not Body_Required (Main_Unit_Node))
403         or else Main_Kind = N_Package_Renaming_Declaration
404         or else Main_Kind = N_Subprogram_Renaming_Declaration
405         or else Nkind (Original_Node (Unit (Main_Unit_Node)))
406                          in N_Generic_Instantiation
407       then
408          Sname := Unit_Name (Main_Unit);
409
410          --  If we do not already have a body name, then get the body name
411          --  (but how can we have a body name here ???)
412
413          if not Is_Body_Name (Sname) then
414             Sname := Get_Body_Name (Sname);
415          end if;
416
417          Fname := Get_File_Name (Sname, Subunit => False);
418          Src_Ind := Load_Source_File (Fname);
419
420          --  Case where body is present and it is not a subunit. Exclude
421          --  the subunit case, because it has nothing to do with the
422          --  package we are compiling. It is illegal for a child unit and a
423          --  subunit with the same expanded name (RM 10.2(9)) to appear
424          --  together in a partition, but there is nothing to stop a
425          --  compilation environment from having both, and the test here
426          --  simply allows that. If there is an attempt to include both in
427          --  a partition, this is diagnosed at bind time. In Ada 83 mode
428          --  this is not a warning case.
429
430          --  Note: if weird file names are being used, we can have
431          --  situation where the file name that supposedly contains body,
432          --  in fact contains a spec, or we can't tell what it contains.
433          --  Skip the error message in these cases.
434
435          --  Also ignore body that is nothing but pragma No_Body; (that's the
436          --  whole point of this pragma, to be used this way and to cause the
437          --  body file to be ignored in this context).
438
439          if Src_Ind /= No_Source_File
440            and then Get_Expected_Unit_Type (Fname) = Expect_Body
441            and then not Source_File_Is_Subunit (Src_Ind)
442            and then not Source_File_Is_No_Body (Src_Ind)
443          then
444             Errout.Finalize (Last_Call => False);
445
446             Error_Msg_Unit_1 := Sname;
447
448             --  Ada 83 case of a package body being ignored. This is not an
449             --  error as far as the Ada 83 RM is concerned, but it is almost
450             --  certainly not what is wanted so output a warning. Give this
451             --  message only if there were no errors, since otherwise it may
452             --  be incorrect (we may have misinterpreted a junk spec as not
453             --  needing a body when it really does).
454
455             if Main_Kind = N_Package_Declaration
456               and then Ada_Version = Ada_83
457               and then Operating_Mode = Generate_Code
458               and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
459               and then not Compilation_Errors
460             then
461                Error_Msg_N
462                  ("package $$ does not require a body?", Main_Unit_Node);
463                Error_Msg_File_1 := Fname;
464                Error_Msg_N ("body in file{? will be ignored", Main_Unit_Node);
465
466                --  Ada 95 cases of a body file present when no body is
467                --  permitted. This we consider to be an error.
468
469             else
470                --  For generic instantiations, we never allow a body
471
472                if Nkind (Original_Node (Unit (Main_Unit_Node)))
473                in N_Generic_Instantiation
474                then
475                   Bad_Body_Error
476                     ("generic instantiation for $$ does not allow a body");
477
478                   --  A library unit that is a renaming never allows a body
479
480                elsif Main_Kind in N_Renaming_Declaration then
481                   Bad_Body_Error
482                     ("renaming declaration for $$ does not allow a body!");
483
484                   --  Remaining cases are packages and generic packages. Here
485                   --  we only do the test if there are no previous errors,
486                   --  because if there are errors, they may lead us to
487                   --  incorrectly believe that a package does not allow a body
488                   --  when in fact it does.
489
490                elsif not Compilation_Errors then
491                   if Main_Kind = N_Package_Declaration then
492                      Bad_Body_Error
493                        ("package $$ does not allow a body!");
494
495                   elsif Main_Kind = N_Generic_Package_Declaration then
496                      Bad_Body_Error
497                        ("generic package $$ does not allow a body!");
498                   end if;
499                end if;
500
501             end if;
502          end if;
503       end if;
504    end Check_Bad_Body;
505
506    -------------------------
507    -- Check_Library_Items --
508    -------------------------
509
510    --  Walk_Library_Items has plenty of assertions, so all we need to do is
511    --  call it, just for these assertions, not actually doing anything else.
512
513    procedure Check_Library_Items is
514
515       procedure Action (Item : Node_Id);
516       --  Action passed to Walk_Library_Items to do nothing
517
518       ------------
519       -- Action --
520       ------------
521
522       procedure Action (Item : Node_Id) is
523       begin
524          null;
525       end Action;
526
527       procedure Walk is new Sem.Walk_Library_Items (Action);
528
529    --  Start of processing for Check_Library_Items
530
531    begin
532       Walk;
533    end Check_Library_Items;
534
535    --------------------
536    -- Check_Rep_Info --
537    --------------------
538
539    procedure Check_Rep_Info is
540    begin
541       if List_Representation_Info /= 0
542         or else List_Representation_Info_Mechanisms
543       then
544          Set_Standard_Error;
545          Write_Eol;
546          Write_Str
547            ("cannot generate representation information, no code generated");
548          Write_Eol;
549          Write_Eol;
550          Set_Standard_Output;
551       end if;
552    end Check_Rep_Info;
553
554 --  Start of processing for Gnat1drv
555
556 begin
557    --  This inner block is set up to catch assertion errors and constraint
558    --  errors. Since the code for handling these errors can cause another
559    --  exception to be raised (namely Unrecoverable_Error), we need two
560    --  nested blocks, so that the outer one handles unrecoverable error.
561
562    begin
563       --  Initialize all packages. For the most part, these initialization
564       --  calls can be made in any order. Exceptions are as follows:
565
566       --  Lib.Initialize need to be called before Scan_Compiler_Arguments,
567       --  because it initializes a table filled by Scan_Compiler_Arguments.
568
569       Osint.Initialize;
570       Fmap.Reset_Tables;
571       Lib.Initialize;
572       Lib.Xref.Initialize;
573       Scan_Compiler_Arguments;
574       Osint.Add_Default_Search_Dirs;
575
576       Nlists.Initialize;
577       Sinput.Initialize;
578       Sem.Initialize;
579       Csets.Initialize;
580       Uintp.Initialize;
581       Urealp.Initialize;
582       Errout.Initialize;
583       Namet.Initialize;
584       SCOs.Initialize;
585       Snames.Initialize;
586       Stringt.Initialize;
587       Inline.Initialize;
588       Par_SCO.Initialize;
589       Sem_Ch8.Initialize;
590       Sem_Ch12.Initialize;
591       Sem_Ch13.Initialize;
592       Sem_Elim.Initialize;
593       Sem_Eval.Initialize;
594       Sem_Type.Init_Interp_Tables;
595
596       --  Acquire target parameters from system.ads (source of package System)
597
598       declare
599          use Sinput;
600
601          S : Source_File_Index;
602          N : File_Name_Type;
603
604       begin
605          Name_Buffer (1 .. 10) := "system.ads";
606          Name_Len := 10;
607          N := Name_Find;
608          S := Load_Source_File (N);
609
610          if S = No_Source_File then
611             Write_Line
612               ("fatal error, run-time library not installed correctly");
613             Write_Line
614               ("cannot locate file system.ads");
615             raise Unrecoverable_Error;
616
617          --  Remember source index of system.ads (which was read successfully)
618
619          else
620             System_Source_File_Index := S;
621          end if;
622
623          Targparm.Get_Target_Parameters
624            (System_Text  => Source_Text  (S),
625             Source_First => Source_First (S),
626             Source_Last  => Source_Last  (S));
627
628          --  Acquire configuration pragma information from Targparm
629
630          Restrict.Restrictions := Targparm.Restrictions_On_Target;
631       end;
632
633       Adjust_Global_Switches;
634
635       --  Output copyright notice if full list mode unless we have a list
636       --  file, in which case we defer this so that it is output in the file
637
638       if (Verbose_Mode or else (Full_List and then Full_List_File_Name = null))
639         and then not Debug_Flag_7
640       then
641          Write_Eol;
642          Write_Str ("GNAT ");
643          Write_Str (Gnat_Version_String);
644          Write_Eol;
645          Write_Str ("Copyright 1992-" &
646                     Current_Year &
647                     ", Free Software Foundation, Inc.");
648          Write_Eol;
649       end if;
650
651       --  Check we do not have more than one source file, this happens only in
652       --  the case where the driver is called directly, it cannot happen when
653       --  gnat1 is invoked from gcc in the normal case.
654
655       if Osint.Number_Of_Files /= 1 then
656          Usage;
657          Write_Eol;
658          Osint.Fail ("you must provide one source file");
659
660       elsif Usage_Requested then
661          Usage;
662       end if;
663
664       Original_Operating_Mode := Operating_Mode;
665       Frontend;
666
667       --  Exit with errors if the main source could not be parsed
668
669       if Sinput.Main_Source_File = No_Source_File then
670          Errout.Finalize (Last_Call => True);
671          Errout.Output_Messages;
672          Exit_Program (E_Errors);
673       end if;
674
675       Main_Unit_Node := Cunit (Main_Unit);
676       Main_Kind := Nkind (Unit (Main_Unit_Node));
677       Check_Bad_Body;
678
679       --  Exit if compilation errors detected
680
681       Errout.Finalize (Last_Call => False);
682
683       if Compilation_Errors then
684          Treepr.Tree_Dump;
685          Sem_Ch13.Validate_Unchecked_Conversions;
686          Sem_Ch13.Validate_Address_Clauses;
687          Errout.Output_Messages;
688          Namet.Finalize;
689
690          --  Generate ALI file if specially requested
691
692          if Opt.Force_ALI_Tree_File then
693             Write_ALI (Object => False);
694             Tree_Gen;
695          end if;
696
697          Errout.Finalize (Last_Call => True);
698          Exit_Program (E_Errors);
699       end if;
700
701       --  Set Generate_Code on main unit and its spec. We do this even if are
702       --  not generating code, since Lib-Writ uses this to determine which
703       --  units get written in the ali file.
704
705       Set_Generate_Code (Main_Unit);
706
707       --  If we have a corresponding spec, and it comes from source
708       --  or it is not a generated spec for a child subprogram body,
709       --  then we need object code for the spec unit as well.
710
711       if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
712         and then not Acts_As_Spec (Main_Unit_Node)
713       then
714          if Nkind (Unit (Main_Unit_Node)) = N_Subprogram_Body
715            and then not Comes_From_Source (Library_Unit (Main_Unit_Node))
716          then
717             null;
718          else
719             Set_Generate_Code
720               (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
721          end if;
722       end if;
723
724       --  Case of no code required to be generated, exit indicating no error
725
726       if Original_Operating_Mode = Check_Syntax then
727          Treepr.Tree_Dump;
728          Errout.Finalize (Last_Call => True);
729          Errout.Output_Messages;
730          Tree_Gen;
731          Namet.Finalize;
732          Check_Rep_Info;
733
734          --  Use a goto instead of calling Exit_Program so that finalization
735          --  occurs normally.
736
737          goto End_Of_Program;
738
739       elsif Original_Operating_Mode = Check_Semantics then
740          Back_End_Mode := Declarations_Only;
741
742       --  All remaining cases are cases in which the user requested that code
743       --  be generated (i.e. no -gnatc or -gnats switch was used). Check if
744       --  we can in fact satisfy this request.
745
746       --  Cannot generate code if someone has turned off code generation for
747       --  any reason at all. We will try to figure out a reason below.
748
749       elsif Operating_Mode /= Generate_Code then
750          Back_End_Mode := Skip;
751
752       --  We can generate code for a subprogram body unless there were missing
753       --  subunits. Note that we always generate code for all generic units (a
754       --  change from some previous versions of GNAT).
755
756       elsif Main_Kind = N_Subprogram_Body
757         and then not Subunits_Missing
758       then
759          Back_End_Mode := Generate_Object;
760
761       --  We can generate code for a package body unless there are subunits
762       --  missing (note that we always generate code for generic units, which
763       --  is a change from some earlier versions of GNAT).
764
765       elsif Main_Kind = N_Package_Body
766         and then not Subunits_Missing
767       then
768          Back_End_Mode := Generate_Object;
769
770       --  We can generate code for a package declaration or a subprogram
771       --  declaration only if it does not required a body.
772
773       elsif (Main_Kind = N_Package_Declaration
774                or else
775              Main_Kind = N_Subprogram_Declaration)
776         and then
777           (not Body_Required (Main_Unit_Node)
778              or else
779            Distribution_Stub_Mode = Generate_Caller_Stub_Body)
780       then
781          Back_End_Mode := Generate_Object;
782
783       --  We can generate code for a generic package declaration of a generic
784       --  subprogram declaration only if does not require a body.
785
786       elsif (Main_Kind = N_Generic_Package_Declaration
787                or else
788              Main_Kind = N_Generic_Subprogram_Declaration)
789         and then not Body_Required (Main_Unit_Node)
790       then
791          Back_End_Mode := Generate_Object;
792
793       --  Compilation units that are renamings do not require bodies,
794       --  so we can generate code for them.
795
796       elsif Main_Kind = N_Package_Renaming_Declaration
797         or else Main_Kind = N_Subprogram_Renaming_Declaration
798       then
799          Back_End_Mode := Generate_Object;
800
801       --  Compilation units that are generic renamings do not require bodies
802       --  so we can generate code for them.
803
804       elsif Main_Kind in N_Generic_Renaming_Declaration then
805          Back_End_Mode := Generate_Object;
806
807       --  It's not an error to generate SCIL for e.g. a spec which has a body
808
809       elsif CodePeer_Mode then
810          Back_End_Mode := Generate_Object;
811
812       --  In all other cases (specs which have bodies, generics, and bodies
813       --  where subunits are missing), we cannot generate code and we generate
814       --  a warning message. Note that generic instantiations are gone at this
815       --  stage since they have been replaced by their instances.
816
817       else
818          Back_End_Mode := Skip;
819       end if;
820
821       --  At this stage Back_End_Mode is set to indicate if the backend should
822       --  be called to generate code. If it is Skip, then code generation has
823       --  been turned off, even though code was requested by the original
824       --  command. This is not an error from the user point of view, but it is
825       --  an error from the point of view of the gcc driver, so we must exit
826       --  with an error status.
827
828       --  We generate an informative message (from the gcc point of view, it
829       --  is an error message, but from the users point of view this is not an
830       --  error, just a consequence of compiling something that cannot
831       --  generate code).
832
833       if Back_End_Mode = Skip then
834          Set_Standard_Error;
835          Write_Str ("cannot generate code for ");
836          Write_Str ("file ");
837          Write_Name (Unit_File_Name (Main_Unit));
838
839          if Subunits_Missing then
840             Write_Str (" (missing subunits)");
841             Write_Eol;
842             Write_Str ("to check parent unit");
843
844          elsif Main_Kind = N_Subunit then
845             Write_Str (" (subunit)");
846             Write_Eol;
847             Write_Str ("to check subunit");
848
849          elsif Main_Kind = N_Subprogram_Declaration then
850             Write_Str (" (subprogram spec)");
851             Write_Eol;
852             Write_Str ("to check subprogram spec");
853
854          --  Generic package body in GNAT implementation mode
855
856          elsif Main_Kind = N_Package_Body and then GNAT_Mode then
857             Write_Str (" (predefined generic)");
858             Write_Eol;
859             Write_Str ("to check predefined generic");
860
861          --  Only other case is a package spec
862
863          else
864             Write_Str (" (package spec)");
865             Write_Eol;
866             Write_Str ("to check package spec");
867          end if;
868
869          Write_Str (" for errors, use ");
870
871          if Hostparm.OpenVMS then
872             Write_Str ("/NOLOAD");
873          else
874             Write_Str ("-gnatc");
875          end if;
876
877          Write_Eol;
878          Set_Standard_Output;
879
880          Sem_Ch13.Validate_Unchecked_Conversions;
881          Sem_Ch13.Validate_Address_Clauses;
882          Errout.Finalize (Last_Call => True);
883          Errout.Output_Messages;
884          Treepr.Tree_Dump;
885          Tree_Gen;
886          Write_ALI (Object => False);
887          Namet.Finalize;
888          Check_Rep_Info;
889
890          --  Exit program with error indication, to kill object file
891
892          Exit_Program (E_No_Code);
893       end if;
894
895       --  In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
896       --  set as indicated by Back_Annotate_Rep_Info being set to True.
897
898       --  We don't call for annotations on a subunit, because to process those
899       --  the back-end requires that the parent(s) be properly compiled.
900
901       --  Annotation is suppressed for targets where front-end layout is
902       --  enabled, because the front end determines representations.
903
904       --  Annotation is also suppressed in the case of compiling for
905       --  a VM, since representations are largely symbolic there.
906
907       if Back_End_Mode = Declarations_Only
908         and then (not (Back_Annotate_Rep_Info or Generate_SCIL)
909                    or else Main_Kind = N_Subunit
910                    or else Targparm.Frontend_Layout_On_Target
911                    or else Targparm.VM_Target /= No_VM)
912       then
913          Sem_Ch13.Validate_Unchecked_Conversions;
914          Sem_Ch13.Validate_Address_Clauses;
915          Errout.Finalize (Last_Call => True);
916          Errout.Output_Messages;
917          Write_ALI (Object => False);
918          Tree_Dump;
919          Tree_Gen;
920          Namet.Finalize;
921          Check_Rep_Info;
922          return;
923       end if;
924
925       --  Ensure that we properly register a dependency on system.ads, since
926       --  even if we do not semantically depend on this, Targparm has read
927       --  system parameters from the system.ads file.
928
929       Lib.Writ.Ensure_System_Dependency;
930
931       --  Add dependencies, if any, on preprocessing data file and on
932       --  preprocessing definition file(s).
933
934       Prepcomp.Add_Dependencies;
935
936       --  Back end needs to explicitly unlock tables it needs to touch
937
938       Atree.Lock;
939       Elists.Lock;
940       Fname.UF.Lock;
941       Inline.Lock;
942       Lib.Lock;
943       Nlists.Lock;
944       Sem.Lock;
945       Sinput.Lock;
946       Namet.Lock;
947       Stringt.Lock;
948
949       --  ???Check_Library_Items under control of a debug flag, because it
950       --  currently does not work if the -gnatn switch (back end inlining) is
951       --  used.
952
953       if Debug_Flag_Dot_WW then
954          Check_Library_Items;
955       end if;
956
957       --  Here we call the back end to generate the output code
958
959       Generating_Code := True;
960       Back_End.Call_Back_End (Back_End_Mode);
961
962       --  Once the backend is complete, we unlock the names table. This call
963       --  allows a few extra entries, needed for example for the file name for
964       --  the library file output.
965
966       Namet.Unlock;
967
968       --  Validate unchecked conversions (using the values for size and
969       --  alignment annotated by the backend where possible).
970
971       Sem_Ch13.Validate_Unchecked_Conversions;
972
973       --  Validate address clauses (again using alignment values annotated
974       --  by the backend where possible).
975
976       Sem_Ch13.Validate_Address_Clauses;
977
978       --  Now we complete output of errors, rep info and the tree info. These
979       --  are delayed till now, since it is perfectly possible for gigi to
980       --  generate errors, modify the tree (in particular by setting flags
981       --  indicating that elaboration is required, and also to back annotate
982       --  representation information for List_Rep_Info.
983
984       Errout.Finalize (Last_Call => True);
985       Errout.Output_Messages;
986       List_Rep_Info;
987
988       --  Only write the library if the backend did not generate any error
989       --  messages. Otherwise signal errors to the driver program so that
990       --  there will be no attempt to generate an object file.
991
992       if Compilation_Errors then
993          Treepr.Tree_Dump;
994          Exit_Program (E_Errors);
995       end if;
996
997       Write_ALI (Object => (Back_End_Mode = Generate_Object));
998
999       --  Generate the ASIS tree after writing the ALI file, since in ASIS
1000       --  mode, Write_ALI may in fact result in further tree decoration from
1001       --  the original tree file. Note that we dump the tree just before
1002       --  generating it, so that the dump will exactly reflect what is written
1003       --  out.
1004
1005       Treepr.Tree_Dump;
1006       Tree_Gen;
1007
1008       --  Finalize name table and we are all done
1009
1010       Namet.Finalize;
1011
1012    exception
1013       --  Handle fatal internal compiler errors
1014
1015       when Rtsfind.RE_Not_Available =>
1016          Comperr.Compiler_Abort ("RE_Not_Available");
1017
1018       when System.Assertions.Assert_Failure =>
1019          Comperr.Compiler_Abort ("Assert_Failure");
1020
1021       when Constraint_Error =>
1022          Comperr.Compiler_Abort ("Constraint_Error");
1023
1024       when Program_Error =>
1025          Comperr.Compiler_Abort ("Program_Error");
1026
1027       when Storage_Error =>
1028
1029          --  Assume this is a bug. If it is real, the message will in any case
1030          --  say Storage_Error, giving a strong hint!
1031
1032          Comperr.Compiler_Abort ("Storage_Error");
1033    end;
1034
1035    <<End_Of_Program>>
1036    null;
1037
1038    --  The outer exception handles an unrecoverable error
1039
1040 exception
1041    when Unrecoverable_Error =>
1042       Errout.Finalize (Last_Call => True);
1043       Errout.Output_Messages;
1044
1045       Set_Standard_Error;
1046       Write_Str ("compilation abandoned");
1047       Write_Eol;
1048
1049       Set_Standard_Output;
1050       Source_Dump;
1051       Tree_Dump;
1052       Exit_Program (E_Errors);
1053
1054 end Gnat1drv;