OSDN Git Service

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