OSDN Git Service

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