1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Back_End; use Back_End;
30 with Csets; use Csets;
31 with Debug; use Debug;
33 with Errout; use Errout;
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
38 with Gnatvsn; use Gnatvsn;
42 with Lib.Writ; use Lib.Writ;
44 with Namet; use Namet;
47 with Osint; use Osint;
48 with Output; use Output;
50 with Repinfo; use Repinfo;
60 with Sinfo; use Sinfo;
61 with Sinput.L; use Sinput.L;
63 with Sprint; use Sprint;
65 with Targparm; use Targparm;
67 with Treepr; use Treepr;
69 with Types; use Types;
70 with Uintp; use Uintp;
71 with Uname; use Uname;
75 with System.Assertions;
78 Main_Unit_Node : Node_Id;
79 -- Compilation unit node for main unit
81 Main_Kind : Node_Kind;
82 -- Kind of main compilation unit node
84 Back_End_Mode : Back_End.Back_End_Mode_Type;
85 -- Record back end mode
87 procedure Check_Bad_Body;
88 -- Called to check if the unit we are compiling has a bad body
90 procedure Check_Rep_Info;
91 -- Called when we are not generating code, to check if -gnatR was requested
92 -- and if so, explain that we will not be honoring the request.
98 procedure Check_Bad_Body is
99 Sname : Unit_Name_Type;
100 Src_Ind : Source_File_Index;
101 Fname : File_Name_Type;
103 procedure Bad_Body_Error (Msg : String);
104 -- Issue message for bad body found
110 procedure Bad_Body_Error (Msg : String) is
112 Error_Msg_N (Msg, Main_Unit_Node);
113 Error_Msg_File_1 := Fname;
114 Error_Msg_N ("remove incorrect body in file{!", Main_Unit_Node);
117 -- Start of processing for Check_Bad_Body
120 -- Nothing to do if we are only checking syntax, because we don't know
121 -- enough to know if we require or forbid a body in this case.
123 if Operating_Mode = Check_Syntax then
127 -- Check for body not allowed
129 if (Main_Kind = N_Package_Declaration
130 and then not Body_Required (Main_Unit_Node))
131 or else (Main_Kind = N_Generic_Package_Declaration
132 and then not Body_Required (Main_Unit_Node))
133 or else Main_Kind = N_Package_Renaming_Declaration
134 or else Main_Kind = N_Subprogram_Renaming_Declaration
135 or else Nkind (Original_Node (Unit (Main_Unit_Node)))
136 in N_Generic_Instantiation
138 Sname := Unit_Name (Main_Unit);
140 -- If we do not already have a body name, then get the body name
141 -- (but how can we have a body name here ???)
143 if not Is_Body_Name (Sname) then
144 Sname := Get_Body_Name (Sname);
147 Fname := Get_File_Name (Sname, Subunit => False);
148 Src_Ind := Load_Source_File (Fname);
150 -- Case where body is present and it is not a subunit. Exclude
151 -- the subunit case, because it has nothing to do with the
152 -- package we are compiling. It is illegal for a child unit and a
153 -- subunit with the same expanded name (RM 10.2(9)) to appear
154 -- together in a partition, but there is nothing to stop a
155 -- compilation environment from having both, and the test here
156 -- simply allows that. If there is an attempt to include both in
157 -- a partition, this is diagnosed at bind time. In Ada 83 mode
158 -- this is not a warning case.
160 -- Note: if weird file names are being used, we can have
161 -- situation where the file name that supposedly contains body,
162 -- in fact contains a spec, or we can't tell what it contains.
163 -- Skip the error message in these cases.
165 -- Also ignore body that is nothing but pragma No_Body; (that's the
166 -- whole point of this pragma, to be used this way and to cause the
167 -- body file to be ignored in this context).
169 if Src_Ind /= No_Source_File
170 and then Get_Expected_Unit_Type (Fname) = Expect_Body
171 and then not Source_File_Is_Subunit (Src_Ind)
172 and then not Source_File_Is_No_Body (Src_Ind)
174 Errout.Finalize (Last_Call => False);
176 Error_Msg_Unit_1 := Sname;
178 -- Ada 83 case of a package body being ignored. This is not an
179 -- error as far as the Ada 83 RM is concerned, but it is almost
180 -- certainly not what is wanted so output a warning. Give this
181 -- message only if there were no errors, since otherwise it may
182 -- be incorrect (we may have misinterpreted a junk spec as not
183 -- needing a body when it really does).
185 if Main_Kind = N_Package_Declaration
186 and then Ada_Version = Ada_83
187 and then Operating_Mode = Generate_Code
188 and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
189 and then not Compilation_Errors
192 ("package $$ does not require a body?", Main_Unit_Node);
193 Error_Msg_File_1 := Fname;
194 Error_Msg_N ("body in file{? will be ignored", Main_Unit_Node);
196 -- Ada 95 cases of a body file present when no body is
197 -- permitted. This we consider to be an error.
200 -- For generic instantiations, we never allow a body
202 if Nkind (Original_Node (Unit (Main_Unit_Node)))
203 in N_Generic_Instantiation
206 ("generic instantiation for $$ does not allow a body");
208 -- A library unit that is a renaming never allows a body
210 elsif Main_Kind in N_Renaming_Declaration then
212 ("renaming declaration for $$ does not allow a body!");
214 -- Remaining cases are packages and generic packages. Here
215 -- we only do the test if there are no previous errors,
216 -- because if there are errors, they may lead us to
217 -- incorrectly believe that a package does not allow a body
218 -- when in fact it does.
220 elsif not Compilation_Errors then
221 if Main_Kind = N_Package_Declaration then
223 ("package $$ does not allow a body!");
225 elsif Main_Kind = N_Generic_Package_Declaration then
227 ("generic package $$ does not allow a body!");
240 procedure Check_Rep_Info is
242 if List_Representation_Info /= 0
243 or else List_Representation_Info_Mechanisms
247 ("cannot generate representation information, no code generated");
253 -- Start of processing for Gnat1drv
256 -- This inner block is set up to catch assertion errors and constraint
257 -- errors. Since the code for handling these errors can cause another
258 -- exception to be raised (namely Unrecoverable_Error), we need two
259 -- nested blocks, so that the outer one handles unrecoverable error.
262 -- Lib.Initialize need to be called before Scan_Compiler_Arguments,
263 -- because it initializes a table filled by Scan_Compiler_Arguments.
269 Scan_Compiler_Arguments;
270 Osint.Add_Default_Search_Dirs;
288 Sem_Type.Init_Interp_Tables;
290 -- Acquire target parameters from system.ads (source of package System)
295 S : Source_File_Index;
299 Name_Buffer (1 .. 10) := "system.ads";
302 S := Load_Source_File (N);
304 if S = No_Source_File then
306 ("fatal error, run-time library not installed correctly");
308 ("cannot locate file system.ads");
309 raise Unrecoverable_Error;
311 -- Remember source index of system.ads (which was read successfully)
314 System_Source_File_Index := S;
317 Targparm.Get_Target_Parameters
318 (System_Text => Source_Text (S),
319 Source_First => Source_First (S),
320 Source_Last => Source_Last (S));
322 -- Acquire configuration pragma information from Targparm
324 Restrict.Restrictions := Targparm.Restrictions_On_Target;
327 -- Set Configurable_Run_Time mode if system.ads flag set
329 if Targparm.Configurable_Run_Time_On_Target or Debug_Flag_YY then
330 Configurable_Run_Time_Mode := True;
333 -- Set -gnatR3m mode if debug flag A set
335 if Debug_Flag_AA then
336 Back_Annotate_Rep_Info := True;
337 List_Representation_Info := 1;
338 List_Representation_Info_Mechanisms := True;
341 -- Disable static allocation of dispatch tables if -gnatd.t or if layout
342 -- is enabled. The front end's layout phase currently treats types that
343 -- have discriminant-dependent arrays as not being static even when a
344 -- discriminant constraint on the type is static, and this leads to
345 -- problems with subtypes of type Ada.Tags.Dispatch_Table_Wrapper. ???
347 if Debug_Flag_Dot_T or else Frontend_Layout_On_Target then
348 Static_Dispatch_Tables := False;
351 -- Output copyright notice if full list mode unless we have a list
352 -- file, in which case we defer this so that it is output in the file
354 if (Verbose_Mode or else (Full_List and then Full_List_File_Name = null))
355 and then not Debug_Flag_7
359 Write_Str (Gnat_Version_String);
361 Write_Str ("Copyright 1992-" &
363 ", Free Software Foundation, Inc.");
367 -- Before we do anything else, adjust certain global values for
368 -- debug switches which modify their normal natural settings.
371 Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
375 Targparm.OpenVMS_On_Target := True;
376 Hostparm.OpenVMS := True;
379 if Debug_Flag_FF then
380 Targparm.Frontend_Layout_On_Target := True;
383 -- We take the default exception mechanism into account
385 if Targparm.ZCX_By_Default_On_Target then
386 if Targparm.GCC_ZCX_Support_On_Target then
387 Exception_Mechanism := Back_End_Exceptions;
390 ("Zero Cost Exceptions not supported on this target");
394 -- Set proper status for overflow checks. We turn on overflow checks
395 -- if -gnatp was not specified, and either -gnato is set or the back
396 -- end takes care of overflow checks. Otherwise we suppress overflow
397 -- checks by default (since front end checks are expensive).
399 if not Opt.Suppress_Checks
400 and then (Opt.Enable_Overflow_Checks
402 (Targparm.Backend_Divide_Checks_On_Target
404 Targparm.Backend_Overflow_Checks_On_Target))
406 Suppress_Options (Overflow_Check) := False;
408 Suppress_Options (Overflow_Check) := True;
411 -- Check we do not have more than one source file, this happens only in
412 -- the case where the driver is called directly, it cannot happen when
413 -- gnat1 is invoked from gcc in the normal case.
415 if Osint.Number_Of_Files /= 1 then
418 Osint.Fail ("you must provide one source file");
420 elsif Usage_Requested then
424 Original_Operating_Mode := Operating_Mode;
427 -- Exit with errors if the main source could not be parsed
429 if Sinput.Main_Source_File = No_Source_File then
430 Errout.Finalize (Last_Call => True);
431 Errout.Output_Messages;
432 Exit_Program (E_Errors);
435 Main_Unit_Node := Cunit (Main_Unit);
436 Main_Kind := Nkind (Unit (Main_Unit_Node));
439 -- Exit if compilation errors detected
441 Errout.Finalize (Last_Call => False);
443 if Compilation_Errors then
445 Sem_Ch13.Validate_Unchecked_Conversions;
446 Errout.Output_Messages;
449 -- Generate ALI file if specially requested
451 if Opt.Force_ALI_Tree_File then
452 Write_ALI (Object => False);
456 Errout.Finalize (Last_Call => True);
457 Exit_Program (E_Errors);
460 -- Set Generate_Code on main unit and its spec. We do this even if are
461 -- not generating code, since Lib-Writ uses this to determine which
462 -- units get written in the ali file.
464 Set_Generate_Code (Main_Unit);
466 -- If we have a corresponding spec, then we need object
467 -- code for the spec unit as well
469 if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
470 and then not Acts_As_Spec (Main_Unit_Node)
473 (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
476 -- Case of no code required to be generated, exit indicating no error
478 if Original_Operating_Mode = Check_Syntax then
480 Errout.Finalize (Last_Call => True);
481 Errout.Output_Messages;
486 -- Use a goto instead of calling Exit_Program so that finalization
491 elsif Original_Operating_Mode = Check_Semantics then
492 Back_End_Mode := Declarations_Only;
494 -- All remaining cases are cases in which the user requested that code
495 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if
496 -- we can in fact satisfy this request.
498 -- Cannot generate code if someone has turned off code generation for
499 -- any reason at all. We will try to figure out a reason below.
501 elsif Operating_Mode /= Generate_Code then
502 Back_End_Mode := Skip;
504 -- We can generate code for a subprogram body unless there were missing
505 -- subunits. Note that we always generate code for all generic units (a
506 -- change from some previous versions of GNAT).
508 elsif Main_Kind = N_Subprogram_Body
509 and then not Subunits_Missing
511 Back_End_Mode := Generate_Object;
513 -- We can generate code for a package body unless there are subunits
514 -- missing (note that we always generate code for generic units, which
515 -- is a change from some earlier versions of GNAT).
517 elsif Main_Kind = N_Package_Body
518 and then not Subunits_Missing
520 Back_End_Mode := Generate_Object;
522 -- We can generate code for a package declaration or a subprogram
523 -- declaration only if it does not required a body.
525 elsif (Main_Kind = N_Package_Declaration
527 Main_Kind = N_Subprogram_Declaration)
529 (not Body_Required (Main_Unit_Node)
531 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
533 Back_End_Mode := Generate_Object;
535 -- We can generate code for a generic package declaration of a generic
536 -- subprogram declaration only if does not require a body.
538 elsif (Main_Kind = N_Generic_Package_Declaration
540 Main_Kind = N_Generic_Subprogram_Declaration)
541 and then not Body_Required (Main_Unit_Node)
543 Back_End_Mode := Generate_Object;
545 -- Compilation units that are renamings do not require bodies,
546 -- so we can generate code for them.
548 elsif Main_Kind = N_Package_Renaming_Declaration
549 or else Main_Kind = N_Subprogram_Renaming_Declaration
551 Back_End_Mode := Generate_Object;
553 -- Compilation units that are generic renamings do not require bodies
554 -- so we can generate code for them.
556 elsif Main_Kind in N_Generic_Renaming_Declaration then
557 Back_End_Mode := Generate_Object;
559 -- In all other cases (specs which have bodies, generics, and bodies
560 -- where subunits are missing), we cannot generate code and we generate
561 -- a warning message. Note that generic instantiations are gone at this
562 -- stage since they have been replaced by their instances.
565 Back_End_Mode := Skip;
568 -- At this stage Call_Back_End is set to indicate if the backend should
569 -- be called to generate code. If it is not set, then code generation
570 -- has been turned off, even though code was requested by the original
571 -- command. This is not an error from the user point of view, but it is
572 -- an error from the point of view of the gcc driver, so we must exit
573 -- with an error status.
575 -- We generate an informative message (from the gcc point of view, it
576 -- is an error message, but from the users point of view this is not an
577 -- error, just a consequence of compiling something that cannot
580 if Back_End_Mode = Skip then
581 Write_Str ("cannot generate code for ");
583 Write_Name (Unit_File_Name (Main_Unit));
585 if Subunits_Missing then
586 Write_Str (" (missing subunits)");
588 Write_Str ("to check parent unit");
590 elsif Main_Kind = N_Subunit then
591 Write_Str (" (subunit)");
593 Write_Str ("to check subunit");
595 elsif Main_Kind = N_Subprogram_Declaration then
596 Write_Str (" (subprogram spec)");
598 Write_Str ("to check subprogram spec");
600 -- Generic package body in GNAT implementation mode
602 elsif Main_Kind = N_Package_Body and then GNAT_Mode then
603 Write_Str (" (predefined generic)");
605 Write_Str ("to check predefined generic");
607 -- Only other case is a package spec
610 Write_Str (" (package spec)");
612 Write_Str ("to check package spec");
615 Write_Str (" for errors, use ");
617 if Hostparm.OpenVMS then
618 Write_Str ("/NOLOAD");
620 Write_Str ("-gnatc");
625 Sem_Ch13.Validate_Unchecked_Conversions;
626 Errout.Finalize (Last_Call => True);
627 Errout.Output_Messages;
630 Write_ALI (Object => False);
634 -- Exit program with error indication, to kill object file
636 Exit_Program (E_No_Code);
639 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
640 -- set as indicated by Back_Annotate_Rep_Info being set to True.
642 -- We don't call for annotations on a subunit, because to process those
643 -- the back-end requires that the parent(s) be properly compiled.
645 -- Annotation is suppressed for targets where front-end layout is
646 -- enabled, because the front end determines representations.
648 -- Annotation is also suppressed in the case of compiling for
649 -- a VM, since representations are largely symbolic there.
651 if Back_End_Mode = Declarations_Only
652 and then (not Back_Annotate_Rep_Info
653 or else Main_Kind = N_Subunit
654 or else Targparm.Frontend_Layout_On_Target
655 or else Targparm.VM_Target /= No_VM)
657 Sem_Ch13.Validate_Unchecked_Conversions;
658 Errout.Finalize (Last_Call => True);
659 Errout.Output_Messages;
660 Write_ALI (Object => False);
668 -- Ensure that we properly register a dependency on system.ads, since
669 -- even if we do not semantically depend on this, Targparm has read
670 -- system parameters from the system.ads file.
672 Lib.Writ.Ensure_System_Dependency;
674 -- Add dependencies, if any, on preprocessing data file and on
675 -- preprocessing definition file(s).
677 Prepcomp.Add_Dependencies;
679 -- Back end needs to explicitly unlock tables it needs to touch
692 -- Here we call the back end to generate the output code
694 Generating_Code := True;
695 Back_End.Call_Back_End (Back_End_Mode);
697 -- Once the backend is complete, we unlock the names table. This call
698 -- allows a few extra entries, needed for example for the file name for
699 -- the library file output.
703 -- Validate unchecked conversions (using the values for size and
704 -- alignment annotated by the backend where possible).
706 Sem_Ch13.Validate_Unchecked_Conversions;
708 -- Now we complete output of errors, rep info and the tree info. These
709 -- are delayed till now, since it is perfectly possible for gigi to
710 -- generate errors, modify the tree (in particular by setting flags
711 -- indicating that elaboration is required, and also to back annotate
712 -- representation information for List_Rep_Info.
714 Errout.Finalize (Last_Call => True);
715 Errout.Output_Messages;
718 -- Only write the library if the backend did not generate any error
719 -- messages. Otherwise signal errors to the driver program so that
720 -- there will be no attempt to generate an object file.
722 if Compilation_Errors then
724 Exit_Program (E_Errors);
727 Write_ALI (Object => (Back_End_Mode = Generate_Object));
729 -- Generate the ASIS tree after writing the ALI file, since in ASIS
730 -- mode, Write_ALI may in fact result in further tree decoration from
731 -- the original tree file. Note that we dump the tree just before
732 -- generating it, so that the dump will exactly reflect what is written
738 -- Finalize name table and we are all done
743 -- Handle fatal internal compiler errors
745 when Rtsfind.RE_Not_Available =>
746 Comperr.Compiler_Abort ("RE_Not_Available");
748 when System.Assertions.Assert_Failure =>
749 Comperr.Compiler_Abort ("Assert_Failure");
751 when Constraint_Error =>
752 Comperr.Compiler_Abort ("Constraint_Error");
754 when Program_Error =>
755 Comperr.Compiler_Abort ("Program_Error");
757 when Storage_Error =>
759 -- Assume this is a bug. If it is real, the message will in any case
760 -- say Storage_Error, giving a strong hint!
762 Comperr.Compiler_Abort ("Storage_Error");
768 -- The outer exception handles an unrecoverable error
771 when Unrecoverable_Error =>
772 Errout.Finalize (Last_Call => True);
773 Errout.Output_Messages;
776 Write_Str ("compilation abandoned");
782 Exit_Program (E_Errors);