OSDN Git Service

2010-10-05 Ed Schonberg <schonberg@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
1302          begin
1303             if not Is_Array_Type (U_Ent) then
1304                Error_Msg_N ("component size requires array type", Nam);
1305                return;
1306             end if;
1307
1308             Btype := Base_Type (U_Ent);
1309             Ctyp := Component_Type (Btype);
1310
1311             if Has_Component_Size_Clause (Btype) then
1312                Error_Msg_N
1313                  ("component size clause for& previously given", Nam);
1314
1315             elsif Csize /= No_Uint then
1316                Check_Size (Expr, Ctyp, Csize, Biased);
1317
1318                if Has_Aliased_Components (Btype)
1319                  and then Csize < 32
1320                  and then Csize /= 8
1321                  and then Csize /= 16
1322                then
1323                   Error_Msg_N
1324                     ("component size incorrect for aliased components", N);
1325                   return;
1326                end if;
1327
1328                --  For the biased case, build a declaration for a subtype
1329                --  that will be used to represent the biased subtype that
1330                --  reflects the biased representation of components. We need
1331                --  this subtype to get proper conversions on referencing
1332                --  elements of the array. Note that component size clauses
1333                --  are ignored in VM mode.
1334
1335                if VM_Target = No_VM then
1336                   if Biased then
1337                      New_Ctyp :=
1338                        Make_Defining_Identifier (Loc,
1339                          Chars =>
1340                            New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1341
1342                      Decl :=
1343                        Make_Subtype_Declaration (Loc,
1344                          Defining_Identifier => New_Ctyp,
1345                          Subtype_Indication  =>
1346                            New_Occurrence_Of (Component_Type (Btype), Loc));
1347
1348                      Set_Parent (Decl, N);
1349                      Analyze (Decl, Suppress => All_Checks);
1350
1351                      Set_Has_Delayed_Freeze        (New_Ctyp, False);
1352                      Set_Esize                     (New_Ctyp, Csize);
1353                      Set_RM_Size                   (New_Ctyp, Csize);
1354                      Init_Alignment                (New_Ctyp);
1355                      Set_Is_Itype                  (New_Ctyp, True);
1356                      Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1357
1358                      Set_Component_Type (Btype, New_Ctyp);
1359                      Set_Biased (New_Ctyp, N, "component size clause");
1360                   end if;
1361
1362                   Set_Component_Size (Btype, Csize);
1363
1364                --  For VM case, we ignore component size clauses
1365
1366                else
1367                   --  Give a warning unless we are in GNAT mode, in which case
1368                   --  the warning is suppressed since it is not useful.
1369
1370                   if not GNAT_Mode then
1371                      Error_Msg_N
1372                        ("?component size ignored in this configuration", N);
1373                   end if;
1374                end if;
1375
1376                --  Deal with warning on overridden size
1377
1378                if Warn_On_Overridden_Size
1379                  and then Has_Size_Clause (Ctyp)
1380                  and then RM_Size (Ctyp) /= Csize
1381                then
1382                   Error_Msg_NE
1383                     ("?component size overrides size clause for&",
1384                      N, Ctyp);
1385                end if;
1386
1387                Set_Has_Component_Size_Clause (Btype, True);
1388                Set_Has_Non_Standard_Rep      (Btype, True);
1389             end if;
1390          end Component_Size_Case;
1391
1392          ------------------
1393          -- External_Tag --
1394          ------------------
1395
1396          when Attribute_External_Tag => External_Tag :
1397          begin
1398             if not Is_Tagged_Type (U_Ent) then
1399                Error_Msg_N ("should be a tagged type", Nam);
1400             end if;
1401
1402             Analyze_And_Resolve (Expr, Standard_String);
1403
1404             if not Is_Static_Expression (Expr) then
1405                Flag_Non_Static_Expr
1406                  ("static string required for tag name!", Nam);
1407             end if;
1408
1409             if VM_Target = No_VM then
1410                Set_Has_External_Tag_Rep_Clause (U_Ent);
1411             else
1412                Error_Msg_Name_1 := Attr;
1413                Error_Msg_N
1414                  ("% attribute unsupported in this configuration", Nam);
1415             end if;
1416
1417             if not Is_Library_Level_Entity (U_Ent) then
1418                Error_Msg_NE
1419                  ("?non-unique external tag supplied for &", N, U_Ent);
1420                Error_Msg_N
1421                  ("?\same external tag applies to all subprogram calls", N);
1422                Error_Msg_N
1423                  ("?\corresponding internal tag cannot be obtained", N);
1424             end if;
1425          end External_Tag;
1426
1427          -----------
1428          -- Input --
1429          -----------
1430
1431          when Attribute_Input =>
1432             Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1433             Set_Has_Specified_Stream_Input (Ent);
1434
1435          -------------------
1436          -- Machine_Radix --
1437          -------------------
1438
1439          --  Machine radix attribute definition clause
1440
1441          when Attribute_Machine_Radix => Machine_Radix : declare
1442             Radix : constant Uint := Static_Integer (Expr);
1443
1444          begin
1445             if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1446                Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1447
1448             elsif Has_Machine_Radix_Clause (U_Ent) then
1449                Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1450                Error_Msg_N ("machine radix clause previously given#", N);
1451
1452             elsif Radix /= No_Uint then
1453                Set_Has_Machine_Radix_Clause (U_Ent);
1454                Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1455
1456                if Radix = 2 then
1457                   null;
1458                elsif Radix = 10 then
1459                   Set_Machine_Radix_10 (U_Ent);
1460                else
1461                   Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1462                end if;
1463             end if;
1464          end Machine_Radix;
1465
1466          -----------------
1467          -- Object_Size --
1468          -----------------
1469
1470          --  Object_Size attribute definition clause
1471
1472          when Attribute_Object_Size => Object_Size : declare
1473             Size : constant Uint := Static_Integer (Expr);
1474
1475             Biased : Boolean;
1476             pragma Warnings (Off, Biased);
1477
1478          begin
1479             if not Is_Type (U_Ent) then
1480                Error_Msg_N ("Object_Size cannot be given for &", Nam);
1481
1482             elsif Has_Object_Size_Clause (U_Ent) then
1483                Error_Msg_N ("Object_Size already given for &", Nam);
1484
1485             else
1486                Check_Size (Expr, U_Ent, Size, Biased);
1487
1488                if Size /= 8
1489                     and then
1490                   Size /= 16
1491                     and then
1492                   Size /= 32
1493                     and then
1494                   UI_Mod (Size, 64) /= 0
1495                then
1496                   Error_Msg_N
1497                     ("Object_Size must be 8, 16, 32, or multiple of 64",
1498                      Expr);
1499                end if;
1500
1501                Set_Esize (U_Ent, Size);
1502                Set_Has_Object_Size_Clause (U_Ent);
1503                Alignment_Check_For_Esize_Change (U_Ent);
1504             end if;
1505          end Object_Size;
1506
1507          ------------
1508          -- Output --
1509          ------------
1510
1511          when Attribute_Output =>
1512             Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1513             Set_Has_Specified_Stream_Output (Ent);
1514
1515          ----------
1516          -- Read --
1517          ----------
1518
1519          when Attribute_Read =>
1520             Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1521             Set_Has_Specified_Stream_Read (Ent);
1522
1523          ----------
1524          -- Size --
1525          ----------
1526
1527          --  Size attribute definition clause
1528
1529          when Attribute_Size => Size : declare
1530             Size   : constant Uint := Static_Integer (Expr);
1531             Etyp   : Entity_Id;
1532             Biased : Boolean;
1533
1534          begin
1535             FOnly := True;
1536
1537             if Has_Size_Clause (U_Ent) then
1538                Error_Msg_N ("size already given for &", Nam);
1539
1540             elsif not Is_Type (U_Ent)
1541               and then Ekind (U_Ent) /= E_Variable
1542               and then Ekind (U_Ent) /= E_Constant
1543             then
1544                Error_Msg_N ("size cannot be given for &", Nam);
1545
1546             elsif Is_Array_Type (U_Ent)
1547               and then not Is_Constrained (U_Ent)
1548             then
1549                Error_Msg_N
1550                  ("size cannot be given for unconstrained array", Nam);
1551
1552             elsif Size /= No_Uint then
1553
1554                if VM_Target /= No_VM and then not GNAT_Mode then
1555
1556                   --  Size clause is not handled properly on VM targets.
1557                   --  Display a warning unless we are in GNAT mode, in which
1558                   --  case this is useless.
1559
1560                   Error_Msg_N
1561                     ("?size clauses are ignored in this configuration", N);
1562                end if;
1563
1564                if Is_Type (U_Ent) then
1565                   Etyp := U_Ent;
1566                else
1567                   Etyp := Etype (U_Ent);
1568                end if;
1569
1570                --  Check size, note that Gigi is in charge of checking that the
1571                --  size of an array or record type is OK. Also we do not check
1572                --  the size in the ordinary fixed-point case, since it is too
1573                --  early to do so (there may be subsequent small clause that
1574                --  affects the size). We can check the size if a small clause
1575                --  has already been given.
1576
1577                if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1578                  or else Has_Small_Clause (U_Ent)
1579                then
1580                   Check_Size (Expr, Etyp, Size, Biased);
1581                   Set_Biased (U_Ent, N, "size clause", Biased);
1582                end if;
1583
1584                --  For types set RM_Size and Esize if possible
1585
1586                if Is_Type (U_Ent) then
1587                   Set_RM_Size (U_Ent, Size);
1588
1589                   --  For scalar types, increase Object_Size to power of 2, but
1590                   --  not less than a storage unit in any case (i.e., normally
1591                   --  this means it will be byte addressable).
1592
1593                   if Is_Scalar_Type (U_Ent) then
1594                      if Size <= System_Storage_Unit then
1595                         Init_Esize (U_Ent, System_Storage_Unit);
1596                      elsif Size <= 16 then
1597                         Init_Esize (U_Ent, 16);
1598                      elsif Size <= 32 then
1599                         Init_Esize (U_Ent, 32);
1600                      else
1601                         Set_Esize  (U_Ent, (Size + 63) / 64 * 64);
1602                      end if;
1603
1604                   --  For all other types, object size = value size. The
1605                   --  backend will adjust as needed.
1606
1607                   else
1608                      Set_Esize (U_Ent, Size);
1609                   end if;
1610
1611                   Alignment_Check_For_Esize_Change (U_Ent);
1612
1613                --  For objects, set Esize only
1614
1615                else
1616                   if Is_Elementary_Type (Etyp) then
1617                      if Size /= System_Storage_Unit
1618                           and then
1619                         Size /= System_Storage_Unit * 2
1620                           and then
1621                         Size /= System_Storage_Unit * 4
1622                            and then
1623                         Size /= System_Storage_Unit * 8
1624                      then
1625                         Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1626                         Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1627                         Error_Msg_N
1628                           ("size for primitive object must be a power of 2"
1629                             & " in the range ^-^", N);
1630                      end if;
1631                   end if;
1632
1633                   Set_Esize (U_Ent, Size);
1634                end if;
1635
1636                Set_Has_Size_Clause (U_Ent);
1637             end if;
1638          end Size;
1639
1640          -----------
1641          -- Small --
1642          -----------
1643
1644          --  Small attribute definition clause
1645
1646          when Attribute_Small => Small : declare
1647             Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1648             Small         : Ureal;
1649
1650          begin
1651             Analyze_And_Resolve (Expr, Any_Real);
1652
1653             if Etype (Expr) = Any_Type then
1654                return;
1655
1656             elsif not Is_Static_Expression (Expr) then
1657                Flag_Non_Static_Expr
1658                  ("small requires static expression!", Expr);
1659                return;
1660
1661             else
1662                Small := Expr_Value_R (Expr);
1663
1664                if Small <= Ureal_0 then
1665                   Error_Msg_N ("small value must be greater than zero", Expr);
1666                   return;
1667                end if;
1668
1669             end if;
1670
1671             if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1672                Error_Msg_N
1673                  ("small requires an ordinary fixed point type", Nam);
1674
1675             elsif Has_Small_Clause (U_Ent) then
1676                Error_Msg_N ("small already given for &", Nam);
1677
1678             elsif Small > Delta_Value (U_Ent) then
1679                Error_Msg_N
1680                  ("small value must not be greater then delta value", Nam);
1681
1682             else
1683                Set_Small_Value (U_Ent, Small);
1684                Set_Small_Value (Implicit_Base, Small);
1685                Set_Has_Small_Clause (U_Ent);
1686                Set_Has_Small_Clause (Implicit_Base);
1687                Set_Has_Non_Standard_Rep (Implicit_Base);
1688             end if;
1689          end Small;
1690
1691          ------------------
1692          -- Storage_Pool --
1693          ------------------
1694
1695          --  Storage_Pool attribute definition clause
1696
1697          when Attribute_Storage_Pool => Storage_Pool : declare
1698             Pool : Entity_Id;
1699             T    : Entity_Id;
1700
1701          begin
1702             if Ekind (U_Ent) = E_Access_Subprogram_Type then
1703                Error_Msg_N
1704                  ("storage pool cannot be given for access-to-subprogram type",
1705                   Nam);
1706                return;
1707
1708             elsif not
1709               Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
1710             then
1711                Error_Msg_N
1712                  ("storage pool can only be given for access types", Nam);
1713                return;
1714
1715             elsif Is_Derived_Type (U_Ent) then
1716                Error_Msg_N
1717                  ("storage pool cannot be given for a derived access type",
1718                   Nam);
1719
1720             elsif Has_Storage_Size_Clause (U_Ent) then
1721                Error_Msg_N ("storage size already given for &", Nam);
1722                return;
1723
1724             elsif Present (Associated_Storage_Pool (U_Ent)) then
1725                Error_Msg_N ("storage pool already given for &", Nam);
1726                return;
1727             end if;
1728
1729             Analyze_And_Resolve
1730               (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1731
1732             if not Denotes_Variable (Expr) then
1733                Error_Msg_N ("storage pool must be a variable", Expr);
1734                return;
1735             end if;
1736
1737             if Nkind (Expr) = N_Type_Conversion then
1738                T := Etype (Expression (Expr));
1739             else
1740                T := Etype (Expr);
1741             end if;
1742
1743             --  The Stack_Bounded_Pool is used internally for implementing
1744             --  access types with a Storage_Size. Since it only work
1745             --  properly when used on one specific type, we need to check
1746             --  that it is not hijacked improperly:
1747             --    type T is access Integer;
1748             --    for T'Storage_Size use n;
1749             --    type Q is access Float;
1750             --    for Q'Storage_Size use T'Storage_Size; -- incorrect
1751
1752             if RTE_Available (RE_Stack_Bounded_Pool)
1753               and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1754             then
1755                Error_Msg_N ("non-shareable internal Pool", Expr);
1756                return;
1757             end if;
1758
1759             --  If the argument is a name that is not an entity name, then
1760             --  we construct a renaming operation to define an entity of
1761             --  type storage pool.
1762
1763             if not Is_Entity_Name (Expr)
1764               and then Is_Object_Reference (Expr)
1765             then
1766                Pool := Make_Temporary (Loc, 'P', Expr);
1767
1768                declare
1769                   Rnode : constant Node_Id :=
1770                             Make_Object_Renaming_Declaration (Loc,
1771                               Defining_Identifier => Pool,
1772                               Subtype_Mark        =>
1773                                 New_Occurrence_Of (Etype (Expr), Loc),
1774                               Name                => Expr);
1775
1776                begin
1777                   Insert_Before (N, Rnode);
1778                   Analyze (Rnode);
1779                   Set_Associated_Storage_Pool (U_Ent, Pool);
1780                end;
1781
1782             elsif Is_Entity_Name (Expr) then
1783                Pool := Entity (Expr);
1784
1785                --  If pool is a renamed object, get original one. This can
1786                --  happen with an explicit renaming, and within instances.
1787
1788                while Present (Renamed_Object (Pool))
1789                  and then Is_Entity_Name (Renamed_Object (Pool))
1790                loop
1791                   Pool := Entity (Renamed_Object (Pool));
1792                end loop;
1793
1794                if Present (Renamed_Object (Pool))
1795                  and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1796                  and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1797                then
1798                   Pool := Entity (Expression (Renamed_Object (Pool)));
1799                end if;
1800
1801                Set_Associated_Storage_Pool (U_Ent, Pool);
1802
1803             elsif Nkind (Expr) = N_Type_Conversion
1804               and then Is_Entity_Name (Expression (Expr))
1805               and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1806             then
1807                Pool := Entity (Expression (Expr));
1808                Set_Associated_Storage_Pool (U_Ent, Pool);
1809
1810             else
1811                Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1812                return;
1813             end if;
1814          end Storage_Pool;
1815
1816          ------------------
1817          -- Storage_Size --
1818          ------------------
1819
1820          --  Storage_Size attribute definition clause
1821
1822          when Attribute_Storage_Size => Storage_Size : declare
1823             Btype : constant Entity_Id := Base_Type (U_Ent);
1824             Sprag : Node_Id;
1825
1826          begin
1827             if Is_Task_Type (U_Ent) then
1828                Check_Restriction (No_Obsolescent_Features, N);
1829
1830                if Warn_On_Obsolescent_Feature then
1831                   Error_Msg_N
1832                     ("storage size clause for task is an " &
1833                      "obsolescent feature (RM J.9)?", N);
1834                   Error_Msg_N ("\use Storage_Size pragma instead?", N);
1835                end if;
1836
1837                FOnly := True;
1838             end if;
1839
1840             if not Is_Access_Type (U_Ent)
1841               and then Ekind (U_Ent) /= E_Task_Type
1842             then
1843                Error_Msg_N ("storage size cannot be given for &", Nam);
1844
1845             elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1846                Error_Msg_N
1847                  ("storage size cannot be given for a derived access type",
1848                   Nam);
1849
1850             elsif Has_Storage_Size_Clause (Btype) then
1851                Error_Msg_N ("storage size already given for &", Nam);
1852
1853             else
1854                Analyze_And_Resolve (Expr, Any_Integer);
1855
1856                if Is_Access_Type (U_Ent) then
1857                   if Present (Associated_Storage_Pool (U_Ent)) then
1858                      Error_Msg_N ("storage pool already given for &", Nam);
1859                      return;
1860                   end if;
1861
1862                   if Compile_Time_Known_Value (Expr)
1863                     and then Expr_Value (Expr) = 0
1864                   then
1865                      Set_No_Pool_Assigned (Btype);
1866                   end if;
1867
1868                else -- Is_Task_Type (U_Ent)
1869                   Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1870
1871                   if Present (Sprag) then
1872                      Error_Msg_Sloc := Sloc (Sprag);
1873                      Error_Msg_N
1874                        ("Storage_Size already specified#", Nam);
1875                      return;
1876                   end if;
1877                end if;
1878
1879                Set_Has_Storage_Size_Clause (Btype);
1880             end if;
1881          end Storage_Size;
1882
1883          -----------------
1884          -- Stream_Size --
1885          -----------------
1886
1887          when Attribute_Stream_Size => Stream_Size : declare
1888             Size : constant Uint := Static_Integer (Expr);
1889
1890          begin
1891             if Ada_Version <= Ada_95 then
1892                Check_Restriction (No_Implementation_Attributes, N);
1893             end if;
1894
1895             if Has_Stream_Size_Clause (U_Ent) then
1896                Error_Msg_N ("Stream_Size already given for &", Nam);
1897
1898             elsif Is_Elementary_Type (U_Ent) then
1899                if Size /= System_Storage_Unit
1900                     and then
1901                   Size /= System_Storage_Unit * 2
1902                     and then
1903                   Size /= System_Storage_Unit * 4
1904                      and then
1905                   Size /= System_Storage_Unit * 8
1906                then
1907                   Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1908                   Error_Msg_N
1909                     ("stream size for elementary type must be a"
1910                        & " power of 2 and at least ^", N);
1911
1912                elsif RM_Size (U_Ent) > Size then
1913                   Error_Msg_Uint_1 := RM_Size (U_Ent);
1914                   Error_Msg_N
1915                     ("stream size for elementary type must be a"
1916                        & " power of 2 and at least ^", N);
1917                end if;
1918
1919                Set_Has_Stream_Size_Clause (U_Ent);
1920
1921             else
1922                Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1923             end if;
1924          end Stream_Size;
1925
1926          ----------------
1927          -- Value_Size --
1928          ----------------
1929
1930          --  Value_Size attribute definition clause
1931
1932          when Attribute_Value_Size => Value_Size : declare
1933             Size   : constant Uint := Static_Integer (Expr);
1934             Biased : Boolean;
1935
1936          begin
1937             if not Is_Type (U_Ent) then
1938                Error_Msg_N ("Value_Size cannot be given for &", Nam);
1939
1940             elsif Present
1941                    (Get_Attribute_Definition_Clause
1942                      (U_Ent, Attribute_Value_Size))
1943             then
1944                Error_Msg_N ("Value_Size already given for &", Nam);
1945
1946             elsif Is_Array_Type (U_Ent)
1947               and then not Is_Constrained (U_Ent)
1948             then
1949                Error_Msg_N
1950                  ("Value_Size cannot be given for unconstrained array", Nam);
1951
1952             else
1953                if Is_Elementary_Type (U_Ent) then
1954                   Check_Size (Expr, U_Ent, Size, Biased);
1955                   Set_Biased (U_Ent, N, "value size clause", Biased);
1956                end if;
1957
1958                Set_RM_Size (U_Ent, Size);
1959             end if;
1960          end Value_Size;
1961
1962          -----------
1963          -- Write --
1964          -----------
1965
1966          when Attribute_Write =>
1967             Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1968             Set_Has_Specified_Stream_Write (Ent);
1969
1970          --  All other attributes cannot be set
1971
1972          when others =>
1973             Error_Msg_N
1974               ("attribute& cannot be set with definition clause", N);
1975       end case;
1976
1977       --  The test for the type being frozen must be performed after
1978       --  any expression the clause has been analyzed since the expression
1979       --  itself might cause freezing that makes the clause illegal.
1980
1981       if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1982          return;
1983       end if;
1984    end Analyze_Attribute_Definition_Clause;
1985
1986    ----------------------------
1987    -- Analyze_Code_Statement --
1988    ----------------------------
1989
1990    procedure Analyze_Code_Statement (N : Node_Id) is
1991       HSS   : constant Node_Id   := Parent (N);
1992       SBody : constant Node_Id   := Parent (HSS);
1993       Subp  : constant Entity_Id := Current_Scope;
1994       Stmt  : Node_Id;
1995       Decl  : Node_Id;
1996       StmtO : Node_Id;
1997       DeclO : Node_Id;
1998
1999    begin
2000       --  Analyze and check we get right type, note that this implements the
2001       --  requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
2002       --  is the only way that Asm_Insn could possibly be visible.
2003
2004       Analyze_And_Resolve (Expression (N));
2005
2006       if Etype (Expression (N)) = Any_Type then
2007          return;
2008       elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
2009          Error_Msg_N ("incorrect type for code statement", N);
2010          return;
2011       end if;
2012
2013       Check_Code_Statement (N);
2014
2015       --  Make sure we appear in the handled statement sequence of a
2016       --  subprogram (RM 13.8(3)).
2017
2018       if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
2019         or else Nkind (SBody) /= N_Subprogram_Body
2020       then
2021          Error_Msg_N
2022            ("code statement can only appear in body of subprogram", N);
2023          return;
2024       end if;
2025
2026       --  Do remaining checks (RM 13.8(3)) if not already done
2027
2028       if not Is_Machine_Code_Subprogram (Subp) then
2029          Set_Is_Machine_Code_Subprogram (Subp);
2030
2031          --  No exception handlers allowed
2032
2033          if Present (Exception_Handlers (HSS)) then
2034             Error_Msg_N
2035               ("exception handlers not permitted in machine code subprogram",
2036                First (Exception_Handlers (HSS)));
2037          end if;
2038
2039          --  No declarations other than use clauses and pragmas (we allow
2040          --  certain internally generated declarations as well).
2041
2042          Decl := First (Declarations (SBody));
2043          while Present (Decl) loop
2044             DeclO := Original_Node (Decl);
2045             if Comes_From_Source (DeclO)
2046               and not Nkind_In (DeclO, N_Pragma,
2047                                        N_Use_Package_Clause,
2048                                        N_Use_Type_Clause,
2049                                        N_Implicit_Label_Declaration)
2050             then
2051                Error_Msg_N
2052                  ("this declaration not allowed in machine code subprogram",
2053                   DeclO);
2054             end if;
2055
2056             Next (Decl);
2057          end loop;
2058
2059          --  No statements other than code statements, pragmas, and labels.
2060          --  Again we allow certain internally generated statements.
2061
2062          Stmt := First (Statements (HSS));
2063          while Present (Stmt) loop
2064             StmtO := Original_Node (Stmt);
2065             if Comes_From_Source (StmtO)
2066               and then not Nkind_In (StmtO, N_Pragma,
2067                                             N_Label,
2068                                             N_Code_Statement)
2069             then
2070                Error_Msg_N
2071                  ("this statement is not allowed in machine code subprogram",
2072                   StmtO);
2073             end if;
2074
2075             Next (Stmt);
2076          end loop;
2077       end if;
2078    end Analyze_Code_Statement;
2079
2080    -----------------------------------------------
2081    -- Analyze_Enumeration_Representation_Clause --
2082    -----------------------------------------------
2083
2084    procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
2085       Ident    : constant Node_Id    := Identifier (N);
2086       Aggr     : constant Node_Id    := Array_Aggregate (N);
2087       Enumtype : Entity_Id;
2088       Elit     : Entity_Id;
2089       Expr     : Node_Id;
2090       Assoc    : Node_Id;
2091       Choice   : Node_Id;
2092       Val      : Uint;
2093       Err      : Boolean := False;
2094
2095       Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
2096       Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
2097       --  Allowed range of universal integer (= allowed range of enum lit vals)
2098
2099       Min : Uint;
2100       Max : Uint;
2101       --  Minimum and maximum values of entries
2102
2103       Max_Node : Node_Id;
2104       --  Pointer to node for literal providing max value
2105
2106    begin
2107       if Ignore_Rep_Clauses then
2108          return;
2109       end if;
2110
2111       --  First some basic error checks
2112
2113       Find_Type (Ident);
2114       Enumtype := Entity (Ident);
2115
2116       if Enumtype = Any_Type
2117         or else Rep_Item_Too_Early (Enumtype, N)
2118       then
2119          return;
2120       else
2121          Enumtype := Underlying_Type (Enumtype);
2122       end if;
2123
2124       if not Is_Enumeration_Type (Enumtype) then
2125          Error_Msg_NE
2126            ("enumeration type required, found}",
2127             Ident, First_Subtype (Enumtype));
2128          return;
2129       end if;
2130
2131       --  Ignore rep clause on generic actual type. This will already have
2132       --  been flagged on the template as an error, and this is the safest
2133       --  way to ensure we don't get a junk cascaded message in the instance.
2134
2135       if Is_Generic_Actual_Type (Enumtype) then
2136          return;
2137
2138       --  Type must be in current scope
2139
2140       elsif Scope (Enumtype) /= Current_Scope then
2141          Error_Msg_N ("type must be declared in this scope", Ident);
2142          return;
2143
2144       --  Type must be a first subtype
2145
2146       elsif not Is_First_Subtype (Enumtype) then
2147          Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
2148          return;
2149
2150       --  Ignore duplicate rep clause
2151
2152       elsif Has_Enumeration_Rep_Clause (Enumtype) then
2153          Error_Msg_N ("duplicate enumeration rep clause ignored", N);
2154          return;
2155
2156       --  Don't allow rep clause for standard [wide_[wide_]]character
2157
2158       elsif Is_Standard_Character_Type (Enumtype) then
2159          Error_Msg_N ("enumeration rep clause not allowed for this type", N);
2160          return;
2161
2162       --  Check that the expression is a proper aggregate (no parentheses)
2163
2164       elsif Paren_Count (Aggr) /= 0 then
2165          Error_Msg
2166            ("extra parentheses surrounding aggregate not allowed",
2167             First_Sloc (Aggr));
2168          return;
2169
2170       --  All tests passed, so set rep clause in place
2171
2172       else
2173          Set_Has_Enumeration_Rep_Clause (Enumtype);
2174          Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
2175       end if;
2176
2177       --  Now we process the aggregate. Note that we don't use the normal
2178       --  aggregate code for this purpose, because we don't want any of the
2179       --  normal expansion activities, and a number of special semantic
2180       --  rules apply (including the component type being any integer type)
2181
2182       Elit := First_Literal (Enumtype);
2183
2184       --  First the positional entries if any
2185
2186       if Present (Expressions (Aggr)) then
2187          Expr := First (Expressions (Aggr));
2188          while Present (Expr) loop
2189             if No (Elit) then
2190                Error_Msg_N ("too many entries in aggregate", Expr);
2191                return;
2192             end if;
2193
2194             Val := Static_Integer (Expr);
2195
2196             --  Err signals that we found some incorrect entries processing
2197             --  the list. The final checks for completeness and ordering are
2198             --  skipped in this case.
2199
2200             if Val = No_Uint then
2201                Err := True;
2202             elsif Val < Lo or else Hi < Val then
2203                Error_Msg_N ("value outside permitted range", Expr);
2204                Err := True;
2205             end if;
2206
2207             Set_Enumeration_Rep (Elit, Val);
2208             Set_Enumeration_Rep_Expr (Elit, Expr);
2209             Next (Expr);
2210             Next (Elit);
2211          end loop;
2212       end if;
2213
2214       --  Now process the named entries if present
2215
2216       if Present (Component_Associations (Aggr)) then
2217          Assoc := First (Component_Associations (Aggr));
2218          while Present (Assoc) loop
2219             Choice := First (Choices (Assoc));
2220
2221             if Present (Next (Choice)) then
2222                Error_Msg_N
2223                  ("multiple choice not allowed here", Next (Choice));
2224                Err := True;
2225             end if;
2226
2227             if Nkind (Choice) = N_Others_Choice then
2228                Error_Msg_N ("others choice not allowed here", Choice);
2229                Err := True;
2230
2231             elsif Nkind (Choice) = N_Range then
2232                --  ??? should allow zero/one element range here
2233                Error_Msg_N ("range not allowed here", Choice);
2234                Err := True;
2235
2236             else
2237                Analyze_And_Resolve (Choice, Enumtype);
2238
2239                if Is_Entity_Name (Choice)
2240                  and then Is_Type (Entity (Choice))
2241                then
2242                   Error_Msg_N ("subtype name not allowed here", Choice);
2243                   Err := True;
2244                   --  ??? should allow static subtype with zero/one entry
2245
2246                elsif Etype (Choice) = Base_Type (Enumtype) then
2247                   if not Is_Static_Expression (Choice) then
2248                      Flag_Non_Static_Expr
2249                        ("non-static expression used for choice!", Choice);
2250                      Err := True;
2251
2252                   else
2253                      Elit := Expr_Value_E (Choice);
2254
2255                      if Present (Enumeration_Rep_Expr (Elit)) then
2256                         Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2257                         Error_Msg_NE
2258                           ("representation for& previously given#",
2259                            Choice, Elit);
2260                         Err := True;
2261                      end if;
2262
2263                      Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
2264
2265                      Expr := Expression (Assoc);
2266                      Val := Static_Integer (Expr);
2267
2268                      if Val = No_Uint then
2269                         Err := True;
2270
2271                      elsif Val < Lo or else Hi < Val then
2272                         Error_Msg_N ("value outside permitted range", Expr);
2273                         Err := True;
2274                      end if;
2275
2276                      Set_Enumeration_Rep (Elit, Val);
2277                   end if;
2278                end if;
2279             end if;
2280
2281             Next (Assoc);
2282          end loop;
2283       end if;
2284
2285       --  Aggregate is fully processed. Now we check that a full set of
2286       --  representations was given, and that they are in range and in order.
2287       --  These checks are only done if no other errors occurred.
2288
2289       if not Err then
2290          Min  := No_Uint;
2291          Max  := No_Uint;
2292
2293          Elit := First_Literal (Enumtype);
2294          while Present (Elit) loop
2295             if No (Enumeration_Rep_Expr (Elit)) then
2296                Error_Msg_NE ("missing representation for&!", N, Elit);
2297
2298             else
2299                Val := Enumeration_Rep (Elit);
2300
2301                if Min = No_Uint then
2302                   Min := Val;
2303                end if;
2304
2305                if Val /= No_Uint then
2306                   if Max /= No_Uint and then Val <= Max then
2307                      Error_Msg_NE
2308                        ("enumeration value for& not ordered!",
2309                         Enumeration_Rep_Expr (Elit), Elit);
2310                   end if;
2311
2312                   Max_Node := Enumeration_Rep_Expr (Elit);
2313                   Max := Val;
2314                end if;
2315
2316                --  If there is at least one literal whose representation is not
2317                --  equal to the Pos value, then note that this enumeration type
2318                --  has a non-standard representation.
2319
2320                if Val /= Enumeration_Pos (Elit) then
2321                   Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2322                end if;
2323             end if;
2324
2325             Next (Elit);
2326          end loop;
2327
2328          --  Now set proper size information
2329
2330          declare
2331             Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2332
2333          begin
2334             if Has_Size_Clause (Enumtype) then
2335
2336                --  All OK, if size is OK now
2337
2338                if RM_Size (Enumtype) >= Minsize then
2339                   null;
2340
2341                else
2342                   --  Try if we can get by with biasing
2343
2344                   Minsize :=
2345                     UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2346
2347                   --  Error message if even biasing does not work
2348
2349                   if RM_Size (Enumtype) < Minsize then
2350                      Error_Msg_Uint_1 := RM_Size (Enumtype);
2351                      Error_Msg_Uint_2 := Max;
2352                      Error_Msg_N
2353                        ("previously given size (^) is too small "
2354                         & "for this value (^)", Max_Node);
2355
2356                   --  If biasing worked, indicate that we now have biased rep
2357
2358                   else
2359                      Set_Biased
2360                        (Enumtype, Size_Clause (Enumtype), "size clause");
2361                   end if;
2362                end if;
2363
2364             else
2365                Set_RM_Size    (Enumtype, Minsize);
2366                Set_Enum_Esize (Enumtype);
2367             end if;
2368
2369             Set_RM_Size   (Base_Type (Enumtype), RM_Size   (Enumtype));
2370             Set_Esize     (Base_Type (Enumtype), Esize     (Enumtype));
2371             Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2372          end;
2373       end if;
2374
2375       --  We repeat the too late test in case it froze itself!
2376
2377       if Rep_Item_Too_Late (Enumtype, N) then
2378          null;
2379       end if;
2380    end Analyze_Enumeration_Representation_Clause;
2381
2382    ----------------------------
2383    -- Analyze_Free_Statement --
2384    ----------------------------
2385
2386    procedure Analyze_Free_Statement (N : Node_Id) is
2387    begin
2388       Analyze (Expression (N));
2389    end Analyze_Free_Statement;
2390
2391    ---------------------------
2392    -- Analyze_Freeze_Entity --
2393    ---------------------------
2394
2395    procedure Analyze_Freeze_Entity (N : Node_Id) is
2396       E : constant Entity_Id := Entity (N);
2397
2398    begin
2399       --  For tagged types covering interfaces add internal entities that link
2400       --  the primitives of the interfaces with the primitives that cover them.
2401
2402       --  Note: These entities were originally generated only when generating
2403       --  code because their main purpose was to provide support to initialize
2404       --  the secondary dispatch tables. They are now generated also when
2405       --  compiling with no code generation to provide ASIS the relationship
2406       --  between interface primitives and tagged type primitives. They are
2407       --  also used to locate primitives covering interfaces when processing
2408       --  generics (see Derive_Subprograms).
2409
2410       if Ada_Version >= Ada_05
2411         and then Ekind (E) = E_Record_Type
2412         and then Is_Tagged_Type (E)
2413         and then not Is_Interface (E)
2414         and then Has_Interfaces (E)
2415       then
2416          --  This would be a good common place to call the routine that checks
2417          --  overriding of interface primitives (and thus factorize calls to
2418          --  Check_Abstract_Overriding located at different contexts in the
2419          --  compiler). However, this is not possible because it causes
2420          --  spurious errors in case of late overriding.
2421
2422          Add_Internal_Interface_Entities (E);
2423       end if;
2424
2425       --  Check CPP types
2426
2427       if Ekind (E) = E_Record_Type
2428         and then Is_CPP_Class (E)
2429         and then Is_Tagged_Type (E)
2430         and then Tagged_Type_Expansion
2431         and then Expander_Active
2432       then
2433          if CPP_Num_Prims (E) = 0 then
2434
2435             --  If the CPP type has user defined components then it must import
2436             --  primitives from C++. This is required because if the C++ class
2437             --  has no primitives then the C++ compiler does not added the _tag
2438             --  component to the type.
2439
2440             pragma Assert (Chars (First_Entity (E)) = Name_uTag);
2441
2442             if First_Entity (E) /= Last_Entity (E) then
2443                Error_Msg_N
2444                  ("?'C'P'P type must import at least one primitive from C++",
2445                   E);
2446             end if;
2447          end if;
2448
2449          --  Check that all its primitives are abstract or imported from C++.
2450          --  Check also availability of the C++ constructor.
2451
2452          declare
2453             Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
2454             Elmt             : Elmt_Id;
2455             Error_Reported   : Boolean := False;
2456             Prim             : Node_Id;
2457
2458          begin
2459             Elmt := First_Elmt (Primitive_Operations (E));
2460             while Present (Elmt) loop
2461                Prim := Node (Elmt);
2462
2463                if Comes_From_Source (Prim) then
2464                   if Is_Abstract_Subprogram (Prim) then
2465                      null;
2466
2467                   elsif not Is_Imported (Prim)
2468                     or else Convention (Prim) /= Convention_CPP
2469                   then
2470                      Error_Msg_N
2471                        ("?primitives of 'C'P'P types must be imported from C++"
2472                         & " or abstract", Prim);
2473
2474                   elsif not Has_Constructors
2475                      and then not Error_Reported
2476                   then
2477                      Error_Msg_Name_1 := Chars (E);
2478                      Error_Msg_N
2479                        ("?'C'P'P constructor required for type %", Prim);
2480                      Error_Reported := True;
2481                   end if;
2482                end if;
2483
2484                Next_Elmt (Elmt);
2485             end loop;
2486          end;
2487       end if;
2488    end Analyze_Freeze_Entity;
2489
2490    ------------------------------------------
2491    -- Analyze_Record_Representation_Clause --
2492    ------------------------------------------
2493
2494    --  Note: we check as much as we can here, but we can't do any checks
2495    --  based on the position values (e.g. overlap checks) until freeze time
2496    --  because especially in Ada 2005 (machine scalar mode), the processing
2497    --  for non-standard bit order can substantially change the positions.
2498    --  See procedure Check_Record_Representation_Clause (called from Freeze)
2499    --  for the remainder of this processing.
2500
2501    procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2502       Ident   : constant Node_Id    := Identifier (N);
2503       Rectype : Entity_Id;
2504       CC      : Node_Id;
2505       Posit   : Uint;
2506       Fbit    : Uint;
2507       Lbit    : Uint;
2508       Hbit    : Uint := Uint_0;
2509       Comp    : Entity_Id;
2510       Ocomp   : Entity_Id;
2511       Biased  : Boolean;
2512
2513       CR_Pragma : Node_Id := Empty;
2514       --  Points to N_Pragma node if Complete_Representation pragma present
2515
2516    begin
2517       if Ignore_Rep_Clauses then
2518          return;
2519       end if;
2520
2521       Find_Type (Ident);
2522       Rectype := Entity (Ident);
2523
2524       if Rectype = Any_Type
2525         or else Rep_Item_Too_Early (Rectype, N)
2526       then
2527          return;
2528       else
2529          Rectype := Underlying_Type (Rectype);
2530       end if;
2531
2532       --  First some basic error checks
2533
2534       if not Is_Record_Type (Rectype) then
2535          Error_Msg_NE
2536            ("record type required, found}", Ident, First_Subtype (Rectype));
2537          return;
2538
2539       elsif Is_Unchecked_Union (Rectype) then
2540          Error_Msg_N
2541            ("record rep clause not allowed for Unchecked_Union", N);
2542
2543       elsif Scope (Rectype) /= Current_Scope then
2544          Error_Msg_N ("type must be declared in this scope", N);
2545          return;
2546
2547       elsif not Is_First_Subtype (Rectype) then
2548          Error_Msg_N ("cannot give record rep clause for subtype", N);
2549          return;
2550
2551       elsif Has_Record_Rep_Clause (Rectype) then
2552          Error_Msg_N ("duplicate record rep clause ignored", N);
2553          return;
2554
2555       elsif Rep_Item_Too_Late (Rectype, N) then
2556          return;
2557       end if;
2558
2559       if Present (Mod_Clause (N)) then
2560          declare
2561             Loc     : constant Source_Ptr := Sloc (N);
2562             M       : constant Node_Id := Mod_Clause (N);
2563             P       : constant List_Id := Pragmas_Before (M);
2564             AtM_Nod : Node_Id;
2565
2566             Mod_Val : Uint;
2567             pragma Warnings (Off, Mod_Val);
2568
2569          begin
2570             Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2571
2572             if Warn_On_Obsolescent_Feature then
2573                Error_Msg_N
2574                  ("mod clause is an obsolescent feature (RM J.8)?", N);
2575                Error_Msg_N
2576                  ("\use alignment attribute definition clause instead?", N);
2577             end if;
2578
2579             if Present (P) then
2580                Analyze_List (P);
2581             end if;
2582
2583             --  In ASIS_Mode mode, expansion is disabled, but we must convert
2584             --  the Mod clause into an alignment clause anyway, so that the
2585             --  back-end can compute and back-annotate properly the size and
2586             --  alignment of types that may include this record.
2587
2588             --  This seems dubious, this destroys the source tree in a manner
2589             --  not detectable by ASIS ???
2590
2591             if Operating_Mode = Check_Semantics
2592               and then ASIS_Mode
2593             then
2594                AtM_Nod :=
2595                  Make_Attribute_Definition_Clause (Loc,
2596                    Name       => New_Reference_To (Base_Type (Rectype), Loc),
2597                    Chars      => Name_Alignment,
2598                    Expression => Relocate_Node (Expression (M)));
2599
2600                Set_From_At_Mod (AtM_Nod);
2601                Insert_After (N, AtM_Nod);
2602                Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2603                Set_Mod_Clause (N, Empty);
2604
2605             else
2606                --  Get the alignment value to perform error checking
2607
2608                Mod_Val := Get_Alignment_Value (Expression (M));
2609             end if;
2610          end;
2611       end if;
2612
2613       --  For untagged types, clear any existing component clauses for the
2614       --  type. If the type is derived, this is what allows us to override
2615       --  a rep clause for the parent. For type extensions, the representation
2616       --  of the inherited components is inherited, so we want to keep previous
2617       --  component clauses for completeness.
2618
2619       if not Is_Tagged_Type (Rectype) then
2620          Comp := First_Component_Or_Discriminant (Rectype);
2621          while Present (Comp) loop
2622             Set_Component_Clause (Comp, Empty);
2623             Next_Component_Or_Discriminant (Comp);
2624          end loop;
2625       end if;
2626
2627       --  All done if no component clauses
2628
2629       CC := First (Component_Clauses (N));
2630
2631       if No (CC) then
2632          return;
2633       end if;
2634
2635       --  A representation like this applies to the base type
2636
2637       Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2638       Set_Has_Non_Standard_Rep  (Base_Type (Rectype));
2639       Set_Has_Specified_Layout  (Base_Type (Rectype));
2640
2641       --  Process the component clauses
2642
2643       while Present (CC) loop
2644
2645          --  Pragma
2646
2647          if Nkind (CC) = N_Pragma then
2648             Analyze (CC);
2649
2650             --  The only pragma of interest is Complete_Representation
2651
2652             if Pragma_Name (CC) = Name_Complete_Representation then
2653                CR_Pragma := CC;
2654             end if;
2655
2656          --  Processing for real component clause
2657
2658          else
2659             Posit := Static_Integer (Position  (CC));
2660             Fbit  := Static_Integer (First_Bit (CC));
2661             Lbit  := Static_Integer (Last_Bit  (CC));
2662
2663             if Posit /= No_Uint
2664               and then Fbit /= No_Uint
2665               and then Lbit /= No_Uint
2666             then
2667                if Posit < 0 then
2668                   Error_Msg_N
2669                     ("position cannot be negative", Position (CC));
2670
2671                elsif Fbit < 0 then
2672                   Error_Msg_N
2673                     ("first bit cannot be negative", First_Bit (CC));
2674
2675                --  The Last_Bit specified in a component clause must not be
2676                --  less than the First_Bit minus one (RM-13.5.1(10)).
2677
2678                elsif Lbit < Fbit - 1 then
2679                   Error_Msg_N
2680                     ("last bit cannot be less than first bit minus one",
2681                      Last_Bit (CC));
2682
2683                --  Values look OK, so find the corresponding record component
2684                --  Even though the syntax allows an attribute reference for
2685                --  implementation-defined components, GNAT does not allow the
2686                --  tag to get an explicit position.
2687
2688                elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2689                   if Attribute_Name (Component_Name (CC)) = Name_Tag then
2690                      Error_Msg_N ("position of tag cannot be specified", CC);
2691                   else
2692                      Error_Msg_N ("illegal component name", CC);
2693                   end if;
2694
2695                else
2696                   Comp := First_Entity (Rectype);
2697                   while Present (Comp) loop
2698                      exit when Chars (Comp) = Chars (Component_Name (CC));
2699                      Next_Entity (Comp);
2700                   end loop;
2701
2702                   if No (Comp) then
2703
2704                      --  Maybe component of base type that is absent from
2705                      --  statically constrained first subtype.
2706
2707                      Comp := First_Entity (Base_Type (Rectype));
2708                      while Present (Comp) loop
2709                         exit when Chars (Comp) = Chars (Component_Name (CC));
2710                         Next_Entity (Comp);
2711                      end loop;
2712                   end if;
2713
2714                   if No (Comp) then
2715                      Error_Msg_N
2716                        ("component clause is for non-existent field", CC);
2717
2718                   elsif Present (Component_Clause (Comp)) then
2719
2720                      --  Diagnose duplicate rep clause, or check consistency
2721                      --  if this is an inherited component. In a double fault,
2722                      --  there may be a duplicate inconsistent clause for an
2723                      --  inherited component.
2724
2725                      if Scope (Original_Record_Component (Comp)) = Rectype
2726                        or else Parent (Component_Clause (Comp)) = N
2727                      then
2728                         Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2729                         Error_Msg_N ("component clause previously given#", CC);
2730
2731                      else
2732                         declare
2733                            Rep1 : constant Node_Id := Component_Clause (Comp);
2734                         begin
2735                            if Intval (Position (Rep1)) /=
2736                                                    Intval (Position (CC))
2737                              or else Intval (First_Bit (Rep1)) /=
2738                                                    Intval (First_Bit (CC))
2739                              or else Intval (Last_Bit (Rep1)) /=
2740                                                    Intval (Last_Bit (CC))
2741                            then
2742                               Error_Msg_N ("component clause inconsistent "
2743                                 & "with representation of ancestor", CC);
2744                            elsif Warn_On_Redundant_Constructs then
2745                               Error_Msg_N ("?redundant component clause "
2746                                 & "for inherited component!", CC);
2747                            end if;
2748                         end;
2749                      end if;
2750
2751                   --  Normal case where this is the first component clause we
2752                   --  have seen for this entity, so set it up properly.
2753
2754                   else
2755                      --  Make reference for field in record rep clause and set
2756                      --  appropriate entity field in the field identifier.
2757
2758                      Generate_Reference
2759                        (Comp, Component_Name (CC), Set_Ref => False);
2760                      Set_Entity (Component_Name (CC), Comp);
2761
2762                      --  Update Fbit and Lbit to the actual bit number
2763
2764                      Fbit := Fbit + UI_From_Int (SSU) * Posit;
2765                      Lbit := Lbit + UI_From_Int (SSU) * Posit;
2766
2767                      if Has_Size_Clause (Rectype)
2768                        and then Esize (Rectype) <= Lbit
2769                      then
2770                         Error_Msg_N
2771                           ("bit number out of range of specified size",
2772                            Last_Bit (CC));
2773                      else
2774                         Set_Component_Clause     (Comp, CC);
2775                         Set_Component_Bit_Offset (Comp, Fbit);
2776                         Set_Esize                (Comp, 1 + (Lbit - Fbit));
2777                         Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2778                         Set_Normalized_Position  (Comp, Fbit / SSU);
2779
2780                         if Warn_On_Overridden_Size
2781                           and then Has_Size_Clause (Etype (Comp))
2782                           and then RM_Size (Etype (Comp)) /= Esize (Comp)
2783                         then
2784                            Error_Msg_NE
2785                              ("?component size overrides size clause for&",
2786                               Component_Name (CC), Etype (Comp));
2787                         end if;
2788
2789                         --  This information is also set in the corresponding
2790                         --  component of the base type, found by accessing the
2791                         --  Original_Record_Component link if it is present.
2792
2793                         Ocomp := Original_Record_Component (Comp);
2794
2795                         if Hbit < Lbit then
2796                            Hbit := Lbit;
2797                         end if;
2798
2799                         Check_Size
2800                           (Component_Name (CC),
2801                            Etype (Comp),
2802                            Esize (Comp),
2803                            Biased);
2804
2805                         Set_Biased
2806                           (Comp, First_Node (CC), "component clause", Biased);
2807
2808                         if Present (Ocomp) then
2809                            Set_Component_Clause     (Ocomp, CC);
2810                            Set_Component_Bit_Offset (Ocomp, Fbit);
2811                            Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2812                            Set_Normalized_Position  (Ocomp, Fbit / SSU);
2813                            Set_Esize                (Ocomp, 1 + (Lbit - Fbit));
2814
2815                            Set_Normalized_Position_Max
2816                              (Ocomp, Normalized_Position (Ocomp));
2817
2818                            --  Note: we don't use Set_Biased here, because we
2819                            --  already gave a warning above if needed, and we
2820                            --  would get a duplicate for the same name here.
2821
2822                            Set_Has_Biased_Representation
2823                              (Ocomp, Has_Biased_Representation (Comp));
2824                         end if;
2825
2826                         if Esize (Comp) < 0 then
2827                            Error_Msg_N ("component size is negative", CC);
2828                         end if;
2829                      end if;
2830                   end if;
2831                end if;
2832             end if;
2833          end if;
2834
2835          Next (CC);
2836       end loop;
2837
2838       --  Check missing components if Complete_Representation pragma appeared
2839
2840       if Present (CR_Pragma) then
2841          Comp := First_Component_Or_Discriminant (Rectype);
2842          while Present (Comp) loop
2843             if No (Component_Clause (Comp)) then
2844                Error_Msg_NE
2845                  ("missing component clause for &", CR_Pragma, Comp);
2846             end if;
2847
2848             Next_Component_Or_Discriminant (Comp);
2849          end loop;
2850
2851          --  If no Complete_Representation pragma, warn if missing components
2852
2853       elsif Warn_On_Unrepped_Components then
2854          declare
2855             Num_Repped_Components   : Nat := 0;
2856             Num_Unrepped_Components : Nat := 0;
2857
2858          begin
2859             --  First count number of repped and unrepped components
2860
2861             Comp := First_Component_Or_Discriminant (Rectype);
2862             while Present (Comp) loop
2863                if Present (Component_Clause (Comp)) then
2864                   Num_Repped_Components := Num_Repped_Components + 1;
2865                else
2866                   Num_Unrepped_Components := Num_Unrepped_Components + 1;
2867                end if;
2868
2869                Next_Component_Or_Discriminant (Comp);
2870             end loop;
2871
2872             --  We are only interested in the case where there is at least one
2873             --  unrepped component, and at least half the components have rep
2874             --  clauses. We figure that if less than half have them, then the
2875             --  partial rep clause is really intentional. If the component
2876             --  type has no underlying type set at this point (as for a generic
2877             --  formal type), we don't know enough to give a warning on the
2878             --  component.
2879
2880             if Num_Unrepped_Components > 0
2881               and then Num_Unrepped_Components < Num_Repped_Components
2882             then
2883                Comp := First_Component_Or_Discriminant (Rectype);
2884                while Present (Comp) loop
2885                   if No (Component_Clause (Comp))
2886                     and then Comes_From_Source (Comp)
2887                     and then Present (Underlying_Type (Etype (Comp)))
2888                     and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
2889                                or else Size_Known_At_Compile_Time
2890                                          (Underlying_Type (Etype (Comp))))
2891                     and then not Has_Warnings_Off (Rectype)
2892                   then
2893                      Error_Msg_Sloc := Sloc (Comp);
2894                      Error_Msg_NE
2895                        ("?no component clause given for & declared #",
2896                         N, Comp);
2897                   end if;
2898
2899                   Next_Component_Or_Discriminant (Comp);
2900                end loop;
2901             end if;
2902          end;
2903       end if;
2904    end Analyze_Record_Representation_Clause;
2905
2906    -----------------------------------
2907    -- Check_Constant_Address_Clause --
2908    -----------------------------------
2909
2910    procedure Check_Constant_Address_Clause
2911      (Expr  : Node_Id;
2912       U_Ent : Entity_Id)
2913    is
2914       procedure Check_At_Constant_Address (Nod : Node_Id);
2915       --  Checks that the given node N represents a name whose 'Address is
2916       --  constant (in the same sense as OK_Constant_Address_Clause, i.e. the
2917       --  address value is the same at the point of declaration of U_Ent and at
2918       --  the time of elaboration of the address clause.
2919
2920       procedure Check_Expr_Constants (Nod : Node_Id);
2921       --  Checks that Nod meets the requirements for a constant address clause
2922       --  in the sense of the enclosing procedure.
2923
2924       procedure Check_List_Constants (Lst : List_Id);
2925       --  Check that all elements of list Lst meet the requirements for a
2926       --  constant address clause in the sense of the enclosing procedure.
2927
2928       -------------------------------
2929       -- Check_At_Constant_Address --
2930       -------------------------------
2931
2932       procedure Check_At_Constant_Address (Nod : Node_Id) is
2933       begin
2934          if Is_Entity_Name (Nod) then
2935             if Present (Address_Clause (Entity ((Nod)))) then
2936                Error_Msg_NE
2937                  ("invalid address clause for initialized object &!",
2938                            Nod, U_Ent);
2939                Error_Msg_NE
2940                  ("address for& cannot" &
2941                     " depend on another address clause! (RM 13.1(22))!",
2942                   Nod, U_Ent);
2943
2944             elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2945               and then Sloc (U_Ent) < Sloc (Entity (Nod))
2946             then
2947                Error_Msg_NE
2948                  ("invalid address clause for initialized object &!",
2949                   Nod, U_Ent);
2950                Error_Msg_Node_2 := U_Ent;
2951                Error_Msg_NE
2952                  ("\& must be defined before & (RM 13.1(22))!",
2953                   Nod, Entity (Nod));
2954             end if;
2955
2956          elsif Nkind (Nod) = N_Selected_Component then
2957             declare
2958                T : constant Entity_Id := Etype (Prefix (Nod));
2959
2960             begin
2961                if (Is_Record_Type (T)
2962                     and then Has_Discriminants (T))
2963                  or else
2964                   (Is_Access_Type (T)
2965                      and then Is_Record_Type (Designated_Type (T))
2966                      and then Has_Discriminants (Designated_Type (T)))
2967                then
2968                   Error_Msg_NE
2969                     ("invalid address clause for initialized object &!",
2970                      Nod, U_Ent);
2971                   Error_Msg_N
2972                     ("\address cannot depend on component" &
2973                      " of discriminated record (RM 13.1(22))!",
2974                      Nod);
2975                else
2976                   Check_At_Constant_Address (Prefix (Nod));
2977                end if;
2978             end;
2979
2980          elsif Nkind (Nod) = N_Indexed_Component then
2981             Check_At_Constant_Address (Prefix (Nod));
2982             Check_List_Constants (Expressions (Nod));
2983
2984          else
2985             Check_Expr_Constants (Nod);
2986          end if;
2987       end Check_At_Constant_Address;
2988
2989       --------------------------
2990       -- Check_Expr_Constants --
2991       --------------------------
2992
2993       procedure Check_Expr_Constants (Nod : Node_Id) is
2994          Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2995          Ent       : Entity_Id           := Empty;
2996
2997       begin
2998          if Nkind (Nod) in N_Has_Etype
2999            and then Etype (Nod) = Any_Type
3000          then
3001             return;
3002          end if;
3003
3004          case Nkind (Nod) is
3005             when N_Empty | N_Error =>
3006                return;
3007
3008             when N_Identifier | N_Expanded_Name =>
3009                Ent := Entity (Nod);
3010
3011                --  We need to look at the original node if it is different
3012                --  from the node, since we may have rewritten things and
3013                --  substituted an identifier representing the rewrite.
3014
3015                if Original_Node (Nod) /= Nod then
3016                   Check_Expr_Constants (Original_Node (Nod));
3017
3018                   --  If the node is an object declaration without initial
3019                   --  value, some code has been expanded, and the expression
3020                   --  is not constant, even if the constituents might be
3021                   --  acceptable, as in A'Address + offset.
3022
3023                   if Ekind (Ent) = E_Variable
3024                     and then
3025                       Nkind (Declaration_Node (Ent)) = N_Object_Declaration
3026                     and then
3027                       No (Expression (Declaration_Node (Ent)))
3028                   then
3029                      Error_Msg_NE
3030                        ("invalid address clause for initialized object &!",
3031                         Nod, U_Ent);
3032
3033                   --  If entity is constant, it may be the result of expanding
3034                   --  a check. We must verify that its declaration appears
3035                   --  before the object in question, else we also reject the
3036                   --  address clause.
3037
3038                   elsif Ekind (Ent) = E_Constant
3039                     and then In_Same_Source_Unit (Ent, U_Ent)
3040                     and then Sloc (Ent) > Loc_U_Ent
3041                   then
3042                      Error_Msg_NE
3043                        ("invalid address clause for initialized object &!",
3044                         Nod, U_Ent);
3045                   end if;
3046
3047                   return;
3048                end if;
3049
3050                --  Otherwise look at the identifier and see if it is OK
3051
3052                if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
3053                  or else Is_Type (Ent)
3054                then
3055                   return;
3056
3057                elsif
3058                   Ekind (Ent) = E_Constant
3059                     or else
3060                   Ekind (Ent) = E_In_Parameter
3061                then
3062                   --  This is the case where we must have Ent defined before
3063                   --  U_Ent. Clearly if they are in different units this
3064                   --  requirement is met since the unit containing Ent is
3065                   --  already processed.
3066
3067                   if not In_Same_Source_Unit (Ent, U_Ent) then
3068                      return;
3069
3070                   --  Otherwise location of Ent must be before the location
3071                   --  of U_Ent, that's what prior defined means.
3072
3073                   elsif Sloc (Ent) < Loc_U_Ent then
3074                      return;
3075
3076                   else
3077                      Error_Msg_NE
3078                        ("invalid address clause for initialized object &!",
3079                         Nod, U_Ent);
3080                      Error_Msg_Node_2 := U_Ent;
3081                      Error_Msg_NE
3082                        ("\& must be defined before & (RM 13.1(22))!",
3083                         Nod, Ent);
3084                   end if;
3085
3086                elsif Nkind (Original_Node (Nod)) = N_Function_Call then
3087                   Check_Expr_Constants (Original_Node (Nod));
3088
3089                else
3090                   Error_Msg_NE
3091                     ("invalid address clause for initialized object &!",
3092                      Nod, U_Ent);
3093
3094                   if Comes_From_Source (Ent) then
3095                      Error_Msg_NE
3096                        ("\reference to variable& not allowed"
3097                           & " (RM 13.1(22))!", Nod, Ent);
3098                   else
3099                      Error_Msg_N
3100                        ("non-static expression not allowed"
3101                           & " (RM 13.1(22))!", Nod);
3102                   end if;
3103                end if;
3104
3105             when N_Integer_Literal   =>
3106
3107                --  If this is a rewritten unchecked conversion, in a system
3108                --  where Address is an integer type, always use the base type
3109                --  for a literal value. This is user-friendly and prevents
3110                --  order-of-elaboration issues with instances of unchecked
3111                --  conversion.
3112
3113                if Nkind (Original_Node (Nod)) = N_Function_Call then
3114                   Set_Etype (Nod, Base_Type (Etype (Nod)));
3115                end if;
3116
3117             when N_Real_Literal      |
3118                  N_String_Literal    |
3119                  N_Character_Literal =>
3120                return;
3121
3122             when N_Range =>
3123                Check_Expr_Constants (Low_Bound (Nod));
3124                Check_Expr_Constants (High_Bound (Nod));
3125
3126             when N_Explicit_Dereference =>
3127                Check_Expr_Constants (Prefix (Nod));
3128
3129             when N_Indexed_Component =>
3130                Check_Expr_Constants (Prefix (Nod));
3131                Check_List_Constants (Expressions (Nod));
3132
3133             when N_Slice =>
3134                Check_Expr_Constants (Prefix (Nod));
3135                Check_Expr_Constants (Discrete_Range (Nod));
3136
3137             when N_Selected_Component =>
3138                Check_Expr_Constants (Prefix (Nod));
3139
3140             when N_Attribute_Reference =>
3141                if Attribute_Name (Nod) = Name_Address
3142                    or else
3143                   Attribute_Name (Nod) = Name_Access
3144                     or else
3145                   Attribute_Name (Nod) = Name_Unchecked_Access
3146                     or else
3147                   Attribute_Name (Nod) = Name_Unrestricted_Access
3148                then
3149                   Check_At_Constant_Address (Prefix (Nod));
3150
3151                else
3152                   Check_Expr_Constants (Prefix (Nod));
3153                   Check_List_Constants (Expressions (Nod));
3154                end if;
3155
3156             when N_Aggregate =>
3157                Check_List_Constants (Component_Associations (Nod));
3158                Check_List_Constants (Expressions (Nod));
3159
3160             when N_Component_Association =>
3161                Check_Expr_Constants (Expression (Nod));
3162
3163             when N_Extension_Aggregate =>
3164                Check_Expr_Constants (Ancestor_Part (Nod));
3165                Check_List_Constants (Component_Associations (Nod));
3166                Check_List_Constants (Expressions (Nod));
3167
3168             when N_Null =>
3169                return;
3170
3171             when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
3172                Check_Expr_Constants (Left_Opnd (Nod));
3173                Check_Expr_Constants (Right_Opnd (Nod));
3174
3175             when N_Unary_Op =>
3176                Check_Expr_Constants (Right_Opnd (Nod));
3177
3178             when N_Type_Conversion           |
3179                  N_Qualified_Expression      |
3180                  N_Allocator                 =>
3181                Check_Expr_Constants (Expression (Nod));
3182
3183             when N_Unchecked_Type_Conversion =>
3184                Check_Expr_Constants (Expression (Nod));
3185
3186                --  If this is a rewritten unchecked conversion, subtypes in
3187                --  this node are those created within the instance. To avoid
3188                --  order of elaboration issues, replace them with their base
3189                --  types. Note that address clauses can cause order of
3190                --  elaboration problems because they are elaborated by the
3191                --  back-end at the point of definition, and may mention
3192                --  entities declared in between (as long as everything is
3193                --  static). It is user-friendly to allow unchecked conversions
3194                --  in this context.
3195
3196                if Nkind (Original_Node (Nod)) = N_Function_Call then
3197                   Set_Etype (Expression (Nod),
3198                     Base_Type (Etype (Expression (Nod))));
3199                   Set_Etype (Nod, Base_Type (Etype (Nod)));
3200                end if;
3201
3202             when N_Function_Call =>
3203                if not Is_Pure (Entity (Name (Nod))) then
3204                   Error_Msg_NE
3205                     ("invalid address clause for initialized object &!",
3206                      Nod, U_Ent);
3207
3208                   Error_Msg_NE
3209                     ("\function & is not pure (RM 13.1(22))!",
3210                      Nod, Entity (Name (Nod)));
3211
3212                else
3213                   Check_List_Constants (Parameter_Associations (Nod));
3214                end if;
3215
3216             when N_Parameter_Association =>
3217                Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3218
3219             when others =>
3220                Error_Msg_NE
3221                  ("invalid address clause for initialized object &!",
3222                   Nod, U_Ent);
3223                Error_Msg_NE
3224                  ("\must be constant defined before& (RM 13.1(22))!",
3225                   Nod, U_Ent);
3226          end case;
3227       end Check_Expr_Constants;
3228
3229       --------------------------
3230       -- Check_List_Constants --
3231       --------------------------
3232
3233       procedure Check_List_Constants (Lst : List_Id) is
3234          Nod1 : Node_Id;
3235
3236       begin
3237          if Present (Lst) then
3238             Nod1 := First (Lst);
3239             while Present (Nod1) loop
3240                Check_Expr_Constants (Nod1);
3241                Next (Nod1);
3242             end loop;
3243          end if;
3244       end Check_List_Constants;
3245
3246    --  Start of processing for Check_Constant_Address_Clause
3247
3248    begin
3249       --  If rep_clauses are to be ignored, no need for legality checks. In
3250       --  particular, no need to pester user about rep clauses that violate
3251       --  the rule on constant addresses, given that these clauses will be
3252       --  removed by Freeze before they reach the back end.
3253
3254       if not Ignore_Rep_Clauses then
3255          Check_Expr_Constants (Expr);
3256       end if;
3257    end Check_Constant_Address_Clause;
3258
3259    ----------------------------------------
3260    -- Check_Record_Representation_Clause --
3261    ----------------------------------------
3262
3263    procedure Check_Record_Representation_Clause (N : Node_Id) is
3264       Loc     : constant Source_Ptr := Sloc (N);
3265       Ident   : constant Node_Id    := Identifier (N);
3266       Rectype : Entity_Id;
3267       Fent    : Entity_Id;
3268       CC      : Node_Id;
3269       Fbit    : Uint;
3270       Lbit    : Uint;
3271       Hbit    : Uint := Uint_0;
3272       Comp    : Entity_Id;
3273       Pcomp   : Entity_Id;
3274
3275       Max_Bit_So_Far : Uint;
3276       --  Records the maximum bit position so far. If all field positions
3277       --  are monotonically increasing, then we can skip the circuit for
3278       --  checking for overlap, since no overlap is possible.
3279
3280       Tagged_Parent : Entity_Id := Empty;
3281       --  This is set in the case of a derived tagged type for which we have
3282       --  Is_Fully_Repped_Tagged_Type True (indicating that all components are
3283       --  positioned by record representation clauses). In this case we must
3284       --  check for overlap between components of this tagged type, and the
3285       --  components of its parent. Tagged_Parent will point to this parent
3286       --  type. For all other cases Tagged_Parent is left set to Empty.
3287
3288       Parent_Last_Bit : Uint;
3289       --  Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
3290       --  last bit position for any field in the parent type. We only need to
3291       --  check overlap for fields starting below this point.
3292
3293       Overlap_Check_Required : Boolean;
3294       --  Used to keep track of whether or not an overlap check is required
3295
3296       Overlap_Detected : Boolean := False;
3297       --  Set True if an overlap is detected
3298
3299       Ccount : Natural := 0;
3300       --  Number of component clauses in record rep clause
3301
3302       procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
3303       --  Given two entities for record components or discriminants, checks
3304       --  if they have overlapping component clauses and issues errors if so.
3305
3306       procedure Find_Component;
3307       --  Finds component entity corresponding to current component clause (in
3308       --  CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
3309       --  start/stop bits for the field. If there is no matching component or
3310       --  if the matching component does not have a component clause, then
3311       --  that's an error and Comp is set to Empty, but no error message is
3312       --  issued, since the message was already given. Comp is also set to
3313       --  Empty if the current "component clause" is in fact a pragma.
3314
3315       -----------------------------
3316       -- Check_Component_Overlap --
3317       -----------------------------
3318
3319       procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
3320          CC1 : constant Node_Id := Component_Clause (C1_Ent);
3321          CC2 : constant Node_Id := Component_Clause (C2_Ent);
3322
3323       begin
3324          if Present (CC1) and then Present (CC2) then
3325
3326             --  Exclude odd case where we have two tag fields in the same
3327             --  record, both at location zero. This seems a bit strange, but
3328             --  it seems to happen in some circumstances, perhaps on an error.
3329
3330             if Chars (C1_Ent) = Name_uTag
3331                  and then
3332                Chars (C2_Ent) = Name_uTag
3333             then
3334                return;
3335             end if;
3336
3337             --  Here we check if the two fields overlap
3338
3339             declare
3340                S1 : constant Uint := Component_Bit_Offset (C1_Ent);
3341                S2 : constant Uint := Component_Bit_Offset (C2_Ent);
3342                E1 : constant Uint := S1 + Esize (C1_Ent);
3343                E2 : constant Uint := S2 + Esize (C2_Ent);
3344
3345             begin
3346                if E2 <= S1 or else E1 <= S2 then
3347                   null;
3348                else
3349                   Error_Msg_Node_2 := Component_Name (CC2);
3350                   Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
3351                   Error_Msg_Node_1 := Component_Name (CC1);
3352                   Error_Msg_N
3353                     ("component& overlaps & #", Component_Name (CC1));
3354                   Overlap_Detected := True;
3355                end if;
3356             end;
3357          end if;
3358       end Check_Component_Overlap;
3359
3360       --------------------
3361       -- Find_Component --
3362       --------------------
3363
3364       procedure Find_Component is
3365
3366          procedure Search_Component (R : Entity_Id);
3367          --  Search components of R for a match. If found, Comp is set.
3368
3369          ----------------------
3370          -- Search_Component --
3371          ----------------------
3372
3373          procedure Search_Component (R : Entity_Id) is
3374          begin
3375             Comp := First_Component_Or_Discriminant (R);
3376             while Present (Comp) loop
3377
3378                --  Ignore error of attribute name for component name (we
3379                --  already gave an error message for this, so no need to
3380                --  complain here)
3381
3382                if Nkind (Component_Name (CC)) = N_Attribute_Reference then
3383                   null;
3384                else
3385                   exit when Chars (Comp) = Chars (Component_Name (CC));
3386                end if;
3387
3388                Next_Component_Or_Discriminant (Comp);
3389             end loop;
3390          end Search_Component;
3391
3392       --  Start of processing for Find_Component
3393
3394       begin
3395          --  Return with Comp set to Empty if we have a pragma
3396
3397          if Nkind (CC) = N_Pragma then
3398             Comp := Empty;
3399             return;
3400          end if;
3401
3402          --  Search current record for matching component
3403
3404          Search_Component (Rectype);
3405
3406          --  If not found, maybe component of base type that is absent from
3407          --  statically constrained first subtype.
3408
3409          if No (Comp) then
3410             Search_Component (Base_Type (Rectype));
3411          end if;
3412
3413          --  If no component, or the component does not reference the component
3414          --  clause in question, then there was some previous error for which
3415          --  we already gave a message, so just return with Comp Empty.
3416
3417          if No (Comp)
3418            or else Component_Clause (Comp) /= CC
3419          then
3420             Comp := Empty;
3421
3422          --  Normal case where we have a component clause
3423
3424          else
3425             Fbit := Component_Bit_Offset (Comp);
3426             Lbit := Fbit + Esize (Comp) - 1;
3427          end if;
3428       end Find_Component;
3429
3430    --  Start of processing for Check_Record_Representation_Clause
3431
3432    begin
3433       Find_Type (Ident);
3434       Rectype := Entity (Ident);
3435
3436       if Rectype = Any_Type then
3437          return;
3438       else
3439          Rectype := Underlying_Type (Rectype);
3440       end if;
3441
3442       --  See if we have a fully repped derived tagged type
3443
3444       declare
3445          PS : constant Entity_Id := Parent_Subtype (Rectype);
3446
3447       begin
3448          if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
3449             Tagged_Parent := PS;
3450
3451             --  Find maximum bit of any component of the parent type
3452
3453             Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
3454             Pcomp := First_Entity (Tagged_Parent);
3455             while Present (Pcomp) loop
3456                if Ekind_In (Pcomp, E_Discriminant, E_Component) then
3457                   if Component_Bit_Offset (Pcomp) /= No_Uint
3458                     and then Known_Static_Esize (Pcomp)
3459                   then
3460                      Parent_Last_Bit :=
3461                        UI_Max
3462                          (Parent_Last_Bit,
3463                           Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
3464                   end if;
3465
3466                   Next_Entity (Pcomp);
3467                end if;
3468             end loop;
3469          end if;
3470       end;
3471
3472       --  All done if no component clauses
3473
3474       CC := First (Component_Clauses (N));
3475
3476       if No (CC) then
3477          return;
3478       end if;
3479
3480       --  If a tag is present, then create a component clause that places it
3481       --  at the start of the record (otherwise gigi may place it after other
3482       --  fields that have rep clauses).
3483
3484       Fent := First_Entity (Rectype);
3485
3486       if Nkind (Fent) = N_Defining_Identifier
3487         and then Chars (Fent) = Name_uTag
3488       then
3489          Set_Component_Bit_Offset    (Fent, Uint_0);
3490          Set_Normalized_Position     (Fent, Uint_0);
3491          Set_Normalized_First_Bit    (Fent, Uint_0);
3492          Set_Normalized_Position_Max (Fent, Uint_0);
3493          Init_Esize                  (Fent, System_Address_Size);
3494
3495          Set_Component_Clause (Fent,
3496            Make_Component_Clause (Loc,
3497              Component_Name =>
3498                Make_Identifier (Loc,
3499                  Chars => Name_uTag),
3500
3501              Position  =>
3502                Make_Integer_Literal (Loc,
3503                  Intval => Uint_0),
3504
3505              First_Bit =>
3506                Make_Integer_Literal (Loc,
3507                  Intval => Uint_0),
3508
3509              Last_Bit  =>
3510                Make_Integer_Literal (Loc,
3511                  UI_From_Int (System_Address_Size))));
3512
3513          Ccount := Ccount + 1;
3514       end if;
3515
3516       Max_Bit_So_Far := Uint_Minus_1;
3517       Overlap_Check_Required := False;
3518
3519       --  Process the component clauses
3520
3521       while Present (CC) loop
3522          Find_Component;
3523
3524          if Present (Comp) then
3525             Ccount := Ccount + 1;
3526
3527             --  We need a full overlap check if record positions non-monotonic
3528
3529             if Fbit <= Max_Bit_So_Far then
3530                Overlap_Check_Required := True;
3531             end if;
3532
3533             Max_Bit_So_Far := Lbit;
3534
3535             --  Check bit position out of range of specified size
3536
3537             if Has_Size_Clause (Rectype)
3538               and then Esize (Rectype) <= Lbit
3539             then
3540                Error_Msg_N
3541                  ("bit number out of range of specified size",
3542                   Last_Bit (CC));
3543
3544                --  Check for overlap with tag field
3545
3546             else
3547                if Is_Tagged_Type (Rectype)
3548                  and then Fbit < System_Address_Size
3549                then
3550                   Error_Msg_NE
3551                     ("component overlaps tag field of&",
3552                      Component_Name (CC), Rectype);
3553                   Overlap_Detected := True;
3554                end if;
3555
3556                if Hbit < Lbit then
3557                   Hbit := Lbit;
3558                end if;
3559             end if;
3560
3561             --  Check parent overlap if component might overlap parent field
3562
3563             if Present (Tagged_Parent)
3564               and then Fbit <= Parent_Last_Bit
3565             then
3566                Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
3567                while Present (Pcomp) loop
3568                   if not Is_Tag (Pcomp)
3569                     and then Chars (Pcomp) /= Name_uParent
3570                   then
3571                      Check_Component_Overlap (Comp, Pcomp);
3572                   end if;
3573
3574                   Next_Component_Or_Discriminant (Pcomp);
3575                end loop;
3576             end if;
3577          end if;
3578
3579          Next (CC);
3580       end loop;
3581
3582       --  Now that we have processed all the component clauses, check for
3583       --  overlap. We have to leave this till last, since the components can
3584       --  appear in any arbitrary order in the representation clause.
3585
3586       --  We do not need this check if all specified ranges were monotonic,
3587       --  as recorded by Overlap_Check_Required being False at this stage.
3588
3589       --  This first section checks if there are any overlapping entries at
3590       --  all. It does this by sorting all entries and then seeing if there are
3591       --  any overlaps. If there are none, then that is decisive, but if there
3592       --  are overlaps, they may still be OK (they may result from fields in
3593       --  different variants).
3594
3595       if Overlap_Check_Required then
3596          Overlap_Check1 : declare
3597
3598             OC_Fbit : array (0 .. Ccount) of Uint;
3599             --  First-bit values for component clauses, the value is the offset
3600             --  of the first bit of the field from start of record. The zero
3601             --  entry is for use in sorting.
3602
3603             OC_Lbit : array (0 .. Ccount) of Uint;
3604             --  Last-bit values for component clauses, the value is the offset
3605             --  of the last bit of the field from start of record. The zero
3606             --  entry is for use in sorting.
3607
3608             OC_Count : Natural := 0;
3609             --  Count of entries in OC_Fbit and OC_Lbit
3610
3611             function OC_Lt (Op1, Op2 : Natural) return Boolean;
3612             --  Compare routine for Sort
3613
3614             procedure OC_Move (From : Natural; To : Natural);
3615             --  Move routine for Sort
3616
3617             package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
3618
3619             -----------
3620             -- OC_Lt --
3621             -----------
3622
3623             function OC_Lt (Op1, Op2 : Natural) return Boolean is
3624             begin
3625                return OC_Fbit (Op1) < OC_Fbit (Op2);
3626             end OC_Lt;
3627
3628             -------------
3629             -- OC_Move --
3630             -------------
3631
3632             procedure OC_Move (From : Natural; To : Natural) is
3633             begin
3634                OC_Fbit (To) := OC_Fbit (From);
3635                OC_Lbit (To) := OC_Lbit (From);
3636             end OC_Move;
3637
3638             --  Start of processing for Overlap_Check
3639
3640          begin
3641             CC := First (Component_Clauses (N));
3642             while Present (CC) loop
3643
3644                --  Exclude component clause already marked in error
3645
3646                if not Error_Posted (CC) then
3647                   Find_Component;
3648
3649                   if Present (Comp) then
3650                      OC_Count := OC_Count + 1;
3651                      OC_Fbit (OC_Count) := Fbit;
3652                      OC_Lbit (OC_Count) := Lbit;
3653                   end if;
3654                end if;
3655
3656                Next (CC);
3657             end loop;
3658
3659             Sorting.Sort (OC_Count);
3660
3661             Overlap_Check_Required := False;
3662             for J in 1 .. OC_Count - 1 loop
3663                if OC_Lbit (J) >= OC_Fbit (J + 1) then
3664                   Overlap_Check_Required := True;
3665                   exit;
3666                end if;
3667             end loop;
3668          end Overlap_Check1;
3669       end if;
3670
3671       --  If Overlap_Check_Required is still True, then we have to do the full
3672       --  scale overlap check, since we have at least two fields that do
3673       --  overlap, and we need to know if that is OK since they are in
3674       --  different variant, or whether we have a definite problem.
3675
3676       if Overlap_Check_Required then
3677          Overlap_Check2 : declare
3678             C1_Ent, C2_Ent : Entity_Id;
3679             --  Entities of components being checked for overlap
3680
3681             Clist : Node_Id;
3682             --  Component_List node whose Component_Items are being checked
3683
3684             Citem : Node_Id;
3685             --  Component declaration for component being checked
3686
3687          begin
3688             C1_Ent := First_Entity (Base_Type (Rectype));
3689
3690             --  Loop through all components in record. For each component check
3691             --  for overlap with any of the preceding elements on the component
3692             --  list containing the component and also, if the component is in
3693             --  a variant, check against components outside the case structure.
3694             --  This latter test is repeated recursively up the variant tree.
3695
3696             Main_Component_Loop : while Present (C1_Ent) loop
3697                if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
3698                   goto Continue_Main_Component_Loop;
3699                end if;
3700
3701                --  Skip overlap check if entity has no declaration node. This
3702                --  happens with discriminants in constrained derived types.
3703                --  Possibly we are missing some checks as a result, but that
3704                --  does not seem terribly serious.
3705
3706                if No (Declaration_Node (C1_Ent)) then
3707                   goto Continue_Main_Component_Loop;
3708                end if;
3709
3710                Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
3711
3712                --  Loop through component lists that need checking. Check the
3713                --  current component list and all lists in variants above us.
3714
3715                Component_List_Loop : loop
3716
3717                   --  If derived type definition, go to full declaration
3718                   --  If at outer level, check discriminants if there are any.
3719
3720                   if Nkind (Clist) = N_Derived_Type_Definition then
3721                      Clist := Parent (Clist);
3722                   end if;
3723
3724                   --  Outer level of record definition, check discriminants
3725
3726                   if Nkind_In (Clist, N_Full_Type_Declaration,
3727                                N_Private_Type_Declaration)
3728                   then
3729                      if Has_Discriminants (Defining_Identifier (Clist)) then
3730                         C2_Ent :=
3731                           First_Discriminant (Defining_Identifier (Clist));
3732                         while Present (C2_Ent) loop
3733                            exit when C1_Ent = C2_Ent;
3734                            Check_Component_Overlap (C1_Ent, C2_Ent);
3735                            Next_Discriminant (C2_Ent);
3736                         end loop;
3737                      end if;
3738
3739                      --  Record extension case
3740
3741                   elsif Nkind (Clist) = N_Derived_Type_Definition then
3742                      Clist := Empty;
3743
3744                      --  Otherwise check one component list
3745
3746                   else
3747                      Citem := First (Component_Items (Clist));
3748                      while Present (Citem) loop
3749                         if Nkind (Citem) = N_Component_Declaration then
3750                            C2_Ent := Defining_Identifier (Citem);
3751                            exit when C1_Ent = C2_Ent;
3752                            Check_Component_Overlap (C1_Ent, C2_Ent);
3753                         end if;
3754
3755                         Next (Citem);
3756                      end loop;
3757                   end if;
3758
3759                   --  Check for variants above us (the parent of the Clist can
3760                   --  be a variant, in which case its parent is a variant part,
3761                   --  and the parent of the variant part is a component list
3762                   --  whose components must all be checked against the current
3763                   --  component for overlap).
3764
3765                   if Nkind (Parent (Clist)) = N_Variant then
3766                      Clist := Parent (Parent (Parent (Clist)));
3767
3768                      --  Check for possible discriminant part in record, this
3769                      --  is treated essentially as another level in the
3770                      --  recursion. For this case the parent of the component
3771                      --  list is the record definition, and its parent is the
3772                      --  full type declaration containing the discriminant
3773                      --  specifications.
3774
3775                   elsif Nkind (Parent (Clist)) = N_Record_Definition then
3776                      Clist := Parent (Parent ((Clist)));
3777
3778                      --  If neither of these two cases, we are at the top of
3779                      --  the tree.
3780
3781                   else
3782                      exit Component_List_Loop;
3783                   end if;
3784                end loop Component_List_Loop;
3785
3786                <<Continue_Main_Component_Loop>>
3787                Next_Entity (C1_Ent);
3788
3789             end loop Main_Component_Loop;
3790          end Overlap_Check2;
3791       end if;
3792
3793       --  The following circuit deals with warning on record holes (gaps). We
3794       --  skip this check if overlap was detected, since it makes sense for the
3795       --  programmer to fix this illegality before worrying about warnings.
3796
3797       if not Overlap_Detected and Warn_On_Record_Holes then
3798          Record_Hole_Check : declare
3799             Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
3800             --  Full declaration of record type
3801
3802             procedure Check_Component_List
3803               (CL   : Node_Id;
3804                Sbit : Uint;
3805                DS   : List_Id);
3806             --  Check component list CL for holes. The starting bit should be
3807             --  Sbit. which is zero for the main record component list and set
3808             --  appropriately for recursive calls for variants. DS is set to
3809             --  a list of discriminant specifications to be included in the
3810             --  consideration of components. It is No_List if none to consider.
3811
3812             --------------------------
3813             -- Check_Component_List --
3814             --------------------------
3815
3816             procedure Check_Component_List
3817               (CL   : Node_Id;
3818                Sbit : Uint;
3819                DS   : List_Id)
3820             is
3821                Compl : Integer;
3822
3823             begin
3824                Compl := Integer (List_Length (Component_Items (CL)));
3825
3826                if DS /= No_List then
3827                   Compl := Compl + Integer (List_Length (DS));
3828                end if;
3829
3830                declare
3831                   Comps : array (Natural range 0 .. Compl) of Entity_Id;
3832                   --  Gather components (zero entry is for sort routine)
3833
3834                   Ncomps : Natural := 0;
3835                   --  Number of entries stored in Comps (starting at Comps (1))
3836
3837                   Citem : Node_Id;
3838                   --  One component item or discriminant specification
3839
3840                   Nbit  : Uint;
3841                   --  Starting bit for next component
3842
3843                   CEnt  : Entity_Id;
3844                   --  Component entity
3845
3846                   Variant : Node_Id;
3847                   --  One variant
3848
3849                   function Lt (Op1, Op2 : Natural) return Boolean;
3850                   --  Compare routine for Sort
3851
3852                   procedure Move (From : Natural; To : Natural);
3853                   --  Move routine for Sort
3854
3855                   package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
3856
3857                   --------
3858                   -- Lt --
3859                   --------
3860
3861                   function Lt (Op1, Op2 : Natural) return Boolean is
3862                   begin
3863                      return Component_Bit_Offset (Comps (Op1))
3864                        <
3865                        Component_Bit_Offset (Comps (Op2));
3866                   end Lt;
3867
3868                   ----------
3869                   -- Move --
3870                   ----------
3871
3872                   procedure Move (From : Natural; To : Natural) is
3873                   begin
3874                      Comps (To) := Comps (From);
3875                   end Move;
3876
3877                begin
3878                   --  Gather discriminants into Comp
3879
3880                   if DS /= No_List then
3881                      Citem := First (DS);
3882                      while Present (Citem) loop
3883                         if Nkind (Citem) = N_Discriminant_Specification then
3884                            declare
3885                               Ent : constant Entity_Id :=
3886                                       Defining_Identifier (Citem);
3887                            begin
3888                               if Ekind (Ent) = E_Discriminant then
3889                                  Ncomps := Ncomps + 1;
3890                                  Comps (Ncomps) := Ent;
3891                               end if;
3892                            end;
3893                         end if;
3894
3895                         Next (Citem);
3896                      end loop;
3897                   end if;
3898
3899                   --  Gather component entities into Comp
3900
3901                   Citem := First (Component_Items (CL));
3902                   while Present (Citem) loop
3903                      if Nkind (Citem) = N_Component_Declaration then
3904                         Ncomps := Ncomps + 1;
3905                         Comps (Ncomps) := Defining_Identifier (Citem);
3906                      end if;
3907
3908                      Next (Citem);
3909                   end loop;
3910
3911                   --  Now sort the component entities based on the first bit.
3912                   --  Note we already know there are no overlapping components.
3913
3914                   Sorting.Sort (Ncomps);
3915
3916                   --  Loop through entries checking for holes
3917
3918                   Nbit := Sbit;
3919                   for J in 1 .. Ncomps loop
3920                      CEnt := Comps (J);
3921                      Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
3922
3923                      if Error_Msg_Uint_1 > 0 then
3924                         Error_Msg_NE
3925                           ("?^-bit gap before component&",
3926                            Component_Name (Component_Clause (CEnt)), CEnt);
3927                      end if;
3928
3929                      Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
3930                   end loop;
3931
3932                   --  Process variant parts recursively if present
3933
3934                   if Present (Variant_Part (CL)) then
3935                      Variant := First (Variants (Variant_Part (CL)));
3936                      while Present (Variant) loop
3937                         Check_Component_List
3938                           (Component_List (Variant), Nbit, No_List);
3939                         Next (Variant);
3940                      end loop;
3941                   end if;
3942                end;
3943             end Check_Component_List;
3944
3945          --  Start of processing for Record_Hole_Check
3946
3947          begin
3948             declare
3949                Sbit : Uint;
3950
3951             begin
3952                if Is_Tagged_Type (Rectype) then
3953                   Sbit := UI_From_Int (System_Address_Size);
3954                else
3955                   Sbit := Uint_0;
3956                end if;
3957
3958                if Nkind (Decl) = N_Full_Type_Declaration
3959                  and then Nkind (Type_Definition (Decl)) = N_Record_Definition
3960                then
3961                   Check_Component_List
3962                     (Component_List (Type_Definition (Decl)),
3963                      Sbit,
3964                      Discriminant_Specifications (Decl));
3965                end if;
3966             end;
3967          end Record_Hole_Check;
3968       end if;
3969
3970       --  For records that have component clauses for all components, and whose
3971       --  size is less than or equal to 32, we need to know the size in the
3972       --  front end to activate possible packed array processing where the
3973       --  component type is a record.
3974
3975       --  At this stage Hbit + 1 represents the first unused bit from all the
3976       --  component clauses processed, so if the component clauses are
3977       --  complete, then this is the length of the record.
3978
3979       --  For records longer than System.Storage_Unit, and for those where not
3980       --  all components have component clauses, the back end determines the
3981       --  length (it may for example be appropriate to round up the size
3982       --  to some convenient boundary, based on alignment considerations, etc).
3983
3984       if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
3985
3986          --  Nothing to do if at least one component has no component clause
3987
3988          Comp := First_Component_Or_Discriminant (Rectype);
3989          while Present (Comp) loop
3990             exit when No (Component_Clause (Comp));
3991             Next_Component_Or_Discriminant (Comp);
3992          end loop;
3993
3994          --  If we fall out of loop, all components have component clauses
3995          --  and so we can set the size to the maximum value.
3996
3997          if No (Comp) then
3998             Set_RM_Size (Rectype, Hbit + 1);
3999          end if;
4000       end if;
4001    end Check_Record_Representation_Clause;
4002
4003    ----------------
4004    -- Check_Size --
4005    ----------------
4006
4007    procedure Check_Size
4008      (N      : Node_Id;
4009       T      : Entity_Id;
4010       Siz    : Uint;
4011       Biased : out Boolean)
4012    is
4013       UT : constant Entity_Id := Underlying_Type (T);
4014       M  : Uint;
4015
4016    begin
4017       Biased := False;
4018
4019       --  Dismiss cases for generic types or types with previous errors
4020
4021       if No (UT)
4022         or else UT = Any_Type
4023         or else Is_Generic_Type (UT)
4024         or else Is_Generic_Type (Root_Type (UT))
4025       then
4026          return;
4027
4028       --  Check case of bit packed array
4029
4030       elsif Is_Array_Type (UT)
4031         and then Known_Static_Component_Size (UT)
4032         and then Is_Bit_Packed_Array (UT)
4033       then
4034          declare
4035             Asiz : Uint;
4036             Indx : Node_Id;
4037             Ityp : Entity_Id;
4038
4039          begin
4040             Asiz := Component_Size (UT);
4041             Indx := First_Index (UT);
4042             loop
4043                Ityp := Etype (Indx);
4044
4045                --  If non-static bound, then we are not in the business of
4046                --  trying to check the length, and indeed an error will be
4047                --  issued elsewhere, since sizes of non-static array types
4048                --  cannot be set implicitly or explicitly.
4049
4050                if not Is_Static_Subtype (Ityp) then
4051                   return;
4052                end if;
4053
4054                --  Otherwise accumulate next dimension
4055
4056                Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
4057                                Expr_Value (Type_Low_Bound  (Ityp)) +
4058                                Uint_1);
4059
4060                Next_Index (Indx);
4061                exit when No (Indx);
4062             end loop;
4063
4064             if Asiz <= Siz then
4065                return;
4066             else
4067                Error_Msg_Uint_1 := Asiz;
4068                Error_Msg_NE
4069                  ("size for& too small, minimum allowed is ^", N, T);
4070                Set_Esize   (T, Asiz);
4071                Set_RM_Size (T, Asiz);
4072             end if;
4073          end;
4074
4075       --  All other composite types are ignored
4076
4077       elsif Is_Composite_Type (UT) then
4078          return;
4079
4080       --  For fixed-point types, don't check minimum if type is not frozen,
4081       --  since we don't know all the characteristics of the type that can
4082       --  affect the size (e.g. a specified small) till freeze time.
4083
4084       elsif Is_Fixed_Point_Type (UT)
4085         and then not Is_Frozen (UT)
4086       then
4087          null;
4088
4089       --  Cases for which a minimum check is required
4090
4091       else
4092          --  Ignore if specified size is correct for the type
4093
4094          if Known_Esize (UT) and then Siz = Esize (UT) then
4095             return;
4096          end if;
4097
4098          --  Otherwise get minimum size
4099
4100          M := UI_From_Int (Minimum_Size (UT));
4101
4102          if Siz < M then
4103
4104             --  Size is less than minimum size, but one possibility remains
4105             --  that we can manage with the new size if we bias the type.
4106
4107             M := UI_From_Int (Minimum_Size (UT, Biased => True));
4108
4109             if Siz < M then
4110                Error_Msg_Uint_1 := M;
4111                Error_Msg_NE
4112                  ("size for& too small, minimum allowed is ^", N, T);
4113                Set_Esize (T, M);
4114                Set_RM_Size (T, M);
4115             else
4116                Biased := True;
4117             end if;
4118          end if;
4119       end if;
4120    end Check_Size;
4121
4122    -------------------------
4123    -- Get_Alignment_Value --
4124    -------------------------
4125
4126    function Get_Alignment_Value (Expr : Node_Id) return Uint is
4127       Align : constant Uint := Static_Integer (Expr);
4128
4129    begin
4130       if Align = No_Uint then
4131          return No_Uint;
4132
4133       elsif Align <= 0 then
4134          Error_Msg_N ("alignment value must be positive", Expr);
4135          return No_Uint;
4136
4137       else
4138          for J in Int range 0 .. 64 loop
4139             declare
4140                M : constant Uint := Uint_2 ** J;
4141
4142             begin
4143                exit when M = Align;
4144
4145                if M > Align then
4146                   Error_Msg_N
4147                     ("alignment value must be power of 2", Expr);
4148                   return No_Uint;
4149                end if;
4150             end;
4151          end loop;
4152
4153          return Align;
4154       end if;
4155    end Get_Alignment_Value;
4156
4157    ----------------
4158    -- Initialize --
4159    ----------------
4160
4161    procedure Initialize is
4162    begin
4163       Unchecked_Conversions.Init;
4164    end Initialize;
4165
4166    -------------------------
4167    -- Is_Operational_Item --
4168    -------------------------
4169
4170    function Is_Operational_Item (N : Node_Id) return Boolean is
4171    begin
4172       if Nkind (N) /= N_Attribute_Definition_Clause then
4173          return False;
4174       else
4175          declare
4176             Id    : constant Attribute_Id := Get_Attribute_Id (Chars (N));
4177          begin
4178             return   Id = Attribute_Input
4179               or else Id = Attribute_Output
4180               or else Id = Attribute_Read
4181               or else Id = Attribute_Write
4182               or else Id = Attribute_External_Tag;
4183          end;
4184       end if;
4185    end Is_Operational_Item;
4186
4187    ------------------
4188    -- Minimum_Size --
4189    ------------------
4190
4191    function Minimum_Size
4192      (T      : Entity_Id;
4193       Biased : Boolean := False) return Nat
4194    is
4195       Lo     : Uint    := No_Uint;
4196       Hi     : Uint    := No_Uint;
4197       LoR    : Ureal   := No_Ureal;
4198       HiR    : Ureal   := No_Ureal;
4199       LoSet  : Boolean := False;
4200       HiSet  : Boolean := False;
4201       B      : Uint;
4202       S      : Nat;
4203       Ancest : Entity_Id;
4204       R_Typ  : constant Entity_Id := Root_Type (T);
4205
4206    begin
4207       --  If bad type, return 0
4208
4209       if T = Any_Type then
4210          return 0;
4211
4212       --  For generic types, just return zero. There cannot be any legitimate
4213       --  need to know such a size, but this routine may be called with a
4214       --  generic type as part of normal processing.
4215
4216       elsif Is_Generic_Type (R_Typ)
4217         or else R_Typ = Any_Type
4218       then
4219          return 0;
4220
4221          --  Access types. Normally an access type cannot have a size smaller
4222          --  than the size of System.Address. The exception is on VMS, where
4223          --  we have short and long addresses, and it is possible for an access
4224          --  type to have a short address size (and thus be less than the size
4225          --  of System.Address itself). We simply skip the check for VMS, and
4226          --  leave it to the back end to do the check.
4227
4228       elsif Is_Access_Type (T) then
4229          if OpenVMS_On_Target then
4230             return 0;
4231          else
4232             return System_Address_Size;
4233          end if;
4234
4235       --  Floating-point types
4236
4237       elsif Is_Floating_Point_Type (T) then
4238          return UI_To_Int (Esize (R_Typ));
4239
4240       --  Discrete types
4241
4242       elsif Is_Discrete_Type (T) then
4243
4244          --  The following loop is looking for the nearest compile time known
4245          --  bounds following the ancestor subtype chain. The idea is to find
4246          --  the most restrictive known bounds information.
4247
4248          Ancest := T;
4249          loop
4250             if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4251                return 0;
4252             end if;
4253
4254             if not LoSet then
4255                if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
4256                   Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
4257                   LoSet := True;
4258                   exit when HiSet;
4259                end if;
4260             end if;
4261
4262             if not HiSet then
4263                if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
4264                   Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
4265                   HiSet := True;
4266                   exit when LoSet;
4267                end if;
4268             end if;
4269
4270             Ancest := Ancestor_Subtype (Ancest);
4271
4272             if No (Ancest) then
4273                Ancest := Base_Type (T);
4274
4275                if Is_Generic_Type (Ancest) then
4276                   return 0;
4277                end if;
4278             end if;
4279          end loop;
4280
4281       --  Fixed-point types. We can't simply use Expr_Value to get the
4282       --  Corresponding_Integer_Value values of the bounds, since these do not
4283       --  get set till the type is frozen, and this routine can be called
4284       --  before the type is frozen. Similarly the test for bounds being static
4285       --  needs to include the case where we have unanalyzed real literals for
4286       --  the same reason.
4287
4288       elsif Is_Fixed_Point_Type (T) then
4289
4290          --  The following loop is looking for the nearest compile time known
4291          --  bounds following the ancestor subtype chain. The idea is to find
4292          --  the most restrictive known bounds information.
4293
4294          Ancest := T;
4295          loop
4296             if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4297                return 0;
4298             end if;
4299
4300             --  Note: In the following two tests for LoSet and HiSet, it may
4301             --  seem redundant to test for N_Real_Literal here since normally
4302             --  one would assume that the test for the value being known at
4303             --  compile time includes this case. However, there is a glitch.
4304             --  If the real literal comes from folding a non-static expression,
4305             --  then we don't consider any non- static expression to be known
4306             --  at compile time if we are in configurable run time mode (needed
4307             --  in some cases to give a clearer definition of what is and what
4308             --  is not accepted). So the test is indeed needed. Without it, we
4309             --  would set neither Lo_Set nor Hi_Set and get an infinite loop.
4310
4311             if not LoSet then
4312                if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
4313                  or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
4314                then
4315                   LoR := Expr_Value_R (Type_Low_Bound (Ancest));
4316                   LoSet := True;
4317                   exit when HiSet;
4318                end if;
4319             end if;
4320
4321             if not HiSet then
4322                if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
4323                  or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
4324                then
4325                   HiR := Expr_Value_R (Type_High_Bound (Ancest));
4326                   HiSet := True;
4327                   exit when LoSet;
4328                end if;
4329             end if;
4330
4331             Ancest := Ancestor_Subtype (Ancest);
4332
4333             if No (Ancest) then
4334                Ancest := Base_Type (T);
4335
4336                if Is_Generic_Type (Ancest) then
4337                   return 0;
4338                end if;
4339             end if;
4340          end loop;
4341
4342          Lo := UR_To_Uint (LoR / Small_Value (T));
4343          Hi := UR_To_Uint (HiR / Small_Value (T));
4344
4345       --  No other types allowed
4346
4347       else
4348          raise Program_Error;
4349       end if;
4350
4351       --  Fall through with Hi and Lo set. Deal with biased case
4352
4353       if (Biased
4354            and then not Is_Fixed_Point_Type (T)
4355            and then not (Is_Enumeration_Type (T)
4356                           and then Has_Non_Standard_Rep (T)))
4357         or else Has_Biased_Representation (T)
4358       then
4359          Hi := Hi - Lo;
4360          Lo := Uint_0;
4361       end if;
4362
4363       --  Signed case. Note that we consider types like range 1 .. -1 to be
4364       --  signed for the purpose of computing the size, since the bounds have
4365       --  to be accommodated in the base type.
4366
4367       if Lo < 0 or else Hi < 0 then
4368          S := 1;
4369          B := Uint_1;
4370
4371          --  S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
4372          --  Note that we accommodate the case where the bounds cross. This
4373          --  can happen either because of the way the bounds are declared
4374          --  or because of the algorithm in Freeze_Fixed_Point_Type.
4375
4376          while Lo < -B
4377            or else Hi < -B
4378            or else Lo >= B
4379            or else Hi >= B
4380          loop
4381             B := Uint_2 ** S;
4382             S := S + 1;
4383          end loop;
4384
4385       --  Unsigned case
4386
4387       else
4388          --  If both bounds are positive, make sure that both are represen-
4389          --  table in the case where the bounds are crossed. This can happen
4390          --  either because of the way the bounds are declared, or because of
4391          --  the algorithm in Freeze_Fixed_Point_Type.
4392
4393          if Lo > Hi then
4394             Hi := Lo;
4395          end if;
4396
4397          --  S = size, (can accommodate 0 .. (2**size - 1))
4398
4399          S := 0;
4400          while Hi >= Uint_2 ** S loop
4401             S := S + 1;
4402          end loop;
4403       end if;
4404
4405       return S;
4406    end Minimum_Size;
4407
4408    ---------------------------
4409    -- New_Stream_Subprogram --
4410    ---------------------------
4411
4412    procedure New_Stream_Subprogram
4413      (N     : Node_Id;
4414       Ent   : Entity_Id;
4415       Subp  : Entity_Id;
4416       Nam   : TSS_Name_Type)
4417    is
4418       Loc       : constant Source_Ptr := Sloc (N);
4419       Sname     : constant Name_Id    := Make_TSS_Name (Base_Type (Ent), Nam);
4420       Subp_Id   : Entity_Id;
4421       Subp_Decl : Node_Id;
4422       F         : Entity_Id;
4423       Etyp      : Entity_Id;
4424
4425       Defer_Declaration : constant Boolean :=
4426                             Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
4427       --  For a tagged type, there is a declaration for each stream attribute
4428       --  at the freeze point, and we must generate only a completion of this
4429       --  declaration. We do the same for private types, because the full view
4430       --  might be tagged. Otherwise we generate a declaration at the point of
4431       --  the attribute definition clause.
4432
4433       function Build_Spec return Node_Id;
4434       --  Used for declaration and renaming declaration, so that this is
4435       --  treated as a renaming_as_body.
4436
4437       ----------------
4438       -- Build_Spec --
4439       ----------------
4440
4441       function Build_Spec return Node_Id is
4442          Out_P   : constant Boolean := (Nam = TSS_Stream_Read);
4443          Formals : List_Id;
4444          Spec    : Node_Id;
4445          T_Ref   : constant Node_Id := New_Reference_To (Etyp, Loc);
4446
4447       begin
4448          Subp_Id := Make_Defining_Identifier (Loc, Sname);
4449
4450          --  S : access Root_Stream_Type'Class
4451
4452          Formals := New_List (
4453                       Make_Parameter_Specification (Loc,
4454                         Defining_Identifier =>
4455                           Make_Defining_Identifier (Loc, Name_S),
4456                         Parameter_Type =>
4457                           Make_Access_Definition (Loc,
4458                             Subtype_Mark =>
4459                               New_Reference_To (
4460                                 Designated_Type (Etype (F)), Loc))));
4461
4462          if Nam = TSS_Stream_Input then
4463             Spec := Make_Function_Specification (Loc,
4464                       Defining_Unit_Name       => Subp_Id,
4465                       Parameter_Specifications => Formals,
4466                       Result_Definition        => T_Ref);
4467          else
4468             --  V : [out] T
4469
4470             Append_To (Formals,
4471               Make_Parameter_Specification (Loc,
4472                 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
4473                 Out_Present         => Out_P,
4474                 Parameter_Type      => T_Ref));
4475
4476             Spec :=
4477               Make_Procedure_Specification (Loc,
4478                 Defining_Unit_Name       => Subp_Id,
4479                 Parameter_Specifications => Formals);
4480          end if;
4481
4482          return Spec;
4483       end Build_Spec;
4484
4485    --  Start of processing for New_Stream_Subprogram
4486
4487    begin
4488       F := First_Formal (Subp);
4489
4490       if Ekind (Subp) = E_Procedure then
4491          Etyp := Etype (Next_Formal (F));
4492       else
4493          Etyp := Etype (Subp);
4494       end if;
4495
4496       --  Prepare subprogram declaration and insert it as an action on the
4497       --  clause node. The visibility for this entity is used to test for
4498       --  visibility of the attribute definition clause (in the sense of
4499       --  8.3(23) as amended by AI-195).
4500
4501       if not Defer_Declaration then
4502          Subp_Decl :=
4503            Make_Subprogram_Declaration (Loc,
4504              Specification => Build_Spec);
4505
4506       --  For a tagged type, there is always a visible declaration for each
4507       --  stream TSS (it is a predefined primitive operation), and the
4508       --  completion of this declaration occurs at the freeze point, which is
4509       --  not always visible at places where the attribute definition clause is
4510       --  visible. So, we create a dummy entity here for the purpose of
4511       --  tracking the visibility of the attribute definition clause itself.
4512
4513       else
4514          Subp_Id :=
4515            Make_Defining_Identifier (Loc,
4516              Chars => New_External_Name (Sname, 'V'));
4517          Subp_Decl :=
4518            Make_Object_Declaration (Loc,
4519              Defining_Identifier => Subp_Id,
4520              Object_Definition   => New_Occurrence_Of (Standard_Boolean, Loc));
4521       end if;
4522
4523       Insert_Action (N, Subp_Decl);
4524       Set_Entity (N, Subp_Id);
4525
4526       Subp_Decl :=
4527         Make_Subprogram_Renaming_Declaration (Loc,
4528           Specification => Build_Spec,
4529           Name => New_Reference_To (Subp, Loc));
4530
4531       if Defer_Declaration then
4532          Set_TSS (Base_Type (Ent), Subp_Id);
4533       else
4534          Insert_Action (N, Subp_Decl);
4535          Copy_TSS (Subp_Id, Base_Type (Ent));
4536       end if;
4537    end New_Stream_Subprogram;
4538
4539    ------------------------
4540    -- Rep_Item_Too_Early --
4541    ------------------------
4542
4543    function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
4544    begin
4545       --  Cannot apply non-operational rep items to generic types
4546
4547       if Is_Operational_Item (N) then
4548          return False;
4549
4550       elsif Is_Type (T)
4551         and then Is_Generic_Type (Root_Type (T))
4552       then
4553          Error_Msg_N ("representation item not allowed for generic type", N);
4554          return True;
4555       end if;
4556
4557       --  Otherwise check for incomplete type
4558
4559       if Is_Incomplete_Or_Private_Type (T)
4560         and then No (Underlying_Type (T))
4561       then
4562          Error_Msg_N
4563            ("representation item must be after full type declaration", N);
4564          return True;
4565
4566       --  If the type has incomplete components, a representation clause is
4567       --  illegal but stream attributes and Convention pragmas are correct.
4568
4569       elsif Has_Private_Component (T) then
4570          if Nkind (N) = N_Pragma then
4571             return False;
4572          else
4573             Error_Msg_N
4574               ("representation item must appear after type is fully defined",
4575                 N);
4576             return True;
4577          end if;
4578       else
4579          return False;
4580       end if;
4581    end Rep_Item_Too_Early;
4582
4583    -----------------------
4584    -- Rep_Item_Too_Late --
4585    -----------------------
4586
4587    function Rep_Item_Too_Late
4588      (T     : Entity_Id;
4589       N     : Node_Id;
4590       FOnly : Boolean := False) return Boolean
4591    is
4592       S           : Entity_Id;
4593       Parent_Type : Entity_Id;
4594
4595       procedure Too_Late;
4596       --  Output the too late message. Note that this is not considered a
4597       --  serious error, since the effect is simply that we ignore the
4598       --  representation clause in this case.
4599
4600       --------------
4601       -- Too_Late --
4602       --------------
4603
4604       procedure Too_Late is
4605       begin
4606          Error_Msg_N ("|representation item appears too late!", N);
4607       end Too_Late;
4608
4609    --  Start of processing for Rep_Item_Too_Late
4610
4611    begin
4612       --  First make sure entity is not frozen (RM 13.1(9)). Exclude imported
4613       --  types, which may be frozen if they appear in a representation clause
4614       --  for a local type.
4615
4616       if Is_Frozen (T)
4617         and then not From_With_Type (T)
4618       then
4619          Too_Late;
4620          S := First_Subtype (T);
4621
4622          if Present (Freeze_Node (S)) then
4623             Error_Msg_NE
4624               ("?no more representation items for }", Freeze_Node (S), S);
4625          end if;
4626
4627          return True;
4628
4629       --  Check for case of non-tagged derived type whose parent either has
4630       --  primitive operations, or is a by reference type (RM 13.1(10)).
4631
4632       elsif Is_Type (T)
4633         and then not FOnly
4634         and then Is_Derived_Type (T)
4635         and then not Is_Tagged_Type (T)
4636       then
4637          Parent_Type := Etype (Base_Type (T));
4638
4639          if Has_Primitive_Operations (Parent_Type) then
4640             Too_Late;
4641             Error_Msg_NE
4642               ("primitive operations already defined for&!", N, Parent_Type);
4643             return True;
4644
4645          elsif Is_By_Reference_Type (Parent_Type) then
4646             Too_Late;
4647             Error_Msg_NE
4648               ("parent type & is a by reference type!", N, Parent_Type);
4649             return True;
4650          end if;
4651       end if;
4652
4653       --  No error, link item into head of chain of rep items for the entity,
4654       --  but avoid chaining if we have an overloadable entity, and the pragma
4655       --  is one that can apply to multiple overloaded entities.
4656
4657       if Is_Overloadable (T)
4658         and then Nkind (N) = N_Pragma
4659       then
4660          declare
4661             Pname : constant Name_Id := Pragma_Name (N);
4662          begin
4663             if Pname = Name_Convention or else
4664                Pname = Name_Import     or else
4665                Pname = Name_Export     or else
4666                Pname = Name_External   or else
4667                Pname = Name_Interface
4668             then
4669                return False;
4670             end if;
4671          end;
4672       end if;
4673
4674       Record_Rep_Item (T, N);
4675       return False;
4676    end Rep_Item_Too_Late;
4677
4678    -------------------------
4679    -- Same_Representation --
4680    -------------------------
4681
4682    function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
4683       T1 : constant Entity_Id := Underlying_Type (Typ1);
4684       T2 : constant Entity_Id := Underlying_Type (Typ2);
4685
4686    begin
4687       --  A quick check, if base types are the same, then we definitely have
4688       --  the same representation, because the subtype specific representation
4689       --  attributes (Size and Alignment) do not affect representation from
4690       --  the point of view of this test.
4691
4692       if Base_Type (T1) = Base_Type (T2) then
4693          return True;
4694
4695       elsif Is_Private_Type (Base_Type (T2))
4696         and then Base_Type (T1) = Full_View (Base_Type (T2))
4697       then
4698          return True;
4699       end if;
4700
4701       --  Tagged types never have differing representations
4702
4703       if Is_Tagged_Type (T1) then
4704          return True;
4705       end if;
4706
4707       --  Representations are definitely different if conventions differ
4708
4709       if Convention (T1) /= Convention (T2) then
4710          return False;
4711       end if;
4712
4713       --  Representations are different if component alignments differ
4714
4715       if (Is_Record_Type (T1) or else Is_Array_Type (T1))
4716         and then
4717          (Is_Record_Type (T2) or else Is_Array_Type (T2))
4718         and then Component_Alignment (T1) /= Component_Alignment (T2)
4719       then
4720          return False;
4721       end if;
4722
4723       --  For arrays, the only real issue is component size. If we know the
4724       --  component size for both arrays, and it is the same, then that's
4725       --  good enough to know we don't have a change of representation.
4726
4727       if Is_Array_Type (T1) then
4728          if Known_Component_Size (T1)
4729            and then Known_Component_Size (T2)
4730            and then Component_Size (T1) = Component_Size (T2)
4731          then
4732             return True;
4733          end if;
4734       end if;
4735
4736       --  Types definitely have same representation if neither has non-standard
4737       --  representation since default representations are always consistent.
4738       --  If only one has non-standard representation, and the other does not,
4739       --  then we consider that they do not have the same representation. They
4740       --  might, but there is no way of telling early enough.
4741
4742       if Has_Non_Standard_Rep (T1) then
4743          if not Has_Non_Standard_Rep (T2) then
4744             return False;
4745          end if;
4746       else
4747          return not Has_Non_Standard_Rep (T2);
4748       end if;
4749
4750       --  Here the two types both have non-standard representation, and we need
4751       --  to determine if they have the same non-standard representation.
4752
4753       --  For arrays, we simply need to test if the component sizes are the
4754       --  same. Pragma Pack is reflected in modified component sizes, so this
4755       --  check also deals with pragma Pack.
4756
4757       if Is_Array_Type (T1) then
4758          return Component_Size (T1) = Component_Size (T2);
4759
4760       --  Tagged types always have the same representation, because it is not
4761       --  possible to specify different representations for common fields.
4762
4763       elsif Is_Tagged_Type (T1) then
4764          return True;
4765
4766       --  Case of record types
4767
4768       elsif Is_Record_Type (T1) then
4769
4770          --  Packed status must conform
4771
4772          if Is_Packed (T1) /= Is_Packed (T2) then
4773             return False;
4774
4775          --  Otherwise we must check components. Typ2 maybe a constrained
4776          --  subtype with fewer components, so we compare the components
4777          --  of the base types.
4778
4779          else
4780             Record_Case : declare
4781                CD1, CD2 : Entity_Id;
4782
4783                function Same_Rep return Boolean;
4784                --  CD1 and CD2 are either components or discriminants. This
4785                --  function tests whether the two have the same representation
4786
4787                --------------
4788                -- Same_Rep --
4789                --------------
4790
4791                function Same_Rep return Boolean is
4792                begin
4793                   if No (Component_Clause (CD1)) then
4794                      return No (Component_Clause (CD2));
4795
4796                   else
4797                      return
4798                         Present (Component_Clause (CD2))
4799                           and then
4800                         Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
4801                           and then
4802                         Esize (CD1) = Esize (CD2);
4803                   end if;
4804                end Same_Rep;
4805
4806             --  Start of processing for Record_Case
4807
4808             begin
4809                if Has_Discriminants (T1) then
4810                   CD1 := First_Discriminant (T1);
4811                   CD2 := First_Discriminant (T2);
4812
4813                   --  The number of discriminants may be different if the
4814                   --  derived type has fewer (constrained by values). The
4815                   --  invisible discriminants retain the representation of
4816                   --  the original, so the discrepancy does not per se
4817                   --  indicate a different representation.
4818
4819                   while Present (CD1)
4820                     and then Present (CD2)
4821                   loop
4822                      if not Same_Rep then
4823                         return False;
4824                      else
4825                         Next_Discriminant (CD1);
4826                         Next_Discriminant (CD2);
4827                      end if;
4828                   end loop;
4829                end if;
4830
4831                CD1 := First_Component (Underlying_Type (Base_Type (T1)));
4832                CD2 := First_Component (Underlying_Type (Base_Type (T2)));
4833
4834                while Present (CD1) loop
4835                   if not Same_Rep then
4836                      return False;
4837                   else
4838                      Next_Component (CD1);
4839                      Next_Component (CD2);
4840                   end if;
4841                end loop;
4842
4843                return True;
4844             end Record_Case;
4845          end if;
4846
4847       --  For enumeration types, we must check each literal to see if the
4848       --  representation is the same. Note that we do not permit enumeration
4849       --  representation clauses for Character and Wide_Character, so these
4850       --  cases were already dealt with.
4851
4852       elsif Is_Enumeration_Type (T1) then
4853          Enumeration_Case : declare
4854             L1, L2 : Entity_Id;
4855
4856          begin
4857             L1 := First_Literal (T1);
4858             L2 := First_Literal (T2);
4859
4860             while Present (L1) loop
4861                if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
4862                   return False;
4863                else
4864                   Next_Literal (L1);
4865                   Next_Literal (L2);
4866                end if;
4867             end loop;
4868
4869             return True;
4870
4871          end Enumeration_Case;
4872
4873       --  Any other types have the same representation for these purposes
4874
4875       else
4876          return True;
4877       end if;
4878    end Same_Representation;
4879
4880    ----------------
4881    -- Set_Biased --
4882    ----------------
4883
4884    procedure Set_Biased
4885      (E      : Entity_Id;
4886       N      : Node_Id;
4887       Msg    : String;
4888       Biased : Boolean := True)
4889    is
4890    begin
4891       if Biased then
4892          Set_Has_Biased_Representation (E);
4893
4894          if Warn_On_Biased_Representation then
4895             Error_Msg_NE
4896               ("?" & Msg & " forces biased representation for&", N, E);
4897          end if;
4898       end if;
4899    end Set_Biased;
4900
4901    --------------------
4902    -- Set_Enum_Esize --
4903    --------------------
4904
4905    procedure Set_Enum_Esize (T : Entity_Id) is
4906       Lo : Uint;
4907       Hi : Uint;
4908       Sz : Nat;
4909
4910    begin
4911       Init_Alignment (T);
4912
4913       --  Find the minimum standard size (8,16,32,64) that fits
4914
4915       Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
4916       Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
4917
4918       if Lo < 0 then
4919          if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
4920             Sz := Standard_Character_Size;  -- May be > 8 on some targets
4921
4922          elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
4923             Sz := 16;
4924
4925          elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
4926             Sz := 32;
4927
4928          else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
4929             Sz := 64;
4930          end if;
4931
4932       else
4933          if Hi < Uint_2**08 then
4934             Sz := Standard_Character_Size;  -- May be > 8 on some targets
4935
4936          elsif Hi < Uint_2**16 then
4937             Sz := 16;
4938
4939          elsif Hi < Uint_2**32 then
4940             Sz := 32;
4941
4942          else pragma Assert (Hi < Uint_2**63);
4943             Sz := 64;
4944          end if;
4945       end if;
4946
4947       --  That minimum is the proper size unless we have a foreign convention
4948       --  and the size required is 32 or less, in which case we bump the size
4949       --  up to 32. This is required for C and C++ and seems reasonable for
4950       --  all other foreign conventions.
4951
4952       if Has_Foreign_Convention (T)
4953         and then Esize (T) < Standard_Integer_Size
4954       then
4955          Init_Esize (T, Standard_Integer_Size);
4956       else
4957          Init_Esize (T, Sz);
4958       end if;
4959    end Set_Enum_Esize;
4960
4961    ------------------------------
4962    -- Validate_Address_Clauses --
4963    ------------------------------
4964
4965    procedure Validate_Address_Clauses is
4966    begin
4967       for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
4968          declare
4969             ACCR : Address_Clause_Check_Record
4970                      renames Address_Clause_Checks.Table (J);
4971
4972             Expr : Node_Id;
4973
4974             X_Alignment : Uint;
4975             Y_Alignment : Uint;
4976
4977             X_Size : Uint;
4978             Y_Size : Uint;
4979
4980          begin
4981             --  Skip processing of this entry if warning already posted
4982
4983             if not Address_Warning_Posted (ACCR.N) then
4984
4985                Expr := Original_Node (Expression (ACCR.N));
4986
4987                --  Get alignments
4988
4989                X_Alignment := Alignment (ACCR.X);
4990                Y_Alignment := Alignment (ACCR.Y);
4991
4992                --  Similarly obtain sizes
4993
4994                X_Size := Esize (ACCR.X);
4995                Y_Size := Esize (ACCR.Y);
4996
4997                --  Check for large object overlaying smaller one
4998
4999                if Y_Size > Uint_0
5000                  and then X_Size > Uint_0
5001                  and then X_Size > Y_Size
5002                then
5003                   Error_Msg_NE
5004                     ("?& overlays smaller object", ACCR.N, ACCR.X);
5005                   Error_Msg_N
5006                     ("\?program execution may be erroneous", ACCR.N);
5007                   Error_Msg_Uint_1 := X_Size;
5008                   Error_Msg_NE
5009                     ("\?size of & is ^", ACCR.N, ACCR.X);
5010                   Error_Msg_Uint_1 := Y_Size;
5011                   Error_Msg_NE
5012                     ("\?size of & is ^", ACCR.N, ACCR.Y);
5013
5014                --  Check for inadequate alignment, both of the base object
5015                --  and of the offset, if any.
5016
5017                --  Note: we do not check the alignment if we gave a size
5018                --  warning, since it would likely be redundant.
5019
5020                elsif Y_Alignment /= Uint_0
5021                  and then (Y_Alignment < X_Alignment
5022                              or else (ACCR.Off
5023                                         and then
5024                                           Nkind (Expr) = N_Attribute_Reference
5025                                         and then
5026                                           Attribute_Name (Expr) = Name_Address
5027                                         and then
5028                                           Has_Compatible_Alignment
5029                                             (ACCR.X, Prefix (Expr))
5030                                              /= Known_Compatible))
5031                then
5032                   Error_Msg_NE
5033                     ("?specified address for& may be inconsistent "
5034                        & "with alignment",
5035                      ACCR.N, ACCR.X);
5036                   Error_Msg_N
5037                     ("\?program execution may be erroneous (RM 13.3(27))",
5038                      ACCR.N);
5039                   Error_Msg_Uint_1 := X_Alignment;
5040                   Error_Msg_NE
5041                     ("\?alignment of & is ^",
5042                      ACCR.N, ACCR.X);
5043                   Error_Msg_Uint_1 := Y_Alignment;
5044                   Error_Msg_NE
5045                     ("\?alignment of & is ^",
5046                      ACCR.N, ACCR.Y);
5047                   if Y_Alignment >= X_Alignment then
5048                      Error_Msg_N
5049                       ("\?but offset is not multiple of alignment",
5050                        ACCR.N);
5051                   end if;
5052                end if;
5053             end if;
5054          end;
5055       end loop;
5056    end Validate_Address_Clauses;
5057
5058    -----------------------------------
5059    -- Validate_Unchecked_Conversion --
5060    -----------------------------------
5061
5062    procedure Validate_Unchecked_Conversion
5063      (N        : Node_Id;
5064       Act_Unit : Entity_Id)
5065    is
5066       Source : Entity_Id;
5067       Target : Entity_Id;
5068       Vnode  : Node_Id;
5069
5070    begin
5071       --  Obtain source and target types. Note that we call Ancestor_Subtype
5072       --  here because the processing for generic instantiation always makes
5073       --  subtypes, and we want the original frozen actual types.
5074
5075       --  If we are dealing with private types, then do the check on their
5076       --  fully declared counterparts if the full declarations have been
5077       --  encountered (they don't have to be visible, but they must exist!)
5078
5079       Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
5080
5081       if Is_Private_Type (Source)
5082         and then Present (Underlying_Type (Source))
5083       then
5084          Source := Underlying_Type (Source);
5085       end if;
5086
5087       Target := Ancestor_Subtype (Etype (Act_Unit));
5088
5089       --  If either type is generic, the instantiation happens within a generic
5090       --  unit, and there is nothing to check. The proper check
5091       --  will happen when the enclosing generic is instantiated.
5092
5093       if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
5094          return;
5095       end if;
5096
5097       if Is_Private_Type (Target)
5098         and then Present (Underlying_Type (Target))
5099       then
5100          Target := Underlying_Type (Target);
5101       end if;
5102
5103       --  Source may be unconstrained array, but not target
5104
5105       if Is_Array_Type (Target)
5106         and then not Is_Constrained (Target)
5107       then
5108          Error_Msg_N
5109            ("unchecked conversion to unconstrained array not allowed", N);
5110          return;
5111       end if;
5112
5113       --  Warn if conversion between two different convention pointers
5114
5115       if Is_Access_Type (Target)
5116         and then Is_Access_Type (Source)
5117         and then Convention (Target) /= Convention (Source)
5118         and then Warn_On_Unchecked_Conversion
5119       then
5120          --  Give warnings for subprogram pointers only on most targets. The
5121          --  exception is VMS, where data pointers can have different lengths
5122          --  depending on the pointer convention.
5123
5124          if Is_Access_Subprogram_Type (Target)
5125            or else Is_Access_Subprogram_Type (Source)
5126            or else OpenVMS_On_Target
5127          then
5128             Error_Msg_N
5129               ("?conversion between pointers with different conventions!", N);
5130          end if;
5131       end if;
5132
5133       --  Warn if one of the operands is Ada.Calendar.Time. Do not emit a
5134       --  warning when compiling GNAT-related sources.
5135
5136       if Warn_On_Unchecked_Conversion
5137         and then not In_Predefined_Unit (N)
5138         and then RTU_Loaded (Ada_Calendar)
5139         and then
5140           (Chars (Source) = Name_Time
5141              or else
5142            Chars (Target) = Name_Time)
5143       then
5144          --  If Ada.Calendar is loaded and the name of one of the operands is
5145          --  Time, there is a good chance that this is Ada.Calendar.Time.
5146
5147          declare
5148             Calendar_Time : constant Entity_Id :=
5149                               Full_View (RTE (RO_CA_Time));
5150          begin
5151             pragma Assert (Present (Calendar_Time));
5152
5153             if Source = Calendar_Time
5154               or else Target = Calendar_Time
5155             then
5156                Error_Msg_N
5157                  ("?representation of 'Time values may change between " &
5158                   "'G'N'A'T versions", N);
5159             end if;
5160          end;
5161       end if;
5162
5163       --  Make entry in unchecked conversion table for later processing by
5164       --  Validate_Unchecked_Conversions, which will check sizes and alignments
5165       --  (using values set by the back-end where possible). This is only done
5166       --  if the appropriate warning is active.
5167
5168       if Warn_On_Unchecked_Conversion then
5169          Unchecked_Conversions.Append
5170            (New_Val => UC_Entry'
5171               (Eloc   => Sloc (N),
5172                Source => Source,
5173                Target => Target));
5174
5175          --  If both sizes are known statically now, then back end annotation
5176          --  is not required to do a proper check but if either size is not
5177          --  known statically, then we need the annotation.
5178
5179          if Known_Static_RM_Size (Source)
5180            and then Known_Static_RM_Size (Target)
5181          then
5182             null;
5183          else
5184             Back_Annotate_Rep_Info := True;
5185          end if;
5186       end if;
5187
5188       --  If unchecked conversion to access type, and access type is declared
5189       --  in the same unit as the unchecked conversion, then set the
5190       --  No_Strict_Aliasing flag (no strict aliasing is implicit in this
5191       --  situation).
5192
5193       if Is_Access_Type (Target) and then
5194         In_Same_Source_Unit (Target, N)
5195       then
5196          Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
5197       end if;
5198
5199       --  Generate N_Validate_Unchecked_Conversion node for back end in
5200       --  case the back end needs to perform special validation checks.
5201
5202       --  Shouldn't this be in Exp_Ch13, since the check only gets done
5203       --  if we have full expansion and the back end is called ???
5204
5205       Vnode :=
5206         Make_Validate_Unchecked_Conversion (Sloc (N));
5207       Set_Source_Type (Vnode, Source);
5208       Set_Target_Type (Vnode, Target);
5209
5210       --  If the unchecked conversion node is in a list, just insert before it.
5211       --  If not we have some strange case, not worth bothering about.
5212
5213       if Is_List_Member (N) then
5214          Insert_After (N, Vnode);
5215       end if;
5216    end Validate_Unchecked_Conversion;
5217
5218    ------------------------------------
5219    -- Validate_Unchecked_Conversions --
5220    ------------------------------------
5221
5222    procedure Validate_Unchecked_Conversions is
5223    begin
5224       for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
5225          declare
5226             T : UC_Entry renames Unchecked_Conversions.Table (N);
5227
5228             Eloc   : constant Source_Ptr := T.Eloc;
5229             Source : constant Entity_Id  := T.Source;
5230             Target : constant Entity_Id  := T.Target;
5231
5232             Source_Siz    : Uint;
5233             Target_Siz    : Uint;
5234
5235          begin
5236             --  This validation check, which warns if we have unequal sizes for
5237             --  unchecked conversion, and thus potentially implementation
5238             --  dependent semantics, is one of the few occasions on which we
5239             --  use the official RM size instead of Esize. See description in
5240             --  Einfo "Handling of Type'Size Values" for details.
5241
5242             if Serious_Errors_Detected = 0
5243               and then Known_Static_RM_Size (Source)
5244               and then Known_Static_RM_Size (Target)
5245
5246               --  Don't do the check if warnings off for either type, note the
5247               --  deliberate use of OR here instead of OR ELSE to get the flag
5248               --  Warnings_Off_Used set for both types if appropriate.
5249
5250               and then not (Has_Warnings_Off (Source)
5251                               or
5252                             Has_Warnings_Off (Target))
5253             then
5254                Source_Siz := RM_Size (Source);
5255                Target_Siz := RM_Size (Target);
5256
5257                if Source_Siz /= Target_Siz then
5258                   Error_Msg
5259                     ("?types for unchecked conversion have different sizes!",
5260                      Eloc);
5261
5262                   if All_Errors_Mode then
5263                      Error_Msg_Name_1 := Chars (Source);
5264                      Error_Msg_Uint_1 := Source_Siz;
5265                      Error_Msg_Name_2 := Chars (Target);
5266                      Error_Msg_Uint_2 := Target_Siz;
5267                      Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
5268
5269                      Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
5270
5271                      if Is_Discrete_Type (Source)
5272                        and then Is_Discrete_Type (Target)
5273                      then
5274                         if Source_Siz > Target_Siz then
5275                            Error_Msg
5276                              ("\?^ high order bits of source will be ignored!",
5277                               Eloc);
5278
5279                         elsif Is_Unsigned_Type (Source) then
5280                            Error_Msg
5281                              ("\?source will be extended with ^ high order " &
5282                               "zero bits?!", Eloc);
5283
5284                         else
5285                            Error_Msg
5286                              ("\?source will be extended with ^ high order " &
5287                               "sign bits!",
5288                               Eloc);
5289                         end if;
5290
5291                      elsif Source_Siz < Target_Siz then
5292                         if Is_Discrete_Type (Target) then
5293                            if Bytes_Big_Endian then
5294                               Error_Msg
5295                                 ("\?target value will include ^ undefined " &
5296                                  "low order bits!",
5297                                  Eloc);
5298                            else
5299                               Error_Msg
5300                                 ("\?target value will include ^ undefined " &
5301                                  "high order bits!",
5302                                  Eloc);
5303                            end if;
5304
5305                         else
5306                            Error_Msg
5307                              ("\?^ trailing bits of target value will be " &
5308                               "undefined!", Eloc);
5309                         end if;
5310
5311                      else pragma Assert (Source_Siz > Target_Siz);
5312                         Error_Msg
5313                           ("\?^ trailing bits of source will be ignored!",
5314                            Eloc);
5315                      end if;
5316                   end if;
5317                end if;
5318             end if;
5319
5320             --  If both types are access types, we need to check the alignment.
5321             --  If the alignment of both is specified, we can do it here.
5322
5323             if Serious_Errors_Detected = 0
5324               and then Ekind (Source) in Access_Kind
5325               and then Ekind (Target) in Access_Kind
5326               and then Target_Strict_Alignment
5327               and then Present (Designated_Type (Source))
5328               and then Present (Designated_Type (Target))
5329             then
5330                declare
5331                   D_Source : constant Entity_Id := Designated_Type (Source);
5332                   D_Target : constant Entity_Id := Designated_Type (Target);
5333
5334                begin
5335                   if Known_Alignment (D_Source)
5336                     and then Known_Alignment (D_Target)
5337                   then
5338                      declare
5339                         Source_Align : constant Uint := Alignment (D_Source);
5340                         Target_Align : constant Uint := Alignment (D_Target);
5341
5342                      begin
5343                         if Source_Align < Target_Align
5344                           and then not Is_Tagged_Type (D_Source)
5345
5346                           --  Suppress warning if warnings suppressed on either
5347                           --  type or either designated type. Note the use of
5348                           --  OR here instead of OR ELSE. That is intentional,
5349                           --  we would like to set flag Warnings_Off_Used in
5350                           --  all types for which warnings are suppressed.
5351
5352                           and then not (Has_Warnings_Off (D_Source)
5353                                           or
5354                                         Has_Warnings_Off (D_Target)
5355                                           or
5356                                         Has_Warnings_Off (Source)
5357                                           or
5358                                         Has_Warnings_Off (Target))
5359                         then
5360                            Error_Msg_Uint_1 := Target_Align;
5361                            Error_Msg_Uint_2 := Source_Align;
5362                            Error_Msg_Node_1 := D_Target;
5363                            Error_Msg_Node_2 := D_Source;
5364                            Error_Msg
5365                              ("?alignment of & (^) is stricter than " &
5366                               "alignment of & (^)!", Eloc);
5367                            Error_Msg
5368                              ("\?resulting access value may have invalid " &
5369                               "alignment!", Eloc);
5370                         end if;
5371                      end;
5372                   end if;
5373                end;
5374             end if;
5375          end;
5376       end loop;
5377    end Validate_Unchecked_Conversions;
5378
5379 end Sem_Ch13;