OSDN Git Service

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