1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree
; use Atree
;
30 with Back_End
; use Back_End
;
32 with Csets
; use Csets
;
33 with Debug
; use Debug
;
35 with Errout
; use Errout
;
36 with Fname
; use Fname
;
37 with Fname
.UF
; use Fname
.UF
;
39 with Gnatvsn
; use Gnatvsn
;
43 with Lib
.Writ
; use Lib
.Writ
;
44 with Namet
; use Namet
;
47 with Osint
; use Osint
;
48 with Output
; use Output
;
49 with Repinfo
; use Repinfo
;
50 with Restrict
; use Restrict
;
54 with Sinfo
; use Sinfo
;
55 with Sinput
.L
; use Sinput
.L
;
57 with Sprint
; use Sprint
;
61 with Treepr
; use Treepr
;
63 with Types
; use Types
;
65 with Uname
; use Uname
;
69 with System
.Assertions
;
72 Main_Unit_Node
: Node_Id
;
73 -- Compilation unit node for main unit
75 Main_Unit_Entity
: Node_Id
;
76 -- Compilation unit entity for main unit
78 Main_Kind
: Node_Kind
;
79 -- Kind of main compilation unit node.
81 Original_Operating_Mode
: Operating_Mode_Type
;
82 -- Save operating type specified by options
84 Back_End_Mode
: Back_End
.Back_End_Mode_Type
;
85 -- Record back end mode
88 -- This inner block is set up to catch assertion errors and constraint
89 -- errors. Since the code for handling these errors can cause another
90 -- exception to be raised (namely Unrecoverable_Error), we need two
91 -- nested blocks, so that the outer one handles unrecoverable error.
94 Osint
.Initialize
(Compiler
);
95 Scan_Compiler_Arguments
;
96 Osint
.Add_Default_Search_Dirs
;
111 -- Output copyright notice if full list mode
113 if (Verbose_Mode
or Full_List
)
114 and then (not Debug_Flag_7
)
118 Write_Str
(Gnat_Version_String
);
119 Write_Str
(" Copyright 1992-2001 Free Software Foundation, Inc.");
123 -- Acquire target parameters and perform required setup
125 Targparm
.Get_Target_Parameters
;
127 if Targparm
.High_Integrity_Mode_On_Target
then
128 Set_No_Run_Time_Mode
;
131 -- Before we do anything else, adjust certain global values for
132 -- debug switches which modify their normal natural settings.
135 Ttypes
.Bytes_Big_Endian
:= not Ttypes
.Bytes_Big_Endian
;
139 Targparm
.OpenVMS_On_Target
:= True;
140 Hostparm
.OpenVMS
:= True;
143 if Debug_Flag_FF
then
144 Targparm
.Frontend_Layout_On_Target
:= True;
147 -- We take the default exception mechanism into account
149 if Targparm
.ZCX_By_Default_On_Target
then
150 if Targparm
.GCC_ZCX_Support_On_Target
then
151 Exception_Mechanism
:= GCC_ZCX
;
153 Exception_Mechanism
:= Front_End_ZCX
;
157 -- We take the command line exception mechanism into account
159 if Opt
.Zero_Cost_Exceptions_Set
then
160 if Opt
.Zero_Cost_Exceptions_Val
= False then
161 Exception_Mechanism
:= Setjmp_Longjmp
;
163 elsif Targparm
.GCC_ZCX_Support_On_Target
then
164 Exception_Mechanism
:= GCC_ZCX
;
166 elsif Targparm
.Front_End_ZCX_Support_On_Target
167 or else Debug_Flag_XX
169 Exception_Mechanism
:= Front_End_ZCX
;
173 ("Zero Cost Exceptions not supported on this target");
177 -- Check we have exactly one source file, this happens only in
178 -- the case where the driver is called directly, it cannot happen
179 -- when gnat1 is invoked from gcc in the normal case.
181 if Osint
.Number_Of_Files
/= 1 then
184 Osint
.Fail
("you must provide one source file");
186 elsif Usage_Requested
then
190 Original_Operating_Mode
:= Operating_Mode
;
192 Main_Unit_Node
:= Cunit
(Main_Unit
);
193 Main_Unit_Entity
:= Cunit_Entity
(Main_Unit
);
194 Main_Kind
:= Nkind
(Unit
(Main_Unit_Node
));
196 -- Check for suspicious or incorrect body present if we are doing
197 -- semantic checking. We omit this check in syntax only mode, because
198 -- in that case we do not know if we need a body or not.
200 if Operating_Mode
/= Check_Syntax
202 ((Main_Kind
= N_Package_Declaration
203 and then not Body_Required
(Main_Unit_Node
))
204 or else (Main_Kind
= N_Generic_Package_Declaration
205 and then not Body_Required
(Main_Unit_Node
))
206 or else Main_Kind
= N_Package_Renaming_Declaration
207 or else Main_Kind
= N_Subprogram_Renaming_Declaration
208 or else Nkind
(Original_Node
(Unit
(Main_Unit_Node
)))
209 in N_Generic_Instantiation
)
212 Sname
: Unit_Name_Type
:= Unit_Name
(Main_Unit
);
213 Src_Ind
: Source_File_Index
;
214 Fname
: File_Name_Type
;
216 procedure Bad_Body
(Msg
: String);
217 -- Issue message for bad body found
219 procedure Bad_Body
(Msg
: String) is
221 Error_Msg_N
(Msg
, Main_Unit_Node
);
222 Error_Msg_Name_1
:= Fname
;
224 ("remove incorrect body in file{!", Main_Unit_Node
);
228 Sname
:= Unit_Name
(Main_Unit
);
230 -- If we do not already have a body name, then get the body
231 -- name (but how can we have a body name here ???)
233 if not Is_Body_Name
(Sname
) then
234 Sname
:= Get_Body_Name
(Sname
);
237 Fname
:= Get_File_Name
(Sname
, Subunit
=> False);
238 Src_Ind
:= Load_Source_File
(Fname
);
240 -- Case where body is present and it is not a subunit. Exclude
241 -- the subunit case, because it has nothing to do with the
242 -- package we are compiling. It is illegal for a child unit
243 -- and a subunit with the same expanded name (RM 10.2(9)) to
244 -- appear together in a partition, but there is nothing to
245 -- stop a compilation environment from having both, and the
246 -- test here simply allows that. If there is an attempt to
247 -- include both in a partition, this is diagnosed at bind time.
248 -- In Ada 83 mode this is not a warning case.
250 if Src_Ind
/= No_Source_File
251 and then not Source_File_Is_Subunit
(Src_Ind
)
253 Error_Msg_Name_1
:= Sname
;
255 -- Ada 83 case of a package body being ignored. This is not
256 -- an error as far as the Ada 83 RM is concerned, but it is
257 -- almost certainly not what is wanted so output a warning.
258 -- Give this message only if there were no errors, since
259 -- otherwise it may be incorrect (we may have misinterpreted
260 -- a junk spec as not needing a body when it really does).
262 if Main_Kind
= N_Package_Declaration
264 and then Operating_Mode
= Generate_Code
265 and then Distribution_Stub_Mode
/= Generate_Caller_Stub_Body
266 and then not Compilation_Errors
269 ("package % does not require a body?!", Main_Unit_Node
);
270 Error_Msg_Name_1
:= Fname
;
272 ("body in file{?! will be ignored", Main_Unit_Node
);
274 -- Ada 95 cases of a body file present when no body is
275 -- permitted. This we consider to be an error.
278 -- For generic instantiations, we never allow a body
280 if Nkind
(Original_Node
(Unit
(Main_Unit_Node
)))
281 in N_Generic_Instantiation
284 ("generic instantiation for % does not allow a body");
286 -- A library unit that is a renaming never allows a body
288 elsif Main_Kind
in N_Renaming_Declaration
then
290 ("renaming declaration for % does not allow a body!");
292 -- Remaining cases are packages and generic packages.
293 -- Here we only do the test if there are no previous
294 -- errors, because if there are errors, they may lead
295 -- us to incorrectly believe that a package does not
296 -- allow a body when in fact it does.
298 elsif not Compilation_Errors
then
299 if Main_Kind
= N_Package_Declaration
then
300 Bad_Body
("package % does not allow a body!");
302 elsif Main_Kind
= N_Generic_Package_Declaration
then
303 Bad_Body
("generic package % does not allow a body!");
312 -- Exit if compilation errors detected
314 if Compilation_Errors
then
316 Sem_Ch13
.Validate_Unchecked_Conversions
;
320 -- Generate ALI file if specially requested
322 if Opt
.Force_ALI_Tree_File
then
323 Write_ALI
(Object
=> False);
327 Exit_Program
(E_Errors
);
330 -- Check for unused with's. We do this whether or not code is generated
332 Sem_Warn
.Check_Unused_Withs
;
334 -- Set Generate_Code on main unit and its spec. We do this even if
335 -- are not generating code, since Lib-Writ uses this to determine
336 -- which units get written in the ali file.
338 Set_Generate_Code
(Main_Unit
);
340 -- If we have a corresponding spec, then we need object
341 -- code for the spec unit as well
343 if Nkind
(Unit
(Main_Unit_Node
)) in N_Unit_Body
344 and then not Acts_As_Spec
(Main_Unit_Node
)
347 (Get_Cunit_Unit_Number
(Library_Unit
(Main_Unit_Node
)));
350 -- Check for unused with's. We do this whether or not code is generated
352 Sem_Warn
.Check_Unused_Withs
;
354 -- Case of no code required to be generated, exit indicating no error
356 if Original_Operating_Mode
= Check_Syntax
then
361 Exit_Program
(E_Success
);
363 elsif Original_Operating_Mode
= Check_Semantics
then
364 Back_End_Mode
:= Declarations_Only
;
366 -- All remaining cases are cases in which the user requested that code
367 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if
368 -- we can in fact satisfy this request.
370 -- Cannot generate code if someone has turned off code generation
371 -- for any reason at all. We will try to figure out a reason below.
373 elsif Operating_Mode
/= Generate_Code
then
374 Back_End_Mode
:= Skip
;
376 -- We can generate code for a subprogram body unless its corresponding
377 -- subprogram spec is a generic delaration. Note that the check for
378 -- No (Library_Unit) here is a defensive check that should not be
379 -- necessary, since the Library_Unit field should be set properly.
381 elsif Main_Kind
= N_Subprogram_Body
382 and then not Subunits_Missing
383 and then (No
(Library_Unit
(Main_Unit_Node
))
384 or else Nkind
(Unit
(Library_Unit
(Main_Unit_Node
))) /=
385 N_Generic_Subprogram_Declaration
386 or else Generic_Separately_Compiled
(Main_Unit_Entity
))
388 Back_End_Mode
:= Generate_Object
;
390 -- We can generate code for a package body unless its corresponding
391 -- package spec is a generic declaration. As described above, the
392 -- check for No (LIbrary_Unit) is a defensive check.
394 elsif Main_Kind
= N_Package_Body
395 and then not Subunits_Missing
396 and then (No
(Library_Unit
(Main_Unit_Node
))
397 or else Nkind
(Unit
(Library_Unit
(Main_Unit_Node
))) /=
398 N_Generic_Package_Declaration
399 or else Generic_Separately_Compiled
(Main_Unit_Entity
))
402 Back_End_Mode
:= Generate_Object
;
404 -- We can generate code for a package declaration or a subprogram
405 -- declaration only if it does not required a body.
407 elsif (Main_Kind
= N_Package_Declaration
409 Main_Kind
= N_Subprogram_Declaration
)
411 (not Body_Required
(Main_Unit_Node
)
413 Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
415 Back_End_Mode
:= Generate_Object
;
417 -- We can generate code for a generic package declaration of a generic
418 -- subprogram declaration only if does not require a body, and if it
419 -- is a generic that is separately compiled.
421 elsif (Main_Kind
= N_Generic_Package_Declaration
423 Main_Kind
= N_Generic_Subprogram_Declaration
)
424 and then not Body_Required
(Main_Unit_Node
)
425 and then Generic_Separately_Compiled
(Main_Unit_Entity
)
427 Back_End_Mode
:= Generate_Object
;
429 -- Compilation units that are renamings do not require bodies,
430 -- so we can generate code for them.
432 elsif Main_Kind
= N_Package_Renaming_Declaration
433 or else Main_Kind
= N_Subprogram_Renaming_Declaration
435 Back_End_Mode
:= Generate_Object
;
437 -- Compilation units that are generic renamings do not require bodies
438 -- so we can generate code for them in the separately compiled case
440 elsif Main_Kind
in N_Generic_Renaming_Declaration
441 and then Generic_Separately_Compiled
(Main_Unit_Entity
)
443 Back_End_Mode
:= Generate_Object
;
445 -- In all other cases (specs which have bodies, generics, and bodies
446 -- where subunits are missing), we cannot generate code and we generate
447 -- a warning message. Note that generic instantiations are gone at this
448 -- stage since they have been replaced by their instances.
451 Back_End_Mode
:= Skip
;
454 -- At this stage Call_Back_End is set to indicate if the backend
455 -- should be called to generate code. If it is not set, then code
456 -- generation has been turned off, even though code was requested
457 -- by the original command. This is not an error from the user
458 -- point of view, but it is an error from the point of view of
459 -- the gcc driver, so we must exit with an error status.
461 -- We generate an informative message (from the gcc point of view,
462 -- it is an error message, but from the users point of view this
463 -- is not an error, just a consequence of compiling something that
464 -- cannot generate code).
466 if Back_End_Mode
= Skip
then
467 Write_Str
("No code generated for ");
469 Write_Name
(Unit_File_Name
(Main_Unit
));
471 if Subunits_Missing
then
472 Write_Str
(" (missing subunits)");
474 elsif Main_Kind
= N_Subunit
then
475 Write_Str
(" (subunit)");
477 elsif Main_Kind
= N_Package_Body
478 or else Main_Kind
= N_Subprogram_Body
480 Write_Str
(" (generic unit)");
482 elsif Main_Kind
= N_Subprogram_Declaration
then
483 Write_Str
(" (subprogram spec)");
485 -- Only other case is a package spec
488 Write_Str
(" (package spec)");
493 Sem_Ch13
.Validate_Unchecked_Conversions
;
497 Write_ALI
(Object
=> False);
500 -- Exit program with error indication, to kill object file
502 Exit_Program
(E_No_Code
);
505 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
506 -- set as indicated by Back_Annotate_Rep_Info being set to True.
508 -- We don't call for annotations on a subunit, because to process those
509 -- the back-end requires that the parent(s) be properly compiled.
511 -- Annotation is also suppressed in the case of compiling for
512 -- the Java VM, since representations are largely symbolic there.
514 if Back_End_Mode
= Declarations_Only
515 and then (not (Back_Annotate_Rep_Info
or Debug_Flag_AA
)
516 or else Main_Kind
= N_Subunit
517 or else Hostparm
.Java_VM
)
519 Sem_Ch13
.Validate_Unchecked_Conversions
;
521 Write_ALI
(Object
=> False);
528 -- Ensure that we properly register a dependency on system.ads,
529 -- since even if we do not semantically depend on this, Targparm
530 -- has read system parameters from the system.ads file.
532 Lib
.Writ
.Ensure_System_Dependency
;
534 -- Back end needs to explicitly unlock tables it needs to touch
547 -- There are cases where the back end emits warnings, e.g. on objects
548 -- that are too large and will cause Storage_Error. If such a warning
549 -- appears in a generic context, then it is always appropriately
550 -- placed on the instance rather than the template, since gigi only
551 -- deals with generated code in instances (in particular the warning
552 -- for oversize objects clearly belongs on the instance).
554 Warn_On_Instance
:= True;
556 -- Here we call the backend to generate the output code
558 Back_End
.Call_Back_End
(Back_End_Mode
);
560 -- Once the backend is complete, we unlock the names table. This
561 -- call allows a few extra entries, needed for example for the file
562 -- name for the library file output.
566 -- Validate unchecked conversions (using the values for size
567 -- and alignment annotated by the backend where possible).
569 Sem_Ch13
.Validate_Unchecked_Conversions
;
571 -- Now we complete output of errors, rep info and the tree info.
572 -- These are delayed till now, since it is perfectly possible for
573 -- gigi to generate errors, modify the tree (in particular by setting
574 -- flags indicating that elaboration is required, and also to back
575 -- annotate representation information for List_Rep_Info.
579 if Opt
.List_Representation_Info
/= 0 or else Debug_Flag_AA
then
583 -- Only write the library if the backend did not generate any error
584 -- messages. Otherwise signal errors to the driver program so that
585 -- there will be no attempt to generate an object file.
587 if Compilation_Errors
then
589 Exit_Program
(E_Errors
);
592 Write_ALI
(Object
=> (Back_End_Mode
= Generate_Object
));
594 -- Generate the ASIS tree after writing the ALI file, since in
595 -- ASIS mode, Write_ALI may in fact result in further tree
596 -- decoration from the original tree file. Note that we dump
597 -- the tree just before generating it, so that the dump will
598 -- exactly reflect what is written out.
603 -- Finalize name table and we are all done
608 -- Handle fatal internal compiler errors
610 when System
.Assertions
.Assert_Failure
=>
611 Comperr
.Compiler_Abort
("Assert_Failure");
613 when Constraint_Error
=>
614 Comperr
.Compiler_Abort
("Constraint_Error");
616 when Program_Error
=>
617 Comperr
.Compiler_Abort
("Program_Error");
619 when Storage_Error
=>
621 -- Assume this is a bug. If it is real, the message will in
622 -- any case say Storage_Error, giving a strong hint!
624 Comperr
.Compiler_Abort
("Storage_Error");
627 -- The outer exception handles an unrecoverable error
630 when Unrecoverable_Error
=>
634 Write_Str
("compilation abandoned");
640 Exit_Program
(E_Errors
);