1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
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
54 D
: constant Node_Id
:= Declaration_Node
(Typ
);
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
)));
63 return Entity
(Subtype_Indication
(D
));
66 -- If not, then no subtype indication is available
78 function Available_View
(Ent
: Entity_Id
) return Entity_Id
is
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
96 function Constant_Value
(Ent
: Entity_Id
) return Node_Id
is
97 D
: constant Node_Id
:= Declaration_Node
(Ent
);
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
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
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
))
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
);
136 return Expression
(Full_D
);
139 -- Otherwise we have no expression to return
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
));
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
;
169 end Corresponding_Unsigned_Type
;
171 -----------------------------
172 -- Enclosing_Dynamic_Scope --
173 -----------------------------
175 function Enclosing_Dynamic_Scope
(Ent
: Entity_Id
) return Entity_Id
is
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
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.
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.
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
)))
212 -- Otherwise keep climbing
218 end Enclosing_Dynamic_Scope
;
220 ------------------------
221 -- First_Discriminant --
222 ------------------------
224 function First_Discriminant
(Typ
: Entity_Id
) return Entity_Id
is
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
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
);
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.
258 end First_Discriminant
;
260 -------------------------------
261 -- First_Stored_Discriminant --
262 -------------------------------
264 function First_Stored_Discriminant
(Typ
: Entity_Id
) return Entity_Id
is
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
284 pragma Assert
(Ekind
(Typ
) = E_Discriminant
);
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
295 elsif Ekind
(Ent
) = E_Discriminant
296 and then Is_Completely_Hidden
(Ent
)
305 end Has_Completely_Hidden_Discriminant
;
307 -- Start of processing for First_Stored_Discriminant
311 (Has_Discriminants
(Typ
)
312 or else Has_Unknown_Discriminants
(Typ
));
314 Ent
:= First_Entity
(Typ
);
316 if Chars
(Ent
) = Name_uTag
then
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
);
328 pragma Assert
(Ekind
(Ent
) = E_Discriminant
);
331 end First_Stored_Discriminant
;
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
);
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
347 if Nkind
(F
) = N_Null_Statement
then
348 F
:= Original_Node
(F
);
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.
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
));
382 return Defining_Identifier
(Associated_Node_For_Itype
(B
));
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.
394 Ent
:= First_Subtype_Link
(F
);
396 if Present
(Ent
) then
404 -------------------------
405 -- First_Tag_Component --
406 -------------------------
408 function First_Tag_Component
(Typ
: Entity_Id
) return Entity_Id
is
413 pragma Assert
(Is_Tagged_Type
(Typ
)
414 or else Is_Class_Wide_Equivalent_Type
(Typ
));
418 if Is_Class_Wide_Type
(Ctyp
) then
419 Ctyp
:= Root_Type
(Ctyp
);
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).
434 Comp
:= First_Entity
(Ctyp
);
435 while Present
(Comp
) loop
436 if Is_Tag
(Comp
) then
443 -- No tag component found
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
);
457 if Nkind
(Nam
) = N_Explicit_Dereference
then
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
)));
472 end Get_Called_Entity
;
478 function Get_Rep_Item
481 Check_Parents
: Boolean := True) return Node_Id
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
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
))
501 if Check_Parents
then
504 -- If Check_Parents is False, return N if the pragma doesn't
505 -- appear in the Rep_Item chain of the parent.
509 Par
: constant Entity_Id
:= Nearest_Ancestor
(E
);
510 -- This node represents the parent type of type E (if any)
516 elsif not Present_In_Rep_Item
(Par
, N
) then
522 elsif Nkind
(N
) = N_Attribute_Definition_Clause
525 or else (Nam
= Name_Priority
526 and then Chars
(N
) = Name_Interrupt_Priority
))
528 if Check_Parents
or else Entity
(N
) = E
then
532 elsif Nkind
(N
) = N_Aspect_Specification
534 (Chars
(Identifier
(N
)) = Nam
537 and then Chars
(Identifier
(N
)) = Name_Interrupt_Priority
))
539 if Check_Parents
then
542 elsif Entity
(N
) = E
then
546 -- A Ghost-related aspect, if disabled, may have been replaced by a
549 elsif Nkind
(N
) = N_Null_Statement
then
550 N
:= Original_Node
(N
);
559 function Get_Rep_Item
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
);
571 -- Check both Nam1_Item and Nam2_Item are present
573 if No
(Nam1_Item
) then
575 elsif No
(Nam2_Item
) then
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
597 function Get_Rep_Pragma
600 Check_Parents
: Boolean := True) return Node_Id
602 N
: constant Node_Id
:= Get_Rep_Item
(E
, Nam
, Check_Parents
);
605 if Present
(N
) and then Nkind
(N
) = N_Pragma
then
612 function Get_Rep_Pragma
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
);
624 -- Check both Nam1_Item and Nam2_Item are present
626 if No
(Nam1_Item
) then
628 elsif No
(Nam2_Item
) then
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
646 ---------------------------------
647 -- Has_External_Tag_Rep_Clause --
648 ---------------------------------
650 function Has_External_Tag_Rep_Clause
(T
: Entity_Id
) return Boolean is
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
;
660 function Has_Rep_Item
663 Check_Parents
: Boolean := True) return Boolean
666 return Present
(Get_Rep_Item
(E
, Nam
, Check_Parents
));
669 function Has_Rep_Item
673 Check_Parents
: Boolean := True) return Boolean
676 return Present
(Get_Rep_Item
(E
, Nam1
, Nam2
, Check_Parents
));
683 function Has_Rep_Pragma
686 Check_Parents
: Boolean := True) return Boolean
689 return Present
(Get_Rep_Pragma
(E
, Nam
, Check_Parents
));
692 function Has_Rep_Pragma
696 Check_Parents
: Boolean := True) return Boolean
699 return Present
(Get_Rep_Pragma
(E
, Nam1
, Nam2
, Check_Parents
));
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
);
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
));
718 end Has_Unconstrained_Elements
;
720 ----------------------
721 -- Has_Variant_Part --
722 ----------------------
724 function Has_Variant_Part
(Typ
: Entity_Id
) return Boolean is
731 FSTyp
:= First_Subtype
(Typ
);
733 if not Has_Discriminants
(FSTyp
) then
737 -- Proceed with cautious checks here, return False if tree is not
738 -- as expected (may be caused by prior errors).
740 Decl
:= Declaration_Node
(FSTyp
);
742 if Nkind
(Decl
) /= N_Full_Type_Declaration
then
746 TDef
:= Type_Definition
(Decl
);
748 if Nkind
(TDef
) /= N_Record_Definition
then
752 CList
:= Component_List
(TDef
);
754 if Nkind
(CList
) /= N_Component_List
then
757 return Present
(Variant_Part
(CList
));
759 end Has_Variant_Part
;
761 ---------------------
762 -- In_Generic_Body --
763 ---------------------
765 function In_Generic_Body
(Id
: Entity_Id
) return Boolean is
769 -- Climb scopes looking for generic body
772 while Present
(S
) and then S
/= Standard_Standard
loop
774 -- Generic package body
776 if Ekind
(S
) = E_Generic_Package
777 and then In_Package_Body
(S
)
781 -- Generic subprogram body
783 elsif Is_Subprogram
(S
)
784 and then Nkind
(Unit_Declaration_Node
(S
)) =
785 N_Generic_Subprogram_Declaration
793 -- False if top of scope stack without finding a generic body
798 -------------------------------
799 -- Initialization_Suppressed --
800 -------------------------------
802 function Initialization_Suppressed
(Typ
: Entity_Id
) return Boolean is
804 return Suppress_Initialization
(Typ
)
805 or else Suppress_Initialization
(Base_Type
(Typ
));
806 end Initialization_Suppressed
;
812 procedure Initialize
is
814 Obsolescent_Warnings
.Init
;
821 function Is_Body
(N
: Node_Id
) return Boolean is
824 N_Body_Stub | N_Entry_Body | N_Package_Body | N_Protected_Body |
825 N_Subprogram_Body | N_Task_Body
;
828 ---------------------
829 -- Is_By_Copy_Type --
830 ---------------------
832 function Is_By_Copy_Type
(Ent
: Entity_Id
) return Boolean is
834 -- If Id is a private type whose full declaration has not been seen,
835 -- we assume for now that it is not a By_Copy type. Clearly this
836 -- attribute should not be used before the type is frozen, but it is
837 -- needed to build the associated record of a protected type. Another
838 -- place where some lookahead for a full view is needed ???
841 Is_Elementary_Type
(Ent
)
842 or else (Is_Private_Type
(Ent
)
843 and then Present
(Underlying_Type
(Ent
))
844 and then Is_Elementary_Type
(Underlying_Type
(Ent
)));
847 --------------------------
848 -- Is_By_Reference_Type --
849 --------------------------
851 function Is_By_Reference_Type
(Ent
: Entity_Id
) return Boolean is
852 Btype
: constant Entity_Id
:= Base_Type
(Ent
);
855 if Is_Private_Type
(Btype
) then
857 Utyp
: constant Entity_Id
:= Underlying_Type
(Btype
);
862 return Is_By_Reference_Type
(Utyp
);
866 elsif Is_Incomplete_Type
(Btype
) then
868 Ftyp
: constant Entity_Id
:= Full_View
(Btype
);
870 -- Return true for a tagged incomplete type built as a shadow
871 -- entity in Build_Limited_Views. It can appear in the profile
872 -- of a thunk and the back end needs to know how it is passed.
875 return Is_Tagged_Type
(Btype
);
877 return Is_By_Reference_Type
(Ftyp
);
881 elsif Is_Concurrent_Type
(Btype
) then
884 elsif Is_Record_Type
(Btype
) then
885 if Is_Controlled
(Btype
)
886 or else Is_Limited_Record
(Btype
)
887 or else Is_Tagged_Type
(Btype
)
888 or else Is_Volatile
(Btype
)
897 C
:= First_Component
(Btype
);
898 while Present
(C
) loop
900 -- For each component, test if its type is a by reference
901 -- type and if its type is volatile. Also test the component
902 -- itself for being volatile. This happens for example when
903 -- a Volatile aspect is added to a component.
905 if Is_By_Reference_Type
(Etype
(C
))
906 or else Is_Volatile
(Etype
(C
))
907 or else Is_Volatile
(C
)
919 elsif Is_Array_Type
(Btype
) then
922 or else Is_By_Reference_Type
(Component_Type
(Btype
))
923 or else Is_Volatile
(Component_Type
(Btype
))
924 or else Has_Volatile_Components
(Btype
);
929 end Is_By_Reference_Type
;
931 -------------------------
932 -- Is_Definite_Subtype --
933 -------------------------
935 function Is_Definite_Subtype
(T
: Entity_Id
) return Boolean is
936 pragma Assert
(Is_Type
(T
));
937 K
: constant Entity_Kind
:= Ekind
(T
);
940 if Is_Constrained
(T
) then
943 elsif K
in Array_Kind
944 or else K
in Class_Wide_Kind
945 or else Has_Unknown_Discriminants
(T
)
949 -- Known discriminants: definite if there are default values. Note that
950 -- if any discriminant has a default, they all do.
952 elsif Has_Discriminants
(T
) then
953 return Present
(Discriminant_Default_Value
(First_Discriminant
(T
)));
958 end Is_Definite_Subtype
;
960 ---------------------
961 -- Is_Derived_Type --
962 ---------------------
964 function Is_Derived_Type
(Ent
: Entity_Id
) return B
is
969 and then Base_Type
(Ent
) /= Root_Type
(Ent
)
970 and then not Is_Class_Wide_Type
(Ent
)
972 -- An access_to_subprogram whose result type is a limited view can
973 -- appear in a return statement, without the full view of the result
974 -- type being available. Do not interpret this as a derived type.
976 and then Ekind
(Ent
) /= E_Subprogram_Type
978 if not Is_Numeric_Type
(Root_Type
(Ent
)) then
982 Par
:= Parent
(First_Subtype
(Ent
));
985 and then Nkind
(Par
) = N_Full_Type_Declaration
986 and then Nkind
(Type_Definition
(Par
)) =
987 N_Derived_Type_Definition
;
995 -----------------------
996 -- Is_Generic_Formal --
997 -----------------------
999 function Is_Generic_Formal
(E
: Entity_Id
) return Boolean is
1006 -- Formal derived types are rewritten as private extensions, so
1007 -- examine original node.
1009 Kind
:= Nkind
(Original_Node
(Parent
(E
)));
1012 Kind
in N_Formal_Object_Declaration | N_Formal_Type_Declaration
1013 or else Is_Formal_Subprogram
(E
)
1015 (Ekind
(E
) = E_Package
1016 and then Nkind
(Original_Node
(Unit_Declaration_Node
(E
))) =
1017 N_Formal_Package_Declaration
);
1019 end Is_Generic_Formal
;
1021 ---------------------
1022 -- Is_Limited_Type --
1023 ---------------------
1025 function Is_Limited_Type
(Ent
: Entity_Id
) return Boolean is
1030 if not Is_Type
(Ent
) then
1034 Btype
:= Base_Type
(Ent
);
1035 Rtype
:= Root_Type
(Btype
);
1037 if Ekind
(Btype
) = E_Limited_Private_Type
1038 or else Is_Limited_Composite
(Btype
)
1042 elsif Is_Concurrent_Type
(Btype
) then
1045 -- The Is_Limited_Record flag normally indicates that the type is
1046 -- limited. The exception is that a type does not inherit limitedness
1047 -- from its interface ancestor. So the type may be derived from a
1048 -- limited interface, but is not limited.
1050 elsif Is_Limited_Record
(Ent
)
1051 and then not Is_Interface
(Ent
)
1055 -- Otherwise we will look around to see if there is some other reason
1056 -- for it to be limited, except that if an error was posted on the
1057 -- entity, then just assume it is non-limited, because it can cause
1058 -- trouble to recurse into a murky entity resulting from other errors.
1060 elsif Error_Posted
(Ent
) then
1063 elsif Is_Record_Type
(Btype
) then
1065 if Is_Limited_Interface
(Ent
) then
1068 -- AI-419: limitedness is not inherited from a limited interface
1070 elsif Is_Limited_Record
(Rtype
) then
1071 return not Is_Interface
(Rtype
)
1072 or else Is_Protected_Interface
(Rtype
)
1073 or else Is_Synchronized_Interface
(Rtype
)
1074 or else Is_Task_Interface
(Rtype
);
1076 elsif Is_Class_Wide_Type
(Btype
) then
1077 return Is_Limited_Type
(Rtype
);
1081 C
: Entity_Id
:= First_Component
(Btype
);
1083 while Present
(C
) loop
1084 if Is_Limited_Type
(Etype
(C
)) then
1095 elsif Is_Array_Type
(Btype
) then
1096 return Is_Limited_Type
(Component_Type
(Btype
));
1101 end Is_Limited_Type
;
1103 -------------------------------
1104 -- Is_Immutably_Limited_Type --
1105 -------------------------------
1107 function Is_Immutably_Limited_Type
(Ent
: Entity_Id
) return Boolean is
1108 Btype
: constant Entity_Id
:= Available_View
(Base_Type
(Ent
));
1111 if Is_Limited_Record
(Btype
) then
1114 elsif Ekind
(Btype
) = E_Limited_Private_Type
1115 and then Nkind
(Parent
(Btype
)) = N_Formal_Type_Declaration
1117 return not In_Package_Body
(Scope
((Btype
)));
1119 elsif Is_Private_Type
(Btype
) then
1121 -- AI05-0063: A type derived from a limited private formal type is
1122 -- not immutably limited in a generic body.
1124 if Is_Derived_Type
(Btype
)
1125 and then Is_Generic_Type
(Etype
(Btype
))
1127 if not Is_Limited_Type
(Etype
(Btype
)) then
1130 -- A descendant of a limited formal type is not immutably limited
1131 -- in the generic body, or in the body of a generic child.
1133 elsif Ekind
(Scope
(Etype
(Btype
))) = E_Generic_Package
then
1134 return not In_Package_Body
(Scope
(Btype
));
1144 elsif Is_Concurrent_Type
(Btype
) then
1150 end Is_Immutably_Limited_Type
;
1152 --------------------------------
1153 -- Is_Inherently_Limited_Type --
1154 --------------------------------
1156 function Is_Inherently_Limited_Type
(Ent
: Entity_Id
) return Boolean is
1157 Btype
: constant Entity_Id
:= Available_View
(Base_Type
(Ent
));
1160 if Is_Limited_Record
(Btype
) then
1163 elsif Ekind
(Btype
) = E_Limited_Private_Type
1164 and then Nkind
(Parent
(Btype
)) = N_Formal_Type_Declaration
1166 return not In_Package_Body
(Scope
((Btype
)));
1168 elsif Is_Private_Type
(Btype
) then
1170 -- AI05-0063: A type derived from a limited private formal type is
1171 -- not immutably limited in a generic body.
1173 if Is_Derived_Type
(Btype
)
1174 and then Is_Generic_Type
(Etype
(Btype
))
1176 if not Is_Limited_Type
(Etype
(Btype
)) then
1179 -- A descendant of a limited formal type is not immutably limited
1180 -- in the generic body, or in the body of a generic child.
1182 elsif Ekind
(Scope
(Etype
(Btype
))) = E_Generic_Package
then
1183 return not In_Package_Body
(Scope
(Btype
));
1191 Utyp
: constant Entity_Id
:= Underlying_Type
(Btype
);
1196 return Is_Inherently_Limited_Type
(Utyp
);
1201 elsif Is_Concurrent_Type
(Btype
) then
1204 elsif Is_Record_Type
(Btype
) then
1206 -- Note that we return True for all limited interfaces, even though
1207 -- (unsynchronized) limited interfaces can have descendants that are
1208 -- nonlimited, because this is a predicate on the type itself, and
1209 -- things like functions with limited interface results need to be
1210 -- handled as build in place even though they might return objects
1211 -- of a type that is not inherently limited.
1213 if Is_Class_Wide_Type
(Btype
) then
1214 return Is_Inherently_Limited_Type
(Root_Type
(Btype
));
1221 C
:= First_Component
(Btype
);
1222 while Present
(C
) loop
1224 -- Don't consider components with interface types (which can
1225 -- only occur in the case of a _parent component anyway).
1226 -- They don't have any components, plus it would cause this
1227 -- function to return true for nonlimited types derived from
1228 -- limited interfaces.
1230 if not Is_Interface
(Etype
(C
))
1231 and then Is_Inherently_Limited_Type
(Etype
(C
))
1243 elsif Is_Array_Type
(Btype
) then
1244 return Is_Inherently_Limited_Type
(Component_Type
(Btype
));
1249 end Is_Inherently_Limited_Type
;
1251 ----------------------
1252 -- Nearest_Ancestor --
1253 ----------------------
1255 function Nearest_Ancestor
(Typ
: Entity_Id
) return Entity_Id
is
1256 D
: constant Node_Id
:= Original_Node
(Declaration_Node
(Typ
));
1257 -- We use the original node of the declaration, because derived
1258 -- types from record subtypes are rewritten as record declarations,
1259 -- and it is the original declaration that carries the ancestor.
1262 -- If we have a subtype declaration, get the ancestor subtype
1264 if Nkind
(D
) = N_Subtype_Declaration
then
1265 if Nkind
(Subtype_Indication
(D
)) = N_Subtype_Indication
then
1266 return Entity
(Subtype_Mark
(Subtype_Indication
(D
)));
1268 return Entity
(Subtype_Indication
(D
));
1271 -- If derived type declaration, find who we are derived from
1273 elsif Nkind
(D
) = N_Full_Type_Declaration
1274 and then Nkind
(Type_Definition
(D
)) = N_Derived_Type_Definition
1277 DTD
: constant Entity_Id
:= Type_Definition
(D
);
1278 SI
: constant Entity_Id
:= Subtype_Indication
(DTD
);
1280 if Is_Entity_Name
(SI
) then
1283 return Entity
(Subtype_Mark
(SI
));
1287 -- If this is a concurrent declaration with a nonempty interface list,
1288 -- get the first progenitor. Account for case of a record type created
1289 -- for a concurrent type (which is the only case that seems to occur
1292 elsif Nkind
(D
) = N_Full_Type_Declaration
1293 and then (Is_Concurrent_Type
(Defining_Identifier
(D
))
1294 or else Is_Concurrent_Record_Type
(Defining_Identifier
(D
)))
1295 and then Is_Non_Empty_List
(Interface_List
(Type_Definition
(D
)))
1297 return Entity
(First
(Interface_List
(Type_Definition
(D
))));
1299 -- If derived type and private type, get the full view to find who we
1300 -- are derived from.
1302 elsif Is_Derived_Type
(Typ
)
1303 and then Is_Private_Type
(Typ
)
1304 and then Present
(Full_View
(Typ
))
1306 return Nearest_Ancestor
(Full_View
(Typ
));
1308 -- Otherwise, nothing useful to return, return Empty
1313 end Nearest_Ancestor
;
1315 ---------------------------
1316 -- Nearest_Dynamic_Scope --
1317 ---------------------------
1319 function Nearest_Dynamic_Scope
(Ent
: Entity_Id
) return Entity_Id
is
1321 if Is_Dynamic_Scope
(Ent
) then
1324 return Enclosing_Dynamic_Scope
(Ent
);
1326 end Nearest_Dynamic_Scope
;
1328 ------------------------
1329 -- Next_Tag_Component --
1330 ------------------------
1332 function Next_Tag_Component
(Tag
: Entity_Id
) return Entity_Id
is
1336 pragma Assert
(Is_Tag
(Tag
));
1338 -- Loop to look for next tag component
1340 Comp
:= Next_Entity
(Tag
);
1341 while Present
(Comp
) loop
1342 if Is_Tag
(Comp
) then
1343 pragma Assert
(Chars
(Comp
) /= Name_uTag
);
1350 -- No tag component found
1353 end Next_Tag_Component
;
1355 --------------------------
1356 -- Number_Discriminants --
1357 --------------------------
1359 function Number_Discriminants
(Typ
: Entity_Id
) return Pos
is
1361 Discr
: Entity_Id
:= First_Discriminant
(Typ
);
1364 while Present
(Discr
) loop
1366 Next_Discriminant
(Discr
);
1370 end Number_Discriminants
;
1372 ----------------------------------------------
1373 -- Object_Type_Has_Constrained_Partial_View --
1374 ----------------------------------------------
1376 function Object_Type_Has_Constrained_Partial_View
1378 Scop
: Entity_Id
) return Boolean
1381 return Has_Constrained_Partial_View
(Typ
)
1382 or else (In_Generic_Body
(Scop
)
1383 and then Is_Generic_Type
(Base_Type
(Typ
))
1384 and then (Is_Private_Type
(Base_Type
(Typ
))
1385 or else Is_Derived_Type
(Base_Type
(Typ
)))
1386 and then not Is_Tagged_Type
(Typ
)
1387 and then not (Is_Array_Type
(Typ
)
1388 and then not Is_Constrained
(Typ
))
1389 and then Has_Discriminants
(Typ
));
1390 end Object_Type_Has_Constrained_Partial_View
;
1396 function Package_Body
(E
: Entity_Id
) return Node_Id
is
1397 Body_Decl
: Node_Id
;
1398 Body_Id
: constant Opt_E_Package_Body_Id
:=
1399 Corresponding_Body
(Package_Spec
(E
));
1402 if Present
(Body_Id
) then
1403 Body_Decl
:= Parent
(Body_Id
);
1405 if Nkind
(Body_Decl
) = N_Defining_Program_Unit_Name
then
1406 Body_Decl
:= Parent
(Body_Decl
);
1409 pragma Assert
(Nkind
(Body_Decl
) = N_Package_Body
);
1421 function Package_Spec
(E
: Entity_Id
) return Node_Id
is
1423 return Parent
(Package_Specification
(E
));
1426 ---------------------------
1427 -- Package_Specification --
1428 ---------------------------
1430 function Package_Specification
(E
: Entity_Id
) return Node_Id
is
1434 pragma Assert
(Is_Package_Or_Generic_Package
(E
));
1438 if Nkind
(N
) = N_Defining_Program_Unit_Name
then
1442 pragma Assert
(Nkind
(N
) = N_Package_Specification
);
1445 end Package_Specification
;
1447 ---------------------
1448 -- Subprogram_Body --
1449 ---------------------
1451 function Subprogram_Body
(E
: Entity_Id
) return Node_Id
is
1452 Body_E
: constant Entity_Id
:= Subprogram_Body_Entity
(E
);
1458 return Parent
(Subprogram_Specification
(Body_E
));
1460 end Subprogram_Body
;
1462 ----------------------------
1463 -- Subprogram_Body_Entity --
1464 ----------------------------
1466 function Subprogram_Body_Entity
(E
: Entity_Id
) return Entity_Id
is
1467 N
: constant Node_Id
:= Parent
(Subprogram_Specification
(E
));
1468 -- Declaration for E
1471 -- If this declaration is not a subprogram body, then it must be a
1472 -- subprogram declaration or body stub, from which we can retrieve the
1473 -- entity for the corresponding subprogram body if any, or an abstract
1474 -- subprogram declaration, for which we return Empty.
1477 when N_Subprogram_Body
=>
1480 when N_Subprogram_Body_Stub
1481 | N_Subprogram_Declaration
1483 return Corresponding_Body
(N
);
1488 end Subprogram_Body_Entity
;
1490 ---------------------
1491 -- Subprogram_Spec --
1492 ---------------------
1494 function Subprogram_Spec
(E
: Entity_Id
) return Node_Id
is
1495 N
: constant Node_Id
:= Parent
(Subprogram_Specification
(E
));
1496 -- Declaration for E
1499 -- This declaration is either subprogram declaration or a subprogram
1500 -- body, in which case return Empty.
1502 if Nkind
(N
) = N_Subprogram_Declaration
then
1507 end Subprogram_Spec
;
1509 ------------------------------
1510 -- Subprogram_Specification --
1511 ------------------------------
1513 function Subprogram_Specification
(E
: Entity_Id
) return Node_Id
is
1519 if Nkind
(N
) = N_Defining_Program_Unit_Name
then
1523 -- If the Parent pointer of E is not a subprogram specification node
1524 -- (going through an intermediate N_Defining_Program_Unit_Name node
1525 -- for subprogram units), then E is an inherited operation. Its parent
1526 -- points to the type derivation that produces the inheritance: that's
1527 -- the node that generates the subprogram specification. Its alias
1528 -- is the parent subprogram, and that one points to a subprogram
1529 -- declaration, or to another type declaration if this is a hierarchy
1532 if Nkind
(N
) not in N_Subprogram_Specification
then
1533 pragma Assert
(Present
(Alias
(E
)));
1534 N
:= Subprogram_Specification
(Alias
(E
));
1538 end Subprogram_Specification
;
1540 --------------------
1541 -- Ultimate_Alias --
1542 --------------------
1544 function Ultimate_Alias
(Prim
: Entity_Id
) return Entity_Id
is
1545 E
: Entity_Id
:= Prim
;
1548 while Present
(Alias
(E
)) loop
1549 pragma Assert
(Alias
(E
) /= E
);
1556 --------------------------
1557 -- Unit_Declaration_Node --
1558 --------------------------
1560 function Unit_Declaration_Node
(Unit_Id
: Entity_Id
) return Node_Id
is
1561 N
: Node_Id
:= Parent
(Unit_Id
);
1564 -- Predefined operators do not have a full function declaration
1566 if Ekind
(Unit_Id
) = E_Operator
then
1570 -- Isn't there some better way to express the following ???
1572 while Nkind
(N
) /= N_Abstract_Subprogram_Declaration
1573 and then Nkind
(N
) /= N_Entry_Body
1574 and then Nkind
(N
) /= N_Entry_Declaration
1575 and then Nkind
(N
) /= N_Formal_Package_Declaration
1576 and then Nkind
(N
) /= N_Function_Instantiation
1577 and then Nkind
(N
) /= N_Generic_Package_Declaration
1578 and then Nkind
(N
) /= N_Generic_Subprogram_Declaration
1579 and then Nkind
(N
) /= N_Package_Declaration
1580 and then Nkind
(N
) /= N_Package_Body
1581 and then Nkind
(N
) /= N_Package_Instantiation
1582 and then Nkind
(N
) /= N_Package_Renaming_Declaration
1583 and then Nkind
(N
) /= N_Procedure_Instantiation
1584 and then Nkind
(N
) /= N_Protected_Body
1585 and then Nkind
(N
) /= N_Protected_Type_Declaration
1586 and then Nkind
(N
) /= N_Subprogram_Declaration
1587 and then Nkind
(N
) /= N_Subprogram_Body
1588 and then Nkind
(N
) /= N_Subprogram_Body_Stub
1589 and then Nkind
(N
) /= N_Subprogram_Renaming_Declaration
1590 and then Nkind
(N
) /= N_Task_Body
1591 and then Nkind
(N
) /= N_Task_Type_Declaration
1592 and then Nkind
(N
) not in N_Formal_Subprogram_Declaration
1593 and then Nkind
(N
) not in N_Generic_Renaming_Declaration
1597 -- We don't use Assert here, because that causes an infinite loop
1598 -- when assertions are turned off. Better to crash.
1601 raise Program_Error
;
1606 end Unit_Declaration_Node
;