OSDN Git Service

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