Properly regenerate gcc/configure.
[official-gcc.git] / gcc / ada / exp_disp.adb
blob99105e0ea4f1052aee2dc73522dfc9e975362279
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ D I S P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, 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 Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_CG; use Exp_CG;
35 with Exp_Dbug; use Exp_Dbug;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Itypes; use Itypes;
40 with Layout; use Layout;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Namet; use Namet;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch7; use Sem_Ch7;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with SCIL_LL; use SCIL_LL;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
68 package body Exp_Disp is
70 -----------------------
71 -- Local Subprograms --
72 -----------------------
74 function Default_Prim_Op_Position (E : Entity_Id) return Uint;
75 -- Ada 2005 (AI-251): Returns the fixed position in the dispatch table
76 -- of the default primitive operations.
78 function Has_DT (Typ : Entity_Id) return Boolean;
79 pragma Inline (Has_DT);
80 -- Returns true if we generate a dispatch table for tagged type Typ
82 function Is_Predefined_Dispatching_Alias (Prim : Entity_Id) return Boolean;
83 -- Returns true if Prim is not a predefined dispatching primitive but it is
84 -- an alias of a predefined dispatching primitive (i.e. through a renaming)
86 function New_Value (From : Node_Id) return Node_Id;
87 -- From is the original Expression. New_Value is equivalent to a call to
88 -- Duplicate_Subexpr with an explicit dereference when From is an access
89 -- parameter.
91 function Original_View_In_Visible_Part (Typ : Entity_Id) return Boolean;
92 -- Check if the type has a private view or if the public view appears in
93 -- the visible part of a package spec.
95 function Prim_Op_Kind
96 (Prim : Entity_Id;
97 Typ : Entity_Id) return Node_Id;
98 -- Ada 2005 (AI-345): Determine the primitive operation kind of Prim
99 -- according to its type Typ. Return a reference to an RE_Prim_Op_Kind
100 -- enumeration value.
102 function Tagged_Kind (T : Entity_Id) return Node_Id;
103 -- Ada 2005 (AI-345): Determine the tagged kind of T and return a reference
104 -- to an RE_Tagged_Kind enumeration value.
106 ----------------------
107 -- Apply_Tag_Checks --
108 ----------------------
110 procedure Apply_Tag_Checks (Call_Node : Node_Id) is
111 Loc : constant Source_Ptr := Sloc (Call_Node);
112 Ctrl_Arg : constant Node_Id := Controlling_Argument (Call_Node);
113 Ctrl_Typ : constant Entity_Id := Base_Type (Etype (Ctrl_Arg));
114 Param_List : constant List_Id := Parameter_Associations (Call_Node);
116 Subp : Entity_Id;
117 CW_Typ : Entity_Id;
118 Param : Node_Id;
119 Typ : Entity_Id;
120 Eq_Prim_Op : Entity_Id := Empty;
122 begin
123 if No_Run_Time_Mode then
124 Error_Msg_CRT ("tagged types", Call_Node);
125 return;
126 end if;
128 -- Apply_Tag_Checks is called directly from the semantics, so we
129 -- need a check to see whether expansion is active before proceeding.
130 -- In addition, there is no need to expand the call when compiling
131 -- under restriction No_Dispatching_Calls; the semantic analyzer has
132 -- previously notified the violation of this restriction.
134 if not Expander_Active
135 or else Restriction_Active (No_Dispatching_Calls)
136 then
137 return;
138 end if;
140 -- Set subprogram. If this is an inherited operation that was
141 -- overridden, the body that is being called is its alias.
143 Subp := Entity (Name (Call_Node));
145 if Present (Alias (Subp))
146 and then Is_Inherited_Operation (Subp)
147 and then No (DTC_Entity (Subp))
148 then
149 Subp := Alias (Subp);
150 end if;
152 -- Definition of the class-wide type and the tagged type
154 -- If the controlling argument is itself a tag rather than a tagged
155 -- object, then use the class-wide type associated with the subprogram's
156 -- controlling type. This case can occur when a call to an inherited
157 -- primitive has an actual that originated from a default parameter
158 -- given by a tag-indeterminate call and when there is no other
159 -- controlling argument providing the tag (AI-239 requires dispatching).
160 -- This capability of dispatching directly by tag is also needed by the
161 -- implementation of AI-260 (for the generic dispatching constructors).
163 if Ctrl_Typ = RTE (RE_Tag)
164 or else (RTE_Available (RE_Interface_Tag)
165 and then Ctrl_Typ = RTE (RE_Interface_Tag))
166 then
167 CW_Typ := Class_Wide_Type (Find_Dispatching_Type (Subp));
169 -- Class_Wide_Type is applied to the expressions used to initialize
170 -- CW_Typ, to ensure that CW_Typ always denotes a class-wide type, since
171 -- there are cases where the controlling type is resolved to a specific
172 -- type (such as for designated types of arguments such as CW'Access).
174 elsif Is_Access_Type (Ctrl_Typ) then
175 CW_Typ := Class_Wide_Type (Designated_Type (Ctrl_Typ));
177 else
178 CW_Typ := Class_Wide_Type (Ctrl_Typ);
179 end if;
181 Typ := Find_Specific_Type (CW_Typ);
183 if not Is_Limited_Type (Typ) then
184 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
185 end if;
187 -- Dispatching call to C++ primitive
189 if Is_CPP_Class (Typ) then
190 null;
192 -- Dispatching call to Ada primitive
194 elsif Present (Param_List) then
196 -- Generate the Tag checks when appropriate
198 Param := First_Actual (Call_Node);
199 while Present (Param) loop
201 -- No tag check with itself
203 if Param = Ctrl_Arg then
204 null;
206 -- No tag check for parameter whose type is neither tagged nor
207 -- access to tagged (for access parameters)
209 elsif No (Find_Controlling_Arg (Param)) then
210 null;
212 -- No tag check for function dispatching on result if the
213 -- Tag given by the context is this one
215 elsif Find_Controlling_Arg (Param) = Ctrl_Arg then
216 null;
218 -- "=" is the only dispatching operation allowed to get operands
219 -- with incompatible tags (it just returns false). We use
220 -- Duplicate_Subexpr_Move_Checks instead of calling Relocate_Node
221 -- because the value will be duplicated to check the tags.
223 elsif Subp = Eq_Prim_Op then
224 null;
226 -- No check in presence of suppress flags
228 elsif Tag_Checks_Suppressed (Etype (Param))
229 or else (Is_Access_Type (Etype (Param))
230 and then Tag_Checks_Suppressed
231 (Designated_Type (Etype (Param))))
232 then
233 null;
235 -- Optimization: no tag checks if the parameters are identical
237 elsif Is_Entity_Name (Param)
238 and then Is_Entity_Name (Ctrl_Arg)
239 and then Entity (Param) = Entity (Ctrl_Arg)
240 then
241 null;
243 -- Now we need to generate the Tag check
245 else
246 -- Generate code for tag equality check
248 -- Perhaps should have Checks.Apply_Tag_Equality_Check???
250 Insert_Action (Ctrl_Arg,
251 Make_Implicit_If_Statement (Call_Node,
252 Condition =>
253 Make_Op_Ne (Loc,
254 Left_Opnd =>
255 Make_Selected_Component (Loc,
256 Prefix => New_Value (Ctrl_Arg),
257 Selector_Name =>
258 New_Occurrence_Of
259 (First_Tag_Component (Typ), Loc)),
261 Right_Opnd =>
262 Make_Selected_Component (Loc,
263 Prefix =>
264 Unchecked_Convert_To (Typ, New_Value (Param)),
265 Selector_Name =>
266 New_Occurrence_Of
267 (First_Tag_Component (Typ), Loc))),
269 Then_Statements =>
270 New_List (New_Constraint_Error (Loc))));
271 end if;
273 Next_Actual (Param);
274 end loop;
275 end if;
276 end Apply_Tag_Checks;
278 ------------------------
279 -- Building_Static_DT --
280 ------------------------
282 function Building_Static_DT (Typ : Entity_Id) return Boolean is
283 Root_Typ : Entity_Id := Root_Type (Typ);
285 begin
286 -- Handle private types
288 if Present (Full_View (Root_Typ)) then
289 Root_Typ := Full_View (Root_Typ);
290 end if;
292 return Static_Dispatch_Tables
293 and then Is_Library_Level_Tagged_Type (Typ)
294 and then VM_Target = No_VM
296 -- If the type is derived from a CPP class we cannot statically
297 -- build the dispatch tables because we must inherit primitives
298 -- from the CPP side.
300 and then not Is_CPP_Class (Root_Typ);
301 end Building_Static_DT;
303 ----------------------------------
304 -- Build_Static_Dispatch_Tables --
305 ----------------------------------
307 procedure Build_Static_Dispatch_Tables (N : Entity_Id) is
308 Target_List : List_Id;
310 procedure Build_Dispatch_Tables (List : List_Id);
311 -- Build the static dispatch table of tagged types found in the list of
312 -- declarations. The generated nodes are added at the end of Target_List
314 procedure Build_Package_Dispatch_Tables (N : Node_Id);
315 -- Build static dispatch tables associated with package declaration N
317 ---------------------------
318 -- Build_Dispatch_Tables --
319 ---------------------------
321 procedure Build_Dispatch_Tables (List : List_Id) is
322 D : Node_Id;
324 begin
325 D := First (List);
326 while Present (D) loop
328 -- Handle nested packages and package bodies recursively. The
329 -- generated code is placed on the Target_List established for
330 -- the enclosing compilation unit.
332 if Nkind (D) = N_Package_Declaration then
333 Build_Package_Dispatch_Tables (D);
335 elsif Nkind (D) = N_Package_Body then
336 Build_Dispatch_Tables (Declarations (D));
338 elsif Nkind (D) = N_Package_Body_Stub
339 and then Present (Library_Unit (D))
340 then
341 Build_Dispatch_Tables
342 (Declarations (Proper_Body (Unit (Library_Unit (D)))));
344 -- Handle full type declarations and derivations of library level
345 -- tagged types
347 elsif Nkind_In (D, N_Full_Type_Declaration,
348 N_Derived_Type_Definition)
349 and then Is_Library_Level_Tagged_Type (Defining_Entity (D))
350 and then Ekind (Defining_Entity (D)) /= E_Record_Subtype
351 and then not Is_Private_Type (Defining_Entity (D))
352 then
353 -- We do not generate dispatch tables for the internal types
354 -- created for a type extension with unknown discriminants
355 -- The needed information is shared with the source type,
356 -- See Expand_N_Record_Extension.
358 if Is_Underlying_Record_View (Defining_Entity (D))
359 or else
360 (not Comes_From_Source (Defining_Entity (D))
361 and then
362 Has_Unknown_Discriminants (Etype (Defining_Entity (D)))
363 and then
364 not Comes_From_Source
365 (First_Subtype (Defining_Entity (D))))
366 then
367 null;
368 else
369 Insert_List_After_And_Analyze (Last (Target_List),
370 Make_DT (Defining_Entity (D)));
371 end if;
373 -- Handle private types of library level tagged types. We must
374 -- exchange the private and full-view to ensure the correct
375 -- expansion. If the full view is a synchronized type ignore
376 -- the type because the table will be built for the corresponding
377 -- record type, that has its own declaration.
379 elsif (Nkind (D) = N_Private_Type_Declaration
380 or else Nkind (D) = N_Private_Extension_Declaration)
381 and then Present (Full_View (Defining_Entity (D)))
382 then
383 declare
384 E1 : constant Entity_Id := Defining_Entity (D);
385 E2 : constant Entity_Id := Full_View (E1);
387 begin
388 if Is_Library_Level_Tagged_Type (E2)
389 and then Ekind (E2) /= E_Record_Subtype
390 and then not Is_Concurrent_Type (E2)
391 then
392 Exchange_Declarations (E1);
393 Insert_List_After_And_Analyze (Last (Target_List),
394 Make_DT (E1));
395 Exchange_Declarations (E2);
396 end if;
397 end;
398 end if;
400 Next (D);
401 end loop;
402 end Build_Dispatch_Tables;
404 -----------------------------------
405 -- Build_Package_Dispatch_Tables --
406 -----------------------------------
408 procedure Build_Package_Dispatch_Tables (N : Node_Id) is
409 Spec : constant Node_Id := Specification (N);
410 Id : constant Entity_Id := Defining_Entity (N);
411 Vis_Decls : constant List_Id := Visible_Declarations (Spec);
412 Priv_Decls : constant List_Id := Private_Declarations (Spec);
414 begin
415 Push_Scope (Id);
417 if Present (Priv_Decls) then
418 Build_Dispatch_Tables (Vis_Decls);
419 Build_Dispatch_Tables (Priv_Decls);
421 elsif Present (Vis_Decls) then
422 Build_Dispatch_Tables (Vis_Decls);
423 end if;
425 Pop_Scope;
426 end Build_Package_Dispatch_Tables;
428 -- Start of processing for Build_Static_Dispatch_Tables
430 begin
431 if not Expander_Active
432 or else not Tagged_Type_Expansion
433 then
434 return;
435 end if;
437 if Nkind (N) = N_Package_Declaration then
438 declare
439 Spec : constant Node_Id := Specification (N);
440 Vis_Decls : constant List_Id := Visible_Declarations (Spec);
441 Priv_Decls : constant List_Id := Private_Declarations (Spec);
443 begin
444 if Present (Priv_Decls)
445 and then Is_Non_Empty_List (Priv_Decls)
446 then
447 Target_List := Priv_Decls;
449 elsif not Present (Vis_Decls) then
450 Target_List := New_List;
451 Set_Private_Declarations (Spec, Target_List);
452 else
453 Target_List := Vis_Decls;
454 end if;
456 Build_Package_Dispatch_Tables (N);
457 end;
459 else pragma Assert (Nkind (N) = N_Package_Body);
460 Target_List := Declarations (N);
461 Build_Dispatch_Tables (Target_List);
462 end if;
463 end Build_Static_Dispatch_Tables;
465 ------------------------------
466 -- Convert_Tag_To_Interface --
467 ------------------------------
469 function Convert_Tag_To_Interface
470 (Typ : Entity_Id;
471 Expr : Node_Id) return Node_Id
473 Loc : constant Source_Ptr := Sloc (Expr);
474 Anon_Type : Entity_Id;
475 Result : Node_Id;
477 begin
478 pragma Assert (Is_Class_Wide_Type (Typ)
479 and then Is_Interface (Typ)
480 and then
481 ((Nkind (Expr) = N_Selected_Component
482 and then Is_Tag (Entity (Selector_Name (Expr))))
483 or else
484 (Nkind (Expr) = N_Function_Call
485 and then RTE_Available (RE_Displace)
486 and then Entity (Name (Expr)) = RTE (RE_Displace))));
488 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Expr);
489 Set_Directly_Designated_Type (Anon_Type, Typ);
490 Set_Etype (Anon_Type, Anon_Type);
491 Set_Can_Never_Be_Null (Anon_Type);
493 -- Decorate the size and alignment attributes of the anonymous access
494 -- type, as required by the back end.
496 Layout_Type (Anon_Type);
498 if Nkind (Expr) = N_Selected_Component
499 and then Is_Tag (Entity (Selector_Name (Expr)))
500 then
501 Result :=
502 Make_Explicit_Dereference (Loc,
503 Unchecked_Convert_To (Anon_Type,
504 Make_Attribute_Reference (Loc,
505 Prefix => Expr,
506 Attribute_Name => Name_Address)));
507 else
508 Result :=
509 Make_Explicit_Dereference (Loc,
510 Unchecked_Convert_To (Anon_Type, Expr));
511 end if;
513 return Result;
514 end Convert_Tag_To_Interface;
516 -------------------
517 -- CPP_Num_Prims --
518 -------------------
520 function CPP_Num_Prims (Typ : Entity_Id) return Nat is
521 CPP_Typ : Entity_Id;
522 Tag_Comp : Entity_Id;
524 begin
525 if not Is_Tagged_Type (Typ)
526 or else not Is_CPP_Class (Root_Type (Typ))
527 then
528 return 0;
530 else
531 CPP_Typ := Enclosing_CPP_Parent (Typ);
532 Tag_Comp := First_Tag_Component (CPP_Typ);
534 -- If number of primitives already set in the tag component, use it
536 if Present (Tag_Comp)
537 and then DT_Entry_Count (Tag_Comp) /= No_Uint
538 then
539 return UI_To_Int (DT_Entry_Count (Tag_Comp));
541 -- Otherwise, count the primitives of the enclosing CPP type
543 else
544 declare
545 Count : Nat := 0;
546 Elmt : Elmt_Id;
548 begin
549 Elmt := First_Elmt (Primitive_Operations (CPP_Typ));
550 while Present (Elmt) loop
551 Count := Count + 1;
552 Next_Elmt (Elmt);
553 end loop;
555 return Count;
556 end;
557 end if;
558 end if;
559 end CPP_Num_Prims;
561 ------------------------------
562 -- Default_Prim_Op_Position --
563 ------------------------------
565 function Default_Prim_Op_Position (E : Entity_Id) return Uint is
566 TSS_Name : TSS_Name_Type;
568 begin
569 Get_Name_String (Chars (E));
570 TSS_Name :=
571 TSS_Name_Type
572 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
574 if Chars (E) = Name_uSize then
575 return Uint_1;
577 elsif TSS_Name = TSS_Stream_Read then
578 return Uint_2;
580 elsif TSS_Name = TSS_Stream_Write then
581 return Uint_3;
583 elsif TSS_Name = TSS_Stream_Input then
584 return Uint_4;
586 elsif TSS_Name = TSS_Stream_Output then
587 return Uint_5;
589 elsif Chars (E) = Name_Op_Eq then
590 return Uint_6;
592 elsif Chars (E) = Name_uAssign then
593 return Uint_7;
595 elsif TSS_Name = TSS_Deep_Adjust then
596 return Uint_8;
598 elsif TSS_Name = TSS_Deep_Finalize then
599 return Uint_9;
601 -- In VM targets unconditionally allow obtaining the position associated
602 -- with predefined interface primitives since in these platforms any
603 -- tagged type has these primitives.
605 elsif Ada_Version >= Ada_2005 or else not Tagged_Type_Expansion then
606 if Chars (E) = Name_uDisp_Asynchronous_Select then
607 return Uint_10;
609 elsif Chars (E) = Name_uDisp_Conditional_Select then
610 return Uint_11;
612 elsif Chars (E) = Name_uDisp_Get_Prim_Op_Kind then
613 return Uint_12;
615 elsif Chars (E) = Name_uDisp_Get_Task_Id then
616 return Uint_13;
618 elsif Chars (E) = Name_uDisp_Requeue then
619 return Uint_14;
621 elsif Chars (E) = Name_uDisp_Timed_Select then
622 return Uint_15;
623 end if;
624 end if;
626 raise Program_Error;
627 end Default_Prim_Op_Position;
629 -----------------------------
630 -- Expand_Dispatching_Call --
631 -----------------------------
633 procedure Expand_Dispatching_Call (Call_Node : Node_Id) is
634 Loc : constant Source_Ptr := Sloc (Call_Node);
635 Call_Typ : constant Entity_Id := Etype (Call_Node);
637 Ctrl_Arg : constant Node_Id := Controlling_Argument (Call_Node);
638 Ctrl_Typ : constant Entity_Id := Base_Type (Etype (Ctrl_Arg));
639 Param_List : constant List_Id := Parameter_Associations (Call_Node);
641 Subp : Entity_Id;
642 CW_Typ : Entity_Id;
643 New_Call : Node_Id;
644 New_Call_Name : Node_Id;
645 New_Params : List_Id := No_List;
646 Param : Node_Id;
647 Res_Typ : Entity_Id;
648 Subp_Ptr_Typ : Entity_Id;
649 Subp_Typ : Entity_Id;
650 Typ : Entity_Id;
651 Eq_Prim_Op : Entity_Id := Empty;
652 Controlling_Tag : Node_Id;
654 function New_Value (From : Node_Id) return Node_Id;
655 -- From is the original Expression. New_Value is equivalent to a call
656 -- to Duplicate_Subexpr with an explicit dereference when From is an
657 -- access parameter.
659 ---------------
660 -- New_Value --
661 ---------------
663 function New_Value (From : Node_Id) return Node_Id is
664 Res : constant Node_Id := Duplicate_Subexpr (From);
665 begin
666 if Is_Access_Type (Etype (From)) then
667 return
668 Make_Explicit_Dereference (Sloc (From),
669 Prefix => Res);
670 else
671 return Res;
672 end if;
673 end New_Value;
675 -- Local variables
677 New_Node : Node_Id;
678 SCIL_Node : Node_Id;
679 SCIL_Related_Node : Node_Id := Call_Node;
681 -- Start of processing for Expand_Dispatching_Call
683 begin
684 if No_Run_Time_Mode then
685 Error_Msg_CRT ("tagged types", Call_Node);
686 return;
687 end if;
689 -- Expand_Dispatching_Call is called directly from the semantics, so we
690 -- only proceed if the expander is active.
692 if not Expander_Active
694 -- And there is no need to expand the call if we are compiling under
695 -- restriction No_Dispatching_Calls; the semantic analyzer has
696 -- previously notified the violation of this restriction.
698 or else Restriction_Active (No_Dispatching_Calls)
700 -- No action needed if the dispatching call has been already expanded
702 or else Is_Expanded_Dispatching_Call (Name (Call_Node))
703 then
704 return;
705 end if;
707 -- Set subprogram. If this is an inherited operation that was
708 -- overridden, the body that is being called is its alias.
710 Subp := Entity (Name (Call_Node));
712 if Present (Alias (Subp))
713 and then Is_Inherited_Operation (Subp)
714 and then No (DTC_Entity (Subp))
715 then
716 Subp := Alias (Subp);
717 end if;
719 -- Definition of the class-wide type and the tagged type
721 -- If the controlling argument is itself a tag rather than a tagged
722 -- object, then use the class-wide type associated with the subprogram's
723 -- controlling type. This case can occur when a call to an inherited
724 -- primitive has an actual that originated from a default parameter
725 -- given by a tag-indeterminate call and when there is no other
726 -- controlling argument providing the tag (AI-239 requires dispatching).
727 -- This capability of dispatching directly by tag is also needed by the
728 -- implementation of AI-260 (for the generic dispatching constructors).
730 if Ctrl_Typ = RTE (RE_Tag)
731 or else (RTE_Available (RE_Interface_Tag)
732 and then Ctrl_Typ = RTE (RE_Interface_Tag))
733 then
734 CW_Typ := Class_Wide_Type (Find_Dispatching_Type (Subp));
736 -- Class_Wide_Type is applied to the expressions used to initialize
737 -- CW_Typ, to ensure that CW_Typ always denotes a class-wide type, since
738 -- there are cases where the controlling type is resolved to a specific
739 -- type (such as for designated types of arguments such as CW'Access).
741 elsif Is_Access_Type (Ctrl_Typ) then
742 CW_Typ := Class_Wide_Type (Designated_Type (Ctrl_Typ));
744 else
745 CW_Typ := Class_Wide_Type (Ctrl_Typ);
746 end if;
748 Typ := Find_Specific_Type (CW_Typ);
750 if not Is_Limited_Type (Typ) then
751 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
752 end if;
754 -- Dispatching call to C++ primitive. Create a new parameter list
755 -- with no tag checks.
757 New_Params := New_List;
759 if Is_CPP_Class (Typ) then
760 Param := First_Actual (Call_Node);
761 while Present (Param) loop
762 Append_To (New_Params, Relocate_Node (Param));
763 Next_Actual (Param);
764 end loop;
766 -- Dispatching call to Ada primitive
768 elsif Present (Param_List) then
769 Apply_Tag_Checks (Call_Node);
771 Param := First_Actual (Call_Node);
772 while Present (Param) loop
774 -- Cases in which we may have generated run-time checks. Note that
775 -- we strip any qualification from Param before comparing with the
776 -- already-stripped controlling argument.
778 if Unqualify (Param) = Ctrl_Arg or else Subp = Eq_Prim_Op then
779 Append_To (New_Params,
780 Duplicate_Subexpr_Move_Checks (Param));
782 elsif Nkind (Parent (Param)) /= N_Parameter_Association
783 or else not Is_Accessibility_Actual (Parent (Param))
784 then
785 Append_To (New_Params, Relocate_Node (Param));
786 end if;
788 Next_Actual (Param);
789 end loop;
790 end if;
792 -- Generate the appropriate subprogram pointer type
794 if Etype (Subp) = Typ then
795 Res_Typ := CW_Typ;
796 else
797 Res_Typ := Etype (Subp);
798 end if;
800 Subp_Typ := Create_Itype (E_Subprogram_Type, Call_Node);
801 Subp_Ptr_Typ := Create_Itype (E_Access_Subprogram_Type, Call_Node);
802 Set_Etype (Subp_Typ, Res_Typ);
803 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
804 Set_Convention (Subp_Typ, Convention (Subp));
806 -- Notify gigi that the designated type is a dispatching primitive
808 Set_Is_Dispatch_Table_Entity (Subp_Typ);
810 -- Create a new list of parameters which is a copy of the old formal
811 -- list including the creation of a new set of matching entities.
813 declare
814 Old_Formal : Entity_Id := First_Formal (Subp);
815 New_Formal : Entity_Id;
816 Extra : Entity_Id := Empty;
818 begin
819 if Present (Old_Formal) then
820 New_Formal := New_Copy (Old_Formal);
821 Set_First_Entity (Subp_Typ, New_Formal);
822 Param := First_Actual (Call_Node);
824 loop
825 Set_Scope (New_Formal, Subp_Typ);
827 -- Change all the controlling argument types to be class-wide
828 -- to avoid a recursion in dispatching.
830 if Is_Controlling_Formal (New_Formal) then
831 Set_Etype (New_Formal, Etype (Param));
832 end if;
834 -- If the type of the formal is an itype, there was code here
835 -- introduced in 1998 in revision 1.46, to create a new itype
836 -- by copy. This seems useless, and in fact leads to semantic
837 -- errors when the itype is the completion of a type derived
838 -- from a private type.
840 Extra := New_Formal;
841 Next_Formal (Old_Formal);
842 exit when No (Old_Formal);
844 Set_Next_Entity (New_Formal, New_Copy (Old_Formal));
845 Next_Entity (New_Formal);
846 Next_Actual (Param);
847 end loop;
849 Set_Next_Entity (New_Formal, Empty);
850 Set_Last_Entity (Subp_Typ, Extra);
851 end if;
853 -- Now that the explicit formals have been duplicated, any extra
854 -- formals needed by the subprogram must be created.
856 if Present (Extra) then
857 Set_Extra_Formal (Extra, Empty);
858 end if;
860 Create_Extra_Formals (Subp_Typ);
861 end;
863 -- Complete description of pointer type, including size information, as
864 -- must be done with itypes to prevent order-of-elaboration anomalies
865 -- in gigi.
867 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
868 Set_Directly_Designated_Type (Subp_Ptr_Typ, Subp_Typ);
869 Set_Convention (Subp_Ptr_Typ, Convention (Subp_Typ));
870 Layout_Type (Subp_Ptr_Typ);
872 -- If the controlling argument is a value of type Ada.Tag or an abstract
873 -- interface class-wide type then use it directly. Otherwise, the tag
874 -- must be extracted from the controlling object.
876 if Ctrl_Typ = RTE (RE_Tag)
877 or else (RTE_Available (RE_Interface_Tag)
878 and then Ctrl_Typ = RTE (RE_Interface_Tag))
879 then
880 Controlling_Tag := Duplicate_Subexpr (Ctrl_Arg);
882 -- Extract the tag from an unchecked type conversion. Done to avoid
883 -- the expansion of additional code just to obtain the value of such
884 -- tag because the current management of interface type conversions
885 -- generates in some cases this unchecked type conversion with the
886 -- tag of the object (see Expand_Interface_Conversion).
888 elsif Nkind (Ctrl_Arg) = N_Unchecked_Type_Conversion
889 and then
890 (Etype (Expression (Ctrl_Arg)) = RTE (RE_Tag)
891 or else
892 (RTE_Available (RE_Interface_Tag)
893 and then
894 Etype (Expression (Ctrl_Arg)) = RTE (RE_Interface_Tag)))
895 then
896 Controlling_Tag := Duplicate_Subexpr (Expression (Ctrl_Arg));
898 -- Ada 2005 (AI-251): Abstract interface class-wide type
900 elsif Is_Interface (Ctrl_Typ)
901 and then Is_Class_Wide_Type (Ctrl_Typ)
902 then
903 Controlling_Tag := Duplicate_Subexpr (Ctrl_Arg);
905 else
906 Controlling_Tag :=
907 Make_Selected_Component (Loc,
908 Prefix => Duplicate_Subexpr_Move_Checks (Ctrl_Arg),
909 Selector_Name => New_Occurrence_Of (DTC_Entity (Subp), Loc));
910 end if;
912 -- Handle dispatching calls to predefined primitives
914 if Is_Predefined_Dispatching_Operation (Subp)
915 or else Is_Predefined_Dispatching_Alias (Subp)
916 then
917 Build_Get_Predefined_Prim_Op_Address (Loc,
918 Tag_Node => Controlling_Tag,
919 Position => DT_Position (Subp),
920 New_Node => New_Node);
922 -- Handle dispatching calls to user-defined primitives
924 else
925 Build_Get_Prim_Op_Address (Loc,
926 Typ => Underlying_Type (Find_Dispatching_Type (Subp)),
927 Tag_Node => Controlling_Tag,
928 Position => DT_Position (Subp),
929 New_Node => New_Node);
930 end if;
932 New_Call_Name :=
933 Unchecked_Convert_To (Subp_Ptr_Typ, New_Node);
935 -- Generate the SCIL node for this dispatching call. Done now because
936 -- attribute SCIL_Controlling_Tag must be set after the new call name
937 -- is built to reference the nodes that will see the SCIL backend
938 -- (because Build_Get_Prim_Op_Address generates an unchecked type
939 -- conversion which relocates the controlling tag node).
941 if Generate_SCIL then
942 SCIL_Node := Make_SCIL_Dispatching_Call (Sloc (Call_Node));
943 Set_SCIL_Entity (SCIL_Node, Typ);
944 Set_SCIL_Target_Prim (SCIL_Node, Subp);
946 -- Common case: the controlling tag is the tag of an object
947 -- (for example, obj.tag)
949 if Nkind (Controlling_Tag) = N_Selected_Component then
950 Set_SCIL_Controlling_Tag (SCIL_Node, Controlling_Tag);
952 -- Handle renaming of selected component
954 elsif Nkind (Controlling_Tag) = N_Identifier
955 and then Nkind (Parent (Entity (Controlling_Tag))) =
956 N_Object_Renaming_Declaration
957 and then Nkind (Name (Parent (Entity (Controlling_Tag)))) =
958 N_Selected_Component
959 then
960 Set_SCIL_Controlling_Tag (SCIL_Node,
961 Name (Parent (Entity (Controlling_Tag))));
963 -- If the controlling tag is an identifier, the SCIL node references
964 -- the corresponding object or parameter declaration
966 elsif Nkind (Controlling_Tag) = N_Identifier
967 and then Nkind_In (Parent (Entity (Controlling_Tag)),
968 N_Object_Declaration,
969 N_Parameter_Specification)
970 then
971 Set_SCIL_Controlling_Tag (SCIL_Node,
972 Parent (Entity (Controlling_Tag)));
974 -- If the controlling tag is a dereference, the SCIL node references
975 -- the corresponding object or parameter declaration
977 elsif Nkind (Controlling_Tag) = N_Explicit_Dereference
978 and then Nkind (Prefix (Controlling_Tag)) = N_Identifier
979 and then Nkind_In (Parent (Entity (Prefix (Controlling_Tag))),
980 N_Object_Declaration,
981 N_Parameter_Specification)
982 then
983 Set_SCIL_Controlling_Tag (SCIL_Node,
984 Parent (Entity (Prefix (Controlling_Tag))));
986 -- For a direct reference of the tag of the type the SCIL node
987 -- references the internal object declaration containing the tag
988 -- of the type.
990 elsif Nkind (Controlling_Tag) = N_Attribute_Reference
991 and then Attribute_Name (Controlling_Tag) = Name_Tag
992 then
993 Set_SCIL_Controlling_Tag (SCIL_Node,
994 Parent
995 (Node
996 (First_Elmt
997 (Access_Disp_Table (Entity (Prefix (Controlling_Tag)))))));
999 -- Interfaces are not supported. For now we leave the SCIL node
1000 -- decorated with the Controlling_Tag. More work needed here???
1002 elsif Is_Interface (Etype (Controlling_Tag)) then
1003 Set_SCIL_Controlling_Tag (SCIL_Node, Controlling_Tag);
1005 else
1006 pragma Assert (False);
1007 null;
1008 end if;
1009 end if;
1011 if Nkind (Call_Node) = N_Function_Call then
1012 New_Call :=
1013 Make_Function_Call (Loc,
1014 Name => New_Call_Name,
1015 Parameter_Associations => New_Params);
1017 -- If this is a dispatching "=", we must first compare the tags so
1018 -- we generate: x.tag = y.tag and then x = y
1020 if Subp = Eq_Prim_Op then
1021 Param := First_Actual (Call_Node);
1022 New_Call :=
1023 Make_And_Then (Loc,
1024 Left_Opnd =>
1025 Make_Op_Eq (Loc,
1026 Left_Opnd =>
1027 Make_Selected_Component (Loc,
1028 Prefix => New_Value (Param),
1029 Selector_Name =>
1030 New_Occurrence_Of (First_Tag_Component (Typ),
1031 Loc)),
1033 Right_Opnd =>
1034 Make_Selected_Component (Loc,
1035 Prefix =>
1036 Unchecked_Convert_To (Typ,
1037 New_Value (Next_Actual (Param))),
1038 Selector_Name =>
1039 New_Occurrence_Of
1040 (First_Tag_Component (Typ), Loc))),
1041 Right_Opnd => New_Call);
1043 SCIL_Related_Node := Right_Opnd (New_Call);
1044 end if;
1046 else
1047 New_Call :=
1048 Make_Procedure_Call_Statement (Loc,
1049 Name => New_Call_Name,
1050 Parameter_Associations => New_Params);
1051 end if;
1053 -- Register the dispatching call in the call graph nodes table
1055 Register_CG_Node (Call_Node);
1057 Rewrite (Call_Node, New_Call);
1059 -- Associate the SCIL node of this dispatching call
1061 if Generate_SCIL then
1062 Set_SCIL_Node (SCIL_Related_Node, SCIL_Node);
1063 end if;
1065 -- Suppress all checks during the analysis of the expanded code to avoid
1066 -- the generation of spurious warnings under ZFP run-time.
1068 Analyze_And_Resolve (Call_Node, Call_Typ, Suppress => All_Checks);
1069 end Expand_Dispatching_Call;
1071 ---------------------------------
1072 -- Expand_Interface_Conversion --
1073 ---------------------------------
1075 procedure Expand_Interface_Conversion (N : Node_Id) is
1076 function Underlying_Record_Type (Typ : Entity_Id) return Entity_Id;
1077 -- Return the underlying record type of Typ.
1079 ----------------------------
1080 -- Underlying_Record_Type --
1081 ----------------------------
1083 function Underlying_Record_Type (Typ : Entity_Id) return Entity_Id is
1084 E : Entity_Id := Typ;
1086 begin
1087 -- Handle access to class-wide interface types
1089 if Is_Access_Type (E) then
1090 E := Etype (Directly_Designated_Type (E));
1091 end if;
1093 -- Handle class-wide types. This conversion can appear explicitly in
1094 -- the source code. Example: I'Class (Obj)
1096 if Is_Class_Wide_Type (E) then
1097 E := Root_Type (E);
1098 end if;
1100 -- If the target type is a tagged synchronized type, the dispatch
1101 -- table info is in the corresponding record type.
1103 if Is_Concurrent_Type (E) then
1104 E := Corresponding_Record_Type (E);
1105 end if;
1107 -- Handle private types
1109 E := Underlying_Type (E);
1111 -- Handle subtypes
1113 return Base_Type (E);
1114 end Underlying_Record_Type;
1116 -- Local variables
1118 Loc : constant Source_Ptr := Sloc (N);
1119 Etyp : constant Entity_Id := Etype (N);
1120 Operand : constant Node_Id := Expression (N);
1121 Operand_Typ : Entity_Id := Etype (Operand);
1122 Func : Node_Id;
1123 Iface_Typ : constant Entity_Id := Underlying_Record_Type (Etype (N));
1124 Iface_Tag : Entity_Id;
1125 Is_Static : Boolean;
1127 -- Start of processing for Expand_Interface_Conversion
1129 begin
1130 -- Freeze the entity associated with the target interface to have
1131 -- available the attribute Access_Disp_Table.
1133 Freeze_Before (N, Iface_Typ);
1135 -- Ada 2005 (AI-345): Handle synchronized interface type derivations
1137 if Is_Concurrent_Type (Operand_Typ) then
1138 Operand_Typ := Base_Type (Corresponding_Record_Type (Operand_Typ));
1139 end if;
1141 -- Evaluate if we can statically displace the pointer to the object
1143 declare
1144 Opnd_Typ : constant Node_Id := Underlying_Record_Type (Operand_Typ);
1146 begin
1147 Is_Static :=
1148 not Is_Interface (Opnd_Typ)
1149 and then Interface_Present_In_Ancestor
1150 (Typ => Opnd_Typ,
1151 Iface => Iface_Typ)
1152 and then (Etype (Opnd_Typ) = Opnd_Typ
1153 or else not
1154 Is_Variable_Size_Record (Etype (Opnd_Typ)));
1155 end;
1157 if not Tagged_Type_Expansion then
1158 if VM_Target /= No_VM then
1159 if Is_Access_Type (Operand_Typ) then
1160 Operand_Typ := Designated_Type (Operand_Typ);
1161 end if;
1163 if Is_Class_Wide_Type (Operand_Typ) then
1164 Operand_Typ := Root_Type (Operand_Typ);
1165 end if;
1167 if not Is_Static and then Operand_Typ /= Iface_Typ then
1168 Insert_Action (N,
1169 Make_Procedure_Call_Statement (Loc,
1170 Name => New_Occurrence_Of
1171 (RTE (RE_Check_Interface_Conversion), Loc),
1172 Parameter_Associations => New_List (
1173 Make_Attribute_Reference (Loc,
1174 Prefix => Duplicate_Subexpr (Expression (N)),
1175 Attribute_Name => Name_Tag),
1176 Make_Attribute_Reference (Loc,
1177 Prefix => New_Occurrence_Of (Iface_Typ, Loc),
1178 Attribute_Name => Name_Tag))));
1179 end if;
1181 -- Just do a conversion ???
1183 Rewrite (N, Unchecked_Convert_To (Etype (N), N));
1184 Analyze (N);
1185 end if;
1187 return;
1189 -- A static conversion to an interface type that is not classwide is
1190 -- curious but legal if the interface operation is a null procedure.
1191 -- If the operation is abstract it will be rejected later.
1193 elsif Is_Static
1194 and then Is_Interface (Etype (N))
1195 and then not Is_Class_Wide_Type (Etype (N))
1196 and then Comes_From_Source (N)
1197 then
1198 Rewrite (N, Unchecked_Convert_To (Etype (N), N));
1199 Analyze (N);
1200 return;
1201 end if;
1203 if not Is_Static then
1205 -- Give error if configurable run time and Displace not available
1207 if not RTE_Available (RE_Displace) then
1208 Error_Msg_CRT ("dynamic interface conversion", N);
1209 return;
1210 end if;
1212 -- Handle conversion of access-to-class-wide interface types. Target
1213 -- can be an access to an object or an access to another class-wide
1214 -- interface (see -1- and -2- in the following example):
1216 -- type Iface1_Ref is access all Iface1'Class;
1217 -- type Iface2_Ref is access all Iface1'Class;
1219 -- Acc1 : Iface1_Ref := new ...
1220 -- Obj : Obj_Ref := Obj_Ref (Acc); -- 1
1221 -- Acc2 : Iface2_Ref := Iface2_Ref (Acc); -- 2
1223 if Is_Access_Type (Operand_Typ) then
1224 Rewrite (N,
1225 Unchecked_Convert_To (Etype (N),
1226 Make_Function_Call (Loc,
1227 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
1228 Parameter_Associations => New_List (
1230 Unchecked_Convert_To (RTE (RE_Address),
1231 Relocate_Node (Expression (N))),
1233 New_Occurrence_Of
1234 (Node (First_Elmt (Access_Disp_Table (Iface_Typ))),
1235 Loc)))));
1237 Analyze (N);
1238 return;
1239 end if;
1241 Rewrite (N,
1242 Make_Function_Call (Loc,
1243 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
1244 Parameter_Associations => New_List (
1245 Make_Attribute_Reference (Loc,
1246 Prefix => Relocate_Node (Expression (N)),
1247 Attribute_Name => Name_Address),
1249 New_Occurrence_Of
1250 (Node (First_Elmt (Access_Disp_Table (Iface_Typ))),
1251 Loc))));
1253 Analyze (N);
1255 -- If target is a class-wide interface, change the type of the data
1256 -- returned by IW_Convert to indicate this is a dispatching call.
1258 declare
1259 New_Itype : Entity_Id;
1261 begin
1262 New_Itype := Create_Itype (E_Anonymous_Access_Type, N);
1263 Set_Etype (New_Itype, New_Itype);
1264 Set_Directly_Designated_Type (New_Itype, Etyp);
1266 Rewrite (N,
1267 Make_Explicit_Dereference (Loc,
1268 Prefix =>
1269 Unchecked_Convert_To (New_Itype, Relocate_Node (N))));
1270 Analyze (N);
1271 Freeze_Itype (New_Itype, N);
1273 return;
1274 end;
1275 end if;
1277 Iface_Tag := Find_Interface_Tag (Operand_Typ, Iface_Typ);
1278 pragma Assert (Iface_Tag /= Empty);
1280 -- Keep separate access types to interfaces because one internal
1281 -- function is used to handle the null value (see following comments)
1283 if not Is_Access_Type (Etype (N)) then
1285 -- Statically displace the pointer to the object to reference the
1286 -- component containing the secondary dispatch table.
1288 Rewrite (N,
1289 Convert_Tag_To_Interface (Class_Wide_Type (Iface_Typ),
1290 Make_Selected_Component (Loc,
1291 Prefix => Relocate_Node (Expression (N)),
1292 Selector_Name => New_Occurrence_Of (Iface_Tag, Loc))));
1294 else
1295 -- Build internal function to handle the case in which the actual is
1296 -- null. If the actual is null returns null because no displacement
1297 -- is required; otherwise performs a type conversion that will be
1298 -- expanded in the code that returns the value of the displaced
1299 -- actual. That is:
1301 -- function Func (O : Address) return Iface_Typ is
1302 -- type Op_Typ is access all Operand_Typ;
1303 -- Aux : Op_Typ := To_Op_Typ (O);
1304 -- begin
1305 -- if O = Null_Address then
1306 -- return null;
1307 -- else
1308 -- return Iface_Typ!(Aux.Iface_Tag'Address);
1309 -- end if;
1310 -- end Func;
1312 declare
1313 Desig_Typ : Entity_Id;
1314 Fent : Entity_Id;
1315 New_Typ_Decl : Node_Id;
1316 Stats : List_Id;
1318 begin
1319 Desig_Typ := Etype (Expression (N));
1321 if Is_Access_Type (Desig_Typ) then
1322 Desig_Typ :=
1323 Available_View (Directly_Designated_Type (Desig_Typ));
1324 end if;
1326 if Is_Concurrent_Type (Desig_Typ) then
1327 Desig_Typ := Base_Type (Corresponding_Record_Type (Desig_Typ));
1328 end if;
1330 New_Typ_Decl :=
1331 Make_Full_Type_Declaration (Loc,
1332 Defining_Identifier => Make_Temporary (Loc, 'T'),
1333 Type_Definition =>
1334 Make_Access_To_Object_Definition (Loc,
1335 All_Present => True,
1336 Null_Exclusion_Present => False,
1337 Constant_Present => False,
1338 Subtype_Indication =>
1339 New_Occurrence_Of (Desig_Typ, Loc)));
1341 Stats := New_List (
1342 Make_Simple_Return_Statement (Loc,
1343 Unchecked_Convert_To (Etype (N),
1344 Make_Attribute_Reference (Loc,
1345 Prefix =>
1346 Make_Selected_Component (Loc,
1347 Prefix =>
1348 Unchecked_Convert_To
1349 (Defining_Identifier (New_Typ_Decl),
1350 Make_Identifier (Loc, Name_uO)),
1351 Selector_Name =>
1352 New_Occurrence_Of (Iface_Tag, Loc)),
1353 Attribute_Name => Name_Address))));
1355 -- If the type is null-excluding, no need for the null branch.
1356 -- Otherwise we need to check for it and return null.
1358 if not Can_Never_Be_Null (Etype (N)) then
1359 Stats := New_List (
1360 Make_If_Statement (Loc,
1361 Condition =>
1362 Make_Op_Eq (Loc,
1363 Left_Opnd => Make_Identifier (Loc, Name_uO),
1364 Right_Opnd => New_Occurrence_Of
1365 (RTE (RE_Null_Address), Loc)),
1367 Then_Statements => New_List (
1368 Make_Simple_Return_Statement (Loc, Make_Null (Loc))),
1369 Else_Statements => Stats));
1370 end if;
1372 Fent := Make_Temporary (Loc, 'F');
1373 Func :=
1374 Make_Subprogram_Body (Loc,
1375 Specification =>
1376 Make_Function_Specification (Loc,
1377 Defining_Unit_Name => Fent,
1379 Parameter_Specifications => New_List (
1380 Make_Parameter_Specification (Loc,
1381 Defining_Identifier =>
1382 Make_Defining_Identifier (Loc, Name_uO),
1383 Parameter_Type =>
1384 New_Occurrence_Of (RTE (RE_Address), Loc))),
1386 Result_Definition =>
1387 New_Occurrence_Of (Etype (N), Loc)),
1389 Declarations => New_List (New_Typ_Decl),
1391 Handled_Statement_Sequence =>
1392 Make_Handled_Sequence_Of_Statements (Loc, Stats));
1394 -- Place function body before the expression containing the
1395 -- conversion. We suppress all checks because the body of the
1396 -- internally generated function already takes care of the case
1397 -- in which the actual is null; therefore there is no need to
1398 -- double check that the pointer is not null when the program
1399 -- executes the alternative that performs the type conversion).
1401 Insert_Action (N, Func, Suppress => All_Checks);
1403 if Is_Access_Type (Etype (Expression (N))) then
1405 -- Generate: Func (Address!(Expression))
1407 Rewrite (N,
1408 Make_Function_Call (Loc,
1409 Name => New_Occurrence_Of (Fent, Loc),
1410 Parameter_Associations => New_List (
1411 Unchecked_Convert_To (RTE (RE_Address),
1412 Relocate_Node (Expression (N))))));
1414 else
1415 -- Generate: Func (Operand_Typ!(Expression)'Address)
1417 Rewrite (N,
1418 Make_Function_Call (Loc,
1419 Name => New_Occurrence_Of (Fent, Loc),
1420 Parameter_Associations => New_List (
1421 Make_Attribute_Reference (Loc,
1422 Prefix => Unchecked_Convert_To (Operand_Typ,
1423 Relocate_Node (Expression (N))),
1424 Attribute_Name => Name_Address))));
1425 end if;
1426 end;
1427 end if;
1429 Analyze (N);
1430 end Expand_Interface_Conversion;
1432 ------------------------------
1433 -- Expand_Interface_Actuals --
1434 ------------------------------
1436 procedure Expand_Interface_Actuals (Call_Node : Node_Id) is
1437 Actual : Node_Id;
1438 Actual_Dup : Node_Id;
1439 Actual_Typ : Entity_Id;
1440 Anon : Entity_Id;
1441 Conversion : Node_Id;
1442 Formal : Entity_Id;
1443 Formal_Typ : Entity_Id;
1444 Subp : Entity_Id;
1445 Formal_DDT : Entity_Id;
1446 Actual_DDT : Entity_Id;
1448 begin
1449 -- This subprogram is called directly from the semantics, so we need a
1450 -- check to see whether expansion is active before proceeding.
1452 if not Expander_Active then
1453 return;
1454 end if;
1456 -- Call using access to subprogram with explicit dereference
1458 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
1459 Subp := Etype (Name (Call_Node));
1461 -- Call using selected component
1463 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
1464 Subp := Entity (Selector_Name (Name (Call_Node)));
1466 -- Call using direct name
1468 else
1469 Subp := Entity (Name (Call_Node));
1470 end if;
1472 -- Ada 2005 (AI-251): Look for interface type formals to force "this"
1473 -- displacement
1475 Formal := First_Formal (Subp);
1476 Actual := First_Actual (Call_Node);
1477 while Present (Formal) loop
1478 Formal_Typ := Etype (Formal);
1480 if Ekind (Formal_Typ) = E_Record_Type_With_Private then
1481 Formal_Typ := Full_View (Formal_Typ);
1482 end if;
1484 if Is_Access_Type (Formal_Typ) then
1485 Formal_DDT := Directly_Designated_Type (Formal_Typ);
1486 end if;
1488 Actual_Typ := Etype (Actual);
1490 if Is_Access_Type (Actual_Typ) then
1491 Actual_DDT := Directly_Designated_Type (Actual_Typ);
1492 end if;
1494 if Is_Interface (Formal_Typ)
1495 and then Is_Class_Wide_Type (Formal_Typ)
1496 then
1497 -- No need to displace the pointer if the type of the actual
1498 -- coincides with the type of the formal.
1500 if Actual_Typ = Formal_Typ then
1501 null;
1503 -- No need to displace the pointer if the interface type is a
1504 -- parent of the type of the actual because in this case the
1505 -- interface primitives are located in the primary dispatch table.
1507 elsif Is_Ancestor (Formal_Typ, Actual_Typ,
1508 Use_Full_View => True)
1509 then
1510 null;
1512 -- Implicit conversion to the class-wide formal type to force the
1513 -- displacement of the pointer.
1515 else
1516 -- Normally, expansion of actuals for calls to build-in-place
1517 -- functions happens as part of Expand_Actuals, but in this
1518 -- case the call will be wrapped in a conversion and soon after
1519 -- expanded further to handle the displacement for a class-wide
1520 -- interface conversion, so if this is a BIP call then we need
1521 -- to handle it now.
1523 if Ada_Version >= Ada_2005
1524 and then Is_Build_In_Place_Function_Call (Actual)
1525 then
1526 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1527 end if;
1529 Conversion := Convert_To (Formal_Typ, Relocate_Node (Actual));
1530 Rewrite (Actual, Conversion);
1531 Analyze_And_Resolve (Actual, Formal_Typ);
1532 end if;
1534 -- Access to class-wide interface type
1536 elsif Is_Access_Type (Formal_Typ)
1537 and then Is_Interface (Formal_DDT)
1538 and then Is_Class_Wide_Type (Formal_DDT)
1539 and then Interface_Present_In_Ancestor
1540 (Typ => Actual_DDT,
1541 Iface => Etype (Formal_DDT))
1542 then
1543 -- Handle attributes 'Access and 'Unchecked_Access
1545 if Nkind (Actual) = N_Attribute_Reference
1546 and then
1547 (Attribute_Name (Actual) = Name_Access
1548 or else Attribute_Name (Actual) = Name_Unchecked_Access)
1549 then
1550 -- This case must have been handled by the analysis and
1551 -- expansion of 'Access. The only exception is when types
1552 -- match and no further expansion is required.
1554 pragma Assert (Base_Type (Etype (Prefix (Actual)))
1555 = Base_Type (Formal_DDT));
1556 null;
1558 -- No need to displace the pointer if the type of the actual
1559 -- coincides with the type of the formal.
1561 elsif Actual_DDT = Formal_DDT then
1562 null;
1564 -- No need to displace the pointer if the interface type is
1565 -- a parent of the type of the actual because in this case the
1566 -- interface primitives are located in the primary dispatch table.
1568 elsif Is_Ancestor (Formal_DDT, Actual_DDT,
1569 Use_Full_View => True)
1570 then
1571 null;
1573 else
1574 Actual_Dup := Relocate_Node (Actual);
1576 if From_Limited_With (Actual_Typ) then
1578 -- If the type of the actual parameter comes from a
1579 -- limited with-clause and the non-limited view is already
1580 -- available, we replace the anonymous access type by
1581 -- a duplicate declaration whose designated type is the
1582 -- non-limited view.
1584 if Ekind (Actual_DDT) = E_Incomplete_Type
1585 and then Present (Non_Limited_View (Actual_DDT))
1586 then
1587 Anon := New_Copy (Actual_Typ);
1589 if Is_Itype (Anon) then
1590 Set_Scope (Anon, Current_Scope);
1591 end if;
1593 Set_Directly_Designated_Type (Anon,
1594 Non_Limited_View (Actual_DDT));
1595 Set_Etype (Actual_Dup, Anon);
1597 elsif Is_Class_Wide_Type (Actual_DDT)
1598 and then Ekind (Etype (Actual_DDT)) = E_Incomplete_Type
1599 and then Present (Non_Limited_View (Etype (Actual_DDT)))
1600 then
1601 Anon := New_Copy (Actual_Typ);
1603 if Is_Itype (Anon) then
1604 Set_Scope (Anon, Current_Scope);
1605 end if;
1607 Set_Directly_Designated_Type (Anon,
1608 New_Copy (Actual_DDT));
1609 Set_Class_Wide_Type (Directly_Designated_Type (Anon),
1610 New_Copy (Class_Wide_Type (Actual_DDT)));
1611 Set_Etype (Directly_Designated_Type (Anon),
1612 Non_Limited_View (Etype (Actual_DDT)));
1613 Set_Etype (
1614 Class_Wide_Type (Directly_Designated_Type (Anon)),
1615 Non_Limited_View (Etype (Actual_DDT)));
1616 Set_Etype (Actual_Dup, Anon);
1617 end if;
1618 end if;
1620 Conversion := Convert_To (Formal_Typ, Actual_Dup);
1621 Rewrite (Actual, Conversion);
1622 Analyze_And_Resolve (Actual, Formal_Typ);
1623 end if;
1624 end if;
1626 Next_Actual (Actual);
1627 Next_Formal (Formal);
1628 end loop;
1629 end Expand_Interface_Actuals;
1631 ----------------------------
1632 -- Expand_Interface_Thunk --
1633 ----------------------------
1635 procedure Expand_Interface_Thunk
1636 (Prim : Node_Id;
1637 Thunk_Id : out Entity_Id;
1638 Thunk_Code : out Node_Id)
1640 Loc : constant Source_Ptr := Sloc (Prim);
1641 Actuals : constant List_Id := New_List;
1642 Decl : constant List_Id := New_List;
1643 Formals : constant List_Id := New_List;
1644 Target : constant Entity_Id := Ultimate_Alias (Prim);
1646 Decl_1 : Node_Id;
1647 Decl_2 : Node_Id;
1648 Expr : Node_Id;
1649 Formal : Node_Id;
1650 Ftyp : Entity_Id;
1651 Iface_Formal : Node_Id;
1652 New_Arg : Node_Id;
1653 Offset_To_Top : Node_Id;
1654 Target_Formal : Entity_Id;
1656 begin
1657 Thunk_Id := Empty;
1658 Thunk_Code := Empty;
1660 -- No thunk needed if the primitive has been eliminated
1662 if Is_Eliminated (Ultimate_Alias (Prim)) then
1663 return;
1665 -- In case of primitives that are functions without formals and a
1666 -- controlling result there is no need to build the thunk.
1668 elsif not Present (First_Formal (Target)) then
1669 pragma Assert (Ekind (Target) = E_Function
1670 and then Has_Controlling_Result (Target));
1671 return;
1672 end if;
1674 -- Duplicate the formals of the Target primitive. In the thunk, the type
1675 -- of the controlling formal is the covered interface type (instead of
1676 -- the target tagged type). Done to avoid problems with discriminated
1677 -- tagged types because, if the controlling type has discriminants with
1678 -- default values, then the type conversions done inside the body of
1679 -- the thunk (after the displacement of the pointer to the base of the
1680 -- actual object) generate code that modify its contents.
1682 -- Note: This special management is not done for predefined primitives
1683 -- because???
1685 if not Is_Predefined_Dispatching_Operation (Prim) then
1686 Iface_Formal := First_Formal (Interface_Alias (Prim));
1687 end if;
1689 Formal := First_Formal (Target);
1690 while Present (Formal) loop
1691 Ftyp := Etype (Formal);
1693 -- Use the interface type as the type of the controlling formal (see
1694 -- comment above).
1696 if not Is_Controlling_Formal (Formal)
1697 or else Is_Predefined_Dispatching_Operation (Prim)
1698 then
1699 Ftyp := Etype (Formal);
1700 Expr := New_Copy_Tree (Expression (Parent (Formal)));
1701 else
1702 Ftyp := Etype (Iface_Formal);
1703 Expr := Empty;
1704 end if;
1706 Append_To (Formals,
1707 Make_Parameter_Specification (Loc,
1708 Defining_Identifier =>
1709 Make_Defining_Identifier (Sloc (Formal),
1710 Chars => Chars (Formal)),
1711 In_Present => In_Present (Parent (Formal)),
1712 Out_Present => Out_Present (Parent (Formal)),
1713 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
1714 Expression => Expr));
1716 if not Is_Predefined_Dispatching_Operation (Prim) then
1717 Next_Formal (Iface_Formal);
1718 end if;
1720 Next_Formal (Formal);
1721 end loop;
1723 Target_Formal := First_Formal (Target);
1724 Formal := First (Formals);
1725 while Present (Formal) loop
1727 -- If the parent is a constrained discriminated type, then the
1728 -- primitive operation will have been defined on a first subtype.
1729 -- For proper matching with controlling type, use base type.
1731 if Ekind (Target_Formal) = E_In_Parameter
1732 and then Ekind (Etype (Target_Formal)) = E_Anonymous_Access_Type
1733 then
1734 Ftyp :=
1735 Base_Type (Directly_Designated_Type (Etype (Target_Formal)));
1736 else
1737 Ftyp := Base_Type (Etype (Target_Formal));
1738 end if;
1740 -- For concurrent types, the relevant information is found in the
1741 -- Corresponding_Record_Type, rather than the type entity itself.
1743 if Is_Concurrent_Type (Ftyp) then
1744 Ftyp := Corresponding_Record_Type (Ftyp);
1745 end if;
1747 if Ekind (Target_Formal) = E_In_Parameter
1748 and then Ekind (Etype (Target_Formal)) = E_Anonymous_Access_Type
1749 and then Is_Controlling_Formal (Target_Formal)
1750 then
1751 -- Generate:
1752 -- type T is access all <<type of the target formal>>
1753 -- S : Storage_Offset := Storage_Offset!(Formal)
1754 -- - Offset_To_Top (address!(Formal))
1756 Decl_2 :=
1757 Make_Full_Type_Declaration (Loc,
1758 Defining_Identifier => Make_Temporary (Loc, 'T'),
1759 Type_Definition =>
1760 Make_Access_To_Object_Definition (Loc,
1761 All_Present => True,
1762 Null_Exclusion_Present => False,
1763 Constant_Present => False,
1764 Subtype_Indication =>
1765 New_Occurrence_Of (Ftyp, Loc)));
1767 New_Arg :=
1768 Unchecked_Convert_To (RTE (RE_Address),
1769 New_Occurrence_Of (Defining_Identifier (Formal), Loc));
1771 if not RTE_Available (RE_Offset_To_Top) then
1772 Offset_To_Top :=
1773 Build_Offset_To_Top (Loc, New_Arg);
1774 else
1775 Offset_To_Top :=
1776 Make_Function_Call (Loc,
1777 Name => New_Occurrence_Of (RTE (RE_Offset_To_Top), Loc),
1778 Parameter_Associations => New_List (New_Arg));
1779 end if;
1781 Decl_1 :=
1782 Make_Object_Declaration (Loc,
1783 Defining_Identifier => Make_Temporary (Loc, 'S'),
1784 Constant_Present => True,
1785 Object_Definition =>
1786 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
1787 Expression =>
1788 Make_Op_Subtract (Loc,
1789 Left_Opnd =>
1790 Unchecked_Convert_To
1791 (RTE (RE_Storage_Offset),
1792 New_Occurrence_Of
1793 (Defining_Identifier (Formal), Loc)),
1794 Right_Opnd =>
1795 Offset_To_Top));
1797 Append_To (Decl, Decl_2);
1798 Append_To (Decl, Decl_1);
1800 -- Reference the new actual. Generate:
1801 -- T!(S)
1803 Append_To (Actuals,
1804 Unchecked_Convert_To
1805 (Defining_Identifier (Decl_2),
1806 New_Occurrence_Of (Defining_Identifier (Decl_1), Loc)));
1808 elsif Is_Controlling_Formal (Target_Formal) then
1810 -- Generate:
1811 -- S1 : Storage_Offset := Storage_Offset!(Formal'Address)
1812 -- - Offset_To_Top (Formal'Address)
1813 -- S2 : Addr_Ptr := Addr_Ptr!(S1)
1815 New_Arg :=
1816 Make_Attribute_Reference (Loc,
1817 Prefix =>
1818 New_Occurrence_Of (Defining_Identifier (Formal), Loc),
1819 Attribute_Name =>
1820 Name_Address);
1822 if not RTE_Available (RE_Offset_To_Top) then
1823 Offset_To_Top :=
1824 Build_Offset_To_Top (Loc, New_Arg);
1825 else
1826 Offset_To_Top :=
1827 Make_Function_Call (Loc,
1828 Name => New_Occurrence_Of (RTE (RE_Offset_To_Top), Loc),
1829 Parameter_Associations => New_List (New_Arg));
1830 end if;
1832 Decl_1 :=
1833 Make_Object_Declaration (Loc,
1834 Defining_Identifier => Make_Temporary (Loc, 'S'),
1835 Constant_Present => True,
1836 Object_Definition =>
1837 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
1838 Expression =>
1839 Make_Op_Subtract (Loc,
1840 Left_Opnd =>
1841 Unchecked_Convert_To
1842 (RTE (RE_Storage_Offset),
1843 Make_Attribute_Reference (Loc,
1844 Prefix =>
1845 New_Occurrence_Of
1846 (Defining_Identifier (Formal), Loc),
1847 Attribute_Name => Name_Address)),
1848 Right_Opnd =>
1849 Offset_To_Top));
1851 Decl_2 :=
1852 Make_Object_Declaration (Loc,
1853 Defining_Identifier => Make_Temporary (Loc, 'S'),
1854 Constant_Present => True,
1855 Object_Definition =>
1856 New_Occurrence_Of (RTE (RE_Addr_Ptr), Loc),
1857 Expression =>
1858 Unchecked_Convert_To
1859 (RTE (RE_Addr_Ptr),
1860 New_Occurrence_Of (Defining_Identifier (Decl_1), Loc)));
1862 Append_To (Decl, Decl_1);
1863 Append_To (Decl, Decl_2);
1865 -- Reference the new actual, generate:
1866 -- Target_Formal (S2.all)
1868 Append_To (Actuals,
1869 Unchecked_Convert_To (Ftyp,
1870 Make_Explicit_Dereference (Loc,
1871 New_Occurrence_Of (Defining_Identifier (Decl_2), Loc))));
1873 -- Ensure proper matching of access types. Required to avoid
1874 -- reporting spurious errors.
1876 elsif Is_Access_Type (Etype (Target_Formal)) then
1877 Append_To (Actuals,
1878 Unchecked_Convert_To (Base_Type (Etype (Target_Formal)),
1879 New_Occurrence_Of (Defining_Identifier (Formal), Loc)));
1881 -- No special management required for this actual
1883 else
1884 Append_To (Actuals,
1885 New_Occurrence_Of (Defining_Identifier (Formal), Loc));
1886 end if;
1888 Next_Formal (Target_Formal);
1889 Next (Formal);
1890 end loop;
1892 Thunk_Id := Make_Temporary (Loc, 'T');
1893 Set_Ekind (Thunk_Id, Ekind (Prim));
1894 Set_Is_Thunk (Thunk_Id);
1895 Set_Convention (Thunk_Id, Convention (Prim));
1896 Set_Thunk_Entity (Thunk_Id, Target);
1898 -- Procedure case
1900 if Ekind (Target) = E_Procedure then
1901 Thunk_Code :=
1902 Make_Subprogram_Body (Loc,
1903 Specification =>
1904 Make_Procedure_Specification (Loc,
1905 Defining_Unit_Name => Thunk_Id,
1906 Parameter_Specifications => Formals),
1907 Declarations => Decl,
1908 Handled_Statement_Sequence =>
1909 Make_Handled_Sequence_Of_Statements (Loc,
1910 Statements => New_List (
1911 Make_Procedure_Call_Statement (Loc,
1912 Name => New_Occurrence_Of (Target, Loc),
1913 Parameter_Associations => Actuals))));
1915 -- Function case
1917 else pragma Assert (Ekind (Target) = E_Function);
1918 declare
1919 Result_Def : Node_Id;
1920 Call_Node : Node_Id;
1922 begin
1923 Call_Node :=
1924 Make_Function_Call (Loc,
1925 Name => New_Occurrence_Of (Target, Loc),
1926 Parameter_Associations => Actuals);
1928 if not Is_Interface (Etype (Prim)) then
1929 Result_Def := New_Copy (Result_Definition (Parent (Target)));
1931 -- Thunk of function returning a class-wide interface object. No
1932 -- extra displacement needed since the displacement is generated
1933 -- in the return statement of Prim. Example:
1935 -- type Iface is interface ...
1936 -- function F (O : Iface) return Iface'Class;
1938 -- type T is new ... and Iface with ...
1939 -- function F (O : T) return Iface'Class;
1941 elsif Is_Class_Wide_Type (Etype (Prim)) then
1942 Result_Def := New_Occurrence_Of (Etype (Prim), Loc);
1944 -- Thunk of function returning an interface object. Displacement
1945 -- needed. Example:
1947 -- type Iface is interface ...
1948 -- function F (O : Iface) return Iface;
1950 -- type T is new ... and Iface with ...
1951 -- function F (O : T) return T;
1953 else
1954 Result_Def :=
1955 New_Occurrence_Of (Class_Wide_Type (Etype (Prim)), Loc);
1957 -- Adding implicit conversion to force the displacement of
1958 -- the pointer to the object to reference the corresponding
1959 -- secondary dispatch table.
1961 Call_Node :=
1962 Make_Type_Conversion (Loc,
1963 Subtype_Mark =>
1964 New_Occurrence_Of (Class_Wide_Type (Etype (Prim)), Loc),
1965 Expression => Relocate_Node (Call_Node));
1966 end if;
1968 Thunk_Code :=
1969 Make_Subprogram_Body (Loc,
1970 Specification =>
1971 Make_Function_Specification (Loc,
1972 Defining_Unit_Name => Thunk_Id,
1973 Parameter_Specifications => Formals,
1974 Result_Definition => Result_Def),
1975 Declarations => Decl,
1976 Handled_Statement_Sequence =>
1977 Make_Handled_Sequence_Of_Statements (Loc,
1978 Statements => New_List (
1979 Make_Simple_Return_Statement (Loc, Call_Node))));
1980 end;
1981 end if;
1982 end Expand_Interface_Thunk;
1984 --------------------------
1985 -- Has_CPP_Constructors --
1986 --------------------------
1988 function Has_CPP_Constructors (Typ : Entity_Id) return Boolean is
1989 E : Entity_Id;
1991 begin
1992 -- Look for the constructor entities
1994 E := Next_Entity (Typ);
1995 while Present (E) loop
1996 if Ekind (E) = E_Function and then Is_Constructor (E) then
1997 return True;
1998 end if;
2000 Next_Entity (E);
2001 end loop;
2003 return False;
2004 end Has_CPP_Constructors;
2006 ------------
2007 -- Has_DT --
2008 ------------
2010 function Has_DT (Typ : Entity_Id) return Boolean is
2011 begin
2012 return not Is_Interface (Typ)
2013 and then not Restriction_Active (No_Dispatching_Calls);
2014 end Has_DT;
2016 ----------------------------------
2017 -- Is_Expanded_Dispatching_Call --
2018 ----------------------------------
2020 function Is_Expanded_Dispatching_Call (N : Node_Id) return Boolean is
2021 begin
2022 return Nkind (N) in N_Subprogram_Call
2023 and then Nkind (Name (N)) = N_Explicit_Dereference
2024 and then Is_Dispatch_Table_Entity (Etype (Name (N)));
2025 end Is_Expanded_Dispatching_Call;
2027 -----------------------------------------
2028 -- Is_Predefined_Dispatching_Operation --
2029 -----------------------------------------
2031 function Is_Predefined_Dispatching_Operation
2032 (E : Entity_Id) return Boolean
2034 TSS_Name : TSS_Name_Type;
2036 begin
2037 if not Is_Dispatching_Operation (E) then
2038 return False;
2039 end if;
2041 Get_Name_String (Chars (E));
2043 -- Most predefined primitives have internally generated names. Equality
2044 -- must be treated differently; the predefined operation is recognized
2045 -- as a homogeneous binary operator that returns Boolean.
2047 if Name_Len > TSS_Name_Type'Last then
2048 TSS_Name := TSS_Name_Type (Name_Buffer (Name_Len - TSS_Name'Length + 1
2049 .. Name_Len));
2050 if Chars (E) = Name_uSize
2051 or else TSS_Name = TSS_Stream_Read
2052 or else TSS_Name = TSS_Stream_Write
2053 or else TSS_Name = TSS_Stream_Input
2054 or else TSS_Name = TSS_Stream_Output
2055 or else
2056 (Chars (E) = Name_Op_Eq
2057 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
2058 or else Chars (E) = Name_uAssign
2059 or else TSS_Name = TSS_Deep_Adjust
2060 or else TSS_Name = TSS_Deep_Finalize
2061 or else Is_Predefined_Interface_Primitive (E)
2062 then
2063 return True;
2064 end if;
2065 end if;
2067 return False;
2068 end Is_Predefined_Dispatching_Operation;
2070 ---------------------------------------
2071 -- Is_Predefined_Internal_Operation --
2072 ---------------------------------------
2074 function Is_Predefined_Internal_Operation
2075 (E : Entity_Id) return Boolean
2077 TSS_Name : TSS_Name_Type;
2079 begin
2080 if not Is_Dispatching_Operation (E) then
2081 return False;
2082 end if;
2084 Get_Name_String (Chars (E));
2086 -- Most predefined primitives have internally generated names. Equality
2087 -- must be treated differently; the predefined operation is recognized
2088 -- as a homogeneous binary operator that returns Boolean.
2090 if Name_Len > TSS_Name_Type'Last then
2091 TSS_Name :=
2092 TSS_Name_Type
2093 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
2095 if Nam_In (Chars (E), Name_uSize, Name_uAssign)
2096 or else
2097 (Chars (E) = Name_Op_Eq
2098 and then Etype (First_Formal (E)) = Etype (Last_Formal (E)))
2099 or else TSS_Name = TSS_Deep_Adjust
2100 or else TSS_Name = TSS_Deep_Finalize
2101 or else Is_Predefined_Interface_Primitive (E)
2102 then
2103 return True;
2104 end if;
2105 end if;
2107 return False;
2108 end Is_Predefined_Internal_Operation;
2110 -------------------------------------
2111 -- Is_Predefined_Dispatching_Alias --
2112 -------------------------------------
2114 function Is_Predefined_Dispatching_Alias (Prim : Entity_Id) return Boolean
2116 begin
2117 return not Is_Predefined_Dispatching_Operation (Prim)
2118 and then Present (Alias (Prim))
2119 and then Is_Predefined_Dispatching_Operation (Ultimate_Alias (Prim));
2120 end Is_Predefined_Dispatching_Alias;
2122 ---------------------------------------
2123 -- Is_Predefined_Interface_Primitive --
2124 ---------------------------------------
2126 function Is_Predefined_Interface_Primitive (E : Entity_Id) return Boolean is
2127 begin
2128 -- In VM targets we don't restrict the functionality of this test to
2129 -- compiling in Ada 2005 mode since in VM targets any tagged type has
2130 -- these primitives.
2132 return (Ada_Version >= Ada_2005 or else not Tagged_Type_Expansion)
2133 and then Nam_In (Chars (E), Name_uDisp_Asynchronous_Select,
2134 Name_uDisp_Conditional_Select,
2135 Name_uDisp_Get_Prim_Op_Kind,
2136 Name_uDisp_Get_Task_Id,
2137 Name_uDisp_Requeue,
2138 Name_uDisp_Timed_Select);
2139 end Is_Predefined_Interface_Primitive;
2141 ----------------------------------------
2142 -- Make_Disp_Asynchronous_Select_Body --
2143 ----------------------------------------
2145 -- For interface types, generate:
2147 -- procedure _Disp_Asynchronous_Select
2148 -- (T : in out <Typ>;
2149 -- S : Integer;
2150 -- P : System.Address;
2151 -- B : out System.Storage_Elements.Dummy_Communication_Block;
2152 -- F : out Boolean)
2153 -- is
2154 -- begin
2155 -- F := False;
2156 -- C := Ada.Tags.POK_Function;
2157 -- end _Disp_Asynchronous_Select;
2159 -- For protected types, generate:
2161 -- procedure _Disp_Asynchronous_Select
2162 -- (T : in out <Typ>;
2163 -- S : Integer;
2164 -- P : System.Address;
2165 -- B : out System.Storage_Elements.Dummy_Communication_Block;
2166 -- F : out Boolean)
2167 -- is
2168 -- I : Integer :=
2169 -- Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP, S));
2170 -- Bnn : System.Tasking.Protected_Objects.Operations.
2171 -- Communication_Block;
2172 -- begin
2173 -- System.Tasking.Protected_Objects.Operations.Protected_Entry_Call
2174 -- (T._object'Access,
2175 -- System.Tasking.Protected_Objects.Protected_Entry_Index (I),
2176 -- P,
2177 -- System.Tasking.Asynchronous_Call,
2178 -- Bnn);
2179 -- B := System.Storage_Elements.Dummy_Communication_Block (Bnn);
2180 -- end _Disp_Asynchronous_Select;
2182 -- For task types, generate:
2184 -- procedure _Disp_Asynchronous_Select
2185 -- (T : in out <Typ>;
2186 -- S : Integer;
2187 -- P : System.Address;
2188 -- B : out System.Storage_Elements.Dummy_Communication_Block;
2189 -- F : out Boolean)
2190 -- is
2191 -- I : Integer :=
2192 -- Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP, S));
2193 -- begin
2194 -- System.Tasking.Rendezvous.Task_Entry_Call
2195 -- (T._task_id,
2196 -- System.Tasking.Task_Entry_Index (I),
2197 -- P,
2198 -- System.Tasking.Asynchronous_Call,
2199 -- F);
2200 -- end _Disp_Asynchronous_Select;
2202 function Make_Disp_Asynchronous_Select_Body
2203 (Typ : Entity_Id) return Node_Id
2205 Com_Block : Entity_Id;
2206 Conc_Typ : Entity_Id := Empty;
2207 Decls : constant List_Id := New_List;
2208 Loc : constant Source_Ptr := Sloc (Typ);
2209 Obj_Ref : Node_Id;
2210 Stmts : constant List_Id := New_List;
2211 Tag_Node : Node_Id;
2213 begin
2214 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2216 -- Null body is generated for interface types
2218 if Is_Interface (Typ) then
2219 return
2220 Make_Subprogram_Body (Loc,
2221 Specification =>
2222 Make_Disp_Asynchronous_Select_Spec (Typ),
2223 Declarations => New_List,
2224 Handled_Statement_Sequence =>
2225 Make_Handled_Sequence_Of_Statements (Loc,
2226 New_List (
2227 Make_Assignment_Statement (Loc,
2228 Name => Make_Identifier (Loc, Name_uF),
2229 Expression => New_Occurrence_Of (Standard_False, Loc)))));
2230 end if;
2232 if Is_Concurrent_Record_Type (Typ) then
2233 Conc_Typ := Corresponding_Concurrent_Type (Typ);
2235 -- Generate:
2236 -- I : Integer :=
2237 -- Ada.Tags.Get_Entry_Index (Ada.Tags.Tag! (<type>VP), S);
2239 -- where I will be used to capture the entry index of the primitive
2240 -- wrapper at position S.
2242 if Tagged_Type_Expansion then
2243 Tag_Node :=
2244 Unchecked_Convert_To (RTE (RE_Tag),
2245 New_Occurrence_Of
2246 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc));
2247 else
2248 Tag_Node :=
2249 Make_Attribute_Reference (Loc,
2250 Prefix => New_Occurrence_Of (Typ, Loc),
2251 Attribute_Name => Name_Tag);
2252 end if;
2254 Append_To (Decls,
2255 Make_Object_Declaration (Loc,
2256 Defining_Identifier =>
2257 Make_Defining_Identifier (Loc, Name_uI),
2258 Object_Definition =>
2259 New_Occurrence_Of (Standard_Integer, Loc),
2260 Expression =>
2261 Make_Function_Call (Loc,
2262 Name =>
2263 New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
2264 Parameter_Associations =>
2265 New_List (Tag_Node, Make_Identifier (Loc, Name_uS)))));
2267 if Ekind (Conc_Typ) = E_Protected_Type then
2269 -- Generate:
2270 -- Bnn : Communication_Block;
2272 Com_Block := Make_Temporary (Loc, 'B');
2273 Append_To (Decls,
2274 Make_Object_Declaration (Loc,
2275 Defining_Identifier => Com_Block,
2276 Object_Definition =>
2277 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
2279 -- Build T._object'Access for calls below
2281 Obj_Ref :=
2282 Make_Attribute_Reference (Loc,
2283 Attribute_Name => Name_Unchecked_Access,
2284 Prefix =>
2285 Make_Selected_Component (Loc,
2286 Prefix => Make_Identifier (Loc, Name_uT),
2287 Selector_Name => Make_Identifier (Loc, Name_uObject)));
2289 case Corresponding_Runtime_Package (Conc_Typ) is
2290 when System_Tasking_Protected_Objects_Entries =>
2292 -- Generate:
2293 -- Protected_Entry_Call
2294 -- (T._object'Access, -- Object
2295 -- Protected_Entry_Index! (I), -- E
2296 -- P, -- Uninterpreted_Data
2297 -- Asynchronous_Call, -- Mode
2298 -- Bnn); -- Communication_Block
2300 -- where T is the protected object, I is the entry index, P
2301 -- is the wrapped parameters and B is the name of the
2302 -- communication block.
2304 Append_To (Stmts,
2305 Make_Procedure_Call_Statement (Loc,
2306 Name =>
2307 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
2308 Parameter_Associations =>
2309 New_List (
2310 Obj_Ref,
2312 Make_Unchecked_Type_Conversion (Loc, -- entry index
2313 Subtype_Mark =>
2314 New_Occurrence_Of
2315 (RTE (RE_Protected_Entry_Index), Loc),
2316 Expression => Make_Identifier (Loc, Name_uI)),
2318 Make_Identifier (Loc, Name_uP), -- parameter block
2319 New_Occurrence_Of -- Asynchronous_Call
2320 (RTE (RE_Asynchronous_Call), Loc),
2321 New_Occurrence_Of -- comm block
2322 (Com_Block, Loc))));
2324 when others =>
2325 raise Program_Error;
2326 end case;
2328 -- Generate:
2329 -- B := Dummy_Communication_Block (Bnn);
2331 Append_To (Stmts,
2332 Make_Assignment_Statement (Loc,
2333 Name => Make_Identifier (Loc, Name_uB),
2334 Expression =>
2335 Make_Unchecked_Type_Conversion (Loc,
2336 Subtype_Mark =>
2337 New_Occurrence_Of
2338 (RTE (RE_Dummy_Communication_Block), Loc),
2339 Expression => New_Occurrence_Of (Com_Block, Loc))));
2341 -- Generate:
2342 -- F := False;
2344 Append_To (Stmts,
2345 Make_Assignment_Statement (Loc,
2346 Name => Make_Identifier (Loc, Name_uF),
2347 Expression => New_Occurrence_Of (Standard_False, Loc)));
2349 else
2350 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
2352 -- Generate:
2353 -- Task_Entry_Call
2354 -- (T._task_id, -- Acceptor
2355 -- Task_Entry_Index! (I), -- E
2356 -- P, -- Uninterpreted_Data
2357 -- Asynchronous_Call, -- Mode
2358 -- F); -- Rendezvous_Successful
2360 -- where T is the task object, I is the entry index, P is the
2361 -- wrapped parameters and F is the status flag.
2363 Append_To (Stmts,
2364 Make_Procedure_Call_Statement (Loc,
2365 Name =>
2366 New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
2367 Parameter_Associations =>
2368 New_List (
2369 Make_Selected_Component (Loc, -- T._task_id
2370 Prefix => Make_Identifier (Loc, Name_uT),
2371 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2373 Make_Unchecked_Type_Conversion (Loc, -- entry index
2374 Subtype_Mark =>
2375 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
2376 Expression => Make_Identifier (Loc, Name_uI)),
2378 Make_Identifier (Loc, Name_uP), -- parameter block
2379 New_Occurrence_Of -- Asynchronous_Call
2380 (RTE (RE_Asynchronous_Call), Loc),
2381 Make_Identifier (Loc, Name_uF)))); -- status flag
2382 end if;
2384 else
2385 -- Ensure that the statements list is non-empty
2387 Append_To (Stmts,
2388 Make_Assignment_Statement (Loc,
2389 Name => Make_Identifier (Loc, Name_uF),
2390 Expression => New_Occurrence_Of (Standard_False, Loc)));
2391 end if;
2393 return
2394 Make_Subprogram_Body (Loc,
2395 Specification =>
2396 Make_Disp_Asynchronous_Select_Spec (Typ),
2397 Declarations => Decls,
2398 Handled_Statement_Sequence =>
2399 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
2400 end Make_Disp_Asynchronous_Select_Body;
2402 ----------------------------------------
2403 -- Make_Disp_Asynchronous_Select_Spec --
2404 ----------------------------------------
2406 function Make_Disp_Asynchronous_Select_Spec
2407 (Typ : Entity_Id) return Node_Id
2409 Loc : constant Source_Ptr := Sloc (Typ);
2410 Def_Id : constant Node_Id :=
2411 Make_Defining_Identifier (Loc,
2412 Name_uDisp_Asynchronous_Select);
2413 Params : constant List_Id := New_List;
2415 begin
2416 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2418 -- T : in out Typ; -- Object parameter
2419 -- S : Integer; -- Primitive operation slot
2420 -- P : Address; -- Wrapped parameters
2421 -- B : out Dummy_Communication_Block; -- Communication block dummy
2422 -- F : out Boolean; -- Status flag
2424 Append_List_To (Params, New_List (
2426 Make_Parameter_Specification (Loc,
2427 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
2428 Parameter_Type => New_Occurrence_Of (Typ, Loc),
2429 In_Present => True,
2430 Out_Present => True),
2432 Make_Parameter_Specification (Loc,
2433 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uS),
2434 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
2436 Make_Parameter_Specification (Loc,
2437 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
2438 Parameter_Type => New_Occurrence_Of (RTE (RE_Address), Loc)),
2440 Make_Parameter_Specification (Loc,
2441 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uB),
2442 Parameter_Type =>
2443 New_Occurrence_Of (RTE (RE_Dummy_Communication_Block), Loc),
2444 Out_Present => True),
2446 Make_Parameter_Specification (Loc,
2447 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uF),
2448 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
2449 Out_Present => True)));
2451 return
2452 Make_Procedure_Specification (Loc,
2453 Defining_Unit_Name => Def_Id,
2454 Parameter_Specifications => Params);
2455 end Make_Disp_Asynchronous_Select_Spec;
2457 ---------------------------------------
2458 -- Make_Disp_Conditional_Select_Body --
2459 ---------------------------------------
2461 -- For interface types, generate:
2463 -- procedure _Disp_Conditional_Select
2464 -- (T : in out <Typ>;
2465 -- S : Integer;
2466 -- P : System.Address;
2467 -- C : out Ada.Tags.Prim_Op_Kind;
2468 -- F : out Boolean)
2469 -- is
2470 -- begin
2471 -- F := False;
2472 -- C := Ada.Tags.POK_Function;
2473 -- end _Disp_Conditional_Select;
2475 -- For protected types, generate:
2477 -- procedure _Disp_Conditional_Select
2478 -- (T : in out <Typ>;
2479 -- S : Integer;
2480 -- P : System.Address;
2481 -- C : out Ada.Tags.Prim_Op_Kind;
2482 -- F : out Boolean)
2483 -- is
2484 -- I : Integer;
2485 -- Bnn : System.Tasking.Protected_Objects.Operations.
2486 -- Communication_Block;
2488 -- begin
2489 -- C := Ada.Tags.Get_Prim_Op_Kind (Ada.Tags.Tag (<Typ>VP, S));
2491 -- if C = Ada.Tags.POK_Procedure
2492 -- or else C = Ada.Tags.POK_Protected_Procedure
2493 -- or else C = Ada.Tags.POK_Task_Procedure
2494 -- then
2495 -- F := True;
2496 -- return;
2497 -- end if;
2499 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP, S));
2500 -- System.Tasking.Protected_Objects.Operations.Protected_Entry_Call
2501 -- (T.object'Access,
2502 -- System.Tasking.Protected_Objects.Protected_Entry_Index (I),
2503 -- P,
2504 -- System.Tasking.Conditional_Call,
2505 -- Bnn);
2506 -- F := not Cancelled (Bnn);
2507 -- end _Disp_Conditional_Select;
2509 -- For task types, generate:
2511 -- procedure _Disp_Conditional_Select
2512 -- (T : in out <Typ>;
2513 -- S : Integer;
2514 -- P : System.Address;
2515 -- C : out Ada.Tags.Prim_Op_Kind;
2516 -- F : out Boolean)
2517 -- is
2518 -- I : Integer;
2520 -- begin
2521 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP, S));
2522 -- System.Tasking.Rendezvous.Task_Entry_Call
2523 -- (T._task_id,
2524 -- System.Tasking.Task_Entry_Index (I),
2525 -- P,
2526 -- System.Tasking.Conditional_Call,
2527 -- F);
2528 -- end _Disp_Conditional_Select;
2530 function Make_Disp_Conditional_Select_Body
2531 (Typ : Entity_Id) return Node_Id
2533 Loc : constant Source_Ptr := Sloc (Typ);
2534 Blk_Nam : Entity_Id;
2535 Conc_Typ : Entity_Id := Empty;
2536 Decls : constant List_Id := New_List;
2537 Obj_Ref : Node_Id;
2538 Stmts : constant List_Id := New_List;
2539 Tag_Node : Node_Id;
2541 begin
2542 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2544 -- Null body is generated for interface types
2546 if Is_Interface (Typ) then
2547 return
2548 Make_Subprogram_Body (Loc,
2549 Specification =>
2550 Make_Disp_Conditional_Select_Spec (Typ),
2551 Declarations => No_List,
2552 Handled_Statement_Sequence =>
2553 Make_Handled_Sequence_Of_Statements (Loc,
2554 New_List (Make_Assignment_Statement (Loc,
2555 Name => Make_Identifier (Loc, Name_uF),
2556 Expression => New_Occurrence_Of (Standard_False, Loc)))));
2557 end if;
2559 if Is_Concurrent_Record_Type (Typ) then
2560 Conc_Typ := Corresponding_Concurrent_Type (Typ);
2562 -- Generate:
2563 -- I : Integer;
2565 -- where I will be used to capture the entry index of the primitive
2566 -- wrapper at position S.
2568 Append_To (Decls,
2569 Make_Object_Declaration (Loc,
2570 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uI),
2571 Object_Definition =>
2572 New_Occurrence_Of (Standard_Integer, Loc)));
2574 -- Generate:
2575 -- C := Ada.Tags.Get_Prim_Op_Kind (Ada.Tags.Tag! (<type>VP), S);
2577 -- if C = POK_Procedure
2578 -- or else C = POK_Protected_Procedure
2579 -- or else C = POK_Task_Procedure;
2580 -- then
2581 -- F := True;
2582 -- return;
2583 -- end if;
2585 Build_Common_Dispatching_Select_Statements (Typ, Stmts);
2587 -- Generate:
2588 -- Bnn : Communication_Block;
2590 -- where Bnn is the name of the communication block used in the
2591 -- call to Protected_Entry_Call.
2593 Blk_Nam := Make_Temporary (Loc, 'B');
2594 Append_To (Decls,
2595 Make_Object_Declaration (Loc,
2596 Defining_Identifier => Blk_Nam,
2597 Object_Definition =>
2598 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
2600 -- Generate:
2601 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag! (<type>VP), S);
2603 -- I is the entry index and S is the dispatch table slot
2605 if Tagged_Type_Expansion then
2606 Tag_Node :=
2607 Unchecked_Convert_To (RTE (RE_Tag),
2608 New_Occurrence_Of
2609 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc));
2611 else
2612 Tag_Node :=
2613 Make_Attribute_Reference (Loc,
2614 Prefix => New_Occurrence_Of (Typ, Loc),
2615 Attribute_Name => Name_Tag);
2616 end if;
2618 Append_To (Stmts,
2619 Make_Assignment_Statement (Loc,
2620 Name => Make_Identifier (Loc, Name_uI),
2621 Expression =>
2622 Make_Function_Call (Loc,
2623 Name =>
2624 New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
2625 Parameter_Associations => New_List (
2626 Tag_Node,
2627 Make_Identifier (Loc, Name_uS)))));
2629 if Ekind (Conc_Typ) = E_Protected_Type then
2631 Obj_Ref := -- T._object'Access
2632 Make_Attribute_Reference (Loc,
2633 Attribute_Name => Name_Unchecked_Access,
2634 Prefix =>
2635 Make_Selected_Component (Loc,
2636 Prefix => Make_Identifier (Loc, Name_uT),
2637 Selector_Name => Make_Identifier (Loc, Name_uObject)));
2639 case Corresponding_Runtime_Package (Conc_Typ) is
2640 when System_Tasking_Protected_Objects_Entries =>
2641 -- Generate:
2643 -- Protected_Entry_Call
2644 -- (T._object'Access, -- Object
2645 -- Protected_Entry_Index! (I), -- E
2646 -- P, -- Uninterpreted_Data
2647 -- Conditional_Call, -- Mode
2648 -- Bnn); -- Block
2650 -- where T is the protected object, I is the entry index, P
2651 -- are the wrapped parameters and Bnn is the name of the
2652 -- communication block.
2654 Append_To (Stmts,
2655 Make_Procedure_Call_Statement (Loc,
2656 Name =>
2657 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
2658 Parameter_Associations => New_List (
2659 Obj_Ref,
2661 Make_Unchecked_Type_Conversion (Loc, -- entry index
2662 Subtype_Mark =>
2663 New_Occurrence_Of
2664 (RTE (RE_Protected_Entry_Index), Loc),
2665 Expression => Make_Identifier (Loc, Name_uI)),
2667 Make_Identifier (Loc, Name_uP), -- parameter block
2669 New_Occurrence_Of -- Conditional_Call
2670 (RTE (RE_Conditional_Call), Loc),
2671 New_Occurrence_Of -- Bnn
2672 (Blk_Nam, Loc))));
2674 when System_Tasking_Protected_Objects_Single_Entry =>
2676 -- If we are compiling for a restricted run-time, the call
2677 -- uses the simpler form.
2679 Append_To (Stmts,
2680 Make_Procedure_Call_Statement (Loc,
2681 Name =>
2682 New_Occurrence_Of
2683 (RTE (RE_Protected_Single_Entry_Call), Loc),
2684 Parameter_Associations => New_List (
2685 Obj_Ref,
2687 Make_Attribute_Reference (Loc,
2688 Prefix => Make_Identifier (Loc, Name_uP),
2689 Attribute_Name => Name_Address),
2691 New_Occurrence_Of
2692 (RTE (RE_Conditional_Call), Loc))));
2693 when others =>
2694 raise Program_Error;
2695 end case;
2697 -- Generate:
2698 -- F := not Cancelled (Bnn);
2700 -- where F is the success flag. The status of Cancelled is negated
2701 -- in order to match the behaviour of the version for task types.
2703 Append_To (Stmts,
2704 Make_Assignment_Statement (Loc,
2705 Name => Make_Identifier (Loc, Name_uF),
2706 Expression =>
2707 Make_Op_Not (Loc,
2708 Right_Opnd =>
2709 Make_Function_Call (Loc,
2710 Name =>
2711 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
2712 Parameter_Associations => New_List (
2713 New_Occurrence_Of (Blk_Nam, Loc))))));
2714 else
2715 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
2717 -- Generate:
2718 -- Task_Entry_Call
2719 -- (T._task_id, -- Acceptor
2720 -- Task_Entry_Index! (I), -- E
2721 -- P, -- Uninterpreted_Data
2722 -- Conditional_Call, -- Mode
2723 -- F); -- Rendezvous_Successful
2725 -- where T is the task object, I is the entry index, P are the
2726 -- wrapped parameters and F is the status flag.
2728 Append_To (Stmts,
2729 Make_Procedure_Call_Statement (Loc,
2730 Name =>
2731 New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
2732 Parameter_Associations => New_List (
2734 Make_Selected_Component (Loc, -- T._task_id
2735 Prefix => Make_Identifier (Loc, Name_uT),
2736 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2738 Make_Unchecked_Type_Conversion (Loc, -- entry index
2739 Subtype_Mark =>
2740 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
2741 Expression => Make_Identifier (Loc, Name_uI)),
2743 Make_Identifier (Loc, Name_uP), -- parameter block
2744 New_Occurrence_Of -- Conditional_Call
2745 (RTE (RE_Conditional_Call), Loc),
2746 Make_Identifier (Loc, Name_uF)))); -- status flag
2747 end if;
2749 else
2750 -- Initialize out parameters
2752 Append_To (Stmts,
2753 Make_Assignment_Statement (Loc,
2754 Name => Make_Identifier (Loc, Name_uF),
2755 Expression => New_Occurrence_Of (Standard_False, Loc)));
2756 Append_To (Stmts,
2757 Make_Assignment_Statement (Loc,
2758 Name => Make_Identifier (Loc, Name_uC),
2759 Expression => New_Occurrence_Of (RTE (RE_POK_Function), Loc)));
2760 end if;
2762 return
2763 Make_Subprogram_Body (Loc,
2764 Specification =>
2765 Make_Disp_Conditional_Select_Spec (Typ),
2766 Declarations => Decls,
2767 Handled_Statement_Sequence =>
2768 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
2769 end Make_Disp_Conditional_Select_Body;
2771 ---------------------------------------
2772 -- Make_Disp_Conditional_Select_Spec --
2773 ---------------------------------------
2775 function Make_Disp_Conditional_Select_Spec
2776 (Typ : Entity_Id) return Node_Id
2778 Loc : constant Source_Ptr := Sloc (Typ);
2779 Def_Id : constant Node_Id :=
2780 Make_Defining_Identifier (Loc,
2781 Name_uDisp_Conditional_Select);
2782 Params : constant List_Id := New_List;
2784 begin
2785 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2787 -- T : in out Typ; -- Object parameter
2788 -- S : Integer; -- Primitive operation slot
2789 -- P : Address; -- Wrapped parameters
2790 -- C : out Prim_Op_Kind; -- Call kind
2791 -- F : out Boolean; -- Status flag
2793 Append_List_To (Params, New_List (
2795 Make_Parameter_Specification (Loc,
2796 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
2797 Parameter_Type => New_Occurrence_Of (Typ, Loc),
2798 In_Present => True,
2799 Out_Present => True),
2801 Make_Parameter_Specification (Loc,
2802 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uS),
2803 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
2805 Make_Parameter_Specification (Loc,
2806 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
2807 Parameter_Type => New_Occurrence_Of (RTE (RE_Address), Loc)),
2809 Make_Parameter_Specification (Loc,
2810 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uC),
2811 Parameter_Type =>
2812 New_Occurrence_Of (RTE (RE_Prim_Op_Kind), Loc),
2813 Out_Present => True),
2815 Make_Parameter_Specification (Loc,
2816 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uF),
2817 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
2818 Out_Present => True)));
2820 return
2821 Make_Procedure_Specification (Loc,
2822 Defining_Unit_Name => Def_Id,
2823 Parameter_Specifications => Params);
2824 end Make_Disp_Conditional_Select_Spec;
2826 -------------------------------------
2827 -- Make_Disp_Get_Prim_Op_Kind_Body --
2828 -------------------------------------
2830 function Make_Disp_Get_Prim_Op_Kind_Body (Typ : Entity_Id) return Node_Id is
2831 Loc : constant Source_Ptr := Sloc (Typ);
2832 Tag_Node : Node_Id;
2834 begin
2835 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2837 if Is_Interface (Typ) then
2838 return
2839 Make_Subprogram_Body (Loc,
2840 Specification =>
2841 Make_Disp_Get_Prim_Op_Kind_Spec (Typ),
2842 Declarations => New_List,
2843 Handled_Statement_Sequence =>
2844 Make_Handled_Sequence_Of_Statements (Loc,
2845 New_List (Make_Null_Statement (Loc))));
2846 end if;
2848 -- Generate:
2849 -- C := get_prim_op_kind (tag! (<type>VP), S);
2851 -- where C is the out parameter capturing the call kind and S is the
2852 -- dispatch table slot number.
2854 if Tagged_Type_Expansion then
2855 Tag_Node :=
2856 Unchecked_Convert_To (RTE (RE_Tag),
2857 New_Occurrence_Of
2858 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc));
2860 else
2861 Tag_Node :=
2862 Make_Attribute_Reference (Loc,
2863 Prefix => New_Occurrence_Of (Typ, Loc),
2864 Attribute_Name => Name_Tag);
2865 end if;
2867 return
2868 Make_Subprogram_Body (Loc,
2869 Specification =>
2870 Make_Disp_Get_Prim_Op_Kind_Spec (Typ),
2871 Declarations => New_List,
2872 Handled_Statement_Sequence =>
2873 Make_Handled_Sequence_Of_Statements (Loc,
2874 New_List (
2875 Make_Assignment_Statement (Loc,
2876 Name => Make_Identifier (Loc, Name_uC),
2877 Expression =>
2878 Make_Function_Call (Loc,
2879 Name =>
2880 New_Occurrence_Of (RTE (RE_Get_Prim_Op_Kind), Loc),
2881 Parameter_Associations => New_List (
2882 Tag_Node,
2883 Make_Identifier (Loc, Name_uS)))))));
2884 end Make_Disp_Get_Prim_Op_Kind_Body;
2886 -------------------------------------
2887 -- Make_Disp_Get_Prim_Op_Kind_Spec --
2888 -------------------------------------
2890 function Make_Disp_Get_Prim_Op_Kind_Spec
2891 (Typ : Entity_Id) return Node_Id
2893 Loc : constant Source_Ptr := Sloc (Typ);
2894 Def_Id : constant Node_Id :=
2895 Make_Defining_Identifier (Loc, Name_uDisp_Get_Prim_Op_Kind);
2896 Params : constant List_Id := New_List;
2898 begin
2899 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2901 -- T : in out Typ; -- Object parameter
2902 -- S : Integer; -- Primitive operation slot
2903 -- C : out Prim_Op_Kind; -- Call kind
2905 Append_List_To (Params, New_List (
2907 Make_Parameter_Specification (Loc,
2908 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
2909 Parameter_Type => New_Occurrence_Of (Typ, Loc),
2910 In_Present => True,
2911 Out_Present => True),
2913 Make_Parameter_Specification (Loc,
2914 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uS),
2915 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
2917 Make_Parameter_Specification (Loc,
2918 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uC),
2919 Parameter_Type =>
2920 New_Occurrence_Of (RTE (RE_Prim_Op_Kind), Loc),
2921 Out_Present => True)));
2923 return
2924 Make_Procedure_Specification (Loc,
2925 Defining_Unit_Name => Def_Id,
2926 Parameter_Specifications => Params);
2927 end Make_Disp_Get_Prim_Op_Kind_Spec;
2929 --------------------------------
2930 -- Make_Disp_Get_Task_Id_Body --
2931 --------------------------------
2933 function Make_Disp_Get_Task_Id_Body
2934 (Typ : Entity_Id) return Node_Id
2936 Loc : constant Source_Ptr := Sloc (Typ);
2937 Ret : Node_Id;
2939 begin
2940 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2942 if Is_Concurrent_Record_Type (Typ)
2943 and then Ekind (Corresponding_Concurrent_Type (Typ)) = E_Task_Type
2944 then
2945 -- Generate:
2946 -- return To_Address (_T._task_id);
2948 Ret :=
2949 Make_Simple_Return_Statement (Loc,
2950 Expression =>
2951 Make_Unchecked_Type_Conversion (Loc,
2952 Subtype_Mark => New_Occurrence_Of (RTE (RE_Address), Loc),
2953 Expression =>
2954 Make_Selected_Component (Loc,
2955 Prefix => Make_Identifier (Loc, Name_uT),
2956 Selector_Name => Make_Identifier (Loc, Name_uTask_Id))));
2958 -- A null body is constructed for non-task types
2960 else
2961 -- Generate:
2962 -- return Null_Address;
2964 Ret :=
2965 Make_Simple_Return_Statement (Loc,
2966 Expression => New_Occurrence_Of (RTE (RE_Null_Address), Loc));
2967 end if;
2969 return
2970 Make_Subprogram_Body (Loc,
2971 Specification => Make_Disp_Get_Task_Id_Spec (Typ),
2972 Declarations => New_List,
2973 Handled_Statement_Sequence =>
2974 Make_Handled_Sequence_Of_Statements (Loc, New_List (Ret)));
2975 end Make_Disp_Get_Task_Id_Body;
2977 --------------------------------
2978 -- Make_Disp_Get_Task_Id_Spec --
2979 --------------------------------
2981 function Make_Disp_Get_Task_Id_Spec
2982 (Typ : Entity_Id) return Node_Id
2984 Loc : constant Source_Ptr := Sloc (Typ);
2986 begin
2987 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2989 return
2990 Make_Function_Specification (Loc,
2991 Defining_Unit_Name =>
2992 Make_Defining_Identifier (Loc, Name_uDisp_Get_Task_Id),
2993 Parameter_Specifications => New_List (
2994 Make_Parameter_Specification (Loc,
2995 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
2996 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
2997 Result_Definition =>
2998 New_Occurrence_Of (RTE (RE_Address), Loc));
2999 end Make_Disp_Get_Task_Id_Spec;
3001 ----------------------------
3002 -- Make_Disp_Requeue_Body --
3003 ----------------------------
3005 function Make_Disp_Requeue_Body
3006 (Typ : Entity_Id) return Node_Id
3008 Loc : constant Source_Ptr := Sloc (Typ);
3009 Conc_Typ : Entity_Id := Empty;
3010 Stmts : constant List_Id := New_List;
3012 begin
3013 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3015 -- Null body is generated for interface types and non-concurrent
3016 -- tagged types.
3018 if Is_Interface (Typ)
3019 or else not Is_Concurrent_Record_Type (Typ)
3020 then
3021 return
3022 Make_Subprogram_Body (Loc,
3023 Specification => Make_Disp_Requeue_Spec (Typ),
3024 Declarations => No_List,
3025 Handled_Statement_Sequence =>
3026 Make_Handled_Sequence_Of_Statements (Loc,
3027 New_List (Make_Null_Statement (Loc))));
3028 end if;
3030 Conc_Typ := Corresponding_Concurrent_Type (Typ);
3032 if Ekind (Conc_Typ) = E_Protected_Type then
3034 -- Generate statements:
3035 -- if F then
3036 -- System.Tasking.Protected_Objects.Operations.
3037 -- Requeue_Protected_Entry
3038 -- (Protection_Entries_Access (P),
3039 -- O._object'Unchecked_Access,
3040 -- Protected_Entry_Index (I),
3041 -- A);
3042 -- else
3043 -- System.Tasking.Protected_Objects.Operations.
3044 -- Requeue_Task_To_Protected_Entry
3045 -- (O._object'Unchecked_Access,
3046 -- Protected_Entry_Index (I),
3047 -- A);
3048 -- end if;
3050 if Restriction_Active (No_Entry_Queue) then
3051 Append_To (Stmts, Make_Null_Statement (Loc));
3052 else
3053 Append_To (Stmts,
3054 Make_If_Statement (Loc,
3055 Condition => Make_Identifier (Loc, Name_uF),
3057 Then_Statements =>
3058 New_List (
3060 -- Call to Requeue_Protected_Entry
3062 Make_Procedure_Call_Statement (Loc,
3063 Name =>
3064 New_Occurrence_Of
3065 (RTE (RE_Requeue_Protected_Entry), Loc),
3066 Parameter_Associations =>
3067 New_List (
3069 Make_Unchecked_Type_Conversion (Loc, -- PEA (P)
3070 Subtype_Mark =>
3071 New_Occurrence_Of (
3072 RTE (RE_Protection_Entries_Access), Loc),
3073 Expression =>
3074 Make_Identifier (Loc, Name_uP)),
3076 Make_Attribute_Reference (Loc, -- O._object'Acc
3077 Attribute_Name =>
3078 Name_Unchecked_Access,
3079 Prefix =>
3080 Make_Selected_Component (Loc,
3081 Prefix =>
3082 Make_Identifier (Loc, Name_uO),
3083 Selector_Name =>
3084 Make_Identifier (Loc, Name_uObject))),
3086 Make_Unchecked_Type_Conversion (Loc, -- entry index
3087 Subtype_Mark =>
3088 New_Occurrence_Of
3089 (RTE (RE_Protected_Entry_Index), Loc),
3090 Expression => Make_Identifier (Loc, Name_uI)),
3092 Make_Identifier (Loc, Name_uA)))), -- abort status
3094 Else_Statements =>
3095 New_List (
3097 -- Call to Requeue_Task_To_Protected_Entry
3099 Make_Procedure_Call_Statement (Loc,
3100 Name =>
3101 New_Occurrence_Of
3102 (RTE (RE_Requeue_Task_To_Protected_Entry), Loc),
3103 Parameter_Associations =>
3104 New_List (
3106 Make_Attribute_Reference (Loc, -- O._object'Acc
3107 Attribute_Name => Name_Unchecked_Access,
3108 Prefix =>
3109 Make_Selected_Component (Loc,
3110 Prefix =>
3111 Make_Identifier (Loc, Name_uO),
3112 Selector_Name =>
3113 Make_Identifier (Loc, Name_uObject))),
3115 Make_Unchecked_Type_Conversion (Loc, -- entry index
3116 Subtype_Mark =>
3117 New_Occurrence_Of
3118 (RTE (RE_Protected_Entry_Index), Loc),
3119 Expression => Make_Identifier (Loc, Name_uI)),
3121 Make_Identifier (Loc, Name_uA)))))); -- abort status
3122 end if;
3124 else
3125 pragma Assert (Is_Task_Type (Conc_Typ));
3127 -- Generate:
3128 -- if F then
3129 -- System.Tasking.Rendezvous.Requeue_Protected_To_Task_Entry
3130 -- (Protection_Entries_Access (P),
3131 -- O._task_id,
3132 -- Task_Entry_Index (I),
3133 -- A);
3134 -- else
3135 -- System.Tasking.Rendezvous.Requeue_Task_Entry
3136 -- (O._task_id,
3137 -- Task_Entry_Index (I),
3138 -- A);
3139 -- end if;
3141 Append_To (Stmts,
3142 Make_If_Statement (Loc,
3143 Condition => Make_Identifier (Loc, Name_uF),
3145 Then_Statements => New_List (
3147 -- Call to Requeue_Protected_To_Task_Entry
3149 Make_Procedure_Call_Statement (Loc,
3150 Name =>
3151 New_Occurrence_Of
3152 (RTE (RE_Requeue_Protected_To_Task_Entry), Loc),
3154 Parameter_Associations => New_List (
3156 Make_Unchecked_Type_Conversion (Loc, -- PEA (P)
3157 Subtype_Mark =>
3158 New_Occurrence_Of
3159 (RTE (RE_Protection_Entries_Access), Loc),
3160 Expression => Make_Identifier (Loc, Name_uP)),
3162 Make_Selected_Component (Loc, -- O._task_id
3163 Prefix => Make_Identifier (Loc, Name_uO),
3164 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3166 Make_Unchecked_Type_Conversion (Loc, -- entry index
3167 Subtype_Mark =>
3168 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
3169 Expression => Make_Identifier (Loc, Name_uI)),
3171 Make_Identifier (Loc, Name_uA)))), -- abort status
3173 Else_Statements => New_List (
3175 -- Call to Requeue_Task_Entry
3177 Make_Procedure_Call_Statement (Loc,
3178 Name =>
3179 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc),
3181 Parameter_Associations => New_List (
3183 Make_Selected_Component (Loc, -- O._task_id
3184 Prefix => Make_Identifier (Loc, Name_uO),
3185 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3187 Make_Unchecked_Type_Conversion (Loc, -- entry index
3188 Subtype_Mark =>
3189 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
3190 Expression => Make_Identifier (Loc, Name_uI)),
3192 Make_Identifier (Loc, Name_uA)))))); -- abort status
3193 end if;
3195 -- Even though no declarations are needed in both cases, we allocate
3196 -- a list for entities added by Freeze.
3198 return
3199 Make_Subprogram_Body (Loc,
3200 Specification => Make_Disp_Requeue_Spec (Typ),
3201 Declarations => New_List,
3202 Handled_Statement_Sequence =>
3203 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
3204 end Make_Disp_Requeue_Body;
3206 ----------------------------
3207 -- Make_Disp_Requeue_Spec --
3208 ----------------------------
3210 function Make_Disp_Requeue_Spec
3211 (Typ : Entity_Id) return Node_Id
3213 Loc : constant Source_Ptr := Sloc (Typ);
3215 begin
3216 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3218 -- O : in out Typ; - Object parameter
3219 -- F : Boolean; - Protected (True) / task (False) flag
3220 -- P : Address; - Protection_Entries_Access value
3221 -- I : Entry_Index - Index of entry call
3222 -- A : Boolean - Abort flag
3224 -- Note that the Protection_Entries_Access value is represented as a
3225 -- System.Address in order to avoid dragging in the tasking runtime
3226 -- when compiling sources without tasking constructs.
3228 return
3229 Make_Procedure_Specification (Loc,
3230 Defining_Unit_Name =>
3231 Make_Defining_Identifier (Loc, Name_uDisp_Requeue),
3233 Parameter_Specifications => New_List (
3235 Make_Parameter_Specification (Loc, -- O
3236 Defining_Identifier =>
3237 Make_Defining_Identifier (Loc, Name_uO),
3238 Parameter_Type =>
3239 New_Occurrence_Of (Typ, Loc),
3240 In_Present => True,
3241 Out_Present => True),
3243 Make_Parameter_Specification (Loc, -- F
3244 Defining_Identifier =>
3245 Make_Defining_Identifier (Loc, Name_uF),
3246 Parameter_Type =>
3247 New_Occurrence_Of (Standard_Boolean, Loc)),
3249 Make_Parameter_Specification (Loc, -- P
3250 Defining_Identifier =>
3251 Make_Defining_Identifier (Loc, Name_uP),
3252 Parameter_Type =>
3253 New_Occurrence_Of (RTE (RE_Address), Loc)),
3255 Make_Parameter_Specification (Loc, -- I
3256 Defining_Identifier =>
3257 Make_Defining_Identifier (Loc, Name_uI),
3258 Parameter_Type =>
3259 New_Occurrence_Of (Standard_Integer, Loc)),
3261 Make_Parameter_Specification (Loc, -- A
3262 Defining_Identifier =>
3263 Make_Defining_Identifier (Loc, Name_uA),
3264 Parameter_Type =>
3265 New_Occurrence_Of (Standard_Boolean, Loc))));
3266 end Make_Disp_Requeue_Spec;
3268 ---------------------------------
3269 -- Make_Disp_Timed_Select_Body --
3270 ---------------------------------
3272 -- For interface types, generate:
3274 -- procedure _Disp_Timed_Select
3275 -- (T : in out <Typ>;
3276 -- S : Integer;
3277 -- P : System.Address;
3278 -- D : Duration;
3279 -- M : Integer;
3280 -- C : out Ada.Tags.Prim_Op_Kind;
3281 -- F : out Boolean)
3282 -- is
3283 -- begin
3284 -- F := False;
3285 -- C := Ada.Tags.POK_Function;
3286 -- end _Disp_Timed_Select;
3288 -- For protected types, generate:
3290 -- procedure _Disp_Timed_Select
3291 -- (T : in out <Typ>;
3292 -- S : Integer;
3293 -- P : System.Address;
3294 -- D : Duration;
3295 -- M : Integer;
3296 -- C : out Ada.Tags.Prim_Op_Kind;
3297 -- F : out Boolean)
3298 -- is
3299 -- I : Integer;
3301 -- begin
3302 -- C := Ada.Tags.Get_Prim_Op_Kind (Ada.Tags.Tag (<Typ>VP), S);
3304 -- if C = Ada.Tags.POK_Procedure
3305 -- or else C = Ada.Tags.POK_Protected_Procedure
3306 -- or else C = Ada.Tags.POK_Task_Procedure
3307 -- then
3308 -- F := True;
3309 -- return;
3310 -- end if;
3312 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP), S);
3313 -- System.Tasking.Protected_Objects.Operations.
3314 -- Timed_Protected_Entry_Call
3315 -- (T._object'Access,
3316 -- System.Tasking.Protected_Objects.Protected_Entry_Index (I),
3317 -- P,
3318 -- D,
3319 -- M,
3320 -- F);
3321 -- end _Disp_Timed_Select;
3323 -- For task types, generate:
3325 -- procedure _Disp_Timed_Select
3326 -- (T : in out <Typ>;
3327 -- S : Integer;
3328 -- P : System.Address;
3329 -- D : Duration;
3330 -- M : Integer;
3331 -- C : out Ada.Tags.Prim_Op_Kind;
3332 -- F : out Boolean)
3333 -- is
3334 -- I : Integer;
3336 -- begin
3337 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP), S);
3338 -- System.Tasking.Rendezvous.Timed_Task_Entry_Call
3339 -- (T._task_id,
3340 -- System.Tasking.Task_Entry_Index (I),
3341 -- P,
3342 -- D,
3343 -- M,
3344 -- F);
3345 -- end _Disp_Time_Select;
3347 function Make_Disp_Timed_Select_Body
3348 (Typ : Entity_Id) return Node_Id
3350 Loc : constant Source_Ptr := Sloc (Typ);
3351 Conc_Typ : Entity_Id := Empty;
3352 Decls : constant List_Id := New_List;
3353 Obj_Ref : Node_Id;
3354 Stmts : constant List_Id := New_List;
3355 Tag_Node : Node_Id;
3357 begin
3358 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3360 -- Null body is generated for interface types
3362 if Is_Interface (Typ) then
3363 return
3364 Make_Subprogram_Body (Loc,
3365 Specification => Make_Disp_Timed_Select_Spec (Typ),
3366 Declarations => New_List,
3367 Handled_Statement_Sequence =>
3368 Make_Handled_Sequence_Of_Statements (Loc,
3369 New_List (
3370 Make_Assignment_Statement (Loc,
3371 Name => Make_Identifier (Loc, Name_uF),
3372 Expression => New_Occurrence_Of (Standard_False, Loc)))));
3373 end if;
3375 if Is_Concurrent_Record_Type (Typ) then
3376 Conc_Typ := Corresponding_Concurrent_Type (Typ);
3378 -- Generate:
3379 -- I : Integer;
3381 -- where I will be used to capture the entry index of the primitive
3382 -- wrapper at position S.
3384 Append_To (Decls,
3385 Make_Object_Declaration (Loc,
3386 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uI),
3387 Object_Definition =>
3388 New_Occurrence_Of (Standard_Integer, Loc)));
3390 -- Generate:
3391 -- C := Get_Prim_Op_Kind (tag! (<type>VP), S);
3393 -- if C = POK_Procedure
3394 -- or else C = POK_Protected_Procedure
3395 -- or else C = POK_Task_Procedure;
3396 -- then
3397 -- F := True;
3398 -- return;
3399 -- end if;
3401 Build_Common_Dispatching_Select_Statements (Typ, Stmts);
3403 -- Generate:
3404 -- I := Get_Entry_Index (tag! (<type>VP), S);
3406 -- I is the entry index and S is the dispatch table slot
3408 if Tagged_Type_Expansion then
3409 Tag_Node :=
3410 Unchecked_Convert_To (RTE (RE_Tag),
3411 New_Occurrence_Of
3412 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc));
3414 else
3415 Tag_Node :=
3416 Make_Attribute_Reference (Loc,
3417 Prefix => New_Occurrence_Of (Typ, Loc),
3418 Attribute_Name => Name_Tag);
3419 end if;
3421 Append_To (Stmts,
3422 Make_Assignment_Statement (Loc,
3423 Name => Make_Identifier (Loc, Name_uI),
3424 Expression =>
3425 Make_Function_Call (Loc,
3426 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
3427 Parameter_Associations => New_List (
3428 Tag_Node,
3429 Make_Identifier (Loc, Name_uS)))));
3431 -- Protected case
3433 if Ekind (Conc_Typ) = E_Protected_Type then
3435 -- Build T._object'Access
3437 Obj_Ref :=
3438 Make_Attribute_Reference (Loc,
3439 Attribute_Name => Name_Unchecked_Access,
3440 Prefix =>
3441 Make_Selected_Component (Loc,
3442 Prefix => Make_Identifier (Loc, Name_uT),
3443 Selector_Name => Make_Identifier (Loc, Name_uObject)));
3445 -- Normal case, No_Entry_Queue restriction not active. In this
3446 -- case we generate:
3448 -- Timed_Protected_Entry_Call
3449 -- (T._object'access,
3450 -- Protected_Entry_Index! (I),
3451 -- P, D, M, F);
3453 -- where T is the protected object, I is the entry index, P are
3454 -- the wrapped parameters, D is the delay amount, M is the delay
3455 -- mode and F is the status flag.
3457 -- Historically, there was also an implementation for single
3458 -- entry protected types (in s-tposen). However, it was removed
3459 -- by also testing for no No_Select_Statements restriction in
3460 -- Exp_Utils.Corresponding_Runtime_Package. This simplified the
3461 -- implementation of s-tposen.adb and provided consistency between
3462 -- all versions of System.Tasking.Protected_Objects.Single_Entry
3463 -- (s-tposen*.adb).
3465 case Corresponding_Runtime_Package (Conc_Typ) is
3466 when System_Tasking_Protected_Objects_Entries =>
3467 Append_To (Stmts,
3468 Make_Procedure_Call_Statement (Loc,
3469 Name =>
3470 New_Occurrence_Of
3471 (RTE (RE_Timed_Protected_Entry_Call), Loc),
3472 Parameter_Associations => New_List (
3473 Obj_Ref,
3475 Make_Unchecked_Type_Conversion (Loc, -- entry index
3476 Subtype_Mark =>
3477 New_Occurrence_Of
3478 (RTE (RE_Protected_Entry_Index), Loc),
3479 Expression => Make_Identifier (Loc, Name_uI)),
3481 Make_Identifier (Loc, Name_uP), -- parameter block
3482 Make_Identifier (Loc, Name_uD), -- delay
3483 Make_Identifier (Loc, Name_uM), -- delay mode
3484 Make_Identifier (Loc, Name_uF)))); -- status flag
3486 when others =>
3487 raise Program_Error;
3488 end case;
3490 -- Task case
3492 else
3493 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
3495 -- Generate:
3496 -- Timed_Task_Entry_Call (
3497 -- T._task_id,
3498 -- Task_Entry_Index! (I),
3499 -- P,
3500 -- D,
3501 -- M,
3502 -- F);
3504 -- where T is the task object, I is the entry index, P are the
3505 -- wrapped parameters, D is the delay amount, M is the delay
3506 -- mode and F is the status flag.
3508 Append_To (Stmts,
3509 Make_Procedure_Call_Statement (Loc,
3510 Name =>
3511 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
3513 Parameter_Associations => New_List (
3514 Make_Selected_Component (Loc, -- T._task_id
3515 Prefix => Make_Identifier (Loc, Name_uT),
3516 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3518 Make_Unchecked_Type_Conversion (Loc, -- entry index
3519 Subtype_Mark =>
3520 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
3521 Expression => Make_Identifier (Loc, Name_uI)),
3523 Make_Identifier (Loc, Name_uP), -- parameter block
3524 Make_Identifier (Loc, Name_uD), -- delay
3525 Make_Identifier (Loc, Name_uM), -- delay mode
3526 Make_Identifier (Loc, Name_uF)))); -- status flag
3527 end if;
3529 else
3530 -- Initialize out parameters
3532 Append_To (Stmts,
3533 Make_Assignment_Statement (Loc,
3534 Name => Make_Identifier (Loc, Name_uF),
3535 Expression => New_Occurrence_Of (Standard_False, Loc)));
3536 Append_To (Stmts,
3537 Make_Assignment_Statement (Loc,
3538 Name => Make_Identifier (Loc, Name_uC),
3539 Expression => New_Occurrence_Of (RTE (RE_POK_Function), Loc)));
3540 end if;
3542 return
3543 Make_Subprogram_Body (Loc,
3544 Specification => Make_Disp_Timed_Select_Spec (Typ),
3545 Declarations => Decls,
3546 Handled_Statement_Sequence =>
3547 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
3548 end Make_Disp_Timed_Select_Body;
3550 ---------------------------------
3551 -- Make_Disp_Timed_Select_Spec --
3552 ---------------------------------
3554 function Make_Disp_Timed_Select_Spec
3555 (Typ : Entity_Id) return Node_Id
3557 Loc : constant Source_Ptr := Sloc (Typ);
3558 Def_Id : constant Node_Id :=
3559 Make_Defining_Identifier (Loc,
3560 Name_uDisp_Timed_Select);
3561 Params : constant List_Id := New_List;
3563 begin
3564 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3566 -- T : in out Typ; -- Object parameter
3567 -- S : Integer; -- Primitive operation slot
3568 -- P : Address; -- Wrapped parameters
3569 -- D : Duration; -- Delay
3570 -- M : Integer; -- Delay Mode
3571 -- C : out Prim_Op_Kind; -- Call kind
3572 -- F : out Boolean; -- Status flag
3574 Append_List_To (Params, New_List (
3576 Make_Parameter_Specification (Loc,
3577 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
3578 Parameter_Type => New_Occurrence_Of (Typ, Loc),
3579 In_Present => True,
3580 Out_Present => True),
3582 Make_Parameter_Specification (Loc,
3583 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uS),
3584 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
3586 Make_Parameter_Specification (Loc,
3587 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
3588 Parameter_Type => New_Occurrence_Of (RTE (RE_Address), Loc)),
3590 Make_Parameter_Specification (Loc,
3591 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uD),
3592 Parameter_Type => New_Occurrence_Of (Standard_Duration, Loc)),
3594 Make_Parameter_Specification (Loc,
3595 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uM),
3596 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
3598 Make_Parameter_Specification (Loc,
3599 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uC),
3600 Parameter_Type =>
3601 New_Occurrence_Of (RTE (RE_Prim_Op_Kind), Loc),
3602 Out_Present => True)));
3604 Append_To (Params,
3605 Make_Parameter_Specification (Loc,
3606 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uF),
3607 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
3608 Out_Present => True));
3610 return
3611 Make_Procedure_Specification (Loc,
3612 Defining_Unit_Name => Def_Id,
3613 Parameter_Specifications => Params);
3614 end Make_Disp_Timed_Select_Spec;
3616 -------------
3617 -- Make_DT --
3618 -------------
3620 -- The frontend supports two models for expanding dispatch tables
3621 -- associated with library-level defined tagged types: statically and
3622 -- non-statically allocated dispatch tables. In the former case the object
3623 -- containing the dispatch table is constant and it is initialized by means
3624 -- of a positional aggregate. In the latter case, the object containing
3625 -- the dispatch table is a variable which is initialized by means of
3626 -- assignments.
3628 -- In case of locally defined tagged types, the object containing the
3629 -- object containing the dispatch table is always a variable (instead of a
3630 -- constant). This is currently required to give support to late overriding
3631 -- of primitives. For example:
3633 -- procedure Example is
3634 -- package Pkg is
3635 -- type T1 is tagged null record;
3636 -- procedure Prim (O : T1);
3637 -- end Pkg;
3639 -- type T2 is new Pkg.T1 with null record;
3640 -- procedure Prim (X : T2) is -- late overriding
3641 -- begin
3642 -- ...
3643 -- ...
3644 -- end;
3646 function Make_DT (Typ : Entity_Id; N : Node_Id := Empty) return List_Id is
3647 Loc : constant Source_Ptr := Sloc (Typ);
3649 Max_Predef_Prims : constant Int :=
3650 UI_To_Int
3651 (Intval
3652 (Expression
3653 (Parent (RTE (RE_Max_Predef_Prims)))));
3655 DT_Decl : constant Elist_Id := New_Elmt_List;
3656 DT_Aggr : constant Elist_Id := New_Elmt_List;
3657 -- Entities marked with attribute Is_Dispatch_Table_Entity
3659 procedure Check_Premature_Freezing
3660 (Subp : Entity_Id;
3661 Tagged_Type : Entity_Id;
3662 Typ : Entity_Id);
3663 -- Verify that all untagged types in the profile of a subprogram are
3664 -- frozen at the point the subprogram is frozen. This enforces the rule
3665 -- on RM 13.14 (14) as modified by AI05-019. At the point a subprogram
3666 -- is frozen, enough must be known about it to build the activation
3667 -- record for it, which requires at least that the size of all
3668 -- parameters be known. Controlling arguments are by-reference,
3669 -- and therefore the rule only applies to untagged types. Typical
3670 -- violation of the rule involves an object declaration that freezes a
3671 -- tagged type, when one of its primitive operations has a type in its
3672 -- profile whose full view has not been analyzed yet. More complex cases
3673 -- involve composite types that have one private unfrozen subcomponent.
3675 procedure Export_DT (Typ : Entity_Id; DT : Entity_Id; Index : Nat := 0);
3676 -- Export the dispatch table DT of tagged type Typ. Required to generate
3677 -- forward references and statically allocate the table. For primary
3678 -- dispatch tables Index is 0; for secondary dispatch tables the value
3679 -- of index must match the Suffix_Index value assigned to the table by
3680 -- Make_Tags when generating its unique external name, and it is used to
3681 -- retrieve from the Dispatch_Table_Wrappers list associated with Typ
3682 -- the external name generated by Import_DT.
3684 procedure Make_Secondary_DT
3685 (Typ : Entity_Id;
3686 Iface : Entity_Id;
3687 Suffix_Index : Int;
3688 Num_Iface_Prims : Nat;
3689 Iface_DT_Ptr : Entity_Id;
3690 Predef_Prims_Ptr : Entity_Id;
3691 Build_Thunks : Boolean;
3692 Result : List_Id);
3693 -- Ada 2005 (AI-251): Expand the declarations for a Secondary Dispatch
3694 -- Table of Typ associated with Iface. Each abstract interface of Typ
3695 -- has two secondary dispatch tables: one containing pointers to thunks
3696 -- and another containing pointers to the primitives covering the
3697 -- interface primitives. The former secondary table is generated when
3698 -- Build_Thunks is True, and provides common support for dispatching
3699 -- calls through interface types; the latter secondary table is
3700 -- generated when Build_Thunks is False, and provides support for
3701 -- Generic Dispatching Constructors that dispatch calls through
3702 -- interface types. When constructing this latter table the value of
3703 -- Suffix_Index is -1 to indicate that there is no need to export such
3704 -- table when building statically allocated dispatch tables; a positive
3705 -- value of Suffix_Index must match the Suffix_Index value assigned to
3706 -- this secondary dispatch table by Make_Tags when its unique external
3707 -- name was generated.
3709 ------------------------------
3710 -- Check_Premature_Freezing --
3711 ------------------------------
3713 procedure Check_Premature_Freezing
3714 (Subp : Entity_Id;
3715 Tagged_Type : Entity_Id;
3716 Typ : Entity_Id)
3718 Comp : Entity_Id;
3720 function Is_Actual_For_Formal_Incomplete_Type
3721 (T : Entity_Id) return Boolean;
3722 -- In Ada 2012, if a nested generic has an incomplete formal type,
3723 -- the actual may be (and usually is) a private type whose completion
3724 -- appears later. It is safe to build the dispatch table in this
3725 -- case, gigi will have full views available.
3727 ------------------------------------------
3728 -- Is_Actual_For_Formal_Incomplete_Type --
3729 ------------------------------------------
3731 function Is_Actual_For_Formal_Incomplete_Type
3732 (T : Entity_Id) return Boolean
3734 Gen_Par : Entity_Id;
3735 F : Node_Id;
3737 begin
3738 if not Is_Generic_Instance (Current_Scope)
3739 or else not Used_As_Generic_Actual (T)
3740 then
3741 return False;
3742 else
3743 Gen_Par := Generic_Parent (Parent (Current_Scope));
3744 end if;
3746 F :=
3747 First
3748 (Generic_Formal_Declarations
3749 (Unit_Declaration_Node (Gen_Par)));
3750 while Present (F) loop
3751 if Ekind (Defining_Identifier (F)) = E_Incomplete_Type then
3752 return True;
3753 end if;
3755 Next (F);
3756 end loop;
3758 return False;
3759 end Is_Actual_For_Formal_Incomplete_Type;
3761 -- Start of processing for Check_Premature_Freezing
3763 begin
3764 -- Note that if the type is a (subtype of) a generic actual, the
3765 -- actual will have been frozen by the instantiation.
3767 if Present (N)
3768 and then Is_Private_Type (Typ)
3769 and then No (Full_View (Typ))
3770 and then not Is_Generic_Type (Typ)
3771 and then not Is_Tagged_Type (Typ)
3772 and then not Is_Frozen (Typ)
3773 and then not Is_Generic_Actual_Type (Typ)
3774 then
3775 Error_Msg_Sloc := Sloc (Subp);
3776 Error_Msg_NE
3777 ("declaration must appear after completion of type &", N, Typ);
3778 Error_Msg_NE
3779 ("\which is an untagged type in the profile of "
3780 & "primitive operation & declared#", N, Subp);
3782 else
3783 Comp := Private_Component (Typ);
3785 if not Is_Tagged_Type (Typ)
3786 and then Present (Comp)
3787 and then not Is_Frozen (Comp)
3788 and then not Is_Actual_For_Formal_Incomplete_Type (Comp)
3789 then
3790 Error_Msg_Sloc := Sloc (Subp);
3791 Error_Msg_Node_2 := Subp;
3792 Error_Msg_Name_1 := Chars (Tagged_Type);
3793 Error_Msg_NE
3794 ("declaration must appear after completion of type &",
3795 N, Comp);
3796 Error_Msg_NE
3797 ("\which is a component of untagged type& in the profile "
3798 & "of primitive & of type % that is frozen by the "
3799 & "declaration ", N, Typ);
3800 end if;
3801 end if;
3802 end Check_Premature_Freezing;
3804 ---------------
3805 -- Export_DT --
3806 ---------------
3808 procedure Export_DT (Typ : Entity_Id; DT : Entity_Id; Index : Nat := 0)
3810 Count : Nat;
3811 Elmt : Elmt_Id;
3813 begin
3814 Set_Is_Statically_Allocated (DT);
3815 Set_Is_True_Constant (DT);
3816 Set_Is_Exported (DT);
3818 Count := 0;
3819 Elmt := First_Elmt (Dispatch_Table_Wrappers (Typ));
3820 while Count /= Index loop
3821 Next_Elmt (Elmt);
3822 Count := Count + 1;
3823 end loop;
3825 pragma Assert (Related_Type (Node (Elmt)) = Typ);
3827 Get_External_Name (Node (Elmt));
3828 Set_Interface_Name (DT,
3829 Make_String_Literal (Loc,
3830 Strval => String_From_Name_Buffer));
3832 -- Ensure proper Sprint output of this implicit importation
3834 Set_Is_Internal (DT);
3835 Set_Is_Public (DT);
3836 end Export_DT;
3838 -----------------------
3839 -- Make_Secondary_DT --
3840 -----------------------
3842 procedure Make_Secondary_DT
3843 (Typ : Entity_Id;
3844 Iface : Entity_Id;
3845 Suffix_Index : Int;
3846 Num_Iface_Prims : Nat;
3847 Iface_DT_Ptr : Entity_Id;
3848 Predef_Prims_Ptr : Entity_Id;
3849 Build_Thunks : Boolean;
3850 Result : List_Id)
3852 Loc : constant Source_Ptr := Sloc (Typ);
3853 Exporting_Table : constant Boolean :=
3854 Building_Static_DT (Typ)
3855 and then Suffix_Index > 0;
3856 Iface_DT : constant Entity_Id := Make_Temporary (Loc, 'T');
3857 Predef_Prims : constant Entity_Id := Make_Temporary (Loc, 'R');
3858 DT_Constr_List : List_Id;
3859 DT_Aggr_List : List_Id;
3860 Empty_DT : Boolean := False;
3861 Nb_Predef_Prims : Nat := 0;
3862 Nb_Prim : Nat;
3863 New_Node : Node_Id;
3864 OSD : Entity_Id;
3865 OSD_Aggr_List : List_Id;
3866 Pos : Nat;
3867 Prim : Entity_Id;
3868 Prim_Elmt : Elmt_Id;
3869 Prim_Ops_Aggr_List : List_Id;
3871 begin
3872 -- Handle cases in which we do not generate statically allocated
3873 -- dispatch tables.
3875 if not Building_Static_DT (Typ) then
3876 Set_Ekind (Predef_Prims, E_Variable);
3877 Set_Ekind (Iface_DT, E_Variable);
3879 -- Statically allocated dispatch tables and related entities are
3880 -- constants.
3882 else
3883 Set_Ekind (Predef_Prims, E_Constant);
3884 Set_Is_Statically_Allocated (Predef_Prims);
3885 Set_Is_True_Constant (Predef_Prims);
3887 Set_Ekind (Iface_DT, E_Constant);
3888 Set_Is_Statically_Allocated (Iface_DT);
3889 Set_Is_True_Constant (Iface_DT);
3890 end if;
3892 -- Calculate the number of slots of the dispatch table. If the number
3893 -- of primitives of Typ is 0 we reserve a dummy single entry for its
3894 -- DT because at run time the pointer to this dummy entry will be
3895 -- used as the tag.
3897 if Num_Iface_Prims = 0 then
3898 Empty_DT := True;
3899 Nb_Prim := 1;
3900 else
3901 Nb_Prim := Num_Iface_Prims;
3902 end if;
3904 -- Generate:
3906 -- Predef_Prims : Address_Array (1 .. Default_Prim_Ops_Count) :=
3907 -- (predef-prim-op-thunk-1'address,
3908 -- predef-prim-op-thunk-2'address,
3909 -- ...
3910 -- predef-prim-op-thunk-n'address);
3911 -- for Predef_Prims'Alignment use Address'Alignment
3913 -- Stage 1: Calculate the number of predefined primitives
3915 if not Building_Static_DT (Typ) then
3916 Nb_Predef_Prims := Max_Predef_Prims;
3917 else
3918 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
3919 while Present (Prim_Elmt) loop
3920 Prim := Node (Prim_Elmt);
3922 if Is_Predefined_Dispatching_Operation (Prim)
3923 and then not Is_Abstract_Subprogram (Prim)
3924 then
3925 Pos := UI_To_Int (DT_Position (Prim));
3927 if Pos > Nb_Predef_Prims then
3928 Nb_Predef_Prims := Pos;
3929 end if;
3930 end if;
3932 Next_Elmt (Prim_Elmt);
3933 end loop;
3934 end if;
3936 -- Stage 2: Create the thunks associated with the predefined
3937 -- primitives and save their entity to fill the aggregate.
3939 declare
3940 Prim_Table : array (Nat range 1 .. Nb_Predef_Prims) of Entity_Id;
3941 Decl : Node_Id;
3942 Thunk_Id : Entity_Id;
3943 Thunk_Code : Node_Id;
3945 begin
3946 Prim_Ops_Aggr_List := New_List;
3947 Prim_Table := (others => Empty);
3949 if Building_Static_DT (Typ) then
3950 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
3951 while Present (Prim_Elmt) loop
3952 Prim := Node (Prim_Elmt);
3954 if Is_Predefined_Dispatching_Operation (Prim)
3955 and then not Is_Abstract_Subprogram (Prim)
3956 and then not Is_Eliminated (Prim)
3957 and then not Present (Prim_Table
3958 (UI_To_Int (DT_Position (Prim))))
3959 then
3960 if not Build_Thunks then
3961 Prim_Table (UI_To_Int (DT_Position (Prim))) :=
3962 Alias (Prim);
3964 else
3965 Expand_Interface_Thunk
3966 (Ultimate_Alias (Prim), Thunk_Id, Thunk_Code);
3968 if Present (Thunk_Id) then
3969 Append_To (Result, Thunk_Code);
3970 Prim_Table (UI_To_Int (DT_Position (Prim)))
3971 := Thunk_Id;
3972 end if;
3973 end if;
3974 end if;
3976 Next_Elmt (Prim_Elmt);
3977 end loop;
3978 end if;
3980 for J in Prim_Table'Range loop
3981 if Present (Prim_Table (J)) then
3982 New_Node :=
3983 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
3984 Make_Attribute_Reference (Loc,
3985 Prefix => New_Occurrence_Of (Prim_Table (J), Loc),
3986 Attribute_Name => Name_Unrestricted_Access));
3987 else
3988 New_Node := Make_Null (Loc);
3989 end if;
3991 Append_To (Prim_Ops_Aggr_List, New_Node);
3992 end loop;
3994 New_Node :=
3995 Make_Aggregate (Loc, Expressions => Prim_Ops_Aggr_List);
3997 -- Remember aggregates initializing dispatch tables
3999 Append_Elmt (New_Node, DT_Aggr);
4001 Decl :=
4002 Make_Subtype_Declaration (Loc,
4003 Defining_Identifier => Make_Temporary (Loc, 'S'),
4004 Subtype_Indication =>
4005 New_Occurrence_Of (RTE (RE_Address_Array), Loc));
4007 Append_To (Result, Decl);
4009 Append_To (Result,
4010 Make_Object_Declaration (Loc,
4011 Defining_Identifier => Predef_Prims,
4012 Constant_Present => Building_Static_DT (Typ),
4013 Aliased_Present => True,
4014 Object_Definition => New_Occurrence_Of
4015 (Defining_Identifier (Decl), Loc),
4016 Expression => New_Node));
4018 Append_To (Result,
4019 Make_Attribute_Definition_Clause (Loc,
4020 Name => New_Occurrence_Of (Predef_Prims, Loc),
4021 Chars => Name_Alignment,
4022 Expression =>
4023 Make_Attribute_Reference (Loc,
4024 Prefix =>
4025 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
4026 Attribute_Name => Name_Alignment)));
4027 end;
4029 -- Generate
4031 -- OSD : Ada.Tags.Object_Specific_Data (Nb_Prims) :=
4032 -- (OSD_Table => (1 => <value>,
4033 -- ...
4034 -- N => <value>));
4036 -- Iface_DT : Dispatch_Table (Nb_Prims) :=
4037 -- ([ Signature => <sig-value> ],
4038 -- Tag_Kind => <tag_kind-value>,
4039 -- Predef_Prims => Predef_Prims'Address,
4040 -- Offset_To_Top => 0,
4041 -- OSD => OSD'Address,
4042 -- Prims_Ptr => (prim-op-1'address,
4043 -- prim-op-2'address,
4044 -- ...
4045 -- prim-op-n'address));
4046 -- for Iface_DT'Alignment use Address'Alignment;
4048 -- Stage 3: Initialize the discriminant and the record components
4050 DT_Constr_List := New_List;
4051 DT_Aggr_List := New_List;
4053 -- Nb_Prim
4055 Append_To (DT_Constr_List, Make_Integer_Literal (Loc, Nb_Prim));
4056 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, Nb_Prim));
4058 -- Signature
4060 if RTE_Record_Component_Available (RE_Signature) then
4061 Append_To (DT_Aggr_List,
4062 New_Occurrence_Of (RTE (RE_Secondary_DT), Loc));
4063 end if;
4065 -- Tag_Kind
4067 if RTE_Record_Component_Available (RE_Tag_Kind) then
4068 Append_To (DT_Aggr_List, Tagged_Kind (Typ));
4069 end if;
4071 -- Predef_Prims
4073 Append_To (DT_Aggr_List,
4074 Make_Attribute_Reference (Loc,
4075 Prefix => New_Occurrence_Of (Predef_Prims, Loc),
4076 Attribute_Name => Name_Address));
4078 -- Note: The correct value of Offset_To_Top will be set by the init
4079 -- subprogram
4081 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, 0));
4083 -- Generate the Object Specific Data table required to dispatch calls
4084 -- through synchronized interfaces.
4086 if Empty_DT
4087 or else Is_Abstract_Type (Typ)
4088 or else Is_Controlled (Typ)
4089 or else Restriction_Active (No_Dispatching_Calls)
4090 or else not Is_Limited_Type (Typ)
4091 or else not Has_Interfaces (Typ)
4092 or else not Build_Thunks
4093 or else not RTE_Record_Component_Available (RE_OSD_Table)
4094 then
4095 -- No OSD table required
4097 Append_To (DT_Aggr_List,
4098 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
4100 else
4101 OSD_Aggr_List := New_List;
4103 declare
4104 Prim_Table : array (Nat range 1 .. Nb_Prim) of Entity_Id;
4105 Prim : Entity_Id;
4106 Prim_Alias : Entity_Id;
4107 Prim_Elmt : Elmt_Id;
4108 E : Entity_Id;
4109 Count : Nat := 0;
4110 Pos : Nat;
4112 begin
4113 Prim_Table := (others => Empty);
4114 Prim_Alias := Empty;
4116 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
4117 while Present (Prim_Elmt) loop
4118 Prim := Node (Prim_Elmt);
4120 if Present (Interface_Alias (Prim))
4121 and then Find_Dispatching_Type
4122 (Interface_Alias (Prim)) = Iface
4123 then
4124 Prim_Alias := Interface_Alias (Prim);
4125 E := Ultimate_Alias (Prim);
4126 Pos := UI_To_Int (DT_Position (Prim_Alias));
4128 if Present (Prim_Table (Pos)) then
4129 pragma Assert (Prim_Table (Pos) = E);
4130 null;
4132 else
4133 Prim_Table (Pos) := E;
4135 Append_To (OSD_Aggr_List,
4136 Make_Component_Association (Loc,
4137 Choices => New_List (
4138 Make_Integer_Literal (Loc,
4139 DT_Position (Prim_Alias))),
4140 Expression =>
4141 Make_Integer_Literal (Loc,
4142 DT_Position (Alias (Prim)))));
4144 Count := Count + 1;
4145 end if;
4146 end if;
4148 Next_Elmt (Prim_Elmt);
4149 end loop;
4150 pragma Assert (Count = Nb_Prim);
4151 end;
4153 OSD := Make_Temporary (Loc, 'I');
4155 Append_To (Result,
4156 Make_Object_Declaration (Loc,
4157 Defining_Identifier => OSD,
4158 Object_Definition =>
4159 Make_Subtype_Indication (Loc,
4160 Subtype_Mark =>
4161 New_Occurrence_Of (RTE (RE_Object_Specific_Data), Loc),
4162 Constraint =>
4163 Make_Index_Or_Discriminant_Constraint (Loc,
4164 Constraints => New_List (
4165 Make_Integer_Literal (Loc, Nb_Prim)))),
4167 Expression =>
4168 Make_Aggregate (Loc,
4169 Component_Associations => New_List (
4170 Make_Component_Association (Loc,
4171 Choices => New_List (
4172 New_Occurrence_Of
4173 (RTE_Record_Component (RE_OSD_Num_Prims), Loc)),
4174 Expression =>
4175 Make_Integer_Literal (Loc, Nb_Prim)),
4177 Make_Component_Association (Loc,
4178 Choices => New_List (
4179 New_Occurrence_Of
4180 (RTE_Record_Component (RE_OSD_Table), Loc)),
4181 Expression => Make_Aggregate (Loc,
4182 Component_Associations => OSD_Aggr_List))))));
4184 Append_To (Result,
4185 Make_Attribute_Definition_Clause (Loc,
4186 Name => New_Occurrence_Of (OSD, Loc),
4187 Chars => Name_Alignment,
4188 Expression =>
4189 Make_Attribute_Reference (Loc,
4190 Prefix =>
4191 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
4192 Attribute_Name => Name_Alignment)));
4194 -- In secondary dispatch tables the Typeinfo component contains
4195 -- the address of the Object Specific Data (see a-tags.ads)
4197 Append_To (DT_Aggr_List,
4198 Make_Attribute_Reference (Loc,
4199 Prefix => New_Occurrence_Of (OSD, Loc),
4200 Attribute_Name => Name_Address));
4201 end if;
4203 -- Initialize the table of primitive operations
4205 Prim_Ops_Aggr_List := New_List;
4207 if Empty_DT then
4208 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
4210 elsif Is_Abstract_Type (Typ)
4211 or else not Building_Static_DT (Typ)
4212 then
4213 for J in 1 .. Nb_Prim loop
4214 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
4215 end loop;
4217 else
4218 declare
4219 CPP_Nb_Prims : constant Nat := CPP_Num_Prims (Typ);
4220 E : Entity_Id;
4221 Prim_Pos : Nat;
4222 Prim_Table : array (Nat range 1 .. Nb_Prim) of Entity_Id;
4223 Thunk_Code : Node_Id;
4224 Thunk_Id : Entity_Id;
4226 begin
4227 Prim_Table := (others => Empty);
4229 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
4230 while Present (Prim_Elmt) loop
4231 Prim := Node (Prim_Elmt);
4232 E := Ultimate_Alias (Prim);
4233 Prim_Pos := UI_To_Int (DT_Position (E));
4235 -- Do not reference predefined primitives because they are
4236 -- located in a separate dispatch table; skip abstract and
4237 -- eliminated primitives; skip primitives located in the C++
4238 -- part of the dispatch table because their slot is set by
4239 -- the IC routine.
4241 if not Is_Predefined_Dispatching_Operation (Prim)
4242 and then Present (Interface_Alias (Prim))
4243 and then not Is_Abstract_Subprogram (Alias (Prim))
4244 and then not Is_Eliminated (Alias (Prim))
4245 and then (not Is_CPP_Class (Root_Type (Typ))
4246 or else Prim_Pos > CPP_Nb_Prims)
4247 and then Find_Dispatching_Type
4248 (Interface_Alias (Prim)) = Iface
4250 -- Generate the code of the thunk only if the abstract
4251 -- interface type is not an immediate ancestor of
4252 -- Tagged_Type. Otherwise the DT associated with the
4253 -- interface is the primary DT.
4255 and then not Is_Ancestor (Iface, Typ,
4256 Use_Full_View => True)
4257 then
4258 if not Build_Thunks then
4259 Prim_Pos :=
4260 UI_To_Int (DT_Position (Interface_Alias (Prim)));
4261 Prim_Table (Prim_Pos) := Alias (Prim);
4263 else
4264 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
4266 if Present (Thunk_Id) then
4267 Prim_Pos :=
4268 UI_To_Int (DT_Position (Interface_Alias (Prim)));
4270 Prim_Table (Prim_Pos) := Thunk_Id;
4271 Append_To (Result, Thunk_Code);
4272 end if;
4273 end if;
4274 end if;
4276 Next_Elmt (Prim_Elmt);
4277 end loop;
4279 for J in Prim_Table'Range loop
4280 if Present (Prim_Table (J)) then
4281 New_Node :=
4282 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
4283 Make_Attribute_Reference (Loc,
4284 Prefix => New_Occurrence_Of (Prim_Table (J), Loc),
4285 Attribute_Name => Name_Unrestricted_Access));
4287 else
4288 New_Node := Make_Null (Loc);
4289 end if;
4291 Append_To (Prim_Ops_Aggr_List, New_Node);
4292 end loop;
4293 end;
4294 end if;
4296 New_Node :=
4297 Make_Aggregate (Loc,
4298 Expressions => Prim_Ops_Aggr_List);
4300 Append_To (DT_Aggr_List, New_Node);
4302 -- Remember aggregates initializing dispatch tables
4304 Append_Elmt (New_Node, DT_Aggr);
4306 -- Note: Secondary dispatch tables cannot be declared constant
4307 -- because the component Offset_To_Top is currently initialized
4308 -- by the IP routine.
4310 Append_To (Result,
4311 Make_Object_Declaration (Loc,
4312 Defining_Identifier => Iface_DT,
4313 Aliased_Present => True,
4314 Constant_Present => False,
4316 Object_Definition =>
4317 Make_Subtype_Indication (Loc,
4318 Subtype_Mark => New_Occurrence_Of
4319 (RTE (RE_Dispatch_Table_Wrapper), Loc),
4320 Constraint => Make_Index_Or_Discriminant_Constraint (Loc,
4321 Constraints => DT_Constr_List)),
4323 Expression =>
4324 Make_Aggregate (Loc,
4325 Expressions => DT_Aggr_List)));
4327 Append_To (Result,
4328 Make_Attribute_Definition_Clause (Loc,
4329 Name => New_Occurrence_Of (Iface_DT, Loc),
4330 Chars => Name_Alignment,
4332 Expression =>
4333 Make_Attribute_Reference (Loc,
4334 Prefix =>
4335 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
4336 Attribute_Name => Name_Alignment)));
4338 if Exporting_Table then
4339 Export_DT (Typ, Iface_DT, Suffix_Index);
4341 -- Generate code to create the pointer to the dispatch table
4343 -- Iface_DT_Ptr : Tag := Tag!(DT.Prims_Ptr'Address);
4345 -- Note: This declaration is not added here if the table is exported
4346 -- because in such case Make_Tags has already added this declaration.
4348 else
4349 Append_To (Result,
4350 Make_Object_Declaration (Loc,
4351 Defining_Identifier => Iface_DT_Ptr,
4352 Constant_Present => True,
4354 Object_Definition =>
4355 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc),
4357 Expression =>
4358 Unchecked_Convert_To (RTE (RE_Interface_Tag),
4359 Make_Attribute_Reference (Loc,
4360 Prefix =>
4361 Make_Selected_Component (Loc,
4362 Prefix => New_Occurrence_Of (Iface_DT, Loc),
4363 Selector_Name =>
4364 New_Occurrence_Of
4365 (RTE_Record_Component (RE_Prims_Ptr), Loc)),
4366 Attribute_Name => Name_Address))));
4367 end if;
4369 Append_To (Result,
4370 Make_Object_Declaration (Loc,
4371 Defining_Identifier => Predef_Prims_Ptr,
4372 Constant_Present => True,
4374 Object_Definition =>
4375 New_Occurrence_Of (RTE (RE_Address), Loc),
4377 Expression =>
4378 Make_Attribute_Reference (Loc,
4379 Prefix =>
4380 Make_Selected_Component (Loc,
4381 Prefix => New_Occurrence_Of (Iface_DT, Loc),
4382 Selector_Name =>
4383 New_Occurrence_Of
4384 (RTE_Record_Component (RE_Predef_Prims), Loc)),
4385 Attribute_Name => Name_Address)));
4387 -- Remember entities containing dispatch tables
4389 Append_Elmt (Predef_Prims, DT_Decl);
4390 Append_Elmt (Iface_DT, DT_Decl);
4391 end Make_Secondary_DT;
4393 -- Local variables
4395 Elab_Code : constant List_Id := New_List;
4396 Result : constant List_Id := New_List;
4397 Tname : constant Name_Id := Chars (Typ);
4398 AI : Elmt_Id;
4399 AI_Tag_Elmt : Elmt_Id;
4400 AI_Tag_Comp : Elmt_Id;
4401 DT_Aggr_List : List_Id;
4402 DT_Constr_List : List_Id;
4403 DT_Ptr : Entity_Id;
4404 ITable : Node_Id;
4405 I_Depth : Nat := 0;
4406 Iface_Table_Node : Node_Id;
4407 Name_ITable : Name_Id;
4408 Nb_Predef_Prims : Nat := 0;
4409 Nb_Prim : Nat := 0;
4410 New_Node : Node_Id;
4411 Num_Ifaces : Nat := 0;
4412 Parent_Typ : Entity_Id;
4413 Prim : Entity_Id;
4414 Prim_Elmt : Elmt_Id;
4415 Prim_Ops_Aggr_List : List_Id;
4416 Suffix_Index : Int;
4417 Typ_Comps : Elist_Id;
4418 Typ_Ifaces : Elist_Id;
4419 TSD_Aggr_List : List_Id;
4420 TSD_Tags_List : List_Id;
4422 -- The following name entries are used by Make_DT to generate a number
4423 -- of entities related to a tagged type. These entities may be generated
4424 -- in a scope other than that of the tagged type declaration, and if
4425 -- the entities for two tagged types with the same name happen to be
4426 -- generated in the same scope, we have to take care to use different
4427 -- names. This is achieved by means of a unique serial number appended
4428 -- to each generated entity name.
4430 Name_DT : constant Name_Id :=
4431 New_External_Name (Tname, 'T', Suffix_Index => -1);
4432 Name_Exname : constant Name_Id :=
4433 New_External_Name (Tname, 'E', Suffix_Index => -1);
4434 Name_HT_Link : constant Name_Id :=
4435 New_External_Name (Tname, 'H', Suffix_Index => -1);
4436 Name_Predef_Prims : constant Name_Id :=
4437 New_External_Name (Tname, 'R', Suffix_Index => -1);
4438 Name_SSD : constant Name_Id :=
4439 New_External_Name (Tname, 'S', Suffix_Index => -1);
4440 Name_TSD : constant Name_Id :=
4441 New_External_Name (Tname, 'B', Suffix_Index => -1);
4443 -- Entities built with above names
4445 DT : constant Entity_Id :=
4446 Make_Defining_Identifier (Loc, Name_DT);
4447 Exname : constant Entity_Id :=
4448 Make_Defining_Identifier (Loc, Name_Exname);
4449 HT_Link : constant Entity_Id :=
4450 Make_Defining_Identifier (Loc, Name_HT_Link);
4451 Predef_Prims : constant Entity_Id :=
4452 Make_Defining_Identifier (Loc, Name_Predef_Prims);
4453 SSD : constant Entity_Id :=
4454 Make_Defining_Identifier (Loc, Name_SSD);
4455 TSD : constant Entity_Id :=
4456 Make_Defining_Identifier (Loc, Name_TSD);
4458 -- Start of processing for Make_DT
4460 begin
4461 pragma Assert (Is_Frozen (Typ));
4463 -- Handle cases in which there is no need to build the dispatch table
4465 if Has_Dispatch_Table (Typ)
4466 or else No (Access_Disp_Table (Typ))
4467 or else Is_CPP_Class (Typ)
4468 or else Convention (Typ) = Convention_CIL
4469 or else Convention (Typ) = Convention_Java
4470 then
4471 return Result;
4473 elsif No_Run_Time_Mode then
4474 Error_Msg_CRT ("tagged types", Typ);
4475 return Result;
4477 elsif not RTE_Available (RE_Tag) then
4478 Append_To (Result,
4479 Make_Object_Declaration (Loc,
4480 Defining_Identifier => Node (First_Elmt
4481 (Access_Disp_Table (Typ))),
4482 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
4483 Constant_Present => True,
4484 Expression =>
4485 Unchecked_Convert_To (RTE (RE_Tag),
4486 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
4488 Analyze_List (Result, Suppress => All_Checks);
4489 Error_Msg_CRT ("tagged types", Typ);
4490 return Result;
4491 end if;
4493 -- Ensure that the value of Max_Predef_Prims defined in a-tags is
4494 -- correct. Valid values are 9 under configurable runtime or 15
4495 -- with full runtime.
4497 if RTE_Available (RE_Interface_Data) then
4498 if Max_Predef_Prims /= 15 then
4499 Error_Msg_N ("run-time library configuration error", Typ);
4500 return Result;
4501 end if;
4502 else
4503 if Max_Predef_Prims /= 9 then
4504 Error_Msg_N ("run-time library configuration error", Typ);
4505 Error_Msg_CRT ("tagged types", Typ);
4506 return Result;
4507 end if;
4508 end if;
4510 -- Initialize Parent_Typ handling private types
4512 Parent_Typ := Etype (Typ);
4514 if Present (Full_View (Parent_Typ)) then
4515 Parent_Typ := Full_View (Parent_Typ);
4516 end if;
4518 -- Ensure that all the primitives are frozen. This is only required when
4519 -- building static dispatch tables --- the primitives must be frozen to
4520 -- be referenced (otherwise we have problems with the backend). It is
4521 -- not a requirement with nonstatic dispatch tables because in this case
4522 -- we generate now an empty dispatch table; the extra code required to
4523 -- register the primitives in the slots will be generated later --- when
4524 -- each primitive is frozen (see Freeze_Subprogram).
4526 if Building_Static_DT (Typ) then
4527 declare
4528 Save : constant Boolean := Freezing_Library_Level_Tagged_Type;
4529 Prim : Entity_Id;
4530 Prim_Elmt : Elmt_Id;
4531 Frnodes : List_Id;
4533 begin
4534 Freezing_Library_Level_Tagged_Type := True;
4536 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
4537 while Present (Prim_Elmt) loop
4538 Prim := Node (Prim_Elmt);
4539 Frnodes := Freeze_Entity (Prim, Typ);
4541 declare
4542 F : Entity_Id;
4544 begin
4545 F := First_Formal (Prim);
4546 while Present (F) loop
4547 Check_Premature_Freezing (Prim, Typ, Etype (F));
4548 Next_Formal (F);
4549 end loop;
4551 Check_Premature_Freezing (Prim, Typ, Etype (Prim));
4552 end;
4554 if Present (Frnodes) then
4555 Append_List_To (Result, Frnodes);
4556 end if;
4558 Next_Elmt (Prim_Elmt);
4559 end loop;
4561 Freezing_Library_Level_Tagged_Type := Save;
4562 end;
4563 end if;
4565 -- Ada 2005 (AI-251): Build the secondary dispatch tables
4567 if Has_Interfaces (Typ) then
4568 Collect_Interface_Components (Typ, Typ_Comps);
4570 -- Each secondary dispatch table is assigned an unique positive
4571 -- suffix index; such value also corresponds with the location of
4572 -- its entity in the Dispatch_Table_Wrappers list (see Make_Tags).
4574 -- Note: This value must be kept sync with the Suffix_Index values
4575 -- generated by Make_Tags
4577 Suffix_Index := 1;
4578 AI_Tag_Elmt :=
4579 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
4581 AI_Tag_Comp := First_Elmt (Typ_Comps);
4582 while Present (AI_Tag_Comp) loop
4583 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'P'));
4585 -- Build the secondary table containing pointers to thunks
4587 Make_Secondary_DT
4588 (Typ => Typ,
4589 Iface => Base_Type
4590 (Related_Type (Node (AI_Tag_Comp))),
4591 Suffix_Index => Suffix_Index,
4592 Num_Iface_Prims => UI_To_Int
4593 (DT_Entry_Count (Node (AI_Tag_Comp))),
4594 Iface_DT_Ptr => Node (AI_Tag_Elmt),
4595 Predef_Prims_Ptr => Node (Next_Elmt (AI_Tag_Elmt)),
4596 Build_Thunks => True,
4597 Result => Result);
4599 -- Skip secondary dispatch table referencing thunks to predefined
4600 -- primitives.
4602 Next_Elmt (AI_Tag_Elmt);
4603 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'Y'));
4605 -- Secondary dispatch table referencing user-defined primitives
4606 -- covered by this interface.
4608 Next_Elmt (AI_Tag_Elmt);
4609 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'D'));
4611 -- Build the secondary table containing pointers to primitives
4612 -- (used to give support to Generic Dispatching Constructors).
4614 Make_Secondary_DT
4615 (Typ => Typ,
4616 Iface => Base_Type
4617 (Related_Type (Node (AI_Tag_Comp))),
4618 Suffix_Index => -1,
4619 Num_Iface_Prims => UI_To_Int
4620 (DT_Entry_Count (Node (AI_Tag_Comp))),
4621 Iface_DT_Ptr => Node (AI_Tag_Elmt),
4622 Predef_Prims_Ptr => Node (Next_Elmt (AI_Tag_Elmt)),
4623 Build_Thunks => False,
4624 Result => Result);
4626 -- Skip secondary dispatch table referencing predefined primitives
4628 Next_Elmt (AI_Tag_Elmt);
4629 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'Z'));
4631 Suffix_Index := Suffix_Index + 1;
4632 Next_Elmt (AI_Tag_Elmt);
4633 Next_Elmt (AI_Tag_Comp);
4634 end loop;
4635 end if;
4637 -- Get the _tag entity and number of primitives of its dispatch table
4639 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Typ)));
4640 Nb_Prim := UI_To_Int (DT_Entry_Count (First_Tag_Component (Typ)));
4642 Set_Is_Statically_Allocated (DT, Is_Library_Level_Tagged_Type (Typ));
4643 Set_Is_Statically_Allocated (SSD, Is_Library_Level_Tagged_Type (Typ));
4644 Set_Is_Statically_Allocated (TSD, Is_Library_Level_Tagged_Type (Typ));
4645 Set_Is_Statically_Allocated (Predef_Prims,
4646 Is_Library_Level_Tagged_Type (Typ));
4648 -- In case of locally defined tagged type we declare the object
4649 -- containing the dispatch table by means of a variable. Its
4650 -- initialization is done later by means of an assignment. This is
4651 -- required to generate its External_Tag.
4653 if not Building_Static_DT (Typ) then
4655 -- Generate:
4656 -- DT : No_Dispatch_Table_Wrapper;
4657 -- for DT'Alignment use Address'Alignment;
4658 -- DT_Ptr : Tag := !Tag (DT.NDT_Prims_Ptr'Address);
4660 if not Has_DT (Typ) then
4661 Append_To (Result,
4662 Make_Object_Declaration (Loc,
4663 Defining_Identifier => DT,
4664 Aliased_Present => True,
4665 Constant_Present => False,
4666 Object_Definition =>
4667 New_Occurrence_Of
4668 (RTE (RE_No_Dispatch_Table_Wrapper), Loc)));
4670 Append_To (Result,
4671 Make_Attribute_Definition_Clause (Loc,
4672 Name => New_Occurrence_Of (DT, Loc),
4673 Chars => Name_Alignment,
4674 Expression =>
4675 Make_Attribute_Reference (Loc,
4676 Prefix =>
4677 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
4678 Attribute_Name => Name_Alignment)));
4680 Append_To (Result,
4681 Make_Object_Declaration (Loc,
4682 Defining_Identifier => DT_Ptr,
4683 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
4684 Constant_Present => True,
4685 Expression =>
4686 Unchecked_Convert_To (RTE (RE_Tag),
4687 Make_Attribute_Reference (Loc,
4688 Prefix =>
4689 Make_Selected_Component (Loc,
4690 Prefix => New_Occurrence_Of (DT, Loc),
4691 Selector_Name =>
4692 New_Occurrence_Of
4693 (RTE_Record_Component (RE_NDT_Prims_Ptr), Loc)),
4694 Attribute_Name => Name_Address))));
4696 Set_Is_Statically_Allocated (DT_Ptr,
4697 Is_Library_Level_Tagged_Type (Typ));
4699 -- Generate the SCIL node for the previous object declaration
4700 -- because it has a tag initialization.
4702 if Generate_SCIL then
4703 New_Node :=
4704 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
4705 Set_SCIL_Entity (New_Node, Typ);
4706 Set_SCIL_Node (Last (Result), New_Node);
4707 end if;
4709 -- Generate:
4710 -- DT : Dispatch_Table_Wrapper (Nb_Prim);
4711 -- for DT'Alignment use Address'Alignment;
4712 -- DT_Ptr : Tag := !Tag (DT.Prims_Ptr'Address);
4714 else
4715 -- If the tagged type has no primitives we add a dummy slot
4716 -- whose address will be the tag of this type.
4718 if Nb_Prim = 0 then
4719 DT_Constr_List :=
4720 New_List (Make_Integer_Literal (Loc, 1));
4721 else
4722 DT_Constr_List :=
4723 New_List (Make_Integer_Literal (Loc, Nb_Prim));
4724 end if;
4726 Append_To (Result,
4727 Make_Object_Declaration (Loc,
4728 Defining_Identifier => DT,
4729 Aliased_Present => True,
4730 Constant_Present => False,
4731 Object_Definition =>
4732 Make_Subtype_Indication (Loc,
4733 Subtype_Mark =>
4734 New_Occurrence_Of (RTE (RE_Dispatch_Table_Wrapper), Loc),
4735 Constraint =>
4736 Make_Index_Or_Discriminant_Constraint (Loc,
4737 Constraints => DT_Constr_List))));
4739 Append_To (Result,
4740 Make_Attribute_Definition_Clause (Loc,
4741 Name => New_Occurrence_Of (DT, Loc),
4742 Chars => Name_Alignment,
4743 Expression =>
4744 Make_Attribute_Reference (Loc,
4745 Prefix =>
4746 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
4747 Attribute_Name => Name_Alignment)));
4749 Append_To (Result,
4750 Make_Object_Declaration (Loc,
4751 Defining_Identifier => DT_Ptr,
4752 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
4753 Constant_Present => True,
4754 Expression =>
4755 Unchecked_Convert_To (RTE (RE_Tag),
4756 Make_Attribute_Reference (Loc,
4757 Prefix =>
4758 Make_Selected_Component (Loc,
4759 Prefix => New_Occurrence_Of (DT, Loc),
4760 Selector_Name =>
4761 New_Occurrence_Of
4762 (RTE_Record_Component (RE_Prims_Ptr), Loc)),
4763 Attribute_Name => Name_Address))));
4765 Set_Is_Statically_Allocated (DT_Ptr,
4766 Is_Library_Level_Tagged_Type (Typ));
4768 -- Generate the SCIL node for the previous object declaration
4769 -- because it has a tag initialization.
4771 if Generate_SCIL then
4772 New_Node :=
4773 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
4774 Set_SCIL_Entity (New_Node, Typ);
4775 Set_SCIL_Node (Last (Result), New_Node);
4776 end if;
4778 Append_To (Result,
4779 Make_Object_Declaration (Loc,
4780 Defining_Identifier =>
4781 Node (Next_Elmt (First_Elmt (Access_Disp_Table (Typ)))),
4782 Constant_Present => True,
4783 Object_Definition =>
4784 New_Occurrence_Of (RTE (RE_Address), Loc),
4785 Expression =>
4786 Make_Attribute_Reference (Loc,
4787 Prefix =>
4788 Make_Selected_Component (Loc,
4789 Prefix => New_Occurrence_Of (DT, Loc),
4790 Selector_Name =>
4791 New_Occurrence_Of
4792 (RTE_Record_Component (RE_Predef_Prims), Loc)),
4793 Attribute_Name => Name_Address)));
4794 end if;
4795 end if;
4797 -- Generate: Exname : constant String := full_qualified_name (typ);
4798 -- The type itself may be an anonymous parent type, so use the first
4799 -- subtype to have a user-recognizable name.
4801 Append_To (Result,
4802 Make_Object_Declaration (Loc,
4803 Defining_Identifier => Exname,
4804 Constant_Present => True,
4805 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4806 Expression =>
4807 Make_String_Literal (Loc,
4808 Strval => Fully_Qualified_Name_String (First_Subtype (Typ)))));
4809 Set_Is_Statically_Allocated (Exname);
4810 Set_Is_True_Constant (Exname);
4812 -- Declare the object used by Ada.Tags.Register_Tag
4814 if RTE_Available (RE_Register_Tag) then
4815 Append_To (Result,
4816 Make_Object_Declaration (Loc,
4817 Defining_Identifier => HT_Link,
4818 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc)));
4819 end if;
4821 -- Generate code to create the storage for the type specific data object
4822 -- with enough space to store the tags of the ancestors plus the tags
4823 -- of all the implemented interfaces (as described in a-tags.adb).
4825 -- TSD : Type_Specific_Data (I_Depth) :=
4826 -- (Idepth => I_Depth,
4827 -- Access_Level => Type_Access_Level (Typ),
4828 -- Alignment => Typ'Alignment,
4829 -- Expanded_Name => Cstring_Ptr!(Exname'Address))
4830 -- External_Tag => Cstring_Ptr!(Exname'Address))
4831 -- HT_Link => HT_Link'Address,
4832 -- Transportable => <<boolean-value>>,
4833 -- Type_Is_Abstract => <<boolean-value>>,
4834 -- Needs_Finalization => <<boolean-value>>,
4835 -- [ Size_Func => Size_Prim'Access, ]
4836 -- [ Interfaces_Table => <<access-value>>, ]
4837 -- [ SSD => SSD_Table'Address ]
4838 -- Tags_Table => (0 => null,
4839 -- 1 => Parent'Tag
4840 -- ...);
4841 -- for TSD'Alignment use Address'Alignment
4843 TSD_Aggr_List := New_List;
4845 -- Idepth: Count ancestors to compute the inheritance depth. For private
4846 -- extensions, always go to the full view in order to compute the real
4847 -- inheritance depth.
4849 declare
4850 Current_Typ : Entity_Id;
4851 Parent_Typ : Entity_Id;
4853 begin
4854 I_Depth := 0;
4855 Current_Typ := Typ;
4856 loop
4857 Parent_Typ := Etype (Current_Typ);
4859 if Is_Private_Type (Parent_Typ) then
4860 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4861 end if;
4863 exit when Parent_Typ = Current_Typ;
4865 I_Depth := I_Depth + 1;
4866 Current_Typ := Parent_Typ;
4867 end loop;
4868 end;
4870 Append_To (TSD_Aggr_List,
4871 Make_Integer_Literal (Loc, I_Depth));
4873 -- Access_Level
4875 Append_To (TSD_Aggr_List,
4876 Make_Integer_Literal (Loc, Type_Access_Level (Typ)));
4878 -- Alignment
4880 -- For CPP types we cannot rely on the value of 'Alignment provided
4881 -- by the backend to initialize this TSD field.
4883 if Convention (Typ) = Convention_CPP
4884 or else Is_CPP_Class (Root_Type (Typ))
4885 then
4886 Append_To (TSD_Aggr_List,
4887 Make_Integer_Literal (Loc, 0));
4888 else
4889 Append_To (TSD_Aggr_List,
4890 Make_Attribute_Reference (Loc,
4891 Prefix => New_Occurrence_Of (Typ, Loc),
4892 Attribute_Name => Name_Alignment));
4893 end if;
4895 -- Expanded_Name
4897 Append_To (TSD_Aggr_List,
4898 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
4899 Make_Attribute_Reference (Loc,
4900 Prefix => New_Occurrence_Of (Exname, Loc),
4901 Attribute_Name => Name_Address)));
4903 -- External_Tag of a local tagged type
4905 -- <typ>A : constant String :=
4906 -- "Internal tag at 16#tag-addr#: <full-name-of-typ>";
4908 -- The reason we generate this strange name is that we do not want to
4909 -- enter local tagged types in the global hash table used to compute
4910 -- the Internal_Tag attribute for two reasons:
4912 -- 1. It is hard to avoid a tasking race condition for entering the
4913 -- entry into the hash table.
4915 -- 2. It would cause a storage leak, unless we rig up considerable
4916 -- mechanism to remove the entry from the hash table on exit.
4918 -- So what we do is to generate the above external tag name, where the
4919 -- hex address is the address of the local dispatch table (i.e. exactly
4920 -- the value we want if Internal_Tag is computed from this string).
4922 -- Of course this value will only be valid if the tagged type is still
4923 -- in scope, but it clearly must be erroneous to compute the internal
4924 -- tag of a tagged type that is out of scope.
4926 -- We don't do this processing if an explicit external tag has been
4927 -- specified. That's an odd case for which we have already issued a
4928 -- warning, where we will not be able to compute the internal tag.
4930 if not Is_Library_Level_Entity (Typ)
4931 and then not Has_External_Tag_Rep_Clause (Typ)
4932 then
4933 declare
4934 Exname : constant Entity_Id :=
4935 Make_Defining_Identifier (Loc,
4936 Chars => New_External_Name (Tname, 'A'));
4937 Full_Name : constant String_Id :=
4938 Fully_Qualified_Name_String (First_Subtype (Typ));
4939 Str1_Id : String_Id;
4940 Str2_Id : String_Id;
4942 begin
4943 -- Generate:
4944 -- Str1 = "Internal tag at 16#";
4946 Start_String;
4947 Store_String_Chars ("Internal tag at 16#");
4948 Str1_Id := End_String;
4950 -- Generate:
4951 -- Str2 = "#: <type-full-name>";
4953 Start_String;
4954 Store_String_Chars ("#: ");
4955 Store_String_Chars (Full_Name);
4956 Str2_Id := End_String;
4958 -- Generate:
4959 -- Exname : constant String :=
4960 -- Str1 & Address_Image (Tag) & Str2;
4962 if RTE_Available (RE_Address_Image) then
4963 Append_To (Result,
4964 Make_Object_Declaration (Loc,
4965 Defining_Identifier => Exname,
4966 Constant_Present => True,
4967 Object_Definition => New_Occurrence_Of
4968 (Standard_String, Loc),
4969 Expression =>
4970 Make_Op_Concat (Loc,
4971 Left_Opnd => Make_String_Literal (Loc, Str1_Id),
4972 Right_Opnd =>
4973 Make_Op_Concat (Loc,
4974 Left_Opnd =>
4975 Make_Function_Call (Loc,
4976 Name =>
4977 New_Occurrence_Of
4978 (RTE (RE_Address_Image), Loc),
4979 Parameter_Associations => New_List (
4980 Unchecked_Convert_To (RTE (RE_Address),
4981 New_Occurrence_Of (DT_Ptr, Loc)))),
4982 Right_Opnd =>
4983 Make_String_Literal (Loc, Str2_Id)))));
4985 else
4986 Append_To (Result,
4987 Make_Object_Declaration (Loc,
4988 Defining_Identifier => Exname,
4989 Constant_Present => True,
4990 Object_Definition =>
4991 New_Occurrence_Of (Standard_String, Loc),
4992 Expression =>
4993 Make_Op_Concat (Loc,
4994 Left_Opnd => Make_String_Literal (Loc, Str1_Id),
4995 Right_Opnd => Make_String_Literal (Loc, Str2_Id))));
4996 end if;
4998 New_Node :=
4999 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5000 Make_Attribute_Reference (Loc,
5001 Prefix => New_Occurrence_Of (Exname, Loc),
5002 Attribute_Name => Name_Address));
5003 end;
5005 -- External tag of a library-level tagged type: Check for a definition
5006 -- of External_Tag. The clause is considered only if it applies to this
5007 -- specific tagged type, as opposed to one of its ancestors.
5008 -- If the type is an unconstrained type extension, we are building the
5009 -- dispatch table of its anonymous base type, so the external tag, if
5010 -- any was specified, must be retrieved from the first subtype. Go to
5011 -- the full view in case the clause is in the private part.
5013 else
5014 declare
5015 Def : constant Node_Id := Get_Attribute_Definition_Clause
5016 (Underlying_Type (First_Subtype (Typ)),
5017 Attribute_External_Tag);
5019 Old_Val : String_Id;
5020 New_Val : String_Id;
5021 E : Entity_Id;
5023 begin
5024 if not Present (Def)
5025 or else Entity (Name (Def)) /= First_Subtype (Typ)
5026 then
5027 New_Node :=
5028 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5029 Make_Attribute_Reference (Loc,
5030 Prefix => New_Occurrence_Of (Exname, Loc),
5031 Attribute_Name => Name_Address));
5032 else
5033 Old_Val := Strval (Expr_Value_S (Expression (Def)));
5035 -- For the rep clause "for <typ>'external_tag use y" generate:
5037 -- <typ>A : constant string := y;
5039 -- <typ>A'Address is used to set the External_Tag component
5040 -- of the TSD
5042 -- Create a new nul terminated string if it is not already
5044 if String_Length (Old_Val) > 0
5045 and then
5046 Get_String_Char (Old_Val, String_Length (Old_Val)) = 0
5047 then
5048 New_Val := Old_Val;
5049 else
5050 Start_String (Old_Val);
5051 Store_String_Char (Get_Char_Code (ASCII.NUL));
5052 New_Val := End_String;
5053 end if;
5055 E := Make_Defining_Identifier (Loc,
5056 New_External_Name (Chars (Typ), 'A'));
5058 Append_To (Result,
5059 Make_Object_Declaration (Loc,
5060 Defining_Identifier => E,
5061 Constant_Present => True,
5062 Object_Definition =>
5063 New_Occurrence_Of (Standard_String, Loc),
5064 Expression =>
5065 Make_String_Literal (Loc, New_Val)));
5067 New_Node :=
5068 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5069 Make_Attribute_Reference (Loc,
5070 Prefix => New_Occurrence_Of (E, Loc),
5071 Attribute_Name => Name_Address));
5072 end if;
5073 end;
5074 end if;
5076 Append_To (TSD_Aggr_List, New_Node);
5078 -- HT_Link
5080 if RTE_Available (RE_Register_Tag) then
5081 Append_To (TSD_Aggr_List,
5082 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5083 Make_Attribute_Reference (Loc,
5084 Prefix => New_Occurrence_Of (HT_Link, Loc),
5085 Attribute_Name => Name_Address)));
5086 else
5087 Append_To (TSD_Aggr_List,
5088 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5089 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5090 end if;
5092 -- Transportable: Set for types that can be used in remote calls
5093 -- with respect to E.4(18) legality rules.
5095 declare
5096 Transportable : Entity_Id;
5098 begin
5099 Transportable :=
5100 Boolean_Literals
5101 (Is_Pure (Typ)
5102 or else Is_Shared_Passive (Typ)
5103 or else
5104 ((Is_Remote_Types (Typ)
5105 or else Is_Remote_Call_Interface (Typ))
5106 and then Original_View_In_Visible_Part (Typ))
5107 or else not Comes_From_Source (Typ));
5109 Append_To (TSD_Aggr_List,
5110 New_Occurrence_Of (Transportable, Loc));
5111 end;
5113 -- Type_Is_Abstract (Ada 2012: AI05-0173). This functionality is
5114 -- not available in the HIE runtime.
5116 if RTE_Record_Component_Available (RE_Type_Is_Abstract) then
5117 declare
5118 Type_Is_Abstract : Entity_Id;
5119 begin
5120 Type_Is_Abstract := Boolean_Literals (Is_Abstract_Type (Typ));
5121 Append_To (TSD_Aggr_List,
5122 New_Occurrence_Of (Type_Is_Abstract, Loc));
5123 end;
5124 end if;
5126 -- Needs_Finalization: Set if the type is controlled or has controlled
5127 -- components.
5129 declare
5130 Needs_Fin : Entity_Id;
5131 begin
5132 Needs_Fin := Boolean_Literals (Needs_Finalization (Typ));
5133 Append_To (TSD_Aggr_List, New_Occurrence_Of (Needs_Fin, Loc));
5134 end;
5136 -- Size_Func
5138 if RTE_Record_Component_Available (RE_Size_Func) then
5140 -- Initialize this field to Null_Address if we are not building
5141 -- static dispatch tables static or if the size function is not
5142 -- available. In the former case we cannot initialize this field
5143 -- until the function is frozen and registered in the dispatch
5144 -- table (see Register_Primitive).
5146 if not Building_Static_DT (Typ) or else not Has_DT (Typ) then
5147 Append_To (TSD_Aggr_List,
5148 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5149 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5151 else
5152 declare
5153 Prim_Elmt : Elmt_Id;
5154 Prim : Entity_Id;
5155 Size_Comp : Node_Id;
5157 begin
5158 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5159 while Present (Prim_Elmt) loop
5160 Prim := Node (Prim_Elmt);
5162 if Chars (Prim) = Name_uSize then
5163 Prim := Ultimate_Alias (Prim);
5165 if Is_Abstract_Subprogram (Prim) then
5166 Size_Comp :=
5167 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5168 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
5169 else
5170 Size_Comp :=
5171 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5172 Make_Attribute_Reference (Loc,
5173 Prefix => New_Occurrence_Of (Prim, Loc),
5174 Attribute_Name => Name_Unrestricted_Access));
5175 end if;
5177 exit;
5178 end if;
5180 Next_Elmt (Prim_Elmt);
5181 end loop;
5183 pragma Assert (Present (Size_Comp));
5184 Append_To (TSD_Aggr_List, Size_Comp);
5185 end;
5186 end if;
5187 end if;
5189 -- Interfaces_Table (required for AI-405)
5191 if RTE_Record_Component_Available (RE_Interfaces_Table) then
5193 -- Count the number of interface types implemented by Typ
5195 Collect_Interfaces (Typ, Typ_Ifaces);
5197 AI := First_Elmt (Typ_Ifaces);
5198 while Present (AI) loop
5199 Num_Ifaces := Num_Ifaces + 1;
5200 Next_Elmt (AI);
5201 end loop;
5203 if Num_Ifaces = 0 then
5204 Iface_Table_Node := Make_Null (Loc);
5206 -- Generate the Interface_Table object
5208 else
5209 declare
5210 TSD_Ifaces_List : constant List_Id := New_List;
5211 Elmt : Elmt_Id;
5212 Sec_DT_Tag : Node_Id;
5214 begin
5215 AI := First_Elmt (Typ_Ifaces);
5216 while Present (AI) loop
5217 if Is_Ancestor (Node (AI), Typ, Use_Full_View => True) then
5218 Sec_DT_Tag :=
5219 New_Occurrence_Of (DT_Ptr, Loc);
5220 else
5221 Elmt :=
5222 Next_Elmt
5223 (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5224 pragma Assert (Has_Thunks (Node (Elmt)));
5226 while Is_Tag (Node (Elmt))
5227 and then not
5228 Is_Ancestor (Node (AI), Related_Type (Node (Elmt)),
5229 Use_Full_View => True)
5230 loop
5231 pragma Assert (Has_Thunks (Node (Elmt)));
5232 Next_Elmt (Elmt);
5233 pragma Assert (Has_Thunks (Node (Elmt)));
5234 Next_Elmt (Elmt);
5235 pragma Assert (not Has_Thunks (Node (Elmt)));
5236 Next_Elmt (Elmt);
5237 pragma Assert (not Has_Thunks (Node (Elmt)));
5238 Next_Elmt (Elmt);
5239 end loop;
5241 pragma Assert (Ekind (Node (Elmt)) = E_Constant
5242 and then not
5243 Has_Thunks (Node (Next_Elmt (Next_Elmt (Elmt)))));
5244 Sec_DT_Tag :=
5245 New_Occurrence_Of (Node (Next_Elmt (Next_Elmt (Elmt))),
5246 Loc);
5247 end if;
5249 Append_To (TSD_Ifaces_List,
5250 Make_Aggregate (Loc,
5251 Expressions => New_List (
5253 -- Iface_Tag
5255 Unchecked_Convert_To (RTE (RE_Tag),
5256 New_Occurrence_Of
5257 (Node (First_Elmt (Access_Disp_Table (Node (AI)))),
5258 Loc)),
5260 -- Static_Offset_To_Top
5262 New_Occurrence_Of (Standard_True, Loc),
5264 -- Offset_To_Top_Value
5266 Make_Integer_Literal (Loc, 0),
5268 -- Offset_To_Top_Func
5270 Make_Null (Loc),
5272 -- Secondary_DT
5274 Unchecked_Convert_To (RTE (RE_Tag), Sec_DT_Tag)
5276 )));
5278 Next_Elmt (AI);
5279 end loop;
5281 Name_ITable := New_External_Name (Tname, 'I');
5282 ITable := Make_Defining_Identifier (Loc, Name_ITable);
5283 Set_Is_Statically_Allocated (ITable,
5284 Is_Library_Level_Tagged_Type (Typ));
5286 -- The table of interfaces is not constant; its slots are
5287 -- filled at run time by the IP routine using attribute
5288 -- 'Position to know the location of the tag components
5289 -- (and this attribute cannot be safely used before the
5290 -- object is initialized).
5292 Append_To (Result,
5293 Make_Object_Declaration (Loc,
5294 Defining_Identifier => ITable,
5295 Aliased_Present => True,
5296 Constant_Present => False,
5297 Object_Definition =>
5298 Make_Subtype_Indication (Loc,
5299 Subtype_Mark =>
5300 New_Occurrence_Of (RTE (RE_Interface_Data), Loc),
5301 Constraint =>
5302 Make_Index_Or_Discriminant_Constraint (Loc,
5303 Constraints => New_List (
5304 Make_Integer_Literal (Loc, Num_Ifaces)))),
5306 Expression => Make_Aggregate (Loc,
5307 Expressions => New_List (
5308 Make_Integer_Literal (Loc, Num_Ifaces),
5309 Make_Aggregate (Loc, TSD_Ifaces_List)))));
5311 Append_To (Result,
5312 Make_Attribute_Definition_Clause (Loc,
5313 Name => New_Occurrence_Of (ITable, Loc),
5314 Chars => Name_Alignment,
5315 Expression =>
5316 Make_Attribute_Reference (Loc,
5317 Prefix =>
5318 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
5319 Attribute_Name => Name_Alignment)));
5321 Iface_Table_Node :=
5322 Make_Attribute_Reference (Loc,
5323 Prefix => New_Occurrence_Of (ITable, Loc),
5324 Attribute_Name => Name_Unchecked_Access);
5325 end;
5326 end if;
5328 Append_To (TSD_Aggr_List, Iface_Table_Node);
5329 end if;
5331 -- Generate the Select Specific Data table for synchronized types that
5332 -- implement synchronized interfaces. The size of the table is
5333 -- constrained by the number of non-predefined primitive operations.
5335 if RTE_Record_Component_Available (RE_SSD) then
5336 if Ada_Version >= Ada_2005
5337 and then Has_DT (Typ)
5338 and then Is_Concurrent_Record_Type (Typ)
5339 and then Has_Interfaces (Typ)
5340 and then Nb_Prim > 0
5341 and then not Is_Abstract_Type (Typ)
5342 and then not Is_Controlled (Typ)
5343 and then not Restriction_Active (No_Dispatching_Calls)
5344 and then not Restriction_Active (No_Select_Statements)
5345 then
5346 Append_To (Result,
5347 Make_Object_Declaration (Loc,
5348 Defining_Identifier => SSD,
5349 Aliased_Present => True,
5350 Object_Definition =>
5351 Make_Subtype_Indication (Loc,
5352 Subtype_Mark => New_Occurrence_Of (
5353 RTE (RE_Select_Specific_Data), Loc),
5354 Constraint =>
5355 Make_Index_Or_Discriminant_Constraint (Loc,
5356 Constraints => New_List (
5357 Make_Integer_Literal (Loc, Nb_Prim))))));
5359 Append_To (Result,
5360 Make_Attribute_Definition_Clause (Loc,
5361 Name => New_Occurrence_Of (SSD, Loc),
5362 Chars => Name_Alignment,
5363 Expression =>
5364 Make_Attribute_Reference (Loc,
5365 Prefix =>
5366 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
5367 Attribute_Name => Name_Alignment)));
5369 -- This table is initialized by Make_Select_Specific_Data_Table,
5370 -- which calls Set_Entry_Index and Set_Prim_Op_Kind.
5372 Append_To (TSD_Aggr_List,
5373 Make_Attribute_Reference (Loc,
5374 Prefix => New_Occurrence_Of (SSD, Loc),
5375 Attribute_Name => Name_Unchecked_Access));
5376 else
5377 Append_To (TSD_Aggr_List, Make_Null (Loc));
5378 end if;
5379 end if;
5381 -- Initialize the table of ancestor tags. In case of interface types
5382 -- this table is not needed.
5384 TSD_Tags_List := New_List;
5386 -- If we are not statically allocating the dispatch table then we must
5387 -- fill position 0 with null because we still have not generated the
5388 -- tag of Typ.
5390 if not Building_Static_DT (Typ)
5391 or else Is_Interface (Typ)
5392 then
5393 Append_To (TSD_Tags_List,
5394 Unchecked_Convert_To (RTE (RE_Tag),
5395 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5397 -- Otherwise we can safely reference the tag
5399 else
5400 Append_To (TSD_Tags_List,
5401 New_Occurrence_Of (DT_Ptr, Loc));
5402 end if;
5404 -- Fill the rest of the table with the tags of the ancestors
5406 declare
5407 Current_Typ : Entity_Id;
5408 Parent_Typ : Entity_Id;
5409 Pos : Nat;
5411 begin
5412 Pos := 1;
5413 Current_Typ := Typ;
5415 loop
5416 Parent_Typ := Etype (Current_Typ);
5418 if Is_Private_Type (Parent_Typ) then
5419 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5420 end if;
5422 exit when Parent_Typ = Current_Typ;
5424 if Is_CPP_Class (Parent_Typ) then
5426 -- The tags defined in the C++ side will be inherited when
5427 -- the object is constructed (Exp_Ch3.Build_Init_Procedure)
5429 Append_To (TSD_Tags_List,
5430 Unchecked_Convert_To (RTE (RE_Tag),
5431 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5432 else
5433 Append_To (TSD_Tags_List,
5434 New_Occurrence_Of
5435 (Node (First_Elmt (Access_Disp_Table (Parent_Typ))),
5436 Loc));
5437 end if;
5439 Pos := Pos + 1;
5440 Current_Typ := Parent_Typ;
5441 end loop;
5443 pragma Assert (Pos = I_Depth + 1);
5444 end;
5446 Append_To (TSD_Aggr_List,
5447 Make_Aggregate (Loc,
5448 Expressions => TSD_Tags_List));
5450 -- Build the TSD object
5452 Append_To (Result,
5453 Make_Object_Declaration (Loc,
5454 Defining_Identifier => TSD,
5455 Aliased_Present => True,
5456 Constant_Present => Building_Static_DT (Typ),
5457 Object_Definition =>
5458 Make_Subtype_Indication (Loc,
5459 Subtype_Mark => New_Occurrence_Of (
5460 RTE (RE_Type_Specific_Data), Loc),
5461 Constraint =>
5462 Make_Index_Or_Discriminant_Constraint (Loc,
5463 Constraints => New_List (
5464 Make_Integer_Literal (Loc, I_Depth)))),
5466 Expression => Make_Aggregate (Loc,
5467 Expressions => TSD_Aggr_List)));
5469 Set_Is_True_Constant (TSD, Building_Static_DT (Typ));
5471 Append_To (Result,
5472 Make_Attribute_Definition_Clause (Loc,
5473 Name => New_Occurrence_Of (TSD, Loc),
5474 Chars => Name_Alignment,
5475 Expression =>
5476 Make_Attribute_Reference (Loc,
5477 Prefix =>
5478 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
5479 Attribute_Name => Name_Alignment)));
5481 -- Initialize or declare the dispatch table object
5483 if not Has_DT (Typ) then
5484 DT_Constr_List := New_List;
5485 DT_Aggr_List := New_List;
5487 -- Typeinfo
5489 New_Node :=
5490 Make_Attribute_Reference (Loc,
5491 Prefix => New_Occurrence_Of (TSD, Loc),
5492 Attribute_Name => Name_Address);
5494 Append_To (DT_Constr_List, New_Node);
5495 Append_To (DT_Aggr_List, New_Copy (New_Node));
5496 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, 0));
5498 -- In case of locally defined tagged types we have already declared
5499 -- and uninitialized object for the dispatch table, which is now
5500 -- initialized by means of the following assignment:
5502 -- DT := (TSD'Address, 0);
5504 if not Building_Static_DT (Typ) then
5505 Append_To (Result,
5506 Make_Assignment_Statement (Loc,
5507 Name => New_Occurrence_Of (DT, Loc),
5508 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
5510 -- In case of library level tagged types we declare and export now
5511 -- the constant object containing the dummy dispatch table. There
5512 -- is no need to declare the tag here because it has been previously
5513 -- declared by Make_Tags
5515 -- DT : aliased constant No_Dispatch_Table :=
5516 -- (NDT_TSD => TSD'Address;
5517 -- NDT_Prims_Ptr => 0);
5518 -- for DT'Alignment use Address'Alignment;
5520 else
5521 Append_To (Result,
5522 Make_Object_Declaration (Loc,
5523 Defining_Identifier => DT,
5524 Aliased_Present => True,
5525 Constant_Present => True,
5526 Object_Definition =>
5527 New_Occurrence_Of (RTE (RE_No_Dispatch_Table_Wrapper), Loc),
5528 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
5530 Append_To (Result,
5531 Make_Attribute_Definition_Clause (Loc,
5532 Name => New_Occurrence_Of (DT, Loc),
5533 Chars => Name_Alignment,
5534 Expression =>
5535 Make_Attribute_Reference (Loc,
5536 Prefix =>
5537 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
5538 Attribute_Name => Name_Alignment)));
5540 Export_DT (Typ, DT);
5541 end if;
5543 -- Common case: Typ has a dispatch table
5545 -- Generate:
5547 -- Predef_Prims : Address_Array (1 .. Default_Prim_Ops_Count) :=
5548 -- (predef-prim-op-1'address,
5549 -- predef-prim-op-2'address,
5550 -- ...
5551 -- predef-prim-op-n'address);
5552 -- for Predef_Prims'Alignment use Address'Alignment
5554 -- DT : Dispatch_Table (Nb_Prims) :=
5555 -- (Signature => <sig-value>,
5556 -- Tag_Kind => <tag_kind-value>,
5557 -- Predef_Prims => Predef_Prims'First'Address,
5558 -- Offset_To_Top => 0,
5559 -- TSD => TSD'Address;
5560 -- Prims_Ptr => (prim-op-1'address,
5561 -- prim-op-2'address,
5562 -- ...
5563 -- prim-op-n'address));
5564 -- for DT'Alignment use Address'Alignment
5566 else
5567 declare
5568 Pos : Nat;
5570 begin
5571 if not Building_Static_DT (Typ) then
5572 Nb_Predef_Prims := Max_Predef_Prims;
5574 else
5575 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5576 while Present (Prim_Elmt) loop
5577 Prim := Node (Prim_Elmt);
5579 if Is_Predefined_Dispatching_Operation (Prim)
5580 and then not Is_Abstract_Subprogram (Prim)
5581 then
5582 Pos := UI_To_Int (DT_Position (Prim));
5584 if Pos > Nb_Predef_Prims then
5585 Nb_Predef_Prims := Pos;
5586 end if;
5587 end if;
5589 Next_Elmt (Prim_Elmt);
5590 end loop;
5591 end if;
5593 declare
5594 Prim_Table : array
5595 (Nat range 1 .. Nb_Predef_Prims) of Entity_Id;
5596 Decl : Node_Id;
5597 E : Entity_Id;
5599 begin
5600 Prim_Ops_Aggr_List := New_List;
5602 Prim_Table := (others => Empty);
5604 if Building_Static_DT (Typ) then
5605 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5606 while Present (Prim_Elmt) loop
5607 Prim := Node (Prim_Elmt);
5609 if Is_Predefined_Dispatching_Operation (Prim)
5610 and then not Is_Abstract_Subprogram (Prim)
5611 and then not Is_Eliminated (Prim)
5612 and then not Present (Prim_Table
5613 (UI_To_Int (DT_Position (Prim))))
5614 then
5615 E := Ultimate_Alias (Prim);
5616 pragma Assert (not Is_Abstract_Subprogram (E));
5617 Prim_Table (UI_To_Int (DT_Position (Prim))) := E;
5618 end if;
5620 Next_Elmt (Prim_Elmt);
5621 end loop;
5622 end if;
5624 for J in Prim_Table'Range loop
5625 if Present (Prim_Table (J)) then
5626 New_Node :=
5627 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
5628 Make_Attribute_Reference (Loc,
5629 Prefix =>
5630 New_Occurrence_Of (Prim_Table (J), Loc),
5631 Attribute_Name => Name_Unrestricted_Access));
5632 else
5633 New_Node := Make_Null (Loc);
5634 end if;
5636 Append_To (Prim_Ops_Aggr_List, New_Node);
5637 end loop;
5639 New_Node :=
5640 Make_Aggregate (Loc,
5641 Expressions => Prim_Ops_Aggr_List);
5643 Decl :=
5644 Make_Subtype_Declaration (Loc,
5645 Defining_Identifier => Make_Temporary (Loc, 'S'),
5646 Subtype_Indication =>
5647 New_Occurrence_Of (RTE (RE_Address_Array), Loc));
5649 Append_To (Result, Decl);
5651 Append_To (Result,
5652 Make_Object_Declaration (Loc,
5653 Defining_Identifier => Predef_Prims,
5654 Aliased_Present => True,
5655 Constant_Present => Building_Static_DT (Typ),
5656 Object_Definition =>
5657 New_Occurrence_Of (Defining_Identifier (Decl), Loc),
5658 Expression => New_Node));
5660 -- Remember aggregates initializing dispatch tables
5662 Append_Elmt (New_Node, DT_Aggr);
5664 Append_To (Result,
5665 Make_Attribute_Definition_Clause (Loc,
5666 Name => New_Occurrence_Of (Predef_Prims, Loc),
5667 Chars => Name_Alignment,
5668 Expression =>
5669 Make_Attribute_Reference (Loc,
5670 Prefix =>
5671 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
5672 Attribute_Name => Name_Alignment)));
5673 end;
5674 end;
5676 -- Stage 1: Initialize the discriminant and the record components
5678 DT_Constr_List := New_List;
5679 DT_Aggr_List := New_List;
5681 -- Num_Prims. If the tagged type has no primitives we add a dummy
5682 -- slot whose address will be the tag of this type.
5684 if Nb_Prim = 0 then
5685 New_Node := Make_Integer_Literal (Loc, 1);
5686 else
5687 New_Node := Make_Integer_Literal (Loc, Nb_Prim);
5688 end if;
5690 Append_To (DT_Constr_List, New_Node);
5691 Append_To (DT_Aggr_List, New_Copy (New_Node));
5693 -- Signature
5695 if RTE_Record_Component_Available (RE_Signature) then
5696 Append_To (DT_Aggr_List,
5697 New_Occurrence_Of (RTE (RE_Primary_DT), Loc));
5698 end if;
5700 -- Tag_Kind
5702 if RTE_Record_Component_Available (RE_Tag_Kind) then
5703 Append_To (DT_Aggr_List, Tagged_Kind (Typ));
5704 end if;
5706 -- Predef_Prims
5708 Append_To (DT_Aggr_List,
5709 Make_Attribute_Reference (Loc,
5710 Prefix => New_Occurrence_Of (Predef_Prims, Loc),
5711 Attribute_Name => Name_Address));
5713 -- Offset_To_Top
5715 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, 0));
5717 -- Typeinfo
5719 Append_To (DT_Aggr_List,
5720 Make_Attribute_Reference (Loc,
5721 Prefix => New_Occurrence_Of (TSD, Loc),
5722 Attribute_Name => Name_Address));
5724 -- Stage 2: Initialize the table of user-defined primitive operations
5726 Prim_Ops_Aggr_List := New_List;
5728 if Nb_Prim = 0 then
5729 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
5731 elsif not Building_Static_DT (Typ) then
5732 for J in 1 .. Nb_Prim loop
5733 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
5734 end loop;
5736 else
5737 declare
5738 CPP_Nb_Prims : constant Nat := CPP_Num_Prims (Typ);
5739 E : Entity_Id;
5740 Prim : Entity_Id;
5741 Prim_Elmt : Elmt_Id;
5742 Prim_Pos : Nat;
5743 Prim_Table : array (Nat range 1 .. Nb_Prim) of Entity_Id;
5745 begin
5746 Prim_Table := (others => Empty);
5748 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5749 while Present (Prim_Elmt) loop
5750 Prim := Node (Prim_Elmt);
5752 -- Retrieve the ultimate alias of the primitive for proper
5753 -- handling of renamings and eliminated primitives.
5755 E := Ultimate_Alias (Prim);
5756 Prim_Pos := UI_To_Int (DT_Position (E));
5758 -- Do not reference predefined primitives because they are
5759 -- located in a separate dispatch table; skip entities with
5760 -- attribute Interface_Alias because they are only required
5761 -- to build secondary dispatch tables; skip abstract and
5762 -- eliminated primitives; for derivations of CPP types skip
5763 -- primitives located in the C++ part of the dispatch table
5764 -- because their slot is initialized by the IC routine.
5766 if not Is_Predefined_Dispatching_Operation (Prim)
5767 and then not Is_Predefined_Dispatching_Operation (E)
5768 and then not Present (Interface_Alias (Prim))
5769 and then not Is_Abstract_Subprogram (E)
5770 and then not Is_Eliminated (E)
5771 and then (not Is_CPP_Class (Root_Type (Typ))
5772 or else Prim_Pos > CPP_Nb_Prims)
5773 then
5774 pragma Assert
5775 (UI_To_Int (DT_Position (Prim)) <= Nb_Prim);
5777 Prim_Table (UI_To_Int (DT_Position (Prim))) := E;
5778 end if;
5780 Next_Elmt (Prim_Elmt);
5781 end loop;
5783 for J in Prim_Table'Range loop
5784 if Present (Prim_Table (J)) then
5785 New_Node :=
5786 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
5787 Make_Attribute_Reference (Loc,
5788 Prefix =>
5789 New_Occurrence_Of (Prim_Table (J), Loc),
5790 Attribute_Name => Name_Unrestricted_Access));
5791 else
5792 New_Node := Make_Null (Loc);
5793 end if;
5795 Append_To (Prim_Ops_Aggr_List, New_Node);
5796 end loop;
5797 end;
5798 end if;
5800 New_Node :=
5801 Make_Aggregate (Loc,
5802 Expressions => Prim_Ops_Aggr_List);
5804 Append_To (DT_Aggr_List, New_Node);
5806 -- Remember aggregates initializing dispatch tables
5808 Append_Elmt (New_Node, DT_Aggr);
5810 -- In case of locally defined tagged types we have already declared
5811 -- and uninitialized object for the dispatch table, which is now
5812 -- initialized by means of an assignment.
5814 if not Building_Static_DT (Typ) then
5815 Append_To (Result,
5816 Make_Assignment_Statement (Loc,
5817 Name => New_Occurrence_Of (DT, Loc),
5818 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
5820 -- In case of library level tagged types we declare now and export
5821 -- the constant object containing the dispatch table.
5823 else
5824 Append_To (Result,
5825 Make_Object_Declaration (Loc,
5826 Defining_Identifier => DT,
5827 Aliased_Present => True,
5828 Constant_Present => True,
5829 Object_Definition =>
5830 Make_Subtype_Indication (Loc,
5831 Subtype_Mark => New_Occurrence_Of
5832 (RTE (RE_Dispatch_Table_Wrapper), Loc),
5833 Constraint => Make_Index_Or_Discriminant_Constraint (Loc,
5834 Constraints => DT_Constr_List)),
5835 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
5837 Append_To (Result,
5838 Make_Attribute_Definition_Clause (Loc,
5839 Name => New_Occurrence_Of (DT, Loc),
5840 Chars => Name_Alignment,
5841 Expression =>
5842 Make_Attribute_Reference (Loc,
5843 Prefix =>
5844 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
5845 Attribute_Name => Name_Alignment)));
5847 Export_DT (Typ, DT);
5848 end if;
5849 end if;
5851 -- Initialize the table of ancestor tags if not building static
5852 -- dispatch table
5854 if not Building_Static_DT (Typ)
5855 and then not Is_Interface (Typ)
5856 and then not Is_CPP_Class (Typ)
5857 then
5858 Append_To (Result,
5859 Make_Assignment_Statement (Loc,
5860 Name =>
5861 Make_Indexed_Component (Loc,
5862 Prefix =>
5863 Make_Selected_Component (Loc,
5864 Prefix => New_Occurrence_Of (TSD, Loc),
5865 Selector_Name =>
5866 New_Occurrence_Of
5867 (RTE_Record_Component (RE_Tags_Table), Loc)),
5868 Expressions =>
5869 New_List (Make_Integer_Literal (Loc, 0))),
5871 Expression =>
5872 New_Occurrence_Of
5873 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)));
5874 end if;
5876 -- Inherit the dispatch tables of the parent. There is no need to
5877 -- inherit anything from the parent when building static dispatch tables
5878 -- because the whole dispatch table (including inherited primitives) has
5879 -- been already built.
5881 if Building_Static_DT (Typ) then
5882 null;
5884 -- If the ancestor is a CPP_Class type we inherit the dispatch tables
5885 -- in the init proc, and we don't need to fill them in here.
5887 elsif Is_CPP_Class (Parent_Typ) then
5888 null;
5890 -- Otherwise we fill in the dispatch tables here
5892 else
5893 if Typ /= Parent_Typ
5894 and then not Is_Interface (Typ)
5895 and then not Restriction_Active (No_Dispatching_Calls)
5896 then
5897 -- Inherit the dispatch table
5899 if not Is_Interface (Typ)
5900 and then not Is_Interface (Parent_Typ)
5901 and then not Is_CPP_Class (Parent_Typ)
5902 then
5903 declare
5904 Nb_Prims : constant Int :=
5905 UI_To_Int (DT_Entry_Count
5906 (First_Tag_Component (Parent_Typ)));
5908 begin
5909 Append_To (Elab_Code,
5910 Build_Inherit_Predefined_Prims (Loc,
5911 Old_Tag_Node =>
5912 New_Occurrence_Of
5913 (Node
5914 (Next_Elmt
5915 (First_Elmt
5916 (Access_Disp_Table (Parent_Typ)))), Loc),
5917 New_Tag_Node =>
5918 New_Occurrence_Of
5919 (Node
5920 (Next_Elmt
5921 (First_Elmt
5922 (Access_Disp_Table (Typ)))), Loc)));
5924 if Nb_Prims /= 0 then
5925 Append_To (Elab_Code,
5926 Build_Inherit_Prims (Loc,
5927 Typ => Typ,
5928 Old_Tag_Node =>
5929 New_Occurrence_Of
5930 (Node
5931 (First_Elmt
5932 (Access_Disp_Table (Parent_Typ))), Loc),
5933 New_Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
5934 Num_Prims => Nb_Prims));
5935 end if;
5936 end;
5937 end if;
5939 -- Inherit the secondary dispatch tables of the ancestor
5941 if not Is_CPP_Class (Parent_Typ) then
5942 declare
5943 Sec_DT_Ancestor : Elmt_Id :=
5944 Next_Elmt
5945 (Next_Elmt
5946 (First_Elmt
5947 (Access_Disp_Table
5948 (Parent_Typ))));
5949 Sec_DT_Typ : Elmt_Id :=
5950 Next_Elmt
5951 (Next_Elmt
5952 (First_Elmt
5953 (Access_Disp_Table (Typ))));
5955 procedure Copy_Secondary_DTs (Typ : Entity_Id);
5956 -- Local procedure required to climb through the ancestors
5957 -- and copy the contents of all their secondary dispatch
5958 -- tables.
5960 ------------------------
5961 -- Copy_Secondary_DTs --
5962 ------------------------
5964 procedure Copy_Secondary_DTs (Typ : Entity_Id) is
5965 E : Entity_Id;
5966 Iface : Elmt_Id;
5968 begin
5969 -- Climb to the ancestor (if any) handling private types
5971 if Present (Full_View (Etype (Typ))) then
5972 if Full_View (Etype (Typ)) /= Typ then
5973 Copy_Secondary_DTs (Full_View (Etype (Typ)));
5974 end if;
5976 elsif Etype (Typ) /= Typ then
5977 Copy_Secondary_DTs (Etype (Typ));
5978 end if;
5980 if Present (Interfaces (Typ))
5981 and then not Is_Empty_Elmt_List (Interfaces (Typ))
5982 then
5983 Iface := First_Elmt (Interfaces (Typ));
5984 E := First_Entity (Typ);
5985 while Present (E)
5986 and then Present (Node (Sec_DT_Ancestor))
5987 and then Ekind (Node (Sec_DT_Ancestor)) = E_Constant
5988 loop
5989 if Is_Tag (E) and then Chars (E) /= Name_uTag then
5990 declare
5991 Num_Prims : constant Int :=
5992 UI_To_Int (DT_Entry_Count (E));
5994 begin
5995 if not Is_Interface (Etype (Typ)) then
5997 -- Inherit first secondary dispatch table
5999 Append_To (Elab_Code,
6000 Build_Inherit_Predefined_Prims (Loc,
6001 Old_Tag_Node =>
6002 Unchecked_Convert_To (RTE (RE_Tag),
6003 New_Occurrence_Of
6004 (Node
6005 (Next_Elmt (Sec_DT_Ancestor)),
6006 Loc)),
6007 New_Tag_Node =>
6008 Unchecked_Convert_To (RTE (RE_Tag),
6009 New_Occurrence_Of
6010 (Node (Next_Elmt (Sec_DT_Typ)),
6011 Loc))));
6013 if Num_Prims /= 0 then
6014 Append_To (Elab_Code,
6015 Build_Inherit_Prims (Loc,
6016 Typ => Node (Iface),
6017 Old_Tag_Node =>
6018 Unchecked_Convert_To
6019 (RTE (RE_Tag),
6020 New_Occurrence_Of
6021 (Node (Sec_DT_Ancestor),
6022 Loc)),
6023 New_Tag_Node =>
6024 Unchecked_Convert_To
6025 (RTE (RE_Tag),
6026 New_Occurrence_Of
6027 (Node (Sec_DT_Typ), Loc)),
6028 Num_Prims => Num_Prims));
6029 end if;
6030 end if;
6032 Next_Elmt (Sec_DT_Ancestor);
6033 Next_Elmt (Sec_DT_Typ);
6035 -- Skip the secondary dispatch table of
6036 -- predefined primitives
6038 Next_Elmt (Sec_DT_Ancestor);
6039 Next_Elmt (Sec_DT_Typ);
6041 if not Is_Interface (Etype (Typ)) then
6043 -- Inherit second secondary dispatch table
6045 Append_To (Elab_Code,
6046 Build_Inherit_Predefined_Prims (Loc,
6047 Old_Tag_Node =>
6048 Unchecked_Convert_To (RTE (RE_Tag),
6049 New_Occurrence_Of
6050 (Node
6051 (Next_Elmt (Sec_DT_Ancestor)),
6052 Loc)),
6053 New_Tag_Node =>
6054 Unchecked_Convert_To (RTE (RE_Tag),
6055 New_Occurrence_Of
6056 (Node (Next_Elmt (Sec_DT_Typ)),
6057 Loc))));
6059 if Num_Prims /= 0 then
6060 Append_To (Elab_Code,
6061 Build_Inherit_Prims (Loc,
6062 Typ => Node (Iface),
6063 Old_Tag_Node =>
6064 Unchecked_Convert_To
6065 (RTE (RE_Tag),
6066 New_Occurrence_Of
6067 (Node (Sec_DT_Ancestor),
6068 Loc)),
6069 New_Tag_Node =>
6070 Unchecked_Convert_To
6071 (RTE (RE_Tag),
6072 New_Occurrence_Of
6073 (Node (Sec_DT_Typ), Loc)),
6074 Num_Prims => Num_Prims));
6075 end if;
6076 end if;
6077 end;
6079 Next_Elmt (Sec_DT_Ancestor);
6080 Next_Elmt (Sec_DT_Typ);
6082 -- Skip the secondary dispatch table of
6083 -- predefined primitives
6085 Next_Elmt (Sec_DT_Ancestor);
6086 Next_Elmt (Sec_DT_Typ);
6088 Next_Elmt (Iface);
6089 end if;
6091 Next_Entity (E);
6092 end loop;
6093 end if;
6094 end Copy_Secondary_DTs;
6096 begin
6097 if Present (Node (Sec_DT_Ancestor))
6098 and then Ekind (Node (Sec_DT_Ancestor)) = E_Constant
6099 then
6100 -- Handle private types
6102 if Present (Full_View (Typ)) then
6103 Copy_Secondary_DTs (Full_View (Typ));
6104 else
6105 Copy_Secondary_DTs (Typ);
6106 end if;
6107 end if;
6108 end;
6109 end if;
6110 end if;
6111 end if;
6113 -- Generate code to check if the external tag of this type is the same
6114 -- as the external tag of some other declaration.
6116 -- Check_TSD (TSD'Unrestricted_Access);
6118 -- This check is a consequence of AI05-0113-1/06, so it officially
6119 -- applies to Ada 2005 (and Ada 2012). It might be argued that it is
6120 -- a desirable check to add in Ada 95 mode, but we hesitate to make
6121 -- this change, as it would be incompatible, and could conceivably
6122 -- cause a problem in existing Aa 95 code.
6124 -- We check for No_Run_Time_Mode here, because we do not want to pick
6125 -- up the RE_Check_TSD entity and call it in No_Run_Time mode.
6127 if not No_Run_Time_Mode
6128 and then Ada_Version >= Ada_2005
6129 and then RTE_Available (RE_Check_TSD)
6130 and then not Duplicated_Tag_Checks_Suppressed (Typ)
6131 then
6132 Append_To (Elab_Code,
6133 Make_Procedure_Call_Statement (Loc,
6134 Name =>
6135 New_Occurrence_Of (RTE (RE_Check_TSD), Loc),
6136 Parameter_Associations => New_List (
6137 Make_Attribute_Reference (Loc,
6138 Prefix => New_Occurrence_Of (TSD, Loc),
6139 Attribute_Name => Name_Unchecked_Access))));
6140 end if;
6142 -- Generate code to register the Tag in the External_Tag hash table for
6143 -- the pure Ada type only.
6145 -- Register_Tag (Dt_Ptr);
6147 -- Skip this action in the following cases:
6148 -- 1) if Register_Tag is not available.
6149 -- 2) in No_Run_Time mode.
6150 -- 3) if Typ is not defined at the library level (this is required
6151 -- to avoid adding concurrency control to the hash table used
6152 -- by the run-time to register the tags).
6154 if not No_Run_Time_Mode
6155 and then Is_Library_Level_Entity (Typ)
6156 and then RTE_Available (RE_Register_Tag)
6157 then
6158 Append_To (Elab_Code,
6159 Make_Procedure_Call_Statement (Loc,
6160 Name =>
6161 New_Occurrence_Of (RTE (RE_Register_Tag), Loc),
6162 Parameter_Associations =>
6163 New_List (New_Occurrence_Of (DT_Ptr, Loc))));
6164 end if;
6166 if not Is_Empty_List (Elab_Code) then
6167 Append_List_To (Result, Elab_Code);
6168 end if;
6170 -- Populate the two auxiliary tables used for dispatching asynchronous,
6171 -- conditional and timed selects for synchronized types that implement
6172 -- a limited interface. Skip this step in Ravenscar profile or when
6173 -- general dispatching is forbidden.
6175 if Ada_Version >= Ada_2005
6176 and then Is_Concurrent_Record_Type (Typ)
6177 and then Has_Interfaces (Typ)
6178 and then not Restriction_Active (No_Dispatching_Calls)
6179 and then not Restriction_Active (No_Select_Statements)
6180 then
6181 Append_List_To (Result,
6182 Make_Select_Specific_Data_Table (Typ));
6183 end if;
6185 -- Remember entities containing dispatch tables
6187 Append_Elmt (Predef_Prims, DT_Decl);
6188 Append_Elmt (DT, DT_Decl);
6190 Analyze_List (Result, Suppress => All_Checks);
6191 Set_Has_Dispatch_Table (Typ);
6193 -- Mark entities containing dispatch tables. Required by the backend to
6194 -- handle them properly.
6196 if Has_DT (Typ) then
6197 declare
6198 Elmt : Elmt_Id;
6200 begin
6201 -- Object declarations
6203 Elmt := First_Elmt (DT_Decl);
6204 while Present (Elmt) loop
6205 Set_Is_Dispatch_Table_Entity (Node (Elmt));
6206 pragma Assert (Ekind (Etype (Node (Elmt))) = E_Array_Subtype
6207 or else Ekind (Etype (Node (Elmt))) = E_Record_Subtype);
6208 Set_Is_Dispatch_Table_Entity (Etype (Node (Elmt)));
6209 Next_Elmt (Elmt);
6210 end loop;
6212 -- Aggregates initializing dispatch tables
6214 Elmt := First_Elmt (DT_Aggr);
6215 while Present (Elmt) loop
6216 Set_Is_Dispatch_Table_Entity (Etype (Node (Elmt)));
6217 Next_Elmt (Elmt);
6218 end loop;
6219 end;
6220 end if;
6222 -- Register the tagged type in the call graph nodes table
6224 Register_CG_Node (Typ);
6226 return Result;
6227 end Make_DT;
6229 -----------------
6230 -- Make_VM_TSD --
6231 -----------------
6233 function Make_VM_TSD (Typ : Entity_Id) return List_Id is
6234 Loc : constant Source_Ptr := Sloc (Typ);
6235 Result : constant List_Id := New_List;
6237 function Count_Primitives (Typ : Entity_Id) return Nat;
6238 -- Count the non-predefined primitive operations of Typ
6240 ----------------------
6241 -- Count_Primitives --
6242 ----------------------
6244 function Count_Primitives (Typ : Entity_Id) return Nat is
6245 Nb_Prim : Nat;
6246 Prim_Elmt : Elmt_Id;
6247 Prim : Entity_Id;
6249 begin
6250 Nb_Prim := 0;
6252 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6253 while Present (Prim_Elmt) loop
6254 Prim := Node (Prim_Elmt);
6256 if Is_Predefined_Dispatching_Operation (Prim)
6257 or else Is_Predefined_Dispatching_Alias (Prim)
6258 then
6259 null;
6261 elsif Present (Interface_Alias (Prim)) then
6262 null;
6264 else
6265 Nb_Prim := Nb_Prim + 1;
6266 end if;
6268 Next_Elmt (Prim_Elmt);
6269 end loop;
6271 return Nb_Prim;
6272 end Count_Primitives;
6274 --------------
6275 -- Make_OSD --
6276 --------------
6278 function Make_OSD (Iface : Entity_Id) return Node_Id;
6279 -- Generate the Object Specific Data table required to dispatch calls
6280 -- through synchronized interfaces. Returns a node that references the
6281 -- generated OSD object.
6283 function Make_OSD (Iface : Entity_Id) return Node_Id is
6284 Nb_Prim : constant Nat := Count_Primitives (Iface);
6285 OSD : Entity_Id;
6286 OSD_Aggr_List : List_Id;
6288 begin
6289 -- Generate
6290 -- OSD : Ada.Tags.Object_Specific_Data (Nb_Prims) :=
6291 -- (OSD_Table => (1 => <value>,
6292 -- ...
6293 -- N => <value>));
6295 if Nb_Prim = 0
6296 or else Is_Abstract_Type (Typ)
6297 or else Is_Controlled (Typ)
6298 or else Restriction_Active (No_Dispatching_Calls)
6299 or else not Is_Limited_Type (Typ)
6300 or else not Has_Interfaces (Typ)
6301 or else not RTE_Record_Component_Available (RE_OSD_Table)
6302 then
6303 -- No OSD table required
6305 return Make_Null (Loc);
6307 else
6308 OSD_Aggr_List := New_List;
6310 declare
6311 Prim_Table : array (Nat range 1 .. Nb_Prim) of Entity_Id;
6312 Prim : Entity_Id;
6313 Prim_Alias : Entity_Id;
6314 Prim_Elmt : Elmt_Id;
6315 E : Entity_Id;
6316 Count : Nat := 0;
6317 Pos : Nat;
6319 begin
6320 Prim_Table := (others => Empty);
6321 Prim_Alias := Empty;
6323 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6324 while Present (Prim_Elmt) loop
6325 Prim := Node (Prim_Elmt);
6327 if Present (Interface_Alias (Prim))
6328 and then Find_Dispatching_Type
6329 (Interface_Alias (Prim)) = Iface
6330 then
6331 Prim_Alias := Interface_Alias (Prim);
6332 E := Ultimate_Alias (Prim);
6333 Pos := UI_To_Int (DT_Position (Prim_Alias));
6335 if Present (Prim_Table (Pos)) then
6336 pragma Assert (Prim_Table (Pos) = E);
6337 null;
6339 else
6340 Prim_Table (Pos) := E;
6342 Append_To (OSD_Aggr_List,
6343 Make_Component_Association (Loc,
6344 Choices => New_List (
6345 Make_Integer_Literal (Loc,
6346 DT_Position (Prim_Alias))),
6347 Expression =>
6348 Make_Integer_Literal (Loc,
6349 DT_Position (Alias (Prim)))));
6351 Count := Count + 1;
6352 end if;
6353 end if;
6355 Next_Elmt (Prim_Elmt);
6356 end loop;
6358 pragma Assert (Count = Nb_Prim);
6359 end;
6361 OSD := Make_Temporary (Loc, 'I');
6363 Append_To (Result,
6364 Make_Object_Declaration (Loc,
6365 Defining_Identifier => OSD,
6366 Aliased_Present => True,
6367 Constant_Present => True,
6368 Object_Definition =>
6369 Make_Subtype_Indication (Loc,
6370 Subtype_Mark =>
6371 New_Occurrence_Of (RTE (RE_Object_Specific_Data), Loc),
6372 Constraint =>
6373 Make_Index_Or_Discriminant_Constraint (Loc,
6374 Constraints => New_List (
6375 Make_Integer_Literal (Loc, Nb_Prim)))),
6377 Expression =>
6378 Make_Aggregate (Loc,
6379 Component_Associations => New_List (
6380 Make_Component_Association (Loc,
6381 Choices => New_List (
6382 New_Occurrence_Of
6383 (RTE_Record_Component (RE_OSD_Num_Prims), Loc)),
6384 Expression =>
6385 Make_Integer_Literal (Loc, Nb_Prim)),
6387 Make_Component_Association (Loc,
6388 Choices => New_List (
6389 New_Occurrence_Of
6390 (RTE_Record_Component (RE_OSD_Table), Loc)),
6391 Expression => Make_Aggregate (Loc,
6392 Component_Associations => OSD_Aggr_List))))));
6394 return
6395 Make_Attribute_Reference (Loc,
6396 Prefix => New_Occurrence_Of (OSD, Loc),
6397 Attribute_Name => Name_Unchecked_Access);
6398 end if;
6399 end Make_OSD;
6401 -- Local variables
6403 Nb_Prim : constant Nat := Count_Primitives (Typ);
6404 AI : Elmt_Id;
6405 I_Depth : Nat;
6406 Iface_Table_Node : Node_Id;
6407 Num_Ifaces : Nat;
6408 TSD_Aggr_List : List_Id;
6409 Typ_Ifaces : Elist_Id;
6410 TSD_Tags_List : List_Id;
6412 Tname : constant Name_Id := Chars (Typ);
6413 Name_SSD : constant Name_Id :=
6414 New_External_Name (Tname, 'S', Suffix_Index => -1);
6415 Name_TSD : constant Name_Id :=
6416 New_External_Name (Tname, 'B', Suffix_Index => -1);
6417 SSD : constant Entity_Id :=
6418 Make_Defining_Identifier (Loc, Name_SSD);
6419 TSD : constant Entity_Id :=
6420 Make_Defining_Identifier (Loc, Name_TSD);
6421 begin
6422 -- Generate code to create the storage for the type specific data object
6423 -- with enough space to store the tags of the ancestors plus the tags
6424 -- of all the implemented interfaces (as described in a-tags.ads).
6426 -- TSD : Type_Specific_Data (I_Depth) :=
6427 -- (Idepth => I_Depth,
6428 -- Tag_Kind => <tag_kind-value>,
6429 -- Access_Level => Type_Access_Level (Typ),
6430 -- Alignment => Typ'Alignment,
6431 -- HT_Link => null,
6432 -- Type_Is_Abstract => <<boolean-value>>,
6433 -- Type_Is_Library_Level => <<boolean-value>>,
6434 -- Interfaces_Table => <<access-value>>
6435 -- SSD => SSD_Table'Address
6436 -- Tags_Table => (0 => Typ'Tag,
6437 -- 1 => Parent'Tag
6438 -- ...));
6440 TSD_Aggr_List := New_List;
6442 -- Idepth: Count ancestors to compute the inheritance depth. For private
6443 -- extensions, always go to the full view in order to compute the real
6444 -- inheritance depth.
6446 declare
6447 Current_Typ : Entity_Id;
6448 Parent_Typ : Entity_Id;
6450 begin
6451 I_Depth := 0;
6452 Current_Typ := Typ;
6453 loop
6454 Parent_Typ := Etype (Current_Typ);
6456 if Is_Private_Type (Parent_Typ) then
6457 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6458 end if;
6460 exit when Parent_Typ = Current_Typ;
6462 I_Depth := I_Depth + 1;
6463 Current_Typ := Parent_Typ;
6464 end loop;
6465 end;
6467 -- I_Depth
6469 Append_To (TSD_Aggr_List,
6470 Make_Integer_Literal (Loc, I_Depth));
6472 -- Tag_Kind
6474 Append_To (TSD_Aggr_List, Tagged_Kind (Typ));
6476 -- Access_Level
6478 Append_To (TSD_Aggr_List,
6479 Make_Integer_Literal (Loc, Type_Access_Level (Typ)));
6481 -- Alignment
6483 -- For CPP types we cannot rely on the value of 'Alignment provided
6484 -- by the backend to initialize this TSD field. Why not???
6486 if Convention (Typ) = Convention_CPP
6487 or else Is_CPP_Class (Root_Type (Typ))
6488 then
6489 Append_To (TSD_Aggr_List,
6490 Make_Integer_Literal (Loc, 0));
6491 else
6492 Append_To (TSD_Aggr_List,
6493 Make_Attribute_Reference (Loc,
6494 Prefix => New_Occurrence_Of (Typ, Loc),
6495 Attribute_Name => Name_Alignment));
6496 end if;
6498 -- HT_Link
6500 Append_To (TSD_Aggr_List,
6501 Make_Null (Loc));
6503 -- Type_Is_Abstract (Ada 2012: AI05-0173)
6505 declare
6506 Type_Is_Abstract : Entity_Id;
6508 begin
6509 Type_Is_Abstract :=
6510 Boolean_Literals (Is_Abstract_Type (Typ));
6512 Append_To (TSD_Aggr_List,
6513 New_Occurrence_Of (Type_Is_Abstract, Loc));
6514 end;
6516 -- Type_Is_Library_Level
6518 declare
6519 Type_Is_Library_Level : Entity_Id;
6520 begin
6521 Type_Is_Library_Level :=
6522 Boolean_Literals (Is_Library_Level_Entity (Typ));
6523 Append_To (TSD_Aggr_List,
6524 New_Occurrence_Of (Type_Is_Library_Level, Loc));
6525 end;
6527 -- Interfaces_Table (required for AI-405)
6529 if RTE_Record_Component_Available (RE_Interfaces_Table) then
6531 -- Count the number of interface types implemented by Typ
6533 Collect_Interfaces (Typ, Typ_Ifaces);
6535 Num_Ifaces := 0;
6536 AI := First_Elmt (Typ_Ifaces);
6537 while Present (AI) loop
6538 Num_Ifaces := Num_Ifaces + 1;
6539 Next_Elmt (AI);
6540 end loop;
6542 if Num_Ifaces = 0 then
6543 Iface_Table_Node := Make_Null (Loc);
6545 -- Generate the Interface_Table object
6547 else
6548 declare
6549 TSD_Ifaces_List : constant List_Id := New_List;
6550 Iface : Entity_Id;
6551 ITable : Node_Id;
6553 begin
6554 AI := First_Elmt (Typ_Ifaces);
6555 while Present (AI) loop
6556 Iface := Node (AI);
6558 Append_To (TSD_Ifaces_List,
6559 Make_Aggregate (Loc,
6560 Expressions => New_List (
6562 -- Iface_Tag
6564 Make_Attribute_Reference (Loc,
6565 Prefix => New_Occurrence_Of (Iface, Loc),
6566 Attribute_Name => Name_Tag),
6568 -- OSD
6570 Make_OSD (Iface))));
6572 Next_Elmt (AI);
6573 end loop;
6575 ITable := Make_Temporary (Loc, 'I');
6577 Append_To (Result,
6578 Make_Object_Declaration (Loc,
6579 Defining_Identifier => ITable,
6580 Aliased_Present => True,
6581 Constant_Present => True,
6582 Object_Definition =>
6583 Make_Subtype_Indication (Loc,
6584 Subtype_Mark =>
6585 New_Occurrence_Of (RTE (RE_Interface_Data), Loc),
6586 Constraint => Make_Index_Or_Discriminant_Constraint
6587 (Loc,
6588 Constraints => New_List (
6589 Make_Integer_Literal (Loc, Num_Ifaces)))),
6591 Expression => Make_Aggregate (Loc,
6592 Expressions => New_List (
6593 Make_Integer_Literal (Loc, Num_Ifaces),
6594 Make_Aggregate (Loc,
6595 Expressions => TSD_Ifaces_List)))));
6597 Iface_Table_Node :=
6598 Make_Attribute_Reference (Loc,
6599 Prefix => New_Occurrence_Of (ITable, Loc),
6600 Attribute_Name => Name_Unchecked_Access);
6601 end;
6602 end if;
6604 Append_To (TSD_Aggr_List, Iface_Table_Node);
6605 end if;
6607 -- Generate the Select Specific Data table for synchronized types that
6608 -- implement synchronized interfaces. The size of the table is
6609 -- constrained by the number of non-predefined primitive operations.
6611 if RTE_Record_Component_Available (RE_SSD) then
6612 if Ada_Version >= Ada_2005
6613 and then Has_DT (Typ)
6614 and then Is_Concurrent_Record_Type (Typ)
6615 and then Has_Interfaces (Typ)
6616 and then Nb_Prim > 0
6617 and then not Is_Abstract_Type (Typ)
6618 and then not Is_Controlled (Typ)
6619 and then not Restriction_Active (No_Dispatching_Calls)
6620 and then not Restriction_Active (No_Select_Statements)
6621 then
6622 Append_To (Result,
6623 Make_Object_Declaration (Loc,
6624 Defining_Identifier => SSD,
6625 Aliased_Present => True,
6626 Object_Definition =>
6627 Make_Subtype_Indication (Loc,
6628 Subtype_Mark => New_Occurrence_Of (
6629 RTE (RE_Select_Specific_Data), Loc),
6630 Constraint =>
6631 Make_Index_Or_Discriminant_Constraint (Loc,
6632 Constraints => New_List (
6633 Make_Integer_Literal (Loc, Nb_Prim))))));
6635 -- This table is initialized by Make_Select_Specific_Data_Table,
6636 -- which calls Set_Entry_Index and Set_Prim_Op_Kind.
6638 Append_To (TSD_Aggr_List,
6639 Make_Attribute_Reference (Loc,
6640 Prefix => New_Occurrence_Of (SSD, Loc),
6641 Attribute_Name => Name_Unchecked_Access));
6642 else
6643 Append_To (TSD_Aggr_List, Make_Null (Loc));
6644 end if;
6645 end if;
6647 -- Initialize the table of ancestor tags. In case of interface types
6648 -- this table is not needed.
6650 TSD_Tags_List := New_List;
6652 -- Fill position 0 with Typ'Tag
6654 Append_To (TSD_Tags_List,
6655 Make_Attribute_Reference (Loc,
6656 Prefix => New_Occurrence_Of (Typ, Loc),
6657 Attribute_Name => Name_Tag));
6659 -- Fill the rest of the table with the tags of the ancestors
6661 declare
6662 Current_Typ : Entity_Id;
6663 Parent_Typ : Entity_Id;
6664 Pos : Nat;
6666 begin
6667 Pos := 1;
6668 Current_Typ := Typ;
6670 loop
6671 Parent_Typ := Etype (Current_Typ);
6673 if Is_Private_Type (Parent_Typ) then
6674 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6675 end if;
6677 exit when Parent_Typ = Current_Typ;
6679 Append_To (TSD_Tags_List,
6680 Make_Attribute_Reference (Loc,
6681 Prefix => New_Occurrence_Of (Parent_Typ, Loc),
6682 Attribute_Name => Name_Tag));
6684 Pos := Pos + 1;
6685 Current_Typ := Parent_Typ;
6686 end loop;
6688 pragma Assert (Pos = I_Depth + 1);
6689 end;
6691 Append_To (TSD_Aggr_List,
6692 Make_Aggregate (Loc,
6693 Expressions => TSD_Tags_List));
6695 -- Build the TSD object
6697 Append_To (Result,
6698 Make_Object_Declaration (Loc,
6699 Defining_Identifier => TSD,
6700 Aliased_Present => True,
6701 Constant_Present => True,
6702 Object_Definition =>
6703 Make_Subtype_Indication (Loc,
6704 Subtype_Mark => New_Occurrence_Of (
6705 RTE (RE_Type_Specific_Data), Loc),
6706 Constraint =>
6707 Make_Index_Or_Discriminant_Constraint (Loc,
6708 Constraints => New_List (
6709 Make_Integer_Literal (Loc, I_Depth)))),
6711 Expression => Make_Aggregate (Loc,
6712 Expressions => TSD_Aggr_List)));
6714 -- Generate:
6715 -- Check_TSD (TSD => TSD'Unrestricted_Access);
6717 if Ada_Version >= Ada_2005
6718 and then Is_Library_Level_Entity (Typ)
6719 and then RTE_Available (RE_Check_TSD)
6720 and then not Duplicated_Tag_Checks_Suppressed (Typ)
6721 then
6722 Append_To (Result,
6723 Make_Procedure_Call_Statement (Loc,
6724 Name => New_Occurrence_Of (RTE (RE_Check_TSD), Loc),
6725 Parameter_Associations => New_List (
6726 Make_Attribute_Reference (Loc,
6727 Prefix => New_Occurrence_Of (TSD, Loc),
6728 Attribute_Name => Name_Unrestricted_Access))));
6729 end if;
6731 -- Generate:
6732 -- Register_TSD (TSD'Unrestricted_Access);
6734 Append_To (Result,
6735 Make_Procedure_Call_Statement (Loc,
6736 Name => New_Occurrence_Of (RTE (RE_Register_TSD), Loc),
6737 Parameter_Associations => New_List (
6738 Make_Attribute_Reference (Loc,
6739 Prefix => New_Occurrence_Of (TSD, Loc),
6740 Attribute_Name => Name_Unrestricted_Access))));
6742 -- Populate the two auxiliary tables used for dispatching asynchronous,
6743 -- conditional and timed selects for synchronized types that implement
6744 -- a limited interface. Skip this step in Ravenscar profile or when
6745 -- general dispatching is forbidden.
6747 if Ada_Version >= Ada_2005
6748 and then Is_Concurrent_Record_Type (Typ)
6749 and then Has_Interfaces (Typ)
6750 and then not Restriction_Active (No_Dispatching_Calls)
6751 and then not Restriction_Active (No_Select_Statements)
6752 then
6753 Append_List_To (Result,
6754 Make_Select_Specific_Data_Table (Typ));
6755 end if;
6757 return Result;
6758 end Make_VM_TSD;
6760 -------------------------------------
6761 -- Make_Select_Specific_Data_Table --
6762 -------------------------------------
6764 function Make_Select_Specific_Data_Table
6765 (Typ : Entity_Id) return List_Id
6767 Assignments : constant List_Id := New_List;
6768 Loc : constant Source_Ptr := Sloc (Typ);
6770 Conc_Typ : Entity_Id;
6771 Decls : List_Id;
6772 Prim : Entity_Id;
6773 Prim_Als : Entity_Id;
6774 Prim_Elmt : Elmt_Id;
6775 Prim_Pos : Uint;
6776 Nb_Prim : Nat := 0;
6778 type Examined_Array is array (Int range <>) of Boolean;
6780 function Find_Entry_Index (E : Entity_Id) return Uint;
6781 -- Given an entry, find its index in the visible declarations of the
6782 -- corresponding concurrent type of Typ.
6784 ----------------------
6785 -- Find_Entry_Index --
6786 ----------------------
6788 function Find_Entry_Index (E : Entity_Id) return Uint is
6789 Index : Uint := Uint_1;
6790 Subp_Decl : Entity_Id;
6792 begin
6793 if Present (Decls)
6794 and then not Is_Empty_List (Decls)
6795 then
6796 Subp_Decl := First (Decls);
6797 while Present (Subp_Decl) loop
6798 if Nkind (Subp_Decl) = N_Entry_Declaration then
6799 if Defining_Identifier (Subp_Decl) = E then
6800 return Index;
6801 end if;
6803 Index := Index + 1;
6804 end if;
6806 Next (Subp_Decl);
6807 end loop;
6808 end if;
6810 return Uint_0;
6811 end Find_Entry_Index;
6813 -- Local variables
6815 Tag_Node : Node_Id;
6817 -- Start of processing for Make_Select_Specific_Data_Table
6819 begin
6820 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
6822 if Present (Corresponding_Concurrent_Type (Typ)) then
6823 Conc_Typ := Corresponding_Concurrent_Type (Typ);
6825 if Present (Full_View (Conc_Typ)) then
6826 Conc_Typ := Full_View (Conc_Typ);
6827 end if;
6829 if Ekind (Conc_Typ) = E_Protected_Type then
6830 Decls := Visible_Declarations (Protected_Definition (
6831 Parent (Conc_Typ)));
6832 else
6833 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
6834 Decls := Visible_Declarations (Task_Definition (
6835 Parent (Conc_Typ)));
6836 end if;
6837 end if;
6839 -- Count the non-predefined primitive operations
6841 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6842 while Present (Prim_Elmt) loop
6843 Prim := Node (Prim_Elmt);
6845 if not (Is_Predefined_Dispatching_Operation (Prim)
6846 or else Is_Predefined_Dispatching_Alias (Prim))
6847 then
6848 Nb_Prim := Nb_Prim + 1;
6849 end if;
6851 Next_Elmt (Prim_Elmt);
6852 end loop;
6854 declare
6855 Examined : Examined_Array (1 .. Nb_Prim) := (others => False);
6857 begin
6858 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6859 while Present (Prim_Elmt) loop
6860 Prim := Node (Prim_Elmt);
6862 -- Look for primitive overriding an abstract interface subprogram
6864 if Present (Interface_Alias (Prim))
6865 and then not
6866 Is_Ancestor
6867 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
6868 Use_Full_View => True)
6869 and then not Examined (UI_To_Int (DT_Position (Alias (Prim))))
6870 then
6871 Prim_Pos := DT_Position (Alias (Prim));
6872 pragma Assert (UI_To_Int (Prim_Pos) <= Nb_Prim);
6873 Examined (UI_To_Int (Prim_Pos)) := True;
6875 -- Set the primitive operation kind regardless of subprogram
6876 -- type. Generate:
6877 -- Ada.Tags.Set_Prim_Op_Kind (DT_Ptr, <position>, <kind>);
6879 if Tagged_Type_Expansion then
6880 Tag_Node :=
6881 New_Occurrence_Of
6882 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6884 else
6885 Tag_Node :=
6886 Make_Attribute_Reference (Loc,
6887 Prefix => New_Occurrence_Of (Typ, Loc),
6888 Attribute_Name => Name_Tag);
6889 end if;
6891 Append_To (Assignments,
6892 Make_Procedure_Call_Statement (Loc,
6893 Name => New_Occurrence_Of (RTE (RE_Set_Prim_Op_Kind), Loc),
6894 Parameter_Associations => New_List (
6895 Tag_Node,
6896 Make_Integer_Literal (Loc, Prim_Pos),
6897 Prim_Op_Kind (Alias (Prim), Typ))));
6899 -- Retrieve the root of the alias chain
6901 Prim_Als := Ultimate_Alias (Prim);
6903 -- In the case of an entry wrapper, set the entry index
6905 if Ekind (Prim) = E_Procedure
6906 and then Is_Primitive_Wrapper (Prim_Als)
6907 and then Ekind (Wrapped_Entity (Prim_Als)) = E_Entry
6908 then
6909 -- Generate:
6910 -- Ada.Tags.Set_Entry_Index
6911 -- (DT_Ptr, <position>, <index>);
6913 if Tagged_Type_Expansion then
6914 Tag_Node :=
6915 New_Occurrence_Of
6916 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6917 else
6918 Tag_Node :=
6919 Make_Attribute_Reference (Loc,
6920 Prefix => New_Occurrence_Of (Typ, Loc),
6921 Attribute_Name => Name_Tag);
6922 end if;
6924 Append_To (Assignments,
6925 Make_Procedure_Call_Statement (Loc,
6926 Name =>
6927 New_Occurrence_Of (RTE (RE_Set_Entry_Index), Loc),
6928 Parameter_Associations => New_List (
6929 Tag_Node,
6930 Make_Integer_Literal (Loc, Prim_Pos),
6931 Make_Integer_Literal (Loc,
6932 Find_Entry_Index (Wrapped_Entity (Prim_Als))))));
6933 end if;
6934 end if;
6936 Next_Elmt (Prim_Elmt);
6937 end loop;
6938 end;
6940 return Assignments;
6941 end Make_Select_Specific_Data_Table;
6943 ---------------
6944 -- Make_Tags --
6945 ---------------
6947 function Make_Tags (Typ : Entity_Id) return List_Id is
6948 Loc : constant Source_Ptr := Sloc (Typ);
6949 Result : constant List_Id := New_List;
6951 procedure Import_DT
6952 (Tag_Typ : Entity_Id;
6953 DT : Entity_Id;
6954 Is_Secondary_DT : Boolean);
6955 -- Import the dispatch table DT of tagged type Tag_Typ. Required to
6956 -- generate forward references and statically allocate the table. For
6957 -- primary dispatch tables that require no dispatch table generate:
6959 -- DT : static aliased constant Non_Dispatch_Table_Wrapper;
6960 -- pragma Import (Ada, DT);
6962 -- Otherwise generate:
6964 -- DT : static aliased constant Dispatch_Table_Wrapper (Nb_Prim);
6965 -- pragma Import (Ada, DT);
6967 ---------------
6968 -- Import_DT --
6969 ---------------
6971 procedure Import_DT
6972 (Tag_Typ : Entity_Id;
6973 DT : Entity_Id;
6974 Is_Secondary_DT : Boolean)
6976 DT_Constr_List : List_Id;
6977 Nb_Prim : Nat;
6979 begin
6980 Set_Is_Imported (DT);
6981 Set_Ekind (DT, E_Constant);
6982 Set_Related_Type (DT, Typ);
6984 -- The scope must be set now to call Get_External_Name
6986 Set_Scope (DT, Current_Scope);
6988 Get_External_Name (DT);
6989 Set_Interface_Name (DT,
6990 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
6992 -- Ensure proper Sprint output of this implicit importation
6994 Set_Is_Internal (DT);
6996 -- Save this entity to allow Make_DT to generate its exportation
6998 Append_Elmt (DT, Dispatch_Table_Wrappers (Typ));
7000 -- No dispatch table required
7002 if not Is_Secondary_DT and then not Has_DT (Tag_Typ) then
7003 Append_To (Result,
7004 Make_Object_Declaration (Loc,
7005 Defining_Identifier => DT,
7006 Aliased_Present => True,
7007 Constant_Present => True,
7008 Object_Definition =>
7009 New_Occurrence_Of
7010 (RTE (RE_No_Dispatch_Table_Wrapper), Loc)));
7012 else
7013 -- Calculate the number of primitives of the dispatch table and
7014 -- the size of the Type_Specific_Data record.
7016 Nb_Prim :=
7017 UI_To_Int (DT_Entry_Count (First_Tag_Component (Tag_Typ)));
7019 -- If the tagged type has no primitives we add a dummy slot whose
7020 -- address will be the tag of this type.
7022 if Nb_Prim = 0 then
7023 DT_Constr_List :=
7024 New_List (Make_Integer_Literal (Loc, 1));
7025 else
7026 DT_Constr_List :=
7027 New_List (Make_Integer_Literal (Loc, Nb_Prim));
7028 end if;
7030 Append_To (Result,
7031 Make_Object_Declaration (Loc,
7032 Defining_Identifier => DT,
7033 Aliased_Present => True,
7034 Constant_Present => True,
7035 Object_Definition =>
7036 Make_Subtype_Indication (Loc,
7037 Subtype_Mark =>
7038 New_Occurrence_Of (RTE (RE_Dispatch_Table_Wrapper), Loc),
7039 Constraint => Make_Index_Or_Discriminant_Constraint (Loc,
7040 Constraints => DT_Constr_List))));
7041 end if;
7042 end Import_DT;
7044 -- Local variables
7046 Tname : constant Name_Id := Chars (Typ);
7047 AI_Tag_Comp : Elmt_Id;
7048 DT : Node_Id := Empty;
7049 DT_Ptr : Node_Id;
7050 Predef_Prims_Ptr : Node_Id;
7051 Iface_DT : Node_Id := Empty;
7052 Iface_DT_Ptr : Node_Id;
7053 New_Node : Node_Id;
7054 Suffix_Index : Int;
7055 Typ_Name : Name_Id;
7056 Typ_Comps : Elist_Id;
7058 -- Start of processing for Make_Tags
7060 begin
7061 pragma Assert (No (Access_Disp_Table (Typ)));
7062 Set_Access_Disp_Table (Typ, New_Elmt_List);
7064 -- 1) Generate the primary tag entities
7066 -- Primary dispatch table containing user-defined primitives
7068 DT_Ptr := Make_Defining_Identifier (Loc, New_External_Name (Tname, 'P'));
7069 Set_Etype (DT_Ptr, RTE (RE_Tag));
7070 Append_Elmt (DT_Ptr, Access_Disp_Table (Typ));
7072 -- Minimum decoration
7074 Set_Ekind (DT_Ptr, E_Variable);
7075 Set_Related_Type (DT_Ptr, Typ);
7077 -- Notify back end that the types are associated with a dispatch table
7079 Set_Is_Dispatch_Table_Entity (RTE (RE_Prim_Ptr));
7080 Set_Is_Dispatch_Table_Entity (RTE (RE_Predef_Prims_Table_Ptr));
7082 -- For CPP types there is no need to build the dispatch tables since
7083 -- they are imported from the C++ side. If the CPP type has an IP then
7084 -- we declare now the variable that will store the copy of the C++ tag.
7085 -- If the CPP type is an interface, we need the variable as well because
7086 -- it becomes the pointer to the corresponding secondary table.
7088 if Is_CPP_Class (Typ) then
7089 if Has_CPP_Constructors (Typ) or else Is_Interface (Typ) then
7090 Append_To (Result,
7091 Make_Object_Declaration (Loc,
7092 Defining_Identifier => DT_Ptr,
7093 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
7094 Expression =>
7095 Unchecked_Convert_To (RTE (RE_Tag),
7096 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
7098 Set_Is_Statically_Allocated (DT_Ptr,
7099 Is_Library_Level_Tagged_Type (Typ));
7100 end if;
7102 -- Ada types
7104 else
7105 -- Primary dispatch table containing predefined primitives
7107 Predef_Prims_Ptr :=
7108 Make_Defining_Identifier (Loc,
7109 Chars => New_External_Name (Tname, 'Y'));
7110 Set_Etype (Predef_Prims_Ptr, RTE (RE_Address));
7111 Append_Elmt (Predef_Prims_Ptr, Access_Disp_Table (Typ));
7113 -- Import the forward declaration of the Dispatch Table wrapper
7114 -- record (Make_DT will take care of exporting it).
7116 if Building_Static_DT (Typ) then
7117 Set_Dispatch_Table_Wrappers (Typ, New_Elmt_List);
7119 DT :=
7120 Make_Defining_Identifier (Loc,
7121 Chars => New_External_Name (Tname, 'T'));
7123 Import_DT (Typ, DT, Is_Secondary_DT => False);
7125 if Has_DT (Typ) then
7126 Append_To (Result,
7127 Make_Object_Declaration (Loc,
7128 Defining_Identifier => DT_Ptr,
7129 Constant_Present => True,
7130 Object_Definition =>
7131 New_Occurrence_Of (RTE (RE_Tag), Loc),
7132 Expression =>
7133 Unchecked_Convert_To (RTE (RE_Tag),
7134 Make_Attribute_Reference (Loc,
7135 Prefix =>
7136 Make_Selected_Component (Loc,
7137 Prefix => New_Occurrence_Of (DT, Loc),
7138 Selector_Name =>
7139 New_Occurrence_Of
7140 (RTE_Record_Component (RE_Prims_Ptr), Loc)),
7141 Attribute_Name => Name_Address))));
7143 -- Generate the SCIL node for the previous object declaration
7144 -- because it has a tag initialization.
7146 if Generate_SCIL then
7147 New_Node :=
7148 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
7149 Set_SCIL_Entity (New_Node, Typ);
7150 Set_SCIL_Node (Last (Result), New_Node);
7151 end if;
7153 Append_To (Result,
7154 Make_Object_Declaration (Loc,
7155 Defining_Identifier => Predef_Prims_Ptr,
7156 Constant_Present => True,
7157 Object_Definition =>
7158 New_Occurrence_Of (RTE (RE_Address), Loc),
7159 Expression =>
7160 Make_Attribute_Reference (Loc,
7161 Prefix =>
7162 Make_Selected_Component (Loc,
7163 Prefix => New_Occurrence_Of (DT, Loc),
7164 Selector_Name =>
7165 New_Occurrence_Of
7166 (RTE_Record_Component (RE_Predef_Prims), Loc)),
7167 Attribute_Name => Name_Address)));
7169 -- No dispatch table required
7171 else
7172 Append_To (Result,
7173 Make_Object_Declaration (Loc,
7174 Defining_Identifier => DT_Ptr,
7175 Constant_Present => True,
7176 Object_Definition =>
7177 New_Occurrence_Of (RTE (RE_Tag), Loc),
7178 Expression =>
7179 Unchecked_Convert_To (RTE (RE_Tag),
7180 Make_Attribute_Reference (Loc,
7181 Prefix =>
7182 Make_Selected_Component (Loc,
7183 Prefix => New_Occurrence_Of (DT, Loc),
7184 Selector_Name =>
7185 New_Occurrence_Of
7186 (RTE_Record_Component (RE_NDT_Prims_Ptr),
7187 Loc)),
7188 Attribute_Name => Name_Address))));
7189 end if;
7191 Set_Is_True_Constant (DT_Ptr);
7192 Set_Is_Statically_Allocated (DT_Ptr);
7193 end if;
7194 end if;
7196 -- 2) Generate the secondary tag entities
7198 -- Collect the components associated with secondary dispatch tables
7200 if Has_Interfaces (Typ) then
7201 Collect_Interface_Components (Typ, Typ_Comps);
7203 -- For each interface type we build a unique external name associated
7204 -- with its secondary dispatch table. This name is used to declare an
7205 -- object that references this secondary dispatch table, whose value
7206 -- will be used for the elaboration of Typ objects, and also for the
7207 -- elaboration of objects of types derived from Typ that do not
7208 -- override the primitives of this interface type.
7210 Suffix_Index := 1;
7212 -- Note: The value of Suffix_Index must be in sync with the values of
7213 -- Suffix_Index in secondary dispatch tables generated by Make_DT.
7215 if Is_CPP_Class (Typ) then
7216 AI_Tag_Comp := First_Elmt (Typ_Comps);
7217 while Present (AI_Tag_Comp) loop
7218 Get_Secondary_DT_External_Name
7219 (Typ, Related_Type (Node (AI_Tag_Comp)), Suffix_Index);
7220 Typ_Name := Name_Find;
7222 -- Declare variables to store copy of the C++ secondary tags
7224 Iface_DT_Ptr :=
7225 Make_Defining_Identifier (Loc,
7226 Chars => New_External_Name (Typ_Name, 'P'));
7227 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7228 Set_Ekind (Iface_DT_Ptr, E_Variable);
7229 Set_Is_Tag (Iface_DT_Ptr);
7231 Set_Has_Thunks (Iface_DT_Ptr);
7232 Set_Related_Type
7233 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7234 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7236 Append_To (Result,
7237 Make_Object_Declaration (Loc,
7238 Defining_Identifier => Iface_DT_Ptr,
7239 Object_Definition => New_Occurrence_Of
7240 (RTE (RE_Interface_Tag), Loc),
7241 Expression =>
7242 Unchecked_Convert_To (RTE (RE_Interface_Tag),
7243 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
7245 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7246 Is_Library_Level_Tagged_Type (Typ));
7248 Next_Elmt (AI_Tag_Comp);
7249 end loop;
7251 -- This is not a CPP_Class type
7253 else
7254 AI_Tag_Comp := First_Elmt (Typ_Comps);
7255 while Present (AI_Tag_Comp) loop
7256 Get_Secondary_DT_External_Name
7257 (Typ, Related_Type (Node (AI_Tag_Comp)), Suffix_Index);
7258 Typ_Name := Name_Find;
7260 if Building_Static_DT (Typ) then
7261 Iface_DT :=
7262 Make_Defining_Identifier (Loc,
7263 Chars => New_External_Name
7264 (Typ_Name, 'T', Suffix_Index => -1));
7265 Import_DT
7266 (Tag_Typ => Related_Type (Node (AI_Tag_Comp)),
7267 DT => Iface_DT,
7268 Is_Secondary_DT => True);
7269 end if;
7271 -- Secondary dispatch table referencing thunks to user-defined
7272 -- primitives covered by this interface.
7274 Iface_DT_Ptr :=
7275 Make_Defining_Identifier (Loc,
7276 Chars => New_External_Name (Typ_Name, 'P'));
7277 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7278 Set_Ekind (Iface_DT_Ptr, E_Constant);
7279 Set_Is_Tag (Iface_DT_Ptr);
7280 Set_Has_Thunks (Iface_DT_Ptr);
7281 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7282 Is_Library_Level_Tagged_Type (Typ));
7283 Set_Is_True_Constant (Iface_DT_Ptr);
7284 Set_Related_Type
7285 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7286 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7288 if Building_Static_DT (Typ) then
7289 Append_To (Result,
7290 Make_Object_Declaration (Loc,
7291 Defining_Identifier => Iface_DT_Ptr,
7292 Constant_Present => True,
7293 Object_Definition => New_Occurrence_Of
7294 (RTE (RE_Interface_Tag), Loc),
7295 Expression =>
7296 Unchecked_Convert_To (RTE (RE_Interface_Tag),
7297 Make_Attribute_Reference (Loc,
7298 Prefix =>
7299 Make_Selected_Component (Loc,
7300 Prefix =>
7301 New_Occurrence_Of (Iface_DT, Loc),
7302 Selector_Name =>
7303 New_Occurrence_Of
7304 (RTE_Record_Component (RE_Prims_Ptr),
7305 Loc)),
7306 Attribute_Name => Name_Address))));
7307 end if;
7309 -- Secondary dispatch table referencing thunks to predefined
7310 -- primitives.
7312 Iface_DT_Ptr :=
7313 Make_Defining_Identifier (Loc,
7314 Chars => New_External_Name (Typ_Name, 'Y'));
7315 Set_Etype (Iface_DT_Ptr, RTE (RE_Address));
7316 Set_Ekind (Iface_DT_Ptr, E_Constant);
7317 Set_Is_Tag (Iface_DT_Ptr);
7318 Set_Has_Thunks (Iface_DT_Ptr);
7319 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7320 Is_Library_Level_Tagged_Type (Typ));
7321 Set_Is_True_Constant (Iface_DT_Ptr);
7322 Set_Related_Type
7323 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7324 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7326 -- Secondary dispatch table referencing user-defined primitives
7327 -- covered by this interface.
7329 Iface_DT_Ptr :=
7330 Make_Defining_Identifier (Loc,
7331 Chars => New_External_Name (Typ_Name, 'D'));
7332 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7333 Set_Ekind (Iface_DT_Ptr, E_Constant);
7334 Set_Is_Tag (Iface_DT_Ptr);
7335 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7336 Is_Library_Level_Tagged_Type (Typ));
7337 Set_Is_True_Constant (Iface_DT_Ptr);
7338 Set_Related_Type
7339 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7340 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7342 -- Secondary dispatch table referencing predefined primitives
7344 Iface_DT_Ptr :=
7345 Make_Defining_Identifier (Loc,
7346 Chars => New_External_Name (Typ_Name, 'Z'));
7347 Set_Etype (Iface_DT_Ptr, RTE (RE_Address));
7348 Set_Ekind (Iface_DT_Ptr, E_Constant);
7349 Set_Is_Tag (Iface_DT_Ptr);
7350 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7351 Is_Library_Level_Tagged_Type (Typ));
7352 Set_Is_True_Constant (Iface_DT_Ptr);
7353 Set_Related_Type
7354 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7355 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7357 Next_Elmt (AI_Tag_Comp);
7358 end loop;
7359 end if;
7360 end if;
7362 -- 3) At the end of Access_Disp_Table, if the type has user-defined
7363 -- primitives, we add the entity of an access type declaration that
7364 -- is used by Build_Get_Prim_Op_Address to expand dispatching calls
7365 -- through the primary dispatch table.
7367 if UI_To_Int (DT_Entry_Count (First_Tag_Component (Typ))) = 0 then
7368 Analyze_List (Result);
7370 -- Generate:
7371 -- type Typ_DT is array (1 .. Nb_Prims) of Prim_Ptr;
7372 -- type Typ_DT_Acc is access Typ_DT;
7374 else
7375 declare
7376 Name_DT_Prims : constant Name_Id :=
7377 New_External_Name (Tname, 'G');
7378 Name_DT_Prims_Acc : constant Name_Id :=
7379 New_External_Name (Tname, 'H');
7380 DT_Prims : constant Entity_Id :=
7381 Make_Defining_Identifier (Loc,
7382 Name_DT_Prims);
7383 DT_Prims_Acc : constant Entity_Id :=
7384 Make_Defining_Identifier (Loc,
7385 Name_DT_Prims_Acc);
7386 begin
7387 Append_To (Result,
7388 Make_Full_Type_Declaration (Loc,
7389 Defining_Identifier => DT_Prims,
7390 Type_Definition =>
7391 Make_Constrained_Array_Definition (Loc,
7392 Discrete_Subtype_Definitions => New_List (
7393 Make_Range (Loc,
7394 Low_Bound => Make_Integer_Literal (Loc, 1),
7395 High_Bound => Make_Integer_Literal (Loc,
7396 DT_Entry_Count
7397 (First_Tag_Component (Typ))))),
7398 Component_Definition =>
7399 Make_Component_Definition (Loc,
7400 Subtype_Indication =>
7401 New_Occurrence_Of (RTE (RE_Prim_Ptr), Loc)))));
7403 Append_To (Result,
7404 Make_Full_Type_Declaration (Loc,
7405 Defining_Identifier => DT_Prims_Acc,
7406 Type_Definition =>
7407 Make_Access_To_Object_Definition (Loc,
7408 Subtype_Indication =>
7409 New_Occurrence_Of (DT_Prims, Loc))));
7411 Append_Elmt (DT_Prims_Acc, Access_Disp_Table (Typ));
7413 -- Analyze the resulting list and suppress the generation of the
7414 -- Init_Proc associated with the above array declaration because
7415 -- this type is never used in object declarations. It is only used
7416 -- to simplify the expansion associated with dispatching calls.
7418 Analyze_List (Result);
7419 Set_Suppress_Initialization (Base_Type (DT_Prims));
7421 -- Disable backend optimizations based on assumptions about the
7422 -- aliasing status of objects designated by the access to the
7423 -- dispatch table. Required to handle dispatch tables imported
7424 -- from C++.
7426 Set_No_Strict_Aliasing (Base_Type (DT_Prims_Acc));
7428 -- Add the freezing nodes of these declarations; required to avoid
7429 -- generating these freezing nodes in wrong scopes (for example in
7430 -- the IC routine of a derivation of Typ).
7432 -- What is an "IC routine"? Is "init_proc" meant here???
7434 Append_List_To (Result, Freeze_Entity (DT_Prims, Typ));
7435 Append_List_To (Result, Freeze_Entity (DT_Prims_Acc, Typ));
7437 -- Mark entity of dispatch table. Required by the back end to
7438 -- handle them properly.
7440 Set_Is_Dispatch_Table_Entity (DT_Prims);
7441 end;
7442 end if;
7444 -- Mark entities of dispatch table. Required by the back end to handle
7445 -- them properly.
7447 if Present (DT) then
7448 Set_Is_Dispatch_Table_Entity (DT);
7449 Set_Is_Dispatch_Table_Entity (Etype (DT));
7450 end if;
7452 if Present (Iface_DT) then
7453 Set_Is_Dispatch_Table_Entity (Iface_DT);
7454 Set_Is_Dispatch_Table_Entity (Etype (Iface_DT));
7455 end if;
7457 if Is_CPP_Class (Root_Type (Typ)) then
7458 Set_Ekind (DT_Ptr, E_Variable);
7459 else
7460 Set_Ekind (DT_Ptr, E_Constant);
7461 end if;
7463 Set_Is_Tag (DT_Ptr);
7464 Set_Related_Type (DT_Ptr, Typ);
7466 return Result;
7467 end Make_Tags;
7469 ---------------
7470 -- New_Value --
7471 ---------------
7473 function New_Value (From : Node_Id) return Node_Id is
7474 Res : constant Node_Id := Duplicate_Subexpr (From);
7475 begin
7476 if Is_Access_Type (Etype (From)) then
7477 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
7478 else
7479 return Res;
7480 end if;
7481 end New_Value;
7483 -----------------------------------
7484 -- Original_View_In_Visible_Part --
7485 -----------------------------------
7487 function Original_View_In_Visible_Part (Typ : Entity_Id) return Boolean is
7488 Scop : constant Entity_Id := Scope (Typ);
7490 begin
7491 -- The scope must be a package
7493 if not Is_Package_Or_Generic_Package (Scop) then
7494 return False;
7495 end if;
7497 -- A type with a private declaration has a private view declared in
7498 -- the visible part.
7500 if Has_Private_Declaration (Typ) then
7501 return True;
7502 end if;
7504 return List_Containing (Parent (Typ)) =
7505 Visible_Declarations (Package_Specification (Scop));
7506 end Original_View_In_Visible_Part;
7508 ------------------
7509 -- Prim_Op_Kind --
7510 ------------------
7512 function Prim_Op_Kind
7513 (Prim : Entity_Id;
7514 Typ : Entity_Id) return Node_Id
7516 Full_Typ : Entity_Id := Typ;
7517 Loc : constant Source_Ptr := Sloc (Prim);
7518 Prim_Op : Entity_Id;
7520 begin
7521 -- Retrieve the original primitive operation
7523 Prim_Op := Ultimate_Alias (Prim);
7525 if Ekind (Typ) = E_Record_Type
7526 and then Present (Corresponding_Concurrent_Type (Typ))
7527 then
7528 Full_Typ := Corresponding_Concurrent_Type (Typ);
7529 end if;
7531 -- When a private tagged type is completed by a concurrent type,
7532 -- retrieve the full view.
7534 if Is_Private_Type (Full_Typ) then
7535 Full_Typ := Full_View (Full_Typ);
7536 end if;
7538 if Ekind (Prim_Op) = E_Function then
7540 -- Protected function
7542 if Ekind (Full_Typ) = E_Protected_Type then
7543 return New_Occurrence_Of (RTE (RE_POK_Protected_Function), Loc);
7545 -- Task function
7547 elsif Ekind (Full_Typ) = E_Task_Type then
7548 return New_Occurrence_Of (RTE (RE_POK_Task_Function), Loc);
7550 -- Regular function
7552 else
7553 return New_Occurrence_Of (RTE (RE_POK_Function), Loc);
7554 end if;
7556 else
7557 pragma Assert (Ekind (Prim_Op) = E_Procedure);
7559 if Ekind (Full_Typ) = E_Protected_Type then
7561 -- Protected entry
7563 if Is_Primitive_Wrapper (Prim_Op)
7564 and then Ekind (Wrapped_Entity (Prim_Op)) = E_Entry
7565 then
7566 return New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc);
7568 -- Protected procedure
7570 else
7571 return
7572 New_Occurrence_Of (RTE (RE_POK_Protected_Procedure), Loc);
7573 end if;
7575 elsif Ekind (Full_Typ) = E_Task_Type then
7577 -- Task entry
7579 if Is_Primitive_Wrapper (Prim_Op)
7580 and then Ekind (Wrapped_Entity (Prim_Op)) = E_Entry
7581 then
7582 return New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc);
7584 -- Task "procedure". These are the internally Expander-generated
7585 -- procedures (task body for instance).
7587 else
7588 return New_Occurrence_Of (RTE (RE_POK_Task_Procedure), Loc);
7589 end if;
7591 -- Regular procedure
7593 else
7594 return New_Occurrence_Of (RTE (RE_POK_Procedure), Loc);
7595 end if;
7596 end if;
7597 end Prim_Op_Kind;
7599 ------------------------
7600 -- Register_Primitive --
7601 ------------------------
7603 function Register_Primitive
7604 (Loc : Source_Ptr;
7605 Prim : Entity_Id) return List_Id
7607 DT_Ptr : Entity_Id;
7608 Iface_Prim : Entity_Id;
7609 Iface_Typ : Entity_Id;
7610 Iface_DT_Ptr : Entity_Id;
7611 Iface_DT_Elmt : Elmt_Id;
7612 L : constant List_Id := New_List;
7613 Pos : Uint;
7614 Tag : Entity_Id;
7615 Tag_Typ : Entity_Id;
7616 Thunk_Id : Entity_Id;
7617 Thunk_Code : Node_Id;
7619 begin
7620 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
7621 pragma Assert (VM_Target = No_VM);
7623 -- Do not register in the dispatch table eliminated primitives
7625 if not RTE_Available (RE_Tag)
7626 or else Is_Eliminated (Ultimate_Alias (Prim))
7627 then
7628 return L;
7629 end if;
7631 if not Present (Interface_Alias (Prim)) then
7632 Tag_Typ := Scope (DTC_Entity (Prim));
7633 Pos := DT_Position (Prim);
7634 Tag := First_Tag_Component (Tag_Typ);
7636 if Is_Predefined_Dispatching_Operation (Prim)
7637 or else Is_Predefined_Dispatching_Alias (Prim)
7638 then
7639 DT_Ptr :=
7640 Node (Next_Elmt (First_Elmt (Access_Disp_Table (Tag_Typ))));
7642 Append_To (L,
7643 Build_Set_Predefined_Prim_Op_Address (Loc,
7644 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7645 Position => Pos,
7646 Address_Node =>
7647 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7648 Make_Attribute_Reference (Loc,
7649 Prefix => New_Occurrence_Of (Prim, Loc),
7650 Attribute_Name => Name_Unrestricted_Access))));
7652 -- Register copy of the pointer to the 'size primitive in the TSD
7654 if Chars (Prim) = Name_uSize
7655 and then RTE_Record_Component_Available (RE_Size_Func)
7656 then
7657 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Tag_Typ)));
7658 Append_To (L,
7659 Build_Set_Size_Function (Loc,
7660 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7661 Size_Func => Prim));
7662 end if;
7664 else
7665 pragma Assert (Pos /= Uint_0 and then Pos <= DT_Entry_Count (Tag));
7667 -- Skip registration of primitives located in the C++ part of the
7668 -- dispatch table. Their slot is set by the IC routine.
7670 if not Is_CPP_Class (Root_Type (Tag_Typ))
7671 or else Pos > CPP_Num_Prims (Tag_Typ)
7672 then
7673 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Tag_Typ)));
7674 Append_To (L,
7675 Build_Set_Prim_Op_Address (Loc,
7676 Typ => Tag_Typ,
7677 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7678 Position => Pos,
7679 Address_Node =>
7680 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7681 Make_Attribute_Reference (Loc,
7682 Prefix => New_Occurrence_Of (Prim, Loc),
7683 Attribute_Name => Name_Unrestricted_Access))));
7684 end if;
7685 end if;
7687 -- Ada 2005 (AI-251): Primitive associated with an interface type
7689 -- Generate the code of the thunk only if the interface type is not an
7690 -- immediate ancestor of Typ; otherwise the dispatch table associated
7691 -- with the interface is the primary dispatch table and we have nothing
7692 -- else to do here.
7694 else
7695 Tag_Typ := Find_Dispatching_Type (Alias (Prim));
7696 Iface_Typ := Find_Dispatching_Type (Interface_Alias (Prim));
7698 pragma Assert (Is_Interface (Iface_Typ));
7700 -- No action needed for interfaces that are ancestors of Typ because
7701 -- their primitives are located in the primary dispatch table.
7703 if Is_Ancestor (Iface_Typ, Tag_Typ, Use_Full_View => True) then
7704 return L;
7706 -- No action needed for primitives located in the C++ part of the
7707 -- dispatch table. Their slot is set by the IC routine.
7709 elsif Is_CPP_Class (Root_Type (Tag_Typ))
7710 and then DT_Position (Alias (Prim)) <= CPP_Num_Prims (Tag_Typ)
7711 and then not Is_Predefined_Dispatching_Operation (Prim)
7712 and then not Is_Predefined_Dispatching_Alias (Prim)
7713 then
7714 return L;
7715 end if;
7717 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7719 if not Is_Ancestor (Iface_Typ, Tag_Typ, Use_Full_View => True)
7720 and then Present (Thunk_Code)
7721 then
7722 -- Generate the code necessary to fill the appropriate entry of
7723 -- the secondary dispatch table of Prim's controlling type with
7724 -- Thunk_Id's address.
7726 Iface_DT_Elmt := Find_Interface_ADT (Tag_Typ, Iface_Typ);
7727 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7728 pragma Assert (Has_Thunks (Iface_DT_Ptr));
7730 Iface_Prim := Interface_Alias (Prim);
7731 Pos := DT_Position (Iface_Prim);
7732 Tag := First_Tag_Component (Iface_Typ);
7734 Prepend_To (L, Thunk_Code);
7736 if Is_Predefined_Dispatching_Operation (Prim)
7737 or else Is_Predefined_Dispatching_Alias (Prim)
7738 then
7739 Append_To (L,
7740 Build_Set_Predefined_Prim_Op_Address (Loc,
7741 Tag_Node =>
7742 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Elmt)), Loc),
7743 Position => Pos,
7744 Address_Node =>
7745 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7746 Make_Attribute_Reference (Loc,
7747 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7748 Attribute_Name => Name_Unrestricted_Access))));
7750 Next_Elmt (Iface_DT_Elmt);
7751 Next_Elmt (Iface_DT_Elmt);
7752 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7753 pragma Assert (not Has_Thunks (Iface_DT_Ptr));
7755 Append_To (L,
7756 Build_Set_Predefined_Prim_Op_Address (Loc,
7757 Tag_Node =>
7758 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Elmt)), Loc),
7759 Position => Pos,
7760 Address_Node =>
7761 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7762 Make_Attribute_Reference (Loc,
7763 Prefix =>
7764 New_Occurrence_Of (Alias (Prim), Loc),
7765 Attribute_Name => Name_Unrestricted_Access))));
7767 else
7768 pragma Assert (Pos /= Uint_0
7769 and then Pos <= DT_Entry_Count (Tag));
7771 Append_To (L,
7772 Build_Set_Prim_Op_Address (Loc,
7773 Typ => Iface_Typ,
7774 Tag_Node => New_Occurrence_Of (Iface_DT_Ptr, Loc),
7775 Position => Pos,
7776 Address_Node =>
7777 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7778 Make_Attribute_Reference (Loc,
7779 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7780 Attribute_Name => Name_Unrestricted_Access))));
7782 Next_Elmt (Iface_DT_Elmt);
7783 Next_Elmt (Iface_DT_Elmt);
7784 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7785 pragma Assert (not Has_Thunks (Iface_DT_Ptr));
7787 Append_To (L,
7788 Build_Set_Prim_Op_Address (Loc,
7789 Typ => Iface_Typ,
7790 Tag_Node => New_Occurrence_Of (Iface_DT_Ptr, Loc),
7791 Position => Pos,
7792 Address_Node =>
7793 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7794 Make_Attribute_Reference (Loc,
7795 Prefix =>
7796 New_Occurrence_Of (Alias (Prim), Loc),
7797 Attribute_Name => Name_Unrestricted_Access))));
7799 end if;
7800 end if;
7801 end if;
7803 return L;
7804 end Register_Primitive;
7806 -------------------------
7807 -- Set_All_DT_Position --
7808 -------------------------
7810 procedure Set_All_DT_Position (Typ : Entity_Id) is
7812 function In_Predef_Prims_DT (Prim : Entity_Id) return Boolean;
7813 -- Returns True if Prim is located in the dispatch table of
7814 -- predefined primitives
7816 procedure Validate_Position (Prim : Entity_Id);
7817 -- Check that position assigned to Prim is completely safe (it has not
7818 -- been assigned to a previously defined primitive operation of Typ).
7820 ------------------------
7821 -- In_Predef_Prims_DT --
7822 ------------------------
7824 function In_Predef_Prims_DT (Prim : Entity_Id) return Boolean is
7825 E : Entity_Id;
7827 begin
7828 -- Predefined primitives
7830 if Is_Predefined_Dispatching_Operation (Prim) then
7831 return True;
7833 -- Renamings of predefined primitives
7835 elsif Present (Alias (Prim))
7836 and then Is_Predefined_Dispatching_Operation (Ultimate_Alias (Prim))
7837 then
7838 if Chars (Ultimate_Alias (Prim)) /= Name_Op_Eq then
7839 return True;
7841 -- User-defined renamings of predefined equality have their own
7842 -- slot in the primary dispatch table
7844 else
7845 E := Prim;
7846 while Present (Alias (E)) loop
7847 if Comes_From_Source (E) then
7848 return False;
7849 end if;
7851 E := Alias (E);
7852 end loop;
7854 return not Comes_From_Source (E);
7855 end if;
7857 -- User-defined primitives
7859 else
7860 return False;
7861 end if;
7862 end In_Predef_Prims_DT;
7864 -----------------------
7865 -- Validate_Position --
7866 -----------------------
7868 procedure Validate_Position (Prim : Entity_Id) is
7869 Op_Elmt : Elmt_Id;
7870 Op : Entity_Id;
7872 begin
7873 -- Aliased primitives are safe
7875 if Present (Alias (Prim)) then
7876 return;
7877 end if;
7879 Op_Elmt := First_Elmt (Primitive_Operations (Typ));
7880 while Present (Op_Elmt) loop
7881 Op := Node (Op_Elmt);
7883 -- No need to check against itself
7885 if Op = Prim then
7886 null;
7888 -- Primitive operations covering abstract interfaces are
7889 -- allocated later
7891 elsif Present (Interface_Alias (Op)) then
7892 null;
7894 -- Predefined dispatching operations are completely safe. They
7895 -- are allocated at fixed positions in a separate table.
7897 elsif Is_Predefined_Dispatching_Operation (Op)
7898 or else Is_Predefined_Dispatching_Alias (Op)
7899 then
7900 null;
7902 -- Aliased subprograms are safe
7904 elsif Present (Alias (Op)) then
7905 null;
7907 elsif DT_Position (Op) = DT_Position (Prim)
7908 and then not Is_Predefined_Dispatching_Operation (Op)
7909 and then not Is_Predefined_Dispatching_Operation (Prim)
7910 and then not Is_Predefined_Dispatching_Alias (Op)
7911 and then not Is_Predefined_Dispatching_Alias (Prim)
7912 then
7913 -- Handle aliased subprograms
7915 declare
7916 Op_1 : Entity_Id;
7917 Op_2 : Entity_Id;
7919 begin
7920 Op_1 := Op;
7921 loop
7922 if Present (Overridden_Operation (Op_1)) then
7923 Op_1 := Overridden_Operation (Op_1);
7924 elsif Present (Alias (Op_1)) then
7925 Op_1 := Alias (Op_1);
7926 else
7927 exit;
7928 end if;
7929 end loop;
7931 Op_2 := Prim;
7932 loop
7933 if Present (Overridden_Operation (Op_2)) then
7934 Op_2 := Overridden_Operation (Op_2);
7935 elsif Present (Alias (Op_2)) then
7936 Op_2 := Alias (Op_2);
7937 else
7938 exit;
7939 end if;
7940 end loop;
7942 if Op_1 /= Op_2 then
7943 raise Program_Error;
7944 end if;
7945 end;
7946 end if;
7948 Next_Elmt (Op_Elmt);
7949 end loop;
7950 end Validate_Position;
7952 -- Local variables
7954 Parent_Typ : constant Entity_Id := Etype (Typ);
7955 First_Prim : constant Elmt_Id := First_Elmt (Primitive_Operations (Typ));
7956 The_Tag : constant Entity_Id := First_Tag_Component (Typ);
7958 Adjusted : Boolean := False;
7959 Finalized : Boolean := False;
7961 Count_Prim : Nat;
7962 DT_Length : Nat;
7963 Nb_Prim : Nat;
7964 Prim : Entity_Id;
7965 Prim_Elmt : Elmt_Id;
7967 -- Start of processing for Set_All_DT_Position
7969 begin
7970 pragma Assert (Present (First_Tag_Component (Typ)));
7972 -- Set the DT_Position for each primitive operation. Perform some sanity
7973 -- checks to avoid building inconsistent dispatch tables.
7975 -- First stage: Set DTC entity of all the primitive operations. This is
7976 -- required to properly read the DT_Position attribute in latter stages.
7978 Prim_Elmt := First_Prim;
7979 Count_Prim := 0;
7980 while Present (Prim_Elmt) loop
7981 Prim := Node (Prim_Elmt);
7983 -- Predefined primitives have a separate dispatch table
7985 if not In_Predef_Prims_DT (Prim) then
7986 Count_Prim := Count_Prim + 1;
7987 end if;
7989 Set_DTC_Entity_Value (Typ, Prim);
7991 -- Clear any previous value of the DT_Position attribute. In this
7992 -- way we ensure that the final position of all the primitives is
7993 -- established by the following stages of this algorithm.
7995 Set_DT_Position (Prim, No_Uint);
7997 Next_Elmt (Prim_Elmt);
7998 end loop;
8000 declare
8001 Fixed_Prim : array (Int range 0 .. Count_Prim) of Boolean :=
8002 (others => False);
8004 E : Entity_Id;
8006 procedure Handle_Inherited_Private_Subprograms (Typ : Entity_Id);
8007 -- Called if Typ is declared in a nested package or a public child
8008 -- package to handle inherited primitives that were inherited by Typ
8009 -- in the visible part, but whose declaration was deferred because
8010 -- the parent operation was private and not visible at that point.
8012 procedure Set_Fixed_Prim (Pos : Nat);
8013 -- Sets to true an element of the Fixed_Prim table to indicate
8014 -- that this entry of the dispatch table of Typ is occupied.
8016 ------------------------------------------
8017 -- Handle_Inherited_Private_Subprograms --
8018 ------------------------------------------
8020 procedure Handle_Inherited_Private_Subprograms (Typ : Entity_Id) is
8021 Op_List : Elist_Id;
8022 Op_Elmt : Elmt_Id;
8023 Op_Elmt_2 : Elmt_Id;
8024 Prim_Op : Entity_Id;
8025 Parent_Subp : Entity_Id;
8027 begin
8028 Op_List := Primitive_Operations (Typ);
8030 Op_Elmt := First_Elmt (Op_List);
8031 while Present (Op_Elmt) loop
8032 Prim_Op := Node (Op_Elmt);
8034 -- Search primitives that are implicit operations with an
8035 -- internal name whose parent operation has a normal name.
8037 if Present (Alias (Prim_Op))
8038 and then Find_Dispatching_Type (Alias (Prim_Op)) /= Typ
8039 and then not Comes_From_Source (Prim_Op)
8040 and then Is_Internal_Name (Chars (Prim_Op))
8041 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
8042 then
8043 Parent_Subp := Alias (Prim_Op);
8045 -- Check if the type has an explicit overriding for this
8046 -- primitive.
8048 Op_Elmt_2 := Next_Elmt (Op_Elmt);
8049 while Present (Op_Elmt_2) loop
8050 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
8051 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
8052 then
8053 Set_DT_Position (Prim_Op, DT_Position (Parent_Subp));
8054 Set_DT_Position (Node (Op_Elmt_2),
8055 DT_Position (Parent_Subp));
8056 Set_Fixed_Prim (UI_To_Int (DT_Position (Prim_Op)));
8058 goto Next_Primitive;
8059 end if;
8061 Next_Elmt (Op_Elmt_2);
8062 end loop;
8063 end if;
8065 <<Next_Primitive>>
8066 Next_Elmt (Op_Elmt);
8067 end loop;
8068 end Handle_Inherited_Private_Subprograms;
8070 --------------------
8071 -- Set_Fixed_Prim --
8072 --------------------
8074 procedure Set_Fixed_Prim (Pos : Nat) is
8075 begin
8076 pragma Assert (Pos <= Count_Prim);
8077 Fixed_Prim (Pos) := True;
8078 exception
8079 when Constraint_Error =>
8080 raise Program_Error;
8081 end Set_Fixed_Prim;
8083 begin
8084 -- In case of nested packages and public child package it may be
8085 -- necessary a special management on inherited subprograms so that
8086 -- the dispatch table is properly filled.
8088 if Ekind (Scope (Scope (Typ))) = E_Package
8089 and then Scope (Scope (Typ)) /= Standard_Standard
8090 and then ((Is_Derived_Type (Typ) and then not Is_Private_Type (Typ))
8091 or else
8092 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration
8093 and then Is_Generic_Type (Typ)))
8094 and then In_Open_Scopes (Scope (Etype (Typ)))
8095 and then Is_Base_Type (Typ)
8096 then
8097 Handle_Inherited_Private_Subprograms (Typ);
8098 end if;
8100 -- Second stage: Register fixed entries
8102 Nb_Prim := 0;
8103 Prim_Elmt := First_Prim;
8104 while Present (Prim_Elmt) loop
8105 Prim := Node (Prim_Elmt);
8107 -- Predefined primitives have a separate table and all its
8108 -- entries are at predefined fixed positions.
8110 if In_Predef_Prims_DT (Prim) then
8111 if Is_Predefined_Dispatching_Operation (Prim) then
8112 Set_DT_Position (Prim, Default_Prim_Op_Position (Prim));
8114 else pragma Assert (Present (Alias (Prim)));
8115 Set_DT_Position (Prim,
8116 Default_Prim_Op_Position (Ultimate_Alias (Prim)));
8117 end if;
8119 -- Overriding primitives of ancestor abstract interfaces
8121 elsif Present (Interface_Alias (Prim))
8122 and then Is_Ancestor
8123 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
8124 Use_Full_View => True)
8125 then
8126 pragma Assert (DT_Position (Prim) = No_Uint
8127 and then Present (DTC_Entity (Interface_Alias (Prim))));
8129 E := Interface_Alias (Prim);
8130 Set_DT_Position (Prim, DT_Position (E));
8132 pragma Assert
8133 (DT_Position (Alias (Prim)) = No_Uint
8134 or else DT_Position (Alias (Prim)) = DT_Position (E));
8135 Set_DT_Position (Alias (Prim), DT_Position (E));
8136 Set_Fixed_Prim (UI_To_Int (DT_Position (Prim)));
8138 -- Overriding primitives must use the same entry as the
8139 -- overridden primitive.
8141 elsif not Present (Interface_Alias (Prim))
8142 and then Present (Alias (Prim))
8143 and then Chars (Prim) = Chars (Alias (Prim))
8144 and then Find_Dispatching_Type (Alias (Prim)) /= Typ
8145 and then Is_Ancestor
8146 (Find_Dispatching_Type (Alias (Prim)), Typ,
8147 Use_Full_View => True)
8148 and then Present (DTC_Entity (Alias (Prim)))
8149 then
8150 E := Alias (Prim);
8151 Set_DT_Position (Prim, DT_Position (E));
8153 if not Is_Predefined_Dispatching_Alias (E) then
8154 Set_Fixed_Prim (UI_To_Int (DT_Position (E)));
8155 end if;
8156 end if;
8158 Next_Elmt (Prim_Elmt);
8159 end loop;
8161 -- Third stage: Fix the position of all the new primitives. Entries
8162 -- associated with primitives covering interfaces are handled in a
8163 -- latter round.
8165 Prim_Elmt := First_Prim;
8166 while Present (Prim_Elmt) loop
8167 Prim := Node (Prim_Elmt);
8169 -- Skip primitives previously set entries
8171 if DT_Position (Prim) /= No_Uint then
8172 null;
8174 -- Primitives covering interface primitives are handled later
8176 elsif Present (Interface_Alias (Prim)) then
8177 null;
8179 else
8180 -- Take the next available position in the DT
8182 loop
8183 Nb_Prim := Nb_Prim + 1;
8184 pragma Assert (Nb_Prim <= Count_Prim);
8185 exit when not Fixed_Prim (Nb_Prim);
8186 end loop;
8188 Set_DT_Position (Prim, UI_From_Int (Nb_Prim));
8189 Set_Fixed_Prim (Nb_Prim);
8190 end if;
8192 Next_Elmt (Prim_Elmt);
8193 end loop;
8194 end;
8196 -- Fourth stage: Complete the decoration of primitives covering
8197 -- interfaces (that is, propagate the DT_Position attribute from
8198 -- the aliased primitive)
8200 Prim_Elmt := First_Prim;
8201 while Present (Prim_Elmt) loop
8202 Prim := Node (Prim_Elmt);
8204 if DT_Position (Prim) = No_Uint
8205 and then Present (Interface_Alias (Prim))
8206 then
8207 pragma Assert (Present (Alias (Prim))
8208 and then Find_Dispatching_Type (Alias (Prim)) = Typ);
8210 -- Check if this entry will be placed in the primary DT
8212 if Is_Ancestor
8213 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
8214 Use_Full_View => True)
8215 then
8216 pragma Assert (DT_Position (Alias (Prim)) /= No_Uint);
8217 Set_DT_Position (Prim, DT_Position (Alias (Prim)));
8219 -- Otherwise it will be placed in the secondary DT
8221 else
8222 pragma Assert
8223 (DT_Position (Interface_Alias (Prim)) /= No_Uint);
8224 Set_DT_Position (Prim,
8225 DT_Position (Interface_Alias (Prim)));
8226 end if;
8227 end if;
8229 Next_Elmt (Prim_Elmt);
8230 end loop;
8232 -- Generate listing showing the contents of the dispatch tables. This
8233 -- action is done before some further static checks because in case of
8234 -- critical errors caused by a wrong dispatch table we need to see the
8235 -- contents of such table.
8237 if Debug_Flag_ZZ then
8238 Write_DT (Typ);
8239 end if;
8241 -- Final stage: Ensure that the table is correct plus some further
8242 -- verifications concerning the primitives.
8244 Prim_Elmt := First_Prim;
8245 DT_Length := 0;
8246 while Present (Prim_Elmt) loop
8247 Prim := Node (Prim_Elmt);
8249 -- At this point all the primitives MUST have a position in the
8250 -- dispatch table.
8252 if DT_Position (Prim) = No_Uint then
8253 raise Program_Error;
8254 end if;
8256 -- Calculate real size of the dispatch table
8258 if not In_Predef_Prims_DT (Prim)
8259 and then UI_To_Int (DT_Position (Prim)) > DT_Length
8260 then
8261 DT_Length := UI_To_Int (DT_Position (Prim));
8262 end if;
8264 -- Ensure that the assigned position to non-predefined dispatching
8265 -- operations in the dispatch table is correct.
8267 if not Is_Predefined_Dispatching_Operation (Prim)
8268 and then not Is_Predefined_Dispatching_Alias (Prim)
8269 then
8270 Validate_Position (Prim);
8271 end if;
8273 if Chars (Prim) = Name_Finalize then
8274 Finalized := True;
8275 end if;
8277 if Chars (Prim) = Name_Adjust then
8278 Adjusted := True;
8279 end if;
8281 -- An abstract operation cannot be declared in the private part for a
8282 -- visible abstract type, because it can't be overridden outside this
8283 -- package hierarchy. For explicit declarations this is checked at
8284 -- the point of declaration, but for inherited operations it must be
8285 -- done when building the dispatch table.
8287 -- Ada 2005 (AI-251): Primitives associated with interfaces are
8288 -- excluded from this check because interfaces must be visible in
8289 -- the public and private part (RM 7.3 (7.3/2))
8291 -- We disable this check in Relaxed_RM_Semantics mode, to accommodate
8292 -- legacy Ada code.
8294 if not Relaxed_RM_Semantics
8295 and then Is_Abstract_Type (Typ)
8296 and then Is_Abstract_Subprogram (Prim)
8297 and then Present (Alias (Prim))
8298 and then not Is_Interface
8299 (Find_Dispatching_Type (Ultimate_Alias (Prim)))
8300 and then not Present (Interface_Alias (Prim))
8301 and then Is_Derived_Type (Typ)
8302 and then In_Private_Part (Current_Scope)
8303 and then
8304 List_Containing (Parent (Prim)) =
8305 Private_Declarations (Package_Specification (Current_Scope))
8306 and then Original_View_In_Visible_Part (Typ)
8307 then
8308 -- We exclude Input and Output stream operations because
8309 -- Limited_Controlled inherits useless Input and Output stream
8310 -- operations from Root_Controlled, which can never be overridden.
8312 if not Is_TSS (Prim, TSS_Stream_Input)
8313 and then
8314 not Is_TSS (Prim, TSS_Stream_Output)
8315 then
8316 Error_Msg_NE
8317 ("abstract inherited private operation&" &
8318 " must be overridden (RM 3.9.3(10))",
8319 Parent (Typ), Prim);
8320 end if;
8321 end if;
8323 Next_Elmt (Prim_Elmt);
8324 end loop;
8326 -- Additional check
8328 if Is_Controlled (Typ) then
8329 if not Finalized then
8330 Error_Msg_N
8331 ("controlled type has no explicit Finalize method??", Typ);
8333 elsif not Adjusted then
8334 Error_Msg_N
8335 ("controlled type has no explicit Adjust method??", Typ);
8336 end if;
8337 end if;
8339 -- Set the final size of the Dispatch Table
8341 Set_DT_Entry_Count (The_Tag, UI_From_Int (DT_Length));
8343 -- The derived type must have at least as many components as its parent
8344 -- (for root types Etype points to itself and the test cannot fail).
8346 if DT_Entry_Count (The_Tag) <
8347 DT_Entry_Count (First_Tag_Component (Parent_Typ))
8348 then
8349 raise Program_Error;
8350 end if;
8351 end Set_All_DT_Position;
8353 --------------------------
8354 -- Set_CPP_Constructors --
8355 --------------------------
8357 procedure Set_CPP_Constructors (Typ : Entity_Id) is
8359 function Gen_Parameters_Profile (E : Entity_Id) return List_Id;
8360 -- Duplicate the parameters profile of the imported C++ constructor
8361 -- adding an access to the object as an additional parameter.
8363 ----------------------------
8364 -- Gen_Parameters_Profile --
8365 ----------------------------
8367 function Gen_Parameters_Profile (E : Entity_Id) return List_Id is
8368 Loc : constant Source_Ptr := Sloc (E);
8369 Parms : List_Id;
8370 P : Node_Id;
8372 begin
8373 Parms :=
8374 New_List (
8375 Make_Parameter_Specification (Loc,
8376 Defining_Identifier =>
8377 Make_Defining_Identifier (Loc, Name_uInit),
8378 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8380 if Present (Parameter_Specifications (Parent (E))) then
8381 P := First (Parameter_Specifications (Parent (E)));
8382 while Present (P) loop
8383 Append_To (Parms,
8384 Make_Parameter_Specification (Loc,
8385 Defining_Identifier =>
8386 Make_Defining_Identifier (Loc,
8387 Chars => Chars (Defining_Identifier (P))),
8388 Parameter_Type => New_Copy_Tree (Parameter_Type (P)),
8389 Expression => New_Copy_Tree (Expression (P))));
8390 Next (P);
8391 end loop;
8392 end if;
8394 return Parms;
8395 end Gen_Parameters_Profile;
8397 -- Local variables
8399 Loc : Source_Ptr;
8400 E : Entity_Id;
8401 Found : Boolean := False;
8402 IP : Entity_Id;
8403 IP_Body : Node_Id;
8404 P : Node_Id;
8405 Parms : List_Id;
8407 Covers_Default_Constructor : Entity_Id := Empty;
8409 -- Start of processing for Set_CPP_Constructor
8411 begin
8412 pragma Assert (Is_CPP_Class (Typ));
8414 -- Look for the constructor entities
8416 E := Next_Entity (Typ);
8417 while Present (E) loop
8418 if Ekind (E) = E_Function
8419 and then Is_Constructor (E)
8420 then
8421 Found := True;
8422 Loc := Sloc (E);
8423 Parms := Gen_Parameters_Profile (E);
8424 IP :=
8425 Make_Defining_Identifier (Loc,
8426 Chars => Make_Init_Proc_Name (Typ));
8428 -- Case 1: Constructor of untagged type
8430 -- If the C++ class has no virtual methods then the matching Ada
8431 -- type is an untagged record type. In such case there is no need
8432 -- to generate a wrapper of the C++ constructor because the _tag
8433 -- component is not available.
8435 if not Is_Tagged_Type (Typ) then
8436 Discard_Node
8437 (Make_Subprogram_Declaration (Loc,
8438 Specification =>
8439 Make_Procedure_Specification (Loc,
8440 Defining_Unit_Name => IP,
8441 Parameter_Specifications => Parms)));
8443 Set_Init_Proc (Typ, IP);
8444 Set_Is_Imported (IP);
8445 Set_Is_Constructor (IP);
8446 Set_Interface_Name (IP, Interface_Name (E));
8447 Set_Convention (IP, Convention_CPP);
8448 Set_Is_Public (IP);
8449 Set_Has_Completion (IP);
8451 -- Case 2: Constructor of a tagged type
8453 -- In this case we generate the IP as a wrapper of the the
8454 -- C++ constructor because IP must also save copy of the _tag
8455 -- generated in the C++ side. The copy of the _tag is used by
8456 -- Build_CPP_Init_Procedure to elaborate derivations of C++ types.
8458 -- Generate:
8459 -- procedure IP (_init : Typ; ...) is
8460 -- procedure ConstructorP (_init : Typ; ...);
8461 -- pragma Import (ConstructorP);
8462 -- begin
8463 -- ConstructorP (_init, ...);
8464 -- if Typ._tag = null then
8465 -- Typ._tag := _init._tag;
8466 -- end if;
8467 -- end IP;
8469 else
8470 declare
8471 Body_Stmts : constant List_Id := New_List;
8472 Constructor_Id : Entity_Id;
8473 Constructor_Decl_Node : Node_Id;
8474 Init_Tags_List : List_Id;
8476 begin
8477 Constructor_Id := Make_Temporary (Loc, 'P');
8479 Constructor_Decl_Node :=
8480 Make_Subprogram_Declaration (Loc,
8481 Make_Procedure_Specification (Loc,
8482 Defining_Unit_Name => Constructor_Id,
8483 Parameter_Specifications => Parms));
8485 Set_Is_Imported (Constructor_Id);
8486 Set_Is_Constructor (Constructor_Id);
8487 Set_Interface_Name (Constructor_Id, Interface_Name (E));
8488 Set_Convention (Constructor_Id, Convention_CPP);
8489 Set_Is_Public (Constructor_Id);
8490 Set_Has_Completion (Constructor_Id);
8492 -- Build the init procedure as a wrapper of this constructor
8494 Parms := Gen_Parameters_Profile (E);
8496 -- Invoke the C++ constructor
8498 declare
8499 Actuals : constant List_Id := New_List;
8501 begin
8502 P := First (Parms);
8503 while Present (P) loop
8504 Append_To (Actuals,
8505 New_Occurrence_Of (Defining_Identifier (P), Loc));
8506 Next (P);
8507 end loop;
8509 Append_To (Body_Stmts,
8510 Make_Procedure_Call_Statement (Loc,
8511 Name => New_Occurrence_Of (Constructor_Id, Loc),
8512 Parameter_Associations => Actuals));
8513 end;
8515 -- Initialize copies of C++ primary and secondary tags
8517 Init_Tags_List := New_List;
8519 declare
8520 Tag_Elmt : Elmt_Id;
8521 Tag_Comp : Node_Id;
8523 begin
8524 Tag_Elmt := First_Elmt (Access_Disp_Table (Typ));
8525 Tag_Comp := First_Tag_Component (Typ);
8527 while Present (Tag_Elmt)
8528 and then Is_Tag (Node (Tag_Elmt))
8529 loop
8530 -- Skip the following assertion with primary tags
8531 -- because Related_Type is not set on primary tag
8532 -- components
8534 pragma Assert
8535 (Tag_Comp = First_Tag_Component (Typ)
8536 or else Related_Type (Node (Tag_Elmt))
8537 = Related_Type (Tag_Comp));
8539 Append_To (Init_Tags_List,
8540 Make_Assignment_Statement (Loc,
8541 Name =>
8542 New_Occurrence_Of (Node (Tag_Elmt), Loc),
8543 Expression =>
8544 Make_Selected_Component (Loc,
8545 Prefix =>
8546 Make_Identifier (Loc, Name_uInit),
8547 Selector_Name =>
8548 New_Occurrence_Of (Tag_Comp, Loc))));
8550 Tag_Comp := Next_Tag_Component (Tag_Comp);
8551 Next_Elmt (Tag_Elmt);
8552 end loop;
8553 end;
8555 Append_To (Body_Stmts,
8556 Make_If_Statement (Loc,
8557 Condition =>
8558 Make_Op_Eq (Loc,
8559 Left_Opnd =>
8560 New_Occurrence_Of
8561 (Node (First_Elmt (Access_Disp_Table (Typ))),
8562 Loc),
8563 Right_Opnd =>
8564 Unchecked_Convert_To (RTE (RE_Tag),
8565 New_Occurrence_Of (RTE (RE_Null_Address), Loc))),
8566 Then_Statements => Init_Tags_List));
8568 IP_Body :=
8569 Make_Subprogram_Body (Loc,
8570 Specification =>
8571 Make_Procedure_Specification (Loc,
8572 Defining_Unit_Name => IP,
8573 Parameter_Specifications => Parms),
8574 Declarations => New_List (Constructor_Decl_Node),
8575 Handled_Statement_Sequence =>
8576 Make_Handled_Sequence_Of_Statements (Loc,
8577 Statements => Body_Stmts,
8578 Exception_Handlers => No_List));
8580 Discard_Node (IP_Body);
8581 Set_Init_Proc (Typ, IP);
8582 end;
8583 end if;
8585 -- If this constructor has parameters and all its parameters have
8586 -- defaults then it covers the default constructor. The semantic
8587 -- analyzer ensures that only one constructor with defaults covers
8588 -- the default constructor.
8590 if Present (Parameter_Specifications (Parent (E)))
8591 and then Needs_No_Actuals (E)
8592 then
8593 Covers_Default_Constructor := IP;
8594 end if;
8595 end if;
8597 Next_Entity (E);
8598 end loop;
8600 -- If there are no constructors, mark the type as abstract since we
8601 -- won't be able to declare objects of that type.
8603 if not Found then
8604 Set_Is_Abstract_Type (Typ);
8605 end if;
8607 -- Handle constructor that has all its parameters with defaults and
8608 -- hence it covers the default constructor. We generate a wrapper IP
8609 -- which calls the covering constructor.
8611 if Present (Covers_Default_Constructor) then
8612 declare
8613 Body_Stmts : List_Id;
8615 begin
8616 Loc := Sloc (Covers_Default_Constructor);
8618 Body_Stmts := New_List (
8619 Make_Procedure_Call_Statement (Loc,
8620 Name =>
8621 New_Occurrence_Of (Covers_Default_Constructor, Loc),
8622 Parameter_Associations => New_List (
8623 Make_Identifier (Loc, Name_uInit))));
8625 IP := Make_Defining_Identifier (Loc, Make_Init_Proc_Name (Typ));
8627 IP_Body :=
8628 Make_Subprogram_Body (Loc,
8629 Specification =>
8630 Make_Procedure_Specification (Loc,
8631 Defining_Unit_Name => IP,
8632 Parameter_Specifications => New_List (
8633 Make_Parameter_Specification (Loc,
8634 Defining_Identifier =>
8635 Make_Defining_Identifier (Loc, Name_uInit),
8636 Parameter_Type => New_Occurrence_Of (Typ, Loc)))),
8638 Declarations => No_List,
8640 Handled_Statement_Sequence =>
8641 Make_Handled_Sequence_Of_Statements (Loc,
8642 Statements => Body_Stmts,
8643 Exception_Handlers => No_List));
8645 Discard_Node (IP_Body);
8646 Set_Init_Proc (Typ, IP);
8647 end;
8648 end if;
8650 -- If the CPP type has constructors then it must import also the default
8651 -- C++ constructor. It is required for default initialization of objects
8652 -- of the type. It is also required to elaborate objects of Ada types
8653 -- that are defined as derivations of this CPP type.
8655 if Has_CPP_Constructors (Typ)
8656 and then No (Init_Proc (Typ))
8657 then
8658 Error_Msg_N ("??default constructor must be imported from C++", Typ);
8659 end if;
8660 end Set_CPP_Constructors;
8662 --------------------------
8663 -- Set_DTC_Entity_Value --
8664 --------------------------
8666 procedure Set_DTC_Entity_Value
8667 (Tagged_Type : Entity_Id;
8668 Prim : Entity_Id)
8670 begin
8671 if Present (Interface_Alias (Prim))
8672 and then Is_Interface
8673 (Find_Dispatching_Type (Interface_Alias (Prim)))
8674 then
8675 Set_DTC_Entity (Prim,
8676 Find_Interface_Tag
8677 (T => Tagged_Type,
8678 Iface => Find_Dispatching_Type (Interface_Alias (Prim))));
8679 else
8680 Set_DTC_Entity (Prim,
8681 First_Tag_Component (Tagged_Type));
8682 end if;
8683 end Set_DTC_Entity_Value;
8685 -----------------
8686 -- Tagged_Kind --
8687 -----------------
8689 function Tagged_Kind (T : Entity_Id) return Node_Id is
8690 Conc_Typ : Entity_Id;
8691 Loc : constant Source_Ptr := Sloc (T);
8693 begin
8694 pragma Assert
8695 (Is_Tagged_Type (T) and then RTE_Available (RE_Tagged_Kind));
8697 -- Abstract kinds
8699 if Is_Abstract_Type (T) then
8700 if Is_Limited_Record (T) then
8701 return New_Occurrence_Of
8702 (RTE (RE_TK_Abstract_Limited_Tagged), Loc);
8703 else
8704 return New_Occurrence_Of
8705 (RTE (RE_TK_Abstract_Tagged), Loc);
8706 end if;
8708 -- Concurrent kinds
8710 elsif Is_Concurrent_Record_Type (T) then
8711 Conc_Typ := Corresponding_Concurrent_Type (T);
8713 if Present (Full_View (Conc_Typ)) then
8714 Conc_Typ := Full_View (Conc_Typ);
8715 end if;
8717 if Ekind (Conc_Typ) = E_Protected_Type then
8718 return New_Occurrence_Of (RTE (RE_TK_Protected), Loc);
8719 else
8720 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
8721 return New_Occurrence_Of (RTE (RE_TK_Task), Loc);
8722 end if;
8724 -- Regular tagged kinds
8726 else
8727 if Is_Limited_Record (T) then
8728 return New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc);
8729 else
8730 return New_Occurrence_Of (RTE (RE_TK_Tagged), Loc);
8731 end if;
8732 end if;
8733 end Tagged_Kind;
8735 --------------
8736 -- Write_DT --
8737 --------------
8739 procedure Write_DT (Typ : Entity_Id) is
8740 Elmt : Elmt_Id;
8741 Prim : Node_Id;
8743 begin
8744 -- Protect this procedure against wrong usage. Required because it will
8745 -- be used directly from GDB
8747 if not (Typ <= Last_Node_Id)
8748 or else not Is_Tagged_Type (Typ)
8749 then
8750 Write_Str ("wrong usage: Write_DT must be used with tagged types");
8751 Write_Eol;
8752 return;
8753 end if;
8755 Write_Int (Int (Typ));
8756 Write_Str (": ");
8757 Write_Name (Chars (Typ));
8759 if Is_Interface (Typ) then
8760 Write_Str (" is interface");
8761 end if;
8763 Write_Eol;
8765 Elmt := First_Elmt (Primitive_Operations (Typ));
8766 while Present (Elmt) loop
8767 Prim := Node (Elmt);
8768 Write_Str (" - ");
8770 -- Indicate if this primitive will be allocated in the primary
8771 -- dispatch table or in a secondary dispatch table associated
8772 -- with an abstract interface type
8774 if Present (DTC_Entity (Prim)) then
8775 if Etype (DTC_Entity (Prim)) = RTE (RE_Tag) then
8776 Write_Str ("[P] ");
8777 else
8778 Write_Str ("[s] ");
8779 end if;
8780 end if;
8782 -- Output the node of this primitive operation and its name
8784 Write_Int (Int (Prim));
8785 Write_Str (": ");
8787 if Is_Predefined_Dispatching_Operation (Prim) then
8788 Write_Str ("(predefined) ");
8789 end if;
8791 -- Prefix the name of the primitive with its corresponding tagged
8792 -- type to facilitate seeing inherited primitives.
8794 if Present (Alias (Prim)) then
8795 Write_Name
8796 (Chars (Find_Dispatching_Type (Ultimate_Alias (Prim))));
8797 else
8798 Write_Name (Chars (Typ));
8799 end if;
8801 Write_Str (".");
8802 Write_Name (Chars (Prim));
8804 -- Indicate if this primitive has an aliased primitive
8806 if Present (Alias (Prim)) then
8807 Write_Str (" (alias = ");
8808 Write_Int (Int (Alias (Prim)));
8810 -- If the DTC_Entity attribute is already set we can also output
8811 -- the name of the interface covered by this primitive (if any).
8813 if Ekind_In (Alias (Prim), E_Function, E_Procedure)
8814 and then Present (DTC_Entity (Alias (Prim)))
8815 and then Is_Interface (Scope (DTC_Entity (Alias (Prim))))
8816 then
8817 Write_Str (" from interface ");
8818 Write_Name (Chars (Scope (DTC_Entity (Alias (Prim)))));
8819 end if;
8821 if Present (Interface_Alias (Prim)) then
8822 Write_Str (", AI_Alias of ");
8824 if Is_Null_Interface_Primitive (Interface_Alias (Prim)) then
8825 Write_Str ("null primitive ");
8826 end if;
8828 Write_Name
8829 (Chars (Find_Dispatching_Type (Interface_Alias (Prim))));
8830 Write_Char (':');
8831 Write_Int (Int (Interface_Alias (Prim)));
8832 end if;
8834 Write_Str (")");
8835 end if;
8837 -- Display the final position of this primitive in its associated
8838 -- (primary or secondary) dispatch table.
8840 if Present (DTC_Entity (Prim))
8841 and then DT_Position (Prim) /= No_Uint
8842 then
8843 Write_Str (" at #");
8844 Write_Int (UI_To_Int (DT_Position (Prim)));
8845 end if;
8847 if Is_Abstract_Subprogram (Prim) then
8848 Write_Str (" is abstract;");
8850 -- Check if this is a null primitive
8852 elsif Comes_From_Source (Prim)
8853 and then Ekind (Prim) = E_Procedure
8854 and then Null_Present (Parent (Prim))
8855 then
8856 Write_Str (" is null;");
8857 end if;
8859 if Is_Eliminated (Ultimate_Alias (Prim)) then
8860 Write_Str (" (eliminated)");
8861 end if;
8863 if Is_Imported (Prim)
8864 and then Convention (Prim) = Convention_CPP
8865 then
8866 Write_Str (" (C++)");
8867 end if;
8869 Write_Eol;
8871 Next_Elmt (Elmt);
8872 end loop;
8873 end Write_DT;
8875 end Exp_Disp;