PR27116, Spelling errors found by Debian style checker
[official-gcc.git] / gcc / ada / exp_disp.adb
blob9e0c87a50951d5c58b4ba1a83e0a875e8445f9b9
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-2023, 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 not Present (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 and then
4605 (Global_Discard_Names or else Einfo.Entities.Discard_Names (Typ));
4607 -- The following name entries are used by Make_DT to generate a number
4608 -- of entities related to a tagged type. These entities may be generated
4609 -- in a scope other than that of the tagged type declaration, and if
4610 -- the entities for two tagged types with the same name happen to be
4611 -- generated in the same scope, we have to take care to use different
4612 -- names. This is achieved by means of a unique serial number appended
4613 -- to each generated entity name.
4615 Name_DT : constant Name_Id :=
4616 New_External_Name (Tname, 'T', Suffix_Index => -1);
4617 Name_Exname : constant Name_Id :=
4618 New_External_Name (Tname, 'E', Suffix_Index => -1);
4619 Name_HT_Link : constant Name_Id :=
4620 New_External_Name (Tname, 'H', Suffix_Index => -1);
4621 Name_Predef_Prims : constant Name_Id :=
4622 New_External_Name (Tname, 'R', Suffix_Index => -1);
4623 Name_SSD : constant Name_Id :=
4624 New_External_Name (Tname, 'S', Suffix_Index => -1);
4625 Name_TSD : constant Name_Id :=
4626 New_External_Name (Tname, 'B', Suffix_Index => -1);
4628 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4629 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4630 -- Save the Ghost-related attributes to restore on exit
4632 AI : Elmt_Id;
4633 AI_Tag_Elmt : Elmt_Id;
4634 AI_Tag_Comp : Elmt_Id;
4635 DT : Entity_Id;
4636 DT_Aggr_List : List_Id;
4637 DT_Constr_List : List_Id;
4638 DT_Ptr : Entity_Id;
4639 Exname : Entity_Id;
4640 HT_Link : Entity_Id;
4641 ITable : Node_Id;
4642 I_Depth : Nat;
4643 Iface_Table_Node : Node_Id;
4644 Name_ITable : Name_Id;
4645 Nb_Prim : Nat := 0;
4646 New_Node : Node_Id;
4647 Num_Ifaces : Nat := 0;
4648 Parent_Typ : Entity_Id;
4649 Predef_Prims : Entity_Id;
4650 Prim : Entity_Id;
4651 Prim_Elmt : Elmt_Id;
4652 Prim_Ops_Aggr_List : List_Id;
4653 SSD : Entity_Id;
4654 Suffix_Index : Int;
4655 Typ_Comps : Elist_Id;
4656 Typ_Ifaces : Elist_Id;
4657 TSD : Entity_Id;
4658 TSD_Aggr_List : List_Id;
4659 TSD_Tags_List : List_Id;
4661 -- Start of processing for Make_DT
4663 begin
4664 pragma Assert (Is_Frozen (Typ));
4666 -- The tagged type being processed may be subject to pragma Ghost. Set
4667 -- the mode now to ensure that any nodes generated during dispatch table
4668 -- creation are properly marked as Ghost.
4670 Set_Ghost_Mode (Typ);
4672 -- Handle cases in which there is no need to build the dispatch table
4674 if Has_Dispatch_Table (Typ)
4675 or else No (Access_Disp_Table (Typ))
4676 or else Is_CPP_Class (Typ)
4677 then
4678 goto Leave;
4680 elsif No_Run_Time_Mode then
4681 Error_Msg_CRT ("tagged types", Typ);
4682 goto Leave;
4684 elsif not RTE_Available (RE_Tag) then
4685 Append_To (Result,
4686 Make_Object_Declaration (Loc,
4687 Defining_Identifier =>
4688 Node (First_Elmt (Access_Disp_Table (Typ))),
4689 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
4690 Constant_Present => True,
4691 Expression =>
4692 Unchecked_Convert_To (RTE (RE_Tag),
4693 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
4695 Analyze_List (Result, Suppress => All_Checks);
4696 Error_Msg_CRT ("tagged types", Typ);
4697 goto Leave;
4698 end if;
4700 -- Ensure that the value of Max_Predef_Prims defined in a-tags is
4701 -- correct. Valid values are 10 under configurable runtime or 16
4702 -- with full runtime.
4704 if RTE_Available (RE_Interface_Data) then
4705 if Max_Predef_Prims /= 16 then
4706 Error_Msg_N ("run-time library configuration error", Typ);
4707 goto Leave;
4708 end if;
4709 else
4710 if Max_Predef_Prims /= 10 then
4711 Error_Msg_N ("run-time library configuration error", Typ);
4712 Error_Msg_CRT ("tagged types", Typ);
4713 goto Leave;
4714 end if;
4715 end if;
4717 DT := Make_Defining_Identifier (Loc, Name_DT);
4718 Exname := Make_Defining_Identifier (Loc, Name_Exname);
4719 HT_Link := Make_Defining_Identifier (Loc, Name_HT_Link);
4720 Predef_Prims := Make_Defining_Identifier (Loc, Name_Predef_Prims);
4721 SSD := Make_Defining_Identifier (Loc, Name_SSD);
4722 TSD := Make_Defining_Identifier (Loc, Name_TSD);
4724 -- Initialize Parent_Typ handling private types
4726 Parent_Typ := Etype (Typ);
4728 if Present (Full_View (Parent_Typ)) then
4729 Parent_Typ := Full_View (Parent_Typ);
4730 end if;
4732 if not Is_Interface (Typ) and then Has_Interfaces (Typ) then
4733 declare
4734 Cannot_Have_Null_Disc : Boolean := False;
4735 Dummy_Object_Typ : constant Entity_Id := Typ;
4736 Name_Dummy_Object : constant Name_Id :=
4737 New_External_Name (Tname,
4738 'P', Suffix_Index => -1);
4739 begin
4740 Dummy_Object := Make_Defining_Identifier (Loc, Name_Dummy_Object);
4742 -- Define the extra object imported and constant to avoid linker
4743 -- errors (since this object is never declared). Required because
4744 -- we implement RM 13.3(19) for exported and imported (variable)
4745 -- objects by making them volatile.
4747 Set_Is_Imported (Dummy_Object);
4748 Mutate_Ekind (Dummy_Object, E_Constant);
4749 Set_Is_True_Constant (Dummy_Object);
4750 Set_Related_Type (Dummy_Object, Typ);
4752 -- The scope must be set now to call Get_External_Name
4754 Set_Scope (Dummy_Object, Current_Scope);
4756 Get_External_Name (Dummy_Object);
4757 Set_Interface_Name (Dummy_Object,
4758 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
4760 -- Ensure proper Sprint output of this implicit importation
4762 Set_Is_Internal (Dummy_Object);
4764 if not Has_Discriminants (Dummy_Object_Typ) then
4765 Append_To (Result,
4766 Make_Object_Declaration (Loc,
4767 Defining_Identifier => Dummy_Object,
4768 Constant_Present => True,
4769 Object_Definition => New_Occurrence_Of
4770 (Dummy_Object_Typ, Loc)));
4771 else
4772 declare
4773 Constr_List : constant List_Id := New_List;
4774 Discrim : Node_Id;
4776 begin
4777 Discrim := First_Discriminant (Dummy_Object_Typ);
4778 while Present (Discrim) loop
4779 if Is_Discrete_Type (Etype (Discrim)) then
4780 Append_To (Constr_List,
4781 Make_Attribute_Reference (Loc,
4782 Prefix =>
4783 New_Occurrence_Of (Etype (Discrim), Loc),
4784 Attribute_Name => Name_First));
4786 else
4787 pragma Assert (Is_Access_Type (Etype (Discrim)));
4788 Cannot_Have_Null_Disc :=
4789 Cannot_Have_Null_Disc
4790 or else Can_Never_Be_Null (Etype (Discrim));
4791 Append_To (Constr_List, Make_Null (Loc));
4792 end if;
4794 Next_Discriminant (Discrim);
4795 end loop;
4797 Append_To (Result,
4798 Make_Object_Declaration (Loc,
4799 Defining_Identifier => Dummy_Object,
4800 Constant_Present => True,
4801 Object_Definition =>
4802 Make_Subtype_Indication (Loc,
4803 Subtype_Mark =>
4804 New_Occurrence_Of (Dummy_Object_Typ, Loc),
4805 Constraint =>
4806 Make_Index_Or_Discriminant_Constraint (Loc,
4807 Constraints => Constr_List))));
4808 end;
4809 end if;
4811 -- Given that the dummy object will not be declared at run time,
4812 -- analyze its declaration with expansion disabled and warnings
4813 -- and error messages ignored.
4815 Expander_Mode_Save_And_Set (False);
4816 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
4817 Analyze (Last (Result), Suppress => All_Checks);
4818 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
4819 Expander_Mode_Restore;
4820 end;
4821 end if;
4823 -- Ada 2005 (AI-251): Build the secondary dispatch tables
4825 if Has_Interfaces (Typ) then
4826 Collect_Interface_Components (Typ, Typ_Comps);
4828 -- Each secondary dispatch table is assigned an unique positive
4829 -- suffix index; such value also corresponds with the location of
4830 -- its entity in the Dispatch_Table_Wrappers list (see Make_Tags).
4832 -- Note: This value must be kept sync with the Suffix_Index values
4833 -- generated by Make_Tags
4835 Suffix_Index := 1;
4836 AI_Tag_Elmt :=
4837 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
4839 AI_Tag_Comp := First_Elmt (Typ_Comps);
4840 while Present (AI_Tag_Comp) loop
4841 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'P'));
4843 -- Build the secondary table containing pointers to thunks
4845 Make_Secondary_DT
4846 (Typ => Typ,
4847 Iface =>
4848 Base_Type (Related_Type (Node (AI_Tag_Comp))),
4849 Iface_Comp => Node (AI_Tag_Comp),
4850 Suffix_Index => Suffix_Index,
4851 Num_Iface_Prims =>
4852 UI_To_Int (DT_Entry_Count (Node (AI_Tag_Comp))),
4853 Iface_DT_Ptr => Node (AI_Tag_Elmt),
4854 Predef_Prims_Ptr => Node (Next_Elmt (AI_Tag_Elmt)),
4855 Build_Thunks => True,
4856 Result => Result);
4858 -- Skip secondary dispatch table referencing thunks to predefined
4859 -- primitives.
4861 Next_Elmt (AI_Tag_Elmt);
4862 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'Y'));
4864 -- Secondary dispatch table referencing user-defined primitives
4865 -- covered by this interface.
4867 Next_Elmt (AI_Tag_Elmt);
4868 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'D'));
4870 -- Build the secondary table containing pointers to primitives
4871 -- (used to give support to Generic Dispatching Constructors).
4873 Make_Secondary_DT
4874 (Typ => Typ,
4875 Iface => Base_Type
4876 (Related_Type (Node (AI_Tag_Comp))),
4877 Iface_Comp => Node (AI_Tag_Comp),
4878 Suffix_Index => -1,
4879 Num_Iface_Prims => UI_To_Int
4880 (DT_Entry_Count (Node (AI_Tag_Comp))),
4881 Iface_DT_Ptr => Node (AI_Tag_Elmt),
4882 Predef_Prims_Ptr => Node (Next_Elmt (AI_Tag_Elmt)),
4883 Build_Thunks => False,
4884 Result => Result);
4886 -- Skip secondary dispatch table referencing predefined primitives
4888 Next_Elmt (AI_Tag_Elmt);
4889 pragma Assert (Has_Suffix (Node (AI_Tag_Elmt), 'Z'));
4891 Suffix_Index := Suffix_Index + 1;
4892 Next_Elmt (AI_Tag_Elmt);
4893 Next_Elmt (AI_Tag_Comp);
4894 end loop;
4895 end if;
4897 -- Get the _tag entity and number of primitives of its dispatch table
4899 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Typ)));
4900 Nb_Prim := UI_To_Int (DT_Entry_Count (First_Tag_Component (Typ)));
4902 if Generate_SCIL then
4903 Nb_Prim := 0;
4904 end if;
4906 Set_Is_Statically_Allocated (DT, Is_Library_Level_Tagged_Type (Typ));
4907 Set_Is_Statically_Allocated (SSD, Is_Library_Level_Tagged_Type (Typ));
4908 Set_Is_Statically_Allocated (TSD, Is_Library_Level_Tagged_Type (Typ));
4909 Set_Is_Statically_Allocated (Predef_Prims,
4910 Is_Library_Level_Tagged_Type (Typ));
4912 -- In case of locally defined tagged type we declare the object
4913 -- containing the dispatch table by means of a variable. Its
4914 -- initialization is done later by means of an assignment. This is
4915 -- required to generate its External_Tag.
4917 if not Building_Static_DT (Typ) then
4919 -- Generate:
4920 -- DT : No_Dispatch_Table_Wrapper;
4921 -- DT_Ptr : Tag := !Tag (DT.NDT_Prims_Ptr'Address);
4923 if not Has_DT (Typ) then
4924 Append_To (Result,
4925 Make_Object_Declaration (Loc,
4926 Defining_Identifier => DT,
4927 Aliased_Present => True,
4928 Constant_Present => False,
4929 Object_Definition =>
4930 New_Occurrence_Of
4931 (RTE (RE_No_Dispatch_Table_Wrapper), Loc)));
4933 Append_To (Result,
4934 Make_Object_Declaration (Loc,
4935 Defining_Identifier => DT_Ptr,
4936 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
4937 Constant_Present => True,
4938 Expression =>
4939 Unchecked_Convert_To (RTE (RE_Tag),
4940 Make_Attribute_Reference (Loc,
4941 Prefix =>
4942 Make_Selected_Component (Loc,
4943 Prefix => New_Occurrence_Of (DT, Loc),
4944 Selector_Name =>
4945 New_Occurrence_Of
4946 (RTE_Record_Component (RE_NDT_Prims_Ptr), Loc)),
4947 Attribute_Name => Name_Address))));
4949 Set_Is_Statically_Allocated (DT_Ptr,
4950 Is_Library_Level_Tagged_Type (Typ));
4952 -- Generate the SCIL node for the previous object declaration
4953 -- because it has a tag initialization.
4955 if Generate_SCIL then
4956 New_Node :=
4957 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
4958 Set_SCIL_Entity (New_Node, Typ);
4959 Set_SCIL_Node (Last (Result), New_Node);
4961 goto Leave_SCIL;
4963 -- Gnat2scil has its own implementation of dispatch tables,
4964 -- different than what is being implemented here. Generating
4965 -- further dispatch table initialization code would just
4966 -- cause gnat2scil to generate useless Scil which CodePeer
4967 -- would waste time and space analyzing, so we skip it.
4968 end if;
4970 -- Generate:
4971 -- DT : Dispatch_Table_Wrapper (Nb_Prim);
4972 -- DT_Ptr : Tag := !Tag (DT.Prims_Ptr'Address);
4974 else
4975 -- If the tagged type has no primitives we add a dummy slot
4976 -- whose address will be the tag of this type.
4978 if Nb_Prim = 0 then
4979 DT_Constr_List :=
4980 New_List (Make_Integer_Literal (Loc, 1));
4981 else
4982 DT_Constr_List :=
4983 New_List (Make_Integer_Literal (Loc, Nb_Prim));
4984 end if;
4986 Append_To (Result,
4987 Make_Object_Declaration (Loc,
4988 Defining_Identifier => DT,
4989 Aliased_Present => True,
4990 Constant_Present => False,
4991 Object_Definition =>
4992 Make_Subtype_Indication (Loc,
4993 Subtype_Mark =>
4994 New_Occurrence_Of (RTE (RE_Dispatch_Table_Wrapper), Loc),
4995 Constraint =>
4996 Make_Index_Or_Discriminant_Constraint (Loc,
4997 Constraints => DT_Constr_List))));
4999 Append_To (Result,
5000 Make_Object_Declaration (Loc,
5001 Defining_Identifier => DT_Ptr,
5002 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
5003 Constant_Present => True,
5004 Expression =>
5005 Unchecked_Convert_To (RTE (RE_Tag),
5006 Make_Attribute_Reference (Loc,
5007 Prefix =>
5008 Make_Selected_Component (Loc,
5009 Prefix => New_Occurrence_Of (DT, Loc),
5010 Selector_Name =>
5011 New_Occurrence_Of
5012 (RTE_Record_Component (RE_Prims_Ptr), Loc)),
5013 Attribute_Name => Name_Address))));
5015 Set_Is_Statically_Allocated (DT_Ptr,
5016 Is_Library_Level_Tagged_Type (Typ));
5018 -- Generate the SCIL node for the previous object declaration
5019 -- because it has a tag initialization.
5021 if Generate_SCIL then
5022 New_Node :=
5023 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
5024 Set_SCIL_Entity (New_Node, Typ);
5025 Set_SCIL_Node (Last (Result), New_Node);
5027 goto Leave_SCIL;
5029 -- Gnat2scil has its own implementation of dispatch tables,
5030 -- different than what is being implemented here. Generating
5031 -- further dispatch table initialization code would just
5032 -- cause gnat2scil to generate useless Scil which CodePeer
5033 -- would waste time and space analyzing, so we skip it.
5034 end if;
5036 Append_To (Result,
5037 Make_Object_Declaration (Loc,
5038 Defining_Identifier =>
5039 Node (Next_Elmt (First_Elmt (Access_Disp_Table (Typ)))),
5040 Constant_Present => True,
5041 Object_Definition =>
5042 New_Occurrence_Of (RTE (RE_Address), Loc),
5043 Expression =>
5044 Make_Attribute_Reference (Loc,
5045 Prefix =>
5046 Make_Selected_Component (Loc,
5047 Prefix => New_Occurrence_Of (DT, Loc),
5048 Selector_Name =>
5049 New_Occurrence_Of
5050 (RTE_Record_Component (RE_Predef_Prims), Loc)),
5051 Attribute_Name => Name_Address)));
5052 end if;
5053 end if;
5055 -- Generate: Expanded_Name : constant String := "";
5057 if Discard_Names then
5058 Append_To (Result,
5059 Make_Object_Declaration (Loc,
5060 Defining_Identifier => Exname,
5061 Constant_Present => True,
5062 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
5063 Expression =>
5064 Make_String_Literal (Loc, "")));
5066 -- Generate: Exname : constant String := full_qualified_name (typ);
5067 -- The type itself may be an anonymous parent type, so use the first
5068 -- subtype to have a user-recognizable name.
5070 else
5071 Append_To (Result,
5072 Make_Object_Declaration (Loc,
5073 Defining_Identifier => Exname,
5074 Constant_Present => True,
5075 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
5076 Expression =>
5077 Make_String_Literal (Loc,
5078 Fully_Qualified_Name_String (First_Subtype (Typ)))));
5079 end if;
5081 Set_Is_Statically_Allocated (Exname);
5082 Set_Is_True_Constant (Exname);
5084 -- Declare the object used by Ada.Tags.Register_Tag, unless
5085 -- No_Tagged_Type_Registration is active.
5087 if not Restriction_Active (No_Tagged_Type_Registration)
5088 and then RTE_Available (RE_Register_Tag)
5089 then
5090 Append_To (Result,
5091 Make_Object_Declaration (Loc,
5092 Defining_Identifier => HT_Link,
5093 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
5094 Expression => New_Occurrence_Of (RTE (RE_No_Tag), Loc)));
5095 end if;
5097 -- Generate code to create the storage for the type specific data object
5098 -- with enough space to store the tags of the ancestors plus the tags
5099 -- of all the implemented interfaces (as described in a-tags.adb).
5101 -- TSD : Type_Specific_Data (I_Depth) :=
5102 -- (Idepth => I_Depth,
5103 -- Access_Level => Type_Access_Level (Typ),
5104 -- Alignment => Typ'Alignment,
5105 -- Expanded_Name => Cstring_Ptr!(Exname'Address))
5106 -- External_Tag => Cstring_Ptr!(Exname'Address))
5107 -- HT_Link => HT_Link'Address,
5108 -- Transportable => <<boolean-value>>,
5109 -- Is_Abstract => <<boolean-value>>,
5110 -- Needs_Finalization => <<boolean-value>>,
5111 -- [ Size_Func => Size_Prim'Access, ]
5112 -- [ Interfaces_Table => <<access-value>>, ]
5113 -- [ SSD => SSD_Table'Address ]
5114 -- Tags_Table => (0 => null,
5115 -- 1 => Parent'Tag
5116 -- ...);
5118 TSD_Aggr_List := New_List;
5120 -- Idepth: Count ancestors to compute the inheritance depth. For private
5121 -- extensions, always go to the full view in order to compute the real
5122 -- inheritance depth.
5124 declare
5125 Current_Typ : Entity_Id;
5126 Parent_Typ : Entity_Id;
5128 begin
5129 I_Depth := 0;
5130 Current_Typ := Typ;
5131 loop
5132 Parent_Typ := Etype (Current_Typ);
5134 if Is_Private_Type (Parent_Typ) then
5135 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5136 end if;
5138 exit when Parent_Typ = Current_Typ;
5140 I_Depth := I_Depth + 1;
5141 Current_Typ := Parent_Typ;
5142 end loop;
5143 end;
5145 Append_To (TSD_Aggr_List,
5146 Make_Integer_Literal (Loc, I_Depth));
5148 -- Access_Level
5150 Append_To (TSD_Aggr_List,
5151 Make_Integer_Literal (Loc, Type_Access_Level (Typ)));
5153 -- Alignment
5155 -- For CPP types we cannot rely on the value of 'Alignment provided
5156 -- by the backend to initialize this TSD field.
5158 if Convention (Typ) = Convention_CPP
5159 or else Is_CPP_Class (Root_Type (Typ))
5160 then
5161 Append_To (TSD_Aggr_List,
5162 Make_Integer_Literal (Loc, 0));
5163 else
5164 Append_To (TSD_Aggr_List,
5165 Make_Attribute_Reference (Loc,
5166 Prefix => New_Occurrence_Of (Typ, Loc),
5167 Attribute_Name => Name_Alignment));
5168 end if;
5170 -- Expanded_Name
5172 Append_To (TSD_Aggr_List,
5173 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5174 Make_Attribute_Reference (Loc,
5175 Prefix => New_Occurrence_Of (Exname, Loc),
5176 Attribute_Name => Name_Address)));
5178 -- External_Tag of a local tagged type
5180 -- <typ>A : constant String :=
5181 -- "Internal tag at 16#tag-addr#: <full-name-of-typ>";
5183 -- The reason we generate this strange name is that we do not want to
5184 -- enter local tagged types in the global hash table used to compute
5185 -- the Internal_Tag attribute for two reasons:
5187 -- 1. It is hard to avoid a tasking race condition for entering the
5188 -- entry into the hash table.
5190 -- 2. It would cause a storage leak, unless we rig up considerable
5191 -- mechanism to remove the entry from the hash table on exit.
5193 -- So what we do is to generate the above external tag name, where the
5194 -- hex address is the address of the local dispatch table (i.e. exactly
5195 -- the value we want if Internal_Tag is computed from this string).
5197 -- Of course this value will only be valid if the tagged type is still
5198 -- in scope, but it clearly must be erroneous to compute the internal
5199 -- tag of a tagged type that is out of scope.
5201 -- We don't do this processing if an explicit external tag has been
5202 -- specified. That's an odd case for which we have already issued a
5203 -- warning, where we will not be able to compute the internal tag.
5205 if not Discard_Names
5206 and then not Is_Library_Level_Entity (Typ)
5207 and then not Has_External_Tag_Rep_Clause (Typ)
5208 then
5209 declare
5210 Exname : constant Entity_Id :=
5211 Make_Defining_Identifier (Loc,
5212 Chars => New_External_Name (Tname, 'A'));
5213 Full_Name : constant String_Id :=
5214 Fully_Qualified_Name_String (First_Subtype (Typ));
5215 Str1_Id : String_Id;
5216 Str2_Id : String_Id;
5218 begin
5219 -- Generate:
5220 -- Str1 = "Internal tag at 16#";
5222 Start_String;
5223 Store_String_Chars ("Internal tag at 16#");
5224 Str1_Id := End_String;
5226 -- Generate:
5227 -- Str2 = "#: <type-full-name>";
5229 Start_String;
5230 Store_String_Chars ("#: ");
5231 Store_String_Chars (Full_Name);
5232 Str2_Id := End_String;
5234 -- Generate:
5235 -- Exname : constant String :=
5236 -- Str1 & Address_Image (Tag) & Str2;
5238 if RTE_Available (RE_Address_Image) then
5239 Append_To (Result,
5240 Make_Object_Declaration (Loc,
5241 Defining_Identifier => Exname,
5242 Constant_Present => True,
5243 Object_Definition => New_Occurrence_Of
5244 (Standard_String, Loc),
5245 Expression =>
5246 Make_Op_Concat (Loc,
5247 Left_Opnd => Make_String_Literal (Loc, Str1_Id),
5248 Right_Opnd =>
5249 Make_Op_Concat (Loc,
5250 Left_Opnd =>
5251 Make_Function_Call (Loc,
5252 Name =>
5253 New_Occurrence_Of
5254 (RTE (RE_Address_Image), Loc),
5255 Parameter_Associations => New_List (
5256 Unchecked_Convert_To (RTE (RE_Address),
5257 New_Occurrence_Of (DT_Ptr, Loc)))),
5258 Right_Opnd =>
5259 Make_String_Literal (Loc, Str2_Id)))));
5261 -- Generate:
5262 -- Exname : constant String := Str1 & Str2;
5264 else
5265 Append_To (Result,
5266 Make_Object_Declaration (Loc,
5267 Defining_Identifier => Exname,
5268 Constant_Present => True,
5269 Object_Definition =>
5270 New_Occurrence_Of (Standard_String, Loc),
5271 Expression =>
5272 Make_Op_Concat (Loc,
5273 Left_Opnd => Make_String_Literal (Loc, Str1_Id),
5274 Right_Opnd => Make_String_Literal (Loc, Str2_Id))));
5275 end if;
5277 New_Node :=
5278 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5279 Make_Attribute_Reference (Loc,
5280 Prefix => New_Occurrence_Of (Exname, Loc),
5281 Attribute_Name => Name_Address));
5282 end;
5284 -- External tag of a library-level tagged type: Check for a definition
5285 -- of External_Tag. The clause is considered only if it applies to this
5286 -- specific tagged type, as opposed to one of its ancestors.
5287 -- If the type is an unconstrained type extension, we are building the
5288 -- dispatch table of its anonymous base type, so the external tag, if
5289 -- any was specified, must be retrieved from the first subtype. Go to
5290 -- the full view in case the clause is in the private part.
5292 else
5293 declare
5294 Def : constant Node_Id := Get_Attribute_Definition_Clause
5295 (Underlying_Type (First_Subtype (Typ)),
5296 Attribute_External_Tag);
5298 Old_Val : String_Id;
5299 New_Val : String_Id;
5300 E : Entity_Id;
5302 begin
5303 if No (Def)
5304 or else Entity (Name (Def)) /= First_Subtype (Typ)
5305 then
5306 New_Node :=
5307 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5308 Make_Attribute_Reference (Loc,
5309 Prefix => New_Occurrence_Of (Exname, Loc),
5310 Attribute_Name => Name_Address));
5311 else
5312 Old_Val := Strval (Expr_Value_S (Expression (Def)));
5314 -- For the rep clause "for <typ>'external_tag use y" generate:
5316 -- <typ>A : constant string := y;
5318 -- <typ>A'Address is used to set the External_Tag component
5319 -- of the TSD
5321 -- Create a new nul terminated string if it is not already
5323 if String_Length (Old_Val) > 0
5324 and then
5325 Get_String_Char (Old_Val, String_Length (Old_Val)) = 0
5326 then
5327 New_Val := Old_Val;
5328 else
5329 Start_String (Old_Val);
5330 Store_String_Char (Get_Char_Code (ASCII.NUL));
5331 New_Val := End_String;
5332 end if;
5334 E := Make_Defining_Identifier (Loc,
5335 New_External_Name (Chars (Typ), 'A'));
5337 Append_To (Result,
5338 Make_Object_Declaration (Loc,
5339 Defining_Identifier => E,
5340 Constant_Present => True,
5341 Object_Definition =>
5342 New_Occurrence_Of (Standard_String, Loc),
5343 Expression =>
5344 Make_String_Literal (Loc, New_Val)));
5346 New_Node :=
5347 Unchecked_Convert_To (RTE (RE_Cstring_Ptr),
5348 Make_Attribute_Reference (Loc,
5349 Prefix => New_Occurrence_Of (E, Loc),
5350 Attribute_Name => Name_Address));
5351 end if;
5352 end;
5353 end if;
5355 Append_To (TSD_Aggr_List, New_Node);
5357 -- HT_Link
5359 if not Restriction_Active (No_Tagged_Type_Registration)
5360 and then RTE_Available (RE_Register_Tag)
5361 then
5362 Append_To (TSD_Aggr_List,
5363 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5364 Make_Attribute_Reference (Loc,
5365 Prefix => New_Occurrence_Of (HT_Link, Loc),
5366 Attribute_Name => Name_Address)));
5368 elsif RTE_Record_Component_Available (RE_HT_Link) then
5369 Append_To (TSD_Aggr_List,
5370 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5371 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5372 end if;
5374 -- Transportable: Set for types that can be used in remote calls
5375 -- with respect to E.4(18) legality rules.
5377 declare
5378 Transportable : Entity_Id;
5380 begin
5381 Transportable :=
5382 Boolean_Literals
5383 (Is_Pure (Typ)
5384 or else Is_Shared_Passive (Typ)
5385 or else
5386 ((Is_Remote_Types (Typ)
5387 or else Is_Remote_Call_Interface (Typ))
5388 and then Original_View_In_Visible_Part (Typ))
5389 or else not Comes_From_Source (Typ));
5391 Append_To (TSD_Aggr_List,
5392 New_Occurrence_Of (Transportable, Loc));
5393 end;
5395 -- Is_Abstract (Ada 2012: AI05-0173). This functionality is not
5396 -- available in the HIE runtime.
5398 if RTE_Record_Component_Available (RE_Is_Abstract) then
5399 declare
5400 Is_Abstract : Entity_Id;
5401 begin
5402 Is_Abstract := Boolean_Literals (Is_Abstract_Type (Typ));
5403 Append_To (TSD_Aggr_List,
5404 New_Occurrence_Of (Is_Abstract, Loc));
5405 end;
5406 end if;
5408 -- Needs_Finalization: Set if the type is controlled or has controlled
5409 -- components.
5411 declare
5412 Needs_Fin : Entity_Id;
5413 begin
5414 Needs_Fin := Boolean_Literals (Needs_Finalization (Typ));
5415 Append_To (TSD_Aggr_List, New_Occurrence_Of (Needs_Fin, Loc));
5416 end;
5418 -- Size_Func
5420 if RTE_Record_Component_Available (RE_Size_Func) then
5422 -- Initialize this field to Null_Address if we are not building
5423 -- static dispatch tables static or if the size function is not
5424 -- available. In the former case we cannot initialize this field
5425 -- until the function is frozen and registered in the dispatch
5426 -- table (see Register_Primitive).
5428 if not Building_Static_DT (Typ) or else not Has_DT (Typ) then
5429 Append_To (TSD_Aggr_List,
5430 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5431 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5433 else
5434 declare
5435 Prim_Elmt : Elmt_Id;
5436 Prim : Entity_Id;
5437 Size_Comp : Node_Id := Empty;
5439 begin
5440 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5441 while Present (Prim_Elmt) loop
5442 Prim := Node (Prim_Elmt);
5444 if Chars (Prim) = Name_uSize then
5445 Prim := Ultimate_Alias (Prim);
5447 if Is_Abstract_Subprogram (Prim) then
5448 Size_Comp :=
5449 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5450 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
5451 else
5452 Size_Comp :=
5453 Unchecked_Convert_To (RTE (RE_Size_Ptr),
5454 Make_Attribute_Reference (Loc,
5455 Prefix => New_Occurrence_Of (Prim, Loc),
5456 Attribute_Name => Name_Unrestricted_Access));
5457 end if;
5459 exit;
5460 end if;
5462 Next_Elmt (Prim_Elmt);
5463 end loop;
5465 pragma Assert (Present (Size_Comp));
5466 Append_To (TSD_Aggr_List, Size_Comp);
5467 end;
5468 end if;
5469 end if;
5471 -- Interfaces_Table (required for AI-405)
5473 if RTE_Record_Component_Available (RE_Interfaces_Table) then
5475 -- Count the number of interface types implemented by Typ
5477 Collect_Interfaces (Typ, Typ_Ifaces);
5479 AI := First_Elmt (Typ_Ifaces);
5480 while Present (AI) loop
5481 Num_Ifaces := Num_Ifaces + 1;
5482 Next_Elmt (AI);
5483 end loop;
5485 if Num_Ifaces = 0 then
5486 Iface_Table_Node := Make_Null (Loc);
5488 -- Generate the Interface_Table object
5490 else
5491 declare
5492 TSD_Ifaces_List : constant List_Id := New_List;
5493 Elmt : Elmt_Id;
5494 Offset_To_Top : Node_Id;
5495 Sec_DT_Tag : Node_Id;
5497 Dummy_Object_Ifaces_List : Elist_Id := No_Elist;
5498 Dummy_Object_Ifaces_Comp_List : Elist_Id := No_Elist;
5499 Dummy_Object_Ifaces_Tag_List : Elist_Id := No_Elist;
5500 -- Interfaces information of the dummy object
5502 begin
5503 -- Collect interfaces information if we need to compute the
5504 -- offset to the top using the dummy object.
5506 if Present (Dummy_Object) then
5507 Collect_Interfaces_Info (Typ,
5508 Ifaces_List => Dummy_Object_Ifaces_List,
5509 Components_List => Dummy_Object_Ifaces_Comp_List,
5510 Tags_List => Dummy_Object_Ifaces_Tag_List);
5511 end if;
5513 AI := First_Elmt (Typ_Ifaces);
5514 while Present (AI) loop
5515 if Is_Ancestor (Node (AI), Typ, Use_Full_View => True) then
5516 Sec_DT_Tag := New_Occurrence_Of (DT_Ptr, Loc);
5518 else
5519 Elmt :=
5520 Next_Elmt
5521 (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5522 pragma Assert (Has_Thunks (Node (Elmt)));
5524 while Is_Tag (Node (Elmt))
5525 and then not
5526 Is_Ancestor (Node (AI), Related_Type (Node (Elmt)),
5527 Use_Full_View => True)
5528 loop
5529 pragma Assert (Has_Thunks (Node (Elmt)));
5530 Next_Elmt (Elmt);
5531 pragma Assert (Has_Thunks (Node (Elmt)));
5532 Next_Elmt (Elmt);
5533 pragma Assert (not Has_Thunks (Node (Elmt)));
5534 Next_Elmt (Elmt);
5535 pragma Assert (not Has_Thunks (Node (Elmt)));
5536 Next_Elmt (Elmt);
5537 end loop;
5539 pragma Assert (Ekind (Node (Elmt)) = E_Constant
5540 and then not
5541 Has_Thunks (Node (Next_Elmt (Next_Elmt (Elmt)))));
5543 Sec_DT_Tag :=
5544 New_Occurrence_Of
5545 (Node (Next_Elmt (Next_Elmt (Elmt))), Loc);
5546 end if;
5548 -- Use the dummy object to compute Offset_To_Top of
5549 -- components located at fixed position.
5551 if Present (Dummy_Object) then
5552 declare
5553 Iface : constant Node_Id := Node (AI);
5554 Iface_Comp : Node_Id := Empty;
5555 Iface_Comp_Elmt : Elmt_Id;
5556 Iface_Elmt : Elmt_Id;
5558 begin
5559 Iface_Elmt :=
5560 First_Elmt (Dummy_Object_Ifaces_List);
5561 Iface_Comp_Elmt :=
5562 First_Elmt (Dummy_Object_Ifaces_Comp_List);
5564 while Present (Iface_Elmt) loop
5565 if Node (Iface_Elmt) = Iface then
5566 Iface_Comp := Node (Iface_Comp_Elmt);
5567 exit;
5568 end if;
5570 Next_Elmt (Iface_Elmt);
5571 Next_Elmt (Iface_Comp_Elmt);
5572 end loop;
5574 pragma Assert (Present (Iface_Comp));
5576 if not
5577 Is_Variable_Size_Record (Etype (Scope (Iface_Comp)))
5578 then
5579 Offset_To_Top :=
5580 Make_Op_Minus (Loc,
5581 Make_Attribute_Reference (Loc,
5582 Prefix =>
5583 Make_Selected_Component (Loc,
5584 Prefix =>
5585 New_Occurrence_Of (Dummy_Object, Loc),
5586 Selector_Name =>
5587 New_Occurrence_Of (Iface_Comp, Loc)),
5588 Attribute_Name => Name_Position));
5589 else
5590 Offset_To_Top := Make_Integer_Literal (Loc, 0);
5591 end if;
5592 end;
5593 else
5594 Offset_To_Top := Make_Integer_Literal (Loc, 0);
5595 end if;
5597 Append_To (TSD_Ifaces_List,
5598 Make_Aggregate (Loc,
5599 Expressions => New_List (
5601 -- Iface_Tag
5603 Unchecked_Convert_To (RTE (RE_Tag),
5604 New_Occurrence_Of
5605 (Node (First_Elmt (Access_Disp_Table (Node (AI)))),
5606 Loc)),
5608 -- Static_Offset_To_Top
5610 New_Occurrence_Of (Standard_True, Loc),
5612 -- Offset_To_Top_Value
5614 Offset_To_Top,
5616 -- Offset_To_Top_Func
5618 Make_Null (Loc),
5620 -- Secondary_DT
5622 Unchecked_Convert_To (RTE (RE_Tag), Sec_DT_Tag))));
5624 Next_Elmt (AI);
5625 end loop;
5627 Name_ITable := New_External_Name (Tname, 'I');
5628 ITable := Make_Defining_Identifier (Loc, Name_ITable);
5629 Set_Is_Statically_Allocated (ITable,
5630 Is_Library_Level_Tagged_Type (Typ));
5632 -- The table of interfaces is constant if we are building a
5633 -- static dispatch table; otherwise is not constant because
5634 -- its slots are filled at run time by the IP routine.
5636 Append_To (Result,
5637 Make_Object_Declaration (Loc,
5638 Defining_Identifier => ITable,
5639 Aliased_Present => True,
5640 Constant_Present => Building_Static_Secondary_DT (Typ),
5641 Object_Definition =>
5642 Make_Subtype_Indication (Loc,
5643 Subtype_Mark =>
5644 New_Occurrence_Of (RTE (RE_Interface_Data), Loc),
5645 Constraint =>
5646 Make_Index_Or_Discriminant_Constraint (Loc,
5647 Constraints => New_List (
5648 Make_Integer_Literal (Loc, Num_Ifaces)))),
5650 Expression =>
5651 Make_Aggregate (Loc,
5652 Expressions => New_List (
5653 Make_Integer_Literal (Loc, Num_Ifaces),
5654 Make_Aggregate (Loc, TSD_Ifaces_List)))));
5656 Iface_Table_Node :=
5657 Make_Attribute_Reference (Loc,
5658 Prefix => New_Occurrence_Of (ITable, Loc),
5659 Attribute_Name => Name_Unchecked_Access);
5660 end;
5661 end if;
5663 Append_To (TSD_Aggr_List, Iface_Table_Node);
5664 end if;
5666 -- Generate the Select Specific Data table for synchronized types that
5667 -- implement synchronized interfaces. The size of the table is
5668 -- constrained by the number of non-predefined primitive operations.
5670 if RTE_Record_Component_Available (RE_SSD) then
5671 if Ada_Version >= Ada_2005
5672 and then Has_DT (Typ)
5673 and then Is_Concurrent_Record_Type (Typ)
5674 and then Has_Interfaces (Typ)
5675 and then Nb_Prim > 0
5676 and then not Is_Abstract_Type (Typ)
5677 and then not Is_Controlled (Typ)
5678 and then not Restriction_Active (No_Dispatching_Calls)
5679 and then not Restriction_Active (No_Select_Statements)
5680 then
5681 Append_To (Result,
5682 Make_Object_Declaration (Loc,
5683 Defining_Identifier => SSD,
5684 Aliased_Present => True,
5685 Object_Definition =>
5686 Make_Subtype_Indication (Loc,
5687 Subtype_Mark => New_Occurrence_Of (
5688 RTE (RE_Select_Specific_Data), Loc),
5689 Constraint =>
5690 Make_Index_Or_Discriminant_Constraint (Loc,
5691 Constraints => New_List (
5692 Make_Integer_Literal (Loc, Nb_Prim))))));
5694 Append_To (Result,
5695 Make_Attribute_Definition_Clause (Loc,
5696 Name => New_Occurrence_Of (SSD, Loc),
5697 Chars => Name_Alignment,
5698 Expression =>
5699 Make_Attribute_Reference (Loc,
5700 Prefix =>
5701 New_Occurrence_Of (RTE (RE_Integer_Address), Loc),
5702 Attribute_Name => Name_Alignment)));
5704 -- This table is initialized by Make_Select_Specific_Data_Table,
5705 -- which calls Set_Entry_Index and Set_Prim_Op_Kind.
5707 Append_To (TSD_Aggr_List,
5708 Make_Attribute_Reference (Loc,
5709 Prefix => New_Occurrence_Of (SSD, Loc),
5710 Attribute_Name => Name_Unchecked_Access));
5711 else
5712 Append_To (TSD_Aggr_List, Make_Null (Loc));
5713 end if;
5714 end if;
5716 -- Initialize the table of ancestor tags. In case of interface types
5717 -- this table is not needed.
5719 TSD_Tags_List := New_List;
5721 -- If we are not statically allocating the dispatch table then we must
5722 -- fill position 0 with null because we still have not generated the
5723 -- tag of Typ.
5725 if not Building_Static_DT (Typ)
5726 or else Is_Interface (Typ)
5727 then
5728 Append_To (TSD_Tags_List,
5729 Unchecked_Convert_To (RTE (RE_Tag),
5730 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5732 -- Otherwise we can safely reference the tag
5734 else
5735 Append_To (TSD_Tags_List,
5736 New_Occurrence_Of (DT_Ptr, Loc));
5737 end if;
5739 -- Fill the rest of the table with the tags of the ancestors
5741 declare
5742 Current_Typ : Entity_Id;
5743 Parent_Typ : Entity_Id;
5744 Pos : Nat;
5746 begin
5747 Pos := 1;
5748 Current_Typ := Typ;
5750 loop
5751 Parent_Typ := Etype (Current_Typ);
5753 if Is_Private_Type (Parent_Typ) then
5754 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5755 end if;
5757 exit when Parent_Typ = Current_Typ;
5759 if Is_CPP_Class (Parent_Typ) then
5761 -- The tags defined in the C++ side will be inherited when
5762 -- the object is constructed (Exp_Ch3.Build_Init_Procedure)
5764 Append_To (TSD_Tags_List,
5765 Unchecked_Convert_To (RTE (RE_Tag),
5766 New_Occurrence_Of (RTE (RE_Null_Address), Loc)));
5767 else
5768 Append_To (TSD_Tags_List,
5769 New_Occurrence_Of
5770 (Node (First_Elmt (Access_Disp_Table (Parent_Typ))),
5771 Loc));
5772 end if;
5774 Pos := Pos + 1;
5775 Current_Typ := Parent_Typ;
5776 end loop;
5778 pragma Assert (Pos = I_Depth + 1);
5779 end;
5781 Append_To (TSD_Aggr_List,
5782 Make_Aggregate (Loc,
5783 Expressions => TSD_Tags_List));
5785 -- Build the TSD object
5787 Append_To (Result,
5788 Make_Object_Declaration (Loc,
5789 Defining_Identifier => TSD,
5790 Aliased_Present => True,
5791 Constant_Present => Building_Static_DT (Typ),
5792 Object_Definition =>
5793 Make_Subtype_Indication (Loc,
5794 Subtype_Mark => New_Occurrence_Of (
5795 RTE (RE_Type_Specific_Data), Loc),
5796 Constraint =>
5797 Make_Index_Or_Discriminant_Constraint (Loc,
5798 Constraints => New_List (
5799 Make_Integer_Literal (Loc, I_Depth)))),
5801 Expression => Make_Aggregate (Loc,
5802 Expressions => TSD_Aggr_List)));
5804 Set_Is_True_Constant (TSD, Building_Static_DT (Typ));
5806 -- The debugging information for type Ada.Tags.Type_Specific_Data is
5807 -- needed by the debugger in order to display values of tagged types.
5809 Set_Needs_Debug_Info (TSD, Needs_Debug_Info (Typ));
5811 -- Initialize or declare the dispatch table object
5813 if not Has_DT (Typ) then
5814 DT_Constr_List := New_List;
5815 DT_Aggr_List := New_List;
5817 -- Typeinfo
5819 New_Node :=
5820 Make_Attribute_Reference (Loc,
5821 Prefix => New_Occurrence_Of (TSD, Loc),
5822 Attribute_Name => Name_Address);
5824 Append_To (DT_Constr_List, New_Node);
5825 Append_To (DT_Aggr_List, New_Copy (New_Node));
5826 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, 0));
5828 -- In case of locally defined tagged types we have already declared
5829 -- and uninitialized object for the dispatch table, which is now
5830 -- initialized by means of the following assignment:
5832 -- DT := (TSD'Address, 0);
5834 if not Building_Static_DT (Typ) then
5835 Append_To (Result,
5836 Make_Assignment_Statement (Loc,
5837 Name => New_Occurrence_Of (DT, Loc),
5838 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
5840 -- In case of library level tagged types we declare and export now
5841 -- the constant object containing the dummy dispatch table. There
5842 -- is no need to declare the tag here because it has been previously
5843 -- declared by Make_Tags
5845 -- DT : aliased constant No_Dispatch_Table :=
5846 -- (NDT_TSD => TSD'Address;
5847 -- NDT_Prims_Ptr => 0);
5849 else
5850 Append_To (Result,
5851 Make_Object_Declaration (Loc,
5852 Defining_Identifier => DT,
5853 Aliased_Present => True,
5854 Constant_Present => True,
5855 Object_Definition =>
5856 New_Occurrence_Of (RTE (RE_No_Dispatch_Table_Wrapper), Loc),
5857 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
5859 Export_DT (Typ, DT);
5860 end if;
5862 -- Common case: Typ has a dispatch table
5864 -- Generate:
5866 -- Predef_Prims : Address_Array (1 .. Default_Prim_Ops_Count) :=
5867 -- (predef-prim-op-1'address,
5868 -- predef-prim-op-2'address,
5869 -- ...
5870 -- predef-prim-op-n'address);
5872 -- DT : Dispatch_Table (Nb_Prims) :=
5873 -- (Signature => <sig-value>,
5874 -- Tag_Kind => <tag_kind-value>,
5875 -- Predef_Prims => Predef_Prims'First'Address,
5876 -- Offset_To_Top => 0,
5877 -- TSD => TSD'Address;
5878 -- Prims_Ptr => (prim-op-1'address,
5879 -- prim-op-2'address,
5880 -- ...
5881 -- prim-op-n'address));
5882 -- for DT'Alignment use Address'Alignment
5884 else
5885 declare
5886 Nb_P_Prims : constant Nat := Number_Of_Predefined_Prims (Typ);
5887 Prim_Table : array (Nat range 1 .. Nb_P_Prims) of Entity_Id;
5888 Decl : Node_Id;
5889 E : Entity_Id;
5890 SS_Thunk_Id : Entity_Id;
5891 SS_Thunk_Code : Node_Id;
5893 begin
5894 Prim_Ops_Aggr_List := New_List;
5895 Prim_Table := (others => Empty);
5897 if Building_Static_DT (Typ) then
5898 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5899 while Present (Prim_Elmt) loop
5900 Prim := Node (Prim_Elmt);
5902 if Is_Predefined_Dispatching_Operation (Prim)
5903 and then not Is_Abstract_Subprogram (Prim)
5904 and then not Is_Eliminated (Prim)
5905 and then not Generate_SCIL
5906 and then No (Prim_Table (UI_To_Int (DT_Position (Prim))))
5907 then
5908 E := Ultimate_Alias (Prim);
5909 pragma Assert (not Is_Abstract_Subprogram (E));
5911 Expand_Secondary_Stack_Thunk
5912 (E, SS_Thunk_Id, SS_Thunk_Code);
5914 if Present (SS_Thunk_Id) then
5915 E := SS_Thunk_Id;
5916 Append_To (Result, SS_Thunk_Code);
5917 end if;
5919 Prim_Table (UI_To_Int (DT_Position (Prim))) := E;
5920 end if;
5922 Next_Elmt (Prim_Elmt);
5923 end loop;
5924 end if;
5926 for J in Prim_Table'Range loop
5927 if Present (Prim_Table (J)) then
5928 New_Node :=
5929 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
5930 Make_Attribute_Reference (Loc,
5931 Prefix =>
5932 New_Occurrence_Of (Prim_Table (J), Loc),
5933 Attribute_Name => Name_Unrestricted_Access));
5934 else
5935 New_Node := Make_Null (Loc);
5936 end if;
5938 Append_To (Prim_Ops_Aggr_List, New_Node);
5939 end loop;
5941 New_Node :=
5942 Make_Aggregate (Loc,
5943 Expressions => Prim_Ops_Aggr_List);
5945 Decl :=
5946 Make_Subtype_Declaration (Loc,
5947 Defining_Identifier => Make_Temporary (Loc, 'S'),
5948 Subtype_Indication =>
5949 New_Occurrence_Of (RTE (RE_Address_Array), Loc));
5951 Append_To (Result, Decl);
5953 Append_To (Result,
5954 Make_Object_Declaration (Loc,
5955 Defining_Identifier => Predef_Prims,
5956 Aliased_Present => True,
5957 Constant_Present => Building_Static_DT (Typ),
5958 Object_Definition =>
5959 New_Occurrence_Of (Defining_Identifier (Decl), Loc),
5960 Expression => New_Node));
5962 -- Remember aggregates initializing dispatch tables
5964 Append_Elmt (New_Node, DT_Aggr);
5965 end;
5967 -- Stage 1: Initialize the discriminant and the record components
5969 DT_Constr_List := New_List;
5970 DT_Aggr_List := New_List;
5972 -- Num_Prims. If the tagged type has no primitives we add a dummy
5973 -- slot whose address will be the tag of this type.
5975 if Nb_Prim = 0 then
5976 New_Node := Make_Integer_Literal (Loc, 1);
5977 else
5978 New_Node := Make_Integer_Literal (Loc, Nb_Prim);
5979 end if;
5981 Append_To (DT_Constr_List, New_Node);
5982 Append_To (DT_Aggr_List, New_Copy (New_Node));
5984 -- Signature
5986 if RTE_Record_Component_Available (RE_Signature) then
5987 Append_To (DT_Aggr_List,
5988 New_Occurrence_Of (RTE (RE_Primary_DT), Loc));
5989 end if;
5991 -- Tag_Kind
5993 if RTE_Record_Component_Available (RE_Tag_Kind) then
5994 Append_To (DT_Aggr_List, Tagged_Kind (Typ));
5995 end if;
5997 -- Predef_Prims
5999 Append_To (DT_Aggr_List,
6000 Make_Attribute_Reference (Loc,
6001 Prefix => New_Occurrence_Of (Predef_Prims, Loc),
6002 Attribute_Name => Name_Address));
6004 -- Offset_To_Top
6006 Append_To (DT_Aggr_List, Make_Integer_Literal (Loc, 0));
6008 -- Typeinfo
6010 Append_To (DT_Aggr_List,
6011 Make_Attribute_Reference (Loc,
6012 Prefix => New_Occurrence_Of (TSD, Loc),
6013 Attribute_Name => Name_Address));
6015 -- Stage 2: Initialize the table of user-defined primitive operations
6017 Prim_Ops_Aggr_List := New_List;
6019 if Nb_Prim = 0 then
6020 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
6022 elsif not Building_Static_DT (Typ) then
6023 for J in 1 .. Nb_Prim loop
6024 Append_To (Prim_Ops_Aggr_List, Make_Null (Loc));
6025 end loop;
6027 else
6028 declare
6029 CPP_Nb_Prims : constant Nat := CPP_Num_Prims (Typ);
6030 E : Entity_Id;
6031 Prim : Entity_Id;
6032 Prim_Elmt : Elmt_Id;
6033 Prim_Pos : Nat;
6034 Prim_Table : array (Nat range 1 .. Nb_Prim) of Entity_Id;
6035 SS_Thunk_Id : Entity_Id;
6036 SS_Thunk_Code : Node_Id;
6038 begin
6039 Prim_Table := (others => Empty);
6041 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6042 while Present (Prim_Elmt) loop
6043 Prim := Node (Prim_Elmt);
6045 -- Retrieve the ultimate alias of the primitive for proper
6046 -- handling of renamings and eliminated primitives.
6048 E := Ultimate_Alias (Prim);
6050 -- If the alias is not a primitive operation then Prim does
6051 -- not rename another primitive, but rather an operation
6052 -- declared elsewhere (e.g. in another scope) and therefore
6053 -- Prim is a new primitive.
6055 if No (Find_Dispatching_Type (E)) then
6056 E := Prim;
6057 end if;
6059 Prim_Pos := UI_To_Int (DT_Position (E));
6061 -- Skip predefined primitives because they are located in a
6062 -- separate dispatch table.
6064 if not Is_Predefined_Dispatching_Operation (Prim)
6065 and then not Is_Predefined_Dispatching_Operation (E)
6067 -- Skip entities with attribute Interface_Alias because
6068 -- those are only required to build secondary dispatch
6069 -- tables.
6071 and then No (Interface_Alias (Prim))
6073 -- Skip abstract and eliminated primitives
6075 and then not Is_Abstract_Subprogram (E)
6076 and then not Is_Eliminated (E)
6078 -- For derivations of CPP types skip primitives located in
6079 -- the C++ part of the dispatch table because their slots
6080 -- are initialized by the IC routine.
6082 and then (not Is_CPP_Class (Root_Type (Typ))
6083 or else Prim_Pos > CPP_Nb_Prims)
6085 -- Skip ignored Ghost subprograms as those will be removed
6086 -- from the executable.
6088 and then not Is_Ignored_Ghost_Entity (E)
6089 then
6090 pragma Assert
6091 (UI_To_Int (DT_Position (Prim)) <= Nb_Prim);
6093 Expand_Secondary_Stack_Thunk
6094 (E, SS_Thunk_Id, SS_Thunk_Code);
6096 if Present (SS_Thunk_Id) then
6097 E := SS_Thunk_Id;
6098 Append_To (Result, SS_Thunk_Code);
6099 end if;
6101 Prim_Table (UI_To_Int (DT_Position (Prim))) := E;
6102 end if;
6104 Next_Elmt (Prim_Elmt);
6105 end loop;
6107 for J in Prim_Table'Range loop
6108 if Present (Prim_Table (J)) then
6109 New_Node :=
6110 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
6111 Make_Attribute_Reference (Loc,
6112 Prefix =>
6113 New_Occurrence_Of (Prim_Table (J), Loc),
6114 Attribute_Name => Name_Unrestricted_Access));
6115 else
6116 New_Node := Make_Null (Loc);
6117 end if;
6119 Append_To (Prim_Ops_Aggr_List, New_Node);
6120 end loop;
6121 end;
6122 end if;
6124 New_Node :=
6125 Make_Aggregate (Loc,
6126 Expressions => Prim_Ops_Aggr_List);
6128 Append_To (DT_Aggr_List, New_Node);
6130 -- Remember aggregates initializing dispatch tables
6132 Append_Elmt (New_Node, DT_Aggr);
6134 -- In case of locally defined tagged types we have already declared
6135 -- and uninitialized object for the dispatch table, which is now
6136 -- initialized by means of an assignment.
6138 if not Building_Static_DT (Typ) then
6139 Append_To (Result,
6140 Make_Assignment_Statement (Loc,
6141 Name => New_Occurrence_Of (DT, Loc),
6142 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
6144 -- In case of library level tagged types we declare now and export
6145 -- the constant object containing the dispatch table.
6147 else
6148 Append_To (Result,
6149 Make_Object_Declaration (Loc,
6150 Defining_Identifier => DT,
6151 Aliased_Present => True,
6152 Constant_Present => True,
6153 Object_Definition =>
6154 Make_Subtype_Indication (Loc,
6155 Subtype_Mark => New_Occurrence_Of
6156 (RTE (RE_Dispatch_Table_Wrapper), Loc),
6157 Constraint => Make_Index_Or_Discriminant_Constraint (Loc,
6158 Constraints => DT_Constr_List)),
6159 Expression => Make_Aggregate (Loc, DT_Aggr_List)));
6161 Export_DT (Typ, DT);
6162 end if;
6163 end if;
6165 -- Initialize the table of ancestor tags if not building static
6166 -- dispatch table
6168 if not Building_Static_DT (Typ)
6169 and then not Is_Interface (Typ)
6170 and then not Is_CPP_Class (Typ)
6171 then
6172 Append_To (Result,
6173 Make_Assignment_Statement (Loc,
6174 Name =>
6175 Make_Indexed_Component (Loc,
6176 Prefix =>
6177 Make_Selected_Component (Loc,
6178 Prefix => New_Occurrence_Of (TSD, Loc),
6179 Selector_Name =>
6180 New_Occurrence_Of
6181 (RTE_Record_Component (RE_Tags_Table), Loc)),
6182 Expressions =>
6183 New_List (Make_Integer_Literal (Loc, 0))),
6185 Expression =>
6186 New_Occurrence_Of
6187 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)));
6188 end if;
6190 -- Inherit the dispatch tables of the parent. There is no need to
6191 -- inherit anything from the parent when building static dispatch tables
6192 -- because the whole dispatch table (including inherited primitives) has
6193 -- been already built.
6195 if Building_Static_DT (Typ) then
6196 null;
6198 -- If the ancestor is a CPP_Class type we inherit the dispatch tables
6199 -- in the init proc, and we don't need to fill them in here.
6201 elsif Is_CPP_Class (Parent_Typ) then
6202 null;
6204 -- Otherwise we fill in the dispatch tables here
6206 else
6207 if Typ /= Parent_Typ
6208 and then not Is_Interface (Typ)
6209 and then not Restriction_Active (No_Dispatching_Calls)
6210 then
6211 -- Inherit the dispatch table
6213 if not Is_Interface (Typ)
6214 and then not Is_Interface (Parent_Typ)
6215 and then not Is_CPP_Class (Parent_Typ)
6216 then
6217 declare
6218 Nb_Prims : constant Int :=
6219 UI_To_Int (DT_Entry_Count
6220 (First_Tag_Component (Parent_Typ)));
6222 begin
6223 Append_To (Elab_Code,
6224 Build_Inherit_Predefined_Prims (Loc,
6225 Old_Tag_Node =>
6226 New_Occurrence_Of
6227 (Node
6228 (Next_Elmt
6229 (First_Elmt
6230 (Access_Disp_Table (Parent_Typ)))), Loc),
6231 New_Tag_Node =>
6232 New_Occurrence_Of
6233 (Node
6234 (Next_Elmt
6235 (First_Elmt
6236 (Access_Disp_Table (Typ)))), Loc),
6237 Num_Predef_Prims =>
6238 Number_Of_Predefined_Prims (Parent_Typ)));
6240 if Nb_Prims /= 0 then
6241 Append_To (Elab_Code,
6242 Build_Inherit_Prims (Loc,
6243 Typ => Typ,
6244 Old_Tag_Node =>
6245 New_Occurrence_Of
6246 (Node
6247 (First_Elmt
6248 (Access_Disp_Table (Parent_Typ))), Loc),
6249 New_Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
6250 Num_Prims => Nb_Prims));
6251 end if;
6252 end;
6253 end if;
6255 -- Inherit the secondary dispatch tables of the ancestor
6257 if not Is_CPP_Class (Parent_Typ) then
6258 declare
6259 Sec_DT_Ancestor : Elmt_Id :=
6260 Next_Elmt
6261 (Next_Elmt
6262 (First_Elmt
6263 (Access_Disp_Table
6264 (Parent_Typ))));
6265 Sec_DT_Typ : Elmt_Id :=
6266 Next_Elmt
6267 (Next_Elmt
6268 (First_Elmt
6269 (Access_Disp_Table (Typ))));
6271 procedure Copy_Secondary_DTs (Typ : Entity_Id);
6272 -- Local procedure required to climb through the ancestors
6273 -- and copy the contents of all their secondary dispatch
6274 -- tables.
6276 ------------------------
6277 -- Copy_Secondary_DTs --
6278 ------------------------
6280 procedure Copy_Secondary_DTs (Typ : Entity_Id) is
6281 E : Entity_Id;
6282 Iface : Elmt_Id;
6284 begin
6285 -- Climb to the ancestor (if any) handling private types
6287 if Present (Full_View (Etype (Typ))) then
6288 if Full_View (Etype (Typ)) /= Typ then
6289 Copy_Secondary_DTs (Full_View (Etype (Typ)));
6290 end if;
6292 elsif Etype (Typ) /= Typ then
6293 Copy_Secondary_DTs (Etype (Typ));
6294 end if;
6296 if Present (Interfaces (Typ))
6297 and then not Is_Empty_Elmt_List (Interfaces (Typ))
6298 then
6299 Iface := First_Elmt (Interfaces (Typ));
6300 E := First_Entity (Typ);
6301 while Present (E)
6302 and then Present (Node (Sec_DT_Ancestor))
6303 and then Ekind (Node (Sec_DT_Ancestor)) = E_Constant
6304 loop
6305 if Is_Tag (E) and then Chars (E) /= Name_uTag then
6306 declare
6307 Num_Prims : constant Int :=
6308 UI_To_Int (DT_Entry_Count (E));
6310 begin
6311 if not Is_Interface (Etype (Typ)) then
6313 -- Inherit first secondary dispatch table
6315 Append_To (Elab_Code,
6316 Build_Inherit_Predefined_Prims (Loc,
6317 Old_Tag_Node =>
6318 Unchecked_Convert_To (RTE (RE_Tag),
6319 New_Occurrence_Of
6320 (Node
6321 (Next_Elmt (Sec_DT_Ancestor)),
6322 Loc)),
6323 New_Tag_Node =>
6324 Unchecked_Convert_To (RTE (RE_Tag),
6325 New_Occurrence_Of
6326 (Node (Next_Elmt (Sec_DT_Typ)),
6327 Loc)),
6328 Num_Predef_Prims =>
6329 Number_Of_Predefined_Prims
6330 (Parent_Typ)));
6332 if Num_Prims /= 0 then
6333 Append_To (Elab_Code,
6334 Build_Inherit_Prims (Loc,
6335 Typ => Node (Iface),
6336 Old_Tag_Node =>
6337 Unchecked_Convert_To
6338 (RTE (RE_Tag),
6339 New_Occurrence_Of
6340 (Node (Sec_DT_Ancestor),
6341 Loc)),
6342 New_Tag_Node =>
6343 Unchecked_Convert_To
6344 (RTE (RE_Tag),
6345 New_Occurrence_Of
6346 (Node (Sec_DT_Typ), Loc)),
6347 Num_Prims => Num_Prims));
6348 end if;
6349 end if;
6351 Next_Elmt (Sec_DT_Ancestor);
6352 Next_Elmt (Sec_DT_Typ);
6354 -- Skip the secondary dispatch table of
6355 -- predefined primitives
6357 Next_Elmt (Sec_DT_Ancestor);
6358 Next_Elmt (Sec_DT_Typ);
6360 if not Is_Interface (Etype (Typ)) then
6362 -- Inherit second secondary dispatch table
6364 Append_To (Elab_Code,
6365 Build_Inherit_Predefined_Prims (Loc,
6366 Old_Tag_Node =>
6367 Unchecked_Convert_To (RTE (RE_Tag),
6368 New_Occurrence_Of
6369 (Node
6370 (Next_Elmt (Sec_DT_Ancestor)),
6371 Loc)),
6372 New_Tag_Node =>
6373 Unchecked_Convert_To (RTE (RE_Tag),
6374 New_Occurrence_Of
6375 (Node (Next_Elmt (Sec_DT_Typ)),
6376 Loc)),
6377 Num_Predef_Prims =>
6378 Number_Of_Predefined_Prims
6379 (Parent_Typ)));
6381 if Num_Prims /= 0 then
6382 Append_To (Elab_Code,
6383 Build_Inherit_Prims (Loc,
6384 Typ => Node (Iface),
6385 Old_Tag_Node =>
6386 Unchecked_Convert_To
6387 (RTE (RE_Tag),
6388 New_Occurrence_Of
6389 (Node (Sec_DT_Ancestor),
6390 Loc)),
6391 New_Tag_Node =>
6392 Unchecked_Convert_To
6393 (RTE (RE_Tag),
6394 New_Occurrence_Of
6395 (Node (Sec_DT_Typ), Loc)),
6396 Num_Prims => Num_Prims));
6397 end if;
6398 end if;
6399 end;
6401 Next_Elmt (Sec_DT_Ancestor);
6402 Next_Elmt (Sec_DT_Typ);
6404 -- Skip the secondary dispatch table of
6405 -- predefined primitives
6407 Next_Elmt (Sec_DT_Ancestor);
6408 Next_Elmt (Sec_DT_Typ);
6410 Next_Elmt (Iface);
6411 end if;
6413 Next_Entity (E);
6414 end loop;
6415 end if;
6416 end Copy_Secondary_DTs;
6418 begin
6419 if Present (Node (Sec_DT_Ancestor))
6420 and then Ekind (Node (Sec_DT_Ancestor)) = E_Constant
6421 then
6422 -- Handle private types
6424 if Present (Full_View (Typ)) then
6425 Copy_Secondary_DTs (Full_View (Typ));
6426 else
6427 Copy_Secondary_DTs (Typ);
6428 end if;
6429 end if;
6430 end;
6431 end if;
6432 end if;
6433 end if;
6435 -- Generate code to check if the external tag of this type is the same
6436 -- as the external tag of some other declaration.
6438 -- Check_TSD (TSD'Unrestricted_Access);
6440 -- This check is a consequence of AI05-0113-1/06, so it officially
6441 -- applies to Ada 2005 (and Ada 2012). It might be argued that it is
6442 -- a desirable check to add in Ada 95 mode, but we hesitate to make
6443 -- this change, as it would be incompatible, and could conceivably
6444 -- cause a problem in existing Ada 95 code.
6446 -- We check for No_Run_Time_Mode here, because we do not want to pick
6447 -- up the RE_Check_TSD entity and call it in No_Run_Time mode.
6449 -- We cannot perform this check if the generation of its expanded name
6450 -- was discarded or if No_Tagged_Type_Registration is active.
6452 if not No_Run_Time_Mode
6453 and then not Discard_Names
6454 and then Ada_Version >= Ada_2005
6455 and then not Duplicated_Tag_Checks_Suppressed (Typ)
6456 and then not Restriction_Active (No_Tagged_Type_Registration)
6457 and then RTE_Available (RE_Check_TSD)
6458 then
6459 Append_To (Elab_Code,
6460 Make_Procedure_Call_Statement (Loc,
6461 Name =>
6462 New_Occurrence_Of (RTE (RE_Check_TSD), Loc),
6463 Parameter_Associations => New_List (
6464 Make_Attribute_Reference (Loc,
6465 Prefix => New_Occurrence_Of (TSD, Loc),
6466 Attribute_Name => Name_Unchecked_Access))));
6467 end if;
6469 -- Generate code to register the Tag in the External_Tag hash table for
6470 -- the pure Ada type only.
6472 -- Register_Tag (Dt_Ptr);
6474 -- Skip this action in the following cases:
6475 -- 1) if Register_Tag is not available.
6476 -- 2) in No_Run_Time mode.
6477 -- 3) if Typ is not defined at the library level (this is required
6478 -- to avoid adding concurrency control to the hash table used
6479 -- by the run-time to register the tags).
6480 -- 4) No_Tagged_Type_Registration is active.
6482 if not No_Run_Time_Mode
6483 and then Is_Library_Level_Entity (Typ)
6484 and then not Restriction_Active (No_Tagged_Type_Registration)
6485 and then RTE_Available (RE_Register_Tag)
6486 then
6487 Append_To (Elab_Code,
6488 Make_Procedure_Call_Statement (Loc,
6489 Name =>
6490 New_Occurrence_Of (RTE (RE_Register_Tag), Loc),
6491 Parameter_Associations =>
6492 New_List (New_Occurrence_Of (DT_Ptr, Loc))));
6493 end if;
6495 Append_List_To (Result, Elab_Code);
6497 -- Populate the two auxiliary tables used for dispatching asynchronous,
6498 -- conditional and timed selects for synchronized types that implement
6499 -- a limited interface. Skip this step in Ravenscar profile or when
6500 -- general dispatching is forbidden.
6502 if Ada_Version >= Ada_2005
6503 and then Is_Concurrent_Record_Type (Typ)
6504 and then Has_Interfaces (Typ)
6505 and then not Restriction_Active (No_Dispatching_Calls)
6506 and then not Restriction_Active (No_Select_Statements)
6507 then
6508 Append_List_To (Result,
6509 Make_Select_Specific_Data_Table (Typ));
6510 end if;
6512 -- Remember entities containing dispatch tables
6514 Append_Elmt (Predef_Prims, DT_Decl);
6515 Append_Elmt (DT, DT_Decl);
6517 Analyze_List (Result, Suppress => All_Checks);
6519 -- Mark entities containing dispatch tables. Required by the backend to
6520 -- handle them properly.
6522 if Has_DT (Typ) then
6523 declare
6524 Elmt : Elmt_Id;
6526 begin
6527 -- Object declarations
6529 Elmt := First_Elmt (DT_Decl);
6530 while Present (Elmt) loop
6531 Set_Is_Dispatch_Table_Entity (Node (Elmt));
6532 pragma Assert (Ekind (Etype (Node (Elmt))) = E_Array_Subtype
6533 or else Ekind (Etype (Node (Elmt))) = E_Record_Subtype);
6534 Set_Is_Dispatch_Table_Entity (Etype (Node (Elmt)));
6535 Next_Elmt (Elmt);
6536 end loop;
6538 -- Aggregates initializing dispatch tables
6540 Elmt := First_Elmt (DT_Aggr);
6541 while Present (Elmt) loop
6542 Set_Is_Dispatch_Table_Entity (Etype (Node (Elmt)));
6543 Next_Elmt (Elmt);
6544 end loop;
6545 end;
6546 end if;
6548 <<Leave_SCIL>>
6550 Set_Has_Dispatch_Table (Typ);
6552 -- Register the tagged type in the call graph nodes table
6554 Register_CG_Node (Typ);
6556 <<Leave>>
6557 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6559 return Result;
6560 end Make_DT;
6562 -------------------------------------
6563 -- Make_Select_Specific_Data_Table --
6564 -------------------------------------
6566 function Make_Select_Specific_Data_Table
6567 (Typ : Entity_Id) return List_Id
6569 Assignments : constant List_Id := New_List;
6570 Loc : constant Source_Ptr := Sloc (Typ);
6572 Conc_Typ : Entity_Id;
6573 Decls : List_Id := No_List;
6574 Prim : Entity_Id;
6575 Prim_Als : Entity_Id;
6576 Prim_Elmt : Elmt_Id;
6577 Prim_Pos : Uint;
6578 Nb_Prim : Nat := 0;
6580 type Examined_Array is array (Int range <>) of Boolean;
6582 function Find_Entry_Index (E : Entity_Id) return Uint;
6583 -- Given an entry, find its index in the visible declarations of the
6584 -- corresponding concurrent type of Typ.
6586 ----------------------
6587 -- Find_Entry_Index --
6588 ----------------------
6590 function Find_Entry_Index (E : Entity_Id) return Uint is
6591 Index : Uint := Uint_0;
6592 Subp_Decl : Node_Id;
6594 begin
6595 Subp_Decl := First (Decls);
6596 while Present (Subp_Decl) loop
6597 if Nkind (Subp_Decl) = N_Entry_Declaration then
6598 Index := Index + 1;
6600 if Defining_Identifier (Subp_Decl) = E then
6601 exit;
6602 end if;
6604 end if;
6606 Next (Subp_Decl);
6607 end loop;
6609 return Index;
6610 end Find_Entry_Index;
6612 -- Local variables
6614 Tag_Node : Node_Id;
6616 -- Start of processing for Make_Select_Specific_Data_Table
6618 begin
6619 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
6621 if Present (Corresponding_Concurrent_Type (Typ)) then
6622 Conc_Typ := Corresponding_Concurrent_Type (Typ);
6624 if Present (Full_View (Conc_Typ)) then
6625 Conc_Typ := Full_View (Conc_Typ);
6626 end if;
6628 if Ekind (Conc_Typ) = E_Protected_Type then
6629 Decls := Visible_Declarations (Protected_Definition (
6630 Parent (Conc_Typ)));
6631 else
6632 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
6633 Decls := Visible_Declarations (Task_Definition (
6634 Parent (Conc_Typ)));
6635 end if;
6636 end if;
6638 -- Count the non-predefined primitive operations
6640 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6641 while Present (Prim_Elmt) loop
6642 Prim := Node (Prim_Elmt);
6644 if not (Is_Predefined_Dispatching_Operation (Prim)
6645 or else Is_Predefined_Dispatching_Alias (Prim))
6646 then
6647 Nb_Prim := Nb_Prim + 1;
6648 end if;
6650 Next_Elmt (Prim_Elmt);
6651 end loop;
6653 declare
6654 Examined : Examined_Array (1 .. Nb_Prim) := (others => False);
6656 begin
6657 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6658 while Present (Prim_Elmt) loop
6659 Prim := Node (Prim_Elmt);
6661 -- Look for primitive overriding an abstract interface subprogram
6663 if Present (Interface_Alias (Prim))
6664 and then not
6665 Is_Ancestor
6666 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
6667 Use_Full_View => True)
6668 and then not Examined (UI_To_Int (DT_Position (Alias (Prim))))
6669 then
6670 Prim_Pos := DT_Position (Alias (Prim));
6671 pragma Assert (UI_To_Int (Prim_Pos) <= Nb_Prim);
6672 Examined (UI_To_Int (Prim_Pos)) := True;
6674 -- Set the primitive operation kind regardless of subprogram
6675 -- type. Generate:
6676 -- Ada.Tags.Set_Prim_Op_Kind (DT_Ptr, <position>, <kind>);
6678 if Tagged_Type_Expansion then
6679 Tag_Node :=
6680 New_Occurrence_Of
6681 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6683 else
6684 Tag_Node :=
6685 Make_Attribute_Reference (Loc,
6686 Prefix => New_Occurrence_Of (Typ, Loc),
6687 Attribute_Name => Name_Tag);
6688 end if;
6690 Append_To (Assignments,
6691 Make_Procedure_Call_Statement (Loc,
6692 Name => New_Occurrence_Of (RTE (RE_Set_Prim_Op_Kind), Loc),
6693 Parameter_Associations => New_List (
6694 Tag_Node,
6695 Make_Integer_Literal (Loc, Prim_Pos),
6696 Prim_Op_Kind (Alias (Prim), Typ))));
6698 -- Retrieve the root of the alias chain
6700 Prim_Als := Ultimate_Alias (Prim);
6702 -- In the case of an entry wrapper, set the entry index
6704 if Ekind (Prim) = E_Procedure
6705 and then Is_Primitive_Wrapper (Prim_Als)
6706 and then Ekind (Wrapped_Entity (Prim_Als)) = E_Entry
6707 then
6708 -- Generate:
6709 -- Ada.Tags.Set_Entry_Index
6710 -- (DT_Ptr, <position>, <index>);
6712 if Tagged_Type_Expansion then
6713 Tag_Node :=
6714 New_Occurrence_Of
6715 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6716 else
6717 Tag_Node :=
6718 Make_Attribute_Reference (Loc,
6719 Prefix => New_Occurrence_Of (Typ, Loc),
6720 Attribute_Name => Name_Tag);
6721 end if;
6723 Append_To (Assignments,
6724 Make_Procedure_Call_Statement (Loc,
6725 Name =>
6726 New_Occurrence_Of (RTE (RE_Set_Entry_Index), Loc),
6727 Parameter_Associations => New_List (
6728 Tag_Node,
6729 Make_Integer_Literal (Loc, Prim_Pos),
6730 Make_Integer_Literal (Loc,
6731 Find_Entry_Index (Wrapped_Entity (Prim_Als))))));
6732 end if;
6733 end if;
6735 Next_Elmt (Prim_Elmt);
6736 end loop;
6737 end;
6739 return Assignments;
6740 end Make_Select_Specific_Data_Table;
6742 ---------------
6743 -- Make_Tags --
6744 ---------------
6746 function Make_Tags (Typ : Entity_Id) return List_Id is
6747 Loc : constant Source_Ptr := Sloc (Typ);
6748 Result : constant List_Id := New_List;
6750 procedure Import_DT
6751 (Tag_Typ : Entity_Id;
6752 DT : Entity_Id;
6753 Is_Secondary_DT : Boolean);
6754 -- Import the dispatch table DT of tagged type Tag_Typ. Required to
6755 -- generate forward references and statically allocate the table. For
6756 -- primary dispatch tables that require no dispatch table generate:
6758 -- DT : static aliased constant Non_Dispatch_Table_Wrapper;
6759 -- pragma Import (Ada, DT);
6761 -- Otherwise generate:
6763 -- DT : static aliased constant Dispatch_Table_Wrapper (Nb_Prim);
6764 -- pragma Import (Ada, DT);
6766 ---------------
6767 -- Import_DT --
6768 ---------------
6770 procedure Import_DT
6771 (Tag_Typ : Entity_Id;
6772 DT : Entity_Id;
6773 Is_Secondary_DT : Boolean)
6775 DT_Constr_List : List_Id;
6776 Nb_Prim : Nat;
6778 begin
6779 Set_Is_Imported (DT);
6780 Mutate_Ekind (DT, E_Constant);
6781 Set_Related_Type (DT, Typ);
6783 -- The scope must be set now to call Get_External_Name
6785 Set_Scope (DT, Current_Scope);
6787 Get_External_Name (DT);
6788 Set_Interface_Name (DT,
6789 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
6791 -- Ensure proper Sprint output of this implicit importation
6793 Set_Is_Internal (DT);
6795 -- Save this entity to allow Make_DT to generate its exportation
6797 Append_Elmt (DT, Dispatch_Table_Wrappers (Typ));
6799 -- No dispatch table required
6801 if not Is_Secondary_DT and then not Has_DT (Tag_Typ) then
6802 Append_To (Result,
6803 Make_Object_Declaration (Loc,
6804 Defining_Identifier => DT,
6805 Aliased_Present => True,
6806 Constant_Present => True,
6807 Object_Definition =>
6808 New_Occurrence_Of
6809 (RTE (RE_No_Dispatch_Table_Wrapper), Loc)));
6811 else
6812 -- Calculate the number of primitives of the dispatch table and
6813 -- the size of the Type_Specific_Data record.
6815 Nb_Prim :=
6816 UI_To_Int (DT_Entry_Count (First_Tag_Component (Tag_Typ)));
6818 -- If the tagged type has no primitives we add a dummy slot whose
6819 -- address will be the tag of this type.
6821 if Nb_Prim = 0 then
6822 DT_Constr_List :=
6823 New_List (Make_Integer_Literal (Loc, 1));
6824 else
6825 DT_Constr_List :=
6826 New_List (Make_Integer_Literal (Loc, Nb_Prim));
6827 end if;
6829 Append_To (Result,
6830 Make_Object_Declaration (Loc,
6831 Defining_Identifier => DT,
6832 Aliased_Present => True,
6833 Constant_Present => True,
6834 Object_Definition =>
6835 Make_Subtype_Indication (Loc,
6836 Subtype_Mark =>
6837 New_Occurrence_Of (RTE (RE_Dispatch_Table_Wrapper), Loc),
6838 Constraint => Make_Index_Or_Discriminant_Constraint (Loc,
6839 Constraints => DT_Constr_List))));
6840 end if;
6841 end Import_DT;
6843 -- Local variables
6845 Tname : constant Name_Id := Chars (Typ);
6846 AI_Tag_Comp : Elmt_Id;
6847 DT : Node_Id := Empty;
6848 DT_Ptr : Node_Id;
6849 Predef_Prims_Ptr : Node_Id;
6850 Iface_DT : Node_Id := Empty;
6851 Iface_DT_Ptr : Node_Id;
6852 New_Node : Node_Id;
6853 Suffix_Index : Int;
6854 Typ_Name : Name_Id;
6855 Typ_Comps : Elist_Id;
6857 -- Start of processing for Make_Tags
6859 begin
6860 pragma Assert (No (Access_Disp_Table (Typ)));
6861 Set_Access_Disp_Table (Typ, New_Elmt_List);
6863 -- If the elaboration of this tagged type needs a boolean flag then
6864 -- define now its entity. It is initialized to True to indicate that
6865 -- elaboration is still pending; set to False by the IP routine.
6867 -- TypFxx : boolean := True;
6869 if Elab_Flag_Needed (Typ) then
6870 Set_Access_Disp_Table_Elab_Flag (Typ,
6871 Make_Defining_Identifier (Loc,
6872 Chars => New_External_Name (Tname, 'F')));
6874 Append_To (Result,
6875 Make_Object_Declaration (Loc,
6876 Defining_Identifier => Access_Disp_Table_Elab_Flag (Typ),
6877 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
6878 Expression => New_Occurrence_Of (Standard_True, Loc)));
6879 end if;
6881 -- 1) Generate the primary tag entities
6883 -- Primary dispatch table containing user-defined primitives
6885 DT_Ptr := Make_Defining_Identifier (Loc, New_External_Name (Tname, 'P'));
6886 Set_Etype (DT_Ptr, RTE (RE_Tag));
6887 Append_Elmt (DT_Ptr, Access_Disp_Table (Typ));
6889 -- Minimum decoration
6891 Mutate_Ekind (DT_Ptr, E_Variable);
6892 Set_Related_Type (DT_Ptr, Typ);
6894 -- Notify back end that the types are associated with a dispatch table
6896 Set_Is_Dispatch_Table_Entity (RTE (RE_Prim_Ptr));
6897 Set_Is_Dispatch_Table_Entity (RTE (RE_Predef_Prims_Table_Ptr));
6899 -- For CPP types there is no need to build the dispatch tables since
6900 -- they are imported from the C++ side. If the CPP type has an IP then
6901 -- we declare now the variable that will store the copy of the C++ tag.
6902 -- If the CPP type is an interface, we need the variable as well because
6903 -- it becomes the pointer to the corresponding secondary table.
6905 if Is_CPP_Class (Typ) then
6906 if Has_CPP_Constructors (Typ) or else Is_Interface (Typ) then
6907 Append_To (Result,
6908 Make_Object_Declaration (Loc,
6909 Defining_Identifier => DT_Ptr,
6910 Object_Definition => New_Occurrence_Of (RTE (RE_Tag), Loc),
6911 Expression =>
6912 Unchecked_Convert_To (RTE (RE_Tag),
6913 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
6915 Set_Is_Statically_Allocated (DT_Ptr,
6916 Is_Library_Level_Tagged_Type (Typ));
6917 end if;
6919 -- Ada types
6921 else
6922 -- Primary dispatch table containing predefined primitives
6924 Predef_Prims_Ptr :=
6925 Make_Defining_Identifier (Loc,
6926 Chars => New_External_Name (Tname, 'Y'));
6927 Set_Etype (Predef_Prims_Ptr, RTE (RE_Address));
6928 Append_Elmt (Predef_Prims_Ptr, Access_Disp_Table (Typ));
6930 -- Import the forward declaration of the Dispatch Table wrapper
6931 -- record (Make_DT will take care of exporting it).
6933 if Building_Static_DT (Typ) then
6934 Set_Dispatch_Table_Wrappers (Typ, New_Elmt_List);
6936 DT :=
6937 Make_Defining_Identifier (Loc,
6938 Chars => New_External_Name (Tname, 'T'));
6940 Import_DT (Typ, DT, Is_Secondary_DT => False);
6942 if Has_DT (Typ) then
6943 Append_To (Result,
6944 Make_Object_Declaration (Loc,
6945 Defining_Identifier => DT_Ptr,
6946 Constant_Present => True,
6947 Object_Definition =>
6948 New_Occurrence_Of (RTE (RE_Tag), Loc),
6949 Expression =>
6950 Unchecked_Convert_To (RTE (RE_Tag),
6951 Make_Attribute_Reference (Loc,
6952 Prefix =>
6953 Make_Selected_Component (Loc,
6954 Prefix => New_Occurrence_Of (DT, Loc),
6955 Selector_Name =>
6956 New_Occurrence_Of
6957 (RTE_Record_Component (RE_Prims_Ptr), Loc)),
6958 Attribute_Name => Name_Address))));
6960 -- Generate the SCIL node for the previous object declaration
6961 -- because it has a tag initialization.
6963 if Generate_SCIL then
6964 New_Node :=
6965 Make_SCIL_Dispatch_Table_Tag_Init (Sloc (Last (Result)));
6966 Set_SCIL_Entity (New_Node, Typ);
6967 Set_SCIL_Node (Last (Result), New_Node);
6968 end if;
6970 Append_To (Result,
6971 Make_Object_Declaration (Loc,
6972 Defining_Identifier => Predef_Prims_Ptr,
6973 Constant_Present => True,
6974 Object_Definition =>
6975 New_Occurrence_Of (RTE (RE_Address), Loc),
6976 Expression =>
6977 Make_Attribute_Reference (Loc,
6978 Prefix =>
6979 Make_Selected_Component (Loc,
6980 Prefix => New_Occurrence_Of (DT, Loc),
6981 Selector_Name =>
6982 New_Occurrence_Of
6983 (RTE_Record_Component (RE_Predef_Prims), Loc)),
6984 Attribute_Name => Name_Address)));
6986 -- No dispatch table required
6988 else
6989 Append_To (Result,
6990 Make_Object_Declaration (Loc,
6991 Defining_Identifier => DT_Ptr,
6992 Constant_Present => True,
6993 Object_Definition =>
6994 New_Occurrence_Of (RTE (RE_Tag), Loc),
6995 Expression =>
6996 Unchecked_Convert_To (RTE (RE_Tag),
6997 Make_Attribute_Reference (Loc,
6998 Prefix =>
6999 Make_Selected_Component (Loc,
7000 Prefix => New_Occurrence_Of (DT, Loc),
7001 Selector_Name =>
7002 New_Occurrence_Of
7003 (RTE_Record_Component (RE_NDT_Prims_Ptr),
7004 Loc)),
7005 Attribute_Name => Name_Address))));
7006 end if;
7008 Set_Is_True_Constant (DT_Ptr);
7009 Set_Is_Statically_Allocated (DT_Ptr);
7010 end if;
7011 end if;
7013 -- 2) Generate the secondary tag entities
7015 -- Collect the components associated with secondary dispatch tables
7017 if Has_Interfaces (Typ) then
7018 Collect_Interface_Components (Typ, Typ_Comps);
7020 -- For each interface type we build a unique external name associated
7021 -- with its secondary dispatch table. This name is used to declare an
7022 -- object that references this secondary dispatch table, whose value
7023 -- will be used for the elaboration of Typ objects, and also for the
7024 -- elaboration of objects of types derived from Typ that do not
7025 -- override the primitives of this interface type.
7027 Suffix_Index := 1;
7029 -- Note: The value of Suffix_Index must be in sync with the values of
7030 -- Suffix_Index in secondary dispatch tables generated by Make_DT.
7032 if Is_CPP_Class (Typ) then
7033 AI_Tag_Comp := First_Elmt (Typ_Comps);
7034 while Present (AI_Tag_Comp) loop
7035 Get_Secondary_DT_External_Name
7036 (Typ, Related_Type (Node (AI_Tag_Comp)), Suffix_Index);
7037 Typ_Name := Name_Find;
7039 -- Declare variables to store copy of the C++ secondary tags
7041 Iface_DT_Ptr :=
7042 Make_Defining_Identifier (Loc,
7043 Chars => New_External_Name (Typ_Name, 'P'));
7044 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7045 Mutate_Ekind (Iface_DT_Ptr, E_Variable);
7046 Set_Is_Tag (Iface_DT_Ptr);
7048 Set_Has_Thunks (Iface_DT_Ptr);
7049 Set_Related_Type
7050 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7051 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7053 Append_To (Result,
7054 Make_Object_Declaration (Loc,
7055 Defining_Identifier => Iface_DT_Ptr,
7056 Object_Definition => New_Occurrence_Of
7057 (RTE (RE_Interface_Tag), Loc),
7058 Expression =>
7059 Unchecked_Convert_To (RTE (RE_Interface_Tag),
7060 New_Occurrence_Of (RTE (RE_Null_Address), Loc))));
7062 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7063 Is_Library_Level_Tagged_Type (Typ));
7065 Next_Elmt (AI_Tag_Comp);
7066 end loop;
7068 -- This is not a CPP_Class type
7070 else
7071 AI_Tag_Comp := First_Elmt (Typ_Comps);
7072 while Present (AI_Tag_Comp) loop
7073 Get_Secondary_DT_External_Name
7074 (Typ, Related_Type (Node (AI_Tag_Comp)), Suffix_Index);
7075 Typ_Name := Name_Find;
7077 if Building_Static_DT (Typ) then
7078 Iface_DT :=
7079 Make_Defining_Identifier (Loc,
7080 Chars => New_External_Name (Typ_Name, 'T'));
7081 Import_DT
7082 (Tag_Typ => Related_Type (Node (AI_Tag_Comp)),
7083 DT => Iface_DT,
7084 Is_Secondary_DT => True);
7085 end if;
7087 -- Secondary dispatch table referencing thunks to user-defined
7088 -- primitives covered by this interface.
7090 Iface_DT_Ptr :=
7091 Make_Defining_Identifier (Loc,
7092 Chars => New_External_Name (Typ_Name, 'P'));
7093 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7094 Mutate_Ekind (Iface_DT_Ptr, E_Constant);
7095 Set_Is_Tag (Iface_DT_Ptr);
7096 Set_Has_Thunks (Iface_DT_Ptr);
7097 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7098 Is_Library_Level_Tagged_Type (Typ));
7099 Set_Is_True_Constant (Iface_DT_Ptr);
7100 Set_Related_Type
7101 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7102 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7104 if Building_Static_DT (Typ) then
7105 Append_To (Result,
7106 Make_Object_Declaration (Loc,
7107 Defining_Identifier => Iface_DT_Ptr,
7108 Constant_Present => True,
7109 Object_Definition => New_Occurrence_Of
7110 (RTE (RE_Interface_Tag), Loc),
7111 Expression =>
7112 Unchecked_Convert_To (RTE (RE_Interface_Tag),
7113 Make_Attribute_Reference (Loc,
7114 Prefix =>
7115 Make_Selected_Component (Loc,
7116 Prefix =>
7117 New_Occurrence_Of (Iface_DT, Loc),
7118 Selector_Name =>
7119 New_Occurrence_Of
7120 (RTE_Record_Component (RE_Prims_Ptr),
7121 Loc)),
7122 Attribute_Name => Name_Address))));
7123 end if;
7125 -- Secondary dispatch table referencing thunks to predefined
7126 -- primitives.
7128 Iface_DT_Ptr :=
7129 Make_Defining_Identifier (Loc,
7130 Chars => New_External_Name (Typ_Name, 'Y'));
7131 Set_Etype (Iface_DT_Ptr, RTE (RE_Address));
7132 Mutate_Ekind (Iface_DT_Ptr, E_Constant);
7133 Set_Is_Tag (Iface_DT_Ptr);
7134 Set_Has_Thunks (Iface_DT_Ptr);
7135 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7136 Is_Library_Level_Tagged_Type (Typ));
7137 Set_Is_True_Constant (Iface_DT_Ptr);
7138 Set_Related_Type
7139 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7140 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7142 -- Secondary dispatch table referencing user-defined primitives
7143 -- covered by this interface.
7145 Iface_DT_Ptr :=
7146 Make_Defining_Identifier (Loc,
7147 Chars => New_External_Name (Typ_Name, 'D'));
7148 Set_Etype (Iface_DT_Ptr, RTE (RE_Interface_Tag));
7149 Mutate_Ekind (Iface_DT_Ptr, E_Constant);
7150 Set_Is_Tag (Iface_DT_Ptr);
7151 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7152 Is_Library_Level_Tagged_Type (Typ));
7153 Set_Is_True_Constant (Iface_DT_Ptr);
7154 Set_Related_Type
7155 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7156 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7158 -- Secondary dispatch table referencing predefined primitives
7160 Iface_DT_Ptr :=
7161 Make_Defining_Identifier (Loc,
7162 Chars => New_External_Name (Typ_Name, 'Z'));
7163 Set_Etype (Iface_DT_Ptr, RTE (RE_Address));
7164 Mutate_Ekind (Iface_DT_Ptr, E_Constant);
7165 Set_Is_Tag (Iface_DT_Ptr);
7166 Set_Is_Statically_Allocated (Iface_DT_Ptr,
7167 Is_Library_Level_Tagged_Type (Typ));
7168 Set_Is_True_Constant (Iface_DT_Ptr);
7169 Set_Related_Type
7170 (Iface_DT_Ptr, Related_Type (Node (AI_Tag_Comp)));
7171 Append_Elmt (Iface_DT_Ptr, Access_Disp_Table (Typ));
7173 Next_Elmt (AI_Tag_Comp);
7174 end loop;
7175 end if;
7176 end if;
7178 -- 3) At the end of Access_Disp_Table, if the type has user-defined
7179 -- primitives, we add the entity of an access type declaration that
7180 -- is used by Build_Get_Prim_Op_Address to expand dispatching calls
7181 -- through the primary dispatch table.
7183 if DT_Entry_Count (First_Tag_Component (Typ)) = 0 then
7184 Analyze_List (Result);
7186 -- Generate:
7187 -- subtype Typ_DT is Address_Array (1 .. Nb_Prims);
7188 -- type Typ_DT_Acc is access Typ_DT;
7190 else
7191 declare
7192 Name_DT_Prims : constant Name_Id :=
7193 New_External_Name (Tname, 'G');
7194 Name_DT_Prims_Acc : constant Name_Id :=
7195 New_External_Name (Tname, 'H');
7196 DT_Prims : constant Entity_Id :=
7197 Make_Defining_Identifier (Loc,
7198 Name_DT_Prims);
7199 DT_Prims_Acc : constant Entity_Id :=
7200 Make_Defining_Identifier (Loc,
7201 Name_DT_Prims_Acc);
7202 begin
7203 Append_To (Result,
7204 Make_Subtype_Declaration (Loc,
7205 Defining_Identifier => DT_Prims,
7206 Subtype_Indication =>
7207 Make_Subtype_Indication (Loc,
7208 Subtype_Mark =>
7209 New_Occurrence_Of (RTE (RE_Address_Array), Loc),
7210 Constraint =>
7211 Make_Index_Or_Discriminant_Constraint (Loc, New_List (
7212 Make_Range (Loc,
7213 Low_Bound => Make_Integer_Literal (Loc, 1),
7214 High_Bound =>
7215 Make_Integer_Literal (Loc,
7216 DT_Entry_Count
7217 (First_Tag_Component (Typ)))))))));
7219 Append_To (Result,
7220 Make_Full_Type_Declaration (Loc,
7221 Defining_Identifier => DT_Prims_Acc,
7222 Type_Definition =>
7223 Make_Access_To_Object_Definition (Loc,
7224 Subtype_Indication =>
7225 New_Occurrence_Of (DT_Prims, Loc))));
7227 Append_Elmt (DT_Prims_Acc, Access_Disp_Table (Typ));
7229 -- Analyze the resulting list and suppress the generation of the
7230 -- Init_Proc associated with the above array declaration because
7231 -- this type is never used in object declarations. It is only used
7232 -- to simplify the expansion associated with dispatching calls.
7234 Analyze_List (Result);
7235 Set_Suppress_Initialization (Base_Type (DT_Prims));
7237 -- Disable backend optimizations based on assumptions about the
7238 -- aliasing status of objects designated by the access to the
7239 -- dispatch table. Required to handle dispatch tables imported
7240 -- from C++.
7242 Set_No_Strict_Aliasing (Base_Type (DT_Prims_Acc));
7244 -- Add the freezing nodes of these declarations; required to avoid
7245 -- generating these freezing nodes in wrong scopes (for example in
7246 -- the IC routine of a derivation of Typ).
7248 -- What is an "IC routine"? Is "init_proc" meant here???
7250 Append_List_To (Result, Freeze_Entity (DT_Prims, Typ));
7251 Append_List_To (Result, Freeze_Entity (DT_Prims_Acc, Typ));
7253 -- Mark entity of dispatch table. Required by the back end to
7254 -- handle them properly.
7256 Set_Is_Dispatch_Table_Entity (DT_Prims);
7257 end;
7258 end if;
7260 -- Mark entities of dispatch table. Required by the back end to handle
7261 -- them properly.
7263 if Present (DT) then
7264 Set_Is_Dispatch_Table_Entity (DT);
7265 Set_Is_Dispatch_Table_Entity (Etype (DT));
7266 end if;
7268 if Present (Iface_DT) then
7269 Set_Is_Dispatch_Table_Entity (Iface_DT);
7270 Set_Is_Dispatch_Table_Entity (Etype (Iface_DT));
7271 end if;
7273 if Is_CPP_Class (Root_Type (Typ)) then
7274 Mutate_Ekind (DT_Ptr, E_Variable);
7275 else
7276 Mutate_Ekind (DT_Ptr, E_Constant);
7277 end if;
7279 Set_Is_Tag (DT_Ptr);
7280 Set_Related_Type (DT_Ptr, Typ);
7282 return Result;
7283 end Make_Tags;
7285 ---------------
7286 -- New_Value --
7287 ---------------
7289 function New_Value (From : Node_Id) return Node_Id is
7290 Res : constant Node_Id := Duplicate_Subexpr (From);
7291 begin
7292 if Is_Access_Type (Etype (From)) then
7293 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
7294 else
7295 return Res;
7296 end if;
7297 end New_Value;
7299 ------------------
7300 -- Prim_Op_Kind --
7301 ------------------
7303 function Prim_Op_Kind
7304 (Prim : Entity_Id;
7305 Typ : Entity_Id) return Node_Id
7307 Full_Typ : Entity_Id := Typ;
7308 Loc : constant Source_Ptr := Sloc (Prim);
7309 Prim_Op : Entity_Id;
7311 begin
7312 -- Retrieve the original primitive operation
7314 Prim_Op := Ultimate_Alias (Prim);
7316 if Ekind (Typ) = E_Record_Type
7317 and then Present (Corresponding_Concurrent_Type (Typ))
7318 then
7319 Full_Typ := Corresponding_Concurrent_Type (Typ);
7320 end if;
7322 -- When a private tagged type is completed by a concurrent type,
7323 -- retrieve the full view.
7325 if Is_Private_Type (Full_Typ) then
7326 Full_Typ := Full_View (Full_Typ);
7327 end if;
7329 if Ekind (Prim_Op) = E_Function then
7331 -- Protected function
7333 if Ekind (Full_Typ) = E_Protected_Type then
7334 return New_Occurrence_Of (RTE (RE_POK_Protected_Function), Loc);
7336 -- Task function
7338 elsif Ekind (Full_Typ) = E_Task_Type then
7339 return New_Occurrence_Of (RTE (RE_POK_Task_Function), Loc);
7341 -- Regular function
7343 else
7344 return New_Occurrence_Of (RTE (RE_POK_Function), Loc);
7345 end if;
7347 else
7348 pragma Assert (Ekind (Prim_Op) = E_Procedure);
7350 if Ekind (Full_Typ) = E_Protected_Type then
7352 -- Protected entry
7354 if Is_Primitive_Wrapper (Prim_Op)
7355 and then Ekind (Wrapped_Entity (Prim_Op)) = E_Entry
7356 then
7357 return New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc);
7359 -- Protected procedure
7361 else
7362 return
7363 New_Occurrence_Of (RTE (RE_POK_Protected_Procedure), Loc);
7364 end if;
7366 elsif Ekind (Full_Typ) = E_Task_Type then
7368 -- Task entry
7370 if Is_Primitive_Wrapper (Prim_Op)
7371 and then Ekind (Wrapped_Entity (Prim_Op)) = E_Entry
7372 then
7373 return New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc);
7375 -- Task "procedure". These are the internally Expander-generated
7376 -- procedures (task body for instance).
7378 else
7379 return New_Occurrence_Of (RTE (RE_POK_Task_Procedure), Loc);
7380 end if;
7382 -- Regular procedure
7384 else
7385 return New_Occurrence_Of (RTE (RE_POK_Procedure), Loc);
7386 end if;
7387 end if;
7388 end Prim_Op_Kind;
7390 -----------------------------------
7391 -- Register_Predefined_Primitive --
7392 -----------------------------------
7394 function Register_Predefined_Primitive
7395 (Loc : Source_Ptr;
7396 Prim : Entity_Id) return List_Id
7398 L : constant List_Id := New_List;
7399 Tagged_Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
7401 E : Entity_Id;
7402 Iface_DT_Ptr : Elmt_Id;
7403 SS_Thunk_Id : Entity_Id;
7404 SS_Thunk_Code : Node_Id;
7405 Thunk_Id : Entity_Id;
7406 Thunk_Code : List_Id;
7408 begin
7409 if No (Access_Disp_Table (Tagged_Typ))
7410 or else not Has_Interfaces (Tagged_Typ)
7411 or else not RTE_Available (RE_Interface_Tag)
7412 or else Restriction_Active (No_Dispatching_Calls)
7413 then
7414 return L;
7415 end if;
7417 -- Skip the first two access-to-dispatch-table pointers since they
7418 -- leads to the primary dispatch table (predefined DT and user
7419 -- defined DT). We are only concerned with the secondary dispatch
7420 -- table pointers. Note that the access-to- dispatch-table pointer
7421 -- corresponds to the first implemented interface retrieved below.
7423 Iface_DT_Ptr :=
7424 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7426 while Present (Iface_DT_Ptr)
7427 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7428 loop
7429 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7431 Expand_Interface_Thunk
7432 (Prim, Thunk_Id, Thunk_Code, Related_Type (Node (Iface_DT_Ptr)));
7434 if Present (Thunk_Id) then
7435 Append_List_To (L, Thunk_Code);
7437 E := Prim;
7438 Expand_Secondary_Stack_Thunk (E, SS_Thunk_Id, SS_Thunk_Code);
7440 if Present (SS_Thunk_Id) then
7441 E := SS_Thunk_Id;
7442 Append_To (L, SS_Thunk_Code);
7443 end if;
7445 Append_To (L,
7446 Build_Set_Predefined_Prim_Op_Address (Loc,
7447 Tag_Node =>
7448 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7449 Position => DT_Position (Prim),
7450 Address_Node =>
7451 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7452 Make_Attribute_Reference (Loc,
7453 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7454 Attribute_Name => Name_Unrestricted_Access))));
7456 Append_To (L,
7457 Build_Set_Predefined_Prim_Op_Address (Loc,
7458 Tag_Node =>
7459 New_Occurrence_Of
7460 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7461 Loc),
7462 Position => DT_Position (Prim),
7463 Address_Node =>
7464 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7465 Make_Attribute_Reference (Loc,
7466 Prefix => New_Occurrence_Of (E, Loc),
7467 Attribute_Name => Name_Unrestricted_Access))));
7468 end if;
7470 -- Skip the tag of the predefined primitives dispatch table
7472 Next_Elmt (Iface_DT_Ptr);
7473 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7475 -- Skip tag of the no-thunks dispatch table
7477 Next_Elmt (Iface_DT_Ptr);
7478 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7480 -- Skip tag of predefined primitives no-thunks dispatch table
7482 Next_Elmt (Iface_DT_Ptr);
7483 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7485 Next_Elmt (Iface_DT_Ptr);
7486 end loop;
7488 return L;
7489 end Register_Predefined_Primitive;
7491 ------------------------
7492 -- Register_Primitive --
7493 ------------------------
7495 function Register_Primitive
7496 (Loc : Source_Ptr;
7497 Prim : Entity_Id) return List_Id
7499 L : constant List_Id := New_List;
7501 DT_Ptr : Entity_Id;
7502 E : Entity_Id;
7503 Iface_Prim : Entity_Id;
7504 Iface_Typ : Entity_Id;
7505 Iface_DT_Ptr : Entity_Id;
7506 Iface_DT_Elmt : Elmt_Id;
7507 Pos : Uint;
7508 SS_Thunk_Id : Entity_Id;
7509 SS_Thunk_Code : Node_Id;
7510 Tag : Entity_Id;
7511 Tag_Typ : Entity_Id;
7512 Thunk_Id : Entity_Id;
7513 Thunk_Code : List_Id;
7515 begin
7516 pragma Assert (not Restriction_Active (No_Dispatching_Calls));
7518 -- Do not register eliminated primitives in the dispatch table
7520 if not RTE_Available (RE_Tag)
7521 or else Is_Eliminated (Ultimate_Alias (Prim))
7522 or else Generate_SCIL
7523 then
7524 return L;
7525 end if;
7527 -- Primitive associated with a tagged type
7529 if No (Interface_Alias (Prim)) then
7530 Tag_Typ := Scope (DTC_Entity (Prim));
7531 Pos := DT_Position (Prim);
7532 Tag := First_Tag_Component (Tag_Typ);
7534 E := Prim;
7535 Expand_Secondary_Stack_Thunk (E, SS_Thunk_Id, SS_Thunk_Code);
7537 if Present (SS_Thunk_Id) then
7538 E := SS_Thunk_Id;
7539 Append_To (L, SS_Thunk_Code);
7540 end if;
7542 if Is_Predefined_Dispatching_Operation (Prim)
7543 or else Is_Predefined_Dispatching_Alias (Prim)
7544 then
7545 DT_Ptr :=
7546 Node (Next_Elmt (First_Elmt (Access_Disp_Table (Tag_Typ))));
7548 Append_To (L,
7549 Build_Set_Predefined_Prim_Op_Address (Loc,
7550 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7551 Position => Pos,
7552 Address_Node =>
7553 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7554 Make_Attribute_Reference (Loc,
7555 Prefix => New_Occurrence_Of (E, Loc),
7556 Attribute_Name => Name_Unrestricted_Access))));
7558 -- Register copy of the pointer to the 'size primitive in the TSD
7560 if Chars (Prim) = Name_uSize
7561 and then RTE_Record_Component_Available (RE_Size_Func)
7562 then
7563 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Tag_Typ)));
7564 Append_To (L,
7565 Build_Set_Size_Function (Loc,
7566 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7567 Size_Func => Prim));
7568 end if;
7570 else
7571 pragma Assert (Pos /= Uint_0 and then Pos <= DT_Entry_Count (Tag));
7573 -- Skip registration of primitives located in the C++ part of the
7574 -- dispatch table. Their slot is set by the IC routine.
7576 if not Is_CPP_Class (Root_Type (Tag_Typ))
7577 or else Pos > CPP_Num_Prims (Tag_Typ)
7578 then
7579 DT_Ptr := Node (First_Elmt (Access_Disp_Table (Tag_Typ)));
7580 Append_To (L,
7581 Build_Set_Prim_Op_Address (Loc,
7582 Typ => Tag_Typ,
7583 Tag_Node => New_Occurrence_Of (DT_Ptr, Loc),
7584 Position => Pos,
7585 Address_Node =>
7586 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7587 Make_Attribute_Reference (Loc,
7588 Prefix => New_Occurrence_Of (E, Loc),
7589 Attribute_Name => Name_Unrestricted_Access))));
7590 end if;
7591 end if;
7593 -- Ada 2005 (AI-251): Primitive associated with an interface type
7595 -- Generate the code of the thunk only if the interface type is not an
7596 -- immediate ancestor of Typ; otherwise the dispatch table associated
7597 -- with the interface is the primary dispatch table and we have nothing
7598 -- else to do here.
7600 else
7601 Tag_Typ := Find_Dispatching_Type (Alias (Prim));
7602 Iface_Typ := Find_Dispatching_Type (Interface_Alias (Prim));
7604 pragma Assert (Is_Interface (Iface_Typ));
7606 -- No action needed for interfaces that are ancestors of Typ because
7607 -- their primitives are located in the primary dispatch table.
7609 if Is_Ancestor (Iface_Typ, Tag_Typ, Use_Full_View => True) then
7610 return L;
7612 -- No action needed for primitives located in the C++ part of the
7613 -- dispatch table. Their slot is set by the IC routine.
7615 elsif Is_CPP_Class (Root_Type (Tag_Typ))
7616 and then DT_Position (Alias (Prim)) <= CPP_Num_Prims (Tag_Typ)
7617 and then not Is_Predefined_Dispatching_Operation (Prim)
7618 and then not Is_Predefined_Dispatching_Alias (Prim)
7619 then
7620 return L;
7621 end if;
7623 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code, Iface_Typ);
7625 if Present (Thunk_Id)
7626 and then not Is_Ancestor (Iface_Typ, Tag_Typ, Use_Full_View => True)
7627 then
7628 -- Generate the code necessary to fill the appropriate entry of
7629 -- the secondary dispatch table of Prim's controlling type with
7630 -- Thunk_Id's address.
7632 Iface_DT_Elmt := Find_Interface_ADT (Tag_Typ, Iface_Typ);
7633 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7634 pragma Assert (Has_Thunks (Iface_DT_Ptr));
7636 Iface_Prim := Interface_Alias (Prim);
7637 Pos := DT_Position (Iface_Prim);
7638 Tag := First_Tag_Component (Iface_Typ);
7640 Append_List_To (L, Thunk_Code);
7642 E := Ultimate_Alias (Prim);
7643 Expand_Secondary_Stack_Thunk (E, SS_Thunk_Id, SS_Thunk_Code);
7645 if Present (SS_Thunk_Id) then
7646 E := SS_Thunk_Id;
7647 Append_To (L, SS_Thunk_Code);
7648 end if;
7650 if Is_Predefined_Dispatching_Operation (Prim)
7651 or else Is_Predefined_Dispatching_Alias (Prim)
7652 then
7653 Append_To (L,
7654 Build_Set_Predefined_Prim_Op_Address (Loc,
7655 Tag_Node =>
7656 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Elmt)), Loc),
7657 Position => Pos,
7658 Address_Node =>
7659 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7660 Make_Attribute_Reference (Loc,
7661 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7662 Attribute_Name => Name_Unrestricted_Access))));
7664 Next_Elmt (Iface_DT_Elmt);
7665 Next_Elmt (Iface_DT_Elmt);
7666 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7667 pragma Assert (not Has_Thunks (Iface_DT_Ptr));
7669 Append_To (L,
7670 Build_Set_Predefined_Prim_Op_Address (Loc,
7671 Tag_Node =>
7672 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Elmt)), Loc),
7673 Position => Pos,
7674 Address_Node =>
7675 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7676 Make_Attribute_Reference (Loc,
7677 Prefix => New_Occurrence_Of (E, Loc),
7678 Attribute_Name => Name_Unrestricted_Access))));
7680 else
7681 pragma Assert (Pos /= Uint_0
7682 and then Pos <= DT_Entry_Count (Tag));
7684 Append_To (L,
7685 Build_Set_Prim_Op_Address (Loc,
7686 Typ => Iface_Typ,
7687 Tag_Node => New_Occurrence_Of (Iface_DT_Ptr, Loc),
7688 Position => Pos,
7689 Address_Node =>
7690 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7691 Make_Attribute_Reference (Loc,
7692 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7693 Attribute_Name => Name_Unrestricted_Access))));
7695 Next_Elmt (Iface_DT_Elmt);
7696 Next_Elmt (Iface_DT_Elmt);
7697 Iface_DT_Ptr := Node (Iface_DT_Elmt);
7698 pragma Assert (not Has_Thunks (Iface_DT_Ptr));
7700 Append_To (L,
7701 Build_Set_Prim_Op_Address (Loc,
7702 Typ => Iface_Typ,
7703 Tag_Node => New_Occurrence_Of (Iface_DT_Ptr, Loc),
7704 Position => Pos,
7705 Address_Node =>
7706 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7707 Make_Attribute_Reference (Loc,
7708 Prefix => New_Occurrence_Of (E, Loc),
7709 Attribute_Name => Name_Unrestricted_Access))));
7711 end if;
7712 end if;
7713 end if;
7715 return L;
7716 end Register_Primitive;
7718 -------------------------
7719 -- Set_All_DT_Position --
7720 -------------------------
7722 procedure Set_All_DT_Position (Typ : Entity_Id) is
7724 function In_Predef_Prims_DT (Prim : Entity_Id) return Boolean;
7725 -- Returns True if Prim is located in the dispatch table of
7726 -- predefined primitives
7728 procedure Validate_Position (Prim : Entity_Id);
7729 -- Check that position assigned to Prim is completely safe (it has not
7730 -- been assigned to a previously defined primitive operation of Typ).
7732 ------------------------
7733 -- In_Predef_Prims_DT --
7734 ------------------------
7736 function In_Predef_Prims_DT (Prim : Entity_Id) return Boolean is
7737 begin
7738 -- Predefined primitives
7740 if Is_Predefined_Dispatching_Operation (Prim) then
7741 return True;
7743 -- Renamings of predefined primitives
7745 elsif Present (Alias (Prim))
7746 and then Is_Predefined_Dispatching_Operation (Ultimate_Alias (Prim))
7747 then
7748 if Chars (Ultimate_Alias (Prim)) /= Name_Op_Eq then
7749 return True;
7751 -- An overriding operation that is a user-defined renaming of
7752 -- predefined equality inherits its slot from the overridden
7753 -- operation. Otherwise it is treated as a predefined op and
7754 -- occupies the same predefined slot as equality. A call to it is
7755 -- transformed into a call to its alias, which is the predefined
7756 -- equality op. A dispatching call thus uses the proper slot if
7757 -- operation is further inherited and called with class-wide
7758 -- arguments.
7760 else
7761 return
7762 not Comes_From_Source (Prim)
7763 or else No (Overridden_Operation (Prim));
7764 end if;
7766 -- User-defined primitives
7768 else
7769 return False;
7770 end if;
7771 end In_Predef_Prims_DT;
7773 -----------------------
7774 -- Validate_Position --
7775 -----------------------
7777 procedure Validate_Position (Prim : Entity_Id) is
7778 Op_Elmt : Elmt_Id;
7779 Op : Entity_Id;
7781 begin
7782 -- Aliased primitives are safe
7784 if Present (Alias (Prim)) then
7785 return;
7786 end if;
7788 Op_Elmt := First_Elmt (Primitive_Operations (Typ));
7789 while Present (Op_Elmt) loop
7790 Op := Node (Op_Elmt);
7792 -- No need to check against itself
7794 if Op = Prim then
7795 null;
7797 -- Primitive operations covering abstract interfaces are
7798 -- allocated later
7800 elsif Present (Interface_Alias (Op)) then
7801 null;
7803 -- Predefined dispatching operations are completely safe. They
7804 -- are allocated at fixed positions in a separate table.
7806 elsif Is_Predefined_Dispatching_Operation (Op)
7807 or else Is_Predefined_Dispatching_Alias (Op)
7808 then
7809 null;
7811 -- Aliased subprograms are safe
7813 elsif Present (Alias (Op)) then
7814 null;
7816 elsif DT_Position (Op) = DT_Position (Prim)
7817 and then not Is_Predefined_Dispatching_Operation (Op)
7818 and then not Is_Predefined_Dispatching_Operation (Prim)
7819 and then not Is_Predefined_Dispatching_Alias (Op)
7820 and then not Is_Predefined_Dispatching_Alias (Prim)
7821 then
7822 -- Handle aliased subprograms
7824 declare
7825 Op_1 : Entity_Id;
7826 Op_2 : Entity_Id;
7828 begin
7829 Op_1 := Op;
7830 loop
7831 if Present (Overridden_Operation (Op_1)) then
7832 Op_1 := Overridden_Operation (Op_1);
7833 elsif Present (Alias (Op_1)) then
7834 Op_1 := Alias (Op_1);
7835 else
7836 exit;
7837 end if;
7838 end loop;
7840 Op_2 := Prim;
7841 loop
7842 if Present (Overridden_Operation (Op_2)) then
7843 Op_2 := Overridden_Operation (Op_2);
7844 elsif Present (Alias (Op_2)) then
7845 Op_2 := Alias (Op_2);
7846 else
7847 exit;
7848 end if;
7849 end loop;
7851 if Op_1 /= Op_2 then
7852 raise Program_Error;
7853 end if;
7854 end;
7855 end if;
7857 Next_Elmt (Op_Elmt);
7858 end loop;
7859 end Validate_Position;
7861 -- Local variables
7863 Parent_Typ : constant Entity_Id := Etype (Typ);
7864 First_Prim : constant Elmt_Id := First_Elmt (Primitive_Operations (Typ));
7865 The_Tag : constant Entity_Id := First_Tag_Component (Typ);
7867 Adjusted : Boolean := False;
7868 Finalized : Boolean := False;
7870 Count_Prim : Nat;
7871 DT_Length : Nat;
7872 Nb_Prim : Nat;
7873 Prim : Entity_Id;
7874 Prim_Elmt : Elmt_Id;
7876 -- Start of processing for Set_All_DT_Position
7878 begin
7879 pragma Assert (Present (First_Tag_Component (Typ)));
7881 -- Set the DT_Position for each primitive operation. Perform some sanity
7882 -- checks to avoid building inconsistent dispatch tables.
7884 -- First stage: Set DTC entity of all the primitive operations. This is
7885 -- required to properly read the DT_Position attribute in latter stages.
7887 Prim_Elmt := First_Prim;
7888 Count_Prim := 0;
7889 while Present (Prim_Elmt) loop
7890 Prim := Node (Prim_Elmt);
7892 -- Predefined primitives have a separate dispatch table
7894 if not In_Predef_Prims_DT (Prim) then
7895 Count_Prim := Count_Prim + 1;
7896 end if;
7898 Set_DTC_Entity_Value (Typ, Prim);
7900 -- Clear any previous value of the DT_Position attribute. In this
7901 -- way we ensure that the final position of all the primitives is
7902 -- established by the following stages of this algorithm.
7904 Set_DT_Position_Value (Prim, No_Uint);
7906 Next_Elmt (Prim_Elmt);
7907 end loop;
7909 declare
7910 Fixed_Prim : array (Int range 0 .. Count_Prim) of Boolean :=
7911 (others => False);
7913 E : Entity_Id;
7915 procedure Handle_Inherited_Private_Subprograms (Typ : Entity_Id);
7916 -- Called if Typ is declared in a nested package or a public child
7917 -- package to handle inherited primitives that were inherited by Typ
7918 -- in the visible part, but whose declaration was deferred because
7919 -- the parent operation was private and not visible at that point.
7921 procedure Set_Fixed_Prim (Pos : Nat);
7922 -- Sets to true an element of the Fixed_Prim table to indicate
7923 -- that this entry of the dispatch table of Typ is occupied.
7925 ------------------------------------------
7926 -- Handle_Inherited_Private_Subprograms --
7927 ------------------------------------------
7929 procedure Handle_Inherited_Private_Subprograms (Typ : Entity_Id) is
7930 Op_List : Elist_Id;
7931 Op_Elmt : Elmt_Id;
7932 Op_Elmt_2 : Elmt_Id;
7933 Prim_Op : Entity_Id;
7934 Parent_Subp : Entity_Id;
7936 begin
7937 Op_List := Primitive_Operations (Typ);
7939 Op_Elmt := First_Elmt (Op_List);
7940 while Present (Op_Elmt) loop
7941 Prim_Op := Node (Op_Elmt);
7943 -- Search primitives that are implicit operations with an
7944 -- internal name whose parent operation has a normal name.
7946 if Present (Alias (Prim_Op))
7947 and then Find_Dispatching_Type (Alias (Prim_Op)) /= Typ
7948 and then not Comes_From_Source (Prim_Op)
7949 and then Is_Internal_Name (Chars (Prim_Op))
7950 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
7951 then
7952 Parent_Subp := Alias (Prim_Op);
7954 -- Check if the type has an explicit overriding for this
7955 -- primitive.
7957 Op_Elmt_2 := Next_Elmt (Op_Elmt);
7958 while Present (Op_Elmt_2) loop
7959 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
7960 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
7961 then
7962 Set_DT_Position_Value (Prim_Op,
7963 DT_Position (Parent_Subp));
7964 Set_DT_Position_Value (Node (Op_Elmt_2),
7965 DT_Position (Parent_Subp));
7966 Set_Fixed_Prim (UI_To_Int (DT_Position (Prim_Op)));
7968 goto Next_Primitive;
7969 end if;
7971 Next_Elmt (Op_Elmt_2);
7972 end loop;
7973 end if;
7975 <<Next_Primitive>>
7976 Next_Elmt (Op_Elmt);
7977 end loop;
7978 end Handle_Inherited_Private_Subprograms;
7980 --------------------
7981 -- Set_Fixed_Prim --
7982 --------------------
7984 procedure Set_Fixed_Prim (Pos : Nat) is
7985 begin
7986 pragma Assert (Pos <= Count_Prim);
7987 Fixed_Prim (Pos) := True;
7988 exception
7989 when Constraint_Error =>
7990 raise Program_Error;
7991 end Set_Fixed_Prim;
7993 begin
7994 -- In case of nested packages and public child package it may be
7995 -- necessary a special management on inherited subprograms so that
7996 -- the dispatch table is properly filled.
7998 if Ekind (Scope (Scope (Typ))) = E_Package
7999 and then Scope (Scope (Typ)) /= Standard_Standard
8000 and then ((Is_Derived_Type (Typ) and then not Is_Private_Type (Typ))
8001 or else
8002 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration
8003 and then Is_Generic_Type (Typ)))
8004 and then In_Open_Scopes (Scope (Etype (Typ)))
8005 and then Is_Base_Type (Typ)
8006 then
8007 Handle_Inherited_Private_Subprograms (Typ);
8008 end if;
8010 -- Second stage: Register fixed entries
8012 Nb_Prim := 0;
8013 Prim_Elmt := First_Prim;
8014 while Present (Prim_Elmt) loop
8015 Prim := Node (Prim_Elmt);
8017 -- Predefined primitives have a separate table and all its
8018 -- entries are at predefined fixed positions.
8020 if In_Predef_Prims_DT (Prim) then
8021 if Is_Predefined_Dispatching_Operation (Prim) then
8022 Set_DT_Position_Value (Prim,
8023 Default_Prim_Op_Position (Prim));
8025 else pragma Assert (Present (Alias (Prim)));
8026 Set_DT_Position_Value (Prim,
8027 Default_Prim_Op_Position (Ultimate_Alias (Prim)));
8028 end if;
8030 -- Overriding primitives of ancestor abstract interfaces
8032 elsif Present (Interface_Alias (Prim))
8033 and then Is_Ancestor
8034 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
8035 Use_Full_View => True)
8036 then
8037 pragma Assert (No (DT_Position (Prim)));
8038 pragma Assert (Present (DTC_Entity (Interface_Alias (Prim))));
8040 E := Interface_Alias (Prim);
8041 Set_DT_Position_Value (Prim, DT_Position (E));
8043 pragma Assert
8044 (No (DT_Position (Alias (Prim)))
8045 or else DT_Position (Alias (Prim)) = DT_Position (E));
8046 Set_DT_Position_Value (Alias (Prim), DT_Position (E));
8047 Set_Fixed_Prim (UI_To_Int (DT_Position (Prim)));
8049 -- Overriding primitives must use the same entry as the overridden
8050 -- primitive. Note that the Alias of the operation is set when the
8051 -- operation is declared by a renaming, in which case it is not
8052 -- overriding. If it renames another primitive it will use the
8053 -- same dispatch table slot, but if it renames an operation in a
8054 -- nested package it's a new primitive and will have its own slot.
8056 elsif No (Interface_Alias (Prim))
8057 and then Present (Alias (Prim))
8058 and then Chars (Prim) = Chars (Alias (Prim))
8059 and then Nkind (Unit_Declaration_Node (Prim)) /=
8060 N_Subprogram_Renaming_Declaration
8061 then
8062 declare
8063 Par_Type : constant Entity_Id :=
8064 Find_Dispatching_Type (Alias (Prim));
8066 begin
8067 if Present (Par_Type)
8068 and then Par_Type /= Typ
8069 and then Is_Ancestor (Par_Type, Typ, Use_Full_View => True)
8070 and then Present (DTC_Entity (Alias (Prim)))
8071 then
8072 E := Alias (Prim);
8073 Set_DT_Position_Value (Prim, DT_Position (E));
8075 if not Is_Predefined_Dispatching_Alias (E) then
8076 Set_Fixed_Prim (UI_To_Int (DT_Position (E)));
8077 end if;
8078 end if;
8079 end;
8080 end if;
8082 Next_Elmt (Prim_Elmt);
8083 end loop;
8085 -- Third stage: Fix the position of all the new primitives. Entries
8086 -- associated with primitives covering interfaces are handled in a
8087 -- latter round.
8089 Prim_Elmt := First_Prim;
8090 while Present (Prim_Elmt) loop
8091 Prim := Node (Prim_Elmt);
8093 -- Skip primitives previously set entries
8095 if Present (DT_Position (Prim)) then
8096 null;
8098 -- Primitives covering interface primitives are handled later
8100 elsif Present (Interface_Alias (Prim)) then
8101 null;
8103 else
8104 -- Take the next available position in the DT
8106 loop
8107 Nb_Prim := Nb_Prim + 1;
8108 pragma Assert (Nb_Prim <= Count_Prim);
8109 exit when not Fixed_Prim (Nb_Prim);
8110 end loop;
8112 Set_DT_Position_Value (Prim, UI_From_Int (Nb_Prim));
8113 Set_Fixed_Prim (Nb_Prim);
8114 end if;
8116 Next_Elmt (Prim_Elmt);
8117 end loop;
8118 end;
8120 -- Fourth stage: Complete the decoration of primitives covering
8121 -- interfaces (that is, propagate the DT_Position attribute from
8122 -- the aliased primitive)
8124 Prim_Elmt := First_Prim;
8125 while Present (Prim_Elmt) loop
8126 Prim := Node (Prim_Elmt);
8128 if No (DT_Position (Prim))
8129 and then Present (Interface_Alias (Prim))
8130 then
8131 pragma Assert (Present (Alias (Prim))
8132 and then Find_Dispatching_Type (Alias (Prim)) = Typ);
8134 -- Check if this entry will be placed in the primary DT
8136 if Is_Ancestor
8137 (Find_Dispatching_Type (Interface_Alias (Prim)), Typ,
8138 Use_Full_View => True)
8139 then
8140 pragma Assert (Present (DT_Position (Alias (Prim))));
8141 Set_DT_Position_Value (Prim, DT_Position (Alias (Prim)));
8143 -- Otherwise it will be placed in the secondary DT
8145 else
8146 pragma Assert
8147 (Present (DT_Position (Interface_Alias (Prim))));
8148 Set_DT_Position_Value (Prim,
8149 DT_Position (Interface_Alias (Prim)));
8150 end if;
8151 end if;
8153 Next_Elmt (Prim_Elmt);
8154 end loop;
8156 -- Generate listing showing the contents of the dispatch tables. This
8157 -- action is done before some further static checks because in case of
8158 -- critical errors caused by a wrong dispatch table we need to see the
8159 -- contents of such table.
8161 if Debug_Flag_ZZ then
8162 Write_DT (Typ);
8163 end if;
8165 -- Final stage: Ensure that the table is correct plus some further
8166 -- verifications concerning the primitives.
8168 Prim_Elmt := First_Prim;
8169 DT_Length := 0;
8170 while Present (Prim_Elmt) loop
8171 Prim := Node (Prim_Elmt);
8173 -- At this point all the primitives MUST have a position in the
8174 -- dispatch table.
8176 if No (DT_Position (Prim)) then
8177 raise Program_Error;
8178 end if;
8180 -- Calculate real size of the dispatch table
8182 if not In_Predef_Prims_DT (Prim)
8183 and then UI_To_Int (DT_Position (Prim)) > DT_Length
8184 then
8185 DT_Length := UI_To_Int (DT_Position (Prim));
8186 end if;
8188 -- Ensure that the assigned position to non-predefined dispatching
8189 -- operations in the dispatch table is correct.
8191 if not Is_Predefined_Dispatching_Operation (Prim)
8192 and then not Is_Predefined_Dispatching_Alias (Prim)
8193 then
8194 Validate_Position (Prim);
8195 end if;
8197 if Chars (Prim) = Name_Finalize then
8198 Finalized := True;
8199 end if;
8201 if Chars (Prim) = Name_Adjust then
8202 Adjusted := True;
8203 end if;
8205 -- An abstract operation cannot be declared in the private part for a
8206 -- visible abstract type, because it can't be overridden outside this
8207 -- package hierarchy. For explicit declarations this is checked at
8208 -- the point of declaration, but for inherited operations it must be
8209 -- done when building the dispatch table.
8211 -- Ada 2005 (AI-251): Primitives associated with interfaces are
8212 -- excluded from this check because interfaces must be visible in
8213 -- the public and private part (RM 7.3 (7.3/2))
8215 -- We disable this check in Relaxed_RM_Semantics mode, to accommodate
8216 -- legacy Ada code.
8218 if not Relaxed_RM_Semantics
8219 and then Is_Abstract_Type (Typ)
8220 and then Is_Abstract_Subprogram (Prim)
8221 and then Present (Alias (Prim))
8222 and then not Is_Interface
8223 (Find_Dispatching_Type (Ultimate_Alias (Prim)))
8224 and then No (Interface_Alias (Prim))
8225 and then Is_Derived_Type (Typ)
8226 and then In_Private_Part (Current_Scope)
8227 and then
8228 List_Containing (Parent (Prim)) =
8229 Private_Declarations (Package_Specification (Current_Scope))
8230 and then Original_View_In_Visible_Part (Typ)
8231 then
8232 -- We exclude Input and Output stream operations because
8233 -- Limited_Controlled inherits useless Input and Output stream
8234 -- operations from Root_Controlled, which can never be overridden.
8235 -- Move this check to sem???
8237 if not Is_TSS (Prim, TSS_Stream_Input)
8238 and then
8239 not Is_TSS (Prim, TSS_Stream_Output)
8240 then
8241 Error_Msg_NE
8242 ("abstract inherited private operation&" &
8243 " must be overridden (RM 3.9.3(10))",
8244 Parent (Typ), Prim);
8245 end if;
8246 end if;
8248 Next_Elmt (Prim_Elmt);
8249 end loop;
8251 -- Additional check
8253 if Is_Controlled (Typ) then
8254 if not Finalized then
8255 Error_Msg_N
8256 ("controlled type has no explicit Finalize method??", Typ);
8258 elsif not Adjusted then
8259 Error_Msg_N
8260 ("controlled type has no explicit Adjust method??", Typ);
8261 end if;
8262 end if;
8264 -- Set the final size of the Dispatch Table
8266 Set_DT_Entry_Count (The_Tag, UI_From_Int (DT_Length));
8268 -- The derived type must have at least as many components as its parent
8269 -- (for root types Etype points to itself and the test cannot fail).
8271 if DT_Entry_Count (The_Tag) <
8272 DT_Entry_Count (First_Tag_Component (Parent_Typ))
8273 then
8274 raise Program_Error;
8275 end if;
8276 end Set_All_DT_Position;
8278 --------------------------
8279 -- Set_CPP_Constructors --
8280 --------------------------
8282 procedure Set_CPP_Constructors (Typ : Entity_Id) is
8284 function Gen_Parameters_Profile (E : Entity_Id) return List_Id;
8285 -- Duplicate the parameters profile of the imported C++ constructor
8286 -- adding the "this" pointer to the object as the additional first
8287 -- parameter under the usual form _Init : in out Typ.
8289 ----------------------------
8290 -- Gen_Parameters_Profile --
8291 ----------------------------
8293 function Gen_Parameters_Profile (E : Entity_Id) return List_Id is
8294 Loc : constant Source_Ptr := Sloc (E);
8295 Parms : List_Id;
8296 P : Node_Id;
8298 begin
8299 Parms :=
8300 New_List (
8301 Make_Parameter_Specification (Loc,
8302 Defining_Identifier =>
8303 Make_Defining_Identifier (Loc, Name_uInit),
8304 In_Present => True,
8305 Out_Present => True,
8306 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
8308 P := First (Parameter_Specifications (Parent (E)));
8309 while Present (P) loop
8310 Append_To (Parms,
8311 Make_Parameter_Specification (Loc,
8312 Defining_Identifier =>
8313 Make_Defining_Identifier (Loc,
8314 Chars => Chars (Defining_Identifier (P))),
8315 Parameter_Type => New_Copy_Tree (Parameter_Type (P)),
8316 Expression => New_Copy_Tree (Expression (P))));
8317 Next (P);
8318 end loop;
8320 return Parms;
8321 end Gen_Parameters_Profile;
8323 -- Local variables
8325 Loc : Source_Ptr;
8326 E : Entity_Id;
8327 Found : Boolean := False;
8328 IP : Entity_Id;
8329 IP_Body : Node_Id;
8330 P : Node_Id;
8331 Parms : List_Id;
8333 Covers_Default_Constructor : Entity_Id := Empty;
8335 -- Start of processing for Set_CPP_Constructor
8337 begin
8338 pragma Assert (Is_CPP_Class (Typ));
8340 -- Look for the constructor entities
8342 E := Next_Entity (Typ);
8343 while Present (E) loop
8344 if Ekind (E) = E_Function
8345 and then Is_Constructor (E)
8346 then
8347 Found := True;
8348 Loc := Sloc (E);
8349 Parms := Gen_Parameters_Profile (E);
8350 IP := Make_Defining_Identifier (Loc, Make_Init_Proc_Name (Typ));
8352 -- Case 1: Constructor of untagged type
8354 -- If the C++ class has no virtual methods then the matching Ada
8355 -- type is an untagged record type. In such case there is no need
8356 -- to generate a wrapper of the C++ constructor because the _tag
8357 -- component is not available.
8359 if not Is_Tagged_Type (Typ) then
8360 Discard_Node
8361 (Make_Subprogram_Declaration (Loc,
8362 Specification =>
8363 Make_Procedure_Specification (Loc,
8364 Defining_Unit_Name => IP,
8365 Parameter_Specifications => Parms)));
8367 Set_Init_Proc (Typ, IP);
8368 Set_Is_Imported (IP);
8369 Set_Is_Constructor (IP);
8370 Set_Interface_Name (IP, Interface_Name (E));
8371 Set_Convention (IP, Convention_CPP);
8372 Set_Is_Public (IP);
8373 Set_Has_Completion (IP);
8375 -- Case 2: Constructor of a tagged type
8377 -- In this case we generate the IP routine as a wrapper of the
8378 -- C++ constructor because IP must also save a copy of the _tag
8379 -- generated in the C++ side. The copy of the _tag is used by
8380 -- Build_CPP_Init_Procedure to elaborate derivations of C++ types.
8382 -- Generate:
8383 -- procedure IP (_init : in out Typ; ...) is
8384 -- procedure ConstructorP (_init : in out Typ; ...);
8385 -- pragma Import (ConstructorP);
8386 -- begin
8387 -- ConstructorP (_init, ...);
8388 -- if Typ._tag = null then
8389 -- Typ._tag := _init._tag;
8390 -- end if;
8391 -- end IP;
8393 else
8394 declare
8395 Body_Stmts : constant List_Id := New_List;
8396 Constructor_Id : Entity_Id;
8397 Constructor_Decl_Node : Node_Id;
8398 Init_Tags_List : List_Id;
8400 begin
8401 Constructor_Id := Make_Temporary (Loc, 'P');
8403 Constructor_Decl_Node :=
8404 Make_Subprogram_Declaration (Loc,
8405 Make_Procedure_Specification (Loc,
8406 Defining_Unit_Name => Constructor_Id,
8407 Parameter_Specifications => Parms));
8409 Set_Is_Imported (Constructor_Id);
8410 Set_Is_Constructor (Constructor_Id);
8411 Set_Interface_Name (Constructor_Id, Interface_Name (E));
8412 Set_Convention (Constructor_Id, Convention_CPP);
8413 Set_Is_Public (Constructor_Id);
8414 Set_Has_Completion (Constructor_Id);
8416 -- Build the init procedure as a wrapper of this constructor
8418 Parms := Gen_Parameters_Profile (E);
8420 -- Invoke the C++ constructor
8422 declare
8423 Actuals : constant List_Id := New_List;
8425 begin
8426 P := First (Parms);
8427 while Present (P) loop
8428 Append_To (Actuals,
8429 New_Occurrence_Of (Defining_Identifier (P), Loc));
8430 Next (P);
8431 end loop;
8433 Append_To (Body_Stmts,
8434 Make_Procedure_Call_Statement (Loc,
8435 Name => New_Occurrence_Of (Constructor_Id, Loc),
8436 Parameter_Associations => Actuals));
8437 end;
8439 -- Initialize copies of C++ primary and secondary tags
8441 Init_Tags_List := New_List;
8443 declare
8444 Tag_Elmt : Elmt_Id;
8445 Tag_Comp : Node_Id;
8447 begin
8448 Tag_Elmt := First_Elmt (Access_Disp_Table (Typ));
8449 Tag_Comp := First_Tag_Component (Typ);
8451 while Present (Tag_Elmt)
8452 and then Is_Tag (Node (Tag_Elmt))
8453 loop
8454 -- Skip the following assertion with primary tags
8455 -- because Related_Type is not set on primary tag
8456 -- components.
8458 pragma Assert
8459 (Tag_Comp = First_Tag_Component (Typ)
8460 or else Related_Type (Node (Tag_Elmt))
8461 = Related_Type (Tag_Comp));
8463 Append_To (Init_Tags_List,
8464 Make_Assignment_Statement (Loc,
8465 Name =>
8466 New_Occurrence_Of (Node (Tag_Elmt), Loc),
8467 Expression =>
8468 Make_Selected_Component (Loc,
8469 Prefix =>
8470 Make_Identifier (Loc, Name_uInit),
8471 Selector_Name =>
8472 New_Occurrence_Of (Tag_Comp, Loc))));
8474 Tag_Comp := Next_Tag_Component (Tag_Comp);
8475 Next_Elmt (Tag_Elmt);
8476 end loop;
8477 end;
8479 Append_To (Body_Stmts,
8480 Make_If_Statement (Loc,
8481 Condition =>
8482 Make_Op_Eq (Loc,
8483 Left_Opnd =>
8484 New_Occurrence_Of
8485 (Node (First_Elmt (Access_Disp_Table (Typ))),
8486 Loc),
8487 Right_Opnd =>
8488 Unchecked_Convert_To (RTE (RE_Tag),
8489 New_Occurrence_Of (RTE (RE_Null_Address), Loc))),
8490 Then_Statements => Init_Tags_List));
8492 IP_Body :=
8493 Make_Subprogram_Body (Loc,
8494 Specification =>
8495 Make_Procedure_Specification (Loc,
8496 Defining_Unit_Name => IP,
8497 Parameter_Specifications => Parms),
8498 Declarations => New_List (Constructor_Decl_Node),
8499 Handled_Statement_Sequence =>
8500 Make_Handled_Sequence_Of_Statements (Loc,
8501 Statements => Body_Stmts,
8502 Exception_Handlers => No_List));
8504 Discard_Node (IP_Body);
8505 Set_Init_Proc (Typ, IP);
8506 end;
8507 end if;
8509 -- If this constructor has parameters and all its parameters have
8510 -- defaults then it covers the default constructor. The semantic
8511 -- analyzer ensures that only one constructor with defaults covers
8512 -- the default constructor.
8514 if Present (Parameter_Specifications (Parent (E)))
8515 and then Needs_No_Actuals (E)
8516 then
8517 Covers_Default_Constructor := IP;
8518 end if;
8519 end if;
8521 Next_Entity (E);
8522 end loop;
8524 -- If there are no constructors, mark the type as abstract since we
8525 -- won't be able to declare objects of that type.
8527 if not Found then
8528 Set_Is_Abstract_Type (Typ);
8529 end if;
8531 -- Handle constructor that has all its parameters with defaults and
8532 -- hence it covers the default constructor. We generate a wrapper IP
8533 -- which calls the covering constructor.
8535 if Present (Covers_Default_Constructor) then
8536 declare
8537 Body_Stmts : List_Id;
8539 begin
8540 Loc := Sloc (Covers_Default_Constructor);
8542 Body_Stmts := New_List (
8543 Make_Procedure_Call_Statement (Loc,
8544 Name =>
8545 New_Occurrence_Of (Covers_Default_Constructor, Loc),
8546 Parameter_Associations => New_List (
8547 Make_Identifier (Loc, Name_uInit))));
8549 IP := Make_Defining_Identifier (Loc, Make_Init_Proc_Name (Typ));
8551 IP_Body :=
8552 Make_Subprogram_Body (Loc,
8553 Specification =>
8554 Make_Procedure_Specification (Loc,
8555 Defining_Unit_Name => IP,
8556 Parameter_Specifications => New_List (
8557 Make_Parameter_Specification (Loc,
8558 Defining_Identifier =>
8559 Make_Defining_Identifier (Loc, Name_uInit),
8560 Parameter_Type => New_Occurrence_Of (Typ, Loc)))),
8562 Declarations => No_List,
8564 Handled_Statement_Sequence =>
8565 Make_Handled_Sequence_Of_Statements (Loc,
8566 Statements => Body_Stmts,
8567 Exception_Handlers => No_List));
8569 Discard_Node (IP_Body);
8570 Set_Init_Proc (Typ, IP);
8571 end;
8572 end if;
8574 -- If the CPP type has constructors then it must import also the default
8575 -- C++ constructor. It is required for default initialization of objects
8576 -- of the type. It is also required to elaborate objects of Ada types
8577 -- that are defined as derivations of this CPP type.
8579 if Has_CPP_Constructors (Typ)
8580 and then No (Init_Proc (Typ))
8581 then
8582 Error_Msg_N ("??default constructor must be imported from C++", Typ);
8583 end if;
8584 end Set_CPP_Constructors;
8586 ---------------------------
8587 -- Set_DT_Position_Value --
8588 ---------------------------
8590 procedure Set_DT_Position_Value (Prim : Entity_Id; Value : Uint) is
8591 begin
8592 Set_DT_Position (Prim, Value);
8594 -- Propagate the value to the wrapped subprogram (if one is present)
8596 if Ekind (Prim) in E_Function | E_Procedure
8597 and then Is_Primitive_Wrapper (Prim)
8598 and then Present (Wrapped_Entity (Prim))
8599 and then Is_Dispatching_Operation (Wrapped_Entity (Prim))
8600 then
8601 Set_DT_Position (Wrapped_Entity (Prim), Value);
8602 end if;
8603 end Set_DT_Position_Value;
8605 --------------------------
8606 -- Set_DTC_Entity_Value --
8607 --------------------------
8609 procedure Set_DTC_Entity_Value
8610 (Tagged_Type : Entity_Id;
8611 Prim : Entity_Id)
8613 begin
8614 if Present (Interface_Alias (Prim))
8615 and then Is_Interface
8616 (Find_Dispatching_Type (Interface_Alias (Prim)))
8617 then
8618 Set_DTC_Entity (Prim,
8619 Find_Interface_Tag
8620 (T => Tagged_Type,
8621 Iface => Find_Dispatching_Type (Interface_Alias (Prim))));
8622 else
8623 Set_DTC_Entity (Prim,
8624 First_Tag_Component (Tagged_Type));
8625 end if;
8627 -- Propagate the value to the wrapped subprogram (if one is present)
8629 if Ekind (Prim) in E_Function | E_Procedure
8630 and then Is_Primitive_Wrapper (Prim)
8631 and then Present (Wrapped_Entity (Prim))
8632 and then Is_Dispatching_Operation (Wrapped_Entity (Prim))
8633 then
8634 Set_DTC_Entity (Wrapped_Entity (Prim), DTC_Entity (Prim));
8635 end if;
8636 end Set_DTC_Entity_Value;
8638 -----------------
8639 -- Tagged_Kind --
8640 -----------------
8642 function Tagged_Kind (T : Entity_Id) return Node_Id is
8643 Conc_Typ : Entity_Id;
8644 Loc : constant Source_Ptr := Sloc (T);
8646 begin
8647 pragma Assert
8648 (Is_Tagged_Type (T) and then RTE_Available (RE_Tagged_Kind));
8650 -- Abstract kinds
8652 if Is_Abstract_Type (T) then
8653 if Is_Limited_Record (T) then
8654 return New_Occurrence_Of
8655 (RTE (RE_TK_Abstract_Limited_Tagged), Loc);
8656 else
8657 return New_Occurrence_Of
8658 (RTE (RE_TK_Abstract_Tagged), Loc);
8659 end if;
8661 -- Concurrent kinds
8663 elsif Is_Concurrent_Record_Type (T) then
8664 Conc_Typ := Corresponding_Concurrent_Type (T);
8666 if Present (Full_View (Conc_Typ)) then
8667 Conc_Typ := Full_View (Conc_Typ);
8668 end if;
8670 if Ekind (Conc_Typ) = E_Protected_Type then
8671 return New_Occurrence_Of (RTE (RE_TK_Protected), Loc);
8672 else
8673 pragma Assert (Ekind (Conc_Typ) = E_Task_Type);
8674 return New_Occurrence_Of (RTE (RE_TK_Task), Loc);
8675 end if;
8677 -- Regular tagged kinds
8679 else
8680 if Is_Limited_Record (T) then
8681 return New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc);
8682 else
8683 return New_Occurrence_Of (RTE (RE_TK_Tagged), Loc);
8684 end if;
8685 end if;
8686 end Tagged_Kind;
8688 --------------
8689 -- Write_DT --
8690 --------------
8692 procedure Write_DT (Typ : Entity_Id) is
8693 Elmt : Elmt_Id;
8694 Prim : Node_Id;
8696 begin
8697 -- Protect this procedure against wrong usage. Required because it will
8698 -- be used directly from GDB
8700 if not (Typ <= Last_Node_Id)
8701 or else not Is_Tagged_Type (Typ)
8702 then
8703 Write_Str ("wrong usage: Write_DT must be used with tagged types");
8704 Write_Eol;
8705 return;
8706 end if;
8708 Write_Int (Int (Typ));
8709 Write_Str (": ");
8710 Write_Name (Chars (Typ));
8712 if Is_Interface (Typ) then
8713 Write_Str (" is interface");
8714 end if;
8716 Write_Eol;
8718 Elmt := First_Elmt (Primitive_Operations (Typ));
8719 while Present (Elmt) loop
8720 Prim := Node (Elmt);
8721 Write_Str (" - ");
8723 -- Indicate if this primitive will be allocated in the primary
8724 -- dispatch table or in a secondary dispatch table associated
8725 -- with an abstract interface type
8727 if Present (DTC_Entity (Prim)) then
8728 if Is_RTE (Etype (DTC_Entity (Prim)), RE_Tag) then
8729 Write_Str ("[P] ");
8730 else
8731 Write_Str ("[s] ");
8732 end if;
8733 end if;
8735 -- Output the node of this primitive operation and its name
8737 Write_Int (Int (Prim));
8738 Write_Str (": ");
8740 if Is_Predefined_Dispatching_Operation (Prim) then
8741 Write_Str ("(predefined) ");
8742 end if;
8744 -- Prefix the name of the primitive with its corresponding tagged
8745 -- type to facilitate seeing inherited primitives.
8747 if Present (Alias (Prim)) then
8748 Write_Name
8749 (Chars (Find_Dispatching_Type (Ultimate_Alias (Prim))));
8750 else
8751 Write_Name (Chars (Typ));
8752 end if;
8754 Write_Str (".");
8755 Write_Name (Chars (Prim));
8757 -- Indicate if this primitive has an aliased primitive
8759 if Present (Alias (Prim)) then
8760 Write_Str (" (alias = ");
8761 Write_Int (Int (Alias (Prim)));
8763 -- If the DTC_Entity attribute is already set we can also output
8764 -- the name of the interface covered by this primitive (if any).
8766 if Ekind (Alias (Prim)) in E_Function | E_Procedure
8767 and then Present (DTC_Entity (Alias (Prim)))
8768 and then Is_Interface (Scope (DTC_Entity (Alias (Prim))))
8769 then
8770 Write_Str (" from interface ");
8771 Write_Name (Chars (Scope (DTC_Entity (Alias (Prim)))));
8772 end if;
8774 if Present (Interface_Alias (Prim)) then
8775 Write_Str (", AI_Alias of ");
8777 if Is_Null_Interface_Primitive (Interface_Alias (Prim)) then
8778 Write_Str ("null primitive ");
8779 end if;
8781 Write_Name
8782 (Chars (Find_Dispatching_Type (Interface_Alias (Prim))));
8783 Write_Char (':');
8784 Write_Int (Int (Interface_Alias (Prim)));
8785 end if;
8787 Write_Str (")");
8788 end if;
8790 -- Display the final position of this primitive in its associated
8791 -- (primary or secondary) dispatch table.
8793 if Present (DTC_Entity (Prim))
8794 and then Present (DT_Position (Prim))
8795 then
8796 Write_Str (" at #");
8797 Write_Int (UI_To_Int (DT_Position (Prim)));
8798 end if;
8800 if Is_Abstract_Subprogram (Prim) then
8801 Write_Str (" is abstract;");
8803 -- Check if this is a null primitive
8805 elsif Comes_From_Source (Prim)
8806 and then Ekind (Prim) = E_Procedure
8807 and then Null_Present (Parent (Prim))
8808 then
8809 Write_Str (" is null;");
8810 end if;
8812 if Is_Eliminated (Ultimate_Alias (Prim)) then
8813 Write_Str (" (eliminated)");
8814 end if;
8816 if Is_Imported (Prim)
8817 and then Convention (Prim) = Convention_CPP
8818 then
8819 Write_Str (" (C++)");
8820 end if;
8822 Write_Eol;
8824 Next_Elmt (Elmt);
8825 end loop;
8826 end Write_DT;
8828 end Exp_Disp;