1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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 Nlists; use Nlists;
37 with Sinfo; use Sinfo;
38 with Snames; use Snames;
39 with Stand; use Stand;
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 Typ = 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
186 elsif S = Standard_Standard
187 or else Is_Dynamic_Scope (S)
191 -- Otherwise keep climbing
197 end Enclosing_Dynamic_Scope;
199 ------------------------
200 -- First_Discriminant --
201 ------------------------
203 function First_Discriminant (Typ : Entity_Id) return Entity_Id is
208 (Has_Discriminants (Typ)
209 or else Has_Unknown_Discriminants (Typ));
211 Ent := First_Entity (Typ);
213 -- The discriminants are not necessarily contiguous, because access
214 -- discriminants will generate itypes. They are not the first entities
215 -- either, because tag and controller record must be ahead of them.
217 if Chars (Ent) = Name_uTag then
218 Ent := Next_Entity (Ent);
221 if Chars (Ent) = Name_uController then
222 Ent := Next_Entity (Ent);
225 -- Skip all hidden stored discriminants if any
227 while Present (Ent) loop
228 exit when Ekind (Ent) = E_Discriminant
229 and then not Is_Completely_Hidden (Ent);
231 Ent := Next_Entity (Ent);
234 pragma Assert (Ekind (Ent) = E_Discriminant);
237 end First_Discriminant;
239 -------------------------
240 -- First_Non_SCIL_Node --
241 -------------------------
243 function First_Non_SCIL_Node (L : List_Id) return Node_Id is
248 while Nkind (N) in N_SCIL_Node loop
253 end First_Non_SCIL_Node;
255 -------------------------------
256 -- First_Stored_Discriminant --
257 -------------------------------
259 function First_Stored_Discriminant (Typ : Entity_Id) return Entity_Id is
262 function Has_Completely_Hidden_Discriminant
263 (Typ : Entity_Id) return Boolean;
264 -- Scans the Discriminants to see whether any are Completely_Hidden
265 -- (the mechanism for describing non-specified stored discriminants)
267 ----------------------------------------
268 -- Has_Completely_Hidden_Discriminant --
269 ----------------------------------------
271 function Has_Completely_Hidden_Discriminant
272 (Typ : Entity_Id) return Boolean
277 pragma Assert (Ekind (Typ) = E_Discriminant);
280 while Present (Ent) and then Ekind (Ent) = E_Discriminant loop
281 if Is_Completely_Hidden (Ent) then
285 Ent := Next_Entity (Ent);
289 end Has_Completely_Hidden_Discriminant;
291 -- Start of processing for First_Stored_Discriminant
295 (Has_Discriminants (Typ)
296 or else Has_Unknown_Discriminants (Typ));
298 Ent := First_Entity (Typ);
300 if Chars (Ent) = Name_uTag then
301 Ent := Next_Entity (Ent);
304 if Chars (Ent) = Name_uController then
305 Ent := Next_Entity (Ent);
308 if Has_Completely_Hidden_Discriminant (Ent) then
310 while Present (Ent) loop
311 exit when Is_Completely_Hidden (Ent);
312 Ent := Next_Entity (Ent);
317 pragma Assert (Ekind (Ent) = E_Discriminant);
320 end First_Stored_Discriminant;
326 function First_Subtype (Typ : Entity_Id) return Entity_Id is
327 B : constant Entity_Id := Base_Type (Typ);
328 F : constant Node_Id := Freeze_Node (B);
332 -- If the base type has no freeze node, it is a type in Standard,
333 -- and always acts as its own first subtype unless it is one of the
334 -- predefined integer types. If the type is formal, it is also a first
335 -- subtype, and its base type has no freeze node. On the other hand, a
336 -- subtype of a generic formal is not its own first subtype. Its base
337 -- type, if anonymous, is attached to the formal type decl. from which
338 -- the first subtype is obtained.
342 if B = Base_Type (Standard_Integer) then
343 return Standard_Integer;
345 elsif B = Base_Type (Standard_Long_Integer) then
346 return Standard_Long_Integer;
348 elsif B = Base_Type (Standard_Short_Short_Integer) then
349 return Standard_Short_Short_Integer;
351 elsif B = Base_Type (Standard_Short_Integer) then
352 return Standard_Short_Integer;
354 elsif B = Base_Type (Standard_Long_Long_Integer) then
355 return Standard_Long_Long_Integer;
357 elsif Is_Generic_Type (Typ) then
358 if Present (Parent (B)) then
359 return Defining_Identifier (Parent (B));
361 return Defining_Identifier (Associated_Node_For_Itype (B));
368 -- Otherwise we check the freeze node, if it has a First_Subtype_Link
369 -- then we use that link, otherwise (happens with some Itypes), we use
370 -- the base type itself.
373 Ent := First_Subtype_Link (F);
375 if Present (Ent) then
383 -------------------------
384 -- First_Tag_Component --
385 -------------------------
387 function First_Tag_Component (Typ : Entity_Id) return Entity_Id is
393 pragma Assert (Is_Tagged_Type (Ctyp));
395 if Is_Class_Wide_Type (Ctyp) then
396 Ctyp := Root_Type (Ctyp);
399 if Is_Private_Type (Ctyp) then
400 Ctyp := Underlying_Type (Ctyp);
402 -- If the underlying type is missing then the source program has
403 -- errors and there is nothing else to do (the full-type declaration
404 -- associated with the private type declaration is missing).
411 Comp := First_Entity (Ctyp);
412 while Present (Comp) loop
413 if Is_Tag (Comp) then
417 Comp := Next_Entity (Comp);
420 -- No tag component found
423 end First_Tag_Component;
429 procedure Initialize is
431 Obsolescent_Warnings.Init;
434 ---------------------
435 -- Is_By_Copy_Type --
436 ---------------------
438 function Is_By_Copy_Type (Ent : Entity_Id) return Boolean is
440 -- If Id is a private type whose full declaration has not been seen,
441 -- we assume for now that it is not a By_Copy type. Clearly this
442 -- attribute should not be used before the type is frozen, but it is
443 -- needed to build the associated record of a protected type. Another
444 -- place where some lookahead for a full view is needed ???
447 Is_Elementary_Type (Ent)
448 or else (Is_Private_Type (Ent)
449 and then Present (Underlying_Type (Ent))
450 and then Is_Elementary_Type (Underlying_Type (Ent)));
453 --------------------------
454 -- Is_By_Reference_Type --
455 --------------------------
457 function Is_By_Reference_Type (Ent : Entity_Id) return Boolean is
458 Btype : constant Entity_Id := Base_Type (Ent);
461 if Error_Posted (Ent)
462 or else Error_Posted (Btype)
466 elsif Is_Private_Type (Btype) then
468 Utyp : constant Entity_Id := Underlying_Type (Btype);
473 return Is_By_Reference_Type (Utyp);
477 elsif Is_Incomplete_Type (Btype) then
479 Ftyp : constant Entity_Id := Full_View (Btype);
484 return Is_By_Reference_Type (Ftyp);
488 elsif Is_Concurrent_Type (Btype) then
491 elsif Is_Record_Type (Btype) then
492 if Is_Limited_Record (Btype)
493 or else Is_Tagged_Type (Btype)
494 or else Is_Volatile (Btype)
503 C := First_Component (Btype);
504 while Present (C) loop
505 if Is_By_Reference_Type (Etype (C))
506 or else Is_Volatile (Etype (C))
511 C := Next_Component (C);
518 elsif Is_Array_Type (Btype) then
521 or else Is_By_Reference_Type (Component_Type (Btype))
522 or else Is_Volatile (Component_Type (Btype))
523 or else Has_Volatile_Components (Btype);
528 end Is_By_Reference_Type;
530 ---------------------
531 -- Is_Derived_Type --
532 ---------------------
534 function Is_Derived_Type (Ent : E) return B is
539 and then Base_Type (Ent) /= Root_Type (Ent)
540 and then not Is_Class_Wide_Type (Ent)
542 if not Is_Numeric_Type (Root_Type (Ent)) then
546 Par := Parent (First_Subtype (Ent));
549 and then Nkind (Par) = N_Full_Type_Declaration
550 and then Nkind (Type_Definition (Par)) =
551 N_Derived_Type_Definition;
559 ---------------------------
560 -- Is_Indefinite_Subtype --
561 ---------------------------
563 function Is_Indefinite_Subtype (Ent : Entity_Id) return Boolean is
564 K : constant Entity_Kind := Ekind (Ent);
567 if Is_Constrained (Ent) then
570 elsif K in Array_Kind
571 or else K in Class_Wide_Kind
572 or else Has_Unknown_Discriminants (Ent)
576 -- Known discriminants: indefinite if there are no default values
578 elsif K in Record_Kind
579 or else Is_Incomplete_Or_Private_Type (Ent)
580 or else Is_Concurrent_Type (Ent)
582 return (Has_Discriminants (Ent)
584 No (Discriminant_Default_Value (First_Discriminant (Ent))));
589 end Is_Indefinite_Subtype;
591 --------------------------------
592 -- Is_Inherently_Limited_Type --
593 --------------------------------
595 function Is_Inherently_Limited_Type (Ent : Entity_Id) return Boolean is
596 Btype : constant Entity_Id := Base_Type (Ent);
599 if Is_Private_Type (Btype) then
601 Utyp : constant Entity_Id := Underlying_Type (Btype);
606 return Is_Inherently_Limited_Type (Utyp);
610 elsif Is_Concurrent_Type (Btype) then
613 elsif Is_Record_Type (Btype) then
615 -- Note that we return True for all limited interfaces, even though
616 -- (unsynchronized) limited interfaces can have descendants that are
617 -- nonlimited, because this is a predicate on the type itself, and
618 -- things like functions with limited interface results need to be
619 -- handled as build in place even though they might return objects
620 -- of a type that is not inherently limited.
622 if Is_Limited_Record (Btype) then
625 elsif Is_Class_Wide_Type (Btype) then
626 return Is_Inherently_Limited_Type (Root_Type (Btype));
633 C := First_Component (Btype);
634 while Present (C) loop
636 -- Don't consider components with interface types (which can
637 -- only occur in the case of a _parent component anyway).
638 -- They don't have any components, plus it would cause this
639 -- function to return true for nonlimited types derived from
640 -- limited intefaces.
642 if not Is_Interface (Etype (C))
643 and then Is_Inherently_Limited_Type (Etype (C))
648 C := Next_Component (C);
655 elsif Is_Array_Type (Btype) then
656 return Is_Inherently_Limited_Type (Component_Type (Btype));
661 end Is_Inherently_Limited_Type;
663 ---------------------
664 -- Is_Limited_Type --
665 ---------------------
667 function Is_Limited_Type (Ent : Entity_Id) return Boolean is
668 Btype : constant E := Base_Type (Ent);
669 Rtype : constant E := Root_Type (Btype);
672 if not Is_Type (Ent) then
675 elsif Ekind (Btype) = E_Limited_Private_Type
676 or else Is_Limited_Composite (Btype)
680 elsif Is_Concurrent_Type (Btype) then
683 -- The Is_Limited_Record flag normally indicates that the type is
684 -- limited. The exception is that a type does not inherit limitedness
685 -- from its interface ancestor. So the type may be derived from a
686 -- limited interface, but is not limited.
688 elsif Is_Limited_Record (Ent)
689 and then not Is_Interface (Ent)
693 -- Otherwise we will look around to see if there is some other reason
694 -- for it to be limited, except that if an error was posted on the
695 -- entity, then just assume it is non-limited, because it can cause
696 -- trouble to recurse into a murky erroneous entity!
698 elsif Error_Posted (Ent) then
701 elsif Is_Record_Type (Btype) then
703 if Is_Limited_Interface (Ent) then
706 -- AI-419: limitedness is not inherited from a limited interface
708 elsif Is_Limited_Record (Rtype) then
709 return not Is_Interface (Rtype)
710 or else Is_Protected_Interface (Rtype)
711 or else Is_Synchronized_Interface (Rtype)
712 or else Is_Task_Interface (Rtype);
714 elsif Is_Class_Wide_Type (Btype) then
715 return Is_Limited_Type (Rtype);
722 C := First_Component (Btype);
723 while Present (C) loop
724 if Is_Limited_Type (Etype (C)) then
728 C := Next_Component (C);
735 elsif Is_Array_Type (Btype) then
736 return Is_Limited_Type (Component_Type (Btype));
743 ---------------------------
744 -- Nearest_Dynamic_Scope --
745 ---------------------------
747 function Nearest_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
749 if Is_Dynamic_Scope (Ent) then
752 return Enclosing_Dynamic_Scope (Ent);
754 end Nearest_Dynamic_Scope;
756 ------------------------
757 -- Next_Non_SCIL_Node --
758 ------------------------
760 function Next_Non_SCIL_Node (N : Node_Id) return Node_Id is
765 while Nkind (Aux_N) in N_SCIL_Node loop
770 end Next_Non_SCIL_Node;
772 ------------------------
773 -- Next_Tag_Component --
774 ------------------------
776 function Next_Tag_Component (Tag : Entity_Id) return Entity_Id is
780 pragma Assert (Is_Tag (Tag));
782 -- Loop to look for next tag component
784 Comp := Next_Entity (Tag);
785 while Present (Comp) loop
786 if Is_Tag (Comp) then
787 pragma Assert (Chars (Comp) /= Name_uTag);
791 Comp := Next_Entity (Comp);
794 -- No tag component found
797 end Next_Tag_Component;
799 --------------------------
800 -- Number_Discriminants --
801 --------------------------
803 function Number_Discriminants (Typ : Entity_Id) return Pos is
809 Discr := First_Discriminant (Typ);
810 while Present (Discr) loop
812 Discr := Next_Discriminant (Discr);
816 end Number_Discriminants;
822 procedure Tree_Read is
824 Obsolescent_Warnings.Tree_Read;
831 procedure Tree_Write is
833 Obsolescent_Warnings.Tree_Write;