OSDN Git Service

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