OSDN Git Service

* sem_ch3.adb: Clarify some ???.
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch13.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ C H 1 3                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                            $Revision$
10 --                                                                          --
11 --          Copyright (C) 1992-2001, Free Software Foundation, Inc.         --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 with Atree;    use Atree;
30 with Checks;   use Checks;
31 with Einfo;    use Einfo;
32 with Errout;   use Errout;
33 with Exp_Tss;  use Exp_Tss;
34 with Exp_Util; use Exp_Util;
35 with Hostparm; use Hostparm;
36 with Lib;      use Lib;
37 with Nlists;   use Nlists;
38 with Nmake;    use Nmake;
39 with Opt;      use Opt;
40 with Rtsfind;  use Rtsfind;
41 with Sem;      use Sem;
42 with Sem_Ch8;  use Sem_Ch8;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res;  use Sem_Res;
45 with Sem_Type; use Sem_Type;
46 with Sem_Util; use Sem_Util;
47 with Stand;    use Stand;
48 with Sinfo;    use Sinfo;
49 with Snames;   use Snames;
50 with Table;
51 with Ttypes;   use Ttypes;
52 with Tbuild;   use Tbuild;
53 with Urealp;   use Urealp;
54
55 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
56
57 package body Sem_Ch13 is
58
59    SSU : constant Pos := System_Storage_Unit;
60    --  Convenient short hand for commonly used constant
61
62    -----------------------
63    -- Local Subprograms --
64    -----------------------
65
66    procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
67    --  This routine is called after setting the Esize of type entity Typ.
68    --  The purpose is to deal with the situation where an aligment has been
69    --  inherited from a derived type that is no longer appropriate for the
70    --  new Esize value. In this case, we reset the Alignment to unknown.
71
72    procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
73    --  Given two entities for record components or discriminants, checks
74    --  if they hav overlapping component clauses and issues errors if so.
75
76    function Get_Alignment_Value (Expr : Node_Id) return Uint;
77    --  Given the expression for an alignment value, returns the corresponding
78    --  Uint value. If the value is inappropriate, then error messages are
79    --  posted as required, and a value of No_Uint is returned.
80
81    function Is_Operational_Item (N : Node_Id) return Boolean;
82    --  A specification for a stream attribute is allowed before the full
83    --  type is declared, as explained in AI-00137 and the corrigendum.
84    --  Attributes that do not specify a representation characteristic are
85    --  operational attributes.
86
87    procedure New_Stream_Function
88      (N    : Node_Id;
89       Ent  : Entity_Id;
90       Subp : Entity_Id;
91       Nam  : Name_Id);
92    --  Create a function renaming of a given stream attribute to the
93    --  designated subprogram and then in the tagged case, provide this as
94    --  a primitive operation, or in the non-tagged case make an appropriate
95    --  TSS entry. Used for Input. This is more properly an expansion activity
96    --  than just semantics, but the presence of user-defined stream functions
97    --  for limited types is a legality check, which is why this takes place
98    --  here rather than in exp_ch13, where it was previously.
99
100    procedure New_Stream_Procedure
101      (N     : Node_Id;
102       Ent   : Entity_Id;
103       Subp  : Entity_Id;
104       Nam   : Name_Id;
105       Out_P : Boolean := False);
106    --  Create a procedure renaming of a given stream attribute to the
107    --  designated subprogram and then in the tagged case, provide this as
108    --  a primitive operation, or in the non-tagged case make an appropriate
109    --  TSS entry. Used for Read, Output, Write.
110
111    procedure Check_Constant_Address_Clause (Expr : Node_Id; U_Ent : Entity_Id);
112    --  Expr is an expression for an address clause. This procedure checks
113    --  that the expression is constant, in the limited sense that it is safe
114    --  to evaluate it at the point the object U_Ent is declared, rather than
115    --  at the point of the address clause. The condition for this to be true
116    --  is that the expression has no variables, no constants declared after
117    --  U_Ent, and no calls to non-pure functions. If this condition is not
118    --  met, then an appropriate error message is posted.
119
120    procedure Warn_Overlay
121      (Expr : Node_Id;
122       Typ  : Entity_Id;
123       Nam  : Node_Id);
124    --  Expr is the expression for an address clause for entity Nam whose type
125    --  is Typ. If Typ has a default initialization, check whether the address
126    --  clause might overlay two entities, and emit a warning on the side effect
127    --  that the initialization will cause.
128
129    ----------------------------------------------
130    -- Table for Validate_Unchecked_Conversions --
131    ----------------------------------------------
132
133    --  The following table collects unchecked conversions for validation.
134    --  Entries are made by Validate_Unchecked_Conversion and then the
135    --  call to Validate_Unchecked_Conversions does the actual error
136    --  checking and posting of warnings. The reason for this delayed
137    --  processing is to take advantage of back-annotations of size and
138    --  alignment values peformed by the back end.
139
140    type UC_Entry is record
141       Enode  : Node_Id;   -- node used for posting warnings
142       Source : Entity_Id; -- source type for unchecked conversion
143       Target : Entity_Id; -- target type for unchecked conversion
144    end record;
145
146    package Unchecked_Conversions is new Table.Table (
147      Table_Component_Type => UC_Entry,
148      Table_Index_Type     => Int,
149      Table_Low_Bound      => 1,
150      Table_Initial        => 50,
151      Table_Increment      => 200,
152      Table_Name           => "Unchecked_Conversions");
153
154    --------------------------------------
155    -- Alignment_Check_For_Esize_Change --
156    --------------------------------------
157
158    procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
159    begin
160       --  If the alignment is known, and not set by a rep clause, and is
161       --  inconsistent with the size being set, then reset it to unknown,
162       --  we assume in this case that the size overrides the inherited
163       --  alignment, and that the alignment must be recomputed.
164
165       if Known_Alignment (Typ)
166         and then not Has_Alignment_Clause (Typ)
167         and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
168       then
169          Init_Alignment (Typ);
170       end if;
171    end Alignment_Check_For_Esize_Change;
172
173    -----------------------
174    -- Analyze_At_Clause --
175    -----------------------
176
177    --  An at clause is replaced by the corresponding Address attribute
178    --  definition clause that is the preferred approach in Ada 95.
179
180    procedure Analyze_At_Clause (N : Node_Id) is
181    begin
182       Rewrite (N,
183         Make_Attribute_Definition_Clause (Sloc (N),
184           Name  => Identifier (N),
185           Chars => Name_Address,
186           Expression => Expression (N)));
187       Analyze_Attribute_Definition_Clause (N);
188    end Analyze_At_Clause;
189
190    -----------------------------------------
191    -- Analyze_Attribute_Definition_Clause --
192    -----------------------------------------
193
194    procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
195       Loc   : constant Source_Ptr   := Sloc (N);
196       Nam   : constant Node_Id      := Name (N);
197       Attr  : constant Name_Id      := Chars (N);
198       Expr  : constant Node_Id      := Expression (N);
199       Id    : constant Attribute_Id := Get_Attribute_Id (Attr);
200       Ent   : Entity_Id;
201       U_Ent : Entity_Id;
202
203       FOnly : Boolean := False;
204       --  Reset to True for subtype specific attribute (Alignment, Size)
205       --  and for stream attributes, i.e. those cases where in the call
206       --  to Rep_Item_Too_Late, FOnly is set True so that only the freezing
207       --  rules are checked. Note that the case of stream attributes is not
208       --  clear from the RM, but see AI95-00137. Also, the RM seems to
209       --  disallow Storage_Size for derived task types, but that is also
210       --  clearly unintentional.
211
212    begin
213       Analyze (Nam);
214       Ent := Entity (Nam);
215
216       if Rep_Item_Too_Early (Ent, N) then
217          return;
218       end if;
219
220       --  Rep clause applies to full view of incomplete type or private type
221       --  if we have one (if not, this is a premature use of the type).
222       --  However, certain semantic checks need to be done on the specified
223       --  entity (i.e. the private view), so we save it in Ent.
224
225       if Is_Private_Type (Ent)
226         and then Is_Derived_Type (Ent)
227         and then not Is_Tagged_Type (Ent)
228         and then No (Full_View (Ent))
229       then
230          --  If this is a private type whose completion is a derivation
231          --  from another private type, there is no full view, and the
232          --  attribute belongs to the type itself, not its underlying parent.
233
234          U_Ent := Ent;
235
236       elsif Ekind (Ent) = E_Incomplete_Type then
237          Ent := Underlying_Type (Ent);
238          U_Ent := Ent;
239       else
240          U_Ent := Underlying_Type (Ent);
241       end if;
242
243       --  Complete other routine error checks
244
245       if Etype (Nam) = Any_Type then
246          return;
247
248       elsif Scope (Ent) /= Current_Scope then
249          Error_Msg_N ("entity must be declared in this scope", Nam);
250          return;
251
252       elsif Is_Type (U_Ent)
253         and then not Is_First_Subtype (U_Ent)
254         and then Id /= Attribute_Object_Size
255         and then Id /= Attribute_Value_Size
256         and then not From_At_Mod (N)
257       then
258          Error_Msg_N ("cannot specify attribute for subtype", Nam);
259          return;
260
261       end if;
262
263       --  Switch on particular attribute
264
265       case Id is
266
267          -------------
268          -- Address --
269          -------------
270
271          --  Address attribute definition clause
272
273          when Attribute_Address => Address : begin
274             Analyze_And_Resolve (Expr, RTE (RE_Address));
275
276             if Present (Address_Clause (U_Ent)) then
277                Error_Msg_N ("address already given for &", Nam);
278
279             --  Case of address clause for subprogram
280
281             elsif Is_Subprogram (U_Ent) then
282
283                if Has_Homonym (U_Ent) then
284                   Error_Msg_N
285                     ("address clause cannot be given " &
286                      "for overloaded subprogram",
287                      Nam);
288                end if;
289
290                --  For subprograms, all address clauses are permitted,
291                --  and we mark the subprogram as having a deferred freeze
292                --  so that Gigi will not elaborate it too soon.
293
294                --  Above needs more comments, what is too soon about???
295
296                Set_Has_Delayed_Freeze (U_Ent);
297
298             --  Case of address clause for entry
299
300             elsif Ekind (U_Ent) = E_Entry then
301
302                if Nkind (Parent (N)) = N_Task_Body then
303                   Error_Msg_N
304                     ("entry address must be specified in task spec", Nam);
305                end if;
306
307                --  For entries, we require a constant address
308
309                Check_Constant_Address_Clause (Expr, U_Ent);
310
311             --  Case of address clause for an object
312
313             elsif
314               Ekind (U_Ent) = E_Variable
315                 or else
316               Ekind (U_Ent) = E_Constant
317             then
318                declare
319                   Decl : constant Node_Id   := Declaration_Node (U_Ent);
320                   Expr : constant Node_Id   := Expression (N);
321                   Typ  : constant Entity_Id := Etype (U_Ent);
322
323                begin
324                   --  Exported variables cannot have an address clause,
325                   --  because this cancels the effect of the pragma Export
326
327                   if Is_Exported (U_Ent) then
328                      Error_Msg_N
329                        ("cannot export object with address clause", Nam);
330
331                   --  Imported variables can have an address clause, but then
332                   --  the import is pretty meaningless except to suppress
333                   --  initializations, so we do not need such variables to
334                   --  be statically allocated (and in fact it causes trouble
335                   --  if the address clause is a local value).
336
337                   elsif Is_Imported (U_Ent) then
338                      Set_Is_Statically_Allocated (U_Ent, False);
339                   end if;
340
341                   --  We mark a possible modification of a variable with an
342                   --  address clause, since it is likely aliasing is occurring.
343
344                   Note_Possible_Modification (Nam);
345
346                   --  If we have no initialization of any kind, then we can
347                   --  safely defer the elaboration of the variable to its
348                   --  freezing point, so that the address clause will be
349                   --  computed at the proper point.
350
351                   --  The same processing applies to all initialized scalar
352                   --  types and all access types. Packed bit arrays of size
353                   --  up to 64 are represented using a modular type with an
354                   --  initialization (to zero) and can be processed like
355                   --  other initialized scalar types.
356
357                   if (No (Expression (Decl))
358                        and then not Has_Non_Null_Base_Init_Proc (Typ))
359
360                     or else
361                       (Present (Expression (Decl))
362                         and then Is_Scalar_Type (Typ))
363
364                     or else
365                       Is_Access_Type (Typ)
366
367                     or else
368                       (Is_Bit_Packed_Array (Base_Type (Typ))
369                         and then
370                           Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
371                   then
372                      Set_Has_Delayed_Freeze (U_Ent);
373
374                   --  Otherwise, we require the address clause to be constant
375
376                   else
377                      Check_Constant_Address_Clause (Expr, U_Ent);
378                   end if;
379
380                   if Is_Exported (U_Ent) then
381                      Error_Msg_N
382                        ("& cannot be exported if an address clause is given",
383                         Nam);
384                      Error_Msg_N
385                        ("\define and export a variable " &
386                         "that holds its address instead",
387                         Nam);
388                   end if;
389
390                   if not Error_Posted (Expr) then
391                      Warn_Overlay (Expr, Typ, Nam);
392                   end if;
393
394                   --  If entity has delayed freeze then we will generate
395                   --  an alignment check at the freeze point. If there is
396                   --  no delayed freeze we can do it right now.
397
398                   if not Has_Delayed_Freeze (U_Ent) then
399                      Apply_Alignment_Check (U_Ent, N);
400                   end if;
401
402                   --  Kill the size check code, since we are not allocating
403                   --  the variable, it is somewhere else.
404
405                   Kill_Size_Check_Code (U_Ent);
406                end;
407
408             --  Not a valid entity for an address clause
409
410             else
411                Error_Msg_N ("address cannot be given for &", Nam);
412             end if;
413          end Address;
414
415          ---------------
416          -- Alignment --
417          ---------------
418
419          --  Alignment attribute definition clause
420
421          when Attribute_Alignment => Alignment_Block : declare
422             Align : Uint := Get_Alignment_Value (Expr);
423
424          begin
425             FOnly := True;
426
427             if not Is_Type (U_Ent)
428               and then Ekind (U_Ent) /= E_Variable
429               and then Ekind (U_Ent) /= E_Constant
430             then
431                Error_Msg_N ("alignment cannot be given for &", Nam);
432
433             elsif Has_Alignment_Clause (U_Ent) then
434                Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
435                Error_Msg_N ("alignment clause previously given#", N);
436
437             elsif Align /= No_Uint then
438                Set_Has_Alignment_Clause (U_Ent);
439                Set_Alignment            (U_Ent, Align);
440             end if;
441          end Alignment_Block;
442
443          ---------------
444          -- Bit_Order --
445          ---------------
446
447          --  Bit_Order attribute definition clause
448
449          when Attribute_Bit_Order => Bit_Order : declare
450          begin
451             if not Is_Record_Type (U_Ent) then
452                Error_Msg_N
453                  ("Bit_Order can only be defined for record type", Nam);
454
455             else
456                Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
457
458                if Etype (Expr) = Any_Type then
459                   return;
460
461                elsif not Is_Static_Expression (Expr) then
462                   Error_Msg_N ("Bit_Order requires static expression", Expr);
463
464                else
465                   if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
466                      Set_Reverse_Bit_Order (U_Ent, True);
467                   end if;
468                end if;
469             end if;
470          end Bit_Order;
471
472          --------------------
473          -- Component_Size --
474          --------------------
475
476          --  Component_Size attribute definition clause
477
478          when Attribute_Component_Size => Component_Size_Case : declare
479             Csize    : constant Uint := Static_Integer (Expr);
480             Btype    : Entity_Id;
481             Biased   : Boolean;
482             New_Ctyp : Entity_Id;
483             Decl     : Node_Id;
484
485          begin
486             if not Is_Array_Type (U_Ent) then
487                Error_Msg_N ("component size requires array type", Nam);
488                return;
489             end if;
490
491             Btype := Base_Type (U_Ent);
492
493             if Has_Component_Size_Clause (Btype) then
494                Error_Msg_N
495                  ("component size clase for& previously given", Nam);
496
497             elsif Csize /= No_Uint then
498                Check_Size (Expr, Component_Type (Btype), Csize, Biased);
499
500                if Has_Aliased_Components (Btype)
501                  and then Csize < 32
502                  and then Csize /= 8
503                  and then Csize /= 16
504                then
505                   Error_Msg_N
506                     ("component size incorrect for aliased components", N);
507                   return;
508                end if;
509
510                --  For the biased case, build a declaration for a subtype
511                --  that will be used to represent the biased subtype that
512                --  reflects the biased representation of components. We need
513                --  this subtype to get proper conversions on referencing
514                --  elements of the array.
515
516                if Biased then
517                   New_Ctyp :=
518                     Make_Defining_Identifier (Loc,
519                       Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
520
521                   Decl :=
522                     Make_Subtype_Declaration (Loc,
523                       Defining_Identifier => New_Ctyp,
524                       Subtype_Indication  =>
525                         New_Occurrence_Of (Component_Type (Btype), Loc));
526
527                   Set_Parent (Decl, N);
528                   Analyze (Decl, Suppress => All_Checks);
529
530                   Set_Has_Delayed_Freeze        (New_Ctyp, False);
531                   Set_Esize                     (New_Ctyp, Csize);
532                   Set_RM_Size                   (New_Ctyp, Csize);
533                   Init_Alignment                (New_Ctyp);
534                   Set_Has_Biased_Representation (New_Ctyp, True);
535                   Set_Is_Itype                  (New_Ctyp, True);
536                   Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
537
538                   Set_Component_Type (Btype, New_Ctyp);
539                end if;
540
541                Set_Component_Size            (Btype, Csize);
542                Set_Has_Component_Size_Clause (Btype, True);
543                Set_Has_Non_Standard_Rep      (Btype, True);
544             end if;
545          end Component_Size_Case;
546
547          ------------------
548          -- External_Tag --
549          ------------------
550
551          when Attribute_External_Tag => External_Tag :
552          begin
553             if not Is_Tagged_Type (U_Ent) then
554                Error_Msg_N ("should be a tagged type", Nam);
555             end if;
556
557             Analyze_And_Resolve (Expr, Standard_String);
558
559             if not Is_Static_Expression (Expr) then
560                Error_Msg_N ("must be a static string", Nam);
561             end if;
562
563             Set_Has_External_Tag_Rep_Clause (U_Ent);
564          end External_Tag;
565
566          -----------
567          -- Input --
568          -----------
569
570          when Attribute_Input => Input : declare
571             Subp : Entity_Id := Empty;
572             I    : Interp_Index;
573             It   : Interp;
574             Pnam : Entity_Id;
575
576             function Has_Good_Profile (Subp : Entity_Id) return Boolean;
577             --  Return true if the entity is a function with an appropriate
578             --  profile for the Input attribute.
579
580             function Has_Good_Profile (Subp : Entity_Id) return Boolean is
581                F  : Entity_Id;
582                Ok : Boolean := False;
583
584             begin
585                if Ekind (Subp) = E_Function then
586                   F := First_Formal (Subp);
587
588                   if Present (F) and then No (Next_Formal (F)) then
589                      if Ekind (Etype (F)) = E_Anonymous_Access_Type
590                        and then
591                          Designated_Type (Etype (F)) =
592                            Class_Wide_Type (RTE (RE_Root_Stream_Type))
593                      then
594                         Ok := Base_Type (Etype (Subp)) = Base_Type (Ent);
595                      end if;
596                   end if;
597                end if;
598
599                return Ok;
600             end Has_Good_Profile;
601
602          --  Start of processing for Input attribute definition
603
604          begin
605             FOnly := True;
606
607             if not Is_Type (U_Ent) then
608                Error_Msg_N ("local name must be a subtype", Nam);
609                return;
610
611             else
612                Pnam := TSS (Base_Type (U_Ent), Name_uInput);
613
614                if Present (Pnam)
615                  and then Base_Type (Etype (Pnam)) = Base_Type (U_Ent)
616                then
617                   Error_Msg_Sloc := Sloc (Pnam);
618                   Error_Msg_N ("input attribute already defined #", Nam);
619                   return;
620                end if;
621             end if;
622
623             Analyze (Expr);
624
625             if Is_Entity_Name (Expr) then
626                if not Is_Overloaded (Expr) then
627                   if Has_Good_Profile (Entity (Expr)) then
628                      Subp := Entity (Expr);
629                   end if;
630
631                else
632                   Get_First_Interp (Expr, I, It);
633
634                   while Present (It.Nam) loop
635                      if Has_Good_Profile (It.Nam) then
636                         Subp := It.Nam;
637                         exit;
638                      end if;
639
640                      Get_Next_Interp (I, It);
641                   end loop;
642                end if;
643             end if;
644
645             if Present (Subp) then
646                Set_Entity (Expr, Subp);
647                Set_Etype (Expr, Etype (Subp));
648                New_Stream_Function (N, U_Ent, Subp,  Name_uInput);
649             else
650                Error_Msg_N ("incorrect expression for input attribute", Expr);
651                return;
652             end if;
653          end Input;
654
655          -------------------
656          -- Machine_Radix --
657          -------------------
658
659          --  Machine radix attribute definition clause
660
661          when Attribute_Machine_Radix => Machine_Radix : declare
662             Radix : constant Uint := Static_Integer (Expr);
663
664          begin
665             if not Is_Decimal_Fixed_Point_Type (U_Ent) then
666                Error_Msg_N ("decimal fixed-point type expected for &", Nam);
667
668             elsif Has_Machine_Radix_Clause (U_Ent) then
669                Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
670                Error_Msg_N ("machine radix clause previously given#", N);
671
672             elsif Radix /= No_Uint then
673                Set_Has_Machine_Radix_Clause (U_Ent);
674                Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
675
676                if Radix = 2 then
677                   null;
678                elsif Radix = 10 then
679                   Set_Machine_Radix_10 (U_Ent);
680                else
681                   Error_Msg_N ("machine radix value must be 2 or 10", Expr);
682                end if;
683             end if;
684          end Machine_Radix;
685
686          -----------------
687          -- Object_Size --
688          -----------------
689
690          --  Object_Size attribute definition clause
691
692          when Attribute_Object_Size => Object_Size : declare
693             Size   : constant Uint := Static_Integer (Expr);
694             Biased : Boolean;
695
696          begin
697             if not Is_Type (U_Ent) then
698                Error_Msg_N ("Object_Size cannot be given for &", Nam);
699
700             elsif Has_Object_Size_Clause (U_Ent) then
701                Error_Msg_N ("Object_Size already given for &", Nam);
702
703             else
704                Check_Size (Expr, U_Ent, Size, Biased);
705
706                if Size /= 8
707                     and then
708                   Size /= 16
709                     and then
710                   Size /= 32
711                     and then
712                   UI_Mod (Size, 64) /= 0
713                then
714                   Error_Msg_N
715                     ("Object_Size must be 8, 16, 32, or multiple of 64",
716                      Expr);
717                end if;
718
719                Set_Esize (U_Ent, Size);
720                Set_Has_Object_Size_Clause (U_Ent);
721                Alignment_Check_For_Esize_Change (U_Ent);
722             end if;
723          end Object_Size;
724
725          ------------
726          -- Output --
727          ------------
728
729          when Attribute_Output => Output : declare
730             Subp : Entity_Id := Empty;
731             I    : Interp_Index;
732             It   : Interp;
733             Pnam : Entity_Id;
734
735             function Has_Good_Profile (Subp : Entity_Id) return Boolean;
736             --  Return true if the entity is a procedure with an
737             --  appropriate profile for the output attribute.
738
739             function Has_Good_Profile (Subp : Entity_Id) return Boolean is
740                F  : Entity_Id;
741                Ok : Boolean := False;
742
743             begin
744                if Ekind (Subp) = E_Procedure then
745                   F := First_Formal (Subp);
746
747                   if Present (F) then
748                      if Ekind (Etype (F)) = E_Anonymous_Access_Type
749                        and then
750                          Designated_Type (Etype (F)) =
751                            Class_Wide_Type (RTE (RE_Root_Stream_Type))
752                      then
753                         Next_Formal (F);
754                         Ok :=  Present (F)
755                           and then Parameter_Mode (F) = E_In_Parameter
756                           and then Base_Type (Etype (F)) = Base_Type (Ent)
757                           and then No (Next_Formal (F));
758                      end if;
759                   end if;
760                end if;
761
762                return Ok;
763             end Has_Good_Profile;
764
765          begin
766             FOnly := True;
767
768             if not Is_Type (U_Ent) then
769                Error_Msg_N ("local name must be a subtype", Nam);
770                return;
771
772             else
773                Pnam := TSS (Base_Type (U_Ent), Name_uOutput);
774
775                if Present (Pnam)
776                  and then
777                    Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
778                                                         = Base_Type (U_Ent)
779                then
780                   Error_Msg_Sloc := Sloc (Pnam);
781                   Error_Msg_N ("output attribute already defined #", Nam);
782                   return;
783                end if;
784             end if;
785
786             Analyze (Expr);
787
788             if Is_Entity_Name (Expr) then
789                if not Is_Overloaded (Expr) then
790                   if Has_Good_Profile (Entity (Expr)) then
791                      Subp := Entity (Expr);
792                   end if;
793
794                else
795                   Get_First_Interp (Expr, I, It);
796
797                   while Present (It.Nam) loop
798                      if Has_Good_Profile (It.Nam) then
799                         Subp := It.Nam;
800                         exit;
801                      end if;
802
803                      Get_Next_Interp (I, It);
804                   end loop;
805                end if;
806             end if;
807
808             if Present (Subp) then
809                Set_Entity (Expr, Subp);
810                Set_Etype (Expr, Etype (Subp));
811                New_Stream_Procedure (N, U_Ent, Subp, Name_uOutput);
812             else
813                Error_Msg_N ("incorrect expression for output attribute", Expr);
814                return;
815             end if;
816          end Output;
817
818          ----------
819          -- Read --
820          ----------
821
822          when Attribute_Read => Read : declare
823             Subp : Entity_Id := Empty;
824             I    : Interp_Index;
825             It   : Interp;
826             Pnam : Entity_Id;
827
828             function Has_Good_Profile (Subp : Entity_Id) return Boolean;
829             --  Return true if the entity is a procedure with an appropriate
830             --  profile for the Read attribute.
831
832             function Has_Good_Profile (Subp : Entity_Id) return Boolean is
833                F     : Entity_Id;
834                Ok    : Boolean := False;
835
836             begin
837                if Ekind (Subp) = E_Procedure then
838                   F := First_Formal (Subp);
839
840                   if Present (F) then
841                      if Ekind (Etype (F)) = E_Anonymous_Access_Type
842                        and then
843                          Designated_Type (Etype (F)) =
844                            Class_Wide_Type (RTE (RE_Root_Stream_Type))
845                      then
846                         Next_Formal (F);
847                         Ok :=  Present (F)
848                           and then Parameter_Mode (F) = E_Out_Parameter
849                           and then Base_Type (Etype (F)) = Base_Type (Ent)
850                           and then No (Next_Formal (F));
851                      end if;
852                   end if;
853                end if;
854
855                return Ok;
856             end Has_Good_Profile;
857
858          --  Start of processing for Read attribute definition
859
860          begin
861             FOnly := True;
862
863             if not Is_Type (U_Ent) then
864                Error_Msg_N ("local name must be a subtype", Nam);
865                return;
866
867             else
868                Pnam := TSS (Base_Type (U_Ent), Name_uRead);
869
870                if Present (Pnam)
871                  and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
872                    = Base_Type (U_Ent)
873                then
874                   Error_Msg_Sloc := Sloc (Pnam);
875                   Error_Msg_N ("read attribute already defined #", Nam);
876                   return;
877                end if;
878             end if;
879
880             Analyze (Expr);
881
882             if Is_Entity_Name (Expr) then
883                if not Is_Overloaded (Expr) then
884                   if Has_Good_Profile (Entity (Expr)) then
885                      Subp := Entity (Expr);
886                   end if;
887
888                else
889                   Get_First_Interp (Expr, I, It);
890
891                   while Present (It.Nam) loop
892                      if Has_Good_Profile (It.Nam) then
893                         Subp := It.Nam;
894                         exit;
895                      end if;
896
897                      Get_Next_Interp (I, It);
898                   end loop;
899                end if;
900             end if;
901
902             if Present (Subp) then
903                Set_Entity (Expr, Subp);
904                Set_Etype (Expr, Etype (Subp));
905                New_Stream_Procedure (N, U_Ent, Subp, Name_uRead, True);
906             else
907                Error_Msg_N ("incorrect expression for read attribute", Expr);
908                return;
909             end if;
910          end Read;
911
912          ----------
913          -- Size --
914          ----------
915
916          --  Size attribute definition clause
917
918          when Attribute_Size => Size : declare
919             Size   : constant Uint := Static_Integer (Expr);
920             Etyp   : Entity_Id;
921             Biased : Boolean;
922
923          begin
924             FOnly := True;
925
926             if Has_Size_Clause (U_Ent) then
927                Error_Msg_N ("size already given for &", Nam);
928
929             elsif not Is_Type (U_Ent)
930               and then Ekind (U_Ent) /= E_Variable
931               and then Ekind (U_Ent) /= E_Constant
932             then
933                Error_Msg_N ("size cannot be given for &", Nam);
934
935             elsif Is_Array_Type (U_Ent)
936               and then not Is_Constrained (U_Ent)
937             then
938                Error_Msg_N
939                  ("size cannot be given for unconstrained array", Nam);
940
941             elsif Size /= No_Uint then
942
943                if Is_Type (U_Ent) then
944                   Etyp := U_Ent;
945                else
946                   Etyp := Etype (U_Ent);
947                end if;
948
949                --  Check size, note that Gigi is in charge of checking
950                --  that the size of an array or record type is OK. Also
951                --  we do not check the size in the ordinary fixed-point
952                --  case, since it is too early to do so (there may be a
953                --  subsequent small clause that affects the size). We can
954                --  check the size if a small clause has already been given.
955
956                if not Is_Ordinary_Fixed_Point_Type (U_Ent)
957                  or else Has_Small_Clause (U_Ent)
958                then
959                   Check_Size (Expr, Etyp, Size, Biased);
960                   Set_Has_Biased_Representation (U_Ent, Biased);
961                end if;
962
963                --  For types set RM_Size and Esize if possible
964
965                if Is_Type (U_Ent) then
966                   Set_RM_Size (U_Ent, Size);
967
968                   --  For scalar types, increase Object_Size to power of 2,
969                   --  but not less than 8 in any case, i.e. byte addressable.
970
971                   if Is_Scalar_Type (U_Ent) then
972                      if Size <= 8 then
973                         Init_Esize (U_Ent, 8);
974                      elsif Size <= 16 then
975                         Init_Esize (U_Ent, 16);
976                      elsif Size <= 32 then
977                         Init_Esize (U_Ent, 32);
978                      else
979                         Set_Esize  (U_Ent, (Size + 63) / 64 * 64);
980                      end if;
981
982                   --  For all other types, object size = value size. The
983                   --  backend will adjust as needed.
984
985                   else
986                      Set_Esize (U_Ent, Size);
987                   end if;
988
989                   Alignment_Check_For_Esize_Change (U_Ent);
990
991                --  For objects, set Esize only
992
993                else
994                   Set_Esize (U_Ent, Size);
995                end if;
996
997                Set_Has_Size_Clause (U_Ent);
998             end if;
999          end Size;
1000
1001          -----------
1002          -- Small --
1003          -----------
1004
1005          --  Small attribute definition clause
1006
1007          when Attribute_Small => Small : declare
1008             Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1009             Small         : Ureal;
1010
1011          begin
1012             Analyze_And_Resolve (Expr, Any_Real);
1013
1014             if Etype (Expr) = Any_Type then
1015                return;
1016
1017             elsif not Is_Static_Expression (Expr) then
1018                Error_Msg_N ("small requires static expression", Expr);
1019                return;
1020
1021             else
1022                Small := Expr_Value_R (Expr);
1023
1024                if Small <= Ureal_0 then
1025                   Error_Msg_N ("small value must be greater than zero", Expr);
1026                   return;
1027                end if;
1028
1029             end if;
1030
1031             if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1032                Error_Msg_N
1033                  ("small requires an ordinary fixed point type", Nam);
1034
1035             elsif Has_Small_Clause (U_Ent) then
1036                Error_Msg_N ("small already given for &", Nam);
1037
1038             elsif Small > Delta_Value (U_Ent) then
1039                Error_Msg_N
1040                  ("small value must not be greater then delta value", Nam);
1041
1042             else
1043                Set_Small_Value (U_Ent, Small);
1044                Set_Small_Value (Implicit_Base, Small);
1045                Set_Has_Small_Clause (U_Ent);
1046                Set_Has_Small_Clause (Implicit_Base);
1047                Set_Has_Non_Standard_Rep (Implicit_Base);
1048             end if;
1049          end Small;
1050
1051          ------------------
1052          -- Storage_Size --
1053          ------------------
1054
1055          --  Storage_Size attribute definition clause
1056
1057          when Attribute_Storage_Size => Storage_Size : declare
1058             Btype : constant Entity_Id := Base_Type (U_Ent);
1059             Sprag : Node_Id;
1060
1061          begin
1062             if Is_Task_Type (U_Ent) then
1063                FOnly := True;
1064             end if;
1065
1066             if not Is_Access_Type (U_Ent)
1067               and then Ekind (U_Ent) /= E_Task_Type
1068             then
1069                Error_Msg_N ("storage size cannot be given for &", Nam);
1070
1071             elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1072                Error_Msg_N
1073                  ("storage size cannot be given for a derived access type",
1074                   Nam);
1075
1076             elsif Has_Storage_Size_Clause (Btype) then
1077                Error_Msg_N ("storage size already given for &", Nam);
1078
1079             else
1080                Analyze_And_Resolve (Expr, Any_Integer);
1081
1082                if Is_Access_Type (U_Ent) then
1083
1084                   if Present (Associated_Storage_Pool (U_Ent)) then
1085                      Error_Msg_N ("storage pool already given for &", Nam);
1086                      return;
1087                   end if;
1088
1089                   if Compile_Time_Known_Value (Expr)
1090                     and then Expr_Value (Expr) = 0
1091                   then
1092                      Set_No_Pool_Assigned (Btype);
1093                   end if;
1094
1095                else -- Is_Task_Type (U_Ent)
1096                   Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1097
1098                   if Present (Sprag) then
1099                      Error_Msg_Sloc := Sloc (Sprag);
1100                      Error_Msg_N
1101                        ("Storage_Size already specified#", Nam);
1102                      return;
1103                   end if;
1104                end if;
1105
1106                Set_Has_Storage_Size_Clause (Btype);
1107             end if;
1108          end Storage_Size;
1109
1110          ------------------
1111          -- Storage_Pool --
1112          ------------------
1113
1114          --  Storage_Pool attribute definition clause
1115
1116          when Attribute_Storage_Pool => Storage_Pool : declare
1117             Pool : Entity_Id;
1118
1119          begin
1120             if Ekind (U_Ent) /= E_Access_Type
1121               and then Ekind (U_Ent) /= E_General_Access_Type
1122             then
1123                Error_Msg_N (
1124                  "storage pool can only be given for access types", Nam);
1125                return;
1126
1127             elsif Is_Derived_Type (U_Ent) then
1128                Error_Msg_N
1129                  ("storage pool cannot be given for a derived access type",
1130                   Nam);
1131
1132             elsif Has_Storage_Size_Clause (U_Ent) then
1133                Error_Msg_N ("storage size already given for &", Nam);
1134                return;
1135
1136             elsif Present (Associated_Storage_Pool (U_Ent)) then
1137                Error_Msg_N ("storage pool already given for &", Nam);
1138                return;
1139             end if;
1140
1141             Analyze_And_Resolve
1142               (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1143
1144             --  If the argument is a name that is not an entity name, then
1145             --  we construct a renaming operation to define an entity of
1146             --  type storage pool.
1147
1148             if not Is_Entity_Name (Expr)
1149               and then Is_Object_Reference (Expr)
1150             then
1151                Pool :=
1152                  Make_Defining_Identifier (Loc,
1153                    Chars => New_Internal_Name ('P'));
1154
1155                declare
1156                   Rnode : constant Node_Id :=
1157                             Make_Object_Renaming_Declaration (Loc,
1158                               Defining_Identifier => Pool,
1159                               Subtype_Mark        =>
1160                                 New_Occurrence_Of (Etype (Expr), Loc),
1161                               Name => Expr);
1162
1163                begin
1164                   Insert_Before (N, Rnode);
1165                   Analyze (Rnode);
1166                   Set_Associated_Storage_Pool (U_Ent, Pool);
1167                end;
1168
1169             elsif Is_Entity_Name (Expr) then
1170                Pool := Entity (Expr);
1171
1172                --  If pool is a renamed object, get original one. This can
1173                --  happen with an explicit renaming, and within instances.
1174
1175                while Present (Renamed_Object (Pool))
1176                  and then Is_Entity_Name (Renamed_Object (Pool))
1177                loop
1178                   Pool := Entity (Renamed_Object (Pool));
1179                end loop;
1180
1181                if Present (Renamed_Object (Pool))
1182                  and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1183                  and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1184                then
1185                   Pool := Entity (Expression (Renamed_Object (Pool)));
1186                end if;
1187
1188                if Present (Etype (Pool))
1189                  and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1190                  and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1191                then
1192                   Set_Associated_Storage_Pool (U_Ent, Pool);
1193                else
1194                   Error_Msg_N ("Non sharable GNAT Pool", Expr);
1195                end if;
1196
1197             --  The pool may be specified as the Storage_Pool of some other
1198             --  type. It is rewritten as a class_wide conversion of the
1199             --  corresponding pool entity.
1200
1201             elsif Nkind (Expr) = N_Type_Conversion
1202               and then Is_Entity_Name (Expression (Expr))
1203               and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1204             then
1205                Pool := Entity (Expression (Expr));
1206
1207                if Present (Etype (Pool))
1208                  and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1209                  and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1210                then
1211                   Set_Associated_Storage_Pool (U_Ent, Pool);
1212                else
1213                   Error_Msg_N ("Non sharable GNAT Pool", Expr);
1214                end if;
1215
1216             else
1217                Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1218                return;
1219             end if;
1220          end Storage_Pool;
1221
1222          ----------------
1223          -- Value_Size --
1224          ----------------
1225
1226          --  Value_Size attribute definition clause
1227
1228          when Attribute_Value_Size => Value_Size : declare
1229             Size   : constant Uint := Static_Integer (Expr);
1230             Biased : Boolean;
1231
1232          begin
1233             if not Is_Type (U_Ent) then
1234                Error_Msg_N ("Value_Size cannot be given for &", Nam);
1235
1236             elsif Present
1237                    (Get_Attribute_Definition_Clause
1238                      (U_Ent, Attribute_Value_Size))
1239             then
1240                Error_Msg_N ("Value_Size already given for &", Nam);
1241
1242             else
1243                if Is_Elementary_Type (U_Ent) then
1244                   Check_Size (Expr, U_Ent, Size, Biased);
1245                   Set_Has_Biased_Representation (U_Ent, Biased);
1246                end if;
1247
1248                Set_RM_Size (U_Ent, Size);
1249             end if;
1250          end Value_Size;
1251
1252          -----------
1253          -- Write --
1254          -----------
1255
1256          --  Write attribute definition clause
1257          --  check for class-wide case will be performed later
1258
1259          when Attribute_Write => Write : declare
1260             Subp : Entity_Id := Empty;
1261             I    : Interp_Index;
1262             It   : Interp;
1263             Pnam : Entity_Id;
1264
1265             function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1266             --  Return true if the entity is a procedure with an
1267             --  appropriate profile for the write attribute.
1268
1269             function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1270                F     : Entity_Id;
1271                Ok    : Boolean := False;
1272
1273             begin
1274                if Ekind (Subp) = E_Procedure then
1275                   F := First_Formal (Subp);
1276
1277                   if Present (F) then
1278                      if Ekind (Etype (F)) = E_Anonymous_Access_Type
1279                        and then
1280                          Designated_Type (Etype (F)) =
1281                            Class_Wide_Type (RTE (RE_Root_Stream_Type))
1282                      then
1283                         Next_Formal (F);
1284                         Ok :=  Present (F)
1285                           and then Parameter_Mode (F) = E_In_Parameter
1286                           and then Base_Type (Etype (F)) = Base_Type (Ent)
1287                           and then No (Next_Formal (F));
1288                      end if;
1289                   end if;
1290                end if;
1291
1292                return Ok;
1293             end Has_Good_Profile;
1294
1295          --  Start of processing for Write attribute definition
1296
1297          begin
1298             FOnly := True;
1299
1300             if not Is_Type (U_Ent) then
1301                Error_Msg_N ("local name must be a subtype", Nam);
1302                return;
1303             end if;
1304
1305             Pnam := TSS (Base_Type (U_Ent), Name_uWrite);
1306
1307             if Present (Pnam)
1308               and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
1309                 = Base_Type (U_Ent)
1310             then
1311                Error_Msg_Sloc := Sloc (Pnam);
1312                Error_Msg_N ("write attribute already defined #", Nam);
1313                return;
1314             end if;
1315
1316             Analyze (Expr);
1317
1318             if Is_Entity_Name (Expr) then
1319                if not Is_Overloaded (Expr) then
1320                   if Has_Good_Profile (Entity (Expr)) then
1321                      Subp := Entity (Expr);
1322                   end if;
1323
1324                else
1325                   Get_First_Interp (Expr, I, It);
1326
1327                   while Present (It.Nam) loop
1328                      if Has_Good_Profile (It.Nam) then
1329                         Subp := It.Nam;
1330                         exit;
1331                      end if;
1332
1333                      Get_Next_Interp (I, It);
1334                   end loop;
1335                end if;
1336             end if;
1337
1338             if Present (Subp) then
1339                Set_Entity (Expr, Subp);
1340                Set_Etype (Expr, Etype (Subp));
1341                New_Stream_Procedure (N, U_Ent, Subp, Name_uWrite);
1342             else
1343                Error_Msg_N ("incorrect expression for write attribute", Expr);
1344                return;
1345             end if;
1346          end Write;
1347
1348          --  All other attributes cannot be set
1349
1350          when others =>
1351             Error_Msg_N
1352               ("attribute& cannot be set with definition clause", N);
1353
1354       end case;
1355
1356       --  The test for the type being frozen must be performed after
1357       --  any expression the clause has been analyzed since the expression
1358       --  itself might cause freezing that makes the clause illegal.
1359
1360       if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1361          return;
1362       end if;
1363    end Analyze_Attribute_Definition_Clause;
1364
1365    ----------------------------
1366    -- Analyze_Code_Statement --
1367    ----------------------------
1368
1369    procedure Analyze_Code_Statement (N : Node_Id) is
1370       HSS   : constant Node_Id   := Parent (N);
1371       SBody : constant Node_Id   := Parent (HSS);
1372       Subp  : constant Entity_Id := Current_Scope;
1373       Stmt  : Node_Id;
1374       Decl  : Node_Id;
1375       StmtO : Node_Id;
1376       DeclO : Node_Id;
1377
1378    begin
1379       --  Analyze and check we get right type, note that this implements the
1380       --  requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1381       --  is the only way that Asm_Insn could possibly be visible.
1382
1383       Analyze_And_Resolve (Expression (N));
1384
1385       if Etype (Expression (N)) = Any_Type then
1386          return;
1387       elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1388          Error_Msg_N ("incorrect type for code statement", N);
1389          return;
1390       end if;
1391
1392       --  Make sure we appear in the handled statement sequence of a
1393       --  subprogram (RM 13.8(3)).
1394
1395       if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1396         or else Nkind (SBody) /= N_Subprogram_Body
1397       then
1398          Error_Msg_N
1399            ("code statement can only appear in body of subprogram", N);
1400          return;
1401       end if;
1402
1403       --  Do remaining checks (RM 13.8(3)) if not already done
1404
1405       if not Is_Machine_Code_Subprogram (Subp) then
1406          Set_Is_Machine_Code_Subprogram (Subp);
1407
1408          --  No exception handlers allowed
1409
1410          if Present (Exception_Handlers (HSS)) then
1411             Error_Msg_N
1412               ("exception handlers not permitted in machine code subprogram",
1413                First (Exception_Handlers (HSS)));
1414          end if;
1415
1416          --  No declarations other than use clauses and pragmas (we allow
1417          --  certain internally generated declarations as well).
1418
1419          Decl := First (Declarations (SBody));
1420          while Present (Decl) loop
1421             DeclO := Original_Node (Decl);
1422             if Comes_From_Source (DeclO)
1423               and then Nkind (DeclO) /= N_Pragma
1424               and then Nkind (DeclO) /= N_Use_Package_Clause
1425               and then Nkind (DeclO) /= N_Use_Type_Clause
1426               and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1427             then
1428                Error_Msg_N
1429                  ("this declaration not allowed in machine code subprogram",
1430                   DeclO);
1431             end if;
1432
1433             Next (Decl);
1434          end loop;
1435
1436          --  No statements other than code statements, pragmas, and labels.
1437          --  Again we allow certain internally generated statements.
1438
1439          Stmt := First (Statements (HSS));
1440          while Present (Stmt) loop
1441             StmtO := Original_Node (Stmt);
1442             if Comes_From_Source (StmtO)
1443               and then Nkind (StmtO) /= N_Pragma
1444               and then Nkind (StmtO) /= N_Label
1445               and then Nkind (StmtO) /= N_Code_Statement
1446             then
1447                Error_Msg_N
1448                  ("this statement is not allowed in machine code subprogram",
1449                   StmtO);
1450             end if;
1451
1452             Next (Stmt);
1453          end loop;
1454       end if;
1455
1456    end Analyze_Code_Statement;
1457
1458    -----------------------------------------------
1459    -- Analyze_Enumeration_Representation_Clause --
1460    -----------------------------------------------
1461
1462    procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1463       Ident    : constant Node_Id    := Identifier (N);
1464       Aggr     : constant Node_Id    := Array_Aggregate (N);
1465       Enumtype : Entity_Id;
1466       Elit     : Entity_Id;
1467       Expr     : Node_Id;
1468       Assoc    : Node_Id;
1469       Choice   : Node_Id;
1470       Val      : Uint;
1471       Err      : Boolean := False;
1472
1473       Lo  : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1474       Hi  : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1475       Min : Uint;
1476       Max : Uint;
1477
1478    begin
1479       --  First some basic error checks
1480
1481       Find_Type (Ident);
1482       Enumtype := Entity (Ident);
1483
1484       if Enumtype = Any_Type
1485         or else Rep_Item_Too_Early (Enumtype, N)
1486       then
1487          return;
1488       else
1489          Enumtype := Underlying_Type (Enumtype);
1490       end if;
1491
1492       if not Is_Enumeration_Type (Enumtype) then
1493          Error_Msg_NE
1494            ("enumeration type required, found}",
1495             Ident, First_Subtype (Enumtype));
1496          return;
1497       end if;
1498
1499       if Scope (Enumtype) /= Current_Scope then
1500          Error_Msg_N ("type must be declared in this scope", Ident);
1501          return;
1502
1503       elsif not Is_First_Subtype (Enumtype) then
1504          Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1505          return;
1506
1507       elsif Has_Enumeration_Rep_Clause (Enumtype) then
1508          Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1509          return;
1510
1511       elsif Root_Type (Enumtype) = Standard_Character
1512         or else Root_Type (Enumtype) = Standard_Wide_Character
1513       then
1514          Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1515
1516       else
1517          Set_Has_Enumeration_Rep_Clause (Enumtype);
1518          Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1519       end if;
1520
1521       --  Now we process the aggregate. Note that we don't use the normal
1522       --  aggregate code for this purpose, because we don't want any of the
1523       --  normal expansion activities, and a number of special semantic
1524       --  rules apply (including the component type being any integer type)
1525
1526       --  Badent signals that we found some incorrect entries processing
1527       --  the list. The final checks for completeness and ordering are
1528       --  skipped in this case.
1529
1530       Elit := First_Literal (Enumtype);
1531
1532       --  First the positional entries if any
1533
1534       if Present (Expressions (Aggr)) then
1535          Expr := First (Expressions (Aggr));
1536          while Present (Expr) loop
1537             if No (Elit) then
1538                Error_Msg_N ("too many entries in aggregate", Expr);
1539                return;
1540             end if;
1541
1542             Val := Static_Integer (Expr);
1543
1544             if Val = No_Uint then
1545                Err := True;
1546
1547             elsif Val < Lo or else Hi < Val then
1548                Error_Msg_N ("value outside permitted range", Expr);
1549                Err := True;
1550             end if;
1551
1552             Set_Enumeration_Rep (Elit, Val);
1553             Set_Enumeration_Rep_Expr (Elit, Expr);
1554             Next (Expr);
1555             Next (Elit);
1556          end loop;
1557       end if;
1558
1559       --  Now process the named entries if present
1560
1561       if Present (Component_Associations (Aggr)) then
1562          Assoc := First (Component_Associations (Aggr));
1563          while Present (Assoc) loop
1564             Choice := First (Choices (Assoc));
1565
1566             if Present (Next (Choice)) then
1567                Error_Msg_N
1568                  ("multiple choice not allowed here", Next (Choice));
1569                Err := True;
1570             end if;
1571
1572             if Nkind (Choice) = N_Others_Choice then
1573                Error_Msg_N ("others choice not allowed here", Choice);
1574                Err := True;
1575
1576             elsif Nkind (Choice) = N_Range then
1577                --  ??? should allow zero/one element range here
1578                Error_Msg_N ("range not allowed here", Choice);
1579                Err := True;
1580
1581             else
1582                Analyze_And_Resolve (Choice, Enumtype);
1583
1584                if Is_Entity_Name (Choice)
1585                  and then Is_Type (Entity (Choice))
1586                then
1587                   Error_Msg_N ("subtype name not allowed here", Choice);
1588                   Err := True;
1589                   --  ??? should allow static subtype with zero/one entry
1590
1591                elsif Etype (Choice) = Base_Type (Enumtype) then
1592                   if not Is_Static_Expression (Choice) then
1593                      Error_Msg_N
1594                        ("non-static expression used for choice", Choice);
1595                      Err := True;
1596
1597                   else
1598                      Elit := Expr_Value_E (Choice);
1599
1600                      if Present (Enumeration_Rep_Expr (Elit)) then
1601                         Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1602                         Error_Msg_NE
1603                           ("representation for& previously given#",
1604                            Choice, Elit);
1605                         Err := True;
1606                      end if;
1607
1608                      Set_Enumeration_Rep_Expr (Elit, Choice);
1609
1610                      Expr := Expression (Assoc);
1611                      Val := Static_Integer (Expr);
1612
1613                      if Val = No_Uint then
1614                         Err := True;
1615
1616                      elsif Val < Lo or else Hi < Val then
1617                         Error_Msg_N ("value outside permitted range", Expr);
1618                         Err := True;
1619                      end if;
1620
1621                      Set_Enumeration_Rep (Elit, Val);
1622                   end if;
1623                end if;
1624             end if;
1625
1626             Next (Assoc);
1627          end loop;
1628       end if;
1629
1630       --  Aggregate is fully processed. Now we check that a full set of
1631       --  representations was given, and that they are in range and in order.
1632       --  These checks are only done if no other errors occurred.
1633
1634       if not Err then
1635          Min  := No_Uint;
1636          Max  := No_Uint;
1637
1638          Elit := First_Literal (Enumtype);
1639          while Present (Elit) loop
1640             if No (Enumeration_Rep_Expr (Elit)) then
1641                Error_Msg_NE ("missing representation for&!", N, Elit);
1642
1643             else
1644                Val := Enumeration_Rep (Elit);
1645
1646                if Min = No_Uint then
1647                   Min := Val;
1648                end if;
1649
1650                if Val /= No_Uint then
1651                   if Max /= No_Uint and then Val <= Max then
1652                      Error_Msg_NE
1653                        ("enumeration value for& not ordered!",
1654                                        Enumeration_Rep_Expr (Elit), Elit);
1655                   end if;
1656
1657                   Max := Val;
1658                end if;
1659
1660                --  If there is at least one literal whose representation
1661                --  is not equal to the Pos value, then note that this
1662                --  enumeration type has a non-standard representation.
1663
1664                if Val /= Enumeration_Pos (Elit) then
1665                   Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
1666                end if;
1667             end if;
1668
1669             Next (Elit);
1670          end loop;
1671
1672          --  Now set proper size information
1673
1674          declare
1675             Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
1676
1677          begin
1678             if Has_Size_Clause (Enumtype) then
1679                if Esize (Enumtype) >= Minsize then
1680                   null;
1681
1682                else
1683                   Minsize :=
1684                     UI_From_Int (Minimum_Size (Enumtype, Biased => True));
1685
1686                   if Esize (Enumtype) < Minsize then
1687                      Error_Msg_N ("previously given size is too small", N);
1688
1689                   else
1690                      Set_Has_Biased_Representation (Enumtype);
1691                   end if;
1692                end if;
1693
1694             else
1695                Set_RM_Size    (Enumtype, Minsize);
1696                Set_Enum_Esize (Enumtype);
1697             end if;
1698
1699             Set_RM_Size   (Base_Type (Enumtype), RM_Size   (Enumtype));
1700             Set_Esize     (Base_Type (Enumtype), Esize     (Enumtype));
1701             Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
1702          end;
1703       end if;
1704
1705       --  We repeat the too late test in case it froze itself!
1706
1707       if Rep_Item_Too_Late (Enumtype, N) then
1708          null;
1709       end if;
1710
1711    end Analyze_Enumeration_Representation_Clause;
1712
1713    ----------------------------
1714    -- Analyze_Free_Statement --
1715    ----------------------------
1716
1717    procedure Analyze_Free_Statement (N : Node_Id) is
1718    begin
1719       Analyze (Expression (N));
1720    end Analyze_Free_Statement;
1721
1722    ------------------------------------------
1723    -- Analyze_Record_Representation_Clause --
1724    ------------------------------------------
1725
1726    procedure Analyze_Record_Representation_Clause (N : Node_Id) is
1727       Loc     : constant Source_Ptr := Sloc (N);
1728       Ident   : constant Node_Id    := Identifier (N);
1729       Rectype : Entity_Id;
1730       Fent    : Entity_Id;
1731       CC      : Node_Id;
1732       Posit   : Uint;
1733       Fbit    : Uint;
1734       Lbit    : Uint;
1735       Hbit    : Uint := Uint_0;
1736       Comp    : Entity_Id;
1737       Ocomp   : Entity_Id;
1738       Biased  : Boolean;
1739
1740       Max_Bit_So_Far : Uint;
1741       --  Records the maximum bit position so far. If all field positoins
1742       --  are monotonically increasing, then we can skip the circuit for
1743       --  checking for overlap, since no overlap is possible.
1744
1745       Overlap_Check_Required : Boolean;
1746       --  Used to keep track of whether or not an overlap check is required
1747
1748       Ccount : Natural := 0;
1749       --  Number of component clauses in record rep clause
1750
1751    begin
1752       Find_Type (Ident);
1753       Rectype := Entity (Ident);
1754
1755       if Rectype = Any_Type
1756         or else Rep_Item_Too_Early (Rectype, N)
1757       then
1758          return;
1759       else
1760          Rectype := Underlying_Type (Rectype);
1761       end if;
1762
1763       --  First some basic error checks
1764
1765       if not Is_Record_Type (Rectype) then
1766          Error_Msg_NE
1767            ("record type required, found}", Ident, First_Subtype (Rectype));
1768          return;
1769
1770       elsif Is_Unchecked_Union (Rectype) then
1771          Error_Msg_N
1772            ("record rep clause not allowed for Unchecked_Union", N);
1773
1774       elsif Scope (Rectype) /= Current_Scope then
1775          Error_Msg_N ("type must be declared in this scope", N);
1776          return;
1777
1778       elsif not Is_First_Subtype (Rectype) then
1779          Error_Msg_N ("cannot give record rep clause for subtype", N);
1780          return;
1781
1782       elsif Has_Record_Rep_Clause (Rectype) then
1783          Error_Msg_N ("duplicate record rep clause ignored", N);
1784          return;
1785
1786       elsif Rep_Item_Too_Late (Rectype, N) then
1787          return;
1788       end if;
1789
1790       if Present (Mod_Clause (N)) then
1791          declare
1792             Loc     : constant Source_Ptr := Sloc (N);
1793             M       : constant Node_Id := Mod_Clause (N);
1794             P       : constant List_Id := Pragmas_Before (M);
1795             Mod_Val : Uint;
1796             AtM_Nod : Node_Id;
1797
1798          begin
1799             if Present (P) then
1800                Analyze_List (P);
1801             end if;
1802
1803             --  In Tree_Output mode, expansion is disabled, but we must
1804             --  convert the Mod clause into an alignment clause anyway, so
1805             --  that the back-end can compute and back-annotate properly the
1806             --  size and alignment of types that may include this record.
1807
1808             if Operating_Mode = Check_Semantics
1809               and then Tree_Output
1810             then
1811                AtM_Nod :=
1812                  Make_Attribute_Definition_Clause (Loc,
1813                    Name       => New_Reference_To (Base_Type (Rectype), Loc),
1814                    Chars      => Name_Alignment,
1815                    Expression => Relocate_Node (Expression (M)));
1816
1817                Set_From_At_Mod (AtM_Nod);
1818                Insert_After (N, AtM_Nod);
1819                Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
1820                Set_Mod_Clause (N, Empty);
1821
1822             else
1823                --  Get the alignment value to perform error checking
1824
1825                Mod_Val := Get_Alignment_Value (Expression (M));
1826
1827             end if;
1828          end;
1829       end if;
1830
1831       --  Clear any existing component clauses for the type (this happens
1832       --  with derived types, where we are now overriding the original)
1833
1834       Fent := First_Entity (Rectype);
1835
1836       Comp := Fent;
1837       while Present (Comp) loop
1838          if Ekind (Comp) = E_Component
1839            or else Ekind (Comp) = E_Discriminant
1840          then
1841             Set_Component_Clause (Comp, Empty);
1842          end if;
1843
1844          Next_Entity (Comp);
1845       end loop;
1846
1847       --  All done if no component clauses
1848
1849       CC := First (Component_Clauses (N));
1850
1851       if No (CC) then
1852          return;
1853       end if;
1854
1855       --  If a tag is present, then create a component clause that places
1856       --  it at the start of the record (otherwise gigi may place it after
1857       --  other fields that have rep clauses).
1858
1859       if Nkind (Fent) = N_Defining_Identifier
1860         and then Chars (Fent) = Name_uTag
1861       then
1862          Set_Component_Bit_Offset    (Fent, Uint_0);
1863          Set_Normalized_Position     (Fent, Uint_0);
1864          Set_Normalized_First_Bit    (Fent, Uint_0);
1865          Set_Normalized_Position_Max (Fent, Uint_0);
1866          Init_Esize                  (Fent, System_Address_Size);
1867
1868          Set_Component_Clause    (Fent,
1869            Make_Component_Clause (Loc,
1870              Component_Name =>
1871                Make_Identifier (Loc,
1872                  Chars => Name_uTag),
1873
1874              Position  =>
1875                Make_Integer_Literal (Loc,
1876                  Intval => Uint_0),
1877
1878              First_Bit =>
1879                Make_Integer_Literal (Loc,
1880                  Intval => Uint_0),
1881
1882              Last_Bit  =>
1883                Make_Integer_Literal (Loc,
1884                  UI_From_Int (System_Address_Size))));
1885
1886          Ccount := Ccount + 1;
1887       end if;
1888
1889       Set_Has_Record_Rep_Clause (Rectype);
1890       Set_Has_Specified_Layout  (Rectype);
1891
1892       --  A representation like this applies to the base type as well
1893
1894       Set_Has_Record_Rep_Clause (Base_Type (Rectype));
1895       Set_Has_Non_Standard_Rep  (Base_Type (Rectype));
1896       Set_Has_Specified_Layout  (Base_Type (Rectype));
1897
1898       Max_Bit_So_Far := Uint_Minus_1;
1899       Overlap_Check_Required := False;
1900
1901       --  Process the component clauses
1902
1903       while Present (CC) loop
1904
1905          --  If pragma, just analyze it
1906
1907          if Nkind (CC) = N_Pragma then
1908             Analyze (CC);
1909
1910          --  Processing for real component clause
1911
1912          else
1913             Ccount := Ccount + 1;
1914             Posit := Static_Integer (Position  (CC));
1915             Fbit  := Static_Integer (First_Bit (CC));
1916             Lbit  := Static_Integer (Last_Bit  (CC));
1917
1918             if Posit /= No_Uint
1919               and then Fbit /= No_Uint
1920               and then Lbit /= No_Uint
1921             then
1922                if Posit < 0 then
1923                   Error_Msg_N
1924                     ("position cannot be negative", Position (CC));
1925
1926                elsif Fbit < 0 then
1927                   Error_Msg_N
1928                     ("first bit cannot be negative", First_Bit (CC));
1929
1930                --  Values look OK, so find the corresponding record component
1931                --  Even though the syntax allows an attribute reference for
1932                --  implementation-defined components, GNAT does not allow the
1933                --  tag to get an explicit position.
1934
1935                elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
1936
1937                   if Attribute_Name (Component_Name (CC)) = Name_Tag then
1938                      Error_Msg_N ("position of tag cannot be specified", CC);
1939                   else
1940                      Error_Msg_N ("illegal component name", CC);
1941                   end if;
1942
1943                else
1944                   Comp := First_Entity (Rectype);
1945                   while Present (Comp) loop
1946                      exit when Chars (Comp) = Chars (Component_Name (CC));
1947                      Next_Entity (Comp);
1948                   end loop;
1949
1950                   if No (Comp) then
1951
1952                      --  Maybe component of base type that is absent from
1953                      --  statically constrained first subtype.
1954
1955                      Comp := First_Entity (Base_Type (Rectype));
1956                      while Present (Comp) loop
1957                         exit when Chars (Comp) = Chars (Component_Name (CC));
1958                         Next_Entity (Comp);
1959                      end loop;
1960                   end if;
1961
1962                   if No (Comp) then
1963                      Error_Msg_N
1964                        ("component clause is for non-existent field", CC);
1965
1966                   elsif Present (Component_Clause (Comp)) then
1967                      Error_Msg_Sloc := Sloc (Component_Clause (Comp));
1968                      Error_Msg_N
1969                        ("component clause previously given#", CC);
1970
1971                   else
1972                      --  Update Fbit and Lbit to the actual bit number.
1973
1974                      Fbit := Fbit + UI_From_Int (SSU) * Posit;
1975                      Lbit := Lbit + UI_From_Int (SSU) * Posit;
1976
1977                      if Fbit <= Max_Bit_So_Far then
1978                         Overlap_Check_Required := True;
1979                      else
1980                         Max_Bit_So_Far := Lbit;
1981                      end if;
1982
1983                      if Has_Size_Clause (Rectype)
1984                        and then Esize (Rectype) <= Lbit
1985                      then
1986                         Error_Msg_N
1987                           ("bit number out of range of specified size",
1988                            Last_Bit (CC));
1989                      else
1990                         Set_Component_Clause     (Comp, CC);
1991                         Set_Component_Bit_Offset (Comp, Fbit);
1992                         Set_Esize                (Comp, 1 + (Lbit - Fbit));
1993                         Set_Normalized_First_Bit (Comp, Fbit mod SSU);
1994                         Set_Normalized_Position  (Comp, Fbit / SSU);
1995
1996                         Set_Normalized_Position_Max
1997                           (Fent, Normalized_Position (Fent));
1998
1999                         if Is_Tagged_Type (Rectype)
2000                           and then Fbit < System_Address_Size
2001                         then
2002                            Error_Msg_NE
2003                              ("component overlaps tag field of&",
2004                               CC, Rectype);
2005                         end if;
2006
2007                         --  Test for large object that is not on a byte
2008                         --  boundary, defined as a large packed array not
2009                         --  represented by a modular type, or an object for
2010                         --  which a size of greater than 64 bits is specified.
2011
2012                         if Fbit mod SSU /= 0 then
2013                            if (Is_Packed_Array_Type (Etype (Comp))
2014                                 and then Is_Array_Type
2015                                      (Packed_Array_Type (Etype (Comp))))
2016                              or else Esize (Etype (Comp)) > 64
2017                            then
2018                               Error_Msg_N
2019                                 ("large component must be on byte boundary",
2020                                  First_Bit (CC));
2021                            end if;
2022                         end if;
2023
2024                         --  This information is also set in the
2025                         --  corresponding component of the base type,
2026                         --  found by accessing the Original_Record_Component
2027                         --  link if it is present.
2028
2029                         Ocomp := Original_Record_Component (Comp);
2030
2031                         if Hbit < Lbit then
2032                            Hbit := Lbit;
2033                         end if;
2034
2035                         Check_Size
2036                           (Component_Name (CC),
2037                            Etype (Comp),
2038                            Esize (Comp),
2039                            Biased);
2040
2041                         Set_Has_Biased_Representation (Comp, Biased);
2042
2043                         if Present (Ocomp) then
2044                            Set_Component_Clause     (Ocomp, CC);
2045                            Set_Component_Bit_Offset (Ocomp, Fbit);
2046                            Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2047                            Set_Normalized_Position  (Ocomp, Fbit / SSU);
2048                            Set_Esize                (Ocomp, 1 + (Lbit - Fbit));
2049
2050                            Set_Normalized_Position_Max
2051                              (Ocomp, Normalized_Position (Ocomp));
2052
2053                            Set_Has_Biased_Representation
2054                              (Ocomp, Has_Biased_Representation (Comp));
2055                         end if;
2056
2057                         if Esize (Comp) < 0 then
2058                            Error_Msg_N ("component size is negative", CC);
2059                         end if;
2060                      end if;
2061                   end if;
2062                end if;
2063             end if;
2064          end if;
2065
2066          Next (CC);
2067       end loop;
2068
2069       --  Now that we have processed all the component clauses, check for
2070       --  overlap. We have to leave this till last, since the components
2071       --  can appear in any arbitrary order in the representation clause.
2072
2073       --  We do not need this check if all specified ranges were monotonic,
2074       --  as recorded by Overlap_Check_Required being False at this stage.
2075
2076       --  This first section checks if there are any overlapping entries
2077       --  at all. It does this by sorting all entries and then seeing if
2078       --  there are any overlaps. If there are none, then that is decisive,
2079       --  but if there are overlaps, they may still be OK (they may result
2080       --  from fields in different variants).
2081
2082       if Overlap_Check_Required then
2083          Overlap_Check1 : declare
2084
2085             OC_Fbit : array (0 .. Ccount) of Uint;
2086             --  First-bit values for component clauses, the value is the
2087             --  offset of the first bit of the field from start of record.
2088             --  The zero entry is for use in sorting.
2089
2090             OC_Lbit : array (0 .. Ccount) of Uint;
2091             --  Last-bit values for component clauses, the value is the
2092             --  offset of the last bit of the field from start of record.
2093             --  The zero entry is for use in sorting.
2094
2095             OC_Count : Natural := 0;
2096             --  Count of entries in OC_Fbit and OC_Lbit
2097
2098             function OC_Lt (Op1, Op2 : Natural) return Boolean;
2099             --  Compare routine for Sort (See GNAT.Heap_Sort_A)
2100
2101             procedure OC_Move (From : Natural; To : Natural);
2102             --  Move routine for Sort (see GNAT.Heap_Sort_A)
2103
2104             function OC_Lt (Op1, Op2 : Natural) return Boolean is
2105             begin
2106                return OC_Fbit (Op1) < OC_Fbit (Op2);
2107             end OC_Lt;
2108
2109             procedure OC_Move (From : Natural; To : Natural) is
2110             begin
2111                OC_Fbit (To) := OC_Fbit (From);
2112                OC_Lbit (To) := OC_Lbit (From);
2113             end OC_Move;
2114
2115          begin
2116             CC := First (Component_Clauses (N));
2117             while Present (CC) loop
2118                if Nkind (CC) /= N_Pragma then
2119                   Posit := Static_Integer (Position  (CC));
2120                   Fbit  := Static_Integer (First_Bit (CC));
2121                   Lbit  := Static_Integer (Last_Bit  (CC));
2122
2123                   if Posit /= No_Uint
2124                     and then Fbit /= No_Uint
2125                     and then Lbit /= No_Uint
2126                   then
2127                      OC_Count := OC_Count + 1;
2128                      Posit := Posit * SSU;
2129                      OC_Fbit (OC_Count) := Fbit + Posit;
2130                      OC_Lbit (OC_Count) := Lbit + Posit;
2131                   end if;
2132                end if;
2133
2134                Next (CC);
2135             end loop;
2136
2137             Sort
2138               (OC_Count,
2139                OC_Move'Unrestricted_Access,
2140                OC_Lt'Unrestricted_Access);
2141
2142             Overlap_Check_Required := False;
2143             for J in 1 .. OC_Count - 1 loop
2144                if OC_Lbit (J) >= OC_Fbit (J + 1) then
2145                   Overlap_Check_Required := True;
2146                   exit;
2147                end if;
2148             end loop;
2149          end Overlap_Check1;
2150       end if;
2151
2152       --  If Overlap_Check_Required is still True, then we have to do
2153       --  the full scale overlap check, since we have at least two fields
2154       --  that do overlap, and we need to know if that is OK since they
2155       --  are in the same variant, or whether we have a definite problem
2156
2157       if Overlap_Check_Required then
2158          Overlap_Check2 : declare
2159             C1_Ent, C2_Ent : Entity_Id;
2160             --  Entities of components being checked for overlap
2161
2162             Clist : Node_Id;
2163             --  Component_List node whose Component_Items are being checked
2164
2165             Citem : Node_Id;
2166             --  Component declaration for component being checked
2167
2168          begin
2169             C1_Ent := First_Entity (Base_Type (Rectype));
2170
2171             --  Loop through all components in record. For each component check
2172             --  for overlap with any of the preceding elements on the component
2173             --  list containing the component, and also, if the component is in
2174             --  a variant, check against components outside the case structure.
2175             --  This latter test is repeated recursively up the variant tree.
2176
2177             Main_Component_Loop : while Present (C1_Ent) loop
2178                if Ekind (C1_Ent) /= E_Component
2179                  and then Ekind (C1_Ent) /= E_Discriminant
2180                then
2181                   goto Continue_Main_Component_Loop;
2182                end if;
2183
2184                --  Skip overlap check if entity has no declaration node. This
2185                --  happens with discriminants in constrained derived types.
2186                --  Probably we are missing some checks as a result, but that
2187                --  does not seem terribly serious ???
2188
2189                if No (Declaration_Node (C1_Ent)) then
2190                   goto Continue_Main_Component_Loop;
2191                end if;
2192
2193                Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2194
2195                --  Loop through component lists that need checking. Check the
2196                --  current component list and all lists in variants above us.
2197
2198                Component_List_Loop : loop
2199
2200                   --  If derived type definition, go to full declaration
2201                   --  If at outer level, check discriminants if there are any
2202
2203                   if Nkind (Clist) = N_Derived_Type_Definition then
2204                      Clist := Parent (Clist);
2205                   end if;
2206
2207                   --  Outer level of record definition, check discriminants
2208
2209                   if Nkind (Clist) = N_Full_Type_Declaration
2210                     or else Nkind (Clist) = N_Private_Type_Declaration
2211                   then
2212                      if Has_Discriminants (Defining_Identifier (Clist)) then
2213                         C2_Ent :=
2214                           First_Discriminant (Defining_Identifier (Clist));
2215
2216                         while Present (C2_Ent) loop
2217                            exit when C1_Ent = C2_Ent;
2218                            Check_Component_Overlap (C1_Ent, C2_Ent);
2219                            Next_Discriminant (C2_Ent);
2220                         end loop;
2221                      end if;
2222
2223                   --  Record extension case
2224
2225                   elsif Nkind (Clist) = N_Derived_Type_Definition then
2226                      Clist := Empty;
2227
2228                   --  Otherwise check one component list
2229
2230                   else
2231                      Citem := First (Component_Items (Clist));
2232
2233                      while Present (Citem) loop
2234                         if Nkind (Citem) = N_Component_Declaration then
2235                            C2_Ent := Defining_Identifier (Citem);
2236                            exit when C1_Ent = C2_Ent;
2237                            Check_Component_Overlap (C1_Ent, C2_Ent);
2238                         end if;
2239
2240                         Next (Citem);
2241                      end loop;
2242                   end if;
2243
2244                   --  Check for variants above us (the parent of the Clist can
2245                   --  be a variant, in which case its parent is a variant part,
2246                   --  and the parent of the variant part is a component list
2247                   --  whose components must all be checked against the current
2248                   --  component for overlap.
2249
2250                   if Nkind (Parent (Clist)) = N_Variant then
2251                      Clist := Parent (Parent (Parent (Clist)));
2252
2253                   --  Check for possible discriminant part in record, this is
2254                   --  treated essentially as another level in the recursion.
2255                   --  For this case we have the parent of the component list
2256                   --  is the record definition, and its parent is the full
2257                   --  type declaration which contains the discriminant
2258                   --  specifications.
2259
2260                   elsif Nkind (Parent (Clist)) = N_Record_Definition then
2261                      Clist := Parent (Parent ((Clist)));
2262
2263                   --  If neither of these two cases, we are at the top of
2264                   --  the tree
2265
2266                   else
2267                      exit Component_List_Loop;
2268                   end if;
2269                end loop Component_List_Loop;
2270
2271                <<Continue_Main_Component_Loop>>
2272                   Next_Entity (C1_Ent);
2273
2274             end loop Main_Component_Loop;
2275          end Overlap_Check2;
2276       end if;
2277
2278       --  For records that have component clauses for all components, and
2279       --  whose size is less than or equal to 32, we need to know the size
2280       --  in the front end to activate possible packed array processing
2281       --  where the component type is a record.
2282
2283       --  At this stage Hbit + 1 represents the first unused bit from all
2284       --  the component clauses processed, so if the component clauses are
2285       --  complete, then this is the length of the record.
2286
2287       --  For records longer than System.Storage_Unit, and for those where
2288       --  not all components have component clauses, the back end determines
2289       --  the length (it may for example be appopriate to round up the size
2290       --  to some convenient boundary, based on alignment considerations etc).
2291
2292       if Unknown_RM_Size (Rectype)
2293         and then Hbit + 1 <= 32
2294       then
2295          --  Nothing to do if at least one component with no component clause
2296
2297          Comp := First_Entity (Rectype);
2298          while Present (Comp) loop
2299             if Ekind (Comp) = E_Component
2300               or else Ekind (Comp) = E_Discriminant
2301             then
2302                if No (Component_Clause (Comp)) then
2303                   return;
2304                end if;
2305             end if;
2306
2307             Next_Entity (Comp);
2308          end loop;
2309
2310          --  If we fall out of loop, all components have component clauses
2311          --  and so we can set the size to the maximum value.
2312
2313          Set_RM_Size (Rectype, Hbit + 1);
2314       end if;
2315
2316    end Analyze_Record_Representation_Clause;
2317
2318    -----------------------------
2319    -- Check_Component_Overlap --
2320    -----------------------------
2321
2322    procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2323    begin
2324       if Present (Component_Clause (C1_Ent))
2325         and then Present (Component_Clause (C2_Ent))
2326       then
2327          --  Exclude odd case where we have two tag fields in the same
2328          --  record, both at location zero. This seems a bit strange,
2329          --  but it seems to happen in some circumstances ???
2330
2331          if Chars (C1_Ent) = Name_uTag
2332            and then Chars (C2_Ent) = Name_uTag
2333          then
2334             return;
2335          end if;
2336
2337          --  Here we check if the two fields overlap
2338
2339          declare
2340             S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2341             S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2342             E1 : constant Uint := S1 + Esize (C1_Ent);
2343             E2 : constant Uint := S2 + Esize (C2_Ent);
2344
2345          begin
2346             if E2 <= S1 or else E1 <= S2 then
2347                null;
2348             else
2349                Error_Msg_Node_2 :=
2350                  Component_Name (Component_Clause (C2_Ent));
2351                Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2352                Error_Msg_Node_1 :=
2353                  Component_Name (Component_Clause (C1_Ent));
2354                Error_Msg_N
2355                  ("component& overlaps & #",
2356                   Component_Name (Component_Clause (C1_Ent)));
2357             end if;
2358          end;
2359       end if;
2360    end Check_Component_Overlap;
2361
2362    -----------------------------------
2363    -- Check_Constant_Address_Clause --
2364    -----------------------------------
2365
2366    procedure Check_Constant_Address_Clause
2367      (Expr  : Node_Id;
2368       U_Ent : Entity_Id)
2369    is
2370       procedure Check_At_Constant_Address (Nod : Node_Id);
2371       --  Checks that the given node N represents a name whose 'Address
2372       --  is constant (in the same sense as OK_Constant_Address_Clause,
2373       --  i.e. the address value is the same at the point of declaration
2374       --  of U_Ent and at the time of elaboration of the address clause.
2375
2376       procedure Check_Expr_Constants (Nod : Node_Id);
2377       --  Checks that Nod meets the requirements for a constant address
2378       --  clause in the sense of the enclosing procedure.
2379
2380       procedure Check_List_Constants (Lst : List_Id);
2381       --  Check that all elements of list Lst meet the requirements for a
2382       --  constant address clause in the sense of the enclosing procedure.
2383
2384       -------------------------------
2385       -- Check_At_Constant_Address --
2386       -------------------------------
2387
2388       procedure Check_At_Constant_Address (Nod : Node_Id) is
2389       begin
2390          if Is_Entity_Name (Nod) then
2391             if Present (Address_Clause (Entity ((Nod)))) then
2392                Error_Msg_NE
2393                  ("invalid address clause for initialized object &!",
2394                            Nod, U_Ent);
2395                Error_Msg_NE
2396                  ("address for& cannot" &
2397                     " depend on another address clause! ('R'M 13.1(22))!",
2398                   Nod, U_Ent);
2399
2400             elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2401               and then Sloc (U_Ent) < Sloc (Entity (Nod))
2402             then
2403                Error_Msg_NE
2404                  ("invalid address clause for initialized object &!",
2405                   Nod, U_Ent);
2406                Error_Msg_Name_1 := Chars (Entity (Nod));
2407                Error_Msg_Name_2 := Chars (U_Ent);
2408                Error_Msg_N
2409                  ("\% must be defined before % ('R'M 13.1(22))!",
2410                   Nod);
2411             end if;
2412
2413          elsif Nkind (Nod) = N_Selected_Component then
2414             declare
2415                T : constant Entity_Id := Etype (Prefix (Nod));
2416
2417             begin
2418                if (Is_Record_Type (T)
2419                     and then Has_Discriminants (T))
2420                  or else
2421                   (Is_Access_Type (T)
2422                      and then Is_Record_Type (Designated_Type (T))
2423                      and then Has_Discriminants (Designated_Type (T)))
2424                then
2425                   Error_Msg_NE
2426                     ("invalid address clause for initialized object &!",
2427                      Nod, U_Ent);
2428                   Error_Msg_N
2429                     ("\address cannot depend on component" &
2430                      " of discriminated record ('R'M 13.1(22))!",
2431                      Nod);
2432                else
2433                   Check_At_Constant_Address (Prefix (Nod));
2434                end if;
2435             end;
2436
2437          elsif Nkind (Nod) = N_Indexed_Component then
2438             Check_At_Constant_Address (Prefix (Nod));
2439             Check_List_Constants (Expressions (Nod));
2440
2441          else
2442             Check_Expr_Constants (Nod);
2443          end if;
2444       end Check_At_Constant_Address;
2445
2446       --------------------------
2447       -- Check_Expr_Constants --
2448       --------------------------
2449
2450       procedure Check_Expr_Constants (Nod : Node_Id) is
2451       begin
2452          if Nkind (Nod) in N_Has_Etype
2453            and then Etype (Nod) = Any_Type
2454          then
2455             return;
2456          end if;
2457
2458          case Nkind (Nod) is
2459             when N_Empty | N_Error =>
2460                return;
2461
2462             when N_Identifier | N_Expanded_Name =>
2463                declare
2464                   Ent       : constant Entity_Id  := Entity (Nod);
2465                   Loc_Ent   : constant Source_Ptr := Sloc (Ent);
2466                   Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2467
2468                begin
2469                   if Ekind (Ent) = E_Named_Integer
2470                        or else
2471                      Ekind (Ent) = E_Named_Real
2472                        or else
2473                      Is_Type (Ent)
2474                   then
2475                      return;
2476
2477                   elsif
2478                      Ekind (Ent) = E_Constant
2479                        or else
2480                      Ekind (Ent) = E_In_Parameter
2481                   then
2482                      --  This is the case where we must have Ent defined
2483                      --  before U_Ent. Clearly if they are in different
2484                      --  units this requirement is met since the unit
2485                      --  containing Ent is already processed.
2486
2487                      if not In_Same_Source_Unit (Ent, U_Ent) then
2488                         return;
2489
2490                      --  Otherwise location of Ent must be before the
2491                      --  location of U_Ent, that's what prior defined means.
2492
2493                      elsif Loc_Ent < Loc_U_Ent then
2494                         return;
2495
2496                      else
2497                         Error_Msg_NE
2498                           ("invalid address clause for initialized object &!",
2499                            Nod, U_Ent);
2500                         Error_Msg_Name_1 := Chars (Ent);
2501                         Error_Msg_Name_2 := Chars (U_Ent);
2502                         Error_Msg_N
2503                           ("\% must be defined before % ('R'M 13.1(22))!",
2504                            Nod);
2505                      end if;
2506
2507                   elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2508                      Check_Expr_Constants (Original_Node (Nod));
2509
2510                   else
2511                      Error_Msg_NE
2512                        ("invalid address clause for initialized object &!",
2513                         Nod, U_Ent);
2514                      Error_Msg_Name_1 := Chars (Ent);
2515                      Error_Msg_N
2516                        ("\reference to variable% not allowed ('R'M 13.1(22))!",
2517                         Nod);
2518                   end if;
2519                end;
2520
2521             when N_Integer_Literal   |
2522                  N_Real_Literal      |
2523                  N_String_Literal    |
2524                  N_Character_Literal =>
2525                return;
2526
2527             when N_Range =>
2528                Check_Expr_Constants (Low_Bound (Nod));
2529                Check_Expr_Constants (High_Bound (Nod));
2530
2531             when N_Explicit_Dereference =>
2532                Check_Expr_Constants (Prefix (Nod));
2533
2534             when N_Indexed_Component =>
2535                Check_Expr_Constants (Prefix (Nod));
2536                Check_List_Constants (Expressions (Nod));
2537
2538             when N_Slice =>
2539                Check_Expr_Constants (Prefix (Nod));
2540                Check_Expr_Constants (Discrete_Range (Nod));
2541
2542             when N_Selected_Component =>
2543                Check_Expr_Constants (Prefix (Nod));
2544
2545             when N_Attribute_Reference =>
2546
2547                if (Attribute_Name (Nod) = Name_Address
2548                     or else
2549                    Attribute_Name (Nod) = Name_Access
2550                     or else
2551                    Attribute_Name (Nod) = Name_Unchecked_Access
2552                     or else
2553                    Attribute_Name (Nod) = Name_Unrestricted_Access)
2554                then
2555                   Check_At_Constant_Address (Prefix (Nod));
2556
2557                else
2558                   Check_Expr_Constants (Prefix (Nod));
2559                   Check_List_Constants (Expressions (Nod));
2560                end if;
2561
2562             when N_Aggregate =>
2563                Check_List_Constants (Component_Associations (Nod));
2564                Check_List_Constants (Expressions (Nod));
2565
2566             when N_Component_Association =>
2567                Check_Expr_Constants (Expression (Nod));
2568
2569             when N_Extension_Aggregate =>
2570                Check_Expr_Constants (Ancestor_Part (Nod));
2571                Check_List_Constants (Component_Associations (Nod));
2572                Check_List_Constants (Expressions (Nod));
2573
2574             when N_Null =>
2575                return;
2576
2577             when N_Binary_Op | N_And_Then | N_Or_Else | N_In | N_Not_In =>
2578                Check_Expr_Constants (Left_Opnd (Nod));
2579                Check_Expr_Constants (Right_Opnd (Nod));
2580
2581             when N_Unary_Op =>
2582                Check_Expr_Constants (Right_Opnd (Nod));
2583
2584             when N_Type_Conversion           |
2585                  N_Qualified_Expression      |
2586                  N_Allocator                 =>
2587                Check_Expr_Constants (Expression (Nod));
2588
2589             when N_Unchecked_Type_Conversion =>
2590                Check_Expr_Constants (Expression (Nod));
2591
2592                --  If this is a rewritten unchecked conversion, subtypes
2593                --  in this node are those created within the instance.
2594                --  To avoid order of elaboration issues, replace them
2595                --  with their base types. Note that address clauses can
2596                --  cause order of elaboration problems because they are
2597                --  elaborated by the back-end at the point of definition,
2598                --  and may mention entities declared in between (as long
2599                --  as everything is static). It is user-friendly to allow
2600                --  unchecked conversions in this context.
2601
2602                if Nkind (Original_Node (Nod)) = N_Function_Call then
2603                   Set_Etype (Expression (Nod),
2604                     Base_Type (Etype (Expression (Nod))));
2605                   Set_Etype (Nod, Base_Type (Etype (Nod)));
2606                end if;
2607
2608             when N_Function_Call =>
2609                if not Is_Pure (Entity (Name (Nod))) then
2610                   Error_Msg_NE
2611                     ("invalid address clause for initialized object &!",
2612                      Nod, U_Ent);
2613
2614                   Error_Msg_NE
2615                     ("\function & is not pure ('R'M 13.1(22))!",
2616                      Nod, Entity (Name (Nod)));
2617
2618                else
2619                   Check_List_Constants (Parameter_Associations (Nod));
2620                end if;
2621
2622             when N_Parameter_Association =>
2623                Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
2624
2625             when others =>
2626                Error_Msg_NE
2627                  ("invalid address clause for initialized object &!",
2628                   Nod, U_Ent);
2629                Error_Msg_NE
2630                  ("\must be constant defined before& ('R'M 13.1(22))!",
2631                   Nod, U_Ent);
2632          end case;
2633       end Check_Expr_Constants;
2634
2635       --------------------------
2636       -- Check_List_Constants --
2637       --------------------------
2638
2639       procedure Check_List_Constants (Lst : List_Id) is
2640          Nod1 : Node_Id;
2641
2642       begin
2643          if Present (Lst) then
2644             Nod1 := First (Lst);
2645             while Present (Nod1) loop
2646                Check_Expr_Constants (Nod1);
2647                Next (Nod1);
2648             end loop;
2649          end if;
2650       end Check_List_Constants;
2651
2652    --  Start of processing for Check_Constant_Address_Clause
2653
2654    begin
2655       Check_Expr_Constants (Expr);
2656    end Check_Constant_Address_Clause;
2657
2658    ----------------
2659    -- Check_Size --
2660    ----------------
2661
2662    procedure Check_Size
2663      (N      : Node_Id;
2664       T      : Entity_Id;
2665       Siz    : Uint;
2666       Biased : out Boolean)
2667    is
2668       UT : constant Entity_Id := Underlying_Type (T);
2669       M  : Uint;
2670
2671    begin
2672       Biased := False;
2673
2674       --  Immediate return if size is same as standard size or if composite
2675       --  item, or generic type, or type with previous errors.
2676
2677       if No (UT)
2678         or else UT = Any_Type
2679         or else Is_Generic_Type (UT)
2680         or else Is_Generic_Type (Root_Type (UT))
2681         or else Is_Composite_Type (UT)
2682         or else (Known_Esize (UT) and then Siz = Esize (UT))
2683       then
2684          return;
2685
2686       --  For fixed-point types, don't check minimum if type is not frozen,
2687       --  since type is not known till then
2688       --  at freeze time.
2689
2690       elsif Is_Fixed_Point_Type (UT)
2691         and then not Is_Frozen (UT)
2692       then
2693          null;
2694
2695       --  Cases for which a minimum check is required
2696
2697       else
2698          M := UI_From_Int (Minimum_Size (UT));
2699
2700          if Siz < M then
2701
2702             --  Size is less than minimum size, but one possibility remains
2703             --  that we can manage with the new size if we bias the type
2704
2705             M := UI_From_Int (Minimum_Size (UT, Biased => True));
2706
2707             if Siz < M then
2708                Error_Msg_Uint_1 := M;
2709                Error_Msg_NE
2710                  ("size for& too small, minimum allowed is ^", N, T);
2711             else
2712                Biased := True;
2713             end if;
2714          end if;
2715       end if;
2716    end Check_Size;
2717
2718    -------------------------
2719    -- Get_Alignment_Value --
2720    -------------------------
2721
2722    function Get_Alignment_Value (Expr : Node_Id) return Uint is
2723       Align : constant Uint := Static_Integer (Expr);
2724
2725    begin
2726       if Align = No_Uint then
2727          return No_Uint;
2728
2729       elsif Align <= 0 then
2730          Error_Msg_N ("alignment value must be positive", Expr);
2731          return No_Uint;
2732
2733       else
2734          for J in Int range 0 .. 64 loop
2735             declare
2736                M : constant Uint := Uint_2 ** J;
2737
2738             begin
2739                exit when M = Align;
2740
2741                if M > Align then
2742                   Error_Msg_N
2743                     ("alignment value must be power of 2", Expr);
2744                   return No_Uint;
2745                end if;
2746             end;
2747          end loop;
2748
2749          return Align;
2750       end if;
2751    end Get_Alignment_Value;
2752
2753    -------------------------------------
2754    -- Get_Attribute_Definition_Clause --
2755    -------------------------------------
2756
2757    function Get_Attribute_Definition_Clause
2758      (E    : Entity_Id;
2759       Id   : Attribute_Id)
2760       return Node_Id
2761    is
2762       N : Node_Id;
2763
2764    begin
2765       N := First_Rep_Item (E);
2766       while Present (N) loop
2767          if Nkind (N) = N_Attribute_Definition_Clause
2768            and then Get_Attribute_Id (Chars (N)) = Id
2769          then
2770             return N;
2771          else
2772             Next_Rep_Item (N);
2773          end if;
2774       end loop;
2775
2776       return Empty;
2777    end Get_Attribute_Definition_Clause;
2778
2779    --------------------
2780    -- Get_Rep_Pragma --
2781    --------------------
2782
2783    function Get_Rep_Pragma (E : Entity_Id; Nam : Name_Id) return Node_Id is
2784       N   : Node_Id;
2785       Typ : Entity_Id;
2786
2787    begin
2788       N := First_Rep_Item (E);
2789
2790       while Present (N) loop
2791          if Nkind (N) = N_Pragma and then Chars (N) = Nam then
2792
2793             if Nam = Name_Stream_Convert then
2794
2795                --  For tagged types this pragma is not inherited, so we
2796                --  must verify that it is defined for the given type and
2797                --  not an ancestor.
2798
2799                Typ := Entity (Expression
2800                        (First (Pragma_Argument_Associations (N))));
2801
2802                if not Is_Tagged_Type (E)
2803                  or else E = Typ
2804                  or else (Is_Private_Type (Typ)
2805                            and then E = Full_View (Typ))
2806                then
2807                   return N;
2808                else
2809                   Next_Rep_Item (N);
2810                end if;
2811
2812             else
2813                return N;
2814             end if;
2815          else
2816             Next_Rep_Item (N);
2817          end if;
2818       end loop;
2819
2820       return Empty;
2821    end Get_Rep_Pragma;
2822
2823    ----------------
2824    -- Initialize --
2825    ----------------
2826
2827    procedure Initialize is
2828    begin
2829       Unchecked_Conversions.Init;
2830    end Initialize;
2831
2832    -------------------------
2833    -- Is_Operational_Item --
2834    -------------------------
2835
2836    function Is_Operational_Item (N : Node_Id) return Boolean is
2837    begin
2838       if Nkind (N) /= N_Attribute_Definition_Clause then
2839          return False;
2840       else
2841          declare
2842             Id    : constant Attribute_Id := Get_Attribute_Id (Chars (N));
2843
2844          begin
2845             return Id = Attribute_Input
2846               or else Id = Attribute_Output
2847               or else Id = Attribute_Read
2848               or else Id = Attribute_Write;
2849          end;
2850       end if;
2851    end Is_Operational_Item;
2852
2853    ------------------
2854    -- Minimum_Size --
2855    ------------------
2856
2857    function Minimum_Size
2858      (T      : Entity_Id;
2859       Biased : Boolean := False)
2860       return   Nat
2861    is
2862       Lo     : Uint    := No_Uint;
2863       Hi     : Uint    := No_Uint;
2864       LoR    : Ureal   := No_Ureal;
2865       HiR    : Ureal   := No_Ureal;
2866       LoSet  : Boolean := False;
2867       HiSet  : Boolean := False;
2868       B      : Uint;
2869       S      : Nat;
2870       Ancest : Entity_Id;
2871
2872    begin
2873       --  If bad type, return 0
2874
2875       if T = Any_Type then
2876          return 0;
2877
2878       --  For generic types, just return zero. There cannot be any legitimate
2879       --  need to know such a size, but this routine may be called with a
2880       --  generic type as part of normal processing.
2881
2882       elsif Is_Generic_Type (Root_Type (T)) then
2883          return 0;
2884
2885       --  Access types
2886
2887       elsif Is_Access_Type (T) then
2888          return System_Address_Size;
2889
2890       --  Floating-point types
2891
2892       elsif Is_Floating_Point_Type (T) then
2893          return UI_To_Int (Esize (Root_Type (T)));
2894
2895       --  Discrete types
2896
2897       elsif Is_Discrete_Type (T) then
2898
2899          --  The following loop is looking for the nearest compile time
2900          --  known bounds following the ancestor subtype chain. The idea
2901          --  is to find the most restrictive known bounds information.
2902
2903          Ancest := T;
2904          loop
2905             if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
2906                return 0;
2907             end if;
2908
2909             if not LoSet then
2910                if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
2911                   Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
2912                   LoSet := True;
2913                   exit when HiSet;
2914                end if;
2915             end if;
2916
2917             if not HiSet then
2918                if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
2919                   Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
2920                   HiSet := True;
2921                   exit when LoSet;
2922                end if;
2923             end if;
2924
2925             Ancest := Ancestor_Subtype (Ancest);
2926
2927             if No (Ancest) then
2928                Ancest := Base_Type (T);
2929
2930                if Is_Generic_Type (Ancest) then
2931                   return 0;
2932                end if;
2933             end if;
2934          end loop;
2935
2936       --  Fixed-point types. We can't simply use Expr_Value to get the
2937       --  Corresponding_Integer_Value values of the bounds, since these
2938       --  do not get set till the type is frozen, and this routine can
2939       --  be called before the type is frozen. Similarly the test for
2940       --  bounds being static needs to include the case where we have
2941       --  unanalyzed real literals for the same reason.
2942
2943       elsif Is_Fixed_Point_Type (T) then
2944
2945          --  The following loop is looking for the nearest compile time
2946          --  known bounds following the ancestor subtype chain. The idea
2947          --  is to find the most restrictive known bounds information.
2948
2949          Ancest := T;
2950          loop
2951             if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
2952                return 0;
2953             end if;
2954
2955             if not LoSet then
2956                if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
2957                  or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
2958                then
2959                   LoR := Expr_Value_R (Type_Low_Bound (Ancest));
2960                   LoSet := True;
2961                   exit when HiSet;
2962                end if;
2963             end if;
2964
2965             if not HiSet then
2966                if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
2967                  or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
2968                then
2969                   HiR := Expr_Value_R (Type_High_Bound (Ancest));
2970                   HiSet := True;
2971                   exit when LoSet;
2972                end if;
2973             end if;
2974
2975             Ancest := Ancestor_Subtype (Ancest);
2976
2977             if No (Ancest) then
2978                Ancest := Base_Type (T);
2979
2980                if Is_Generic_Type (Ancest) then
2981                   return 0;
2982                end if;
2983             end if;
2984          end loop;
2985
2986          Lo := UR_To_Uint (LoR / Small_Value (T));
2987          Hi := UR_To_Uint (HiR / Small_Value (T));
2988
2989       --  No other types allowed
2990
2991       else
2992          raise Program_Error;
2993       end if;
2994
2995       --  Fall through with Hi and Lo set. Deal with biased case.
2996
2997       if (Biased and then not Is_Fixed_Point_Type (T))
2998         or else Has_Biased_Representation (T)
2999       then
3000          Hi := Hi - Lo;
3001          Lo := Uint_0;
3002       end if;
3003
3004       --  Signed case. Note that we consider types like range 1 .. -1 to be
3005       --  signed for the purpose of computing the size, since the bounds
3006       --  have to be accomodated in the base type.
3007
3008       if Lo < 0 or else Hi < 0 then
3009          S := 1;
3010          B := Uint_1;
3011
3012          --  S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3013          --  Note that we accommodate the case where the bounds cross. This
3014          --  can happen either because of the way the bounds are declared
3015          --  or because of the algorithm in Freeze_Fixed_Point_Type.
3016
3017          while Lo < -B
3018            or else Hi < -B
3019            or else Lo >= B
3020            or else Hi >= B
3021          loop
3022             B := Uint_2 ** S;
3023             S := S + 1;
3024          end loop;
3025
3026       --  Unsigned case
3027
3028       else
3029          --  If both bounds are positive, make sure that both are represen-
3030          --  table in the case where the bounds are crossed. This can happen
3031          --  either because of the way the bounds are declared, or because of
3032          --  the algorithm in Freeze_Fixed_Point_Type.
3033
3034          if Lo > Hi then
3035             Hi := Lo;
3036          end if;
3037
3038          --  S = size, (can accommodate 0 .. (2**size - 1))
3039
3040          S := 0;
3041          while Hi >= Uint_2 ** S loop
3042             S := S + 1;
3043          end loop;
3044       end if;
3045
3046       return S;
3047    end Minimum_Size;
3048
3049    -------------------------
3050    -- New_Stream_Function --
3051    -------------------------
3052
3053    procedure New_Stream_Function
3054      (N    : Node_Id;
3055       Ent  : Entity_Id;
3056       Subp : Entity_Id;
3057       Nam  : Name_Id)
3058    is
3059       Loc       : constant Source_Ptr := Sloc (N);
3060       Subp_Id   : Entity_Id := Make_Defining_Identifier (Loc, Nam);
3061       Subp_Decl : Node_Id;
3062       F         : Entity_Id;
3063       Etyp      : Entity_Id;
3064
3065    begin
3066       F        := First_Formal (Subp);
3067       Etyp     := Etype (Subp);
3068
3069       Subp_Decl :=
3070         Make_Subprogram_Renaming_Declaration (Loc,
3071           Specification =>
3072
3073             Make_Function_Specification (Loc,
3074               Defining_Unit_Name => Subp_Id,
3075               Parameter_Specifications =>
3076                 New_List (
3077                   Make_Parameter_Specification (Loc,
3078                     Defining_Identifier =>
3079                       Make_Defining_Identifier (Loc, Name_S),
3080                     Parameter_Type =>
3081                       Make_Access_Definition (Loc,
3082                         Subtype_Mark =>
3083                           New_Reference_To (
3084                             Designated_Type (Etype (F)), Loc)))),
3085
3086               Subtype_Mark =>
3087                 New_Reference_To (Etyp, Loc)),
3088
3089         Name => New_Reference_To (Subp, Loc));
3090
3091       if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3092          Set_TSS (Base_Type (Ent), Subp_Id);
3093       else
3094          Insert_Action (N, Subp_Decl);
3095          Copy_TSS (Subp_Id, Base_Type (Ent));
3096       end if;
3097
3098    end New_Stream_Function;
3099
3100    --------------------------
3101    -- New_Stream_Procedure --
3102    --------------------------
3103
3104    procedure New_Stream_Procedure
3105      (N     : Node_Id;
3106       Ent   : Entity_Id;
3107       Subp  : Entity_Id;
3108       Nam   : Name_Id;
3109       Out_P : Boolean := False)
3110    is
3111       Loc       : constant Source_Ptr := Sloc (N);
3112       Subp_Id   : Entity_Id := Make_Defining_Identifier (Loc, Nam);
3113       Subp_Decl : Node_Id;
3114       F         : Entity_Id;
3115       Etyp      : Entity_Id;
3116
3117    begin
3118       F        := First_Formal (Subp);
3119       Etyp     := Etype (Next_Formal (F));
3120
3121       Subp_Decl :=
3122         Make_Subprogram_Renaming_Declaration (Loc,
3123           Specification =>
3124
3125             Make_Procedure_Specification (Loc,
3126               Defining_Unit_Name => Subp_Id,
3127               Parameter_Specifications =>
3128                 New_List (
3129                   Make_Parameter_Specification (Loc,
3130                     Defining_Identifier =>
3131                       Make_Defining_Identifier (Loc, Name_S),
3132                     Parameter_Type =>
3133                       Make_Access_Definition (Loc,
3134                         Subtype_Mark =>
3135                           New_Reference_To (
3136                             Designated_Type (Etype (F)), Loc))),
3137
3138                   Make_Parameter_Specification (Loc,
3139                     Defining_Identifier =>
3140                       Make_Defining_Identifier (Loc, Name_V),
3141                     Out_Present => Out_P,
3142                     Parameter_Type =>
3143                       New_Reference_To (Etyp, Loc)))),
3144         Name => New_Reference_To (Subp, Loc));
3145
3146       if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3147          Set_TSS (Base_Type (Ent), Subp_Id);
3148       else
3149          Insert_Action (N, Subp_Decl);
3150          Copy_TSS (Subp_Id, Base_Type (Ent));
3151       end if;
3152
3153    end New_Stream_Procedure;
3154
3155    ---------------------
3156    -- Record_Rep_Item --
3157    ---------------------
3158
3159    procedure Record_Rep_Item (T : Entity_Id; N : Node_Id) is
3160    begin
3161       Set_Next_Rep_Item (N, First_Rep_Item (T));
3162       Set_First_Rep_Item (T, N);
3163    end Record_Rep_Item;
3164
3165    ------------------------
3166    -- Rep_Item_Too_Early --
3167    ------------------------
3168
3169    function Rep_Item_Too_Early
3170      (T     : Entity_Id;
3171       N     : Node_Id)
3172       return  Boolean
3173    is
3174    begin
3175       --  Cannot apply rep items to generic types
3176
3177       if Is_Type (T)
3178         and then Is_Generic_Type (Root_Type (T))
3179       then
3180          Error_Msg_N
3181            ("representation item not allowed for generic type", N);
3182          return True;
3183       end if;
3184
3185       --  Otherwise check for incompleted type
3186
3187       if Is_Incomplete_Or_Private_Type (T)
3188         and then No (Underlying_Type (T))
3189       then
3190          Error_Msg_N
3191            ("representation item must be after full type declaration", N);
3192          return True;
3193
3194       --  If the type has incompleted components, a representation clause is
3195       --  illegal but stream attributes and Convention pragmas are correct.
3196
3197       elsif Has_Private_Component (T) then
3198          if (Nkind (N) = N_Pragma or else Is_Operational_Item (N)) then
3199             return False;
3200          else
3201             Error_Msg_N
3202               ("representation item must appear after type is fully defined",
3203                 N);
3204             return True;
3205          end if;
3206       else
3207          return False;
3208       end if;
3209    end Rep_Item_Too_Early;
3210
3211    -----------------------
3212    -- Rep_Item_Too_Late --
3213    -----------------------
3214
3215    function Rep_Item_Too_Late
3216      (T     : Entity_Id;
3217       N     : Node_Id;
3218       FOnly : Boolean := False)
3219       return  Boolean
3220    is
3221       S           : Entity_Id;
3222       Parent_Type : Entity_Id;
3223
3224       procedure Too_Late;
3225       --  Output the too late message
3226
3227       procedure Too_Late is
3228       begin
3229          Error_Msg_N ("representation item appears too late!", N);
3230       end Too_Late;
3231
3232    --  Start of processing for Rep_Item_Too_Late
3233
3234    begin
3235       --  First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3236       --  types, which may be frozen if they appear in a representation clause
3237       --  for a local type.
3238
3239       if Is_Frozen (T)
3240         and then not From_With_Type (T)
3241       then
3242          Too_Late;
3243          S := First_Subtype (T);
3244
3245          if Present (Freeze_Node (S)) then
3246             Error_Msg_NE
3247               ("?no more representation items for }!", Freeze_Node (S), S);
3248          end if;
3249
3250          return True;
3251
3252       --  Check for case of non-tagged derived type whose parent either has
3253       --  primitive operations, or is a by reference type (RM 13.1(10)).
3254
3255       elsif Is_Type (T)
3256         and then not FOnly
3257         and then Is_Derived_Type (T)
3258         and then not Is_Tagged_Type (T)
3259       then
3260          Parent_Type := Etype (Base_Type (T));
3261
3262          if Has_Primitive_Operations (Parent_Type) then
3263             Too_Late;
3264             Error_Msg_NE
3265               ("primitive operations already defined for&!", N, Parent_Type);
3266             return True;
3267
3268          elsif Is_By_Reference_Type (Parent_Type) then
3269             Too_Late;
3270             Error_Msg_NE
3271               ("parent type & is a by reference type!", N, Parent_Type);
3272             return True;
3273          end if;
3274       end if;
3275
3276       --  No error, link item into head of chain of rep items for the entity
3277
3278       Record_Rep_Item (T, N);
3279       return False;
3280    end Rep_Item_Too_Late;
3281
3282    -------------------------
3283    -- Same_Representation --
3284    -------------------------
3285
3286    function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3287       T1 : constant Entity_Id := Underlying_Type (Typ1);
3288       T2 : constant Entity_Id := Underlying_Type (Typ2);
3289
3290    begin
3291       --  A quick check, if base types are the same, then we definitely have
3292       --  the same representation, because the subtype specific representation
3293       --  attributes (Size and Alignment) do not affect representation from
3294       --  the point of view of this test.
3295
3296       if Base_Type (T1) = Base_Type (T2) then
3297          return True;
3298
3299       elsif Is_Private_Type (Base_Type (T2))
3300         and then Base_Type (T1) = Full_View (Base_Type (T2))
3301       then
3302          return True;
3303       end if;
3304
3305       --  Tagged types never have differing representations
3306
3307       if Is_Tagged_Type (T1) then
3308          return True;
3309       end if;
3310
3311       --  Representations are definitely different if conventions differ
3312
3313       if Convention (T1) /= Convention (T2) then
3314          return False;
3315       end if;
3316
3317       --  Representations are different if component alignments differ
3318
3319       if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3320         and then
3321          (Is_Record_Type (T2) or else Is_Array_Type (T2))
3322         and then Component_Alignment (T1) /= Component_Alignment (T2)
3323       then
3324          return False;
3325       end if;
3326
3327       --  For arrays, the only real issue is component size. If we know the
3328       --  component size for both arrays, and it is the same, then that's
3329       --  good enough to know we don't have a change of representation.
3330
3331       if Is_Array_Type (T1) then
3332          if Known_Component_Size (T1)
3333            and then Known_Component_Size (T2)
3334            and then Component_Size (T1) = Component_Size (T2)
3335          then
3336             return True;
3337          end if;
3338       end if;
3339
3340       --  Types definitely have same representation if neither has non-standard
3341       --  representation since default representations are always consistent.
3342       --  If only one has non-standard representation, and the other does not,
3343       --  then we consider that they do not have the same representation. They
3344       --  might, but there is no way of telling early enough.
3345
3346       if Has_Non_Standard_Rep (T1) then
3347          if not Has_Non_Standard_Rep (T2) then
3348             return False;
3349          end if;
3350       else
3351          return not Has_Non_Standard_Rep (T2);
3352       end if;
3353
3354       --  Here the two types both have non-standard representation, and we
3355       --  need to determine if they have the same non-standard representation
3356
3357       --  For arrays, we simply need to test if the component sizes are the
3358       --  same. Pragma Pack is reflected in modified component sizes, so this
3359       --  check also deals with pragma Pack.
3360
3361       if Is_Array_Type (T1) then
3362          return Component_Size (T1) = Component_Size (T2);
3363
3364       --  Tagged types always have the same representation, because it is not
3365       --  possible to specify different representations for common fields.
3366
3367       elsif Is_Tagged_Type (T1) then
3368          return True;
3369
3370       --  Case of record types
3371
3372       elsif Is_Record_Type (T1) then
3373
3374          --  Packed status must conform
3375
3376          if Is_Packed (T1) /= Is_Packed (T2) then
3377             return False;
3378
3379          --  Otherwise we must check components. Typ2 maybe a constrained
3380          --  subtype with fewer components, so we compare the components
3381          --  of the base types.
3382
3383          else
3384             Record_Case : declare
3385                CD1, CD2 : Entity_Id;
3386
3387                function Same_Rep return Boolean;
3388                --  CD1 and CD2 are either components or discriminants. This
3389                --  function tests whether the two have the same representation
3390
3391                function Same_Rep return Boolean is
3392                begin
3393                   if No (Component_Clause (CD1)) then
3394                      return No (Component_Clause (CD2));
3395
3396                   else
3397                      return
3398                         Present (Component_Clause (CD2))
3399                           and then
3400                         Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3401                           and then
3402                         Esize (CD1) = Esize (CD2);
3403                   end if;
3404                end Same_Rep;
3405
3406             --  Start processing for Record_Case
3407
3408             begin
3409                if Has_Discriminants (T1) then
3410                   CD1 := First_Discriminant (T1);
3411                   CD2 := First_Discriminant (T2);
3412
3413                   while Present (CD1) loop
3414                      if not Same_Rep then
3415                         return False;
3416                      else
3417                         Next_Discriminant (CD1);
3418                         Next_Discriminant (CD2);
3419                      end if;
3420                   end loop;
3421                end if;
3422
3423                CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3424                CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3425
3426                while Present (CD1) loop
3427                   if not Same_Rep then
3428                      return False;
3429                   else
3430                      Next_Component (CD1);
3431                      Next_Component (CD2);
3432                   end if;
3433                end loop;
3434
3435                return True;
3436             end Record_Case;
3437          end if;
3438
3439       --  For enumeration types, we must check each literal to see if the
3440       --  representation is the same. Note that we do not permit enumeration
3441       --  reprsentation clauses for Character and Wide_Character, so these
3442       --  cases were already dealt with.
3443
3444       elsif Is_Enumeration_Type (T1) then
3445
3446          Enumeration_Case : declare
3447             L1, L2 : Entity_Id;
3448
3449          begin
3450             L1 := First_Literal (T1);
3451             L2 := First_Literal (T2);
3452
3453             while Present (L1) loop
3454                if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3455                   return False;
3456                else
3457                   Next_Literal (L1);
3458                   Next_Literal (L2);
3459                end if;
3460             end loop;
3461
3462             return True;
3463
3464          end Enumeration_Case;
3465
3466       --  Any other types have the same representation for these purposes
3467
3468       else
3469          return True;
3470       end if;
3471
3472    end Same_Representation;
3473
3474    --------------------
3475    -- Set_Enum_Esize --
3476    --------------------
3477
3478    procedure Set_Enum_Esize (T : Entity_Id) is
3479       Lo : Uint;
3480       Hi : Uint;
3481       Sz : Nat;
3482
3483    begin
3484       Init_Alignment (T);
3485
3486       --  Find the minimum standard size (8,16,32,64) that fits
3487
3488       Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3489       Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3490
3491       if Lo < 0 then
3492          if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3493             Sz := 8;
3494
3495          elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3496             Sz := 16;
3497
3498          elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3499             Sz := 32;
3500
3501          else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3502             Sz := 64;
3503          end if;
3504
3505       else
3506          if Hi < Uint_2**08 then
3507             Sz := 8;
3508
3509          elsif Hi < Uint_2**16 then
3510             Sz := 16;
3511
3512          elsif Hi < Uint_2**32 then
3513             Sz := 32;
3514
3515          else pragma Assert (Hi < Uint_2**63);
3516             Sz := 64;
3517          end if;
3518       end if;
3519
3520       --  That minimum is the proper size unless we have a foreign convention
3521       --  and the size required is 32 or less, in which case we bump the size
3522       --  up to 32. This is required for C and C++ and seems reasonable for
3523       --  all other foreign conventions.
3524
3525       if Has_Foreign_Convention (T)
3526         and then Esize (T) < Standard_Integer_Size
3527       then
3528          Init_Esize (T, Standard_Integer_Size);
3529
3530       else
3531          Init_Esize (T, Sz);
3532       end if;
3533
3534    end Set_Enum_Esize;
3535
3536    -----------------------------------
3537    -- Validate_Unchecked_Conversion --
3538    -----------------------------------
3539
3540    procedure Validate_Unchecked_Conversion
3541      (N        : Node_Id;
3542       Act_Unit : Entity_Id)
3543    is
3544       Source : Entity_Id;
3545       Target : Entity_Id;
3546       Vnode  : Node_Id;
3547
3548    begin
3549       --  Obtain source and target types. Note that we call Ancestor_Subtype
3550       --  here because the processing for generic instantiation always makes
3551       --  subtypes, and we want the original frozen actual types.
3552
3553       --  If we are dealing with private types, then do the check on their
3554       --  fully declared counterparts if the full declarations have been
3555       --  encountered (they don't have to be visible, but they must exist!)
3556
3557       Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
3558
3559       if Is_Private_Type (Source)
3560         and then Present (Underlying_Type (Source))
3561       then
3562          Source := Underlying_Type (Source);
3563       end if;
3564
3565       Target := Ancestor_Subtype (Etype (Act_Unit));
3566
3567       --  If either type is generic, the instantiation happens within a
3568       --  generic unit, and there is nothing to check. The proper check
3569       --  will happen when the enclosing generic is instantiated.
3570
3571       if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
3572          return;
3573       end if;
3574
3575       if Is_Private_Type (Target)
3576         and then Present (Underlying_Type (Target))
3577       then
3578          Target := Underlying_Type (Target);
3579       end if;
3580
3581       --  Source may be unconstrained array, but not target
3582
3583       if Is_Array_Type (Target)
3584         and then not Is_Constrained (Target)
3585       then
3586          Error_Msg_N
3587            ("unchecked conversion to unconstrained array not allowed", N);
3588          return;
3589       end if;
3590
3591       --  Make entry in unchecked conversion table for later processing
3592       --  by Validate_Unchecked_Conversions, which will check sizes and
3593       --  alignments (using values set by the back-end where possible).
3594
3595       Unchecked_Conversions.Append
3596         (New_Val => UC_Entry'
3597            (Enode  => N,
3598             Source => Source,
3599             Target => Target));
3600
3601       --  Generate N_Validate_Unchecked_Conversion node for back end if
3602       --  the back end needs to perform special validation checks. At the
3603       --  current time, only the JVM version requires such checks.
3604
3605       if Java_VM then
3606          Vnode :=
3607            Make_Validate_Unchecked_Conversion (Sloc (N));
3608          Set_Source_Type (Vnode, Source);
3609          Set_Target_Type (Vnode, Target);
3610          Insert_After (N, Vnode);
3611       end if;
3612    end Validate_Unchecked_Conversion;
3613
3614    ------------------------------------
3615    -- Validate_Unchecked_Conversions --
3616    ------------------------------------
3617
3618    procedure Validate_Unchecked_Conversions is
3619    begin
3620       for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
3621          declare
3622             T : UC_Entry renames Unchecked_Conversions.Table (N);
3623
3624             Enode  : constant Node_Id   := T.Enode;
3625             Source : constant Entity_Id := T.Source;
3626             Target : constant Entity_Id := T.Target;
3627
3628             Source_Siz    : Uint;
3629             Target_Siz    : Uint;
3630
3631          begin
3632             --  This validation check, which warns if we have unequal sizes
3633             --  for unchecked conversion, and thus potentially implementation
3634             --  dependent semantics, is one of the few occasions on which we
3635             --  use the official RM size instead of Esize. See description
3636             --  in Einfo "Handling of Type'Size Values" for details.
3637
3638             if Errors_Detected = 0
3639               and then Known_Static_RM_Size (Source)
3640               and then Known_Static_RM_Size (Target)
3641             then
3642                Source_Siz := RM_Size (Source);
3643                Target_Siz := RM_Size (Target);
3644
3645                if Source_Siz /= Target_Siz then
3646                   Warn_On_Instance := True;
3647                   Error_Msg_N
3648                     ("types for unchecked conversion have different sizes?",
3649                      Enode);
3650
3651                   if All_Errors_Mode then
3652                      Error_Msg_Name_1 := Chars (Source);
3653                      Error_Msg_Uint_1 := Source_Siz;
3654                      Error_Msg_Name_2 := Chars (Target);
3655                      Error_Msg_Uint_2 := Target_Siz;
3656                      Error_Msg_N
3657                        ("\size of % is ^, size of % is ^?", Enode);
3658
3659                      Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
3660
3661                      if Is_Discrete_Type (Source)
3662                        and then Is_Discrete_Type (Target)
3663                      then
3664                         if Source_Siz > Target_Siz then
3665                            Error_Msg_N
3666                              ("\^ high order bits of source will be ignored?",
3667                               Enode);
3668
3669                         elsif Is_Modular_Integer_Type (Source) then
3670                            Error_Msg_N
3671                              ("\source will be extended with ^ high order " &
3672                               "zero bits?", Enode);
3673
3674                         else
3675                            Error_Msg_N
3676                              ("\source will be extended with ^ high order " &
3677                               "sign bits?",
3678                               Enode);
3679                         end if;
3680
3681                      elsif Source_Siz < Target_Siz then
3682                         if Is_Discrete_Type (Target) then
3683                            if Bytes_Big_Endian then
3684                               Error_Msg_N
3685                                 ("\target value will include ^ undefined " &
3686                                  "low order bits?",
3687                                  Enode);
3688                            else
3689                               Error_Msg_N
3690                                 ("\target value will include ^ undefined " &
3691                                  "high order bits?",
3692                                  Enode);
3693                            end if;
3694
3695                         else
3696                            Error_Msg_N
3697                              ("\^ trailing bits of target value will be " &
3698                               "undefined?", Enode);
3699                         end if;
3700
3701                      else pragma Assert (Source_Siz > Target_Siz);
3702                         Error_Msg_N
3703                           ("\^ trailing bits of source will be ignored?",
3704                            Enode);
3705                      end if;
3706                   end if;
3707
3708                   Warn_On_Instance := False;
3709                end if;
3710             end if;
3711
3712             --  If both types are access types, we need to check the alignment.
3713             --  If the alignment of both is specified, we can do it here.
3714
3715             if Errors_Detected = 0
3716               and then Ekind (Source) in Access_Kind
3717               and then Ekind (Target) in Access_Kind
3718               and then Target_Strict_Alignment
3719               and then Present (Designated_Type (Source))
3720               and then Present (Designated_Type (Target))
3721             then
3722                declare
3723                   D_Source : constant Entity_Id := Designated_Type (Source);
3724                   D_Target : constant Entity_Id := Designated_Type (Target);
3725
3726                begin
3727                   if Known_Alignment (D_Source)
3728                     and then Known_Alignment (D_Target)
3729                   then
3730                      declare
3731                         Source_Align : constant Uint := Alignment (D_Source);
3732                         Target_Align : constant Uint := Alignment (D_Target);
3733
3734                      begin
3735                         if Source_Align < Target_Align
3736                           and then not Is_Tagged_Type (D_Source)
3737                         then
3738                            Warn_On_Instance := True;
3739                            Error_Msg_Uint_1 := Target_Align;
3740                            Error_Msg_Uint_2 := Source_Align;
3741                            Error_Msg_Node_2 := D_Source;
3742                            Error_Msg_NE
3743                              ("alignment of & (^) is stricter than " &
3744                               "alignment of & (^)?", Enode, D_Target);
3745
3746                            if All_Errors_Mode then
3747                               Error_Msg_N
3748                                 ("\resulting access value may have invalid " &
3749                                  "alignment?", Enode);
3750                            end if;
3751
3752                            Warn_On_Instance := False;
3753                         end if;
3754                      end;
3755                   end if;
3756                end;
3757             end if;
3758          end;
3759       end loop;
3760    end Validate_Unchecked_Conversions;
3761
3762    ------------------
3763    -- Warn_Overlay --
3764    ------------------
3765
3766    procedure Warn_Overlay
3767      (Expr : Node_Id;
3768       Typ  : Entity_Id;
3769       Nam  : Node_Id)
3770    is
3771       Old  : Entity_Id := Empty;
3772       Decl : Node_Id;
3773
3774    begin
3775       if not Address_Clause_Overlay_Warnings then
3776          return;
3777       end if;
3778
3779       if Present (Expr)
3780         and then (Has_Non_Null_Base_Init_Proc (Typ)
3781                     or else Is_Access_Type (Typ))
3782         and then not Is_Imported (Entity (Nam))
3783       then
3784          if Nkind (Expr) = N_Attribute_Reference
3785            and then Is_Entity_Name (Prefix (Expr))
3786          then
3787             Old := Entity (Prefix (Expr));
3788
3789          elsif Is_Entity_Name (Expr)
3790            and then Ekind (Entity (Expr)) = E_Constant
3791          then
3792             Decl := Declaration_Node (Entity (Expr));
3793
3794             if Nkind (Decl) = N_Object_Declaration
3795               and then Present (Expression (Decl))
3796               and then Nkind (Expression (Decl)) = N_Attribute_Reference
3797               and then Is_Entity_Name (Prefix (Expression (Decl)))
3798             then
3799                Old := Entity (Prefix (Expression (Decl)));
3800
3801             elsif Nkind (Expr) = N_Function_Call then
3802                return;
3803             end if;
3804
3805          --  A function call (most likely to To_Address) is probably not
3806          --  an overlay, so skip warning. Ditto if the function call was
3807          --  inlined and transformed into an entity.
3808
3809          elsif Nkind (Original_Node (Expr)) = N_Function_Call then
3810             return;
3811          end if;
3812
3813          Decl := Next (Parent (Expr));
3814
3815          --  If a pragma Import follows, we assume that it is for the current
3816          --  target of the address clause, and skip the warning.
3817
3818          if Present (Decl)
3819            and then Nkind (Decl) = N_Pragma
3820            and then Chars (Decl) = Name_Import
3821          then
3822             return;
3823          end if;
3824
3825          if Present (Old) then
3826             Error_Msg_Node_2 := Old;
3827             Error_Msg_N
3828               ("default initialization of & may modify &?",
3829                Nam);
3830          else
3831             Error_Msg_N
3832               ("default initialization of & may modify overlaid storage?",
3833                Nam);
3834          end if;
3835
3836          --  Add friendly warning if initialization comes from a packed array
3837          --  component.
3838
3839          if Is_Record_Type (Typ)  then
3840             declare
3841                Comp : Entity_Id;
3842
3843             begin
3844                Comp := First_Component (Typ);
3845
3846                while Present (Comp) loop
3847                   if Nkind (Parent (Comp)) = N_Component_Declaration
3848                     and then Present (Expression (Parent (Comp)))
3849                   then
3850                      exit;
3851                   elsif Is_Array_Type (Etype (Comp))
3852                      and then Present (Packed_Array_Type (Etype (Comp)))
3853                   then
3854                      Error_Msg_NE
3855                        ("packed array component& will be initialized to zero?",
3856                           Nam, Comp);
3857                      exit;
3858                   else
3859                      Next_Component (Comp);
3860                   end if;
3861                end loop;
3862             end;
3863          end if;
3864
3865          Error_Msg_N
3866            ("use pragma Import for & to " &
3867               "suppress initialization ('R'M B.1(24))?",
3868              Nam);
3869       end if;
3870    end Warn_Overlay;
3871
3872 end Sem_Ch13;