OSDN Git Service

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