OSDN Git Service

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