OSDN Git Service

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