1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 -- 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. --
28 -- GNAT was originally developed by the GNAT team at New York University. --
29 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 ------------------------------------------------------------------------------
33 with Atree; use Atree;
34 with Einfo; use Einfo;
35 with Namet; use Namet;
36 with Sinfo; use Sinfo;
37 with Snames; use Snames;
38 with Stand; use Stand;
39 with Targparm; use Targparm;
41 package body Sem_Aux is
43 ----------------------
44 -- Ancestor_Subtype --
45 ----------------------
47 function Ancestor_Subtype (Typ : Entity_Id) return Entity_Id is
49 -- If this is first subtype, or is a base type, then there is no
50 -- ancestor subtype, so we return Empty to indicate this fact.
52 if Is_First_Subtype (Typ) or else Is_Base_Type (Typ) then
57 D : constant Node_Id := Declaration_Node (Typ);
60 -- If we have a subtype declaration, get the ancestor subtype
62 if Nkind (D) = N_Subtype_Declaration then
63 if Nkind (Subtype_Indication (D)) = N_Subtype_Indication then
64 return Entity (Subtype_Mark (Subtype_Indication (D)));
66 return Entity (Subtype_Indication (D));
69 -- If not, then no subtype indication is available
81 function Available_View (Typ : Entity_Id) return Entity_Id is
83 if Is_Incomplete_Type (Typ)
84 and then Present (Non_Limited_View (Typ))
86 -- The non-limited view may itself be an incomplete type, in which
87 -- case get its full view.
89 return Get_Full_View (Non_Limited_View (Typ));
91 elsif Is_Class_Wide_Type (Typ)
92 and then Is_Incomplete_Type (Etype (Typ))
93 and then Present (Non_Limited_View (Etype (Typ)))
95 return Class_Wide_Type (Non_Limited_View (Etype (Typ)));
106 function Constant_Value (Ent : Entity_Id) return Node_Id is
107 D : constant Node_Id := Declaration_Node (Ent);
111 -- If we have no declaration node, then return no constant value. Not
112 -- clear how this can happen, but it does sometimes and this is the
118 -- Normal case where a declaration node is present
120 elsif Nkind (D) = N_Object_Renaming_Declaration then
121 return Renamed_Object (Ent);
123 -- If this is a component declaration whose entity is a constant, it is
124 -- a prival within a protected function (and so has no constant value).
126 elsif Nkind (D) = N_Component_Declaration then
129 -- If there is an expression, return it
131 elsif Present (Expression (D)) then
132 return (Expression (D));
134 -- For a constant, see if we have a full view
136 elsif Ekind (Ent) = E_Constant
137 and then Present (Full_View (Ent))
139 Full_D := Parent (Full_View (Ent));
141 -- The full view may have been rewritten as an object renaming
143 if Nkind (Full_D) = N_Object_Renaming_Declaration then
144 return Name (Full_D);
146 return Expression (Full_D);
149 -- Otherwise we have no expression to return
156 -----------------------------
157 -- Enclosing_Dynamic_Scope --
158 -----------------------------
160 function Enclosing_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
164 -- The following test is an error defense against some syntax errors
165 -- that can leave scopes very messed up.
167 if Ent = Standard_Standard then
171 -- Normal case, search enclosing scopes
173 -- Note: the test for Present (S) should not be required, it defends
174 -- against an ill-formed tree.
178 -- If we somehow got an empty value for Scope, the tree must be
179 -- malformed. Rather than blow up we return Standard in this case.
182 return Standard_Standard;
184 -- Quit if we get to standard or a dynamic scope. We must also
185 -- handle enclosing scopes that have a full view; required to
186 -- locate enclosing scopes that are synchronized private types
187 -- whose full view is a task type.
189 elsif S = Standard_Standard
190 or else Is_Dynamic_Scope (S)
191 or else (Is_Private_Type (S)
192 and then Present (Full_View (S))
193 and then Is_Dynamic_Scope (Full_View (S)))
197 -- Otherwise keep climbing
203 end Enclosing_Dynamic_Scope;
205 ------------------------
206 -- First_Discriminant --
207 ------------------------
209 function First_Discriminant (Typ : Entity_Id) return Entity_Id is
214 (Has_Discriminants (Typ) or else Has_Unknown_Discriminants (Typ));
216 Ent := First_Entity (Typ);
218 -- The discriminants are not necessarily contiguous, because access
219 -- discriminants will generate itypes. They are not the first entities
220 -- either, because tag and controller record must be ahead of them.
222 if Chars (Ent) = Name_uTag then
223 Ent := Next_Entity (Ent);
226 if Chars (Ent) = Name_uController then
227 Ent := Next_Entity (Ent);
230 -- Skip all hidden stored discriminants if any
232 while Present (Ent) loop
233 exit when Ekind (Ent) = E_Discriminant
234 and then not Is_Completely_Hidden (Ent);
236 Ent := Next_Entity (Ent);
239 pragma Assert (Ekind (Ent) = E_Discriminant);
242 end First_Discriminant;
244 -------------------------------
245 -- First_Stored_Discriminant --
246 -------------------------------
248 function First_Stored_Discriminant (Typ : Entity_Id) return Entity_Id is
251 function Has_Completely_Hidden_Discriminant
252 (Typ : Entity_Id) return Boolean;
253 -- Scans the Discriminants to see whether any are Completely_Hidden
254 -- (the mechanism for describing non-specified stored discriminants)
256 ----------------------------------------
257 -- Has_Completely_Hidden_Discriminant --
258 ----------------------------------------
260 function Has_Completely_Hidden_Discriminant
261 (Typ : Entity_Id) return Boolean
266 pragma Assert (Ekind (Typ) = E_Discriminant);
269 while Present (Ent) and then Ekind (Ent) = E_Discriminant loop
270 if Is_Completely_Hidden (Ent) then
274 Ent := Next_Entity (Ent);
278 end Has_Completely_Hidden_Discriminant;
280 -- Start of processing for First_Stored_Discriminant
284 (Has_Discriminants (Typ)
285 or else Has_Unknown_Discriminants (Typ));
287 Ent := First_Entity (Typ);
289 if Chars (Ent) = Name_uTag then
290 Ent := Next_Entity (Ent);
293 if Chars (Ent) = Name_uController then
294 Ent := Next_Entity (Ent);
297 if Has_Completely_Hidden_Discriminant (Ent) then
299 while Present (Ent) loop
300 exit when Is_Completely_Hidden (Ent);
301 Ent := Next_Entity (Ent);
306 pragma Assert (Ekind (Ent) = E_Discriminant);
309 end First_Stored_Discriminant;
315 function First_Subtype (Typ : Entity_Id) return Entity_Id is
316 B : constant Entity_Id := Base_Type (Typ);
317 F : constant Node_Id := Freeze_Node (B);
321 -- If the base type has no freeze node, it is a type in Standard, and
322 -- always acts as its own first subtype, except where it is one of the
323 -- predefined integer types. If the type is formal, it is also a first
324 -- subtype, and its base type has no freeze node. On the other hand, a
325 -- subtype of a generic formal is not its own first subtype. Its base
326 -- type, if anonymous, is attached to the formal type decl. from which
327 -- the first subtype is obtained.
330 if B = Base_Type (Standard_Integer) then
331 return Standard_Integer;
333 elsif B = Base_Type (Standard_Long_Integer) then
334 return Standard_Long_Integer;
336 elsif B = Base_Type (Standard_Short_Short_Integer) then
337 return Standard_Short_Short_Integer;
339 elsif B = Base_Type (Standard_Short_Integer) then
340 return Standard_Short_Integer;
342 elsif B = Base_Type (Standard_Long_Long_Integer) then
343 return Standard_Long_Long_Integer;
345 elsif Is_Generic_Type (Typ) then
346 if Present (Parent (B)) then
347 return Defining_Identifier (Parent (B));
349 return Defining_Identifier (Associated_Node_For_Itype (B));
356 -- Otherwise we check the freeze node, if it has a First_Subtype_Link
357 -- then we use that link, otherwise (happens with some Itypes), we use
358 -- the base type itself.
361 Ent := First_Subtype_Link (F);
363 if Present (Ent) then
371 -------------------------
372 -- First_Tag_Component --
373 -------------------------
375 function First_Tag_Component (Typ : Entity_Id) return Entity_Id is
381 pragma Assert (Is_Tagged_Type (Ctyp));
383 if Is_Class_Wide_Type (Ctyp) then
384 Ctyp := Root_Type (Ctyp);
387 if Is_Private_Type (Ctyp) then
388 Ctyp := Underlying_Type (Ctyp);
390 -- If the underlying type is missing then the source program has
391 -- errors and there is nothing else to do (the full-type declaration
392 -- associated with the private type declaration is missing).
399 Comp := First_Entity (Ctyp);
400 while Present (Comp) loop
401 if Is_Tag (Comp) then
405 Comp := Next_Entity (Comp);
408 -- No tag component found
411 end First_Tag_Component;
413 -------------------------------
414 -- Initialization_Suppressed --
415 -------------------------------
417 function Initialization_Suppressed (Typ : Entity_Id) return Boolean is
419 return Suppress_Initialization (Typ)
420 or else Suppress_Initialization (Base_Type (Typ));
421 end Initialization_Suppressed;
427 procedure Initialize is
429 Obsolescent_Warnings.Init;
432 ---------------------
433 -- Is_By_Copy_Type --
434 ---------------------
436 function Is_By_Copy_Type (Ent : Entity_Id) return Boolean is
438 -- If Id is a private type whose full declaration has not been seen,
439 -- we assume for now that it is not a By_Copy type. Clearly this
440 -- attribute should not be used before the type is frozen, but it is
441 -- needed to build the associated record of a protected type. Another
442 -- place where some lookahead for a full view is needed ???
445 Is_Elementary_Type (Ent)
446 or else (Is_Private_Type (Ent)
447 and then Present (Underlying_Type (Ent))
448 and then Is_Elementary_Type (Underlying_Type (Ent)));
451 --------------------------
452 -- Is_By_Reference_Type --
453 --------------------------
455 function Is_By_Reference_Type (Ent : Entity_Id) return Boolean is
456 Btype : constant Entity_Id := Base_Type (Ent);
459 if Error_Posted (Ent)
460 or else Error_Posted (Btype)
464 elsif Is_Private_Type (Btype) then
466 Utyp : constant Entity_Id := Underlying_Type (Btype);
471 return Is_By_Reference_Type (Utyp);
475 elsif Is_Incomplete_Type (Btype) then
477 Ftyp : constant Entity_Id := Full_View (Btype);
482 return Is_By_Reference_Type (Ftyp);
486 elsif Is_Concurrent_Type (Btype) then
489 elsif Is_Record_Type (Btype) then
490 if Is_Limited_Record (Btype)
491 or else Is_Tagged_Type (Btype)
492 or else Is_Volatile (Btype)
501 C := First_Component (Btype);
502 while Present (C) loop
503 if Is_By_Reference_Type (Etype (C))
504 or else Is_Volatile (Etype (C))
509 C := Next_Component (C);
516 elsif Is_Array_Type (Btype) then
519 or else Is_By_Reference_Type (Component_Type (Btype))
520 or else Is_Volatile (Component_Type (Btype))
521 or else Has_Volatile_Components (Btype);
526 end Is_By_Reference_Type;
528 ---------------------
529 -- Is_Derived_Type --
530 ---------------------
532 function Is_Derived_Type (Ent : E) return B is
537 and then Base_Type (Ent) /= Root_Type (Ent)
538 and then not Is_Class_Wide_Type (Ent)
540 if not Is_Numeric_Type (Root_Type (Ent)) then
544 Par := Parent (First_Subtype (Ent));
547 and then Nkind (Par) = N_Full_Type_Declaration
548 and then Nkind (Type_Definition (Par)) =
549 N_Derived_Type_Definition;
557 -----------------------
558 -- Is_Generic_Formal --
559 -----------------------
561 function Is_Generic_Formal (E : Entity_Id) return Boolean is
567 Kind := Nkind (Parent (E));
569 Nkind_In (Kind, N_Formal_Object_Declaration,
570 N_Formal_Package_Declaration,
571 N_Formal_Type_Declaration)
572 or else Is_Formal_Subprogram (E);
574 end Is_Generic_Formal;
576 ---------------------------
577 -- Is_Indefinite_Subtype --
578 ---------------------------
580 function Is_Indefinite_Subtype (Ent : Entity_Id) return Boolean is
581 K : constant Entity_Kind := Ekind (Ent);
584 if Is_Constrained (Ent) then
587 elsif K in Array_Kind
588 or else K in Class_Wide_Kind
589 or else Has_Unknown_Discriminants (Ent)
593 -- Known discriminants: indefinite if there are no default values
595 elsif K in Record_Kind
596 or else Is_Incomplete_Or_Private_Type (Ent)
597 or else Is_Concurrent_Type (Ent)
599 return (Has_Discriminants (Ent)
601 No (Discriminant_Default_Value (First_Discriminant (Ent))));
606 end Is_Indefinite_Subtype;
608 -------------------------------
609 -- Is_Immutably_Limited_Type --
610 -------------------------------
612 function Is_Immutably_Limited_Type (Ent : Entity_Id) return Boolean is
613 Btype : constant Entity_Id := Base_Type (Ent);
616 if Is_Limited_Record (Btype) then
619 elsif Ekind (Btype) = E_Limited_Private_Type
620 and then Nkind (Parent (Btype)) = N_Formal_Type_Declaration
622 return not In_Package_Body (Scope ((Btype)));
625 if Is_Private_Type (Btype) then
627 -- AI05-0063: A type derived from a limited private formal type is
628 -- not immutably limited in a generic body.
630 if Is_Derived_Type (Btype)
631 and then Is_Generic_Type (Etype (Btype))
633 if not Is_Limited_Type (Etype (Btype)) then
636 -- A descendant of a limited formal type is not immutably limited
637 -- in the generic body, or in the body of a generic child.
639 elsif Ekind (Scope (Etype (Btype))) = E_Generic_Package then
640 return not In_Package_Body (Scope (Btype));
648 Utyp : constant Entity_Id := Underlying_Type (Btype);
653 return Is_Immutably_Limited_Type (Utyp);
658 elsif Is_Concurrent_Type (Btype) then
661 elsif Is_Record_Type (Btype) then
663 -- Note that we return True for all limited interfaces, even though
664 -- (unsynchronized) limited interfaces can have descendants that are
665 -- nonlimited, because this is a predicate on the type itself, and
666 -- things like functions with limited interface results need to be
667 -- handled as build in place even though they might return objects
668 -- of a type that is not inherently limited.
670 if Is_Class_Wide_Type (Btype) then
671 return Is_Immutably_Limited_Type (Root_Type (Btype));
678 C := First_Component (Btype);
679 while Present (C) loop
681 -- Don't consider components with interface types (which can
682 -- only occur in the case of a _parent component anyway).
683 -- They don't have any components, plus it would cause this
684 -- function to return true for nonlimited types derived from
685 -- limited interfaces.
687 if not Is_Interface (Etype (C))
688 and then Is_Immutably_Limited_Type (Etype (C))
693 C := Next_Component (C);
700 elsif Is_Array_Type (Btype) then
701 return Is_Immutably_Limited_Type (Component_Type (Btype));
706 end Is_Immutably_Limited_Type;
708 ---------------------
709 -- Is_Limited_Type --
710 ---------------------
712 function Is_Limited_Type (Ent : Entity_Id) return Boolean is
713 Btype : constant E := Base_Type (Ent);
714 Rtype : constant E := Root_Type (Btype);
717 if not Is_Type (Ent) then
720 elsif Ekind (Btype) = E_Limited_Private_Type
721 or else Is_Limited_Composite (Btype)
725 elsif Is_Concurrent_Type (Btype) then
728 -- The Is_Limited_Record flag normally indicates that the type is
729 -- limited. The exception is that a type does not inherit limitedness
730 -- from its interface ancestor. So the type may be derived from a
731 -- limited interface, but is not limited.
733 elsif Is_Limited_Record (Ent)
734 and then not Is_Interface (Ent)
738 -- Otherwise we will look around to see if there is some other reason
739 -- for it to be limited, except that if an error was posted on the
740 -- entity, then just assume it is non-limited, because it can cause
741 -- trouble to recurse into a murky erroneous entity!
743 elsif Error_Posted (Ent) then
746 elsif Is_Record_Type (Btype) then
748 if Is_Limited_Interface (Ent) then
751 -- AI-419: limitedness is not inherited from a limited interface
753 elsif Is_Limited_Record (Rtype) then
754 return not Is_Interface (Rtype)
755 or else Is_Protected_Interface (Rtype)
756 or else Is_Synchronized_Interface (Rtype)
757 or else Is_Task_Interface (Rtype);
759 elsif Is_Class_Wide_Type (Btype) then
760 return Is_Limited_Type (Rtype);
767 C := First_Component (Btype);
768 while Present (C) loop
769 if Is_Limited_Type (Etype (C)) then
773 C := Next_Component (C);
780 elsif Is_Array_Type (Btype) then
781 return Is_Limited_Type (Component_Type (Btype));
788 --------------------------
789 -- Is_VM_By_Copy_Actual --
790 --------------------------
792 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
794 return VM_Target /= No_VM
795 and then Nkind (N) = N_Identifier
796 and then Present (Renamed_Object (Entity (N)))
797 and then Nkind (Renamed_Object (Entity (N))) = N_Slice;
798 end Is_VM_By_Copy_Actual;
800 ----------------------
801 -- Nearest_Ancestor --
802 ----------------------
804 function Nearest_Ancestor (Typ : Entity_Id) return Entity_Id is
805 D : constant Node_Id := Declaration_Node (Typ);
808 -- If we have a subtype declaration, get the ancestor subtype
810 if Nkind (D) = N_Subtype_Declaration then
811 if Nkind (Subtype_Indication (D)) = N_Subtype_Indication then
812 return Entity (Subtype_Mark (Subtype_Indication (D)));
814 return Entity (Subtype_Indication (D));
817 -- If derived type declaration, find who we are derived from
819 elsif Nkind (D) = N_Full_Type_Declaration
820 and then Nkind (Type_Definition (D)) = N_Derived_Type_Definition
823 DTD : constant Entity_Id := Type_Definition (D);
824 SI : constant Entity_Id := Subtype_Indication (DTD);
826 if Is_Entity_Name (SI) then
829 return Entity (Subtype_Mark (SI));
833 -- Otherwise, nothing useful to return, return Empty
838 end Nearest_Ancestor;
840 ---------------------------
841 -- Nearest_Dynamic_Scope --
842 ---------------------------
844 function Nearest_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
846 if Is_Dynamic_Scope (Ent) then
849 return Enclosing_Dynamic_Scope (Ent);
851 end Nearest_Dynamic_Scope;
853 ------------------------
854 -- Next_Tag_Component --
855 ------------------------
857 function Next_Tag_Component (Tag : Entity_Id) return Entity_Id is
861 pragma Assert (Is_Tag (Tag));
863 -- Loop to look for next tag component
865 Comp := Next_Entity (Tag);
866 while Present (Comp) loop
867 if Is_Tag (Comp) then
868 pragma Assert (Chars (Comp) /= Name_uTag);
872 Comp := Next_Entity (Comp);
875 -- No tag component found
878 end Next_Tag_Component;
880 --------------------------
881 -- Number_Discriminants --
882 --------------------------
884 function Number_Discriminants (Typ : Entity_Id) return Pos is
890 Discr := First_Discriminant (Typ);
891 while Present (Discr) loop
893 Discr := Next_Discriminant (Discr);
897 end Number_Discriminants;
903 procedure Tree_Read is
905 Obsolescent_Warnings.Tree_Read;
912 procedure Tree_Write is
914 Obsolescent_Warnings.Tree_Write;
921 function Ultimate_Alias (Prim : Entity_Id) return Entity_Id is
922 E : Entity_Id := Prim;
925 while Present (Alias (E)) loop
926 pragma Assert (Alias (E) /= E);