OSDN Git Service

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