1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with 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
;
35 with Ghost
; use Ghost
;
37 with Lib
.Xref
; use Lib
.Xref
;
38 with Namet
; use Namet
;
39 with Nlists
; use Nlists
;
40 with Nmake
; use Nmake
;
42 with Restrict
; use Restrict
;
43 with Rident
; use Rident
;
44 with Rtsfind
; use Rtsfind
;
46 with Sem_Aux
; use Sem_Aux
;
47 with Sem_Case
; use Sem_Case
;
48 with Sem_Ch3
; use Sem_Ch3
;
49 with Sem_Ch6
; use Sem_Ch6
;
50 with Sem_Ch8
; use Sem_Ch8
;
51 with Sem_Dim
; use Sem_Dim
;
52 with Sem_Disp
; use Sem_Disp
;
53 with Sem_Elab
; use Sem_Elab
;
54 with Sem_Eval
; use Sem_Eval
;
55 with Sem_Res
; use Sem_Res
;
56 with Sem_Type
; use Sem_Type
;
57 with Sem_Util
; use Sem_Util
;
58 with Sem_Warn
; use Sem_Warn
;
59 with Snames
; use Snames
;
60 with Stand
; use Stand
;
61 with Sinfo
; use Sinfo
;
62 with Targparm
; use Targparm
;
63 with Tbuild
; use Tbuild
;
64 with Uintp
; use Uintp
;
66 package body Sem_Ch5
is
68 Unblocked_Exit_Count
: Nat
:= 0;
69 -- This variable is used when processing if statements, case statements,
70 -- and block statements. It counts the number of exit points that are not
71 -- blocked by unconditional transfer instructions: for IF and CASE, these
72 -- are the branches of the conditional; for a block, they are the statement
73 -- sequence of the block, and the statement sequences of any exception
74 -- handlers that are part of the block. When processing is complete, if
75 -- this count is zero, it means that control cannot fall through the IF,
76 -- CASE or block statement. This is used for the generation of warning
77 -- messages. This variable is recursively saved on entry to processing the
78 -- construct, and restored on exit.
80 procedure Preanalyze_Range
(R_Copy
: Node_Id
);
81 -- Determine expected type of range or domain of iteration of Ada 2012
82 -- loop by analyzing separate copy. Do the analysis and resolution of the
83 -- copy of the bound(s) with expansion disabled, to prevent the generation
84 -- of finalization actions. This prevents memory leaks when the bounds
85 -- contain calls to functions returning controlled arrays or when the
86 -- domain of iteration is a container.
88 ------------------------
89 -- Analyze_Assignment --
90 ------------------------
92 procedure Analyze_Assignment
(N
: Node_Id
) is
93 GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
94 Lhs
: constant Node_Id
:= Name
(N
);
95 Rhs
: constant Node_Id
:= Expression
(N
);
100 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
);
101 -- N is the node for the left hand side of an assignment, and it is not
102 -- a variable. This routine issues an appropriate diagnostic.
105 -- This is called to kill current value settings of a simple variable
106 -- on the left hand side. We call it if we find any error in analyzing
107 -- the assignment, and at the end of processing before setting any new
108 -- current values in place.
110 procedure Restore_Globals
;
111 -- Restore the values of all saved global variables
113 procedure Set_Assignment_Type
115 Opnd_Type
: in out Entity_Id
);
116 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
117 -- nominal subtype. This procedure is used to deal with cases where the
118 -- nominal subtype must be replaced by the actual subtype.
120 -------------------------------
121 -- Diagnose_Non_Variable_Lhs --
122 -------------------------------
124 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
) is
126 -- Not worth posting another error if left hand side already flagged
127 -- as being illegal in some respect.
129 if Error_Posted
(N
) then
132 -- Some special bad cases of entity names
134 elsif Is_Entity_Name
(N
) then
136 Ent
: constant Entity_Id
:= Entity
(N
);
139 if Ekind
(Ent
) = E_In_Parameter
then
141 ("assignment to IN mode parameter not allowed", N
);
144 -- Renamings of protected private components are turned into
145 -- constants when compiling a protected function. In the case
146 -- of single protected types, the private component appears
149 elsif (Is_Prival
(Ent
)
151 (Ekind
(Current_Scope
) = E_Function
152 or else Ekind
(Enclosing_Dynamic_Scope
153 (Current_Scope
)) = E_Function
))
155 (Ekind
(Ent
) = E_Component
156 and then Is_Protected_Type
(Scope
(Ent
)))
159 ("protected function cannot modify protected object", N
);
162 elsif Ekind
(Ent
) = E_Loop_Parameter
then
163 Error_Msg_N
("assignment to loop parameter not allowed", N
);
168 -- For indexed components, test prefix if it is in array. We do not
169 -- want to recurse for cases where the prefix is a pointer, since we
170 -- may get a message confusing the pointer and what it references.
172 elsif Nkind
(N
) = N_Indexed_Component
173 and then Is_Array_Type
(Etype
(Prefix
(N
)))
175 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
178 -- Another special case for assignment to discriminant
180 elsif Nkind
(N
) = N_Selected_Component
then
181 if Present
(Entity
(Selector_Name
(N
)))
182 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
184 Error_Msg_N
("assignment to discriminant not allowed", N
);
187 -- For selection from record, diagnose prefix, but note that again
188 -- we only do this for a record, not e.g. for a pointer.
190 elsif Is_Record_Type
(Etype
(Prefix
(N
))) then
191 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
196 -- If we fall through, we have no special message to issue
198 Error_Msg_N
("left hand side of assignment must be a variable", N
);
199 end Diagnose_Non_Variable_Lhs
;
205 procedure Kill_Lhs
is
207 if Is_Entity_Name
(Lhs
) then
209 Ent
: constant Entity_Id
:= Entity
(Lhs
);
211 if Present
(Ent
) then
212 Kill_Current_Values
(Ent
);
218 ---------------------
219 -- Restore_Globals --
220 ---------------------
222 procedure Restore_Globals
is
227 -------------------------
228 -- Set_Assignment_Type --
229 -------------------------
231 procedure Set_Assignment_Type
233 Opnd_Type
: in out Entity_Id
)
236 Require_Entity
(Opnd
);
238 -- If the assignment operand is an in-out or out parameter, then we
239 -- get the actual subtype (needed for the unconstrained case). If the
240 -- operand is the actual in an entry declaration, then within the
241 -- accept statement it is replaced with a local renaming, which may
242 -- also have an actual subtype.
244 if Is_Entity_Name
(Opnd
)
245 and then (Ekind
(Entity
(Opnd
)) = E_Out_Parameter
246 or else Ekind_In
(Entity
(Opnd
),
248 E_Generic_In_Out_Parameter
)
250 (Ekind
(Entity
(Opnd
)) = E_Variable
251 and then Nkind
(Parent
(Entity
(Opnd
))) =
252 N_Object_Renaming_Declaration
253 and then Nkind
(Parent
(Parent
(Entity
(Opnd
)))) =
256 Opnd_Type
:= Get_Actual_Subtype
(Opnd
);
258 -- If assignment operand is a component reference, then we get the
259 -- actual subtype of the component for the unconstrained case.
261 elsif Nkind_In
(Opnd
, N_Selected_Component
, N_Explicit_Dereference
)
262 and then not Is_Unchecked_Union
(Opnd_Type
)
264 Decl
:= Build_Actual_Subtype_Of_Component
(Opnd_Type
, Opnd
);
266 if Present
(Decl
) then
267 Insert_Action
(N
, Decl
);
268 Mark_Rewrite_Insertion
(Decl
);
270 Opnd_Type
:= Defining_Identifier
(Decl
);
271 Set_Etype
(Opnd
, Opnd_Type
);
272 Freeze_Itype
(Opnd_Type
, N
);
274 elsif Is_Constrained
(Etype
(Opnd
)) then
275 Opnd_Type
:= Etype
(Opnd
);
278 -- For slice, use the constrained subtype created for the slice
280 elsif Nkind
(Opnd
) = N_Slice
then
281 Opnd_Type
:= Etype
(Opnd
);
283 end Set_Assignment_Type
;
285 -- Start of processing for Analyze_Assignment
288 Mark_Coextensions
(N
, Rhs
);
290 -- Analyze the target of the assignment first in case the expression
291 -- contains references to Ghost entities. The checks that verify the
292 -- proper use of a Ghost entity need to know the enclosing context.
296 -- The left hand side of an assignment may reference an entity subject
297 -- to pragma Ghost with policy Ignore. Set the mode now to ensure that
298 -- any nodes generated during analysis and expansion are properly
299 -- flagged as ignored Ghost.
304 -- Ensure that we never do an assignment on a variable marked as
305 -- as Safe_To_Reevaluate.
307 pragma Assert
(not Is_Entity_Name
(Lhs
)
308 or else Ekind
(Entity
(Lhs
)) /= E_Variable
309 or else not Is_Safe_To_Reevaluate
(Entity
(Lhs
)));
311 -- Start type analysis for assignment
315 -- In the most general case, both Lhs and Rhs can be overloaded, and we
316 -- must compute the intersection of the possible types on each side.
318 if Is_Overloaded
(Lhs
) then
325 Get_First_Interp
(Lhs
, I
, It
);
327 while Present
(It
.Typ
) loop
328 if Has_Compatible_Type
(Rhs
, It
.Typ
) then
329 if T1
/= Any_Type
then
331 -- An explicit dereference is overloaded if the prefix
332 -- is. Try to remove the ambiguity on the prefix, the
333 -- error will be posted there if the ambiguity is real.
335 if Nkind
(Lhs
) = N_Explicit_Dereference
then
338 PI1
: Interp_Index
:= 0;
344 Get_First_Interp
(Prefix
(Lhs
), PI
, PIt
);
346 while Present
(PIt
.Typ
) loop
347 if Is_Access_Type
(PIt
.Typ
)
348 and then Has_Compatible_Type
349 (Rhs
, Designated_Type
(PIt
.Typ
))
353 Disambiguate
(Prefix
(Lhs
),
356 if PIt
= No_Interp
then
358 ("ambiguous left-hand side"
359 & " in assignment", Lhs
);
362 Resolve
(Prefix
(Lhs
), PIt
.Typ
);
372 Get_Next_Interp
(PI
, PIt
);
378 ("ambiguous left-hand side in assignment", Lhs
);
386 Get_Next_Interp
(I
, It
);
390 if T1
= Any_Type
then
392 ("no valid types for left-hand side for assignment", Lhs
);
399 -- The resulting assignment type is T1, so now we will resolve the left
400 -- hand side of the assignment using this determined type.
404 -- Cases where Lhs is not a variable
406 if not Is_Variable
(Lhs
) then
408 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
416 if Ada_Version
>= Ada_2005
then
418 -- Handle chains of renamings
421 while Nkind
(Ent
) in N_Has_Entity
422 and then Present
(Entity
(Ent
))
423 and then Present
(Renamed_Object
(Entity
(Ent
)))
425 Ent
:= Renamed_Object
(Entity
(Ent
));
428 if (Nkind
(Ent
) = N_Attribute_Reference
429 and then Attribute_Name
(Ent
) = Name_Priority
)
431 -- Renamings of the attribute Priority applied to protected
432 -- objects have been previously expanded into calls to the
433 -- Get_Ceiling run-time subprogram.
436 (Nkind
(Ent
) = N_Function_Call
437 and then (Entity
(Name
(Ent
)) = RTE
(RE_Get_Ceiling
)
439 Entity
(Name
(Ent
)) = RTE
(RO_PE_Get_Ceiling
)))
441 -- The enclosing subprogram cannot be a protected function
444 while not (Is_Subprogram
(S
)
445 and then Convention
(S
) = Convention_Protected
)
446 and then S
/= Standard_Standard
451 if Ekind
(S
) = E_Function
452 and then Convention
(S
) = Convention_Protected
455 ("protected function cannot modify protected object",
459 -- Changes of the ceiling priority of the protected object
460 -- are only effective if the Ceiling_Locking policy is in
461 -- effect (AARM D.5.2 (5/2)).
463 if Locking_Policy
/= 'C' then
464 Error_Msg_N
("assignment to the attribute PRIORITY has " &
466 Error_Msg_N
("\since no Locking_Policy has been " &
476 Diagnose_Non_Variable_Lhs
(Lhs
);
480 -- Error of assigning to limited type. We do however allow this in
481 -- certain cases where the front end generates the assignments.
483 elsif Is_Limited_Type
(T1
)
484 and then not Assignment_OK
(Lhs
)
485 and then not Assignment_OK
(Original_Node
(Lhs
))
486 and then not Is_Value_Type
(T1
)
488 -- CPP constructors can only be called in declarations
490 if Is_CPP_Constructor_Call
(Rhs
) then
491 Error_Msg_N
("invalid use of 'C'P'P constructor", Rhs
);
494 ("left hand of assignment must not be limited type", Lhs
);
495 Explain_Limited_Type
(T1
, Lhs
);
501 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
502 -- abstract. This is only checked when the assignment Comes_From_Source,
503 -- because in some cases the expander generates such assignments (such
504 -- in the _assign operation for an abstract type).
506 elsif Is_Abstract_Type
(T1
) and then Comes_From_Source
(N
) then
508 ("target of assignment operation must not be abstract", Lhs
);
511 -- Resolution may have updated the subtype, in case the left-hand side
512 -- is a private protected component. Use the correct subtype to avoid
513 -- scoping issues in the back-end.
517 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
518 -- type. For example:
522 -- type Acc is access P.T;
525 -- with Pkg; use Acc;
526 -- procedure Example is
529 -- A.all := B.all; -- ERROR
532 if Nkind
(Lhs
) = N_Explicit_Dereference
533 and then Ekind
(T1
) = E_Incomplete_Type
535 Error_Msg_N
("invalid use of incomplete type", Lhs
);
541 -- Now we can complete the resolution of the right hand side
543 Set_Assignment_Type
(Lhs
, T1
);
546 -- This is the point at which we check for an unset reference
548 Check_Unset_Reference
(Rhs
);
549 Check_Unprotected_Access
(Lhs
, Rhs
);
551 -- Remaining steps are skipped if Rhs was syntactically in error
561 if not Covers
(T1
, T2
) then
562 Wrong_Type
(Rhs
, Etype
(Lhs
));
568 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
569 -- types, use the non-limited view if available
571 if Nkind
(Rhs
) = N_Explicit_Dereference
572 and then Is_Tagged_Type
(T2
)
573 and then Has_Non_Limited_View
(T2
)
575 T2
:= Non_Limited_View
(T2
);
578 Set_Assignment_Type
(Rhs
, T2
);
580 if Total_Errors_Detected
/= 0 then
590 if T1
= Any_Type
or else T2
= Any_Type
then
596 -- If the rhs is class-wide or dynamically tagged, then require the lhs
597 -- to be class-wide. The case where the rhs is a dynamically tagged call
598 -- to a dispatching operation with a controlling access result is
599 -- excluded from this check, since the target has an access type (and
600 -- no tag propagation occurs in that case).
602 if (Is_Class_Wide_Type
(T2
)
603 or else (Is_Dynamically_Tagged
(Rhs
)
604 and then not Is_Access_Type
(T1
)))
605 and then not Is_Class_Wide_Type
(T1
)
607 Error_Msg_N
("dynamically tagged expression not allowed!", Rhs
);
609 elsif Is_Class_Wide_Type
(T1
)
610 and then not Is_Class_Wide_Type
(T2
)
611 and then not Is_Tag_Indeterminate
(Rhs
)
612 and then not Is_Dynamically_Tagged
(Rhs
)
614 Error_Msg_N
("dynamically tagged expression required!", Rhs
);
617 -- Propagate the tag from a class-wide target to the rhs when the rhs
618 -- is a tag-indeterminate call.
620 if Is_Tag_Indeterminate
(Rhs
) then
621 if Is_Class_Wide_Type
(T1
) then
622 Propagate_Tag
(Lhs
, Rhs
);
624 elsif Nkind
(Rhs
) = N_Function_Call
625 and then Is_Entity_Name
(Name
(Rhs
))
626 and then Is_Abstract_Subprogram
(Entity
(Name
(Rhs
)))
629 ("call to abstract function must be dispatching", Name
(Rhs
));
631 elsif Nkind
(Rhs
) = N_Qualified_Expression
632 and then Nkind
(Expression
(Rhs
)) = N_Function_Call
633 and then Is_Entity_Name
(Name
(Expression
(Rhs
)))
635 Is_Abstract_Subprogram
(Entity
(Name
(Expression
(Rhs
))))
638 ("call to abstract function must be dispatching",
639 Name
(Expression
(Rhs
)));
643 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
644 -- apply an implicit conversion of the rhs to that type to force
645 -- appropriate static and run-time accessibility checks. This applies
646 -- as well to anonymous access-to-subprogram types that are component
647 -- subtypes or formal parameters.
649 if Ada_Version
>= Ada_2005
and then Is_Access_Type
(T1
) then
650 if Is_Local_Anonymous_Access
(T1
)
651 or else Ekind
(T2
) = E_Anonymous_Access_Subprogram_Type
653 -- Handle assignment to an Ada 2012 stand-alone object
654 -- of an anonymous access type.
656 or else (Ekind
(T1
) = E_Anonymous_Access_Type
657 and then Nkind
(Associated_Node_For_Itype
(T1
)) =
658 N_Object_Declaration
)
661 Rewrite
(Rhs
, Convert_To
(T1
, Relocate_Node
(Rhs
)));
662 Analyze_And_Resolve
(Rhs
, T1
);
666 -- Ada 2005 (AI-231): Assignment to not null variable
668 if Ada_Version
>= Ada_2005
669 and then Can_Never_Be_Null
(T1
)
670 and then not Assignment_OK
(Lhs
)
672 -- Case where we know the right hand side is null
674 if Known_Null
(Rhs
) then
675 Apply_Compile_Time_Constraint_Error
678 "(Ada 2005) null not allowed in null-excluding objects??",
679 Reason
=> CE_Null_Not_Allowed
);
681 -- We still mark this as a possible modification, that's necessary
682 -- to reset Is_True_Constant, and desirable for xref purposes.
684 Note_Possible_Modification
(Lhs
, Sure
=> True);
688 -- If we know the right hand side is non-null, then we convert to the
689 -- target type, since we don't need a run time check in that case.
691 elsif not Can_Never_Be_Null
(T2
) then
692 Rewrite
(Rhs
, Convert_To
(T1
, Relocate_Node
(Rhs
)));
693 Analyze_And_Resolve
(Rhs
, T1
);
697 if Is_Scalar_Type
(T1
) then
698 Apply_Scalar_Range_Check
(Rhs
, Etype
(Lhs
));
700 -- For array types, verify that lengths match. If the right hand side
701 -- is a function call that has been inlined, the assignment has been
702 -- rewritten as a block, and the constraint check will be applied to the
703 -- assignment within the block.
705 elsif Is_Array_Type
(T1
)
706 and then (Nkind
(Rhs
) /= N_Type_Conversion
707 or else Is_Constrained
(Etype
(Rhs
)))
708 and then (Nkind
(Rhs
) /= N_Function_Call
709 or else Nkind
(N
) /= N_Block_Statement
)
711 -- Assignment verifies that the length of the Lsh and Rhs are equal,
712 -- but of course the indexes do not have to match. If the right-hand
713 -- side is a type conversion to an unconstrained type, a length check
714 -- is performed on the expression itself during expansion. In rare
715 -- cases, the redundant length check is computed on an index type
716 -- with a different representation, triggering incorrect code in the
719 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
722 -- Discriminant checks are applied in the course of expansion
727 -- Note: modifications of the Lhs may only be recorded after
728 -- checks have been applied.
730 Note_Possible_Modification
(Lhs
, Sure
=> True);
732 -- ??? a real accessibility check is needed when ???
734 -- Post warning for redundant assignment or variable to itself
736 if Warn_On_Redundant_Constructs
738 -- We only warn for source constructs
740 and then Comes_From_Source
(N
)
742 -- Where the object is the same on both sides
744 and then Same_Object
(Lhs
, Original_Node
(Rhs
))
746 -- But exclude the case where the right side was an operation that
747 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
748 -- don't want to warn in such a case, since it is reasonable to write
749 -- such expressions especially when K is defined symbolically in some
752 and then Nkind
(Original_Node
(Rhs
)) not in N_Op
754 if Nkind
(Lhs
) in N_Has_Entity
then
755 Error_Msg_NE
-- CODEFIX
756 ("?r?useless assignment of & to itself!", N
, Entity
(Lhs
));
758 Error_Msg_N
-- CODEFIX
759 ("?r?useless assignment of object to itself!", N
);
763 -- Check for non-allowed composite assignment
765 if not Support_Composite_Assign_On_Target
766 and then (Is_Array_Type
(T1
) or else Is_Record_Type
(T1
))
767 and then (not Has_Size_Clause
(T1
) or else Esize
(T1
) > 64)
769 Error_Msg_CRT
("composite assignment", N
);
772 -- Check elaboration warning for left side if not in elab code
774 if not In_Subprogram_Or_Concurrent_Unit
then
775 Check_Elab_Assign
(Lhs
);
778 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
779 -- assignment is a source assignment in the extended main source unit.
780 -- We are not interested in any reference information outside this
781 -- context, or in compiler generated assignment statements.
783 if Comes_From_Source
(N
)
784 and then In_Extended_Main_Source_Unit
(Lhs
)
786 Set_Referenced_Modified
(Lhs
, Out_Param
=> False);
789 -- RM 7.3.2 (12/3) An assignment to a view conversion (from a type
790 -- to one of its ancestors) requires an invariant check. Apply check
791 -- only if expression comes from source, otherwise it will be applied
792 -- when value is assigned to source entity.
794 if Nkind
(Lhs
) = N_Type_Conversion
795 and then Has_Invariants
(Etype
(Expression
(Lhs
)))
796 and then Comes_From_Source
(Expression
(Lhs
))
798 Insert_After
(N
, Make_Invariant_Call
(Expression
(Lhs
)));
801 -- Final step. If left side is an entity, then we may be able to reset
802 -- the current tracked values to new safe values. We only have something
803 -- to do if the left side is an entity name, and expansion has not
804 -- modified the node into something other than an assignment, and of
805 -- course we only capture values if it is safe to do so.
807 if Is_Entity_Name
(Lhs
)
808 and then Nkind
(N
) = N_Assignment_Statement
811 Ent
: constant Entity_Id
:= Entity
(Lhs
);
814 if Safe_To_Capture_Value
(N
, Ent
) then
816 -- If simple variable on left side, warn if this assignment
817 -- blots out another one (rendering it useless). We only do
818 -- this for source assignments, otherwise we can generate bogus
819 -- warnings when an assignment is rewritten as another
820 -- assignment, and gets tied up with itself.
822 if Warn_On_Modified_Unread
823 and then Is_Assignable
(Ent
)
824 and then Comes_From_Source
(N
)
825 and then In_Extended_Main_Source_Unit
(Ent
)
827 Warn_On_Useless_Assignment
(Ent
, N
);
830 -- If we are assigning an access type and the left side is an
831 -- entity, then make sure that the Is_Known_[Non_]Null flags
832 -- properly reflect the state of the entity after assignment.
834 if Is_Access_Type
(T1
) then
835 if Known_Non_Null
(Rhs
) then
836 Set_Is_Known_Non_Null
(Ent
, True);
838 elsif Known_Null
(Rhs
)
839 and then not Can_Never_Be_Null
(Ent
)
841 Set_Is_Known_Null
(Ent
, True);
844 Set_Is_Known_Null
(Ent
, False);
846 if not Can_Never_Be_Null
(Ent
) then
847 Set_Is_Known_Non_Null
(Ent
, False);
851 -- For discrete types, we may be able to set the current value
852 -- if the value is known at compile time.
854 elsif Is_Discrete_Type
(T1
)
855 and then Compile_Time_Known_Value
(Rhs
)
857 Set_Current_Value
(Ent
, Rhs
);
859 Set_Current_Value
(Ent
, Empty
);
862 -- If not safe to capture values, kill them
870 -- If assigning to an object in whole or in part, note location of
871 -- assignment in case no one references value. We only do this for
872 -- source assignments, otherwise we can generate bogus warnings when an
873 -- assignment is rewritten as another assignment, and gets tied up with
877 Ent
: constant Entity_Id
:= Get_Enclosing_Object
(Lhs
);
880 and then Safe_To_Capture_Value
(N
, Ent
)
881 and then Nkind
(N
) = N_Assignment_Statement
882 and then Warn_On_Modified_Unread
883 and then Is_Assignable
(Ent
)
884 and then Comes_From_Source
(N
)
885 and then In_Extended_Main_Source_Unit
(Ent
)
887 Set_Last_Assignment
(Ent
, Lhs
);
891 Analyze_Dimension
(N
);
893 end Analyze_Assignment
;
895 -----------------------------
896 -- Analyze_Block_Statement --
897 -----------------------------
899 procedure Analyze_Block_Statement
(N
: Node_Id
) is
900 procedure Install_Return_Entities
(Scop
: Entity_Id
);
901 -- Install all entities of return statement scope Scop in the visibility
902 -- chain except for the return object since its entity is reused in a
905 -----------------------------
906 -- Install_Return_Entities --
907 -----------------------------
909 procedure Install_Return_Entities
(Scop
: Entity_Id
) is
913 Id
:= First_Entity
(Scop
);
914 while Present
(Id
) loop
916 -- Do not install the return object
918 if not Ekind_In
(Id
, E_Constant
, E_Variable
)
919 or else not Is_Return_Object
(Id
)
926 end Install_Return_Entities
;
928 -- Local constants and variables
930 Decls
: constant List_Id
:= Declarations
(N
);
931 Id
: constant Node_Id
:= Identifier
(N
);
932 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
934 Is_BIP_Return_Statement
: Boolean;
936 -- Start of processing for Analyze_Block_Statement
939 -- In SPARK mode, we reject block statements. Note that the case of
940 -- block statements generated by the expander is fine.
942 if Nkind
(Original_Node
(N
)) = N_Block_Statement
then
943 Check_SPARK_05_Restriction
("block statement is not allowed", N
);
946 -- If no handled statement sequence is present, things are really messed
947 -- up, and we just return immediately (defence against previous errors).
950 Check_Error_Detected
;
954 -- Detect whether the block is actually a rewritten return statement of
955 -- a build-in-place function.
957 Is_BIP_Return_Statement
:=
959 and then Present
(Entity
(Id
))
960 and then Ekind
(Entity
(Id
)) = E_Return_Statement
961 and then Is_Build_In_Place_Function
962 (Return_Applies_To
(Entity
(Id
)));
964 -- Normal processing with HSS present
967 EH
: constant List_Id
:= Exception_Handlers
(HSS
);
968 Ent
: Entity_Id
:= Empty
;
971 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
972 -- Recursively save value of this global, will be restored on exit
975 -- Initialize unblocked exit count for statements of begin block
976 -- plus one for each exception handler that is present.
978 Unblocked_Exit_Count
:= 1;
981 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ List_Length
(EH
);
984 -- If a label is present analyze it and mark it as referenced
990 -- An error defense. If we have an identifier, but no entity, then
991 -- something is wrong. If previous errors, then just remove the
992 -- identifier and continue, otherwise raise an exception.
995 Check_Error_Detected
;
996 Set_Identifier
(N
, Empty
);
999 Set_Ekind
(Ent
, E_Block
);
1000 Generate_Reference
(Ent
, N
, ' ');
1001 Generate_Definition
(Ent
);
1003 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
1004 Set_Label_Construct
(Parent
(Ent
), N
);
1009 -- If no entity set, create a label entity
1012 Ent
:= New_Internal_Entity
(E_Block
, Current_Scope
, Sloc
(N
), 'B');
1013 Set_Identifier
(N
, New_Occurrence_Of
(Ent
, Sloc
(N
)));
1014 Set_Parent
(Ent
, N
);
1017 Set_Etype
(Ent
, Standard_Void_Type
);
1018 Set_Block_Node
(Ent
, Identifier
(N
));
1021 -- The block served as an extended return statement. Ensure that any
1022 -- entities created during the analysis and expansion of the return
1023 -- object declaration are once again visible.
1025 if Is_BIP_Return_Statement
then
1026 Install_Return_Entities
(Ent
);
1029 if Present
(Decls
) then
1030 Analyze_Declarations
(Decls
);
1032 Inspect_Deferred_Constant_Completion
(Decls
);
1036 Process_End_Label
(HSS
, 'e', Ent
);
1038 -- If exception handlers are present, then we indicate that enclosing
1039 -- scopes contain a block with handlers. We only need to mark non-
1042 if Present
(EH
) then
1045 Set_Has_Nested_Block_With_Handler
(S
);
1046 exit when Is_Overloadable
(S
)
1047 or else Ekind
(S
) = E_Package
1048 or else Is_Generic_Unit
(S
);
1053 Check_References
(Ent
);
1054 Warn_On_Useless_Assignments
(Ent
);
1057 if Unblocked_Exit_Count
= 0 then
1058 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1059 Check_Unreachable_Code
(N
);
1061 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1064 end Analyze_Block_Statement
;
1066 --------------------------------
1067 -- Analyze_Compound_Statement --
1068 --------------------------------
1070 procedure Analyze_Compound_Statement
(N
: Node_Id
) is
1072 Analyze_List
(Actions
(N
));
1073 end Analyze_Compound_Statement
;
1075 ----------------------------
1076 -- Analyze_Case_Statement --
1077 ----------------------------
1079 procedure Analyze_Case_Statement
(N
: Node_Id
) is
1081 Exp_Type
: Entity_Id
;
1082 Exp_Btype
: Entity_Id
;
1085 Others_Present
: Boolean;
1086 -- Indicates if Others was present
1088 pragma Warnings
(Off
, Last_Choice
);
1089 -- Don't care about assigned value
1091 Statements_Analyzed
: Boolean := False;
1092 -- Set True if at least some statement sequences get analyzed. If False
1093 -- on exit, means we had a serious error that prevented full analysis of
1094 -- the case statement, and as a result it is not a good idea to output
1095 -- warning messages about unreachable code.
1097 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1098 -- Recursively save value of this global, will be restored on exit
1100 procedure Non_Static_Choice_Error
(Choice
: Node_Id
);
1101 -- Error routine invoked by the generic instantiation below when the
1102 -- case statement has a non static choice.
1104 procedure Process_Statements
(Alternative
: Node_Id
);
1105 -- Analyzes the statements associated with a case alternative. Needed
1106 -- by instantiation below.
1108 package Analyze_Case_Choices
is new
1109 Generic_Analyze_Choices
1110 (Process_Associated_Node
=> Process_Statements
);
1111 use Analyze_Case_Choices
;
1112 -- Instantiation of the generic choice analysis package
1114 package Check_Case_Choices
is new
1115 Generic_Check_Choices
1116 (Process_Empty_Choice
=> No_OP
,
1117 Process_Non_Static_Choice
=> Non_Static_Choice_Error
,
1118 Process_Associated_Node
=> No_OP
);
1119 use Check_Case_Choices
;
1120 -- Instantiation of the generic choice processing package
1122 -----------------------------
1123 -- Non_Static_Choice_Error --
1124 -----------------------------
1126 procedure Non_Static_Choice_Error
(Choice
: Node_Id
) is
1128 Flag_Non_Static_Expr
1129 ("choice given in case statement is not static!", Choice
);
1130 end Non_Static_Choice_Error
;
1132 ------------------------
1133 -- Process_Statements --
1134 ------------------------
1136 procedure Process_Statements
(Alternative
: Node_Id
) is
1137 Choices
: constant List_Id
:= Discrete_Choices
(Alternative
);
1141 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
1142 Statements_Analyzed
:= True;
1144 -- An interesting optimization. If the case statement expression
1145 -- is a simple entity, then we can set the current value within an
1146 -- alternative if the alternative has one possible value.
1150 -- when 2 | 3 => beta
1151 -- when others => gamma
1153 -- Here we know that N is initially 1 within alpha, but for beta and
1154 -- gamma, we do not know anything more about the initial value.
1156 if Is_Entity_Name
(Exp
) then
1157 Ent
:= Entity
(Exp
);
1159 if Ekind_In
(Ent
, E_Variable
,
1163 if List_Length
(Choices
) = 1
1164 and then Nkind
(First
(Choices
)) in N_Subexpr
1165 and then Compile_Time_Known_Value
(First
(Choices
))
1167 Set_Current_Value
(Entity
(Exp
), First
(Choices
));
1170 Analyze_Statements
(Statements
(Alternative
));
1172 -- After analyzing the case, set the current value to empty
1173 -- since we won't know what it is for the next alternative
1174 -- (unless reset by this same circuit), or after the case.
1176 Set_Current_Value
(Entity
(Exp
), Empty
);
1181 -- Case where expression is not an entity name of a variable
1183 Analyze_Statements
(Statements
(Alternative
));
1184 end Process_Statements
;
1186 -- Start of processing for Analyze_Case_Statement
1189 Unblocked_Exit_Count
:= 0;
1190 Exp
:= Expression
(N
);
1193 -- The expression must be of any discrete type. In rare cases, the
1194 -- expander constructs a case statement whose expression has a private
1195 -- type whose full view is discrete. This can happen when generating
1196 -- a stream operation for a variant type after the type is frozen,
1197 -- when the partial of view of the type of the discriminant is private.
1198 -- In that case, use the full view to analyze case alternatives.
1200 if not Is_Overloaded
(Exp
)
1201 and then not Comes_From_Source
(N
)
1202 and then Is_Private_Type
(Etype
(Exp
))
1203 and then Present
(Full_View
(Etype
(Exp
)))
1204 and then Is_Discrete_Type
(Full_View
(Etype
(Exp
)))
1206 Resolve
(Exp
, Etype
(Exp
));
1207 Exp_Type
:= Full_View
(Etype
(Exp
));
1210 Analyze_And_Resolve
(Exp
, Any_Discrete
);
1211 Exp_Type
:= Etype
(Exp
);
1214 Check_Unset_Reference
(Exp
);
1215 Exp_Btype
:= Base_Type
(Exp_Type
);
1217 -- The expression must be of a discrete type which must be determinable
1218 -- independently of the context in which the expression occurs, but
1219 -- using the fact that the expression must be of a discrete type.
1220 -- Moreover, the type this expression must not be a character literal
1221 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1223 -- If error already reported by Resolve, nothing more to do
1225 if Exp_Btype
= Any_Discrete
or else Exp_Btype
= Any_Type
then
1228 elsif Exp_Btype
= Any_Character
then
1230 ("character literal as case expression is ambiguous", Exp
);
1233 elsif Ada_Version
= Ada_83
1234 and then (Is_Generic_Type
(Exp_Btype
)
1235 or else Is_Generic_Type
(Root_Type
(Exp_Btype
)))
1238 ("(Ada 83) case expression cannot be of a generic type", Exp
);
1242 -- If the case expression is a formal object of mode in out, then treat
1243 -- it as having a nonstatic subtype by forcing use of the base type
1244 -- (which has to get passed to Check_Case_Choices below). Also use base
1245 -- type when the case expression is parenthesized.
1247 if Paren_Count
(Exp
) > 0
1248 or else (Is_Entity_Name
(Exp
)
1249 and then Ekind
(Entity
(Exp
)) = E_Generic_In_Out_Parameter
)
1251 Exp_Type
:= Exp_Btype
;
1254 -- Call instantiated procedures to analyzwe and check discrete choices
1256 Analyze_Choices
(Alternatives
(N
), Exp_Type
);
1257 Check_Choices
(N
, Alternatives
(N
), Exp_Type
, Others_Present
);
1259 -- Case statement with single OTHERS alternative not allowed in SPARK
1261 if Others_Present
and then List_Length
(Alternatives
(N
)) = 1 then
1262 Check_SPARK_05_Restriction
1263 ("OTHERS as unique case alternative is not allowed", N
);
1266 if Exp_Type
= Universal_Integer
and then not Others_Present
then
1267 Error_Msg_N
("case on universal integer requires OTHERS choice", Exp
);
1270 -- If all our exits were blocked by unconditional transfers of control,
1271 -- then the entire CASE statement acts as an unconditional transfer of
1272 -- control, so treat it like one, and check unreachable code. Skip this
1273 -- test if we had serious errors preventing any statement analysis.
1275 if Unblocked_Exit_Count
= 0 and then Statements_Analyzed
then
1276 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1277 Check_Unreachable_Code
(N
);
1279 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1282 -- If the expander is active it will detect the case of a statically
1283 -- determined single alternative and remove warnings for the case, but
1284 -- if we are not doing expansion, that circuit won't be active. Here we
1285 -- duplicate the effect of removing warnings in the same way, so that
1286 -- we will get the same set of warnings in -gnatc mode.
1288 if not Expander_Active
1289 and then Compile_Time_Known_Value
(Expression
(N
))
1290 and then Serious_Errors_Detected
= 0
1293 Chosen
: constant Node_Id
:= Find_Static_Alternative
(N
);
1297 Alt
:= First
(Alternatives
(N
));
1298 while Present
(Alt
) loop
1299 if Alt
/= Chosen
then
1300 Remove_Warning_Messages
(Statements
(Alt
));
1307 end Analyze_Case_Statement
;
1309 ----------------------------
1310 -- Analyze_Exit_Statement --
1311 ----------------------------
1313 -- If the exit includes a name, it must be the name of a currently open
1314 -- loop. Otherwise there must be an innermost open loop on the stack, to
1315 -- which the statement implicitly refers.
1317 -- Additionally, in SPARK mode:
1319 -- The exit can only name the closest enclosing loop;
1321 -- An exit with a when clause must be directly contained in a loop;
1323 -- An exit without a when clause must be directly contained in an
1324 -- if-statement with no elsif or else, which is itself directly contained
1325 -- in a loop. The exit must be the last statement in the if-statement.
1327 procedure Analyze_Exit_Statement
(N
: Node_Id
) is
1328 Target
: constant Node_Id
:= Name
(N
);
1329 Cond
: constant Node_Id
:= Condition
(N
);
1330 Scope_Id
: Entity_Id
;
1336 Check_Unreachable_Code
(N
);
1339 if Present
(Target
) then
1341 U_Name
:= Entity
(Target
);
1343 if not In_Open_Scopes
(U_Name
) or else Ekind
(U_Name
) /= E_Loop
then
1344 Error_Msg_N
("invalid loop name in exit statement", N
);
1348 if Has_Loop_In_Inner_Open_Scopes
(U_Name
) then
1349 Check_SPARK_05_Restriction
1350 ("exit label must name the closest enclosing loop", N
);
1353 Set_Has_Exit
(U_Name
);
1360 for J
in reverse 0 .. Scope_Stack
.Last
loop
1361 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
1362 Kind
:= Ekind
(Scope_Id
);
1364 if Kind
= E_Loop
and then (No
(Target
) or else Scope_Id
= U_Name
) then
1365 Set_Has_Exit
(Scope_Id
);
1368 elsif Kind
= E_Block
1369 or else Kind
= E_Loop
1370 or else Kind
= E_Return_Statement
1376 ("cannot exit from program unit or accept statement", N
);
1381 -- Verify that if present the condition is a Boolean expression
1383 if Present
(Cond
) then
1384 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1385 Check_Unset_Reference
(Cond
);
1388 -- In SPARK mode, verify that the exit statement respects the SPARK
1391 if Present
(Cond
) then
1392 if Nkind
(Parent
(N
)) /= N_Loop_Statement
then
1393 Check_SPARK_05_Restriction
1394 ("exit with when clause must be directly in loop", N
);
1398 if Nkind
(Parent
(N
)) /= N_If_Statement
then
1399 if Nkind
(Parent
(N
)) = N_Elsif_Part
then
1400 Check_SPARK_05_Restriction
1401 ("exit must be in IF without ELSIF", N
);
1403 Check_SPARK_05_Restriction
("exit must be directly in IF", N
);
1406 elsif Nkind
(Parent
(Parent
(N
))) /= N_Loop_Statement
then
1407 Check_SPARK_05_Restriction
1408 ("exit must be in IF directly in loop", N
);
1410 -- First test the presence of ELSE, so that an exit in an ELSE leads
1411 -- to an error mentioning the ELSE.
1413 elsif Present
(Else_Statements
(Parent
(N
))) then
1414 Check_SPARK_05_Restriction
("exit must be in IF without ELSE", N
);
1416 -- An exit in an ELSIF does not reach here, as it would have been
1417 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1419 elsif Present
(Elsif_Parts
(Parent
(N
))) then
1420 Check_SPARK_05_Restriction
("exit must be in IF without ELSIF", N
);
1424 -- Chain exit statement to associated loop entity
1426 Set_Next_Exit_Statement
(N
, First_Exit_Statement
(Scope_Id
));
1427 Set_First_Exit_Statement
(Scope_Id
, N
);
1429 -- Since the exit may take us out of a loop, any previous assignment
1430 -- statement is not useless, so clear last assignment indications. It
1431 -- is OK to keep other current values, since if the exit statement
1432 -- does not exit, then the current values are still valid.
1434 Kill_Current_Values
(Last_Assignment_Only
=> True);
1435 end Analyze_Exit_Statement
;
1437 ----------------------------
1438 -- Analyze_Goto_Statement --
1439 ----------------------------
1441 procedure Analyze_Goto_Statement
(N
: Node_Id
) is
1442 Label
: constant Node_Id
:= Name
(N
);
1443 Scope_Id
: Entity_Id
;
1444 Label_Scope
: Entity_Id
;
1445 Label_Ent
: Entity_Id
;
1448 Check_SPARK_05_Restriction
("goto statement is not allowed", N
);
1450 -- Actual semantic checks
1452 Check_Unreachable_Code
(N
);
1453 Kill_Current_Values
(Last_Assignment_Only
=> True);
1456 Label_Ent
:= Entity
(Label
);
1458 -- Ignore previous error
1460 if Label_Ent
= Any_Id
then
1461 Check_Error_Detected
;
1464 -- We just have a label as the target of a goto
1466 elsif Ekind
(Label_Ent
) /= E_Label
then
1467 Error_Msg_N
("target of goto statement must be a label", Label
);
1470 -- Check that the target of the goto is reachable according to Ada
1471 -- scoping rules. Note: the special gotos we generate for optimizing
1472 -- local handling of exceptions would violate these rules, but we mark
1473 -- such gotos as analyzed when built, so this code is never entered.
1475 elsif not Reachable
(Label_Ent
) then
1476 Error_Msg_N
("target of goto statement is not reachable", Label
);
1480 -- Here if goto passes initial validity checks
1482 Label_Scope
:= Enclosing_Scope
(Label_Ent
);
1484 for J
in reverse 0 .. Scope_Stack
.Last
loop
1485 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
1487 if Label_Scope
= Scope_Id
1488 or else not Ekind_In
(Scope_Id
, E_Block
, E_Loop
, E_Return_Statement
)
1490 if Scope_Id
/= Label_Scope
then
1492 ("cannot exit from program unit or accept statement", N
);
1499 raise Program_Error
;
1500 end Analyze_Goto_Statement
;
1502 --------------------------
1503 -- Analyze_If_Statement --
1504 --------------------------
1506 -- A special complication arises in the analysis of if statements
1508 -- The expander has circuitry to completely delete code that it can tell
1509 -- will not be executed (as a result of compile time known conditions). In
1510 -- the analyzer, we ensure that code that will be deleted in this manner
1511 -- is analyzed but not expanded. This is obviously more efficient, but
1512 -- more significantly, difficulties arise if code is expanded and then
1513 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1514 -- generated in deleted code must be frozen from start, because the nodes
1515 -- on which they depend will not be available at the freeze point.
1517 procedure Analyze_If_Statement
(N
: Node_Id
) is
1520 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1521 -- Recursively save value of this global, will be restored on exit
1523 Save_In_Deleted_Code
: Boolean;
1525 Del
: Boolean := False;
1526 -- This flag gets set True if a True condition has been found, which
1527 -- means that remaining ELSE/ELSIF parts are deleted.
1529 procedure Analyze_Cond_Then
(Cnode
: Node_Id
);
1530 -- This is applied to either the N_If_Statement node itself or to an
1531 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1532 -- statements associated with it.
1534 -----------------------
1535 -- Analyze_Cond_Then --
1536 -----------------------
1538 procedure Analyze_Cond_Then
(Cnode
: Node_Id
) is
1539 Cond
: constant Node_Id
:= Condition
(Cnode
);
1540 Tstm
: constant List_Id
:= Then_Statements
(Cnode
);
1543 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
1544 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1545 Check_Unset_Reference
(Cond
);
1546 Set_Current_Value_Condition
(Cnode
);
1548 -- If already deleting, then just analyze then statements
1551 Analyze_Statements
(Tstm
);
1553 -- Compile time known value, not deleting yet
1555 elsif Compile_Time_Known_Value
(Cond
) then
1556 Save_In_Deleted_Code
:= In_Deleted_Code
;
1558 -- If condition is True, then analyze the THEN statements and set
1559 -- no expansion for ELSE and ELSIF parts.
1561 if Is_True
(Expr_Value
(Cond
)) then
1562 Analyze_Statements
(Tstm
);
1564 Expander_Mode_Save_And_Set
(False);
1565 In_Deleted_Code
:= True;
1567 -- If condition is False, analyze THEN with expansion off
1569 else -- Is_False (Expr_Value (Cond))
1570 Expander_Mode_Save_And_Set
(False);
1571 In_Deleted_Code
:= True;
1572 Analyze_Statements
(Tstm
);
1573 Expander_Mode_Restore
;
1574 In_Deleted_Code
:= Save_In_Deleted_Code
;
1577 -- Not known at compile time, not deleting, normal analysis
1580 Analyze_Statements
(Tstm
);
1582 end Analyze_Cond_Then
;
1584 -- Start of Analyze_If_Statement
1587 -- Initialize exit count for else statements. If there is no else part,
1588 -- this count will stay non-zero reflecting the fact that the uncovered
1589 -- else case is an unblocked exit.
1591 Unblocked_Exit_Count
:= 1;
1592 Analyze_Cond_Then
(N
);
1594 -- Now to analyze the elsif parts if any are present
1596 if Present
(Elsif_Parts
(N
)) then
1597 E
:= First
(Elsif_Parts
(N
));
1598 while Present
(E
) loop
1599 Analyze_Cond_Then
(E
);
1604 if Present
(Else_Statements
(N
)) then
1605 Analyze_Statements
(Else_Statements
(N
));
1608 -- If all our exits were blocked by unconditional transfers of control,
1609 -- then the entire IF statement acts as an unconditional transfer of
1610 -- control, so treat it like one, and check unreachable code.
1612 if Unblocked_Exit_Count
= 0 then
1613 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1614 Check_Unreachable_Code
(N
);
1616 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1620 Expander_Mode_Restore
;
1621 In_Deleted_Code
:= Save_In_Deleted_Code
;
1624 if not Expander_Active
1625 and then Compile_Time_Known_Value
(Condition
(N
))
1626 and then Serious_Errors_Detected
= 0
1628 if Is_True
(Expr_Value
(Condition
(N
))) then
1629 Remove_Warning_Messages
(Else_Statements
(N
));
1631 if Present
(Elsif_Parts
(N
)) then
1632 E
:= First
(Elsif_Parts
(N
));
1633 while Present
(E
) loop
1634 Remove_Warning_Messages
(Then_Statements
(E
));
1640 Remove_Warning_Messages
(Then_Statements
(N
));
1644 -- Warn on redundant if statement that has no effect
1646 -- Note, we could also check empty ELSIF parts ???
1648 if Warn_On_Redundant_Constructs
1650 -- If statement must be from source
1652 and then Comes_From_Source
(N
)
1654 -- Condition must not have obvious side effect
1656 and then Has_No_Obvious_Side_Effects
(Condition
(N
))
1658 -- No elsif parts of else part
1660 and then No
(Elsif_Parts
(N
))
1661 and then No
(Else_Statements
(N
))
1663 -- Then must be a single null statement
1665 and then List_Length
(Then_Statements
(N
)) = 1
1667 -- Go to original node, since we may have rewritten something as
1668 -- a null statement (e.g. a case we could figure the outcome of).
1671 T
: constant Node_Id
:= First
(Then_Statements
(N
));
1672 S
: constant Node_Id
:= Original_Node
(T
);
1675 if Comes_From_Source
(S
) and then Nkind
(S
) = N_Null_Statement
then
1676 Error_Msg_N
("if statement has no effect?r?", N
);
1680 end Analyze_If_Statement
;
1682 ----------------------------------------
1683 -- Analyze_Implicit_Label_Declaration --
1684 ----------------------------------------
1686 -- An implicit label declaration is generated in the innermost enclosing
1687 -- declarative part. This is done for labels, and block and loop names.
1689 -- Note: any changes in this routine may need to be reflected in
1690 -- Analyze_Label_Entity.
1692 procedure Analyze_Implicit_Label_Declaration
(N
: Node_Id
) is
1693 Id
: constant Node_Id
:= Defining_Identifier
(N
);
1696 Set_Ekind
(Id
, E_Label
);
1697 Set_Etype
(Id
, Standard_Void_Type
);
1698 Set_Enclosing_Scope
(Id
, Current_Scope
);
1699 end Analyze_Implicit_Label_Declaration
;
1701 ------------------------------
1702 -- Analyze_Iteration_Scheme --
1703 ------------------------------
1705 procedure Analyze_Iteration_Scheme
(N
: Node_Id
) is
1707 Iter_Spec
: Node_Id
;
1708 Loop_Spec
: Node_Id
;
1711 -- For an infinite loop, there is no iteration scheme
1717 Cond
:= Condition
(N
);
1718 Iter_Spec
:= Iterator_Specification
(N
);
1719 Loop_Spec
:= Loop_Parameter_Specification
(N
);
1721 if Present
(Cond
) then
1722 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1723 Check_Unset_Reference
(Cond
);
1724 Set_Current_Value_Condition
(N
);
1726 elsif Present
(Iter_Spec
) then
1727 Analyze_Iterator_Specification
(Iter_Spec
);
1730 Analyze_Loop_Parameter_Specification
(Loop_Spec
);
1732 end Analyze_Iteration_Scheme
;
1734 ------------------------------------
1735 -- Analyze_Iterator_Specification --
1736 ------------------------------------
1738 procedure Analyze_Iterator_Specification
(N
: Node_Id
) is
1739 Loc
: constant Source_Ptr
:= Sloc
(N
);
1740 Def_Id
: constant Node_Id
:= Defining_Identifier
(N
);
1741 Subt
: constant Node_Id
:= Subtype_Indication
(N
);
1742 Iter_Name
: constant Node_Id
:= Name
(N
);
1748 procedure Check_Reverse_Iteration
(Typ
: Entity_Id
);
1749 -- For an iteration over a container, if the loop carries the Reverse
1750 -- indicator, verify that the container type has an Iterate aspect that
1751 -- implements the reversible iterator interface.
1753 function Get_Cursor_Type
(Typ
: Entity_Id
) return Entity_Id
;
1754 -- For containers with Iterator and related aspects, the cursor is
1755 -- obtained by locating an entity with the proper name in the scope
1758 -----------------------------
1759 -- Check_Reverse_Iteration --
1760 -----------------------------
1762 procedure Check_Reverse_Iteration
(Typ
: Entity_Id
) is
1764 if Reverse_Present
(N
)
1765 and then not Is_Array_Type
(Typ
)
1766 and then not Is_Reversible_Iterator
(Typ
)
1769 ("container type does not support reverse iteration", N
, Typ
);
1771 end Check_Reverse_Iteration
;
1773 ---------------------
1774 -- Get_Cursor_Type --
1775 ---------------------
1777 function Get_Cursor_Type
(Typ
: Entity_Id
) return Entity_Id
is
1781 Ent
:= First_Entity
(Scope
(Typ
));
1782 while Present
(Ent
) loop
1783 exit when Chars
(Ent
) = Name_Cursor
;
1791 -- The cursor is the target of generated assignments in the
1792 -- loop, and cannot have a limited type.
1794 if Is_Limited_Type
(Etype
(Ent
)) then
1795 Error_Msg_N
("cursor type cannot be limited", N
);
1799 end Get_Cursor_Type
;
1801 -- Start of processing for Analyze_iterator_Specification
1804 Enter_Name
(Def_Id
);
1806 -- AI12-0151 specifies that when the subtype indication is present, it
1807 -- must statically match the type of the array or container element.
1808 -- To simplify this check, we introduce a subtype declaration with the
1809 -- given subtype indication when it carries a constraint, and rewrite
1810 -- the original as a reference to the created subtype entity.
1812 if Present
(Subt
) then
1813 if Nkind
(Subt
) = N_Subtype_Indication
then
1815 S
: constant Entity_Id
:= Make_Temporary
(Sloc
(Subt
), 'S');
1816 Decl
: constant Node_Id
:=
1817 Make_Subtype_Declaration
(Loc
,
1818 Defining_Identifier
=> S
,
1819 Subtype_Indication
=> New_Copy_Tree
(Subt
));
1821 Insert_Before
(Parent
(Parent
(N
)), Decl
);
1823 Rewrite
(Subt
, New_Occurrence_Of
(S
, Sloc
(Subt
)));
1829 -- Save entity of subtype indication for subsequent check
1831 Bas
:= Entity
(Subt
);
1834 Preanalyze_Range
(Iter_Name
);
1836 -- Set the kind of the loop variable, which is not visible within
1837 -- the iterator name.
1839 Set_Ekind
(Def_Id
, E_Variable
);
1841 -- Provide a link between the iterator variable and the container, for
1842 -- subsequent use in cross-reference and modification information.
1844 if Of_Present
(N
) then
1845 Set_Related_Expression
(Def_Id
, Iter_Name
);
1847 -- For a container, the iterator is specified through the aspect
1849 if not Is_Array_Type
(Etype
(Iter_Name
)) then
1851 Iterator
: constant Entity_Id
:=
1852 Find_Value_Of_Aspect
1853 (Etype
(Iter_Name
), Aspect_Default_Iterator
);
1859 if No
(Iterator
) then
1860 null; -- error reported below.
1862 elsif not Is_Overloaded
(Iterator
) then
1863 Check_Reverse_Iteration
(Etype
(Iterator
));
1865 -- If Iterator is overloaded, use reversible iterator if
1866 -- one is available.
1868 elsif Is_Overloaded
(Iterator
) then
1869 Get_First_Interp
(Iterator
, I
, It
);
1870 while Present
(It
.Nam
) loop
1871 if Ekind
(It
.Nam
) = E_Function
1872 and then Is_Reversible_Iterator
(Etype
(It
.Nam
))
1874 Set_Etype
(Iterator
, It
.Typ
);
1875 Set_Entity
(Iterator
, It
.Nam
);
1879 Get_Next_Interp
(I
, It
);
1882 Check_Reverse_Iteration
(Etype
(Iterator
));
1888 -- If the domain of iteration is an expression, create a declaration for
1889 -- it, so that finalization actions are introduced outside of the loop.
1890 -- The declaration must be a renaming because the body of the loop may
1891 -- assign to elements.
1893 if not Is_Entity_Name
(Iter_Name
)
1895 -- When the context is a quantified expression, the renaming
1896 -- declaration is delayed until the expansion phase if we are
1899 and then (Nkind
(Parent
(N
)) /= N_Quantified_Expression
1900 or else Operating_Mode
= Check_Semantics
)
1902 -- Do not perform this expansion in SPARK mode, since the formal
1903 -- verification directly deals with the source form of the iterator.
1904 -- Ditto for ASIS, where the temporary may hide the transformation
1905 -- of a selected component into a prefixed function call.
1907 and then not GNATprove_Mode
1908 and then not ASIS_Mode
1911 Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R', Iter_Name
);
1917 -- If the domain of iteration is an array component that depends
1918 -- on a discriminant, create actual subtype for it. Pre-analysis
1919 -- does not generate the actual subtype of a selected component.
1921 if Nkind
(Iter_Name
) = N_Selected_Component
1922 and then Is_Array_Type
(Etype
(Iter_Name
))
1925 Build_Actual_Subtype_Of_Component
1926 (Etype
(Selector_Name
(Iter_Name
)), Iter_Name
);
1927 Insert_Action
(N
, Act_S
);
1929 if Present
(Act_S
) then
1930 Typ
:= Defining_Identifier
(Act_S
);
1932 Typ
:= Etype
(Iter_Name
);
1936 Typ
:= Etype
(Iter_Name
);
1938 -- Verify that the expression produces an iterator
1940 if not Of_Present
(N
) and then not Is_Iterator
(Typ
)
1941 and then not Is_Array_Type
(Typ
)
1942 and then No
(Find_Aspect
(Typ
, Aspect_Iterable
))
1945 ("expect object that implements iterator interface",
1950 -- Protect against malformed iterator
1952 if Typ
= Any_Type
then
1953 Error_Msg_N
("invalid expression in loop iterator", Iter_Name
);
1957 if not Of_Present
(N
) then
1958 Check_Reverse_Iteration
(Typ
);
1961 -- The name in the renaming declaration may be a function call.
1962 -- Indicate that it does not come from source, to suppress
1963 -- spurious warnings on renamings of parameterless functions,
1964 -- a common enough idiom in user-defined iterators.
1967 Make_Object_Renaming_Declaration
(Loc
,
1968 Defining_Identifier
=> Id
,
1969 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
1971 New_Copy_Tree
(Iter_Name
, New_Sloc
=> Loc
));
1973 Insert_Actions
(Parent
(Parent
(N
)), New_List
(Decl
));
1974 Rewrite
(Name
(N
), New_Occurrence_Of
(Id
, Loc
));
1975 Set_Etype
(Id
, Typ
);
1976 Set_Etype
(Name
(N
), Typ
);
1979 -- Container is an entity or an array with uncontrolled components, or
1980 -- else it is a container iterator given by a function call, typically
1981 -- called Iterate in the case of predefined containers, even though
1982 -- Iterate is not a reserved name. What matters is that the return type
1983 -- of the function is an iterator type.
1985 elsif Is_Entity_Name
(Iter_Name
) then
1986 Analyze
(Iter_Name
);
1988 if Nkind
(Iter_Name
) = N_Function_Call
then
1990 C
: constant Node_Id
:= Name
(Iter_Name
);
1995 if not Is_Overloaded
(Iter_Name
) then
1996 Resolve
(Iter_Name
, Etype
(C
));
1999 Get_First_Interp
(C
, I
, It
);
2000 while It
.Typ
/= Empty
loop
2001 if Reverse_Present
(N
) then
2002 if Is_Reversible_Iterator
(It
.Typ
) then
2003 Resolve
(Iter_Name
, It
.Typ
);
2007 elsif Is_Iterator
(It
.Typ
) then
2008 Resolve
(Iter_Name
, It
.Typ
);
2012 Get_Next_Interp
(I
, It
);
2017 -- Domain of iteration is not overloaded
2020 Resolve
(Iter_Name
, Etype
(Iter_Name
));
2023 if not Of_Present
(N
) then
2024 Check_Reverse_Iteration
(Etype
(Iter_Name
));
2028 -- Get base type of container, for proper retrieval of Cursor type
2029 -- and primitive operations.
2031 Typ
:= Base_Type
(Etype
(Iter_Name
));
2033 if Is_Array_Type
(Typ
) then
2034 if Of_Present
(N
) then
2035 Set_Etype
(Def_Id
, Component_Type
(Typ
));
2037 -- The loop variable is aliased if the array components are
2040 Set_Is_Aliased
(Def_Id
, Has_Aliased_Components
(Typ
));
2042 -- AI12-0151 stipulates that the container cannot be a component
2043 -- that depends on a discriminant if the enclosing object is
2044 -- mutable, to prevent a modification of the container in the
2045 -- course of an iteration.
2047 -- Should comment on need to go to Original_Node ???
2049 if Nkind
(Original_Node
(Iter_Name
)) = N_Selected_Component
2050 and then Is_Dependent_Component_Of_Mutable_Object
2051 (Original_Node
(Iter_Name
))
2054 ("container cannot be a discriminant-dependent "
2055 & "component of a mutable object", N
);
2060 (Base_Type
(Bas
) /= Base_Type
(Component_Type
(Typ
))
2062 not Subtypes_Statically_Match
(Bas
, Component_Type
(Typ
)))
2065 ("subtype indication does not match component type", Subt
);
2068 -- Here we have a missing Range attribute
2072 ("missing Range attribute in iteration over an array", N
);
2074 -- In Ada 2012 mode, this may be an attempt at an iterator
2076 if Ada_Version
>= Ada_2012
then
2078 ("\if& is meant to designate an element of the array, use OF",
2082 -- Prevent cascaded errors
2084 Set_Ekind
(Def_Id
, E_Loop_Parameter
);
2085 Set_Etype
(Def_Id
, Etype
(First_Index
(Typ
)));
2088 -- Check for type error in iterator
2090 elsif Typ
= Any_Type
then
2093 -- Iteration over a container
2096 Set_Ekind
(Def_Id
, E_Loop_Parameter
);
2097 Error_Msg_Ada_2012_Feature
("container iterator", Sloc
(N
));
2101 if Of_Present
(N
) then
2102 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2104 Elt
: constant Entity_Id
:=
2105 Get_Iterable_Type_Primitive
(Typ
, Name_Element
);
2109 ("missing Element primitive for iteration", N
);
2111 Set_Etype
(Def_Id
, Etype
(Elt
));
2115 -- For a predefined container, The type of the loop variable is
2116 -- the Iterator_Element aspect of the container type.
2120 Element
: constant Entity_Id
:=
2121 Find_Value_Of_Aspect
(Typ
, Aspect_Iterator_Element
);
2122 Iterator
: constant Entity_Id
:=
2123 Find_Value_Of_Aspect
(Typ
, Aspect_Default_Iterator
);
2124 Cursor_Type
: Entity_Id
;
2127 if No
(Element
) then
2128 Error_Msg_NE
("cannot iterate over&", N
, Typ
);
2132 Set_Etype
(Def_Id
, Entity
(Element
));
2133 Cursor_Type
:= Get_Cursor_Type
(Typ
);
2134 pragma Assert
(Present
(Cursor_Type
));
2136 -- If subtype indication was given, verify that it covers
2137 -- the element type of the container.
2140 and then (not Covers
(Bas
, Etype
(Def_Id
))
2141 or else not Subtypes_Statically_Match
2142 (Bas
, Etype
(Def_Id
)))
2145 ("subtype indication does not match element type",
2149 -- If the container has a variable indexing aspect, the
2150 -- element is a variable and is modifiable in the loop.
2152 if Has_Aspect
(Typ
, Aspect_Variable_Indexing
) then
2153 Set_Ekind
(Def_Id
, E_Variable
);
2156 -- If the container is a constant, iterating over it
2157 -- requires a Constant_Indexing operation.
2159 if not Is_Variable
(Iter_Name
)
2160 and then not Has_Aspect
(Typ
, Aspect_Constant_Indexing
)
2162 Error_Msg_N
("iteration over constant container "
2163 & "require constant_indexing aspect", N
);
2165 -- The Iterate function may have an in_out parameter,
2166 -- and a constant container is thus illegal.
2168 elsif Present
(Iterator
)
2169 and then Ekind
(Entity
(Iterator
)) = E_Function
2170 and then Ekind
(First_Formal
(Entity
(Iterator
))) /=
2172 and then not Is_Variable
(Iter_Name
)
2175 ("variable container expected", N
);
2178 if Nkind
(Original_Node
(Iter_Name
))
2179 = N_Selected_Component
2181 Is_Dependent_Component_Of_Mutable_Object
2182 (Original_Node
(Iter_Name
))
2185 ("container cannot be a discriminant-dependent "
2186 & "component of a mutable object", N
);
2192 -- IN iterator, domain is a range, or a call to Iterate function
2195 -- For an iteration of the form IN, the name must denote an
2196 -- iterator, typically the result of a call to Iterate. Give a
2197 -- useful error message when the name is a container by itself.
2199 -- The type may be a formal container type, which has to have
2200 -- an Iterable aspect detailing the required primitives.
2202 if Is_Entity_Name
(Original_Node
(Name
(N
)))
2203 and then not Is_Iterator
(Typ
)
2205 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2208 elsif not Has_Aspect
(Typ
, Aspect_Iterator_Element
) then
2210 ("cannot iterate over&", Name
(N
), Typ
);
2213 ("name must be an iterator, not a container", Name
(N
));
2216 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2220 ("\to iterate directly over the elements of a container, "
2221 & "write `of &`", Name
(N
), Original_Node
(Name
(N
)));
2223 -- No point in continuing analysis of iterator spec
2229 -- If the name is a call (typically prefixed) to some Iterate
2230 -- function, it has been rewritten as an object declaration.
2231 -- If that object is a selected component, verify that it is not
2232 -- a component of an unconstrained mutable object.
2234 if Nkind
(Iter_Name
) = N_Identifier
then
2236 Orig_Node
: constant Node_Id
:= Original_Node
(Iter_Name
);
2237 Iter_Kind
: constant Node_Kind
:= Nkind
(Orig_Node
);
2241 if Iter_Kind
= N_Selected_Component
then
2242 Obj
:= Prefix
(Orig_Node
);
2244 elsif Iter_Kind
= N_Function_Call
then
2245 Obj
:= First_Actual
(Orig_Node
);
2247 -- If neither, the name comes from source
2253 if Nkind
(Obj
) = N_Selected_Component
2254 and then Is_Dependent_Component_Of_Mutable_Object
(Obj
)
2257 ("container cannot be a discriminant-dependent "
2258 & "component of a mutable object", N
);
2263 -- The result type of Iterate function is the classwide type of
2264 -- the interface parent. We need the specific Cursor type defined
2265 -- in the container package. We obtain it by name for a predefined
2266 -- container, or through the Iterable aspect for a formal one.
2268 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2271 (Parent
(Find_Value_Of_Aspect
(Typ
, Aspect_Iterable
)),
2273 Ent
:= Etype
(Def_Id
);
2276 Set_Etype
(Def_Id
, Get_Cursor_Type
(Typ
));
2282 -- A loop parameter cannot be effectively volatile. This check is
2283 -- peformed only when SPARK_Mode is on as it is not a standard Ada
2284 -- legality check (SPARK RM 7.1.3(6)).
2286 -- Not clear whether this applies to element iterators, where the
2287 -- cursor is not an explicit entity ???
2290 and then not Of_Present
(N
)
2291 and then Is_Effectively_Volatile
(Ent
)
2293 Error_Msg_N
("loop parameter cannot be volatile", Ent
);
2295 end Analyze_Iterator_Specification
;
2301 -- Note: the semantic work required for analyzing labels (setting them as
2302 -- reachable) was done in a prepass through the statements in the block,
2303 -- so that forward gotos would be properly handled. See Analyze_Statements
2304 -- for further details. The only processing required here is to deal with
2305 -- optimizations that depend on an assumption of sequential control flow,
2306 -- since of course the occurrence of a label breaks this assumption.
2308 procedure Analyze_Label
(N
: Node_Id
) is
2309 pragma Warnings
(Off
, N
);
2311 Kill_Current_Values
;
2314 --------------------------
2315 -- Analyze_Label_Entity --
2316 --------------------------
2318 procedure Analyze_Label_Entity
(E
: Entity_Id
) is
2320 Set_Ekind
(E
, E_Label
);
2321 Set_Etype
(E
, Standard_Void_Type
);
2322 Set_Enclosing_Scope
(E
, Current_Scope
);
2323 Set_Reachable
(E
, True);
2324 end Analyze_Label_Entity
;
2326 ------------------------------------------
2327 -- Analyze_Loop_Parameter_Specification --
2328 ------------------------------------------
2330 procedure Analyze_Loop_Parameter_Specification
(N
: Node_Id
) is
2331 Loop_Nod
: constant Node_Id
:= Parent
(Parent
(N
));
2333 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
);
2334 -- If the bounds are given by a 'Range reference on a function call
2335 -- that returns a controlled array, introduce an explicit declaration
2336 -- to capture the bounds, so that the function result can be finalized
2337 -- in timely fashion.
2339 procedure Check_Predicate_Use
(T
: Entity_Id
);
2340 -- Diagnose Attempt to iterate through non-static predicate. Note that
2341 -- a type with inherited predicates may have both static and dynamic
2342 -- forms. In this case it is not sufficent to check the static predicate
2343 -- function only, look for a dynamic predicate aspect as well.
2345 function Has_Call_Using_Secondary_Stack
(N
: Node_Id
) return Boolean;
2346 -- N is the node for an arbitrary construct. This function searches the
2347 -- construct N to see if any expressions within it contain function
2348 -- calls that use the secondary stack, returning True if any such call
2349 -- is found, and False otherwise.
2351 procedure Process_Bounds
(R
: Node_Id
);
2352 -- If the iteration is given by a range, create temporaries and
2353 -- assignment statements block to capture the bounds and perform
2354 -- required finalization actions in case a bound includes a function
2355 -- call that uses the temporary stack. We first pre-analyze a copy of
2356 -- the range in order to determine the expected type, and analyze and
2357 -- resolve the original bounds.
2359 --------------------------------------
2360 -- Check_Controlled_Array_Attribute --
2361 --------------------------------------
2363 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
) is
2365 if Nkind
(DS
) = N_Attribute_Reference
2366 and then Is_Entity_Name
(Prefix
(DS
))
2367 and then Ekind
(Entity
(Prefix
(DS
))) = E_Function
2368 and then Is_Array_Type
(Etype
(Entity
(Prefix
(DS
))))
2370 Is_Controlled
(Component_Type
(Etype
(Entity
(Prefix
(DS
)))))
2371 and then Expander_Active
2374 Loc
: constant Source_Ptr
:= Sloc
(N
);
2375 Arr
: constant Entity_Id
:= Etype
(Entity
(Prefix
(DS
)));
2376 Indx
: constant Entity_Id
:=
2377 Base_Type
(Etype
(First_Index
(Arr
)));
2378 Subt
: constant Entity_Id
:= Make_Temporary
(Loc
, 'S');
2383 Make_Subtype_Declaration
(Loc
,
2384 Defining_Identifier
=> Subt
,
2385 Subtype_Indication
=>
2386 Make_Subtype_Indication
(Loc
,
2387 Subtype_Mark
=> New_Occurrence_Of
(Indx
, Loc
),
2389 Make_Range_Constraint
(Loc
, Relocate_Node
(DS
))));
2390 Insert_Before
(Loop_Nod
, Decl
);
2394 Make_Attribute_Reference
(Loc
,
2395 Prefix
=> New_Occurrence_Of
(Subt
, Loc
),
2396 Attribute_Name
=> Attribute_Name
(DS
)));
2401 end Check_Controlled_Array_Attribute
;
2403 -------------------------
2404 -- Check_Predicate_Use --
2405 -------------------------
2407 procedure Check_Predicate_Use
(T
: Entity_Id
) is
2409 -- A predicated subtype is illegal in loops and related constructs
2410 -- if the predicate is not static, or if it is a non-static subtype
2411 -- of a statically predicated subtype.
2413 if Is_Discrete_Type
(T
)
2414 and then Has_Predicates
(T
)
2415 and then (not Has_Static_Predicate
(T
)
2416 or else not Is_Static_Subtype
(T
)
2417 or else Has_Dynamic_Predicate_Aspect
(T
))
2419 -- Seems a confusing message for the case of a static predicate
2420 -- with a non-static subtype???
2422 Bad_Predicated_Subtype_Use
2423 ("cannot use subtype& with non-static predicate for loop "
2424 & "iteration", Discrete_Subtype_Definition
(N
),
2425 T
, Suggest_Static
=> True);
2427 elsif Inside_A_Generic
and then Is_Generic_Formal
(T
) then
2428 Set_No_Dynamic_Predicate_On_Actual
(T
);
2430 end Check_Predicate_Use
;
2432 ------------------------------------
2433 -- Has_Call_Using_Secondary_Stack --
2434 ------------------------------------
2436 function Has_Call_Using_Secondary_Stack
(N
: Node_Id
) return Boolean is
2438 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
2439 -- Check if N is a function call which uses the secondary stack
2445 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
2448 Return_Typ
: Entity_Id
;
2451 if Nkind
(N
) = N_Function_Call
then
2454 -- Call using access to subprogram with explicit dereference
2456 if Nkind
(Nam
) = N_Explicit_Dereference
then
2457 Subp
:= Etype
(Nam
);
2459 -- Call using a selected component notation or Ada 2005 object
2460 -- operation notation
2462 elsif Nkind
(Nam
) = N_Selected_Component
then
2463 Subp
:= Entity
(Selector_Name
(Nam
));
2468 Subp
:= Entity
(Nam
);
2471 Return_Typ
:= Etype
(Subp
);
2473 if Is_Composite_Type
(Return_Typ
)
2474 and then not Is_Constrained
(Return_Typ
)
2478 elsif Sec_Stack_Needed_For_Return
(Subp
) then
2483 -- Continue traversing the tree
2488 function Check_Calls
is new Traverse_Func
(Check_Call
);
2490 -- Start of processing for Has_Call_Using_Secondary_Stack
2493 return Check_Calls
(N
) = Abandon
;
2494 end Has_Call_Using_Secondary_Stack
;
2496 --------------------
2497 -- Process_Bounds --
2498 --------------------
2500 procedure Process_Bounds
(R
: Node_Id
) is
2501 Loc
: constant Source_Ptr
:= Sloc
(N
);
2504 (Original_Bound
: Node_Id
;
2505 Analyzed_Bound
: Node_Id
;
2506 Typ
: Entity_Id
) return Node_Id
;
2507 -- Capture value of bound and return captured value
2514 (Original_Bound
: Node_Id
;
2515 Analyzed_Bound
: Node_Id
;
2516 Typ
: Entity_Id
) return Node_Id
2523 -- If the bound is a constant or an object, no need for a separate
2524 -- declaration. If the bound is the result of previous expansion
2525 -- it is already analyzed and should not be modified. Note that
2526 -- the Bound will be resolved later, if needed, as part of the
2527 -- call to Make_Index (literal bounds may need to be resolved to
2530 if Analyzed
(Original_Bound
) then
2531 return Original_Bound
;
2533 elsif Nkind_In
(Analyzed_Bound
, N_Integer_Literal
,
2534 N_Character_Literal
)
2535 or else Is_Entity_Name
(Analyzed_Bound
)
2537 Analyze_And_Resolve
(Original_Bound
, Typ
);
2538 return Original_Bound
;
2541 -- Normally, the best approach is simply to generate a constant
2542 -- declaration that captures the bound. However, there is a nasty
2543 -- case where this is wrong. If the bound is complex, and has a
2544 -- possible use of the secondary stack, we need to generate a
2545 -- separate assignment statement to ensure the creation of a block
2546 -- which will release the secondary stack.
2548 -- We prefer the constant declaration, since it leaves us with a
2549 -- proper trace of the value, useful in optimizations that get rid
2550 -- of junk range checks.
2552 if not Has_Call_Using_Secondary_Stack
(Analyzed_Bound
) then
2553 Analyze_And_Resolve
(Original_Bound
, Typ
);
2555 -- Ensure that the bound is valid. This check should not be
2556 -- generated when the range belongs to a quantified expression
2557 -- as the construct is still not expanded into its final form.
2559 if Nkind
(Parent
(R
)) /= N_Loop_Parameter_Specification
2560 or else Nkind
(Parent
(Parent
(R
))) /= N_Quantified_Expression
2562 Ensure_Valid
(Original_Bound
);
2565 Force_Evaluation
(Original_Bound
);
2566 return Original_Bound
;
2569 Id
:= Make_Temporary
(Loc
, 'R', Original_Bound
);
2571 -- Here we make a declaration with a separate assignment
2572 -- statement, and insert before loop header.
2575 Make_Object_Declaration
(Loc
,
2576 Defining_Identifier
=> Id
,
2577 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
2580 Make_Assignment_Statement
(Loc
,
2581 Name
=> New_Occurrence_Of
(Id
, Loc
),
2582 Expression
=> Relocate_Node
(Original_Bound
));
2584 Insert_Actions
(Loop_Nod
, New_List
(Decl
, Assign
));
2586 -- Now that this temporary variable is initialized we decorate it
2587 -- as safe-to-reevaluate to inform to the backend that no further
2588 -- asignment will be issued and hence it can be handled as side
2589 -- effect free. Note that this decoration must be done when the
2590 -- assignment has been analyzed because otherwise it will be
2591 -- rejected (see Analyze_Assignment).
2593 Set_Is_Safe_To_Reevaluate
(Id
);
2595 Rewrite
(Original_Bound
, New_Occurrence_Of
(Id
, Loc
));
2597 if Nkind
(Assign
) = N_Assignment_Statement
then
2598 return Expression
(Assign
);
2600 return Original_Bound
;
2604 Hi
: constant Node_Id
:= High_Bound
(R
);
2605 Lo
: constant Node_Id
:= Low_Bound
(R
);
2606 R_Copy
: constant Node_Id
:= New_Copy_Tree
(R
);
2611 -- Start of processing for Process_Bounds
2614 Set_Parent
(R_Copy
, Parent
(R
));
2615 Preanalyze_Range
(R_Copy
);
2616 Typ
:= Etype
(R_Copy
);
2618 -- If the type of the discrete range is Universal_Integer, then the
2619 -- bound's type must be resolved to Integer, and any object used to
2620 -- hold the bound must also have type Integer, unless the literal
2621 -- bounds are constant-folded expressions with a user-defined type.
2623 if Typ
= Universal_Integer
then
2624 if Nkind
(Lo
) = N_Integer_Literal
2625 and then Present
(Etype
(Lo
))
2626 and then Scope
(Etype
(Lo
)) /= Standard_Standard
2630 elsif Nkind
(Hi
) = N_Integer_Literal
2631 and then Present
(Etype
(Hi
))
2632 and then Scope
(Etype
(Hi
)) /= Standard_Standard
2637 Typ
:= Standard_Integer
;
2643 New_Lo
:= One_Bound
(Lo
, Low_Bound
(R_Copy
), Typ
);
2644 New_Hi
:= One_Bound
(Hi
, High_Bound
(R_Copy
), Typ
);
2646 -- Propagate staticness to loop range itself, in case the
2647 -- corresponding subtype is static.
2649 if New_Lo
/= Lo
and then Is_OK_Static_Expression
(New_Lo
) then
2650 Rewrite
(Low_Bound
(R
), New_Copy
(New_Lo
));
2653 if New_Hi
/= Hi
and then Is_OK_Static_Expression
(New_Hi
) then
2654 Rewrite
(High_Bound
(R
), New_Copy
(New_Hi
));
2660 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(N
);
2661 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2665 -- Start of processing for Analyze_Loop_Parameter_Specification
2670 -- We always consider the loop variable to be referenced, since the loop
2671 -- may be used just for counting purposes.
2673 Generate_Reference
(Id
, N
, ' ');
2675 -- Check for the case of loop variable hiding a local variable (used
2676 -- later on to give a nice warning if the hidden variable is never
2680 H
: constant Entity_Id
:= Homonym
(Id
);
2683 and then Ekind
(H
) = E_Variable
2684 and then Is_Discrete_Type
(Etype
(H
))
2685 and then Enclosing_Dynamic_Scope
(H
) = Enclosing_Dynamic_Scope
(Id
)
2687 Set_Hiding_Loop_Variable
(H
, Id
);
2691 -- Loop parameter specification must include subtype mark in SPARK
2693 if Nkind
(DS
) = N_Range
then
2694 Check_SPARK_05_Restriction
2695 ("loop parameter specification must include subtype mark", N
);
2698 -- Analyze the subtype definition and create temporaries for the bounds.
2699 -- Do not evaluate the range when preanalyzing a quantified expression
2700 -- because bounds expressed as function calls with side effects will be
2701 -- incorrectly replicated.
2703 if Nkind
(DS
) = N_Range
2704 and then Expander_Active
2705 and then Nkind
(Parent
(N
)) /= N_Quantified_Expression
2707 Process_Bounds
(DS
);
2709 -- Either the expander not active or the range of iteration is a subtype
2710 -- indication, an entity, or a function call that yields an aggregate or
2714 DS_Copy
:= New_Copy_Tree
(DS
);
2715 Set_Parent
(DS_Copy
, Parent
(DS
));
2716 Preanalyze_Range
(DS_Copy
);
2718 -- Ada 2012: If the domain of iteration is:
2720 -- a) a function call,
2721 -- b) an identifier that is not a type,
2722 -- c) an attribute reference 'Old (within a postcondition)
2723 -- d) an unchecked conversion
2725 -- then it is an iteration over a container. It was classified as
2726 -- a loop specification by the parser, and must be rewritten now
2727 -- to activate container iteration. The last case will occur within
2728 -- an expanded inlined call, where the expansion wraps an actual in
2729 -- an unchecked conversion when needed. The expression of the
2730 -- conversion is always an object.
2732 if Nkind
(DS_Copy
) = N_Function_Call
2733 or else (Is_Entity_Name
(DS_Copy
)
2734 and then not Is_Type
(Entity
(DS_Copy
)))
2735 or else (Nkind
(DS_Copy
) = N_Attribute_Reference
2736 and then Nam_In
(Attribute_Name
(DS_Copy
),
2737 Name_Old
, Name_Loop_Entry
))
2738 or else Nkind
(DS_Copy
) = N_Unchecked_Type_Conversion
2739 or else Has_Aspect
(Etype
(DS_Copy
), Aspect_Iterable
)
2741 -- This is an iterator specification. Rewrite it as such and
2742 -- analyze it to capture function calls that may require
2743 -- finalization actions.
2746 I_Spec
: constant Node_Id
:=
2747 Make_Iterator_Specification
(Sloc
(N
),
2748 Defining_Identifier
=> Relocate_Node
(Id
),
2750 Subtype_Indication
=> Empty
,
2751 Reverse_Present
=> Reverse_Present
(N
));
2752 Scheme
: constant Node_Id
:= Parent
(N
);
2755 Set_Iterator_Specification
(Scheme
, I_Spec
);
2756 Set_Loop_Parameter_Specification
(Scheme
, Empty
);
2757 Analyze_Iterator_Specification
(I_Spec
);
2759 -- In a generic context, analyze the original domain of
2760 -- iteration, for name capture.
2762 if not Expander_Active
then
2766 -- Set kind of loop parameter, which may be used in the
2767 -- subsequent analysis of the condition in a quantified
2770 Set_Ekind
(Id
, E_Loop_Parameter
);
2774 -- Domain of iteration is not a function call, and is side-effect
2778 -- A quantified expression that appears in a pre/post condition
2779 -- is pre-analyzed several times. If the range is given by an
2780 -- attribute reference it is rewritten as a range, and this is
2781 -- done even with expansion disabled. If the type is already set
2782 -- do not reanalyze, because a range with static bounds may be
2783 -- typed Integer by default.
2785 if Nkind
(Parent
(N
)) = N_Quantified_Expression
2786 and then Present
(Etype
(DS
))
2799 -- Some additional checks if we are iterating through a type
2801 if Is_Entity_Name
(DS
)
2802 and then Present
(Entity
(DS
))
2803 and then Is_Type
(Entity
(DS
))
2805 -- The subtype indication may denote the completion of an incomplete
2806 -- type declaration.
2808 if Ekind
(Entity
(DS
)) = E_Incomplete_Type
then
2809 Set_Entity
(DS
, Get_Full_View
(Entity
(DS
)));
2810 Set_Etype
(DS
, Entity
(DS
));
2813 Check_Predicate_Use
(Entity
(DS
));
2816 -- Error if not discrete type
2818 if not Is_Discrete_Type
(Etype
(DS
)) then
2819 Wrong_Type
(DS
, Any_Discrete
);
2820 Set_Etype
(DS
, Any_Type
);
2823 Check_Controlled_Array_Attribute
(DS
);
2825 if Nkind
(DS
) = N_Subtype_Indication
then
2826 Check_Predicate_Use
(Entity
(Subtype_Mark
(DS
)));
2829 Make_Index
(DS
, N
, In_Iter_Schm
=> True);
2830 Set_Ekind
(Id
, E_Loop_Parameter
);
2832 -- A quantified expression which appears in a pre- or post-condition may
2833 -- be analyzed multiple times. The analysis of the range creates several
2834 -- itypes which reside in different scopes depending on whether the pre-
2835 -- or post-condition has been expanded. Update the type of the loop
2836 -- variable to reflect the proper itype at each stage of analysis.
2839 or else Etype
(Id
) = Any_Type
2841 (Present
(Etype
(Id
))
2842 and then Is_Itype
(Etype
(Id
))
2843 and then Nkind
(Parent
(Loop_Nod
)) = N_Expression_With_Actions
2844 and then Nkind
(Original_Node
(Parent
(Loop_Nod
))) =
2845 N_Quantified_Expression
)
2847 Set_Etype
(Id
, Etype
(DS
));
2850 -- Treat a range as an implicit reference to the type, to inhibit
2851 -- spurious warnings.
2853 Generate_Reference
(Base_Type
(Etype
(DS
)), N
, ' ');
2854 Set_Is_Known_Valid
(Id
, True);
2856 -- The loop is not a declarative part, so the loop variable must be
2857 -- frozen explicitly. Do not freeze while preanalyzing a quantified
2858 -- expression because the freeze node will not be inserted into the
2859 -- tree due to flag Is_Spec_Expression being set.
2861 if Nkind
(Parent
(N
)) /= N_Quantified_Expression
then
2863 Flist
: constant List_Id
:= Freeze_Entity
(Id
, N
);
2865 if Is_Non_Empty_List
(Flist
) then
2866 Insert_Actions
(N
, Flist
);
2871 -- Case where we have a range or a subtype, get type bounds
2873 if Nkind_In
(DS
, N_Range
, N_Subtype_Indication
)
2874 and then not Error_Posted
(DS
)
2875 and then Etype
(DS
) /= Any_Type
2876 and then Is_Discrete_Type
(Etype
(DS
))
2883 if Nkind
(DS
) = N_Range
then
2884 L
:= Low_Bound
(DS
);
2885 H
:= High_Bound
(DS
);
2888 Type_Low_Bound
(Underlying_Type
(Etype
(Subtype_Mark
(DS
))));
2890 Type_High_Bound
(Underlying_Type
(Etype
(Subtype_Mark
(DS
))));
2893 -- Check for null or possibly null range and issue warning. We
2894 -- suppress such messages in generic templates and instances,
2895 -- because in practice they tend to be dubious in these cases. The
2896 -- check applies as well to rewritten array element loops where a
2897 -- null range may be detected statically.
2899 if Compile_Time_Compare
(L
, H
, Assume_Valid
=> True) = GT
then
2901 -- Suppress the warning if inside a generic template or
2902 -- instance, since in practice they tend to be dubious in these
2903 -- cases since they can result from intended parameterization.
2905 if not Inside_A_Generic
and then not In_Instance
then
2907 -- Specialize msg if invalid values could make the loop
2908 -- non-null after all.
2910 if Compile_Time_Compare
2911 (L
, H
, Assume_Valid
=> False) = GT
2913 -- Since we know the range of the loop is null, set the
2914 -- appropriate flag to remove the loop entirely during
2917 Set_Is_Null_Loop
(Loop_Nod
);
2919 if Comes_From_Source
(N
) then
2921 ("??loop range is null, loop will not execute", DS
);
2924 -- Here is where the loop could execute because of
2925 -- invalid values, so issue appropriate message and in
2926 -- this case we do not set the Is_Null_Loop flag since
2927 -- the loop may execute.
2929 elsif Comes_From_Source
(N
) then
2931 ("??loop range may be null, loop may not execute",
2934 ("??can only execute if invalid values are present",
2939 -- In either case, suppress warnings in the body of the loop,
2940 -- since it is likely that these warnings will be inappropriate
2941 -- if the loop never actually executes, which is likely.
2943 Set_Suppress_Loop_Warnings
(Loop_Nod
);
2945 -- The other case for a warning is a reverse loop where the
2946 -- upper bound is the integer literal zero or one, and the
2947 -- lower bound may exceed this value.
2949 -- For example, we have
2951 -- for J in reverse N .. 1 loop
2953 -- In practice, this is very likely to be a case of reversing
2954 -- the bounds incorrectly in the range.
2956 elsif Reverse_Present
(N
)
2957 and then Nkind
(Original_Node
(H
)) = N_Integer_Literal
2959 (Intval
(Original_Node
(H
)) = Uint_0
2961 Intval
(Original_Node
(H
)) = Uint_1
)
2963 -- Lower bound may in fact be known and known not to exceed
2964 -- upper bound (e.g. reverse 0 .. 1) and that's OK.
2966 if Compile_Time_Known_Value
(L
)
2967 and then Expr_Value
(L
) <= Expr_Value
(H
)
2971 -- Otherwise warning is warranted
2974 Error_Msg_N
("??loop range may be null", DS
);
2975 Error_Msg_N
("\??bounds may be wrong way round", DS
);
2979 -- Check if either bound is known to be outside the range of the
2980 -- loop parameter type, this is e.g. the case of a loop from
2981 -- 20..X where the type is 1..19.
2983 -- Such a loop is dubious since either it raises CE or it executes
2984 -- zero times, and that cannot be useful!
2986 if Etype
(DS
) /= Any_Type
2987 and then not Error_Posted
(DS
)
2988 and then Nkind
(DS
) = N_Subtype_Indication
2989 and then Nkind
(Constraint
(DS
)) = N_Range_Constraint
2992 LLo
: constant Node_Id
:=
2993 Low_Bound
(Range_Expression
(Constraint
(DS
)));
2994 LHi
: constant Node_Id
:=
2995 High_Bound
(Range_Expression
(Constraint
(DS
)));
2997 Bad_Bound
: Node_Id
:= Empty
;
2998 -- Suspicious loop bound
3001 -- At this stage L, H are the bounds of the type, and LLo
3002 -- Lhi are the low bound and high bound of the loop.
3004 if Compile_Time_Compare
(LLo
, L
, Assume_Valid
=> True) = LT
3006 Compile_Time_Compare
(LLo
, H
, Assume_Valid
=> True) = GT
3011 if Compile_Time_Compare
(LHi
, L
, Assume_Valid
=> True) = LT
3013 Compile_Time_Compare
(LHi
, H
, Assume_Valid
=> True) = GT
3018 if Present
(Bad_Bound
) then
3020 ("suspicious loop bound out of range of "
3021 & "loop subtype??", Bad_Bound
);
3023 ("\loop executes zero times or raises "
3024 & "Constraint_Error??", Bad_Bound
);
3029 -- This declare block is about warnings, if we get an exception while
3030 -- testing for warnings, we simply abandon the attempt silently. This
3031 -- most likely occurs as the result of a previous error, but might
3032 -- just be an obscure case we have missed. In either case, not giving
3033 -- the warning is perfectly acceptable.
3036 when others => null;
3040 -- A loop parameter cannot be effectively volatile. This check is
3041 -- peformed only when SPARK_Mode is on as it is not a standard Ada
3042 -- legality check (SPARK RM 7.1.3(6)).
3044 if SPARK_Mode
= On
and then Is_Effectively_Volatile
(Id
) then
3045 Error_Msg_N
("loop parameter cannot be volatile", Id
);
3047 end Analyze_Loop_Parameter_Specification
;
3049 ----------------------------
3050 -- Analyze_Loop_Statement --
3051 ----------------------------
3053 procedure Analyze_Loop_Statement
(N
: Node_Id
) is
3055 function Is_Container_Iterator
(Iter
: Node_Id
) return Boolean;
3056 -- Given a loop iteration scheme, determine whether it is an Ada 2012
3057 -- container iteration.
3059 function Is_Wrapped_In_Block
(N
: Node_Id
) return Boolean;
3060 -- Determine whether loop statement N has been wrapped in a block to
3061 -- capture finalization actions that may be generated for container
3062 -- iterators. Prevents infinite recursion when block is analyzed.
3063 -- Routine is a noop if loop is single statement within source block.
3065 ---------------------------
3066 -- Is_Container_Iterator --
3067 ---------------------------
3069 function Is_Container_Iterator
(Iter
: Node_Id
) return Boolean is
3078 elsif Present
(Condition
(Iter
)) then
3081 -- for Def_Id in [reverse] Name loop
3082 -- for Def_Id [: Subtype_Indication] of [reverse] Name loop
3084 elsif Present
(Iterator_Specification
(Iter
)) then
3086 Nam
: constant Node_Id
:= Name
(Iterator_Specification
(Iter
));
3090 Nam_Copy
:= New_Copy_Tree
(Nam
);
3091 Set_Parent
(Nam_Copy
, Parent
(Nam
));
3092 Preanalyze_Range
(Nam_Copy
);
3094 -- The only two options here are iteration over a container or
3097 return not Is_Array_Type
(Etype
(Nam_Copy
));
3100 -- for Def_Id in [reverse] Discrete_Subtype_Definition loop
3104 LP
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
3105 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(LP
);
3109 DS_Copy
:= New_Copy_Tree
(DS
);
3110 Set_Parent
(DS_Copy
, Parent
(DS
));
3111 Preanalyze_Range
(DS_Copy
);
3113 -- Check for a call to Iterate ()
3116 Nkind
(DS_Copy
) = N_Function_Call
3117 and then Needs_Finalization
(Etype
(DS_Copy
));
3120 end Is_Container_Iterator
;
3122 -------------------------
3123 -- Is_Wrapped_In_Block --
3124 -------------------------
3126 function Is_Wrapped_In_Block
(N
: Node_Id
) return Boolean is
3132 -- Check if current scope is a block that is not a transient block.
3134 if Ekind
(Current_Scope
) /= E_Block
3135 or else No
(Block_Node
(Current_Scope
))
3141 Handled_Statement_Sequence
(Parent
(Block_Node
(Current_Scope
)));
3143 -- Skip leading pragmas that may be introduced for invariant and
3144 -- predicate checks.
3146 Stat
:= First
(Statements
(HSS
));
3147 while Present
(Stat
) and then Nkind
(Stat
) = N_Pragma
loop
3148 Stat
:= Next
(Stat
);
3151 return Stat
= N
and then No
(Next
(Stat
));
3153 end Is_Wrapped_In_Block
;
3155 -- Local declarations
3157 Id
: constant Node_Id
:= Identifier
(N
);
3158 Iter
: constant Node_Id
:= Iteration_Scheme
(N
);
3159 Loc
: constant Source_Ptr
:= Sloc
(N
);
3163 -- Start of processing for Analyze_Loop_Statement
3166 if Present
(Id
) then
3168 -- Make name visible, e.g. for use in exit statements. Loop labels
3169 -- are always considered to be referenced.
3174 -- Guard against serious error (typically, a scope mismatch when
3175 -- semantic analysis is requested) by creating loop entity to
3176 -- continue analysis.
3179 if Total_Errors_Detected
/= 0 then
3180 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Loc
, 'L');
3182 raise Program_Error
;
3185 -- Verify that the loop name is hot hidden by an unrelated
3186 -- declaration in an inner scope.
3188 elsif Ekind
(Ent
) /= E_Label
and then Ekind
(Ent
) /= E_Loop
then
3189 Error_Msg_Sloc
:= Sloc
(Ent
);
3190 Error_Msg_N
("implicit label declaration for & is hidden#", Id
);
3192 if Present
(Homonym
(Ent
))
3193 and then Ekind
(Homonym
(Ent
)) = E_Label
3195 Set_Entity
(Id
, Ent
);
3196 Set_Ekind
(Ent
, E_Loop
);
3200 Generate_Reference
(Ent
, N
, ' ');
3201 Generate_Definition
(Ent
);
3203 -- If we found a label, mark its type. If not, ignore it, since it
3204 -- means we have a conflicting declaration, which would already
3205 -- have been diagnosed at declaration time. Set Label_Construct
3206 -- of the implicit label declaration, which is not created by the
3207 -- parser for generic units.
3209 if Ekind
(Ent
) = E_Label
then
3210 Set_Ekind
(Ent
, E_Loop
);
3212 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
3213 Set_Label_Construct
(Parent
(Ent
), N
);
3218 -- Case of no identifier present
3221 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Loc
, 'L');
3222 Set_Etype
(Ent
, Standard_Void_Type
);
3223 Set_Parent
(Ent
, N
);
3226 -- Iteration over a container in Ada 2012 involves the creation of a
3227 -- controlled iterator object. Wrap the loop in a block to ensure the
3228 -- timely finalization of the iterator and release of container locks.
3229 -- The same applies to the use of secondary stack when obtaining an
3232 if Ada_Version
>= Ada_2012
3233 and then Is_Container_Iterator
(Iter
)
3234 and then not Is_Wrapped_In_Block
(N
)
3237 Block_Nod
: Node_Id
;
3238 Block_Id
: Entity_Id
;
3242 Make_Block_Statement
(Loc
,
3243 Declarations
=> New_List
,
3244 Handled_Statement_Sequence
=>
3245 Make_Handled_Sequence_Of_Statements
(Loc
,
3246 Statements
=> New_List
(Relocate_Node
(N
))));
3248 Add_Block_Identifier
(Block_Nod
, Block_Id
);
3250 -- The expansion of iterator loops generates an iterator in order
3251 -- to traverse the elements of a container:
3253 -- Iter : <iterator type> := Iterate (Container)'reference;
3255 -- The iterator is controlled and returned on the secondary stack.
3256 -- The analysis of the call to Iterate establishes a transient
3257 -- scope to deal with the secondary stack management, but never
3258 -- really creates a physical block as this would kill the iterator
3259 -- too early (see Wrap_Transient_Declaration). To address this
3260 -- case, mark the generated block as needing secondary stack
3263 Set_Uses_Sec_Stack
(Block_Id
);
3265 Rewrite
(N
, Block_Nod
);
3271 -- Kill current values on entry to loop, since statements in the body of
3272 -- the loop may have been executed before the loop is entered. Similarly
3273 -- we kill values after the loop, since we do not know that the body of
3274 -- the loop was executed.
3276 Kill_Current_Values
;
3278 Analyze_Iteration_Scheme
(Iter
);
3280 -- Check for following case which merits a warning if the type E of is
3281 -- a multi-dimensional array (and no explicit subscript ranges present).
3287 and then Present
(Loop_Parameter_Specification
(Iter
))
3290 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
3291 DSD
: constant Node_Id
:=
3292 Original_Node
(Discrete_Subtype_Definition
(LPS
));
3294 if Nkind
(DSD
) = N_Attribute_Reference
3295 and then Attribute_Name
(DSD
) = Name_Range
3296 and then No
(Expressions
(DSD
))
3299 Typ
: constant Entity_Id
:= Etype
(Prefix
(DSD
));
3301 if Is_Array_Type
(Typ
)
3302 and then Number_Dimensions
(Typ
) > 1
3303 and then Nkind
(Parent
(N
)) = N_Loop_Statement
3304 and then Present
(Iteration_Scheme
(Parent
(N
)))
3307 OIter
: constant Node_Id
:=
3308 Iteration_Scheme
(Parent
(N
));
3309 OLPS
: constant Node_Id
:=
3310 Loop_Parameter_Specification
(OIter
);
3311 ODSD
: constant Node_Id
:=
3312 Original_Node
(Discrete_Subtype_Definition
(OLPS
));
3314 if Nkind
(ODSD
) = N_Attribute_Reference
3315 and then Attribute_Name
(ODSD
) = Name_Range
3316 and then No
(Expressions
(ODSD
))
3317 and then Etype
(Prefix
(ODSD
)) = Typ
3319 Error_Msg_Sloc
:= Sloc
(ODSD
);
3321 ("inner range same as outer range#??", DSD
);
3330 -- Analyze the statements of the body except in the case of an Ada 2012
3331 -- iterator with the expander active. In this case the expander will do
3332 -- a rewrite of the loop into a while loop. We will then analyze the
3333 -- loop body when we analyze this while loop.
3335 -- We need to do this delay because if the container is for indefinite
3336 -- types the actual subtype of the components will only be determined
3337 -- when the cursor declaration is analyzed.
3339 -- If the expander is not active, or in SPARK mode, then we want to
3340 -- analyze the loop body now even in the Ada 2012 iterator case, since
3341 -- the rewriting will not be done. Insert the loop variable in the
3342 -- current scope, if not done when analysing the iteration scheme.
3343 -- Set its kind properly to detect improper uses in the loop body.
3346 and then Present
(Iterator_Specification
(Iter
))
3348 if not Expander_Active
then
3350 I_Spec
: constant Node_Id
:= Iterator_Specification
(Iter
);
3351 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3354 if Scope
(Id
) /= Current_Scope
then
3358 -- In an element iterator, The loop parameter is a variable if
3359 -- the domain of iteration (container or array) is a variable.
3361 if not Of_Present
(I_Spec
)
3362 or else not Is_Variable
(Name
(I_Spec
))
3364 Set_Ekind
(Id
, E_Loop_Parameter
);
3368 Analyze_Statements
(Statements
(N
));
3373 -- Pre-Ada2012 for-loops and while loops.
3375 Analyze_Statements
(Statements
(N
));
3378 -- When the iteration scheme of a loop contains attribute 'Loop_Entry,
3379 -- the loop is transformed into a conditional block. Retrieve the loop.
3383 if Subject_To_Loop_Entry_Attributes
(Stmt
) then
3384 Stmt
:= Find_Loop_In_Conditional_Block
(Stmt
);
3387 -- Finish up processing for the loop. We kill all current values, since
3388 -- in general we don't know if the statements in the loop have been
3389 -- executed. We could do a bit better than this with a loop that we
3390 -- know will execute at least once, but it's not worth the trouble and
3391 -- the front end is not in the business of flow tracing.
3393 Process_End_Label
(Stmt
, 'e', Ent
);
3395 Kill_Current_Values
;
3397 -- Check for infinite loop. Skip check for generated code, since it
3398 -- justs waste time and makes debugging the routine called harder.
3400 -- Note that we have to wait till the body of the loop is fully analyzed
3401 -- before making this call, since Check_Infinite_Loop_Warning relies on
3402 -- being able to use semantic visibility information to find references.
3404 if Comes_From_Source
(Stmt
) then
3405 Check_Infinite_Loop_Warning
(Stmt
);
3408 -- Code after loop is unreachable if the loop has no WHILE or FOR and
3409 -- contains no EXIT statements within the body of the loop.
3411 if No
(Iter
) and then not Has_Exit
(Ent
) then
3412 Check_Unreachable_Code
(Stmt
);
3414 end Analyze_Loop_Statement
;
3416 ----------------------------
3417 -- Analyze_Null_Statement --
3418 ----------------------------
3420 -- Note: the semantics of the null statement is implemented by a single
3421 -- null statement, too bad everything isn't as simple as this.
3423 procedure Analyze_Null_Statement
(N
: Node_Id
) is
3424 pragma Warnings
(Off
, N
);
3427 end Analyze_Null_Statement
;
3429 ------------------------
3430 -- Analyze_Statements --
3431 ------------------------
3433 procedure Analyze_Statements
(L
: List_Id
) is
3438 -- The labels declared in the statement list are reachable from
3439 -- statements in the list. We do this as a prepass so that any goto
3440 -- statement will be properly flagged if its target is not reachable.
3441 -- This is not required, but is nice behavior.
3444 while Present
(S
) loop
3445 if Nkind
(S
) = N_Label
then
3446 Analyze
(Identifier
(S
));
3447 Lab
:= Entity
(Identifier
(S
));
3449 -- If we found a label mark it as reachable
3451 if Ekind
(Lab
) = E_Label
then
3452 Generate_Definition
(Lab
);
3453 Set_Reachable
(Lab
);
3455 if Nkind
(Parent
(Lab
)) = N_Implicit_Label_Declaration
then
3456 Set_Label_Construct
(Parent
(Lab
), S
);
3459 -- If we failed to find a label, it means the implicit declaration
3460 -- of the label was hidden. A for-loop parameter can do this to
3461 -- a label with the same name inside the loop, since the implicit
3462 -- label declaration is in the innermost enclosing body or block
3466 Error_Msg_Sloc
:= Sloc
(Lab
);
3468 ("implicit label declaration for & is hidden#",
3476 -- Perform semantic analysis on all statements
3478 Conditional_Statements_Begin
;
3481 while Present
(S
) loop
3484 -- Remove dimension in all statements
3486 Remove_Dimension_In_Statement
(S
);
3490 Conditional_Statements_End
;
3492 -- Make labels unreachable. Visibility is not sufficient, because labels
3493 -- in one if-branch for example are not reachable from the other branch,
3494 -- even though their declarations are in the enclosing declarative part.
3497 while Present
(S
) loop
3498 if Nkind
(S
) = N_Label
then
3499 Set_Reachable
(Entity
(Identifier
(S
)), False);
3504 end Analyze_Statements
;
3506 ----------------------------
3507 -- Check_Unreachable_Code --
3508 ----------------------------
3510 procedure Check_Unreachable_Code
(N
: Node_Id
) is
3511 Error_Node
: Node_Id
;
3515 if Is_List_Member
(N
) and then Comes_From_Source
(N
) then
3520 Nxt
:= Original_Node
(Next
(N
));
3522 -- Skip past pragmas
3524 while Nkind
(Nxt
) = N_Pragma
loop
3525 Nxt
:= Original_Node
(Next
(Nxt
));
3528 -- If a label follows us, then we never have dead code, since
3529 -- someone could branch to the label, so we just ignore it, unless
3530 -- we are in formal mode where goto statements are not allowed.
3532 if Nkind
(Nxt
) = N_Label
3533 and then not Restriction_Check_Required
(SPARK_05
)
3537 -- Otherwise see if we have a real statement following us
3540 and then Comes_From_Source
(Nxt
)
3541 and then Is_Statement
(Nxt
)
3543 -- Special very annoying exception. If we have a return that
3544 -- follows a raise, then we allow it without a warning, since
3545 -- the Ada RM annoyingly requires a useless return here.
3547 if Nkind
(Original_Node
(N
)) /= N_Raise_Statement
3548 or else Nkind
(Nxt
) /= N_Simple_Return_Statement
3550 -- The rather strange shenanigans with the warning message
3551 -- here reflects the fact that Kill_Dead_Code is very good
3552 -- at removing warnings in deleted code, and this is one
3553 -- warning we would prefer NOT to have removed.
3557 -- If we have unreachable code, analyze and remove the
3558 -- unreachable code, since it is useless and we don't
3559 -- want to generate junk warnings.
3561 -- We skip this step if we are not in code generation mode
3562 -- or CodePeer mode.
3564 -- This is the one case where we remove dead code in the
3565 -- semantics as opposed to the expander, and we do not want
3566 -- to remove code if we are not in code generation mode,
3567 -- since this messes up the ASIS trees or loses useful
3568 -- information in the CodePeer tree.
3570 -- Note that one might react by moving the whole circuit to
3571 -- exp_ch5, but then we lose the warning in -gnatc mode.
3573 if Operating_Mode
= Generate_Code
3574 and then not CodePeer_Mode
3579 -- Quit deleting when we have nothing more to delete
3580 -- or if we hit a label (since someone could transfer
3581 -- control to a label, so we should not delete it).
3583 exit when No
(Nxt
) or else Nkind
(Nxt
) = N_Label
;
3585 -- Statement/declaration is to be deleted
3589 Kill_Dead_Code
(Nxt
);
3593 -- Now issue the warning (or error in formal mode)
3595 if Restriction_Check_Required
(SPARK_05
) then
3596 Check_SPARK_05_Restriction
3597 ("unreachable code is not allowed", Error_Node
);
3599 Error_Msg
("??unreachable code!", Sloc
(Error_Node
));
3603 -- If the unconditional transfer of control instruction is the
3604 -- last statement of a sequence, then see if our parent is one of
3605 -- the constructs for which we count unblocked exits, and if so,
3606 -- adjust the count.
3611 -- Statements in THEN part or ELSE part of IF statement
3613 if Nkind
(P
) = N_If_Statement
then
3616 -- Statements in ELSIF part of an IF statement
3618 elsif Nkind
(P
) = N_Elsif_Part
then
3620 pragma Assert
(Nkind
(P
) = N_If_Statement
);
3622 -- Statements in CASE statement alternative
3624 elsif Nkind
(P
) = N_Case_Statement_Alternative
then
3626 pragma Assert
(Nkind
(P
) = N_Case_Statement
);
3628 -- Statements in body of block
3630 elsif Nkind
(P
) = N_Handled_Sequence_Of_Statements
3631 and then Nkind
(Parent
(P
)) = N_Block_Statement
3633 -- The original loop is now placed inside a block statement
3634 -- due to the expansion of attribute 'Loop_Entry. Return as
3635 -- this is not a "real" block for the purposes of exit
3638 if Nkind
(N
) = N_Loop_Statement
3639 and then Subject_To_Loop_Entry_Attributes
(N
)
3644 -- Statements in exception handler in a block
3646 elsif Nkind
(P
) = N_Exception_Handler
3647 and then Nkind
(Parent
(P
)) = N_Handled_Sequence_Of_Statements
3648 and then Nkind
(Parent
(Parent
(P
))) = N_Block_Statement
3652 -- None of these cases, so return
3658 -- This was one of the cases we are looking for (i.e. the
3659 -- parent construct was IF, CASE or block) so decrement count.
3661 Unblocked_Exit_Count
:= Unblocked_Exit_Count
- 1;
3665 end Check_Unreachable_Code
;
3667 ----------------------
3668 -- Preanalyze_Range --
3669 ----------------------
3671 procedure Preanalyze_Range
(R_Copy
: Node_Id
) is
3672 Save_Analysis
: constant Boolean := Full_Analysis
;
3676 Full_Analysis
:= False;
3677 Expander_Mode_Save_And_Set
(False);
3681 if Nkind
(R_Copy
) in N_Subexpr
and then Is_Overloaded
(R_Copy
) then
3683 -- Apply preference rules for range of predefined integer types, or
3684 -- diagnose true ambiguity.
3689 Found
: Entity_Id
:= Empty
;
3692 Get_First_Interp
(R_Copy
, I
, It
);
3693 while Present
(It
.Typ
) loop
3694 if Is_Discrete_Type
(It
.Typ
) then
3698 if Scope
(Found
) = Standard_Standard
then
3701 elsif Scope
(It
.Typ
) = Standard_Standard
then
3705 -- Both of them are user-defined
3708 ("ambiguous bounds in range of iteration", R_Copy
);
3709 Error_Msg_N
("\possible interpretations:", R_Copy
);
3710 Error_Msg_NE
("\\} ", R_Copy
, Found
);
3711 Error_Msg_NE
("\\} ", R_Copy
, It
.Typ
);
3717 Get_Next_Interp
(I
, It
);
3722 -- Subtype mark in iteration scheme
3724 if Is_Entity_Name
(R_Copy
) and then Is_Type
(Entity
(R_Copy
)) then
3727 -- Expression in range, or Ada 2012 iterator
3729 elsif Nkind
(R_Copy
) in N_Subexpr
then
3731 Typ
:= Etype
(R_Copy
);
3733 if Is_Discrete_Type
(Typ
) then
3736 -- Check that the resulting object is an iterable container
3738 elsif Has_Aspect
(Typ
, Aspect_Iterator_Element
)
3739 or else Has_Aspect
(Typ
, Aspect_Constant_Indexing
)
3740 or else Has_Aspect
(Typ
, Aspect_Variable_Indexing
)
3744 -- The expression may yield an implicit reference to an iterable
3745 -- container. Insert explicit dereference so that proper type is
3746 -- visible in the loop.
3748 elsif Has_Implicit_Dereference
(Etype
(R_Copy
)) then
3753 Disc
:= First_Discriminant
(Typ
);
3754 while Present
(Disc
) loop
3755 if Has_Implicit_Dereference
(Disc
) then
3756 Build_Explicit_Dereference
(R_Copy
, Disc
);
3760 Next_Discriminant
(Disc
);
3767 Expander_Mode_Restore
;
3768 Full_Analysis
:= Save_Analysis
;
3769 end Preanalyze_Range
;