rs6000: Update ELFv2 stack frame comment showing the correct ROP save location
[official-gcc.git] / gcc / ada / exp_disp.adb
blob66be77c9ffca4d2c4e79e7672702e9ed8332c510
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-2024, 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 Accessibility; use Accessibility;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Atag; use Exp_Atag;
37 with Exp_Ch6; use Exp_Ch6;
38 with Exp_CG; use Exp_CG;
39 with Exp_Dbug; use Exp_Dbug;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Itypes; use Itypes;
45 with Layout; use Layout;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
48 with Namet; use Namet;
49 with Opt; use Opt;
50 with Output; use Output;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Res; use Sem_Res;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Sinfo; use Sinfo;
65 with Sinfo.Nodes; use Sinfo.Nodes;
66 with Sinfo.Utils; use Sinfo.Utils;
67 with Snames; use Snames;
68 with Stand; use Stand;
69 with Stringt; use Stringt;
70 with Strub; use Strub;
71 with SCIL_LL; use SCIL_LL;
72 with Tbuild; use Tbuild;
74 package body Exp_Disp is
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 function Default_Prim_Op_Position (E : Entity_Id) return Uint;
81 -- Ada 2005 (AI-251): Returns the fixed position in the dispatch table
82 -- of the default primitive operations.
84 procedure Expand_Interface_Thunk
85 (Prim : Entity_Id;
86 Thunk_Id : out Entity_Id;
87 Thunk_Code : out List_Id;
88 Iface : Entity_Id);
89 -- Ada 2005 (AI-251): When a tagged type implements abstract interfaces we
90 -- generate additional subprograms (thunks) associated with each primitive
91 -- Prim to have a layout compatible with the C++ ABI. The thunk displaces
92 -- the pointers to the actuals that depend on the controlling type before
93 -- transferring control to the target subprogram. If there is no need to
94 -- generate the thunk, then Thunk_Id is set to Empty. Otherwise Thunk_Id
95 -- is set to the defining identifier of the thunk and Thunk_Code to the
96 -- code generated for the thunk respectively.
98 procedure Expand_Secondary_Stack_Thunk
99 (Prim : Entity_Id;
100 Thunk_Id : out Entity_Id;
101 Thunk_Code : out Node_Id);
102 -- When a primitive function of a tagged type can dispatch on result and
103 -- the tagged type is not returned on the secondary stack, we generate an
104 -- additional function (thunk) that calls the primitive function with the
105 -- same actuals and move its result onto the secondary stack. This thunk
106 -- is intended to be put into the slot of the primitive function in the
107 -- dispatch table, so as to be invoked in lieu of the primitive function
108 -- in dispatching calls. If there is no need to generate the thunk, then
109 -- Thunk_Id is set to Empty. Otherwise Thunk_Id is set to the defining
110 -- identifier of the thunk and Thunk_Code to the code generated for the
111 -- thunk respectively.
113 function Has_DT (Typ : Entity_Id) return Boolean;
114 pragma Inline (Has_DT);
115 -- Returns true if we generate a dispatch table for tagged type Typ
117 function Is_Predefined_Dispatching_Alias (Prim : Entity_Id) return Boolean;
118 -- Returns true if Prim is not a predefined dispatching primitive but it is
119 -- an alias of a predefined dispatching primitive (i.e. through a renaming)
121 function New_Value (From : Node_Id) return Node_Id;
122 -- From is the original Expression. New_Value is equivalent to a call to
123 -- Duplicate_Subexpr with an explicit dereference when From is an access
124 -- parameter.
126 function Prim_Op_Kind
127 (Prim : Entity_Id;
128 Typ : Entity_Id) return Node_Id;
129 -- Ada 2005 (AI-345): Determine the primitive operation kind of Prim
130 -- according to its type Typ. Return a reference to an RE_Prim_Op_Kind
131 -- enumeration value.
133 function Tagged_Kind (T : Entity_Id) return Node_Id;
134 -- Ada 2005 (AI-345): Determine the tagged kind of T and return a reference
135 -- to an RE_Tagged_Kind enumeration value.
137 ----------------------
138 -- Apply_Tag_Checks --
139 ----------------------
141 procedure Apply_Tag_Checks (Call_Node : Node_Id) is
142 Loc : constant Source_Ptr := Sloc (Call_Node);
143 Ctrl_Arg : constant Node_Id := Controlling_Argument (Call_Node);
144 Ctrl_Typ : constant Entity_Id := Base_Type (Etype (Ctrl_Arg));
145 Param_List : constant List_Id := Parameter_Associations (Call_Node);
147 Subp : Entity_Id;
148 CW_Typ : Entity_Id;
149 Param : Node_Id;
150 Typ : Entity_Id;
151 Eq_Prim_Op : Entity_Id := Empty;
153 begin
154 if No_Run_Time_Mode then
155 Error_Msg_CRT ("tagged types", Call_Node);
156 return;
157 end if;
159 -- Apply_Tag_Checks is called directly from the semantics, so we
160 -- need a check to see whether expansion is active before proceeding.
161 -- In addition, there is no need to expand the call when compiling
162 -- under restriction No_Dispatching_Calls; the semantic analyzer has
163 -- previously notified the violation of this restriction.
165 if not Expander_Active
166 or else Restriction_Active (No_Dispatching_Calls)
167 then
168 return;
169 end if;
171 -- Set subprogram. If this is an inherited operation that was
172 -- overridden, the body that is being called is its alias.
174 Subp := Entity (Name (Call_Node));
176 if Present (Alias (Subp))
177 and then Is_Inherited_Operation (Subp)
178 and then No (DTC_Entity (Subp))
179 then
180 Subp := Alias (Subp);
181 end if;
183 -- Definition of the class-wide type and the tagged type
185 -- If the controlling argument is itself a tag rather than a tagged
186 -- object, then use the class-wide type associated with the subprogram's
187 -- controlling type. This case can occur when a call to an inherited
188 -- primitive has an actual that originated from a default parameter
189 -- given by a tag-indeterminate call and when there is no other
190 -- controlling argument providing the tag (AI-239 requires dispatching).
191 -- This capability of dispatching directly by tag is also needed by the
192 -- implementation of AI-260 (for the generic dispatching constructors).
194 if Is_RTE (Ctrl_Typ, RE_Tag)
195 or else Is_RTE (Ctrl_Typ, RE_Interface_Tag)
196 then
197 CW_Typ := Class_Wide_Type (Find_Dispatching_Type (Subp));
199 -- Class_Wide_Type is applied to the expressions used to initialize
200 -- CW_Typ, to ensure that CW_Typ always denotes a class-wide type, since
201 -- there are cases where the controlling type is resolved to a specific
202 -- type (such as for designated types of arguments such as CW'Access).
204 elsif Is_Access_Type (Ctrl_Typ) then
205 CW_Typ := Class_Wide_Type (Designated_Type (Ctrl_Typ));
207 else
208 CW_Typ := Class_Wide_Type (Ctrl_Typ);
209 end if;
211 Typ := Find_Specific_Type (CW_Typ);
213 if not Is_Limited_Type (Typ) then
214 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
215 end if;
217 -- Dispatching call to C++ primitive
219 if Is_CPP_Class (Typ) then
220 null;
222 -- Dispatching call to Ada primitive
224 elsif Present (Param_List) then
226 -- Generate the Tag checks when appropriate
228 Param := First_Actual (Call_Node);
229 while Present (Param) loop
231 -- No tag check with itself
233 if Param = Ctrl_Arg then
234 null;
236 -- No tag check for parameter whose type is neither tagged nor
237 -- access to tagged (for access parameters)
239 elsif No (Find_Controlling_Arg (Param)) then
240 null;
242 -- No tag check for function dispatching on result if the
243 -- Tag given by the context is this one
245 elsif Find_Controlling_Arg (Param) = Ctrl_Arg then
246 null;
248 -- "=" is the only dispatching operation allowed to get operands
249 -- with incompatible tags (it just returns false). We use
250 -- Duplicate_Subexpr_Move_Checks instead of calling Relocate_Node
251 -- because the value will be duplicated to check the tags.
253 elsif Subp = Eq_Prim_Op then
254 null;
256 -- No check in presence of suppress flags
258 elsif Tag_Checks_Suppressed (Etype (Param))
259 or else (Is_Access_Type (Etype (Param))
260 and then Tag_Checks_Suppressed
261 (Designated_Type (Etype (Param))))
262 then
263 null;
265 -- Optimization: no tag checks if the parameters are identical
267 elsif Is_Entity_Name (Param)
268 and then Is_Entity_Name (Ctrl_Arg)
269 and then Entity (Param) = Entity (Ctrl_Arg)
270 then
271 null;
273 -- Now we need to generate the Tag check
275 else
276 -- Generate code for tag equality check
278 -- Perhaps should have Checks.Apply_Tag_Equality_Check???
280 Insert_Action (Ctrl_Arg,
281 Make_Implicit_If_Statement (Call_Node,
282 Condition =>
283 Make_Op_Ne (Loc,
284 Left_Opnd =>
285 Make_Selected_Component (Loc,
286 Prefix => New_Value (Ctrl_Arg),
287 Selector_Name =>
288 New_Occurrence_Of
289 (First_Tag_Component (Typ), Loc)),
291 Right_Opnd =>
292 Make_Selected_Component (Loc,
293 Prefix =>
294 Unchecked_Convert_To (Typ, New_Value (Param)),
295 Selector_Name =>
296 New_Occurrence_Of
297 (First_Tag_Component (Typ), Loc))),
299 Then_Statements =>
300 New_List (New_Constraint_Error (Loc))));
301 end if;
303 Next_Actual (Param);
304 end loop;
305 end if;
306 end Apply_Tag_Checks;
308 ------------------------
309 -- Building_Static_DT --
310 ------------------------
312 function Building_Static_DT (Typ : Entity_Id) return Boolean is
313 Root_Typ : Entity_Id := Root_Type (Typ);
314 Static_DT : Boolean;
316 begin
317 -- Handle private types
319 if Present (Full_View (Root_Typ)) then
320 Root_Typ := Full_View (Root_Typ);
321 end if;
323 Static_DT :=
324 Building_Static_Dispatch_Tables
325 and then Is_Library_Level_Tagged_Type (Typ)
327 -- If the type is derived from a CPP class we cannot statically
328 -- build the dispatch tables because we must inherit primitives
329 -- from the CPP side.
331 and then not Is_CPP_Class (Root_Typ);
333 if not Static_DT then
334 Check_Restriction (Static_Dispatch_Tables, Typ);
335 end if;
337 return Static_DT;
338 end Building_Static_DT;
340 ----------------------------------
341 -- Building_Static_Secondary_DT --
342 ----------------------------------
344 function Building_Static_Secondary_DT (Typ : Entity_Id) return Boolean is
345 Full_Typ : Entity_Id := Typ;
346 Root_Typ : Entity_Id := Root_Type (Typ);
347 Static_DT : Boolean;
349 begin
350 -- Handle private types
352 if Present (Full_View (Typ)) then
353 Full_Typ := Full_View (Typ);
354 end if;
356 if Present (Full_View (Root_Typ)) then
357 Root_Typ := Full_View (Root_Typ);
358 end if;
360 Static_DT :=
361 Building_Static_DT (Full_Typ)
362 and then not Is_Interface (Full_Typ)
363 and then Has_Interfaces (Full_Typ)
364 and then (Full_Typ = Root_Typ
365 or else not Is_Variable_Size_Record (Etype (Full_Typ)));
367 if not Static_DT
368 and then not Is_Interface (Full_Typ)
369 and then Has_Interfaces (Full_Typ)
370 then
371 Check_Restriction (Static_Dispatch_Tables, Typ);
372 end if;
374 return Static_DT;
375 end Building_Static_Secondary_DT;
377 ----------------------------------
378 -- Build_Static_Dispatch_Tables --
379 ----------------------------------
381 procedure Build_Static_Dispatch_Tables (N : Node_Id) is
382 Target_List : List_Id;
384 procedure Build_Dispatch_Tables (List : List_Id);
385 -- Build the static dispatch table of tagged types found in the list of
386 -- declarations. The generated nodes are added at the end of Target_List
388 procedure Build_Package_Dispatch_Tables (N : Node_Id);
389 -- Build static dispatch tables associated with package declaration N
391 procedure Make_And_Insert_Dispatch_Table (Typ : Entity_Id);
392 -- Build the dispatch table of the tagged type Typ and insert it at the
393 -- end of Target_List after wrapping it in the Actions list of a freeze
394 -- node, so that it is skipped by Sem_Elab (Expand_Freeze_Record_Type
395 -- does the same for nonstatic dispatch tables).
397 ---------------------------
398 -- Build_Dispatch_Tables --
399 ---------------------------
401 procedure Build_Dispatch_Tables (List : List_Id) is
402 D : Node_Id;
404 begin
405 D := First (List);
406 while Present (D) loop
408 -- Handle nested packages and package bodies recursively. The
409 -- generated code is placed on the Target_List established for
410 -- the enclosing compilation unit.
412 if Nkind (D) = N_Package_Declaration then
413 Build_Package_Dispatch_Tables (D);
415 elsif Nkind (D) = N_Package_Body then
416 Build_Dispatch_Tables (Declarations (D));
418 elsif Nkind (D) = N_Package_Body_Stub
419 and then Present (Library_Unit (D))
420 then
421 Build_Dispatch_Tables
422 (Declarations (Proper_Body (Unit (Library_Unit (D)))));
424 -- Handle full type declarations and derivations of library level
425 -- tagged types
427 elsif Nkind (D) in
428 N_Full_Type_Declaration | N_Derived_Type_Definition
429 and then Is_Library_Level_Tagged_Type (Defining_Entity (D))
430 and then Ekind (Defining_Entity (D)) /= E_Record_Subtype
431 and then not Is_Private_Type (Defining_Entity (D))
432 then
433 -- We do not generate dispatch tables for the internal types
434 -- created for a type extension with unknown discriminants
435 -- The needed information is shared with the source type,
436 -- See Expand_N_Record_Extension.
438 if Is_Underlying_Record_View (Defining_Entity (D))
439 or else
440 (not Comes_From_Source (Defining_Entity (D))
441 and then
442 Has_Unknown_Discriminants (Etype (Defining_Entity (D)))
443 and then
444 not Comes_From_Source
445 (First_Subtype (Defining_Entity (D))))
446 then
447 null;
448 else
449 Make_And_Insert_Dispatch_Table (Defining_Entity (D));
450 end if;
452 -- Handle private types of library level tagged types. We must
453 -- exchange the private and full-view to ensure the correct
454 -- expansion. If the full view is a synchronized type ignore
455 -- the type because the table will be built for the corresponding
456 -- record type, that has its own declaration.
458 elsif (Nkind (D) = N_Private_Type_Declaration
459 or else Nkind (D) = N_Private_Extension_Declaration)
460 and then Present (Full_View (Defining_Entity (D)))
461 then
462 declare
463 E1 : constant Entity_Id := Defining_Entity (D);
464 E2 : constant Entity_Id := Full_View (E1);
466 begin
467 if Is_Library_Level_Tagged_Type (E2)
468 and then Ekind (E2) /= E_Record_Subtype
469 and then not Is_Concurrent_Type (E2)
470 then
471 Exchange_Declarations (E1);
472 Make_And_Insert_Dispatch_Table (E1);
473 Exchange_Declarations (E2);
474 end if;
475 end;
476 end if;
478 Next (D);
479 end loop;
480 end Build_Dispatch_Tables;
482 -----------------------------------
483 -- Build_Package_Dispatch_Tables --
484 -----------------------------------
486 procedure Build_Package_Dispatch_Tables (N : Node_Id) is
487 Spec : constant Node_Id := Specification (N);
488 Id : constant Entity_Id := Defining_Entity (N);
489 Vis_Decls : constant List_Id := Visible_Declarations (Spec);
490 Priv_Decls : constant List_Id := Private_Declarations (Spec);
492 begin
493 Push_Scope (Id);
495 if Present (Priv_Decls) then
496 Build_Dispatch_Tables (Vis_Decls);
497 Build_Dispatch_Tables (Priv_Decls);
499 elsif Present (Vis_Decls) then
500 Build_Dispatch_Tables (Vis_Decls);
501 end if;
503 Pop_Scope;
504 end Build_Package_Dispatch_Tables;
506 ------------------------------------
507 -- Make_And_Insert_Dispatch_Table --
508 ------------------------------------
510 procedure Make_And_Insert_Dispatch_Table (Typ : Entity_Id) is
511 F_Typ : constant Entity_Id := Create_Itype (E_Class_Wide_Type, Typ);
512 -- The code generator discards freeze nodes of CW types after
513 -- evaluating their side effects, so create an artificial one.
515 F_Nod : constant Node_Id := Make_Freeze_Entity (Sloc (Typ));
517 begin
518 Set_Is_Frozen (F_Typ);
519 Set_Entity (F_Nod, F_Typ);
520 Set_Actions (F_Nod, Make_DT (Typ));
522 Insert_After_And_Analyze (Last (Target_List), F_Nod);
523 end Make_And_Insert_Dispatch_Table;
525 -- Start of processing for Build_Static_Dispatch_Tables
527 begin
528 if Nkind (N) = N_Package_Declaration then
529 declare
530 Spec : constant Node_Id := Specification (N);
531 Vis_Decls : constant List_Id := Visible_Declarations (Spec);
532 Priv_Decls : constant List_Id := Private_Declarations (Spec);
534 begin
535 if Present (Priv_Decls)
536 and then Is_Non_Empty_List (Priv_Decls)
537 then
538 Target_List := Priv_Decls;
540 elsif No (Vis_Decls) then
541 Target_List := New_List;
542 Set_Private_Declarations (Spec, Target_List);
543 else
544 Target_List := Vis_Decls;
545 end if;
547 Build_Package_Dispatch_Tables (N);
548 end;
550 else pragma Assert (Nkind (N) = N_Package_Body);
551 declare
552 Spec_Id : constant Entity_Id := Corresponding_Spec (N);
554 begin
555 Push_Scope (Spec_Id);
556 Target_List := Declarations (N);
557 Build_Dispatch_Tables (Target_List);
558 Pop_Scope;
559 end;
560 end if;
561 end Build_Static_Dispatch_Tables;
563 ------------------------------
564 -- Convert_Tag_To_Interface --
565 ------------------------------
567 function Convert_Tag_To_Interface
568 (Typ : Entity_Id;
569 Expr : Node_Id) return Node_Id
571 Loc : constant Source_Ptr := Sloc (Expr);
572 Anon_Type : Entity_Id;
573 Result : Node_Id;
575 begin
576 pragma Assert (Is_Class_Wide_Type (Typ)
577 and then Is_Interface (Typ)
578 and then
579 ((Nkind (Expr) = N_Selected_Component
580 and then Is_Tag (Entity (Selector_Name (Expr))))
581 or else
582 (Nkind (Expr) = N_Function_Call
583 and then Is_RTE (Entity (Name (Expr)), RE_Displace))));
585 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Expr);
586 Set_Directly_Designated_Type (Anon_Type, Typ);
587 Set_Etype (Anon_Type, Anon_Type);
588 Set_Can_Never_Be_Null (Anon_Type);
590 -- Decorate the size and alignment attributes of the anonymous access
591 -- type, as required by the back end.
593 Layout_Type (Anon_Type);
595 if Nkind (Expr) = N_Selected_Component
596 and then Is_Tag (Entity (Selector_Name (Expr)))
597 then
598 Result :=
599 Make_Explicit_Dereference (Loc,
600 Unchecked_Convert_To (Anon_Type,
601 Make_Attribute_Reference (Loc,
602 Prefix => Expr,
603 Attribute_Name => Name_Address)));
604 else
605 Result :=
606 Make_Explicit_Dereference (Loc,
607 Unchecked_Convert_To (Anon_Type, Expr));
608 end if;
610 return Result;
611 end Convert_Tag_To_Interface;
613 -------------------
614 -- CPP_Num_Prims --
615 -------------------
617 function CPP_Num_Prims (Typ : Entity_Id) return Nat is
618 CPP_Typ : Entity_Id;
619 Tag_Comp : Entity_Id;
621 begin
622 if not Is_Tagged_Type (Typ)
623 or else not Is_CPP_Class (Root_Type (Typ))
624 then
625 return 0;
627 else
628 CPP_Typ := Enclosing_CPP_Parent (Typ);
629 Tag_Comp := First_Tag_Component (CPP_Typ);
631 -- If number of primitives already set in the tag component, use it
633 if Present (Tag_Comp)
634 and then Present (DT_Entry_Count (Tag_Comp))
635 then
636 return UI_To_Int (DT_Entry_Count (Tag_Comp));
638 -- Otherwise, count the primitives of the enclosing CPP type
640 else
641 return List_Length (Primitive_Operations (CPP_Typ));
642 end if;
643 end if;
644 end CPP_Num_Prims;
646 ------------------------------
647 -- Default_Prim_Op_Position --
648 ------------------------------
650 function Default_Prim_Op_Position (E : Entity_Id) return Uint is
651 TSS_Name : TSS_Name_Type;
653 begin
654 Get_Name_String (Chars (E));
655 TSS_Name :=
656 TSS_Name_Type
657 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
659 if Chars (E) = Name_uSize then
660 return Uint_1;
662 elsif TSS_Name = TSS_Stream_Read then
663 return Uint_2;
665 elsif TSS_Name = TSS_Stream_Write then
666 return Uint_3;
668 elsif TSS_Name = TSS_Stream_Input then
669 return Uint_4;
671 elsif TSS_Name = TSS_Stream_Output then
672 return Uint_5;
674 elsif Chars (E) = Name_Op_Eq then
675 return Uint_6;
677 elsif Chars (E) = Name_uAssign then
678 return Uint_7;
680 elsif TSS_Name = TSS_Deep_Adjust then
681 return Uint_8;
683 elsif TSS_Name = TSS_Deep_Finalize then
684 return Uint_9;
686 elsif TSS_Name = TSS_Put_Image then
687 return Uint_10;
689 -- In VM targets unconditionally allow obtaining the position associated
690 -- with predefined interface primitives since in these platforms any
691 -- tagged type has these primitives.
693 elsif Ada_Version >= Ada_2005 or else not Tagged_Type_Expansion then
694 if Chars (E) = Name_uDisp_Asynchronous_Select then
695 return Uint_11;
697 elsif Chars (E) = Name_uDisp_Conditional_Select then
698 return Uint_12;
700 elsif Chars (E) = Name_uDisp_Get_Prim_Op_Kind then
701 return Uint_13;
703 elsif Chars (E) = Name_uDisp_Get_Task_Id then
704 return Uint_14;
706 elsif Chars (E) = Name_uDisp_Requeue then
707 return Uint_15;
709 elsif Chars (E) = Name_uDisp_Timed_Select then
710 return Uint_16;
711 end if;
712 end if;
714 raise Program_Error;
715 end Default_Prim_Op_Position;
717 ----------------------
718 -- Elab_Flag_Needed --
719 ----------------------
721 function Elab_Flag_Needed (Typ : Entity_Id) return Boolean is
722 begin
723 return Ada_Version >= Ada_2005
724 and then not Is_Interface (Typ)
725 and then Has_Interfaces (Typ)
726 and then not Building_Static_DT (Typ);
727 end Elab_Flag_Needed;
729 -----------------------------
730 -- Expand_Dispatching_Call --
731 -----------------------------
733 procedure Expand_Dispatching_Call (Call_Node : Node_Id) is
734 Loc : constant Source_Ptr := Sloc (Call_Node);
735 Call_Typ : constant Entity_Id := Etype (Call_Node);
737 Ctrl_Arg : constant Node_Id := Controlling_Argument (Call_Node);
738 Ctrl_Typ : constant Entity_Id := Base_Type (Etype (Ctrl_Arg));
739 Param_List : constant List_Id := Parameter_Associations (Call_Node);
741 Subp : Entity_Id;
742 CW_Typ : Entity_Id;
743 New_Call : Node_Id;
744 New_Call_Name : Node_Id;
745 New_Params : List_Id := No_List;
746 Param : Node_Id;
747 Subp_Ptr_Typ : Entity_Id;
748 Subp_Typ : Entity_Id;
749 Typ : Entity_Id;
750 Eq_Prim_Op : Entity_Id := Empty;
751 Controlling_Tag : Node_Id;
753 function New_Value (From : Node_Id) return Node_Id;
754 -- From is the original Expression. New_Value is equivalent to a call
755 -- to Duplicate_Subexpr with an explicit dereference when From is an
756 -- access parameter.
758 ---------------
759 -- New_Value --
760 ---------------
762 function New_Value (From : Node_Id) return Node_Id is
763 Res : constant Node_Id := Duplicate_Subexpr (From);
764 begin
765 if Is_Access_Type (Etype (From)) then
766 return
767 Make_Explicit_Dereference (Sloc (From),
768 Prefix => Res);
769 else
770 return Res;
771 end if;
772 end New_Value;
774 -- Local variables
776 New_Node : Node_Id;
777 SCIL_Node : Node_Id := Empty;
778 SCIL_Related_Node : Node_Id := Call_Node;
780 -- Start of processing for Expand_Dispatching_Call
782 begin
783 if No_Run_Time_Mode then
784 Error_Msg_CRT ("tagged types", Call_Node);
785 return;
786 end if;
788 -- Expand_Dispatching_Call is called directly from the semantics, so we
789 -- only proceed if the expander is active.
791 if not Expander_Active
793 -- And there is no need to expand the call if we are compiling under
794 -- restriction No_Dispatching_Calls; the semantic analyzer has
795 -- previously notified the violation of this restriction.
797 or else Restriction_Active (No_Dispatching_Calls)
799 -- No action needed if the dispatching call has been already expanded
801 or else Is_Expanded_Dispatching_Call (Name (Call_Node))
802 then
803 return;
804 end if;
806 -- Set subprogram. If this is an inherited operation that was
807 -- overridden, the body that is being called is its alias.
809 Subp := Entity (Name (Call_Node));
811 if Present (Alias (Subp))
812 and then Is_Inherited_Operation (Subp)
813 and then No (DTC_Entity (Subp))
814 then
815 Subp := Alias (Subp);
816 end if;
818 -- Definition of the class-wide type and the tagged type
820 -- If the controlling argument is itself a tag rather than a tagged
821 -- object, then use the class-wide type associated with the subprogram's
822 -- controlling type. This case can occur when a call to an inherited
823 -- primitive has an actual that originated from a default parameter
824 -- given by a tag-indeterminate call and when there is no other
825 -- controlling argument providing the tag (AI-239 requires dispatching).
826 -- This capability of dispatching directly by tag is also needed by the
827 -- implementation of AI-260 (for the generic dispatching constructors).
829 if Is_RTE (Ctrl_Typ, RE_Tag)
830 or else Is_RTE (Ctrl_Typ, RE_Interface_Tag)
831 then
832 CW_Typ := Class_Wide_Type (Find_Dispatching_Type (Subp));
834 -- Class_Wide_Type is applied to the expressions used to initialize
835 -- CW_Typ, to ensure that CW_Typ always denotes a class-wide type, since
836 -- there are cases where the controlling type is resolved to a specific
837 -- type (such as for designated types of arguments such as CW'Access).
839 elsif Is_Access_Type (Ctrl_Typ) then
840 CW_Typ := Class_Wide_Type (Designated_Type (Ctrl_Typ));
842 else
843 CW_Typ := Class_Wide_Type (Ctrl_Typ);
844 end if;
846 Typ := Find_Specific_Type (CW_Typ);
848 -- The tagged type of a dispatching call must be frozen at this stage
850 pragma Assert (Is_Frozen (Typ));
852 if not Is_Limited_Type (Typ) then
853 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
854 end if;
856 -- Dispatching call to C++ primitive. Create a new parameter list
857 -- with no tag checks.
859 New_Params := New_List;
861 if Is_CPP_Class (Typ) then
862 Param := First_Actual (Call_Node);
863 while Present (Param) loop
864 Append_To (New_Params, Relocate_Node (Param));
865 Next_Actual (Param);
866 end loop;
868 -- Dispatching call to Ada primitive
870 elsif Present (Param_List) then
871 Apply_Tag_Checks (Call_Node);
873 Param := First_Actual (Call_Node);
874 while Present (Param) loop
876 -- Cases in which we may have generated run-time checks. Note that
877 -- we strip any qualification from Param before comparing with the
878 -- already-stripped controlling argument.
880 if Unqualify (Param) = Ctrl_Arg or else Subp = Eq_Prim_Op then
881 Append_To (New_Params,
882 Duplicate_Subexpr_Move_Checks (Param));
884 elsif Nkind (Parent (Param)) /= N_Parameter_Association
885 or else not Is_Accessibility_Actual (Parent (Param))
886 then
887 Append_To (New_Params, Relocate_Node (Param));
888 end if;
890 Next_Actual (Param);
891 end loop;
892 end if;
894 -- Generate the appropriate subprogram designated type
896 Subp_Typ := Create_Itype (E_Subprogram_Type, Call_Node);
897 Copy_Strub_Mode (Subp_Typ, Subp);
898 Set_Convention (Subp_Typ, Convention (Subp));
900 -- If this is a function and it has a controlling tagged result, then
901 -- the call is dispatching on result and returns the class-wide type.
903 if Ekind (Subp) = E_Function
904 and then Has_Controlling_Result (Subp)
905 and then Is_Tagged_Type (Etype (Subp))
906 then
907 Set_Etype (Subp_Typ, Class_Wide_Type (Etype (Subp)));
908 Set_Returns_By_Ref (Subp_Typ, True);
909 else
910 Set_Etype (Subp_Typ, Etype (Subp));
911 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
912 end if;
914 -- Notify gigi that the designated type is a dispatching primitive
916 Set_Is_Dispatch_Table_Entity (Subp_Typ);
918 -- Create a new list of parameters which is a copy of the old formal
919 -- list including the creation of a new set of matching entities.
921 declare
922 Old_Formal : Entity_Id := First_Formal (Subp);
923 New_Formal : Entity_Id;
924 Last_Formal : Entity_Id := Empty;
926 begin
927 if Present (Old_Formal) then
928 New_Formal := New_Copy (Old_Formal);
929 Set_First_Entity (Subp_Typ, New_Formal);
930 Param := First_Actual (Call_Node);
932 loop
933 Set_Scope (New_Formal, Subp_Typ);
935 -- Change all the controlling argument types to be class-wide
936 -- to avoid a recursion in dispatching.
938 if Is_Controlling_Formal (New_Formal) then
939 Set_Etype (New_Formal, Etype (Param));
940 end if;
942 -- If the type of the formal is an itype, there was code here
943 -- introduced in 1998 in revision 1.46, to create a new itype
944 -- by copy. This seems useless, and in fact leads to semantic
945 -- errors when the itype is the completion of a type derived
946 -- from a private type.
948 Last_Formal := New_Formal;
949 Next_Formal (Old_Formal);
950 exit when No (Old_Formal);
952 Link_Entities (New_Formal, New_Copy (Old_Formal));
953 Next_Entity (New_Formal);
954 Next_Actual (Param);
955 end loop;
957 Unlink_Next_Entity (New_Formal);
958 Set_Last_Entity (Subp_Typ, Last_Formal);
959 end if;
961 -- Now that the explicit formals have been duplicated, any extra
962 -- formals needed by the subprogram must be duplicated; we know
963 -- that extra formals are available because they were added when
964 -- the tagged type was frozen (see Expand_Freeze_Record_Type).
966 pragma Assert (Is_Frozen (Typ));
968 -- Warning: The addition of the extra formals cannot be performed
969 -- here invoking Create_Extra_Formals since we must ensure that all
970 -- the extra formals of the pointer type and the target subprogram
971 -- match (and for functions that return a tagged type the profile of
972 -- the built subprogram type always returns a class-wide type, which
973 -- may affect the addition of some extra formals).
975 if Present (Last_Formal)
976 and then Present (Extra_Formal (Last_Formal))
977 then
978 Old_Formal := Extra_Formal (Last_Formal);
979 New_Formal := New_Copy (Old_Formal);
980 Set_Scope (New_Formal, Subp_Typ);
982 Set_Extra_Formal (Last_Formal, New_Formal);
983 Set_Extra_Formals (Subp_Typ, New_Formal);
985 if Ekind (Subp) = E_Function
986 and then Present (Extra_Accessibility_Of_Result (Subp))
987 and then Extra_Accessibility_Of_Result (Subp) = Old_Formal
988 then
989 Set_Extra_Accessibility_Of_Result (Subp_Typ, New_Formal);
990 end if;
992 Old_Formal := Extra_Formal (Old_Formal);
993 while Present (Old_Formal) loop
994 Set_Extra_Formal (New_Formal, New_Copy (Old_Formal));
995 New_Formal := Extra_Formal (New_Formal);
996 Set_Scope (New_Formal, Subp_Typ);
998 if Ekind (Subp) = E_Function
999 and then Present (Extra_Accessibility_Of_Result (Subp))
1000 and then Extra_Accessibility_Of_Result (Subp) = Old_Formal
1001 then
1002 Set_Extra_Accessibility_Of_Result (Subp_Typ, New_Formal);
1003 end if;
1005 Old_Formal := Extra_Formal (Old_Formal);
1006 end loop;
1007 end if;
1008 end;
1010 -- Generate the appropriate subprogram pointer type and decorate it
1012 Subp_Ptr_Typ := Create_Itype (E_Access_Subprogram_Type, Call_Node);
1013 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
1014 Set_Directly_Designated_Type (Subp_Ptr_Typ, Subp_Typ);
1015 Set_Convention (Subp_Ptr_Typ, Convention (Subp_Typ));
1016 Layout_Type (Subp_Ptr_Typ);
1018 -- If the controlling argument is a value of type Ada.Tag or an abstract
1019 -- interface class-wide type then use it directly. Otherwise, the tag
1020 -- must be extracted from the controlling object.
1022 if Is_RTE (Ctrl_Typ, RE_Tag)
1023 or else Is_RTE (Ctrl_Typ, RE_Interface_Tag)
1024 then
1025 Controlling_Tag := Duplicate_Subexpr (Ctrl_Arg);
1027 -- Extract the tag from an unchecked type conversion. Done to avoid
1028 -- the expansion of additional code just to obtain the value of such
1029 -- tag because the current management of interface type conversions
1030 -- generates in some cases this unchecked type conversion with the
1031 -- tag of the object (see Expand_Interface_Conversion).
1033 elsif Nkind (Ctrl_Arg) = N_Unchecked_Type_Conversion
1034 and then
1035 (Is_RTE (Etype (Expression (Ctrl_Arg)), RE_Tag)
1036 or else
1037 Is_RTE (Etype (Expression (Ctrl_Arg)), RE_Interface_Tag))
1038 then
1039 Controlling_Tag := Duplicate_Subexpr (Expression (Ctrl_Arg));
1041 -- Ada 2005 (AI-251): Abstract interface class-wide type
1043 elsif Is_Interface (Ctrl_Typ) and then Is_Class_Wide_Type (Ctrl_Typ) then
1044 Controlling_Tag :=
1045 Make_Attribute_Reference (Loc,
1046 Prefix => Duplicate_Subexpr (Ctrl_Arg),
1047 Attribute_Name => Name_Tag);
1049 elsif Is_Access_Type (Ctrl_Typ) then
1050 Controlling_Tag :=
1051 Make_Selected_Component (Loc,
1052 Prefix =>
1053 Make_Explicit_Dereference (Loc,
1054 Duplicate_Subexpr_Move_Checks (Ctrl_Arg)),
1055 Selector_Name => New_Occurrence_Of (DTC_Entity (Subp), Loc));
1057 else
1058 Controlling_Tag :=
1059 Make_Selected_Component (Loc,
1060 Prefix => Duplicate_Subexpr_Move_Checks (Ctrl_Arg),
1061 Selector_Name => New_Occurrence_Of (DTC_Entity (Subp), Loc));
1062 end if;
1064 -- Handle dispatching calls to predefined primitives
1066 if Is_Predefined_Dispatching_Operation (Subp)
1067 or else Is_Predefined_Dispatching_Alias (Subp)
1068 then
1069 Build_Get_Predefined_Prim_Op_Address (Loc,
1070 Tag_Node => Controlling_Tag,
1071 Position => DT_Position (Subp),
1072 New_Node => New_Node);
1074 -- Handle dispatching calls to user-defined primitives
1076 else
1077 Build_Get_Prim_Op_Address (Loc,
1078 Typ => Underlying_Type (Find_Dispatching_Type (Subp)),
1079 Tag_Node => Controlling_Tag,
1080 Position => DT_Position (Subp),
1081 New_Node => New_Node);
1082 end if;
1084 New_Call_Name :=
1085 Unchecked_Convert_To (Subp_Ptr_Typ, New_Node);
1087 -- Generate the SCIL node for this dispatching call. Done now because
1088 -- attribute SCIL_Controlling_Tag must be set after the new call name
1089 -- is built to reference the nodes that will see the SCIL backend
1090 -- (because Build_Get_Prim_Op_Address generates an unchecked type
1091 -- conversion which relocates the controlling tag node).
1093 if Generate_SCIL then
1094 SCIL_Node := Make_SCIL_Dispatching_Call (Sloc (Call_Node));
1095 Set_SCIL_Entity (SCIL_Node, Typ);
1096 Set_SCIL_Target_Prim (SCIL_Node, Subp);
1098 -- Common case: the controlling tag is the tag of an object
1099 -- (for example, obj.tag)
1101 if Nkind (Controlling_Tag) = N_Selected_Component then
1102 Set_SCIL_Controlling_Tag (SCIL_Node, Controlling_Tag);
1104 -- Handle renaming of selected component
1106 elsif Nkind (Controlling_Tag) = N_Identifier
1107 and then Nkind (Parent (Entity (Controlling_Tag))) =
1108 N_Object_Renaming_Declaration
1109 and then Nkind (Name (Parent (Entity (Controlling_Tag)))) =
1110 N_Selected_Component
1111 then
1112 Set_SCIL_Controlling_Tag (SCIL_Node,
1113 Name (Parent (Entity (Controlling_Tag))));
1115 -- If the controlling tag is an identifier, the SCIL node references
1116 -- the corresponding object or parameter declaration
1118 elsif Nkind (Controlling_Tag) = N_Identifier
1119 and then Nkind (Parent (Entity (Controlling_Tag))) in
1120 N_Object_Declaration | N_Parameter_Specification
1121 then
1122 Set_SCIL_Controlling_Tag (SCIL_Node,
1123 Parent (Entity (Controlling_Tag)));
1125 -- If the controlling tag is a dereference, the SCIL node references
1126 -- the corresponding object or parameter declaration
1128 elsif Nkind (Controlling_Tag) = N_Explicit_Dereference
1129 and then Nkind (Prefix (Controlling_Tag)) = N_Identifier
1130 and then Nkind (Parent (Entity (Prefix (Controlling_Tag)))) in
1131 N_Object_Declaration | N_Parameter_Specification
1132 then
1133 Set_SCIL_Controlling_Tag (SCIL_Node,
1134 Parent (Entity (Prefix (Controlling_Tag))));
1136 -- Depending on whether a dereference is involved, the SCIL node
1137 -- references the corresponding object/parameter declaration or
1138 -- the internal object declaration containing the tag of the type.
1140 elsif Nkind (Controlling_Tag) = N_Attribute_Reference
1141 and then Attribute_Name (Controlling_Tag) = Name_Tag
1142 then
1143 declare
1144 Prefix_Node : constant Node_Id := Prefix (Controlling_Tag);
1145 Ent : constant Entity_Id := Entity
1146 (if Nkind (Prefix_Node) = N_Explicit_Dereference then
1147 Prefix (Prefix_Node)
1148 else
1149 Prefix_Node);
1151 begin
1152 if Ekind (Ent) in E_Record_Type
1153 | E_Record_Subtype
1154 | E_Record_Type_With_Private
1155 then
1156 Set_SCIL_Controlling_Tag (SCIL_Node,
1157 Parent
1158 (Node
1159 (First_Elmt
1160 (Access_Disp_Table (Ent)))));
1162 else
1163 Set_SCIL_Controlling_Tag (SCIL_Node, Parent (Ent));
1164 end if;
1165 end;
1167 -- Interfaces are not supported. For now we leave the SCIL node
1168 -- decorated with the Controlling_Tag. More work needed here???
1170 elsif Is_Interface (Etype (Controlling_Tag)) then
1171 Set_SCIL_Controlling_Tag (SCIL_Node, Controlling_Tag);
1173 else
1174 pragma Assert (False);
1175 null;
1176 end if;
1177 end if;
1179 if Nkind (Call_Node) = N_Function_Call then
1180 New_Call :=
1181 Make_Function_Call (Loc,
1182 Name => New_Call_Name,
1183 Parameter_Associations => New_Params);
1185 -- If this is a dispatching "=", we must first compare the tags so
1186 -- we generate: x.tag = y.tag and then x = y
1188 if Subp = Eq_Prim_Op then
1189 Param := First_Actual (Call_Node);
1190 New_Call :=
1191 Make_And_Then (Loc,
1192 Left_Opnd =>
1193 Make_Op_Eq (Loc,
1194 Left_Opnd =>
1195 Make_Selected_Component (Loc,
1196 Prefix => New_Value (Param),
1197 Selector_Name =>
1198 New_Occurrence_Of (First_Tag_Component (Typ),
1199 Loc)),
1201 Right_Opnd =>
1202 Make_Selected_Component (Loc,
1203 Prefix =>
1204 Unchecked_Convert_To (Typ,
1205 New_Value (Next_Actual (Param))),
1206 Selector_Name =>
1207 New_Occurrence_Of
1208 (First_Tag_Component (Typ), Loc))),
1209 Right_Opnd => New_Call);
1211 SCIL_Related_Node := Right_Opnd (New_Call);
1212 end if;
1214 else
1215 New_Call :=
1216 Make_Procedure_Call_Statement (Loc,
1217 Name => New_Call_Name,
1218 Parameter_Associations => New_Params);
1219 end if;
1221 -- Register the dispatching call in the call graph nodes table
1223 Register_CG_Node (Call_Node);
1225 Rewrite (Call_Node, New_Call);
1227 -- Associate the SCIL node of this dispatching call
1229 if Generate_SCIL then
1230 Set_SCIL_Node (SCIL_Related_Node, SCIL_Node);
1231 end if;
1233 -- Suppress all checks during the analysis of the expanded code to avoid
1234 -- the generation of spurious warnings under ZFP run-time.
1236 Analyze_And_Resolve (Call_Node, Call_Typ, Suppress => All_Checks);
1237 end Expand_Dispatching_Call;
1239 ---------------------------------
1240 -- Expand_Interface_Conversion --
1241 ---------------------------------
1243 procedure Expand_Interface_Conversion (N : Node_Id) is
1245 function Underlying_Record_Type (Typ : Entity_Id) return Entity_Id;
1246 -- Return the underlying record type of Typ
1248 ----------------------------
1249 -- Underlying_Record_Type --
1250 ----------------------------
1252 function Underlying_Record_Type (Typ : Entity_Id) return Entity_Id is
1253 E : Entity_Id := Typ;
1255 begin
1256 -- Handle access types
1258 if Is_Access_Type (E) then
1259 E := Directly_Designated_Type (E);
1260 end if;
1262 -- Handle class-wide types. This conversion can appear explicitly in
1263 -- the source code. Example: I'Class (Obj)
1265 if Is_Class_Wide_Type (E) then
1266 E := Root_Type (E);
1267 end if;
1269 -- If the target type is a tagged synchronized type, the dispatch
1270 -- table info is in the corresponding record type.
1272 if Is_Concurrent_Type (E) then
1273 E := Corresponding_Record_Type (E);
1274 end if;
1276 -- Handle private types
1278 E := Underlying_Type (E);
1280 -- Handle subtypes
1282 return Base_Type (E);
1283 end Underlying_Record_Type;
1285 -- Local variables
1287 Loc : constant Source_Ptr := Sloc (N);
1288 Etyp : constant Entity_Id := Etype (N);
1289 Operand : constant Node_Id := Expression (N);
1290 Operand_Typ : Entity_Id := Etype (Operand);
1291 Func : Node_Id;
1292 Iface_Typ : constant Entity_Id := Underlying_Record_Type (Etype (N));
1293 Iface_Tag : Entity_Id;
1294 Is_Static : Boolean;
1296 -- Start of processing for Expand_Interface_Conversion
1298 begin
1299 -- Freeze the entity associated with the target interface to have
1300 -- available the attribute Access_Disp_Table.
1302 Freeze_Before (N, Iface_Typ);
1304 -- Ada 2005 (AI-345): Handle synchronized interface type derivations
1306 if Is_Concurrent_Type (Operand_Typ) then
1307 Operand_Typ := Base_Type (Corresponding_Record_Type (Operand_Typ));
1308 end if;
1310 -- No displacement of the pointer to the object needed when the type of
1311 -- the operand is not an interface type and the interface is one of
1312 -- its parent types (since they share the primary dispatch table).
1314 declare
1315 Opnd : Entity_Id := Operand_Typ;
1317 begin
1318 if Is_Access_Type (Opnd) then
1319 Opnd := Designated_Type (Opnd);
1320 end if;
1322 Opnd := Underlying_Record_Type (Opnd);
1324 if not Is_Interface (Opnd)
1325 and then Is_Ancestor (Iface_Typ, Opnd, Use_Full_View => True)
1326 then
1327 return;
1329 -- When the target type is an interface type that is an ancestor of
1330 -- the operand type, it is generally safe to skip generating code to
1331 -- displace the pointer to the object to reference the secondary
1332 -- dispatch table of the target interface type. Two scenarios are
1333 -- possible here:
1334 -- 1) The operand type is a regular tagged type
1335 -- 2) The operand type is an interface type
1336 -- In the former case the target interface and the regular tagged
1337 -- type share the primary dispatch table of the object; in the latter
1338 -- case the operand interface has all the primitives of the ancestor
1339 -- interface type (and exactly in the same dispatch table slots).
1341 -- The exception to this general rule is when the underlying object
1342 -- is built by means of a dispatching constructor (since in such case
1343 -- the expansion of the constructor call is a direct call to an
1344 -- object primitive, i.e. without thunks, and the expansion of
1345 -- the constructor call adds this explicit conversion to the target
1346 -- interface type to force the displacement of the pointer to the
1347 -- object to reference the corresponding secondary dispatch table
1348 -- (cf. Make_DT and Expand_Dispatching_Constructor_Call)).
1350 -- Under configurable runtime it is safe to skip generating code to
1351 -- displace the pointer to the object, because generic dispatching
1352 -- constructors are not supported.
1354 elsif Is_Interface (Iface_Typ)
1355 and then Is_Ancestor (Iface_Typ, Opnd, Use_Full_View => True)
1356 and then not RTE_Available (RE_Displace)
1357 then
1358 return;
1359 end if;
1360 end;
1362 -- Evaluate if we can statically displace the pointer to the object
1364 declare
1365 Opnd_Typ : constant Node_Id := Underlying_Record_Type (Operand_Typ);
1367 begin
1368 Is_Static :=
1369 not Is_Interface (Opnd_Typ)
1370 and then Interface_Present_In_Ancestor
1371 (Typ => Opnd_Typ,
1372 Iface => Iface_Typ)
1373 and then (Etype (Opnd_Typ) = Opnd_Typ
1374 or else not
1375 Is_Variable_Size_Record (Etype (Opnd_Typ)));
1376 end;
1378 if not Tagged_Type_Expansion then
1379 return;
1381 -- A static conversion to an interface type that is not class-wide is
1382 -- curious but legal if the interface operation is a null procedure.
1383 -- If the operation is abstract it will be rejected later.
1385 elsif Is_Static
1386 and then Is_Interface (Etype (N))
1387 and then not Is_Class_Wide_Type (Etype (N))
1388 and then Comes_From_Source (N)
1389 then
1390 Rewrite (N, Unchecked_Convert_To (Etype (N), N));
1391 Analyze (N);
1392 return;
1393 end if;
1395 if not Is_Static then
1397 -- Give error if configurable run-time and Displace not available
1399 if not RTE_Available (RE_Displace) then
1400 Error_Msg_CRT ("dynamic interface conversion", N);
1401 return;
1402 end if;
1404 -- Handle conversion of access-to-class-wide interface types. Target
1405 -- can be an access to an object or an access to another class-wide
1406 -- interface (see -1- and -2- in the following example):
1408 -- type Iface1_Ref is access all Iface1'Class;
1409 -- type Iface2_Ref is access all Iface1'Class;
1411 -- Acc1 : Iface1_Ref := new ...
1412 -- Obj : Obj_Ref := Obj_Ref (Acc); -- 1
1413 -- Acc2 : Iface2_Ref := Iface2_Ref (Acc); -- 2
1415 if Is_Access_Type (Operand_Typ) then
1416 Rewrite (N,
1417 Unchecked_Convert_To (Etype (N),
1418 Make_Function_Call (Loc,
1419 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
1420 Parameter_Associations => New_List (
1422 Unchecked_Convert_To (RTE (RE_Address),
1423 Relocate_Node (Expression (N))),
1425 New_Occurrence_Of
1426 (Node (First_Elmt (Access_Disp_Table (Iface_Typ))),
1427 Loc)))));
1429 Analyze (N);
1430 return;
1431 end if;
1433 Rewrite (N,
1434 Make_Function_Call (Loc,
1435 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
1436 Parameter_Associations => New_List (
1437 Make_Attribute_Reference (Loc,
1438 Prefix => Relocate_Node (Expression (N)),
1439 Attribute_Name => Name_Address),
1441 New_Occurrence_Of
1442 (Node (First_Elmt (Access_Disp_Table (Iface_Typ))),
1443 Loc))));
1445 Analyze (N);
1447 -- If target is a class-wide interface, change the type of the data
1448 -- returned by IW_Convert to indicate this is a dispatching call.
1450 declare
1451 New_Itype : Entity_Id;
1453 begin
1454 New_Itype := Create_Itype (E_Anonymous_Access_Type, N);
1455 Set_Etype (New_Itype, New_Itype);
1456 Set_Directly_Designated_Type (New_Itype, Etyp);
1458 Rewrite (N,
1459 Make_Explicit_Dereference (Loc,
1460 Prefix =>
1461 Unchecked_Convert_To (New_Itype, Relocate_Node (N))));
1462 Analyze (N);
1463 Freeze_Itype (New_Itype, N);
1465 return;
1466 end;
1467 end if;
1469 Iface_Tag := Find_Interface_Tag (Operand_Typ, Iface_Typ);
1470 pragma Assert (Present (Iface_Tag));
1472 -- Keep separate access types to interfaces because one internal
1473 -- function is used to handle the null value (see following comments)
1475 if not Is_Access_Type (Etype (N)) then
1477 -- Statically displace the pointer to the object to reference the
1478 -- component containing the secondary dispatch table.
1480 Rewrite (N,
1481 Convert_Tag_To_Interface (Class_Wide_Type (Iface_Typ),
1482 Make_Selected_Component (Loc,
1483 Prefix => Relocate_Node (Expression (N)),
1484 Selector_Name => New_Occurrence_Of (Iface_Tag, Loc))));
1486 else
1487 -- Build internal function to handle the case in which the actual is
1488 -- null. If the actual is null returns null because no displacement
1489 -- is required; otherwise performs a type conversion that will be
1490 -- expanded in the code that returns the value of the displaced
1491 -- actual. That is:
1493 -- function Func (O : Address) return Iface_Typ is
1494 -- type Op_Typ is access all Operand_Typ;
1495 -- Aux : Op_Typ := To_Op_Typ (O);
1496 -- begin
1497 -- if O = Null_Address then
1498 -- return null;
1499 -- else
1500 -- return Iface_Typ!(Aux.Iface_Tag'Address);
1501 -- end if;
1502 -- end Func;
1504 declare
1505 Desig_Typ : Entity_Id;
1506 Fent : Entity_Id;
1507 New_Typ_Decl : Node_Id;
1508 Stats : List_Id;
1510 begin
1511 Desig_Typ := Etype (Expression (N));
1513 if Is_Access_Type (Desig_Typ) then
1514 Desig_Typ :=
1515 Available_View (Directly_Designated_Type (Desig_Typ));
1516 end if;
1518 if Is_Concurrent_Type (Desig_Typ) then
1519 Desig_Typ := Base_Type (Corresponding_Record_Type (Desig_Typ));
1520 end if;
1522 New_Typ_Decl :=
1523 Make_Full_Type_Declaration (Loc,
1524 Defining_Identifier => Make_Temporary (Loc, 'T'),
1525 Type_Definition =>
1526 Make_Access_To_Object_Definition (Loc,
1527 All_Present => True,
1528 Null_Exclusion_Present => False,
1529 Constant_Present => False,
1530 Subtype_Indication =>
1531 New_Occurrence_Of (Desig_Typ, Loc)));
1533 Stats := New_List (
1534 Make_Simple_Return_Statement (Loc,
1535 Unchecked_Convert_To (Etype (N),
1536 Make_Attribute_Reference (Loc,
1537 Prefix =>
1538 Make_Selected_Component (Loc,
1539 Prefix =>
1540 Unchecked_Convert_To
1541 (Defining_Identifier (New_Typ_Decl),
1542 Make_Identifier (Loc, Name_uO)),
1543 Selector_Name =>
1544 New_Occurrence_Of (Iface_Tag, Loc)),
1545 Attribute_Name => Name_Address))));
1547 -- If the type is null-excluding, no need for the null branch.
1548 -- Otherwise we need to check for it and return null.
1550 if not Can_Never_Be_Null (Etype (N)) then
1551 Stats := New_List (
1552 Make_If_Statement (Loc,
1553 Condition =>
1554 Make_Op_Eq (Loc,
1555 Left_Opnd => Make_Identifier (Loc, Name_uO),
1556 Right_Opnd => New_Occurrence_Of
1557 (RTE (RE_Null_Address), Loc)),
1559 Then_Statements => New_List (
1560 Make_Simple_Return_Statement (Loc, Make_Null (Loc))),
1561 Else_Statements => Stats));
1562 end if;
1564 Fent := Make_Temporary (Loc, 'F');
1565 Func :=
1566 Make_Subprogram_Body (Loc,
1567 Specification =>
1568 Make_Function_Specification (Loc,
1569 Defining_Unit_Name => Fent,
1571 Parameter_Specifications => New_List (
1572 Make_Parameter_Specification (Loc,
1573 Defining_Identifier =>
1574 Make_Defining_Identifier (Loc, Name_uO),
1575 Parameter_Type =>
1576 New_Occurrence_Of (RTE (RE_Address), Loc))),
1578 Result_Definition =>
1579 New_Occurrence_Of (Etype (N), Loc)),
1581 Declarations => New_List (New_Typ_Decl),
1583 Handled_Statement_Sequence =>
1584 Make_Handled_Sequence_Of_Statements (Loc, Stats));
1586 -- Place function body before the expression containing the
1587 -- conversion. We suppress all checks because the body of the
1588 -- internally generated function already takes care of the case
1589 -- in which the actual is null; therefore there is no need to
1590 -- double check that the pointer is not null when the program
1591 -- executes the alternative that performs the type conversion).
1593 Insert_Action (N, Func, Suppress => All_Checks);
1595 if Is_Access_Type (Etype (Expression (N))) then
1597 -- Generate: Func (Address!(Expression))
1599 Rewrite (N,
1600 Make_Function_Call (Loc,
1601 Name => New_Occurrence_Of (Fent, Loc),
1602 Parameter_Associations => New_List (
1603 Unchecked_Convert_To (RTE (RE_Address),
1604 Relocate_Node (Expression (N))))));
1606 else
1607 -- Generate: Func (Operand_Typ!(Expression)'Address)
1609 Rewrite (N,
1610 Make_Function_Call (Loc,
1611 Name => New_Occurrence_Of (Fent, Loc),
1612 Parameter_Associations => New_List (
1613 Make_Attribute_Reference (Loc,
1614 Prefix => Unchecked_Convert_To (Operand_Typ,
1615 Relocate_Node (Expression (N))),
1616 Attribute_Name => Name_Address))));
1617 end if;
1618 end;
1619 end if;
1621 Analyze (N);
1622 end Expand_Interface_Conversion;
1624 ------------------------------
1625 -- Expand_Interface_Actuals --
1626 ------------------------------
1628 procedure Expand_Interface_Actuals (Call_Node : Node_Id) is
1629 Actual : Node_Id;
1630 Actual_Dup : Node_Id;
1631 Actual_Typ : Entity_Id;
1632 Anon : Entity_Id;
1633 Conversion : Node_Id;
1634 Formal : Entity_Id;
1635 Formal_Typ : Entity_Id;
1636 Subp : Entity_Id;
1637 Formal_DDT : Entity_Id := Empty; -- initialize to prevent warning
1638 Actual_DDT : Entity_Id := Empty; -- initialize to prevent warning
1640 begin
1641 -- This subprogram is called directly from the semantics, so we need a
1642 -- check to see whether expansion is active before proceeding.
1644 if not Expander_Active then
1645 return;
1646 end if;
1648 -- Call using access to subprogram with explicit dereference
1650 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
1651 Subp := Etype (Name (Call_Node));
1653 -- Call using selected component
1655 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
1656 Subp := Entity (Selector_Name (Name (Call_Node)));
1658 -- Call using direct name
1660 else
1661 Subp := Entity (Name (Call_Node));
1662 end if;
1664 -- Ada 2005 (AI-251): Look for interface type formals to force "this"
1665 -- displacement
1667 Formal := First_Formal (Subp);
1668 Actual := First_Actual (Call_Node);
1669 while Present (Formal) loop
1670 Formal_Typ := Etype (Formal);
1672 if Has_Non_Limited_View (Formal_Typ) then
1673 Formal_Typ := Non_Limited_View (Formal_Typ);
1674 end if;
1676 if Ekind (Formal_Typ) = E_Record_Type_With_Private then
1677 Formal_Typ := Full_View (Formal_Typ);
1678 end if;
1680 if Is_Access_Type (Formal_Typ) then
1681 Formal_DDT := Directly_Designated_Type (Formal_Typ);
1683 if Has_Non_Limited_View (Formal_DDT) then
1684 Formal_DDT := Non_Limited_View (Formal_DDT);
1685 end if;
1686 end if;
1688 Actual_Typ := Etype (Actual);
1690 if Has_Non_Limited_View (Actual_Typ) then
1691 Actual_Typ := Non_Limited_View (Actual_Typ);
1692 end if;
1694 if Is_Access_Type (Actual_Typ) then
1695 Actual_DDT := Directly_Designated_Type (Actual_Typ);
1697 if Has_Non_Limited_View (Actual_DDT) then
1698 Actual_DDT := Non_Limited_View (Actual_DDT);
1699 end if;
1700 end if;
1702 if Is_Interface (Formal_Typ)
1703 and then Is_Class_Wide_Type (Formal_Typ)
1704 then
1705 -- No need to displace the pointer if the type of the actual
1706 -- coincides with the type of the formal.
1708 if Actual_Typ = Formal_Typ then
1709 null;
1711 -- No need to displace the pointer if the interface type is a
1712 -- parent of the type of the actual because in this case the
1713 -- interface primitives are located in the primary dispatch table.
1715 elsif Is_Ancestor (Formal_Typ, Actual_Typ,
1716 Use_Full_View => True)
1717 then
1718 null;
1720 -- Implicit conversion to the class-wide formal type to force the
1721 -- displacement of the pointer.
1723 else
1724 -- Normally, expansion of actuals for calls to build-in-place
1725 -- functions happens as part of Expand_Actuals, but in this
1726 -- case the call will be wrapped in a conversion and soon after
1727 -- expanded further to handle the displacement for a class-wide
1728 -- interface conversion, so if this is a BIP call then we need
1729 -- to handle it now.
1731 if Is_Build_In_Place_Function_Call (Actual) then
1732 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1733 end if;
1735 Conversion := Convert_To (Formal_Typ, Relocate_Node (Actual));
1736 Rewrite (Actual, Conversion);
1737 Analyze_And_Resolve (Actual, Formal_Typ);
1738 end if;
1740 -- Access to class-wide interface type
1742 elsif Is_Access_Type (Formal_Typ)
1743 and then Is_Interface (Formal_DDT)
1744 and then Is_Class_Wide_Type (Formal_DDT)
1745 and then Interface_Present_In_Ancestor
1746 (Typ => Actual_DDT,
1747 Iface => Etype (Formal_DDT))
1748 then
1749 -- Handle attributes 'Access and 'Unchecked_Access
1751 if Nkind (Actual) = N_Attribute_Reference
1752 and then
1753 (Attribute_Name (Actual) = Name_Access
1754 or else Attribute_Name (Actual) = Name_Unchecked_Access)
1755 then
1756 -- This case must have been handled by the analysis and
1757 -- expansion of 'Access. The only exception is when types
1758 -- match and no further expansion is required.
1760 pragma Assert (Base_Type (Etype (Prefix (Actual)))
1761 = Base_Type (Formal_DDT));
1762 null;
1764 -- No need to displace the pointer if the type of the actual
1765 -- coincides with the type of the formal.
1767 elsif Actual_DDT = Formal_DDT then
1768 null;
1770 -- No need to displace the pointer if the interface type is
1771 -- a parent of the type of the actual because in this case the
1772 -- interface primitives are located in the primary dispatch table.
1774 elsif Is_Ancestor (Formal_DDT, Actual_DDT,
1775 Use_Full_View => True)
1776 then
1777 null;
1779 else
1780 Actual_Dup := Relocate_Node (Actual);
1782 if From_Limited_With (Actual_Typ) then
1784 -- If the type of the actual parameter comes from a limited
1785 -- with_clause and the nonlimited view is already available,
1786 -- we replace the anonymous access type by a duplicate
1787 -- declaration whose designated type is the nonlimited view.
1789 if Has_Non_Limited_View (Actual_DDT) then
1790 Anon := New_Copy (Actual_Typ);
1792 if Is_Itype (Anon) then
1793 Set_Scope (Anon, Current_Scope);
1794 end if;
1796 Set_Directly_Designated_Type
1797 (Anon, Non_Limited_View (Actual_DDT));
1798 Set_Etype (Actual_Dup, Anon);
1799 end if;
1800 end if;
1802 Conversion := Convert_To (Formal_Typ, Actual_Dup);
1803 Rewrite (Actual, Conversion);
1804 Analyze_And_Resolve (Actual, Formal_Typ);
1805 end if;
1806 end if;
1808 Next_Actual (Actual);
1809 Next_Formal (Formal);
1810 end loop;
1811 end Expand_Interface_Actuals;
1813 ----------------------------
1814 -- Expand_Interface_Thunk --
1815 ----------------------------
1817 procedure Expand_Interface_Thunk
1818 (Prim : Entity_Id;
1819 Thunk_Id : out Entity_Id;
1820 Thunk_Code : out List_Id;
1821 Iface : Entity_Id)
1823 Actuals : constant List_Id := New_List;
1824 Decl : constant List_Id := New_List;
1825 Formals : constant List_Id := New_List;
1826 Loc : constant Source_Ptr := Sloc (Prim);
1827 Target : constant Entity_Id := Ultimate_Alias (Prim);
1828 Is_Predef_Op : constant Boolean :=
1829 Is_Predefined_Dispatching_Operation (Prim)
1830 or else Is_Predefined_Dispatching_Operation (Target);
1832 Decl_1 : Node_Id;
1833 Decl_2 : Node_Id;
1834 Expr : Node_Id;
1835 Formal : Entity_Id;
1836 Ftyp : Entity_Id;
1837 Iface_Formal : Entity_Id;
1838 New_Arg : Node_Id;
1839 Offset_To_Top : Node_Id;
1840 Target_Formal : Entity_Id;
1842 begin
1843 Thunk_Id := Empty;
1844 Thunk_Code := Empty_List;
1846 -- No thunk needed if the primitive has been eliminated
1848 if Is_Eliminated (Target) then
1849 return;
1851 -- No thunk needed if the primitive has no formals. In this case, this
1852 -- must be a function with a controlling result.
1854 elsif No (First_Formal (Target)) then
1855 pragma Assert (Ekind (Target) = E_Function
1856 and then Has_Controlling_Result (Target));
1858 return;
1859 end if;
1861 -- Duplicate the formals of the target primitive. In the thunk, the type
1862 -- of the controlling formal is the covered interface type (instead of
1863 -- the target tagged type). Done to avoid problems with discriminated
1864 -- tagged types because, if the controlling type has discriminants with
1865 -- default values, then the type conversions done inside the body of
1866 -- the thunk (after the displacement of the pointer to the base of the
1867 -- actual object) generate code that modify its contents.
1869 -- Note: This special management is not done for predefined primitives
1870 -- because they don't have available the Interface_Alias attribute (see
1871 -- Sem_Ch3.Add_Internal_Interface_Entities).
1873 if Is_Predef_Op then
1874 Iface_Formal := Empty;
1875 else
1876 Iface_Formal := First_Formal (Interface_Alias (Prim));
1877 end if;
1879 Formal := First_Formal (Target);
1880 while Present (Formal) loop
1881 -- Use the interface type as the type of the controlling formal (see
1882 -- comment above).
1884 if not Is_Controlling_Formal (Formal) then
1885 Ftyp := Etype (Formal);
1886 Expr := New_Copy_Tree (Expression (Parent (Formal)));
1888 -- For predefined primitives the controlling type of the thunk is
1889 -- the interface type passed by the caller (since they don't have
1890 -- available the Interface_Alias attribute; see comment above).
1892 elsif Is_Predef_Op then
1893 Ftyp := Iface;
1894 Expr := Empty;
1896 else
1897 Ftyp := Etype (Iface_Formal);
1898 Expr := Empty;
1900 -- Sanity check performed to ensure the proper controlling type
1901 -- when the thunk has exactly one controlling parameter and it
1902 -- comes first. In such a case, the GCC back end reuses the C++
1903 -- thunks machinery which perform a computation equivalent to
1904 -- the code generated by the expander; for other cases the GCC
1905 -- back end translates the expanded code unmodified. However, as
1906 -- a generalization, the check is performed for all controlling
1907 -- types.
1909 if Is_Access_Type (Ftyp) then
1910 pragma Assert (Base_Type (Designated_Type (Ftyp)) = Iface);
1911 null;
1912 else
1913 Ftyp := Base_Type (Ftyp);
1914 pragma Assert (Ftyp = Iface);
1915 end if;
1916 end if;
1918 Append_To (Formals,
1919 Make_Parameter_Specification (Loc,
1920 Defining_Identifier =>
1921 Make_Defining_Identifier (Sloc (Formal),
1922 Chars => Chars (Formal)),
1923 Aliased_Present => Aliased_Present (Parent (Formal)),
1924 In_Present => In_Present (Parent (Formal)),
1925 Out_Present => Out_Present (Parent (Formal)),
1926 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
1927 Expression => Expr));
1929 if not Is_Predef_Op then
1930 Next_Formal (Iface_Formal);
1931 end if;
1933 Next_Formal (Formal);
1934 end loop;
1936 Target_Formal := First_Formal (Target);
1937 Formal := First (Formals);
1938 while Present (Formal) loop
1940 -- If the parent is a constrained discriminated type, then the
1941 -- primitive operation will have been defined on a first subtype.
1942 -- For proper matching with controlling type, use base type.
1944 if Ekind (Target_Formal) = E_In_Parameter
1945 and then Ekind (Etype (Target_Formal)) = E_Anonymous_Access_Type
1946 then
1947 Ftyp :=
1948 Base_Type (Directly_Designated_Type (Etype (Target_Formal)));
1949 else
1950 Ftyp := Base_Type (Etype (Target_Formal));
1951 end if;
1953 -- For concurrent types, the relevant information is found in the
1954 -- Corresponding_Record_Type, rather than the type entity itself.
1956 if Is_Concurrent_Type (Ftyp) then
1957 Ftyp := Corresponding_Record_Type (Ftyp);
1958 end if;
1960 if Ekind (Target_Formal) = E_In_Parameter
1961 and then Ekind (Etype (Target_Formal)) = E_Anonymous_Access_Type
1962 and then Is_Controlling_Formal (Target_Formal)
1963 then
1964 -- Generate:
1965 -- type T is access all <<type of the target formal>>
1966 -- S : constant Address := Address!(Formal)
1967 -- + Offset_To_Top (Address!(Formal))
1969 Decl_2 :=
1970 Make_Full_Type_Declaration (Loc,
1971 Defining_Identifier => Make_Temporary (Loc, 'T'),
1972 Type_Definition =>
1973 Make_Access_To_Object_Definition (Loc,
1974 All_Present => True,
1975 Null_Exclusion_Present => False,
1976 Constant_Present => False,
1977 Subtype_Indication =>
1978 New_Occurrence_Of (Ftyp, Loc)));
1980 New_Arg :=
1981 Unchecked_Convert_To (RTE (RE_Address),
1982 New_Occurrence_Of (Defining_Identifier (Formal), Loc));
1984 if not RTE_Available (RE_Offset_To_Top) then
1985 Offset_To_Top :=
1986 Build_Offset_To_Top (Loc, New_Arg);
1987 else
1988 Offset_To_Top :=
1989 Make_Function_Call (Loc,
1990 Name => New_Occurrence_Of (RTE (RE_Offset_To_Top), Loc),
1991 Parameter_Associations => New_List (New_Arg));
1992 end if;
1994 Decl_1 :=
1995 Make_Object_Declaration (Loc,
1996 Defining_Identifier => Make_Temporary (Loc, 'S'),
1997 Constant_Present => True,
1998 Object_Definition =>
1999 New_Occurrence_Of (RTE (RE_Address), Loc),
2000 Expression =>
2001 Make_Function_Call (Loc,
2002 Name =>
2003 Make_Expanded_Name (Loc,
2004 Chars => Name_Op_Add,
2005 Prefix =>
2006 New_Occurrence_Of
2007 (RTU_Entity (System_Storage_Elements), Loc),
2008 Selector_Name =>
2009 Make_Identifier (Loc, Name_Op_Add)),
2010 Parameter_Associations => New_List (
2011 New_Copy_Tree (New_Arg),
2012 Offset_To_Top)));
2014 Append_To (Decl, Decl_2);
2015 Append_To (Decl, Decl_1);
2017 -- Reference the new actual. Generate:
2018 -- T!(S)
2020 Append_To (Actuals,
2021 Unchecked_Convert_To
2022 (Defining_Identifier (Decl_2),
2023 New_Occurrence_Of (Defining_Identifier (Decl_1), Loc)));
2025 elsif Is_Controlling_Formal (Target_Formal) then
2027 -- Generate:
2028 -- S1 : constant Address := Formal'Address
2029 -- + Offset_To_Top (Formal'Address)
2030 -- S2 : constant Addr_Ptr := Addr_Ptr!(S1)
2032 New_Arg :=
2033 Make_Attribute_Reference (Loc,
2034 Prefix =>
2035 New_Occurrence_Of (Defining_Identifier (Formal), Loc),
2036 Attribute_Name => Name_Address);
2038 if not RTE_Available (RE_Offset_To_Top) then
2039 Offset_To_Top :=
2040 Build_Offset_To_Top (Loc, New_Arg);
2041 else
2042 Offset_To_Top :=
2043 Make_Function_Call (Loc,
2044 Name => New_Occurrence_Of (RTE (RE_Offset_To_Top), Loc),
2045 Parameter_Associations => New_List (New_Arg));
2046 end if;
2048 Decl_1 :=
2049 Make_Object_Declaration (Loc,
2050 Defining_Identifier => Make_Temporary (Loc, 'S'),
2051 Constant_Present => True,
2052 Object_Definition =>
2053 New_Occurrence_Of (RTE (RE_Address), Loc),
2054 Expression =>
2055 Make_Function_Call (Loc,
2056 Name =>
2057 Make_Expanded_Name (Loc,
2058 Chars => Name_Op_Add,
2059 Prefix =>
2060 New_Occurrence_Of
2061 (RTU_Entity (System_Storage_Elements), Loc),
2062 Selector_Name =>
2063 Make_Identifier (Loc, Name_Op_Add)),
2064 Parameter_Associations => New_List (
2065 New_Copy_Tree (New_Arg),
2066 Offset_To_Top)));
2068 Decl_2 :=
2069 Make_Object_Declaration (Loc,
2070 Defining_Identifier => Make_Temporary (Loc, 'S'),
2071 Constant_Present => True,
2072 Object_Definition =>
2073 New_Occurrence_Of (RTE (RE_Addr_Ptr), Loc),
2074 Expression =>
2075 Unchecked_Convert_To
2076 (RTE (RE_Addr_Ptr),
2077 New_Occurrence_Of (Defining_Identifier (Decl_1), Loc)));
2079 Append_To (Decl, Decl_1);
2080 Append_To (Decl, Decl_2);
2082 -- Reference the new actual, generate:
2083 -- Target_Formal (S2.all)
2085 Append_To (Actuals,
2086 Unchecked_Convert_To (Ftyp,
2087 Make_Explicit_Dereference (Loc,
2088 New_Occurrence_Of (Defining_Identifier (Decl_2), Loc))));
2090 -- Ensure proper matching of access types. Required to avoid
2091 -- reporting spurious errors.
2093 elsif Is_Access_Type (Etype (Target_Formal)) then
2094 Append_To (Actuals,
2095 Unchecked_Convert_To (Base_Type (Etype (Target_Formal)),
2096 New_Occurrence_Of (Defining_Identifier (Formal), Loc)));
2098 -- No special management required for this actual
2100 else
2101 Append_To (Actuals,
2102 New_Occurrence_Of (Defining_Identifier (Formal), Loc));
2103 end if;
2105 Next_Formal (Target_Formal);
2106 Next (Formal);
2107 end loop;
2109 Thunk_Id := Make_Temporary (Loc, 'T');
2111 -- Note: any change to this symbol name needs to be coordinated
2112 -- with GNATcoverage, as that tool relies on it to identify
2113 -- thunks and exclude them from source coverage analysis.
2115 Mutate_Ekind (Thunk_Id, Ekind (Prim));
2116 Set_Is_Thunk (Thunk_Id);
2117 Set_Has_Controlling_Result (Thunk_Id, False);
2118 Set_Convention (Thunk_Id, Convention (Prim));
2119 Set_Needs_Debug_Info (Thunk_Id, Needs_Debug_Info (Target));
2120 Set_Thunk_Entity (Thunk_Id, Target);
2122 Thunk_Code := New_List;
2124 -- Procedure case
2126 if Ekind (Target) = E_Procedure then
2127 Append_To (Thunk_Code,
2128 Make_Subprogram_Body (Loc,
2129 Specification =>
2130 Make_Procedure_Specification (Loc,
2131 Defining_Unit_Name => Thunk_Id,
2132 Parameter_Specifications => Formals),
2133 Declarations => Decl,
2134 Handled_Statement_Sequence =>
2135 Make_Handled_Sequence_Of_Statements (Loc,
2136 Statements => New_List (
2137 Make_Procedure_Call_Statement (Loc,
2138 Name => New_Occurrence_Of (Target, Loc),
2139 Parameter_Associations => Actuals)))));
2141 -- Function case
2143 else pragma Assert (Ekind (Target) = E_Function);
2144 declare
2145 Call_Node : Node_Id;
2146 Result_Def : Node_Id;
2147 SS_Thunk_Id : Entity_Id;
2148 SS_Thunk_Code : Node_Id;
2150 begin
2151 Call_Node :=
2152 Make_Function_Call (Loc,
2153 Name => New_Occurrence_Of (Target, Loc),
2154 Parameter_Associations => Actuals);
2156 if not Is_Interface (Etype (Prim)) then
2157 Result_Def := New_Copy (Result_Definition (Parent (Target)));
2159 -- Thunk of function returning a class-wide interface object. No
2160 -- extra displacement needed since the displacement is generated
2161 -- in the return statement of Prim. Example:
2163 -- type Iface is interface ...
2164 -- function F (O : Iface) return Iface'Class;
2166 -- type T is new ... and Iface with ...
2167 -- function F (O : T) return Iface'Class;
2169 elsif Is_Class_Wide_Type (Etype (Prim)) then
2170 Result_Def := New_Occurrence_Of (Etype (Prim), Loc);
2172 -- Thunk of function returning an interface object. Displacement
2173 -- needed. Example:
2175 -- type Iface is interface ...
2176 -- function F (O : Iface) return Iface;
2178 -- type T is new ... and Iface with ...
2179 -- function F (O : T) return T;
2181 else
2182 Expand_Secondary_Stack_Thunk
2183 (Target, SS_Thunk_Id, SS_Thunk_Code);
2185 if Present (SS_Thunk_Id) then
2186 Set_Thunk_Entity (Thunk_Id, SS_Thunk_Id);
2187 Call_Node :=
2188 Make_Function_Call (Loc,
2189 Name =>
2190 New_Occurrence_Of (SS_Thunk_Id, Loc),
2191 Parameter_Associations => Actuals);
2192 Append_To (Thunk_Code, SS_Thunk_Code);
2193 end if;
2195 Result_Def :=
2196 New_Occurrence_Of (Class_Wide_Type (Etype (Prim)), Loc);
2198 -- Adding implicit conversion to force the displacement of
2199 -- the pointer to the object to reference the corresponding
2200 -- secondary dispatch table.
2202 Call_Node :=
2203 Make_Type_Conversion (Loc,
2204 Subtype_Mark =>
2205 New_Occurrence_Of (Class_Wide_Type (Etype (Prim)), Loc),
2206 Expression => Relocate_Node (Call_Node));
2207 end if;
2209 Append_To (Thunk_Code,
2210 Make_Subprogram_Body (Loc,
2211 Specification =>
2212 Make_Function_Specification (Loc,
2213 Defining_Unit_Name => Thunk_Id,
2214 Parameter_Specifications => Formals,
2215 Result_Definition => Result_Def),
2216 Declarations => Decl,
2217 Handled_Statement_Sequence =>
2218 Make_Handled_Sequence_Of_Statements (Loc,
2219 Statements => New_List (
2220 Make_Simple_Return_Statement (Loc, Call_Node)))));
2221 end;
2222 end if;
2223 end Expand_Interface_Thunk;
2225 ------------------------------------
2226 -- Expand_Secondary_Stack_Thunk --
2227 ------------------------------------
2229 procedure Expand_Secondary_Stack_Thunk
2230 (Prim : Entity_Id;
2231 Thunk_Id : out Entity_Id;
2232 Thunk_Code : out Node_Id)
2234 Actuals : constant List_Id := New_List;
2235 Formals : constant List_Id := New_List;
2236 Loc : constant Source_Ptr := Sloc (Prim);
2237 Typ : constant Entity_Id := Etype (Prim);
2239 Call_Node : Node_Id;
2240 Expr : Node_Id;
2241 Formal : Entity_Id;
2242 Prim_Formal : Entity_Id;
2243 Result_Def : Node_Id;
2245 begin
2246 Thunk_Id := Empty;
2247 Thunk_Code := Empty;
2249 -- No thunk needed if the primitive has been eliminated
2251 if Is_Eliminated (Prim) then
2252 return;
2254 -- No thunk needed for procedures or functions not dispatching on result
2256 elsif Ekind (Prim) = E_Procedure
2257 or else not Has_Controlling_Result (Prim)
2258 then
2259 return;
2261 -- No thunk needed if the result type is an access type
2263 elsif Is_Access_Type (Typ) then
2264 return;
2266 -- No thunk needed if the tagged type is returned in place
2268 elsif Is_Build_In_Place_Result_Type (Typ) then
2269 return;
2271 -- No thunk needed if the tagged type is returned on the secondary stack
2273 elsif Needs_Secondary_Stack (Typ) then
2274 return;
2275 end if;
2277 pragma Assert (Is_Tagged_Type (Typ));
2279 -- Duplicate the formals of the target primitive and build the actuals
2281 Prim_Formal := First_Formal (Prim);
2282 while Present (Prim_Formal) loop
2283 Expr := New_Copy_Tree (Expression (Parent (Prim_Formal)));
2285 Formal :=
2286 Make_Defining_Identifier (Sloc (Prim_Formal),
2287 Chars => Chars (Prim_Formal));
2289 Append_To (Formals,
2290 Make_Parameter_Specification (Loc,
2291 Defining_Identifier => Formal,
2292 Aliased_Present => Aliased_Present (Parent (Prim_Formal)),
2293 In_Present => In_Present (Parent (Prim_Formal)),
2294 Out_Present => Out_Present (Parent (Prim_Formal)),
2295 Parameter_Type => New_Occurrence_Of (Etype (Prim_Formal), Loc),
2296 Expression => Expr));
2298 -- Ensure proper matching of access types. Required to avoid
2299 -- reporting spurious errors.
2301 if Is_Access_Type (Etype (Prim_Formal)) then
2302 Append_To (Actuals,
2303 Unchecked_Convert_To (Base_Type (Etype (Prim_Formal)),
2304 New_Occurrence_Of (Formal, Loc)));
2306 -- No special management required for this actual
2308 else
2309 Append_To (Actuals, New_Occurrence_Of (Formal, Loc));
2310 end if;
2312 Next_Formal (Prim_Formal);
2313 end loop;
2315 Thunk_Id := Make_Temporary (Loc, 'T');
2317 -- Note: any change to this symbol name needs to be coordinated
2318 -- with GNATcoverage, as that tool relies on it to identify
2319 -- thunks and exclude them from source coverage analysis.
2321 Mutate_Ekind (Thunk_Id, E_Function);
2322 Set_Is_Thunk (Thunk_Id);
2323 Set_Has_Controlling_Result (Thunk_Id, True);
2324 Set_Convention (Thunk_Id, Convention (Prim));
2325 Set_Needs_Debug_Info (Thunk_Id, Needs_Debug_Info (Prim));
2326 Set_Thunk_Entity (Thunk_Id, Prim);
2328 Result_Def := New_Copy (Result_Definition (Parent (Prim)));
2330 Call_Node :=
2331 Make_Function_Call (Loc,
2332 Name => New_Occurrence_Of (Prim, Loc),
2333 Parameter_Associations => Actuals);
2335 Thunk_Code :=
2336 Make_Subprogram_Body (Loc,
2337 Specification =>
2338 Make_Function_Specification (Loc,
2339 Defining_Unit_Name => Thunk_Id,
2340 Parameter_Specifications => Formals,
2341 Result_Definition => Result_Def),
2342 Declarations => Empty_List,
2343 Handled_Statement_Sequence =>
2344 Make_Handled_Sequence_Of_Statements (Loc,
2345 Statements => New_List (
2346 Make_Simple_Return_Statement (Loc, Call_Node))));
2347 end Expand_Secondary_Stack_Thunk;
2349 --------------------------
2350 -- Has_CPP_Constructors --
2351 --------------------------
2353 function Has_CPP_Constructors (Typ : Entity_Id) return Boolean is
2354 E : Entity_Id;
2356 begin
2357 -- Look for the constructor entities
2359 E := Next_Entity (Typ);
2360 while Present (E) loop
2361 if Ekind (E) = E_Function and then Is_Constructor (E) then
2362 return True;
2363 end if;
2365 Next_Entity (E);
2366 end loop;
2368 return False;
2369 end Has_CPP_Constructors;
2371 ------------
2372 -- Has_DT --
2373 ------------
2375 function Has_DT (Typ : Entity_Id) return Boolean is
2376 begin
2377 return not Is_Interface (Typ)
2378 and then not Restriction_Active (No_Dispatching_Calls);
2379 end Has_DT;
2381 ----------------------------------
2382 -- Is_Expanded_Dispatching_Call --
2383 ----------------------------------
2385 function Is_Expanded_Dispatching_Call (N : Node_Id) return Boolean is
2386 begin
2387 return Nkind (N) in N_Subprogram_Call
2388 and then Nkind (Name (N)) = N_Explicit_Dereference
2389 and then Is_Dispatch_Table_Entity (Etype (Name (N)));
2390 end Is_Expanded_Dispatching_Call;
2392 -------------------------------------
2393 -- Is_Predefined_Dispatching_Alias --
2394 -------------------------------------
2396 function Is_Predefined_Dispatching_Alias (Prim : Entity_Id) return Boolean
2398 begin
2399 return not Is_Predefined_Dispatching_Operation (Prim)
2400 and then Present (Alias (Prim))
2401 and then Is_Predefined_Dispatching_Operation (Ultimate_Alias (Prim));
2402 end Is_Predefined_Dispatching_Alias;
2404 ----------------------------------------
2405 -- Make_Disp_Asynchronous_Select_Body --
2406 ----------------------------------------
2408 -- For interface types, generate:
2410 -- procedure _Disp_Asynchronous_Select
2411 -- (T : in out <Typ>;
2412 -- S : Integer;
2413 -- P : System.Address;
2414 -- B : out System.Storage_Elements.Dummy_Communication_Block;
2415 -- F : out Boolean)
2416 -- is
2417 -- begin
2418 -- F := False;
2419 -- C := Ada.Tags.POK_Function;
2420 -- end _Disp_Asynchronous_Select;
2422 -- For protected types, generate:
2424 -- procedure _Disp_Asynchronous_Select
2425 -- (T : in out <Typ>;
2426 -- S : Integer;
2427 -- P : System.Address;
2428 -- B : out System.Storage_Elements.Dummy_Communication_Block;
2429 -- F : out Boolean)
2430 -- is
2431 -- I : Integer :=
2432 -- Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP, S));
2433 -- Bnn : System.Tasking.Protected_Objects.Operations.
2434 -- Communication_Block;
2435 -- begin
2436 -- System.Tasking.Protected_Objects.Operations.Protected_Entry_Call
2437 -- (T._object'Access,
2438 -- System.Tasking.Protected_Objects.Protected_Entry_Index (I),
2439 -- P,
2440 -- System.Tasking.Asynchronous_Call,
2441 -- Bnn);
2442 -- B := System.Storage_Elements.Dummy_Communication_Block (Bnn);
2443 -- end _Disp_Asynchronous_Select;
2445 -- For task types, generate:
2447 -- procedure _Disp_Asynchronous_Select
2448 -- (T : in out <Typ>;
2449 -- S : Integer;
2450 -- P : System.Address;
2451 -- B : out System.Storage_Elements.Dummy_Communication_Block;
2452 -- F : out Boolean)
2453 -- is
2454 -- I : Integer :=
2455 -- Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP, S));
2456 -- begin
2457 -- System.Tasking.Rendezvous.Task_Entry_Call
2458 -- (T._task_id,
2459 -- System.Tasking.Task_Entry_Index (I),
2460 -- P,
2461 -- System.Tasking.Asynchronous_Call,
2462 -- F);
2463 -- end _Disp_Asynchronous_Select;
2465 function Make_Disp_Asynchronous_Select_Body
2466 (Typ : Entity_Id) return Node_Id
2468 Com_Block : Entity_Id;
2469 Conc_Typ : Entity_Id := Empty;
2470 Decls : constant List_Id := New_List;
2471 Loc : constant Source_Ptr := Sloc (Typ);
2472 Obj_Ref : Node_Id;
2473 Stmts : constant List_Id := New_List;
2474 Tag_Node : Node_Id;
2476 begin
2477 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2479 -- Null body is generated for interface types
2481 if Is_Interface (Typ) then
2482 return
2483 Make_Subprogram_Body (Loc,
2484 Specification =>
2485 Make_Disp_Asynchronous_Select_Spec (Typ),
2486 Declarations => New_List,
2487 Handled_Statement_Sequence =>
2488 Make_Handled_Sequence_Of_Statements (Loc,
2489 New_List (
2490 Make_Assignment_Statement (Loc,
2491 Name => Make_Identifier (Loc, Name_uF),
2492 Expression => New_Occurrence_Of (Standard_False, Loc)))));
2493 end if;
2495 if Is_Concurrent_Record_Type (Typ) then
2496 Conc_Typ := Corresponding_Concurrent_Type (Typ);
2498 -- Generate:
2499 -- I : Integer :=
2500 -- Ada.Tags.Get_Entry_Index (Ada.Tags.Tag! (<type>VP), S);
2502 -- where I will be used to capture the entry index of the primitive
2503 -- wrapper at position S.
2505 if Tagged_Type_Expansion then
2506 Tag_Node :=
2507 Unchecked_Convert_To (RTE (RE_Tag),
2508 New_Occurrence_Of
2509 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc));
2510 else
2511 Tag_Node :=
2512 Make_Attribute_Reference (Loc,
2513 Prefix => New_Occurrence_Of (Typ, Loc),
2514 Attribute_Name => Name_Tag);
2515 end if;
2517 Append_To (Decls,
2518 Make_Object_Declaration (Loc,
2519 Defining_Identifier =>
2520 Make_Defining_Identifier (Loc, Name_uI),
2521 Object_Definition =>
2522 New_Occurrence_Of (Standard_Integer, Loc),
2523 Expression =>
2524 Make_Function_Call (Loc,
2525 Name =>
2526 New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
2527 Parameter_Associations =>
2528 New_List (Tag_Node, Make_Identifier (Loc, Name_uS)))));
2530 if Ekind (Conc_Typ) = E_Protected_Type then
2532 -- Generate:
2533 -- Bnn : Communication_Block;
2535 Com_Block := Make_Temporary (Loc, 'B');
2536 Append_To (Decls,
2537 Make_Object_Declaration (Loc,
2538 Defining_Identifier => Com_Block,
2539 Object_Definition =>
2540 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
2542 -- Build T._object'Access for calls below
2544 Obj_Ref :=
2545 Make_Attribute_Reference (Loc,
2546 Attribute_Name => Name_Unchecked_Access,
2547 Prefix =>
2548 Make_Selected_Component (Loc,
2549 Prefix => Make_Identifier (Loc, Name_uT),
2550 Selector_Name => Make_Identifier (Loc, Name_uObject)));
2552 case Corresponding_Runtime_Package (Conc_Typ) is
2553 when System_Tasking_Protected_Objects_Entries =>
2555 -- Generate:
2556 -- Protected_Entry_Call
2557 -- (T._object'Access, -- Object
2558 -- Protected_Entry_Index! (I), -- E
2559 -- P, -- Uninterpreted_Data
2560 -- Asynchronous_Call, -- Mode
2561 -- Bnn); -- Communication_Block
2563 -- where T is the protected object, I is the entry index, P
2564 -- is the wrapped parameters and B is the name of the
2565 -- communication block.
2567 Append_To (Stmts,
2568 Make_Procedure_Call_Statement (Loc,
2569 Name =>
2570 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
2571 Parameter_Associations =>
2572 New_List (
2573 Obj_Ref,
2575 Unchecked_Convert_To ( -- entry index
2576 RTE (RE_Protected_Entry_Index),
2577 Make_Identifier (Loc, Name_uI)),
2579 Make_Identifier (Loc, Name_uP), -- parameter block
2580 New_Occurrence_Of -- Asynchronous_Call
2581 (RTE (RE_Asynchronous_Call), Loc),
2582 New_Occurrence_Of -- comm block
2583 (Com_Block, Loc))));
2585 when others =>
2586 raise Program_Error;
2587 end case;
2589 -- Generate:
2590 -- B := Dummy_Communication_Block (Bnn);
2592 Append_To (Stmts,
2593 Make_Assignment_Statement (Loc,
2594 Name => Make_Identifier (Loc, Name_uB),
2595 Expression =>
2596 Unchecked_Convert_To
2597 (RTE (RE_Dummy_Communication_Block),
2598 New_Occurrence_Of (Com_Block, Loc))));
2600 -- Generate:
2601 -- F := False;
2603 Append_To (Stmts,
2604 Make_Assignment_Statement (Loc,
2605 Name => Make_Identifier (Loc, Name_uF),
2606 Expression => New_Occurrence_Of (Standard_False, Loc)));
2608 else
2609 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
2611 -- Generate:
2612 -- Task_Entry_Call
2613 -- (T._task_id, -- Acceptor
2614 -- Task_Entry_Index! (I), -- E
2615 -- P, -- Uninterpreted_Data
2616 -- Asynchronous_Call, -- Mode
2617 -- F); -- Rendezvous_Successful
2619 -- where T is the task object, I is the entry index, P is the
2620 -- wrapped parameters and F is the status flag.
2622 Append_To (Stmts,
2623 Make_Procedure_Call_Statement (Loc,
2624 Name =>
2625 New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
2626 Parameter_Associations =>
2627 New_List (
2628 Make_Selected_Component (Loc, -- T._task_id
2629 Prefix => Make_Identifier (Loc, Name_uT),
2630 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2632 Unchecked_Convert_To ( -- entry index
2633 RTE (RE_Task_Entry_Index),
2634 Make_Identifier (Loc, Name_uI)),
2636 Make_Identifier (Loc, Name_uP), -- parameter block
2637 New_Occurrence_Of -- Asynchronous_Call
2638 (RTE (RE_Asynchronous_Call), Loc),
2639 Make_Identifier (Loc, Name_uF)))); -- status flag
2640 end if;
2642 else
2643 -- Ensure that the statements list is non-empty
2645 Append_To (Stmts,
2646 Make_Assignment_Statement (Loc,
2647 Name => Make_Identifier (Loc, Name_uF),
2648 Expression => New_Occurrence_Of (Standard_False, Loc)));
2649 end if;
2651 return
2652 Make_Subprogram_Body (Loc,
2653 Specification =>
2654 Make_Disp_Asynchronous_Select_Spec (Typ),
2655 Declarations => Decls,
2656 Handled_Statement_Sequence =>
2657 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
2658 end Make_Disp_Asynchronous_Select_Body;
2660 ----------------------------------------
2661 -- Make_Disp_Asynchronous_Select_Spec --
2662 ----------------------------------------
2664 function Make_Disp_Asynchronous_Select_Spec
2665 (Typ : Entity_Id) return Node_Id
2667 Loc : constant Source_Ptr := Sloc (Typ);
2668 B_Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
2669 Def_Id : constant Entity_Id :=
2670 Make_Defining_Identifier (Loc,
2671 Name_uDisp_Asynchronous_Select);
2672 Params : List_Id;
2674 begin
2675 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2677 -- T : in out Typ; -- Object parameter
2678 -- S : Integer; -- Primitive operation slot
2679 -- P : Address; -- Wrapped parameters
2680 -- B : out Dummy_Communication_Block; -- Communication block dummy
2681 -- F : out Boolean; -- Status flag
2683 -- The B parameter may be left uninitialized
2685 Set_Warnings_Off (B_Id);
2687 Params := New_List (
2689 Make_Parameter_Specification (Loc,
2690 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
2691 Parameter_Type => New_Occurrence_Of (Typ, Loc),
2692 In_Present => True,
2693 Out_Present => True),
2695 Make_Parameter_Specification (Loc,
2696 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uS),
2697 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
2699 Make_Parameter_Specification (Loc,
2700 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
2701 Parameter_Type => New_Occurrence_Of (RTE (RE_Address), Loc)),
2703 Make_Parameter_Specification (Loc,
2704 Defining_Identifier => B_Id,
2705 Parameter_Type =>
2706 New_Occurrence_Of (RTE (RE_Dummy_Communication_Block), Loc),
2707 Out_Present => True),
2709 Make_Parameter_Specification (Loc,
2710 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uF),
2711 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
2712 Out_Present => True));
2714 return
2715 Make_Procedure_Specification (Loc,
2716 Defining_Unit_Name => Def_Id,
2717 Parameter_Specifications => Params);
2718 end Make_Disp_Asynchronous_Select_Spec;
2720 ---------------------------------------
2721 -- Make_Disp_Conditional_Select_Body --
2722 ---------------------------------------
2724 -- For interface types, generate:
2726 -- procedure _Disp_Conditional_Select
2727 -- (T : in out <Typ>;
2728 -- S : Integer;
2729 -- P : System.Address;
2730 -- C : out Ada.Tags.Prim_Op_Kind;
2731 -- F : out Boolean)
2732 -- is
2733 -- begin
2734 -- F := False;
2735 -- C := Ada.Tags.POK_Function;
2736 -- end _Disp_Conditional_Select;
2738 -- For protected types, generate:
2740 -- procedure _Disp_Conditional_Select
2741 -- (T : in out <Typ>;
2742 -- S : Integer;
2743 -- P : System.Address;
2744 -- C : out Ada.Tags.Prim_Op_Kind;
2745 -- F : out Boolean)
2746 -- is
2747 -- I : Integer;
2748 -- Bnn : System.Tasking.Protected_Objects.Operations.
2749 -- Communication_Block;
2751 -- begin
2752 -- C := Ada.Tags.Get_Prim_Op_Kind (Ada.Tags.Tag (<Typ>VP, S));
2754 -- if C = Ada.Tags.POK_Procedure
2755 -- or else C = Ada.Tags.POK_Protected_Procedure
2756 -- or else C = Ada.Tags.POK_Task_Procedure
2757 -- then
2758 -- F := True;
2759 -- return;
2760 -- end if;
2762 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP, S));
2763 -- System.Tasking.Protected_Objects.Operations.Protected_Entry_Call
2764 -- (T.object'Access,
2765 -- System.Tasking.Protected_Objects.Protected_Entry_Index (I),
2766 -- P,
2767 -- System.Tasking.Conditional_Call,
2768 -- Bnn);
2769 -- F := not Cancelled (Bnn);
2770 -- end _Disp_Conditional_Select;
2772 -- For task types, generate:
2774 -- procedure _Disp_Conditional_Select
2775 -- (T : in out <Typ>;
2776 -- S : Integer;
2777 -- P : System.Address;
2778 -- C : out Ada.Tags.Prim_Op_Kind;
2779 -- F : out Boolean)
2780 -- is
2781 -- I : Integer;
2783 -- begin
2784 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP, S));
2785 -- System.Tasking.Rendezvous.Task_Entry_Call
2786 -- (T._task_id,
2787 -- System.Tasking.Task_Entry_Index (I),
2788 -- P,
2789 -- System.Tasking.Conditional_Call,
2790 -- F);
2791 -- end _Disp_Conditional_Select;
2793 function Make_Disp_Conditional_Select_Body
2794 (Typ : Entity_Id) return Node_Id
2796 Loc : constant Source_Ptr := Sloc (Typ);
2797 Blk_Nam : Entity_Id;
2798 Conc_Typ : Entity_Id := Empty;
2799 Decls : constant List_Id := New_List;
2800 Obj_Ref : Node_Id;
2801 Stmts : constant List_Id := New_List;
2802 Tag_Node : Node_Id;
2804 begin
2805 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
2807 -- Null body is generated for interface types
2809 if Is_Interface (Typ) then
2810 return
2811 Make_Subprogram_Body (Loc,
2812 Specification =>
2813 Make_Disp_Conditional_Select_Spec (Typ),
2814 Declarations => No_List,
2815 Handled_Statement_Sequence =>
2816 Make_Handled_Sequence_Of_Statements (Loc,
2817 New_List (Make_Assignment_Statement (Loc,
2818 Name => Make_Identifier (Loc, Name_uF),
2819 Expression => New_Occurrence_Of (Standard_False, Loc)))));
2820 end if;
2822 if Is_Concurrent_Record_Type (Typ) then
2823 Conc_Typ := Corresponding_Concurrent_Type (Typ);
2825 -- Generate:
2826 -- I : Integer;
2828 -- where I will be used to capture the entry index of the primitive
2829 -- wrapper at position S.
2831 Append_To (Decls,
2832 Make_Object_Declaration (Loc,
2833 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uI),
2834 Object_Definition =>
2835 New_Occurrence_Of (Standard_Integer, Loc)));
2837 -- Generate:
2838 -- C := Ada.Tags.Get_Prim_Op_Kind (Ada.Tags.Tag! (<type>VP), S);
2840 -- if C = POK_Procedure
2841 -- or else C = POK_Protected_Procedure
2842 -- or else C = POK_Task_Procedure;
2843 -- then
2844 -- F := True;
2845 -- return;
2846 -- end if;
2848 Build_Common_Dispatching_Select_Statements (Typ, Stmts);
2850 -- Generate:
2851 -- Bnn : Communication_Block;
2853 -- where Bnn is the name of the communication block used in the
2854 -- call to Protected_Entry_Call.
2856 Blk_Nam := Make_Temporary (Loc, 'B');
2857 Append_To (Decls,
2858 Make_Object_Declaration (Loc,
2859 Defining_Identifier => Blk_Nam,
2860 Object_Definition =>
2861 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
2863 -- Generate:
2864 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag! (<type>VP), S);
2866 -- I is the entry index and S is the dispatch table slot
2868 if Tagged_Type_Expansion then
2869 Tag_Node :=
2870 Unchecked_Convert_To (RTE (RE_Tag),
2871 New_Occurrence_Of
2872 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc));
2874 else
2875 Tag_Node :=
2876 Make_Attribute_Reference (Loc,
2877 Prefix => New_Occurrence_Of (Typ, Loc),
2878 Attribute_Name => Name_Tag);
2879 end if;
2881 Append_To (Stmts,
2882 Make_Assignment_Statement (Loc,
2883 Name => Make_Identifier (Loc, Name_uI),
2884 Expression =>
2885 Make_Function_Call (Loc,
2886 Name =>
2887 New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
2888 Parameter_Associations => New_List (
2889 Tag_Node,
2890 Make_Identifier (Loc, Name_uS)))));
2892 if Ekind (Conc_Typ) = E_Protected_Type then
2894 Obj_Ref := -- T._object'Access
2895 Make_Attribute_Reference (Loc,
2896 Attribute_Name => Name_Unchecked_Access,
2897 Prefix =>
2898 Make_Selected_Component (Loc,
2899 Prefix => Make_Identifier (Loc, Name_uT),
2900 Selector_Name => Make_Identifier (Loc, Name_uObject)));
2902 case Corresponding_Runtime_Package (Conc_Typ) is
2903 when System_Tasking_Protected_Objects_Entries =>
2904 -- Generate:
2906 -- Protected_Entry_Call
2907 -- (T._object'Access, -- Object
2908 -- Protected_Entry_Index! (I), -- E
2909 -- P, -- Uninterpreted_Data
2910 -- Conditional_Call, -- Mode
2911 -- Bnn); -- Block
2913 -- where T is the protected object, I is the entry index, P
2914 -- are the wrapped parameters and Bnn is the name of the
2915 -- communication block.
2917 Append_To (Stmts,
2918 Make_Procedure_Call_Statement (Loc,
2919 Name =>
2920 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
2921 Parameter_Associations => New_List (
2922 Obj_Ref,
2924 Unchecked_Convert_To ( -- entry index
2925 RTE (RE_Protected_Entry_Index),
2926 Make_Identifier (Loc, Name_uI)),
2928 Make_Identifier (Loc, Name_uP), -- parameter block
2930 New_Occurrence_Of -- Conditional_Call
2931 (RTE (RE_Conditional_Call), Loc),
2932 New_Occurrence_Of -- Bnn
2933 (Blk_Nam, Loc))));
2935 when System_Tasking_Protected_Objects_Single_Entry =>
2937 -- If we are compiling for a restricted run-time, the call
2938 -- uses the simpler form.
2940 Append_To (Stmts,
2941 Make_Procedure_Call_Statement (Loc,
2942 Name =>
2943 New_Occurrence_Of
2944 (RTE (RE_Protected_Single_Entry_Call), Loc),
2945 Parameter_Associations => New_List (
2946 Obj_Ref,
2948 Make_Attribute_Reference (Loc,
2949 Prefix => Make_Identifier (Loc, Name_uP),
2950 Attribute_Name => Name_Address),
2952 New_Occurrence_Of
2953 (RTE (RE_Conditional_Call), Loc))));
2954 when others =>
2955 raise Program_Error;
2956 end case;
2958 -- Generate:
2959 -- F := not Cancelled (Bnn);
2961 -- where F is the success flag. The status of Cancelled is negated
2962 -- in order to match the behavior of the version for task types.
2964 Append_To (Stmts,
2965 Make_Assignment_Statement (Loc,
2966 Name => Make_Identifier (Loc, Name_uF),
2967 Expression =>
2968 Make_Op_Not (Loc,
2969 Right_Opnd =>
2970 Make_Function_Call (Loc,
2971 Name =>
2972 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
2973 Parameter_Associations => New_List (
2974 New_Occurrence_Of (Blk_Nam, Loc))))));
2975 else
2976 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
2978 -- Generate:
2979 -- Task_Entry_Call
2980 -- (T._task_id, -- Acceptor
2981 -- Task_Entry_Index! (I), -- E
2982 -- P, -- Uninterpreted_Data
2983 -- Conditional_Call, -- Mode
2984 -- F); -- Rendezvous_Successful
2986 -- where T is the task object, I is the entry index, P are the
2987 -- wrapped parameters and F is the status flag.
2989 Append_To (Stmts,
2990 Make_Procedure_Call_Statement (Loc,
2991 Name =>
2992 New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
2993 Parameter_Associations => New_List (
2995 Make_Selected_Component (Loc, -- T._task_id
2996 Prefix => Make_Identifier (Loc, Name_uT),
2997 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2999 Unchecked_Convert_To ( -- entry index
3000 RTE (RE_Task_Entry_Index),
3001 Make_Identifier (Loc, Name_uI)),
3003 Make_Identifier (Loc, Name_uP), -- parameter block
3004 New_Occurrence_Of -- Conditional_Call
3005 (RTE (RE_Conditional_Call), Loc),
3006 Make_Identifier (Loc, Name_uF)))); -- status flag
3007 end if;
3009 else
3010 -- Initialize out parameters
3012 Append_To (Stmts,
3013 Make_Assignment_Statement (Loc,
3014 Name => Make_Identifier (Loc, Name_uF),
3015 Expression => New_Occurrence_Of (Standard_False, Loc)));
3016 Append_To (Stmts,
3017 Make_Assignment_Statement (Loc,
3018 Name => Make_Identifier (Loc, Name_uC),
3019 Expression => New_Occurrence_Of (RTE (RE_POK_Function), Loc)));
3020 end if;
3022 return
3023 Make_Subprogram_Body (Loc,
3024 Specification =>
3025 Make_Disp_Conditional_Select_Spec (Typ),
3026 Declarations => Decls,
3027 Handled_Statement_Sequence =>
3028 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
3029 end Make_Disp_Conditional_Select_Body;
3031 ---------------------------------------
3032 -- Make_Disp_Conditional_Select_Spec --
3033 ---------------------------------------
3035 function Make_Disp_Conditional_Select_Spec
3036 (Typ : Entity_Id) return Node_Id
3038 Loc : constant Source_Ptr := Sloc (Typ);
3039 Def_Id : constant Node_Id :=
3040 Make_Defining_Identifier (Loc,
3041 Name_uDisp_Conditional_Select);
3042 Params : constant List_Id := New_List;
3044 begin
3045 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3047 -- T : in out Typ; -- Object parameter
3048 -- S : Integer; -- Primitive operation slot
3049 -- P : Address; -- Wrapped parameters
3050 -- C : out Prim_Op_Kind; -- Call kind
3051 -- F : out Boolean; -- Status flag
3053 Append_List_To (Params, New_List (
3055 Make_Parameter_Specification (Loc,
3056 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
3057 Parameter_Type => New_Occurrence_Of (Typ, Loc),
3058 In_Present => True,
3059 Out_Present => True),
3061 Make_Parameter_Specification (Loc,
3062 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uS),
3063 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
3065 Make_Parameter_Specification (Loc,
3066 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
3067 Parameter_Type => New_Occurrence_Of (RTE (RE_Address), Loc)),
3069 Make_Parameter_Specification (Loc,
3070 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uC),
3071 Parameter_Type =>
3072 New_Occurrence_Of (RTE (RE_Prim_Op_Kind), Loc),
3073 Out_Present => True),
3075 Make_Parameter_Specification (Loc,
3076 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uF),
3077 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
3078 Out_Present => True)));
3080 return
3081 Make_Procedure_Specification (Loc,
3082 Defining_Unit_Name => Def_Id,
3083 Parameter_Specifications => Params);
3084 end Make_Disp_Conditional_Select_Spec;
3086 -------------------------------------
3087 -- Make_Disp_Get_Prim_Op_Kind_Body --
3088 -------------------------------------
3090 function Make_Disp_Get_Prim_Op_Kind_Body (Typ : Entity_Id) return Node_Id is
3091 Loc : constant Source_Ptr := Sloc (Typ);
3092 Tag_Node : Node_Id;
3094 begin
3095 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3097 if Is_Interface (Typ) then
3098 return
3099 Make_Subprogram_Body (Loc,
3100 Specification =>
3101 Make_Disp_Get_Prim_Op_Kind_Spec (Typ),
3102 Declarations => New_List,
3103 Handled_Statement_Sequence =>
3104 Make_Handled_Sequence_Of_Statements (Loc,
3105 New_List (Make_Null_Statement (Loc))));
3106 end if;
3108 -- Generate:
3109 -- C := get_prim_op_kind (tag! (<type>VP), S);
3111 -- where C is the out parameter capturing the call kind and S is the
3112 -- dispatch table slot number.
3114 if Tagged_Type_Expansion then
3115 Tag_Node :=
3116 Unchecked_Convert_To (RTE (RE_Tag),
3117 New_Occurrence_Of
3118 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc));
3120 else
3121 Tag_Node :=
3122 Make_Attribute_Reference (Loc,
3123 Prefix => New_Occurrence_Of (Typ, Loc),
3124 Attribute_Name => Name_Tag);
3125 end if;
3127 return
3128 Make_Subprogram_Body (Loc,
3129 Specification =>
3130 Make_Disp_Get_Prim_Op_Kind_Spec (Typ),
3131 Declarations => New_List,
3132 Handled_Statement_Sequence =>
3133 Make_Handled_Sequence_Of_Statements (Loc,
3134 New_List (
3135 Make_Assignment_Statement (Loc,
3136 Name => Make_Identifier (Loc, Name_uC),
3137 Expression =>
3138 Make_Function_Call (Loc,
3139 Name =>
3140 New_Occurrence_Of (RTE (RE_Get_Prim_Op_Kind), Loc),
3141 Parameter_Associations => New_List (
3142 Tag_Node,
3143 Make_Identifier (Loc, Name_uS)))))));
3144 end Make_Disp_Get_Prim_Op_Kind_Body;
3146 -------------------------------------
3147 -- Make_Disp_Get_Prim_Op_Kind_Spec --
3148 -------------------------------------
3150 function Make_Disp_Get_Prim_Op_Kind_Spec
3151 (Typ : Entity_Id) return Node_Id
3153 Loc : constant Source_Ptr := Sloc (Typ);
3154 Def_Id : constant Node_Id :=
3155 Make_Defining_Identifier (Loc, Name_uDisp_Get_Prim_Op_Kind);
3156 Params : constant List_Id := New_List;
3158 begin
3159 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3161 -- T : in out Typ; -- Object parameter
3162 -- S : Integer; -- Primitive operation slot
3163 -- C : out Prim_Op_Kind; -- Call kind
3165 Append_List_To (Params, New_List (
3167 Make_Parameter_Specification (Loc,
3168 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
3169 Parameter_Type => New_Occurrence_Of (Typ, Loc),
3170 In_Present => True,
3171 Out_Present => True),
3173 Make_Parameter_Specification (Loc,
3174 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uS),
3175 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
3177 Make_Parameter_Specification (Loc,
3178 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uC),
3179 Parameter_Type =>
3180 New_Occurrence_Of (RTE (RE_Prim_Op_Kind), Loc),
3181 Out_Present => True)));
3183 return
3184 Make_Procedure_Specification (Loc,
3185 Defining_Unit_Name => Def_Id,
3186 Parameter_Specifications => Params);
3187 end Make_Disp_Get_Prim_Op_Kind_Spec;
3189 --------------------------------
3190 -- Make_Disp_Get_Task_Id_Body --
3191 --------------------------------
3193 function Make_Disp_Get_Task_Id_Body
3194 (Typ : Entity_Id) return Node_Id
3196 Loc : constant Source_Ptr := Sloc (Typ);
3197 Ret : Node_Id;
3199 begin
3200 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3202 if Is_Concurrent_Record_Type (Typ)
3203 and then Ekind (Corresponding_Concurrent_Type (Typ)) = E_Task_Type
3204 then
3205 -- Generate:
3206 -- return To_Address (_T._task_id);
3208 Ret :=
3209 Make_Simple_Return_Statement (Loc,
3210 Expression =>
3211 Unchecked_Convert_To
3212 (RTE (RE_Address),
3213 Make_Selected_Component (Loc,
3214 Prefix => Make_Identifier (Loc, Name_uT),
3215 Selector_Name => Make_Identifier (Loc, Name_uTask_Id))));
3217 -- A null body is constructed for non-task types
3219 else
3220 -- Generate:
3221 -- return Null_Address;
3223 Ret :=
3224 Make_Simple_Return_Statement (Loc,
3225 Expression => New_Occurrence_Of (RTE (RE_Null_Address), Loc));
3226 end if;
3228 return
3229 Make_Subprogram_Body (Loc,
3230 Specification => Make_Disp_Get_Task_Id_Spec (Typ),
3231 Declarations => New_List,
3232 Handled_Statement_Sequence =>
3233 Make_Handled_Sequence_Of_Statements (Loc, New_List (Ret)));
3234 end Make_Disp_Get_Task_Id_Body;
3236 --------------------------------
3237 -- Make_Disp_Get_Task_Id_Spec --
3238 --------------------------------
3240 function Make_Disp_Get_Task_Id_Spec
3241 (Typ : Entity_Id) return Node_Id
3243 Loc : constant Source_Ptr := Sloc (Typ);
3245 begin
3246 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3248 return
3249 Make_Function_Specification (Loc,
3250 Defining_Unit_Name =>
3251 Make_Defining_Identifier (Loc, Name_uDisp_Get_Task_Id),
3252 Parameter_Specifications => New_List (
3253 Make_Parameter_Specification (Loc,
3254 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
3255 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
3256 Result_Definition =>
3257 New_Occurrence_Of (RTE (RE_Address), Loc));
3258 end Make_Disp_Get_Task_Id_Spec;
3260 ----------------------------
3261 -- Make_Disp_Requeue_Body --
3262 ----------------------------
3264 function Make_Disp_Requeue_Body
3265 (Typ : Entity_Id) return Node_Id
3267 Loc : constant Source_Ptr := Sloc (Typ);
3268 Conc_Typ : Entity_Id := Empty;
3269 Stmts : constant List_Id := New_List;
3271 begin
3272 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3274 -- Null body is generated for interface types and nonconcurrent
3275 -- tagged types.
3277 if Is_Interface (Typ)
3278 or else not Is_Concurrent_Record_Type (Typ)
3279 then
3280 return
3281 Make_Subprogram_Body (Loc,
3282 Specification => Make_Disp_Requeue_Spec (Typ),
3283 Declarations => No_List,
3284 Handled_Statement_Sequence =>
3285 Make_Handled_Sequence_Of_Statements (Loc,
3286 New_List (Make_Null_Statement (Loc))));
3287 end if;
3289 Conc_Typ := Corresponding_Concurrent_Type (Typ);
3291 if Ekind (Conc_Typ) = E_Protected_Type then
3293 -- Generate statements:
3294 -- if F then
3295 -- System.Tasking.Protected_Objects.Operations.
3296 -- Requeue_Protected_Entry
3297 -- (Protection_Entries_Access (P),
3298 -- O._object'Unchecked_Access,
3299 -- Protected_Entry_Index (I),
3300 -- A);
3301 -- else
3302 -- System.Tasking.Protected_Objects.Operations.
3303 -- Requeue_Task_To_Protected_Entry
3304 -- (O._object'Unchecked_Access,
3305 -- Protected_Entry_Index (I),
3306 -- A);
3307 -- end if;
3309 if Restriction_Active (No_Entry_Queue) then
3310 Append_To (Stmts, Make_Null_Statement (Loc));
3311 else
3312 Append_To (Stmts,
3313 Make_If_Statement (Loc,
3314 Condition => Make_Identifier (Loc, Name_uF),
3316 Then_Statements =>
3317 New_List (
3319 -- Call to Requeue_Protected_Entry
3321 Make_Procedure_Call_Statement (Loc,
3322 Name =>
3323 New_Occurrence_Of
3324 (RTE (RE_Requeue_Protected_Entry), Loc),
3325 Parameter_Associations =>
3326 New_List (
3328 Unchecked_Convert_To ( -- PEA (P)
3329 RTE (RE_Protection_Entries_Access),
3330 Make_Identifier (Loc, Name_uP)),
3332 Make_Attribute_Reference (Loc, -- O._object'Acc
3333 Attribute_Name =>
3334 Name_Unchecked_Access,
3335 Prefix =>
3336 Make_Selected_Component (Loc,
3337 Prefix =>
3338 Make_Identifier (Loc, Name_uO),
3339 Selector_Name =>
3340 Make_Identifier (Loc, Name_uObject))),
3342 Unchecked_Convert_To ( -- entry index
3343 RTE (RE_Protected_Entry_Index),
3344 Make_Identifier (Loc, Name_uI)),
3346 Make_Identifier (Loc, Name_uA)))), -- abort status
3348 Else_Statements =>
3349 New_List (
3351 -- Call to Requeue_Task_To_Protected_Entry
3353 Make_Procedure_Call_Statement (Loc,
3354 Name =>
3355 New_Occurrence_Of
3356 (RTE (RE_Requeue_Task_To_Protected_Entry), Loc),
3357 Parameter_Associations =>
3358 New_List (
3360 Make_Attribute_Reference (Loc, -- O._object'Acc
3361 Attribute_Name => Name_Unchecked_Access,
3362 Prefix =>
3363 Make_Selected_Component (Loc,
3364 Prefix =>
3365 Make_Identifier (Loc, Name_uO),
3366 Selector_Name =>
3367 Make_Identifier (Loc, Name_uObject))),
3369 Unchecked_Convert_To ( -- entry index
3370 RTE (RE_Protected_Entry_Index),
3371 Make_Identifier (Loc, Name_uI)),
3373 Make_Identifier (Loc, Name_uA)))))); -- abort status
3374 end if;
3376 else
3377 pragma Assert (Is_Task_Type (Conc_Typ));
3379 -- Generate:
3380 -- if F then
3381 -- System.Tasking.Rendezvous.Requeue_Protected_To_Task_Entry
3382 -- (Protection_Entries_Access (P),
3383 -- O._task_id,
3384 -- Task_Entry_Index (I),
3385 -- A);
3386 -- else
3387 -- System.Tasking.Rendezvous.Requeue_Task_Entry
3388 -- (O._task_id,
3389 -- Task_Entry_Index (I),
3390 -- A);
3391 -- end if;
3393 Append_To (Stmts,
3394 Make_If_Statement (Loc,
3395 Condition => Make_Identifier (Loc, Name_uF),
3397 Then_Statements => New_List (
3399 -- Call to Requeue_Protected_To_Task_Entry
3401 Make_Procedure_Call_Statement (Loc,
3402 Name =>
3403 New_Occurrence_Of
3404 (RTE (RE_Requeue_Protected_To_Task_Entry), Loc),
3406 Parameter_Associations => New_List (
3408 Unchecked_Convert_To ( -- PEA (P)
3409 RTE (RE_Protection_Entries_Access),
3410 Make_Identifier (Loc, Name_uP)),
3412 Make_Selected_Component (Loc, -- O._task_id
3413 Prefix => Make_Identifier (Loc, Name_uO),
3414 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3416 Unchecked_Convert_To ( -- entry index
3417 RTE (RE_Task_Entry_Index),
3418 Make_Identifier (Loc, Name_uI)),
3420 Make_Identifier (Loc, Name_uA)))), -- abort status
3422 Else_Statements => New_List (
3424 -- Call to Requeue_Task_Entry
3426 Make_Procedure_Call_Statement (Loc,
3427 Name =>
3428 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc),
3430 Parameter_Associations => New_List (
3432 Make_Selected_Component (Loc, -- O._task_id
3433 Prefix => Make_Identifier (Loc, Name_uO),
3434 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3436 Unchecked_Convert_To ( -- entry index
3437 RTE (RE_Task_Entry_Index),
3438 Make_Identifier (Loc, Name_uI)),
3440 Make_Identifier (Loc, Name_uA)))))); -- abort status
3441 end if;
3443 -- Even though no declarations are needed in both cases, we allocate
3444 -- a list for entities added by Freeze.
3446 return
3447 Make_Subprogram_Body (Loc,
3448 Specification => Make_Disp_Requeue_Spec (Typ),
3449 Declarations => New_List,
3450 Handled_Statement_Sequence =>
3451 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
3452 end Make_Disp_Requeue_Body;
3454 ----------------------------
3455 -- Make_Disp_Requeue_Spec --
3456 ----------------------------
3458 function Make_Disp_Requeue_Spec
3459 (Typ : Entity_Id) return Node_Id
3461 Loc : constant Source_Ptr := Sloc (Typ);
3463 begin
3464 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3466 -- O : in out Typ; - Object parameter
3467 -- F : Boolean; - Protected (True) / task (False) flag
3468 -- P : Address; - Protection_Entries_Access value
3469 -- I : Entry_Index - Index of entry call
3470 -- A : Boolean - Abort flag
3472 -- Note that the Protection_Entries_Access value is represented as a
3473 -- System.Address in order to avoid dragging in the tasking runtime
3474 -- when compiling sources without tasking constructs.
3476 return
3477 Make_Procedure_Specification (Loc,
3478 Defining_Unit_Name =>
3479 Make_Defining_Identifier (Loc, Name_uDisp_Requeue),
3481 Parameter_Specifications => New_List (
3483 Make_Parameter_Specification (Loc, -- O
3484 Defining_Identifier =>
3485 Make_Defining_Identifier (Loc, Name_uO),
3486 Parameter_Type =>
3487 New_Occurrence_Of (Typ, Loc),
3488 In_Present => True,
3489 Out_Present => True),
3491 Make_Parameter_Specification (Loc, -- F
3492 Defining_Identifier =>
3493 Make_Defining_Identifier (Loc, Name_uF),
3494 Parameter_Type =>
3495 New_Occurrence_Of (Standard_Boolean, Loc)),
3497 Make_Parameter_Specification (Loc, -- P
3498 Defining_Identifier =>
3499 Make_Defining_Identifier (Loc, Name_uP),
3500 Parameter_Type =>
3501 New_Occurrence_Of (RTE (RE_Address), Loc)),
3503 Make_Parameter_Specification (Loc, -- I
3504 Defining_Identifier =>
3505 Make_Defining_Identifier (Loc, Name_uI),
3506 Parameter_Type =>
3507 New_Occurrence_Of (Standard_Integer, Loc)),
3509 Make_Parameter_Specification (Loc, -- A
3510 Defining_Identifier =>
3511 Make_Defining_Identifier (Loc, Name_uA),
3512 Parameter_Type =>
3513 New_Occurrence_Of (Standard_Boolean, Loc))));
3514 end Make_Disp_Requeue_Spec;
3516 ---------------------------------
3517 -- Make_Disp_Timed_Select_Body --
3518 ---------------------------------
3520 -- For interface types, generate:
3522 -- procedure _Disp_Timed_Select
3523 -- (T : in out <Typ>;
3524 -- S : Integer;
3525 -- P : System.Address;
3526 -- D : Duration;
3527 -- M : Integer;
3528 -- C : out Ada.Tags.Prim_Op_Kind;
3529 -- F : out Boolean)
3530 -- is
3531 -- begin
3532 -- F := False;
3533 -- C := Ada.Tags.POK_Function;
3534 -- end _Disp_Timed_Select;
3536 -- For protected types, generate:
3538 -- procedure _Disp_Timed_Select
3539 -- (T : in out <Typ>;
3540 -- S : Integer;
3541 -- P : System.Address;
3542 -- D : Duration;
3543 -- M : Integer;
3544 -- C : out Ada.Tags.Prim_Op_Kind;
3545 -- F : out Boolean)
3546 -- is
3547 -- I : Integer;
3549 -- begin
3550 -- C := Ada.Tags.Get_Prim_Op_Kind (Ada.Tags.Tag (<Typ>VP), S);
3552 -- if C = Ada.Tags.POK_Procedure
3553 -- or else C = Ada.Tags.POK_Protected_Procedure
3554 -- or else C = Ada.Tags.POK_Task_Procedure
3555 -- then
3556 -- F := True;
3557 -- return;
3558 -- end if;
3560 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP), S);
3561 -- System.Tasking.Protected_Objects.Operations.
3562 -- Timed_Protected_Entry_Call
3563 -- (T._object'Access,
3564 -- System.Tasking.Protected_Objects.Protected_Entry_Index (I),
3565 -- P,
3566 -- D,
3567 -- M,
3568 -- F);
3569 -- end _Disp_Timed_Select;
3571 -- For task types, generate:
3573 -- procedure _Disp_Timed_Select
3574 -- (T : in out <Typ>;
3575 -- S : Integer;
3576 -- P : System.Address;
3577 -- D : Duration;
3578 -- M : Integer;
3579 -- C : out Ada.Tags.Prim_Op_Kind;
3580 -- F : out Boolean)
3581 -- is
3582 -- I : Integer;
3584 -- begin
3585 -- I := Ada.Tags.Get_Entry_Index (Ada.Tags.Tag (<Typ>VP), S);
3586 -- System.Tasking.Rendezvous.Timed_Task_Entry_Call
3587 -- (T._task_id,
3588 -- System.Tasking.Task_Entry_Index (I),
3589 -- P,
3590 -- D,
3591 -- M,
3592 -- F);
3593 -- end _Disp_Time_Select;
3595 function Make_Disp_Timed_Select_Body
3596 (Typ : Entity_Id) return Node_Id
3598 Loc : constant Source_Ptr := Sloc (Typ);
3599 Conc_Typ : Entity_Id := Empty;
3600 Decls : constant List_Id := New_List;
3601 Obj_Ref : Node_Id;
3602 Stmts : constant List_Id := New_List;
3603 Tag_Node : Node_Id;
3605 begin
3606 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3608 -- Null body is generated for interface types
3610 if Is_Interface (Typ) then
3611 return
3612 Make_Subprogram_Body (Loc,
3613 Specification => Make_Disp_Timed_Select_Spec (Typ),
3614 Declarations => New_List,
3615 Handled_Statement_Sequence =>
3616 Make_Handled_Sequence_Of_Statements (Loc,
3617 New_List (
3618 Make_Assignment_Statement (Loc,
3619 Name => Make_Identifier (Loc, Name_uF),
3620 Expression => New_Occurrence_Of (Standard_False, Loc)))));
3621 end if;
3623 if Is_Concurrent_Record_Type (Typ) then
3624 Conc_Typ := Corresponding_Concurrent_Type (Typ);
3626 -- Generate:
3627 -- I : Integer;
3629 -- where I will be used to capture the entry index of the primitive
3630 -- wrapper at position S.
3632 Append_To (Decls,
3633 Make_Object_Declaration (Loc,
3634 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uI),
3635 Object_Definition =>
3636 New_Occurrence_Of (Standard_Integer, Loc)));
3638 -- Generate:
3639 -- C := Get_Prim_Op_Kind (tag! (<type>VP), S);
3641 -- if C = POK_Procedure
3642 -- or else C = POK_Protected_Procedure
3643 -- or else C = POK_Task_Procedure;
3644 -- then
3645 -- F := True;
3646 -- return;
3647 -- end if;
3649 Build_Common_Dispatching_Select_Statements (Typ, Stmts);
3651 -- Generate:
3652 -- I := Get_Entry_Index (tag! (<type>VP), S);
3654 -- I is the entry index and S is the dispatch table slot
3656 if Tagged_Type_Expansion then
3657 Tag_Node :=
3658 Unchecked_Convert_To (RTE (RE_Tag),
3659 New_Occurrence_Of
3660 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc));
3662 else
3663 Tag_Node :=
3664 Make_Attribute_Reference (Loc,
3665 Prefix => New_Occurrence_Of (Typ, Loc),
3666 Attribute_Name => Name_Tag);
3667 end if;
3669 Append_To (Stmts,
3670 Make_Assignment_Statement (Loc,
3671 Name => Make_Identifier (Loc, Name_uI),
3672 Expression =>
3673 Make_Function_Call (Loc,
3674 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
3675 Parameter_Associations => New_List (
3676 Tag_Node,
3677 Make_Identifier (Loc, Name_uS)))));
3679 -- Protected case
3681 if Ekind (Conc_Typ) = E_Protected_Type then
3683 -- Build T._object'Access
3685 Obj_Ref :=
3686 Make_Attribute_Reference (Loc,
3687 Attribute_Name => Name_Unchecked_Access,
3688 Prefix =>
3689 Make_Selected_Component (Loc,
3690 Prefix => Make_Identifier (Loc, Name_uT),
3691 Selector_Name => Make_Identifier (Loc, Name_uObject)));
3693 -- Normal case, No_Entry_Queue restriction not active. In this
3694 -- case we generate:
3696 -- Timed_Protected_Entry_Call
3697 -- (T._object'access,
3698 -- Protected_Entry_Index! (I),
3699 -- P, D, M, F);
3701 -- where T is the protected object, I is the entry index, P are
3702 -- the wrapped parameters, D is the delay amount, M is the delay
3703 -- mode and F is the status flag.
3705 -- Historically, there was also an implementation for single
3706 -- entry protected types (in s-tposen). However, it was removed
3707 -- by also testing for no No_Select_Statements restriction in
3708 -- Exp_Utils.Corresponding_Runtime_Package. This simplified the
3709 -- implementation of s-tposen.adb and provided consistency between
3710 -- all versions of System.Tasking.Protected_Objects.Single_Entry
3711 -- (s-tposen*.adb).
3713 case Corresponding_Runtime_Package (Conc_Typ) is
3714 when System_Tasking_Protected_Objects_Entries =>
3715 Append_To (Stmts,
3716 Make_Procedure_Call_Statement (Loc,
3717 Name =>
3718 New_Occurrence_Of
3719 (RTE (RE_Timed_Protected_Entry_Call), Loc),
3720 Parameter_Associations => New_List (
3721 Obj_Ref,
3723 Unchecked_Convert_To ( -- entry index
3724 RTE (RE_Protected_Entry_Index),
3725 Make_Identifier (Loc, Name_uI)),
3727 Make_Identifier (Loc, Name_uP), -- parameter block
3728 Make_Identifier (Loc, Name_uD), -- delay
3729 Make_Identifier (Loc, Name_uM), -- delay mode
3730 Make_Identifier (Loc, Name_uF)))); -- status flag
3732 when others =>
3733 raise Program_Error;
3734 end case;
3736 -- Task case
3738 else
3739 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
3741 -- Generate:
3742 -- Timed_Task_Entry_Call (
3743 -- T._task_id,
3744 -- Task_Entry_Index! (I),
3745 -- P,
3746 -- D,
3747 -- M,
3748 -- F);
3750 -- where T is the task object, I is the entry index, P are the
3751 -- wrapped parameters, D is the delay amount, M is the delay
3752 -- mode and F is the status flag.
3754 Append_To (Stmts,
3755 Make_Procedure_Call_Statement (Loc,
3756 Name =>
3757 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
3759 Parameter_Associations => New_List (
3760 Make_Selected_Component (Loc, -- T._task_id
3761 Prefix => Make_Identifier (Loc, Name_uT),
3762 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3764 Unchecked_Convert_To ( -- entry index
3765 RTE (RE_Task_Entry_Index),
3766 Make_Identifier (Loc, Name_uI)),
3768 Make_Identifier (Loc, Name_uP), -- parameter block
3769 Make_Identifier (Loc, Name_uD), -- delay
3770 Make_Identifier (Loc, Name_uM), -- delay mode
3771 Make_Identifier (Loc, Name_uF)))); -- status flag
3772 end if;
3774 else
3775 -- Initialize out parameters
3777 Append_To (Stmts,
3778 Make_Assignment_Statement (Loc,
3779 Name => Make_Identifier (Loc, Name_uF),
3780 Expression => New_Occurrence_Of (Standard_False, Loc)));
3781 Append_To (Stmts,
3782 Make_Assignment_Statement (Loc,
3783 Name => Make_Identifier (Loc, Name_uC),
3784 Expression => New_Occurrence_Of (RTE (RE_POK_Function), Loc)));
3785 end if;
3787 return
3788 Make_Subprogram_Body (Loc,
3789 Specification => Make_Disp_Timed_Select_Spec (Typ),
3790 Declarations => Decls,
3791 Handled_Statement_Sequence =>
3792 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
3793 end Make_Disp_Timed_Select_Body;
3795 ---------------------------------
3796 -- Make_Disp_Timed_Select_Spec --
3797 ---------------------------------
3799 function Make_Disp_Timed_Select_Spec
3800 (Typ : Entity_Id) return Node_Id
3802 Loc : constant Source_Ptr := Sloc (Typ);
3803 Def_Id : constant Node_Id :=
3804 Make_Defining_Identifier (Loc,
3805 Name_uDisp_Timed_Select);
3806 Params : constant List_Id := New_List;
3808 begin
3809 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
3811 -- T : in out Typ; -- Object parameter
3812 -- S : Integer; -- Primitive operation slot
3813 -- P : Address; -- Wrapped parameters
3814 -- D : Duration; -- Delay
3815 -- M : Integer; -- Delay Mode
3816 -- C : out Prim_Op_Kind; -- Call kind
3817 -- F : out Boolean; -- Status flag
3819 Append_List_To (Params, New_List (
3821 Make_Parameter_Specification (Loc,
3822 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uT),
3823 Parameter_Type => New_Occurrence_Of (Typ, Loc),
3824 In_Present => True,
3825 Out_Present => True),
3827 Make_Parameter_Specification (Loc,
3828 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uS),
3829 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
3831 Make_Parameter_Specification (Loc,
3832 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
3833 Parameter_Type => New_Occurrence_Of (RTE (RE_Address), Loc)),
3835 Make_Parameter_Specification (Loc,
3836 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uD),
3837 Parameter_Type => New_Occurrence_Of (Standard_Duration, Loc)),
3839 Make_Parameter_Specification (Loc,
3840 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uM),
3841 Parameter_Type => New_Occurrence_Of (Standard_Integer, Loc)),
3843 Make_Parameter_Specification (Loc,
3844 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uC),
3845 Parameter_Type =>
3846 New_Occurrence_Of (RTE (RE_Prim_Op_Kind), Loc),
3847 Out_Present => True)));
3849 Append_To (Params,
3850 Make_Parameter_Specification (Loc,
3851 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uF),
3852 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
3853 Out_Present => True));
3855 return
3856 Make_Procedure_Specification (Loc,
3857 Defining_Unit_Name => Def_Id,
3858 Parameter_Specifications => Params);
3859 end Make_Disp_Timed_Select_Spec;
3861 -------------
3862 -- Make_DT --
3863 -------------
3865 -- The frontend supports two models for expanding dispatch tables
3866 -- associated with library-level defined tagged types: statically and
3867 -- non-statically allocated dispatch tables. In the former case the object
3868 -- containing the dispatch table is constant and it is initialized by means
3869 -- of a positional aggregate. In the latter case, the object containing
3870 -- the dispatch table is a variable which is initialized by means of
3871 -- assignments.
3873 -- In case of locally defined tagged types, the object containing the
3874 -- object containing the dispatch table is always a variable (instead of a
3875 -- constant). This is currently required to give support to late overriding
3876 -- of primitives. For example:
3878 -- procedure Example is
3879 -- package Pkg is
3880 -- type T1 is tagged null record;
3881 -- procedure Prim (O : T1);
3882 -- end Pkg;
3884 -- type T2 is new Pkg.T1 with null record;
3885 -- procedure Prim (X : T2) is -- late overriding
3886 -- begin
3887 -- ...
3888 -- ...
3889 -- end;
3891 -- WARNING: This routine manages Ghost regions. Return statements must be
3892 -- replaced by gotos which jump to the end of the routine and restore the
3893 -- Ghost mode.
3895 function Make_DT (Typ : Entity_Id) return List_Id is
3896 Loc : constant Source_Ptr := Sloc (Typ);
3898 Max_Predef_Prims : constant Int :=
3899 UI_To_Int
3900 (Intval
3901 (Expression
3902 (Parent (RTE (RE_Max_Predef_Prims)))));
3904 DT_Decl : constant Elist_Id := New_Elmt_List;
3905 DT_Aggr : constant Elist_Id := New_Elmt_List;
3906 -- Entities marked with attribute Is_Dispatch_Table_Entity
3908 Dummy_Object : Entity_Id := Empty;
3909 -- Extra nonexistent object of type Typ internally used to compute the
3910 -- offset to the components that reference secondary dispatch tables.
3911 -- Used to compute the offset of components located at fixed position.
3913 procedure Export_DT (Typ : Entity_Id; DT : Entity_Id; Index : Nat := 0);
3914 -- Export the dispatch table DT of tagged type Typ. Required to generate
3915 -- forward references and statically allocate the table. For primary
3916 -- dispatch tables Index is 0; for secondary dispatch tables the value
3917 -- of index must match the Suffix_Index value assigned to the table by
3918 -- Make_Tags when generating its unique external name, and it is used to
3919 -- retrieve from the Dispatch_Table_Wrappers list associated with Typ
3920 -- the external name generated by Import_DT.
3922 procedure Make_Secondary_DT
3923 (Typ : Entity_Id;
3924 Iface : Entity_Id;
3925 Iface_Comp : Node_Id;
3926 Suffix_Index : Int;
3927 Num_Iface_Prims : Nat;
3928 Iface_DT_Ptr : Entity_Id;
3929 Predef_Prims_Ptr : Entity_Id;
3930 Build_Thunks : Boolean;
3931 Result : List_Id);
3932 -- Ada 2005 (AI-251): Expand the declarations for a Secondary Dispatch
3933 -- Table of Typ associated with Iface. Each abstract interface of Typ
3934 -- has two secondary dispatch tables: one containing pointers to thunks
3935 -- and another containing pointers to the primitives covering the
3936 -- interface primitives. The former secondary table is generated when
3937 -- Build_Thunks is True, and provides common support for dispatching
3938 -- calls through interface types; the latter secondary table is
3939 -- generated when Build_Thunks is False, and provides support for
3940 -- Generic Dispatching Constructors that dispatch calls through
3941 -- interface types. When constructing this latter table the value of
3942 -- Suffix_Index is -1 to indicate that there is no need to export such
3943 -- table when building statically allocated dispatch tables; a positive
3944 -- value of Suffix_Index must match the Suffix_Index value assigned to
3945 -- this secondary dispatch table by Make_Tags when its unique external
3946 -- name was generated.
3948 function Number_Of_Predefined_Prims (Typ : Entity_Id) return Nat;
3949 -- Returns the number of predefined primitives of Typ
3951 ---------------
3952 -- Export_DT --
3953 ---------------
3955 procedure Export_DT (Typ : Entity_Id; DT : Entity_Id; Index : Nat := 0)
3957 Count : Nat;
3958 Elmt : Elmt_Id;
3960 begin
3961 Set_Is_Statically_Allocated (DT);
3962 Set_Is_True_Constant (DT);
3963 Set_Is_Exported (DT);
3965 Count := 0;
3966 Elmt := First_Elmt (Dispatch_Table_Wrappers (Typ));
3967 while Count /= Index loop
3968 Next_Elmt (Elmt);
3969 Count := Count + 1;
3970 end loop;
3972 -- Related_Type (Node (Elmt)) should be equal to Typ here, but we
3973 -- can't assert that, because it is sometimes false in illegal
3974 -- programs. We can't check Serious_Errors_Detected, because the
3975 -- errors have not yet been detected.
3977 Get_External_Name (Node (Elmt));
3978 Set_Interface_Name (DT,
3979 Make_String_Literal (Loc,
3980 Strval => String_From_Name_Buffer));
3982 -- Ensure proper Sprint output of this implicit importation
3984 Set_Is_Internal (DT);
3985 Set_Is_Public (DT);
3986 end Export_DT;
3988 -----------------------
3989 -- Make_Secondary_DT --
3990 -----------------------
3992 procedure Make_Secondary_DT
3993 (Typ : Entity_Id;
3994 Iface : Entity_Id;
3995 Iface_Comp : Node_Id;
3996 Suffix_Index : Int;
3997 Num_Iface_Prims : Nat;
3998 Iface_DT_Ptr : Entity_Id;
3999 Predef_Prims_Ptr : Entity_Id;
4000 Build_Thunks : Boolean;
4001 Result : List_Id)
4003 Loc : constant Source_Ptr := Sloc (Typ);
4004 Exporting_Table : constant Boolean :=
4005 Building_Static_DT (Typ)
4006 and then Suffix_Index > 0;
4007 Iface_DT : constant Entity_Id := Make_Temporary (Loc, 'T');
4008 Predef_Prims : constant Entity_Id := Make_Temporary (Loc, 'R');
4009 DT_Constr_List : List_Id;
4010 DT_Aggr_List : List_Id;
4011 Empty_DT : Boolean := False;
4012 Nb_Prim : Nat;
4013 New_Node : Node_Id;
4014 OSD : Entity_Id;
4015 OSD_Aggr_List : List_Id;
4016 Prim : Entity_Id;
4017 Prim_Elmt : Elmt_Id;
4018 Prim_Ops_Aggr_List : List_Id;
4020 begin
4021 -- Handle cases in which we do not generate statically allocated
4022 -- dispatch tables.
4024 if not Building_Static_DT (Typ) then
4025 Mutate_Ekind (Predef_Prims, E_Variable);
4026 Mutate_Ekind (Iface_DT, E_Variable);
4028 -- Statically allocated dispatch tables and related entities are
4029 -- constants.
4031 else
4032 Mutate_Ekind (Predef_Prims, E_Constant);
4033 Set_Is_Statically_Allocated (Predef_Prims);
4034 Set_Is_True_Constant (Predef_Prims);
4036 Mutate_Ekind (Iface_DT, E_Constant);
4037 Set_Is_Statically_Allocated (Iface_DT);
4038 Set_Is_True_Constant (Iface_DT);
4039 end if;
4041 -- Calculate the number of slots of the dispatch table. If the number
4042 -- of primitives of Typ is 0 we reserve a dummy single entry for its
4043 -- DT because at run time the pointer to this dummy entry will be
4044 -- used as the tag.
4046 if Num_Iface_Prims = 0 then
4047 Empty_DT := True;
4048 Nb_Prim := 1;
4049 else
4050 Nb_Prim := Num_Iface_Prims;
4051 end if;
4053 -- Generate:
4055 -- Predef_Prims : Address_Array (1 .. Default_Prim_Ops_Count) :=
4056 -- (predef-prim-op-thunk-1'address,
4057 -- predef-prim-op-thunk-2'address,
4058 -- ...
4059 -- predef-prim-op-thunk-n'address);
4061 -- Create the thunks associated with the predefined primitives and
4062 -- save their entity to fill the aggregate.
4064 declare
4065 Nb_P_Prims : constant Nat := Number_Of_Predefined_Prims (Typ);
4066 Prim_Table : array (Nat range 1 .. Nb_P_Prims) of Entity_Id;
4067 Decl : Node_Id;
4068 E : Entity_Id;
4069 SS_Thunk_Id : Entity_Id;
4070 SS_Thunk_Code : Node_Id;
4071 Thunk_Id : Entity_Id;
4072 Thunk_Code : List_Id;
4074 begin
4075 Prim_Ops_Aggr_List := New_List;
4076 Prim_Table := (others => Empty);
4078 if Building_Static_DT (Typ) then
4079 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
4080 while Present (Prim_Elmt) loop
4081 Prim := Node (Prim_Elmt);
4083 if Is_Predefined_Dispatching_Operation (Prim)
4084 and then not Is_Abstract_Subprogram (Prim)
4085 and then not Is_Eliminated (Prim)
4086 and then not Generate_SCIL
4087 and then No (Prim_Table (UI_To_Int (DT_Position (Prim))))
4088 then
4089 if not Build_Thunks then
4090 E := Ultimate_Alias (Prim);
4091 Expand_Secondary_Stack_Thunk
4092 (E, SS_Thunk_Id, SS_Thunk_Code);
4094 if Present (SS_Thunk_Id) then
4095 E := SS_Thunk_Id;
4096 Append_To (Result, SS_Thunk_Code);
4097 end if;
4099 Prim_Table (UI_To_Int (DT_Position (Prim))) := E;
4101 else
4102 Expand_Interface_Thunk
4103 (Prim, Thunk_Id, Thunk_Code, Iface);
4105 if Present (Thunk_Id) then
4106 Append_List_To (Result, Thunk_Code);
4107 Prim_Table (UI_To_Int (DT_Position (Prim))) :=
4108 Thunk_Id;
4109 end if;
4110 end if;
4111 end if;
4113 Next_Elmt (Prim_Elmt);
4114 end loop;
4115 end if;
4117 for J in Prim_Table'Range loop
4118 if Present (Prim_Table (J)) then
4119 New_Node :=
4120 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
4121 Make_Attribute_Reference (Loc,
4122 Prefix => New_Occurrence_Of (Prim_Table (J), Loc),
4123 Attribute_Name => Name_Unrestricted_Access));
4124 else
4125 New_Node := Make_Null (Loc);
4126 end if;
4128 Append_To (Prim_Ops_Aggr_List, New_Node);
4129 end loop;
4131 New_Node :=
4132 Make_Aggregate (Loc, Expressions => Prim_Ops_Aggr_List);
4134 -- Remember aggregates initializing dispatch tables
4136 Append_Elmt (New_Node, DT_Aggr);
4138 Decl :=
4139 Make_Subtype_Declaration (Loc,
4140 Defining_Identifier => Make_Temporary (Loc, 'S'),
4141 Subtype_Indication =>
4142 New_Occurrence_Of (RTE (RE_Address_Array), Loc));
4144 Append_To (Result, Decl);
4146 Append_To (Result,
4147 Make_Object_Declaration (Loc,
4148 Defining_Identifier => Predef_Prims,
4149 Constant_Present => Building_Static_DT (Typ),
4150 Aliased_Present => True,
4151 Object_Definition => New_Occurrence_Of
4152 (Defining_Identifier (Decl), Loc),
4153 Expression => New_Node));
4154 end;
4156 -- Generate
4158 -- OSD : Ada.Tags.Object_Specific_Data (Nb_Prims) :=
4159 -- (OSD_Table => (1 => <value>,
4160 -- ...
4161 -- N => <value>));
4162 -- for OSD'Alignment use Address'Alignment;
4164 -- Iface_DT : Dispatch_Table (Nb_Prims) :=
4165 -- ([ Signature => <sig-value> ],
4166 -- Tag_Kind => <tag_kind-value>,
4167 -- Predef_Prims => Predef_Prims'Address,
4168 -- Offset_To_Top => 0,
4169 -- OSD => OSD'Address,
4170 -- Prims_Ptr => (prim-op-1'address,
4171 -- prim-op-2'address,
4172 -- ...
4173 -- prim-op-n'address));
4175 -- Stage 3: Initialize the discriminant and the record components
4177 DT_Constr_List := New_List;
4178 DT_Aggr_List := New_List;
4180 -- Nb_Prim
4182 Append_To (DT_Constr_List, Make_Integer_Literal (Loc, Nb_Prim));
4183 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, Nb_Prim));
4185 -- Signature
4187 if RTE_Record_Component_Available (RE_Signature) then
4188 Append_To (DT_Aggr_List,
4189 New_Occurrence_Of (RTE (RE_Secondary_DT), Loc));
4190 end if;
4192 -- Tag_Kind
4194 if RTE_Record_Component_Available (RE_Tag_Kind) then
4195 Append_To (DT_Aggr_List, Tagged_Kind (Typ));
4196 end if;
4198 -- Predef_Prims
4200 Append_To (DT_Aggr_List,
4201 Make_Attribute_Reference (Loc,
4202 Prefix => New_Occurrence_Of (Predef_Prims, Loc),
4203 Attribute_Name => Name_Address));
4205 -- Interface component located at variable offset; the value of
4206 -- Offset_To_Top will be set by the init subprogram.
4208 if No (Dummy_Object)
4209 or else Is_Variable_Size_Record (Etype (Scope (Iface_Comp)))
4210 then
4211 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, 0));
4213 -- Interface component located at fixed offset
4215 else
4216 Append_To (DT_Aggr_List,
4217 Make_Op_Minus (Loc,
4218 Make_Attribute_Reference (Loc,
4219 Prefix =>
4220 Make_Selected_Component (Loc,
4221 Prefix =>
4222 New_Occurrence_Of (Dummy_Object, Loc),
4223 Selector_Name =>
4224 New_Occurrence_Of (Iface_Comp, Loc)),
4225 Attribute_Name => Name_Position)));
4226 end if;
4228 -- Generate the Object Specific Data table required to dispatch calls
4229 -- through synchronized interfaces.
4231 if Empty_DT
4232 or else Is_Abstract_Type (Typ)
4233 or else Is_Controlled (Typ)
4234 or else Restriction_Active (No_Dispatching_Calls)
4235 or else not Is_Limited_Type (Typ)
4236 or else not Has_Interfaces (Typ)
4237 or else not Build_Thunks
4238 or else not RTE_Record_Component_Available (RE_OSD_Table)
4239 then
4240 -- No OSD table required
4242 Append_To (DT_Aggr_List,
4243 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
4245 else
4246 OSD_Aggr_List := New_List;
4248 declare
4249 Prim_Table : array (Nat range 1 .. Nb_Prim) of Entity_Id;
4250 Prim : Entity_Id;
4251 Prim_Alias : Entity_Id;
4252 Prim_Elmt : Elmt_Id;
4253 E : Entity_Id;
4254 Count : Nat;
4255 Pos : Nat;
4256 SS_Thunk_Id : Entity_Id;
4257 SS_Thunk_Code : Node_Id;
4259 begin
4260 Prim_Table := (others => Empty);
4261 Prim_Alias := Empty;
4262 Count := 0;
4264 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
4265 while Present (Prim_Elmt) loop
4266 Prim := Node (Prim_Elmt);
4268 if Present (Interface_Alias (Prim))
4269 and then Find_Dispatching_Type
4270 (Interface_Alias (Prim)) = Iface
4271 then
4272 Prim_Alias := Interface_Alias (Prim);
4273 E := Ultimate_Alias (Prim);
4274 Pos := UI_To_Int (DT_Position (Prim_Alias));
4276 if No (Prim_Table (Pos)) then
4277 Expand_Secondary_Stack_Thunk
4278 (E, SS_Thunk_Id, SS_Thunk_Code);
4280 if Present (SS_Thunk_Id) then
4281 E := SS_Thunk_Id;
4282 Append_To (Result, SS_Thunk_Code);
4283 end if;
4285 Prim_Table (Pos) := E;
4287 Append_To (OSD_Aggr_List,
4288 Make_Component_Association (Loc,
4289 Choices => New_List (
4290 Make_Integer_Literal (Loc,
4291 DT_Position (Prim_Alias))),
4292 Expression =>
4293 Make_Integer_Literal (Loc,
4294 DT_Position (Alias (Prim)))));
4296 Count := Count + 1;
4297 end if;
4298 end if;
4300 Next_Elmt (Prim_Elmt);
4301 end loop;
4302 pragma Assert (Count = Nb_Prim);
4303 end;
4305 OSD := Make_Temporary (Loc, 'I');
4307 Append_To (Result,
4308 Make_Object_Declaration (Loc,
4309 Defining_Identifier => OSD,
4310 Constant_Present => True,
4311 Object_Definition =>
4312 Make_Subtype_Indication (Loc,
4313 Subtype_Mark =>
4314 New_Occurrence_Of (RTE (RE_Object_Specific_Data), Loc),
4315 Constraint =>
4316 Make_Index_Or_Discriminant_Constraint (Loc,
4317 Constraints => New_List (
4318 Make_Integer_Literal (Loc, Nb_Prim)))),
4320 Expression =>
4321 Make_Aggregate (Loc,
4322 Component_Associations => New_List (
4323 Make_Component_Association (Loc,
4324 Choices => New_List (
4325 New_Occurrence_Of
4326 (RTE_Record_Component (RE_OSD_Num_Prims), Loc)),
4327 Expression =>
4328 Make_Integer_Literal (Loc, Nb_Prim)),
4330 Make_Component_Association (Loc,
4331 Choices => New_List (
4332 New_Occurrence_Of
4333 (RTE_Record_Component (RE_OSD_Table), Loc)),
4334 Expression => Make_Aggregate (Loc,
4335 Component_Associations => OSD_Aggr_List))))));
4337 Append_To (Result,
4338 Make_Attribute_Definition_Clause (Loc,
4339 Name => New_Occurrence_Of (OSD, Loc),
4340 Chars => Name_Alignment,
4341 Expression =>
4342 Make_Attribute_Reference (Loc,
4343 Prefix =>
4344 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
4345 Attribute_Name => Name_Alignment)));
4347 -- In secondary dispatch tables the Typeinfo component contains
4348 -- the address of the Object Specific Data (see a-tags.ads).
4350 Append_To (DT_Aggr_List,
4351 Make_Attribute_Reference (Loc,
4352 Prefix => New_Occurrence_Of (OSD, Loc),
4353 Attribute_Name => Name_Address));
4354 end if;
4356 -- Initialize the table of primitive operations
4358 Prim_Ops_Aggr_List := New_List;
4360 if Empty_DT then
4361 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
4363 elsif Is_Abstract_Type (Typ)
4364 or else not Building_Static_DT (Typ)
4365 then
4366 for J in 1 .. Nb_Prim loop
4367 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
4368 end loop;
4370 else
4371 declare
4372 CPP_Nb_Prims : constant Nat := CPP_Num_Prims (Typ);
4373 E : Entity_Id;
4374 Prim_Pos : Nat;
4375 Prim_Table : array (Nat range 1 .. Nb_Prim) of Entity_Id;
4376 SS_Thunk_Id : Entity_Id;
4377 SS_Thunk_Code : Node_Id;
4378 Thunk_Id : Entity_Id;
4379 Thunk_Code : List_Id;
4381 begin
4382 Prim_Table := (others => Empty);
4384 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
4385 while Present (Prim_Elmt) loop
4386 Prim := Node (Prim_Elmt);
4387 E := Ultimate_Alias (Prim);
4388 Prim_Pos := UI_To_Int (DT_Position (E));
4390 -- Do not reference predefined primitives because they are
4391 -- located in a separate dispatch table; skip abstract and
4392 -- eliminated primitives; skip primitives located in the C++
4393 -- part of the dispatch table because their slot is set by
4394 -- the IC routine.
4396 if not Is_Predefined_Dispatching_Operation (Prim)
4397 and then Present (Interface_Alias (Prim))
4398 and then not Is_Abstract_Subprogram (Alias (Prim))
4399 and then not Is_Eliminated (Alias (Prim))
4400 and then (not Is_CPP_Class (Root_Type (Typ))
4401 or else Prim_Pos > CPP_Nb_Prims)
4402 and then Find_Dispatching_Type
4403 (Interface_Alias (Prim)) = Iface
4405 -- Generate the code of the thunk only if the abstract
4406 -- interface type is not an immediate ancestor of
4407 -- Tagged_Type. Otherwise the DT associated with the
4408 -- interface is the primary DT.
4410 and then not Is_Ancestor (Iface, Typ,
4411 Use_Full_View => True)
4412 then
4413 if not Build_Thunks then
4414 E := Alias (Prim);
4415 Expand_Secondary_Stack_Thunk
4416 (E, SS_Thunk_Id, SS_Thunk_Code);
4418 if Present (SS_Thunk_Id) then
4419 E := SS_Thunk_Id;
4420 Append_To (Result, SS_Thunk_Code);
4421 end if;
4423 Prim_Pos :=
4424 UI_To_Int (DT_Position (Interface_Alias (Prim)));
4425 Prim_Table (Prim_Pos) := E;
4427 else
4428 Expand_Interface_Thunk
4429 (Prim, Thunk_Id, Thunk_Code, Iface);
4431 if Present (Thunk_Id) then
4432 Prim_Pos :=
4433 UI_To_Int (DT_Position (Interface_Alias (Prim)));
4435 Prim_Table (Prim_Pos) := Thunk_Id;
4436 Append_List_To (Result, Thunk_Code);
4437 end if;
4438 end if;
4439 end if;
4441 Next_Elmt (Prim_Elmt);
4442 end loop;
4444 for J in Prim_Table'Range loop
4445 if Present (Prim_Table (J)) then
4446 New_Node :=
4447 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
4448 Make_Attribute_Reference (Loc,
4449 Prefix => New_Occurrence_Of (Prim_Table (J), Loc),
4450 Attribute_Name => Name_Unrestricted_Access));
4452 else
4453 New_Node := Make_Null (Loc);
4454 end if;
4456 Append_To (Prim_Ops_Aggr_List, New_Node);
4457 end loop;
4458 end;
4459 end if;
4461 New_Node :=
4462 Make_Aggregate (Loc,
4463 Expressions => Prim_Ops_Aggr_List);
4465 Append_To (DT_Aggr_List, New_Node);
4467 -- Remember aggregates initializing dispatch tables
4469 Append_Elmt (New_Node, DT_Aggr);
4471 -- Note: Secondary dispatch tables are declared constant only if
4472 -- we can compute their offset field by means of the extra dummy
4473 -- object; otherwise they cannot be declared constant and the
4474 -- Offset_To_Top component is initialized by the IP routine.
4476 Append_To (Result,
4477 Make_Object_Declaration (Loc,
4478 Defining_Identifier => Iface_DT,
4479 Aliased_Present => True,
4480 Constant_Present => Building_Static_Secondary_DT (Typ),
4482 Object_Definition =>
4483 Make_Subtype_Indication (Loc,
4484 Subtype_Mark => New_Occurrence_Of
4485 (RTE (RE_Dispatch_Table_Wrapper), Loc),
4486 Constraint => Make_Index_Or_Discriminant_Constraint (Loc,
4487 Constraints => DT_Constr_List)),
4489 Expression =>
4490 Make_Aggregate (Loc,
4491 Expressions => DT_Aggr_List)));
4493 if Exporting_Table then
4494 Export_DT (Typ, Iface_DT, Suffix_Index);
4496 -- Generate code to create the pointer to the dispatch table
4498 -- Iface_DT_Ptr : Tag := Tag!(DT.Prims_Ptr'Address);
4500 -- Note: This declaration is not added here if the table is exported
4501 -- because in such case Make_Tags has already added this declaration.
4503 else
4504 Append_To (Result,
4505 Make_Object_Declaration (Loc,
4506 Defining_Identifier => Iface_DT_Ptr,
4507 Constant_Present => True,
4509 Object_Definition =>
4510 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc),
4512 Expression =>
4513 Unchecked_Convert_To (RTE (RE_Interface_Tag),
4514 Make_Attribute_Reference (Loc,
4515 Prefix =>
4516 Make_Selected_Component (Loc,
4517 Prefix => New_Occurrence_Of (Iface_DT, Loc),
4518 Selector_Name =>
4519 New_Occurrence_Of
4520 (RTE_Record_Component (RE_Prims_Ptr), Loc)),
4521 Attribute_Name => Name_Address))));
4522 end if;
4524 Append_To (Result,
4525 Make_Object_Declaration (Loc,
4526 Defining_Identifier => Predef_Prims_Ptr,
4527 Constant_Present => True,
4529 Object_Definition =>
4530 New_Occurrence_Of (RTE (RE_Address), Loc),
4532 Expression =>
4533 Make_Attribute_Reference (Loc,
4534 Prefix =>
4535 Make_Selected_Component (Loc,
4536 Prefix => New_Occurrence_Of (Iface_DT, Loc),
4537 Selector_Name =>
4538 New_Occurrence_Of
4539 (RTE_Record_Component (RE_Predef_Prims), Loc)),
4540 Attribute_Name => Name_Address)));
4542 -- Remember entities containing dispatch tables
4544 Append_Elmt (Predef_Prims, DT_Decl);
4545 Append_Elmt (Iface_DT, DT_Decl);
4546 end Make_Secondary_DT;
4548 --------------------------------
4549 -- Number_Of_Predefined_Prims --
4550 --------------------------------
4552 function Number_Of_Predefined_Prims (Typ : Entity_Id) return Nat is
4553 Nb_Predef_Prims : Nat := 0;
4555 begin
4556 if not Generate_SCIL then
4557 declare
4558 Prim : Entity_Id;
4559 Prim_Elmt : Elmt_Id;
4560 Pos : Nat;
4562 begin
4563 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
4564 while Present (Prim_Elmt) loop
4565 Prim := Node (Prim_Elmt);
4567 if Is_Predefined_Dispatching_Operation (Prim)
4568 and then not Is_Abstract_Subprogram (Prim)
4569 then
4570 Pos := UI_To_Int (DT_Position (Prim));
4572 if Pos > Nb_Predef_Prims then
4573 Nb_Predef_Prims := Pos;
4574 end if;
4575 end if;
4577 Next_Elmt (Prim_Elmt);
4578 end loop;
4579 end;
4580 end if;
4582 pragma Assert (Nb_Predef_Prims <= Max_Predef_Prims);
4583 return Nb_Predef_Prims;
4584 end Number_Of_Predefined_Prims;
4586 -- Local variables
4588 Elab_Code : constant List_Id := New_List;
4589 Result : constant List_Id := New_List;
4590 Tname : constant Name_Id := Chars (Typ);
4592 -- When pragmas Discard_Names and No_Tagged_Streams simultaneously apply
4593 -- we initialize the Expanded_Name and the External_Tag of this tagged
4594 -- type with an empty string. This is useful to avoid exposing entity
4595 -- names at binary level. It can be done when both pragmas apply because
4596 -- (1) Discard_Names allows initializing Expanded_Name with an
4597 -- implementation defined value (Ada RM Section C.5 (7/2)).
4598 -- (2) External_Tag (combined with Internal_Tag) is used for object
4599 -- streaming and No_Tagged_Streams inhibits the generation of
4600 -- streams.
4602 Discard_Names : constant Boolean :=
4603 (Present (No_Tagged_Streams_Pragma (Typ))
4604 or else Restriction_Active (No_Streams))
4605 and then
4606 (Global_Discard_Names or else Einfo.Entities.Discard_Names (Typ));
4608 -- The following name entries are used by Make_DT to generate a number
4609 -- of entities related to a tagged type. These entities may be generated
4610 -- in a scope other than that of the tagged type declaration, and if
4611 -- the entities for two tagged types with the same name happen to be
4612 -- generated in the same scope, we have to take care to use different
4613 -- names. This is achieved by means of a unique serial number appended
4614 -- to each generated entity name.
4616 Name_DT : constant Name_Id :=
4617 New_External_Name (Tname, 'T', Suffix_Index => -1);
4618 Name_Exname : constant Name_Id :=
4619 New_External_Name (Tname, 'E', Suffix_Index => -1);
4620 Name_HT_Link : constant Name_Id :=
4621 New_External_Name (Tname, 'H', Suffix_Index => -1);
4622 Name_Predef_Prims : constant Name_Id :=
4623 New_External_Name (Tname, 'R', Suffix_Index => -1);
4624 Name_SSD : constant Name_Id :=
4625 New_External_Name (Tname, 'S', Suffix_Index => -1);
4626 Name_TSD : constant Name_Id :=
4627 New_External_Name (Tname, 'B', Suffix_Index => -1);
4629 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4630 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4631 -- Save the Ghost-related attributes to restore on exit
4633 AI : Elmt_Id;
4634 AI_Tag_Elmt : Elmt_Id;
4635 AI_Tag_Comp : Elmt_Id;
4636 DT : Entity_Id;
4637 DT_Aggr_List : List_Id;
4638 DT_Constr_List : List_Id;
4639 DT_Ptr : Entity_Id;
4640 Exname : Entity_Id;
4641 HT_Link : Entity_Id;
4642 ITable : Node_Id;
4643 I_Depth : Nat;
4644 Iface_Table_Node : Node_Id;
4645 Name_ITable : Name_Id;
4646 Nb_Prim : Nat := 0;
4647 New_Node : Node_Id;
4648 Num_Ifaces : Nat := 0;
4649 Parent_Typ : Entity_Id;
4650 Predef_Prims : Entity_Id;
4651 Prim : Entity_Id;
4652 Prim_Elmt : Elmt_Id;
4653 Prim_Ops_Aggr_List : List_Id;
4654 SSD : Entity_Id;
4655 Suffix_Index : Int;
4656 Typ_Comps : Elist_Id;
4657 Typ_Ifaces : Elist_Id;
4658 TSD : Entity_Id;
4659 TSD_Aggr_List : List_Id;
4660 TSD_Tags_List : List_Id;
4662 -- Start of processing for Make_DT
4664 begin
4665 pragma Assert (Is_Frozen (Typ));
4667 -- The tagged type being processed may be subject to pragma Ghost. Set
4668 -- the mode now to ensure that any nodes generated during dispatch table
4669 -- creation are properly marked as Ghost.
4671 Set_Ghost_Mode (Typ);
4673 -- Handle cases in which there is no need to build the dispatch table
4675 if Has_Dispatch_Table (Typ)
4676 or else No (Access_Disp_Table (Typ))
4677 or else Is_CPP_Class (Typ)
4678 then
4679 goto Leave;
4681 elsif No_Run_Time_Mode then
4682 Error_Msg_CRT ("tagged types", Typ);
4683 goto Leave;
4685 elsif not RTE_Available (RE_Tag) then
4686 Append_To (Result,
4687 Make_Object_Declaration (Loc,
4688 Defining_Identifier =>
4689 Node (First_Elmt (Access_Disp_Table (Typ))),
4690 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
4691 Constant_Present => True,
4692 Expression =>
4693 Unchecked_Convert_To (RTE (RE_Tag),
4694 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
4696 Analyze_List (Result, Suppress => All_Checks);
4697 Error_Msg_CRT ("tagged types", Typ);
4698 goto Leave;
4699 end if;
4701 -- Ensure that the value of Max_Predef_Prims defined in a-tags is
4702 -- correct. Valid values are 10 under configurable runtime or 16
4703 -- with full runtime.
4705 if RTE_Available (RE_Interface_Data) then
4706 if Max_Predef_Prims /= 16 then
4707 Error_Msg_N ("run-time library configuration error", Typ);
4708 goto Leave;
4709 end if;
4710 else
4711 if Max_Predef_Prims /= 10 then
4712 Error_Msg_N ("run-time library configuration error", Typ);
4713 Error_Msg_CRT ("tagged types", Typ);
4714 goto Leave;
4715 end if;
4716 end if;
4718 DT := Make_Defining_Identifier (Loc, Name_DT);
4719 Exname := Make_Defining_Identifier (Loc, Name_Exname);
4720 HT_Link := Make_Defining_Identifier (Loc, Name_HT_Link);
4721 Predef_Prims := Make_Defining_Identifier (Loc, Name_Predef_Prims);
4722 SSD := Make_Defining_Identifier (Loc, Name_SSD);
4723 TSD := Make_Defining_Identifier (Loc, Name_TSD);
4725 -- Initialize Parent_Typ handling private types
4727 Parent_Typ := Etype (Typ);
4729 if Present (Full_View (Parent_Typ)) then
4730 Parent_Typ := Full_View (Parent_Typ);
4731 end if;
4733 if not Is_Interface (Typ) and then Has_Interfaces (Typ) then
4734 declare
4735 Cannot_Have_Null_Disc : Boolean := False;
4736 Dummy_Object_Typ : constant Entity_Id := Typ;
4737 Name_Dummy_Object : constant Name_Id :=
4738 New_External_Name (Tname,
4739 'P', Suffix_Index => -1);
4740 begin
4741 Dummy_Object := Make_Defining_Identifier (Loc, Name_Dummy_Object);
4743 -- Define the extra object imported and constant to avoid linker
4744 -- errors (since this object is never declared). Required because
4745 -- we implement RM 13.3(19) for exported and imported (variable)
4746 -- objects by making them volatile.
4748 Set_Is_Imported (Dummy_Object);
4749 Mutate_Ekind (Dummy_Object, E_Constant);
4750 Set_Is_True_Constant (Dummy_Object);
4751 Set_Related_Type (Dummy_Object, Typ);
4753 -- The scope must be set now to call Get_External_Name
4755 Set_Scope (Dummy_Object, Current_Scope);
4757 Get_External_Name (Dummy_Object);
4758 Set_Interface_Name (Dummy_Object,
4759 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
4761 -- Ensure proper Sprint output of this implicit importation
4763 Set_Is_Internal (Dummy_Object);
4765 if not Has_Discriminants (Dummy_Object_Typ) then
4766 Append_To (Result,
4767 Make_Object_Declaration (Loc,
4768 Defining_Identifier => Dummy_Object,
4769 Constant_Present => True,
4770 Object_Definition => New_Occurrence_Of
4771 (Dummy_Object_Typ, Loc)));
4772 else
4773 declare
4774 Constr_List : constant List_Id := New_List;
4775 Discrim : Node_Id;
4777 begin
4778 Discrim := First_Discriminant (Dummy_Object_Typ);
4779 while Present (Discrim) loop
4780 if Is_Discrete_Type (Etype (Discrim)) then
4781 Append_To (Constr_List,
4782 Make_Attribute_Reference (Loc,
4783 Prefix =>
4784 New_Occurrence_Of (Etype (Discrim), Loc),
4785 Attribute_Name => Name_First));
4787 else
4788 pragma Assert (Is_Access_Type (Etype (Discrim)));
4789 Cannot_Have_Null_Disc :=
4790 Cannot_Have_Null_Disc
4791 or else Can_Never_Be_Null (Etype (Discrim));
4792 Append_To (Constr_List, Make_Null (Loc));
4793 end if;
4795 Next_Discriminant (Discrim);
4796 end loop;
4798 Append_To (Result,
4799 Make_Object_Declaration (Loc,
4800 Defining_Identifier => Dummy_Object,
4801 Constant_Present => True,
4802 Object_Definition =>
4803 Make_Subtype_Indication (Loc,
4804 Subtype_Mark =>
4805 New_Occurrence_Of (Dummy_Object_Typ, Loc),
4806 Constraint =>
4807 Make_Index_Or_Discriminant_Constraint (Loc,
4808 Constraints => Constr_List))));
4809 end;
4810 end if;
4812 -- Given that the dummy object will not be declared at run time,
4813 -- analyze its declaration with expansion disabled and warnings
4814 -- and error messages ignored.
4816 Expander_Mode_Save_And_Set (False);
4817 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
4818 Analyze (Last (Result), Suppress => All_Checks);
4819 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
4820 Expander_Mode_Restore;
4821 end;
4822 end if;
4824 -- Ada 2005 (AI-251): Build the secondary dispatch tables
4826 if Has_Interfaces (Typ) then
4827 Collect_Interface_Components (Typ, Typ_Comps);
4829 -- Each secondary dispatch table is assigned an unique positive
4830 -- suffix index; such value also corresponds with the location of
4831 -- its entity in the Dispatch_Table_Wrappers list (see Make_Tags).
4833 -- Note: This value must be kept sync with the Suffix_Index values
4834 -- generated by Make_Tags
4836 Suffix_Index := 1;
4837 AI_Tag_Elmt :=
4838 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
4840 AI_Tag_Comp := First_Elmt (Typ_Comps);
4841 while Present (AI_Tag_Comp) loop
4842 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'P'));
4844 -- Build the secondary table containing pointers to thunks
4846 Make_Secondary_DT
4847 (Typ => Typ,
4848 Iface =>
4849 Base_Type (Related_Type (Node (AI_Tag_Comp))),
4850 Iface_Comp => Node (AI_Tag_Comp),
4851 Suffix_Index => Suffix_Index,
4852 Num_Iface_Prims =>
4853 UI_To_Int (DT_Entry_Count (Node (AI_Tag_Comp))),
4854 Iface_DT_Ptr => Node (AI_Tag_Elmt),
4855 Predef_Prims_Ptr => Node (Next_Elmt (AI_Tag_Elmt)),
4856 Build_Thunks => True,
4857 Result => Result);
4859 -- Skip secondary dispatch table referencing thunks to predefined
4860 -- primitives.
4862 Next_Elmt (AI_Tag_Elmt);
4863 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'Y'));
4865 -- Secondary dispatch table referencing user-defined primitives
4866 -- covered by this interface.
4868 Next_Elmt (AI_Tag_Elmt);
4869 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'D'));
4871 -- Build the secondary table containing pointers to primitives
4872 -- (used to give support to Generic Dispatching Constructors).
4874 Make_Secondary_DT
4875 (Typ => Typ,
4876 Iface => Base_Type
4877 (Related_Type (Node (AI_Tag_Comp))),
4878 Iface_Comp => Node (AI_Tag_Comp),
4879 Suffix_Index => -1,
4880 Num_Iface_Prims => UI_To_Int
4881 (DT_Entry_Count (Node (AI_Tag_Comp))),
4882 Iface_DT_Ptr => Node (AI_Tag_Elmt),
4883 Predef_Prims_Ptr => Node (Next_Elmt (AI_Tag_Elmt)),
4884 Build_Thunks => False,
4885 Result => Result);
4887 -- Skip secondary dispatch table referencing predefined primitives
4889 Next_Elmt (AI_Tag_Elmt);
4890 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'Z'));
4892 Suffix_Index := Suffix_Index + 1;
4893 Next_Elmt (AI_Tag_Elmt);
4894 Next_Elmt (AI_Tag_Comp);
4895 end loop;
4896 end if;
4898 -- Get the _tag entity and number of primitives of its dispatch table
4900 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Typ)));
4901 Nb_Prim := UI_To_Int (DT_Entry_Count (First_Tag_Component (Typ)));
4903 if Generate_SCIL then
4904 Nb_Prim := 0;
4905 end if;
4907 Set_Is_Statically_Allocated (DT, Is_Library_Level_Tagged_Type (Typ));
4908 Set_Is_Statically_Allocated (SSD, Is_Library_Level_Tagged_Type (Typ));
4909 Set_Is_Statically_Allocated (TSD, Is_Library_Level_Tagged_Type (Typ));
4910 Set_Is_Statically_Allocated (Predef_Prims,
4911 Is_Library_Level_Tagged_Type (Typ));
4913 -- In case of locally defined tagged type we declare the object
4914 -- containing the dispatch table by means of a variable. Its
4915 -- initialization is done later by means of an assignment. This is
4916 -- required to generate its External_Tag.
4918 if not Building_Static_DT (Typ) then
4920 -- Generate:
4921 -- DT : No_Dispatch_Table_Wrapper;
4922 -- DT_Ptr : Tag := !Tag (DT.NDT_Prims_Ptr'Address);
4924 if not Has_DT (Typ) then
4925 Append_To (Result,
4926 Make_Object_Declaration (Loc,
4927 Defining_Identifier => DT,
4928 Aliased_Present => True,
4929 Constant_Present => False,
4930 Object_Definition =>
4931 New_Occurrence_Of
4932 (RTE (RE_No_Dispatch_Table_Wrapper), Loc)));
4934 Append_To (Result,
4935 Make_Object_Declaration (Loc,
4936 Defining_Identifier => DT_Ptr,
4937 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
4938 Constant_Present => True,
4939 Expression =>
4940 Unchecked_Convert_To (RTE (RE_Tag),
4941 Make_Attribute_Reference (Loc,
4942 Prefix =>
4943 Make_Selected_Component (Loc,
4944 Prefix => New_Occurrence_Of (DT, Loc),
4945 Selector_Name =>
4946 New_Occurrence_Of
4947 (RTE_Record_Component (RE_NDT_Prims_Ptr), Loc)),
4948 Attribute_Name => Name_Address))));
4950 Set_Is_Statically_Allocated (DT_Ptr,
4951 Is_Library_Level_Tagged_Type (Typ));
4953 -- Generate the SCIL node for the previous object declaration
4954 -- because it has a tag initialization.
4956 if Generate_SCIL then
4957 New_Node :=
4958 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
4959 Set_SCIL_Entity (New_Node, Typ);
4960 Set_SCIL_Node (Last (Result), New_Node);
4962 goto Leave_SCIL;
4964 -- Gnat2scil has its own implementation of dispatch tables,
4965 -- different than what is being implemented here. Generating
4966 -- further dispatch table initialization code would just
4967 -- cause gnat2scil to generate useless Scil which CodePeer
4968 -- would waste time and space analyzing, so we skip it.
4969 end if;
4971 -- Generate:
4972 -- DT : Dispatch_Table_Wrapper (Nb_Prim);
4973 -- DT_Ptr : Tag := !Tag (DT.Prims_Ptr'Address);
4975 else
4976 -- If the tagged type has no primitives we add a dummy slot
4977 -- whose address will be the tag of this type.
4979 if Nb_Prim = 0 then
4980 DT_Constr_List :=
4981 New_List (Make_Integer_Literal (Loc, 1));
4982 else
4983 DT_Constr_List :=
4984 New_List (Make_Integer_Literal (Loc, Nb_Prim));
4985 end if;
4987 Append_To (Result,
4988 Make_Object_Declaration (Loc,
4989 Defining_Identifier => DT,
4990 Aliased_Present => True,
4991 Constant_Present => False,
4992 Object_Definition =>
4993 Make_Subtype_Indication (Loc,
4994 Subtype_Mark =>
4995 New_Occurrence_Of (RTE (RE_Dispatch_Table_Wrapper), Loc),
4996 Constraint =>
4997 Make_Index_Or_Discriminant_Constraint (Loc,
4998 Constraints => DT_Constr_List))));
5000 Append_To (Result,
5001 Make_Object_Declaration (Loc,
5002 Defining_Identifier => DT_Ptr,
5003 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
5004 Constant_Present => True,
5005 Expression =>
5006 Unchecked_Convert_To (RTE (RE_Tag),
5007 Make_Attribute_Reference (Loc,
5008 Prefix =>
5009 Make_Selected_Component (Loc,
5010 Prefix => New_Occurrence_Of (DT, Loc),
5011 Selector_Name =>
5012 New_Occurrence_Of
5013 (RTE_Record_Component (RE_Prims_Ptr), Loc)),
5014 Attribute_Name => Name_Address))));
5016 Set_Is_Statically_Allocated (DT_Ptr,
5017 Is_Library_Level_Tagged_Type (Typ));
5019 -- Generate the SCIL node for the previous object declaration
5020 -- because it has a tag initialization.
5022 if Generate_SCIL then
5023 New_Node :=
5024 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
5025 Set_SCIL_Entity (New_Node, Typ);
5026 Set_SCIL_Node (Last (Result), New_Node);
5028 goto Leave_SCIL;
5030 -- Gnat2scil has its own implementation of dispatch tables,
5031 -- different than what is being implemented here. Generating
5032 -- further dispatch table initialization code would just
5033 -- cause gnat2scil to generate useless Scil which CodePeer
5034 -- would waste time and space analyzing, so we skip it.
5035 end if;
5037 Append_To (Result,
5038 Make_Object_Declaration (Loc,
5039 Defining_Identifier =>
5040 Node (Next_Elmt (First_Elmt (Access_Disp_Table (Typ)))),
5041 Constant_Present => True,
5042 Object_Definition =>
5043 New_Occurrence_Of (RTE (RE_Address), Loc),
5044 Expression =>
5045 Make_Attribute_Reference (Loc,
5046 Prefix =>
5047 Make_Selected_Component (Loc,
5048 Prefix => New_Occurrence_Of (DT, Loc),
5049 Selector_Name =>
5050 New_Occurrence_Of
5051 (RTE_Record_Component (RE_Predef_Prims), Loc)),
5052 Attribute_Name => Name_Address)));
5053 end if;
5054 end if;
5056 -- Generate: Expanded_Name : constant String := "";
5058 if Discard_Names then
5059 Append_To (Result,
5060 Make_Object_Declaration (Loc,
5061 Defining_Identifier => Exname,
5062 Constant_Present => True,
5063 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
5064 Expression =>
5065 Make_String_Literal (Loc, "")));
5067 -- Generate: Exname : constant String := full_qualified_name (typ);
5068 -- The type itself may be an anonymous parent type, so use the first
5069 -- subtype to have a user-recognizable name.
5071 else
5072 Append_To (Result,
5073 Make_Object_Declaration (Loc,
5074 Defining_Identifier => Exname,
5075 Constant_Present => True,
5076 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
5077 Expression =>
5078 Make_String_Literal (Loc,
5079 Fully_Qualified_Name_String (First_Subtype (Typ)))));
5080 end if;
5082 Set_Is_Statically_Allocated (Exname);
5083 Set_Is_True_Constant (Exname);
5085 -- Declare the object used by Ada.Tags.Register_Tag, unless
5086 -- No_Tagged_Type_Registration is active.
5088 if not Restriction_Active (No_Tagged_Type_Registration)
5089 and then RTE_Available (RE_Register_Tag)
5090 then
5091 Append_To (Result,
5092 Make_Object_Declaration (Loc,
5093 Defining_Identifier => HT_Link,
5094 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
5095 Expression => New_Occurrence_Of (RTE (RE_No_Tag), Loc)));
5096 end if;
5098 -- Generate code to create the storage for the type specific data object
5099 -- with enough space to store the tags of the ancestors plus the tags
5100 -- of all the implemented interfaces (as described in a-tags.adb).
5102 -- TSD : Type_Specific_Data (I_Depth) :=
5103 -- (Idepth => I_Depth,
5104 -- Access_Level => Type_Access_Level (Typ),
5105 -- Alignment => Typ'Alignment,
5106 -- Expanded_Name => Cstring_Ptr!(Exname'Address))
5107 -- External_Tag => Cstring_Ptr!(Exname'Address))
5108 -- HT_Link => HT_Link'Address,
5109 -- Transportable => <<boolean-value>>,
5110 -- Is_Abstract => <<boolean-value>>,
5111 -- Needs_Finalization => <<boolean-value>>,
5112 -- [ Size_Func => Size_Prim'Access, ]
5113 -- [ Interfaces_Table => <<access-value>>, ]
5114 -- [ SSD => SSD_Table'Address ]
5115 -- Tags_Table => (0 => null,
5116 -- 1 => Parent'Tag
5117 -- ...);
5119 TSD_Aggr_List := New_List;
5121 -- Idepth: Count ancestors to compute the inheritance depth. For private
5122 -- extensions, always go to the full view in order to compute the real
5123 -- inheritance depth.
5125 declare
5126 Current_Typ : Entity_Id;
5127 Parent_Typ : Entity_Id;
5129 begin
5130 I_Depth := 0;
5131 Current_Typ := Typ;
5132 loop
5133 Parent_Typ := Etype (Current_Typ);
5135 if Is_Private_Type (Parent_Typ) then
5136 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5137 end if;
5139 exit when Parent_Typ = Current_Typ;
5141 I_Depth := I_Depth + 1;
5142 Current_Typ := Parent_Typ;
5143 end loop;
5144 end;
5146 Append_To (TSD_Aggr_List,
5147 Make_Integer_Literal (Loc, I_Depth));
5149 -- Access_Level
5151 Append_To (TSD_Aggr_List,
5152 Make_Integer_Literal (Loc, Type_Access_Level (Typ)));
5154 -- Alignment
5156 -- For CPP types we cannot rely on the value of 'Alignment provided
5157 -- by the backend to initialize this TSD field.
5159 if Convention (Typ) = Convention_CPP
5160 or else Is_CPP_Class (Root_Type (Typ))
5161 then
5162 Append_To (TSD_Aggr_List,
5163 Make_Integer_Literal (Loc, 0));
5164 else
5165 Append_To (TSD_Aggr_List,
5166 Make_Attribute_Reference (Loc,
5167 Prefix => New_Occurrence_Of (Typ, Loc),
5168 Attribute_Name => Name_Alignment));
5169 end if;
5171 -- Expanded_Name
5173 Append_To (TSD_Aggr_List,
5174 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5175 Make_Attribute_Reference (Loc,
5176 Prefix => New_Occurrence_Of (Exname, Loc),
5177 Attribute_Name => Name_Address)));
5179 -- External_Tag of a local tagged type
5181 -- <typ>A : constant String :=
5182 -- "Internal tag at 16#tag-addr#: <full-name-of-typ>";
5184 -- The reason we generate this strange name is that we do not want to
5185 -- enter local tagged types in the global hash table used to compute
5186 -- the Internal_Tag attribute for two reasons:
5188 -- 1. It is hard to avoid a tasking race condition for entering the
5189 -- entry into the hash table.
5191 -- 2. It would cause a storage leak, unless we rig up considerable
5192 -- mechanism to remove the entry from the hash table on exit.
5194 -- So what we do is to generate the above external tag name, where the
5195 -- hex address is the address of the local dispatch table (i.e. exactly
5196 -- the value we want if Internal_Tag is computed from this string).
5198 -- Of course this value will only be valid if the tagged type is still
5199 -- in scope, but it clearly must be erroneous to compute the internal
5200 -- tag of a tagged type that is out of scope.
5202 -- We don't do this processing if an explicit external tag has been
5203 -- specified. That's an odd case for which we have already issued a
5204 -- warning, where we will not be able to compute the internal tag.
5206 if not Discard_Names
5207 and then not Is_Library_Level_Entity (Typ)
5208 and then not Has_External_Tag_Rep_Clause (Typ)
5209 then
5210 declare
5211 Exname : constant Entity_Id :=
5212 Make_Defining_Identifier (Loc,
5213 Chars => New_External_Name (Tname, 'A'));
5214 Full_Name : constant String_Id :=
5215 Fully_Qualified_Name_String (First_Subtype (Typ));
5216 Str1_Id : String_Id;
5217 Str2_Id : String_Id;
5219 begin
5220 -- Generate:
5221 -- Str1 = "Internal tag at 16#";
5223 Start_String;
5224 Store_String_Chars ("Internal tag at 16#");
5225 Str1_Id := End_String;
5227 -- Generate:
5228 -- Str2 = "#: <type-full-name>";
5230 Start_String;
5231 Store_String_Chars ("#: ");
5232 Store_String_Chars (Full_Name);
5233 Str2_Id := End_String;
5235 -- Generate:
5236 -- Exname : constant String :=
5237 -- Str1 & Address_Image (Tag) & Str2;
5239 if RTE_Available (RE_Address_Image) then
5240 Append_To (Result,
5241 Make_Object_Declaration (Loc,
5242 Defining_Identifier => Exname,
5243 Constant_Present => True,
5244 Object_Definition => New_Occurrence_Of
5245 (Standard_String, Loc),
5246 Expression =>
5247 Make_Op_Concat (Loc,
5248 Left_Opnd => Make_String_Literal (Loc, Str1_Id),
5249 Right_Opnd =>
5250 Make_Op_Concat (Loc,
5251 Left_Opnd =>
5252 Make_Function_Call (Loc,
5253 Name =>
5254 New_Occurrence_Of
5255 (RTE (RE_Address_Image), Loc),
5256 Parameter_Associations => New_List (
5257 Unchecked_Convert_To (RTE (RE_Address),
5258 New_Occurrence_Of (DT_Ptr, Loc)))),
5259 Right_Opnd =>
5260 Make_String_Literal (Loc, Str2_Id)))));
5262 -- Generate:
5263 -- Exname : constant String := Str1 & Str2;
5265 else
5266 Append_To (Result,
5267 Make_Object_Declaration (Loc,
5268 Defining_Identifier => Exname,
5269 Constant_Present => True,
5270 Object_Definition =>
5271 New_Occurrence_Of (Standard_String, Loc),
5272 Expression =>
5273 Make_Op_Concat (Loc,
5274 Left_Opnd => Make_String_Literal (Loc, Str1_Id),
5275 Right_Opnd => Make_String_Literal (Loc, Str2_Id))));
5276 end if;
5278 New_Node :=
5279 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5280 Make_Attribute_Reference (Loc,
5281 Prefix => New_Occurrence_Of (Exname, Loc),
5282 Attribute_Name => Name_Address));
5283 end;
5285 -- External tag of a library-level tagged type: Check for a definition
5286 -- of External_Tag. The clause is considered only if it applies to this
5287 -- specific tagged type, as opposed to one of its ancestors.
5288 -- If the type is an unconstrained type extension, we are building the
5289 -- dispatch table of its anonymous base type, so the external tag, if
5290 -- any was specified, must be retrieved from the first subtype. Go to
5291 -- the full view in case the clause is in the private part.
5293 else
5294 declare
5295 Def : constant Node_Id := Get_Attribute_Definition_Clause
5296 (Underlying_Type (First_Subtype (Typ)),
5297 Attribute_External_Tag);
5299 Old_Val : String_Id;
5300 New_Val : String_Id;
5301 E : Entity_Id;
5303 begin
5304 if No (Def)
5305 or else Entity (Name (Def)) /= First_Subtype (Typ)
5306 then
5307 New_Node :=
5308 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5309 Make_Attribute_Reference (Loc,
5310 Prefix => New_Occurrence_Of (Exname, Loc),
5311 Attribute_Name => Name_Address));
5312 else
5313 Old_Val := Strval (Expr_Value_S (Expression (Def)));
5315 -- For the rep clause "for <typ>'external_tag use y" generate:
5317 -- <typ>A : constant string := y;
5319 -- <typ>A'Address is used to set the External_Tag component
5320 -- of the TSD
5322 -- Create a new nul terminated string if it is not already
5324 if String_Length (Old_Val) > 0
5325 and then
5326 Get_String_Char (Old_Val, String_Length (Old_Val)) = 0
5327 then
5328 New_Val := Old_Val;
5329 else
5330 Start_String (Old_Val);
5331 Store_String_Char (Get_Char_Code (ASCII.NUL));
5332 New_Val := End_String;
5333 end if;
5335 E := Make_Defining_Identifier (Loc,
5336 New_External_Name (Chars (Typ), 'A'));
5338 Append_To (Result,
5339 Make_Object_Declaration (Loc,
5340 Defining_Identifier => E,
5341 Constant_Present => True,
5342 Object_Definition =>
5343 New_Occurrence_Of (Standard_String, Loc),
5344 Expression =>
5345 Make_String_Literal (Loc, New_Val)));
5347 New_Node :=
5348 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5349 Make_Attribute_Reference (Loc,
5350 Prefix => New_Occurrence_Of (E, Loc),
5351 Attribute_Name => Name_Address));
5352 end if;
5353 end;
5354 end if;
5356 Append_To (TSD_Aggr_List, New_Node);
5358 -- HT_Link
5360 if not Restriction_Active (No_Tagged_Type_Registration)
5361 and then RTE_Available (RE_Register_Tag)
5362 then
5363 Append_To (TSD_Aggr_List,
5364 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5365 Make_Attribute_Reference (Loc,
5366 Prefix => New_Occurrence_Of (HT_Link, Loc),
5367 Attribute_Name => Name_Address)));
5369 elsif RTE_Record_Component_Available (RE_HT_Link) then
5370 Append_To (TSD_Aggr_List,
5371 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5372 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5373 end if;
5375 -- Transportable: Set for types that can be used in remote calls
5376 -- with respect to E.4(18) legality rules.
5378 declare
5379 Transportable : Entity_Id;
5381 begin
5382 Transportable :=
5383 Boolean_Literals
5384 (Is_Pure (Typ)
5385 or else Is_Shared_Passive (Typ)
5386 or else
5387 ((Is_Remote_Types (Typ)
5388 or else Is_Remote_Call_Interface (Typ))
5389 and then Original_View_In_Visible_Part (Typ))
5390 or else not Comes_From_Source (Typ));
5392 Append_To (TSD_Aggr_List,
5393 New_Occurrence_Of (Transportable, Loc));
5394 end;
5396 -- Is_Abstract (Ada 2012: AI05-0173). This functionality is not
5397 -- available in the HIE runtime.
5399 if RTE_Record_Component_Available (RE_Is_Abstract) then
5400 declare
5401 Is_Abstract : Entity_Id;
5402 begin
5403 Is_Abstract := Boolean_Literals (Is_Abstract_Type (Typ));
5404 Append_To (TSD_Aggr_List,
5405 New_Occurrence_Of (Is_Abstract, Loc));
5406 end;
5407 end if;
5409 -- Needs_Finalization: Set if the type is controlled or has controlled
5410 -- components.
5412 declare
5413 Needs_Fin : Entity_Id;
5414 begin
5415 Needs_Fin := Boolean_Literals (Needs_Finalization (Typ));
5416 Append_To (TSD_Aggr_List, New_Occurrence_Of (Needs_Fin, Loc));
5417 end;
5419 -- Size_Func
5421 if RTE_Record_Component_Available (RE_Size_Func) then
5423 -- Initialize this field to Null_Address if we are not building
5424 -- static dispatch tables static or if the size function is not
5425 -- available. In the former case we cannot initialize this field
5426 -- until the function is frozen and registered in the dispatch
5427 -- table (see Register_Primitive).
5429 if not Building_Static_DT (Typ) or else not Has_DT (Typ) then
5430 Append_To (TSD_Aggr_List,
5431 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5432 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5434 else
5435 declare
5436 Prim_Elmt : Elmt_Id;
5437 Prim : Entity_Id;
5438 Size_Comp : Node_Id := Empty;
5440 begin
5441 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5442 while Present (Prim_Elmt) loop
5443 Prim := Node (Prim_Elmt);
5445 if Chars (Prim) = Name_uSize then
5446 Prim := Ultimate_Alias (Prim);
5448 if Is_Abstract_Subprogram (Prim) then
5449 Size_Comp :=
5450 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5451 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
5452 else
5453 Size_Comp :=
5454 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5455 Make_Attribute_Reference (Loc,
5456 Prefix => New_Occurrence_Of (Prim, Loc),
5457 Attribute_Name => Name_Unrestricted_Access));
5458 end if;
5460 exit;
5461 end if;
5463 Next_Elmt (Prim_Elmt);
5464 end loop;
5466 pragma Assert (Present (Size_Comp));
5467 Append_To (TSD_Aggr_List, Size_Comp);
5468 end;
5469 end if;
5470 end if;
5472 -- Interfaces_Table (required for AI-405)
5474 if RTE_Record_Component_Available (RE_Interfaces_Table) then
5476 -- Count the number of interface types implemented by Typ
5478 Collect_Interfaces (Typ, Typ_Ifaces);
5480 AI := First_Elmt (Typ_Ifaces);
5481 while Present (AI) loop
5482 Num_Ifaces := Num_Ifaces + 1;
5483 Next_Elmt (AI);
5484 end loop;
5486 if Num_Ifaces = 0 then
5487 Iface_Table_Node := Make_Null (Loc);
5489 -- Generate the Interface_Table object
5491 else
5492 declare
5493 TSD_Ifaces_List : constant List_Id := New_List;
5494 Elmt : Elmt_Id;
5495 Offset_To_Top : Node_Id;
5496 Sec_DT_Tag : Node_Id;
5498 Dummy_Object_Ifaces_List : Elist_Id := No_Elist;
5499 Dummy_Object_Ifaces_Comp_List : Elist_Id := No_Elist;
5500 Dummy_Object_Ifaces_Tag_List : Elist_Id := No_Elist;
5501 -- Interfaces information of the dummy object
5503 begin
5504 -- Collect interfaces information if we need to compute the
5505 -- offset to the top using the dummy object.
5507 if Present (Dummy_Object) then
5508 Collect_Interfaces_Info (Typ,
5509 Ifaces_List => Dummy_Object_Ifaces_List,
5510 Components_List => Dummy_Object_Ifaces_Comp_List,
5511 Tags_List => Dummy_Object_Ifaces_Tag_List);
5512 end if;
5514 AI := First_Elmt (Typ_Ifaces);
5515 while Present (AI) loop
5516 if Is_Ancestor (Node (AI), Typ, Use_Full_View => True) then
5517 Sec_DT_Tag := New_Occurrence_Of (DT_Ptr, Loc);
5519 else
5520 Elmt :=
5521 Next_Elmt
5522 (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5523 pragma Assert (Has_Thunks (Node (Elmt)));
5525 while Is_Tag (Node (Elmt))
5526 and then not
5527 Is_Ancestor (Node (AI), Related_Type (Node (Elmt)),
5528 Use_Full_View => True)
5529 loop
5530 pragma Assert (Has_Thunks (Node (Elmt)));
5531 Next_Elmt (Elmt);
5532 pragma Assert (Has_Thunks (Node (Elmt)));
5533 Next_Elmt (Elmt);
5534 pragma Assert (not Has_Thunks (Node (Elmt)));
5535 Next_Elmt (Elmt);
5536 pragma Assert (not Has_Thunks (Node (Elmt)));
5537 Next_Elmt (Elmt);
5538 end loop;
5540 pragma Assert (Ekind (Node (Elmt)) = E_Constant
5541 and then not
5542 Has_Thunks (Node (Next_Elmt (Next_Elmt (Elmt)))));
5544 Sec_DT_Tag :=
5545 New_Occurrence_Of
5546 (Node (Next_Elmt (Next_Elmt (Elmt))), Loc);
5547 end if;
5549 -- Use the dummy object to compute Offset_To_Top of
5550 -- components located at fixed position.
5552 if Present (Dummy_Object) then
5553 declare
5554 Iface : constant Node_Id := Node (AI);
5555 Iface_Comp : Node_Id := Empty;
5556 Iface_Comp_Elmt : Elmt_Id;
5557 Iface_Elmt : Elmt_Id;
5559 begin
5560 Iface_Elmt :=
5561 First_Elmt (Dummy_Object_Ifaces_List);
5562 Iface_Comp_Elmt :=
5563 First_Elmt (Dummy_Object_Ifaces_Comp_List);
5565 while Present (Iface_Elmt) loop
5566 if Node (Iface_Elmt) = Iface then
5567 Iface_Comp := Node (Iface_Comp_Elmt);
5568 exit;
5569 end if;
5571 Next_Elmt (Iface_Elmt);
5572 Next_Elmt (Iface_Comp_Elmt);
5573 end loop;
5575 pragma Assert (Present (Iface_Comp));
5577 if not
5578 Is_Variable_Size_Record (Etype (Scope (Iface_Comp)))
5579 then
5580 Offset_To_Top :=
5581 Make_Op_Minus (Loc,
5582 Make_Attribute_Reference (Loc,
5583 Prefix =>
5584 Make_Selected_Component (Loc,
5585 Prefix =>
5586 New_Occurrence_Of (Dummy_Object, Loc),
5587 Selector_Name =>
5588 New_Occurrence_Of (Iface_Comp, Loc)),
5589 Attribute_Name => Name_Position));
5590 else
5591 Offset_To_Top := Make_Integer_Literal (Loc, 0);
5592 end if;
5593 end;
5594 else
5595 Offset_To_Top := Make_Integer_Literal (Loc, 0);
5596 end if;
5598 Append_To (TSD_Ifaces_List,
5599 Make_Aggregate (Loc,
5600 Expressions => New_List (
5602 -- Iface_Tag
5604 Unchecked_Convert_To (RTE (RE_Tag),
5605 New_Occurrence_Of
5606 (Node (First_Elmt (Access_Disp_Table (Node (AI)))),
5607 Loc)),
5609 -- Static_Offset_To_Top
5611 New_Occurrence_Of (Standard_True, Loc),
5613 -- Offset_To_Top_Value
5615 Offset_To_Top,
5617 -- Offset_To_Top_Func
5619 Make_Null (Loc),
5621 -- Secondary_DT
5623 Unchecked_Convert_To (RTE (RE_Tag), Sec_DT_Tag))));
5625 Next_Elmt (AI);
5626 end loop;
5628 Name_ITable := New_External_Name (Tname, 'I');
5629 ITable := Make_Defining_Identifier (Loc, Name_ITable);
5630 Set_Is_Statically_Allocated (ITable,
5631 Is_Library_Level_Tagged_Type (Typ));
5633 -- The table of interfaces is constant if we are building a
5634 -- static dispatch table; otherwise is not constant because
5635 -- its slots are filled at run time by the IP routine.
5637 Append_To (Result,
5638 Make_Object_Declaration (Loc,
5639 Defining_Identifier => ITable,
5640 Aliased_Present => True,
5641 Constant_Present => Building_Static_Secondary_DT (Typ),
5642 Object_Definition =>
5643 Make_Subtype_Indication (Loc,
5644 Subtype_Mark =>
5645 New_Occurrence_Of (RTE (RE_Interface_Data), Loc),
5646 Constraint =>
5647 Make_Index_Or_Discriminant_Constraint (Loc,
5648 Constraints => New_List (
5649 Make_Integer_Literal (Loc, Num_Ifaces)))),
5651 Expression =>
5652 Make_Aggregate (Loc,
5653 Expressions => New_List (
5654 Make_Integer_Literal (Loc, Num_Ifaces),
5655 Make_Aggregate (Loc, TSD_Ifaces_List)))));
5657 Iface_Table_Node :=
5658 Make_Attribute_Reference (Loc,
5659 Prefix => New_Occurrence_Of (ITable, Loc),
5660 Attribute_Name => Name_Unchecked_Access);
5661 end;
5662 end if;
5664 Append_To (TSD_Aggr_List, Iface_Table_Node);
5665 end if;
5667 -- Generate the Select Specific Data table for synchronized types that
5668 -- implement synchronized interfaces. The size of the table is
5669 -- constrained by the number of non-predefined primitive operations.
5671 if RTE_Record_Component_Available (RE_SSD) then
5672 if Ada_Version >= Ada_2005
5673 and then Has_DT (Typ)
5674 and then Is_Concurrent_Record_Type (Typ)
5675 and then Has_Interfaces (Typ)
5676 and then Nb_Prim > 0
5677 and then not Is_Abstract_Type (Typ)
5678 and then not Is_Controlled (Typ)
5679 and then not Restriction_Active (No_Dispatching_Calls)
5680 and then not Restriction_Active (No_Select_Statements)
5681 then
5682 Append_To (Result,
5683 Make_Object_Declaration (Loc,
5684 Defining_Identifier => SSD,
5685 Aliased_Present => True,
5686 Object_Definition =>
5687 Make_Subtype_Indication (Loc,
5688 Subtype_Mark => New_Occurrence_Of (
5689 RTE (RE_Select_Specific_Data), Loc),
5690 Constraint =>
5691 Make_Index_Or_Discriminant_Constraint (Loc,
5692 Constraints => New_List (
5693 Make_Integer_Literal (Loc, Nb_Prim))))));
5695 Append_To (Result,
5696 Make_Attribute_Definition_Clause (Loc,
5697 Name => New_Occurrence_Of (SSD, Loc),
5698 Chars => Name_Alignment,
5699 Expression =>
5700 Make_Attribute_Reference (Loc,
5701 Prefix =>
5702 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
5703 Attribute_Name => Name_Alignment)));
5705 -- This table is initialized by Make_Select_Specific_Data_Table,
5706 -- which calls Set_Entry_Index and Set_Prim_Op_Kind.
5708 Append_To (TSD_Aggr_List,
5709 Make_Attribute_Reference (Loc,
5710 Prefix => New_Occurrence_Of (SSD, Loc),
5711 Attribute_Name => Name_Unchecked_Access));
5712 else
5713 Append_To (TSD_Aggr_List, Make_Null (Loc));
5714 end if;
5715 end if;
5717 -- Initialize the table of ancestor tags. In case of interface types
5718 -- this table is not needed.
5720 TSD_Tags_List := New_List;
5722 -- If we are not statically allocating the dispatch table then we must
5723 -- fill position 0 with null because we still have not generated the
5724 -- tag of Typ.
5726 if not Building_Static_DT (Typ)
5727 or else Is_Interface (Typ)
5728 then
5729 Append_To (TSD_Tags_List,
5730 Unchecked_Convert_To (RTE (RE_Tag),
5731 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5733 -- Otherwise we can safely reference the tag
5735 else
5736 Append_To (TSD_Tags_List,
5737 New_Occurrence_Of (DT_Ptr, Loc));
5738 end if;
5740 -- Fill the rest of the table with the tags of the ancestors
5742 declare
5743 Current_Typ : Entity_Id;
5744 Parent_Typ : Entity_Id;
5745 Pos : Nat;
5747 begin
5748 Pos := 1;
5749 Current_Typ := Typ;
5751 loop
5752 Parent_Typ := Etype (Current_Typ);
5754 if Is_Private_Type (Parent_Typ) then
5755 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5756 end if;
5758 exit when Parent_Typ = Current_Typ;
5760 if Is_CPP_Class (Parent_Typ) then
5762 -- The tags defined in the C++ side will be inherited when
5763 -- the object is constructed (Exp_Ch3.Build_Init_Procedure)
5765 Append_To (TSD_Tags_List,
5766 Unchecked_Convert_To (RTE (RE_Tag),
5767 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5768 else
5769 Append_To (TSD_Tags_List,
5770 New_Occurrence_Of
5771 (Node (First_Elmt (Access_Disp_Table (Parent_Typ))),
5772 Loc));
5773 end if;
5775 Pos := Pos + 1;
5776 Current_Typ := Parent_Typ;
5777 end loop;
5779 pragma Assert (Pos = I_Depth + 1);
5780 end;
5782 Append_To (TSD_Aggr_List,
5783 Make_Aggregate (Loc,
5784 Expressions => TSD_Tags_List));
5786 -- Build the TSD object
5788 Append_To (Result,
5789 Make_Object_Declaration (Loc,
5790 Defining_Identifier => TSD,
5791 Aliased_Present => True,
5792 Constant_Present => Building_Static_DT (Typ),
5793 Object_Definition =>
5794 Make_Subtype_Indication (Loc,
5795 Subtype_Mark => New_Occurrence_Of (
5796 RTE (RE_Type_Specific_Data), Loc),
5797 Constraint =>
5798 Make_Index_Or_Discriminant_Constraint (Loc,
5799 Constraints => New_List (
5800 Make_Integer_Literal (Loc, I_Depth)))),
5802 Expression => Make_Aggregate (Loc,
5803 Expressions => TSD_Aggr_List)));
5805 Set_Is_True_Constant (TSD, Building_Static_DT (Typ));
5807 -- The debugging information for type Ada.Tags.Type_Specific_Data is
5808 -- needed by the debugger in order to display values of tagged types.
5810 Set_Needs_Debug_Info (TSD, Needs_Debug_Info (Typ));
5812 -- Initialize or declare the dispatch table object
5814 if not Has_DT (Typ) then
5815 DT_Constr_List := New_List;
5816 DT_Aggr_List := New_List;
5818 -- Typeinfo
5820 New_Node :=
5821 Make_Attribute_Reference (Loc,
5822 Prefix => New_Occurrence_Of (TSD, Loc),
5823 Attribute_Name => Name_Address);
5825 Append_To (DT_Constr_List, New_Node);
5826 Append_To (DT_Aggr_List, New_Copy (New_Node));
5827 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, 0));
5829 -- In case of locally defined tagged types we have already declared
5830 -- and uninitialized object for the dispatch table, which is now
5831 -- initialized by means of the following assignment:
5833 -- DT := (TSD'Address, 0);
5835 if not Building_Static_DT (Typ) then
5836 Append_To (Result,
5837 Make_Assignment_Statement (Loc,
5838 Name => New_Occurrence_Of (DT, Loc),
5839 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
5841 -- In case of library level tagged types we declare and export now
5842 -- the constant object containing the dummy dispatch table. There
5843 -- is no need to declare the tag here because it has been previously
5844 -- declared by Make_Tags
5846 -- DT : aliased constant No_Dispatch_Table :=
5847 -- (NDT_TSD => TSD'Address;
5848 -- NDT_Prims_Ptr => 0);
5850 else
5851 Append_To (Result,
5852 Make_Object_Declaration (Loc,
5853 Defining_Identifier => DT,
5854 Aliased_Present => True,
5855 Constant_Present => True,
5856 Object_Definition =>
5857 New_Occurrence_Of (RTE (RE_No_Dispatch_Table_Wrapper), Loc),
5858 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
5860 Export_DT (Typ, DT);
5861 end if;
5863 -- Common case: Typ has a dispatch table
5865 -- Generate:
5867 -- Predef_Prims : Address_Array (1 .. Default_Prim_Ops_Count) :=
5868 -- (predef-prim-op-1'address,
5869 -- predef-prim-op-2'address,
5870 -- ...
5871 -- predef-prim-op-n'address);
5873 -- DT : Dispatch_Table (Nb_Prims) :=
5874 -- (Signature => <sig-value>,
5875 -- Tag_Kind => <tag_kind-value>,
5876 -- Predef_Prims => Predef_Prims'First'Address,
5877 -- Offset_To_Top => 0,
5878 -- TSD => TSD'Address;
5879 -- Prims_Ptr => (prim-op-1'address,
5880 -- prim-op-2'address,
5881 -- ...
5882 -- prim-op-n'address));
5883 -- for DT'Alignment use Address'Alignment
5885 else
5886 declare
5887 Nb_P_Prims : constant Nat := Number_Of_Predefined_Prims (Typ);
5888 Prim_Table : array (Nat range 1 .. Nb_P_Prims) of Entity_Id;
5889 Decl : Node_Id;
5890 E : Entity_Id;
5891 SS_Thunk_Id : Entity_Id;
5892 SS_Thunk_Code : Node_Id;
5894 begin
5895 Prim_Ops_Aggr_List := New_List;
5896 Prim_Table := (others => Empty);
5898 if Building_Static_DT (Typ) then
5899 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5900 while Present (Prim_Elmt) loop
5901 Prim := Node (Prim_Elmt);
5903 if Is_Predefined_Dispatching_Operation (Prim)
5904 and then not Is_Abstract_Subprogram (Prim)
5905 and then not Is_Eliminated (Prim)
5906 and then not Generate_SCIL
5907 and then No (Prim_Table (UI_To_Int (DT_Position (Prim))))
5908 then
5909 E := Ultimate_Alias (Prim);
5910 pragma Assert (not Is_Abstract_Subprogram (E));
5912 Expand_Secondary_Stack_Thunk
5913 (E, SS_Thunk_Id, SS_Thunk_Code);
5915 if Present (SS_Thunk_Id) then
5916 E := SS_Thunk_Id;
5917 Append_To (Result, SS_Thunk_Code);
5918 end if;
5920 Prim_Table (UI_To_Int (DT_Position (Prim))) := E;
5921 end if;
5923 Next_Elmt (Prim_Elmt);
5924 end loop;
5925 end if;
5927 for J in Prim_Table'Range loop
5928 if Present (Prim_Table (J)) then
5929 New_Node :=
5930 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
5931 Make_Attribute_Reference (Loc,
5932 Prefix =>
5933 New_Occurrence_Of (Prim_Table (J), Loc),
5934 Attribute_Name => Name_Unrestricted_Access));
5935 else
5936 New_Node := Make_Null (Loc);
5937 end if;
5939 Append_To (Prim_Ops_Aggr_List, New_Node);
5940 end loop;
5942 New_Node :=
5943 Make_Aggregate (Loc,
5944 Expressions => Prim_Ops_Aggr_List);
5946 Decl :=
5947 Make_Subtype_Declaration (Loc,
5948 Defining_Identifier => Make_Temporary (Loc, 'S'),
5949 Subtype_Indication =>
5950 New_Occurrence_Of (RTE (RE_Address_Array), Loc));
5952 Append_To (Result, Decl);
5954 Append_To (Result,
5955 Make_Object_Declaration (Loc,
5956 Defining_Identifier => Predef_Prims,
5957 Aliased_Present => True,
5958 Constant_Present => Building_Static_DT (Typ),
5959 Object_Definition =>
5960 New_Occurrence_Of (Defining_Identifier (Decl), Loc),
5961 Expression => New_Node));
5963 -- Remember aggregates initializing dispatch tables
5965 Append_Elmt (New_Node, DT_Aggr);
5966 end;
5968 -- Stage 1: Initialize the discriminant and the record components
5970 DT_Constr_List := New_List;
5971 DT_Aggr_List := New_List;
5973 -- Num_Prims. If the tagged type has no primitives we add a dummy
5974 -- slot whose address will be the tag of this type.
5976 if Nb_Prim = 0 then
5977 New_Node := Make_Integer_Literal (Loc, 1);
5978 else
5979 New_Node := Make_Integer_Literal (Loc, Nb_Prim);
5980 end if;
5982 Append_To (DT_Constr_List, New_Node);
5983 Append_To (DT_Aggr_List, New_Copy (New_Node));
5985 -- Signature
5987 if RTE_Record_Component_Available (RE_Signature) then
5988 Append_To (DT_Aggr_List,
5989 New_Occurrence_Of (RTE (RE_Primary_DT), Loc));
5990 end if;
5992 -- Tag_Kind
5994 if RTE_Record_Component_Available (RE_Tag_Kind) then
5995 Append_To (DT_Aggr_List, Tagged_Kind (Typ));
5996 end if;
5998 -- Predef_Prims
6000 Append_To (DT_Aggr_List,
6001 Make_Attribute_Reference (Loc,
6002 Prefix => New_Occurrence_Of (Predef_Prims, Loc),
6003 Attribute_Name => Name_Address));
6005 -- Offset_To_Top
6007 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, 0));
6009 -- Typeinfo
6011 Append_To (DT_Aggr_List,
6012 Make_Attribute_Reference (Loc,
6013 Prefix => New_Occurrence_Of (TSD, Loc),
6014 Attribute_Name => Name_Address));
6016 -- Stage 2: Initialize the table of user-defined primitive operations
6018 Prim_Ops_Aggr_List := New_List;
6020 if Nb_Prim = 0 then
6021 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
6023 elsif not Building_Static_DT (Typ) then
6024 for J in 1 .. Nb_Prim loop
6025 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
6026 end loop;
6028 else
6029 declare
6030 CPP_Nb_Prims : constant Nat := CPP_Num_Prims (Typ);
6031 E : Entity_Id;
6032 Prim : Entity_Id;
6033 Prim_Elmt : Elmt_Id;
6034 Prim_Pos : Nat;
6035 Prim_Table : array (Nat range 1 .. Nb_Prim) of Entity_Id;
6036 SS_Thunk_Id : Entity_Id;
6037 SS_Thunk_Code : Node_Id;
6039 begin
6040 Prim_Table := (others => Empty);
6042 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6043 while Present (Prim_Elmt) loop
6044 Prim := Node (Prim_Elmt);
6046 -- Retrieve the ultimate alias of the primitive for proper
6047 -- handling of renamings and eliminated primitives.
6049 E := Ultimate_Alias (Prim);
6051 -- If the alias is not a primitive operation then Prim does
6052 -- not rename another primitive, but rather an operation
6053 -- declared elsewhere (e.g. in another scope) and therefore
6054 -- Prim is a new primitive.
6056 if No (Find_Dispatching_Type (E)) then
6057 E := Prim;
6058 end if;
6060 Prim_Pos := UI_To_Int (DT_Position (E));
6062 -- Skip predefined primitives because they are located in a
6063 -- separate dispatch table.
6065 if not Is_Predefined_Dispatching_Operation (Prim)
6066 and then not Is_Predefined_Dispatching_Operation (E)
6068 -- Skip entities with attribute Interface_Alias because
6069 -- those are only required to build secondary dispatch
6070 -- tables.
6072 and then No (Interface_Alias (Prim))
6074 -- Skip abstract and eliminated primitives
6076 and then not Is_Abstract_Subprogram (E)
6077 and then not Is_Eliminated (E)
6079 -- For derivations of CPP types skip primitives located in
6080 -- the C++ part of the dispatch table because their slots
6081 -- are initialized by the IC routine.
6083 and then (not Is_CPP_Class (Root_Type (Typ))
6084 or else Prim_Pos > CPP_Nb_Prims)
6086 -- Skip ignored Ghost subprograms as those will be removed
6087 -- from the executable.
6089 and then not Is_Ignored_Ghost_Entity (E)
6090 then
6091 pragma Assert
6092 (UI_To_Int (DT_Position (Prim)) <= Nb_Prim);
6094 Expand_Secondary_Stack_Thunk
6095 (E, SS_Thunk_Id, SS_Thunk_Code);
6097 if Present (SS_Thunk_Id) then
6098 E := SS_Thunk_Id;
6099 Append_To (Result, SS_Thunk_Code);
6100 end if;
6102 Prim_Table (UI_To_Int (DT_Position (Prim))) := E;
6103 end if;
6105 Next_Elmt (Prim_Elmt);
6106 end loop;
6108 for J in Prim_Table'Range loop
6109 if Present (Prim_Table (J)) then
6110 New_Node :=
6111 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
6112 Make_Attribute_Reference (Loc,
6113 Prefix =>
6114 New_Occurrence_Of (Prim_Table (J), Loc),
6115 Attribute_Name => Name_Unrestricted_Access));
6116 else
6117 New_Node := Make_Null (Loc);
6118 end if;
6120 Append_To (Prim_Ops_Aggr_List, New_Node);
6121 end loop;
6122 end;
6123 end if;
6125 New_Node :=
6126 Make_Aggregate (Loc,
6127 Expressions => Prim_Ops_Aggr_List);
6129 Append_To (DT_Aggr_List, New_Node);
6131 -- Remember aggregates initializing dispatch tables
6133 Append_Elmt (New_Node, DT_Aggr);
6135 -- In case of locally defined tagged types we have already declared
6136 -- and uninitialized object for the dispatch table, which is now
6137 -- initialized by means of an assignment.
6139 if not Building_Static_DT (Typ) then
6140 Append_To (Result,
6141 Make_Assignment_Statement (Loc,
6142 Name => New_Occurrence_Of (DT, Loc),
6143 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
6145 -- In case of library level tagged types we declare now and export
6146 -- the constant object containing the dispatch table.
6148 else
6149 Append_To (Result,
6150 Make_Object_Declaration (Loc,
6151 Defining_Identifier => DT,
6152 Aliased_Present => True,
6153 Constant_Present => True,
6154 Object_Definition =>
6155 Make_Subtype_Indication (Loc,
6156 Subtype_Mark => New_Occurrence_Of
6157 (RTE (RE_Dispatch_Table_Wrapper), Loc),
6158 Constraint => Make_Index_Or_Discriminant_Constraint (Loc,
6159 Constraints => DT_Constr_List)),
6160 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
6162 Export_DT (Typ, DT);
6163 end if;
6164 end if;
6166 -- Initialize the table of ancestor tags if not building static
6167 -- dispatch table
6169 if not Building_Static_DT (Typ)
6170 and then not Is_Interface (Typ)
6171 and then not Is_CPP_Class (Typ)
6172 then
6173 Append_To (Result,
6174 Make_Assignment_Statement (Loc,
6175 Name =>
6176 Make_Indexed_Component (Loc,
6177 Prefix =>
6178 Make_Selected_Component (Loc,
6179 Prefix => New_Occurrence_Of (TSD, Loc),
6180 Selector_Name =>
6181 New_Occurrence_Of
6182 (RTE_Record_Component (RE_Tags_Table), Loc)),
6183 Expressions =>
6184 New_List (Make_Integer_Literal (Loc, 0))),
6186 Expression =>
6187 New_Occurrence_Of
6188 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)));
6189 end if;
6191 -- Inherit the dispatch tables of the parent. There is no need to
6192 -- inherit anything from the parent when building static dispatch tables
6193 -- because the whole dispatch table (including inherited primitives) has
6194 -- been already built.
6196 if Building_Static_DT (Typ) then
6197 null;
6199 -- If the ancestor is a CPP_Class type we inherit the dispatch tables
6200 -- in the init proc, and we don't need to fill them in here.
6202 elsif Is_CPP_Class (Parent_Typ) then
6203 null;
6205 -- Otherwise we fill in the dispatch tables here
6207 else
6208 if Typ /= Parent_Typ
6209 and then not Is_Interface (Typ)
6210 and then not Restriction_Active (No_Dispatching_Calls)
6211 then
6212 -- Inherit the dispatch table
6214 if not Is_Interface (Typ)
6215 and then not Is_Interface (Parent_Typ)
6216 and then not Is_CPP_Class (Parent_Typ)
6217 then
6218 declare
6219 Nb_Prims : constant Int :=
6220 UI_To_Int (DT_Entry_Count
6221 (First_Tag_Component (Parent_Typ)));
6223 begin
6224 Append_To (Elab_Code,
6225 Build_Inherit_Predefined_Prims (Loc,
6226 Old_Tag_Node =>
6227 New_Occurrence_Of
6228 (Node
6229 (Next_Elmt
6230 (First_Elmt
6231 (Access_Disp_Table (Parent_Typ)))), Loc),
6232 New_Tag_Node =>
6233 New_Occurrence_Of
6234 (Node
6235 (Next_Elmt
6236 (First_Elmt
6237 (Access_Disp_Table (Typ)))), Loc),
6238 Num_Predef_Prims =>
6239 Number_Of_Predefined_Prims (Parent_Typ)));
6241 if Nb_Prims /= 0 then
6242 Append_To (Elab_Code,
6243 Build_Inherit_Prims (Loc,
6244 Typ => Typ,
6245 Old_Tag_Node =>
6246 New_Occurrence_Of
6247 (Node
6248 (First_Elmt
6249 (Access_Disp_Table (Parent_Typ))), Loc),
6250 New_Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
6251 Num_Prims => Nb_Prims));
6252 end if;
6253 end;
6254 end if;
6256 -- Inherit the secondary dispatch tables of the ancestor
6258 if not Is_CPP_Class (Parent_Typ) then
6259 declare
6260 Sec_DT_Ancestor : Elmt_Id :=
6261 Next_Elmt
6262 (Next_Elmt
6263 (First_Elmt
6264 (Access_Disp_Table
6265 (Parent_Typ))));
6266 Sec_DT_Typ : Elmt_Id :=
6267 Next_Elmt
6268 (Next_Elmt
6269 (First_Elmt
6270 (Access_Disp_Table (Typ))));
6272 procedure Copy_Secondary_DTs (Typ : Entity_Id);
6273 -- Local procedure required to climb through the ancestors
6274 -- and copy the contents of all their secondary dispatch
6275 -- tables.
6277 ------------------------
6278 -- Copy_Secondary_DTs --
6279 ------------------------
6281 procedure Copy_Secondary_DTs (Typ : Entity_Id) is
6282 E : Entity_Id;
6283 Iface : Elmt_Id;
6285 begin
6286 -- Climb to the ancestor (if any) handling private types
6288 if Present (Full_View (Etype (Typ))) then
6289 if Full_View (Etype (Typ)) /= Typ then
6290 Copy_Secondary_DTs (Full_View (Etype (Typ)));
6291 end if;
6293 elsif Etype (Typ) /= Typ then
6294 Copy_Secondary_DTs (Etype (Typ));
6295 end if;
6297 if Present (Interfaces (Typ))
6298 and then not Is_Empty_Elmt_List (Interfaces (Typ))
6299 then
6300 Iface := First_Elmt (Interfaces (Typ));
6301 E := First_Entity (Typ);
6302 while Present (E)
6303 and then Present (Node (Sec_DT_Ancestor))
6304 and then Ekind (Node (Sec_DT_Ancestor)) = E_Constant
6305 loop
6306 if Is_Tag (E) and then Chars (E) /= Name_uTag then
6307 declare
6308 Num_Prims : constant Int :=
6309 UI_To_Int (DT_Entry_Count (E));
6311 begin
6312 if not Is_Interface (Etype (Typ)) then
6314 -- Inherit first secondary dispatch table
6316 Append_To (Elab_Code,
6317 Build_Inherit_Predefined_Prims (Loc,
6318 Old_Tag_Node =>
6319 Unchecked_Convert_To (RTE (RE_Tag),
6320 New_Occurrence_Of
6321 (Node
6322 (Next_Elmt (Sec_DT_Ancestor)),
6323 Loc)),
6324 New_Tag_Node =>
6325 Unchecked_Convert_To (RTE (RE_Tag),
6326 New_Occurrence_Of
6327 (Node (Next_Elmt (Sec_DT_Typ)),
6328 Loc)),
6329 Num_Predef_Prims =>
6330 Number_Of_Predefined_Prims
6331 (Parent_Typ)));
6333 if Num_Prims /= 0 then
6334 Append_To (Elab_Code,
6335 Build_Inherit_Prims (Loc,
6336 Typ => Node (Iface),
6337 Old_Tag_Node =>
6338 Unchecked_Convert_To
6339 (RTE (RE_Tag),
6340 New_Occurrence_Of
6341 (Node (Sec_DT_Ancestor),
6342 Loc)),
6343 New_Tag_Node =>
6344 Unchecked_Convert_To
6345 (RTE (RE_Tag),
6346 New_Occurrence_Of
6347 (Node (Sec_DT_Typ), Loc)),
6348 Num_Prims => Num_Prims));
6349 end if;
6350 end if;
6352 Next_Elmt (Sec_DT_Ancestor);
6353 Next_Elmt (Sec_DT_Typ);
6355 -- Skip the secondary dispatch table of
6356 -- predefined primitives
6358 Next_Elmt (Sec_DT_Ancestor);
6359 Next_Elmt (Sec_DT_Typ);
6361 if not Is_Interface (Etype (Typ)) then
6363 -- Inherit second secondary dispatch table
6365 Append_To (Elab_Code,
6366 Build_Inherit_Predefined_Prims (Loc,
6367 Old_Tag_Node =>
6368 Unchecked_Convert_To (RTE (RE_Tag),
6369 New_Occurrence_Of
6370 (Node
6371 (Next_Elmt (Sec_DT_Ancestor)),
6372 Loc)),
6373 New_Tag_Node =>
6374 Unchecked_Convert_To (RTE (RE_Tag),
6375 New_Occurrence_Of
6376 (Node (Next_Elmt (Sec_DT_Typ)),
6377 Loc)),
6378 Num_Predef_Prims =>
6379 Number_Of_Predefined_Prims
6380 (Parent_Typ)));
6382 if Num_Prims /= 0 then
6383 Append_To (Elab_Code,
6384 Build_Inherit_Prims (Loc,
6385 Typ => Node (Iface),
6386 Old_Tag_Node =>
6387 Unchecked_Convert_To
6388 (RTE (RE_Tag),
6389 New_Occurrence_Of
6390 (Node (Sec_DT_Ancestor),
6391 Loc)),
6392 New_Tag_Node =>
6393 Unchecked_Convert_To
6394 (RTE (RE_Tag),
6395 New_Occurrence_Of
6396 (Node (Sec_DT_Typ), Loc)),
6397 Num_Prims => Num_Prims));
6398 end if;
6399 end if;
6400 end;
6402 Next_Elmt (Sec_DT_Ancestor);
6403 Next_Elmt (Sec_DT_Typ);
6405 -- Skip the secondary dispatch table of
6406 -- predefined primitives
6408 Next_Elmt (Sec_DT_Ancestor);
6409 Next_Elmt (Sec_DT_Typ);
6411 Next_Elmt (Iface);
6412 end if;
6414 Next_Entity (E);
6415 end loop;
6416 end if;
6417 end Copy_Secondary_DTs;
6419 begin
6420 if Present (Node (Sec_DT_Ancestor))
6421 and then Ekind (Node (Sec_DT_Ancestor)) = E_Constant
6422 then
6423 -- Handle private types
6425 if Present (Full_View (Typ)) then
6426 Copy_Secondary_DTs (Full_View (Typ));
6427 else
6428 Copy_Secondary_DTs (Typ);
6429 end if;
6430 end if;
6431 end;
6432 end if;
6433 end if;
6434 end if;
6436 -- Generate code to check if the external tag of this type is the same
6437 -- as the external tag of some other declaration.
6439 -- Check_TSD (TSD'Unrestricted_Access);
6441 -- This check is a consequence of AI05-0113-1/06, so it officially
6442 -- applies to Ada 2005 (and Ada 2012). It might be argued that it is
6443 -- a desirable check to add in Ada 95 mode, but we hesitate to make
6444 -- this change, as it would be incompatible, and could conceivably
6445 -- cause a problem in existing Ada 95 code.
6447 -- We check for No_Run_Time_Mode here, because we do not want to pick
6448 -- up the RE_Check_TSD entity and call it in No_Run_Time mode.
6450 -- We cannot perform this check if the generation of its expanded name
6451 -- was discarded or if No_Tagged_Type_Registration is active.
6453 if not No_Run_Time_Mode
6454 and then not Discard_Names
6455 and then Ada_Version >= Ada_2005
6456 and then not Duplicated_Tag_Checks_Suppressed (Typ)
6457 and then not Restriction_Active (No_Tagged_Type_Registration)
6458 and then RTE_Available (RE_Check_TSD)
6459 then
6460 Append_To (Elab_Code,
6461 Make_Procedure_Call_Statement (Loc,
6462 Name =>
6463 New_Occurrence_Of (RTE (RE_Check_TSD), Loc),
6464 Parameter_Associations => New_List (
6465 Make_Attribute_Reference (Loc,
6466 Prefix => New_Occurrence_Of (TSD, Loc),
6467 Attribute_Name => Name_Unchecked_Access))));
6468 end if;
6470 -- Generate code to register the Tag in the External_Tag hash table for
6471 -- the pure Ada type only.
6473 -- Register_Tag (Dt_Ptr);
6475 -- Skip this action in the following cases:
6476 -- 1) if Register_Tag is not available.
6477 -- 2) in No_Run_Time mode.
6478 -- 3) if Typ is not defined at the library level (this is required
6479 -- to avoid adding concurrency control to the hash table used
6480 -- by the run-time to register the tags).
6481 -- 4) No_Tagged_Type_Registration is active.
6483 if not No_Run_Time_Mode
6484 and then Is_Library_Level_Entity (Typ)
6485 and then not Restriction_Active (No_Tagged_Type_Registration)
6486 and then RTE_Available (RE_Register_Tag)
6487 then
6488 Append_To (Elab_Code,
6489 Make_Procedure_Call_Statement (Loc,
6490 Name =>
6491 New_Occurrence_Of (RTE (RE_Register_Tag), Loc),
6492 Parameter_Associations =>
6493 New_List (New_Occurrence_Of (DT_Ptr, Loc))));
6494 end if;
6496 Append_List_To (Result, Elab_Code);
6498 -- Populate the two auxiliary tables used for dispatching asynchronous,
6499 -- conditional and timed selects for synchronized types that implement
6500 -- a limited interface. Skip this step in Ravenscar profile or when
6501 -- general dispatching is forbidden.
6503 if Ada_Version >= Ada_2005
6504 and then Is_Concurrent_Record_Type (Typ)
6505 and then Has_Interfaces (Typ)
6506 and then not Restriction_Active (No_Dispatching_Calls)
6507 and then not Restriction_Active (No_Select_Statements)
6508 then
6509 Append_List_To (Result,
6510 Make_Select_Specific_Data_Table (Typ));
6511 end if;
6513 -- Remember entities containing dispatch tables
6515 Append_Elmt (Predef_Prims, DT_Decl);
6516 Append_Elmt (DT, DT_Decl);
6518 Analyze_List (Result, Suppress => All_Checks);
6520 -- Mark entities containing dispatch tables. Required by the backend to
6521 -- handle them properly.
6523 if Has_DT (Typ) then
6524 declare
6525 Elmt : Elmt_Id;
6527 begin
6528 -- Object declarations
6530 Elmt := First_Elmt (DT_Decl);
6531 while Present (Elmt) loop
6532 Set_Is_Dispatch_Table_Entity (Node (Elmt));
6533 pragma Assert (Ekind (Etype (Node (Elmt))) = E_Array_Subtype
6534 or else Ekind (Etype (Node (Elmt))) = E_Record_Subtype);
6535 Set_Is_Dispatch_Table_Entity (Etype (Node (Elmt)));
6536 Next_Elmt (Elmt);
6537 end loop;
6539 -- Aggregates initializing dispatch tables
6541 Elmt := First_Elmt (DT_Aggr);
6542 while Present (Elmt) loop
6543 Set_Is_Dispatch_Table_Entity (Etype (Node (Elmt)));
6544 Next_Elmt (Elmt);
6545 end loop;
6546 end;
6547 end if;
6549 <<Leave_SCIL>>
6551 Set_Has_Dispatch_Table (Typ);
6553 -- Register the tagged type in the call graph nodes table
6555 Register_CG_Node (Typ);
6557 <<Leave>>
6558 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6560 return Result;
6561 end Make_DT;
6563 -------------------------------------
6564 -- Make_Select_Specific_Data_Table --
6565 -------------------------------------
6567 function Make_Select_Specific_Data_Table
6568 (Typ : Entity_Id) return List_Id
6570 Assignments : constant List_Id := New_List;
6571 Loc : constant Source_Ptr := Sloc (Typ);
6573 Conc_Typ : Entity_Id;
6574 Decls : List_Id := No_List;
6575 Prim : Entity_Id;
6576 Prim_Als : Entity_Id;
6577 Prim_Elmt : Elmt_Id;
6578 Prim_Pos : Uint;
6579 Nb_Prim : Nat := 0;
6581 type Examined_Array is array (Int range <>) of Boolean;
6583 function Find_Entry_Index (E : Entity_Id) return Uint;
6584 -- Given an entry, find its index in the visible declarations of the
6585 -- corresponding concurrent type of Typ.
6587 ----------------------
6588 -- Find_Entry_Index --
6589 ----------------------
6591 function Find_Entry_Index (E : Entity_Id) return Uint is
6592 Index : Uint := Uint_0;
6593 Subp_Decl : Node_Id;
6595 begin
6596 Subp_Decl := First (Decls);
6597 while Present (Subp_Decl) loop
6598 if Nkind (Subp_Decl) = N_Entry_Declaration then
6599 Index := Index + 1;
6601 if Defining_Identifier (Subp_Decl) = E then
6602 exit;
6603 end if;
6605 end if;
6607 Next (Subp_Decl);
6608 end loop;
6610 return Index;
6611 end Find_Entry_Index;
6613 -- Local variables
6615 Tag_Node : Node_Id;
6617 -- Start of processing for Make_Select_Specific_Data_Table
6619 begin
6620 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
6622 if Present (Corresponding_Concurrent_Type (Typ)) then
6623 Conc_Typ := Corresponding_Concurrent_Type (Typ);
6625 if Present (Full_View (Conc_Typ)) then
6626 Conc_Typ := Full_View (Conc_Typ);
6627 end if;
6629 if Ekind (Conc_Typ) = E_Protected_Type then
6630 Decls := Visible_Declarations (Protected_Definition (
6631 Parent (Conc_Typ)));
6632 else
6633 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
6634 Decls := Visible_Declarations (Task_Definition (
6635 Parent (Conc_Typ)));
6636 end if;
6637 end if;
6639 -- Count the non-predefined primitive operations
6641 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6642 while Present (Prim_Elmt) loop
6643 Prim := Node (Prim_Elmt);
6645 if not (Is_Predefined_Dispatching_Operation (Prim)
6646 or else Is_Predefined_Dispatching_Alias (Prim))
6647 then
6648 Nb_Prim := Nb_Prim + 1;
6649 end if;
6651 Next_Elmt (Prim_Elmt);
6652 end loop;
6654 declare
6655 Examined : Examined_Array (1 .. Nb_Prim) := (others => False);
6657 begin
6658 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6659 while Present (Prim_Elmt) loop
6660 Prim := Node (Prim_Elmt);
6662 -- Look for primitive overriding an abstract interface subprogram
6664 if Present (Interface_Alias (Prim))
6665 and then not
6666 Is_Ancestor
6667 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
6668 Use_Full_View => True)
6669 and then not Examined (UI_To_Int (DT_Position (Alias (Prim))))
6670 then
6671 Prim_Pos := DT_Position (Alias (Prim));
6672 pragma Assert (UI_To_Int (Prim_Pos) <= Nb_Prim);
6673 Examined (UI_To_Int (Prim_Pos)) := True;
6675 -- Set the primitive operation kind regardless of subprogram
6676 -- type. Generate:
6677 -- Ada.Tags.Set_Prim_Op_Kind (DT_Ptr, <position>, <kind>);
6679 if Tagged_Type_Expansion then
6680 Tag_Node :=
6681 New_Occurrence_Of
6682 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6684 else
6685 Tag_Node :=
6686 Make_Attribute_Reference (Loc,
6687 Prefix => New_Occurrence_Of (Typ, Loc),
6688 Attribute_Name => Name_Tag);
6689 end if;
6691 Append_To (Assignments,
6692 Make_Procedure_Call_Statement (Loc,
6693 Name => New_Occurrence_Of (RTE (RE_Set_Prim_Op_Kind), Loc),
6694 Parameter_Associations => New_List (
6695 Tag_Node,
6696 Make_Integer_Literal (Loc, Prim_Pos),
6697 Prim_Op_Kind (Alias (Prim), Typ))));
6699 -- Retrieve the root of the alias chain
6701 Prim_Als := Ultimate_Alias (Prim);
6703 -- In the case of an entry wrapper, set the entry index
6705 if Ekind (Prim) = E_Procedure
6706 and then Is_Primitive_Wrapper (Prim_Als)
6707 and then Ekind (Wrapped_Entity (Prim_Als)) = E_Entry
6708 then
6709 -- Generate:
6710 -- Ada.Tags.Set_Entry_Index
6711 -- (DT_Ptr, <position>, <index>);
6713 if Tagged_Type_Expansion then
6714 Tag_Node :=
6715 New_Occurrence_Of
6716 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6717 else
6718 Tag_Node :=
6719 Make_Attribute_Reference (Loc,
6720 Prefix => New_Occurrence_Of (Typ, Loc),
6721 Attribute_Name => Name_Tag);
6722 end if;
6724 Append_To (Assignments,
6725 Make_Procedure_Call_Statement (Loc,
6726 Name =>
6727 New_Occurrence_Of (RTE (RE_Set_Entry_Index), Loc),
6728 Parameter_Associations => New_List (
6729 Tag_Node,
6730 Make_Integer_Literal (Loc, Prim_Pos),
6731 Make_Integer_Literal (Loc,
6732 Find_Entry_Index (Wrapped_Entity (Prim_Als))))));
6733 end if;
6734 end if;
6736 Next_Elmt (Prim_Elmt);
6737 end loop;
6738 end;
6740 return Assignments;
6741 end Make_Select_Specific_Data_Table;
6743 ---------------
6744 -- Make_Tags --
6745 ---------------
6747 function Make_Tags (Typ : Entity_Id) return List_Id is
6748 Loc : constant Source_Ptr := Sloc (Typ);
6749 Result : constant List_Id := New_List;
6751 procedure Import_DT
6752 (Tag_Typ : Entity_Id;
6753 DT : Entity_Id;
6754 Is_Secondary_DT : Boolean);
6755 -- Import the dispatch table DT of tagged type Tag_Typ. Required to
6756 -- generate forward references and statically allocate the table. For
6757 -- primary dispatch tables that require no dispatch table generate:
6759 -- DT : static aliased constant Non_Dispatch_Table_Wrapper;
6760 -- pragma Import (Ada, DT);
6762 -- Otherwise generate:
6764 -- DT : static aliased constant Dispatch_Table_Wrapper (Nb_Prim);
6765 -- pragma Import (Ada, DT);
6767 ---------------
6768 -- Import_DT --
6769 ---------------
6771 procedure Import_DT
6772 (Tag_Typ : Entity_Id;
6773 DT : Entity_Id;
6774 Is_Secondary_DT : Boolean)
6776 DT_Constr_List : List_Id;
6777 Nb_Prim : Nat;
6779 begin
6780 Set_Is_Imported (DT);
6781 Mutate_Ekind (DT, E_Constant);
6782 Set_Related_Type (DT, Typ);
6784 -- The scope must be set now to call Get_External_Name
6786 Set_Scope (DT, Current_Scope);
6788 Get_External_Name (DT);
6789 Set_Interface_Name (DT,
6790 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
6792 -- Ensure proper Sprint output of this implicit importation
6794 Set_Is_Internal (DT);
6796 -- Save this entity to allow Make_DT to generate its exportation
6798 Append_Elmt (DT, Dispatch_Table_Wrappers (Typ));
6800 -- No dispatch table required
6802 if not Is_Secondary_DT and then not Has_DT (Tag_Typ) then
6803 Append_To (Result,
6804 Make_Object_Declaration (Loc,
6805 Defining_Identifier => DT,
6806 Aliased_Present => True,
6807 Constant_Present => True,
6808 Object_Definition =>
6809 New_Occurrence_Of
6810 (RTE (RE_No_Dispatch_Table_Wrapper), Loc)));
6812 else
6813 -- Calculate the number of primitives of the dispatch table and
6814 -- the size of the Type_Specific_Data record.
6816 Nb_Prim :=
6817 UI_To_Int (DT_Entry_Count (First_Tag_Component (Tag_Typ)));
6819 -- If the tagged type has no primitives we add a dummy slot whose
6820 -- address will be the tag of this type.
6822 if Nb_Prim = 0 then
6823 DT_Constr_List :=
6824 New_List (Make_Integer_Literal (Loc, 1));
6825 else
6826 DT_Constr_List :=
6827 New_List (Make_Integer_Literal (Loc, Nb_Prim));
6828 end if;
6830 Append_To (Result,
6831 Make_Object_Declaration (Loc,
6832 Defining_Identifier => DT,
6833 Aliased_Present => True,
6834 Constant_Present => True,
6835 Object_Definition =>
6836 Make_Subtype_Indication (Loc,
6837 Subtype_Mark =>
6838 New_Occurrence_Of (RTE (RE_Dispatch_Table_Wrapper), Loc),
6839 Constraint => Make_Index_Or_Discriminant_Constraint (Loc,
6840 Constraints => DT_Constr_List))));
6841 end if;
6842 end Import_DT;
6844 -- Local variables
6846 Tname : constant Name_Id := Chars (Typ);
6847 AI_Tag_Comp : Elmt_Id;
6848 DT : Node_Id := Empty;
6849 DT_Ptr : Node_Id;
6850 Predef_Prims_Ptr : Node_Id;
6851 Iface_DT : Node_Id := Empty;
6852 Iface_DT_Ptr : Node_Id;
6853 New_Node : Node_Id;
6854 Suffix_Index : Int;
6855 Typ_Name : Name_Id;
6856 Typ_Comps : Elist_Id;
6858 -- Start of processing for Make_Tags
6860 begin
6861 pragma Assert (No (Access_Disp_Table (Typ)));
6862 Set_Access_Disp_Table (Typ, New_Elmt_List);
6864 -- If the elaboration of this tagged type needs a boolean flag then
6865 -- define now its entity. It is initialized to True to indicate that
6866 -- elaboration is still pending; set to False by the IP routine.
6868 -- TypFxx : boolean := True;
6870 if Elab_Flag_Needed (Typ) then
6871 Set_Access_Disp_Table_Elab_Flag (Typ,
6872 Make_Defining_Identifier (Loc,
6873 Chars => New_External_Name (Tname, 'F')));
6875 Append_To (Result,
6876 Make_Object_Declaration (Loc,
6877 Defining_Identifier => Access_Disp_Table_Elab_Flag (Typ),
6878 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
6879 Expression => New_Occurrence_Of (Standard_True, Loc)));
6880 end if;
6882 -- 1) Generate the primary tag entities
6884 -- Primary dispatch table containing user-defined primitives
6886 DT_Ptr := Make_Defining_Identifier (Loc, New_External_Name (Tname, 'P'));
6887 Set_Etype (DT_Ptr, RTE (RE_Tag));
6888 Append_Elmt (DT_Ptr, Access_Disp_Table (Typ));
6890 -- Minimum decoration
6892 Mutate_Ekind (DT_Ptr, E_Variable);
6893 Set_Related_Type (DT_Ptr, Typ);
6895 -- Notify back end that the types are associated with a dispatch table
6897 Set_Is_Dispatch_Table_Entity (RTE (RE_Prim_Ptr));
6898 Set_Is_Dispatch_Table_Entity (RTE (RE_Predef_Prims_Table_Ptr));
6900 -- For CPP types there is no need to build the dispatch tables since
6901 -- they are imported from the C++ side. If the CPP type has an IP then
6902 -- we declare now the variable that will store the copy of the C++ tag.
6903 -- If the CPP type is an interface, we need the variable as well because
6904 -- it becomes the pointer to the corresponding secondary table.
6906 if Is_CPP_Class (Typ) then
6907 if Has_CPP_Constructors (Typ) or else Is_Interface (Typ) then
6908 Append_To (Result,
6909 Make_Object_Declaration (Loc,
6910 Defining_Identifier => DT_Ptr,
6911 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
6912 Expression =>
6913 Unchecked_Convert_To (RTE (RE_Tag),
6914 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
6916 Set_Is_Statically_Allocated (DT_Ptr,
6917 Is_Library_Level_Tagged_Type (Typ));
6918 end if;
6920 -- Ada types
6922 else
6923 -- Primary dispatch table containing predefined primitives
6925 Predef_Prims_Ptr :=
6926 Make_Defining_Identifier (Loc,
6927 Chars => New_External_Name (Tname, 'Y'));
6928 Set_Etype (Predef_Prims_Ptr, RTE (RE_Address));
6929 Append_Elmt (Predef_Prims_Ptr, Access_Disp_Table (Typ));
6931 -- Import the forward declaration of the Dispatch Table wrapper
6932 -- record (Make_DT will take care of exporting it).
6934 if Building_Static_DT (Typ) then
6935 Set_Dispatch_Table_Wrappers (Typ, New_Elmt_List);
6937 DT :=
6938 Make_Defining_Identifier (Loc,
6939 Chars => New_External_Name (Tname, 'T'));
6941 Import_DT (Typ, DT, Is_Secondary_DT => False);
6943 if Has_DT (Typ) then
6944 Append_To (Result,
6945 Make_Object_Declaration (Loc,
6946 Defining_Identifier => DT_Ptr,
6947 Constant_Present => True,
6948 Object_Definition =>
6949 New_Occurrence_Of (RTE (RE_Tag), Loc),
6950 Expression =>
6951 Unchecked_Convert_To (RTE (RE_Tag),
6952 Make_Attribute_Reference (Loc,
6953 Prefix =>
6954 Make_Selected_Component (Loc,
6955 Prefix => New_Occurrence_Of (DT, Loc),
6956 Selector_Name =>
6957 New_Occurrence_Of
6958 (RTE_Record_Component (RE_Prims_Ptr), Loc)),
6959 Attribute_Name => Name_Address))));
6961 -- Generate the SCIL node for the previous object declaration
6962 -- because it has a tag initialization.
6964 if Generate_SCIL then
6965 New_Node :=
6966 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
6967 Set_SCIL_Entity (New_Node, Typ);
6968 Set_SCIL_Node (Last (Result), New_Node);
6969 end if;
6971 Append_To (Result,
6972 Make_Object_Declaration (Loc,
6973 Defining_Identifier => Predef_Prims_Ptr,
6974 Constant_Present => True,
6975 Object_Definition =>
6976 New_Occurrence_Of (RTE (RE_Address), Loc),
6977 Expression =>
6978 Make_Attribute_Reference (Loc,
6979 Prefix =>
6980 Make_Selected_Component (Loc,
6981 Prefix => New_Occurrence_Of (DT, Loc),
6982 Selector_Name =>
6983 New_Occurrence_Of
6984 (RTE_Record_Component (RE_Predef_Prims), Loc)),
6985 Attribute_Name => Name_Address)));
6987 -- No dispatch table required
6989 else
6990 Append_To (Result,
6991 Make_Object_Declaration (Loc,
6992 Defining_Identifier => DT_Ptr,
6993 Constant_Present => True,
6994 Object_Definition =>
6995 New_Occurrence_Of (RTE (RE_Tag), Loc),
6996 Expression =>
6997 Unchecked_Convert_To (RTE (RE_Tag),
6998 Make_Attribute_Reference (Loc,
6999 Prefix =>
7000 Make_Selected_Component (Loc,
7001 Prefix => New_Occurrence_Of (DT, Loc),
7002 Selector_Name =>
7003 New_Occurrence_Of
7004 (RTE_Record_Component (RE_NDT_Prims_Ptr),
7005 Loc)),
7006 Attribute_Name => Name_Address))));
7007 end if;
7009 Set_Is_True_Constant (DT_Ptr);
7010 Set_Is_Statically_Allocated (DT_Ptr);
7011 end if;
7012 end if;
7014 -- 2) Generate the secondary tag entities
7016 -- Collect the components associated with secondary dispatch tables
7018 if Has_Interfaces (Typ) then
7019 Collect_Interface_Components (Typ, Typ_Comps);
7021 -- For each interface type we build a unique external name associated
7022 -- with its secondary dispatch table. This name is used to declare an
7023 -- object that references this secondary dispatch table, whose value
7024 -- will be used for the elaboration of Typ objects, and also for the
7025 -- elaboration of objects of types derived from Typ that do not
7026 -- override the primitives of this interface type.
7028 Suffix_Index := 1;
7030 -- Note: The value of Suffix_Index must be in sync with the values of
7031 -- Suffix_Index in secondary dispatch tables generated by Make_DT.
7033 if Is_CPP_Class (Typ) then
7034 AI_Tag_Comp := First_Elmt (Typ_Comps);
7035 while Present (AI_Tag_Comp) loop
7036 Get_Secondary_DT_External_Name
7037 (Typ, Related_Type (Node (AI_Tag_Comp)), Suffix_Index);
7038 Typ_Name := Name_Find;
7040 -- Declare variables to store copy of the C++ secondary tags
7042 Iface_DT_Ptr :=
7043 Make_Defining_Identifier (Loc,
7044 Chars => New_External_Name (Typ_Name, 'P'));
7045 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7046 Mutate_Ekind (Iface_DT_Ptr, E_Variable);
7047 Set_Is_Tag (Iface_DT_Ptr);
7049 Set_Has_Thunks (Iface_DT_Ptr);
7050 Set_Related_Type
7051 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7052 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7054 Append_To (Result,
7055 Make_Object_Declaration (Loc,
7056 Defining_Identifier => Iface_DT_Ptr,
7057 Object_Definition => New_Occurrence_Of
7058 (RTE (RE_Interface_Tag), Loc),
7059 Expression =>
7060 Unchecked_Convert_To (RTE (RE_Interface_Tag),
7061 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
7063 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7064 Is_Library_Level_Tagged_Type (Typ));
7066 Next_Elmt (AI_Tag_Comp);
7067 end loop;
7069 -- This is not a CPP_Class type
7071 else
7072 AI_Tag_Comp := First_Elmt (Typ_Comps);
7073 while Present (AI_Tag_Comp) loop
7074 Get_Secondary_DT_External_Name
7075 (Typ, Related_Type (Node (AI_Tag_Comp)), Suffix_Index);
7076 Typ_Name := Name_Find;
7078 if Building_Static_DT (Typ) then
7079 Iface_DT :=
7080 Make_Defining_Identifier (Loc,
7081 Chars => New_External_Name (Typ_Name, 'T'));
7082 Import_DT
7083 (Tag_Typ => Related_Type (Node (AI_Tag_Comp)),
7084 DT => Iface_DT,
7085 Is_Secondary_DT => True);
7086 end if;
7088 -- Secondary dispatch table referencing thunks to user-defined
7089 -- primitives covered by this interface.
7091 Iface_DT_Ptr :=
7092 Make_Defining_Identifier (Loc,
7093 Chars => New_External_Name (Typ_Name, 'P'));
7094 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7095 Mutate_Ekind (Iface_DT_Ptr, E_Constant);
7096 Set_Is_Tag (Iface_DT_Ptr);
7097 Set_Has_Thunks (Iface_DT_Ptr);
7098 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7099 Is_Library_Level_Tagged_Type (Typ));
7100 Set_Is_True_Constant (Iface_DT_Ptr);
7101 Set_Related_Type
7102 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7103 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7105 if Building_Static_DT (Typ) then
7106 Append_To (Result,
7107 Make_Object_Declaration (Loc,
7108 Defining_Identifier => Iface_DT_Ptr,
7109 Constant_Present => True,
7110 Object_Definition => New_Occurrence_Of
7111 (RTE (RE_Interface_Tag), Loc),
7112 Expression =>
7113 Unchecked_Convert_To (RTE (RE_Interface_Tag),
7114 Make_Attribute_Reference (Loc,
7115 Prefix =>
7116 Make_Selected_Component (Loc,
7117 Prefix =>
7118 New_Occurrence_Of (Iface_DT, Loc),
7119 Selector_Name =>
7120 New_Occurrence_Of
7121 (RTE_Record_Component (RE_Prims_Ptr),
7122 Loc)),
7123 Attribute_Name => Name_Address))));
7124 end if;
7126 -- Secondary dispatch table referencing thunks to predefined
7127 -- primitives.
7129 Iface_DT_Ptr :=
7130 Make_Defining_Identifier (Loc,
7131 Chars => New_External_Name (Typ_Name, 'Y'));
7132 Set_Etype (Iface_DT_Ptr, RTE (RE_Address));
7133 Mutate_Ekind (Iface_DT_Ptr, E_Constant);
7134 Set_Is_Tag (Iface_DT_Ptr);
7135 Set_Has_Thunks (Iface_DT_Ptr);
7136 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7137 Is_Library_Level_Tagged_Type (Typ));
7138 Set_Is_True_Constant (Iface_DT_Ptr);
7139 Set_Related_Type
7140 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7141 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7143 -- Secondary dispatch table referencing user-defined primitives
7144 -- covered by this interface.
7146 Iface_DT_Ptr :=
7147 Make_Defining_Identifier (Loc,
7148 Chars => New_External_Name (Typ_Name, 'D'));
7149 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7150 Mutate_Ekind (Iface_DT_Ptr, E_Constant);
7151 Set_Is_Tag (Iface_DT_Ptr);
7152 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7153 Is_Library_Level_Tagged_Type (Typ));
7154 Set_Is_True_Constant (Iface_DT_Ptr);
7155 Set_Related_Type
7156 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7157 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7159 -- Secondary dispatch table referencing predefined primitives
7161 Iface_DT_Ptr :=
7162 Make_Defining_Identifier (Loc,
7163 Chars => New_External_Name (Typ_Name, 'Z'));
7164 Set_Etype (Iface_DT_Ptr, RTE (RE_Address));
7165 Mutate_Ekind (Iface_DT_Ptr, E_Constant);
7166 Set_Is_Tag (Iface_DT_Ptr);
7167 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7168 Is_Library_Level_Tagged_Type (Typ));
7169 Set_Is_True_Constant (Iface_DT_Ptr);
7170 Set_Related_Type
7171 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7172 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7174 Next_Elmt (AI_Tag_Comp);
7175 end loop;
7176 end if;
7177 end if;
7179 -- 3) At the end of Access_Disp_Table, if the type has user-defined
7180 -- primitives, we add the entity of an access type declaration that
7181 -- is used by Build_Get_Prim_Op_Address to expand dispatching calls
7182 -- through the primary dispatch table.
7184 if DT_Entry_Count (First_Tag_Component (Typ)) = 0 then
7185 Analyze_List (Result);
7187 -- Generate:
7188 -- subtype Typ_DT is Address_Array (1 .. Nb_Prims);
7189 -- type Typ_DT_Acc is access Typ_DT;
7191 else
7192 declare
7193 Name_DT_Prims : constant Name_Id :=
7194 New_External_Name (Tname, 'G');
7195 Name_DT_Prims_Acc : constant Name_Id :=
7196 New_External_Name (Tname, 'H');
7197 DT_Prims : constant Entity_Id :=
7198 Make_Defining_Identifier (Loc,
7199 Name_DT_Prims);
7200 DT_Prims_Acc : constant Entity_Id :=
7201 Make_Defining_Identifier (Loc,
7202 Name_DT_Prims_Acc);
7203 begin
7204 Append_To (Result,
7205 Make_Subtype_Declaration (Loc,
7206 Defining_Identifier => DT_Prims,
7207 Subtype_Indication =>
7208 Make_Subtype_Indication (Loc,
7209 Subtype_Mark =>
7210 New_Occurrence_Of (RTE (RE_Address_Array), Loc),
7211 Constraint =>
7212 Make_Index_Or_Discriminant_Constraint (Loc, New_List (
7213 Make_Range (Loc,
7214 Low_Bound => Make_Integer_Literal (Loc, 1),
7215 High_Bound =>
7216 Make_Integer_Literal (Loc,
7217 DT_Entry_Count
7218 (First_Tag_Component (Typ)))))))));
7220 Append_To (Result,
7221 Make_Full_Type_Declaration (Loc,
7222 Defining_Identifier => DT_Prims_Acc,
7223 Type_Definition =>
7224 Make_Access_To_Object_Definition (Loc,
7225 Subtype_Indication =>
7226 New_Occurrence_Of (DT_Prims, Loc))));
7228 Append_Elmt (DT_Prims_Acc, Access_Disp_Table (Typ));
7230 -- Analyze the resulting list and suppress the generation of the
7231 -- Init_Proc associated with the above array declaration because
7232 -- this type is never used in object declarations. It is only used
7233 -- to simplify the expansion associated with dispatching calls.
7235 Analyze_List (Result);
7236 Set_Suppress_Initialization (Base_Type (DT_Prims));
7238 -- Disable backend optimizations based on assumptions about the
7239 -- aliasing status of objects designated by the access to the
7240 -- dispatch table. Required to handle dispatch tables imported
7241 -- from C++.
7243 Set_No_Strict_Aliasing (Base_Type (DT_Prims_Acc));
7245 -- Add the freezing nodes of these declarations; required to avoid
7246 -- generating these freezing nodes in wrong scopes (for example in
7247 -- the IC routine of a derivation of Typ).
7249 -- What is an "IC routine"? Is "init_proc" meant here???
7251 Append_List_To (Result, Freeze_Entity (DT_Prims, Typ));
7252 Append_List_To (Result, Freeze_Entity (DT_Prims_Acc, Typ));
7254 -- Mark entity of dispatch table. Required by the back end to
7255 -- handle them properly.
7257 Set_Is_Dispatch_Table_Entity (DT_Prims);
7258 end;
7259 end if;
7261 -- Mark entities of dispatch table. Required by the back end to handle
7262 -- them properly.
7264 if Present (DT) then
7265 Set_Is_Dispatch_Table_Entity (DT);
7266 Set_Is_Dispatch_Table_Entity (Etype (DT));
7267 end if;
7269 if Present (Iface_DT) then
7270 Set_Is_Dispatch_Table_Entity (Iface_DT);
7271 Set_Is_Dispatch_Table_Entity (Etype (Iface_DT));
7272 end if;
7274 if Is_CPP_Class (Root_Type (Typ)) then
7275 Mutate_Ekind (DT_Ptr, E_Variable);
7276 else
7277 Mutate_Ekind (DT_Ptr, E_Constant);
7278 end if;
7280 Set_Is_Tag (DT_Ptr);
7281 Set_Related_Type (DT_Ptr, Typ);
7283 return Result;
7284 end Make_Tags;
7286 ---------------
7287 -- New_Value --
7288 ---------------
7290 function New_Value (From : Node_Id) return Node_Id is
7291 Res : constant Node_Id := Duplicate_Subexpr (From);
7292 begin
7293 if Is_Access_Type (Etype (From)) then
7294 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
7295 else
7296 return Res;
7297 end if;
7298 end New_Value;
7300 ------------------
7301 -- Prim_Op_Kind --
7302 ------------------
7304 function Prim_Op_Kind
7305 (Prim : Entity_Id;
7306 Typ : Entity_Id) return Node_Id
7308 Full_Typ : Entity_Id := Typ;
7309 Loc : constant Source_Ptr := Sloc (Prim);
7310 Prim_Op : Entity_Id;
7312 begin
7313 -- Retrieve the original primitive operation
7315 Prim_Op := Ultimate_Alias (Prim);
7317 if Ekind (Typ) = E_Record_Type
7318 and then Present (Corresponding_Concurrent_Type (Typ))
7319 then
7320 Full_Typ := Corresponding_Concurrent_Type (Typ);
7321 end if;
7323 -- When a private tagged type is completed by a concurrent type,
7324 -- retrieve the full view.
7326 if Is_Private_Type (Full_Typ) then
7327 Full_Typ := Full_View (Full_Typ);
7328 end if;
7330 if Ekind (Prim_Op) = E_Function then
7332 -- Protected function
7334 if Ekind (Full_Typ) = E_Protected_Type then
7335 return New_Occurrence_Of (RTE (RE_POK_Protected_Function), Loc);
7337 -- Task function
7339 elsif Ekind (Full_Typ) = E_Task_Type then
7340 return New_Occurrence_Of (RTE (RE_POK_Task_Function), Loc);
7342 -- Regular function
7344 else
7345 return New_Occurrence_Of (RTE (RE_POK_Function), Loc);
7346 end if;
7348 else
7349 pragma Assert (Ekind (Prim_Op) = E_Procedure);
7351 if Ekind (Full_Typ) = E_Protected_Type then
7353 -- Protected entry
7355 if Is_Primitive_Wrapper (Prim_Op)
7356 and then Ekind (Wrapped_Entity (Prim_Op)) = E_Entry
7357 then
7358 return New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc);
7360 -- Protected procedure
7362 else
7363 return
7364 New_Occurrence_Of (RTE (RE_POK_Protected_Procedure), Loc);
7365 end if;
7367 elsif Ekind (Full_Typ) = E_Task_Type then
7369 -- Task entry
7371 if Is_Primitive_Wrapper (Prim_Op)
7372 and then Ekind (Wrapped_Entity (Prim_Op)) = E_Entry
7373 then
7374 return New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc);
7376 -- Task "procedure". These are the internally Expander-generated
7377 -- procedures (task body for instance).
7379 else
7380 return New_Occurrence_Of (RTE (RE_POK_Task_Procedure), Loc);
7381 end if;
7383 -- Regular procedure
7385 else
7386 return New_Occurrence_Of (RTE (RE_POK_Procedure), Loc);
7387 end if;
7388 end if;
7389 end Prim_Op_Kind;
7391 -----------------------------------
7392 -- Register_Predefined_Primitive --
7393 -----------------------------------
7395 function Register_Predefined_Primitive
7396 (Loc : Source_Ptr;
7397 Prim : Entity_Id) return List_Id
7399 L : constant List_Id := New_List;
7400 Tagged_Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
7402 E : Entity_Id;
7403 Iface_DT_Ptr : Elmt_Id;
7404 SS_Thunk_Id : Entity_Id;
7405 SS_Thunk_Code : Node_Id;
7406 Thunk_Id : Entity_Id;
7407 Thunk_Code : List_Id;
7409 begin
7410 if No (Access_Disp_Table (Tagged_Typ))
7411 or else not Has_Interfaces (Tagged_Typ)
7412 or else not RTE_Available (RE_Interface_Tag)
7413 or else Restriction_Active (No_Dispatching_Calls)
7414 then
7415 return L;
7416 end if;
7418 -- Skip the first two access-to-dispatch-table pointers since they
7419 -- leads to the primary dispatch table (predefined DT and user
7420 -- defined DT). We are only concerned with the secondary dispatch
7421 -- table pointers. Note that the access-to- dispatch-table pointer
7422 -- corresponds to the first implemented interface retrieved below.
7424 Iface_DT_Ptr :=
7425 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7427 while Present (Iface_DT_Ptr)
7428 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7429 loop
7430 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7432 Expand_Interface_Thunk
7433 (Prim, Thunk_Id, Thunk_Code, Related_Type (Node (Iface_DT_Ptr)));
7435 if Present (Thunk_Id) then
7436 Append_List_To (L, Thunk_Code);
7438 E := Prim;
7439 Expand_Secondary_Stack_Thunk (E, SS_Thunk_Id, SS_Thunk_Code);
7441 if Present (SS_Thunk_Id) then
7442 E := SS_Thunk_Id;
7443 Append_To (L, SS_Thunk_Code);
7444 end if;
7446 Append_To (L,
7447 Build_Set_Predefined_Prim_Op_Address (Loc,
7448 Tag_Node =>
7449 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7450 Position => DT_Position (Prim),
7451 Address_Node =>
7452 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7453 Make_Attribute_Reference (Loc,
7454 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7455 Attribute_Name => Name_Unrestricted_Access))));
7457 Append_To (L,
7458 Build_Set_Predefined_Prim_Op_Address (Loc,
7459 Tag_Node =>
7460 New_Occurrence_Of
7461 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7462 Loc),
7463 Position => DT_Position (Prim),
7464 Address_Node =>
7465 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7466 Make_Attribute_Reference (Loc,
7467 Prefix => New_Occurrence_Of (E, Loc),
7468 Attribute_Name => Name_Unrestricted_Access))));
7469 end if;
7471 -- Skip the tag of the predefined primitives dispatch table
7473 Next_Elmt (Iface_DT_Ptr);
7474 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7476 -- Skip tag of the no-thunks dispatch table
7478 Next_Elmt (Iface_DT_Ptr);
7479 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7481 -- Skip tag of predefined primitives no-thunks dispatch table
7483 Next_Elmt (Iface_DT_Ptr);
7484 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7486 Next_Elmt (Iface_DT_Ptr);
7487 end loop;
7489 return L;
7490 end Register_Predefined_Primitive;
7492 ------------------------
7493 -- Register_Primitive --
7494 ------------------------
7496 function Register_Primitive
7497 (Loc : Source_Ptr;
7498 Prim : Entity_Id) return List_Id
7500 L : constant List_Id := New_List;
7502 DT_Ptr : Entity_Id;
7503 E : Entity_Id;
7504 Iface_Prim : Entity_Id;
7505 Iface_Typ : Entity_Id;
7506 Iface_DT_Ptr : Entity_Id;
7507 Iface_DT_Elmt : Elmt_Id;
7508 Pos : Uint;
7509 SS_Thunk_Id : Entity_Id;
7510 SS_Thunk_Code : Node_Id;
7511 Tag : Entity_Id;
7512 Tag_Typ : Entity_Id;
7513 Thunk_Id : Entity_Id;
7514 Thunk_Code : List_Id;
7516 begin
7517 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
7519 -- Do not register eliminated primitives in the dispatch table
7521 if not RTE_Available (RE_Tag)
7522 or else Is_Eliminated (Ultimate_Alias (Prim))
7523 or else Generate_SCIL
7524 then
7525 return L;
7526 end if;
7528 -- Primitive associated with a tagged type
7530 if No (Interface_Alias (Prim)) then
7531 Tag_Typ := Scope (DTC_Entity (Prim));
7532 Pos := DT_Position (Prim);
7533 Tag := First_Tag_Component (Tag_Typ);
7535 E := Prim;
7536 Expand_Secondary_Stack_Thunk (E, SS_Thunk_Id, SS_Thunk_Code);
7538 if Present (SS_Thunk_Id) then
7539 E := SS_Thunk_Id;
7540 Append_To (L, SS_Thunk_Code);
7541 end if;
7543 if Is_Predefined_Dispatching_Operation (Prim)
7544 or else Is_Predefined_Dispatching_Alias (Prim)
7545 then
7546 DT_Ptr :=
7547 Node (Next_Elmt (First_Elmt (Access_Disp_Table (Tag_Typ))));
7549 Append_To (L,
7550 Build_Set_Predefined_Prim_Op_Address (Loc,
7551 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7552 Position => Pos,
7553 Address_Node =>
7554 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7555 Make_Attribute_Reference (Loc,
7556 Prefix => New_Occurrence_Of (E, Loc),
7557 Attribute_Name => Name_Unrestricted_Access))));
7559 -- Register copy of the pointer to the 'size primitive in the TSD
7561 if Chars (Prim) = Name_uSize
7562 and then RTE_Record_Component_Available (RE_Size_Func)
7563 then
7564 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Tag_Typ)));
7565 Append_To (L,
7566 Build_Set_Size_Function (Loc,
7567 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7568 Size_Func => Prim));
7569 end if;
7571 else
7572 pragma Assert (Pos /= Uint_0 and then Pos <= DT_Entry_Count (Tag));
7574 -- Skip registration of primitives located in the C++ part of the
7575 -- dispatch table. Their slot is set by the IC routine.
7577 if not Is_CPP_Class (Root_Type (Tag_Typ))
7578 or else Pos > CPP_Num_Prims (Tag_Typ)
7579 then
7580 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Tag_Typ)));
7581 Append_To (L,
7582 Build_Set_Prim_Op_Address (Loc,
7583 Typ => Tag_Typ,
7584 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7585 Position => Pos,
7586 Address_Node =>
7587 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7588 Make_Attribute_Reference (Loc,
7589 Prefix => New_Occurrence_Of (E, Loc),
7590 Attribute_Name => Name_Unrestricted_Access))));
7591 end if;
7592 end if;
7594 -- Ada 2005 (AI-251): Primitive associated with an interface type
7596 -- Generate the code of the thunk only if the interface type is not an
7597 -- immediate ancestor of Typ; otherwise the dispatch table associated
7598 -- with the interface is the primary dispatch table and we have nothing
7599 -- else to do here.
7601 else
7602 Tag_Typ := Find_Dispatching_Type (Alias (Prim));
7603 Iface_Typ := Find_Dispatching_Type (Interface_Alias (Prim));
7605 pragma Assert (Is_Interface (Iface_Typ));
7607 -- No action needed for interfaces that are ancestors of Typ because
7608 -- their primitives are located in the primary dispatch table.
7610 if Is_Ancestor (Iface_Typ, Tag_Typ, Use_Full_View => True) then
7611 return L;
7613 -- No action needed for primitives located in the C++ part of the
7614 -- dispatch table. Their slot is set by the IC routine.
7616 elsif Is_CPP_Class (Root_Type (Tag_Typ))
7617 and then DT_Position (Alias (Prim)) <= CPP_Num_Prims (Tag_Typ)
7618 and then not Is_Predefined_Dispatching_Operation (Prim)
7619 and then not Is_Predefined_Dispatching_Alias (Prim)
7620 then
7621 return L;
7622 end if;
7624 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code, Iface_Typ);
7626 if Present (Thunk_Id)
7627 and then not Is_Ancestor (Iface_Typ, Tag_Typ, Use_Full_View => True)
7628 then
7629 -- Generate the code necessary to fill the appropriate entry of
7630 -- the secondary dispatch table of Prim's controlling type with
7631 -- Thunk_Id's address.
7633 Iface_DT_Elmt := Find_Interface_ADT (Tag_Typ, Iface_Typ);
7634 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7635 pragma Assert (Has_Thunks (Iface_DT_Ptr));
7637 Iface_Prim := Interface_Alias (Prim);
7638 Pos := DT_Position (Iface_Prim);
7639 Tag := First_Tag_Component (Iface_Typ);
7641 Append_List_To (L, Thunk_Code);
7643 E := Ultimate_Alias (Prim);
7644 Expand_Secondary_Stack_Thunk (E, SS_Thunk_Id, SS_Thunk_Code);
7646 if Present (SS_Thunk_Id) then
7647 E := SS_Thunk_Id;
7648 Append_To (L, SS_Thunk_Code);
7649 end if;
7651 if Is_Predefined_Dispatching_Operation (Prim)
7652 or else Is_Predefined_Dispatching_Alias (Prim)
7653 then
7654 Append_To (L,
7655 Build_Set_Predefined_Prim_Op_Address (Loc,
7656 Tag_Node =>
7657 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Elmt)), Loc),
7658 Position => Pos,
7659 Address_Node =>
7660 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7661 Make_Attribute_Reference (Loc,
7662 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7663 Attribute_Name => Name_Unrestricted_Access))));
7665 Next_Elmt (Iface_DT_Elmt);
7666 Next_Elmt (Iface_DT_Elmt);
7667 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7668 pragma Assert (not Has_Thunks (Iface_DT_Ptr));
7670 Append_To (L,
7671 Build_Set_Predefined_Prim_Op_Address (Loc,
7672 Tag_Node =>
7673 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Elmt)), Loc),
7674 Position => Pos,
7675 Address_Node =>
7676 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7677 Make_Attribute_Reference (Loc,
7678 Prefix => New_Occurrence_Of (E, Loc),
7679 Attribute_Name => Name_Unrestricted_Access))));
7681 else
7682 pragma Assert (Pos /= Uint_0
7683 and then Pos <= DT_Entry_Count (Tag));
7685 Append_To (L,
7686 Build_Set_Prim_Op_Address (Loc,
7687 Typ => Iface_Typ,
7688 Tag_Node => New_Occurrence_Of (Iface_DT_Ptr, Loc),
7689 Position => Pos,
7690 Address_Node =>
7691 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7692 Make_Attribute_Reference (Loc,
7693 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7694 Attribute_Name => Name_Unrestricted_Access))));
7696 Next_Elmt (Iface_DT_Elmt);
7697 Next_Elmt (Iface_DT_Elmt);
7698 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7699 pragma Assert (not Has_Thunks (Iface_DT_Ptr));
7701 Append_To (L,
7702 Build_Set_Prim_Op_Address (Loc,
7703 Typ => Iface_Typ,
7704 Tag_Node => New_Occurrence_Of (Iface_DT_Ptr, Loc),
7705 Position => Pos,
7706 Address_Node =>
7707 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7708 Make_Attribute_Reference (Loc,
7709 Prefix => New_Occurrence_Of (E, Loc),
7710 Attribute_Name => Name_Unrestricted_Access))));
7712 end if;
7713 end if;
7714 end if;
7716 return L;
7717 end Register_Primitive;
7719 -------------------------
7720 -- Set_All_DT_Position --
7721 -------------------------
7723 procedure Set_All_DT_Position (Typ : Entity_Id) is
7725 function In_Predef_Prims_DT (Prim : Entity_Id) return Boolean;
7726 -- Returns True if Prim is located in the dispatch table of
7727 -- predefined primitives
7729 procedure Validate_Position (Prim : Entity_Id);
7730 -- Check that position assigned to Prim is completely safe (it has not
7731 -- been assigned to a previously defined primitive operation of Typ).
7733 ------------------------
7734 -- In_Predef_Prims_DT --
7735 ------------------------
7737 function In_Predef_Prims_DT (Prim : Entity_Id) return Boolean is
7738 begin
7739 -- Predefined primitives
7741 if Is_Predefined_Dispatching_Operation (Prim) then
7742 return True;
7744 -- Renamings of predefined primitives
7746 elsif Present (Alias (Prim))
7747 and then Is_Predefined_Dispatching_Operation (Ultimate_Alias (Prim))
7748 then
7749 if Chars (Ultimate_Alias (Prim)) /= Name_Op_Eq then
7750 return True;
7752 -- An overriding operation that is a user-defined renaming of
7753 -- predefined equality inherits its slot from the overridden
7754 -- operation. Otherwise it is treated as a predefined op and
7755 -- occupies the same predefined slot as equality. A call to it is
7756 -- transformed into a call to its alias, which is the predefined
7757 -- equality op. A dispatching call thus uses the proper slot if
7758 -- operation is further inherited and called with class-wide
7759 -- arguments.
7761 else
7762 return
7763 not Comes_From_Source (Prim)
7764 or else No (Overridden_Operation (Prim));
7765 end if;
7767 -- User-defined primitives
7769 else
7770 return False;
7771 end if;
7772 end In_Predef_Prims_DT;
7774 -----------------------
7775 -- Validate_Position --
7776 -----------------------
7778 procedure Validate_Position (Prim : Entity_Id) is
7779 Op_Elmt : Elmt_Id;
7780 Op : Entity_Id;
7782 begin
7783 -- Aliased primitives are safe
7785 if Present (Alias (Prim)) then
7786 return;
7787 end if;
7789 Op_Elmt := First_Elmt (Primitive_Operations (Typ));
7790 while Present (Op_Elmt) loop
7791 Op := Node (Op_Elmt);
7793 -- No need to check against itself
7795 if Op = Prim then
7796 null;
7798 -- Primitive operations covering abstract interfaces are
7799 -- allocated later
7801 elsif Present (Interface_Alias (Op)) then
7802 null;
7804 -- Predefined dispatching operations are completely safe. They
7805 -- are allocated at fixed positions in a separate table.
7807 elsif Is_Predefined_Dispatching_Operation (Op)
7808 or else Is_Predefined_Dispatching_Alias (Op)
7809 then
7810 null;
7812 -- Aliased subprograms are safe
7814 elsif Present (Alias (Op)) then
7815 null;
7817 elsif DT_Position (Op) = DT_Position (Prim)
7818 and then not Is_Predefined_Dispatching_Operation (Op)
7819 and then not Is_Predefined_Dispatching_Operation (Prim)
7820 and then not Is_Predefined_Dispatching_Alias (Op)
7821 and then not Is_Predefined_Dispatching_Alias (Prim)
7822 then
7823 -- Handle aliased subprograms
7825 declare
7826 Op_1 : Entity_Id;
7827 Op_2 : Entity_Id;
7829 begin
7830 Op_1 := Op;
7831 loop
7832 if Present (Overridden_Operation (Op_1)) then
7833 Op_1 := Overridden_Operation (Op_1);
7834 elsif Present (Alias (Op_1)) then
7835 Op_1 := Alias (Op_1);
7836 else
7837 exit;
7838 end if;
7839 end loop;
7841 Op_2 := Prim;
7842 loop
7843 if Present (Overridden_Operation (Op_2)) then
7844 Op_2 := Overridden_Operation (Op_2);
7845 elsif Present (Alias (Op_2)) then
7846 Op_2 := Alias (Op_2);
7847 else
7848 exit;
7849 end if;
7850 end loop;
7852 if Op_1 /= Op_2 then
7853 raise Program_Error;
7854 end if;
7855 end;
7856 end if;
7858 Next_Elmt (Op_Elmt);
7859 end loop;
7860 end Validate_Position;
7862 -- Local variables
7864 Parent_Typ : constant Entity_Id := Etype (Typ);
7865 First_Prim : constant Elmt_Id := First_Elmt (Primitive_Operations (Typ));
7866 The_Tag : constant Entity_Id := First_Tag_Component (Typ);
7868 Adjusted : Boolean := False;
7869 Finalized : Boolean := False;
7871 Count_Prim : Nat;
7872 DT_Length : Nat;
7873 Nb_Prim : Nat;
7874 Prim : Entity_Id;
7875 Prim_Elmt : Elmt_Id;
7877 -- Start of processing for Set_All_DT_Position
7879 begin
7880 pragma Assert (Present (First_Tag_Component (Typ)));
7882 -- Set the DT_Position for each primitive operation. Perform some sanity
7883 -- checks to avoid building inconsistent dispatch tables.
7885 -- First stage: Set DTC entity of all the primitive operations. This is
7886 -- required to properly read the DT_Position attribute in latter stages.
7888 Prim_Elmt := First_Prim;
7889 Count_Prim := 0;
7890 while Present (Prim_Elmt) loop
7891 Prim := Node (Prim_Elmt);
7893 -- Predefined primitives have a separate dispatch table
7895 if not In_Predef_Prims_DT (Prim) then
7896 Count_Prim := Count_Prim + 1;
7897 end if;
7899 Set_DTC_Entity_Value (Typ, Prim);
7901 -- Clear any previous value of the DT_Position attribute. In this
7902 -- way we ensure that the final position of all the primitives is
7903 -- established by the following stages of this algorithm.
7905 Set_DT_Position_Value (Prim, No_Uint);
7907 Next_Elmt (Prim_Elmt);
7908 end loop;
7910 declare
7911 Fixed_Prim : array (Int range 0 .. Count_Prim) of Boolean :=
7912 (others => False);
7914 E : Entity_Id;
7916 procedure Handle_Inherited_Private_Subprograms (Typ : Entity_Id);
7917 -- Called if Typ is declared in a nested package or a public child
7918 -- package to handle inherited primitives that were inherited by Typ
7919 -- in the visible part, but whose declaration was deferred because
7920 -- the parent operation was private and not visible at that point.
7922 procedure Set_Fixed_Prim (Pos : Nat);
7923 -- Sets to true an element of the Fixed_Prim table to indicate
7924 -- that this entry of the dispatch table of Typ is occupied.
7926 ------------------------------------------
7927 -- Handle_Inherited_Private_Subprograms --
7928 ------------------------------------------
7930 procedure Handle_Inherited_Private_Subprograms (Typ : Entity_Id) is
7931 Op_List : Elist_Id;
7932 Op_Elmt : Elmt_Id;
7933 Op_Elmt_2 : Elmt_Id;
7934 Prim_Op : Entity_Id;
7935 Parent_Subp : Entity_Id;
7937 begin
7938 Op_List := Primitive_Operations (Typ);
7940 Op_Elmt := First_Elmt (Op_List);
7941 while Present (Op_Elmt) loop
7942 Prim_Op := Node (Op_Elmt);
7944 -- Search primitives that are implicit operations with an
7945 -- internal name whose parent operation has a normal name.
7947 if Present (Alias (Prim_Op))
7948 and then Find_Dispatching_Type (Alias (Prim_Op)) /= Typ
7949 and then not Comes_From_Source (Prim_Op)
7950 and then Is_Internal_Name (Chars (Prim_Op))
7951 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
7952 then
7953 Parent_Subp := Alias (Prim_Op);
7955 -- Check if the type has an explicit overriding for this
7956 -- primitive.
7958 Op_Elmt_2 := Next_Elmt (Op_Elmt);
7959 while Present (Op_Elmt_2) loop
7960 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
7961 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
7962 then
7963 Set_DT_Position_Value (Prim_Op,
7964 DT_Position (Parent_Subp));
7965 Set_DT_Position_Value (Node (Op_Elmt_2),
7966 DT_Position (Parent_Subp));
7967 Set_Fixed_Prim (UI_To_Int (DT_Position (Prim_Op)));
7969 goto Next_Primitive;
7970 end if;
7972 Next_Elmt (Op_Elmt_2);
7973 end loop;
7974 end if;
7976 <<Next_Primitive>>
7977 Next_Elmt (Op_Elmt);
7978 end loop;
7979 end Handle_Inherited_Private_Subprograms;
7981 --------------------
7982 -- Set_Fixed_Prim --
7983 --------------------
7985 procedure Set_Fixed_Prim (Pos : Nat) is
7986 begin
7987 pragma Assert (Pos <= Count_Prim);
7988 Fixed_Prim (Pos) := True;
7989 exception
7990 when Constraint_Error =>
7991 raise Program_Error;
7992 end Set_Fixed_Prim;
7994 begin
7995 -- In case of nested packages and public child package it may be
7996 -- necessary a special management on inherited subprograms so that
7997 -- the dispatch table is properly filled.
7999 if Ekind (Scope (Scope (Typ))) = E_Package
8000 and then Scope (Scope (Typ)) /= Standard_Standard
8001 and then ((Is_Derived_Type (Typ) and then not Is_Private_Type (Typ))
8002 or else
8003 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration
8004 and then Is_Generic_Type (Typ)))
8005 and then In_Open_Scopes (Scope (Etype (Typ)))
8006 and then Is_Base_Type (Typ)
8007 then
8008 Handle_Inherited_Private_Subprograms (Typ);
8009 end if;
8011 -- Second stage: Register fixed entries
8013 Nb_Prim := 0;
8014 Prim_Elmt := First_Prim;
8015 while Present (Prim_Elmt) loop
8016 Prim := Node (Prim_Elmt);
8018 -- Predefined primitives have a separate table and all its
8019 -- entries are at predefined fixed positions.
8021 if In_Predef_Prims_DT (Prim) then
8022 if Is_Predefined_Dispatching_Operation (Prim) then
8023 Set_DT_Position_Value (Prim,
8024 Default_Prim_Op_Position (Prim));
8026 else pragma Assert (Present (Alias (Prim)));
8027 Set_DT_Position_Value (Prim,
8028 Default_Prim_Op_Position (Ultimate_Alias (Prim)));
8029 end if;
8031 -- Overriding primitives of ancestor abstract interfaces
8033 elsif Present (Interface_Alias (Prim))
8034 and then Is_Ancestor
8035 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
8036 Use_Full_View => True)
8037 then
8038 pragma Assert (No (DT_Position (Prim)));
8039 pragma Assert (Present (DTC_Entity (Interface_Alias (Prim))));
8041 E := Interface_Alias (Prim);
8042 Set_DT_Position_Value (Prim, DT_Position (E));
8044 pragma Assert
8045 (No (DT_Position (Alias (Prim)))
8046 or else DT_Position (Alias (Prim)) = DT_Position (E));
8047 Set_DT_Position_Value (Alias (Prim), DT_Position (E));
8048 Set_Fixed_Prim (UI_To_Int (DT_Position (Prim)));
8050 -- Overriding primitives must use the same entry as the overridden
8051 -- primitive. Note that the Alias of the operation is set when the
8052 -- operation is declared by a renaming, in which case it is not
8053 -- overriding. If it renames another primitive it will use the
8054 -- same dispatch table slot, but if it renames an operation in a
8055 -- nested package it's a new primitive and will have its own slot.
8057 elsif No (Interface_Alias (Prim))
8058 and then Present (Alias (Prim))
8059 and then Chars (Prim) = Chars (Alias (Prim))
8060 and then Nkind (Unit_Declaration_Node (Prim)) /=
8061 N_Subprogram_Renaming_Declaration
8062 then
8063 declare
8064 Par_Type : constant Entity_Id :=
8065 Find_Dispatching_Type (Alias (Prim));
8067 begin
8068 if Present (Par_Type)
8069 and then Par_Type /= Typ
8070 and then Is_Ancestor (Par_Type, Typ, Use_Full_View => True)
8071 and then Present (DTC_Entity (Alias (Prim)))
8072 then
8073 E := Alias (Prim);
8074 Set_DT_Position_Value (Prim, DT_Position (E));
8076 if not Is_Predefined_Dispatching_Alias (E) then
8077 Set_Fixed_Prim (UI_To_Int (DT_Position (E)));
8078 end if;
8079 end if;
8080 end;
8081 end if;
8083 Next_Elmt (Prim_Elmt);
8084 end loop;
8086 -- Third stage: Fix the position of all the new primitives. Entries
8087 -- associated with primitives covering interfaces are handled in a
8088 -- latter round.
8090 Prim_Elmt := First_Prim;
8091 while Present (Prim_Elmt) loop
8092 Prim := Node (Prim_Elmt);
8094 -- Skip primitives previously set entries
8096 if Present (DT_Position (Prim)) then
8097 null;
8099 -- Primitives covering interface primitives are handled later
8101 elsif Present (Interface_Alias (Prim)) then
8102 null;
8104 else
8105 -- Take the next available position in the DT
8107 loop
8108 Nb_Prim := Nb_Prim + 1;
8109 pragma Assert (Nb_Prim <= Count_Prim);
8110 exit when not Fixed_Prim (Nb_Prim);
8111 end loop;
8113 Set_DT_Position_Value (Prim, UI_From_Int (Nb_Prim));
8114 Set_Fixed_Prim (Nb_Prim);
8115 end if;
8117 Next_Elmt (Prim_Elmt);
8118 end loop;
8119 end;
8121 -- Fourth stage: Complete the decoration of primitives covering
8122 -- interfaces (that is, propagate the DT_Position attribute from
8123 -- the aliased primitive)
8125 Prim_Elmt := First_Prim;
8126 while Present (Prim_Elmt) loop
8127 Prim := Node (Prim_Elmt);
8129 if No (DT_Position (Prim))
8130 and then Present (Interface_Alias (Prim))
8131 then
8132 pragma Assert (Present (Alias (Prim))
8133 and then Find_Dispatching_Type (Alias (Prim)) = Typ);
8135 -- Check if this entry will be placed in the primary DT
8137 if Is_Ancestor
8138 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
8139 Use_Full_View => True)
8140 then
8141 pragma Assert (Present (DT_Position (Alias (Prim))));
8142 Set_DT_Position_Value (Prim, DT_Position (Alias (Prim)));
8144 -- Otherwise it will be placed in the secondary DT
8146 else
8147 pragma Assert
8148 (Present (DT_Position (Interface_Alias (Prim))));
8149 Set_DT_Position_Value (Prim,
8150 DT_Position (Interface_Alias (Prim)));
8151 end if;
8152 end if;
8154 Next_Elmt (Prim_Elmt);
8155 end loop;
8157 -- Generate listing showing the contents of the dispatch tables. This
8158 -- action is done before some further static checks because in case of
8159 -- critical errors caused by a wrong dispatch table we need to see the
8160 -- contents of such table.
8162 if Debug_Flag_ZZ then
8163 Write_DT (Typ);
8164 end if;
8166 -- Final stage: Ensure that the table is correct plus some further
8167 -- verifications concerning the primitives.
8169 Prim_Elmt := First_Prim;
8170 DT_Length := 0;
8171 while Present (Prim_Elmt) loop
8172 Prim := Node (Prim_Elmt);
8174 -- At this point all the primitives MUST have a position in the
8175 -- dispatch table.
8177 if No (DT_Position (Prim)) then
8178 raise Program_Error;
8179 end if;
8181 -- Calculate real size of the dispatch table
8183 if not In_Predef_Prims_DT (Prim)
8184 and then UI_To_Int (DT_Position (Prim)) > DT_Length
8185 then
8186 DT_Length := UI_To_Int (DT_Position (Prim));
8187 end if;
8189 -- Ensure that the assigned position to non-predefined dispatching
8190 -- operations in the dispatch table is correct.
8192 if not Is_Predefined_Dispatching_Operation (Prim)
8193 and then not Is_Predefined_Dispatching_Alias (Prim)
8194 then
8195 Validate_Position (Prim);
8196 end if;
8198 if Chars (Prim) = Name_Finalize then
8199 Finalized := True;
8200 end if;
8202 if Chars (Prim) = Name_Adjust then
8203 Adjusted := True;
8204 end if;
8206 -- An abstract operation cannot be declared in the private part for a
8207 -- visible abstract type, because it can't be overridden outside this
8208 -- package hierarchy. For explicit declarations this is checked at
8209 -- the point of declaration, but for inherited operations it must be
8210 -- done when building the dispatch table.
8212 -- Ada 2005 (AI-251): Primitives associated with interfaces are
8213 -- excluded from this check because interfaces must be visible in
8214 -- the public and private part (RM 7.3 (7.3/2))
8216 -- We disable this check in Relaxed_RM_Semantics mode, to accommodate
8217 -- legacy Ada code.
8219 if not Relaxed_RM_Semantics
8220 and then Is_Abstract_Type (Typ)
8221 and then Is_Abstract_Subprogram (Prim)
8222 and then Present (Alias (Prim))
8223 and then not Is_Interface
8224 (Find_Dispatching_Type (Ultimate_Alias (Prim)))
8225 and then No (Interface_Alias (Prim))
8226 and then Is_Derived_Type (Typ)
8227 and then In_Private_Part (Current_Scope)
8228 and then
8229 List_Containing (Parent (Prim)) =
8230 Private_Declarations (Package_Specification (Current_Scope))
8231 and then Original_View_In_Visible_Part (Typ)
8232 then
8233 -- We exclude Input and Output stream operations because
8234 -- Limited_Controlled inherits useless Input and Output stream
8235 -- operations from Root_Controlled, which can never be overridden.
8236 -- Move this check to sem???
8238 if not Is_TSS (Prim, TSS_Stream_Input)
8239 and then
8240 not Is_TSS (Prim, TSS_Stream_Output)
8241 then
8242 Error_Msg_NE
8243 ("abstract inherited private operation&" &
8244 " must be overridden (RM 3.9.3(10))",
8245 Parent (Typ), Prim);
8246 end if;
8247 end if;
8249 Next_Elmt (Prim_Elmt);
8250 end loop;
8252 -- Additional check
8254 if Is_Controlled (Typ) then
8255 if not Finalized then
8256 Error_Msg_N
8257 ("controlled type has no explicit Finalize method??", Typ);
8259 elsif not Adjusted then
8260 Error_Msg_N
8261 ("controlled type has no explicit Adjust method??", Typ);
8262 end if;
8263 end if;
8265 -- Set the final size of the Dispatch Table
8267 Set_DT_Entry_Count (The_Tag, UI_From_Int (DT_Length));
8269 -- The derived type must have at least as many components as its parent
8270 -- (for root types Etype points to itself and the test cannot fail).
8272 if DT_Entry_Count (The_Tag) <
8273 DT_Entry_Count (First_Tag_Component (Parent_Typ))
8274 then
8275 raise Program_Error;
8276 end if;
8277 end Set_All_DT_Position;
8279 --------------------------
8280 -- Set_CPP_Constructors --
8281 --------------------------
8283 procedure Set_CPP_Constructors (Typ : Entity_Id) is
8285 function Gen_Parameters_Profile (E : Entity_Id) return List_Id;
8286 -- Duplicate the parameters profile of the imported C++ constructor
8287 -- adding the "this" pointer to the object as the additional first
8288 -- parameter under the usual form _Init : in out Typ.
8290 ----------------------------
8291 -- Gen_Parameters_Profile --
8292 ----------------------------
8294 function Gen_Parameters_Profile (E : Entity_Id) return List_Id is
8295 Loc : constant Source_Ptr := Sloc (E);
8296 Parms : List_Id;
8297 P : Node_Id;
8299 begin
8300 Parms :=
8301 New_List (
8302 Make_Parameter_Specification (Loc,
8303 Defining_Identifier =>
8304 Make_Defining_Identifier (Loc, Name_uInit),
8305 In_Present => True,
8306 Out_Present => True,
8307 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8309 P := First (Parameter_Specifications (Parent (E)));
8310 while Present (P) loop
8311 Append_To (Parms,
8312 Make_Parameter_Specification (Loc,
8313 Defining_Identifier =>
8314 Make_Defining_Identifier (Loc,
8315 Chars => Chars (Defining_Identifier (P))),
8316 Parameter_Type => New_Copy_Tree (Parameter_Type (P)),
8317 Expression => New_Copy_Tree (Expression (P))));
8318 Next (P);
8319 end loop;
8321 return Parms;
8322 end Gen_Parameters_Profile;
8324 -- Local variables
8326 Loc : Source_Ptr;
8327 E : Entity_Id;
8328 Found : Boolean := False;
8329 IP : Entity_Id;
8330 IP_Body : Node_Id;
8331 P : Node_Id;
8332 Parms : List_Id;
8334 Covers_Default_Constructor : Entity_Id := Empty;
8336 -- Start of processing for Set_CPP_Constructor
8338 begin
8339 pragma Assert (Is_CPP_Class (Typ));
8341 -- Look for the constructor entities
8343 E := Next_Entity (Typ);
8344 while Present (E) loop
8345 if Ekind (E) = E_Function
8346 and then Is_Constructor (E)
8347 then
8348 Found := True;
8349 Loc := Sloc (E);
8350 Parms := Gen_Parameters_Profile (E);
8351 IP := Make_Defining_Identifier (Loc, Make_Init_Proc_Name (Typ));
8353 -- Case 1: Constructor of untagged type
8355 -- If the C++ class has no virtual methods then the matching Ada
8356 -- type is an untagged record type. In such case there is no need
8357 -- to generate a wrapper of the C++ constructor because the _tag
8358 -- component is not available.
8360 if not Is_Tagged_Type (Typ) then
8361 Discard_Node
8362 (Make_Subprogram_Declaration (Loc,
8363 Specification =>
8364 Make_Procedure_Specification (Loc,
8365 Defining_Unit_Name => IP,
8366 Parameter_Specifications => Parms)));
8368 Set_Init_Proc (Typ, IP);
8369 Set_Is_Imported (IP);
8370 Set_Is_Constructor (IP);
8371 Set_Interface_Name (IP, Interface_Name (E));
8372 Set_Convention (IP, Convention_CPP);
8373 Set_Is_Public (IP);
8374 Set_Has_Completion (IP);
8376 -- Case 2: Constructor of a tagged type
8378 -- In this case we generate the IP routine as a wrapper of the
8379 -- C++ constructor because IP must also save a copy of the _tag
8380 -- generated in the C++ side. The copy of the _tag is used by
8381 -- Build_CPP_Init_Procedure to elaborate derivations of C++ types.
8383 -- Generate:
8384 -- procedure IP (_init : in out Typ; ...) is
8385 -- procedure ConstructorP (_init : in out Typ; ...);
8386 -- pragma Import (ConstructorP);
8387 -- begin
8388 -- ConstructorP (_init, ...);
8389 -- if Typ._tag = null then
8390 -- Typ._tag := _init._tag;
8391 -- end if;
8392 -- end IP;
8394 else
8395 declare
8396 Body_Stmts : constant List_Id := New_List;
8397 Constructor_Id : Entity_Id;
8398 Constructor_Decl_Node : Node_Id;
8399 Init_Tags_List : List_Id;
8401 begin
8402 Constructor_Id := Make_Temporary (Loc, 'P');
8404 Constructor_Decl_Node :=
8405 Make_Subprogram_Declaration (Loc,
8406 Make_Procedure_Specification (Loc,
8407 Defining_Unit_Name => Constructor_Id,
8408 Parameter_Specifications => Parms));
8410 Set_Is_Imported (Constructor_Id);
8411 Set_Is_Constructor (Constructor_Id);
8412 Set_Interface_Name (Constructor_Id, Interface_Name (E));
8413 Set_Convention (Constructor_Id, Convention_CPP);
8414 Set_Is_Public (Constructor_Id);
8415 Set_Has_Completion (Constructor_Id);
8417 -- Build the init procedure as a wrapper of this constructor
8419 Parms := Gen_Parameters_Profile (E);
8421 -- Invoke the C++ constructor
8423 declare
8424 Actuals : constant List_Id := New_List;
8426 begin
8427 P := First (Parms);
8428 while Present (P) loop
8429 Append_To (Actuals,
8430 New_Occurrence_Of (Defining_Identifier (P), Loc));
8431 Next (P);
8432 end loop;
8434 Append_To (Body_Stmts,
8435 Make_Procedure_Call_Statement (Loc,
8436 Name => New_Occurrence_Of (Constructor_Id, Loc),
8437 Parameter_Associations => Actuals));
8438 end;
8440 -- Initialize copies of C++ primary and secondary tags
8442 Init_Tags_List := New_List;
8444 declare
8445 Tag_Elmt : Elmt_Id;
8446 Tag_Comp : Node_Id;
8448 begin
8449 Tag_Elmt := First_Elmt (Access_Disp_Table (Typ));
8450 Tag_Comp := First_Tag_Component (Typ);
8452 while Present (Tag_Elmt)
8453 and then Is_Tag (Node (Tag_Elmt))
8454 loop
8455 -- Skip the following assertion with primary tags
8456 -- because Related_Type is not set on primary tag
8457 -- components.
8459 pragma Assert
8460 (Tag_Comp = First_Tag_Component (Typ)
8461 or else Related_Type (Node (Tag_Elmt))
8462 = Related_Type (Tag_Comp));
8464 Append_To (Init_Tags_List,
8465 Make_Assignment_Statement (Loc,
8466 Name =>
8467 New_Occurrence_Of (Node (Tag_Elmt), Loc),
8468 Expression =>
8469 Make_Selected_Component (Loc,
8470 Prefix =>
8471 Make_Identifier (Loc, Name_uInit),
8472 Selector_Name =>
8473 New_Occurrence_Of (Tag_Comp, Loc))));
8475 Tag_Comp := Next_Tag_Component (Tag_Comp);
8476 Next_Elmt (Tag_Elmt);
8477 end loop;
8478 end;
8480 Append_To (Body_Stmts,
8481 Make_If_Statement (Loc,
8482 Condition =>
8483 Make_Op_Eq (Loc,
8484 Left_Opnd =>
8485 New_Occurrence_Of
8486 (Node (First_Elmt (Access_Disp_Table (Typ))),
8487 Loc),
8488 Right_Opnd =>
8489 Unchecked_Convert_To (RTE (RE_Tag),
8490 New_Occurrence_Of (RTE (RE_Null_Address), Loc))),
8491 Then_Statements => Init_Tags_List));
8493 IP_Body :=
8494 Make_Subprogram_Body (Loc,
8495 Specification =>
8496 Make_Procedure_Specification (Loc,
8497 Defining_Unit_Name => IP,
8498 Parameter_Specifications => Parms),
8499 Declarations => New_List (Constructor_Decl_Node),
8500 Handled_Statement_Sequence =>
8501 Make_Handled_Sequence_Of_Statements (Loc,
8502 Statements => Body_Stmts,
8503 Exception_Handlers => No_List));
8505 Discard_Node (IP_Body);
8506 Set_Init_Proc (Typ, IP);
8507 end;
8508 end if;
8510 -- If this constructor has parameters and all its parameters have
8511 -- defaults then it covers the default constructor. The semantic
8512 -- analyzer ensures that only one constructor with defaults covers
8513 -- the default constructor.
8515 if Present (Parameter_Specifications (Parent (E)))
8516 and then Needs_No_Actuals (E)
8517 then
8518 Covers_Default_Constructor := IP;
8519 end if;
8520 end if;
8522 Next_Entity (E);
8523 end loop;
8525 -- If there are no constructors, mark the type as abstract since we
8526 -- won't be able to declare objects of that type.
8528 if not Found then
8529 Set_Is_Abstract_Type (Typ);
8530 end if;
8532 -- Handle constructor that has all its parameters with defaults and
8533 -- hence it covers the default constructor. We generate a wrapper IP
8534 -- which calls the covering constructor.
8536 if Present (Covers_Default_Constructor) then
8537 declare
8538 Body_Stmts : List_Id;
8540 begin
8541 Loc := Sloc (Covers_Default_Constructor);
8543 Body_Stmts := New_List (
8544 Make_Procedure_Call_Statement (Loc,
8545 Name =>
8546 New_Occurrence_Of (Covers_Default_Constructor, Loc),
8547 Parameter_Associations => New_List (
8548 Make_Identifier (Loc, Name_uInit))));
8550 IP := Make_Defining_Identifier (Loc, Make_Init_Proc_Name (Typ));
8552 IP_Body :=
8553 Make_Subprogram_Body (Loc,
8554 Specification =>
8555 Make_Procedure_Specification (Loc,
8556 Defining_Unit_Name => IP,
8557 Parameter_Specifications => New_List (
8558 Make_Parameter_Specification (Loc,
8559 Defining_Identifier =>
8560 Make_Defining_Identifier (Loc, Name_uInit),
8561 Parameter_Type => New_Occurrence_Of (Typ, Loc)))),
8563 Declarations => No_List,
8565 Handled_Statement_Sequence =>
8566 Make_Handled_Sequence_Of_Statements (Loc,
8567 Statements => Body_Stmts,
8568 Exception_Handlers => No_List));
8570 Discard_Node (IP_Body);
8571 Set_Init_Proc (Typ, IP);
8572 end;
8573 end if;
8575 -- If the CPP type has constructors then it must import also the default
8576 -- C++ constructor. It is required for default initialization of objects
8577 -- of the type. It is also required to elaborate objects of Ada types
8578 -- that are defined as derivations of this CPP type.
8580 if Has_CPP_Constructors (Typ)
8581 and then No (Init_Proc (Typ))
8582 then
8583 Error_Msg_N ("??default constructor must be imported from C++", Typ);
8584 end if;
8585 end Set_CPP_Constructors;
8587 ---------------------------
8588 -- Set_DT_Position_Value --
8589 ---------------------------
8591 procedure Set_DT_Position_Value (Prim : Entity_Id; Value : Uint) is
8592 begin
8593 Set_DT_Position (Prim, Value);
8595 -- Propagate the value to the wrapped subprogram (if one is present)
8597 if Ekind (Prim) in E_Function | E_Procedure
8598 and then Is_Primitive_Wrapper (Prim)
8599 and then Present (Wrapped_Entity (Prim))
8600 and then Is_Dispatching_Operation (Wrapped_Entity (Prim))
8601 then
8602 Set_DT_Position (Wrapped_Entity (Prim), Value);
8603 end if;
8604 end Set_DT_Position_Value;
8606 --------------------------
8607 -- Set_DTC_Entity_Value --
8608 --------------------------
8610 procedure Set_DTC_Entity_Value
8611 (Tagged_Type : Entity_Id;
8612 Prim : Entity_Id)
8614 begin
8615 if Present (Interface_Alias (Prim))
8616 and then Is_Interface
8617 (Find_Dispatching_Type (Interface_Alias (Prim)))
8618 then
8619 Set_DTC_Entity (Prim,
8620 Find_Interface_Tag
8621 (T => Tagged_Type,
8622 Iface => Find_Dispatching_Type (Interface_Alias (Prim))));
8623 else
8624 Set_DTC_Entity (Prim,
8625 First_Tag_Component (Tagged_Type));
8626 end if;
8628 -- Propagate the value to the wrapped subprogram (if one is present)
8630 if Ekind (Prim) in E_Function | E_Procedure
8631 and then Is_Primitive_Wrapper (Prim)
8632 and then Present (Wrapped_Entity (Prim))
8633 and then Is_Dispatching_Operation (Wrapped_Entity (Prim))
8634 then
8635 Set_DTC_Entity (Wrapped_Entity (Prim), DTC_Entity (Prim));
8636 end if;
8637 end Set_DTC_Entity_Value;
8639 -----------------
8640 -- Tagged_Kind --
8641 -----------------
8643 function Tagged_Kind (T : Entity_Id) return Node_Id is
8644 Conc_Typ : Entity_Id;
8645 Loc : constant Source_Ptr := Sloc (T);
8647 begin
8648 pragma Assert
8649 (Is_Tagged_Type (T) and then RTE_Available (RE_Tagged_Kind));
8651 -- Abstract kinds
8653 if Is_Abstract_Type (T) then
8654 if Is_Limited_Record (T) then
8655 return New_Occurrence_Of
8656 (RTE (RE_TK_Abstract_Limited_Tagged), Loc);
8657 else
8658 return New_Occurrence_Of
8659 (RTE (RE_TK_Abstract_Tagged), Loc);
8660 end if;
8662 -- Concurrent kinds
8664 elsif Is_Concurrent_Record_Type (T) then
8665 Conc_Typ := Corresponding_Concurrent_Type (T);
8667 if Present (Full_View (Conc_Typ)) then
8668 Conc_Typ := Full_View (Conc_Typ);
8669 end if;
8671 if Ekind (Conc_Typ) = E_Protected_Type then
8672 return New_Occurrence_Of (RTE (RE_TK_Protected), Loc);
8673 else
8674 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
8675 return New_Occurrence_Of (RTE (RE_TK_Task), Loc);
8676 end if;
8678 -- Regular tagged kinds
8680 else
8681 if Is_Limited_Record (T) then
8682 return New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc);
8683 else
8684 return New_Occurrence_Of (RTE (RE_TK_Tagged), Loc);
8685 end if;
8686 end if;
8687 end Tagged_Kind;
8689 --------------
8690 -- Write_DT --
8691 --------------
8693 procedure Write_DT (Typ : Entity_Id) is
8694 Elmt : Elmt_Id;
8695 Prim : Node_Id;
8697 begin
8698 -- Protect this procedure against wrong usage. Required because it will
8699 -- be used directly from GDB
8701 if not (Typ <= Last_Node_Id)
8702 or else not Is_Tagged_Type (Typ)
8703 then
8704 Write_Str ("wrong usage: Write_DT must be used with tagged types");
8705 Write_Eol;
8706 return;
8707 end if;
8709 Write_Int (Int (Typ));
8710 Write_Str (": ");
8711 Write_Name (Chars (Typ));
8713 if Is_Interface (Typ) then
8714 Write_Str (" is interface");
8715 end if;
8717 Write_Eol;
8719 Elmt := First_Elmt (Primitive_Operations (Typ));
8720 while Present (Elmt) loop
8721 Prim := Node (Elmt);
8722 Write_Str (" - ");
8724 -- Indicate if this primitive will be allocated in the primary
8725 -- dispatch table or in a secondary dispatch table associated
8726 -- with an abstract interface type
8728 if Present (DTC_Entity (Prim)) then
8729 if Is_RTE (Etype (DTC_Entity (Prim)), RE_Tag) then
8730 Write_Str ("[P] ");
8731 else
8732 Write_Str ("[s] ");
8733 end if;
8734 end if;
8736 -- Output the node of this primitive operation and its name
8738 Write_Int (Int (Prim));
8739 Write_Str (": ");
8741 if Is_Predefined_Dispatching_Operation (Prim) then
8742 Write_Str ("(predefined) ");
8743 end if;
8745 -- Prefix the name of the primitive with its corresponding tagged
8746 -- type to facilitate seeing inherited primitives.
8748 if Present (Alias (Prim)) then
8749 Write_Name
8750 (Chars (Find_Dispatching_Type (Ultimate_Alias (Prim))));
8751 else
8752 Write_Name (Chars (Typ));
8753 end if;
8755 Write_Str (".");
8756 Write_Name (Chars (Prim));
8758 -- Indicate if this primitive has an aliased primitive
8760 if Present (Alias (Prim)) then
8761 Write_Str (" (alias = ");
8762 Write_Int (Int (Alias (Prim)));
8764 -- If the DTC_Entity attribute is already set we can also output
8765 -- the name of the interface covered by this primitive (if any).
8767 if Ekind (Alias (Prim)) in E_Function | E_Procedure
8768 and then Present (DTC_Entity (Alias (Prim)))
8769 and then Is_Interface (Scope (DTC_Entity (Alias (Prim))))
8770 then
8771 Write_Str (" from interface ");
8772 Write_Name (Chars (Scope (DTC_Entity (Alias (Prim)))));
8773 end if;
8775 if Present (Interface_Alias (Prim)) then
8776 Write_Str (", AI_Alias of ");
8778 if Is_Null_Interface_Primitive (Interface_Alias (Prim)) then
8779 Write_Str ("null primitive ");
8780 end if;
8782 Write_Name
8783 (Chars (Find_Dispatching_Type (Interface_Alias (Prim))));
8784 Write_Char (':');
8785 Write_Int (Int (Interface_Alias (Prim)));
8786 end if;
8788 Write_Str (")");
8789 end if;
8791 -- Display the final position of this primitive in its associated
8792 -- (primary or secondary) dispatch table.
8794 if Present (DTC_Entity (Prim))
8795 and then Present (DT_Position (Prim))
8796 then
8797 Write_Str (" at #");
8798 Write_Int (UI_To_Int (DT_Position (Prim)));
8799 end if;
8801 if Is_Abstract_Subprogram (Prim) then
8802 Write_Str (" is abstract;");
8804 -- Check if this is a null primitive
8806 elsif Comes_From_Source (Prim)
8807 and then Ekind (Prim) = E_Procedure
8808 and then Null_Present (Parent (Prim))
8809 then
8810 Write_Str (" is null;");
8811 end if;
8813 if Is_Eliminated (Ultimate_Alias (Prim)) then
8814 Write_Str (" (eliminated)");
8815 end if;
8817 if Is_Imported (Prim)
8818 and then Convention (Prim) = Convention_CPP
8819 then
8820 Write_Str (" (C++)");
8821 end if;
8823 Write_Eol;
8825 Next_Elmt (Elmt);
8826 end loop;
8827 end Write_DT;
8829 end Exp_Disp;