Debian Codegraph @

gcc/ada/accessibility.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- A C C E S S I B I L I T Y --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2022-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Einfo.Utils; use Einfo.Utils;
34 with Exp_Atag; use Exp_Atag;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Restrict; use Restrict;
43 with Rtsfind; use Rtsfind;
44 with Sem; use Sem;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Res; use Sem_Res;
48 with Sem_Util; use Sem_Util;
49 with Sinfo; use Sinfo;
50 with Sinfo.Nodes; use Sinfo.Nodes;
51 with Sinfo.Utils; use Sinfo.Utils;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Tbuild; use Tbuild;
55
56 package body Accessibility is
57
58 ---------------------------
59 -- Accessibility_Message --
60 ---------------------------
61
62 procedure Accessibility_Message (N : Node_Id; Typ : Entity_Id) is
63 Loc : constant Source_Ptr := Sloc (N);
64 P : constant Node_Id := Prefix (N);
65 Indic : Node_Id := Parent (Parent (N));
66
67 begin
68 -- In an instance, this is a runtime check, but one we know will fail,
69 -- so generate an appropriate warning.
70
71 if In_Instance_Body then
72 Error_Msg_Warn := SPARK_Mode /= On;
73 Error_Msg_F
74 ("non-local pointer cannot point to local object<<", P);
75 Error_Msg_F ("\Program_Error [<<", P);
76 Rewrite (N,
77 Make_Raise_Program_Error (Loc,
78 Reason => PE_Accessibility_Check_Failed));
79 Set_Etype (N, Typ);
80 return;
81
82 else
83 Error_Msg_F ("non-local pointer cannot point to local object", P);
84
85 -- Check for case where we have a missing access definition
86
87 if Is_Record_Type (Current_Scope)
88 and then
89 Nkind (Parent (N)) in N_Discriminant_Association
90 | N_Index_Or_Discriminant_Constraint
91 then
92 Indic := Parent (Parent (N));
93 while Present (Indic)
94 and then Nkind (Indic) /= N_Subtype_Indication
95 loop
96 Indic := Parent (Indic);
97 end loop;
98
99 if Present (Indic) then
100 Error_Msg_NE
101 ("\use an access definition for" &
102 " the access discriminant of&",
103 N, Entity (Subtype_Mark (Indic)));
104 end if;
105 end if;
106 end if;
107 end Accessibility_Message;
108
109 -------------------------
110 -- Accessibility_Level --
111 -------------------------
112
113 function Accessibility_Level
114 (Expr : Node_Id;
115 Level : Accessibility_Level_Kind;
116 In_Return_Context : Boolean := False;
117 Allow_Alt_Model : Boolean := True) return Node_Id
118 is
119 Loc : constant Source_Ptr := Sloc (Expr);
120
121 function Accessibility_Level (Expr : Node_Id) return Node_Id is
122 (Accessibility_Level
123 (Expr, Level, In_Return_Context, Allow_Alt_Model));
124 -- Renaming of the enclosing function to facilitate recursive calls
125
126 function Make_Level_Literal (Level : Uint) return Node_Id;
127 -- Construct an integer literal representing an accessibility level with
128 -- its type set to Natural.
129
130 function Innermost_Master_Scope_Depth (N : Node_Id) return Uint;
131 -- Returns the scope depth of the given node's innermost enclosing scope
132 -- (effectively the accessibility level of the innermost enclosing
133 -- master).
134
135 function Function_Call_Or_Allocator_Level (N : Node_Id) return Node_Id;
136 -- Centralized processing of subprogram calls which may appear in prefix
137 -- notation.
138
139 function Typ_Access_Level (Typ : Entity_Id) return Uint
140 is (Type_Access_Level (Typ, Allow_Alt_Model));
141 -- Renaming of Type_Access_Level with Allow_Alt_Model specified to avoid
142 -- passing the parameter specifically in every call.
143
144 ----------------------------------
145 -- Innermost_Master_Scope_Depth --
146 ----------------------------------
147
148 function Innermost_Master_Scope_Depth (N : Node_Id) return Uint is
149 Encl_Scop : Entity_Id;
150 Ent : Entity_Id;
151 Node_Par : Node_Id := Parent (N);
152 Master_Lvl_Modifier : Int := 0;
153
154 begin
155 -- Locate the nearest enclosing node (by traversing Parents)
156 -- that Defining_Entity can be applied to, and return the
157 -- depth of that entity's nearest enclosing scope.
158
159 -- The RM 7.6.1(3) definition of "master" includes statements
160 -- and conditions for loops among other things. Are these cases
161 -- detected properly ???
162
163 while Present (Node_Par) loop
164 Ent := Defining_Entity_Or_Empty (Node_Par);
165
166 if Present (Ent) then
167 -- X'Old is nested within the current subprogram, so we do not
168 -- want Find_Enclosing_Scope of that subprogram. If this is an
169 -- allocator, then we're looking for the innermost master of
170 -- the call, so again we do not want Find_Enclosing_Scope.
171
172 if (Nkind (N) = N_Attribute_Reference
173 and then Attribute_Name (N) = Name_Old)
174 or else Nkind (N) = N_Allocator
175 then
176 Encl_Scop := Ent;
177 else
178 Encl_Scop := Find_Enclosing_Scope (Ent);
179 end if;
180
181 -- Ignore transient scopes made during expansion while also
182 -- taking into account certain expansions - like iterators
183 -- which get expanded into renamings and thus not marked
184 -- as coming from source.
185
186 if Comes_From_Source (Node_Par)
187 or else (Nkind (Node_Par) = N_Object_Renaming_Declaration
188 and then Comes_From_Iterator (Node_Par))
189 then
190 -- Handle the case of expressions within library level
191 -- subprograms here by adding one to the level modifier.
192
193 if Encl_Scop = Standard_Standard
194 and then Nkind (Node_Par) = N_Subprogram_Body
195 then
196 Master_Lvl_Modifier := Master_Lvl_Modifier + 1;
197 end if;
198
199 -- Note that in some rare cases the scope depth may not be
200 -- set, for example, when we are in the middle of analyzing
201 -- a type and the enclosing scope is said type. In that case
202 -- simply return zero for the outermost scope.
203
204 if Scope_Depth_Set (Encl_Scop) then
205 return Scope_Depth (Encl_Scop) + Master_Lvl_Modifier;
206 else
207 return Uint_0;
208 end if;
209 end if;
210
211 -- For a return statement within a function, return
212 -- the depth of the function itself. This is not just
213 -- a small optimization, but matters when analyzing
214 -- the expression in an expression function before
215 -- the body is created.
216
217 elsif Nkind (Node_Par) in N_Extended_Return_Statement
218 | N_Simple_Return_Statement
219 then
220 return Scope_Depth (Enclosing_Subprogram (Node_Par));
221
222 -- Statements are counted as masters
223
224 elsif Is_Master (Node_Par) then
225 Master_Lvl_Modifier := Master_Lvl_Modifier + 1;
226
227 end if;
228
229 Node_Par := Parent (Node_Par);
230 end loop;
231
232 -- Should never reach the following return
233
234 pragma Assert (False);
235
236 return Scope_Depth (Current_Scope) + 1;
237 end Innermost_Master_Scope_Depth;
238
239 ------------------------
240 -- Make_Level_Literal --
241 ------------------------
242
243 function Make_Level_Literal (Level : Uint) return Node_Id is
244 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
245
246 begin
247 Set_Etype (Result, Standard_Natural);
248 return Result;
249 end Make_Level_Literal;
250
251 --------------------------------------
252 -- Function_Call_Or_Allocator_Level --
253 --------------------------------------
254
255 function Function_Call_Or_Allocator_Level (N : Node_Id) return Node_Id is
256 Par : Node_Id;
257 Prev_Par : Node_Id;
258 begin
259 -- Results of functions are objects, so we either get the
260 -- accessibility of the function or, in case of a call which is
261 -- indirect, the level of the access-to-subprogram type.
262
263 -- This code looks wrong ???
264
265 if Nkind (N) = N_Function_Call
266 and then Ada_Version < Ada_2005
267 then
268 if Is_Entity_Name (Name (N)) then
269 return Make_Level_Literal
270 (Subprogram_Access_Level (Entity (Name (N))));
271 else
272 return Make_Level_Literal
273 (Typ_Access_Level (Etype (Prefix (Name (N)))));
274 end if;
275
276 -- We ignore coextensions as they cannot be implemented under the
277 -- "small-integer" model.
278
279 elsif Nkind (N) = N_Allocator
280 and then (Is_Static_Coextension (N)
281 or else Is_Dynamic_Coextension (N))
282 then
283 return Make_Level_Literal (Scope_Depth (Standard_Standard));
284 end if;
285
286 -- Named access types have a designated level
287
288 if Is_Named_Access_Type (Etype (N)) then
289 return Make_Level_Literal (Typ_Access_Level (Etype (N)));
290
291 -- Otherwise, the level is dictated by RM 3.10.2 (10.7/3)
292
293 else
294 -- Check No_Dynamic_Accessibility_Checks restriction override for
295 -- alternative accessibility model.
296
297 if Allow_Alt_Model
298 and then No_Dynamic_Accessibility_Checks_Enabled (N)
299 and then Is_Anonymous_Access_Type (Etype (N))
300 then
301 -- In the alternative model the level is that of the
302 -- designated type.
303
304 if Debug_Flag_Underscore_B then
305 return Make_Level_Literal (Typ_Access_Level (Etype (N)));
306
307 -- For function calls the level is that of the innermost
308 -- master, otherwise (for allocators etc.) we get the level
309 -- of the corresponding anonymous access type, which is
310 -- calculated through the normal path of execution.
311
312 elsif Nkind (N) = N_Function_Call then
313 return Make_Level_Literal
314 (Innermost_Master_Scope_Depth (Expr));
315 end if;
316 end if;
317
318 if Nkind (N) = N_Function_Call then
319 -- Dynamic checks are generated when we are within a return
320 -- value or we are in a function call within an anonymous
321 -- access discriminant constraint of a return object (signified
322 -- by In_Return_Context) on the side of the callee.
323
324 -- So, in this case, return accessibility level of the
325 -- enclosing subprogram.
326
327 if In_Return_Value (N)
328 or else In_Return_Context
329 then
330 return Make_Level_Literal
331 (Subprogram_Access_Level (Current_Subprogram));
332 end if;
333 end if;
334
335 -- When the call is being dereferenced the level is that of the
336 -- enclosing master of the dereferenced call.
337
338 if Nkind (Parent (N)) in N_Explicit_Dereference
339 | N_Indexed_Component
340 | N_Selected_Component
341 then
342 return Make_Level_Literal
343 (Innermost_Master_Scope_Depth (Expr));
344 end if;
345
346 -- Find any relevant enclosing parent nodes that designate an
347 -- object being initialized.
348
349 -- Note: The above is only relevant if the result is used "in its
350 -- entirety" as RM 3.10.2 (10.2/3) states. However, this is
351 -- accounted for in the case statement in the main body of
352 -- Accessibility_Level for N_Selected_Component.
353
354 Par := Parent (Expr);
355 Prev_Par := Empty;
356 while Present (Par) loop
357 -- Detect an expanded implicit conversion, typically this
358 -- occurs on implicitly converted actuals in calls.
359
360 -- Does this catch all implicit conversions ???
361
362 if Nkind (Par) = N_Type_Conversion
363 and then Is_Named_Access_Type (Etype (Par))
364 then
365 return Make_Level_Literal
366 (Typ_Access_Level (Etype (Par)));
367 end if;
368
369 -- Jump out when we hit an object declaration or the right-hand
370 -- side of an assignment, or a construct such as an aggregate
371 -- subtype indication which would be the result is not used
372 -- "in its entirety."
373
374 exit when Nkind (Par) in N_Object_Declaration
375 or else (Nkind (Par) = N_Assignment_Statement
376 and then Name (Par) /= Prev_Par);
377
378 Prev_Par := Par;
379 Par := Parent (Par);
380 end loop;
381
382 -- Assignment statements are handled in a similar way in
383 -- accordance to the left-hand part. However, strictly speaking,
384 -- this is illegal according to the RM, but this change is needed
385 -- to pass an ACATS C-test and is useful in general ???
386
387 case Nkind (Par) is
388 when N_Object_Declaration =>
389 return Make_Level_Literal
390 (Scope_Depth
391 (Scope (Defining_Identifier (Par))));
392
393 when N_Assignment_Statement =>
394 -- Return the accessibility level of the left-hand part
395
396 return Accessibility_Level
397 (Expr => Name (Par),
398 Level => Object_Decl_Level,
399 In_Return_Context => In_Return_Context);
400
401 when others =>
402 return Make_Level_Literal
403 (Innermost_Master_Scope_Depth (Expr));
404 end case;
405 end if;
406 end Function_Call_Or_Allocator_Level;
407
408 -- Local variables
409
410 E : Node_Id;
411 Pre : Node_Id;
412
413 -- Start of processing for Accessibility_Level
414
415 begin
416 -- We could be looking at a reference to a formal due to the expansion
417 -- of entries and other cases, so obtain the renaming if necessary.
418
419 if Present (Param_Entity (Expr)) then
420 E := Param_Entity (Expr);
421
422 -- Use the original node unless it is an unanalyzed identifier, as we
423 -- don't want to reason on unanalyzed expressions from predicates.
424
425 elsif Nkind (Original_Node (Expr)) /= N_Identifier
426 or else Analyzed (Original_Node (Expr))
427 then
428 E := Original_Node (Expr);
429
430 else
431 E := Expr;
432 end if;
433
434 -- Extract the entity
435
436 if Nkind (E) in N_Has_Entity and then Present (Entity (E)) then
437 E := Entity (E);
438
439 -- Deal with a possible renaming of a private protected component
440
441 if Ekind (E) in E_Constant | E_Variable and then Is_Prival (E) then
442 E := Prival_Link (E);
443 end if;
444 end if;
445
446 -- Perform the processing on the expression
447
448 case Nkind (E) is
449 -- The level of an aggregate is that of the innermost master that
450 -- evaluates it as defined in RM 3.10.2 (10/4).
451
452 when N_Aggregate =>
453 return Make_Level_Literal (Innermost_Master_Scope_Depth (Expr));
454
455 -- The accessibility level is that of the access type, except for
456 -- anonymous allocators which have special rules defined in RM 3.10.2
457 -- (14/3).
458
459 when N_Allocator =>
460 return Function_Call_Or_Allocator_Level (E);
461
462 -- We could reach this point for two reasons. Either the expression
463 -- applies to a special attribute ('Loop_Entry, 'Result, or 'Old), or
464 -- we are looking at the access attributes directly ('Access,
465 -- 'Address, or 'Unchecked_Access).
466
467 when N_Attribute_Reference =>
468 Pre := Original_Node (Prefix (E));
469
470 -- Regular 'Access attribute presence means we have to look at the
471 -- prefix.
472
473 if Attribute_Name (E) = Name_Access then
474 return Accessibility_Level (Prefix (E));
475
476 -- If we have reached a 'Input attribute then this is the
477 -- the result of the expansion of an object declaration with
478 -- an initial value featuring it. Is this the only case ???
479
480 -- For example:
481
482 -- Opaque : aliased Stream_Element_Array :=
483 -- Stream_Element_Array'Input (S);
484
485 elsif Attribute_Name (E) = Name_Input then
486
487 -- Return the level of the enclosing declaration
488
489 return Make_Level_Literal
490 (Innermost_Master_Scope_Depth
491 (Enclosing_Declaration (Expr)));
492
493 -- Unchecked or unrestricted attributes have unlimited depth
494
495 elsif Attribute_Name (E) in Name_Address
496 | Name_Unchecked_Access
497 | Name_Unrestricted_Access
498 then
499 return Make_Level_Literal (Scope_Depth (Standard_Standard));
500
501 -- 'Access can be taken further against other special attributes,
502 -- so handle these cases explicitly.
503
504 elsif Attribute_Name (E)
505 in Name_Old |
506 Name_Loop_Entry |
507 Name_Result |
508 Name_Super |
509 Name_Tag |
510 Name_Safe_First |
511 Name_Safe_Last |
512 Name_First |
513 Name_Last
514 then
515 -- Named access types
516
517 if Is_Named_Access_Type (Etype (Pre)) then
518 return Make_Level_Literal
519 (Typ_Access_Level (Etype (Pre)));
520
521 -- Anonymous access types
522
523 elsif Nkind (Pre) in N_Has_Entity
524 and then Ekind (Entity (Pre)) not in Subprogram_Kind
525 and then Present (Get_Dynamic_Accessibility (Entity (Pre)))
526 and then Level = Dynamic_Level
527 then
528 pragma Assert (Is_Anonymous_Access_Type (Etype (Pre)));
529 return New_Occurrence_Of
530 (Get_Dynamic_Accessibility (Entity (Pre)), Loc);
531
532 -- Otherwise the level is treated in a similar way as
533 -- aggregates according to RM 6.1.1 (35.1/4) which concerns
534 -- an implicit constant declaration - in turn defining the
535 -- accessibility level to be that of the implicit constant
536 -- declaration.
537
538 else
539 return Make_Level_Literal
540 (Innermost_Master_Scope_Depth (Expr));
541 end if;
542
543 else
544 raise Program_Error;
545 end if;
546
547 -- This is the "base case" for accessibility level calculations which
548 -- means we are near the end of our recursive traversal.
549
550 when N_Defining_Identifier =>
551 -- A dynamic check is performed on the side of the callee when we
552 -- are within a return statement, so return a library-level
553 -- accessibility level to null out checks on the side of the
554 -- caller.
555
556 if Is_Explicitly_Aliased (E)
557 and then (In_Return_Context
558 or else (Level /= Dynamic_Level
559 and then In_Return_Value (Expr)))
560 then
561 return Make_Level_Literal (Scope_Depth (Standard_Standard));
562
563 -- Something went wrong and an extra accessibility formal has not
564 -- been generated when one should have ???
565
566 elsif Is_Formal (E)
567 and then No (Get_Dynamic_Accessibility (E))
568 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
569 then
570 return Make_Level_Literal (Scope_Depth (Standard_Standard));
571
572 -- Stand-alone object of an anonymous access type "SAOAAT"
573
574 elsif (Is_Formal (E)
575 or else Ekind (E) in E_Variable
576 | E_Constant)
577 and then Present (Get_Dynamic_Accessibility (E))
578 and then (Level = Dynamic_Level
579 or else Level = Zero_On_Dynamic_Level)
580 then
581 if Level = Zero_On_Dynamic_Level then
582 return Make_Level_Literal
583 (Scope_Depth (Standard_Standard));
584 end if;
585
586 -- No_Dynamic_Accessibility_Checks restriction override for
587 -- alternative accessibility model.
588
589 if Allow_Alt_Model
590 and then No_Dynamic_Accessibility_Checks_Enabled (E)
591 then
592 -- In the alternative model the level is that of the
593 -- designated type entity's context.
594
595 if Debug_Flag_Underscore_B then
596 return Make_Level_Literal (Typ_Access_Level (Etype (E)));
597
598 -- Otherwise the level depends on the entity's context
599
600 elsif Is_Formal (E) then
601 return Make_Level_Literal
602 (Subprogram_Access_Level
603 (Enclosing_Subprogram (E)));
604 else
605 return Make_Level_Literal
606 (Scope_Depth (Enclosing_Dynamic_Scope (E)));
607 end if;
608 end if;
609
610 -- Return the dynamic level in the normal case
611
612 return New_Occurrence_Of
613 (Get_Dynamic_Accessibility (E), Loc);
614
615 -- Initialization procedures have a special extra accessibility
616 -- parameter associated with the level at which the object
617 -- being initialized exists
618
619 elsif Ekind (E) = E_Record_Type
620 and then Is_Limited_Record (E)
621 and then Current_Scope = Init_Proc (E)
622 and then Present (Init_Proc_Level_Formal (Current_Scope))
623 then
624 return New_Occurrence_Of
625 (Init_Proc_Level_Formal (Current_Scope), Loc);
626
627 -- Current instance of the type is deeper than that of the type
628 -- according to RM 3.10.2 (21).
629
630 elsif Is_Type (E) then
631 -- When restriction No_Dynamic_Accessibility_Checks is active
632 -- along with -gnatd_b.
633
634 if Allow_Alt_Model
635 and then No_Dynamic_Accessibility_Checks_Enabled (E)
636 and then Debug_Flag_Underscore_B
637 then
638 return Make_Level_Literal (Typ_Access_Level (E));
639 end if;
640
641 -- Normal path
642
643 return Make_Level_Literal (Typ_Access_Level (E) + 1);
644
645 -- Move up the renamed entity or object if it came from source
646 -- since expansion may have created a dummy renaming under
647 -- certain circumstances.
648
649 -- Note: We check if the original node of the renaming comes
650 -- from source because the node may have been rewritten.
651
652 elsif Present (Renamed_Entity_Or_Object (E))
653 and then Comes_From_Source
654 (Original_Node (Renamed_Entity_Or_Object (E)))
655 then
656 return Accessibility_Level (Renamed_Entity_Or_Object (E));
657
658 -- Named access types get their level from their associated type
659
660 elsif Is_Named_Access_Type (Etype (E)) then
661 return Make_Level_Literal
662 (Typ_Access_Level (Etype (E)));
663
664 -- Check if E is an expansion-generated renaming of an iterator
665 -- by examining Related_Expression. If so, determine the
666 -- accessibility level based on the original expression.
667
668 elsif Ekind (E) in E_Constant | E_Variable
669 and then Present (Related_Expression (E))
670 then
671 return Accessibility_Level (Related_Expression (E));
672
673 elsif Level = Dynamic_Level
674 and then Ekind (E) in E_In_Parameter | E_In_Out_Parameter
675 and then Present (Init_Proc_Level_Formal (Scope (E)))
676 then
677 return New_Occurrence_Of
678 (Init_Proc_Level_Formal (Scope (E)), Loc);
679
680 -- Normal object - get the level of the enclosing scope
681
682 else
683 return Make_Level_Literal
684 (Scope_Depth (Enclosing_Dynamic_Scope (E)));
685 end if;
686
687 -- Handle indexed and selected components including the special cases
688 -- whereby there is an implicit dereference, a component of a
689 -- composite type, or a function call in prefix notation.
690
691 -- We don't handle function calls in prefix notation correctly ???
692
693 when N_Indexed_Component | N_Selected_Component | N_Slice =>
694 Pre := Prefix (E);
695
696 -- Fetch the original node when the prefix comes from the result
697 -- of expanding a function call since we want to find the level
698 -- of the original source call.
699
700 if not Comes_From_Source (Pre)
701 and then Nkind (Original_Node (Pre)) = N_Function_Call
702 then
703 Pre := Original_Node (Pre);
704 end if;
705
706 -- When E is an indexed component or selected component and
707 -- the current Expr is a function call, we know that we are
708 -- looking at an expanded call in prefix notation.
709
710 if Nkind (Expr) = N_Function_Call then
711 return Function_Call_Or_Allocator_Level (Expr);
712
713 -- If the prefix is a named access type, then we are dealing
714 -- with an implicit deferences. In that case the level is that
715 -- of the named access type in the prefix.
716
717 elsif Is_Named_Access_Type (Etype (Pre)) then
718 return Make_Level_Literal
719 (Typ_Access_Level (Etype (Pre)));
720
721 -- The current expression is a named access type, so there is no
722 -- reason to look at the prefix. Instead obtain the level of E's
723 -- named access type.
724
725 elsif Is_Named_Access_Type (Etype (E)) then
726 return Make_Level_Literal
727 (Typ_Access_Level (Etype (E)));
728
729 -- A nondiscriminant selected component where the component
730 -- is an anonymous access type means that its associated
731 -- level is that of the containing type - see RM 3.10.2 (16).
732
733 -- Note that when restriction No_Dynamic_Accessibility_Checks is
734 -- in effect we treat discriminant components as regular
735 -- components.
736
737 elsif
738 (Nkind (E) = N_Selected_Component
739 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
740 and then Ekind (Etype (Pre)) /= E_Anonymous_Access_Type
741 and then (not (Nkind (Selector_Name (E)) in N_Has_Entity
742 and then Ekind (Entity (Selector_Name (E)))
743 = E_Discriminant)
744
745 -- The alternative accessibility models both treat
746 -- discriminants as regular components.
747
748 or else (No_Dynamic_Accessibility_Checks_Enabled (E)
749 and then Allow_Alt_Model)))
750
751 -- Arrays featuring components of anonymous access components
752 -- get their corresponding level from their containing type's
753 -- declaration.
754
755 or else
756 (Nkind (E) = N_Indexed_Component
757 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
758 and then Ekind (Etype (Pre)) in Array_Kind
759 and then Ekind (Component_Type (Base_Type (Etype (Pre))))
760 = E_Anonymous_Access_Type)
761 then
762 -- When restriction No_Dynamic_Accessibility_Checks is active
763 -- and -gnatd_b set, the level is that of the designated type.
764
765 if Allow_Alt_Model
766 and then No_Dynamic_Accessibility_Checks_Enabled (E)
767 and then Debug_Flag_Underscore_B
768 then
769 return Make_Level_Literal
770 (Typ_Access_Level (Etype (E)));
771 end if;
772
773 -- Otherwise proceed normally
774
775 return Make_Level_Literal
776 (Typ_Access_Level (Etype (Prefix (E))));
777
778 -- The accessibility calculation routine that handles function
779 -- calls (Function_Call_Level) assumes, in the case the
780 -- result is of an anonymous access type, that the result will be
781 -- used "in its entirety" when the call is present within an
782 -- assignment or object declaration.
783
784 -- To properly handle cases where the result is not used in its
785 -- entirety, we test if the prefix of the component in question is
786 -- a function call, which tells us that one of its components has
787 -- been identified and is being accessed. Therefore we can
788 -- conclude that the result is not used "in its entirety"
789 -- according to RM 3.10.2 (10.2/3).
790
791 elsif Nkind (Pre) = N_Function_Call
792 and then not Is_Named_Access_Type (Etype (Pre))
793 then
794 -- Dynamic checks are generated when we are within a return
795 -- value or we are in a function call within an anonymous
796 -- access discriminant constraint of a return object (signified
797 -- by In_Return_Context) on the side of the callee.
798
799 -- So, in this case, return a library accessibility level to
800 -- null out the check on the side of the caller.
801
802 if (In_Return_Value (E)
803 or else In_Return_Context)
804 and then Level /= Dynamic_Level
805 then
806 return Make_Level_Literal
807 (Scope_Depth (Standard_Standard));
808 end if;
809
810 return Make_Level_Literal
811 (Innermost_Master_Scope_Depth (Expr));
812
813 -- Otherwise, continue recursing over the expression prefixes
814
815 else
816 return Accessibility_Level (Prefix (E));
817 end if;
818
819 -- Qualified expressions
820
821 when N_Qualified_Expression =>
822 if Is_Named_Access_Type (Etype (E)) then
823 return Make_Level_Literal
824 (Typ_Access_Level (Etype (E)));
825 else
826 return Accessibility_Level (Expression (E));
827 end if;
828
829 -- Handle function calls
830
831 when N_Function_Call =>
832 return Function_Call_Or_Allocator_Level (E);
833
834 -- Explicit dereference accessibility level calculation
835
836 when N_Explicit_Dereference =>
837 Pre := Original_Node (Prefix (E));
838
839 -- The prefix is a named access type so the level is taken from
840 -- its type.
841
842 if Is_Named_Access_Type (Etype (Pre)) then
843 return Make_Level_Literal (Typ_Access_Level (Etype (Pre)));
844
845 -- Otherwise, recurse deeper
846
847 else
848 return Accessibility_Level (Prefix (E));
849 end if;
850
851 -- Type conversions
852
853 when N_Type_Conversion | N_Unchecked_Type_Conversion =>
854 -- View conversions are special in that they require use to
855 -- inspect the expression of the type conversion.
856
857 -- Allocators of anonymous access types are internally generated,
858 -- so recurse deeper in that case as well.
859
860 if Is_View_Conversion (E)
861 or else Ekind (Etype (E)) = E_Anonymous_Access_Type
862 then
863 return Accessibility_Level (Expression (E));
864
865 -- We don't care about the master if we are looking at a named
866 -- access type.
867
868 elsif Is_Named_Access_Type (Etype (E)) then
869 return Make_Level_Literal
870 (Typ_Access_Level (Etype (E)));
871
872 -- In section RM 3.10.2 (10/4) the accessibility rules for
873 -- aggregates and value conversions are outlined. Are these
874 -- followed in the case of initialization of an object ???
875
876 -- Should use Innermost_Master_Scope_Depth ???
877
878 else
879 return Accessibility_Level (Current_Scope);
880 end if;
881
882 -- Default to the type accessibility level for the type of the
883 -- expression's entity.
884
885 when others =>
886 return Make_Level_Literal (Typ_Access_Level (Etype (E)));
887 end case;
888 end Accessibility_Level;
889
890 -------------------------------
891 -- Apply_Accessibility_Check --
892 -------------------------------
893
894 procedure Apply_Accessibility_Check
895 (N : Node_Id;
896 Typ : Entity_Id;
897 Insert_Node : Node_Id)
898 is
899 Loc : constant Source_Ptr := Sloc (N);
900
901 Check_Cond : Node_Id;
902 Param_Ent : Entity_Id := Param_Entity (N);
903 Param_Level : Node_Id;
904 Type_Level : Node_Id;
905
906 begin
907 -- Verify we haven't tried to add a dynamic accessibility check when we
908 -- shouldn't.
909
910 pragma Assert (not No_Dynamic_Accessibility_Checks_Enabled (N));
911
912 if Ada_Version >= Ada_2012
913 and then No (Param_Ent)
914 and then Is_Entity_Name (N)
915 and then Ekind (Entity (N)) in E_Constant | E_Variable
916 and then Present (Effective_Extra_Accessibility (Entity (N)))
917 then
918 Param_Ent := Entity (N);
919 while Present (Renamed_Object (Param_Ent)) loop
920 -- Renamed_Object must return an Entity_Name here
921 -- because of preceding "Present (E_E_A (...))" test.
922
923 Param_Ent := Entity (Renamed_Object (Param_Ent));
924 end loop;
925 end if;
926
927 if Inside_A_Generic then
928 return;
929
930 -- Only apply the run-time check if the access parameter has an
931 -- associated extra access level parameter and when accessibility checks
932 -- are enabled.
933
934 elsif Present (Param_Ent)
935 and then Present (Get_Dynamic_Accessibility (Param_Ent))
936 and then not Accessibility_Checks_Suppressed (Param_Ent)
937 and then not Accessibility_Checks_Suppressed (Typ)
938 then
939 -- Obtain the parameter's accessibility level
940
941 Param_Level :=
942 New_Occurrence_Of (Get_Dynamic_Accessibility (Param_Ent), Loc);
943
944 -- Use the dynamic accessibility parameter for the function's result
945 -- when one has been created instead of statically referring to the
946 -- deepest type level so as to appropriatly handle the rules for
947 -- RM 3.10.2 (10.1/3).
948
949 if Ekind (Scope (Param_Ent)) = E_Function
950 and then In_Return_Value (N)
951 and then Ekind (Typ) = E_Anonymous_Access_Type
952 then
953 -- Associate the level of the result type to the extra result
954 -- accessibility parameter belonging to the current function.
955
956 if Present (Extra_Accessibility_Of_Result (Scope (Param_Ent))) then
957 Type_Level :=
958 New_Occurrence_Of
959 (Extra_Accessibility_Of_Result (Scope (Param_Ent)), Loc);
960
961 -- In Ada 2005 and earlier modes, a result extra accessibility
962 -- parameter is not generated and no dynamic check is performed.
963
964 else
965 return;
966 end if;
967
968 -- Otherwise get the type's accessibility level normally
969
970 else
971 Type_Level :=
972 Make_Integer_Literal (Loc, Deepest_Type_Access_Level (Typ));
973 end if;
974
975 -- Raise Program_Error if the accessibility level of the access
976 -- parameter is deeper than the level of the target access type.
977
978 Check_Cond :=
979 Make_Op_Gt (Loc,
980 Left_Opnd => Param_Level,
981 Right_Opnd => Type_Level);
982
983 Insert_Action (Insert_Node,
984 Make_Raise_Program_Error (Loc,
985 Condition => Check_Cond,
986 Reason => PE_Accessibility_Check_Failed));
987
988 Analyze_And_Resolve (N);
989
990 -- If constant folding has happened on the condition for the
991 -- generated error, then warn about it being unconditional.
992
993 if Nkind (Check_Cond) = N_Identifier
994 and then Entity (Check_Cond) = Standard_True
995 then
996 Error_Msg_Warn := SPARK_Mode /= On;
997 Error_Msg_N ("accessibility check fails<<", N);
998 Error_Msg_N ("\Program_Error [<<", N);
999 end if;
1000 end if;
1001 end Apply_Accessibility_Check;
1002
1003 ---------------------------------------------
1004 -- Apply_Accessibility_Check_For_Allocator --
1005 ---------------------------------------------
1006
1007 procedure Apply_Accessibility_Check_For_Allocator
1008 (N : Node_Id;
1009 Exp : Node_Id;
1010 Ref : Node_Id;
1011 Built_In_Place : Boolean := False)
1012 is
1013 Loc : constant Source_Ptr := Sloc (N);
1014 PtrT : constant Entity_Id := Etype (N);
1015 DesigT : constant Entity_Id := Designated_Type (PtrT);
1016 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
1017 Cond : Node_Id;
1018 Fin_Call : Node_Id;
1019 Free_Stmt : Node_Id;
1020 Obj_Ref : Node_Id;
1021 Stmts : List_Id;
1022
1023 begin
1024 if Ada_Version >= Ada_2005
1025 and then Is_Class_Wide_Type (DesigT)
1026 and then Tagged_Type_Expansion
1027 and then not Scope_Suppress.Suppress (Accessibility_Check)
1028 and then not No_Dynamic_Accessibility_Checks_Enabled (Ref)
1029 and then
1030 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
1031 or else
1032 (Is_Class_Wide_Type (Etype (Exp))
1033 and then Scope (PtrT) /= Current_Scope))
1034 then
1035 -- If the allocator was built in place, Ref is already a reference
1036 -- to the access object initialized to the result of the allocator
1037 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
1038 -- Remove_Side_Effects for cases where the build-in-place call may
1039 -- still be the prefix of the reference (to avoid generating
1040 -- duplicate calls). Otherwise, it is the entity associated with
1041 -- the object containing the address of the allocated object.
1042
1043 if Built_In_Place then
1044 Remove_Side_Effects (Ref);
1045 Obj_Ref := New_Copy_Tree (Ref);
1046 else
1047 Obj_Ref := New_Occurrence_Of (Ref, Loc);
1048 end if;
1049
1050 -- For access to interface types we must generate code to displace
1051 -- the pointer to the base of the object since the subsequent code
1052 -- references components located in the TSD of the object (which
1053 -- is associated with the primary dispatch table --see a-tags.ads)
1054 -- and also generates code invoking Free, which requires also a
1055 -- reference to the base of the unallocated object.
1056
1057 if Is_Interface (DesigT) and then Tagged_Type_Expansion then
1058 Obj_Ref :=
1059 Unchecked_Convert_To (Etype (Obj_Ref),
1060 Make_Function_Call (Loc,
1061 Name =>
1062 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
1063 Parameter_Associations => New_List (
1064 Unchecked_Convert_To (RTE (RE_Address),
1065 New_Copy_Tree (Obj_Ref)))));
1066 end if;
1067
1068 -- Step 1: Create the object clean up code
1069
1070 Stmts := New_List;
1071
1072 -- Deallocate the object if the accessibility check fails. This is
1073 -- done only on targets or profiles that support deallocation.
1074
1075 -- Free (Obj_Ref);
1076
1077 if RTE_Available (RE_Free) then
1078 Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref));
1079 Set_Storage_Pool (Free_Stmt, Pool_Id);
1080
1081 Append_To (Stmts, Free_Stmt);
1082
1083 -- The target or profile cannot deallocate objects
1084
1085 else
1086 Free_Stmt := Empty;
1087 end if;
1088
1089 -- Finalize the object if applicable. Generate:
1090
1091 -- [Deep_]Finalize (Obj_Ref.all);
1092
1093 if Needs_Finalization (DesigT)
1094 and then not No_Heap_Finalization (PtrT)
1095 then
1096 Fin_Call :=
1097 Make_Final_Call
1098 (Obj_Ref =>
1099 Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)),
1100 Typ => DesigT);
1101
1102 -- Guard against a missing [Deep_]Finalize when the designated
1103 -- type was not properly frozen.
1104
1105 if No (Fin_Call) then
1106 Fin_Call := Make_Null_Statement (Loc);
1107 end if;
1108
1109 -- When the target or profile supports deallocation, wrap the
1110 -- finalization call in a block to ensure proper deallocation even
1111 -- if finalization fails. Generate:
1112
1113 -- begin
1114 -- <Fin_Call>
1115 -- exception
1116 -- when others =>
1117 -- <Free_Stmt>
1118 -- raise;
1119 -- end;
1120
1121 if Present (Free_Stmt) then
1122 Fin_Call :=
1123 Make_Block_Statement (Loc,
1124 Handled_Statement_Sequence =>
1125 Make_Handled_Sequence_Of_Statements (Loc,
1126 Statements => New_List (Fin_Call),
1127
1128 Exception_Handlers => New_List (
1129 Make_Exception_Handler (Loc,
1130 Exception_Choices => New_List (
1131 Make_Others_Choice (Loc)),
1132 Statements => New_List (
1133 New_Copy_Tree (Free_Stmt),
1134 Make_Raise_Statement (Loc))))));
1135 end if;
1136
1137 Prepend_To (Stmts, Fin_Call);
1138 end if;
1139
1140 -- Signal the accessibility failure through a Program_Error
1141
1142 Append_To (Stmts,
1143 Make_Raise_Program_Error (Loc,
1144 Reason => PE_Accessibility_Check_Failed));
1145
1146 -- Step 2: Create the accessibility comparison
1147
1148 -- Generate:
1149 -- Ref'Tag
1150
1151 Obj_Ref :=
1152 Make_Attribute_Reference (Loc,
1153 Prefix => Obj_Ref,
1154 Attribute_Name => Name_Tag);
1155
1156 -- For tagged types, determine the accessibility level by looking at
1157 -- the type specific data of the dispatch table. Generate:
1158
1159 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
1160
1161 if Tagged_Type_Expansion then
1162 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
1163
1164 -- Use a runtime call to determine the accessibility level when
1165 -- compiling on virtual machine targets. Generate:
1166
1167 -- Get_Access_Level (Ref'Tag)
1168
1169 else
1170 Cond :=
1171 Make_Function_Call (Loc,
1172 Name =>
1173 New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc),
1174 Parameter_Associations => New_List (Obj_Ref));
1175 end if;
1176
1177 Cond :=
1178 Make_Op_Gt (Loc,
1179 Left_Opnd => Cond,
1180 Right_Opnd => Accessibility_Level (N, Dynamic_Level));
1181
1182 -- Due to the complexity and side effects of the check, utilize an if
1183 -- statement instead of the regular Program_Error circuitry.
1184
1185 Insert_Action (N,
1186 Make_Implicit_If_Statement (N,
1187 Condition => Cond,
1188 Then_Statements => Stmts));
1189 end if;
1190 end Apply_Accessibility_Check_For_Allocator;
1191
1192 ------------------------------------------
1193 -- Check_Return_Construct_Accessibility --
1194 ------------------------------------------
1195
1196 procedure Check_Return_Construct_Accessibility
1197 (Return_Stmt : Node_Id;
1198 Stm_Entity : Entity_Id)
1199 is
1200 Loc : constant Source_Ptr := Sloc (Return_Stmt);
1201 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
1202
1203 R_Type : constant Entity_Id := Etype (Scope_Id);
1204 -- Function result subtype
1205
1206 function First_Selector (Assoc : Node_Id) return Node_Id;
1207 -- Obtain the first selector or choice from a given association
1208
1209 function Is_Formal_Of_Current_Function
1210 (Assoc_Expr : Node_Id) return Boolean;
1211 -- Predicate to test if a given expression associated with a
1212 -- discriminant is a formal parameter to the function in which the
1213 -- return construct we checking applies to.
1214
1215 --------------------
1216 -- First_Selector --
1217 --------------------
1218
1219 function First_Selector (Assoc : Node_Id) return Node_Id is
1220 begin
1221 if Nkind (Assoc) = N_Component_Association then
1222 return First (Choices (Assoc));
1223
1224 elsif Nkind (Assoc) = N_Discriminant_Association then
1225 return First (Selector_Names (Assoc));
1226
1227 else
1228 raise Program_Error;
1229 end if;
1230 end First_Selector;
1231
1232 -----------------------------------
1233 -- Is_Formal_Of_Current_Function --
1234 -----------------------------------
1235
1236 function Is_Formal_Of_Current_Function
1237 (Assoc_Expr : Node_Id) return Boolean is
1238 begin
1239 return Is_Entity_Name (Assoc_Expr)
1240 and then Enclosing_Subprogram
1241 (Entity (Assoc_Expr)) = Scope_Id
1242 and then Is_Formal (Entity (Assoc_Expr));
1243 end Is_Formal_Of_Current_Function;
1244
1245 -- Local declarations
1246
1247 Assoc : Node_Id := Empty;
1248 -- Assoc should perhaps be renamed and declared as a
1249 -- Node_Or_Entity_Id since it encompasses not only component and
1250 -- discriminant associations, but also discriminant components within
1251 -- a type declaration or subtype indication ???
1252
1253 Assoc_Expr : Node_Id;
1254 Assoc_Present : Boolean := False;
1255
1256 Check_Cond : Node_Id;
1257 Unseen_Disc_Count : Nat := 0;
1258 Seen_Discs : Elist_Id;
1259 Disc : Entity_Id;
1260 First_Disc : Entity_Id;
1261
1262 Obj_Decl : Node_Id;
1263 Return_Con : Node_Id;
1264 Unqual : Node_Id;
1265
1266 -- Start of processing for Check_Return_Construct_Accessibility
1267
1268 begin
1269 -- Only perform checks on record types with access discriminants and
1270 -- non-internally generated functions.
1271
1272 if not Is_Record_Type (R_Type)
1273 or else not Has_Anonymous_Access_Discriminant (R_Type)
1274 or else not Comes_From_Source (Return_Stmt)
1275 then
1276 return;
1277 end if;
1278
1279 -- We are only interested in return statements
1280
1281 if Nkind (Return_Stmt) not in
1282 N_Extended_Return_Statement | N_Simple_Return_Statement
1283 then
1284 return;
1285 end if;
1286
1287 -- Fetch the object from the return statement, in the case of a
1288 -- simple return statement the expression is part of the node.
1289
1290 if Nkind (Return_Stmt) = N_Extended_Return_Statement then
1291 -- Obtain the object definition from the expanded extended return
1292
1293 Return_Con := First (Return_Object_Declarations (Return_Stmt));
1294 while Present (Return_Con) loop
1295 -- Inspect the original node to avoid object declarations
1296 -- expanded into renamings.
1297
1298 if Nkind (Original_Node (Return_Con)) = N_Object_Declaration
1299 and then Comes_From_Source (Original_Node (Return_Con))
1300 then
1301 exit;
1302 end if;
1303
1304 Next (Return_Con);
1305 end loop;
1306
1307 pragma Assert (Present (Return_Con));
1308
1309 -- Could be dealing with a renaming
1310
1311 Return_Con := Original_Node (Return_Con);
1312 else
1313 Return_Con := Expression (Return_Stmt);
1314 end if;
1315
1316 -- Obtain the accessibility levels of the expressions associated
1317 -- with all anonymous access discriminants, then generate a
1318 -- dynamic check or static error when relevant.
1319
1320 -- Note the repeated use of Original_Node to avoid checking
1321 -- expanded code.
1322
1323 Unqual := Original_Node (Unqualify (Original_Node (Return_Con)));
1324
1325 -- Get the corresponding declaration based on the return object's
1326 -- identifier.
1327
1328 if Nkind (Unqual) = N_Identifier
1329 and then Nkind (Parent (Entity (Unqual)))
1330 in N_Object_Declaration
1331 | N_Object_Renaming_Declaration
1332 then
1333 Obj_Decl := Original_Node (Parent (Entity (Unqual)));
1334
1335 -- We were passed the object declaration directly, so use it
1336
1337 elsif Nkind (Unqual) in N_Object_Declaration
1338 | N_Object_Renaming_Declaration
1339 then
1340 Obj_Decl := Unqual;
1341
1342 -- Otherwise, we are looking at something else
1343
1344 else
1345 Obj_Decl := Empty;
1346
1347 end if;
1348
1349 -- Hop up object renamings when present
1350
1351 if Present (Obj_Decl)
1352 and then Nkind (Obj_Decl) = N_Object_Renaming_Declaration
1353 then
1354 while Nkind (Obj_Decl) = N_Object_Renaming_Declaration loop
1355
1356 if Nkind (Name (Obj_Decl)) not in N_Entity then
1357 -- We may be looking at the expansion of iterators or
1358 -- some other internally generated construct, so it is safe
1359 -- to ignore checks ???
1360
1361 if not Comes_From_Source (Obj_Decl) then
1362 return;
1363 end if;
1364
1365 Obj_Decl := Original_Node
1366 (Declaration_Node
1367 (Ultimate_Prefix (Name (Obj_Decl))));
1368
1369 -- Move up to the next declaration based on the object's name
1370
1371 else
1372 Obj_Decl := Original_Node
1373 (Declaration_Node (Name (Obj_Decl)));
1374 end if;
1375 end loop;
1376 end if;
1377
1378 -- Obtain the discriminant values from the return aggregate
1379
1380 -- Do we cover extension aggregates correctly ???
1381
1382 if Nkind (Unqual) = N_Aggregate then
1383 if Present (Expressions (Unqual)) then
1384 Assoc := First (Expressions (Unqual));
1385 else
1386 Assoc := First (Component_Associations (Unqual));
1387 end if;
1388
1389 -- There is an object declaration for the return object
1390
1391 elsif Present (Obj_Decl) then
1392 -- When a subtype indication is present in an object declaration
1393 -- it must contain the object's discriminants.
1394
1395 if Nkind (Object_Definition (Obj_Decl)) = N_Subtype_Indication then
1396 Assoc := First
1397 (Constraints
1398 (Constraint
1399 (Object_Definition (Obj_Decl))));
1400
1401 -- The object declaration contains an aggregate
1402
1403 elsif Present (Expression (Obj_Decl)) then
1404
1405 if Nkind (Unqualify (Expression (Obj_Decl))) = N_Aggregate then
1406 -- Grab the first associated discriminant expresion
1407
1408 if Present
1409 (Expressions (Unqualify (Expression (Obj_Decl))))
1410 then
1411 Assoc := First
1412 (Expressions
1413 (Unqualify (Expression (Obj_Decl))));
1414 else
1415 Assoc := First
1416 (Component_Associations
1417 (Unqualify (Expression (Obj_Decl))));
1418 end if;
1419
1420 -- Otherwise, this is something else
1421
1422 else
1423 return;
1424 end if;
1425
1426 -- There are no supplied discriminants in the object declaration,
1427 -- so get them from the type definition since they must be default
1428 -- initialized.
1429
1430 -- Do we handle constrained subtypes correctly ???
1431
1432 elsif Nkind (Unqual) = N_Object_Declaration then
1433 Assoc := First_Discriminant
1434 (Etype (Object_Definition (Obj_Decl)));
1435
1436 else
1437 Assoc := First_Discriminant (Etype (Unqual));
1438 end if;
1439
1440 -- When we are not looking at an aggregate or an identifier, return
1441 -- since any other construct (like a function call) is not
1442 -- applicable since checks will be performed on the side of the
1443 -- callee.
1444
1445 else
1446 return;
1447 end if;
1448
1449 -- Obtain the discriminants so we know the actual type in case the
1450 -- value of their associated expression gets implicitly converted.
1451
1452 if No (Obj_Decl) then
1453 pragma Assert (Nkind (Unqual) = N_Aggregate);
1454
1455 Disc := First_Discriminant (Etype (Unqual));
1456
1457 else
1458 Disc := First_Discriminant
1459 (Etype (Defining_Identifier (Obj_Decl)));
1460 end if;
1461
1462 -- Preserve the first discriminant for checking named associations
1463
1464 First_Disc := Disc;
1465
1466 -- Count the number of discriminants for processing an aggregate
1467 -- which includes an others.
1468
1469 Disc := First_Disc;
1470 while Present (Disc) loop
1471 Unseen_Disc_Count := Unseen_Disc_Count + 1;
1472
1473 Next_Discriminant (Disc);
1474 end loop;
1475
1476 Seen_Discs := New_Elmt_List;
1477
1478 -- Loop through each of the discriminants and check each expression
1479 -- associated with an anonymous access discriminant.
1480
1481 -- When named associations occur in the return aggregate then
1482 -- discriminants can be in any order, so we need to ensure we do
1483 -- not continue to loop when all discriminants have been seen.
1484
1485 Disc := First_Disc;
1486 while Present (Assoc)
1487 and then (Present (Disc) or else Assoc_Present)
1488 and then Unseen_Disc_Count > 0
1489 loop
1490 -- Handle named associations by searching through the names of
1491 -- the relevant discriminant components.
1492
1493 if Nkind (Assoc)
1494 in N_Component_Association | N_Discriminant_Association
1495 then
1496 Assoc_Expr := Expression (Assoc);
1497 Assoc_Present := True;
1498
1499 -- We currently don't handle box initialized discriminants,
1500 -- however, since default initialized anonymous access
1501 -- discriminants are a corner case, this is ok for now ???
1502
1503 if Nkind (Assoc) = N_Component_Association
1504 and then Box_Present (Assoc)
1505 then
1506 if Nkind (First_Selector (Assoc)) = N_Others_Choice then
1507 Unseen_Disc_Count := 0;
1508 end if;
1509
1510 -- When others is present we must identify a discriminant we
1511 -- haven't already seen so as to get the appropriate type for
1512 -- the static accessibility check.
1513
1514 -- This works because all components within an others clause
1515 -- must have the same type.
1516
1517 elsif Nkind (First_Selector (Assoc)) = N_Others_Choice then
1518
1519 Disc := First_Disc;
1520 Outer : while Present (Disc) loop
1521 declare
1522 Current_Seen_Disc : Elmt_Id;
1523 begin
1524 -- Move through the list of identified discriminants
1525
1526 Current_Seen_Disc := First_Elmt (Seen_Discs);
1527 while Present (Current_Seen_Disc) loop
1528 -- Exit the loop when we found a match
1529
1530 exit when
1531 Chars (Node (Current_Seen_Disc)) = Chars (Disc);
1532
1533 Next_Elmt (Current_Seen_Disc);
1534 end loop;
1535
1536 -- When we have exited the above loop without finding
1537 -- a match then we know that Disc has not been seen.
1538
1539 exit Outer when No (Current_Seen_Disc);
1540 end;
1541
1542 Next_Discriminant (Disc);
1543 end loop Outer;
1544
1545 -- If we got to an others clause with a non-zero
1546 -- discriminant count there must be a discriminant left to
1547 -- check.
1548
1549 pragma Assert (Present (Disc));
1550
1551 -- Set the unseen discriminant count to zero because we know
1552 -- an others clause sets all remaining components of an
1553 -- aggregate.
1554
1555 Unseen_Disc_Count := 0;
1556
1557 -- Move through each of the selectors in the named association
1558 -- and obtain a discriminant for accessibility checking if one
1559 -- is referenced in the list. Also track which discriminants
1560 -- are referenced for the purpose of handling an others clause.
1561
1562 else
1563 declare
1564 Assoc_Choice : Node_Id;
1565 Curr_Disc : Node_Id;
1566 begin
1567
1568 Disc := Empty;
1569 Curr_Disc := First_Disc;
1570 while Present (Curr_Disc) loop
1571 -- Check each of the choices in the associations for a
1572 -- match to the name of the current discriminant.
1573
1574 Assoc_Choice := First_Selector (Assoc);
1575 while Present (Assoc_Choice) loop
1576 -- When the name matches we track that we have seen
1577 -- the discriminant, but instead of exiting the
1578 -- loop we continue iterating to make sure all the
1579 -- discriminants within the named association get
1580 -- tracked.
1581
1582 if Chars (Assoc_Choice) = Chars (Curr_Disc) then
1583 Append_Elmt (Curr_Disc, Seen_Discs);
1584
1585 Disc := Curr_Disc;
1586 Unseen_Disc_Count := Unseen_Disc_Count - 1;
1587 end if;
1588
1589 Next (Assoc_Choice);
1590 end loop;
1591
1592 Next_Discriminant (Curr_Disc);
1593 end loop;
1594 end;
1595 end if;
1596
1597 -- Unwrap the associated expression if we are looking at a default
1598 -- initialized type declaration. In this case Assoc is not really
1599 -- an association, but a component declaration. Should Assoc be
1600 -- renamed in some way to be more clear ???
1601
1602 -- This occurs when the return object does not initialize
1603 -- discriminant and instead relies on the type declaration for
1604 -- their supplied values.
1605
1606 elsif Nkind (Assoc) in N_Entity
1607 and then Ekind (Assoc) = E_Discriminant
1608 then
1609 Append_Elmt (Disc, Seen_Discs);
1610
1611 Assoc_Expr := Discriminant_Default_Value (Assoc);
1612 Unseen_Disc_Count := Unseen_Disc_Count - 1;
1613
1614 -- Otherwise, there is nothing to do because Assoc is an
1615 -- expression within the return aggregate itself.
1616
1617 else
1618 Append_Elmt (Disc, Seen_Discs);
1619
1620 Assoc_Expr := Assoc;
1621 Unseen_Disc_Count := Unseen_Disc_Count - 1;
1622 end if;
1623
1624 -- Check the accessibility level of the expression when the
1625 -- discriminant is of an anonymous access type.
1626
1627 if Present (Assoc_Expr)
1628 and then Present (Disc)
1629 and then Ekind (Etype (Disc)) = E_Anonymous_Access_Type
1630
1631 -- We disable the check when we have a tagged return type and
1632 -- the associated expression for the discriminant is a formal
1633 -- parameter since the check would require us to compare the
1634 -- accessibility level of Assoc_Expr to the level of the
1635 -- Extra_Accessibility_Of_Result of the function - which is
1636 -- currently disabled for functions with tagged return types.
1637 -- This may change in the future ???
1638
1639 -- See Needs_Result_Accessibility_Level for details.
1640
1641 and then not
1642 (No (Extra_Accessibility_Of_Result (Scope_Id))
1643 and then Is_Formal_Of_Current_Function (Assoc_Expr)
1644 and then Is_Tagged_Type (Etype (Scope_Id)))
1645 then
1646 -- Generate a dynamic check based on the extra accessibility of
1647 -- the result or the scope of the current function.
1648
1649 Check_Cond :=
1650 Make_Op_Gt (Loc,
1651 Left_Opnd => Accessibility_Level
1652 (Expr => Assoc_Expr,
1653 Level => Dynamic_Level,
1654 In_Return_Context => True),
1655 Right_Opnd =>
1656 (if Present (Extra_Accessibility_Of_Result (Scope_Id))
1657
1658 -- When Assoc_Expr is a formal we have to look at the
1659 -- extra accessibility-level formal associated with
1660 -- the result.
1661
1662 and then Is_Formal_Of_Current_Function (Assoc_Expr)
1663 then
1664 New_Occurrence_Of
1665 (Extra_Accessibility_Of_Result (Scope_Id), Loc)
1666
1667 -- Otherwise, we compare the level of Assoc_Expr to the
1668 -- scope of the current function.
1669
1670 else
1671 Make_Integer_Literal
1672 (Loc, Scope_Depth (Scope (Scope_Id)))));
1673
1674 Insert_Before_And_Analyze (Return_Stmt,
1675 Make_Raise_Program_Error (Loc,
1676 Condition => Check_Cond,
1677 Reason => PE_Accessibility_Check_Failed));
1678
1679 -- If constant folding has happened on the condition for the
1680 -- generated error, then warn about it being unconditional when
1681 -- we know an error will be raised.
1682
1683 if Nkind (Check_Cond) = N_Identifier
1684 and then Entity (Check_Cond) = Standard_True
1685 then
1686 Error_Msg_N
1687 ("access discriminant in return object would be a dangling"
1688 & " reference", Return_Stmt);
1689 end if;
1690 end if;
1691
1692 -- Iterate over the discriminants, except when we have encountered
1693 -- a named association since the discriminant order becomes
1694 -- irrelevant in that case.
1695
1696 if not Assoc_Present then
1697 Next_Discriminant (Disc);
1698 end if;
1699
1700 -- Iterate over associations
1701
1702 if not Is_List_Member (Assoc) then
1703 exit;
1704 else
1705 Next (Assoc);
1706 end if;
1707 end loop;
1708 end Check_Return_Construct_Accessibility;
1709
1710 -------------------------------
1711 -- Deepest_Type_Access_Level --
1712 -------------------------------
1713
1714 function Deepest_Type_Access_Level
1715 (Typ : Entity_Id;
1716 Allow_Alt_Model : Boolean := True) return Uint
1717 is
1718 begin
1719 if Ekind (Typ) = E_Anonymous_Access_Type
1720 and then not Is_Local_Anonymous_Access (Typ)
1721 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
1722 then
1723 -- No_Dynamic_Accessibility_Checks override for alternative
1724 -- accessibility model.
1725
1726 if Allow_Alt_Model
1727 and then No_Dynamic_Accessibility_Checks_Enabled (Typ)
1728 then
1729 return Type_Access_Level (Typ, Allow_Alt_Model);
1730 end if;
1731
1732 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
1733 -- access type.
1734
1735 return
1736 Scope_Depth (Enclosing_Dynamic_Scope
1737 (Defining_Identifier
1738 (Associated_Node_For_Itype (Typ))));
1739
1740 -- For generic formal type, return Int'Last (infinite).
1741 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
1742
1743 elsif Is_Generic_Type (Root_Type (Typ)) then
1744 return UI_From_Int (Int'Last);
1745
1746 else
1747 return Type_Access_Level (Typ, Allow_Alt_Model);
1748 end if;
1749 end Deepest_Type_Access_Level;
1750
1751 -----------------------------------
1752 -- Effective_Extra_Accessibility --
1753 -----------------------------------
1754
1755 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
1756 begin
1757 if Present (Renamed_Object (Id))
1758 and then Is_Entity_Name (Renamed_Object (Id))
1759 then
1760 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
1761 else
1762 return Extra_Accessibility (Id);
1763 end if;
1764 end Effective_Extra_Accessibility;
1765
1766 -------------------------------
1767 -- Get_Dynamic_Accessibility --
1768 -------------------------------
1769
1770 function Get_Dynamic_Accessibility (E : Entity_Id) return Entity_Id is
1771 begin
1772 -- When minimum accessibility is set for E then we utilize it - except
1773 -- in a few edge cases like the expansion of select statements where
1774 -- generated subprogram may attempt to unnecessarily use a minimum
1775 -- accessibility object declared outside of scope.
1776
1777 -- To avoid these situations where expansion may get complex we verify
1778 -- that the minimum accessibility object is within scope.
1779
1780 if Is_Formal (E)
1781 and then Present (Minimum_Accessibility (E))
1782 and then In_Open_Scopes (Scope (Minimum_Accessibility (E)))
1783 then
1784 return Minimum_Accessibility (E);
1785 end if;
1786
1787 return Extra_Accessibility (E);
1788 end Get_Dynamic_Accessibility;
1789
1790 -----------------------
1791 -- Has_Access_Values --
1792 -----------------------
1793
1794 function Has_Access_Values (T : Entity_Id) return Boolean
1795 is
1796 Typ : constant Entity_Id := Underlying_Type (T);
1797
1798 begin
1799 -- Case of a private type which is not completed yet. This can only
1800 -- happen in the case of a generic formal type appearing directly, or
1801 -- as a component of the type to which this function is being applied
1802 -- at the top level. Return False in this case, since we certainly do
1803 -- not know that the type contains access types.
1804
1805 if No (Typ) then
1806 return False;
1807
1808 elsif Is_Access_Type (Typ) then
1809 return True;
1810
1811 elsif Is_Array_Type (Typ) then
1812 return Has_Access_Values (Component_Type (Typ));
1813
1814 elsif Is_Record_Type (Typ) then
1815 declare
1816 Comp : Entity_Id;
1817
1818 begin
1819 -- Loop to check components
1820
1821 Comp := First_Component_Or_Discriminant (Typ);
1822 while Present (Comp) loop
1823
1824 -- Check for access component, tag field does not count, even
1825 -- though it is implemented internally using an access type.
1826
1827 if Has_Access_Values (Etype (Comp))
1828 and then Chars (Comp) /= Name_uTag
1829 then
1830 return True;
1831 end if;
1832
1833 Next_Component_Or_Discriminant (Comp);
1834 end loop;
1835 end;
1836
1837 return False;
1838
1839 else
1840 return False;
1841 end if;
1842 end Has_Access_Values;
1843
1844 ---------------------------------------
1845 -- Has_Anonymous_Access_Discriminant --
1846 ---------------------------------------
1847
1848 function Has_Anonymous_Access_Discriminant (Typ : Entity_Id) return Boolean
1849 is
1850 Disc : Node_Id;
1851
1852 begin
1853 if not Has_Discriminants (Typ) then
1854 return False;
1855 end if;
1856
1857 Disc := First_Discriminant (Typ);
1858 while Present (Disc) loop
1859 if Ekind (Etype (Disc)) = E_Anonymous_Access_Type then
1860 return True;
1861 end if;
1862
1863 Next_Discriminant (Disc);
1864 end loop;
1865
1866 return False;
1867 end Has_Anonymous_Access_Discriminant;
1868
1869 --------------------------------------------
1870 -- Has_Unconstrained_Access_Discriminants --
1871 --------------------------------------------
1872
1873 function Has_Unconstrained_Access_Discriminants
1874 (Subtyp : Entity_Id) return Boolean
1875 is
1876 Discr : Entity_Id;
1877
1878 begin
1879 if Has_Discriminants (Subtyp)
1880 and then not Is_Constrained (Subtyp)
1881 then
1882 Discr := First_Discriminant (Subtyp);
1883 while Present (Discr) loop
1884 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
1885 return True;
1886 end if;
1887
1888 Next_Discriminant (Discr);
1889 end loop;
1890 end if;
1891
1892 return False;
1893 end Has_Unconstrained_Access_Discriminants;
1894
1895 --------------------------------
1896 -- Is_Anonymous_Access_Actual --
1897 --------------------------------
1898
1899 function Is_Anonymous_Access_Actual (N : Node_Id) return Boolean is
1900 Par : Node_Id;
1901 begin
1902 if Ekind (Etype (N)) /= E_Anonymous_Access_Type then
1903 return False;
1904 end if;
1905
1906 Par := Parent (N);
1907 while Present (Par)
1908 and then Nkind (Par) in N_Case_Expression
1909 | N_If_Expression
1910 | N_Parameter_Association
1911 loop
1912 Par := Parent (Par);
1913 end loop;
1914 return Nkind (Par) in N_Subprogram_Call;
1915 end Is_Anonymous_Access_Actual;
1916
1917 --------------------------------------
1918 -- Is_Special_Aliased_Formal_Access --
1919 --------------------------------------
1920
1921 function Is_Special_Aliased_Formal_Access
1922 (Exp : Node_Id;
1923 In_Return_Context : Boolean := False) return Boolean
1924 is
1925 Scop : constant Entity_Id := Current_Subprogram;
1926 begin
1927 -- Verify the expression is an access reference to 'Access within a
1928 -- return statement as this is the only time an explicitly aliased
1929 -- formal has different semantics.
1930
1931 if Nkind (Exp) /= N_Attribute_Reference
1932 or else Get_Attribute_Id (Attribute_Name (Exp)) /= Attribute_Access
1933 or else not (In_Return_Value (Exp)
1934 or else In_Return_Context)
1935 or else not Needs_Result_Accessibility_Level (Scop)
1936 then
1937 return False;
1938 end if;
1939
1940 -- Check if the prefix of the reference is indeed an explicitly aliased
1941 -- formal parameter for the function Scop. Additionally, we must check
1942 -- that Scop returns an anonymous access type, otherwise the special
1943 -- rules dictating a need for a dynamic check are not in effect.
1944
1945 return Is_Entity_Name (Prefix (Exp))
1946 and then Is_Explicitly_Aliased (Entity (Prefix (Exp)));
1947 end Is_Special_Aliased_Formal_Access;
1948
1949 --------------------------------------
1950 -- Needs_Result_Accessibility_Level --
1951 --------------------------------------
1952
1953 function Needs_Result_Accessibility_Level
1954 (Func_Id : Entity_Id) return Boolean
1955 is
1956 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
1957
1958 function Has_Unconstrained_Access_Discriminant_Component
1959 (Comp_Typ : Entity_Id) return Boolean;
1960 -- Returns True if any component of the type has an unconstrained access
1961 -- discriminant.
1962
1963 -----------------------------------------------------
1964 -- Has_Unconstrained_Access_Discriminant_Component --
1965 -----------------------------------------------------
1966
1967 function Has_Unconstrained_Access_Discriminant_Component
1968 (Comp_Typ : Entity_Id) return Boolean
1969 is
1970 begin
1971 if not Is_Limited_Type (Comp_Typ) then
1972 return False;
1973
1974 -- Only limited types can have access discriminants with
1975 -- defaults.
1976
1977 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
1978 return True;
1979
1980 elsif Is_Array_Type (Comp_Typ) then
1981 return Has_Unconstrained_Access_Discriminant_Component
1982 (Underlying_Type (Component_Type (Comp_Typ)));
1983
1984 elsif Is_Record_Type (Comp_Typ) then
1985 declare
1986 Comp : Entity_Id;
1987
1988 begin
1989 Comp := First_Component (Comp_Typ);
1990 while Present (Comp) loop
1991 if Has_Unconstrained_Access_Discriminant_Component
1992 (Underlying_Type (Etype (Comp)))
1993 then
1994 return True;
1995 end if;
1996
1997 Next_Component (Comp);
1998 end loop;
1999 end;
2000 end if;
2001
2002 return False;
2003 end Has_Unconstrained_Access_Discriminant_Component;
2004
2005 Disable_Tagged_Cases : constant Boolean := True;
2006 -- Flag used to temporarily disable a "True" result for tagged types.
2007 -- See comments further below for details.
2008
2009 -- Start of processing for Needs_Result_Accessibility_Level
2010
2011 begin
2012 -- False if completion unavailable, which can happen when we are
2013 -- analyzing an abstract subprogram or if the subprogram has
2014 -- delayed freezing.
2015
2016 if No (Func_Typ) then
2017 return False;
2018
2019 -- False if not a function, also handle enum-lit renames case
2020
2021 elsif Func_Typ = Standard_Void_Type
2022 or else Is_Scalar_Type (Func_Typ)
2023 then
2024 return False;
2025
2026 -- Handle a corner case, a cross-dialect subp renaming. For example,
2027 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
2028 -- an Ada 2005 (or earlier) unit references predefined run-time units.
2029
2030 elsif Present (Alias (Func_Id)) then
2031
2032 -- Unimplemented: a cross-dialect subp renaming which does not set
2033 -- the Alias attribute (e.g., a rename of a dereference of an access
2034 -- to subprogram value). ???
2035
2036 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
2037
2038 -- Remaining cases require Ada 2012 mode, unless they are dispatching
2039 -- operations, since they may be overridden by Ada_2012 primitives.
2040
2041 elsif Ada_Version < Ada_2012
2042 and then not Is_Dispatching_Operation (Func_Id)
2043 then
2044 return False;
2045
2046 -- Handle the situation where a result is an anonymous access type
2047 -- RM 3.10.2 (10.3/3).
2048
2049 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type then
2050 return True;
2051
2052 -- In the case of, say, a null tagged record result type, the need for
2053 -- this extra parameter might not be obvious so this function returns
2054 -- True for all tagged types for compatibility reasons.
2055
2056 -- A function with, say, a tagged null controlling result type might
2057 -- be overridden by a primitive of an extension having an access
2058 -- discriminant and the overrider and overridden must have compatible
2059 -- calling conventions (including implicitly declared parameters).
2060
2061 -- Similarly, values of one access-to-subprogram type might designate
2062 -- both a primitive subprogram of a given type and a function which is,
2063 -- for example, not a primitive subprogram of any type. Again, this
2064 -- requires calling convention compatibility. It might be possible to
2065 -- solve these issues by introducing wrappers, but that is not the
2066 -- approach that was chosen.
2067
2068 -- Note: Despite the reasoning noted above, the extra accessibility
2069 -- parameter for tagged types is disabled for performance reasons.
2070
2071 elsif Is_Tagged_Type (Func_Typ) then
2072 return not Disable_Tagged_Cases;
2073
2074 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
2075 return True;
2076
2077 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
2078 return True;
2079
2080 -- False for all other cases
2081
2082 else
2083 return False;
2084 end if;
2085 end Needs_Result_Accessibility_Level;
2086
2087 ------------------------------------------
2088 -- Prefix_With_Safe_Accessibility_Level --
2089 ------------------------------------------
2090
2091 function Prefix_With_Safe_Accessibility_Level
2092 (N : Node_Id;
2093 Typ : Entity_Id) return Boolean
2094 is
2095 P : constant Node_Id := Prefix (N);
2096 Aname : constant Name_Id := Attribute_Name (N);
2097 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
2098 Btyp : constant Entity_Id := Base_Type (Typ);
2099
2100 function Safe_Value_Conversions return Boolean;
2101 -- Return False if the prefix has a value conversion of an array type
2102
2103 ----------------------------
2104 -- Safe_Value_Conversions --
2105 ----------------------------
2106
2107 function Safe_Value_Conversions return Boolean is
2108 PP : Node_Id := P;
2109
2110 begin
2111 loop
2112 if Nkind (PP) in N_Selected_Component | N_Indexed_Component then
2113 PP := Prefix (PP);
2114
2115 elsif Comes_From_Source (PP)
2116 and then Nkind (PP) in N_Type_Conversion
2117 | N_Unchecked_Type_Conversion
2118 and then Is_Array_Type (Etype (PP))
2119 then
2120 return False;
2121
2122 elsif Comes_From_Source (PP)
2123 and then Nkind (PP) = N_Qualified_Expression
2124 and then Is_Array_Type (Etype (PP))
2125 and then Nkind (Original_Node (Expression (PP))) in
2126 N_Aggregate | N_Extension_Aggregate
2127 then
2128 return False;
2129
2130 else
2131 exit;
2132 end if;
2133 end loop;
2134
2135 return True;
2136 end Safe_Value_Conversions;
2137
2138 -- Start of processing for Prefix_With_Safe_Accessibility_Level
2139
2140 begin
2141 -- No check required for unchecked and unrestricted access
2142
2143 if Attr_Id = Attribute_Unchecked_Access
2144 or else Attr_Id = Attribute_Unrestricted_Access
2145 then
2146 return True;
2147
2148 -- Check value conversions
2149
2150 elsif Ekind (Btyp) = E_General_Access_Type
2151 and then not Safe_Value_Conversions
2152 then
2153 return False;
2154 end if;
2155
2156 return True;
2157 end Prefix_With_Safe_Accessibility_Level;
2158
2159 -----------------------------
2160 -- Subprogram_Access_Level --
2161 -----------------------------
2162
2163 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
2164 begin
2165 if Present (Alias (Subp)) then
2166 return Subprogram_Access_Level (Alias (Subp));
2167 else
2168 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
2169 end if;
2170 end Subprogram_Access_Level;
2171
2172 --------------------------------
2173 -- Static_Accessibility_Level --
2174 --------------------------------
2175
2176 function Static_Accessibility_Level
2177 (Expr : Node_Id;
2178 Level : Static_Accessibility_Level_Kind;
2179 In_Return_Context : Boolean := False) return Uint
2180 is
2181 begin
2182 return Intval
2183 (Accessibility_Level (Expr, Level, In_Return_Context));
2184 end Static_Accessibility_Level;
2185
2186 -----------------------
2187 -- Type_Access_Level --
2188 -----------------------
2189
2190 function Type_Access_Level
2191 (Typ : Entity_Id;
2192 Allow_Alt_Model : Boolean := True;
2193 Assoc_Ent : Entity_Id := Empty) return Uint
2194 is
2195 Btyp : Entity_Id := Base_Type (Typ);
2196 Def_Ent : Entity_Id;
2197
2198 begin
2199 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
2200 -- simply use the level where the type is declared. This is true for
2201 -- stand-alone object declarations, and for anonymous access types
2202 -- associated with components the level is the same as that of the
2203 -- enclosing composite type. However, special treatment is needed for
2204 -- the cases of access parameters, return objects of an anonymous access
2205 -- type, and, in Ada 95, access discriminants of limited types.
2206
2207 if Is_Access_Type (Btyp) then
2208 if Ekind (Btyp) = E_Anonymous_Access_Type then
2209 -- No_Dynamic_Accessibility_Checks restriction override for
2210 -- alternative accessibility model.
2211
2212 if Allow_Alt_Model
2213 and then No_Dynamic_Accessibility_Checks_Enabled (Btyp)
2214 then
2215 -- In the -gnatd_b model, the level of an anonymous access
2216 -- type is always that of the designated type.
2217
2218 if Debug_Flag_Underscore_B then
2219 return Type_Access_Level
2220 (Designated_Type (Btyp), Allow_Alt_Model);
2221 end if;
2222
2223 -- When an anonymous access type's Assoc_Ent is specified,
2224 -- calculate the result based on the general accessibility
2225 -- level routine.
2226
2227 -- We would like to use Associated_Node_For_Itype here instead,
2228 -- but in some cases it is not fine grained enough ???
2229
2230 if Present (Assoc_Ent) then
2231 return Static_Accessibility_Level
2232 (Assoc_Ent, Object_Decl_Level);
2233 end if;
2234
2235 -- Otherwise take the context of the anonymous access type into
2236 -- account.
2237
2238 -- Obtain the defining entity for the internally generated
2239 -- anonymous access type.
2240
2241 Def_Ent := Defining_Entity_Or_Empty
2242 (Associated_Node_For_Itype (Typ));
2243
2244 if Present (Def_Ent) then
2245 -- When the defining entity is a subprogram then we know the
2246 -- anonymous access type Typ has been generated to either
2247 -- describe an anonymous access type formal or an anonymous
2248 -- access result type.
2249
2250 -- Since we are only interested in the formal case, avoid
2251 -- the anonymous access result type.
2252
2253 if Is_Subprogram (Def_Ent)
2254 and then not (Ekind (Def_Ent) = E_Function
2255 and then Etype (Def_Ent) = Typ)
2256 then
2257 -- When the type comes from an anonymous access
2258 -- parameter, the level is that of the subprogram
2259 -- declaration.
2260
2261 return Scope_Depth (Def_Ent);
2262
2263 -- When the type is an access discriminant, the level is
2264 -- that of the type.
2265
2266 elsif Ekind (Def_Ent) = E_Discriminant then
2267 return Scope_Depth
2268 (if Present (Full_View (Scope (Def_Ent))) then
2269 Full_View (Scope (Def_Ent))
2270 else
2271 Scope (Def_Ent));
2272 end if;
2273 end if;
2274
2275 -- If the type is a nonlocal anonymous access type (such as for
2276 -- an access parameter) we treat it as being declared at the
2277 -- library level to ensure that names such as X.all'access don't
2278 -- fail static accessibility checks.
2279
2280 elsif not Is_Local_Anonymous_Access (Typ) then
2281 return Scope_Depth (Standard_Standard);
2282
2283 -- If this is a return object, the accessibility level is that of
2284 -- the result subtype of the enclosing function. The test here is
2285 -- little complicated, because we have to account for extended
2286 -- return statements that have been rewritten as blocks, in which
2287 -- case we have to find and the Is_Return_Object attribute of the
2288 -- itype's associated object. It would be nice to find a way to
2289 -- simplify this test, but it doesn't seem worthwhile to add a new
2290 -- flag just for purposes of this test. ???
2291
2292 elsif Ekind (Scope (Btyp)) = E_Return_Statement
2293 or else
2294 (Is_Itype (Btyp)
2295 and then Nkind (Associated_Node_For_Itype (Btyp)) =
2296 N_Object_Declaration
2297 and then Is_Return_Object
2298 (Defining_Identifier
2299 (Associated_Node_For_Itype (Btyp))))
2300 then
2301 declare
2302 Scop : Entity_Id;
2303
2304 begin
2305 Scop := Scope (Scope (Btyp));
2306 while Present (Scop) loop
2307 exit when Ekind (Scop) = E_Function;
2308 Scop := Scope (Scop);
2309 end loop;
2310
2311 -- Treat the return object's type as having the level of the
2312 -- function's result subtype (as per RM05-6.5(5.3/2)).
2313
2314 return Type_Access_Level (Etype (Scop), Allow_Alt_Model);
2315 end;
2316 end if;
2317 end if;
2318
2319 Btyp := Root_Type (Btyp);
2320
2321 -- The accessibility level of anonymous access types associated with
2322 -- discriminants is that of the current instance of the type, and
2323 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
2324
2325 -- AI-402: access discriminants have accessibility based on the
2326 -- object rather than the type in Ada 2005, so the above paragraph
2327 -- doesn't apply.
2328
2329 -- ??? Needs completion with rules from AI-416
2330
2331 if Ada_Version <= Ada_95
2332 and then Ekind (Typ) = E_Anonymous_Access_Type
2333 and then Present (Associated_Node_For_Itype (Typ))
2334 and then Nkind (Associated_Node_For_Itype (Typ)) =
2335 N_Discriminant_Specification
2336 then
2337 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
2338 end if;
2339 end if;
2340
2341 -- Return library level for a generic formal type. This is done because
2342 -- RM(10.3.2) says that "The statically deeper relationship does not
2343 -- apply to ... a descendant of a generic formal type". Rather than
2344 -- checking at each point where a static accessibility check is
2345 -- performed to see if we are dealing with a formal type, this rule is
2346 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
2347 -- return extreme values for a formal type; Deepest_Type_Access_Level
2348 -- returns Int'Last. By calling the appropriate function from among the
2349 -- two, we ensure that the static accessibility check will pass if we
2350 -- happen to run into a formal type. More specifically, we should call
2351 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
2352 -- call occurs as part of a static accessibility check and the error
2353 -- case is the case where the type's level is too shallow (as opposed
2354 -- to too deep).
2355
2356 if Is_Generic_Type (Root_Type (Btyp)) then
2357 return Scope_Depth (Standard_Standard);
2358 end if;
2359
2360 -- It is possible that the current scope is an aliased subprogram -
2361 -- this can happen when an abstract primitive from a root type is not
2362 -- not visible.
2363
2364 if Is_Subprogram (Enclosing_Dynamic_Scope (Btyp))
2365 and then Present (Alias (Enclosing_Dynamic_Scope (Btyp)))
2366 then
2367 return Scope_Depth (Ultimate_Alias (Enclosing_Dynamic_Scope (Btyp)));
2368
2369 -- Otherwise, simply use the enclosing dynamic scope
2370
2371 else
2372 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
2373 end if;
2374 end Type_Access_Level;
2375
2376 end Accessibility;