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