1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 Aspects
; use Aspects
;
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_Ch6
; use Exp_Ch6
;
33 with Exp_Util
; use Exp_Util
;
34 with Freeze
; use Freeze
;
36 with Lib
.Xref
; use Lib
.Xref
;
37 with Namet
; use Namet
;
38 with Nlists
; use Nlists
;
39 with Nmake
; use Nmake
;
41 with Restrict
; use Restrict
;
42 with Rident
; use Rident
;
43 with Rtsfind
; use Rtsfind
;
45 with Sem_Aux
; use Sem_Aux
;
46 with Sem_Case
; use Sem_Case
;
47 with Sem_Ch3
; use Sem_Ch3
;
48 with Sem_Ch6
; use Sem_Ch6
;
49 with Sem_Ch8
; use Sem_Ch8
;
50 with Sem_Dim
; use Sem_Dim
;
51 with Sem_Disp
; use Sem_Disp
;
52 with Sem_Elab
; use Sem_Elab
;
53 with Sem_Eval
; use Sem_Eval
;
54 with Sem_Res
; use Sem_Res
;
55 with Sem_Type
; use Sem_Type
;
56 with Sem_Util
; use Sem_Util
;
57 with Sem_Warn
; use Sem_Warn
;
58 with Snames
; use Snames
;
59 with Stand
; use Stand
;
60 with Sinfo
; use Sinfo
;
61 with Targparm
; use Targparm
;
62 with Tbuild
; use Tbuild
;
63 with Uintp
; use Uintp
;
65 package body Sem_Ch5
is
67 Unblocked_Exit_Count
: Nat
:= 0;
68 -- This variable is used when processing if statements, case statements,
69 -- and block statements. It counts the number of exit points that are not
70 -- blocked by unconditional transfer instructions: for IF and CASE, these
71 -- are the branches of the conditional; for a block, they are the statement
72 -- sequence of the block, and the statement sequences of any exception
73 -- handlers that are part of the block. When processing is complete, if
74 -- this count is zero, it means that control cannot fall through the IF,
75 -- CASE or block statement. This is used for the generation of warning
76 -- messages. This variable is recursively saved on entry to processing the
77 -- construct, and restored on exit.
79 procedure Preanalyze_Range
(R_Copy
: Node_Id
);
80 -- Determine expected type of range or domain of iteration of Ada 2012
81 -- loop by analyzing separate copy. Do the analysis and resolution of the
82 -- copy of the bound(s) with expansion disabled, to prevent the generation
83 -- of finalization actions. This prevents memory leaks when the bounds
84 -- contain calls to functions returning controlled arrays or when the
85 -- domain of iteration is a container.
87 ------------------------
88 -- Analyze_Assignment --
89 ------------------------
91 procedure Analyze_Assignment
(N
: Node_Id
) is
92 Lhs
: constant Node_Id
:= Name
(N
);
93 Rhs
: constant Node_Id
:= Expression
(N
);
98 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
);
99 -- N is the node for the left hand side of an assignment, and it is not
100 -- a variable. This routine issues an appropriate diagnostic.
103 -- This is called to kill current value settings of a simple variable
104 -- on the left hand side. We call it if we find any error in analyzing
105 -- the assignment, and at the end of processing before setting any new
106 -- current values in place.
108 procedure Set_Assignment_Type
110 Opnd_Type
: in out Entity_Id
);
111 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
112 -- nominal subtype. This procedure is used to deal with cases where the
113 -- nominal subtype must be replaced by the actual subtype.
115 -------------------------------
116 -- Diagnose_Non_Variable_Lhs --
117 -------------------------------
119 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
) is
121 -- Not worth posting another error if left hand side already flagged
122 -- as being illegal in some respect.
124 if Error_Posted
(N
) then
127 -- Some special bad cases of entity names
129 elsif Is_Entity_Name
(N
) then
131 Ent
: constant Entity_Id
:= Entity
(N
);
134 if Ekind
(Ent
) = E_In_Parameter
then
136 ("assignment to IN mode parameter not allowed", N
);
138 -- Renamings of protected private components are turned into
139 -- constants when compiling a protected function. In the case
140 -- of single protected types, the private component appears
143 elsif (Is_Prival
(Ent
)
145 (Ekind
(Current_Scope
) = E_Function
146 or else Ekind
(Enclosing_Dynamic_Scope
147 (Current_Scope
)) = E_Function
))
149 (Ekind
(Ent
) = E_Component
150 and then Is_Protected_Type
(Scope
(Ent
)))
153 ("protected function cannot modify protected object", N
);
155 elsif Ekind
(Ent
) = E_Loop_Parameter
then
157 ("assignment to loop parameter not allowed", N
);
161 ("left hand side of assignment must be a variable", N
);
165 -- For indexed components or selected components, test prefix
167 elsif Nkind
(N
) = N_Indexed_Component
then
168 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
170 -- Another special case for assignment to discriminant
172 elsif Nkind
(N
) = N_Selected_Component
then
173 if Present
(Entity
(Selector_Name
(N
)))
174 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
177 ("assignment to discriminant not allowed", N
);
179 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
183 -- If we fall through, we have no special message to issue!
185 Error_Msg_N
("left hand side of assignment must be a variable", N
);
187 end Diagnose_Non_Variable_Lhs
;
193 procedure Kill_Lhs
is
195 if Is_Entity_Name
(Lhs
) then
197 Ent
: constant Entity_Id
:= Entity
(Lhs
);
199 if Present
(Ent
) then
200 Kill_Current_Values
(Ent
);
206 -------------------------
207 -- Set_Assignment_Type --
208 -------------------------
210 procedure Set_Assignment_Type
212 Opnd_Type
: in out Entity_Id
)
215 Require_Entity
(Opnd
);
217 -- If the assignment operand is an in-out or out parameter, then we
218 -- get the actual subtype (needed for the unconstrained case). If the
219 -- operand is the actual in an entry declaration, then within the
220 -- accept statement it is replaced with a local renaming, which may
221 -- also have an actual subtype.
223 if Is_Entity_Name
(Opnd
)
224 and then (Ekind
(Entity
(Opnd
)) = E_Out_Parameter
225 or else Ekind
(Entity
(Opnd
)) =
227 or else Ekind
(Entity
(Opnd
)) =
228 E_Generic_In_Out_Parameter
230 (Ekind
(Entity
(Opnd
)) = E_Variable
231 and then Nkind
(Parent
(Entity
(Opnd
))) =
232 N_Object_Renaming_Declaration
233 and then Nkind
(Parent
(Parent
(Entity
(Opnd
)))) =
236 Opnd_Type
:= Get_Actual_Subtype
(Opnd
);
238 -- If assignment operand is a component reference, then we get the
239 -- actual subtype of the component for the unconstrained case.
241 elsif Nkind_In
(Opnd
, N_Selected_Component
, N_Explicit_Dereference
)
242 and then not Is_Unchecked_Union
(Opnd_Type
)
244 Decl
:= Build_Actual_Subtype_Of_Component
(Opnd_Type
, Opnd
);
246 if Present
(Decl
) then
247 Insert_Action
(N
, Decl
);
248 Mark_Rewrite_Insertion
(Decl
);
250 Opnd_Type
:= Defining_Identifier
(Decl
);
251 Set_Etype
(Opnd
, Opnd_Type
);
252 Freeze_Itype
(Opnd_Type
, N
);
254 elsif Is_Constrained
(Etype
(Opnd
)) then
255 Opnd_Type
:= Etype
(Opnd
);
258 -- For slice, use the constrained subtype created for the slice
260 elsif Nkind
(Opnd
) = N_Slice
then
261 Opnd_Type
:= Etype
(Opnd
);
263 end Set_Assignment_Type
;
265 -- Start of processing for Analyze_Assignment
268 Mark_Coextensions
(N
, Rhs
);
273 -- Ensure that we never do an assignment on a variable marked as
274 -- as Safe_To_Reevaluate.
276 pragma Assert
(not Is_Entity_Name
(Lhs
)
277 or else Ekind
(Entity
(Lhs
)) /= E_Variable
278 or else not Is_Safe_To_Reevaluate
(Entity
(Lhs
)));
280 -- Start type analysis for assignment
284 -- In the most general case, both Lhs and Rhs can be overloaded, and we
285 -- must compute the intersection of the possible types on each side.
287 if Is_Overloaded
(Lhs
) then
294 Get_First_Interp
(Lhs
, I
, It
);
296 while Present
(It
.Typ
) loop
297 if Has_Compatible_Type
(Rhs
, It
.Typ
) then
298 if T1
/= Any_Type
then
300 -- An explicit dereference is overloaded if the prefix
301 -- is. Try to remove the ambiguity on the prefix, the
302 -- error will be posted there if the ambiguity is real.
304 if Nkind
(Lhs
) = N_Explicit_Dereference
then
307 PI1
: Interp_Index
:= 0;
313 Get_First_Interp
(Prefix
(Lhs
), PI
, PIt
);
315 while Present
(PIt
.Typ
) loop
316 if Is_Access_Type
(PIt
.Typ
)
317 and then Has_Compatible_Type
318 (Rhs
, Designated_Type
(PIt
.Typ
))
322 Disambiguate
(Prefix
(Lhs
),
325 if PIt
= No_Interp
then
327 ("ambiguous left-hand side"
328 & " in assignment", Lhs
);
331 Resolve
(Prefix
(Lhs
), PIt
.Typ
);
341 Get_Next_Interp
(PI
, PIt
);
347 ("ambiguous left-hand side in assignment", Lhs
);
355 Get_Next_Interp
(I
, It
);
359 if T1
= Any_Type
then
361 ("no valid types for left-hand side for assignment", Lhs
);
367 -- The resulting assignment type is T1, so now we will resolve the left
368 -- hand side of the assignment using this determined type.
372 -- Cases where Lhs is not a variable
374 if not Is_Variable
(Lhs
) then
376 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
384 if Ada_Version
>= Ada_2005
then
386 -- Handle chains of renamings
389 while Nkind
(Ent
) in N_Has_Entity
390 and then Present
(Entity
(Ent
))
391 and then Present
(Renamed_Object
(Entity
(Ent
)))
393 Ent
:= Renamed_Object
(Entity
(Ent
));
396 if (Nkind
(Ent
) = N_Attribute_Reference
397 and then Attribute_Name
(Ent
) = Name_Priority
)
399 -- Renamings of the attribute Priority applied to protected
400 -- objects have been previously expanded into calls to the
401 -- Get_Ceiling run-time subprogram.
404 (Nkind
(Ent
) = N_Function_Call
405 and then (Entity
(Name
(Ent
)) = RTE
(RE_Get_Ceiling
)
407 Entity
(Name
(Ent
)) = RTE
(RO_PE_Get_Ceiling
)))
409 -- The enclosing subprogram cannot be a protected function
412 while not (Is_Subprogram
(S
)
413 and then Convention
(S
) = Convention_Protected
)
414 and then S
/= Standard_Standard
419 if Ekind
(S
) = E_Function
420 and then Convention
(S
) = Convention_Protected
423 ("protected function cannot modify protected object",
427 -- Changes of the ceiling priority of the protected object
428 -- are only effective if the Ceiling_Locking policy is in
429 -- effect (AARM D.5.2 (5/2)).
431 if Locking_Policy
/= 'C' then
432 Error_Msg_N
("assignment to the attribute PRIORITY has " &
434 Error_Msg_N
("\since no Locking_Policy has been " &
443 Diagnose_Non_Variable_Lhs
(Lhs
);
446 -- Error of assigning to limited type. We do however allow this in
447 -- certain cases where the front end generates the assignments.
449 elsif Is_Limited_Type
(T1
)
450 and then not Assignment_OK
(Lhs
)
451 and then not Assignment_OK
(Original_Node
(Lhs
))
452 and then not Is_Value_Type
(T1
)
454 -- CPP constructors can only be called in declarations
456 if Is_CPP_Constructor_Call
(Rhs
) then
457 Error_Msg_N
("invalid use of 'C'P'P constructor", Rhs
);
460 ("left hand of assignment must not be limited type", Lhs
);
461 Explain_Limited_Type
(T1
, Lhs
);
465 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
466 -- abstract. This is only checked when the assignment Comes_From_Source,
467 -- because in some cases the expander generates such assignments (such
468 -- in the _assign operation for an abstract type).
470 elsif Is_Abstract_Type
(T1
) and then Comes_From_Source
(N
) then
472 ("target of assignment operation must not be abstract", Lhs
);
475 -- Resolution may have updated the subtype, in case the left-hand side
476 -- is a private protected component. Use the correct subtype to avoid
477 -- scoping issues in the back-end.
481 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
482 -- type. For example:
486 -- type Acc is access P.T;
489 -- with Pkg; use Acc;
490 -- procedure Example is
493 -- A.all := B.all; -- ERROR
496 if Nkind
(Lhs
) = N_Explicit_Dereference
497 and then Ekind
(T1
) = E_Incomplete_Type
499 Error_Msg_N
("invalid use of incomplete type", Lhs
);
504 -- Now we can complete the resolution of the right hand side
506 Set_Assignment_Type
(Lhs
, T1
);
509 -- This is the point at which we check for an unset reference
511 Check_Unset_Reference
(Rhs
);
512 Check_Unprotected_Access
(Lhs
, Rhs
);
514 -- Remaining steps are skipped if Rhs was syntactically in error
523 if not Covers
(T1
, T2
) then
524 Wrong_Type
(Rhs
, Etype
(Lhs
));
529 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
530 -- types, use the non-limited view if available
532 if Nkind
(Rhs
) = N_Explicit_Dereference
533 and then Ekind
(T2
) = E_Incomplete_Type
534 and then Is_Tagged_Type
(T2
)
535 and then Present
(Non_Limited_View
(T2
))
537 T2
:= Non_Limited_View
(T2
);
540 Set_Assignment_Type
(Rhs
, T2
);
542 if Total_Errors_Detected
/= 0 then
552 if T1
= Any_Type
or else T2
= Any_Type
then
557 -- If the rhs is class-wide or dynamically tagged, then require the lhs
558 -- to be class-wide. The case where the rhs is a dynamically tagged call
559 -- to a dispatching operation with a controlling access result is
560 -- excluded from this check, since the target has an access type (and
561 -- no tag propagation occurs in that case).
563 if (Is_Class_Wide_Type
(T2
)
564 or else (Is_Dynamically_Tagged
(Rhs
)
565 and then not Is_Access_Type
(T1
)))
566 and then not Is_Class_Wide_Type
(T1
)
568 Error_Msg_N
("dynamically tagged expression not allowed!", Rhs
);
570 elsif Is_Class_Wide_Type
(T1
)
571 and then not Is_Class_Wide_Type
(T2
)
572 and then not Is_Tag_Indeterminate
(Rhs
)
573 and then not Is_Dynamically_Tagged
(Rhs
)
575 Error_Msg_N
("dynamically tagged expression required!", Rhs
);
578 -- Propagate the tag from a class-wide target to the rhs when the rhs
579 -- is a tag-indeterminate call.
581 if Is_Tag_Indeterminate
(Rhs
) then
582 if Is_Class_Wide_Type
(T1
) then
583 Propagate_Tag
(Lhs
, Rhs
);
585 elsif Nkind
(Rhs
) = N_Function_Call
586 and then Is_Entity_Name
(Name
(Rhs
))
587 and then Is_Abstract_Subprogram
(Entity
(Name
(Rhs
)))
590 ("call to abstract function must be dispatching", Name
(Rhs
));
592 elsif Nkind
(Rhs
) = N_Qualified_Expression
593 and then Nkind
(Expression
(Rhs
)) = N_Function_Call
594 and then Is_Entity_Name
(Name
(Expression
(Rhs
)))
596 Is_Abstract_Subprogram
(Entity
(Name
(Expression
(Rhs
))))
599 ("call to abstract function must be dispatching",
600 Name
(Expression
(Rhs
)));
604 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
605 -- apply an implicit conversion of the rhs to that type to force
606 -- appropriate static and run-time accessibility checks. This applies
607 -- as well to anonymous access-to-subprogram types that are component
608 -- subtypes or formal parameters.
610 if Ada_Version
>= Ada_2005
611 and then Is_Access_Type
(T1
)
613 if Is_Local_Anonymous_Access
(T1
)
614 or else Ekind
(T2
) = E_Anonymous_Access_Subprogram_Type
616 -- Handle assignment to an Ada 2012 stand-alone object
617 -- of an anonymous access type.
619 or else (Ekind
(T1
) = E_Anonymous_Access_Type
620 and then Nkind
(Associated_Node_For_Itype
(T1
)) =
621 N_Object_Declaration
)
624 Rewrite
(Rhs
, Convert_To
(T1
, Relocate_Node
(Rhs
)));
625 Analyze_And_Resolve
(Rhs
, T1
);
629 -- Ada 2005 (AI-231): Assignment to not null variable
631 if Ada_Version
>= Ada_2005
632 and then Can_Never_Be_Null
(T1
)
633 and then not Assignment_OK
(Lhs
)
635 -- Case where we know the right hand side is null
637 if Known_Null
(Rhs
) then
638 Apply_Compile_Time_Constraint_Error
641 "(Ada 2005) null not allowed in null-excluding objects??",
642 Reason
=> CE_Null_Not_Allowed
);
644 -- We still mark this as a possible modification, that's necessary
645 -- to reset Is_True_Constant, and desirable for xref purposes.
647 Note_Possible_Modification
(Lhs
, Sure
=> True);
650 -- If we know the right hand side is non-null, then we convert to the
651 -- target type, since we don't need a run time check in that case.
653 elsif not Can_Never_Be_Null
(T2
) then
654 Rewrite
(Rhs
, Convert_To
(T1
, Relocate_Node
(Rhs
)));
655 Analyze_And_Resolve
(Rhs
, T1
);
659 if Is_Scalar_Type
(T1
) then
660 Apply_Scalar_Range_Check
(Rhs
, Etype
(Lhs
));
662 -- For array types, verify that lengths match. If the right hand side
663 -- is a function call that has been inlined, the assignment has been
664 -- rewritten as a block, and the constraint check will be applied to the
665 -- assignment within the block.
667 elsif Is_Array_Type
(T1
)
669 (Nkind
(Rhs
) /= N_Type_Conversion
670 or else Is_Constrained
(Etype
(Rhs
)))
672 (Nkind
(Rhs
) /= N_Function_Call
673 or else Nkind
(N
) /= N_Block_Statement
)
675 -- Assignment verifies that the length of the Lsh and Rhs are equal,
676 -- but of course the indexes do not have to match. If the right-hand
677 -- side is a type conversion to an unconstrained type, a length check
678 -- is performed on the expression itself during expansion. In rare
679 -- cases, the redundant length check is computed on an index type
680 -- with a different representation, triggering incorrect code in the
683 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
686 -- Discriminant checks are applied in the course of expansion
691 -- Note: modifications of the Lhs may only be recorded after
692 -- checks have been applied.
694 Note_Possible_Modification
(Lhs
, Sure
=> True);
696 -- ??? a real accessibility check is needed when ???
698 -- Post warning for redundant assignment or variable to itself
700 if Warn_On_Redundant_Constructs
702 -- We only warn for source constructs
704 and then Comes_From_Source
(N
)
706 -- Where the object is the same on both sides
708 and then Same_Object
(Lhs
, Original_Node
(Rhs
))
710 -- But exclude the case where the right side was an operation that
711 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
712 -- don't want to warn in such a case, since it is reasonable to write
713 -- such expressions especially when K is defined symbolically in some
716 and then Nkind
(Original_Node
(Rhs
)) not in N_Op
718 if Nkind
(Lhs
) in N_Has_Entity
then
719 Error_Msg_NE
-- CODEFIX
720 ("?r?useless assignment of & to itself!", N
, Entity
(Lhs
));
722 Error_Msg_N
-- CODEFIX
723 ("?r?useless assignment of object to itself!", N
);
727 -- Check for non-allowed composite assignment
729 if not Support_Composite_Assign_On_Target
730 and then (Is_Array_Type
(T1
) or else Is_Record_Type
(T1
))
731 and then (not Has_Size_Clause
(T1
) or else Esize
(T1
) > 64)
733 Error_Msg_CRT
("composite assignment", N
);
736 -- Check elaboration warning for left side if not in elab code
738 if not In_Subprogram_Or_Concurrent_Unit
then
739 Check_Elab_Assign
(Lhs
);
742 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
743 -- assignment is a source assignment in the extended main source unit.
744 -- We are not interested in any reference information outside this
745 -- context, or in compiler generated assignment statements.
747 if Comes_From_Source
(N
)
748 and then In_Extended_Main_Source_Unit
(Lhs
)
750 Set_Referenced_Modified
(Lhs
, Out_Param
=> False);
753 -- Final step. If left side is an entity, then we may be able to reset
754 -- the current tracked values to new safe values. We only have something
755 -- to do if the left side is an entity name, and expansion has not
756 -- modified the node into something other than an assignment, and of
757 -- course we only capture values if it is safe to do so.
759 if Is_Entity_Name
(Lhs
)
760 and then Nkind
(N
) = N_Assignment_Statement
763 Ent
: constant Entity_Id
:= Entity
(Lhs
);
766 if Safe_To_Capture_Value
(N
, Ent
) then
768 -- If simple variable on left side, warn if this assignment
769 -- blots out another one (rendering it useless). We only do
770 -- this for source assignments, otherwise we can generate bogus
771 -- warnings when an assignment is rewritten as another
772 -- assignment, and gets tied up with itself.
774 if Warn_On_Modified_Unread
775 and then Is_Assignable
(Ent
)
776 and then Comes_From_Source
(N
)
777 and then In_Extended_Main_Source_Unit
(Ent
)
779 Warn_On_Useless_Assignment
(Ent
, N
);
782 -- If we are assigning an access type and the left side is an
783 -- entity, then make sure that the Is_Known_[Non_]Null flags
784 -- properly reflect the state of the entity after assignment.
786 if Is_Access_Type
(T1
) then
787 if Known_Non_Null
(Rhs
) then
788 Set_Is_Known_Non_Null
(Ent
, True);
790 elsif Known_Null
(Rhs
)
791 and then not Can_Never_Be_Null
(Ent
)
793 Set_Is_Known_Null
(Ent
, True);
796 Set_Is_Known_Null
(Ent
, False);
798 if not Can_Never_Be_Null
(Ent
) then
799 Set_Is_Known_Non_Null
(Ent
, False);
803 -- For discrete types, we may be able to set the current value
804 -- if the value is known at compile time.
806 elsif Is_Discrete_Type
(T1
)
807 and then Compile_Time_Known_Value
(Rhs
)
809 Set_Current_Value
(Ent
, Rhs
);
811 Set_Current_Value
(Ent
, Empty
);
814 -- If not safe to capture values, kill them
822 -- If assigning to an object in whole or in part, note location of
823 -- assignment in case no one references value. We only do this for
824 -- source assignments, otherwise we can generate bogus warnings when an
825 -- assignment is rewritten as another assignment, and gets tied up with
829 Ent
: constant Entity_Id
:= Get_Enclosing_Object
(Lhs
);
832 and then Safe_To_Capture_Value
(N
, Ent
)
833 and then Nkind
(N
) = N_Assignment_Statement
834 and then Warn_On_Modified_Unread
835 and then Is_Assignable
(Ent
)
836 and then Comes_From_Source
(N
)
837 and then In_Extended_Main_Source_Unit
(Ent
)
839 Set_Last_Assignment
(Ent
, Lhs
);
843 Analyze_Dimension
(N
);
844 end Analyze_Assignment
;
846 -----------------------------
847 -- Analyze_Block_Statement --
848 -----------------------------
850 procedure Analyze_Block_Statement
(N
: Node_Id
) is
851 procedure Install_Return_Entities
(Scop
: Entity_Id
);
852 -- Install all entities of return statement scope Scop in the visibility
853 -- chain except for the return object since its entity is reused in a
856 -----------------------------
857 -- Install_Return_Entities --
858 -----------------------------
860 procedure Install_Return_Entities
(Scop
: Entity_Id
) is
864 Id
:= First_Entity
(Scop
);
865 while Present
(Id
) loop
867 -- Do not install the return object
869 if not Ekind_In
(Id
, E_Constant
, E_Variable
)
870 or else not Is_Return_Object
(Id
)
877 end Install_Return_Entities
;
879 -- Local constants and variables
881 Decls
: constant List_Id
:= Declarations
(N
);
882 Id
: constant Node_Id
:= Identifier
(N
);
883 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
885 Is_BIP_Return_Statement
: Boolean;
887 -- Start of processing for Analyze_Block_Statement
890 -- In SPARK mode, we reject block statements. Note that the case of
891 -- block statements generated by the expander is fine.
893 if Nkind
(Original_Node
(N
)) = N_Block_Statement
then
894 Check_SPARK_Restriction
("block statement is not allowed", N
);
897 -- If no handled statement sequence is present, things are really messed
898 -- up, and we just return immediately (defence against previous errors).
901 Check_Error_Detected
;
905 -- Detect whether the block is actually a rewritten return statement of
906 -- a build-in-place function.
908 Is_BIP_Return_Statement
:=
910 and then Present
(Entity
(Id
))
911 and then Ekind
(Entity
(Id
)) = E_Return_Statement
912 and then Is_Build_In_Place_Function
913 (Return_Applies_To
(Entity
(Id
)));
915 -- Normal processing with HSS present
918 EH
: constant List_Id
:= Exception_Handlers
(HSS
);
919 Ent
: Entity_Id
:= Empty
;
922 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
923 -- Recursively save value of this global, will be restored on exit
926 -- Initialize unblocked exit count for statements of begin block
927 -- plus one for each exception handler that is present.
929 Unblocked_Exit_Count
:= 1;
932 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ List_Length
(EH
);
935 -- If a label is present analyze it and mark it as referenced
941 -- An error defense. If we have an identifier, but no entity, then
942 -- something is wrong. If previous errors, then just remove the
943 -- identifier and continue, otherwise raise an exception.
946 Check_Error_Detected
;
947 Set_Identifier
(N
, Empty
);
950 Set_Ekind
(Ent
, E_Block
);
951 Generate_Reference
(Ent
, N
, ' ');
952 Generate_Definition
(Ent
);
954 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
955 Set_Label_Construct
(Parent
(Ent
), N
);
960 -- If no entity set, create a label entity
963 Ent
:= New_Internal_Entity
(E_Block
, Current_Scope
, Sloc
(N
), 'B');
964 Set_Identifier
(N
, New_Occurrence_Of
(Ent
, Sloc
(N
)));
968 Set_Etype
(Ent
, Standard_Void_Type
);
969 Set_Block_Node
(Ent
, Identifier
(N
));
972 -- The block served as an extended return statement. Ensure that any
973 -- entities created during the analysis and expansion of the return
974 -- object declaration are once again visible.
976 if Is_BIP_Return_Statement
then
977 Install_Return_Entities
(Ent
);
980 if Present
(Decls
) then
981 Analyze_Declarations
(Decls
);
983 Inspect_Deferred_Constant_Completion
(Decls
);
987 Process_End_Label
(HSS
, 'e', Ent
);
989 -- If exception handlers are present, then we indicate that enclosing
990 -- scopes contain a block with handlers. We only need to mark non-
996 Set_Has_Nested_Block_With_Handler
(S
);
997 exit when Is_Overloadable
(S
)
998 or else Ekind
(S
) = E_Package
999 or else Is_Generic_Unit
(S
);
1004 Check_References
(Ent
);
1005 Warn_On_Useless_Assignments
(Ent
);
1008 if Unblocked_Exit_Count
= 0 then
1009 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1010 Check_Unreachable_Code
(N
);
1012 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1015 end Analyze_Block_Statement
;
1017 ----------------------------
1018 -- Analyze_Case_Statement --
1019 ----------------------------
1021 procedure Analyze_Case_Statement
(N
: Node_Id
) is
1023 Exp_Type
: Entity_Id
;
1024 Exp_Btype
: Entity_Id
;
1026 Dont_Care
: Boolean;
1027 Others_Present
: Boolean;
1029 pragma Warnings
(Off
, Last_Choice
);
1030 pragma Warnings
(Off
, Dont_Care
);
1031 -- Don't care about assigned values
1033 Statements_Analyzed
: Boolean := False;
1034 -- Set True if at least some statement sequences get analyzed. If False
1035 -- on exit, means we had a serious error that prevented full analysis of
1036 -- the case statement, and as a result it is not a good idea to output
1037 -- warning messages about unreachable code.
1039 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1040 -- Recursively save value of this global, will be restored on exit
1042 procedure Non_Static_Choice_Error
(Choice
: Node_Id
);
1043 -- Error routine invoked by the generic instantiation below when the
1044 -- case statement has a non static choice.
1046 procedure Process_Statements
(Alternative
: Node_Id
);
1047 -- Analyzes all the statements associated with a case alternative.
1048 -- Needed by the generic instantiation below.
1050 package Case_Choices_Processing
is new
1051 Generic_Choices_Processing
1052 (Get_Alternatives
=> Alternatives
,
1053 Get_Choices
=> Discrete_Choices
,
1054 Process_Empty_Choice
=> No_OP
,
1055 Process_Non_Static_Choice
=> Non_Static_Choice_Error
,
1056 Process_Associated_Node
=> Process_Statements
);
1057 use Case_Choices_Processing
;
1058 -- Instantiation of the generic choice processing package
1060 -----------------------------
1061 -- Non_Static_Choice_Error --
1062 -----------------------------
1064 procedure Non_Static_Choice_Error
(Choice
: Node_Id
) is
1066 Flag_Non_Static_Expr
1067 ("choice given in case statement is not static!", Choice
);
1068 end Non_Static_Choice_Error
;
1070 ------------------------
1071 -- Process_Statements --
1072 ------------------------
1074 procedure Process_Statements
(Alternative
: Node_Id
) is
1075 Choices
: constant List_Id
:= Discrete_Choices
(Alternative
);
1079 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
1080 Statements_Analyzed
:= True;
1082 -- An interesting optimization. If the case statement expression
1083 -- is a simple entity, then we can set the current value within an
1084 -- alternative if the alternative has one possible value.
1088 -- when 2 | 3 => beta
1089 -- when others => gamma
1091 -- Here we know that N is initially 1 within alpha, but for beta and
1092 -- gamma, we do not know anything more about the initial value.
1094 if Is_Entity_Name
(Exp
) then
1095 Ent
:= Entity
(Exp
);
1097 if Ekind_In
(Ent
, E_Variable
,
1101 if List_Length
(Choices
) = 1
1102 and then Nkind
(First
(Choices
)) in N_Subexpr
1103 and then Compile_Time_Known_Value
(First
(Choices
))
1105 Set_Current_Value
(Entity
(Exp
), First
(Choices
));
1108 Analyze_Statements
(Statements
(Alternative
));
1110 -- After analyzing the case, set the current value to empty
1111 -- since we won't know what it is for the next alternative
1112 -- (unless reset by this same circuit), or after the case.
1114 Set_Current_Value
(Entity
(Exp
), Empty
);
1119 -- Case where expression is not an entity name of a variable
1121 Analyze_Statements
(Statements
(Alternative
));
1122 end Process_Statements
;
1124 -- Start of processing for Analyze_Case_Statement
1127 Unblocked_Exit_Count
:= 0;
1128 Exp
:= Expression
(N
);
1131 -- The expression must be of any discrete type. In rare cases, the
1132 -- expander constructs a case statement whose expression has a private
1133 -- type whose full view is discrete. This can happen when generating
1134 -- a stream operation for a variant type after the type is frozen,
1135 -- when the partial of view of the type of the discriminant is private.
1136 -- In that case, use the full view to analyze case alternatives.
1138 if not Is_Overloaded
(Exp
)
1139 and then not Comes_From_Source
(N
)
1140 and then Is_Private_Type
(Etype
(Exp
))
1141 and then Present
(Full_View
(Etype
(Exp
)))
1142 and then Is_Discrete_Type
(Full_View
(Etype
(Exp
)))
1144 Resolve
(Exp
, Etype
(Exp
));
1145 Exp_Type
:= Full_View
(Etype
(Exp
));
1148 Analyze_And_Resolve
(Exp
, Any_Discrete
);
1149 Exp_Type
:= Etype
(Exp
);
1152 Check_Unset_Reference
(Exp
);
1153 Exp_Btype
:= Base_Type
(Exp_Type
);
1155 -- The expression must be of a discrete type which must be determinable
1156 -- independently of the context in which the expression occurs, but
1157 -- using the fact that the expression must be of a discrete type.
1158 -- Moreover, the type this expression must not be a character literal
1159 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1161 -- If error already reported by Resolve, nothing more to do
1163 if Exp_Btype
= Any_Discrete
1164 or else Exp_Btype
= Any_Type
1168 elsif Exp_Btype
= Any_Character
then
1170 ("character literal as case expression is ambiguous", Exp
);
1173 elsif Ada_Version
= Ada_83
1174 and then (Is_Generic_Type
(Exp_Btype
)
1175 or else Is_Generic_Type
(Root_Type
(Exp_Btype
)))
1178 ("(Ada 83) case expression cannot be of a generic type", Exp
);
1182 -- If the case expression is a formal object of mode in out, then treat
1183 -- it as having a nonstatic subtype by forcing use of the base type
1184 -- (which has to get passed to Check_Case_Choices below). Also use base
1185 -- type when the case expression is parenthesized.
1187 if Paren_Count
(Exp
) > 0
1188 or else (Is_Entity_Name
(Exp
)
1189 and then Ekind
(Entity
(Exp
)) = E_Generic_In_Out_Parameter
)
1191 Exp_Type
:= Exp_Btype
;
1194 -- Call instantiated Analyze_Choices which does the rest of the work
1196 Analyze_Choices
(N
, Exp_Type
, Dont_Care
, Others_Present
);
1198 -- A case statement with a single OTHERS alternative is not allowed
1202 and then List_Length
(Alternatives
(N
)) = 1
1204 Check_SPARK_Restriction
1205 ("OTHERS as unique case alternative is not allowed", N
);
1208 if Exp_Type
= Universal_Integer
and then not Others_Present
then
1209 Error_Msg_N
("case on universal integer requires OTHERS choice", Exp
);
1212 -- If all our exits were blocked by unconditional transfers of control,
1213 -- then the entire CASE statement acts as an unconditional transfer of
1214 -- control, so treat it like one, and check unreachable code. Skip this
1215 -- test if we had serious errors preventing any statement analysis.
1217 if Unblocked_Exit_Count
= 0 and then Statements_Analyzed
then
1218 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1219 Check_Unreachable_Code
(N
);
1221 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1224 if not Expander_Active
1225 and then Compile_Time_Known_Value
(Expression
(N
))
1226 and then Serious_Errors_Detected
= 0
1229 Chosen
: constant Node_Id
:= Find_Static_Alternative
(N
);
1233 Alt
:= First
(Alternatives
(N
));
1234 while Present
(Alt
) loop
1235 if Alt
/= Chosen
then
1236 Remove_Warning_Messages
(Statements
(Alt
));
1243 end Analyze_Case_Statement
;
1245 ----------------------------
1246 -- Analyze_Exit_Statement --
1247 ----------------------------
1249 -- If the exit includes a name, it must be the name of a currently open
1250 -- loop. Otherwise there must be an innermost open loop on the stack, to
1251 -- which the statement implicitly refers.
1253 -- Additionally, in SPARK mode:
1255 -- The exit can only name the closest enclosing loop;
1257 -- An exit with a when clause must be directly contained in a loop;
1259 -- An exit without a when clause must be directly contained in an
1260 -- if-statement with no elsif or else, which is itself directly contained
1261 -- in a loop. The exit must be the last statement in the if-statement.
1263 procedure Analyze_Exit_Statement
(N
: Node_Id
) is
1264 Target
: constant Node_Id
:= Name
(N
);
1265 Cond
: constant Node_Id
:= Condition
(N
);
1266 Scope_Id
: Entity_Id
;
1272 Check_Unreachable_Code
(N
);
1275 if Present
(Target
) then
1277 U_Name
:= Entity
(Target
);
1279 if not In_Open_Scopes
(U_Name
) or else Ekind
(U_Name
) /= E_Loop
then
1280 Error_Msg_N
("invalid loop name in exit statement", N
);
1284 if Has_Loop_In_Inner_Open_Scopes
(U_Name
) then
1285 Check_SPARK_Restriction
1286 ("exit label must name the closest enclosing loop", N
);
1289 Set_Has_Exit
(U_Name
);
1296 for J
in reverse 0 .. Scope_Stack
.Last
loop
1297 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
1298 Kind
:= Ekind
(Scope_Id
);
1301 and then (No
(Target
) or else Scope_Id
= U_Name
)
1303 Set_Has_Exit
(Scope_Id
);
1306 elsif Kind
= E_Block
1307 or else Kind
= E_Loop
1308 or else Kind
= E_Return_Statement
1314 ("cannot exit from program unit or accept statement", N
);
1319 -- Verify that if present the condition is a Boolean expression
1321 if Present
(Cond
) then
1322 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1323 Check_Unset_Reference
(Cond
);
1326 -- In SPARK mode, verify that the exit statement respects the SPARK
1329 if Present
(Cond
) then
1330 if Nkind
(Parent
(N
)) /= N_Loop_Statement
then
1331 Check_SPARK_Restriction
1332 ("exit with when clause must be directly in loop", N
);
1336 if Nkind
(Parent
(N
)) /= N_If_Statement
then
1337 if Nkind
(Parent
(N
)) = N_Elsif_Part
then
1338 Check_SPARK_Restriction
1339 ("exit must be in IF without ELSIF", N
);
1341 Check_SPARK_Restriction
("exit must be directly in IF", N
);
1344 elsif Nkind
(Parent
(Parent
(N
))) /= N_Loop_Statement
then
1345 Check_SPARK_Restriction
1346 ("exit must be in IF directly in loop", N
);
1348 -- First test the presence of ELSE, so that an exit in an ELSE leads
1349 -- to an error mentioning the ELSE.
1351 elsif Present
(Else_Statements
(Parent
(N
))) then
1352 Check_SPARK_Restriction
("exit must be in IF without ELSE", N
);
1354 -- An exit in an ELSIF does not reach here, as it would have been
1355 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1357 elsif Present
(Elsif_Parts
(Parent
(N
))) then
1358 Check_SPARK_Restriction
("exit must be in IF without ELSIF", N
);
1362 -- Chain exit statement to associated loop entity
1364 Set_Next_Exit_Statement
(N
, First_Exit_Statement
(Scope_Id
));
1365 Set_First_Exit_Statement
(Scope_Id
, N
);
1367 -- Since the exit may take us out of a loop, any previous assignment
1368 -- statement is not useless, so clear last assignment indications. It
1369 -- is OK to keep other current values, since if the exit statement
1370 -- does not exit, then the current values are still valid.
1372 Kill_Current_Values
(Last_Assignment_Only
=> True);
1373 end Analyze_Exit_Statement
;
1375 ----------------------------
1376 -- Analyze_Goto_Statement --
1377 ----------------------------
1379 procedure Analyze_Goto_Statement
(N
: Node_Id
) is
1380 Label
: constant Node_Id
:= Name
(N
);
1381 Scope_Id
: Entity_Id
;
1382 Label_Scope
: Entity_Id
;
1383 Label_Ent
: Entity_Id
;
1386 Check_SPARK_Restriction
("goto statement is not allowed", N
);
1388 -- Actual semantic checks
1390 Check_Unreachable_Code
(N
);
1391 Kill_Current_Values
(Last_Assignment_Only
=> True);
1394 Label_Ent
:= Entity
(Label
);
1396 -- Ignore previous error
1398 if Label_Ent
= Any_Id
then
1399 Check_Error_Detected
;
1402 -- We just have a label as the target of a goto
1404 elsif Ekind
(Label_Ent
) /= E_Label
then
1405 Error_Msg_N
("target of goto statement must be a label", Label
);
1408 -- Check that the target of the goto is reachable according to Ada
1409 -- scoping rules. Note: the special gotos we generate for optimizing
1410 -- local handling of exceptions would violate these rules, but we mark
1411 -- such gotos as analyzed when built, so this code is never entered.
1413 elsif not Reachable
(Label_Ent
) then
1414 Error_Msg_N
("target of goto statement is not reachable", Label
);
1418 -- Here if goto passes initial validity checks
1420 Label_Scope
:= Enclosing_Scope
(Label_Ent
);
1422 for J
in reverse 0 .. Scope_Stack
.Last
loop
1423 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
1425 if Label_Scope
= Scope_Id
1426 or else (Ekind
(Scope_Id
) /= E_Block
1427 and then Ekind
(Scope_Id
) /= E_Loop
1428 and then Ekind
(Scope_Id
) /= E_Return_Statement
)
1430 if Scope_Id
/= Label_Scope
then
1432 ("cannot exit from program unit or accept statement", N
);
1439 raise Program_Error
;
1440 end Analyze_Goto_Statement
;
1442 --------------------------
1443 -- Analyze_If_Statement --
1444 --------------------------
1446 -- A special complication arises in the analysis of if statements
1448 -- The expander has circuitry to completely delete code that it can tell
1449 -- will not be executed (as a result of compile time known conditions). In
1450 -- the analyzer, we ensure that code that will be deleted in this manner is
1451 -- analyzed but not expanded. This is obviously more efficient, but more
1452 -- significantly, difficulties arise if code is expanded and then
1453 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1454 -- generated in deleted code must be frozen from start, because the nodes
1455 -- on which they depend will not be available at the freeze point.
1457 procedure Analyze_If_Statement
(N
: Node_Id
) is
1460 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1461 -- Recursively save value of this global, will be restored on exit
1463 Save_In_Deleted_Code
: Boolean;
1465 Del
: Boolean := False;
1466 -- This flag gets set True if a True condition has been found, which
1467 -- means that remaining ELSE/ELSIF parts are deleted.
1469 procedure Analyze_Cond_Then
(Cnode
: Node_Id
);
1470 -- This is applied to either the N_If_Statement node itself or to an
1471 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1472 -- statements associated with it.
1474 -----------------------
1475 -- Analyze_Cond_Then --
1476 -----------------------
1478 procedure Analyze_Cond_Then
(Cnode
: Node_Id
) is
1479 Cond
: constant Node_Id
:= Condition
(Cnode
);
1480 Tstm
: constant List_Id
:= Then_Statements
(Cnode
);
1483 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
1484 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1485 Check_Unset_Reference
(Cond
);
1486 Set_Current_Value_Condition
(Cnode
);
1488 -- If already deleting, then just analyze then statements
1491 Analyze_Statements
(Tstm
);
1493 -- Compile time known value, not deleting yet
1495 elsif Compile_Time_Known_Value
(Cond
) then
1496 Save_In_Deleted_Code
:= In_Deleted_Code
;
1498 -- If condition is True, then analyze the THEN statements and set
1499 -- no expansion for ELSE and ELSIF parts.
1501 if Is_True
(Expr_Value
(Cond
)) then
1502 Analyze_Statements
(Tstm
);
1504 Expander_Mode_Save_And_Set
(False);
1505 In_Deleted_Code
:= True;
1507 -- If condition is False, analyze THEN with expansion off
1509 else -- Is_False (Expr_Value (Cond))
1510 Expander_Mode_Save_And_Set
(False);
1511 In_Deleted_Code
:= True;
1512 Analyze_Statements
(Tstm
);
1513 Expander_Mode_Restore
;
1514 In_Deleted_Code
:= Save_In_Deleted_Code
;
1517 -- Not known at compile time, not deleting, normal analysis
1520 Analyze_Statements
(Tstm
);
1522 end Analyze_Cond_Then
;
1524 -- Start of Analyze_If_Statement
1527 -- Initialize exit count for else statements. If there is no else part,
1528 -- this count will stay non-zero reflecting the fact that the uncovered
1529 -- else case is an unblocked exit.
1531 Unblocked_Exit_Count
:= 1;
1532 Analyze_Cond_Then
(N
);
1534 -- Now to analyze the elsif parts if any are present
1536 if Present
(Elsif_Parts
(N
)) then
1537 E
:= First
(Elsif_Parts
(N
));
1538 while Present
(E
) loop
1539 Analyze_Cond_Then
(E
);
1544 if Present
(Else_Statements
(N
)) then
1545 Analyze_Statements
(Else_Statements
(N
));
1548 -- If all our exits were blocked by unconditional transfers of control,
1549 -- then the entire IF statement acts as an unconditional transfer of
1550 -- control, so treat it like one, and check unreachable code.
1552 if Unblocked_Exit_Count
= 0 then
1553 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1554 Check_Unreachable_Code
(N
);
1556 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1560 Expander_Mode_Restore
;
1561 In_Deleted_Code
:= Save_In_Deleted_Code
;
1564 if not Expander_Active
1565 and then Compile_Time_Known_Value
(Condition
(N
))
1566 and then Serious_Errors_Detected
= 0
1568 if Is_True
(Expr_Value
(Condition
(N
))) then
1569 Remove_Warning_Messages
(Else_Statements
(N
));
1571 if Present
(Elsif_Parts
(N
)) then
1572 E
:= First
(Elsif_Parts
(N
));
1573 while Present
(E
) loop
1574 Remove_Warning_Messages
(Then_Statements
(E
));
1580 Remove_Warning_Messages
(Then_Statements
(N
));
1583 end Analyze_If_Statement
;
1585 ----------------------------------------
1586 -- Analyze_Implicit_Label_Declaration --
1587 ----------------------------------------
1589 -- An implicit label declaration is generated in the innermost enclosing
1590 -- declarative part. This is done for labels, and block and loop names.
1592 -- Note: any changes in this routine may need to be reflected in
1593 -- Analyze_Label_Entity.
1595 procedure Analyze_Implicit_Label_Declaration
(N
: Node_Id
) is
1596 Id
: constant Node_Id
:= Defining_Identifier
(N
);
1599 Set_Ekind
(Id
, E_Label
);
1600 Set_Etype
(Id
, Standard_Void_Type
);
1601 Set_Enclosing_Scope
(Id
, Current_Scope
);
1602 end Analyze_Implicit_Label_Declaration
;
1604 ------------------------------
1605 -- Analyze_Iteration_Scheme --
1606 ------------------------------
1608 procedure Analyze_Iteration_Scheme
(N
: Node_Id
) is
1610 Iter_Spec
: Node_Id
;
1611 Loop_Spec
: Node_Id
;
1614 -- For an infinite loop, there is no iteration scheme
1620 Cond
:= Condition
(N
);
1621 Iter_Spec
:= Iterator_Specification
(N
);
1622 Loop_Spec
:= Loop_Parameter_Specification
(N
);
1624 if Present
(Cond
) then
1625 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1626 Check_Unset_Reference
(Cond
);
1627 Set_Current_Value_Condition
(N
);
1629 elsif Present
(Iter_Spec
) then
1630 Analyze_Iterator_Specification
(Iter_Spec
);
1633 Analyze_Loop_Parameter_Specification
(Loop_Spec
);
1635 end Analyze_Iteration_Scheme
;
1637 ------------------------------------
1638 -- Analyze_Iterator_Specification --
1639 ------------------------------------
1641 procedure Analyze_Iterator_Specification
(N
: Node_Id
) is
1642 Loc
: constant Source_Ptr
:= Sloc
(N
);
1643 Def_Id
: constant Node_Id
:= Defining_Identifier
(N
);
1644 Subt
: constant Node_Id
:= Subtype_Indication
(N
);
1645 Iter_Name
: constant Node_Id
:= Name
(N
);
1651 Enter_Name
(Def_Id
);
1653 if Present
(Subt
) then
1657 Preanalyze_Range
(Iter_Name
);
1659 -- Set the kind of the loop variable, which is not visible within
1660 -- the iterator name.
1662 Set_Ekind
(Def_Id
, E_Variable
);
1664 -- If the domain of iteration is an expression, create a declaration for
1665 -- it, so that finalization actions are introduced outside of the loop.
1666 -- The declaration must be a renaming because the body of the loop may
1667 -- assign to elements.
1669 if not Is_Entity_Name
(Iter_Name
)
1671 -- When the context is a quantified expression, the renaming
1672 -- declaration is delayed until the expansion phase if we are
1675 and then (Nkind
(Parent
(N
)) /= N_Quantified_Expression
1676 or else Operating_Mode
= Check_Semantics
)
1678 -- Do not perform this expansion in Alfa mode, since the formal
1679 -- verification directly deals with the source form of the iterator.
1681 and then not Alfa_Mode
1684 Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R', Iter_Name
);
1688 Typ
:= Etype
(Iter_Name
);
1690 -- Protect against malformed iterator
1692 if Typ
= Any_Type
then
1693 Error_Msg_N
("invalid expression in loop iterator", Iter_Name
);
1697 -- The name in the renaming declaration may be a function call.
1698 -- Indicate that it does not come from source, to suppress
1699 -- spurious warnings on renamings of parameterless functions,
1700 -- a common enough idiom in user-defined iterators.
1703 Make_Object_Renaming_Declaration
(Loc
,
1704 Defining_Identifier
=> Id
,
1705 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
1707 New_Copy_Tree
(Iter_Name
, New_Sloc
=> Loc
));
1709 Insert_Actions
(Parent
(Parent
(N
)), New_List
(Decl
));
1710 Rewrite
(Name
(N
), New_Occurrence_Of
(Id
, Loc
));
1711 Set_Etype
(Id
, Typ
);
1712 Set_Etype
(Name
(N
), Typ
);
1715 -- Container is an entity or an array with uncontrolled components, or
1716 -- else it is a container iterator given by a function call, typically
1717 -- called Iterate in the case of predefined containers, even though
1718 -- Iterate is not a reserved name. What matters is that the return type
1719 -- of the function is an iterator type.
1721 elsif Is_Entity_Name
(Iter_Name
) then
1722 Analyze
(Iter_Name
);
1724 if Nkind
(Iter_Name
) = N_Function_Call
then
1726 C
: constant Node_Id
:= Name
(Iter_Name
);
1731 if not Is_Overloaded
(Iter_Name
) then
1732 Resolve
(Iter_Name
, Etype
(C
));
1735 Get_First_Interp
(C
, I
, It
);
1736 while It
.Typ
/= Empty
loop
1737 if Reverse_Present
(N
) then
1738 if Is_Reversible_Iterator
(It
.Typ
) then
1739 Resolve
(Iter_Name
, It
.Typ
);
1743 elsif Is_Iterator
(It
.Typ
) then
1744 Resolve
(Iter_Name
, It
.Typ
);
1748 Get_Next_Interp
(I
, It
);
1753 -- Domain of iteration is not overloaded
1756 Resolve
(Iter_Name
, Etype
(Iter_Name
));
1760 Typ
:= Etype
(Iter_Name
);
1762 if Is_Array_Type
(Typ
) then
1763 if Of_Present
(N
) then
1764 Set_Etype
(Def_Id
, Component_Type
(Typ
));
1766 -- Here we have a missing Range attribute
1770 ("missing Range attribute in iteration over an array", N
);
1772 -- In Ada 2012 mode, this may be an attempt at an iterator
1774 if Ada_Version
>= Ada_2012
then
1776 ("\if& is meant to designate an element of the array, use OF",
1780 -- Prevent cascaded errors
1782 Set_Ekind
(Def_Id
, E_Loop_Parameter
);
1783 Set_Etype
(Def_Id
, Etype
(First_Index
(Typ
)));
1786 -- Check for type error in iterator
1788 elsif Typ
= Any_Type
then
1791 -- Iteration over a container
1794 Set_Ekind
(Def_Id
, E_Loop_Parameter
);
1796 if Of_Present
(N
) then
1798 -- The type of the loop variable is the Iterator_Element aspect of
1799 -- the container type.
1802 Element
: constant Entity_Id
:=
1803 Find_Aspect
(Typ
, Aspect_Iterator_Element
);
1805 if No
(Element
) then
1806 Error_Msg_NE
("cannot iterate over&", N
, Typ
);
1809 Set_Etype
(Def_Id
, Entity
(Element
));
1811 -- If the container has a variable indexing aspect, the
1812 -- element is a variable and is modifiable in the loop.
1814 if Present
(Find_Aspect
(Typ
, Aspect_Variable_Indexing
)) then
1815 Set_Ekind
(Def_Id
, E_Variable
);
1821 -- For an iteration of the form IN, the name must denote an
1822 -- iterator, typically the result of a call to Iterate. Give a
1823 -- useful error message when the name is a container by itself.
1825 if Is_Entity_Name
(Original_Node
(Name
(N
)))
1826 and then not Is_Iterator
(Typ
)
1828 if No
(Find_Aspect
(Typ
, Aspect_Iterator_Element
)) then
1830 ("cannot iterate over&", Name
(N
), Typ
);
1833 ("name must be an iterator, not a container", Name
(N
));
1837 ("\to iterate directly over the elements of a container, " &
1838 "write `of &`", Name
(N
), Original_Node
(Name
(N
)));
1841 -- The result type of Iterate function is the classwide type of
1842 -- the interface parent. We need the specific Cursor type defined
1843 -- in the container package.
1845 Ent
:= First_Entity
(Scope
(Typ
));
1846 while Present
(Ent
) loop
1847 if Chars
(Ent
) = Name_Cursor
then
1848 Set_Etype
(Def_Id
, Etype
(Ent
));
1856 end Analyze_Iterator_Specification
;
1862 -- Note: the semantic work required for analyzing labels (setting them as
1863 -- reachable) was done in a prepass through the statements in the block,
1864 -- so that forward gotos would be properly handled. See Analyze_Statements
1865 -- for further details. The only processing required here is to deal with
1866 -- optimizations that depend on an assumption of sequential control flow,
1867 -- since of course the occurrence of a label breaks this assumption.
1869 procedure Analyze_Label
(N
: Node_Id
) is
1870 pragma Warnings
(Off
, N
);
1872 Kill_Current_Values
;
1875 --------------------------
1876 -- Analyze_Label_Entity --
1877 --------------------------
1879 procedure Analyze_Label_Entity
(E
: Entity_Id
) is
1881 Set_Ekind
(E
, E_Label
);
1882 Set_Etype
(E
, Standard_Void_Type
);
1883 Set_Enclosing_Scope
(E
, Current_Scope
);
1884 Set_Reachable
(E
, True);
1885 end Analyze_Label_Entity
;
1887 ------------------------------------------
1888 -- Analyze_Loop_Parameter_Specification --
1889 ------------------------------------------
1891 procedure Analyze_Loop_Parameter_Specification
(N
: Node_Id
) is
1892 Loop_Nod
: constant Node_Id
:= Parent
(Parent
(N
));
1894 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
);
1895 -- If the bounds are given by a 'Range reference on a function call
1896 -- that returns a controlled array, introduce an explicit declaration
1897 -- to capture the bounds, so that the function result can be finalized
1898 -- in timely fashion.
1900 function Has_Call_Using_Secondary_Stack
(N
: Node_Id
) return Boolean;
1901 -- N is the node for an arbitrary construct. This function searches the
1902 -- construct N to see if any expressions within it contain function
1903 -- calls that use the secondary stack, returning True if any such call
1904 -- is found, and False otherwise.
1906 procedure Process_Bounds
(R
: Node_Id
);
1907 -- If the iteration is given by a range, create temporaries and
1908 -- assignment statements block to capture the bounds and perform
1909 -- required finalization actions in case a bound includes a function
1910 -- call that uses the temporary stack. We first pre-analyze a copy of
1911 -- the range in order to determine the expected type, and analyze and
1912 -- resolve the original bounds.
1914 --------------------------------------
1915 -- Check_Controlled_Array_Attribute --
1916 --------------------------------------
1918 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
) is
1920 if Nkind
(DS
) = N_Attribute_Reference
1921 and then Is_Entity_Name
(Prefix
(DS
))
1922 and then Ekind
(Entity
(Prefix
(DS
))) = E_Function
1923 and then Is_Array_Type
(Etype
(Entity
(Prefix
(DS
))))
1925 Is_Controlled
(Component_Type
(Etype
(Entity
(Prefix
(DS
)))))
1926 and then Expander_Active
1929 Loc
: constant Source_Ptr
:= Sloc
(N
);
1930 Arr
: constant Entity_Id
:= Etype
(Entity
(Prefix
(DS
)));
1931 Indx
: constant Entity_Id
:=
1932 Base_Type
(Etype
(First_Index
(Arr
)));
1933 Subt
: constant Entity_Id
:= Make_Temporary
(Loc
, 'S');
1938 Make_Subtype_Declaration
(Loc
,
1939 Defining_Identifier
=> Subt
,
1940 Subtype_Indication
=>
1941 Make_Subtype_Indication
(Loc
,
1942 Subtype_Mark
=> New_Reference_To
(Indx
, Loc
),
1944 Make_Range_Constraint
(Loc
, Relocate_Node
(DS
))));
1945 Insert_Before
(Loop_Nod
, Decl
);
1949 Make_Attribute_Reference
(Loc
,
1950 Prefix
=> New_Reference_To
(Subt
, Loc
),
1951 Attribute_Name
=> Attribute_Name
(DS
)));
1956 end Check_Controlled_Array_Attribute
;
1958 ------------------------------------
1959 -- Has_Call_Using_Secondary_Stack --
1960 ------------------------------------
1962 function Has_Call_Using_Secondary_Stack
(N
: Node_Id
) return Boolean is
1964 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
1965 -- Check if N is a function call which uses the secondary stack
1971 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
1974 Return_Typ
: Entity_Id
;
1977 if Nkind
(N
) = N_Function_Call
then
1980 -- Call using access to subprogram with explicit dereference
1982 if Nkind
(Nam
) = N_Explicit_Dereference
then
1983 Subp
:= Etype
(Nam
);
1985 -- Call using a selected component notation or Ada 2005 object
1986 -- operation notation
1988 elsif Nkind
(Nam
) = N_Selected_Component
then
1989 Subp
:= Entity
(Selector_Name
(Nam
));
1994 Subp
:= Entity
(Nam
);
1997 Return_Typ
:= Etype
(Subp
);
1999 if Is_Composite_Type
(Return_Typ
)
2000 and then not Is_Constrained
(Return_Typ
)
2004 elsif Sec_Stack_Needed_For_Return
(Subp
) then
2009 -- Continue traversing the tree
2014 function Check_Calls
is new Traverse_Func
(Check_Call
);
2016 -- Start of processing for Has_Call_Using_Secondary_Stack
2019 return Check_Calls
(N
) = Abandon
;
2020 end Has_Call_Using_Secondary_Stack
;
2022 --------------------
2023 -- Process_Bounds --
2024 --------------------
2026 procedure Process_Bounds
(R
: Node_Id
) is
2027 Loc
: constant Source_Ptr
:= Sloc
(N
);
2030 (Original_Bound
: Node_Id
;
2031 Analyzed_Bound
: Node_Id
;
2032 Typ
: Entity_Id
) return Node_Id
;
2033 -- Capture value of bound and return captured value
2040 (Original_Bound
: Node_Id
;
2041 Analyzed_Bound
: Node_Id
;
2042 Typ
: Entity_Id
) return Node_Id
2049 -- If the bound is a constant or an object, no need for a separate
2050 -- declaration. If the bound is the result of previous expansion
2051 -- it is already analyzed and should not be modified. Note that
2052 -- the Bound will be resolved later, if needed, as part of the
2053 -- call to Make_Index (literal bounds may need to be resolved to
2056 if Analyzed
(Original_Bound
) then
2057 return Original_Bound
;
2059 elsif Nkind_In
(Analyzed_Bound
, N_Integer_Literal
,
2060 N_Character_Literal
)
2061 or else Is_Entity_Name
(Analyzed_Bound
)
2063 Analyze_And_Resolve
(Original_Bound
, Typ
);
2064 return Original_Bound
;
2067 -- Normally, the best approach is simply to generate a constant
2068 -- declaration that captures the bound. However, there is a nasty
2069 -- case where this is wrong. If the bound is complex, and has a
2070 -- possible use of the secondary stack, we need to generate a
2071 -- separate assignment statement to ensure the creation of a block
2072 -- which will release the secondary stack.
2074 -- We prefer the constant declaration, since it leaves us with a
2075 -- proper trace of the value, useful in optimizations that get rid
2076 -- of junk range checks.
2078 if not Has_Call_Using_Secondary_Stack
(Analyzed_Bound
) then
2079 Analyze_And_Resolve
(Original_Bound
, Typ
);
2080 Force_Evaluation
(Original_Bound
);
2081 return Original_Bound
;
2084 Id
:= Make_Temporary
(Loc
, 'R', Original_Bound
);
2086 -- Here we make a declaration with a separate assignment
2087 -- statement, and insert before loop header.
2090 Make_Object_Declaration
(Loc
,
2091 Defining_Identifier
=> Id
,
2092 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
2095 Make_Assignment_Statement
(Loc
,
2096 Name
=> New_Occurrence_Of
(Id
, Loc
),
2097 Expression
=> Relocate_Node
(Original_Bound
));
2099 Insert_Actions
(Loop_Nod
, New_List
(Decl
, Assign
));
2101 -- Now that this temporary variable is initialized we decorate it
2102 -- as safe-to-reevaluate to inform to the backend that no further
2103 -- asignment will be issued and hence it can be handled as side
2104 -- effect free. Note that this decoration must be done when the
2105 -- assignment has been analyzed because otherwise it will be
2106 -- rejected (see Analyze_Assignment).
2108 Set_Is_Safe_To_Reevaluate
(Id
);
2110 Rewrite
(Original_Bound
, New_Occurrence_Of
(Id
, Loc
));
2112 if Nkind
(Assign
) = N_Assignment_Statement
then
2113 return Expression
(Assign
);
2115 return Original_Bound
;
2119 Hi
: constant Node_Id
:= High_Bound
(R
);
2120 Lo
: constant Node_Id
:= Low_Bound
(R
);
2121 R_Copy
: constant Node_Id
:= New_Copy_Tree
(R
);
2126 -- Start of processing for Process_Bounds
2129 Set_Parent
(R_Copy
, Parent
(R
));
2130 Preanalyze_Range
(R_Copy
);
2131 Typ
:= Etype
(R_Copy
);
2133 -- If the type of the discrete range is Universal_Integer, then the
2134 -- bound's type must be resolved to Integer, and any object used to
2135 -- hold the bound must also have type Integer, unless the literal
2136 -- bounds are constant-folded expressions with a user-defined type.
2138 if Typ
= Universal_Integer
then
2139 if Nkind
(Lo
) = N_Integer_Literal
2140 and then Present
(Etype
(Lo
))
2141 and then Scope
(Etype
(Lo
)) /= Standard_Standard
2145 elsif Nkind
(Hi
) = N_Integer_Literal
2146 and then Present
(Etype
(Hi
))
2147 and then Scope
(Etype
(Hi
)) /= Standard_Standard
2152 Typ
:= Standard_Integer
;
2158 New_Lo
:= One_Bound
(Lo
, Low_Bound
(R_Copy
), Typ
);
2159 New_Hi
:= One_Bound
(Hi
, High_Bound
(R_Copy
), Typ
);
2161 -- Propagate staticness to loop range itself, in case the
2162 -- corresponding subtype is static.
2165 and then Is_Static_Expression
(New_Lo
)
2167 Rewrite
(Low_Bound
(R
), New_Copy
(New_Lo
));
2171 and then Is_Static_Expression
(New_Hi
)
2173 Rewrite
(High_Bound
(R
), New_Copy
(New_Hi
));
2179 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(N
);
2180 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2184 -- Start of processing for Analyze_Loop_Parameter_Specification
2189 -- We always consider the loop variable to be referenced, since the loop
2190 -- may be used just for counting purposes.
2192 Generate_Reference
(Id
, N
, ' ');
2194 -- Check for the case of loop variable hiding a local variable (used
2195 -- later on to give a nice warning if the hidden variable is never
2199 H
: constant Entity_Id
:= Homonym
(Id
);
2202 and then Ekind
(H
) = E_Variable
2203 and then Is_Discrete_Type
(Etype
(H
))
2204 and then Enclosing_Dynamic_Scope
(H
) = Enclosing_Dynamic_Scope
(Id
)
2206 Set_Hiding_Loop_Variable
(H
, Id
);
2210 -- Loop parameter specification must include subtype mark in SPARK
2212 if Nkind
(DS
) = N_Range
then
2213 Check_SPARK_Restriction
2214 ("loop parameter specification must include subtype mark", N
);
2217 -- Analyze the subtype definition and create temporaries for the bounds.
2218 -- Do not evaluate the range when preanalyzing a quantified expression
2219 -- because bounds expressed as function calls with side effects will be
2220 -- erroneously replicated.
2222 if Nkind
(DS
) = N_Range
2223 and then Expander_Active
2224 and then Nkind
(Parent
(N
)) /= N_Quantified_Expression
2226 Process_Bounds
(DS
);
2228 -- Either the expander not active or the range of iteration is a subtype
2229 -- indication, an entity, or a function call that yields an aggregate or
2233 DS_Copy
:= New_Copy_Tree
(DS
);
2234 Set_Parent
(DS_Copy
, Parent
(DS
));
2235 Preanalyze_Range
(DS_Copy
);
2237 -- Ada 2012: If the domain of iteration is a function call, it is the
2238 -- new iterator form.
2240 if Nkind
(DS_Copy
) = N_Function_Call
2242 (Is_Entity_Name
(DS_Copy
)
2243 and then not Is_Type
(Entity
(DS_Copy
)))
2245 -- This is an iterator specification. Rewrite it as such and
2246 -- analyze it to capture function calls that may require
2247 -- finalization actions.
2250 I_Spec
: constant Node_Id
:=
2251 Make_Iterator_Specification
(Sloc
(N
),
2252 Defining_Identifier
=> Relocate_Node
(Id
),
2254 Subtype_Indication
=> Empty
,
2255 Reverse_Present
=> Reverse_Present
(N
));
2256 Scheme
: constant Node_Id
:= Parent
(N
);
2259 Set_Iterator_Specification
(Scheme
, I_Spec
);
2260 Set_Loop_Parameter_Specification
(Scheme
, Empty
);
2261 Analyze_Iterator_Specification
(I_Spec
);
2263 -- In a generic context, analyze the original domain of
2264 -- iteration, for name capture.
2266 if not Expander_Active
then
2270 -- Set kind of loop parameter, which may be used in the
2271 -- subsequent analysis of the condition in a quantified
2274 Set_Ekind
(Id
, E_Loop_Parameter
);
2278 -- Domain of iteration is not a function call, and is side-effect
2282 -- A quantified expression that appears in a pre/post condition
2283 -- is pre-analyzed several times. If the range is given by an
2284 -- attribute reference it is rewritten as a range, and this is
2285 -- done even with expansion disabled. If the type is already set
2286 -- do not reanalyze, because a range with static bounds may be
2287 -- typed Integer by default.
2289 if Nkind
(Parent
(N
)) = N_Quantified_Expression
2290 and then Present
(Etype
(DS
))
2303 -- Some additional checks if we are iterating through a type
2305 if Is_Entity_Name
(DS
)
2306 and then Present
(Entity
(DS
))
2307 and then Is_Type
(Entity
(DS
))
2309 -- The subtype indication may denote the completion of an incomplete
2310 -- type declaration.
2312 if Ekind
(Entity
(DS
)) = E_Incomplete_Type
then
2313 Set_Entity
(DS
, Get_Full_View
(Entity
(DS
)));
2314 Set_Etype
(DS
, Entity
(DS
));
2317 -- Attempt to iterate through non-static predicate
2319 if Is_Discrete_Type
(Entity
(DS
))
2320 and then Present
(Predicate_Function
(Entity
(DS
)))
2321 and then No
(Static_Predicate
(Entity
(DS
)))
2323 Bad_Predicated_Subtype_Use
2324 ("cannot use subtype& with non-static predicate for loop " &
2325 "iteration", DS
, Entity
(DS
));
2329 -- Error if not discrete type
2331 if not Is_Discrete_Type
(Etype
(DS
)) then
2332 Wrong_Type
(DS
, Any_Discrete
);
2333 Set_Etype
(DS
, Any_Type
);
2336 Check_Controlled_Array_Attribute
(DS
);
2338 Make_Index
(DS
, N
, In_Iter_Schm
=> True);
2339 Set_Ekind
(Id
, E_Loop_Parameter
);
2341 -- A quantified expression which appears in a pre- or post-condition may
2342 -- be analyzed multiple times. The analysis of the range creates several
2343 -- itypes which reside in different scopes depending on whether the pre-
2344 -- or post-condition has been expanded. Update the type of the loop
2345 -- variable to reflect the proper itype at each stage of analysis.
2348 or else Etype
(Id
) = Any_Type
2350 (Present
(Etype
(Id
))
2351 and then Is_Itype
(Etype
(Id
))
2352 and then Nkind
(Parent
(Loop_Nod
)) = N_Expression_With_Actions
2353 and then Nkind
(Original_Node
(Parent
(Loop_Nod
))) =
2354 N_Quantified_Expression
)
2356 Set_Etype
(Id
, Etype
(DS
));
2359 -- Treat a range as an implicit reference to the type, to inhibit
2360 -- spurious warnings.
2362 Generate_Reference
(Base_Type
(Etype
(DS
)), N
, ' ');
2363 Set_Is_Known_Valid
(Id
, True);
2365 -- The loop is not a declarative part, so the loop variable must be
2366 -- frozen explicitly. Do not freeze while preanalyzing a quantified
2367 -- expression because the freeze node will not be inserted into the
2368 -- tree due to flag Is_Spec_Expression being set.
2370 if Nkind
(Parent
(N
)) /= N_Quantified_Expression
then
2372 Flist
: constant List_Id
:= Freeze_Entity
(Id
, N
);
2374 if Is_Non_Empty_List
(Flist
) then
2375 Insert_Actions
(N
, Flist
);
2380 -- Check for null or possibly null range and issue warning. We suppress
2381 -- such messages in generic templates and instances, because in practice
2382 -- they tend to be dubious in these cases.
2384 if Nkind
(DS
) = N_Range
and then Comes_From_Source
(N
) then
2386 L
: constant Node_Id
:= Low_Bound
(DS
);
2387 H
: constant Node_Id
:= High_Bound
(DS
);
2390 -- If range of loop is null, issue warning
2392 if Compile_Time_Compare
(L
, H
, Assume_Valid
=> True) = GT
then
2394 -- Suppress the warning if inside a generic template or
2395 -- instance, since in practice they tend to be dubious in these
2396 -- cases since they can result from intended parametrization.
2398 if not Inside_A_Generic
2399 and then not In_Instance
2401 -- Specialize msg if invalid values could make the loop
2402 -- non-null after all.
2404 if Compile_Time_Compare
2405 (L
, H
, Assume_Valid
=> False) = GT
2408 ("??loop range is null, loop will not execute", DS
);
2410 -- Since we know the range of the loop is null, set the
2411 -- appropriate flag to remove the loop entirely during
2414 Set_Is_Null_Loop
(Loop_Nod
);
2416 -- Here is where the loop could execute because of invalid
2417 -- values, so issue appropriate message and in this case we
2418 -- do not set the Is_Null_Loop flag since the loop may
2423 ("??loop range may be null, loop may not execute",
2426 ("??can only execute if invalid values are present",
2431 -- In either case, suppress warnings in the body of the loop,
2432 -- since it is likely that these warnings will be inappropriate
2433 -- if the loop never actually executes, which is likely.
2435 Set_Suppress_Loop_Warnings
(Loop_Nod
);
2437 -- The other case for a warning is a reverse loop where the
2438 -- upper bound is the integer literal zero or one, and the
2439 -- lower bound can be positive.
2441 -- For example, we have
2443 -- for J in reverse N .. 1 loop
2445 -- In practice, this is very likely to be a case of reversing
2446 -- the bounds incorrectly in the range.
2448 elsif Reverse_Present
(N
)
2449 and then Nkind
(Original_Node
(H
)) = N_Integer_Literal
2451 (Intval
(Original_Node
(H
)) = Uint_0
2452 or else Intval
(Original_Node
(H
)) = Uint_1
)
2454 Error_Msg_N
("??loop range may be null", DS
);
2455 Error_Msg_N
("\??bounds may be wrong way round", DS
);
2459 end Analyze_Loop_Parameter_Specification
;
2461 ----------------------------
2462 -- Analyze_Loop_Statement --
2463 ----------------------------
2465 procedure Analyze_Loop_Statement
(N
: Node_Id
) is
2467 function Is_Container_Iterator
(Iter
: Node_Id
) return Boolean;
2468 -- Given a loop iteration scheme, determine whether it is an Ada 2012
2469 -- container iteration.
2471 function Is_Wrapped_In_Block
(N
: Node_Id
) return Boolean;
2472 -- Determine whether node N is the sole statement of a block
2474 ---------------------------
2475 -- Is_Container_Iterator --
2476 ---------------------------
2478 function Is_Container_Iterator
(Iter
: Node_Id
) return Boolean is
2487 elsif Present
(Condition
(Iter
)) then
2490 -- for Def_Id in [reverse] Name loop
2491 -- for Def_Id [: Subtype_Indication] of [reverse] Name loop
2493 elsif Present
(Iterator_Specification
(Iter
)) then
2495 Nam
: constant Node_Id
:= Name
(Iterator_Specification
(Iter
));
2499 Nam_Copy
:= New_Copy_Tree
(Nam
);
2500 Set_Parent
(Nam_Copy
, Parent
(Nam
));
2501 Preanalyze_Range
(Nam_Copy
);
2503 -- The only two options here are iteration over a container or
2506 return not Is_Array_Type
(Etype
(Nam_Copy
));
2509 -- for Def_Id in [reverse] Discrete_Subtype_Definition loop
2513 LP
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
2514 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(LP
);
2518 DS_Copy
:= New_Copy_Tree
(DS
);
2519 Set_Parent
(DS_Copy
, Parent
(DS
));
2520 Preanalyze_Range
(DS_Copy
);
2522 -- Check for a call to Iterate ()
2525 Nkind
(DS_Copy
) = N_Function_Call
2526 and then Needs_Finalization
(Etype
(DS_Copy
));
2529 end Is_Container_Iterator
;
2531 -------------------------
2532 -- Is_Wrapped_In_Block --
2533 -------------------------
2535 function Is_Wrapped_In_Block
(N
: Node_Id
) return Boolean is
2536 HSS
: constant Node_Id
:= Parent
(N
);
2540 Nkind
(HSS
) = N_Handled_Sequence_Of_Statements
2541 and then Nkind
(Parent
(HSS
)) = N_Block_Statement
2542 and then First
(Statements
(HSS
)) = N
2543 and then No
(Next
(First
(Statements
(HSS
))));
2544 end Is_Wrapped_In_Block
;
2546 -- Local declarations
2548 Id
: constant Node_Id
:= Identifier
(N
);
2549 Iter
: constant Node_Id
:= Iteration_Scheme
(N
);
2550 Loc
: constant Source_Ptr
:= Sloc
(N
);
2553 -- Start of processing for Analyze_Loop_Statement
2556 if Present
(Id
) then
2558 -- Make name visible, e.g. for use in exit statements. Loop labels
2559 -- are always considered to be referenced.
2564 -- Guard against serious error (typically, a scope mismatch when
2565 -- semantic analysis is requested) by creating loop entity to
2566 -- continue analysis.
2569 if Total_Errors_Detected
/= 0 then
2570 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Loc
, 'L');
2572 raise Program_Error
;
2576 Generate_Reference
(Ent
, N
, ' ');
2577 Generate_Definition
(Ent
);
2579 -- If we found a label, mark its type. If not, ignore it, since it
2580 -- means we have a conflicting declaration, which would already
2581 -- have been diagnosed at declaration time. Set Label_Construct
2582 -- of the implicit label declaration, which is not created by the
2583 -- parser for generic units.
2585 if Ekind
(Ent
) = E_Label
then
2586 Set_Ekind
(Ent
, E_Loop
);
2588 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
2589 Set_Label_Construct
(Parent
(Ent
), N
);
2594 -- Case of no identifier present
2597 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Loc
, 'L');
2598 Set_Etype
(Ent
, Standard_Void_Type
);
2599 Set_Parent
(Ent
, N
);
2602 -- Iteration over a container in Ada 2012 involves the creation of a
2603 -- controlled iterator object. Wrap the loop in a block to ensure the
2604 -- timely finalization of the iterator and release of container locks.
2606 if Ada_Version
>= Ada_2012
2607 and then Is_Container_Iterator
(Iter
)
2608 and then not Is_Wrapped_In_Block
(N
)
2611 Make_Block_Statement
(Loc
,
2612 Declarations
=> New_List
,
2613 Handled_Statement_Sequence
=>
2614 Make_Handled_Sequence_Of_Statements
(Loc
,
2615 Statements
=> New_List
(Relocate_Node
(N
)))));
2621 -- Kill current values on entry to loop, since statements in the body of
2622 -- the loop may have been executed before the loop is entered. Similarly
2623 -- we kill values after the loop, since we do not know that the body of
2624 -- the loop was executed.
2626 Kill_Current_Values
;
2628 Analyze_Iteration_Scheme
(Iter
);
2630 -- Check for following case which merits a warning if the type E of is
2631 -- a multi-dimensional array (and no explicit subscript ranges present).
2637 and then Present
(Loop_Parameter_Specification
(Iter
))
2640 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
2641 DSD
: constant Node_Id
:=
2642 Original_Node
(Discrete_Subtype_Definition
(LPS
));
2644 if Nkind
(DSD
) = N_Attribute_Reference
2645 and then Attribute_Name
(DSD
) = Name_Range
2646 and then No
(Expressions
(DSD
))
2649 Typ
: constant Entity_Id
:= Etype
(Prefix
(DSD
));
2651 if Is_Array_Type
(Typ
)
2652 and then Number_Dimensions
(Typ
) > 1
2653 and then Nkind
(Parent
(N
)) = N_Loop_Statement
2654 and then Present
(Iteration_Scheme
(Parent
(N
)))
2657 OIter
: constant Node_Id
:=
2658 Iteration_Scheme
(Parent
(N
));
2659 OLPS
: constant Node_Id
:=
2660 Loop_Parameter_Specification
(OIter
);
2661 ODSD
: constant Node_Id
:=
2662 Original_Node
(Discrete_Subtype_Definition
(OLPS
));
2664 if Nkind
(ODSD
) = N_Attribute_Reference
2665 and then Attribute_Name
(ODSD
) = Name_Range
2666 and then No
(Expressions
(ODSD
))
2667 and then Etype
(Prefix
(ODSD
)) = Typ
2669 Error_Msg_Sloc
:= Sloc
(ODSD
);
2671 ("inner range same as outer range#??", DSD
);
2680 -- Analyze the statements of the body except in the case of an Ada 2012
2681 -- iterator with the expander active. In this case the expander will do
2682 -- a rewrite of the loop into a while loop. We will then analyze the
2683 -- loop body when we analyze this while loop.
2685 -- We need to do this delay because if the container is for indefinite
2686 -- types the actual subtype of the components will only be determined
2687 -- when the cursor declaration is analyzed.
2689 -- If the expander is not active, or in Alfa mode, then we want to
2690 -- analyze the loop body now even in the Ada 2012 iterator case, since
2691 -- the rewriting will not be done. Insert the loop variable in the
2692 -- current scope, if not done when analysing the iteration scheme.
2695 or else No
(Iterator_Specification
(Iter
))
2696 or else not Full_Expander_Active
2699 and then Present
(Iterator_Specification
(Iter
))
2702 Id
: constant Entity_Id
:=
2703 Defining_Identifier
(Iterator_Specification
(Iter
));
2705 if Scope
(Id
) /= Current_Scope
then
2711 Analyze_Statements
(Statements
(N
));
2714 -- Finish up processing for the loop. We kill all current values, since
2715 -- in general we don't know if the statements in the loop have been
2716 -- executed. We could do a bit better than this with a loop that we
2717 -- know will execute at least once, but it's not worth the trouble and
2718 -- the front end is not in the business of flow tracing.
2720 Process_End_Label
(N
, 'e', Ent
);
2722 Kill_Current_Values
;
2724 -- Check for infinite loop. Skip check for generated code, since it
2725 -- justs waste time and makes debugging the routine called harder.
2727 -- Note that we have to wait till the body of the loop is fully analyzed
2728 -- before making this call, since Check_Infinite_Loop_Warning relies on
2729 -- being able to use semantic visibility information to find references.
2731 if Comes_From_Source
(N
) then
2732 Check_Infinite_Loop_Warning
(N
);
2735 -- Code after loop is unreachable if the loop has no WHILE or FOR and
2736 -- contains no EXIT statements within the body of the loop.
2738 if No
(Iter
) and then not Has_Exit
(Ent
) then
2739 Check_Unreachable_Code
(N
);
2741 end Analyze_Loop_Statement
;
2743 ----------------------------
2744 -- Analyze_Null_Statement --
2745 ----------------------------
2747 -- Note: the semantics of the null statement is implemented by a single
2748 -- null statement, too bad everything isn't as simple as this!
2750 procedure Analyze_Null_Statement
(N
: Node_Id
) is
2751 pragma Warnings
(Off
, N
);
2754 end Analyze_Null_Statement
;
2756 ------------------------
2757 -- Analyze_Statements --
2758 ------------------------
2760 procedure Analyze_Statements
(L
: List_Id
) is
2765 -- The labels declared in the statement list are reachable from
2766 -- statements in the list. We do this as a prepass so that any goto
2767 -- statement will be properly flagged if its target is not reachable.
2768 -- This is not required, but is nice behavior!
2771 while Present
(S
) loop
2772 if Nkind
(S
) = N_Label
then
2773 Analyze
(Identifier
(S
));
2774 Lab
:= Entity
(Identifier
(S
));
2776 -- If we found a label mark it as reachable
2778 if Ekind
(Lab
) = E_Label
then
2779 Generate_Definition
(Lab
);
2780 Set_Reachable
(Lab
);
2782 if Nkind
(Parent
(Lab
)) = N_Implicit_Label_Declaration
then
2783 Set_Label_Construct
(Parent
(Lab
), S
);
2786 -- If we failed to find a label, it means the implicit declaration
2787 -- of the label was hidden. A for-loop parameter can do this to
2788 -- a label with the same name inside the loop, since the implicit
2789 -- label declaration is in the innermost enclosing body or block
2793 Error_Msg_Sloc
:= Sloc
(Lab
);
2795 ("implicit label declaration for & is hidden#",
2803 -- Perform semantic analysis on all statements
2805 Conditional_Statements_Begin
;
2808 while Present
(S
) loop
2811 -- Remove dimension in all statements
2813 Remove_Dimension_In_Statement
(S
);
2817 Conditional_Statements_End
;
2819 -- Make labels unreachable. Visibility is not sufficient, because labels
2820 -- in one if-branch for example are not reachable from the other branch,
2821 -- even though their declarations are in the enclosing declarative part.
2824 while Present
(S
) loop
2825 if Nkind
(S
) = N_Label
then
2826 Set_Reachable
(Entity
(Identifier
(S
)), False);
2831 end Analyze_Statements
;
2833 ----------------------------
2834 -- Check_Unreachable_Code --
2835 ----------------------------
2837 procedure Check_Unreachable_Code
(N
: Node_Id
) is
2838 Error_Node
: Node_Id
;
2842 if Is_List_Member
(N
)
2843 and then Comes_From_Source
(N
)
2849 Nxt
:= Original_Node
(Next
(N
));
2851 -- Skip past pragmas
2853 while Nkind
(Nxt
) = N_Pragma
loop
2854 Nxt
:= Original_Node
(Next
(Nxt
));
2857 -- If a label follows us, then we never have dead code, since
2858 -- someone could branch to the label, so we just ignore it, unless
2859 -- we are in formal mode where goto statements are not allowed.
2861 if Nkind
(Nxt
) = N_Label
2862 and then not Restriction_Check_Required
(SPARK
)
2866 -- Otherwise see if we have a real statement following us
2869 and then Comes_From_Source
(Nxt
)
2870 and then Is_Statement
(Nxt
)
2872 -- Special very annoying exception. If we have a return that
2873 -- follows a raise, then we allow it without a warning, since
2874 -- the Ada RM annoyingly requires a useless return here!
2876 if Nkind
(Original_Node
(N
)) /= N_Raise_Statement
2877 or else Nkind
(Nxt
) /= N_Simple_Return_Statement
2879 -- The rather strange shenanigans with the warning message
2880 -- here reflects the fact that Kill_Dead_Code is very good
2881 -- at removing warnings in deleted code, and this is one
2882 -- warning we would prefer NOT to have removed.
2886 -- If we have unreachable code, analyze and remove the
2887 -- unreachable code, since it is useless and we don't
2888 -- want to generate junk warnings.
2890 -- We skip this step if we are not in code generation mode.
2891 -- This is the one case where we remove dead code in the
2892 -- semantics as opposed to the expander, and we do not want
2893 -- to remove code if we are not in code generation mode,
2894 -- since this messes up the ASIS trees.
2896 -- Note that one might react by moving the whole circuit to
2897 -- exp_ch5, but then we lose the warning in -gnatc mode.
2899 if Operating_Mode
= Generate_Code
then
2903 -- Quit deleting when we have nothing more to delete
2904 -- or if we hit a label (since someone could transfer
2905 -- control to a label, so we should not delete it).
2907 exit when No
(Nxt
) or else Nkind
(Nxt
) = N_Label
;
2909 -- Statement/declaration is to be deleted
2913 Kill_Dead_Code
(Nxt
);
2917 -- Now issue the warning (or error in formal mode)
2919 if Restriction_Check_Required
(SPARK
) then
2920 Check_SPARK_Restriction
2921 ("unreachable code is not allowed", Error_Node
);
2923 Error_Msg
("??unreachable code!", Sloc
(Error_Node
));
2927 -- If the unconditional transfer of control instruction is the
2928 -- last statement of a sequence, then see if our parent is one of
2929 -- the constructs for which we count unblocked exits, and if so,
2930 -- adjust the count.
2935 -- Statements in THEN part or ELSE part of IF statement
2937 if Nkind
(P
) = N_If_Statement
then
2940 -- Statements in ELSIF part of an IF statement
2942 elsif Nkind
(P
) = N_Elsif_Part
then
2944 pragma Assert
(Nkind
(P
) = N_If_Statement
);
2946 -- Statements in CASE statement alternative
2948 elsif Nkind
(P
) = N_Case_Statement_Alternative
then
2950 pragma Assert
(Nkind
(P
) = N_Case_Statement
);
2952 -- Statements in body of block
2954 elsif Nkind
(P
) = N_Handled_Sequence_Of_Statements
2955 and then Nkind
(Parent
(P
)) = N_Block_Statement
2959 -- Statements in exception handler in a block
2961 elsif Nkind
(P
) = N_Exception_Handler
2962 and then Nkind
(Parent
(P
)) = N_Handled_Sequence_Of_Statements
2963 and then Nkind
(Parent
(Parent
(P
))) = N_Block_Statement
2967 -- None of these cases, so return
2973 -- This was one of the cases we are looking for (i.e. the
2974 -- parent construct was IF, CASE or block) so decrement count.
2976 Unblocked_Exit_Count
:= Unblocked_Exit_Count
- 1;
2980 end Check_Unreachable_Code
;
2982 ----------------------
2983 -- Preanalyze_Range --
2984 ----------------------
2986 procedure Preanalyze_Range
(R_Copy
: Node_Id
) is
2987 Save_Analysis
: constant Boolean := Full_Analysis
;
2991 Full_Analysis
:= False;
2992 Expander_Mode_Save_And_Set
(False);
2996 if Nkind
(R_Copy
) in N_Subexpr
2997 and then Is_Overloaded
(R_Copy
)
2999 -- Apply preference rules for range of predefined integer types, or
3000 -- diagnose true ambiguity.
3005 Found
: Entity_Id
:= Empty
;
3008 Get_First_Interp
(R_Copy
, I
, It
);
3009 while Present
(It
.Typ
) loop
3010 if Is_Discrete_Type
(It
.Typ
) then
3014 if Scope
(Found
) = Standard_Standard
then
3017 elsif Scope
(It
.Typ
) = Standard_Standard
then
3021 -- Both of them are user-defined
3024 ("ambiguous bounds in range of iteration", R_Copy
);
3025 Error_Msg_N
("\possible interpretations:", R_Copy
);
3026 Error_Msg_NE
("\\} ", R_Copy
, Found
);
3027 Error_Msg_NE
("\\} ", R_Copy
, It
.Typ
);
3033 Get_Next_Interp
(I
, It
);
3038 -- Subtype mark in iteration scheme
3040 if Is_Entity_Name
(R_Copy
)
3041 and then Is_Type
(Entity
(R_Copy
))
3045 -- Expression in range, or Ada 2012 iterator
3047 elsif Nkind
(R_Copy
) in N_Subexpr
then
3049 Typ
:= Etype
(R_Copy
);
3051 if Is_Discrete_Type
(Typ
) then
3054 -- Check that the resulting object is an iterable container
3056 elsif Present
(Find_Aspect
(Typ
, Aspect_Iterator_Element
))
3057 or else Present
(Find_Aspect
(Typ
, Aspect_Constant_Indexing
))
3058 or else Present
(Find_Aspect
(Typ
, Aspect_Variable_Indexing
))
3062 -- The expression may yield an implicit reference to an iterable
3063 -- container. Insert explicit dereference so that proper type is
3064 -- visible in the loop.
3066 elsif Has_Implicit_Dereference
(Etype
(R_Copy
)) then
3071 Disc
:= First_Discriminant
(Typ
);
3072 while Present
(Disc
) loop
3073 if Has_Implicit_Dereference
(Disc
) then
3074 Build_Explicit_Dereference
(R_Copy
, Disc
);
3078 Next_Discriminant
(Disc
);
3085 Expander_Mode_Restore
;
3086 Full_Analysis
:= Save_Analysis
;
3087 end Preanalyze_Range
;