OSDN Git Service

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