1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 Checks
; use Checks
;
29 with Einfo
; use Einfo
;
30 with Errout
; use Errout
;
31 with Expander
; use Expander
;
32 with Exp_Util
; use Exp_Util
;
33 with Freeze
; use Freeze
;
34 with Lib
.Xref
; use Lib
.Xref
;
35 with Nlists
; use Nlists
;
36 with Nmake
; use Nmake
;
39 with Sem_Case
; use Sem_Case
;
40 with Sem_Ch3
; use Sem_Ch3
;
41 with Sem_Ch8
; use Sem_Ch8
;
42 with Sem_Disp
; use Sem_Disp
;
43 with Sem_Eval
; use Sem_Eval
;
44 with Sem_Res
; use Sem_Res
;
45 with Sem_Type
; use Sem_Type
;
46 with Sem_Util
; use Sem_Util
;
47 with Sem_Warn
; use Sem_Warn
;
48 with Stand
; use Stand
;
49 with Sinfo
; use Sinfo
;
50 with Targparm
; use Targparm
;
51 with Tbuild
; use Tbuild
;
52 with Uintp
; use Uintp
;
54 package body Sem_Ch5
is
56 Unblocked_Exit_Count
: Nat
:= 0;
57 -- This variable is used when processing if statements or case
58 -- statements, it counts the number of branches of the conditional
59 -- that are not blocked by unconditional transfer instructions. At
60 -- the end of processing, if the count is zero, it means that control
61 -- cannot fall through the conditional statement. This is used for
62 -- the generation of warning messages. This variable is recursively
63 -- saved on entry to processing an if or case, and restored on exit.
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure Analyze_Iteration_Scheme
(N
: Node_Id
);
71 procedure Check_Possible_Current_Value_Condition
(Cnode
: Node_Id
);
72 -- Cnode is N_If_Statement, N_Elsif_Part, or N_Iteration_Scheme
73 -- (the latter when a WHILE condition is present). This call checks
74 -- if Condition (Cnode) is of the form ([NOT] var op val), where var
75 -- is a simple object, val is known at compile time, and op is one
76 -- of the six relational operators. If this is the case, and the
77 -- Current_Value field of "var" is not set, then it is set to Cnode.
78 -- See Exp_Util.Set_Current_Value_Condition for further details.
80 ------------------------
81 -- Analyze_Assignment --
82 ------------------------
84 procedure Analyze_Assignment
(N
: Node_Id
) is
85 Lhs
: constant Node_Id
:= Name
(N
);
86 Rhs
: constant Node_Id
:= Expression
(N
);
92 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
);
93 -- N is the node for the left hand side of an assignment, and it
94 -- is not a variable. This routine issues an appropriate diagnostic.
96 procedure Set_Assignment_Type
98 Opnd_Type
: in out Entity_Id
);
99 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type
100 -- is the nominal subtype. This procedure is used to deal with cases
101 -- where the nominal subtype must be replaced by the actual subtype.
103 -------------------------------
104 -- Diagnose_Non_Variable_Lhs --
105 -------------------------------
107 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
) is
109 -- Not worth posting another error if left hand side already
110 -- flagged as being illegal in some respect
112 if Error_Posted
(N
) then
115 -- Some special bad cases of entity names
117 elsif Is_Entity_Name
(N
) then
118 if Ekind
(Entity
(N
)) = E_In_Parameter
then
120 ("assignment to IN mode parameter not allowed", N
);
122 -- Private declarations in a protected object are turned into
123 -- constants when compiling a protected function.
125 elsif Present
(Scope
(Entity
(N
)))
126 and then Is_Protected_Type
(Scope
(Entity
(N
)))
128 (Ekind
(Current_Scope
) = E_Function
130 Ekind
(Enclosing_Dynamic_Scope
(Current_Scope
)) = E_Function
)
133 ("protected function cannot modify protected object", N
);
135 elsif Ekind
(Entity
(N
)) = E_Loop_Parameter
then
137 ("assignment to loop parameter not allowed", N
);
141 ("left hand side of assignment must be a variable", N
);
144 -- For indexed components or selected components, test prefix
146 elsif Nkind
(N
) = N_Indexed_Component
then
147 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
149 -- Another special case for assignment to discriminant.
151 elsif Nkind
(N
) = N_Selected_Component
then
152 if Present
(Entity
(Selector_Name
(N
)))
153 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
156 ("assignment to discriminant not allowed", N
);
158 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
162 -- If we fall through, we have no special message to issue!
164 Error_Msg_N
("left hand side of assignment must be a variable", N
);
166 end Diagnose_Non_Variable_Lhs
;
168 -------------------------
169 -- Set_Assignment_Type --
170 -------------------------
172 procedure Set_Assignment_Type
174 Opnd_Type
: in out Entity_Id
)
177 Require_Entity
(Opnd
);
179 -- If the assignment operand is an in-out or out parameter, then we
180 -- get the actual subtype (needed for the unconstrained case).
181 -- If the operand is the actual in an entry declaration, then within
182 -- the accept statement it is replaced with a local renaming, which
183 -- may also have an actual subtype.
185 if Is_Entity_Name
(Opnd
)
186 and then (Ekind
(Entity
(Opnd
)) = E_Out_Parameter
187 or else Ekind
(Entity
(Opnd
)) =
189 or else Ekind
(Entity
(Opnd
)) =
190 E_Generic_In_Out_Parameter
192 (Ekind
(Entity
(Opnd
)) = E_Variable
193 and then Nkind
(Parent
(Entity
(Opnd
))) =
194 N_Object_Renaming_Declaration
195 and then Nkind
(Parent
(Parent
(Entity
(Opnd
)))) =
198 Opnd_Type
:= Get_Actual_Subtype
(Opnd
);
200 -- If assignment operand is a component reference, then we get the
201 -- actual subtype of the component for the unconstrained case.
204 (Nkind
(Opnd
) = N_Selected_Component
205 or else Nkind
(Opnd
) = N_Explicit_Dereference
)
206 and then not Is_Unchecked_Union
(Opnd_Type
)
208 Decl
:= Build_Actual_Subtype_Of_Component
(Opnd_Type
, Opnd
);
210 if Present
(Decl
) then
211 Insert_Action
(N
, Decl
);
212 Mark_Rewrite_Insertion
(Decl
);
214 Opnd_Type
:= Defining_Identifier
(Decl
);
215 Set_Etype
(Opnd
, Opnd_Type
);
216 Freeze_Itype
(Opnd_Type
, N
);
218 elsif Is_Constrained
(Etype
(Opnd
)) then
219 Opnd_Type
:= Etype
(Opnd
);
222 -- For slice, use the constrained subtype created for the slice
224 elsif Nkind
(Opnd
) = N_Slice
then
225 Opnd_Type
:= Etype
(Opnd
);
227 end Set_Assignment_Type
;
229 -- Start of processing for Analyze_Assignment
236 -- In the most general case, both Lhs and Rhs can be overloaded, and we
237 -- must compute the intersection of the possible types on each side.
239 if Is_Overloaded
(Lhs
) then
246 Get_First_Interp
(Lhs
, I
, It
);
248 while Present
(It
.Typ
) loop
249 if Has_Compatible_Type
(Rhs
, It
.Typ
) then
250 if T1
/= Any_Type
then
252 -- An explicit dereference is overloaded if the prefix
253 -- is. Try to remove the ambiguity on the prefix, the
254 -- error will be posted there if the ambiguity is real.
256 if Nkind
(Lhs
) = N_Explicit_Dereference
then
259 PI1
: Interp_Index
:= 0;
265 Get_First_Interp
(Prefix
(Lhs
), PI
, PIt
);
267 while Present
(PIt
.Typ
) loop
268 if Is_Access_Type
(PIt
.Typ
)
269 and then Has_Compatible_Type
270 (Rhs
, Designated_Type
(PIt
.Typ
))
274 Disambiguate
(Prefix
(Lhs
),
277 if PIt
= No_Interp
then
279 ("ambiguous left-hand side"
280 & " in assignment", Lhs
);
283 Resolve
(Prefix
(Lhs
), PIt
.Typ
);
293 Get_Next_Interp
(PI
, PIt
);
299 ("ambiguous left-hand side in assignment", Lhs
);
307 Get_Next_Interp
(I
, It
);
311 if T1
= Any_Type
then
313 ("no valid types for left-hand side for assignment", Lhs
);
320 if not Is_Variable
(Lhs
) then
321 Diagnose_Non_Variable_Lhs
(Lhs
);
324 elsif Is_Limited_Type
(T1
)
325 and then not Assignment_OK
(Lhs
)
326 and then not Assignment_OK
(Original_Node
(Lhs
))
329 ("left hand of assignment must not be limited type", Lhs
);
330 Explain_Limited_Type
(T1
, Lhs
);
334 -- Resolution may have updated the subtype, in case the left-hand
335 -- side is a private protected component. Use the correct subtype
336 -- to avoid scoping issues in the back-end.
339 Set_Assignment_Type
(Lhs
, T1
);
342 Check_Unset_Reference
(Rhs
);
344 -- Remaining steps are skipped if Rhs was syntactically in error
352 if Covers
(T1
, T2
) then
355 Wrong_Type
(Rhs
, Etype
(Lhs
));
359 Set_Assignment_Type
(Rhs
, T2
);
361 if Total_Errors_Detected
/= 0 then
371 if T1
= Any_Type
or else T2
= Any_Type
then
375 if (Is_Class_Wide_Type
(T2
) or else Is_Dynamically_Tagged
(Rhs
))
376 and then not Is_Class_Wide_Type
(T1
)
378 Error_Msg_N
("dynamically tagged expression not allowed!", Rhs
);
380 elsif Is_Class_Wide_Type
(T1
)
381 and then not Is_Class_Wide_Type
(T2
)
382 and then not Is_Tag_Indeterminate
(Rhs
)
383 and then not Is_Dynamically_Tagged
(Rhs
)
385 Error_Msg_N
("dynamically tagged expression required!", Rhs
);
388 -- Tag propagation is done only in semantics mode only. If expansion
389 -- is on, the rhs tag indeterminate function call has been expanded
390 -- and tag propagation would have happened too late, so the
391 -- propagation take place in expand_call instead.
393 if not Expander_Active
394 and then Is_Class_Wide_Type
(T1
)
395 and then Is_Tag_Indeterminate
(Rhs
)
397 Propagate_Tag
(Lhs
, Rhs
);
402 if Extensions_Allowed
403 and then Nkind
(Rhs
) = N_Null
404 and then Is_Access_Type
(T1
)
405 and then not Assignment_OK
(Lhs
)
406 and then ((Is_Entity_Name
(Lhs
)
407 and then Can_Never_Be_Null
(Entity
(Lhs
)))
408 or else Can_Never_Be_Null
(Etype
(Lhs
)))
411 ("(Ada 0Y) NULL not allowed in null-excluding objects", Lhs
);
414 if Is_Scalar_Type
(T1
) then
415 Apply_Scalar_Range_Check
(Rhs
, Etype
(Lhs
));
417 elsif Is_Array_Type
(T1
)
419 (Nkind
(Rhs
) /= N_Type_Conversion
420 or else Is_Constrained
(Etype
(Rhs
)))
422 -- Assignment verifies that the length of the Lsh and Rhs are equal,
423 -- but of course the indices do not have to match. If the right-hand
424 -- side is a type conversion to an unconstrained type, a length check
425 -- is performed on the expression itself during expansion. In rare
426 -- cases, the redundant length check is computed on an index type
427 -- with a different representation, triggering incorrect code in
430 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
433 -- Discriminant checks are applied in the course of expansion
438 -- Note: modifications of the Lhs may only be recorded after
439 -- checks have been applied.
441 Note_Possible_Modification
(Lhs
);
443 -- ??? a real accessibility check is needed when ???
445 -- Post warning for useless assignment
447 if Warn_On_Redundant_Constructs
449 -- We only warn for source constructs
451 and then Comes_From_Source
(N
)
453 -- Where the entity is the same on both sides
455 and then Is_Entity_Name
(Lhs
)
456 and then Is_Entity_Name
(Original_Node
(Rhs
))
457 and then Entity
(Lhs
) = Entity
(Original_Node
(Rhs
))
459 -- But exclude the case where the right side was an operation
460 -- that got rewritten (e.g. JUNK + K, where K was known to be
461 -- zero). We don't want to warn in such a case, since it is
462 -- reasonable to write such expressions especially when K is
463 -- defined symbolically in some other package.
465 and then Nkind
(Original_Node
(Rhs
)) not in N_Op
468 ("?useless assignment of & to itself", N
, Entity
(Lhs
));
471 -- Check for non-allowed composite assignment
473 if not Support_Composite_Assign_On_Target
474 and then (Is_Array_Type
(T1
) or else Is_Record_Type
(T1
))
475 and then (not Has_Size_Clause
(T1
) or else Esize
(T1
) > 64)
477 Error_Msg_CRT
("composite assignment", N
);
480 -- One more step. Let's see if we have a simple assignment of a
481 -- known at compile time value to a simple variable. If so, we
482 -- can record the value as the current value providing that:
484 -- We still have a simple assignment statement (no expansion
485 -- activity has modified it in some peculiar manner)
487 -- The type is a discrete type
489 -- The assignment is to a named entity
491 -- The value is known at compile time
493 if Nkind
(N
) /= N_Assignment_Statement
494 or else not Is_Discrete_Type
(T1
)
495 or else not Is_Entity_Name
(Lhs
)
496 or else not Compile_Time_Known_Value
(Rhs
)
503 -- Capture value if save to do so
505 if Safe_To_Capture_Value
(N
, Ent
) then
506 Set_Current_Value
(Ent
, Rhs
);
508 end Analyze_Assignment
;
510 -----------------------------
511 -- Analyze_Block_Statement --
512 -----------------------------
514 procedure Analyze_Block_Statement
(N
: Node_Id
) is
515 Decls
: constant List_Id
:= Declarations
(N
);
516 Id
: constant Node_Id
:= Identifier
(N
);
517 Ent
: Entity_Id
:= Empty
;
520 -- If a label is present analyze it and mark it as referenced
526 -- An error defense. If we have an identifier, but no entity, then
527 -- something is wrong. If we have previous errors, then just remove
528 -- the identifier and continue, otherwise raise an exception.
531 if Total_Errors_Detected
/= 0 then
532 Set_Identifier
(N
, Empty
);
538 Set_Ekind
(Ent
, E_Block
);
539 Generate_Reference
(Ent
, N
, ' ');
540 Generate_Definition
(Ent
);
542 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
543 Set_Label_Construct
(Parent
(Ent
), N
);
548 -- If no entity set, create a label entity
551 Ent
:= New_Internal_Entity
(E_Block
, Current_Scope
, Sloc
(N
), 'B');
552 Set_Identifier
(N
, New_Occurrence_Of
(Ent
, Sloc
(N
)));
556 Set_Etype
(Ent
, Standard_Void_Type
);
557 Set_Block_Node
(Ent
, Identifier
(N
));
560 if Present
(Decls
) then
561 Analyze_Declarations
(Decls
);
565 Analyze
(Handled_Statement_Sequence
(N
));
566 Process_End_Label
(Handled_Statement_Sequence
(N
), 'e', Ent
);
568 -- Analyze exception handlers if present. Note that the test for
569 -- HSS being present is an error defence against previous errors.
571 if Present
(Handled_Statement_Sequence
(N
))
572 and then Present
(Exception_Handlers
(Handled_Statement_Sequence
(N
)))
575 S
: Entity_Id
:= Scope
(Ent
);
578 -- Indicate that enclosing scopes contain a block with handlers.
579 -- Only non-generic scopes need to be marked.
582 Set_Has_Nested_Block_With_Handler
(S
);
583 exit when Is_Overloadable
(S
)
584 or else Ekind
(S
) = E_Package
585 or else Is_Generic_Unit
(S
);
591 Check_References
(Ent
);
593 end Analyze_Block_Statement
;
595 ----------------------------
596 -- Analyze_Case_Statement --
597 ----------------------------
599 procedure Analyze_Case_Statement
(N
: Node_Id
) is
601 Statements_Analyzed
: Boolean := False;
602 -- Set True if at least some statement sequences get analyzed.
603 -- If False on exit, means we had a serious error that prevented
604 -- full analysis of the case statement, and as a result it is not
605 -- a good idea to output warning messages about unreachable code.
607 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
608 -- Recursively save value of this global, will be restored on exit
610 procedure Non_Static_Choice_Error
(Choice
: Node_Id
);
611 -- Error routine invoked by the generic instantiation below when
612 -- the case statment has a non static choice.
614 procedure Process_Statements
(Alternative
: Node_Id
);
615 -- Analyzes all the statements associated to a case alternative.
616 -- Needed by the generic instantiation below.
618 package Case_Choices_Processing
is new
619 Generic_Choices_Processing
620 (Get_Alternatives
=> Alternatives
,
621 Get_Choices
=> Discrete_Choices
,
622 Process_Empty_Choice
=> No_OP
,
623 Process_Non_Static_Choice
=> Non_Static_Choice_Error
,
624 Process_Associated_Node
=> Process_Statements
);
625 use Case_Choices_Processing
;
626 -- Instantiation of the generic choice processing package
628 -----------------------------
629 -- Non_Static_Choice_Error --
630 -----------------------------
632 procedure Non_Static_Choice_Error
(Choice
: Node_Id
) is
635 ("choice given in case statement is not static!", Choice
);
636 end Non_Static_Choice_Error
;
638 ------------------------
639 -- Process_Statements --
640 ------------------------
642 procedure Process_Statements
(Alternative
: Node_Id
) is
644 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
645 Statements_Analyzed
:= True;
646 Analyze_Statements
(Statements
(Alternative
));
647 end Process_Statements
;
649 -- Variables local to Analyze_Case_Statement.
652 Exp_Type
: Entity_Id
;
653 Exp_Btype
: Entity_Id
;
655 Case_Table
: Choice_Table_Type
(1 .. Number_Of_Choices
(N
));
658 Others_Present
: Boolean;
660 -- Start of processing for Analyze_Case_Statement
663 Unblocked_Exit_Count
:= 0;
664 Exp
:= Expression
(N
);
665 Analyze_And_Resolve
(Exp
, Any_Discrete
);
666 Check_Unset_Reference
(Exp
);
667 Exp_Type
:= Etype
(Exp
);
668 Exp_Btype
:= Base_Type
(Exp_Type
);
670 -- The expression must be of a discrete type which must be determinable
671 -- independently of the context in which the expression occurs, but
672 -- using the fact that the expression must be of a discrete type.
673 -- Moreover, the type this expression must not be a character literal
674 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
676 -- If error already reported by Resolve, nothing more to do
678 if Exp_Btype
= Any_Discrete
679 or else Exp_Btype
= Any_Type
683 elsif Exp_Btype
= Any_Character
then
685 ("character literal as case expression is ambiguous", Exp
);
689 and then (Is_Generic_Type
(Exp_Btype
)
690 or else Is_Generic_Type
(Root_Type
(Exp_Btype
)))
693 ("(Ada 83) case expression cannot be of a generic type", Exp
);
697 -- If the case expression is a formal object of mode in out, then
698 -- treat it as having a nonstatic subtype by forcing use of the base
699 -- type (which has to get passed to Check_Case_Choices below). Also
700 -- use base type when the case expression is parenthesized.
702 if Paren_Count
(Exp
) > 0
703 or else (Is_Entity_Name
(Exp
)
704 and then Ekind
(Entity
(Exp
)) = E_Generic_In_Out_Parameter
)
706 Exp_Type
:= Exp_Btype
;
709 -- Call instantiated Analyze_Choices which does the rest of the work
712 (N
, Exp_Type
, Case_Table
, Last_Choice
, Dont_Care
, Others_Present
);
714 if Exp_Type
= Universal_Integer
and then not Others_Present
then
715 Error_Msg_N
("case on universal integer requires OTHERS choice", Exp
);
718 -- If all our exits were blocked by unconditional transfers of control,
719 -- then the entire CASE statement acts as an unconditional transfer of
720 -- control, so treat it like one, and check unreachable code. Skip this
721 -- test if we had serious errors preventing any statement analysis.
723 if Unblocked_Exit_Count
= 0 and then Statements_Analyzed
then
724 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
725 Check_Unreachable_Code
(N
);
727 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
730 if not Expander_Active
731 and then Compile_Time_Known_Value
(Expression
(N
))
732 and then Serious_Errors_Detected
= 0
735 Chosen
: constant Node_Id
:= Find_Static_Alternative
(N
);
739 Alt
:= First
(Alternatives
(N
));
741 while Present
(Alt
) loop
742 if Alt
/= Chosen
then
743 Remove_Warning_Messages
(Statements
(Alt
));
750 end Analyze_Case_Statement
;
752 ----------------------------
753 -- Analyze_Exit_Statement --
754 ----------------------------
756 -- If the exit includes a name, it must be the name of a currently open
757 -- loop. Otherwise there must be an innermost open loop on the stack,
758 -- to which the statement implicitly refers.
760 procedure Analyze_Exit_Statement
(N
: Node_Id
) is
761 Target
: constant Node_Id
:= Name
(N
);
762 Cond
: constant Node_Id
:= Condition
(N
);
763 Scope_Id
: Entity_Id
;
769 Check_Unreachable_Code
(N
);
772 if Present
(Target
) then
774 U_Name
:= Entity
(Target
);
776 if not In_Open_Scopes
(U_Name
) or else Ekind
(U_Name
) /= E_Loop
then
777 Error_Msg_N
("invalid loop name in exit statement", N
);
780 Set_Has_Exit
(U_Name
);
787 for J
in reverse 0 .. Scope_Stack
.Last
loop
788 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
789 Kind
:= Ekind
(Scope_Id
);
792 and then (No
(Target
) or else Scope_Id
= U_Name
) then
793 Set_Has_Exit
(Scope_Id
);
796 elsif Kind
= E_Block
or else Kind
= E_Loop
then
801 ("cannot exit from program unit or accept statement", N
);
806 -- Verify that if present the condition is a Boolean expression
808 if Present
(Cond
) then
809 Analyze_And_Resolve
(Cond
, Any_Boolean
);
810 Check_Unset_Reference
(Cond
);
812 end Analyze_Exit_Statement
;
814 ----------------------------
815 -- Analyze_Goto_Statement --
816 ----------------------------
818 procedure Analyze_Goto_Statement
(N
: Node_Id
) is
819 Label
: constant Node_Id
:= Name
(N
);
820 Scope_Id
: Entity_Id
;
821 Label_Scope
: Entity_Id
;
824 Check_Unreachable_Code
(N
);
828 if Entity
(Label
) = Any_Id
then
831 elsif Ekind
(Entity
(Label
)) /= E_Label
then
832 Error_Msg_N
("target of goto statement must be a label", Label
);
835 elsif not Reachable
(Entity
(Label
)) then
836 Error_Msg_N
("target of goto statement is not reachable", Label
);
840 Label_Scope
:= Enclosing_Scope
(Entity
(Label
));
842 for J
in reverse 0 .. Scope_Stack
.Last
loop
843 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
845 if Label_Scope
= Scope_Id
846 or else (Ekind
(Scope_Id
) /= E_Block
847 and then Ekind
(Scope_Id
) /= E_Loop
)
849 if Scope_Id
/= Label_Scope
then
851 ("cannot exit from program unit or accept statement", N
);
859 end Analyze_Goto_Statement
;
861 --------------------------
862 -- Analyze_If_Statement --
863 --------------------------
865 -- A special complication arises in the analysis of if statements.
867 -- The expander has circuitry to completely delete code that it
868 -- can tell will not be executed (as a result of compile time known
869 -- conditions). In the analyzer, we ensure that code that will be
870 -- deleted in this manner is analyzed but not expanded. This is
871 -- obviously more efficient, but more significantly, difficulties
872 -- arise if code is expanded and then eliminated (e.g. exception
873 -- table entries disappear). Similarly, itypes generated in deleted
874 -- code must be frozen from start, because the nodes on which they
875 -- depend will not be available at the freeze point.
877 procedure Analyze_If_Statement
(N
: Node_Id
) is
880 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
881 -- Recursively save value of this global, will be restored on exit
883 Save_In_Deleted_Code
: Boolean;
885 Del
: Boolean := False;
886 -- This flag gets set True if a True condition has been found,
887 -- which means that remaining ELSE/ELSIF parts are deleted.
889 procedure Analyze_Cond_Then
(Cnode
: Node_Id
);
890 -- This is applied to either the N_If_Statement node itself or
891 -- to an N_Elsif_Part node. It deals with analyzing the condition
892 -- and the THEN statements associated with it.
894 -----------------------
895 -- Analyze_Cond_Then --
896 -----------------------
898 procedure Analyze_Cond_Then
(Cnode
: Node_Id
) is
899 Cond
: constant Node_Id
:= Condition
(Cnode
);
900 Tstm
: constant List_Id
:= Then_Statements
(Cnode
);
903 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
904 Analyze_And_Resolve
(Cond
, Any_Boolean
);
905 Check_Unset_Reference
(Cond
);
906 Check_Possible_Current_Value_Condition
(Cnode
);
908 -- If already deleting, then just analyze then statements
911 Analyze_Statements
(Tstm
);
913 -- Compile time known value, not deleting yet
915 elsif Compile_Time_Known_Value
(Cond
) then
916 Save_In_Deleted_Code
:= In_Deleted_Code
;
918 -- If condition is True, then analyze the THEN statements
919 -- and set no expansion for ELSE and ELSIF parts.
921 if Is_True
(Expr_Value
(Cond
)) then
922 Analyze_Statements
(Tstm
);
924 Expander_Mode_Save_And_Set
(False);
925 In_Deleted_Code
:= True;
927 -- If condition is False, analyze THEN with expansion off
929 else -- Is_False (Expr_Value (Cond))
930 Expander_Mode_Save_And_Set
(False);
931 In_Deleted_Code
:= True;
932 Analyze_Statements
(Tstm
);
933 Expander_Mode_Restore
;
934 In_Deleted_Code
:= Save_In_Deleted_Code
;
937 -- Not known at compile time, not deleting, normal analysis
940 Analyze_Statements
(Tstm
);
942 end Analyze_Cond_Then
;
944 -- Start of Analyze_If_Statement
947 -- Initialize exit count for else statements. If there is no else
948 -- part, this count will stay non-zero reflecting the fact that the
949 -- uncovered else case is an unblocked exit.
951 Unblocked_Exit_Count
:= 1;
952 Analyze_Cond_Then
(N
);
954 -- Now to analyze the elsif parts if any are present
956 if Present
(Elsif_Parts
(N
)) then
957 E
:= First
(Elsif_Parts
(N
));
958 while Present
(E
) loop
959 Analyze_Cond_Then
(E
);
964 if Present
(Else_Statements
(N
)) then
965 Analyze_Statements
(Else_Statements
(N
));
968 -- If all our exits were blocked by unconditional transfers of control,
969 -- then the entire IF statement acts as an unconditional transfer of
970 -- control, so treat it like one, and check unreachable code.
972 if Unblocked_Exit_Count
= 0 then
973 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
974 Check_Unreachable_Code
(N
);
976 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
980 Expander_Mode_Restore
;
981 In_Deleted_Code
:= Save_In_Deleted_Code
;
984 if not Expander_Active
985 and then Compile_Time_Known_Value
(Condition
(N
))
986 and then Serious_Errors_Detected
= 0
988 if Is_True
(Expr_Value
(Condition
(N
))) then
989 Remove_Warning_Messages
(Else_Statements
(N
));
991 if Present
(Elsif_Parts
(N
)) then
992 E
:= First
(Elsif_Parts
(N
));
994 while Present
(E
) loop
995 Remove_Warning_Messages
(Then_Statements
(E
));
1001 Remove_Warning_Messages
(Then_Statements
(N
));
1004 end Analyze_If_Statement
;
1006 ----------------------------------------
1007 -- Analyze_Implicit_Label_Declaration --
1008 ----------------------------------------
1010 -- An implicit label declaration is generated in the innermost
1011 -- enclosing declarative part. This is done for labels as well as
1012 -- block and loop names.
1014 -- Note: any changes in this routine may need to be reflected in
1015 -- Analyze_Label_Entity.
1017 procedure Analyze_Implicit_Label_Declaration
(N
: Node_Id
) is
1018 Id
: constant Node_Id
:= Defining_Identifier
(N
);
1021 Set_Ekind
(Id
, E_Label
);
1022 Set_Etype
(Id
, Standard_Void_Type
);
1023 Set_Enclosing_Scope
(Id
, Current_Scope
);
1024 end Analyze_Implicit_Label_Declaration
;
1026 ------------------------------
1027 -- Analyze_Iteration_Scheme --
1028 ------------------------------
1030 procedure Analyze_Iteration_Scheme
(N
: Node_Id
) is
1031 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
);
1032 -- If the bounds are given by a 'Range reference on a function call
1033 -- that returns a controlled array, introduce an explicit declaration
1034 -- to capture the bounds, so that the function result can be finalized
1035 -- in timely fashion.
1037 --------------------------------------
1038 -- Check_Controlled_Array_Attribute --
1039 --------------------------------------
1041 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
) is
1043 if Nkind
(DS
) = N_Attribute_Reference
1044 and then Is_Entity_Name
(Prefix
(DS
))
1045 and then Ekind
(Entity
(Prefix
(DS
))) = E_Function
1046 and then Is_Array_Type
(Etype
(Entity
(Prefix
(DS
))))
1049 Component_Type
(Etype
(Entity
(Prefix
(DS
)))))
1050 and then Expander_Active
1053 Loc
: constant Source_Ptr
:= Sloc
(N
);
1054 Arr
: constant Entity_Id
:=
1055 Etype
(Entity
(Prefix
(DS
)));
1056 Indx
: constant Entity_Id
:=
1057 Base_Type
(Etype
(First_Index
(Arr
)));
1058 Subt
: constant Entity_Id
:=
1059 Make_Defining_Identifier
1060 (Loc
, New_Internal_Name
('S'));
1065 Make_Subtype_Declaration
(Loc
,
1066 Defining_Identifier
=> Subt
,
1067 Subtype_Indication
=>
1068 Make_Subtype_Indication
(Loc
,
1069 Subtype_Mark
=> New_Reference_To
(Indx
, Loc
),
1071 Make_Range_Constraint
(Loc
,
1072 Relocate_Node
(DS
))));
1073 Insert_Before
(Parent
(N
), Decl
);
1077 Make_Attribute_Reference
(Loc
,
1078 Prefix
=> New_Reference_To
(Subt
, Loc
),
1079 Attribute_Name
=> Attribute_Name
(DS
)));
1083 end Check_Controlled_Array_Attribute
;
1085 -- Start of processing for Analyze_Iteration_Scheme
1088 -- For an infinite loop, there is no iteration scheme
1095 Cond
: constant Node_Id
:= Condition
(N
);
1098 -- For WHILE loop, verify that the condition is a Boolean
1099 -- expression and resolve and check it.
1101 if Present
(Cond
) then
1102 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1103 Check_Unset_Reference
(Cond
);
1105 -- Else we have a FOR loop
1109 LP
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
1110 Id
: constant Entity_Id
:= Defining_Identifier
(LP
);
1111 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(LP
);
1116 -- We always consider the loop variable to be referenced,
1117 -- since the loop may be used just for counting purposes.
1119 Generate_Reference
(Id
, N
, ' ');
1121 -- Check for case of loop variable hiding a local
1122 -- variable (used later on to give a nice warning
1123 -- if the hidden variable is never assigned).
1126 H
: constant Entity_Id
:= Homonym
(Id
);
1129 and then Enclosing_Dynamic_Scope
(H
) =
1130 Enclosing_Dynamic_Scope
(Id
)
1131 and then Ekind
(H
) = E_Variable
1132 and then Is_Discrete_Type
(Etype
(H
))
1134 Set_Hiding_Loop_Variable
(H
, Id
);
1138 -- Now analyze the subtype definition
1146 -- The subtype indication may denote the completion
1147 -- of an incomplete type declaration.
1149 if Is_Entity_Name
(DS
)
1150 and then Present
(Entity
(DS
))
1151 and then Is_Type
(Entity
(DS
))
1152 and then Ekind
(Entity
(DS
)) = E_Incomplete_Type
1154 Set_Entity
(DS
, Get_Full_View
(Entity
(DS
)));
1155 Set_Etype
(DS
, Entity
(DS
));
1158 if not Is_Discrete_Type
(Etype
(DS
)) then
1159 Wrong_Type
(DS
, Any_Discrete
);
1160 Set_Etype
(DS
, Any_Type
);
1163 Check_Controlled_Array_Attribute
(DS
);
1164 Make_Index
(DS
, LP
);
1166 Set_Ekind
(Id
, E_Loop_Parameter
);
1167 Set_Etype
(Id
, Etype
(DS
));
1168 Set_Is_Known_Valid
(Id
, True);
1170 -- The loop is not a declarative part, so the only entity
1171 -- declared "within" must be frozen explicitly.
1174 Flist
: constant List_Id
:= Freeze_Entity
(Id
, Sloc
(N
));
1176 if Is_Non_Empty_List
(Flist
) then
1177 Insert_Actions
(N
, Flist
);
1181 -- Check for null or possibly null range and issue warning.
1182 -- We suppress such messages in generic templates and
1183 -- instances, because in practice they tend to be dubious
1186 if Nkind
(DS
) = N_Range
1187 and then Comes_From_Source
(N
)
1190 L
: constant Node_Id
:= Low_Bound
(DS
);
1191 H
: constant Node_Id
:= High_Bound
(DS
);
1201 Determine_Range
(L
, LOK
, Llo
, Lhi
);
1202 Determine_Range
(H
, HOK
, Hlo
, Hhi
);
1204 -- If range of loop is null, issue warning
1206 if (LOK
and HOK
) and then Llo
> Hhi
then
1208 -- Suppress the warning if inside a generic
1209 -- template or instance, since in practice
1210 -- they tend to be dubious in these cases since
1211 -- they can result from intended parametrization.
1213 if not Inside_A_Generic
1214 and then not In_Instance
1217 ("?loop range is null, loop will not execute",
1221 -- Since we know the range of the loop is null,
1222 -- set the appropriate flag to suppress any
1223 -- warnings that would otherwise be issued in
1224 -- the body of the loop that will not execute.
1225 -- We do this even in the generic case, since
1226 -- if it is dubious to warn on the null loop
1227 -- itself, it is certainly dubious to warn for
1228 -- conditions that occur inside it!
1230 Set_Is_Null_Loop
(Parent
(N
));
1232 -- The other case for a warning is a reverse loop
1233 -- where the upper bound is the integer literal
1234 -- zero or one, and the lower bound can be positive.
1236 -- For example, we have
1238 -- for J in reverse N .. 1 loop
1240 -- In practice, this is very likely to be a case
1241 -- of reversing the bounds incorrectly in the range.
1243 elsif Reverse_Present
(LP
)
1244 and then Nkind
(H
) = N_Integer_Literal
1245 and then (Intval
(H
) = Uint_0
1247 Intval
(H
) = Uint_1
)
1250 Error_Msg_N
("?loop range may be null", DS
);
1258 end Analyze_Iteration_Scheme
;
1264 -- Note: the semantic work required for analyzing labels (setting them as
1265 -- reachable) was done in a prepass through the statements in the block,
1266 -- so that forward gotos would be properly handled. See Analyze_Statements
1267 -- for further details. The only processing required here is to deal with
1268 -- optimizations that depend on an assumption of sequential control flow,
1269 -- since of course the occurrence of a label breaks this assumption.
1271 procedure Analyze_Label
(N
: Node_Id
) is
1272 pragma Warnings
(Off
, N
);
1274 Kill_Current_Values
;
1277 --------------------------
1278 -- Analyze_Label_Entity --
1279 --------------------------
1281 procedure Analyze_Label_Entity
(E
: Entity_Id
) is
1283 Set_Ekind
(E
, E_Label
);
1284 Set_Etype
(E
, Standard_Void_Type
);
1285 Set_Enclosing_Scope
(E
, Current_Scope
);
1286 Set_Reachable
(E
, True);
1287 end Analyze_Label_Entity
;
1289 ----------------------------
1290 -- Analyze_Loop_Statement --
1291 ----------------------------
1293 procedure Analyze_Loop_Statement
(N
: Node_Id
) is
1294 Id
: constant Node_Id
:= Identifier
(N
);
1298 if Present
(Id
) then
1300 -- Make name visible, e.g. for use in exit statements. Loop
1301 -- labels are always considered to be referenced.
1305 Generate_Reference
(Ent
, N
, ' ');
1306 Generate_Definition
(Ent
);
1308 -- If we found a label, mark its type. If not, ignore it, since it
1309 -- means we have a conflicting declaration, which would already have
1310 -- been diagnosed at declaration time. Set Label_Construct of the
1311 -- implicit label declaration, which is not created by the parser
1312 -- for generic units.
1314 if Ekind
(Ent
) = E_Label
then
1315 Set_Ekind
(Ent
, E_Loop
);
1317 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
1318 Set_Label_Construct
(Parent
(Ent
), N
);
1322 -- Case of no identifier present
1325 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Sloc
(N
), 'L');
1326 Set_Etype
(Ent
, Standard_Void_Type
);
1327 Set_Parent
(Ent
, N
);
1330 -- Kill current values on entry to loop, since statements in body
1331 -- of loop may have been executed before the loop is entered.
1332 -- Similarly we kill values after the loop, since we do not know
1333 -- that the body of the loop was executed.
1335 Kill_Current_Values
;
1337 Analyze_Iteration_Scheme
(Iteration_Scheme
(N
));
1338 Analyze_Statements
(Statements
(N
));
1339 Process_End_Label
(N
, 'e', Ent
);
1341 Kill_Current_Values
;
1342 end Analyze_Loop_Statement
;
1344 ----------------------------
1345 -- Analyze_Null_Statement --
1346 ----------------------------
1348 -- Note: the semantics of the null statement is implemented by a single
1349 -- null statement, too bad everything isn't as simple as this!
1351 procedure Analyze_Null_Statement
(N
: Node_Id
) is
1352 pragma Warnings
(Off
, N
);
1355 end Analyze_Null_Statement
;
1357 ------------------------
1358 -- Analyze_Statements --
1359 ------------------------
1361 procedure Analyze_Statements
(L
: List_Id
) is
1366 -- The labels declared in the statement list are reachable from
1367 -- statements in the list. We do this as a prepass so that any
1368 -- goto statement will be properly flagged if its target is not
1369 -- reachable. This is not required, but is nice behavior!
1372 while Present
(S
) loop
1373 if Nkind
(S
) = N_Label
then
1374 Analyze
(Identifier
(S
));
1375 Lab
:= Entity
(Identifier
(S
));
1377 -- If we found a label mark it as reachable.
1379 if Ekind
(Lab
) = E_Label
then
1380 Generate_Definition
(Lab
);
1381 Set_Reachable
(Lab
);
1383 if Nkind
(Parent
(Lab
)) = N_Implicit_Label_Declaration
then
1384 Set_Label_Construct
(Parent
(Lab
), S
);
1387 -- If we failed to find a label, it means the implicit declaration
1388 -- of the label was hidden. A for-loop parameter can do this to
1389 -- a label with the same name inside the loop, since the implicit
1390 -- label declaration is in the innermost enclosing body or block
1394 Error_Msg_Sloc
:= Sloc
(Lab
);
1396 ("implicit label declaration for & is hidden#",
1404 -- Perform semantic analysis on all statements
1406 Conditional_Statements_Begin
;
1409 while Present
(S
) loop
1414 Conditional_Statements_End
;
1416 -- Make labels unreachable. Visibility is not sufficient, because
1417 -- labels in one if-branch for example are not reachable from the
1418 -- other branch, even though their declarations are in the enclosing
1419 -- declarative part.
1422 while Present
(S
) loop
1423 if Nkind
(S
) = N_Label
then
1424 Set_Reachable
(Entity
(Identifier
(S
)), False);
1429 end Analyze_Statements
;
1431 --------------------------------------------
1432 -- Check_Possible_Current_Value_Condition --
1433 --------------------------------------------
1435 procedure Check_Possible_Current_Value_Condition
(Cnode
: Node_Id
) is
1439 -- Loop to deal with (ignore for now) any NOT operators present
1441 Cond
:= Condition
(Cnode
);
1442 while Nkind
(Cond
) = N_Op_Not
loop
1443 Cond
:= Right_Opnd
(Cond
);
1446 -- Check possible relational operator
1448 if Nkind
(Cond
) = N_Op_Eq
1450 Nkind
(Cond
) = N_Op_Ne
1452 Nkind
(Cond
) = N_Op_Ge
1454 Nkind
(Cond
) = N_Op_Le
1456 Nkind
(Cond
) = N_Op_Gt
1458 Nkind
(Cond
) = N_Op_Lt
1460 if Compile_Time_Known_Value
(Right_Opnd
(Cond
))
1461 and then Nkind
(Left_Opnd
(Cond
)) = N_Identifier
1464 Ent
: constant Entity_Id
:= Entity
(Left_Opnd
(Cond
));
1467 if Ekind
(Ent
) = E_Variable
1469 Ekind
(Ent
) = E_Constant
1473 Ekind
(Ent
) = E_Loop_Parameter
1475 -- Here we have a case where the Current_Value field
1476 -- may need to be set. We set it if it is not already
1477 -- set to a compile time expression value.
1479 -- Note that this represents a decision that one
1480 -- condition blots out another previous one. That's
1481 -- certainly right if they occur at the same level.
1482 -- If the second one is nested, then the decision is
1483 -- neither right nor wrong (it would be equally OK
1484 -- to leave the outer one in place, or take the new
1485 -- inner one. Really we should record both, but our
1486 -- data structures are not that elaborate.
1488 if Nkind
(Current_Value
(Ent
)) not in N_Subexpr
then
1489 Set_Current_Value
(Ent
, Cnode
);
1495 end Check_Possible_Current_Value_Condition
;
1497 ----------------------------
1498 -- Check_Unreachable_Code --
1499 ----------------------------
1501 procedure Check_Unreachable_Code
(N
: Node_Id
) is
1502 Error_Loc
: Source_Ptr
;
1506 if Is_List_Member
(N
)
1507 and then Comes_From_Source
(N
)
1513 Nxt
:= Original_Node
(Next
(N
));
1516 and then Comes_From_Source
(Nxt
)
1517 and then Is_Statement
(Nxt
)
1519 -- Special very annoying exception. If we have a return that
1520 -- follows a raise, then we allow it without a warning, since
1521 -- the Ada RM annoyingly requires a useless return here!
1523 if Nkind
(Original_Node
(N
)) /= N_Raise_Statement
1524 or else Nkind
(Nxt
) /= N_Return_Statement
1526 -- The rather strange shenanigans with the warning message
1527 -- here reflects the fact that Kill_Dead_Code is very good
1528 -- at removing warnings in deleted code, and this is one
1529 -- warning we would prefer NOT to have removed :-)
1531 Error_Loc
:= Sloc
(Nxt
);
1533 -- If we have unreachable code, analyze and remove the
1534 -- unreachable code, since it is useless and we don't
1535 -- want to generate junk warnings.
1537 -- We skip this step if we are not in code generation mode.
1538 -- This is the one case where we remove dead code in the
1539 -- semantics as opposed to the expander, and we do not want
1540 -- to remove code if we are not in code generation mode,
1541 -- since this messes up the ASIS trees.
1543 -- Note that one might react by moving the whole circuit to
1544 -- exp_ch5, but then we lose the warning in -gnatc mode.
1546 if Operating_Mode
= Generate_Code
then
1550 -- Quit deleting when we have nothing more to delete
1551 -- or if we hit a label (since someone could transfer
1552 -- control to a label, so we should not delete it).
1554 exit when No
(Nxt
) or else Nkind
(Nxt
) = N_Label
;
1556 -- Statement/declaration is to be deleted
1560 Kill_Dead_Code
(Nxt
);
1564 -- Now issue the warning
1566 Error_Msg
("?unreachable code", Error_Loc
);
1569 -- If the unconditional transfer of control instruction is
1570 -- the last statement of a sequence, then see if our parent
1571 -- is an IF statement, and if so adjust the unblocked exit
1572 -- count of the if statement to reflect the fact that this
1573 -- branch of the if is indeed blocked by a transfer of control.
1578 if Nkind
(P
) = N_If_Statement
then
1581 elsif Nkind
(P
) = N_Elsif_Part
then
1583 pragma Assert
(Nkind
(P
) = N_If_Statement
);
1585 elsif Nkind
(P
) = N_Case_Statement_Alternative
then
1587 pragma Assert
(Nkind
(P
) = N_Case_Statement
);
1593 Unblocked_Exit_Count
:= Unblocked_Exit_Count
- 1;
1597 end Check_Unreachable_Code
;