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