OSDN Git Service

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