Daily bump.
[official-gcc.git] / gcc / ada / sem_ch13.adb
blobfff9bded5229bd51f0d8191d5fa129f757403e2d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2012, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
35 with Lib; use Lib;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
40 with Opt; use Opt;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Rtsfind; use Rtsfind;
44 with Sem; use Sem;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch6; use Sem_Ch6;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Ch9; use Sem_Ch9;
50 with Sem_Dim; use Sem_Dim;
51 with Sem_Disp; use Sem_Disp;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Sinput; use Sinput;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Sinfo; use Sinfo;
61 with Stringt; use Stringt;
62 with Targparm; use Targparm;
63 with Ttypes; use Ttypes;
64 with Tbuild; use Tbuild;
65 with Urealp; use Urealp;
66 with Warnsw; use Warnsw;
68 with GNAT.Heap_Sort_G;
70 package body Sem_Ch13 is
72 SSU : constant Pos := System_Storage_Unit;
73 -- Convenient short hand for commonly used constant
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
80 -- This routine is called after setting one of the sizes of type entity
81 -- Typ to Size. The purpose is to deal with the situation of a derived
82 -- type whose inherited alignment is no longer appropriate for the new
83 -- size value. In this case, we reset the Alignment to unknown.
85 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id);
86 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
87 -- then either there are pragma Invariant entries on the rep chain for the
88 -- type (note that Predicate aspects are converted to pragma Predicate), or
89 -- there are inherited aspects from a parent type, or ancestor subtypes.
90 -- This procedure builds the spec and body for the Predicate function that
91 -- tests these predicates. N is the freeze node for the type. The spec of
92 -- the function is inserted before the freeze node, and the body of the
93 -- function is inserted after the freeze node.
95 procedure Build_Static_Predicate
96 (Typ : Entity_Id;
97 Expr : Node_Id;
98 Nam : Name_Id);
99 -- Given a predicated type Typ, where Typ is a discrete static subtype,
100 -- whose predicate expression is Expr, tests if Expr is a static predicate,
101 -- and if so, builds the predicate range list. Nam is the name of the one
102 -- argument to the predicate function. Occurrences of the type name in the
103 -- predicate expression have been replaced by identifier references to this
104 -- name, which is unique, so any identifier with Chars matching Nam must be
105 -- a reference to the type. If the predicate is non-static, this procedure
106 -- returns doing nothing. If the predicate is static, then the predicate
107 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
108 -- a canonicalized membership operation.
110 function Get_Alignment_Value (Expr : Node_Id) return Uint;
111 -- Given the expression for an alignment value, returns the corresponding
112 -- Uint value. If the value is inappropriate, then error messages are
113 -- posted as required, and a value of No_Uint is returned.
115 function Is_Operational_Item (N : Node_Id) return Boolean;
116 -- A specification for a stream attribute is allowed before the full type
117 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
118 -- that do not specify a representation characteristic are operational
119 -- attributes.
121 procedure New_Stream_Subprogram
122 (N : Node_Id;
123 Ent : Entity_Id;
124 Subp : Entity_Id;
125 Nam : TSS_Name_Type);
126 -- Create a subprogram renaming of a given stream attribute to the
127 -- designated subprogram and then in the tagged case, provide this as a
128 -- primitive operation, or in the non-tagged case make an appropriate TSS
129 -- entry. This is more properly an expansion activity than just semantics,
130 -- but the presence of user-defined stream functions for limited types is a
131 -- legality check, which is why this takes place here rather than in
132 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
133 -- function to be generated.
135 -- To avoid elaboration anomalies with freeze nodes, for untagged types
136 -- we generate both a subprogram declaration and a subprogram renaming
137 -- declaration, so that the attribute specification is handled as a
138 -- renaming_as_body. For tagged types, the specification is one of the
139 -- primitive specs.
141 generic
142 with procedure Replace_Type_Reference (N : Node_Id);
143 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
144 -- This is used to scan an expression for a predicate or invariant aspect
145 -- replacing occurrences of the name TName (the name of the subtype to
146 -- which the aspect applies) with appropriate references to the parameter
147 -- of the predicate function or invariant procedure. The procedure passed
148 -- as a generic parameter does the actual replacement of node N, which is
149 -- either a simple direct reference to TName, or a selected component that
150 -- represents an appropriately qualified occurrence of TName.
152 procedure Set_Biased
153 (E : Entity_Id;
154 N : Node_Id;
155 Msg : String;
156 Biased : Boolean := True);
157 -- If Biased is True, sets Has_Biased_Representation flag for E, and
158 -- outputs a warning message at node N if Warn_On_Biased_Representation is
159 -- is True. This warning inserts the string Msg to describe the construct
160 -- causing biasing.
162 ----------------------------------------------
163 -- Table for Validate_Unchecked_Conversions --
164 ----------------------------------------------
166 -- The following table collects unchecked conversions for validation.
167 -- Entries are made by Validate_Unchecked_Conversion and then the call
168 -- to Validate_Unchecked_Conversions does the actual error checking and
169 -- posting of warnings. The reason for this delayed processing is to take
170 -- advantage of back-annotations of size and alignment values performed by
171 -- the back end.
173 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
174 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
175 -- already have modified all Sloc values if the -gnatD option is set.
177 type UC_Entry is record
178 Eloc : Source_Ptr; -- node used for posting warnings
179 Source : Entity_Id; -- source type for unchecked conversion
180 Target : Entity_Id; -- target type for unchecked conversion
181 end record;
183 package Unchecked_Conversions is new Table.Table (
184 Table_Component_Type => UC_Entry,
185 Table_Index_Type => Int,
186 Table_Low_Bound => 1,
187 Table_Initial => 50,
188 Table_Increment => 200,
189 Table_Name => "Unchecked_Conversions");
191 ----------------------------------------
192 -- Table for Validate_Address_Clauses --
193 ----------------------------------------
195 -- If an address clause has the form
197 -- for X'Address use Expr
199 -- where Expr is of the form Y'Address or recursively is a reference to a
200 -- constant of either of these forms, and X and Y are entities of objects,
201 -- then if Y has a smaller alignment than X, that merits a warning about
202 -- possible bad alignment. The following table collects address clauses of
203 -- this kind. We put these in a table so that they can be checked after the
204 -- back end has completed annotation of the alignments of objects, since we
205 -- can catch more cases that way.
207 type Address_Clause_Check_Record is record
208 N : Node_Id;
209 -- The address clause
211 X : Entity_Id;
212 -- The entity of the object overlaying Y
214 Y : Entity_Id;
215 -- The entity of the object being overlaid
217 Off : Boolean;
218 -- Whether the address is offset within Y
219 end record;
221 package Address_Clause_Checks is new Table.Table (
222 Table_Component_Type => Address_Clause_Check_Record,
223 Table_Index_Type => Int,
224 Table_Low_Bound => 1,
225 Table_Initial => 20,
226 Table_Increment => 200,
227 Table_Name => "Address_Clause_Checks");
229 -----------------------------------------
230 -- Adjust_Record_For_Reverse_Bit_Order --
231 -----------------------------------------
233 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
234 Comp : Node_Id;
235 CC : Node_Id;
237 begin
238 -- Processing depends on version of Ada
240 -- For Ada 95, we just renumber bits within a storage unit. We do the
241 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
242 -- Ada 83, and are free to add this extension.
244 if Ada_Version < Ada_2005 then
245 Comp := First_Component_Or_Discriminant (R);
246 while Present (Comp) loop
247 CC := Component_Clause (Comp);
249 -- If component clause is present, then deal with the non-default
250 -- bit order case for Ada 95 mode.
252 -- We only do this processing for the base type, and in fact that
253 -- is important, since otherwise if there are record subtypes, we
254 -- could reverse the bits once for each subtype, which is wrong.
256 if Present (CC)
257 and then Ekind (R) = E_Record_Type
258 then
259 declare
260 CFB : constant Uint := Component_Bit_Offset (Comp);
261 CSZ : constant Uint := Esize (Comp);
262 CLC : constant Node_Id := Component_Clause (Comp);
263 Pos : constant Node_Id := Position (CLC);
264 FB : constant Node_Id := First_Bit (CLC);
266 Storage_Unit_Offset : constant Uint :=
267 CFB / System_Storage_Unit;
269 Start_Bit : constant Uint :=
270 CFB mod System_Storage_Unit;
272 begin
273 -- Cases where field goes over storage unit boundary
275 if Start_Bit + CSZ > System_Storage_Unit then
277 -- Allow multi-byte field but generate warning
279 if Start_Bit mod System_Storage_Unit = 0
280 and then CSZ mod System_Storage_Unit = 0
281 then
282 Error_Msg_N
283 ("multi-byte field specified with non-standard"
284 & " Bit_Order?", CLC);
286 if Bytes_Big_Endian then
287 Error_Msg_N
288 ("bytes are not reversed "
289 & "(component is big-endian)?", CLC);
290 else
291 Error_Msg_N
292 ("bytes are not reversed "
293 & "(component is little-endian)?", CLC);
294 end if;
296 -- Do not allow non-contiguous field
298 else
299 Error_Msg_N
300 ("attempt to specify non-contiguous field "
301 & "not permitted", CLC);
302 Error_Msg_N
303 ("\caused by non-standard Bit_Order "
304 & "specified", CLC);
305 Error_Msg_N
306 ("\consider possibility of using "
307 & "Ada 2005 mode here", CLC);
308 end if;
310 -- Case where field fits in one storage unit
312 else
313 -- Give warning if suspicious component clause
315 if Intval (FB) >= System_Storage_Unit
316 and then Warn_On_Reverse_Bit_Order
317 then
318 Error_Msg_N
319 ("?Bit_Order clause does not affect " &
320 "byte ordering", Pos);
321 Error_Msg_Uint_1 :=
322 Intval (Pos) + Intval (FB) /
323 System_Storage_Unit;
324 Error_Msg_N
325 ("?position normalized to ^ before bit " &
326 "order interpreted", Pos);
327 end if;
329 -- Here is where we fix up the Component_Bit_Offset value
330 -- to account for the reverse bit order. Some examples of
331 -- what needs to be done are:
333 -- First_Bit .. Last_Bit Component_Bit_Offset
334 -- old new old new
336 -- 0 .. 0 7 .. 7 0 7
337 -- 0 .. 1 6 .. 7 0 6
338 -- 0 .. 2 5 .. 7 0 5
339 -- 0 .. 7 0 .. 7 0 4
341 -- 1 .. 1 6 .. 6 1 6
342 -- 1 .. 4 3 .. 6 1 3
343 -- 4 .. 7 0 .. 3 4 0
345 -- The rule is that the first bit is is obtained by
346 -- subtracting the old ending bit from storage_unit - 1.
348 Set_Component_Bit_Offset
349 (Comp,
350 (Storage_Unit_Offset * System_Storage_Unit) +
351 (System_Storage_Unit - 1) -
352 (Start_Bit + CSZ - 1));
354 Set_Normalized_First_Bit
355 (Comp,
356 Component_Bit_Offset (Comp) mod
357 System_Storage_Unit);
358 end if;
359 end;
360 end if;
362 Next_Component_Or_Discriminant (Comp);
363 end loop;
365 -- For Ada 2005, we do machine scalar processing, as fully described In
366 -- AI-133. This involves gathering all components which start at the
367 -- same byte offset and processing them together. Same approach is still
368 -- valid in later versions including Ada 2012.
370 else
371 declare
372 Max_Machine_Scalar_Size : constant Uint :=
373 UI_From_Int
374 (Standard_Long_Long_Integer_Size);
375 -- We use this as the maximum machine scalar size
377 Num_CC : Natural;
378 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
380 begin
381 -- This first loop through components does two things. First it
382 -- deals with the case of components with component clauses whose
383 -- length is greater than the maximum machine scalar size (either
384 -- accepting them or rejecting as needed). Second, it counts the
385 -- number of components with component clauses whose length does
386 -- not exceed this maximum for later processing.
388 Num_CC := 0;
389 Comp := First_Component_Or_Discriminant (R);
390 while Present (Comp) loop
391 CC := Component_Clause (Comp);
393 if Present (CC) then
394 declare
395 Fbit : constant Uint :=
396 Static_Integer (First_Bit (CC));
397 Lbit : constant Uint :=
398 Static_Integer (Last_Bit (CC));
400 begin
401 -- Case of component with last bit >= max machine scalar
403 if Lbit >= Max_Machine_Scalar_Size then
405 -- This is allowed only if first bit is zero, and
406 -- last bit + 1 is a multiple of storage unit size.
408 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
410 -- This is the case to give a warning if enabled
412 if Warn_On_Reverse_Bit_Order then
413 Error_Msg_N
414 ("multi-byte field specified with "
415 & " non-standard Bit_Order?", CC);
417 if Bytes_Big_Endian then
418 Error_Msg_N
419 ("\bytes are not reversed "
420 & "(component is big-endian)?", CC);
421 else
422 Error_Msg_N
423 ("\bytes are not reversed "
424 & "(component is little-endian)?", CC);
425 end if;
426 end if;
428 -- Give error message for RM 13.5.1(10) violation
430 else
431 Error_Msg_FE
432 ("machine scalar rules not followed for&",
433 First_Bit (CC), Comp);
435 Error_Msg_Uint_1 := Lbit;
436 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
437 Error_Msg_F
438 ("\last bit (^) exceeds maximum machine "
439 & "scalar size (^)",
440 First_Bit (CC));
442 if (Lbit + 1) mod SSU /= 0 then
443 Error_Msg_Uint_1 := SSU;
444 Error_Msg_F
445 ("\and is not a multiple of Storage_Unit (^) "
446 & "(RM 13.4.1(10))",
447 First_Bit (CC));
449 else
450 Error_Msg_Uint_1 := Fbit;
451 Error_Msg_F
452 ("\and first bit (^) is non-zero "
453 & "(RM 13.4.1(10))",
454 First_Bit (CC));
455 end if;
456 end if;
458 -- OK case of machine scalar related component clause,
459 -- For now, just count them.
461 else
462 Num_CC := Num_CC + 1;
463 end if;
464 end;
465 end if;
467 Next_Component_Or_Discriminant (Comp);
468 end loop;
470 -- We need to sort the component clauses on the basis of the
471 -- Position values in the clause, so we can group clauses with
472 -- the same Position. together to determine the relevant machine
473 -- scalar size.
475 Sort_CC : declare
476 Comps : array (0 .. Num_CC) of Entity_Id;
477 -- Array to collect component and discriminant entities. The
478 -- data starts at index 1, the 0'th entry is for the sort
479 -- routine.
481 function CP_Lt (Op1, Op2 : Natural) return Boolean;
482 -- Compare routine for Sort
484 procedure CP_Move (From : Natural; To : Natural);
485 -- Move routine for Sort
487 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
489 Start : Natural;
490 Stop : Natural;
491 -- Start and stop positions in the component list of the set of
492 -- components with the same starting position (that constitute
493 -- components in a single machine scalar).
495 MaxL : Uint;
496 -- Maximum last bit value of any component in this set
498 MSS : Uint;
499 -- Corresponding machine scalar size
501 -----------
502 -- CP_Lt --
503 -----------
505 function CP_Lt (Op1, Op2 : Natural) return Boolean is
506 begin
507 return Position (Component_Clause (Comps (Op1))) <
508 Position (Component_Clause (Comps (Op2)));
509 end CP_Lt;
511 -------------
512 -- CP_Move --
513 -------------
515 procedure CP_Move (From : Natural; To : Natural) is
516 begin
517 Comps (To) := Comps (From);
518 end CP_Move;
520 -- Start of processing for Sort_CC
522 begin
523 -- Collect the machine scalar relevant component clauses
525 Num_CC := 0;
526 Comp := First_Component_Or_Discriminant (R);
527 while Present (Comp) loop
528 declare
529 CC : constant Node_Id := Component_Clause (Comp);
531 begin
532 -- Collect only component clauses whose last bit is less
533 -- than machine scalar size. Any component clause whose
534 -- last bit exceeds this value does not take part in
535 -- machine scalar layout considerations. The test for
536 -- Error_Posted makes sure we exclude component clauses
537 -- for which we already posted an error.
539 if Present (CC)
540 and then not Error_Posted (Last_Bit (CC))
541 and then Static_Integer (Last_Bit (CC)) <
542 Max_Machine_Scalar_Size
543 then
544 Num_CC := Num_CC + 1;
545 Comps (Num_CC) := Comp;
546 end if;
547 end;
549 Next_Component_Or_Discriminant (Comp);
550 end loop;
552 -- Sort by ascending position number
554 Sorting.Sort (Num_CC);
556 -- We now have all the components whose size does not exceed
557 -- the max machine scalar value, sorted by starting position.
558 -- In this loop we gather groups of clauses starting at the
559 -- same position, to process them in accordance with AI-133.
561 Stop := 0;
562 while Stop < Num_CC loop
563 Start := Stop + 1;
564 Stop := Start;
565 MaxL :=
566 Static_Integer
567 (Last_Bit (Component_Clause (Comps (Start))));
568 while Stop < Num_CC loop
569 if Static_Integer
570 (Position (Component_Clause (Comps (Stop + 1)))) =
571 Static_Integer
572 (Position (Component_Clause (Comps (Stop))))
573 then
574 Stop := Stop + 1;
575 MaxL :=
576 UI_Max
577 (MaxL,
578 Static_Integer
579 (Last_Bit
580 (Component_Clause (Comps (Stop)))));
581 else
582 exit;
583 end if;
584 end loop;
586 -- Now we have a group of component clauses from Start to
587 -- Stop whose positions are identical, and MaxL is the
588 -- maximum last bit value of any of these components.
590 -- We need to determine the corresponding machine scalar
591 -- size. This loop assumes that machine scalar sizes are
592 -- even, and that each possible machine scalar has twice
593 -- as many bits as the next smaller one.
595 MSS := Max_Machine_Scalar_Size;
596 while MSS mod 2 = 0
597 and then (MSS / 2) >= SSU
598 and then (MSS / 2) > MaxL
599 loop
600 MSS := MSS / 2;
601 end loop;
603 -- Here is where we fix up the Component_Bit_Offset value
604 -- to account for the reverse bit order. Some examples of
605 -- what needs to be done for the case of a machine scalar
606 -- size of 8 are:
608 -- First_Bit .. Last_Bit Component_Bit_Offset
609 -- old new old new
611 -- 0 .. 0 7 .. 7 0 7
612 -- 0 .. 1 6 .. 7 0 6
613 -- 0 .. 2 5 .. 7 0 5
614 -- 0 .. 7 0 .. 7 0 4
616 -- 1 .. 1 6 .. 6 1 6
617 -- 1 .. 4 3 .. 6 1 3
618 -- 4 .. 7 0 .. 3 4 0
620 -- The rule is that the first bit is obtained by subtracting
621 -- the old ending bit from machine scalar size - 1.
623 for C in Start .. Stop loop
624 declare
625 Comp : constant Entity_Id := Comps (C);
626 CC : constant Node_Id :=
627 Component_Clause (Comp);
628 LB : constant Uint :=
629 Static_Integer (Last_Bit (CC));
630 NFB : constant Uint := MSS - Uint_1 - LB;
631 NLB : constant Uint := NFB + Esize (Comp) - 1;
632 Pos : constant Uint :=
633 Static_Integer (Position (CC));
635 begin
636 if Warn_On_Reverse_Bit_Order then
637 Error_Msg_Uint_1 := MSS;
638 Error_Msg_N
639 ("info: reverse bit order in machine " &
640 "scalar of length^?", First_Bit (CC));
641 Error_Msg_Uint_1 := NFB;
642 Error_Msg_Uint_2 := NLB;
644 if Bytes_Big_Endian then
645 Error_Msg_NE
646 ("?\info: big-endian range for "
647 & "component & is ^ .. ^",
648 First_Bit (CC), Comp);
649 else
650 Error_Msg_NE
651 ("?\info: little-endian range "
652 & "for component & is ^ .. ^",
653 First_Bit (CC), Comp);
654 end if;
655 end if;
657 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
658 Set_Normalized_First_Bit (Comp, NFB mod SSU);
659 end;
660 end loop;
661 end loop;
662 end Sort_CC;
663 end;
664 end if;
665 end Adjust_Record_For_Reverse_Bit_Order;
667 -------------------------------------
668 -- Alignment_Check_For_Size_Change --
669 -------------------------------------
671 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
672 begin
673 -- If the alignment is known, and not set by a rep clause, and is
674 -- inconsistent with the size being set, then reset it to unknown,
675 -- we assume in this case that the size overrides the inherited
676 -- alignment, and that the alignment must be recomputed.
678 if Known_Alignment (Typ)
679 and then not Has_Alignment_Clause (Typ)
680 and then Size mod (Alignment (Typ) * SSU) /= 0
681 then
682 Init_Alignment (Typ);
683 end if;
684 end Alignment_Check_For_Size_Change;
686 -------------------------------------
687 -- Analyze_Aspects_At_Freeze_Point --
688 -------------------------------------
690 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
691 ASN : Node_Id;
692 A_Id : Aspect_Id;
693 Ritem : Node_Id;
695 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
696 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
697 -- the aspect specification node ASN.
699 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
700 -- Given an aspect specification node ASN whose expression is an
701 -- optional Boolean, this routines creates the corresponding pragma
702 -- at the freezing point.
704 ----------------------------------
705 -- Analyze_Aspect_Default_Value --
706 ----------------------------------
708 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
709 Ent : constant Entity_Id := Entity (ASN);
710 Expr : constant Node_Id := Expression (ASN);
711 Id : constant Node_Id := Identifier (ASN);
713 begin
714 Error_Msg_Name_1 := Chars (Id);
716 if not Is_Type (Ent) then
717 Error_Msg_N ("aspect% can only apply to a type", Id);
718 return;
720 elsif not Is_First_Subtype (Ent) then
721 Error_Msg_N ("aspect% cannot apply to subtype", Id);
722 return;
724 elsif A_Id = Aspect_Default_Value
725 and then not Is_Scalar_Type (Ent)
726 then
727 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
728 return;
730 elsif A_Id = Aspect_Default_Component_Value then
731 if not Is_Array_Type (Ent) then
732 Error_Msg_N ("aspect% can only be applied to array type", Id);
733 return;
735 elsif not Is_Scalar_Type (Component_Type (Ent)) then
736 Error_Msg_N ("aspect% requires scalar components", Id);
737 return;
738 end if;
739 end if;
741 Set_Has_Default_Aspect (Base_Type (Ent));
743 if Is_Scalar_Type (Ent) then
744 Set_Default_Aspect_Value (Ent, Expr);
745 else
746 Set_Default_Aspect_Component_Value (Ent, Expr);
747 end if;
748 end Analyze_Aspect_Default_Value;
750 -------------------------------------
751 -- Make_Pragma_From_Boolean_Aspect --
752 -------------------------------------
754 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
755 Ident : constant Node_Id := Identifier (ASN);
756 A_Name : constant Name_Id := Chars (Ident);
757 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
758 Ent : constant Entity_Id := Entity (ASN);
759 Expr : constant Node_Id := Expression (ASN);
760 Loc : constant Source_Ptr := Sloc (ASN);
762 Prag : Node_Id;
764 procedure Check_False_Aspect_For_Derived_Type;
765 -- This procedure checks for the case of a false aspect for a derived
766 -- type, which improperly tries to cancel an aspect inherited from
767 -- the parent.
769 -----------------------------------------
770 -- Check_False_Aspect_For_Derived_Type --
771 -----------------------------------------
773 procedure Check_False_Aspect_For_Derived_Type is
774 Par : Node_Id;
776 begin
777 -- We are only checking derived types
779 if not Is_Derived_Type (E) then
780 return;
781 end if;
783 Par := Nearest_Ancestor (E);
785 case A_Id is
786 when Aspect_Atomic | Aspect_Shared =>
787 if not Is_Atomic (Par) then
788 return;
789 end if;
791 when Aspect_Atomic_Components =>
792 if not Has_Atomic_Components (Par) then
793 return;
794 end if;
796 when Aspect_Discard_Names =>
797 if not Discard_Names (Par) then
798 return;
799 end if;
801 when Aspect_Pack =>
802 if not Is_Packed (Par) then
803 return;
804 end if;
806 when Aspect_Unchecked_Union =>
807 if not Is_Unchecked_Union (Par) then
808 return;
809 end if;
811 when Aspect_Volatile =>
812 if not Is_Volatile (Par) then
813 return;
814 end if;
816 when Aspect_Volatile_Components =>
817 if not Has_Volatile_Components (Par) then
818 return;
819 end if;
821 when others =>
822 return;
823 end case;
825 -- Fall through means we are canceling an inherited aspect
827 Error_Msg_Name_1 := A_Name;
828 Error_Msg_NE ("derived type& inherits aspect%, cannot cancel",
829 Expr,
832 end Check_False_Aspect_For_Derived_Type;
834 -- Start of processing for Make_Pragma_From_Boolean_Aspect
836 begin
837 if Is_False (Static_Boolean (Expr)) then
838 Check_False_Aspect_For_Derived_Type;
840 else
841 Prag :=
842 Make_Pragma (Loc,
843 Pragma_Argument_Associations => New_List (
844 New_Occurrence_Of (Ent, Sloc (Ident))),
845 Pragma_Identifier =>
846 Make_Identifier (Sloc (Ident), Chars (Ident)));
848 Set_From_Aspect_Specification (Prag, True);
849 Set_Corresponding_Aspect (Prag, ASN);
850 Set_Aspect_Rep_Item (ASN, Prag);
851 Set_Is_Delayed_Aspect (Prag);
852 Set_Parent (Prag, ASN);
853 end if;
854 end Make_Pragma_From_Boolean_Aspect;
856 -- Start of processing for Analyze_Aspects_At_Freeze_Point
858 begin
859 -- Must be visible in current scope.
861 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
862 return;
863 end if;
865 -- Look for aspect specification entries for this entity
867 ASN := First_Rep_Item (E);
868 while Present (ASN) loop
869 if Nkind (ASN) = N_Aspect_Specification
870 and then Entity (ASN) = E
871 and then Is_Delayed_Aspect (ASN)
872 then
873 A_Id := Get_Aspect_Id (Chars (Identifier (ASN)));
875 case A_Id is
877 -- For aspects whose expression is an optional Boolean, make
878 -- the corresponding pragma at the freezing point.
880 when Boolean_Aspects |
881 Library_Unit_Aspects =>
882 Make_Pragma_From_Boolean_Aspect (ASN);
884 -- Special handling for aspects that don't correspond to
885 -- pragmas/attributes.
887 when Aspect_Default_Value |
888 Aspect_Default_Component_Value =>
889 Analyze_Aspect_Default_Value (ASN);
891 when others =>
892 null;
893 end case;
895 Ritem := Aspect_Rep_Item (ASN);
897 if Present (Ritem) then
898 Analyze (Ritem);
899 end if;
900 end if;
902 Next_Rep_Item (ASN);
903 end loop;
904 end Analyze_Aspects_At_Freeze_Point;
906 -----------------------------------
907 -- Analyze_Aspect_Specifications --
908 -----------------------------------
910 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
911 Aspect : Node_Id;
912 Aitem : Node_Id;
913 Ent : Node_Id;
915 L : constant List_Id := Aspect_Specifications (N);
917 Ins_Node : Node_Id := N;
918 -- Insert pragmas/attribute definition clause after this node when no
919 -- delayed analysis is required.
921 -- The general processing involves building an attribute definition
922 -- clause or a pragma node that corresponds to the aspect. Then in order
923 -- to delay the evaluation of this aspect to the freeze point, we attach
924 -- the corresponding pragma/attribute definition clause to the aspect
925 -- specification node, which is then placed in the Rep Item chain. In
926 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
927 -- and we evaluate the rep item at the freeze point. When the aspect
928 -- doesn't have a corresponding pragma/attribute definition clause, then
929 -- its analysis is simply delayed at the freeze point.
931 -- Some special cases don't require delay analysis, thus the aspect is
932 -- analyzed right now.
934 -- Note that there is a special handling for
935 -- Pre/Post/Test_Case/Contract_Case aspects. In this case, we do not
936 -- have to worry about delay issues, since the pragmas themselves deal
937 -- with delay of visibility for the expression analysis. Thus, we just
938 -- insert the pragma after the node N.
940 begin
941 pragma Assert (Present (L));
943 -- Loop through aspects
945 Aspect := First (L);
946 Aspect_Loop : while Present (Aspect) loop
947 declare
948 Expr : constant Node_Id := Expression (Aspect);
949 Id : constant Node_Id := Identifier (Aspect);
950 Loc : constant Source_Ptr := Sloc (Aspect);
951 Nam : constant Name_Id := Chars (Id);
952 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
953 Anod : Node_Id;
955 Delay_Required : Boolean := True;
956 -- Set False if delay is not required
958 Eloc : Source_Ptr := No_Location;
959 -- Source location of expression, modified when we split PPC's. It
960 -- is set below when Expr is present.
962 procedure Analyze_Aspect_External_Or_Link_Name;
963 -- This routine performs the analysis of the External_Name or
964 -- Link_Name aspects.
966 procedure Analyze_Aspect_Implicit_Dereference;
967 -- This routine performs the analysis of the Implicit_Dereference
968 -- aspects.
970 ------------------------------------------
971 -- Analyze_Aspect_External_Or_Link_Name --
972 ------------------------------------------
974 procedure Analyze_Aspect_External_Or_Link_Name is
975 begin
976 -- Verify that there is an Import/Export aspect defined for the
977 -- entity. The processing of that aspect in turn checks that
978 -- there is a Convention aspect declared. The pragma is
979 -- constructed when processing the Convention aspect.
981 declare
982 A : Node_Id;
984 begin
985 A := First (L);
986 while Present (A) loop
987 exit when Chars (Identifier (A)) = Name_Export
988 or else Chars (Identifier (A)) = Name_Import;
989 Next (A);
990 end loop;
992 if No (A) then
993 Error_Msg_N
994 ("Missing Import/Export for Link/External name",
995 Aspect);
996 end if;
997 end;
998 end Analyze_Aspect_External_Or_Link_Name;
1000 -----------------------------------------
1001 -- Analyze_Aspect_Implicit_Dereference --
1002 -----------------------------------------
1004 procedure Analyze_Aspect_Implicit_Dereference is
1005 begin
1006 if not Is_Type (E)
1007 or else not Has_Discriminants (E)
1008 then
1009 Error_Msg_N
1010 ("Aspect must apply to a type with discriminants", N);
1012 else
1013 declare
1014 Disc : Entity_Id;
1016 begin
1017 Disc := First_Discriminant (E);
1018 while Present (Disc) loop
1019 if Chars (Expr) = Chars (Disc)
1020 and then Ekind (Etype (Disc)) =
1021 E_Anonymous_Access_Type
1022 then
1023 Set_Has_Implicit_Dereference (E);
1024 Set_Has_Implicit_Dereference (Disc);
1025 return;
1026 end if;
1028 Next_Discriminant (Disc);
1029 end loop;
1031 -- Error if no proper access discriminant.
1033 Error_Msg_NE
1034 ("not an access discriminant of&", Expr, E);
1035 end;
1036 end if;
1037 end Analyze_Aspect_Implicit_Dereference;
1039 begin
1040 -- Skip aspect if already analyzed (not clear if this is needed)
1042 if Analyzed (Aspect) then
1043 goto Continue;
1044 end if;
1046 -- Set the source location of expression, used in the case of
1047 -- a failed precondition/postcondition or invariant. Note that
1048 -- the source location of the expression is not usually the best
1049 -- choice here. For example, it gets located on the last AND
1050 -- keyword in a chain of boolean expressiond AND'ed together.
1051 -- It is best to put the message on the first character of the
1052 -- assertion, which is the effect of the First_Node call here.
1054 if Present (Expr) then
1055 Eloc := Sloc (First_Node (Expr));
1056 end if;
1058 -- Check restriction No_Implementation_Aspect_Specifications
1060 if Impl_Defined_Aspects (A_Id) then
1061 Check_Restriction
1062 (No_Implementation_Aspect_Specifications, Aspect);
1063 end if;
1065 -- Check restriction No_Specification_Of_Aspect
1067 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1069 -- Analyze this aspect
1071 Set_Analyzed (Aspect);
1072 Set_Entity (Aspect, E);
1073 Ent := New_Occurrence_Of (E, Sloc (Id));
1075 -- Check for duplicate aspect. Note that the Comes_From_Source
1076 -- test allows duplicate Pre/Post's that we generate internally
1077 -- to escape being flagged here.
1079 if No_Duplicates_Allowed (A_Id) then
1080 Anod := First (L);
1081 while Anod /= Aspect loop
1082 if Same_Aspect
1083 (A_Id, Get_Aspect_Id (Chars (Identifier (Anod))))
1084 and then Comes_From_Source (Aspect)
1085 then
1086 Error_Msg_Name_1 := Nam;
1087 Error_Msg_Sloc := Sloc (Anod);
1089 -- Case of same aspect specified twice
1091 if Class_Present (Anod) = Class_Present (Aspect) then
1092 if not Class_Present (Anod) then
1093 Error_Msg_NE
1094 ("aspect% for & previously given#",
1095 Id, E);
1096 else
1097 Error_Msg_NE
1098 ("aspect `%''Class` for & previously given#",
1099 Id, E);
1100 end if;
1101 end if;
1102 end if;
1104 Next (Anod);
1105 end loop;
1106 end if;
1108 -- Check some general restrictions on language defined aspects
1110 if not Impl_Defined_Aspects (A_Id) then
1111 Error_Msg_Name_1 := Nam;
1113 -- Not allowed for renaming declarations
1115 if Nkind (N) in N_Renaming_Declaration then
1116 Error_Msg_N
1117 ("aspect % not allowed for renaming declaration",
1118 Aspect);
1119 end if;
1121 -- Not allowed for formal type declarations
1123 if Nkind (N) = N_Formal_Type_Declaration then
1124 Error_Msg_N
1125 ("aspect % not allowed for formal type declaration",
1126 Aspect);
1127 end if;
1128 end if;
1130 -- Copy expression for later processing by the procedures
1131 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1133 Set_Entity (Id, New_Copy_Tree (Expr));
1135 -- Processing based on specific aspect
1137 case A_Id is
1139 -- No_Aspect should be impossible
1141 when No_Aspect =>
1142 raise Program_Error;
1144 -- Case 1: Aspects corresponding to attribute definition
1145 -- clauses.
1147 when Aspect_Address |
1148 Aspect_Alignment |
1149 Aspect_Bit_Order |
1150 Aspect_Component_Size |
1151 Aspect_Constant_Indexing |
1152 Aspect_Default_Iterator |
1153 Aspect_Dispatching_Domain |
1154 Aspect_External_Tag |
1155 Aspect_Input |
1156 Aspect_Iterator_Element |
1157 Aspect_Machine_Radix |
1158 Aspect_Object_Size |
1159 Aspect_Output |
1160 Aspect_Read |
1161 Aspect_Scalar_Storage_Order |
1162 Aspect_Size |
1163 Aspect_Small |
1164 Aspect_Simple_Storage_Pool |
1165 Aspect_Storage_Pool |
1166 Aspect_Storage_Size |
1167 Aspect_Stream_Size |
1168 Aspect_Value_Size |
1169 Aspect_Variable_Indexing |
1170 Aspect_Write =>
1172 -- Indexing aspects apply only to tagged type
1174 if (A_Id = Aspect_Constant_Indexing
1175 or else A_Id = Aspect_Variable_Indexing)
1176 and then not (Is_Type (E)
1177 and then Is_Tagged_Type (E))
1178 then
1179 Error_Msg_N ("indexing applies to a tagged type", N);
1180 goto Continue;
1181 end if;
1183 -- Construct the attribute definition clause
1185 Aitem :=
1186 Make_Attribute_Definition_Clause (Loc,
1187 Name => Ent,
1188 Chars => Chars (Id),
1189 Expression => Relocate_Node (Expr));
1191 -- Case 2: Aspects cooresponding to pragmas
1193 -- Case 2a: Aspects corresponding to pragmas with two
1194 -- arguments, where the first argument is a local name
1195 -- referring to the entity, and the second argument is the
1196 -- aspect definition expression.
1198 when Aspect_Suppress |
1199 Aspect_Unsuppress =>
1201 -- Construct the pragma
1203 Aitem :=
1204 Make_Pragma (Loc,
1205 Pragma_Argument_Associations => New_List (
1206 New_Occurrence_Of (E, Loc),
1207 Relocate_Node (Expr)),
1208 Pragma_Identifier =>
1209 Make_Identifier (Sloc (Id), Chars (Id)));
1211 when Aspect_Synchronization =>
1213 -- The aspect corresponds to pragma Implemented.
1214 -- Construct the pragma
1216 Aitem :=
1217 Make_Pragma (Loc,
1218 Pragma_Argument_Associations => New_List (
1219 New_Occurrence_Of (E, Loc),
1220 Relocate_Node (Expr)),
1221 Pragma_Identifier =>
1222 Make_Identifier (Sloc (Id), Name_Implemented));
1224 -- No delay is required since the only values are: By_Entry
1225 -- | By_Protected_Procedure | By_Any | Optional which don't
1226 -- get analyzed anyway.
1228 Delay_Required := False;
1230 when Aspect_Attach_Handler =>
1231 Aitem :=
1232 Make_Pragma (Loc,
1233 Pragma_Identifier =>
1234 Make_Identifier (Sloc (Id), Name_Attach_Handler),
1235 Pragma_Argument_Associations =>
1236 New_List (Ent, Relocate_Node (Expr)));
1238 when Aspect_Dynamic_Predicate |
1239 Aspect_Predicate |
1240 Aspect_Static_Predicate =>
1242 -- Construct the pragma (always a pragma Predicate, with
1243 -- flags recording whether it is static/dynamic).
1245 Aitem :=
1246 Make_Pragma (Loc,
1247 Pragma_Argument_Associations =>
1248 New_List (Ent, Relocate_Node (Expr)),
1249 Class_Present => Class_Present (Aspect),
1250 Pragma_Identifier =>
1251 Make_Identifier (Sloc (Id), Name_Predicate));
1253 -- If the type is private, indicate that its completion
1254 -- has a freeze node, because that is the one that will be
1255 -- visible at freeze time.
1257 Set_Has_Predicates (E);
1259 if Is_Private_Type (E)
1260 and then Present (Full_View (E))
1261 then
1262 Set_Has_Predicates (Full_View (E));
1263 Set_Has_Delayed_Aspects (Full_View (E));
1264 Ensure_Freeze_Node (Full_View (E));
1265 end if;
1267 -- Case 2b: Aspects corresponding to pragmas with two
1268 -- arguments, where the second argument is a local name
1269 -- referring to the entity, and the first argument is the
1270 -- aspect definition expression.
1272 when Aspect_Convention =>
1274 -- The aspect may be part of the specification of an import
1275 -- or export pragma. Scan the aspect list to gather the
1276 -- other components, if any. The name of the generated
1277 -- pragma is one of Convention/Import/Export.
1279 declare
1280 P_Name : Name_Id;
1281 A_Name : Name_Id;
1282 A : Node_Id;
1283 Arg_List : List_Id;
1284 Found : Boolean;
1285 L_Assoc : Node_Id;
1286 E_Assoc : Node_Id;
1288 begin
1289 P_Name := Chars (Id);
1290 Found := False;
1291 Arg_List := New_List;
1292 L_Assoc := Empty;
1293 E_Assoc := Empty;
1295 A := First (L);
1296 while Present (A) loop
1297 A_Name := Chars (Identifier (A));
1299 if A_Name = Name_Import
1300 or else A_Name = Name_Export
1301 then
1302 if Found then
1303 Error_Msg_N ("conflicting", A);
1304 else
1305 Found := True;
1306 end if;
1308 P_Name := A_Name;
1310 elsif A_Name = Name_Link_Name then
1311 L_Assoc := Make_Pragma_Argument_Association (Loc,
1312 Chars => A_Name,
1313 Expression => Relocate_Node (Expression (A)));
1315 elsif A_Name = Name_External_Name then
1316 E_Assoc := Make_Pragma_Argument_Association (Loc,
1317 Chars => A_Name,
1318 Expression => Relocate_Node (Expression (A)));
1319 end if;
1321 Next (A);
1322 end loop;
1324 Arg_List := New_List (Relocate_Node (Expr), Ent);
1325 if Present (L_Assoc) then
1326 Append_To (Arg_List, L_Assoc);
1327 end if;
1329 if Present (E_Assoc) then
1330 Append_To (Arg_List, E_Assoc);
1331 end if;
1333 Aitem :=
1334 Make_Pragma (Loc,
1335 Pragma_Argument_Associations => Arg_List,
1336 Pragma_Identifier =>
1337 Make_Identifier (Loc, P_Name));
1338 end;
1340 -- The following three aspects can be specified for a
1341 -- subprogram body, in which case we generate pragmas for them
1342 -- and insert them ahead of local declarations, rather than
1343 -- after the body.
1345 when Aspect_CPU |
1346 Aspect_Interrupt_Priority |
1347 Aspect_Priority =>
1348 if Nkind (N) = N_Subprogram_Body then
1349 Aitem :=
1350 Make_Pragma (Loc,
1351 Pragma_Argument_Associations =>
1352 New_List (Relocate_Node (Expr)),
1353 Pragma_Identifier =>
1354 Make_Identifier (Sloc (Id), Chars (Id)));
1355 else
1356 Aitem :=
1357 Make_Attribute_Definition_Clause (Loc,
1358 Name => Ent,
1359 Chars => Chars (Id),
1360 Expression => Relocate_Node (Expr));
1361 end if;
1363 when Aspect_Warnings =>
1365 -- Construct the pragma
1367 Aitem :=
1368 Make_Pragma (Loc,
1369 Pragma_Argument_Associations => New_List (
1370 Relocate_Node (Expr),
1371 New_Occurrence_Of (E, Loc)),
1372 Pragma_Identifier =>
1373 Make_Identifier (Sloc (Id), Chars (Id)),
1374 Class_Present => Class_Present (Aspect));
1376 -- We don't have to play the delay game here, since the only
1377 -- values are ON/OFF which don't get analyzed anyway.
1379 Delay_Required := False;
1381 -- Case 2c: Aspects corresponding to pragmas with three
1382 -- arguments.
1384 -- Invariant aspects have a first argument that references the
1385 -- entity, a second argument that is the expression and a third
1386 -- argument that is an appropriate message.
1388 when Aspect_Invariant |
1389 Aspect_Type_Invariant =>
1391 -- Analysis of the pragma will verify placement legality:
1392 -- an invariant must apply to a private type, or appear in
1393 -- the private part of a spec and apply to a completion.
1395 -- Construct the pragma
1397 Aitem :=
1398 Make_Pragma (Loc,
1399 Pragma_Argument_Associations =>
1400 New_List (Ent, Relocate_Node (Expr)),
1401 Class_Present => Class_Present (Aspect),
1402 Pragma_Identifier =>
1403 Make_Identifier (Sloc (Id), Name_Invariant));
1405 -- Add message unless exception messages are suppressed
1407 if not Opt.Exception_Locations_Suppressed then
1408 Append_To (Pragma_Argument_Associations (Aitem),
1409 Make_Pragma_Argument_Association (Eloc,
1410 Chars => Name_Message,
1411 Expression =>
1412 Make_String_Literal (Eloc,
1413 Strval => "failed invariant from "
1414 & Build_Location_String (Eloc))));
1415 end if;
1417 -- For Invariant case, insert immediately after the entity
1418 -- declaration. We do not have to worry about delay issues
1419 -- since the pragma processing takes care of this.
1421 Delay_Required := False;
1423 -- Case 3 : Aspects that don't correspond to pragma/attribute
1424 -- definition clause.
1426 -- Case 3a: The aspects listed below don't correspond to
1427 -- pragmas/attributes but do require delayed analysis.
1429 when Aspect_Default_Value |
1430 Aspect_Default_Component_Value =>
1431 Aitem := Empty;
1433 -- Case 3b: The aspects listed below don't correspond to
1434 -- pragmas/attributes and don't need delayed analysis.
1436 -- For Implicit_Dereference, External_Name and Link_Name, only
1437 -- the legality checks are done during the analysis, thus no
1438 -- delay is required.
1440 when Aspect_Implicit_Dereference =>
1441 Analyze_Aspect_Implicit_Dereference;
1442 goto Continue;
1444 when Aspect_External_Name |
1445 Aspect_Link_Name =>
1446 Analyze_Aspect_External_Or_Link_Name;
1447 goto Continue;
1449 when Aspect_Dimension =>
1450 Analyze_Aspect_Dimension (N, Id, Expr);
1451 goto Continue;
1453 when Aspect_Dimension_System =>
1454 Analyze_Aspect_Dimension_System (N, Id, Expr);
1455 goto Continue;
1457 -- Case 4: Special handling for aspects
1458 -- Pre/Post/Test_Case/Contract_Case whose corresponding pragmas
1459 -- take care of the delay.
1461 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1462 -- with a first argument that is the expression, and a second
1463 -- argument that is an informative message if the test fails.
1464 -- This is inserted right after the declaration, to get the
1465 -- required pragma placement. The processing for the pragmas
1466 -- takes care of the required delay.
1468 when Pre_Post_Aspects => declare
1469 Pname : Name_Id;
1471 begin
1472 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1473 Pname := Name_Precondition;
1474 else
1475 Pname := Name_Postcondition;
1476 end if;
1478 -- If the expressions is of the form A and then B, then
1479 -- we generate separate Pre/Post aspects for the separate
1480 -- clauses. Since we allow multiple pragmas, there is no
1481 -- problem in allowing multiple Pre/Post aspects internally.
1482 -- These should be treated in reverse order (B first and
1483 -- A second) since they are later inserted just after N in
1484 -- the order they are treated. This way, the pragma for A
1485 -- ends up preceding the pragma for B, which may have an
1486 -- importance for the error raised (either constraint error
1487 -- or precondition error).
1489 -- We do not do this for Pre'Class, since we have to put
1490 -- these conditions together in a complex OR expression
1492 -- We do not do this in ASIS mode, as ASIS relies on the
1493 -- original node representing the complete expression, when
1494 -- retrieving it through the source aspect table.
1496 if not ASIS_Mode
1497 and then (Pname = Name_Postcondition
1498 or else not Class_Present (Aspect))
1499 then
1500 while Nkind (Expr) = N_And_Then loop
1501 Insert_After (Aspect,
1502 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1503 Identifier => Identifier (Aspect),
1504 Expression => Relocate_Node (Left_Opnd (Expr)),
1505 Class_Present => Class_Present (Aspect),
1506 Split_PPC => True));
1507 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1508 Eloc := Sloc (Expr);
1509 end loop;
1510 end if;
1512 -- Build the precondition/postcondition pragma
1514 Aitem :=
1515 Make_Pragma (Loc,
1516 Pragma_Identifier =>
1517 Make_Identifier (Sloc (Id), Pname),
1518 Class_Present => Class_Present (Aspect),
1519 Split_PPC => Split_PPC (Aspect),
1520 Pragma_Argument_Associations => New_List (
1521 Make_Pragma_Argument_Association (Eloc,
1522 Chars => Name_Check,
1523 Expression => Relocate_Node (Expr))));
1525 -- Add message unless exception messages are suppressed
1527 if not Opt.Exception_Locations_Suppressed then
1528 Append_To (Pragma_Argument_Associations (Aitem),
1529 Make_Pragma_Argument_Association (Eloc,
1530 Chars => Name_Message,
1531 Expression =>
1532 Make_String_Literal (Eloc,
1533 Strval => "failed "
1534 & Get_Name_String (Pname)
1535 & " from "
1536 & Build_Location_String (Eloc))));
1537 end if;
1539 Set_From_Aspect_Specification (Aitem, True);
1540 Set_Corresponding_Aspect (Aitem, Aspect);
1541 Set_Is_Delayed_Aspect (Aspect);
1543 -- For Pre/Post cases, insert immediately after the entity
1544 -- declaration, since that is the required pragma placement.
1545 -- Note that for these aspects, we do not have to worry
1546 -- about delay issues, since the pragmas themselves deal
1547 -- with delay of visibility for the expression analysis.
1549 -- If the entity is a library-level subprogram, the pre/
1550 -- postconditions must be treated as late pragmas.
1552 if Nkind (Parent (N)) = N_Compilation_Unit then
1553 Add_Global_Declaration (Aitem);
1554 else
1555 Insert_After (N, Aitem);
1556 end if;
1558 goto Continue;
1559 end;
1561 when Aspect_Contract_Case |
1562 Aspect_Test_Case =>
1563 declare
1564 Args : List_Id;
1565 Comp_Expr : Node_Id;
1566 Comp_Assn : Node_Id;
1567 New_Expr : Node_Id;
1569 begin
1570 Args := New_List;
1572 if Nkind (Parent (N)) = N_Compilation_Unit then
1573 Error_Msg_Name_1 := Nam;
1574 Error_Msg_N ("incorrect placement of aspect `%`", E);
1575 goto Continue;
1576 end if;
1578 if Nkind (Expr) /= N_Aggregate then
1579 Error_Msg_Name_1 := Nam;
1580 Error_Msg_NE
1581 ("wrong syntax for aspect `%` for &", Id, E);
1582 goto Continue;
1583 end if;
1585 -- Make pragma expressions refer to the original aspect
1586 -- expressions through the Original_Node link. This is
1587 -- used in semantic analysis for ASIS mode, so that the
1588 -- original expression also gets analyzed.
1590 Comp_Expr := First (Expressions (Expr));
1591 while Present (Comp_Expr) loop
1592 New_Expr := Relocate_Node (Comp_Expr);
1593 Set_Original_Node (New_Expr, Comp_Expr);
1594 Append
1595 (Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1596 Expression => New_Expr),
1597 Args);
1598 Next (Comp_Expr);
1599 end loop;
1601 Comp_Assn := First (Component_Associations (Expr));
1602 while Present (Comp_Assn) loop
1603 if List_Length (Choices (Comp_Assn)) /= 1
1604 or else
1605 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1606 then
1607 Error_Msg_Name_1 := Nam;
1608 Error_Msg_NE
1609 ("wrong syntax for aspect `%` for &", Id, E);
1610 goto Continue;
1611 end if;
1613 New_Expr := Relocate_Node (Expression (Comp_Assn));
1614 Set_Original_Node (New_Expr, Expression (Comp_Assn));
1615 Append (Make_Pragma_Argument_Association (
1616 Sloc => Sloc (Comp_Assn),
1617 Chars => Chars (First (Choices (Comp_Assn))),
1618 Expression => New_Expr),
1619 Args);
1620 Next (Comp_Assn);
1621 end loop;
1623 -- Build the contract-case or test-case pragma
1625 Aitem :=
1626 Make_Pragma (Loc,
1627 Pragma_Identifier =>
1628 Make_Identifier (Sloc (Id), Nam),
1629 Pragma_Argument_Associations =>
1630 Args);
1632 Delay_Required := False;
1633 end;
1635 -- Case 5: Special handling for aspects with an optional
1636 -- boolean argument.
1638 -- In the general case, the corresponding pragma cannot be
1639 -- generated yet because the evaluation of the boolean needs to
1640 -- be delayed til the freeze point.
1642 when Boolean_Aspects |
1643 Library_Unit_Aspects =>
1645 Set_Is_Boolean_Aspect (Aspect);
1647 -- Lock_Free aspect only apply to protected objects
1649 if A_Id = Aspect_Lock_Free then
1650 if Ekind (E) /= E_Protected_Type then
1651 Error_Msg_Name_1 := Nam;
1652 Error_Msg_N
1653 ("aspect % only applies to a protected object",
1654 Aspect);
1656 else
1657 -- Set the Uses_Lock_Free flag to True if there is no
1658 -- expression or if the expression is True. ??? The
1659 -- evaluation of this aspect should be delayed to the
1660 -- freeze point.
1662 if No (Expr)
1663 or else Is_True (Static_Boolean (Expr))
1664 then
1665 Set_Uses_Lock_Free (E);
1666 end if;
1668 Record_Rep_Item (E, Aspect);
1669 end if;
1671 goto Continue;
1673 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
1675 -- Verify that there is an aspect Convention that will
1676 -- incorporate the Import/Export aspect, and eventual
1677 -- Link/External names.
1679 declare
1680 A : Node_Id;
1682 begin
1683 A := First (L);
1684 while Present (A) loop
1685 exit when Chars (Identifier (A)) = Name_Convention;
1686 Next (A);
1687 end loop;
1689 if No (A) then
1690 Error_Msg_N
1691 ("missing Convention aspect for Export/Import",
1692 Aspect);
1693 end if;
1694 end;
1696 goto Continue;
1697 end if;
1699 -- This requires special handling in the case of a package
1700 -- declaration, the pragma needs to be inserted in the list
1701 -- of declarations for the associated package. There is no
1702 -- issue of visibility delay for these aspects.
1704 if A_Id in Library_Unit_Aspects
1705 and then Nkind (N) = N_Package_Declaration
1706 and then Nkind (Parent (N)) /= N_Compilation_Unit
1707 then
1708 Error_Msg_N
1709 ("incorrect context for library unit aspect&", Id);
1710 goto Continue;
1711 end if;
1713 -- Special handling when the aspect has no expression. In
1714 -- this case the value is considered to be True. Thus, we
1715 -- simply insert the pragma, no delay is required.
1717 if No (Expr) then
1718 Aitem :=
1719 Make_Pragma (Loc,
1720 Pragma_Argument_Associations => New_List (Ent),
1721 Pragma_Identifier =>
1722 Make_Identifier (Sloc (Id), Chars (Id)));
1724 Delay_Required := False;
1726 -- In general cases, the corresponding pragma/attribute
1727 -- definition clause will be inserted later at the freezing
1728 -- point.
1730 else
1731 Aitem := Empty;
1732 end if;
1733 end case;
1735 -- Attach the corresponding pragma/attribute definition clause to
1736 -- the aspect specification node.
1738 if Present (Aitem) then
1739 Set_From_Aspect_Specification (Aitem, True);
1741 if Nkind (Aitem) = N_Pragma then
1742 Set_Corresponding_Aspect (Aitem, Aspect);
1743 end if;
1744 end if;
1746 -- In the context of a compilation unit, we directly put the
1747 -- pragma in the Pragmas_After list of the
1748 -- N_Compilation_Unit_Aux node (No delay is required here)
1749 -- except for aspects on a subprogram body (see below).
1751 if Nkind (Parent (N)) = N_Compilation_Unit
1752 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
1753 then
1754 declare
1755 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
1757 begin
1758 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
1760 -- For a Boolean aspect, create the corresponding pragma if
1761 -- no expression or if the value is True.
1763 if Is_Boolean_Aspect (Aspect)
1764 and then No (Aitem)
1765 then
1766 if Is_True (Static_Boolean (Expr)) then
1767 Aitem :=
1768 Make_Pragma (Loc,
1769 Pragma_Argument_Associations => New_List (Ent),
1770 Pragma_Identifier =>
1771 Make_Identifier (Sloc (Id), Chars (Id)));
1773 Set_From_Aspect_Specification (Aitem, True);
1774 Set_Corresponding_Aspect (Aitem, Aspect);
1776 else
1777 goto Continue;
1778 end if;
1779 end if;
1781 -- If the aspect is on a subprogram body (relevant aspects
1782 -- are Inline and Priority), add the pragma in front of
1783 -- the declarations.
1785 if Nkind (N) = N_Subprogram_Body then
1786 if No (Declarations (N)) then
1787 Set_Declarations (N, New_List);
1788 end if;
1790 Prepend (Aitem, Declarations (N));
1792 else
1793 if No (Pragmas_After (Aux)) then
1794 Set_Pragmas_After (Aux, Empty_List);
1795 end if;
1797 Append (Aitem, Pragmas_After (Aux));
1798 end if;
1800 goto Continue;
1801 end;
1802 end if;
1804 -- The evaluation of the aspect is delayed to the freezing point.
1805 -- The pragma or attribute clause if there is one is then attached
1806 -- to the aspect specification which is placed in the rep item
1807 -- list.
1809 if Delay_Required then
1810 if Present (Aitem) then
1811 Set_Is_Delayed_Aspect (Aitem);
1812 Set_Aspect_Rep_Item (Aspect, Aitem);
1813 Set_Parent (Aitem, Aspect);
1814 end if;
1816 Set_Is_Delayed_Aspect (Aspect);
1817 Set_Has_Delayed_Aspects (E);
1818 Record_Rep_Item (E, Aspect);
1820 -- When delay is not required and the context is not a compilation
1821 -- unit, we simply insert the pragma/attribute definition clause
1822 -- in sequence.
1824 else
1825 Insert_After (Ins_Node, Aitem);
1826 Ins_Node := Aitem;
1827 end if;
1828 end;
1830 <<Continue>>
1831 Next (Aspect);
1832 end loop Aspect_Loop;
1834 if Has_Delayed_Aspects (E) then
1835 Ensure_Freeze_Node (E);
1836 end if;
1837 end Analyze_Aspect_Specifications;
1839 -----------------------
1840 -- Analyze_At_Clause --
1841 -----------------------
1843 -- An at clause is replaced by the corresponding Address attribute
1844 -- definition clause that is the preferred approach in Ada 95.
1846 procedure Analyze_At_Clause (N : Node_Id) is
1847 CS : constant Boolean := Comes_From_Source (N);
1849 begin
1850 -- This is an obsolescent feature
1852 Check_Restriction (No_Obsolescent_Features, N);
1854 if Warn_On_Obsolescent_Feature then
1855 Error_Msg_N
1856 ("at clause is an obsolescent feature (RM J.7(2))?", N);
1857 Error_Msg_N
1858 ("\use address attribute definition clause instead?", N);
1859 end if;
1861 -- Rewrite as address clause
1863 Rewrite (N,
1864 Make_Attribute_Definition_Clause (Sloc (N),
1865 Name => Identifier (N),
1866 Chars => Name_Address,
1867 Expression => Expression (N)));
1869 -- We preserve Comes_From_Source, since logically the clause still comes
1870 -- from the source program even though it is changed in form.
1872 Set_Comes_From_Source (N, CS);
1874 -- Analyze rewritten clause
1876 Analyze_Attribute_Definition_Clause (N);
1877 end Analyze_At_Clause;
1879 -----------------------------------------
1880 -- Analyze_Attribute_Definition_Clause --
1881 -----------------------------------------
1883 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
1884 Loc : constant Source_Ptr := Sloc (N);
1885 Nam : constant Node_Id := Name (N);
1886 Attr : constant Name_Id := Chars (N);
1887 Expr : constant Node_Id := Expression (N);
1888 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
1890 Ent : Entity_Id;
1891 -- The entity of Nam after it is analyzed. In the case of an incomplete
1892 -- type, this is the underlying type.
1894 U_Ent : Entity_Id;
1895 -- The underlying entity to which the attribute applies. Generally this
1896 -- is the Underlying_Type of Ent, except in the case where the clause
1897 -- applies to full view of incomplete type or private type in which case
1898 -- U_Ent is just a copy of Ent.
1900 FOnly : Boolean := False;
1901 -- Reset to True for subtype specific attribute (Alignment, Size)
1902 -- and for stream attributes, i.e. those cases where in the call
1903 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
1904 -- rules are checked. Note that the case of stream attributes is not
1905 -- clear from the RM, but see AI95-00137. Also, the RM seems to
1906 -- disallow Storage_Size for derived task types, but that is also
1907 -- clearly unintentional.
1909 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
1910 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
1911 -- definition clauses.
1913 function Duplicate_Clause return Boolean;
1914 -- This routine checks if the aspect for U_Ent being given by attribute
1915 -- definition clause N is for an aspect that has already been specified,
1916 -- and if so gives an error message. If there is a duplicate, True is
1917 -- returned, otherwise if there is no error, False is returned.
1919 procedure Check_Indexing_Functions;
1920 -- Check that the function in Constant_Indexing or Variable_Indexing
1921 -- attribute has the proper type structure. If the name is overloaded,
1922 -- check that all interpretations are legal.
1924 procedure Check_Iterator_Functions;
1925 -- Check that there is a single function in Default_Iterator attribute
1926 -- has the proper type structure.
1928 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
1929 -- Common legality check for the previous two
1931 -----------------------------------
1932 -- Analyze_Stream_TSS_Definition --
1933 -----------------------------------
1935 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
1936 Subp : Entity_Id := Empty;
1937 I : Interp_Index;
1938 It : Interp;
1939 Pnam : Entity_Id;
1941 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
1942 -- True for Read attribute, false for other attributes
1944 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1945 -- Return true if the entity is a subprogram with an appropriate
1946 -- profile for the attribute being defined.
1948 ----------------------
1949 -- Has_Good_Profile --
1950 ----------------------
1952 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1953 F : Entity_Id;
1954 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
1955 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
1956 (False => E_Procedure, True => E_Function);
1957 Typ : Entity_Id;
1959 begin
1960 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
1961 return False;
1962 end if;
1964 F := First_Formal (Subp);
1966 if No (F)
1967 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
1968 or else Designated_Type (Etype (F)) /=
1969 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1970 then
1971 return False;
1972 end if;
1974 if not Is_Function then
1975 Next_Formal (F);
1977 declare
1978 Expected_Mode : constant array (Boolean) of Entity_Kind :=
1979 (False => E_In_Parameter,
1980 True => E_Out_Parameter);
1981 begin
1982 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
1983 return False;
1984 end if;
1985 end;
1987 Typ := Etype (F);
1989 else
1990 Typ := Etype (Subp);
1991 end if;
1993 return Base_Type (Typ) = Base_Type (Ent)
1994 and then No (Next_Formal (F));
1995 end Has_Good_Profile;
1997 -- Start of processing for Analyze_Stream_TSS_Definition
1999 begin
2000 FOnly := True;
2002 if not Is_Type (U_Ent) then
2003 Error_Msg_N ("local name must be a subtype", Nam);
2004 return;
2005 end if;
2007 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
2009 -- If Pnam is present, it can be either inherited from an ancestor
2010 -- type (in which case it is legal to redefine it for this type), or
2011 -- be a previous definition of the attribute for the same type (in
2012 -- which case it is illegal).
2014 -- In the first case, it will have been analyzed already, and we
2015 -- can check that its profile does not match the expected profile
2016 -- for a stream attribute of U_Ent. In the second case, either Pnam
2017 -- has been analyzed (and has the expected profile), or it has not
2018 -- been analyzed yet (case of a type that has not been frozen yet
2019 -- and for which the stream attribute has been set using Set_TSS).
2021 if Present (Pnam)
2022 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
2023 then
2024 Error_Msg_Sloc := Sloc (Pnam);
2025 Error_Msg_Name_1 := Attr;
2026 Error_Msg_N ("% attribute already defined #", Nam);
2027 return;
2028 end if;
2030 Analyze (Expr);
2032 if Is_Entity_Name (Expr) then
2033 if not Is_Overloaded (Expr) then
2034 if Has_Good_Profile (Entity (Expr)) then
2035 Subp := Entity (Expr);
2036 end if;
2038 else
2039 Get_First_Interp (Expr, I, It);
2040 while Present (It.Nam) loop
2041 if Has_Good_Profile (It.Nam) then
2042 Subp := It.Nam;
2043 exit;
2044 end if;
2046 Get_Next_Interp (I, It);
2047 end loop;
2048 end if;
2049 end if;
2051 if Present (Subp) then
2052 if Is_Abstract_Subprogram (Subp) then
2053 Error_Msg_N ("stream subprogram must not be abstract", Expr);
2054 return;
2055 end if;
2057 Set_Entity (Expr, Subp);
2058 Set_Etype (Expr, Etype (Subp));
2060 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
2062 else
2063 Error_Msg_Name_1 := Attr;
2064 Error_Msg_N ("incorrect expression for% attribute", Expr);
2065 end if;
2066 end Analyze_Stream_TSS_Definition;
2068 ------------------------------
2069 -- Check_Indexing_Functions --
2070 ------------------------------
2072 procedure Check_Indexing_Functions is
2074 procedure Check_One_Function (Subp : Entity_Id);
2075 -- Check one possible interpretation
2077 ------------------------
2078 -- Check_One_Function --
2079 ------------------------
2081 procedure Check_One_Function (Subp : Entity_Id) is
2082 Default_Element : constant Node_Id :=
2083 Find_Aspect
2084 (Etype (First_Formal (Subp)),
2085 Aspect_Iterator_Element);
2087 begin
2088 if not Check_Primitive_Function (Subp) then
2089 Error_Msg_NE
2090 ("aspect Indexing requires a function that applies to type&",
2091 Subp, Ent);
2092 end if;
2094 -- An indexing function must return either the default element of
2095 -- the container, or a reference type.
2097 if Present (Default_Element) then
2098 Analyze (Default_Element);
2099 if Is_Entity_Name (Default_Element)
2100 and then Covers (Entity (Default_Element), Etype (Subp))
2101 then
2102 return;
2103 end if;
2104 end if;
2106 -- Otherwise the return type must be a reference type.
2108 if not Has_Implicit_Dereference (Etype (Subp)) then
2109 Error_Msg_N
2110 ("function for indexing must return a reference type", Subp);
2111 end if;
2112 end Check_One_Function;
2114 -- Start of processing for Check_Indexing_Functions
2116 begin
2117 if In_Instance then
2118 return;
2119 end if;
2121 Analyze (Expr);
2123 if not Is_Overloaded (Expr) then
2124 Check_One_Function (Entity (Expr));
2126 else
2127 declare
2128 I : Interp_Index;
2129 It : Interp;
2131 begin
2132 Get_First_Interp (Expr, I, It);
2133 while Present (It.Nam) loop
2135 -- Note that analysis will have added the interpretation
2136 -- that corresponds to the dereference. We only check the
2137 -- subprogram itself.
2139 if Is_Overloadable (It.Nam) then
2140 Check_One_Function (It.Nam);
2141 end if;
2143 Get_Next_Interp (I, It);
2144 end loop;
2145 end;
2146 end if;
2147 end Check_Indexing_Functions;
2149 ------------------------------
2150 -- Check_Iterator_Functions --
2151 ------------------------------
2153 procedure Check_Iterator_Functions is
2154 Default : Entity_Id;
2156 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
2157 -- Check one possible interpretation for validity
2159 ----------------------------
2160 -- Valid_Default_Iterator --
2161 ----------------------------
2163 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2164 Formal : Entity_Id;
2166 begin
2167 if not Check_Primitive_Function (Subp) then
2168 return False;
2169 else
2170 Formal := First_Formal (Subp);
2171 end if;
2173 -- False if any subsequent formal has no default expression
2175 Formal := Next_Formal (Formal);
2176 while Present (Formal) loop
2177 if No (Expression (Parent (Formal))) then
2178 return False;
2179 end if;
2181 Next_Formal (Formal);
2182 end loop;
2184 -- True if all subsequent formals have default expressions
2186 return True;
2187 end Valid_Default_Iterator;
2189 -- Start of processing for Check_Iterator_Functions
2191 begin
2192 Analyze (Expr);
2194 if not Is_Entity_Name (Expr) then
2195 Error_Msg_N ("aspect Iterator must be a function name", Expr);
2196 end if;
2198 if not Is_Overloaded (Expr) then
2199 if not Check_Primitive_Function (Entity (Expr)) then
2200 Error_Msg_NE
2201 ("aspect Indexing requires a function that applies to type&",
2202 Entity (Expr), Ent);
2203 end if;
2205 if not Valid_Default_Iterator (Entity (Expr)) then
2206 Error_Msg_N ("improper function for default iterator", Expr);
2207 end if;
2209 else
2210 Default := Empty;
2211 declare
2212 I : Interp_Index;
2213 It : Interp;
2215 begin
2216 Get_First_Interp (Expr, I, It);
2217 while Present (It.Nam) loop
2218 if not Check_Primitive_Function (It.Nam)
2219 or else not Valid_Default_Iterator (It.Nam)
2220 then
2221 Remove_Interp (I);
2223 elsif Present (Default) then
2224 Error_Msg_N ("default iterator must be unique", Expr);
2226 else
2227 Default := It.Nam;
2228 end if;
2230 Get_Next_Interp (I, It);
2231 end loop;
2232 end;
2234 if Present (Default) then
2235 Set_Entity (Expr, Default);
2236 Set_Is_Overloaded (Expr, False);
2237 end if;
2238 end if;
2239 end Check_Iterator_Functions;
2241 -------------------------------
2242 -- Check_Primitive_Function --
2243 -------------------------------
2245 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2246 Ctrl : Entity_Id;
2248 begin
2249 if Ekind (Subp) /= E_Function then
2250 return False;
2251 end if;
2253 if No (First_Formal (Subp)) then
2254 return False;
2255 else
2256 Ctrl := Etype (First_Formal (Subp));
2257 end if;
2259 if Ctrl = Ent
2260 or else Ctrl = Class_Wide_Type (Ent)
2261 or else
2262 (Ekind (Ctrl) = E_Anonymous_Access_Type
2263 and then
2264 (Designated_Type (Ctrl) = Ent
2265 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2266 then
2267 null;
2269 else
2270 return False;
2271 end if;
2273 return True;
2274 end Check_Primitive_Function;
2276 ----------------------
2277 -- Duplicate_Clause --
2278 ----------------------
2280 function Duplicate_Clause return Boolean is
2281 A : Node_Id;
2283 begin
2284 -- Nothing to do if this attribute definition clause comes from
2285 -- an aspect specification, since we could not be duplicating an
2286 -- explicit clause, and we dealt with the case of duplicated aspects
2287 -- in Analyze_Aspect_Specifications.
2289 if From_Aspect_Specification (N) then
2290 return False;
2291 end if;
2293 -- Otherwise current clause may duplicate previous clause, or a
2294 -- previously given pragma or aspect specification for the same
2295 -- aspect.
2297 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
2299 if Present (A) then
2300 Error_Msg_Name_1 := Chars (N);
2301 Error_Msg_Sloc := Sloc (A);
2303 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2304 return True;
2305 end if;
2307 return False;
2308 end Duplicate_Clause;
2310 -- Start of processing for Analyze_Attribute_Definition_Clause
2312 begin
2313 -- The following code is a defense against recursion. Not clear that
2314 -- this can happen legitimately, but perhaps some error situations
2315 -- can cause it, and we did see this recursion during testing.
2317 if Analyzed (N) then
2318 return;
2319 else
2320 Set_Analyzed (N, True);
2321 end if;
2323 -- Ignore some selected attributes in CodePeer mode since they are not
2324 -- relevant in this context.
2326 if CodePeer_Mode then
2327 case Id is
2329 -- Ignore Component_Size in CodePeer mode, to avoid changing the
2330 -- internal representation of types by implicitly packing them.
2332 when Attribute_Component_Size =>
2333 Rewrite (N, Make_Null_Statement (Sloc (N)));
2334 return;
2336 when others =>
2337 null;
2338 end case;
2339 end if;
2341 -- Process Ignore_Rep_Clauses option
2343 if Ignore_Rep_Clauses then
2344 case Id is
2346 -- The following should be ignored. They do not affect legality
2347 -- and may be target dependent. The basic idea of -gnatI is to
2348 -- ignore any rep clauses that may be target dependent but do not
2349 -- affect legality (except possibly to be rejected because they
2350 -- are incompatible with the compilation target).
2352 when Attribute_Alignment |
2353 Attribute_Bit_Order |
2354 Attribute_Component_Size |
2355 Attribute_Machine_Radix |
2356 Attribute_Object_Size |
2357 Attribute_Size |
2358 Attribute_Stream_Size |
2359 Attribute_Value_Size =>
2360 Rewrite (N, Make_Null_Statement (Sloc (N)));
2361 return;
2363 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2365 when Attribute_Small =>
2366 if Ignore_Rep_Clauses then
2367 Rewrite (N, Make_Null_Statement (Sloc (N)));
2368 return;
2369 end if;
2371 -- The following should not be ignored, because in the first place
2372 -- they are reasonably portable, and should not cause problems in
2373 -- compiling code from another target, and also they do affect
2374 -- legality, e.g. failing to provide a stream attribute for a
2375 -- type may make a program illegal.
2377 when Attribute_External_Tag |
2378 Attribute_Input |
2379 Attribute_Output |
2380 Attribute_Read |
2381 Attribute_Simple_Storage_Pool |
2382 Attribute_Storage_Pool |
2383 Attribute_Storage_Size |
2384 Attribute_Write =>
2385 null;
2387 -- Other cases are errors ("attribute& cannot be set with
2388 -- definition clause"), which will be caught below.
2390 when others =>
2391 null;
2392 end case;
2393 end if;
2395 Analyze (Nam);
2396 Ent := Entity (Nam);
2398 if Rep_Item_Too_Early (Ent, N) then
2399 return;
2400 end if;
2402 -- Rep clause applies to full view of incomplete type or private type if
2403 -- we have one (if not, this is a premature use of the type). However,
2404 -- certain semantic checks need to be done on the specified entity (i.e.
2405 -- the private view), so we save it in Ent.
2407 if Is_Private_Type (Ent)
2408 and then Is_Derived_Type (Ent)
2409 and then not Is_Tagged_Type (Ent)
2410 and then No (Full_View (Ent))
2411 then
2412 -- If this is a private type whose completion is a derivation from
2413 -- another private type, there is no full view, and the attribute
2414 -- belongs to the type itself, not its underlying parent.
2416 U_Ent := Ent;
2418 elsif Ekind (Ent) = E_Incomplete_Type then
2420 -- The attribute applies to the full view, set the entity of the
2421 -- attribute definition accordingly.
2423 Ent := Underlying_Type (Ent);
2424 U_Ent := Ent;
2425 Set_Entity (Nam, Ent);
2427 else
2428 U_Ent := Underlying_Type (Ent);
2429 end if;
2431 -- Avoid cascaded error
2433 if Etype (Nam) = Any_Type then
2434 return;
2436 -- Must be declared in current scope or in case of an aspect
2437 -- specification, must be visible in current scope.
2439 elsif Scope (Ent) /= Current_Scope
2440 and then
2441 not (From_Aspect_Specification (N)
2442 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
2443 then
2444 Error_Msg_N ("entity must be declared in this scope", Nam);
2445 return;
2447 -- Must not be a source renaming (we do have some cases where the
2448 -- expander generates a renaming, and those cases are OK, in such
2449 -- cases any attribute applies to the renamed object as well).
2451 elsif Is_Object (Ent)
2452 and then Present (Renamed_Object (Ent))
2453 then
2454 -- Case of renamed object from source, this is an error
2456 if Comes_From_Source (Renamed_Object (Ent)) then
2457 Get_Name_String (Chars (N));
2458 Error_Msg_Strlen := Name_Len;
2459 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2460 Error_Msg_N
2461 ("~ clause not allowed for a renaming declaration "
2462 & "(RM 13.1(6))", Nam);
2463 return;
2465 -- For the case of a compiler generated renaming, the attribute
2466 -- definition clause applies to the renamed object created by the
2467 -- expander. The easiest general way to handle this is to create a
2468 -- copy of the attribute definition clause for this object.
2470 else
2471 Insert_Action (N,
2472 Make_Attribute_Definition_Clause (Loc,
2473 Name =>
2474 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2475 Chars => Chars (N),
2476 Expression => Duplicate_Subexpr (Expression (N))));
2477 end if;
2479 -- If no underlying entity, use entity itself, applies to some
2480 -- previously detected error cases ???
2482 elsif No (U_Ent) then
2483 U_Ent := Ent;
2485 -- Cannot specify for a subtype (exception Object/Value_Size)
2487 elsif Is_Type (U_Ent)
2488 and then not Is_First_Subtype (U_Ent)
2489 and then Id /= Attribute_Object_Size
2490 and then Id /= Attribute_Value_Size
2491 and then not From_At_Mod (N)
2492 then
2493 Error_Msg_N ("cannot specify attribute for subtype", Nam);
2494 return;
2495 end if;
2497 Set_Entity (N, U_Ent);
2499 -- Switch on particular attribute
2501 case Id is
2503 -------------
2504 -- Address --
2505 -------------
2507 -- Address attribute definition clause
2509 when Attribute_Address => Address : begin
2511 -- A little error check, catch for X'Address use X'Address;
2513 if Nkind (Nam) = N_Identifier
2514 and then Nkind (Expr) = N_Attribute_Reference
2515 and then Attribute_Name (Expr) = Name_Address
2516 and then Nkind (Prefix (Expr)) = N_Identifier
2517 and then Chars (Nam) = Chars (Prefix (Expr))
2518 then
2519 Error_Msg_NE
2520 ("address for & is self-referencing", Prefix (Expr), Ent);
2521 return;
2522 end if;
2524 -- Not that special case, carry on with analysis of expression
2526 Analyze_And_Resolve (Expr, RTE (RE_Address));
2528 -- Even when ignoring rep clauses we need to indicate that the
2529 -- entity has an address clause and thus it is legal to declare
2530 -- it imported.
2532 if Ignore_Rep_Clauses then
2533 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2534 Record_Rep_Item (U_Ent, N);
2535 end if;
2537 return;
2538 end if;
2540 if Duplicate_Clause then
2541 null;
2543 -- Case of address clause for subprogram
2545 elsif Is_Subprogram (U_Ent) then
2546 if Has_Homonym (U_Ent) then
2547 Error_Msg_N
2548 ("address clause cannot be given " &
2549 "for overloaded subprogram",
2550 Nam);
2551 return;
2552 end if;
2554 -- For subprograms, all address clauses are permitted, and we
2555 -- mark the subprogram as having a deferred freeze so that Gigi
2556 -- will not elaborate it too soon.
2558 -- Above needs more comments, what is too soon about???
2560 Set_Has_Delayed_Freeze (U_Ent);
2562 -- Case of address clause for entry
2564 elsif Ekind (U_Ent) = E_Entry then
2565 if Nkind (Parent (N)) = N_Task_Body then
2566 Error_Msg_N
2567 ("entry address must be specified in task spec", Nam);
2568 return;
2569 end if;
2571 -- For entries, we require a constant address
2573 Check_Constant_Address_Clause (Expr, U_Ent);
2575 -- Special checks for task types
2577 if Is_Task_Type (Scope (U_Ent))
2578 and then Comes_From_Source (Scope (U_Ent))
2579 then
2580 Error_Msg_N
2581 ("?entry address declared for entry in task type", N);
2582 Error_Msg_N
2583 ("\?only one task can be declared of this type", N);
2584 end if;
2586 -- Entry address clauses are obsolescent
2588 Check_Restriction (No_Obsolescent_Features, N);
2590 if Warn_On_Obsolescent_Feature then
2591 Error_Msg_N
2592 ("attaching interrupt to task entry is an " &
2593 "obsolescent feature (RM J.7.1)?", N);
2594 Error_Msg_N
2595 ("\use interrupt procedure instead?", N);
2596 end if;
2598 -- Case of an address clause for a controlled object which we
2599 -- consider to be erroneous.
2601 elsif Is_Controlled (Etype (U_Ent))
2602 or else Has_Controlled_Component (Etype (U_Ent))
2603 then
2604 Error_Msg_NE
2605 ("?controlled object& must not be overlaid", Nam, U_Ent);
2606 Error_Msg_N
2607 ("\?Program_Error will be raised at run time", Nam);
2608 Insert_Action (Declaration_Node (U_Ent),
2609 Make_Raise_Program_Error (Loc,
2610 Reason => PE_Overlaid_Controlled_Object));
2611 return;
2613 -- Case of address clause for a (non-controlled) object
2615 elsif
2616 Ekind (U_Ent) = E_Variable
2617 or else
2618 Ekind (U_Ent) = E_Constant
2619 then
2620 declare
2621 Expr : constant Node_Id := Expression (N);
2622 O_Ent : Entity_Id;
2623 Off : Boolean;
2625 begin
2626 -- Exported variables cannot have an address clause, because
2627 -- this cancels the effect of the pragma Export.
2629 if Is_Exported (U_Ent) then
2630 Error_Msg_N
2631 ("cannot export object with address clause", Nam);
2632 return;
2633 end if;
2635 Find_Overlaid_Entity (N, O_Ent, Off);
2637 -- Overlaying controlled objects is erroneous
2639 if Present (O_Ent)
2640 and then (Has_Controlled_Component (Etype (O_Ent))
2641 or else Is_Controlled (Etype (O_Ent)))
2642 then
2643 Error_Msg_N
2644 ("?cannot overlay with controlled object", Expr);
2645 Error_Msg_N
2646 ("\?Program_Error will be raised at run time", Expr);
2647 Insert_Action (Declaration_Node (U_Ent),
2648 Make_Raise_Program_Error (Loc,
2649 Reason => PE_Overlaid_Controlled_Object));
2650 return;
2652 elsif Present (O_Ent)
2653 and then Ekind (U_Ent) = E_Constant
2654 and then not Is_Constant_Object (O_Ent)
2655 then
2656 Error_Msg_N ("constant overlays a variable?", Expr);
2658 -- Imported variables can have an address clause, but then
2659 -- the import is pretty meaningless except to suppress
2660 -- initializations, so we do not need such variables to
2661 -- be statically allocated (and in fact it causes trouble
2662 -- if the address clause is a local value).
2664 elsif Is_Imported (U_Ent) then
2665 Set_Is_Statically_Allocated (U_Ent, False);
2666 end if;
2668 -- We mark a possible modification of a variable with an
2669 -- address clause, since it is likely aliasing is occurring.
2671 Note_Possible_Modification (Nam, Sure => False);
2673 -- Here we are checking for explicit overlap of one variable
2674 -- by another, and if we find this then mark the overlapped
2675 -- variable as also being volatile to prevent unwanted
2676 -- optimizations. This is a significant pessimization so
2677 -- avoid it when there is an offset, i.e. when the object
2678 -- is composite; they cannot be optimized easily anyway.
2680 if Present (O_Ent)
2681 and then Is_Object (O_Ent)
2682 and then not Off
2683 then
2684 Set_Treat_As_Volatile (O_Ent);
2685 end if;
2687 -- Legality checks on the address clause for initialized
2688 -- objects is deferred until the freeze point, because
2689 -- a subsequent pragma might indicate that the object
2690 -- is imported and thus not initialized.
2692 Set_Has_Delayed_Freeze (U_Ent);
2694 -- If an initialization call has been generated for this
2695 -- object, it needs to be deferred to after the freeze node
2696 -- we have just now added, otherwise GIGI will see a
2697 -- reference to the variable (as actual to the IP call)
2698 -- before its definition.
2700 declare
2701 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
2702 begin
2703 if Present (Init_Call) then
2704 Remove (Init_Call);
2705 Append_Freeze_Action (U_Ent, Init_Call);
2706 end if;
2707 end;
2709 if Is_Exported (U_Ent) then
2710 Error_Msg_N
2711 ("& cannot be exported if an address clause is given",
2712 Nam);
2713 Error_Msg_N
2714 ("\define and export a variable " &
2715 "that holds its address instead",
2716 Nam);
2717 end if;
2719 -- Entity has delayed freeze, so we will generate an
2720 -- alignment check at the freeze point unless suppressed.
2722 if not Range_Checks_Suppressed (U_Ent)
2723 and then not Alignment_Checks_Suppressed (U_Ent)
2724 then
2725 Set_Check_Address_Alignment (N);
2726 end if;
2728 -- Kill the size check code, since we are not allocating
2729 -- the variable, it is somewhere else.
2731 Kill_Size_Check_Code (U_Ent);
2733 -- If the address clause is of the form:
2735 -- for Y'Address use X'Address
2737 -- or
2739 -- Const : constant Address := X'Address;
2740 -- ...
2741 -- for Y'Address use Const;
2743 -- then we make an entry in the table for checking the size
2744 -- and alignment of the overlaying variable. We defer this
2745 -- check till after code generation to take full advantage
2746 -- of the annotation done by the back end. This entry is
2747 -- only made if the address clause comes from source.
2749 -- If the entity has a generic type, the check will be
2750 -- performed in the instance if the actual type justifies
2751 -- it, and we do not insert the clause in the table to
2752 -- prevent spurious warnings.
2754 if Address_Clause_Overlay_Warnings
2755 and then Comes_From_Source (N)
2756 and then Present (O_Ent)
2757 and then Is_Object (O_Ent)
2758 then
2759 if not Is_Generic_Type (Etype (U_Ent)) then
2760 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
2761 end if;
2763 -- If variable overlays a constant view, and we are
2764 -- warning on overlays, then mark the variable as
2765 -- overlaying a constant (we will give warnings later
2766 -- if this variable is assigned).
2768 if Is_Constant_Object (O_Ent)
2769 and then Ekind (U_Ent) = E_Variable
2770 then
2771 Set_Overlays_Constant (U_Ent);
2772 end if;
2773 end if;
2774 end;
2776 -- Not a valid entity for an address clause
2778 else
2779 Error_Msg_N ("address cannot be given for &", Nam);
2780 end if;
2781 end Address;
2783 ---------------
2784 -- Alignment --
2785 ---------------
2787 -- Alignment attribute definition clause
2789 when Attribute_Alignment => Alignment : declare
2790 Align : constant Uint := Get_Alignment_Value (Expr);
2791 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
2793 begin
2794 FOnly := True;
2796 if not Is_Type (U_Ent)
2797 and then Ekind (U_Ent) /= E_Variable
2798 and then Ekind (U_Ent) /= E_Constant
2799 then
2800 Error_Msg_N ("alignment cannot be given for &", Nam);
2802 elsif Duplicate_Clause then
2803 null;
2805 elsif Align /= No_Uint then
2806 Set_Has_Alignment_Clause (U_Ent);
2808 -- Tagged type case, check for attempt to set alignment to a
2809 -- value greater than Max_Align, and reset if so.
2811 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
2812 Error_Msg_N
2813 ("?alignment for & set to Maximum_Aligment", Nam);
2814 Set_Alignment (U_Ent, Max_Align);
2816 -- All other cases
2818 else
2819 Set_Alignment (U_Ent, Align);
2820 end if;
2822 -- For an array type, U_Ent is the first subtype. In that case,
2823 -- also set the alignment of the anonymous base type so that
2824 -- other subtypes (such as the itypes for aggregates of the
2825 -- type) also receive the expected alignment.
2827 if Is_Array_Type (U_Ent) then
2828 Set_Alignment (Base_Type (U_Ent), Align);
2829 end if;
2830 end if;
2831 end Alignment;
2833 ---------------
2834 -- Bit_Order --
2835 ---------------
2837 -- Bit_Order attribute definition clause
2839 when Attribute_Bit_Order => Bit_Order : declare
2840 begin
2841 if not Is_Record_Type (U_Ent) then
2842 Error_Msg_N
2843 ("Bit_Order can only be defined for record type", Nam);
2845 elsif Duplicate_Clause then
2846 null;
2848 else
2849 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
2851 if Etype (Expr) = Any_Type then
2852 return;
2854 elsif not Is_Static_Expression (Expr) then
2855 Flag_Non_Static_Expr
2856 ("Bit_Order requires static expression!", Expr);
2858 else
2859 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
2860 Set_Reverse_Bit_Order (U_Ent, True);
2861 end if;
2862 end if;
2863 end if;
2864 end Bit_Order;
2866 --------------------
2867 -- Component_Size --
2868 --------------------
2870 -- Component_Size attribute definition clause
2872 when Attribute_Component_Size => Component_Size_Case : declare
2873 Csize : constant Uint := Static_Integer (Expr);
2874 Ctyp : Entity_Id;
2875 Btype : Entity_Id;
2876 Biased : Boolean;
2877 New_Ctyp : Entity_Id;
2878 Decl : Node_Id;
2880 begin
2881 if not Is_Array_Type (U_Ent) then
2882 Error_Msg_N ("component size requires array type", Nam);
2883 return;
2884 end if;
2886 Btype := Base_Type (U_Ent);
2887 Ctyp := Component_Type (Btype);
2889 if Duplicate_Clause then
2890 null;
2892 elsif Rep_Item_Too_Early (Btype, N) then
2893 null;
2895 elsif Csize /= No_Uint then
2896 Check_Size (Expr, Ctyp, Csize, Biased);
2898 -- For the biased case, build a declaration for a subtype that
2899 -- will be used to represent the biased subtype that reflects
2900 -- the biased representation of components. We need the subtype
2901 -- to get proper conversions on referencing elements of the
2902 -- array. Note: component size clauses are ignored in VM mode.
2904 if VM_Target = No_VM then
2905 if Biased then
2906 New_Ctyp :=
2907 Make_Defining_Identifier (Loc,
2908 Chars =>
2909 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
2911 Decl :=
2912 Make_Subtype_Declaration (Loc,
2913 Defining_Identifier => New_Ctyp,
2914 Subtype_Indication =>
2915 New_Occurrence_Of (Component_Type (Btype), Loc));
2917 Set_Parent (Decl, N);
2918 Analyze (Decl, Suppress => All_Checks);
2920 Set_Has_Delayed_Freeze (New_Ctyp, False);
2921 Set_Esize (New_Ctyp, Csize);
2922 Set_RM_Size (New_Ctyp, Csize);
2923 Init_Alignment (New_Ctyp);
2924 Set_Is_Itype (New_Ctyp, True);
2925 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
2927 Set_Component_Type (Btype, New_Ctyp);
2928 Set_Biased (New_Ctyp, N, "component size clause");
2929 end if;
2931 Set_Component_Size (Btype, Csize);
2933 -- For VM case, we ignore component size clauses
2935 else
2936 -- Give a warning unless we are in GNAT mode, in which case
2937 -- the warning is suppressed since it is not useful.
2939 if not GNAT_Mode then
2940 Error_Msg_N
2941 ("?component size ignored in this configuration", N);
2942 end if;
2943 end if;
2945 -- Deal with warning on overridden size
2947 if Warn_On_Overridden_Size
2948 and then Has_Size_Clause (Ctyp)
2949 and then RM_Size (Ctyp) /= Csize
2950 then
2951 Error_Msg_NE
2952 ("?component size overrides size clause for&",
2953 N, Ctyp);
2954 end if;
2956 Set_Has_Component_Size_Clause (Btype, True);
2957 Set_Has_Non_Standard_Rep (Btype, True);
2958 end if;
2959 end Component_Size_Case;
2961 -----------------------
2962 -- Constant_Indexing --
2963 -----------------------
2965 when Attribute_Constant_Indexing =>
2966 Check_Indexing_Functions;
2968 ---------
2969 -- CPU --
2970 ---------
2972 when Attribute_CPU => CPU :
2973 begin
2974 -- CPU attribute definition clause not allowed except from aspect
2975 -- specification.
2977 if From_Aspect_Specification (N) then
2978 if not Is_Task_Type (U_Ent) then
2979 Error_Msg_N ("CPU can only be defined for task", Nam);
2981 elsif Duplicate_Clause then
2982 null;
2984 else
2985 -- The expression must be analyzed in the special manner
2986 -- described in "Handling of Default and Per-Object
2987 -- Expressions" in sem.ads.
2989 -- The visibility to the discriminants must be restored
2991 Push_Scope_And_Install_Discriminants (U_Ent);
2992 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
2993 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
2995 if not Is_Static_Expression (Expr) then
2996 Check_Restriction (Static_Priorities, Expr);
2997 end if;
2998 end if;
3000 else
3001 Error_Msg_N
3002 ("attribute& cannot be set with definition clause", N);
3003 end if;
3004 end CPU;
3006 ----------------------
3007 -- Default_Iterator --
3008 ----------------------
3010 when Attribute_Default_Iterator => Default_Iterator : declare
3011 Func : Entity_Id;
3013 begin
3014 if not Is_Tagged_Type (U_Ent) then
3015 Error_Msg_N
3016 ("aspect Default_Iterator applies to tagged type", Nam);
3017 end if;
3019 Check_Iterator_Functions;
3021 Analyze (Expr);
3023 if not Is_Entity_Name (Expr)
3024 or else Ekind (Entity (Expr)) /= E_Function
3025 then
3026 Error_Msg_N ("aspect Iterator must be a function", Expr);
3027 else
3028 Func := Entity (Expr);
3029 end if;
3031 if No (First_Formal (Func))
3032 or else Etype (First_Formal (Func)) /= U_Ent
3033 then
3034 Error_Msg_NE
3035 ("Default Iterator must be a primitive of&", Func, U_Ent);
3036 end if;
3037 end Default_Iterator;
3039 ------------------------
3040 -- Dispatching_Domain --
3041 ------------------------
3043 when Attribute_Dispatching_Domain => Dispatching_Domain :
3044 begin
3045 -- Dispatching_Domain attribute definition clause not allowed
3046 -- except from aspect specification.
3048 if From_Aspect_Specification (N) then
3049 if not Is_Task_Type (U_Ent) then
3050 Error_Msg_N ("Dispatching_Domain can only be defined" &
3051 "for task",
3052 Nam);
3054 elsif Duplicate_Clause then
3055 null;
3057 else
3058 -- The expression must be analyzed in the special manner
3059 -- described in "Handling of Default and Per-Object
3060 -- Expressions" in sem.ads.
3062 -- The visibility to the discriminants must be restored
3064 Push_Scope_And_Install_Discriminants (U_Ent);
3066 Preanalyze_Spec_Expression
3067 (Expr, RTE (RE_Dispatching_Domain));
3069 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3070 end if;
3072 else
3073 Error_Msg_N
3074 ("attribute& cannot be set with definition clause", N);
3075 end if;
3076 end Dispatching_Domain;
3078 ------------------
3079 -- External_Tag --
3080 ------------------
3082 when Attribute_External_Tag => External_Tag :
3083 begin
3084 if not Is_Tagged_Type (U_Ent) then
3085 Error_Msg_N ("should be a tagged type", Nam);
3086 end if;
3088 if Duplicate_Clause then
3089 null;
3091 else
3092 Analyze_And_Resolve (Expr, Standard_String);
3094 if not Is_Static_Expression (Expr) then
3095 Flag_Non_Static_Expr
3096 ("static string required for tag name!", Nam);
3097 end if;
3099 if VM_Target = No_VM then
3100 Set_Has_External_Tag_Rep_Clause (U_Ent);
3101 else
3102 Error_Msg_Name_1 := Attr;
3103 Error_Msg_N
3104 ("% attribute unsupported in this configuration", Nam);
3105 end if;
3107 if not Is_Library_Level_Entity (U_Ent) then
3108 Error_Msg_NE
3109 ("?non-unique external tag supplied for &", N, U_Ent);
3110 Error_Msg_N
3111 ("?\same external tag applies to all subprogram calls", N);
3112 Error_Msg_N
3113 ("?\corresponding internal tag cannot be obtained", N);
3114 end if;
3115 end if;
3116 end External_Tag;
3118 --------------------------
3119 -- Implicit_Dereference --
3120 --------------------------
3122 when Attribute_Implicit_Dereference =>
3124 -- Legality checks already performed at the point of the type
3125 -- declaration, aspect is not delayed.
3127 null;
3129 -----------
3130 -- Input --
3131 -----------
3133 when Attribute_Input =>
3134 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
3135 Set_Has_Specified_Stream_Input (Ent);
3137 ------------------------
3138 -- Interrupt_Priority --
3139 ------------------------
3141 when Attribute_Interrupt_Priority => Interrupt_Priority :
3142 begin
3143 -- Interrupt_Priority attribute definition clause not allowed
3144 -- except from aspect specification.
3146 if From_Aspect_Specification (N) then
3147 if not (Is_Protected_Type (U_Ent)
3148 or else Is_Task_Type (U_Ent))
3149 then
3150 Error_Msg_N
3151 ("Interrupt_Priority can only be defined for task" &
3152 "and protected object",
3153 Nam);
3155 elsif Duplicate_Clause then
3156 null;
3158 else
3159 -- The expression must be analyzed in the special manner
3160 -- described in "Handling of Default and Per-Object
3161 -- Expressions" in sem.ads.
3163 -- The visibility to the discriminants must be restored
3165 Push_Scope_And_Install_Discriminants (U_Ent);
3167 Preanalyze_Spec_Expression
3168 (Expr, RTE (RE_Interrupt_Priority));
3170 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3171 end if;
3173 else
3174 Error_Msg_N
3175 ("attribute& cannot be set with definition clause", N);
3176 end if;
3177 end Interrupt_Priority;
3179 ----------------------
3180 -- Iterator_Element --
3181 ----------------------
3183 when Attribute_Iterator_Element =>
3184 Analyze (Expr);
3186 if not Is_Entity_Name (Expr)
3187 or else not Is_Type (Entity (Expr))
3188 then
3189 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
3190 end if;
3192 -------------------
3193 -- Machine_Radix --
3194 -------------------
3196 -- Machine radix attribute definition clause
3198 when Attribute_Machine_Radix => Machine_Radix : declare
3199 Radix : constant Uint := Static_Integer (Expr);
3201 begin
3202 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
3203 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
3205 elsif Duplicate_Clause then
3206 null;
3208 elsif Radix /= No_Uint then
3209 Set_Has_Machine_Radix_Clause (U_Ent);
3210 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
3212 if Radix = 2 then
3213 null;
3214 elsif Radix = 10 then
3215 Set_Machine_Radix_10 (U_Ent);
3216 else
3217 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
3218 end if;
3219 end if;
3220 end Machine_Radix;
3222 -----------------
3223 -- Object_Size --
3224 -----------------
3226 -- Object_Size attribute definition clause
3228 when Attribute_Object_Size => Object_Size : declare
3229 Size : constant Uint := Static_Integer (Expr);
3231 Biased : Boolean;
3232 pragma Warnings (Off, Biased);
3234 begin
3235 if not Is_Type (U_Ent) then
3236 Error_Msg_N ("Object_Size cannot be given for &", Nam);
3238 elsif Duplicate_Clause then
3239 null;
3241 else
3242 Check_Size (Expr, U_Ent, Size, Biased);
3244 if Size /= 8
3245 and then
3246 Size /= 16
3247 and then
3248 Size /= 32
3249 and then
3250 UI_Mod (Size, 64) /= 0
3251 then
3252 Error_Msg_N
3253 ("Object_Size must be 8, 16, 32, or multiple of 64",
3254 Expr);
3255 end if;
3257 Set_Esize (U_Ent, Size);
3258 Set_Has_Object_Size_Clause (U_Ent);
3259 Alignment_Check_For_Size_Change (U_Ent, Size);
3260 end if;
3261 end Object_Size;
3263 ------------
3264 -- Output --
3265 ------------
3267 when Attribute_Output =>
3268 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
3269 Set_Has_Specified_Stream_Output (Ent);
3271 --------------
3272 -- Priority --
3273 --------------
3275 when Attribute_Priority => Priority :
3276 begin
3277 -- Priority attribute definition clause not allowed except from
3278 -- aspect specification.
3280 if From_Aspect_Specification (N) then
3281 if not (Is_Protected_Type (U_Ent)
3282 or else Is_Task_Type (U_Ent)
3283 or else Ekind (U_Ent) = E_Procedure)
3284 then
3285 Error_Msg_N
3286 ("Priority can only be defined for task and protected " &
3287 "object",
3288 Nam);
3290 elsif Duplicate_Clause then
3291 null;
3293 else
3294 -- The expression must be analyzed in the special manner
3295 -- described in "Handling of Default and Per-Object
3296 -- Expressions" in sem.ads.
3298 -- The visibility to the discriminants must be restored
3300 Push_Scope_And_Install_Discriminants (U_Ent);
3301 Preanalyze_Spec_Expression (Expr, Standard_Integer);
3302 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3304 if not Is_Static_Expression (Expr) then
3305 Check_Restriction (Static_Priorities, Expr);
3306 end if;
3307 end if;
3309 else
3310 Error_Msg_N
3311 ("attribute& cannot be set with definition clause", N);
3312 end if;
3313 end Priority;
3315 ----------
3316 -- Read --
3317 ----------
3319 when Attribute_Read =>
3320 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
3321 Set_Has_Specified_Stream_Read (Ent);
3323 --------------------------
3324 -- Scalar_Storage_Order --
3325 --------------------------
3327 -- Scalar_Storage_Order attribute definition clause
3329 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
3330 begin
3331 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
3332 Error_Msg_N
3333 ("Scalar_Storage_Order can only be defined for "
3334 & "record or array type", Nam);
3336 elsif Duplicate_Clause then
3337 null;
3339 else
3340 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3342 if Etype (Expr) = Any_Type then
3343 return;
3345 elsif not Is_Static_Expression (Expr) then
3346 Flag_Non_Static_Expr
3347 ("Scalar_Storage_Order requires static expression!", Expr);
3349 else
3350 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3351 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
3352 end if;
3353 end if;
3354 end if;
3355 end Scalar_Storage_Order;
3357 ----------
3358 -- Size --
3359 ----------
3361 -- Size attribute definition clause
3363 when Attribute_Size => Size : declare
3364 Size : constant Uint := Static_Integer (Expr);
3365 Etyp : Entity_Id;
3366 Biased : Boolean;
3368 begin
3369 FOnly := True;
3371 if Duplicate_Clause then
3372 null;
3374 elsif not Is_Type (U_Ent)
3375 and then Ekind (U_Ent) /= E_Variable
3376 and then Ekind (U_Ent) /= E_Constant
3377 then
3378 Error_Msg_N ("size cannot be given for &", Nam);
3380 elsif Is_Array_Type (U_Ent)
3381 and then not Is_Constrained (U_Ent)
3382 then
3383 Error_Msg_N
3384 ("size cannot be given for unconstrained array", Nam);
3386 elsif Size /= No_Uint then
3387 if VM_Target /= No_VM and then not GNAT_Mode then
3389 -- Size clause is not handled properly on VM targets.
3390 -- Display a warning unless we are in GNAT mode, in which
3391 -- case this is useless.
3393 Error_Msg_N
3394 ("?size clauses are ignored in this configuration", N);
3395 end if;
3397 if Is_Type (U_Ent) then
3398 Etyp := U_Ent;
3399 else
3400 Etyp := Etype (U_Ent);
3401 end if;
3403 -- Check size, note that Gigi is in charge of checking that the
3404 -- size of an array or record type is OK. Also we do not check
3405 -- the size in the ordinary fixed-point case, since it is too
3406 -- early to do so (there may be subsequent small clause that
3407 -- affects the size). We can check the size if a small clause
3408 -- has already been given.
3410 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
3411 or else Has_Small_Clause (U_Ent)
3412 then
3413 Check_Size (Expr, Etyp, Size, Biased);
3414 Set_Biased (U_Ent, N, "size clause", Biased);
3415 end if;
3417 -- For types set RM_Size and Esize if possible
3419 if Is_Type (U_Ent) then
3420 Set_RM_Size (U_Ent, Size);
3422 -- For elementary types, increase Object_Size to power of 2,
3423 -- but not less than a storage unit in any case (normally
3424 -- this means it will be byte addressable).
3426 -- For all other types, nothing else to do, we leave Esize
3427 -- (object size) unset, the back end will set it from the
3428 -- size and alignment in an appropriate manner.
3430 -- In both cases, we check whether the alignment must be
3431 -- reset in the wake of the size change.
3433 if Is_Elementary_Type (U_Ent) then
3434 if Size <= System_Storage_Unit then
3435 Init_Esize (U_Ent, System_Storage_Unit);
3436 elsif Size <= 16 then
3437 Init_Esize (U_Ent, 16);
3438 elsif Size <= 32 then
3439 Init_Esize (U_Ent, 32);
3440 else
3441 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
3442 end if;
3444 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3445 else
3446 Alignment_Check_For_Size_Change (U_Ent, Size);
3447 end if;
3449 -- For objects, set Esize only
3451 else
3452 if Is_Elementary_Type (Etyp) then
3453 if Size /= System_Storage_Unit
3454 and then
3455 Size /= System_Storage_Unit * 2
3456 and then
3457 Size /= System_Storage_Unit * 4
3458 and then
3459 Size /= System_Storage_Unit * 8
3460 then
3461 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3462 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3463 Error_Msg_N
3464 ("size for primitive object must be a power of 2"
3465 & " in the range ^-^", N);
3466 end if;
3467 end if;
3469 Set_Esize (U_Ent, Size);
3470 end if;
3472 Set_Has_Size_Clause (U_Ent);
3473 end if;
3474 end Size;
3476 -----------
3477 -- Small --
3478 -----------
3480 -- Small attribute definition clause
3482 when Attribute_Small => Small : declare
3483 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3484 Small : Ureal;
3486 begin
3487 Analyze_And_Resolve (Expr, Any_Real);
3489 if Etype (Expr) = Any_Type then
3490 return;
3492 elsif not Is_Static_Expression (Expr) then
3493 Flag_Non_Static_Expr
3494 ("small requires static expression!", Expr);
3495 return;
3497 else
3498 Small := Expr_Value_R (Expr);
3500 if Small <= Ureal_0 then
3501 Error_Msg_N ("small value must be greater than zero", Expr);
3502 return;
3503 end if;
3505 end if;
3507 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3508 Error_Msg_N
3509 ("small requires an ordinary fixed point type", Nam);
3511 elsif Has_Small_Clause (U_Ent) then
3512 Error_Msg_N ("small already given for &", Nam);
3514 elsif Small > Delta_Value (U_Ent) then
3515 Error_Msg_N
3516 ("small value must not be greater than delta value", Nam);
3518 else
3519 Set_Small_Value (U_Ent, Small);
3520 Set_Small_Value (Implicit_Base, Small);
3521 Set_Has_Small_Clause (U_Ent);
3522 Set_Has_Small_Clause (Implicit_Base);
3523 Set_Has_Non_Standard_Rep (Implicit_Base);
3524 end if;
3525 end Small;
3527 ------------------
3528 -- Storage_Pool --
3529 ------------------
3531 -- Storage_Pool attribute definition clause
3533 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
3534 Pool : Entity_Id;
3535 T : Entity_Id;
3537 begin
3538 if Ekind (U_Ent) = E_Access_Subprogram_Type then
3539 Error_Msg_N
3540 ("storage pool cannot be given for access-to-subprogram type",
3541 Nam);
3542 return;
3544 elsif not
3545 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3546 then
3547 Error_Msg_N
3548 ("storage pool can only be given for access types", Nam);
3549 return;
3551 elsif Is_Derived_Type (U_Ent) then
3552 Error_Msg_N
3553 ("storage pool cannot be given for a derived access type",
3554 Nam);
3556 elsif Duplicate_Clause then
3557 return;
3559 elsif Present (Associated_Storage_Pool (U_Ent)) then
3560 Error_Msg_N ("storage pool already given for &", Nam);
3561 return;
3562 end if;
3564 if Id = Attribute_Storage_Pool then
3565 Analyze_And_Resolve
3566 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3568 -- In the Simple_Storage_Pool case, we allow a variable of any
3569 -- simple storage pool type, so we Resolve without imposing an
3570 -- expected type.
3572 else
3573 Analyze_And_Resolve (Expr);
3575 if not Present (Get_Rep_Pragma
3576 (Etype (Expr), Name_Simple_Storage_Pool_Type))
3577 then
3578 Error_Msg_N
3579 ("expression must be of a simple storage pool type", Expr);
3580 end if;
3581 end if;
3583 if not Denotes_Variable (Expr) then
3584 Error_Msg_N ("storage pool must be a variable", Expr);
3585 return;
3586 end if;
3588 if Nkind (Expr) = N_Type_Conversion then
3589 T := Etype (Expression (Expr));
3590 else
3591 T := Etype (Expr);
3592 end if;
3594 -- The Stack_Bounded_Pool is used internally for implementing
3595 -- access types with a Storage_Size. Since it only work properly
3596 -- when used on one specific type, we need to check that it is not
3597 -- hijacked improperly:
3599 -- type T is access Integer;
3600 -- for T'Storage_Size use n;
3601 -- type Q is access Float;
3602 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
3604 if RTE_Available (RE_Stack_Bounded_Pool)
3605 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
3606 then
3607 Error_Msg_N ("non-shareable internal Pool", Expr);
3608 return;
3609 end if;
3611 -- If the argument is a name that is not an entity name, then
3612 -- we construct a renaming operation to define an entity of
3613 -- type storage pool.
3615 if not Is_Entity_Name (Expr)
3616 and then Is_Object_Reference (Expr)
3617 then
3618 Pool := Make_Temporary (Loc, 'P', Expr);
3620 declare
3621 Rnode : constant Node_Id :=
3622 Make_Object_Renaming_Declaration (Loc,
3623 Defining_Identifier => Pool,
3624 Subtype_Mark =>
3625 New_Occurrence_Of (Etype (Expr), Loc),
3626 Name => Expr);
3628 begin
3629 Insert_Before (N, Rnode);
3630 Analyze (Rnode);
3631 Set_Associated_Storage_Pool (U_Ent, Pool);
3632 end;
3634 elsif Is_Entity_Name (Expr) then
3635 Pool := Entity (Expr);
3637 -- If pool is a renamed object, get original one. This can
3638 -- happen with an explicit renaming, and within instances.
3640 while Present (Renamed_Object (Pool))
3641 and then Is_Entity_Name (Renamed_Object (Pool))
3642 loop
3643 Pool := Entity (Renamed_Object (Pool));
3644 end loop;
3646 if Present (Renamed_Object (Pool))
3647 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
3648 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
3649 then
3650 Pool := Entity (Expression (Renamed_Object (Pool)));
3651 end if;
3653 Set_Associated_Storage_Pool (U_Ent, Pool);
3655 elsif Nkind (Expr) = N_Type_Conversion
3656 and then Is_Entity_Name (Expression (Expr))
3657 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
3658 then
3659 Pool := Entity (Expression (Expr));
3660 Set_Associated_Storage_Pool (U_Ent, Pool);
3662 else
3663 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
3664 return;
3665 end if;
3666 end;
3668 ------------------
3669 -- Storage_Size --
3670 ------------------
3672 -- Storage_Size attribute definition clause
3674 when Attribute_Storage_Size => Storage_Size : declare
3675 Btype : constant Entity_Id := Base_Type (U_Ent);
3677 begin
3678 if Is_Task_Type (U_Ent) then
3679 Check_Restriction (No_Obsolescent_Features, N);
3681 if Warn_On_Obsolescent_Feature then
3682 Error_Msg_N
3683 ("storage size clause for task is an " &
3684 "obsolescent feature (RM J.9)?", N);
3685 Error_Msg_N ("\use Storage_Size pragma instead?", N);
3686 end if;
3688 FOnly := True;
3689 end if;
3691 if not Is_Access_Type (U_Ent)
3692 and then Ekind (U_Ent) /= E_Task_Type
3693 then
3694 Error_Msg_N ("storage size cannot be given for &", Nam);
3696 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
3697 Error_Msg_N
3698 ("storage size cannot be given for a derived access type",
3699 Nam);
3701 elsif Duplicate_Clause then
3702 null;
3704 else
3705 Analyze_And_Resolve (Expr, Any_Integer);
3707 if Is_Access_Type (U_Ent) then
3708 if Present (Associated_Storage_Pool (U_Ent)) then
3709 Error_Msg_N ("storage pool already given for &", Nam);
3710 return;
3711 end if;
3713 if Is_OK_Static_Expression (Expr)
3714 and then Expr_Value (Expr) = 0
3715 then
3716 Set_No_Pool_Assigned (Btype);
3717 end if;
3718 end if;
3720 Set_Has_Storage_Size_Clause (Btype);
3721 end if;
3722 end Storage_Size;
3724 -----------------
3725 -- Stream_Size --
3726 -----------------
3728 when Attribute_Stream_Size => Stream_Size : declare
3729 Size : constant Uint := Static_Integer (Expr);
3731 begin
3732 if Ada_Version <= Ada_95 then
3733 Check_Restriction (No_Implementation_Attributes, N);
3734 end if;
3736 if Duplicate_Clause then
3737 null;
3739 elsif Is_Elementary_Type (U_Ent) then
3740 if Size /= System_Storage_Unit
3741 and then
3742 Size /= System_Storage_Unit * 2
3743 and then
3744 Size /= System_Storage_Unit * 4
3745 and then
3746 Size /= System_Storage_Unit * 8
3747 then
3748 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3749 Error_Msg_N
3750 ("stream size for elementary type must be a"
3751 & " power of 2 and at least ^", N);
3753 elsif RM_Size (U_Ent) > Size then
3754 Error_Msg_Uint_1 := RM_Size (U_Ent);
3755 Error_Msg_N
3756 ("stream size for elementary type must be a"
3757 & " power of 2 and at least ^", N);
3758 end if;
3760 Set_Has_Stream_Size_Clause (U_Ent);
3762 else
3763 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
3764 end if;
3765 end Stream_Size;
3767 ----------------
3768 -- Value_Size --
3769 ----------------
3771 -- Value_Size attribute definition clause
3773 when Attribute_Value_Size => Value_Size : declare
3774 Size : constant Uint := Static_Integer (Expr);
3775 Biased : Boolean;
3777 begin
3778 if not Is_Type (U_Ent) then
3779 Error_Msg_N ("Value_Size cannot be given for &", Nam);
3781 elsif Duplicate_Clause then
3782 null;
3784 elsif Is_Array_Type (U_Ent)
3785 and then not Is_Constrained (U_Ent)
3786 then
3787 Error_Msg_N
3788 ("Value_Size cannot be given for unconstrained array", Nam);
3790 else
3791 if Is_Elementary_Type (U_Ent) then
3792 Check_Size (Expr, U_Ent, Size, Biased);
3793 Set_Biased (U_Ent, N, "value size clause", Biased);
3794 end if;
3796 Set_RM_Size (U_Ent, Size);
3797 end if;
3798 end Value_Size;
3800 -----------------------
3801 -- Variable_Indexing --
3802 -----------------------
3804 when Attribute_Variable_Indexing =>
3805 Check_Indexing_Functions;
3807 -----------
3808 -- Write --
3809 -----------
3811 when Attribute_Write =>
3812 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
3813 Set_Has_Specified_Stream_Write (Ent);
3815 -- All other attributes cannot be set
3817 when others =>
3818 Error_Msg_N
3819 ("attribute& cannot be set with definition clause", N);
3820 end case;
3822 -- The test for the type being frozen must be performed after any
3823 -- expression the clause has been analyzed since the expression itself
3824 -- might cause freezing that makes the clause illegal.
3826 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
3827 return;
3828 end if;
3829 end Analyze_Attribute_Definition_Clause;
3831 ----------------------------
3832 -- Analyze_Code_Statement --
3833 ----------------------------
3835 procedure Analyze_Code_Statement (N : Node_Id) is
3836 HSS : constant Node_Id := Parent (N);
3837 SBody : constant Node_Id := Parent (HSS);
3838 Subp : constant Entity_Id := Current_Scope;
3839 Stmt : Node_Id;
3840 Decl : Node_Id;
3841 StmtO : Node_Id;
3842 DeclO : Node_Id;
3844 begin
3845 -- Analyze and check we get right type, note that this implements the
3846 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
3847 -- is the only way that Asm_Insn could possibly be visible.
3849 Analyze_And_Resolve (Expression (N));
3851 if Etype (Expression (N)) = Any_Type then
3852 return;
3853 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
3854 Error_Msg_N ("incorrect type for code statement", N);
3855 return;
3856 end if;
3858 Check_Code_Statement (N);
3860 -- Make sure we appear in the handled statement sequence of a
3861 -- subprogram (RM 13.8(3)).
3863 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
3864 or else Nkind (SBody) /= N_Subprogram_Body
3865 then
3866 Error_Msg_N
3867 ("code statement can only appear in body of subprogram", N);
3868 return;
3869 end if;
3871 -- Do remaining checks (RM 13.8(3)) if not already done
3873 if not Is_Machine_Code_Subprogram (Subp) then
3874 Set_Is_Machine_Code_Subprogram (Subp);
3876 -- No exception handlers allowed
3878 if Present (Exception_Handlers (HSS)) then
3879 Error_Msg_N
3880 ("exception handlers not permitted in machine code subprogram",
3881 First (Exception_Handlers (HSS)));
3882 end if;
3884 -- No declarations other than use clauses and pragmas (we allow
3885 -- certain internally generated declarations as well).
3887 Decl := First (Declarations (SBody));
3888 while Present (Decl) loop
3889 DeclO := Original_Node (Decl);
3890 if Comes_From_Source (DeclO)
3891 and not Nkind_In (DeclO, N_Pragma,
3892 N_Use_Package_Clause,
3893 N_Use_Type_Clause,
3894 N_Implicit_Label_Declaration)
3895 then
3896 Error_Msg_N
3897 ("this declaration not allowed in machine code subprogram",
3898 DeclO);
3899 end if;
3901 Next (Decl);
3902 end loop;
3904 -- No statements other than code statements, pragmas, and labels.
3905 -- Again we allow certain internally generated statements.
3907 -- In Ada 2012, qualified expressions are names, and the code
3908 -- statement is initially parsed as a procedure call.
3910 Stmt := First (Statements (HSS));
3911 while Present (Stmt) loop
3912 StmtO := Original_Node (Stmt);
3914 -- A procedure call transformed into a code statement is OK.
3916 if Ada_Version >= Ada_2012
3917 and then Nkind (StmtO) = N_Procedure_Call_Statement
3918 and then Nkind (Name (StmtO)) = N_Qualified_Expression
3919 then
3920 null;
3922 elsif Comes_From_Source (StmtO)
3923 and then not Nkind_In (StmtO, N_Pragma,
3924 N_Label,
3925 N_Code_Statement)
3926 then
3927 Error_Msg_N
3928 ("this statement is not allowed in machine code subprogram",
3929 StmtO);
3930 end if;
3932 Next (Stmt);
3933 end loop;
3934 end if;
3935 end Analyze_Code_Statement;
3937 -----------------------------------------------
3938 -- Analyze_Enumeration_Representation_Clause --
3939 -----------------------------------------------
3941 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
3942 Ident : constant Node_Id := Identifier (N);
3943 Aggr : constant Node_Id := Array_Aggregate (N);
3944 Enumtype : Entity_Id;
3945 Elit : Entity_Id;
3946 Expr : Node_Id;
3947 Assoc : Node_Id;
3948 Choice : Node_Id;
3949 Val : Uint;
3951 Err : Boolean := False;
3952 -- Set True to avoid cascade errors and crashes on incorrect source code
3954 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
3955 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
3956 -- Allowed range of universal integer (= allowed range of enum lit vals)
3958 Min : Uint;
3959 Max : Uint;
3960 -- Minimum and maximum values of entries
3962 Max_Node : Node_Id;
3963 -- Pointer to node for literal providing max value
3965 begin
3966 if Ignore_Rep_Clauses then
3967 return;
3968 end if;
3970 -- First some basic error checks
3972 Find_Type (Ident);
3973 Enumtype := Entity (Ident);
3975 if Enumtype = Any_Type
3976 or else Rep_Item_Too_Early (Enumtype, N)
3977 then
3978 return;
3979 else
3980 Enumtype := Underlying_Type (Enumtype);
3981 end if;
3983 if not Is_Enumeration_Type (Enumtype) then
3984 Error_Msg_NE
3985 ("enumeration type required, found}",
3986 Ident, First_Subtype (Enumtype));
3987 return;
3988 end if;
3990 -- Ignore rep clause on generic actual type. This will already have
3991 -- been flagged on the template as an error, and this is the safest
3992 -- way to ensure we don't get a junk cascaded message in the instance.
3994 if Is_Generic_Actual_Type (Enumtype) then
3995 return;
3997 -- Type must be in current scope
3999 elsif Scope (Enumtype) /= Current_Scope then
4000 Error_Msg_N ("type must be declared in this scope", Ident);
4001 return;
4003 -- Type must be a first subtype
4005 elsif not Is_First_Subtype (Enumtype) then
4006 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
4007 return;
4009 -- Ignore duplicate rep clause
4011 elsif Has_Enumeration_Rep_Clause (Enumtype) then
4012 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
4013 return;
4015 -- Don't allow rep clause for standard [wide_[wide_]]character
4017 elsif Is_Standard_Character_Type (Enumtype) then
4018 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
4019 return;
4021 -- Check that the expression is a proper aggregate (no parentheses)
4023 elsif Paren_Count (Aggr) /= 0 then
4024 Error_Msg
4025 ("extra parentheses surrounding aggregate not allowed",
4026 First_Sloc (Aggr));
4027 return;
4029 -- All tests passed, so set rep clause in place
4031 else
4032 Set_Has_Enumeration_Rep_Clause (Enumtype);
4033 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
4034 end if;
4036 -- Now we process the aggregate. Note that we don't use the normal
4037 -- aggregate code for this purpose, because we don't want any of the
4038 -- normal expansion activities, and a number of special semantic
4039 -- rules apply (including the component type being any integer type)
4041 Elit := First_Literal (Enumtype);
4043 -- First the positional entries if any
4045 if Present (Expressions (Aggr)) then
4046 Expr := First (Expressions (Aggr));
4047 while Present (Expr) loop
4048 if No (Elit) then
4049 Error_Msg_N ("too many entries in aggregate", Expr);
4050 return;
4051 end if;
4053 Val := Static_Integer (Expr);
4055 -- Err signals that we found some incorrect entries processing
4056 -- the list. The final checks for completeness and ordering are
4057 -- skipped in this case.
4059 if Val = No_Uint then
4060 Err := True;
4061 elsif Val < Lo or else Hi < Val then
4062 Error_Msg_N ("value outside permitted range", Expr);
4063 Err := True;
4064 end if;
4066 Set_Enumeration_Rep (Elit, Val);
4067 Set_Enumeration_Rep_Expr (Elit, Expr);
4068 Next (Expr);
4069 Next (Elit);
4070 end loop;
4071 end if;
4073 -- Now process the named entries if present
4075 if Present (Component_Associations (Aggr)) then
4076 Assoc := First (Component_Associations (Aggr));
4077 while Present (Assoc) loop
4078 Choice := First (Choices (Assoc));
4080 if Present (Next (Choice)) then
4081 Error_Msg_N
4082 ("multiple choice not allowed here", Next (Choice));
4083 Err := True;
4084 end if;
4086 if Nkind (Choice) = N_Others_Choice then
4087 Error_Msg_N ("others choice not allowed here", Choice);
4088 Err := True;
4090 elsif Nkind (Choice) = N_Range then
4092 -- ??? should allow zero/one element range here
4094 Error_Msg_N ("range not allowed here", Choice);
4095 Err := True;
4097 else
4098 Analyze_And_Resolve (Choice, Enumtype);
4100 if Error_Posted (Choice) then
4101 Err := True;
4102 end if;
4104 if not Err then
4105 if Is_Entity_Name (Choice)
4106 and then Is_Type (Entity (Choice))
4107 then
4108 Error_Msg_N ("subtype name not allowed here", Choice);
4109 Err := True;
4111 -- ??? should allow static subtype with zero/one entry
4113 elsif Etype (Choice) = Base_Type (Enumtype) then
4114 if not Is_Static_Expression (Choice) then
4115 Flag_Non_Static_Expr
4116 ("non-static expression used for choice!", Choice);
4117 Err := True;
4119 else
4120 Elit := Expr_Value_E (Choice);
4122 if Present (Enumeration_Rep_Expr (Elit)) then
4123 Error_Msg_Sloc :=
4124 Sloc (Enumeration_Rep_Expr (Elit));
4125 Error_Msg_NE
4126 ("representation for& previously given#",
4127 Choice, Elit);
4128 Err := True;
4129 end if;
4131 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
4133 Expr := Expression (Assoc);
4134 Val := Static_Integer (Expr);
4136 if Val = No_Uint then
4137 Err := True;
4139 elsif Val < Lo or else Hi < Val then
4140 Error_Msg_N ("value outside permitted range", Expr);
4141 Err := True;
4142 end if;
4144 Set_Enumeration_Rep (Elit, Val);
4145 end if;
4146 end if;
4147 end if;
4148 end if;
4150 Next (Assoc);
4151 end loop;
4152 end if;
4154 -- Aggregate is fully processed. Now we check that a full set of
4155 -- representations was given, and that they are in range and in order.
4156 -- These checks are only done if no other errors occurred.
4158 if not Err then
4159 Min := No_Uint;
4160 Max := No_Uint;
4162 Elit := First_Literal (Enumtype);
4163 while Present (Elit) loop
4164 if No (Enumeration_Rep_Expr (Elit)) then
4165 Error_Msg_NE ("missing representation for&!", N, Elit);
4167 else
4168 Val := Enumeration_Rep (Elit);
4170 if Min = No_Uint then
4171 Min := Val;
4172 end if;
4174 if Val /= No_Uint then
4175 if Max /= No_Uint and then Val <= Max then
4176 Error_Msg_NE
4177 ("enumeration value for& not ordered!",
4178 Enumeration_Rep_Expr (Elit), Elit);
4179 end if;
4181 Max_Node := Enumeration_Rep_Expr (Elit);
4182 Max := Val;
4183 end if;
4185 -- If there is at least one literal whose representation is not
4186 -- equal to the Pos value, then note that this enumeration type
4187 -- has a non-standard representation.
4189 if Val /= Enumeration_Pos (Elit) then
4190 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
4191 end if;
4192 end if;
4194 Next (Elit);
4195 end loop;
4197 -- Now set proper size information
4199 declare
4200 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
4202 begin
4203 if Has_Size_Clause (Enumtype) then
4205 -- All OK, if size is OK now
4207 if RM_Size (Enumtype) >= Minsize then
4208 null;
4210 else
4211 -- Try if we can get by with biasing
4213 Minsize :=
4214 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
4216 -- Error message if even biasing does not work
4218 if RM_Size (Enumtype) < Minsize then
4219 Error_Msg_Uint_1 := RM_Size (Enumtype);
4220 Error_Msg_Uint_2 := Max;
4221 Error_Msg_N
4222 ("previously given size (^) is too small "
4223 & "for this value (^)", Max_Node);
4225 -- If biasing worked, indicate that we now have biased rep
4227 else
4228 Set_Biased
4229 (Enumtype, Size_Clause (Enumtype), "size clause");
4230 end if;
4231 end if;
4233 else
4234 Set_RM_Size (Enumtype, Minsize);
4235 Set_Enum_Esize (Enumtype);
4236 end if;
4238 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
4239 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
4240 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
4241 end;
4242 end if;
4244 -- We repeat the too late test in case it froze itself!
4246 if Rep_Item_Too_Late (Enumtype, N) then
4247 null;
4248 end if;
4249 end Analyze_Enumeration_Representation_Clause;
4251 ----------------------------
4252 -- Analyze_Free_Statement --
4253 ----------------------------
4255 procedure Analyze_Free_Statement (N : Node_Id) is
4256 begin
4257 Analyze (Expression (N));
4258 end Analyze_Free_Statement;
4260 ---------------------------
4261 -- Analyze_Freeze_Entity --
4262 ---------------------------
4264 procedure Analyze_Freeze_Entity (N : Node_Id) is
4265 E : constant Entity_Id := Entity (N);
4267 begin
4268 -- Remember that we are processing a freezing entity. Required to
4269 -- ensure correct decoration of internal entities associated with
4270 -- interfaces (see New_Overloaded_Entity).
4272 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
4274 -- For tagged types covering interfaces add internal entities that link
4275 -- the primitives of the interfaces with the primitives that cover them.
4276 -- Note: These entities were originally generated only when generating
4277 -- code because their main purpose was to provide support to initialize
4278 -- the secondary dispatch tables. They are now generated also when
4279 -- compiling with no code generation to provide ASIS the relationship
4280 -- between interface primitives and tagged type primitives. They are
4281 -- also used to locate primitives covering interfaces when processing
4282 -- generics (see Derive_Subprograms).
4284 if Ada_Version >= Ada_2005
4285 and then Ekind (E) = E_Record_Type
4286 and then Is_Tagged_Type (E)
4287 and then not Is_Interface (E)
4288 and then Has_Interfaces (E)
4289 then
4290 -- This would be a good common place to call the routine that checks
4291 -- overriding of interface primitives (and thus factorize calls to
4292 -- Check_Abstract_Overriding located at different contexts in the
4293 -- compiler). However, this is not possible because it causes
4294 -- spurious errors in case of late overriding.
4296 Add_Internal_Interface_Entities (E);
4297 end if;
4299 -- Check CPP types
4301 if Ekind (E) = E_Record_Type
4302 and then Is_CPP_Class (E)
4303 and then Is_Tagged_Type (E)
4304 and then Tagged_Type_Expansion
4305 and then Expander_Active
4306 then
4307 if CPP_Num_Prims (E) = 0 then
4309 -- If the CPP type has user defined components then it must import
4310 -- primitives from C++. This is required because if the C++ class
4311 -- has no primitives then the C++ compiler does not added the _tag
4312 -- component to the type.
4314 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
4316 if First_Entity (E) /= Last_Entity (E) then
4317 Error_Msg_N
4318 ("?'C'P'P type must import at least one primitive from C++",
4320 end if;
4321 end if;
4323 -- Check that all its primitives are abstract or imported from C++.
4324 -- Check also availability of the C++ constructor.
4326 declare
4327 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
4328 Elmt : Elmt_Id;
4329 Error_Reported : Boolean := False;
4330 Prim : Node_Id;
4332 begin
4333 Elmt := First_Elmt (Primitive_Operations (E));
4334 while Present (Elmt) loop
4335 Prim := Node (Elmt);
4337 if Comes_From_Source (Prim) then
4338 if Is_Abstract_Subprogram (Prim) then
4339 null;
4341 elsif not Is_Imported (Prim)
4342 or else Convention (Prim) /= Convention_CPP
4343 then
4344 Error_Msg_N
4345 ("?primitives of 'C'P'P types must be imported from C++"
4346 & " or abstract", Prim);
4348 elsif not Has_Constructors
4349 and then not Error_Reported
4350 then
4351 Error_Msg_Name_1 := Chars (E);
4352 Error_Msg_N
4353 ("?'C'P'P constructor required for type %", Prim);
4354 Error_Reported := True;
4355 end if;
4356 end if;
4358 Next_Elmt (Elmt);
4359 end loop;
4360 end;
4361 end if;
4363 -- Check Ada derivation of CPP type
4365 if Expander_Active
4366 and then Tagged_Type_Expansion
4367 and then Ekind (E) = E_Record_Type
4368 and then Etype (E) /= E
4369 and then Is_CPP_Class (Etype (E))
4370 and then CPP_Num_Prims (Etype (E)) > 0
4371 and then not Is_CPP_Class (E)
4372 and then not Has_CPP_Constructors (Etype (E))
4373 then
4374 -- If the parent has C++ primitives but it has no constructor then
4375 -- check that all the primitives are overridden in this derivation;
4376 -- otherwise the constructor of the parent is needed to build the
4377 -- dispatch table.
4379 declare
4380 Elmt : Elmt_Id;
4381 Prim : Node_Id;
4383 begin
4384 Elmt := First_Elmt (Primitive_Operations (E));
4385 while Present (Elmt) loop
4386 Prim := Node (Elmt);
4388 if not Is_Abstract_Subprogram (Prim)
4389 and then No (Interface_Alias (Prim))
4390 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
4391 then
4392 Error_Msg_Name_1 := Chars (Etype (E));
4393 Error_Msg_N
4394 ("'C'P'P constructor required for parent type %", E);
4395 exit;
4396 end if;
4398 Next_Elmt (Elmt);
4399 end loop;
4400 end;
4401 end if;
4403 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
4405 -- If we have a type with predicates, build predicate function
4407 if Is_Type (E) and then Has_Predicates (E) then
4408 Build_Predicate_Function (E, N);
4409 end if;
4411 -- If type has delayed aspects, this is where we do the preanalysis at
4412 -- the freeze point, as part of the consistent visibility check. Note
4413 -- that this must be done after calling Build_Predicate_Function or
4414 -- Build_Invariant_Procedure since these subprograms fix occurrences of
4415 -- the subtype name in the saved expression so that they will not cause
4416 -- trouble in the preanalysis.
4418 if Has_Delayed_Aspects (E)
4419 and then Scope (E) = Current_Scope
4420 then
4421 -- Retrieve the visibility to the discriminants in order to properly
4422 -- analyze the aspects.
4424 Push_Scope_And_Install_Discriminants (E);
4426 declare
4427 Ritem : Node_Id;
4429 begin
4430 -- Look for aspect specification entries for this entity
4432 Ritem := First_Rep_Item (E);
4433 while Present (Ritem) loop
4434 if Nkind (Ritem) = N_Aspect_Specification
4435 and then Entity (Ritem) = E
4436 and then Is_Delayed_Aspect (Ritem)
4437 then
4438 Check_Aspect_At_Freeze_Point (Ritem);
4439 end if;
4441 Next_Rep_Item (Ritem);
4442 end loop;
4443 end;
4445 Uninstall_Discriminants_And_Pop_Scope (E);
4446 end if;
4447 end Analyze_Freeze_Entity;
4449 ------------------------------------------
4450 -- Analyze_Record_Representation_Clause --
4451 ------------------------------------------
4453 -- Note: we check as much as we can here, but we can't do any checks
4454 -- based on the position values (e.g. overlap checks) until freeze time
4455 -- because especially in Ada 2005 (machine scalar mode), the processing
4456 -- for non-standard bit order can substantially change the positions.
4457 -- See procedure Check_Record_Representation_Clause (called from Freeze)
4458 -- for the remainder of this processing.
4460 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
4461 Ident : constant Node_Id := Identifier (N);
4462 Biased : Boolean;
4463 CC : Node_Id;
4464 Comp : Entity_Id;
4465 Fbit : Uint;
4466 Hbit : Uint := Uint_0;
4467 Lbit : Uint;
4468 Ocomp : Entity_Id;
4469 Posit : Uint;
4470 Rectype : Entity_Id;
4472 CR_Pragma : Node_Id := Empty;
4473 -- Points to N_Pragma node if Complete_Representation pragma present
4475 begin
4476 if Ignore_Rep_Clauses then
4477 return;
4478 end if;
4480 Find_Type (Ident);
4481 Rectype := Entity (Ident);
4483 if Rectype = Any_Type
4484 or else Rep_Item_Too_Early (Rectype, N)
4485 then
4486 return;
4487 else
4488 Rectype := Underlying_Type (Rectype);
4489 end if;
4491 -- First some basic error checks
4493 if not Is_Record_Type (Rectype) then
4494 Error_Msg_NE
4495 ("record type required, found}", Ident, First_Subtype (Rectype));
4496 return;
4498 elsif Scope (Rectype) /= Current_Scope then
4499 Error_Msg_N ("type must be declared in this scope", N);
4500 return;
4502 elsif not Is_First_Subtype (Rectype) then
4503 Error_Msg_N ("cannot give record rep clause for subtype", N);
4504 return;
4506 elsif Has_Record_Rep_Clause (Rectype) then
4507 Error_Msg_N ("duplicate record rep clause ignored", N);
4508 return;
4510 elsif Rep_Item_Too_Late (Rectype, N) then
4511 return;
4512 end if;
4514 if Present (Mod_Clause (N)) then
4515 declare
4516 Loc : constant Source_Ptr := Sloc (N);
4517 M : constant Node_Id := Mod_Clause (N);
4518 P : constant List_Id := Pragmas_Before (M);
4519 AtM_Nod : Node_Id;
4521 Mod_Val : Uint;
4522 pragma Warnings (Off, Mod_Val);
4524 begin
4525 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
4527 if Warn_On_Obsolescent_Feature then
4528 Error_Msg_N
4529 ("mod clause is an obsolescent feature (RM J.8)?", N);
4530 Error_Msg_N
4531 ("\use alignment attribute definition clause instead?", N);
4532 end if;
4534 if Present (P) then
4535 Analyze_List (P);
4536 end if;
4538 -- In ASIS_Mode mode, expansion is disabled, but we must convert
4539 -- the Mod clause into an alignment clause anyway, so that the
4540 -- back-end can compute and back-annotate properly the size and
4541 -- alignment of types that may include this record.
4543 -- This seems dubious, this destroys the source tree in a manner
4544 -- not detectable by ASIS ???
4546 if Operating_Mode = Check_Semantics and then ASIS_Mode then
4547 AtM_Nod :=
4548 Make_Attribute_Definition_Clause (Loc,
4549 Name => New_Reference_To (Base_Type (Rectype), Loc),
4550 Chars => Name_Alignment,
4551 Expression => Relocate_Node (Expression (M)));
4553 Set_From_At_Mod (AtM_Nod);
4554 Insert_After (N, AtM_Nod);
4555 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
4556 Set_Mod_Clause (N, Empty);
4558 else
4559 -- Get the alignment value to perform error checking
4561 Mod_Val := Get_Alignment_Value (Expression (M));
4562 end if;
4563 end;
4564 end if;
4566 -- For untagged types, clear any existing component clauses for the
4567 -- type. If the type is derived, this is what allows us to override
4568 -- a rep clause for the parent. For type extensions, the representation
4569 -- of the inherited components is inherited, so we want to keep previous
4570 -- component clauses for completeness.
4572 if not Is_Tagged_Type (Rectype) then
4573 Comp := First_Component_Or_Discriminant (Rectype);
4574 while Present (Comp) loop
4575 Set_Component_Clause (Comp, Empty);
4576 Next_Component_Or_Discriminant (Comp);
4577 end loop;
4578 end if;
4580 -- All done if no component clauses
4582 CC := First (Component_Clauses (N));
4584 if No (CC) then
4585 return;
4586 end if;
4588 -- A representation like this applies to the base type
4590 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
4591 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
4592 Set_Has_Specified_Layout (Base_Type (Rectype));
4594 -- Process the component clauses
4596 while Present (CC) loop
4598 -- Pragma
4600 if Nkind (CC) = N_Pragma then
4601 Analyze (CC);
4603 -- The only pragma of interest is Complete_Representation
4605 if Pragma_Name (CC) = Name_Complete_Representation then
4606 CR_Pragma := CC;
4607 end if;
4609 -- Processing for real component clause
4611 else
4612 Posit := Static_Integer (Position (CC));
4613 Fbit := Static_Integer (First_Bit (CC));
4614 Lbit := Static_Integer (Last_Bit (CC));
4616 if Posit /= No_Uint
4617 and then Fbit /= No_Uint
4618 and then Lbit /= No_Uint
4619 then
4620 if Posit < 0 then
4621 Error_Msg_N
4622 ("position cannot be negative", Position (CC));
4624 elsif Fbit < 0 then
4625 Error_Msg_N
4626 ("first bit cannot be negative", First_Bit (CC));
4628 -- The Last_Bit specified in a component clause must not be
4629 -- less than the First_Bit minus one (RM-13.5.1(10)).
4631 elsif Lbit < Fbit - 1 then
4632 Error_Msg_N
4633 ("last bit cannot be less than first bit minus one",
4634 Last_Bit (CC));
4636 -- Values look OK, so find the corresponding record component
4637 -- Even though the syntax allows an attribute reference for
4638 -- implementation-defined components, GNAT does not allow the
4639 -- tag to get an explicit position.
4641 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
4642 if Attribute_Name (Component_Name (CC)) = Name_Tag then
4643 Error_Msg_N ("position of tag cannot be specified", CC);
4644 else
4645 Error_Msg_N ("illegal component name", CC);
4646 end if;
4648 else
4649 Comp := First_Entity (Rectype);
4650 while Present (Comp) loop
4651 exit when Chars (Comp) = Chars (Component_Name (CC));
4652 Next_Entity (Comp);
4653 end loop;
4655 if No (Comp) then
4657 -- Maybe component of base type that is absent from
4658 -- statically constrained first subtype.
4660 Comp := First_Entity (Base_Type (Rectype));
4661 while Present (Comp) loop
4662 exit when Chars (Comp) = Chars (Component_Name (CC));
4663 Next_Entity (Comp);
4664 end loop;
4665 end if;
4667 if No (Comp) then
4668 Error_Msg_N
4669 ("component clause is for non-existent field", CC);
4671 -- Ada 2012 (AI05-0026): Any name that denotes a
4672 -- discriminant of an object of an unchecked union type
4673 -- shall not occur within a record_representation_clause.
4675 -- The general restriction of using record rep clauses on
4676 -- Unchecked_Union types has now been lifted. Since it is
4677 -- possible to introduce a record rep clause which mentions
4678 -- the discriminant of an Unchecked_Union in non-Ada 2012
4679 -- code, this check is applied to all versions of the
4680 -- language.
4682 elsif Ekind (Comp) = E_Discriminant
4683 and then Is_Unchecked_Union (Rectype)
4684 then
4685 Error_Msg_N
4686 ("cannot reference discriminant of unchecked union",
4687 Component_Name (CC));
4689 elsif Present (Component_Clause (Comp)) then
4691 -- Diagnose duplicate rep clause, or check consistency
4692 -- if this is an inherited component. In a double fault,
4693 -- there may be a duplicate inconsistent clause for an
4694 -- inherited component.
4696 if Scope (Original_Record_Component (Comp)) = Rectype
4697 or else Parent (Component_Clause (Comp)) = N
4698 then
4699 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
4700 Error_Msg_N ("component clause previously given#", CC);
4702 else
4703 declare
4704 Rep1 : constant Node_Id := Component_Clause (Comp);
4705 begin
4706 if Intval (Position (Rep1)) /=
4707 Intval (Position (CC))
4708 or else Intval (First_Bit (Rep1)) /=
4709 Intval (First_Bit (CC))
4710 or else Intval (Last_Bit (Rep1)) /=
4711 Intval (Last_Bit (CC))
4712 then
4713 Error_Msg_N ("component clause inconsistent "
4714 & "with representation of ancestor", CC);
4715 elsif Warn_On_Redundant_Constructs then
4716 Error_Msg_N ("?redundant component clause "
4717 & "for inherited component!", CC);
4718 end if;
4719 end;
4720 end if;
4722 -- Normal case where this is the first component clause we
4723 -- have seen for this entity, so set it up properly.
4725 else
4726 -- Make reference for field in record rep clause and set
4727 -- appropriate entity field in the field identifier.
4729 Generate_Reference
4730 (Comp, Component_Name (CC), Set_Ref => False);
4731 Set_Entity (Component_Name (CC), Comp);
4733 -- Update Fbit and Lbit to the actual bit number
4735 Fbit := Fbit + UI_From_Int (SSU) * Posit;
4736 Lbit := Lbit + UI_From_Int (SSU) * Posit;
4738 if Has_Size_Clause (Rectype)
4739 and then RM_Size (Rectype) <= Lbit
4740 then
4741 Error_Msg_N
4742 ("bit number out of range of specified size",
4743 Last_Bit (CC));
4744 else
4745 Set_Component_Clause (Comp, CC);
4746 Set_Component_Bit_Offset (Comp, Fbit);
4747 Set_Esize (Comp, 1 + (Lbit - Fbit));
4748 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
4749 Set_Normalized_Position (Comp, Fbit / SSU);
4751 if Warn_On_Overridden_Size
4752 and then Has_Size_Clause (Etype (Comp))
4753 and then RM_Size (Etype (Comp)) /= Esize (Comp)
4754 then
4755 Error_Msg_NE
4756 ("?component size overrides size clause for&",
4757 Component_Name (CC), Etype (Comp));
4758 end if;
4760 -- This information is also set in the corresponding
4761 -- component of the base type, found by accessing the
4762 -- Original_Record_Component link if it is present.
4764 Ocomp := Original_Record_Component (Comp);
4766 if Hbit < Lbit then
4767 Hbit := Lbit;
4768 end if;
4770 Check_Size
4771 (Component_Name (CC),
4772 Etype (Comp),
4773 Esize (Comp),
4774 Biased);
4776 Set_Biased
4777 (Comp, First_Node (CC), "component clause", Biased);
4779 if Present (Ocomp) then
4780 Set_Component_Clause (Ocomp, CC);
4781 Set_Component_Bit_Offset (Ocomp, Fbit);
4782 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
4783 Set_Normalized_Position (Ocomp, Fbit / SSU);
4784 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
4786 Set_Normalized_Position_Max
4787 (Ocomp, Normalized_Position (Ocomp));
4789 -- Note: we don't use Set_Biased here, because we
4790 -- already gave a warning above if needed, and we
4791 -- would get a duplicate for the same name here.
4793 Set_Has_Biased_Representation
4794 (Ocomp, Has_Biased_Representation (Comp));
4795 end if;
4797 if Esize (Comp) < 0 then
4798 Error_Msg_N ("component size is negative", CC);
4799 end if;
4800 end if;
4801 end if;
4802 end if;
4803 end if;
4804 end if;
4806 Next (CC);
4807 end loop;
4809 -- Check missing components if Complete_Representation pragma appeared
4811 if Present (CR_Pragma) then
4812 Comp := First_Component_Or_Discriminant (Rectype);
4813 while Present (Comp) loop
4814 if No (Component_Clause (Comp)) then
4815 Error_Msg_NE
4816 ("missing component clause for &", CR_Pragma, Comp);
4817 end if;
4819 Next_Component_Or_Discriminant (Comp);
4820 end loop;
4822 -- If no Complete_Representation pragma, warn if missing components
4824 elsif Warn_On_Unrepped_Components then
4825 declare
4826 Num_Repped_Components : Nat := 0;
4827 Num_Unrepped_Components : Nat := 0;
4829 begin
4830 -- First count number of repped and unrepped components
4832 Comp := First_Component_Or_Discriminant (Rectype);
4833 while Present (Comp) loop
4834 if Present (Component_Clause (Comp)) then
4835 Num_Repped_Components := Num_Repped_Components + 1;
4836 else
4837 Num_Unrepped_Components := Num_Unrepped_Components + 1;
4838 end if;
4840 Next_Component_Or_Discriminant (Comp);
4841 end loop;
4843 -- We are only interested in the case where there is at least one
4844 -- unrepped component, and at least half the components have rep
4845 -- clauses. We figure that if less than half have them, then the
4846 -- partial rep clause is really intentional. If the component
4847 -- type has no underlying type set at this point (as for a generic
4848 -- formal type), we don't know enough to give a warning on the
4849 -- component.
4851 if Num_Unrepped_Components > 0
4852 and then Num_Unrepped_Components < Num_Repped_Components
4853 then
4854 Comp := First_Component_Or_Discriminant (Rectype);
4855 while Present (Comp) loop
4856 if No (Component_Clause (Comp))
4857 and then Comes_From_Source (Comp)
4858 and then Present (Underlying_Type (Etype (Comp)))
4859 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
4860 or else Size_Known_At_Compile_Time
4861 (Underlying_Type (Etype (Comp))))
4862 and then not Has_Warnings_Off (Rectype)
4863 then
4864 Error_Msg_Sloc := Sloc (Comp);
4865 Error_Msg_NE
4866 ("?no component clause given for & declared #",
4867 N, Comp);
4868 end if;
4870 Next_Component_Or_Discriminant (Comp);
4871 end loop;
4872 end if;
4873 end;
4874 end if;
4875 end Analyze_Record_Representation_Clause;
4877 -------------------------------
4878 -- Build_Invariant_Procedure --
4879 -------------------------------
4881 -- The procedure that is constructed here has the form
4883 -- procedure typInvariant (Ixxx : typ) is
4884 -- begin
4885 -- pragma Check (Invariant, exp, "failed invariant from xxx");
4886 -- pragma Check (Invariant, exp, "failed invariant from xxx");
4887 -- ...
4888 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
4889 -- ...
4890 -- end typInvariant;
4892 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
4893 Loc : constant Source_Ptr := Sloc (Typ);
4894 Stmts : List_Id;
4895 Spec : Node_Id;
4896 SId : Entity_Id;
4897 PDecl : Node_Id;
4898 PBody : Node_Id;
4900 Visible_Decls : constant List_Id := Visible_Declarations (N);
4901 Private_Decls : constant List_Id := Private_Declarations (N);
4903 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
4904 -- Appends statements to Stmts for any invariants in the rep item chain
4905 -- of the given type. If Inherit is False, then we only process entries
4906 -- on the chain for the type Typ. If Inherit is True, then we ignore any
4907 -- Invariant aspects, but we process all Invariant'Class aspects, adding
4908 -- "inherited" to the exception message and generating an informational
4909 -- message about the inheritance of an invariant.
4911 Object_Name : constant Name_Id := New_Internal_Name ('I');
4912 -- Name for argument of invariant procedure
4914 Object_Entity : constant Node_Id :=
4915 Make_Defining_Identifier (Loc, Object_Name);
4916 -- The procedure declaration entity for the argument
4918 --------------------
4919 -- Add_Invariants --
4920 --------------------
4922 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
4923 Ritem : Node_Id;
4924 Arg1 : Node_Id;
4925 Arg2 : Node_Id;
4926 Arg3 : Node_Id;
4927 Exp : Node_Id;
4928 Loc : Source_Ptr;
4929 Assoc : List_Id;
4930 Str : String_Id;
4932 procedure Replace_Type_Reference (N : Node_Id);
4933 -- Replace a single occurrence N of the subtype name with a reference
4934 -- to the formal of the predicate function. N can be an identifier
4935 -- referencing the subtype, or a selected component, representing an
4936 -- appropriately qualified occurrence of the subtype name.
4938 procedure Replace_Type_References is
4939 new Replace_Type_References_Generic (Replace_Type_Reference);
4940 -- Traverse an expression replacing all occurrences of the subtype
4941 -- name with appropriate references to the object that is the formal
4942 -- parameter of the predicate function. Note that we must ensure
4943 -- that the type and entity information is properly set in the
4944 -- replacement node, since we will do a Preanalyze call of this
4945 -- expression without proper visibility of the procedure argument.
4947 ----------------------------
4948 -- Replace_Type_Reference --
4949 ----------------------------
4951 procedure Replace_Type_Reference (N : Node_Id) is
4952 begin
4953 -- Invariant'Class, replace with T'Class (obj)
4955 if Class_Present (Ritem) then
4956 Rewrite (N,
4957 Make_Type_Conversion (Loc,
4958 Subtype_Mark =>
4959 Make_Attribute_Reference (Loc,
4960 Prefix => New_Occurrence_Of (T, Loc),
4961 Attribute_Name => Name_Class),
4962 Expression => Make_Identifier (Loc, Object_Name)));
4964 Set_Entity (Expression (N), Object_Entity);
4965 Set_Etype (Expression (N), Typ);
4967 -- Invariant, replace with obj
4969 else
4970 Rewrite (N, Make_Identifier (Loc, Object_Name));
4971 Set_Entity (N, Object_Entity);
4972 Set_Etype (N, Typ);
4973 end if;
4974 end Replace_Type_Reference;
4976 -- Start of processing for Add_Invariants
4978 begin
4979 Ritem := First_Rep_Item (T);
4980 while Present (Ritem) loop
4981 if Nkind (Ritem) = N_Pragma
4982 and then Pragma_Name (Ritem) = Name_Invariant
4983 then
4984 Arg1 := First (Pragma_Argument_Associations (Ritem));
4985 Arg2 := Next (Arg1);
4986 Arg3 := Next (Arg2);
4988 Arg1 := Get_Pragma_Arg (Arg1);
4989 Arg2 := Get_Pragma_Arg (Arg2);
4991 -- For Inherit case, ignore Invariant, process only Class case
4993 if Inherit then
4994 if not Class_Present (Ritem) then
4995 goto Continue;
4996 end if;
4998 -- For Inherit false, process only item for right type
5000 else
5001 if Entity (Arg1) /= Typ then
5002 goto Continue;
5003 end if;
5004 end if;
5006 if No (Stmts) then
5007 Stmts := Empty_List;
5008 end if;
5010 Exp := New_Copy_Tree (Arg2);
5011 Loc := Sloc (Exp);
5013 -- We need to replace any occurrences of the name of the type
5014 -- with references to the object, converted to type'Class in
5015 -- the case of Invariant'Class aspects.
5017 Replace_Type_References (Exp, Chars (T));
5019 -- If this invariant comes from an aspect, find the aspect
5020 -- specification, and replace the saved expression because
5021 -- we need the subtype references replaced for the calls to
5022 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5023 -- and Check_Aspect_At_End_Of_Declarations.
5025 if From_Aspect_Specification (Ritem) then
5026 declare
5027 Aitem : Node_Id;
5029 begin
5030 -- Loop to find corresponding aspect, note that this
5031 -- must be present given the pragma is marked delayed.
5033 Aitem := Next_Rep_Item (Ritem);
5034 while Present (Aitem) loop
5035 if Nkind (Aitem) = N_Aspect_Specification
5036 and then Aspect_Rep_Item (Aitem) = Ritem
5037 then
5038 Set_Entity
5039 (Identifier (Aitem), New_Copy_Tree (Exp));
5040 exit;
5041 end if;
5043 Aitem := Next_Rep_Item (Aitem);
5044 end loop;
5045 end;
5046 end if;
5048 -- Now we need to preanalyze the expression to properly capture
5049 -- the visibility in the visible part. The expression will not
5050 -- be analyzed for real until the body is analyzed, but that is
5051 -- at the end of the private part and has the wrong visibility.
5053 Set_Parent (Exp, N);
5054 Preanalyze_Spec_Expression (Exp, Standard_Boolean);
5056 -- Build first two arguments for Check pragma
5058 Assoc := New_List (
5059 Make_Pragma_Argument_Association (Loc,
5060 Expression => Make_Identifier (Loc, Name_Invariant)),
5061 Make_Pragma_Argument_Association (Loc, Expression => Exp));
5063 -- Add message if present in Invariant pragma
5065 if Present (Arg3) then
5066 Str := Strval (Get_Pragma_Arg (Arg3));
5068 -- If inherited case, and message starts "failed invariant",
5069 -- change it to be "failed inherited invariant".
5071 if Inherit then
5072 String_To_Name_Buffer (Str);
5074 if Name_Buffer (1 .. 16) = "failed invariant" then
5075 Insert_Str_In_Name_Buffer ("inherited ", 8);
5076 Str := String_From_Name_Buffer;
5077 end if;
5078 end if;
5080 Append_To (Assoc,
5081 Make_Pragma_Argument_Association (Loc,
5082 Expression => Make_String_Literal (Loc, Str)));
5083 end if;
5085 -- Add Check pragma to list of statements
5087 Append_To (Stmts,
5088 Make_Pragma (Loc,
5089 Pragma_Identifier =>
5090 Make_Identifier (Loc, Name_Check),
5091 Pragma_Argument_Associations => Assoc));
5093 -- If Inherited case and option enabled, output info msg. Note
5094 -- that we know this is a case of Invariant'Class.
5096 if Inherit and Opt.List_Inherited_Aspects then
5097 Error_Msg_Sloc := Sloc (Ritem);
5098 Error_Msg_N
5099 ("?info: & inherits `Invariant''Class` aspect from #",
5100 Typ);
5101 end if;
5102 end if;
5104 <<Continue>>
5105 Next_Rep_Item (Ritem);
5106 end loop;
5107 end Add_Invariants;
5109 -- Start of processing for Build_Invariant_Procedure
5111 begin
5112 Stmts := No_List;
5113 PDecl := Empty;
5114 PBody := Empty;
5115 Set_Etype (Object_Entity, Typ);
5117 -- Add invariants for the current type
5119 Add_Invariants (Typ, Inherit => False);
5121 -- Add invariants for parent types
5123 declare
5124 Current_Typ : Entity_Id;
5125 Parent_Typ : Entity_Id;
5127 begin
5128 Current_Typ := Typ;
5129 loop
5130 Parent_Typ := Etype (Current_Typ);
5132 if Is_Private_Type (Parent_Typ)
5133 and then Present (Full_View (Base_Type (Parent_Typ)))
5134 then
5135 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5136 end if;
5138 exit when Parent_Typ = Current_Typ;
5140 Current_Typ := Parent_Typ;
5141 Add_Invariants (Current_Typ, Inherit => True);
5142 end loop;
5143 end;
5145 -- Build the procedure if we generated at least one Check pragma
5147 if Stmts /= No_List then
5149 -- Build procedure declaration
5151 SId :=
5152 Make_Defining_Identifier (Loc,
5153 Chars => New_External_Name (Chars (Typ), "Invariant"));
5154 Set_Has_Invariants (SId);
5155 Set_Invariant_Procedure (Typ, SId);
5157 Spec :=
5158 Make_Procedure_Specification (Loc,
5159 Defining_Unit_Name => SId,
5160 Parameter_Specifications => New_List (
5161 Make_Parameter_Specification (Loc,
5162 Defining_Identifier => Object_Entity,
5163 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5165 PDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
5167 -- Build procedure body
5169 SId :=
5170 Make_Defining_Identifier (Loc,
5171 Chars => New_External_Name (Chars (Typ), "Invariant"));
5173 Spec :=
5174 Make_Procedure_Specification (Loc,
5175 Defining_Unit_Name => SId,
5176 Parameter_Specifications => New_List (
5177 Make_Parameter_Specification (Loc,
5178 Defining_Identifier =>
5179 Make_Defining_Identifier (Loc, Object_Name),
5180 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5182 PBody :=
5183 Make_Subprogram_Body (Loc,
5184 Specification => Spec,
5185 Declarations => Empty_List,
5186 Handled_Statement_Sequence =>
5187 Make_Handled_Sequence_Of_Statements (Loc,
5188 Statements => Stmts));
5190 -- Insert procedure declaration and spec at the appropriate points.
5191 -- Skip this if there are no private declarations (that's an error
5192 -- that will be diagnosed elsewhere, and there is no point in having
5193 -- an invariant procedure set if the full declaration is missing).
5195 if Present (Private_Decls) then
5197 -- The spec goes at the end of visible declarations, but they have
5198 -- already been analyzed, so we need to explicitly do the analyze.
5200 Append_To (Visible_Decls, PDecl);
5201 Analyze (PDecl);
5203 -- The body goes at the end of the private declarations, which we
5204 -- have not analyzed yet, so we do not need to perform an explicit
5205 -- analyze call. We skip this if there are no private declarations
5206 -- (this is an error that will be caught elsewhere);
5208 Append_To (Private_Decls, PBody);
5210 -- If the invariant appears on the full view of a type, the
5211 -- analysis of the private part is complete, and we must
5212 -- analyze the new body explicitly.
5214 if In_Private_Part (Current_Scope) then
5215 Analyze (PBody);
5216 end if;
5217 end if;
5218 end if;
5219 end Build_Invariant_Procedure;
5221 ------------------------------
5222 -- Build_Predicate_Function --
5223 ------------------------------
5225 -- The procedure that is constructed here has the form
5227 -- function typPredicate (Ixxx : typ) return Boolean is
5228 -- begin
5229 -- return
5230 -- exp1 and then exp2 and then ...
5231 -- and then typ1Predicate (typ1 (Ixxx))
5232 -- and then typ2Predicate (typ2 (Ixxx))
5233 -- and then ...;
5234 -- end typPredicate;
5236 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
5237 -- this is the point at which these expressions get analyzed, providing the
5238 -- required delay, and typ1, typ2, are entities from which predicates are
5239 -- inherited. Note that we do NOT generate Check pragmas, that's because we
5240 -- use this function even if checks are off, e.g. for membership tests.
5242 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
5243 Loc : constant Source_Ptr := Sloc (Typ);
5244 Spec : Node_Id;
5245 SId : Entity_Id;
5246 FDecl : Node_Id;
5247 FBody : Node_Id;
5249 Expr : Node_Id;
5250 -- This is the expression for the return statement in the function. It
5251 -- is build by connecting the component predicates with AND THEN.
5253 procedure Add_Call (T : Entity_Id);
5254 -- Includes a call to the predicate function for type T in Expr if T
5255 -- has predicates and Predicate_Function (T) is non-empty.
5257 procedure Add_Predicates;
5258 -- Appends expressions for any Predicate pragmas in the rep item chain
5259 -- Typ to Expr. Note that we look only at items for this exact entity.
5260 -- Inheritance of predicates for the parent type is done by calling the
5261 -- Predicate_Function of the parent type, using Add_Call above.
5263 Object_Name : constant Name_Id := New_Internal_Name ('I');
5264 -- Name for argument of Predicate procedure
5266 Object_Entity : constant Entity_Id :=
5267 Make_Defining_Identifier (Loc, Object_Name);
5268 -- The entity for the spec entity for the argument
5270 Dynamic_Predicate_Present : Boolean := False;
5271 -- Set True if a dynamic predicate is present, results in the entire
5272 -- predicate being considered dynamic even if it looks static
5274 Static_Predicate_Present : Node_Id := Empty;
5275 -- Set to N_Pragma node for a static predicate if one is encountered.
5277 --------------
5278 -- Add_Call --
5279 --------------
5281 procedure Add_Call (T : Entity_Id) is
5282 Exp : Node_Id;
5284 begin
5285 if Present (T) and then Present (Predicate_Function (T)) then
5286 Set_Has_Predicates (Typ);
5288 -- Build the call to the predicate function of T
5290 Exp :=
5291 Make_Predicate_Call
5292 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
5294 -- Add call to evolving expression, using AND THEN if needed
5296 if No (Expr) then
5297 Expr := Exp;
5298 else
5299 Expr :=
5300 Make_And_Then (Loc,
5301 Left_Opnd => Relocate_Node (Expr),
5302 Right_Opnd => Exp);
5303 end if;
5305 -- Output info message on inheritance if required. Note we do not
5306 -- give this information for generic actual types, since it is
5307 -- unwelcome noise in that case in instantiations. We also
5308 -- generally suppress the message in instantiations, and also
5309 -- if it involves internal names.
5311 if Opt.List_Inherited_Aspects
5312 and then not Is_Generic_Actual_Type (Typ)
5313 and then Instantiation_Depth (Sloc (Typ)) = 0
5314 and then not Is_Internal_Name (Chars (T))
5315 and then not Is_Internal_Name (Chars (Typ))
5316 then
5317 Error_Msg_Sloc := Sloc (Predicate_Function (T));
5318 Error_Msg_Node_2 := T;
5319 Error_Msg_N ("?info: & inherits predicate from & #", Typ);
5320 end if;
5321 end if;
5322 end Add_Call;
5324 --------------------
5325 -- Add_Predicates --
5326 --------------------
5328 procedure Add_Predicates is
5329 Ritem : Node_Id;
5330 Arg1 : Node_Id;
5331 Arg2 : Node_Id;
5333 procedure Replace_Type_Reference (N : Node_Id);
5334 -- Replace a single occurrence N of the subtype name with a reference
5335 -- to the formal of the predicate function. N can be an identifier
5336 -- referencing the subtype, or a selected component, representing an
5337 -- appropriately qualified occurrence of the subtype name.
5339 procedure Replace_Type_References is
5340 new Replace_Type_References_Generic (Replace_Type_Reference);
5341 -- Traverse an expression changing every occurrence of an identifier
5342 -- whose name matches the name of the subtype with a reference to
5343 -- the formal parameter of the predicate function.
5345 ----------------------------
5346 -- Replace_Type_Reference --
5347 ----------------------------
5349 procedure Replace_Type_Reference (N : Node_Id) is
5350 begin
5351 Rewrite (N, Make_Identifier (Loc, Object_Name));
5352 Set_Entity (N, Object_Entity);
5353 Set_Etype (N, Typ);
5354 end Replace_Type_Reference;
5356 -- Start of processing for Add_Predicates
5358 begin
5359 Ritem := First_Rep_Item (Typ);
5360 while Present (Ritem) loop
5361 if Nkind (Ritem) = N_Pragma
5362 and then Pragma_Name (Ritem) = Name_Predicate
5363 then
5364 if Present (Corresponding_Aspect (Ritem)) then
5365 case Chars (Identifier (Corresponding_Aspect (Ritem))) is
5366 when Name_Dynamic_Predicate =>
5367 Dynamic_Predicate_Present := True;
5368 when Name_Static_Predicate =>
5369 Static_Predicate_Present := Ritem;
5370 when others =>
5371 null;
5372 end case;
5373 end if;
5375 -- Acquire arguments
5377 Arg1 := First (Pragma_Argument_Associations (Ritem));
5378 Arg2 := Next (Arg1);
5380 Arg1 := Get_Pragma_Arg (Arg1);
5381 Arg2 := Get_Pragma_Arg (Arg2);
5383 -- See if this predicate pragma is for the current type or for
5384 -- its full view. A predicate on a private completion is placed
5385 -- on the partial view beause this is the visible entity that
5386 -- is frozen.
5388 if Entity (Arg1) = Typ
5389 or else Full_View (Entity (Arg1)) = Typ
5390 then
5392 -- We have a match, this entry is for our subtype
5394 -- We need to replace any occurrences of the name of the
5395 -- type with references to the object.
5397 Replace_Type_References (Arg2, Chars (Typ));
5399 -- If this predicate comes from an aspect, find the aspect
5400 -- specification, and replace the saved expression because
5401 -- we need the subtype references replaced for the calls to
5402 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5403 -- and Check_Aspect_At_End_Of_Declarations.
5405 if From_Aspect_Specification (Ritem) then
5406 declare
5407 Aitem : Node_Id;
5409 begin
5410 -- Loop to find corresponding aspect, note that this
5411 -- must be present given the pragma is marked delayed.
5413 Aitem := Next_Rep_Item (Ritem);
5414 loop
5415 if Nkind (Aitem) = N_Aspect_Specification
5416 and then Aspect_Rep_Item (Aitem) = Ritem
5417 then
5418 Set_Entity
5419 (Identifier (Aitem), New_Copy_Tree (Arg2));
5420 exit;
5421 end if;
5423 Aitem := Next_Rep_Item (Aitem);
5424 end loop;
5425 end;
5426 end if;
5428 -- Now we can add the expression
5430 if No (Expr) then
5431 Expr := Relocate_Node (Arg2);
5433 -- There already was a predicate, so add to it
5435 else
5436 Expr :=
5437 Make_And_Then (Loc,
5438 Left_Opnd => Relocate_Node (Expr),
5439 Right_Opnd => Relocate_Node (Arg2));
5440 end if;
5441 end if;
5442 end if;
5444 Next_Rep_Item (Ritem);
5445 end loop;
5446 end Add_Predicates;
5448 -- Start of processing for Build_Predicate_Function
5450 begin
5451 -- Initialize for construction of statement list
5453 Expr := Empty;
5455 -- Return if already built or if type does not have predicates
5457 if not Has_Predicates (Typ)
5458 or else Present (Predicate_Function (Typ))
5459 then
5460 return;
5461 end if;
5463 -- Add Predicates for the current type
5465 Add_Predicates;
5467 -- Add predicates for ancestor if present
5469 declare
5470 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
5471 begin
5472 if Present (Atyp) then
5473 Add_Call (Atyp);
5474 end if;
5475 end;
5477 -- If we have predicates, build the function
5479 if Present (Expr) then
5481 -- Build function declaration
5483 pragma Assert (Has_Predicates (Typ));
5484 SId :=
5485 Make_Defining_Identifier (Loc,
5486 Chars => New_External_Name (Chars (Typ), "Predicate"));
5487 Set_Has_Predicates (SId);
5488 Set_Predicate_Function (Typ, SId);
5490 -- The predicate function is shared between views of a type.
5492 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5493 Set_Predicate_Function (Full_View (Typ), SId);
5494 end if;
5496 Spec :=
5497 Make_Function_Specification (Loc,
5498 Defining_Unit_Name => SId,
5499 Parameter_Specifications => New_List (
5500 Make_Parameter_Specification (Loc,
5501 Defining_Identifier => Object_Entity,
5502 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
5503 Result_Definition =>
5504 New_Occurrence_Of (Standard_Boolean, Loc));
5506 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
5508 -- Build function body
5510 SId :=
5511 Make_Defining_Identifier (Loc,
5512 Chars => New_External_Name (Chars (Typ), "Predicate"));
5514 Spec :=
5515 Make_Function_Specification (Loc,
5516 Defining_Unit_Name => SId,
5517 Parameter_Specifications => New_List (
5518 Make_Parameter_Specification (Loc,
5519 Defining_Identifier =>
5520 Make_Defining_Identifier (Loc, Object_Name),
5521 Parameter_Type =>
5522 New_Occurrence_Of (Typ, Loc))),
5523 Result_Definition =>
5524 New_Occurrence_Of (Standard_Boolean, Loc));
5526 FBody :=
5527 Make_Subprogram_Body (Loc,
5528 Specification => Spec,
5529 Declarations => Empty_List,
5530 Handled_Statement_Sequence =>
5531 Make_Handled_Sequence_Of_Statements (Loc,
5532 Statements => New_List (
5533 Make_Simple_Return_Statement (Loc,
5534 Expression => Expr))));
5536 -- Insert declaration before freeze node and body after
5538 Insert_Before_And_Analyze (N, FDecl);
5539 Insert_After_And_Analyze (N, FBody);
5541 -- Deal with static predicate case
5543 if Ekind_In (Typ, E_Enumeration_Subtype,
5544 E_Modular_Integer_Subtype,
5545 E_Signed_Integer_Subtype)
5546 and then Is_Static_Subtype (Typ)
5547 and then not Dynamic_Predicate_Present
5548 then
5549 Build_Static_Predicate (Typ, Expr, Object_Name);
5551 if Present (Static_Predicate_Present)
5552 and No (Static_Predicate (Typ))
5553 then
5554 Error_Msg_F
5555 ("expression does not have required form for "
5556 & "static predicate",
5557 Next (First (Pragma_Argument_Associations
5558 (Static_Predicate_Present))));
5559 end if;
5560 end if;
5561 end if;
5562 end Build_Predicate_Function;
5564 ----------------------------
5565 -- Build_Static_Predicate --
5566 ----------------------------
5568 procedure Build_Static_Predicate
5569 (Typ : Entity_Id;
5570 Expr : Node_Id;
5571 Nam : Name_Id)
5573 Loc : constant Source_Ptr := Sloc (Expr);
5575 Non_Static : exception;
5576 -- Raised if something non-static is found
5578 Btyp : constant Entity_Id := Base_Type (Typ);
5580 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
5581 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
5582 -- Low bound and high bound value of base type of Typ
5584 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
5585 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
5586 -- Low bound and high bound values of static subtype Typ
5588 type REnt is record
5589 Lo, Hi : Uint;
5590 end record;
5591 -- One entry in a Rlist value, a single REnt (range entry) value
5592 -- denotes one range from Lo to Hi. To represent a single value
5593 -- range Lo = Hi = value.
5595 type RList is array (Nat range <>) of REnt;
5596 -- A list of ranges. The ranges are sorted in increasing order,
5597 -- and are disjoint (there is a gap of at least one value between
5598 -- each range in the table). A value is in the set of ranges in
5599 -- Rlist if it lies within one of these ranges
5601 False_Range : constant RList :=
5602 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
5603 -- An empty set of ranges represents a range list that can never be
5604 -- satisfied, since there are no ranges in which the value could lie,
5605 -- so it does not lie in any of them. False_Range is a canonical value
5606 -- for this empty set, but general processing should test for an Rlist
5607 -- with length zero (see Is_False predicate), since other null ranges
5608 -- may appear which must be treated as False.
5610 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
5611 -- Range representing True, value must be in the base range
5613 function "and" (Left, Right : RList) return RList;
5614 -- And's together two range lists, returning a range list. This is
5615 -- a set intersection operation.
5617 function "or" (Left, Right : RList) return RList;
5618 -- Or's together two range lists, returning a range list. This is a
5619 -- set union operation.
5621 function "not" (Right : RList) return RList;
5622 -- Returns complement of a given range list, i.e. a range list
5623 -- representing all the values in TLo .. THi that are not in the
5624 -- input operand Right.
5626 function Build_Val (V : Uint) return Node_Id;
5627 -- Return an analyzed N_Identifier node referencing this value, suitable
5628 -- for use as an entry in the Static_Predicate list. This node is typed
5629 -- with the base type.
5631 function Build_Range (Lo, Hi : Uint) return Node_Id;
5632 -- Return an analyzed N_Range node referencing this range, suitable
5633 -- for use as an entry in the Static_Predicate list. This node is typed
5634 -- with the base type.
5636 function Get_RList (Exp : Node_Id) return RList;
5637 -- This is a recursive routine that converts the given expression into
5638 -- a list of ranges, suitable for use in building the static predicate.
5640 function Is_False (R : RList) return Boolean;
5641 pragma Inline (Is_False);
5642 -- Returns True if the given range list is empty, and thus represents
5643 -- a False list of ranges that can never be satisfied.
5645 function Is_True (R : RList) return Boolean;
5646 -- Returns True if R trivially represents the True predicate by having
5647 -- a single range from BLo to BHi.
5649 function Is_Type_Ref (N : Node_Id) return Boolean;
5650 pragma Inline (Is_Type_Ref);
5651 -- Returns if True if N is a reference to the type for the predicate in
5652 -- the expression (i.e. if it is an identifier whose Chars field matches
5653 -- the Nam given in the call).
5655 function Lo_Val (N : Node_Id) return Uint;
5656 -- Given static expression or static range from a Static_Predicate list,
5657 -- gets expression value or low bound of range.
5659 function Hi_Val (N : Node_Id) return Uint;
5660 -- Given static expression or static range from a Static_Predicate list,
5661 -- gets expression value of high bound of range.
5663 function Membership_Entry (N : Node_Id) return RList;
5664 -- Given a single membership entry (range, value, or subtype), returns
5665 -- the corresponding range list. Raises Static_Error if not static.
5667 function Membership_Entries (N : Node_Id) return RList;
5668 -- Given an element on an alternatives list of a membership operation,
5669 -- returns the range list corresponding to this entry and all following
5670 -- entries (i.e. returns the "or" of this list of values).
5672 function Stat_Pred (Typ : Entity_Id) return RList;
5673 -- Given a type, if it has a static predicate, then return the predicate
5674 -- as a range list, otherwise raise Non_Static.
5676 -----------
5677 -- "and" --
5678 -----------
5680 function "and" (Left, Right : RList) return RList is
5681 FEnt : REnt;
5682 -- First range of result
5684 SLeft : Nat := Left'First;
5685 -- Start of rest of left entries
5687 SRight : Nat := Right'First;
5688 -- Start of rest of right entries
5690 begin
5691 -- If either range is True, return the other
5693 if Is_True (Left) then
5694 return Right;
5695 elsif Is_True (Right) then
5696 return Left;
5697 end if;
5699 -- If either range is False, return False
5701 if Is_False (Left) or else Is_False (Right) then
5702 return False_Range;
5703 end if;
5705 -- Loop to remove entries at start that are disjoint, and thus
5706 -- just get discarded from the result entirely.
5708 loop
5709 -- If no operands left in either operand, result is false
5711 if SLeft > Left'Last or else SRight > Right'Last then
5712 return False_Range;
5714 -- Discard first left operand entry if disjoint with right
5716 elsif Left (SLeft).Hi < Right (SRight).Lo then
5717 SLeft := SLeft + 1;
5719 -- Discard first right operand entry if disjoint with left
5721 elsif Right (SRight).Hi < Left (SLeft).Lo then
5722 SRight := SRight + 1;
5724 -- Otherwise we have an overlapping entry
5726 else
5727 exit;
5728 end if;
5729 end loop;
5731 -- Now we have two non-null operands, and first entries overlap.
5732 -- The first entry in the result will be the overlapping part of
5733 -- these two entries.
5735 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
5736 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
5738 -- Now we can remove the entry that ended at a lower value, since
5739 -- its contribution is entirely contained in Fent.
5741 if Left (SLeft).Hi <= Right (SRight).Hi then
5742 SLeft := SLeft + 1;
5743 else
5744 SRight := SRight + 1;
5745 end if;
5747 -- Compute result by concatenating this first entry with the "and"
5748 -- of the remaining parts of the left and right operands. Note that
5749 -- if either of these is empty, "and" will yield empty, so that we
5750 -- will end up with just Fent, which is what we want in that case.
5752 return
5753 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
5754 end "and";
5756 -----------
5757 -- "not" --
5758 -----------
5760 function "not" (Right : RList) return RList is
5761 begin
5762 -- Return True if False range
5764 if Is_False (Right) then
5765 return True_Range;
5766 end if;
5768 -- Return False if True range
5770 if Is_True (Right) then
5771 return False_Range;
5772 end if;
5774 -- Here if not trivial case
5776 declare
5777 Result : RList (1 .. Right'Length + 1);
5778 -- May need one more entry for gap at beginning and end
5780 Count : Nat := 0;
5781 -- Number of entries stored in Result
5783 begin
5784 -- Gap at start
5786 if Right (Right'First).Lo > TLo then
5787 Count := Count + 1;
5788 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
5789 end if;
5791 -- Gaps between ranges
5793 for J in Right'First .. Right'Last - 1 loop
5794 Count := Count + 1;
5795 Result (Count) :=
5796 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
5797 end loop;
5799 -- Gap at end
5801 if Right (Right'Last).Hi < THi then
5802 Count := Count + 1;
5803 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
5804 end if;
5806 return Result (1 .. Count);
5807 end;
5808 end "not";
5810 ----------
5811 -- "or" --
5812 ----------
5814 function "or" (Left, Right : RList) return RList is
5815 FEnt : REnt;
5816 -- First range of result
5818 SLeft : Nat := Left'First;
5819 -- Start of rest of left entries
5821 SRight : Nat := Right'First;
5822 -- Start of rest of right entries
5824 begin
5825 -- If either range is True, return True
5827 if Is_True (Left) or else Is_True (Right) then
5828 return True_Range;
5829 end if;
5831 -- If either range is False (empty), return the other
5833 if Is_False (Left) then
5834 return Right;
5835 elsif Is_False (Right) then
5836 return Left;
5837 end if;
5839 -- Initialize result first entry from left or right operand
5840 -- depending on which starts with the lower range.
5842 if Left (SLeft).Lo < Right (SRight).Lo then
5843 FEnt := Left (SLeft);
5844 SLeft := SLeft + 1;
5845 else
5846 FEnt := Right (SRight);
5847 SRight := SRight + 1;
5848 end if;
5850 -- This loop eats ranges from left and right operands that
5851 -- are contiguous with the first range we are gathering.
5853 loop
5854 -- Eat first entry in left operand if contiguous or
5855 -- overlapped by gathered first operand of result.
5857 if SLeft <= Left'Last
5858 and then Left (SLeft).Lo <= FEnt.Hi + 1
5859 then
5860 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
5861 SLeft := SLeft + 1;
5863 -- Eat first entry in right operand if contiguous or
5864 -- overlapped by gathered right operand of result.
5866 elsif SRight <= Right'Last
5867 and then Right (SRight).Lo <= FEnt.Hi + 1
5868 then
5869 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
5870 SRight := SRight + 1;
5872 -- All done if no more entries to eat!
5874 else
5875 exit;
5876 end if;
5877 end loop;
5879 -- Obtain result as the first entry we just computed, concatenated
5880 -- to the "or" of the remaining results (if one operand is empty,
5881 -- this will just concatenate with the other
5883 return
5884 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
5885 end "or";
5887 -----------------
5888 -- Build_Range --
5889 -----------------
5891 function Build_Range (Lo, Hi : Uint) return Node_Id is
5892 Result : Node_Id;
5893 begin
5894 if Lo = Hi then
5895 return Build_Val (Hi);
5896 else
5897 Result :=
5898 Make_Range (Loc,
5899 Low_Bound => Build_Val (Lo),
5900 High_Bound => Build_Val (Hi));
5901 Set_Etype (Result, Btyp);
5902 Set_Analyzed (Result);
5903 return Result;
5904 end if;
5905 end Build_Range;
5907 ---------------
5908 -- Build_Val --
5909 ---------------
5911 function Build_Val (V : Uint) return Node_Id is
5912 Result : Node_Id;
5914 begin
5915 if Is_Enumeration_Type (Typ) then
5916 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
5917 else
5918 Result := Make_Integer_Literal (Loc, V);
5919 end if;
5921 Set_Etype (Result, Btyp);
5922 Set_Is_Static_Expression (Result);
5923 Set_Analyzed (Result);
5924 return Result;
5925 end Build_Val;
5927 ---------------
5928 -- Get_RList --
5929 ---------------
5931 function Get_RList (Exp : Node_Id) return RList is
5932 Op : Node_Kind;
5933 Val : Uint;
5935 begin
5936 -- Static expression can only be true or false
5938 if Is_OK_Static_Expression (Exp) then
5940 -- For False
5942 if Expr_Value (Exp) = 0 then
5943 return False_Range;
5944 else
5945 return True_Range;
5946 end if;
5947 end if;
5949 -- Otherwise test node type
5951 Op := Nkind (Exp);
5953 case Op is
5955 -- And
5957 when N_Op_And | N_And_Then =>
5958 return Get_RList (Left_Opnd (Exp))
5960 Get_RList (Right_Opnd (Exp));
5962 -- Or
5964 when N_Op_Or | N_Or_Else =>
5965 return Get_RList (Left_Opnd (Exp))
5967 Get_RList (Right_Opnd (Exp));
5969 -- Not
5971 when N_Op_Not =>
5972 return not Get_RList (Right_Opnd (Exp));
5974 -- Comparisons of type with static value
5976 when N_Op_Compare =>
5977 -- Type is left operand
5979 if Is_Type_Ref (Left_Opnd (Exp))
5980 and then Is_OK_Static_Expression (Right_Opnd (Exp))
5981 then
5982 Val := Expr_Value (Right_Opnd (Exp));
5984 -- Typ is right operand
5986 elsif Is_Type_Ref (Right_Opnd (Exp))
5987 and then Is_OK_Static_Expression (Left_Opnd (Exp))
5988 then
5989 Val := Expr_Value (Left_Opnd (Exp));
5991 -- Invert sense of comparison
5993 case Op is
5994 when N_Op_Gt => Op := N_Op_Lt;
5995 when N_Op_Lt => Op := N_Op_Gt;
5996 when N_Op_Ge => Op := N_Op_Le;
5997 when N_Op_Le => Op := N_Op_Ge;
5998 when others => null;
5999 end case;
6001 -- Other cases are non-static
6003 else
6004 raise Non_Static;
6005 end if;
6007 -- Construct range according to comparison operation
6009 case Op is
6010 when N_Op_Eq =>
6011 return RList'(1 => REnt'(Val, Val));
6013 when N_Op_Ge =>
6014 return RList'(1 => REnt'(Val, BHi));
6016 when N_Op_Gt =>
6017 return RList'(1 => REnt'(Val + 1, BHi));
6019 when N_Op_Le =>
6020 return RList'(1 => REnt'(BLo, Val));
6022 when N_Op_Lt =>
6023 return RList'(1 => REnt'(BLo, Val - 1));
6025 when N_Op_Ne =>
6026 return RList'(REnt'(BLo, Val - 1),
6027 REnt'(Val + 1, BHi));
6029 when others =>
6030 raise Program_Error;
6031 end case;
6033 -- Membership (IN)
6035 when N_In =>
6036 if not Is_Type_Ref (Left_Opnd (Exp)) then
6037 raise Non_Static;
6038 end if;
6040 if Present (Right_Opnd (Exp)) then
6041 return Membership_Entry (Right_Opnd (Exp));
6042 else
6043 return Membership_Entries (First (Alternatives (Exp)));
6044 end if;
6046 -- Negative membership (NOT IN)
6048 when N_Not_In =>
6049 if not Is_Type_Ref (Left_Opnd (Exp)) then
6050 raise Non_Static;
6051 end if;
6053 if Present (Right_Opnd (Exp)) then
6054 return not Membership_Entry (Right_Opnd (Exp));
6055 else
6056 return not Membership_Entries (First (Alternatives (Exp)));
6057 end if;
6059 -- Function call, may be call to static predicate
6061 when N_Function_Call =>
6062 if Is_Entity_Name (Name (Exp)) then
6063 declare
6064 Ent : constant Entity_Id := Entity (Name (Exp));
6065 begin
6066 if Has_Predicates (Ent) then
6067 return Stat_Pred (Etype (First_Formal (Ent)));
6068 end if;
6069 end;
6070 end if;
6072 -- Other function call cases are non-static
6074 raise Non_Static;
6076 -- Qualified expression, dig out the expression
6078 when N_Qualified_Expression =>
6079 return Get_RList (Expression (Exp));
6081 -- Xor operator
6083 when N_Op_Xor =>
6084 return (Get_RList (Left_Opnd (Exp))
6085 and not Get_RList (Right_Opnd (Exp)))
6086 or (Get_RList (Right_Opnd (Exp))
6087 and not Get_RList (Left_Opnd (Exp)));
6089 -- Any other node type is non-static
6091 when others =>
6092 raise Non_Static;
6093 end case;
6094 end Get_RList;
6096 ------------
6097 -- Hi_Val --
6098 ------------
6100 function Hi_Val (N : Node_Id) return Uint is
6101 begin
6102 if Is_Static_Expression (N) then
6103 return Expr_Value (N);
6104 else
6105 pragma Assert (Nkind (N) = N_Range);
6106 return Expr_Value (High_Bound (N));
6107 end if;
6108 end Hi_Val;
6110 --------------
6111 -- Is_False --
6112 --------------
6114 function Is_False (R : RList) return Boolean is
6115 begin
6116 return R'Length = 0;
6117 end Is_False;
6119 -------------
6120 -- Is_True --
6121 -------------
6123 function Is_True (R : RList) return Boolean is
6124 begin
6125 return R'Length = 1
6126 and then R (R'First).Lo = BLo
6127 and then R (R'First).Hi = BHi;
6128 end Is_True;
6130 -----------------
6131 -- Is_Type_Ref --
6132 -----------------
6134 function Is_Type_Ref (N : Node_Id) return Boolean is
6135 begin
6136 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
6137 end Is_Type_Ref;
6139 ------------
6140 -- Lo_Val --
6141 ------------
6143 function Lo_Val (N : Node_Id) return Uint is
6144 begin
6145 if Is_Static_Expression (N) then
6146 return Expr_Value (N);
6147 else
6148 pragma Assert (Nkind (N) = N_Range);
6149 return Expr_Value (Low_Bound (N));
6150 end if;
6151 end Lo_Val;
6153 ------------------------
6154 -- Membership_Entries --
6155 ------------------------
6157 function Membership_Entries (N : Node_Id) return RList is
6158 begin
6159 if No (Next (N)) then
6160 return Membership_Entry (N);
6161 else
6162 return Membership_Entry (N) or Membership_Entries (Next (N));
6163 end if;
6164 end Membership_Entries;
6166 ----------------------
6167 -- Membership_Entry --
6168 ----------------------
6170 function Membership_Entry (N : Node_Id) return RList is
6171 Val : Uint;
6172 SLo : Uint;
6173 SHi : Uint;
6175 begin
6176 -- Range case
6178 if Nkind (N) = N_Range then
6179 if not Is_Static_Expression (Low_Bound (N))
6180 or else
6181 not Is_Static_Expression (High_Bound (N))
6182 then
6183 raise Non_Static;
6184 else
6185 SLo := Expr_Value (Low_Bound (N));
6186 SHi := Expr_Value (High_Bound (N));
6187 return RList'(1 => REnt'(SLo, SHi));
6188 end if;
6190 -- Static expression case
6192 elsif Is_Static_Expression (N) then
6193 Val := Expr_Value (N);
6194 return RList'(1 => REnt'(Val, Val));
6196 -- Identifier (other than static expression) case
6198 else pragma Assert (Nkind (N) = N_Identifier);
6200 -- Type case
6202 if Is_Type (Entity (N)) then
6204 -- If type has predicates, process them
6206 if Has_Predicates (Entity (N)) then
6207 return Stat_Pred (Entity (N));
6209 -- For static subtype without predicates, get range
6211 elsif Is_Static_Subtype (Entity (N)) then
6212 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
6213 SHi := Expr_Value (Type_High_Bound (Entity (N)));
6214 return RList'(1 => REnt'(SLo, SHi));
6216 -- Any other type makes us non-static
6218 else
6219 raise Non_Static;
6220 end if;
6222 -- Any other kind of identifier in predicate (e.g. a non-static
6223 -- expression value) means this is not a static predicate.
6225 else
6226 raise Non_Static;
6227 end if;
6228 end if;
6229 end Membership_Entry;
6231 ---------------
6232 -- Stat_Pred --
6233 ---------------
6235 function Stat_Pred (Typ : Entity_Id) return RList is
6236 begin
6237 -- Not static if type does not have static predicates
6239 if not Has_Predicates (Typ)
6240 or else No (Static_Predicate (Typ))
6241 then
6242 raise Non_Static;
6243 end if;
6245 -- Otherwise we convert the predicate list to a range list
6247 declare
6248 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
6249 P : Node_Id;
6251 begin
6252 P := First (Static_Predicate (Typ));
6253 for J in Result'Range loop
6254 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
6255 Next (P);
6256 end loop;
6258 return Result;
6259 end;
6260 end Stat_Pred;
6262 -- Start of processing for Build_Static_Predicate
6264 begin
6265 -- Now analyze the expression to see if it is a static predicate
6267 declare
6268 Ranges : constant RList := Get_RList (Expr);
6269 -- Range list from expression if it is static
6271 Plist : List_Id;
6273 begin
6274 -- Convert range list into a form for the static predicate. In the
6275 -- Ranges array, we just have raw ranges, these must be converted
6276 -- to properly typed and analyzed static expressions or range nodes.
6278 -- Note: here we limit ranges to the ranges of the subtype, so that
6279 -- a predicate is always false for values outside the subtype. That
6280 -- seems fine, such values are invalid anyway, and considering them
6281 -- to fail the predicate seems allowed and friendly, and furthermore
6282 -- simplifies processing for case statements and loops.
6284 Plist := New_List;
6286 for J in Ranges'Range loop
6287 declare
6288 Lo : Uint := Ranges (J).Lo;
6289 Hi : Uint := Ranges (J).Hi;
6291 begin
6292 -- Ignore completely out of range entry
6294 if Hi < TLo or else Lo > THi then
6295 null;
6297 -- Otherwise process entry
6299 else
6300 -- Adjust out of range value to subtype range
6302 if Lo < TLo then
6303 Lo := TLo;
6304 end if;
6306 if Hi > THi then
6307 Hi := THi;
6308 end if;
6310 -- Convert range into required form
6312 if Lo = Hi then
6313 Append_To (Plist, Build_Val (Lo));
6314 else
6315 Append_To (Plist, Build_Range (Lo, Hi));
6316 end if;
6317 end if;
6318 end;
6319 end loop;
6321 -- Processing was successful and all entries were static, so now we
6322 -- can store the result as the predicate list.
6324 Set_Static_Predicate (Typ, Plist);
6326 -- The processing for static predicates put the expression into
6327 -- canonical form as a series of ranges. It also eliminated
6328 -- duplicates and collapsed and combined ranges. We might as well
6329 -- replace the alternatives list of the right operand of the
6330 -- membership test with the static predicate list, which will
6331 -- usually be more efficient.
6333 declare
6334 New_Alts : constant List_Id := New_List;
6335 Old_Node : Node_Id;
6336 New_Node : Node_Id;
6338 begin
6339 Old_Node := First (Plist);
6340 while Present (Old_Node) loop
6341 New_Node := New_Copy (Old_Node);
6343 if Nkind (New_Node) = N_Range then
6344 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
6345 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
6346 end if;
6348 Append_To (New_Alts, New_Node);
6349 Next (Old_Node);
6350 end loop;
6352 -- If empty list, replace by False
6354 if Is_Empty_List (New_Alts) then
6355 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
6357 -- Else replace by set membership test
6359 else
6360 Rewrite (Expr,
6361 Make_In (Loc,
6362 Left_Opnd => Make_Identifier (Loc, Nam),
6363 Right_Opnd => Empty,
6364 Alternatives => New_Alts));
6366 -- Resolve new expression in function context
6368 Install_Formals (Predicate_Function (Typ));
6369 Push_Scope (Predicate_Function (Typ));
6370 Analyze_And_Resolve (Expr, Standard_Boolean);
6371 Pop_Scope;
6372 end if;
6373 end;
6374 end;
6376 -- If non-static, return doing nothing
6378 exception
6379 when Non_Static =>
6380 return;
6381 end Build_Static_Predicate;
6383 -----------------------------------------
6384 -- Check_Aspect_At_End_Of_Declarations --
6385 -----------------------------------------
6387 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
6388 Ent : constant Entity_Id := Entity (ASN);
6389 Ident : constant Node_Id := Identifier (ASN);
6390 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6392 End_Decl_Expr : constant Node_Id := Entity (Ident);
6393 -- Expression to be analyzed at end of declarations
6395 Freeze_Expr : constant Node_Id := Expression (ASN);
6396 -- Expression from call to Check_Aspect_At_Freeze_Point
6398 T : constant Entity_Id := Etype (Freeze_Expr);
6399 -- Type required for preanalyze call
6401 Err : Boolean;
6402 -- Set False if error
6404 -- On entry to this procedure, Entity (Ident) contains a copy of the
6405 -- original expression from the aspect, saved for this purpose, and
6406 -- but Expression (Ident) is a preanalyzed copy of the expression,
6407 -- preanalyzed just after the freeze point.
6409 procedure Check_Overloaded_Name;
6410 -- For aspects whose expression is simply a name, this routine checks if
6411 -- the name is overloaded or not. If so, it verifies there is an
6412 -- interpretation that matches the entity obtained at the freeze point,
6413 -- otherwise the compiler complains.
6415 ---------------------------
6416 -- Check_Overloaded_Name --
6417 ---------------------------
6419 procedure Check_Overloaded_Name is
6420 begin
6421 if not Is_Overloaded (End_Decl_Expr) then
6422 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
6424 else
6425 Err := True;
6427 declare
6428 Index : Interp_Index;
6429 It : Interp;
6431 begin
6432 Get_First_Interp (End_Decl_Expr, Index, It);
6433 while Present (It.Typ) loop
6434 if It.Nam = Entity (Freeze_Expr) then
6435 Err := False;
6436 exit;
6437 end if;
6439 Get_Next_Interp (Index, It);
6440 end loop;
6441 end;
6442 end if;
6443 end Check_Overloaded_Name;
6445 -- Start of processing for Check_Aspect_At_End_Of_Declarations
6447 begin
6448 -- Case of aspects Dimension, Dimension_System and Synchronization
6450 if A_Id = Aspect_Synchronization then
6451 return;
6453 -- Case of stream attributes, just have to compare entities. However,
6454 -- the expression is just a name (possibly overloaded), and there may
6455 -- be stream operations declared for unrelated types, so we just need
6456 -- to verify that one of these interpretations is the one available at
6457 -- at the freeze point.
6459 elsif A_Id = Aspect_Input or else
6460 A_Id = Aspect_Output or else
6461 A_Id = Aspect_Read or else
6462 A_Id = Aspect_Write
6463 then
6464 Analyze (End_Decl_Expr);
6465 Check_Overloaded_Name;
6467 elsif A_Id = Aspect_Variable_Indexing or else
6468 A_Id = Aspect_Constant_Indexing or else
6469 A_Id = Aspect_Default_Iterator or else
6470 A_Id = Aspect_Iterator_Element
6471 then
6472 -- Make type unfrozen before analysis, to prevent spurious errors
6473 -- about late attributes.
6475 Set_Is_Frozen (Ent, False);
6476 Analyze (End_Decl_Expr);
6477 Set_Is_Frozen (Ent, True);
6479 -- If the end of declarations comes before any other freeze
6480 -- point, the Freeze_Expr is not analyzed: no check needed.
6482 if Analyzed (Freeze_Expr) and then not In_Instance then
6483 Check_Overloaded_Name;
6484 else
6485 Err := False;
6486 end if;
6488 -- All other cases
6490 else
6491 -- In a generic context the aspect expressions have not been
6492 -- preanalyzed, so do it now. There are no conformance checks
6493 -- to perform in this case.
6495 if No (T) then
6496 Check_Aspect_At_Freeze_Point (ASN);
6497 return;
6499 -- The default values attributes may be defined in the private part,
6500 -- and the analysis of the expression may take place when only the
6501 -- partial view is visible. The expression must be scalar, so use
6502 -- the full view to resolve.
6504 elsif (A_Id = Aspect_Default_Value
6505 or else
6506 A_Id = Aspect_Default_Component_Value)
6507 and then Is_Private_Type (T)
6508 then
6509 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
6510 else
6511 Preanalyze_Spec_Expression (End_Decl_Expr, T);
6512 end if;
6514 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
6515 end if;
6517 -- Output error message if error
6519 if Err then
6520 Error_Msg_NE
6521 ("visibility of aspect for& changes after freeze point",
6522 ASN, Ent);
6523 Error_Msg_NE
6524 ("?info: & is frozen here, aspects evaluated at this point",
6525 Freeze_Node (Ent), Ent);
6526 end if;
6527 end Check_Aspect_At_End_Of_Declarations;
6529 ----------------------------------
6530 -- Check_Aspect_At_Freeze_Point --
6531 ----------------------------------
6533 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
6534 Ident : constant Node_Id := Identifier (ASN);
6535 -- Identifier (use Entity field to save expression)
6537 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6539 T : Entity_Id := Empty;
6540 -- Type required for preanalyze call
6542 begin
6543 -- On entry to this procedure, Entity (Ident) contains a copy of the
6544 -- original expression from the aspect, saved for this purpose.
6546 -- On exit from this procedure Entity (Ident) is unchanged, still
6547 -- containing that copy, but Expression (Ident) is a preanalyzed copy
6548 -- of the expression, preanalyzed just after the freeze point.
6550 -- Make a copy of the expression to be preanalyed
6552 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
6554 -- Find type for preanalyze call
6556 case A_Id is
6558 -- No_Aspect should be impossible
6560 when No_Aspect =>
6561 raise Program_Error;
6563 -- Aspects taking an optional boolean argument
6565 when Boolean_Aspects |
6566 Library_Unit_Aspects =>
6567 T := Standard_Boolean;
6569 when Aspect_Attach_Handler =>
6570 T := RTE (RE_Interrupt_ID);
6572 when Aspect_Convention =>
6573 return;
6575 -- Default_Value is resolved with the type entity in question
6577 when Aspect_Default_Value =>
6578 T := Entity (ASN);
6580 -- Default_Component_Value is resolved with the component type
6582 when Aspect_Default_Component_Value =>
6583 T := Component_Type (Entity (ASN));
6585 -- Aspects corresponding to attribute definition clauses
6587 when Aspect_Address =>
6588 T := RTE (RE_Address);
6590 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
6591 T := RTE (RE_Bit_Order);
6593 when Aspect_CPU =>
6594 T := RTE (RE_CPU_Range);
6596 when Aspect_Dispatching_Domain =>
6597 T := RTE (RE_Dispatching_Domain);
6599 when Aspect_External_Tag =>
6600 T := Standard_String;
6602 when Aspect_External_Name =>
6603 T := Standard_String;
6605 when Aspect_Link_Name =>
6606 T := Standard_String;
6608 when Aspect_Priority | Aspect_Interrupt_Priority =>
6609 T := Standard_Integer;
6611 when Aspect_Small =>
6612 T := Universal_Real;
6614 -- For a simple storage pool, we have to retrieve the type of the
6615 -- pool object associated with the aspect's corresponding attribute
6616 -- definition clause.
6618 when Aspect_Simple_Storage_Pool =>
6619 T := Etype (Expression (Aspect_Rep_Item (ASN)));
6621 when Aspect_Storage_Pool =>
6622 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
6624 when Aspect_Alignment |
6625 Aspect_Component_Size |
6626 Aspect_Machine_Radix |
6627 Aspect_Object_Size |
6628 Aspect_Size |
6629 Aspect_Storage_Size |
6630 Aspect_Stream_Size |
6631 Aspect_Value_Size =>
6632 T := Any_Integer;
6634 when Aspect_Synchronization =>
6635 return;
6637 -- Special case, the expression of these aspects is just an entity
6638 -- that does not need any resolution, so just analyze.
6640 when Aspect_Input |
6641 Aspect_Output |
6642 Aspect_Read |
6643 Aspect_Suppress |
6644 Aspect_Unsuppress |
6645 Aspect_Warnings |
6646 Aspect_Write =>
6647 Analyze (Expression (ASN));
6648 return;
6650 -- Same for Iterator aspects, where the expression is a function
6651 -- name. Legality rules are checked separately.
6653 when Aspect_Constant_Indexing |
6654 Aspect_Default_Iterator |
6655 Aspect_Iterator_Element |
6656 Aspect_Variable_Indexing =>
6657 Analyze (Expression (ASN));
6658 return;
6660 -- Invariant/Predicate take boolean expressions
6662 when Aspect_Dynamic_Predicate |
6663 Aspect_Invariant |
6664 Aspect_Predicate |
6665 Aspect_Static_Predicate |
6666 Aspect_Type_Invariant =>
6667 T := Standard_Boolean;
6669 -- Here is the list of aspects that don't require delay analysis.
6671 when Aspect_Contract_Case |
6672 Aspect_Dimension |
6673 Aspect_Dimension_System |
6674 Aspect_Implicit_Dereference |
6675 Aspect_Post |
6676 Aspect_Postcondition |
6677 Aspect_Pre |
6678 Aspect_Precondition |
6679 Aspect_Test_Case =>
6680 raise Program_Error;
6682 end case;
6684 -- Do the preanalyze call
6686 Preanalyze_Spec_Expression (Expression (ASN), T);
6687 end Check_Aspect_At_Freeze_Point;
6689 -----------------------------------
6690 -- Check_Constant_Address_Clause --
6691 -----------------------------------
6693 procedure Check_Constant_Address_Clause
6694 (Expr : Node_Id;
6695 U_Ent : Entity_Id)
6697 procedure Check_At_Constant_Address (Nod : Node_Id);
6698 -- Checks that the given node N represents a name whose 'Address is
6699 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
6700 -- address value is the same at the point of declaration of U_Ent and at
6701 -- the time of elaboration of the address clause.
6703 procedure Check_Expr_Constants (Nod : Node_Id);
6704 -- Checks that Nod meets the requirements for a constant address clause
6705 -- in the sense of the enclosing procedure.
6707 procedure Check_List_Constants (Lst : List_Id);
6708 -- Check that all elements of list Lst meet the requirements for a
6709 -- constant address clause in the sense of the enclosing procedure.
6711 -------------------------------
6712 -- Check_At_Constant_Address --
6713 -------------------------------
6715 procedure Check_At_Constant_Address (Nod : Node_Id) is
6716 begin
6717 if Is_Entity_Name (Nod) then
6718 if Present (Address_Clause (Entity ((Nod)))) then
6719 Error_Msg_NE
6720 ("invalid address clause for initialized object &!",
6721 Nod, U_Ent);
6722 Error_Msg_NE
6723 ("address for& cannot" &
6724 " depend on another address clause! (RM 13.1(22))!",
6725 Nod, U_Ent);
6727 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
6728 and then Sloc (U_Ent) < Sloc (Entity (Nod))
6729 then
6730 Error_Msg_NE
6731 ("invalid address clause for initialized object &!",
6732 Nod, U_Ent);
6733 Error_Msg_Node_2 := U_Ent;
6734 Error_Msg_NE
6735 ("\& must be defined before & (RM 13.1(22))!",
6736 Nod, Entity (Nod));
6737 end if;
6739 elsif Nkind (Nod) = N_Selected_Component then
6740 declare
6741 T : constant Entity_Id := Etype (Prefix (Nod));
6743 begin
6744 if (Is_Record_Type (T)
6745 and then Has_Discriminants (T))
6746 or else
6747 (Is_Access_Type (T)
6748 and then Is_Record_Type (Designated_Type (T))
6749 and then Has_Discriminants (Designated_Type (T)))
6750 then
6751 Error_Msg_NE
6752 ("invalid address clause for initialized object &!",
6753 Nod, U_Ent);
6754 Error_Msg_N
6755 ("\address cannot depend on component" &
6756 " of discriminated record (RM 13.1(22))!",
6757 Nod);
6758 else
6759 Check_At_Constant_Address (Prefix (Nod));
6760 end if;
6761 end;
6763 elsif Nkind (Nod) = N_Indexed_Component then
6764 Check_At_Constant_Address (Prefix (Nod));
6765 Check_List_Constants (Expressions (Nod));
6767 else
6768 Check_Expr_Constants (Nod);
6769 end if;
6770 end Check_At_Constant_Address;
6772 --------------------------
6773 -- Check_Expr_Constants --
6774 --------------------------
6776 procedure Check_Expr_Constants (Nod : Node_Id) is
6777 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
6778 Ent : Entity_Id := Empty;
6780 begin
6781 if Nkind (Nod) in N_Has_Etype
6782 and then Etype (Nod) = Any_Type
6783 then
6784 return;
6785 end if;
6787 case Nkind (Nod) is
6788 when N_Empty | N_Error =>
6789 return;
6791 when N_Identifier | N_Expanded_Name =>
6792 Ent := Entity (Nod);
6794 -- We need to look at the original node if it is different
6795 -- from the node, since we may have rewritten things and
6796 -- substituted an identifier representing the rewrite.
6798 if Original_Node (Nod) /= Nod then
6799 Check_Expr_Constants (Original_Node (Nod));
6801 -- If the node is an object declaration without initial
6802 -- value, some code has been expanded, and the expression
6803 -- is not constant, even if the constituents might be
6804 -- acceptable, as in A'Address + offset.
6806 if Ekind (Ent) = E_Variable
6807 and then
6808 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
6809 and then
6810 No (Expression (Declaration_Node (Ent)))
6811 then
6812 Error_Msg_NE
6813 ("invalid address clause for initialized object &!",
6814 Nod, U_Ent);
6816 -- If entity is constant, it may be the result of expanding
6817 -- a check. We must verify that its declaration appears
6818 -- before the object in question, else we also reject the
6819 -- address clause.
6821 elsif Ekind (Ent) = E_Constant
6822 and then In_Same_Source_Unit (Ent, U_Ent)
6823 and then Sloc (Ent) > Loc_U_Ent
6824 then
6825 Error_Msg_NE
6826 ("invalid address clause for initialized object &!",
6827 Nod, U_Ent);
6828 end if;
6830 return;
6831 end if;
6833 -- Otherwise look at the identifier and see if it is OK
6835 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
6836 or else Is_Type (Ent)
6837 then
6838 return;
6840 elsif
6841 Ekind (Ent) = E_Constant
6842 or else
6843 Ekind (Ent) = E_In_Parameter
6844 then
6845 -- This is the case where we must have Ent defined before
6846 -- U_Ent. Clearly if they are in different units this
6847 -- requirement is met since the unit containing Ent is
6848 -- already processed.
6850 if not In_Same_Source_Unit (Ent, U_Ent) then
6851 return;
6853 -- Otherwise location of Ent must be before the location
6854 -- of U_Ent, that's what prior defined means.
6856 elsif Sloc (Ent) < Loc_U_Ent then
6857 return;
6859 else
6860 Error_Msg_NE
6861 ("invalid address clause for initialized object &!",
6862 Nod, U_Ent);
6863 Error_Msg_Node_2 := U_Ent;
6864 Error_Msg_NE
6865 ("\& must be defined before & (RM 13.1(22))!",
6866 Nod, Ent);
6867 end if;
6869 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
6870 Check_Expr_Constants (Original_Node (Nod));
6872 else
6873 Error_Msg_NE
6874 ("invalid address clause for initialized object &!",
6875 Nod, U_Ent);
6877 if Comes_From_Source (Ent) then
6878 Error_Msg_NE
6879 ("\reference to variable& not allowed"
6880 & " (RM 13.1(22))!", Nod, Ent);
6881 else
6882 Error_Msg_N
6883 ("non-static expression not allowed"
6884 & " (RM 13.1(22))!", Nod);
6885 end if;
6886 end if;
6888 when N_Integer_Literal =>
6890 -- If this is a rewritten unchecked conversion, in a system
6891 -- where Address is an integer type, always use the base type
6892 -- for a literal value. This is user-friendly and prevents
6893 -- order-of-elaboration issues with instances of unchecked
6894 -- conversion.
6896 if Nkind (Original_Node (Nod)) = N_Function_Call then
6897 Set_Etype (Nod, Base_Type (Etype (Nod)));
6898 end if;
6900 when N_Real_Literal |
6901 N_String_Literal |
6902 N_Character_Literal =>
6903 return;
6905 when N_Range =>
6906 Check_Expr_Constants (Low_Bound (Nod));
6907 Check_Expr_Constants (High_Bound (Nod));
6909 when N_Explicit_Dereference =>
6910 Check_Expr_Constants (Prefix (Nod));
6912 when N_Indexed_Component =>
6913 Check_Expr_Constants (Prefix (Nod));
6914 Check_List_Constants (Expressions (Nod));
6916 when N_Slice =>
6917 Check_Expr_Constants (Prefix (Nod));
6918 Check_Expr_Constants (Discrete_Range (Nod));
6920 when N_Selected_Component =>
6921 Check_Expr_Constants (Prefix (Nod));
6923 when N_Attribute_Reference =>
6924 if Attribute_Name (Nod) = Name_Address
6925 or else
6926 Attribute_Name (Nod) = Name_Access
6927 or else
6928 Attribute_Name (Nod) = Name_Unchecked_Access
6929 or else
6930 Attribute_Name (Nod) = Name_Unrestricted_Access
6931 then
6932 Check_At_Constant_Address (Prefix (Nod));
6934 else
6935 Check_Expr_Constants (Prefix (Nod));
6936 Check_List_Constants (Expressions (Nod));
6937 end if;
6939 when N_Aggregate =>
6940 Check_List_Constants (Component_Associations (Nod));
6941 Check_List_Constants (Expressions (Nod));
6943 when N_Component_Association =>
6944 Check_Expr_Constants (Expression (Nod));
6946 when N_Extension_Aggregate =>
6947 Check_Expr_Constants (Ancestor_Part (Nod));
6948 Check_List_Constants (Component_Associations (Nod));
6949 Check_List_Constants (Expressions (Nod));
6951 when N_Null =>
6952 return;
6954 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
6955 Check_Expr_Constants (Left_Opnd (Nod));
6956 Check_Expr_Constants (Right_Opnd (Nod));
6958 when N_Unary_Op =>
6959 Check_Expr_Constants (Right_Opnd (Nod));
6961 when N_Type_Conversion |
6962 N_Qualified_Expression |
6963 N_Allocator =>
6964 Check_Expr_Constants (Expression (Nod));
6966 when N_Unchecked_Type_Conversion =>
6967 Check_Expr_Constants (Expression (Nod));
6969 -- If this is a rewritten unchecked conversion, subtypes in
6970 -- this node are those created within the instance. To avoid
6971 -- order of elaboration issues, replace them with their base
6972 -- types. Note that address clauses can cause order of
6973 -- elaboration problems because they are elaborated by the
6974 -- back-end at the point of definition, and may mention
6975 -- entities declared in between (as long as everything is
6976 -- static). It is user-friendly to allow unchecked conversions
6977 -- in this context.
6979 if Nkind (Original_Node (Nod)) = N_Function_Call then
6980 Set_Etype (Expression (Nod),
6981 Base_Type (Etype (Expression (Nod))));
6982 Set_Etype (Nod, Base_Type (Etype (Nod)));
6983 end if;
6985 when N_Function_Call =>
6986 if not Is_Pure (Entity (Name (Nod))) then
6987 Error_Msg_NE
6988 ("invalid address clause for initialized object &!",
6989 Nod, U_Ent);
6991 Error_Msg_NE
6992 ("\function & is not pure (RM 13.1(22))!",
6993 Nod, Entity (Name (Nod)));
6995 else
6996 Check_List_Constants (Parameter_Associations (Nod));
6997 end if;
6999 when N_Parameter_Association =>
7000 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7002 when others =>
7003 Error_Msg_NE
7004 ("invalid address clause for initialized object &!",
7005 Nod, U_Ent);
7006 Error_Msg_NE
7007 ("\must be constant defined before& (RM 13.1(22))!",
7008 Nod, U_Ent);
7009 end case;
7010 end Check_Expr_Constants;
7012 --------------------------
7013 -- Check_List_Constants --
7014 --------------------------
7016 procedure Check_List_Constants (Lst : List_Id) is
7017 Nod1 : Node_Id;
7019 begin
7020 if Present (Lst) then
7021 Nod1 := First (Lst);
7022 while Present (Nod1) loop
7023 Check_Expr_Constants (Nod1);
7024 Next (Nod1);
7025 end loop;
7026 end if;
7027 end Check_List_Constants;
7029 -- Start of processing for Check_Constant_Address_Clause
7031 begin
7032 -- If rep_clauses are to be ignored, no need for legality checks. In
7033 -- particular, no need to pester user about rep clauses that violate
7034 -- the rule on constant addresses, given that these clauses will be
7035 -- removed by Freeze before they reach the back end.
7037 if not Ignore_Rep_Clauses then
7038 Check_Expr_Constants (Expr);
7039 end if;
7040 end Check_Constant_Address_Clause;
7042 ----------------------------------------
7043 -- Check_Record_Representation_Clause --
7044 ----------------------------------------
7046 procedure Check_Record_Representation_Clause (N : Node_Id) is
7047 Loc : constant Source_Ptr := Sloc (N);
7048 Ident : constant Node_Id := Identifier (N);
7049 Rectype : Entity_Id;
7050 Fent : Entity_Id;
7051 CC : Node_Id;
7052 Fbit : Uint;
7053 Lbit : Uint;
7054 Hbit : Uint := Uint_0;
7055 Comp : Entity_Id;
7056 Pcomp : Entity_Id;
7058 Max_Bit_So_Far : Uint;
7059 -- Records the maximum bit position so far. If all field positions
7060 -- are monotonically increasing, then we can skip the circuit for
7061 -- checking for overlap, since no overlap is possible.
7063 Tagged_Parent : Entity_Id := Empty;
7064 -- This is set in the case of a derived tagged type for which we have
7065 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
7066 -- positioned by record representation clauses). In this case we must
7067 -- check for overlap between components of this tagged type, and the
7068 -- components of its parent. Tagged_Parent will point to this parent
7069 -- type. For all other cases Tagged_Parent is left set to Empty.
7071 Parent_Last_Bit : Uint;
7072 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
7073 -- last bit position for any field in the parent type. We only need to
7074 -- check overlap for fields starting below this point.
7076 Overlap_Check_Required : Boolean;
7077 -- Used to keep track of whether or not an overlap check is required
7079 Overlap_Detected : Boolean := False;
7080 -- Set True if an overlap is detected
7082 Ccount : Natural := 0;
7083 -- Number of component clauses in record rep clause
7085 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
7086 -- Given two entities for record components or discriminants, checks
7087 -- if they have overlapping component clauses and issues errors if so.
7089 procedure Find_Component;
7090 -- Finds component entity corresponding to current component clause (in
7091 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
7092 -- start/stop bits for the field. If there is no matching component or
7093 -- if the matching component does not have a component clause, then
7094 -- that's an error and Comp is set to Empty, but no error message is
7095 -- issued, since the message was already given. Comp is also set to
7096 -- Empty if the current "component clause" is in fact a pragma.
7098 -----------------------------
7099 -- Check_Component_Overlap --
7100 -----------------------------
7102 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
7103 CC1 : constant Node_Id := Component_Clause (C1_Ent);
7104 CC2 : constant Node_Id := Component_Clause (C2_Ent);
7106 begin
7107 if Present (CC1) and then Present (CC2) then
7109 -- Exclude odd case where we have two tag fields in the same
7110 -- record, both at location zero. This seems a bit strange, but
7111 -- it seems to happen in some circumstances, perhaps on an error.
7113 if Chars (C1_Ent) = Name_uTag
7114 and then
7115 Chars (C2_Ent) = Name_uTag
7116 then
7117 return;
7118 end if;
7120 -- Here we check if the two fields overlap
7122 declare
7123 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
7124 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
7125 E1 : constant Uint := S1 + Esize (C1_Ent);
7126 E2 : constant Uint := S2 + Esize (C2_Ent);
7128 begin
7129 if E2 <= S1 or else E1 <= S2 then
7130 null;
7131 else
7132 Error_Msg_Node_2 := Component_Name (CC2);
7133 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7134 Error_Msg_Node_1 := Component_Name (CC1);
7135 Error_Msg_N
7136 ("component& overlaps & #", Component_Name (CC1));
7137 Overlap_Detected := True;
7138 end if;
7139 end;
7140 end if;
7141 end Check_Component_Overlap;
7143 --------------------
7144 -- Find_Component --
7145 --------------------
7147 procedure Find_Component is
7149 procedure Search_Component (R : Entity_Id);
7150 -- Search components of R for a match. If found, Comp is set.
7152 ----------------------
7153 -- Search_Component --
7154 ----------------------
7156 procedure Search_Component (R : Entity_Id) is
7157 begin
7158 Comp := First_Component_Or_Discriminant (R);
7159 while Present (Comp) loop
7161 -- Ignore error of attribute name for component name (we
7162 -- already gave an error message for this, so no need to
7163 -- complain here)
7165 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
7166 null;
7167 else
7168 exit when Chars (Comp) = Chars (Component_Name (CC));
7169 end if;
7171 Next_Component_Or_Discriminant (Comp);
7172 end loop;
7173 end Search_Component;
7175 -- Start of processing for Find_Component
7177 begin
7178 -- Return with Comp set to Empty if we have a pragma
7180 if Nkind (CC) = N_Pragma then
7181 Comp := Empty;
7182 return;
7183 end if;
7185 -- Search current record for matching component
7187 Search_Component (Rectype);
7189 -- If not found, maybe component of base type that is absent from
7190 -- statically constrained first subtype.
7192 if No (Comp) then
7193 Search_Component (Base_Type (Rectype));
7194 end if;
7196 -- If no component, or the component does not reference the component
7197 -- clause in question, then there was some previous error for which
7198 -- we already gave a message, so just return with Comp Empty.
7200 if No (Comp)
7201 or else Component_Clause (Comp) /= CC
7202 then
7203 Comp := Empty;
7205 -- Normal case where we have a component clause
7207 else
7208 Fbit := Component_Bit_Offset (Comp);
7209 Lbit := Fbit + Esize (Comp) - 1;
7210 end if;
7211 end Find_Component;
7213 -- Start of processing for Check_Record_Representation_Clause
7215 begin
7216 Find_Type (Ident);
7217 Rectype := Entity (Ident);
7219 if Rectype = Any_Type then
7220 return;
7221 else
7222 Rectype := Underlying_Type (Rectype);
7223 end if;
7225 -- See if we have a fully repped derived tagged type
7227 declare
7228 PS : constant Entity_Id := Parent_Subtype (Rectype);
7230 begin
7231 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
7232 Tagged_Parent := PS;
7234 -- Find maximum bit of any component of the parent type
7236 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
7237 Pcomp := First_Entity (Tagged_Parent);
7238 while Present (Pcomp) loop
7239 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
7240 if Component_Bit_Offset (Pcomp) /= No_Uint
7241 and then Known_Static_Esize (Pcomp)
7242 then
7243 Parent_Last_Bit :=
7244 UI_Max
7245 (Parent_Last_Bit,
7246 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
7247 end if;
7249 Next_Entity (Pcomp);
7250 end if;
7251 end loop;
7252 end if;
7253 end;
7255 -- All done if no component clauses
7257 CC := First (Component_Clauses (N));
7259 if No (CC) then
7260 return;
7261 end if;
7263 -- If a tag is present, then create a component clause that places it
7264 -- at the start of the record (otherwise gigi may place it after other
7265 -- fields that have rep clauses).
7267 Fent := First_Entity (Rectype);
7269 if Nkind (Fent) = N_Defining_Identifier
7270 and then Chars (Fent) = Name_uTag
7271 then
7272 Set_Component_Bit_Offset (Fent, Uint_0);
7273 Set_Normalized_Position (Fent, Uint_0);
7274 Set_Normalized_First_Bit (Fent, Uint_0);
7275 Set_Normalized_Position_Max (Fent, Uint_0);
7276 Init_Esize (Fent, System_Address_Size);
7278 Set_Component_Clause (Fent,
7279 Make_Component_Clause (Loc,
7280 Component_Name => Make_Identifier (Loc, Name_uTag),
7282 Position => Make_Integer_Literal (Loc, Uint_0),
7283 First_Bit => Make_Integer_Literal (Loc, Uint_0),
7284 Last_Bit =>
7285 Make_Integer_Literal (Loc,
7286 UI_From_Int (System_Address_Size))));
7288 Ccount := Ccount + 1;
7289 end if;
7291 Max_Bit_So_Far := Uint_Minus_1;
7292 Overlap_Check_Required := False;
7294 -- Process the component clauses
7296 while Present (CC) loop
7297 Find_Component;
7299 if Present (Comp) then
7300 Ccount := Ccount + 1;
7302 -- We need a full overlap check if record positions non-monotonic
7304 if Fbit <= Max_Bit_So_Far then
7305 Overlap_Check_Required := True;
7306 end if;
7308 Max_Bit_So_Far := Lbit;
7310 -- Check bit position out of range of specified size
7312 if Has_Size_Clause (Rectype)
7313 and then RM_Size (Rectype) <= Lbit
7314 then
7315 Error_Msg_N
7316 ("bit number out of range of specified size",
7317 Last_Bit (CC));
7319 -- Check for overlap with tag field
7321 else
7322 if Is_Tagged_Type (Rectype)
7323 and then Fbit < System_Address_Size
7324 then
7325 Error_Msg_NE
7326 ("component overlaps tag field of&",
7327 Component_Name (CC), Rectype);
7328 Overlap_Detected := True;
7329 end if;
7331 if Hbit < Lbit then
7332 Hbit := Lbit;
7333 end if;
7334 end if;
7336 -- Check parent overlap if component might overlap parent field
7338 if Present (Tagged_Parent)
7339 and then Fbit <= Parent_Last_Bit
7340 then
7341 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
7342 while Present (Pcomp) loop
7343 if not Is_Tag (Pcomp)
7344 and then Chars (Pcomp) /= Name_uParent
7345 then
7346 Check_Component_Overlap (Comp, Pcomp);
7347 end if;
7349 Next_Component_Or_Discriminant (Pcomp);
7350 end loop;
7351 end if;
7352 end if;
7354 Next (CC);
7355 end loop;
7357 -- Now that we have processed all the component clauses, check for
7358 -- overlap. We have to leave this till last, since the components can
7359 -- appear in any arbitrary order in the representation clause.
7361 -- We do not need this check if all specified ranges were monotonic,
7362 -- as recorded by Overlap_Check_Required being False at this stage.
7364 -- This first section checks if there are any overlapping entries at
7365 -- all. It does this by sorting all entries and then seeing if there are
7366 -- any overlaps. If there are none, then that is decisive, but if there
7367 -- are overlaps, they may still be OK (they may result from fields in
7368 -- different variants).
7370 if Overlap_Check_Required then
7371 Overlap_Check1 : declare
7373 OC_Fbit : array (0 .. Ccount) of Uint;
7374 -- First-bit values for component clauses, the value is the offset
7375 -- of the first bit of the field from start of record. The zero
7376 -- entry is for use in sorting.
7378 OC_Lbit : array (0 .. Ccount) of Uint;
7379 -- Last-bit values for component clauses, the value is the offset
7380 -- of the last bit of the field from start of record. The zero
7381 -- entry is for use in sorting.
7383 OC_Count : Natural := 0;
7384 -- Count of entries in OC_Fbit and OC_Lbit
7386 function OC_Lt (Op1, Op2 : Natural) return Boolean;
7387 -- Compare routine for Sort
7389 procedure OC_Move (From : Natural; To : Natural);
7390 -- Move routine for Sort
7392 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
7394 -----------
7395 -- OC_Lt --
7396 -----------
7398 function OC_Lt (Op1, Op2 : Natural) return Boolean is
7399 begin
7400 return OC_Fbit (Op1) < OC_Fbit (Op2);
7401 end OC_Lt;
7403 -------------
7404 -- OC_Move --
7405 -------------
7407 procedure OC_Move (From : Natural; To : Natural) is
7408 begin
7409 OC_Fbit (To) := OC_Fbit (From);
7410 OC_Lbit (To) := OC_Lbit (From);
7411 end OC_Move;
7413 -- Start of processing for Overlap_Check
7415 begin
7416 CC := First (Component_Clauses (N));
7417 while Present (CC) loop
7419 -- Exclude component clause already marked in error
7421 if not Error_Posted (CC) then
7422 Find_Component;
7424 if Present (Comp) then
7425 OC_Count := OC_Count + 1;
7426 OC_Fbit (OC_Count) := Fbit;
7427 OC_Lbit (OC_Count) := Lbit;
7428 end if;
7429 end if;
7431 Next (CC);
7432 end loop;
7434 Sorting.Sort (OC_Count);
7436 Overlap_Check_Required := False;
7437 for J in 1 .. OC_Count - 1 loop
7438 if OC_Lbit (J) >= OC_Fbit (J + 1) then
7439 Overlap_Check_Required := True;
7440 exit;
7441 end if;
7442 end loop;
7443 end Overlap_Check1;
7444 end if;
7446 -- If Overlap_Check_Required is still True, then we have to do the full
7447 -- scale overlap check, since we have at least two fields that do
7448 -- overlap, and we need to know if that is OK since they are in
7449 -- different variant, or whether we have a definite problem.
7451 if Overlap_Check_Required then
7452 Overlap_Check2 : declare
7453 C1_Ent, C2_Ent : Entity_Id;
7454 -- Entities of components being checked for overlap
7456 Clist : Node_Id;
7457 -- Component_List node whose Component_Items are being checked
7459 Citem : Node_Id;
7460 -- Component declaration for component being checked
7462 begin
7463 C1_Ent := First_Entity (Base_Type (Rectype));
7465 -- Loop through all components in record. For each component check
7466 -- for overlap with any of the preceding elements on the component
7467 -- list containing the component and also, if the component is in
7468 -- a variant, check against components outside the case structure.
7469 -- This latter test is repeated recursively up the variant tree.
7471 Main_Component_Loop : while Present (C1_Ent) loop
7472 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
7473 goto Continue_Main_Component_Loop;
7474 end if;
7476 -- Skip overlap check if entity has no declaration node. This
7477 -- happens with discriminants in constrained derived types.
7478 -- Possibly we are missing some checks as a result, but that
7479 -- does not seem terribly serious.
7481 if No (Declaration_Node (C1_Ent)) then
7482 goto Continue_Main_Component_Loop;
7483 end if;
7485 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
7487 -- Loop through component lists that need checking. Check the
7488 -- current component list and all lists in variants above us.
7490 Component_List_Loop : loop
7492 -- If derived type definition, go to full declaration
7493 -- If at outer level, check discriminants if there are any.
7495 if Nkind (Clist) = N_Derived_Type_Definition then
7496 Clist := Parent (Clist);
7497 end if;
7499 -- Outer level of record definition, check discriminants
7501 if Nkind_In (Clist, N_Full_Type_Declaration,
7502 N_Private_Type_Declaration)
7503 then
7504 if Has_Discriminants (Defining_Identifier (Clist)) then
7505 C2_Ent :=
7506 First_Discriminant (Defining_Identifier (Clist));
7507 while Present (C2_Ent) loop
7508 exit when C1_Ent = C2_Ent;
7509 Check_Component_Overlap (C1_Ent, C2_Ent);
7510 Next_Discriminant (C2_Ent);
7511 end loop;
7512 end if;
7514 -- Record extension case
7516 elsif Nkind (Clist) = N_Derived_Type_Definition then
7517 Clist := Empty;
7519 -- Otherwise check one component list
7521 else
7522 Citem := First (Component_Items (Clist));
7523 while Present (Citem) loop
7524 if Nkind (Citem) = N_Component_Declaration then
7525 C2_Ent := Defining_Identifier (Citem);
7526 exit when C1_Ent = C2_Ent;
7527 Check_Component_Overlap (C1_Ent, C2_Ent);
7528 end if;
7530 Next (Citem);
7531 end loop;
7532 end if;
7534 -- Check for variants above us (the parent of the Clist can
7535 -- be a variant, in which case its parent is a variant part,
7536 -- and the parent of the variant part is a component list
7537 -- whose components must all be checked against the current
7538 -- component for overlap).
7540 if Nkind (Parent (Clist)) = N_Variant then
7541 Clist := Parent (Parent (Parent (Clist)));
7543 -- Check for possible discriminant part in record, this
7544 -- is treated essentially as another level in the
7545 -- recursion. For this case the parent of the component
7546 -- list is the record definition, and its parent is the
7547 -- full type declaration containing the discriminant
7548 -- specifications.
7550 elsif Nkind (Parent (Clist)) = N_Record_Definition then
7551 Clist := Parent (Parent ((Clist)));
7553 -- If neither of these two cases, we are at the top of
7554 -- the tree.
7556 else
7557 exit Component_List_Loop;
7558 end if;
7559 end loop Component_List_Loop;
7561 <<Continue_Main_Component_Loop>>
7562 Next_Entity (C1_Ent);
7564 end loop Main_Component_Loop;
7565 end Overlap_Check2;
7566 end if;
7568 -- The following circuit deals with warning on record holes (gaps). We
7569 -- skip this check if overlap was detected, since it makes sense for the
7570 -- programmer to fix this illegality before worrying about warnings.
7572 if not Overlap_Detected and Warn_On_Record_Holes then
7573 Record_Hole_Check : declare
7574 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
7575 -- Full declaration of record type
7577 procedure Check_Component_List
7578 (CL : Node_Id;
7579 Sbit : Uint;
7580 DS : List_Id);
7581 -- Check component list CL for holes. The starting bit should be
7582 -- Sbit. which is zero for the main record component list and set
7583 -- appropriately for recursive calls for variants. DS is set to
7584 -- a list of discriminant specifications to be included in the
7585 -- consideration of components. It is No_List if none to consider.
7587 --------------------------
7588 -- Check_Component_List --
7589 --------------------------
7591 procedure Check_Component_List
7592 (CL : Node_Id;
7593 Sbit : Uint;
7594 DS : List_Id)
7596 Compl : Integer;
7598 begin
7599 Compl := Integer (List_Length (Component_Items (CL)));
7601 if DS /= No_List then
7602 Compl := Compl + Integer (List_Length (DS));
7603 end if;
7605 declare
7606 Comps : array (Natural range 0 .. Compl) of Entity_Id;
7607 -- Gather components (zero entry is for sort routine)
7609 Ncomps : Natural := 0;
7610 -- Number of entries stored in Comps (starting at Comps (1))
7612 Citem : Node_Id;
7613 -- One component item or discriminant specification
7615 Nbit : Uint;
7616 -- Starting bit for next component
7618 CEnt : Entity_Id;
7619 -- Component entity
7621 Variant : Node_Id;
7622 -- One variant
7624 function Lt (Op1, Op2 : Natural) return Boolean;
7625 -- Compare routine for Sort
7627 procedure Move (From : Natural; To : Natural);
7628 -- Move routine for Sort
7630 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
7632 --------
7633 -- Lt --
7634 --------
7636 function Lt (Op1, Op2 : Natural) return Boolean is
7637 begin
7638 return Component_Bit_Offset (Comps (Op1))
7640 Component_Bit_Offset (Comps (Op2));
7641 end Lt;
7643 ----------
7644 -- Move --
7645 ----------
7647 procedure Move (From : Natural; To : Natural) is
7648 begin
7649 Comps (To) := Comps (From);
7650 end Move;
7652 begin
7653 -- Gather discriminants into Comp
7655 if DS /= No_List then
7656 Citem := First (DS);
7657 while Present (Citem) loop
7658 if Nkind (Citem) = N_Discriminant_Specification then
7659 declare
7660 Ent : constant Entity_Id :=
7661 Defining_Identifier (Citem);
7662 begin
7663 if Ekind (Ent) = E_Discriminant then
7664 Ncomps := Ncomps + 1;
7665 Comps (Ncomps) := Ent;
7666 end if;
7667 end;
7668 end if;
7670 Next (Citem);
7671 end loop;
7672 end if;
7674 -- Gather component entities into Comp
7676 Citem := First (Component_Items (CL));
7677 while Present (Citem) loop
7678 if Nkind (Citem) = N_Component_Declaration then
7679 Ncomps := Ncomps + 1;
7680 Comps (Ncomps) := Defining_Identifier (Citem);
7681 end if;
7683 Next (Citem);
7684 end loop;
7686 -- Now sort the component entities based on the first bit.
7687 -- Note we already know there are no overlapping components.
7689 Sorting.Sort (Ncomps);
7691 -- Loop through entries checking for holes
7693 Nbit := Sbit;
7694 for J in 1 .. Ncomps loop
7695 CEnt := Comps (J);
7696 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
7698 if Error_Msg_Uint_1 > 0 then
7699 Error_Msg_NE
7700 ("?^-bit gap before component&",
7701 Component_Name (Component_Clause (CEnt)), CEnt);
7702 end if;
7704 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
7705 end loop;
7707 -- Process variant parts recursively if present
7709 if Present (Variant_Part (CL)) then
7710 Variant := First (Variants (Variant_Part (CL)));
7711 while Present (Variant) loop
7712 Check_Component_List
7713 (Component_List (Variant), Nbit, No_List);
7714 Next (Variant);
7715 end loop;
7716 end if;
7717 end;
7718 end Check_Component_List;
7720 -- Start of processing for Record_Hole_Check
7722 begin
7723 declare
7724 Sbit : Uint;
7726 begin
7727 if Is_Tagged_Type (Rectype) then
7728 Sbit := UI_From_Int (System_Address_Size);
7729 else
7730 Sbit := Uint_0;
7731 end if;
7733 if Nkind (Decl) = N_Full_Type_Declaration
7734 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7735 then
7736 Check_Component_List
7737 (Component_List (Type_Definition (Decl)),
7738 Sbit,
7739 Discriminant_Specifications (Decl));
7740 end if;
7741 end;
7742 end Record_Hole_Check;
7743 end if;
7745 -- For records that have component clauses for all components, and whose
7746 -- size is less than or equal to 32, we need to know the size in the
7747 -- front end to activate possible packed array processing where the
7748 -- component type is a record.
7750 -- At this stage Hbit + 1 represents the first unused bit from all the
7751 -- component clauses processed, so if the component clauses are
7752 -- complete, then this is the length of the record.
7754 -- For records longer than System.Storage_Unit, and for those where not
7755 -- all components have component clauses, the back end determines the
7756 -- length (it may for example be appropriate to round up the size
7757 -- to some convenient boundary, based on alignment considerations, etc).
7759 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
7761 -- Nothing to do if at least one component has no component clause
7763 Comp := First_Component_Or_Discriminant (Rectype);
7764 while Present (Comp) loop
7765 exit when No (Component_Clause (Comp));
7766 Next_Component_Or_Discriminant (Comp);
7767 end loop;
7769 -- If we fall out of loop, all components have component clauses
7770 -- and so we can set the size to the maximum value.
7772 if No (Comp) then
7773 Set_RM_Size (Rectype, Hbit + 1);
7774 end if;
7775 end if;
7776 end Check_Record_Representation_Clause;
7778 ----------------
7779 -- Check_Size --
7780 ----------------
7782 procedure Check_Size
7783 (N : Node_Id;
7784 T : Entity_Id;
7785 Siz : Uint;
7786 Biased : out Boolean)
7788 UT : constant Entity_Id := Underlying_Type (T);
7789 M : Uint;
7791 begin
7792 Biased := False;
7794 -- Reject patently improper size values.
7796 if Is_Elementary_Type (T)
7797 and then Siz > UI_From_Int (Int'Last)
7798 then
7799 Error_Msg_N ("Size value too large for elementary type", N);
7801 if Nkind (Original_Node (N)) = N_Op_Expon then
7802 Error_Msg_N
7803 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
7804 end if;
7805 end if;
7807 -- Dismiss cases for generic types or types with previous errors
7809 if No (UT)
7810 or else UT = Any_Type
7811 or else Is_Generic_Type (UT)
7812 or else Is_Generic_Type (Root_Type (UT))
7813 then
7814 return;
7816 -- Check case of bit packed array
7818 elsif Is_Array_Type (UT)
7819 and then Known_Static_Component_Size (UT)
7820 and then Is_Bit_Packed_Array (UT)
7821 then
7822 declare
7823 Asiz : Uint;
7824 Indx : Node_Id;
7825 Ityp : Entity_Id;
7827 begin
7828 Asiz := Component_Size (UT);
7829 Indx := First_Index (UT);
7830 loop
7831 Ityp := Etype (Indx);
7833 -- If non-static bound, then we are not in the business of
7834 -- trying to check the length, and indeed an error will be
7835 -- issued elsewhere, since sizes of non-static array types
7836 -- cannot be set implicitly or explicitly.
7838 if not Is_Static_Subtype (Ityp) then
7839 return;
7840 end if;
7842 -- Otherwise accumulate next dimension
7844 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
7845 Expr_Value (Type_Low_Bound (Ityp)) +
7846 Uint_1);
7848 Next_Index (Indx);
7849 exit when No (Indx);
7850 end loop;
7852 if Asiz <= Siz then
7853 return;
7854 else
7855 Error_Msg_Uint_1 := Asiz;
7856 Error_Msg_NE
7857 ("size for& too small, minimum allowed is ^", N, T);
7858 Set_Esize (T, Asiz);
7859 Set_RM_Size (T, Asiz);
7860 end if;
7861 end;
7863 -- All other composite types are ignored
7865 elsif Is_Composite_Type (UT) then
7866 return;
7868 -- For fixed-point types, don't check minimum if type is not frozen,
7869 -- since we don't know all the characteristics of the type that can
7870 -- affect the size (e.g. a specified small) till freeze time.
7872 elsif Is_Fixed_Point_Type (UT)
7873 and then not Is_Frozen (UT)
7874 then
7875 null;
7877 -- Cases for which a minimum check is required
7879 else
7880 -- Ignore if specified size is correct for the type
7882 if Known_Esize (UT) and then Siz = Esize (UT) then
7883 return;
7884 end if;
7886 -- Otherwise get minimum size
7888 M := UI_From_Int (Minimum_Size (UT));
7890 if Siz < M then
7892 -- Size is less than minimum size, but one possibility remains
7893 -- that we can manage with the new size if we bias the type.
7895 M := UI_From_Int (Minimum_Size (UT, Biased => True));
7897 if Siz < M then
7898 Error_Msg_Uint_1 := M;
7899 Error_Msg_NE
7900 ("size for& too small, minimum allowed is ^", N, T);
7901 Set_Esize (T, M);
7902 Set_RM_Size (T, M);
7903 else
7904 Biased := True;
7905 end if;
7906 end if;
7907 end if;
7908 end Check_Size;
7910 -------------------------
7911 -- Get_Alignment_Value --
7912 -------------------------
7914 function Get_Alignment_Value (Expr : Node_Id) return Uint is
7915 Align : constant Uint := Static_Integer (Expr);
7917 begin
7918 if Align = No_Uint then
7919 return No_Uint;
7921 elsif Align <= 0 then
7922 Error_Msg_N ("alignment value must be positive", Expr);
7923 return No_Uint;
7925 else
7926 for J in Int range 0 .. 64 loop
7927 declare
7928 M : constant Uint := Uint_2 ** J;
7930 begin
7931 exit when M = Align;
7933 if M > Align then
7934 Error_Msg_N
7935 ("alignment value must be power of 2", Expr);
7936 return No_Uint;
7937 end if;
7938 end;
7939 end loop;
7941 return Align;
7942 end if;
7943 end Get_Alignment_Value;
7945 -------------------------------------
7946 -- Inherit_Aspects_At_Freeze_Point --
7947 -------------------------------------
7949 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
7950 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
7951 (Rep_Item : Node_Id) return Boolean;
7952 -- This routine checks if Rep_Item is either a pragma or an aspect
7953 -- specification node whose correponding pragma (if any) is present in
7954 -- the Rep Item chain of the entity it has been specified to.
7956 --------------------------------------------------
7957 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
7958 --------------------------------------------------
7960 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
7961 (Rep_Item : Node_Id) return Boolean
7963 begin
7964 return Nkind (Rep_Item) = N_Pragma
7965 or else Present_In_Rep_Item
7966 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
7967 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
7969 begin
7970 -- A representation item is either subtype-specific (Size and Alignment
7971 -- clauses) or type-related (all others). Subtype-specific aspects may
7972 -- differ for different subtypes of the same type.(RM 13.1.8)
7974 -- A derived type inherits each type-related representation aspect of
7975 -- its parent type that was directly specified before the declaration of
7976 -- the derived type. (RM 13.1.15)
7978 -- A derived subtype inherits each subtype-specific representation
7979 -- aspect of its parent subtype that was directly specified before the
7980 -- declaration of the derived type .(RM 13.1.15)
7982 -- The general processing involves inheriting a representation aspect
7983 -- from a parent type whenever the first rep item (aspect specification,
7984 -- attribute definition clause, pragma) corresponding to the given
7985 -- representation aspect in the rep item chain of Typ, if any, isn't
7986 -- directly specified to Typ but to one of its parents.
7988 -- ??? Note that, for now, just a limited number of representation
7989 -- aspects have been inherited here so far. Many of them are still
7990 -- inherited in Sem_Ch3. This will be fixed soon. Here is a
7991 -- non-exhaustive list of aspects that likely also need to be moved to
7992 -- this routine: Alignment, Component_Alignment, Component_Size,
7993 -- Machine_Radix, Object_Size, Pack, Predicates,
7994 -- Preelaborable_Initialization, RM_Size and Small.
7996 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
7997 return;
7998 end if;
8000 -- Ada_05/Ada_2005
8002 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
8003 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
8004 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8005 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
8006 then
8007 Set_Is_Ada_2005_Only (Typ);
8008 end if;
8010 -- Ada_12/Ada_2012
8012 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
8013 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
8014 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8015 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
8016 then
8017 Set_Is_Ada_2012_Only (Typ);
8018 end if;
8020 -- Atomic/Shared
8022 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
8023 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
8024 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8025 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
8026 then
8027 Set_Is_Atomic (Typ);
8028 Set_Treat_As_Volatile (Typ);
8029 Set_Is_Volatile (Typ);
8030 end if;
8032 -- Default_Component_Value.
8034 if Is_Array_Type (Typ)
8035 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
8036 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
8037 then
8038 Set_Default_Aspect_Component_Value (Typ,
8039 Default_Aspect_Component_Value
8040 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
8041 end if;
8043 -- Default_Value.
8045 if Is_Scalar_Type (Typ)
8046 and then Has_Rep_Item (Typ, Name_Default_Value, False)
8047 and then Has_Rep_Item (Typ, Name_Default_Value)
8048 then
8049 Set_Default_Aspect_Value (Typ,
8050 Default_Aspect_Value
8051 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
8052 end if;
8054 -- Discard_Names
8056 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
8057 and then Has_Rep_Item (Typ, Name_Discard_Names)
8058 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8059 (Get_Rep_Item (Typ, Name_Discard_Names))
8060 then
8061 Set_Discard_Names (Typ);
8062 end if;
8064 -- Invariants
8066 if not Has_Rep_Item (Typ, Name_Invariant, False)
8067 and then Has_Rep_Item (Typ, Name_Invariant)
8068 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8069 (Get_Rep_Item (Typ, Name_Invariant))
8070 then
8071 Set_Has_Invariants (Typ);
8073 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
8074 Set_Has_Inheritable_Invariants (Typ);
8075 end if;
8076 end if;
8078 -- Volatile
8080 if not Has_Rep_Item (Typ, Name_Volatile, False)
8081 and then Has_Rep_Item (Typ, Name_Volatile)
8082 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8083 (Get_Rep_Item (Typ, Name_Volatile))
8084 then
8085 Set_Treat_As_Volatile (Typ);
8086 Set_Is_Volatile (Typ);
8087 end if;
8089 -- Inheritance for derived types only
8091 if Is_Derived_Type (Typ) then
8092 declare
8093 Bas_Typ : constant Entity_Id := Base_Type (Typ);
8094 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
8096 begin
8097 -- Atomic_Components
8099 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
8100 and then Has_Rep_Item (Typ, Name_Atomic_Components)
8101 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8102 (Get_Rep_Item (Typ, Name_Atomic_Components))
8103 then
8104 Set_Has_Atomic_Components (Imp_Bas_Typ);
8105 end if;
8107 -- Volatile_Components
8109 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
8110 and then Has_Rep_Item (Typ, Name_Volatile_Components)
8111 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8112 (Get_Rep_Item (Typ, Name_Volatile_Components))
8113 then
8114 Set_Has_Volatile_Components (Imp_Bas_Typ);
8115 end if;
8117 -- Finalize_Storage_Only.
8119 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
8120 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
8121 then
8122 Set_Finalize_Storage_Only (Bas_Typ);
8123 end if;
8125 -- Universal_Aliasing
8127 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
8128 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
8129 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8130 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
8131 then
8132 Set_Universal_Aliasing (Imp_Bas_Typ);
8133 end if;
8135 -- Record type specific aspects
8137 if Is_Record_Type (Typ) then
8138 -- Bit_Order
8140 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
8141 and then Has_Rep_Item (Typ, Name_Bit_Order)
8142 then
8143 Set_Reverse_Bit_Order (Bas_Typ,
8144 Reverse_Bit_Order (Entity (Name
8145 (Get_Rep_Item (Typ, Name_Bit_Order)))));
8146 end if;
8148 -- Scalar_Storage_Order
8150 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
8151 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
8152 then
8153 Set_Reverse_Storage_Order (Bas_Typ,
8154 Reverse_Storage_Order (Entity (Name
8155 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
8156 end if;
8157 end if;
8158 end;
8159 end if;
8160 end Inherit_Aspects_At_Freeze_Point;
8162 ----------------
8163 -- Initialize --
8164 ----------------
8166 procedure Initialize is
8167 begin
8168 Address_Clause_Checks.Init;
8169 Independence_Checks.Init;
8170 Unchecked_Conversions.Init;
8171 end Initialize;
8173 -------------------------
8174 -- Is_Operational_Item --
8175 -------------------------
8177 function Is_Operational_Item (N : Node_Id) return Boolean is
8178 begin
8179 if Nkind (N) /= N_Attribute_Definition_Clause then
8180 return False;
8181 else
8182 declare
8183 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
8184 begin
8185 return Id = Attribute_Input
8186 or else Id = Attribute_Output
8187 or else Id = Attribute_Read
8188 or else Id = Attribute_Write
8189 or else Id = Attribute_External_Tag;
8190 end;
8191 end if;
8192 end Is_Operational_Item;
8194 ------------------
8195 -- Minimum_Size --
8196 ------------------
8198 function Minimum_Size
8199 (T : Entity_Id;
8200 Biased : Boolean := False) return Nat
8202 Lo : Uint := No_Uint;
8203 Hi : Uint := No_Uint;
8204 LoR : Ureal := No_Ureal;
8205 HiR : Ureal := No_Ureal;
8206 LoSet : Boolean := False;
8207 HiSet : Boolean := False;
8208 B : Uint;
8209 S : Nat;
8210 Ancest : Entity_Id;
8211 R_Typ : constant Entity_Id := Root_Type (T);
8213 begin
8214 -- If bad type, return 0
8216 if T = Any_Type then
8217 return 0;
8219 -- For generic types, just return zero. There cannot be any legitimate
8220 -- need to know such a size, but this routine may be called with a
8221 -- generic type as part of normal processing.
8223 elsif Is_Generic_Type (R_Typ)
8224 or else R_Typ = Any_Type
8225 then
8226 return 0;
8228 -- Access types. Normally an access type cannot have a size smaller
8229 -- than the size of System.Address. The exception is on VMS, where
8230 -- we have short and long addresses, and it is possible for an access
8231 -- type to have a short address size (and thus be less than the size
8232 -- of System.Address itself). We simply skip the check for VMS, and
8233 -- leave it to the back end to do the check.
8235 elsif Is_Access_Type (T) then
8236 if OpenVMS_On_Target then
8237 return 0;
8238 else
8239 return System_Address_Size;
8240 end if;
8242 -- Floating-point types
8244 elsif Is_Floating_Point_Type (T) then
8245 return UI_To_Int (Esize (R_Typ));
8247 -- Discrete types
8249 elsif Is_Discrete_Type (T) then
8251 -- The following loop is looking for the nearest compile time known
8252 -- bounds following the ancestor subtype chain. The idea is to find
8253 -- the most restrictive known bounds information.
8255 Ancest := T;
8256 loop
8257 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8258 return 0;
8259 end if;
8261 if not LoSet then
8262 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
8263 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
8264 LoSet := True;
8265 exit when HiSet;
8266 end if;
8267 end if;
8269 if not HiSet then
8270 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
8271 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
8272 HiSet := True;
8273 exit when LoSet;
8274 end if;
8275 end if;
8277 Ancest := Ancestor_Subtype (Ancest);
8279 if No (Ancest) then
8280 Ancest := Base_Type (T);
8282 if Is_Generic_Type (Ancest) then
8283 return 0;
8284 end if;
8285 end if;
8286 end loop;
8288 -- Fixed-point types. We can't simply use Expr_Value to get the
8289 -- Corresponding_Integer_Value values of the bounds, since these do not
8290 -- get set till the type is frozen, and this routine can be called
8291 -- before the type is frozen. Similarly the test for bounds being static
8292 -- needs to include the case where we have unanalyzed real literals for
8293 -- the same reason.
8295 elsif Is_Fixed_Point_Type (T) then
8297 -- The following loop is looking for the nearest compile time known
8298 -- bounds following the ancestor subtype chain. The idea is to find
8299 -- the most restrictive known bounds information.
8301 Ancest := T;
8302 loop
8303 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8304 return 0;
8305 end if;
8307 -- Note: In the following two tests for LoSet and HiSet, it may
8308 -- seem redundant to test for N_Real_Literal here since normally
8309 -- one would assume that the test for the value being known at
8310 -- compile time includes this case. However, there is a glitch.
8311 -- If the real literal comes from folding a non-static expression,
8312 -- then we don't consider any non- static expression to be known
8313 -- at compile time if we are in configurable run time mode (needed
8314 -- in some cases to give a clearer definition of what is and what
8315 -- is not accepted). So the test is indeed needed. Without it, we
8316 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
8318 if not LoSet then
8319 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
8320 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
8321 then
8322 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
8323 LoSet := True;
8324 exit when HiSet;
8325 end if;
8326 end if;
8328 if not HiSet then
8329 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
8330 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
8331 then
8332 HiR := Expr_Value_R (Type_High_Bound (Ancest));
8333 HiSet := True;
8334 exit when LoSet;
8335 end if;
8336 end if;
8338 Ancest := Ancestor_Subtype (Ancest);
8340 if No (Ancest) then
8341 Ancest := Base_Type (T);
8343 if Is_Generic_Type (Ancest) then
8344 return 0;
8345 end if;
8346 end if;
8347 end loop;
8349 Lo := UR_To_Uint (LoR / Small_Value (T));
8350 Hi := UR_To_Uint (HiR / Small_Value (T));
8352 -- No other types allowed
8354 else
8355 raise Program_Error;
8356 end if;
8358 -- Fall through with Hi and Lo set. Deal with biased case
8360 if (Biased
8361 and then not Is_Fixed_Point_Type (T)
8362 and then not (Is_Enumeration_Type (T)
8363 and then Has_Non_Standard_Rep (T)))
8364 or else Has_Biased_Representation (T)
8365 then
8366 Hi := Hi - Lo;
8367 Lo := Uint_0;
8368 end if;
8370 -- Signed case. Note that we consider types like range 1 .. -1 to be
8371 -- signed for the purpose of computing the size, since the bounds have
8372 -- to be accommodated in the base type.
8374 if Lo < 0 or else Hi < 0 then
8375 S := 1;
8376 B := Uint_1;
8378 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
8379 -- Note that we accommodate the case where the bounds cross. This
8380 -- can happen either because of the way the bounds are declared
8381 -- or because of the algorithm in Freeze_Fixed_Point_Type.
8383 while Lo < -B
8384 or else Hi < -B
8385 or else Lo >= B
8386 or else Hi >= B
8387 loop
8388 B := Uint_2 ** S;
8389 S := S + 1;
8390 end loop;
8392 -- Unsigned case
8394 else
8395 -- If both bounds are positive, make sure that both are represen-
8396 -- table in the case where the bounds are crossed. This can happen
8397 -- either because of the way the bounds are declared, or because of
8398 -- the algorithm in Freeze_Fixed_Point_Type.
8400 if Lo > Hi then
8401 Hi := Lo;
8402 end if;
8404 -- S = size, (can accommodate 0 .. (2**size - 1))
8406 S := 0;
8407 while Hi >= Uint_2 ** S loop
8408 S := S + 1;
8409 end loop;
8410 end if;
8412 return S;
8413 end Minimum_Size;
8415 ---------------------------
8416 -- New_Stream_Subprogram --
8417 ---------------------------
8419 procedure New_Stream_Subprogram
8420 (N : Node_Id;
8421 Ent : Entity_Id;
8422 Subp : Entity_Id;
8423 Nam : TSS_Name_Type)
8425 Loc : constant Source_Ptr := Sloc (N);
8426 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
8427 Subp_Id : Entity_Id;
8428 Subp_Decl : Node_Id;
8429 F : Entity_Id;
8430 Etyp : Entity_Id;
8432 Defer_Declaration : constant Boolean :=
8433 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
8434 -- For a tagged type, there is a declaration for each stream attribute
8435 -- at the freeze point, and we must generate only a completion of this
8436 -- declaration. We do the same for private types, because the full view
8437 -- might be tagged. Otherwise we generate a declaration at the point of
8438 -- the attribute definition clause.
8440 function Build_Spec return Node_Id;
8441 -- Used for declaration and renaming declaration, so that this is
8442 -- treated as a renaming_as_body.
8444 ----------------
8445 -- Build_Spec --
8446 ----------------
8448 function Build_Spec return Node_Id is
8449 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
8450 Formals : List_Id;
8451 Spec : Node_Id;
8452 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
8454 begin
8455 Subp_Id := Make_Defining_Identifier (Loc, Sname);
8457 -- S : access Root_Stream_Type'Class
8459 Formals := New_List (
8460 Make_Parameter_Specification (Loc,
8461 Defining_Identifier =>
8462 Make_Defining_Identifier (Loc, Name_S),
8463 Parameter_Type =>
8464 Make_Access_Definition (Loc,
8465 Subtype_Mark =>
8466 New_Reference_To (
8467 Designated_Type (Etype (F)), Loc))));
8469 if Nam = TSS_Stream_Input then
8470 Spec := Make_Function_Specification (Loc,
8471 Defining_Unit_Name => Subp_Id,
8472 Parameter_Specifications => Formals,
8473 Result_Definition => T_Ref);
8474 else
8475 -- V : [out] T
8477 Append_To (Formals,
8478 Make_Parameter_Specification (Loc,
8479 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8480 Out_Present => Out_P,
8481 Parameter_Type => T_Ref));
8483 Spec :=
8484 Make_Procedure_Specification (Loc,
8485 Defining_Unit_Name => Subp_Id,
8486 Parameter_Specifications => Formals);
8487 end if;
8489 return Spec;
8490 end Build_Spec;
8492 -- Start of processing for New_Stream_Subprogram
8494 begin
8495 F := First_Formal (Subp);
8497 if Ekind (Subp) = E_Procedure then
8498 Etyp := Etype (Next_Formal (F));
8499 else
8500 Etyp := Etype (Subp);
8501 end if;
8503 -- Prepare subprogram declaration and insert it as an action on the
8504 -- clause node. The visibility for this entity is used to test for
8505 -- visibility of the attribute definition clause (in the sense of
8506 -- 8.3(23) as amended by AI-195).
8508 if not Defer_Declaration then
8509 Subp_Decl :=
8510 Make_Subprogram_Declaration (Loc,
8511 Specification => Build_Spec);
8513 -- For a tagged type, there is always a visible declaration for each
8514 -- stream TSS (it is a predefined primitive operation), and the
8515 -- completion of this declaration occurs at the freeze point, which is
8516 -- not always visible at places where the attribute definition clause is
8517 -- visible. So, we create a dummy entity here for the purpose of
8518 -- tracking the visibility of the attribute definition clause itself.
8520 else
8521 Subp_Id :=
8522 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
8523 Subp_Decl :=
8524 Make_Object_Declaration (Loc,
8525 Defining_Identifier => Subp_Id,
8526 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
8527 end if;
8529 Insert_Action (N, Subp_Decl);
8530 Set_Entity (N, Subp_Id);
8532 Subp_Decl :=
8533 Make_Subprogram_Renaming_Declaration (Loc,
8534 Specification => Build_Spec,
8535 Name => New_Reference_To (Subp, Loc));
8537 if Defer_Declaration then
8538 Set_TSS (Base_Type (Ent), Subp_Id);
8539 else
8540 Insert_Action (N, Subp_Decl);
8541 Copy_TSS (Subp_Id, Base_Type (Ent));
8542 end if;
8543 end New_Stream_Subprogram;
8545 ------------------------
8546 -- Rep_Item_Too_Early --
8547 ------------------------
8549 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
8550 begin
8551 -- Cannot apply non-operational rep items to generic types
8553 if Is_Operational_Item (N) then
8554 return False;
8556 elsif Is_Type (T)
8557 and then Is_Generic_Type (Root_Type (T))
8558 then
8559 Error_Msg_N ("representation item not allowed for generic type", N);
8560 return True;
8561 end if;
8563 -- Otherwise check for incomplete type
8565 if Is_Incomplete_Or_Private_Type (T)
8566 and then No (Underlying_Type (T))
8567 and then
8568 (Nkind (N) /= N_Pragma
8569 or else Get_Pragma_Id (N) /= Pragma_Import)
8570 then
8571 Error_Msg_N
8572 ("representation item must be after full type declaration", N);
8573 return True;
8575 -- If the type has incomplete components, a representation clause is
8576 -- illegal but stream attributes and Convention pragmas are correct.
8578 elsif Has_Private_Component (T) then
8579 if Nkind (N) = N_Pragma then
8580 return False;
8581 else
8582 Error_Msg_N
8583 ("representation item must appear after type is fully defined",
8585 return True;
8586 end if;
8587 else
8588 return False;
8589 end if;
8590 end Rep_Item_Too_Early;
8592 -----------------------
8593 -- Rep_Item_Too_Late --
8594 -----------------------
8596 function Rep_Item_Too_Late
8597 (T : Entity_Id;
8598 N : Node_Id;
8599 FOnly : Boolean := False) return Boolean
8601 S : Entity_Id;
8602 Parent_Type : Entity_Id;
8604 procedure Too_Late;
8605 -- Output the too late message. Note that this is not considered a
8606 -- serious error, since the effect is simply that we ignore the
8607 -- representation clause in this case.
8609 --------------
8610 -- Too_Late --
8611 --------------
8613 procedure Too_Late is
8614 begin
8615 Error_Msg_N ("|representation item appears too late!", N);
8616 end Too_Late;
8618 -- Start of processing for Rep_Item_Too_Late
8620 begin
8621 -- First make sure entity is not frozen (RM 13.1(9))
8623 if Is_Frozen (T)
8625 -- Exclude imported types, which may be frozen if they appear in a
8626 -- representation clause for a local type.
8628 and then not From_With_Type (T)
8630 -- Exclude generated entitiesa (not coming from source). The common
8631 -- case is when we generate a renaming which prematurely freezes the
8632 -- renamed internal entity, but we still want to be able to set copies
8633 -- of attribute values such as Size/Alignment.
8635 and then Comes_From_Source (T)
8636 then
8637 Too_Late;
8638 S := First_Subtype (T);
8640 if Present (Freeze_Node (S)) then
8641 Error_Msg_NE
8642 ("?no more representation items for }", Freeze_Node (S), S);
8643 end if;
8645 return True;
8647 -- Check for case of non-tagged derived type whose parent either has
8648 -- primitive operations, or is a by reference type (RM 13.1(10)).
8650 elsif Is_Type (T)
8651 and then not FOnly
8652 and then Is_Derived_Type (T)
8653 and then not Is_Tagged_Type (T)
8654 then
8655 Parent_Type := Etype (Base_Type (T));
8657 if Has_Primitive_Operations (Parent_Type) then
8658 Too_Late;
8659 Error_Msg_NE
8660 ("primitive operations already defined for&!", N, Parent_Type);
8661 return True;
8663 elsif Is_By_Reference_Type (Parent_Type) then
8664 Too_Late;
8665 Error_Msg_NE
8666 ("parent type & is a by reference type!", N, Parent_Type);
8667 return True;
8668 end if;
8669 end if;
8671 -- No error, link item into head of chain of rep items for the entity,
8672 -- but avoid chaining if we have an overloadable entity, and the pragma
8673 -- is one that can apply to multiple overloaded entities.
8675 if Is_Overloadable (T)
8676 and then Nkind (N) = N_Pragma
8677 then
8678 declare
8679 Pname : constant Name_Id := Pragma_Name (N);
8680 begin
8681 if Pname = Name_Convention or else
8682 Pname = Name_Import or else
8683 Pname = Name_Export or else
8684 Pname = Name_External or else
8685 Pname = Name_Interface
8686 then
8687 return False;
8688 end if;
8689 end;
8690 end if;
8692 Record_Rep_Item (T, N);
8693 return False;
8694 end Rep_Item_Too_Late;
8696 -------------------------------------
8697 -- Replace_Type_References_Generic --
8698 -------------------------------------
8700 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
8702 function Replace_Node (N : Node_Id) return Traverse_Result;
8703 -- Processes a single node in the traversal procedure below, checking
8704 -- if node N should be replaced, and if so, doing the replacement.
8706 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
8707 -- This instantiation provides the body of Replace_Type_References
8709 ------------------
8710 -- Replace_Node --
8711 ------------------
8713 function Replace_Node (N : Node_Id) return Traverse_Result is
8714 S : Entity_Id;
8715 P : Node_Id;
8717 begin
8718 -- Case of identifier
8720 if Nkind (N) = N_Identifier then
8722 -- If not the type name, all done with this node
8724 if Chars (N) /= TName then
8725 return Skip;
8727 -- Otherwise do the replacement and we are done with this node
8729 else
8730 Replace_Type_Reference (N);
8731 return Skip;
8732 end if;
8734 -- Case of selected component (which is what a qualification
8735 -- looks like in the unanalyzed tree, which is what we have.
8737 elsif Nkind (N) = N_Selected_Component then
8739 -- If selector name is not our type, keeping going (we might
8740 -- still have an occurrence of the type in the prefix).
8742 if Nkind (Selector_Name (N)) /= N_Identifier
8743 or else Chars (Selector_Name (N)) /= TName
8744 then
8745 return OK;
8747 -- Selector name is our type, check qualification
8749 else
8750 -- Loop through scopes and prefixes, doing comparison
8752 S := Current_Scope;
8753 P := Prefix (N);
8754 loop
8755 -- Continue if no more scopes or scope with no name
8757 if No (S) or else Nkind (S) not in N_Has_Chars then
8758 return OK;
8759 end if;
8761 -- Do replace if prefix is an identifier matching the
8762 -- scope that we are currently looking at.
8764 if Nkind (P) = N_Identifier
8765 and then Chars (P) = Chars (S)
8766 then
8767 Replace_Type_Reference (N);
8768 return Skip;
8769 end if;
8771 -- Go check scope above us if prefix is itself of the
8772 -- form of a selected component, whose selector matches
8773 -- the scope we are currently looking at.
8775 if Nkind (P) = N_Selected_Component
8776 and then Nkind (Selector_Name (P)) = N_Identifier
8777 and then Chars (Selector_Name (P)) = Chars (S)
8778 then
8779 S := Scope (S);
8780 P := Prefix (P);
8782 -- For anything else, we don't have a match, so keep on
8783 -- going, there are still some weird cases where we may
8784 -- still have a replacement within the prefix.
8786 else
8787 return OK;
8788 end if;
8789 end loop;
8790 end if;
8792 -- Continue for any other node kind
8794 else
8795 return OK;
8796 end if;
8797 end Replace_Node;
8799 begin
8800 Replace_Type_Refs (N);
8801 end Replace_Type_References_Generic;
8803 -------------------------
8804 -- Same_Representation --
8805 -------------------------
8807 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
8808 T1 : constant Entity_Id := Underlying_Type (Typ1);
8809 T2 : constant Entity_Id := Underlying_Type (Typ2);
8811 begin
8812 -- A quick check, if base types are the same, then we definitely have
8813 -- the same representation, because the subtype specific representation
8814 -- attributes (Size and Alignment) do not affect representation from
8815 -- the point of view of this test.
8817 if Base_Type (T1) = Base_Type (T2) then
8818 return True;
8820 elsif Is_Private_Type (Base_Type (T2))
8821 and then Base_Type (T1) = Full_View (Base_Type (T2))
8822 then
8823 return True;
8824 end if;
8826 -- Tagged types never have differing representations
8828 if Is_Tagged_Type (T1) then
8829 return True;
8830 end if;
8832 -- Representations are definitely different if conventions differ
8834 if Convention (T1) /= Convention (T2) then
8835 return False;
8836 end if;
8838 -- Representations are different if component alignments differ
8840 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
8841 and then
8842 (Is_Record_Type (T2) or else Is_Array_Type (T2))
8843 and then Component_Alignment (T1) /= Component_Alignment (T2)
8844 then
8845 return False;
8846 end if;
8848 -- For arrays, the only real issue is component size. If we know the
8849 -- component size for both arrays, and it is the same, then that's
8850 -- good enough to know we don't have a change of representation.
8852 if Is_Array_Type (T1) then
8853 if Known_Component_Size (T1)
8854 and then Known_Component_Size (T2)
8855 and then Component_Size (T1) = Component_Size (T2)
8856 then
8857 if VM_Target = No_VM then
8858 return True;
8860 -- In VM targets the representation of arrays with aliased
8861 -- components differs from arrays with non-aliased components
8863 else
8864 return Has_Aliased_Components (Base_Type (T1))
8866 Has_Aliased_Components (Base_Type (T2));
8867 end if;
8868 end if;
8869 end if;
8871 -- Types definitely have same representation if neither has non-standard
8872 -- representation since default representations are always consistent.
8873 -- If only one has non-standard representation, and the other does not,
8874 -- then we consider that they do not have the same representation. They
8875 -- might, but there is no way of telling early enough.
8877 if Has_Non_Standard_Rep (T1) then
8878 if not Has_Non_Standard_Rep (T2) then
8879 return False;
8880 end if;
8881 else
8882 return not Has_Non_Standard_Rep (T2);
8883 end if;
8885 -- Here the two types both have non-standard representation, and we need
8886 -- to determine if they have the same non-standard representation.
8888 -- For arrays, we simply need to test if the component sizes are the
8889 -- same. Pragma Pack is reflected in modified component sizes, so this
8890 -- check also deals with pragma Pack.
8892 if Is_Array_Type (T1) then
8893 return Component_Size (T1) = Component_Size (T2);
8895 -- Tagged types always have the same representation, because it is not
8896 -- possible to specify different representations for common fields.
8898 elsif Is_Tagged_Type (T1) then
8899 return True;
8901 -- Case of record types
8903 elsif Is_Record_Type (T1) then
8905 -- Packed status must conform
8907 if Is_Packed (T1) /= Is_Packed (T2) then
8908 return False;
8910 -- Otherwise we must check components. Typ2 maybe a constrained
8911 -- subtype with fewer components, so we compare the components
8912 -- of the base types.
8914 else
8915 Record_Case : declare
8916 CD1, CD2 : Entity_Id;
8918 function Same_Rep return Boolean;
8919 -- CD1 and CD2 are either components or discriminants. This
8920 -- function tests whether the two have the same representation
8922 --------------
8923 -- Same_Rep --
8924 --------------
8926 function Same_Rep return Boolean is
8927 begin
8928 if No (Component_Clause (CD1)) then
8929 return No (Component_Clause (CD2));
8931 else
8932 return
8933 Present (Component_Clause (CD2))
8934 and then
8935 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
8936 and then
8937 Esize (CD1) = Esize (CD2);
8938 end if;
8939 end Same_Rep;
8941 -- Start of processing for Record_Case
8943 begin
8944 if Has_Discriminants (T1) then
8945 CD1 := First_Discriminant (T1);
8946 CD2 := First_Discriminant (T2);
8948 -- The number of discriminants may be different if the
8949 -- derived type has fewer (constrained by values). The
8950 -- invisible discriminants retain the representation of
8951 -- the original, so the discrepancy does not per se
8952 -- indicate a different representation.
8954 while Present (CD1)
8955 and then Present (CD2)
8956 loop
8957 if not Same_Rep then
8958 return False;
8959 else
8960 Next_Discriminant (CD1);
8961 Next_Discriminant (CD2);
8962 end if;
8963 end loop;
8964 end if;
8966 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
8967 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
8969 while Present (CD1) loop
8970 if not Same_Rep then
8971 return False;
8972 else
8973 Next_Component (CD1);
8974 Next_Component (CD2);
8975 end if;
8976 end loop;
8978 return True;
8979 end Record_Case;
8980 end if;
8982 -- For enumeration types, we must check each literal to see if the
8983 -- representation is the same. Note that we do not permit enumeration
8984 -- representation clauses for Character and Wide_Character, so these
8985 -- cases were already dealt with.
8987 elsif Is_Enumeration_Type (T1) then
8988 Enumeration_Case : declare
8989 L1, L2 : Entity_Id;
8991 begin
8992 L1 := First_Literal (T1);
8993 L2 := First_Literal (T2);
8995 while Present (L1) loop
8996 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
8997 return False;
8998 else
8999 Next_Literal (L1);
9000 Next_Literal (L2);
9001 end if;
9002 end loop;
9004 return True;
9006 end Enumeration_Case;
9008 -- Any other types have the same representation for these purposes
9010 else
9011 return True;
9012 end if;
9013 end Same_Representation;
9015 ----------------
9016 -- Set_Biased --
9017 ----------------
9019 procedure Set_Biased
9020 (E : Entity_Id;
9021 N : Node_Id;
9022 Msg : String;
9023 Biased : Boolean := True)
9025 begin
9026 if Biased then
9027 Set_Has_Biased_Representation (E);
9029 if Warn_On_Biased_Representation then
9030 Error_Msg_NE
9031 ("?" & Msg & " forces biased representation for&", N, E);
9032 end if;
9033 end if;
9034 end Set_Biased;
9036 --------------------
9037 -- Set_Enum_Esize --
9038 --------------------
9040 procedure Set_Enum_Esize (T : Entity_Id) is
9041 Lo : Uint;
9042 Hi : Uint;
9043 Sz : Nat;
9045 begin
9046 Init_Alignment (T);
9048 -- Find the minimum standard size (8,16,32,64) that fits
9050 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
9051 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
9053 if Lo < 0 then
9054 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
9055 Sz := Standard_Character_Size; -- May be > 8 on some targets
9057 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
9058 Sz := 16;
9060 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
9061 Sz := 32;
9063 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
9064 Sz := 64;
9065 end if;
9067 else
9068 if Hi < Uint_2**08 then
9069 Sz := Standard_Character_Size; -- May be > 8 on some targets
9071 elsif Hi < Uint_2**16 then
9072 Sz := 16;
9074 elsif Hi < Uint_2**32 then
9075 Sz := 32;
9077 else pragma Assert (Hi < Uint_2**63);
9078 Sz := 64;
9079 end if;
9080 end if;
9082 -- That minimum is the proper size unless we have a foreign convention
9083 -- and the size required is 32 or less, in which case we bump the size
9084 -- up to 32. This is required for C and C++ and seems reasonable for
9085 -- all other foreign conventions.
9087 if Has_Foreign_Convention (T)
9088 and then Esize (T) < Standard_Integer_Size
9089 then
9090 Init_Esize (T, Standard_Integer_Size);
9091 else
9092 Init_Esize (T, Sz);
9093 end if;
9094 end Set_Enum_Esize;
9096 ------------------------------
9097 -- Validate_Address_Clauses --
9098 ------------------------------
9100 procedure Validate_Address_Clauses is
9101 begin
9102 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
9103 declare
9104 ACCR : Address_Clause_Check_Record
9105 renames Address_Clause_Checks.Table (J);
9107 Expr : Node_Id;
9109 X_Alignment : Uint;
9110 Y_Alignment : Uint;
9112 X_Size : Uint;
9113 Y_Size : Uint;
9115 begin
9116 -- Skip processing of this entry if warning already posted
9118 if not Address_Warning_Posted (ACCR.N) then
9120 Expr := Original_Node (Expression (ACCR.N));
9122 -- Get alignments
9124 X_Alignment := Alignment (ACCR.X);
9125 Y_Alignment := Alignment (ACCR.Y);
9127 -- Similarly obtain sizes
9129 X_Size := Esize (ACCR.X);
9130 Y_Size := Esize (ACCR.Y);
9132 -- Check for large object overlaying smaller one
9134 if Y_Size > Uint_0
9135 and then X_Size > Uint_0
9136 and then X_Size > Y_Size
9137 then
9138 Error_Msg_NE
9139 ("?& overlays smaller object", ACCR.N, ACCR.X);
9140 Error_Msg_N
9141 ("\?program execution may be erroneous", ACCR.N);
9142 Error_Msg_Uint_1 := X_Size;
9143 Error_Msg_NE
9144 ("\?size of & is ^", ACCR.N, ACCR.X);
9145 Error_Msg_Uint_1 := Y_Size;
9146 Error_Msg_NE
9147 ("\?size of & is ^", ACCR.N, ACCR.Y);
9149 -- Check for inadequate alignment, both of the base object
9150 -- and of the offset, if any.
9152 -- Note: we do not check the alignment if we gave a size
9153 -- warning, since it would likely be redundant.
9155 elsif Y_Alignment /= Uint_0
9156 and then (Y_Alignment < X_Alignment
9157 or else (ACCR.Off
9158 and then
9159 Nkind (Expr) = N_Attribute_Reference
9160 and then
9161 Attribute_Name (Expr) = Name_Address
9162 and then
9163 Has_Compatible_Alignment
9164 (ACCR.X, Prefix (Expr))
9165 /= Known_Compatible))
9166 then
9167 Error_Msg_NE
9168 ("?specified address for& may be inconsistent "
9169 & "with alignment",
9170 ACCR.N, ACCR.X);
9171 Error_Msg_N
9172 ("\?program execution may be erroneous (RM 13.3(27))",
9173 ACCR.N);
9174 Error_Msg_Uint_1 := X_Alignment;
9175 Error_Msg_NE
9176 ("\?alignment of & is ^",
9177 ACCR.N, ACCR.X);
9178 Error_Msg_Uint_1 := Y_Alignment;
9179 Error_Msg_NE
9180 ("\?alignment of & is ^",
9181 ACCR.N, ACCR.Y);
9182 if Y_Alignment >= X_Alignment then
9183 Error_Msg_N
9184 ("\?but offset is not multiple of alignment",
9185 ACCR.N);
9186 end if;
9187 end if;
9188 end if;
9189 end;
9190 end loop;
9191 end Validate_Address_Clauses;
9193 ---------------------------
9194 -- Validate_Independence --
9195 ---------------------------
9197 procedure Validate_Independence is
9198 SU : constant Uint := UI_From_Int (System_Storage_Unit);
9199 N : Node_Id;
9200 E : Entity_Id;
9201 IC : Boolean;
9202 Comp : Entity_Id;
9203 Addr : Node_Id;
9204 P : Node_Id;
9206 procedure Check_Array_Type (Atyp : Entity_Id);
9207 -- Checks if the array type Atyp has independent components, and
9208 -- if not, outputs an appropriate set of error messages.
9210 procedure No_Independence;
9211 -- Output message that independence cannot be guaranteed
9213 function OK_Component (C : Entity_Id) return Boolean;
9214 -- Checks one component to see if it is independently accessible, and
9215 -- if so yields True, otherwise yields False if independent access
9216 -- cannot be guaranteed. This is a conservative routine, it only
9217 -- returns True if it knows for sure, it returns False if it knows
9218 -- there is a problem, or it cannot be sure there is no problem.
9220 procedure Reason_Bad_Component (C : Entity_Id);
9221 -- Outputs continuation message if a reason can be determined for
9222 -- the component C being bad.
9224 ----------------------
9225 -- Check_Array_Type --
9226 ----------------------
9228 procedure Check_Array_Type (Atyp : Entity_Id) is
9229 Ctyp : constant Entity_Id := Component_Type (Atyp);
9231 begin
9232 -- OK if no alignment clause, no pack, and no component size
9234 if not Has_Component_Size_Clause (Atyp)
9235 and then not Has_Alignment_Clause (Atyp)
9236 and then not Is_Packed (Atyp)
9237 then
9238 return;
9239 end if;
9241 -- Check actual component size
9243 if not Known_Component_Size (Atyp)
9244 or else not (Addressable (Component_Size (Atyp))
9245 and then Component_Size (Atyp) < 64)
9246 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
9247 then
9248 No_Independence;
9250 -- Bad component size, check reason
9252 if Has_Component_Size_Clause (Atyp) then
9253 P :=
9254 Get_Attribute_Definition_Clause
9255 (Atyp, Attribute_Component_Size);
9257 if Present (P) then
9258 Error_Msg_Sloc := Sloc (P);
9259 Error_Msg_N ("\because of Component_Size clause#", N);
9260 return;
9261 end if;
9262 end if;
9264 if Is_Packed (Atyp) then
9265 P := Get_Rep_Pragma (Atyp, Name_Pack);
9267 if Present (P) then
9268 Error_Msg_Sloc := Sloc (P);
9269 Error_Msg_N ("\because of pragma Pack#", N);
9270 return;
9271 end if;
9272 end if;
9274 -- No reason found, just return
9276 return;
9277 end if;
9279 -- Array type is OK independence-wise
9281 return;
9282 end Check_Array_Type;
9284 ---------------------
9285 -- No_Independence --
9286 ---------------------
9288 procedure No_Independence is
9289 begin
9290 if Pragma_Name (N) = Name_Independent then
9291 Error_Msg_NE
9292 ("independence cannot be guaranteed for&", N, E);
9293 else
9294 Error_Msg_NE
9295 ("independent components cannot be guaranteed for&", N, E);
9296 end if;
9297 end No_Independence;
9299 ------------------
9300 -- OK_Component --
9301 ------------------
9303 function OK_Component (C : Entity_Id) return Boolean is
9304 Rec : constant Entity_Id := Scope (C);
9305 Ctyp : constant Entity_Id := Etype (C);
9307 begin
9308 -- OK if no component clause, no Pack, and no alignment clause
9310 if No (Component_Clause (C))
9311 and then not Is_Packed (Rec)
9312 and then not Has_Alignment_Clause (Rec)
9313 then
9314 return True;
9315 end if;
9317 -- Here we look at the actual component layout. A component is
9318 -- addressable if its size is a multiple of the Esize of the
9319 -- component type, and its starting position in the record has
9320 -- appropriate alignment, and the record itself has appropriate
9321 -- alignment to guarantee the component alignment.
9323 -- Make sure sizes are static, always assume the worst for any
9324 -- cases where we cannot check static values.
9326 if not (Known_Static_Esize (C)
9327 and then Known_Static_Esize (Ctyp))
9328 then
9329 return False;
9330 end if;
9332 -- Size of component must be addressable or greater than 64 bits
9333 -- and a multiple of bytes.
9335 if not Addressable (Esize (C))
9336 and then Esize (C) < Uint_64
9337 then
9338 return False;
9339 end if;
9341 -- Check size is proper multiple
9343 if Esize (C) mod Esize (Ctyp) /= 0 then
9344 return False;
9345 end if;
9347 -- Check alignment of component is OK
9349 if not Known_Component_Bit_Offset (C)
9350 or else Component_Bit_Offset (C) < Uint_0
9351 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
9352 then
9353 return False;
9354 end if;
9356 -- Check alignment of record type is OK
9358 if not Known_Alignment (Rec)
9359 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
9360 then
9361 return False;
9362 end if;
9364 -- All tests passed, component is addressable
9366 return True;
9367 end OK_Component;
9369 --------------------------
9370 -- Reason_Bad_Component --
9371 --------------------------
9373 procedure Reason_Bad_Component (C : Entity_Id) is
9374 Rec : constant Entity_Id := Scope (C);
9375 Ctyp : constant Entity_Id := Etype (C);
9377 begin
9378 -- If component clause present assume that's the problem
9380 if Present (Component_Clause (C)) then
9381 Error_Msg_Sloc := Sloc (Component_Clause (C));
9382 Error_Msg_N ("\because of Component_Clause#", N);
9383 return;
9384 end if;
9386 -- If pragma Pack clause present, assume that's the problem
9388 if Is_Packed (Rec) then
9389 P := Get_Rep_Pragma (Rec, Name_Pack);
9391 if Present (P) then
9392 Error_Msg_Sloc := Sloc (P);
9393 Error_Msg_N ("\because of pragma Pack#", N);
9394 return;
9395 end if;
9396 end if;
9398 -- See if record has bad alignment clause
9400 if Has_Alignment_Clause (Rec)
9401 and then Known_Alignment (Rec)
9402 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
9403 then
9404 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
9406 if Present (P) then
9407 Error_Msg_Sloc := Sloc (P);
9408 Error_Msg_N ("\because of Alignment clause#", N);
9409 end if;
9410 end if;
9412 -- Couldn't find a reason, so return without a message
9414 return;
9415 end Reason_Bad_Component;
9417 -- Start of processing for Validate_Independence
9419 begin
9420 for J in Independence_Checks.First .. Independence_Checks.Last loop
9421 N := Independence_Checks.Table (J).N;
9422 E := Independence_Checks.Table (J).E;
9423 IC := Pragma_Name (N) = Name_Independent_Components;
9425 -- Deal with component case
9427 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
9428 if not OK_Component (E) then
9429 No_Independence;
9430 Reason_Bad_Component (E);
9431 goto Continue;
9432 end if;
9433 end if;
9435 -- Deal with record with Independent_Components
9437 if IC and then Is_Record_Type (E) then
9438 Comp := First_Component_Or_Discriminant (E);
9439 while Present (Comp) loop
9440 if not OK_Component (Comp) then
9441 No_Independence;
9442 Reason_Bad_Component (Comp);
9443 goto Continue;
9444 end if;
9446 Next_Component_Or_Discriminant (Comp);
9447 end loop;
9448 end if;
9450 -- Deal with address clause case
9452 if Is_Object (E) then
9453 Addr := Address_Clause (E);
9455 if Present (Addr) then
9456 No_Independence;
9457 Error_Msg_Sloc := Sloc (Addr);
9458 Error_Msg_N ("\because of Address clause#", N);
9459 goto Continue;
9460 end if;
9461 end if;
9463 -- Deal with independent components for array type
9465 if IC and then Is_Array_Type (E) then
9466 Check_Array_Type (E);
9467 end if;
9469 -- Deal with independent components for array object
9471 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
9472 Check_Array_Type (Etype (E));
9473 end if;
9475 <<Continue>> null;
9476 end loop;
9477 end Validate_Independence;
9479 -----------------------------------
9480 -- Validate_Unchecked_Conversion --
9481 -----------------------------------
9483 procedure Validate_Unchecked_Conversion
9484 (N : Node_Id;
9485 Act_Unit : Entity_Id)
9487 Source : Entity_Id;
9488 Target : Entity_Id;
9489 Vnode : Node_Id;
9491 begin
9492 -- Obtain source and target types. Note that we call Ancestor_Subtype
9493 -- here because the processing for generic instantiation always makes
9494 -- subtypes, and we want the original frozen actual types.
9496 -- If we are dealing with private types, then do the check on their
9497 -- fully declared counterparts if the full declarations have been
9498 -- encountered (they don't have to be visible, but they must exist!)
9500 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
9502 if Is_Private_Type (Source)
9503 and then Present (Underlying_Type (Source))
9504 then
9505 Source := Underlying_Type (Source);
9506 end if;
9508 Target := Ancestor_Subtype (Etype (Act_Unit));
9510 -- If either type is generic, the instantiation happens within a generic
9511 -- unit, and there is nothing to check. The proper check will happen
9512 -- when the enclosing generic is instantiated.
9514 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
9515 return;
9516 end if;
9518 if Is_Private_Type (Target)
9519 and then Present (Underlying_Type (Target))
9520 then
9521 Target := Underlying_Type (Target);
9522 end if;
9524 -- Source may be unconstrained array, but not target
9526 if Is_Array_Type (Target)
9527 and then not Is_Constrained (Target)
9528 then
9529 Error_Msg_N
9530 ("unchecked conversion to unconstrained array not allowed", N);
9531 return;
9532 end if;
9534 -- Warn if conversion between two different convention pointers
9536 if Is_Access_Type (Target)
9537 and then Is_Access_Type (Source)
9538 and then Convention (Target) /= Convention (Source)
9539 and then Warn_On_Unchecked_Conversion
9540 then
9541 -- Give warnings for subprogram pointers only on most targets. The
9542 -- exception is VMS, where data pointers can have different lengths
9543 -- depending on the pointer convention.
9545 if Is_Access_Subprogram_Type (Target)
9546 or else Is_Access_Subprogram_Type (Source)
9547 or else OpenVMS_On_Target
9548 then
9549 Error_Msg_N
9550 ("?conversion between pointers with different conventions!", N);
9551 end if;
9552 end if;
9554 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
9555 -- warning when compiling GNAT-related sources.
9557 if Warn_On_Unchecked_Conversion
9558 and then not In_Predefined_Unit (N)
9559 and then RTU_Loaded (Ada_Calendar)
9560 and then
9561 (Chars (Source) = Name_Time
9562 or else
9563 Chars (Target) = Name_Time)
9564 then
9565 -- If Ada.Calendar is loaded and the name of one of the operands is
9566 -- Time, there is a good chance that this is Ada.Calendar.Time.
9568 declare
9569 Calendar_Time : constant Entity_Id :=
9570 Full_View (RTE (RO_CA_Time));
9571 begin
9572 pragma Assert (Present (Calendar_Time));
9574 if Source = Calendar_Time
9575 or else Target = Calendar_Time
9576 then
9577 Error_Msg_N
9578 ("?representation of 'Time values may change between " &
9579 "'G'N'A'T versions", N);
9580 end if;
9581 end;
9582 end if;
9584 -- Make entry in unchecked conversion table for later processing by
9585 -- Validate_Unchecked_Conversions, which will check sizes and alignments
9586 -- (using values set by the back-end where possible). This is only done
9587 -- if the appropriate warning is active.
9589 if Warn_On_Unchecked_Conversion then
9590 Unchecked_Conversions.Append
9591 (New_Val => UC_Entry'
9592 (Eloc => Sloc (N),
9593 Source => Source,
9594 Target => Target));
9596 -- If both sizes are known statically now, then back end annotation
9597 -- is not required to do a proper check but if either size is not
9598 -- known statically, then we need the annotation.
9600 if Known_Static_RM_Size (Source)
9601 and then Known_Static_RM_Size (Target)
9602 then
9603 null;
9604 else
9605 Back_Annotate_Rep_Info := True;
9606 end if;
9607 end if;
9609 -- If unchecked conversion to access type, and access type is declared
9610 -- in the same unit as the unchecked conversion, then set the flag
9611 -- No_Strict_Aliasing (no strict aliasing is implicit here)
9613 if Is_Access_Type (Target) and then
9614 In_Same_Source_Unit (Target, N)
9615 then
9616 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
9617 end if;
9619 -- Generate N_Validate_Unchecked_Conversion node for back end in case
9620 -- the back end needs to perform special validation checks.
9622 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
9623 -- have full expansion and the back end is called ???
9625 Vnode :=
9626 Make_Validate_Unchecked_Conversion (Sloc (N));
9627 Set_Source_Type (Vnode, Source);
9628 Set_Target_Type (Vnode, Target);
9630 -- If the unchecked conversion node is in a list, just insert before it.
9631 -- If not we have some strange case, not worth bothering about.
9633 if Is_List_Member (N) then
9634 Insert_After (N, Vnode);
9635 end if;
9636 end Validate_Unchecked_Conversion;
9638 ------------------------------------
9639 -- Validate_Unchecked_Conversions --
9640 ------------------------------------
9642 procedure Validate_Unchecked_Conversions is
9643 begin
9644 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
9645 declare
9646 T : UC_Entry renames Unchecked_Conversions.Table (N);
9648 Eloc : constant Source_Ptr := T.Eloc;
9649 Source : constant Entity_Id := T.Source;
9650 Target : constant Entity_Id := T.Target;
9652 Source_Siz : Uint;
9653 Target_Siz : Uint;
9655 begin
9656 -- This validation check, which warns if we have unequal sizes for
9657 -- unchecked conversion, and thus potentially implementation
9658 -- dependent semantics, is one of the few occasions on which we
9659 -- use the official RM size instead of Esize. See description in
9660 -- Einfo "Handling of Type'Size Values" for details.
9662 if Serious_Errors_Detected = 0
9663 and then Known_Static_RM_Size (Source)
9664 and then Known_Static_RM_Size (Target)
9666 -- Don't do the check if warnings off for either type, note the
9667 -- deliberate use of OR here instead of OR ELSE to get the flag
9668 -- Warnings_Off_Used set for both types if appropriate.
9670 and then not (Has_Warnings_Off (Source)
9672 Has_Warnings_Off (Target))
9673 then
9674 Source_Siz := RM_Size (Source);
9675 Target_Siz := RM_Size (Target);
9677 if Source_Siz /= Target_Siz then
9678 Error_Msg
9679 ("?types for unchecked conversion have different sizes!",
9680 Eloc);
9682 if All_Errors_Mode then
9683 Error_Msg_Name_1 := Chars (Source);
9684 Error_Msg_Uint_1 := Source_Siz;
9685 Error_Msg_Name_2 := Chars (Target);
9686 Error_Msg_Uint_2 := Target_Siz;
9687 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
9689 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
9691 if Is_Discrete_Type (Source)
9692 and then Is_Discrete_Type (Target)
9693 then
9694 if Source_Siz > Target_Siz then
9695 Error_Msg
9696 ("\?^ high order bits of source will be ignored!",
9697 Eloc);
9699 elsif Is_Unsigned_Type (Source) then
9700 Error_Msg
9701 ("\?source will be extended with ^ high order " &
9702 "zero bits?!", Eloc);
9704 else
9705 Error_Msg
9706 ("\?source will be extended with ^ high order " &
9707 "sign bits!",
9708 Eloc);
9709 end if;
9711 elsif Source_Siz < Target_Siz then
9712 if Is_Discrete_Type (Target) then
9713 if Bytes_Big_Endian then
9714 Error_Msg
9715 ("\?target value will include ^ undefined " &
9716 "low order bits!",
9717 Eloc);
9718 else
9719 Error_Msg
9720 ("\?target value will include ^ undefined " &
9721 "high order bits!",
9722 Eloc);
9723 end if;
9725 else
9726 Error_Msg
9727 ("\?^ trailing bits of target value will be " &
9728 "undefined!", Eloc);
9729 end if;
9731 else pragma Assert (Source_Siz > Target_Siz);
9732 Error_Msg
9733 ("\?^ trailing bits of source will be ignored!",
9734 Eloc);
9735 end if;
9736 end if;
9737 end if;
9738 end if;
9740 -- If both types are access types, we need to check the alignment.
9741 -- If the alignment of both is specified, we can do it here.
9743 if Serious_Errors_Detected = 0
9744 and then Ekind (Source) in Access_Kind
9745 and then Ekind (Target) in Access_Kind
9746 and then Target_Strict_Alignment
9747 and then Present (Designated_Type (Source))
9748 and then Present (Designated_Type (Target))
9749 then
9750 declare
9751 D_Source : constant Entity_Id := Designated_Type (Source);
9752 D_Target : constant Entity_Id := Designated_Type (Target);
9754 begin
9755 if Known_Alignment (D_Source)
9756 and then Known_Alignment (D_Target)
9757 then
9758 declare
9759 Source_Align : constant Uint := Alignment (D_Source);
9760 Target_Align : constant Uint := Alignment (D_Target);
9762 begin
9763 if Source_Align < Target_Align
9764 and then not Is_Tagged_Type (D_Source)
9766 -- Suppress warning if warnings suppressed on either
9767 -- type or either designated type. Note the use of
9768 -- OR here instead of OR ELSE. That is intentional,
9769 -- we would like to set flag Warnings_Off_Used in
9770 -- all types for which warnings are suppressed.
9772 and then not (Has_Warnings_Off (D_Source)
9774 Has_Warnings_Off (D_Target)
9776 Has_Warnings_Off (Source)
9778 Has_Warnings_Off (Target))
9779 then
9780 Error_Msg_Uint_1 := Target_Align;
9781 Error_Msg_Uint_2 := Source_Align;
9782 Error_Msg_Node_1 := D_Target;
9783 Error_Msg_Node_2 := D_Source;
9784 Error_Msg
9785 ("?alignment of & (^) is stricter than " &
9786 "alignment of & (^)!", Eloc);
9787 Error_Msg
9788 ("\?resulting access value may have invalid " &
9789 "alignment!", Eloc);
9790 end if;
9791 end;
9792 end if;
9793 end;
9794 end if;
9795 end;
9796 end loop;
9797 end Validate_Unchecked_Conversions;
9799 end Sem_Ch13;