1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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 procedure Preanalyze_Range
(R_Copy
: Node_Id
);
87 -- Determine expected type of range or domain of iteration of Ada 2012
88 -- loop by analyzing separate copy. Do the analysis and resolution of the
89 -- copy of the bound(s) with expansion disabled, to prevent the generation
90 -- of finalization actions. This prevents memory leaks when the bounds
91 -- contain calls to functions returning controlled arrays or when the
92 -- domain of iteration is a container.
94 ------------------------
95 -- Analyze_Assignment --
96 ------------------------
98 -- WARNING: This routine manages Ghost regions. Return statements must be
99 -- replaced by gotos which jump to the end of the routine and restore the
102 procedure Analyze_Assignment
(N
: Node_Id
) is
103 Lhs
: constant Node_Id
:= Name
(N
);
104 Rhs
: Node_Id
:= Expression
(N
);
106 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
);
107 -- N is the node for the left hand side of an assignment, and it is not
108 -- a variable. This routine issues an appropriate diagnostic.
111 -- This is called to kill current value settings of a simple variable
112 -- on the left hand side. We call it if we find any error in analyzing
113 -- the assignment, and at the end of processing before setting any new
114 -- current values in place.
116 procedure Set_Assignment_Type
118 Opnd_Type
: in out Entity_Id
);
119 -- Opnd is either the Lhs or Rhs of the assignment, and Opnd_Type is the
120 -- nominal subtype. This procedure is used to deal with cases where the
121 -- nominal subtype must be replaced by the actual subtype.
123 procedure Transform_BIP_Assignment
(Typ
: Entity_Id
);
124 function Should_Transform_BIP_Assignment
125 (Typ
: Entity_Id
) return Boolean;
126 -- If the right-hand side of an assignment statement is a build-in-place
127 -- call we cannot build in place, so we insert a temp initialized with
128 -- the call, and transform the assignment statement to copy the temp.
129 -- Transform_BIP_Assignment does the tranformation, and
130 -- Should_Transform_BIP_Assignment determines whether we should.
131 -- The same goes for qualified expressions and conversions whose
132 -- operand is such a call.
134 -- This is only for nonlimited types; assignment statements are illegal
135 -- for limited types, but are generated internally for aggregates and
136 -- init procs. These limited-type are not really assignment statements
137 -- -- conceptually, they are initializations, so should not be
140 -- Similarly, for nonlimited types, aggregates and init procs generate
141 -- assignment statements that are really initializations. These are
142 -- marked No_Ctrl_Actions.
144 -------------------------------
145 -- Diagnose_Non_Variable_Lhs --
146 -------------------------------
148 procedure Diagnose_Non_Variable_Lhs
(N
: Node_Id
) is
150 -- Not worth posting another error if left hand side already flagged
151 -- as being illegal in some respect.
153 if Error_Posted
(N
) then
156 -- Some special bad cases of entity names
158 elsif Is_Entity_Name
(N
) then
160 Ent
: constant Entity_Id
:= Entity
(N
);
163 if Ekind
(Ent
) = E_In_Parameter
then
165 ("assignment to IN mode parameter not allowed", N
);
168 -- Renamings of protected private components are turned into
169 -- constants when compiling a protected function. In the case
170 -- of single protected types, the private component appears
173 elsif (Is_Prival
(Ent
)
175 (Ekind
(Current_Scope
) = E_Function
176 or else Ekind
(Enclosing_Dynamic_Scope
177 (Current_Scope
)) = E_Function
))
179 (Ekind
(Ent
) = E_Component
180 and then Is_Protected_Type
(Scope
(Ent
)))
183 ("protected function cannot modify protected object", N
);
186 elsif Ekind
(Ent
) = E_Loop_Parameter
then
187 Error_Msg_N
("assignment to loop parameter not allowed", N
);
192 -- For indexed components, test prefix if it is in array. We do not
193 -- want to recurse for cases where the prefix is a pointer, since we
194 -- may get a message confusing the pointer and what it references.
196 elsif Nkind
(N
) = N_Indexed_Component
197 and then Is_Array_Type
(Etype
(Prefix
(N
)))
199 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
202 -- Another special case for assignment to discriminant
204 elsif Nkind
(N
) = N_Selected_Component
then
205 if Present
(Entity
(Selector_Name
(N
)))
206 and then Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
208 Error_Msg_N
("assignment to discriminant not allowed", N
);
211 -- For selection from record, diagnose prefix, but note that again
212 -- we only do this for a record, not e.g. for a pointer.
214 elsif Is_Record_Type
(Etype
(Prefix
(N
))) then
215 Diagnose_Non_Variable_Lhs
(Prefix
(N
));
220 -- If we fall through, we have no special message to issue
222 Error_Msg_N
("left hand side of assignment must be a variable", N
);
223 end Diagnose_Non_Variable_Lhs
;
229 procedure Kill_Lhs
is
231 if Is_Entity_Name
(Lhs
) then
233 Ent
: constant Entity_Id
:= Entity
(Lhs
);
235 if Present
(Ent
) then
236 Kill_Current_Values
(Ent
);
242 -------------------------
243 -- Set_Assignment_Type --
244 -------------------------
246 procedure Set_Assignment_Type
248 Opnd_Type
: in out Entity_Id
)
253 Require_Entity
(Opnd
);
255 -- If the assignment operand is an in-out or out parameter, then we
256 -- get the actual subtype (needed for the unconstrained case). If the
257 -- operand is the actual in an entry declaration, then within the
258 -- accept statement it is replaced with a local renaming, which may
259 -- also have an actual subtype.
261 if Is_Entity_Name
(Opnd
)
262 and then (Ekind
(Entity
(Opnd
)) = E_Out_Parameter
263 or else Ekind_In
(Entity
(Opnd
),
265 E_Generic_In_Out_Parameter
)
267 (Ekind
(Entity
(Opnd
)) = E_Variable
268 and then Nkind
(Parent
(Entity
(Opnd
))) =
269 N_Object_Renaming_Declaration
270 and then Nkind
(Parent
(Parent
(Entity
(Opnd
)))) =
273 Opnd_Type
:= Get_Actual_Subtype
(Opnd
);
275 -- If assignment operand is a component reference, then we get the
276 -- actual subtype of the component for the unconstrained case.
278 elsif Nkind_In
(Opnd
, N_Selected_Component
, N_Explicit_Dereference
)
279 and then not Is_Unchecked_Union
(Opnd_Type
)
281 Decl
:= Build_Actual_Subtype_Of_Component
(Opnd_Type
, Opnd
);
283 if Present
(Decl
) then
284 Insert_Action
(N
, Decl
);
285 Mark_Rewrite_Insertion
(Decl
);
287 Opnd_Type
:= Defining_Identifier
(Decl
);
288 Set_Etype
(Opnd
, Opnd_Type
);
289 Freeze_Itype
(Opnd_Type
, N
);
291 elsif Is_Constrained
(Etype
(Opnd
)) then
292 Opnd_Type
:= Etype
(Opnd
);
295 -- For slice, use the constrained subtype created for the slice
297 elsif Nkind
(Opnd
) = N_Slice
then
298 Opnd_Type
:= Etype
(Opnd
);
300 end Set_Assignment_Type
;
302 -------------------------------------
303 -- Should_Transform_BIP_Assignment --
304 -------------------------------------
306 function Should_Transform_BIP_Assignment
307 (Typ
: Entity_Id
) return Boolean
313 and then not Is_Limited_View
(Typ
)
314 and then Is_Build_In_Place_Result_Type
(Typ
)
315 and then not No_Ctrl_Actions
(N
)
317 -- This function is called early, before name resolution is
318 -- complete, so we have to deal with things that might turn into
319 -- function calls later. N_Function_Call and N_Op nodes are the
320 -- obvious case. An N_Identifier or N_Expanded_Name is a
321 -- parameterless function call if it denotes a function.
322 -- Finally, an attribute reference can be a function call.
324 case Nkind
(Unqual_Conv
(Rhs
)) is
333 case Ekind
(Entity
(Unqual_Conv
(Rhs
))) is
343 when N_Attribute_Reference
=>
344 Result
:= Attribute_Name
(Unqual_Conv
(Rhs
)) = Name_Input
;
345 -- T'Input will turn into a call whose result type is T
355 end Should_Transform_BIP_Assignment
;
357 ------------------------------
358 -- Transform_BIP_Assignment --
359 ------------------------------
361 procedure Transform_BIP_Assignment
(Typ
: Entity_Id
) is
363 -- Tranform "X : [constant] T := F (...);" into:
365 -- Temp : constant T := F (...);
368 Loc
: constant Source_Ptr
:= Sloc
(N
);
369 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'Y', Rhs
);
370 Obj_Decl
: constant Node_Id
:=
371 Make_Object_Declaration
(Loc
,
372 Defining_Identifier
=> Def_Id
,
373 Constant_Present
=> True,
374 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
376 Has_Init_Expression
=> True);
379 Set_Etype
(Def_Id
, Typ
);
380 Set_Expression
(N
, New_Occurrence_Of
(Def_Id
, Loc
));
382 -- At this point, Rhs is no longer equal to Expression (N), so:
384 Rhs
:= Expression
(N
);
386 Insert_Action
(N
, Obj_Decl
);
387 end Transform_BIP_Assignment
;
394 Save_Full_Analysis
: Boolean := False;
395 -- Force initialization to facilitate static analysis
397 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
398 -- Save the Ghost mode to restore on exit
400 -- Start of processing for Analyze_Assignment
403 Mark_Coextensions
(N
, Rhs
);
405 -- Preserve relevant elaboration-related attributes of the context which
406 -- are no longer available or very expensive to recompute once analysis,
407 -- resolution, and expansion are over.
409 Mark_Elaboration_Attributes
414 -- Analyze the target of the assignment first in case the expression
415 -- contains references to Ghost entities. The checks that verify the
416 -- proper use of a Ghost entity need to know the enclosing context.
420 -- An assignment statement is Ghost when the left hand side denotes a
421 -- Ghost entity. Set the mode now to ensure that any nodes generated
422 -- during analysis and expansion are properly marked as Ghost.
424 if Has_Target_Names
(N
) then
425 Current_Assignment
:= N
;
426 Expander_Mode_Save_And_Set
(False);
427 Save_Full_Analysis
:= Full_Analysis
;
428 Full_Analysis
:= False;
430 Current_Assignment
:= Empty
;
433 Mark_And_Set_Ghost_Assignment
(N
);
436 -- Ensure that we never do an assignment on a variable marked as
437 -- Is_Safe_To_Reevaluate.
440 (not Is_Entity_Name
(Lhs
)
441 or else Ekind
(Entity
(Lhs
)) /= E_Variable
442 or else not Is_Safe_To_Reevaluate
(Entity
(Lhs
)));
444 -- Start type analysis for assignment
448 -- In the most general case, both Lhs and Rhs can be overloaded, and we
449 -- must compute the intersection of the possible types on each side.
451 if Is_Overloaded
(Lhs
) then
458 Get_First_Interp
(Lhs
, I
, It
);
460 while Present
(It
.Typ
) loop
462 -- An indexed component with generalized indexing is always
463 -- overloaded with the corresponding dereference. Discard the
464 -- interpretation that yields a reference type, which is not
467 if Nkind
(Lhs
) = N_Indexed_Component
468 and then Present
(Generalized_Indexing
(Lhs
))
469 and then Has_Implicit_Dereference
(It
.Typ
)
473 -- This may be a call to a parameterless function through an
474 -- implicit dereference, so discard interpretation as well.
476 elsif Is_Entity_Name
(Lhs
)
477 and then Has_Implicit_Dereference
(It
.Typ
)
481 elsif Has_Compatible_Type
(Rhs
, It
.Typ
) then
482 if T1
= Any_Type
then
485 -- An explicit dereference is overloaded if the prefix
486 -- is. Try to remove the ambiguity on the prefix, the
487 -- error will be posted there if the ambiguity is real.
489 if Nkind
(Lhs
) = N_Explicit_Dereference
then
492 PI1
: Interp_Index
:= 0;
498 Get_First_Interp
(Prefix
(Lhs
), PI
, PIt
);
500 while Present
(PIt
.Typ
) loop
501 if Is_Access_Type
(PIt
.Typ
)
502 and then Has_Compatible_Type
503 (Rhs
, Designated_Type
(PIt
.Typ
))
507 Disambiguate
(Prefix
(Lhs
),
510 if PIt
= No_Interp
then
512 ("ambiguous left-hand side in "
513 & "assignment", Lhs
);
516 Resolve
(Prefix
(Lhs
), PIt
.Typ
);
526 Get_Next_Interp
(PI
, PIt
);
532 ("ambiguous left-hand side in assignment", Lhs
);
538 Get_Next_Interp
(I
, It
);
542 if T1
= Any_Type
then
544 ("no valid types for left-hand side for assignment", Lhs
);
550 -- Deal with build-in-place calls for nonlimited types. We don't do this
551 -- later, because resolving the rhs tranforms it incorrectly for build-
554 if Should_Transform_BIP_Assignment
(Typ
=> T1
) then
556 -- In certain cases involving user-defined concatenation operators,
557 -- we need to resolve the right-hand side before transforming the
560 case Nkind
(Unqual_Conv
(Rhs
)) is
561 when N_Function_Call
=>
564 First
(Parameter_Associations
(Unqual_Conv
(Rhs
)));
565 Actual_Exp
: Node_Id
;
568 while Present
(Actual
) loop
569 if Nkind
(Actual
) = N_Parameter_Association
then
570 Actual_Exp
:= Explicit_Actual_Parameter
(Actual
);
572 Actual_Exp
:= Actual
;
575 if Nkind
(Actual_Exp
) = N_Op_Concat
then
584 when N_Attribute_Reference
595 Transform_BIP_Assignment
(Typ
=> T1
);
598 pragma Assert
(not Should_Transform_BIP_Assignment
(Typ
=> T1
));
600 -- The resulting assignment type is T1, so now we will resolve the left
601 -- hand side of the assignment using this determined type.
605 -- Cases where Lhs is not a variable. In an instance or an inlined body
606 -- no need for further check because assignment was legal in template.
608 if In_Inlined_Body
then
611 elsif not Is_Variable
(Lhs
) then
613 -- Ada 2005 (AI-327): Check assignment to the attribute Priority of a
621 if Ada_Version
>= Ada_2005
then
623 -- Handle chains of renamings
626 while Nkind
(Ent
) in N_Has_Entity
627 and then Present
(Entity
(Ent
))
628 and then Present
(Renamed_Object
(Entity
(Ent
)))
630 Ent
:= Renamed_Object
(Entity
(Ent
));
633 if (Nkind
(Ent
) = N_Attribute_Reference
634 and then Attribute_Name
(Ent
) = Name_Priority
)
636 -- Renamings of the attribute Priority applied to protected
637 -- objects have been previously expanded into calls to the
638 -- Get_Ceiling run-time subprogram.
640 or else Is_Expanded_Priority_Attribute
(Ent
)
642 -- The enclosing subprogram cannot be a protected function
645 while not (Is_Subprogram
(S
)
646 and then Convention
(S
) = Convention_Protected
)
647 and then S
/= Standard_Standard
652 if Ekind
(S
) = E_Function
653 and then Convention
(S
) = Convention_Protected
656 ("protected function cannot modify protected object",
660 -- Changes of the ceiling priority of the protected object
661 -- are only effective if the Ceiling_Locking policy is in
662 -- effect (AARM D.5.2 (5/2)).
664 if Locking_Policy
/= 'C' then
666 ("assignment to the attribute PRIORITY has no effect??",
669 ("\since no Locking_Policy has been specified??", Lhs
);
677 Diagnose_Non_Variable_Lhs
(Lhs
);
680 -- Error of assigning to limited type. We do however allow this in
681 -- certain cases where the front end generates the assignments.
683 elsif Is_Limited_Type
(T1
)
684 and then not Assignment_OK
(Lhs
)
685 and then not Assignment_OK
(Original_Node
(Lhs
))
687 -- CPP constructors can only be called in declarations
689 if Is_CPP_Constructor_Call
(Rhs
) then
690 Error_Msg_N
("invalid use of 'C'P'P constructor", Rhs
);
693 ("left hand of assignment must not be limited type", Lhs
);
694 Explain_Limited_Type
(T1
, Lhs
);
699 -- A class-wide type may be a limited view. This illegal case is not
700 -- caught by previous checks.
702 elsif Ekind
(T1
) = E_Class_Wide_Type
and then From_Limited_With
(T1
) then
703 Error_Msg_NE
("invalid use of limited view of&", Lhs
, T1
);
706 -- Enforce RM 3.9.3 (8): the target of an assignment operation cannot be
707 -- abstract. This is only checked when the assignment Comes_From_Source,
708 -- because in some cases the expander generates such assignments (such
709 -- in the _assign operation for an abstract type).
711 elsif Is_Abstract_Type
(T1
) and then Comes_From_Source
(N
) then
713 ("target of assignment operation must not be abstract", Lhs
);
716 -- Resolution may have updated the subtype, in case the left-hand side
717 -- is a private protected component. Use the correct subtype to avoid
718 -- scoping issues in the back-end.
722 -- Ada 2005 (AI-50217, AI-326): Check wrong dereference of incomplete
723 -- type. For example:
727 -- type Acc is access P.T;
730 -- with Pkg; use Acc;
731 -- procedure Example is
734 -- A.all := B.all; -- ERROR
737 if Nkind
(Lhs
) = N_Explicit_Dereference
738 and then Ekind
(T1
) = E_Incomplete_Type
740 Error_Msg_N
("invalid use of incomplete type", Lhs
);
745 -- Now we can complete the resolution of the right hand side
747 Set_Assignment_Type
(Lhs
, T1
);
749 -- If the target of the assignment is an entity of a mutable type and
750 -- the expression is a conditional expression, its alternatives can be
751 -- of different subtypes of the nominal type of the LHS, so they must be
752 -- resolved with the base type, given that their subtype may differ from
753 -- that of the target mutable object.
755 if Is_Entity_Name
(Lhs
)
756 and then Ekind_In
(Entity
(Lhs
), E_In_Out_Parameter
,
759 and then Is_Composite_Type
(T1
)
760 and then not Is_Constrained
(Etype
(Entity
(Lhs
)))
761 and then Nkind_In
(Rhs
, N_If_Expression
, N_Case_Expression
)
763 Resolve
(Rhs
, Base_Type
(T1
));
769 -- This is the point at which we check for an unset reference
771 Check_Unset_Reference
(Rhs
);
772 Check_Unprotected_Access
(Lhs
, Rhs
);
774 -- Remaining steps are skipped if Rhs was syntactically in error
783 if not Covers
(T1
, T2
) then
784 Wrong_Type
(Rhs
, Etype
(Lhs
));
789 -- Ada 2005 (AI-326): In case of explicit dereference of incomplete
790 -- types, use the non-limited view if available
792 if Nkind
(Rhs
) = N_Explicit_Dereference
793 and then Is_Tagged_Type
(T2
)
794 and then Has_Non_Limited_View
(T2
)
796 T2
:= Non_Limited_View
(T2
);
799 Set_Assignment_Type
(Rhs
, T2
);
801 if Total_Errors_Detected
/= 0 then
811 if T1
= Any_Type
or else T2
= Any_Type
then
816 -- If the rhs is class-wide or dynamically tagged, then require the lhs
817 -- to be class-wide. The case where the rhs is a dynamically tagged call
818 -- to a dispatching operation with a controlling access result is
819 -- excluded from this check, since the target has an access type (and
820 -- no tag propagation occurs in that case).
822 if (Is_Class_Wide_Type
(T2
)
823 or else (Is_Dynamically_Tagged
(Rhs
)
824 and then not Is_Access_Type
(T1
)))
825 and then not Is_Class_Wide_Type
(T1
)
827 Error_Msg_N
("dynamically tagged expression not allowed!", Rhs
);
829 elsif Is_Class_Wide_Type
(T1
)
830 and then not Is_Class_Wide_Type
(T2
)
831 and then not Is_Tag_Indeterminate
(Rhs
)
832 and then not Is_Dynamically_Tagged
(Rhs
)
834 Error_Msg_N
("dynamically tagged expression required!", Rhs
);
837 -- Propagate the tag from a class-wide target to the rhs when the rhs
838 -- is a tag-indeterminate call.
840 if Is_Tag_Indeterminate
(Rhs
) then
841 if Is_Class_Wide_Type
(T1
) then
842 Propagate_Tag
(Lhs
, Rhs
);
844 elsif Nkind
(Rhs
) = N_Function_Call
845 and then Is_Entity_Name
(Name
(Rhs
))
846 and then Is_Abstract_Subprogram
(Entity
(Name
(Rhs
)))
849 ("call to abstract function must be dispatching", Name
(Rhs
));
851 elsif Nkind
(Rhs
) = N_Qualified_Expression
852 and then Nkind
(Expression
(Rhs
)) = N_Function_Call
853 and then Is_Entity_Name
(Name
(Expression
(Rhs
)))
855 Is_Abstract_Subprogram
(Entity
(Name
(Expression
(Rhs
))))
858 ("call to abstract function must be dispatching",
859 Name
(Expression
(Rhs
)));
863 -- Ada 2005 (AI-385): When the lhs type is an anonymous access type,
864 -- apply an implicit conversion of the rhs to that type to force
865 -- appropriate static and run-time accessibility checks. This applies
866 -- as well to anonymous access-to-subprogram types that are component
867 -- subtypes or formal parameters.
869 if Ada_Version
>= Ada_2005
and then Is_Access_Type
(T1
) then
870 if Is_Local_Anonymous_Access
(T1
)
871 or else Ekind
(T2
) = E_Anonymous_Access_Subprogram_Type
873 -- Handle assignment to an Ada 2012 stand-alone object
874 -- of an anonymous access type.
876 or else (Ekind
(T1
) = E_Anonymous_Access_Type
877 and then Nkind
(Associated_Node_For_Itype
(T1
)) =
878 N_Object_Declaration
)
881 Rewrite
(Rhs
, Convert_To
(T1
, Relocate_Node
(Rhs
)));
882 Analyze_And_Resolve
(Rhs
, T1
);
886 -- Ada 2005 (AI-231): Assignment to not null variable
888 if Ada_Version
>= Ada_2005
889 and then Can_Never_Be_Null
(T1
)
890 and then not Assignment_OK
(Lhs
)
892 -- Case where we know the right hand side is null
894 if Known_Null
(Rhs
) then
895 Apply_Compile_Time_Constraint_Error
898 "(Ada 2005) null not allowed in null-excluding objects??",
899 Reason
=> CE_Null_Not_Allowed
);
901 -- We still mark this as a possible modification, that's necessary
902 -- to reset Is_True_Constant, and desirable for xref purposes.
904 Note_Possible_Modification
(Lhs
, Sure
=> True);
907 -- If we know the right hand side is non-null, then we convert to the
908 -- target type, since we don't need a run time check in that case.
910 elsif not Can_Never_Be_Null
(T2
) then
911 Rewrite
(Rhs
, Convert_To
(T1
, Relocate_Node
(Rhs
)));
912 Analyze_And_Resolve
(Rhs
, T1
);
916 if Is_Scalar_Type
(T1
) then
917 Apply_Scalar_Range_Check
(Rhs
, Etype
(Lhs
));
919 -- For array types, verify that lengths match. If the right hand side
920 -- is a function call that has been inlined, the assignment has been
921 -- rewritten as a block, and the constraint check will be applied to the
922 -- assignment within the block.
924 elsif Is_Array_Type
(T1
)
925 and then (Nkind
(Rhs
) /= N_Type_Conversion
926 or else Is_Constrained
(Etype
(Rhs
)))
927 and then (Nkind
(Rhs
) /= N_Function_Call
928 or else Nkind
(N
) /= N_Block_Statement
)
930 -- Assignment verifies that the length of the Lsh and Rhs are equal,
931 -- but of course the indexes do not have to match. If the right-hand
932 -- side is a type conversion to an unconstrained type, a length check
933 -- is performed on the expression itself during expansion. In rare
934 -- cases, the redundant length check is computed on an index type
935 -- with a different representation, triggering incorrect code in the
938 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
941 -- Discriminant checks are applied in the course of expansion
946 -- Note: modifications of the Lhs may only be recorded after
947 -- checks have been applied.
949 Note_Possible_Modification
(Lhs
, Sure
=> True);
951 -- ??? a real accessibility check is needed when ???
953 -- Post warning for redundant assignment or variable to itself
955 if Warn_On_Redundant_Constructs
957 -- We only warn for source constructs
959 and then Comes_From_Source
(N
)
961 -- Where the object is the same on both sides
963 and then Same_Object
(Lhs
, Original_Node
(Rhs
))
965 -- But exclude the case where the right side was an operation that
966 -- got rewritten (e.g. JUNK + K, where K was known to be zero). We
967 -- don't want to warn in such a case, since it is reasonable to write
968 -- such expressions especially when K is defined symbolically in some
971 and then Nkind
(Original_Node
(Rhs
)) not in N_Op
973 if Nkind
(Lhs
) in N_Has_Entity
then
974 Error_Msg_NE
-- CODEFIX
975 ("?r?useless assignment of & to itself!", N
, Entity
(Lhs
));
977 Error_Msg_N
-- CODEFIX
978 ("?r?useless assignment of object to itself!", N
);
982 -- Check for non-allowed composite assignment
984 if not Support_Composite_Assign_On_Target
985 and then (Is_Array_Type
(T1
) or else Is_Record_Type
(T1
))
986 and then (not Has_Size_Clause
(T1
) or else Esize
(T1
) > 64)
988 Error_Msg_CRT
("composite assignment", N
);
991 -- Check elaboration warning for left side if not in elab code
993 if Legacy_Elaboration_Checks
994 and not In_Subprogram_Or_Concurrent_Unit
996 Check_Elab_Assign
(Lhs
);
999 -- Save the scenario for later examination by the ABE Processing phase
1001 Record_Elaboration_Scenario
(N
);
1003 -- Set Referenced_As_LHS if appropriate. We only set this flag if the
1004 -- assignment is a source assignment in the extended main source unit.
1005 -- We are not interested in any reference information outside this
1006 -- context, or in compiler generated assignment statements.
1008 if Comes_From_Source
(N
)
1009 and then In_Extended_Main_Source_Unit
(Lhs
)
1011 Set_Referenced_Modified
(Lhs
, Out_Param
=> False);
1014 -- RM 7.3.2 (12/3): An assignment to a view conversion (from a type to
1015 -- one of its ancestors) requires an invariant check. Apply check only
1016 -- if expression comes from source, otherwise it will be applied when
1017 -- value is assigned to source entity. This is not done in GNATprove
1018 -- mode, as GNATprove handles invariant checks itself.
1020 if Nkind
(Lhs
) = N_Type_Conversion
1021 and then Has_Invariants
(Etype
(Expression
(Lhs
)))
1022 and then Comes_From_Source
(Expression
(Lhs
))
1023 and then not GNATprove_Mode
1025 Insert_After
(N
, Make_Invariant_Call
(Expression
(Lhs
)));
1028 -- Final step. If left side is an entity, then we may be able to reset
1029 -- the current tracked values to new safe values. We only have something
1030 -- to do if the left side is an entity name, and expansion has not
1031 -- modified the node into something other than an assignment, and of
1032 -- course we only capture values if it is safe to do so.
1034 if Is_Entity_Name
(Lhs
)
1035 and then Nkind
(N
) = N_Assignment_Statement
1038 Ent
: constant Entity_Id
:= Entity
(Lhs
);
1041 if Safe_To_Capture_Value
(N
, Ent
) then
1043 -- If simple variable on left side, warn if this assignment
1044 -- blots out another one (rendering it useless). We only do
1045 -- this for source assignments, otherwise we can generate bogus
1046 -- warnings when an assignment is rewritten as another
1047 -- assignment, and gets tied up with itself.
1049 -- There may have been a previous reference to a component of
1050 -- the variable, which in general removes the Last_Assignment
1051 -- field of the variable to indicate a relevant use of the
1052 -- previous assignment. However, if the assignment is to a
1053 -- subcomponent the reference may not have registered, because
1054 -- it is not possible to determine whether the context is an
1055 -- assignment. In those cases we generate a Deferred_Reference,
1056 -- to be used at the end of compilation to generate the right
1057 -- kind of reference, and we suppress a potential warning for
1058 -- a useless assignment, which might be premature. This may
1059 -- lose a warning in rare cases, but seems preferable to a
1060 -- misleading warning.
1062 if Warn_On_Modified_Unread
1063 and then Is_Assignable
(Ent
)
1064 and then Comes_From_Source
(N
)
1065 and then In_Extended_Main_Source_Unit
(Ent
)
1066 and then not Has_Deferred_Reference
(Ent
)
1068 Warn_On_Useless_Assignment
(Ent
, N
);
1071 -- If we are assigning an access type and the left side is an
1072 -- entity, then make sure that the Is_Known_[Non_]Null flags
1073 -- properly reflect the state of the entity after assignment.
1075 if Is_Access_Type
(T1
) then
1076 if Known_Non_Null
(Rhs
) then
1077 Set_Is_Known_Non_Null
(Ent
, True);
1079 elsif Known_Null
(Rhs
)
1080 and then not Can_Never_Be_Null
(Ent
)
1082 Set_Is_Known_Null
(Ent
, True);
1085 Set_Is_Known_Null
(Ent
, False);
1087 if not Can_Never_Be_Null
(Ent
) then
1088 Set_Is_Known_Non_Null
(Ent
, False);
1092 -- For discrete types, we may be able to set the current value
1093 -- if the value is known at compile time.
1095 elsif Is_Discrete_Type
(T1
)
1096 and then Compile_Time_Known_Value
(Rhs
)
1098 Set_Current_Value
(Ent
, Rhs
);
1100 Set_Current_Value
(Ent
, Empty
);
1103 -- If not safe to capture values, kill them
1111 -- If assigning to an object in whole or in part, note location of
1112 -- assignment in case no one references value. We only do this for
1113 -- source assignments, otherwise we can generate bogus warnings when an
1114 -- assignment is rewritten as another assignment, and gets tied up with
1118 Ent
: constant Entity_Id
:= Get_Enclosing_Object
(Lhs
);
1121 and then Safe_To_Capture_Value
(N
, Ent
)
1122 and then Nkind
(N
) = N_Assignment_Statement
1123 and then Warn_On_Modified_Unread
1124 and then Is_Assignable
(Ent
)
1125 and then Comes_From_Source
(N
)
1126 and then In_Extended_Main_Source_Unit
(Ent
)
1128 Set_Last_Assignment
(Ent
, Lhs
);
1132 Analyze_Dimension
(N
);
1135 Restore_Ghost_Mode
(Saved_GM
);
1137 -- If the right-hand side contains target names, expansion has been
1138 -- disabled to prevent expansion that might move target names out of
1139 -- the context of the assignment statement. Restore the expander mode
1140 -- now so that assignment statement can be properly expanded.
1142 if Nkind
(N
) = N_Assignment_Statement
then
1143 if Has_Target_Names
(N
) then
1144 Expander_Mode_Restore
;
1145 Full_Analysis
:= Save_Full_Analysis
;
1148 pragma Assert
(not Should_Transform_BIP_Assignment
(Typ
=> T1
));
1150 end Analyze_Assignment
;
1152 -----------------------------
1153 -- Analyze_Block_Statement --
1154 -----------------------------
1156 procedure Analyze_Block_Statement
(N
: Node_Id
) is
1157 procedure Install_Return_Entities
(Scop
: Entity_Id
);
1158 -- Install all entities of return statement scope Scop in the visibility
1159 -- chain except for the return object since its entity is reused in a
1162 -----------------------------
1163 -- Install_Return_Entities --
1164 -----------------------------
1166 procedure Install_Return_Entities
(Scop
: Entity_Id
) is
1170 Id
:= First_Entity
(Scop
);
1171 while Present
(Id
) loop
1173 -- Do not install the return object
1175 if not Ekind_In
(Id
, E_Constant
, E_Variable
)
1176 or else not Is_Return_Object
(Id
)
1178 Install_Entity
(Id
);
1183 end Install_Return_Entities
;
1185 -- Local constants and variables
1187 Decls
: constant List_Id
:= Declarations
(N
);
1188 Id
: constant Node_Id
:= Identifier
(N
);
1189 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
1191 Is_BIP_Return_Statement
: Boolean;
1193 -- Start of processing for Analyze_Block_Statement
1196 -- In SPARK mode, we reject block statements. Note that the case of
1197 -- block statements generated by the expander is fine.
1199 if Nkind
(Original_Node
(N
)) = N_Block_Statement
then
1200 Check_SPARK_05_Restriction
("block statement is not allowed", N
);
1203 -- If no handled statement sequence is present, things are really messed
1204 -- up, and we just return immediately (defence against previous errors).
1207 Check_Error_Detected
;
1211 -- Detect whether the block is actually a rewritten return statement of
1212 -- a build-in-place function.
1214 Is_BIP_Return_Statement
:=
1216 and then Present
(Entity
(Id
))
1217 and then Ekind
(Entity
(Id
)) = E_Return_Statement
1218 and then Is_Build_In_Place_Function
1219 (Return_Applies_To
(Entity
(Id
)));
1221 -- Normal processing with HSS present
1224 EH
: constant List_Id
:= Exception_Handlers
(HSS
);
1225 Ent
: Entity_Id
:= Empty
;
1228 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1229 -- Recursively save value of this global, will be restored on exit
1232 -- Initialize unblocked exit count for statements of begin block
1233 -- plus one for each exception handler that is present.
1235 Unblocked_Exit_Count
:= 1;
1237 if Present
(EH
) then
1238 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ List_Length
(EH
);
1241 -- If a label is present analyze it and mark it as referenced
1243 if Present
(Id
) then
1247 -- An error defense. If we have an identifier, but no entity, then
1248 -- something is wrong. If previous errors, then just remove the
1249 -- identifier and continue, otherwise raise an exception.
1252 Check_Error_Detected
;
1253 Set_Identifier
(N
, Empty
);
1256 Set_Ekind
(Ent
, E_Block
);
1257 Generate_Reference
(Ent
, N
, ' ');
1258 Generate_Definition
(Ent
);
1260 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
1261 Set_Label_Construct
(Parent
(Ent
), N
);
1266 -- If no entity set, create a label entity
1269 Ent
:= New_Internal_Entity
(E_Block
, Current_Scope
, Sloc
(N
), 'B');
1270 Set_Identifier
(N
, New_Occurrence_Of
(Ent
, Sloc
(N
)));
1271 Set_Parent
(Ent
, N
);
1274 Set_Etype
(Ent
, Standard_Void_Type
);
1275 Set_Block_Node
(Ent
, Identifier
(N
));
1278 -- The block served as an extended return statement. Ensure that any
1279 -- entities created during the analysis and expansion of the return
1280 -- object declaration are once again visible.
1282 if Is_BIP_Return_Statement
then
1283 Install_Return_Entities
(Ent
);
1286 if Present
(Decls
) then
1287 Analyze_Declarations
(Decls
);
1289 Inspect_Deferred_Constant_Completion
(Decls
);
1293 Process_End_Label
(HSS
, 'e', Ent
);
1295 -- If exception handlers are present, then we indicate that enclosing
1296 -- scopes contain a block with handlers. We only need to mark non-
1299 if Present
(EH
) then
1302 Set_Has_Nested_Block_With_Handler
(S
);
1303 exit when Is_Overloadable
(S
)
1304 or else Ekind
(S
) = E_Package
1305 or else Is_Generic_Unit
(S
);
1310 Check_References
(Ent
);
1311 Update_Use_Clause_Chain
;
1314 if Unblocked_Exit_Count
= 0 then
1315 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1316 Check_Unreachable_Code
(N
);
1318 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1321 end Analyze_Block_Statement
;
1323 --------------------------------
1324 -- Analyze_Compound_Statement --
1325 --------------------------------
1327 procedure Analyze_Compound_Statement
(N
: Node_Id
) is
1329 Analyze_List
(Actions
(N
));
1330 end Analyze_Compound_Statement
;
1332 ----------------------------
1333 -- Analyze_Case_Statement --
1334 ----------------------------
1336 procedure Analyze_Case_Statement
(N
: Node_Id
) is
1338 Exp_Type
: Entity_Id
;
1339 Exp_Btype
: Entity_Id
;
1342 Others_Present
: Boolean;
1343 -- Indicates if Others was present
1345 pragma Warnings
(Off
, Last_Choice
);
1346 -- Don't care about assigned value
1348 Statements_Analyzed
: Boolean := False;
1349 -- Set True if at least some statement sequences get analyzed. If False
1350 -- on exit, means we had a serious error that prevented full analysis of
1351 -- the case statement, and as a result it is not a good idea to output
1352 -- warning messages about unreachable code.
1354 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1355 -- Recursively save value of this global, will be restored on exit
1357 procedure Non_Static_Choice_Error
(Choice
: Node_Id
);
1358 -- Error routine invoked by the generic instantiation below when the
1359 -- case statement has a non static choice.
1361 procedure Process_Statements
(Alternative
: Node_Id
);
1362 -- Analyzes the statements associated with a case alternative. Needed
1363 -- by instantiation below.
1365 package Analyze_Case_Choices
is new
1366 Generic_Analyze_Choices
1367 (Process_Associated_Node
=> Process_Statements
);
1368 use Analyze_Case_Choices
;
1369 -- Instantiation of the generic choice analysis package
1371 package Check_Case_Choices
is new
1372 Generic_Check_Choices
1373 (Process_Empty_Choice
=> No_OP
,
1374 Process_Non_Static_Choice
=> Non_Static_Choice_Error
,
1375 Process_Associated_Node
=> No_OP
);
1376 use Check_Case_Choices
;
1377 -- Instantiation of the generic choice processing package
1379 -----------------------------
1380 -- Non_Static_Choice_Error --
1381 -----------------------------
1383 procedure Non_Static_Choice_Error
(Choice
: Node_Id
) is
1385 Flag_Non_Static_Expr
1386 ("choice given in case statement is not static!", Choice
);
1387 end Non_Static_Choice_Error
;
1389 ------------------------
1390 -- Process_Statements --
1391 ------------------------
1393 procedure Process_Statements
(Alternative
: Node_Id
) is
1394 Choices
: constant List_Id
:= Discrete_Choices
(Alternative
);
1398 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
1399 Statements_Analyzed
:= True;
1401 -- An interesting optimization. If the case statement expression
1402 -- is a simple entity, then we can set the current value within an
1403 -- alternative if the alternative has one possible value.
1407 -- when 2 | 3 => beta
1408 -- when others => gamma
1410 -- Here we know that N is initially 1 within alpha, but for beta and
1411 -- gamma, we do not know anything more about the initial value.
1413 if Is_Entity_Name
(Exp
) then
1414 Ent
:= Entity
(Exp
);
1416 if Ekind_In
(Ent
, E_Variable
,
1420 if List_Length
(Choices
) = 1
1421 and then Nkind
(First
(Choices
)) in N_Subexpr
1422 and then Compile_Time_Known_Value
(First
(Choices
))
1424 Set_Current_Value
(Entity
(Exp
), First
(Choices
));
1427 Analyze_Statements
(Statements
(Alternative
));
1429 -- After analyzing the case, set the current value to empty
1430 -- since we won't know what it is for the next alternative
1431 -- (unless reset by this same circuit), or after the case.
1433 Set_Current_Value
(Entity
(Exp
), Empty
);
1438 -- Case where expression is not an entity name of a variable
1440 Analyze_Statements
(Statements
(Alternative
));
1441 end Process_Statements
;
1443 -- Start of processing for Analyze_Case_Statement
1446 Unblocked_Exit_Count
:= 0;
1447 Exp
:= Expression
(N
);
1450 -- The expression must be of any discrete type. In rare cases, the
1451 -- expander constructs a case statement whose expression has a private
1452 -- type whose full view is discrete. This can happen when generating
1453 -- a stream operation for a variant type after the type is frozen,
1454 -- when the partial of view of the type of the discriminant is private.
1455 -- In that case, use the full view to analyze case alternatives.
1457 if not Is_Overloaded
(Exp
)
1458 and then not Comes_From_Source
(N
)
1459 and then Is_Private_Type
(Etype
(Exp
))
1460 and then Present
(Full_View
(Etype
(Exp
)))
1461 and then Is_Discrete_Type
(Full_View
(Etype
(Exp
)))
1463 Resolve
(Exp
, Etype
(Exp
));
1464 Exp_Type
:= Full_View
(Etype
(Exp
));
1467 Analyze_And_Resolve
(Exp
, Any_Discrete
);
1468 Exp_Type
:= Etype
(Exp
);
1471 Check_Unset_Reference
(Exp
);
1472 Exp_Btype
:= Base_Type
(Exp_Type
);
1474 -- The expression must be of a discrete type which must be determinable
1475 -- independently of the context in which the expression occurs, but
1476 -- using the fact that the expression must be of a discrete type.
1477 -- Moreover, the type this expression must not be a character literal
1478 -- (which is always ambiguous) or, for Ada-83, a generic formal type.
1480 -- If error already reported by Resolve, nothing more to do
1482 if Exp_Btype
= Any_Discrete
or else Exp_Btype
= Any_Type
then
1485 elsif Exp_Btype
= Any_Character
then
1487 ("character literal as case expression is ambiguous", Exp
);
1490 elsif Ada_Version
= Ada_83
1491 and then (Is_Generic_Type
(Exp_Btype
)
1492 or else Is_Generic_Type
(Root_Type
(Exp_Btype
)))
1495 ("(Ada 83) case expression cannot be of a generic type", Exp
);
1499 -- If the case expression is a formal object of mode in out, then treat
1500 -- it as having a nonstatic subtype by forcing use of the base type
1501 -- (which has to get passed to Check_Case_Choices below). Also use base
1502 -- type when the case expression is parenthesized.
1504 if Paren_Count
(Exp
) > 0
1505 or else (Is_Entity_Name
(Exp
)
1506 and then Ekind
(Entity
(Exp
)) = E_Generic_In_Out_Parameter
)
1508 Exp_Type
:= Exp_Btype
;
1511 -- Call instantiated procedures to analyzwe and check discrete choices
1513 Analyze_Choices
(Alternatives
(N
), Exp_Type
);
1514 Check_Choices
(N
, Alternatives
(N
), Exp_Type
, Others_Present
);
1516 -- Case statement with single OTHERS alternative not allowed in SPARK
1518 if Others_Present
and then List_Length
(Alternatives
(N
)) = 1 then
1519 Check_SPARK_05_Restriction
1520 ("OTHERS as unique case alternative is not allowed", N
);
1523 if Exp_Type
= Universal_Integer
and then not Others_Present
then
1524 Error_Msg_N
("case on universal integer requires OTHERS choice", Exp
);
1527 -- If all our exits were blocked by unconditional transfers of control,
1528 -- then the entire CASE statement acts as an unconditional transfer of
1529 -- control, so treat it like one, and check unreachable code. Skip this
1530 -- test if we had serious errors preventing any statement analysis.
1532 if Unblocked_Exit_Count
= 0 and then Statements_Analyzed
then
1533 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1534 Check_Unreachable_Code
(N
);
1536 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1539 -- If the expander is active it will detect the case of a statically
1540 -- determined single alternative and remove warnings for the case, but
1541 -- if we are not doing expansion, that circuit won't be active. Here we
1542 -- duplicate the effect of removing warnings in the same way, so that
1543 -- we will get the same set of warnings in -gnatc mode.
1545 if not Expander_Active
1546 and then Compile_Time_Known_Value
(Expression
(N
))
1547 and then Serious_Errors_Detected
= 0
1550 Chosen
: constant Node_Id
:= Find_Static_Alternative
(N
);
1554 Alt
:= First
(Alternatives
(N
));
1555 while Present
(Alt
) loop
1556 if Alt
/= Chosen
then
1557 Remove_Warning_Messages
(Statements
(Alt
));
1564 end Analyze_Case_Statement
;
1566 ----------------------------
1567 -- Analyze_Exit_Statement --
1568 ----------------------------
1570 -- If the exit includes a name, it must be the name of a currently open
1571 -- loop. Otherwise there must be an innermost open loop on the stack, to
1572 -- which the statement implicitly refers.
1574 -- Additionally, in SPARK mode:
1576 -- The exit can only name the closest enclosing loop;
1578 -- An exit with a when clause must be directly contained in a loop;
1580 -- An exit without a when clause must be directly contained in an
1581 -- if-statement with no elsif or else, which is itself directly contained
1582 -- in a loop. The exit must be the last statement in the if-statement.
1584 procedure Analyze_Exit_Statement
(N
: Node_Id
) is
1585 Target
: constant Node_Id
:= Name
(N
);
1586 Cond
: constant Node_Id
:= Condition
(N
);
1587 Scope_Id
: Entity_Id
:= Empty
; -- initialize to prevent warning
1593 Check_Unreachable_Code
(N
);
1596 if Present
(Target
) then
1598 U_Name
:= Entity
(Target
);
1600 if not In_Open_Scopes
(U_Name
) or else Ekind
(U_Name
) /= E_Loop
then
1601 Error_Msg_N
("invalid loop name in exit statement", N
);
1605 if Has_Loop_In_Inner_Open_Scopes
(U_Name
) then
1606 Check_SPARK_05_Restriction
1607 ("exit label must name the closest enclosing loop", N
);
1610 Set_Has_Exit
(U_Name
);
1617 for J
in reverse 0 .. Scope_Stack
.Last
loop
1618 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
1619 Kind
:= Ekind
(Scope_Id
);
1621 if Kind
= E_Loop
and then (No
(Target
) or else Scope_Id
= U_Name
) then
1622 Set_Has_Exit
(Scope_Id
);
1625 elsif Kind
= E_Block
1626 or else Kind
= E_Loop
1627 or else Kind
= E_Return_Statement
1633 ("cannot exit from program unit or accept statement", N
);
1638 -- Verify that if present the condition is a Boolean expression
1640 if Present
(Cond
) then
1641 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1642 Check_Unset_Reference
(Cond
);
1645 -- In SPARK mode, verify that the exit statement respects the SPARK
1648 if Present
(Cond
) then
1649 if Nkind
(Parent
(N
)) /= N_Loop_Statement
then
1650 Check_SPARK_05_Restriction
1651 ("exit with when clause must be directly in loop", N
);
1655 if Nkind
(Parent
(N
)) /= N_If_Statement
then
1656 if Nkind
(Parent
(N
)) = N_Elsif_Part
then
1657 Check_SPARK_05_Restriction
1658 ("exit must be in IF without ELSIF", N
);
1660 Check_SPARK_05_Restriction
("exit must be directly in IF", N
);
1663 elsif Nkind
(Parent
(Parent
(N
))) /= N_Loop_Statement
then
1664 Check_SPARK_05_Restriction
1665 ("exit must be in IF directly in loop", N
);
1667 -- First test the presence of ELSE, so that an exit in an ELSE leads
1668 -- to an error mentioning the ELSE.
1670 elsif Present
(Else_Statements
(Parent
(N
))) then
1671 Check_SPARK_05_Restriction
("exit must be in IF without ELSE", N
);
1673 -- An exit in an ELSIF does not reach here, as it would have been
1674 -- detected in the case (Nkind (Parent (N)) /= N_If_Statement).
1676 elsif Present
(Elsif_Parts
(Parent
(N
))) then
1677 Check_SPARK_05_Restriction
("exit must be in IF without ELSIF", N
);
1681 -- Chain exit statement to associated loop entity
1683 Set_Next_Exit_Statement
(N
, First_Exit_Statement
(Scope_Id
));
1684 Set_First_Exit_Statement
(Scope_Id
, N
);
1686 -- Since the exit may take us out of a loop, any previous assignment
1687 -- statement is not useless, so clear last assignment indications. It
1688 -- is OK to keep other current values, since if the exit statement
1689 -- does not exit, then the current values are still valid.
1691 Kill_Current_Values
(Last_Assignment_Only
=> True);
1692 end Analyze_Exit_Statement
;
1694 ----------------------------
1695 -- Analyze_Goto_Statement --
1696 ----------------------------
1698 procedure Analyze_Goto_Statement
(N
: Node_Id
) is
1699 Label
: constant Node_Id
:= Name
(N
);
1700 Scope_Id
: Entity_Id
;
1701 Label_Scope
: Entity_Id
;
1702 Label_Ent
: Entity_Id
;
1705 Check_SPARK_05_Restriction
("goto statement is not allowed", N
);
1707 -- Actual semantic checks
1709 Check_Unreachable_Code
(N
);
1710 Kill_Current_Values
(Last_Assignment_Only
=> True);
1713 Label_Ent
:= Entity
(Label
);
1715 -- Ignore previous error
1717 if Label_Ent
= Any_Id
then
1718 Check_Error_Detected
;
1721 -- We just have a label as the target of a goto
1723 elsif Ekind
(Label_Ent
) /= E_Label
then
1724 Error_Msg_N
("target of goto statement must be a label", Label
);
1727 -- Check that the target of the goto is reachable according to Ada
1728 -- scoping rules. Note: the special gotos we generate for optimizing
1729 -- local handling of exceptions would violate these rules, but we mark
1730 -- such gotos as analyzed when built, so this code is never entered.
1732 elsif not Reachable
(Label_Ent
) then
1733 Error_Msg_N
("target of goto statement is not reachable", Label
);
1737 -- Here if goto passes initial validity checks
1739 Label_Scope
:= Enclosing_Scope
(Label_Ent
);
1741 for J
in reverse 0 .. Scope_Stack
.Last
loop
1742 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
1744 if Label_Scope
= Scope_Id
1745 or else not Ekind_In
(Scope_Id
, E_Block
, E_Loop
, E_Return_Statement
)
1747 if Scope_Id
/= Label_Scope
then
1749 ("cannot exit from program unit or accept statement", N
);
1756 raise Program_Error
;
1757 end Analyze_Goto_Statement
;
1759 --------------------------
1760 -- Analyze_If_Statement --
1761 --------------------------
1763 -- A special complication arises in the analysis of if statements
1765 -- The expander has circuitry to completely delete code that it can tell
1766 -- will not be executed (as a result of compile time known conditions). In
1767 -- the analyzer, we ensure that code that will be deleted in this manner
1768 -- is analyzed but not expanded. This is obviously more efficient, but
1769 -- more significantly, difficulties arise if code is expanded and then
1770 -- eliminated (e.g. exception table entries disappear). Similarly, itypes
1771 -- generated in deleted code must be frozen from start, because the nodes
1772 -- on which they depend will not be available at the freeze point.
1774 procedure Analyze_If_Statement
(N
: Node_Id
) is
1777 Save_Unblocked_Exit_Count
: constant Nat
:= Unblocked_Exit_Count
;
1778 -- Recursively save value of this global, will be restored on exit
1780 Save_In_Deleted_Code
: Boolean;
1782 Del
: Boolean := False;
1783 -- This flag gets set True if a True condition has been found, which
1784 -- means that remaining ELSE/ELSIF parts are deleted.
1786 procedure Analyze_Cond_Then
(Cnode
: Node_Id
);
1787 -- This is applied to either the N_If_Statement node itself or to an
1788 -- N_Elsif_Part node. It deals with analyzing the condition and the THEN
1789 -- statements associated with it.
1791 -----------------------
1792 -- Analyze_Cond_Then --
1793 -----------------------
1795 procedure Analyze_Cond_Then
(Cnode
: Node_Id
) is
1796 Cond
: constant Node_Id
:= Condition
(Cnode
);
1797 Tstm
: constant List_Id
:= Then_Statements
(Cnode
);
1800 Unblocked_Exit_Count
:= Unblocked_Exit_Count
+ 1;
1801 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1802 Check_Unset_Reference
(Cond
);
1803 Set_Current_Value_Condition
(Cnode
);
1805 -- If already deleting, then just analyze then statements
1808 Analyze_Statements
(Tstm
);
1810 -- Compile time known value, not deleting yet
1812 elsif Compile_Time_Known_Value
(Cond
) then
1813 Save_In_Deleted_Code
:= In_Deleted_Code
;
1815 -- If condition is True, then analyze the THEN statements and set
1816 -- no expansion for ELSE and ELSIF parts.
1818 if Is_True
(Expr_Value
(Cond
)) then
1819 Analyze_Statements
(Tstm
);
1821 Expander_Mode_Save_And_Set
(False);
1822 In_Deleted_Code
:= True;
1824 -- If condition is False, analyze THEN with expansion off
1826 else -- Is_False (Expr_Value (Cond))
1827 Expander_Mode_Save_And_Set
(False);
1828 In_Deleted_Code
:= True;
1829 Analyze_Statements
(Tstm
);
1830 Expander_Mode_Restore
;
1831 In_Deleted_Code
:= Save_In_Deleted_Code
;
1834 -- Not known at compile time, not deleting, normal analysis
1837 Analyze_Statements
(Tstm
);
1839 end Analyze_Cond_Then
;
1841 -- Start of processing for Analyze_If_Statement
1844 -- Initialize exit count for else statements. If there is no else part,
1845 -- this count will stay non-zero reflecting the fact that the uncovered
1846 -- else case is an unblocked exit.
1848 Unblocked_Exit_Count
:= 1;
1849 Analyze_Cond_Then
(N
);
1851 -- Now to analyze the elsif parts if any are present
1853 if Present
(Elsif_Parts
(N
)) then
1854 E
:= First
(Elsif_Parts
(N
));
1855 while Present
(E
) loop
1856 Analyze_Cond_Then
(E
);
1861 if Present
(Else_Statements
(N
)) then
1862 Analyze_Statements
(Else_Statements
(N
));
1865 -- If all our exits were blocked by unconditional transfers of control,
1866 -- then the entire IF statement acts as an unconditional transfer of
1867 -- control, so treat it like one, and check unreachable code.
1869 if Unblocked_Exit_Count
= 0 then
1870 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1871 Check_Unreachable_Code
(N
);
1873 Unblocked_Exit_Count
:= Save_Unblocked_Exit_Count
;
1877 Expander_Mode_Restore
;
1878 In_Deleted_Code
:= Save_In_Deleted_Code
;
1881 if not Expander_Active
1882 and then Compile_Time_Known_Value
(Condition
(N
))
1883 and then Serious_Errors_Detected
= 0
1885 if Is_True
(Expr_Value
(Condition
(N
))) then
1886 Remove_Warning_Messages
(Else_Statements
(N
));
1888 if Present
(Elsif_Parts
(N
)) then
1889 E
:= First
(Elsif_Parts
(N
));
1890 while Present
(E
) loop
1891 Remove_Warning_Messages
(Then_Statements
(E
));
1897 Remove_Warning_Messages
(Then_Statements
(N
));
1901 -- Warn on redundant if statement that has no effect
1903 -- Note, we could also check empty ELSIF parts ???
1905 if Warn_On_Redundant_Constructs
1907 -- If statement must be from source
1909 and then Comes_From_Source
(N
)
1911 -- Condition must not have obvious side effect
1913 and then Has_No_Obvious_Side_Effects
(Condition
(N
))
1915 -- No elsif parts of else part
1917 and then No
(Elsif_Parts
(N
))
1918 and then No
(Else_Statements
(N
))
1920 -- Then must be a single null statement
1922 and then List_Length
(Then_Statements
(N
)) = 1
1924 -- Go to original node, since we may have rewritten something as
1925 -- a null statement (e.g. a case we could figure the outcome of).
1928 T
: constant Node_Id
:= First
(Then_Statements
(N
));
1929 S
: constant Node_Id
:= Original_Node
(T
);
1932 if Comes_From_Source
(S
) and then Nkind
(S
) = N_Null_Statement
then
1933 Error_Msg_N
("if statement has no effect?r?", N
);
1937 end Analyze_If_Statement
;
1939 ----------------------------------------
1940 -- Analyze_Implicit_Label_Declaration --
1941 ----------------------------------------
1943 -- An implicit label declaration is generated in the innermost enclosing
1944 -- declarative part. This is done for labels, and block and loop names.
1946 -- Note: any changes in this routine may need to be reflected in
1947 -- Analyze_Label_Entity.
1949 procedure Analyze_Implicit_Label_Declaration
(N
: Node_Id
) is
1950 Id
: constant Node_Id
:= Defining_Identifier
(N
);
1953 Set_Ekind
(Id
, E_Label
);
1954 Set_Etype
(Id
, Standard_Void_Type
);
1955 Set_Enclosing_Scope
(Id
, Current_Scope
);
1956 end Analyze_Implicit_Label_Declaration
;
1958 ------------------------------
1959 -- Analyze_Iteration_Scheme --
1960 ------------------------------
1962 procedure Analyze_Iteration_Scheme
(N
: Node_Id
) is
1964 Iter_Spec
: Node_Id
;
1965 Loop_Spec
: Node_Id
;
1968 -- For an infinite loop, there is no iteration scheme
1974 Cond
:= Condition
(N
);
1975 Iter_Spec
:= Iterator_Specification
(N
);
1976 Loop_Spec
:= Loop_Parameter_Specification
(N
);
1978 if Present
(Cond
) then
1979 Analyze_And_Resolve
(Cond
, Any_Boolean
);
1980 Check_Unset_Reference
(Cond
);
1981 Set_Current_Value_Condition
(N
);
1983 elsif Present
(Iter_Spec
) then
1984 Analyze_Iterator_Specification
(Iter_Spec
);
1987 Analyze_Loop_Parameter_Specification
(Loop_Spec
);
1989 end Analyze_Iteration_Scheme
;
1991 ------------------------------------
1992 -- Analyze_Iterator_Specification --
1993 ------------------------------------
1995 procedure Analyze_Iterator_Specification
(N
: Node_Id
) is
1996 procedure Check_Reverse_Iteration
(Typ
: Entity_Id
);
1997 -- For an iteration over a container, if the loop carries the Reverse
1998 -- indicator, verify that the container type has an Iterate aspect that
1999 -- implements the reversible iterator interface.
2001 function Get_Cursor_Type
(Typ
: Entity_Id
) return Entity_Id
;
2002 -- For containers with Iterator and related aspects, the cursor is
2003 -- obtained by locating an entity with the proper name in the scope
2006 -----------------------------
2007 -- Check_Reverse_Iteration --
2008 -----------------------------
2010 procedure Check_Reverse_Iteration
(Typ
: Entity_Id
) is
2012 if Reverse_Present
(N
) then
2013 if Is_Array_Type
(Typ
)
2014 or else Is_Reversible_Iterator
(Typ
)
2016 (Present
(Find_Aspect
(Typ
, Aspect_Iterable
))
2019 (Get_Iterable_Type_Primitive
(Typ
, Name_Previous
)))
2024 ("container type does not support reverse iteration", N
, Typ
);
2027 end Check_Reverse_Iteration
;
2029 ---------------------
2030 -- Get_Cursor_Type --
2031 ---------------------
2033 function Get_Cursor_Type
(Typ
: Entity_Id
) return Entity_Id
is
2037 -- If iterator type is derived, the cursor is declared in the scope
2038 -- of the parent type.
2040 if Is_Derived_Type
(Typ
) then
2041 Ent
:= First_Entity
(Scope
(Etype
(Typ
)));
2043 Ent
:= First_Entity
(Scope
(Typ
));
2046 while Present
(Ent
) loop
2047 exit when Chars
(Ent
) = Name_Cursor
;
2055 -- The cursor is the target of generated assignments in the
2056 -- loop, and cannot have a limited type.
2058 if Is_Limited_Type
(Etype
(Ent
)) then
2059 Error_Msg_N
("cursor type cannot be limited", N
);
2063 end Get_Cursor_Type
;
2067 Def_Id
: constant Node_Id
:= Defining_Identifier
(N
);
2068 Iter_Name
: constant Node_Id
:= Name
(N
);
2069 Loc
: constant Source_Ptr
:= Sloc
(N
);
2070 Subt
: constant Node_Id
:= Subtype_Indication
(N
);
2072 Bas
: Entity_Id
:= Empty
; -- initialize to prevent warning
2075 -- Start of processing for Analyze_Iterator_Specification
2078 Enter_Name
(Def_Id
);
2080 -- AI12-0151 specifies that when the subtype indication is present, it
2081 -- must statically match the type of the array or container element.
2082 -- To simplify this check, we introduce a subtype declaration with the
2083 -- given subtype indication when it carries a constraint, and rewrite
2084 -- the original as a reference to the created subtype entity.
2086 if Present
(Subt
) then
2087 if Nkind
(Subt
) = N_Subtype_Indication
then
2089 S
: constant Entity_Id
:= Make_Temporary
(Sloc
(Subt
), 'S');
2090 Decl
: constant Node_Id
:=
2091 Make_Subtype_Declaration
(Loc
,
2092 Defining_Identifier
=> S
,
2093 Subtype_Indication
=> New_Copy_Tree
(Subt
));
2095 Insert_Before
(Parent
(Parent
(N
)), Decl
);
2097 Rewrite
(Subt
, New_Occurrence_Of
(S
, Sloc
(Subt
)));
2103 -- Save entity of subtype indication for subsequent check
2105 Bas
:= Entity
(Subt
);
2108 Preanalyze_Range
(Iter_Name
);
2110 -- Set the kind of the loop variable, which is not visible within the
2113 Set_Ekind
(Def_Id
, E_Variable
);
2115 -- Provide a link between the iterator variable and the container, for
2116 -- subsequent use in cross-reference and modification information.
2118 if Of_Present
(N
) then
2119 Set_Related_Expression
(Def_Id
, Iter_Name
);
2121 -- For a container, the iterator is specified through the aspect
2123 if not Is_Array_Type
(Etype
(Iter_Name
)) then
2125 Iterator
: constant Entity_Id
:=
2126 Find_Value_Of_Aspect
2127 (Etype
(Iter_Name
), Aspect_Default_Iterator
);
2133 if No
(Iterator
) then
2134 null; -- error reported below
2136 elsif not Is_Overloaded
(Iterator
) then
2137 Check_Reverse_Iteration
(Etype
(Iterator
));
2139 -- If Iterator is overloaded, use reversible iterator if one is
2142 elsif Is_Overloaded
(Iterator
) then
2143 Get_First_Interp
(Iterator
, I
, It
);
2144 while Present
(It
.Nam
) loop
2145 if Ekind
(It
.Nam
) = E_Function
2146 and then Is_Reversible_Iterator
(Etype
(It
.Nam
))
2148 Set_Etype
(Iterator
, It
.Typ
);
2149 Set_Entity
(Iterator
, It
.Nam
);
2153 Get_Next_Interp
(I
, It
);
2156 Check_Reverse_Iteration
(Etype
(Iterator
));
2162 -- If the domain of iteration is an expression, create a declaration for
2163 -- it, so that finalization actions are introduced outside of the loop.
2164 -- The declaration must be a renaming because the body of the loop may
2165 -- assign to elements.
2167 if not Is_Entity_Name
(Iter_Name
)
2169 -- When the context is a quantified expression, the renaming
2170 -- declaration is delayed until the expansion phase if we are
2173 and then (Nkind
(Parent
(N
)) /= N_Quantified_Expression
2174 or else Operating_Mode
= Check_Semantics
)
2176 -- Do not perform this expansion for ASIS and when expansion is
2177 -- disabled, where the temporary may hide the transformation of a
2178 -- selected component into a prefixed function call, and references
2179 -- need to see the original expression.
2181 and then Expander_Active
2184 Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R', Iter_Name
);
2190 -- If the domain of iteration is an array component that depends
2191 -- on a discriminant, create actual subtype for it. Pre-analysis
2192 -- does not generate the actual subtype of a selected component.
2194 if Nkind
(Iter_Name
) = N_Selected_Component
2195 and then Is_Array_Type
(Etype
(Iter_Name
))
2198 Build_Actual_Subtype_Of_Component
2199 (Etype
(Selector_Name
(Iter_Name
)), Iter_Name
);
2200 Insert_Action
(N
, Act_S
);
2202 if Present
(Act_S
) then
2203 Typ
:= Defining_Identifier
(Act_S
);
2205 Typ
:= Etype
(Iter_Name
);
2209 Typ
:= Etype
(Iter_Name
);
2211 -- Verify that the expression produces an iterator
2213 if not Of_Present
(N
) and then not Is_Iterator
(Typ
)
2214 and then not Is_Array_Type
(Typ
)
2215 and then No
(Find_Aspect
(Typ
, Aspect_Iterable
))
2218 ("expect object that implements iterator interface",
2223 -- Protect against malformed iterator
2225 if Typ
= Any_Type
then
2226 Error_Msg_N
("invalid expression in loop iterator", Iter_Name
);
2230 if not Of_Present
(N
) then
2231 Check_Reverse_Iteration
(Typ
);
2234 -- The name in the renaming declaration may be a function call.
2235 -- Indicate that it does not come from source, to suppress
2236 -- spurious warnings on renamings of parameterless functions,
2237 -- a common enough idiom in user-defined iterators.
2240 Make_Object_Renaming_Declaration
(Loc
,
2241 Defining_Identifier
=> Id
,
2242 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
2244 New_Copy_Tree
(Iter_Name
, New_Sloc
=> Loc
));
2246 Insert_Actions
(Parent
(Parent
(N
)), New_List
(Decl
));
2247 Rewrite
(Name
(N
), New_Occurrence_Of
(Id
, Loc
));
2248 Set_Etype
(Id
, Typ
);
2249 Set_Etype
(Name
(N
), Typ
);
2252 -- Container is an entity or an array with uncontrolled components, or
2253 -- else it is a container iterator given by a function call, typically
2254 -- called Iterate in the case of predefined containers, even though
2255 -- Iterate is not a reserved name. What matters is that the return type
2256 -- of the function is an iterator type.
2258 elsif Is_Entity_Name
(Iter_Name
) then
2259 Analyze
(Iter_Name
);
2261 if Nkind
(Iter_Name
) = N_Function_Call
then
2263 C
: constant Node_Id
:= Name
(Iter_Name
);
2268 if not Is_Overloaded
(Iter_Name
) then
2269 Resolve
(Iter_Name
, Etype
(C
));
2272 Get_First_Interp
(C
, I
, It
);
2273 while It
.Typ
/= Empty
loop
2274 if Reverse_Present
(N
) then
2275 if Is_Reversible_Iterator
(It
.Typ
) then
2276 Resolve
(Iter_Name
, It
.Typ
);
2280 elsif Is_Iterator
(It
.Typ
) then
2281 Resolve
(Iter_Name
, It
.Typ
);
2285 Get_Next_Interp
(I
, It
);
2290 -- Domain of iteration is not overloaded
2293 Resolve
(Iter_Name
, Etype
(Iter_Name
));
2296 if not Of_Present
(N
) then
2297 Check_Reverse_Iteration
(Etype
(Iter_Name
));
2301 -- Get base type of container, for proper retrieval of Cursor type
2302 -- and primitive operations.
2304 Typ
:= Base_Type
(Etype
(Iter_Name
));
2306 if Is_Array_Type
(Typ
) then
2307 if Of_Present
(N
) then
2308 Set_Etype
(Def_Id
, Component_Type
(Typ
));
2310 -- The loop variable is aliased if the array components are
2313 Set_Is_Aliased
(Def_Id
, Has_Aliased_Components
(Typ
));
2315 -- AI12-0047 stipulates that the domain (array or container)
2316 -- cannot be a component that depends on a discriminant if the
2317 -- enclosing object is mutable, to prevent a modification of the
2318 -- dowmain of iteration in the course of an iteration.
2320 -- If the object is an expression it has been captured in a
2321 -- temporary, so examine original node.
2323 if Nkind
(Original_Node
(Iter_Name
)) = N_Selected_Component
2324 and then Is_Dependent_Component_Of_Mutable_Object
2325 (Original_Node
(Iter_Name
))
2328 ("iterable name cannot be a discriminant-dependent "
2329 & "component of a mutable object", N
);
2334 (Base_Type
(Bas
) /= Base_Type
(Component_Type
(Typ
))
2336 not Subtypes_Statically_Match
(Bas
, Component_Type
(Typ
)))
2339 ("subtype indication does not match component type", Subt
);
2342 -- Here we have a missing Range attribute
2346 ("missing Range attribute in iteration over an array", N
);
2348 -- In Ada 2012 mode, this may be an attempt at an iterator
2350 if Ada_Version
>= Ada_2012
then
2352 ("\if& is meant to designate an element of the array, use OF",
2356 -- Prevent cascaded errors
2358 Set_Ekind
(Def_Id
, E_Loop_Parameter
);
2359 Set_Etype
(Def_Id
, Etype
(First_Index
(Typ
)));
2362 -- Check for type error in iterator
2364 elsif Typ
= Any_Type
then
2367 -- Iteration over a container
2370 Set_Ekind
(Def_Id
, E_Loop_Parameter
);
2371 Error_Msg_Ada_2012_Feature
("container iterator", Sloc
(N
));
2375 if Of_Present
(N
) then
2376 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2378 Elt
: constant Entity_Id
:=
2379 Get_Iterable_Type_Primitive
(Typ
, Name_Element
);
2383 ("missing Element primitive for iteration", N
);
2385 Set_Etype
(Def_Id
, Etype
(Elt
));
2386 Check_Reverse_Iteration
(Typ
);
2390 -- For a predefined container, The type of the loop variable is
2391 -- the Iterator_Element aspect of the container type.
2395 Element
: constant Entity_Id
:=
2396 Find_Value_Of_Aspect
2397 (Typ
, Aspect_Iterator_Element
);
2398 Iterator
: constant Entity_Id
:=
2399 Find_Value_Of_Aspect
2400 (Typ
, Aspect_Default_Iterator
);
2401 Orig_Iter_Name
: constant Node_Id
:=
2402 Original_Node
(Iter_Name
);
2403 Cursor_Type
: Entity_Id
;
2406 if No
(Element
) then
2407 Error_Msg_NE
("cannot iterate over&", N
, Typ
);
2411 Set_Etype
(Def_Id
, Entity
(Element
));
2412 Cursor_Type
:= Get_Cursor_Type
(Typ
);
2413 pragma Assert
(Present
(Cursor_Type
));
2415 -- If subtype indication was given, verify that it covers
2416 -- the element type of the container.
2419 and then (not Covers
(Bas
, Etype
(Def_Id
))
2420 or else not Subtypes_Statically_Match
2421 (Bas
, Etype
(Def_Id
)))
2424 ("subtype indication does not match element type",
2428 -- If the container has a variable indexing aspect, the
2429 -- element is a variable and is modifiable in the loop.
2431 if Has_Aspect
(Typ
, Aspect_Variable_Indexing
) then
2432 Set_Ekind
(Def_Id
, E_Variable
);
2435 -- If the container is a constant, iterating over it
2436 -- requires a Constant_Indexing operation.
2438 if not Is_Variable
(Iter_Name
)
2439 and then not Has_Aspect
(Typ
, Aspect_Constant_Indexing
)
2442 ("iteration over constant container require "
2443 & "constant_indexing aspect", N
);
2445 -- The Iterate function may have an in_out parameter,
2446 -- and a constant container is thus illegal.
2448 elsif Present
(Iterator
)
2449 and then Ekind
(Entity
(Iterator
)) = E_Function
2450 and then Ekind
(First_Formal
(Entity
(Iterator
))) /=
2452 and then not Is_Variable
(Iter_Name
)
2454 Error_Msg_N
("variable container expected", N
);
2457 -- Detect a case where the iterator denotes a component
2458 -- of a mutable object which depends on a discriminant.
2459 -- Note that the iterator may denote a function call in
2460 -- qualified form, in which case this check should not
2463 if Nkind
(Orig_Iter_Name
) = N_Selected_Component
2465 Present
(Entity
(Selector_Name
(Orig_Iter_Name
)))
2467 (Entity
(Selector_Name
(Orig_Iter_Name
)),
2470 and then Is_Dependent_Component_Of_Mutable_Object
2474 ("container cannot be a discriminant-dependent "
2475 & "component of a mutable object", N
);
2481 -- IN iterator, domain is a range, or a call to Iterate function
2484 -- For an iteration of the form IN, the name must denote an
2485 -- iterator, typically the result of a call to Iterate. Give a
2486 -- useful error message when the name is a container by itself.
2488 -- The type may be a formal container type, which has to have
2489 -- an Iterable aspect detailing the required primitives.
2491 if Is_Entity_Name
(Original_Node
(Name
(N
)))
2492 and then not Is_Iterator
(Typ
)
2494 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2497 elsif not Has_Aspect
(Typ
, Aspect_Iterator_Element
) then
2499 ("cannot iterate over&", Name
(N
), Typ
);
2502 ("name must be an iterator, not a container", Name
(N
));
2505 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2509 ("\to iterate directly over the elements of a container, "
2510 & "write `of &`", Name
(N
), Original_Node
(Name
(N
)));
2512 -- No point in continuing analysis of iterator spec
2518 -- If the name is a call (typically prefixed) to some Iterate
2519 -- function, it has been rewritten as an object declaration.
2520 -- If that object is a selected component, verify that it is not
2521 -- a component of an unconstrained mutable object.
2523 if Nkind
(Iter_Name
) = N_Identifier
2524 or else (not Expander_Active
and Comes_From_Source
(Iter_Name
))
2527 Orig_Node
: constant Node_Id
:= Original_Node
(Iter_Name
);
2528 Iter_Kind
: constant Node_Kind
:= Nkind
(Orig_Node
);
2532 if Iter_Kind
= N_Selected_Component
then
2533 Obj
:= Prefix
(Orig_Node
);
2535 elsif Iter_Kind
= N_Function_Call
then
2536 Obj
:= First_Actual
(Orig_Node
);
2538 -- If neither, the name comes from source
2544 if Nkind
(Obj
) = N_Selected_Component
2545 and then Is_Dependent_Component_Of_Mutable_Object
(Obj
)
2548 ("container cannot be a discriminant-dependent "
2549 & "component of a mutable object", N
);
2554 -- The result type of Iterate function is the classwide type of
2555 -- the interface parent. We need the specific Cursor type defined
2556 -- in the container package. We obtain it by name for a predefined
2557 -- container, or through the Iterable aspect for a formal one.
2559 if Has_Aspect
(Typ
, Aspect_Iterable
) then
2562 (Parent
(Find_Value_Of_Aspect
(Typ
, Aspect_Iterable
)),
2566 Set_Etype
(Def_Id
, Get_Cursor_Type
(Typ
));
2567 Check_Reverse_Iteration
(Etype
(Iter_Name
));
2572 end Analyze_Iterator_Specification
;
2578 -- Note: the semantic work required for analyzing labels (setting them as
2579 -- reachable) was done in a prepass through the statements in the block,
2580 -- so that forward gotos would be properly handled. See Analyze_Statements
2581 -- for further details. The only processing required here is to deal with
2582 -- optimizations that depend on an assumption of sequential control flow,
2583 -- since of course the occurrence of a label breaks this assumption.
2585 procedure Analyze_Label
(N
: Node_Id
) is
2586 pragma Warnings
(Off
, N
);
2588 Kill_Current_Values
;
2591 --------------------------
2592 -- Analyze_Label_Entity --
2593 --------------------------
2595 procedure Analyze_Label_Entity
(E
: Entity_Id
) is
2597 Set_Ekind
(E
, E_Label
);
2598 Set_Etype
(E
, Standard_Void_Type
);
2599 Set_Enclosing_Scope
(E
, Current_Scope
);
2600 Set_Reachable
(E
, True);
2601 end Analyze_Label_Entity
;
2603 ------------------------------------------
2604 -- Analyze_Loop_Parameter_Specification --
2605 ------------------------------------------
2607 procedure Analyze_Loop_Parameter_Specification
(N
: Node_Id
) is
2608 Loop_Nod
: constant Node_Id
:= Parent
(Parent
(N
));
2610 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
);
2611 -- If the bounds are given by a 'Range reference on a function call
2612 -- that returns a controlled array, introduce an explicit declaration
2613 -- to capture the bounds, so that the function result can be finalized
2614 -- in timely fashion.
2616 procedure Check_Predicate_Use
(T
: Entity_Id
);
2617 -- Diagnose Attempt to iterate through non-static predicate. Note that
2618 -- a type with inherited predicates may have both static and dynamic
2619 -- forms. In this case it is not sufficent to check the static predicate
2620 -- function only, look for a dynamic predicate aspect as well.
2622 function Has_Call_Using_Secondary_Stack
(N
: Node_Id
) return Boolean;
2623 -- N is the node for an arbitrary construct. This function searches the
2624 -- construct N to see if any expressions within it contain function
2625 -- calls that use the secondary stack, returning True if any such call
2626 -- is found, and False otherwise.
2628 procedure Process_Bounds
(R
: Node_Id
);
2629 -- If the iteration is given by a range, create temporaries and
2630 -- assignment statements block to capture the bounds and perform
2631 -- required finalization actions in case a bound includes a function
2632 -- call that uses the temporary stack. We first pre-analyze a copy of
2633 -- the range in order to determine the expected type, and analyze and
2634 -- resolve the original bounds.
2636 --------------------------------------
2637 -- Check_Controlled_Array_Attribute --
2638 --------------------------------------
2640 procedure Check_Controlled_Array_Attribute
(DS
: Node_Id
) is
2642 if Nkind
(DS
) = N_Attribute_Reference
2643 and then Is_Entity_Name
(Prefix
(DS
))
2644 and then Ekind
(Entity
(Prefix
(DS
))) = E_Function
2645 and then Is_Array_Type
(Etype
(Entity
(Prefix
(DS
))))
2647 Is_Controlled
(Component_Type
(Etype
(Entity
(Prefix
(DS
)))))
2648 and then Expander_Active
2651 Loc
: constant Source_Ptr
:= Sloc
(N
);
2652 Arr
: constant Entity_Id
:= Etype
(Entity
(Prefix
(DS
)));
2653 Indx
: constant Entity_Id
:=
2654 Base_Type
(Etype
(First_Index
(Arr
)));
2655 Subt
: constant Entity_Id
:= Make_Temporary
(Loc
, 'S');
2660 Make_Subtype_Declaration
(Loc
,
2661 Defining_Identifier
=> Subt
,
2662 Subtype_Indication
=>
2663 Make_Subtype_Indication
(Loc
,
2664 Subtype_Mark
=> New_Occurrence_Of
(Indx
, Loc
),
2666 Make_Range_Constraint
(Loc
, Relocate_Node
(DS
))));
2667 Insert_Before
(Loop_Nod
, Decl
);
2671 Make_Attribute_Reference
(Loc
,
2672 Prefix
=> New_Occurrence_Of
(Subt
, Loc
),
2673 Attribute_Name
=> Attribute_Name
(DS
)));
2678 end Check_Controlled_Array_Attribute
;
2680 -------------------------
2681 -- Check_Predicate_Use --
2682 -------------------------
2684 procedure Check_Predicate_Use
(T
: Entity_Id
) is
2686 -- A predicated subtype is illegal in loops and related constructs
2687 -- if the predicate is not static, or if it is a non-static subtype
2688 -- of a statically predicated subtype.
2690 if Is_Discrete_Type
(T
)
2691 and then Has_Predicates
(T
)
2692 and then (not Has_Static_Predicate
(T
)
2693 or else not Is_Static_Subtype
(T
)
2694 or else Has_Dynamic_Predicate_Aspect
(T
))
2696 -- Seems a confusing message for the case of a static predicate
2697 -- with a non-static subtype???
2699 Bad_Predicated_Subtype_Use
2700 ("cannot use subtype& with non-static predicate for loop "
2701 & "iteration", Discrete_Subtype_Definition
(N
),
2702 T
, Suggest_Static
=> True);
2704 elsif Inside_A_Generic
2705 and then Is_Generic_Formal
(T
)
2706 and then Is_Discrete_Type
(T
)
2708 Set_No_Dynamic_Predicate_On_Actual
(T
);
2710 end Check_Predicate_Use
;
2712 ------------------------------------
2713 -- Has_Call_Using_Secondary_Stack --
2714 ------------------------------------
2716 function Has_Call_Using_Secondary_Stack
(N
: Node_Id
) return Boolean is
2718 function Check_Call
(N
: Node_Id
) return Traverse_Result
;
2719 -- Check if N is a function call which uses the secondary stack
2725 function Check_Call
(N
: Node_Id
) return Traverse_Result
is
2728 Return_Typ
: Entity_Id
;
2731 if Nkind
(N
) = N_Function_Call
then
2734 -- Call using access to subprogram with explicit dereference
2736 if Nkind
(Nam
) = N_Explicit_Dereference
then
2737 Subp
:= Etype
(Nam
);
2739 -- Call using a selected component notation or Ada 2005 object
2740 -- operation notation
2742 elsif Nkind
(Nam
) = N_Selected_Component
then
2743 Subp
:= Entity
(Selector_Name
(Nam
));
2748 Subp
:= Entity
(Nam
);
2751 Return_Typ
:= Etype
(Subp
);
2753 if Is_Composite_Type
(Return_Typ
)
2754 and then not Is_Constrained
(Return_Typ
)
2758 elsif Sec_Stack_Needed_For_Return
(Subp
) then
2763 -- Continue traversing the tree
2768 function Check_Calls
is new Traverse_Func
(Check_Call
);
2770 -- Start of processing for Has_Call_Using_Secondary_Stack
2773 return Check_Calls
(N
) = Abandon
;
2774 end Has_Call_Using_Secondary_Stack
;
2776 --------------------
2777 -- Process_Bounds --
2778 --------------------
2780 procedure Process_Bounds
(R
: Node_Id
) is
2781 Loc
: constant Source_Ptr
:= Sloc
(N
);
2784 (Original_Bound
: Node_Id
;
2785 Analyzed_Bound
: Node_Id
;
2786 Typ
: Entity_Id
) return Node_Id
;
2787 -- Capture value of bound and return captured value
2794 (Original_Bound
: Node_Id
;
2795 Analyzed_Bound
: Node_Id
;
2796 Typ
: Entity_Id
) return Node_Id
2803 -- If the bound is a constant or an object, no need for a separate
2804 -- declaration. If the bound is the result of previous expansion
2805 -- it is already analyzed and should not be modified. Note that
2806 -- the Bound will be resolved later, if needed, as part of the
2807 -- call to Make_Index (literal bounds may need to be resolved to
2810 if Analyzed
(Original_Bound
) then
2811 return Original_Bound
;
2813 elsif Nkind_In
(Analyzed_Bound
, N_Integer_Literal
,
2814 N_Character_Literal
)
2815 or else Is_Entity_Name
(Analyzed_Bound
)
2817 Analyze_And_Resolve
(Original_Bound
, Typ
);
2818 return Original_Bound
;
2821 -- Normally, the best approach is simply to generate a constant
2822 -- declaration that captures the bound. However, there is a nasty
2823 -- case where this is wrong. If the bound is complex, and has a
2824 -- possible use of the secondary stack, we need to generate a
2825 -- separate assignment statement to ensure the creation of a block
2826 -- which will release the secondary stack.
2828 -- We prefer the constant declaration, since it leaves us with a
2829 -- proper trace of the value, useful in optimizations that get rid
2830 -- of junk range checks.
2832 if not Has_Call_Using_Secondary_Stack
(Analyzed_Bound
) then
2833 Analyze_And_Resolve
(Original_Bound
, Typ
);
2835 -- Ensure that the bound is valid. This check should not be
2836 -- generated when the range belongs to a quantified expression
2837 -- as the construct is still not expanded into its final form.
2839 if Nkind
(Parent
(R
)) /= N_Loop_Parameter_Specification
2840 or else Nkind
(Parent
(Parent
(R
))) /= N_Quantified_Expression
2842 Ensure_Valid
(Original_Bound
);
2845 Force_Evaluation
(Original_Bound
);
2846 return Original_Bound
;
2849 Id
:= Make_Temporary
(Loc
, 'R', Original_Bound
);
2851 -- Here we make a declaration with a separate assignment
2852 -- statement, and insert before loop header.
2855 Make_Object_Declaration
(Loc
,
2856 Defining_Identifier
=> Id
,
2857 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
2860 Make_Assignment_Statement
(Loc
,
2861 Name
=> New_Occurrence_Of
(Id
, Loc
),
2862 Expression
=> Relocate_Node
(Original_Bound
));
2864 Insert_Actions
(Loop_Nod
, New_List
(Decl
, Assign
));
2866 -- Now that this temporary variable is initialized we decorate it
2867 -- as safe-to-reevaluate to inform to the backend that no further
2868 -- asignment will be issued and hence it can be handled as side
2869 -- effect free. Note that this decoration must be done when the
2870 -- assignment has been analyzed because otherwise it will be
2871 -- rejected (see Analyze_Assignment).
2873 Set_Is_Safe_To_Reevaluate
(Id
);
2875 Rewrite
(Original_Bound
, New_Occurrence_Of
(Id
, Loc
));
2877 if Nkind
(Assign
) = N_Assignment_Statement
then
2878 return Expression
(Assign
);
2880 return Original_Bound
;
2884 Hi
: constant Node_Id
:= High_Bound
(R
);
2885 Lo
: constant Node_Id
:= Low_Bound
(R
);
2886 R_Copy
: constant Node_Id
:= New_Copy_Tree
(R
);
2891 -- Start of processing for Process_Bounds
2894 Set_Parent
(R_Copy
, Parent
(R
));
2895 Preanalyze_Range
(R_Copy
);
2896 Typ
:= Etype
(R_Copy
);
2898 -- If the type of the discrete range is Universal_Integer, then the
2899 -- bound's type must be resolved to Integer, and any object used to
2900 -- hold the bound must also have type Integer, unless the literal
2901 -- bounds are constant-folded expressions with a user-defined type.
2903 if Typ
= Universal_Integer
then
2904 if Nkind
(Lo
) = N_Integer_Literal
2905 and then Present
(Etype
(Lo
))
2906 and then Scope
(Etype
(Lo
)) /= Standard_Standard
2910 elsif Nkind
(Hi
) = N_Integer_Literal
2911 and then Present
(Etype
(Hi
))
2912 and then Scope
(Etype
(Hi
)) /= Standard_Standard
2917 Typ
:= Standard_Integer
;
2923 New_Lo
:= One_Bound
(Lo
, Low_Bound
(R_Copy
), Typ
);
2924 New_Hi
:= One_Bound
(Hi
, High_Bound
(R_Copy
), Typ
);
2926 -- Propagate staticness to loop range itself, in case the
2927 -- corresponding subtype is static.
2929 if New_Lo
/= Lo
and then Is_OK_Static_Expression
(New_Lo
) then
2930 Rewrite
(Low_Bound
(R
), New_Copy
(New_Lo
));
2933 if New_Hi
/= Hi
and then Is_OK_Static_Expression
(New_Hi
) then
2934 Rewrite
(High_Bound
(R
), New_Copy
(New_Hi
));
2940 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(N
);
2941 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2945 -- Start of processing for Analyze_Loop_Parameter_Specification
2950 -- We always consider the loop variable to be referenced, since the loop
2951 -- may be used just for counting purposes.
2953 Generate_Reference
(Id
, N
, ' ');
2955 -- Check for the case of loop variable hiding a local variable (used
2956 -- later on to give a nice warning if the hidden variable is never
2960 H
: constant Entity_Id
:= Homonym
(Id
);
2963 and then Ekind
(H
) = E_Variable
2964 and then Is_Discrete_Type
(Etype
(H
))
2965 and then Enclosing_Dynamic_Scope
(H
) = Enclosing_Dynamic_Scope
(Id
)
2967 Set_Hiding_Loop_Variable
(H
, Id
);
2971 -- Loop parameter specification must include subtype mark in SPARK
2973 if Nkind
(DS
) = N_Range
then
2974 Check_SPARK_05_Restriction
2975 ("loop parameter specification must include subtype mark", N
);
2978 -- Analyze the subtype definition and create temporaries for the bounds.
2979 -- Do not evaluate the range when preanalyzing a quantified expression
2980 -- because bounds expressed as function calls with side effects will be
2981 -- incorrectly replicated.
2983 if Nkind
(DS
) = N_Range
2984 and then Expander_Active
2985 and then Nkind
(Parent
(N
)) /= N_Quantified_Expression
2987 Process_Bounds
(DS
);
2989 -- Either the expander not active or the range of iteration is a subtype
2990 -- indication, an entity, or a function call that yields an aggregate or
2994 DS_Copy
:= New_Copy_Tree
(DS
);
2995 Set_Parent
(DS_Copy
, Parent
(DS
));
2996 Preanalyze_Range
(DS_Copy
);
2998 -- Ada 2012: If the domain of iteration is:
3000 -- a) a function call,
3001 -- b) an identifier that is not a type,
3002 -- c) an attribute reference 'Old (within a postcondition),
3003 -- d) an unchecked conversion or a qualified expression with
3004 -- the proper iterator type.
3006 -- then it is an iteration over a container. It was classified as
3007 -- a loop specification by the parser, and must be rewritten now
3008 -- to activate container iteration. The last case will occur within
3009 -- an expanded inlined call, where the expansion wraps an actual in
3010 -- an unchecked conversion when needed. The expression of the
3011 -- conversion is always an object.
3013 if Nkind
(DS_Copy
) = N_Function_Call
3015 or else (Is_Entity_Name
(DS_Copy
)
3016 and then not Is_Type
(Entity
(DS_Copy
)))
3018 or else (Nkind
(DS_Copy
) = N_Attribute_Reference
3019 and then Nam_In
(Attribute_Name
(DS_Copy
),
3020 Name_Loop_Entry
, Name_Old
))
3022 or else Has_Aspect
(Etype
(DS_Copy
), Aspect_Iterable
)
3024 or else Nkind
(DS_Copy
) = N_Unchecked_Type_Conversion
3025 or else (Nkind
(DS_Copy
) = N_Qualified_Expression
3026 and then Is_Iterator
(Etype
(DS_Copy
)))
3028 -- This is an iterator specification. Rewrite it as such and
3029 -- analyze it to capture function calls that may require
3030 -- finalization actions.
3033 I_Spec
: constant Node_Id
:=
3034 Make_Iterator_Specification
(Sloc
(N
),
3035 Defining_Identifier
=> Relocate_Node
(Id
),
3037 Subtype_Indication
=> Empty
,
3038 Reverse_Present
=> Reverse_Present
(N
));
3039 Scheme
: constant Node_Id
:= Parent
(N
);
3042 Set_Iterator_Specification
(Scheme
, I_Spec
);
3043 Set_Loop_Parameter_Specification
(Scheme
, Empty
);
3044 Analyze_Iterator_Specification
(I_Spec
);
3046 -- In a generic context, analyze the original domain of
3047 -- iteration, for name capture.
3049 if not Expander_Active
then
3053 -- Set kind of loop parameter, which may be used in the
3054 -- subsequent analysis of the condition in a quantified
3057 Set_Ekind
(Id
, E_Loop_Parameter
);
3061 -- Domain of iteration is not a function call, and is side-effect
3065 -- A quantified expression that appears in a pre/post condition
3066 -- is pre-analyzed several times. If the range is given by an
3067 -- attribute reference it is rewritten as a range, and this is
3068 -- done even with expansion disabled. If the type is already set
3069 -- do not reanalyze, because a range with static bounds may be
3070 -- typed Integer by default.
3072 if Nkind
(Parent
(N
)) = N_Quantified_Expression
3073 and then Present
(Etype
(DS
))
3086 -- Some additional checks if we are iterating through a type
3088 if Is_Entity_Name
(DS
)
3089 and then Present
(Entity
(DS
))
3090 and then Is_Type
(Entity
(DS
))
3092 -- The subtype indication may denote the completion of an incomplete
3093 -- type declaration.
3095 if Ekind
(Entity
(DS
)) = E_Incomplete_Type
then
3096 Set_Entity
(DS
, Get_Full_View
(Entity
(DS
)));
3097 Set_Etype
(DS
, Entity
(DS
));
3100 Check_Predicate_Use
(Entity
(DS
));
3103 -- Error if not discrete type
3105 if not Is_Discrete_Type
(Etype
(DS
)) then
3106 Wrong_Type
(DS
, Any_Discrete
);
3107 Set_Etype
(DS
, Any_Type
);
3110 Check_Controlled_Array_Attribute
(DS
);
3112 if Nkind
(DS
) = N_Subtype_Indication
then
3113 Check_Predicate_Use
(Entity
(Subtype_Mark
(DS
)));
3116 Make_Index
(DS
, N
, In_Iter_Schm
=> True);
3117 Set_Ekind
(Id
, E_Loop_Parameter
);
3119 -- A quantified expression which appears in a pre- or post-condition may
3120 -- be analyzed multiple times. The analysis of the range creates several
3121 -- itypes which reside in different scopes depending on whether the pre-
3122 -- or post-condition has been expanded. Update the type of the loop
3123 -- variable to reflect the proper itype at each stage of analysis.
3126 or else Etype
(Id
) = Any_Type
3128 (Present
(Etype
(Id
))
3129 and then Is_Itype
(Etype
(Id
))
3130 and then Nkind
(Parent
(Loop_Nod
)) = N_Expression_With_Actions
3131 and then Nkind
(Original_Node
(Parent
(Loop_Nod
))) =
3132 N_Quantified_Expression
)
3134 Set_Etype
(Id
, Etype
(DS
));
3137 -- Treat a range as an implicit reference to the type, to inhibit
3138 -- spurious warnings.
3140 Generate_Reference
(Base_Type
(Etype
(DS
)), N
, ' ');
3141 Set_Is_Known_Valid
(Id
, True);
3143 -- The loop is not a declarative part, so the loop variable must be
3144 -- frozen explicitly. Do not freeze while preanalyzing a quantified
3145 -- expression because the freeze node will not be inserted into the
3146 -- tree due to flag Is_Spec_Expression being set.
3148 if Nkind
(Parent
(N
)) /= N_Quantified_Expression
then
3150 Flist
: constant List_Id
:= Freeze_Entity
(Id
, N
);
3152 if Is_Non_Empty_List
(Flist
) then
3153 Insert_Actions
(N
, Flist
);
3158 -- Case where we have a range or a subtype, get type bounds
3160 if Nkind_In
(DS
, N_Range
, N_Subtype_Indication
)
3161 and then not Error_Posted
(DS
)
3162 and then Etype
(DS
) /= Any_Type
3163 and then Is_Discrete_Type
(Etype
(DS
))
3170 if Nkind
(DS
) = N_Range
then
3171 L
:= Low_Bound
(DS
);
3172 H
:= High_Bound
(DS
);
3175 Type_Low_Bound
(Underlying_Type
(Etype
(Subtype_Mark
(DS
))));
3177 Type_High_Bound
(Underlying_Type
(Etype
(Subtype_Mark
(DS
))));
3180 -- Check for null or possibly null range and issue warning. We
3181 -- suppress such messages in generic templates and instances,
3182 -- because in practice they tend to be dubious in these cases. The
3183 -- check applies as well to rewritten array element loops where a
3184 -- null range may be detected statically.
3186 if Compile_Time_Compare
(L
, H
, Assume_Valid
=> True) = GT
then
3188 -- Suppress the warning if inside a generic template or
3189 -- instance, since in practice they tend to be dubious in these
3190 -- cases since they can result from intended parameterization.
3192 if not Inside_A_Generic
and then not In_Instance
then
3194 -- Specialize msg if invalid values could make the loop
3195 -- non-null after all.
3197 if Compile_Time_Compare
3198 (L
, H
, Assume_Valid
=> False) = GT
3200 -- Since we know the range of the loop is null, set the
3201 -- appropriate flag to remove the loop entirely during
3204 Set_Is_Null_Loop
(Loop_Nod
);
3206 if Comes_From_Source
(N
) then
3208 ("??loop range is null, loop will not execute", DS
);
3211 -- Here is where the loop could execute because of
3212 -- invalid values, so issue appropriate message and in
3213 -- this case we do not set the Is_Null_Loop flag since
3214 -- the loop may execute.
3216 elsif Comes_From_Source
(N
) then
3218 ("??loop range may be null, loop may not execute",
3221 ("??can only execute if invalid values are present",
3226 -- In either case, suppress warnings in the body of the loop,
3227 -- since it is likely that these warnings will be inappropriate
3228 -- if the loop never actually executes, which is likely.
3230 Set_Suppress_Loop_Warnings
(Loop_Nod
);
3232 -- The other case for a warning is a reverse loop where the
3233 -- upper bound is the integer literal zero or one, and the
3234 -- lower bound may exceed this value.
3236 -- For example, we have
3238 -- for J in reverse N .. 1 loop
3240 -- In practice, this is very likely to be a case of reversing
3241 -- the bounds incorrectly in the range.
3243 elsif Reverse_Present
(N
)
3244 and then Nkind
(Original_Node
(H
)) = N_Integer_Literal
3246 (Intval
(Original_Node
(H
)) = Uint_0
3248 Intval
(Original_Node
(H
)) = Uint_1
)
3250 -- Lower bound may in fact be known and known not to exceed
3251 -- upper bound (e.g. reverse 0 .. 1) and that's OK.
3253 if Compile_Time_Known_Value
(L
)
3254 and then Expr_Value
(L
) <= Expr_Value
(H
)
3258 -- Otherwise warning is warranted
3261 Error_Msg_N
("??loop range may be null", DS
);
3262 Error_Msg_N
("\??bounds may be wrong way round", DS
);
3266 -- Check if either bound is known to be outside the range of the
3267 -- loop parameter type, this is e.g. the case of a loop from
3268 -- 20..X where the type is 1..19.
3270 -- Such a loop is dubious since either it raises CE or it executes
3271 -- zero times, and that cannot be useful!
3273 if Etype
(DS
) /= Any_Type
3274 and then not Error_Posted
(DS
)
3275 and then Nkind
(DS
) = N_Subtype_Indication
3276 and then Nkind
(Constraint
(DS
)) = N_Range_Constraint
3279 LLo
: constant Node_Id
:=
3280 Low_Bound
(Range_Expression
(Constraint
(DS
)));
3281 LHi
: constant Node_Id
:=
3282 High_Bound
(Range_Expression
(Constraint
(DS
)));
3284 Bad_Bound
: Node_Id
:= Empty
;
3285 -- Suspicious loop bound
3288 -- At this stage L, H are the bounds of the type, and LLo
3289 -- Lhi are the low bound and high bound of the loop.
3291 if Compile_Time_Compare
(LLo
, L
, Assume_Valid
=> True) = LT
3293 Compile_Time_Compare
(LLo
, H
, Assume_Valid
=> True) = GT
3298 if Compile_Time_Compare
(LHi
, L
, Assume_Valid
=> True) = LT
3300 Compile_Time_Compare
(LHi
, H
, Assume_Valid
=> True) = GT
3305 if Present
(Bad_Bound
) then
3307 ("suspicious loop bound out of range of "
3308 & "loop subtype??", Bad_Bound
);
3310 ("\loop executes zero times or raises "
3311 & "Constraint_Error??", Bad_Bound
);
3316 -- This declare block is about warnings, if we get an exception while
3317 -- testing for warnings, we simply abandon the attempt silently. This
3318 -- most likely occurs as the result of a previous error, but might
3319 -- just be an obscure case we have missed. In either case, not giving
3320 -- the warning is perfectly acceptable.
3323 when others => null;
3327 -- A loop parameter cannot be effectively volatile (SPARK RM 7.1.3(4)).
3328 -- This check is relevant only when SPARK_Mode is on as it is not a
3329 -- standard Ada legality check.
3331 if SPARK_Mode
= On
and then Is_Effectively_Volatile
(Id
) then
3332 Error_Msg_N
("loop parameter cannot be volatile", Id
);
3334 end Analyze_Loop_Parameter_Specification
;
3336 ----------------------------
3337 -- Analyze_Loop_Statement --
3338 ----------------------------
3340 procedure Analyze_Loop_Statement
(N
: Node_Id
) is
3342 function Is_Container_Iterator
(Iter
: Node_Id
) return Boolean;
3343 -- Given a loop iteration scheme, determine whether it is an Ada 2012
3344 -- container iteration.
3346 function Is_Wrapped_In_Block
(N
: Node_Id
) return Boolean;
3347 -- Determine whether loop statement N has been wrapped in a block to
3348 -- capture finalization actions that may be generated for container
3349 -- iterators. Prevents infinite recursion when block is analyzed.
3350 -- Routine is a noop if loop is single statement within source block.
3352 ---------------------------
3353 -- Is_Container_Iterator --
3354 ---------------------------
3356 function Is_Container_Iterator
(Iter
: Node_Id
) return Boolean is
3365 elsif Present
(Condition
(Iter
)) then
3368 -- for Def_Id in [reverse] Name loop
3369 -- for Def_Id [: Subtype_Indication] of [reverse] Name loop
3371 elsif Present
(Iterator_Specification
(Iter
)) then
3373 Nam
: constant Node_Id
:= Name
(Iterator_Specification
(Iter
));
3377 Nam_Copy
:= New_Copy_Tree
(Nam
);
3378 Set_Parent
(Nam_Copy
, Parent
(Nam
));
3379 Preanalyze_Range
(Nam_Copy
);
3381 -- The only two options here are iteration over a container or
3384 return not Is_Array_Type
(Etype
(Nam_Copy
));
3387 -- for Def_Id in [reverse] Discrete_Subtype_Definition loop
3391 LP
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
3392 DS
: constant Node_Id
:= Discrete_Subtype_Definition
(LP
);
3396 DS_Copy
:= New_Copy_Tree
(DS
);
3397 Set_Parent
(DS_Copy
, Parent
(DS
));
3398 Preanalyze_Range
(DS_Copy
);
3400 -- Check for a call to Iterate () or an expression with
3401 -- an iterator type.
3404 (Nkind
(DS_Copy
) = N_Function_Call
3405 and then Needs_Finalization
(Etype
(DS_Copy
)))
3406 or else Is_Iterator
(Etype
(DS_Copy
));
3409 end Is_Container_Iterator
;
3411 -------------------------
3412 -- Is_Wrapped_In_Block --
3413 -------------------------
3415 function Is_Wrapped_In_Block
(N
: Node_Id
) return Boolean is
3421 -- Check if current scope is a block that is not a transient block.
3423 if Ekind
(Current_Scope
) /= E_Block
3424 or else No
(Block_Node
(Current_Scope
))
3430 Handled_Statement_Sequence
(Parent
(Block_Node
(Current_Scope
)));
3432 -- Skip leading pragmas that may be introduced for invariant and
3433 -- predicate checks.
3435 Stat
:= First
(Statements
(HSS
));
3436 while Present
(Stat
) and then Nkind
(Stat
) = N_Pragma
loop
3437 Stat
:= Next
(Stat
);
3440 return Stat
= N
and then No
(Next
(Stat
));
3442 end Is_Wrapped_In_Block
;
3444 -- Local declarations
3446 Id
: constant Node_Id
:= Identifier
(N
);
3447 Iter
: constant Node_Id
:= Iteration_Scheme
(N
);
3448 Loc
: constant Source_Ptr
:= Sloc
(N
);
3452 -- Start of processing for Analyze_Loop_Statement
3455 if Present
(Id
) then
3457 -- Make name visible, e.g. for use in exit statements. Loop labels
3458 -- are always considered to be referenced.
3463 -- Guard against serious error (typically, a scope mismatch when
3464 -- semantic analysis is requested) by creating loop entity to
3465 -- continue analysis.
3468 if Total_Errors_Detected
/= 0 then
3469 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Loc
, 'L');
3471 raise Program_Error
;
3474 -- Verify that the loop name is hot hidden by an unrelated
3475 -- declaration in an inner scope.
3477 elsif Ekind
(Ent
) /= E_Label
and then Ekind
(Ent
) /= E_Loop
then
3478 Error_Msg_Sloc
:= Sloc
(Ent
);
3479 Error_Msg_N
("implicit label declaration for & is hidden#", Id
);
3481 if Present
(Homonym
(Ent
))
3482 and then Ekind
(Homonym
(Ent
)) = E_Label
3484 Set_Entity
(Id
, Ent
);
3485 Set_Ekind
(Ent
, E_Loop
);
3489 Generate_Reference
(Ent
, N
, ' ');
3490 Generate_Definition
(Ent
);
3492 -- If we found a label, mark its type. If not, ignore it, since it
3493 -- means we have a conflicting declaration, which would already
3494 -- have been diagnosed at declaration time. Set Label_Construct
3495 -- of the implicit label declaration, which is not created by the
3496 -- parser for generic units.
3498 if Ekind
(Ent
) = E_Label
then
3499 Set_Ekind
(Ent
, E_Loop
);
3501 if Nkind
(Parent
(Ent
)) = N_Implicit_Label_Declaration
then
3502 Set_Label_Construct
(Parent
(Ent
), N
);
3507 -- Case of no identifier present. Create one and attach it to the
3508 -- loop statement for use as a scope and as a reference for later
3509 -- expansions. Indicate that the label does not come from source,
3510 -- and attach it to the loop statement so it is part of the tree,
3511 -- even without a full declaration.
3514 Ent
:= New_Internal_Entity
(E_Loop
, Current_Scope
, Loc
, 'L');
3515 Set_Etype
(Ent
, Standard_Void_Type
);
3516 Set_Identifier
(N
, New_Occurrence_Of
(Ent
, Loc
));
3517 Set_Parent
(Ent
, N
);
3518 Set_Has_Created_Identifier
(N
);
3521 -- If the iterator specification has a syntactic error, transform
3522 -- construct into an infinite loop to prevent a crash and perform
3526 and then Present
(Iterator_Specification
(Iter
))
3527 and then Error_Posted
(Iterator_Specification
(Iter
))
3529 Set_Iteration_Scheme
(N
, Empty
);
3534 -- Iteration over a container in Ada 2012 involves the creation of a
3535 -- controlled iterator object. Wrap the loop in a block to ensure the
3536 -- timely finalization of the iterator and release of container locks.
3537 -- The same applies to the use of secondary stack when obtaining an
3540 if Ada_Version
>= Ada_2012
3541 and then Is_Container_Iterator
(Iter
)
3542 and then not Is_Wrapped_In_Block
(N
)
3545 Block_Nod
: Node_Id
;
3546 Block_Id
: Entity_Id
;
3550 Make_Block_Statement
(Loc
,
3551 Declarations
=> New_List
,
3552 Handled_Statement_Sequence
=>
3553 Make_Handled_Sequence_Of_Statements
(Loc
,
3554 Statements
=> New_List
(Relocate_Node
(N
))));
3556 Add_Block_Identifier
(Block_Nod
, Block_Id
);
3558 -- The expansion of iterator loops generates an iterator in order
3559 -- to traverse the elements of a container:
3561 -- Iter : <iterator type> := Iterate (Container)'reference;
3563 -- The iterator is controlled and returned on the secondary stack.
3564 -- The analysis of the call to Iterate establishes a transient
3565 -- scope to deal with the secondary stack management, but never
3566 -- really creates a physical block as this would kill the iterator
3567 -- too early (see Wrap_Transient_Declaration). To address this
3568 -- case, mark the generated block as needing secondary stack
3571 Set_Uses_Sec_Stack
(Block_Id
);
3573 Rewrite
(N
, Block_Nod
);
3579 -- Kill current values on entry to loop, since statements in the body of
3580 -- the loop may have been executed before the loop is entered. Similarly
3581 -- we kill values after the loop, since we do not know that the body of
3582 -- the loop was executed.
3584 Kill_Current_Values
;
3586 Analyze_Iteration_Scheme
(Iter
);
3588 -- Check for following case which merits a warning if the type E of is
3589 -- a multi-dimensional array (and no explicit subscript ranges present).
3595 and then Present
(Loop_Parameter_Specification
(Iter
))
3598 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Iter
);
3599 DSD
: constant Node_Id
:=
3600 Original_Node
(Discrete_Subtype_Definition
(LPS
));
3602 if Nkind
(DSD
) = N_Attribute_Reference
3603 and then Attribute_Name
(DSD
) = Name_Range
3604 and then No
(Expressions
(DSD
))
3607 Typ
: constant Entity_Id
:= Etype
(Prefix
(DSD
));
3609 if Is_Array_Type
(Typ
)
3610 and then Number_Dimensions
(Typ
) > 1
3611 and then Nkind
(Parent
(N
)) = N_Loop_Statement
3612 and then Present
(Iteration_Scheme
(Parent
(N
)))
3615 OIter
: constant Node_Id
:=
3616 Iteration_Scheme
(Parent
(N
));
3617 OLPS
: constant Node_Id
:=
3618 Loop_Parameter_Specification
(OIter
);
3619 ODSD
: constant Node_Id
:=
3620 Original_Node
(Discrete_Subtype_Definition
(OLPS
));
3622 if Nkind
(ODSD
) = N_Attribute_Reference
3623 and then Attribute_Name
(ODSD
) = Name_Range
3624 and then No
(Expressions
(ODSD
))
3625 and then Etype
(Prefix
(ODSD
)) = Typ
3627 Error_Msg_Sloc
:= Sloc
(ODSD
);
3629 ("inner range same as outer range#??", DSD
);
3638 -- Analyze the statements of the body except in the case of an Ada 2012
3639 -- iterator with the expander active. In this case the expander will do
3640 -- a rewrite of the loop into a while loop. We will then analyze the
3641 -- loop body when we analyze this while loop.
3643 -- We need to do this delay because if the container is for indefinite
3644 -- types the actual subtype of the components will only be determined
3645 -- when the cursor declaration is analyzed.
3647 -- If the expander is not active then we want to analyze the loop body
3648 -- now even in the Ada 2012 iterator case, since the rewriting will not
3649 -- be done. Insert the loop variable in the current scope, if not done
3650 -- when analysing the iteration scheme. Set its kind properly to detect
3651 -- improper uses in the loop body.
3653 -- In GNATprove mode, we do one of the above depending on the kind of
3654 -- loop. If it is an iterator over an array, then we do not analyze the
3655 -- loop now. We will analyze it after it has been rewritten by the
3656 -- special SPARK expansion which is activated in GNATprove mode. We need
3657 -- to do this so that other expansions that should occur in GNATprove
3658 -- mode take into account the specificities of the rewritten loop, in
3659 -- particular the introduction of a renaming (which needs to be
3662 -- In other cases in GNATprove mode then we want to analyze the loop
3663 -- body now, since no rewriting will occur. Within a generic the
3664 -- GNATprove mode is irrelevant, we must analyze the generic for
3665 -- non-local name capture.
3668 and then Present
(Iterator_Specification
(Iter
))
3671 and then Is_Iterator_Over_Array
(Iterator_Specification
(Iter
))
3672 and then not Inside_A_Generic
3676 elsif not Expander_Active
then
3678 I_Spec
: constant Node_Id
:= Iterator_Specification
(Iter
);
3679 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3682 if Scope
(Id
) /= Current_Scope
then
3686 -- In an element iterator, The loop parameter is a variable if
3687 -- the domain of iteration (container or array) is a variable.
3689 if not Of_Present
(I_Spec
)
3690 or else not Is_Variable
(Name
(I_Spec
))
3692 Set_Ekind
(Id
, E_Loop_Parameter
);
3696 Analyze_Statements
(Statements
(N
));
3700 -- Pre-Ada2012 for-loops and while loops
3702 Analyze_Statements
(Statements
(N
));
3705 -- When the iteration scheme of a loop contains attribute 'Loop_Entry,
3706 -- the loop is transformed into a conditional block. Retrieve the loop.
3710 if Subject_To_Loop_Entry_Attributes
(Stmt
) then
3711 Stmt
:= Find_Loop_In_Conditional_Block
(Stmt
);
3714 -- Finish up processing for the loop. We kill all current values, since
3715 -- in general we don't know if the statements in the loop have been
3716 -- executed. We could do a bit better than this with a loop that we
3717 -- know will execute at least once, but it's not worth the trouble and
3718 -- the front end is not in the business of flow tracing.
3720 Process_End_Label
(Stmt
, 'e', Ent
);
3722 Kill_Current_Values
;
3724 -- Check for infinite loop. Skip check for generated code, since it
3725 -- justs waste time and makes debugging the routine called harder.
3727 -- Note that we have to wait till the body of the loop is fully analyzed
3728 -- before making this call, since Check_Infinite_Loop_Warning relies on
3729 -- being able to use semantic visibility information to find references.
3731 if Comes_From_Source
(Stmt
) then
3732 Check_Infinite_Loop_Warning
(Stmt
);
3735 -- Code after loop is unreachable if the loop has no WHILE or FOR and
3736 -- contains no EXIT statements within the body of the loop.
3738 if No
(Iter
) and then not Has_Exit
(Ent
) then
3739 Check_Unreachable_Code
(Stmt
);
3741 end Analyze_Loop_Statement
;
3743 ----------------------------
3744 -- Analyze_Null_Statement --
3745 ----------------------------
3747 -- Note: the semantics of the null statement is implemented by a single
3748 -- null statement, too bad everything isn't as simple as this.
3750 procedure Analyze_Null_Statement
(N
: Node_Id
) is
3751 pragma Warnings
(Off
, N
);
3754 end Analyze_Null_Statement
;
3756 -------------------------
3757 -- Analyze_Target_Name --
3758 -------------------------
3760 procedure Analyze_Target_Name
(N
: Node_Id
) is
3762 -- A target name has the type of the left-hand side of the enclosing
3765 Set_Etype
(N
, Etype
(Name
(Current_Assignment
)));
3766 end Analyze_Target_Name
;
3768 ------------------------
3769 -- Analyze_Statements --
3770 ------------------------
3772 procedure Analyze_Statements
(L
: List_Id
) is
3777 -- The labels declared in the statement list are reachable from
3778 -- statements in the list. We do this as a prepass so that any goto
3779 -- statement will be properly flagged if its target is not reachable.
3780 -- This is not required, but is nice behavior.
3783 while Present
(S
) loop
3784 if Nkind
(S
) = N_Label
then
3785 Analyze
(Identifier
(S
));
3786 Lab
:= Entity
(Identifier
(S
));
3788 -- If we found a label mark it as reachable
3790 if Ekind
(Lab
) = E_Label
then
3791 Generate_Definition
(Lab
);
3792 Set_Reachable
(Lab
);
3794 if Nkind
(Parent
(Lab
)) = N_Implicit_Label_Declaration
then
3795 Set_Label_Construct
(Parent
(Lab
), S
);
3798 -- If we failed to find a label, it means the implicit declaration
3799 -- of the label was hidden. A for-loop parameter can do this to
3800 -- a label with the same name inside the loop, since the implicit
3801 -- label declaration is in the innermost enclosing body or block
3805 Error_Msg_Sloc
:= Sloc
(Lab
);
3807 ("implicit label declaration for & is hidden#",
3815 -- Perform semantic analysis on all statements
3817 Conditional_Statements_Begin
;
3820 while Present
(S
) loop
3823 -- Remove dimension in all statements
3825 Remove_Dimension_In_Statement
(S
);
3829 Conditional_Statements_End
;
3831 -- Make labels unreachable. Visibility is not sufficient, because labels
3832 -- in one if-branch for example are not reachable from the other branch,
3833 -- even though their declarations are in the enclosing declarative part.
3836 while Present
(S
) loop
3837 if Nkind
(S
) = N_Label
then
3838 Set_Reachable
(Entity
(Identifier
(S
)), False);
3843 end Analyze_Statements
;
3845 ----------------------------
3846 -- Check_Unreachable_Code --
3847 ----------------------------
3849 procedure Check_Unreachable_Code
(N
: Node_Id
) is
3850 Error_Node
: Node_Id
;
3854 if Is_List_Member
(N
) and then Comes_From_Source
(N
) then
3859 Nxt
:= Original_Node
(Next
(N
));
3861 -- Skip past pragmas
3863 while Nkind
(Nxt
) = N_Pragma
loop
3864 Nxt
:= Original_Node
(Next
(Nxt
));
3867 -- If a label follows us, then we never have dead code, since
3868 -- someone could branch to the label, so we just ignore it, unless
3869 -- we are in formal mode where goto statements are not allowed.
3871 if Nkind
(Nxt
) = N_Label
3872 and then not Restriction_Check_Required
(SPARK_05
)
3876 -- Otherwise see if we have a real statement following us
3879 and then Comes_From_Source
(Nxt
)
3880 and then Is_Statement
(Nxt
)
3882 -- Special very annoying exception. If we have a return that
3883 -- follows a raise, then we allow it without a warning, since
3884 -- the Ada RM annoyingly requires a useless return here.
3886 if Nkind
(Original_Node
(N
)) /= N_Raise_Statement
3887 or else Nkind
(Nxt
) /= N_Simple_Return_Statement
3889 -- The rather strange shenanigans with the warning message
3890 -- here reflects the fact that Kill_Dead_Code is very good
3891 -- at removing warnings in deleted code, and this is one
3892 -- warning we would prefer NOT to have removed.
3896 -- If we have unreachable code, analyze and remove the
3897 -- unreachable code, since it is useless and we don't
3898 -- want to generate junk warnings.
3900 -- We skip this step if we are not in code generation mode
3901 -- or CodePeer mode.
3903 -- This is the one case where we remove dead code in the
3904 -- semantics as opposed to the expander, and we do not want
3905 -- to remove code if we are not in code generation mode,
3906 -- since this messes up the ASIS trees or loses useful
3907 -- information in the CodePeer tree.
3909 -- Note that one might react by moving the whole circuit to
3910 -- exp_ch5, but then we lose the warning in -gnatc mode.
3912 if Operating_Mode
= Generate_Code
3913 and then not CodePeer_Mode
3918 -- Quit deleting when we have nothing more to delete
3919 -- or if we hit a label (since someone could transfer
3920 -- control to a label, so we should not delete it).
3922 exit when No
(Nxt
) or else Nkind
(Nxt
) = N_Label
;
3924 -- Statement/declaration is to be deleted
3928 Kill_Dead_Code
(Nxt
);
3932 -- Now issue the warning (or error in formal mode)
3934 if Restriction_Check_Required
(SPARK_05
) then
3935 Check_SPARK_05_Restriction
3936 ("unreachable code is not allowed", Error_Node
);
3939 ("??unreachable code!", Sloc
(Error_Node
), Error_Node
);
3943 -- If the unconditional transfer of control instruction is the
3944 -- last statement of a sequence, then see if our parent is one of
3945 -- the constructs for which we count unblocked exits, and if so,
3946 -- adjust the count.
3951 -- Statements in THEN part or ELSE part of IF statement
3953 if Nkind
(P
) = N_If_Statement
then
3956 -- Statements in ELSIF part of an IF statement
3958 elsif Nkind
(P
) = N_Elsif_Part
then
3960 pragma Assert
(Nkind
(P
) = N_If_Statement
);
3962 -- Statements in CASE statement alternative
3964 elsif Nkind
(P
) = N_Case_Statement_Alternative
then
3966 pragma Assert
(Nkind
(P
) = N_Case_Statement
);
3968 -- Statements in body of block
3970 elsif Nkind
(P
) = N_Handled_Sequence_Of_Statements
3971 and then Nkind
(Parent
(P
)) = N_Block_Statement
3973 -- The original loop is now placed inside a block statement
3974 -- due to the expansion of attribute 'Loop_Entry. Return as
3975 -- this is not a "real" block for the purposes of exit
3978 if Nkind
(N
) = N_Loop_Statement
3979 and then Subject_To_Loop_Entry_Attributes
(N
)
3984 -- Statements in exception handler in a block
3986 elsif Nkind
(P
) = N_Exception_Handler
3987 and then Nkind
(Parent
(P
)) = N_Handled_Sequence_Of_Statements
3988 and then Nkind
(Parent
(Parent
(P
))) = N_Block_Statement
3992 -- None of these cases, so return
3998 -- This was one of the cases we are looking for (i.e. the
3999 -- parent construct was IF, CASE or block) so decrement count.
4001 Unblocked_Exit_Count
:= Unblocked_Exit_Count
- 1;
4005 end Check_Unreachable_Code
;
4007 ----------------------
4008 -- Preanalyze_Range --
4009 ----------------------
4011 procedure Preanalyze_Range
(R_Copy
: Node_Id
) is
4012 Save_Analysis
: constant Boolean := Full_Analysis
;
4016 Full_Analysis
:= False;
4017 Expander_Mode_Save_And_Set
(False);
4021 if Nkind
(R_Copy
) in N_Subexpr
and then Is_Overloaded
(R_Copy
) then
4023 -- Apply preference rules for range of predefined integer types, or
4024 -- check for array or iterable construct for "of" iterator, or
4025 -- diagnose true ambiguity.
4030 Found
: Entity_Id
:= Empty
;
4033 Get_First_Interp
(R_Copy
, I
, It
);
4034 while Present
(It
.Typ
) loop
4035 if Is_Discrete_Type
(It
.Typ
) then
4039 if Scope
(Found
) = Standard_Standard
then
4042 elsif Scope
(It
.Typ
) = Standard_Standard
then
4046 -- Both of them are user-defined
4049 ("ambiguous bounds in range of iteration", R_Copy
);
4050 Error_Msg_N
("\possible interpretations:", R_Copy
);
4051 Error_Msg_NE
("\\} ", R_Copy
, Found
);
4052 Error_Msg_NE
("\\} ", R_Copy
, It
.Typ
);
4057 elsif Nkind
(Parent
(R_Copy
)) = N_Iterator_Specification
4058 and then Of_Present
(Parent
(R_Copy
))
4060 if Is_Array_Type
(It
.Typ
)
4061 or else Has_Aspect
(It
.Typ
, Aspect_Iterator_Element
)
4062 or else Has_Aspect
(It
.Typ
, Aspect_Constant_Indexing
)
4063 or else Has_Aspect
(It
.Typ
, Aspect_Variable_Indexing
)
4067 Set_Etype
(R_Copy
, It
.Typ
);
4070 Error_Msg_N
("ambiguous domain of iteration", R_Copy
);
4075 Get_Next_Interp
(I
, It
);
4080 -- Subtype mark in iteration scheme
4082 if Is_Entity_Name
(R_Copy
) and then Is_Type
(Entity
(R_Copy
)) then
4085 -- Expression in range, or Ada 2012 iterator
4087 elsif Nkind
(R_Copy
) in N_Subexpr
then
4089 Typ
:= Etype
(R_Copy
);
4091 if Is_Discrete_Type
(Typ
) then
4094 -- Check that the resulting object is an iterable container
4096 elsif Has_Aspect
(Typ
, Aspect_Iterator_Element
)
4097 or else Has_Aspect
(Typ
, Aspect_Constant_Indexing
)
4098 or else Has_Aspect
(Typ
, Aspect_Variable_Indexing
)
4102 -- The expression may yield an implicit reference to an iterable
4103 -- container. Insert explicit dereference so that proper type is
4104 -- visible in the loop.
4106 elsif Has_Implicit_Dereference
(Etype
(R_Copy
)) then
4111 Disc
:= First_Discriminant
(Typ
);
4112 while Present
(Disc
) loop
4113 if Has_Implicit_Dereference
(Disc
) then
4114 Build_Explicit_Dereference
(R_Copy
, Disc
);
4118 Next_Discriminant
(Disc
);
4125 Expander_Mode_Restore
;
4126 Full_Analysis
:= Save_Analysis
;
4127 end Preanalyze_Range
;