OSDN Git Service

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