OSDN Git Service

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