1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Debug_A; use Debug_A;
33 with Einfo; use Einfo;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Itypes; use Itypes;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Nlists; use Nlists;
46 with Output; use Output;
47 with Restrict; use Restrict;
48 with Rtsfind; use Rtsfind;
50 with Sem_Aggr; use Sem_Aggr;
51 with Sem_Attr; use Sem_Attr;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch4; use Sem_Ch4;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Intr; use Sem_Intr;
61 with Sem_Util; use Sem_Util;
62 with Sem_Type; use Sem_Type;
63 with Sem_Warn; use Sem_Warn;
64 with Sinfo; use Sinfo;
65 with Stand; use Stand;
66 with Stringt; use Stringt;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Uintp; use Uintp;
70 with Urealp; use Urealp;
72 package body Sem_Res is
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 -- Second pass (top-down) type checking and overload resolution procedures
79 -- Typ is the type required by context. These procedures propagate the
80 -- type information recursively to the descendants of N. If the node
81 -- is not overloaded, its Etype is established in the first pass. If
82 -- overloaded, the Resolve routines set the correct type. For arith.
83 -- operators, the Etype is the base type of the context.
85 -- Note that Resolve_Attribute is separated off in Sem_Attr
87 procedure Ambiguous_Character (C : Node_Id);
88 -- Give list of candidate interpretations when a character literal cannot
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 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
102 -- Given a call node, N, which is known to occur immediately within the
103 -- subprogram being called, determines whether it is a detectable case of
104 -- an infinite recursion, and if so, outputs appropriate messages. Returns
105 -- True if an infinite recursion is detected, and False otherwise.
107 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
108 -- If the type of the object being initialized uses the secondary stack
109 -- directly or indirectly, create a transient scope for the call to the
110 -- Init_Proc. This is because we do not create transient scopes for the
111 -- initialization of individual components within the init_proc itself.
112 -- Could be optimized away perhaps?
114 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
115 -- Utility to check whether the name in the call is a predefined
116 -- operator, in which case the call is made into an operator node.
117 -- An instance of an intrinsic conversion operation may be given
118 -- an operator name, but is not treated like an operator.
120 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
121 -- If a default expression in entry call N depends on the discriminants
122 -- of the task, it must be replaced with a reference to the discriminant
123 -- of the task being called.
125 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
126 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
127 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
128 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
129 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
130 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id);
131 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
132 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
133 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
134 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
135 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
136 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
137 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
138 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
139 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
140 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
141 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
142 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
143 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
144 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
145 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
146 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
147 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
148 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
149 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
150 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
151 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
152 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id);
153 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
154 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
155 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
156 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
158 function Operator_Kind
162 -- Utility to map the name of an operator into the corresponding Node. Used
163 -- by other node rewriting procedures.
165 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
166 -- Resolve actuals of call, and add default expressions for missing ones.
168 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
169 -- Called from Resolve_Call, when the prefix denotes an entry or element
170 -- of entry family. Actuals are resolved as for subprograms, and the node
171 -- is rebuilt as an entry call. Also called for protected operations. Typ
172 -- is the context type, which is used when the operation is a protected
173 -- function with no arguments, and the return value is indexed.
175 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
176 -- A call to a user-defined intrinsic operator is rewritten as a call
177 -- to the corresponding predefined operator, with suitable conversions.
179 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
180 -- If an operator node resolves to a call to a user-defined operator,
181 -- rewrite the node as a function call.
183 procedure Make_Call_Into_Operator
187 -- Inverse transformation: if an operator is given in functional notation,
188 -- then after resolving the node, transform into an operator node, so
189 -- that operands are resolved properly. Recall that predefined operators
190 -- do not have a full signature and special resolution rules apply.
192 procedure Rewrite_Renamed_Operator (N : Node_Id; Op : Entity_Id);
193 -- An operator can rename another, e.g. in an instantiation. In that
194 -- case, the proper operator node must be constructed.
196 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
197 -- The String_Literal_Subtype is built for all strings that are not
198 -- operands of a static concatenation operation. If the argument is not
199 -- a String the function is a no-op.
201 procedure Set_Slice_Subtype (N : Node_Id);
202 -- Build subtype of array type, with the range specified by the slice.
204 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
205 -- A universal_fixed expression in an universal context is unambiguous if
206 -- there is only one applicable fixed point type. Determining whether
207 -- there is only one requires a search over all visible entities, and
208 -- happens only in very pathological cases (see 6115-006).
210 function Valid_Conversion
215 -- Verify legality rules given in 4.6 (8-23). Target is the target
216 -- type of the conversion, which may be an implicit conversion of
217 -- an actual parameter to an anonymous access type (in which case
218 -- N denotes the actual parameter and N = Operand).
220 -------------------------
221 -- Ambiguous_Character --
222 -------------------------
224 procedure Ambiguous_Character (C : Node_Id) is
228 if Nkind (C) = N_Character_Literal then
229 Error_Msg_N ("ambiguous character literal", C);
231 ("\possible interpretations: Character, Wide_Character!", C);
233 E := Current_Entity (C);
237 while Present (E) loop
238 Error_Msg_NE ("\possible interpretation:}!", C, Etype (E));
243 end Ambiguous_Character;
245 -------------------------
246 -- Analyze_And_Resolve --
247 -------------------------
249 procedure Analyze_And_Resolve (N : Node_Id) is
252 Resolve (N, Etype (N));
253 end Analyze_And_Resolve;
255 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
259 end Analyze_And_Resolve;
261 -- Version withs check(s) suppressed
263 procedure Analyze_And_Resolve
268 Scop : Entity_Id := Current_Scope;
271 if Suppress = All_Checks then
273 Svg : constant Suppress_Record := Scope_Suppress;
276 Scope_Suppress := (others => True);
277 Analyze_And_Resolve (N, Typ);
278 Scope_Suppress := Svg;
283 Svg : constant Boolean := Get_Scope_Suppress (Suppress);
286 Set_Scope_Suppress (Suppress, True);
287 Analyze_And_Resolve (N, Typ);
288 Set_Scope_Suppress (Suppress, Svg);
292 if Current_Scope /= Scop
293 and then Scope_Is_Transient
295 -- This can only happen if a transient scope was created
296 -- for an inner expression, which will be removed upon
297 -- completion of the analysis of an enclosing construct.
298 -- The transient scope must have the suppress status of
299 -- the enclosing environment, not of this Analyze call.
301 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
304 end Analyze_And_Resolve;
306 procedure Analyze_And_Resolve
310 Scop : Entity_Id := Current_Scope;
313 if Suppress = All_Checks then
315 Svg : constant Suppress_Record := Scope_Suppress;
318 Scope_Suppress := (others => True);
319 Analyze_And_Resolve (N);
320 Scope_Suppress := Svg;
325 Svg : constant Boolean := Get_Scope_Suppress (Suppress);
328 Set_Scope_Suppress (Suppress, True);
329 Analyze_And_Resolve (N);
330 Set_Scope_Suppress (Suppress, Svg);
334 if Current_Scope /= Scop
335 and then Scope_Is_Transient
337 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
340 end Analyze_And_Resolve;
342 ----------------------------
343 -- Check_Discriminant_Use --
344 ----------------------------
346 procedure Check_Discriminant_Use (N : Node_Id) is
347 PN : constant Node_Id := Parent (N);
348 Disc : constant Entity_Id := Entity (N);
353 -- Any use in a default expression is legal.
355 if In_Default_Expression then
358 elsif Nkind (PN) = N_Range then
360 -- Discriminant cannot be used to constrain a scalar type.
364 if Nkind (P) = N_Range_Constraint
365 and then Nkind (Parent (P)) = N_Subtype_Indication
366 and then Nkind (Parent (Parent (P))) = N_Component_Declaration
368 Error_Msg_N ("discriminant cannot constrain scalar type", N);
370 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
372 -- The following check catches the unusual case where
373 -- a discriminant appears within an index constraint
374 -- that is part of a larger expression within a constraint
375 -- on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
376 -- For now we only check case of record components, and
377 -- note that a similar check should also apply in the
378 -- case of discriminant constraints below. ???
380 -- Note that the check for N_Subtype_Declaration below is to
381 -- detect the valid use of discriminants in the constraints of a
382 -- subtype declaration when this subtype declaration appears
383 -- inside the scope of a record type (which is syntactically
384 -- illegal, but which may be created as part of derived type
385 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
388 if Ekind (Current_Scope) = E_Record_Type
389 and then Scope (Disc) = Current_Scope
391 (Nkind (Parent (P)) = N_Subtype_Indication
393 (Nkind (Parent (Parent (P))) = N_Component_Declaration
394 or else Nkind (Parent (Parent (P))) = N_Subtype_Declaration)
395 and then Paren_Count (N) = 0)
398 ("discriminant must appear alone in component constraint", N);
402 -- Detect a common beginner error:
403 -- type R (D : Positive := 100) is record
404 -- Name: String (1 .. D);
407 -- The default value causes an object of type R to be
408 -- allocated with room for Positive'Last characters.
416 function Large_Storage_Type (T : Entity_Id) return Boolean;
417 -- Return True if type T has a large enough range that
418 -- any array whose index type covered the whole range of
419 -- the type would likely raise Storage_Error.
421 function Large_Storage_Type (T : Entity_Id) return Boolean is
426 T = Standard_Positive
428 T = Standard_Natural;
429 end Large_Storage_Type;
432 -- Check that the Disc has a large range
434 if not Large_Storage_Type (Etype (Disc)) then
438 -- If the enclosing type is limited, we allocate only the
439 -- default value, not the maximum, and there is no need for
442 if Is_Limited_Type (Scope (Disc)) then
446 -- Check that it is the high bound
448 if N /= High_Bound (PN)
449 or else not Present (Discriminant_Default_Value (Disc))
454 -- Check the array allows a large range at this bound.
455 -- First find the array
459 if Nkind (SI) /= N_Subtype_Indication then
463 T := Entity (Subtype_Mark (SI));
465 if not Is_Array_Type (T) then
469 -- Next, find the dimension
471 TB := First_Index (T);
472 CB := First (Constraints (P));
474 and then Present (TB)
475 and then Present (CB)
486 -- Now, check the dimension has a large range
488 if not Large_Storage_Type (Etype (TB)) then
492 -- Warn about the danger
495 ("creation of object of this type may raise Storage_Error?",
504 -- Legal case is in index or discriminant constraint
506 elsif Nkind (PN) = N_Index_Or_Discriminant_Constraint
507 or else Nkind (PN) = N_Discriminant_Association
509 if Paren_Count (N) > 0 then
511 ("discriminant in constraint must appear alone", N);
516 -- Otherwise, context is an expression. It should not be within
517 -- (i.e. a subexpression of) a constraint for a component.
523 while Nkind (P) /= N_Component_Declaration
524 and then Nkind (P) /= N_Subtype_Indication
525 and then Nkind (P) /= N_Entry_Declaration
532 -- If the discriminant is used in an expression that is a bound
533 -- of a scalar type, an Itype is created and the bounds are attached
534 -- to its range, not to the original subtype indication. Such use
535 -- is of course a double fault.
537 if (Nkind (P) = N_Subtype_Indication
539 (Nkind (Parent (P)) = N_Component_Declaration
540 or else Nkind (Parent (P)) = N_Derived_Type_Definition)
541 and then D = Constraint (P))
543 -- The constraint itself may be given by a subtype indication,
544 -- rather than by a more common discrete range.
546 or else (Nkind (P) = N_Subtype_Indication
547 and then Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
549 or else Nkind (P) = N_Entry_Declaration
550 or else Nkind (D) = N_Defining_Identifier
553 ("discriminant in constraint must appear alone", N);
556 end Check_Discriminant_Use;
558 --------------------------------
559 -- Check_For_Visible_Operator --
560 --------------------------------
562 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
563 Orig_Node : Node_Id := Original_Node (N);
566 if Comes_From_Source (Orig_Node)
567 and then not In_Open_Scopes (Scope (T))
568 and then not Is_Potentially_Use_Visible (T)
569 and then not In_Use (T)
570 and then not In_Use (Scope (T))
571 and then (not Present (Entity (N))
572 or else Ekind (Entity (N)) /= E_Function)
573 and then (Nkind (Orig_Node) /= N_Function_Call
574 or else Nkind (Name (Orig_Node)) /= N_Expanded_Name
575 or else Entity (Prefix (Name (Orig_Node))) /= Scope (T))
576 and then not In_Instance
579 ("operator for} is not directly visible!", N, First_Subtype (T));
580 Error_Msg_N ("use clause would make operation legal!", N);
582 end Check_For_Visible_Operator;
584 ------------------------------
585 -- Check_Infinite_Recursion --
586 ------------------------------
588 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
593 -- Loop moving up tree, quitting if something tells us we are
594 -- definitely not in an infinite recursion situation.
599 exit when Nkind (P) = N_Subprogram_Body;
601 if Nkind (P) = N_Or_Else or else
602 Nkind (P) = N_And_Then or else
603 Nkind (P) = N_If_Statement or else
604 Nkind (P) = N_Case_Statement
608 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
609 and then C /= First (Statements (P))
618 Warn_On_Instance := True;
619 Error_Msg_N ("possible infinite recursion?", N);
620 Error_Msg_N ("\Storage_Error may be raised at run time?", N);
621 Warn_On_Instance := False;
624 end Check_Infinite_Recursion;
626 -------------------------------
627 -- Check_Initialization_Call --
628 -------------------------------
630 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
631 Typ : Entity_Id := Etype (First_Formal (Nam));
633 function Uses_SS (T : Entity_Id) return Boolean;
635 function Uses_SS (T : Entity_Id) return Boolean is
641 or else Has_Controlled_Component (T)
642 or else Functions_Return_By_DSP_On_Target
646 elsif Is_Array_Type (T) then
647 return Uses_SS (Component_Type (T));
649 elsif Is_Record_Type (T) then
650 Comp := First_Component (T);
652 while Present (Comp) loop
654 if Ekind (Comp) = E_Component
655 and then Nkind (Parent (Comp)) = N_Component_Declaration
657 Expr := Expression (Parent (Comp));
659 if Nkind (Expr) = N_Function_Call
660 and then Requires_Transient_Scope (Etype (Expr))
664 elsif Uses_SS (Etype (Comp)) then
669 Next_Component (Comp);
680 if Uses_SS (Typ) then
681 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
683 end Check_Initialization_Call;
685 ------------------------------
686 -- Check_Parameterless_Call --
687 ------------------------------
689 procedure Check_Parameterless_Call (N : Node_Id) is
693 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
697 -- Rewrite as call if overloadable entity that is (or could be, in
698 -- the overloaded case) a function call. If we know for sure that
699 -- the entity is an enumeration literal, we do not rewrite it.
701 if (Is_Entity_Name (N)
702 and then Is_Overloadable (Entity (N))
703 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
704 or else Is_Overloaded (N)))
706 -- Rewrite as call if it is an explicit deference of an expression of
707 -- a subprogram access type, and the suprogram type is not that of a
708 -- procedure or entry.
711 (Nkind (N) = N_Explicit_Dereference
712 and then Ekind (Etype (N)) = E_Subprogram_Type
713 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type)
715 -- Rewrite as call if it is a selected component which is a function,
716 -- this is the case of a call to a protected function (which may be
717 -- overloaded with other protected operations).
720 (Nkind (N) = N_Selected_Component
721 and then (Ekind (Entity (Selector_Name (N))) = E_Function
722 or else ((Ekind (Entity (Selector_Name (N))) = E_Entry
724 Ekind (Entity (Selector_Name (N))) = E_Procedure)
725 and then Is_Overloaded (Selector_Name (N)))))
727 -- If one of the above three conditions is met, rewrite as call.
728 -- Apply the rewriting only once.
731 if Nkind (Parent (N)) /= N_Function_Call
732 or else N /= Name (Parent (N))
736 -- If overloaded, overload set belongs to new copy.
738 Save_Interps (N, Nam);
740 -- Change node to parameterless function call (note that the
741 -- Parameter_Associations associations field is left set to Empty,
742 -- its normal default value since there are no parameters)
744 Change_Node (N, N_Function_Call);
746 Set_Sloc (N, Sloc (Nam));
750 elsif Nkind (N) = N_Parameter_Association then
751 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
753 end Check_Parameterless_Call;
755 ----------------------
756 -- Is_Predefined_Op --
757 ----------------------
759 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
761 return Is_Intrinsic_Subprogram (Nam)
762 and then not Is_Generic_Instance (Nam)
763 and then Chars (Nam) in Any_Operator_Name
764 and then (No (Alias (Nam))
765 or else Is_Predefined_Op (Alias (Nam)));
766 end Is_Predefined_Op;
768 -----------------------------
769 -- Make_Call_Into_Operator --
770 -----------------------------
772 procedure Make_Call_Into_Operator
777 Op_Name : constant Name_Id := Chars (Op_Id);
778 Act1 : Node_Id := First_Actual (N);
779 Act2 : Node_Id := Next_Actual (Act1);
780 Error : Boolean := False;
781 Is_Binary : constant Boolean := Present (Act2);
783 Opnd_Type : Entity_Id;
784 Orig_Type : Entity_Id := Empty;
787 type Kind_Test is access function (E : Entity_Id) return Boolean;
789 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
790 -- Determine whether E is an access type declared by an access decla-
791 -- ration, and not an (anonymous) allocator type.
793 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
794 -- If the operand is not universal, and the operator is given by a
795 -- expanded name, verify that the operand has an interpretation with
796 -- a type defined in the given scope of the operator.
798 function Type_In_P (Test : Kind_Test) return Entity_Id;
799 -- Find a type of the given class in the package Pack that contains
802 -----------------------------
803 -- Is_Definite_Access_Type --
804 -----------------------------
806 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
807 Btyp : constant Entity_Id := Base_Type (E);
809 return Ekind (Btyp) = E_Access_Type
810 or else (Ekind (Btyp) = E_Access_Subprogram_Type
811 and then Comes_From_Source (Btyp));
812 end Is_Definite_Access_Type;
814 ---------------------------
815 -- Operand_Type_In_Scope --
816 ---------------------------
818 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
819 Nod : constant Node_Id := Right_Opnd (Op_Node);
824 if not Is_Overloaded (Nod) then
825 return Scope (Base_Type (Etype (Nod))) = S;
828 Get_First_Interp (Nod, I, It);
830 while Present (It.Typ) loop
832 if Scope (Base_Type (It.Typ)) = S then
836 Get_Next_Interp (I, It);
841 end Operand_Type_In_Scope;
847 function Type_In_P (Test : Kind_Test) return Entity_Id is
850 function In_Decl return Boolean;
851 -- Verify that node is not part of the type declaration for the
852 -- candidate type, which would otherwise be invisible.
858 function In_Decl return Boolean is
859 Decl_Node : constant Node_Id := Parent (E);
865 if Etype (E) = Any_Type then
868 elsif No (Decl_Node) then
873 and then Nkind (N2) /= N_Compilation_Unit
875 if N2 = Decl_Node then
886 -- Start of processing for Type_In_P
889 -- If the context type is declared in the prefix package, this
890 -- is the desired base type.
892 if Scope (Base_Type (Typ)) = Pack
895 return Base_Type (Typ);
898 E := First_Entity (Pack);
900 while Present (E) loop
915 ---------------------------
916 -- Operand_Type_In_Scope --
917 ---------------------------
919 -- Start of processing for Make_Call_Into_Operator
922 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
927 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
928 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
929 Save_Interps (Act1, Left_Opnd (Op_Node));
930 Save_Interps (Act2, Right_Opnd (Op_Node));
931 Act1 := Left_Opnd (Op_Node);
932 Act2 := Right_Opnd (Op_Node);
937 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
938 Save_Interps (Act1, Right_Opnd (Op_Node));
939 Act1 := Right_Opnd (Op_Node);
942 -- If the operator is denoted by an expanded name, and the prefix is
943 -- not Standard, but the operator is a predefined one whose scope is
944 -- Standard, then this is an implicit_operator, inserted as an
945 -- interpretation by the procedure of the same name. This procedure
946 -- overestimates the presence of implicit operators, because it does
947 -- not examine the type of the operands. Verify now that the operand
948 -- type appears in the given scope. If right operand is universal,
949 -- check the other operand. In the case of concatenation, either
950 -- argument can be the component type, so check the type of the result.
951 -- If both arguments are literals, look for a type of the right kind
952 -- defined in the given scope. This elaborate nonsense is brought to
953 -- you courtesy of b33302a. The type itself must be frozen, so we must
954 -- find the type of the proper class in the given scope.
956 -- A final wrinkle is the multiplication operator for fixed point
957 -- types, which is defined in Standard only, and not in the scope of
958 -- the fixed_point type itself.
960 if Nkind (Name (N)) = N_Expanded_Name then
961 Pack := Entity (Prefix (Name (N)));
963 -- If the entity being called is defined in the given package,
964 -- it is a renaming of a predefined operator, and known to be
967 if Scope (Entity (Name (N))) = Pack
968 and then Pack /= Standard_Standard
972 elsif (Op_Name = Name_Op_Multiply
973 or else Op_Name = Name_Op_Divide)
974 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
975 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
977 if Pack /= Standard_Standard then
982 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
984 if Op_Name = Name_Op_Concat then
985 Opnd_Type := Base_Type (Typ);
987 elsif (Scope (Opnd_Type) = Standard_Standard
989 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
991 and then not Comes_From_Source (Opnd_Type))
993 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
996 if Scope (Opnd_Type) = Standard_Standard then
998 -- Verify that the scope contains a type that corresponds to
999 -- the given literal. Optimize the case where Pack is Standard.
1001 if Pack /= Standard_Standard then
1003 if Opnd_Type = Universal_Integer then
1004 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1006 elsif Opnd_Type = Universal_Real then
1007 Orig_Type := Type_In_P (Is_Real_Type'Access);
1009 elsif Opnd_Type = Any_String then
1010 Orig_Type := Type_In_P (Is_String_Type'Access);
1012 elsif Opnd_Type = Any_Access then
1013 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1015 elsif Opnd_Type = Any_Composite then
1016 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1018 if Present (Orig_Type) then
1019 if Has_Private_Component (Orig_Type) then
1022 Set_Etype (Act1, Orig_Type);
1025 Set_Etype (Act2, Orig_Type);
1034 Error := No (Orig_Type);
1037 elsif Ekind (Opnd_Type) = E_Allocator_Type
1038 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1042 -- If the type is defined elsewhere, and the operator is not
1043 -- defined in the given scope (by a renaming declaration, e.g.)
1044 -- then this is an error as well. If an extension of System is
1045 -- present, and the type may be defined there, Pack must be
1048 elsif Scope (Opnd_Type) /= Pack
1049 and then Scope (Op_Id) /= Pack
1050 and then (No (System_Aux_Id)
1051 or else Scope (Opnd_Type) /= System_Aux_Id
1052 or else Pack /= Scope (System_Aux_Id))
1056 elsif Pack = Standard_Standard
1057 and then not Operand_Type_In_Scope (Standard_Standard)
1064 Error_Msg_Node_2 := Pack;
1066 ("& not declared in&", N, Selector_Name (Name (N)));
1067 Set_Etype (N, Any_Type);
1072 Set_Chars (Op_Node, Op_Name);
1073 Set_Etype (Op_Node, Base_Type (Etype (N)));
1074 Set_Entity (Op_Node, Op_Id);
1075 Generate_Reference (Op_Id, N, ' ');
1076 Rewrite (N, Op_Node);
1079 -- For predefined operators on literals, the operation freezes
1082 if Present (Orig_Type) then
1083 Set_Etype (Act1, Orig_Type);
1084 Freeze_Expression (Act1);
1086 end Make_Call_Into_Operator;
1092 function Operator_Kind
1094 Is_Binary : Boolean)
1101 if Op_Name = Name_Op_And then Kind := N_Op_And;
1102 elsif Op_Name = Name_Op_Or then Kind := N_Op_Or;
1103 elsif Op_Name = Name_Op_Xor then Kind := N_Op_Xor;
1104 elsif Op_Name = Name_Op_Eq then Kind := N_Op_Eq;
1105 elsif Op_Name = Name_Op_Ne then Kind := N_Op_Ne;
1106 elsif Op_Name = Name_Op_Lt then Kind := N_Op_Lt;
1107 elsif Op_Name = Name_Op_Le then Kind := N_Op_Le;
1108 elsif Op_Name = Name_Op_Gt then Kind := N_Op_Gt;
1109 elsif Op_Name = Name_Op_Ge then Kind := N_Op_Ge;
1110 elsif Op_Name = Name_Op_Add then Kind := N_Op_Add;
1111 elsif Op_Name = Name_Op_Subtract then Kind := N_Op_Subtract;
1112 elsif Op_Name = Name_Op_Concat then Kind := N_Op_Concat;
1113 elsif Op_Name = Name_Op_Multiply then Kind := N_Op_Multiply;
1114 elsif Op_Name = Name_Op_Divide then Kind := N_Op_Divide;
1115 elsif Op_Name = Name_Op_Mod then Kind := N_Op_Mod;
1116 elsif Op_Name = Name_Op_Rem then Kind := N_Op_Rem;
1117 elsif Op_Name = Name_Op_Expon then Kind := N_Op_Expon;
1119 raise Program_Error;
1125 if Op_Name = Name_Op_Add then Kind := N_Op_Plus;
1126 elsif Op_Name = Name_Op_Subtract then Kind := N_Op_Minus;
1127 elsif Op_Name = Name_Op_Abs then Kind := N_Op_Abs;
1128 elsif Op_Name = Name_Op_Not then Kind := N_Op_Not;
1130 raise Program_Error;
1137 -----------------------------
1138 -- Pre_Analyze_And_Resolve --
1139 -----------------------------
1141 procedure Pre_Analyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1142 Save_Full_Analysis : constant Boolean := Full_Analysis;
1145 Full_Analysis := False;
1146 Expander_Mode_Save_And_Set (False);
1148 -- We suppress all checks for this analysis, since the checks will
1149 -- be applied properly, and in the right location, when the default
1150 -- expression is reanalyzed and reexpanded later on.
1152 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1154 Expander_Mode_Restore;
1155 Full_Analysis := Save_Full_Analysis;
1156 end Pre_Analyze_And_Resolve;
1158 -- Version without context type.
1160 procedure Pre_Analyze_And_Resolve (N : Node_Id) is
1161 Save_Full_Analysis : constant Boolean := Full_Analysis;
1164 Full_Analysis := False;
1165 Expander_Mode_Save_And_Set (False);
1168 Resolve (N, Etype (N), Suppress => All_Checks);
1170 Expander_Mode_Restore;
1171 Full_Analysis := Save_Full_Analysis;
1172 end Pre_Analyze_And_Resolve;
1174 ----------------------------------
1175 -- Replace_Actual_Discriminants --
1176 ----------------------------------
1178 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1179 Loc : constant Source_Ptr := Sloc (N);
1180 Tsk : Node_Id := Empty;
1182 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1188 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1192 if Nkind (Nod) = N_Identifier then
1193 Ent := Entity (Nod);
1196 and then Ekind (Ent) = E_Discriminant
1199 Make_Selected_Component (Loc,
1200 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1201 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1203 Set_Etype (Nod, Etype (Ent));
1211 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1213 -- Start of processing for Replace_Actual_Discriminants
1216 if not Expander_Active then
1220 if Nkind (Name (N)) = N_Selected_Component then
1221 Tsk := Prefix (Name (N));
1223 elsif Nkind (Name (N)) = N_Indexed_Component then
1224 Tsk := Prefix (Prefix (Name (N)));
1230 Replace_Discrs (Default);
1232 end Replace_Actual_Discriminants;
1238 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1240 I1 : Interp_Index := 0; -- prevent junk warning
1243 Found : Boolean := False;
1244 Seen : Entity_Id := Empty; -- prevent junk warning
1245 Ctx_Type : Entity_Id := Typ;
1246 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1247 Ambiguous : Boolean := False;
1249 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1250 -- Try and fix up a literal so that it matches its expected type. New
1251 -- literals are manufactured if necessary to avoid cascaded errors.
1253 procedure Resolution_Failed;
1254 -- Called when attempt at resolving current expression fails
1256 --------------------
1257 -- Patch_Up_Value --
1258 --------------------
1260 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1262 if Nkind (N) = N_Integer_Literal
1263 and then Is_Real_Type (Typ)
1266 Make_Real_Literal (Sloc (N),
1267 Realval => UR_From_Uint (Intval (N))));
1268 Set_Etype (N, Universal_Real);
1269 Set_Is_Static_Expression (N);
1271 elsif Nkind (N) = N_Real_Literal
1272 and then Is_Integer_Type (Typ)
1275 Make_Integer_Literal (Sloc (N),
1276 Intval => UR_To_Uint (Realval (N))));
1277 Set_Etype (N, Universal_Integer);
1278 Set_Is_Static_Expression (N);
1279 elsif Nkind (N) = N_String_Literal
1280 and then Is_Character_Type (Typ)
1282 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1284 Make_Character_Literal (Sloc (N),
1286 Char_Literal_Value => Char_Code (Character'Pos ('A'))));
1287 Set_Etype (N, Any_Character);
1288 Set_Is_Static_Expression (N);
1290 elsif Nkind (N) /= N_String_Literal
1291 and then Is_String_Type (Typ)
1294 Make_String_Literal (Sloc (N),
1295 Strval => End_String));
1297 elsif Nkind (N) = N_Range then
1298 Patch_Up_Value (Low_Bound (N), Typ);
1299 Patch_Up_Value (High_Bound (N), Typ);
1303 -----------------------
1304 -- Resolution_Failed --
1305 -----------------------
1307 procedure Resolution_Failed is
1309 Patch_Up_Value (N, Typ);
1311 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1312 Set_Is_Overloaded (N, False);
1314 -- The caller will return without calling the expander, so we need
1315 -- to set the analyzed flag. Note that it is fine to set Analyzed
1316 -- to True even if we are in the middle of a shallow analysis,
1317 -- (see the spec of sem for more details) since this is an error
1318 -- situation anyway, and there is no point in repeating the
1319 -- analysis later (indeed it won't work to repeat it later, since
1320 -- we haven't got a clear resolution of which entity is being
1323 Set_Analyzed (N, True);
1325 end Resolution_Failed;
1327 -- Start of processing for Resolve
1334 -- Access attribute on remote subprogram cannot be used for
1335 -- a non-remote access-to-subprogram type.
1337 if Nkind (N) = N_Attribute_Reference
1338 and then (Attribute_Name (N) = Name_Access
1339 or else Attribute_Name (N) = Name_Unrestricted_Access
1340 or else Attribute_Name (N) = Name_Unchecked_Access)
1341 and then Comes_From_Source (N)
1342 and then Is_Entity_Name (Prefix (N))
1343 and then Is_Subprogram (Entity (Prefix (N)))
1344 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1345 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1348 ("prefix must statically denote a non-remote subprogram", N);
1351 -- If the context is a Remote_Access_To_Subprogram, access attributes
1352 -- must be resolved with the corresponding fat pointer. There is no need
1353 -- to check for the attribute name since the return type of an
1354 -- attribute is never a remote type.
1356 if Nkind (N) = N_Attribute_Reference
1357 and then Comes_From_Source (N)
1358 and then (Is_Remote_Call_Interface (Typ)
1359 or else Is_Remote_Types (Typ))
1362 Attr : constant Attribute_Id :=
1363 Get_Attribute_Id (Attribute_Name (N));
1364 Pref : constant Node_Id := Prefix (N);
1367 Is_Remote : Boolean := True;
1370 -- Check that Typ is a fat pointer with a reference to a RAS as
1371 -- original access type.
1374 (Ekind (Typ) = E_Access_Subprogram_Type
1375 and then Present (Equivalent_Type (Typ)))
1377 (Ekind (Typ) = E_Record_Type
1378 and then Present (Corresponding_Remote_Type (Typ)))
1381 -- Prefix (N) must statically denote a remote subprogram
1382 -- declared in a package specification.
1384 if Attr = Attribute_Access then
1385 Decl := Unit_Declaration_Node (Entity (Pref));
1387 if Nkind (Decl) = N_Subprogram_Body then
1388 Spec := Corresponding_Spec (Decl);
1390 if not No (Spec) then
1391 Decl := Unit_Declaration_Node (Spec);
1395 Spec := Parent (Decl);
1397 if not Is_Entity_Name (Prefix (N))
1398 or else Nkind (Spec) /= N_Package_Specification
1400 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1404 ("prefix must statically denote a remote subprogram ",
1409 if Attr = Attribute_Access
1410 or else Attr = Attribute_Unchecked_Access
1411 or else Attr = Attribute_Unrestricted_Access
1413 Check_Subtype_Conformant
1414 (New_Id => Entity (Prefix (N)),
1415 Old_Id => Designated_Type
1416 (Corresponding_Remote_Type (Typ)),
1419 Process_Remote_AST_Attribute (N, Typ);
1426 Debug_A_Entry ("resolving ", N);
1428 if Is_Fixed_Point_Type (Typ) then
1429 Check_Restriction (No_Fixed_Point, N);
1431 elsif Is_Floating_Point_Type (Typ)
1432 and then Typ /= Universal_Real
1433 and then Typ /= Any_Real
1435 Check_Restriction (No_Floating_Point, N);
1438 -- Return if already analyzed
1440 if Analyzed (N) then
1441 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
1444 -- Return if type = Any_Type (previous error encountered)
1446 elsif Etype (N) = Any_Type then
1447 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
1451 Check_Parameterless_Call (N);
1453 -- If not overloaded, then we know the type, and all that needs doing
1454 -- is to check that this type is compatible with the context.
1456 if not Is_Overloaded (N) then
1457 Found := Covers (Typ, Etype (N));
1458 Expr_Type := Etype (N);
1460 -- In the overloaded case, we must select the interpretation that
1461 -- is compatible with the context (i.e. the type passed to Resolve)
1464 Get_First_Interp (N, I, It);
1466 -- Loop through possible interpretations
1468 Interp_Loop : while Present (It.Typ) loop
1470 -- We are only interested in interpretations that are compatible
1471 -- with the expected type, any other interpretations are ignored
1473 if Covers (Typ, It.Typ) then
1475 -- First matching interpretation
1481 Expr_Type := It.Typ;
1483 -- Matching intepretation that is not the first, maybe an
1484 -- error, but there are some cases where preference rules are
1485 -- used to choose between the two possibilities. These and
1486 -- some more obscure cases are handled in Disambiguate.
1489 Error_Msg_Sloc := Sloc (Seen);
1490 It1 := Disambiguate (N, I1, I, Typ);
1492 if It1 = No_Interp then
1494 -- Before we issue an ambiguity complaint, check for
1495 -- the case of a subprogram call where at least one
1496 -- of the arguments is Any_Type, and if so, suppress
1497 -- the message, since it is a cascaded error.
1499 if Nkind (N) = N_Function_Call
1500 or else Nkind (N) = N_Procedure_Call_Statement
1503 A : Node_Id := First_Actual (N);
1507 while Present (A) loop
1510 if Nkind (E) = N_Parameter_Association then
1511 E := Explicit_Actual_Parameter (E);
1514 if Etype (E) = Any_Type then
1515 if Debug_Flag_V then
1516 Write_Str ("Any_Type in call");
1527 elsif Nkind (N) in N_Binary_Op
1528 and then (Etype (Left_Opnd (N)) = Any_Type
1529 or else Etype (Right_Opnd (N)) = Any_Type)
1533 elsif Nkind (N) in N_Unary_Op
1534 and then Etype (Right_Opnd (N)) = Any_Type
1539 -- Not that special case, so issue message using the
1540 -- flag Ambiguous to control printing of the header
1541 -- message only at the start of an ambiguous set.
1543 if not Ambiguous then
1545 ("ambiguous expression (cannot resolve&)!",
1548 ("possible interpretation#!", N);
1552 Error_Msg_Sloc := Sloc (It.Nam);
1553 Error_Msg_N ("possible interpretation#!", N);
1555 -- Disambiguation has succeeded. Skip the remaining
1559 Expr_Type := It1.Typ;
1561 while Present (It.Typ) loop
1562 Get_Next_Interp (I, It);
1567 -- We have a matching interpretation, Expr_Type is the
1568 -- type from this interpretation, and Seen is the entity.
1570 -- For an operator, just set the entity name. The type will
1571 -- be set by the specific operator resolution routine.
1573 if Nkind (N) in N_Op then
1574 Set_Entity (N, Seen);
1575 Generate_Reference (Seen, N);
1577 elsif Nkind (N) = N_Character_Literal then
1578 Set_Etype (N, Expr_Type);
1580 -- For an explicit dereference, attribute reference, range,
1581 -- short-circuit form (which is not an operator node),
1582 -- or a call with a name that is an explicit dereference,
1583 -- there is nothing to be done at this point.
1585 elsif Nkind (N) = N_Explicit_Dereference
1586 or else Nkind (N) = N_Attribute_Reference
1587 or else Nkind (N) = N_And_Then
1588 or else Nkind (N) = N_Indexed_Component
1589 or else Nkind (N) = N_Or_Else
1590 or else Nkind (N) = N_Range
1591 or else Nkind (N) = N_Selected_Component
1592 or else Nkind (N) = N_Slice
1593 or else Nkind (Name (N)) = N_Explicit_Dereference
1597 -- For procedure or function calls, set the type of the
1598 -- name, and also the entity pointer for the prefix
1600 elsif (Nkind (N) = N_Procedure_Call_Statement
1601 or else Nkind (N) = N_Function_Call)
1602 and then (Is_Entity_Name (Name (N))
1603 or else Nkind (Name (N)) = N_Operator_Symbol)
1605 Set_Etype (Name (N), Expr_Type);
1606 Set_Entity (Name (N), Seen);
1607 Generate_Reference (Seen, Name (N));
1609 elsif Nkind (N) = N_Function_Call
1610 and then Nkind (Name (N)) = N_Selected_Component
1612 Set_Etype (Name (N), Expr_Type);
1613 Set_Entity (Selector_Name (Name (N)), Seen);
1614 Generate_Reference (Seen, Selector_Name (Name (N)));
1616 -- For all other cases, just set the type of the Name
1619 Set_Etype (Name (N), Expr_Type);
1622 -- Here if interpetation is incompatible with context type
1625 if Debug_Flag_V then
1626 Write_Str (" intepretation incompatible with context");
1631 -- Move to next interpretation
1633 exit Interp_Loop when not Present (It.Typ);
1635 Get_Next_Interp (I, It);
1636 end loop Interp_Loop;
1639 -- At this stage Found indicates whether or not an acceptable
1640 -- interpretation exists. If not, then we have an error, except
1641 -- that if the context is Any_Type as a result of some other error,
1642 -- then we suppress the error report.
1645 if Typ /= Any_Type then
1647 -- If type we are looking for is Void, then this is the
1648 -- procedure call case, and the error is simply that what
1649 -- we gave is not a procedure name (we think of procedure
1650 -- calls as expressions with types internally, but the user
1651 -- doesn't think of them this way!)
1653 if Typ = Standard_Void_Type then
1654 Error_Msg_N ("expect procedure name in procedure call", N);
1657 -- Otherwise we do have a subexpression with the wrong type
1659 -- Check for the case of an allocator which uses an access
1660 -- type instead of the designated type. This is a common
1661 -- error and we specialize the message, posting an error
1662 -- on the operand of the allocator, complaining that we
1663 -- expected the designated type of the allocator.
1665 elsif Nkind (N) = N_Allocator
1666 and then Ekind (Typ) in Access_Kind
1667 and then Ekind (Etype (N)) in Access_Kind
1668 and then Designated_Type (Etype (N)) = Typ
1670 Wrong_Type (Expression (N), Designated_Type (Typ));
1673 -- Check for view mismatch on Null in instances, for
1674 -- which the view-swapping mechanism has no identifier.
1676 elsif (In_Instance or else In_Inlined_Body)
1677 and then (Nkind (N) = N_Null)
1678 and then Is_Private_Type (Typ)
1679 and then Is_Access_Type (Full_View (Typ))
1681 Resolve (N, Full_View (Typ));
1685 -- Check for an aggregate. Sometimes we can get bogus
1686 -- aggregates from misuse of parentheses, and we are
1687 -- about to complain about the aggregate without even
1688 -- looking inside it.
1690 -- Instead, if we have an aggregate of type Any_Composite,
1691 -- then analyze and resolve the component fields, and then
1692 -- only issue another message if we get no errors doing
1693 -- this (otherwise assume that the errors in the aggregate
1694 -- caused the problem).
1696 elsif Nkind (N) = N_Aggregate
1697 and then Etype (N) = Any_Composite
1700 -- Disable expansion in any case. If there is a type mismatch
1701 -- it may be fatal to try to expand the aggregate. The flag
1702 -- would otherwise be set to false when the error is posted.
1704 Expander_Active := False;
1707 procedure Check_Aggr (Aggr : Node_Id);
1708 -- Check one aggregate, and set Found to True if we
1709 -- have a definite error in any of its elements
1711 procedure Check_Elmt (Aelmt : Node_Id);
1712 -- Check one element of aggregate and set Found to
1713 -- True if we definitely have an error in the element.
1715 procedure Check_Aggr (Aggr : Node_Id) is
1719 if Present (Expressions (Aggr)) then
1720 Elmt := First (Expressions (Aggr));
1721 while Present (Elmt) loop
1727 if Present (Component_Associations (Aggr)) then
1728 Elmt := First (Component_Associations (Aggr));
1729 while Present (Elmt) loop
1730 Check_Elmt (Expression (Elmt));
1736 procedure Check_Elmt (Aelmt : Node_Id) is
1738 -- If we have a nested aggregate, go inside it (to
1739 -- attempt a naked analyze-resolve of the aggregate
1740 -- can cause undesirable cascaded errors). Do not
1741 -- resolve expression if it needs a type from context,
1742 -- as for integer * fixed expression.
1744 if Nkind (Aelmt) = N_Aggregate then
1750 if not Is_Overloaded (Aelmt)
1751 and then Etype (Aelmt) /= Any_Fixed
1753 Resolve (Aelmt, Etype (Aelmt));
1756 if Etype (Aelmt) = Any_Type then
1767 -- If an error message was issued already, Found got reset
1768 -- to True, so if it is still False, issue the standard
1769 -- Wrong_Type message.
1772 if Is_Overloaded (N)
1773 and then Nkind (N) = N_Function_Call
1775 Error_Msg_Node_2 := Typ;
1776 Error_Msg_NE ("no visible interpretation of&" &
1777 " matches expected type&", N, Name (N));
1779 if All_Errors_Mode then
1781 Index : Interp_Index;
1785 Error_Msg_N ("\possible interpretations:", N);
1786 Get_First_Interp (Name (N), Index, It);
1788 while Present (It.Nam) loop
1790 Error_Msg_Sloc := Sloc (It.Nam);
1791 Error_Msg_Node_2 := It.Typ;
1792 Error_Msg_NE ("\& declared#, type&",
1795 Get_Next_Interp (Index, It);
1799 Error_Msg_N ("\use -gnatf for details", N);
1802 Wrong_Type (N, Typ);
1810 -- Test if we have more than one interpretation for the context
1812 elsif Ambiguous then
1816 -- Here we have an acceptable interpretation for the context
1819 -- A user-defined operator is tranformed into a function call at
1820 -- this point, so that further processing knows that operators are
1821 -- really operators (i.e. are predefined operators). User-defined
1822 -- operators that are intrinsic are just renamings of the predefined
1823 -- ones, and need not be turned into calls either, but if they rename
1824 -- a different operator, we must transform the node accordingly.
1825 -- Instantiations of Unchecked_Conversion are intrinsic but are
1826 -- treated as functions, even if given an operator designator.
1828 if Nkind (N) in N_Op
1829 and then Present (Entity (N))
1830 and then Ekind (Entity (N)) /= E_Operator
1833 if not Is_Predefined_Op (Entity (N)) then
1834 Rewrite_Operator_As_Call (N, Entity (N));
1836 elsif Present (Alias (Entity (N))) then
1837 Rewrite_Renamed_Operator (N, Alias (Entity (N)));
1841 -- Propagate type information and normalize tree for various
1842 -- predefined operations. If the context only imposes a class of
1843 -- types, rather than a specific type, propagate the actual type
1846 if Typ = Any_Integer
1847 or else Typ = Any_Boolean
1848 or else Typ = Any_Modular
1849 or else Typ = Any_Real
1850 or else Typ = Any_Discrete
1852 Ctx_Type := Expr_Type;
1854 -- Any_Fixed is legal in a real context only if a specific
1855 -- fixed point type is imposed. If Norman Cohen can be
1856 -- confused by this, it deserves a separate message.
1859 and then Expr_Type = Any_Fixed
1861 Error_Msg_N ("Illegal context for mixed mode operation", N);
1862 Set_Etype (N, Universal_Real);
1863 Ctx_Type := Universal_Real;
1867 case N_Subexpr'(Nkind (N)) is
1869 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
1871 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
1873 when N_And_Then | N_Or_Else
1874 => Resolve_Short_Circuit (N, Ctx_Type);
1876 when N_Attribute_Reference
1877 => Resolve_Attribute (N, Ctx_Type);
1879 when N_Character_Literal
1880 => Resolve_Character_Literal (N, Ctx_Type);
1882 when N_Conditional_Expression
1883 => Resolve_Conditional_Expression (N, Ctx_Type);
1885 when N_Expanded_Name
1886 => Resolve_Entity_Name (N, Ctx_Type);
1888 when N_Extension_Aggregate
1889 => Resolve_Extension_Aggregate (N, Ctx_Type);
1891 when N_Explicit_Dereference
1892 => Resolve_Explicit_Dereference (N, Ctx_Type);
1894 when N_Function_Call
1895 => Resolve_Call (N, Ctx_Type);
1898 => Resolve_Entity_Name (N, Ctx_Type);
1900 when N_In | N_Not_In
1901 => Resolve_Membership_Op (N, Ctx_Type);
1903 when N_Indexed_Component
1904 => Resolve_Indexed_Component (N, Ctx_Type);
1906 when N_Integer_Literal
1907 => Resolve_Integer_Literal (N, Ctx_Type);
1909 when N_Null => Resolve_Null (N, Ctx_Type);
1911 when N_Op_And | N_Op_Or | N_Op_Xor
1912 => Resolve_Logical_Op (N, Ctx_Type);
1914 when N_Op_Eq | N_Op_Ne
1915 => Resolve_Equality_Op (N, Ctx_Type);
1917 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
1918 => Resolve_Comparison_Op (N, Ctx_Type);
1920 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
1922 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
1923 N_Op_Divide | N_Op_Mod | N_Op_Rem
1925 => Resolve_Arithmetic_Op (N, Ctx_Type);
1927 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
1929 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
1931 when N_Op_Plus | N_Op_Minus | N_Op_Abs
1932 => Resolve_Unary_Op (N, Ctx_Type);
1934 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
1936 when N_Procedure_Call_Statement
1937 => Resolve_Call (N, Ctx_Type);
1939 when N_Operator_Symbol
1940 => Resolve_Operator_Symbol (N, Ctx_Type);
1942 when N_Qualified_Expression
1943 => Resolve_Qualified_Expression (N, Ctx_Type);
1945 when N_Raise_xxx_Error
1946 => Set_Etype (N, Ctx_Type);
1948 when N_Range => Resolve_Range (N, Ctx_Type);
1951 => Resolve_Real_Literal (N, Ctx_Type);
1953 when N_Reference => Resolve_Reference (N, Ctx_Type);
1955 when N_Selected_Component
1956 => Resolve_Selected_Component (N, Ctx_Type);
1958 when N_Slice => Resolve_Slice (N, Ctx_Type);
1960 when N_String_Literal
1961 => Resolve_String_Literal (N, Ctx_Type);
1963 when N_Subprogram_Info
1964 => Resolve_Subprogram_Info (N, Ctx_Type);
1966 when N_Type_Conversion
1967 => Resolve_Type_Conversion (N, Ctx_Type);
1969 when N_Unchecked_Expression =>
1970 Resolve_Unchecked_Expression (N, Ctx_Type);
1972 when N_Unchecked_Type_Conversion =>
1973 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
1977 -- If the subexpression was replaced by a non-subexpression, then
1978 -- all we do is to expand it. The only legitimate case we know of
1979 -- is converting procedure call statement to entry call statements,
1980 -- but there may be others, so we are making this test general.
1982 if Nkind (N) not in N_Subexpr then
1983 Debug_A_Exit ("resolving ", N, " (done)");
1988 -- The expression is definitely NOT overloaded at this point, so
1989 -- we reset the Is_Overloaded flag to avoid any confusion when
1990 -- reanalyzing the node.
1992 Set_Is_Overloaded (N, False);
1994 -- Freeze expression type, entity if it is a name, and designated
1995 -- type if it is an allocator (RM 13.14(9,10)).
1997 -- Now that the resolution of the type of the node is complete,
1998 -- and we did not detect an error, we can expand this node. We
1999 -- skip the expand call if we are in a default expression, see
2000 -- section "Handling of Default Expressions" in Sem spec.
2002 Debug_A_Exit ("resolving ", N, " (done)");
2004 -- We unconditionally freeze the expression, even if we are in
2005 -- default expression mode (the Freeze_Expression routine tests
2006 -- this flag and only freezes static types if it is set).
2008 Freeze_Expression (N);
2010 -- Now we can do the expansion
2017 -- Version with check(s) suppressed
2019 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2021 if Suppress = All_Checks then
2023 Svg : constant Suppress_Record := Scope_Suppress;
2026 Scope_Suppress := (others => True);
2028 Scope_Suppress := Svg;
2033 Svg : constant Boolean := Get_Scope_Suppress (Suppress);
2036 Set_Scope_Suppress (Suppress, True);
2038 Set_Scope_Suppress (Suppress, Svg);
2043 ---------------------
2044 -- Resolve_Actuals --
2045 ---------------------
2047 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2048 Loc : constant Source_Ptr := Sloc (N);
2053 Prev : Node_Id := Empty;
2055 procedure Insert_Default;
2056 -- If the actual is missing in a call, insert in the actuals list
2057 -- an instance of the default expression. The insertion is always
2058 -- a named association.
2060 --------------------
2061 -- Insert_Default --
2062 --------------------
2064 procedure Insert_Default is
2069 -- Note that we do a full New_Copy_Tree, so that any associated
2070 -- Itypes are properly copied. This may not be needed any more,
2071 -- but it does no harm as a safety measure! Defaults of a generic
2072 -- formal may be out of bounds of the corresponding actual (see
2073 -- cc1311b) and an additional check may be required.
2075 if Present (Default_Value (F)) then
2077 Actval := New_Copy_Tree (Default_Value (F),
2078 New_Scope => Current_Scope, New_Sloc => Loc);
2080 if Is_Concurrent_Type (Scope (Nam))
2081 and then Has_Discriminants (Scope (Nam))
2083 Replace_Actual_Discriminants (N, Actval);
2086 if Is_Overloadable (Nam)
2087 and then Present (Alias (Nam))
2089 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
2090 and then not Is_Tagged_Type (Etype (F))
2092 -- If default is a real literal, do not introduce a
2093 -- conversion whose effect may depend on the run-time
2094 -- size of universal real.
2096 if Nkind (Actval) = N_Real_Literal then
2097 Set_Etype (Actval, Base_Type (Etype (F)));
2099 Actval := Unchecked_Convert_To (Etype (F), Actval);
2103 if Is_Scalar_Type (Etype (F)) then
2104 Enable_Range_Check (Actval);
2107 Set_Parent (Actval, N);
2108 Analyze_And_Resolve (Actval, Etype (Actval));
2110 Set_Parent (Actval, N);
2112 -- Resolve aggregates with their base type, to avoid scope
2113 -- anomalies: the subtype was first built in the suprogram
2114 -- declaration, and the current call may be nested.
2116 if Nkind (Actval) = N_Aggregate
2117 and then Has_Discriminants (Etype (Actval))
2119 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2121 Analyze_And_Resolve (Actval, Etype (Actval));
2125 -- If default is a tag indeterminate function call, propagate
2126 -- tag to obtain proper dispatching.
2128 if Is_Controlling_Formal (F)
2129 and then Nkind (Default_Value (F)) = N_Function_Call
2131 Set_Is_Controlling_Actual (Actval);
2135 -- Missing argument in call, nothing to insert.
2139 -- If the default expression raises constraint error, then just
2140 -- silently replace it with an N_Raise_Constraint_Error node,
2141 -- since we already gave the warning on the subprogram spec.
2143 if Raises_Constraint_Error (Actval) then
2145 Make_Raise_Constraint_Error (Loc));
2146 Set_Raises_Constraint_Error (Actval);
2147 Set_Etype (Actval, Etype (F));
2151 Make_Parameter_Association (Loc,
2152 Explicit_Actual_Parameter => Actval,
2153 Selector_Name => Make_Identifier (Loc, Chars (F)));
2155 -- Case of insertion is first named actual
2157 if No (Prev) or else
2158 Nkind (Parent (Prev)) /= N_Parameter_Association
2160 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
2161 Set_First_Named_Actual (N, Actval);
2164 if not Present (Parameter_Associations (N)) then
2165 Set_Parameter_Associations (N, New_List (Assoc));
2167 Append (Assoc, Parameter_Associations (N));
2171 Insert_After (Prev, Assoc);
2174 -- Case of insertion is not first named actual
2177 Set_Next_Named_Actual
2178 (Assoc, Next_Named_Actual (Parent (Prev)));
2179 Set_Next_Named_Actual (Parent (Prev), Actval);
2180 Append (Assoc, Parameter_Associations (N));
2183 Mark_Rewrite_Insertion (Assoc);
2184 Mark_Rewrite_Insertion (Actval);
2189 -- Start of processing for Resolve_Actuals
2192 A := First_Actual (N);
2193 F := First_Formal (Nam);
2195 while Present (F) loop
2198 and then (Nkind (Parent (A)) /= N_Parameter_Association
2200 Chars (Selector_Name (Parent (A))) = Chars (F))
2202 -- If the formal is Out or In_Out, do not resolve and expand the
2203 -- conversion, because it is subsequently expanded into explicit
2204 -- temporaries and assignments. However, the object of the
2205 -- conversion can be resolved. An exception is the case of
2206 -- a tagged type conversion with a class-wide actual. In that
2207 -- case we want the tag check to occur and no temporary will
2208 -- will be needed (no representation change can occur) and
2209 -- the parameter is passed by reference, so we go ahead and
2210 -- resolve the type conversion.
2212 if Ekind (F) /= E_In_Parameter
2213 and then Nkind (A) = N_Type_Conversion
2214 and then not Is_Class_Wide_Type (Etype (Expression (A)))
2216 if Conversion_OK (A)
2217 or else Valid_Conversion (A, Etype (A), Expression (A))
2219 Resolve (Expression (A), Etype (Expression (A)));
2223 Resolve (A, Etype (F));
2229 if Ekind (F) /= E_In_Parameter
2230 and then not Is_OK_Variable_For_Out_Formal (A)
2232 -- Specialize error message for protected procedure call
2233 -- within function call of the same protected object.
2235 if Is_Entity_Name (A)
2236 and then Chars (Entity (A)) = Name_uObject
2237 and then Ekind (Current_Scope) = E_Function
2238 and then Convention (Current_Scope) = Convention_Protected
2239 and then Ekind (Nam) /= E_Function
2241 Error_Msg_N ("within protected function, protected " &
2242 "object is constant", A);
2243 Error_Msg_N ("\cannot call operation that may modify it", A);
2245 Error_Msg_NE ("actual for& must be a variable", A, F);
2249 if Ekind (F) /= E_Out_Parameter then
2250 Check_Unset_Reference (A);
2253 and then Is_Entity_Name (A)
2254 and then Ekind (Entity (A)) = E_Out_Parameter
2256 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
2260 -- Apply appropriate range checks for in, out, and in-out
2261 -- parameters. Out and in-out parameters also need a separate
2262 -- check, if there is a type conversion, to make sure the return
2263 -- value meets the constraints of the variable before the
2266 -- Gigi looks at the check flag and uses the appropriate types.
2267 -- For now since one flag is used there is an optimization which
2268 -- might not be done in the In Out case since Gigi does not do
2269 -- any analysis. More thought required about this ???
2271 if Ekind (F) = E_In_Parameter
2272 or else Ekind (F) = E_In_Out_Parameter
2274 if Is_Scalar_Type (Etype (A)) then
2275 Apply_Scalar_Range_Check (A, F_Typ);
2277 elsif Is_Array_Type (Etype (A)) then
2278 Apply_Length_Check (A, F_Typ);
2280 elsif Is_Record_Type (F_Typ)
2281 and then Has_Discriminants (F_Typ)
2282 and then Is_Constrained (F_Typ)
2283 and then (not Is_Derived_Type (F_Typ)
2284 or else Comes_From_Source (Nam))
2286 Apply_Discriminant_Check (A, F_Typ);
2288 elsif Is_Access_Type (F_Typ)
2289 and then Is_Array_Type (Designated_Type (F_Typ))
2290 and then Is_Constrained (Designated_Type (F_Typ))
2292 Apply_Length_Check (A, F_Typ);
2294 elsif Is_Access_Type (F_Typ)
2295 and then Has_Discriminants (Designated_Type (F_Typ))
2296 and then Is_Constrained (Designated_Type (F_Typ))
2298 Apply_Discriminant_Check (A, F_Typ);
2301 Apply_Range_Check (A, F_Typ);
2305 if Ekind (F) = E_Out_Parameter
2306 or else Ekind (F) = E_In_Out_Parameter
2309 if Nkind (A) = N_Type_Conversion then
2310 if Is_Scalar_Type (A_Typ) then
2311 Apply_Scalar_Range_Check
2312 (Expression (A), Etype (Expression (A)), A_Typ);
2315 (Expression (A), Etype (Expression (A)), A_Typ);
2319 if Is_Scalar_Type (F_Typ) then
2320 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
2322 elsif Is_Array_Type (F_Typ)
2323 and then Ekind (F) = E_Out_Parameter
2325 Apply_Length_Check (A, F_Typ);
2328 Apply_Range_Check (A, A_Typ, F_Typ);
2333 -- An actual associated with an access parameter is implicitly
2334 -- converted to the anonymous access type of the formal and
2335 -- must satisfy the legality checks for access conversions.
2337 if Ekind (F_Typ) = E_Anonymous_Access_Type then
2338 if not Valid_Conversion (A, F_Typ, A) then
2340 ("invalid implicit conversion for access parameter", A);
2344 -- Check bad case of atomic/volatile argument (RM C.6(12))
2346 if Is_By_Reference_Type (Etype (F))
2347 and then Comes_From_Source (N)
2349 if Is_Atomic_Object (A)
2350 and then not Is_Atomic (Etype (F))
2353 ("cannot pass atomic argument to non-atomic formal",
2356 elsif Is_Volatile_Object (A)
2357 and then not Is_Volatile (Etype (F))
2360 ("cannot pass volatile argument to non-volatile formal",
2365 -- Check that subprograms don't have improper controlling
2366 -- arguments (RM 3.9.2 (9))
2368 if Is_Controlling_Formal (F) then
2369 Set_Is_Controlling_Actual (A);
2370 elsif Nkind (A) = N_Explicit_Dereference then
2371 Validate_Remote_Access_To_Class_Wide_Type (A);
2374 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
2375 and then not Is_Class_Wide_Type (F_Typ)
2376 and then not Is_Controlling_Formal (F)
2378 Error_Msg_N ("class-wide argument not allowed here!", A);
2379 if Is_Subprogram (Nam) then
2380 Error_Msg_Node_2 := F_Typ;
2382 ("& is not a primitive operation of &!", A, Nam);
2385 elsif Is_Access_Type (A_Typ)
2386 and then Is_Access_Type (F_Typ)
2387 and then Ekind (F_Typ) /= E_Access_Subprogram_Type
2388 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
2389 or else (Nkind (A) = N_Attribute_Reference
2390 and then Is_Class_Wide_Type (Etype (Prefix (A)))))
2391 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
2392 and then not Is_Controlling_Formal (F)
2395 ("access to class-wide argument not allowed here!", A);
2396 if Is_Subprogram (Nam) then
2397 Error_Msg_Node_2 := Designated_Type (F_Typ);
2399 ("& is not a primitive operation of &!", A, Nam);
2405 -- If it is a named association, treat the selector_name as
2406 -- a proper identifier, and mark the corresponding entity.
2408 if Nkind (Parent (A)) = N_Parameter_Association then
2409 Set_Entity (Selector_Name (Parent (A)), F);
2410 Generate_Reference (F, Selector_Name (Parent (A)));
2411 Set_Etype (Selector_Name (Parent (A)), F_Typ);
2412 Generate_Reference (F_Typ, N, ' ');
2425 end Resolve_Actuals;
2427 -----------------------
2428 -- Resolve_Allocator --
2429 -----------------------
2431 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
2432 E : constant Node_Id := Expression (N);
2434 Discrim : Entity_Id;
2439 -- Replace general access with specific type
2441 if Ekind (Etype (N)) = E_Allocator_Type then
2442 Set_Etype (N, Base_Type (Typ));
2445 if Is_Abstract (Typ) then
2446 Error_Msg_N ("type of allocator cannot be abstract", N);
2449 -- For qualified expression, resolve the expression using the
2450 -- given subtype (nothing to do for type mark, subtype indication)
2452 if Nkind (E) = N_Qualified_Expression then
2453 if Is_Class_Wide_Type (Etype (E))
2454 and then not Is_Class_Wide_Type (Designated_Type (Typ))
2457 ("class-wide allocator not allowed for this access type", N);
2460 Resolve (Expression (E), Etype (E));
2461 Check_Unset_Reference (Expression (E));
2463 -- For a subtype mark or subtype indication, freeze the subtype
2466 Freeze_Expression (E);
2468 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
2470 ("initialization required for access-to-constant allocator", N);
2473 -- A special accessibility check is needed for allocators that
2474 -- constrain access discriminants. The level of the type of the
2475 -- expression used to contrain an access discriminant cannot be
2476 -- deeper than the type of the allocator (in constrast to access
2477 -- parameters, where the level of the actual can be arbitrary).
2478 -- We can't use Valid_Conversion to perform this check because
2479 -- in general the type of the allocator is unrelated to the type
2480 -- of the access discriminant. Note that specialized checks are
2481 -- needed for the cases of a constraint expression which is an
2482 -- access attribute or an access discriminant.
2484 if Nkind (Original_Node (E)) = N_Subtype_Indication
2485 and then Ekind (Typ) /= E_Anonymous_Access_Type
2487 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
2489 if Has_Discriminants (Subtyp) then
2490 Discrim := First_Discriminant (Base_Type (Subtyp));
2491 Constr := First (Constraints (Constraint (Original_Node (E))));
2493 while Present (Discrim) and then Present (Constr) loop
2494 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
2495 if Nkind (Constr) = N_Discriminant_Association then
2496 Disc_Exp := Original_Node (Expression (Constr));
2498 Disc_Exp := Original_Node (Constr);
2501 if Type_Access_Level (Etype (Disc_Exp))
2502 > Type_Access_Level (Typ)
2505 ("operand type has deeper level than allocator type",
2508 elsif Nkind (Disc_Exp) = N_Attribute_Reference
2509 and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
2511 and then Object_Access_Level (Prefix (Disc_Exp))
2512 > Type_Access_Level (Typ)
2515 ("prefix of attribute has deeper level than"
2516 & " allocator type", Disc_Exp);
2518 -- When the operand is an access discriminant the check
2519 -- is against the level of the prefix object.
2521 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
2522 and then Nkind (Disc_Exp) = N_Selected_Component
2523 and then Object_Access_Level (Prefix (Disc_Exp))
2524 > Type_Access_Level (Typ)
2527 ("access discriminant has deeper level than"
2528 & " allocator type", Disc_Exp);
2531 Next_Discriminant (Discrim);
2538 -- Check for allocation from an empty storage pool
2540 if No_Pool_Assigned (Typ) then
2542 Loc : constant Source_Ptr := Sloc (N);
2545 Error_Msg_N ("?allocation from empty storage pool!", N);
2546 Error_Msg_N ("?Storage_Error will be raised at run time!", N);
2548 Make_Raise_Storage_Error (Loc));
2551 end Resolve_Allocator;
2553 ---------------------------
2554 -- Resolve_Arithmetic_Op --
2555 ---------------------------
2557 -- Used for resolving all arithmetic operators except exponentiation
2559 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
2560 L : constant Node_Id := Left_Opnd (N);
2561 R : constant Node_Id := Right_Opnd (N);
2563 TL : Entity_Id := Base_Type (Etype (L));
2564 TR : Entity_Id := Base_Type (Etype (R));
2566 B_Typ : constant Entity_Id := Base_Type (Typ);
2567 -- We do the resolution using the base type, because intermediate values
2568 -- in expressions always are of the base type, not a subtype of it.
2570 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
2571 -- Return True iff given type is Integer or universal real/integer
2573 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
2574 -- Choose type of integer literal in fixed-point operation to conform
2575 -- to available fixed-point type. T is the type of the other operand,
2576 -- which is needed to determine the expected type of N.
2578 procedure Set_Operand_Type (N : Node_Id);
2579 -- Set operand type to T if universal
2581 function Universal_Interpretation (N : Node_Id) return Entity_Id;
2582 -- Find universal type of operand, if any.
2584 -----------------------------
2585 -- Is_Integer_Or_Universal --
2586 -----------------------------
2588 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
2590 Index : Interp_Index;
2594 if not Is_Overloaded (N) then
2596 return Base_Type (T) = Base_Type (Standard_Integer)
2597 or else T = Universal_Integer
2598 or else T = Universal_Real;
2600 Get_First_Interp (N, Index, It);
2602 while Present (It.Typ) loop
2604 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
2605 or else It.Typ = Universal_Integer
2606 or else It.Typ = Universal_Real
2611 Get_Next_Interp (Index, It);
2616 end Is_Integer_Or_Universal;
2618 ----------------------------
2619 -- Set_Mixed_Mode_Operand --
2620 ----------------------------
2622 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
2623 Index : Interp_Index;
2627 if Universal_Interpretation (N) = Universal_Integer then
2629 -- A universal integer literal is resolved as standard integer
2630 -- except in the case of a fixed-point result, where we leave
2631 -- it as universal (to be handled by Exp_Fixd later on)
2633 if Is_Fixed_Point_Type (T) then
2634 Resolve (N, Universal_Integer);
2636 Resolve (N, Standard_Integer);
2639 elsif Universal_Interpretation (N) = Universal_Real
2640 and then (T = Base_Type (Standard_Integer)
2641 or else T = Universal_Integer
2642 or else T = Universal_Real)
2644 -- A universal real can appear in a fixed-type context. We resolve
2645 -- the literal with that context, even though this might raise an
2646 -- exception prematurely (the other operand may be zero).
2650 elsif Etype (N) = Base_Type (Standard_Integer)
2651 and then T = Universal_Real
2652 and then Is_Overloaded (N)
2654 -- Integer arg in mixed-mode operation. Resolve with universal
2655 -- type, in case preference rule must be applied.
2657 Resolve (N, Universal_Integer);
2660 and then B_Typ /= Universal_Fixed
2662 -- Not a mixed-mode operation. Resolve with context.
2666 elsif Etype (N) = Any_Fixed then
2668 -- N may itself be a mixed-mode operation, so use context type.
2672 elsif Is_Fixed_Point_Type (T)
2673 and then B_Typ = Universal_Fixed
2674 and then Is_Overloaded (N)
2676 -- Must be (fixed * fixed) operation, operand must have one
2677 -- compatible interpretation.
2679 Resolve (N, Any_Fixed);
2681 elsif Is_Fixed_Point_Type (B_Typ)
2682 and then (T = Universal_Real
2683 or else Is_Fixed_Point_Type (T))
2684 and then Is_Overloaded (N)
2686 -- C * F(X) in a fixed context, where C is a real literal or a
2687 -- fixed-point expression. F must have either a fixed type
2688 -- interpretation or an integer interpretation, but not both.
2690 Get_First_Interp (N, Index, It);
2692 while Present (It.Typ) loop
2694 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
2696 if Analyzed (N) then
2697 Error_Msg_N ("ambiguous operand in fixed operation", N);
2699 Resolve (N, Standard_Integer);
2702 elsif Is_Fixed_Point_Type (It.Typ) then
2704 if Analyzed (N) then
2705 Error_Msg_N ("ambiguous operand in fixed operation", N);
2707 Resolve (N, It.Typ);
2711 Get_Next_Interp (Index, It);
2714 -- Reanalyze the literal with the fixed type of the context.
2717 Set_Analyzed (R, False);
2720 Set_Analyzed (L, False);
2725 Resolve (N, Etype (N));
2727 end Set_Mixed_Mode_Operand;
2729 ----------------------
2730 -- Set_Operand_Type --
2731 ----------------------
2733 procedure Set_Operand_Type (N : Node_Id) is
2735 if Etype (N) = Universal_Integer
2736 or else Etype (N) = Universal_Real
2740 end Set_Operand_Type;
2742 ------------------------------
2743 -- Universal_Interpretation --
2744 ------------------------------
2746 function Universal_Interpretation (N : Node_Id) return Entity_Id is
2747 Index : Interp_Index;
2751 if not Is_Overloaded (N) then
2753 if Etype (N) = Universal_Integer
2754 or else Etype (N) = Universal_Real
2762 Get_First_Interp (N, Index, It);
2764 while Present (It.Typ) loop
2766 if It.Typ = Universal_Integer
2767 or else It.Typ = Universal_Real
2772 Get_Next_Interp (Index, It);
2777 end Universal_Interpretation;
2779 -- Start of processing for Resolve_Arithmetic_Op
2782 if Comes_From_Source (N)
2783 and then Ekind (Entity (N)) = E_Function
2784 and then Is_Imported (Entity (N))
2785 and then Present (First_Rep_Item (Entity (N)))
2787 Resolve_Intrinsic_Operator (N, Typ);
2790 -- Special-case for mixed-mode universal expressions or fixed point
2791 -- type operation: each argument is resolved separately. The same
2792 -- treatment is required if one of the operands of a fixed point
2793 -- operation is universal real, since in this case we don't do a
2794 -- conversion to a specific fixed-point type (instead the expander
2795 -- takes care of the case).
2797 elsif (B_Typ = Universal_Integer
2798 or else B_Typ = Universal_Real)
2799 and then Present (Universal_Interpretation (L))
2800 and then Present (Universal_Interpretation (R))
2802 Resolve (L, Universal_Interpretation (L));
2803 Resolve (R, Universal_Interpretation (R));
2804 Set_Etype (N, B_Typ);
2806 elsif (B_Typ = Universal_Real
2807 or else Etype (N) = Universal_Fixed
2808 or else (Etype (N) = Any_Fixed
2809 and then Is_Fixed_Point_Type (B_Typ))
2810 or else (Is_Fixed_Point_Type (B_Typ)
2811 and then (Is_Integer_Or_Universal (L)
2813 Is_Integer_Or_Universal (R))))
2814 and then (Nkind (N) = N_Op_Multiply or else
2815 Nkind (N) = N_Op_Divide)
2817 if TL = Universal_Integer or else TR = Universal_Integer then
2818 Check_For_Visible_Operator (N, B_Typ);
2821 -- If context is a fixed type and one operand is integer, the
2822 -- other is resolved with the type of the context.
2824 if Is_Fixed_Point_Type (B_Typ)
2825 and then (Base_Type (TL) = Base_Type (Standard_Integer)
2826 or else TL = Universal_Integer)
2831 elsif Is_Fixed_Point_Type (B_Typ)
2832 and then (Base_Type (TR) = Base_Type (Standard_Integer)
2833 or else TR = Universal_Integer)
2839 Set_Mixed_Mode_Operand (L, TR);
2840 Set_Mixed_Mode_Operand (R, TL);
2843 if Etype (N) = Universal_Fixed
2844 or else Etype (N) = Any_Fixed
2846 if B_Typ = Universal_Fixed
2847 and then Nkind (Parent (N)) /= N_Type_Conversion
2848 and then Nkind (Parent (N)) /= N_Unchecked_Type_Conversion
2851 ("type cannot be determined from context!", N);
2853 ("\explicit conversion to result type required", N);
2855 Set_Etype (L, Any_Type);
2856 Set_Etype (R, Any_Type);
2860 and then Etype (N) = Universal_Fixed
2861 and then Nkind (Parent (N)) /= N_Type_Conversion
2862 and then Nkind (Parent (N)) /= N_Unchecked_Type_Conversion
2865 ("(Ada 83) fixed-point operation " &
2866 "needs explicit conversion",
2870 Set_Etype (N, B_Typ);
2873 elsif Is_Fixed_Point_Type (B_Typ)
2874 and then (Is_Integer_Or_Universal (L)
2875 or else Nkind (L) = N_Real_Literal
2876 or else Nkind (R) = N_Real_Literal
2878 Is_Integer_Or_Universal (R))
2880 Set_Etype (N, B_Typ);
2882 elsif Etype (N) = Any_Fixed then
2884 -- If no previous errors, this is only possible if one operand
2885 -- is overloaded and the context is universal. Resolve as such.
2887 Set_Etype (N, B_Typ);
2891 if (TL = Universal_Integer or else TL = Universal_Real)
2892 and then (TR = Universal_Integer or else TR = Universal_Real)
2894 Check_For_Visible_Operator (N, B_Typ);
2897 -- If the context is Universal_Fixed and the operands are also
2898 -- universal fixed, this is an error, unless there is only one
2899 -- applicable fixed_point type (usually duration).
2901 if B_Typ = Universal_Fixed
2902 and then Etype (L) = Universal_Fixed
2904 T := Unique_Fixed_Point_Type (N);
2906 if T = Any_Type then
2919 -- If one of the arguments was resolved to a non-universal type.
2920 -- label the result of the operation itself with the same type.
2921 -- Do the same for the universal argument, if any.
2923 T := Intersect_Types (L, R);
2924 Set_Etype (N, Base_Type (T));
2925 Set_Operand_Type (L);
2926 Set_Operand_Type (R);
2929 Generate_Operator_Reference (N);
2930 Eval_Arithmetic_Op (N);
2932 -- Set overflow and division checking bit. Much cleverer code needed
2933 -- here eventually and perhaps the Resolve routines should be separated
2934 -- for the various arithmetic operations, since they will need
2935 -- different processing. ???
2937 if Nkind (N) in N_Op then
2938 if not Overflow_Checks_Suppressed (Etype (N)) then
2939 Set_Do_Overflow_Check (N);
2942 if (Nkind (N) = N_Op_Divide
2943 or else Nkind (N) = N_Op_Rem
2944 or else Nkind (N) = N_Op_Mod)
2945 and then not Division_Checks_Suppressed (Etype (N))
2947 Set_Do_Division_Check (N);
2951 Check_Unset_Reference (L);
2952 Check_Unset_Reference (R);
2954 end Resolve_Arithmetic_Op;
2960 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
2961 Loc : constant Source_Ptr := Sloc (N);
2962 Subp : constant Node_Id := Name (N);
2970 -- The context imposes a unique interpretation with type Typ on
2971 -- a procedure or function call. Find the entity of the subprogram
2972 -- that yields the expected type, and propagate the corresponding
2973 -- formal constraints on the actuals. The caller has established
2974 -- that an interpretation exists, and emitted an error if not unique.
2976 -- First deal with the case of a call to an access-to-subprogram,
2977 -- dereference made explicit in Analyze_Call.
2979 if Ekind (Etype (Subp)) = E_Subprogram_Type then
2981 if not Is_Overloaded (Subp) then
2982 Nam := Etype (Subp);
2985 -- Find the interpretation whose type (a subprogram type)
2986 -- has a return type that is compatible with the context.
2987 -- Analysis of the node has established that one exists.
2989 Get_First_Interp (Subp, I, It);
2992 while Present (It.Typ) loop
2994 if Covers (Typ, Etype (It.Typ)) then
2999 Get_Next_Interp (I, It);
3003 raise Program_Error;
3007 -- If the prefix is not an entity, then resolve it
3009 if not Is_Entity_Name (Subp) then
3010 Resolve (Subp, Nam);
3013 -- If this is a procedure call which is really an entry call, do
3014 -- the conversion of the procedure call to an entry call. Protected
3015 -- operations use the same circuitry because the name in the call
3016 -- can be an arbitrary expression with special resolution rules.
3018 elsif Nkind (Subp) = N_Selected_Component
3019 or else Nkind (Subp) = N_Indexed_Component
3020 or else (Is_Entity_Name (Subp)
3021 and then Ekind (Entity (Subp)) = E_Entry)
3023 Resolve_Entry_Call (N, Typ);
3024 Check_Elab_Call (N);
3027 -- Normal subprogram call with name established in Resolve
3029 elsif not (Is_Type (Entity (Subp))) then
3030 Nam := Entity (Subp);
3031 Set_Entity_With_Style_Check (Subp, Nam);
3032 Generate_Reference (Nam, Subp);
3034 -- Otherwise we must have the case of an overloaded call
3037 pragma Assert (Is_Overloaded (Subp));
3038 Nam := Empty; -- We know that it will be assigned in loop below.
3040 Get_First_Interp (Subp, I, It);
3042 while Present (It.Typ) loop
3043 if Covers (Typ, It.Typ) then
3045 Set_Entity_With_Style_Check (Subp, Nam);
3046 Generate_Reference (Nam, Subp);
3050 Get_Next_Interp (I, It);
3054 -- Check that a call to Current_Task does not occur in an entry body
3056 if Is_RTE (Nam, RE_Current_Task) then
3066 if Nkind (P) = N_Entry_Body then
3068 ("& should not be used in entry body ('R'M C.7(17))",
3076 -- Check that a procedure call does not occur in the context
3077 -- of the entry call statement of a conditional or timed
3078 -- entry call. Note that the case of a call to a subprogram
3079 -- renaming of an entry will also be rejected. The test
3080 -- for N not being an N_Entry_Call_Statement is defensive,
3081 -- covering the possibility that the processing of entry
3082 -- calls might reach this point due to later modifications
3083 -- of the code above.
3085 if Nkind (Parent (N)) = N_Entry_Call_Alternative
3086 and then Nkind (N) /= N_Entry_Call_Statement
3087 and then Entry_Call_Statement (Parent (N)) = N
3089 Error_Msg_N ("entry call required in select statement", N);
3092 -- Freeze the subprogram name if not in default expression. Note
3093 -- that we freeze procedure calls as well as function calls.
3094 -- Procedure calls are not frozen according to the rules (RM
3095 -- 13.14(14)) because it is impossible to have a procedure call to
3096 -- a non-frozen procedure in pure Ada, but in the code that we
3097 -- generate in the expander, this rule needs extending because we
3098 -- can generate procedure calls that need freezing.
3100 if Is_Entity_Name (Subp) and then not In_Default_Expression then
3101 Freeze_Expression (Subp);
3104 -- For a predefined operator, the type of the result is the type
3105 -- imposed by context, except for a predefined operation on universal
3106 -- fixed. Otherwise The type of the call is the type returned by the
3107 -- subprogram being called.
3109 if Is_Predefined_Op (Nam) then
3111 if Etype (N) /= Universal_Fixed then
3115 -- If the subprogram returns an array type, and the context
3116 -- requires the component type of that array type, the node is
3117 -- really an indexing of the parameterless call. Resolve as such.
3119 elsif Needs_No_Actuals (Nam)
3121 ((Is_Array_Type (Etype (Nam))
3122 and then Covers (Typ, Component_Type (Etype (Nam))))
3123 or else (Is_Access_Type (Etype (Nam))
3124 and then Is_Array_Type (Designated_Type (Etype (Nam)))
3127 Component_Type (Designated_Type (Etype (Nam))))))
3130 Index_Node : Node_Id;
3134 if Component_Type (Etype (Nam)) /= Any_Type then
3136 Make_Indexed_Component (Loc,
3138 Make_Function_Call (Loc,
3139 Name => New_Occurrence_Of (Nam, Loc)),
3140 Expressions => Parameter_Associations (N));
3142 -- Since we are correcting a node classification error made by
3143 -- the parser, we call Replace rather than Rewrite.
3145 Replace (N, Index_Node);
3146 Set_Etype (Prefix (N), Etype (Nam));
3148 Resolve_Indexed_Component (N, Typ);
3149 Check_Elab_Call (Prefix (N));
3156 Set_Etype (N, Etype (Nam));
3159 -- In the case where the call is to an overloaded subprogram, Analyze
3160 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
3161 -- such a case Normalize_Actuals needs to be called once more to order
3162 -- the actuals correctly. Otherwise the call will have the ordering
3163 -- given by the last overloaded subprogram whether this is the correct
3164 -- one being called or not.
3166 if Is_Overloaded (Subp) then
3167 Normalize_Actuals (N, Nam, False, Norm_OK);
3168 pragma Assert (Norm_OK);
3171 -- In any case, call is fully resolved now. Reset Overload flag, to
3172 -- prevent subsequent overload resolution if node is analyzed again
3174 Set_Is_Overloaded (Subp, False);
3175 Set_Is_Overloaded (N, False);
3177 -- If we are calling the current subprogram from immediately within
3178 -- its body, then that is the case where we can sometimes detect
3179 -- cases of infinite recursion statically. Do not try this in case
3180 -- restriction No_Recursion is in effect anyway.
3182 Scop := Current_Scope;
3185 and then not Restrictions (No_Recursion)
3186 and then Check_Infinite_Recursion (N)
3188 -- Here we detected and flagged an infinite recursion, so we do
3189 -- not need to test the case below for further warnings.
3193 -- If call is to immediately containing subprogram, then check for
3194 -- the case of a possible run-time detectable infinite recursion.
3197 while Scop /= Standard_Standard loop
3199 -- Although in general recursion is not statically checkable,
3200 -- the case of calling an immediately containing subprogram
3201 -- is easy to catch.
3203 Check_Restriction (No_Recursion, N);
3205 -- If the recursive call is to a parameterless procedure, then
3206 -- even if we can't statically detect infinite recursion, this
3207 -- is pretty suspicious, and we output a warning. Furthermore,
3208 -- we will try later to detect some cases here at run time by
3209 -- expanding checking code (see Detect_Infinite_Recursion in
3210 -- package Exp_Ch6).
3211 -- If the recursive call is within a handler we do not emit a
3212 -- warning, because this is a common idiom: loop until input
3213 -- is correct, catch illegal input in handler and restart.
3215 if No (First_Formal (Nam))
3216 and then Etype (Nam) = Standard_Void_Type
3217 and then not Error_Posted (N)
3218 and then Nkind (Parent (N)) /= N_Exception_Handler
3220 Set_Has_Recursive_Call (Nam);
3221 Error_Msg_N ("possible infinite recursion?", N);
3222 Error_Msg_N ("Storage_Error may be raised at run time?", N);
3228 Scop := Scope (Scop);
3232 -- If subprogram name is a predefined operator, it was given in
3233 -- functional notation. Replace call node with operator node, so
3234 -- that actuals can be resolved appropriately.
3236 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
3237 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
3240 elsif Present (Alias (Nam))
3241 and then Is_Predefined_Op (Alias (Nam))
3243 Resolve_Actuals (N, Nam);
3244 Make_Call_Into_Operator (N, Typ, Alias (Nam));
3248 -- Create a transient scope if the resulting type requires it.
3249 -- There are 3 notable exceptions: in init_procs, the transient scope
3250 -- overhead is not needed and even incorrect due to the actual expansion
3251 -- of adjust calls; the second case is enumeration literal pseudo calls,
3252 -- the other case is intrinsic subprograms (Unchecked_Conversion and
3253 -- source information functions) that do not use the secondary stack
3254 -- even though the return type is unconstrained.
3256 -- If this is an initialization call for a type whose initialization
3257 -- uses the secondary stack, we also need to create a transient scope
3258 -- for it, precisely because we will not do it within the init_proc
3262 and then Is_Type (Etype (Nam))
3263 and then Requires_Transient_Scope (Etype (Nam))
3264 and then Ekind (Nam) /= E_Enumeration_Literal
3265 and then not Within_Init_Proc
3266 and then not Is_Intrinsic_Subprogram (Nam)
3268 Establish_Transient_Scope
3269 (N, Sec_Stack => not Functions_Return_By_DSP_On_Target);
3271 elsif Chars (Nam) = Name_uInit_Proc
3272 and then not Within_Init_Proc
3274 Check_Initialization_Call (N, Nam);
3277 -- A protected function cannot be called within the definition of the
3278 -- enclosing protected type.
3280 if Is_Protected_Type (Scope (Nam))
3281 and then In_Open_Scopes (Scope (Nam))
3282 and then not Has_Completion (Scope (Nam))
3285 ("& cannot be called before end of protected definition", N, Nam);
3288 -- Propagate interpretation to actuals, and add default expressions
3291 if Present (First_Formal (Nam)) then
3292 Resolve_Actuals (N, Nam);
3294 -- Overloaded literals are rewritten as function calls, for
3295 -- purpose of resolution. After resolution, we can replace
3296 -- the call with the literal itself.
3298 elsif Ekind (Nam) = E_Enumeration_Literal then
3299 Copy_Node (Subp, N);
3300 Resolve_Entity_Name (N, Typ);
3302 -- Avoid validation, since it is a static function call.
3307 -- If the subprogram is a primitive operation, check whether or not
3308 -- it is a correct dispatching call.
3310 if Is_Overloadable (Nam)
3311 and then Is_Dispatching_Operation (Nam)
3313 Check_Dispatching_Call (N);
3315 -- If the subprogram is abstract, check that the call has a
3316 -- controlling argument (i.e. is dispatching) or is disptaching on
3319 if Is_Abstract (Nam)
3320 and then No (Controlling_Argument (N))
3321 and then not Is_Class_Wide_Type (Typ)
3322 and then not Is_Tag_Indeterminate (N)
3324 Error_Msg_N ("call to abstract subprogram must be dispatching", N);
3327 elsif Is_Abstract (Nam)
3328 and then not In_Instance
3330 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
3333 if Is_Intrinsic_Subprogram (Nam) then
3334 Check_Intrinsic_Call (N);
3337 -- If we fall through we definitely have a non-static call
3339 Check_Elab_Call (N);
3343 -------------------------------
3344 -- Resolve_Character_Literal --
3345 -------------------------------
3347 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
3348 B_Typ : constant Entity_Id := Base_Type (Typ);
3352 -- Verify that the character does belong to the type of the context
3354 Set_Etype (N, B_Typ);
3355 Eval_Character_Literal (N);
3357 -- Wide_Character literals must always be defined, since the set of
3358 -- wide character literals is complete, i.e. if a character literal
3359 -- is accepted by the parser, then it is OK for wide character.
3361 if Root_Type (B_Typ) = Standard_Wide_Character then
3364 -- Always accept character literal for type Any_Character, which
3365 -- occurs in error situations and in comparisons of literals, both
3366 -- of which should accept all literals.
3368 elsif B_Typ = Any_Character then
3371 -- For Standard.Character or a type derived from it, check that
3372 -- the literal is in range
3374 elsif Root_Type (B_Typ) = Standard_Character then
3375 if In_Character_Range (Char_Literal_Value (N)) then
3379 -- If the entity is already set, this has already been resolved in
3380 -- a generic context, or comes from expansion. Nothing else to do.
3382 elsif Present (Entity (N)) then
3385 -- Otherwise we have a user defined character type, and we can use
3386 -- the standard visibility mechanisms to locate the referenced entity
3389 C := Current_Entity (N);
3391 while Present (C) loop
3392 if Etype (C) = B_Typ then
3393 Set_Entity_With_Style_Check (N, C);
3394 Generate_Reference (C, N);
3402 -- If we fall through, then the literal does not match any of the
3403 -- entries of the enumeration type. This isn't just a constraint
3404 -- error situation, it is an illegality (see RM 4.2).
3407 ("character not defined for }", N, First_Subtype (B_Typ));
3409 end Resolve_Character_Literal;
3411 ---------------------------
3412 -- Resolve_Comparison_Op --
3413 ---------------------------
3415 -- Context requires a boolean type, and plays no role in resolution.
3416 -- Processing identical to that for equality operators.
3418 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
3419 L : constant Node_Id := Left_Opnd (N);
3420 R : constant Node_Id := Right_Opnd (N);
3424 -- If this is an intrinsic operation which is not predefined, use
3425 -- the types of its declared arguments to resolve the possibly
3426 -- overloaded operands. Otherwise the operands are unambiguous and
3427 -- specify the expected type.
3429 if Scope (Entity (N)) /= Standard_Standard then
3430 T := Etype (First_Entity (Entity (N)));
3432 T := Find_Unique_Type (L, R);
3434 if T = Any_Fixed then
3435 T := Unique_Fixed_Point_Type (L);
3440 Generate_Reference (T, N, ' ');
3442 if T /= Any_Type then
3445 or else T = Any_Composite
3446 or else T = Any_Character
3448 if T = Any_Character then
3449 Ambiguous_Character (L);
3451 Error_Msg_N ("ambiguous operands for comparison", N);
3454 Set_Etype (N, Any_Type);
3458 if Comes_From_Source (N)
3459 and then Has_Unchecked_Union (T)
3462 ("cannot compare Unchecked_Union values", N);
3467 Check_Unset_Reference (L);
3468 Check_Unset_Reference (R);
3469 Generate_Operator_Reference (N);
3470 Eval_Relational_Op (N);
3474 end Resolve_Comparison_Op;
3476 ------------------------------------
3477 -- Resolve_Conditional_Expression --
3478 ------------------------------------
3480 procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
3481 Condition : constant Node_Id := First (Expressions (N));
3482 Then_Expr : constant Node_Id := Next (Condition);
3483 Else_Expr : constant Node_Id := Next (Then_Expr);
3486 Resolve (Condition, Standard_Boolean);
3487 Resolve (Then_Expr, Typ);
3488 Resolve (Else_Expr, Typ);
3491 Eval_Conditional_Expression (N);
3492 end Resolve_Conditional_Expression;
3494 -----------------------------------------
3495 -- Resolve_Discrete_Subtype_Indication --
3496 -----------------------------------------
3498 procedure Resolve_Discrete_Subtype_Indication
3506 Analyze (Subtype_Mark (N));
3507 S := Entity (Subtype_Mark (N));
3509 if Nkind (Constraint (N)) /= N_Range_Constraint then
3510 Error_Msg_N ("expect range constraint for discrete type", N);
3511 Set_Etype (N, Any_Type);
3514 R := Range_Expression (Constraint (N));
3522 if Base_Type (S) /= Base_Type (Typ) then
3524 ("expect subtype of }", N, First_Subtype (Typ));
3526 -- Rewrite the constraint as a range of Typ
3527 -- to allow compilation to proceed further.
3530 Rewrite (Low_Bound (R),
3531 Make_Attribute_Reference (Sloc (Low_Bound (R)),
3532 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
3533 Attribute_Name => Name_First));
3534 Rewrite (High_Bound (R),
3535 Make_Attribute_Reference (Sloc (High_Bound (R)),
3536 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
3537 Attribute_Name => Name_First));
3541 Set_Etype (N, Etype (R));
3543 -- Additionally, we must check that the bounds are compatible
3544 -- with the given subtype, which might be different from the
3545 -- type of the context.
3547 Apply_Range_Check (R, S);
3549 -- ??? If the above check statically detects a Constraint_Error
3550 -- it replaces the offending bound(s) of the range R with a
3551 -- Constraint_Error node. When the itype which uses these bounds
3552 -- is frozen the resulting call to Duplicate_Subexpr generates
3553 -- a new temporary for the bounds.
3555 -- Unfortunately there are other itypes that are also made depend
3556 -- on these bounds, so when Duplicate_Subexpr is called they get
3557 -- a forward reference to the newly created temporaries and Gigi
3558 -- aborts on such forward references. This is probably sign of a
3559 -- more fundamental problem somewhere else in either the order of
3560 -- itype freezing or the way certain itypes are constructed.
3562 -- To get around this problem we call Remove_Side_Effects right
3563 -- away if either bounds of R are a Constraint_Error.
3566 L : Node_Id := Low_Bound (R);
3567 H : Node_Id := High_Bound (R);
3570 if Nkind (L) = N_Raise_Constraint_Error then
3571 Remove_Side_Effects (L);
3574 if Nkind (H) = N_Raise_Constraint_Error then
3575 Remove_Side_Effects (H);
3579 Check_Unset_Reference (Low_Bound (R));
3580 Check_Unset_Reference (High_Bound (R));
3583 end Resolve_Discrete_Subtype_Indication;
3585 -------------------------
3586 -- Resolve_Entity_Name --
3587 -------------------------
3589 -- Used to resolve identifiers and expanded names
3591 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
3592 E : constant Entity_Id := Entity (N);
3595 -- Replace named numbers by corresponding literals. Note that this is
3596 -- the one case where Resolve_Entity_Name must reset the Etype, since
3597 -- it is currently marked as universal.
3599 if Ekind (E) = E_Named_Integer then
3601 Eval_Named_Integer (N);
3603 elsif Ekind (E) = E_Named_Real then
3605 Eval_Named_Real (N);
3607 -- Allow use of subtype only if it is a concurrent type where we are
3608 -- currently inside the body. This will eventually be expanded
3609 -- into a call to Self (for tasks) or _object (for protected
3610 -- objects). Any other use of a subtype is invalid.
3612 elsif Is_Type (E) then
3613 if Is_Concurrent_Type (E)
3614 and then In_Open_Scopes (E)
3619 ("Invalid use of subtype mark in expression or call", N);
3622 -- Check discriminant use if entity is discriminant in current scope,
3623 -- i.e. discriminant of record or concurrent type currently being
3624 -- analyzed. Uses in corresponding body are unrestricted.
3626 elsif Ekind (E) = E_Discriminant
3627 and then Scope (E) = Current_Scope
3628 and then not Has_Completion (Current_Scope)
3630 Check_Discriminant_Use (N);
3632 -- A parameterless generic function cannot appear in a context that
3633 -- requires resolution.
3635 elsif Ekind (E) = E_Generic_Function then
3636 Error_Msg_N ("illegal use of generic function", N);
3638 elsif Ekind (E) = E_Out_Parameter
3640 and then (Nkind (Parent (N)) in N_Op
3641 or else (Nkind (Parent (N)) = N_Assignment_Statement
3642 and then N = Expression (Parent (N)))
3643 or else Nkind (Parent (N)) = N_Explicit_Dereference)
3645 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
3647 -- In all other cases, just do the possible static evaluation
3650 -- A deferred constant that appears in an expression must have
3651 -- a completion, unless it has been removed by in-place expansion
3654 if Ekind (E) = E_Constant
3655 and then Comes_From_Source (E)
3656 and then No (Constant_Value (E))
3657 and then Is_Frozen (Etype (E))
3658 and then not In_Default_Expression
3659 and then not Is_Imported (E)
3662 if No_Initialization (Parent (E))
3663 or else (Present (Full_View (E))
3664 and then No_Initialization (Parent (Full_View (E))))
3669 "deferred constant is frozen before completion", N);
3673 Eval_Entity_Name (N);
3675 end Resolve_Entity_Name;
3681 procedure Resolve_Entry (Entry_Name : Node_Id) is
3682 Loc : constant Source_Ptr := Sloc (Entry_Name);
3690 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
3691 -- If the bounds of the entry family being called depend on task
3692 -- discriminants, build a new index subtype where a discriminant is
3693 -- replaced with the value of the discriminant of the target task.
3694 -- The target task is the prefix of the entry name in the call.
3696 -----------------------
3697 -- Actual_Index_Type --
3698 -----------------------
3700 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
3701 Typ : Entity_Id := Entry_Index_Type (E);
3702 Tsk : Entity_Id := Scope (E);
3703 Lo : Node_Id := Type_Low_Bound (Typ);
3704 Hi : Node_Id := Type_High_Bound (Typ);
3707 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
3708 -- If the bound is given by a discriminant, replace with a reference
3709 -- to the discriminant of the same name in the target task.
3710 -- If the entry name is the target of a requeue statement and the
3711 -- entry is in the current protected object, the bound to be used
3712 -- is the discriminal of the object (see apply_range_checks for
3713 -- details of the transformation).
3715 -----------------------------
3716 -- Actual_Discriminant_Ref --
3717 -----------------------------
3719 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
3720 Typ : Entity_Id := Etype (Bound);
3724 Remove_Side_Effects (Bound);
3726 if not Is_Entity_Name (Bound)
3727 or else Ekind (Entity (Bound)) /= E_Discriminant
3731 elsif Is_Protected_Type (Tsk)
3732 and then In_Open_Scopes (Tsk)
3733 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
3735 return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
3739 Make_Selected_Component (Loc,
3740 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
3741 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
3746 end Actual_Discriminant_Ref;
3748 -- Start of processing for Actual_Index_Type
3751 if not Has_Discriminants (Tsk)
3752 or else (not Is_Entity_Name (Lo)
3753 and then not Is_Entity_Name (Hi))
3755 return Entry_Index_Type (E);
3758 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
3759 Set_Etype (New_T, Base_Type (Typ));
3760 Set_Size_Info (New_T, Typ);
3761 Set_RM_Size (New_T, RM_Size (Typ));
3762 Set_Scalar_Range (New_T,
3763 Make_Range (Sloc (Entry_Name),
3764 Low_Bound => Actual_Discriminant_Ref (Lo),
3765 High_Bound => Actual_Discriminant_Ref (Hi)));
3769 end Actual_Index_Type;
3771 -- Start of processing of Resolve_Entry
3774 -- Find name of entry being called, and resolve prefix of name
3775 -- with its own type. The prefix can be overloaded, and the name
3776 -- and signature of the entry must be taken into account.
3778 if Nkind (Entry_Name) = N_Indexed_Component then
3780 -- Case of dealing with entry family within the current tasks
3782 E_Name := Prefix (Entry_Name);
3785 E_Name := Entry_Name;
3788 if Is_Entity_Name (E_Name) then
3789 -- Entry call to an entry (or entry family) in the current task.
3790 -- This is legal even though the task will deadlock. Rewrite as
3791 -- call to current task.
3793 -- This can also be a call to an entry in an enclosing task.
3794 -- If this is a single task, we have to retrieve its name,
3795 -- because the scope of the entry is the task type, not the
3796 -- object. If the enclosing task is a task type, the identity
3797 -- of the task is given by its own self variable.
3799 -- Finally this can be a requeue on an entry of the same task
3800 -- or protected object.
3802 S := Scope (Entity (E_Name));
3804 for J in reverse 0 .. Scope_Stack.Last loop
3806 if Is_Task_Type (Scope_Stack.Table (J).Entity)
3807 and then not Comes_From_Source (S)
3809 -- S is an enclosing task or protected object. The concurrent
3810 -- declaration has been converted into a type declaration, and
3811 -- the object itself has an object declaration that follows
3812 -- the type in the same declarative part.
3814 Tsk := Next_Entity (S);
3816 while Etype (Tsk) /= S loop
3823 elsif S = Scope_Stack.Table (J).Entity then
3825 -- Call to current task. Will be transformed into call to Self
3833 Make_Selected_Component (Loc,
3834 Prefix => New_Occurrence_Of (S, Loc),
3836 New_Occurrence_Of (Entity (E_Name), Loc));
3837 Rewrite (E_Name, New_N);
3840 elsif Nkind (Entry_Name) = N_Selected_Component
3841 and then Is_Overloaded (Prefix (Entry_Name))
3843 -- Use the entry name (which must be unique at this point) to
3844 -- find the prefix that returns the corresponding task type or
3848 Pref : Node_Id := Prefix (Entry_Name);
3851 Ent : Entity_Id := Entity (Selector_Name (Entry_Name));
3854 Get_First_Interp (Pref, I, It);
3856 while Present (It.Typ) loop
3858 if Scope (Ent) = It.Typ then
3859 Set_Etype (Pref, It.Typ);
3863 Get_Next_Interp (I, It);
3868 if Nkind (Entry_Name) = N_Selected_Component then
3869 Resolve (Prefix (Entry_Name), Etype (Prefix (Entry_Name)));
3871 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
3872 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
3873 Resolve (Prefix (Prefix (Entry_Name)),
3874 Etype (Prefix (Prefix (Entry_Name))));
3876 Index := First (Expressions (Entry_Name));
3877 Resolve (Index, Entry_Index_Type (Nam));
3879 -- Up to this point the expression could have been the actual
3880 -- in a simple entry call, and be given by a named association.
3882 if Nkind (Index) = N_Parameter_Association then
3883 Error_Msg_N ("expect expression for entry index", Index);
3885 Apply_Range_Check (Index, Actual_Index_Type (Nam));
3891 ------------------------
3892 -- Resolve_Entry_Call --
3893 ------------------------
3895 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
3896 Entry_Name : constant Node_Id := Name (N);
3897 Loc : constant Source_Ptr := Sloc (Entry_Name);
3899 First_Named : Node_Id;
3906 -- Processing of the name is similar for entry calls and protected
3907 -- operation calls. Once the entity is determined, we can complete
3908 -- the resolution of the actuals.
3910 -- The selector may be overloaded, in the case of a protected object
3911 -- with overloaded functions. The type of the context is used for
3914 if Nkind (Entry_Name) = N_Selected_Component
3915 and then Is_Overloaded (Selector_Name (Entry_Name))
3916 and then Typ /= Standard_Void_Type
3923 Get_First_Interp (Selector_Name (Entry_Name), I, It);
3925 while Present (It.Typ) loop
3927 if Covers (Typ, It.Typ) then
3928 Set_Entity (Selector_Name (Entry_Name), It.Nam);
3929 Set_Etype (Entry_Name, It.Typ);
3931 Generate_Reference (It.Typ, N, ' ');
3934 Get_Next_Interp (I, It);
3939 Resolve_Entry (Entry_Name);
3941 if Nkind (Entry_Name) = N_Selected_Component then
3943 -- Simple entry call.
3945 Nam := Entity (Selector_Name (Entry_Name));
3946 Obj := Prefix (Entry_Name);
3947 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
3949 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
3951 -- Call to member of entry family.
3953 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
3954 Obj := Prefix (Prefix (Entry_Name));
3955 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
3958 -- Use context type to disambiguate a protected function that can be
3959 -- called without actuals and that returns an array type, and where
3960 -- the argument list may be an indexing of the returned value.
3962 if Ekind (Nam) = E_Function
3963 and then Needs_No_Actuals (Nam)
3964 and then Present (Parameter_Associations (N))
3966 ((Is_Array_Type (Etype (Nam))
3967 and then Covers (Typ, Component_Type (Etype (Nam))))
3969 or else (Is_Access_Type (Etype (Nam))
3970 and then Is_Array_Type (Designated_Type (Etype (Nam)))
3971 and then Covers (Typ,
3972 Component_Type (Designated_Type (Etype (Nam))))))
3975 Index_Node : Node_Id;
3979 Make_Indexed_Component (Loc,
3981 Make_Function_Call (Loc,
3982 Name => Relocate_Node (Entry_Name)),
3983 Expressions => Parameter_Associations (N));
3985 -- Since we are correcting a node classification error made by
3986 -- the parser, we call Replace rather than Rewrite.
3988 Replace (N, Index_Node);
3989 Set_Etype (Prefix (N), Etype (Nam));
3991 Resolve_Indexed_Component (N, Typ);
3996 -- The operation name may have been overloaded. Order the actuals
3997 -- according to the formals of the resolved entity.
4000 Normalize_Actuals (N, Nam, False, Norm_OK);
4001 pragma Assert (Norm_OK);
4004 Resolve_Actuals (N, Nam);
4005 Generate_Reference (Nam, Entry_Name);
4007 if Ekind (Nam) = E_Entry
4008 or else Ekind (Nam) = E_Entry_Family
4010 Check_Potentially_Blocking_Operation (N);
4013 -- Verify that a procedure call cannot masquerade as an entry
4014 -- call where an entry call is expected.
4016 if Ekind (Nam) = E_Procedure then
4018 if Nkind (Parent (N)) = N_Entry_Call_Alternative
4019 and then N = Entry_Call_Statement (Parent (N))
4021 Error_Msg_N ("entry call required in select statement", N);
4023 elsif Nkind (Parent (N)) = N_Triggering_Alternative
4024 and then N = Triggering_Statement (Parent (N))
4026 Error_Msg_N ("triggering statement cannot be procedure call", N);
4028 elsif Ekind (Scope (Nam)) = E_Task_Type
4029 and then not In_Open_Scopes (Scope (Nam))
4031 Error_Msg_N ("Task has no entry with this name", Entry_Name);
4035 -- After resolution, entry calls and protected procedure calls
4036 -- are changed into entry calls, for expansion. The structure
4037 -- of the node does not change, so it can safely be done in place.
4038 -- Protected function calls must keep their structure because they
4039 -- are subexpressions.
4041 if Ekind (Nam) /= E_Function then
4043 -- A protected operation that is not a function may modify the
4044 -- corresponding object, and cannot apply to a constant.
4045 -- If this is an internal call, the prefix is the type itself.
4047 if Is_Protected_Type (Scope (Nam))
4048 and then not Is_Variable (Obj)
4049 and then (not Is_Entity_Name (Obj)
4050 or else not Is_Type (Entity (Obj)))
4053 ("prefix of protected procedure or entry call must be variable",
4057 Actuals := Parameter_Associations (N);
4058 First_Named := First_Named_Actual (N);
4061 Make_Entry_Call_Statement (Loc,
4063 Parameter_Associations => Actuals));
4065 Set_First_Named_Actual (N, First_Named);
4066 Set_Analyzed (N, True);
4068 -- Protected functions can return on the secondary stack, in which
4069 -- case we must trigger the transient scope mechanism
4071 elsif Expander_Active
4072 and then Requires_Transient_Scope (Etype (Nam))
4074 Establish_Transient_Scope (N,
4075 Sec_Stack => not Functions_Return_By_DSP_On_Target);
4078 end Resolve_Entry_Call;
4080 -------------------------
4081 -- Resolve_Equality_Op --
4082 -------------------------
4084 -- Both arguments must have the same type, and the boolean context
4085 -- does not participate in the resolution. The first pass verifies
4086 -- that the interpretation is not ambiguous, and the type of the left
4087 -- argument is correctly set, or is Any_Type in case of ambiguity.
4088 -- If both arguments are strings or aggregates, allocators, or Null,
4089 -- they are ambiguous even though they carry a single (universal) type.
4090 -- Diagnose this case here.
4092 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
4093 L : constant Node_Id := Left_Opnd (N);
4094 R : constant Node_Id := Right_Opnd (N);
4095 T : Entity_Id := Find_Unique_Type (L, R);
4097 function Find_Unique_Access_Type return Entity_Id;
4098 -- In the case of allocators, make a last-ditch attempt to find a single
4099 -- access type with the right designated type. This is semantically
4100 -- dubious, and of no interest to any real code, but c48008a makes it
4103 -----------------------------
4104 -- Find_Unique_Access_Type --
4105 -----------------------------
4107 function Find_Unique_Access_Type return Entity_Id is
4110 S : Entity_Id := Current_Scope;
4113 if Ekind (Etype (R)) = E_Allocator_Type then
4114 Acc := Designated_Type (Etype (R));
4116 elsif Ekind (Etype (L)) = E_Allocator_Type then
4117 Acc := Designated_Type (Etype (L));
4123 while S /= Standard_Standard loop
4124 E := First_Entity (S);
4126 while Present (E) loop
4129 and then Is_Access_Type (E)
4130 and then Ekind (E) /= E_Allocator_Type
4131 and then Designated_Type (E) = Base_Type (Acc)
4143 end Find_Unique_Access_Type;
4145 -- Start of processing for Resolve_Equality_Op
4148 Set_Etype (N, Base_Type (Typ));
4149 Generate_Reference (T, N, ' ');
4151 if T = Any_Fixed then
4152 T := Unique_Fixed_Point_Type (L);
4155 if T /= Any_Type then
4158 or else T = Any_Composite
4159 or else T = Any_Character
4162 if T = Any_Character then
4163 Ambiguous_Character (L);
4165 Error_Msg_N ("ambiguous operands for equality", N);
4168 Set_Etype (N, Any_Type);
4171 elsif T = Any_Access
4172 or else Ekind (T) = E_Allocator_Type
4174 T := Find_Unique_Access_Type;
4177 Error_Msg_N ("ambiguous operands for equality", N);
4178 Set_Etype (N, Any_Type);
4183 if Comes_From_Source (N)
4184 and then Has_Unchecked_Union (T)
4187 ("cannot compare Unchecked_Union values", N);
4192 Check_Unset_Reference (L);
4193 Check_Unset_Reference (R);
4194 Generate_Operator_Reference (N);
4196 -- If this is an inequality, it may be the implicit inequality
4197 -- created for a user-defined operation, in which case the corres-
4198 -- ponding equality operation is not intrinsic, and the operation
4199 -- cannot be constant-folded. Else fold.
4201 if Nkind (N) = N_Op_Eq
4202 or else Comes_From_Source (Entity (N))
4203 or else Ekind (Entity (N)) = E_Operator
4204 or else Is_Intrinsic_Subprogram
4205 (Corresponding_Equality (Entity (N)))
4207 Eval_Relational_Op (N);
4208 elsif Nkind (N) = N_Op_Ne
4209 and then Is_Abstract (Entity (N))
4211 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
4214 end Resolve_Equality_Op;
4216 ----------------------------------
4217 -- Resolve_Explicit_Dereference --
4218 ----------------------------------
4220 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
4221 P : constant Node_Id := Prefix (N);
4226 -- Now that we know the type, check that this is not a
4227 -- dereference of an uncompleted type. Note that this
4228 -- is not entirely correct, because dereferences of
4229 -- private types are legal in default expressions.
4230 -- This consideration also applies to similar checks
4231 -- for allocators, qualified expressions, and type
4234 Check_Fully_Declared (Typ, N);
4236 if Is_Overloaded (P) then
4238 -- Use the context type to select the prefix that has the
4239 -- correct designated type.
4241 Get_First_Interp (P, I, It);
4242 while Present (It.Typ) loop
4243 exit when Is_Access_Type (It.Typ)
4244 and then Covers (Typ, Designated_Type (It.Typ));
4246 Get_Next_Interp (I, It);
4249 Resolve (P, It.Typ);
4250 Set_Etype (N, Designated_Type (It.Typ));
4253 Resolve (P, Etype (P));
4256 if Is_Access_Type (Etype (P)) then
4257 Apply_Access_Check (N);
4260 -- If the designated type is a packed unconstrained array type,
4261 -- and the explicit dereference is not in the context of an
4262 -- attribute reference, then we must compute and set the actual
4263 -- subtype, since it is needed by Gigi. The reason we exclude
4264 -- the attribute case is that this is handled fine by Gigi, and
4265 -- in fact we use such attributes to build the actual subtype.
4266 -- We also exclude generated code (which builds actual subtypes
4267 -- directly if they are needed).
4269 if Is_Array_Type (Etype (N))
4270 and then Is_Packed (Etype (N))
4271 and then not Is_Constrained (Etype (N))
4272 and then Nkind (Parent (N)) /= N_Attribute_Reference
4273 and then Comes_From_Source (N)
4275 Set_Etype (N, Get_Actual_Subtype (N));
4278 -- Note: there is no Eval processing required for an explicit
4279 -- deference, because the type is known to be an allocators, and
4280 -- allocator expressions can never be static.
4282 end Resolve_Explicit_Dereference;
4284 -------------------------------
4285 -- Resolve_Indexed_Component --
4286 -------------------------------
4288 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
4289 Name : constant Node_Id := Prefix (N);
4291 Array_Type : Entity_Id := Empty; -- to prevent junk warning
4295 if Is_Overloaded (Name) then
4297 -- Use the context type to select the prefix that yields the
4298 -- correct component type.
4303 I1 : Interp_Index := 0;
4304 P : constant Node_Id := Prefix (N);
4305 Found : Boolean := False;
4308 Get_First_Interp (P, I, It);
4310 while Present (It.Typ) loop
4312 if (Is_Array_Type (It.Typ)
4313 and then Covers (Typ, Component_Type (It.Typ)))
4314 or else (Is_Access_Type (It.Typ)
4315 and then Is_Array_Type (Designated_Type (It.Typ))
4317 (Typ, Component_Type (Designated_Type (It.Typ))))
4320 It := Disambiguate (P, I1, I, Any_Type);
4322 if It = No_Interp then
4323 Error_Msg_N ("ambiguous prefix for indexing", N);
4329 Array_Type := It.Typ;
4335 Array_Type := It.Typ;
4340 Get_Next_Interp (I, It);
4345 Array_Type := Etype (Name);
4348 Resolve (Name, Array_Type);
4349 Array_Type := Get_Actual_Subtype_If_Available (Name);
4351 -- If prefix is access type, dereference to get real array type.
4352 -- Note: we do not apply an access check because the expander always
4353 -- introduces an explicit dereference, and the check will happen there.
4355 if Is_Access_Type (Array_Type) then
4356 Array_Type := Designated_Type (Array_Type);
4359 -- If name was overloaded, set component type correctly now.
4361 Set_Etype (N, Component_Type (Array_Type));
4363 Index := First_Index (Array_Type);
4364 Expr := First (Expressions (N));
4366 -- The prefix may have resolved to a string literal, in which case
4367 -- its etype has a special representation. This is only possible
4368 -- currently if the prefix is a static concatenation, written in
4369 -- functional notation.
4371 if Ekind (Array_Type) = E_String_Literal_Subtype then
4372 Resolve (Expr, Standard_Positive);
4375 while Present (Index) and Present (Expr) loop
4376 Resolve (Expr, Etype (Index));
4377 Check_Unset_Reference (Expr);
4379 if Is_Scalar_Type (Etype (Expr)) then
4380 Apply_Scalar_Range_Check (Expr, Etype (Index));
4382 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
4390 Eval_Indexed_Component (N);
4392 end Resolve_Indexed_Component;
4394 -----------------------------
4395 -- Resolve_Integer_Literal --
4396 -----------------------------
4398 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
4401 Eval_Integer_Literal (N);
4402 end Resolve_Integer_Literal;
4404 ---------------------------------
4405 -- Resolve_Intrinsic_Operator --
4406 ---------------------------------
4408 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
4410 Arg1 : Node_Id := Left_Opnd (N);
4411 Arg2 : Node_Id := Right_Opnd (N);
4416 while Scope (Op) /= Standard_Standard loop
4418 pragma Assert (Present (Op));
4423 if Typ /= Etype (Arg1) or else Typ = Etype (Arg2) then
4424 Rewrite (Left_Opnd (N), Convert_To (Typ, Arg1));
4425 Rewrite (Right_Opnd (N), Convert_To (Typ, Arg2));
4427 Analyze (Left_Opnd (N));
4428 Analyze (Right_Opnd (N));
4431 Resolve_Arithmetic_Op (N, Typ);
4432 end Resolve_Intrinsic_Operator;
4434 ------------------------
4435 -- Resolve_Logical_Op --
4436 ------------------------
4438 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
4442 -- Predefined operations on scalar types yield the base type. On
4443 -- the other hand, logical operations on arrays yield the type of
4444 -- the arguments (and the context).
4446 if Is_Array_Type (Typ) then
4449 B_Typ := Base_Type (Typ);
4452 -- The following test is required because the operands of the operation
4453 -- may be literals, in which case the resulting type appears to be
4454 -- compatible with a signed integer type, when in fact it is compatible
4455 -- only with modular types. If the context itself is universal, the
4456 -- operation is illegal.
4458 if not Valid_Boolean_Arg (Typ) then
4459 Error_Msg_N ("invalid context for logical operation", N);
4460 Set_Etype (N, Any_Type);
4463 elsif Typ = Any_Modular then
4465 ("no modular type available in this context", N);
4466 Set_Etype (N, Any_Type);
4470 Resolve (Left_Opnd (N), B_Typ);
4471 Resolve (Right_Opnd (N), B_Typ);
4473 Check_Unset_Reference (Left_Opnd (N));
4474 Check_Unset_Reference (Right_Opnd (N));
4476 Set_Etype (N, B_Typ);
4477 Generate_Operator_Reference (N);
4478 Eval_Logical_Op (N);
4479 end Resolve_Logical_Op;
4481 ---------------------------
4482 -- Resolve_Membership_Op --
4483 ---------------------------
4485 -- The context can only be a boolean type, and does not determine
4486 -- the arguments. Arguments should be unambiguous, but the preference
4487 -- rule for universal types applies.
4489 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
4490 L : constant Node_Id := Left_Opnd (N);
4491 R : constant Node_Id := Right_Opnd (N);
4495 if L = Error or else R = Error then
4499 if not Is_Overloaded (R)
4501 (Etype (R) = Universal_Integer or else
4502 Etype (R) = Universal_Real)
4503 and then Is_Overloaded (L)
4507 T := Intersect_Types (L, R);
4511 Check_Unset_Reference (L);
4513 if Nkind (R) = N_Range
4514 and then not Is_Scalar_Type (T)
4516 Error_Msg_N ("scalar type required for range", R);
4519 if Is_Entity_Name (R) then
4520 Freeze_Expression (R);
4523 Check_Unset_Reference (R);
4526 Eval_Membership_Op (N);
4527 end Resolve_Membership_Op;
4533 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
4535 -- For now allow circumvention of the restriction against
4536 -- anonymous null access values via a debug switch to allow
4537 -- for easier transition.
4540 and then Ekind (Typ) = E_Anonymous_Access_Type
4541 and then Comes_From_Source (N)
4543 -- In the common case of a call which uses an explicitly null
4544 -- value for an access parameter, give specialized error msg
4546 if Nkind (Parent (N)) = N_Procedure_Call_Statement
4548 Nkind (Parent (N)) = N_Function_Call
4551 ("null is not allowed as argument for an access parameter", N);
4553 -- Standard message for all other cases (are there any?)
4557 ("null cannot be of an anonymous access type", N);
4561 -- In a distributed context, null for a remote access to subprogram
4562 -- may need to be replaced with a special record aggregate. In this
4563 -- case, return after having done the transformation.
4565 if (Ekind (Typ) = E_Record_Type
4566 or else Is_Remote_Access_To_Subprogram_Type (Typ))
4567 and then Remote_AST_Null_Value (N, Typ)
4572 -- The null literal takes its type from the context.
4577 -----------------------
4578 -- Resolve_Op_Concat --
4579 -----------------------
4581 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
4582 Btyp : constant Entity_Id := Base_Type (Typ);
4583 Op1 : constant Node_Id := Left_Opnd (N);
4584 Op2 : constant Node_Id := Right_Opnd (N);
4586 procedure Resolve_Concatenation_Arg (Arg : Node_Id; Is_Comp : Boolean);
4587 -- Internal procedure to resolve one operand of concatenation operator.
4588 -- The operand is either of the array type or of the component type.
4589 -- If the operand is an aggregate, and the component type is composite,
4590 -- this is ambiguous if component type has aggregates.
4592 -------------------------------
4593 -- Resolve_Concatenation_Arg --
4594 -------------------------------
4596 procedure Resolve_Concatenation_Arg (Arg : Node_Id; Is_Comp : Boolean) is
4600 or else (not Is_Overloaded (Arg)
4601 and then Etype (Arg) /= Any_Composite
4602 and then Covers (Component_Type (Typ), Etype (Arg)))
4604 Resolve (Arg, Component_Type (Typ));
4606 Resolve (Arg, Btyp);
4609 elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
4611 if Nkind (Arg) = N_Aggregate
4612 and then Is_Composite_Type (Component_Type (Typ))
4614 if Is_Private_Type (Component_Type (Typ)) then
4615 Resolve (Arg, Btyp);
4618 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
4619 Set_Etype (Arg, Any_Type);
4623 if Is_Overloaded (Arg)
4624 and then Has_Compatible_Type (Arg, Typ)
4625 and then Etype (Arg) /= Any_Type
4627 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
4634 Get_First_Interp (Arg, I, It);
4636 while Present (It.Nam) loop
4638 if Base_Type (Etype (It.Nam)) = Base_Type (Typ)
4639 or else Base_Type (Etype (It.Nam)) =
4640 Base_Type (Component_Type (Typ))
4642 Error_Msg_Sloc := Sloc (It.Nam);
4643 Error_Msg_N ("\possible interpretation#", Arg);
4646 Get_Next_Interp (I, It);
4651 Resolve (Arg, Component_Type (Typ));
4653 if Arg = Left_Opnd (N) then
4654 Set_Is_Component_Left_Opnd (N);
4656 Set_Is_Component_Right_Opnd (N);
4661 Resolve (Arg, Btyp);
4664 Check_Unset_Reference (Arg);
4665 end Resolve_Concatenation_Arg;
4667 -- Start of processing for Resolve_Op_Concat
4670 Set_Etype (N, Btyp);
4672 if Is_Limited_Composite (Btyp) then
4673 Error_Msg_N ("concatenation not available for limited array", N);
4676 -- If the operands are themselves concatenations, resolve them as
4677 -- such directly. This removes several layers of recursion and allows
4678 -- GNAT to handle larger multiple concatenations.
4680 if Nkind (Op1) = N_Op_Concat
4681 and then not Is_Array_Type (Component_Type (Typ))
4682 and then Entity (Op1) = Entity (N)
4684 Resolve_Op_Concat (Op1, Typ);
4686 Resolve_Concatenation_Arg
4687 (Op1, Is_Component_Left_Opnd (N));
4690 if Nkind (Op2) = N_Op_Concat
4691 and then not Is_Array_Type (Component_Type (Typ))
4692 and then Entity (Op2) = Entity (N)
4694 Resolve_Op_Concat (Op2, Typ);
4696 Resolve_Concatenation_Arg
4697 (Op2, Is_Component_Right_Opnd (N));
4700 Generate_Operator_Reference (N);
4702 if Is_String_Type (Typ) then
4703 Eval_Concatenation (N);
4706 -- If this is not a static concatenation, but the result is a
4707 -- string type (and not an array of strings) insure that static
4708 -- string operands have their subtypes properly constructed.
4710 if Nkind (N) /= N_String_Literal
4711 and then Is_Character_Type (Component_Type (Typ))
4713 Set_String_Literal_Subtype (Op1, Typ);
4714 Set_String_Literal_Subtype (Op2, Typ);
4716 end Resolve_Op_Concat;
4718 ----------------------
4719 -- Resolve_Op_Expon --
4720 ----------------------
4722 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
4723 B_Typ : constant Entity_Id := Base_Type (Typ);
4726 -- Catch attempts to do fixed-point exponentation with universal
4727 -- operands, which is a case where the illegality is not caught
4728 -- during normal operator analysis.
4730 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
4731 Error_Msg_N ("exponentiation not available for fixed point", N);
4735 if Etype (Left_Opnd (N)) = Universal_Integer
4736 or else Etype (Left_Opnd (N)) = Universal_Real
4738 Check_For_Visible_Operator (N, B_Typ);
4741 -- We do the resolution using the base type, because intermediate values
4742 -- in expressions always are of the base type, not a subtype of it.
4744 Resolve (Left_Opnd (N), B_Typ);
4745 Resolve (Right_Opnd (N), Standard_Integer);
4747 Check_Unset_Reference (Left_Opnd (N));
4748 Check_Unset_Reference (Right_Opnd (N));
4750 Set_Etype (N, B_Typ);
4751 Generate_Operator_Reference (N);
4754 -- Set overflow checking bit. Much cleverer code needed here eventually
4755 -- and perhaps the Resolve routines should be separated for the various
4756 -- arithmetic operations, since they will need different processing. ???
4758 if Nkind (N) in N_Op then
4759 if not Overflow_Checks_Suppressed (Etype (N)) then
4760 Set_Do_Overflow_Check (N, True);
4764 end Resolve_Op_Expon;
4766 --------------------
4767 -- Resolve_Op_Not --
4768 --------------------
4770 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
4773 function Parent_Is_Boolean return Boolean;
4774 -- This function determines if the parent node is a boolean operator
4775 -- or operation (comparison op, membership test, or short circuit form)
4776 -- and the not in question is the left operand of this operation.
4777 -- Note that if the not is in parens, then false is returned.
4779 function Parent_Is_Boolean return Boolean is
4781 if Paren_Count (N) /= 0 then
4785 case Nkind (Parent (N)) is
4800 return Left_Opnd (Parent (N)) = N;
4806 end Parent_Is_Boolean;
4808 -- Start of processing for Resolve_Op_Not
4811 -- Predefined operations on scalar types yield the base type. On
4812 -- the other hand, logical operations on arrays yield the type of
4813 -- the arguments (and the context).
4815 if Is_Array_Type (Typ) then
4818 B_Typ := Base_Type (Typ);
4821 if not Valid_Boolean_Arg (Typ) then
4822 Error_Msg_N ("invalid operand type for operator&", N);
4823 Set_Etype (N, Any_Type);
4826 elsif (Typ = Universal_Integer
4827 or else Typ = Any_Modular)
4829 if Parent_Is_Boolean then
4831 ("operand of not must be enclosed in parentheses",
4835 ("no modular type available in this context", N);
4838 Set_Etype (N, Any_Type);
4842 if not Is_Boolean_Type (Typ)
4843 and then Parent_Is_Boolean
4845 Error_Msg_N ("?not expression should be parenthesized here", N);
4848 Resolve (Right_Opnd (N), B_Typ);
4849 Check_Unset_Reference (Right_Opnd (N));
4850 Set_Etype (N, B_Typ);
4851 Generate_Operator_Reference (N);
4856 -----------------------------
4857 -- Resolve_Operator_Symbol --
4858 -----------------------------
4860 -- Nothing to be done, all resolved already
4862 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
4865 end Resolve_Operator_Symbol;
4867 ----------------------------------
4868 -- Resolve_Qualified_Expression --
4869 ----------------------------------
4871 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
4872 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
4873 Expr : constant Node_Id := Expression (N);
4876 Resolve (Expr, Target_Typ);
4878 -- A qualified expression requires an exact match of the type,
4879 -- class-wide matching is not allowed.
4881 if Is_Class_Wide_Type (Target_Typ)
4882 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
4884 Wrong_Type (Expr, Target_Typ);
4887 -- If the target type is unconstrained, then we reset the type of
4888 -- the result from the type of the expression. For other cases, the
4889 -- actual subtype of the expression is the target type.
4891 if Is_Composite_Type (Target_Typ)
4892 and then not Is_Constrained (Target_Typ)
4894 Set_Etype (N, Etype (Expr));
4897 Eval_Qualified_Expression (N);
4898 end Resolve_Qualified_Expression;
4904 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
4905 L : constant Node_Id := Low_Bound (N);
4906 H : constant Node_Id := High_Bound (N);
4913 Check_Unset_Reference (L);
4914 Check_Unset_Reference (H);
4916 -- We have to check the bounds for being within the base range as
4917 -- required for a non-static context. Normally this is automatic
4918 -- and done as part of evaluating expressions, but the N_Range
4919 -- node is an exception, since in GNAT we consider this node to
4920 -- be a subexpression, even though in Ada it is not. The circuit
4921 -- in Sem_Eval could check for this, but that would put the test
4922 -- on the main evaluation path for expressions.
4924 Check_Non_Static_Context (L);
4925 Check_Non_Static_Context (H);
4929 --------------------------
4930 -- Resolve_Real_Literal --
4931 --------------------------
4933 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
4934 Actual_Typ : constant Entity_Id := Etype (N);
4937 -- Special processing for fixed-point literals to make sure that the
4938 -- value is an exact multiple of small where this is required. We
4939 -- skip this for the universal real case, and also for generic types.
4941 if Is_Fixed_Point_Type (Typ)
4942 and then Typ /= Universal_Fixed
4943 and then Typ /= Any_Fixed
4944 and then not Is_Generic_Type (Typ)
4947 Val : constant Ureal := Realval (N);
4948 Cintr : constant Ureal := Val / Small_Value (Typ);
4949 Cint : constant Uint := UR_Trunc (Cintr);
4950 Den : constant Uint := Norm_Den (Cintr);
4954 -- Case of literal is not an exact multiple of the Small
4958 -- For a source program literal for a decimal fixed-point
4959 -- type, this is statically illegal (RM 4.9(36)).
4961 if Is_Decimal_Fixed_Point_Type (Typ)
4962 and then Actual_Typ = Universal_Real
4963 and then Comes_From_Source (N)
4965 Error_Msg_N ("value has extraneous low order digits", N);
4968 -- Replace literal by a value that is the exact representation
4969 -- of a value of the type, i.e. a multiple of the small value,
4970 -- by truncation, since Machine_Rounds is false for all GNAT
4971 -- fixed-point types (RM 4.9(38)).
4973 Stat := Is_Static_Expression (N);
4975 Make_Real_Literal (Sloc (N),
4976 Realval => Small_Value (Typ) * Cint));
4978 Set_Is_Static_Expression (N, Stat);
4981 -- In all cases, set the corresponding integer field
4983 Set_Corresponding_Integer_Value (N, Cint);
4987 -- Now replace the actual type by the expected type as usual
4990 Eval_Real_Literal (N);
4991 end Resolve_Real_Literal;
4993 -----------------------
4994 -- Resolve_Reference --
4995 -----------------------
4997 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
4998 P : constant Node_Id := Prefix (N);
5001 -- Replace general access with specific type
5003 if Ekind (Etype (N)) = E_Allocator_Type then
5004 Set_Etype (N, Base_Type (Typ));
5007 Resolve (P, Designated_Type (Etype (N)));
5009 -- If we are taking the reference of a volatile entity, then treat
5010 -- it as a potential modification of this entity. This is much too
5011 -- conservative, but is necessary because remove side effects can
5012 -- result in transformations of normal assignments into reference
5013 -- sequences that otherwise fail to notice the modification.
5015 if Is_Entity_Name (P) and then Is_Volatile (Entity (P)) then
5016 Note_Possible_Modification (P);
5018 end Resolve_Reference;
5020 --------------------------------
5021 -- Resolve_Selected_Component --
5022 --------------------------------
5024 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
5026 Comp1 : Entity_Id := Empty; -- prevent junk warning
5027 P : constant Node_Id := Prefix (N);
5028 S : constant Node_Id := Selector_Name (N);
5029 T : Entity_Id := Etype (P);
5031 I1 : Interp_Index := 0; -- prevent junk warning
5036 function Init_Component return Boolean;
5037 -- Check whether this is the initialization of a component within an
5038 -- init_proc (by assignment or call to another init_proc). If true,
5039 -- there is no need for a discriminant check.
5041 --------------------
5042 -- Init_Component --
5043 --------------------
5045 function Init_Component return Boolean is
5047 return Inside_Init_Proc
5048 and then Nkind (Prefix (N)) = N_Identifier
5049 and then Chars (Prefix (N)) = Name_uInit
5050 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
5053 -- Start of processing for Resolve_Selected_Component
5056 if Is_Overloaded (P) then
5058 -- Use the context type to select the prefix that has a selector
5059 -- of the correct name and type.
5062 Get_First_Interp (P, I, It);
5064 Search : while Present (It.Typ) loop
5065 if Is_Access_Type (It.Typ) then
5066 T := Designated_Type (It.Typ);
5071 if Is_Record_Type (T) then
5072 Comp := First_Entity (T);
5074 while Present (Comp) loop
5076 if Chars (Comp) = Chars (S)
5077 and then Covers (Etype (Comp), Typ)
5086 It := Disambiguate (P, I1, I, Any_Type);
5088 if It = No_Interp then
5090 ("ambiguous prefix for selected component", N);
5097 if Scope (Comp1) /= It1.Typ then
5099 -- Resolution chooses the new interpretation.
5100 -- Find the component with the right name.
5102 Comp1 := First_Entity (It1.Typ);
5104 while Present (Comp1)
5105 and then Chars (Comp1) /= Chars (S)
5107 Comp1 := Next_Entity (Comp1);
5116 Comp := Next_Entity (Comp);
5121 Get_Next_Interp (I, It);
5125 Resolve (P, It1.Typ);
5127 Set_Entity (S, Comp1);
5130 -- Resolve prefix with its type.
5135 -- Deal with access type case
5137 if Is_Access_Type (Etype (P)) then
5138 Apply_Access_Check (N);
5139 T := Designated_Type (Etype (P));
5144 if Has_Discriminants (T)
5145 and then Present (Original_Record_Component (Entity (S)))
5146 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
5147 and then Present (Discriminant_Checking_Func
5148 (Original_Record_Component (Entity (S))))
5149 and then not Discriminant_Checks_Suppressed (T)
5150 and then not Init_Component
5152 Set_Do_Discriminant_Check (N);
5155 if Ekind (Entity (S)) = E_Void then
5156 Error_Msg_N ("premature use of component", S);
5159 -- If the prefix is a record conversion, this may be a renamed
5160 -- discriminant whose bounds differ from those of the original
5161 -- one, so we must ensure that a range check is performed.
5163 if Nkind (P) = N_Type_Conversion
5164 and then Ekind (Entity (S)) = E_Discriminant
5166 Set_Etype (N, Base_Type (Typ));
5169 -- Note: No Eval processing is required, because the prefix is of a
5170 -- record type, or protected type, and neither can possibly be static.
5172 end Resolve_Selected_Component;
5178 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
5179 B_Typ : constant Entity_Id := Base_Type (Typ);
5180 L : constant Node_Id := Left_Opnd (N);
5181 R : constant Node_Id := Right_Opnd (N);
5184 -- We do the resolution using the base type, because intermediate values
5185 -- in expressions always are of the base type, not a subtype of it.
5188 Resolve (R, Standard_Natural);
5190 Check_Unset_Reference (L);
5191 Check_Unset_Reference (R);
5193 Set_Etype (N, B_Typ);
5194 Generate_Operator_Reference (N);
5198 ---------------------------
5199 -- Resolve_Short_Circuit --
5200 ---------------------------
5202 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
5203 B_Typ : constant Entity_Id := Base_Type (Typ);
5204 L : constant Node_Id := Left_Opnd (N);
5205 R : constant Node_Id := Right_Opnd (N);
5211 Check_Unset_Reference (L);
5212 Check_Unset_Reference (R);
5214 Set_Etype (N, B_Typ);
5215 Eval_Short_Circuit (N);
5216 end Resolve_Short_Circuit;
5222 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
5223 Name : constant Node_Id := Prefix (N);
5224 Drange : constant Node_Id := Discrete_Range (N);
5225 Array_Type : Entity_Id := Empty;
5229 if Is_Overloaded (Name) then
5231 -- Use the context type to select the prefix that yields the
5232 -- correct array type.
5236 I1 : Interp_Index := 0;
5238 P : constant Node_Id := Prefix (N);
5239 Found : Boolean := False;
5242 Get_First_Interp (P, I, It);
5244 while Present (It.Typ) loop
5246 if (Is_Array_Type (It.Typ)
5247 and then Covers (Typ, It.Typ))
5248 or else (Is_Access_Type (It.Typ)
5249 and then Is_Array_Type (Designated_Type (It.Typ))
5250 and then Covers (Typ, Designated_Type (It.Typ)))
5253 It := Disambiguate (P, I1, I, Any_Type);
5255 if It = No_Interp then
5256 Error_Msg_N ("ambiguous prefix for slicing", N);
5261 Array_Type := It.Typ;
5266 Array_Type := It.Typ;
5271 Get_Next_Interp (I, It);
5276 Array_Type := Etype (Name);
5279 Resolve (Name, Array_Type);
5281 if Is_Access_Type (Array_Type) then
5282 Apply_Access_Check (N);
5283 Array_Type := Designated_Type (Array_Type);
5285 elsif Is_Entity_Name (Name)
5286 or else (Nkind (Name) = N_Function_Call
5287 and then not Is_Constrained (Etype (Name)))
5289 Array_Type := Get_Actual_Subtype (Name);
5292 -- If name was overloaded, set slice type correctly now
5294 Set_Etype (N, Array_Type);
5296 -- If the range is specified by a subtype mark, no resolution
5299 if not Is_Entity_Name (Drange) then
5300 Index := First_Index (Array_Type);
5301 Resolve (Drange, Base_Type (Etype (Index)));
5303 if Nkind (Drange) = N_Range then
5304 Apply_Range_Check (Drange, Etype (Index));
5308 Set_Slice_Subtype (N);
5313 ----------------------------
5314 -- Resolve_String_Literal --
5315 ----------------------------
5317 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
5318 C_Typ : constant Entity_Id := Component_Type (Typ);
5319 R_Typ : constant Entity_Id := Root_Type (C_Typ);
5320 Loc : constant Source_Ptr := Sloc (N);
5321 Str : constant String_Id := Strval (N);
5322 Strlen : constant Nat := String_Length (Str);
5323 Subtype_Id : Entity_Id;
5324 Need_Check : Boolean;
5327 -- For a string appearing in a concatenation, defer creation of the
5328 -- string_literal_subtype until the end of the resolution of the
5329 -- concatenation, because the literal may be constant-folded away.
5330 -- This is a useful optimization for long concatenation expressions.
5332 -- If the string is an aggregate built for a single character (which
5333 -- happens in a non-static context) or a is null string to which special
5334 -- checks may apply, we build the subtype. Wide strings must also get
5335 -- a string subtype if they come from a one character aggregate. Strings
5336 -- generated by attributes might be static, but it is often hard to
5337 -- determine whether the enclosing context is static, so we generate
5338 -- subtypes for them as well, thus losing some rarer optimizations ???
5339 -- Same for strings that come from a static conversion.
5342 (Strlen = 0 and then Typ /= Standard_String)
5343 or else Nkind (Parent (N)) /= N_Op_Concat
5344 or else (N /= Left_Opnd (Parent (N))
5345 and then N /= Right_Opnd (Parent (N)))
5346 or else (Typ = Standard_Wide_String
5347 and then Nkind (Original_Node (N)) /= N_String_Literal);
5349 -- If the resolving type is itself a string literal subtype, we
5350 -- can just reuse it, since there is no point in creating another.
5352 if Ekind (Typ) = E_String_Literal_Subtype then
5355 elsif Nkind (Parent (N)) = N_Op_Concat
5356 and then not Need_Check
5357 and then Nkind (Original_Node (N)) /= N_Character_Literal
5358 and then Nkind (Original_Node (N)) /= N_Attribute_Reference
5359 and then Nkind (Original_Node (N)) /= N_Qualified_Expression
5360 and then Nkind (Original_Node (N)) /= N_Type_Conversion
5364 -- Otherwise we must create a string literal subtype. Note that the
5365 -- whole idea of string literal subtypes is simply to avoid the need
5366 -- for building a full fledged array subtype for each literal.
5368 Set_String_Literal_Subtype (N, Typ);
5369 Subtype_Id := Etype (N);
5372 if Nkind (Parent (N)) /= N_Op_Concat
5375 Set_Etype (N, Subtype_Id);
5376 Eval_String_Literal (N);
5379 if Is_Limited_Composite (Typ)
5380 or else Is_Private_Composite (Typ)
5382 Error_Msg_N ("string literal not available for private array", N);
5383 Set_Etype (N, Any_Type);
5387 -- The validity of a null string has been checked in the
5388 -- call to Eval_String_Literal.
5393 -- Always accept string literal with component type Any_Character,
5394 -- which occurs in error situations and in comparisons of literals,
5395 -- both of which should accept all literals.
5397 elsif R_Typ = Any_Character then
5400 -- If the type is bit-packed, then we always tranform the string
5401 -- literal into a full fledged aggregate.
5403 elsif Is_Bit_Packed_Array (Typ) then
5406 -- Deal with cases of Wide_String and String
5409 -- For Standard.Wide_String, or any other type whose component
5410 -- type is Standard.Wide_Character, we know that all the
5411 -- characters in the string must be acceptable, since the parser
5412 -- accepted the characters as valid character literals.
5414 if R_Typ = Standard_Wide_Character then
5417 -- For the case of Standard.String, or any other type whose
5418 -- component type is Standard.Character, we must make sure that
5419 -- there are no wide characters in the string, i.e. that it is
5420 -- entirely composed of characters in range of type String.
5422 -- If the string literal is the result of a static concatenation,
5423 -- the test has already been performed on the components, and need
5426 elsif R_Typ = Standard_Character
5427 and then Nkind (Original_Node (N)) /= N_Op_Concat
5429 for J in 1 .. Strlen loop
5430 if not In_Character_Range (Get_String_Char (Str, J)) then
5432 -- If we are out of range, post error. This is one of the
5433 -- very few places that we place the flag in the middle of
5434 -- a token, right under the offending wide character.
5437 ("literal out of range of type Character",
5438 Source_Ptr (Int (Loc) + J));
5443 -- If the root type is not a standard character, then we will convert
5444 -- the string into an aggregate and will let the aggregate code do
5452 -- See if the component type of the array corresponding to the
5453 -- string has compile time known bounds. If yes we can directly
5454 -- check whether the evaluation of the string will raise constraint
5455 -- error. Otherwise we need to transform the string literal into
5456 -- the corresponding character aggregate and let the aggregate
5457 -- code do the checking.
5459 if R_Typ = Standard_Wide_Character
5460 or else R_Typ = Standard_Character
5462 -- Check for the case of full range, where we are definitely OK
5464 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
5468 -- Here the range is not the complete base type range, so check
5471 Comp_Typ_Lo : constant Node_Id :=
5472 Type_Low_Bound (Component_Type (Typ));
5473 Comp_Typ_Hi : constant Node_Id :=
5474 Type_High_Bound (Component_Type (Typ));
5479 if Compile_Time_Known_Value (Comp_Typ_Lo)
5480 and then Compile_Time_Known_Value (Comp_Typ_Hi)
5482 for J in 1 .. Strlen loop
5483 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
5485 if Char_Val < Expr_Value (Comp_Typ_Lo)
5486 or else Char_Val > Expr_Value (Comp_Typ_Hi)
5488 Apply_Compile_Time_Constraint_Error
5489 (N, "character out of range?",
5490 Loc => Source_Ptr (Int (Loc) + J));
5500 -- If we got here we meed to transform the string literal into the
5501 -- equivalent qualified positional array aggregate. This is rather
5502 -- heavy artillery for this situation, but it is hard work to avoid.
5505 Lits : List_Id := New_List;
5506 P : Source_Ptr := Loc + 1;
5510 -- Build the character literals, we give them source locations
5511 -- that correspond to the string positions, which is a bit tricky
5512 -- given the possible presence of wide character escape sequences.
5514 for J in 1 .. Strlen loop
5515 C := Get_String_Char (Str, J);
5516 Set_Character_Literal_Name (C);
5519 Make_Character_Literal (P, Name_Find, C));
5521 if In_Character_Range (C) then
5524 -- Should we have a call to Skip_Wide here ???
5532 Make_Qualified_Expression (Loc,
5533 Subtype_Mark => New_Reference_To (Typ, Loc),
5535 Make_Aggregate (Loc, Expressions => Lits)));
5537 Analyze_And_Resolve (N, Typ);
5539 end Resolve_String_Literal;
5541 -----------------------------
5542 -- Resolve_Subprogram_Info --
5543 -----------------------------
5545 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
5548 end Resolve_Subprogram_Info;
5550 -----------------------------
5551 -- Resolve_Type_Conversion --
5552 -----------------------------
5554 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
5555 Target_Type : constant Entity_Id := Etype (N);
5556 Conv_OK : constant Boolean := Conversion_OK (N);
5558 Opnd_Type : Entity_Id;
5562 Operand := Expression (N);
5565 and then not Valid_Conversion (N, Target_Type, Operand)
5570 if Etype (Operand) = Any_Fixed then
5572 -- Mixed-mode operation involving a literal. Context must be a fixed
5573 -- type which is applied to the literal subsequently.
5575 if Is_Fixed_Point_Type (Typ) then
5576 Set_Etype (Operand, Universal_Real);
5578 elsif Is_Numeric_Type (Typ)
5579 and then (Nkind (Operand) = N_Op_Multiply
5580 or else Nkind (Operand) = N_Op_Divide)
5581 and then (Etype (Right_Opnd (Operand)) = Universal_Real
5582 or else Etype (Left_Opnd (Operand)) = Universal_Real)
5584 if Unique_Fixed_Point_Type (N) = Any_Type then
5585 return; -- expression is ambiguous.
5587 Set_Etype (Operand, Standard_Duration);
5590 if Etype (Right_Opnd (Operand)) = Universal_Real then
5591 Rop := New_Copy_Tree (Right_Opnd (Operand));
5593 Rop := New_Copy_Tree (Left_Opnd (Operand));
5596 Resolve (Rop, Standard_Long_Long_Float);
5598 if Realval (Rop) /= Ureal_0
5599 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
5601 Error_Msg_N ("universal real operand can only be interpreted?",
5603 Error_Msg_N ("\as Duration, and will lose precision?", Rop);
5607 Error_Msg_N ("invalid context for mixed mode operation", N);
5608 Set_Etype (Operand, Any_Type);
5613 Opnd_Type := Etype (Operand);
5614 Resolve (Operand, Opnd_Type);
5616 -- Note: we do the Eval_Type_Conversion call before applying the
5617 -- required checks for a subtype conversion. This is important,
5618 -- since both are prepared under certain circumstances to change
5619 -- the type conversion to a constraint error node, but in the case
5620 -- of Eval_Type_Conversion this may reflect an illegality in the
5621 -- static case, and we would miss the illegality (getting only a
5622 -- warning message), if we applied the type conversion checks first.
5624 Eval_Type_Conversion (N);
5626 -- If after evaluation, we still have a type conversion, then we
5627 -- may need to apply checks required for a subtype conversion.
5629 -- Skip these type conversion checks if universal fixed operands
5630 -- operands involved, since range checks are handled separately for
5631 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
5633 if Nkind (N) = N_Type_Conversion
5634 and then not Is_Generic_Type (Root_Type (Target_Type))
5635 and then Target_Type /= Universal_Fixed
5636 and then Opnd_Type /= Universal_Fixed
5638 Apply_Type_Conversion_Checks (N);
5641 -- Issue warning for conversion of simple object to its own type
5643 if Warn_On_Redundant_Constructs
5644 and then Comes_From_Source (N)
5645 and then Nkind (N) = N_Type_Conversion
5646 and then Is_Entity_Name (Expression (N))
5647 and then Etype (Entity (Expression (N))) = Target_Type
5650 ("?useless conversion, & has this type",
5651 N, Entity (Expression (N)));
5653 end Resolve_Type_Conversion;
5655 ----------------------
5656 -- Resolve_Unary_Op --
5657 ----------------------
5659 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
5660 B_Typ : Entity_Id := Base_Type (Typ);
5661 R : constant Node_Id := Right_Opnd (N);
5664 -- Generate warning for expressions like -5 mod 3
5666 if Paren_Count (N) = 0
5667 and then Nkind (N) = N_Op_Minus
5668 and then Nkind (Right_Opnd (N)) = N_Op_Mod
5671 ("?unary minus expression should be parenthesized here", N);
5674 if Etype (R) = Universal_Integer
5675 or else Etype (R) = Universal_Real
5677 Check_For_Visible_Operator (N, B_Typ);
5680 Set_Etype (N, B_Typ);
5682 Check_Unset_Reference (R);
5683 Generate_Operator_Reference (N);
5686 -- Set overflow checking bit. Much cleverer code needed here eventually
5687 -- and perhaps the Resolve routines should be separated for the various
5688 -- arithmetic operations, since they will need different processing ???
5690 if Nkind (N) in N_Op then
5691 if not Overflow_Checks_Suppressed (Etype (N)) then
5692 Set_Do_Overflow_Check (N, True);
5696 end Resolve_Unary_Op;
5698 ----------------------------------
5699 -- Resolve_Unchecked_Expression --
5700 ----------------------------------
5702 procedure Resolve_Unchecked_Expression
5707 Resolve (Expression (N), Typ, Suppress => All_Checks);
5709 end Resolve_Unchecked_Expression;
5711 ---------------------------------------
5712 -- Resolve_Unchecked_Type_Conversion --
5713 ---------------------------------------
5715 procedure Resolve_Unchecked_Type_Conversion
5719 Operand : constant Node_Id := Expression (N);
5720 Opnd_Type : constant Entity_Id := Etype (Operand);
5723 -- Resolve operand using its own type.
5725 Resolve (Operand, Opnd_Type);
5726 Eval_Unchecked_Conversion (N);
5728 end Resolve_Unchecked_Type_Conversion;
5730 ------------------------------
5731 -- Rewrite_Operator_As_Call --
5732 ------------------------------
5734 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
5735 Loc : Source_Ptr := Sloc (N);
5736 Actuals : List_Id := New_List;
5740 if Nkind (N) in N_Binary_Op then
5741 Append (Left_Opnd (N), Actuals);
5744 Append (Right_Opnd (N), Actuals);
5747 Make_Function_Call (Sloc => Loc,
5748 Name => New_Occurrence_Of (Nam, Loc),
5749 Parameter_Associations => Actuals);
5751 Preserve_Comes_From_Source (New_N, N);
5752 Preserve_Comes_From_Source (Name (New_N), N);
5754 Set_Etype (N, Etype (Nam));
5755 end Rewrite_Operator_As_Call;
5757 ------------------------------
5758 -- Rewrite_Renamed_Operator --
5759 ------------------------------
5761 procedure Rewrite_Renamed_Operator (N : Node_Id; Op : Entity_Id) is
5762 Nam : constant Name_Id := Chars (Op);
5763 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
5767 if Chars (N) /= Nam then
5769 -- Rewrite the operator node using the real operator, not its
5772 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
5773 Set_Chars (Op_Node, Nam);
5774 Set_Etype (Op_Node, Etype (N));
5775 Set_Entity (Op_Node, Op);
5776 Set_Right_Opnd (Op_Node, Right_Opnd (N));
5778 Generate_Reference (Op, N);
5781 Set_Left_Opnd (Op_Node, Left_Opnd (N));
5784 Rewrite (N, Op_Node);
5786 end Rewrite_Renamed_Operator;
5788 -----------------------
5789 -- Set_Slice_Subtype --
5790 -----------------------
5792 -- Build an implicit subtype declaration to represent the type delivered
5793 -- by the slice. This is an abbreviated version of an array subtype. We
5794 -- define an index subtype for the slice, using either the subtype name
5795 -- or the discrete range of the slice. To be consistent with index usage
5796 -- elsewhere, we create a list header to hold the single index. This list
5797 -- is not otherwise attached to the syntax tree.
5799 procedure Set_Slice_Subtype (N : Node_Id) is
5800 Loc : constant Source_Ptr := Sloc (N);
5802 Index_List : List_Id := New_List;
5803 Index_Subtype : Entity_Id;
5804 Index_Type : Entity_Id;
5805 Slice_Subtype : Entity_Id;
5806 Drange : constant Node_Id := Discrete_Range (N);
5809 if Is_Entity_Name (Drange) then
5810 Index_Subtype := Entity (Drange);
5813 -- We force the evaluation of a range. This is definitely needed in
5814 -- the renamed case, and seems safer to do unconditionally. Note in
5815 -- any case that since we will create and insert an Itype referring
5816 -- to this range, we must make sure any side effect removal actions
5817 -- are inserted before the Itype definition.
5819 if Nkind (Drange) = N_Range then
5820 Force_Evaluation (Low_Bound (Drange));
5821 Force_Evaluation (High_Bound (Drange));
5824 Index_Type := Base_Type (Etype (Drange));
5826 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
5828 Set_Scalar_Range (Index_Subtype, Drange);
5829 Set_Etype (Index_Subtype, Index_Type);
5830 Set_Size_Info (Index_Subtype, Index_Type);
5831 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
5834 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
5836 Index := New_Occurrence_Of (Index_Subtype, Loc);
5837 Set_Etype (Index, Index_Subtype);
5838 Append (Index, Index_List);
5840 Set_Component_Type (Slice_Subtype, Component_Type (Etype (N)));
5841 Set_First_Index (Slice_Subtype, Index);
5842 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
5843 Set_Is_Constrained (Slice_Subtype, True);
5844 Init_Size_Align (Slice_Subtype);
5846 Check_Compile_Time_Size (Slice_Subtype);
5848 -- The Etype of the existing Slice node is reset to this slice
5849 -- subtype. Its bounds are obtained from its first index.
5851 Set_Etype (N, Slice_Subtype);
5853 -- In the packed case, this must be immediately frozen
5855 -- Couldn't we always freeze here??? and if we did, then the above
5856 -- call to Check_Compile_Time_Size could be eliminated, which would
5857 -- be nice, because then that routine could be made private to Freeze.
5859 if Is_Packed (Slice_Subtype) and not In_Default_Expression then
5860 Freeze_Itype (Slice_Subtype, N);
5863 end Set_Slice_Subtype;
5865 --------------------------------
5866 -- Set_String_Literal_Subtype --
5867 --------------------------------
5869 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
5870 Subtype_Id : Entity_Id;
5873 if Nkind (N) /= N_String_Literal then
5877 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
5880 Set_Component_Type (Subtype_Id, Component_Type (Typ));
5881 Set_String_Literal_Length (Subtype_Id,
5882 UI_From_Int (String_Length (Strval (N))));
5883 Set_Etype (Subtype_Id, Base_Type (Typ));
5884 Set_Is_Constrained (Subtype_Id);
5886 -- The low bound is set from the low bound of the corresponding
5887 -- index type. Note that we do not store the high bound in the
5888 -- string literal subtype, but it can be deduced if necssary
5889 -- from the length and the low bound.
5891 Set_String_Literal_Low_Bound
5892 (Subtype_Id, Type_Low_Bound (Etype (First_Index (Typ))));
5894 Set_Etype (N, Subtype_Id);
5895 end Set_String_Literal_Subtype;
5897 -----------------------------
5898 -- Unique_Fixed_Point_Type --
5899 -----------------------------
5901 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
5902 T1 : Entity_Id := Empty;
5907 procedure Fixed_Point_Error;
5908 -- If true ambiguity, give details.
5910 procedure Fixed_Point_Error is
5912 Error_Msg_N ("ambiguous universal_fixed_expression", N);
5913 Error_Msg_NE ("\possible interpretation as}", N, T1);
5914 Error_Msg_NE ("\possible interpretation as}", N, T2);
5915 end Fixed_Point_Error;
5918 -- The operations on Duration are visible, so Duration is always a
5919 -- possible interpretation.
5921 T1 := Standard_Duration;
5923 Scop := Current_Scope;
5925 -- Look for fixed-point types in enclosing scopes.
5927 while Scop /= Standard_Standard loop
5928 T2 := First_Entity (Scop);
5930 while Present (T2) loop
5931 if Is_Fixed_Point_Type (T2)
5932 and then Current_Entity (T2) = T2
5933 and then Scope (Base_Type (T2)) = Scop
5935 if Present (T1) then
5946 Scop := Scope (Scop);
5949 -- Look for visible fixed type declarations in the context.
5951 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
5953 while Present (Item) loop
5955 if Nkind (Item) = N_With_Clause then
5956 Scop := Entity (Name (Item));
5957 T2 := First_Entity (Scop);
5959 while Present (T2) loop
5960 if Is_Fixed_Point_Type (T2)
5961 and then Scope (Base_Type (T2)) = Scop
5962 and then (Is_Potentially_Use_Visible (T2)
5963 or else In_Use (T2))
5965 if Present (T1) then
5980 if Nkind (N) = N_Real_Literal then
5981 Error_Msg_NE ("real literal interpreted as }?", N, T1);
5984 Error_Msg_NE ("universal_fixed expression interpreted as }?", N, T1);
5988 end Unique_Fixed_Point_Type;
5990 ----------------------
5991 -- Valid_Conversion --
5992 ----------------------
5994 function Valid_Conversion
6000 Target_Type : Entity_Id := Base_Type (Target);
6001 Opnd_Type : Entity_Id := Etype (Operand);
6003 function Conversion_Check
6007 -- Little routine to post Msg if Valid is False, returns Valid value
6009 function Valid_Tagged_Conversion
6010 (Target_Type : Entity_Id;
6011 Opnd_Type : Entity_Id)
6013 -- Specifically test for validity of tagged conversions
6015 ----------------------
6016 -- Conversion_Check --
6017 ----------------------
6019 function Conversion_Check
6026 Error_Msg_N (Msg, Operand);
6030 end Conversion_Check;
6032 -----------------------------
6033 -- Valid_Tagged_Conversion --
6034 -----------------------------
6036 function Valid_Tagged_Conversion
6037 (Target_Type : Entity_Id;
6038 Opnd_Type : Entity_Id)
6042 -- Upward conversions are allowed (RM 4.6(22)).
6044 if Covers (Target_Type, Opnd_Type)
6045 or else Is_Ancestor (Target_Type, Opnd_Type)
6049 -- Downward conversion are allowed if the operand is
6050 -- is class-wide (RM 4.6(23)).
6052 elsif Is_Class_Wide_Type (Opnd_Type)
6053 and then Covers (Opnd_Type, Target_Type)
6057 elsif Covers (Opnd_Type, Target_Type)
6058 or else Is_Ancestor (Opnd_Type, Target_Type)
6061 Conversion_Check (False,
6062 "downward conversion of tagged objects not allowed");
6065 ("invalid tagged conversion, not compatible with}",
6066 N, First_Subtype (Opnd_Type));
6069 end Valid_Tagged_Conversion;
6071 -- Start of processing for Valid_Conversion
6074 Check_Parameterless_Call (Operand);
6076 if Is_Overloaded (Operand) then
6085 -- Remove procedure calls, which syntactically cannot appear
6086 -- in this context, but which cannot be removed by type checking,
6087 -- because the context does not impose a type.
6089 Get_First_Interp (Operand, I, It);
6091 while Present (It.Typ) loop
6093 if It.Typ = Standard_Void_Type then
6097 Get_Next_Interp (I, It);
6100 Get_First_Interp (Operand, I, It);
6105 Error_Msg_N ("illegal operand in conversion", Operand);
6109 Get_Next_Interp (I, It);
6111 if Present (It.Typ) then
6113 It1 := Disambiguate (Operand, I1, I, Any_Type);
6115 if It1 = No_Interp then
6116 Error_Msg_N ("ambiguous operand in conversion", Operand);
6118 Error_Msg_Sloc := Sloc (It.Nam);
6119 Error_Msg_N ("possible interpretation#!", Operand);
6121 Error_Msg_Sloc := Sloc (N1);
6122 Error_Msg_N ("possible interpretation#!", Operand);
6128 Set_Etype (Operand, It1.Typ);
6129 Opnd_Type := It1.Typ;
6133 if Chars (Current_Scope) = Name_Unchecked_Conversion then
6135 -- This check is dubious, what if there were a user defined
6136 -- scope whose name was Unchecked_Conversion ???
6140 elsif Is_Numeric_Type (Target_Type) then
6141 if Opnd_Type = Universal_Fixed then
6144 return Conversion_Check (Is_Numeric_Type (Opnd_Type),
6145 "illegal operand for numeric conversion");
6148 elsif Is_Array_Type (Target_Type) then
6149 if not Is_Array_Type (Opnd_Type)
6150 or else Opnd_Type = Any_Composite
6151 or else Opnd_Type = Any_String
6154 ("illegal operand for array conversion", Operand);
6157 elsif Number_Dimensions (Target_Type) /=
6158 Number_Dimensions (Opnd_Type)
6161 ("incompatible number of dimensions for conversion", Operand);
6166 Target_Index : Node_Id := First_Index (Target_Type);
6167 Opnd_Index : Node_Id := First_Index (Opnd_Type);
6169 Target_Index_Type : Entity_Id;
6170 Opnd_Index_Type : Entity_Id;
6172 Target_Comp_Type : Entity_Id := Component_Type (Target_Type);
6173 Opnd_Comp_Type : Entity_Id := Component_Type (Opnd_Type);
6176 while Present (Target_Index) and then Present (Opnd_Index) loop
6177 Target_Index_Type := Etype (Target_Index);
6178 Opnd_Index_Type := Etype (Opnd_Index);
6180 if not (Is_Integer_Type (Target_Index_Type)
6181 and then Is_Integer_Type (Opnd_Index_Type))
6182 and then (Root_Type (Target_Index_Type)
6183 /= Root_Type (Opnd_Index_Type))
6186 ("incompatible index types for array conversion",
6191 Next_Index (Target_Index);
6192 Next_Index (Opnd_Index);
6195 if Base_Type (Target_Comp_Type) /=
6196 Base_Type (Opnd_Comp_Type)
6199 ("incompatible component types for array conversion",
6204 Is_Constrained (Target_Comp_Type)
6205 /= Is_Constrained (Opnd_Comp_Type)
6206 or else not Subtypes_Statically_Match
6207 (Target_Comp_Type, Opnd_Comp_Type)
6210 ("component subtypes must statically match", Operand);
6219 elsif (Ekind (Target_Type) = E_General_Access_Type
6220 or else Ekind (Target_Type) = E_Anonymous_Access_Type)
6223 (Is_Access_Type (Opnd_Type)
6224 and then Ekind (Opnd_Type) /=
6225 E_Access_Subprogram_Type
6226 and then Ekind (Opnd_Type) /=
6227 E_Access_Protected_Subprogram_Type,
6228 "must be an access-to-object type")
6230 if Is_Access_Constant (Opnd_Type)
6231 and then not Is_Access_Constant (Target_Type)
6234 ("access-to-constant operand type not allowed", Operand);
6238 -- Check the static accessibility rule of 4.6(17). Note that
6239 -- the check is not enforced when within an instance body, since
6240 -- the RM requires such cases to be caught at run time.
6242 if Ekind (Target_Type) /= E_Anonymous_Access_Type then
6243 if Type_Access_Level (Opnd_Type)
6244 > Type_Access_Level (Target_Type)
6246 -- In an instance, this is a run-time check, but one we
6247 -- know will fail, so generate an appropriate warning.
6248 -- The raise will be generated by Expand_N_Type_Conversion.
6250 if In_Instance_Body then
6252 ("?cannot convert local pointer to non-local access type",
6255 ("?Program_Error will be raised at run time", Operand);
6259 ("cannot convert local pointer to non-local access type",
6264 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type then
6266 -- When the operand is a selected access discriminant
6267 -- the check needs to be made against the level of the
6268 -- object denoted by the prefix of the selected name.
6269 -- (Object_Access_Level handles checking the prefix
6270 -- of the operand for this case.)
6272 if Nkind (Operand) = N_Selected_Component
6273 and then Object_Access_Level (Operand)
6274 > Type_Access_Level (Target_Type)
6276 -- In an instance, this is a run-time check, but one we
6277 -- know will fail, so generate an appropriate warning.
6278 -- The raise will be generated by Expand_N_Type_Conversion.
6280 if In_Instance_Body then
6282 ("?cannot convert access discriminant to non-local" &
6283 " access type", Operand);
6285 ("?Program_Error will be raised at run time", Operand);
6289 ("cannot convert access discriminant to non-local" &
6290 " access type", Operand);
6295 -- The case of a reference to an access discriminant
6296 -- from within a type declaration (which will appear
6297 -- as a discriminal) is always illegal because the
6298 -- level of the discriminant is considered to be
6299 -- deeper than any (namable) access type.
6301 if Is_Entity_Name (Operand)
6302 and then (Ekind (Entity (Operand)) = E_In_Parameter
6303 or else Ekind (Entity (Operand)) = E_Constant)
6304 and then Present (Discriminal_Link (Entity (Operand)))
6307 ("discriminant has deeper accessibility level than target",
6315 Target : constant Entity_Id := Designated_Type (Target_Type);
6316 Opnd : constant Entity_Id := Designated_Type (Opnd_Type);
6319 if Is_Tagged_Type (Target) then
6320 return Valid_Tagged_Conversion (Target, Opnd);
6323 if Base_Type (Target) /= Base_Type (Opnd) then
6325 ("target designated type not compatible with }",
6326 N, Base_Type (Opnd));
6329 elsif not Subtypes_Statically_Match (Target, Opnd)
6330 and then (not Has_Discriminants (Target)
6331 or else Is_Constrained (Target))
6334 ("target designated subtype not compatible with }",
6344 elsif Ekind (Target_Type) = E_Access_Subprogram_Type
6345 and then Conversion_Check
6346 (Ekind (Base_Type (Opnd_Type)) = E_Access_Subprogram_Type,
6347 "illegal operand for access subprogram conversion")
6349 -- Check that the designated types are subtype conformant
6351 if not Subtype_Conformant (Designated_Type (Opnd_Type),
6352 Designated_Type (Target_Type))
6355 ("operand type is not subtype conformant with target type",
6359 -- Check the static accessibility rule of 4.6(20)
6361 if Type_Access_Level (Opnd_Type) >
6362 Type_Access_Level (Target_Type)
6365 ("operand type has deeper accessibility level than target",
6368 -- Check that if the operand type is declared in a generic body,
6369 -- then the target type must be declared within that same body
6370 -- (enforces last sentence of 4.6(20)).
6372 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
6374 O_Gen : constant Node_Id :=
6375 Enclosing_Generic_Body (Opnd_Type);
6378 Enclosing_Generic_Body (Target_Type);
6381 while Present (T_Gen) and then T_Gen /= O_Gen loop
6382 T_Gen := Enclosing_Generic_Body (T_Gen);
6385 if T_Gen /= O_Gen then
6387 ("target type must be declared in same generic body"
6388 & " as operand type", N);
6395 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
6396 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
6398 -- It is valid to convert from one RAS type to another provided
6399 -- that their specification statically match.
6401 Check_Subtype_Conformant
6403 Designated_Type (Corresponding_Remote_Type (Target_Type)),
6405 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
6410 elsif Is_Tagged_Type (Target_Type) then
6411 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
6413 -- Types derived from the same root type are convertible.
6415 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
6418 -- In an instance, there may be inconsistent views of the same
6419 -- type, or types derived from the same type.
6422 and then Underlying_Type (Target_Type) = Underlying_Type (Opnd_Type)
6426 -- Special check for common access type error case
6428 elsif Ekind (Target_Type) = E_Access_Type
6429 and then Is_Access_Type (Opnd_Type)
6431 Error_Msg_N ("target type must be general access type!", N);
6432 Error_Msg_NE ("add ALL to }!", N, Target_Type);
6437 Error_Msg_NE ("invalid conversion, not compatible with }",
6442 end Valid_Conversion;