OSDN Git Service

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