OSDN Git Service

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