sh.c (shift_insns_rtx, [...]): Truncate shift counts to avoid out-of-bounds array...
[official-gcc.git] / gcc / ada / sem_aux.adb
blobf2f55ce2ba01503c02f62df861ad3132eab98eb5
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-2008, 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 Namet; use Namet;
36 with Sinfo; use Sinfo;
37 with Snames; use Snames;
38 with Stand; use Stand;
40 package body Sem_Aux is
42 ----------------------
43 -- Ancestor_Subtype --
44 ----------------------
46 function Ancestor_Subtype (Typ : Entity_Id) return Entity_Id is
47 begin
48 -- If this is first subtype, or is a base type, then there is no
49 -- ancestor subtype, so we return Empty to indicate this fact.
51 if Is_First_Subtype (Typ) or else Typ = Base_Type (Typ) then
52 return Empty;
53 end if;
55 declare
56 D : constant Node_Id := Declaration_Node (Typ);
58 begin
59 -- If we have a subtype declaration, get the ancestor subtype
61 if Nkind (D) = N_Subtype_Declaration then
62 if Nkind (Subtype_Indication (D)) = N_Subtype_Indication then
63 return Entity (Subtype_Mark (Subtype_Indication (D)));
64 else
65 return Entity (Subtype_Indication (D));
66 end if;
68 -- If not, then no subtype indication is available
70 else
71 return Empty;
72 end if;
73 end;
74 end Ancestor_Subtype;
76 --------------------
77 -- Available_View --
78 --------------------
80 function Available_View (Typ : Entity_Id) return Entity_Id is
81 begin
82 if Is_Incomplete_Type (Typ)
83 and then Present (Non_Limited_View (Typ))
84 then
85 -- The non-limited view may itself be an incomplete type, in which
86 -- case get its full view.
88 return Get_Full_View (Non_Limited_View (Typ));
90 elsif Is_Class_Wide_Type (Typ)
91 and then Is_Incomplete_Type (Etype (Typ))
92 and then Present (Non_Limited_View (Etype (Typ)))
93 then
94 return Class_Wide_Type (Non_Limited_View (Etype (Typ)));
96 else
97 return Typ;
98 end if;
99 end Available_View;
101 --------------------
102 -- Constant_Value --
103 --------------------
105 function Constant_Value (Ent : Entity_Id) return Node_Id is
106 D : constant Node_Id := Declaration_Node (Ent);
107 Full_D : Node_Id;
109 begin
110 -- If we have no declaration node, then return no constant value. Not
111 -- clear how this can happen, but it does sometimes and this is the
112 -- safest approach.
114 if No (D) then
115 return Empty;
117 -- Normal case where a declaration node is present
119 elsif Nkind (D) = N_Object_Renaming_Declaration then
120 return Renamed_Object (Ent);
122 -- If this is a component declaration whose entity is a constant, it is
123 -- a prival within a protected function (and so has no constant value).
125 elsif Nkind (D) = N_Component_Declaration then
126 return Empty;
128 -- If there is an expression, return it
130 elsif Present (Expression (D)) then
131 return (Expression (D));
133 -- For a constant, see if we have a full view
135 elsif Ekind (Ent) = E_Constant
136 and then Present (Full_View (Ent))
137 then
138 Full_D := Parent (Full_View (Ent));
140 -- The full view may have been rewritten as an object renaming
142 if Nkind (Full_D) = N_Object_Renaming_Declaration then
143 return Name (Full_D);
144 else
145 return Expression (Full_D);
146 end if;
148 -- Otherwise we have no expression to return
150 else
151 return Empty;
152 end if;
153 end Constant_Value;
155 -----------------------------
156 -- Enclosing_Dynamic_Scope --
157 -----------------------------
159 function Enclosing_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
160 S : Entity_Id;
162 begin
163 -- The following test is an error defense against some syntax errors
164 -- that can leave scopes very messed up.
166 if Ent = Standard_Standard then
167 return Ent;
168 end if;
170 -- Normal case, search enclosing scopes
172 -- Note: the test for Present (S) should not be required, it defends
173 -- against an ill-formed tree.
175 S := Scope (Ent);
176 loop
177 -- If we somehow got an empty value for Scope, the tree must be
178 -- malformed. Rather than blow up we return Standard in this case.
180 if No (S) then
181 return Standard_Standard;
183 -- Quit if we get to standard or a dynamic scope
185 elsif S = Standard_Standard
186 or else Is_Dynamic_Scope (S)
187 then
188 return S;
190 -- Otherwise keep climbing
192 else
193 S := Scope (S);
194 end if;
195 end loop;
196 end Enclosing_Dynamic_Scope;
198 ------------------------
199 -- First_Discriminant --
200 ------------------------
202 function First_Discriminant (Typ : Entity_Id) return Entity_Id is
203 Ent : Entity_Id;
205 begin
206 pragma Assert
207 (Has_Discriminants (Typ)
208 or else Has_Unknown_Discriminants (Typ));
210 Ent := First_Entity (Typ);
212 -- The discriminants are not necessarily contiguous, because access
213 -- discriminants will generate itypes. They are not the first entities
214 -- either, because tag and controller record must be ahead of them.
216 if Chars (Ent) = Name_uTag then
217 Ent := Next_Entity (Ent);
218 end if;
220 if Chars (Ent) = Name_uController then
221 Ent := Next_Entity (Ent);
222 end if;
224 -- Skip all hidden stored discriminants if any
226 while Present (Ent) loop
227 exit when Ekind (Ent) = E_Discriminant
228 and then not Is_Completely_Hidden (Ent);
230 Ent := Next_Entity (Ent);
231 end loop;
233 pragma Assert (Ekind (Ent) = E_Discriminant);
235 return Ent;
236 end First_Discriminant;
238 -------------------------------
239 -- First_Stored_Discriminant --
240 -------------------------------
242 function First_Stored_Discriminant (Typ : Entity_Id) return Entity_Id is
243 Ent : Entity_Id;
245 function Has_Completely_Hidden_Discriminant
246 (Typ : Entity_Id) return Boolean;
247 -- Scans the Discriminants to see whether any are Completely_Hidden
248 -- (the mechanism for describing non-specified stored discriminants)
250 ----------------------------------------
251 -- Has_Completely_Hidden_Discriminant --
252 ----------------------------------------
254 function Has_Completely_Hidden_Discriminant
255 (Typ : Entity_Id) return Boolean
257 Ent : Entity_Id;
259 begin
260 pragma Assert (Ekind (Typ) = E_Discriminant);
262 Ent := Typ;
263 while Present (Ent) and then Ekind (Ent) = E_Discriminant loop
264 if Is_Completely_Hidden (Ent) then
265 return True;
266 end if;
268 Ent := Next_Entity (Ent);
269 end loop;
271 return False;
272 end Has_Completely_Hidden_Discriminant;
274 -- Start of processing for First_Stored_Discriminant
276 begin
277 pragma Assert
278 (Has_Discriminants (Typ)
279 or else Has_Unknown_Discriminants (Typ));
281 Ent := First_Entity (Typ);
283 if Chars (Ent) = Name_uTag then
284 Ent := Next_Entity (Ent);
285 end if;
287 if Chars (Ent) = Name_uController then
288 Ent := Next_Entity (Ent);
289 end if;
291 if Has_Completely_Hidden_Discriminant (Ent) then
293 while Present (Ent) loop
294 exit when Is_Completely_Hidden (Ent);
295 Ent := Next_Entity (Ent);
296 end loop;
298 end if;
300 pragma Assert (Ekind (Ent) = E_Discriminant);
302 return Ent;
303 end First_Stored_Discriminant;
305 -------------------
306 -- First_Subtype --
307 -------------------
309 function First_Subtype (Typ : Entity_Id) return Entity_Id is
310 B : constant Entity_Id := Base_Type (Typ);
311 F : constant Node_Id := Freeze_Node (B);
312 Ent : Entity_Id;
314 begin
315 -- If the base type has no freeze node, it is a type in standard,
316 -- and always acts as its own first subtype unless it is one of the
317 -- predefined integer types. If the type is formal, it is also a first
318 -- subtype, and its base type has no freeze node. On the other hand, a
319 -- subtype of a generic formal is not its own first_subtype. Its base
320 -- type, if anonymous, is attached to the formal type decl. from which
321 -- the first subtype is obtained.
323 if No (F) then
325 if B = Base_Type (Standard_Integer) then
326 return Standard_Integer;
328 elsif B = Base_Type (Standard_Long_Integer) then
329 return Standard_Long_Integer;
331 elsif B = Base_Type (Standard_Short_Short_Integer) then
332 return Standard_Short_Short_Integer;
334 elsif B = Base_Type (Standard_Short_Integer) then
335 return Standard_Short_Integer;
337 elsif B = Base_Type (Standard_Long_Long_Integer) then
338 return Standard_Long_Long_Integer;
340 elsif Is_Generic_Type (Typ) then
341 if Present (Parent (B)) then
342 return Defining_Identifier (Parent (B));
343 else
344 return Defining_Identifier (Associated_Node_For_Itype (B));
345 end if;
347 else
348 return B;
349 end if;
351 -- Otherwise we check the freeze node, if it has a First_Subtype_Link
352 -- then we use that link, otherwise (happens with some Itypes), we use
353 -- the base type itself.
355 else
356 Ent := First_Subtype_Link (F);
358 if Present (Ent) then
359 return Ent;
360 else
361 return B;
362 end if;
363 end if;
364 end First_Subtype;
366 -------------------------
367 -- First_Tag_Component --
368 -------------------------
370 function First_Tag_Component (Typ : Entity_Id) return Entity_Id is
371 Comp : Entity_Id;
372 Ctyp : Entity_Id;
374 begin
375 Ctyp := Typ;
376 pragma Assert (Is_Tagged_Type (Ctyp));
378 if Is_Class_Wide_Type (Ctyp) then
379 Ctyp := Root_Type (Ctyp);
380 end if;
382 if Is_Private_Type (Ctyp) then
383 Ctyp := Underlying_Type (Ctyp);
385 -- If the underlying type is missing then the source program has
386 -- errors and there is nothing else to do (the full-type declaration
387 -- associated with the private type declaration is missing).
389 if No (Ctyp) then
390 return Empty;
391 end if;
392 end if;
394 Comp := First_Entity (Ctyp);
395 while Present (Comp) loop
396 if Is_Tag (Comp) then
397 return Comp;
398 end if;
400 Comp := Next_Entity (Comp);
401 end loop;
403 -- No tag component found
405 return Empty;
406 end First_Tag_Component;
408 ----------------
409 -- Initialize --
410 ----------------
412 procedure Initialize is
413 begin
414 Obsolescent_Warnings.Init;
415 end Initialize;
417 ---------------------
418 -- Is_By_Copy_Type --
419 ---------------------
421 function Is_By_Copy_Type (Ent : Entity_Id) return Boolean is
422 begin
423 -- If Id is a private type whose full declaration has not been seen,
424 -- we assume for now that it is not a By_Copy type. Clearly this
425 -- attribute should not be used before the type is frozen, but it is
426 -- needed to build the associated record of a protected type. Another
427 -- place where some lookahead for a full view is needed ???
429 return
430 Is_Elementary_Type (Ent)
431 or else (Is_Private_Type (Ent)
432 and then Present (Underlying_Type (Ent))
433 and then Is_Elementary_Type (Underlying_Type (Ent)));
434 end Is_By_Copy_Type;
436 --------------------------
437 -- Is_By_Reference_Type --
438 --------------------------
440 function Is_By_Reference_Type (Ent : Entity_Id) return Boolean is
441 Btype : constant Entity_Id := Base_Type (Ent);
443 begin
444 if Error_Posted (Ent)
445 or else Error_Posted (Btype)
446 then
447 return False;
449 elsif Is_Private_Type (Btype) then
450 declare
451 Utyp : constant Entity_Id := Underlying_Type (Btype);
452 begin
453 if No (Utyp) then
454 return False;
455 else
456 return Is_By_Reference_Type (Utyp);
457 end if;
458 end;
460 elsif Is_Incomplete_Type (Btype) then
461 declare
462 Ftyp : constant Entity_Id := Full_View (Btype);
463 begin
464 if No (Ftyp) then
465 return False;
466 else
467 return Is_By_Reference_Type (Ftyp);
468 end if;
469 end;
471 elsif Is_Concurrent_Type (Btype) then
472 return True;
474 elsif Is_Record_Type (Btype) then
475 if Is_Limited_Record (Btype)
476 or else Is_Tagged_Type (Btype)
477 or else Is_Volatile (Btype)
478 then
479 return True;
481 else
482 declare
483 C : Entity_Id;
485 begin
486 C := First_Component (Btype);
487 while Present (C) loop
488 if Is_By_Reference_Type (Etype (C))
489 or else Is_Volatile (Etype (C))
490 then
491 return True;
492 end if;
494 C := Next_Component (C);
495 end loop;
496 end;
498 return False;
499 end if;
501 elsif Is_Array_Type (Btype) then
502 return
503 Is_Volatile (Btype)
504 or else Is_By_Reference_Type (Component_Type (Btype))
505 or else Is_Volatile (Component_Type (Btype))
506 or else Has_Volatile_Components (Btype);
508 else
509 return False;
510 end if;
511 end Is_By_Reference_Type;
513 ---------------------
514 -- Is_Derived_Type --
515 ---------------------
517 function Is_Derived_Type (Ent : E) return B is
518 Par : Node_Id;
520 begin
521 if Is_Type (Ent)
522 and then Base_Type (Ent) /= Root_Type (Ent)
523 and then not Is_Class_Wide_Type (Ent)
524 then
525 if not Is_Numeric_Type (Root_Type (Ent)) then
526 return True;
528 else
529 Par := Parent (First_Subtype (Ent));
531 return Present (Par)
532 and then Nkind (Par) = N_Full_Type_Declaration
533 and then Nkind (Type_Definition (Par)) =
534 N_Derived_Type_Definition;
535 end if;
537 else
538 return False;
539 end if;
540 end Is_Derived_Type;
542 ---------------------------
543 -- Is_Indefinite_Subtype --
544 ---------------------------
546 function Is_Indefinite_Subtype (Ent : Entity_Id) return Boolean is
547 K : constant Entity_Kind := Ekind (Ent);
549 begin
550 if Is_Constrained (Ent) then
551 return False;
553 elsif K in Array_Kind
554 or else K in Class_Wide_Kind
555 or else Has_Unknown_Discriminants (Ent)
556 then
557 return True;
559 -- Known discriminants: indefinite if there are no default values
561 elsif K in Record_Kind
562 or else Is_Incomplete_Or_Private_Type (Ent)
563 or else Is_Concurrent_Type (Ent)
564 then
565 return (Has_Discriminants (Ent)
566 and then
567 No (Discriminant_Default_Value (First_Discriminant (Ent))));
569 else
570 return False;
571 end if;
572 end Is_Indefinite_Subtype;
574 --------------------------------
575 -- Is_Inherently_Limited_Type --
576 --------------------------------
578 function Is_Inherently_Limited_Type (Ent : Entity_Id) return Boolean is
579 Btype : constant Entity_Id := Base_Type (Ent);
581 begin
582 if Is_Private_Type (Btype) then
583 declare
584 Utyp : constant Entity_Id := Underlying_Type (Btype);
585 begin
586 if No (Utyp) then
587 return False;
588 else
589 return Is_Inherently_Limited_Type (Utyp);
590 end if;
591 end;
593 elsif Is_Concurrent_Type (Btype) then
594 return True;
596 elsif Is_Record_Type (Btype) then
597 if Is_Limited_Record (Btype) then
598 return not Is_Interface (Btype)
599 or else Is_Protected_Interface (Btype)
600 or else Is_Synchronized_Interface (Btype)
601 or else Is_Task_Interface (Btype);
603 elsif Is_Class_Wide_Type (Btype) then
604 return Is_Inherently_Limited_Type (Root_Type (Btype));
606 else
607 declare
608 C : Entity_Id;
610 begin
611 C := First_Component (Btype);
612 while Present (C) loop
613 if Is_Inherently_Limited_Type (Etype (C)) then
614 return True;
615 end if;
617 C := Next_Component (C);
618 end loop;
619 end;
621 return False;
622 end if;
624 elsif Is_Array_Type (Btype) then
625 return Is_Inherently_Limited_Type (Component_Type (Btype));
627 else
628 return False;
629 end if;
630 end Is_Inherently_Limited_Type;
632 ---------------------
633 -- Is_Limited_Type --
634 ---------------------
636 function Is_Limited_Type (Ent : Entity_Id) return Boolean is
637 Btype : constant E := Base_Type (Ent);
638 Rtype : constant E := Root_Type (Btype);
640 begin
641 if not Is_Type (Ent) then
642 return False;
644 elsif Ekind (Btype) = E_Limited_Private_Type
645 or else Is_Limited_Composite (Btype)
646 then
647 return True;
649 elsif Is_Concurrent_Type (Btype) then
650 return True;
652 -- The Is_Limited_Record flag normally indicates that the type is
653 -- limited. The exception is that a type does not inherit limitedness
654 -- from its interface ancestor. So the type may be derived from a
655 -- limited interface, but is not limited.
657 elsif Is_Limited_Record (Ent)
658 and then not Is_Interface (Ent)
659 then
660 return True;
662 -- Otherwise we will look around to see if there is some other reason
663 -- for it to be limited, except that if an error was posted on the
664 -- entity, then just assume it is non-limited, because it can cause
665 -- trouble to recurse into a murky erroneous entity!
667 elsif Error_Posted (Ent) then
668 return False;
670 elsif Is_Record_Type (Btype) then
672 if Is_Limited_Interface (Ent) then
673 return True;
675 -- AI-419: limitedness is not inherited from a limited interface
677 elsif Is_Limited_Record (Rtype) then
678 return not Is_Interface (Rtype)
679 or else Is_Protected_Interface (Rtype)
680 or else Is_Synchronized_Interface (Rtype)
681 or else Is_Task_Interface (Rtype);
683 elsif Is_Class_Wide_Type (Btype) then
684 return Is_Limited_Type (Rtype);
686 else
687 declare
688 C : E;
690 begin
691 C := First_Component (Btype);
692 while Present (C) loop
693 if Is_Limited_Type (Etype (C)) then
694 return True;
695 end if;
697 C := Next_Component (C);
698 end loop;
699 end;
701 return False;
702 end if;
704 elsif Is_Array_Type (Btype) then
705 return Is_Limited_Type (Component_Type (Btype));
707 else
708 return False;
709 end if;
710 end Is_Limited_Type;
712 ---------------------------
713 -- Nearest_Dynamic_Scope --
714 ---------------------------
716 function Nearest_Dynamic_Scope (Ent : Entity_Id) return Entity_Id is
717 begin
718 if Is_Dynamic_Scope (Ent) then
719 return Ent;
720 else
721 return Enclosing_Dynamic_Scope (Ent);
722 end if;
723 end Nearest_Dynamic_Scope;
725 ------------------------
726 -- Next_Tag_Component --
727 ------------------------
729 function Next_Tag_Component (Tag : Entity_Id) return Entity_Id is
730 Comp : Entity_Id;
732 begin
733 pragma Assert (Is_Tag (Tag));
735 -- Loop to look for next tag component
737 Comp := Next_Entity (Tag);
738 while Present (Comp) loop
739 if Is_Tag (Comp) then
740 pragma Assert (Chars (Comp) /= Name_uTag);
741 return Comp;
742 end if;
744 Comp := Next_Entity (Comp);
745 end loop;
747 -- No tag component found
749 return Empty;
750 end Next_Tag_Component;
752 --------------------------
753 -- Number_Discriminants --
754 --------------------------
756 function Number_Discriminants (Typ : Entity_Id) return Pos is
757 N : Int;
758 Discr : Entity_Id;
760 begin
761 N := 0;
762 Discr := First_Discriminant (Typ);
763 while Present (Discr) loop
764 N := N + 1;
765 Discr := Next_Discriminant (Discr);
766 end loop;
768 return N;
769 end Number_Discriminants;
771 ---------------
772 -- Tree_Read --
773 ---------------
775 procedure Tree_Read is
776 begin
777 Obsolescent_Warnings.Tree_Read;
778 end Tree_Read;
780 ----------------
781 -- Tree_Write --
782 ----------------
784 procedure Tree_Write is
785 begin
786 Obsolescent_Warnings.Tree_Write;
787 end Tree_Write;
789 end Sem_Aux;