OSDN Git Service

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