OSDN Git Service

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