1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Debug_A; use Debug_A;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
48 with Output; use Output;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch4; use Sem_Ch4;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Intr; use Sem_Intr;
64 with Sem_Util; use Sem_Util;
65 with Sem_Type; use Sem_Type;
66 with Sem_Warn; use Sem_Warn;
67 with Sinfo; use Sinfo;
68 with Snames; use Snames;
69 with Stand; use Stand;
70 with Stringt; use Stringt;
71 with Targparm; use Targparm;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
76 package body Sem_Res is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 -- Second pass (top-down) type checking and overload resolution procedures
83 -- Typ is the type required by context. These procedures propagate the
84 -- type information recursively to the descendants of N. If the node
85 -- is not overloaded, its Etype is established in the first pass. If
86 -- overloaded, the Resolve routines set the correct type. For arith.
87 -- operators, the Etype is the base type of the context.
89 -- Note that Resolve_Attribute is separated off in Sem_Attr
91 procedure Check_Discriminant_Use (N : Node_Id);
92 -- Enforce the restrictions on the use of discriminants when constraining
93 -- a component of a discriminated type (record or concurrent type).
95 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
96 -- Given a node for an operator associated with type T, check that
97 -- the operator is visible. Operators all of whose operands are
98 -- universal must be checked for visibility during resolution
99 -- because their type is not determinable based on their operands.
101 procedure Check_Fully_Declared_Prefix
104 -- Check that the type of the prefix of a dereference is not incomplete
106 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
107 -- Given a call node, N, which is known to occur immediately within the
108 -- subprogram being called, determines whether it is a detectable case of
109 -- an infinite recursion, and if so, outputs appropriate messages. Returns
110 -- True if an infinite recursion is detected, and False otherwise.
112 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
113 -- If the type of the object being initialized uses the secondary stack
114 -- directly or indirectly, create a transient scope for the call to the
115 -- init proc. This is because we do not create transient scopes for the
116 -- initialization of individual components within the init proc itself.
117 -- Could be optimized away perhaps?
119 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
120 -- Utility to check whether the name in the call is a predefined
121 -- operator, in which case the call is made into an operator node.
122 -- An instance of an intrinsic conversion operation may be given
123 -- an operator name, but is not treated like an operator.
125 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
126 -- If a default expression in entry call N depends on the discriminants
127 -- of the task, it must be replaced with a reference to the discriminant
128 -- of the task being called.
130 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
131 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
132 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
133 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
134 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
135 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
136 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
137 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
138 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
139 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
140 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
141 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
142 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
143 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
144 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
145 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
146 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
147 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
148 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
149 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
150 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
151 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
152 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
153 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
154 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
155 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
156 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
157 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
158 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
159 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
160 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
161 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
163 function Operator_Kind
165 Is_Binary : Boolean) return Node_Kind;
166 -- Utility to map the name of an operator into the corresponding Node. Used
167 -- by other node rewriting procedures.
169 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
170 -- Resolve actuals of call, and add default expressions for missing ones.
171 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
172 -- called subprogram.
174 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
175 -- Called from Resolve_Call, when the prefix denotes an entry or element
176 -- of entry family. Actuals are resolved as for subprograms, and the node
177 -- is rebuilt as an entry call. Also called for protected operations. Typ
178 -- is the context type, which is used when the operation is a protected
179 -- function with no arguments, and the return value is indexed.
181 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
182 -- A call to a user-defined intrinsic operator is rewritten as a call
183 -- to the corresponding predefined operator, with suitable conversions.
185 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
186 -- Ditto, for unary operators (only arithmetic ones)
188 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
189 -- If an operator node resolves to a call to a user-defined operator,
190 -- rewrite the node as a function call.
192 procedure Make_Call_Into_Operator
196 -- Inverse transformation: if an operator is given in functional notation,
197 -- then after resolving the node, transform into an operator node, so
198 -- that operands are resolved properly. Recall that predefined operators
199 -- do not have a full signature and special resolution rules apply.
201 procedure Rewrite_Renamed_Operator
205 -- An operator can rename another, e.g. in an instantiation. In that
206 -- case, the proper operator node must be constructed and resolved.
208 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
209 -- The String_Literal_Subtype is built for all strings that are not
210 -- operands of a static concatenation operation. If the argument is
211 -- not a N_String_Literal node, then the call has no effect.
213 procedure Set_Slice_Subtype (N : Node_Id);
214 -- Build subtype of array type, with the range specified by the slice
216 procedure Simplify_Type_Conversion (N : Node_Id);
217 -- Called after N has been resolved and evaluated, but before range checks
218 -- have been applied. Currently simplifies a combination of floating-point
219 -- to integer conversion and Truncation attribute.
221 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
222 -- A universal_fixed expression in an universal context is unambiguous
223 -- if there is only one applicable fixed point type. Determining whether
224 -- there is only one requires a search over all visible entities, and
225 -- happens only in very pathological cases (see 6115-006).
227 function Valid_Conversion
230 Operand : Node_Id) return Boolean;
231 -- Verify legality rules given in 4.6 (8-23). Target is the target
232 -- type of the conversion, which may be an implicit conversion of
233 -- an actual parameter to an anonymous access type (in which case
234 -- N denotes the actual parameter and N = Operand).
236 -------------------------
237 -- Ambiguous_Character --
238 -------------------------
240 procedure Ambiguous_Character (C : Node_Id) is
244 if Nkind (C) = N_Character_Literal then
245 Error_Msg_N ("ambiguous character literal", C);
247 -- First the ones in Standard
250 ("\\possible interpretation: Character!", C);
252 ("\\possible interpretation: Wide_Character!", C);
254 -- Include Wide_Wide_Character in Ada 2005 mode
256 if Ada_Version >= Ada_05 then
258 ("\\possible interpretation: Wide_Wide_Character!", C);
261 -- Now any other types that match
263 E := Current_Entity (C);
264 while Present (E) loop
265 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
269 end Ambiguous_Character;
271 -------------------------
272 -- Analyze_And_Resolve --
273 -------------------------
275 procedure Analyze_And_Resolve (N : Node_Id) is
279 end Analyze_And_Resolve;
281 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
285 end Analyze_And_Resolve;
287 -- Version withs check(s) suppressed
289 procedure Analyze_And_Resolve
294 Scop : constant Entity_Id := Current_Scope;
297 if Suppress = All_Checks then
299 Svg : constant Suppress_Array := Scope_Suppress;
301 Scope_Suppress := (others => True);
302 Analyze_And_Resolve (N, Typ);
303 Scope_Suppress := Svg;
308 Svg : constant Boolean := Scope_Suppress (Suppress);
311 Scope_Suppress (Suppress) := True;
312 Analyze_And_Resolve (N, Typ);
313 Scope_Suppress (Suppress) := Svg;
317 if Current_Scope /= Scop
318 and then Scope_Is_Transient
320 -- This can only happen if a transient scope was created
321 -- for an inner expression, which will be removed upon
322 -- completion of the analysis of an enclosing construct.
323 -- The transient scope must have the suppress status of
324 -- the enclosing environment, not of this Analyze call.
326 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
329 end Analyze_And_Resolve;
331 procedure Analyze_And_Resolve
335 Scop : constant Entity_Id := Current_Scope;
338 if Suppress = All_Checks then
340 Svg : constant Suppress_Array := Scope_Suppress;
342 Scope_Suppress := (others => True);
343 Analyze_And_Resolve (N);
344 Scope_Suppress := Svg;
349 Svg : constant Boolean := Scope_Suppress (Suppress);
352 Scope_Suppress (Suppress) := True;
353 Analyze_And_Resolve (N);
354 Scope_Suppress (Suppress) := Svg;
358 if Current_Scope /= Scop
359 and then Scope_Is_Transient
361 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
364 end Analyze_And_Resolve;
366 ----------------------------
367 -- Check_Discriminant_Use --
368 ----------------------------
370 procedure Check_Discriminant_Use (N : Node_Id) is
371 PN : constant Node_Id := Parent (N);
372 Disc : constant Entity_Id := Entity (N);
377 -- Any use in a default expression is legal
379 if In_Default_Expression then
382 elsif Nkind (PN) = N_Range then
384 -- Discriminant cannot be used to constrain a scalar type
388 if Nkind (P) = N_Range_Constraint
389 and then Nkind (Parent (P)) = N_Subtype_Indication
390 and then Nkind (Parent (Parent (P))) = N_Component_Definition
392 Error_Msg_N ("discriminant cannot constrain scalar type", N);
394 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
396 -- The following check catches the unusual case where
397 -- a discriminant appears within an index constraint
398 -- that is part of a larger expression within a constraint
399 -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
400 -- For now we only check case of record components, and
401 -- note that a similar check should also apply in the
402 -- case of discriminant constraints below. ???
404 -- Note that the check for N_Subtype_Declaration below is to
405 -- detect the valid use of discriminants in the constraints of a
406 -- subtype declaration when this subtype declaration appears
407 -- inside the scope of a record type (which is syntactically
408 -- illegal, but which may be created as part of derived type
409 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
412 if Ekind (Current_Scope) = E_Record_Type
413 and then Scope (Disc) = Current_Scope
415 (Nkind (Parent (P)) = N_Subtype_Indication
417 (Nkind (Parent (Parent (P))) = N_Component_Definition
419 Nkind (Parent (Parent (P))) = N_Subtype_Declaration)
420 and then Paren_Count (N) = 0)
423 ("discriminant must appear alone in component constraint", N);
427 -- Detect a common beginner error:
429 -- type R (D : Positive := 100) is record
430 -- Name : String (1 .. D);
433 -- The default value causes an object of type R to be
434 -- allocated with room for Positive'Last characters.
442 function Large_Storage_Type (T : Entity_Id) return Boolean;
443 -- Return True if type T has a large enough range that
444 -- any array whose index type covered the whole range of
445 -- the type would likely raise Storage_Error.
447 ------------------------
448 -- Large_Storage_Type --
449 ------------------------
451 function Large_Storage_Type (T : Entity_Id) return Boolean is
456 T = Standard_Positive
458 T = Standard_Natural;
459 end Large_Storage_Type;
462 -- Check that the Disc has a large range
464 if not Large_Storage_Type (Etype (Disc)) then
468 -- If the enclosing type is limited, we allocate only the
469 -- default value, not the maximum, and there is no need for
472 if Is_Limited_Type (Scope (Disc)) then
476 -- Check that it is the high bound
478 if N /= High_Bound (PN)
479 or else No (Discriminant_Default_Value (Disc))
484 -- Check the array allows a large range at this bound.
485 -- First find the array
489 if Nkind (SI) /= N_Subtype_Indication then
493 T := Entity (Subtype_Mark (SI));
495 if not Is_Array_Type (T) then
499 -- Next, find the dimension
501 TB := First_Index (T);
502 CB := First (Constraints (P));
504 and then Present (TB)
505 and then Present (CB)
516 -- Now, check the dimension has a large range
518 if not Large_Storage_Type (Etype (TB)) then
522 -- Warn about the danger
525 ("?creation of & object may raise Storage_Error!",
534 -- Legal case is in index or discriminant constraint
536 elsif Nkind (PN) = N_Index_Or_Discriminant_Constraint
537 or else Nkind (PN) = N_Discriminant_Association
539 if Paren_Count (N) > 0 then
541 ("discriminant in constraint must appear alone", N);
543 elsif Nkind (N) = N_Expanded_Name
544 and then Comes_From_Source (N)
547 ("discriminant must appear alone as a direct name", N);
552 -- Otherwise, context is an expression. It should not be within
553 -- (i.e. a subexpression of) a constraint for a component.
558 while Nkind (P) /= N_Component_Declaration
559 and then Nkind (P) /= N_Subtype_Indication
560 and then Nkind (P) /= N_Entry_Declaration
567 -- If the discriminant is used in an expression that is a bound
568 -- of a scalar type, an Itype is created and the bounds are attached
569 -- to its range, not to the original subtype indication. Such use
570 -- is of course a double fault.
572 if (Nkind (P) = N_Subtype_Indication
574 (Nkind (Parent (P)) = N_Component_Definition
576 Nkind (Parent (P)) = N_Derived_Type_Definition)
577 and then D = Constraint (P))
579 -- The constraint itself may be given by a subtype indication,
580 -- rather than by a more common discrete range.
582 or else (Nkind (P) = N_Subtype_Indication
584 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
585 or else Nkind (P) = N_Entry_Declaration
586 or else Nkind (D) = N_Defining_Identifier
589 ("discriminant in constraint must appear alone", N);
592 end Check_Discriminant_Use;
594 --------------------------------
595 -- Check_For_Visible_Operator --
596 --------------------------------
598 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
600 if Is_Invisible_Operator (N, T) then
602 ("operator for} is not directly visible!", N, First_Subtype (T));
603 Error_Msg_N ("use clause would make operation legal!", N);
605 end Check_For_Visible_Operator;
607 ----------------------------------
608 -- Check_Fully_Declared_Prefix --
609 ----------------------------------
611 procedure Check_Fully_Declared_Prefix
616 -- Check that the designated type of the prefix of a dereference is
617 -- not an incomplete type. This cannot be done unconditionally, because
618 -- dereferences of private types are legal in default expressions. This
619 -- case is taken care of in Check_Fully_Declared, called below. There
620 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
622 -- This consideration also applies to similar checks for allocators,
623 -- qualified expressions, and type conversions.
625 -- An additional exception concerns other per-object expressions that
626 -- are not directly related to component declarations, in particular
627 -- representation pragmas for tasks. These will be per-object
628 -- expressions if they depend on discriminants or some global entity.
629 -- If the task has access discriminants, the designated type may be
630 -- incomplete at the point the expression is resolved. This resolution
631 -- takes place within the body of the initialization procedure, where
632 -- the discriminant is replaced by its discriminal.
634 if Is_Entity_Name (Pref)
635 and then Ekind (Entity (Pref)) = E_In_Parameter
639 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
640 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
641 -- Analyze_Object_Renaming, and Freeze_Entity.
643 elsif Ada_Version >= Ada_05
644 and then Is_Entity_Name (Pref)
645 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
647 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
651 Check_Fully_Declared (Typ, Parent (Pref));
653 end Check_Fully_Declared_Prefix;
655 ------------------------------
656 -- Check_Infinite_Recursion --
657 ------------------------------
659 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
663 function Same_Argument_List return Boolean;
664 -- Check whether list of actuals is identical to list of formals
665 -- of called function (which is also the enclosing scope).
667 ------------------------
668 -- Same_Argument_List --
669 ------------------------
671 function Same_Argument_List return Boolean is
677 if not Is_Entity_Name (Name (N)) then
680 Subp := Entity (Name (N));
683 F := First_Formal (Subp);
684 A := First_Actual (N);
685 while Present (F) and then Present (A) loop
686 if not Is_Entity_Name (A)
687 or else Entity (A) /= F
697 end Same_Argument_List;
699 -- Start of processing for Check_Infinite_Recursion
702 -- Loop moving up tree, quitting if something tells us we are
703 -- definitely not in an infinite recursion situation.
708 exit when Nkind (P) = N_Subprogram_Body;
710 if Nkind (P) = N_Or_Else or else
711 Nkind (P) = N_And_Then or else
712 Nkind (P) = N_If_Statement or else
713 Nkind (P) = N_Case_Statement
717 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
718 and then C /= First (Statements (P))
720 -- If the call is the expression of a return statement and
721 -- the actuals are identical to the formals, it's worth a
722 -- warning. However, we skip this if there is an immediately
723 -- preceding raise statement, since the call is never executed.
725 -- Furthermore, this corresponds to a common idiom:
727 -- function F (L : Thing) return Boolean is
729 -- raise Program_Error;
733 -- for generating a stub function
735 if Nkind (Parent (N)) = N_Simple_Return_Statement
736 and then Same_Argument_List
738 exit when not Is_List_Member (Parent (N));
740 -- OK, return statement is in a statement list, look for raise
746 -- Skip past N_Freeze_Entity nodes generated by expansion
748 Nod := Prev (Parent (N));
750 and then Nkind (Nod) = N_Freeze_Entity
755 -- If no raise statement, give warning
757 exit when Nkind (Nod) /= N_Raise_Statement
759 (Nkind (Nod) not in N_Raise_xxx_Error
760 or else Present (Condition (Nod)));
771 Error_Msg_N ("!?possible infinite recursion", N);
772 Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
775 end Check_Infinite_Recursion;
777 -------------------------------
778 -- Check_Initialization_Call --
779 -------------------------------
781 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
782 Typ : constant Entity_Id := Etype (First_Formal (Nam));
784 function Uses_SS (T : Entity_Id) return Boolean;
785 -- Check whether the creation of an object of the type will involve
786 -- use of the secondary stack. If T is a record type, this is true
787 -- if the expression for some component uses the secondary stack, eg.
788 -- through a call to a function that returns an unconstrained value.
789 -- False if T is controlled, because cleanups occur elsewhere.
795 function Uses_SS (T : Entity_Id) return Boolean is
798 Full_Type : Entity_Id := Underlying_Type (T);
801 -- Normally we want to use the underlying type, but if it's not set
802 -- then continue with T.
804 if not Present (Full_Type) then
808 if Is_Controlled (Full_Type) then
811 elsif Is_Array_Type (Full_Type) then
812 return Uses_SS (Component_Type (Full_Type));
814 elsif Is_Record_Type (Full_Type) then
815 Comp := First_Component (Full_Type);
816 while Present (Comp) loop
817 if Ekind (Comp) = E_Component
818 and then Nkind (Parent (Comp)) = N_Component_Declaration
820 -- The expression for a dynamic component may be rewritten
821 -- as a dereference, so retrieve original node.
823 Expr := Original_Node (Expression (Parent (Comp)));
825 -- Return True if the expression is a call to a function
826 -- (including an attribute function such as Image) with
827 -- a result that requires a transient scope.
829 if (Nkind (Expr) = N_Function_Call
830 or else (Nkind (Expr) = N_Attribute_Reference
831 and then Present (Expressions (Expr))))
832 and then Requires_Transient_Scope (Etype (Expr))
836 elsif Uses_SS (Etype (Comp)) then
841 Next_Component (Comp);
851 -- Start of processing for Check_Initialization_Call
854 -- Establish a transient scope if the type needs it
856 if Uses_SS (Typ) then
857 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
859 end Check_Initialization_Call;
861 ------------------------------
862 -- Check_Parameterless_Call --
863 ------------------------------
865 procedure Check_Parameterless_Call (N : Node_Id) is
868 function Prefix_Is_Access_Subp return Boolean;
869 -- If the prefix is of an access_to_subprogram type, the node must be
870 -- rewritten as a call. Ditto if the prefix is overloaded and all its
871 -- interpretations are access to subprograms.
873 ---------------------------
874 -- Prefix_Is_Access_Subp --
875 ---------------------------
877 function Prefix_Is_Access_Subp return Boolean is
882 if not Is_Overloaded (N) then
884 Ekind (Etype (N)) = E_Subprogram_Type
885 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
887 Get_First_Interp (N, I, It);
888 while Present (It.Typ) loop
889 if Ekind (It.Typ) /= E_Subprogram_Type
890 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
895 Get_Next_Interp (I, It);
900 end Prefix_Is_Access_Subp;
902 -- Start of processing for Check_Parameterless_Call
905 -- Defend against junk stuff if errors already detected
907 if Total_Errors_Detected /= 0 then
908 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
910 elsif Nkind (N) in N_Has_Chars
911 and then Chars (N) in Error_Name_Or_No_Name
919 -- If the context expects a value, and the name is a procedure,
920 -- this is most likely a missing 'Access. Do not try to resolve
921 -- the parameterless call, error will be caught when the outer
924 if Is_Entity_Name (N)
925 and then Ekind (Entity (N)) = E_Procedure
926 and then not Is_Overloaded (N)
928 (Nkind (Parent (N)) = N_Parameter_Association
929 or else Nkind (Parent (N)) = N_Function_Call
930 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
935 -- Rewrite as call if overloadable entity that is (or could be, in
936 -- the overloaded case) a function call. If we know for sure that
937 -- the entity is an enumeration literal, we do not rewrite it.
939 if (Is_Entity_Name (N)
940 and then Is_Overloadable (Entity (N))
941 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
942 or else Is_Overloaded (N)))
944 -- Rewrite as call if it is an explicit deference of an expression of
945 -- a subprogram access type, and the suprogram type is not that of a
946 -- procedure or entry.
949 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
951 -- Rewrite as call if it is a selected component which is a function,
952 -- this is the case of a call to a protected function (which may be
953 -- overloaded with other protected operations).
956 (Nkind (N) = N_Selected_Component
957 and then (Ekind (Entity (Selector_Name (N))) = E_Function
959 ((Ekind (Entity (Selector_Name (N))) = E_Entry
961 Ekind (Entity (Selector_Name (N))) = E_Procedure)
962 and then Is_Overloaded (Selector_Name (N)))))
964 -- If one of the above three conditions is met, rewrite as call.
965 -- Apply the rewriting only once.
968 if Nkind (Parent (N)) /= N_Function_Call
969 or else N /= Name (Parent (N))
973 -- If overloaded, overload set belongs to new copy
975 Save_Interps (N, Nam);
977 -- Change node to parameterless function call (note that the
978 -- Parameter_Associations associations field is left set to Empty,
979 -- its normal default value since there are no parameters)
981 Change_Node (N, N_Function_Call);
983 Set_Sloc (N, Sloc (Nam));
987 elsif Nkind (N) = N_Parameter_Association then
988 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
990 end Check_Parameterless_Call;
992 ----------------------
993 -- Is_Predefined_Op --
994 ----------------------
996 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
998 return Is_Intrinsic_Subprogram (Nam)
999 and then not Is_Generic_Instance (Nam)
1000 and then Chars (Nam) in Any_Operator_Name
1001 and then (No (Alias (Nam))
1002 or else Is_Predefined_Op (Alias (Nam)));
1003 end Is_Predefined_Op;
1005 -----------------------------
1006 -- Make_Call_Into_Operator --
1007 -----------------------------
1009 procedure Make_Call_Into_Operator
1014 Op_Name : constant Name_Id := Chars (Op_Id);
1015 Act1 : Node_Id := First_Actual (N);
1016 Act2 : Node_Id := Next_Actual (Act1);
1017 Error : Boolean := False;
1018 Func : constant Entity_Id := Entity (Name (N));
1019 Is_Binary : constant Boolean := Present (Act2);
1021 Opnd_Type : Entity_Id;
1022 Orig_Type : Entity_Id := Empty;
1025 type Kind_Test is access function (E : Entity_Id) return Boolean;
1027 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
1028 -- Determine whether E is an access type declared by an access decla-
1029 -- ration, and not an (anonymous) allocator type.
1031 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1032 -- If the operand is not universal, and the operator is given by a
1033 -- expanded name, verify that the operand has an interpretation with
1034 -- a type defined in the given scope of the operator.
1036 function Type_In_P (Test : Kind_Test) return Entity_Id;
1037 -- Find a type of the given class in the package Pack that contains
1040 -----------------------------
1041 -- Is_Definite_Access_Type --
1042 -----------------------------
1044 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1045 Btyp : constant Entity_Id := Base_Type (E);
1047 return Ekind (Btyp) = E_Access_Type
1048 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1049 and then Comes_From_Source (Btyp));
1050 end Is_Definite_Access_Type;
1052 ---------------------------
1053 -- Operand_Type_In_Scope --
1054 ---------------------------
1056 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1057 Nod : constant Node_Id := Right_Opnd (Op_Node);
1062 if not Is_Overloaded (Nod) then
1063 return Scope (Base_Type (Etype (Nod))) = S;
1066 Get_First_Interp (Nod, I, It);
1067 while Present (It.Typ) loop
1068 if Scope (Base_Type (It.Typ)) = S then
1072 Get_Next_Interp (I, It);
1077 end Operand_Type_In_Scope;
1083 function Type_In_P (Test : Kind_Test) return Entity_Id is
1086 function In_Decl return Boolean;
1087 -- Verify that node is not part of the type declaration for the
1088 -- candidate type, which would otherwise be invisible.
1094 function In_Decl return Boolean is
1095 Decl_Node : constant Node_Id := Parent (E);
1101 if Etype (E) = Any_Type then
1104 elsif No (Decl_Node) then
1109 and then Nkind (N2) /= N_Compilation_Unit
1111 if N2 = Decl_Node then
1122 -- Start of processing for Type_In_P
1125 -- If the context type is declared in the prefix package, this
1126 -- is the desired base type.
1128 if Scope (Base_Type (Typ)) = Pack
1131 return Base_Type (Typ);
1134 E := First_Entity (Pack);
1135 while Present (E) loop
1137 and then not In_Decl
1149 -- Start of processing for Make_Call_Into_Operator
1152 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1157 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1158 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1159 Save_Interps (Act1, Left_Opnd (Op_Node));
1160 Save_Interps (Act2, Right_Opnd (Op_Node));
1161 Act1 := Left_Opnd (Op_Node);
1162 Act2 := Right_Opnd (Op_Node);
1167 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1168 Save_Interps (Act1, Right_Opnd (Op_Node));
1169 Act1 := Right_Opnd (Op_Node);
1172 -- If the operator is denoted by an expanded name, and the prefix is
1173 -- not Standard, but the operator is a predefined one whose scope is
1174 -- Standard, then this is an implicit_operator, inserted as an
1175 -- interpretation by the procedure of the same name. This procedure
1176 -- overestimates the presence of implicit operators, because it does
1177 -- not examine the type of the operands. Verify now that the operand
1178 -- type appears in the given scope. If right operand is universal,
1179 -- check the other operand. In the case of concatenation, either
1180 -- argument can be the component type, so check the type of the result.
1181 -- If both arguments are literals, look for a type of the right kind
1182 -- defined in the given scope. This elaborate nonsense is brought to
1183 -- you courtesy of b33302a. The type itself must be frozen, so we must
1184 -- find the type of the proper class in the given scope.
1186 -- A final wrinkle is the multiplication operator for fixed point
1187 -- types, which is defined in Standard only, and not in the scope of
1188 -- the fixed_point type itself.
1190 if Nkind (Name (N)) = N_Expanded_Name then
1191 Pack := Entity (Prefix (Name (N)));
1193 -- If the entity being called is defined in the given package,
1194 -- it is a renaming of a predefined operator, and known to be
1197 if Scope (Entity (Name (N))) = Pack
1198 and then Pack /= Standard_Standard
1202 -- Visibility does not need to be checked in an instance: if the
1203 -- operator was not visible in the generic it has been diagnosed
1204 -- already, else there is an implicit copy of it in the instance.
1206 elsif In_Instance then
1209 elsif (Op_Name = Name_Op_Multiply
1210 or else Op_Name = Name_Op_Divide)
1211 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1212 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1214 if Pack /= Standard_Standard then
1218 -- Ada 2005, AI-420: Predefined equality on Universal_Access
1221 elsif Ada_Version >= Ada_05
1222 and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1223 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1228 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1230 if Op_Name = Name_Op_Concat then
1231 Opnd_Type := Base_Type (Typ);
1233 elsif (Scope (Opnd_Type) = Standard_Standard
1235 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1237 and then not Comes_From_Source (Opnd_Type))
1239 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1242 if Scope (Opnd_Type) = Standard_Standard then
1244 -- Verify that the scope contains a type that corresponds to
1245 -- the given literal. Optimize the case where Pack is Standard.
1247 if Pack /= Standard_Standard then
1249 if Opnd_Type = Universal_Integer then
1250 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1252 elsif Opnd_Type = Universal_Real then
1253 Orig_Type := Type_In_P (Is_Real_Type'Access);
1255 elsif Opnd_Type = Any_String then
1256 Orig_Type := Type_In_P (Is_String_Type'Access);
1258 elsif Opnd_Type = Any_Access then
1259 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1261 elsif Opnd_Type = Any_Composite then
1262 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1264 if Present (Orig_Type) then
1265 if Has_Private_Component (Orig_Type) then
1268 Set_Etype (Act1, Orig_Type);
1271 Set_Etype (Act2, Orig_Type);
1280 Error := No (Orig_Type);
1283 elsif Ekind (Opnd_Type) = E_Allocator_Type
1284 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1288 -- If the type is defined elsewhere, and the operator is not
1289 -- defined in the given scope (by a renaming declaration, e.g.)
1290 -- then this is an error as well. If an extension of System is
1291 -- present, and the type may be defined there, Pack must be
1294 elsif Scope (Opnd_Type) /= Pack
1295 and then Scope (Op_Id) /= Pack
1296 and then (No (System_Aux_Id)
1297 or else Scope (Opnd_Type) /= System_Aux_Id
1298 or else Pack /= Scope (System_Aux_Id))
1300 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1303 Error := not Operand_Type_In_Scope (Pack);
1306 elsif Pack = Standard_Standard
1307 and then not Operand_Type_In_Scope (Standard_Standard)
1314 Error_Msg_Node_2 := Pack;
1316 ("& not declared in&", N, Selector_Name (Name (N)));
1317 Set_Etype (N, Any_Type);
1322 Set_Chars (Op_Node, Op_Name);
1324 if not Is_Private_Type (Etype (N)) then
1325 Set_Etype (Op_Node, Base_Type (Etype (N)));
1327 Set_Etype (Op_Node, Etype (N));
1330 -- If this is a call to a function that renames a predefined equality,
1331 -- the renaming declaration provides a type that must be used to
1332 -- resolve the operands. This must be done now because resolution of
1333 -- the equality node will not resolve any remaining ambiguity, and it
1334 -- assumes that the first operand is not overloaded.
1336 if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1337 and then Ekind (Func) = E_Function
1338 and then Is_Overloaded (Act1)
1340 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1341 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1344 Set_Entity (Op_Node, Op_Id);
1345 Generate_Reference (Op_Id, N, ' ');
1346 Rewrite (N, Op_Node);
1348 -- If this is an arithmetic operator and the result type is private,
1349 -- the operands and the result must be wrapped in conversion to
1350 -- expose the underlying numeric type and expand the proper checks,
1351 -- e.g. on division.
1353 if Is_Private_Type (Typ) then
1355 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1356 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1357 Resolve_Intrinsic_Operator (N, Typ);
1359 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1360 Resolve_Intrinsic_Unary_Operator (N, Typ);
1369 -- For predefined operators on literals, the operation freezes
1372 if Present (Orig_Type) then
1373 Set_Etype (Act1, Orig_Type);
1374 Freeze_Expression (Act1);
1376 end Make_Call_Into_Operator;
1382 function Operator_Kind
1384 Is_Binary : Boolean) return Node_Kind
1390 if Op_Name = Name_Op_And then
1392 elsif Op_Name = Name_Op_Or then
1394 elsif Op_Name = Name_Op_Xor then
1396 elsif Op_Name = Name_Op_Eq then
1398 elsif Op_Name = Name_Op_Ne then
1400 elsif Op_Name = Name_Op_Lt then
1402 elsif Op_Name = Name_Op_Le then
1404 elsif Op_Name = Name_Op_Gt then
1406 elsif Op_Name = Name_Op_Ge then
1408 elsif Op_Name = Name_Op_Add then
1410 elsif Op_Name = Name_Op_Subtract then
1411 Kind := N_Op_Subtract;
1412 elsif Op_Name = Name_Op_Concat then
1413 Kind := N_Op_Concat;
1414 elsif Op_Name = Name_Op_Multiply then
1415 Kind := N_Op_Multiply;
1416 elsif Op_Name = Name_Op_Divide then
1417 Kind := N_Op_Divide;
1418 elsif Op_Name = Name_Op_Mod then
1420 elsif Op_Name = Name_Op_Rem then
1422 elsif Op_Name = Name_Op_Expon then
1425 raise Program_Error;
1431 if Op_Name = Name_Op_Add then
1433 elsif Op_Name = Name_Op_Subtract then
1435 elsif Op_Name = Name_Op_Abs then
1437 elsif Op_Name = Name_Op_Not then
1440 raise Program_Error;
1447 -----------------------------
1448 -- Pre_Analyze_And_Resolve --
1449 -----------------------------
1451 procedure Pre_Analyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1452 Save_Full_Analysis : constant Boolean := Full_Analysis;
1455 Full_Analysis := False;
1456 Expander_Mode_Save_And_Set (False);
1458 -- We suppress all checks for this analysis, since the checks will
1459 -- be applied properly, and in the right location, when the default
1460 -- expression is reanalyzed and reexpanded later on.
1462 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1464 Expander_Mode_Restore;
1465 Full_Analysis := Save_Full_Analysis;
1466 end Pre_Analyze_And_Resolve;
1468 -- Version without context type
1470 procedure Pre_Analyze_And_Resolve (N : Node_Id) is
1471 Save_Full_Analysis : constant Boolean := Full_Analysis;
1474 Full_Analysis := False;
1475 Expander_Mode_Save_And_Set (False);
1478 Resolve (N, Etype (N), Suppress => All_Checks);
1480 Expander_Mode_Restore;
1481 Full_Analysis := Save_Full_Analysis;
1482 end Pre_Analyze_And_Resolve;
1484 ----------------------------------
1485 -- Replace_Actual_Discriminants --
1486 ----------------------------------
1488 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1489 Loc : constant Source_Ptr := Sloc (N);
1490 Tsk : Node_Id := Empty;
1492 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1498 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1502 if Nkind (Nod) = N_Identifier then
1503 Ent := Entity (Nod);
1506 and then Ekind (Ent) = E_Discriminant
1509 Make_Selected_Component (Loc,
1510 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1511 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1513 Set_Etype (Nod, Etype (Ent));
1521 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1523 -- Start of processing for Replace_Actual_Discriminants
1526 if not Expander_Active then
1530 if Nkind (Name (N)) = N_Selected_Component then
1531 Tsk := Prefix (Name (N));
1533 elsif Nkind (Name (N)) = N_Indexed_Component then
1534 Tsk := Prefix (Prefix (Name (N)));
1540 Replace_Discrs (Default);
1542 end Replace_Actual_Discriminants;
1548 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1549 Ambiguous : Boolean := False;
1550 Ctx_Type : Entity_Id := Typ;
1551 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1552 Err_Type : Entity_Id := Empty;
1553 Found : Boolean := False;
1556 I1 : Interp_Index := 0; -- prevent junk warning
1559 Seen : Entity_Id := Empty; -- prevent junk warning
1561 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1562 -- Determine whether a node comes from a predefined library unit or
1565 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1566 -- Try and fix up a literal so that it matches its expected type. New
1567 -- literals are manufactured if necessary to avoid cascaded errors.
1569 procedure Resolution_Failed;
1570 -- Called when attempt at resolving current expression fails
1572 ------------------------------------
1573 -- Comes_From_Predefined_Lib_Unit --
1574 -------------------------------------
1576 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1579 Sloc (Nod) = Standard_Location
1580 or else Is_Predefined_File_Name (Unit_File_Name (
1581 Get_Source_Unit (Sloc (Nod))));
1582 end Comes_From_Predefined_Lib_Unit;
1584 --------------------
1585 -- Patch_Up_Value --
1586 --------------------
1588 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1590 if Nkind (N) = N_Integer_Literal
1591 and then Is_Real_Type (Typ)
1594 Make_Real_Literal (Sloc (N),
1595 Realval => UR_From_Uint (Intval (N))));
1596 Set_Etype (N, Universal_Real);
1597 Set_Is_Static_Expression (N);
1599 elsif Nkind (N) = N_Real_Literal
1600 and then Is_Integer_Type (Typ)
1603 Make_Integer_Literal (Sloc (N),
1604 Intval => UR_To_Uint (Realval (N))));
1605 Set_Etype (N, Universal_Integer);
1606 Set_Is_Static_Expression (N);
1607 elsif Nkind (N) = N_String_Literal
1608 and then Is_Character_Type (Typ)
1610 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1612 Make_Character_Literal (Sloc (N),
1614 Char_Literal_Value =>
1615 UI_From_Int (Character'Pos ('A'))));
1616 Set_Etype (N, Any_Character);
1617 Set_Is_Static_Expression (N);
1619 elsif Nkind (N) /= N_String_Literal
1620 and then Is_String_Type (Typ)
1623 Make_String_Literal (Sloc (N),
1624 Strval => End_String));
1626 elsif Nkind (N) = N_Range then
1627 Patch_Up_Value (Low_Bound (N), Typ);
1628 Patch_Up_Value (High_Bound (N), Typ);
1632 -----------------------
1633 -- Resolution_Failed --
1634 -----------------------
1636 procedure Resolution_Failed is
1638 Patch_Up_Value (N, Typ);
1640 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1641 Set_Is_Overloaded (N, False);
1643 -- The caller will return without calling the expander, so we need
1644 -- to set the analyzed flag. Note that it is fine to set Analyzed
1645 -- to True even if we are in the middle of a shallow analysis,
1646 -- (see the spec of sem for more details) since this is an error
1647 -- situation anyway, and there is no point in repeating the
1648 -- analysis later (indeed it won't work to repeat it later, since
1649 -- we haven't got a clear resolution of which entity is being
1652 Set_Analyzed (N, True);
1654 end Resolution_Failed;
1656 -- Start of processing for Resolve
1663 -- Access attribute on remote subprogram cannot be used for
1664 -- a non-remote access-to-subprogram type.
1666 if Nkind (N) = N_Attribute_Reference
1667 and then (Attribute_Name (N) = Name_Access
1668 or else Attribute_Name (N) = Name_Unrestricted_Access
1669 or else Attribute_Name (N) = Name_Unchecked_Access)
1670 and then Comes_From_Source (N)
1671 and then Is_Entity_Name (Prefix (N))
1672 and then Is_Subprogram (Entity (Prefix (N)))
1673 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1674 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1677 ("prefix must statically denote a non-remote subprogram", N);
1680 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1682 -- If the context is a Remote_Access_To_Subprogram, access attributes
1683 -- must be resolved with the corresponding fat pointer. There is no need
1684 -- to check for the attribute name since the return type of an
1685 -- attribute is never a remote type.
1687 if Nkind (N) = N_Attribute_Reference
1688 and then Comes_From_Source (N)
1689 and then (Is_Remote_Call_Interface (Typ)
1690 or else Is_Remote_Types (Typ))
1693 Attr : constant Attribute_Id :=
1694 Get_Attribute_Id (Attribute_Name (N));
1695 Pref : constant Node_Id := Prefix (N);
1698 Is_Remote : Boolean := True;
1701 -- Check that Typ is a remote access-to-subprogram type
1703 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1704 -- Prefix (N) must statically denote a remote subprogram
1705 -- declared in a package specification.
1707 if Attr = Attribute_Access then
1708 Decl := Unit_Declaration_Node (Entity (Pref));
1710 if Nkind (Decl) = N_Subprogram_Body then
1711 Spec := Corresponding_Spec (Decl);
1713 if not No (Spec) then
1714 Decl := Unit_Declaration_Node (Spec);
1718 Spec := Parent (Decl);
1720 if not Is_Entity_Name (Prefix (N))
1721 or else Nkind (Spec) /= N_Package_Specification
1723 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1727 ("prefix must statically denote a remote subprogram ",
1732 -- If we are generating code for a distributed program.
1733 -- perform semantic checks against the corresponding
1736 if (Attr = Attribute_Access
1737 or else Attr = Attribute_Unchecked_Access
1738 or else Attr = Attribute_Unrestricted_Access)
1739 and then Expander_Active
1740 and then Get_PCS_Name /= Name_No_DSA
1742 Check_Subtype_Conformant
1743 (New_Id => Entity (Prefix (N)),
1744 Old_Id => Designated_Type
1745 (Corresponding_Remote_Type (Typ)),
1749 Process_Remote_AST_Attribute (N, Typ);
1756 Debug_A_Entry ("resolving ", N);
1758 if Comes_From_Source (N) then
1759 if Is_Fixed_Point_Type (Typ) then
1760 Check_Restriction (No_Fixed_Point, N);
1762 elsif Is_Floating_Point_Type (Typ)
1763 and then Typ /= Universal_Real
1764 and then Typ /= Any_Real
1766 Check_Restriction (No_Floating_Point, N);
1770 -- Return if already analyzed
1772 if Analyzed (N) then
1773 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
1776 -- Return if type = Any_Type (previous error encountered)
1778 elsif Etype (N) = Any_Type then
1779 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
1783 Check_Parameterless_Call (N);
1785 -- If not overloaded, then we know the type, and all that needs doing
1786 -- is to check that this type is compatible with the context.
1788 if not Is_Overloaded (N) then
1789 Found := Covers (Typ, Etype (N));
1790 Expr_Type := Etype (N);
1792 -- In the overloaded case, we must select the interpretation that
1793 -- is compatible with the context (i.e. the type passed to Resolve)
1796 -- Loop through possible interpretations
1798 Get_First_Interp (N, I, It);
1799 Interp_Loop : while Present (It.Typ) loop
1801 -- We are only interested in interpretations that are compatible
1802 -- with the expected type, any other interpretations are ignored.
1804 if not Covers (Typ, It.Typ) then
1805 if Debug_Flag_V then
1806 Write_Str (" interpretation incompatible with context");
1811 -- Skip the current interpretation if it is disabled by an
1812 -- abstract operator. This action is performed only when the
1813 -- type against which we are resolving is the same as the
1814 -- type of the interpretation.
1816 if Ada_Version >= Ada_05
1817 and then It.Typ = Typ
1818 and then Typ /= Universal_Integer
1819 and then Typ /= Universal_Real
1820 and then Present (It.Abstract_Op)
1825 -- First matching interpretation
1831 Expr_Type := It.Typ;
1833 -- Matching interpretation that is not the first, maybe an
1834 -- error, but there are some cases where preference rules are
1835 -- used to choose between the two possibilities. These and
1836 -- some more obscure cases are handled in Disambiguate.
1839 -- If the current statement is part of a predefined library
1840 -- unit, then all interpretations which come from user level
1841 -- packages should not be considered.
1844 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
1849 Error_Msg_Sloc := Sloc (Seen);
1850 It1 := Disambiguate (N, I1, I, Typ);
1852 -- Disambiguation has succeeded. Skip the remaining
1855 if It1 /= No_Interp then
1857 Expr_Type := It1.Typ;
1859 while Present (It.Typ) loop
1860 Get_Next_Interp (I, It);
1864 -- Before we issue an ambiguity complaint, check for
1865 -- the case of a subprogram call where at least one
1866 -- of the arguments is Any_Type, and if so, suppress
1867 -- the message, since it is a cascaded error.
1869 if Nkind (N) = N_Function_Call
1870 or else Nkind (N) = N_Procedure_Call_Statement
1877 A := First_Actual (N);
1878 while Present (A) loop
1881 if Nkind (E) = N_Parameter_Association then
1882 E := Explicit_Actual_Parameter (E);
1885 if Etype (E) = Any_Type then
1886 if Debug_Flag_V then
1887 Write_Str ("Any_Type in call");
1898 elsif Nkind (N) in N_Binary_Op
1899 and then (Etype (Left_Opnd (N)) = Any_Type
1900 or else Etype (Right_Opnd (N)) = Any_Type)
1904 elsif Nkind (N) in N_Unary_Op
1905 and then Etype (Right_Opnd (N)) = Any_Type
1910 -- Not that special case, so issue message using the
1911 -- flag Ambiguous to control printing of the header
1912 -- message only at the start of an ambiguous set.
1914 if not Ambiguous then
1915 if Nkind (N) = N_Function_Call
1916 and then Nkind (Name (N)) = N_Explicit_Dereference
1919 ("ambiguous expression "
1920 & "(cannot resolve indirect call)!", N);
1923 ("ambiguous expression (cannot resolve&)!",
1929 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
1931 ("\\possible interpretation (inherited)#!", N);
1933 Error_Msg_N ("\\possible interpretation#!", N);
1937 Error_Msg_Sloc := Sloc (It.Nam);
1939 -- By default, the error message refers to the candidate
1940 -- interpretation. But if it is a predefined operator, it
1941 -- is implicitly declared at the declaration of the type
1942 -- of the operand. Recover the sloc of that declaration
1943 -- for the error message.
1945 if Nkind (N) in N_Op
1946 and then Scope (It.Nam) = Standard_Standard
1947 and then not Is_Overloaded (Right_Opnd (N))
1948 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
1951 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
1953 if Comes_From_Source (Err_Type)
1954 and then Present (Parent (Err_Type))
1956 Error_Msg_Sloc := Sloc (Parent (Err_Type));
1959 elsif Nkind (N) in N_Binary_Op
1960 and then Scope (It.Nam) = Standard_Standard
1961 and then not Is_Overloaded (Left_Opnd (N))
1962 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
1965 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
1967 if Comes_From_Source (Err_Type)
1968 and then Present (Parent (Err_Type))
1970 Error_Msg_Sloc := Sloc (Parent (Err_Type));
1973 -- If this is an indirect call, use the subprogram_type
1974 -- in the message, to have a meaningful location.
1975 -- Indicate as well if this is an inherited operation,
1976 -- created by a type declaration.
1978 elsif Nkind (N) = N_Function_Call
1979 and then Nkind (Name (N)) = N_Explicit_Dereference
1980 and then Is_Type (It.Nam)
1984 Sloc (Associated_Node_For_Itype (Err_Type));
1989 if Nkind (N) in N_Op
1990 and then Scope (It.Nam) = Standard_Standard
1991 and then Present (Err_Type)
1993 -- Special-case the message for universal_fixed
1994 -- operators, which are not declared with the type
1995 -- of the operand, but appear forever in Standard.
1997 if It.Typ = Universal_Fixed
1998 and then Scope (It.Nam) = Standard_Standard
2001 ("\\possible interpretation as " &
2002 "universal_fixed operation " &
2003 "(RM 4.5.5 (19))", N);
2006 ("\\possible interpretation (predefined)#!", N);
2010 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2013 ("\\possible interpretation (inherited)#!", N);
2015 Error_Msg_N ("\\possible interpretation#!", N);
2021 -- We have a matching interpretation, Expr_Type is the type
2022 -- from this interpretation, and Seen is the entity.
2024 -- For an operator, just set the entity name. The type will be
2025 -- set by the specific operator resolution routine.
2027 if Nkind (N) in N_Op then
2028 Set_Entity (N, Seen);
2029 Generate_Reference (Seen, N);
2031 elsif Nkind (N) = N_Character_Literal then
2032 Set_Etype (N, Expr_Type);
2034 -- For an explicit dereference, attribute reference, range,
2035 -- short-circuit form (which is not an operator node), or call
2036 -- with a name that is an explicit dereference, there is
2037 -- nothing to be done at this point.
2039 elsif Nkind (N) = N_Explicit_Dereference
2040 or else Nkind (N) = N_Attribute_Reference
2041 or else Nkind (N) = N_And_Then
2042 or else Nkind (N) = N_Indexed_Component
2043 or else Nkind (N) = N_Or_Else
2044 or else Nkind (N) = N_Range
2045 or else Nkind (N) = N_Selected_Component
2046 or else Nkind (N) = N_Slice
2047 or else Nkind (Name (N)) = N_Explicit_Dereference
2051 -- For procedure or function calls, set the type of the name,
2052 -- and also the entity pointer for the prefix
2054 elsif (Nkind (N) = N_Procedure_Call_Statement
2055 or else Nkind (N) = N_Function_Call)
2056 and then (Is_Entity_Name (Name (N))
2057 or else Nkind (Name (N)) = N_Operator_Symbol)
2059 Set_Etype (Name (N), Expr_Type);
2060 Set_Entity (Name (N), Seen);
2061 Generate_Reference (Seen, Name (N));
2063 elsif Nkind (N) = N_Function_Call
2064 and then Nkind (Name (N)) = N_Selected_Component
2066 Set_Etype (Name (N), Expr_Type);
2067 Set_Entity (Selector_Name (Name (N)), Seen);
2068 Generate_Reference (Seen, Selector_Name (Name (N)));
2070 -- For all other cases, just set the type of the Name
2073 Set_Etype (Name (N), Expr_Type);
2080 -- Move to next interpretation
2082 exit Interp_Loop when No (It.Typ);
2084 Get_Next_Interp (I, It);
2085 end loop Interp_Loop;
2088 -- At this stage Found indicates whether or not an acceptable
2089 -- interpretation exists. If not, then we have an error, except
2090 -- that if the context is Any_Type as a result of some other error,
2091 -- then we suppress the error report.
2094 if Typ /= Any_Type then
2096 -- If type we are looking for is Void, then this is the procedure
2097 -- call case, and the error is simply that what we gave is not a
2098 -- procedure name (we think of procedure calls as expressions with
2099 -- types internally, but the user doesn't think of them this way!)
2101 if Typ = Standard_Void_Type then
2103 -- Special case message if function used as a procedure
2105 if Nkind (N) = N_Procedure_Call_Statement
2106 and then Is_Entity_Name (Name (N))
2107 and then Ekind (Entity (Name (N))) = E_Function
2110 ("cannot use function & in a procedure call",
2111 Name (N), Entity (Name (N)));
2113 -- Otherwise give general message (not clear what cases this
2114 -- covers, but no harm in providing for them!)
2117 Error_Msg_N ("expect procedure name in procedure call", N);
2122 -- Otherwise we do have a subexpression with the wrong type
2124 -- Check for the case of an allocator which uses an access type
2125 -- instead of the designated type. This is a common error and we
2126 -- specialize the message, posting an error on the operand of the
2127 -- allocator, complaining that we expected the designated type of
2130 elsif Nkind (N) = N_Allocator
2131 and then Ekind (Typ) in Access_Kind
2132 and then Ekind (Etype (N)) in Access_Kind
2133 and then Designated_Type (Etype (N)) = Typ
2135 Wrong_Type (Expression (N), Designated_Type (Typ));
2138 -- Check for view mismatch on Null in instances, for which the
2139 -- view-swapping mechanism has no identifier.
2141 elsif (In_Instance or else In_Inlined_Body)
2142 and then (Nkind (N) = N_Null)
2143 and then Is_Private_Type (Typ)
2144 and then Is_Access_Type (Full_View (Typ))
2146 Resolve (N, Full_View (Typ));
2150 -- Check for an aggregate. Sometimes we can get bogus aggregates
2151 -- from misuse of parentheses, and we are about to complain about
2152 -- the aggregate without even looking inside it.
2154 -- Instead, if we have an aggregate of type Any_Composite, then
2155 -- analyze and resolve the component fields, and then only issue
2156 -- another message if we get no errors doing this (otherwise
2157 -- assume that the errors in the aggregate caused the problem).
2159 elsif Nkind (N) = N_Aggregate
2160 and then Etype (N) = Any_Composite
2162 -- Disable expansion in any case. If there is a type mismatch
2163 -- it may be fatal to try to expand the aggregate. The flag
2164 -- would otherwise be set to false when the error is posted.
2166 Expander_Active := False;
2169 procedure Check_Aggr (Aggr : Node_Id);
2170 -- Check one aggregate, and set Found to True if we have a
2171 -- definite error in any of its elements
2173 procedure Check_Elmt (Aelmt : Node_Id);
2174 -- Check one element of aggregate and set Found to True if
2175 -- we definitely have an error in the element.
2181 procedure Check_Aggr (Aggr : Node_Id) is
2185 if Present (Expressions (Aggr)) then
2186 Elmt := First (Expressions (Aggr));
2187 while Present (Elmt) loop
2193 if Present (Component_Associations (Aggr)) then
2194 Elmt := First (Component_Associations (Aggr));
2195 while Present (Elmt) loop
2197 -- If this is a default-initialized component, then
2198 -- there is nothing to check. The box will be
2199 -- replaced by the appropriate call during late
2202 if not Box_Present (Elmt) then
2203 Check_Elmt (Expression (Elmt));
2215 procedure Check_Elmt (Aelmt : Node_Id) is
2217 -- If we have a nested aggregate, go inside it (to
2218 -- attempt a naked analyze-resolve of the aggregate
2219 -- can cause undesirable cascaded errors). Do not
2220 -- resolve expression if it needs a type from context,
2221 -- as for integer * fixed expression.
2223 if Nkind (Aelmt) = N_Aggregate then
2229 if not Is_Overloaded (Aelmt)
2230 and then Etype (Aelmt) /= Any_Fixed
2235 if Etype (Aelmt) = Any_Type then
2246 -- If an error message was issued already, Found got reset
2247 -- to True, so if it is still False, issue the standard
2248 -- Wrong_Type message.
2251 if Is_Overloaded (N)
2252 and then Nkind (N) = N_Function_Call
2255 Subp_Name : Node_Id;
2257 if Is_Entity_Name (Name (N)) then
2258 Subp_Name := Name (N);
2260 elsif Nkind (Name (N)) = N_Selected_Component then
2262 -- Protected operation: retrieve operation name
2264 Subp_Name := Selector_Name (Name (N));
2266 raise Program_Error;
2269 Error_Msg_Node_2 := Typ;
2270 Error_Msg_NE ("no visible interpretation of&" &
2271 " matches expected type&", N, Subp_Name);
2274 if All_Errors_Mode then
2276 Index : Interp_Index;
2280 Error_Msg_N ("\\possible interpretations:", N);
2282 Get_First_Interp (Name (N), Index, It);
2283 while Present (It.Nam) loop
2284 Error_Msg_Sloc := Sloc (It.Nam);
2285 Error_Msg_Node_2 := It.Nam;
2287 ("\\ type& for & declared#", N, It.Typ);
2288 Get_Next_Interp (Index, It);
2293 Error_Msg_N ("\use -gnatf for details", N);
2296 Wrong_Type (N, Typ);
2304 -- Test if we have more than one interpretation for the context
2306 elsif Ambiguous then
2310 -- Here we have an acceptable interpretation for the context
2313 -- Propagate type information and normalize tree for various
2314 -- predefined operations. If the context only imposes a class of
2315 -- types, rather than a specific type, propagate the actual type
2318 if Typ = Any_Integer
2319 or else Typ = Any_Boolean
2320 or else Typ = Any_Modular
2321 or else Typ = Any_Real
2322 or else Typ = Any_Discrete
2324 Ctx_Type := Expr_Type;
2326 -- Any_Fixed is legal in a real context only if a specific
2327 -- fixed point type is imposed. If Norman Cohen can be
2328 -- confused by this, it deserves a separate message.
2331 and then Expr_Type = Any_Fixed
2333 Error_Msg_N ("illegal context for mixed mode operation", N);
2334 Set_Etype (N, Universal_Real);
2335 Ctx_Type := Universal_Real;
2339 -- A user-defined operator is tranformed into a function call at
2340 -- this point, so that further processing knows that operators are
2341 -- really operators (i.e. are predefined operators). User-defined
2342 -- operators that are intrinsic are just renamings of the predefined
2343 -- ones, and need not be turned into calls either, but if they rename
2344 -- a different operator, we must transform the node accordingly.
2345 -- Instantiations of Unchecked_Conversion are intrinsic but are
2346 -- treated as functions, even if given an operator designator.
2348 if Nkind (N) in N_Op
2349 and then Present (Entity (N))
2350 and then Ekind (Entity (N)) /= E_Operator
2353 if not Is_Predefined_Op (Entity (N)) then
2354 Rewrite_Operator_As_Call (N, Entity (N));
2356 elsif Present (Alias (Entity (N)))
2358 Nkind (Parent (Parent (Entity (N))))
2359 = N_Subprogram_Renaming_Declaration
2361 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2363 -- If the node is rewritten, it will be fully resolved in
2364 -- Rewrite_Renamed_Operator.
2366 if Analyzed (N) then
2372 case N_Subexpr'(Nkind (N)) is
2374 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2376 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2378 when N_And_Then | N_Or_Else
2379 => Resolve_Short_Circuit (N, Ctx_Type);
2381 when N_Attribute_Reference
2382 => Resolve_Attribute (N, Ctx_Type);
2384 when N_Character_Literal
2385 => Resolve_Character_Literal (N, Ctx_Type);
2387 when N_Conditional_Expression
2388 => Resolve_Conditional_Expression (N, Ctx_Type);
2390 when N_Expanded_Name
2391 => Resolve_Entity_Name (N, Ctx_Type);
2393 when N_Extension_Aggregate
2394 => Resolve_Extension_Aggregate (N, Ctx_Type);
2396 when N_Explicit_Dereference
2397 => Resolve_Explicit_Dereference (N, Ctx_Type);
2399 when N_Function_Call
2400 => Resolve_Call (N, Ctx_Type);
2403 => Resolve_Entity_Name (N, Ctx_Type);
2405 when N_Indexed_Component
2406 => Resolve_Indexed_Component (N, Ctx_Type);
2408 when N_Integer_Literal
2409 => Resolve_Integer_Literal (N, Ctx_Type);
2411 when N_Membership_Test
2412 => Resolve_Membership_Op (N, Ctx_Type);
2414 when N_Null => Resolve_Null (N, Ctx_Type);
2416 when N_Op_And | N_Op_Or | N_Op_Xor
2417 => Resolve_Logical_Op (N, Ctx_Type);
2419 when N_Op_Eq | N_Op_Ne
2420 => Resolve_Equality_Op (N, Ctx_Type);
2422 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2423 => Resolve_Comparison_Op (N, Ctx_Type);
2425 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2427 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2428 N_Op_Divide | N_Op_Mod | N_Op_Rem
2430 => Resolve_Arithmetic_Op (N, Ctx_Type);
2432 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2434 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2436 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2437 => Resolve_Unary_Op (N, Ctx_Type);
2439 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2441 when N_Procedure_Call_Statement
2442 => Resolve_Call (N, Ctx_Type);
2444 when N_Operator_Symbol
2445 => Resolve_Operator_Symbol (N, Ctx_Type);
2447 when N_Qualified_Expression
2448 => Resolve_Qualified_Expression (N, Ctx_Type);
2450 when N_Raise_xxx_Error
2451 => Set_Etype (N, Ctx_Type);
2453 when N_Range => Resolve_Range (N, Ctx_Type);
2456 => Resolve_Real_Literal (N, Ctx_Type);
2458 when N_Reference => Resolve_Reference (N, Ctx_Type);
2460 when N_Selected_Component
2461 => Resolve_Selected_Component (N, Ctx_Type);
2463 when N_Slice => Resolve_Slice (N, Ctx_Type);
2465 when N_String_Literal
2466 => Resolve_String_Literal (N, Ctx_Type);
2468 when N_Subprogram_Info
2469 => Resolve_Subprogram_Info (N, Ctx_Type);
2471 when N_Type_Conversion
2472 => Resolve_Type_Conversion (N, Ctx_Type);
2474 when N_Unchecked_Expression =>
2475 Resolve_Unchecked_Expression (N, Ctx_Type);
2477 when N_Unchecked_Type_Conversion =>
2478 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2482 -- If the subexpression was replaced by a non-subexpression, then
2483 -- all we do is to expand it. The only legitimate case we know of
2484 -- is converting procedure call statement to entry call statements,
2485 -- but there may be others, so we are making this test general.
2487 if Nkind (N) not in N_Subexpr then
2488 Debug_A_Exit ("resolving ", N, " (done)");
2493 -- The expression is definitely NOT overloaded at this point, so
2494 -- we reset the Is_Overloaded flag to avoid any confusion when
2495 -- reanalyzing the node.
2497 Set_Is_Overloaded (N, False);
2499 -- Freeze expression type, entity if it is a name, and designated
2500 -- type if it is an allocator (RM 13.14(10,11,13)).
2502 -- Now that the resolution of the type of the node is complete,
2503 -- and we did not detect an error, we can expand this node. We
2504 -- skip the expand call if we are in a default expression, see
2505 -- section "Handling of Default Expressions" in Sem spec.
2507 Debug_A_Exit ("resolving ", N, " (done)");
2509 -- We unconditionally freeze the expression, even if we are in
2510 -- default expression mode (the Freeze_Expression routine tests
2511 -- this flag and only freezes static types if it is set).
2513 Freeze_Expression (N);
2515 -- Now we can do the expansion
2525 -- Version with check(s) suppressed
2527 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2529 if Suppress = All_Checks then
2531 Svg : constant Suppress_Array := Scope_Suppress;
2533 Scope_Suppress := (others => True);
2535 Scope_Suppress := Svg;
2540 Svg : constant Boolean := Scope_Suppress (Suppress);
2542 Scope_Suppress (Suppress) := True;
2544 Scope_Suppress (Suppress) := Svg;
2553 -- Version with implicit type
2555 procedure Resolve (N : Node_Id) is
2557 Resolve (N, Etype (N));
2560 ---------------------
2561 -- Resolve_Actuals --
2562 ---------------------
2564 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2565 Loc : constant Source_Ptr := Sloc (N);
2570 Prev : Node_Id := Empty;
2572 procedure Check_Prefixed_Call;
2573 -- If the original node is an overloaded call in prefix notation,
2574 -- insert an 'Access or a dereference as needed over the first actual.
2575 -- Try_Object_Operation has already verified that there is a valid
2576 -- interpretation, but the form of the actual can only be determined
2577 -- once the primitive operation is identified.
2579 procedure Insert_Default;
2580 -- If the actual is missing in a call, insert in the actuals list
2581 -- an instance of the default expression. The insertion is always
2582 -- a named association.
2584 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2585 -- Check whether T1 and T2, or their full views, are derived from a
2586 -- common type. Used to enforce the restrictions on array conversions
2589 -------------------------
2590 -- Check_Prefixed_Call --
2591 -------------------------
2593 procedure Check_Prefixed_Call is
2594 Act : constant Node_Id := First_Actual (N);
2595 A_Type : constant Entity_Id := Etype (Act);
2596 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2597 Orig : constant Node_Id := Original_Node (N);
2601 -- Check whether the call is a prefixed call, with or without
2602 -- additional actuals.
2604 if Nkind (Orig) = N_Selected_Component
2606 (Nkind (Orig) = N_Indexed_Component
2607 and then Nkind (Prefix (Orig)) = N_Selected_Component
2608 and then Is_Entity_Name (Prefix (Prefix (Orig)))
2609 and then Is_Entity_Name (Act)
2610 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
2612 if Is_Access_Type (A_Type)
2613 and then not Is_Access_Type (F_Type)
2615 -- Introduce dereference on object in prefix
2618 Make_Explicit_Dereference (Sloc (Act),
2619 Prefix => Relocate_Node (Act));
2620 Rewrite (Act, New_A);
2623 elsif Is_Access_Type (F_Type)
2624 and then not Is_Access_Type (A_Type)
2626 -- Introduce an implicit 'Access in prefix
2628 if not Is_Aliased_View (Act) then
2630 ("object in prefixed call to& must be aliased"
2631 & " (RM-2005 4.3.1 (13))",
2636 Make_Attribute_Reference (Loc,
2637 Attribute_Name => Name_Access,
2638 Prefix => Relocate_Node (Act)));
2643 end Check_Prefixed_Call;
2645 --------------------
2646 -- Insert_Default --
2647 --------------------
2649 procedure Insert_Default is
2654 -- Missing argument in call, nothing to insert
2656 if No (Default_Value (F)) then
2660 -- Note that we do a full New_Copy_Tree, so that any associated
2661 -- Itypes are properly copied. This may not be needed any more,
2662 -- but it does no harm as a safety measure! Defaults of a generic
2663 -- formal may be out of bounds of the corresponding actual (see
2664 -- cc1311b) and an additional check may be required.
2669 New_Scope => Current_Scope,
2672 if Is_Concurrent_Type (Scope (Nam))
2673 and then Has_Discriminants (Scope (Nam))
2675 Replace_Actual_Discriminants (N, Actval);
2678 if Is_Overloadable (Nam)
2679 and then Present (Alias (Nam))
2681 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
2682 and then not Is_Tagged_Type (Etype (F))
2684 -- If default is a real literal, do not introduce a
2685 -- conversion whose effect may depend on the run-time
2686 -- size of universal real.
2688 if Nkind (Actval) = N_Real_Literal then
2689 Set_Etype (Actval, Base_Type (Etype (F)));
2691 Actval := Unchecked_Convert_To (Etype (F), Actval);
2695 if Is_Scalar_Type (Etype (F)) then
2696 Enable_Range_Check (Actval);
2699 Set_Parent (Actval, N);
2701 -- Resolve aggregates with their base type, to avoid scope
2702 -- anomalies: the subtype was first built in the suprogram
2703 -- declaration, and the current call may be nested.
2705 if Nkind (Actval) = N_Aggregate
2706 and then Has_Discriminants (Etype (Actval))
2708 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2710 Analyze_And_Resolve (Actval, Etype (Actval));
2714 Set_Parent (Actval, N);
2716 -- See note above concerning aggregates
2718 if Nkind (Actval) = N_Aggregate
2719 and then Has_Discriminants (Etype (Actval))
2721 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2723 -- Resolve entities with their own type, which may differ
2724 -- from the type of a reference in a generic context (the
2725 -- view swapping mechanism did not anticipate the re-analysis
2726 -- of default values in calls).
2728 elsif Is_Entity_Name (Actval) then
2729 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
2732 Analyze_And_Resolve (Actval, Etype (Actval));
2736 -- If default is a tag indeterminate function call, propagate
2737 -- tag to obtain proper dispatching.
2739 if Is_Controlling_Formal (F)
2740 and then Nkind (Default_Value (F)) = N_Function_Call
2742 Set_Is_Controlling_Actual (Actval);
2747 -- If the default expression raises constraint error, then just
2748 -- silently replace it with an N_Raise_Constraint_Error node,
2749 -- since we already gave the warning on the subprogram spec.
2751 if Raises_Constraint_Error (Actval) then
2753 Make_Raise_Constraint_Error (Loc,
2754 Reason => CE_Range_Check_Failed));
2755 Set_Raises_Constraint_Error (Actval);
2756 Set_Etype (Actval, Etype (F));
2760 Make_Parameter_Association (Loc,
2761 Explicit_Actual_Parameter => Actval,
2762 Selector_Name => Make_Identifier (Loc, Chars (F)));
2764 -- Case of insertion is first named actual
2766 if No (Prev) or else
2767 Nkind (Parent (Prev)) /= N_Parameter_Association
2769 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
2770 Set_First_Named_Actual (N, Actval);
2773 if No (Parameter_Associations (N)) then
2774 Set_Parameter_Associations (N, New_List (Assoc));
2776 Append (Assoc, Parameter_Associations (N));
2780 Insert_After (Prev, Assoc);
2783 -- Case of insertion is not first named actual
2786 Set_Next_Named_Actual
2787 (Assoc, Next_Named_Actual (Parent (Prev)));
2788 Set_Next_Named_Actual (Parent (Prev), Actval);
2789 Append (Assoc, Parameter_Associations (N));
2792 Mark_Rewrite_Insertion (Assoc);
2793 Mark_Rewrite_Insertion (Actval);
2802 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
2803 FT1 : Entity_Id := T1;
2804 FT2 : Entity_Id := T2;
2807 if Is_Private_Type (T1)
2808 and then Present (Full_View (T1))
2810 FT1 := Full_View (T1);
2813 if Is_Private_Type (T2)
2814 and then Present (Full_View (T2))
2816 FT2 := Full_View (T2);
2819 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
2822 -- Start of processing for Resolve_Actuals
2825 if Present (First_Actual (N)) then
2826 Check_Prefixed_Call;
2829 A := First_Actual (N);
2830 F := First_Formal (Nam);
2831 while Present (F) loop
2832 if No (A) and then Needs_No_Actuals (Nam) then
2835 -- If we have an error in any actual or formal, indicated by
2836 -- a type of Any_Type, then abandon resolution attempt, and
2837 -- set result type to Any_Type.
2839 elsif (Present (A) and then Etype (A) = Any_Type)
2840 or else Etype (F) = Any_Type
2842 Set_Etype (N, Any_Type);
2847 and then (Nkind (Parent (A)) /= N_Parameter_Association
2849 Chars (Selector_Name (Parent (A))) = Chars (F))
2851 -- If the formal is Out or In_Out, do not resolve and expand the
2852 -- conversion, because it is subsequently expanded into explicit
2853 -- temporaries and assignments. However, the object of the
2854 -- conversion can be resolved. An exception is the case of tagged
2855 -- type conversion with a class-wide actual. In that case we want
2856 -- the tag check to occur and no temporary will be needed (no
2857 -- representation change can occur) and the parameter is passed by
2858 -- reference, so we go ahead and resolve the type conversion.
2859 -- Another exception is the case of reference to component or
2860 -- subcomponent of a bit-packed array, in which case we want to
2861 -- defer expansion to the point the in and out assignments are
2864 if Ekind (F) /= E_In_Parameter
2865 and then Nkind (A) = N_Type_Conversion
2866 and then not Is_Class_Wide_Type (Etype (Expression (A)))
2868 if Ekind (F) = E_In_Out_Parameter
2869 and then Is_Array_Type (Etype (F))
2871 if Has_Aliased_Components (Etype (Expression (A)))
2872 /= Has_Aliased_Components (Etype (F))
2874 if Ada_Version < Ada_05 then
2876 ("both component types in a view conversion must be"
2877 & " aliased, or neither", A);
2879 -- Ada 2005: rule is relaxed (see AI-363)
2881 elsif Has_Aliased_Components (Etype (F))
2883 not Has_Aliased_Components (Etype (Expression (A)))
2886 ("view conversion operand must have aliased " &
2889 ("\since target type has aliased components", N);
2892 elsif not Same_Ancestor (Etype (F), Etype (Expression (A)))
2894 (Is_By_Reference_Type (Etype (F))
2895 or else Is_By_Reference_Type (Etype (Expression (A))))
2898 ("view conversion between unrelated by reference " &
2899 "array types not allowed (\'A'I-00246)", A);
2903 if (Conversion_OK (A)
2904 or else Valid_Conversion (A, Etype (A), Expression (A)))
2905 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
2907 Resolve (Expression (A));
2910 -- If the actual is a function call that returns a limited
2911 -- unconstrained object that needs finalization, create a
2912 -- transient scope for it, so that it can receive the proper
2913 -- finalization list.
2915 elsif Nkind (A) = N_Function_Call
2916 and then Is_Limited_Record (Etype (F))
2917 and then not Is_Constrained (Etype (F))
2918 and then Expander_Active
2920 (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
2922 Establish_Transient_Scope (A, False);
2925 if Nkind (A) = N_Type_Conversion
2926 and then Is_Array_Type (Etype (F))
2927 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
2929 (Is_Limited_Type (Etype (F))
2930 or else Is_Limited_Type (Etype (Expression (A))))
2933 ("conversion between unrelated limited array types " &
2934 "not allowed (\A\I-00246)", A);
2936 if Is_Limited_Type (Etype (F)) then
2937 Explain_Limited_Type (Etype (F), A);
2940 if Is_Limited_Type (Etype (Expression (A))) then
2941 Explain_Limited_Type (Etype (Expression (A)), A);
2945 -- (Ada 2005: AI-251): If the actual is an allocator whose
2946 -- directly designated type is a class-wide interface, we build
2947 -- an anonymous access type to use it as the type of the
2948 -- allocator. Later, when the subprogram call is expanded, if
2949 -- the interface has a secondary dispatch table the expander
2950 -- will add a type conversion to force the correct displacement
2953 if Nkind (A) = N_Allocator then
2955 DDT : constant Entity_Id :=
2956 Directly_Designated_Type (Base_Type (Etype (F)));
2957 New_Itype : Entity_Id;
2959 if Is_Class_Wide_Type (DDT)
2960 and then Is_Interface (DDT)
2962 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
2963 Set_Etype (New_Itype, Etype (A));
2964 Init_Size_Align (New_Itype);
2965 Set_Directly_Designated_Type (New_Itype,
2966 Directly_Designated_Type (Etype (A)));
2967 Set_Etype (A, New_Itype);
2970 -- Ada 2005, AI-162:If the actual is an allocator, the
2971 -- innermost enclosing statement is the master of the
2972 -- created object. This needs to be done with expansion
2973 -- enabled only, otherwise the transient scope will not
2974 -- be removed in the expansion of the wrapped construct.
2976 if (Is_Controlled (DDT)
2977 or else Has_Task (DDT))
2978 and then Expander_Active
2980 Establish_Transient_Scope (A, False);
2985 -- (Ada 2005): The call may be to a primitive operation of
2986 -- a tagged synchronized type, declared outside of the type.
2987 -- In this case the controlling actual must be converted to
2988 -- its corresponding record type, which is the formal type.
2990 if Is_Concurrent_Type (Etype (A))
2991 and then Etype (F) = Corresponding_Record_Type (Etype (A))
2994 Unchecked_Convert_To
2995 (Corresponding_Record_Type (Etype (A)), A));
2998 Resolve (A, Etype (F));
3004 -- Perform error checks for IN and IN OUT parameters
3006 if Ekind (F) /= E_Out_Parameter then
3008 -- Check unset reference. For scalar parameters, it is clearly
3009 -- wrong to pass an uninitialized value as either an IN or
3010 -- IN-OUT parameter. For composites, it is also clearly an
3011 -- error to pass a completely uninitialized value as an IN
3012 -- parameter, but the case of IN OUT is trickier. We prefer
3013 -- not to give a warning here. For example, suppose there is
3014 -- a routine that sets some component of a record to False.
3015 -- It is perfectly reasonable to make this IN-OUT and allow
3016 -- either initialized or uninitialized records to be passed
3019 -- For partially initialized composite values, we also avoid
3020 -- warnings, since it is quite likely that we are passing a
3021 -- partially initialized value and only the initialized fields
3022 -- will in fact be read in the subprogram.
3024 if Is_Scalar_Type (A_Typ)
3025 or else (Ekind (F) = E_In_Parameter
3026 and then not Is_Partially_Initialized_Type (A_Typ))
3028 Check_Unset_Reference (A);
3031 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3032 -- actual to a nested call, since this is case of reading an
3033 -- out parameter, which is not allowed.
3035 if Ada_Version = Ada_83
3036 and then Is_Entity_Name (A)
3037 and then Ekind (Entity (A)) = E_Out_Parameter
3039 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3043 if Ekind (F) /= E_In_Parameter
3044 and then not Is_OK_Variable_For_Out_Formal (A)
3046 Error_Msg_NE ("actual for& must be a variable", A, F);
3048 if Is_Entity_Name (A) then
3049 Kill_Checks (Entity (A));
3055 if Etype (A) = Any_Type then
3056 Set_Etype (N, Any_Type);
3060 -- Apply appropriate range checks for in, out, and in-out
3061 -- parameters. Out and in-out parameters also need a separate
3062 -- check, if there is a type conversion, to make sure the return
3063 -- value meets the constraints of the variable before the
3066 -- Gigi looks at the check flag and uses the appropriate types.
3067 -- For now since one flag is used there is an optimization which
3068 -- might not be done in the In Out case since Gigi does not do
3069 -- any analysis. More thought required about this ???
3071 if Ekind (F) = E_In_Parameter
3072 or else Ekind (F) = E_In_Out_Parameter
3074 if Is_Scalar_Type (Etype (A)) then
3075 Apply_Scalar_Range_Check (A, F_Typ);
3077 elsif Is_Array_Type (Etype (A)) then
3078 Apply_Length_Check (A, F_Typ);
3080 elsif Is_Record_Type (F_Typ)
3081 and then Has_Discriminants (F_Typ)
3082 and then Is_Constrained (F_Typ)
3083 and then (not Is_Derived_Type (F_Typ)
3084 or else Comes_From_Source (Nam))
3086 Apply_Discriminant_Check (A, F_Typ);
3088 elsif Is_Access_Type (F_Typ)
3089 and then Is_Array_Type (Designated_Type (F_Typ))
3090 and then Is_Constrained (Designated_Type (F_Typ))
3092 Apply_Length_Check (A, F_Typ);
3094 elsif Is_Access_Type (F_Typ)
3095 and then Has_Discriminants (Designated_Type (F_Typ))
3096 and then Is_Constrained (Designated_Type (F_Typ))
3098 Apply_Discriminant_Check (A, F_Typ);
3101 Apply_Range_Check (A, F_Typ);
3104 -- Ada 2005 (AI-231)
3106 if Ada_Version >= Ada_05
3107 and then Is_Access_Type (F_Typ)
3108 and then Can_Never_Be_Null (F_Typ)
3109 and then Known_Null (A)
3111 Apply_Compile_Time_Constraint_Error
3113 Msg => "(Ada 2005) null not allowed in "
3114 & "null-excluding formal?",
3115 Reason => CE_Null_Not_Allowed);
3119 if Ekind (F) = E_Out_Parameter
3120 or else Ekind (F) = E_In_Out_Parameter
3122 if Nkind (A) = N_Type_Conversion then
3123 if Is_Scalar_Type (A_Typ) then
3124 Apply_Scalar_Range_Check
3125 (Expression (A), Etype (Expression (A)), A_Typ);
3128 (Expression (A), Etype (Expression (A)), A_Typ);
3132 if Is_Scalar_Type (F_Typ) then
3133 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3135 elsif Is_Array_Type (F_Typ)
3136 and then Ekind (F) = E_Out_Parameter
3138 Apply_Length_Check (A, F_Typ);
3141 Apply_Range_Check (A, A_Typ, F_Typ);
3146 -- An actual associated with an access parameter is implicitly
3147 -- converted to the anonymous access type of the formal and
3148 -- must satisfy the legality checks for access conversions.
3150 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3151 if not Valid_Conversion (A, F_Typ, A) then
3153 ("invalid implicit conversion for access parameter", A);
3157 -- Check bad case of atomic/volatile argument (RM C.6(12))
3159 if Is_By_Reference_Type (Etype (F))
3160 and then Comes_From_Source (N)
3162 if Is_Atomic_Object (A)
3163 and then not Is_Atomic (Etype (F))
3166 ("cannot pass atomic argument to non-atomic formal",
3169 elsif Is_Volatile_Object (A)
3170 and then not Is_Volatile (Etype (F))
3173 ("cannot pass volatile argument to non-volatile formal",
3178 -- Check that subprograms don't have improper controlling
3179 -- arguments (RM 3.9.2 (9))
3181 -- A primitive operation may have an access parameter of an
3182 -- incomplete tagged type, but a dispatching call is illegal
3183 -- if the type is still incomplete.
3185 if Is_Controlling_Formal (F) then
3186 Set_Is_Controlling_Actual (A);
3188 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3190 Desig : constant Entity_Id := Designated_Type (Etype (F));
3192 if Ekind (Desig) = E_Incomplete_Type
3193 and then No (Full_View (Desig))
3194 and then No (Non_Limited_View (Desig))
3197 ("premature use of incomplete type& " &
3198 "in dispatching call", A, Desig);
3203 elsif Nkind (A) = N_Explicit_Dereference then
3204 Validate_Remote_Access_To_Class_Wide_Type (A);
3207 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3208 and then not Is_Class_Wide_Type (F_Typ)
3209 and then not Is_Controlling_Formal (F)
3211 Error_Msg_N ("class-wide argument not allowed here!", A);
3213 if Is_Subprogram (Nam)
3214 and then Comes_From_Source (Nam)
3216 Error_Msg_Node_2 := F_Typ;
3218 ("& is not a dispatching operation of &!", A, Nam);
3221 elsif Is_Access_Type (A_Typ)
3222 and then Is_Access_Type (F_Typ)
3223 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3224 and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3225 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3226 or else (Nkind (A) = N_Attribute_Reference
3228 Is_Class_Wide_Type (Etype (Prefix (A)))))
3229 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3230 and then not Is_Controlling_Formal (F)
3233 ("access to class-wide argument not allowed here!", A);
3235 if Is_Subprogram (Nam)
3236 and then Comes_From_Source (Nam)
3238 Error_Msg_Node_2 := Designated_Type (F_Typ);
3240 ("& is not a dispatching operation of &!", A, Nam);
3246 -- If it is a named association, treat the selector_name as
3247 -- a proper identifier, and mark the corresponding entity.
3249 if Nkind (Parent (A)) = N_Parameter_Association then
3250 Set_Entity (Selector_Name (Parent (A)), F);
3251 Generate_Reference (F, Selector_Name (Parent (A)));
3252 Set_Etype (Selector_Name (Parent (A)), F_Typ);
3253 Generate_Reference (F_Typ, N, ' ');
3258 if Ekind (F) /= E_Out_Parameter then
3259 Check_Unset_Reference (A);
3264 -- Case where actual is not present
3272 end Resolve_Actuals;
3274 -----------------------
3275 -- Resolve_Allocator --
3276 -----------------------
3278 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3279 E : constant Node_Id := Expression (N);
3281 Discrim : Entity_Id;
3284 Assoc : Node_Id := Empty;
3287 procedure Check_Allocator_Discrim_Accessibility
3288 (Disc_Exp : Node_Id;
3289 Alloc_Typ : Entity_Id);
3290 -- Check that accessibility level associated with an access discriminant
3291 -- initialized in an allocator by the expression Disc_Exp is not deeper
3292 -- than the level of the allocator type Alloc_Typ. An error message is
3293 -- issued if this condition is violated. Specialized checks are done for
3294 -- the cases of a constraint expression which is an access attribute or
3295 -- an access discriminant.
3297 function In_Dispatching_Context return Boolean;
3298 -- If the allocator is an actual in a call, it is allowed to be class-
3299 -- wide when the context is not because it is a controlling actual.
3301 procedure Propagate_Coextensions (Root : Node_Id);
3302 -- Propagate all nested coextensions which are located one nesting
3303 -- level down the tree to the node Root. Example:
3306 -- Level_1_Coextension
3307 -- Level_2_Coextension
3309 -- The algorithm is paired with delay actions done by the Expander. In
3310 -- the above example, assume all coextensions are controlled types.
3311 -- The cycle of analysis, resolution and expansion will yield:
3313 -- 1) Analyze Top_Record
3314 -- 2) Analyze Level_1_Coextension
3315 -- 3) Analyze Level_2_Coextension
3316 -- 4) Resolve Level_2_Coextnesion. The allocator is marked as a
3318 -- 5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3319 -- generated to capture the allocated object. Temp_1 is attached
3320 -- to the coextension chain of Level_2_Coextension.
3321 -- 6) Resolve Level_1_Coextension. The allocator is marked as a
3322 -- coextension. A forward tree traversal is performed which finds
3323 -- Level_2_Coextension's list and copies its contents into its
3325 -- 7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3326 -- generated to capture the allocated object. Temp_2 is attached
3327 -- to the coextension chain of Level_1_Coextension. Currently, the
3328 -- contents of the list are [Temp_2, Temp_1].
3329 -- 8) Resolve Top_Record. A forward tree traversal is performed which
3330 -- finds Level_1_Coextension's list and copies its contents into
3332 -- 9) Expand Top_Record. Generate finalization calls for Temp_1 and
3333 -- Temp_2 and attach them to Top_Record's finalization list.
3335 -------------------------------------------
3336 -- Check_Allocator_Discrim_Accessibility --
3337 -------------------------------------------
3339 procedure Check_Allocator_Discrim_Accessibility
3340 (Disc_Exp : Node_Id;
3341 Alloc_Typ : Entity_Id)
3344 if Type_Access_Level (Etype (Disc_Exp)) >
3345 Type_Access_Level (Alloc_Typ)
3348 ("operand type has deeper level than allocator type", Disc_Exp);
3350 -- When the expression is an Access attribute the level of the prefix
3351 -- object must not be deeper than that of the allocator's type.
3353 elsif Nkind (Disc_Exp) = N_Attribute_Reference
3354 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
3356 and then Object_Access_Level (Prefix (Disc_Exp))
3357 > Type_Access_Level (Alloc_Typ)
3360 ("prefix of attribute has deeper level than allocator type",
3363 -- When the expression is an access discriminant the check is against
3364 -- the level of the prefix object.
3366 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
3367 and then Nkind (Disc_Exp) = N_Selected_Component
3368 and then Object_Access_Level (Prefix (Disc_Exp))
3369 > Type_Access_Level (Alloc_Typ)
3372 ("access discriminant has deeper level than allocator type",
3375 -- All other cases are legal
3380 end Check_Allocator_Discrim_Accessibility;
3382 ----------------------------
3383 -- In_Dispatching_Context --
3384 ----------------------------
3386 function In_Dispatching_Context return Boolean is
3387 Par : constant Node_Id := Parent (N);
3389 return (Nkind (Par) = N_Function_Call
3390 or else Nkind (Par) = N_Procedure_Call_Statement)
3391 and then Is_Entity_Name (Name (Par))
3392 and then Is_Dispatching_Operation (Entity (Name (Par)));
3393 end In_Dispatching_Context;
3395 ----------------------------
3396 -- Propagate_Coextensions --
3397 ----------------------------
3399 procedure Propagate_Coextensions (Root : Node_Id) is
3401 procedure Copy_List (From : Elist_Id; To : Elist_Id);
3402 -- Copy the contents of list From into list To, preserving the
3403 -- order of elements.
3405 function Process_Allocator (Nod : Node_Id) return Traverse_Result;
3406 -- Recognize an allocator or a rewritten allocator node and add it
3407 -- allong with its nested coextensions to the list of Root.
3413 procedure Copy_List (From : Elist_Id; To : Elist_Id) is
3414 From_Elmt : Elmt_Id;
3416 From_Elmt := First_Elmt (From);
3417 while Present (From_Elmt) loop
3418 Append_Elmt (Node (From_Elmt), To);
3419 Next_Elmt (From_Elmt);
3423 -----------------------
3424 -- Process_Allocator --
3425 -----------------------
3427 function Process_Allocator (Nod : Node_Id) return Traverse_Result is
3428 Orig_Nod : Node_Id := Nod;
3431 -- This is a possible rewritten subtype indication allocator. Any
3432 -- nested coextensions will appear as discriminant constraints.
3434 if Nkind (Nod) = N_Identifier
3435 and then Present (Original_Node (Nod))
3436 and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
3440 Discr_Elmt : Elmt_Id;
3443 if Is_Record_Type (Entity (Nod)) then
3445 First_Elmt (Discriminant_Constraint (Entity (Nod)));
3446 while Present (Discr_Elmt) loop
3447 Discr := Node (Discr_Elmt);
3449 if Nkind (Discr) = N_Identifier
3450 and then Present (Original_Node (Discr))
3451 and then Nkind (Original_Node (Discr)) = N_Allocator
3452 and then Present (Coextensions (
3453 Original_Node (Discr)))
3455 if No (Coextensions (Root)) then
3456 Set_Coextensions (Root, New_Elmt_List);
3460 (From => Coextensions (Original_Node (Discr)),
3461 To => Coextensions (Root));
3464 Next_Elmt (Discr_Elmt);
3467 -- There is no need to continue the traversal of this
3468 -- subtree since all the information has already been
3475 -- Case of either a stand alone allocator or a rewritten allocator
3476 -- with an aggregate.
3479 if Present (Original_Node (Nod)) then
3480 Orig_Nod := Original_Node (Nod);
3483 if Nkind (Orig_Nod) = N_Allocator then
3485 -- Propagate the list of nested coextensions to the Root
3486 -- allocator. This is done through list copy since a single
3487 -- allocator may have multiple coextensions. Do not touch
3488 -- coextensions roots.
3490 if not Is_Coextension_Root (Orig_Nod)
3491 and then Present (Coextensions (Orig_Nod))
3493 if No (Coextensions (Root)) then
3494 Set_Coextensions (Root, New_Elmt_List);
3498 (From => Coextensions (Orig_Nod),
3499 To => Coextensions (Root));
3502 -- There is no need to continue the traversal of this
3503 -- subtree since all the information has already been
3510 -- Keep on traversing, looking for the next allocator
3513 end Process_Allocator;
3515 procedure Process_Allocators is
3516 new Traverse_Proc (Process_Allocator);
3518 -- Start of processing for Propagate_Coextensions
3521 Process_Allocators (Expression (Root));
3522 end Propagate_Coextensions;
3524 -- Start of processing for Resolve_Allocator
3527 -- Replace general access with specific type
3529 if Ekind (Etype (N)) = E_Allocator_Type then
3530 Set_Etype (N, Base_Type (Typ));
3533 if Is_Abstract_Type (Typ) then
3534 Error_Msg_N ("type of allocator cannot be abstract", N);
3537 -- For qualified expression, resolve the expression using the
3538 -- given subtype (nothing to do for type mark, subtype indication)
3540 if Nkind (E) = N_Qualified_Expression then
3541 if Is_Class_Wide_Type (Etype (E))
3542 and then not Is_Class_Wide_Type (Designated_Type (Typ))
3543 and then not In_Dispatching_Context
3546 ("class-wide allocator not allowed for this access type", N);
3549 Resolve (Expression (E), Etype (E));
3550 Check_Unset_Reference (Expression (E));
3552 -- A qualified expression requires an exact match of the type,
3553 -- class-wide matching is not allowed.
3555 if (Is_Class_Wide_Type (Etype (Expression (E)))
3556 or else Is_Class_Wide_Type (Etype (E)))
3557 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
3559 Wrong_Type (Expression (E), Etype (E));
3562 -- A special accessibility check is needed for allocators that
3563 -- constrain access discriminants. The level of the type of the
3564 -- expression used to constrain an access discriminant cannot be
3565 -- deeper than the type of the allocator (in constrast to access
3566 -- parameters, where the level of the actual can be arbitrary).
3568 -- We can't use Valid_Conversion to perform this check because
3569 -- in general the type of the allocator is unrelated to the type
3570 -- of the access discriminant.
3572 if Ekind (Typ) /= E_Anonymous_Access_Type
3573 or else Is_Local_Anonymous_Access (Typ)
3575 Subtyp := Entity (Subtype_Mark (E));
3577 Aggr := Original_Node (Expression (E));
3579 if Has_Discriminants (Subtyp)
3581 (Nkind (Aggr) = N_Aggregate
3583 Nkind (Aggr) = N_Extension_Aggregate)
3585 Discrim := First_Discriminant (Base_Type (Subtyp));
3587 -- Get the first component expression of the aggregate
3589 if Present (Expressions (Aggr)) then
3590 Disc_Exp := First (Expressions (Aggr));
3592 elsif Present (Component_Associations (Aggr)) then
3593 Assoc := First (Component_Associations (Aggr));
3595 if Present (Assoc) then
3596 Disc_Exp := Expression (Assoc);
3605 while Present (Discrim) and then Present (Disc_Exp) loop
3606 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
3607 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
3610 Next_Discriminant (Discrim);
3612 if Present (Discrim) then
3613 if Present (Assoc) then
3615 Disc_Exp := Expression (Assoc);
3617 elsif Present (Next (Disc_Exp)) then
3621 Assoc := First (Component_Associations (Aggr));
3623 if Present (Assoc) then
3624 Disc_Exp := Expression (Assoc);
3634 -- For a subtype mark or subtype indication, freeze the subtype
3637 Freeze_Expression (E);
3639 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
3641 ("initialization required for access-to-constant allocator", N);
3644 -- A special accessibility check is needed for allocators that
3645 -- constrain access discriminants. The level of the type of the
3646 -- expression used to constrain an access discriminant cannot be
3647 -- deeper than the type of the allocator (in constrast to access
3648 -- parameters, where the level of the actual can be arbitrary).
3649 -- We can't use Valid_Conversion to perform this check because
3650 -- in general the type of the allocator is unrelated to the type
3651 -- of the access discriminant.
3653 if Nkind (Original_Node (E)) = N_Subtype_Indication
3654 and then (Ekind (Typ) /= E_Anonymous_Access_Type
3655 or else Is_Local_Anonymous_Access (Typ))
3657 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
3659 if Has_Discriminants (Subtyp) then
3660 Discrim := First_Discriminant (Base_Type (Subtyp));
3661 Constr := First (Constraints (Constraint (Original_Node (E))));
3662 while Present (Discrim) and then Present (Constr) loop
3663 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
3664 if Nkind (Constr) = N_Discriminant_Association then
3665 Disc_Exp := Original_Node (Expression (Constr));
3667 Disc_Exp := Original_Node (Constr);
3670 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
3673 Next_Discriminant (Discrim);
3680 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
3681 -- check that the level of the type of the created object is not deeper
3682 -- than the level of the allocator's access type, since extensions can
3683 -- now occur at deeper levels than their ancestor types. This is a
3684 -- static accessibility level check; a run-time check is also needed in
3685 -- the case of an initialized allocator with a class-wide argument (see
3686 -- Expand_Allocator_Expression).
3688 if Ada_Version >= Ada_05
3689 and then Is_Class_Wide_Type (Designated_Type (Typ))
3692 Exp_Typ : Entity_Id;
3695 if Nkind (E) = N_Qualified_Expression then
3696 Exp_Typ := Etype (E);
3697 elsif Nkind (E) = N_Subtype_Indication then
3698 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
3700 Exp_Typ := Entity (E);
3703 if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
3704 if In_Instance_Body then
3705 Error_Msg_N ("?type in allocator has deeper level than" &
3706 " designated class-wide type", E);
3707 Error_Msg_N ("\?Program_Error will be raised at run time",
3710 Make_Raise_Program_Error (Sloc (N),
3711 Reason => PE_Accessibility_Check_Failed));
3714 -- Do not apply Ada 2005 accessibility checks on a class-wide
3715 -- allocator if the type given in the allocator is a formal
3716 -- type. A run-time check will be performed in the instance.
3718 elsif not Is_Generic_Type (Exp_Typ) then
3719 Error_Msg_N ("type in allocator has deeper level than" &
3720 " designated class-wide type", E);
3726 -- Check for allocation from an empty storage pool
3728 if No_Pool_Assigned (Typ) then
3730 Loc : constant Source_Ptr := Sloc (N);
3732 Error_Msg_N ("?allocation from empty storage pool!", N);
3733 Error_Msg_N ("\?Storage_Error will be raised at run time!", N);
3735 Make_Raise_Storage_Error (Loc,
3736 Reason => SE_Empty_Storage_Pool));
3739 -- If the context is an unchecked conversion, as may happen within
3740 -- an inlined subprogram, the allocator is being resolved with its
3741 -- own anonymous type. In that case, if the target type has a specific
3742 -- storage pool, it must be inherited explicitly by the allocator type.
3744 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
3745 and then No (Associated_Storage_Pool (Typ))
3747 Set_Associated_Storage_Pool
3748 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
3751 -- An erroneous allocator may be rewritten as a raise Program_Error
3754 if Nkind (N) = N_Allocator then
3756 -- An anonymous access discriminant is the definition of a
3759 if Ekind (Typ) = E_Anonymous_Access_Type
3760 and then Nkind (Associated_Node_For_Itype (Typ)) =
3761 N_Discriminant_Specification
3763 -- Avoid marking an allocator as a dynamic coextension if it is
3764 -- within a static construct.
3766 if not Is_Static_Coextension (N) then
3767 Set_Is_Dynamic_Coextension (N);
3770 -- Cleanup for potential static coextensions
3773 Set_Is_Dynamic_Coextension (N, False);
3774 Set_Is_Static_Coextension (N, False);
3777 -- There is no need to propagate any nested coextensions if they
3778 -- are marked as static since they will be rewritten on the spot.
3780 if not Is_Static_Coextension (N) then
3781 Propagate_Coextensions (N);
3784 end Resolve_Allocator;
3786 ---------------------------
3787 -- Resolve_Arithmetic_Op --
3788 ---------------------------
3790 -- Used for resolving all arithmetic operators except exponentiation
3792 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
3793 L : constant Node_Id := Left_Opnd (N);
3794 R : constant Node_Id := Right_Opnd (N);
3795 TL : constant Entity_Id := Base_Type (Etype (L));
3796 TR : constant Entity_Id := Base_Type (Etype (R));
3800 B_Typ : constant Entity_Id := Base_Type (Typ);
3801 -- We do the resolution using the base type, because intermediate values
3802 -- in expressions always are of the base type, not a subtype of it.
3804 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
3805 -- Returns True if N is in a context that expects "any real type"
3807 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
3808 -- Return True iff given type is Integer or universal real/integer
3810 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
3811 -- Choose type of integer literal in fixed-point operation to conform
3812 -- to available fixed-point type. T is the type of the other operand,
3813 -- which is needed to determine the expected type of N.
3815 procedure Set_Operand_Type (N : Node_Id);
3816 -- Set operand type to T if universal
3818 -------------------------------
3819 -- Expected_Type_Is_Any_Real --
3820 -------------------------------
3822 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
3824 -- N is the expression after "delta" in a fixed_point_definition;
3827 return Nkind (Parent (N)) = N_Ordinary_Fixed_Point_Definition
3828 or else Nkind (Parent (N)) = N_Decimal_Fixed_Point_Definition
3830 -- N is one of the bounds in a real_range_specification;
3833 or else Nkind (Parent (N)) = N_Real_Range_Specification
3835 -- N is the expression of a delta_constraint;
3838 or else Nkind (Parent (N)) = N_Delta_Constraint;
3839 end Expected_Type_Is_Any_Real;
3841 -----------------------------
3842 -- Is_Integer_Or_Universal --
3843 -----------------------------
3845 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
3847 Index : Interp_Index;
3851 if not Is_Overloaded (N) then
3853 return Base_Type (T) = Base_Type (Standard_Integer)
3854 or else T = Universal_Integer
3855 or else T = Universal_Real;
3857 Get_First_Interp (N, Index, It);
3858 while Present (It.Typ) loop
3859 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
3860 or else It.Typ = Universal_Integer
3861 or else It.Typ = Universal_Real
3866 Get_Next_Interp (Index, It);
3871 end Is_Integer_Or_Universal;
3873 ----------------------------
3874 -- Set_Mixed_Mode_Operand --
3875 ----------------------------
3877 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
3878 Index : Interp_Index;
3882 if Universal_Interpretation (N) = Universal_Integer then
3884 -- A universal integer literal is resolved as standard integer
3885 -- except in the case of a fixed-point result, where we leave it
3886 -- as universal (to be handled by Exp_Fixd later on)
3888 if Is_Fixed_Point_Type (T) then
3889 Resolve (N, Universal_Integer);
3891 Resolve (N, Standard_Integer);
3894 elsif Universal_Interpretation (N) = Universal_Real
3895 and then (T = Base_Type (Standard_Integer)
3896 or else T = Universal_Integer
3897 or else T = Universal_Real)
3899 -- A universal real can appear in a fixed-type context. We resolve
3900 -- the literal with that context, even though this might raise an
3901 -- exception prematurely (the other operand may be zero).
3905 elsif Etype (N) = Base_Type (Standard_Integer)
3906 and then T = Universal_Real
3907 and then Is_Overloaded (N)
3909 -- Integer arg in mixed-mode operation. Resolve with universal
3910 -- type, in case preference rule must be applied.
3912 Resolve (N, Universal_Integer);
3915 and then B_Typ /= Universal_Fixed
3917 -- Not a mixed-mode operation, resolve with context
3921 elsif Etype (N) = Any_Fixed then
3923 -- N may itself be a mixed-mode operation, so use context type
3927 elsif Is_Fixed_Point_Type (T)
3928 and then B_Typ = Universal_Fixed
3929 and then Is_Overloaded (N)
3931 -- Must be (fixed * fixed) operation, operand must have one
3932 -- compatible interpretation.
3934 Resolve (N, Any_Fixed);
3936 elsif Is_Fixed_Point_Type (B_Typ)
3937 and then (T = Universal_Real
3938 or else Is_Fixed_Point_Type (T))
3939 and then Is_Overloaded (N)
3941 -- C * F(X) in a fixed context, where C is a real literal or a
3942 -- fixed-point expression. F must have either a fixed type
3943 -- interpretation or an integer interpretation, but not both.
3945 Get_First_Interp (N, Index, It);
3946 while Present (It.Typ) loop
3947 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
3949 if Analyzed (N) then
3950 Error_Msg_N ("ambiguous operand in fixed operation", N);
3952 Resolve (N, Standard_Integer);
3955 elsif Is_Fixed_Point_Type (It.Typ) then
3957 if Analyzed (N) then
3958 Error_Msg_N ("ambiguous operand in fixed operation", N);
3960 Resolve (N, It.Typ);
3964 Get_Next_Interp (Index, It);
3967 -- Reanalyze the literal with the fixed type of the context. If
3968 -- context is Universal_Fixed, we are within a conversion, leave
3969 -- the literal as a universal real because there is no usable
3970 -- fixed type, and the target of the conversion plays no role in
3984 if B_Typ = Universal_Fixed
3985 and then Nkind (Op2) = N_Real_Literal
3987 T2 := Universal_Real;
3992 Set_Analyzed (Op2, False);
3999 end Set_Mixed_Mode_Operand;
4001 ----------------------
4002 -- Set_Operand_Type --
4003 ----------------------
4005 procedure Set_Operand_Type (N : Node_Id) is
4007 if Etype (N) = Universal_Integer
4008 or else Etype (N) = Universal_Real
4012 end Set_Operand_Type;
4014 -- Start of processing for Resolve_Arithmetic_Op
4017 if Comes_From_Source (N)
4018 and then Ekind (Entity (N)) = E_Function
4019 and then Is_Imported (Entity (N))
4020 and then Is_Intrinsic_Subprogram (Entity (N))
4022 Resolve_Intrinsic_Operator (N, Typ);
4025 -- Special-case for mixed-mode universal expressions or fixed point
4026 -- type operation: each argument is resolved separately. The same
4027 -- treatment is required if one of the operands of a fixed point
4028 -- operation is universal real, since in this case we don't do a
4029 -- conversion to a specific fixed-point type (instead the expander
4030 -- takes care of the case).
4032 elsif (B_Typ = Universal_Integer
4033 or else B_Typ = Universal_Real)
4034 and then Present (Universal_Interpretation (L))
4035 and then Present (Universal_Interpretation (R))
4037 Resolve (L, Universal_Interpretation (L));
4038 Resolve (R, Universal_Interpretation (R));
4039 Set_Etype (N, B_Typ);
4041 elsif (B_Typ = Universal_Real
4042 or else Etype (N) = Universal_Fixed
4043 or else (Etype (N) = Any_Fixed
4044 and then Is_Fixed_Point_Type (B_Typ))
4045 or else (Is_Fixed_Point_Type (B_Typ)
4046 and then (Is_Integer_Or_Universal (L)
4048 Is_Integer_Or_Universal (R))))
4049 and then (Nkind (N) = N_Op_Multiply or else
4050 Nkind (N) = N_Op_Divide)
4052 if TL = Universal_Integer or else TR = Universal_Integer then
4053 Check_For_Visible_Operator (N, B_Typ);
4056 -- If context is a fixed type and one operand is integer, the
4057 -- other is resolved with the type of the context.
4059 if Is_Fixed_Point_Type (B_Typ)
4060 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4061 or else TL = Universal_Integer)
4066 elsif Is_Fixed_Point_Type (B_Typ)
4067 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4068 or else TR = Universal_Integer)
4074 Set_Mixed_Mode_Operand (L, TR);
4075 Set_Mixed_Mode_Operand (R, TL);
4078 -- Check the rule in RM05-4.5.5(19.1/2) disallowing the
4079 -- universal_fixed multiplying operators from being used when the
4080 -- expected type is also universal_fixed. Note that B_Typ will be
4081 -- Universal_Fixed in some cases where the expected type is actually
4082 -- Any_Real; Expected_Type_Is_Any_Real takes care of that case.
4084 if Etype (N) = Universal_Fixed
4085 or else Etype (N) = Any_Fixed
4087 if B_Typ = Universal_Fixed
4088 and then not Expected_Type_Is_Any_Real (N)
4089 and then Nkind (Parent (N)) /= N_Type_Conversion
4090 and then Nkind (Parent (N)) /= N_Unchecked_Type_Conversion
4093 ("type cannot be determined from context!", N);
4095 ("\explicit conversion to result type required", N);
4097 Set_Etype (L, Any_Type);
4098 Set_Etype (R, Any_Type);
4101 if Ada_Version = Ada_83
4102 and then Etype (N) = Universal_Fixed
4103 and then Nkind (Parent (N)) /= N_Type_Conversion
4104 and then Nkind (Parent (N)) /= N_Unchecked_Type_Conversion
4107 ("(Ada 83) fixed-point operation " &
4108 "needs explicit conversion",
4112 -- The expected type is "any real type" in contexts like
4113 -- type T is delta <universal_fixed-expression> ...
4114 -- in which case we need to set the type to Universal_Real
4115 -- so that static expression evaluation will work properly.
4117 if Expected_Type_Is_Any_Real (N) then
4118 Set_Etype (N, Universal_Real);
4120 Set_Etype (N, B_Typ);
4124 elsif Is_Fixed_Point_Type (B_Typ)
4125 and then (Is_Integer_Or_Universal (L)
4126 or else Nkind (L) = N_Real_Literal
4127 or else Nkind (R) = N_Real_Literal
4129 Is_Integer_Or_Universal (R))
4131 Set_Etype (N, B_Typ);
4133 elsif Etype (N) = Any_Fixed then
4135 -- If no previous errors, this is only possible if one operand
4136 -- is overloaded and the context is universal. Resolve as such.
4138 Set_Etype (N, B_Typ);
4142 if (TL = Universal_Integer or else TL = Universal_Real)
4143 and then (TR = Universal_Integer or else TR = Universal_Real)
4145 Check_For_Visible_Operator (N, B_Typ);
4148 -- If the context is Universal_Fixed and the operands are also
4149 -- universal fixed, this is an error, unless there is only one
4150 -- applicable fixed_point type (usually duration).
4152 if B_Typ = Universal_Fixed
4153 and then Etype (L) = Universal_Fixed
4155 T := Unique_Fixed_Point_Type (N);
4157 if T = Any_Type then
4170 -- If one of the arguments was resolved to a non-universal type.
4171 -- label the result of the operation itself with the same type.
4172 -- Do the same for the universal argument, if any.
4174 T := Intersect_Types (L, R);
4175 Set_Etype (N, Base_Type (T));
4176 Set_Operand_Type (L);
4177 Set_Operand_Type (R);
4180 Generate_Operator_Reference (N, Typ);
4181 Eval_Arithmetic_Op (N);
4183 -- Set overflow and division checking bit. Much cleverer code needed
4184 -- here eventually and perhaps the Resolve routines should be separated
4185 -- for the various arithmetic operations, since they will need
4186 -- different processing. ???
4188 if Nkind (N) in N_Op then
4189 if not Overflow_Checks_Suppressed (Etype (N)) then
4190 Enable_Overflow_Check (N);
4193 -- Give warning if explicit division by zero
4195 if (Nkind (N) = N_Op_Divide
4196 or else Nkind (N) = N_Op_Rem
4197 or else Nkind (N) = N_Op_Mod)
4198 and then not Division_Checks_Suppressed (Etype (N))
4200 Rop := Right_Opnd (N);
4202 if Compile_Time_Known_Value (Rop)
4203 and then ((Is_Integer_Type (Etype (Rop))
4204 and then Expr_Value (Rop) = Uint_0)
4206 (Is_Real_Type (Etype (Rop))
4207 and then Expr_Value_R (Rop) = Ureal_0))
4209 -- Specialize the warning message according to the operation
4213 Apply_Compile_Time_Constraint_Error
4214 (N, "division by zero?", CE_Divide_By_Zero,
4215 Loc => Sloc (Right_Opnd (N)));
4218 Apply_Compile_Time_Constraint_Error
4219 (N, "rem with zero divisor?", CE_Divide_By_Zero,
4220 Loc => Sloc (Right_Opnd (N)));
4223 Apply_Compile_Time_Constraint_Error
4224 (N, "mod with zero divisor?", CE_Divide_By_Zero,
4225 Loc => Sloc (Right_Opnd (N)));
4227 -- Division by zero can only happen with division, rem,
4228 -- and mod operations.
4231 raise Program_Error;
4234 -- Otherwise just set the flag to check at run time
4237 Activate_Division_Check (N);
4242 Check_Unset_Reference (L);
4243 Check_Unset_Reference (R);
4244 end Resolve_Arithmetic_Op;
4250 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4251 Loc : constant Source_Ptr := Sloc (N);
4252 Subp : constant Node_Id := Name (N);
4261 -- The context imposes a unique interpretation with type Typ on a
4262 -- procedure or function call. Find the entity of the subprogram that
4263 -- yields the expected type, and propagate the corresponding formal
4264 -- constraints on the actuals. The caller has established that an
4265 -- interpretation exists, and emitted an error if not unique.
4267 -- First deal with the case of a call to an access-to-subprogram,
4268 -- dereference made explicit in Analyze_Call.
4270 if Ekind (Etype (Subp)) = E_Subprogram_Type then
4271 if not Is_Overloaded (Subp) then
4272 Nam := Etype (Subp);
4275 -- Find the interpretation whose type (a subprogram type) has a
4276 -- return type that is compatible with the context. Analysis of
4277 -- the node has established that one exists.
4281 Get_First_Interp (Subp, I, It);
4282 while Present (It.Typ) loop
4283 if Covers (Typ, Etype (It.Typ)) then
4288 Get_Next_Interp (I, It);
4292 raise Program_Error;
4296 -- If the prefix is not an entity, then resolve it
4298 if not Is_Entity_Name (Subp) then
4299 Resolve (Subp, Nam);
4302 -- For an indirect call, we always invalidate checks, since we do not
4303 -- know whether the subprogram is local or global. Yes we could do
4304 -- better here, e.g. by knowing that there are no local subprograms,
4305 -- but it does not seem worth the effort. Similarly, we kill all
4306 -- knowledge of current constant values.
4308 Kill_Current_Values;
4310 -- If this is a procedure call which is really an entry call, do
4311 -- the conversion of the procedure call to an entry call. Protected
4312 -- operations use the same circuitry because the name in the call
4313 -- can be an arbitrary expression with special resolution rules.
4315 elsif Nkind (Subp) = N_Selected_Component
4316 or else Nkind (Subp) = N_Indexed_Component
4317 or else (Is_Entity_Name (Subp)
4318 and then Ekind (Entity (Subp)) = E_Entry)
4320 Resolve_Entry_Call (N, Typ);
4321 Check_Elab_Call (N);
4323 -- Kill checks and constant values, as above for indirect case
4324 -- Who knows what happens when another task is activated?
4326 Kill_Current_Values;
4329 -- Normal subprogram call with name established in Resolve
4331 elsif not (Is_Type (Entity (Subp))) then
4332 Nam := Entity (Subp);
4333 Set_Entity_With_Style_Check (Subp, Nam);
4334 Generate_Reference (Nam, Subp);
4336 -- Otherwise we must have the case of an overloaded call
4339 pragma Assert (Is_Overloaded (Subp));
4340 Nam := Empty; -- We know that it will be assigned in loop below
4342 Get_First_Interp (Subp, I, It);
4343 while Present (It.Typ) loop
4344 if Covers (Typ, It.Typ) then
4346 Set_Entity_With_Style_Check (Subp, Nam);
4347 Generate_Reference (Nam, Subp);
4351 Get_Next_Interp (I, It);
4355 -- Check that a call to Current_Task does not occur in an entry body
4357 if Is_RTE (Nam, RE_Current_Task) then
4367 if Nkind (P) = N_Entry_Body
4368 or else (Nkind (P) = N_Subprogram_Body
4369 and then Is_Entry_Barrier_Function (P))
4373 ("?& should not be used in entry body (RM C.7(17))",
4376 ("\Program_Error will be raised at run time?", N, Nam);
4378 Make_Raise_Program_Error (Loc,
4379 Reason => PE_Current_Task_In_Entry_Body));
4380 Set_Etype (N, Rtype);
4387 -- Check that a procedure call does not occur in the context of the
4388 -- entry call statement of a conditional or timed entry call. Note that
4389 -- the case of a call to a subprogram renaming of an entry will also be
4390 -- rejected. The test for N not being an N_Entry_Call_Statement is
4391 -- defensive, covering the possibility that the processing of entry
4392 -- calls might reach this point due to later modifications of the code
4395 if Nkind (Parent (N)) = N_Entry_Call_Alternative
4396 and then Nkind (N) /= N_Entry_Call_Statement
4397 and then Entry_Call_Statement (Parent (N)) = N
4399 if Ada_Version < Ada_05 then
4400 Error_Msg_N ("entry call required in select statement", N);
4402 -- Ada 2005 (AI-345): If a procedure_call_statement is used
4403 -- for a procedure_or_entry_call, the procedure_name or pro-
4404 -- cedure_prefix of the procedure_call_statement shall denote
4405 -- an entry renamed by a procedure, or (a view of) a primitive
4406 -- subprogram of a limited interface whose first parameter is
4407 -- a controlling parameter.
4409 elsif Nkind (N) = N_Procedure_Call_Statement
4410 and then not Is_Renamed_Entry (Nam)
4411 and then not Is_Controlling_Limited_Procedure (Nam)
4414 ("entry call or dispatching primitive of interface required", N);
4418 -- Check that this is not a call to a protected procedure or
4419 -- entry from within a protected function.
4421 if Ekind (Current_Scope) = E_Function
4422 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
4423 and then Ekind (Nam) /= E_Function
4424 and then Scope (Nam) = Scope (Current_Scope)
4426 Error_Msg_N ("within protected function, protected " &
4427 "object is constant", N);
4428 Error_Msg_N ("\cannot call operation that may modify it", N);
4431 -- Freeze the subprogram name if not in default expression. Note that we
4432 -- freeze procedure calls as well as function calls. Procedure calls are
4433 -- not frozen according to the rules (RM 13.14(14)) because it is
4434 -- impossible to have a procedure call to a non-frozen procedure in pure
4435 -- Ada, but in the code that we generate in the expander, this rule
4436 -- needs extending because we can generate procedure calls that need
4439 if Is_Entity_Name (Subp) and then not In_Default_Expression then
4440 Freeze_Expression (Subp);
4443 -- For a predefined operator, the type of the result is the type imposed
4444 -- by context, except for a predefined operation on universal fixed.
4445 -- Otherwise The type of the call is the type returned by the subprogram
4448 if Is_Predefined_Op (Nam) then
4449 if Etype (N) /= Universal_Fixed then
4453 -- If the subprogram returns an array type, and the context requires the
4454 -- component type of that array type, the node is really an indexing of
4455 -- the parameterless call. Resolve as such. A pathological case occurs
4456 -- when the type of the component is an access to the array type. In
4457 -- this case the call is truly ambiguous.
4459 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
4461 ((Is_Array_Type (Etype (Nam))
4462 and then Covers (Typ, Component_Type (Etype (Nam))))
4463 or else (Is_Access_Type (Etype (Nam))
4464 and then Is_Array_Type (Designated_Type (Etype (Nam)))
4467 Component_Type (Designated_Type (Etype (Nam))))))
4470 Index_Node : Node_Id;
4472 Ret_Type : constant Entity_Id := Etype (Nam);
4475 if Is_Access_Type (Ret_Type)
4476 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
4479 ("cannot disambiguate function call and indexing", N);
4481 New_Subp := Relocate_Node (Subp);
4482 Set_Entity (Subp, Nam);
4484 if Component_Type (Ret_Type) /= Any_Type then
4485 if Needs_No_Actuals (Nam) then
4487 -- Indexed call to a parameterless function
4490 Make_Indexed_Component (Loc,
4492 Make_Function_Call (Loc,
4494 Expressions => Parameter_Associations (N));
4496 -- An Ada 2005 prefixed call to a primitive operation
4497 -- whose first parameter is the prefix. This prefix was
4498 -- prepended to the parameter list, which is actually a
4499 -- list of indices. Remove the prefix in order to build
4500 -- the proper indexed component.
4503 Make_Indexed_Component (Loc,
4505 Make_Function_Call (Loc,
4507 Parameter_Associations =>
4509 (Remove_Head (Parameter_Associations (N)))),
4510 Expressions => Parameter_Associations (N));
4513 -- Since we are correcting a node classification error made
4514 -- by the parser, we call Replace rather than Rewrite.
4516 Replace (N, Index_Node);
4517 Set_Etype (Prefix (N), Ret_Type);
4519 Resolve_Indexed_Component (N, Typ);
4520 Check_Elab_Call (Prefix (N));
4528 Set_Etype (N, Etype (Nam));
4531 -- In the case where the call is to an overloaded subprogram, Analyze
4532 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
4533 -- such a case Normalize_Actuals needs to be called once more to order
4534 -- the actuals correctly. Otherwise the call will have the ordering
4535 -- given by the last overloaded subprogram whether this is the correct
4536 -- one being called or not.
4538 if Is_Overloaded (Subp) then
4539 Normalize_Actuals (N, Nam, False, Norm_OK);
4540 pragma Assert (Norm_OK);
4543 -- In any case, call is fully resolved now. Reset Overload flag, to
4544 -- prevent subsequent overload resolution if node is analyzed again
4546 Set_Is_Overloaded (Subp, False);
4547 Set_Is_Overloaded (N, False);
4549 -- If we are calling the current subprogram from immediately within its
4550 -- body, then that is the case where we can sometimes detect cases of
4551 -- infinite recursion statically. Do not try this in case restriction
4552 -- No_Recursion is in effect anyway, and do it only for source calls.
4554 if Comes_From_Source (N) then
4555 Scop := Current_Scope;
4558 and then not Restriction_Active (No_Recursion)
4559 and then Check_Infinite_Recursion (N)
4561 -- Here we detected and flagged an infinite recursion, so we do
4562 -- not need to test the case below for further warnings.
4566 -- If call is to immediately containing subprogram, then check for
4567 -- the case of a possible run-time detectable infinite recursion.
4570 Scope_Loop : while Scop /= Standard_Standard loop
4573 -- Although in general case, recursion is not statically
4574 -- checkable, the case of calling an immediately containing
4575 -- subprogram is easy to catch.
4577 Check_Restriction (No_Recursion, N);
4579 -- If the recursive call is to a parameterless subprogram,
4580 -- then even if we can't statically detect infinite
4581 -- recursion, this is pretty suspicious, and we output a
4582 -- warning. Furthermore, we will try later to detect some
4583 -- cases here at run time by expanding checking code (see
4584 -- Detect_Infinite_Recursion in package Exp_Ch6).
4586 -- If the recursive call is within a handler, do not emit a
4587 -- warning, because this is a common idiom: loop until input
4588 -- is correct, catch illegal input in handler and restart.
4590 if No (First_Formal (Nam))
4591 and then Etype (Nam) = Standard_Void_Type
4592 and then not Error_Posted (N)
4593 and then Nkind (Parent (N)) /= N_Exception_Handler
4595 -- For the case of a procedure call. We give the message
4596 -- only if the call is the first statement in a sequence
4597 -- of statements, or if all previous statements are
4598 -- simple assignments. This is simply a heuristic to
4599 -- decrease false positives, without losing too many good
4600 -- warnings. The idea is that these previous statements
4601 -- may affect global variables the procedure depends on.
4603 if Nkind (N) = N_Procedure_Call_Statement
4604 and then Is_List_Member (N)
4610 while Present (P) loop
4611 if Nkind (P) /= N_Assignment_Statement then
4620 -- Do not give warning if we are in a conditional context
4623 K : constant Node_Kind := Nkind (Parent (N));
4625 if (K = N_Loop_Statement
4626 and then Present (Iteration_Scheme (Parent (N))))
4627 or else K = N_If_Statement
4628 or else K = N_Elsif_Part
4629 or else K = N_Case_Statement_Alternative
4635 -- Here warning is to be issued
4637 Set_Has_Recursive_Call (Nam);
4639 ("?possible infinite recursion!", N);
4641 ("\?Storage_Error may be raised at run time!", N);
4647 Scop := Scope (Scop);
4648 end loop Scope_Loop;
4652 -- If subprogram name is a predefined operator, it was given in
4653 -- functional notation. Replace call node with operator node, so
4654 -- that actuals can be resolved appropriately.
4656 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
4657 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
4660 elsif Present (Alias (Nam))
4661 and then Is_Predefined_Op (Alias (Nam))
4663 Resolve_Actuals (N, Nam);
4664 Make_Call_Into_Operator (N, Typ, Alias (Nam));
4668 -- Create a transient scope if the resulting type requires it
4670 -- There are 4 notable exceptions: in init procs, the transient scope
4671 -- overhead is not needed and even incorrect due to the actual expansion
4672 -- of adjust calls; the second case is enumeration literal pseudo calls;
4673 -- the third case is intrinsic subprograms (Unchecked_Conversion and
4674 -- source information functions) that do not use the secondary stack
4675 -- even though the return type is unconstrained; the fourth case is a
4676 -- call to a build-in-place function, since such functions may allocate
4677 -- their result directly in a target object, and cases where the result
4678 -- does get allocated in the secondary stack are checked for within the
4679 -- specialized Exp_Ch6 procedures for expanding build-in-place calls.
4681 -- If this is an initialization call for a type whose initialization
4682 -- uses the secondary stack, we also need to create a transient scope
4683 -- for it, precisely because we will not do it within the init proc
4686 -- If the subprogram is marked Inlined_Always, then even if it returns
4687 -- an unconstrained type the call does not require use of the secondary
4691 and then Present (First_Rep_Item (Nam))
4692 and then Nkind (First_Rep_Item (Nam)) = N_Pragma
4693 and then Chars (First_Rep_Item (Nam)) = Name_Inline_Always
4697 elsif Expander_Active
4698 and then Is_Type (Etype (Nam))
4699 and then Requires_Transient_Scope (Etype (Nam))
4700 and then not Is_Build_In_Place_Function (Nam)
4701 and then Ekind (Nam) /= E_Enumeration_Literal
4702 and then not Within_Init_Proc
4703 and then not Is_Intrinsic_Subprogram (Nam)
4705 Establish_Transient_Scope (N, Sec_Stack => True);
4707 -- If the call appears within the bounds of a loop, it will
4708 -- be rewritten and reanalyzed, nothing left to do here.
4710 if Nkind (N) /= N_Function_Call then
4714 elsif Is_Init_Proc (Nam)
4715 and then not Within_Init_Proc
4717 Check_Initialization_Call (N, Nam);
4720 -- A protected function cannot be called within the definition of the
4721 -- enclosing protected type.
4723 if Is_Protected_Type (Scope (Nam))
4724 and then In_Open_Scopes (Scope (Nam))
4725 and then not Has_Completion (Scope (Nam))
4728 ("& cannot be called before end of protected definition", N, Nam);
4731 -- Propagate interpretation to actuals, and add default expressions
4734 if Present (First_Formal (Nam)) then
4735 Resolve_Actuals (N, Nam);
4737 -- Overloaded literals are rewritten as function calls, for
4738 -- purpose of resolution. After resolution, we can replace
4739 -- the call with the literal itself.
4741 elsif Ekind (Nam) = E_Enumeration_Literal then
4742 Copy_Node (Subp, N);
4743 Resolve_Entity_Name (N, Typ);
4745 -- Avoid validation, since it is a static function call
4750 -- If the subprogram is not global, then kill all saved values and
4751 -- checks. This is a bit conservative, since in many cases we could do
4752 -- better, but it is not worth the effort. Similarly, we kill constant
4753 -- values. However we do not need to do this for internal entities
4754 -- (unless they are inherited user-defined subprograms), since they
4755 -- are not in the business of molesting local values.
4757 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
4758 -- kill all checks and values for calls to global subprograms. This
4759 -- takes care of the case where an access to a local subprogram is
4760 -- taken, and could be passed directly or indirectly and then called
4761 -- from almost any context.
4763 -- Note: we do not do this step till after resolving the actuals. That
4764 -- way we still take advantage of the current value information while
4765 -- scanning the actuals.
4767 if (not Is_Library_Level_Entity (Nam)
4768 or else Suppress_Value_Tracking_On_Call (Current_Scope))
4769 and then (Comes_From_Source (Nam)
4770 or else (Present (Alias (Nam))
4771 and then Comes_From_Source (Alias (Nam))))
4773 Kill_Current_Values;
4776 -- If the subprogram is a primitive operation, check whether or not
4777 -- it is a correct dispatching call.
4779 if Is_Overloadable (Nam)
4780 and then Is_Dispatching_Operation (Nam)
4782 Check_Dispatching_Call (N);
4784 elsif Ekind (Nam) /= E_Subprogram_Type
4785 and then Is_Abstract_Subprogram (Nam)
4786 and then not In_Instance
4788 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
4791 if Is_Intrinsic_Subprogram (Nam) then
4792 Check_Intrinsic_Call (N);
4796 Check_Elab_Call (N);
4799 -------------------------------
4800 -- Resolve_Character_Literal --
4801 -------------------------------
4803 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
4804 B_Typ : constant Entity_Id := Base_Type (Typ);
4808 -- Verify that the character does belong to the type of the context
4810 Set_Etype (N, B_Typ);
4811 Eval_Character_Literal (N);
4813 -- Wide_Wide_Character literals must always be defined, since the set
4814 -- of wide wide character literals is complete, i.e. if a character
4815 -- literal is accepted by the parser, then it is OK for wide wide
4816 -- character (out of range character literals are rejected).
4818 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
4821 -- Always accept character literal for type Any_Character, which
4822 -- occurs in error situations and in comparisons of literals, both
4823 -- of which should accept all literals.
4825 elsif B_Typ = Any_Character then
4828 -- For Standard.Character or a type derived from it, check that
4829 -- the literal is in range
4831 elsif Root_Type (B_Typ) = Standard_Character then
4832 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
4836 -- For Standard.Wide_Character or a type derived from it, check
4837 -- that the literal is in range
4839 elsif Root_Type (B_Typ) = Standard_Wide_Character then
4840 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
4844 -- For Standard.Wide_Wide_Character or a type derived from it, we
4845 -- know the literal is in range, since the parser checked!
4847 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
4850 -- If the entity is already set, this has already been resolved in
4851 -- a generic context, or comes from expansion. Nothing else to do.
4853 elsif Present (Entity (N)) then
4856 -- Otherwise we have a user defined character type, and we can use
4857 -- the standard visibility mechanisms to locate the referenced entity
4860 C := Current_Entity (N);
4861 while Present (C) loop
4862 if Etype (C) = B_Typ then
4863 Set_Entity_With_Style_Check (N, C);
4864 Generate_Reference (C, N);
4872 -- If we fall through, then the literal does not match any of the
4873 -- entries of the enumeration type. This isn't just a constraint
4874 -- error situation, it is an illegality (see RM 4.2).
4877 ("character not defined for }", N, First_Subtype (B_Typ));
4878 end Resolve_Character_Literal;
4880 ---------------------------
4881 -- Resolve_Comparison_Op --
4882 ---------------------------
4884 -- Context requires a boolean type, and plays no role in resolution.
4885 -- Processing identical to that for equality operators. The result
4886 -- type is the base type, which matters when pathological subtypes of
4887 -- booleans with limited ranges are used.
4889 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
4890 L : constant Node_Id := Left_Opnd (N);
4891 R : constant Node_Id := Right_Opnd (N);
4895 -- If this is an intrinsic operation which is not predefined, use
4896 -- the types of its declared arguments to resolve the possibly
4897 -- overloaded operands. Otherwise the operands are unambiguous and
4898 -- specify the expected type.
4900 if Scope (Entity (N)) /= Standard_Standard then
4901 T := Etype (First_Entity (Entity (N)));
4904 T := Find_Unique_Type (L, R);
4906 if T = Any_Fixed then
4907 T := Unique_Fixed_Point_Type (L);
4911 Set_Etype (N, Base_Type (Typ));
4912 Generate_Reference (T, N, ' ');
4914 if T /= Any_Type then
4916 or else T = Any_Composite
4917 or else T = Any_Character
4919 if T = Any_Character then
4920 Ambiguous_Character (L);
4922 Error_Msg_N ("ambiguous operands for comparison", N);
4925 Set_Etype (N, Any_Type);
4931 Check_Unset_Reference (L);
4932 Check_Unset_Reference (R);
4933 Generate_Operator_Reference (N, T);
4934 Eval_Relational_Op (N);
4937 end Resolve_Comparison_Op;
4939 ------------------------------------
4940 -- Resolve_Conditional_Expression --
4941 ------------------------------------
4943 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
4944 Condition : constant Node_Id := First (Expressions (N));
4945 Then_Expr : constant Node_Id := Next (Condition);
4946 Else_Expr : constant Node_Id := Next (Then_Expr);
4949 Resolve (Condition, Standard_Boolean);
4950 Resolve (Then_Expr, Typ);
4951 Resolve (Else_Expr, Typ);
4954 Eval_Conditional_Expression (N);
4955 end Resolve_Conditional_Expression;
4957 -----------------------------------------
4958 -- Resolve_Discrete_Subtype_Indication --
4959 -----------------------------------------
4961 procedure Resolve_Discrete_Subtype_Indication
4969 Analyze (Subtype_Mark (N));
4970 S := Entity (Subtype_Mark (N));
4972 if Nkind (Constraint (N)) /= N_Range_Constraint then
4973 Error_Msg_N ("expect range constraint for discrete type", N);
4974 Set_Etype (N, Any_Type);
4977 R := Range_Expression (Constraint (N));
4985 if Base_Type (S) /= Base_Type (Typ) then
4987 ("expect subtype of }", N, First_Subtype (Typ));
4989 -- Rewrite the constraint as a range of Typ
4990 -- to allow compilation to proceed further.
4993 Rewrite (Low_Bound (R),
4994 Make_Attribute_Reference (Sloc (Low_Bound (R)),
4995 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
4996 Attribute_Name => Name_First));
4997 Rewrite (High_Bound (R),
4998 Make_Attribute_Reference (Sloc (High_Bound (R)),
4999 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
5000 Attribute_Name => Name_First));
5004 Set_Etype (N, Etype (R));
5006 -- Additionally, we must check that the bounds are compatible
5007 -- with the given subtype, which might be different from the
5008 -- type of the context.
5010 Apply_Range_Check (R, S);
5012 -- ??? If the above check statically detects a Constraint_Error
5013 -- it replaces the offending bound(s) of the range R with a
5014 -- Constraint_Error node. When the itype which uses these bounds
5015 -- is frozen the resulting call to Duplicate_Subexpr generates
5016 -- a new temporary for the bounds.
5018 -- Unfortunately there are other itypes that are also made depend
5019 -- on these bounds, so when Duplicate_Subexpr is called they get
5020 -- a forward reference to the newly created temporaries and Gigi
5021 -- aborts on such forward references. This is probably sign of a
5022 -- more fundamental problem somewhere else in either the order of
5023 -- itype freezing or the way certain itypes are constructed.
5025 -- To get around this problem we call Remove_Side_Effects right
5026 -- away if either bounds of R are a Constraint_Error.
5029 L : constant Node_Id := Low_Bound (R);
5030 H : constant Node_Id := High_Bound (R);
5033 if Nkind (L) = N_Raise_Constraint_Error then
5034 Remove_Side_Effects (L);
5037 if Nkind (H) = N_Raise_Constraint_Error then
5038 Remove_Side_Effects (H);
5042 Check_Unset_Reference (Low_Bound (R));
5043 Check_Unset_Reference (High_Bound (R));
5046 end Resolve_Discrete_Subtype_Indication;
5048 -------------------------
5049 -- Resolve_Entity_Name --
5050 -------------------------
5052 -- Used to resolve identifiers and expanded names
5054 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
5055 E : constant Entity_Id := Entity (N);
5058 -- If garbage from errors, set to Any_Type and return
5060 if No (E) and then Total_Errors_Detected /= 0 then
5061 Set_Etype (N, Any_Type);
5065 -- Replace named numbers by corresponding literals. Note that this is
5066 -- the one case where Resolve_Entity_Name must reset the Etype, since
5067 -- it is currently marked as universal.
5069 if Ekind (E) = E_Named_Integer then
5071 Eval_Named_Integer (N);
5073 elsif Ekind (E) = E_Named_Real then
5075 Eval_Named_Real (N);
5077 -- Allow use of subtype only if it is a concurrent type where we are
5078 -- currently inside the body. This will eventually be expanded
5079 -- into a call to Self (for tasks) or _object (for protected
5080 -- objects). Any other use of a subtype is invalid.
5082 elsif Is_Type (E) then
5083 if Is_Concurrent_Type (E)
5084 and then In_Open_Scopes (E)
5089 ("invalid use of subtype mark in expression or call", N);
5092 -- Check discriminant use if entity is discriminant in current scope,
5093 -- i.e. discriminant of record or concurrent type currently being
5094 -- analyzed. Uses in corresponding body are unrestricted.
5096 elsif Ekind (E) = E_Discriminant
5097 and then Scope (E) = Current_Scope
5098 and then not Has_Completion (Current_Scope)
5100 Check_Discriminant_Use (N);
5102 -- A parameterless generic function cannot appear in a context that
5103 -- requires resolution.
5105 elsif Ekind (E) = E_Generic_Function then
5106 Error_Msg_N ("illegal use of generic function", N);
5108 elsif Ekind (E) = E_Out_Parameter
5109 and then Ada_Version = Ada_83
5110 and then (Nkind (Parent (N)) in N_Op
5111 or else (Nkind (Parent (N)) = N_Assignment_Statement
5112 and then N = Expression (Parent (N)))
5113 or else Nkind (Parent (N)) = N_Explicit_Dereference)
5115 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
5117 -- In all other cases, just do the possible static evaluation
5120 -- A deferred constant that appears in an expression must have
5121 -- a completion, unless it has been removed by in-place expansion
5124 if Ekind (E) = E_Constant
5125 and then Comes_From_Source (E)
5126 and then No (Constant_Value (E))
5127 and then Is_Frozen (Etype (E))
5128 and then not In_Default_Expression
5129 and then not Is_Imported (E)
5132 if No_Initialization (Parent (E))
5133 or else (Present (Full_View (E))
5134 and then No_Initialization (Parent (Full_View (E))))
5139 "deferred constant is frozen before completion", N);
5143 Eval_Entity_Name (N);
5145 end Resolve_Entity_Name;
5151 procedure Resolve_Entry (Entry_Name : Node_Id) is
5152 Loc : constant Source_Ptr := Sloc (Entry_Name);
5160 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
5161 -- If the bounds of the entry family being called depend on task
5162 -- discriminants, build a new index subtype where a discriminant is
5163 -- replaced with the value of the discriminant of the target task.
5164 -- The target task is the prefix of the entry name in the call.
5166 -----------------------
5167 -- Actual_Index_Type --
5168 -----------------------
5170 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
5171 Typ : constant Entity_Id := Entry_Index_Type (E);
5172 Tsk : constant Entity_Id := Scope (E);
5173 Lo : constant Node_Id := Type_Low_Bound (Typ);
5174 Hi : constant Node_Id := Type_High_Bound (Typ);
5177 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
5178 -- If the bound is given by a discriminant, replace with a reference
5179 -- to the discriminant of the same name in the target task.
5180 -- If the entry name is the target of a requeue statement and the
5181 -- entry is in the current protected object, the bound to be used
5182 -- is the discriminal of the object (see apply_range_checks for
5183 -- details of the transformation).
5185 -----------------------------
5186 -- Actual_Discriminant_Ref --
5187 -----------------------------
5189 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
5190 Typ : constant Entity_Id := Etype (Bound);
5194 Remove_Side_Effects (Bound);
5196 if not Is_Entity_Name (Bound)
5197 or else Ekind (Entity (Bound)) /= E_Discriminant
5201 elsif Is_Protected_Type (Tsk)
5202 and then In_Open_Scopes (Tsk)
5203 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
5205 return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
5209 Make_Selected_Component (Loc,
5210 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
5211 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
5216 end Actual_Discriminant_Ref;
5218 -- Start of processing for Actual_Index_Type
5221 if not Has_Discriminants (Tsk)
5222 or else (not Is_Entity_Name (Lo)
5223 and then not Is_Entity_Name (Hi))
5225 return Entry_Index_Type (E);
5228 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
5229 Set_Etype (New_T, Base_Type (Typ));
5230 Set_Size_Info (New_T, Typ);
5231 Set_RM_Size (New_T, RM_Size (Typ));
5232 Set_Scalar_Range (New_T,
5233 Make_Range (Sloc (Entry_Name),
5234 Low_Bound => Actual_Discriminant_Ref (Lo),
5235 High_Bound => Actual_Discriminant_Ref (Hi)));
5239 end Actual_Index_Type;
5241 -- Start of processing of Resolve_Entry
5244 -- Find name of entry being called, and resolve prefix of name
5245 -- with its own type. The prefix can be overloaded, and the name
5246 -- and signature of the entry must be taken into account.
5248 if Nkind (Entry_Name) = N_Indexed_Component then
5250 -- Case of dealing with entry family within the current tasks
5252 E_Name := Prefix (Entry_Name);
5255 E_Name := Entry_Name;
5258 if Is_Entity_Name (E_Name) then
5259 -- Entry call to an entry (or entry family) in the current task.
5260 -- This is legal even though the task will deadlock. Rewrite as
5261 -- call to current task.
5263 -- This can also be a call to an entry in an enclosing task.
5264 -- If this is a single task, we have to retrieve its name,
5265 -- because the scope of the entry is the task type, not the
5266 -- object. If the enclosing task is a task type, the identity
5267 -- of the task is given by its own self variable.
5269 -- Finally this can be a requeue on an entry of the same task
5270 -- or protected object.
5272 S := Scope (Entity (E_Name));
5274 for J in reverse 0 .. Scope_Stack.Last loop
5276 if Is_Task_Type (Scope_Stack.Table (J).Entity)
5277 and then not Comes_From_Source (S)
5279 -- S is an enclosing task or protected object. The concurrent
5280 -- declaration has been converted into a type declaration, and
5281 -- the object itself has an object declaration that follows
5282 -- the type in the same declarative part.
5284 Tsk := Next_Entity (S);
5285 while Etype (Tsk) /= S loop
5292 elsif S = Scope_Stack.Table (J).Entity then
5294 -- Call to current task. Will be transformed into call to Self
5302 Make_Selected_Component (Loc,
5303 Prefix => New_Occurrence_Of (S, Loc),
5305 New_Occurrence_Of (Entity (E_Name), Loc));
5306 Rewrite (E_Name, New_N);
5309 elsif Nkind (Entry_Name) = N_Selected_Component
5310 and then Is_Overloaded (Prefix (Entry_Name))
5312 -- Use the entry name (which must be unique at this point) to
5313 -- find the prefix that returns the corresponding task type or
5317 Pref : constant Node_Id := Prefix (Entry_Name);
5318 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
5323 Get_First_Interp (Pref, I, It);
5324 while Present (It.Typ) loop
5325 if Scope (Ent) = It.Typ then
5326 Set_Etype (Pref, It.Typ);
5330 Get_Next_Interp (I, It);
5335 if Nkind (Entry_Name) = N_Selected_Component then
5336 Resolve (Prefix (Entry_Name));
5338 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5339 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5340 Resolve (Prefix (Prefix (Entry_Name)));
5341 Index := First (Expressions (Entry_Name));
5342 Resolve (Index, Entry_Index_Type (Nam));
5344 -- Up to this point the expression could have been the actual
5345 -- in a simple entry call, and be given by a named association.
5347 if Nkind (Index) = N_Parameter_Association then
5348 Error_Msg_N ("expect expression for entry index", Index);
5350 Apply_Range_Check (Index, Actual_Index_Type (Nam));
5355 ------------------------
5356 -- Resolve_Entry_Call --
5357 ------------------------
5359 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
5360 Entry_Name : constant Node_Id := Name (N);
5361 Loc : constant Source_Ptr := Sloc (Entry_Name);
5363 First_Named : Node_Id;
5370 -- We kill all checks here, because it does not seem worth the
5371 -- effort to do anything better, an entry call is a big operation.
5375 -- Processing of the name is similar for entry calls and protected
5376 -- operation calls. Once the entity is determined, we can complete
5377 -- the resolution of the actuals.
5379 -- The selector may be overloaded, in the case of a protected object
5380 -- with overloaded functions. The type of the context is used for
5383 if Nkind (Entry_Name) = N_Selected_Component
5384 and then Is_Overloaded (Selector_Name (Entry_Name))
5385 and then Typ /= Standard_Void_Type
5392 Get_First_Interp (Selector_Name (Entry_Name), I, It);
5393 while Present (It.Typ) loop
5394 if Covers (Typ, It.Typ) then
5395 Set_Entity (Selector_Name (Entry_Name), It.Nam);
5396 Set_Etype (Entry_Name, It.Typ);
5398 Generate_Reference (It.Typ, N, ' ');
5401 Get_Next_Interp (I, It);
5406 Resolve_Entry (Entry_Name);
5408 if Nkind (Entry_Name) = N_Selected_Component then
5410 -- Simple entry call
5412 Nam := Entity (Selector_Name (Entry_Name));
5413 Obj := Prefix (Entry_Name);
5414 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
5416 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5418 -- Call to member of entry family
5420 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5421 Obj := Prefix (Prefix (Entry_Name));
5422 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
5425 -- We cannot in general check the maximum depth of protected entry
5426 -- calls at compile time. But we can tell that any protected entry
5427 -- call at all violates a specified nesting depth of zero.
5429 if Is_Protected_Type (Scope (Nam)) then
5430 Check_Restriction (Max_Entry_Queue_Length, N);
5433 -- Use context type to disambiguate a protected function that can be
5434 -- called without actuals and that returns an array type, and where
5435 -- the argument list may be an indexing of the returned value.
5437 if Ekind (Nam) = E_Function
5438 and then Needs_No_Actuals (Nam)
5439 and then Present (Parameter_Associations (N))
5441 ((Is_Array_Type (Etype (Nam))
5442 and then Covers (Typ, Component_Type (Etype (Nam))))
5444 or else (Is_Access_Type (Etype (Nam))
5445 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5446 and then Covers (Typ,
5447 Component_Type (Designated_Type (Etype (Nam))))))
5450 Index_Node : Node_Id;
5454 Make_Indexed_Component (Loc,
5456 Make_Function_Call (Loc,
5457 Name => Relocate_Node (Entry_Name)),
5458 Expressions => Parameter_Associations (N));
5460 -- Since we are correcting a node classification error made by
5461 -- the parser, we call Replace rather than Rewrite.
5463 Replace (N, Index_Node);
5464 Set_Etype (Prefix (N), Etype (Nam));
5466 Resolve_Indexed_Component (N, Typ);
5471 -- The operation name may have been overloaded. Order the actuals
5472 -- according to the formals of the resolved entity, and set the
5473 -- return type to that of the operation.
5476 Normalize_Actuals (N, Nam, False, Norm_OK);
5477 pragma Assert (Norm_OK);
5478 Set_Etype (N, Etype (Nam));
5481 Resolve_Actuals (N, Nam);
5482 Generate_Reference (Nam, Entry_Name);
5484 if Ekind (Nam) = E_Entry
5485 or else Ekind (Nam) = E_Entry_Family
5487 Check_Potentially_Blocking_Operation (N);
5490 -- Verify that a procedure call cannot masquerade as an entry
5491 -- call where an entry call is expected.
5493 if Ekind (Nam) = E_Procedure then
5494 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5495 and then N = Entry_Call_Statement (Parent (N))
5497 Error_Msg_N ("entry call required in select statement", N);
5499 elsif Nkind (Parent (N)) = N_Triggering_Alternative
5500 and then N = Triggering_Statement (Parent (N))
5502 Error_Msg_N ("triggering statement cannot be procedure call", N);
5504 elsif Ekind (Scope (Nam)) = E_Task_Type
5505 and then not In_Open_Scopes (Scope (Nam))
5507 Error_Msg_N ("task has no entry with this name", Entry_Name);
5511 -- After resolution, entry calls and protected procedure calls
5512 -- are changed into entry calls, for expansion. The structure
5513 -- of the node does not change, so it can safely be done in place.
5514 -- Protected function calls must keep their structure because they
5515 -- are subexpressions.
5517 if Ekind (Nam) /= E_Function then
5519 -- A protected operation that is not a function may modify the
5520 -- corresponding object, and cannot apply to a constant.
5521 -- If this is an internal call, the prefix is the type itself.
5523 if Is_Protected_Type (Scope (Nam))
5524 and then not Is_Variable (Obj)
5525 and then (not Is_Entity_Name (Obj)
5526 or else not Is_Type (Entity (Obj)))
5529 ("prefix of protected procedure or entry call must be variable",
5533 Actuals := Parameter_Associations (N);
5534 First_Named := First_Named_Actual (N);
5537 Make_Entry_Call_Statement (Loc,
5539 Parameter_Associations => Actuals));
5541 Set_First_Named_Actual (N, First_Named);
5542 Set_Analyzed (N, True);
5544 -- Protected functions can return on the secondary stack, in which
5545 -- case we must trigger the transient scope mechanism.
5547 elsif Expander_Active
5548 and then Requires_Transient_Scope (Etype (Nam))
5550 Establish_Transient_Scope (N, Sec_Stack => True);
5552 end Resolve_Entry_Call;
5554 -------------------------
5555 -- Resolve_Equality_Op --
5556 -------------------------
5558 -- Both arguments must have the same type, and the boolean context
5559 -- does not participate in the resolution. The first pass verifies
5560 -- that the interpretation is not ambiguous, and the type of the left
5561 -- argument is correctly set, or is Any_Type in case of ambiguity.
5562 -- If both arguments are strings or aggregates, allocators, or Null,
5563 -- they are ambiguous even though they carry a single (universal) type.
5564 -- Diagnose this case here.
5566 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
5567 L : constant Node_Id := Left_Opnd (N);
5568 R : constant Node_Id := Right_Opnd (N);
5569 T : Entity_Id := Find_Unique_Type (L, R);
5571 function Find_Unique_Access_Type return Entity_Id;
5572 -- In the case of allocators, make a last-ditch attempt to find a single
5573 -- access type with the right designated type. This is semantically
5574 -- dubious, and of no interest to any real code, but c48008a makes it
5577 -----------------------------
5578 -- Find_Unique_Access_Type --
5579 -----------------------------
5581 function Find_Unique_Access_Type return Entity_Id is
5587 if Ekind (Etype (R)) = E_Allocator_Type then
5588 Acc := Designated_Type (Etype (R));
5589 elsif Ekind (Etype (L)) = E_Allocator_Type then
5590 Acc := Designated_Type (Etype (L));
5596 while S /= Standard_Standard loop
5597 E := First_Entity (S);
5598 while Present (E) loop
5600 and then Is_Access_Type (E)
5601 and then Ekind (E) /= E_Allocator_Type
5602 and then Designated_Type (E) = Base_Type (Acc)
5614 end Find_Unique_Access_Type;
5616 -- Start of processing for Resolve_Equality_Op
5619 Set_Etype (N, Base_Type (Typ));
5620 Generate_Reference (T, N, ' ');
5622 if T = Any_Fixed then
5623 T := Unique_Fixed_Point_Type (L);
5626 if T /= Any_Type then
5628 or else T = Any_Composite
5629 or else T = Any_Character
5631 if T = Any_Character then
5632 Ambiguous_Character (L);
5634 Error_Msg_N ("ambiguous operands for equality", N);
5637 Set_Etype (N, Any_Type);
5640 elsif T = Any_Access
5641 or else Ekind (T) = E_Allocator_Type
5642 or else Ekind (T) = E_Access_Attribute_Type
5644 T := Find_Unique_Access_Type;
5647 Error_Msg_N ("ambiguous operands for equality", N);
5648 Set_Etype (N, Any_Type);
5656 -- If the unique type is a class-wide type then it will be expanded
5657 -- into a dispatching call to the predefined primitive. Therefore we
5658 -- check here for potential violation of such restriction.
5660 if Is_Class_Wide_Type (T) then
5661 Check_Restriction (No_Dispatching_Calls, N);
5664 if Warn_On_Redundant_Constructs
5665 and then Comes_From_Source (N)
5666 and then Is_Entity_Name (R)
5667 and then Entity (R) = Standard_True
5668 and then Comes_From_Source (R)
5670 Error_Msg_N ("?comparison with True is redundant!", R);
5673 Check_Unset_Reference (L);
5674 Check_Unset_Reference (R);
5675 Generate_Operator_Reference (N, T);
5677 -- If this is an inequality, it may be the implicit inequality
5678 -- created for a user-defined operation, in which case the corres-
5679 -- ponding equality operation is not intrinsic, and the operation
5680 -- cannot be constant-folded. Else fold.
5682 if Nkind (N) = N_Op_Eq
5683 or else Comes_From_Source (Entity (N))
5684 or else Ekind (Entity (N)) = E_Operator
5685 or else Is_Intrinsic_Subprogram
5686 (Corresponding_Equality (Entity (N)))
5688 Eval_Relational_Op (N);
5689 elsif Nkind (N) = N_Op_Ne
5690 and then Is_Abstract_Subprogram (Entity (N))
5692 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
5695 -- Ada 2005: If one operand is an anonymous access type, convert
5696 -- the other operand to it, to ensure that the underlying types
5697 -- match in the back-end. Same for access_to_subprogram, and the
5698 -- conversion verifies that the types are subtype conformant.
5700 -- We apply the same conversion in the case one of the operands is
5701 -- a private subtype of the type of the other.
5703 -- Why the Expander_Active test here ???
5707 (Ekind (T) = E_Anonymous_Access_Type
5708 or else Ekind (T) = E_Anonymous_Access_Subprogram_Type
5709 or else Is_Private_Type (T))
5711 if Etype (L) /= T then
5713 Make_Unchecked_Type_Conversion (Sloc (L),
5714 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
5715 Expression => Relocate_Node (L)));
5716 Analyze_And_Resolve (L, T);
5719 if (Etype (R)) /= T then
5721 Make_Unchecked_Type_Conversion (Sloc (R),
5722 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
5723 Expression => Relocate_Node (R)));
5724 Analyze_And_Resolve (R, T);
5728 end Resolve_Equality_Op;
5730 ----------------------------------
5731 -- Resolve_Explicit_Dereference --
5732 ----------------------------------
5734 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
5735 Loc : constant Source_Ptr := Sloc (N);
5737 P : constant Node_Id := Prefix (N);
5742 Check_Fully_Declared_Prefix (Typ, P);
5744 if Is_Overloaded (P) then
5746 -- Use the context type to select the prefix that has the correct
5749 Get_First_Interp (P, I, It);
5750 while Present (It.Typ) loop
5751 exit when Is_Access_Type (It.Typ)
5752 and then Covers (Typ, Designated_Type (It.Typ));
5753 Get_Next_Interp (I, It);
5756 if Present (It.Typ) then
5757 Resolve (P, It.Typ);
5759 -- If no interpretation covers the designated type of the prefix,
5760 -- this is the pathological case where not all implementations of
5761 -- the prefix allow the interpretation of the node as a call. Now
5762 -- that the expected type is known, Remove other interpretations
5763 -- from prefix, rewrite it as a call, and resolve again, so that
5764 -- the proper call node is generated.
5766 Get_First_Interp (P, I, It);
5767 while Present (It.Typ) loop
5768 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
5772 Get_Next_Interp (I, It);
5776 Make_Function_Call (Loc,
5778 Make_Explicit_Dereference (Loc,
5780 Parameter_Associations => New_List);
5782 Save_Interps (N, New_N);
5784 Analyze_And_Resolve (N, Typ);
5788 Set_Etype (N, Designated_Type (It.Typ));
5794 if Is_Access_Type (Etype (P)) then
5795 Apply_Access_Check (N);
5798 -- If the designated type is a packed unconstrained array type, and the
5799 -- explicit dereference is not in the context of an attribute reference,
5800 -- then we must compute and set the actual subtype, since it is needed
5801 -- by Gigi. The reason we exclude the attribute case is that this is
5802 -- handled fine by Gigi, and in fact we use such attributes to build the
5803 -- actual subtype. We also exclude generated code (which builds actual
5804 -- subtypes directly if they are needed).
5806 if Is_Array_Type (Etype (N))
5807 and then Is_Packed (Etype (N))
5808 and then not Is_Constrained (Etype (N))
5809 and then Nkind (Parent (N)) /= N_Attribute_Reference
5810 and then Comes_From_Source (N)
5812 Set_Etype (N, Get_Actual_Subtype (N));
5815 -- Note: there is no Eval processing required for an explicit deference,
5816 -- because the type is known to be an allocators, and allocator
5817 -- expressions can never be static.
5819 end Resolve_Explicit_Dereference;
5821 -------------------------------
5822 -- Resolve_Indexed_Component --
5823 -------------------------------
5825 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
5826 Name : constant Node_Id := Prefix (N);
5828 Array_Type : Entity_Id := Empty; -- to prevent junk warning
5832 if Is_Overloaded (Name) then
5834 -- Use the context type to select the prefix that yields the correct
5840 I1 : Interp_Index := 0;
5841 P : constant Node_Id := Prefix (N);
5842 Found : Boolean := False;
5845 Get_First_Interp (P, I, It);
5846 while Present (It.Typ) loop
5847 if (Is_Array_Type (It.Typ)
5848 and then Covers (Typ, Component_Type (It.Typ)))
5849 or else (Is_Access_Type (It.Typ)
5850 and then Is_Array_Type (Designated_Type (It.Typ))
5852 (Typ, Component_Type (Designated_Type (It.Typ))))
5855 It := Disambiguate (P, I1, I, Any_Type);
5857 if It = No_Interp then
5858 Error_Msg_N ("ambiguous prefix for indexing", N);
5864 Array_Type := It.Typ;
5870 Array_Type := It.Typ;
5875 Get_Next_Interp (I, It);
5880 Array_Type := Etype (Name);
5883 Resolve (Name, Array_Type);
5884 Array_Type := Get_Actual_Subtype_If_Available (Name);
5886 -- If prefix is access type, dereference to get real array type.
5887 -- Note: we do not apply an access check because the expander always
5888 -- introduces an explicit dereference, and the check will happen there.
5890 if Is_Access_Type (Array_Type) then
5891 Array_Type := Designated_Type (Array_Type);
5894 -- If name was overloaded, set component type correctly now
5895 -- If a misplaced call to an entry family (which has no index typs)
5896 -- return. Error will be diagnosed from calling context.
5898 if Is_Array_Type (Array_Type) then
5899 Set_Etype (N, Component_Type (Array_Type));
5904 Index := First_Index (Array_Type);
5905 Expr := First (Expressions (N));
5907 -- The prefix may have resolved to a string literal, in which case its
5908 -- etype has a special representation. This is only possible currently
5909 -- if the prefix is a static concatenation, written in functional
5912 if Ekind (Array_Type) = E_String_Literal_Subtype then
5913 Resolve (Expr, Standard_Positive);
5916 while Present (Index) and Present (Expr) loop
5917 Resolve (Expr, Etype (Index));
5918 Check_Unset_Reference (Expr);
5920 if Is_Scalar_Type (Etype (Expr)) then
5921 Apply_Scalar_Range_Check (Expr, Etype (Index));
5923 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
5931 -- Do not generate the warning on suspicious index if we are analyzing
5932 -- package Ada.Tags; otherwise we will report the warning with the
5933 -- Prims_Ptr field of the dispatch table.
5935 if Scope (Etype (Prefix (N))) = Standard_Standard
5937 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
5940 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
5941 Eval_Indexed_Component (N);
5943 end Resolve_Indexed_Component;
5945 -----------------------------
5946 -- Resolve_Integer_Literal --
5947 -----------------------------
5949 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
5952 Eval_Integer_Literal (N);
5953 end Resolve_Integer_Literal;
5955 --------------------------------
5956 -- Resolve_Intrinsic_Operator --
5957 --------------------------------
5959 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
5960 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
5967 while Scope (Op) /= Standard_Standard loop
5969 pragma Assert (Present (Op));
5973 Set_Is_Overloaded (N, False);
5975 -- If the operand type is private, rewrite with suitable conversions on
5976 -- the operands and the result, to expose the proper underlying numeric
5979 if Is_Private_Type (Typ) then
5980 Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd (N));
5982 if Nkind (N) = N_Op_Expon then
5983 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
5985 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
5988 Save_Interps (Left_Opnd (N), Expression (Arg1));
5989 Save_Interps (Right_Opnd (N), Expression (Arg2));
5991 Set_Left_Opnd (N, Arg1);
5992 Set_Right_Opnd (N, Arg2);
5994 Set_Etype (N, Btyp);
5995 Rewrite (N, Unchecked_Convert_To (Typ, N));
5998 elsif Typ /= Etype (Left_Opnd (N))
5999 or else Typ /= Etype (Right_Opnd (N))
6001 -- Add explicit conversion where needed, and save interpretations
6002 -- in case operands are overloaded.
6004 Arg1 := Convert_To (Typ, Left_Opnd (N));
6005 Arg2 := Convert_To (Typ, Right_Opnd (N));
6007 if Nkind (Arg1) = N_Type_Conversion then
6008 Save_Interps (Left_Opnd (N), Expression (Arg1));
6010 Save_Interps (Left_Opnd (N), Arg1);
6013 if Nkind (Arg2) = N_Type_Conversion then
6014 Save_Interps (Right_Opnd (N), Expression (Arg2));
6016 Save_Interps (Right_Opnd (N), Arg2);
6019 Rewrite (Left_Opnd (N), Arg1);
6020 Rewrite (Right_Opnd (N), Arg2);
6023 Resolve_Arithmetic_Op (N, Typ);
6026 Resolve_Arithmetic_Op (N, Typ);
6028 end Resolve_Intrinsic_Operator;
6030 --------------------------------------
6031 -- Resolve_Intrinsic_Unary_Operator --
6032 --------------------------------------
6034 procedure Resolve_Intrinsic_Unary_Operator
6038 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6044 while Scope (Op) /= Standard_Standard loop
6046 pragma Assert (Present (Op));
6051 if Is_Private_Type (Typ) then
6052 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6053 Save_Interps (Right_Opnd (N), Expression (Arg2));
6055 Set_Right_Opnd (N, Arg2);
6057 Set_Etype (N, Btyp);
6058 Rewrite (N, Unchecked_Convert_To (Typ, N));
6062 Resolve_Unary_Op (N, Typ);
6064 end Resolve_Intrinsic_Unary_Operator;
6066 ------------------------
6067 -- Resolve_Logical_Op --
6068 ------------------------
6070 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
6072 N_Opr : constant Node_Kind := Nkind (N);
6075 -- Predefined operations on scalar types yield the base type. On the
6076 -- other hand, logical operations on arrays yield the type of the
6077 -- arguments (and the context).
6079 if Is_Array_Type (Typ) then
6082 B_Typ := Base_Type (Typ);
6085 -- The following test is required because the operands of the operation
6086 -- may be literals, in which case the resulting type appears to be
6087 -- compatible with a signed integer type, when in fact it is compatible
6088 -- only with modular types. If the context itself is universal, the
6089 -- operation is illegal.
6091 if not Valid_Boolean_Arg (Typ) then
6092 Error_Msg_N ("invalid context for logical operation", N);
6093 Set_Etype (N, Any_Type);
6096 elsif Typ = Any_Modular then
6098 ("no modular type available in this context", N);
6099 Set_Etype (N, Any_Type);
6101 elsif Is_Modular_Integer_Type (Typ)
6102 and then Etype (Left_Opnd (N)) = Universal_Integer
6103 and then Etype (Right_Opnd (N)) = Universal_Integer
6105 Check_For_Visible_Operator (N, B_Typ);
6108 Resolve (Left_Opnd (N), B_Typ);
6109 Resolve (Right_Opnd (N), B_Typ);
6111 Check_Unset_Reference (Left_Opnd (N));
6112 Check_Unset_Reference (Right_Opnd (N));
6114 Set_Etype (N, B_Typ);
6115 Generate_Operator_Reference (N, B_Typ);
6116 Eval_Logical_Op (N);
6118 -- Check for violation of restriction No_Direct_Boolean_Operators
6119 -- if the operator was not eliminated by the Eval_Logical_Op call.
6121 if Nkind (N) = N_Opr
6122 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
6124 Check_Restriction (No_Direct_Boolean_Operators, N);
6126 end Resolve_Logical_Op;
6128 ---------------------------
6129 -- Resolve_Membership_Op --
6130 ---------------------------
6132 -- The context can only be a boolean type, and does not determine
6133 -- the arguments. Arguments should be unambiguous, but the preference
6134 -- rule for universal types applies.
6136 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
6137 pragma Warnings (Off, Typ);
6139 L : constant Node_Id := Left_Opnd (N);
6140 R : constant Node_Id := Right_Opnd (N);
6144 if L = Error or else R = Error then
6148 if not Is_Overloaded (R)
6150 (Etype (R) = Universal_Integer or else
6151 Etype (R) = Universal_Real)
6152 and then Is_Overloaded (L)
6156 -- Ada 2005 (AI-251): Give support to the following case:
6158 -- type I is interface;
6159 -- type T is tagged ...
6161 -- function Test (O : I'Class) is
6163 -- return O in T'Class.
6166 -- In this case we have nothing else to do; the membership test will be
6167 -- done at run-time.
6169 elsif Ada_Version >= Ada_05
6170 and then Is_Class_Wide_Type (Etype (L))
6171 and then Is_Interface (Etype (L))
6172 and then Is_Class_Wide_Type (Etype (R))
6173 and then not Is_Interface (Etype (R))
6178 T := Intersect_Types (L, R);
6182 Check_Unset_Reference (L);
6184 if Nkind (R) = N_Range
6185 and then not Is_Scalar_Type (T)
6187 Error_Msg_N ("scalar type required for range", R);
6190 if Is_Entity_Name (R) then
6191 Freeze_Expression (R);
6194 Check_Unset_Reference (R);
6197 Eval_Membership_Op (N);
6198 end Resolve_Membership_Op;
6204 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
6206 -- Handle restriction against anonymous null access values This
6207 -- restriction can be turned off using -gnatdh.
6209 -- Ada 2005 (AI-231): Remove restriction
6211 if Ada_Version < Ada_05
6212 and then not Debug_Flag_J
6213 and then Ekind (Typ) = E_Anonymous_Access_Type
6214 and then Comes_From_Source (N)
6216 -- In the common case of a call which uses an explicitly null
6217 -- value for an access parameter, give specialized error msg
6219 if Nkind (Parent (N)) = N_Procedure_Call_Statement
6221 Nkind (Parent (N)) = N_Function_Call
6224 ("null is not allowed as argument for an access parameter", N);
6226 -- Standard message for all other cases (are there any?)
6230 ("null cannot be of an anonymous access type", N);
6234 -- In a distributed context, null for a remote access to subprogram
6235 -- may need to be replaced with a special record aggregate. In this
6236 -- case, return after having done the transformation.
6238 if (Ekind (Typ) = E_Record_Type
6239 or else Is_Remote_Access_To_Subprogram_Type (Typ))
6240 and then Remote_AST_Null_Value (N, Typ)
6245 -- The null literal takes its type from the context
6250 -----------------------
6251 -- Resolve_Op_Concat --
6252 -----------------------
6254 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
6255 Btyp : constant Entity_Id := Base_Type (Typ);
6256 Op1 : constant Node_Id := Left_Opnd (N);
6257 Op2 : constant Node_Id := Right_Opnd (N);
6259 procedure Resolve_Concatenation_Arg (Arg : Node_Id; Is_Comp : Boolean);
6260 -- Internal procedure to resolve one operand of concatenation operator.
6261 -- The operand is either of the array type or of the component type.
6262 -- If the operand is an aggregate, and the component type is composite,
6263 -- this is ambiguous if component type has aggregates.
6265 -------------------------------
6266 -- Resolve_Concatenation_Arg --
6267 -------------------------------
6269 procedure Resolve_Concatenation_Arg (Arg : Node_Id; Is_Comp : Boolean) is
6273 or else (not Is_Overloaded (Arg)
6274 and then Etype (Arg) /= Any_Composite
6275 and then Covers (Component_Type (Typ), Etype (Arg)))
6277 Resolve (Arg, Component_Type (Typ));
6279 Resolve (Arg, Btyp);
6282 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
6284 if Nkind (Arg) = N_Aggregate
6285 and then Is_Composite_Type (Component_Type (Typ))
6287 if Is_Private_Type (Component_Type (Typ)) then
6288 Resolve (Arg, Btyp);
6291 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
6292 Set_Etype (Arg, Any_Type);
6296 if Is_Overloaded (Arg)
6297 and then Has_Compatible_Type (Arg, Typ)
6298 and then Etype (Arg) /= Any_Type
6307 Get_First_Interp (Arg, I, It);
6309 Get_Next_Interp (I, It);
6311 -- Special-case the error message when the overloading
6312 -- is caused by a function that yields and array and
6313 -- can be called without parameters.
6315 if It.Nam = Func then
6316 Error_Msg_Sloc := Sloc (Func);
6317 Error_Msg_N ("ambiguous call to function#", Arg);
6319 ("\\interpretation as call yields&", Arg, Typ);
6321 ("\\interpretation as indexing of call yields&",
6322 Arg, Component_Type (Typ));
6326 ("ambiguous operand for concatenation!", Arg);
6327 Get_First_Interp (Arg, I, It);
6328 while Present (It.Nam) loop
6329 Error_Msg_Sloc := Sloc (It.Nam);
6331 if Base_Type (It.Typ) = Base_Type (Typ)
6332 or else Base_Type (It.Typ) =
6333 Base_Type (Component_Type (Typ))
6335 Error_Msg_N ("\\possible interpretation#", Arg);
6338 Get_Next_Interp (I, It);
6344 Resolve (Arg, Component_Type (Typ));
6346 if Nkind (Arg) = N_String_Literal then
6347 Set_Etype (Arg, Component_Type (Typ));
6350 if Arg = Left_Opnd (N) then
6351 Set_Is_Component_Left_Opnd (N);
6353 Set_Is_Component_Right_Opnd (N);
6358 Resolve (Arg, Btyp);
6361 Check_Unset_Reference (Arg);
6362 end Resolve_Concatenation_Arg;
6364 -- Start of processing for Resolve_Op_Concat
6367 -- The parser folds an enormous sequence of concatenations of string
6368 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
6369 -- in the right. If the expression resolves to a predefined "&"
6370 -- operator, all is well. Otherwise, the parser's folding is wrong, so
6371 -- we give an error. See P_Simple_Expression in Par.Ch4.
6373 if Nkind (Op2) = N_String_Literal
6374 and then Is_Folded_In_Parser (Op2)
6375 and then Ekind (Entity (N)) = E_Function
6377 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
6378 and then String_Length (Strval (Op1)) = 0);
6379 Error_Msg_N ("too many user-defined concatenations", N);
6383 Set_Etype (N, Btyp);
6385 if Is_Limited_Composite (Btyp) then
6386 Error_Msg_N ("concatenation not available for limited array", N);
6387 Explain_Limited_Type (Btyp, N);
6390 -- If the operands are themselves concatenations, resolve them as such
6391 -- directly. This removes several layers of recursion and allows GNAT to
6392 -- handle larger multiple concatenations.
6394 if Nkind (Op1) = N_Op_Concat
6395 and then not Is_Array_Type (Component_Type (Typ))
6396 and then Entity (Op1) = Entity (N)
6398 Resolve_Op_Concat (Op1, Typ);
6400 Resolve_Concatenation_Arg
6401 (Op1, Is_Component_Left_Opnd (N));
6404 if Nkind (Op2) = N_Op_Concat
6405 and then not Is_Array_Type (Component_Type (Typ))
6406 and then Entity (Op2) = Entity (N)
6408 Resolve_Op_Concat (Op2, Typ);
6410 Resolve_Concatenation_Arg
6411 (Op2, Is_Component_Right_Opnd (N));
6414 Generate_Operator_Reference (N, Typ);
6416 if Is_String_Type (Typ) then
6417 Eval_Concatenation (N);
6420 -- If this is not a static concatenation, but the result is a
6421 -- string type (and not an array of strings) insure that static
6422 -- string operands have their subtypes properly constructed.
6424 if Nkind (N) /= N_String_Literal
6425 and then Is_Character_Type (Component_Type (Typ))
6427 Set_String_Literal_Subtype (Op1, Typ);
6428 Set_String_Literal_Subtype (Op2, Typ);
6430 end Resolve_Op_Concat;
6432 ----------------------
6433 -- Resolve_Op_Expon --
6434 ----------------------
6436 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
6437 B_Typ : constant Entity_Id := Base_Type (Typ);
6440 -- Catch attempts to do fixed-point exponentation with universal
6441 -- operands, which is a case where the illegality is not caught during
6442 -- normal operator analysis.
6444 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
6445 Error_Msg_N ("exponentiation not available for fixed point", N);
6449 if Comes_From_Source (N)
6450 and then Ekind (Entity (N)) = E_Function
6451 and then Is_Imported (Entity (N))
6452 and then Is_Intrinsic_Subprogram (Entity (N))
6454 Resolve_Intrinsic_Operator (N, Typ);
6458 if Etype (Left_Opnd (N)) = Universal_Integer
6459 or else Etype (Left_Opnd (N)) = Universal_Real
6461 Check_For_Visible_Operator (N, B_Typ);
6464 -- We do the resolution using the base type, because intermediate values
6465 -- in expressions always are of the base type, not a subtype of it.
6467 Resolve (Left_Opnd (N), B_Typ);
6468 Resolve (Right_Opnd (N), Standard_Integer);
6470 Check_Unset_Reference (Left_Opnd (N));
6471 Check_Unset_Reference (Right_Opnd (N));
6473 Set_Etype (N, B_Typ);
6474 Generate_Operator_Reference (N, B_Typ);
6477 -- Set overflow checking bit. Much cleverer code needed here eventually
6478 -- and perhaps the Resolve routines should be separated for the various
6479 -- arithmetic operations, since they will need different processing. ???
6481 if Nkind (N) in N_Op then
6482 if not Overflow_Checks_Suppressed (Etype (N)) then
6483 Enable_Overflow_Check (N);
6486 end Resolve_Op_Expon;
6488 --------------------
6489 -- Resolve_Op_Not --
6490 --------------------
6492 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
6495 function Parent_Is_Boolean return Boolean;
6496 -- This function determines if the parent node is a boolean operator
6497 -- or operation (comparison op, membership test, or short circuit form)
6498 -- and the not in question is the left operand of this operation.
6499 -- Note that if the not is in parens, then false is returned.
6501 -----------------------
6502 -- Parent_Is_Boolean --
6503 -----------------------
6505 function Parent_Is_Boolean return Boolean is
6507 if Paren_Count (N) /= 0 then
6511 case Nkind (Parent (N)) is
6526 return Left_Opnd (Parent (N)) = N;
6532 end Parent_Is_Boolean;
6534 -- Start of processing for Resolve_Op_Not
6537 -- Predefined operations on scalar types yield the base type. On the
6538 -- other hand, logical operations on arrays yield the type of the
6539 -- arguments (and the context).
6541 if Is_Array_Type (Typ) then
6544 B_Typ := Base_Type (Typ);
6547 -- Straigtforward case of incorrect arguments
6549 if not Valid_Boolean_Arg (Typ) then
6550 Error_Msg_N ("invalid operand type for operator&", N);
6551 Set_Etype (N, Any_Type);
6554 -- Special case of probable missing parens
6556 elsif Typ = Universal_Integer or else Typ = Any_Modular then
6557 if Parent_Is_Boolean then
6559 ("operand of not must be enclosed in parentheses",
6563 ("no modular type available in this context", N);
6566 Set_Etype (N, Any_Type);
6569 -- OK resolution of not
6572 -- Warn if non-boolean types involved. This is a case like not a < b
6573 -- where a and b are modular, where we will get (not a) < b and most
6574 -- likely not (a < b) was intended.
6576 if Warn_On_Questionable_Missing_Parens
6577 and then not Is_Boolean_Type (Typ)
6578 and then Parent_Is_Boolean
6580 Error_Msg_N ("?not expression should be parenthesized here!", N);
6583 Resolve (Right_Opnd (N), B_Typ);
6584 Check_Unset_Reference (Right_Opnd (N));
6585 Set_Etype (N, B_Typ);
6586 Generate_Operator_Reference (N, B_Typ);
6591 -----------------------------
6592 -- Resolve_Operator_Symbol --
6593 -----------------------------
6595 -- Nothing to be done, all resolved already
6597 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
6598 pragma Warnings (Off, N);
6599 pragma Warnings (Off, Typ);
6603 end Resolve_Operator_Symbol;
6605 ----------------------------------
6606 -- Resolve_Qualified_Expression --
6607 ----------------------------------
6609 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
6610 pragma Warnings (Off, Typ);
6612 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
6613 Expr : constant Node_Id := Expression (N);
6616 Resolve (Expr, Target_Typ);
6618 -- A qualified expression requires an exact match of the type,
6619 -- class-wide matching is not allowed. However, if the qualifying
6620 -- type is specific and the expression has a class-wide type, it
6621 -- may still be okay, since it can be the result of the expansion
6622 -- of a call to a dispatching function, so we also have to check
6623 -- class-wideness of the type of the expression's original node.
6625 if (Is_Class_Wide_Type (Target_Typ)
6627 (Is_Class_Wide_Type (Etype (Expr))
6628 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
6629 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
6631 Wrong_Type (Expr, Target_Typ);
6634 -- If the target type is unconstrained, then we reset the type of
6635 -- the result from the type of the expression. For other cases, the
6636 -- actual subtype of the expression is the target type.
6638 if Is_Composite_Type (Target_Typ)
6639 and then not Is_Constrained (Target_Typ)
6641 Set_Etype (N, Etype (Expr));
6644 Eval_Qualified_Expression (N);
6645 end Resolve_Qualified_Expression;
6651 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
6652 L : constant Node_Id := Low_Bound (N);
6653 H : constant Node_Id := High_Bound (N);
6660 Check_Unset_Reference (L);
6661 Check_Unset_Reference (H);
6663 -- We have to check the bounds for being within the base range as
6664 -- required for a non-static context. Normally this is automatic and
6665 -- done as part of evaluating expressions, but the N_Range node is an
6666 -- exception, since in GNAT we consider this node to be a subexpression,
6667 -- even though in Ada it is not. The circuit in Sem_Eval could check for
6668 -- this, but that would put the test on the main evaluation path for
6671 Check_Non_Static_Context (L);
6672 Check_Non_Static_Context (H);
6674 -- Check for an ambiguous range over character literals. This will
6675 -- happen with a membership test involving only literals.
6677 if Typ = Any_Character then
6678 Ambiguous_Character (L);
6679 Set_Etype (N, Any_Type);
6683 -- If bounds are static, constant-fold them, so size computations
6684 -- are identical between front-end and back-end. Do not perform this
6685 -- transformation while analyzing generic units, as type information
6686 -- would then be lost when reanalyzing the constant node in the
6689 if Is_Discrete_Type (Typ) and then Expander_Active then
6690 if Is_OK_Static_Expression (L) then
6691 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
6694 if Is_OK_Static_Expression (H) then
6695 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
6700 --------------------------
6701 -- Resolve_Real_Literal --
6702 --------------------------
6704 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
6705 Actual_Typ : constant Entity_Id := Etype (N);
6708 -- Special processing for fixed-point literals to make sure that the
6709 -- value is an exact multiple of small where this is required. We
6710 -- skip this for the universal real case, and also for generic types.
6712 if Is_Fixed_Point_Type (Typ)
6713 and then Typ /= Universal_Fixed
6714 and then Typ /= Any_Fixed
6715 and then not Is_Generic_Type (Typ)
6718 Val : constant Ureal := Realval (N);
6719 Cintr : constant Ureal := Val / Small_Value (Typ);
6720 Cint : constant Uint := UR_Trunc (Cintr);
6721 Den : constant Uint := Norm_Den (Cintr);
6725 -- Case of literal is not an exact multiple of the Small
6729 -- For a source program literal for a decimal fixed-point
6730 -- type, this is statically illegal (RM 4.9(36)).
6732 if Is_Decimal_Fixed_Point_Type (Typ)
6733 and then Actual_Typ = Universal_Real
6734 and then Comes_From_Source (N)
6736 Error_Msg_N ("value has extraneous low order digits", N);
6739 -- Generate a warning if literal from source
6741 if Is_Static_Expression (N)
6742 and then Warn_On_Bad_Fixed_Value
6745 ("?static fixed-point value is not a multiple of Small!",
6749 -- Replace literal by a value that is the exact representation
6750 -- of a value of the type, i.e. a multiple of the small value,
6751 -- by truncation, since Machine_Rounds is false for all GNAT
6752 -- fixed-point types (RM 4.9(38)).
6754 Stat := Is_Static_Expression (N);
6756 Make_Real_Literal (Sloc (N),
6757 Realval => Small_Value (Typ) * Cint));
6759 Set_Is_Static_Expression (N, Stat);
6762 -- In all cases, set the corresponding integer field
6764 Set_Corresponding_Integer_Value (N, Cint);
6768 -- Now replace the actual type by the expected type as usual
6771 Eval_Real_Literal (N);
6772 end Resolve_Real_Literal;
6774 -----------------------
6775 -- Resolve_Reference --
6776 -----------------------
6778 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
6779 P : constant Node_Id := Prefix (N);
6782 -- Replace general access with specific type
6784 if Ekind (Etype (N)) = E_Allocator_Type then
6785 Set_Etype (N, Base_Type (Typ));
6788 Resolve (P, Designated_Type (Etype (N)));
6790 -- If we are taking the reference of a volatile entity, then treat
6791 -- it as a potential modification of this entity. This is much too
6792 -- conservative, but is necessary because remove side effects can
6793 -- result in transformations of normal assignments into reference
6794 -- sequences that otherwise fail to notice the modification.
6796 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
6797 Note_Possible_Modification (P);
6799 end Resolve_Reference;
6801 --------------------------------
6802 -- Resolve_Selected_Component --
6803 --------------------------------
6805 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
6807 Comp1 : Entity_Id := Empty; -- prevent junk warning
6808 P : constant Node_Id := Prefix (N);
6809 S : constant Node_Id := Selector_Name (N);
6810 T : Entity_Id := Etype (P);
6812 I1 : Interp_Index := 0; -- prevent junk warning
6817 function Init_Component return Boolean;
6818 -- Check whether this is the initialization of a component within an
6819 -- init proc (by assignment or call to another init proc). If true,
6820 -- there is no need for a discriminant check.
6822 --------------------
6823 -- Init_Component --
6824 --------------------
6826 function Init_Component return Boolean is
6828 return Inside_Init_Proc
6829 and then Nkind (Prefix (N)) = N_Identifier
6830 and then Chars (Prefix (N)) = Name_uInit
6831 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
6834 -- Start of processing for Resolve_Selected_Component
6837 if Is_Overloaded (P) then
6839 -- Use the context type to select the prefix that has a selector
6840 -- of the correct name and type.
6843 Get_First_Interp (P, I, It);
6845 Search : while Present (It.Typ) loop
6846 if Is_Access_Type (It.Typ) then
6847 T := Designated_Type (It.Typ);
6852 if Is_Record_Type (T) then
6853 Comp := First_Entity (T);
6854 while Present (Comp) loop
6855 if Chars (Comp) = Chars (S)
6856 and then Covers (Etype (Comp), Typ)
6865 It := Disambiguate (P, I1, I, Any_Type);
6867 if It = No_Interp then
6869 ("ambiguous prefix for selected component", N);
6876 -- There may be an implicit dereference. Retrieve
6877 -- designated record type.
6879 if Is_Access_Type (It1.Typ) then
6880 T := Designated_Type (It1.Typ);
6885 if Scope (Comp1) /= T then
6887 -- Resolution chooses the new interpretation.
6888 -- Find the component with the right name.
6890 Comp1 := First_Entity (T);
6891 while Present (Comp1)
6892 and then Chars (Comp1) /= Chars (S)
6894 Comp1 := Next_Entity (Comp1);
6903 Comp := Next_Entity (Comp);
6908 Get_Next_Interp (I, It);
6911 Resolve (P, It1.Typ);
6913 Set_Entity_With_Style_Check (S, Comp1);
6916 -- Resolve prefix with its type
6921 -- Generate cross-reference. We needed to wait until full overloading
6922 -- resolution was complete to do this, since otherwise we can't tell if
6923 -- we are an Lvalue of not.
6925 if May_Be_Lvalue (N) then
6926 Generate_Reference (Entity (S), S, 'm');
6928 Generate_Reference (Entity (S), S, 'r');
6931 -- If prefix is an access type, the node will be transformed into an
6932 -- explicit dereference during expansion. The type of the node is the
6933 -- designated type of that of the prefix.
6935 if Is_Access_Type (Etype (P)) then
6936 T := Designated_Type (Etype (P));
6937 Check_Fully_Declared_Prefix (T, P);
6942 if Has_Discriminants (T)
6943 and then (Ekind (Entity (S)) = E_Component
6945 Ekind (Entity (S)) = E_Discriminant)
6946 and then Present (Original_Record_Component (Entity (S)))
6947 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
6948 and then Present (Discriminant_Checking_Func
6949 (Original_Record_Component (Entity (S))))
6950 and then not Discriminant_Checks_Suppressed (T)
6951 and then not Init_Component
6953 Set_Do_Discriminant_Check (N);
6956 if Ekind (Entity (S)) = E_Void then
6957 Error_Msg_N ("premature use of component", S);
6960 -- If the prefix is a record conversion, this may be a renamed
6961 -- discriminant whose bounds differ from those of the original
6962 -- one, so we must ensure that a range check is performed.
6964 if Nkind (P) = N_Type_Conversion
6965 and then Ekind (Entity (S)) = E_Discriminant
6966 and then Is_Discrete_Type (Typ)
6968 Set_Etype (N, Base_Type (Typ));
6971 -- Note: No Eval processing is required, because the prefix is of a
6972 -- record type, or protected type, and neither can possibly be static.
6974 end Resolve_Selected_Component;
6980 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
6981 B_Typ : constant Entity_Id := Base_Type (Typ);
6982 L : constant Node_Id := Left_Opnd (N);
6983 R : constant Node_Id := Right_Opnd (N);
6986 -- We do the resolution using the base type, because intermediate values
6987 -- in expressions always are of the base type, not a subtype of it.
6990 Resolve (R, Standard_Natural);
6992 Check_Unset_Reference (L);
6993 Check_Unset_Reference (R);
6995 Set_Etype (N, B_Typ);
6996 Generate_Operator_Reference (N, B_Typ);
7000 ---------------------------
7001 -- Resolve_Short_Circuit --
7002 ---------------------------
7004 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
7005 B_Typ : constant Entity_Id := Base_Type (Typ);
7006 L : constant Node_Id := Left_Opnd (N);
7007 R : constant Node_Id := Right_Opnd (N);
7013 Check_Unset_Reference (L);
7014 Check_Unset_Reference (R);
7016 Set_Etype (N, B_Typ);
7017 Eval_Short_Circuit (N);
7018 end Resolve_Short_Circuit;
7024 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
7025 Name : constant Node_Id := Prefix (N);
7026 Drange : constant Node_Id := Discrete_Range (N);
7027 Array_Type : Entity_Id := Empty;
7031 if Is_Overloaded (Name) then
7033 -- Use the context type to select the prefix that yields the
7034 -- correct array type.
7038 I1 : Interp_Index := 0;
7040 P : constant Node_Id := Prefix (N);
7041 Found : Boolean := False;
7044 Get_First_Interp (P, I, It);
7045 while Present (It.Typ) loop
7046 if (Is_Array_Type (It.Typ)
7047 and then Covers (Typ, It.Typ))
7048 or else (Is_Access_Type (It.Typ)
7049 and then Is_Array_Type (Designated_Type (It.Typ))
7050 and then Covers (Typ, Designated_Type (It.Typ)))
7053 It := Disambiguate (P, I1, I, Any_Type);
7055 if It = No_Interp then
7056 Error_Msg_N ("ambiguous prefix for slicing", N);
7061 Array_Type := It.Typ;
7066 Array_Type := It.Typ;
7071 Get_Next_Interp (I, It);
7076 Array_Type := Etype (Name);
7079 Resolve (Name, Array_Type);
7081 if Is_Access_Type (Array_Type) then
7082 Apply_Access_Check (N);
7083 Array_Type := Designated_Type (Array_Type);
7085 -- If the prefix is an access to an unconstrained array, we must use
7086 -- the actual subtype of the object to perform the index checks. The
7087 -- object denoted by the prefix is implicit in the node, so we build
7088 -- an explicit representation for it in order to compute the actual
7091 if not Is_Constrained (Array_Type) then
7092 Remove_Side_Effects (Prefix (N));
7095 Obj : constant Node_Id :=
7096 Make_Explicit_Dereference (Sloc (N),
7097 Prefix => New_Copy_Tree (Prefix (N)));
7099 Set_Etype (Obj, Array_Type);
7100 Set_Parent (Obj, Parent (N));
7101 Array_Type := Get_Actual_Subtype (Obj);
7105 elsif Is_Entity_Name (Name)
7106 or else (Nkind (Name) = N_Function_Call
7107 and then not Is_Constrained (Etype (Name)))
7109 Array_Type := Get_Actual_Subtype (Name);
7111 -- If the name is a selected component that depends on discriminants,
7112 -- build an actual subtype for it. This can happen only when the name
7113 -- itself is overloaded; otherwise the actual subtype is created when
7114 -- the selected component is analyzed.
7116 elsif Nkind (Name) = N_Selected_Component
7117 and then Full_Analysis
7118 and then Depends_On_Discriminant (First_Index (Array_Type))
7121 Act_Decl : constant Node_Id :=
7122 Build_Actual_Subtype_Of_Component (Array_Type, Name);
7124 Insert_Action (N, Act_Decl);
7125 Array_Type := Defining_Identifier (Act_Decl);
7129 -- If name was overloaded, set slice type correctly now
7131 Set_Etype (N, Array_Type);
7133 -- If the range is specified by a subtype mark, no resolution is
7134 -- necessary. Else resolve the bounds, and apply needed checks.
7136 if not Is_Entity_Name (Drange) then
7137 Index := First_Index (Array_Type);
7138 Resolve (Drange, Base_Type (Etype (Index)));
7140 if Nkind (Drange) = N_Range
7142 -- Do not apply the range check to nodes associated with the
7143 -- frontend expansion of the dispatch table. We first check
7144 -- if Ada.Tags is already loaded to void the addition of an
7145 -- undesired dependence on such run-time unit.
7150 (RTU_Loaded (Ada_Tags)
7151 and then Nkind (Prefix (N)) = N_Selected_Component
7152 and then Present (Entity (Selector_Name (Prefix (N))))
7153 and then Entity (Selector_Name (Prefix (N))) =
7154 RTE_Record_Component (RE_Prims_Ptr)))
7156 Apply_Range_Check (Drange, Etype (Index));
7160 Set_Slice_Subtype (N);
7162 if Nkind (Drange) = N_Range then
7163 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
7164 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
7170 ----------------------------
7171 -- Resolve_String_Literal --
7172 ----------------------------
7174 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
7175 C_Typ : constant Entity_Id := Component_Type (Typ);
7176 R_Typ : constant Entity_Id := Root_Type (C_Typ);
7177 Loc : constant Source_Ptr := Sloc (N);
7178 Str : constant String_Id := Strval (N);
7179 Strlen : constant Nat := String_Length (Str);
7180 Subtype_Id : Entity_Id;
7181 Need_Check : Boolean;
7184 -- For a string appearing in a concatenation, defer creation of the
7185 -- string_literal_subtype until the end of the resolution of the
7186 -- concatenation, because the literal may be constant-folded away. This
7187 -- is a useful optimization for long concatenation expressions.
7189 -- If the string is an aggregate built for a single character (which
7190 -- happens in a non-static context) or a is null string to which special
7191 -- checks may apply, we build the subtype. Wide strings must also get a
7192 -- string subtype if they come from a one character aggregate. Strings
7193 -- generated by attributes might be static, but it is often hard to
7194 -- determine whether the enclosing context is static, so we generate
7195 -- subtypes for them as well, thus losing some rarer optimizations ???
7196 -- Same for strings that come from a static conversion.
7199 (Strlen = 0 and then Typ /= Standard_String)
7200 or else Nkind (Parent (N)) /= N_Op_Concat
7201 or else (N /= Left_Opnd (Parent (N))
7202 and then N /= Right_Opnd (Parent (N)))
7203 or else ((Typ = Standard_Wide_String
7204 or else Typ = Standard_Wide_Wide_String)
7205 and then Nkind (Original_Node (N)) /= N_String_Literal);
7207 -- If the resolving type is itself a string literal subtype, we
7208 -- can just reuse it, since there is no point in creating another.
7210 if Ekind (Typ) = E_String_Literal_Subtype then
7213 elsif Nkind (Parent (N)) = N_Op_Concat
7214 and then not Need_Check
7215 and then Nkind (Original_Node (N)) /= N_Character_Literal
7216 and then Nkind (Original_Node (N)) /= N_Attribute_Reference
7217 and then Nkind (Original_Node (N)) /= N_Qualified_Expression
7218 and then Nkind (Original_Node (N)) /= N_Type_Conversion
7222 -- Otherwise we must create a string literal subtype. Note that the
7223 -- whole idea of string literal subtypes is simply to avoid the need
7224 -- for building a full fledged array subtype for each literal.
7226 Set_String_Literal_Subtype (N, Typ);
7227 Subtype_Id := Etype (N);
7230 if Nkind (Parent (N)) /= N_Op_Concat
7233 Set_Etype (N, Subtype_Id);
7234 Eval_String_Literal (N);
7237 if Is_Limited_Composite (Typ)
7238 or else Is_Private_Composite (Typ)
7240 Error_Msg_N ("string literal not available for private array", N);
7241 Set_Etype (N, Any_Type);
7245 -- The validity of a null string has been checked in the
7246 -- call to Eval_String_Literal.
7251 -- Always accept string literal with component type Any_Character, which
7252 -- occurs in error situations and in comparisons of literals, both of
7253 -- which should accept all literals.
7255 elsif R_Typ = Any_Character then
7258 -- If the type is bit-packed, then we always tranform the string literal
7259 -- into a full fledged aggregate.
7261 elsif Is_Bit_Packed_Array (Typ) then
7264 -- Deal with cases of Wide_Wide_String, Wide_String, and String
7267 -- For Standard.Wide_Wide_String, or any other type whose component
7268 -- type is Standard.Wide_Wide_Character, we know that all the
7269 -- characters in the string must be acceptable, since the parser
7270 -- accepted the characters as valid character literals.
7272 if R_Typ = Standard_Wide_Wide_Character then
7275 -- For the case of Standard.String, or any other type whose component
7276 -- type is Standard.Character, we must make sure that there are no
7277 -- wide characters in the string, i.e. that it is entirely composed
7278 -- of characters in range of type Character.
7280 -- If the string literal is the result of a static concatenation, the
7281 -- test has already been performed on the components, and need not be
7284 elsif R_Typ = Standard_Character
7285 and then Nkind (Original_Node (N)) /= N_Op_Concat
7287 for J in 1 .. Strlen loop
7288 if not In_Character_Range (Get_String_Char (Str, J)) then
7290 -- If we are out of range, post error. This is one of the
7291 -- very few places that we place the flag in the middle of
7292 -- a token, right under the offending wide character.
7295 ("literal out of range of type Standard.Character",
7296 Source_Ptr (Int (Loc) + J));
7301 -- For the case of Standard.Wide_String, or any other type whose
7302 -- component type is Standard.Wide_Character, we must make sure that
7303 -- there are no wide characters in the string, i.e. that it is
7304 -- entirely composed of characters in range of type Wide_Character.
7306 -- If the string literal is the result of a static concatenation,
7307 -- the test has already been performed on the components, and need
7310 elsif R_Typ = Standard_Wide_Character
7311 and then Nkind (Original_Node (N)) /= N_Op_Concat
7313 for J in 1 .. Strlen loop
7314 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
7316 -- If we are out of range, post error. This is one of the
7317 -- very few places that we place the flag in the middle of
7318 -- a token, right under the offending wide character.
7320 -- This is not quite right, because characters in general
7321 -- will take more than one character position ???
7324 ("literal out of range of type Standard.Wide_Character",
7325 Source_Ptr (Int (Loc) + J));
7330 -- If the root type is not a standard character, then we will convert
7331 -- the string into an aggregate and will let the aggregate code do
7332 -- the checking. Standard Wide_Wide_Character is also OK here.
7338 -- See if the component type of the array corresponding to the string
7339 -- has compile time known bounds. If yes we can directly check
7340 -- whether the evaluation of the string will raise constraint error.
7341 -- Otherwise we need to transform the string literal into the
7342 -- corresponding character aggregate and let the aggregate
7343 -- code do the checking.
7345 if R_Typ = Standard_Character
7346 or else R_Typ = Standard_Wide_Character
7347 or else R_Typ = Standard_Wide_Wide_Character
7349 -- Check for the case of full range, where we are definitely OK
7351 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
7355 -- Here the range is not the complete base type range, so check
7358 Comp_Typ_Lo : constant Node_Id :=
7359 Type_Low_Bound (Component_Type (Typ));
7360 Comp_Typ_Hi : constant Node_Id :=
7361 Type_High_Bound (Component_Type (Typ));
7366 if Compile_Time_Known_Value (Comp_Typ_Lo)
7367 and then Compile_Time_Known_Value (Comp_Typ_Hi)
7369 for J in 1 .. Strlen loop
7370 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
7372 if Char_Val < Expr_Value (Comp_Typ_Lo)
7373 or else Char_Val > Expr_Value (Comp_Typ_Hi)
7375 Apply_Compile_Time_Constraint_Error
7376 (N, "character out of range?", CE_Range_Check_Failed,
7377 Loc => Source_Ptr (Int (Loc) + J));
7387 -- If we got here we meed to transform the string literal into the
7388 -- equivalent qualified positional array aggregate. This is rather
7389 -- heavy artillery for this situation, but it is hard work to avoid.
7392 Lits : constant List_Id := New_List;
7393 P : Source_Ptr := Loc + 1;
7397 -- Build the character literals, we give them source locations that
7398 -- correspond to the string positions, which is a bit tricky given
7399 -- the possible presence of wide character escape sequences.
7401 for J in 1 .. Strlen loop
7402 C := Get_String_Char (Str, J);
7403 Set_Character_Literal_Name (C);
7406 Make_Character_Literal (P,
7408 Char_Literal_Value => UI_From_CC (C)));
7410 if In_Character_Range (C) then
7413 -- Should we have a call to Skip_Wide here ???
7421 Make_Qualified_Expression (Loc,
7422 Subtype_Mark => New_Reference_To (Typ, Loc),
7424 Make_Aggregate (Loc, Expressions => Lits)));
7426 Analyze_And_Resolve (N, Typ);
7428 end Resolve_String_Literal;
7430 -----------------------------
7431 -- Resolve_Subprogram_Info --
7432 -----------------------------
7434 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
7437 end Resolve_Subprogram_Info;
7439 -----------------------------
7440 -- Resolve_Type_Conversion --
7441 -----------------------------
7443 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
7444 Conv_OK : constant Boolean := Conversion_OK (N);
7445 Operand : constant Node_Id := Expression (N);
7446 Operand_Typ : constant Entity_Id := Etype (Operand);
7447 Target_Typ : constant Entity_Id := Etype (N);
7454 and then not Valid_Conversion (N, Target_Typ, Operand)
7459 if Etype (Operand) = Any_Fixed then
7461 -- Mixed-mode operation involving a literal. Context must be a fixed
7462 -- type which is applied to the literal subsequently.
7464 if Is_Fixed_Point_Type (Typ) then
7465 Set_Etype (Operand, Universal_Real);
7467 elsif Is_Numeric_Type (Typ)
7468 and then (Nkind (Operand) = N_Op_Multiply
7469 or else Nkind (Operand) = N_Op_Divide)
7470 and then (Etype (Right_Opnd (Operand)) = Universal_Real
7471 or else Etype (Left_Opnd (Operand)) = Universal_Real)
7473 -- Return if expression is ambiguous
7475 if Unique_Fixed_Point_Type (N) = Any_Type then
7478 -- If nothing else, the available fixed type is Duration
7481 Set_Etype (Operand, Standard_Duration);
7484 -- Resolve the real operand with largest available precision
7486 if Etype (Right_Opnd (Operand)) = Universal_Real then
7487 Rop := New_Copy_Tree (Right_Opnd (Operand));
7489 Rop := New_Copy_Tree (Left_Opnd (Operand));
7492 Resolve (Rop, Universal_Real);
7494 -- If the operand is a literal (it could be a non-static and
7495 -- illegal exponentiation) check whether the use of Duration
7496 -- is potentially inaccurate.
7498 if Nkind (Rop) = N_Real_Literal
7499 and then Realval (Rop) /= Ureal_0
7500 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
7503 ("?universal real operand can only " &
7504 "be interpreted as Duration!",
7507 ("\?precision will be lost in the conversion!", Rop);
7510 elsif Is_Numeric_Type (Typ)
7511 and then Nkind (Operand) in N_Op
7512 and then Unique_Fixed_Point_Type (N) /= Any_Type
7514 Set_Etype (Operand, Standard_Duration);
7517 Error_Msg_N ("invalid context for mixed mode operation", N);
7518 Set_Etype (Operand, Any_Type);
7525 -- Note: we do the Eval_Type_Conversion call before applying the
7526 -- required checks for a subtype conversion. This is important,
7527 -- since both are prepared under certain circumstances to change
7528 -- the type conversion to a constraint error node, but in the case
7529 -- of Eval_Type_Conversion this may reflect an illegality in the
7530 -- static case, and we would miss the illegality (getting only a
7531 -- warning message), if we applied the type conversion checks first.
7533 Eval_Type_Conversion (N);
7535 -- Even when evaluation is not possible, we may be able to simplify
7536 -- the conversion or its expression. This needs to be done before
7537 -- applying checks, since otherwise the checks may use the original
7538 -- expression and defeat the simplifications. This is specifically
7539 -- the case for elimination of the floating-point Truncation
7540 -- attribute in float-to-int conversions.
7542 Simplify_Type_Conversion (N);
7544 -- If after evaluation we still have a type conversion, then we
7545 -- may need to apply checks required for a subtype conversion.
7547 -- Skip these type conversion checks if universal fixed operands
7548 -- operands involved, since range checks are handled separately for
7549 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
7551 if Nkind (N) = N_Type_Conversion
7552 and then not Is_Generic_Type (Root_Type (Target_Typ))
7553 and then Target_Typ /= Universal_Fixed
7554 and then Operand_Typ /= Universal_Fixed
7556 Apply_Type_Conversion_Checks (N);
7559 -- Issue warning for conversion of simple object to its own type
7560 -- We have to test the original nodes, since they may have been
7561 -- rewritten by various optimizations.
7563 Orig_N := Original_Node (N);
7565 if Warn_On_Redundant_Constructs
7566 and then Comes_From_Source (Orig_N)
7567 and then Nkind (Orig_N) = N_Type_Conversion
7568 and then not In_Instance
7570 Orig_N := Original_Node (Expression (Orig_N));
7571 Orig_T := Target_Typ;
7573 -- If the node is part of a larger expression, the Target_Type
7574 -- may not be the original type of the node if the context is a
7575 -- condition. Recover original type to see if conversion is needed.
7577 if Is_Boolean_Type (Orig_T)
7578 and then Nkind (Parent (N)) in N_Op
7580 Orig_T := Etype (Parent (N));
7583 if Is_Entity_Name (Orig_N)
7585 (Etype (Entity (Orig_N)) = Orig_T
7587 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
7588 and then Covers (Orig_T, Etype (Entity (Orig_N)))))
7590 Error_Msg_Node_2 := Orig_T;
7592 ("?redundant conversion, & is of type &!", N, Entity (Orig_N));
7596 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
7597 -- No need to perform any interface conversion if the type of the
7598 -- expression coincides with the target type.
7600 if Ada_Version >= Ada_05
7601 and then Expander_Active
7602 and then Operand_Typ /= Target_Typ
7605 Opnd : Entity_Id := Operand_Typ;
7606 Target : Entity_Id := Target_Typ;
7609 if Is_Access_Type (Opnd) then
7610 Opnd := Directly_Designated_Type (Opnd);
7613 if Is_Access_Type (Target_Typ) then
7614 Target := Directly_Designated_Type (Target);
7617 if Opnd = Target then
7620 -- Conversion from interface type
7622 elsif Is_Interface (Opnd) then
7624 -- Ada 2005 (AI-217): Handle entities from limited views
7626 if From_With_Type (Opnd) then
7627 Error_Msg_Qual_Level := 99;
7628 Error_Msg_NE ("missing with-clause on package &", N,
7629 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
7631 ("type conversions require visibility of the full view",
7634 elsif From_With_Type (Target)
7636 (Is_Access_Type (Target_Typ)
7637 and then Present (Non_Limited_View (Etype (Target))))
7639 Error_Msg_Qual_Level := 99;
7640 Error_Msg_NE ("missing with-clause on package &", N,
7641 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
7643 ("type conversions require visibility of the full view",
7647 Expand_Interface_Conversion (N, Is_Static => False);
7650 -- Conversion to interface type
7652 elsif Is_Interface (Target) then
7656 if Ekind (Opnd) = E_Protected_Subtype
7657 or else Ekind (Opnd) = E_Task_Subtype
7659 Opnd := Etype (Opnd);
7662 if not Interface_Present_In_Ancestor
7666 if Is_Class_Wide_Type (Opnd) then
7668 -- The static analysis is not enough to know if the
7669 -- interface is implemented or not. Hence we must pass
7670 -- the work to the expander to generate code to evaluate
7671 -- the conversion at run-time.
7673 Expand_Interface_Conversion (N, Is_Static => False);
7676 Error_Msg_Name_1 := Chars (Etype (Target));
7677 Error_Msg_Name_2 := Chars (Opnd);
7679 ("wrong interface conversion (% is not a progenitor " &
7684 Expand_Interface_Conversion (N);
7689 end Resolve_Type_Conversion;
7691 ----------------------
7692 -- Resolve_Unary_Op --
7693 ----------------------
7695 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
7696 B_Typ : constant Entity_Id := Base_Type (Typ);
7697 R : constant Node_Id := Right_Opnd (N);
7703 -- Deal with intrinsic unary operators
7705 if Comes_From_Source (N)
7706 and then Ekind (Entity (N)) = E_Function
7707 and then Is_Imported (Entity (N))
7708 and then Is_Intrinsic_Subprogram (Entity (N))
7710 Resolve_Intrinsic_Unary_Operator (N, Typ);
7714 -- Deal with universal cases
7716 if Etype (R) = Universal_Integer
7718 Etype (R) = Universal_Real
7720 Check_For_Visible_Operator (N, B_Typ);
7723 Set_Etype (N, B_Typ);
7726 -- Generate warning for expressions like abs (x mod 2)
7728 if Warn_On_Redundant_Constructs
7729 and then Nkind (N) = N_Op_Abs
7731 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
7733 if OK and then Hi >= Lo and then Lo >= 0 then
7735 ("?abs applied to known non-negative value has no effect", N);
7739 -- Deal with reference generation
7741 Check_Unset_Reference (R);
7742 Generate_Operator_Reference (N, B_Typ);
7745 -- Set overflow checking bit. Much cleverer code needed here eventually
7746 -- and perhaps the Resolve routines should be separated for the various
7747 -- arithmetic operations, since they will need different processing ???
7749 if Nkind (N) in N_Op then
7750 if not Overflow_Checks_Suppressed (Etype (N)) then
7751 Enable_Overflow_Check (N);
7755 -- Generate warning for expressions like -5 mod 3 for integers. No
7756 -- need to worry in the floating-point case, since parens do not affect
7757 -- the result so there is no point in giving in a warning.
7760 Norig : constant Node_Id := Original_Node (N);
7769 if Warn_On_Questionable_Missing_Parens
7770 and then Comes_From_Source (Norig)
7771 and then Is_Integer_Type (Typ)
7772 and then Nkind (Norig) = N_Op_Minus
7774 Rorig := Original_Node (Right_Opnd (Norig));
7776 -- We are looking for cases where the right operand is not
7777 -- parenthesized, and is a bianry operator, multiply, divide, or
7778 -- mod. These are the cases where the grouping can affect results.
7780 if Paren_Count (Rorig) = 0
7781 and then (Nkind (Rorig) = N_Op_Mod
7783 Nkind (Rorig) = N_Op_Multiply
7785 Nkind (Rorig) = N_Op_Divide)
7787 -- For mod, we always give the warning, since the value is
7788 -- affected by the parenthesization (e.g. (-5) mod 315 /=
7789 -- (5 mod 315)). But for the other cases, the only concern is
7790 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
7791 -- overflows, but (-2) * 64 does not). So we try to give the
7792 -- message only when overflow is possible.
7794 if Nkind (Rorig) /= N_Op_Mod
7795 and then Compile_Time_Known_Value (R)
7797 Val := Expr_Value (R);
7799 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7800 HB := Expr_Value (Type_High_Bound (Typ));
7802 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
7805 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7806 LB := Expr_Value (Type_Low_Bound (Typ));
7808 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
7811 -- Note that the test below is deliberately excluding
7812 -- the largest negative number, since that is a potentially
7813 -- troublesome case (e.g. -2 * x, where the result is the
7814 -- largest negative integer has an overflow with 2 * x).
7816 if Val > LB and then Val <= HB then
7821 -- For the multiplication case, the only case we have to worry
7822 -- about is when (-a)*b is exactly the largest negative number
7823 -- so that -(a*b) can cause overflow. This can only happen if
7824 -- a is a power of 2, and more generally if any operand is a
7825 -- constant that is not a power of 2, then the parentheses
7826 -- cannot affect whether overflow occurs. We only bother to
7827 -- test the left most operand
7829 -- Loop looking at left operands for one that has known value
7832 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
7833 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
7834 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
7836 -- Operand value of 0 or 1 skips warning
7841 -- Otherwise check power of 2, if power of 2, warn, if
7842 -- anything else, skip warning.
7845 while Lval /= 2 loop
7846 if Lval mod 2 = 1 then
7857 -- Keep looking at left operands
7859 Opnd := Left_Opnd (Opnd);
7862 -- For rem or "/" we can only have a problematic situation
7863 -- if the divisor has a value of minus one or one. Otherwise
7864 -- overflow is impossible (divisor > 1) or we have a case of
7865 -- division by zero in any case.
7867 if (Nkind (Rorig) = N_Op_Divide
7869 Nkind (Rorig) = N_Op_Rem)
7870 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
7871 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
7876 -- If we fall through warning should be issued
7879 ("?unary minus expression should be parenthesized here!", N);
7883 end Resolve_Unary_Op;
7885 ----------------------------------
7886 -- Resolve_Unchecked_Expression --
7887 ----------------------------------
7889 procedure Resolve_Unchecked_Expression
7894 Resolve (Expression (N), Typ, Suppress => All_Checks);
7896 end Resolve_Unchecked_Expression;
7898 ---------------------------------------
7899 -- Resolve_Unchecked_Type_Conversion --
7900 ---------------------------------------
7902 procedure Resolve_Unchecked_Type_Conversion
7906 pragma Warnings (Off, Typ);
7908 Operand : constant Node_Id := Expression (N);
7909 Opnd_Type : constant Entity_Id := Etype (Operand);
7912 -- Resolve operand using its own type
7914 Resolve (Operand, Opnd_Type);
7915 Eval_Unchecked_Conversion (N);
7917 end Resolve_Unchecked_Type_Conversion;
7919 ------------------------------
7920 -- Rewrite_Operator_As_Call --
7921 ------------------------------
7923 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
7924 Loc : constant Source_Ptr := Sloc (N);
7925 Actuals : constant List_Id := New_List;
7929 if Nkind (N) in N_Binary_Op then
7930 Append (Left_Opnd (N), Actuals);
7933 Append (Right_Opnd (N), Actuals);
7936 Make_Function_Call (Sloc => Loc,
7937 Name => New_Occurrence_Of (Nam, Loc),
7938 Parameter_Associations => Actuals);
7940 Preserve_Comes_From_Source (New_N, N);
7941 Preserve_Comes_From_Source (Name (New_N), N);
7943 Set_Etype (N, Etype (Nam));
7944 end Rewrite_Operator_As_Call;
7946 ------------------------------
7947 -- Rewrite_Renamed_Operator --
7948 ------------------------------
7950 procedure Rewrite_Renamed_Operator
7955 Nam : constant Name_Id := Chars (Op);
7956 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
7960 -- Rewrite the operator node using the real operator, not its
7961 -- renaming. Exclude user-defined intrinsic operations of the same
7962 -- name, which are treated separately and rewritten as calls.
7964 if Ekind (Op) /= E_Function
7965 or else Chars (N) /= Nam
7967 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
7968 Set_Chars (Op_Node, Nam);
7969 Set_Etype (Op_Node, Etype (N));
7970 Set_Entity (Op_Node, Op);
7971 Set_Right_Opnd (Op_Node, Right_Opnd (N));
7973 -- Indicate that both the original entity and its renaming are
7974 -- referenced at this point.
7976 Generate_Reference (Entity (N), N);
7977 Generate_Reference (Op, N);
7980 Set_Left_Opnd (Op_Node, Left_Opnd (N));
7983 Rewrite (N, Op_Node);
7985 -- If the context type is private, add the appropriate conversions
7986 -- so that the operator is applied to the full view. This is done
7987 -- in the routines that resolve intrinsic operators,
7989 if Is_Intrinsic_Subprogram (Op)
7990 and then Is_Private_Type (Typ)
7993 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
7994 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
7995 Resolve_Intrinsic_Operator (N, Typ);
7997 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
7998 Resolve_Intrinsic_Unary_Operator (N, Typ);
8005 elsif Ekind (Op) = E_Function
8006 and then Is_Intrinsic_Subprogram (Op)
8008 -- Operator renames a user-defined operator of the same name. Use
8009 -- the original operator in the node, which is the one that Gigi
8013 Set_Is_Overloaded (N, False);
8015 end Rewrite_Renamed_Operator;
8017 -----------------------
8018 -- Set_Slice_Subtype --
8019 -----------------------
8021 -- Build an implicit subtype declaration to represent the type delivered
8022 -- by the slice. This is an abbreviated version of an array subtype. We
8023 -- define an index subtype for the slice, using either the subtype name
8024 -- or the discrete range of the slice. To be consistent with index usage
8025 -- elsewhere, we create a list header to hold the single index. This list
8026 -- is not otherwise attached to the syntax tree.
8028 procedure Set_Slice_Subtype (N : Node_Id) is
8029 Loc : constant Source_Ptr := Sloc (N);
8030 Index_List : constant List_Id := New_List;
8032 Index_Subtype : Entity_Id;
8033 Index_Type : Entity_Id;
8034 Slice_Subtype : Entity_Id;
8035 Drange : constant Node_Id := Discrete_Range (N);
8038 if Is_Entity_Name (Drange) then
8039 Index_Subtype := Entity (Drange);
8042 -- We force the evaluation of a range. This is definitely needed in
8043 -- the renamed case, and seems safer to do unconditionally. Note in
8044 -- any case that since we will create and insert an Itype referring
8045 -- to this range, we must make sure any side effect removal actions
8046 -- are inserted before the Itype definition.
8048 if Nkind (Drange) = N_Range then
8049 Force_Evaluation (Low_Bound (Drange));
8050 Force_Evaluation (High_Bound (Drange));
8053 Index_Type := Base_Type (Etype (Drange));
8055 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8057 Set_Scalar_Range (Index_Subtype, Drange);
8058 Set_Etype (Index_Subtype, Index_Type);
8059 Set_Size_Info (Index_Subtype, Index_Type);
8060 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
8063 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
8065 Index := New_Occurrence_Of (Index_Subtype, Loc);
8066 Set_Etype (Index, Index_Subtype);
8067 Append (Index, Index_List);
8069 Set_First_Index (Slice_Subtype, Index);
8070 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
8071 Set_Is_Constrained (Slice_Subtype, True);
8072 Init_Size_Align (Slice_Subtype);
8074 Check_Compile_Time_Size (Slice_Subtype);
8076 -- The Etype of the existing Slice node is reset to this slice subtype.
8077 -- Its bounds are obtained from its first index.
8079 Set_Etype (N, Slice_Subtype);
8081 -- In the packed case, this must be immediately frozen
8083 -- Couldn't we always freeze here??? and if we did, then the above
8084 -- call to Check_Compile_Time_Size could be eliminated, which would
8085 -- be nice, because then that routine could be made private to Freeze.
8087 if Is_Packed (Slice_Subtype) and not In_Default_Expression then
8088 Freeze_Itype (Slice_Subtype, N);
8091 end Set_Slice_Subtype;
8093 --------------------------------
8094 -- Set_String_Literal_Subtype --
8095 --------------------------------
8097 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
8098 Loc : constant Source_Ptr := Sloc (N);
8099 Low_Bound : constant Node_Id :=
8100 Type_Low_Bound (Etype (First_Index (Typ)));
8101 Subtype_Id : Entity_Id;
8104 if Nkind (N) /= N_String_Literal then
8108 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
8109 Set_String_Literal_Length (Subtype_Id, UI_From_Int
8110 (String_Length (Strval (N))));
8111 Set_Etype (Subtype_Id, Base_Type (Typ));
8112 Set_Is_Constrained (Subtype_Id);
8113 Set_Etype (N, Subtype_Id);
8115 if Is_OK_Static_Expression (Low_Bound) then
8117 -- The low bound is set from the low bound of the corresponding
8118 -- index type. Note that we do not store the high bound in the
8119 -- string literal subtype, but it can be deduced if necessary
8120 -- from the length and the low bound.
8122 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
8125 Set_String_Literal_Low_Bound
8126 (Subtype_Id, Make_Integer_Literal (Loc, 1));
8127 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
8129 -- Build bona fide subtype for the string, and wrap it in an
8130 -- unchecked conversion, because the backend expects the
8131 -- String_Literal_Subtype to have a static lower bound.
8134 Index_List : constant List_Id := New_List;
8135 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
8136 High_Bound : constant Node_Id :=
8138 Left_Opnd => New_Copy_Tree (Low_Bound),
8140 Make_Integer_Literal (Loc,
8141 String_Length (Strval (N)) - 1));
8142 Array_Subtype : Entity_Id;
8143 Index_Subtype : Entity_Id;
8149 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8150 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
8151 Set_Scalar_Range (Index_Subtype, Drange);
8152 Set_Parent (Drange, N);
8153 Analyze_And_Resolve (Drange, Index_Type);
8155 Set_Etype (Index_Subtype, Index_Type);
8156 Set_Size_Info (Index_Subtype, Index_Type);
8157 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
8159 Array_Subtype := Create_Itype (E_Array_Subtype, N);
8161 Index := New_Occurrence_Of (Index_Subtype, Loc);
8162 Set_Etype (Index, Index_Subtype);
8163 Append (Index, Index_List);
8165 Set_First_Index (Array_Subtype, Index);
8166 Set_Etype (Array_Subtype, Base_Type (Typ));
8167 Set_Is_Constrained (Array_Subtype, True);
8168 Init_Size_Align (Array_Subtype);
8171 Make_Unchecked_Type_Conversion (Loc,
8172 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
8173 Expression => Relocate_Node (N)));
8174 Set_Etype (N, Array_Subtype);
8177 end Set_String_Literal_Subtype;
8179 ------------------------------
8180 -- Simplify_Type_Conversion --
8181 ------------------------------
8183 procedure Simplify_Type_Conversion (N : Node_Id) is
8185 if Nkind (N) = N_Type_Conversion then
8187 Operand : constant Node_Id := Expression (N);
8188 Target_Typ : constant Entity_Id := Etype (N);
8189 Opnd_Typ : constant Entity_Id := Etype (Operand);
8192 if Is_Floating_Point_Type (Opnd_Typ)
8194 (Is_Integer_Type (Target_Typ)
8195 or else (Is_Fixed_Point_Type (Target_Typ)
8196 and then Conversion_OK (N)))
8197 and then Nkind (Operand) = N_Attribute_Reference
8198 and then Attribute_Name (Operand) = Name_Truncation
8200 -- Special processing required if the conversion is the expression
8201 -- of a Truncation attribute reference. In this case we replace:
8203 -- ityp (ftyp'Truncation (x))
8209 -- with the Float_Truncate flag set, which is more efficient
8213 Relocate_Node (First (Expressions (Operand))));
8214 Set_Float_Truncate (N, True);
8218 end Simplify_Type_Conversion;
8220 -----------------------------
8221 -- Unique_Fixed_Point_Type --
8222 -----------------------------
8224 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
8225 T1 : Entity_Id := Empty;
8230 procedure Fixed_Point_Error;
8231 -- If true ambiguity, give details
8233 -----------------------
8234 -- Fixed_Point_Error --
8235 -----------------------
8237 procedure Fixed_Point_Error is
8239 Error_Msg_N ("ambiguous universal_fixed_expression", N);
8240 Error_Msg_NE ("\\possible interpretation as}", N, T1);
8241 Error_Msg_NE ("\\possible interpretation as}", N, T2);
8242 end Fixed_Point_Error;
8244 -- Start of processing for Unique_Fixed_Point_Type
8247 -- The operations on Duration are visible, so Duration is always a
8248 -- possible interpretation.
8250 T1 := Standard_Duration;
8252 -- Look for fixed-point types in enclosing scopes
8254 Scop := Current_Scope;
8255 while Scop /= Standard_Standard loop
8256 T2 := First_Entity (Scop);
8257 while Present (T2) loop
8258 if Is_Fixed_Point_Type (T2)
8259 and then Current_Entity (T2) = T2
8260 and then Scope (Base_Type (T2)) = Scop
8262 if Present (T1) then
8273 Scop := Scope (Scop);
8276 -- Look for visible fixed type declarations in the context
8278 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
8279 while Present (Item) loop
8280 if Nkind (Item) = N_With_Clause then
8281 Scop := Entity (Name (Item));
8282 T2 := First_Entity (Scop);
8283 while Present (T2) loop
8284 if Is_Fixed_Point_Type (T2)
8285 and then Scope (Base_Type (T2)) = Scop
8286 and then (Is_Potentially_Use_Visible (T2)
8287 or else In_Use (T2))
8289 if Present (T1) then
8304 if Nkind (N) = N_Real_Literal then
8305 Error_Msg_NE ("?real literal interpreted as }!", N, T1);
8308 Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
8312 end Unique_Fixed_Point_Type;
8314 ----------------------
8315 -- Valid_Conversion --
8316 ----------------------
8318 function Valid_Conversion
8321 Operand : Node_Id) return Boolean
8323 Target_Type : constant Entity_Id := Base_Type (Target);
8324 Opnd_Type : Entity_Id := Etype (Operand);
8326 function Conversion_Check
8328 Msg : String) return Boolean;
8329 -- Little routine to post Msg if Valid is False, returns Valid value
8331 function Valid_Tagged_Conversion
8332 (Target_Type : Entity_Id;
8333 Opnd_Type : Entity_Id) return Boolean;
8334 -- Specifically test for validity of tagged conversions
8336 function Valid_Array_Conversion return Boolean;
8337 -- Check index and component conformance, and accessibility levels
8338 -- if the component types are anonymous access types (Ada 2005)
8340 ----------------------
8341 -- Conversion_Check --
8342 ----------------------
8344 function Conversion_Check
8346 Msg : String) return Boolean
8350 Error_Msg_N (Msg, Operand);
8354 end Conversion_Check;
8356 ----------------------------
8357 -- Valid_Array_Conversion --
8358 ----------------------------
8360 function Valid_Array_Conversion return Boolean
8362 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
8363 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
8365 Opnd_Index : Node_Id;
8366 Opnd_Index_Type : Entity_Id;
8368 Target_Comp_Type : constant Entity_Id :=
8369 Component_Type (Target_Type);
8370 Target_Comp_Base : constant Entity_Id :=
8371 Base_Type (Target_Comp_Type);
8373 Target_Index : Node_Id;
8374 Target_Index_Type : Entity_Id;
8377 -- Error if wrong number of dimensions
8380 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
8383 ("incompatible number of dimensions for conversion", Operand);
8386 -- Number of dimensions matches
8389 -- Loop through indexes of the two arrays
8391 Target_Index := First_Index (Target_Type);
8392 Opnd_Index := First_Index (Opnd_Type);
8393 while Present (Target_Index) and then Present (Opnd_Index) loop
8394 Target_Index_Type := Etype (Target_Index);
8395 Opnd_Index_Type := Etype (Opnd_Index);
8397 -- Error if index types are incompatible
8399 if not (Is_Integer_Type (Target_Index_Type)
8400 and then Is_Integer_Type (Opnd_Index_Type))
8401 and then (Root_Type (Target_Index_Type)
8402 /= Root_Type (Opnd_Index_Type))
8405 ("incompatible index types for array conversion",
8410 Next_Index (Target_Index);
8411 Next_Index (Opnd_Index);
8414 -- If component types have same base type, all set
8416 if Target_Comp_Base = Opnd_Comp_Base then
8419 -- Here if base types of components are not the same. The only
8420 -- time this is allowed is if we have anonymous access types.
8422 -- The conversion of arrays of anonymous access types can lead
8423 -- to dangling pointers. AI-392 formalizes the accessibility
8424 -- checks that must be applied to such conversions to prevent
8425 -- out-of-scope references.
8428 (Ekind (Target_Comp_Base) = E_Anonymous_Access_Type
8430 Ekind (Target_Comp_Base) = E_Anonymous_Access_Subprogram_Type)
8431 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
8433 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
8435 if Type_Access_Level (Target_Type) <
8436 Type_Access_Level (Opnd_Type)
8438 if In_Instance_Body then
8439 Error_Msg_N ("?source array type " &
8440 "has deeper accessibility level than target", Operand);
8441 Error_Msg_N ("\?Program_Error will be raised at run time",
8444 Make_Raise_Program_Error (Sloc (N),
8445 Reason => PE_Accessibility_Check_Failed));
8446 Set_Etype (N, Target_Type);
8449 -- Conversion not allowed because of accessibility levels
8452 Error_Msg_N ("source array type " &
8453 "has deeper accessibility level than target", Operand);
8460 -- All other cases where component base types do not match
8464 ("incompatible component types for array conversion",
8469 -- Check that component subtypes statically match
8471 if Is_Constrained (Target_Comp_Type) /=
8472 Is_Constrained (Opnd_Comp_Type)
8473 or else not Subtypes_Statically_Match
8474 (Target_Comp_Type, Opnd_Comp_Type)
8477 ("component subtypes must statically match", Operand);
8483 end Valid_Array_Conversion;
8485 -----------------------------
8486 -- Valid_Tagged_Conversion --
8487 -----------------------------
8489 function Valid_Tagged_Conversion
8490 (Target_Type : Entity_Id;
8491 Opnd_Type : Entity_Id) return Boolean
8494 -- Upward conversions are allowed (RM 4.6(22))
8496 if Covers (Target_Type, Opnd_Type)
8497 or else Is_Ancestor (Target_Type, Opnd_Type)
8501 -- Downward conversion are allowed if the operand is class-wide
8504 elsif Is_Class_Wide_Type (Opnd_Type)
8505 and then Covers (Opnd_Type, Target_Type)
8509 elsif Covers (Opnd_Type, Target_Type)
8510 or else Is_Ancestor (Opnd_Type, Target_Type)
8513 Conversion_Check (False,
8514 "downward conversion of tagged objects not allowed");
8516 -- Ada 2005 (AI-251): The conversion to/from interface types is
8519 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
8522 -- If the operand is a class-wide type obtained through a limited_
8523 -- with clause, and the context includes the non-limited view, use
8524 -- it to determine whether the conversion is legal.
8526 elsif Is_Class_Wide_Type (Opnd_Type)
8527 and then From_With_Type (Opnd_Type)
8528 and then Present (Non_Limited_View (Etype (Opnd_Type)))
8529 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
8533 elsif Is_Access_Type (Opnd_Type)
8534 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
8540 ("invalid tagged conversion, not compatible with}",
8541 N, First_Subtype (Opnd_Type));
8544 end Valid_Tagged_Conversion;
8546 -- Start of processing for Valid_Conversion
8549 Check_Parameterless_Call (Operand);
8551 if Is_Overloaded (Operand) then
8560 -- Remove procedure calls, which syntactically cannot appear
8561 -- in this context, but which cannot be removed by type checking,
8562 -- because the context does not impose a type.
8564 -- When compiling for VMS, spurious ambiguities can be produced
8565 -- when arithmetic operations have a literal operand and return
8566 -- System.Address or a descendant of it. These ambiguities are
8567 -- otherwise resolved by the context, but for conversions there
8568 -- is no context type and the removal of the spurious operations
8569 -- must be done explicitly here.
8571 -- The node may be labelled overloaded, but still contain only
8572 -- one interpretation because others were discarded in previous
8573 -- filters. If this is the case, retain the single interpretation
8576 Get_First_Interp (Operand, I, It);
8577 Opnd_Type := It.Typ;
8578 Get_Next_Interp (I, It);
8581 and then Opnd_Type /= Standard_Void_Type
8583 -- More than one candidate interpretation is available
8585 Get_First_Interp (Operand, I, It);
8586 while Present (It.Typ) loop
8587 if It.Typ = Standard_Void_Type then
8591 if Present (System_Aux_Id)
8592 and then Is_Descendent_Of_Address (It.Typ)
8597 Get_Next_Interp (I, It);
8601 Get_First_Interp (Operand, I, It);
8606 Error_Msg_N ("illegal operand in conversion", Operand);
8610 Get_Next_Interp (I, It);
8612 if Present (It.Typ) then
8614 It1 := Disambiguate (Operand, I1, I, Any_Type);
8616 if It1 = No_Interp then
8617 Error_Msg_N ("ambiguous operand in conversion", Operand);
8619 Error_Msg_Sloc := Sloc (It.Nam);
8620 Error_Msg_N ("\\possible interpretation#!", Operand);
8622 Error_Msg_Sloc := Sloc (N1);
8623 Error_Msg_N ("\\possible interpretation#!", Operand);
8629 Set_Etype (Operand, It1.Typ);
8630 Opnd_Type := It1.Typ;
8636 if Is_Numeric_Type (Target_Type) then
8638 -- A universal fixed expression can be converted to any numeric type
8640 if Opnd_Type = Universal_Fixed then
8643 -- Also no need to check when in an instance or inlined body, because
8644 -- the legality has been established when the template was analyzed.
8645 -- Furthermore, numeric conversions may occur where only a private
8646 -- view of the operand type is visible at the instanciation point.
8647 -- This results in a spurious error if we check that the operand type
8648 -- is a numeric type.
8650 -- Note: in a previous version of this unit, the following tests were
8651 -- applied only for generated code (Comes_From_Source set to False),
8652 -- but in fact the test is required for source code as well, since
8653 -- this situation can arise in source code.
8655 elsif In_Instance or else In_Inlined_Body then
8658 -- Otherwise we need the conversion check
8661 return Conversion_Check
8662 (Is_Numeric_Type (Opnd_Type),
8663 "illegal operand for numeric conversion");
8668 elsif Is_Array_Type (Target_Type) then
8669 if not Is_Array_Type (Opnd_Type)
8670 or else Opnd_Type = Any_Composite
8671 or else Opnd_Type = Any_String
8674 ("illegal operand for array conversion", Operand);
8677 return Valid_Array_Conversion;
8680 -- Anonymous access types where target references an interface
8682 elsif (Ekind (Target_Type) = E_General_Access_Type
8684 Ekind (Target_Type) = E_Anonymous_Access_Type)
8685 and then Is_Interface (Directly_Designated_Type (Target_Type))
8687 -- Check the static accessibility rule of 4.6(17). Note that the
8688 -- check is not enforced when within an instance body, since the RM
8689 -- requires such cases to be caught at run time.
8691 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
8692 if Type_Access_Level (Opnd_Type) >
8693 Type_Access_Level (Target_Type)
8695 -- In an instance, this is a run-time check, but one we know
8696 -- will fail, so generate an appropriate warning. The raise
8697 -- will be generated by Expand_N_Type_Conversion.
8699 if In_Instance_Body then
8701 ("?cannot convert local pointer to non-local access type",
8704 ("\?Program_Error will be raised at run time", Operand);
8707 ("cannot convert local pointer to non-local access type",
8712 -- Special accessibility checks are needed in the case of access
8713 -- discriminants declared for a limited type.
8715 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
8716 and then not Is_Local_Anonymous_Access (Opnd_Type)
8718 -- When the operand is a selected access discriminant the check
8719 -- needs to be made against the level of the object denoted by
8720 -- the prefix of the selected name. (Object_Access_Level
8721 -- handles checking the prefix of the operand for this case.)
8723 if Nkind (Operand) = N_Selected_Component
8724 and then Object_Access_Level (Operand) >
8725 Type_Access_Level (Target_Type)
8727 -- In an instance, this is a run-time check, but one we
8728 -- know will fail, so generate an appropriate warning.
8729 -- The raise will be generated by Expand_N_Type_Conversion.
8731 if In_Instance_Body then
8733 ("?cannot convert access discriminant to non-local" &
8734 " access type", Operand);
8736 ("\?Program_Error will be raised at run time", Operand);
8739 ("cannot convert access discriminant to non-local" &
8740 " access type", Operand);
8745 -- The case of a reference to an access discriminant from
8746 -- within a limited type declaration (which will appear as
8747 -- a discriminal) is always illegal because the level of the
8748 -- discriminant is considered to be deeper than any (namable)
8751 if Is_Entity_Name (Operand)
8752 and then not Is_Local_Anonymous_Access (Opnd_Type)
8753 and then (Ekind (Entity (Operand)) = E_In_Parameter
8754 or else Ekind (Entity (Operand)) = E_Constant)
8755 and then Present (Discriminal_Link (Entity (Operand)))
8758 ("discriminant has deeper accessibility level than target",
8767 -- General and anonymous access types
8769 elsif (Ekind (Target_Type) = E_General_Access_Type
8770 or else Ekind (Target_Type) = E_Anonymous_Access_Type)
8773 (Is_Access_Type (Opnd_Type)
8774 and then Ekind (Opnd_Type) /=
8775 E_Access_Subprogram_Type
8776 and then Ekind (Opnd_Type) /=
8777 E_Access_Protected_Subprogram_Type,
8778 "must be an access-to-object type")
8780 if Is_Access_Constant (Opnd_Type)
8781 and then not Is_Access_Constant (Target_Type)
8784 ("access-to-constant operand type not allowed", Operand);
8788 -- Check the static accessibility rule of 4.6(17). Note that the
8789 -- check is not enforced when within an instance body, since the RM
8790 -- requires such cases to be caught at run time.
8792 if Ekind (Target_Type) /= E_Anonymous_Access_Type
8793 or else Is_Local_Anonymous_Access (Target_Type)
8795 if Type_Access_Level (Opnd_Type)
8796 > Type_Access_Level (Target_Type)
8798 -- In an instance, this is a run-time check, but one we
8799 -- know will fail, so generate an appropriate warning.
8800 -- The raise will be generated by Expand_N_Type_Conversion.
8802 if In_Instance_Body then
8804 ("?cannot convert local pointer to non-local access type",
8807 ("\?Program_Error will be raised at run time", Operand);
8810 -- Avoid generation of spurious error message
8812 if not Error_Posted (N) then
8814 ("cannot convert local pointer to non-local access type",
8821 -- Special accessibility checks are needed in the case of access
8822 -- discriminants declared for a limited type.
8824 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
8825 and then not Is_Local_Anonymous_Access (Opnd_Type)
8828 -- When the operand is a selected access discriminant the check
8829 -- needs to be made against the level of the object denoted by
8830 -- the prefix of the selected name. (Object_Access_Level
8831 -- handles checking the prefix of the operand for this case.)
8833 if Nkind (Operand) = N_Selected_Component
8834 and then Object_Access_Level (Operand)
8835 > Type_Access_Level (Target_Type)
8837 -- In an instance, this is a run-time check, but one we
8838 -- know will fail, so generate an appropriate warning.
8839 -- The raise will be generated by Expand_N_Type_Conversion.
8841 if In_Instance_Body then
8843 ("?cannot convert access discriminant to non-local" &
8844 " access type", Operand);
8846 ("\?Program_Error will be raised at run time",
8851 ("cannot convert access discriminant to non-local" &
8852 " access type", Operand);
8857 -- The case of a reference to an access discriminant from
8858 -- within a limited type declaration (which will appear as
8859 -- a discriminal) is always illegal because the level of the
8860 -- discriminant is considered to be deeper than any (namable)
8863 if Is_Entity_Name (Operand)
8864 and then (Ekind (Entity (Operand)) = E_In_Parameter
8865 or else Ekind (Entity (Operand)) = E_Constant)
8866 and then Present (Discriminal_Link (Entity (Operand)))
8869 ("discriminant has deeper accessibility level than target",
8877 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
8878 -- Helper function to handle limited views
8880 --------------------------
8881 -- Full_Designated_Type --
8882 --------------------------
8884 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
8885 Desig : constant Entity_Id := Designated_Type (T);
8887 if From_With_Type (Desig)
8888 and then Is_Incomplete_Type (Desig)
8889 and then Present (Non_Limited_View (Desig))
8891 return Non_Limited_View (Desig);
8895 end Full_Designated_Type;
8897 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
8898 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
8900 Same_Base : constant Boolean :=
8901 Base_Type (Target) = Base_Type (Opnd);
8904 if Is_Tagged_Type (Target) then
8905 return Valid_Tagged_Conversion (Target, Opnd);
8908 if not Same_Base then
8910 ("target designated type not compatible with }",
8911 N, Base_Type (Opnd));
8914 -- Ada 2005 AI-384: legality rule is symmetric in both
8915 -- designated types. The conversion is legal (with possible
8916 -- constraint check) if either designated type is
8919 elsif Subtypes_Statically_Match (Target, Opnd)
8921 (Has_Discriminants (Target)
8923 (not Is_Constrained (Opnd)
8924 or else not Is_Constrained (Target)))
8930 ("target designated subtype not compatible with }",
8937 -- Subprogram access types
8939 elsif (Ekind (Target_Type) = E_Access_Subprogram_Type
8941 Ekind (Target_Type) = E_Anonymous_Access_Subprogram_Type)
8942 and then No (Corresponding_Remote_Type (Opnd_Type))
8945 Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
8948 ("illegal attempt to store anonymous access to subprogram",
8951 ("\value has deeper accessibility than any master " &
8955 if Is_Entity_Name (Operand)
8956 and then Ekind (Entity (Operand)) = E_In_Parameter
8959 ("\use named access type for& instead of access parameter",
8960 Operand, Entity (Operand));
8964 -- Check that the designated types are subtype conformant
8966 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
8967 Old_Id => Designated_Type (Opnd_Type),
8970 -- Check the static accessibility rule of 4.6(20)
8972 if Type_Access_Level (Opnd_Type) >
8973 Type_Access_Level (Target_Type)
8976 ("operand type has deeper accessibility level than target",
8979 -- Check that if the operand type is declared in a generic body,
8980 -- then the target type must be declared within that same body
8981 -- (enforces last sentence of 4.6(20)).
8983 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
8985 O_Gen : constant Node_Id :=
8986 Enclosing_Generic_Body (Opnd_Type);
8991 T_Gen := Enclosing_Generic_Body (Target_Type);
8992 while Present (T_Gen) and then T_Gen /= O_Gen loop
8993 T_Gen := Enclosing_Generic_Body (T_Gen);
8996 if T_Gen /= O_Gen then
8998 ("target type must be declared in same generic body"
8999 & " as operand type", N);
9006 -- Remote subprogram access types
9008 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
9009 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
9011 -- It is valid to convert from one RAS type to another provided
9012 -- that their specification statically match.
9014 Check_Subtype_Conformant
9016 Designated_Type (Corresponding_Remote_Type (Target_Type)),
9018 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
9025 elsif Is_Tagged_Type (Target_Type) then
9026 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
9028 -- Types derived from the same root type are convertible
9030 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
9033 -- In an instance or an inlined body, there may be inconsistent
9034 -- views of the same type, or of types derived from a common root.
9036 elsif (In_Instance or In_Inlined_Body)
9038 Root_Type (Underlying_Type (Target_Type)) =
9039 Root_Type (Underlying_Type (Opnd_Type))
9043 -- Special check for common access type error case
9045 elsif Ekind (Target_Type) = E_Access_Type
9046 and then Is_Access_Type (Opnd_Type)
9048 Error_Msg_N ("target type must be general access type!", N);
9049 Error_Msg_NE ("add ALL to }!", N, Target_Type);
9054 Error_Msg_NE ("invalid conversion, not compatible with }",
9059 end Valid_Conversion;