Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / sem_aux.adb
blob658110f98d2a7112b0dd7e94a8e683a381a2251f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ A U X --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Einfo; use Einfo;
28 with Einfo.Entities; use Einfo.Entities;
29 with Einfo.Utils; use Einfo.Utils;
30 with Nlists; use Nlists;
31 with Sinfo; use Sinfo;
32 with Sinfo.Nodes; use Sinfo.Nodes;
33 with Sinfo.Utils; use Sinfo.Utils;
34 with Snames; use Snames;
35 with Stand; use Stand;
36 with Uintp; use Uintp;
38 package body Sem_Aux is
40 ----------------------
41 -- Ancestor_Subtype --
42 ----------------------
44 function Ancestor_Subtype (Typ : Entity_Id) return Entity_Id is
45 begin
46 -- If this is first subtype, or is a base type, then there is no
47 -- ancestor subtype, so we return Empty to indicate this fact.
49 if Is_First_Subtype (Typ) or else Is_Base_Type (Typ) then
50 return Empty;
51 end if;
53 declare
54 D : constant Node_Id := Declaration_Node (Typ);
56 begin
57 -- If we have a subtype declaration, get the ancestor subtype
59 if Nkind (D) = N_Subtype_Declaration then
60 if Nkind (Subtype_Indication (D)) = N_Subtype_Indication then
61 return Entity (Subtype_Mark (Subtype_Indication (D)));
62 else
63 return Entity (Subtype_Indication (D));
64 end if;
66 -- If not, then no subtype indication is available
68 else
69 return Empty;
70 end if;
71 end;
72 end Ancestor_Subtype;
74 --------------------
75 -- Available_View --
76 --------------------
78 function Available_View (Ent : Entity_Id) return Entity_Id is
79 begin
80 -- Obtain the non-limited view (if available)
82 if Has_Non_Limited_View (Ent) then
83 return Get_Full_View (Non_Limited_View (Ent));
85 -- In all other cases, return entity unchanged
87 else
88 return Ent;
89 end if;
90 end Available_View;
92 --------------------
93 -- Constant_Value --
94 --------------------
96 function Constant_Value (Ent : Entity_Id) return Node_Id is
97 D : constant Node_Id := Declaration_Node (Ent);
98 Full_D : Node_Id;
100 begin
101 -- If we have no declaration node, then return no constant value. Not
102 -- clear how this can happen, but it does sometimes and this is the
103 -- safest approach.
105 if No (D) then
106 return Empty;
108 -- Normal case where a declaration node is present
110 elsif Nkind (D) = N_Object_Renaming_Declaration then
111 return Renamed_Object (Ent);
113 -- If this is a component declaration whose entity is a constant, it is
114 -- a prival within a protected function (and so has no constant value).
116 elsif Nkind (D) = N_Component_Declaration then
117 return Empty;
119 -- If there is an expression, return it
121 elsif Present (Expression (D)) then
122 return Expression (D);
124 -- For a constant, see if we have a full view
126 elsif Ekind (Ent) = E_Constant
127 and then Present (Full_View (Ent))
128 then
129 Full_D := Parent (Full_View (Ent));
131 -- The full view may have been rewritten as an object renaming
133 if Nkind (Full_D) = N_Object_Renaming_Declaration then
134 return Name (Full_D);
135 else
136 return Expression (Full_D);
137 end if;
139 -- Otherwise we have no expression to return
141 else
142 return Empty;
143 end if;
144 end Constant_Value;
146 ---------------------------------
147 -- Corresponding_Unsigned_Type --
148 ---------------------------------
150 function Corresponding_Unsigned_Type (Typ : Entity_Id) return Entity_Id is
151 pragma Assert (Is_Signed_Integer_Type (Typ));
152 Siz : constant Uint := Esize (Base_Type (Typ));
153 begin
154 if Siz = Esize (Standard_Short_Short_Integer) then
155 return Standard_Short_Short_Unsigned;
156 elsif Siz = Esize (Standard_Short_Integer) then
157 return Standard_Short_Unsigned;
158 elsif Siz = Esize (Standard_Unsigned) then
159 return Standard_Unsigned;
160 elsif Siz = Esize (Standard_Long_Integer) then
161 return Standard_Long_Unsigned;
162 elsif Siz = Esize (Standard_Long_Long_Integer) then
163 return Standard_Long_Long_Unsigned;
164 elsif Siz = Esize (Standard_Long_Long_Long_Integer) then
165 return Standard_Long_Long_Long_Unsigned;
166 else
167 raise Program_Error;
168 end if;
169 end Corresponding_Unsigned_Type;
171 -----------------------------
172 -- Enclosing_Dynamic_Scope --
173 -----------------------------
175 function Enclosing_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
176 S : Entity_Id;
178 begin
179 -- The following test is an error defense against some syntax errors
180 -- that can leave scopes very messed up.
182 if Ent = Standard_Standard then
183 return Ent;
184 end if;
186 -- Normal case, search enclosing scopes
188 -- Note: the test for Present (S) should not be required, it defends
189 -- against an ill-formed tree.
191 S := Scope (Ent);
192 loop
193 -- If we somehow got an empty value for Scope, the tree must be
194 -- malformed. Rather than blow up we return Standard in this case.
196 if No (S) then
197 return Standard_Standard;
199 -- Quit if we get to standard or a dynamic scope. We must also
200 -- handle enclosing scopes that have a full view; required to
201 -- locate enclosing scopes that are synchronized private types
202 -- whose full view is a task type.
204 elsif S = Standard_Standard
205 or else Is_Dynamic_Scope (S)
206 or else (Is_Private_Type (S)
207 and then Present (Full_View (S))
208 and then Is_Dynamic_Scope (Full_View (S)))
209 then
210 return S;
212 -- Otherwise keep climbing
214 else
215 S := Scope (S);
216 end if;
217 end loop;
218 end Enclosing_Dynamic_Scope;
220 ------------------------
221 -- First_Discriminant --
222 ------------------------
224 function First_Discriminant (Typ : Entity_Id) return Entity_Id is
225 Ent : Entity_Id;
227 begin
228 pragma Assert
229 (Has_Discriminants (Typ) or else Has_Unknown_Discriminants (Typ));
231 Ent := First_Entity (Typ);
233 -- The discriminants are not necessarily contiguous, because access
234 -- discriminants will generate itypes. They are not the first entities
235 -- either because the tag must be ahead of them.
237 if Chars (Ent) = Name_uTag then
238 Next_Entity (Ent);
239 end if;
241 -- Skip all hidden stored discriminants if any
243 while Present (Ent) loop
244 exit when Ekind (Ent) = E_Discriminant
245 and then not Is_Completely_Hidden (Ent);
247 Next_Entity (Ent);
248 end loop;
250 -- Call may be on a private type with unknown discriminants, in which
251 -- case Ent is Empty, and as per the spec, we return Empty in this case.
253 -- Historical note: The assertion in previous versions that Ent is a
254 -- discriminant was overly cautious and prevented convenient application
255 -- of this function in the gnatprove context.
257 return Ent;
258 end First_Discriminant;
260 -------------------------------
261 -- First_Stored_Discriminant --
262 -------------------------------
264 function First_Stored_Discriminant (Typ : Entity_Id) return Entity_Id is
265 Ent : Entity_Id;
267 function Has_Completely_Hidden_Discriminant
268 (Typ : Entity_Id) return Boolean;
269 -- Scans the Discriminants to see whether any are Completely_Hidden
270 -- (the mechanism for describing non-specified stored discriminants)
271 -- Note that the entity list for the type may contain anonymous access
272 -- types created by expressions that constrain access discriminants.
274 ----------------------------------------
275 -- Has_Completely_Hidden_Discriminant --
276 ----------------------------------------
278 function Has_Completely_Hidden_Discriminant
279 (Typ : Entity_Id) return Boolean
281 Ent : Entity_Id;
283 begin
284 pragma Assert (Ekind (Typ) = E_Discriminant);
286 Ent := Typ;
287 while Present (Ent) loop
289 -- Skip anonymous types that may be created by expressions
290 -- used as discriminant constraints on inherited discriminants.
292 if Is_Itype (Ent) then
293 null;
295 elsif Ekind (Ent) = E_Discriminant
296 and then Is_Completely_Hidden (Ent)
297 then
298 return True;
299 end if;
301 Next_Entity (Ent);
302 end loop;
304 return False;
305 end Has_Completely_Hidden_Discriminant;
307 -- Start of processing for First_Stored_Discriminant
309 begin
310 pragma Assert
311 (Has_Discriminants (Typ)
312 or else Has_Unknown_Discriminants (Typ));
314 Ent := First_Entity (Typ);
316 if Chars (Ent) = Name_uTag then
317 Next_Entity (Ent);
318 end if;
320 if Has_Completely_Hidden_Discriminant (Ent) then
321 while Present (Ent) loop
322 exit when Ekind (Ent) = E_Discriminant
323 and then Is_Completely_Hidden (Ent);
324 Next_Entity (Ent);
325 end loop;
326 end if;
328 pragma Assert (Ekind (Ent) = E_Discriminant);
330 return Ent;
331 end First_Stored_Discriminant;
333 -------------------
334 -- First_Subtype --
335 -------------------
337 function First_Subtype (Typ : Entity_Id) return Entity_Id is
338 B : constant Entity_Id := Base_Type (Typ);
339 F : Node_Id := Freeze_Node (B);
340 Ent : Entity_Id;
342 begin
343 -- The freeze node of a ghost type might have been rewritten in a null
344 -- statement by the time gigi calls First_Subtype on the corresponding
345 -- type.
347 if Nkind (F) = N_Null_Statement then
348 F := Original_Node (F);
349 end if;
351 -- If the base type has no freeze node, it is a type in Standard, and
352 -- always acts as its own first subtype, except where it is one of the
353 -- predefined integer types. If the type is formal, it is also a first
354 -- subtype, and its base type has no freeze node. On the other hand, a
355 -- subtype of a generic formal is not its own first subtype. Its base
356 -- type, if anonymous, is attached to the formal type declaration from
357 -- which the first subtype is obtained.
359 if No (F) then
360 if B = Base_Type (Standard_Integer) then
361 return Standard_Integer;
363 elsif B = Base_Type (Standard_Long_Integer) then
364 return Standard_Long_Integer;
366 elsif B = Base_Type (Standard_Short_Short_Integer) then
367 return Standard_Short_Short_Integer;
369 elsif B = Base_Type (Standard_Short_Integer) then
370 return Standard_Short_Integer;
372 elsif B = Base_Type (Standard_Long_Long_Integer) then
373 return Standard_Long_Long_Integer;
375 elsif B = Base_Type (Standard_Long_Long_Long_Integer) then
376 return Standard_Long_Long_Long_Integer;
378 elsif Is_Generic_Type (Typ) then
379 if Present (Parent (B)) then
380 return Defining_Identifier (Parent (B));
381 else
382 return Defining_Identifier (Associated_Node_For_Itype (B));
383 end if;
385 else
386 return B;
387 end if;
389 -- Otherwise we check the freeze node, if it has a First_Subtype_Link
390 -- then we use that link, otherwise (happens with some Itypes), we use
391 -- the base type itself.
393 else
394 Ent := First_Subtype_Link (F);
396 if Present (Ent) then
397 return Ent;
398 else
399 return B;
400 end if;
401 end if;
402 end First_Subtype;
404 -------------------------
405 -- First_Tag_Component --
406 -------------------------
408 function First_Tag_Component (Typ : Entity_Id) return Entity_Id is
409 Comp : Entity_Id;
410 Ctyp : Entity_Id;
412 begin
413 pragma Assert (Is_Tagged_Type (Typ)
414 or else Is_Class_Wide_Equivalent_Type (Typ));
416 Ctyp := Typ;
418 if Is_Class_Wide_Type (Ctyp) then
419 Ctyp := Root_Type (Ctyp);
420 end if;
422 if Is_Private_Type (Ctyp) then
423 Ctyp := Underlying_Type (Ctyp);
425 -- If the underlying type is missing then the source program has
426 -- errors and there is nothing else to do (the full-type declaration
427 -- associated with the private type declaration is missing).
429 if No (Ctyp) then
430 return Empty;
431 end if;
432 end if;
434 Comp := First_Entity (Ctyp);
435 while Present (Comp) loop
436 if Is_Tag (Comp) then
437 return Comp;
438 end if;
440 Next_Entity (Comp);
441 end loop;
443 -- No tag component found
445 return Empty;
446 end First_Tag_Component;
448 -----------------------
449 -- Get_Called_Entity --
450 -----------------------
452 function Get_Called_Entity (Call : Node_Id) return Entity_Id is
453 Nam : constant Node_Id := Name (Call);
454 Id : Entity_Id;
456 begin
457 if Nkind (Nam) = N_Explicit_Dereference then
458 Id := Etype (Nam);
459 pragma Assert (Ekind (Id) = E_Subprogram_Type);
461 elsif Nkind (Nam) = N_Selected_Component then
462 Id := Entity (Selector_Name (Nam));
464 elsif Nkind (Nam) = N_Indexed_Component then
465 Id := Entity (Selector_Name (Prefix (Nam)));
467 else
468 Id := Entity (Nam);
469 end if;
471 return Id;
472 end Get_Called_Entity;
474 ------------------
475 -- Get_Rep_Item --
476 ------------------
478 function Get_Rep_Item
479 (E : Entity_Id;
480 Nam : Name_Id;
481 Check_Parents : Boolean := True) return Node_Id
483 N : Node_Id;
485 begin
486 N := First_Rep_Item (E);
487 while Present (N) loop
489 -- Only one of Priority / Interrupt_Priority can be specified, so
490 -- return whichever one is present to catch illegal duplication.
492 if Nkind (N) = N_Pragma
493 and then
494 (Pragma_Name_Unmapped (N) = Nam
495 or else (Nam = Name_Priority
496 and then Pragma_Name (N) =
497 Name_Interrupt_Priority)
498 or else (Nam = Name_Interrupt_Priority
499 and then Pragma_Name (N) = Name_Priority))
500 then
501 if Check_Parents then
502 return N;
504 -- If Check_Parents is False, return N if the pragma doesn't
505 -- appear in the Rep_Item chain of the parent.
507 else
508 declare
509 Par : constant Entity_Id := Nearest_Ancestor (E);
510 -- This node represents the parent type of type E (if any)
512 begin
513 if No (Par) then
514 return N;
516 elsif not Present_In_Rep_Item (Par, N) then
517 return N;
518 end if;
519 end;
520 end if;
522 elsif Nkind (N) = N_Attribute_Definition_Clause
523 and then
524 (Chars (N) = Nam
525 or else (Nam = Name_Priority
526 and then Chars (N) = Name_Interrupt_Priority))
527 then
528 if Check_Parents or else Entity (N) = E then
529 return N;
530 end if;
532 elsif Nkind (N) = N_Aspect_Specification
533 and then
534 (Chars (Identifier (N)) = Nam
535 or else
536 (Nam = Name_Priority
537 and then Chars (Identifier (N)) = Name_Interrupt_Priority))
538 then
539 if Check_Parents then
540 return N;
542 elsif Entity (N) = E then
543 return N;
544 end if;
546 -- A Ghost-related aspect, if disabled, may have been replaced by a
547 -- null statement.
549 elsif Nkind (N) = N_Null_Statement then
550 N := Original_Node (N);
551 end if;
553 Next_Rep_Item (N);
554 end loop;
556 return Empty;
557 end Get_Rep_Item;
559 function Get_Rep_Item
560 (E : Entity_Id;
561 Nam1 : Name_Id;
562 Nam2 : Name_Id;
563 Check_Parents : Boolean := True) return Node_Id
565 Nam1_Item : constant Node_Id := Get_Rep_Item (E, Nam1, Check_Parents);
566 Nam2_Item : constant Node_Id := Get_Rep_Item (E, Nam2, Check_Parents);
568 N : Node_Id;
570 begin
571 -- Check both Nam1_Item and Nam2_Item are present
573 if No (Nam1_Item) then
574 return Nam2_Item;
575 elsif No (Nam2_Item) then
576 return Nam1_Item;
577 end if;
579 -- Return the first node encountered in the list
581 N := First_Rep_Item (E);
582 while Present (N) loop
583 if N = Nam1_Item or else N = Nam2_Item then
584 return N;
585 end if;
587 Next_Rep_Item (N);
588 end loop;
590 return Empty;
591 end Get_Rep_Item;
593 --------------------
594 -- Get_Rep_Pragma --
595 --------------------
597 function Get_Rep_Pragma
598 (E : Entity_Id;
599 Nam : Name_Id;
600 Check_Parents : Boolean := True) return Node_Id
602 N : constant Node_Id := Get_Rep_Item (E, Nam, Check_Parents);
604 begin
605 if Present (N) and then Nkind (N) = N_Pragma then
606 return N;
607 end if;
609 return Empty;
610 end Get_Rep_Pragma;
612 function Get_Rep_Pragma
613 (E : Entity_Id;
614 Nam1 : Name_Id;
615 Nam2 : Name_Id;
616 Check_Parents : Boolean := True) return Node_Id
618 Nam1_Item : constant Node_Id := Get_Rep_Pragma (E, Nam1, Check_Parents);
619 Nam2_Item : constant Node_Id := Get_Rep_Pragma (E, Nam2, Check_Parents);
621 N : Node_Id;
623 begin
624 -- Check both Nam1_Item and Nam2_Item are present
626 if No (Nam1_Item) then
627 return Nam2_Item;
628 elsif No (Nam2_Item) then
629 return Nam1_Item;
630 end if;
632 -- Return the first node encountered in the list
634 N := First_Rep_Item (E);
635 while Present (N) loop
636 if N = Nam1_Item or else N = Nam2_Item then
637 return N;
638 end if;
640 Next_Rep_Item (N);
641 end loop;
643 return Empty;
644 end Get_Rep_Pragma;
646 ---------------------------------
647 -- Has_External_Tag_Rep_Clause --
648 ---------------------------------
650 function Has_External_Tag_Rep_Clause (T : Entity_Id) return Boolean is
651 begin
652 pragma Assert (Is_Tagged_Type (T));
653 return Has_Rep_Item (T, Name_External_Tag, Check_Parents => False);
654 end Has_External_Tag_Rep_Clause;
656 ------------------
657 -- Has_Rep_Item --
658 ------------------
660 function Has_Rep_Item
661 (E : Entity_Id;
662 Nam : Name_Id;
663 Check_Parents : Boolean := True) return Boolean
665 begin
666 return Present (Get_Rep_Item (E, Nam, Check_Parents));
667 end Has_Rep_Item;
669 function Has_Rep_Item
670 (E : Entity_Id;
671 Nam1 : Name_Id;
672 Nam2 : Name_Id;
673 Check_Parents : Boolean := True) return Boolean
675 begin
676 return Present (Get_Rep_Item (E, Nam1, Nam2, Check_Parents));
677 end Has_Rep_Item;
679 --------------------
680 -- Has_Rep_Pragma --
681 --------------------
683 function Has_Rep_Pragma
684 (E : Entity_Id;
685 Nam : Name_Id;
686 Check_Parents : Boolean := True) return Boolean
688 begin
689 return Present (Get_Rep_Pragma (E, Nam, Check_Parents));
690 end Has_Rep_Pragma;
692 function Has_Rep_Pragma
693 (E : Entity_Id;
694 Nam1 : Name_Id;
695 Nam2 : Name_Id;
696 Check_Parents : Boolean := True) return Boolean
698 begin
699 return Present (Get_Rep_Pragma (E, Nam1, Nam2, Check_Parents));
700 end Has_Rep_Pragma;
702 --------------------------------
703 -- Has_Unconstrained_Elements --
704 --------------------------------
706 function Has_Unconstrained_Elements (T : Entity_Id) return Boolean is
707 U_T : constant Entity_Id := Underlying_Type (T);
708 begin
709 if No (U_T) then
710 return False;
711 elsif Is_Record_Type (U_T) then
712 return Has_Discriminants (U_T) and then not Is_Constrained (U_T);
713 elsif Is_Array_Type (U_T) then
714 return Has_Unconstrained_Elements (Component_Type (U_T));
715 else
716 return False;
717 end if;
718 end Has_Unconstrained_Elements;
720 ----------------------
721 -- Has_Variant_Part --
722 ----------------------
724 function Has_Variant_Part (Typ : Entity_Id) return Boolean is
725 FSTyp : Entity_Id;
726 Decl : Node_Id;
727 TDef : Node_Id;
728 CList : Node_Id;
730 begin
731 if not Is_Type (Typ) then
732 return False;
733 end if;
735 FSTyp := First_Subtype (Typ);
737 if not Has_Discriminants (FSTyp) then
738 return False;
739 end if;
741 -- Proceed with cautious checks here, return False if tree is not
742 -- as expected (may be caused by prior errors).
744 Decl := Declaration_Node (FSTyp);
746 if Nkind (Decl) /= N_Full_Type_Declaration then
747 return False;
748 end if;
750 TDef := Type_Definition (Decl);
752 if Nkind (TDef) /= N_Record_Definition then
753 return False;
754 end if;
756 CList := Component_List (TDef);
758 if Nkind (CList) /= N_Component_List then
759 return False;
760 else
761 return Present (Variant_Part (CList));
762 end if;
763 end Has_Variant_Part;
765 ---------------------
766 -- In_Generic_Body --
767 ---------------------
769 function In_Generic_Body (Id : Entity_Id) return Boolean is
770 S : Entity_Id;
772 begin
773 -- Climb scopes looking for generic body
775 S := Id;
776 while Present (S) and then S /= Standard_Standard loop
778 -- Generic package body
780 if Ekind (S) = E_Generic_Package
781 and then In_Package_Body (S)
782 then
783 return True;
785 -- Generic subprogram body
787 elsif Is_Subprogram (S)
788 and then Nkind (Unit_Declaration_Node (S)) =
789 N_Generic_Subprogram_Declaration
790 then
791 return True;
792 end if;
794 S := Scope (S);
795 end loop;
797 -- False if top of scope stack without finding a generic body
799 return False;
800 end In_Generic_Body;
802 -------------------------------
803 -- Initialization_Suppressed --
804 -------------------------------
806 function Initialization_Suppressed (Typ : Entity_Id) return Boolean is
807 begin
808 return Suppress_Initialization (Typ)
809 or else Suppress_Initialization (Base_Type (Typ));
810 end Initialization_Suppressed;
812 ----------------
813 -- Initialize --
814 ----------------
816 procedure Initialize is
817 begin
818 Obsolescent_Warnings.Init;
819 end Initialize;
821 -------------
822 -- Is_Body --
823 -------------
825 function Is_Body (N : Node_Id) return Boolean is
826 begin
827 return Nkind (N) in
828 N_Body_Stub | N_Entry_Body | N_Package_Body | N_Protected_Body |
829 N_Subprogram_Body | N_Task_Body;
830 end Is_Body;
832 ---------------------
833 -- Is_By_Copy_Type --
834 ---------------------
836 function Is_By_Copy_Type (Ent : Entity_Id) return Boolean is
837 begin
838 -- If Id is a private type whose full declaration has not been seen,
839 -- we assume for now that it is not a By_Copy type. Clearly this
840 -- attribute should not be used before the type is frozen, but it is
841 -- needed to build the associated record of a protected type. Another
842 -- place where some lookahead for a full view is needed ???
844 return
845 Is_Elementary_Type (Ent)
846 or else (Is_Private_Type (Ent)
847 and then Present (Underlying_Type (Ent))
848 and then Is_Elementary_Type (Underlying_Type (Ent)));
849 end Is_By_Copy_Type;
851 --------------------------
852 -- Is_By_Reference_Type --
853 --------------------------
855 function Is_By_Reference_Type (Ent : Entity_Id) return Boolean is
856 Btype : constant Entity_Id := Base_Type (Ent);
858 begin
859 if Is_Private_Type (Btype) then
860 declare
861 Utyp : constant Entity_Id := Underlying_Type (Btype);
862 begin
863 if No (Utyp) then
864 return False;
865 else
866 return Is_By_Reference_Type (Utyp);
867 end if;
868 end;
870 elsif Is_Incomplete_Type (Btype) then
871 declare
872 Ftyp : constant Entity_Id := Full_View (Btype);
873 begin
874 -- Return true for a tagged incomplete type built as a shadow
875 -- entity in Build_Limited_Views. It can appear in the profile
876 -- of a thunk and the back end needs to know how it is passed.
878 if No (Ftyp) then
879 return Is_Tagged_Type (Btype);
880 else
881 return Is_By_Reference_Type (Ftyp);
882 end if;
883 end;
885 elsif Is_Concurrent_Type (Btype) then
886 return True;
888 elsif Is_Record_Type (Btype) then
889 if Is_Limited_Record (Btype)
890 or else Is_Tagged_Type (Btype)
891 or else Is_Volatile (Btype)
892 then
893 return True;
895 else
896 declare
897 C : Entity_Id;
899 begin
900 C := First_Component (Btype);
901 while Present (C) loop
903 -- For each component, test if its type is a by reference
904 -- type and if its type is volatile. Also test the component
905 -- itself for being volatile. This happens for example when
906 -- a Volatile aspect is added to a component.
908 if Is_By_Reference_Type (Etype (C))
909 or else Is_Volatile (Etype (C))
910 or else Is_Volatile (C)
911 then
912 return True;
913 end if;
915 Next_Component (C);
916 end loop;
917 end;
919 return False;
920 end if;
922 elsif Is_Array_Type (Btype) then
923 return
924 Is_Volatile (Btype)
925 or else Is_By_Reference_Type (Component_Type (Btype))
926 or else Is_Volatile (Component_Type (Btype))
927 or else Has_Volatile_Components (Btype);
929 else
930 return False;
931 end if;
932 end Is_By_Reference_Type;
934 -------------------------
935 -- Is_Definite_Subtype --
936 -------------------------
938 function Is_Definite_Subtype (T : Entity_Id) return Boolean is
939 pragma Assert (Is_Type (T));
940 K : constant Entity_Kind := Ekind (T);
942 begin
943 if Is_Constrained (T) then
944 return True;
946 elsif K in Array_Kind
947 or else K in Class_Wide_Kind
948 or else Has_Unknown_Discriminants (T)
949 then
950 return False;
952 -- Known discriminants: definite if there are default values. Note that
953 -- if any discriminant has a default, they all do.
955 elsif Has_Discriminants (T) then
956 return Present (Discriminant_Default_Value (First_Discriminant (T)));
958 else
959 return True;
960 end if;
961 end Is_Definite_Subtype;
963 ---------------------
964 -- Is_Derived_Type --
965 ---------------------
967 function Is_Derived_Type (Ent : Entity_Id) return B is
968 Par : Node_Id;
970 begin
971 if Is_Type (Ent)
972 and then Base_Type (Ent) /= Root_Type (Ent)
973 and then not Is_Class_Wide_Type (Ent)
975 -- An access_to_subprogram whose result type is a limited view can
976 -- appear in a return statement, without the full view of the result
977 -- type being available. Do not interpret this as a derived type.
979 and then Ekind (Ent) /= E_Subprogram_Type
980 then
981 if not Is_Numeric_Type (Root_Type (Ent)) then
982 return True;
984 else
985 Par := Parent (First_Subtype (Ent));
987 return Present (Par)
988 and then Nkind (Par) = N_Full_Type_Declaration
989 and then Nkind (Type_Definition (Par)) =
990 N_Derived_Type_Definition;
991 end if;
993 else
994 return False;
995 end if;
996 end Is_Derived_Type;
998 -----------------------
999 -- Is_Generic_Formal --
1000 -----------------------
1002 function Is_Generic_Formal (E : Entity_Id) return Boolean is
1003 Kind : Node_Kind;
1005 begin
1006 if No (E) then
1007 return False;
1008 else
1009 -- Formal derived types are rewritten as private extensions, so
1010 -- examine original node.
1012 Kind := Nkind (Original_Node (Parent (E)));
1014 return
1015 Kind in N_Formal_Object_Declaration | N_Formal_Type_Declaration
1016 or else Is_Formal_Subprogram (E)
1017 or else
1018 (Ekind (E) = E_Package
1019 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
1020 N_Formal_Package_Declaration);
1021 end if;
1022 end Is_Generic_Formal;
1024 -------------------------------
1025 -- Is_Immutably_Limited_Type --
1026 -------------------------------
1028 function Is_Immutably_Limited_Type (Ent : Entity_Id) return Boolean is
1029 Btype : constant Entity_Id := Available_View (Base_Type (Ent));
1031 begin
1032 if Is_Limited_Record (Btype) then
1033 return True;
1035 elsif Ekind (Btype) = E_Limited_Private_Type
1036 and then Nkind (Parent (Btype)) = N_Formal_Type_Declaration
1037 then
1038 return not In_Package_Body (Scope ((Btype)));
1040 elsif Is_Private_Type (Btype) then
1042 -- AI05-0063: A type derived from a limited private formal type is
1043 -- not immutably limited in a generic body.
1045 if Is_Derived_Type (Btype)
1046 and then Is_Generic_Type (Etype (Btype))
1047 then
1048 if not Is_Limited_Type (Etype (Btype)) then
1049 return False;
1051 -- A descendant of a limited formal type is not immutably limited
1052 -- in the generic body, or in the body of a generic child.
1054 elsif Ekind (Scope (Etype (Btype))) = E_Generic_Package then
1055 return not In_Package_Body (Scope (Btype));
1057 else
1058 return False;
1059 end if;
1061 else
1062 return False;
1063 end if;
1065 elsif Is_Concurrent_Type (Btype) then
1066 return True;
1068 else
1069 return False;
1070 end if;
1071 end Is_Immutably_Limited_Type;
1073 ---------------------
1074 -- Is_Limited_Type --
1075 ---------------------
1077 function Is_Limited_Type (Ent : Entity_Id) return Boolean is
1078 Btype : Entity_Id;
1079 Rtype : Entity_Id;
1081 begin
1082 if not Is_Type (Ent) then
1083 return False;
1084 end if;
1086 Btype := Base_Type (Ent);
1087 Rtype := Root_Type (Btype);
1089 if Ekind (Btype) = E_Limited_Private_Type
1090 or else Is_Limited_Composite (Btype)
1091 then
1092 return True;
1094 elsif Is_Concurrent_Type (Btype) then
1095 return True;
1097 -- The Is_Limited_Record flag normally indicates that the type is
1098 -- limited. The exception is that a type does not inherit limitedness
1099 -- from its interface ancestor. So the type may be derived from a
1100 -- limited interface, but is not limited.
1102 elsif Is_Limited_Record (Ent)
1103 and then not Is_Interface (Ent)
1104 then
1105 return True;
1107 -- Otherwise we will look around to see if there is some other reason
1108 -- for it to be limited, except that if an error was posted on the
1109 -- entity, then just assume it is non-limited, because it can cause
1110 -- trouble to recurse into a murky entity resulting from other errors.
1112 elsif Error_Posted (Ent) then
1113 return False;
1115 elsif Is_Record_Type (Btype) then
1117 if Is_Limited_Interface (Ent) then
1118 return True;
1120 -- AI-419: limitedness is not inherited from a limited interface
1122 elsif Is_Limited_Record (Rtype) then
1123 return not Is_Interface (Rtype)
1124 or else Is_Protected_Interface (Rtype)
1125 or else Is_Synchronized_Interface (Rtype)
1126 or else Is_Task_Interface (Rtype);
1128 elsif Is_Class_Wide_Type (Btype) then
1129 return Is_Limited_Type (Rtype);
1131 else
1132 declare
1133 C : Entity_Id := First_Component (Btype);
1134 begin
1135 while Present (C) loop
1136 if Is_Limited_Type (Etype (C)) then
1137 return True;
1138 end if;
1140 Next_Component (C);
1141 end loop;
1142 end;
1144 return False;
1145 end if;
1147 elsif Is_Array_Type (Btype) then
1148 return Is_Limited_Type (Component_Type (Btype));
1150 else
1151 return False;
1152 end if;
1153 end Is_Limited_Type;
1155 ---------------------
1156 -- Is_Limited_View --
1157 ---------------------
1159 function Is_Limited_View (Ent : Entity_Id) return Boolean is
1160 Btype : constant Entity_Id := Available_View (Base_Type (Ent));
1162 begin
1163 if Is_Limited_Record (Btype) then
1164 return True;
1166 elsif Ekind (Btype) = E_Limited_Private_Type
1167 and then Nkind (Parent (Btype)) = N_Formal_Type_Declaration
1168 then
1169 return not In_Package_Body (Scope ((Btype)));
1171 elsif Is_Private_Type (Btype) then
1173 -- AI05-0063: A type derived from a limited private formal type is
1174 -- not immutably limited in a generic body.
1176 if Is_Derived_Type (Btype)
1177 and then Is_Generic_Type (Etype (Btype))
1178 then
1179 if not Is_Limited_Type (Etype (Btype)) then
1180 return False;
1182 -- A descendant of a limited formal type is not immutably limited
1183 -- in the generic body, or in the body of a generic child.
1185 elsif Ekind (Scope (Etype (Btype))) = E_Generic_Package then
1186 return not In_Package_Body (Scope (Btype));
1188 else
1189 return False;
1190 end if;
1192 else
1193 declare
1194 Utyp : constant Entity_Id := Underlying_Type (Btype);
1195 begin
1196 if No (Utyp) then
1197 return False;
1198 else
1199 return Is_Limited_View (Utyp);
1200 end if;
1201 end;
1202 end if;
1204 elsif Is_Concurrent_Type (Btype) then
1205 return True;
1207 elsif Is_Record_Type (Btype) then
1209 -- Note that we return True for all limited interfaces, even though
1210 -- (unsynchronized) limited interfaces can have descendants that are
1211 -- nonlimited, because this is a predicate on the type itself, and
1212 -- things like functions with limited interface results need to be
1213 -- handled as build in place even though they might return objects
1214 -- of a type that is not inherently limited.
1216 if Is_Class_Wide_Type (Btype) then
1217 return Is_Limited_View (Root_Type (Btype));
1219 else
1220 declare
1221 C : Entity_Id;
1223 begin
1224 C := First_Component (Btype);
1225 while Present (C) loop
1227 -- Don't consider components with interface types (which can
1228 -- only occur in the case of a _parent component anyway).
1229 -- They don't have any components, plus it would cause this
1230 -- function to return true for nonlimited types derived from
1231 -- limited interfaces.
1233 if not Is_Interface (Etype (C))
1234 and then Is_Limited_View (Etype (C))
1235 then
1236 return True;
1237 end if;
1239 Next_Component (C);
1240 end loop;
1241 end;
1243 return False;
1244 end if;
1246 elsif Is_Array_Type (Btype) then
1247 return Is_Limited_View (Component_Type (Btype));
1249 else
1250 return False;
1251 end if;
1252 end Is_Limited_View;
1254 ----------------------
1255 -- Nearest_Ancestor --
1256 ----------------------
1258 function Nearest_Ancestor (Typ : Entity_Id) return Entity_Id is
1259 D : constant Node_Id := Original_Node (Declaration_Node (Typ));
1260 -- We use the original node of the declaration, because derived
1261 -- types from record subtypes are rewritten as record declarations,
1262 -- and it is the original declaration that carries the ancestor.
1264 begin
1265 -- If we have a subtype declaration, get the ancestor subtype
1267 if Nkind (D) = N_Subtype_Declaration then
1268 if Nkind (Subtype_Indication (D)) = N_Subtype_Indication then
1269 return Entity (Subtype_Mark (Subtype_Indication (D)));
1270 else
1271 return Entity (Subtype_Indication (D));
1272 end if;
1274 -- If derived type declaration, find who we are derived from
1276 elsif Nkind (D) = N_Full_Type_Declaration
1277 and then Nkind (Type_Definition (D)) = N_Derived_Type_Definition
1278 then
1279 declare
1280 DTD : constant Entity_Id := Type_Definition (D);
1281 SI : constant Entity_Id := Subtype_Indication (DTD);
1282 begin
1283 if Is_Entity_Name (SI) then
1284 return Entity (SI);
1285 else
1286 return Entity (Subtype_Mark (SI));
1287 end if;
1288 end;
1290 -- If this is a concurrent declaration with a nonempty interface list,
1291 -- get the first progenitor. Account for case of a record type created
1292 -- for a concurrent type (which is the only case that seems to occur
1293 -- in practice).
1295 elsif Nkind (D) = N_Full_Type_Declaration
1296 and then (Is_Concurrent_Type (Defining_Identifier (D))
1297 or else Is_Concurrent_Record_Type (Defining_Identifier (D)))
1298 and then Is_Non_Empty_List (Interface_List (Type_Definition (D)))
1299 then
1300 return Entity (First (Interface_List (Type_Definition (D))));
1302 -- If derived type and private type, get the full view to find who we
1303 -- are derived from.
1305 elsif Is_Derived_Type (Typ)
1306 and then Is_Private_Type (Typ)
1307 and then Present (Full_View (Typ))
1308 then
1309 return Nearest_Ancestor (Full_View (Typ));
1311 -- Otherwise, nothing useful to return, return Empty
1313 else
1314 return Empty;
1315 end if;
1316 end Nearest_Ancestor;
1318 ---------------------------
1319 -- Nearest_Dynamic_Scope --
1320 ---------------------------
1322 function Nearest_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
1323 begin
1324 if Is_Dynamic_Scope (Ent) then
1325 return Ent;
1326 else
1327 return Enclosing_Dynamic_Scope (Ent);
1328 end if;
1329 end Nearest_Dynamic_Scope;
1331 ------------------------
1332 -- Next_Tag_Component --
1333 ------------------------
1335 function Next_Tag_Component (Tag : Entity_Id) return Entity_Id is
1336 Comp : Entity_Id;
1338 begin
1339 pragma Assert (Is_Tag (Tag));
1341 -- Loop to look for next tag component
1343 Comp := Next_Entity (Tag);
1344 while Present (Comp) loop
1345 if Is_Tag (Comp) then
1346 pragma Assert (Chars (Comp) /= Name_uTag);
1347 return Comp;
1348 end if;
1350 Next_Entity (Comp);
1351 end loop;
1353 -- No tag component found
1355 return Empty;
1356 end Next_Tag_Component;
1358 --------------------------
1359 -- Number_Discriminants --
1360 --------------------------
1362 function Number_Discriminants (Typ : Entity_Id) return Pos is
1363 N : Nat := 0;
1364 Discr : Entity_Id := First_Discriminant (Typ);
1366 begin
1367 while Present (Discr) loop
1368 N := N + 1;
1369 Next_Discriminant (Discr);
1370 end loop;
1372 return N;
1373 end Number_Discriminants;
1375 ----------------------------------------------
1376 -- Object_Type_Has_Constrained_Partial_View --
1377 ----------------------------------------------
1379 function Object_Type_Has_Constrained_Partial_View
1380 (Typ : Entity_Id;
1381 Scop : Entity_Id) return Boolean
1383 begin
1384 return Has_Constrained_Partial_View (Typ)
1385 or else (In_Generic_Body (Scop)
1386 and then Is_Generic_Type (Base_Type (Typ))
1387 and then (Is_Private_Type (Base_Type (Typ))
1388 or else Is_Derived_Type (Base_Type (Typ)))
1389 and then not Is_Tagged_Type (Typ)
1390 and then not (Is_Array_Type (Typ)
1391 and then not Is_Constrained (Typ))
1392 and then Has_Discriminants (Typ));
1393 end Object_Type_Has_Constrained_Partial_View;
1395 ------------------
1396 -- Package_Body --
1397 ------------------
1399 function Package_Body (E : Entity_Id) return Node_Id is
1400 Body_Decl : Node_Id;
1401 Body_Id : constant Opt_E_Package_Body_Id :=
1402 Corresponding_Body (Package_Spec (E));
1404 begin
1405 if Present (Body_Id) then
1406 Body_Decl := Parent (Body_Id);
1408 if Nkind (Body_Decl) = N_Defining_Program_Unit_Name then
1409 Body_Decl := Parent (Body_Decl);
1410 end if;
1412 pragma Assert (Nkind (Body_Decl) = N_Package_Body);
1414 return Body_Decl;
1415 else
1416 return Empty;
1417 end if;
1418 end Package_Body;
1420 ------------------
1421 -- Package_Spec --
1422 ------------------
1424 function Package_Spec (E : Entity_Id) return Node_Id is
1425 begin
1426 return Parent (Package_Specification (E));
1427 end Package_Spec;
1429 ---------------------------
1430 -- Package_Specification --
1431 ---------------------------
1433 function Package_Specification (E : Entity_Id) return Node_Id is
1434 N : Node_Id;
1436 begin
1437 pragma Assert (Is_Package_Or_Generic_Package (E));
1439 N := Parent (E);
1441 if Nkind (N) = N_Defining_Program_Unit_Name then
1442 N := Parent (N);
1443 end if;
1445 pragma Assert (Nkind (N) = N_Package_Specification);
1447 return N;
1448 end Package_Specification;
1450 ---------------------
1451 -- Subprogram_Body --
1452 ---------------------
1454 function Subprogram_Body (E : Entity_Id) return Node_Id is
1455 Body_E : constant Entity_Id := Subprogram_Body_Entity (E);
1457 begin
1458 if No (Body_E) then
1459 return Empty;
1460 else
1461 return Parent (Subprogram_Specification (Body_E));
1462 end if;
1463 end Subprogram_Body;
1465 ----------------------------
1466 -- Subprogram_Body_Entity --
1467 ----------------------------
1469 function Subprogram_Body_Entity (E : Entity_Id) return Entity_Id is
1470 N : constant Node_Id := Parent (Subprogram_Specification (E));
1471 -- Declaration for E
1473 begin
1474 -- If this declaration is not a subprogram body, then it must be a
1475 -- subprogram declaration or body stub, from which we can retrieve the
1476 -- entity for the corresponding subprogram body if any, or an abstract
1477 -- subprogram declaration, for which we return Empty.
1479 case Nkind (N) is
1480 when N_Subprogram_Body =>
1481 return E;
1483 when N_Subprogram_Body_Stub
1484 | N_Subprogram_Declaration
1486 return Corresponding_Body (N);
1488 when others =>
1489 return Empty;
1490 end case;
1491 end Subprogram_Body_Entity;
1493 ---------------------
1494 -- Subprogram_Spec --
1495 ---------------------
1497 function Subprogram_Spec (E : Entity_Id) return Node_Id is
1498 N : constant Node_Id := Parent (Subprogram_Specification (E));
1499 -- Declaration for E
1501 begin
1502 -- This declaration is either subprogram declaration or a subprogram
1503 -- body, in which case return Empty.
1505 if Nkind (N) = N_Subprogram_Declaration then
1506 return N;
1507 else
1508 return Empty;
1509 end if;
1510 end Subprogram_Spec;
1512 ------------------------------
1513 -- Subprogram_Specification --
1514 ------------------------------
1516 function Subprogram_Specification (E : Entity_Id) return Node_Id is
1517 N : Node_Id;
1519 begin
1520 N := Parent (E);
1522 if Nkind (N) = N_Defining_Program_Unit_Name then
1523 N := Parent (N);
1524 end if;
1526 -- If the Parent pointer of E is not a subprogram specification node
1527 -- (going through an intermediate N_Defining_Program_Unit_Name node
1528 -- for subprogram units), then E is an inherited operation. Its parent
1529 -- points to the type derivation that produces the inheritance: that's
1530 -- the node that generates the subprogram specification. Its alias
1531 -- is the parent subprogram, and that one points to a subprogram
1532 -- declaration, or to another type declaration if this is a hierarchy
1533 -- of derivations.
1535 if Nkind (N) not in N_Subprogram_Specification then
1536 pragma Assert (Present (Alias (E)));
1537 N := Subprogram_Specification (Alias (E));
1538 end if;
1540 return N;
1541 end Subprogram_Specification;
1543 --------------------
1544 -- Ultimate_Alias --
1545 --------------------
1547 function Ultimate_Alias (Prim : Entity_Id) return Entity_Id is
1548 E : Entity_Id := Prim;
1550 begin
1551 while Present (Alias (E)) loop
1552 pragma Assert (Alias (E) /= E);
1553 E := Alias (E);
1554 end loop;
1556 return E;
1557 end Ultimate_Alias;
1559 --------------------------
1560 -- Unit_Declaration_Node --
1561 --------------------------
1563 function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
1564 N : Node_Id := Parent (Unit_Id);
1566 begin
1567 -- Predefined operators do not have a full function declaration
1569 if Ekind (Unit_Id) = E_Operator then
1570 return N;
1571 end if;
1573 -- Isn't there some better way to express the following ???
1575 while Nkind (N) /= N_Abstract_Subprogram_Declaration
1576 and then Nkind (N) /= N_Entry_Body
1577 and then Nkind (N) /= N_Entry_Declaration
1578 and then Nkind (N) /= N_Formal_Package_Declaration
1579 and then Nkind (N) /= N_Function_Instantiation
1580 and then Nkind (N) /= N_Generic_Package_Declaration
1581 and then Nkind (N) /= N_Generic_Subprogram_Declaration
1582 and then Nkind (N) /= N_Package_Declaration
1583 and then Nkind (N) /= N_Package_Body
1584 and then Nkind (N) /= N_Package_Instantiation
1585 and then Nkind (N) /= N_Package_Renaming_Declaration
1586 and then Nkind (N) /= N_Procedure_Instantiation
1587 and then Nkind (N) /= N_Protected_Body
1588 and then Nkind (N) /= N_Protected_Type_Declaration
1589 and then Nkind (N) /= N_Subprogram_Declaration
1590 and then Nkind (N) /= N_Subprogram_Body
1591 and then Nkind (N) /= N_Subprogram_Body_Stub
1592 and then Nkind (N) /= N_Subprogram_Renaming_Declaration
1593 and then Nkind (N) /= N_Task_Body
1594 and then Nkind (N) /= N_Task_Type_Declaration
1595 and then Nkind (N) not in N_Formal_Subprogram_Declaration
1596 and then Nkind (N) not in N_Generic_Renaming_Declaration
1597 loop
1598 N := Parent (N);
1600 -- We don't use Assert here, because that causes an infinite loop
1601 -- when assertions are turned off. Better to crash.
1603 if No (N) then
1604 raise Program_Error;
1605 end if;
1606 end loop;
1608 return N;
1609 end Unit_Declaration_Node;
1611 end Sem_Aux;