OSDN Git Service

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