OSDN Git Service

2009-05-06 Ed Schonberg <schonberg@adacore.com>
[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 --          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Checks;   use Checks;
28 with Einfo;    use Einfo;
29 with Errout;   use Errout;
30 with Exp_Tss;  use Exp_Tss;
31 with Exp_Util; use Exp_Util;
32 with Lib;      use Lib;
33 with Lib.Xref; use Lib.Xref;
34 with Namet;    use Namet;
35 with Nlists;   use Nlists;
36 with Nmake;    use Nmake;
37 with Opt;      use Opt;
38 with Restrict; use Restrict;
39 with Rident;   use Rident;
40 with Rtsfind;  use Rtsfind;
41 with Sem;      use Sem;
42 with Sem_Aux;  use Sem_Aux;
43 with Sem_Ch8;  use Sem_Ch8;
44 with Sem_Eval; use Sem_Eval;
45 with Sem_Res;  use Sem_Res;
46 with Sem_Type; use Sem_Type;
47 with Sem_Util; use Sem_Util;
48 with Sem_Warn; use Sem_Warn;
49 with Snames;   use Snames;
50 with Stand;    use Stand;
51 with Sinfo;    use Sinfo;
52 with Table;
53 with Targparm; use Targparm;
54 with Ttypes;   use Ttypes;
55 with Tbuild;   use Tbuild;
56 with Urealp;   use Urealp;
57
58 with GNAT.Heap_Sort_G;
59
60 package body Sem_Ch13 is
61
62    SSU : constant Pos := System_Storage_Unit;
63    --  Convenient short hand for commonly used constant
64
65    -----------------------
66    -- Local Subprograms --
67    -----------------------
68
69    procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
70    --  This routine is called after setting the Esize of type entity Typ.
71    --  The purpose is to deal with the situation where an alignment has been
72    --  inherited from a derived type that is no longer appropriate for the
73    --  new Esize value. In this case, we reset the Alignment to unknown.
74
75    procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
76    --  Given two entities for record components or discriminants, checks
77    --  if they have overlapping component clauses and issues errors if so.
78
79    function Get_Alignment_Value (Expr : Node_Id) return Uint;
80    --  Given the expression for an alignment value, returns the corresponding
81    --  Uint value. If the value is inappropriate, then error messages are
82    --  posted as required, and a value of No_Uint is returned.
83
84    function Is_Operational_Item (N : Node_Id) return Boolean;
85    --  A specification for a stream attribute is allowed before the full
86    --  type is declared, as explained in AI-00137 and the corrigendum.
87    --  Attributes that do not specify a representation characteristic are
88    --  operational attributes.
89
90    function Address_Aliased_Entity (N : Node_Id) return Entity_Id;
91    --  If expression N is of the form E'Address, return E
92
93    procedure New_Stream_Subprogram
94      (N    : Node_Id;
95       Ent  : Entity_Id;
96       Subp : Entity_Id;
97       Nam  : TSS_Name_Type);
98    --  Create a subprogram renaming of a given stream attribute to the
99    --  designated subprogram and then in the tagged case, provide this as a
100    --  primitive operation, or in the non-tagged case make an appropriate TSS
101    --  entry. This is more properly an expansion activity than just semantics,
102    --  but the presence of user-defined stream functions for limited types is a
103    --  legality check, which is why this takes place here rather than in
104    --  exp_ch13, where it was previously. Nam indicates the name of the TSS
105    --  function to be generated.
106    --
107    --  To avoid elaboration anomalies with freeze nodes, for untagged types
108    --  we generate both a subprogram declaration and a subprogram renaming
109    --  declaration, so that the attribute specification is handled as a
110    --  renaming_as_body. For tagged types, the specification is one of the
111    --  primitive specs.
112
113    ----------------------------------------------
114    -- Table for Validate_Unchecked_Conversions --
115    ----------------------------------------------
116
117    --  The following table collects unchecked conversions for validation.
118    --  Entries are made by Validate_Unchecked_Conversion and then the
119    --  call to Validate_Unchecked_Conversions does the actual error
120    --  checking and posting of warnings. The reason for this delayed
121    --  processing is to take advantage of back-annotations of size and
122    --  alignment values performed by the back end.
123
124    --  Note: the reason we store a Source_Ptr value instead of a Node_Id
125    --  is that by the time Validate_Unchecked_Conversions is called, Sprint
126    --  will already have modified all Sloc values if the -gnatD option is set.
127
128    type UC_Entry is record
129       Eloc   : Source_Ptr; -- node used for posting warnings
130       Source : Entity_Id;  -- source type for unchecked conversion
131       Target : Entity_Id;  -- target type for unchecked conversion
132    end record;
133
134    package Unchecked_Conversions is new Table.Table (
135      Table_Component_Type => UC_Entry,
136      Table_Index_Type     => Int,
137      Table_Low_Bound      => 1,
138      Table_Initial        => 50,
139      Table_Increment      => 200,
140      Table_Name           => "Unchecked_Conversions");
141
142    ----------------------------------------
143    -- Table for Validate_Address_Clauses --
144    ----------------------------------------
145
146    --  If an address clause has the form
147
148    --    for X'Address use Expr
149
150    --  where Expr is of the form Y'Address or recursively is a reference
151    --  to a constant of either of these forms, and X and Y are entities of
152    --  objects, then if Y has a smaller alignment than X, that merits a
153    --  warning about possible bad alignment. The following table collects
154    --  address clauses of this kind. We put these in a table so that they
155    --  can be checked after the back end has completed annotation of the
156    --  alignments of objects, since we can catch more cases that way.
157
158    type Address_Clause_Check_Record is record
159       N : Node_Id;
160       --  The address clause
161
162       X : Entity_Id;
163       --  The entity of the object overlaying Y
164
165       Y : Entity_Id;
166       --  The entity of the object being overlaid
167    end record;
168
169    package Address_Clause_Checks is new Table.Table (
170      Table_Component_Type => Address_Clause_Check_Record,
171      Table_Index_Type     => Int,
172      Table_Low_Bound      => 1,
173      Table_Initial        => 20,
174      Table_Increment      => 200,
175      Table_Name           => "Address_Clause_Checks");
176
177    ----------------------------
178    -- Address_Aliased_Entity --
179    ----------------------------
180
181    function Address_Aliased_Entity (N : Node_Id) return Entity_Id is
182    begin
183       if Nkind (N) = N_Attribute_Reference
184         and then Attribute_Name (N) = Name_Address
185       then
186          declare
187             P : Node_Id;
188
189          begin
190             P := Prefix (N);
191             while Nkind_In (P, N_Selected_Component, N_Indexed_Component) loop
192                P := Prefix (P);
193             end loop;
194
195             if Is_Entity_Name (P) then
196                return Entity (P);
197             end if;
198          end;
199       end if;
200
201       return Empty;
202    end Address_Aliased_Entity;
203
204    -----------------------------------------
205    -- Adjust_Record_For_Reverse_Bit_Order --
206    -----------------------------------------
207
208    procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
209       Max_Machine_Scalar_Size : constant Uint :=
210                                   UI_From_Int
211                                     (Standard_Long_Long_Integer_Size);
212       --  We use this as the maximum machine scalar size in the sense of AI-133
213
214       Num_CC : Natural;
215       Comp   : Entity_Id;
216       SSU    : constant Uint := UI_From_Int (System_Storage_Unit);
217
218    begin
219       --  This first loop through components does two things. First it deals
220       --  with the case of components with component clauses whose length is
221       --  greater than the maximum machine scalar size (either accepting them
222       --  or rejecting as needed). Second, it counts the number of components
223       --  with component clauses whose length does not exceed this maximum for
224       --  later processing.
225
226       Num_CC := 0;
227       Comp   := First_Component_Or_Discriminant (R);
228       while Present (Comp) loop
229          declare
230             CC : constant Node_Id := Component_Clause (Comp);
231
232          begin
233             if Present (CC) then
234                declare
235                   Fbit : constant Uint := Static_Integer (First_Bit (CC));
236
237                begin
238                   --  Case of component with size > max machine scalar
239
240                   if Esize (Comp) > Max_Machine_Scalar_Size then
241
242                      --  Must begin on byte boundary
243
244                      if Fbit mod SSU /= 0 then
245                         Error_Msg_N
246                           ("illegal first bit value for reverse bit order",
247                            First_Bit (CC));
248                         Error_Msg_Uint_1 := SSU;
249                         Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
250
251                         Error_Msg_N
252                           ("\must be a multiple of ^ if size greater than ^",
253                            First_Bit (CC));
254
255                      --  Must end on byte boundary
256
257                      elsif Esize (Comp) mod SSU /= 0 then
258                         Error_Msg_N
259                           ("illegal last bit value for reverse bit order",
260                            Last_Bit (CC));
261                         Error_Msg_Uint_1 := SSU;
262                         Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
263
264                         Error_Msg_N
265                           ("\must be a multiple of ^ if size greater than ^",
266                            Last_Bit (CC));
267
268                      --  OK, give warning if enabled
269
270                      elsif Warn_On_Reverse_Bit_Order then
271                         Error_Msg_N
272                           ("multi-byte field specified with non-standard"
273                            & " Bit_Order?", CC);
274
275                         if Bytes_Big_Endian then
276                            Error_Msg_N
277                              ("\bytes are not reversed "
278                               & "(component is big-endian)?", CC);
279                         else
280                            Error_Msg_N
281                              ("\bytes are not reversed "
282                               & "(component is little-endian)?", CC);
283                         end if;
284                      end if;
285
286                      --  Case where size is not greater than max machine
287                      --  scalar. For now, we just count these.
288
289                   else
290                      Num_CC := Num_CC + 1;
291                   end if;
292                end;
293             end if;
294          end;
295
296          Next_Component_Or_Discriminant (Comp);
297       end loop;
298
299       --  We need to sort the component clauses on the basis of the Position
300       --  values in the clause, so we can group clauses with the same Position.
301       --  together to determine the relevant machine scalar size.
302
303       declare
304          Comps : array (0 .. Num_CC) of Entity_Id;
305          --  Array to collect component and discriminant entities. The data
306          --  starts at index 1, the 0'th entry is for the sort routine.
307
308          function CP_Lt (Op1, Op2 : Natural) return Boolean;
309          --  Compare routine for Sort
310
311          procedure CP_Move (From : Natural; To : Natural);
312          --  Move routine for Sort
313
314          package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
315
316          Start : Natural;
317          Stop  : Natural;
318          --  Start and stop positions in component list of set of components
319          --  with the same starting position (that constitute components in
320          --  a single machine scalar).
321
322          MaxL : Uint;
323          --  Maximum last bit value of any component in this set
324
325          MSS : Uint;
326          --  Corresponding machine scalar size
327
328          -----------
329          -- CP_Lt --
330          -----------
331
332          function CP_Lt (Op1, Op2 : Natural) return Boolean is
333          begin
334             return Position (Component_Clause (Comps (Op1))) <
335                    Position (Component_Clause (Comps (Op2)));
336          end CP_Lt;
337
338          -------------
339          -- CP_Move --
340          -------------
341
342          procedure CP_Move (From : Natural; To : Natural) is
343          begin
344             Comps (To) := Comps (From);
345          end CP_Move;
346
347       begin
348          --  Collect the component clauses
349
350          Num_CC := 0;
351          Comp   := First_Component_Or_Discriminant (R);
352          while Present (Comp) loop
353             if Present (Component_Clause (Comp))
354               and then Esize (Comp) <= Max_Machine_Scalar_Size
355             then
356                Num_CC := Num_CC + 1;
357                Comps (Num_CC) := Comp;
358             end if;
359
360             Next_Component_Or_Discriminant (Comp);
361          end loop;
362
363          --  Sort by ascending position number
364
365          Sorting.Sort (Num_CC);
366
367          --  We now have all the components whose size does not exceed the max
368          --  machine scalar value, sorted by starting position. In this loop
369          --  we gather groups of clauses starting at the same position, to
370          --  process them in accordance with Ada 2005 AI-133.
371
372          Stop := 0;
373          while Stop < Num_CC loop
374             Start := Stop + 1;
375             Stop  := Start;
376             MaxL  :=
377               Static_Integer (Last_Bit (Component_Clause (Comps (Start))));
378             while Stop < Num_CC loop
379                if Static_Integer
380                     (Position (Component_Clause (Comps (Stop + 1)))) =
381                   Static_Integer
382                     (Position (Component_Clause (Comps (Stop))))
383                then
384                   Stop := Stop + 1;
385                   MaxL :=
386                     UI_Max
387                       (MaxL,
388                        Static_Integer
389                          (Last_Bit (Component_Clause (Comps (Stop)))));
390                else
391                   exit;
392                end if;
393             end loop;
394
395             --  Now we have a group of component clauses from Start to Stop
396             --  whose positions are identical, and MaxL is the maximum last bit
397             --  value of any of these components.
398
399             --  We need to determine the corresponding machine scalar size.
400             --  This loop assumes that machine scalar sizes are even, and that
401             --  each possible machine scalar has twice as many bits as the
402             --  next smaller one.
403
404             MSS := Max_Machine_Scalar_Size;
405             while MSS mod 2 = 0
406               and then (MSS / 2) >= SSU
407               and then (MSS / 2) > MaxL
408             loop
409                MSS := MSS / 2;
410             end loop;
411
412             --  Here is where we fix up the Component_Bit_Offset value to
413             --  account for the reverse bit order. Some examples of what needs
414             --  to be done for the case of a machine scalar size of 8 are:
415
416             --    First_Bit .. Last_Bit     Component_Bit_Offset
417             --      old          new          old       new
418
419             --     0 .. 0       7 .. 7         0         7
420             --     0 .. 1       6 .. 7         0         6
421             --     0 .. 2       5 .. 7         0         5
422             --     0 .. 7       0 .. 7         0         4
423
424             --     1 .. 1       6 .. 6         1         6
425             --     1 .. 4       3 .. 6         1         3
426             --     4 .. 7       0 .. 3         4         0
427
428             --  The general rule is that the first bit is obtained by
429             --  subtracting the old ending bit from machine scalar size - 1.
430
431             for C in Start .. Stop loop
432                declare
433                   Comp : constant Entity_Id := Comps (C);
434                   CC   : constant Node_Id   := Component_Clause (Comp);
435                   LB   : constant Uint := Static_Integer (Last_Bit (CC));
436                   NFB  : constant Uint := MSS - Uint_1 - LB;
437                   NLB  : constant Uint := NFB + Esize (Comp) - 1;
438                   Pos  : constant Uint := Static_Integer (Position (CC));
439
440                begin
441                   if Warn_On_Reverse_Bit_Order then
442                      Error_Msg_Uint_1 := MSS;
443                      Error_Msg_N
444                        ("info: reverse bit order in machine " &
445                        "scalar of length^?", First_Bit (CC));
446                      Error_Msg_Uint_1 := NFB;
447                      Error_Msg_Uint_2 := NLB;
448
449                      if Bytes_Big_Endian then
450                         Error_Msg_NE
451                           ("?\info: big-endian range for "
452                            & "component & is ^ .. ^",
453                            First_Bit (CC), Comp);
454                      else
455                         Error_Msg_NE
456                           ("?\info: little-endian range "
457                            & "for component & is ^ .. ^",
458                            First_Bit (CC), Comp);
459                      end if;
460                   end if;
461
462                   Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
463                   Set_Normalized_First_Bit (Comp, NFB mod SSU);
464                end;
465             end loop;
466          end loop;
467       end;
468    end Adjust_Record_For_Reverse_Bit_Order;
469
470    --------------------------------------
471    -- Alignment_Check_For_Esize_Change --
472    --------------------------------------
473
474    procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
475    begin
476       --  If the alignment is known, and not set by a rep clause, and is
477       --  inconsistent with the size being set, then reset it to unknown,
478       --  we assume in this case that the size overrides the inherited
479       --  alignment, and that the alignment must be recomputed.
480
481       if Known_Alignment (Typ)
482         and then not Has_Alignment_Clause (Typ)
483         and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
484       then
485          Init_Alignment (Typ);
486       end if;
487    end Alignment_Check_For_Esize_Change;
488
489    -----------------------
490    -- Analyze_At_Clause --
491    -----------------------
492
493    --  An at clause is replaced by the corresponding Address attribute
494    --  definition clause that is the preferred approach in Ada 95.
495
496    procedure Analyze_At_Clause (N : Node_Id) is
497       CS : constant Boolean := Comes_From_Source (N);
498
499    begin
500       --  This is an obsolescent feature
501
502       Check_Restriction (No_Obsolescent_Features, N);
503
504       if Warn_On_Obsolescent_Feature then
505          Error_Msg_N
506            ("at clause is an obsolescent feature (RM J.7(2))?", N);
507          Error_Msg_N
508            ("\use address attribute definition clause instead?", N);
509       end if;
510
511       --  Rewrite as address clause
512
513       Rewrite (N,
514         Make_Attribute_Definition_Clause (Sloc (N),
515           Name  => Identifier (N),
516           Chars => Name_Address,
517           Expression => Expression (N)));
518
519       --  We preserve Comes_From_Source, since logically the clause still
520       --  comes from the source program even though it is changed in form.
521
522       Set_Comes_From_Source (N, CS);
523
524       --  Analyze rewritten clause
525
526       Analyze_Attribute_Definition_Clause (N);
527    end Analyze_At_Clause;
528
529    -----------------------------------------
530    -- Analyze_Attribute_Definition_Clause --
531    -----------------------------------------
532
533    procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
534       Loc   : constant Source_Ptr   := Sloc (N);
535       Nam   : constant Node_Id      := Name (N);
536       Attr  : constant Name_Id      := Chars (N);
537       Expr  : constant Node_Id      := Expression (N);
538       Id    : constant Attribute_Id := Get_Attribute_Id (Attr);
539       Ent   : Entity_Id;
540       U_Ent : Entity_Id;
541
542       FOnly : Boolean := False;
543       --  Reset to True for subtype specific attribute (Alignment, Size)
544       --  and for stream attributes, i.e. those cases where in the call
545       --  to Rep_Item_Too_Late, FOnly is set True so that only the freezing
546       --  rules are checked. Note that the case of stream attributes is not
547       --  clear from the RM, but see AI95-00137. Also, the RM seems to
548       --  disallow Storage_Size for derived task types, but that is also
549       --  clearly unintentional.
550
551       procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
552       --  Common processing for 'Read, 'Write, 'Input and 'Output attribute
553       --  definition clauses.
554
555       -----------------------------------
556       -- Analyze_Stream_TSS_Definition --
557       -----------------------------------
558
559       procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
560          Subp : Entity_Id := Empty;
561          I    : Interp_Index;
562          It   : Interp;
563          Pnam : Entity_Id;
564
565          Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
566
567          function Has_Good_Profile (Subp : Entity_Id) return Boolean;
568          --  Return true if the entity is a subprogram with an appropriate
569          --  profile for the attribute being defined.
570
571          ----------------------
572          -- Has_Good_Profile --
573          ----------------------
574
575          function Has_Good_Profile (Subp : Entity_Id) return Boolean is
576             F              : Entity_Id;
577             Is_Function    : constant Boolean := (TSS_Nam = TSS_Stream_Input);
578             Expected_Ekind : constant array (Boolean) of Entity_Kind :=
579                                (False => E_Procedure, True => E_Function);
580             Typ            : Entity_Id;
581
582          begin
583             if Ekind (Subp) /= Expected_Ekind (Is_Function) then
584                return False;
585             end if;
586
587             F := First_Formal (Subp);
588
589             if No (F)
590               or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
591               or else Designated_Type (Etype (F)) /=
592                                Class_Wide_Type (RTE (RE_Root_Stream_Type))
593             then
594                return False;
595             end if;
596
597             if not Is_Function then
598                Next_Formal (F);
599
600                declare
601                   Expected_Mode : constant array (Boolean) of Entity_Kind :=
602                                     (False => E_In_Parameter,
603                                      True  => E_Out_Parameter);
604                begin
605                   if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
606                      return False;
607                   end if;
608                end;
609
610                Typ := Etype (F);
611
612             else
613                Typ := Etype (Subp);
614             end if;
615
616             return Base_Type (Typ) = Base_Type (Ent)
617               and then No (Next_Formal (F));
618          end Has_Good_Profile;
619
620       --  Start of processing for Analyze_Stream_TSS_Definition
621
622       begin
623          FOnly := True;
624
625          if not Is_Type (U_Ent) then
626             Error_Msg_N ("local name must be a subtype", Nam);
627             return;
628          end if;
629
630          Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
631
632          --  If Pnam is present, it can be either inherited from an ancestor
633          --  type (in which case it is legal to redefine it for this type), or
634          --  be a previous definition of the attribute for the same type (in
635          --  which case it is illegal).
636
637          --  In the first case, it will have been analyzed already, and we
638          --  can check that its profile does not match the expected profile
639          --  for a stream attribute of U_Ent. In the second case, either Pnam
640          --  has been analyzed (and has the expected profile), or it has not
641          --  been analyzed yet (case of a type that has not been frozen yet
642          --  and for which the stream attribute has been set using Set_TSS).
643
644          if Present (Pnam)
645            and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
646          then
647             Error_Msg_Sloc := Sloc (Pnam);
648             Error_Msg_Name_1 := Attr;
649             Error_Msg_N ("% attribute already defined #", Nam);
650             return;
651          end if;
652
653          Analyze (Expr);
654
655          if Is_Entity_Name (Expr) then
656             if not Is_Overloaded (Expr) then
657                if Has_Good_Profile (Entity (Expr)) then
658                   Subp := Entity (Expr);
659                end if;
660
661             else
662                Get_First_Interp (Expr, I, It);
663                while Present (It.Nam) loop
664                   if Has_Good_Profile (It.Nam) then
665                      Subp := It.Nam;
666                      exit;
667                   end if;
668
669                   Get_Next_Interp (I, It);
670                end loop;
671             end if;
672          end if;
673
674          if Present (Subp) then
675             if Is_Abstract_Subprogram (Subp) then
676                Error_Msg_N ("stream subprogram must not be abstract", Expr);
677                return;
678             end if;
679
680             Set_Entity (Expr, Subp);
681             Set_Etype (Expr, Etype (Subp));
682
683             New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
684
685          else
686             Error_Msg_Name_1 := Attr;
687             Error_Msg_N ("incorrect expression for% attribute", Expr);
688          end if;
689       end Analyze_Stream_TSS_Definition;
690
691    --  Start of processing for Analyze_Attribute_Definition_Clause
692
693    begin
694       --  Process Ignore_Rep_Clauses option
695
696       if Ignore_Rep_Clauses then
697          case Id is
698
699             --  The following should be ignored. They do not affect legality
700             --  and may be target dependent. The basic idea of -gnatI is to
701             --  ignore any rep clauses that may be target dependent but do not
702             --  affect legality (except possibly to be rejected because they
703             --  are incompatible with the compilation target).
704
705             when Attribute_Address        |
706                  Attribute_Alignment      |
707                  Attribute_Bit_Order      |
708                  Attribute_Component_Size |
709                  Attribute_Machine_Radix  |
710                  Attribute_Object_Size    |
711                  Attribute_Size           |
712                  Attribute_Small          |
713                  Attribute_Stream_Size    |
714                  Attribute_Value_Size     =>
715
716                Rewrite (N, Make_Null_Statement (Sloc (N)));
717                return;
718
719             --  The following should not be ignored, because in the first place
720             --  they are reasonably portable, and should not cause problems in
721             --  compiling code from another target, and also they do affect
722             --  legality, e.g. failing to provide a stream attribute for a
723             --  type may make a program illegal.
724
725             when Attribute_External_Tag   |
726                  Attribute_Input          |
727                  Attribute_Output         |
728                  Attribute_Read           |
729                  Attribute_Storage_Pool   |
730                  Attribute_Storage_Size   |
731                  Attribute_Write          =>
732                null;
733
734             --  Other cases are errors, which will be caught below
735
736             when others =>
737                null;
738          end case;
739       end if;
740
741       Analyze (Nam);
742       Ent := Entity (Nam);
743
744       if Rep_Item_Too_Early (Ent, N) then
745          return;
746       end if;
747
748       --  Rep clause applies to full view of incomplete type or private type if
749       --  we have one (if not, this is a premature use of the type). However,
750       --  certain semantic checks need to be done on the specified entity (i.e.
751       --  the private view), so we save it in Ent.
752
753       if Is_Private_Type (Ent)
754         and then Is_Derived_Type (Ent)
755         and then not Is_Tagged_Type (Ent)
756         and then No (Full_View (Ent))
757       then
758          --  If this is a private type whose completion is a derivation from
759          --  another private type, there is no full view, and the attribute
760          --  belongs to the type itself, not its underlying parent.
761
762          U_Ent := Ent;
763
764       elsif Ekind (Ent) = E_Incomplete_Type then
765
766          --  The attribute applies to the full view, set the entity of the
767          --  attribute definition accordingly.
768
769          Ent := Underlying_Type (Ent);
770          U_Ent := Ent;
771          Set_Entity (Nam, Ent);
772
773       else
774          U_Ent := Underlying_Type (Ent);
775       end if;
776
777       --  Complete other routine error checks
778
779       if Etype (Nam) = Any_Type then
780          return;
781
782       elsif Scope (Ent) /= Current_Scope then
783          Error_Msg_N ("entity must be declared in this scope", Nam);
784          return;
785
786       elsif No (U_Ent) then
787          U_Ent := Ent;
788
789       elsif Is_Type (U_Ent)
790         and then not Is_First_Subtype (U_Ent)
791         and then Id /= Attribute_Object_Size
792         and then Id /= Attribute_Value_Size
793         and then not From_At_Mod (N)
794       then
795          Error_Msg_N ("cannot specify attribute for subtype", Nam);
796          return;
797       end if;
798
799       --  Switch on particular attribute
800
801       case Id is
802
803          -------------
804          -- Address --
805          -------------
806
807          --  Address attribute definition clause
808
809          when Attribute_Address => Address : begin
810
811             --  A little error check, catch for X'Address use X'Address;
812
813             if Nkind (Nam) = N_Identifier
814               and then Nkind (Expr) = N_Attribute_Reference
815               and then Attribute_Name (Expr) = Name_Address
816               and then Nkind (Prefix (Expr)) = N_Identifier
817               and then Chars (Nam) = Chars (Prefix (Expr))
818             then
819                Error_Msg_NE
820                  ("address for & is self-referencing", Prefix (Expr), Ent);
821                return;
822             end if;
823
824             --  Not that special case, carry on with analysis of expression
825
826             Analyze_And_Resolve (Expr, RTE (RE_Address));
827
828             if Present (Address_Clause (U_Ent)) then
829                Error_Msg_N ("address already given for &", Nam);
830
831             --  Case of address clause for subprogram
832
833             elsif Is_Subprogram (U_Ent) then
834                if Has_Homonym (U_Ent) then
835                   Error_Msg_N
836                     ("address clause cannot be given " &
837                      "for overloaded subprogram",
838                      Nam);
839                   return;
840                end if;
841
842                --  For subprograms, all address clauses are permitted, and we
843                --  mark the subprogram as having a deferred freeze so that Gigi
844                --  will not elaborate it too soon.
845
846                --  Above needs more comments, what is too soon about???
847
848                Set_Has_Delayed_Freeze (U_Ent);
849
850             --  Case of address clause for entry
851
852             elsif Ekind (U_Ent) = E_Entry then
853                if Nkind (Parent (N)) = N_Task_Body then
854                   Error_Msg_N
855                     ("entry address must be specified in task spec", Nam);
856                   return;
857                end if;
858
859                --  For entries, we require a constant address
860
861                Check_Constant_Address_Clause (Expr, U_Ent);
862
863                --  Special checks for task types
864
865                if Is_Task_Type (Scope (U_Ent))
866                  and then Comes_From_Source (Scope (U_Ent))
867                then
868                   Error_Msg_N
869                     ("?entry address declared for entry in task type", N);
870                   Error_Msg_N
871                     ("\?only one task can be declared of this type", N);
872                end if;
873
874                --  Entry address clauses are obsolescent
875
876                Check_Restriction (No_Obsolescent_Features, N);
877
878                if Warn_On_Obsolescent_Feature then
879                   Error_Msg_N
880                     ("attaching interrupt to task entry is an " &
881                      "obsolescent feature (RM J.7.1)?", N);
882                   Error_Msg_N
883                     ("\use interrupt procedure instead?", N);
884                end if;
885
886             --  Case of an address clause for a controlled object which we
887             --  consider to be erroneous.
888
889             elsif Is_Controlled (Etype (U_Ent))
890               or else Has_Controlled_Component (Etype (U_Ent))
891             then
892                Error_Msg_NE
893                  ("?controlled object& must not be overlaid", Nam, U_Ent);
894                Error_Msg_N
895                  ("\?Program_Error will be raised at run time", Nam);
896                Insert_Action (Declaration_Node (U_Ent),
897                  Make_Raise_Program_Error (Loc,
898                    Reason => PE_Overlaid_Controlled_Object));
899                return;
900
901             --  Case of address clause for a (non-controlled) object
902
903             elsif
904               Ekind (U_Ent) = E_Variable
905                 or else
906               Ekind (U_Ent) = E_Constant
907             then
908                declare
909                   Expr  : constant Node_Id   := Expression (N);
910                   Aent  : constant Entity_Id := Address_Aliased_Entity (Expr);
911                   Ent_Y : constant Entity_Id := Find_Overlaid_Object (N);
912
913                begin
914                   --  Exported variables cannot have an address clause,
915                   --  because this cancels the effect of the pragma Export
916
917                   if Is_Exported (U_Ent) then
918                      Error_Msg_N
919                        ("cannot export object with address clause", Nam);
920                      return;
921
922                   --  Overlaying controlled objects is erroneous
923
924                   elsif Present (Aent)
925                     and then (Has_Controlled_Component (Etype (Aent))
926                                 or else Is_Controlled (Etype (Aent)))
927                   then
928                      Error_Msg_N
929                        ("?cannot overlay with controlled object", Expr);
930                      Error_Msg_N
931                        ("\?Program_Error will be raised at run time", Expr);
932                      Insert_Action (Declaration_Node (U_Ent),
933                        Make_Raise_Program_Error (Loc,
934                          Reason => PE_Overlaid_Controlled_Object));
935                      return;
936
937                   elsif Present (Aent)
938                     and then Ekind (U_Ent) = E_Constant
939                     and then Ekind (Aent) /= E_Constant
940                   then
941                      Error_Msg_N ("constant overlays a variable?", Expr);
942
943                   elsif Present (Renamed_Object (U_Ent)) then
944                      Error_Msg_N
945                        ("address clause not allowed"
946                           & " for a renaming declaration (RM 13.1(6))", Nam);
947                      return;
948
949                   --  Imported variables can have an address clause, but then
950                   --  the import is pretty meaningless except to suppress
951                   --  initializations, so we do not need such variables to
952                   --  be statically allocated (and in fact it causes trouble
953                   --  if the address clause is a local value).
954
955                   elsif Is_Imported (U_Ent) then
956                      Set_Is_Statically_Allocated (U_Ent, False);
957                   end if;
958
959                   --  We mark a possible modification of a variable with an
960                   --  address clause, since it is likely aliasing is occurring.
961
962                   Note_Possible_Modification (Nam, Sure => False);
963
964                   --  Here we are checking for explicit overlap of one variable
965                   --  by another, and if we find this then mark the overlapped
966                   --  variable as also being volatile to prevent unwanted
967                   --  optimizations.
968
969                   if Present (Ent_Y) then
970                      Set_Treat_As_Volatile (Ent_Y);
971                   end if;
972
973                   --  Legality checks on the address clause for initialized
974                   --  objects is deferred until the freeze point, because
975                   --  a subsequent pragma might indicate that the object is
976                   --  imported and thus not initialized.
977
978                   Set_Has_Delayed_Freeze (U_Ent);
979
980                   if Is_Exported (U_Ent) then
981                      Error_Msg_N
982                        ("& cannot be exported if an address clause is given",
983                         Nam);
984                      Error_Msg_N
985                        ("\define and export a variable " &
986                         "that holds its address instead",
987                         Nam);
988                   end if;
989
990                   --  Entity has delayed freeze, so we will generate an
991                   --  alignment check at the freeze point unless suppressed.
992
993                   if not Range_Checks_Suppressed (U_Ent)
994                     and then not Alignment_Checks_Suppressed (U_Ent)
995                   then
996                      Set_Check_Address_Alignment (N);
997                   end if;
998
999                   --  Kill the size check code, since we are not allocating
1000                   --  the variable, it is somewhere else.
1001
1002                   Kill_Size_Check_Code (U_Ent);
1003                end;
1004
1005                --  If the address clause is of the form:
1006
1007                --    for Y'Address use X'Address
1008
1009                --  or
1010
1011                --    Const : constant Address := X'Address;
1012                --    ...
1013                --    for Y'Address use Const;
1014
1015                --  then we make an entry in the table for checking the size and
1016                --  alignment of the overlaying variable. We defer this check
1017                --  till after code generation to take full advantage of the
1018                --  annotation done by the back end. This entry is only made if
1019                --  we have not already posted a warning about size/alignment
1020                --  (some warnings of this type are posted in Checks), and if
1021                --  the address clause comes from source.
1022
1023                if Address_Clause_Overlay_Warnings
1024                  and then Comes_From_Source (N)
1025                then
1026                   declare
1027                      Ent_X : Entity_Id := Empty;
1028                      Ent_Y : Entity_Id := Empty;
1029
1030                   begin
1031                      Ent_Y := Find_Overlaid_Object (N);
1032
1033                      if Present (Ent_Y) and then Is_Entity_Name (Name (N)) then
1034                         Ent_X := Entity (Name (N));
1035                         Address_Clause_Checks.Append ((N, Ent_X, Ent_Y));
1036
1037                         --  If variable overlays a constant view, and we are
1038                         --  warning on overlays, then mark the variable as
1039                         --  overlaying a constant (we will give warnings later
1040                         --  if this variable is assigned).
1041
1042                         if Is_Constant_Object (Ent_Y)
1043                           and then Ekind (Ent_X) = E_Variable
1044                         then
1045                            Set_Overlays_Constant (Ent_X);
1046                         end if;
1047                      end if;
1048                   end;
1049                end if;
1050
1051             --  Not a valid entity for an address clause
1052
1053             else
1054                Error_Msg_N ("address cannot be given for &", Nam);
1055             end if;
1056          end Address;
1057
1058          ---------------
1059          -- Alignment --
1060          ---------------
1061
1062          --  Alignment attribute definition clause
1063
1064          when Attribute_Alignment => Alignment_Block : declare
1065             Align : constant Uint := Get_Alignment_Value (Expr);
1066
1067          begin
1068             FOnly := True;
1069
1070             if not Is_Type (U_Ent)
1071               and then Ekind (U_Ent) /= E_Variable
1072               and then Ekind (U_Ent) /= E_Constant
1073             then
1074                Error_Msg_N ("alignment cannot be given for &", Nam);
1075
1076             elsif Has_Alignment_Clause (U_Ent) then
1077                Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1078                Error_Msg_N ("alignment clause previously given#", N);
1079
1080             elsif Align /= No_Uint then
1081                Set_Has_Alignment_Clause (U_Ent);
1082                Set_Alignment            (U_Ent, Align);
1083             end if;
1084          end Alignment_Block;
1085
1086          ---------------
1087          -- Bit_Order --
1088          ---------------
1089
1090          --  Bit_Order attribute definition clause
1091
1092          when Attribute_Bit_Order => Bit_Order : declare
1093          begin
1094             if not Is_Record_Type (U_Ent) then
1095                Error_Msg_N
1096                  ("Bit_Order can only be defined for record type", Nam);
1097
1098             else
1099                Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1100
1101                if Etype (Expr) = Any_Type then
1102                   return;
1103
1104                elsif not Is_Static_Expression (Expr) then
1105                   Flag_Non_Static_Expr
1106                     ("Bit_Order requires static expression!", Expr);
1107
1108                else
1109                   if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1110                      Set_Reverse_Bit_Order (U_Ent, True);
1111                   end if;
1112                end if;
1113             end if;
1114          end Bit_Order;
1115
1116          --------------------
1117          -- Component_Size --
1118          --------------------
1119
1120          --  Component_Size attribute definition clause
1121
1122          when Attribute_Component_Size => Component_Size_Case : declare
1123             Csize    : constant Uint := Static_Integer (Expr);
1124             Btype    : Entity_Id;
1125             Biased   : Boolean;
1126             New_Ctyp : Entity_Id;
1127             Decl     : Node_Id;
1128
1129          begin
1130             if not Is_Array_Type (U_Ent) then
1131                Error_Msg_N ("component size requires array type", Nam);
1132                return;
1133             end if;
1134
1135             Btype := Base_Type (U_Ent);
1136
1137             if Has_Component_Size_Clause (Btype) then
1138                Error_Msg_N
1139                  ("component size clause for& previously given", Nam);
1140
1141             elsif Csize /= No_Uint then
1142                Check_Size (Expr, Component_Type (Btype), Csize, Biased);
1143
1144                if Has_Aliased_Components (Btype)
1145                  and then Csize < 32
1146                  and then Csize /= 8
1147                  and then Csize /= 16
1148                then
1149                   Error_Msg_N
1150                     ("component size incorrect for aliased components", N);
1151                   return;
1152                end if;
1153
1154                --  For the biased case, build a declaration for a subtype
1155                --  that will be used to represent the biased subtype that
1156                --  reflects the biased representation of components. We need
1157                --  this subtype to get proper conversions on referencing
1158                --  elements of the array. Note that component size clauses
1159                --  are ignored in VM mode.
1160
1161                if VM_Target = No_VM then
1162                   if Biased then
1163                      New_Ctyp :=
1164                        Make_Defining_Identifier (Loc,
1165                          Chars =>
1166                            New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1167
1168                      Decl :=
1169                        Make_Subtype_Declaration (Loc,
1170                          Defining_Identifier => New_Ctyp,
1171                          Subtype_Indication  =>
1172                            New_Occurrence_Of (Component_Type (Btype), Loc));
1173
1174                      Set_Parent (Decl, N);
1175                      Analyze (Decl, Suppress => All_Checks);
1176
1177                      Set_Has_Delayed_Freeze        (New_Ctyp, False);
1178                      Set_Esize                     (New_Ctyp, Csize);
1179                      Set_RM_Size                   (New_Ctyp, Csize);
1180                      Init_Alignment                (New_Ctyp);
1181                      Set_Has_Biased_Representation (New_Ctyp, True);
1182                      Set_Is_Itype                  (New_Ctyp, True);
1183                      Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1184
1185                      Set_Component_Type (Btype, New_Ctyp);
1186
1187                      if Warn_On_Biased_Representation then
1188                         Error_Msg_N
1189                           ("?component size clause forces biased "
1190                            & "representation", N);
1191                      end if;
1192                   end if;
1193
1194                   Set_Component_Size (Btype, Csize);
1195
1196                --  For VM case, we ignore component size clauses
1197
1198                else
1199                   --  Give a warning unless we are in GNAT mode, in which case
1200                   --  the warning is suppressed since it is not useful.
1201
1202                   if not GNAT_Mode then
1203                      Error_Msg_N
1204                        ("?component size ignored in this configuration", N);
1205                   end if;
1206                end if;
1207
1208                Set_Has_Component_Size_Clause (Btype, True);
1209                Set_Has_Non_Standard_Rep      (Btype, True);
1210             end if;
1211          end Component_Size_Case;
1212
1213          ------------------
1214          -- External_Tag --
1215          ------------------
1216
1217          when Attribute_External_Tag => External_Tag :
1218          begin
1219             if not Is_Tagged_Type (U_Ent) then
1220                Error_Msg_N ("should be a tagged type", Nam);
1221             end if;
1222
1223             Analyze_And_Resolve (Expr, Standard_String);
1224
1225             if not Is_Static_Expression (Expr) then
1226                Flag_Non_Static_Expr
1227                  ("static string required for tag name!", Nam);
1228             end if;
1229
1230             if VM_Target = No_VM then
1231                Set_Has_External_Tag_Rep_Clause (U_Ent);
1232             elsif not Inspector_Mode then
1233                Error_Msg_Name_1 := Attr;
1234                Error_Msg_N
1235                  ("% attribute unsupported in this configuration", Nam);
1236             end if;
1237
1238             if not Is_Library_Level_Entity (U_Ent) then
1239                Error_Msg_NE
1240                  ("?non-unique external tag supplied for &", N, U_Ent);
1241                Error_Msg_N
1242                  ("?\same external tag applies to all subprogram calls", N);
1243                Error_Msg_N
1244                  ("?\corresponding internal tag cannot be obtained", N);
1245             end if;
1246          end External_Tag;
1247
1248          -----------
1249          -- Input --
1250          -----------
1251
1252          when Attribute_Input =>
1253             Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1254             Set_Has_Specified_Stream_Input (Ent);
1255
1256          -------------------
1257          -- Machine_Radix --
1258          -------------------
1259
1260          --  Machine radix attribute definition clause
1261
1262          when Attribute_Machine_Radix => Machine_Radix : declare
1263             Radix : constant Uint := Static_Integer (Expr);
1264
1265          begin
1266             if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1267                Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1268
1269             elsif Has_Machine_Radix_Clause (U_Ent) then
1270                Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1271                Error_Msg_N ("machine radix clause previously given#", N);
1272
1273             elsif Radix /= No_Uint then
1274                Set_Has_Machine_Radix_Clause (U_Ent);
1275                Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1276
1277                if Radix = 2 then
1278                   null;
1279                elsif Radix = 10 then
1280                   Set_Machine_Radix_10 (U_Ent);
1281                else
1282                   Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1283                end if;
1284             end if;
1285          end Machine_Radix;
1286
1287          -----------------
1288          -- Object_Size --
1289          -----------------
1290
1291          --  Object_Size attribute definition clause
1292
1293          when Attribute_Object_Size => Object_Size : declare
1294             Size : constant Uint := Static_Integer (Expr);
1295
1296             Biased : Boolean;
1297             pragma Warnings (Off, Biased);
1298
1299          begin
1300             if not Is_Type (U_Ent) then
1301                Error_Msg_N ("Object_Size cannot be given for &", Nam);
1302
1303             elsif Has_Object_Size_Clause (U_Ent) then
1304                Error_Msg_N ("Object_Size already given for &", Nam);
1305
1306             else
1307                Check_Size (Expr, U_Ent, Size, Biased);
1308
1309                if Size /= 8
1310                     and then
1311                   Size /= 16
1312                     and then
1313                   Size /= 32
1314                     and then
1315                   UI_Mod (Size, 64) /= 0
1316                then
1317                   Error_Msg_N
1318                     ("Object_Size must be 8, 16, 32, or multiple of 64",
1319                      Expr);
1320                end if;
1321
1322                Set_Esize (U_Ent, Size);
1323                Set_Has_Object_Size_Clause (U_Ent);
1324                Alignment_Check_For_Esize_Change (U_Ent);
1325             end if;
1326          end Object_Size;
1327
1328          ------------
1329          -- Output --
1330          ------------
1331
1332          when Attribute_Output =>
1333             Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1334             Set_Has_Specified_Stream_Output (Ent);
1335
1336          ----------
1337          -- Read --
1338          ----------
1339
1340          when Attribute_Read =>
1341             Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1342             Set_Has_Specified_Stream_Read (Ent);
1343
1344          ----------
1345          -- Size --
1346          ----------
1347
1348          --  Size attribute definition clause
1349
1350          when Attribute_Size => Size : declare
1351             Size   : constant Uint := Static_Integer (Expr);
1352             Etyp   : Entity_Id;
1353             Biased : Boolean;
1354
1355          begin
1356             FOnly := True;
1357
1358             if Has_Size_Clause (U_Ent) then
1359                Error_Msg_N ("size already given for &", Nam);
1360
1361             elsif not Is_Type (U_Ent)
1362               and then Ekind (U_Ent) /= E_Variable
1363               and then Ekind (U_Ent) /= E_Constant
1364             then
1365                Error_Msg_N ("size cannot be given for &", Nam);
1366
1367             elsif Is_Array_Type (U_Ent)
1368               and then not Is_Constrained (U_Ent)
1369             then
1370                Error_Msg_N
1371                  ("size cannot be given for unconstrained array", Nam);
1372
1373             elsif Size /= No_Uint then
1374                if Is_Type (U_Ent) then
1375                   Etyp := U_Ent;
1376                else
1377                   Etyp := Etype (U_Ent);
1378                end if;
1379
1380                --  Check size, note that Gigi is in charge of checking that the
1381                --  size of an array or record type is OK. Also we do not check
1382                --  the size in the ordinary fixed-point case, since it is too
1383                --  early to do so (there may be subsequent small clause that
1384                --  affects the size). We can check the size if a small clause
1385                --  has already been given.
1386
1387                if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1388                  or else Has_Small_Clause (U_Ent)
1389                then
1390                   Check_Size (Expr, Etyp, Size, Biased);
1391                      Set_Has_Biased_Representation (U_Ent, Biased);
1392
1393                   if Biased and Warn_On_Biased_Representation then
1394                      Error_Msg_N
1395                        ("?size clause forces biased representation", N);
1396                   end if;
1397                end if;
1398
1399                --  For types set RM_Size and Esize if possible
1400
1401                if Is_Type (U_Ent) then
1402                   Set_RM_Size (U_Ent, Size);
1403
1404                   --  For scalar types, increase Object_Size to power of 2, but
1405                   --  not less than a storage unit in any case (i.e., normally
1406                   --  this means it will be byte addressable).
1407
1408                   if Is_Scalar_Type (U_Ent) then
1409                      if Size <= System_Storage_Unit then
1410                         Init_Esize (U_Ent, System_Storage_Unit);
1411                      elsif Size <= 16 then
1412                         Init_Esize (U_Ent, 16);
1413                      elsif Size <= 32 then
1414                         Init_Esize (U_Ent, 32);
1415                      else
1416                         Set_Esize  (U_Ent, (Size + 63) / 64 * 64);
1417                      end if;
1418
1419                   --  For all other types, object size = value size. The
1420                   --  backend will adjust as needed.
1421
1422                   else
1423                      Set_Esize (U_Ent, Size);
1424                   end if;
1425
1426                   Alignment_Check_For_Esize_Change (U_Ent);
1427
1428                --  For objects, set Esize only
1429
1430                else
1431                   if Is_Elementary_Type (Etyp) then
1432                      if Size /= System_Storage_Unit
1433                           and then
1434                         Size /= System_Storage_Unit * 2
1435                           and then
1436                         Size /= System_Storage_Unit * 4
1437                            and then
1438                         Size /= System_Storage_Unit * 8
1439                      then
1440                         Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1441                         Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1442                         Error_Msg_N
1443                           ("size for primitive object must be a power of 2"
1444                             & " in the range ^-^", N);
1445                      end if;
1446                   end if;
1447
1448                   Set_Esize (U_Ent, Size);
1449                end if;
1450
1451                Set_Has_Size_Clause (U_Ent);
1452             end if;
1453          end Size;
1454
1455          -----------
1456          -- Small --
1457          -----------
1458
1459          --  Small attribute definition clause
1460
1461          when Attribute_Small => Small : declare
1462             Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1463             Small         : Ureal;
1464
1465          begin
1466             Analyze_And_Resolve (Expr, Any_Real);
1467
1468             if Etype (Expr) = Any_Type then
1469                return;
1470
1471             elsif not Is_Static_Expression (Expr) then
1472                Flag_Non_Static_Expr
1473                  ("small requires static expression!", Expr);
1474                return;
1475
1476             else
1477                Small := Expr_Value_R (Expr);
1478
1479                if Small <= Ureal_0 then
1480                   Error_Msg_N ("small value must be greater than zero", Expr);
1481                   return;
1482                end if;
1483
1484             end if;
1485
1486             if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1487                Error_Msg_N
1488                  ("small requires an ordinary fixed point type", Nam);
1489
1490             elsif Has_Small_Clause (U_Ent) then
1491                Error_Msg_N ("small already given for &", Nam);
1492
1493             elsif Small > Delta_Value (U_Ent) then
1494                Error_Msg_N
1495                  ("small value must not be greater then delta value", Nam);
1496
1497             else
1498                Set_Small_Value (U_Ent, Small);
1499                Set_Small_Value (Implicit_Base, Small);
1500                Set_Has_Small_Clause (U_Ent);
1501                Set_Has_Small_Clause (Implicit_Base);
1502                Set_Has_Non_Standard_Rep (Implicit_Base);
1503             end if;
1504          end Small;
1505
1506          ------------------
1507          -- Storage_Pool --
1508          ------------------
1509
1510          --  Storage_Pool attribute definition clause
1511
1512          when Attribute_Storage_Pool => Storage_Pool : declare
1513             Pool : Entity_Id;
1514             T    : Entity_Id;
1515
1516          begin
1517             if Ekind (U_Ent) = E_Access_Subprogram_Type then
1518                Error_Msg_N
1519                  ("storage pool cannot be given for access-to-subprogram type",
1520                   Nam);
1521                return;
1522
1523             elsif Ekind (U_Ent) /= E_Access_Type
1524               and then Ekind (U_Ent) /= E_General_Access_Type
1525             then
1526                Error_Msg_N
1527                  ("storage pool can only be given for access types", Nam);
1528                return;
1529
1530             elsif Is_Derived_Type (U_Ent) then
1531                Error_Msg_N
1532                  ("storage pool cannot be given for a derived access type",
1533                   Nam);
1534
1535             elsif Has_Storage_Size_Clause (U_Ent) then
1536                Error_Msg_N ("storage size already given for &", Nam);
1537                return;
1538
1539             elsif Present (Associated_Storage_Pool (U_Ent)) then
1540                Error_Msg_N ("storage pool already given for &", Nam);
1541                return;
1542             end if;
1543
1544             Analyze_And_Resolve
1545               (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1546
1547             if not Denotes_Variable (Expr) then
1548                Error_Msg_N ("storage pool must be a variable", Expr);
1549                return;
1550             end if;
1551
1552             if Nkind (Expr) = N_Type_Conversion then
1553                T := Etype (Expression (Expr));
1554             else
1555                T := Etype (Expr);
1556             end if;
1557
1558             --  The Stack_Bounded_Pool is used internally for implementing
1559             --  access types with a Storage_Size. Since it only work
1560             --  properly when used on one specific type, we need to check
1561             --  that it is not hijacked improperly:
1562             --    type T is access Integer;
1563             --    for T'Storage_Size use n;
1564             --    type Q is access Float;
1565             --    for Q'Storage_Size use T'Storage_Size; -- incorrect
1566
1567             if RTE_Available (RE_Stack_Bounded_Pool)
1568               and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1569             then
1570                Error_Msg_N ("non-shareable internal Pool", Expr);
1571                return;
1572             end if;
1573
1574             --  If the argument is a name that is not an entity name, then
1575             --  we construct a renaming operation to define an entity of
1576             --  type storage pool.
1577
1578             if not Is_Entity_Name (Expr)
1579               and then Is_Object_Reference (Expr)
1580             then
1581                Pool :=
1582                  Make_Defining_Identifier (Loc,
1583                    Chars => New_Internal_Name ('P'));
1584
1585                declare
1586                   Rnode : constant Node_Id :=
1587                             Make_Object_Renaming_Declaration (Loc,
1588                               Defining_Identifier => Pool,
1589                               Subtype_Mark        =>
1590                                 New_Occurrence_Of (Etype (Expr), Loc),
1591                               Name => Expr);
1592
1593                begin
1594                   Insert_Before (N, Rnode);
1595                   Analyze (Rnode);
1596                   Set_Associated_Storage_Pool (U_Ent, Pool);
1597                end;
1598
1599             elsif Is_Entity_Name (Expr) then
1600                Pool := Entity (Expr);
1601
1602                --  If pool is a renamed object, get original one. This can
1603                --  happen with an explicit renaming, and within instances.
1604
1605                while Present (Renamed_Object (Pool))
1606                  and then Is_Entity_Name (Renamed_Object (Pool))
1607                loop
1608                   Pool := Entity (Renamed_Object (Pool));
1609                end loop;
1610
1611                if Present (Renamed_Object (Pool))
1612                  and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1613                  and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1614                then
1615                   Pool := Entity (Expression (Renamed_Object (Pool)));
1616                end if;
1617
1618                Set_Associated_Storage_Pool (U_Ent, Pool);
1619
1620             elsif Nkind (Expr) = N_Type_Conversion
1621               and then Is_Entity_Name (Expression (Expr))
1622               and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1623             then
1624                Pool := Entity (Expression (Expr));
1625                Set_Associated_Storage_Pool (U_Ent, Pool);
1626
1627             else
1628                Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1629                return;
1630             end if;
1631          end Storage_Pool;
1632
1633          ------------------
1634          -- Storage_Size --
1635          ------------------
1636
1637          --  Storage_Size attribute definition clause
1638
1639          when Attribute_Storage_Size => Storage_Size : declare
1640             Btype : constant Entity_Id := Base_Type (U_Ent);
1641             Sprag : Node_Id;
1642
1643          begin
1644             if Is_Task_Type (U_Ent) then
1645                Check_Restriction (No_Obsolescent_Features, N);
1646
1647                if Warn_On_Obsolescent_Feature then
1648                   Error_Msg_N
1649                     ("storage size clause for task is an " &
1650                      "obsolescent feature (RM J.9)?", N);
1651                   Error_Msg_N
1652                     ("\use Storage_Size pragma instead?", N);
1653                end if;
1654
1655                FOnly := True;
1656             end if;
1657
1658             if not Is_Access_Type (U_Ent)
1659               and then Ekind (U_Ent) /= E_Task_Type
1660             then
1661                Error_Msg_N ("storage size cannot be given for &", Nam);
1662
1663             elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1664                Error_Msg_N
1665                  ("storage size cannot be given for a derived access type",
1666                   Nam);
1667
1668             elsif Has_Storage_Size_Clause (Btype) then
1669                Error_Msg_N ("storage size already given for &", Nam);
1670
1671             else
1672                Analyze_And_Resolve (Expr, Any_Integer);
1673
1674                if Is_Access_Type (U_Ent) then
1675                   if Present (Associated_Storage_Pool (U_Ent)) then
1676                      Error_Msg_N ("storage pool already given for &", Nam);
1677                      return;
1678                   end if;
1679
1680                   if Compile_Time_Known_Value (Expr)
1681                     and then Expr_Value (Expr) = 0
1682                   then
1683                      Set_No_Pool_Assigned (Btype);
1684                   end if;
1685
1686                else -- Is_Task_Type (U_Ent)
1687                   Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1688
1689                   if Present (Sprag) then
1690                      Error_Msg_Sloc := Sloc (Sprag);
1691                      Error_Msg_N
1692                        ("Storage_Size already specified#", Nam);
1693                      return;
1694                   end if;
1695                end if;
1696
1697                Set_Has_Storage_Size_Clause (Btype);
1698             end if;
1699          end Storage_Size;
1700
1701          -----------------
1702          -- Stream_Size --
1703          -----------------
1704
1705          when Attribute_Stream_Size => Stream_Size : declare
1706             Size : constant Uint := Static_Integer (Expr);
1707
1708          begin
1709             if Ada_Version <= Ada_95 then
1710                Check_Restriction (No_Implementation_Attributes, N);
1711             end if;
1712
1713             if Has_Stream_Size_Clause (U_Ent) then
1714                Error_Msg_N ("Stream_Size already given for &", Nam);
1715
1716             elsif Is_Elementary_Type (U_Ent) then
1717                if Size /= System_Storage_Unit
1718                     and then
1719                   Size /= System_Storage_Unit * 2
1720                     and then
1721                   Size /= System_Storage_Unit * 4
1722                      and then
1723                   Size /= System_Storage_Unit * 8
1724                then
1725                   Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1726                   Error_Msg_N
1727                     ("stream size for elementary type must be a"
1728                        & " power of 2 and at least ^", N);
1729
1730                elsif RM_Size (U_Ent) > Size then
1731                   Error_Msg_Uint_1 := RM_Size (U_Ent);
1732                   Error_Msg_N
1733                     ("stream size for elementary type must be a"
1734                        & " power of 2 and at least ^", N);
1735                end if;
1736
1737                Set_Has_Stream_Size_Clause (U_Ent);
1738
1739             else
1740                Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1741             end if;
1742          end Stream_Size;
1743
1744          ----------------
1745          -- Value_Size --
1746          ----------------
1747
1748          --  Value_Size attribute definition clause
1749
1750          when Attribute_Value_Size => Value_Size : declare
1751             Size   : constant Uint := Static_Integer (Expr);
1752             Biased : Boolean;
1753
1754          begin
1755             if not Is_Type (U_Ent) then
1756                Error_Msg_N ("Value_Size cannot be given for &", Nam);
1757
1758             elsif Present
1759                    (Get_Attribute_Definition_Clause
1760                      (U_Ent, Attribute_Value_Size))
1761             then
1762                Error_Msg_N ("Value_Size already given for &", Nam);
1763
1764             elsif Is_Array_Type (U_Ent)
1765               and then not Is_Constrained (U_Ent)
1766             then
1767                Error_Msg_N
1768                  ("Value_Size cannot be given for unconstrained array", Nam);
1769
1770             else
1771                if Is_Elementary_Type (U_Ent) then
1772                   Check_Size (Expr, U_Ent, Size, Biased);
1773                   Set_Has_Biased_Representation (U_Ent, Biased);
1774
1775                   if Biased and Warn_On_Biased_Representation then
1776                      Error_Msg_N
1777                        ("?value size clause forces biased representation", N);
1778                   end if;
1779                end if;
1780
1781                Set_RM_Size (U_Ent, Size);
1782             end if;
1783          end Value_Size;
1784
1785          -----------
1786          -- Write --
1787          -----------
1788
1789          when Attribute_Write =>
1790             Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1791             Set_Has_Specified_Stream_Write (Ent);
1792
1793          --  All other attributes cannot be set
1794
1795          when others =>
1796             Error_Msg_N
1797               ("attribute& cannot be set with definition clause", N);
1798       end case;
1799
1800       --  The test for the type being frozen must be performed after
1801       --  any expression the clause has been analyzed since the expression
1802       --  itself might cause freezing that makes the clause illegal.
1803
1804       if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1805          return;
1806       end if;
1807    end Analyze_Attribute_Definition_Clause;
1808
1809    ----------------------------
1810    -- Analyze_Code_Statement --
1811    ----------------------------
1812
1813    procedure Analyze_Code_Statement (N : Node_Id) is
1814       HSS   : constant Node_Id   := Parent (N);
1815       SBody : constant Node_Id   := Parent (HSS);
1816       Subp  : constant Entity_Id := Current_Scope;
1817       Stmt  : Node_Id;
1818       Decl  : Node_Id;
1819       StmtO : Node_Id;
1820       DeclO : Node_Id;
1821
1822    begin
1823       --  Analyze and check we get right type, note that this implements the
1824       --  requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1825       --  is the only way that Asm_Insn could possibly be visible.
1826
1827       Analyze_And_Resolve (Expression (N));
1828
1829       if Etype (Expression (N)) = Any_Type then
1830          return;
1831       elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1832          Error_Msg_N ("incorrect type for code statement", N);
1833          return;
1834       end if;
1835
1836       Check_Code_Statement (N);
1837
1838       --  Make sure we appear in the handled statement sequence of a
1839       --  subprogram (RM 13.8(3)).
1840
1841       if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1842         or else Nkind (SBody) /= N_Subprogram_Body
1843       then
1844          Error_Msg_N
1845            ("code statement can only appear in body of subprogram", N);
1846          return;
1847       end if;
1848
1849       --  Do remaining checks (RM 13.8(3)) if not already done
1850
1851       if not Is_Machine_Code_Subprogram (Subp) then
1852          Set_Is_Machine_Code_Subprogram (Subp);
1853
1854          --  No exception handlers allowed
1855
1856          if Present (Exception_Handlers (HSS)) then
1857             Error_Msg_N
1858               ("exception handlers not permitted in machine code subprogram",
1859                First (Exception_Handlers (HSS)));
1860          end if;
1861
1862          --  No declarations other than use clauses and pragmas (we allow
1863          --  certain internally generated declarations as well).
1864
1865          Decl := First (Declarations (SBody));
1866          while Present (Decl) loop
1867             DeclO := Original_Node (Decl);
1868             if Comes_From_Source (DeclO)
1869               and not Nkind_In (DeclO, N_Pragma,
1870                                        N_Use_Package_Clause,
1871                                        N_Use_Type_Clause,
1872                                        N_Implicit_Label_Declaration)
1873             then
1874                Error_Msg_N
1875                  ("this declaration not allowed in machine code subprogram",
1876                   DeclO);
1877             end if;
1878
1879             Next (Decl);
1880          end loop;
1881
1882          --  No statements other than code statements, pragmas, and labels.
1883          --  Again we allow certain internally generated statements.
1884
1885          Stmt := First (Statements (HSS));
1886          while Present (Stmt) loop
1887             StmtO := Original_Node (Stmt);
1888             if Comes_From_Source (StmtO)
1889               and then not Nkind_In (StmtO, N_Pragma,
1890                                             N_Label,
1891                                             N_Code_Statement)
1892             then
1893                Error_Msg_N
1894                  ("this statement is not allowed in machine code subprogram",
1895                   StmtO);
1896             end if;
1897
1898             Next (Stmt);
1899          end loop;
1900       end if;
1901    end Analyze_Code_Statement;
1902
1903    -----------------------------------------------
1904    -- Analyze_Enumeration_Representation_Clause --
1905    -----------------------------------------------
1906
1907    procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1908       Ident    : constant Node_Id    := Identifier (N);
1909       Aggr     : constant Node_Id    := Array_Aggregate (N);
1910       Enumtype : Entity_Id;
1911       Elit     : Entity_Id;
1912       Expr     : Node_Id;
1913       Assoc    : Node_Id;
1914       Choice   : Node_Id;
1915       Val      : Uint;
1916       Err      : Boolean := False;
1917
1918       Lo  : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1919       Hi  : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1920       Min : Uint;
1921       Max : Uint;
1922
1923    begin
1924       if Ignore_Rep_Clauses then
1925          return;
1926       end if;
1927
1928       --  First some basic error checks
1929
1930       Find_Type (Ident);
1931       Enumtype := Entity (Ident);
1932
1933       if Enumtype = Any_Type
1934         or else Rep_Item_Too_Early (Enumtype, N)
1935       then
1936          return;
1937       else
1938          Enumtype := Underlying_Type (Enumtype);
1939       end if;
1940
1941       if not Is_Enumeration_Type (Enumtype) then
1942          Error_Msg_NE
1943            ("enumeration type required, found}",
1944             Ident, First_Subtype (Enumtype));
1945          return;
1946       end if;
1947
1948       --  Ignore rep clause on generic actual type. This will already have
1949       --  been flagged on the template as an error, and this is the safest
1950       --  way to ensure we don't get a junk cascaded message in the instance.
1951
1952       if Is_Generic_Actual_Type (Enumtype) then
1953          return;
1954
1955       --  Type must be in current scope
1956
1957       elsif Scope (Enumtype) /= Current_Scope then
1958          Error_Msg_N ("type must be declared in this scope", Ident);
1959          return;
1960
1961       --  Type must be a first subtype
1962
1963       elsif not Is_First_Subtype (Enumtype) then
1964          Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1965          return;
1966
1967       --  Ignore duplicate rep clause
1968
1969       elsif Has_Enumeration_Rep_Clause (Enumtype) then
1970          Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1971          return;
1972
1973       --  Don't allow rep clause for standard [wide_[wide_]]character
1974
1975       elsif Is_Standard_Character_Type (Enumtype) then
1976          Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1977          return;
1978
1979       --  Check that the expression is a proper aggregate (no parentheses)
1980
1981       elsif Paren_Count (Aggr) /= 0 then
1982          Error_Msg
1983            ("extra parentheses surrounding aggregate not allowed",
1984             First_Sloc (Aggr));
1985          return;
1986
1987       --  All tests passed, so set rep clause in place
1988
1989       else
1990          Set_Has_Enumeration_Rep_Clause (Enumtype);
1991          Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1992       end if;
1993
1994       --  Now we process the aggregate. Note that we don't use the normal
1995       --  aggregate code for this purpose, because we don't want any of the
1996       --  normal expansion activities, and a number of special semantic
1997       --  rules apply (including the component type being any integer type)
1998
1999       Elit := First_Literal (Enumtype);
2000
2001       --  First the positional entries if any
2002
2003       if Present (Expressions (Aggr)) then
2004          Expr := First (Expressions (Aggr));
2005          while Present (Expr) loop
2006             if No (Elit) then
2007                Error_Msg_N ("too many entries in aggregate", Expr);
2008                return;
2009             end if;
2010
2011             Val := Static_Integer (Expr);
2012
2013             --  Err signals that we found some incorrect entries processing
2014             --  the list. The final checks for completeness and ordering are
2015             --  skipped in this case.
2016
2017             if Val = No_Uint then
2018                Err := True;
2019             elsif Val < Lo or else Hi < Val then
2020                Error_Msg_N ("value outside permitted range", Expr);
2021                Err := True;
2022             end if;
2023
2024             Set_Enumeration_Rep (Elit, Val);
2025             Set_Enumeration_Rep_Expr (Elit, Expr);
2026             Next (Expr);
2027             Next (Elit);
2028          end loop;
2029       end if;
2030
2031       --  Now process the named entries if present
2032
2033       if Present (Component_Associations (Aggr)) then
2034          Assoc := First (Component_Associations (Aggr));
2035          while Present (Assoc) loop
2036             Choice := First (Choices (Assoc));
2037
2038             if Present (Next (Choice)) then
2039                Error_Msg_N
2040                  ("multiple choice not allowed here", Next (Choice));
2041                Err := True;
2042             end if;
2043
2044             if Nkind (Choice) = N_Others_Choice then
2045                Error_Msg_N ("others choice not allowed here", Choice);
2046                Err := True;
2047
2048             elsif Nkind (Choice) = N_Range then
2049                --  ??? should allow zero/one element range here
2050                Error_Msg_N ("range not allowed here", Choice);
2051                Err := True;
2052
2053             else
2054                Analyze_And_Resolve (Choice, Enumtype);
2055
2056                if Is_Entity_Name (Choice)
2057                  and then Is_Type (Entity (Choice))
2058                then
2059                   Error_Msg_N ("subtype name not allowed here", Choice);
2060                   Err := True;
2061                   --  ??? should allow static subtype with zero/one entry
2062
2063                elsif Etype (Choice) = Base_Type (Enumtype) then
2064                   if not Is_Static_Expression (Choice) then
2065                      Flag_Non_Static_Expr
2066                        ("non-static expression used for choice!", Choice);
2067                      Err := True;
2068
2069                   else
2070                      Elit := Expr_Value_E (Choice);
2071
2072                      if Present (Enumeration_Rep_Expr (Elit)) then
2073                         Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2074                         Error_Msg_NE
2075                           ("representation for& previously given#",
2076                            Choice, Elit);
2077                         Err := True;
2078                      end if;
2079
2080                      Set_Enumeration_Rep_Expr (Elit, Choice);
2081
2082                      Expr := Expression (Assoc);
2083                      Val := Static_Integer (Expr);
2084
2085                      if Val = No_Uint then
2086                         Err := True;
2087
2088                      elsif Val < Lo or else Hi < Val then
2089                         Error_Msg_N ("value outside permitted range", Expr);
2090                         Err := True;
2091                      end if;
2092
2093                      Set_Enumeration_Rep (Elit, Val);
2094                   end if;
2095                end if;
2096             end if;
2097
2098             Next (Assoc);
2099          end loop;
2100       end if;
2101
2102       --  Aggregate is fully processed. Now we check that a full set of
2103       --  representations was given, and that they are in range and in order.
2104       --  These checks are only done if no other errors occurred.
2105
2106       if not Err then
2107          Min  := No_Uint;
2108          Max  := No_Uint;
2109
2110          Elit := First_Literal (Enumtype);
2111          while Present (Elit) loop
2112             if No (Enumeration_Rep_Expr (Elit)) then
2113                Error_Msg_NE ("missing representation for&!", N, Elit);
2114
2115             else
2116                Val := Enumeration_Rep (Elit);
2117
2118                if Min = No_Uint then
2119                   Min := Val;
2120                end if;
2121
2122                if Val /= No_Uint then
2123                   if Max /= No_Uint and then Val <= Max then
2124                      Error_Msg_NE
2125                        ("enumeration value for& not ordered!",
2126                                        Enumeration_Rep_Expr (Elit), Elit);
2127                   end if;
2128
2129                   Max := Val;
2130                end if;
2131
2132                --  If there is at least one literal whose representation
2133                --  is not equal to the Pos value, then note that this
2134                --  enumeration type has a non-standard representation.
2135
2136                if Val /= Enumeration_Pos (Elit) then
2137                   Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2138                end if;
2139             end if;
2140
2141             Next (Elit);
2142          end loop;
2143
2144          --  Now set proper size information
2145
2146          declare
2147             Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2148
2149          begin
2150             if Has_Size_Clause (Enumtype) then
2151                if Esize (Enumtype) >= Minsize then
2152                   null;
2153
2154                else
2155                   Minsize :=
2156                     UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2157
2158                   if Esize (Enumtype) < Minsize then
2159                      Error_Msg_N ("previously given size is too small", N);
2160
2161                   else
2162                      Set_Has_Biased_Representation (Enumtype);
2163                   end if;
2164                end if;
2165
2166             else
2167                Set_RM_Size    (Enumtype, Minsize);
2168                Set_Enum_Esize (Enumtype);
2169             end if;
2170
2171             Set_RM_Size   (Base_Type (Enumtype), RM_Size   (Enumtype));
2172             Set_Esize     (Base_Type (Enumtype), Esize     (Enumtype));
2173             Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2174          end;
2175       end if;
2176
2177       --  We repeat the too late test in case it froze itself!
2178
2179       if Rep_Item_Too_Late (Enumtype, N) then
2180          null;
2181       end if;
2182    end Analyze_Enumeration_Representation_Clause;
2183
2184    ----------------------------
2185    -- Analyze_Free_Statement --
2186    ----------------------------
2187
2188    procedure Analyze_Free_Statement (N : Node_Id) is
2189    begin
2190       Analyze (Expression (N));
2191    end Analyze_Free_Statement;
2192
2193    ------------------------------------------
2194    -- Analyze_Record_Representation_Clause --
2195    ------------------------------------------
2196
2197    procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2198       Loc     : constant Source_Ptr := Sloc (N);
2199       Ident   : constant Node_Id    := Identifier (N);
2200       Rectype : Entity_Id;
2201       Fent    : Entity_Id;
2202       CC      : Node_Id;
2203       Posit   : Uint;
2204       Fbit    : Uint;
2205       Lbit    : Uint;
2206       Hbit    : Uint := Uint_0;
2207       Comp    : Entity_Id;
2208       Ocomp   : Entity_Id;
2209       Biased  : Boolean;
2210
2211       Max_Bit_So_Far : Uint;
2212       --  Records the maximum bit position so far. If all field positions
2213       --  are monotonically increasing, then we can skip the circuit for
2214       --  checking for overlap, since no overlap is possible.
2215
2216       Overlap_Check_Required : Boolean;
2217       --  Used to keep track of whether or not an overlap check is required
2218
2219       Ccount : Natural := 0;
2220       --  Number of component clauses in record rep clause
2221
2222       CR_Pragma : Node_Id := Empty;
2223       --  Points to N_Pragma node if Complete_Representation pragma present
2224
2225    begin
2226       if Ignore_Rep_Clauses then
2227          return;
2228       end if;
2229
2230       Find_Type (Ident);
2231       Rectype := Entity (Ident);
2232
2233       if Rectype = Any_Type
2234         or else Rep_Item_Too_Early (Rectype, N)
2235       then
2236          return;
2237       else
2238          Rectype := Underlying_Type (Rectype);
2239       end if;
2240
2241       --  First some basic error checks
2242
2243       if not Is_Record_Type (Rectype) then
2244          Error_Msg_NE
2245            ("record type required, found}", Ident, First_Subtype (Rectype));
2246          return;
2247
2248       elsif Is_Unchecked_Union (Rectype) then
2249          Error_Msg_N
2250            ("record rep clause not allowed for Unchecked_Union", N);
2251
2252       elsif Scope (Rectype) /= Current_Scope then
2253          Error_Msg_N ("type must be declared in this scope", N);
2254          return;
2255
2256       elsif not Is_First_Subtype (Rectype) then
2257          Error_Msg_N ("cannot give record rep clause for subtype", N);
2258          return;
2259
2260       elsif Has_Record_Rep_Clause (Rectype) then
2261          Error_Msg_N ("duplicate record rep clause ignored", N);
2262          return;
2263
2264       elsif Rep_Item_Too_Late (Rectype, N) then
2265          return;
2266       end if;
2267
2268       if Present (Mod_Clause (N)) then
2269          declare
2270             Loc     : constant Source_Ptr := Sloc (N);
2271             M       : constant Node_Id := Mod_Clause (N);
2272             P       : constant List_Id := Pragmas_Before (M);
2273             AtM_Nod : Node_Id;
2274
2275             Mod_Val : Uint;
2276             pragma Warnings (Off, Mod_Val);
2277
2278          begin
2279             Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2280
2281             if Warn_On_Obsolescent_Feature then
2282                Error_Msg_N
2283                  ("mod clause is an obsolescent feature (RM J.8)?", N);
2284                Error_Msg_N
2285                  ("\use alignment attribute definition clause instead?", N);
2286             end if;
2287
2288             if Present (P) then
2289                Analyze_List (P);
2290             end if;
2291
2292             --  In ASIS_Mode mode, expansion is disabled, but we must convert
2293             --  the Mod clause into an alignment clause anyway, so that the
2294             --  back-end can compute and back-annotate properly the size and
2295             --  alignment of types that may include this record.
2296
2297             --  This seems dubious, this destroys the source tree in a manner
2298             --  not detectable by ASIS ???
2299
2300             if Operating_Mode = Check_Semantics
2301               and then ASIS_Mode
2302             then
2303                AtM_Nod :=
2304                  Make_Attribute_Definition_Clause (Loc,
2305                    Name       => New_Reference_To (Base_Type (Rectype), Loc),
2306                    Chars      => Name_Alignment,
2307                    Expression => Relocate_Node (Expression (M)));
2308
2309                Set_From_At_Mod (AtM_Nod);
2310                Insert_After (N, AtM_Nod);
2311                Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2312                Set_Mod_Clause (N, Empty);
2313
2314             else
2315                --  Get the alignment value to perform error checking
2316
2317                Mod_Val := Get_Alignment_Value (Expression (M));
2318
2319             end if;
2320          end;
2321       end if;
2322
2323       --  For untagged types, clear any existing component clauses for the
2324       --  type. If the type is derived, this is what allows us to override
2325       --  a rep clause for the parent. For type extensions, the representation
2326       --  of the inherited components is inherited, so we want to keep previous
2327       --  component clauses for completeness.
2328
2329       if not Is_Tagged_Type (Rectype) then
2330          Comp := First_Component_Or_Discriminant (Rectype);
2331          while Present (Comp) loop
2332             Set_Component_Clause (Comp, Empty);
2333             Next_Component_Or_Discriminant (Comp);
2334          end loop;
2335       end if;
2336
2337       --  All done if no component clauses
2338
2339       CC := First (Component_Clauses (N));
2340
2341       if No (CC) then
2342          return;
2343       end if;
2344
2345       --  If a tag is present, then create a component clause that places it
2346       --  at the start of the record (otherwise gigi may place it after other
2347       --  fields that have rep clauses).
2348
2349       Fent := First_Entity (Rectype);
2350
2351       if Nkind (Fent) = N_Defining_Identifier
2352         and then Chars (Fent) = Name_uTag
2353       then
2354          Set_Component_Bit_Offset    (Fent, Uint_0);
2355          Set_Normalized_Position     (Fent, Uint_0);
2356          Set_Normalized_First_Bit    (Fent, Uint_0);
2357          Set_Normalized_Position_Max (Fent, Uint_0);
2358          Init_Esize                  (Fent, System_Address_Size);
2359
2360          Set_Component_Clause    (Fent,
2361            Make_Component_Clause (Loc,
2362              Component_Name =>
2363                Make_Identifier (Loc,
2364                  Chars => Name_uTag),
2365
2366              Position  =>
2367                Make_Integer_Literal (Loc,
2368                  Intval => Uint_0),
2369
2370              First_Bit =>
2371                Make_Integer_Literal (Loc,
2372                  Intval => Uint_0),
2373
2374              Last_Bit  =>
2375                Make_Integer_Literal (Loc,
2376                  UI_From_Int (System_Address_Size))));
2377
2378          Ccount := Ccount + 1;
2379       end if;
2380
2381       --  A representation like this applies to the base type
2382
2383       Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2384       Set_Has_Non_Standard_Rep  (Base_Type (Rectype));
2385       Set_Has_Specified_Layout  (Base_Type (Rectype));
2386
2387       Max_Bit_So_Far := Uint_Minus_1;
2388       Overlap_Check_Required := False;
2389
2390       --  Process the component clauses
2391
2392       while Present (CC) loop
2393
2394          --  Pragma
2395
2396          if Nkind (CC) = N_Pragma then
2397             Analyze (CC);
2398
2399             --  The only pragma of interest is Complete_Representation
2400
2401             if Pragma_Name (CC) = Name_Complete_Representation then
2402                CR_Pragma := CC;
2403             end if;
2404
2405          --  Processing for real component clause
2406
2407          else
2408             Ccount := Ccount + 1;
2409             Posit := Static_Integer (Position  (CC));
2410             Fbit  := Static_Integer (First_Bit (CC));
2411             Lbit  := Static_Integer (Last_Bit  (CC));
2412
2413             if Posit /= No_Uint
2414               and then Fbit /= No_Uint
2415               and then Lbit /= No_Uint
2416             then
2417                if Posit < 0 then
2418                   Error_Msg_N
2419                     ("position cannot be negative", Position (CC));
2420
2421                elsif Fbit < 0 then
2422                   Error_Msg_N
2423                     ("first bit cannot be negative", First_Bit (CC));
2424
2425                --  The Last_Bit specified in a component clause must not be
2426                --  less than the First_Bit minus one (RM-13.5.1(10)).
2427
2428                elsif Lbit < Fbit - 1 then
2429                   Error_Msg_N
2430                     ("last bit cannot be less than first bit minus one",
2431                      Last_Bit (CC));
2432
2433                --  Values look OK, so find the corresponding record component
2434                --  Even though the syntax allows an attribute reference for
2435                --  implementation-defined components, GNAT does not allow the
2436                --  tag to get an explicit position.
2437
2438                elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2439                   if Attribute_Name (Component_Name (CC)) = Name_Tag then
2440                      Error_Msg_N ("position of tag cannot be specified", CC);
2441                   else
2442                      Error_Msg_N ("illegal component name", CC);
2443                   end if;
2444
2445                else
2446                   Comp := First_Entity (Rectype);
2447                   while Present (Comp) loop
2448                      exit when Chars (Comp) = Chars (Component_Name (CC));
2449                      Next_Entity (Comp);
2450                   end loop;
2451
2452                   if No (Comp) then
2453
2454                      --  Maybe component of base type that is absent from
2455                      --  statically constrained first subtype.
2456
2457                      Comp := First_Entity (Base_Type (Rectype));
2458                      while Present (Comp) loop
2459                         exit when Chars (Comp) = Chars (Component_Name (CC));
2460                         Next_Entity (Comp);
2461                      end loop;
2462                   end if;
2463
2464                   if No (Comp) then
2465                      Error_Msg_N
2466                        ("component clause is for non-existent field", CC);
2467
2468                   elsif Present (Component_Clause (Comp)) then
2469
2470                      --  Diagnose duplicate rep clause, or check consistency
2471                      --  if this is an inherited component. In a double fault,
2472                      --  there may be a duplicate inconsistent clause for an
2473                      --  inherited component.
2474
2475                      if Scope (Original_Record_Component (Comp)) = Rectype
2476                        or else Parent (Component_Clause (Comp)) = N
2477                      then
2478                         Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2479                         Error_Msg_N ("component clause previously given#", CC);
2480
2481                      else
2482                         declare
2483                            Rep1 : constant Node_Id := Component_Clause (Comp);
2484                         begin
2485                            if Intval (Position (Rep1)) /=
2486                                                    Intval (Position (CC))
2487                              or else Intval (First_Bit (Rep1)) /=
2488                                                    Intval (First_Bit (CC))
2489                              or else Intval (Last_Bit (Rep1)) /=
2490                                                    Intval (Last_Bit (CC))
2491                            then
2492                               Error_Msg_N ("component clause inconsistent "
2493                                 & "with representation of ancestor", CC);
2494                            elsif Warn_On_Redundant_Constructs then
2495                               Error_Msg_N ("?redundant component clause "
2496                                 & "for inherited component!", CC);
2497                            end if;
2498                         end;
2499                      end if;
2500
2501                   else
2502                      --  Make reference for field in record rep clause and set
2503                      --  appropriate entity field in the field identifier.
2504
2505                      Generate_Reference
2506                        (Comp, Component_Name (CC), Set_Ref => False);
2507                      Set_Entity (Component_Name (CC), Comp);
2508
2509                      --  Update Fbit and Lbit to the actual bit number
2510
2511                      Fbit := Fbit + UI_From_Int (SSU) * Posit;
2512                      Lbit := Lbit + UI_From_Int (SSU) * Posit;
2513
2514                      if Fbit <= Max_Bit_So_Far then
2515                         Overlap_Check_Required := True;
2516                      else
2517                         Max_Bit_So_Far := Lbit;
2518                      end if;
2519
2520                      if Has_Size_Clause (Rectype)
2521                        and then Esize (Rectype) <= Lbit
2522                      then
2523                         Error_Msg_N
2524                           ("bit number out of range of specified size",
2525                            Last_Bit (CC));
2526                      else
2527                         Set_Component_Clause     (Comp, CC);
2528                         Set_Component_Bit_Offset (Comp, Fbit);
2529                         Set_Esize                (Comp, 1 + (Lbit - Fbit));
2530                         Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2531                         Set_Normalized_Position  (Comp, Fbit / SSU);
2532
2533                         Set_Normalized_Position_Max
2534                           (Fent, Normalized_Position (Fent));
2535
2536                         if Is_Tagged_Type (Rectype)
2537                           and then Fbit < System_Address_Size
2538                         then
2539                            Error_Msg_NE
2540                              ("component overlaps tag field of&",
2541                               CC, Rectype);
2542                         end if;
2543
2544                         --  This information is also set in the corresponding
2545                         --  component of the base type, found by accessing the
2546                         --  Original_Record_Component link if it is present.
2547
2548                         Ocomp := Original_Record_Component (Comp);
2549
2550                         if Hbit < Lbit then
2551                            Hbit := Lbit;
2552                         end if;
2553
2554                         Check_Size
2555                           (Component_Name (CC),
2556                            Etype (Comp),
2557                            Esize (Comp),
2558                            Biased);
2559
2560                         Set_Has_Biased_Representation (Comp, Biased);
2561
2562                         if Biased and Warn_On_Biased_Representation then
2563                            Error_Msg_F
2564                              ("?component clause forces biased "
2565                               & "representation", CC);
2566                         end if;
2567
2568                         if Present (Ocomp) then
2569                            Set_Component_Clause     (Ocomp, CC);
2570                            Set_Component_Bit_Offset (Ocomp, Fbit);
2571                            Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2572                            Set_Normalized_Position  (Ocomp, Fbit / SSU);
2573                            Set_Esize                (Ocomp, 1 + (Lbit - Fbit));
2574
2575                            Set_Normalized_Position_Max
2576                              (Ocomp, Normalized_Position (Ocomp));
2577
2578                            Set_Has_Biased_Representation
2579                              (Ocomp, Has_Biased_Representation (Comp));
2580                         end if;
2581
2582                         if Esize (Comp) < 0 then
2583                            Error_Msg_N ("component size is negative", CC);
2584                         end if;
2585                      end if;
2586                   end if;
2587                end if;
2588             end if;
2589          end if;
2590
2591          Next (CC);
2592       end loop;
2593
2594       --  Now that we have processed all the component clauses, check for
2595       --  overlap. We have to leave this till last, since the components can
2596       --  appear in any arbitrary order in the representation clause.
2597
2598       --  We do not need this check if all specified ranges were monotonic,
2599       --  as recorded by Overlap_Check_Required being False at this stage.
2600
2601       --  This first section checks if there are any overlapping entries at
2602       --  all. It does this by sorting all entries and then seeing if there are
2603       --  any overlaps. If there are none, then that is decisive, but if there
2604       --  are overlaps, they may still be OK (they may result from fields in
2605       --  different variants).
2606
2607       if Overlap_Check_Required then
2608          Overlap_Check1 : declare
2609
2610             OC_Fbit : array (0 .. Ccount) of Uint;
2611             --  First-bit values for component clauses, the value is the offset
2612             --  of the first bit of the field from start of record. The zero
2613             --  entry is for use in sorting.
2614
2615             OC_Lbit : array (0 .. Ccount) of Uint;
2616             --  Last-bit values for component clauses, the value is the offset
2617             --  of the last bit of the field from start of record. The zero
2618             --  entry is for use in sorting.
2619
2620             OC_Count : Natural := 0;
2621             --  Count of entries in OC_Fbit and OC_Lbit
2622
2623             function OC_Lt (Op1, Op2 : Natural) return Boolean;
2624             --  Compare routine for Sort
2625
2626             procedure OC_Move (From : Natural; To : Natural);
2627             --  Move routine for Sort
2628
2629             package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
2630
2631             function OC_Lt (Op1, Op2 : Natural) return Boolean is
2632             begin
2633                return OC_Fbit (Op1) < OC_Fbit (Op2);
2634             end OC_Lt;
2635
2636             procedure OC_Move (From : Natural; To : Natural) is
2637             begin
2638                OC_Fbit (To) := OC_Fbit (From);
2639                OC_Lbit (To) := OC_Lbit (From);
2640             end OC_Move;
2641
2642          begin
2643             CC := First (Component_Clauses (N));
2644             while Present (CC) loop
2645                if Nkind (CC) /= N_Pragma then
2646                   Posit := Static_Integer (Position  (CC));
2647                   Fbit  := Static_Integer (First_Bit (CC));
2648                   Lbit  := Static_Integer (Last_Bit  (CC));
2649
2650                   if Posit /= No_Uint
2651                     and then Fbit /= No_Uint
2652                     and then Lbit /= No_Uint
2653                   then
2654                      OC_Count := OC_Count + 1;
2655                      Posit := Posit * SSU;
2656                      OC_Fbit (OC_Count) := Fbit + Posit;
2657                      OC_Lbit (OC_Count) := Lbit + Posit;
2658                   end if;
2659                end if;
2660
2661                Next (CC);
2662             end loop;
2663
2664             Sorting.Sort (OC_Count);
2665
2666             Overlap_Check_Required := False;
2667             for J in 1 .. OC_Count - 1 loop
2668                if OC_Lbit (J) >= OC_Fbit (J + 1) then
2669                   Overlap_Check_Required := True;
2670                   exit;
2671                end if;
2672             end loop;
2673          end Overlap_Check1;
2674       end if;
2675
2676       --  If Overlap_Check_Required is still True, then we have to do the full
2677       --  scale overlap check, since we have at least two fields that do
2678       --  overlap, and we need to know if that is OK since they are in
2679       --  different variant, or whether we have a definite problem.
2680
2681       if Overlap_Check_Required then
2682          Overlap_Check2 : declare
2683             C1_Ent, C2_Ent : Entity_Id;
2684             --  Entities of components being checked for overlap
2685
2686             Clist : Node_Id;
2687             --  Component_List node whose Component_Items are being checked
2688
2689             Citem : Node_Id;
2690             --  Component declaration for component being checked
2691
2692          begin
2693             C1_Ent := First_Entity (Base_Type (Rectype));
2694
2695             --  Loop through all components in record. For each component check
2696             --  for overlap with any of the preceding elements on the component
2697             --  list containing the component and also, if the component is in
2698             --  a variant, check against components outside the case structure.
2699             --  This latter test is repeated recursively up the variant tree.
2700
2701             Main_Component_Loop : while Present (C1_Ent) loop
2702                if Ekind (C1_Ent) /= E_Component
2703                  and then Ekind (C1_Ent) /= E_Discriminant
2704                then
2705                   goto Continue_Main_Component_Loop;
2706                end if;
2707
2708                --  Skip overlap check if entity has no declaration node. This
2709                --  happens with discriminants in constrained derived types.
2710                --  Probably we are missing some checks as a result, but that
2711                --  does not seem terribly serious ???
2712
2713                if No (Declaration_Node (C1_Ent)) then
2714                   goto Continue_Main_Component_Loop;
2715                end if;
2716
2717                Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2718
2719                --  Loop through component lists that need checking. Check the
2720                --  current component list and all lists in variants above us.
2721
2722                Component_List_Loop : loop
2723
2724                   --  If derived type definition, go to full declaration
2725                   --  If at outer level, check discriminants if there are any.
2726
2727                   if Nkind (Clist) = N_Derived_Type_Definition then
2728                      Clist := Parent (Clist);
2729                   end if;
2730
2731                   --  Outer level of record definition, check discriminants
2732
2733                   if Nkind_In (Clist, N_Full_Type_Declaration,
2734                                       N_Private_Type_Declaration)
2735                   then
2736                      if Has_Discriminants (Defining_Identifier (Clist)) then
2737                         C2_Ent :=
2738                           First_Discriminant (Defining_Identifier (Clist));
2739
2740                         while Present (C2_Ent) loop
2741                            exit when C1_Ent = C2_Ent;
2742                            Check_Component_Overlap (C1_Ent, C2_Ent);
2743                            Next_Discriminant (C2_Ent);
2744                         end loop;
2745                      end if;
2746
2747                   --  Record extension case
2748
2749                   elsif Nkind (Clist) = N_Derived_Type_Definition then
2750                      Clist := Empty;
2751
2752                   --  Otherwise check one component list
2753
2754                   else
2755                      Citem := First (Component_Items (Clist));
2756
2757                      while Present (Citem) loop
2758                         if Nkind (Citem) = N_Component_Declaration then
2759                            C2_Ent := Defining_Identifier (Citem);
2760                            exit when C1_Ent = C2_Ent;
2761                            Check_Component_Overlap (C1_Ent, C2_Ent);
2762                         end if;
2763
2764                         Next (Citem);
2765                      end loop;
2766                   end if;
2767
2768                   --  Check for variants above us (the parent of the Clist can
2769                   --  be a variant, in which case its parent is a variant part,
2770                   --  and the parent of the variant part is a component list
2771                   --  whose components must all be checked against the current
2772                   --  component for overlap).
2773
2774                   if Nkind (Parent (Clist)) = N_Variant then
2775                      Clist := Parent (Parent (Parent (Clist)));
2776
2777                   --  Check for possible discriminant part in record, this is
2778                   --  treated essentially as another level in the recursion.
2779                   --  For this case the parent of the component list is the
2780                   --  record definition, and its parent is the full type
2781                   --  declaration containing the discriminant specifications.
2782
2783                   elsif Nkind (Parent (Clist)) = N_Record_Definition then
2784                      Clist := Parent (Parent ((Clist)));
2785
2786                   --  If neither of these two cases, we are at the top of
2787                   --  the tree.
2788
2789                   else
2790                      exit Component_List_Loop;
2791                   end if;
2792                end loop Component_List_Loop;
2793
2794                <<Continue_Main_Component_Loop>>
2795                   Next_Entity (C1_Ent);
2796
2797             end loop Main_Component_Loop;
2798          end Overlap_Check2;
2799       end if;
2800
2801       --  For records that have component clauses for all components, and whose
2802       --  size is less than or equal to 32, we need to know the size in the
2803       --  front end to activate possible packed array processing where the
2804       --  component type is a record.
2805
2806       --  At this stage Hbit + 1 represents the first unused bit from all the
2807       --  component clauses processed, so if the component clauses are
2808       --  complete, then this is the length of the record.
2809
2810       --  For records longer than System.Storage_Unit, and for those where not
2811       --  all components have component clauses, the back end determines the
2812       --  length (it may for example be appropriate to round up the size
2813       --  to some convenient boundary, based on alignment considerations, etc).
2814
2815       if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
2816
2817          --  Nothing to do if at least one component has no component clause
2818
2819          Comp := First_Component_Or_Discriminant (Rectype);
2820          while Present (Comp) loop
2821             exit when No (Component_Clause (Comp));
2822             Next_Component_Or_Discriminant (Comp);
2823          end loop;
2824
2825          --  If we fall out of loop, all components have component clauses
2826          --  and so we can set the size to the maximum value.
2827
2828          if No (Comp) then
2829             Set_RM_Size (Rectype, Hbit + 1);
2830          end if;
2831       end if;
2832
2833       --  Check missing components if Complete_Representation pragma appeared
2834
2835       if Present (CR_Pragma) then
2836          Comp := First_Component_Or_Discriminant (Rectype);
2837          while Present (Comp) loop
2838             if No (Component_Clause (Comp)) then
2839                Error_Msg_NE
2840                  ("missing component clause for &", CR_Pragma, Comp);
2841             end if;
2842
2843             Next_Component_Or_Discriminant (Comp);
2844          end loop;
2845
2846       --  If no Complete_Representation pragma, warn if missing components
2847
2848       elsif Warn_On_Unrepped_Components then
2849          declare
2850             Num_Repped_Components   : Nat := 0;
2851             Num_Unrepped_Components : Nat := 0;
2852
2853          begin
2854             --  First count number of repped and unrepped components
2855
2856             Comp := First_Component_Or_Discriminant (Rectype);
2857             while Present (Comp) loop
2858                if Present (Component_Clause (Comp)) then
2859                   Num_Repped_Components := Num_Repped_Components + 1;
2860                else
2861                   Num_Unrepped_Components := Num_Unrepped_Components + 1;
2862                end if;
2863
2864                Next_Component_Or_Discriminant (Comp);
2865             end loop;
2866
2867             --  We are only interested in the case where there is at least one
2868             --  unrepped component, and at least half the components have rep
2869             --  clauses. We figure that if less than half have them, then the
2870             --  partial rep clause is really intentional. If the component
2871             --  type has no underlying type set at this point (as for a generic
2872             --  formal type), we don't know enough to give a warning on the
2873             --  component.
2874
2875             if Num_Unrepped_Components > 0
2876               and then Num_Unrepped_Components < Num_Repped_Components
2877             then
2878                Comp := First_Component_Or_Discriminant (Rectype);
2879                while Present (Comp) loop
2880                   if No (Component_Clause (Comp))
2881                     and then Comes_From_Source (Comp)
2882                     and then Present (Underlying_Type (Etype (Comp)))
2883                     and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
2884                                 or else Size_Known_At_Compile_Time
2885                                              (Underlying_Type (Etype (Comp))))
2886                     and then not Has_Warnings_Off (Rectype)
2887                   then
2888                      Error_Msg_Sloc := Sloc (Comp);
2889                      Error_Msg_NE
2890                        ("?no component clause given for & declared #",
2891                         N, Comp);
2892                   end if;
2893
2894                   Next_Component_Or_Discriminant (Comp);
2895                end loop;
2896             end if;
2897          end;
2898       end if;
2899    end Analyze_Record_Representation_Clause;
2900
2901    -----------------------------
2902    -- Check_Component_Overlap --
2903    -----------------------------
2904
2905    procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2906    begin
2907       if Present (Component_Clause (C1_Ent))
2908         and then Present (Component_Clause (C2_Ent))
2909       then
2910          --  Exclude odd case where we have two tag fields in the same record,
2911          --  both at location zero. This seems a bit strange, but it seems to
2912          --  happen in some circumstances ???
2913
2914          if Chars (C1_Ent) = Name_uTag
2915            and then Chars (C2_Ent) = Name_uTag
2916          then
2917             return;
2918          end if;
2919
2920          --  Here we check if the two fields overlap
2921
2922          declare
2923             S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2924             S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2925             E1 : constant Uint := S1 + Esize (C1_Ent);
2926             E2 : constant Uint := S2 + Esize (C2_Ent);
2927
2928          begin
2929             if E2 <= S1 or else E1 <= S2 then
2930                null;
2931             else
2932                Error_Msg_Node_2 :=
2933                  Component_Name (Component_Clause (C2_Ent));
2934                Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2935                Error_Msg_Node_1 :=
2936                  Component_Name (Component_Clause (C1_Ent));
2937                Error_Msg_N
2938                  ("component& overlaps & #",
2939                   Component_Name (Component_Clause (C1_Ent)));
2940             end if;
2941          end;
2942       end if;
2943    end Check_Component_Overlap;
2944
2945    -----------------------------------
2946    -- Check_Constant_Address_Clause --
2947    -----------------------------------
2948
2949    procedure Check_Constant_Address_Clause
2950      (Expr  : Node_Id;
2951       U_Ent : Entity_Id)
2952    is
2953       procedure Check_At_Constant_Address (Nod : Node_Id);
2954       --  Checks that the given node N represents a name whose 'Address is
2955       --  constant (in the same sense as OK_Constant_Address_Clause, i.e. the
2956       --  address value is the same at the point of declaration of U_Ent and at
2957       --  the time of elaboration of the address clause.
2958
2959       procedure Check_Expr_Constants (Nod : Node_Id);
2960       --  Checks that Nod meets the requirements for a constant address clause
2961       --  in the sense of the enclosing procedure.
2962
2963       procedure Check_List_Constants (Lst : List_Id);
2964       --  Check that all elements of list Lst meet the requirements for a
2965       --  constant address clause in the sense of the enclosing procedure.
2966
2967       -------------------------------
2968       -- Check_At_Constant_Address --
2969       -------------------------------
2970
2971       procedure Check_At_Constant_Address (Nod : Node_Id) is
2972       begin
2973          if Is_Entity_Name (Nod) then
2974             if Present (Address_Clause (Entity ((Nod)))) then
2975                Error_Msg_NE
2976                  ("invalid address clause for initialized object &!",
2977                            Nod, U_Ent);
2978                Error_Msg_NE
2979                  ("address for& cannot" &
2980                     " depend on another address clause! (RM 13.1(22))!",
2981                   Nod, U_Ent);
2982
2983             elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2984               and then Sloc (U_Ent) < Sloc (Entity (Nod))
2985             then
2986                Error_Msg_NE
2987                  ("invalid address clause for initialized object &!",
2988                   Nod, U_Ent);
2989                Error_Msg_Node_2 := U_Ent;
2990                Error_Msg_NE
2991                  ("\& must be defined before & (RM 13.1(22))!",
2992                   Nod, Entity (Nod));
2993             end if;
2994
2995          elsif Nkind (Nod) = N_Selected_Component then
2996             declare
2997                T : constant Entity_Id := Etype (Prefix (Nod));
2998
2999             begin
3000                if (Is_Record_Type (T)
3001                     and then Has_Discriminants (T))
3002                  or else
3003                   (Is_Access_Type (T)
3004                      and then Is_Record_Type (Designated_Type (T))
3005                      and then Has_Discriminants (Designated_Type (T)))
3006                then
3007                   Error_Msg_NE
3008                     ("invalid address clause for initialized object &!",
3009                      Nod, U_Ent);
3010                   Error_Msg_N
3011                     ("\address cannot depend on component" &
3012                      " of discriminated record (RM 13.1(22))!",
3013                      Nod);
3014                else
3015                   Check_At_Constant_Address (Prefix (Nod));
3016                end if;
3017             end;
3018
3019          elsif Nkind (Nod) = N_Indexed_Component then
3020             Check_At_Constant_Address (Prefix (Nod));
3021             Check_List_Constants (Expressions (Nod));
3022
3023          else
3024             Check_Expr_Constants (Nod);
3025          end if;
3026       end Check_At_Constant_Address;
3027
3028       --------------------------
3029       -- Check_Expr_Constants --
3030       --------------------------
3031
3032       procedure Check_Expr_Constants (Nod : Node_Id) is
3033          Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
3034          Ent       : Entity_Id           := Empty;
3035
3036       begin
3037          if Nkind (Nod) in N_Has_Etype
3038            and then Etype (Nod) = Any_Type
3039          then
3040             return;
3041          end if;
3042
3043          case Nkind (Nod) is
3044             when N_Empty | N_Error =>
3045                return;
3046
3047             when N_Identifier | N_Expanded_Name =>
3048                Ent := Entity (Nod);
3049
3050                --  We need to look at the original node if it is different
3051                --  from the node, since we may have rewritten things and
3052                --  substituted an identifier representing the rewrite.
3053
3054                if Original_Node (Nod) /= Nod then
3055                   Check_Expr_Constants (Original_Node (Nod));
3056
3057                   --  If the node is an object declaration without initial
3058                   --  value, some code has been expanded, and the expression
3059                   --  is not constant, even if the constituents might be
3060                   --  acceptable, as in A'Address + offset.
3061
3062                   if Ekind (Ent) = E_Variable
3063                     and then
3064                       Nkind (Declaration_Node (Ent)) = N_Object_Declaration
3065                     and then
3066                       No (Expression (Declaration_Node (Ent)))
3067                   then
3068                      Error_Msg_NE
3069                        ("invalid address clause for initialized object &!",
3070                         Nod, U_Ent);
3071
3072                   --  If entity is constant, it may be the result of expanding
3073                   --  a check. We must verify that its declaration appears
3074                   --  before the object in question, else we also reject the
3075                   --  address clause.
3076
3077                   elsif Ekind (Ent) = E_Constant
3078                     and then In_Same_Source_Unit (Ent, U_Ent)
3079                     and then Sloc (Ent) > Loc_U_Ent
3080                   then
3081                      Error_Msg_NE
3082                        ("invalid address clause for initialized object &!",
3083                         Nod, U_Ent);
3084                   end if;
3085
3086                   return;
3087                end if;
3088
3089                --  Otherwise look at the identifier and see if it is OK
3090
3091                if Ekind (Ent) = E_Named_Integer
3092                     or else
3093                   Ekind (Ent) = E_Named_Real
3094                     or else
3095                   Is_Type (Ent)
3096                then
3097                   return;
3098
3099                elsif
3100                   Ekind (Ent) = E_Constant
3101                     or else
3102                   Ekind (Ent) = E_In_Parameter
3103                then
3104                   --  This is the case where we must have Ent defined before
3105                   --  U_Ent. Clearly if they are in different units this
3106                   --  requirement is met since the unit containing Ent is
3107                   --  already processed.
3108
3109                   if not In_Same_Source_Unit (Ent, U_Ent) then
3110                      return;
3111
3112                   --  Otherwise location of Ent must be before the location
3113                   --  of U_Ent, that's what prior defined means.
3114
3115                   elsif Sloc (Ent) < Loc_U_Ent then
3116                      return;
3117
3118                   else
3119                      Error_Msg_NE
3120                        ("invalid address clause for initialized object &!",
3121                         Nod, U_Ent);
3122                      Error_Msg_Node_2 := U_Ent;
3123                      Error_Msg_NE
3124                        ("\& must be defined before & (RM 13.1(22))!",
3125                         Nod, Ent);
3126                   end if;
3127
3128                elsif Nkind (Original_Node (Nod)) = N_Function_Call then
3129                   Check_Expr_Constants (Original_Node (Nod));
3130
3131                else
3132                   Error_Msg_NE
3133                     ("invalid address clause for initialized object &!",
3134                      Nod, U_Ent);
3135
3136                   if Comes_From_Source (Ent) then
3137                      Error_Msg_NE
3138                        ("\reference to variable& not allowed"
3139                           & " (RM 13.1(22))!", Nod, Ent);
3140                   else
3141                      Error_Msg_N
3142                        ("non-static expression not allowed"
3143                           & " (RM 13.1(22))!", Nod);
3144                   end if;
3145                end if;
3146
3147             when N_Integer_Literal   =>
3148
3149                --  If this is a rewritten unchecked conversion, in a system
3150                --  where Address is an integer type, always use the base type
3151                --  for a literal value. This is user-friendly and prevents
3152                --  order-of-elaboration issues with instances of unchecked
3153                --  conversion.
3154
3155                if Nkind (Original_Node (Nod)) = N_Function_Call then
3156                   Set_Etype (Nod, Base_Type (Etype (Nod)));
3157                end if;
3158
3159             when N_Real_Literal      |
3160                  N_String_Literal    |
3161                  N_Character_Literal =>
3162                return;
3163
3164             when N_Range =>
3165                Check_Expr_Constants (Low_Bound (Nod));
3166                Check_Expr_Constants (High_Bound (Nod));
3167
3168             when N_Explicit_Dereference =>
3169                Check_Expr_Constants (Prefix (Nod));
3170
3171             when N_Indexed_Component =>
3172                Check_Expr_Constants (Prefix (Nod));
3173                Check_List_Constants (Expressions (Nod));
3174
3175             when N_Slice =>
3176                Check_Expr_Constants (Prefix (Nod));
3177                Check_Expr_Constants (Discrete_Range (Nod));
3178
3179             when N_Selected_Component =>
3180                Check_Expr_Constants (Prefix (Nod));
3181
3182             when N_Attribute_Reference =>
3183                if Attribute_Name (Nod) = Name_Address
3184                    or else
3185                   Attribute_Name (Nod) = Name_Access
3186                     or else
3187                   Attribute_Name (Nod) = Name_Unchecked_Access
3188                     or else
3189                   Attribute_Name (Nod) = Name_Unrestricted_Access
3190                then
3191                   Check_At_Constant_Address (Prefix (Nod));
3192
3193                else
3194                   Check_Expr_Constants (Prefix (Nod));
3195                   Check_List_Constants (Expressions (Nod));
3196                end if;
3197
3198             when N_Aggregate =>
3199                Check_List_Constants (Component_Associations (Nod));
3200                Check_List_Constants (Expressions (Nod));
3201
3202             when N_Component_Association =>
3203                Check_Expr_Constants (Expression (Nod));
3204
3205             when N_Extension_Aggregate =>
3206                Check_Expr_Constants (Ancestor_Part (Nod));
3207                Check_List_Constants (Component_Associations (Nod));
3208                Check_List_Constants (Expressions (Nod));
3209
3210             when N_Null =>
3211                return;
3212
3213             when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test =>
3214                Check_Expr_Constants (Left_Opnd (Nod));
3215                Check_Expr_Constants (Right_Opnd (Nod));
3216
3217             when N_Unary_Op =>
3218                Check_Expr_Constants (Right_Opnd (Nod));
3219
3220             when N_Type_Conversion           |
3221                  N_Qualified_Expression      |
3222                  N_Allocator                 =>
3223                Check_Expr_Constants (Expression (Nod));
3224
3225             when N_Unchecked_Type_Conversion =>
3226                Check_Expr_Constants (Expression (Nod));
3227
3228                --  If this is a rewritten unchecked conversion, subtypes in
3229                --  this node are those created within the instance. To avoid
3230                --  order of elaboration issues, replace them with their base
3231                --  types. Note that address clauses can cause order of
3232                --  elaboration problems because they are elaborated by the
3233                --  back-end at the point of definition, and may mention
3234                --  entities declared in between (as long as everything is
3235                --  static). It is user-friendly to allow unchecked conversions
3236                --  in this context.
3237
3238                if Nkind (Original_Node (Nod)) = N_Function_Call then
3239                   Set_Etype (Expression (Nod),
3240                     Base_Type (Etype (Expression (Nod))));
3241                   Set_Etype (Nod, Base_Type (Etype (Nod)));
3242                end if;
3243
3244             when N_Function_Call =>
3245                if not Is_Pure (Entity (Name (Nod))) then
3246                   Error_Msg_NE
3247                     ("invalid address clause for initialized object &!",
3248                      Nod, U_Ent);
3249
3250                   Error_Msg_NE
3251                     ("\function & is not pure (RM 13.1(22))!",
3252                      Nod, Entity (Name (Nod)));
3253
3254                else
3255                   Check_List_Constants (Parameter_Associations (Nod));
3256                end if;
3257
3258             when N_Parameter_Association =>
3259                Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3260
3261             when others =>
3262                Error_Msg_NE
3263                  ("invalid address clause for initialized object &!",
3264                   Nod, U_Ent);
3265                Error_Msg_NE
3266                  ("\must be constant defined before& (RM 13.1(22))!",
3267                   Nod, U_Ent);
3268          end case;
3269       end Check_Expr_Constants;
3270
3271       --------------------------
3272       -- Check_List_Constants --
3273       --------------------------
3274
3275       procedure Check_List_Constants (Lst : List_Id) is
3276          Nod1 : Node_Id;
3277
3278       begin
3279          if Present (Lst) then
3280             Nod1 := First (Lst);
3281             while Present (Nod1) loop
3282                Check_Expr_Constants (Nod1);
3283                Next (Nod1);
3284             end loop;
3285          end if;
3286       end Check_List_Constants;
3287
3288    --  Start of processing for Check_Constant_Address_Clause
3289
3290    begin
3291       Check_Expr_Constants (Expr);
3292    end Check_Constant_Address_Clause;
3293
3294    ----------------
3295    -- Check_Size --
3296    ----------------
3297
3298    procedure Check_Size
3299      (N      : Node_Id;
3300       T      : Entity_Id;
3301       Siz    : Uint;
3302       Biased : out Boolean)
3303    is
3304       UT : constant Entity_Id := Underlying_Type (T);
3305       M  : Uint;
3306
3307    begin
3308       Biased := False;
3309
3310       --  Dismiss cases for generic types or types with previous errors
3311
3312       if No (UT)
3313         or else UT = Any_Type
3314         or else Is_Generic_Type (UT)
3315         or else Is_Generic_Type (Root_Type (UT))
3316       then
3317          return;
3318
3319       --  Check case of bit packed array
3320
3321       elsif Is_Array_Type (UT)
3322         and then Known_Static_Component_Size (UT)
3323         and then Is_Bit_Packed_Array (UT)
3324       then
3325          declare
3326             Asiz : Uint;
3327             Indx : Node_Id;
3328             Ityp : Entity_Id;
3329
3330          begin
3331             Asiz := Component_Size (UT);
3332             Indx := First_Index (UT);
3333             loop
3334                Ityp := Etype (Indx);
3335
3336                --  If non-static bound, then we are not in the business of
3337                --  trying to check the length, and indeed an error will be
3338                --  issued elsewhere, since sizes of non-static array types
3339                --  cannot be set implicitly or explicitly.
3340
3341                if not Is_Static_Subtype (Ityp) then
3342                   return;
3343                end if;
3344
3345                --  Otherwise accumulate next dimension
3346
3347                Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
3348                                Expr_Value (Type_Low_Bound  (Ityp)) +
3349                                Uint_1);
3350
3351                Next_Index (Indx);
3352                exit when No (Indx);
3353             end loop;
3354
3355             if Asiz <= Siz then
3356                return;
3357             else
3358                Error_Msg_Uint_1 := Asiz;
3359                Error_Msg_NE
3360                  ("size for& too small, minimum allowed is ^", N, T);
3361                Set_Esize   (T, Asiz);
3362                Set_RM_Size (T, Asiz);
3363             end if;
3364          end;
3365
3366       --  All other composite types are ignored
3367
3368       elsif Is_Composite_Type (UT) then
3369          return;
3370
3371       --  For fixed-point types, don't check minimum if type is not frozen,
3372       --  since we don't know all the characteristics of the type that can
3373       --  affect the size (e.g. a specified small) till freeze time.
3374
3375       elsif Is_Fixed_Point_Type (UT)
3376         and then not Is_Frozen (UT)
3377       then
3378          null;
3379
3380       --  Cases for which a minimum check is required
3381
3382       else
3383          --  Ignore if specified size is correct for the type
3384
3385          if Known_Esize (UT) and then Siz = Esize (UT) then
3386             return;
3387          end if;
3388
3389          --  Otherwise get minimum size
3390
3391          M := UI_From_Int (Minimum_Size (UT));
3392
3393          if Siz < M then
3394
3395             --  Size is less than minimum size, but one possibility remains
3396             --  that we can manage with the new size if we bias the type.
3397
3398             M := UI_From_Int (Minimum_Size (UT, Biased => True));
3399
3400             if Siz < M then
3401                Error_Msg_Uint_1 := M;
3402                Error_Msg_NE
3403                  ("size for& too small, minimum allowed is ^", N, T);
3404                Set_Esize (T, M);
3405                Set_RM_Size (T, M);
3406             else
3407                Biased := True;
3408             end if;
3409          end if;
3410       end if;
3411    end Check_Size;
3412
3413    -------------------------
3414    -- Get_Alignment_Value --
3415    -------------------------
3416
3417    function Get_Alignment_Value (Expr : Node_Id) return Uint is
3418       Align : constant Uint := Static_Integer (Expr);
3419
3420    begin
3421       if Align = No_Uint then
3422          return No_Uint;
3423
3424       elsif Align <= 0 then
3425          Error_Msg_N ("alignment value must be positive", Expr);
3426          return No_Uint;
3427
3428       else
3429          for J in Int range 0 .. 64 loop
3430             declare
3431                M : constant Uint := Uint_2 ** J;
3432
3433             begin
3434                exit when M = Align;
3435
3436                if M > Align then
3437                   Error_Msg_N
3438                     ("alignment value must be power of 2", Expr);
3439                   return No_Uint;
3440                end if;
3441             end;
3442          end loop;
3443
3444          return Align;
3445       end if;
3446    end Get_Alignment_Value;
3447
3448    ----------------
3449    -- Initialize --
3450    ----------------
3451
3452    procedure Initialize is
3453    begin
3454       Unchecked_Conversions.Init;
3455    end Initialize;
3456
3457    -------------------------
3458    -- Is_Operational_Item --
3459    -------------------------
3460
3461    function Is_Operational_Item (N : Node_Id) return Boolean is
3462    begin
3463       if Nkind (N) /= N_Attribute_Definition_Clause then
3464          return False;
3465       else
3466          declare
3467             Id    : constant Attribute_Id := Get_Attribute_Id (Chars (N));
3468          begin
3469             return   Id = Attribute_Input
3470               or else Id = Attribute_Output
3471               or else Id = Attribute_Read
3472               or else Id = Attribute_Write
3473               or else Id = Attribute_External_Tag;
3474          end;
3475       end if;
3476    end Is_Operational_Item;
3477
3478    ------------------
3479    -- Minimum_Size --
3480    ------------------
3481
3482    function Minimum_Size
3483      (T      : Entity_Id;
3484       Biased : Boolean := False) return Nat
3485    is
3486       Lo     : Uint    := No_Uint;
3487       Hi     : Uint    := No_Uint;
3488       LoR    : Ureal   := No_Ureal;
3489       HiR    : Ureal   := No_Ureal;
3490       LoSet  : Boolean := False;
3491       HiSet  : Boolean := False;
3492       B      : Uint;
3493       S      : Nat;
3494       Ancest : Entity_Id;
3495       R_Typ  : constant Entity_Id := Root_Type (T);
3496
3497    begin
3498       --  If bad type, return 0
3499
3500       if T = Any_Type then
3501          return 0;
3502
3503       --  For generic types, just return zero. There cannot be any legitimate
3504       --  need to know such a size, but this routine may be called with a
3505       --  generic type as part of normal processing.
3506
3507       elsif Is_Generic_Type (R_Typ)
3508         or else R_Typ = Any_Type
3509       then
3510          return 0;
3511
3512          --  Access types. Normally an access type cannot have a size smaller
3513          --  than the size of System.Address. The exception is on VMS, where
3514          --  we have short and long addresses, and it is possible for an access
3515          --  type to have a short address size (and thus be less than the size
3516          --  of System.Address itself). We simply skip the check for VMS, and
3517          --  leave it to the back end to do the check.
3518
3519       elsif Is_Access_Type (T) then
3520          if OpenVMS_On_Target then
3521             return 0;
3522          else
3523             return System_Address_Size;
3524          end if;
3525
3526       --  Floating-point types
3527
3528       elsif Is_Floating_Point_Type (T) then
3529          return UI_To_Int (Esize (R_Typ));
3530
3531       --  Discrete types
3532
3533       elsif Is_Discrete_Type (T) then
3534
3535          --  The following loop is looking for the nearest compile time known
3536          --  bounds following the ancestor subtype chain. The idea is to find
3537          --  the most restrictive known bounds information.
3538
3539          Ancest := T;
3540          loop
3541             if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3542                return 0;
3543             end if;
3544
3545             if not LoSet then
3546                if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
3547                   Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
3548                   LoSet := True;
3549                   exit when HiSet;
3550                end if;
3551             end if;
3552
3553             if not HiSet then
3554                if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
3555                   Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
3556                   HiSet := True;
3557                   exit when LoSet;
3558                end if;
3559             end if;
3560
3561             Ancest := Ancestor_Subtype (Ancest);
3562
3563             if No (Ancest) then
3564                Ancest := Base_Type (T);
3565
3566                if Is_Generic_Type (Ancest) then
3567                   return 0;
3568                end if;
3569             end if;
3570          end loop;
3571
3572       --  Fixed-point types. We can't simply use Expr_Value to get the
3573       --  Corresponding_Integer_Value values of the bounds, since these do not
3574       --  get set till the type is frozen, and this routine can be called
3575       --  before the type is frozen. Similarly the test for bounds being static
3576       --  needs to include the case where we have unanalyzed real literals for
3577       --  the same reason.
3578
3579       elsif Is_Fixed_Point_Type (T) then
3580
3581          --  The following loop is looking for the nearest compile time known
3582          --  bounds following the ancestor subtype chain. The idea is to find
3583          --  the most restrictive known bounds information.
3584
3585          Ancest := T;
3586          loop
3587             if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3588                return 0;
3589             end if;
3590
3591             --  Note: In the following two tests for LoSet and HiSet, it may
3592             --  seem redundant to test for N_Real_Literal here since normally
3593             --  one would assume that the test for the value being known at
3594             --  compile time includes this case. However, there is a glitch.
3595             --  If the real literal comes from folding a non-static expression,
3596             --  then we don't consider any non- static expression to be known
3597             --  at compile time if we are in configurable run time mode (needed
3598             --  in some cases to give a clearer definition of what is and what
3599             --  is not accepted). So the test is indeed needed. Without it, we
3600             --  would set neither Lo_Set nor Hi_Set and get an infinite loop.
3601
3602             if not LoSet then
3603                if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
3604                  or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
3605                then
3606                   LoR := Expr_Value_R (Type_Low_Bound (Ancest));
3607                   LoSet := True;
3608                   exit when HiSet;
3609                end if;
3610             end if;
3611
3612             if not HiSet then
3613                if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
3614                  or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
3615                then
3616                   HiR := Expr_Value_R (Type_High_Bound (Ancest));
3617                   HiSet := True;
3618                   exit when LoSet;
3619                end if;
3620             end if;
3621
3622             Ancest := Ancestor_Subtype (Ancest);
3623
3624             if No (Ancest) then
3625                Ancest := Base_Type (T);
3626
3627                if Is_Generic_Type (Ancest) then
3628                   return 0;
3629                end if;
3630             end if;
3631          end loop;
3632
3633          Lo := UR_To_Uint (LoR / Small_Value (T));
3634          Hi := UR_To_Uint (HiR / Small_Value (T));
3635
3636       --  No other types allowed
3637
3638       else
3639          raise Program_Error;
3640       end if;
3641
3642       --  Fall through with Hi and Lo set. Deal with biased case
3643
3644       if (Biased
3645            and then not Is_Fixed_Point_Type (T)
3646            and then not (Is_Enumeration_Type (T)
3647                           and then Has_Non_Standard_Rep (T)))
3648         or else Has_Biased_Representation (T)
3649       then
3650          Hi := Hi - Lo;
3651          Lo := Uint_0;
3652       end if;
3653
3654       --  Signed case. Note that we consider types like range 1 .. -1 to be
3655       --  signed for the purpose of computing the size, since the bounds have
3656       --  to be accommodated in the base type.
3657
3658       if Lo < 0 or else Hi < 0 then
3659          S := 1;
3660          B := Uint_1;
3661
3662          --  S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3663          --  Note that we accommodate the case where the bounds cross. This
3664          --  can happen either because of the way the bounds are declared
3665          --  or because of the algorithm in Freeze_Fixed_Point_Type.
3666
3667          while Lo < -B
3668            or else Hi < -B
3669            or else Lo >= B
3670            or else Hi >= B
3671          loop
3672             B := Uint_2 ** S;
3673             S := S + 1;
3674          end loop;
3675
3676       --  Unsigned case
3677
3678       else
3679          --  If both bounds are positive, make sure that both are represen-
3680          --  table in the case where the bounds are crossed. This can happen
3681          --  either because of the way the bounds are declared, or because of
3682          --  the algorithm in Freeze_Fixed_Point_Type.
3683
3684          if Lo > Hi then
3685             Hi := Lo;
3686          end if;
3687
3688          --  S = size, (can accommodate 0 .. (2**size - 1))
3689
3690          S := 0;
3691          while Hi >= Uint_2 ** S loop
3692             S := S + 1;
3693          end loop;
3694       end if;
3695
3696       return S;
3697    end Minimum_Size;
3698
3699    ---------------------------
3700    -- New_Stream_Subprogram --
3701    ---------------------------
3702
3703    procedure New_Stream_Subprogram
3704      (N     : Node_Id;
3705       Ent   : Entity_Id;
3706       Subp  : Entity_Id;
3707       Nam   : TSS_Name_Type)
3708    is
3709       Loc       : constant Source_Ptr := Sloc (N);
3710       Sname     : constant Name_Id    := Make_TSS_Name (Base_Type (Ent), Nam);
3711       Subp_Id   : Entity_Id;
3712       Subp_Decl : Node_Id;
3713       F         : Entity_Id;
3714       Etyp      : Entity_Id;
3715
3716       Defer_Declaration : constant Boolean :=
3717                             Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
3718       --  For a tagged type, there is a declaration for each stream attribute
3719       --  at the freeze point, and we must generate only a completion of this
3720       --  declaration. We do the same for private types, because the full view
3721       --  might be tagged. Otherwise we generate a declaration at the point of
3722       --  the attribute definition clause.
3723
3724       function Build_Spec return Node_Id;
3725       --  Used for declaration and renaming declaration, so that this is
3726       --  treated as a renaming_as_body.
3727
3728       ----------------
3729       -- Build_Spec --
3730       ----------------
3731
3732       function Build_Spec return Node_Id is
3733          Out_P   : constant Boolean := (Nam = TSS_Stream_Read);
3734          Formals : List_Id;
3735          Spec    : Node_Id;
3736          T_Ref   : constant Node_Id := New_Reference_To (Etyp, Loc);
3737
3738       begin
3739          Subp_Id := Make_Defining_Identifier (Loc, Sname);
3740
3741          --  S : access Root_Stream_Type'Class
3742
3743          Formals := New_List (
3744                       Make_Parameter_Specification (Loc,
3745                         Defining_Identifier =>
3746                           Make_Defining_Identifier (Loc, Name_S),
3747                         Parameter_Type =>
3748                           Make_Access_Definition (Loc,
3749                             Subtype_Mark =>
3750                               New_Reference_To (
3751                                 Designated_Type (Etype (F)), Loc))));
3752
3753          if Nam = TSS_Stream_Input then
3754             Spec := Make_Function_Specification (Loc,
3755                       Defining_Unit_Name       => Subp_Id,
3756                       Parameter_Specifications => Formals,
3757                       Result_Definition        => T_Ref);
3758          else
3759             --  V : [out] T
3760
3761             Append_To (Formals,
3762               Make_Parameter_Specification (Loc,
3763                 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
3764                 Out_Present         => Out_P,
3765                 Parameter_Type      => T_Ref));
3766
3767             Spec := Make_Procedure_Specification (Loc,
3768                       Defining_Unit_Name       => Subp_Id,
3769                       Parameter_Specifications => Formals);
3770          end if;
3771
3772          return Spec;
3773       end Build_Spec;
3774
3775    --  Start of processing for New_Stream_Subprogram
3776
3777    begin
3778       F := First_Formal (Subp);
3779
3780       if Ekind (Subp) = E_Procedure then
3781          Etyp := Etype (Next_Formal (F));
3782       else
3783          Etyp := Etype (Subp);
3784       end if;
3785
3786       --  Prepare subprogram declaration and insert it as an action on the
3787       --  clause node. The visibility for this entity is used to test for
3788       --  visibility of the attribute definition clause (in the sense of
3789       --  8.3(23) as amended by AI-195).
3790
3791       if not Defer_Declaration then
3792          Subp_Decl :=
3793            Make_Subprogram_Declaration (Loc,
3794              Specification => Build_Spec);
3795
3796       --  For a tagged type, there is always a visible declaration for each
3797       --  stream TSS (it is a predefined primitive operation), and the
3798       --  completion of this declaration occurs at the freeze point, which is
3799       --  not always visible at places where the attribute definition clause is
3800       --  visible. So, we create a dummy entity here for the purpose of
3801       --  tracking the visibility of the attribute definition clause itself.
3802
3803       else
3804          Subp_Id :=
3805            Make_Defining_Identifier (Loc,
3806              Chars => New_External_Name (Sname, 'V'));
3807          Subp_Decl :=
3808            Make_Object_Declaration (Loc,
3809              Defining_Identifier => Subp_Id,
3810              Object_Definition   => New_Occurrence_Of (Standard_Boolean, Loc));
3811       end if;
3812
3813       Insert_Action (N, Subp_Decl);
3814       Set_Entity (N, Subp_Id);
3815
3816       Subp_Decl :=
3817         Make_Subprogram_Renaming_Declaration (Loc,
3818           Specification => Build_Spec,
3819           Name => New_Reference_To (Subp, Loc));
3820
3821       if Defer_Declaration then
3822          Set_TSS (Base_Type (Ent), Subp_Id);
3823       else
3824          Insert_Action (N, Subp_Decl);
3825          Copy_TSS (Subp_Id, Base_Type (Ent));
3826       end if;
3827    end New_Stream_Subprogram;
3828
3829    ------------------------
3830    -- Rep_Item_Too_Early --
3831    ------------------------
3832
3833    function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
3834    begin
3835       --  Cannot apply non-operational rep items to generic types
3836
3837       if Is_Operational_Item (N) then
3838          return False;
3839
3840       elsif Is_Type (T)
3841         and then Is_Generic_Type (Root_Type (T))
3842       then
3843          Error_Msg_N
3844            ("representation item not allowed for generic type", N);
3845          return True;
3846       end if;
3847
3848       --  Otherwise check for incomplete type
3849
3850       if Is_Incomplete_Or_Private_Type (T)
3851         and then No (Underlying_Type (T))
3852       then
3853          Error_Msg_N
3854            ("representation item must be after full type declaration", N);
3855          return True;
3856
3857       --  If the type has incomplete components, a representation clause is
3858       --  illegal but stream attributes and Convention pragmas are correct.
3859
3860       elsif Has_Private_Component (T) then
3861          if Nkind (N) = N_Pragma then
3862             return False;
3863          else
3864             Error_Msg_N
3865               ("representation item must appear after type is fully defined",
3866                 N);
3867             return True;
3868          end if;
3869       else
3870          return False;
3871       end if;
3872    end Rep_Item_Too_Early;
3873
3874    -----------------------
3875    -- Rep_Item_Too_Late --
3876    -----------------------
3877
3878    function Rep_Item_Too_Late
3879      (T     : Entity_Id;
3880       N     : Node_Id;
3881       FOnly : Boolean := False) return Boolean
3882    is
3883       S           : Entity_Id;
3884       Parent_Type : Entity_Id;
3885
3886       procedure Too_Late;
3887       --  Output the too late message. Note that this is not considered a
3888       --  serious error, since the effect is simply that we ignore the
3889       --  representation clause in this case.
3890
3891       --------------
3892       -- Too_Late --
3893       --------------
3894
3895       procedure Too_Late is
3896       begin
3897          Error_Msg_N ("|representation item appears too late!", N);
3898       end Too_Late;
3899
3900    --  Start of processing for Rep_Item_Too_Late
3901
3902    begin
3903       --  First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3904       --  types, which may be frozen if they appear in a representation clause
3905       --  for a local type.
3906
3907       if Is_Frozen (T)
3908         and then not From_With_Type (T)
3909       then
3910          Too_Late;
3911          S := First_Subtype (T);
3912
3913          if Present (Freeze_Node (S)) then
3914             Error_Msg_NE
3915               ("?no more representation items for }", Freeze_Node (S), S);
3916          end if;
3917
3918          return True;
3919
3920       --  Check for case of non-tagged derived type whose parent either has
3921       --  primitive operations, or is a by reference type (RM 13.1(10)).
3922
3923       elsif Is_Type (T)
3924         and then not FOnly
3925         and then Is_Derived_Type (T)
3926         and then not Is_Tagged_Type (T)
3927       then
3928          Parent_Type := Etype (Base_Type (T));
3929
3930          if Has_Primitive_Operations (Parent_Type) then
3931             Too_Late;
3932             Error_Msg_NE
3933               ("primitive operations already defined for&!", N, Parent_Type);
3934             return True;
3935
3936          elsif Is_By_Reference_Type (Parent_Type) then
3937             Too_Late;
3938             Error_Msg_NE
3939               ("parent type & is a by reference type!", N, Parent_Type);
3940             return True;
3941          end if;
3942       end if;
3943
3944       --  No error, link item into head of chain of rep items for the entity,
3945       --  but avoid chaining if we have an overloadable entity, and the pragma
3946       --  is one that can apply to multiple overloaded entities.
3947
3948       if Is_Overloadable (T)
3949         and then Nkind (N) = N_Pragma
3950       then
3951          declare
3952             Pname : constant Name_Id := Pragma_Name (N);
3953          begin
3954             if Pname = Name_Convention or else
3955                Pname = Name_Import     or else
3956                Pname = Name_Export     or else
3957                Pname = Name_External   or else
3958                Pname = Name_Interface
3959             then
3960                return False;
3961             end if;
3962          end;
3963       end if;
3964
3965       Record_Rep_Item (T, N);
3966       return False;
3967    end Rep_Item_Too_Late;
3968
3969    -------------------------
3970    -- Same_Representation --
3971    -------------------------
3972
3973    function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3974       T1 : constant Entity_Id := Underlying_Type (Typ1);
3975       T2 : constant Entity_Id := Underlying_Type (Typ2);
3976
3977    begin
3978       --  A quick check, if base types are the same, then we definitely have
3979       --  the same representation, because the subtype specific representation
3980       --  attributes (Size and Alignment) do not affect representation from
3981       --  the point of view of this test.
3982
3983       if Base_Type (T1) = Base_Type (T2) then
3984          return True;
3985
3986       elsif Is_Private_Type (Base_Type (T2))
3987         and then Base_Type (T1) = Full_View (Base_Type (T2))
3988       then
3989          return True;
3990       end if;
3991
3992       --  Tagged types never have differing representations
3993
3994       if Is_Tagged_Type (T1) then
3995          return True;
3996       end if;
3997
3998       --  Representations are definitely different if conventions differ
3999
4000       if Convention (T1) /= Convention (T2) then
4001          return False;
4002       end if;
4003
4004       --  Representations are different if component alignments differ
4005
4006       if (Is_Record_Type (T1) or else Is_Array_Type (T1))
4007         and then
4008          (Is_Record_Type (T2) or else Is_Array_Type (T2))
4009         and then Component_Alignment (T1) /= Component_Alignment (T2)
4010       then
4011          return False;
4012       end if;
4013
4014       --  For arrays, the only real issue is component size. If we know the
4015       --  component size for both arrays, and it is the same, then that's
4016       --  good enough to know we don't have a change of representation.
4017
4018       if Is_Array_Type (T1) then
4019          if Known_Component_Size (T1)
4020            and then Known_Component_Size (T2)
4021            and then Component_Size (T1) = Component_Size (T2)
4022          then
4023             return True;
4024          end if;
4025       end if;
4026
4027       --  Types definitely have same representation if neither has non-standard
4028       --  representation since default representations are always consistent.
4029       --  If only one has non-standard representation, and the other does not,
4030       --  then we consider that they do not have the same representation. They
4031       --  might, but there is no way of telling early enough.
4032
4033       if Has_Non_Standard_Rep (T1) then
4034          if not Has_Non_Standard_Rep (T2) then
4035             return False;
4036          end if;
4037       else
4038          return not Has_Non_Standard_Rep (T2);
4039       end if;
4040
4041       --  Here the two types both have non-standard representation, and we need
4042       --  to determine if they have the same non-standard representation.
4043
4044       --  For arrays, we simply need to test if the component sizes are the
4045       --  same. Pragma Pack is reflected in modified component sizes, so this
4046       --  check also deals with pragma Pack.
4047
4048       if Is_Array_Type (T1) then
4049          return Component_Size (T1) = Component_Size (T2);
4050
4051       --  Tagged types always have the same representation, because it is not
4052       --  possible to specify different representations for common fields.
4053
4054       elsif Is_Tagged_Type (T1) then
4055          return True;
4056
4057       --  Case of record types
4058
4059       elsif Is_Record_Type (T1) then
4060
4061          --  Packed status must conform
4062
4063          if Is_Packed (T1) /= Is_Packed (T2) then
4064             return False;
4065
4066          --  Otherwise we must check components. Typ2 maybe a constrained
4067          --  subtype with fewer components, so we compare the components
4068          --  of the base types.
4069
4070          else
4071             Record_Case : declare
4072                CD1, CD2 : Entity_Id;
4073
4074                function Same_Rep return Boolean;
4075                --  CD1 and CD2 are either components or discriminants. This
4076                --  function tests whether the two have the same representation
4077
4078                --------------
4079                -- Same_Rep --
4080                --------------
4081
4082                function Same_Rep return Boolean is
4083                begin
4084                   if No (Component_Clause (CD1)) then
4085                      return No (Component_Clause (CD2));
4086
4087                   else
4088                      return
4089                         Present (Component_Clause (CD2))
4090                           and then
4091                         Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
4092                           and then
4093                         Esize (CD1) = Esize (CD2);
4094                   end if;
4095                end Same_Rep;
4096
4097             --  Start of processing for Record_Case
4098
4099             begin
4100                if Has_Discriminants (T1) then
4101                   CD1 := First_Discriminant (T1);
4102                   CD2 := First_Discriminant (T2);
4103
4104                   --  The number of discriminants may be different if the
4105                   --  derived type has fewer (constrained by values). The
4106                   --  invisible discriminants retain the representation of
4107                   --  the original, so the discrepancy does not per se
4108                   --  indicate a different representation.
4109
4110                   while Present (CD1)
4111                     and then Present (CD2)
4112                   loop
4113                      if not Same_Rep then
4114                         return False;
4115                      else
4116                         Next_Discriminant (CD1);
4117                         Next_Discriminant (CD2);
4118                      end if;
4119                   end loop;
4120                end if;
4121
4122                CD1 := First_Component (Underlying_Type (Base_Type (T1)));
4123                CD2 := First_Component (Underlying_Type (Base_Type (T2)));
4124
4125                while Present (CD1) loop
4126                   if not Same_Rep then
4127                      return False;
4128                   else
4129                      Next_Component (CD1);
4130                      Next_Component (CD2);
4131                   end if;
4132                end loop;
4133
4134                return True;
4135             end Record_Case;
4136          end if;
4137
4138       --  For enumeration types, we must check each literal to see if the
4139       --  representation is the same. Note that we do not permit enumeration
4140       --  representation clauses for Character and Wide_Character, so these
4141       --  cases were already dealt with.
4142
4143       elsif Is_Enumeration_Type (T1) then
4144
4145          Enumeration_Case : declare
4146             L1, L2 : Entity_Id;
4147
4148          begin
4149             L1 := First_Literal (T1);
4150             L2 := First_Literal (T2);
4151
4152             while Present (L1) loop
4153                if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
4154                   return False;
4155                else
4156                   Next_Literal (L1);
4157                   Next_Literal (L2);
4158                end if;
4159             end loop;
4160
4161             return True;
4162
4163          end Enumeration_Case;
4164
4165       --  Any other types have the same representation for these purposes
4166
4167       else
4168          return True;
4169       end if;
4170    end Same_Representation;
4171
4172    --------------------
4173    -- Set_Enum_Esize --
4174    --------------------
4175
4176    procedure Set_Enum_Esize (T : Entity_Id) is
4177       Lo : Uint;
4178       Hi : Uint;
4179       Sz : Nat;
4180
4181    begin
4182       Init_Alignment (T);
4183
4184       --  Find the minimum standard size (8,16,32,64) that fits
4185
4186       Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
4187       Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
4188
4189       if Lo < 0 then
4190          if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
4191             Sz := Standard_Character_Size;  -- May be > 8 on some targets
4192
4193          elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
4194             Sz := 16;
4195
4196          elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
4197             Sz := 32;
4198
4199          else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
4200             Sz := 64;
4201          end if;
4202
4203       else
4204          if Hi < Uint_2**08 then
4205             Sz := Standard_Character_Size;  -- May be > 8 on some targets
4206
4207          elsif Hi < Uint_2**16 then
4208             Sz := 16;
4209
4210          elsif Hi < Uint_2**32 then
4211             Sz := 32;
4212
4213          else pragma Assert (Hi < Uint_2**63);
4214             Sz := 64;
4215          end if;
4216       end if;
4217
4218       --  That minimum is the proper size unless we have a foreign convention
4219       --  and the size required is 32 or less, in which case we bump the size
4220       --  up to 32. This is required for C and C++ and seems reasonable for
4221       --  all other foreign conventions.
4222
4223       if Has_Foreign_Convention (T)
4224         and then Esize (T) < Standard_Integer_Size
4225       then
4226          Init_Esize (T, Standard_Integer_Size);
4227       else
4228          Init_Esize (T, Sz);
4229       end if;
4230    end Set_Enum_Esize;
4231
4232    ------------------------------
4233    -- Validate_Address_Clauses --
4234    ------------------------------
4235
4236    procedure Validate_Address_Clauses is
4237    begin
4238       for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
4239          declare
4240             ACCR : Address_Clause_Check_Record
4241                      renames Address_Clause_Checks.Table (J);
4242
4243             X_Alignment : Uint;
4244             Y_Alignment : Uint;
4245
4246             X_Size : Uint;
4247             Y_Size : Uint;
4248
4249          begin
4250             --  Skip processing of this entry if warning already posted
4251
4252             if not Address_Warning_Posted (ACCR.N) then
4253
4254                --  Get alignments. Really we should always have the alignment
4255                --  of the objects properly back annotated, but right now the
4256                --  back end fails to back annotate for address clauses???
4257
4258                if Known_Alignment (ACCR.X) then
4259                   X_Alignment := Alignment (ACCR.X);
4260                else
4261                   X_Alignment := Alignment (Etype (ACCR.X));
4262                end if;
4263
4264                if Known_Alignment (ACCR.Y) then
4265                   Y_Alignment := Alignment (ACCR.Y);
4266                else
4267                   Y_Alignment := Alignment (Etype (ACCR.Y));
4268                end if;
4269
4270                --  Similarly obtain sizes
4271
4272                if Known_Esize (ACCR.X) then
4273                   X_Size := Esize (ACCR.X);
4274                else
4275                   X_Size := Esize (Etype (ACCR.X));
4276                end if;
4277
4278                if Known_Esize (ACCR.Y) then
4279                   Y_Size := Esize (ACCR.Y);
4280                else
4281                   Y_Size := Esize (Etype (ACCR.Y));
4282                end if;
4283
4284                --  Check for large object overlaying smaller one
4285
4286                if Y_Size > Uint_0
4287                  and then X_Size > Uint_0
4288                  and then X_Size > Y_Size
4289                then
4290                   Error_Msg_N
4291                     ("?size for overlaid object is too small", ACCR.N);
4292                   Error_Msg_Uint_1 := X_Size;
4293                   Error_Msg_NE
4294                     ("\?size of & is ^", ACCR.N, ACCR.X);
4295                   Error_Msg_Uint_1 := Y_Size;
4296                   Error_Msg_NE
4297                     ("\?size of & is ^", ACCR.N, ACCR.Y);
4298
4299                   --  Check for inadequate alignment. Again the defensive check
4300                   --  on Y_Alignment should not be needed, but because of the
4301                   --  failure in back end annotation, we can have an alignment
4302                   --  of 0 here???
4303
4304                   --  Note: we do not check alignments if we gave a size
4305                   --  warning, since it would likely be redundant.
4306
4307                elsif Y_Alignment /= Uint_0
4308                  and then Y_Alignment < X_Alignment
4309                then
4310                   Error_Msg_NE
4311                     ("?specified address for& may be inconsistent "
4312                        & "with alignment",
4313                      ACCR.N, ACCR.X);
4314                   Error_Msg_N
4315                     ("\?program execution may be erroneous (RM 13.3(27))",
4316                      ACCR.N);
4317                   Error_Msg_Uint_1 := X_Alignment;
4318                   Error_Msg_NE
4319                     ("\?alignment of & is ^",
4320                      ACCR.N, ACCR.X);
4321                   Error_Msg_Uint_1 := Y_Alignment;
4322                   Error_Msg_NE
4323                     ("\?alignment of & is ^",
4324                      ACCR.N, ACCR.Y);
4325                end if;
4326             end if;
4327          end;
4328       end loop;
4329    end Validate_Address_Clauses;
4330
4331    -----------------------------------
4332    -- Validate_Unchecked_Conversion --
4333    -----------------------------------
4334
4335    procedure Validate_Unchecked_Conversion
4336      (N        : Node_Id;
4337       Act_Unit : Entity_Id)
4338    is
4339       Source : Entity_Id;
4340       Target : Entity_Id;
4341       Vnode  : Node_Id;
4342
4343    begin
4344       --  Obtain source and target types. Note that we call Ancestor_Subtype
4345       --  here because the processing for generic instantiation always makes
4346       --  subtypes, and we want the original frozen actual types.
4347
4348       --  If we are dealing with private types, then do the check on their
4349       --  fully declared counterparts if the full declarations have been
4350       --  encountered (they don't have to be visible, but they must exist!)
4351
4352       Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
4353
4354       if Is_Private_Type (Source)
4355         and then Present (Underlying_Type (Source))
4356       then
4357          Source := Underlying_Type (Source);
4358       end if;
4359
4360       Target := Ancestor_Subtype (Etype (Act_Unit));
4361
4362       --  If either type is generic, the instantiation happens within a generic
4363       --  unit, and there is nothing to check. The proper check
4364       --  will happen when the enclosing generic is instantiated.
4365
4366       if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
4367          return;
4368       end if;
4369
4370       if Is_Private_Type (Target)
4371         and then Present (Underlying_Type (Target))
4372       then
4373          Target := Underlying_Type (Target);
4374       end if;
4375
4376       --  Source may be unconstrained array, but not target
4377
4378       if Is_Array_Type (Target)
4379         and then not Is_Constrained (Target)
4380       then
4381          Error_Msg_N
4382            ("unchecked conversion to unconstrained array not allowed", N);
4383          return;
4384       end if;
4385
4386       --  Warn if conversion between two different convention pointers
4387
4388       if Is_Access_Type (Target)
4389         and then Is_Access_Type (Source)
4390         and then Convention (Target) /= Convention (Source)
4391         and then Warn_On_Unchecked_Conversion
4392       then
4393          --  Give warnings for subprogram pointers only on most targets. The
4394          --  exception is VMS, where data pointers can have different lengths
4395          --  depending on the pointer convention.
4396
4397          if Is_Access_Subprogram_Type (Target)
4398            or else Is_Access_Subprogram_Type (Source)
4399            or else OpenVMS_On_Target
4400          then
4401             Error_Msg_N
4402               ("?conversion between pointers with different conventions!", N);
4403          end if;
4404       end if;
4405
4406       --  Warn if one of the operands is Ada.Calendar.Time. Do not emit a
4407       --  warning when compiling GNAT-related sources.
4408
4409       if Warn_On_Unchecked_Conversion
4410         and then not In_Predefined_Unit (N)
4411         and then RTU_Loaded (Ada_Calendar)
4412         and then
4413           (Chars (Source) = Name_Time
4414              or else
4415            Chars (Target) = Name_Time)
4416       then
4417          --  If Ada.Calendar is loaded and the name of one of the operands is
4418          --  Time, there is a good chance that this is Ada.Calendar.Time.
4419
4420          declare
4421             Calendar_Time : constant Entity_Id :=
4422                               Full_View (RTE (RO_CA_Time));
4423          begin
4424             pragma Assert (Present (Calendar_Time));
4425
4426             if Source = Calendar_Time
4427               or else Target = Calendar_Time
4428             then
4429                Error_Msg_N
4430                  ("?representation of 'Time values may change between " &
4431                   "'G'N'A'T versions", N);
4432             end if;
4433          end;
4434       end if;
4435
4436       --  Make entry in unchecked conversion table for later processing by
4437       --  Validate_Unchecked_Conversions, which will check sizes and alignments
4438       --  (using values set by the back-end where possible). This is only done
4439       --  if the appropriate warning is active.
4440
4441       if Warn_On_Unchecked_Conversion then
4442          Unchecked_Conversions.Append
4443            (New_Val => UC_Entry'
4444               (Eloc   => Sloc (N),
4445                Source => Source,
4446                Target => Target));
4447
4448          --  If both sizes are known statically now, then back end annotation
4449          --  is not required to do a proper check but if either size is not
4450          --  known statically, then we need the annotation.
4451
4452          if Known_Static_RM_Size (Source)
4453            and then Known_Static_RM_Size (Target)
4454          then
4455             null;
4456          else
4457             Back_Annotate_Rep_Info := True;
4458          end if;
4459       end if;
4460
4461       --  If unchecked conversion to access type, and access type is declared
4462       --  in the same unit as the unchecked conversion, then set the
4463       --  No_Strict_Aliasing flag (no strict aliasing is implicit in this
4464       --  situation).
4465
4466       if Is_Access_Type (Target) and then
4467         In_Same_Source_Unit (Target, N)
4468       then
4469          Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
4470       end if;
4471
4472       --  Generate N_Validate_Unchecked_Conversion node for back end in
4473       --  case the back end needs to perform special validation checks.
4474
4475       --  Shouldn't this be in Exp_Ch13, since the check only gets done
4476       --  if we have full expansion and the back end is called ???
4477
4478       Vnode :=
4479         Make_Validate_Unchecked_Conversion (Sloc (N));
4480       Set_Source_Type (Vnode, Source);
4481       Set_Target_Type (Vnode, Target);
4482
4483       --  If the unchecked conversion node is in a list, just insert before it.
4484       --  If not we have some strange case, not worth bothering about.
4485
4486       if Is_List_Member (N) then
4487          Insert_After (N, Vnode);
4488       end if;
4489    end Validate_Unchecked_Conversion;
4490
4491    ------------------------------------
4492    -- Validate_Unchecked_Conversions --
4493    ------------------------------------
4494
4495    procedure Validate_Unchecked_Conversions is
4496    begin
4497       for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
4498          declare
4499             T : UC_Entry renames Unchecked_Conversions.Table (N);
4500
4501             Eloc   : constant Source_Ptr := T.Eloc;
4502             Source : constant Entity_Id  := T.Source;
4503             Target : constant Entity_Id  := T.Target;
4504
4505             Source_Siz    : Uint;
4506             Target_Siz    : Uint;
4507
4508          begin
4509             --  This validation check, which warns if we have unequal sizes for
4510             --  unchecked conversion, and thus potentially implementation
4511             --  dependent semantics, is one of the few occasions on which we
4512             --  use the official RM size instead of Esize. See description in
4513             --  Einfo "Handling of Type'Size Values" for details.
4514
4515             if Serious_Errors_Detected = 0
4516               and then Known_Static_RM_Size (Source)
4517               and then Known_Static_RM_Size (Target)
4518
4519               --  Don't do the check if warnings off for either type, note the
4520               --  deliberate use of OR here instead of OR ELSE to get the flag
4521               --  Warnings_Off_Used set for both types if appropriate.
4522
4523               and then not (Has_Warnings_Off (Source)
4524                               or
4525                             Has_Warnings_Off (Target))
4526             then
4527                Source_Siz := RM_Size (Source);
4528                Target_Siz := RM_Size (Target);
4529
4530                if Source_Siz /= Target_Siz then
4531                   Error_Msg
4532                     ("?types for unchecked conversion have different sizes!",
4533                      Eloc);
4534
4535                   if All_Errors_Mode then
4536                      Error_Msg_Name_1 := Chars (Source);
4537                      Error_Msg_Uint_1 := Source_Siz;
4538                      Error_Msg_Name_2 := Chars (Target);
4539                      Error_Msg_Uint_2 := Target_Siz;
4540                      Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
4541
4542                      Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
4543
4544                      if Is_Discrete_Type (Source)
4545                        and then Is_Discrete_Type (Target)
4546                      then
4547                         if Source_Siz > Target_Siz then
4548                            Error_Msg
4549                              ("\?^ high order bits of source will be ignored!",
4550                               Eloc);
4551
4552                         elsif Is_Unsigned_Type (Source) then
4553                            Error_Msg
4554                              ("\?source will be extended with ^ high order " &
4555                               "zero bits?!", Eloc);
4556
4557                         else
4558                            Error_Msg
4559                              ("\?source will be extended with ^ high order " &
4560                               "sign bits!",
4561                               Eloc);
4562                         end if;
4563
4564                      elsif Source_Siz < Target_Siz then
4565                         if Is_Discrete_Type (Target) then
4566                            if Bytes_Big_Endian then
4567                               Error_Msg
4568                                 ("\?target value will include ^ undefined " &
4569                                  "low order bits!",
4570                                  Eloc);
4571                            else
4572                               Error_Msg
4573                                 ("\?target value will include ^ undefined " &
4574                                  "high order bits!",
4575                                  Eloc);
4576                            end if;
4577
4578                         else
4579                            Error_Msg
4580                              ("\?^ trailing bits of target value will be " &
4581                               "undefined!", Eloc);
4582                         end if;
4583
4584                      else pragma Assert (Source_Siz > Target_Siz);
4585                         Error_Msg
4586                           ("\?^ trailing bits of source will be ignored!",
4587                            Eloc);
4588                      end if;
4589                   end if;
4590                end if;
4591             end if;
4592
4593             --  If both types are access types, we need to check the alignment.
4594             --  If the alignment of both is specified, we can do it here.
4595
4596             if Serious_Errors_Detected = 0
4597               and then Ekind (Source) in Access_Kind
4598               and then Ekind (Target) in Access_Kind
4599               and then Target_Strict_Alignment
4600               and then Present (Designated_Type (Source))
4601               and then Present (Designated_Type (Target))
4602             then
4603                declare
4604                   D_Source : constant Entity_Id := Designated_Type (Source);
4605                   D_Target : constant Entity_Id := Designated_Type (Target);
4606
4607                begin
4608                   if Known_Alignment (D_Source)
4609                     and then Known_Alignment (D_Target)
4610                   then
4611                      declare
4612                         Source_Align : constant Uint := Alignment (D_Source);
4613                         Target_Align : constant Uint := Alignment (D_Target);
4614
4615                      begin
4616                         if Source_Align < Target_Align
4617                           and then not Is_Tagged_Type (D_Source)
4618
4619                           --  Suppress warning if warnings suppressed on either
4620                           --  type or either designated type. Note the use of
4621                           --  OR here instead of OR ELSE. That is intentional,
4622                           --  we would like to set flag Warnings_Off_Used in
4623                           --  all types for which warnings are suppressed.
4624
4625                           and then not (Has_Warnings_Off (D_Source)
4626                                           or
4627                                         Has_Warnings_Off (D_Target)
4628                                           or
4629                                         Has_Warnings_Off (Source)
4630                                           or
4631                                         Has_Warnings_Off (Target))
4632                         then
4633                            Error_Msg_Uint_1 := Target_Align;
4634                            Error_Msg_Uint_2 := Source_Align;
4635                            Error_Msg_Node_1 := D_Target;
4636                            Error_Msg_Node_2 := D_Source;
4637                            Error_Msg
4638                              ("?alignment of & (^) is stricter than " &
4639                               "alignment of & (^)!", Eloc);
4640                            Error_Msg
4641                              ("\?resulting access value may have invalid " &
4642                               "alignment!", Eloc);
4643                         end if;
4644                      end;
4645                   end if;
4646                end;
4647             end if;
4648          end;
4649       end loop;
4650    end Validate_Unchecked_Conversions;
4651
4652 end Sem_Ch13;