* config/avr/avr.md: Fix indentations of insn C snippets.
[official-gcc.git] / gcc / ada / sem_aux.adb
blobbb24fc2e21ac5236064d9b2cbe9a732dbff479ca
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-2012, 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 -- As a special exception, if other files instantiate generics from this --
22 -- unit, or you link this unit with other files to produce an executable, --
23 -- this unit does not by itself cause the resulting executable to be --
24 -- covered by the GNU General Public License. This exception does not --
25 -- however invalidate any other reasons why the executable file might be --
26 -- covered by the GNU Public License. --
27 -- --
28 -- GNAT was originally developed by the GNAT team at New York University. --
29 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 -- --
31 ------------------------------------------------------------------------------
33 with Atree; use Atree;
34 with Einfo; use Einfo;
35 with Sinfo; use Sinfo;
36 with Snames; use Snames;
37 with Stand; use Stand;
39 package body Sem_Aux is
41 ----------------------
42 -- Ancestor_Subtype --
43 ----------------------
45 function Ancestor_Subtype (Typ : Entity_Id) return Entity_Id is
46 begin
47 -- If this is first subtype, or is a base type, then there is no
48 -- ancestor subtype, so we return Empty to indicate this fact.
50 if Is_First_Subtype (Typ) or else Is_Base_Type (Typ) then
51 return Empty;
52 end if;
54 declare
55 D : constant Node_Id := Declaration_Node (Typ);
57 begin
58 -- If we have a subtype declaration, get the ancestor subtype
60 if Nkind (D) = N_Subtype_Declaration then
61 if Nkind (Subtype_Indication (D)) = N_Subtype_Indication then
62 return Entity (Subtype_Mark (Subtype_Indication (D)));
63 else
64 return Entity (Subtype_Indication (D));
65 end if;
67 -- If not, then no subtype indication is available
69 else
70 return Empty;
71 end if;
72 end;
73 end Ancestor_Subtype;
75 --------------------
76 -- Available_View --
77 --------------------
79 function Available_View (Typ : Entity_Id) return Entity_Id is
80 begin
81 if Is_Incomplete_Type (Typ)
82 and then Present (Non_Limited_View (Typ))
83 then
84 -- The non-limited view may itself be an incomplete type, in which
85 -- case get its full view.
87 return Get_Full_View (Non_Limited_View (Typ));
89 elsif Is_Class_Wide_Type (Typ)
90 and then Is_Incomplete_Type (Etype (Typ))
91 and then Present (Non_Limited_View (Etype (Typ)))
92 then
93 return Class_Wide_Type (Non_Limited_View (Etype (Typ)));
95 else
96 return Typ;
97 end if;
98 end Available_View;
100 --------------------
101 -- Constant_Value --
102 --------------------
104 function Constant_Value (Ent : Entity_Id) return Node_Id is
105 D : constant Node_Id := Declaration_Node (Ent);
106 Full_D : Node_Id;
108 begin
109 -- If we have no declaration node, then return no constant value. Not
110 -- clear how this can happen, but it does sometimes and this is the
111 -- safest approach.
113 if No (D) then
114 return Empty;
116 -- Normal case where a declaration node is present
118 elsif Nkind (D) = N_Object_Renaming_Declaration then
119 return Renamed_Object (Ent);
121 -- If this is a component declaration whose entity is a constant, it is
122 -- a prival within a protected function (and so has no constant value).
124 elsif Nkind (D) = N_Component_Declaration then
125 return Empty;
127 -- If there is an expression, return it
129 elsif Present (Expression (D)) then
130 return (Expression (D));
132 -- For a constant, see if we have a full view
134 elsif Ekind (Ent) = E_Constant
135 and then Present (Full_View (Ent))
136 then
137 Full_D := Parent (Full_View (Ent));
139 -- The full view may have been rewritten as an object renaming
141 if Nkind (Full_D) = N_Object_Renaming_Declaration then
142 return Name (Full_D);
143 else
144 return Expression (Full_D);
145 end if;
147 -- Otherwise we have no expression to return
149 else
150 return Empty;
151 end if;
152 end Constant_Value;
154 ----------------------------------------------
155 -- Effectively_Has_Constrained_Partial_View --
156 ----------------------------------------------
158 function Effectively_Has_Constrained_Partial_View
159 (Typ : Entity_Id;
160 Scop : Entity_Id) return Boolean
162 begin
163 return Has_Constrained_Partial_View (Typ)
164 or else (In_Generic_Body (Scop)
165 and then Is_Generic_Type (Base_Type (Typ))
166 and then Is_Private_Type (Base_Type (Typ))
167 and then not Is_Tagged_Type (Typ)
168 and then not (Is_Array_Type (Typ)
169 and then not Is_Constrained (Typ))
170 and then Has_Discriminants (Typ));
171 end Effectively_Has_Constrained_Partial_View;
173 -----------------------------
174 -- Enclosing_Dynamic_Scope --
175 -----------------------------
177 function Enclosing_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
178 S : Entity_Id;
180 begin
181 -- The following test is an error defense against some syntax errors
182 -- that can leave scopes very messed up.
184 if Ent = Standard_Standard then
185 return Ent;
186 end if;
188 -- Normal case, search enclosing scopes
190 -- Note: the test for Present (S) should not be required, it defends
191 -- against an ill-formed tree.
193 S := Scope (Ent);
194 loop
195 -- If we somehow got an empty value for Scope, the tree must be
196 -- malformed. Rather than blow up we return Standard in this case.
198 if No (S) then
199 return Standard_Standard;
201 -- Quit if we get to standard or a dynamic scope. We must also
202 -- handle enclosing scopes that have a full view; required to
203 -- locate enclosing scopes that are synchronized private types
204 -- whose full view is a task type.
206 elsif S = Standard_Standard
207 or else Is_Dynamic_Scope (S)
208 or else (Is_Private_Type (S)
209 and then Present (Full_View (S))
210 and then Is_Dynamic_Scope (Full_View (S)))
211 then
212 return S;
214 -- Otherwise keep climbing
216 else
217 S := Scope (S);
218 end if;
219 end loop;
220 end Enclosing_Dynamic_Scope;
222 ------------------------
223 -- First_Discriminant --
224 ------------------------
226 function First_Discriminant (Typ : Entity_Id) return Entity_Id is
227 Ent : Entity_Id;
229 begin
230 pragma Assert
231 (Has_Discriminants (Typ) or else Has_Unknown_Discriminants (Typ));
233 Ent := First_Entity (Typ);
235 -- The discriminants are not necessarily contiguous, because access
236 -- discriminants will generate itypes. They are not the first entities
237 -- either because the tag must be ahead of them.
239 if Chars (Ent) = Name_uTag then
240 Ent := Next_Entity (Ent);
241 end if;
243 -- Skip all hidden stored discriminants if any
245 while Present (Ent) loop
246 exit when Ekind (Ent) = E_Discriminant
247 and then not Is_Completely_Hidden (Ent);
249 Ent := Next_Entity (Ent);
250 end loop;
252 pragma Assert (Ekind (Ent) = E_Discriminant);
254 return Ent;
255 end First_Discriminant;
257 -------------------------------
258 -- First_Stored_Discriminant --
259 -------------------------------
261 function First_Stored_Discriminant (Typ : Entity_Id) return Entity_Id is
262 Ent : Entity_Id;
264 function Has_Completely_Hidden_Discriminant
265 (Typ : Entity_Id) return Boolean;
266 -- Scans the Discriminants to see whether any are Completely_Hidden
267 -- (the mechanism for describing non-specified stored discriminants)
269 ----------------------------------------
270 -- Has_Completely_Hidden_Discriminant --
271 ----------------------------------------
273 function Has_Completely_Hidden_Discriminant
274 (Typ : Entity_Id) return Boolean
276 Ent : Entity_Id;
278 begin
279 pragma Assert (Ekind (Typ) = E_Discriminant);
281 Ent := Typ;
282 while Present (Ent) and then Ekind (Ent) = E_Discriminant loop
283 if Is_Completely_Hidden (Ent) then
284 return True;
285 end if;
287 Ent := Next_Entity (Ent);
288 end loop;
290 return False;
291 end Has_Completely_Hidden_Discriminant;
293 -- Start of processing for First_Stored_Discriminant
295 begin
296 pragma Assert
297 (Has_Discriminants (Typ)
298 or else Has_Unknown_Discriminants (Typ));
300 Ent := First_Entity (Typ);
302 if Chars (Ent) = Name_uTag then
303 Ent := Next_Entity (Ent);
304 end if;
306 if Has_Completely_Hidden_Discriminant (Ent) then
307 while Present (Ent) loop
308 exit when Is_Completely_Hidden (Ent);
309 Ent := Next_Entity (Ent);
310 end loop;
311 end if;
313 pragma Assert (Ekind (Ent) = E_Discriminant);
315 return Ent;
316 end First_Stored_Discriminant;
318 -------------------
319 -- First_Subtype --
320 -------------------
322 function First_Subtype (Typ : Entity_Id) return Entity_Id is
323 B : constant Entity_Id := Base_Type (Typ);
324 F : constant Node_Id := Freeze_Node (B);
325 Ent : Entity_Id;
327 begin
328 -- If the base type has no freeze node, it is a type in Standard, and
329 -- always acts as its own first subtype, except where it is one of the
330 -- predefined integer types. If the type is formal, it is also a first
331 -- subtype, and its base type has no freeze node. On the other hand, a
332 -- subtype of a generic formal is not its own first subtype. Its base
333 -- type, if anonymous, is attached to the formal type decl. from which
334 -- the first subtype is obtained.
336 if No (F) then
337 if B = Base_Type (Standard_Integer) then
338 return Standard_Integer;
340 elsif B = Base_Type (Standard_Long_Integer) then
341 return Standard_Long_Integer;
343 elsif B = Base_Type (Standard_Short_Short_Integer) then
344 return Standard_Short_Short_Integer;
346 elsif B = Base_Type (Standard_Short_Integer) then
347 return Standard_Short_Integer;
349 elsif B = Base_Type (Standard_Long_Long_Integer) then
350 return Standard_Long_Long_Integer;
352 elsif Is_Generic_Type (Typ) then
353 if Present (Parent (B)) then
354 return Defining_Identifier (Parent (B));
355 else
356 return Defining_Identifier (Associated_Node_For_Itype (B));
357 end if;
359 else
360 return B;
361 end if;
363 -- Otherwise we check the freeze node, if it has a First_Subtype_Link
364 -- then we use that link, otherwise (happens with some Itypes), we use
365 -- the base type itself.
367 else
368 Ent := First_Subtype_Link (F);
370 if Present (Ent) then
371 return Ent;
372 else
373 return B;
374 end if;
375 end if;
376 end First_Subtype;
378 -------------------------
379 -- First_Tag_Component --
380 -------------------------
382 function First_Tag_Component (Typ : Entity_Id) return Entity_Id is
383 Comp : Entity_Id;
384 Ctyp : Entity_Id;
386 begin
387 Ctyp := Typ;
388 pragma Assert (Is_Tagged_Type (Ctyp));
390 if Is_Class_Wide_Type (Ctyp) then
391 Ctyp := Root_Type (Ctyp);
392 end if;
394 if Is_Private_Type (Ctyp) then
395 Ctyp := Underlying_Type (Ctyp);
397 -- If the underlying type is missing then the source program has
398 -- errors and there is nothing else to do (the full-type declaration
399 -- associated with the private type declaration is missing).
401 if No (Ctyp) then
402 return Empty;
403 end if;
404 end if;
406 Comp := First_Entity (Ctyp);
407 while Present (Comp) loop
408 if Is_Tag (Comp) then
409 return Comp;
410 end if;
412 Comp := Next_Entity (Comp);
413 end loop;
415 -- No tag component found
417 return Empty;
418 end First_Tag_Component;
420 ------------------
421 -- Get_Rep_Item --
422 ------------------
424 function Get_Rep_Item
425 (E : Entity_Id;
426 Nam : Name_Id;
427 Check_Parents : Boolean := True) return Node_Id
429 N : Node_Id;
431 begin
432 N := First_Rep_Item (E);
433 while Present (N) loop
434 if Nkind (N) = N_Pragma
435 and then
436 (Pragma_Name (N) = Nam
437 or else (Nam = Name_Priority
438 and then Pragma_Name (N) = Name_Interrupt_Priority))
439 then
440 if Check_Parents then
441 return N;
443 -- If Check_Parents is False, return N if the pragma doesn't
444 -- appear in the Rep_Item chain of the parent.
446 else
447 declare
448 Par : constant Entity_Id := Nearest_Ancestor (E);
449 -- This node represents the parent type of type E (if any)
451 begin
452 if No (Par) then
453 return N;
455 elsif not Present_In_Rep_Item (Par, N) then
456 return N;
457 end if;
458 end;
459 end if;
461 elsif Nkind (N) = N_Attribute_Definition_Clause
462 and then
463 (Chars (N) = Nam
464 or else (Nam = Name_Priority
465 and then Chars (N) = Name_Interrupt_Priority))
466 then
467 if Check_Parents or else Entity (N) = E then
468 return N;
469 end if;
471 elsif Nkind (N) = N_Aspect_Specification
472 and then
473 (Chars (Identifier (N)) = Nam
474 or else (Nam = Name_Priority
475 and then Chars (Identifier (N)) =
476 Name_Interrupt_Priority))
477 then
478 if Check_Parents then
479 return N;
481 elsif Entity (N) = E then
482 return N;
483 end if;
484 end if;
486 Next_Rep_Item (N);
487 end loop;
489 return Empty;
490 end Get_Rep_Item;
492 function Get_Rep_Item
493 (E : Entity_Id;
494 Nam1 : Name_Id;
495 Nam2 : Name_Id;
496 Check_Parents : Boolean := True) return Node_Id
498 Nam1_Item : constant Node_Id := Get_Rep_Item (E, Nam1, Check_Parents);
499 Nam2_Item : constant Node_Id := Get_Rep_Item (E, Nam2, Check_Parents);
501 N : Node_Id;
503 begin
504 -- Check both Nam1_Item and Nam2_Item are present
506 if No (Nam1_Item) then
507 return Nam2_Item;
508 elsif No (Nam2_Item) then
509 return Nam1_Item;
510 end if;
512 -- Return the first node encountered in the list
514 N := First_Rep_Item (E);
515 while Present (N) loop
516 if N = Nam1_Item or else N = Nam2_Item then
517 return N;
518 end if;
520 Next_Rep_Item (N);
521 end loop;
523 return Empty;
524 end Get_Rep_Item;
526 --------------------
527 -- Get_Rep_Pragma --
528 --------------------
530 function Get_Rep_Pragma
531 (E : Entity_Id;
532 Nam : Name_Id;
533 Check_Parents : Boolean := True) return Node_Id
535 N : Node_Id;
537 begin
538 N := Get_Rep_Item (E, Nam, Check_Parents);
540 if Present (N) and then Nkind (N) = N_Pragma then
541 return N;
542 end if;
544 return Empty;
545 end Get_Rep_Pragma;
547 function Get_Rep_Pragma
548 (E : Entity_Id;
549 Nam1 : Name_Id;
550 Nam2 : Name_Id;
551 Check_Parents : Boolean := True) return Node_Id
553 Nam1_Item : constant Node_Id := Get_Rep_Pragma (E, Nam1, Check_Parents);
554 Nam2_Item : constant Node_Id := Get_Rep_Pragma (E, Nam2, Check_Parents);
556 N : Node_Id;
558 begin
559 -- Check both Nam1_Item and Nam2_Item are present
561 if No (Nam1_Item) then
562 return Nam2_Item;
563 elsif No (Nam2_Item) then
564 return Nam1_Item;
565 end if;
567 -- Return the first node encountered in the list
569 N := First_Rep_Item (E);
570 while Present (N) loop
571 if N = Nam1_Item or else N = Nam2_Item then
572 return N;
573 end if;
575 Next_Rep_Item (N);
576 end loop;
578 return Empty;
579 end Get_Rep_Pragma;
581 ------------------
582 -- Has_Rep_Item --
583 ------------------
585 function Has_Rep_Item
586 (E : Entity_Id;
587 Nam : Name_Id;
588 Check_Parents : Boolean := True) return Boolean
590 begin
591 return Present (Get_Rep_Item (E, Nam, Check_Parents));
592 end Has_Rep_Item;
594 function Has_Rep_Item
595 (E : Entity_Id;
596 Nam1 : Name_Id;
597 Nam2 : Name_Id;
598 Check_Parents : Boolean := True) return Boolean
600 begin
601 return Present (Get_Rep_Item (E, Nam1, Nam2, Check_Parents));
602 end Has_Rep_Item;
604 --------------------
605 -- Has_Rep_Pragma --
606 --------------------
608 function Has_Rep_Pragma
609 (E : Entity_Id;
610 Nam : Name_Id;
611 Check_Parents : Boolean := True) return Boolean
613 begin
614 return Present (Get_Rep_Pragma (E, Nam, Check_Parents));
615 end Has_Rep_Pragma;
617 function Has_Rep_Pragma
618 (E : Entity_Id;
619 Nam1 : Name_Id;
620 Nam2 : Name_Id;
621 Check_Parents : Boolean := True) return Boolean
623 begin
624 return Present (Get_Rep_Pragma (E, Nam1, Nam2, Check_Parents));
625 end Has_Rep_Pragma;
627 -------------------------------
628 -- Initialization_Suppressed --
629 -------------------------------
631 function Initialization_Suppressed (Typ : Entity_Id) return Boolean is
632 begin
633 return Suppress_Initialization (Typ)
634 or else Suppress_Initialization (Base_Type (Typ));
635 end Initialization_Suppressed;
637 ----------------
638 -- Initialize --
639 ----------------
641 procedure Initialize is
642 begin
643 Obsolescent_Warnings.Init;
644 end Initialize;
646 ---------------------
647 -- In_Generic_Body --
648 ---------------------
650 function In_Generic_Body (Id : Entity_Id) return Boolean is
651 S : Entity_Id;
653 begin
654 -- Climb scopes looking for generic body
656 S := Id;
657 while Present (S) and then S /= Standard_Standard loop
659 -- Generic package body
661 if Ekind (S) = E_Generic_Package
662 and then In_Package_Body (S)
663 then
664 return True;
666 -- Generic subprogram body
668 elsif Is_Subprogram (S)
669 and then Nkind (Unit_Declaration_Node (S))
670 = N_Generic_Subprogram_Declaration
671 then
672 return True;
673 end if;
675 S := Scope (S);
676 end loop;
678 -- False if top of scope stack without finding a generic body
680 return False;
681 end In_Generic_Body;
683 ---------------------
684 -- Is_By_Copy_Type --
685 ---------------------
687 function Is_By_Copy_Type (Ent : Entity_Id) return Boolean is
688 begin
689 -- If Id is a private type whose full declaration has not been seen,
690 -- we assume for now that it is not a By_Copy type. Clearly this
691 -- attribute should not be used before the type is frozen, but it is
692 -- needed to build the associated record of a protected type. Another
693 -- place where some lookahead for a full view is needed ???
695 return
696 Is_Elementary_Type (Ent)
697 or else (Is_Private_Type (Ent)
698 and then Present (Underlying_Type (Ent))
699 and then Is_Elementary_Type (Underlying_Type (Ent)));
700 end Is_By_Copy_Type;
702 --------------------------
703 -- Is_By_Reference_Type --
704 --------------------------
706 function Is_By_Reference_Type (Ent : Entity_Id) return Boolean is
707 Btype : constant Entity_Id := Base_Type (Ent);
709 begin
710 if Error_Posted (Ent) or else Error_Posted (Btype) then
711 return False;
713 elsif Is_Private_Type (Btype) then
714 declare
715 Utyp : constant Entity_Id := Underlying_Type (Btype);
716 begin
717 if No (Utyp) then
718 return False;
719 else
720 return Is_By_Reference_Type (Utyp);
721 end if;
722 end;
724 elsif Is_Incomplete_Type (Btype) then
725 declare
726 Ftyp : constant Entity_Id := Full_View (Btype);
727 begin
728 if No (Ftyp) then
729 return False;
730 else
731 return Is_By_Reference_Type (Ftyp);
732 end if;
733 end;
735 elsif Is_Concurrent_Type (Btype) then
736 return True;
738 elsif Is_Record_Type (Btype) then
739 if Is_Limited_Record (Btype)
740 or else Is_Tagged_Type (Btype)
741 or else Is_Volatile (Btype)
742 then
743 return True;
745 else
746 declare
747 C : Entity_Id;
749 begin
750 C := First_Component (Btype);
751 while Present (C) loop
752 if Is_By_Reference_Type (Etype (C))
753 or else Is_Volatile (Etype (C))
754 then
755 return True;
756 end if;
758 C := Next_Component (C);
759 end loop;
760 end;
762 return False;
763 end if;
765 elsif Is_Array_Type (Btype) then
766 return
767 Is_Volatile (Btype)
768 or else Is_By_Reference_Type (Component_Type (Btype))
769 or else Is_Volatile (Component_Type (Btype))
770 or else Has_Volatile_Components (Btype);
772 else
773 return False;
774 end if;
775 end Is_By_Reference_Type;
777 ---------------------
778 -- Is_Derived_Type --
779 ---------------------
781 function Is_Derived_Type (Ent : E) return B is
782 Par : Node_Id;
784 begin
785 if Is_Type (Ent)
786 and then Base_Type (Ent) /= Root_Type (Ent)
787 and then not Is_Class_Wide_Type (Ent)
788 then
789 if not Is_Numeric_Type (Root_Type (Ent)) then
790 return True;
792 else
793 Par := Parent (First_Subtype (Ent));
795 return Present (Par)
796 and then Nkind (Par) = N_Full_Type_Declaration
797 and then Nkind (Type_Definition (Par)) =
798 N_Derived_Type_Definition;
799 end if;
801 else
802 return False;
803 end if;
804 end Is_Derived_Type;
806 -----------------------
807 -- Is_Generic_Formal --
808 -----------------------
810 function Is_Generic_Formal (E : Entity_Id) return Boolean is
811 Kind : Node_Kind;
812 begin
813 if No (E) then
814 return False;
815 else
816 Kind := Nkind (Parent (E));
817 return
818 Nkind_In (Kind, N_Formal_Object_Declaration,
819 N_Formal_Package_Declaration,
820 N_Formal_Type_Declaration)
821 or else Is_Formal_Subprogram (E);
822 end if;
823 end Is_Generic_Formal;
825 ---------------------------
826 -- Is_Indefinite_Subtype --
827 ---------------------------
829 function Is_Indefinite_Subtype (Ent : Entity_Id) return Boolean is
830 K : constant Entity_Kind := Ekind (Ent);
832 begin
833 if Is_Constrained (Ent) then
834 return False;
836 elsif K in Array_Kind
837 or else K in Class_Wide_Kind
838 or else Has_Unknown_Discriminants (Ent)
839 then
840 return True;
842 -- Known discriminants: indefinite if there are no default values
844 elsif K in Record_Kind
845 or else Is_Incomplete_Or_Private_Type (Ent)
846 or else Is_Concurrent_Type (Ent)
847 then
848 return (Has_Discriminants (Ent)
849 and then
850 No (Discriminant_Default_Value (First_Discriminant (Ent))));
852 else
853 return False;
854 end if;
855 end Is_Indefinite_Subtype;
857 -------------------------------
858 -- Is_Immutably_Limited_Type --
859 -------------------------------
861 function Is_Immutably_Limited_Type (Ent : Entity_Id) return Boolean is
862 Btype : constant Entity_Id := Available_View (Base_Type (Ent));
864 begin
865 if Is_Limited_Record (Btype) then
866 return True;
868 elsif Ekind (Btype) = E_Limited_Private_Type
869 and then Nkind (Parent (Btype)) = N_Formal_Type_Declaration
870 then
871 return not In_Package_Body (Scope ((Btype)));
873 elsif Is_Private_Type (Btype) then
875 -- AI05-0063: A type derived from a limited private formal type is
876 -- not immutably limited in a generic body.
878 if Is_Derived_Type (Btype)
879 and then Is_Generic_Type (Etype (Btype))
880 then
881 if not Is_Limited_Type (Etype (Btype)) then
882 return False;
884 -- A descendant of a limited formal type is not immutably limited
885 -- in the generic body, or in the body of a generic child.
887 elsif Ekind (Scope (Etype (Btype))) = E_Generic_Package then
888 return not In_Package_Body (Scope (Btype));
890 else
891 return False;
892 end if;
894 else
895 declare
896 Utyp : constant Entity_Id := Underlying_Type (Btype);
897 begin
898 if No (Utyp) then
899 return False;
900 else
901 return Is_Immutably_Limited_Type (Utyp);
902 end if;
903 end;
904 end if;
906 elsif Is_Concurrent_Type (Btype) then
907 return True;
909 elsif Is_Record_Type (Btype) then
911 -- Note that we return True for all limited interfaces, even though
912 -- (unsynchronized) limited interfaces can have descendants that are
913 -- nonlimited, because this is a predicate on the type itself, and
914 -- things like functions with limited interface results need to be
915 -- handled as build in place even though they might return objects
916 -- of a type that is not inherently limited.
918 if Is_Class_Wide_Type (Btype) then
919 return Is_Immutably_Limited_Type (Root_Type (Btype));
921 else
922 declare
923 C : Entity_Id;
925 begin
926 C := First_Component (Btype);
927 while Present (C) loop
929 -- Don't consider components with interface types (which can
930 -- only occur in the case of a _parent component anyway).
931 -- They don't have any components, plus it would cause this
932 -- function to return true for nonlimited types derived from
933 -- limited interfaces.
935 if not Is_Interface (Etype (C))
936 and then Is_Immutably_Limited_Type (Etype (C))
937 then
938 return True;
939 end if;
941 C := Next_Component (C);
942 end loop;
943 end;
945 return False;
946 end if;
948 elsif Is_Array_Type (Btype) then
949 return Is_Immutably_Limited_Type (Component_Type (Btype));
951 else
952 return False;
953 end if;
954 end Is_Immutably_Limited_Type;
956 ---------------------
957 -- Is_Limited_Type --
958 ---------------------
960 function Is_Limited_Type (Ent : Entity_Id) return Boolean is
961 Btype : constant E := Base_Type (Ent);
962 Rtype : constant E := Root_Type (Btype);
964 begin
965 if not Is_Type (Ent) then
966 return False;
968 elsif Ekind (Btype) = E_Limited_Private_Type
969 or else Is_Limited_Composite (Btype)
970 then
971 return True;
973 elsif Is_Concurrent_Type (Btype) then
974 return True;
976 -- The Is_Limited_Record flag normally indicates that the type is
977 -- limited. The exception is that a type does not inherit limitedness
978 -- from its interface ancestor. So the type may be derived from a
979 -- limited interface, but is not limited.
981 elsif Is_Limited_Record (Ent)
982 and then not Is_Interface (Ent)
983 then
984 return True;
986 -- Otherwise we will look around to see if there is some other reason
987 -- for it to be limited, except that if an error was posted on the
988 -- entity, then just assume it is non-limited, because it can cause
989 -- trouble to recurse into a murky erroneous entity!
991 elsif Error_Posted (Ent) then
992 return False;
994 elsif Is_Record_Type (Btype) then
996 if Is_Limited_Interface (Ent) then
997 return True;
999 -- AI-419: limitedness is not inherited from a limited interface
1001 elsif Is_Limited_Record (Rtype) then
1002 return not Is_Interface (Rtype)
1003 or else Is_Protected_Interface (Rtype)
1004 or else Is_Synchronized_Interface (Rtype)
1005 or else Is_Task_Interface (Rtype);
1007 elsif Is_Class_Wide_Type (Btype) then
1008 return Is_Limited_Type (Rtype);
1010 else
1011 declare
1012 C : E;
1014 begin
1015 C := First_Component (Btype);
1016 while Present (C) loop
1017 if Is_Limited_Type (Etype (C)) then
1018 return True;
1019 end if;
1021 C := Next_Component (C);
1022 end loop;
1023 end;
1025 return False;
1026 end if;
1028 elsif Is_Array_Type (Btype) then
1029 return Is_Limited_Type (Component_Type (Btype));
1031 else
1032 return False;
1033 end if;
1034 end Is_Limited_Type;
1036 ----------------------
1037 -- Nearest_Ancestor --
1038 ----------------------
1040 function Nearest_Ancestor (Typ : Entity_Id) return Entity_Id is
1041 D : constant Node_Id := Declaration_Node (Typ);
1043 begin
1044 -- If we have a subtype declaration, get the ancestor subtype
1046 if Nkind (D) = N_Subtype_Declaration then
1047 if Nkind (Subtype_Indication (D)) = N_Subtype_Indication then
1048 return Entity (Subtype_Mark (Subtype_Indication (D)));
1049 else
1050 return Entity (Subtype_Indication (D));
1051 end if;
1053 -- If derived type declaration, find who we are derived from
1055 elsif Nkind (D) = N_Full_Type_Declaration
1056 and then Nkind (Type_Definition (D)) = N_Derived_Type_Definition
1057 then
1058 declare
1059 DTD : constant Entity_Id := Type_Definition (D);
1060 SI : constant Entity_Id := Subtype_Indication (DTD);
1061 begin
1062 if Is_Entity_Name (SI) then
1063 return Entity (SI);
1064 else
1065 return Entity (Subtype_Mark (SI));
1066 end if;
1067 end;
1069 -- If derived type and private type, get the full view to find who we
1070 -- are derived from.
1072 elsif Is_Derived_Type (Typ)
1073 and then Is_Private_Type (Typ)
1074 and then Present (Full_View (Typ))
1075 then
1076 return Nearest_Ancestor (Full_View (Typ));
1078 -- Otherwise, nothing useful to return, return Empty
1080 else
1081 return Empty;
1082 end if;
1083 end Nearest_Ancestor;
1085 ---------------------------
1086 -- Nearest_Dynamic_Scope --
1087 ---------------------------
1089 function Nearest_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
1090 begin
1091 if Is_Dynamic_Scope (Ent) then
1092 return Ent;
1093 else
1094 return Enclosing_Dynamic_Scope (Ent);
1095 end if;
1096 end Nearest_Dynamic_Scope;
1098 ------------------------
1099 -- Next_Tag_Component --
1100 ------------------------
1102 function Next_Tag_Component (Tag : Entity_Id) return Entity_Id is
1103 Comp : Entity_Id;
1105 begin
1106 pragma Assert (Is_Tag (Tag));
1108 -- Loop to look for next tag component
1110 Comp := Next_Entity (Tag);
1111 while Present (Comp) loop
1112 if Is_Tag (Comp) then
1113 pragma Assert (Chars (Comp) /= Name_uTag);
1114 return Comp;
1115 end if;
1117 Comp := Next_Entity (Comp);
1118 end loop;
1120 -- No tag component found
1122 return Empty;
1123 end Next_Tag_Component;
1125 --------------------------
1126 -- Number_Discriminants --
1127 --------------------------
1129 function Number_Discriminants (Typ : Entity_Id) return Pos is
1130 N : Int;
1131 Discr : Entity_Id;
1133 begin
1134 N := 0;
1135 Discr := First_Discriminant (Typ);
1136 while Present (Discr) loop
1137 N := N + 1;
1138 Discr := Next_Discriminant (Discr);
1139 end loop;
1141 return N;
1142 end Number_Discriminants;
1144 ---------------
1145 -- Tree_Read --
1146 ---------------
1148 procedure Tree_Read is
1149 begin
1150 Obsolescent_Warnings.Tree_Read;
1151 end Tree_Read;
1153 ----------------
1154 -- Tree_Write --
1155 ----------------
1157 procedure Tree_Write is
1158 begin
1159 Obsolescent_Warnings.Tree_Write;
1160 end Tree_Write;
1162 --------------------
1163 -- Ultimate_Alias --
1164 --------------------
1166 function Ultimate_Alias (Prim : Entity_Id) return Entity_Id is
1167 E : Entity_Id := Prim;
1169 begin
1170 while Present (Alias (E)) loop
1171 pragma Assert (Alias (E) /= E);
1172 E := Alias (E);
1173 end loop;
1175 return E;
1176 end Ultimate_Alias;
1178 --------------------------
1179 -- Unit_Declaration_Node --
1180 --------------------------
1182 function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
1183 N : Node_Id := Parent (Unit_Id);
1185 begin
1186 -- Predefined operators do not have a full function declaration
1188 if Ekind (Unit_Id) = E_Operator then
1189 return N;
1190 end if;
1192 -- Isn't there some better way to express the following ???
1194 while Nkind (N) /= N_Abstract_Subprogram_Declaration
1195 and then Nkind (N) /= N_Formal_Package_Declaration
1196 and then Nkind (N) /= N_Function_Instantiation
1197 and then Nkind (N) /= N_Generic_Package_Declaration
1198 and then Nkind (N) /= N_Generic_Subprogram_Declaration
1199 and then Nkind (N) /= N_Package_Declaration
1200 and then Nkind (N) /= N_Package_Body
1201 and then Nkind (N) /= N_Package_Instantiation
1202 and then Nkind (N) /= N_Package_Renaming_Declaration
1203 and then Nkind (N) /= N_Procedure_Instantiation
1204 and then Nkind (N) /= N_Protected_Body
1205 and then Nkind (N) /= N_Subprogram_Declaration
1206 and then Nkind (N) /= N_Subprogram_Body
1207 and then Nkind (N) /= N_Subprogram_Body_Stub
1208 and then Nkind (N) /= N_Subprogram_Renaming_Declaration
1209 and then Nkind (N) /= N_Task_Body
1210 and then Nkind (N) /= N_Task_Type_Declaration
1211 and then Nkind (N) not in N_Formal_Subprogram_Declaration
1212 and then Nkind (N) not in N_Generic_Renaming_Declaration
1213 loop
1214 N := Parent (N);
1216 -- We don't use Assert here, because that causes an infinite loop
1217 -- when assertions are turned off. Better to crash.
1219 if No (N) then
1220 raise Program_Error;
1221 end if;
1222 end loop;
1224 return N;
1225 end Unit_Declaration_Node;
1227 end Sem_Aux;