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 Ada_Version
>= Ada_05
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 2005) 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 Exp_Type
: Entity_Id
;
602 Exp_Btype
: Entity_Id
;
605 Others_Present
: Boolean;
607 Statements_Analyzed
: Boolean := False;
608 -- Set True if at least some statement sequences get analyzed.
609 -- If False on exit, means we had a serious error that prevented
610 -- full analysis of the case statement, and as a result it is not
611 -- a good idea to output warning messages about unreachable code.
613 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
614 -- Recursively save value of this global, will be restored on exit
616 procedure Non_Static_Choice_Error
(Choice
: Node_Id
);
617 -- Error routine invoked by the generic instantiation below when
618 -- the case statment has a non static choice.
620 procedure Process_Statements
(Alternative
: Node_Id
);
621 -- Analyzes all the statements associated to a case alternative.
622 -- Needed by the generic instantiation below.
624 package Case_Choices_Processing
is new
625 Generic_Choices_Processing
626 (Get_Alternatives
=> Alternatives
,
627 Get_Choices
=> Discrete_Choices
,
628 Process_Empty_Choice
=> No_OP
,
629 Process_Non_Static_Choice
=> Non_Static_Choice_Error
,
630 Process_Associated_Node
=> Process_Statements
);
631 use Case_Choices_Processing
;
632 -- Instantiation of the generic choice processing package
634 -----------------------------
635 -- Non_Static_Choice_Error --
636 -----------------------------
638 procedure Non_Static_Choice_Error
(Choice
: Node_Id
) is
641 ("choice given in case statement is not static!", Choice
);
642 end Non_Static_Choice_Error
;
644 ------------------------
645 -- Process_Statements --
646 ------------------------
648 procedure Process_Statements
(Alternative
: Node_Id
) is
649 Choices
: constant List_Id
:= Discrete_Choices
(Alternative
);
653 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
654 Statements_Analyzed
:= True;
656 -- An interesting optimization. If the case statement expression
657 -- is a simple entity, then we can set the current value within
658 -- an alternative if the alternative has one possible value.
662 -- when 2 | 3 => beta
663 -- when others => gamma
665 -- Here we know that N is initially 1 within alpha, but for beta
666 -- and gamma, we do not know anything more about the initial value.
668 if Is_Entity_Name
(Exp
) then
671 if Ekind
(Ent
) = E_Variable
673 Ekind
(Ent
) = E_In_Out_Parameter
675 Ekind
(Ent
) = E_Out_Parameter
677 if List_Length
(Choices
) = 1
678 and then Nkind
(First
(Choices
)) in N_Subexpr
679 and then Compile_Time_Known_Value
(First
(Choices
))
681 Set_Current_Value
(Entity
(Exp
), First
(Choices
));
684 Analyze_Statements
(Statements
(Alternative
));
686 -- After analyzing the case, set the current value to empty
687 -- since we won't know what it is for the next alternative
688 -- (unless reset by this same circuit), or after the case.
690 Set_Current_Value
(Entity
(Exp
), Empty
);
695 -- Case where expression is not an entity name of a variable
697 Analyze_Statements
(Statements
(Alternative
));
698 end Process_Statements
;
700 -- Table to record choices. Put after subprograms since we make
701 -- a call to Number_Of_Choices to get the right number of entries.
703 Case_Table
: Choice_Table_Type
(1 .. Number_Of_Choices
(N
));
705 -- Start of processing for Analyze_Case_Statement
708 Unblocked_Exit_Count
:= 0;
709 Exp
:= Expression
(N
);
710 Analyze_And_Resolve
(Exp
, Any_Discrete
);
711 Check_Unset_Reference
(Exp
);
712 Exp_Type
:= Etype
(Exp
);
713 Exp_Btype
:= Base_Type
(Exp_Type
);
715 -- The expression must be of a discrete type which must be determinable
716 -- independently of the context in which the expression occurs, but
717 -- using the fact that the expression must be of a discrete type.
718 -- Moreover, the type this expression must not be a character literal
719 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
721 -- If error already reported by Resolve, nothing more to do
723 if Exp_Btype
= Any_Discrete
724 or else Exp_Btype
= Any_Type
728 elsif Exp_Btype
= Any_Character
then
730 ("character literal as case expression is ambiguous", Exp
);
733 elsif Ada_Version
= Ada_83
734 and then (Is_Generic_Type
(Exp_Btype
)
735 or else Is_Generic_Type
(Root_Type
(Exp_Btype
)))
738 ("(Ada 83) case expression cannot be of a generic type", Exp
);
742 -- If the case expression is a formal object of mode in out, then
743 -- treat it as having a nonstatic subtype by forcing use of the base
744 -- type (which has to get passed to Check_Case_Choices below). Also
745 -- use base type when the case expression is parenthesized.
747 if Paren_Count
(Exp
) > 0
748 or else (Is_Entity_Name
(Exp
)
749 and then Ekind
(Entity
(Exp
)) = E_Generic_In_Out_Parameter
)
751 Exp_Type
:= Exp_Btype
;
754 -- Call instantiated Analyze_Choices which does the rest of the work
757 (N
, Exp_Type
, Case_Table
, Last_Choice
, Dont_Care
, Others_Present
);
759 if Exp_Type
= Universal_Integer
and then not Others_Present
then
760 Error_Msg_N
("case on universal integer requires OTHERS choice", Exp
);
763 -- If all our exits were blocked by unconditional transfers of control,
764 -- then the entire CASE statement acts as an unconditional transfer of
765 -- control, so treat it like one, and check unreachable code. Skip this
766 -- test if we had serious errors preventing any statement analysis.
768 if Unblocked_Exit_Count
= 0 and then Statements_Analyzed
then
769 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
770 Check_Unreachable_Code
(N
);
772 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
775 if not Expander_Active
776 and then Compile_Time_Known_Value
(Expression
(N
))
777 and then Serious_Errors_Detected
= 0
780 Chosen
: constant Node_Id
:= Find_Static_Alternative
(N
);
784 Alt
:= First
(Alternatives
(N
));
786 while Present
(Alt
) loop
787 if Alt
/= Chosen
then
788 Remove_Warning_Messages
(Statements
(Alt
));
795 end Analyze_Case_Statement
;
797 ----------------------------
798 -- Analyze_Exit_Statement --
799 ----------------------------
801 -- If the exit includes a name, it must be the name of a currently open
802 -- loop. Otherwise there must be an innermost open loop on the stack,
803 -- to which the statement implicitly refers.
805 procedure Analyze_Exit_Statement
(N
: Node_Id
) is
806 Target
: constant Node_Id
:= Name
(N
);
807 Cond
: constant Node_Id
:= Condition
(N
);
808 Scope_Id
: Entity_Id
;
814 Check_Unreachable_Code
(N
);
817 if Present
(Target
) then
819 U_Name
:= Entity
(Target
);
821 if not In_Open_Scopes
(U_Name
) or else Ekind
(U_Name
) /= E_Loop
then
822 Error_Msg_N
("invalid loop name in exit statement", N
);
825 Set_Has_Exit
(U_Name
);
832 for J
in reverse 0 .. Scope_Stack
.Last
loop
833 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
834 Kind
:= Ekind
(Scope_Id
);
837 and then (No
(Target
) or else Scope_Id
= U_Name
) then
838 Set_Has_Exit
(Scope_Id
);
841 elsif Kind
= E_Block
or else Kind
= E_Loop
then
846 ("cannot exit from program unit or accept statement", N
);
851 -- Verify that if present the condition is a Boolean expression
853 if Present
(Cond
) then
854 Analyze_And_Resolve
(Cond
, Any_Boolean
);
855 Check_Unset_Reference
(Cond
);
857 end Analyze_Exit_Statement
;
859 ----------------------------
860 -- Analyze_Goto_Statement --
861 ----------------------------
863 procedure Analyze_Goto_Statement
(N
: Node_Id
) is
864 Label
: constant Node_Id
:= Name
(N
);
865 Scope_Id
: Entity_Id
;
866 Label_Scope
: Entity_Id
;
869 Check_Unreachable_Code
(N
);
873 if Entity
(Label
) = Any_Id
then
876 elsif Ekind
(Entity
(Label
)) /= E_Label
then
877 Error_Msg_N
("target of goto statement must be a label", Label
);
880 elsif not Reachable
(Entity
(Label
)) then
881 Error_Msg_N
("target of goto statement is not reachable", Label
);
885 Label_Scope
:= Enclosing_Scope
(Entity
(Label
));
887 for J
in reverse 0 .. Scope_Stack
.Last
loop
888 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
890 if Label_Scope
= Scope_Id
891 or else (Ekind
(Scope_Id
) /= E_Block
892 and then Ekind
(Scope_Id
) /= E_Loop
)
894 if Scope_Id
/= Label_Scope
then
896 ("cannot exit from program unit or accept statement", N
);
904 end Analyze_Goto_Statement
;
906 --------------------------
907 -- Analyze_If_Statement --
908 --------------------------
910 -- A special complication arises in the analysis of if statements.
912 -- The expander has circuitry to completely delete code that it
913 -- can tell will not be executed (as a result of compile time known
914 -- conditions). In the analyzer, we ensure that code that will be
915 -- deleted in this manner is analyzed but not expanded. This is
916 -- obviously more efficient, but more significantly, difficulties
917 -- arise if code is expanded and then eliminated (e.g. exception
918 -- table entries disappear). Similarly, itypes generated in deleted
919 -- code must be frozen from start, because the nodes on which they
920 -- depend will not be available at the freeze point.
922 procedure Analyze_If_Statement
(N
: Node_Id
) is
925 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
926 -- Recursively save value of this global, will be restored on exit
928 Save_In_Deleted_Code
: Boolean;
930 Del
: Boolean := False;
931 -- This flag gets set True if a True condition has been found,
932 -- which means that remaining ELSE/ELSIF parts are deleted.
934 procedure Analyze_Cond_Then
(Cnode
: Node_Id
);
935 -- This is applied to either the N_If_Statement node itself or
936 -- to an N_Elsif_Part node. It deals with analyzing the condition
937 -- and the THEN statements associated with it.
939 -----------------------
940 -- Analyze_Cond_Then --
941 -----------------------
943 procedure Analyze_Cond_Then
(Cnode
: Node_Id
) is
944 Cond
: constant Node_Id
:= Condition
(Cnode
);
945 Tstm
: constant List_Id
:= Then_Statements
(Cnode
);
948 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
949 Analyze_And_Resolve
(Cond
, Any_Boolean
);
950 Check_Unset_Reference
(Cond
);
951 Check_Possible_Current_Value_Condition
(Cnode
);
953 -- If already deleting, then just analyze then statements
956 Analyze_Statements
(Tstm
);
958 -- Compile time known value, not deleting yet
960 elsif Compile_Time_Known_Value
(Cond
) then
961 Save_In_Deleted_Code
:= In_Deleted_Code
;
963 -- If condition is True, then analyze the THEN statements
964 -- and set no expansion for ELSE and ELSIF parts.
966 if Is_True
(Expr_Value
(Cond
)) then
967 Analyze_Statements
(Tstm
);
969 Expander_Mode_Save_And_Set
(False);
970 In_Deleted_Code
:= True;
972 -- If condition is False, analyze THEN with expansion off
974 else -- Is_False (Expr_Value (Cond))
975 Expander_Mode_Save_And_Set
(False);
976 In_Deleted_Code
:= True;
977 Analyze_Statements
(Tstm
);
978 Expander_Mode_Restore
;
979 In_Deleted_Code
:= Save_In_Deleted_Code
;
982 -- Not known at compile time, not deleting, normal analysis
985 Analyze_Statements
(Tstm
);
987 end Analyze_Cond_Then
;
989 -- Start of Analyze_If_Statement
992 -- Initialize exit count for else statements. If there is no else
993 -- part, this count will stay non-zero reflecting the fact that the
994 -- uncovered else case is an unblocked exit.
996 Unblocked_Exit_Count
:= 1;
997 Analyze_Cond_Then
(N
);
999 -- Now to analyze the elsif parts if any are present
1001 if Present
(Elsif_Parts
(N
)) then
1002 E
:= First
(Elsif_Parts
(N
));
1003 while Present
(E
) loop
1004 Analyze_Cond_Then
(E
);
1009 if Present
(Else_Statements
(N
)) then
1010 Analyze_Statements
(Else_Statements
(N
));
1013 -- If all our exits were blocked by unconditional transfers of control,
1014 -- then the entire IF statement acts as an unconditional transfer of
1015 -- control, so treat it like one, and check unreachable code.
1017 if Unblocked_Exit_Count
= 0 then
1018 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1019 Check_Unreachable_Code
(N
);
1021 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1025 Expander_Mode_Restore
;
1026 In_Deleted_Code
:= Save_In_Deleted_Code
;
1029 if not Expander_Active
1030 and then Compile_Time_Known_Value
(Condition
(N
))
1031 and then Serious_Errors_Detected
= 0
1033 if Is_True
(Expr_Value
(Condition
(N
))) then
1034 Remove_Warning_Messages
(Else_Statements
(N
));
1036 if Present
(Elsif_Parts
(N
)) then
1037 E
:= First
(Elsif_Parts
(N
));
1039 while Present
(E
) loop
1040 Remove_Warning_Messages
(Then_Statements
(E
));
1046 Remove_Warning_Messages
(Then_Statements
(N
));
1049 end Analyze_If_Statement
;
1051 ----------------------------------------
1052 -- Analyze_Implicit_Label_Declaration --
1053 ----------------------------------------
1055 -- An implicit label declaration is generated in the innermost
1056 -- enclosing declarative part. This is done for labels as well as
1057 -- block and loop names.
1059 -- Note: any changes in this routine may need to be reflected in
1060 -- Analyze_Label_Entity.
1062 procedure Analyze_Implicit_Label_Declaration
(N
: Node_Id
) is
1063 Id
: constant Node_Id
:= Defining_Identifier
(N
);
1066 Set_Ekind
(Id
, E_Label
);
1067 Set_Etype
(Id
, Standard_Void_Type
);
1068 Set_Enclosing_Scope
(Id
, Current_Scope
);
1069 end Analyze_Implicit_Label_Declaration
;
1071 ------------------------------
1072 -- Analyze_Iteration_Scheme --
1073 ------------------------------
1075 procedure Analyze_Iteration_Scheme
(N
: Node_Id
) is
1076 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
);
1077 -- If the bounds are given by a 'Range reference on a function call
1078 -- that returns a controlled array, introduce an explicit declaration
1079 -- to capture the bounds, so that the function result can be finalized
1080 -- in timely fashion.
1082 --------------------------------------
1083 -- Check_Controlled_Array_Attribute --
1084 --------------------------------------
1086 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
) is
1088 if Nkind
(DS
) = N_Attribute_Reference
1089 and then Is_Entity_Name
(Prefix
(DS
))
1090 and then Ekind
(Entity
(Prefix
(DS
))) = E_Function
1091 and then Is_Array_Type
(Etype
(Entity
(Prefix
(DS
))))
1094 Component_Type
(Etype
(Entity
(Prefix
(DS
)))))
1095 and then Expander_Active
1098 Loc
: constant Source_Ptr
:= Sloc
(N
);
1099 Arr
: constant Entity_Id
:=
1100 Etype
(Entity
(Prefix
(DS
)));
1101 Indx
: constant Entity_Id
:=
1102 Base_Type
(Etype
(First_Index
(Arr
)));
1103 Subt
: constant Entity_Id
:=
1104 Make_Defining_Identifier
1105 (Loc
, New_Internal_Name
('S'));
1110 Make_Subtype_Declaration
(Loc
,
1111 Defining_Identifier
=> Subt
,
1112 Subtype_Indication
=>
1113 Make_Subtype_Indication
(Loc
,
1114 Subtype_Mark
=> New_Reference_To
(Indx
, Loc
),
1116 Make_Range_Constraint
(Loc
,
1117 Relocate_Node
(DS
))));
1118 Insert_Before
(Parent
(N
), Decl
);
1122 Make_Attribute_Reference
(Loc
,
1123 Prefix
=> New_Reference_To
(Subt
, Loc
),
1124 Attribute_Name
=> Attribute_Name
(DS
)));
1128 end Check_Controlled_Array_Attribute
;
1130 -- Start of processing for Analyze_Iteration_Scheme
1133 -- For an infinite loop, there is no iteration scheme
1140 Cond
: constant Node_Id
:= Condition
(N
);
1143 -- For WHILE loop, verify that the condition is a Boolean
1144 -- expression and resolve and check it.
1146 if Present
(Cond
) then
1147 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1148 Check_Unset_Reference
(Cond
);
1150 -- Else we have a FOR loop
1154 LP
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
1155 Id
: constant Entity_Id
:= Defining_Identifier
(LP
);
1156 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(LP
);
1161 -- We always consider the loop variable to be referenced,
1162 -- since the loop may be used just for counting purposes.
1164 Generate_Reference
(Id
, N
, ' ');
1166 -- Check for case of loop variable hiding a local
1167 -- variable (used later on to give a nice warning
1168 -- if the hidden variable is never assigned).
1171 H
: constant Entity_Id
:= Homonym
(Id
);
1174 and then Enclosing_Dynamic_Scope
(H
) =
1175 Enclosing_Dynamic_Scope
(Id
)
1176 and then Ekind
(H
) = E_Variable
1177 and then Is_Discrete_Type
(Etype
(H
))
1179 Set_Hiding_Loop_Variable
(H
, Id
);
1183 -- Now analyze the subtype definition
1191 -- The subtype indication may denote the completion
1192 -- of an incomplete type declaration.
1194 if Is_Entity_Name
(DS
)
1195 and then Present
(Entity
(DS
))
1196 and then Is_Type
(Entity
(DS
))
1197 and then Ekind
(Entity
(DS
)) = E_Incomplete_Type
1199 Set_Entity
(DS
, Get_Full_View
(Entity
(DS
)));
1200 Set_Etype
(DS
, Entity
(DS
));
1203 if not Is_Discrete_Type
(Etype
(DS
)) then
1204 Wrong_Type
(DS
, Any_Discrete
);
1205 Set_Etype
(DS
, Any_Type
);
1208 Check_Controlled_Array_Attribute
(DS
);
1209 Make_Index
(DS
, LP
);
1211 Set_Ekind
(Id
, E_Loop_Parameter
);
1212 Set_Etype
(Id
, Etype
(DS
));
1213 Set_Is_Known_Valid
(Id
, True);
1215 -- The loop is not a declarative part, so the only entity
1216 -- declared "within" must be frozen explicitly.
1219 Flist
: constant List_Id
:= Freeze_Entity
(Id
, Sloc
(N
));
1221 if Is_Non_Empty_List
(Flist
) then
1222 Insert_Actions
(N
, Flist
);
1226 -- Check for null or possibly null range and issue warning.
1227 -- We suppress such messages in generic templates and
1228 -- instances, because in practice they tend to be dubious
1231 if Nkind
(DS
) = N_Range
1232 and then Comes_From_Source
(N
)
1235 L
: constant Node_Id
:= Low_Bound
(DS
);
1236 H
: constant Node_Id
:= High_Bound
(DS
);
1246 Determine_Range
(L
, LOK
, Llo
, Lhi
);
1247 Determine_Range
(H
, HOK
, Hlo
, Hhi
);
1249 -- If range of loop is null, issue warning
1251 if (LOK
and HOK
) and then Llo
> Hhi
then
1253 -- Suppress the warning if inside a generic
1254 -- template or instance, since in practice
1255 -- they tend to be dubious in these cases since
1256 -- they can result from intended parametrization.
1258 if not Inside_A_Generic
1259 and then not In_Instance
1262 ("?loop range is null, loop will not execute",
1266 -- Since we know the range of the loop is null,
1267 -- set the appropriate flag to suppress any
1268 -- warnings that would otherwise be issued in
1269 -- the body of the loop that will not execute.
1270 -- We do this even in the generic case, since
1271 -- if it is dubious to warn on the null loop
1272 -- itself, it is certainly dubious to warn for
1273 -- conditions that occur inside it!
1275 Set_Is_Null_Loop
(Parent
(N
));
1277 -- The other case for a warning is a reverse loop
1278 -- where the upper bound is the integer literal
1279 -- zero or one, and the lower bound can be positive.
1281 -- For example, we have
1283 -- for J in reverse N .. 1 loop
1285 -- In practice, this is very likely to be a case
1286 -- of reversing the bounds incorrectly in the range.
1288 elsif Reverse_Present
(LP
)
1289 and then Nkind
(H
) = N_Integer_Literal
1290 and then (Intval
(H
) = Uint_0
1292 Intval
(H
) = Uint_1
)
1295 Error_Msg_N
("?loop range may be null", DS
);
1303 end Analyze_Iteration_Scheme
;
1309 -- Note: the semantic work required for analyzing labels (setting them as
1310 -- reachable) was done in a prepass through the statements in the block,
1311 -- so that forward gotos would be properly handled. See Analyze_Statements
1312 -- for further details. The only processing required here is to deal with
1313 -- optimizations that depend on an assumption of sequential control flow,
1314 -- since of course the occurrence of a label breaks this assumption.
1316 procedure Analyze_Label
(N
: Node_Id
) is
1317 pragma Warnings
(Off
, N
);
1319 Kill_Current_Values
;
1322 --------------------------
1323 -- Analyze_Label_Entity --
1324 --------------------------
1326 procedure Analyze_Label_Entity
(E
: Entity_Id
) is
1328 Set_Ekind
(E
, E_Label
);
1329 Set_Etype
(E
, Standard_Void_Type
);
1330 Set_Enclosing_Scope
(E
, Current_Scope
);
1331 Set_Reachable
(E
, True);
1332 end Analyze_Label_Entity
;
1334 ----------------------------
1335 -- Analyze_Loop_Statement --
1336 ----------------------------
1338 procedure Analyze_Loop_Statement
(N
: Node_Id
) is
1339 Id
: constant Node_Id
:= Identifier
(N
);
1343 if Present
(Id
) then
1345 -- Make name visible, e.g. for use in exit statements. Loop
1346 -- labels are always considered to be referenced.
1350 Generate_Reference
(Ent
, N
, ' ');
1351 Generate_Definition
(Ent
);
1353 -- If we found a label, mark its type. If not, ignore it, since it
1354 -- means we have a conflicting declaration, which would already have
1355 -- been diagnosed at declaration time. Set Label_Construct of the
1356 -- implicit label declaration, which is not created by the parser
1357 -- for generic units.
1359 if Ekind
(Ent
) = E_Label
then
1360 Set_Ekind
(Ent
, E_Loop
);
1362 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
1363 Set_Label_Construct
(Parent
(Ent
), N
);
1367 -- Case of no identifier present
1370 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Sloc
(N
), 'L');
1371 Set_Etype
(Ent
, Standard_Void_Type
);
1372 Set_Parent
(Ent
, N
);
1375 -- Kill current values on entry to loop, since statements in body
1376 -- of loop may have been executed before the loop is entered.
1377 -- Similarly we kill values after the loop, since we do not know
1378 -- that the body of the loop was executed.
1380 Kill_Current_Values
;
1382 Analyze_Iteration_Scheme
(Iteration_Scheme
(N
));
1383 Analyze_Statements
(Statements
(N
));
1384 Process_End_Label
(N
, 'e', Ent
);
1386 Kill_Current_Values
;
1387 end Analyze_Loop_Statement
;
1389 ----------------------------
1390 -- Analyze_Null_Statement --
1391 ----------------------------
1393 -- Note: the semantics of the null statement is implemented by a single
1394 -- null statement, too bad everything isn't as simple as this!
1396 procedure Analyze_Null_Statement
(N
: Node_Id
) is
1397 pragma Warnings
(Off
, N
);
1400 end Analyze_Null_Statement
;
1402 ------------------------
1403 -- Analyze_Statements --
1404 ------------------------
1406 procedure Analyze_Statements
(L
: List_Id
) is
1411 -- The labels declared in the statement list are reachable from
1412 -- statements in the list. We do this as a prepass so that any
1413 -- goto statement will be properly flagged if its target is not
1414 -- reachable. This is not required, but is nice behavior!
1417 while Present
(S
) loop
1418 if Nkind
(S
) = N_Label
then
1419 Analyze
(Identifier
(S
));
1420 Lab
:= Entity
(Identifier
(S
));
1422 -- If we found a label mark it as reachable.
1424 if Ekind
(Lab
) = E_Label
then
1425 Generate_Definition
(Lab
);
1426 Set_Reachable
(Lab
);
1428 if Nkind
(Parent
(Lab
)) = N_Implicit_Label_Declaration
then
1429 Set_Label_Construct
(Parent
(Lab
), S
);
1432 -- If we failed to find a label, it means the implicit declaration
1433 -- of the label was hidden. A for-loop parameter can do this to
1434 -- a label with the same name inside the loop, since the implicit
1435 -- label declaration is in the innermost enclosing body or block
1439 Error_Msg_Sloc
:= Sloc
(Lab
);
1441 ("implicit label declaration for & is hidden#",
1449 -- Perform semantic analysis on all statements
1451 Conditional_Statements_Begin
;
1454 while Present
(S
) loop
1459 Conditional_Statements_End
;
1461 -- Make labels unreachable. Visibility is not sufficient, because
1462 -- labels in one if-branch for example are not reachable from the
1463 -- other branch, even though their declarations are in the enclosing
1464 -- declarative part.
1467 while Present
(S
) loop
1468 if Nkind
(S
) = N_Label
then
1469 Set_Reachable
(Entity
(Identifier
(S
)), False);
1474 end Analyze_Statements
;
1476 --------------------------------------------
1477 -- Check_Possible_Current_Value_Condition --
1478 --------------------------------------------
1480 procedure Check_Possible_Current_Value_Condition
(Cnode
: Node_Id
) is
1484 -- Loop to deal with (ignore for now) any NOT operators present
1486 Cond
:= Condition
(Cnode
);
1487 while Nkind
(Cond
) = N_Op_Not
loop
1488 Cond
:= Right_Opnd
(Cond
);
1491 -- Check possible relational operator
1493 if Nkind
(Cond
) = N_Op_Eq
1495 Nkind
(Cond
) = N_Op_Ne
1497 Nkind
(Cond
) = N_Op_Ge
1499 Nkind
(Cond
) = N_Op_Le
1501 Nkind
(Cond
) = N_Op_Gt
1503 Nkind
(Cond
) = N_Op_Lt
1505 if Compile_Time_Known_Value
(Right_Opnd
(Cond
))
1506 and then Nkind
(Left_Opnd
(Cond
)) = N_Identifier
1509 Ent
: constant Entity_Id
:= Entity
(Left_Opnd
(Cond
));
1512 if Ekind
(Ent
) = E_Variable
1514 Ekind
(Ent
) = E_Constant
1518 Ekind
(Ent
) = E_Loop_Parameter
1520 -- Here we have a case where the Current_Value field
1521 -- may need to be set. We set it if it is not already
1522 -- set to a compile time expression value.
1524 -- Note that this represents a decision that one
1525 -- condition blots out another previous one. That's
1526 -- certainly right if they occur at the same level.
1527 -- If the second one is nested, then the decision is
1528 -- neither right nor wrong (it would be equally OK
1529 -- to leave the outer one in place, or take the new
1530 -- inner one. Really we should record both, but our
1531 -- data structures are not that elaborate.
1533 if Nkind
(Current_Value
(Ent
)) not in N_Subexpr
then
1534 Set_Current_Value
(Ent
, Cnode
);
1540 end Check_Possible_Current_Value_Condition
;
1542 ----------------------------
1543 -- Check_Unreachable_Code --
1544 ----------------------------
1546 procedure Check_Unreachable_Code
(N
: Node_Id
) is
1547 Error_Loc
: Source_Ptr
;
1551 if Is_List_Member
(N
)
1552 and then Comes_From_Source
(N
)
1558 Nxt
:= Original_Node
(Next
(N
));
1561 and then Comes_From_Source
(Nxt
)
1562 and then Is_Statement
(Nxt
)
1564 -- Special very annoying exception. If we have a return that
1565 -- follows a raise, then we allow it without a warning, since
1566 -- the Ada RM annoyingly requires a useless return here!
1568 if Nkind
(Original_Node
(N
)) /= N_Raise_Statement
1569 or else Nkind
(Nxt
) /= N_Return_Statement
1571 -- The rather strange shenanigans with the warning message
1572 -- here reflects the fact that Kill_Dead_Code is very good
1573 -- at removing warnings in deleted code, and this is one
1574 -- warning we would prefer NOT to have removed :-)
1576 Error_Loc
:= Sloc
(Nxt
);
1578 -- If we have unreachable code, analyze and remove the
1579 -- unreachable code, since it is useless and we don't
1580 -- want to generate junk warnings.
1582 -- We skip this step if we are not in code generation mode.
1583 -- This is the one case where we remove dead code in the
1584 -- semantics as opposed to the expander, and we do not want
1585 -- to remove code if we are not in code generation mode,
1586 -- since this messes up the ASIS trees.
1588 -- Note that one might react by moving the whole circuit to
1589 -- exp_ch5, but then we lose the warning in -gnatc mode.
1591 if Operating_Mode
= Generate_Code
then
1595 -- Quit deleting when we have nothing more to delete
1596 -- or if we hit a label (since someone could transfer
1597 -- control to a label, so we should not delete it).
1599 exit when No
(Nxt
) or else Nkind
(Nxt
) = N_Label
;
1601 -- Statement/declaration is to be deleted
1605 Kill_Dead_Code
(Nxt
);
1609 -- Now issue the warning
1611 Error_Msg
("?unreachable code", Error_Loc
);
1614 -- If the unconditional transfer of control instruction is
1615 -- the last statement of a sequence, then see if our parent
1616 -- is an IF statement, and if so adjust the unblocked exit
1617 -- count of the if statement to reflect the fact that this
1618 -- branch of the if is indeed blocked by a transfer of control.
1623 if Nkind
(P
) = N_If_Statement
then
1626 elsif Nkind
(P
) = N_Elsif_Part
then
1628 pragma Assert
(Nkind
(P
) = N_If_Statement
);
1630 elsif Nkind
(P
) = N_Case_Statement_Alternative
then
1632 pragma Assert
(Nkind
(P
) = N_Case_Statement
);
1638 Unblocked_Exit_Count
:= Unblocked_Exit_Count
- 1;
1642 end Check_Unreachable_Code
;