1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Back_End
; use Back_End
;
30 with Csets
; use Csets
;
31 with Debug
; use Debug
;
33 with Errout
; use Errout
;
36 with Fname
; use Fname
;
37 with Fname
.UF
; use Fname
.UF
;
40 with Gnatvsn
; use Gnatvsn
;
43 with Lib
.Writ
; use Lib
.Writ
;
45 with Namet
; use Namet
;
48 with Osint
; use Osint
;
49 with Output
; use Output
;
52 with Repinfo
; use Repinfo
;
54 with Rident
; use Rident
;
65 with Sinfo
; use Sinfo
;
66 with Sinput
.L
; use Sinput
.L
;
68 with Sprint
; use Sprint
;
70 with Stylesw
; use Stylesw
;
71 with Targparm
; use Targparm
;
74 with Treepr
; use Treepr
;
76 with Types
; use Types
;
77 with Uintp
; use Uintp
;
78 with Uname
; use Uname
;
81 with Validsw
; use Validsw
;
83 with System
.Assertions
;
91 Main_Unit_Node
: Node_Id
;
92 -- Compilation unit node for main unit
94 Main_Kind
: Node_Kind
;
95 -- Kind of main compilation unit node
97 Back_End_Mode
: Back_End
.Back_End_Mode_Type
;
98 -- Record back end mode
100 procedure Adjust_Global_Switches
;
101 -- There are various interactions between front end switch settings,
102 -- including debug switch settings and target dependent parameters.
103 -- This procedure takes care of properly handling these interactions.
104 -- We do it after scanning out all the switches, so that we are not
105 -- depending on the order in which switches appear.
107 procedure Check_Bad_Body
;
108 -- Called to check if the unit we are compiling has a bad body
110 procedure Check_Rep_Info
;
111 -- Called when we are not generating code, to check if -gnatR was requested
112 -- and if so, explain that we will not be honoring the request.
114 procedure Post_Compilation_Validation_Checks
;
115 -- This procedure performs various validation checks that have to be left
116 -- to the end of the compilation process, after generating code but before
117 -- issuing error messages. In particular, these checks generally require
118 -- the information provided by the back end in back annotation of declared
119 -- entities (e.g. actual size and alignment values chosen by the back end).
121 ----------------------------
122 -- Adjust_Global_Switches --
123 ----------------------------
125 procedure Adjust_Global_Switches
is
127 -- -gnatd.M enables Relaxed_RM_Semantics
129 if Debug_Flag_Dot_MM
then
130 Relaxed_RM_Semantics
:= True;
133 -- -gnatd.1 enables unnesting of subprograms
135 if Debug_Flag_Dot_1
then
136 Unnest_Subprogram_Mode
:= True;
139 -- -gnatd.V or -gnatd.u enables special C expansion mode
141 if Debug_Flag_Dot_VV
or Debug_Flag_Dot_U
then
142 Modify_Tree_For_C
:= True;
145 -- Other flags set if we are generating C code
147 if Debug_Flag_Dot_VV
then
148 Generate_C_Code
:= True;
149 Unnest_Subprogram_Mode
:= True;
152 -- -gnatd.E sets Error_To_Warning mode, causing selected error messages
153 -- to be treated as warnings instead of errors.
155 if Debug_Flag_Dot_EE
then
156 Error_To_Warning
:= True;
159 -- Disable CodePeer_Mode in Check_Syntax, since we need front-end
162 if Operating_Mode
= Check_Syntax
then
163 CodePeer_Mode
:= False;
166 -- Set ASIS mode if -gnatt and -gnatc are set
168 if Operating_Mode
= Check_Semantics
and then Tree_Output
then
171 -- Turn off inlining in ASIS mode, since ASIS cannot handle the extra
172 -- information in the trees caused by inlining being active.
174 -- More specifically, the tree seems to be malformed from the ASIS
175 -- point of view if -gnatc and -gnatn appear together???
177 Inline_Active
:= False;
179 -- Turn off SCIL generation and CodePeer mode in semantics mode,
180 -- since SCIL requires front-end expansion.
182 Generate_SCIL
:= False;
183 CodePeer_Mode
:= False;
186 -- SCIL mode needs to disable front-end inlining since the generated
187 -- trees (in particular order and consistency between specs compiled
188 -- as part of a main unit or as part of a with-clause) are causing
191 if Generate_SCIL
then
192 Front_End_Inlining
:= False;
195 -- Tune settings for optimal SCIL generation in CodePeer mode
197 if CodePeer_Mode
then
199 -- Turn off gnatprove mode (which can be set via e.g. -gnatd.F), not
200 -- compatible with CodePeer mode.
202 GNATprove_Mode
:= False;
203 Debug_Flag_Dot_FF
:= False;
205 -- Turn off C tree generation, not compatible with CodePeer mode. We
206 -- do not expect this to happen in normal use, since both modes are
207 -- enabled by special tools, but it is useful to turn off these flags
208 -- this way when we are doing CodePeer tests on existing test suites
209 -- that may have -gnatd.V set, to avoid the need for special casing.
211 Modify_Tree_For_C
:= False;
212 Generate_C_Code
:= False;
213 Unnest_Subprogram_Mode
:= False;
215 -- Turn off inlining, confuses CodePeer output and gains nothing
217 Front_End_Inlining
:= False;
218 Inline_Active
:= False;
220 -- Disable front-end optimizations, to keep the tree as close to the
221 -- source code as possible, and also to avoid inconsistencies between
222 -- trees when using different optimization switches.
224 Optimization_Level
:= 0;
226 -- Enable some restrictions systematically to simplify the generated
227 -- code (and ease analysis). Note that restriction checks are also
228 -- disabled in CodePeer mode, see Restrict.Check_Restriction, and
229 -- user specified Restrictions pragmas are ignored, see
230 -- Sem_Prag.Process_Restrictions_Or_Restriction_Warnings.
232 Restrict
.Restrictions
.Set
(No_Initialize_Scalars
) := True;
233 Restrict
.Restrictions
.Set
(No_Task_Hierarchy
) := True;
234 Restrict
.Restrictions
.Set
(No_Abort_Statements
) := True;
235 Restrict
.Restrictions
.Set
(Max_Asynchronous_Select_Nesting
) := True;
236 Restrict
.Restrictions
.Value
(Max_Asynchronous_Select_Nesting
) := 0;
238 -- Suppress division by zero and access checks since they are handled
239 -- implicitly by CodePeer.
241 -- Turn off dynamic elaboration checks: generates inconsistencies in
242 -- trees between specs compiled as part of a main unit or as part of
245 -- Turn off alignment checks: these cannot be proved statically by
246 -- CodePeer and generate false positives.
248 -- Enable all other language checks
250 Suppress_Options
.Suppress
:=
251 (Access_Check
=> True,
252 Alignment_Check
=> True,
253 Division_Check
=> True,
254 Elaboration_Check
=> True,
257 Dynamic_Elaboration_Checks
:= False;
259 -- Set STRICT mode for overflow checks if not set explicitly. This
260 -- prevents suppressing of overflow checks by default, in code down
263 if Suppress_Options
.Overflow_Mode_General
= Not_Set
then
264 Suppress_Options
.Overflow_Mode_General
:= Strict
;
265 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
268 -- CodePeer handles division and overflow checks directly, based on
269 -- the marks set by the frontend, hence no special expansion should
270 -- be performed in the frontend for division and overflow checks.
272 Backend_Divide_Checks_On_Target
:= True;
273 Backend_Overflow_Checks_On_Target
:= True;
275 -- Kill debug of generated code, since it messes up sloc values
277 Debug_Generated_Code
:= False;
279 -- Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
280 -- Do we really need to spend time generating xref in CodePeer
281 -- mode??? Consider setting Xref_Active to False.
285 -- Polling mode forced off, since it generates confusing junk
287 Polling_Required
:= False;
289 -- Set operating mode to Generate_Code to benefit from full front-end
290 -- expansion (e.g. generics).
292 Operating_Mode
:= Generate_Code
;
294 -- We need SCIL generation of course
296 Generate_SCIL
:= True;
298 -- Enable assertions, since they give CodePeer valuable extra info
300 Assertions_Enabled
:= True;
302 -- Disable all simple value propagation. This is an optimization
303 -- which is valuable for code optimization, and also for generation
304 -- of compiler warnings, but these are being turned off by default,
305 -- and CodePeer generates better messages (referencing original
306 -- variables) this way.
308 Debug_Flag_MM
:= True;
310 -- Set normal RM validity checking, and checking of IN OUT parameters
311 -- (this might give CodePeer more useful checks to analyze, to be
312 -- confirmed???). All other validity checking is turned off, since
313 -- this can generate very complex trees that only confuse CodePeer
314 -- and do not bring enough useful info.
316 Reset_Validity_Check_Options
;
317 Validity_Check_Default
:= True;
318 Validity_Check_In_Out_Params
:= True;
319 Validity_Check_In_Params
:= True;
321 -- Turn off style check options and ignore any style check pragmas
322 -- since we are not interested in any front-end warnings when we are
323 -- getting CodePeer output.
325 Reset_Style_Check_Options
;
326 Ignore_Style_Checks_Pragmas
:= True;
328 -- Always perform semantics and generate ali files in CodePeer mode,
329 -- so that a gnatmake -c -k will proceed further when possible.
331 Force_ALI_Tree_File
:= True;
332 Try_Semantics
:= True;
334 -- Make the Ada front-end more liberal so that the compiler will
335 -- allow illegal code that is allowed by other compilers. CodePeer
336 -- is in the business of finding problems, not enforcing rules.
337 -- This is useful when using CodePeer mode with other compilers.
339 Relaxed_RM_Semantics
:= True;
342 -- Enable some individual switches that are implied by relaxed RM
345 if Relaxed_RM_Semantics
then
346 Opt
.Allow_Integer_Address
:= True;
347 Overriding_Renamings
:= True;
348 Treat_Categorization_Errors_As_Warnings
:= True;
351 -- Enable GNATprove_Mode when using -gnatd.F switch
353 if Debug_Flag_Dot_FF
then
354 GNATprove_Mode
:= True;
357 -- GNATprove_Mode is also activated by default in the gnat2why
360 if GNATprove_Mode
then
362 -- Turn off inlining, which would confuse formal verification output
365 Front_End_Inlining
:= False;
366 Inline_Active
:= False;
368 -- Issue warnings for failure to inline subprograms, as otherwise
369 -- expected in GNATprove mode for the local subprograms without
372 Ineffective_Inline_Warnings
:= True;
374 -- Disable front-end optimizations, to keep the tree as close to the
375 -- source code as possible, and also to avoid inconsistencies between
376 -- trees when using different optimization switches.
378 Optimization_Level
:= 0;
380 -- Enable some restrictions systematically to simplify the generated
381 -- code (and ease analysis). Note that restriction checks are also
382 -- disabled in SPARK mode, see Restrict.Check_Restriction, and user
383 -- specified Restrictions pragmas are ignored, see
384 -- Sem_Prag.Process_Restrictions_Or_Restriction_Warnings.
386 Restrict
.Restrictions
.Set
(No_Initialize_Scalars
) := True;
388 -- Note: at this point we used to suppress various checks, but that
389 -- is not what we want. We need the semantic processing for these
390 -- checks (which will set flags like Do_Overflow_Check, showing the
391 -- points at which potential checks are required semantically). We
392 -- don't want the expansion associated with these checks, but that
393 -- happens anyway because this expansion is simply not done in the
394 -- SPARK version of the expander.
396 -- On the contrary, we need to enable explicitly all language checks,
397 -- as they may have been suppressed by the use of switch -gnatp.
399 Suppress_Options
.Suppress
:= (others => False);
401 -- Turn off dynamic elaboration checks. SPARK mode depends on the
402 -- use of the static elaboration mode.
404 Dynamic_Elaboration_Checks
:= False;
406 -- Detect overflow on unconstrained floating-point types, such as
407 -- the predefined types Float, Long_Float and Long_Long_Float from
408 -- package Standard. Not necessary if float overflows are checked
409 -- (Machine_Overflow true), since appropriate Do_Overflow_Check flags
410 -- will be set in any case.
412 Check_Float_Overflow
:= not Machine_Overflows_On_Target
;
414 -- Set STRICT mode for overflow checks if not set explicitly. This
415 -- prevents suppressing of overflow checks by default, in code down
418 if Suppress_Options
.Overflow_Mode_General
= Not_Set
then
419 Suppress_Options
.Overflow_Mode_General
:= Strict
;
420 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
423 -- Kill debug of generated code, since it messes up sloc values
425 Debug_Generated_Code
:= False;
427 -- Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
428 -- as it is needed for computing effects of subprograms in the formal
429 -- verification backend.
433 -- Polling mode forced off, since it generates confusing junk
435 Polling_Required
:= False;
437 -- Set operating mode to Check_Semantics, but a light front-end
438 -- expansion is still performed.
440 Operating_Mode
:= Check_Semantics
;
442 -- Enable assertions, since they give valuable extra information for
443 -- formal verification.
445 Assertions_Enabled
:= True;
447 -- Disable validity checks, since it generates code raising
448 -- exceptions for invalid data, which confuses GNATprove. Invalid
449 -- data is directly detected by GNATprove's flow analysis.
451 Validity_Checks_On
:= False;
453 -- Turn off style check options since we are not interested in any
454 -- front-end warnings when we are getting SPARK output.
456 Reset_Style_Check_Options
;
458 -- Suppress the generation of name tables for enumerations, which are
459 -- not needed for formal verification, and fall outside the SPARK
460 -- subset (use of pointers).
462 Global_Discard_Names
:= True;
464 -- Suppress the expansion of tagged types and dispatching calls,
465 -- which lead to the generation of non-SPARK code (use of pointers),
466 -- which is more complex to formally verify than the original source.
468 Tagged_Type_Expansion
:= False;
471 -- Set Configurable_Run_Time mode if system.ads flag set or if the
472 -- special debug flag -gnatdY is set.
474 if Targparm
.Configurable_Run_Time_On_Target
or Debug_Flag_YY
then
475 Configurable_Run_Time_Mode
:= True;
478 -- Set -gnatR3m mode if debug flag A set
480 if Debug_Flag_AA
then
481 Back_Annotate_Rep_Info
:= True;
482 List_Representation_Info
:= 1;
483 List_Representation_Info_Mechanisms
:= True;
486 -- Force Target_Strict_Alignment true if debug flag -gnatd.a is set
488 if Debug_Flag_Dot_A
then
489 Ttypes
.Target_Strict_Alignment
:= True;
492 -- Increase size of allocated entities if debug flag -gnatd.N is set
494 if Debug_Flag_Dot_NN
then
495 Atree
.Num_Extension_Nodes
:= Atree
.Num_Extension_Nodes
+ 1;
498 -- Disable static allocation of dispatch tables if -gnatd.t or if layout
499 -- is enabled. The front end's layout phase currently treats types that
500 -- have discriminant-dependent arrays as not being static even when a
501 -- discriminant constraint on the type is static, and this leads to
502 -- problems with subtypes of type Ada.Tags.Dispatch_Table_Wrapper. ???
504 if Debug_Flag_Dot_T
or else Frontend_Layout_On_Target
then
505 Static_Dispatch_Tables
:= False;
508 -- Flip endian mode if -gnatd8 set
511 Ttypes
.Bytes_Big_Endian
:= not Ttypes
.Bytes_Big_Endian
;
514 -- Activate front end layout if debug flag -gnatdF is set
516 if Debug_Flag_FF
then
517 Targparm
.Frontend_Layout_On_Target
:= True;
520 -- Set and check exception mechanism
522 if Targparm
.ZCX_By_Default_On_Target
then
523 Exception_Mechanism
:= Back_End_Exceptions
;
526 -- Set proper status for overflow check mechanism
528 -- If already set (by -gnato or above in SPARK or CodePeer mode) then we
529 -- have nothing to do.
531 if Opt
.Suppress_Options
.Overflow_Mode_General
/= Not_Set
then
534 -- Otherwise set overflow mode defaults
537 -- Overflow checks are on by default (Suppress set False) except in
538 -- GNAT_Mode, where we want them off by default (we are not ready to
539 -- enable overflow checks in the compiler yet, for one thing the case
540 -- of 64-bit checks needs System.Arith_64 which is not a compiler
541 -- unit and it is a pain to try to include it in the compiler.
543 Suppress_Options
.Suppress
(Overflow_Check
) := GNAT_Mode
;
545 -- Set appropriate default overflow handling mode. Note: at present
546 -- we set STRICT in all three of the following cases. They are
547 -- separated because in the future we may make different choices.
549 -- By default set STRICT mode if -gnatg in effect
552 Suppress_Options
.Overflow_Mode_General
:= Strict
;
553 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
555 -- If we have backend divide and overflow checks, then by default
556 -- overflow checks are STRICT. Historically this code used to also
557 -- activate overflow checks, although no target currently has these
558 -- flags set, so this was dead code anyway.
560 elsif Targparm
.Backend_Divide_Checks_On_Target
562 Targparm
.Backend_Overflow_Checks_On_Target
564 Suppress_Options
.Overflow_Mode_General
:= Strict
;
565 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
567 -- Otherwise for now, default is STRICT mode. This may change in the
568 -- future, but for now this is the compatible behavior with previous
572 Suppress_Options
.Overflow_Mode_General
:= Strict
;
573 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
577 -- Set default for atomic synchronization. As this synchronization
578 -- between atomic accesses can be expensive, and not typically needed
579 -- on some targets, an optional target parameter can turn the option
580 -- off. Note Atomic Synchronization is implemented as check.
582 Suppress_Options
.Suppress
(Atomic_Synchronization
) :=
583 not Atomic_Sync_Default_On_Target
;
585 -- Set default for Alignment_Check, if we are on a machine with non-
586 -- strict alignment, then we suppress this check, since it is over-
587 -- zealous for such machines.
589 if not Ttypes
.Target_Strict_Alignment
then
590 Suppress_Options
.Suppress
(Alignment_Check
) := True;
593 -- Set switch indicating if back end can handle limited types, and
594 -- guarantee that no incorrect copies are made (e.g. in the context
595 -- of an if or case expression).
597 -- Debug flag -gnatd.L decisively sets usage on
599 if Debug_Flag_Dot_LL
then
600 Back_End_Handles_Limited_Types
:= True;
602 -- If no debug flag, usage off for AAMP, VM, SCIL cases
605 or else VM_Target
/= No_VM
606 or else Generate_SCIL
608 Back_End_Handles_Limited_Types
:= False;
610 -- Otherwise normal gcc back end, for now still turn flag off by
611 -- default, since there are unresolved problems in the front end.
614 Back_End_Handles_Limited_Types
:= False;
617 -- If the inlining level has not been set by the user, compute it from
618 -- the optimization level: 1 at -O1/-O2 (and -Os), 2 at -O3 and above.
620 if Inline_Level
= 0 then
621 if Optimization_Level
< 3 then
628 -- Treat -gnatn as equivalent to -gnatN for non-GCC targets
630 if Inline_Active
and not Front_End_Inlining
then
632 -- We really should have a tag for this, what if we added a new
633 -- back end some day, it would not be true for this test, but it
634 -- would be non-GCC, so this is a bit troublesome ???
636 Front_End_Inlining
:= VM_Target
/= No_VM
or else AAMP_On_Target
;
639 -- Set back end inlining indication
643 -- No back end inlining available for VM targets
647 -- No back end inlining available on AAMP
649 and then not AAMP_On_Target
651 -- No back end inlining in GNATprove mode, since it just confuses
652 -- the formal verification process.
654 and then not GNATprove_Mode
656 -- No back end inlining if front end inlining explicitly enabled.
657 -- Done to minimize the output differences to customers still using
658 -- this deprecated switch; in addition, this behavior reduces the
659 -- output differences in old tests.
661 and then not Front_End_Inlining
663 -- Back end inlining is disabled if debug flag .z is set
665 and then not Debug_Flag_Dot_Z
;
667 -- Output warning if -gnateE specified and cannot be supported
669 if Exception_Extra_Info
670 and then Restrict
.No_Exception_Handlers_Set
674 ("warning: extra exception information (-gnateE) was specified");
677 ("warning: this capability is not available in this configuration");
682 -- Finally capture adjusted value of Suppress_Options as the initial
683 -- value for Scope_Suppress, which will be modified as we move from
684 -- scope to scope (by Suppress/Unsuppress/Overflow_Checks pragmas).
686 Sem
.Scope_Suppress
:= Opt
.Suppress_Options
;
687 end Adjust_Global_Switches
;
693 procedure Check_Bad_Body
is
694 Sname
: Unit_Name_Type
;
695 Src_Ind
: Source_File_Index
;
696 Fname
: File_Name_Type
;
698 procedure Bad_Body_Error
(Msg
: String);
699 -- Issue message for bad body found
705 procedure Bad_Body_Error
(Msg
: String) is
707 Error_Msg_N
(Msg
, Main_Unit_Node
);
708 Error_Msg_File_1
:= Fname
;
709 Error_Msg_N
("remove incorrect body in file{!", Main_Unit_Node
);
712 -- Start of processing for Check_Bad_Body
715 -- Nothing to do if we are only checking syntax, because we don't know
716 -- enough to know if we require or forbid a body in this case.
718 if Operating_Mode
= Check_Syntax
then
722 -- Check for body not allowed
724 if (Main_Kind
= N_Package_Declaration
725 and then not Body_Required
(Main_Unit_Node
))
726 or else (Main_Kind
= N_Generic_Package_Declaration
727 and then not Body_Required
(Main_Unit_Node
))
728 or else Main_Kind
= N_Package_Renaming_Declaration
729 or else Main_Kind
= N_Subprogram_Renaming_Declaration
730 or else Nkind
(Original_Node
(Unit
(Main_Unit_Node
)))
731 in N_Generic_Instantiation
733 Sname
:= Unit_Name
(Main_Unit
);
735 -- If we do not already have a body name, then get the body name
737 if not Is_Body_Name
(Sname
) then
738 Sname
:= Get_Body_Name
(Sname
);
741 Fname
:= Get_File_Name
(Sname
, Subunit
=> False);
742 Src_Ind
:= Load_Source_File
(Fname
);
744 -- Case where body is present and it is not a subunit. Exclude the
745 -- subunit case, because it has nothing to do with the package we are
746 -- compiling. It is illegal for a child unit and a subunit with the
747 -- same expanded name (RM 10.2(9)) to appear together in a partition,
748 -- but there is nothing to stop a compilation environment from having
749 -- both, and the test here simply allows that. If there is an attempt
750 -- to include both in a partition, this is diagnosed at bind time. In
751 -- Ada 83 mode this is not a warning case.
753 -- Note that in general we do not give the message if the file in
754 -- question does not look like a body. This includes weird cases,
755 -- but in particular means that if the file is just a No_Body pragma,
756 -- then we won't give the message (that's the whole point of this
757 -- pragma, to be used this way and to cause the body file to be
758 -- ignored in this context).
760 if Src_Ind
/= No_Source_File
761 and then Source_File_Is_Body
(Src_Ind
)
763 Errout
.Finalize
(Last_Call
=> False);
765 Error_Msg_Unit_1
:= Sname
;
767 -- Ada 83 case of a package body being ignored. This is not an
768 -- error as far as the Ada 83 RM is concerned, but it is almost
769 -- certainly not what is wanted so output a warning. Give this
770 -- message only if there were no errors, since otherwise it may
771 -- be incorrect (we may have misinterpreted a junk spec as not
772 -- needing a body when it really does).
774 if Main_Kind
= N_Package_Declaration
775 and then Ada_Version
= Ada_83
776 and then Operating_Mode
= Generate_Code
777 and then Distribution_Stub_Mode
/= Generate_Caller_Stub_Body
778 and then not Compilation_Errors
781 ("package $$ does not require a body??", Main_Unit_Node
);
782 Error_Msg_File_1
:= Fname
;
783 Error_Msg_N
("body in file{ will be ignored??", Main_Unit_Node
);
785 -- Ada 95 cases of a body file present when no body is
786 -- permitted. This we consider to be an error.
789 -- For generic instantiations, we never allow a body
791 if Nkind
(Original_Node
(Unit
(Main_Unit_Node
))) in
792 N_Generic_Instantiation
795 ("generic instantiation for $$ does not allow a body");
797 -- A library unit that is a renaming never allows a body
799 elsif Main_Kind
in N_Renaming_Declaration
then
801 ("renaming declaration for $$ does not allow a body!");
803 -- Remaining cases are packages and generic packages. Here
804 -- we only do the test if there are no previous errors,
805 -- because if there are errors, they may lead us to
806 -- incorrectly believe that a package does not allow a
807 -- body when in fact it does.
809 elsif not Compilation_Errors
then
810 if Main_Kind
= N_Package_Declaration
then
812 ("package $$ does not allow a body!");
814 elsif Main_Kind
= N_Generic_Package_Declaration
then
816 ("generic package $$ does not allow a body!");
829 procedure Check_Rep_Info
is
831 if List_Representation_Info
/= 0
832 or else List_Representation_Info_Mechanisms
837 ("cannot generate representation information, no code generated");
844 ----------------------------------------
845 -- Post_Compilation_Validation_Checks --
846 ----------------------------------------
848 procedure Post_Compilation_Validation_Checks
is
850 -- Validate alignment check warnings. In some cases we generate warnings
851 -- about possible alignment errors because we don't know the alignment
852 -- that will be chosen by the back end. This routine is in charge of
853 -- getting rid of those warnings if we can tell they are not needed.
855 Checks
.Validate_Alignment_Check_Warnings
;
857 -- Validate unchecked conversions (using the values for size and
858 -- alignment annotated by the backend where possible).
860 Sem_Ch13
.Validate_Unchecked_Conversions
;
862 -- Validate address clauses (again using alignment values annotated
863 -- by the backend where possible).
865 Sem_Ch13
.Validate_Address_Clauses
;
867 -- Validate independence pragmas (again using values annotated by the
868 -- back end for component layout where possible) but only for non-GCC
869 -- back ends, as this is done a priori for GCC back ends.
871 if VM_Target
/= No_VM
or else AAMP_On_Target
then
872 Sem_Ch13
.Validate_Independence
;
875 end Post_Compilation_Validation_Checks
;
877 -- Start of processing for Gnat1drv
880 -- This inner block is set up to catch assertion errors and constraint
881 -- errors. Since the code for handling these errors can cause another
882 -- exception to be raised (namely Unrecoverable_Error), we need two
883 -- nested blocks, so that the outer one handles unrecoverable error.
886 -- Initialize all packages. For the most part, these initialization
887 -- calls can be made in any order. Exceptions are as follows:
889 -- Lib.Initialize need to be called before Scan_Compiler_Arguments,
890 -- because it initializes a table filled by Scan_Compiler_Arguments.
896 Scan_Compiler_Arguments
;
897 Osint
.Add_Default_Search_Dirs
;
918 Sem_Type
.Init_Interp_Tables
;
920 -- Capture compilation date and time
922 Opt
.Compilation_Time
:= System
.OS_Lib
.Current_Time_String
;
924 -- Get the target parameters only when -gnats is not used, to avoid
925 -- failing when there is no default runtime.
927 if Operating_Mode
/= Check_Syntax
then
929 -- Acquire target parameters from system.ads (package System source)
931 Targparm_Acquire
: declare
934 S
: Source_File_Index
;
938 Name_Buffer
(1 .. 10) := "system.ads";
941 S
:= Load_Source_File
(N
);
943 -- Failed to read system.ads, fatal error
945 if S
= No_Source_File
then
947 ("fatal error, run-time library not installed correctly");
948 Write_Line
("cannot locate file system.ads");
949 raise Unrecoverable_Error
;
951 -- Read system.ads successfully, remember its source index
954 System_Source_File_Index
:= S
;
957 -- Call to get target parameters. Note that the actual interface
958 -- routines are in Tbuild. They can't be in this procedure because
959 -- of accessibility issues.
961 Targparm
.Get_Target_Parameters
962 (System_Text
=> Source_Text
(S
),
963 Source_First
=> Source_First
(S
),
964 Source_Last
=> Source_Last
(S
),
965 Make_Id
=> Tbuild
.Make_Id
'Access,
966 Make_SC
=> Tbuild
.Make_SC
'Access,
967 Set_NOD
=> Tbuild
.Set_NOD
'Access,
968 Set_NSA
=> Tbuild
.Set_NSA
'Access,
969 Set_NUA
=> Tbuild
.Set_NUA
'Access,
970 Set_NUP
=> Tbuild
.Set_NUP
'Access);
972 -- Acquire configuration pragma information from Targparm
974 Restrict
.Restrictions
:= Targparm
.Restrictions_On_Target
;
975 end Targparm_Acquire
;
978 -- Perform various adjustments and settings of global switches
980 Adjust_Global_Switches
;
982 -- Output copyright notice if full list mode unless we have a list
983 -- file, in which case we defer this so that it is output in the file.
985 if (Verbose_Mode
or else (Full_List
and then Full_List_File_Name
= null))
987 -- Debug flag gnatd7 suppresses this copyright notice
989 and then not Debug_Flag_7
993 Write_Str
(Gnat_Version_String
);
995 Write_Str
("Copyright 1992-" & Current_Year
996 & ", Free Software Foundation, Inc.");
1000 -- Check we do not have more than one source file, this happens only in
1001 -- the case where the driver is called directly, it cannot happen when
1002 -- gnat1 is invoked from gcc in the normal case.
1004 if Osint
.Number_Of_Files
/= 1 then
1007 Osint
.Fail
("you must provide one source file");
1009 elsif Usage_Requested
then
1013 -- Generate target dependent output file if requested
1015 if Target_Dependent_Info_Write_Name
/= null then
1016 Set_Targ
.Write_Target_Dependent_Values
;
1019 -- Call the front end
1021 Original_Operating_Mode
:= Operating_Mode
;
1024 -- Exit with errors if the main source could not be parsed.
1026 if Sinput
.Main_Source_File
= No_Source_File
then
1027 Errout
.Finalize
(Last_Call
=> True);
1028 Errout
.Output_Messages
;
1029 Exit_Program
(E_Errors
);
1032 Main_Unit_Node
:= Cunit
(Main_Unit
);
1033 Main_Kind
:= Nkind
(Unit
(Main_Unit_Node
));
1036 -- In CodePeer mode we always delete old SCIL files before regenerating
1037 -- new ones, in case of e.g. errors, and also to remove obsolete scilx
1038 -- files generated by CodePeer itself.
1040 if CodePeer_Mode
then
1041 Comperr
.Delete_SCIL_Files
;
1044 -- Exit if compilation errors detected
1046 Errout
.Finalize
(Last_Call
=> False);
1048 if Compilation_Errors
then
1050 Post_Compilation_Validation_Checks
;
1051 Errout
.Output_Messages
;
1054 -- Generate ALI file if specially requested
1056 if Opt
.Force_ALI_Tree_File
then
1057 Write_ALI
(Object
=> False);
1061 Errout
.Finalize
(Last_Call
=> True);
1062 Exit_Program
(E_Errors
);
1065 -- Set Generate_Code on main unit and its spec. We do this even if are
1066 -- not generating code, since Lib-Writ uses this to determine which
1067 -- units get written in the ali file.
1069 Set_Generate_Code
(Main_Unit
);
1071 -- If we have a corresponding spec, and it comes from source or it is
1072 -- not a generated spec for a child subprogram body, then we need object
1073 -- code for the spec unit as well.
1075 if Nkind
(Unit
(Main_Unit_Node
)) in N_Unit_Body
1076 and then not Acts_As_Spec
(Main_Unit_Node
)
1078 if Nkind
(Unit
(Main_Unit_Node
)) = N_Subprogram_Body
1079 and then not Comes_From_Source
(Library_Unit
(Main_Unit_Node
))
1084 (Get_Cunit_Unit_Number
(Library_Unit
(Main_Unit_Node
)));
1088 -- Case of no code required to be generated, exit indicating no error
1090 if Original_Operating_Mode
= Check_Syntax
then
1092 Errout
.Finalize
(Last_Call
=> True);
1093 Errout
.Output_Messages
;
1098 -- Use a goto instead of calling Exit_Program so that finalization
1101 goto End_Of_Program
;
1103 elsif Original_Operating_Mode
= Check_Semantics
then
1104 Back_End_Mode
:= Declarations_Only
;
1106 -- All remaining cases are cases in which the user requested that code
1107 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if we
1108 -- can in fact satisfy this request.
1110 -- Cannot generate code if someone has turned off code generation for
1111 -- any reason at all. We will try to figure out a reason below.
1113 elsif Operating_Mode
/= Generate_Code
then
1114 Back_End_Mode
:= Skip
;
1116 -- We can generate code for a subprogram body unless there were missing
1117 -- subunits. Note that we always generate code for all generic units (a
1118 -- change from some previous versions of GNAT).
1120 elsif Main_Kind
= N_Subprogram_Body
and then not Subunits_Missing
then
1121 Back_End_Mode
:= Generate_Object
;
1123 -- We can generate code for a package body unless there are subunits
1124 -- missing (note that we always generate code for generic units, which
1125 -- is a change from some earlier versions of GNAT).
1127 elsif Main_Kind
= N_Package_Body
and then not Subunits_Missing
then
1128 Back_End_Mode
:= Generate_Object
;
1130 -- We can generate code for a package declaration or a subprogram
1131 -- declaration only if it does not required a body.
1133 elsif Nkind_In
(Main_Kind
,
1134 N_Package_Declaration
,
1135 N_Subprogram_Declaration
)
1137 (not Body_Required
(Main_Unit_Node
)
1139 Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
1141 Back_End_Mode
:= Generate_Object
;
1143 -- We can generate code for a generic package declaration of a generic
1144 -- subprogram declaration only if does not require a body.
1146 elsif Nkind_In
(Main_Kind
, N_Generic_Package_Declaration
,
1147 N_Generic_Subprogram_Declaration
)
1148 and then not Body_Required
(Main_Unit_Node
)
1150 Back_End_Mode
:= Generate_Object
;
1152 -- Compilation units that are renamings do not require bodies, so we can
1153 -- generate code for them.
1155 elsif Nkind_In
(Main_Kind
, N_Package_Renaming_Declaration
,
1156 N_Subprogram_Renaming_Declaration
)
1158 Back_End_Mode
:= Generate_Object
;
1160 -- Compilation units that are generic renamings do not require bodies
1161 -- so we can generate code for them.
1163 elsif Main_Kind
in N_Generic_Renaming_Declaration
then
1164 Back_End_Mode
:= Generate_Object
;
1166 -- It is not an error to analyze in CodePeer mode a spec which requires
1167 -- a body, in order to generate SCIL for this spec.
1169 elsif CodePeer_Mode
then
1170 Back_End_Mode
:= Generate_Object
;
1172 -- It is not an error to analyze in GNATprove mode a spec which requires
1173 -- a body, when the body is not available. During frame condition
1174 -- generation, the corresponding ALI file is generated. During
1175 -- analysis, the spec is analyzed.
1177 elsif GNATprove_Mode
then
1178 Back_End_Mode
:= Declarations_Only
;
1180 -- In all other cases (specs which have bodies, generics, and bodies
1181 -- where subunits are missing), we cannot generate code and we generate
1182 -- a warning message. Note that generic instantiations are gone at this
1183 -- stage since they have been replaced by their instances.
1186 Back_End_Mode
:= Skip
;
1189 -- At this stage Back_End_Mode is set to indicate if the backend should
1190 -- be called to generate code. If it is Skip, then code generation has
1191 -- been turned off, even though code was requested by the original
1192 -- command. This is not an error from the user point of view, but it is
1193 -- an error from the point of view of the gcc driver, so we must exit
1194 -- with an error status.
1196 -- We generate an informative message (from the gcc point of view, it
1197 -- is an error message, but from the users point of view this is not an
1198 -- error, just a consequence of compiling something that cannot
1201 if Back_End_Mode
= Skip
then
1203 Write_Str
("cannot generate code for ");
1204 Write_Str
("file ");
1205 Write_Name
(Unit_File_Name
(Main_Unit
));
1207 if Subunits_Missing
then
1208 Write_Str
(" (missing subunits)");
1211 -- Force generation of ALI file, for backward compatibility
1213 Opt
.Force_ALI_Tree_File
:= True;
1215 elsif Main_Kind
= N_Subunit
then
1216 Write_Str
(" (subunit)");
1219 -- Force generation of ALI file, for backward compatibility
1221 Opt
.Force_ALI_Tree_File
:= True;
1223 elsif Main_Kind
= N_Subprogram_Declaration
then
1224 Write_Str
(" (subprogram spec)");
1227 -- Generic package body in GNAT implementation mode
1229 elsif Main_Kind
= N_Package_Body
and then GNAT_Mode
then
1230 Write_Str
(" (predefined generic)");
1233 -- Force generation of ALI file, for backward compatibility
1235 Opt
.Force_ALI_Tree_File
:= True;
1237 -- Only other case is a package spec
1240 Write_Str
(" (package spec)");
1244 Set_Standard_Output
;
1246 Post_Compilation_Validation_Checks
;
1247 Errout
.Finalize
(Last_Call
=> True);
1248 Errout
.Output_Messages
;
1252 -- Generate ALI file if specially requested, or for missing subunits,
1253 -- subunits or predefined generic.
1255 if Opt
.Force_ALI_Tree_File
then
1256 Write_ALI
(Object
=> False);
1262 -- Exit program with error indication, to kill object file
1264 Exit_Program
(E_No_Code
);
1267 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also set
1268 -- as indicated by Back_Annotate_Rep_Info being set to True.
1270 -- We don't call for annotations on a subunit, because to process those
1271 -- the back-end requires that the parent(s) be properly compiled.
1273 -- Annotation is suppressed for targets where front-end layout is
1274 -- enabled, because the front end determines representations.
1276 -- Annotation is also suppressed in the case of compiling for a VM,
1277 -- since representations are largely symbolic there.
1279 if Back_End_Mode
= Declarations_Only
1281 (not (Back_Annotate_Rep_Info
or Generate_SCIL
or GNATprove_Mode
)
1282 or else Main_Kind
= N_Subunit
1283 or else Frontend_Layout_On_Target
1284 or else VM_Target
/= No_VM
)
1286 Post_Compilation_Validation_Checks
;
1287 Errout
.Finalize
(Last_Call
=> True);
1288 Errout
.Output_Messages
;
1289 Write_ALI
(Object
=> False);
1297 -- Ensure that we properly register a dependency on system.ads, since
1298 -- even if we do not semantically depend on this, Targparm has read
1299 -- system parameters from the system.ads file.
1301 Lib
.Writ
.Ensure_System_Dependency
;
1303 -- Add dependencies, if any, on preprocessing data file and on
1304 -- preprocessing definition file(s).
1306 Prepcomp
.Add_Dependencies
;
1308 -- In gnatprove mode we're writing the ALI much earlier than usual
1309 -- as flow analysis needs the file present in order to append its
1310 -- own globals to it.
1312 if GNATprove_Mode
then
1314 -- Note: In GNATprove mode, an "object" file is always generated as
1315 -- the result of calling gnat1 or gnat2why, although this is not the
1316 -- same as the object file produced for compilation.
1318 Write_ALI
(Object
=> True);
1321 -- Some back ends (for instance Gigi) are known to rely on SCOs for code
1322 -- generation. Make sure they are available.
1324 if Generate_SCO
then
1325 Par_SCO
.SCO_Record_Filtered
;
1328 -- Back end needs to explicitly unlock tables it needs to touch
1342 -- Here we call the back end to generate the output code
1344 Generating_Code
:= True;
1345 Back_End
.Call_Back_End
(Back_End_Mode
);
1347 -- Once the backend is complete, we unlock the names table. This call
1348 -- allows a few extra entries, needed for example for the file name for
1349 -- the library file output.
1353 -- Generate the call-graph output of dispatching calls
1355 Exp_CG
.Generate_CG_Output
;
1357 -- Perform post compilation validation checks
1359 Post_Compilation_Validation_Checks
;
1361 -- Now we complete output of errors, rep info and the tree info. These
1362 -- are delayed till now, since it is perfectly possible for gigi to
1363 -- generate errors, modify the tree (in particular by setting flags
1364 -- indicating that elaboration is required, and also to back annotate
1365 -- representation information for List_Rep_Info.
1367 Errout
.Finalize
(Last_Call
=> True);
1368 Errout
.Output_Messages
;
1369 List_Rep_Info
(Ttypes
.Bytes_Big_Endian
);
1370 Inline
.List_Inlining_Info
;
1372 -- Only write the library if the backend did not generate any error
1373 -- messages. Otherwise signal errors to the driver program so that
1374 -- there will be no attempt to generate an object file.
1376 if Compilation_Errors
then
1378 Exit_Program
(E_Errors
);
1381 if not GNATprove_Mode
then
1382 Write_ALI
(Object
=> (Back_End_Mode
= Generate_Object
));
1385 if not Compilation_Errors
then
1387 -- In case of ada backends, we need to make sure that the generated
1388 -- object file has a timestamp greater than the ALI file. We do this
1389 -- to make gnatmake happy when checking the ALI and obj timestamps,
1390 -- where it expects the object file being written after the ali file.
1392 -- Gnatmake's assumption is true for gcc platforms where the gcc
1393 -- wrapper needs to call the assembler after calling gnat1, but is
1394 -- not true for ada backends, where the object files are created
1395 -- directly by gnat1 (so are created before the ali file).
1397 Back_End
.Gen_Or_Update_Object_File
;
1400 -- Generate ASIS tree after writing the ALI file, since in ASIS mode,
1401 -- Write_ALI may in fact result in further tree decoration from the
1402 -- original tree file. Note that we dump the tree just before generating
1403 -- it, so that the dump will exactly reflect what is written out.
1408 -- Finalize name table and we are all done
1413 -- Handle fatal internal compiler errors
1415 when Rtsfind
.RE_Not_Available
=>
1416 Comperr
.Compiler_Abort
("RE_Not_Available");
1418 when System
.Assertions
.Assert_Failure
=>
1419 Comperr
.Compiler_Abort
("Assert_Failure");
1421 when Constraint_Error
=>
1422 Comperr
.Compiler_Abort
("Constraint_Error");
1424 when Program_Error
=>
1425 Comperr
.Compiler_Abort
("Program_Error");
1427 when Storage_Error
=>
1429 -- Assume this is a bug. If it is real, the message will in any case
1430 -- say Storage_Error, giving a strong hint.
1432 Comperr
.Compiler_Abort
("Storage_Error");
1438 -- The outer exception handles an unrecoverable error
1441 when Unrecoverable_Error
=>
1442 Errout
.Finalize
(Last_Call
=> True);
1443 Errout
.Output_Messages
;
1446 Write_Str
("compilation abandoned");
1449 Set_Standard_Output
;
1452 Exit_Program
(E_Errors
);