1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, 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
;
45 with Sem_Aux
; use Sem_Aux
;
46 with Sem_Case
; use Sem_Case
;
47 with Sem_Ch3
; use Sem_Ch3
;
48 with Sem_Ch6
; use Sem_Ch6
;
49 with Sem_Ch8
; use Sem_Ch8
;
50 with Sem_Dim
; use Sem_Dim
;
51 with Sem_Disp
; use Sem_Disp
;
52 with Sem_Elab
; use Sem_Elab
;
53 with Sem_Eval
; use Sem_Eval
;
54 with Sem_Res
; use Sem_Res
;
55 with Sem_Type
; use Sem_Type
;
56 with Sem_Util
; use Sem_Util
;
57 with Sem_Warn
; use Sem_Warn
;
58 with Snames
; use Snames
;
59 with Stand
; use Stand
;
60 with Sinfo
; use Sinfo
;
61 with Targparm
; use Targparm
;
62 with Tbuild
; use Tbuild
;
63 with Uintp
; use Uintp
;
65 package body Sem_Ch5
is
67 Current_Assignment
: Node_Id
:= Empty
;
68 -- This variable holds the node for an assignment that contains target
69 -- names. The corresponding flag has been set by the parser, and when
70 -- set the analysis of the RHS must be done with all expansion disabled,
71 -- because the assignment is reanalyzed after expansion has replaced all
72 -- occurrences of the target name appropriately.
74 Unblocked_Exit_Count
: Nat
:= 0;
75 -- This variable is used when processing if statements, case statements,
76 -- and block statements. It counts the number of exit points that are not
77 -- blocked by unconditional transfer instructions: for IF and CASE, these
78 -- are the branches of the conditional; for a block, they are the statement
79 -- sequence of the block, and the statement sequences of any exception
80 -- handlers that are part of the block. When processing is complete, if
81 -- this count is zero, it means that control cannot fall through the IF,
82 -- CASE or block statement. This is used for the generation of warning
83 -- messages. This variable is recursively saved on entry to processing the
84 -- construct, and restored on exit.
86 function Has_Call_Using_Secondary_Stack
(N
: Node_Id
) return Boolean;
87 -- N is the node for an arbitrary construct. This function searches the
88 -- construct N to see if any expressions within it contain function
89 -- calls that use the secondary stack, returning True if any such call
90 -- is found, and False otherwise.
92 procedure Preanalyze_Range
(R_Copy
: Node_Id
);
93 -- Determine expected type of range or domain of iteration of Ada 2012
94 -- loop by analyzing separate copy. Do the analysis and resolution of the
95 -- copy of the bound(s) with expansion disabled, to prevent the generation
96 -- of finalization actions. This prevents memory leaks when the bounds
97 -- contain calls to functions returning controlled arrays or when the
98 -- domain of iteration is a container.
100 ------------------------
101 -- Analyze_Assignment --
102 ------------------------
104 -- WARNING: This routine manages Ghost regions. Return statements must be
105 -- replaced by gotos which jump to the end of the routine and restore the
108 procedure Analyze_Assignment
(N
: Node_Id
) is
109 Lhs
: constant Node_Id
:= Name
(N
);
110 Rhs
: Node_Id
:= Expression
(N
);
112 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
);
113 -- N is the node for the left hand side of an assignment, and it is not
114 -- a variable. This routine issues an appropriate diagnostic.
116 function Is_Protected_Part_Of_Constituent
117 (Nod
: Node_Id
) return Boolean;
118 -- Determine whether arbitrary node Nod denotes a Part_Of constituent of
119 -- a single protected type.
122 -- This is called to kill current value settings of a simple variable
123 -- on the left hand side. We call it if we find any error in analyzing
124 -- the assignment, and at the end of processing before setting any new
125 -- current values in place.
127 procedure Set_Assignment_Type
129 Opnd_Type
: in out Entity_Id
);
130 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
131 -- nominal subtype. This procedure is used to deal with cases where the
132 -- nominal subtype must be replaced by the actual subtype.
134 procedure Transform_BIP_Assignment
(Typ
: Entity_Id
);
135 function Should_Transform_BIP_Assignment
136 (Typ
: Entity_Id
) return Boolean;
137 -- If the right-hand side of an assignment statement is a build-in-place
138 -- call we cannot build in place, so we insert a temp initialized with
139 -- the call, and transform the assignment statement to copy the temp.
140 -- Transform_BIP_Assignment does the tranformation, and
141 -- Should_Transform_BIP_Assignment determines whether we should.
142 -- The same goes for qualified expressions and conversions whose
143 -- operand is such a call.
145 -- This is only for nonlimited types; assignment statements are illegal
146 -- for limited types, but are generated internally for aggregates and
147 -- init procs. These limited-type are not really assignment statements
148 -- -- conceptually, they are initializations, so should not be
151 -- Similarly, for nonlimited types, aggregates and init procs generate
152 -- assignment statements that are really initializations. These are
153 -- marked No_Ctrl_Actions.
155 function Within_Function
return Boolean;
156 -- Determine whether the current scope is a function or appears within
159 -------------------------------
160 -- Diagnose_Non_Variable_Lhs --
161 -------------------------------
163 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
) is
165 -- Not worth posting another error if left hand side already flagged
166 -- as being illegal in some respect.
168 if Error_Posted
(N
) then
171 -- Some special bad cases of entity names
173 elsif Is_Entity_Name
(N
) then
175 Ent
: constant Entity_Id
:= Entity
(N
);
178 if Ekind
(Ent
) = E_Loop_Parameter
179 or else Is_Loop_Parameter
(Ent
)
181 Error_Msg_N
("assignment to loop parameter not allowed", N
);
184 elsif Ekind
(Ent
) = E_In_Parameter
then
186 ("assignment to IN mode parameter not allowed", N
);
189 -- Renamings of protected private components are turned into
190 -- constants when compiling a protected function. In the case
191 -- of single protected types, the private component appears
194 elsif (Is_Prival
(Ent
) and then Within_Function
)
196 (Ekind
(Ent
) = E_Component
197 and then Is_Protected_Type
(Scope
(Ent
)))
200 ("protected function cannot modify protected object", N
);
205 -- For indexed components, test prefix if it is in array. We do not
206 -- want to recurse for cases where the prefix is a pointer, since we
207 -- may get a message confusing the pointer and what it references.
209 elsif Nkind
(N
) = N_Indexed_Component
210 and then Is_Array_Type
(Etype
(Prefix
(N
)))
212 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
215 -- Another special case for assignment to discriminant
217 elsif Nkind
(N
) = N_Selected_Component
then
218 if Present
(Entity
(Selector_Name
(N
)))
219 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
221 Error_Msg_N
("assignment to discriminant not allowed", N
);
224 -- For selection from record, diagnose prefix, but note that again
225 -- we only do this for a record, not e.g. for a pointer.
227 elsif Is_Record_Type
(Etype
(Prefix
(N
))) then
228 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
233 -- If we fall through, we have no special message to issue
235 Error_Msg_N
("left hand side of assignment must be a variable", N
);
236 end Diagnose_Non_Variable_Lhs
;
238 --------------------------------------
239 -- Is_Protected_Part_Of_Constituent --
240 --------------------------------------
242 function Is_Protected_Part_Of_Constituent
243 (Nod
: Node_Id
) return Boolean
245 Encap_Id
: Entity_Id
;
249 -- Abstract states and variables may act as Part_Of constituents of
250 -- single protected types, however only variables can be modified by
253 if Is_Entity_Name
(Nod
) then
254 Var_Id
:= Entity
(Nod
);
256 if Present
(Var_Id
) and then Ekind
(Var_Id
) = E_Variable
then
257 Encap_Id
:= Encapsulating_State
(Var_Id
);
259 -- To qualify, the node must denote a reference to a variable
260 -- whose encapsulating state is a single protected object.
264 and then Is_Single_Protected_Object
(Encap_Id
);
269 end Is_Protected_Part_Of_Constituent
;
275 procedure Kill_Lhs
is
277 if Is_Entity_Name
(Lhs
) then
279 Ent
: constant Entity_Id
:= Entity
(Lhs
);
281 if Present
(Ent
) then
282 Kill_Current_Values
(Ent
);
288 -------------------------
289 -- Set_Assignment_Type --
290 -------------------------
292 procedure Set_Assignment_Type
294 Opnd_Type
: in out Entity_Id
)
299 Require_Entity
(Opnd
);
301 -- If the assignment operand is an in-out or out parameter, then we
302 -- get the actual subtype (needed for the unconstrained case). If the
303 -- operand is the actual in an entry declaration, then within the
304 -- accept statement it is replaced with a local renaming, which may
305 -- also have an actual subtype.
307 if Is_Entity_Name
(Opnd
)
308 and then (Ekind
(Entity
(Opnd
)) = E_Out_Parameter
309 or else Ekind_In
(Entity
(Opnd
),
311 E_Generic_In_Out_Parameter
)
313 (Ekind
(Entity
(Opnd
)) = E_Variable
314 and then Nkind
(Parent
(Entity
(Opnd
))) =
315 N_Object_Renaming_Declaration
316 and then Nkind
(Parent
(Parent
(Entity
(Opnd
)))) =
319 Opnd_Type
:= Get_Actual_Subtype
(Opnd
);
321 -- If assignment operand is a component reference, then we get the
322 -- actual subtype of the component for the unconstrained case.
324 elsif Nkind_In
(Opnd
, N_Selected_Component
, N_Explicit_Dereference
)
325 and then not Is_Unchecked_Union
(Opnd_Type
)
327 Decl
:= Build_Actual_Subtype_Of_Component
(Opnd_Type
, Opnd
);
329 if Present
(Decl
) then
330 Insert_Action
(N
, Decl
);
331 Mark_Rewrite_Insertion
(Decl
);
333 Opnd_Type
:= Defining_Identifier
(Decl
);
334 Set_Etype
(Opnd
, Opnd_Type
);
335 Freeze_Itype
(Opnd_Type
, N
);
337 elsif Is_Constrained
(Etype
(Opnd
)) then
338 Opnd_Type
:= Etype
(Opnd
);
341 -- For slice, use the constrained subtype created for the slice
343 elsif Nkind
(Opnd
) = N_Slice
then
344 Opnd_Type
:= Etype
(Opnd
);
346 end Set_Assignment_Type
;
348 -------------------------------------
349 -- Should_Transform_BIP_Assignment --
350 -------------------------------------
352 function Should_Transform_BIP_Assignment
353 (Typ
: Entity_Id
) return Boolean
359 and then not Is_Limited_View
(Typ
)
360 and then Is_Build_In_Place_Result_Type
(Typ
)
361 and then not No_Ctrl_Actions
(N
)
363 -- This function is called early, before name resolution is
364 -- complete, so we have to deal with things that might turn into
365 -- function calls later. N_Function_Call and N_Op nodes are the
366 -- obvious case. An N_Identifier or N_Expanded_Name is a
367 -- parameterless function call if it denotes a function.
368 -- Finally, an attribute reference can be a function call.
370 case Nkind
(Unqual_Conv
(Rhs
)) is
379 case Ekind
(Entity
(Unqual_Conv
(Rhs
))) is
389 when N_Attribute_Reference
=>
390 Result
:= Attribute_Name
(Unqual_Conv
(Rhs
)) = Name_Input
;
391 -- T'Input will turn into a call whose result type is T
401 end Should_Transform_BIP_Assignment
;
403 ------------------------------
404 -- Transform_BIP_Assignment --
405 ------------------------------
407 procedure Transform_BIP_Assignment
(Typ
: Entity_Id
) is
409 -- Tranform "X : [constant] T := F (...);" into:
411 -- Temp : constant T := F (...);
414 Loc
: constant Source_Ptr
:= Sloc
(N
);
415 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'Y', Rhs
);
416 Obj_Decl
: constant Node_Id
:=
417 Make_Object_Declaration
(Loc
,
418 Defining_Identifier
=> Def_Id
,
419 Constant_Present
=> True,
420 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
422 Has_Init_Expression
=> True);
425 Set_Etype
(Def_Id
, Typ
);
426 Set_Expression
(N
, New_Occurrence_Of
(Def_Id
, Loc
));
428 -- At this point, Rhs is no longer equal to Expression (N), so:
430 Rhs
:= Expression
(N
);
432 Insert_Action
(N
, Obj_Decl
);
433 end Transform_BIP_Assignment
;
435 ---------------------
436 -- Within_Function --
437 ---------------------
439 function Within_Function
return Boolean is
440 Scop_Id
: constant Entity_Id
:= Current_Scope
;
443 if Ekind
(Scop_Id
) = E_Function
then
446 elsif Ekind
(Enclosing_Dynamic_Scope
(Scop_Id
)) = E_Function
then
458 Save_Full_Analysis
: Boolean := False;
459 -- Force initialization to facilitate static analysis
461 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
462 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
463 -- Save the Ghost-related attributes to restore on exit
465 -- Start of processing for Analyze_Assignment
468 Mark_Coextensions
(N
, Rhs
);
470 -- Preserve relevant elaboration-related attributes of the context which
471 -- are no longer available or very expensive to recompute once analysis,
472 -- resolution, and expansion are over.
474 Mark_Elaboration_Attributes
479 -- Analyze the target of the assignment first in case the expression
480 -- contains references to Ghost entities. The checks that verify the
481 -- proper use of a Ghost entity need to know the enclosing context.
485 -- An assignment statement is Ghost when the left hand side denotes a
486 -- Ghost entity. Set the mode now to ensure that any nodes generated
487 -- during analysis and expansion are properly marked as Ghost.
489 if Has_Target_Names
(N
) then
490 Current_Assignment
:= N
;
491 Expander_Mode_Save_And_Set
(False);
492 Save_Full_Analysis
:= Full_Analysis
;
493 Full_Analysis
:= False;
495 Current_Assignment
:= Empty
;
498 Mark_And_Set_Ghost_Assignment
(N
);
501 -- Ensure that we never do an assignment on a variable marked as
502 -- Is_Safe_To_Reevaluate.
505 (not Is_Entity_Name
(Lhs
)
506 or else Ekind
(Entity
(Lhs
)) /= E_Variable
507 or else not Is_Safe_To_Reevaluate
(Entity
(Lhs
)));
509 -- Start type analysis for assignment
513 -- In the most general case, both Lhs and Rhs can be overloaded, and we
514 -- must compute the intersection of the possible types on each side.
516 if Is_Overloaded
(Lhs
) then
523 Get_First_Interp
(Lhs
, I
, It
);
525 while Present
(It
.Typ
) loop
527 -- An indexed component with generalized indexing is always
528 -- overloaded with the corresponding dereference. Discard the
529 -- interpretation that yields a reference type, which is not
532 if Nkind
(Lhs
) = N_Indexed_Component
533 and then Present
(Generalized_Indexing
(Lhs
))
534 and then Has_Implicit_Dereference
(It
.Typ
)
538 -- This may be a call to a parameterless function through an
539 -- implicit dereference, so discard interpretation as well.
541 elsif Is_Entity_Name
(Lhs
)
542 and then Has_Implicit_Dereference
(It
.Typ
)
546 elsif Has_Compatible_Type
(Rhs
, It
.Typ
) then
547 if T1
= Any_Type
then
550 -- An explicit dereference is overloaded if the prefix
551 -- is. Try to remove the ambiguity on the prefix, the
552 -- error will be posted there if the ambiguity is real.
554 if Nkind
(Lhs
) = N_Explicit_Dereference
then
557 PI1
: Interp_Index
:= 0;
563 Get_First_Interp
(Prefix
(Lhs
), PI
, PIt
);
565 while Present
(PIt
.Typ
) loop
566 if Is_Access_Type
(PIt
.Typ
)
567 and then Has_Compatible_Type
568 (Rhs
, Designated_Type
(PIt
.Typ
))
572 Disambiguate
(Prefix
(Lhs
),
575 if PIt
= No_Interp
then
577 ("ambiguous left-hand side in "
578 & "assignment", Lhs
);
581 Resolve
(Prefix
(Lhs
), PIt
.Typ
);
591 Get_Next_Interp
(PI
, PIt
);
597 ("ambiguous left-hand side in assignment", Lhs
);
603 Get_Next_Interp
(I
, It
);
607 if T1
= Any_Type
then
609 ("no valid types for left-hand side for assignment", Lhs
);
615 -- Deal with build-in-place calls for nonlimited types. We don't do this
616 -- later, because resolving the rhs tranforms it incorrectly for build-
619 if Should_Transform_BIP_Assignment
(Typ
=> T1
) then
621 -- In certain cases involving user-defined concatenation operators,
622 -- we need to resolve the right-hand side before transforming the
625 case Nkind
(Unqual_Conv
(Rhs
)) is
626 when N_Function_Call
=>
629 First
(Parameter_Associations
(Unqual_Conv
(Rhs
)));
630 Actual_Exp
: Node_Id
;
633 while Present
(Actual
) loop
634 if Nkind
(Actual
) = N_Parameter_Association
then
635 Actual_Exp
:= Explicit_Actual_Parameter
(Actual
);
637 Actual_Exp
:= Actual
;
640 if Nkind
(Actual_Exp
) = N_Op_Concat
then
649 when N_Attribute_Reference
660 Transform_BIP_Assignment
(Typ
=> T1
);
663 pragma Assert
(not Should_Transform_BIP_Assignment
(Typ
=> T1
));
665 -- The resulting assignment type is T1, so now we will resolve the left
666 -- hand side of the assignment using this determined type.
670 -- Cases where Lhs is not a variable. In an instance or an inlined body
671 -- no need for further check because assignment was legal in template.
673 if In_Inlined_Body
then
676 elsif not Is_Variable
(Lhs
) then
678 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
686 if Ada_Version
>= Ada_2005
then
688 -- Handle chains of renamings
691 while Nkind
(Ent
) in N_Has_Entity
692 and then Present
(Entity
(Ent
))
693 and then Present
(Renamed_Object
(Entity
(Ent
)))
695 Ent
:= Renamed_Object
(Entity
(Ent
));
698 if (Nkind
(Ent
) = N_Attribute_Reference
699 and then Attribute_Name
(Ent
) = Name_Priority
)
701 -- Renamings of the attribute Priority applied to protected
702 -- objects have been previously expanded into calls to the
703 -- Get_Ceiling run-time subprogram.
705 or else Is_Expanded_Priority_Attribute
(Ent
)
707 -- The enclosing subprogram cannot be a protected function
710 while not (Is_Subprogram
(S
)
711 and then Convention
(S
) = Convention_Protected
)
712 and then S
/= Standard_Standard
717 if Ekind
(S
) = E_Function
718 and then Convention
(S
) = Convention_Protected
721 ("protected function cannot modify protected object",
725 -- Changes of the ceiling priority of the protected object
726 -- are only effective if the Ceiling_Locking policy is in
727 -- effect (AARM D.5.2 (5/2)).
729 if Locking_Policy
/= 'C' then
731 ("assignment to the attribute PRIORITY has no effect??",
734 ("\since no Locking_Policy has been specified??", Lhs
);
742 Diagnose_Non_Variable_Lhs
(Lhs
);
745 -- Error of assigning to limited type. We do however allow this in
746 -- certain cases where the front end generates the assignments.
748 elsif Is_Limited_Type
(T1
)
749 and then not Assignment_OK
(Lhs
)
750 and then not Assignment_OK
(Original_Node
(Lhs
))
752 -- CPP constructors can only be called in declarations
754 if Is_CPP_Constructor_Call
(Rhs
) then
755 Error_Msg_N
("invalid use of 'C'P'P constructor", Rhs
);
758 ("left hand of assignment must not be limited type", Lhs
);
759 Explain_Limited_Type
(T1
, Lhs
);
764 -- A class-wide type may be a limited view. This illegal case is not
765 -- caught by previous checks.
767 elsif Ekind
(T1
) = E_Class_Wide_Type
and then From_Limited_With
(T1
) then
768 Error_Msg_NE
("invalid use of limited view of&", Lhs
, T1
);
771 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
772 -- abstract. This is only checked when the assignment Comes_From_Source,
773 -- because in some cases the expander generates such assignments (such
774 -- in the _assign operation for an abstract type).
776 elsif Is_Abstract_Type
(T1
) and then Comes_From_Source
(N
) then
778 ("target of assignment operation must not be abstract", Lhs
);
781 -- Variables which are Part_Of constituents of single protected types
782 -- behave in similar fashion to protected components. Such variables
783 -- cannot be modified by protected functions.
785 if Is_Protected_Part_Of_Constituent
(Lhs
) and then Within_Function
then
787 ("protected function cannot modify protected object", Lhs
);
790 -- Resolution may have updated the subtype, in case the left-hand side
791 -- is a private protected component. Use the correct subtype to avoid
792 -- scoping issues in the back-end.
796 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
797 -- type. For example:
801 -- type Acc is access P.T;
804 -- with Pkg; use Acc;
805 -- procedure Example is
808 -- A.all := B.all; -- ERROR
811 if Nkind
(Lhs
) = N_Explicit_Dereference
812 and then Ekind
(T1
) = E_Incomplete_Type
814 Error_Msg_N
("invalid use of incomplete type", Lhs
);
819 -- Now we can complete the resolution of the right hand side
821 Set_Assignment_Type
(Lhs
, T1
);
823 -- If the target of the assignment is an entity of a mutable type and
824 -- the expression is a conditional expression, its alternatives can be
825 -- of different subtypes of the nominal type of the LHS, so they must be
826 -- resolved with the base type, given that their subtype may differ from
827 -- that of the target mutable object.
829 if Is_Entity_Name
(Lhs
)
830 and then Ekind_In
(Entity
(Lhs
), E_In_Out_Parameter
,
833 and then Is_Composite_Type
(T1
)
834 and then not Is_Constrained
(Etype
(Entity
(Lhs
)))
835 and then Nkind_In
(Rhs
, N_If_Expression
, N_Case_Expression
)
837 Resolve
(Rhs
, Base_Type
(T1
));
843 -- This is the point at which we check for an unset reference
845 Check_Unset_Reference
(Rhs
);
846 Check_Unprotected_Access
(Lhs
, Rhs
);
848 -- Remaining steps are skipped if Rhs was syntactically in error
857 if not Covers
(T1
, T2
) then
858 Wrong_Type
(Rhs
, Etype
(Lhs
));
863 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
864 -- types, use the non-limited view if available
866 if Nkind
(Rhs
) = N_Explicit_Dereference
867 and then Is_Tagged_Type
(T2
)
868 and then Has_Non_Limited_View
(T2
)
870 T2
:= Non_Limited_View
(T2
);
873 Set_Assignment_Type
(Rhs
, T2
);
875 if Total_Errors_Detected
/= 0 then
885 if T1
= Any_Type
or else T2
= Any_Type
then
890 -- If the rhs is class-wide or dynamically tagged, then require the lhs
891 -- to be class-wide. The case where the rhs is a dynamically tagged call
892 -- to a dispatching operation with a controlling access result is
893 -- excluded from this check, since the target has an access type (and
894 -- no tag propagation occurs in that case).
896 if (Is_Class_Wide_Type
(T2
)
897 or else (Is_Dynamically_Tagged
(Rhs
)
898 and then not Is_Access_Type
(T1
)))
899 and then not Is_Class_Wide_Type
(T1
)
901 Error_Msg_N
("dynamically tagged expression not allowed!", Rhs
);
903 elsif Is_Class_Wide_Type
(T1
)
904 and then not Is_Class_Wide_Type
(T2
)
905 and then not Is_Tag_Indeterminate
(Rhs
)
906 and then not Is_Dynamically_Tagged
(Rhs
)
908 Error_Msg_N
("dynamically tagged expression required!", Rhs
);
911 -- Propagate the tag from a class-wide target to the rhs when the rhs
912 -- is a tag-indeterminate call.
914 if Is_Tag_Indeterminate
(Rhs
) then
915 if Is_Class_Wide_Type
(T1
) then
916 Propagate_Tag
(Lhs
, Rhs
);
918 elsif Nkind
(Rhs
) = N_Function_Call
919 and then Is_Entity_Name
(Name
(Rhs
))
920 and then Is_Abstract_Subprogram
(Entity
(Name
(Rhs
)))
923 ("call to abstract function must be dispatching", Name
(Rhs
));
925 elsif Nkind
(Rhs
) = N_Qualified_Expression
926 and then Nkind
(Expression
(Rhs
)) = N_Function_Call
927 and then Is_Entity_Name
(Name
(Expression
(Rhs
)))
929 Is_Abstract_Subprogram
(Entity
(Name
(Expression
(Rhs
))))
932 ("call to abstract function must be dispatching",
933 Name
(Expression
(Rhs
)));
937 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
938 -- apply an implicit conversion of the rhs to that type to force
939 -- appropriate static and run-time accessibility checks. This applies
940 -- as well to anonymous access-to-subprogram types that are component
941 -- subtypes or formal parameters.
943 if Ada_Version
>= Ada_2005
and then Is_Access_Type
(T1
) then
944 if Is_Local_Anonymous_Access
(T1
)
945 or else Ekind
(T2
) = E_Anonymous_Access_Subprogram_Type
947 -- Handle assignment to an Ada 2012 stand-alone object
948 -- of an anonymous access type.
950 or else (Ekind
(T1
) = E_Anonymous_Access_Type
951 and then Nkind
(Associated_Node_For_Itype
(T1
)) =
952 N_Object_Declaration
)
955 Rewrite
(Rhs
, Convert_To
(T1
, Relocate_Node
(Rhs
)));
956 Analyze_And_Resolve
(Rhs
, T1
);
960 -- Ada 2005 (AI-231): Assignment to not null variable
962 if Ada_Version
>= Ada_2005
963 and then Can_Never_Be_Null
(T1
)
964 and then not Assignment_OK
(Lhs
)
966 -- Case where we know the right hand side is null
968 if Known_Null
(Rhs
) then
969 Apply_Compile_Time_Constraint_Error
972 "(Ada 2005) null not allowed in null-excluding objects??",
973 Reason
=> CE_Null_Not_Allowed
);
975 -- We still mark this as a possible modification, that's necessary
976 -- to reset Is_True_Constant, and desirable for xref purposes.
978 Note_Possible_Modification
(Lhs
, Sure
=> True);
981 -- If we know the right hand side is non-null, then we convert to the
982 -- target type, since we don't need a run time check in that case.
984 elsif not Can_Never_Be_Null
(T2
) then
985 Rewrite
(Rhs
, Convert_To
(T1
, Relocate_Node
(Rhs
)));
986 Analyze_And_Resolve
(Rhs
, T1
);
990 if Is_Scalar_Type
(T1
) then
991 Apply_Scalar_Range_Check
(Rhs
, Etype
(Lhs
));
993 -- For array types, verify that lengths match. If the right hand side
994 -- is a function call that has been inlined, the assignment has been
995 -- rewritten as a block, and the constraint check will be applied to the
996 -- assignment within the block.
998 elsif Is_Array_Type
(T1
)
999 and then (Nkind
(Rhs
) /= N_Type_Conversion
1000 or else Is_Constrained
(Etype
(Rhs
)))
1001 and then (Nkind
(Rhs
) /= N_Function_Call
1002 or else Nkind
(N
) /= N_Block_Statement
)
1004 -- Assignment verifies that the length of the Lsh and Rhs are equal,
1005 -- but of course the indexes do not have to match. If the right-hand
1006 -- side is a type conversion to an unconstrained type, a length check
1007 -- is performed on the expression itself during expansion. In rare
1008 -- cases, the redundant length check is computed on an index type
1009 -- with a different representation, triggering incorrect code in the
1012 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
1015 -- Discriminant checks are applied in the course of expansion
1020 -- Note: modifications of the Lhs may only be recorded after
1021 -- checks have been applied.
1023 Note_Possible_Modification
(Lhs
, Sure
=> True);
1025 -- ??? a real accessibility check is needed when ???
1027 -- Post warning for redundant assignment or variable to itself
1029 if Warn_On_Redundant_Constructs
1031 -- We only warn for source constructs
1033 and then Comes_From_Source
(N
)
1035 -- Where the object is the same on both sides
1037 and then Same_Object
(Lhs
, Original_Node
(Rhs
))
1039 -- But exclude the case where the right side was an operation that
1040 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
1041 -- don't want to warn in such a case, since it is reasonable to write
1042 -- such expressions especially when K is defined symbolically in some
1045 and then Nkind
(Original_Node
(Rhs
)) not in N_Op
1047 if Nkind
(Lhs
) in N_Has_Entity
then
1048 Error_Msg_NE
-- CODEFIX
1049 ("?r?useless assignment of & to itself!", N
, Entity
(Lhs
));
1051 Error_Msg_N
-- CODEFIX
1052 ("?r?useless assignment of object to itself!", N
);
1056 -- Check for non-allowed composite assignment
1058 if not Support_Composite_Assign_On_Target
1059 and then (Is_Array_Type
(T1
) or else Is_Record_Type
(T1
))
1060 and then (not Has_Size_Clause
(T1
) or else Esize
(T1
) > 64)
1062 Error_Msg_CRT
("composite assignment", N
);
1065 -- Check elaboration warning for left side if not in elab code
1067 if Legacy_Elaboration_Checks
1068 and not In_Subprogram_Or_Concurrent_Unit
1070 Check_Elab_Assign
(Lhs
);
1073 -- Save the scenario for later examination by the ABE Processing phase
1075 Record_Elaboration_Scenario
(N
);
1077 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
1078 -- assignment is a source assignment in the extended main source unit.
1079 -- We are not interested in any reference information outside this
1080 -- context, or in compiler generated assignment statements.
1082 if Comes_From_Source
(N
)
1083 and then In_Extended_Main_Source_Unit
(Lhs
)
1085 Set_Referenced_Modified
(Lhs
, Out_Param
=> False);
1088 -- RM 7.3.2 (12/3): An assignment to a view conversion (from a type to
1089 -- one of its ancestors) requires an invariant check. Apply check only
1090 -- if expression comes from source, otherwise it will be applied when
1091 -- value is assigned to source entity. This is not done in GNATprove
1092 -- mode, as GNATprove handles invariant checks itself.
1094 if Nkind
(Lhs
) = N_Type_Conversion
1095 and then Has_Invariants
(Etype
(Expression
(Lhs
)))
1096 and then Comes_From_Source
(Expression
(Lhs
))
1097 and then not GNATprove_Mode
1099 Insert_After
(N
, Make_Invariant_Call
(Expression
(Lhs
)));
1102 -- Final step. If left side is an entity, then we may be able to reset
1103 -- the current tracked values to new safe values. We only have something
1104 -- to do if the left side is an entity name, and expansion has not
1105 -- modified the node into something other than an assignment, and of
1106 -- course we only capture values if it is safe to do so.
1108 if Is_Entity_Name
(Lhs
)
1109 and then Nkind
(N
) = N_Assignment_Statement
1112 Ent
: constant Entity_Id
:= Entity
(Lhs
);
1115 if Safe_To_Capture_Value
(N
, Ent
) then
1117 -- If simple variable on left side, warn if this assignment
1118 -- blots out another one (rendering it useless). We only do
1119 -- this for source assignments, otherwise we can generate bogus
1120 -- warnings when an assignment is rewritten as another
1121 -- assignment, and gets tied up with itself.
1123 -- There may have been a previous reference to a component of
1124 -- the variable, which in general removes the Last_Assignment
1125 -- field of the variable to indicate a relevant use of the
1126 -- previous assignment. However, if the assignment is to a
1127 -- subcomponent the reference may not have registered, because
1128 -- it is not possible to determine whether the context is an
1129 -- assignment. In those cases we generate a Deferred_Reference,
1130 -- to be used at the end of compilation to generate the right
1131 -- kind of reference, and we suppress a potential warning for
1132 -- a useless assignment, which might be premature. This may
1133 -- lose a warning in rare cases, but seems preferable to a
1134 -- misleading warning.
1136 if Warn_On_Modified_Unread
1137 and then Is_Assignable
(Ent
)
1138 and then Comes_From_Source
(N
)
1139 and then In_Extended_Main_Source_Unit
(Ent
)
1140 and then not Has_Deferred_Reference
(Ent
)
1142 Warn_On_Useless_Assignment
(Ent
, N
);
1145 -- If we are assigning an access type and the left side is an
1146 -- entity, then make sure that the Is_Known_[Non_]Null flags
1147 -- properly reflect the state of the entity after assignment.
1149 if Is_Access_Type
(T1
) then
1150 if Known_Non_Null
(Rhs
) then
1151 Set_Is_Known_Non_Null
(Ent
, True);
1153 elsif Known_Null
(Rhs
)
1154 and then not Can_Never_Be_Null
(Ent
)
1156 Set_Is_Known_Null
(Ent
, True);
1159 Set_Is_Known_Null
(Ent
, False);
1161 if not Can_Never_Be_Null
(Ent
) then
1162 Set_Is_Known_Non_Null
(Ent
, False);
1166 -- For discrete types, we may be able to set the current value
1167 -- if the value is known at compile time.
1169 elsif Is_Discrete_Type
(T1
)
1170 and then Compile_Time_Known_Value
(Rhs
)
1172 Set_Current_Value
(Ent
, Rhs
);
1174 Set_Current_Value
(Ent
, Empty
);
1177 -- If not safe to capture values, kill them
1185 -- If assigning to an object in whole or in part, note location of
1186 -- assignment in case no one references value. We only do this for
1187 -- source assignments, otherwise we can generate bogus warnings when an
1188 -- assignment is rewritten as another assignment, and gets tied up with
1192 Ent
: constant Entity_Id
:= Get_Enclosing_Object
(Lhs
);
1195 and then Safe_To_Capture_Value
(N
, Ent
)
1196 and then Nkind
(N
) = N_Assignment_Statement
1197 and then Warn_On_Modified_Unread
1198 and then Is_Assignable
(Ent
)
1199 and then Comes_From_Source
(N
)
1200 and then In_Extended_Main_Source_Unit
(Ent
)
1202 Set_Last_Assignment
(Ent
, Lhs
);
1206 Analyze_Dimension
(N
);
1209 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
1211 -- If the right-hand side contains target names, expansion has been
1212 -- disabled to prevent expansion that might move target names out of
1213 -- the context of the assignment statement. Restore the expander mode
1214 -- now so that assignment statement can be properly expanded.
1216 if Nkind
(N
) = N_Assignment_Statement
then
1217 if Has_Target_Names
(N
) then
1218 Expander_Mode_Restore
;
1219 Full_Analysis
:= Save_Full_Analysis
;
1222 pragma Assert
(not Should_Transform_BIP_Assignment
(Typ
=> T1
));
1224 end Analyze_Assignment
;
1226 -----------------------------
1227 -- Analyze_Block_Statement --
1228 -----------------------------
1230 procedure Analyze_Block_Statement
(N
: Node_Id
) is
1231 procedure Install_Return_Entities
(Scop
: Entity_Id
);
1232 -- Install all entities of return statement scope Scop in the visibility
1233 -- chain except for the return object since its entity is reused in a
1236 -----------------------------
1237 -- Install_Return_Entities --
1238 -----------------------------
1240 procedure Install_Return_Entities
(Scop
: Entity_Id
) is
1244 Id
:= First_Entity
(Scop
);
1245 while Present
(Id
) loop
1247 -- Do not install the return object
1249 if not Ekind_In
(Id
, E_Constant
, E_Variable
)
1250 or else not Is_Return_Object
(Id
)
1252 Install_Entity
(Id
);
1257 end Install_Return_Entities
;
1259 -- Local constants and variables
1261 Decls
: constant List_Id
:= Declarations
(N
);
1262 Id
: constant Node_Id
:= Identifier
(N
);
1263 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
1265 Is_BIP_Return_Statement
: Boolean;
1267 -- Start of processing for Analyze_Block_Statement
1270 -- In SPARK mode, we reject block statements. Note that the case of
1271 -- block statements generated by the expander is fine.
1273 if Nkind
(Original_Node
(N
)) = N_Block_Statement
then
1274 Check_SPARK_05_Restriction
("block statement is not allowed", N
);
1277 -- If no handled statement sequence is present, things are really messed
1278 -- up, and we just return immediately (defence against previous errors).
1281 Check_Error_Detected
;
1285 -- Detect whether the block is actually a rewritten return statement of
1286 -- a build-in-place function.
1288 Is_BIP_Return_Statement
:=
1290 and then Present
(Entity
(Id
))
1291 and then Ekind
(Entity
(Id
)) = E_Return_Statement
1292 and then Is_Build_In_Place_Function
1293 (Return_Applies_To
(Entity
(Id
)));
1295 -- Normal processing with HSS present
1298 EH
: constant List_Id
:= Exception_Handlers
(HSS
);
1299 Ent
: Entity_Id
:= Empty
;
1302 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1303 -- Recursively save value of this global, will be restored on exit
1306 -- Initialize unblocked exit count for statements of begin block
1307 -- plus one for each exception handler that is present.
1309 Unblocked_Exit_Count
:= 1;
1311 if Present
(EH
) then
1312 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ List_Length
(EH
);
1315 -- If a label is present analyze it and mark it as referenced
1317 if Present
(Id
) then
1321 -- An error defense. If we have an identifier, but no entity, then
1322 -- something is wrong. If previous errors, then just remove the
1323 -- identifier and continue, otherwise raise an exception.
1326 Check_Error_Detected
;
1327 Set_Identifier
(N
, Empty
);
1330 Set_Ekind
(Ent
, E_Block
);
1331 Generate_Reference
(Ent
, N
, ' ');
1332 Generate_Definition
(Ent
);
1334 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
1335 Set_Label_Construct
(Parent
(Ent
), N
);
1340 -- If no entity set, create a label entity
1343 Ent
:= New_Internal_Entity
(E_Block
, Current_Scope
, Sloc
(N
), 'B');
1344 Set_Identifier
(N
, New_Occurrence_Of
(Ent
, Sloc
(N
)));
1345 Set_Parent
(Ent
, N
);
1348 Set_Etype
(Ent
, Standard_Void_Type
);
1349 Set_Block_Node
(Ent
, Identifier
(N
));
1352 -- The block served as an extended return statement. Ensure that any
1353 -- entities created during the analysis and expansion of the return
1354 -- object declaration are once again visible.
1356 if Is_BIP_Return_Statement
then
1357 Install_Return_Entities
(Ent
);
1360 if Present
(Decls
) then
1361 Analyze_Declarations
(Decls
);
1363 Inspect_Deferred_Constant_Completion
(Decls
);
1367 Process_End_Label
(HSS
, 'e', Ent
);
1369 -- If exception handlers are present, then we indicate that enclosing
1370 -- scopes contain a block with handlers. We only need to mark non-
1373 if Present
(EH
) then
1376 Set_Has_Nested_Block_With_Handler
(S
);
1377 exit when Is_Overloadable
(S
)
1378 or else Ekind
(S
) = E_Package
1379 or else Is_Generic_Unit
(S
);
1384 Check_References
(Ent
);
1385 Update_Use_Clause_Chain
;
1388 if Unblocked_Exit_Count
= 0 then
1389 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1390 Check_Unreachable_Code
(N
);
1392 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1395 end Analyze_Block_Statement
;
1397 --------------------------------
1398 -- Analyze_Compound_Statement --
1399 --------------------------------
1401 procedure Analyze_Compound_Statement
(N
: Node_Id
) is
1403 Analyze_List
(Actions
(N
));
1404 end Analyze_Compound_Statement
;
1406 ----------------------------
1407 -- Analyze_Case_Statement --
1408 ----------------------------
1410 procedure Analyze_Case_Statement
(N
: Node_Id
) is
1412 Exp_Type
: Entity_Id
;
1413 Exp_Btype
: Entity_Id
;
1416 Others_Present
: Boolean;
1417 -- Indicates if Others was present
1419 pragma Warnings
(Off
, Last_Choice
);
1420 -- Don't care about assigned value
1422 Statements_Analyzed
: Boolean := False;
1423 -- Set True if at least some statement sequences get analyzed. If False
1424 -- on exit, means we had a serious error that prevented full analysis of
1425 -- the case statement, and as a result it is not a good idea to output
1426 -- warning messages about unreachable code.
1428 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1429 -- Recursively save value of this global, will be restored on exit
1431 procedure Non_Static_Choice_Error
(Choice
: Node_Id
);
1432 -- Error routine invoked by the generic instantiation below when the
1433 -- case statement has a non static choice.
1435 procedure Process_Statements
(Alternative
: Node_Id
);
1436 -- Analyzes the statements associated with a case alternative. Needed
1437 -- by instantiation below.
1439 package Analyze_Case_Choices
is new
1440 Generic_Analyze_Choices
1441 (Process_Associated_Node
=> Process_Statements
);
1442 use Analyze_Case_Choices
;
1443 -- Instantiation of the generic choice analysis package
1445 package Check_Case_Choices
is new
1446 Generic_Check_Choices
1447 (Process_Empty_Choice
=> No_OP
,
1448 Process_Non_Static_Choice
=> Non_Static_Choice_Error
,
1449 Process_Associated_Node
=> No_OP
);
1450 use Check_Case_Choices
;
1451 -- Instantiation of the generic choice processing package
1453 -----------------------------
1454 -- Non_Static_Choice_Error --
1455 -----------------------------
1457 procedure Non_Static_Choice_Error
(Choice
: Node_Id
) is
1459 Flag_Non_Static_Expr
1460 ("choice given in case statement is not static!", Choice
);
1461 end Non_Static_Choice_Error
;
1463 ------------------------
1464 -- Process_Statements --
1465 ------------------------
1467 procedure Process_Statements
(Alternative
: Node_Id
) is
1468 Choices
: constant List_Id
:= Discrete_Choices
(Alternative
);
1472 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
1473 Statements_Analyzed
:= True;
1475 -- An interesting optimization. If the case statement expression
1476 -- is a simple entity, then we can set the current value within an
1477 -- alternative if the alternative has one possible value.
1481 -- when 2 | 3 => beta
1482 -- when others => gamma
1484 -- Here we know that N is initially 1 within alpha, but for beta and
1485 -- gamma, we do not know anything more about the initial value.
1487 if Is_Entity_Name
(Exp
) then
1488 Ent
:= Entity
(Exp
);
1490 if Ekind_In
(Ent
, E_Variable
,
1494 if List_Length
(Choices
) = 1
1495 and then Nkind
(First
(Choices
)) in N_Subexpr
1496 and then Compile_Time_Known_Value
(First
(Choices
))
1498 Set_Current_Value
(Entity
(Exp
), First
(Choices
));
1501 Analyze_Statements
(Statements
(Alternative
));
1503 -- After analyzing the case, set the current value to empty
1504 -- since we won't know what it is for the next alternative
1505 -- (unless reset by this same circuit), or after the case.
1507 Set_Current_Value
(Entity
(Exp
), Empty
);
1512 -- Case where expression is not an entity name of a variable
1514 Analyze_Statements
(Statements
(Alternative
));
1515 end Process_Statements
;
1517 -- Start of processing for Analyze_Case_Statement
1520 Unblocked_Exit_Count
:= 0;
1521 Exp
:= Expression
(N
);
1524 -- The expression must be of any discrete type. In rare cases, the
1525 -- expander constructs a case statement whose expression has a private
1526 -- type whose full view is discrete. This can happen when generating
1527 -- a stream operation for a variant type after the type is frozen,
1528 -- when the partial of view of the type of the discriminant is private.
1529 -- In that case, use the full view to analyze case alternatives.
1531 if not Is_Overloaded
(Exp
)
1532 and then not Comes_From_Source
(N
)
1533 and then Is_Private_Type
(Etype
(Exp
))
1534 and then Present
(Full_View
(Etype
(Exp
)))
1535 and then Is_Discrete_Type
(Full_View
(Etype
(Exp
)))
1537 Resolve
(Exp
, Etype
(Exp
));
1538 Exp_Type
:= Full_View
(Etype
(Exp
));
1541 Analyze_And_Resolve
(Exp
, Any_Discrete
);
1542 Exp_Type
:= Etype
(Exp
);
1545 Check_Unset_Reference
(Exp
);
1546 Exp_Btype
:= Base_Type
(Exp_Type
);
1548 -- The expression must be of a discrete type which must be determinable
1549 -- independently of the context in which the expression occurs, but
1550 -- using the fact that the expression must be of a discrete type.
1551 -- Moreover, the type this expression must not be a character literal
1552 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1554 -- If error already reported by Resolve, nothing more to do
1556 if Exp_Btype
= Any_Discrete
or else Exp_Btype
= Any_Type
then
1559 elsif Exp_Btype
= Any_Character
then
1561 ("character literal as case expression is ambiguous", Exp
);
1564 elsif Ada_Version
= Ada_83
1565 and then (Is_Generic_Type
(Exp_Btype
)
1566 or else Is_Generic_Type
(Root_Type
(Exp_Btype
)))
1569 ("(Ada 83) case expression cannot be of a generic type", Exp
);
1573 -- If the case expression is a formal object of mode in out, then treat
1574 -- it as having a nonstatic subtype by forcing use of the base type
1575 -- (which has to get passed to Check_Case_Choices below). Also use base
1576 -- type when the case expression is parenthesized.
1578 if Paren_Count
(Exp
) > 0
1579 or else (Is_Entity_Name
(Exp
)
1580 and then Ekind
(Entity
(Exp
)) = E_Generic_In_Out_Parameter
)
1582 Exp_Type
:= Exp_Btype
;
1585 -- Call instantiated procedures to analyzwe and check discrete choices
1587 Analyze_Choices
(Alternatives
(N
), Exp_Type
);
1588 Check_Choices
(N
, Alternatives
(N
), Exp_Type
, Others_Present
);
1590 -- Case statement with single OTHERS alternative not allowed in SPARK
1592 if Others_Present
and then List_Length
(Alternatives
(N
)) = 1 then
1593 Check_SPARK_05_Restriction
1594 ("OTHERS as unique case alternative is not allowed", N
);
1597 if Exp_Type
= Universal_Integer
and then not Others_Present
then
1598 Error_Msg_N
("case on universal integer requires OTHERS choice", Exp
);
1601 -- If all our exits were blocked by unconditional transfers of control,
1602 -- then the entire CASE statement acts as an unconditional transfer of
1603 -- control, so treat it like one, and check unreachable code. Skip this
1604 -- test if we had serious errors preventing any statement analysis.
1606 if Unblocked_Exit_Count
= 0 and then Statements_Analyzed
then
1607 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1608 Check_Unreachable_Code
(N
);
1610 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1613 -- If the expander is active it will detect the case of a statically
1614 -- determined single alternative and remove warnings for the case, but
1615 -- if we are not doing expansion, that circuit won't be active. Here we
1616 -- duplicate the effect of removing warnings in the same way, so that
1617 -- we will get the same set of warnings in -gnatc mode.
1619 if not Expander_Active
1620 and then Compile_Time_Known_Value
(Expression
(N
))
1621 and then Serious_Errors_Detected
= 0
1624 Chosen
: constant Node_Id
:= Find_Static_Alternative
(N
);
1628 Alt
:= First
(Alternatives
(N
));
1629 while Present
(Alt
) loop
1630 if Alt
/= Chosen
then
1631 Remove_Warning_Messages
(Statements
(Alt
));
1638 end Analyze_Case_Statement
;
1640 ----------------------------
1641 -- Analyze_Exit_Statement --
1642 ----------------------------
1644 -- If the exit includes a name, it must be the name of a currently open
1645 -- loop. Otherwise there must be an innermost open loop on the stack, to
1646 -- which the statement implicitly refers.
1648 -- Additionally, in SPARK mode:
1650 -- The exit can only name the closest enclosing loop;
1652 -- An exit with a when clause must be directly contained in a loop;
1654 -- An exit without a when clause must be directly contained in an
1655 -- if-statement with no elsif or else, which is itself directly contained
1656 -- in a loop. The exit must be the last statement in the if-statement.
1658 procedure Analyze_Exit_Statement
(N
: Node_Id
) is
1659 Target
: constant Node_Id
:= Name
(N
);
1660 Cond
: constant Node_Id
:= Condition
(N
);
1661 Scope_Id
: Entity_Id
:= Empty
; -- initialize to prevent warning
1667 Check_Unreachable_Code
(N
);
1670 if Present
(Target
) then
1672 U_Name
:= Entity
(Target
);
1674 if not In_Open_Scopes
(U_Name
) or else Ekind
(U_Name
) /= E_Loop
then
1675 Error_Msg_N
("invalid loop name in exit statement", N
);
1679 if Has_Loop_In_Inner_Open_Scopes
(U_Name
) then
1680 Check_SPARK_05_Restriction
1681 ("exit label must name the closest enclosing loop", N
);
1684 Set_Has_Exit
(U_Name
);
1691 for J
in reverse 0 .. Scope_Stack
.Last
loop
1692 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
1693 Kind
:= Ekind
(Scope_Id
);
1695 if Kind
= E_Loop
and then (No
(Target
) or else Scope_Id
= U_Name
) then
1696 Set_Has_Exit
(Scope_Id
);
1699 elsif Kind
= E_Block
1700 or else Kind
= E_Loop
1701 or else Kind
= E_Return_Statement
1707 ("cannot exit from program unit or accept statement", N
);
1712 -- Verify that if present the condition is a Boolean expression
1714 if Present
(Cond
) then
1715 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1716 Check_Unset_Reference
(Cond
);
1719 -- In SPARK mode, verify that the exit statement respects the SPARK
1722 if Present
(Cond
) then
1723 if Nkind
(Parent
(N
)) /= N_Loop_Statement
then
1724 Check_SPARK_05_Restriction
1725 ("exit with when clause must be directly in loop", N
);
1729 if Nkind
(Parent
(N
)) /= N_If_Statement
then
1730 if Nkind
(Parent
(N
)) = N_Elsif_Part
then
1731 Check_SPARK_05_Restriction
1732 ("exit must be in IF without ELSIF", N
);
1734 Check_SPARK_05_Restriction
("exit must be directly in IF", N
);
1737 elsif Nkind
(Parent
(Parent
(N
))) /= N_Loop_Statement
then
1738 Check_SPARK_05_Restriction
1739 ("exit must be in IF directly in loop", N
);
1741 -- First test the presence of ELSE, so that an exit in an ELSE leads
1742 -- to an error mentioning the ELSE.
1744 elsif Present
(Else_Statements
(Parent
(N
))) then
1745 Check_SPARK_05_Restriction
("exit must be in IF without ELSE", N
);
1747 -- An exit in an ELSIF does not reach here, as it would have been
1748 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1750 elsif Present
(Elsif_Parts
(Parent
(N
))) then
1751 Check_SPARK_05_Restriction
("exit must be in IF without ELSIF", N
);
1755 -- Chain exit statement to associated loop entity
1757 Set_Next_Exit_Statement
(N
, First_Exit_Statement
(Scope_Id
));
1758 Set_First_Exit_Statement
(Scope_Id
, N
);
1760 -- Since the exit may take us out of a loop, any previous assignment
1761 -- statement is not useless, so clear last assignment indications. It
1762 -- is OK to keep other current values, since if the exit statement
1763 -- does not exit, then the current values are still valid.
1765 Kill_Current_Values
(Last_Assignment_Only
=> True);
1766 end Analyze_Exit_Statement
;
1768 ----------------------------
1769 -- Analyze_Goto_Statement --
1770 ----------------------------
1772 procedure Analyze_Goto_Statement
(N
: Node_Id
) is
1773 Label
: constant Node_Id
:= Name
(N
);
1774 Scope_Id
: Entity_Id
;
1775 Label_Scope
: Entity_Id
;
1776 Label_Ent
: Entity_Id
;
1779 Check_SPARK_05_Restriction
("goto statement is not allowed", N
);
1781 -- Actual semantic checks
1783 Check_Unreachable_Code
(N
);
1784 Kill_Current_Values
(Last_Assignment_Only
=> True);
1787 Label_Ent
:= Entity
(Label
);
1789 -- Ignore previous error
1791 if Label_Ent
= Any_Id
then
1792 Check_Error_Detected
;
1795 -- We just have a label as the target of a goto
1797 elsif Ekind
(Label_Ent
) /= E_Label
then
1798 Error_Msg_N
("target of goto statement must be a label", Label
);
1801 -- Check that the target of the goto is reachable according to Ada
1802 -- scoping rules. Note: the special gotos we generate for optimizing
1803 -- local handling of exceptions would violate these rules, but we mark
1804 -- such gotos as analyzed when built, so this code is never entered.
1806 elsif not Reachable
(Label_Ent
) then
1807 Error_Msg_N
("target of goto statement is not reachable", Label
);
1811 -- Here if goto passes initial validity checks
1813 Label_Scope
:= Enclosing_Scope
(Label_Ent
);
1815 for J
in reverse 0 .. Scope_Stack
.Last
loop
1816 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
1818 if Label_Scope
= Scope_Id
1819 or else not Ekind_In
(Scope_Id
, E_Block
, E_Loop
, E_Return_Statement
)
1821 if Scope_Id
/= Label_Scope
then
1823 ("cannot exit from program unit or accept statement", N
);
1830 raise Program_Error
;
1831 end Analyze_Goto_Statement
;
1833 --------------------------
1834 -- Analyze_If_Statement --
1835 --------------------------
1837 -- A special complication arises in the analysis of if statements
1839 -- The expander has circuitry to completely delete code that it can tell
1840 -- will not be executed (as a result of compile time known conditions). In
1841 -- the analyzer, we ensure that code that will be deleted in this manner
1842 -- is analyzed but not expanded. This is obviously more efficient, but
1843 -- more significantly, difficulties arise if code is expanded and then
1844 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1845 -- generated in deleted code must be frozen from start, because the nodes
1846 -- on which they depend will not be available at the freeze point.
1848 procedure Analyze_If_Statement
(N
: Node_Id
) is
1851 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1852 -- Recursively save value of this global, will be restored on exit
1854 Save_In_Deleted_Code
: Boolean;
1856 Del
: Boolean := False;
1857 -- This flag gets set True if a True condition has been found, which
1858 -- means that remaining ELSE/ELSIF parts are deleted.
1860 procedure Analyze_Cond_Then
(Cnode
: Node_Id
);
1861 -- This is applied to either the N_If_Statement node itself or to an
1862 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1863 -- statements associated with it.
1865 -----------------------
1866 -- Analyze_Cond_Then --
1867 -----------------------
1869 procedure Analyze_Cond_Then
(Cnode
: Node_Id
) is
1870 Cond
: constant Node_Id
:= Condition
(Cnode
);
1871 Tstm
: constant List_Id
:= Then_Statements
(Cnode
);
1874 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
1875 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1876 Check_Unset_Reference
(Cond
);
1877 Set_Current_Value_Condition
(Cnode
);
1879 -- If already deleting, then just analyze then statements
1882 Analyze_Statements
(Tstm
);
1884 -- Compile time known value, not deleting yet
1886 elsif Compile_Time_Known_Value
(Cond
) then
1887 Save_In_Deleted_Code
:= In_Deleted_Code
;
1889 -- If condition is True, then analyze the THEN statements and set
1890 -- no expansion for ELSE and ELSIF parts.
1892 if Is_True
(Expr_Value
(Cond
)) then
1893 Analyze_Statements
(Tstm
);
1895 Expander_Mode_Save_And_Set
(False);
1896 In_Deleted_Code
:= True;
1898 -- If condition is False, analyze THEN with expansion off
1900 else -- Is_False (Expr_Value (Cond))
1901 Expander_Mode_Save_And_Set
(False);
1902 In_Deleted_Code
:= True;
1903 Analyze_Statements
(Tstm
);
1904 Expander_Mode_Restore
;
1905 In_Deleted_Code
:= Save_In_Deleted_Code
;
1908 -- Not known at compile time, not deleting, normal analysis
1911 Analyze_Statements
(Tstm
);
1913 end Analyze_Cond_Then
;
1915 -- Start of processing for Analyze_If_Statement
1918 -- Initialize exit count for else statements. If there is no else part,
1919 -- this count will stay non-zero reflecting the fact that the uncovered
1920 -- else case is an unblocked exit.
1922 Unblocked_Exit_Count
:= 1;
1923 Analyze_Cond_Then
(N
);
1925 -- Now to analyze the elsif parts if any are present
1927 if Present
(Elsif_Parts
(N
)) then
1928 E
:= First
(Elsif_Parts
(N
));
1929 while Present
(E
) loop
1930 Analyze_Cond_Then
(E
);
1935 if Present
(Else_Statements
(N
)) then
1936 Analyze_Statements
(Else_Statements
(N
));
1939 -- If all our exits were blocked by unconditional transfers of control,
1940 -- then the entire IF statement acts as an unconditional transfer of
1941 -- control, so treat it like one, and check unreachable code.
1943 if Unblocked_Exit_Count
= 0 then
1944 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1945 Check_Unreachable_Code
(N
);
1947 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1951 Expander_Mode_Restore
;
1952 In_Deleted_Code
:= Save_In_Deleted_Code
;
1955 if not Expander_Active
1956 and then Compile_Time_Known_Value
(Condition
(N
))
1957 and then Serious_Errors_Detected
= 0
1959 if Is_True
(Expr_Value
(Condition
(N
))) then
1960 Remove_Warning_Messages
(Else_Statements
(N
));
1962 if Present
(Elsif_Parts
(N
)) then
1963 E
:= First
(Elsif_Parts
(N
));
1964 while Present
(E
) loop
1965 Remove_Warning_Messages
(Then_Statements
(E
));
1971 Remove_Warning_Messages
(Then_Statements
(N
));
1975 -- Warn on redundant if statement that has no effect
1977 -- Note, we could also check empty ELSIF parts ???
1979 if Warn_On_Redundant_Constructs
1981 -- If statement must be from source
1983 and then Comes_From_Source
(N
)
1985 -- Condition must not have obvious side effect
1987 and then Has_No_Obvious_Side_Effects
(Condition
(N
))
1989 -- No elsif parts of else part
1991 and then No
(Elsif_Parts
(N
))
1992 and then No
(Else_Statements
(N
))
1994 -- Then must be a single null statement
1996 and then List_Length
(Then_Statements
(N
)) = 1
1998 -- Go to original node, since we may have rewritten something as
1999 -- a null statement (e.g. a case we could figure the outcome of).
2002 T
: constant Node_Id
:= First
(Then_Statements
(N
));
2003 S
: constant Node_Id
:= Original_Node
(T
);
2006 if Comes_From_Source
(S
) and then Nkind
(S
) = N_Null_Statement
then
2007 Error_Msg_N
("if statement has no effect?r?", N
);
2011 end Analyze_If_Statement
;
2013 ----------------------------------------
2014 -- Analyze_Implicit_Label_Declaration --
2015 ----------------------------------------
2017 -- An implicit label declaration is generated in the innermost enclosing
2018 -- declarative part. This is done for labels, and block and loop names.
2020 -- Note: any changes in this routine may need to be reflected in
2021 -- Analyze_Label_Entity.
2023 procedure Analyze_Implicit_Label_Declaration
(N
: Node_Id
) is
2024 Id
: constant Node_Id
:= Defining_Identifier
(N
);
2027 Set_Ekind
(Id
, E_Label
);
2028 Set_Etype
(Id
, Standard_Void_Type
);
2029 Set_Enclosing_Scope
(Id
, Current_Scope
);
2030 end Analyze_Implicit_Label_Declaration
;
2032 ------------------------------
2033 -- Analyze_Iteration_Scheme --
2034 ------------------------------
2036 procedure Analyze_Iteration_Scheme
(N
: Node_Id
) is
2038 Iter_Spec
: Node_Id
;
2039 Loop_Spec
: Node_Id
;
2042 -- For an infinite loop, there is no iteration scheme
2048 Cond
:= Condition
(N
);
2049 Iter_Spec
:= Iterator_Specification
(N
);
2050 Loop_Spec
:= Loop_Parameter_Specification
(N
);
2052 if Present
(Cond
) then
2053 Analyze_And_Resolve
(Cond
, Any_Boolean
);
2054 Check_Unset_Reference
(Cond
);
2055 Set_Current_Value_Condition
(N
);
2057 elsif Present
(Iter_Spec
) then
2058 Analyze_Iterator_Specification
(Iter_Spec
);
2061 Analyze_Loop_Parameter_Specification
(Loop_Spec
);
2063 end Analyze_Iteration_Scheme
;
2065 ------------------------------------
2066 -- Analyze_Iterator_Specification --
2067 ------------------------------------
2069 procedure Analyze_Iterator_Specification
(N
: Node_Id
) is
2070 Def_Id
: constant Node_Id
:= Defining_Identifier
(N
);
2071 Iter_Name
: constant Node_Id
:= Name
(N
);
2072 Loc
: constant Source_Ptr
:= Sloc
(N
);
2073 Subt
: constant Node_Id
:= Subtype_Indication
(N
);
2075 Bas
: Entity_Id
:= Empty
; -- initialize to prevent warning
2078 procedure Check_Reverse_Iteration
(Typ
: Entity_Id
);
2079 -- For an iteration over a container, if the loop carries the Reverse
2080 -- indicator, verify that the container type has an Iterate aspect that
2081 -- implements the reversible iterator interface.
2083 procedure Check_Subtype_Indication
(Comp_Type
: Entity_Id
);
2084 -- If a subtype indication is present, verify that it is consistent
2085 -- with the component type of the array or container name.
2087 function Get_Cursor_Type
(Typ
: Entity_Id
) return Entity_Id
;
2088 -- For containers with Iterator and related aspects, the cursor is
2089 -- obtained by locating an entity with the proper name in the scope
2092 -----------------------------
2093 -- Check_Reverse_Iteration --
2094 -----------------------------
2096 procedure Check_Reverse_Iteration
(Typ
: Entity_Id
) is
2098 if Reverse_Present
(N
) then
2099 if Is_Array_Type
(Typ
)
2100 or else Is_Reversible_Iterator
(Typ
)
2102 (Present
(Find_Aspect
(Typ
, Aspect_Iterable
))
2105 (Get_Iterable_Type_Primitive
(Typ
, Name_Previous
)))
2110 ("container type does not support reverse iteration", N
, Typ
);
2113 end Check_Reverse_Iteration
;
2115 -------------------------------
2116 -- Check_Subtype_Indication --
2117 -------------------------------
2119 procedure Check_Subtype_Indication
(Comp_Type
: Entity_Id
) is
2122 and then (not Covers
(Base_Type
((Bas
)), Comp_Type
)
2123 or else not Subtypes_Statically_Match
(Bas
, Comp_Type
))
2125 if Is_Array_Type
(Typ
) then
2127 ("subtype indication does not match component type", Subt
);
2130 ("subtype indication does not match element type", Subt
);
2133 end Check_Subtype_Indication
;
2135 ---------------------
2136 -- Get_Cursor_Type --
2137 ---------------------
2139 function Get_Cursor_Type
(Typ
: Entity_Id
) return Entity_Id
is
2143 -- If iterator type is derived, the cursor is declared in the scope
2144 -- of the parent type.
2146 if Is_Derived_Type
(Typ
) then
2147 Ent
:= First_Entity
(Scope
(Etype
(Typ
)));
2149 Ent
:= First_Entity
(Scope
(Typ
));
2152 while Present
(Ent
) loop
2153 exit when Chars
(Ent
) = Name_Cursor
;
2161 -- The cursor is the target of generated assignments in the
2162 -- loop, and cannot have a limited type.
2164 if Is_Limited_Type
(Etype
(Ent
)) then
2165 Error_Msg_N
("cursor type cannot be limited", N
);
2169 end Get_Cursor_Type
;
2171 -- Start of processing for Analyze_Iterator_Specification
2174 Enter_Name
(Def_Id
);
2176 -- AI12-0151 specifies that when the subtype indication is present, it
2177 -- must statically match the type of the array or container element.
2178 -- To simplify this check, we introduce a subtype declaration with the
2179 -- given subtype indication when it carries a constraint, and rewrite
2180 -- the original as a reference to the created subtype entity.
2182 if Present
(Subt
) then
2183 if Nkind
(Subt
) = N_Subtype_Indication
then
2185 S
: constant Entity_Id
:= Make_Temporary
(Sloc
(Subt
), 'S');
2186 Decl
: constant Node_Id
:=
2187 Make_Subtype_Declaration
(Loc
,
2188 Defining_Identifier
=> S
,
2189 Subtype_Indication
=> New_Copy_Tree
(Subt
));
2191 Insert_Before
(Parent
(Parent
(N
)), Decl
);
2193 Rewrite
(Subt
, New_Occurrence_Of
(S
, Sloc
(Subt
)));
2199 -- Save entity of subtype indication for subsequent check
2201 Bas
:= Entity
(Subt
);
2204 Preanalyze_Range
(Iter_Name
);
2206 -- Set the kind of the loop variable, which is not visible within the
2209 Set_Ekind
(Def_Id
, E_Variable
);
2211 -- Provide a link between the iterator variable and the container, for
2212 -- subsequent use in cross-reference and modification information.
2214 if Of_Present
(N
) then
2215 Set_Related_Expression
(Def_Id
, Iter_Name
);
2217 -- For a container, the iterator is specified through the aspect
2219 if not Is_Array_Type
(Etype
(Iter_Name
)) then
2221 Iterator
: constant Entity_Id
:=
2222 Find_Value_Of_Aspect
2223 (Etype
(Iter_Name
), Aspect_Default_Iterator
);
2229 if No
(Iterator
) then
2230 null; -- error reported below
2232 elsif not Is_Overloaded
(Iterator
) then
2233 Check_Reverse_Iteration
(Etype
(Iterator
));
2235 -- If Iterator is overloaded, use reversible iterator if one is
2238 elsif Is_Overloaded
(Iterator
) then
2239 Get_First_Interp
(Iterator
, I
, It
);
2240 while Present
(It
.Nam
) loop
2241 if Ekind
(It
.Nam
) = E_Function
2242 and then Is_Reversible_Iterator
(Etype
(It
.Nam
))
2244 Set_Etype
(Iterator
, It
.Typ
);
2245 Set_Entity
(Iterator
, It
.Nam
);
2249 Get_Next_Interp
(I
, It
);
2252 Check_Reverse_Iteration
(Etype
(Iterator
));
2258 -- If the domain of iteration is an expression, create a declaration for
2259 -- it, so that finalization actions are introduced outside of the loop.
2260 -- The declaration must be a renaming because the body of the loop may
2261 -- assign to elements.
2263 if not Is_Entity_Name
(Iter_Name
)
2265 -- When the context is a quantified expression, the renaming
2266 -- declaration is delayed until the expansion phase if we are
2269 and then (Nkind
(Parent
(N
)) /= N_Quantified_Expression
2270 or else Operating_Mode
= Check_Semantics
)
2272 -- Do not perform this expansion for ASIS and when expansion is
2273 -- disabled, where the temporary may hide the transformation of a
2274 -- selected component into a prefixed function call, and references
2275 -- need to see the original expression.
2277 and then Expander_Active
2280 Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R', Iter_Name
);
2286 -- If the domain of iteration is an array component that depends
2287 -- on a discriminant, create actual subtype for it. preanalysis
2288 -- does not generate the actual subtype of a selected component.
2290 if Nkind
(Iter_Name
) = N_Selected_Component
2291 and then Is_Array_Type
(Etype
(Iter_Name
))
2294 Build_Actual_Subtype_Of_Component
2295 (Etype
(Selector_Name
(Iter_Name
)), Iter_Name
);
2296 Insert_Action
(N
, Act_S
);
2298 if Present
(Act_S
) then
2299 Typ
:= Defining_Identifier
(Act_S
);
2301 Typ
:= Etype
(Iter_Name
);
2305 Typ
:= Etype
(Iter_Name
);
2307 -- Verify that the expression produces an iterator
2309 if not Of_Present
(N
) and then not Is_Iterator
(Typ
)
2310 and then not Is_Array_Type
(Typ
)
2311 and then No
(Find_Aspect
(Typ
, Aspect_Iterable
))
2314 ("expect object that implements iterator interface",
2319 -- Protect against malformed iterator
2321 if Typ
= Any_Type
then
2322 Error_Msg_N
("invalid expression in loop iterator", Iter_Name
);
2326 if not Of_Present
(N
) then
2327 Check_Reverse_Iteration
(Typ
);
2330 -- The name in the renaming declaration may be a function call.
2331 -- Indicate that it does not come from source, to suppress
2332 -- spurious warnings on renamings of parameterless functions,
2333 -- a common enough idiom in user-defined iterators.
2336 Make_Object_Renaming_Declaration
(Loc
,
2337 Defining_Identifier
=> Id
,
2338 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
2340 New_Copy_Tree
(Iter_Name
, New_Sloc
=> Loc
));
2342 Insert_Actions
(Parent
(Parent
(N
)), New_List
(Decl
));
2343 Rewrite
(Name
(N
), New_Occurrence_Of
(Id
, Loc
));
2344 Set_Etype
(Id
, Typ
);
2345 Set_Etype
(Name
(N
), Typ
);
2348 -- Container is an entity or an array with uncontrolled components, or
2349 -- else it is a container iterator given by a function call, typically
2350 -- called Iterate in the case of predefined containers, even though
2351 -- Iterate is not a reserved name. What matters is that the return type
2352 -- of the function is an iterator type.
2354 elsif Is_Entity_Name
(Iter_Name
) then
2355 Analyze
(Iter_Name
);
2357 if Nkind
(Iter_Name
) = N_Function_Call
then
2359 C
: constant Node_Id
:= Name
(Iter_Name
);
2364 if not Is_Overloaded
(Iter_Name
) then
2365 Resolve
(Iter_Name
, Etype
(C
));
2368 Get_First_Interp
(C
, I
, It
);
2369 while It
.Typ
/= Empty
loop
2370 if Reverse_Present
(N
) then
2371 if Is_Reversible_Iterator
(It
.Typ
) then
2372 Resolve
(Iter_Name
, It
.Typ
);
2376 elsif Is_Iterator
(It
.Typ
) then
2377 Resolve
(Iter_Name
, It
.Typ
);
2381 Get_Next_Interp
(I
, It
);
2386 -- Domain of iteration is not overloaded
2389 Resolve
(Iter_Name
, Etype
(Iter_Name
));
2392 if not Of_Present
(N
) then
2393 Check_Reverse_Iteration
(Etype
(Iter_Name
));
2397 -- Get base type of container, for proper retrieval of Cursor type
2398 -- and primitive operations.
2400 Typ
:= Base_Type
(Etype
(Iter_Name
));
2402 if Is_Array_Type
(Typ
) then
2403 if Of_Present
(N
) then
2404 Set_Etype
(Def_Id
, Component_Type
(Typ
));
2406 -- The loop variable is aliased if the array components are
2409 Set_Is_Aliased
(Def_Id
, Has_Aliased_Components
(Typ
));
2411 -- AI12-0047 stipulates that the domain (array or container)
2412 -- cannot be a component that depends on a discriminant if the
2413 -- enclosing object is mutable, to prevent a modification of the
2414 -- dowmain of iteration in the course of an iteration.
2416 -- If the object is an expression it has been captured in a
2417 -- temporary, so examine original node.
2419 if Nkind
(Original_Node
(Iter_Name
)) = N_Selected_Component
2420 and then Is_Dependent_Component_Of_Mutable_Object
2421 (Original_Node
(Iter_Name
))
2424 ("iterable name cannot be a discriminant-dependent "
2425 & "component of a mutable object", N
);
2428 Check_Subtype_Indication
(Component_Type
(Typ
));
2430 -- Here we have a missing Range attribute
2434 ("missing Range attribute in iteration over an array", N
);
2436 -- In Ada 2012 mode, this may be an attempt at an iterator
2438 if Ada_Version
>= Ada_2012
then
2440 ("\if& is meant to designate an element of the array, use OF",
2444 -- Prevent cascaded errors
2446 Set_Ekind
(Def_Id
, E_Loop_Parameter
);
2447 Set_Etype
(Def_Id
, Etype
(First_Index
(Typ
)));
2450 -- Check for type error in iterator
2452 elsif Typ
= Any_Type
then
2455 -- Iteration over a container
2458 Set_Ekind
(Def_Id
, E_Loop_Parameter
);
2459 Error_Msg_Ada_2012_Feature
("container iterator", Sloc
(N
));
2463 if Of_Present
(N
) then
2464 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2466 Elt
: constant Entity_Id
:=
2467 Get_Iterable_Type_Primitive
(Typ
, Name_Element
);
2471 ("missing Element primitive for iteration", N
);
2473 Set_Etype
(Def_Id
, Etype
(Elt
));
2474 Check_Reverse_Iteration
(Typ
);
2478 Check_Subtype_Indication
(Etype
(Def_Id
));
2480 -- For a predefined container, The type of the loop variable is
2481 -- the Iterator_Element aspect of the container type.
2485 Element
: constant Entity_Id
:=
2486 Find_Value_Of_Aspect
2487 (Typ
, Aspect_Iterator_Element
);
2488 Iterator
: constant Entity_Id
:=
2489 Find_Value_Of_Aspect
2490 (Typ
, Aspect_Default_Iterator
);
2491 Orig_Iter_Name
: constant Node_Id
:=
2492 Original_Node
(Iter_Name
);
2493 Cursor_Type
: Entity_Id
;
2496 if No
(Element
) then
2497 Error_Msg_NE
("cannot iterate over&", N
, Typ
);
2501 Set_Etype
(Def_Id
, Entity
(Element
));
2502 Cursor_Type
:= Get_Cursor_Type
(Typ
);
2503 pragma Assert
(Present
(Cursor_Type
));
2505 Check_Subtype_Indication
(Etype
(Def_Id
));
2507 -- If the container has a variable indexing aspect, the
2508 -- element is a variable and is modifiable in the loop.
2510 if Has_Aspect
(Typ
, Aspect_Variable_Indexing
) then
2511 Set_Ekind
(Def_Id
, E_Variable
);
2514 -- If the container is a constant, iterating over it
2515 -- requires a Constant_Indexing operation.
2517 if not Is_Variable
(Iter_Name
)
2518 and then not Has_Aspect
(Typ
, Aspect_Constant_Indexing
)
2521 ("iteration over constant container require "
2522 & "constant_indexing aspect", N
);
2524 -- The Iterate function may have an in_out parameter,
2525 -- and a constant container is thus illegal.
2527 elsif Present
(Iterator
)
2528 and then Ekind
(Entity
(Iterator
)) = E_Function
2529 and then Ekind
(First_Formal
(Entity
(Iterator
))) /=
2531 and then not Is_Variable
(Iter_Name
)
2533 Error_Msg_N
("variable container expected", N
);
2536 -- Detect a case where the iterator denotes a component
2537 -- of a mutable object which depends on a discriminant.
2538 -- Note that the iterator may denote a function call in
2539 -- qualified form, in which case this check should not
2542 if Nkind
(Orig_Iter_Name
) = N_Selected_Component
2544 Present
(Entity
(Selector_Name
(Orig_Iter_Name
)))
2546 (Entity
(Selector_Name
(Orig_Iter_Name
)),
2549 and then Is_Dependent_Component_Of_Mutable_Object
2553 ("container cannot be a discriminant-dependent "
2554 & "component of a mutable object", N
);
2560 -- IN iterator, domain is a range, or a call to Iterate function
2563 -- For an iteration of the form IN, the name must denote an
2564 -- iterator, typically the result of a call to Iterate. Give a
2565 -- useful error message when the name is a container by itself.
2567 -- The type may be a formal container type, which has to have
2568 -- an Iterable aspect detailing the required primitives.
2570 if Is_Entity_Name
(Original_Node
(Name
(N
)))
2571 and then not Is_Iterator
(Typ
)
2573 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2576 elsif not Has_Aspect
(Typ
, Aspect_Iterator_Element
) then
2578 ("cannot iterate over&", Name
(N
), Typ
);
2581 ("name must be an iterator, not a container", Name
(N
));
2584 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2588 ("\to iterate directly over the elements of a container, "
2589 & "write `of &`", Name
(N
), Original_Node
(Name
(N
)));
2591 -- No point in continuing analysis of iterator spec
2597 -- If the name is a call (typically prefixed) to some Iterate
2598 -- function, it has been rewritten as an object declaration.
2599 -- If that object is a selected component, verify that it is not
2600 -- a component of an unconstrained mutable object.
2602 if Nkind
(Iter_Name
) = N_Identifier
2603 or else (not Expander_Active
and Comes_From_Source
(Iter_Name
))
2606 Orig_Node
: constant Node_Id
:= Original_Node
(Iter_Name
);
2607 Iter_Kind
: constant Node_Kind
:= Nkind
(Orig_Node
);
2611 if Iter_Kind
= N_Selected_Component
then
2612 Obj
:= Prefix
(Orig_Node
);
2614 elsif Iter_Kind
= N_Function_Call
then
2615 Obj
:= First_Actual
(Orig_Node
);
2617 -- If neither, the name comes from source
2623 if Nkind
(Obj
) = N_Selected_Component
2624 and then Is_Dependent_Component_Of_Mutable_Object
(Obj
)
2627 ("container cannot be a discriminant-dependent "
2628 & "component of a mutable object", N
);
2633 -- The result type of Iterate function is the classwide type of
2634 -- the interface parent. We need the specific Cursor type defined
2635 -- in the container package. We obtain it by name for a predefined
2636 -- container, or through the Iterable aspect for a formal one.
2638 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2641 (Parent
(Find_Value_Of_Aspect
(Typ
, Aspect_Iterable
)),
2645 Set_Etype
(Def_Id
, Get_Cursor_Type
(Typ
));
2646 Check_Reverse_Iteration
(Etype
(Iter_Name
));
2651 end Analyze_Iterator_Specification
;
2657 -- Note: the semantic work required for analyzing labels (setting them as
2658 -- reachable) was done in a prepass through the statements in the block,
2659 -- so that forward gotos would be properly handled. See Analyze_Statements
2660 -- for further details. The only processing required here is to deal with
2661 -- optimizations that depend on an assumption of sequential control flow,
2662 -- since of course the occurrence of a label breaks this assumption.
2664 procedure Analyze_Label
(N
: Node_Id
) is
2665 pragma Warnings
(Off
, N
);
2667 Kill_Current_Values
;
2670 --------------------------
2671 -- Analyze_Label_Entity --
2672 --------------------------
2674 procedure Analyze_Label_Entity
(E
: Entity_Id
) is
2676 Set_Ekind
(E
, E_Label
);
2677 Set_Etype
(E
, Standard_Void_Type
);
2678 Set_Enclosing_Scope
(E
, Current_Scope
);
2679 Set_Reachable
(E
, True);
2680 end Analyze_Label_Entity
;
2682 ------------------------------------------
2683 -- Analyze_Loop_Parameter_Specification --
2684 ------------------------------------------
2686 procedure Analyze_Loop_Parameter_Specification
(N
: Node_Id
) is
2687 Loop_Nod
: constant Node_Id
:= Parent
(Parent
(N
));
2689 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
);
2690 -- If the bounds are given by a 'Range reference on a function call
2691 -- that returns a controlled array, introduce an explicit declaration
2692 -- to capture the bounds, so that the function result can be finalized
2693 -- in timely fashion.
2695 procedure Check_Predicate_Use
(T
: Entity_Id
);
2696 -- Diagnose Attempt to iterate through non-static predicate. Note that
2697 -- a type with inherited predicates may have both static and dynamic
2698 -- forms. In this case it is not sufficent to check the static predicate
2699 -- function only, look for a dynamic predicate aspect as well.
2701 procedure Process_Bounds
(R
: Node_Id
);
2702 -- If the iteration is given by a range, create temporaries and
2703 -- assignment statements block to capture the bounds and perform
2704 -- required finalization actions in case a bound includes a function
2705 -- call that uses the temporary stack. We first preanalyze a copy of
2706 -- the range in order to determine the expected type, and analyze and
2707 -- resolve the original bounds.
2709 --------------------------------------
2710 -- Check_Controlled_Array_Attribute --
2711 --------------------------------------
2713 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
) is
2715 if Nkind
(DS
) = N_Attribute_Reference
2716 and then Is_Entity_Name
(Prefix
(DS
))
2717 and then Ekind
(Entity
(Prefix
(DS
))) = E_Function
2718 and then Is_Array_Type
(Etype
(Entity
(Prefix
(DS
))))
2720 Is_Controlled
(Component_Type
(Etype
(Entity
(Prefix
(DS
)))))
2721 and then Expander_Active
2724 Loc
: constant Source_Ptr
:= Sloc
(N
);
2725 Arr
: constant Entity_Id
:= Etype
(Entity
(Prefix
(DS
)));
2726 Indx
: constant Entity_Id
:=
2727 Base_Type
(Etype
(First_Index
(Arr
)));
2728 Subt
: constant Entity_Id
:= Make_Temporary
(Loc
, 'S');
2733 Make_Subtype_Declaration
(Loc
,
2734 Defining_Identifier
=> Subt
,
2735 Subtype_Indication
=>
2736 Make_Subtype_Indication
(Loc
,
2737 Subtype_Mark
=> New_Occurrence_Of
(Indx
, Loc
),
2739 Make_Range_Constraint
(Loc
, Relocate_Node
(DS
))));
2740 Insert_Before
(Loop_Nod
, Decl
);
2744 Make_Attribute_Reference
(Loc
,
2745 Prefix
=> New_Occurrence_Of
(Subt
, Loc
),
2746 Attribute_Name
=> Attribute_Name
(DS
)));
2751 end Check_Controlled_Array_Attribute
;
2753 -------------------------
2754 -- Check_Predicate_Use --
2755 -------------------------
2757 procedure Check_Predicate_Use
(T
: Entity_Id
) is
2759 -- A predicated subtype is illegal in loops and related constructs
2760 -- if the predicate is not static, or if it is a non-static subtype
2761 -- of a statically predicated subtype.
2763 if Is_Discrete_Type
(T
)
2764 and then Has_Predicates
(T
)
2765 and then (not Has_Static_Predicate
(T
)
2766 or else not Is_Static_Subtype
(T
)
2767 or else Has_Dynamic_Predicate_Aspect
(T
))
2769 -- Seems a confusing message for the case of a static predicate
2770 -- with a non-static subtype???
2772 Bad_Predicated_Subtype_Use
2773 ("cannot use subtype& with non-static predicate for loop "
2774 & "iteration", Discrete_Subtype_Definition
(N
),
2775 T
, Suggest_Static
=> True);
2777 elsif Inside_A_Generic
2778 and then Is_Generic_Formal
(T
)
2779 and then Is_Discrete_Type
(T
)
2781 Set_No_Dynamic_Predicate_On_Actual
(T
);
2783 end Check_Predicate_Use
;
2785 --------------------
2786 -- Process_Bounds --
2787 --------------------
2789 procedure Process_Bounds
(R
: Node_Id
) is
2790 Loc
: constant Source_Ptr
:= Sloc
(N
);
2793 (Original_Bound
: Node_Id
;
2794 Analyzed_Bound
: Node_Id
;
2795 Typ
: Entity_Id
) return Node_Id
;
2796 -- Capture value of bound and return captured value
2803 (Original_Bound
: Node_Id
;
2804 Analyzed_Bound
: Node_Id
;
2805 Typ
: Entity_Id
) return Node_Id
2812 -- If the bound is a constant or an object, no need for a separate
2813 -- declaration. If the bound is the result of previous expansion
2814 -- it is already analyzed and should not be modified. Note that
2815 -- the Bound will be resolved later, if needed, as part of the
2816 -- call to Make_Index (literal bounds may need to be resolved to
2819 if Analyzed
(Original_Bound
) then
2820 return Original_Bound
;
2822 elsif Nkind_In
(Analyzed_Bound
, N_Integer_Literal
,
2823 N_Character_Literal
)
2824 or else Is_Entity_Name
(Analyzed_Bound
)
2826 Analyze_And_Resolve
(Original_Bound
, Typ
);
2827 return Original_Bound
;
2830 -- Normally, the best approach is simply to generate a constant
2831 -- declaration that captures the bound. However, there is a nasty
2832 -- case where this is wrong. If the bound is complex, and has a
2833 -- possible use of the secondary stack, we need to generate a
2834 -- separate assignment statement to ensure the creation of a block
2835 -- which will release the secondary stack.
2837 -- We prefer the constant declaration, since it leaves us with a
2838 -- proper trace of the value, useful in optimizations that get rid
2839 -- of junk range checks.
2841 if not Has_Call_Using_Secondary_Stack
(Analyzed_Bound
) then
2842 Analyze_And_Resolve
(Original_Bound
, Typ
);
2844 -- Ensure that the bound is valid. This check should not be
2845 -- generated when the range belongs to a quantified expression
2846 -- as the construct is still not expanded into its final form.
2848 if Nkind
(Parent
(R
)) /= N_Loop_Parameter_Specification
2849 or else Nkind
(Parent
(Parent
(R
))) /= N_Quantified_Expression
2851 Ensure_Valid
(Original_Bound
);
2854 Force_Evaluation
(Original_Bound
);
2855 return Original_Bound
;
2858 Id
:= Make_Temporary
(Loc
, 'R', Original_Bound
);
2860 -- Here we make a declaration with a separate assignment
2861 -- statement, and insert before loop header.
2864 Make_Object_Declaration
(Loc
,
2865 Defining_Identifier
=> Id
,
2866 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
2869 Make_Assignment_Statement
(Loc
,
2870 Name
=> New_Occurrence_Of
(Id
, Loc
),
2871 Expression
=> Relocate_Node
(Original_Bound
));
2873 Insert_Actions
(Loop_Nod
, New_List
(Decl
, Assign
));
2875 -- Now that this temporary variable is initialized we decorate it
2876 -- as safe-to-reevaluate to inform to the backend that no further
2877 -- asignment will be issued and hence it can be handled as side
2878 -- effect free. Note that this decoration must be done when the
2879 -- assignment has been analyzed because otherwise it will be
2880 -- rejected (see Analyze_Assignment).
2882 Set_Is_Safe_To_Reevaluate
(Id
);
2884 Rewrite
(Original_Bound
, New_Occurrence_Of
(Id
, Loc
));
2886 if Nkind
(Assign
) = N_Assignment_Statement
then
2887 return Expression
(Assign
);
2889 return Original_Bound
;
2893 Hi
: constant Node_Id
:= High_Bound
(R
);
2894 Lo
: constant Node_Id
:= Low_Bound
(R
);
2895 R_Copy
: constant Node_Id
:= New_Copy_Tree
(R
);
2900 -- Start of processing for Process_Bounds
2903 Set_Parent
(R_Copy
, Parent
(R
));
2904 Preanalyze_Range
(R_Copy
);
2905 Typ
:= Etype
(R_Copy
);
2907 -- If the type of the discrete range is Universal_Integer, then the
2908 -- bound's type must be resolved to Integer, and any object used to
2909 -- hold the bound must also have type Integer, unless the literal
2910 -- bounds are constant-folded expressions with a user-defined type.
2912 if Typ
= Universal_Integer
then
2913 if Nkind
(Lo
) = N_Integer_Literal
2914 and then Present
(Etype
(Lo
))
2915 and then Scope
(Etype
(Lo
)) /= Standard_Standard
2919 elsif Nkind
(Hi
) = N_Integer_Literal
2920 and then Present
(Etype
(Hi
))
2921 and then Scope
(Etype
(Hi
)) /= Standard_Standard
2926 Typ
:= Standard_Integer
;
2932 New_Lo
:= One_Bound
(Lo
, Low_Bound
(R_Copy
), Typ
);
2933 New_Hi
:= One_Bound
(Hi
, High_Bound
(R_Copy
), Typ
);
2935 -- Propagate staticness to loop range itself, in case the
2936 -- corresponding subtype is static.
2938 if New_Lo
/= Lo
and then Is_OK_Static_Expression
(New_Lo
) then
2939 Rewrite
(Low_Bound
(R
), New_Copy
(New_Lo
));
2942 if New_Hi
/= Hi
and then Is_OK_Static_Expression
(New_Hi
) then
2943 Rewrite
(High_Bound
(R
), New_Copy
(New_Hi
));
2949 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(N
);
2950 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2954 -- Start of processing for Analyze_Loop_Parameter_Specification
2959 -- We always consider the loop variable to be referenced, since the loop
2960 -- may be used just for counting purposes.
2962 Generate_Reference
(Id
, N
, ' ');
2964 -- Check for the case of loop variable hiding a local variable (used
2965 -- later on to give a nice warning if the hidden variable is never
2969 H
: constant Entity_Id
:= Homonym
(Id
);
2972 and then Ekind
(H
) = E_Variable
2973 and then Is_Discrete_Type
(Etype
(H
))
2974 and then Enclosing_Dynamic_Scope
(H
) = Enclosing_Dynamic_Scope
(Id
)
2976 Set_Hiding_Loop_Variable
(H
, Id
);
2980 -- Loop parameter specification must include subtype mark in SPARK
2982 if Nkind
(DS
) = N_Range
then
2983 Check_SPARK_05_Restriction
2984 ("loop parameter specification must include subtype mark", N
);
2987 -- Analyze the subtype definition and create temporaries for the bounds.
2988 -- Do not evaluate the range when preanalyzing a quantified expression
2989 -- because bounds expressed as function calls with side effects will be
2990 -- incorrectly replicated.
2992 if Nkind
(DS
) = N_Range
2993 and then Expander_Active
2994 and then Nkind
(Parent
(N
)) /= N_Quantified_Expression
2996 Process_Bounds
(DS
);
2998 -- Either the expander not active or the range of iteration is a subtype
2999 -- indication, an entity, or a function call that yields an aggregate or
3003 DS_Copy
:= New_Copy_Tree
(DS
);
3004 Set_Parent
(DS_Copy
, Parent
(DS
));
3005 Preanalyze_Range
(DS_Copy
);
3007 -- Ada 2012: If the domain of iteration is:
3009 -- a) a function call,
3010 -- b) an identifier that is not a type,
3011 -- c) an attribute reference 'Old (within a postcondition),
3012 -- d) an unchecked conversion or a qualified expression with
3013 -- the proper iterator type.
3015 -- then it is an iteration over a container. It was classified as
3016 -- a loop specification by the parser, and must be rewritten now
3017 -- to activate container iteration. The last case will occur within
3018 -- an expanded inlined call, where the expansion wraps an actual in
3019 -- an unchecked conversion when needed. The expression of the
3020 -- conversion is always an object.
3022 if Nkind
(DS_Copy
) = N_Function_Call
3024 or else (Is_Entity_Name
(DS_Copy
)
3025 and then not Is_Type
(Entity
(DS_Copy
)))
3027 or else (Nkind
(DS_Copy
) = N_Attribute_Reference
3028 and then Nam_In
(Attribute_Name
(DS_Copy
),
3029 Name_Loop_Entry
, Name_Old
))
3031 or else Has_Aspect
(Etype
(DS_Copy
), Aspect_Iterable
)
3033 or else Nkind
(DS_Copy
) = N_Unchecked_Type_Conversion
3034 or else (Nkind
(DS_Copy
) = N_Qualified_Expression
3035 and then Is_Iterator
(Etype
(DS_Copy
)))
3037 -- This is an iterator specification. Rewrite it as such and
3038 -- analyze it to capture function calls that may require
3039 -- finalization actions.
3042 I_Spec
: constant Node_Id
:=
3043 Make_Iterator_Specification
(Sloc
(N
),
3044 Defining_Identifier
=> Relocate_Node
(Id
),
3046 Subtype_Indication
=> Empty
,
3047 Reverse_Present
=> Reverse_Present
(N
));
3048 Scheme
: constant Node_Id
:= Parent
(N
);
3051 Set_Iterator_Specification
(Scheme
, I_Spec
);
3052 Set_Loop_Parameter_Specification
(Scheme
, Empty
);
3053 Analyze_Iterator_Specification
(I_Spec
);
3055 -- In a generic context, analyze the original domain of
3056 -- iteration, for name capture.
3058 if not Expander_Active
then
3062 -- Set kind of loop parameter, which may be used in the
3063 -- subsequent analysis of the condition in a quantified
3066 Set_Ekind
(Id
, E_Loop_Parameter
);
3070 -- Domain of iteration is not a function call, and is side-effect
3074 -- A quantified expression that appears in a pre/post condition
3075 -- is preanalyzed several times. If the range is given by an
3076 -- attribute reference it is rewritten as a range, and this is
3077 -- done even with expansion disabled. If the type is already set
3078 -- do not reanalyze, because a range with static bounds may be
3079 -- typed Integer by default.
3081 if Nkind
(Parent
(N
)) = N_Quantified_Expression
3082 and then Present
(Etype
(DS
))
3095 -- Some additional checks if we are iterating through a type
3097 if Is_Entity_Name
(DS
)
3098 and then Present
(Entity
(DS
))
3099 and then Is_Type
(Entity
(DS
))
3101 -- The subtype indication may denote the completion of an incomplete
3102 -- type declaration.
3104 if Ekind
(Entity
(DS
)) = E_Incomplete_Type
then
3105 Set_Entity
(DS
, Get_Full_View
(Entity
(DS
)));
3106 Set_Etype
(DS
, Entity
(DS
));
3109 Check_Predicate_Use
(Entity
(DS
));
3112 -- Error if not discrete type
3114 if not Is_Discrete_Type
(Etype
(DS
)) then
3115 Wrong_Type
(DS
, Any_Discrete
);
3116 Set_Etype
(DS
, Any_Type
);
3119 Check_Controlled_Array_Attribute
(DS
);
3121 if Nkind
(DS
) = N_Subtype_Indication
then
3122 Check_Predicate_Use
(Entity
(Subtype_Mark
(DS
)));
3125 Make_Index
(DS
, N
, In_Iter_Schm
=> True);
3126 Set_Ekind
(Id
, E_Loop_Parameter
);
3128 -- A quantified expression which appears in a pre- or post-condition may
3129 -- be analyzed multiple times. The analysis of the range creates several
3130 -- itypes which reside in different scopes depending on whether the pre-
3131 -- or post-condition has been expanded. Update the type of the loop
3132 -- variable to reflect the proper itype at each stage of analysis.
3135 or else Etype
(Id
) = Any_Type
3137 (Present
(Etype
(Id
))
3138 and then Is_Itype
(Etype
(Id
))
3139 and then Nkind
(Parent
(Loop_Nod
)) = N_Expression_With_Actions
3140 and then Nkind
(Original_Node
(Parent
(Loop_Nod
))) =
3141 N_Quantified_Expression
)
3143 Set_Etype
(Id
, Etype
(DS
));
3146 -- Treat a range as an implicit reference to the type, to inhibit
3147 -- spurious warnings.
3149 Generate_Reference
(Base_Type
(Etype
(DS
)), N
, ' ');
3150 Set_Is_Known_Valid
(Id
, True);
3152 -- The loop is not a declarative part, so the loop variable must be
3153 -- frozen explicitly. Do not freeze while preanalyzing a quantified
3154 -- expression because the freeze node will not be inserted into the
3155 -- tree due to flag Is_Spec_Expression being set.
3157 if Nkind
(Parent
(N
)) /= N_Quantified_Expression
then
3159 Flist
: constant List_Id
:= Freeze_Entity
(Id
, N
);
3161 if Is_Non_Empty_List
(Flist
) then
3162 Insert_Actions
(N
, Flist
);
3167 -- Case where we have a range or a subtype, get type bounds
3169 if Nkind_In
(DS
, N_Range
, N_Subtype_Indication
)
3170 and then not Error_Posted
(DS
)
3171 and then Etype
(DS
) /= Any_Type
3172 and then Is_Discrete_Type
(Etype
(DS
))
3179 if Nkind
(DS
) = N_Range
then
3180 L
:= Low_Bound
(DS
);
3181 H
:= High_Bound
(DS
);
3184 Type_Low_Bound
(Underlying_Type
(Etype
(Subtype_Mark
(DS
))));
3186 Type_High_Bound
(Underlying_Type
(Etype
(Subtype_Mark
(DS
))));
3189 -- Check for null or possibly null range and issue warning. We
3190 -- suppress such messages in generic templates and instances,
3191 -- because in practice they tend to be dubious in these cases. The
3192 -- check applies as well to rewritten array element loops where a
3193 -- null range may be detected statically.
3195 if Compile_Time_Compare
(L
, H
, Assume_Valid
=> True) = GT
then
3197 -- Suppress the warning if inside a generic template or
3198 -- instance, since in practice they tend to be dubious in these
3199 -- cases since they can result from intended parameterization.
3201 if not Inside_A_Generic
and then not In_Instance
then
3203 -- Specialize msg if invalid values could make the loop
3204 -- non-null after all.
3206 if Compile_Time_Compare
3207 (L
, H
, Assume_Valid
=> False) = GT
3209 -- Since we know the range of the loop is null, set the
3210 -- appropriate flag to remove the loop entirely during
3213 Set_Is_Null_Loop
(Loop_Nod
);
3215 if Comes_From_Source
(N
) then
3217 ("??loop range is null, loop will not execute", DS
);
3220 -- Here is where the loop could execute because of
3221 -- invalid values, so issue appropriate message and in
3222 -- this case we do not set the Is_Null_Loop flag since
3223 -- the loop may execute.
3225 elsif Comes_From_Source
(N
) then
3227 ("??loop range may be null, loop may not execute",
3230 ("??can only execute if invalid values are present",
3235 -- In either case, suppress warnings in the body of the loop,
3236 -- since it is likely that these warnings will be inappropriate
3237 -- if the loop never actually executes, which is likely.
3239 Set_Suppress_Loop_Warnings
(Loop_Nod
);
3241 -- The other case for a warning is a reverse loop where the
3242 -- upper bound is the integer literal zero or one, and the
3243 -- lower bound may exceed this value.
3245 -- For example, we have
3247 -- for J in reverse N .. 1 loop
3249 -- In practice, this is very likely to be a case of reversing
3250 -- the bounds incorrectly in the range.
3252 elsif Reverse_Present
(N
)
3253 and then Nkind
(Original_Node
(H
)) = N_Integer_Literal
3255 (Intval
(Original_Node
(H
)) = Uint_0
3257 Intval
(Original_Node
(H
)) = Uint_1
)
3259 -- Lower bound may in fact be known and known not to exceed
3260 -- upper bound (e.g. reverse 0 .. 1) and that's OK.
3262 if Compile_Time_Known_Value
(L
)
3263 and then Expr_Value
(L
) <= Expr_Value
(H
)
3267 -- Otherwise warning is warranted
3270 Error_Msg_N
("??loop range may be null", DS
);
3271 Error_Msg_N
("\??bounds may be wrong way round", DS
);
3275 -- Check if either bound is known to be outside the range of the
3276 -- loop parameter type, this is e.g. the case of a loop from
3277 -- 20..X where the type is 1..19.
3279 -- Such a loop is dubious since either it raises CE or it executes
3280 -- zero times, and that cannot be useful!
3282 if Etype
(DS
) /= Any_Type
3283 and then not Error_Posted
(DS
)
3284 and then Nkind
(DS
) = N_Subtype_Indication
3285 and then Nkind
(Constraint
(DS
)) = N_Range_Constraint
3288 LLo
: constant Node_Id
:=
3289 Low_Bound
(Range_Expression
(Constraint
(DS
)));
3290 LHi
: constant Node_Id
:=
3291 High_Bound
(Range_Expression
(Constraint
(DS
)));
3293 Bad_Bound
: Node_Id
:= Empty
;
3294 -- Suspicious loop bound
3297 -- At this stage L, H are the bounds of the type, and LLo
3298 -- Lhi are the low bound and high bound of the loop.
3300 if Compile_Time_Compare
(LLo
, L
, Assume_Valid
=> True) = LT
3302 Compile_Time_Compare
(LLo
, H
, Assume_Valid
=> True) = GT
3307 if Compile_Time_Compare
(LHi
, L
, Assume_Valid
=> True) = LT
3309 Compile_Time_Compare
(LHi
, H
, Assume_Valid
=> True) = GT
3314 if Present
(Bad_Bound
) then
3316 ("suspicious loop bound out of range of "
3317 & "loop subtype??", Bad_Bound
);
3319 ("\loop executes zero times or raises "
3320 & "Constraint_Error??", Bad_Bound
);
3325 -- This declare block is about warnings, if we get an exception while
3326 -- testing for warnings, we simply abandon the attempt silently. This
3327 -- most likely occurs as the result of a previous error, but might
3328 -- just be an obscure case we have missed. In either case, not giving
3329 -- the warning is perfectly acceptable.
3332 when others => null;
3336 -- A loop parameter cannot be effectively volatile (SPARK RM 7.1.3(4)).
3337 -- This check is relevant only when SPARK_Mode is on as it is not a
3338 -- standard Ada legality check.
3340 if SPARK_Mode
= On
and then Is_Effectively_Volatile
(Id
) then
3341 Error_Msg_N
("loop parameter cannot be volatile", Id
);
3343 end Analyze_Loop_Parameter_Specification
;
3345 ----------------------------
3346 -- Analyze_Loop_Statement --
3347 ----------------------------
3349 procedure Analyze_Loop_Statement
(N
: Node_Id
) is
3351 function Is_Container_Iterator
(Iter
: Node_Id
) return Boolean;
3352 -- Given a loop iteration scheme, determine whether it is an Ada 2012
3353 -- container iteration.
3355 function Is_Wrapped_In_Block
(N
: Node_Id
) return Boolean;
3356 -- Determine whether loop statement N has been wrapped in a block to
3357 -- capture finalization actions that may be generated for container
3358 -- iterators. Prevents infinite recursion when block is analyzed.
3359 -- Routine is a noop if loop is single statement within source block.
3361 ---------------------------
3362 -- Is_Container_Iterator --
3363 ---------------------------
3365 function Is_Container_Iterator
(Iter
: Node_Id
) return Boolean is
3374 elsif Present
(Condition
(Iter
)) then
3377 -- for Def_Id in [reverse] Name loop
3378 -- for Def_Id [: Subtype_Indication] of [reverse] Name loop
3380 elsif Present
(Iterator_Specification
(Iter
)) then
3382 Nam
: constant Node_Id
:= Name
(Iterator_Specification
(Iter
));
3386 Nam_Copy
:= New_Copy_Tree
(Nam
);
3387 Set_Parent
(Nam_Copy
, Parent
(Nam
));
3388 Preanalyze_Range
(Nam_Copy
);
3390 -- The only two options here are iteration over a container or
3393 return not Is_Array_Type
(Etype
(Nam_Copy
));
3396 -- for Def_Id in [reverse] Discrete_Subtype_Definition loop
3400 LP
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
3401 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(LP
);
3405 DS_Copy
:= New_Copy_Tree
(DS
);
3406 Set_Parent
(DS_Copy
, Parent
(DS
));
3407 Preanalyze_Range
(DS_Copy
);
3409 -- Check for a call to Iterate () or an expression with
3410 -- an iterator type.
3413 (Nkind
(DS_Copy
) = N_Function_Call
3414 and then Needs_Finalization
(Etype
(DS_Copy
)))
3415 or else Is_Iterator
(Etype
(DS_Copy
));
3418 end Is_Container_Iterator
;
3420 -------------------------
3421 -- Is_Wrapped_In_Block --
3422 -------------------------
3424 function Is_Wrapped_In_Block
(N
: Node_Id
) return Boolean is
3430 -- Check if current scope is a block that is not a transient block.
3432 if Ekind
(Current_Scope
) /= E_Block
3433 or else No
(Block_Node
(Current_Scope
))
3439 Handled_Statement_Sequence
(Parent
(Block_Node
(Current_Scope
)));
3441 -- Skip leading pragmas that may be introduced for invariant and
3442 -- predicate checks.
3444 Stat
:= First
(Statements
(HSS
));
3445 while Present
(Stat
) and then Nkind
(Stat
) = N_Pragma
loop
3446 Stat
:= Next
(Stat
);
3449 return Stat
= N
and then No
(Next
(Stat
));
3451 end Is_Wrapped_In_Block
;
3453 -- Local declarations
3455 Id
: constant Node_Id
:= Identifier
(N
);
3456 Iter
: constant Node_Id
:= Iteration_Scheme
(N
);
3457 Loc
: constant Source_Ptr
:= Sloc
(N
);
3461 -- Start of processing for Analyze_Loop_Statement
3464 if Present
(Id
) then
3466 -- Make name visible, e.g. for use in exit statements. Loop labels
3467 -- are always considered to be referenced.
3472 -- Guard against serious error (typically, a scope mismatch when
3473 -- semantic analysis is requested) by creating loop entity to
3474 -- continue analysis.
3477 if Total_Errors_Detected
/= 0 then
3478 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Loc
, 'L');
3480 raise Program_Error
;
3483 -- Verify that the loop name is hot hidden by an unrelated
3484 -- declaration in an inner scope.
3486 elsif Ekind
(Ent
) /= E_Label
and then Ekind
(Ent
) /= E_Loop
then
3487 Error_Msg_Sloc
:= Sloc
(Ent
);
3488 Error_Msg_N
("implicit label declaration for & is hidden#", Id
);
3490 if Present
(Homonym
(Ent
))
3491 and then Ekind
(Homonym
(Ent
)) = E_Label
3493 Set_Entity
(Id
, Ent
);
3494 Set_Ekind
(Ent
, E_Loop
);
3498 Generate_Reference
(Ent
, N
, ' ');
3499 Generate_Definition
(Ent
);
3501 -- If we found a label, mark its type. If not, ignore it, since it
3502 -- means we have a conflicting declaration, which would already
3503 -- have been diagnosed at declaration time. Set Label_Construct
3504 -- of the implicit label declaration, which is not created by the
3505 -- parser for generic units.
3507 if Ekind
(Ent
) = E_Label
then
3508 Set_Ekind
(Ent
, E_Loop
);
3510 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
3511 Set_Label_Construct
(Parent
(Ent
), N
);
3516 -- Case of no identifier present. Create one and attach it to the
3517 -- loop statement for use as a scope and as a reference for later
3518 -- expansions. Indicate that the label does not come from source,
3519 -- and attach it to the loop statement so it is part of the tree,
3520 -- even without a full declaration.
3523 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Loc
, 'L');
3524 Set_Etype
(Ent
, Standard_Void_Type
);
3525 Set_Identifier
(N
, New_Occurrence_Of
(Ent
, Loc
));
3526 Set_Parent
(Ent
, N
);
3527 Set_Has_Created_Identifier
(N
);
3530 -- If the iterator specification has a syntactic error, transform
3531 -- construct into an infinite loop to prevent a crash and perform
3535 and then Present
(Iterator_Specification
(Iter
))
3536 and then Error_Posted
(Iterator_Specification
(Iter
))
3538 Set_Iteration_Scheme
(N
, Empty
);
3543 -- Iteration over a container in Ada 2012 involves the creation of a
3544 -- controlled iterator object. Wrap the loop in a block to ensure the
3545 -- timely finalization of the iterator and release of container locks.
3546 -- The same applies to the use of secondary stack when obtaining an
3549 if Ada_Version
>= Ada_2012
3550 and then Is_Container_Iterator
(Iter
)
3551 and then not Is_Wrapped_In_Block
(N
)
3554 Block_Nod
: Node_Id
;
3555 Block_Id
: Entity_Id
;
3559 Make_Block_Statement
(Loc
,
3560 Declarations
=> New_List
,
3561 Handled_Statement_Sequence
=>
3562 Make_Handled_Sequence_Of_Statements
(Loc
,
3563 Statements
=> New_List
(Relocate_Node
(N
))));
3565 Add_Block_Identifier
(Block_Nod
, Block_Id
);
3567 -- The expansion of iterator loops generates an iterator in order
3568 -- to traverse the elements of a container:
3570 -- Iter : <iterator type> := Iterate (Container)'reference;
3572 -- The iterator is controlled and returned on the secondary stack.
3573 -- The analysis of the call to Iterate establishes a transient
3574 -- scope to deal with the secondary stack management, but never
3575 -- really creates a physical block as this would kill the iterator
3576 -- too early (see Wrap_Transient_Declaration). To address this
3577 -- case, mark the generated block as needing secondary stack
3580 Set_Uses_Sec_Stack
(Block_Id
);
3582 Rewrite
(N
, Block_Nod
);
3588 -- Wrap the loop in a block when the evaluation of the loop iterator
3589 -- relies on the secondary stack. Required to ensure releasing the
3590 -- secondary stack as soon as the loop completes.
3593 and then Present
(Loop_Parameter_Specification
(Iter
))
3594 and then not Is_Wrapped_In_Block
(N
)
3597 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
3598 DSD
: constant Node_Id
:=
3599 Original_Node
(Discrete_Subtype_Definition
(LPS
));
3601 Block_Id
: Entity_Id
;
3602 Block_Nod
: Node_Id
;
3607 if Nkind
(DSD
) = N_Subtype_Indication
3608 and then Nkind
(Range_Expression
(Constraint
(DSD
))) = N_Range
3612 (Low_Bound
(Range_Expression
(Constraint
(DSD
))));
3615 (High_Bound
(Range_Expression
(Constraint
(DSD
))));
3620 if Has_Call_Using_Secondary_Stack
(LB
)
3621 or else Has_Call_Using_Secondary_Stack
(HB
)
3624 Make_Block_Statement
(Loc
,
3625 Declarations
=> New_List
,
3626 Handled_Statement_Sequence
=>
3627 Make_Handled_Sequence_Of_Statements
(Loc
,
3628 Statements
=> New_List
(Relocate_Node
(N
))));
3630 Add_Block_Identifier
(Block_Nod
, Block_Id
);
3631 Set_Uses_Sec_Stack
(Block_Id
);
3632 Rewrite
(N
, Block_Nod
);
3640 -- Kill current values on entry to loop, since statements in the body of
3641 -- the loop may have been executed before the loop is entered. Similarly
3642 -- we kill values after the loop, since we do not know that the body of
3643 -- the loop was executed.
3645 Kill_Current_Values
;
3647 Analyze_Iteration_Scheme
(Iter
);
3649 -- Check for following case which merits a warning if the type E of is
3650 -- a multi-dimensional array (and no explicit subscript ranges present).
3656 and then Present
(Loop_Parameter_Specification
(Iter
))
3659 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
3660 DSD
: constant Node_Id
:=
3661 Original_Node
(Discrete_Subtype_Definition
(LPS
));
3663 if Nkind
(DSD
) = N_Attribute_Reference
3664 and then Attribute_Name
(DSD
) = Name_Range
3665 and then No
(Expressions
(DSD
))
3668 Typ
: constant Entity_Id
:= Etype
(Prefix
(DSD
));
3670 if Is_Array_Type
(Typ
)
3671 and then Number_Dimensions
(Typ
) > 1
3672 and then Nkind
(Parent
(N
)) = N_Loop_Statement
3673 and then Present
(Iteration_Scheme
(Parent
(N
)))
3676 OIter
: constant Node_Id
:=
3677 Iteration_Scheme
(Parent
(N
));
3678 OLPS
: constant Node_Id
:=
3679 Loop_Parameter_Specification
(OIter
);
3680 ODSD
: constant Node_Id
:=
3681 Original_Node
(Discrete_Subtype_Definition
(OLPS
));
3683 if Nkind
(ODSD
) = N_Attribute_Reference
3684 and then Attribute_Name
(ODSD
) = Name_Range
3685 and then No
(Expressions
(ODSD
))
3686 and then Etype
(Prefix
(ODSD
)) = Typ
3688 Error_Msg_Sloc
:= Sloc
(ODSD
);
3690 ("inner range same as outer range#??", DSD
);
3699 -- Analyze the statements of the body except in the case of an Ada 2012
3700 -- iterator with the expander active. In this case the expander will do
3701 -- a rewrite of the loop into a while loop. We will then analyze the
3702 -- loop body when we analyze this while loop.
3704 -- We need to do this delay because if the container is for indefinite
3705 -- types the actual subtype of the components will only be determined
3706 -- when the cursor declaration is analyzed.
3708 -- If the expander is not active then we want to analyze the loop body
3709 -- now even in the Ada 2012 iterator case, since the rewriting will not
3710 -- be done. Insert the loop variable in the current scope, if not done
3711 -- when analysing the iteration scheme. Set its kind properly to detect
3712 -- improper uses in the loop body.
3714 -- In GNATprove mode, we do one of the above depending on the kind of
3715 -- loop. If it is an iterator over an array, then we do not analyze the
3716 -- loop now. We will analyze it after it has been rewritten by the
3717 -- special SPARK expansion which is activated in GNATprove mode. We need
3718 -- to do this so that other expansions that should occur in GNATprove
3719 -- mode take into account the specificities of the rewritten loop, in
3720 -- particular the introduction of a renaming (which needs to be
3723 -- In other cases in GNATprove mode then we want to analyze the loop
3724 -- body now, since no rewriting will occur. Within a generic the
3725 -- GNATprove mode is irrelevant, we must analyze the generic for
3726 -- non-local name capture.
3729 and then Present
(Iterator_Specification
(Iter
))
3732 and then Is_Iterator_Over_Array
(Iterator_Specification
(Iter
))
3733 and then not Inside_A_Generic
3737 elsif not Expander_Active
then
3739 I_Spec
: constant Node_Id
:= Iterator_Specification
(Iter
);
3740 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3743 if Scope
(Id
) /= Current_Scope
then
3747 -- In an element iterator, The loop parameter is a variable if
3748 -- the domain of iteration (container or array) is a variable.
3750 if not Of_Present
(I_Spec
)
3751 or else not Is_Variable
(Name
(I_Spec
))
3753 Set_Ekind
(Id
, E_Loop_Parameter
);
3757 Analyze_Statements
(Statements
(N
));
3761 -- Pre-Ada2012 for-loops and while loops
3763 Analyze_Statements
(Statements
(N
));
3766 -- When the iteration scheme of a loop contains attribute 'Loop_Entry,
3767 -- the loop is transformed into a conditional block. Retrieve the loop.
3771 if Subject_To_Loop_Entry_Attributes
(Stmt
) then
3772 Stmt
:= Find_Loop_In_Conditional_Block
(Stmt
);
3775 -- Finish up processing for the loop. We kill all current values, since
3776 -- in general we don't know if the statements in the loop have been
3777 -- executed. We could do a bit better than this with a loop that we
3778 -- know will execute at least once, but it's not worth the trouble and
3779 -- the front end is not in the business of flow tracing.
3781 Process_End_Label
(Stmt
, 'e', Ent
);
3783 Kill_Current_Values
;
3785 -- Check for infinite loop. Skip check for generated code, since it
3786 -- justs waste time and makes debugging the routine called harder.
3788 -- Note that we have to wait till the body of the loop is fully analyzed
3789 -- before making this call, since Check_Infinite_Loop_Warning relies on
3790 -- being able to use semantic visibility information to find references.
3792 if Comes_From_Source
(Stmt
) then
3793 Check_Infinite_Loop_Warning
(Stmt
);
3796 -- Code after loop is unreachable if the loop has no WHILE or FOR and
3797 -- contains no EXIT statements within the body of the loop.
3799 if No
(Iter
) and then not Has_Exit
(Ent
) then
3800 Check_Unreachable_Code
(Stmt
);
3802 end Analyze_Loop_Statement
;
3804 ----------------------------
3805 -- Analyze_Null_Statement --
3806 ----------------------------
3808 -- Note: the semantics of the null statement is implemented by a single
3809 -- null statement, too bad everything isn't as simple as this.
3811 procedure Analyze_Null_Statement
(N
: Node_Id
) is
3812 pragma Warnings
(Off
, N
);
3815 end Analyze_Null_Statement
;
3817 -------------------------
3818 -- Analyze_Target_Name --
3819 -------------------------
3821 procedure Analyze_Target_Name
(N
: Node_Id
) is
3823 -- A target name has the type of the left-hand side of the enclosing
3826 Set_Etype
(N
, Etype
(Name
(Current_Assignment
)));
3827 end Analyze_Target_Name
;
3829 ------------------------
3830 -- Analyze_Statements --
3831 ------------------------
3833 procedure Analyze_Statements
(L
: List_Id
) is
3838 -- The labels declared in the statement list are reachable from
3839 -- statements in the list. We do this as a prepass so that any goto
3840 -- statement will be properly flagged if its target is not reachable.
3841 -- This is not required, but is nice behavior.
3844 while Present
(S
) loop
3845 if Nkind
(S
) = N_Label
then
3846 Analyze
(Identifier
(S
));
3847 Lab
:= Entity
(Identifier
(S
));
3849 -- If we found a label mark it as reachable
3851 if Ekind
(Lab
) = E_Label
then
3852 Generate_Definition
(Lab
);
3853 Set_Reachable
(Lab
);
3855 if Nkind
(Parent
(Lab
)) = N_Implicit_Label_Declaration
then
3856 Set_Label_Construct
(Parent
(Lab
), S
);
3859 -- If we failed to find a label, it means the implicit declaration
3860 -- of the label was hidden. A for-loop parameter can do this to
3861 -- a label with the same name inside the loop, since the implicit
3862 -- label declaration is in the innermost enclosing body or block
3866 Error_Msg_Sloc
:= Sloc
(Lab
);
3868 ("implicit label declaration for & is hidden#",
3876 -- Perform semantic analysis on all statements
3878 Conditional_Statements_Begin
;
3881 while Present
(S
) loop
3884 -- Remove dimension in all statements
3886 Remove_Dimension_In_Statement
(S
);
3890 Conditional_Statements_End
;
3892 -- Make labels unreachable. Visibility is not sufficient, because labels
3893 -- in one if-branch for example are not reachable from the other branch,
3894 -- even though their declarations are in the enclosing declarative part.
3897 while Present
(S
) loop
3898 if Nkind
(S
) = N_Label
then
3899 Set_Reachable
(Entity
(Identifier
(S
)), False);
3904 end Analyze_Statements
;
3906 ----------------------------
3907 -- Check_Unreachable_Code --
3908 ----------------------------
3910 procedure Check_Unreachable_Code
(N
: Node_Id
) is
3911 Error_Node
: Node_Id
;
3915 if Is_List_Member
(N
) and then Comes_From_Source
(N
) then
3920 Nxt
:= Original_Node
(Next
(N
));
3922 -- Skip past pragmas
3924 while Nkind
(Nxt
) = N_Pragma
loop
3925 Nxt
:= Original_Node
(Next
(Nxt
));
3928 -- If a label follows us, then we never have dead code, since
3929 -- someone could branch to the label, so we just ignore it, unless
3930 -- we are in formal mode where goto statements are not allowed.
3932 if Nkind
(Nxt
) = N_Label
3933 and then not Restriction_Check_Required
(SPARK_05
)
3937 -- Otherwise see if we have a real statement following us
3940 and then Comes_From_Source
(Nxt
)
3941 and then Is_Statement
(Nxt
)
3943 -- Special very annoying exception. If we have a return that
3944 -- follows a raise, then we allow it without a warning, since
3945 -- the Ada RM annoyingly requires a useless return here.
3947 if Nkind
(Original_Node
(N
)) /= N_Raise_Statement
3948 or else Nkind
(Nxt
) /= N_Simple_Return_Statement
3950 -- The rather strange shenanigans with the warning message
3951 -- here reflects the fact that Kill_Dead_Code is very good
3952 -- at removing warnings in deleted code, and this is one
3953 -- warning we would prefer NOT to have removed.
3957 -- If we have unreachable code, analyze and remove the
3958 -- unreachable code, since it is useless and we don't
3959 -- want to generate junk warnings.
3961 -- We skip this step if we are not in code generation mode
3962 -- or CodePeer mode.
3964 -- This is the one case where we remove dead code in the
3965 -- semantics as opposed to the expander, and we do not want
3966 -- to remove code if we are not in code generation mode,
3967 -- since this messes up the ASIS trees or loses useful
3968 -- information in the CodePeer tree.
3970 -- Note that one might react by moving the whole circuit to
3971 -- exp_ch5, but then we lose the warning in -gnatc mode.
3973 if Operating_Mode
= Generate_Code
3974 and then not CodePeer_Mode
3979 -- Quit deleting when we have nothing more to delete
3980 -- or if we hit a label (since someone could transfer
3981 -- control to a label, so we should not delete it).
3983 exit when No
(Nxt
) or else Nkind
(Nxt
) = N_Label
;
3985 -- Statement/declaration is to be deleted
3989 Kill_Dead_Code
(Nxt
);
3993 -- Now issue the warning (or error in formal mode)
3995 if Restriction_Check_Required
(SPARK_05
) then
3996 Check_SPARK_05_Restriction
3997 ("unreachable code is not allowed", Error_Node
);
4000 ("??unreachable code!", Sloc
(Error_Node
), Error_Node
);
4004 -- If the unconditional transfer of control instruction is the
4005 -- last statement of a sequence, then see if our parent is one of
4006 -- the constructs for which we count unblocked exits, and if so,
4007 -- adjust the count.
4012 -- Statements in THEN part or ELSE part of IF statement
4014 if Nkind
(P
) = N_If_Statement
then
4017 -- Statements in ELSIF part of an IF statement
4019 elsif Nkind
(P
) = N_Elsif_Part
then
4021 pragma Assert
(Nkind
(P
) = N_If_Statement
);
4023 -- Statements in CASE statement alternative
4025 elsif Nkind
(P
) = N_Case_Statement_Alternative
then
4027 pragma Assert
(Nkind
(P
) = N_Case_Statement
);
4029 -- Statements in body of block
4031 elsif Nkind
(P
) = N_Handled_Sequence_Of_Statements
4032 and then Nkind
(Parent
(P
)) = N_Block_Statement
4034 -- The original loop is now placed inside a block statement
4035 -- due to the expansion of attribute 'Loop_Entry. Return as
4036 -- this is not a "real" block for the purposes of exit
4039 if Nkind
(N
) = N_Loop_Statement
4040 and then Subject_To_Loop_Entry_Attributes
(N
)
4045 -- Statements in exception handler in a block
4047 elsif Nkind
(P
) = N_Exception_Handler
4048 and then Nkind
(Parent
(P
)) = N_Handled_Sequence_Of_Statements
4049 and then Nkind
(Parent
(Parent
(P
))) = N_Block_Statement
4053 -- None of these cases, so return
4059 -- This was one of the cases we are looking for (i.e. the
4060 -- parent construct was IF, CASE or block) so decrement count.
4062 Unblocked_Exit_Count
:= Unblocked_Exit_Count
- 1;
4066 end Check_Unreachable_Code
;
4068 ------------------------------------
4069 -- Has_Call_Using_Secondary_Stack --
4070 ------------------------------------
4072 function Has_Call_Using_Secondary_Stack
(N
: Node_Id
) return Boolean is
4073 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
4074 -- Check if N is a function call which uses the secondary stack
4080 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
4086 if Nkind
(N
) = N_Function_Call
then
4089 -- Obtain the subprogram being invoked
4092 if Nkind
(Nam
) = N_Explicit_Dereference
then
4093 Nam
:= Prefix
(Nam
);
4095 elsif Nkind
(Nam
) = N_Selected_Component
then
4096 Nam
:= Selector_Name
(Nam
);
4103 Subp
:= Entity
(Nam
);
4104 Typ
:= Etype
(Subp
);
4106 if Requires_Transient_Scope
(Typ
) then
4109 elsif Sec_Stack_Needed_For_Return
(Subp
) then
4114 -- Continue traversing the tree
4119 function Check_Calls
is new Traverse_Func
(Check_Call
);
4121 -- Start of processing for Has_Call_Using_Secondary_Stack
4124 return Check_Calls
(N
) = Abandon
;
4125 end Has_Call_Using_Secondary_Stack
;
4127 ----------------------
4128 -- Preanalyze_Range --
4129 ----------------------
4131 procedure Preanalyze_Range
(R_Copy
: Node_Id
) is
4132 Save_Analysis
: constant Boolean := Full_Analysis
;
4136 Full_Analysis
:= False;
4137 Expander_Mode_Save_And_Set
(False);
4139 -- In addition to the above we must ecplicity suppress the
4140 -- generation of freeze nodes which might otherwise be generated
4141 -- during resolution of the range (e.g. if given by an attribute
4142 -- that will freeze its prefix).
4144 Set_Must_Not_Freeze
(R_Copy
);
4146 if Nkind
(R_Copy
) = N_Attribute_Reference
then
4147 Set_Must_Not_Freeze
(Prefix
(R_Copy
));
4152 if Nkind
(R_Copy
) in N_Subexpr
and then Is_Overloaded
(R_Copy
) then
4154 -- Apply preference rules for range of predefined integer types, or
4155 -- check for array or iterable construct for "of" iterator, or
4156 -- diagnose true ambiguity.
4161 Found
: Entity_Id
:= Empty
;
4164 Get_First_Interp
(R_Copy
, I
, It
);
4165 while Present
(It
.Typ
) loop
4166 if Is_Discrete_Type
(It
.Typ
) then
4170 if Scope
(Found
) = Standard_Standard
then
4173 elsif Scope
(It
.Typ
) = Standard_Standard
then
4177 -- Both of them are user-defined
4180 ("ambiguous bounds in range of iteration", R_Copy
);
4181 Error_Msg_N
("\possible interpretations:", R_Copy
);
4182 Error_Msg_NE
("\\} ", R_Copy
, Found
);
4183 Error_Msg_NE
("\\} ", R_Copy
, It
.Typ
);
4188 elsif Nkind
(Parent
(R_Copy
)) = N_Iterator_Specification
4189 and then Of_Present
(Parent
(R_Copy
))
4191 if Is_Array_Type
(It
.Typ
)
4192 or else Has_Aspect
(It
.Typ
, Aspect_Iterator_Element
)
4193 or else Has_Aspect
(It
.Typ
, Aspect_Constant_Indexing
)
4194 or else Has_Aspect
(It
.Typ
, Aspect_Variable_Indexing
)
4198 Set_Etype
(R_Copy
, It
.Typ
);
4201 Error_Msg_N
("ambiguous domain of iteration", R_Copy
);
4206 Get_Next_Interp
(I
, It
);
4211 -- Subtype mark in iteration scheme
4213 if Is_Entity_Name
(R_Copy
) and then Is_Type
(Entity
(R_Copy
)) then
4216 -- Expression in range, or Ada 2012 iterator
4218 elsif Nkind
(R_Copy
) in N_Subexpr
then
4220 Typ
:= Etype
(R_Copy
);
4222 if Is_Discrete_Type
(Typ
) then
4225 -- Check that the resulting object is an iterable container
4227 elsif Has_Aspect
(Typ
, Aspect_Iterator_Element
)
4228 or else Has_Aspect
(Typ
, Aspect_Constant_Indexing
)
4229 or else Has_Aspect
(Typ
, Aspect_Variable_Indexing
)
4233 -- The expression may yield an implicit reference to an iterable
4234 -- container. Insert explicit dereference so that proper type is
4235 -- visible in the loop.
4237 elsif Has_Implicit_Dereference
(Etype
(R_Copy
)) then
4242 Disc
:= First_Discriminant
(Typ
);
4243 while Present
(Disc
) loop
4244 if Has_Implicit_Dereference
(Disc
) then
4245 Build_Explicit_Dereference
(R_Copy
, Disc
);
4249 Next_Discriminant
(Disc
);
4256 Expander_Mode_Restore
;
4257 Full_Analysis
:= Save_Analysis
;
4258 end Preanalyze_Range
;