OSDN Git Service

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