OSDN Git Service

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