OSDN Git Service

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