Merge from mainline (160224:163495).
[official-gcc/graphite-test-results.git] / gcc / ada / sem_disp.adb
bloba21337bb600c8ba225dee4b54e44738a927d0e94
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ D I S P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Elists; use Elists;
29 with Einfo; use Einfo;
30 with Exp_Disp; use Exp_Disp;
31 with Exp_Util; use Exp_Util;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Tss; use Exp_Tss;
34 with Errout; use Errout;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Output; use Output;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch3; use Sem_Ch3;
46 with Sem_Ch6; use Sem_Ch6;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Type; use Sem_Type;
49 with Sem_Util; use Sem_Util;
50 with Snames; use Snames;
51 with Sinfo; use Sinfo;
52 with Tbuild; use Tbuild;
53 with Uintp; use Uintp;
55 package body Sem_Disp is
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 procedure Add_Dispatching_Operation
62 (Tagged_Type : Entity_Id;
63 New_Op : Entity_Id);
64 -- Add New_Op in the list of primitive operations of Tagged_Type
66 function Check_Controlling_Type
67 (T : Entity_Id;
68 Subp : Entity_Id) return Entity_Id;
69 -- T is the tagged type of a formal parameter or the result of Subp.
70 -- If the subprogram has a controlling parameter or result that matches
71 -- the type, then returns the tagged type of that parameter or result
72 -- (returning the designated tagged type in the case of an access
73 -- parameter); otherwise returns empty.
75 -------------------------------
76 -- Add_Dispatching_Operation --
77 -------------------------------
79 procedure Add_Dispatching_Operation
80 (Tagged_Type : Entity_Id;
81 New_Op : Entity_Id)
83 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
85 begin
86 -- The dispatching operation may already be on the list, if it is the
87 -- wrapper for an inherited function of a null extension (see Exp_Ch3
88 -- for the construction of function wrappers). The list of primitive
89 -- operations must not contain duplicates.
91 Append_Unique_Elmt (New_Op, List);
92 end Add_Dispatching_Operation;
94 -------------------------------
95 -- Check_Controlling_Formals --
96 -------------------------------
98 procedure Check_Controlling_Formals
99 (Typ : Entity_Id;
100 Subp : Entity_Id)
102 Formal : Entity_Id;
103 Ctrl_Type : Entity_Id;
105 begin
106 Formal := First_Formal (Subp);
107 while Present (Formal) loop
108 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
110 if Present (Ctrl_Type) then
112 -- When controlling type is concurrent and declared within a
113 -- generic or inside an instance use corresponding record type.
115 if Is_Concurrent_Type (Ctrl_Type)
116 and then Present (Corresponding_Record_Type (Ctrl_Type))
117 then
118 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
119 end if;
121 if Ctrl_Type = Typ then
122 Set_Is_Controlling_Formal (Formal);
124 -- Ada 2005 (AI-231): Anonymous access types that are used in
125 -- controlling parameters exclude null because it is necessary
126 -- to read the tag to dispatch, and null has no tag.
128 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
129 Set_Can_Never_Be_Null (Etype (Formal));
130 Set_Is_Known_Non_Null (Etype (Formal));
131 end if;
133 -- Check that the parameter's nominal subtype statically
134 -- matches the first subtype.
136 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
137 if not Subtypes_Statically_Match
138 (Typ, Designated_Type (Etype (Formal)))
139 then
140 Error_Msg_N
141 ("parameter subtype does not match controlling type",
142 Formal);
143 end if;
145 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
146 Error_Msg_N
147 ("parameter subtype does not match controlling type",
148 Formal);
149 end if;
151 if Present (Default_Value (Formal)) then
153 -- In Ada 2005, access parameters can have defaults
155 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
156 and then Ada_Version < Ada_05
157 then
158 Error_Msg_N
159 ("default not allowed for controlling access parameter",
160 Default_Value (Formal));
162 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
163 Error_Msg_N
164 ("default expression must be a tag indeterminate" &
165 " function call", Default_Value (Formal));
166 end if;
167 end if;
169 elsif Comes_From_Source (Subp) then
170 Error_Msg_N
171 ("operation can be dispatching in only one type", Subp);
172 end if;
173 end if;
175 Next_Formal (Formal);
176 end loop;
178 if Ekind_In (Subp, E_Function, E_Generic_Function) then
179 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
181 if Present (Ctrl_Type) then
182 if Ctrl_Type = Typ then
183 Set_Has_Controlling_Result (Subp);
185 -- Check that result subtype statically matches first subtype
186 -- (Ada 2005): Subp may have a controlling access result.
188 if Subtypes_Statically_Match (Typ, Etype (Subp))
189 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
190 and then
191 Subtypes_Statically_Match
192 (Typ, Designated_Type (Etype (Subp))))
193 then
194 null;
196 else
197 Error_Msg_N
198 ("result subtype does not match controlling type", Subp);
199 end if;
201 elsif Comes_From_Source (Subp) then
202 Error_Msg_N
203 ("operation can be dispatching in only one type", Subp);
204 end if;
205 end if;
206 end if;
207 end Check_Controlling_Formals;
209 ----------------------------
210 -- Check_Controlling_Type --
211 ----------------------------
213 function Check_Controlling_Type
214 (T : Entity_Id;
215 Subp : Entity_Id) return Entity_Id
217 Tagged_Type : Entity_Id := Empty;
219 begin
220 if Is_Tagged_Type (T) then
221 if Is_First_Subtype (T) then
222 Tagged_Type := T;
223 else
224 Tagged_Type := Base_Type (T);
225 end if;
227 elsif Ekind (T) = E_Anonymous_Access_Type
228 and then Is_Tagged_Type (Designated_Type (T))
229 then
230 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
231 if Is_First_Subtype (Designated_Type (T)) then
232 Tagged_Type := Designated_Type (T);
233 else
234 Tagged_Type := Base_Type (Designated_Type (T));
235 end if;
237 -- Ada 2005: an incomplete type can be tagged. An operation with an
238 -- access parameter of the type is dispatching.
240 elsif Scope (Designated_Type (T)) = Current_Scope then
241 Tagged_Type := Designated_Type (T);
243 -- Ada 2005 (AI-50217)
245 elsif From_With_Type (Designated_Type (T))
246 and then Present (Non_Limited_View (Designated_Type (T)))
247 then
248 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
249 Tagged_Type := Non_Limited_View (Designated_Type (T));
250 else
251 Tagged_Type := Base_Type (Non_Limited_View
252 (Designated_Type (T)));
253 end if;
254 end if;
255 end if;
257 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
258 return Empty;
260 -- The dispatching type and the primitive operation must be defined in
261 -- the same scope, except in the case of internal operations and formal
262 -- abstract subprograms.
264 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
265 and then (not Is_Generic_Type (Tagged_Type)
266 or else not Comes_From_Source (Subp)))
267 or else
268 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
269 or else
270 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
271 and then
272 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
273 and then
274 Is_Abstract_Subprogram (Subp))
275 then
276 return Tagged_Type;
278 else
279 return Empty;
280 end if;
281 end Check_Controlling_Type;
283 ----------------------------
284 -- Check_Dispatching_Call --
285 ----------------------------
287 procedure Check_Dispatching_Call (N : Node_Id) is
288 Loc : constant Source_Ptr := Sloc (N);
289 Actual : Node_Id;
290 Formal : Entity_Id;
291 Control : Node_Id := Empty;
292 Func : Entity_Id;
293 Subp_Entity : Entity_Id;
294 Indeterm_Ancestor_Call : Boolean := False;
295 Indeterm_Ctrl_Type : Entity_Id;
297 Static_Tag : Node_Id := Empty;
298 -- If a controlling formal has a statically tagged actual, the tag of
299 -- this actual is to be used for any tag-indeterminate actual.
301 procedure Check_Direct_Call;
302 -- In the case when the controlling actual is a class-wide type whose
303 -- root type's completion is a task or protected type, the call is in
304 -- fact direct. This routine detects the above case and modifies the
305 -- call accordingly.
307 procedure Check_Dispatching_Context;
308 -- If the call is tag-indeterminate and the entity being called is
309 -- abstract, verify that the context is a call that will eventually
310 -- provide a tag for dispatching, or has provided one already.
312 -----------------------
313 -- Check_Direct_Call --
314 -----------------------
316 procedure Check_Direct_Call is
317 Typ : Entity_Id := Etype (Control);
319 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean;
320 -- Determine whether an entity denotes a user-defined equality
322 ------------------------------
323 -- Is_User_Defined_Equality --
324 ------------------------------
326 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
327 begin
328 return
329 Ekind (Id) = E_Function
330 and then Chars (Id) = Name_Op_Eq
331 and then Comes_From_Source (Id)
333 -- Internally generated equalities have a full type declaration
334 -- as their parent.
336 and then Nkind (Parent (Id)) = N_Function_Specification;
337 end Is_User_Defined_Equality;
339 -- Start of processing for Check_Direct_Call
341 begin
342 -- Predefined primitives do not receive wrappers since they are built
343 -- from scratch for the corresponding record of synchronized types.
344 -- Equality is in general predefined, but is excluded from the check
345 -- when it is user-defined.
347 if Is_Predefined_Dispatching_Operation (Subp_Entity)
348 and then not Is_User_Defined_Equality (Subp_Entity)
349 then
350 return;
351 end if;
353 if Is_Class_Wide_Type (Typ) then
354 Typ := Root_Type (Typ);
355 end if;
357 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
358 Typ := Full_View (Typ);
359 end if;
361 if Is_Concurrent_Type (Typ)
362 and then
363 Present (Corresponding_Record_Type (Typ))
364 then
365 Typ := Corresponding_Record_Type (Typ);
367 -- The concurrent record's list of primitives should contain a
368 -- wrapper for the entity of the call, retrieve it.
370 declare
371 Prim : Entity_Id;
372 Prim_Elmt : Elmt_Id;
373 Wrapper_Found : Boolean := False;
375 begin
376 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
377 while Present (Prim_Elmt) loop
378 Prim := Node (Prim_Elmt);
380 if Is_Primitive_Wrapper (Prim)
381 and then Wrapped_Entity (Prim) = Subp_Entity
382 then
383 Wrapper_Found := True;
384 exit;
385 end if;
387 Next_Elmt (Prim_Elmt);
388 end loop;
390 -- A primitive declared between two views should have a
391 -- corresponding wrapper.
393 pragma Assert (Wrapper_Found);
395 -- Modify the call by setting the proper entity
397 Set_Entity (Name (N), Prim);
398 end;
399 end if;
400 end Check_Direct_Call;
402 -------------------------------
403 -- Check_Dispatching_Context --
404 -------------------------------
406 procedure Check_Dispatching_Context is
407 Subp : constant Entity_Id := Entity (Name (N));
408 Par : Node_Id;
410 begin
411 if Is_Abstract_Subprogram (Subp)
412 and then No (Controlling_Argument (N))
413 then
414 if Present (Alias (Subp))
415 and then not Is_Abstract_Subprogram (Alias (Subp))
416 and then No (DTC_Entity (Subp))
417 then
418 -- Private overriding of inherited abstract operation, call is
419 -- legal.
421 Set_Entity (Name (N), Alias (Subp));
422 return;
424 else
425 Par := Parent (N);
426 while Present (Par) loop
427 if Nkind_In (Par, N_Function_Call,
428 N_Procedure_Call_Statement,
429 N_Assignment_Statement,
430 N_Op_Eq,
431 N_Op_Ne)
432 and then Is_Tagged_Type (Etype (Subp))
433 then
434 return;
436 elsif Nkind (Par) = N_Qualified_Expression
437 or else Nkind (Par) = N_Unchecked_Type_Conversion
438 then
439 Par := Parent (Par);
441 else
442 if Ekind (Subp) = E_Function then
443 Error_Msg_N
444 ("call to abstract function must be dispatching", N);
446 -- This error can occur for a procedure in the case of a
447 -- call to an abstract formal procedure with a statically
448 -- tagged operand.
450 else
451 Error_Msg_N
452 ("call to abstract procedure must be dispatching",
454 end if;
456 return;
457 end if;
458 end loop;
459 end if;
460 end if;
461 end Check_Dispatching_Context;
463 -- Start of processing for Check_Dispatching_Call
465 begin
466 -- Find a controlling argument, if any
468 if Present (Parameter_Associations (N)) then
469 Subp_Entity := Entity (Name (N));
471 Actual := First_Actual (N);
472 Formal := First_Formal (Subp_Entity);
473 while Present (Actual) loop
474 Control := Find_Controlling_Arg (Actual);
475 exit when Present (Control);
477 -- Check for the case where the actual is a tag-indeterminate call
478 -- whose result type is different than the tagged type associated
479 -- with the containing call, but is an ancestor of the type.
481 if Is_Controlling_Formal (Formal)
482 and then Is_Tag_Indeterminate (Actual)
483 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
484 and then Is_Ancestor (Etype (Actual), Etype (Formal))
485 then
486 Indeterm_Ancestor_Call := True;
487 Indeterm_Ctrl_Type := Etype (Formal);
489 -- If the formal is controlling but the actual is not, the type
490 -- of the actual is statically known, and may be used as the
491 -- controlling tag for some other tag-indeterminate actual.
493 elsif Is_Controlling_Formal (Formal)
494 and then Is_Entity_Name (Actual)
495 and then Is_Tagged_Type (Etype (Actual))
496 then
497 Static_Tag := Actual;
498 end if;
500 Next_Actual (Actual);
501 Next_Formal (Formal);
502 end loop;
504 -- If the call doesn't have a controlling actual but does have an
505 -- indeterminate actual that requires dispatching treatment, then an
506 -- object is needed that will serve as the controlling argument for a
507 -- dispatching call on the indeterminate actual. This can only occur
508 -- in the unusual situation of a default actual given by a
509 -- tag-indeterminate call and where the type of the call is an
510 -- ancestor of the type associated with a containing call to an
511 -- inherited operation (see AI-239).
513 -- Rather than create an object of the tagged type, which would be
514 -- problematic for various reasons (default initialization,
515 -- discriminants), the tag of the containing call's associated tagged
516 -- type is directly used to control the dispatching.
518 if No (Control)
519 and then Indeterm_Ancestor_Call
520 and then No (Static_Tag)
521 then
522 Control :=
523 Make_Attribute_Reference (Loc,
524 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
525 Attribute_Name => Name_Tag);
527 Analyze (Control);
528 end if;
530 if Present (Control) then
532 -- Verify that no controlling arguments are statically tagged
534 if Debug_Flag_E then
535 Write_Str ("Found Dispatching call");
536 Write_Int (Int (N));
537 Write_Eol;
538 end if;
540 Actual := First_Actual (N);
541 while Present (Actual) loop
542 if Actual /= Control then
544 if not Is_Controlling_Actual (Actual) then
545 null; -- Can be anything
547 elsif Is_Dynamically_Tagged (Actual) then
548 null; -- Valid parameter
550 elsif Is_Tag_Indeterminate (Actual) then
552 -- The tag is inherited from the enclosing call (the node
553 -- we are currently analyzing). Explicitly expand the
554 -- actual, since the previous call to Expand (from
555 -- Resolve_Call) had no way of knowing about the required
556 -- dispatching.
558 Propagate_Tag (Control, Actual);
560 else
561 Error_Msg_N
562 ("controlling argument is not dynamically tagged",
563 Actual);
564 return;
565 end if;
566 end if;
568 Next_Actual (Actual);
569 end loop;
571 -- Mark call as a dispatching call
573 Set_Controlling_Argument (N, Control);
574 Check_Restriction (No_Dispatching_Calls, N);
576 -- The dispatching call may need to be converted into a direct
577 -- call in certain cases.
579 Check_Direct_Call;
581 -- If there is a statically tagged actual and a tag-indeterminate
582 -- call to a function of the ancestor (such as that provided by a
583 -- default), then treat this as a dispatching call and propagate
584 -- the tag to the tag-indeterminate call(s).
586 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
587 Control :=
588 Make_Attribute_Reference (Loc,
589 Prefix =>
590 New_Occurrence_Of (Etype (Static_Tag), Loc),
591 Attribute_Name => Name_Tag);
593 Analyze (Control);
595 Actual := First_Actual (N);
596 Formal := First_Formal (Subp_Entity);
597 while Present (Actual) loop
598 if Is_Tag_Indeterminate (Actual)
599 and then Is_Controlling_Formal (Formal)
600 then
601 Propagate_Tag (Control, Actual);
602 end if;
604 Next_Actual (Actual);
605 Next_Formal (Formal);
606 end loop;
608 Check_Dispatching_Context;
610 else
611 -- The call is not dispatching, so check that there aren't any
612 -- tag-indeterminate abstract calls left.
614 Actual := First_Actual (N);
615 while Present (Actual) loop
616 if Is_Tag_Indeterminate (Actual) then
618 -- Function call case
620 if Nkind (Original_Node (Actual)) = N_Function_Call then
621 Func := Entity (Name (Original_Node (Actual)));
623 -- If the actual is an attribute then it can't be abstract
624 -- (the only current case of a tag-indeterminate attribute
625 -- is the stream Input attribute).
627 elsif
628 Nkind (Original_Node (Actual)) = N_Attribute_Reference
629 then
630 Func := Empty;
632 -- Only other possibility is a qualified expression whose
633 -- constituent expression is itself a call.
635 else
636 Func :=
637 Entity (Name
638 (Original_Node
639 (Expression (Original_Node (Actual)))));
640 end if;
642 if Present (Func) and then Is_Abstract_Subprogram (Func) then
643 Error_Msg_N
644 ("call to abstract function must be dispatching", N);
645 end if;
646 end if;
648 Next_Actual (Actual);
649 end loop;
651 Check_Dispatching_Context;
652 end if;
654 else
655 -- If dispatching on result, the enclosing call, if any, will
656 -- determine the controlling argument. Otherwise this is the
657 -- primitive operation of the root type.
659 Check_Dispatching_Context;
660 end if;
661 end Check_Dispatching_Call;
663 ---------------------------------
664 -- Check_Dispatching_Operation --
665 ---------------------------------
667 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
668 Tagged_Type : Entity_Id;
669 Has_Dispatching_Parent : Boolean := False;
670 Body_Is_Last_Primitive : Boolean := False;
672 begin
673 if not Ekind_In (Subp, E_Procedure, E_Function) then
674 return;
675 end if;
677 Set_Is_Dispatching_Operation (Subp, False);
678 Tagged_Type := Find_Dispatching_Type (Subp);
680 -- Ada 2005 (AI-345): Use the corresponding record (if available).
681 -- Required because primitives of concurrent types are be attached
682 -- to the corresponding record (not to the concurrent type).
684 if Ada_Version >= Ada_05
685 and then Present (Tagged_Type)
686 and then Is_Concurrent_Type (Tagged_Type)
687 and then Present (Corresponding_Record_Type (Tagged_Type))
688 then
689 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
690 end if;
692 -- (AI-345): The task body procedure is not a primitive of the tagged
693 -- type
695 if Present (Tagged_Type)
696 and then Is_Concurrent_Record_Type (Tagged_Type)
697 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
698 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
699 and then Subp = Get_Task_Body_Procedure
700 (Corresponding_Concurrent_Type (Tagged_Type))
701 then
702 return;
703 end if;
705 -- If Subp is derived from a dispatching operation then it should
706 -- always be treated as dispatching. In this case various checks
707 -- below will be bypassed. Makes sure that late declarations for
708 -- inherited private subprograms are treated as dispatching, even
709 -- if the associated tagged type is already frozen.
711 Has_Dispatching_Parent :=
712 Present (Alias (Subp))
713 and then Is_Dispatching_Operation (Alias (Subp));
715 if No (Tagged_Type) then
717 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
718 -- with an abstract interface type unless the interface acts as a
719 -- parent type in a derivation. If the interface type is a formal
720 -- type then the operation is not primitive and therefore legal.
722 declare
723 E : Entity_Id;
724 Typ : Entity_Id;
726 begin
727 E := First_Entity (Subp);
728 while Present (E) loop
730 -- For an access parameter, check designated type
732 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
733 Typ := Designated_Type (Etype (E));
734 else
735 Typ := Etype (E);
736 end if;
738 if Comes_From_Source (Subp)
739 and then Is_Interface (Typ)
740 and then not Is_Class_Wide_Type (Typ)
741 and then not Is_Derived_Type (Typ)
742 and then not Is_Generic_Type (Typ)
743 and then not In_Instance
744 then
745 Error_Msg_N ("?declaration of& is too late!", Subp);
746 Error_Msg_NE -- CODEFIX??
747 ("\spec should appear immediately after declaration of &!",
748 Subp, Typ);
749 exit;
750 end if;
752 Next_Entity (E);
753 end loop;
755 -- In case of functions check also the result type
757 if Ekind (Subp) = E_Function then
758 if Is_Access_Type (Etype (Subp)) then
759 Typ := Designated_Type (Etype (Subp));
760 else
761 Typ := Etype (Subp);
762 end if;
764 if not Is_Class_Wide_Type (Typ)
765 and then Is_Interface (Typ)
766 and then not Is_Derived_Type (Typ)
767 then
768 Error_Msg_N ("?declaration of& is too late!", Subp);
769 Error_Msg_NE
770 ("\spec should appear immediately after declaration of &!",
771 Subp, Typ);
772 end if;
773 end if;
774 end;
776 return;
778 -- The subprograms build internally after the freezing point (such as
779 -- init procs, interface thunks, type support subprograms, and Offset
780 -- to top functions for accessing interface components in variable
781 -- size tagged types) are not primitives.
783 elsif Is_Frozen (Tagged_Type)
784 and then not Comes_From_Source (Subp)
785 and then not Has_Dispatching_Parent
786 then
787 -- Complete decoration of internally built subprograms that override
788 -- a dispatching primitive. These entities correspond with the
789 -- following cases:
791 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
792 -- to override functions of nonabstract null extensions. These
793 -- primitives were added to the list of primitives of the tagged
794 -- type by Make_Controlling_Function_Wrappers. However, attribute
795 -- Is_Dispatching_Operation must be set to true.
797 -- 2. Subprograms associated with stream attributes (built by
798 -- New_Stream_Subprogram)
800 if Present (Old_Subp)
801 and then Is_Overriding_Operation (Subp)
802 and then Is_Dispatching_Operation (Old_Subp)
803 then
804 pragma Assert
805 ((Ekind (Subp) = E_Function
806 and then Is_Dispatching_Operation (Old_Subp)
807 and then Is_Null_Extension (Base_Type (Etype (Subp))))
808 or else Get_TSS_Name (Subp) = TSS_Stream_Read
809 or else Get_TSS_Name (Subp) = TSS_Stream_Write);
811 Set_Is_Dispatching_Operation (Subp);
812 end if;
814 return;
816 -- The operation may be a child unit, whose scope is the defining
817 -- package, but which is not a primitive operation of the type.
819 elsif Is_Child_Unit (Subp) then
820 return;
822 -- If the subprogram is not defined in a package spec, the only case
823 -- where it can be a dispatching op is when it overrides an operation
824 -- before the freezing point of the type.
826 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
827 or else In_Package_Body (Scope (Subp)))
828 and then not Has_Dispatching_Parent
829 then
830 if not Comes_From_Source (Subp)
831 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
832 then
833 null;
835 -- If the type is already frozen, the overriding is not allowed
836 -- except when Old_Subp is not a dispatching operation (which can
837 -- occur when Old_Subp was inherited by an untagged type). However,
838 -- a body with no previous spec freezes the type *after* its
839 -- declaration, and therefore is a legal overriding (unless the type
840 -- has already been frozen). Only the first such body is legal.
842 elsif Present (Old_Subp)
843 and then Is_Dispatching_Operation (Old_Subp)
844 then
845 if Comes_From_Source (Subp)
846 and then
847 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
848 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
849 then
850 declare
851 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
852 Decl_Item : Node_Id;
854 begin
855 -- ??? The checks here for whether the type has been
856 -- frozen prior to the new body are not complete. It's
857 -- not simple to check frozenness at this point since
858 -- the body has already caused the type to be prematurely
859 -- frozen in Analyze_Declarations, but we're forced to
860 -- recheck this here because of the odd rule interpretation
861 -- that allows the overriding if the type wasn't frozen
862 -- prior to the body. The freezing action should probably
863 -- be delayed until after the spec is seen, but that's
864 -- a tricky change to the delicate freezing code.
866 -- Look at each declaration following the type up until the
867 -- new subprogram body. If any of the declarations is a body
868 -- then the type has been frozen already so the overriding
869 -- primitive is illegal.
871 Decl_Item := Next (Parent (Tagged_Type));
872 while Present (Decl_Item)
873 and then (Decl_Item /= Subp_Body)
874 loop
875 if Comes_From_Source (Decl_Item)
876 and then (Nkind (Decl_Item) in N_Proper_Body
877 or else Nkind (Decl_Item) in N_Body_Stub)
878 then
879 Error_Msg_N ("overriding of& is too late!", Subp);
880 Error_Msg_N
881 ("\spec should appear immediately after the type!",
882 Subp);
883 exit;
884 end if;
886 Next (Decl_Item);
887 end loop;
889 -- If the subprogram doesn't follow in the list of
890 -- declarations including the type then the type has
891 -- definitely been frozen already and the body is illegal.
893 if No (Decl_Item) then
894 Error_Msg_N ("overriding of& is too late!", Subp);
895 Error_Msg_N
896 ("\spec should appear immediately after the type!",
897 Subp);
899 elsif Is_Frozen (Subp) then
901 -- The subprogram body declares a primitive operation.
902 -- if the subprogram is already frozen, we must update
903 -- its dispatching information explicitly here. The
904 -- information is taken from the overridden subprogram.
905 -- We must also generate a cross-reference entry because
906 -- references to other primitives were already created
907 -- when type was frozen.
909 Body_Is_Last_Primitive := True;
911 if Present (DTC_Entity (Old_Subp)) then
912 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
913 Set_DT_Position (Subp, DT_Position (Old_Subp));
915 if not Restriction_Active (No_Dispatching_Calls) then
916 if Building_Static_DT (Tagged_Type) then
918 -- If the static dispatch table has not been
919 -- built then there is nothing else to do now;
920 -- otherwise we notify that we cannot build the
921 -- static dispatch table.
923 if Has_Dispatch_Table (Tagged_Type) then
924 Error_Msg_N
925 ("overriding of& is too late for building" &
926 " static dispatch tables!", Subp);
927 Error_Msg_N
928 ("\spec should appear immediately after" &
929 " the type!", Subp);
930 end if;
932 else
933 Insert_Actions_After (Subp_Body,
934 Register_Primitive (Sloc (Subp_Body),
935 Prim => Subp));
936 end if;
938 -- Indicate that this is an overriding operation,
939 -- and replace the overriden entry in the list of
940 -- primitive operations, which is used for xref
941 -- generation subsequently.
943 Generate_Reference (Tagged_Type, Subp, 'P', False);
944 Override_Dispatching_Operation
945 (Tagged_Type, Old_Subp, Subp);
946 end if;
947 end if;
948 end if;
949 end;
951 else
952 Error_Msg_N ("overriding of& is too late!", Subp);
953 Error_Msg_N
954 ("\subprogram spec should appear immediately after the type!",
955 Subp);
956 end if;
958 -- If the type is not frozen yet and we are not in the overriding
959 -- case it looks suspiciously like an attempt to define a primitive
960 -- operation, which requires the declaration to be in a package spec
961 -- (3.2.3(6)).
963 elsif not Is_Frozen (Tagged_Type) then
964 Error_Msg_N
965 ("?not dispatching (must be defined in a package spec)", Subp);
966 return;
968 -- When the type is frozen, it is legitimate to define a new
969 -- non-primitive operation.
971 else
972 return;
973 end if;
975 -- Now, we are sure that the scope is a package spec. If the subprogram
976 -- is declared after the freezing point of the type that's an error
978 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
979 Error_Msg_N ("this primitive operation is declared too late", Subp);
980 Error_Msg_NE
981 ("?no primitive operations for& after this line",
982 Freeze_Node (Tagged_Type),
983 Tagged_Type);
984 return;
985 end if;
987 Check_Controlling_Formals (Tagged_Type, Subp);
989 -- Now it should be a correct primitive operation, put it in the list
991 if Present (Old_Subp) then
993 -- If the type has interfaces we complete this check after we set
994 -- attribute Is_Dispatching_Operation.
996 Check_Subtype_Conformant (Subp, Old_Subp);
998 if (Chars (Subp) = Name_Initialize
999 or else Chars (Subp) = Name_Adjust
1000 or else Chars (Subp) = Name_Finalize)
1001 and then Is_Controlled (Tagged_Type)
1002 and then not Is_Visibly_Controlled (Tagged_Type)
1003 then
1004 Set_Is_Overriding_Operation (Subp, False);
1006 -- If the subprogram specification carries an overriding
1007 -- indicator, no need for the warning: it is either redundant,
1008 -- or else an error will be reported.
1010 if Nkind (Parent (Subp)) = N_Procedure_Specification
1011 and then
1012 (Must_Override (Parent (Subp))
1013 or else Must_Not_Override (Parent (Subp)))
1014 then
1015 null;
1017 -- Here we need the warning
1019 else
1020 Error_Msg_NE
1021 ("operation does not override inherited&?", Subp, Subp);
1022 end if;
1024 else
1025 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1026 Set_Is_Overriding_Operation (Subp);
1028 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1029 -- that covers abstract interface subprograms we must register it
1030 -- in all the secondary dispatch tables associated with abstract
1031 -- interfaces. We do this now only if not building static tables.
1032 -- Otherwise the patch code is emitted after those tables are
1033 -- built, to prevent access_before_elaboration in gigi.
1035 if Body_Is_Last_Primitive then
1036 declare
1037 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1038 Elmt : Elmt_Id;
1039 Prim : Node_Id;
1041 begin
1042 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1043 while Present (Elmt) loop
1044 Prim := Node (Elmt);
1046 if Present (Alias (Prim))
1047 and then Present (Interface_Alias (Prim))
1048 and then Alias (Prim) = Subp
1049 and then not Building_Static_DT (Tagged_Type)
1050 then
1051 Insert_Actions_After (Subp_Body,
1052 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1053 end if;
1055 Next_Elmt (Elmt);
1056 end loop;
1058 -- Redisplay the contents of the updated dispatch table
1060 if Debug_Flag_ZZ then
1061 Write_Str ("Late overriding: ");
1062 Write_DT (Tagged_Type);
1063 end if;
1064 end;
1065 end if;
1066 end if;
1068 -- If the tagged type is a concurrent type then we must be compiling
1069 -- with no code generation (we are either compiling a generic unit or
1070 -- compiling under -gnatc mode) because we have previously tested that
1071 -- no serious errors has been reported. In this case we do not add the
1072 -- primitive to the list of primitives of Tagged_Type but we leave the
1073 -- primitive decorated as a dispatching operation to be able to analyze
1074 -- and report errors associated with the Object.Operation notation.
1076 elsif Is_Concurrent_Type (Tagged_Type) then
1077 pragma Assert (not Expander_Active);
1078 null;
1080 -- If no old subprogram, then we add this as a dispatching operation,
1081 -- but we avoid doing this if an error was posted, to prevent annoying
1082 -- cascaded errors.
1084 elsif not Error_Posted (Subp) then
1085 Add_Dispatching_Operation (Tagged_Type, Subp);
1086 end if;
1088 Set_Is_Dispatching_Operation (Subp, True);
1090 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1091 -- subtype conformance against all the interfaces covered by this
1092 -- primitive.
1094 if Present (Old_Subp)
1095 and then Has_Interfaces (Tagged_Type)
1096 then
1097 declare
1098 Ifaces_List : Elist_Id;
1099 Iface_Elmt : Elmt_Id;
1100 Iface_Prim_Elmt : Elmt_Id;
1101 Iface_Prim : Entity_Id;
1102 Ret_Typ : Entity_Id;
1104 begin
1105 Collect_Interfaces (Tagged_Type, Ifaces_List);
1107 Iface_Elmt := First_Elmt (Ifaces_List);
1108 while Present (Iface_Elmt) loop
1109 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1110 Iface_Prim_Elmt :=
1111 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1112 while Present (Iface_Prim_Elmt) loop
1113 Iface_Prim := Node (Iface_Prim_Elmt);
1115 if Is_Interface_Conformant
1116 (Tagged_Type, Iface_Prim, Subp)
1117 then
1118 -- Handle procedures, functions whose return type
1119 -- matches, or functions not returning interfaces
1121 if Ekind (Subp) = E_Procedure
1122 or else Etype (Iface_Prim) = Etype (Subp)
1123 or else not Is_Interface (Etype (Iface_Prim))
1124 then
1125 Check_Subtype_Conformant
1126 (New_Id => Subp,
1127 Old_Id => Iface_Prim,
1128 Err_Loc => Subp,
1129 Skip_Controlling_Formals => True);
1131 -- Handle functions returning interfaces
1133 elsif Implements_Interface
1134 (Etype (Subp), Etype (Iface_Prim))
1135 then
1136 -- Temporarily force both entities to return the
1137 -- same type. Required because Subtype_Conformant
1138 -- does not handle this case.
1140 Ret_Typ := Etype (Iface_Prim);
1141 Set_Etype (Iface_Prim, Etype (Subp));
1143 Check_Subtype_Conformant
1144 (New_Id => Subp,
1145 Old_Id => Iface_Prim,
1146 Err_Loc => Subp,
1147 Skip_Controlling_Formals => True);
1149 Set_Etype (Iface_Prim, Ret_Typ);
1150 end if;
1151 end if;
1153 Next_Elmt (Iface_Prim_Elmt);
1154 end loop;
1155 end if;
1157 Next_Elmt (Iface_Elmt);
1158 end loop;
1159 end;
1160 end if;
1162 if not Body_Is_Last_Primitive then
1163 Set_DT_Position (Subp, No_Uint);
1165 elsif Has_Controlled_Component (Tagged_Type)
1166 and then
1167 (Chars (Subp) = Name_Initialize
1168 or else
1169 Chars (Subp) = Name_Adjust
1170 or else
1171 Chars (Subp) = Name_Finalize)
1172 then
1173 declare
1174 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1175 Decl : Node_Id;
1176 Old_P : Entity_Id;
1177 Old_Bod : Node_Id;
1178 Old_Spec : Entity_Id;
1180 C_Names : constant array (1 .. 3) of Name_Id :=
1181 (Name_Initialize,
1182 Name_Adjust,
1183 Name_Finalize);
1185 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
1186 (TSS_Deep_Initialize,
1187 TSS_Deep_Adjust,
1188 TSS_Deep_Finalize);
1190 begin
1191 -- Remove previous controlled function which was constructed and
1192 -- analyzed when the type was frozen. This requires removing the
1193 -- body of the redefined primitive, as well as its specification
1194 -- if needed (there is no spec created for Deep_Initialize, see
1195 -- exp_ch3.adb). We must also dismantle the exception information
1196 -- that may have been generated for it when front end zero-cost
1197 -- tables are enabled.
1199 for J in D_Names'Range loop
1200 Old_P := TSS (Tagged_Type, D_Names (J));
1202 if Present (Old_P)
1203 and then Chars (Subp) = C_Names (J)
1204 then
1205 Old_Bod := Unit_Declaration_Node (Old_P);
1206 Remove (Old_Bod);
1207 Set_Is_Eliminated (Old_P);
1208 Set_Scope (Old_P, Scope (Current_Scope));
1210 if Nkind (Old_Bod) = N_Subprogram_Body
1211 and then Present (Corresponding_Spec (Old_Bod))
1212 then
1213 Old_Spec := Corresponding_Spec (Old_Bod);
1214 Set_Has_Completion (Old_Spec, False);
1215 end if;
1216 end if;
1217 end loop;
1219 Build_Late_Proc (Tagged_Type, Chars (Subp));
1221 -- The new operation is added to the actions of the freeze node
1222 -- for the type, but this node has already been analyzed, so we
1223 -- must retrieve and analyze explicitly the new body.
1225 if Present (F_Node)
1226 and then Present (Actions (F_Node))
1227 then
1228 Decl := Last (Actions (F_Node));
1229 Analyze (Decl);
1230 end if;
1231 end;
1232 end if;
1233 end Check_Dispatching_Operation;
1235 ------------------------------------------
1236 -- Check_Operation_From_Incomplete_Type --
1237 ------------------------------------------
1239 procedure Check_Operation_From_Incomplete_Type
1240 (Subp : Entity_Id;
1241 Typ : Entity_Id)
1243 Full : constant Entity_Id := Full_View (Typ);
1244 Parent_Typ : constant Entity_Id := Etype (Full);
1245 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1246 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1247 Op1, Op2 : Elmt_Id;
1248 Prev : Elmt_Id := No_Elmt;
1250 function Derives_From (Proc : Entity_Id) return Boolean;
1251 -- Check that Subp has the signature of an operation derived from Proc.
1252 -- Subp has an access parameter that designates Typ.
1254 ------------------
1255 -- Derives_From --
1256 ------------------
1258 function Derives_From (Proc : Entity_Id) return Boolean is
1259 F1, F2 : Entity_Id;
1261 begin
1262 if Chars (Proc) /= Chars (Subp) then
1263 return False;
1264 end if;
1266 F1 := First_Formal (Proc);
1267 F2 := First_Formal (Subp);
1268 while Present (F1) and then Present (F2) loop
1269 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1270 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1271 return False;
1272 elsif Designated_Type (Etype (F1)) = Parent_Typ
1273 and then Designated_Type (Etype (F2)) /= Full
1274 then
1275 return False;
1276 end if;
1278 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1279 return False;
1281 elsif Etype (F1) /= Etype (F2) then
1282 return False;
1283 end if;
1285 Next_Formal (F1);
1286 Next_Formal (F2);
1287 end loop;
1289 return No (F1) and then No (F2);
1290 end Derives_From;
1292 -- Start of processing for Check_Operation_From_Incomplete_Type
1294 begin
1295 -- The operation may override an inherited one, or may be a new one
1296 -- altogether. The inherited operation will have been hidden by the
1297 -- current one at the point of the type derivation, so it does not
1298 -- appear in the list of primitive operations of the type. We have to
1299 -- find the proper place of insertion in the list of primitive opera-
1300 -- tions by iterating over the list for the parent type.
1302 Op1 := First_Elmt (Old_Prim);
1303 Op2 := First_Elmt (New_Prim);
1304 while Present (Op1) and then Present (Op2) loop
1305 if Derives_From (Node (Op1)) then
1306 if No (Prev) then
1308 -- Avoid adding it to the list of primitives if already there!
1310 if Node (Op2) /= Subp then
1311 Prepend_Elmt (Subp, New_Prim);
1312 end if;
1314 else
1315 Insert_Elmt_After (Subp, Prev);
1316 end if;
1318 return;
1319 end if;
1321 Prev := Op2;
1322 Next_Elmt (Op1);
1323 Next_Elmt (Op2);
1324 end loop;
1326 -- Operation is a new primitive
1328 Append_Elmt (Subp, New_Prim);
1329 end Check_Operation_From_Incomplete_Type;
1331 ---------------------------------------
1332 -- Check_Operation_From_Private_View --
1333 ---------------------------------------
1335 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1336 Tagged_Type : Entity_Id;
1338 begin
1339 if Is_Dispatching_Operation (Alias (Subp)) then
1340 Set_Scope (Subp, Current_Scope);
1341 Tagged_Type := Find_Dispatching_Type (Subp);
1343 -- Add Old_Subp to primitive operations if not already present
1345 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1346 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1348 -- If Old_Subp isn't already marked as dispatching then
1349 -- this is the case of an operation of an untagged private
1350 -- type fulfilled by a tagged type that overrides an
1351 -- inherited dispatching operation, so we set the necessary
1352 -- dispatching attributes here.
1354 if not Is_Dispatching_Operation (Old_Subp) then
1356 -- If the untagged type has no discriminants, and the full
1357 -- view is constrained, there will be a spurious mismatch
1358 -- of subtypes on the controlling arguments, because the tagged
1359 -- type is the internal base type introduced in the derivation.
1360 -- Use the original type to verify conformance, rather than the
1361 -- base type.
1363 if not Comes_From_Source (Tagged_Type)
1364 and then Has_Discriminants (Tagged_Type)
1365 then
1366 declare
1367 Formal : Entity_Id;
1369 begin
1370 Formal := First_Formal (Old_Subp);
1371 while Present (Formal) loop
1372 if Tagged_Type = Base_Type (Etype (Formal)) then
1373 Tagged_Type := Etype (Formal);
1374 end if;
1376 Next_Formal (Formal);
1377 end loop;
1378 end;
1380 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1381 Tagged_Type := Etype (Old_Subp);
1382 end if;
1383 end if;
1385 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1386 Set_Is_Dispatching_Operation (Old_Subp, True);
1387 Set_DT_Position (Old_Subp, No_Uint);
1388 end if;
1390 -- If the old subprogram is an explicit renaming of some other
1391 -- entity, it is not overridden by the inherited subprogram.
1392 -- Otherwise, update its alias and other attributes.
1394 if Present (Alias (Old_Subp))
1395 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1396 N_Subprogram_Renaming_Declaration
1397 then
1398 Set_Alias (Old_Subp, Alias (Subp));
1400 -- The derived subprogram should inherit the abstractness
1401 -- of the parent subprogram (except in the case of a function
1402 -- returning the type). This sets the abstractness properly
1403 -- for cases where a private extension may have inherited
1404 -- an abstract operation, but the full type is derived from
1405 -- a descendant type and inherits a nonabstract version.
1407 if Etype (Subp) /= Tagged_Type then
1408 Set_Is_Abstract_Subprogram
1409 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1410 end if;
1411 end if;
1412 end if;
1413 end if;
1414 end Check_Operation_From_Private_View;
1416 --------------------------
1417 -- Find_Controlling_Arg --
1418 --------------------------
1420 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1421 Orig_Node : constant Node_Id := Original_Node (N);
1422 Typ : Entity_Id;
1424 begin
1425 if Nkind (Orig_Node) = N_Qualified_Expression then
1426 return Find_Controlling_Arg (Expression (Orig_Node));
1427 end if;
1429 -- Dispatching on result case. If expansion is disabled, the node still
1430 -- has the structure of a function call. However, if the function name
1431 -- is an operator and the call was given in infix form, the original
1432 -- node has no controlling result and we must examine the current node.
1434 if Nkind (N) = N_Function_Call
1435 and then Present (Controlling_Argument (N))
1436 and then Has_Controlling_Result (Entity (Name (N)))
1437 then
1438 return Controlling_Argument (N);
1440 -- If expansion is enabled, the call may have been transformed into
1441 -- an indirect call, and we need to recover the original node.
1443 elsif Nkind (Orig_Node) = N_Function_Call
1444 and then Present (Controlling_Argument (Orig_Node))
1445 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1446 then
1447 return Controlling_Argument (Orig_Node);
1449 -- Normal case
1451 elsif Is_Controlling_Actual (N)
1452 or else
1453 (Nkind (Parent (N)) = N_Qualified_Expression
1454 and then Is_Controlling_Actual (Parent (N)))
1455 then
1456 Typ := Etype (N);
1458 if Is_Access_Type (Typ) then
1460 -- In the case of an Access attribute, use the type of the prefix,
1461 -- since in the case of an actual for an access parameter, the
1462 -- attribute's type may be of a specific designated type, even
1463 -- though the prefix type is class-wide.
1465 if Nkind (N) = N_Attribute_Reference then
1466 Typ := Etype (Prefix (N));
1468 -- An allocator is dispatching if the type of qualified expression
1469 -- is class_wide, in which case this is the controlling type.
1471 elsif Nkind (Orig_Node) = N_Allocator
1472 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1473 then
1474 Typ := Etype (Expression (Orig_Node));
1475 else
1476 Typ := Designated_Type (Typ);
1477 end if;
1478 end if;
1480 if Is_Class_Wide_Type (Typ)
1481 or else
1482 (Nkind (Parent (N)) = N_Qualified_Expression
1483 and then Is_Access_Type (Etype (N))
1484 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1485 then
1486 return N;
1487 end if;
1488 end if;
1490 return Empty;
1491 end Find_Controlling_Arg;
1493 ---------------------------
1494 -- Find_Dispatching_Type --
1495 ---------------------------
1497 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1498 A_Formal : Entity_Id;
1499 Formal : Entity_Id;
1500 Ctrl_Type : Entity_Id;
1502 begin
1503 if Present (DTC_Entity (Subp)) then
1504 return Scope (DTC_Entity (Subp));
1506 -- For subprograms internally generated by derivations of tagged types
1507 -- use the alias subprogram as a reference to locate the dispatching
1508 -- type of Subp.
1510 elsif not Comes_From_Source (Subp)
1511 and then Present (Alias (Subp))
1512 and then Is_Dispatching_Operation (Alias (Subp))
1513 then
1514 if Ekind (Alias (Subp)) = E_Function
1515 and then Has_Controlling_Result (Alias (Subp))
1516 then
1517 return Check_Controlling_Type (Etype (Subp), Subp);
1519 else
1520 Formal := First_Formal (Subp);
1521 A_Formal := First_Formal (Alias (Subp));
1522 while Present (A_Formal) loop
1523 if Is_Controlling_Formal (A_Formal) then
1524 return Check_Controlling_Type (Etype (Formal), Subp);
1525 end if;
1527 Next_Formal (Formal);
1528 Next_Formal (A_Formal);
1529 end loop;
1531 pragma Assert (False);
1532 return Empty;
1533 end if;
1535 -- General case
1537 else
1538 Formal := First_Formal (Subp);
1539 while Present (Formal) loop
1540 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1542 if Present (Ctrl_Type) then
1543 return Ctrl_Type;
1544 end if;
1546 Next_Formal (Formal);
1547 end loop;
1549 -- The subprogram may also be dispatching on result
1551 if Present (Etype (Subp)) then
1552 return Check_Controlling_Type (Etype (Subp), Subp);
1553 end if;
1554 end if;
1556 pragma Assert (not Is_Dispatching_Operation (Subp));
1557 return Empty;
1558 end Find_Dispatching_Type;
1560 ---------------------------------------
1561 -- Find_Primitive_Covering_Interface --
1562 ---------------------------------------
1564 function Find_Primitive_Covering_Interface
1565 (Tagged_Type : Entity_Id;
1566 Iface_Prim : Entity_Id) return Entity_Id
1568 E : Entity_Id;
1570 begin
1571 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1572 or else (Present (Alias (Iface_Prim))
1573 and then
1574 Is_Interface
1575 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1577 E := Current_Entity (Iface_Prim);
1578 while Present (E) loop
1579 if Is_Subprogram (E)
1580 and then Is_Dispatching_Operation (E)
1581 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1582 then
1583 return E;
1584 end if;
1586 E := Homonym (E);
1587 end loop;
1589 return Empty;
1590 end Find_Primitive_Covering_Interface;
1592 ---------------------------
1593 -- Is_Dynamically_Tagged --
1594 ---------------------------
1596 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1597 begin
1598 if Nkind (N) = N_Error then
1599 return False;
1600 else
1601 return Find_Controlling_Arg (N) /= Empty;
1602 end if;
1603 end Is_Dynamically_Tagged;
1605 --------------------------
1606 -- Is_Tag_Indeterminate --
1607 --------------------------
1609 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1610 Nam : Entity_Id;
1611 Actual : Node_Id;
1612 Orig_Node : constant Node_Id := Original_Node (N);
1614 begin
1615 if Nkind (Orig_Node) = N_Function_Call
1616 and then Is_Entity_Name (Name (Orig_Node))
1617 then
1618 Nam := Entity (Name (Orig_Node));
1620 if not Has_Controlling_Result (Nam) then
1621 return False;
1623 -- An explicit dereference means that the call has already been
1624 -- expanded and there is no tag to propagate.
1626 elsif Nkind (N) = N_Explicit_Dereference then
1627 return False;
1629 -- If there are no actuals, the call is tag-indeterminate
1631 elsif No (Parameter_Associations (Orig_Node)) then
1632 return True;
1634 else
1635 Actual := First_Actual (Orig_Node);
1636 while Present (Actual) loop
1637 if Is_Controlling_Actual (Actual)
1638 and then not Is_Tag_Indeterminate (Actual)
1639 then
1640 return False; -- one operand is dispatching
1641 end if;
1643 Next_Actual (Actual);
1644 end loop;
1646 return True;
1647 end if;
1649 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1650 return Is_Tag_Indeterminate (Expression (Orig_Node));
1652 -- Case of a call to the Input attribute (possibly rewritten), which is
1653 -- always tag-indeterminate except when its prefix is a Class attribute.
1655 elsif Nkind (Orig_Node) = N_Attribute_Reference
1656 and then
1657 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1658 and then
1659 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1660 then
1661 return True;
1663 -- In Ada 2005 a function that returns an anonymous access type can
1664 -- dispatching, and the dereference of a call to such a function
1665 -- is also tag-indeterminate.
1667 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1668 and then Ada_Version >= Ada_05
1669 then
1670 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1672 else
1673 return False;
1674 end if;
1675 end Is_Tag_Indeterminate;
1677 ------------------------------------
1678 -- Override_Dispatching_Operation --
1679 ------------------------------------
1681 procedure Override_Dispatching_Operation
1682 (Tagged_Type : Entity_Id;
1683 Prev_Op : Entity_Id;
1684 New_Op : Entity_Id)
1686 Elmt : Elmt_Id;
1687 Prim : Node_Id;
1689 begin
1690 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1691 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1693 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1694 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1695 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1696 end if;
1698 -- If there is no previous operation to override, the type declaration
1699 -- was malformed, and an error must have been emitted already.
1701 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1702 while Present (Elmt)
1703 and then Node (Elmt) /= Prev_Op
1704 loop
1705 Next_Elmt (Elmt);
1706 end loop;
1708 if No (Elmt) then
1709 return;
1710 end if;
1712 -- The location of entities that come from source in the list of
1713 -- primitives of the tagged type must follow their order of occurrence
1714 -- in the sources to fulfill the C++ ABI. If the overriden entity is a
1715 -- primitive of an interface that is not an ancestor of this tagged
1716 -- type (that is, it is an entity added to the list of primitives by
1717 -- Derive_Interface_Progenitors), then we must append the new entity
1718 -- at the end of the list of primitives.
1720 if Present (Alias (Prev_Op))
1721 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
1722 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
1723 Tagged_Type)
1724 then
1725 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
1726 Append_Elmt (New_Op, Primitive_Operations (Tagged_Type));
1728 -- The new primitive replaces the overriden entity. Required to ensure
1729 -- that overriding primitive is assigned the same dispatch table slot.
1731 else
1732 Replace_Elmt (Elmt, New_Op);
1733 end if;
1735 if Ada_Version >= Ada_05
1736 and then Has_Interfaces (Tagged_Type)
1737 then
1738 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1739 -- entities of the overridden primitive to reference New_Op, and also
1740 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1741 -- that the new operation is subtype conformant with the interface
1742 -- operations that it implements (for operations inherited from the
1743 -- parent itself, this check is made when building the derived type).
1745 -- Note: This code is only executed in case of late overriding
1747 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1748 while Present (Elmt) loop
1749 Prim := Node (Elmt);
1751 if Prim = New_Op then
1752 null;
1754 -- Note: The check on Is_Subprogram protects the frontend against
1755 -- reading attributes in entities that are not yet fully decorated
1757 elsif Is_Subprogram (Prim)
1758 and then Present (Interface_Alias (Prim))
1759 and then Alias (Prim) = Prev_Op
1760 and then Present (Etype (New_Op))
1761 then
1762 Set_Alias (Prim, New_Op);
1763 Check_Subtype_Conformant (New_Op, Prim);
1764 Set_Is_Abstract_Subprogram (Prim,
1765 Is_Abstract_Subprogram (New_Op));
1767 -- Ensure that this entity will be expanded to fill the
1768 -- corresponding entry in its dispatch table.
1770 if not Is_Abstract_Subprogram (Prim) then
1771 Set_Has_Delayed_Freeze (Prim);
1772 end if;
1773 end if;
1775 Next_Elmt (Elmt);
1776 end loop;
1777 end if;
1779 if (not Is_Package_Or_Generic_Package (Current_Scope))
1780 or else not In_Private_Part (Current_Scope)
1781 then
1782 -- Not a private primitive
1784 null;
1786 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1788 -- Make the overriding operation into an alias of the implicit one.
1789 -- In this fashion a call from outside ends up calling the new body
1790 -- even if non-dispatching, and a call from inside calls the
1791 -- overriding operation because it hides the implicit one. To
1792 -- indicate that the body of Prev_Op is never called, set its
1793 -- dispatch table entity to Empty. If the overridden operation
1794 -- has a dispatching result, so does the overriding one.
1796 Set_Alias (Prev_Op, New_Op);
1797 Set_DTC_Entity (Prev_Op, Empty);
1798 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
1799 return;
1800 end if;
1801 end Override_Dispatching_Operation;
1803 -------------------
1804 -- Propagate_Tag --
1805 -------------------
1807 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1808 Call_Node : Node_Id;
1809 Arg : Node_Id;
1811 begin
1812 if Nkind (Actual) = N_Function_Call then
1813 Call_Node := Actual;
1815 elsif Nkind (Actual) = N_Identifier
1816 and then Nkind (Original_Node (Actual)) = N_Function_Call
1817 then
1818 -- Call rewritten as object declaration when stack-checking is
1819 -- enabled. Propagate tag to expression in declaration, which is
1820 -- original call.
1822 Call_Node := Expression (Parent (Entity (Actual)));
1824 -- Ada 2005: If this is a dereference of a call to a function with a
1825 -- dispatching access-result, the tag is propagated when the dereference
1826 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1828 elsif Nkind (Actual) = N_Explicit_Dereference
1829 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1830 then
1831 return;
1833 -- Only other possibilities are parenthesized or qualified expression,
1834 -- or an expander-generated unchecked conversion of a function call to
1835 -- a stream Input attribute.
1837 else
1838 Call_Node := Expression (Actual);
1839 end if;
1841 -- Do not set the Controlling_Argument if already set. This happens in
1842 -- the special case of _Input (see Exp_Attr, case Input).
1844 if No (Controlling_Argument (Call_Node)) then
1845 Set_Controlling_Argument (Call_Node, Control);
1846 end if;
1848 Arg := First_Actual (Call_Node);
1850 while Present (Arg) loop
1851 if Is_Tag_Indeterminate (Arg) then
1852 Propagate_Tag (Control, Arg);
1853 end if;
1855 Next_Actual (Arg);
1856 end loop;
1858 -- Expansion of dispatching calls is suppressed when VM_Target, because
1859 -- the VM back-ends directly handle the generation of dispatching calls
1860 -- and would have to undo any expansion to an indirect call.
1862 if Tagged_Type_Expansion then
1863 Expand_Dispatching_Call (Call_Node);
1865 -- Expansion of a dispatching call results in an indirect call, which in
1866 -- turn causes current values to be killed (see Resolve_Call), so on VM
1867 -- targets we do the call here to ensure consistent warnings between VM
1868 -- and non-VM targets.
1870 else
1871 Kill_Current_Values;
1872 end if;
1873 end Propagate_Tag;
1875 end Sem_Disp;