OSDN Git Service

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