OSDN Git Service

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