Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / sem_ch13.adb
blob37fd72253d6501f03e5d28e14d153ba2eedaeab9
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-2013, 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 Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Lib; use Lib;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch6; use Sem_Ch6;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Ch9; use Sem_Ch9;
51 with Sem_Dim; use Sem_Dim;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Res; use Sem_Res;
56 with Sem_Type; use Sem_Type;
57 with Sem_Util; use Sem_Util;
58 with Sem_Warn; use Sem_Warn;
59 with Sinput; use Sinput;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Stringt; use Stringt;
64 with Targparm; use Targparm;
65 with Ttypes; use Ttypes;
66 with Tbuild; use Tbuild;
67 with Urealp; use Urealp;
68 with Warnsw; use Warnsw;
70 with GNAT.Heap_Sort_G;
72 package body Sem_Ch13 is
74 SSU : constant Pos := System_Storage_Unit;
75 -- Convenient short hand for commonly used constant
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
82 -- This routine is called after setting one of the sizes of type entity
83 -- Typ to Size. The purpose is to deal with the situation of a derived
84 -- type whose inherited alignment is no longer appropriate for the new
85 -- size value. In this case, we reset the Alignment to unknown.
87 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
88 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
89 -- then either there are pragma Predicate entries on the rep chain for the
90 -- type (note that Predicate aspects are converted to pragma Predicate), or
91 -- there are inherited aspects from a parent type, or ancestor subtypes.
92 -- This procedure builds the spec and body for the Predicate function that
93 -- tests these predicates. N is the freeze node for the type. The spec of
94 -- the function is inserted before the freeze node, and the body of the
95 -- function is inserted after the freeze node. If the predicate expression
96 -- has at least one Raise_Expression, then this procedure also builds the
97 -- M version of the predicate function for use in membership tests.
99 procedure Build_Static_Predicate
100 (Typ : Entity_Id;
101 Expr : Node_Id;
102 Nam : Name_Id);
103 -- Given a predicated type Typ, where Typ is a discrete static subtype,
104 -- whose predicate expression is Expr, tests if Expr is a static predicate,
105 -- and if so, builds the predicate range list. Nam is the name of the one
106 -- argument to the predicate function. Occurrences of the type name in the
107 -- predicate expression have been replaced by identifier references to this
108 -- name, which is unique, so any identifier with Chars matching Nam must be
109 -- a reference to the type. If the predicate is non-static, this procedure
110 -- returns doing nothing. If the predicate is static, then the predicate
111 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
112 -- a canonicalized membership operation.
114 function Get_Alignment_Value (Expr : Node_Id) return Uint;
115 -- Given the expression for an alignment value, returns the corresponding
116 -- Uint value. If the value is inappropriate, then error messages are
117 -- posted as required, and a value of No_Uint is returned.
119 function Is_Operational_Item (N : Node_Id) return Boolean;
120 -- A specification for a stream attribute is allowed before the full type
121 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
122 -- that do not specify a representation characteristic are operational
123 -- attributes.
125 procedure New_Stream_Subprogram
126 (N : Node_Id;
127 Ent : Entity_Id;
128 Subp : Entity_Id;
129 Nam : TSS_Name_Type);
130 -- Create a subprogram renaming of a given stream attribute to the
131 -- designated subprogram and then in the tagged case, provide this as a
132 -- primitive operation, or in the non-tagged case make an appropriate TSS
133 -- entry. This is more properly an expansion activity than just semantics,
134 -- but the presence of user-defined stream functions for limited types is a
135 -- legality check, which is why this takes place here rather than in
136 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
137 -- function to be generated.
139 -- To avoid elaboration anomalies with freeze nodes, for untagged types
140 -- we generate both a subprogram declaration and a subprogram renaming
141 -- declaration, so that the attribute specification is handled as a
142 -- renaming_as_body. For tagged types, the specification is one of the
143 -- primitive specs.
145 generic
146 with procedure Replace_Type_Reference (N : Node_Id);
147 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
148 -- This is used to scan an expression for a predicate or invariant aspect
149 -- replacing occurrences of the name TName (the name of the subtype to
150 -- which the aspect applies) with appropriate references to the parameter
151 -- of the predicate function or invariant procedure. The procedure passed
152 -- as a generic parameter does the actual replacement of node N, which is
153 -- either a simple direct reference to TName, or a selected component that
154 -- represents an appropriately qualified occurrence of TName.
156 procedure Set_Biased
157 (E : Entity_Id;
158 N : Node_Id;
159 Msg : String;
160 Biased : Boolean := True);
161 -- If Biased is True, sets Has_Biased_Representation flag for E, and
162 -- outputs a warning message at node N if Warn_On_Biased_Representation is
163 -- is True. This warning inserts the string Msg to describe the construct
164 -- causing biasing.
166 ----------------------------------------------
167 -- Table for Validate_Unchecked_Conversions --
168 ----------------------------------------------
170 -- The following table collects unchecked conversions for validation.
171 -- Entries are made by Validate_Unchecked_Conversion and then the call
172 -- to Validate_Unchecked_Conversions does the actual error checking and
173 -- posting of warnings. The reason for this delayed processing is to take
174 -- advantage of back-annotations of size and alignment values performed by
175 -- the back end.
177 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
178 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
179 -- already have modified all Sloc values if the -gnatD option is set.
181 type UC_Entry is record
182 Eloc : Source_Ptr; -- node used for posting warnings
183 Source : Entity_Id; -- source type for unchecked conversion
184 Target : Entity_Id; -- target type for unchecked conversion
185 end record;
187 package Unchecked_Conversions is new Table.Table (
188 Table_Component_Type => UC_Entry,
189 Table_Index_Type => Int,
190 Table_Low_Bound => 1,
191 Table_Initial => 50,
192 Table_Increment => 200,
193 Table_Name => "Unchecked_Conversions");
195 ----------------------------------------
196 -- Table for Validate_Address_Clauses --
197 ----------------------------------------
199 -- If an address clause has the form
201 -- for X'Address use Expr
203 -- where Expr is of the form Y'Address or recursively is a reference to a
204 -- constant of either of these forms, and X and Y are entities of objects,
205 -- then if Y has a smaller alignment than X, that merits a warning about
206 -- possible bad alignment. The following table collects address clauses of
207 -- this kind. We put these in a table so that they can be checked after the
208 -- back end has completed annotation of the alignments of objects, since we
209 -- can catch more cases that way.
211 type Address_Clause_Check_Record is record
212 N : Node_Id;
213 -- The address clause
215 X : Entity_Id;
216 -- The entity of the object overlaying Y
218 Y : Entity_Id;
219 -- The entity of the object being overlaid
221 Off : Boolean;
222 -- Whether the address is offset within Y
223 end record;
225 package Address_Clause_Checks is new Table.Table (
226 Table_Component_Type => Address_Clause_Check_Record,
227 Table_Index_Type => Int,
228 Table_Low_Bound => 1,
229 Table_Initial => 20,
230 Table_Increment => 200,
231 Table_Name => "Address_Clause_Checks");
233 -----------------------------------------
234 -- Adjust_Record_For_Reverse_Bit_Order --
235 -----------------------------------------
237 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
238 Comp : Node_Id;
239 CC : Node_Id;
241 begin
242 -- Processing depends on version of Ada
244 -- For Ada 95, we just renumber bits within a storage unit. We do the
245 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
246 -- Ada 83, and are free to add this extension.
248 if Ada_Version < Ada_2005 then
249 Comp := First_Component_Or_Discriminant (R);
250 while Present (Comp) loop
251 CC := Component_Clause (Comp);
253 -- If component clause is present, then deal with the non-default
254 -- bit order case for Ada 95 mode.
256 -- We only do this processing for the base type, and in fact that
257 -- is important, since otherwise if there are record subtypes, we
258 -- could reverse the bits once for each subtype, which is wrong.
260 if Present (CC) and then Ekind (R) = E_Record_Type then
261 declare
262 CFB : constant Uint := Component_Bit_Offset (Comp);
263 CSZ : constant Uint := Esize (Comp);
264 CLC : constant Node_Id := Component_Clause (Comp);
265 Pos : constant Node_Id := Position (CLC);
266 FB : constant Node_Id := First_Bit (CLC);
268 Storage_Unit_Offset : constant Uint :=
269 CFB / System_Storage_Unit;
271 Start_Bit : constant Uint :=
272 CFB mod System_Storage_Unit;
274 begin
275 -- Cases where field goes over storage unit boundary
277 if Start_Bit + CSZ > System_Storage_Unit then
279 -- Allow multi-byte field but generate warning
281 if Start_Bit mod System_Storage_Unit = 0
282 and then CSZ mod System_Storage_Unit = 0
283 then
284 Error_Msg_N
285 ("multi-byte field specified with non-standard"
286 & " Bit_Order??", CLC);
288 if Bytes_Big_Endian then
289 Error_Msg_N
290 ("bytes are not reversed "
291 & "(component is big-endian)??", CLC);
292 else
293 Error_Msg_N
294 ("bytes are not reversed "
295 & "(component is little-endian)??", CLC);
296 end if;
298 -- Do not allow non-contiguous field
300 else
301 Error_Msg_N
302 ("attempt to specify non-contiguous field "
303 & "not permitted", CLC);
304 Error_Msg_N
305 ("\caused by non-standard Bit_Order "
306 & "specified", CLC);
307 Error_Msg_N
308 ("\consider possibility of using "
309 & "Ada 2005 mode here", CLC);
310 end if;
312 -- Case where field fits in one storage unit
314 else
315 -- Give warning if suspicious component clause
317 if Intval (FB) >= System_Storage_Unit
318 and then Warn_On_Reverse_Bit_Order
319 then
320 Error_Msg_N
321 ("Bit_Order clause does not affect " &
322 "byte ordering?V?", Pos);
323 Error_Msg_Uint_1 :=
324 Intval (Pos) + Intval (FB) /
325 System_Storage_Unit;
326 Error_Msg_N
327 ("position normalized to ^ before bit " &
328 "order interpreted?V?", Pos);
329 end if;
331 -- Here is where we fix up the Component_Bit_Offset value
332 -- to account for the reverse bit order. Some examples of
333 -- what needs to be done are:
335 -- First_Bit .. Last_Bit Component_Bit_Offset
336 -- old new old new
338 -- 0 .. 0 7 .. 7 0 7
339 -- 0 .. 1 6 .. 7 0 6
340 -- 0 .. 2 5 .. 7 0 5
341 -- 0 .. 7 0 .. 7 0 4
343 -- 1 .. 1 6 .. 6 1 6
344 -- 1 .. 4 3 .. 6 1 3
345 -- 4 .. 7 0 .. 3 4 0
347 -- The rule is that the first bit is is obtained by
348 -- subtracting the old ending bit from storage_unit - 1.
350 Set_Component_Bit_Offset
351 (Comp,
352 (Storage_Unit_Offset * System_Storage_Unit) +
353 (System_Storage_Unit - 1) -
354 (Start_Bit + CSZ - 1));
356 Set_Normalized_First_Bit
357 (Comp,
358 Component_Bit_Offset (Comp) mod
359 System_Storage_Unit);
360 end if;
361 end;
362 end if;
364 Next_Component_Or_Discriminant (Comp);
365 end loop;
367 -- For Ada 2005, we do machine scalar processing, as fully described In
368 -- AI-133. This involves gathering all components which start at the
369 -- same byte offset and processing them together. Same approach is still
370 -- valid in later versions including Ada 2012.
372 else
373 declare
374 Max_Machine_Scalar_Size : constant Uint :=
375 UI_From_Int
376 (Standard_Long_Long_Integer_Size);
377 -- We use this as the maximum machine scalar size
379 Num_CC : Natural;
380 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
382 begin
383 -- This first loop through components does two things. First it
384 -- deals with the case of components with component clauses whose
385 -- length is greater than the maximum machine scalar size (either
386 -- accepting them or rejecting as needed). Second, it counts the
387 -- number of components with component clauses whose length does
388 -- not exceed this maximum for later processing.
390 Num_CC := 0;
391 Comp := First_Component_Or_Discriminant (R);
392 while Present (Comp) loop
393 CC := Component_Clause (Comp);
395 if Present (CC) then
396 declare
397 Fbit : constant Uint := Static_Integer (First_Bit (CC));
398 Lbit : constant Uint := 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?V?", CC);
417 if Bytes_Big_Endian then
418 Error_Msg_N
419 ("\bytes are not reversed "
420 & "(component is big-endian)?V?", CC);
421 else
422 Error_Msg_N
423 ("\bytes are not reversed "
424 & "(component is little-endian)?V?", 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 := Component_Clause (Comp);
628 LB : constant Uint := Static_Integer (Last_Bit (CC));
629 NFB : constant Uint := MSS - Uint_1 - LB;
630 NLB : constant Uint := NFB + Esize (Comp) - 1;
631 Pos : constant Uint := Static_Integer (Position (CC));
633 begin
634 if Warn_On_Reverse_Bit_Order then
635 Error_Msg_Uint_1 := MSS;
636 Error_Msg_N
637 ("info: reverse bit order in machine " &
638 "scalar of length^?V?", First_Bit (CC));
639 Error_Msg_Uint_1 := NFB;
640 Error_Msg_Uint_2 := NLB;
642 if Bytes_Big_Endian then
643 Error_Msg_NE
644 ("\info: big-endian range for "
645 & "component & is ^ .. ^?V?",
646 First_Bit (CC), Comp);
647 else
648 Error_Msg_NE
649 ("\info: little-endian range "
650 & "for component & is ^ .. ^?V?",
651 First_Bit (CC), Comp);
652 end if;
653 end if;
655 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
656 Set_Normalized_First_Bit (Comp, NFB mod SSU);
657 end;
658 end loop;
659 end loop;
660 end Sort_CC;
661 end;
662 end if;
663 end Adjust_Record_For_Reverse_Bit_Order;
665 -------------------------------------
666 -- Alignment_Check_For_Size_Change --
667 -------------------------------------
669 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
670 begin
671 -- If the alignment is known, and not set by a rep clause, and is
672 -- inconsistent with the size being set, then reset it to unknown,
673 -- we assume in this case that the size overrides the inherited
674 -- alignment, and that the alignment must be recomputed.
676 if Known_Alignment (Typ)
677 and then not Has_Alignment_Clause (Typ)
678 and then Size mod (Alignment (Typ) * SSU) /= 0
679 then
680 Init_Alignment (Typ);
681 end if;
682 end Alignment_Check_For_Size_Change;
684 -------------------------------------
685 -- Analyze_Aspects_At_Freeze_Point --
686 -------------------------------------
688 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
689 ASN : Node_Id;
690 A_Id : Aspect_Id;
691 Ritem : Node_Id;
693 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
694 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
695 -- the aspect specification node ASN.
697 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
698 -- Given an aspect specification node ASN whose expression is an
699 -- optional Boolean, this routines creates the corresponding pragma
700 -- at the freezing point.
702 ----------------------------------
703 -- Analyze_Aspect_Default_Value --
704 ----------------------------------
706 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
707 Ent : constant Entity_Id := Entity (ASN);
708 Expr : constant Node_Id := Expression (ASN);
709 Id : constant Node_Id := Identifier (ASN);
711 begin
712 Error_Msg_Name_1 := Chars (Id);
714 if not Is_Type (Ent) then
715 Error_Msg_N ("aspect% can only apply to a type", Id);
716 return;
718 elsif not Is_First_Subtype (Ent) then
719 Error_Msg_N ("aspect% cannot apply to subtype", Id);
720 return;
722 elsif A_Id = Aspect_Default_Value
723 and then not Is_Scalar_Type (Ent)
724 then
725 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
726 return;
728 elsif A_Id = Aspect_Default_Component_Value then
729 if not Is_Array_Type (Ent) then
730 Error_Msg_N ("aspect% can only be applied to array type", Id);
731 return;
733 elsif not Is_Scalar_Type (Component_Type (Ent)) then
734 Error_Msg_N ("aspect% requires scalar components", Id);
735 return;
736 end if;
737 end if;
739 Set_Has_Default_Aspect (Base_Type (Ent));
741 if Is_Scalar_Type (Ent) then
742 Set_Default_Aspect_Value (Ent, Expr);
744 -- Place default value of base type as well, because that is
745 -- the semantics of the aspect. It is convenient to link the
746 -- aspect to both the (possibly anonymous) base type and to
747 -- the given first subtype.
749 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
751 else
752 Set_Default_Aspect_Component_Value (Ent, Expr);
753 end if;
754 end Analyze_Aspect_Default_Value;
756 -------------------------------------
757 -- Make_Pragma_From_Boolean_Aspect --
758 -------------------------------------
760 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
761 Ident : constant Node_Id := Identifier (ASN);
762 A_Name : constant Name_Id := Chars (Ident);
763 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
764 Ent : constant Entity_Id := Entity (ASN);
765 Expr : constant Node_Id := Expression (ASN);
766 Loc : constant Source_Ptr := Sloc (ASN);
768 Prag : Node_Id;
770 procedure Check_False_Aspect_For_Derived_Type;
771 -- This procedure checks for the case of a false aspect for a derived
772 -- type, which improperly tries to cancel an aspect inherited from
773 -- the parent.
775 -----------------------------------------
776 -- Check_False_Aspect_For_Derived_Type --
777 -----------------------------------------
779 procedure Check_False_Aspect_For_Derived_Type is
780 Par : Node_Id;
782 begin
783 -- We are only checking derived types
785 if not Is_Derived_Type (E) then
786 return;
787 end if;
789 Par := Nearest_Ancestor (E);
791 case A_Id is
792 when Aspect_Atomic | Aspect_Shared =>
793 if not Is_Atomic (Par) then
794 return;
795 end if;
797 when Aspect_Atomic_Components =>
798 if not Has_Atomic_Components (Par) then
799 return;
800 end if;
802 when Aspect_Discard_Names =>
803 if not Discard_Names (Par) then
804 return;
805 end if;
807 when Aspect_Pack =>
808 if not Is_Packed (Par) then
809 return;
810 end if;
812 when Aspect_Unchecked_Union =>
813 if not Is_Unchecked_Union (Par) then
814 return;
815 end if;
817 when Aspect_Volatile =>
818 if not Is_Volatile (Par) then
819 return;
820 end if;
822 when Aspect_Volatile_Components =>
823 if not Has_Volatile_Components (Par) then
824 return;
825 end if;
827 when others =>
828 return;
829 end case;
831 -- Fall through means we are canceling an inherited aspect
833 Error_Msg_Name_1 := A_Name;
834 Error_Msg_NE ("derived type& inherits aspect%, cannot cancel",
835 Expr,
838 end Check_False_Aspect_For_Derived_Type;
840 -- Start of processing for Make_Pragma_From_Boolean_Aspect
842 begin
843 if Is_False (Static_Boolean (Expr)) then
844 Check_False_Aspect_For_Derived_Type;
846 else
847 Prag :=
848 Make_Pragma (Loc,
849 Pragma_Argument_Associations => New_List (
850 Make_Pragma_Argument_Association (Sloc (Ident),
851 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
853 Pragma_Identifier =>
854 Make_Identifier (Sloc (Ident), Chars (Ident)));
856 Set_From_Aspect_Specification (Prag, True);
857 Set_Corresponding_Aspect (Prag, ASN);
858 Set_Aspect_Rep_Item (ASN, Prag);
859 Set_Is_Delayed_Aspect (Prag);
860 Set_Parent (Prag, ASN);
861 end if;
862 end Make_Pragma_From_Boolean_Aspect;
864 -- Start of processing for Analyze_Aspects_At_Freeze_Point
866 begin
867 -- Must be visible in current scope
869 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
870 return;
871 end if;
873 -- Look for aspect specification entries for this entity
875 ASN := First_Rep_Item (E);
876 while Present (ASN) loop
877 if Nkind (ASN) = N_Aspect_Specification
878 and then Entity (ASN) = E
879 and then Is_Delayed_Aspect (ASN)
880 then
881 A_Id := Get_Aspect_Id (ASN);
883 case A_Id is
885 -- For aspects whose expression is an optional Boolean, make
886 -- the corresponding pragma at the freezing point.
888 when Boolean_Aspects |
889 Library_Unit_Aspects =>
890 Make_Pragma_From_Boolean_Aspect (ASN);
892 -- Special handling for aspects that don't correspond to
893 -- pragmas/attributes.
895 when Aspect_Default_Value |
896 Aspect_Default_Component_Value =>
897 Analyze_Aspect_Default_Value (ASN);
899 -- Ditto for iterator aspects, because the corresponding
900 -- attributes may not have been analyzed yet.
902 when Aspect_Constant_Indexing |
903 Aspect_Variable_Indexing |
904 Aspect_Default_Iterator |
905 Aspect_Iterator_Element =>
906 Analyze (Expression (ASN));
908 when others =>
909 null;
910 end case;
912 Ritem := Aspect_Rep_Item (ASN);
914 if Present (Ritem) then
915 Analyze (Ritem);
916 end if;
917 end if;
919 Next_Rep_Item (ASN);
920 end loop;
921 end Analyze_Aspects_At_Freeze_Point;
923 -----------------------------------
924 -- Analyze_Aspect_Specifications --
925 -----------------------------------
927 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
928 procedure Decorate_Delayed_Aspect_And_Pragma
929 (Asp : Node_Id;
930 Prag : Node_Id);
931 -- Establish the linkages between a delayed aspect and its corresponding
932 -- pragma. Set all delay-related flags on both constructs.
934 procedure Insert_Delayed_Pragma (Prag : Node_Id);
935 -- Insert a postcondition-like pragma into the tree depending on the
936 -- context. Prag must denote one of the following: Pre, Post, Depends,
937 -- Global or Contract_Cases.
939 ----------------------------------------
940 -- Decorate_Delayed_Aspect_And_Pragma --
941 ----------------------------------------
943 procedure Decorate_Delayed_Aspect_And_Pragma
944 (Asp : Node_Id;
945 Prag : Node_Id)
947 begin
948 Set_Aspect_Rep_Item (Asp, Prag);
949 Set_Corresponding_Aspect (Prag, Asp);
950 Set_From_Aspect_Specification (Prag);
951 Set_Is_Delayed_Aspect (Prag);
952 Set_Is_Delayed_Aspect (Asp);
953 Set_Parent (Prag, Asp);
954 end Decorate_Delayed_Aspect_And_Pragma;
956 ---------------------------
957 -- Insert_Delayed_Pragma --
958 ---------------------------
960 procedure Insert_Delayed_Pragma (Prag : Node_Id) is
961 Aux : Node_Id;
963 begin
964 -- When the context is a library unit, the pragma is added to the
965 -- Pragmas_After list.
967 if Nkind (Parent (N)) = N_Compilation_Unit then
968 Aux := Aux_Decls_Node (Parent (N));
970 if No (Pragmas_After (Aux)) then
971 Set_Pragmas_After (Aux, New_List);
972 end if;
974 Prepend (Prag, Pragmas_After (Aux));
976 -- Pragmas associated with subprogram bodies are inserted in the
977 -- declarative part.
979 elsif Nkind (N) = N_Subprogram_Body then
980 if No (Declarations (N)) then
981 Set_Declarations (N, New_List);
982 end if;
984 Append (Prag, Declarations (N));
986 -- Default
988 else
989 Insert_After (N, Prag);
991 -- Analyze the pragma before analyzing the proper body of a stub.
992 -- This ensures that the pragma will appear on the proper contract
993 -- list (see N_Contract).
995 if Nkind (N) = N_Subprogram_Body_Stub then
996 Analyze (Prag);
997 end if;
998 end if;
999 end Insert_Delayed_Pragma;
1001 -- Local variables
1003 Aspect : Node_Id;
1004 Aitem : Node_Id;
1005 Ent : Node_Id;
1007 L : constant List_Id := Aspect_Specifications (N);
1009 Ins_Node : Node_Id := N;
1010 -- Insert pragmas/attribute definition clause after this node when no
1011 -- delayed analysis is required.
1013 -- Start of processing for Analyze_Aspect_Specifications
1015 -- The general processing involves building an attribute definition
1016 -- clause or a pragma node that corresponds to the aspect. Then in order
1017 -- to delay the evaluation of this aspect to the freeze point, we attach
1018 -- the corresponding pragma/attribute definition clause to the aspect
1019 -- specification node, which is then placed in the Rep Item chain. In
1020 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1021 -- and we evaluate the rep item at the freeze point. When the aspect
1022 -- doesn't have a corresponding pragma/attribute definition clause, then
1023 -- its analysis is simply delayed at the freeze point.
1025 -- Some special cases don't require delay analysis, thus the aspect is
1026 -- analyzed right now.
1028 -- Note that there is a special handling for Pre, Post, Test_Case,
1029 -- Contract_Cases aspects. In these cases, we do not have to worry
1030 -- about delay issues, since the pragmas themselves deal with delay
1031 -- of visibility for the expression analysis. Thus, we just insert
1032 -- the pragma after the node N.
1034 begin
1035 pragma Assert (Present (L));
1037 -- Loop through aspects
1039 Aspect := First (L);
1040 Aspect_Loop : while Present (Aspect) loop
1041 Analyze_One_Aspect : declare
1042 Expr : constant Node_Id := Expression (Aspect);
1043 Id : constant Node_Id := Identifier (Aspect);
1044 Loc : constant Source_Ptr := Sloc (Aspect);
1045 Nam : constant Name_Id := Chars (Id);
1046 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1047 Anod : Node_Id;
1049 Delay_Required : Boolean := True;
1050 -- Set False if delay is not required
1052 Eloc : Source_Ptr := No_Location;
1053 -- Source location of expression, modified when we split PPC's. It
1054 -- is set below when Expr is present.
1056 procedure Analyze_Aspect_External_Or_Link_Name;
1057 -- Perform analysis of the External_Name or Link_Name aspects
1059 procedure Analyze_Aspect_Implicit_Dereference;
1060 -- Perform analysis of the Implicit_Dereference aspects
1062 procedure Make_Aitem_Pragma
1063 (Pragma_Argument_Associations : List_Id;
1064 Pragma_Name : Name_Id);
1065 -- This is a wrapper for Make_Pragma used for converting aspects
1066 -- to pragmas. It takes care of Sloc (set from Loc) and building
1067 -- the pragma identifier from the given name. In addition the
1068 -- flags Class_Present and Split_PPC are set from the aspect
1069 -- node, as well as Is_Ignored. This routine also sets the
1070 -- From_Aspect_Specification in the resulting pragma node to
1071 -- True, and sets Corresponding_Aspect to point to the aspect.
1072 -- The resulting pragma is assigned to Aitem.
1074 ------------------------------------------
1075 -- Analyze_Aspect_External_Or_Link_Name --
1076 ------------------------------------------
1078 procedure Analyze_Aspect_External_Or_Link_Name is
1079 begin
1080 -- Verify that there is an Import/Export aspect defined for the
1081 -- entity. The processing of that aspect in turn checks that
1082 -- there is a Convention aspect declared. The pragma is
1083 -- constructed when processing the Convention aspect.
1085 declare
1086 A : Node_Id;
1088 begin
1089 A := First (L);
1090 while Present (A) loop
1091 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1092 Name_Import);
1093 Next (A);
1094 end loop;
1096 if No (A) then
1097 Error_Msg_N
1098 ("missing Import/Export for Link/External name",
1099 Aspect);
1100 end if;
1101 end;
1102 end Analyze_Aspect_External_Or_Link_Name;
1104 -----------------------------------------
1105 -- Analyze_Aspect_Implicit_Dereference --
1106 -----------------------------------------
1108 procedure Analyze_Aspect_Implicit_Dereference is
1109 begin
1110 if not Is_Type (E) or else not Has_Discriminants (E) then
1111 Error_Msg_N
1112 ("aspect must apply to a type with discriminants", N);
1114 else
1115 declare
1116 Disc : Entity_Id;
1118 begin
1119 Disc := First_Discriminant (E);
1120 while Present (Disc) loop
1121 if Chars (Expr) = Chars (Disc)
1122 and then Ekind (Etype (Disc)) =
1123 E_Anonymous_Access_Type
1124 then
1125 Set_Has_Implicit_Dereference (E);
1126 Set_Has_Implicit_Dereference (Disc);
1127 return;
1128 end if;
1130 Next_Discriminant (Disc);
1131 end loop;
1133 -- Error if no proper access discriminant.
1135 Error_Msg_NE
1136 ("not an access discriminant of&", Expr, E);
1137 end;
1138 end if;
1139 end Analyze_Aspect_Implicit_Dereference;
1141 -----------------------
1142 -- Make_Aitem_Pragma --
1143 -----------------------
1145 procedure Make_Aitem_Pragma
1146 (Pragma_Argument_Associations : List_Id;
1147 Pragma_Name : Name_Id)
1149 begin
1150 -- We should never get here if aspect was disabled
1152 pragma Assert (not Is_Disabled (Aspect));
1154 -- Build the pragma
1156 Aitem :=
1157 Make_Pragma (Loc,
1158 Pragma_Argument_Associations =>
1159 Pragma_Argument_Associations,
1160 Pragma_Identifier =>
1161 Make_Identifier (Sloc (Id), Pragma_Name),
1162 Class_Present => Class_Present (Aspect),
1163 Split_PPC => Split_PPC (Aspect));
1165 -- Set additional semantic fields
1167 if Is_Ignored (Aspect) then
1168 Set_Is_Ignored (Aitem);
1169 end if;
1171 Set_Corresponding_Aspect (Aitem, Aspect);
1172 Set_From_Aspect_Specification (Aitem, True);
1173 end Make_Aitem_Pragma;
1175 -- Start of processing for Analyze_One_Aspect
1177 begin
1178 -- Skip aspect if already analyzed (not clear if this is needed)
1180 if Analyzed (Aspect) then
1181 goto Continue;
1182 end if;
1184 -- Skip looking at aspect if it is totally disabled. Just mark
1185 -- it as such for later reference in the tree. This also sets
1186 -- the Is_Ignored flag appropriately.
1188 Check_Applicable_Policy (Aspect);
1190 if Is_Disabled (Aspect) then
1191 goto Continue;
1192 end if;
1194 -- Set the source location of expression, used in the case of
1195 -- a failed precondition/postcondition or invariant. Note that
1196 -- the source location of the expression is not usually the best
1197 -- choice here. For example, it gets located on the last AND
1198 -- keyword in a chain of boolean expressiond AND'ed together.
1199 -- It is best to put the message on the first character of the
1200 -- assertion, which is the effect of the First_Node call here.
1202 if Present (Expr) then
1203 Eloc := Sloc (First_Node (Expr));
1204 end if;
1206 -- Check restriction No_Implementation_Aspect_Specifications
1208 if Implementation_Defined_Aspect (A_Id) then
1209 Check_Restriction
1210 (No_Implementation_Aspect_Specifications, Aspect);
1211 end if;
1213 -- Check restriction No_Specification_Of_Aspect
1215 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1217 -- Analyze this aspect (actual analysis is delayed till later)
1219 Set_Analyzed (Aspect);
1220 Set_Entity (Aspect, E);
1221 Ent := New_Occurrence_Of (E, Sloc (Id));
1223 -- Check for duplicate aspect. Note that the Comes_From_Source
1224 -- test allows duplicate Pre/Post's that we generate internally
1225 -- to escape being flagged here.
1227 if No_Duplicates_Allowed (A_Id) then
1228 Anod := First (L);
1229 while Anod /= Aspect loop
1230 if Comes_From_Source (Aspect)
1231 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1232 then
1233 Error_Msg_Name_1 := Nam;
1234 Error_Msg_Sloc := Sloc (Anod);
1236 -- Case of same aspect specified twice
1238 if Class_Present (Anod) = Class_Present (Aspect) then
1239 if not Class_Present (Anod) then
1240 Error_Msg_NE
1241 ("aspect% for & previously given#",
1242 Id, E);
1243 else
1244 Error_Msg_NE
1245 ("aspect `%''Class` for & previously given#",
1246 Id, E);
1247 end if;
1248 end if;
1249 end if;
1251 Next (Anod);
1252 end loop;
1253 end if;
1255 -- Check some general restrictions on language defined aspects
1257 if not Implementation_Defined_Aspect (A_Id) then
1258 Error_Msg_Name_1 := Nam;
1260 -- Not allowed for renaming declarations
1262 if Nkind (N) in N_Renaming_Declaration then
1263 Error_Msg_N
1264 ("aspect % not allowed for renaming declaration",
1265 Aspect);
1266 end if;
1268 -- Not allowed for formal type declarations
1270 if Nkind (N) = N_Formal_Type_Declaration then
1271 Error_Msg_N
1272 ("aspect % not allowed for formal type declaration",
1273 Aspect);
1274 end if;
1275 end if;
1277 -- Copy expression for later processing by the procedures
1278 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1280 Set_Entity (Id, New_Copy_Tree (Expr));
1282 -- Processing based on specific aspect
1284 case A_Id is
1286 -- No_Aspect should be impossible
1288 when No_Aspect =>
1289 raise Program_Error;
1291 -- Case 1: Aspects corresponding to attribute definition
1292 -- clauses.
1294 when Aspect_Address |
1295 Aspect_Alignment |
1296 Aspect_Bit_Order |
1297 Aspect_Component_Size |
1298 Aspect_Constant_Indexing |
1299 Aspect_Default_Iterator |
1300 Aspect_Dispatching_Domain |
1301 Aspect_External_Tag |
1302 Aspect_Input |
1303 Aspect_Iterator_Element |
1304 Aspect_Machine_Radix |
1305 Aspect_Object_Size |
1306 Aspect_Output |
1307 Aspect_Read |
1308 Aspect_Scalar_Storage_Order |
1309 Aspect_Size |
1310 Aspect_Small |
1311 Aspect_Simple_Storage_Pool |
1312 Aspect_Storage_Pool |
1313 Aspect_Stream_Size |
1314 Aspect_Value_Size |
1315 Aspect_Variable_Indexing |
1316 Aspect_Write =>
1318 -- Indexing aspects apply only to tagged type
1320 if (A_Id = Aspect_Constant_Indexing
1321 or else A_Id = Aspect_Variable_Indexing)
1322 and then not (Is_Type (E)
1323 and then Is_Tagged_Type (E))
1324 then
1325 Error_Msg_N ("indexing applies to a tagged type", N);
1326 goto Continue;
1327 end if;
1329 -- Construct the attribute definition clause
1331 Aitem :=
1332 Make_Attribute_Definition_Clause (Loc,
1333 Name => Ent,
1334 Chars => Chars (Id),
1335 Expression => Relocate_Node (Expr));
1337 -- If the address is specified, then we treat the entity as
1338 -- referenced, to avoid spurious warnings. This is analogous
1339 -- to what is done with an attribute definition clause, but
1340 -- here we don't want to generate a reference because this
1341 -- is the point of definition of the entity.
1343 if A_Id = Aspect_Address then
1344 Set_Referenced (E);
1345 end if;
1347 -- Case 2: Aspects corresponding to pragmas
1349 -- Case 2a: Aspects corresponding to pragmas with two
1350 -- arguments, where the first argument is a local name
1351 -- referring to the entity, and the second argument is the
1352 -- aspect definition expression.
1354 -- Suppress/Unsuppress
1356 when Aspect_Suppress |
1357 Aspect_Unsuppress =>
1359 Make_Aitem_Pragma
1360 (Pragma_Argument_Associations => New_List (
1361 Make_Pragma_Argument_Association (Loc,
1362 Expression => New_Occurrence_Of (E, Loc)),
1363 Make_Pragma_Argument_Association (Sloc (Expr),
1364 Expression => Relocate_Node (Expr))),
1365 Pragma_Name => Chars (Id));
1367 -- Synchronization
1369 -- Corresponds to pragma Implemented, construct the pragma
1371 when Aspect_Synchronization =>
1373 Make_Aitem_Pragma
1374 (Pragma_Argument_Associations => New_List (
1375 Make_Pragma_Argument_Association (Loc,
1376 Expression => New_Occurrence_Of (E, Loc)),
1377 Make_Pragma_Argument_Association (Sloc (Expr),
1378 Expression => Relocate_Node (Expr))),
1379 Pragma_Name => Name_Implemented);
1381 -- No delay is required since the only values are: By_Entry
1382 -- | By_Protected_Procedure | By_Any | Optional which don't
1383 -- get analyzed anyway.
1385 Delay_Required := False;
1387 -- Attach Handler
1389 when Aspect_Attach_Handler =>
1390 Make_Aitem_Pragma
1391 (Pragma_Argument_Associations => New_List (
1392 Make_Pragma_Argument_Association (Sloc (Ent),
1393 Expression => Ent),
1394 Make_Pragma_Argument_Association (Sloc (Expr),
1395 Expression => Relocate_Node (Expr))),
1396 Pragma_Name => Name_Attach_Handler);
1398 -- Dynamic_Predicate, Predicate, Static_Predicate
1400 when Aspect_Dynamic_Predicate |
1401 Aspect_Predicate |
1402 Aspect_Static_Predicate =>
1404 -- Construct the pragma (always a pragma Predicate, with
1405 -- flags recording whether it is static/dynamic). We also
1406 -- set flags recording this in the type itself.
1408 Make_Aitem_Pragma
1409 (Pragma_Argument_Associations => New_List (
1410 Make_Pragma_Argument_Association (Sloc (Ent),
1411 Expression => Ent),
1412 Make_Pragma_Argument_Association (Sloc (Expr),
1413 Expression => Relocate_Node (Expr))),
1414 Pragma_Name => Name_Predicate);
1416 -- Mark type has predicates, and remember what kind of
1417 -- aspect lead to this predicate (we need this to access
1418 -- the right set of check policies later on).
1420 Set_Has_Predicates (E);
1422 if A_Id = Aspect_Dynamic_Predicate then
1423 Set_Has_Dynamic_Predicate_Aspect (E);
1424 elsif A_Id = Aspect_Static_Predicate then
1425 Set_Has_Static_Predicate_Aspect (E);
1426 end if;
1428 -- If the type is private, indicate that its completion
1429 -- has a freeze node, because that is the one that will be
1430 -- visible at freeze time.
1432 if Is_Private_Type (E) and then Present (Full_View (E)) then
1433 Set_Has_Predicates (Full_View (E));
1435 if A_Id = Aspect_Dynamic_Predicate then
1436 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1437 elsif A_Id = Aspect_Static_Predicate then
1438 Set_Has_Static_Predicate_Aspect (Full_View (E));
1439 end if;
1441 Set_Has_Delayed_Aspects (Full_View (E));
1442 Ensure_Freeze_Node (Full_View (E));
1443 end if;
1445 -- Case 2b: Aspects corresponding to pragmas with two
1446 -- arguments, where the second argument is a local name
1447 -- referring to the entity, and the first argument is the
1448 -- aspect definition expression.
1450 -- Convention
1452 when Aspect_Convention =>
1454 -- The aspect may be part of the specification of an import
1455 -- or export pragma. Scan the aspect list to gather the
1456 -- other components, if any. The name of the generated
1457 -- pragma is one of Convention/Import/Export.
1459 declare
1460 P_Name : Name_Id;
1461 A_Name : Name_Id;
1462 A : Node_Id;
1463 Arg_List : List_Id;
1464 Found : Boolean;
1465 L_Assoc : Node_Id;
1466 E_Assoc : Node_Id;
1468 begin
1469 P_Name := Chars (Id);
1470 Found := False;
1471 Arg_List := New_List;
1472 L_Assoc := Empty;
1473 E_Assoc := Empty;
1475 A := First (L);
1476 while Present (A) loop
1477 A_Name := Chars (Identifier (A));
1479 if Nam_In (A_Name, Name_Import, Name_Export) then
1480 if Found then
1481 Error_Msg_N ("conflicting", A);
1482 else
1483 Found := True;
1484 end if;
1486 P_Name := A_Name;
1488 elsif A_Name = Name_Link_Name then
1489 L_Assoc :=
1490 Make_Pragma_Argument_Association (Loc,
1491 Chars => A_Name,
1492 Expression => Relocate_Node (Expression (A)));
1494 elsif A_Name = Name_External_Name then
1495 E_Assoc :=
1496 Make_Pragma_Argument_Association (Loc,
1497 Chars => A_Name,
1498 Expression => Relocate_Node (Expression (A)));
1499 end if;
1501 Next (A);
1502 end loop;
1504 Arg_List := New_List (
1505 Make_Pragma_Argument_Association (Sloc (Expr),
1506 Expression => Relocate_Node (Expr)),
1507 Make_Pragma_Argument_Association (Sloc (Ent),
1508 Expression => Ent));
1510 if Present (L_Assoc) then
1511 Append_To (Arg_List, L_Assoc);
1512 end if;
1514 if Present (E_Assoc) then
1515 Append_To (Arg_List, E_Assoc);
1516 end if;
1518 Make_Aitem_Pragma
1519 (Pragma_Argument_Associations => Arg_List,
1520 Pragma_Name => P_Name);
1522 -- Convention is a static name, and must be associated
1523 -- with the entity at once.
1525 Delay_Required := False;
1526 end;
1528 -- CPU, Interrupt_Priority, Priority
1530 -- These three aspects can be specified for a subprogram body,
1531 -- in which case we generate pragmas for them and insert them
1532 -- ahead of local declarations, rather than after the body.
1534 when Aspect_CPU |
1535 Aspect_Interrupt_Priority |
1536 Aspect_Priority =>
1538 if Nkind (N) = N_Subprogram_Body then
1539 Make_Aitem_Pragma
1540 (Pragma_Argument_Associations => New_List (
1541 Make_Pragma_Argument_Association (Sloc (Expr),
1542 Expression => Relocate_Node (Expr))),
1543 Pragma_Name => Chars (Id));
1545 else
1546 Aitem :=
1547 Make_Attribute_Definition_Clause (Loc,
1548 Name => Ent,
1549 Chars => Chars (Id),
1550 Expression => Relocate_Node (Expr));
1551 end if;
1553 -- Warnings
1555 when Aspect_Warnings =>
1557 Make_Aitem_Pragma
1558 (Pragma_Argument_Associations => New_List (
1559 Make_Pragma_Argument_Association (Sloc (Expr),
1560 Expression => Relocate_Node (Expr)),
1561 Make_Pragma_Argument_Association (Loc,
1562 Expression => New_Occurrence_Of (E, Loc))),
1563 Pragma_Name => Chars (Id));
1565 -- We don't have to play the delay game here, since the only
1566 -- values are ON/OFF which don't get analyzed anyway.
1568 Delay_Required := False;
1570 -- Case 2c: Aspects corresponding to pragmas with three
1571 -- arguments.
1573 -- Invariant aspects have a first argument that references the
1574 -- entity, a second argument that is the expression and a third
1575 -- argument that is an appropriate message.
1577 -- Invariant, Type_Invariant
1579 when Aspect_Invariant |
1580 Aspect_Type_Invariant =>
1582 -- Analysis of the pragma will verify placement legality:
1583 -- an invariant must apply to a private type, or appear in
1584 -- the private part of a spec and apply to a completion.
1586 Make_Aitem_Pragma
1587 (Pragma_Argument_Associations => New_List (
1588 Make_Pragma_Argument_Association (Sloc (Ent),
1589 Expression => Ent),
1590 Make_Pragma_Argument_Association (Sloc (Expr),
1591 Expression => Relocate_Node (Expr))),
1592 Pragma_Name => Name_Invariant);
1594 -- Add message unless exception messages are suppressed
1596 if not Opt.Exception_Locations_Suppressed then
1597 Append_To (Pragma_Argument_Associations (Aitem),
1598 Make_Pragma_Argument_Association (Eloc,
1599 Chars => Name_Message,
1600 Expression =>
1601 Make_String_Literal (Eloc,
1602 Strval => "failed invariant from "
1603 & Build_Location_String (Eloc))));
1604 end if;
1606 -- For Invariant case, insert immediately after the entity
1607 -- declaration. We do not have to worry about delay issues
1608 -- since the pragma processing takes care of this.
1610 Delay_Required := False;
1612 -- Case 2d : Aspects that correspond to a pragma with one
1613 -- argument.
1615 -- Abstract_State
1617 when Aspect_Abstract_State =>
1618 Make_Aitem_Pragma
1619 (Pragma_Argument_Associations => New_List (
1620 Make_Pragma_Argument_Association (Loc,
1621 Expression => Relocate_Node (Expr))),
1622 Pragma_Name => Name_Abstract_State);
1623 Delay_Required := False;
1625 -- Depends
1627 -- Aspect Depends must be delayed because it mentions names
1628 -- of inputs and output that are classified by aspect Global.
1629 -- The aspect and pragma are treated the same way as a post
1630 -- condition.
1632 when Aspect_Depends =>
1633 Make_Aitem_Pragma
1634 (Pragma_Argument_Associations => New_List (
1635 Make_Pragma_Argument_Association (Loc,
1636 Expression => Relocate_Node (Expr))),
1637 Pragma_Name => Name_Depends);
1639 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
1640 Insert_Delayed_Pragma (Aitem);
1641 goto Continue;
1643 -- Global
1645 -- Aspect Global must be delayed because it can mention names
1646 -- and benefit from the forward visibility rules applicable to
1647 -- aspects of subprograms. The aspect and pragma are treated
1648 -- the same way as a post condition.
1650 when Aspect_Global =>
1651 Make_Aitem_Pragma
1652 (Pragma_Argument_Associations => New_List (
1653 Make_Pragma_Argument_Association (Loc,
1654 Expression => Relocate_Node (Expr))),
1655 Pragma_Name => Name_Global);
1657 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
1658 Insert_Delayed_Pragma (Aitem);
1659 goto Continue;
1661 -- SPARK_Mode
1663 when Aspect_SPARK_Mode =>
1664 Make_Aitem_Pragma
1665 (Pragma_Argument_Associations => New_List (
1666 Make_Pragma_Argument_Association (Loc,
1667 Expression => Relocate_Node (Expr))),
1668 Pragma_Name => Name_SPARK_Mode);
1669 Delay_Required := False;
1671 -- Relative_Deadline
1673 when Aspect_Relative_Deadline =>
1674 Make_Aitem_Pragma
1675 (Pragma_Argument_Associations => New_List (
1676 Make_Pragma_Argument_Association (Loc,
1677 Expression => Relocate_Node (Expr))),
1678 Pragma_Name => Name_Relative_Deadline);
1680 -- If the aspect applies to a task, the corresponding pragma
1681 -- must appear within its declarations, not after.
1683 if Nkind (N) = N_Task_Type_Declaration then
1684 declare
1685 Def : Node_Id;
1686 V : List_Id;
1688 begin
1689 if No (Task_Definition (N)) then
1690 Set_Task_Definition (N,
1691 Make_Task_Definition (Loc,
1692 Visible_Declarations => New_List,
1693 End_Label => Empty));
1694 end if;
1696 Def := Task_Definition (N);
1697 V := Visible_Declarations (Def);
1698 if not Is_Empty_List (V) then
1699 Insert_Before (First (V), Aitem);
1701 else
1702 Set_Visible_Declarations (Def, New_List (Aitem));
1703 end if;
1705 goto Continue;
1706 end;
1707 end if;
1709 -- Case 3 : Aspects that don't correspond to pragma/attribute
1710 -- definition clause.
1712 -- Case 3a: The aspects listed below don't correspond to
1713 -- pragmas/attributes but do require delayed analysis.
1715 -- Default_Value, Default_Component_Value
1717 when Aspect_Default_Value |
1718 Aspect_Default_Component_Value =>
1719 Aitem := Empty;
1721 -- Case 3b: The aspects listed below don't correspond to
1722 -- pragmas/attributes and don't need delayed analysis.
1724 -- Implicit_Dereference
1726 -- For Implicit_Dereference, External_Name and Link_Name, only
1727 -- the legality checks are done during the analysis, thus no
1728 -- delay is required.
1730 when Aspect_Implicit_Dereference =>
1731 Analyze_Aspect_Implicit_Dereference;
1732 goto Continue;
1734 -- External_Name, Link_Name
1736 when Aspect_External_Name |
1737 Aspect_Link_Name =>
1738 Analyze_Aspect_External_Or_Link_Name;
1739 goto Continue;
1741 -- Dimension
1743 when Aspect_Dimension =>
1744 Analyze_Aspect_Dimension (N, Id, Expr);
1745 goto Continue;
1747 -- Dimension_System
1749 when Aspect_Dimension_System =>
1750 Analyze_Aspect_Dimension_System (N, Id, Expr);
1751 goto Continue;
1753 -- Case 4: Aspects requiring special handling
1755 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
1756 -- pragmas take care of the delay.
1758 -- Pre/Post
1760 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1761 -- with a first argument that is the expression, and a second
1762 -- argument that is an informative message if the test fails.
1763 -- This is inserted right after the declaration, to get the
1764 -- required pragma placement. The processing for the pragmas
1765 -- takes care of the required delay.
1767 when Pre_Post_Aspects => Pre_Post : declare
1768 Pname : Name_Id;
1770 begin
1771 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1772 Pname := Name_Precondition;
1773 else
1774 Pname := Name_Postcondition;
1775 end if;
1777 -- If the expressions is of the form A and then B, then
1778 -- we generate separate Pre/Post aspects for the separate
1779 -- clauses. Since we allow multiple pragmas, there is no
1780 -- problem in allowing multiple Pre/Post aspects internally.
1781 -- These should be treated in reverse order (B first and
1782 -- A second) since they are later inserted just after N in
1783 -- the order they are treated. This way, the pragma for A
1784 -- ends up preceding the pragma for B, which may have an
1785 -- importance for the error raised (either constraint error
1786 -- or precondition error).
1788 -- We do not do this for Pre'Class, since we have to put
1789 -- these conditions together in a complex OR expression
1791 -- We do not do this in ASIS mode, as ASIS relies on the
1792 -- original node representing the complete expression, when
1793 -- retrieving it through the source aspect table.
1795 if not ASIS_Mode
1796 and then (Pname = Name_Postcondition
1797 or else not Class_Present (Aspect))
1798 then
1799 while Nkind (Expr) = N_And_Then loop
1800 Insert_After (Aspect,
1801 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1802 Identifier => Identifier (Aspect),
1803 Expression => Relocate_Node (Left_Opnd (Expr)),
1804 Class_Present => Class_Present (Aspect),
1805 Split_PPC => True));
1806 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1807 Eloc := Sloc (Expr);
1808 end loop;
1809 end if;
1811 -- Build the precondition/postcondition pragma
1813 -- Add note about why we do NOT need Copy_Tree here ???
1815 Make_Aitem_Pragma
1816 (Pragma_Argument_Associations => New_List (
1817 Make_Pragma_Argument_Association (Eloc,
1818 Chars => Name_Check,
1819 Expression => Relocate_Node (Expr))),
1820 Pragma_Name => Pname);
1822 -- Add message unless exception messages are suppressed
1824 if not Opt.Exception_Locations_Suppressed then
1825 Append_To (Pragma_Argument_Associations (Aitem),
1826 Make_Pragma_Argument_Association (Eloc,
1827 Chars => Name_Message,
1828 Expression =>
1829 Make_String_Literal (Eloc,
1830 Strval => "failed "
1831 & Get_Name_String (Pname)
1832 & " from "
1833 & Build_Location_String (Eloc))));
1834 end if;
1836 Set_Is_Delayed_Aspect (Aspect);
1838 -- For Pre/Post cases, insert immediately after the entity
1839 -- declaration, since that is the required pragma placement.
1840 -- Note that for these aspects, we do not have to worry
1841 -- about delay issues, since the pragmas themselves deal
1842 -- with delay of visibility for the expression analysis.
1844 Insert_Delayed_Pragma (Aitem);
1845 goto Continue;
1846 end Pre_Post;
1848 -- Test_Case
1850 when Aspect_Test_Case => Test_Case : declare
1851 Args : List_Id;
1852 Comp_Expr : Node_Id;
1853 Comp_Assn : Node_Id;
1854 New_Expr : Node_Id;
1856 begin
1857 Args := New_List;
1859 if Nkind (Parent (N)) = N_Compilation_Unit then
1860 Error_Msg_Name_1 := Nam;
1861 Error_Msg_N ("incorrect placement of aspect `%`", E);
1862 goto Continue;
1863 end if;
1865 if Nkind (Expr) /= N_Aggregate then
1866 Error_Msg_Name_1 := Nam;
1867 Error_Msg_NE
1868 ("wrong syntax for aspect `%` for &", Id, E);
1869 goto Continue;
1870 end if;
1872 -- Make pragma expressions refer to the original aspect
1873 -- expressions through the Original_Node link. This is
1874 -- used in semantic analysis for ASIS mode, so that the
1875 -- original expression also gets analyzed.
1877 Comp_Expr := First (Expressions (Expr));
1878 while Present (Comp_Expr) loop
1879 New_Expr := Relocate_Node (Comp_Expr);
1880 Set_Original_Node (New_Expr, Comp_Expr);
1881 Append_To (Args,
1882 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1883 Expression => New_Expr));
1884 Next (Comp_Expr);
1885 end loop;
1887 Comp_Assn := First (Component_Associations (Expr));
1888 while Present (Comp_Assn) loop
1889 if List_Length (Choices (Comp_Assn)) /= 1
1890 or else
1891 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1892 then
1893 Error_Msg_Name_1 := Nam;
1894 Error_Msg_NE
1895 ("wrong syntax for aspect `%` for &", Id, E);
1896 goto Continue;
1897 end if;
1899 New_Expr := Relocate_Node (Expression (Comp_Assn));
1900 Set_Original_Node (New_Expr, Expression (Comp_Assn));
1901 Append_To (Args,
1902 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
1903 Chars => Chars (First (Choices (Comp_Assn))),
1904 Expression => New_Expr));
1905 Next (Comp_Assn);
1906 end loop;
1908 -- Build the test-case pragma
1910 Make_Aitem_Pragma
1911 (Pragma_Argument_Associations => Args,
1912 Pragma_Name => Nam);
1914 Delay_Required := False;
1915 end Test_Case;
1917 -- Contract_Cases
1919 when Aspect_Contract_Cases =>
1920 Make_Aitem_Pragma
1921 (Pragma_Argument_Associations => New_List (
1922 Make_Pragma_Argument_Association (Loc,
1923 Expression => Relocate_Node (Expr))),
1924 Pragma_Name => Nam);
1926 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
1927 Insert_Delayed_Pragma (Aitem);
1928 goto Continue;
1930 -- Case 5: Special handling for aspects with an optional
1931 -- boolean argument.
1933 -- In the general case, the corresponding pragma cannot be
1934 -- generated yet because the evaluation of the boolean needs
1935 -- to be delayed till the freeze point.
1937 when Boolean_Aspects |
1938 Library_Unit_Aspects =>
1940 Set_Is_Boolean_Aspect (Aspect);
1942 -- Lock_Free aspect only apply to protected objects
1944 if A_Id = Aspect_Lock_Free then
1945 if Ekind (E) /= E_Protected_Type then
1946 Error_Msg_Name_1 := Nam;
1947 Error_Msg_N
1948 ("aspect % only applies to a protected object",
1949 Aspect);
1951 else
1952 -- Set the Uses_Lock_Free flag to True if there is no
1953 -- expression or if the expression is True. ??? The
1954 -- evaluation of this aspect should be delayed to the
1955 -- freeze point.
1957 if No (Expr)
1958 or else Is_True (Static_Boolean (Expr))
1959 then
1960 Set_Uses_Lock_Free (E);
1961 end if;
1963 Record_Rep_Item (E, Aspect);
1964 end if;
1966 goto Continue;
1968 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
1970 -- Verify that there is an aspect Convention that will
1971 -- incorporate the Import/Export aspect, and eventual
1972 -- Link/External names.
1974 declare
1975 A : Node_Id;
1977 begin
1978 A := First (L);
1979 while Present (A) loop
1980 exit when Chars (Identifier (A)) = Name_Convention;
1981 Next (A);
1982 end loop;
1984 if No (A) then
1985 Error_Msg_N
1986 ("missing Convention aspect for Export/Import",
1987 Aspect);
1988 end if;
1989 end;
1991 goto Continue;
1992 end if;
1994 -- This requires special handling in the case of a package
1995 -- declaration, the pragma needs to be inserted in the list
1996 -- of declarations for the associated package. There is no
1997 -- issue of visibility delay for these aspects.
1999 if A_Id in Library_Unit_Aspects
2000 and then
2001 Nkind_In (N, N_Package_Declaration,
2002 N_Generic_Package_Declaration)
2003 and then Nkind (Parent (N)) /= N_Compilation_Unit
2004 then
2005 Error_Msg_N
2006 ("incorrect context for library unit aspect&", Id);
2007 goto Continue;
2008 end if;
2010 -- Special handling when the aspect has no expression. In
2011 -- this case the value is considered to be True. Thus, we
2012 -- simply insert the pragma, no delay is required.
2014 if No (Expr) then
2015 Make_Aitem_Pragma
2016 (Pragma_Argument_Associations => New_List (
2017 Make_Pragma_Argument_Association (Sloc (Ent),
2018 Expression => Ent)),
2019 Pragma_Name => Chars (Id));
2021 Delay_Required := False;
2023 -- In general cases, the corresponding pragma/attribute
2024 -- definition clause will be inserted later at the freezing
2025 -- point.
2027 else
2028 Aitem := Empty;
2029 end if;
2031 -- Storage_Size
2033 -- This is special because for access types we need to generate
2034 -- an attribute definition clause. This also works for single
2035 -- task declarations, but it does not work for task type
2036 -- declarations, because we have the case where the expression
2037 -- references a discriminant of the task type. That can't use
2038 -- an attribute definition clause because we would not have
2039 -- visibility on the discriminant. For that case we must
2040 -- generate a pragma in the task definition.
2042 when Aspect_Storage_Size =>
2044 -- Task type case
2046 if Ekind (E) = E_Task_Type then
2047 declare
2048 Decl : constant Node_Id := Declaration_Node (E);
2050 begin
2051 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
2053 -- If no task definition, create one
2055 if No (Task_Definition (Decl)) then
2056 Set_Task_Definition (Decl,
2057 Make_Task_Definition (Loc,
2058 Visible_Declarations => Empty_List,
2059 End_Label => Empty));
2060 end if;
2062 -- Create a pragma and put it at the start of the
2063 -- task definition for the task type declaration.
2065 Make_Aitem_Pragma
2066 (Pragma_Argument_Associations => New_List (
2067 Make_Pragma_Argument_Association (Loc,
2068 Expression => Relocate_Node (Expr))),
2069 Pragma_Name => Name_Storage_Size);
2071 Prepend
2072 (Aitem,
2073 Visible_Declarations (Task_Definition (Decl)));
2074 goto Continue;
2075 end;
2077 -- All other cases, generate attribute definition
2079 else
2080 Aitem :=
2081 Make_Attribute_Definition_Clause (Loc,
2082 Name => Ent,
2083 Chars => Chars (Id),
2084 Expression => Relocate_Node (Expr));
2085 end if;
2086 end case;
2088 -- Attach the corresponding pragma/attribute definition clause to
2089 -- the aspect specification node.
2091 if Present (Aitem) then
2092 Set_From_Aspect_Specification (Aitem, True);
2093 end if;
2095 -- Aspect Abstract_State introduces implicit declarations for all
2096 -- state abstraction entities it defines. To emulate this behavior
2097 -- insert the pragma at the start of the visible declarations of
2098 -- the related package.
2100 if Nam = Name_Abstract_State
2101 and then Nkind (N) = N_Package_Declaration
2102 then
2103 if No (Visible_Declarations (Specification (N))) then
2104 Set_Visible_Declarations (Specification (N), New_List);
2105 end if;
2107 Prepend (Aitem, Visible_Declarations (Specification (N)));
2108 goto Continue;
2110 -- In the context of a compilation unit, we directly put the
2111 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
2112 -- node (no delay is required here) except for aspects on a
2113 -- subprogram body (see below) and a generic package, for which
2114 -- we need to introduce the pragma before building the generic
2115 -- copy (see sem_ch12).
2117 elsif Nkind (Parent (N)) = N_Compilation_Unit
2118 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
2119 then
2120 declare
2121 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
2123 begin
2124 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
2126 -- For a Boolean aspect, create the corresponding pragma if
2127 -- no expression or if the value is True.
2129 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
2130 if Is_True (Static_Boolean (Expr)) then
2131 Make_Aitem_Pragma
2132 (Pragma_Argument_Associations => New_List (
2133 Make_Pragma_Argument_Association (Sloc (Ent),
2134 Expression => Ent)),
2135 Pragma_Name => Chars (Id));
2137 Set_From_Aspect_Specification (Aitem, True);
2138 Set_Corresponding_Aspect (Aitem, Aspect);
2140 else
2141 goto Continue;
2142 end if;
2143 end if;
2145 -- If the aspect is on a subprogram body (relevant aspects
2146 -- are Inline and Priority), add the pragma in front of
2147 -- the declarations.
2149 if Nkind (N) = N_Subprogram_Body then
2150 if No (Declarations (N)) then
2151 Set_Declarations (N, New_List);
2152 end if;
2154 Prepend (Aitem, Declarations (N));
2156 elsif Nkind (N) = N_Generic_Package_Declaration then
2157 if No (Visible_Declarations (Specification (N))) then
2158 Set_Visible_Declarations (Specification (N), New_List);
2159 end if;
2161 Prepend (Aitem,
2162 Visible_Declarations (Specification (N)));
2164 else
2165 if No (Pragmas_After (Aux)) then
2166 Set_Pragmas_After (Aux, New_List);
2167 end if;
2169 Append (Aitem, Pragmas_After (Aux));
2170 end if;
2172 goto Continue;
2173 end;
2174 end if;
2176 -- The evaluation of the aspect is delayed to the freezing point.
2177 -- The pragma or attribute clause if there is one is then attached
2178 -- to the aspect specification which is placed in the rep item
2179 -- list.
2181 if Delay_Required then
2182 if Present (Aitem) then
2183 Set_Is_Delayed_Aspect (Aitem);
2184 Set_Aspect_Rep_Item (Aspect, Aitem);
2185 Set_Parent (Aitem, Aspect);
2186 end if;
2188 Set_Is_Delayed_Aspect (Aspect);
2190 -- In the case of Default_Value, link the aspect to base type
2191 -- as well, even though it appears on a first subtype. This is
2192 -- mandated by the semantics of the aspect. Do not establish
2193 -- the link when processing the base type itself as this leads
2194 -- to a rep item circularity. Verify that we are dealing with
2195 -- a scalar type to prevent cascaded errors.
2197 if A_Id = Aspect_Default_Value
2198 and then Is_Scalar_Type (E)
2199 and then Base_Type (E) /= E
2200 then
2201 Set_Has_Delayed_Aspects (Base_Type (E));
2202 Record_Rep_Item (Base_Type (E), Aspect);
2203 end if;
2205 Set_Has_Delayed_Aspects (E);
2206 Record_Rep_Item (E, Aspect);
2208 -- When delay is not required and the context is not a compilation
2209 -- unit, we simply insert the pragma/attribute definition clause
2210 -- in sequence.
2212 else
2213 Insert_After (Ins_Node, Aitem);
2214 Ins_Node := Aitem;
2215 end if;
2216 end Analyze_One_Aspect;
2218 <<Continue>>
2219 Next (Aspect);
2220 end loop Aspect_Loop;
2222 if Has_Delayed_Aspects (E) then
2223 Ensure_Freeze_Node (E);
2224 end if;
2225 end Analyze_Aspect_Specifications;
2227 -----------------------
2228 -- Analyze_At_Clause --
2229 -----------------------
2231 -- An at clause is replaced by the corresponding Address attribute
2232 -- definition clause that is the preferred approach in Ada 95.
2234 procedure Analyze_At_Clause (N : Node_Id) is
2235 CS : constant Boolean := Comes_From_Source (N);
2237 begin
2238 -- This is an obsolescent feature
2240 Check_Restriction (No_Obsolescent_Features, N);
2242 if Warn_On_Obsolescent_Feature then
2243 Error_Msg_N
2244 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
2245 Error_Msg_N
2246 ("\?j?use address attribute definition clause instead", N);
2247 end if;
2249 -- Rewrite as address clause
2251 Rewrite (N,
2252 Make_Attribute_Definition_Clause (Sloc (N),
2253 Name => Identifier (N),
2254 Chars => Name_Address,
2255 Expression => Expression (N)));
2257 -- We preserve Comes_From_Source, since logically the clause still comes
2258 -- from the source program even though it is changed in form.
2260 Set_Comes_From_Source (N, CS);
2262 -- Analyze rewritten clause
2264 Analyze_Attribute_Definition_Clause (N);
2265 end Analyze_At_Clause;
2267 -----------------------------------------
2268 -- Analyze_Attribute_Definition_Clause --
2269 -----------------------------------------
2271 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
2272 Loc : constant Source_Ptr := Sloc (N);
2273 Nam : constant Node_Id := Name (N);
2274 Attr : constant Name_Id := Chars (N);
2275 Expr : constant Node_Id := Expression (N);
2276 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
2278 Ent : Entity_Id;
2279 -- The entity of Nam after it is analyzed. In the case of an incomplete
2280 -- type, this is the underlying type.
2282 U_Ent : Entity_Id;
2283 -- The underlying entity to which the attribute applies. Generally this
2284 -- is the Underlying_Type of Ent, except in the case where the clause
2285 -- applies to full view of incomplete type or private type in which case
2286 -- U_Ent is just a copy of Ent.
2288 FOnly : Boolean := False;
2289 -- Reset to True for subtype specific attribute (Alignment, Size)
2290 -- and for stream attributes, i.e. those cases where in the call
2291 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
2292 -- rules are checked. Note that the case of stream attributes is not
2293 -- clear from the RM, but see AI95-00137. Also, the RM seems to
2294 -- disallow Storage_Size for derived task types, but that is also
2295 -- clearly unintentional.
2297 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
2298 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
2299 -- definition clauses.
2301 function Duplicate_Clause return Boolean;
2302 -- This routine checks if the aspect for U_Ent being given by attribute
2303 -- definition clause N is for an aspect that has already been specified,
2304 -- and if so gives an error message. If there is a duplicate, True is
2305 -- returned, otherwise if there is no error, False is returned.
2307 procedure Check_Indexing_Functions;
2308 -- Check that the function in Constant_Indexing or Variable_Indexing
2309 -- attribute has the proper type structure. If the name is overloaded,
2310 -- check that some interpretation is legal.
2312 procedure Check_Iterator_Functions;
2313 -- Check that there is a single function in Default_Iterator attribute
2314 -- has the proper type structure.
2316 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
2317 -- Common legality check for the previous two
2319 -----------------------------------
2320 -- Analyze_Stream_TSS_Definition --
2321 -----------------------------------
2323 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
2324 Subp : Entity_Id := Empty;
2325 I : Interp_Index;
2326 It : Interp;
2327 Pnam : Entity_Id;
2329 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
2330 -- True for Read attribute, false for other attributes
2332 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
2333 -- Return true if the entity is a subprogram with an appropriate
2334 -- profile for the attribute being defined.
2336 ----------------------
2337 -- Has_Good_Profile --
2338 ----------------------
2340 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
2341 F : Entity_Id;
2342 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
2343 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
2344 (False => E_Procedure, True => E_Function);
2345 Typ : Entity_Id;
2347 begin
2348 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
2349 return False;
2350 end if;
2352 F := First_Formal (Subp);
2354 if No (F)
2355 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
2356 or else Designated_Type (Etype (F)) /=
2357 Class_Wide_Type (RTE (RE_Root_Stream_Type))
2358 then
2359 return False;
2360 end if;
2362 if not Is_Function then
2363 Next_Formal (F);
2365 declare
2366 Expected_Mode : constant array (Boolean) of Entity_Kind :=
2367 (False => E_In_Parameter,
2368 True => E_Out_Parameter);
2369 begin
2370 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
2371 return False;
2372 end if;
2373 end;
2375 Typ := Etype (F);
2377 else
2378 Typ := Etype (Subp);
2379 end if;
2381 return Base_Type (Typ) = Base_Type (Ent)
2382 and then No (Next_Formal (F));
2383 end Has_Good_Profile;
2385 -- Start of processing for Analyze_Stream_TSS_Definition
2387 begin
2388 FOnly := True;
2390 if not Is_Type (U_Ent) then
2391 Error_Msg_N ("local name must be a subtype", Nam);
2392 return;
2393 end if;
2395 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
2397 -- If Pnam is present, it can be either inherited from an ancestor
2398 -- type (in which case it is legal to redefine it for this type), or
2399 -- be a previous definition of the attribute for the same type (in
2400 -- which case it is illegal).
2402 -- In the first case, it will have been analyzed already, and we
2403 -- can check that its profile does not match the expected profile
2404 -- for a stream attribute of U_Ent. In the second case, either Pnam
2405 -- has been analyzed (and has the expected profile), or it has not
2406 -- been analyzed yet (case of a type that has not been frozen yet
2407 -- and for which the stream attribute has been set using Set_TSS).
2409 if Present (Pnam)
2410 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
2411 then
2412 Error_Msg_Sloc := Sloc (Pnam);
2413 Error_Msg_Name_1 := Attr;
2414 Error_Msg_N ("% attribute already defined #", Nam);
2415 return;
2416 end if;
2418 Analyze (Expr);
2420 if Is_Entity_Name (Expr) then
2421 if not Is_Overloaded (Expr) then
2422 if Has_Good_Profile (Entity (Expr)) then
2423 Subp := Entity (Expr);
2424 end if;
2426 else
2427 Get_First_Interp (Expr, I, It);
2428 while Present (It.Nam) loop
2429 if Has_Good_Profile (It.Nam) then
2430 Subp := It.Nam;
2431 exit;
2432 end if;
2434 Get_Next_Interp (I, It);
2435 end loop;
2436 end if;
2437 end if;
2439 if Present (Subp) then
2440 if Is_Abstract_Subprogram (Subp) then
2441 Error_Msg_N ("stream subprogram must not be abstract", Expr);
2442 return;
2443 end if;
2445 Set_Entity (Expr, Subp);
2446 Set_Etype (Expr, Etype (Subp));
2448 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
2450 else
2451 Error_Msg_Name_1 := Attr;
2452 Error_Msg_N ("incorrect expression for% attribute", Expr);
2453 end if;
2454 end Analyze_Stream_TSS_Definition;
2456 ------------------------------
2457 -- Check_Indexing_Functions --
2458 ------------------------------
2460 procedure Check_Indexing_Functions is
2461 Indexing_Found : Boolean;
2463 procedure Check_One_Function (Subp : Entity_Id);
2464 -- Check one possible interpretation. Sets Indexing_Found True if an
2465 -- indexing function is found.
2467 ------------------------
2468 -- Check_One_Function --
2469 ------------------------
2471 procedure Check_One_Function (Subp : Entity_Id) is
2472 Default_Element : constant Node_Id :=
2473 Find_Value_Of_Aspect
2474 (Etype (First_Formal (Subp)),
2475 Aspect_Iterator_Element);
2477 begin
2478 if not Check_Primitive_Function (Subp)
2479 and then not Is_Overloaded (Expr)
2480 then
2481 Error_Msg_NE
2482 ("aspect Indexing requires a function that applies to type&",
2483 Subp, Ent);
2484 end if;
2486 -- An indexing function must return either the default element of
2487 -- the container, or a reference type. For variable indexing it
2488 -- must be the latter.
2490 if Present (Default_Element) then
2491 Analyze (Default_Element);
2493 if Is_Entity_Name (Default_Element)
2494 and then Covers (Entity (Default_Element), Etype (Subp))
2495 then
2496 Indexing_Found := True;
2497 return;
2498 end if;
2499 end if;
2501 -- For variable_indexing the return type must be a reference type
2503 if Attr = Name_Variable_Indexing
2504 and then not Has_Implicit_Dereference (Etype (Subp))
2505 then
2506 Error_Msg_N
2507 ("function for indexing must return a reference type", Subp);
2509 else
2510 Indexing_Found := True;
2511 end if;
2512 end Check_One_Function;
2514 -- Start of processing for Check_Indexing_Functions
2516 begin
2517 if In_Instance then
2518 return;
2519 end if;
2521 Analyze (Expr);
2523 if not Is_Overloaded (Expr) then
2524 Check_One_Function (Entity (Expr));
2526 else
2527 declare
2528 I : Interp_Index;
2529 It : Interp;
2531 begin
2532 Indexing_Found := False;
2533 Get_First_Interp (Expr, I, It);
2534 while Present (It.Nam) loop
2536 -- Note that analysis will have added the interpretation
2537 -- that corresponds to the dereference. We only check the
2538 -- subprogram itself.
2540 if Is_Overloadable (It.Nam) then
2541 Check_One_Function (It.Nam);
2542 end if;
2544 Get_Next_Interp (I, It);
2545 end loop;
2547 if not Indexing_Found then
2548 Error_Msg_NE
2549 ("aspect Indexing requires a function that "
2550 & "applies to type&", Expr, Ent);
2551 end if;
2552 end;
2553 end if;
2554 end Check_Indexing_Functions;
2556 ------------------------------
2557 -- Check_Iterator_Functions --
2558 ------------------------------
2560 procedure Check_Iterator_Functions is
2561 Default : Entity_Id;
2563 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
2564 -- Check one possible interpretation for validity
2566 ----------------------------
2567 -- Valid_Default_Iterator --
2568 ----------------------------
2570 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2571 Formal : Entity_Id;
2573 begin
2574 if not Check_Primitive_Function (Subp) then
2575 return False;
2576 else
2577 Formal := First_Formal (Subp);
2578 end if;
2580 -- False if any subsequent formal has no default expression
2582 Formal := Next_Formal (Formal);
2583 while Present (Formal) loop
2584 if No (Expression (Parent (Formal))) then
2585 return False;
2586 end if;
2588 Next_Formal (Formal);
2589 end loop;
2591 -- True if all subsequent formals have default expressions
2593 return True;
2594 end Valid_Default_Iterator;
2596 -- Start of processing for Check_Iterator_Functions
2598 begin
2599 Analyze (Expr);
2601 if not Is_Entity_Name (Expr) then
2602 Error_Msg_N ("aspect Iterator must be a function name", Expr);
2603 end if;
2605 if not Is_Overloaded (Expr) then
2606 if not Check_Primitive_Function (Entity (Expr)) then
2607 Error_Msg_NE
2608 ("aspect Indexing requires a function that applies to type&",
2609 Entity (Expr), Ent);
2610 end if;
2612 if not Valid_Default_Iterator (Entity (Expr)) then
2613 Error_Msg_N ("improper function for default iterator", Expr);
2614 end if;
2616 else
2617 Default := Empty;
2618 declare
2619 I : Interp_Index;
2620 It : Interp;
2622 begin
2623 Get_First_Interp (Expr, I, It);
2624 while Present (It.Nam) loop
2625 if not Check_Primitive_Function (It.Nam)
2626 or else not Valid_Default_Iterator (It.Nam)
2627 then
2628 Remove_Interp (I);
2630 elsif Present (Default) then
2631 Error_Msg_N ("default iterator must be unique", Expr);
2633 else
2634 Default := It.Nam;
2635 end if;
2637 Get_Next_Interp (I, It);
2638 end loop;
2639 end;
2641 if Present (Default) then
2642 Set_Entity (Expr, Default);
2643 Set_Is_Overloaded (Expr, False);
2644 end if;
2645 end if;
2646 end Check_Iterator_Functions;
2648 -------------------------------
2649 -- Check_Primitive_Function --
2650 -------------------------------
2652 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2653 Ctrl : Entity_Id;
2655 begin
2656 if Ekind (Subp) /= E_Function then
2657 return False;
2658 end if;
2660 if No (First_Formal (Subp)) then
2661 return False;
2662 else
2663 Ctrl := Etype (First_Formal (Subp));
2664 end if;
2666 if Ctrl = Ent
2667 or else Ctrl = Class_Wide_Type (Ent)
2668 or else
2669 (Ekind (Ctrl) = E_Anonymous_Access_Type
2670 and then
2671 (Designated_Type (Ctrl) = Ent
2672 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2673 then
2674 null;
2676 else
2677 return False;
2678 end if;
2680 return True;
2681 end Check_Primitive_Function;
2683 ----------------------
2684 -- Duplicate_Clause --
2685 ----------------------
2687 function Duplicate_Clause return Boolean is
2688 A : Node_Id;
2690 begin
2691 -- Nothing to do if this attribute definition clause comes from
2692 -- an aspect specification, since we could not be duplicating an
2693 -- explicit clause, and we dealt with the case of duplicated aspects
2694 -- in Analyze_Aspect_Specifications.
2696 if From_Aspect_Specification (N) then
2697 return False;
2698 end if;
2700 -- Otherwise current clause may duplicate previous clause, or a
2701 -- previously given pragma or aspect specification for the same
2702 -- aspect.
2704 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
2706 if Present (A) then
2707 Error_Msg_Name_1 := Chars (N);
2708 Error_Msg_Sloc := Sloc (A);
2710 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2711 return True;
2712 end if;
2714 return False;
2715 end Duplicate_Clause;
2717 -- Start of processing for Analyze_Attribute_Definition_Clause
2719 begin
2720 -- The following code is a defense against recursion. Not clear that
2721 -- this can happen legitimately, but perhaps some error situations
2722 -- can cause it, and we did see this recursion during testing.
2724 if Analyzed (N) then
2725 return;
2726 else
2727 Set_Analyzed (N, True);
2728 end if;
2730 -- Ignore some selected attributes in CodePeer mode since they are not
2731 -- relevant in this context.
2733 if CodePeer_Mode then
2734 case Id is
2736 -- Ignore Component_Size in CodePeer mode, to avoid changing the
2737 -- internal representation of types by implicitly packing them.
2739 when Attribute_Component_Size =>
2740 Rewrite (N, Make_Null_Statement (Sloc (N)));
2741 return;
2743 when others =>
2744 null;
2745 end case;
2746 end if;
2748 -- Process Ignore_Rep_Clauses option
2750 if Ignore_Rep_Clauses then
2751 case Id is
2753 -- The following should be ignored. They do not affect legality
2754 -- and may be target dependent. The basic idea of -gnatI is to
2755 -- ignore any rep clauses that may be target dependent but do not
2756 -- affect legality (except possibly to be rejected because they
2757 -- are incompatible with the compilation target).
2759 when Attribute_Alignment |
2760 Attribute_Bit_Order |
2761 Attribute_Component_Size |
2762 Attribute_Machine_Radix |
2763 Attribute_Object_Size |
2764 Attribute_Size |
2765 Attribute_Stream_Size |
2766 Attribute_Value_Size =>
2767 Rewrite (N, Make_Null_Statement (Sloc (N)));
2768 return;
2770 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2772 when Attribute_Small =>
2773 if Ignore_Rep_Clauses then
2774 Rewrite (N, Make_Null_Statement (Sloc (N)));
2775 return;
2776 end if;
2778 -- The following should not be ignored, because in the first place
2779 -- they are reasonably portable, and should not cause problems in
2780 -- compiling code from another target, and also they do affect
2781 -- legality, e.g. failing to provide a stream attribute for a
2782 -- type may make a program illegal.
2784 when Attribute_External_Tag |
2785 Attribute_Input |
2786 Attribute_Output |
2787 Attribute_Read |
2788 Attribute_Simple_Storage_Pool |
2789 Attribute_Storage_Pool |
2790 Attribute_Storage_Size |
2791 Attribute_Write =>
2792 null;
2794 -- Other cases are errors ("attribute& cannot be set with
2795 -- definition clause"), which will be caught below.
2797 when others =>
2798 null;
2799 end case;
2800 end if;
2802 Analyze (Nam);
2803 Ent := Entity (Nam);
2805 if Rep_Item_Too_Early (Ent, N) then
2806 return;
2807 end if;
2809 -- Rep clause applies to full view of incomplete type or private type if
2810 -- we have one (if not, this is a premature use of the type). However,
2811 -- certain semantic checks need to be done on the specified entity (i.e.
2812 -- the private view), so we save it in Ent.
2814 if Is_Private_Type (Ent)
2815 and then Is_Derived_Type (Ent)
2816 and then not Is_Tagged_Type (Ent)
2817 and then No (Full_View (Ent))
2818 then
2819 -- If this is a private type whose completion is a derivation from
2820 -- another private type, there is no full view, and the attribute
2821 -- belongs to the type itself, not its underlying parent.
2823 U_Ent := Ent;
2825 elsif Ekind (Ent) = E_Incomplete_Type then
2827 -- The attribute applies to the full view, set the entity of the
2828 -- attribute definition accordingly.
2830 Ent := Underlying_Type (Ent);
2831 U_Ent := Ent;
2832 Set_Entity (Nam, Ent);
2834 else
2835 U_Ent := Underlying_Type (Ent);
2836 end if;
2838 -- Avoid cascaded error
2840 if Etype (Nam) = Any_Type then
2841 return;
2843 -- Must be declared in current scope or in case of an aspect
2844 -- specification, must be visible in current scope.
2846 elsif Scope (Ent) /= Current_Scope
2847 and then
2848 not (From_Aspect_Specification (N)
2849 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
2850 then
2851 Error_Msg_N ("entity must be declared in this scope", Nam);
2852 return;
2854 -- Must not be a source renaming (we do have some cases where the
2855 -- expander generates a renaming, and those cases are OK, in such
2856 -- cases any attribute applies to the renamed object as well).
2858 elsif Is_Object (Ent)
2859 and then Present (Renamed_Object (Ent))
2860 then
2861 -- Case of renamed object from source, this is an error
2863 if Comes_From_Source (Renamed_Object (Ent)) then
2864 Get_Name_String (Chars (N));
2865 Error_Msg_Strlen := Name_Len;
2866 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2867 Error_Msg_N
2868 ("~ clause not allowed for a renaming declaration "
2869 & "(RM 13.1(6))", Nam);
2870 return;
2872 -- For the case of a compiler generated renaming, the attribute
2873 -- definition clause applies to the renamed object created by the
2874 -- expander. The easiest general way to handle this is to create a
2875 -- copy of the attribute definition clause for this object.
2877 else
2878 Insert_Action (N,
2879 Make_Attribute_Definition_Clause (Loc,
2880 Name =>
2881 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2882 Chars => Chars (N),
2883 Expression => Duplicate_Subexpr (Expression (N))));
2884 end if;
2886 -- If no underlying entity, use entity itself, applies to some
2887 -- previously detected error cases ???
2889 elsif No (U_Ent) then
2890 U_Ent := Ent;
2892 -- Cannot specify for a subtype (exception Object/Value_Size)
2894 elsif Is_Type (U_Ent)
2895 and then not Is_First_Subtype (U_Ent)
2896 and then Id /= Attribute_Object_Size
2897 and then Id /= Attribute_Value_Size
2898 and then not From_At_Mod (N)
2899 then
2900 Error_Msg_N ("cannot specify attribute for subtype", Nam);
2901 return;
2902 end if;
2904 Set_Entity (N, U_Ent);
2905 Check_Restriction_No_Use_Of_Attribute (N);
2907 -- Switch on particular attribute
2909 case Id is
2911 -------------
2912 -- Address --
2913 -------------
2915 -- Address attribute definition clause
2917 when Attribute_Address => Address : begin
2919 -- A little error check, catch for X'Address use X'Address;
2921 if Nkind (Nam) = N_Identifier
2922 and then Nkind (Expr) = N_Attribute_Reference
2923 and then Attribute_Name (Expr) = Name_Address
2924 and then Nkind (Prefix (Expr)) = N_Identifier
2925 and then Chars (Nam) = Chars (Prefix (Expr))
2926 then
2927 Error_Msg_NE
2928 ("address for & is self-referencing", Prefix (Expr), Ent);
2929 return;
2930 end if;
2932 -- Not that special case, carry on with analysis of expression
2934 Analyze_And_Resolve (Expr, RTE (RE_Address));
2936 -- Even when ignoring rep clauses we need to indicate that the
2937 -- entity has an address clause and thus it is legal to declare
2938 -- it imported.
2940 if Ignore_Rep_Clauses then
2941 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2942 Record_Rep_Item (U_Ent, N);
2943 end if;
2945 return;
2946 end if;
2948 if Duplicate_Clause then
2949 null;
2951 -- Case of address clause for subprogram
2953 elsif Is_Subprogram (U_Ent) then
2954 if Has_Homonym (U_Ent) then
2955 Error_Msg_N
2956 ("address clause cannot be given " &
2957 "for overloaded subprogram",
2958 Nam);
2959 return;
2960 end if;
2962 -- For subprograms, all address clauses are permitted, and we
2963 -- mark the subprogram as having a deferred freeze so that Gigi
2964 -- will not elaborate it too soon.
2966 -- Above needs more comments, what is too soon about???
2968 Set_Has_Delayed_Freeze (U_Ent);
2970 -- Case of address clause for entry
2972 elsif Ekind (U_Ent) = E_Entry then
2973 if Nkind (Parent (N)) = N_Task_Body then
2974 Error_Msg_N
2975 ("entry address must be specified in task spec", Nam);
2976 return;
2977 end if;
2979 -- For entries, we require a constant address
2981 Check_Constant_Address_Clause (Expr, U_Ent);
2983 -- Special checks for task types
2985 if Is_Task_Type (Scope (U_Ent))
2986 and then Comes_From_Source (Scope (U_Ent))
2987 then
2988 Error_Msg_N
2989 ("??entry address declared for entry in task type", N);
2990 Error_Msg_N
2991 ("\??only one task can be declared of this type", N);
2992 end if;
2994 -- Entry address clauses are obsolescent
2996 Check_Restriction (No_Obsolescent_Features, N);
2998 if Warn_On_Obsolescent_Feature then
2999 Error_Msg_N
3000 ("?j?attaching interrupt to task entry is an " &
3001 "obsolescent feature (RM J.7.1)", N);
3002 Error_Msg_N
3003 ("\?j?use interrupt procedure instead", N);
3004 end if;
3006 -- Case of an address clause for a controlled object which we
3007 -- consider to be erroneous.
3009 elsif Is_Controlled (Etype (U_Ent))
3010 or else Has_Controlled_Component (Etype (U_Ent))
3011 then
3012 Error_Msg_NE
3013 ("??controlled object& must not be overlaid", Nam, U_Ent);
3014 Error_Msg_N
3015 ("\??Program_Error will be raised at run time", Nam);
3016 Insert_Action (Declaration_Node (U_Ent),
3017 Make_Raise_Program_Error (Loc,
3018 Reason => PE_Overlaid_Controlled_Object));
3019 return;
3021 -- Case of address clause for a (non-controlled) object
3023 elsif
3024 Ekind (U_Ent) = E_Variable
3025 or else
3026 Ekind (U_Ent) = E_Constant
3027 then
3028 declare
3029 Expr : constant Node_Id := Expression (N);
3030 O_Ent : Entity_Id;
3031 Off : Boolean;
3033 begin
3034 -- Exported variables cannot have an address clause, because
3035 -- this cancels the effect of the pragma Export.
3037 if Is_Exported (U_Ent) then
3038 Error_Msg_N
3039 ("cannot export object with address clause", Nam);
3040 return;
3041 end if;
3043 Find_Overlaid_Entity (N, O_Ent, Off);
3045 -- Overlaying controlled objects is erroneous
3047 if Present (O_Ent)
3048 and then (Has_Controlled_Component (Etype (O_Ent))
3049 or else Is_Controlled (Etype (O_Ent)))
3050 then
3051 Error_Msg_N
3052 ("??cannot overlay with controlled object", Expr);
3053 Error_Msg_N
3054 ("\??Program_Error will be raised at run time", Expr);
3055 Insert_Action (Declaration_Node (U_Ent),
3056 Make_Raise_Program_Error (Loc,
3057 Reason => PE_Overlaid_Controlled_Object));
3058 return;
3060 elsif Present (O_Ent)
3061 and then Ekind (U_Ent) = E_Constant
3062 and then not Is_Constant_Object (O_Ent)
3063 then
3064 Error_Msg_N ("??constant overlays a variable", Expr);
3066 -- Imported variables can have an address clause, but then
3067 -- the import is pretty meaningless except to suppress
3068 -- initializations, so we do not need such variables to
3069 -- be statically allocated (and in fact it causes trouble
3070 -- if the address clause is a local value).
3072 elsif Is_Imported (U_Ent) then
3073 Set_Is_Statically_Allocated (U_Ent, False);
3074 end if;
3076 -- We mark a possible modification of a variable with an
3077 -- address clause, since it is likely aliasing is occurring.
3079 Note_Possible_Modification (Nam, Sure => False);
3081 -- Here we are checking for explicit overlap of one variable
3082 -- by another, and if we find this then mark the overlapped
3083 -- variable as also being volatile to prevent unwanted
3084 -- optimizations. This is a significant pessimization so
3085 -- avoid it when there is an offset, i.e. when the object
3086 -- is composite; they cannot be optimized easily anyway.
3088 if Present (O_Ent)
3089 and then Is_Object (O_Ent)
3090 and then not Off
3092 -- The following test is an expedient solution to what
3093 -- is really a problem in CodePeer. Suppressing the
3094 -- Set_Treat_As_Volatile call here prevents later
3095 -- generation (in some cases) of trees that CodePeer
3096 -- should, but currently does not, handle correctly.
3097 -- This test should probably be removed when CodePeer
3098 -- is improved, just because we want the tree CodePeer
3099 -- analyzes to match the tree for which we generate code
3100 -- as closely as is practical. ???
3102 and then not CodePeer_Mode
3103 then
3104 -- ??? O_Ent might not be in current unit
3106 Set_Treat_As_Volatile (O_Ent);
3107 end if;
3109 -- Legality checks on the address clause for initialized
3110 -- objects is deferred until the freeze point, because
3111 -- a subsequent pragma might indicate that the object
3112 -- is imported and thus not initialized. Also, the address
3113 -- clause might involve entities that have yet to be
3114 -- elaborated.
3116 Set_Has_Delayed_Freeze (U_Ent);
3118 -- If an initialization call has been generated for this
3119 -- object, it needs to be deferred to after the freeze node
3120 -- we have just now added, otherwise GIGI will see a
3121 -- reference to the variable (as actual to the IP call)
3122 -- before its definition.
3124 declare
3125 Init_Call : constant Node_Id :=
3126 Remove_Init_Call (U_Ent, N);
3128 begin
3129 if Present (Init_Call) then
3131 -- If the init call is an expression with actions with
3132 -- null expression, just extract the actions.
3134 if Nkind (Init_Call) = N_Expression_With_Actions
3135 and then
3136 Nkind (Expression (Init_Call)) = N_Null_Statement
3137 then
3138 Append_Freeze_Actions (U_Ent, Actions (Init_Call));
3140 -- General case: move Init_Call to freeze actions
3142 else
3143 Append_Freeze_Action (U_Ent, Init_Call);
3144 end if;
3145 end if;
3146 end;
3148 if Is_Exported (U_Ent) then
3149 Error_Msg_N
3150 ("& cannot be exported if an address clause is given",
3151 Nam);
3152 Error_Msg_N
3153 ("\define and export a variable "
3154 & "that holds its address instead", Nam);
3155 end if;
3157 -- Entity has delayed freeze, so we will generate an
3158 -- alignment check at the freeze point unless suppressed.
3160 if not Range_Checks_Suppressed (U_Ent)
3161 and then not Alignment_Checks_Suppressed (U_Ent)
3162 then
3163 Set_Check_Address_Alignment (N);
3164 end if;
3166 -- Kill the size check code, since we are not allocating
3167 -- the variable, it is somewhere else.
3169 Kill_Size_Check_Code (U_Ent);
3171 -- If the address clause is of the form:
3173 -- for Y'Address use X'Address
3175 -- or
3177 -- Const : constant Address := X'Address;
3178 -- ...
3179 -- for Y'Address use Const;
3181 -- then we make an entry in the table for checking the size
3182 -- and alignment of the overlaying variable. We defer this
3183 -- check till after code generation to take full advantage
3184 -- of the annotation done by the back end. This entry is
3185 -- only made if the address clause comes from source.
3187 -- If the entity has a generic type, the check will be
3188 -- performed in the instance if the actual type justifies
3189 -- it, and we do not insert the clause in the table to
3190 -- prevent spurious warnings.
3192 if Address_Clause_Overlay_Warnings
3193 and then Comes_From_Source (N)
3194 and then Present (O_Ent)
3195 and then Is_Object (O_Ent)
3196 then
3197 if not Is_Generic_Type (Etype (U_Ent)) then
3198 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
3199 end if;
3201 -- If variable overlays a constant view, and we are
3202 -- warning on overlays, then mark the variable as
3203 -- overlaying a constant (we will give warnings later
3204 -- if this variable is assigned).
3206 if Is_Constant_Object (O_Ent)
3207 and then Ekind (U_Ent) = E_Variable
3208 then
3209 Set_Overlays_Constant (U_Ent);
3210 end if;
3211 end if;
3212 end;
3214 -- Not a valid entity for an address clause
3216 else
3217 Error_Msg_N ("address cannot be given for &", Nam);
3218 end if;
3219 end Address;
3221 ---------------
3222 -- Alignment --
3223 ---------------
3225 -- Alignment attribute definition clause
3227 when Attribute_Alignment => Alignment : declare
3228 Align : constant Uint := Get_Alignment_Value (Expr);
3229 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
3231 begin
3232 FOnly := True;
3234 if not Is_Type (U_Ent)
3235 and then Ekind (U_Ent) /= E_Variable
3236 and then Ekind (U_Ent) /= E_Constant
3237 then
3238 Error_Msg_N ("alignment cannot be given for &", Nam);
3240 elsif Duplicate_Clause then
3241 null;
3243 elsif Align /= No_Uint then
3244 Set_Has_Alignment_Clause (U_Ent);
3246 -- Tagged type case, check for attempt to set alignment to a
3247 -- value greater than Max_Align, and reset if so.
3249 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
3250 Error_Msg_N
3251 ("alignment for & set to Maximum_Aligment??", Nam);
3252 Set_Alignment (U_Ent, Max_Align);
3254 -- All other cases
3256 else
3257 Set_Alignment (U_Ent, Align);
3258 end if;
3260 -- For an array type, U_Ent is the first subtype. In that case,
3261 -- also set the alignment of the anonymous base type so that
3262 -- other subtypes (such as the itypes for aggregates of the
3263 -- type) also receive the expected alignment.
3265 if Is_Array_Type (U_Ent) then
3266 Set_Alignment (Base_Type (U_Ent), Align);
3267 end if;
3268 end if;
3269 end Alignment;
3271 ---------------
3272 -- Bit_Order --
3273 ---------------
3275 -- Bit_Order attribute definition clause
3277 when Attribute_Bit_Order => Bit_Order : declare
3278 begin
3279 if not Is_Record_Type (U_Ent) then
3280 Error_Msg_N
3281 ("Bit_Order can only be defined for record type", Nam);
3283 elsif Duplicate_Clause then
3284 null;
3286 else
3287 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3289 if Etype (Expr) = Any_Type then
3290 return;
3292 elsif not Is_Static_Expression (Expr) then
3293 Flag_Non_Static_Expr
3294 ("Bit_Order requires static expression!", Expr);
3296 else
3297 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3298 Set_Reverse_Bit_Order (U_Ent, True);
3299 end if;
3300 end if;
3301 end if;
3302 end Bit_Order;
3304 --------------------
3305 -- Component_Size --
3306 --------------------
3308 -- Component_Size attribute definition clause
3310 when Attribute_Component_Size => Component_Size_Case : declare
3311 Csize : constant Uint := Static_Integer (Expr);
3312 Ctyp : Entity_Id;
3313 Btype : Entity_Id;
3314 Biased : Boolean;
3315 New_Ctyp : Entity_Id;
3316 Decl : Node_Id;
3318 begin
3319 if not Is_Array_Type (U_Ent) then
3320 Error_Msg_N ("component size requires array type", Nam);
3321 return;
3322 end if;
3324 Btype := Base_Type (U_Ent);
3325 Ctyp := Component_Type (Btype);
3327 if Duplicate_Clause then
3328 null;
3330 elsif Rep_Item_Too_Early (Btype, N) then
3331 null;
3333 elsif Csize /= No_Uint then
3334 Check_Size (Expr, Ctyp, Csize, Biased);
3336 -- For the biased case, build a declaration for a subtype that
3337 -- will be used to represent the biased subtype that reflects
3338 -- the biased representation of components. We need the subtype
3339 -- to get proper conversions on referencing elements of the
3340 -- array. Note: component size clauses are ignored in VM mode.
3342 if VM_Target = No_VM then
3343 if Biased then
3344 New_Ctyp :=
3345 Make_Defining_Identifier (Loc,
3346 Chars =>
3347 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3349 Decl :=
3350 Make_Subtype_Declaration (Loc,
3351 Defining_Identifier => New_Ctyp,
3352 Subtype_Indication =>
3353 New_Occurrence_Of (Component_Type (Btype), Loc));
3355 Set_Parent (Decl, N);
3356 Analyze (Decl, Suppress => All_Checks);
3358 Set_Has_Delayed_Freeze (New_Ctyp, False);
3359 Set_Esize (New_Ctyp, Csize);
3360 Set_RM_Size (New_Ctyp, Csize);
3361 Init_Alignment (New_Ctyp);
3362 Set_Is_Itype (New_Ctyp, True);
3363 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
3365 Set_Component_Type (Btype, New_Ctyp);
3366 Set_Biased (New_Ctyp, N, "component size clause");
3367 end if;
3369 Set_Component_Size (Btype, Csize);
3371 -- For VM case, we ignore component size clauses
3373 else
3374 -- Give a warning unless we are in GNAT mode, in which case
3375 -- the warning is suppressed since it is not useful.
3377 if not GNAT_Mode then
3378 Error_Msg_N
3379 ("component size ignored in this configuration??", N);
3380 end if;
3381 end if;
3383 -- Deal with warning on overridden size
3385 if Warn_On_Overridden_Size
3386 and then Has_Size_Clause (Ctyp)
3387 and then RM_Size (Ctyp) /= Csize
3388 then
3389 Error_Msg_NE
3390 ("component size overrides size clause for&?S?", N, Ctyp);
3391 end if;
3393 Set_Has_Component_Size_Clause (Btype, True);
3394 Set_Has_Non_Standard_Rep (Btype, True);
3395 end if;
3396 end Component_Size_Case;
3398 -----------------------
3399 -- Constant_Indexing --
3400 -----------------------
3402 when Attribute_Constant_Indexing =>
3403 Check_Indexing_Functions;
3405 ---------
3406 -- CPU --
3407 ---------
3409 when Attribute_CPU => CPU :
3410 begin
3411 -- CPU attribute definition clause not allowed except from aspect
3412 -- specification.
3414 if From_Aspect_Specification (N) then
3415 if not Is_Task_Type (U_Ent) then
3416 Error_Msg_N ("CPU can only be defined for task", Nam);
3418 elsif Duplicate_Clause then
3419 null;
3421 else
3422 -- The expression must be analyzed in the special manner
3423 -- described in "Handling of Default and Per-Object
3424 -- Expressions" in sem.ads.
3426 -- The visibility to the discriminants must be restored
3428 Push_Scope_And_Install_Discriminants (U_Ent);
3429 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
3430 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3432 if not Is_Static_Expression (Expr) then
3433 Check_Restriction (Static_Priorities, Expr);
3434 end if;
3435 end if;
3437 else
3438 Error_Msg_N
3439 ("attribute& cannot be set with definition clause", N);
3440 end if;
3441 end CPU;
3443 ----------------------
3444 -- Default_Iterator --
3445 ----------------------
3447 when Attribute_Default_Iterator => Default_Iterator : declare
3448 Func : Entity_Id;
3450 begin
3451 if not Is_Tagged_Type (U_Ent) then
3452 Error_Msg_N
3453 ("aspect Default_Iterator applies to tagged type", Nam);
3454 end if;
3456 Check_Iterator_Functions;
3458 Analyze (Expr);
3460 if not Is_Entity_Name (Expr)
3461 or else Ekind (Entity (Expr)) /= E_Function
3462 then
3463 Error_Msg_N ("aspect Iterator must be a function", Expr);
3464 else
3465 Func := Entity (Expr);
3466 end if;
3468 if No (First_Formal (Func))
3469 or else Etype (First_Formal (Func)) /= U_Ent
3470 then
3471 Error_Msg_NE
3472 ("Default Iterator must be a primitive of&", Func, U_Ent);
3473 end if;
3474 end Default_Iterator;
3476 ------------------------
3477 -- Dispatching_Domain --
3478 ------------------------
3480 when Attribute_Dispatching_Domain => Dispatching_Domain :
3481 begin
3482 -- Dispatching_Domain attribute definition clause not allowed
3483 -- except from aspect specification.
3485 if From_Aspect_Specification (N) then
3486 if not Is_Task_Type (U_Ent) then
3487 Error_Msg_N ("Dispatching_Domain can only be defined" &
3488 "for task",
3489 Nam);
3491 elsif Duplicate_Clause then
3492 null;
3494 else
3495 -- The expression must be analyzed in the special manner
3496 -- described in "Handling of Default and Per-Object
3497 -- Expressions" in sem.ads.
3499 -- The visibility to the discriminants must be restored
3501 Push_Scope_And_Install_Discriminants (U_Ent);
3503 Preanalyze_Spec_Expression
3504 (Expr, RTE (RE_Dispatching_Domain));
3506 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3507 end if;
3509 else
3510 Error_Msg_N
3511 ("attribute& cannot be set with definition clause", N);
3512 end if;
3513 end Dispatching_Domain;
3515 ------------------
3516 -- External_Tag --
3517 ------------------
3519 when Attribute_External_Tag => External_Tag :
3520 begin
3521 if not Is_Tagged_Type (U_Ent) then
3522 Error_Msg_N ("should be a tagged type", Nam);
3523 end if;
3525 if Duplicate_Clause then
3526 null;
3528 else
3529 Analyze_And_Resolve (Expr, Standard_String);
3531 if not Is_Static_Expression (Expr) then
3532 Flag_Non_Static_Expr
3533 ("static string required for tag name!", Nam);
3534 end if;
3536 if VM_Target = No_VM then
3537 Set_Has_External_Tag_Rep_Clause (U_Ent);
3538 else
3539 Error_Msg_Name_1 := Attr;
3540 Error_Msg_N
3541 ("% attribute unsupported in this configuration", Nam);
3542 end if;
3544 if not Is_Library_Level_Entity (U_Ent) then
3545 Error_Msg_NE
3546 ("??non-unique external tag supplied for &", N, U_Ent);
3547 Error_Msg_N
3548 ("\??same external tag applies to all "
3549 & "subprogram calls", N);
3550 Error_Msg_N
3551 ("\??corresponding internal tag cannot be obtained", N);
3552 end if;
3553 end if;
3554 end External_Tag;
3556 --------------------------
3557 -- Implicit_Dereference --
3558 --------------------------
3560 when Attribute_Implicit_Dereference =>
3562 -- Legality checks already performed at the point of the type
3563 -- declaration, aspect is not delayed.
3565 null;
3567 -----------
3568 -- Input --
3569 -----------
3571 when Attribute_Input =>
3572 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
3573 Set_Has_Specified_Stream_Input (Ent);
3575 ------------------------
3576 -- Interrupt_Priority --
3577 ------------------------
3579 when Attribute_Interrupt_Priority => Interrupt_Priority :
3580 begin
3581 -- Interrupt_Priority attribute definition clause not allowed
3582 -- except from aspect specification.
3584 if From_Aspect_Specification (N) then
3585 if not (Is_Protected_Type (U_Ent)
3586 or else Is_Task_Type (U_Ent))
3587 then
3588 Error_Msg_N
3589 ("Interrupt_Priority can only be defined for task" &
3590 "and protected object",
3591 Nam);
3593 elsif Duplicate_Clause then
3594 null;
3596 else
3597 -- The expression must be analyzed in the special manner
3598 -- described in "Handling of Default and Per-Object
3599 -- Expressions" in sem.ads.
3601 -- The visibility to the discriminants must be restored
3603 Push_Scope_And_Install_Discriminants (U_Ent);
3605 Preanalyze_Spec_Expression
3606 (Expr, RTE (RE_Interrupt_Priority));
3608 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3609 end if;
3611 else
3612 Error_Msg_N
3613 ("attribute& cannot be set with definition clause", N);
3614 end if;
3615 end Interrupt_Priority;
3617 ----------------------
3618 -- Iterator_Element --
3619 ----------------------
3621 when Attribute_Iterator_Element =>
3622 Analyze (Expr);
3624 if not Is_Entity_Name (Expr)
3625 or else not Is_Type (Entity (Expr))
3626 then
3627 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
3628 end if;
3630 -------------------
3631 -- Machine_Radix --
3632 -------------------
3634 -- Machine radix attribute definition clause
3636 when Attribute_Machine_Radix => Machine_Radix : declare
3637 Radix : constant Uint := Static_Integer (Expr);
3639 begin
3640 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
3641 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
3643 elsif Duplicate_Clause then
3644 null;
3646 elsif Radix /= No_Uint then
3647 Set_Has_Machine_Radix_Clause (U_Ent);
3648 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
3650 if Radix = 2 then
3651 null;
3652 elsif Radix = 10 then
3653 Set_Machine_Radix_10 (U_Ent);
3654 else
3655 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
3656 end if;
3657 end if;
3658 end Machine_Radix;
3660 -----------------
3661 -- Object_Size --
3662 -----------------
3664 -- Object_Size attribute definition clause
3666 when Attribute_Object_Size => Object_Size : declare
3667 Size : constant Uint := Static_Integer (Expr);
3669 Biased : Boolean;
3670 pragma Warnings (Off, Biased);
3672 begin
3673 if not Is_Type (U_Ent) then
3674 Error_Msg_N ("Object_Size cannot be given for &", Nam);
3676 elsif Duplicate_Clause then
3677 null;
3679 else
3680 Check_Size (Expr, U_Ent, Size, Biased);
3682 if Size /= 8
3683 and then
3684 Size /= 16
3685 and then
3686 Size /= 32
3687 and then
3688 UI_Mod (Size, 64) /= 0
3689 then
3690 Error_Msg_N
3691 ("Object_Size must be 8, 16, 32, or multiple of 64",
3692 Expr);
3693 end if;
3695 Set_Esize (U_Ent, Size);
3696 Set_Has_Object_Size_Clause (U_Ent);
3697 Alignment_Check_For_Size_Change (U_Ent, Size);
3698 end if;
3699 end Object_Size;
3701 ------------
3702 -- Output --
3703 ------------
3705 when Attribute_Output =>
3706 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
3707 Set_Has_Specified_Stream_Output (Ent);
3709 --------------
3710 -- Priority --
3711 --------------
3713 when Attribute_Priority => Priority :
3714 begin
3715 -- Priority attribute definition clause not allowed except from
3716 -- aspect specification.
3718 if From_Aspect_Specification (N) then
3719 if not (Is_Protected_Type (U_Ent)
3720 or else Is_Task_Type (U_Ent)
3721 or else Ekind (U_Ent) = E_Procedure)
3722 then
3723 Error_Msg_N
3724 ("Priority can only be defined for task and protected " &
3725 "object",
3726 Nam);
3728 elsif Duplicate_Clause then
3729 null;
3731 else
3732 -- The expression must be analyzed in the special manner
3733 -- described in "Handling of Default and Per-Object
3734 -- Expressions" in sem.ads.
3736 -- The visibility to the discriminants must be restored
3738 Push_Scope_And_Install_Discriminants (U_Ent);
3739 Preanalyze_Spec_Expression (Expr, Standard_Integer);
3740 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3742 if not Is_Static_Expression (Expr) then
3743 Check_Restriction (Static_Priorities, Expr);
3744 end if;
3745 end if;
3747 else
3748 Error_Msg_N
3749 ("attribute& cannot be set with definition clause", N);
3750 end if;
3751 end Priority;
3753 ----------
3754 -- Read --
3755 ----------
3757 when Attribute_Read =>
3758 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
3759 Set_Has_Specified_Stream_Read (Ent);
3761 --------------------------
3762 -- Scalar_Storage_Order --
3763 --------------------------
3765 -- Scalar_Storage_Order attribute definition clause
3767 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
3768 begin
3769 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
3770 Error_Msg_N
3771 ("Scalar_Storage_Order can only be defined for "
3772 & "record or array type", Nam);
3774 elsif Duplicate_Clause then
3775 null;
3777 else
3778 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3780 if Etype (Expr) = Any_Type then
3781 return;
3783 elsif not Is_Static_Expression (Expr) then
3784 Flag_Non_Static_Expr
3785 ("Scalar_Storage_Order requires static expression!", Expr);
3787 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3789 -- Here for the case of a non-default (i.e. non-confirming)
3790 -- Scalar_Storage_Order attribute definition.
3792 if Support_Nondefault_SSO_On_Target then
3793 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
3794 else
3795 Error_Msg_N
3796 ("non-default Scalar_Storage_Order "
3797 & "not supported on target", Expr);
3798 end if;
3799 end if;
3800 end if;
3801 end Scalar_Storage_Order;
3803 ----------
3804 -- Size --
3805 ----------
3807 -- Size attribute definition clause
3809 when Attribute_Size => Size : declare
3810 Size : constant Uint := Static_Integer (Expr);
3811 Etyp : Entity_Id;
3812 Biased : Boolean;
3814 begin
3815 FOnly := True;
3817 if Duplicate_Clause then
3818 null;
3820 elsif not Is_Type (U_Ent)
3821 and then Ekind (U_Ent) /= E_Variable
3822 and then Ekind (U_Ent) /= E_Constant
3823 then
3824 Error_Msg_N ("size cannot be given for &", Nam);
3826 elsif Is_Array_Type (U_Ent)
3827 and then not Is_Constrained (U_Ent)
3828 then
3829 Error_Msg_N
3830 ("size cannot be given for unconstrained array", Nam);
3832 elsif Size /= No_Uint then
3833 if VM_Target /= No_VM and then not GNAT_Mode then
3835 -- Size clause is not handled properly on VM targets.
3836 -- Display a warning unless we are in GNAT mode, in which
3837 -- case this is useless.
3839 Error_Msg_N
3840 ("size clauses are ignored in this configuration??", N);
3841 end if;
3843 if Is_Type (U_Ent) then
3844 Etyp := U_Ent;
3845 else
3846 Etyp := Etype (U_Ent);
3847 end if;
3849 -- Check size, note that Gigi is in charge of checking that the
3850 -- size of an array or record type is OK. Also we do not check
3851 -- the size in the ordinary fixed-point case, since it is too
3852 -- early to do so (there may be subsequent small clause that
3853 -- affects the size). We can check the size if a small clause
3854 -- has already been given.
3856 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
3857 or else Has_Small_Clause (U_Ent)
3858 then
3859 Check_Size (Expr, Etyp, Size, Biased);
3860 Set_Biased (U_Ent, N, "size clause", Biased);
3861 end if;
3863 -- For types set RM_Size and Esize if possible
3865 if Is_Type (U_Ent) then
3866 Set_RM_Size (U_Ent, Size);
3868 -- For elementary types, increase Object_Size to power of 2,
3869 -- but not less than a storage unit in any case (normally
3870 -- this means it will be byte addressable).
3872 -- For all other types, nothing else to do, we leave Esize
3873 -- (object size) unset, the back end will set it from the
3874 -- size and alignment in an appropriate manner.
3876 -- In both cases, we check whether the alignment must be
3877 -- reset in the wake of the size change.
3879 if Is_Elementary_Type (U_Ent) then
3880 if Size <= System_Storage_Unit then
3881 Init_Esize (U_Ent, System_Storage_Unit);
3882 elsif Size <= 16 then
3883 Init_Esize (U_Ent, 16);
3884 elsif Size <= 32 then
3885 Init_Esize (U_Ent, 32);
3886 else
3887 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
3888 end if;
3890 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3891 else
3892 Alignment_Check_For_Size_Change (U_Ent, Size);
3893 end if;
3895 -- For objects, set Esize only
3897 else
3898 if Is_Elementary_Type (Etyp) then
3899 if Size /= System_Storage_Unit
3900 and then
3901 Size /= System_Storage_Unit * 2
3902 and then
3903 Size /= System_Storage_Unit * 4
3904 and then
3905 Size /= System_Storage_Unit * 8
3906 then
3907 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3908 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3909 Error_Msg_N
3910 ("size for primitive object must be a power of 2"
3911 & " in the range ^-^", N);
3912 end if;
3913 end if;
3915 Set_Esize (U_Ent, Size);
3916 end if;
3918 Set_Has_Size_Clause (U_Ent);
3919 end if;
3920 end Size;
3922 -----------
3923 -- Small --
3924 -----------
3926 -- Small attribute definition clause
3928 when Attribute_Small => Small : declare
3929 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3930 Small : Ureal;
3932 begin
3933 Analyze_And_Resolve (Expr, Any_Real);
3935 if Etype (Expr) = Any_Type then
3936 return;
3938 elsif not Is_Static_Expression (Expr) then
3939 Flag_Non_Static_Expr
3940 ("small requires static expression!", Expr);
3941 return;
3943 else
3944 Small := Expr_Value_R (Expr);
3946 if Small <= Ureal_0 then
3947 Error_Msg_N ("small value must be greater than zero", Expr);
3948 return;
3949 end if;
3951 end if;
3953 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3954 Error_Msg_N
3955 ("small requires an ordinary fixed point type", Nam);
3957 elsif Has_Small_Clause (U_Ent) then
3958 Error_Msg_N ("small already given for &", Nam);
3960 elsif Small > Delta_Value (U_Ent) then
3961 Error_Msg_N
3962 ("small value must not be greater than delta value", Nam);
3964 else
3965 Set_Small_Value (U_Ent, Small);
3966 Set_Small_Value (Implicit_Base, Small);
3967 Set_Has_Small_Clause (U_Ent);
3968 Set_Has_Small_Clause (Implicit_Base);
3969 Set_Has_Non_Standard_Rep (Implicit_Base);
3970 end if;
3971 end Small;
3973 ------------------
3974 -- Storage_Pool --
3975 ------------------
3977 -- Storage_Pool attribute definition clause
3979 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
3980 Pool : Entity_Id;
3981 T : Entity_Id;
3983 begin
3984 if Ekind (U_Ent) = E_Access_Subprogram_Type then
3985 Error_Msg_N
3986 ("storage pool cannot be given for access-to-subprogram type",
3987 Nam);
3988 return;
3990 elsif not
3991 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3992 then
3993 Error_Msg_N
3994 ("storage pool can only be given for access types", Nam);
3995 return;
3997 elsif Is_Derived_Type (U_Ent) then
3998 Error_Msg_N
3999 ("storage pool cannot be given for a derived access type",
4000 Nam);
4002 elsif Duplicate_Clause then
4003 return;
4005 elsif Present (Associated_Storage_Pool (U_Ent)) then
4006 Error_Msg_N ("storage pool already given for &", Nam);
4007 return;
4008 end if;
4010 if Id = Attribute_Storage_Pool then
4011 Analyze_And_Resolve
4012 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4014 -- In the Simple_Storage_Pool case, we allow a variable of any
4015 -- simple storage pool type, so we Resolve without imposing an
4016 -- expected type.
4018 else
4019 Analyze_And_Resolve (Expr);
4021 if not Present (Get_Rep_Pragma
4022 (Etype (Expr), Name_Simple_Storage_Pool_Type))
4023 then
4024 Error_Msg_N
4025 ("expression must be of a simple storage pool type", Expr);
4026 end if;
4027 end if;
4029 if not Denotes_Variable (Expr) then
4030 Error_Msg_N ("storage pool must be a variable", Expr);
4031 return;
4032 end if;
4034 if Nkind (Expr) = N_Type_Conversion then
4035 T := Etype (Expression (Expr));
4036 else
4037 T := Etype (Expr);
4038 end if;
4040 -- The Stack_Bounded_Pool is used internally for implementing
4041 -- access types with a Storage_Size. Since it only work properly
4042 -- when used on one specific type, we need to check that it is not
4043 -- hijacked improperly:
4045 -- type T is access Integer;
4046 -- for T'Storage_Size use n;
4047 -- type Q is access Float;
4048 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
4050 if RTE_Available (RE_Stack_Bounded_Pool)
4051 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
4052 then
4053 Error_Msg_N ("non-shareable internal Pool", Expr);
4054 return;
4055 end if;
4057 -- If the argument is a name that is not an entity name, then
4058 -- we construct a renaming operation to define an entity of
4059 -- type storage pool.
4061 if not Is_Entity_Name (Expr)
4062 and then Is_Object_Reference (Expr)
4063 then
4064 Pool := Make_Temporary (Loc, 'P', Expr);
4066 declare
4067 Rnode : constant Node_Id :=
4068 Make_Object_Renaming_Declaration (Loc,
4069 Defining_Identifier => Pool,
4070 Subtype_Mark =>
4071 New_Occurrence_Of (Etype (Expr), Loc),
4072 Name => Expr);
4074 begin
4075 Insert_Before (N, Rnode);
4076 Analyze (Rnode);
4077 Set_Associated_Storage_Pool (U_Ent, Pool);
4078 end;
4080 elsif Is_Entity_Name (Expr) then
4081 Pool := Entity (Expr);
4083 -- If pool is a renamed object, get original one. This can
4084 -- happen with an explicit renaming, and within instances.
4086 while Present (Renamed_Object (Pool))
4087 and then Is_Entity_Name (Renamed_Object (Pool))
4088 loop
4089 Pool := Entity (Renamed_Object (Pool));
4090 end loop;
4092 if Present (Renamed_Object (Pool))
4093 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
4094 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
4095 then
4096 Pool := Entity (Expression (Renamed_Object (Pool)));
4097 end if;
4099 Set_Associated_Storage_Pool (U_Ent, Pool);
4101 elsif Nkind (Expr) = N_Type_Conversion
4102 and then Is_Entity_Name (Expression (Expr))
4103 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
4104 then
4105 Pool := Entity (Expression (Expr));
4106 Set_Associated_Storage_Pool (U_Ent, Pool);
4108 else
4109 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
4110 return;
4111 end if;
4112 end;
4114 ------------------
4115 -- Storage_Size --
4116 ------------------
4118 -- Storage_Size attribute definition clause
4120 when Attribute_Storage_Size => Storage_Size : declare
4121 Btype : constant Entity_Id := Base_Type (U_Ent);
4123 begin
4124 if Is_Task_Type (U_Ent) then
4126 -- Check obsolescent (but never obsolescent if from aspect!)
4128 if not From_Aspect_Specification (N) then
4129 Check_Restriction (No_Obsolescent_Features, N);
4131 if Warn_On_Obsolescent_Feature then
4132 Error_Msg_N
4133 ("?j?storage size clause for task is an " &
4134 "obsolescent feature (RM J.9)", N);
4135 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
4136 end if;
4137 end if;
4139 FOnly := True;
4140 end if;
4142 if not Is_Access_Type (U_Ent)
4143 and then Ekind (U_Ent) /= E_Task_Type
4144 then
4145 Error_Msg_N ("storage size cannot be given for &", Nam);
4147 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
4148 Error_Msg_N
4149 ("storage size cannot be given for a derived access type",
4150 Nam);
4152 elsif Duplicate_Clause then
4153 null;
4155 else
4156 Analyze_And_Resolve (Expr, Any_Integer);
4158 if Is_Access_Type (U_Ent) then
4159 if Present (Associated_Storage_Pool (U_Ent)) then
4160 Error_Msg_N ("storage pool already given for &", Nam);
4161 return;
4162 end if;
4164 if Is_OK_Static_Expression (Expr)
4165 and then Expr_Value (Expr) = 0
4166 then
4167 Set_No_Pool_Assigned (Btype);
4168 end if;
4169 end if;
4171 Set_Has_Storage_Size_Clause (Btype);
4172 end if;
4173 end Storage_Size;
4175 -----------------
4176 -- Stream_Size --
4177 -----------------
4179 when Attribute_Stream_Size => Stream_Size : declare
4180 Size : constant Uint := Static_Integer (Expr);
4182 begin
4183 if Ada_Version <= Ada_95 then
4184 Check_Restriction (No_Implementation_Attributes, N);
4185 end if;
4187 if Duplicate_Clause then
4188 null;
4190 elsif Is_Elementary_Type (U_Ent) then
4191 if Size /= System_Storage_Unit
4192 and then
4193 Size /= System_Storage_Unit * 2
4194 and then
4195 Size /= System_Storage_Unit * 4
4196 and then
4197 Size /= System_Storage_Unit * 8
4198 then
4199 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4200 Error_Msg_N
4201 ("stream size for elementary type must be a"
4202 & " power of 2 and at least ^", N);
4204 elsif RM_Size (U_Ent) > Size then
4205 Error_Msg_Uint_1 := RM_Size (U_Ent);
4206 Error_Msg_N
4207 ("stream size for elementary type must be a"
4208 & " power of 2 and at least ^", N);
4209 end if;
4211 Set_Has_Stream_Size_Clause (U_Ent);
4213 else
4214 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
4215 end if;
4216 end Stream_Size;
4218 ----------------
4219 -- Value_Size --
4220 ----------------
4222 -- Value_Size attribute definition clause
4224 when Attribute_Value_Size => Value_Size : declare
4225 Size : constant Uint := Static_Integer (Expr);
4226 Biased : Boolean;
4228 begin
4229 if not Is_Type (U_Ent) then
4230 Error_Msg_N ("Value_Size cannot be given for &", Nam);
4232 elsif Duplicate_Clause then
4233 null;
4235 elsif Is_Array_Type (U_Ent)
4236 and then not Is_Constrained (U_Ent)
4237 then
4238 Error_Msg_N
4239 ("Value_Size cannot be given for unconstrained array", Nam);
4241 else
4242 if Is_Elementary_Type (U_Ent) then
4243 Check_Size (Expr, U_Ent, Size, Biased);
4244 Set_Biased (U_Ent, N, "value size clause", Biased);
4245 end if;
4247 Set_RM_Size (U_Ent, Size);
4248 end if;
4249 end Value_Size;
4251 -----------------------
4252 -- Variable_Indexing --
4253 -----------------------
4255 when Attribute_Variable_Indexing =>
4256 Check_Indexing_Functions;
4258 -----------
4259 -- Write --
4260 -----------
4262 when Attribute_Write =>
4263 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
4264 Set_Has_Specified_Stream_Write (Ent);
4266 -- All other attributes cannot be set
4268 when others =>
4269 Error_Msg_N
4270 ("attribute& cannot be set with definition clause", N);
4271 end case;
4273 -- The test for the type being frozen must be performed after any
4274 -- expression the clause has been analyzed since the expression itself
4275 -- might cause freezing that makes the clause illegal.
4277 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
4278 return;
4279 end if;
4280 end Analyze_Attribute_Definition_Clause;
4282 ----------------------------
4283 -- Analyze_Code_Statement --
4284 ----------------------------
4286 procedure Analyze_Code_Statement (N : Node_Id) is
4287 HSS : constant Node_Id := Parent (N);
4288 SBody : constant Node_Id := Parent (HSS);
4289 Subp : constant Entity_Id := Current_Scope;
4290 Stmt : Node_Id;
4291 Decl : Node_Id;
4292 StmtO : Node_Id;
4293 DeclO : Node_Id;
4295 begin
4296 -- Analyze and check we get right type, note that this implements the
4297 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
4298 -- is the only way that Asm_Insn could possibly be visible.
4300 Analyze_And_Resolve (Expression (N));
4302 if Etype (Expression (N)) = Any_Type then
4303 return;
4304 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
4305 Error_Msg_N ("incorrect type for code statement", N);
4306 return;
4307 end if;
4309 Check_Code_Statement (N);
4311 -- Make sure we appear in the handled statement sequence of a
4312 -- subprogram (RM 13.8(3)).
4314 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
4315 or else Nkind (SBody) /= N_Subprogram_Body
4316 then
4317 Error_Msg_N
4318 ("code statement can only appear in body of subprogram", N);
4319 return;
4320 end if;
4322 -- Do remaining checks (RM 13.8(3)) if not already done
4324 if not Is_Machine_Code_Subprogram (Subp) then
4325 Set_Is_Machine_Code_Subprogram (Subp);
4327 -- No exception handlers allowed
4329 if Present (Exception_Handlers (HSS)) then
4330 Error_Msg_N
4331 ("exception handlers not permitted in machine code subprogram",
4332 First (Exception_Handlers (HSS)));
4333 end if;
4335 -- No declarations other than use clauses and pragmas (we allow
4336 -- certain internally generated declarations as well).
4338 Decl := First (Declarations (SBody));
4339 while Present (Decl) loop
4340 DeclO := Original_Node (Decl);
4341 if Comes_From_Source (DeclO)
4342 and not Nkind_In (DeclO, N_Pragma,
4343 N_Use_Package_Clause,
4344 N_Use_Type_Clause,
4345 N_Implicit_Label_Declaration)
4346 then
4347 Error_Msg_N
4348 ("this declaration not allowed in machine code subprogram",
4349 DeclO);
4350 end if;
4352 Next (Decl);
4353 end loop;
4355 -- No statements other than code statements, pragmas, and labels.
4356 -- Again we allow certain internally generated statements.
4358 -- In Ada 2012, qualified expressions are names, and the code
4359 -- statement is initially parsed as a procedure call.
4361 Stmt := First (Statements (HSS));
4362 while Present (Stmt) loop
4363 StmtO := Original_Node (Stmt);
4365 -- A procedure call transformed into a code statement is OK.
4367 if Ada_Version >= Ada_2012
4368 and then Nkind (StmtO) = N_Procedure_Call_Statement
4369 and then Nkind (Name (StmtO)) = N_Qualified_Expression
4370 then
4371 null;
4373 elsif Comes_From_Source (StmtO)
4374 and then not Nkind_In (StmtO, N_Pragma,
4375 N_Label,
4376 N_Code_Statement)
4377 then
4378 Error_Msg_N
4379 ("this statement is not allowed in machine code subprogram",
4380 StmtO);
4381 end if;
4383 Next (Stmt);
4384 end loop;
4385 end if;
4386 end Analyze_Code_Statement;
4388 -----------------------------------------------
4389 -- Analyze_Enumeration_Representation_Clause --
4390 -----------------------------------------------
4392 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
4393 Ident : constant Node_Id := Identifier (N);
4394 Aggr : constant Node_Id := Array_Aggregate (N);
4395 Enumtype : Entity_Id;
4396 Elit : Entity_Id;
4397 Expr : Node_Id;
4398 Assoc : Node_Id;
4399 Choice : Node_Id;
4400 Val : Uint;
4402 Err : Boolean := False;
4403 -- Set True to avoid cascade errors and crashes on incorrect source code
4405 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
4406 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
4407 -- Allowed range of universal integer (= allowed range of enum lit vals)
4409 Min : Uint;
4410 Max : Uint;
4411 -- Minimum and maximum values of entries
4413 Max_Node : Node_Id;
4414 -- Pointer to node for literal providing max value
4416 begin
4417 if Ignore_Rep_Clauses then
4418 return;
4419 end if;
4421 -- Ignore enumeration rep clauses by default in CodePeer mode,
4422 -- unless -gnatd.I is specified, as a work around for potential false
4423 -- positive messages.
4425 if CodePeer_Mode and not Debug_Flag_Dot_II then
4426 return;
4427 end if;
4429 -- First some basic error checks
4431 Find_Type (Ident);
4432 Enumtype := Entity (Ident);
4434 if Enumtype = Any_Type
4435 or else Rep_Item_Too_Early (Enumtype, N)
4436 then
4437 return;
4438 else
4439 Enumtype := Underlying_Type (Enumtype);
4440 end if;
4442 if not Is_Enumeration_Type (Enumtype) then
4443 Error_Msg_NE
4444 ("enumeration type required, found}",
4445 Ident, First_Subtype (Enumtype));
4446 return;
4447 end if;
4449 -- Ignore rep clause on generic actual type. This will already have
4450 -- been flagged on the template as an error, and this is the safest
4451 -- way to ensure we don't get a junk cascaded message in the instance.
4453 if Is_Generic_Actual_Type (Enumtype) then
4454 return;
4456 -- Type must be in current scope
4458 elsif Scope (Enumtype) /= Current_Scope then
4459 Error_Msg_N ("type must be declared in this scope", Ident);
4460 return;
4462 -- Type must be a first subtype
4464 elsif not Is_First_Subtype (Enumtype) then
4465 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
4466 return;
4468 -- Ignore duplicate rep clause
4470 elsif Has_Enumeration_Rep_Clause (Enumtype) then
4471 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
4472 return;
4474 -- Don't allow rep clause for standard [wide_[wide_]]character
4476 elsif Is_Standard_Character_Type (Enumtype) then
4477 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
4478 return;
4480 -- Check that the expression is a proper aggregate (no parentheses)
4482 elsif Paren_Count (Aggr) /= 0 then
4483 Error_Msg
4484 ("extra parentheses surrounding aggregate not allowed",
4485 First_Sloc (Aggr));
4486 return;
4488 -- All tests passed, so set rep clause in place
4490 else
4491 Set_Has_Enumeration_Rep_Clause (Enumtype);
4492 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
4493 end if;
4495 -- Now we process the aggregate. Note that we don't use the normal
4496 -- aggregate code for this purpose, because we don't want any of the
4497 -- normal expansion activities, and a number of special semantic
4498 -- rules apply (including the component type being any integer type)
4500 Elit := First_Literal (Enumtype);
4502 -- First the positional entries if any
4504 if Present (Expressions (Aggr)) then
4505 Expr := First (Expressions (Aggr));
4506 while Present (Expr) loop
4507 if No (Elit) then
4508 Error_Msg_N ("too many entries in aggregate", Expr);
4509 return;
4510 end if;
4512 Val := Static_Integer (Expr);
4514 -- Err signals that we found some incorrect entries processing
4515 -- the list. The final checks for completeness and ordering are
4516 -- skipped in this case.
4518 if Val = No_Uint then
4519 Err := True;
4520 elsif Val < Lo or else Hi < Val then
4521 Error_Msg_N ("value outside permitted range", Expr);
4522 Err := True;
4523 end if;
4525 Set_Enumeration_Rep (Elit, Val);
4526 Set_Enumeration_Rep_Expr (Elit, Expr);
4527 Next (Expr);
4528 Next (Elit);
4529 end loop;
4530 end if;
4532 -- Now process the named entries if present
4534 if Present (Component_Associations (Aggr)) then
4535 Assoc := First (Component_Associations (Aggr));
4536 while Present (Assoc) loop
4537 Choice := First (Choices (Assoc));
4539 if Present (Next (Choice)) then
4540 Error_Msg_N
4541 ("multiple choice not allowed here", Next (Choice));
4542 Err := True;
4543 end if;
4545 if Nkind (Choice) = N_Others_Choice then
4546 Error_Msg_N ("others choice not allowed here", Choice);
4547 Err := True;
4549 elsif Nkind (Choice) = N_Range then
4551 -- ??? should allow zero/one element range here
4553 Error_Msg_N ("range not allowed here", Choice);
4554 Err := True;
4556 else
4557 Analyze_And_Resolve (Choice, Enumtype);
4559 if Error_Posted (Choice) then
4560 Err := True;
4561 end if;
4563 if not Err then
4564 if Is_Entity_Name (Choice)
4565 and then Is_Type (Entity (Choice))
4566 then
4567 Error_Msg_N ("subtype name not allowed here", Choice);
4568 Err := True;
4570 -- ??? should allow static subtype with zero/one entry
4572 elsif Etype (Choice) = Base_Type (Enumtype) then
4573 if not Is_Static_Expression (Choice) then
4574 Flag_Non_Static_Expr
4575 ("non-static expression used for choice!", Choice);
4576 Err := True;
4578 else
4579 Elit := Expr_Value_E (Choice);
4581 if Present (Enumeration_Rep_Expr (Elit)) then
4582 Error_Msg_Sloc :=
4583 Sloc (Enumeration_Rep_Expr (Elit));
4584 Error_Msg_NE
4585 ("representation for& previously given#",
4586 Choice, Elit);
4587 Err := True;
4588 end if;
4590 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
4592 Expr := Expression (Assoc);
4593 Val := Static_Integer (Expr);
4595 if Val = No_Uint then
4596 Err := True;
4598 elsif Val < Lo or else Hi < Val then
4599 Error_Msg_N ("value outside permitted range", Expr);
4600 Err := True;
4601 end if;
4603 Set_Enumeration_Rep (Elit, Val);
4604 end if;
4605 end if;
4606 end if;
4607 end if;
4609 Next (Assoc);
4610 end loop;
4611 end if;
4613 -- Aggregate is fully processed. Now we check that a full set of
4614 -- representations was given, and that they are in range and in order.
4615 -- These checks are only done if no other errors occurred.
4617 if not Err then
4618 Min := No_Uint;
4619 Max := No_Uint;
4621 Elit := First_Literal (Enumtype);
4622 while Present (Elit) loop
4623 if No (Enumeration_Rep_Expr (Elit)) then
4624 Error_Msg_NE ("missing representation for&!", N, Elit);
4626 else
4627 Val := Enumeration_Rep (Elit);
4629 if Min = No_Uint then
4630 Min := Val;
4631 end if;
4633 if Val /= No_Uint then
4634 if Max /= No_Uint and then Val <= Max then
4635 Error_Msg_NE
4636 ("enumeration value for& not ordered!",
4637 Enumeration_Rep_Expr (Elit), Elit);
4638 end if;
4640 Max_Node := Enumeration_Rep_Expr (Elit);
4641 Max := Val;
4642 end if;
4644 -- If there is at least one literal whose representation is not
4645 -- equal to the Pos value, then note that this enumeration type
4646 -- has a non-standard representation.
4648 if Val /= Enumeration_Pos (Elit) then
4649 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
4650 end if;
4651 end if;
4653 Next (Elit);
4654 end loop;
4656 -- Now set proper size information
4658 declare
4659 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
4661 begin
4662 if Has_Size_Clause (Enumtype) then
4664 -- All OK, if size is OK now
4666 if RM_Size (Enumtype) >= Minsize then
4667 null;
4669 else
4670 -- Try if we can get by with biasing
4672 Minsize :=
4673 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
4675 -- Error message if even biasing does not work
4677 if RM_Size (Enumtype) < Minsize then
4678 Error_Msg_Uint_1 := RM_Size (Enumtype);
4679 Error_Msg_Uint_2 := Max;
4680 Error_Msg_N
4681 ("previously given size (^) is too small "
4682 & "for this value (^)", Max_Node);
4684 -- If biasing worked, indicate that we now have biased rep
4686 else
4687 Set_Biased
4688 (Enumtype, Size_Clause (Enumtype), "size clause");
4689 end if;
4690 end if;
4692 else
4693 Set_RM_Size (Enumtype, Minsize);
4694 Set_Enum_Esize (Enumtype);
4695 end if;
4697 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
4698 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
4699 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
4700 end;
4701 end if;
4703 -- We repeat the too late test in case it froze itself!
4705 if Rep_Item_Too_Late (Enumtype, N) then
4706 null;
4707 end if;
4708 end Analyze_Enumeration_Representation_Clause;
4710 ----------------------------
4711 -- Analyze_Free_Statement --
4712 ----------------------------
4714 procedure Analyze_Free_Statement (N : Node_Id) is
4715 begin
4716 Analyze (Expression (N));
4717 end Analyze_Free_Statement;
4719 ---------------------------
4720 -- Analyze_Freeze_Entity --
4721 ---------------------------
4723 procedure Analyze_Freeze_Entity (N : Node_Id) is
4724 E : constant Entity_Id := Entity (N);
4726 begin
4727 -- Remember that we are processing a freezing entity. Required to
4728 -- ensure correct decoration of internal entities associated with
4729 -- interfaces (see New_Overloaded_Entity).
4731 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
4733 -- For tagged types covering interfaces add internal entities that link
4734 -- the primitives of the interfaces with the primitives that cover them.
4735 -- Note: These entities were originally generated only when generating
4736 -- code because their main purpose was to provide support to initialize
4737 -- the secondary dispatch tables. They are now generated also when
4738 -- compiling with no code generation to provide ASIS the relationship
4739 -- between interface primitives and tagged type primitives. They are
4740 -- also used to locate primitives covering interfaces when processing
4741 -- generics (see Derive_Subprograms).
4743 if Ada_Version >= Ada_2005
4744 and then Ekind (E) = E_Record_Type
4745 and then Is_Tagged_Type (E)
4746 and then not Is_Interface (E)
4747 and then Has_Interfaces (E)
4748 then
4749 -- This would be a good common place to call the routine that checks
4750 -- overriding of interface primitives (and thus factorize calls to
4751 -- Check_Abstract_Overriding located at different contexts in the
4752 -- compiler). However, this is not possible because it causes
4753 -- spurious errors in case of late overriding.
4755 Add_Internal_Interface_Entities (E);
4756 end if;
4758 -- Check CPP types
4760 if Ekind (E) = E_Record_Type
4761 and then Is_CPP_Class (E)
4762 and then Is_Tagged_Type (E)
4763 and then Tagged_Type_Expansion
4764 and then Expander_Active
4765 then
4766 if CPP_Num_Prims (E) = 0 then
4768 -- If the CPP type has user defined components then it must import
4769 -- primitives from C++. This is required because if the C++ class
4770 -- has no primitives then the C++ compiler does not added the _tag
4771 -- component to the type.
4773 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
4775 if First_Entity (E) /= Last_Entity (E) then
4776 Error_Msg_N
4777 ("'C'P'P type must import at least one primitive from C++??",
4779 end if;
4780 end if;
4782 -- Check that all its primitives are abstract or imported from C++.
4783 -- Check also availability of the C++ constructor.
4785 declare
4786 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
4787 Elmt : Elmt_Id;
4788 Error_Reported : Boolean := False;
4789 Prim : Node_Id;
4791 begin
4792 Elmt := First_Elmt (Primitive_Operations (E));
4793 while Present (Elmt) loop
4794 Prim := Node (Elmt);
4796 if Comes_From_Source (Prim) then
4797 if Is_Abstract_Subprogram (Prim) then
4798 null;
4800 elsif not Is_Imported (Prim)
4801 or else Convention (Prim) /= Convention_CPP
4802 then
4803 Error_Msg_N
4804 ("primitives of 'C'P'P types must be imported from C++ "
4805 & "or abstract??", Prim);
4807 elsif not Has_Constructors
4808 and then not Error_Reported
4809 then
4810 Error_Msg_Name_1 := Chars (E);
4811 Error_Msg_N
4812 ("??'C'P'P constructor required for type %", Prim);
4813 Error_Reported := True;
4814 end if;
4815 end if;
4817 Next_Elmt (Elmt);
4818 end loop;
4819 end;
4820 end if;
4822 -- Check Ada derivation of CPP type
4824 if Expander_Active
4825 and then Tagged_Type_Expansion
4826 and then Ekind (E) = E_Record_Type
4827 and then Etype (E) /= E
4828 and then Is_CPP_Class (Etype (E))
4829 and then CPP_Num_Prims (Etype (E)) > 0
4830 and then not Is_CPP_Class (E)
4831 and then not Has_CPP_Constructors (Etype (E))
4832 then
4833 -- If the parent has C++ primitives but it has no constructor then
4834 -- check that all the primitives are overridden in this derivation;
4835 -- otherwise the constructor of the parent is needed to build the
4836 -- dispatch table.
4838 declare
4839 Elmt : Elmt_Id;
4840 Prim : Node_Id;
4842 begin
4843 Elmt := First_Elmt (Primitive_Operations (E));
4844 while Present (Elmt) loop
4845 Prim := Node (Elmt);
4847 if not Is_Abstract_Subprogram (Prim)
4848 and then No (Interface_Alias (Prim))
4849 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
4850 then
4851 Error_Msg_Name_1 := Chars (Etype (E));
4852 Error_Msg_N
4853 ("'C'P'P constructor required for parent type %", E);
4854 exit;
4855 end if;
4857 Next_Elmt (Elmt);
4858 end loop;
4859 end;
4860 end if;
4862 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
4864 -- If we have a type with predicates, build predicate function
4866 if Is_Type (E) and then Has_Predicates (E) then
4867 Build_Predicate_Functions (E, N);
4868 end if;
4870 -- If type has delayed aspects, this is where we do the preanalysis at
4871 -- the freeze point, as part of the consistent visibility check. Note
4872 -- that this must be done after calling Build_Predicate_Functions or
4873 -- Build_Invariant_Procedure since these subprograms fix occurrences of
4874 -- the subtype name in the saved expression so that they will not cause
4875 -- trouble in the preanalysis.
4877 if Has_Delayed_Aspects (E)
4878 and then Scope (E) = Current_Scope
4879 then
4880 -- Retrieve the visibility to the discriminants in order to properly
4881 -- analyze the aspects.
4883 Push_Scope_And_Install_Discriminants (E);
4885 declare
4886 Ritem : Node_Id;
4888 begin
4889 -- Look for aspect specification entries for this entity
4891 Ritem := First_Rep_Item (E);
4892 while Present (Ritem) loop
4893 if Nkind (Ritem) = N_Aspect_Specification
4894 and then Entity (Ritem) = E
4895 and then Is_Delayed_Aspect (Ritem)
4896 then
4897 Check_Aspect_At_Freeze_Point (Ritem);
4898 end if;
4900 Next_Rep_Item (Ritem);
4901 end loop;
4902 end;
4904 Uninstall_Discriminants_And_Pop_Scope (E);
4905 end if;
4906 end Analyze_Freeze_Entity;
4908 ------------------------------------------
4909 -- Analyze_Record_Representation_Clause --
4910 ------------------------------------------
4912 -- Note: we check as much as we can here, but we can't do any checks
4913 -- based on the position values (e.g. overlap checks) until freeze time
4914 -- because especially in Ada 2005 (machine scalar mode), the processing
4915 -- for non-standard bit order can substantially change the positions.
4916 -- See procedure Check_Record_Representation_Clause (called from Freeze)
4917 -- for the remainder of this processing.
4919 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
4920 Ident : constant Node_Id := Identifier (N);
4921 Biased : Boolean;
4922 CC : Node_Id;
4923 Comp : Entity_Id;
4924 Fbit : Uint;
4925 Hbit : Uint := Uint_0;
4926 Lbit : Uint;
4927 Ocomp : Entity_Id;
4928 Posit : Uint;
4929 Rectype : Entity_Id;
4930 Recdef : Node_Id;
4932 function Is_Inherited (Comp : Entity_Id) return Boolean;
4933 -- True if Comp is an inherited component in a record extension
4935 ------------------
4936 -- Is_Inherited --
4937 ------------------
4939 function Is_Inherited (Comp : Entity_Id) return Boolean is
4940 Comp_Base : Entity_Id;
4942 begin
4943 if Ekind (Rectype) = E_Record_Subtype then
4944 Comp_Base := Original_Record_Component (Comp);
4945 else
4946 Comp_Base := Comp;
4947 end if;
4949 return Comp_Base /= Original_Record_Component (Comp_Base);
4950 end Is_Inherited;
4952 -- Local variables
4954 Is_Record_Extension : Boolean;
4955 -- True if Rectype is a record extension
4957 CR_Pragma : Node_Id := Empty;
4958 -- Points to N_Pragma node if Complete_Representation pragma present
4960 -- Start of processing for Analyze_Record_Representation_Clause
4962 begin
4963 if Ignore_Rep_Clauses then
4964 return;
4965 end if;
4967 Find_Type (Ident);
4968 Rectype := Entity (Ident);
4970 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
4971 return;
4972 else
4973 Rectype := Underlying_Type (Rectype);
4974 end if;
4976 -- First some basic error checks
4978 if not Is_Record_Type (Rectype) then
4979 Error_Msg_NE
4980 ("record type required, found}", Ident, First_Subtype (Rectype));
4981 return;
4983 elsif Scope (Rectype) /= Current_Scope then
4984 Error_Msg_N ("type must be declared in this scope", N);
4985 return;
4987 elsif not Is_First_Subtype (Rectype) then
4988 Error_Msg_N ("cannot give record rep clause for subtype", N);
4989 return;
4991 elsif Has_Record_Rep_Clause (Rectype) then
4992 Error_Msg_N ("duplicate record rep clause ignored", N);
4993 return;
4995 elsif Rep_Item_Too_Late (Rectype, N) then
4996 return;
4997 end if;
4999 -- We know we have a first subtype, now possibly go the the anonymous
5000 -- base type to determine whether Rectype is a record extension.
5002 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
5003 Is_Record_Extension :=
5004 Nkind (Recdef) = N_Derived_Type_Definition
5005 and then Present (Record_Extension_Part (Recdef));
5007 if Present (Mod_Clause (N)) then
5008 declare
5009 Loc : constant Source_Ptr := Sloc (N);
5010 M : constant Node_Id := Mod_Clause (N);
5011 P : constant List_Id := Pragmas_Before (M);
5012 AtM_Nod : Node_Id;
5014 Mod_Val : Uint;
5015 pragma Warnings (Off, Mod_Val);
5017 begin
5018 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
5020 if Warn_On_Obsolescent_Feature then
5021 Error_Msg_N
5022 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
5023 Error_Msg_N
5024 ("\?j?use alignment attribute definition clause instead", N);
5025 end if;
5027 if Present (P) then
5028 Analyze_List (P);
5029 end if;
5031 -- In ASIS_Mode mode, expansion is disabled, but we must convert
5032 -- the Mod clause into an alignment clause anyway, so that the
5033 -- back-end can compute and back-annotate properly the size and
5034 -- alignment of types that may include this record.
5036 -- This seems dubious, this destroys the source tree in a manner
5037 -- not detectable by ASIS ???
5039 if Operating_Mode = Check_Semantics and then ASIS_Mode then
5040 AtM_Nod :=
5041 Make_Attribute_Definition_Clause (Loc,
5042 Name => New_Reference_To (Base_Type (Rectype), Loc),
5043 Chars => Name_Alignment,
5044 Expression => Relocate_Node (Expression (M)));
5046 Set_From_At_Mod (AtM_Nod);
5047 Insert_After (N, AtM_Nod);
5048 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
5049 Set_Mod_Clause (N, Empty);
5051 else
5052 -- Get the alignment value to perform error checking
5054 Mod_Val := Get_Alignment_Value (Expression (M));
5055 end if;
5056 end;
5057 end if;
5059 -- For untagged types, clear any existing component clauses for the
5060 -- type. If the type is derived, this is what allows us to override
5061 -- a rep clause for the parent. For type extensions, the representation
5062 -- of the inherited components is inherited, so we want to keep previous
5063 -- component clauses for completeness.
5065 if not Is_Tagged_Type (Rectype) then
5066 Comp := First_Component_Or_Discriminant (Rectype);
5067 while Present (Comp) loop
5068 Set_Component_Clause (Comp, Empty);
5069 Next_Component_Or_Discriminant (Comp);
5070 end loop;
5071 end if;
5073 -- All done if no component clauses
5075 CC := First (Component_Clauses (N));
5077 if No (CC) then
5078 return;
5079 end if;
5081 -- A representation like this applies to the base type
5083 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
5084 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
5085 Set_Has_Specified_Layout (Base_Type (Rectype));
5087 -- Process the component clauses
5089 while Present (CC) loop
5091 -- Pragma
5093 if Nkind (CC) = N_Pragma then
5094 Analyze (CC);
5096 -- The only pragma of interest is Complete_Representation
5098 if Pragma_Name (CC) = Name_Complete_Representation then
5099 CR_Pragma := CC;
5100 end if;
5102 -- Processing for real component clause
5104 else
5105 Posit := Static_Integer (Position (CC));
5106 Fbit := Static_Integer (First_Bit (CC));
5107 Lbit := Static_Integer (Last_Bit (CC));
5109 if Posit /= No_Uint
5110 and then Fbit /= No_Uint
5111 and then Lbit /= No_Uint
5112 then
5113 if Posit < 0 then
5114 Error_Msg_N
5115 ("position cannot be negative", Position (CC));
5117 elsif Fbit < 0 then
5118 Error_Msg_N
5119 ("first bit cannot be negative", First_Bit (CC));
5121 -- The Last_Bit specified in a component clause must not be
5122 -- less than the First_Bit minus one (RM-13.5.1(10)).
5124 elsif Lbit < Fbit - 1 then
5125 Error_Msg_N
5126 ("last bit cannot be less than first bit minus one",
5127 Last_Bit (CC));
5129 -- Values look OK, so find the corresponding record component
5130 -- Even though the syntax allows an attribute reference for
5131 -- implementation-defined components, GNAT does not allow the
5132 -- tag to get an explicit position.
5134 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
5135 if Attribute_Name (Component_Name (CC)) = Name_Tag then
5136 Error_Msg_N ("position of tag cannot be specified", CC);
5137 else
5138 Error_Msg_N ("illegal component name", CC);
5139 end if;
5141 else
5142 Comp := First_Entity (Rectype);
5143 while Present (Comp) loop
5144 exit when Chars (Comp) = Chars (Component_Name (CC));
5145 Next_Entity (Comp);
5146 end loop;
5148 if No (Comp) then
5150 -- Maybe component of base type that is absent from
5151 -- statically constrained first subtype.
5153 Comp := First_Entity (Base_Type (Rectype));
5154 while Present (Comp) loop
5155 exit when Chars (Comp) = Chars (Component_Name (CC));
5156 Next_Entity (Comp);
5157 end loop;
5158 end if;
5160 if No (Comp) then
5161 Error_Msg_N
5162 ("component clause is for non-existent field", CC);
5164 -- Ada 2012 (AI05-0026): Any name that denotes a
5165 -- discriminant of an object of an unchecked union type
5166 -- shall not occur within a record_representation_clause.
5168 -- The general restriction of using record rep clauses on
5169 -- Unchecked_Union types has now been lifted. Since it is
5170 -- possible to introduce a record rep clause which mentions
5171 -- the discriminant of an Unchecked_Union in non-Ada 2012
5172 -- code, this check is applied to all versions of the
5173 -- language.
5175 elsif Ekind (Comp) = E_Discriminant
5176 and then Is_Unchecked_Union (Rectype)
5177 then
5178 Error_Msg_N
5179 ("cannot reference discriminant of unchecked union",
5180 Component_Name (CC));
5182 elsif Is_Record_Extension and then Is_Inherited (Comp) then
5183 Error_Msg_NE
5184 ("component clause not allowed for inherited "
5185 & "component&", CC, Comp);
5187 elsif Present (Component_Clause (Comp)) then
5189 -- Diagnose duplicate rep clause, or check consistency
5190 -- if this is an inherited component. In a double fault,
5191 -- there may be a duplicate inconsistent clause for an
5192 -- inherited component.
5194 if Scope (Original_Record_Component (Comp)) = Rectype
5195 or else Parent (Component_Clause (Comp)) = N
5196 then
5197 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
5198 Error_Msg_N ("component clause previously given#", CC);
5200 else
5201 declare
5202 Rep1 : constant Node_Id := Component_Clause (Comp);
5203 begin
5204 if Intval (Position (Rep1)) /=
5205 Intval (Position (CC))
5206 or else Intval (First_Bit (Rep1)) /=
5207 Intval (First_Bit (CC))
5208 or else Intval (Last_Bit (Rep1)) /=
5209 Intval (Last_Bit (CC))
5210 then
5211 Error_Msg_N
5212 ("component clause inconsistent "
5213 & "with representation of ancestor", CC);
5215 elsif Warn_On_Redundant_Constructs then
5216 Error_Msg_N
5217 ("?r?redundant confirming component clause "
5218 & "for component!", CC);
5219 end if;
5220 end;
5221 end if;
5223 -- Normal case where this is the first component clause we
5224 -- have seen for this entity, so set it up properly.
5226 else
5227 -- Make reference for field in record rep clause and set
5228 -- appropriate entity field in the field identifier.
5230 Generate_Reference
5231 (Comp, Component_Name (CC), Set_Ref => False);
5232 Set_Entity (Component_Name (CC), Comp);
5234 -- Update Fbit and Lbit to the actual bit number
5236 Fbit := Fbit + UI_From_Int (SSU) * Posit;
5237 Lbit := Lbit + UI_From_Int (SSU) * Posit;
5239 if Has_Size_Clause (Rectype)
5240 and then RM_Size (Rectype) <= Lbit
5241 then
5242 Error_Msg_N
5243 ("bit number out of range of specified size",
5244 Last_Bit (CC));
5245 else
5246 Set_Component_Clause (Comp, CC);
5247 Set_Component_Bit_Offset (Comp, Fbit);
5248 Set_Esize (Comp, 1 + (Lbit - Fbit));
5249 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
5250 Set_Normalized_Position (Comp, Fbit / SSU);
5252 if Warn_On_Overridden_Size
5253 and then Has_Size_Clause (Etype (Comp))
5254 and then RM_Size (Etype (Comp)) /= Esize (Comp)
5255 then
5256 Error_Msg_NE
5257 ("?S?component size overrides size clause for&",
5258 Component_Name (CC), Etype (Comp));
5259 end if;
5261 -- This information is also set in the corresponding
5262 -- component of the base type, found by accessing the
5263 -- Original_Record_Component link if it is present.
5265 Ocomp := Original_Record_Component (Comp);
5267 if Hbit < Lbit then
5268 Hbit := Lbit;
5269 end if;
5271 Check_Size
5272 (Component_Name (CC),
5273 Etype (Comp),
5274 Esize (Comp),
5275 Biased);
5277 Set_Biased
5278 (Comp, First_Node (CC), "component clause", Biased);
5280 if Present (Ocomp) then
5281 Set_Component_Clause (Ocomp, CC);
5282 Set_Component_Bit_Offset (Ocomp, Fbit);
5283 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
5284 Set_Normalized_Position (Ocomp, Fbit / SSU);
5285 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
5287 Set_Normalized_Position_Max
5288 (Ocomp, Normalized_Position (Ocomp));
5290 -- Note: we don't use Set_Biased here, because we
5291 -- already gave a warning above if needed, and we
5292 -- would get a duplicate for the same name here.
5294 Set_Has_Biased_Representation
5295 (Ocomp, Has_Biased_Representation (Comp));
5296 end if;
5298 if Esize (Comp) < 0 then
5299 Error_Msg_N ("component size is negative", CC);
5300 end if;
5301 end if;
5302 end if;
5303 end if;
5304 end if;
5305 end if;
5307 Next (CC);
5308 end loop;
5310 -- Check missing components if Complete_Representation pragma appeared
5312 if Present (CR_Pragma) then
5313 Comp := First_Component_Or_Discriminant (Rectype);
5314 while Present (Comp) loop
5315 if No (Component_Clause (Comp)) then
5316 Error_Msg_NE
5317 ("missing component clause for &", CR_Pragma, Comp);
5318 end if;
5320 Next_Component_Or_Discriminant (Comp);
5321 end loop;
5323 -- Give missing components warning if required
5325 elsif Warn_On_Unrepped_Components then
5326 declare
5327 Num_Repped_Components : Nat := 0;
5328 Num_Unrepped_Components : Nat := 0;
5330 begin
5331 -- First count number of repped and unrepped components
5333 Comp := First_Component_Or_Discriminant (Rectype);
5334 while Present (Comp) loop
5335 if Present (Component_Clause (Comp)) then
5336 Num_Repped_Components := Num_Repped_Components + 1;
5337 else
5338 Num_Unrepped_Components := Num_Unrepped_Components + 1;
5339 end if;
5341 Next_Component_Or_Discriminant (Comp);
5342 end loop;
5344 -- We are only interested in the case where there is at least one
5345 -- unrepped component, and at least half the components have rep
5346 -- clauses. We figure that if less than half have them, then the
5347 -- partial rep clause is really intentional. If the component
5348 -- type has no underlying type set at this point (as for a generic
5349 -- formal type), we don't know enough to give a warning on the
5350 -- component.
5352 if Num_Unrepped_Components > 0
5353 and then Num_Unrepped_Components < Num_Repped_Components
5354 then
5355 Comp := First_Component_Or_Discriminant (Rectype);
5356 while Present (Comp) loop
5357 if No (Component_Clause (Comp))
5358 and then Comes_From_Source (Comp)
5359 and then Present (Underlying_Type (Etype (Comp)))
5360 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
5361 or else Size_Known_At_Compile_Time
5362 (Underlying_Type (Etype (Comp))))
5363 and then not Has_Warnings_Off (Rectype)
5364 then
5365 Error_Msg_Sloc := Sloc (Comp);
5366 Error_Msg_NE
5367 ("?C?no component clause given for & declared #",
5368 N, Comp);
5369 end if;
5371 Next_Component_Or_Discriminant (Comp);
5372 end loop;
5373 end if;
5374 end;
5375 end if;
5376 end Analyze_Record_Representation_Clause;
5378 -------------------------------------------
5379 -- Build_Invariant_Procedure_Declaration --
5380 -------------------------------------------
5382 function Build_Invariant_Procedure_Declaration
5383 (Typ : Entity_Id) return Node_Id
5385 Loc : constant Source_Ptr := Sloc (Typ);
5386 Object_Entity : constant Entity_Id :=
5387 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
5388 Spec : Node_Id;
5389 SId : Entity_Id;
5391 begin
5392 Set_Etype (Object_Entity, Typ);
5394 -- Check for duplicate definiations.
5396 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
5397 return Empty;
5398 end if;
5400 SId :=
5401 Make_Defining_Identifier (Loc,
5402 Chars => New_External_Name (Chars (Typ), "Invariant"));
5403 Set_Has_Invariants (Typ);
5404 Set_Ekind (SId, E_Procedure);
5405 Set_Is_Invariant_Procedure (SId);
5406 Set_Invariant_Procedure (Typ, SId);
5408 Spec :=
5409 Make_Procedure_Specification (Loc,
5410 Defining_Unit_Name => SId,
5411 Parameter_Specifications => New_List (
5412 Make_Parameter_Specification (Loc,
5413 Defining_Identifier => Object_Entity,
5414 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5416 return Make_Subprogram_Declaration (Loc, Specification => Spec);
5417 end Build_Invariant_Procedure_Declaration;
5419 -------------------------------
5420 -- Build_Invariant_Procedure --
5421 -------------------------------
5423 -- The procedure that is constructed here has the form
5425 -- procedure typInvariant (Ixxx : typ) is
5426 -- begin
5427 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5428 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5429 -- ...
5430 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
5431 -- ...
5432 -- end typInvariant;
5434 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
5435 Loc : constant Source_Ptr := Sloc (Typ);
5436 Stmts : List_Id;
5437 Spec : Node_Id;
5438 SId : Entity_Id;
5439 PDecl : Node_Id;
5440 PBody : Node_Id;
5442 Visible_Decls : constant List_Id := Visible_Declarations (N);
5443 Private_Decls : constant List_Id := Private_Declarations (N);
5445 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
5446 -- Appends statements to Stmts for any invariants in the rep item chain
5447 -- of the given type. If Inherit is False, then we only process entries
5448 -- on the chain for the type Typ. If Inherit is True, then we ignore any
5449 -- Invariant aspects, but we process all Invariant'Class aspects, adding
5450 -- "inherited" to the exception message and generating an informational
5451 -- message about the inheritance of an invariant.
5453 Object_Name : Name_Id;
5454 -- Name for argument of invariant procedure
5456 Object_Entity : Node_Id;
5457 -- The entity of the formal for the procedure
5459 --------------------
5460 -- Add_Invariants --
5461 --------------------
5463 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
5464 Ritem : Node_Id;
5465 Arg1 : Node_Id;
5466 Arg2 : Node_Id;
5467 Arg3 : Node_Id;
5468 Exp : Node_Id;
5469 Loc : Source_Ptr;
5470 Assoc : List_Id;
5471 Str : String_Id;
5473 procedure Replace_Type_Reference (N : Node_Id);
5474 -- Replace a single occurrence N of the subtype name with a reference
5475 -- to the formal of the predicate function. N can be an identifier
5476 -- referencing the subtype, or a selected component, representing an
5477 -- appropriately qualified occurrence of the subtype name.
5479 procedure Replace_Type_References is
5480 new Replace_Type_References_Generic (Replace_Type_Reference);
5481 -- Traverse an expression replacing all occurrences of the subtype
5482 -- name with appropriate references to the object that is the formal
5483 -- parameter of the predicate function. Note that we must ensure
5484 -- that the type and entity information is properly set in the
5485 -- replacement node, since we will do a Preanalyze call of this
5486 -- expression without proper visibility of the procedure argument.
5488 ----------------------------
5489 -- Replace_Type_Reference --
5490 ----------------------------
5492 -- Note: See comments in Add_Predicates.Replace_Type_Reference
5493 -- regarding handling of Sloc and Comes_From_Source.
5495 procedure Replace_Type_Reference (N : Node_Id) is
5496 begin
5497 -- Invariant'Class, replace with T'Class (obj)
5499 if Class_Present (Ritem) then
5500 Rewrite (N,
5501 Make_Type_Conversion (Sloc (N),
5502 Subtype_Mark =>
5503 Make_Attribute_Reference (Sloc (N),
5504 Prefix => New_Occurrence_Of (T, Sloc (N)),
5505 Attribute_Name => Name_Class),
5506 Expression => Make_Identifier (Sloc (N), Object_Name)));
5508 Set_Entity (Expression (N), Object_Entity);
5509 Set_Etype (Expression (N), Typ);
5511 -- Invariant, replace with obj
5513 else
5514 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5515 Set_Entity (N, Object_Entity);
5516 Set_Etype (N, Typ);
5517 end if;
5519 Set_Comes_From_Source (N, True);
5520 end Replace_Type_Reference;
5522 -- Start of processing for Add_Invariants
5524 begin
5525 Ritem := First_Rep_Item (T);
5526 while Present (Ritem) loop
5527 if Nkind (Ritem) = N_Pragma
5528 and then Pragma_Name (Ritem) = Name_Invariant
5529 then
5530 Arg1 := First (Pragma_Argument_Associations (Ritem));
5531 Arg2 := Next (Arg1);
5532 Arg3 := Next (Arg2);
5534 Arg1 := Get_Pragma_Arg (Arg1);
5535 Arg2 := Get_Pragma_Arg (Arg2);
5537 -- For Inherit case, ignore Invariant, process only Class case
5539 if Inherit then
5540 if not Class_Present (Ritem) then
5541 goto Continue;
5542 end if;
5544 -- For Inherit false, process only item for right type
5546 else
5547 if Entity (Arg1) /= Typ then
5548 goto Continue;
5549 end if;
5550 end if;
5552 if No (Stmts) then
5553 Stmts := Empty_List;
5554 end if;
5556 Exp := New_Copy_Tree (Arg2);
5558 -- Preserve sloc of original pragma Invariant
5560 Loc := Sloc (Ritem);
5562 -- We need to replace any occurrences of the name of the type
5563 -- with references to the object, converted to type'Class in
5564 -- the case of Invariant'Class aspects.
5566 Replace_Type_References (Exp, Chars (T));
5568 -- If this invariant comes from an aspect, find the aspect
5569 -- specification, and replace the saved expression because
5570 -- we need the subtype references replaced for the calls to
5571 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5572 -- and Check_Aspect_At_End_Of_Declarations.
5574 if From_Aspect_Specification (Ritem) then
5575 declare
5576 Aitem : Node_Id;
5578 begin
5579 -- Loop to find corresponding aspect, note that this
5580 -- must be present given the pragma is marked delayed.
5582 Aitem := Next_Rep_Item (Ritem);
5583 while Present (Aitem) loop
5584 if Nkind (Aitem) = N_Aspect_Specification
5585 and then Aspect_Rep_Item (Aitem) = Ritem
5586 then
5587 Set_Entity
5588 (Identifier (Aitem), New_Copy_Tree (Exp));
5589 exit;
5590 end if;
5592 Aitem := Next_Rep_Item (Aitem);
5593 end loop;
5594 end;
5595 end if;
5597 -- Now we need to preanalyze the expression to properly capture
5598 -- the visibility in the visible part. The expression will not
5599 -- be analyzed for real until the body is analyzed, but that is
5600 -- at the end of the private part and has the wrong visibility.
5602 Set_Parent (Exp, N);
5603 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
5605 -- Build first two arguments for Check pragma
5607 Assoc := New_List (
5608 Make_Pragma_Argument_Association (Loc,
5609 Expression => Make_Identifier (Loc, Name_Invariant)),
5610 Make_Pragma_Argument_Association (Loc,
5611 Expression => Exp));
5613 -- Add message if present in Invariant pragma
5615 if Present (Arg3) then
5616 Str := Strval (Get_Pragma_Arg (Arg3));
5618 -- If inherited case, and message starts "failed invariant",
5619 -- change it to be "failed inherited invariant".
5621 if Inherit then
5622 String_To_Name_Buffer (Str);
5624 if Name_Buffer (1 .. 16) = "failed invariant" then
5625 Insert_Str_In_Name_Buffer ("inherited ", 8);
5626 Str := String_From_Name_Buffer;
5627 end if;
5628 end if;
5630 Append_To (Assoc,
5631 Make_Pragma_Argument_Association (Loc,
5632 Expression => Make_String_Literal (Loc, Str)));
5633 end if;
5635 -- Add Check pragma to list of statements
5637 Append_To (Stmts,
5638 Make_Pragma (Loc,
5639 Pragma_Identifier =>
5640 Make_Identifier (Loc, Name_Check),
5641 Pragma_Argument_Associations => Assoc));
5643 -- If Inherited case and option enabled, output info msg. Note
5644 -- that we know this is a case of Invariant'Class.
5646 if Inherit and Opt.List_Inherited_Aspects then
5647 Error_Msg_Sloc := Sloc (Ritem);
5648 Error_Msg_N
5649 ("?L?info: & inherits `Invariant''Class` aspect from #",
5650 Typ);
5651 end if;
5652 end if;
5654 <<Continue>>
5655 Next_Rep_Item (Ritem);
5656 end loop;
5657 end Add_Invariants;
5659 -- Start of processing for Build_Invariant_Procedure
5661 begin
5662 Stmts := No_List;
5663 PDecl := Empty;
5664 PBody := Empty;
5665 SId := Empty;
5667 -- If the aspect specification exists for some view of the type, the
5668 -- declaration for the procedure has been created.
5670 if Has_Invariants (Typ) then
5671 SId := Invariant_Procedure (Typ);
5672 end if;
5674 if Present (SId) then
5675 PDecl := Unit_Declaration_Node (SId);
5677 else
5678 PDecl := Build_Invariant_Procedure_Declaration (Typ);
5679 end if;
5681 -- Recover formal of procedure, for use in the calls to invariant
5682 -- functions (including inherited ones).
5684 Object_Entity :=
5685 Defining_Identifier
5686 (First (Parameter_Specifications (Specification (PDecl))));
5687 Object_Name := Chars (Object_Entity);
5689 -- Add invariants for the current type
5691 Add_Invariants (Typ, Inherit => False);
5693 -- Add invariants for parent types
5695 declare
5696 Current_Typ : Entity_Id;
5697 Parent_Typ : Entity_Id;
5699 begin
5700 Current_Typ := Typ;
5701 loop
5702 Parent_Typ := Etype (Current_Typ);
5704 if Is_Private_Type (Parent_Typ)
5705 and then Present (Full_View (Base_Type (Parent_Typ)))
5706 then
5707 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5708 end if;
5710 exit when Parent_Typ = Current_Typ;
5712 Current_Typ := Parent_Typ;
5713 Add_Invariants (Current_Typ, Inherit => True);
5714 end loop;
5715 end;
5717 -- Build the procedure if we generated at least one Check pragma
5719 if Stmts /= No_List then
5720 Spec := Copy_Separate_Tree (Specification (PDecl));
5722 PBody :=
5723 Make_Subprogram_Body (Loc,
5724 Specification => Spec,
5725 Declarations => Empty_List,
5726 Handled_Statement_Sequence =>
5727 Make_Handled_Sequence_Of_Statements (Loc,
5728 Statements => Stmts));
5730 -- Insert procedure declaration and spec at the appropriate points.
5731 -- If declaration is already analyzed, it was processed by the
5732 -- generated pragma.
5734 if Present (Private_Decls) then
5736 -- The spec goes at the end of visible declarations, but they have
5737 -- already been analyzed, so we need to explicitly do the analyze.
5739 if not Analyzed (PDecl) then
5740 Append_To (Visible_Decls, PDecl);
5741 Analyze (PDecl);
5742 end if;
5744 -- The body goes at the end of the private declarations, which we
5745 -- have not analyzed yet, so we do not need to perform an explicit
5746 -- analyze call. We skip this if there are no private declarations
5747 -- (this is an error that will be caught elsewhere);
5749 Append_To (Private_Decls, PBody);
5751 -- If the invariant appears on the full view of a type, the
5752 -- analysis of the private part is complete, and we must
5753 -- analyze the new body explicitly.
5755 if In_Private_Part (Current_Scope) then
5756 Analyze (PBody);
5757 end if;
5759 -- If there are no private declarations this may be an error that
5760 -- will be diagnosed elsewhere. However, if this is a non-private
5761 -- type that inherits invariants, it needs no completion and there
5762 -- may be no private part. In this case insert invariant procedure
5763 -- at end of current declarative list, and analyze at once, given
5764 -- that the type is about to be frozen.
5766 elsif not Is_Private_Type (Typ) then
5767 Append_To (Visible_Decls, PDecl);
5768 Append_To (Visible_Decls, PBody);
5769 Analyze (PDecl);
5770 Analyze (PBody);
5771 end if;
5772 end if;
5773 end Build_Invariant_Procedure;
5775 -------------------------------
5776 -- Build_Predicate_Functions --
5777 -------------------------------
5779 -- The procedures that are constructed here have the form:
5781 -- function typPredicate (Ixxx : typ) return Boolean is
5782 -- begin
5783 -- return
5784 -- exp1 and then exp2 and then ...
5785 -- and then typ1Predicate (typ1 (Ixxx))
5786 -- and then typ2Predicate (typ2 (Ixxx))
5787 -- and then ...;
5788 -- end typPredicate;
5790 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
5791 -- this is the point at which these expressions get analyzed, providing the
5792 -- required delay, and typ1, typ2, are entities from which predicates are
5793 -- inherited. Note that we do NOT generate Check pragmas, that's because we
5794 -- use this function even if checks are off, e.g. for membership tests.
5796 -- If the expression has at least one Raise_Expression, then we also build
5797 -- the typPredicateM version of the function, in which any occurrence of a
5798 -- Raise_Expression is converted to "return False".
5800 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
5801 Loc : constant Source_Ptr := Sloc (Typ);
5803 Expr : Node_Id;
5804 -- This is the expression for the result of the function. It is
5805 -- is build by connecting the component predicates with AND THEN.
5807 Expr_M : Node_Id;
5808 -- This is the corresponding return expression for the Predicate_M
5809 -- function. It differs in that raise expressions are marked for
5810 -- special expansion (see Process_REs).
5812 Object_Name : constant Name_Id := New_Internal_Name ('I');
5813 -- Name for argument of Predicate procedure. Note that we use the same
5814 -- name for both predicate procedure. That way the reference within the
5815 -- predicate expression is the same in both functions.
5817 Object_Entity : constant Entity_Id :=
5818 Make_Defining_Identifier (Loc, Chars => Object_Name);
5819 -- Entity for argument of Predicate procedure
5821 Object_Entity_M : constant Entity_Id :=
5822 Make_Defining_Identifier (Loc, Chars => Object_Name);
5823 -- Entity for argument of Predicate_M procedure
5825 Raise_Expression_Present : Boolean := False;
5826 -- Set True if Expr has at least one Raise_Expression
5828 Static_Predic : Node_Id := Empty;
5829 -- Set to N_Pragma node for a static predicate if one is encountered
5831 procedure Add_Call (T : Entity_Id);
5832 -- Includes a call to the predicate function for type T in Expr if T
5833 -- has predicates and Predicate_Function (T) is non-empty.
5835 procedure Add_Predicates;
5836 -- Appends expressions for any Predicate pragmas in the rep item chain
5837 -- Typ to Expr. Note that we look only at items for this exact entity.
5838 -- Inheritance of predicates for the parent type is done by calling the
5839 -- Predicate_Function of the parent type, using Add_Call above.
5841 function Test_RE (N : Node_Id) return Traverse_Result;
5842 -- Used in Test_REs, tests one node for being a raise expression, and if
5843 -- so sets Raise_Expression_Present True.
5845 procedure Test_REs is new Traverse_Proc (Test_RE);
5846 -- Tests to see if Expr contains any raise expressions
5848 function Process_RE (N : Node_Id) return Traverse_Result;
5849 -- Used in Process REs, tests if node N is a raise expression, and if
5850 -- so, marks it to be converted to return False.
5852 procedure Process_REs is new Traverse_Proc (Process_RE);
5853 -- Marks any raise expressions in Expr_M to return False
5855 --------------
5856 -- Add_Call --
5857 --------------
5859 procedure Add_Call (T : Entity_Id) is
5860 Exp : Node_Id;
5862 begin
5863 if Present (T) and then Present (Predicate_Function (T)) then
5864 Set_Has_Predicates (Typ);
5866 -- Build the call to the predicate function of T
5868 Exp :=
5869 Make_Predicate_Call
5870 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
5872 -- Add call to evolving expression, using AND THEN if needed
5874 if No (Expr) then
5875 Expr := Exp;
5876 else
5877 Expr :=
5878 Make_And_Then (Loc,
5879 Left_Opnd => Relocate_Node (Expr),
5880 Right_Opnd => Exp);
5881 end if;
5883 -- Output info message on inheritance if required. Note we do not
5884 -- give this information for generic actual types, since it is
5885 -- unwelcome noise in that case in instantiations. We also
5886 -- generally suppress the message in instantiations, and also
5887 -- if it involves internal names.
5889 if Opt.List_Inherited_Aspects
5890 and then not Is_Generic_Actual_Type (Typ)
5891 and then Instantiation_Depth (Sloc (Typ)) = 0
5892 and then not Is_Internal_Name (Chars (T))
5893 and then not Is_Internal_Name (Chars (Typ))
5894 then
5895 Error_Msg_Sloc := Sloc (Predicate_Function (T));
5896 Error_Msg_Node_2 := T;
5897 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
5898 end if;
5899 end if;
5900 end Add_Call;
5902 --------------------
5903 -- Add_Predicates --
5904 --------------------
5906 procedure Add_Predicates is
5907 Ritem : Node_Id;
5908 Arg1 : Node_Id;
5909 Arg2 : Node_Id;
5911 procedure Replace_Type_Reference (N : Node_Id);
5912 -- Replace a single occurrence N of the subtype name with a reference
5913 -- to the formal of the predicate function. N can be an identifier
5914 -- referencing the subtype, or a selected component, representing an
5915 -- appropriately qualified occurrence of the subtype name.
5917 procedure Replace_Type_References is
5918 new Replace_Type_References_Generic (Replace_Type_Reference);
5919 -- Traverse an expression changing every occurrence of an identifier
5920 -- whose name matches the name of the subtype with a reference to
5921 -- the formal parameter of the predicate function.
5923 ----------------------------
5924 -- Replace_Type_Reference --
5925 ----------------------------
5927 procedure Replace_Type_Reference (N : Node_Id) is
5928 begin
5929 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5930 -- Use the Sloc of the usage name, not the defining name
5932 Set_Etype (N, Typ);
5933 Set_Entity (N, Object_Entity);
5935 -- We want to treat the node as if it comes from source, so that
5936 -- ASIS will not ignore it
5938 Set_Comes_From_Source (N, True);
5939 end Replace_Type_Reference;
5941 -- Start of processing for Add_Predicates
5943 begin
5944 Ritem := First_Rep_Item (Typ);
5945 while Present (Ritem) loop
5946 if Nkind (Ritem) = N_Pragma
5947 and then Pragma_Name (Ritem) = Name_Predicate
5948 then
5949 -- Save the static predicate of the type for diagnostics and
5950 -- error reporting purposes.
5952 if Present (Corresponding_Aspect (Ritem))
5953 and then Chars (Identifier (Corresponding_Aspect (Ritem))) =
5954 Name_Static_Predicate
5955 then
5956 Static_Predic := Ritem;
5957 end if;
5959 -- Acquire arguments
5961 Arg1 := First (Pragma_Argument_Associations (Ritem));
5962 Arg2 := Next (Arg1);
5964 Arg1 := Get_Pragma_Arg (Arg1);
5965 Arg2 := Get_Pragma_Arg (Arg2);
5967 -- See if this predicate pragma is for the current type or for
5968 -- its full view. A predicate on a private completion is placed
5969 -- on the partial view beause this is the visible entity that
5970 -- is frozen.
5972 if Entity (Arg1) = Typ
5973 or else Full_View (Entity (Arg1)) = Typ
5974 then
5975 -- We have a match, this entry is for our subtype
5977 -- We need to replace any occurrences of the name of the
5978 -- type with references to the object.
5980 Replace_Type_References (Arg2, Chars (Typ));
5982 -- If this predicate comes from an aspect, find the aspect
5983 -- specification, and replace the saved expression because
5984 -- we need the subtype references replaced for the calls to
5985 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5986 -- and Check_Aspect_At_End_Of_Declarations.
5988 if From_Aspect_Specification (Ritem) then
5989 declare
5990 Aitem : Node_Id;
5992 begin
5993 -- Loop to find corresponding aspect, note that this
5994 -- must be present given the pragma is marked delayed.
5996 Aitem := Next_Rep_Item (Ritem);
5997 loop
5998 if Nkind (Aitem) = N_Aspect_Specification
5999 and then Aspect_Rep_Item (Aitem) = Ritem
6000 then
6001 Set_Entity
6002 (Identifier (Aitem), New_Copy_Tree (Arg2));
6003 exit;
6004 end if;
6006 Aitem := Next_Rep_Item (Aitem);
6007 end loop;
6008 end;
6009 end if;
6011 -- Now we can add the expression
6013 if No (Expr) then
6014 Expr := Relocate_Node (Arg2);
6016 -- There already was a predicate, so add to it
6018 else
6019 Expr :=
6020 Make_And_Then (Loc,
6021 Left_Opnd => Relocate_Node (Expr),
6022 Right_Opnd => Relocate_Node (Arg2));
6023 end if;
6024 end if;
6025 end if;
6027 Next_Rep_Item (Ritem);
6028 end loop;
6029 end Add_Predicates;
6031 ----------------
6032 -- Process_RE --
6033 ----------------
6035 function Process_RE (N : Node_Id) return Traverse_Result is
6036 begin
6037 if Nkind (N) = N_Raise_Expression then
6038 Set_Convert_To_Return_False (N);
6039 return Skip;
6040 else
6041 return OK;
6042 end if;
6043 end Process_RE;
6045 -------------
6046 -- Test_RE --
6047 -------------
6049 function Test_RE (N : Node_Id) return Traverse_Result is
6050 begin
6051 if Nkind (N) = N_Raise_Expression then
6052 Raise_Expression_Present := True;
6053 return Abandon;
6054 else
6055 return OK;
6056 end if;
6057 end Test_RE;
6059 -- Start of processing for Build_Predicate_Functions
6061 begin
6062 -- Return if already built or if type does not have predicates
6064 if not Has_Predicates (Typ)
6065 or else Present (Predicate_Function (Typ))
6066 then
6067 return;
6068 end if;
6070 -- Prepare to construct predicate expression
6072 Expr := Empty;
6074 -- Add Predicates for the current type
6076 Add_Predicates;
6078 -- Add predicates for ancestor if present
6080 declare
6081 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
6082 begin
6083 if Present (Atyp) then
6084 Add_Call (Atyp);
6085 end if;
6086 end;
6088 -- Case where predicates are present
6090 if Present (Expr) then
6092 -- Test for raise expression present
6094 Test_REs (Expr);
6096 -- If raise expression is present, capture a copy of Expr for use
6097 -- in building the predicateM function version later on. For this
6098 -- copy we replace references to Object_Entity by Object_Entity_M.
6100 if Raise_Expression_Present then
6101 declare
6102 Map : constant Elist_Id := New_Elmt_List;
6103 begin
6104 Append_Elmt (Object_Entity, Map);
6105 Append_Elmt (Object_Entity_M, Map);
6106 Expr_M := New_Copy_Tree (Expr, Map => Map);
6107 end;
6108 end if;
6110 -- Build the main predicate function
6112 declare
6113 SId : constant Entity_Id :=
6114 Make_Defining_Identifier (Loc,
6115 Chars => New_External_Name (Chars (Typ), "Predicate"));
6116 -- The entity for the the function spec
6118 SIdB : constant Entity_Id :=
6119 Make_Defining_Identifier (Loc,
6120 Chars => New_External_Name (Chars (Typ), "Predicate"));
6121 -- The entity for the function body
6123 Spec : Node_Id;
6124 FDecl : Node_Id;
6125 FBody : Node_Id;
6127 begin
6128 -- Build function declaration
6130 Set_Ekind (SId, E_Function);
6131 Set_Is_Predicate_Function (SId);
6132 Set_Predicate_Function (Typ, SId);
6134 -- The predicate function is shared between views of a type
6136 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6137 Set_Predicate_Function (Full_View (Typ), SId);
6138 end if;
6140 Spec :=
6141 Make_Function_Specification (Loc,
6142 Defining_Unit_Name => SId,
6143 Parameter_Specifications => New_List (
6144 Make_Parameter_Specification (Loc,
6145 Defining_Identifier => Object_Entity,
6146 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6147 Result_Definition =>
6148 New_Occurrence_Of (Standard_Boolean, Loc));
6150 FDecl :=
6151 Make_Subprogram_Declaration (Loc,
6152 Specification => Spec);
6154 -- Build function body
6156 Spec :=
6157 Make_Function_Specification (Loc,
6158 Defining_Unit_Name => SIdB,
6159 Parameter_Specifications => New_List (
6160 Make_Parameter_Specification (Loc,
6161 Defining_Identifier =>
6162 Make_Defining_Identifier (Loc, Object_Name),
6163 Parameter_Type =>
6164 New_Occurrence_Of (Typ, Loc))),
6165 Result_Definition =>
6166 New_Occurrence_Of (Standard_Boolean, Loc));
6168 FBody :=
6169 Make_Subprogram_Body (Loc,
6170 Specification => Spec,
6171 Declarations => Empty_List,
6172 Handled_Statement_Sequence =>
6173 Make_Handled_Sequence_Of_Statements (Loc,
6174 Statements => New_List (
6175 Make_Simple_Return_Statement (Loc,
6176 Expression => Expr))));
6178 -- Insert declaration before freeze node and body after
6180 Insert_Before_And_Analyze (N, FDecl);
6181 Insert_After_And_Analyze (N, FBody);
6182 end;
6184 -- Test for raise expressions present and if so build M version
6186 if Raise_Expression_Present then
6187 declare
6188 SId : constant Entity_Id :=
6189 Make_Defining_Identifier (Loc,
6190 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6191 -- The entity for the the function spec
6193 SIdB : constant Entity_Id :=
6194 Make_Defining_Identifier (Loc,
6195 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6196 -- The entity for the function body
6198 Spec : Node_Id;
6199 FDecl : Node_Id;
6200 FBody : Node_Id;
6201 BTemp : Entity_Id;
6203 begin
6204 -- Mark any raise expressions for special expansion
6206 Process_REs (Expr_M);
6208 -- Build function declaration
6210 Set_Ekind (SId, E_Function);
6211 Set_Is_Predicate_Function_M (SId);
6212 Set_Predicate_Function_M (Typ, SId);
6214 -- The predicate function is shared between views of a type
6216 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6217 Set_Predicate_Function_M (Full_View (Typ), SId);
6218 end if;
6220 Spec :=
6221 Make_Function_Specification (Loc,
6222 Defining_Unit_Name => SId,
6223 Parameter_Specifications => New_List (
6224 Make_Parameter_Specification (Loc,
6225 Defining_Identifier => Object_Entity_M,
6226 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6227 Result_Definition =>
6228 New_Occurrence_Of (Standard_Boolean, Loc));
6230 FDecl :=
6231 Make_Subprogram_Declaration (Loc,
6232 Specification => Spec);
6234 -- Build function body
6236 Spec :=
6237 Make_Function_Specification (Loc,
6238 Defining_Unit_Name => SIdB,
6239 Parameter_Specifications => New_List (
6240 Make_Parameter_Specification (Loc,
6241 Defining_Identifier =>
6242 Make_Defining_Identifier (Loc, Object_Name),
6243 Parameter_Type =>
6244 New_Occurrence_Of (Typ, Loc))),
6245 Result_Definition =>
6246 New_Occurrence_Of (Standard_Boolean, Loc));
6248 -- Build the body, we declare the boolean expression before
6249 -- doing the return, because we are not really confident of
6250 -- what happens if a return appears within a return!
6252 BTemp :=
6253 Make_Defining_Identifier (Loc,
6254 Chars => New_Internal_Name ('B'));
6256 FBody :=
6257 Make_Subprogram_Body (Loc,
6258 Specification => Spec,
6260 Declarations => New_List (
6261 Make_Object_Declaration (Loc,
6262 Defining_Identifier => BTemp,
6263 Constant_Present => True,
6264 Object_Definition =>
6265 New_Reference_To (Standard_Boolean, Loc),
6266 Expression => Expr_M)),
6268 Handled_Statement_Sequence =>
6269 Make_Handled_Sequence_Of_Statements (Loc,
6270 Statements => New_List (
6271 Make_Simple_Return_Statement (Loc,
6272 Expression => New_Reference_To (BTemp, Loc)))));
6274 -- Insert declaration before freeze node and body after
6276 Insert_Before_And_Analyze (N, FDecl);
6277 Insert_After_And_Analyze (N, FBody);
6278 end;
6279 end if;
6281 if Is_Scalar_Type (Typ) then
6283 -- Attempt to build a static predicate for a discrete or a real
6284 -- subtype. This action may fail because the actual expression may
6285 -- not be static. Note that the presence of an inherited or
6286 -- explicitly declared dynamic predicate is orthogonal to this
6287 -- check because we are only interested in the static predicate.
6289 if Ekind_In (Typ, E_Decimal_Fixed_Point_Subtype,
6290 E_Enumeration_Subtype,
6291 E_Floating_Point_Subtype,
6292 E_Modular_Integer_Subtype,
6293 E_Ordinary_Fixed_Point_Subtype,
6294 E_Signed_Integer_Subtype)
6295 then
6296 Build_Static_Predicate (Typ, Expr, Object_Name);
6298 -- Emit an error when the predicate is categorized as static
6299 -- but its expression is dynamic.
6301 if Present (Static_Predic)
6302 and then No (Static_Predicate (Typ))
6303 then
6304 Error_Msg_F
6305 ("expression does not have required form for "
6306 & "static predicate",
6307 Next (First (Pragma_Argument_Associations
6308 (Static_Predic))));
6309 end if;
6310 end if;
6312 -- If a static predicate applies on other types, that's an error:
6313 -- either the type is scalar but non-static, or it's not even a
6314 -- scalar type. We do not issue an error on generated types, as
6315 -- these may be duplicates of the same error on a source type.
6317 elsif Present (Static_Predic) and then Comes_From_Source (Typ) then
6318 if Is_Scalar_Type (Typ) then
6319 Error_Msg_FE
6320 ("static predicate not allowed for non-static type&",
6321 Typ, Typ);
6322 else
6323 Error_Msg_FE
6324 ("static predicate not allowed for non-scalar type&",
6325 Typ, Typ);
6326 end if;
6327 end if;
6328 end if;
6329 end Build_Predicate_Functions;
6331 ----------------------------
6332 -- Build_Static_Predicate --
6333 ----------------------------
6335 procedure Build_Static_Predicate
6336 (Typ : Entity_Id;
6337 Expr : Node_Id;
6338 Nam : Name_Id)
6340 Loc : constant Source_Ptr := Sloc (Expr);
6342 Non_Static : exception;
6343 -- Raised if something non-static is found
6345 Btyp : constant Entity_Id := Base_Type (Typ);
6347 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6348 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6349 -- Low bound and high bound value of base type of Typ
6351 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6352 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6353 -- Low bound and high bound values of static subtype Typ
6355 type REnt is record
6356 Lo, Hi : Uint;
6357 end record;
6358 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6359 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6360 -- value.
6362 type RList is array (Nat range <>) of REnt;
6363 -- A list of ranges. The ranges are sorted in increasing order, and are
6364 -- disjoint (there is a gap of at least one value between each range in
6365 -- the table). A value is in the set of ranges in Rlist if it lies
6366 -- within one of these ranges.
6368 False_Range : constant RList :=
6369 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6370 -- An empty set of ranges represents a range list that can never be
6371 -- satisfied, since there are no ranges in which the value could lie,
6372 -- so it does not lie in any of them. False_Range is a canonical value
6373 -- for this empty set, but general processing should test for an Rlist
6374 -- with length zero (see Is_False predicate), since other null ranges
6375 -- may appear which must be treated as False.
6377 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6378 -- Range representing True, value must be in the base range
6380 function "and" (Left : RList; Right : RList) return RList;
6381 -- And's together two range lists, returning a range list. This is a set
6382 -- intersection operation.
6384 function "or" (Left : RList; Right : RList) return RList;
6385 -- Or's together two range lists, returning a range list. This is a set
6386 -- union operation.
6388 function "not" (Right : RList) return RList;
6389 -- Returns complement of a given range list, i.e. a range list
6390 -- representing all the values in TLo .. THi that are not in the input
6391 -- operand Right.
6393 function Build_Val (V : Uint) return Node_Id;
6394 -- Return an analyzed N_Identifier node referencing this value, suitable
6395 -- for use as an entry in the Static_Predicate list. This node is typed
6396 -- with the base type.
6398 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6399 -- Return an analyzed N_Range node referencing this range, suitable for
6400 -- use as an entry in the Static_Predicate list. This node is typed with
6401 -- the base type.
6403 function Get_RList (Exp : Node_Id) return RList;
6404 -- This is a recursive routine that converts the given expression into a
6405 -- list of ranges, suitable for use in building the static predicate.
6407 function Is_False (R : RList) return Boolean;
6408 pragma Inline (Is_False);
6409 -- Returns True if the given range list is empty, and thus represents a
6410 -- False list of ranges that can never be satisfied.
6412 function Is_True (R : RList) return Boolean;
6413 -- Returns True if R trivially represents the True predicate by having a
6414 -- single range from BLo to BHi.
6416 function Is_Type_Ref (N : Node_Id) return Boolean;
6417 pragma Inline (Is_Type_Ref);
6418 -- Returns if True if N is a reference to the type for the predicate in
6419 -- the expression (i.e. if it is an identifier whose Chars field matches
6420 -- the Nam given in the call).
6422 function Lo_Val (N : Node_Id) return Uint;
6423 -- Given static expression or static range from a Static_Predicate list,
6424 -- gets expression value or low bound of range.
6426 function Hi_Val (N : Node_Id) return Uint;
6427 -- Given static expression or static range from a Static_Predicate list,
6428 -- gets expression value of high bound of range.
6430 function Membership_Entry (N : Node_Id) return RList;
6431 -- Given a single membership entry (range, value, or subtype), returns
6432 -- the corresponding range list. Raises Static_Error if not static.
6434 function Membership_Entries (N : Node_Id) return RList;
6435 -- Given an element on an alternatives list of a membership operation,
6436 -- returns the range list corresponding to this entry and all following
6437 -- entries (i.e. returns the "or" of this list of values).
6439 function Stat_Pred (Typ : Entity_Id) return RList;
6440 -- Given a type, if it has a static predicate, then return the predicate
6441 -- as a range list, otherwise raise Non_Static.
6443 -----------
6444 -- "and" --
6445 -----------
6447 function "and" (Left : RList; Right : RList) return RList is
6448 FEnt : REnt;
6449 -- First range of result
6451 SLeft : Nat := Left'First;
6452 -- Start of rest of left entries
6454 SRight : Nat := Right'First;
6455 -- Start of rest of right entries
6457 begin
6458 -- If either range is True, return the other
6460 if Is_True (Left) then
6461 return Right;
6462 elsif Is_True (Right) then
6463 return Left;
6464 end if;
6466 -- If either range is False, return False
6468 if Is_False (Left) or else Is_False (Right) then
6469 return False_Range;
6470 end if;
6472 -- Loop to remove entries at start that are disjoint, and thus just
6473 -- get discarded from the result entirely.
6475 loop
6476 -- If no operands left in either operand, result is false
6478 if SLeft > Left'Last or else SRight > Right'Last then
6479 return False_Range;
6481 -- Discard first left operand entry if disjoint with right
6483 elsif Left (SLeft).Hi < Right (SRight).Lo then
6484 SLeft := SLeft + 1;
6486 -- Discard first right operand entry if disjoint with left
6488 elsif Right (SRight).Hi < Left (SLeft).Lo then
6489 SRight := SRight + 1;
6491 -- Otherwise we have an overlapping entry
6493 else
6494 exit;
6495 end if;
6496 end loop;
6498 -- Now we have two non-null operands, and first entries overlap. The
6499 -- first entry in the result will be the overlapping part of these
6500 -- two entries.
6502 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6503 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6505 -- Now we can remove the entry that ended at a lower value, since its
6506 -- contribution is entirely contained in Fent.
6508 if Left (SLeft).Hi <= Right (SRight).Hi then
6509 SLeft := SLeft + 1;
6510 else
6511 SRight := SRight + 1;
6512 end if;
6514 -- Compute result by concatenating this first entry with the "and" of
6515 -- the remaining parts of the left and right operands. Note that if
6516 -- either of these is empty, "and" will yield empty, so that we will
6517 -- end up with just Fent, which is what we want in that case.
6519 return
6520 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6521 end "and";
6523 -----------
6524 -- "not" --
6525 -----------
6527 function "not" (Right : RList) return RList is
6528 begin
6529 -- Return True if False range
6531 if Is_False (Right) then
6532 return True_Range;
6533 end if;
6535 -- Return False if True range
6537 if Is_True (Right) then
6538 return False_Range;
6539 end if;
6541 -- Here if not trivial case
6543 declare
6544 Result : RList (1 .. Right'Length + 1);
6545 -- May need one more entry for gap at beginning and end
6547 Count : Nat := 0;
6548 -- Number of entries stored in Result
6550 begin
6551 -- Gap at start
6553 if Right (Right'First).Lo > TLo then
6554 Count := Count + 1;
6555 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6556 end if;
6558 -- Gaps between ranges
6560 for J in Right'First .. Right'Last - 1 loop
6561 Count := Count + 1;
6562 Result (Count) :=
6563 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6564 end loop;
6566 -- Gap at end
6568 if Right (Right'Last).Hi < THi then
6569 Count := Count + 1;
6570 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6571 end if;
6573 return Result (1 .. Count);
6574 end;
6575 end "not";
6577 ----------
6578 -- "or" --
6579 ----------
6581 function "or" (Left : RList; Right : RList) return RList is
6582 FEnt : REnt;
6583 -- First range of result
6585 SLeft : Nat := Left'First;
6586 -- Start of rest of left entries
6588 SRight : Nat := Right'First;
6589 -- Start of rest of right entries
6591 begin
6592 -- If either range is True, return True
6594 if Is_True (Left) or else Is_True (Right) then
6595 return True_Range;
6596 end if;
6598 -- If either range is False (empty), return the other
6600 if Is_False (Left) then
6601 return Right;
6602 elsif Is_False (Right) then
6603 return Left;
6604 end if;
6606 -- Initialize result first entry from left or right operand depending
6607 -- on which starts with the lower range.
6609 if Left (SLeft).Lo < Right (SRight).Lo then
6610 FEnt := Left (SLeft);
6611 SLeft := SLeft + 1;
6612 else
6613 FEnt := Right (SRight);
6614 SRight := SRight + 1;
6615 end if;
6617 -- This loop eats ranges from left and right operands that are
6618 -- contiguous with the first range we are gathering.
6620 loop
6621 -- Eat first entry in left operand if contiguous or overlapped by
6622 -- gathered first operand of result.
6624 if SLeft <= Left'Last
6625 and then Left (SLeft).Lo <= FEnt.Hi + 1
6626 then
6627 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6628 SLeft := SLeft + 1;
6630 -- Eat first entry in right operand if contiguous or overlapped by
6631 -- gathered right operand of result.
6633 elsif SRight <= Right'Last
6634 and then Right (SRight).Lo <= FEnt.Hi + 1
6635 then
6636 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6637 SRight := SRight + 1;
6639 -- All done if no more entries to eat
6641 else
6642 exit;
6643 end if;
6644 end loop;
6646 -- Obtain result as the first entry we just computed, concatenated
6647 -- to the "or" of the remaining results (if one operand is empty,
6648 -- this will just concatenate with the other
6650 return
6651 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6652 end "or";
6654 -----------------
6655 -- Build_Range --
6656 -----------------
6658 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6659 Result : Node_Id;
6661 begin
6662 Result :=
6663 Make_Range (Loc,
6664 Low_Bound => Build_Val (Lo),
6665 High_Bound => Build_Val (Hi));
6666 Set_Etype (Result, Btyp);
6667 Set_Analyzed (Result);
6669 return Result;
6670 end Build_Range;
6672 ---------------
6673 -- Build_Val --
6674 ---------------
6676 function Build_Val (V : Uint) return Node_Id is
6677 Result : Node_Id;
6679 begin
6680 if Is_Enumeration_Type (Typ) then
6681 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6682 else
6683 Result := Make_Integer_Literal (Loc, V);
6684 end if;
6686 Set_Etype (Result, Btyp);
6687 Set_Is_Static_Expression (Result);
6688 Set_Analyzed (Result);
6689 return Result;
6690 end Build_Val;
6692 ---------------
6693 -- Get_RList --
6694 ---------------
6696 function Get_RList (Exp : Node_Id) return RList is
6697 Op : Node_Kind;
6698 Val : Uint;
6700 begin
6701 -- Static expression can only be true or false
6703 if Is_OK_Static_Expression (Exp) then
6705 -- For False
6707 if Expr_Value (Exp) = 0 then
6708 return False_Range;
6709 else
6710 return True_Range;
6711 end if;
6712 end if;
6714 -- Otherwise test node type
6716 Op := Nkind (Exp);
6718 case Op is
6720 -- And
6722 when N_Op_And | N_And_Then =>
6723 return Get_RList (Left_Opnd (Exp))
6725 Get_RList (Right_Opnd (Exp));
6727 -- Or
6729 when N_Op_Or | N_Or_Else =>
6730 return Get_RList (Left_Opnd (Exp))
6732 Get_RList (Right_Opnd (Exp));
6734 -- Not
6736 when N_Op_Not =>
6737 return not Get_RList (Right_Opnd (Exp));
6739 -- Comparisons of type with static value
6741 when N_Op_Compare =>
6743 -- Type is left operand
6745 if Is_Type_Ref (Left_Opnd (Exp))
6746 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6747 then
6748 Val := Expr_Value (Right_Opnd (Exp));
6750 -- Typ is right operand
6752 elsif Is_Type_Ref (Right_Opnd (Exp))
6753 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6754 then
6755 Val := Expr_Value (Left_Opnd (Exp));
6757 -- Invert sense of comparison
6759 case Op is
6760 when N_Op_Gt => Op := N_Op_Lt;
6761 when N_Op_Lt => Op := N_Op_Gt;
6762 when N_Op_Ge => Op := N_Op_Le;
6763 when N_Op_Le => Op := N_Op_Ge;
6764 when others => null;
6765 end case;
6767 -- Other cases are non-static
6769 else
6770 raise Non_Static;
6771 end if;
6773 -- Construct range according to comparison operation
6775 case Op is
6776 when N_Op_Eq =>
6777 return RList'(1 => REnt'(Val, Val));
6779 when N_Op_Ge =>
6780 return RList'(1 => REnt'(Val, BHi));
6782 when N_Op_Gt =>
6783 return RList'(1 => REnt'(Val + 1, BHi));
6785 when N_Op_Le =>
6786 return RList'(1 => REnt'(BLo, Val));
6788 when N_Op_Lt =>
6789 return RList'(1 => REnt'(BLo, Val - 1));
6791 when N_Op_Ne =>
6792 return RList'(REnt'(BLo, Val - 1),
6793 REnt'(Val + 1, BHi));
6795 when others =>
6796 raise Program_Error;
6797 end case;
6799 -- Membership (IN)
6801 when N_In =>
6802 if not Is_Type_Ref (Left_Opnd (Exp)) then
6803 raise Non_Static;
6804 end if;
6806 if Present (Right_Opnd (Exp)) then
6807 return Membership_Entry (Right_Opnd (Exp));
6808 else
6809 return Membership_Entries (First (Alternatives (Exp)));
6810 end if;
6812 -- Negative membership (NOT IN)
6814 when N_Not_In =>
6815 if not Is_Type_Ref (Left_Opnd (Exp)) then
6816 raise Non_Static;
6817 end if;
6819 if Present (Right_Opnd (Exp)) then
6820 return not Membership_Entry (Right_Opnd (Exp));
6821 else
6822 return not Membership_Entries (First (Alternatives (Exp)));
6823 end if;
6825 -- Function call, may be call to static predicate
6827 when N_Function_Call =>
6828 if Is_Entity_Name (Name (Exp)) then
6829 declare
6830 Ent : constant Entity_Id := Entity (Name (Exp));
6831 begin
6832 if Is_Predicate_Function (Ent)
6833 or else
6834 Is_Predicate_Function_M (Ent)
6835 then
6836 return Stat_Pred (Etype (First_Formal (Ent)));
6837 end if;
6838 end;
6839 end if;
6841 -- Other function call cases are non-static
6843 raise Non_Static;
6845 -- Qualified expression, dig out the expression
6847 when N_Qualified_Expression =>
6848 return Get_RList (Expression (Exp));
6850 -- Xor operator
6852 when N_Op_Xor =>
6853 return (Get_RList (Left_Opnd (Exp))
6854 and not Get_RList (Right_Opnd (Exp)))
6855 or (Get_RList (Right_Opnd (Exp))
6856 and not Get_RList (Left_Opnd (Exp)));
6858 -- Any other node type is non-static
6860 when others =>
6861 raise Non_Static;
6862 end case;
6863 end Get_RList;
6865 ------------
6866 -- Hi_Val --
6867 ------------
6869 function Hi_Val (N : Node_Id) return Uint is
6870 begin
6871 if Is_Static_Expression (N) then
6872 return Expr_Value (N);
6873 else
6874 pragma Assert (Nkind (N) = N_Range);
6875 return Expr_Value (High_Bound (N));
6876 end if;
6877 end Hi_Val;
6879 --------------
6880 -- Is_False --
6881 --------------
6883 function Is_False (R : RList) return Boolean is
6884 begin
6885 return R'Length = 0;
6886 end Is_False;
6888 -------------
6889 -- Is_True --
6890 -------------
6892 function Is_True (R : RList) return Boolean is
6893 begin
6894 return R'Length = 1
6895 and then R (R'First).Lo = BLo
6896 and then R (R'First).Hi = BHi;
6897 end Is_True;
6899 -----------------
6900 -- Is_Type_Ref --
6901 -----------------
6903 function Is_Type_Ref (N : Node_Id) return Boolean is
6904 begin
6905 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
6906 end Is_Type_Ref;
6908 ------------
6909 -- Lo_Val --
6910 ------------
6912 function Lo_Val (N : Node_Id) return Uint is
6913 begin
6914 if Is_Static_Expression (N) then
6915 return Expr_Value (N);
6916 else
6917 pragma Assert (Nkind (N) = N_Range);
6918 return Expr_Value (Low_Bound (N));
6919 end if;
6920 end Lo_Val;
6922 ------------------------
6923 -- Membership_Entries --
6924 ------------------------
6926 function Membership_Entries (N : Node_Id) return RList is
6927 begin
6928 if No (Next (N)) then
6929 return Membership_Entry (N);
6930 else
6931 return Membership_Entry (N) or Membership_Entries (Next (N));
6932 end if;
6933 end Membership_Entries;
6935 ----------------------
6936 -- Membership_Entry --
6937 ----------------------
6939 function Membership_Entry (N : Node_Id) return RList is
6940 Val : Uint;
6941 SLo : Uint;
6942 SHi : Uint;
6944 begin
6945 -- Range case
6947 if Nkind (N) = N_Range then
6948 if not Is_Static_Expression (Low_Bound (N))
6949 or else
6950 not Is_Static_Expression (High_Bound (N))
6951 then
6952 raise Non_Static;
6953 else
6954 SLo := Expr_Value (Low_Bound (N));
6955 SHi := Expr_Value (High_Bound (N));
6956 return RList'(1 => REnt'(SLo, SHi));
6957 end if;
6959 -- Static expression case
6961 elsif Is_Static_Expression (N) then
6962 Val := Expr_Value (N);
6963 return RList'(1 => REnt'(Val, Val));
6965 -- Identifier (other than static expression) case
6967 else pragma Assert (Nkind (N) = N_Identifier);
6969 -- Type case
6971 if Is_Type (Entity (N)) then
6973 -- If type has predicates, process them
6975 if Has_Predicates (Entity (N)) then
6976 return Stat_Pred (Entity (N));
6978 -- For static subtype without predicates, get range
6980 elsif Is_Static_Subtype (Entity (N)) then
6981 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
6982 SHi := Expr_Value (Type_High_Bound (Entity (N)));
6983 return RList'(1 => REnt'(SLo, SHi));
6985 -- Any other type makes us non-static
6987 else
6988 raise Non_Static;
6989 end if;
6991 -- Any other kind of identifier in predicate (e.g. a non-static
6992 -- expression value) means this is not a static predicate.
6994 else
6995 raise Non_Static;
6996 end if;
6997 end if;
6998 end Membership_Entry;
7000 ---------------
7001 -- Stat_Pred --
7002 ---------------
7004 function Stat_Pred (Typ : Entity_Id) return RList is
7005 begin
7006 -- Not static if type does not have static predicates
7008 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
7009 raise Non_Static;
7010 end if;
7012 -- Otherwise we convert the predicate list to a range list
7014 declare
7015 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
7016 P : Node_Id;
7018 begin
7019 P := First (Static_Predicate (Typ));
7020 for J in Result'Range loop
7021 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7022 Next (P);
7023 end loop;
7025 return Result;
7026 end;
7027 end Stat_Pred;
7029 -- Start of processing for Build_Static_Predicate
7031 begin
7032 -- Now analyze the expression to see if it is a static predicate
7034 declare
7035 Ranges : constant RList := Get_RList (Expr);
7036 -- Range list from expression if it is static
7038 Plist : List_Id;
7040 begin
7041 -- Convert range list into a form for the static predicate. In the
7042 -- Ranges array, we just have raw ranges, these must be converted
7043 -- to properly typed and analyzed static expressions or range nodes.
7045 -- Note: here we limit ranges to the ranges of the subtype, so that
7046 -- a predicate is always false for values outside the subtype. That
7047 -- seems fine, such values are invalid anyway, and considering them
7048 -- to fail the predicate seems allowed and friendly, and furthermore
7049 -- simplifies processing for case statements and loops.
7051 Plist := New_List;
7053 for J in Ranges'Range loop
7054 declare
7055 Lo : Uint := Ranges (J).Lo;
7056 Hi : Uint := Ranges (J).Hi;
7058 begin
7059 -- Ignore completely out of range entry
7061 if Hi < TLo or else Lo > THi then
7062 null;
7064 -- Otherwise process entry
7066 else
7067 -- Adjust out of range value to subtype range
7069 if Lo < TLo then
7070 Lo := TLo;
7071 end if;
7073 if Hi > THi then
7074 Hi := THi;
7075 end if;
7077 -- Convert range into required form
7079 Append_To (Plist, Build_Range (Lo, Hi));
7080 end if;
7081 end;
7082 end loop;
7084 -- Processing was successful and all entries were static, so now we
7085 -- can store the result as the predicate list.
7087 Set_Static_Predicate (Typ, Plist);
7089 -- The processing for static predicates put the expression into
7090 -- canonical form as a series of ranges. It also eliminated
7091 -- duplicates and collapsed and combined ranges. We might as well
7092 -- replace the alternatives list of the right operand of the
7093 -- membership test with the static predicate list, which will
7094 -- usually be more efficient.
7096 declare
7097 New_Alts : constant List_Id := New_List;
7098 Old_Node : Node_Id;
7099 New_Node : Node_Id;
7101 begin
7102 Old_Node := First (Plist);
7103 while Present (Old_Node) loop
7104 New_Node := New_Copy (Old_Node);
7106 if Nkind (New_Node) = N_Range then
7107 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7108 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7109 end if;
7111 Append_To (New_Alts, New_Node);
7112 Next (Old_Node);
7113 end loop;
7115 -- If empty list, replace by False
7117 if Is_Empty_List (New_Alts) then
7118 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7120 -- Else replace by set membership test
7122 else
7123 Rewrite (Expr,
7124 Make_In (Loc,
7125 Left_Opnd => Make_Identifier (Loc, Nam),
7126 Right_Opnd => Empty,
7127 Alternatives => New_Alts));
7129 -- Resolve new expression in function context
7131 Install_Formals (Predicate_Function (Typ));
7132 Push_Scope (Predicate_Function (Typ));
7133 Analyze_And_Resolve (Expr, Standard_Boolean);
7134 Pop_Scope;
7135 end if;
7136 end;
7137 end;
7139 -- If non-static, return doing nothing
7141 exception
7142 when Non_Static =>
7143 return;
7144 end Build_Static_Predicate;
7146 -----------------------------------------
7147 -- Check_Aspect_At_End_Of_Declarations --
7148 -----------------------------------------
7150 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
7151 Ent : constant Entity_Id := Entity (ASN);
7152 Ident : constant Node_Id := Identifier (ASN);
7153 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7155 End_Decl_Expr : constant Node_Id := Entity (Ident);
7156 -- Expression to be analyzed at end of declarations
7158 Freeze_Expr : constant Node_Id := Expression (ASN);
7159 -- Expression from call to Check_Aspect_At_Freeze_Point
7161 T : constant Entity_Id := Etype (Freeze_Expr);
7162 -- Type required for preanalyze call
7164 Err : Boolean;
7165 -- Set False if error
7167 -- On entry to this procedure, Entity (Ident) contains a copy of the
7168 -- original expression from the aspect, saved for this purpose, and
7169 -- but Expression (Ident) is a preanalyzed copy of the expression,
7170 -- preanalyzed just after the freeze point.
7172 procedure Check_Overloaded_Name;
7173 -- For aspects whose expression is simply a name, this routine checks if
7174 -- the name is overloaded or not. If so, it verifies there is an
7175 -- interpretation that matches the entity obtained at the freeze point,
7176 -- otherwise the compiler complains.
7178 ---------------------------
7179 -- Check_Overloaded_Name --
7180 ---------------------------
7182 procedure Check_Overloaded_Name is
7183 begin
7184 if not Is_Overloaded (End_Decl_Expr) then
7185 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
7187 else
7188 Err := True;
7190 declare
7191 Index : Interp_Index;
7192 It : Interp;
7194 begin
7195 Get_First_Interp (End_Decl_Expr, Index, It);
7196 while Present (It.Typ) loop
7197 if It.Nam = Entity (Freeze_Expr) then
7198 Err := False;
7199 exit;
7200 end if;
7202 Get_Next_Interp (Index, It);
7203 end loop;
7204 end;
7205 end if;
7206 end Check_Overloaded_Name;
7208 -- Start of processing for Check_Aspect_At_End_Of_Declarations
7210 begin
7211 -- Case of aspects Dimension, Dimension_System and Synchronization
7213 if A_Id = Aspect_Synchronization then
7214 return;
7216 -- Case of stream attributes, just have to compare entities. However,
7217 -- the expression is just a name (possibly overloaded), and there may
7218 -- be stream operations declared for unrelated types, so we just need
7219 -- to verify that one of these interpretations is the one available at
7220 -- at the freeze point.
7222 elsif A_Id = Aspect_Input or else
7223 A_Id = Aspect_Output or else
7224 A_Id = Aspect_Read or else
7225 A_Id = Aspect_Write
7226 then
7227 Analyze (End_Decl_Expr);
7228 Check_Overloaded_Name;
7230 elsif A_Id = Aspect_Variable_Indexing or else
7231 A_Id = Aspect_Constant_Indexing or else
7232 A_Id = Aspect_Default_Iterator or else
7233 A_Id = Aspect_Iterator_Element
7234 then
7235 -- Make type unfrozen before analysis, to prevent spurious errors
7236 -- about late attributes.
7238 Set_Is_Frozen (Ent, False);
7239 Analyze (End_Decl_Expr);
7240 Set_Is_Frozen (Ent, True);
7242 -- If the end of declarations comes before any other freeze
7243 -- point, the Freeze_Expr is not analyzed: no check needed.
7245 if Analyzed (Freeze_Expr) and then not In_Instance then
7246 Check_Overloaded_Name;
7247 else
7248 Err := False;
7249 end if;
7251 -- All other cases
7253 else
7254 -- In a generic context the aspect expressions have not been
7255 -- preanalyzed, so do it now. There are no conformance checks
7256 -- to perform in this case.
7258 if No (T) then
7259 Check_Aspect_At_Freeze_Point (ASN);
7260 return;
7262 -- The default values attributes may be defined in the private part,
7263 -- and the analysis of the expression may take place when only the
7264 -- partial view is visible. The expression must be scalar, so use
7265 -- the full view to resolve.
7267 elsif (A_Id = Aspect_Default_Value
7268 or else
7269 A_Id = Aspect_Default_Component_Value)
7270 and then Is_Private_Type (T)
7271 then
7272 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
7273 else
7274 Preanalyze_Spec_Expression (End_Decl_Expr, T);
7275 end if;
7277 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
7278 end if;
7280 -- Output error message if error
7282 if Err then
7283 Error_Msg_NE
7284 ("visibility of aspect for& changes after freeze point",
7285 ASN, Ent);
7286 Error_Msg_NE
7287 ("info: & is frozen here, aspects evaluated at this point??",
7288 Freeze_Node (Ent), Ent);
7289 end if;
7290 end Check_Aspect_At_End_Of_Declarations;
7292 ----------------------------------
7293 -- Check_Aspect_At_Freeze_Point --
7294 ----------------------------------
7296 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
7297 Ident : constant Node_Id := Identifier (ASN);
7298 -- Identifier (use Entity field to save expression)
7300 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7302 T : Entity_Id := Empty;
7303 -- Type required for preanalyze call
7305 begin
7306 -- On entry to this procedure, Entity (Ident) contains a copy of the
7307 -- original expression from the aspect, saved for this purpose.
7309 -- On exit from this procedure Entity (Ident) is unchanged, still
7310 -- containing that copy, but Expression (Ident) is a preanalyzed copy
7311 -- of the expression, preanalyzed just after the freeze point.
7313 -- Make a copy of the expression to be preanalyzed
7315 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
7317 -- Find type for preanalyze call
7319 case A_Id is
7321 -- No_Aspect should be impossible
7323 when No_Aspect =>
7324 raise Program_Error;
7326 -- Aspects taking an optional boolean argument
7328 when Boolean_Aspects |
7329 Library_Unit_Aspects =>
7330 T := Standard_Boolean;
7332 -- Aspects corresponding to attribute definition clauses
7334 when Aspect_Address =>
7335 T := RTE (RE_Address);
7337 when Aspect_Attach_Handler =>
7338 T := RTE (RE_Interrupt_ID);
7340 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
7341 T := RTE (RE_Bit_Order);
7343 when Aspect_Convention =>
7344 return;
7346 when Aspect_CPU =>
7347 T := RTE (RE_CPU_Range);
7349 -- Default_Component_Value is resolved with the component type
7351 when Aspect_Default_Component_Value =>
7352 T := Component_Type (Entity (ASN));
7354 -- Default_Value is resolved with the type entity in question
7356 when Aspect_Default_Value =>
7357 T := Entity (ASN);
7359 -- Depends is a delayed aspect because it mentiones names first
7360 -- introduced by aspect Global which is already delayed. There is
7361 -- no action to be taken with respect to the aspect itself as the
7362 -- analysis is done by the corresponding pragma.
7364 when Aspect_Depends =>
7365 return;
7367 when Aspect_Dispatching_Domain =>
7368 T := RTE (RE_Dispatching_Domain);
7370 when Aspect_External_Tag =>
7371 T := Standard_String;
7373 when Aspect_External_Name =>
7374 T := Standard_String;
7376 -- Global is a delayed aspect because it may reference names that
7377 -- have not been declared yet. There is no action to be taken with
7378 -- respect to the aspect itself as the reference checking is done
7379 -- on the corresponding pragma.
7381 when Aspect_Global =>
7382 return;
7384 when Aspect_Link_Name =>
7385 T := Standard_String;
7387 when Aspect_Priority | Aspect_Interrupt_Priority =>
7388 T := Standard_Integer;
7390 when Aspect_Relative_Deadline =>
7391 T := RTE (RE_Time_Span);
7393 when Aspect_Small =>
7394 T := Universal_Real;
7396 -- For a simple storage pool, we have to retrieve the type of the
7397 -- pool object associated with the aspect's corresponding attribute
7398 -- definition clause.
7400 when Aspect_Simple_Storage_Pool =>
7401 T := Etype (Expression (Aspect_Rep_Item (ASN)));
7403 when Aspect_Storage_Pool =>
7404 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
7406 when Aspect_Alignment |
7407 Aspect_Component_Size |
7408 Aspect_Machine_Radix |
7409 Aspect_Object_Size |
7410 Aspect_Size |
7411 Aspect_Storage_Size |
7412 Aspect_Stream_Size |
7413 Aspect_Value_Size =>
7414 T := Any_Integer;
7416 when Aspect_Synchronization =>
7417 return;
7419 -- Special case, the expression of these aspects is just an entity
7420 -- that does not need any resolution, so just analyze.
7422 when Aspect_Input |
7423 Aspect_Output |
7424 Aspect_Read |
7425 Aspect_Suppress |
7426 Aspect_Unsuppress |
7427 Aspect_Warnings |
7428 Aspect_Write =>
7429 Analyze (Expression (ASN));
7430 return;
7432 -- Same for Iterator aspects, where the expression is a function
7433 -- name. Legality rules are checked separately.
7435 when Aspect_Constant_Indexing |
7436 Aspect_Default_Iterator |
7437 Aspect_Iterator_Element |
7438 Aspect_Variable_Indexing =>
7439 Analyze (Expression (ASN));
7440 return;
7442 -- Invariant/Predicate take boolean expressions
7444 when Aspect_Dynamic_Predicate |
7445 Aspect_Invariant |
7446 Aspect_Predicate |
7447 Aspect_Static_Predicate |
7448 Aspect_Type_Invariant =>
7449 T := Standard_Boolean;
7451 -- Here is the list of aspects that don't require delay analysis
7453 when Aspect_Abstract_State |
7454 Aspect_Contract_Cases |
7455 Aspect_Dimension |
7456 Aspect_Dimension_System |
7457 Aspect_Implicit_Dereference |
7458 Aspect_Post |
7459 Aspect_Postcondition |
7460 Aspect_Pre |
7461 Aspect_Precondition |
7462 Aspect_SPARK_Mode |
7463 Aspect_Test_Case =>
7464 raise Program_Error;
7466 end case;
7468 -- Do the preanalyze call
7470 Preanalyze_Spec_Expression (Expression (ASN), T);
7471 end Check_Aspect_At_Freeze_Point;
7473 -----------------------------------
7474 -- Check_Constant_Address_Clause --
7475 -----------------------------------
7477 procedure Check_Constant_Address_Clause
7478 (Expr : Node_Id;
7479 U_Ent : Entity_Id)
7481 procedure Check_At_Constant_Address (Nod : Node_Id);
7482 -- Checks that the given node N represents a name whose 'Address is
7483 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
7484 -- address value is the same at the point of declaration of U_Ent and at
7485 -- the time of elaboration of the address clause.
7487 procedure Check_Expr_Constants (Nod : Node_Id);
7488 -- Checks that Nod meets the requirements for a constant address clause
7489 -- in the sense of the enclosing procedure.
7491 procedure Check_List_Constants (Lst : List_Id);
7492 -- Check that all elements of list Lst meet the requirements for a
7493 -- constant address clause in the sense of the enclosing procedure.
7495 -------------------------------
7496 -- Check_At_Constant_Address --
7497 -------------------------------
7499 procedure Check_At_Constant_Address (Nod : Node_Id) is
7500 begin
7501 if Is_Entity_Name (Nod) then
7502 if Present (Address_Clause (Entity ((Nod)))) then
7503 Error_Msg_NE
7504 ("invalid address clause for initialized object &!",
7505 Nod, U_Ent);
7506 Error_Msg_NE
7507 ("address for& cannot" &
7508 " depend on another address clause! (RM 13.1(22))!",
7509 Nod, U_Ent);
7511 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
7512 and then Sloc (U_Ent) < Sloc (Entity (Nod))
7513 then
7514 Error_Msg_NE
7515 ("invalid address clause for initialized object &!",
7516 Nod, U_Ent);
7517 Error_Msg_Node_2 := U_Ent;
7518 Error_Msg_NE
7519 ("\& must be defined before & (RM 13.1(22))!",
7520 Nod, Entity (Nod));
7521 end if;
7523 elsif Nkind (Nod) = N_Selected_Component then
7524 declare
7525 T : constant Entity_Id := Etype (Prefix (Nod));
7527 begin
7528 if (Is_Record_Type (T)
7529 and then Has_Discriminants (T))
7530 or else
7531 (Is_Access_Type (T)
7532 and then Is_Record_Type (Designated_Type (T))
7533 and then Has_Discriminants (Designated_Type (T)))
7534 then
7535 Error_Msg_NE
7536 ("invalid address clause for initialized object &!",
7537 Nod, U_Ent);
7538 Error_Msg_N
7539 ("\address cannot depend on component" &
7540 " of discriminated record (RM 13.1(22))!",
7541 Nod);
7542 else
7543 Check_At_Constant_Address (Prefix (Nod));
7544 end if;
7545 end;
7547 elsif Nkind (Nod) = N_Indexed_Component then
7548 Check_At_Constant_Address (Prefix (Nod));
7549 Check_List_Constants (Expressions (Nod));
7551 else
7552 Check_Expr_Constants (Nod);
7553 end if;
7554 end Check_At_Constant_Address;
7556 --------------------------
7557 -- Check_Expr_Constants --
7558 --------------------------
7560 procedure Check_Expr_Constants (Nod : Node_Id) is
7561 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
7562 Ent : Entity_Id := Empty;
7564 begin
7565 if Nkind (Nod) in N_Has_Etype
7566 and then Etype (Nod) = Any_Type
7567 then
7568 return;
7569 end if;
7571 case Nkind (Nod) is
7572 when N_Empty | N_Error =>
7573 return;
7575 when N_Identifier | N_Expanded_Name =>
7576 Ent := Entity (Nod);
7578 -- We need to look at the original node if it is different
7579 -- from the node, since we may have rewritten things and
7580 -- substituted an identifier representing the rewrite.
7582 if Original_Node (Nod) /= Nod then
7583 Check_Expr_Constants (Original_Node (Nod));
7585 -- If the node is an object declaration without initial
7586 -- value, some code has been expanded, and the expression
7587 -- is not constant, even if the constituents might be
7588 -- acceptable, as in A'Address + offset.
7590 if Ekind (Ent) = E_Variable
7591 and then
7592 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
7593 and then
7594 No (Expression (Declaration_Node (Ent)))
7595 then
7596 Error_Msg_NE
7597 ("invalid address clause for initialized object &!",
7598 Nod, U_Ent);
7600 -- If entity is constant, it may be the result of expanding
7601 -- a check. We must verify that its declaration appears
7602 -- before the object in question, else we also reject the
7603 -- address clause.
7605 elsif Ekind (Ent) = E_Constant
7606 and then In_Same_Source_Unit (Ent, U_Ent)
7607 and then Sloc (Ent) > Loc_U_Ent
7608 then
7609 Error_Msg_NE
7610 ("invalid address clause for initialized object &!",
7611 Nod, U_Ent);
7612 end if;
7614 return;
7615 end if;
7617 -- Otherwise look at the identifier and see if it is OK
7619 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
7620 or else Is_Type (Ent)
7621 then
7622 return;
7624 elsif
7625 Ekind (Ent) = E_Constant
7626 or else
7627 Ekind (Ent) = E_In_Parameter
7628 then
7629 -- This is the case where we must have Ent defined before
7630 -- U_Ent. Clearly if they are in different units this
7631 -- requirement is met since the unit containing Ent is
7632 -- already processed.
7634 if not In_Same_Source_Unit (Ent, U_Ent) then
7635 return;
7637 -- Otherwise location of Ent must be before the location
7638 -- of U_Ent, that's what prior defined means.
7640 elsif Sloc (Ent) < Loc_U_Ent then
7641 return;
7643 else
7644 Error_Msg_NE
7645 ("invalid address clause for initialized object &!",
7646 Nod, U_Ent);
7647 Error_Msg_Node_2 := U_Ent;
7648 Error_Msg_NE
7649 ("\& must be defined before & (RM 13.1(22))!",
7650 Nod, Ent);
7651 end if;
7653 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
7654 Check_Expr_Constants (Original_Node (Nod));
7656 else
7657 Error_Msg_NE
7658 ("invalid address clause for initialized object &!",
7659 Nod, U_Ent);
7661 if Comes_From_Source (Ent) then
7662 Error_Msg_NE
7663 ("\reference to variable& not allowed"
7664 & " (RM 13.1(22))!", Nod, Ent);
7665 else
7666 Error_Msg_N
7667 ("non-static expression not allowed"
7668 & " (RM 13.1(22))!", Nod);
7669 end if;
7670 end if;
7672 when N_Integer_Literal =>
7674 -- If this is a rewritten unchecked conversion, in a system
7675 -- where Address is an integer type, always use the base type
7676 -- for a literal value. This is user-friendly and prevents
7677 -- order-of-elaboration issues with instances of unchecked
7678 -- conversion.
7680 if Nkind (Original_Node (Nod)) = N_Function_Call then
7681 Set_Etype (Nod, Base_Type (Etype (Nod)));
7682 end if;
7684 when N_Real_Literal |
7685 N_String_Literal |
7686 N_Character_Literal =>
7687 return;
7689 when N_Range =>
7690 Check_Expr_Constants (Low_Bound (Nod));
7691 Check_Expr_Constants (High_Bound (Nod));
7693 when N_Explicit_Dereference =>
7694 Check_Expr_Constants (Prefix (Nod));
7696 when N_Indexed_Component =>
7697 Check_Expr_Constants (Prefix (Nod));
7698 Check_List_Constants (Expressions (Nod));
7700 when N_Slice =>
7701 Check_Expr_Constants (Prefix (Nod));
7702 Check_Expr_Constants (Discrete_Range (Nod));
7704 when N_Selected_Component =>
7705 Check_Expr_Constants (Prefix (Nod));
7707 when N_Attribute_Reference =>
7708 if Nam_In (Attribute_Name (Nod), Name_Address,
7709 Name_Access,
7710 Name_Unchecked_Access,
7711 Name_Unrestricted_Access)
7712 then
7713 Check_At_Constant_Address (Prefix (Nod));
7715 else
7716 Check_Expr_Constants (Prefix (Nod));
7717 Check_List_Constants (Expressions (Nod));
7718 end if;
7720 when N_Aggregate =>
7721 Check_List_Constants (Component_Associations (Nod));
7722 Check_List_Constants (Expressions (Nod));
7724 when N_Component_Association =>
7725 Check_Expr_Constants (Expression (Nod));
7727 when N_Extension_Aggregate =>
7728 Check_Expr_Constants (Ancestor_Part (Nod));
7729 Check_List_Constants (Component_Associations (Nod));
7730 Check_List_Constants (Expressions (Nod));
7732 when N_Null =>
7733 return;
7735 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
7736 Check_Expr_Constants (Left_Opnd (Nod));
7737 Check_Expr_Constants (Right_Opnd (Nod));
7739 when N_Unary_Op =>
7740 Check_Expr_Constants (Right_Opnd (Nod));
7742 when N_Type_Conversion |
7743 N_Qualified_Expression |
7744 N_Allocator |
7745 N_Unchecked_Type_Conversion =>
7746 Check_Expr_Constants (Expression (Nod));
7748 when N_Function_Call =>
7749 if not Is_Pure (Entity (Name (Nod))) then
7750 Error_Msg_NE
7751 ("invalid address clause for initialized object &!",
7752 Nod, U_Ent);
7754 Error_Msg_NE
7755 ("\function & is not pure (RM 13.1(22))!",
7756 Nod, Entity (Name (Nod)));
7758 else
7759 Check_List_Constants (Parameter_Associations (Nod));
7760 end if;
7762 when N_Parameter_Association =>
7763 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7765 when others =>
7766 Error_Msg_NE
7767 ("invalid address clause for initialized object &!",
7768 Nod, U_Ent);
7769 Error_Msg_NE
7770 ("\must be constant defined before& (RM 13.1(22))!",
7771 Nod, U_Ent);
7772 end case;
7773 end Check_Expr_Constants;
7775 --------------------------
7776 -- Check_List_Constants --
7777 --------------------------
7779 procedure Check_List_Constants (Lst : List_Id) is
7780 Nod1 : Node_Id;
7782 begin
7783 if Present (Lst) then
7784 Nod1 := First (Lst);
7785 while Present (Nod1) loop
7786 Check_Expr_Constants (Nod1);
7787 Next (Nod1);
7788 end loop;
7789 end if;
7790 end Check_List_Constants;
7792 -- Start of processing for Check_Constant_Address_Clause
7794 begin
7795 -- If rep_clauses are to be ignored, no need for legality checks. In
7796 -- particular, no need to pester user about rep clauses that violate
7797 -- the rule on constant addresses, given that these clauses will be
7798 -- removed by Freeze before they reach the back end.
7800 if not Ignore_Rep_Clauses then
7801 Check_Expr_Constants (Expr);
7802 end if;
7803 end Check_Constant_Address_Clause;
7805 ----------------------------------------
7806 -- Check_Record_Representation_Clause --
7807 ----------------------------------------
7809 procedure Check_Record_Representation_Clause (N : Node_Id) is
7810 Loc : constant Source_Ptr := Sloc (N);
7811 Ident : constant Node_Id := Identifier (N);
7812 Rectype : Entity_Id;
7813 Fent : Entity_Id;
7814 CC : Node_Id;
7815 Fbit : Uint;
7816 Lbit : Uint;
7817 Hbit : Uint := Uint_0;
7818 Comp : Entity_Id;
7819 Pcomp : Entity_Id;
7821 Max_Bit_So_Far : Uint;
7822 -- Records the maximum bit position so far. If all field positions
7823 -- are monotonically increasing, then we can skip the circuit for
7824 -- checking for overlap, since no overlap is possible.
7826 Tagged_Parent : Entity_Id := Empty;
7827 -- This is set in the case of a derived tagged type for which we have
7828 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
7829 -- positioned by record representation clauses). In this case we must
7830 -- check for overlap between components of this tagged type, and the
7831 -- components of its parent. Tagged_Parent will point to this parent
7832 -- type. For all other cases Tagged_Parent is left set to Empty.
7834 Parent_Last_Bit : Uint;
7835 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
7836 -- last bit position for any field in the parent type. We only need to
7837 -- check overlap for fields starting below this point.
7839 Overlap_Check_Required : Boolean;
7840 -- Used to keep track of whether or not an overlap check is required
7842 Overlap_Detected : Boolean := False;
7843 -- Set True if an overlap is detected
7845 Ccount : Natural := 0;
7846 -- Number of component clauses in record rep clause
7848 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
7849 -- Given two entities for record components or discriminants, checks
7850 -- if they have overlapping component clauses and issues errors if so.
7852 procedure Find_Component;
7853 -- Finds component entity corresponding to current component clause (in
7854 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
7855 -- start/stop bits for the field. If there is no matching component or
7856 -- if the matching component does not have a component clause, then
7857 -- that's an error and Comp is set to Empty, but no error message is
7858 -- issued, since the message was already given. Comp is also set to
7859 -- Empty if the current "component clause" is in fact a pragma.
7861 -----------------------------
7862 -- Check_Component_Overlap --
7863 -----------------------------
7865 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
7866 CC1 : constant Node_Id := Component_Clause (C1_Ent);
7867 CC2 : constant Node_Id := Component_Clause (C2_Ent);
7869 begin
7870 if Present (CC1) and then Present (CC2) then
7872 -- Exclude odd case where we have two tag components in the same
7873 -- record, both at location zero. This seems a bit strange, but
7874 -- it seems to happen in some circumstances, perhaps on an error.
7876 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
7877 return;
7878 end if;
7880 -- Here we check if the two fields overlap
7882 declare
7883 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
7884 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
7885 E1 : constant Uint := S1 + Esize (C1_Ent);
7886 E2 : constant Uint := S2 + Esize (C2_Ent);
7888 begin
7889 if E2 <= S1 or else E1 <= S2 then
7890 null;
7891 else
7892 Error_Msg_Node_2 := Component_Name (CC2);
7893 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7894 Error_Msg_Node_1 := Component_Name (CC1);
7895 Error_Msg_N
7896 ("component& overlaps & #", Component_Name (CC1));
7897 Overlap_Detected := True;
7898 end if;
7899 end;
7900 end if;
7901 end Check_Component_Overlap;
7903 --------------------
7904 -- Find_Component --
7905 --------------------
7907 procedure Find_Component is
7909 procedure Search_Component (R : Entity_Id);
7910 -- Search components of R for a match. If found, Comp is set
7912 ----------------------
7913 -- Search_Component --
7914 ----------------------
7916 procedure Search_Component (R : Entity_Id) is
7917 begin
7918 Comp := First_Component_Or_Discriminant (R);
7919 while Present (Comp) loop
7921 -- Ignore error of attribute name for component name (we
7922 -- already gave an error message for this, so no need to
7923 -- complain here)
7925 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
7926 null;
7927 else
7928 exit when Chars (Comp) = Chars (Component_Name (CC));
7929 end if;
7931 Next_Component_Or_Discriminant (Comp);
7932 end loop;
7933 end Search_Component;
7935 -- Start of processing for Find_Component
7937 begin
7938 -- Return with Comp set to Empty if we have a pragma
7940 if Nkind (CC) = N_Pragma then
7941 Comp := Empty;
7942 return;
7943 end if;
7945 -- Search current record for matching component
7947 Search_Component (Rectype);
7949 -- If not found, maybe component of base type discriminant that is
7950 -- absent from statically constrained first subtype.
7952 if No (Comp) then
7953 Search_Component (Base_Type (Rectype));
7954 end if;
7956 -- If no component, or the component does not reference the component
7957 -- clause in question, then there was some previous error for which
7958 -- we already gave a message, so just return with Comp Empty.
7960 if No (Comp) or else Component_Clause (Comp) /= CC then
7961 Check_Error_Detected;
7962 Comp := Empty;
7964 -- Normal case where we have a component clause
7966 else
7967 Fbit := Component_Bit_Offset (Comp);
7968 Lbit := Fbit + Esize (Comp) - 1;
7969 end if;
7970 end Find_Component;
7972 -- Start of processing for Check_Record_Representation_Clause
7974 begin
7975 Find_Type (Ident);
7976 Rectype := Entity (Ident);
7978 if Rectype = Any_Type then
7979 return;
7980 else
7981 Rectype := Underlying_Type (Rectype);
7982 end if;
7984 -- See if we have a fully repped derived tagged type
7986 declare
7987 PS : constant Entity_Id := Parent_Subtype (Rectype);
7989 begin
7990 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
7991 Tagged_Parent := PS;
7993 -- Find maximum bit of any component of the parent type
7995 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
7996 Pcomp := First_Entity (Tagged_Parent);
7997 while Present (Pcomp) loop
7998 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
7999 if Component_Bit_Offset (Pcomp) /= No_Uint
8000 and then Known_Static_Esize (Pcomp)
8001 then
8002 Parent_Last_Bit :=
8003 UI_Max
8004 (Parent_Last_Bit,
8005 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
8006 end if;
8008 Next_Entity (Pcomp);
8009 end if;
8010 end loop;
8011 end if;
8012 end;
8014 -- All done if no component clauses
8016 CC := First (Component_Clauses (N));
8018 if No (CC) then
8019 return;
8020 end if;
8022 -- If a tag is present, then create a component clause that places it
8023 -- at the start of the record (otherwise gigi may place it after other
8024 -- fields that have rep clauses).
8026 Fent := First_Entity (Rectype);
8028 if Nkind (Fent) = N_Defining_Identifier
8029 and then Chars (Fent) = Name_uTag
8030 then
8031 Set_Component_Bit_Offset (Fent, Uint_0);
8032 Set_Normalized_Position (Fent, Uint_0);
8033 Set_Normalized_First_Bit (Fent, Uint_0);
8034 Set_Normalized_Position_Max (Fent, Uint_0);
8035 Init_Esize (Fent, System_Address_Size);
8037 Set_Component_Clause (Fent,
8038 Make_Component_Clause (Loc,
8039 Component_Name => Make_Identifier (Loc, Name_uTag),
8041 Position => Make_Integer_Literal (Loc, Uint_0),
8042 First_Bit => Make_Integer_Literal (Loc, Uint_0),
8043 Last_Bit =>
8044 Make_Integer_Literal (Loc,
8045 UI_From_Int (System_Address_Size))));
8047 Ccount := Ccount + 1;
8048 end if;
8050 Max_Bit_So_Far := Uint_Minus_1;
8051 Overlap_Check_Required := False;
8053 -- Process the component clauses
8055 while Present (CC) loop
8056 Find_Component;
8058 if Present (Comp) then
8059 Ccount := Ccount + 1;
8061 -- We need a full overlap check if record positions non-monotonic
8063 if Fbit <= Max_Bit_So_Far then
8064 Overlap_Check_Required := True;
8065 end if;
8067 Max_Bit_So_Far := Lbit;
8069 -- Check bit position out of range of specified size
8071 if Has_Size_Clause (Rectype)
8072 and then RM_Size (Rectype) <= Lbit
8073 then
8074 Error_Msg_N
8075 ("bit number out of range of specified size",
8076 Last_Bit (CC));
8078 -- Check for overlap with tag component
8080 else
8081 if Is_Tagged_Type (Rectype)
8082 and then Fbit < System_Address_Size
8083 then
8084 Error_Msg_NE
8085 ("component overlaps tag field of&",
8086 Component_Name (CC), Rectype);
8087 Overlap_Detected := True;
8088 end if;
8090 if Hbit < Lbit then
8091 Hbit := Lbit;
8092 end if;
8093 end if;
8095 -- Check parent overlap if component might overlap parent field
8097 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
8098 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
8099 while Present (Pcomp) loop
8100 if not Is_Tag (Pcomp)
8101 and then Chars (Pcomp) /= Name_uParent
8102 then
8103 Check_Component_Overlap (Comp, Pcomp);
8104 end if;
8106 Next_Component_Or_Discriminant (Pcomp);
8107 end loop;
8108 end if;
8109 end if;
8111 Next (CC);
8112 end loop;
8114 -- Now that we have processed all the component clauses, check for
8115 -- overlap. We have to leave this till last, since the components can
8116 -- appear in any arbitrary order in the representation clause.
8118 -- We do not need this check if all specified ranges were monotonic,
8119 -- as recorded by Overlap_Check_Required being False at this stage.
8121 -- This first section checks if there are any overlapping entries at
8122 -- all. It does this by sorting all entries and then seeing if there are
8123 -- any overlaps. If there are none, then that is decisive, but if there
8124 -- are overlaps, they may still be OK (they may result from fields in
8125 -- different variants).
8127 if Overlap_Check_Required then
8128 Overlap_Check1 : declare
8130 OC_Fbit : array (0 .. Ccount) of Uint;
8131 -- First-bit values for component clauses, the value is the offset
8132 -- of the first bit of the field from start of record. The zero
8133 -- entry is for use in sorting.
8135 OC_Lbit : array (0 .. Ccount) of Uint;
8136 -- Last-bit values for component clauses, the value is the offset
8137 -- of the last bit of the field from start of record. The zero
8138 -- entry is for use in sorting.
8140 OC_Count : Natural := 0;
8141 -- Count of entries in OC_Fbit and OC_Lbit
8143 function OC_Lt (Op1, Op2 : Natural) return Boolean;
8144 -- Compare routine for Sort
8146 procedure OC_Move (From : Natural; To : Natural);
8147 -- Move routine for Sort
8149 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
8151 -----------
8152 -- OC_Lt --
8153 -----------
8155 function OC_Lt (Op1, Op2 : Natural) return Boolean is
8156 begin
8157 return OC_Fbit (Op1) < OC_Fbit (Op2);
8158 end OC_Lt;
8160 -------------
8161 -- OC_Move --
8162 -------------
8164 procedure OC_Move (From : Natural; To : Natural) is
8165 begin
8166 OC_Fbit (To) := OC_Fbit (From);
8167 OC_Lbit (To) := OC_Lbit (From);
8168 end OC_Move;
8170 -- Start of processing for Overlap_Check
8172 begin
8173 CC := First (Component_Clauses (N));
8174 while Present (CC) loop
8176 -- Exclude component clause already marked in error
8178 if not Error_Posted (CC) then
8179 Find_Component;
8181 if Present (Comp) then
8182 OC_Count := OC_Count + 1;
8183 OC_Fbit (OC_Count) := Fbit;
8184 OC_Lbit (OC_Count) := Lbit;
8185 end if;
8186 end if;
8188 Next (CC);
8189 end loop;
8191 Sorting.Sort (OC_Count);
8193 Overlap_Check_Required := False;
8194 for J in 1 .. OC_Count - 1 loop
8195 if OC_Lbit (J) >= OC_Fbit (J + 1) then
8196 Overlap_Check_Required := True;
8197 exit;
8198 end if;
8199 end loop;
8200 end Overlap_Check1;
8201 end if;
8203 -- If Overlap_Check_Required is still True, then we have to do the full
8204 -- scale overlap check, since we have at least two fields that do
8205 -- overlap, and we need to know if that is OK since they are in
8206 -- different variant, or whether we have a definite problem.
8208 if Overlap_Check_Required then
8209 Overlap_Check2 : declare
8210 C1_Ent, C2_Ent : Entity_Id;
8211 -- Entities of components being checked for overlap
8213 Clist : Node_Id;
8214 -- Component_List node whose Component_Items are being checked
8216 Citem : Node_Id;
8217 -- Component declaration for component being checked
8219 begin
8220 C1_Ent := First_Entity (Base_Type (Rectype));
8222 -- Loop through all components in record. For each component check
8223 -- for overlap with any of the preceding elements on the component
8224 -- list containing the component and also, if the component is in
8225 -- a variant, check against components outside the case structure.
8226 -- This latter test is repeated recursively up the variant tree.
8228 Main_Component_Loop : while Present (C1_Ent) loop
8229 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
8230 goto Continue_Main_Component_Loop;
8231 end if;
8233 -- Skip overlap check if entity has no declaration node. This
8234 -- happens with discriminants in constrained derived types.
8235 -- Possibly we are missing some checks as a result, but that
8236 -- does not seem terribly serious.
8238 if No (Declaration_Node (C1_Ent)) then
8239 goto Continue_Main_Component_Loop;
8240 end if;
8242 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
8244 -- Loop through component lists that need checking. Check the
8245 -- current component list and all lists in variants above us.
8247 Component_List_Loop : loop
8249 -- If derived type definition, go to full declaration
8250 -- If at outer level, check discriminants if there are any.
8252 if Nkind (Clist) = N_Derived_Type_Definition then
8253 Clist := Parent (Clist);
8254 end if;
8256 -- Outer level of record definition, check discriminants
8258 if Nkind_In (Clist, N_Full_Type_Declaration,
8259 N_Private_Type_Declaration)
8260 then
8261 if Has_Discriminants (Defining_Identifier (Clist)) then
8262 C2_Ent :=
8263 First_Discriminant (Defining_Identifier (Clist));
8264 while Present (C2_Ent) loop
8265 exit when C1_Ent = C2_Ent;
8266 Check_Component_Overlap (C1_Ent, C2_Ent);
8267 Next_Discriminant (C2_Ent);
8268 end loop;
8269 end if;
8271 -- Record extension case
8273 elsif Nkind (Clist) = N_Derived_Type_Definition then
8274 Clist := Empty;
8276 -- Otherwise check one component list
8278 else
8279 Citem := First (Component_Items (Clist));
8280 while Present (Citem) loop
8281 if Nkind (Citem) = N_Component_Declaration then
8282 C2_Ent := Defining_Identifier (Citem);
8283 exit when C1_Ent = C2_Ent;
8284 Check_Component_Overlap (C1_Ent, C2_Ent);
8285 end if;
8287 Next (Citem);
8288 end loop;
8289 end if;
8291 -- Check for variants above us (the parent of the Clist can
8292 -- be a variant, in which case its parent is a variant part,
8293 -- and the parent of the variant part is a component list
8294 -- whose components must all be checked against the current
8295 -- component for overlap).
8297 if Nkind (Parent (Clist)) = N_Variant then
8298 Clist := Parent (Parent (Parent (Clist)));
8300 -- Check for possible discriminant part in record, this
8301 -- is treated essentially as another level in the
8302 -- recursion. For this case the parent of the component
8303 -- list is the record definition, and its parent is the
8304 -- full type declaration containing the discriminant
8305 -- specifications.
8307 elsif Nkind (Parent (Clist)) = N_Record_Definition then
8308 Clist := Parent (Parent ((Clist)));
8310 -- If neither of these two cases, we are at the top of
8311 -- the tree.
8313 else
8314 exit Component_List_Loop;
8315 end if;
8316 end loop Component_List_Loop;
8318 <<Continue_Main_Component_Loop>>
8319 Next_Entity (C1_Ent);
8321 end loop Main_Component_Loop;
8322 end Overlap_Check2;
8323 end if;
8325 -- The following circuit deals with warning on record holes (gaps). We
8326 -- skip this check if overlap was detected, since it makes sense for the
8327 -- programmer to fix this illegality before worrying about warnings.
8329 if not Overlap_Detected and Warn_On_Record_Holes then
8330 Record_Hole_Check : declare
8331 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
8332 -- Full declaration of record type
8334 procedure Check_Component_List
8335 (CL : Node_Id;
8336 Sbit : Uint;
8337 DS : List_Id);
8338 -- Check component list CL for holes. The starting bit should be
8339 -- Sbit. which is zero for the main record component list and set
8340 -- appropriately for recursive calls for variants. DS is set to
8341 -- a list of discriminant specifications to be included in the
8342 -- consideration of components. It is No_List if none to consider.
8344 --------------------------
8345 -- Check_Component_List --
8346 --------------------------
8348 procedure Check_Component_List
8349 (CL : Node_Id;
8350 Sbit : Uint;
8351 DS : List_Id)
8353 Compl : Integer;
8355 begin
8356 Compl := Integer (List_Length (Component_Items (CL)));
8358 if DS /= No_List then
8359 Compl := Compl + Integer (List_Length (DS));
8360 end if;
8362 declare
8363 Comps : array (Natural range 0 .. Compl) of Entity_Id;
8364 -- Gather components (zero entry is for sort routine)
8366 Ncomps : Natural := 0;
8367 -- Number of entries stored in Comps (starting at Comps (1))
8369 Citem : Node_Id;
8370 -- One component item or discriminant specification
8372 Nbit : Uint;
8373 -- Starting bit for next component
8375 CEnt : Entity_Id;
8376 -- Component entity
8378 Variant : Node_Id;
8379 -- One variant
8381 function Lt (Op1, Op2 : Natural) return Boolean;
8382 -- Compare routine for Sort
8384 procedure Move (From : Natural; To : Natural);
8385 -- Move routine for Sort
8387 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
8389 --------
8390 -- Lt --
8391 --------
8393 function Lt (Op1, Op2 : Natural) return Boolean is
8394 begin
8395 return Component_Bit_Offset (Comps (Op1))
8397 Component_Bit_Offset (Comps (Op2));
8398 end Lt;
8400 ----------
8401 -- Move --
8402 ----------
8404 procedure Move (From : Natural; To : Natural) is
8405 begin
8406 Comps (To) := Comps (From);
8407 end Move;
8409 begin
8410 -- Gather discriminants into Comp
8412 if DS /= No_List then
8413 Citem := First (DS);
8414 while Present (Citem) loop
8415 if Nkind (Citem) = N_Discriminant_Specification then
8416 declare
8417 Ent : constant Entity_Id :=
8418 Defining_Identifier (Citem);
8419 begin
8420 if Ekind (Ent) = E_Discriminant then
8421 Ncomps := Ncomps + 1;
8422 Comps (Ncomps) := Ent;
8423 end if;
8424 end;
8425 end if;
8427 Next (Citem);
8428 end loop;
8429 end if;
8431 -- Gather component entities into Comp
8433 Citem := First (Component_Items (CL));
8434 while Present (Citem) loop
8435 if Nkind (Citem) = N_Component_Declaration then
8436 Ncomps := Ncomps + 1;
8437 Comps (Ncomps) := Defining_Identifier (Citem);
8438 end if;
8440 Next (Citem);
8441 end loop;
8443 -- Now sort the component entities based on the first bit.
8444 -- Note we already know there are no overlapping components.
8446 Sorting.Sort (Ncomps);
8448 -- Loop through entries checking for holes
8450 Nbit := Sbit;
8451 for J in 1 .. Ncomps loop
8452 CEnt := Comps (J);
8453 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
8455 if Error_Msg_Uint_1 > 0 then
8456 Error_Msg_NE
8457 ("?H?^-bit gap before component&",
8458 Component_Name (Component_Clause (CEnt)), CEnt);
8459 end if;
8461 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
8462 end loop;
8464 -- Process variant parts recursively if present
8466 if Present (Variant_Part (CL)) then
8467 Variant := First (Variants (Variant_Part (CL)));
8468 while Present (Variant) loop
8469 Check_Component_List
8470 (Component_List (Variant), Nbit, No_List);
8471 Next (Variant);
8472 end loop;
8473 end if;
8474 end;
8475 end Check_Component_List;
8477 -- Start of processing for Record_Hole_Check
8479 begin
8480 declare
8481 Sbit : Uint;
8483 begin
8484 if Is_Tagged_Type (Rectype) then
8485 Sbit := UI_From_Int (System_Address_Size);
8486 else
8487 Sbit := Uint_0;
8488 end if;
8490 if Nkind (Decl) = N_Full_Type_Declaration
8491 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8492 then
8493 Check_Component_List
8494 (Component_List (Type_Definition (Decl)),
8495 Sbit,
8496 Discriminant_Specifications (Decl));
8497 end if;
8498 end;
8499 end Record_Hole_Check;
8500 end if;
8502 -- For records that have component clauses for all components, and whose
8503 -- size is less than or equal to 32, we need to know the size in the
8504 -- front end to activate possible packed array processing where the
8505 -- component type is a record.
8507 -- At this stage Hbit + 1 represents the first unused bit from all the
8508 -- component clauses processed, so if the component clauses are
8509 -- complete, then this is the length of the record.
8511 -- For records longer than System.Storage_Unit, and for those where not
8512 -- all components have component clauses, the back end determines the
8513 -- length (it may for example be appropriate to round up the size
8514 -- to some convenient boundary, based on alignment considerations, etc).
8516 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
8518 -- Nothing to do if at least one component has no component clause
8520 Comp := First_Component_Or_Discriminant (Rectype);
8521 while Present (Comp) loop
8522 exit when No (Component_Clause (Comp));
8523 Next_Component_Or_Discriminant (Comp);
8524 end loop;
8526 -- If we fall out of loop, all components have component clauses
8527 -- and so we can set the size to the maximum value.
8529 if No (Comp) then
8530 Set_RM_Size (Rectype, Hbit + 1);
8531 end if;
8532 end if;
8533 end Check_Record_Representation_Clause;
8535 ----------------
8536 -- Check_Size --
8537 ----------------
8539 procedure Check_Size
8540 (N : Node_Id;
8541 T : Entity_Id;
8542 Siz : Uint;
8543 Biased : out Boolean)
8545 UT : constant Entity_Id := Underlying_Type (T);
8546 M : Uint;
8548 begin
8549 Biased := False;
8551 -- Reject patently improper size values.
8553 if Is_Elementary_Type (T)
8554 and then Siz > UI_From_Int (Int'Last)
8555 then
8556 Error_Msg_N ("Size value too large for elementary type", N);
8558 if Nkind (Original_Node (N)) = N_Op_Expon then
8559 Error_Msg_N
8560 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
8561 end if;
8562 end if;
8564 -- Dismiss generic types
8566 if Is_Generic_Type (T)
8567 or else
8568 Is_Generic_Type (UT)
8569 or else
8570 Is_Generic_Type (Root_Type (UT))
8571 then
8572 return;
8574 -- Guard against previous errors
8576 elsif No (UT) or else UT = Any_Type then
8577 Check_Error_Detected;
8578 return;
8580 -- Check case of bit packed array
8582 elsif Is_Array_Type (UT)
8583 and then Known_Static_Component_Size (UT)
8584 and then Is_Bit_Packed_Array (UT)
8585 then
8586 declare
8587 Asiz : Uint;
8588 Indx : Node_Id;
8589 Ityp : Entity_Id;
8591 begin
8592 Asiz := Component_Size (UT);
8593 Indx := First_Index (UT);
8594 loop
8595 Ityp := Etype (Indx);
8597 -- If non-static bound, then we are not in the business of
8598 -- trying to check the length, and indeed an error will be
8599 -- issued elsewhere, since sizes of non-static array types
8600 -- cannot be set implicitly or explicitly.
8602 if not Is_Static_Subtype (Ityp) then
8603 return;
8604 end if;
8606 -- Otherwise accumulate next dimension
8608 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
8609 Expr_Value (Type_Low_Bound (Ityp)) +
8610 Uint_1);
8612 Next_Index (Indx);
8613 exit when No (Indx);
8614 end loop;
8616 if Asiz <= Siz then
8617 return;
8619 else
8620 Error_Msg_Uint_1 := Asiz;
8621 Error_Msg_NE
8622 ("size for& too small, minimum allowed is ^", N, T);
8623 Set_Esize (T, Asiz);
8624 Set_RM_Size (T, Asiz);
8625 end if;
8626 end;
8628 -- All other composite types are ignored
8630 elsif Is_Composite_Type (UT) then
8631 return;
8633 -- For fixed-point types, don't check minimum if type is not frozen,
8634 -- since we don't know all the characteristics of the type that can
8635 -- affect the size (e.g. a specified small) till freeze time.
8637 elsif Is_Fixed_Point_Type (UT)
8638 and then not Is_Frozen (UT)
8639 then
8640 null;
8642 -- Cases for which a minimum check is required
8644 else
8645 -- Ignore if specified size is correct for the type
8647 if Known_Esize (UT) and then Siz = Esize (UT) then
8648 return;
8649 end if;
8651 -- Otherwise get minimum size
8653 M := UI_From_Int (Minimum_Size (UT));
8655 if Siz < M then
8657 -- Size is less than minimum size, but one possibility remains
8658 -- that we can manage with the new size if we bias the type.
8660 M := UI_From_Int (Minimum_Size (UT, Biased => True));
8662 if Siz < M then
8663 Error_Msg_Uint_1 := M;
8664 Error_Msg_NE
8665 ("size for& too small, minimum allowed is ^", N, T);
8666 Set_Esize (T, M);
8667 Set_RM_Size (T, M);
8668 else
8669 Biased := True;
8670 end if;
8671 end if;
8672 end if;
8673 end Check_Size;
8675 -------------------------
8676 -- Get_Alignment_Value --
8677 -------------------------
8679 function Get_Alignment_Value (Expr : Node_Id) return Uint is
8680 Align : constant Uint := Static_Integer (Expr);
8682 begin
8683 if Align = No_Uint then
8684 return No_Uint;
8686 elsif Align <= 0 then
8687 Error_Msg_N ("alignment value must be positive", Expr);
8688 return No_Uint;
8690 else
8691 for J in Int range 0 .. 64 loop
8692 declare
8693 M : constant Uint := Uint_2 ** J;
8695 begin
8696 exit when M = Align;
8698 if M > Align then
8699 Error_Msg_N
8700 ("alignment value must be power of 2", Expr);
8701 return No_Uint;
8702 end if;
8703 end;
8704 end loop;
8706 return Align;
8707 end if;
8708 end Get_Alignment_Value;
8710 -------------------------------------
8711 -- Inherit_Aspects_At_Freeze_Point --
8712 -------------------------------------
8714 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
8715 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8716 (Rep_Item : Node_Id) return Boolean;
8717 -- This routine checks if Rep_Item is either a pragma or an aspect
8718 -- specification node whose correponding pragma (if any) is present in
8719 -- the Rep Item chain of the entity it has been specified to.
8721 --------------------------------------------------
8722 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
8723 --------------------------------------------------
8725 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8726 (Rep_Item : Node_Id) return Boolean
8728 begin
8729 return Nkind (Rep_Item) = N_Pragma
8730 or else Present_In_Rep_Item
8731 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
8732 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
8734 -- Start of processing for Inherit_Aspects_At_Freeze_Point
8736 begin
8737 -- A representation item is either subtype-specific (Size and Alignment
8738 -- clauses) or type-related (all others). Subtype-specific aspects may
8739 -- differ for different subtypes of the same type (RM 13.1.8).
8741 -- A derived type inherits each type-related representation aspect of
8742 -- its parent type that was directly specified before the declaration of
8743 -- the derived type (RM 13.1.15).
8745 -- A derived subtype inherits each subtype-specific representation
8746 -- aspect of its parent subtype that was directly specified before the
8747 -- declaration of the derived type (RM 13.1.15).
8749 -- The general processing involves inheriting a representation aspect
8750 -- from a parent type whenever the first rep item (aspect specification,
8751 -- attribute definition clause, pragma) corresponding to the given
8752 -- representation aspect in the rep item chain of Typ, if any, isn't
8753 -- directly specified to Typ but to one of its parents.
8755 -- ??? Note that, for now, just a limited number of representation
8756 -- aspects have been inherited here so far. Many of them are
8757 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
8758 -- a non- exhaustive list of aspects that likely also need to
8759 -- be moved to this routine: Alignment, Component_Alignment,
8760 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
8761 -- Preelaborable_Initialization, RM_Size and Small.
8763 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
8764 return;
8765 end if;
8767 -- Ada_05/Ada_2005
8769 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
8770 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
8771 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8772 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
8773 then
8774 Set_Is_Ada_2005_Only (Typ);
8775 end if;
8777 -- Ada_12/Ada_2012
8779 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
8780 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
8781 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8782 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
8783 then
8784 Set_Is_Ada_2012_Only (Typ);
8785 end if;
8787 -- Atomic/Shared
8789 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
8790 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
8791 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8792 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
8793 then
8794 Set_Is_Atomic (Typ);
8795 Set_Treat_As_Volatile (Typ);
8796 Set_Is_Volatile (Typ);
8797 end if;
8799 -- Default_Component_Value
8801 if Is_Array_Type (Typ)
8802 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
8803 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
8804 then
8805 Set_Default_Aspect_Component_Value (Typ,
8806 Default_Aspect_Component_Value
8807 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
8808 end if;
8810 -- Default_Value
8812 if Is_Scalar_Type (Typ)
8813 and then Has_Rep_Item (Typ, Name_Default_Value, False)
8814 and then Has_Rep_Item (Typ, Name_Default_Value)
8815 then
8816 Set_Default_Aspect_Value (Typ,
8817 Default_Aspect_Value
8818 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
8819 end if;
8821 -- Discard_Names
8823 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
8824 and then Has_Rep_Item (Typ, Name_Discard_Names)
8825 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8826 (Get_Rep_Item (Typ, Name_Discard_Names))
8827 then
8828 Set_Discard_Names (Typ);
8829 end if;
8831 -- Invariants
8833 if not Has_Rep_Item (Typ, Name_Invariant, False)
8834 and then Has_Rep_Item (Typ, Name_Invariant)
8835 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8836 (Get_Rep_Item (Typ, Name_Invariant))
8837 then
8838 Set_Has_Invariants (Typ);
8840 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
8841 Set_Has_Inheritable_Invariants (Typ);
8842 end if;
8843 end if;
8845 -- Volatile
8847 if not Has_Rep_Item (Typ, Name_Volatile, False)
8848 and then Has_Rep_Item (Typ, Name_Volatile)
8849 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8850 (Get_Rep_Item (Typ, Name_Volatile))
8851 then
8852 Set_Treat_As_Volatile (Typ);
8853 Set_Is_Volatile (Typ);
8854 end if;
8856 -- Inheritance for derived types only
8858 if Is_Derived_Type (Typ) then
8859 declare
8860 Bas_Typ : constant Entity_Id := Base_Type (Typ);
8861 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
8863 begin
8864 -- Atomic_Components
8866 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
8867 and then Has_Rep_Item (Typ, Name_Atomic_Components)
8868 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8869 (Get_Rep_Item (Typ, Name_Atomic_Components))
8870 then
8871 Set_Has_Atomic_Components (Imp_Bas_Typ);
8872 end if;
8874 -- Volatile_Components
8876 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
8877 and then Has_Rep_Item (Typ, Name_Volatile_Components)
8878 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8879 (Get_Rep_Item (Typ, Name_Volatile_Components))
8880 then
8881 Set_Has_Volatile_Components (Imp_Bas_Typ);
8882 end if;
8884 -- Finalize_Storage_Only.
8886 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
8887 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
8888 then
8889 Set_Finalize_Storage_Only (Bas_Typ);
8890 end if;
8892 -- Universal_Aliasing
8894 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
8895 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
8896 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8897 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
8898 then
8899 Set_Universal_Aliasing (Imp_Bas_Typ);
8900 end if;
8902 -- Record type specific aspects
8904 if Is_Record_Type (Typ) then
8906 -- Bit_Order
8908 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
8909 and then Has_Rep_Item (Typ, Name_Bit_Order)
8910 then
8911 Set_Reverse_Bit_Order (Bas_Typ,
8912 Reverse_Bit_Order (Entity (Name
8913 (Get_Rep_Item (Typ, Name_Bit_Order)))));
8914 end if;
8916 -- Scalar_Storage_Order
8918 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
8919 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
8920 then
8921 Set_Reverse_Storage_Order (Bas_Typ,
8922 Reverse_Storage_Order (Entity (Name
8923 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
8924 end if;
8925 end if;
8926 end;
8927 end if;
8928 end Inherit_Aspects_At_Freeze_Point;
8930 ----------------
8931 -- Initialize --
8932 ----------------
8934 procedure Initialize is
8935 begin
8936 Address_Clause_Checks.Init;
8937 Independence_Checks.Init;
8938 Unchecked_Conversions.Init;
8939 end Initialize;
8941 -------------------------
8942 -- Is_Operational_Item --
8943 -------------------------
8945 function Is_Operational_Item (N : Node_Id) return Boolean is
8946 begin
8947 if Nkind (N) /= N_Attribute_Definition_Clause then
8948 return False;
8950 else
8951 declare
8952 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
8953 begin
8954 return Id = Attribute_Input
8955 or else Id = Attribute_Output
8956 or else Id = Attribute_Read
8957 or else Id = Attribute_Write
8958 or else Id = Attribute_External_Tag;
8959 end;
8960 end if;
8961 end Is_Operational_Item;
8963 ------------------
8964 -- Minimum_Size --
8965 ------------------
8967 function Minimum_Size
8968 (T : Entity_Id;
8969 Biased : Boolean := False) return Nat
8971 Lo : Uint := No_Uint;
8972 Hi : Uint := No_Uint;
8973 LoR : Ureal := No_Ureal;
8974 HiR : Ureal := No_Ureal;
8975 LoSet : Boolean := False;
8976 HiSet : Boolean := False;
8977 B : Uint;
8978 S : Nat;
8979 Ancest : Entity_Id;
8980 R_Typ : constant Entity_Id := Root_Type (T);
8982 begin
8983 -- If bad type, return 0
8985 if T = Any_Type then
8986 return 0;
8988 -- For generic types, just return zero. There cannot be any legitimate
8989 -- need to know such a size, but this routine may be called with a
8990 -- generic type as part of normal processing.
8992 elsif Is_Generic_Type (R_Typ)
8993 or else R_Typ = Any_Type
8994 then
8995 return 0;
8997 -- Access types. Normally an access type cannot have a size smaller
8998 -- than the size of System.Address. The exception is on VMS, where
8999 -- we have short and long addresses, and it is possible for an access
9000 -- type to have a short address size (and thus be less than the size
9001 -- of System.Address itself). We simply skip the check for VMS, and
9002 -- leave it to the back end to do the check.
9004 elsif Is_Access_Type (T) then
9005 if OpenVMS_On_Target then
9006 return 0;
9007 else
9008 return System_Address_Size;
9009 end if;
9011 -- Floating-point types
9013 elsif Is_Floating_Point_Type (T) then
9014 return UI_To_Int (Esize (R_Typ));
9016 -- Discrete types
9018 elsif Is_Discrete_Type (T) then
9020 -- The following loop is looking for the nearest compile time known
9021 -- bounds following the ancestor subtype chain. The idea is to find
9022 -- the most restrictive known bounds information.
9024 Ancest := T;
9025 loop
9026 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
9027 return 0;
9028 end if;
9030 if not LoSet then
9031 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
9032 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
9033 LoSet := True;
9034 exit when HiSet;
9035 end if;
9036 end if;
9038 if not HiSet then
9039 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
9040 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
9041 HiSet := True;
9042 exit when LoSet;
9043 end if;
9044 end if;
9046 Ancest := Ancestor_Subtype (Ancest);
9048 if No (Ancest) then
9049 Ancest := Base_Type (T);
9051 if Is_Generic_Type (Ancest) then
9052 return 0;
9053 end if;
9054 end if;
9055 end loop;
9057 -- Fixed-point types. We can't simply use Expr_Value to get the
9058 -- Corresponding_Integer_Value values of the bounds, since these do not
9059 -- get set till the type is frozen, and this routine can be called
9060 -- before the type is frozen. Similarly the test for bounds being static
9061 -- needs to include the case where we have unanalyzed real literals for
9062 -- the same reason.
9064 elsif Is_Fixed_Point_Type (T) then
9066 -- The following loop is looking for the nearest compile time known
9067 -- bounds following the ancestor subtype chain. The idea is to find
9068 -- the most restrictive known bounds information.
9070 Ancest := T;
9071 loop
9072 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
9073 return 0;
9074 end if;
9076 -- Note: In the following two tests for LoSet and HiSet, it may
9077 -- seem redundant to test for N_Real_Literal here since normally
9078 -- one would assume that the test for the value being known at
9079 -- compile time includes this case. However, there is a glitch.
9080 -- If the real literal comes from folding a non-static expression,
9081 -- then we don't consider any non- static expression to be known
9082 -- at compile time if we are in configurable run time mode (needed
9083 -- in some cases to give a clearer definition of what is and what
9084 -- is not accepted). So the test is indeed needed. Without it, we
9085 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
9087 if not LoSet then
9088 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
9089 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
9090 then
9091 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
9092 LoSet := True;
9093 exit when HiSet;
9094 end if;
9095 end if;
9097 if not HiSet then
9098 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
9099 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
9100 then
9101 HiR := Expr_Value_R (Type_High_Bound (Ancest));
9102 HiSet := True;
9103 exit when LoSet;
9104 end if;
9105 end if;
9107 Ancest := Ancestor_Subtype (Ancest);
9109 if No (Ancest) then
9110 Ancest := Base_Type (T);
9112 if Is_Generic_Type (Ancest) then
9113 return 0;
9114 end if;
9115 end if;
9116 end loop;
9118 Lo := UR_To_Uint (LoR / Small_Value (T));
9119 Hi := UR_To_Uint (HiR / Small_Value (T));
9121 -- No other types allowed
9123 else
9124 raise Program_Error;
9125 end if;
9127 -- Fall through with Hi and Lo set. Deal with biased case
9129 if (Biased
9130 and then not Is_Fixed_Point_Type (T)
9131 and then not (Is_Enumeration_Type (T)
9132 and then Has_Non_Standard_Rep (T)))
9133 or else Has_Biased_Representation (T)
9134 then
9135 Hi := Hi - Lo;
9136 Lo := Uint_0;
9137 end if;
9139 -- Signed case. Note that we consider types like range 1 .. -1 to be
9140 -- signed for the purpose of computing the size, since the bounds have
9141 -- to be accommodated in the base type.
9143 if Lo < 0 or else Hi < 0 then
9144 S := 1;
9145 B := Uint_1;
9147 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
9148 -- Note that we accommodate the case where the bounds cross. This
9149 -- can happen either because of the way the bounds are declared
9150 -- or because of the algorithm in Freeze_Fixed_Point_Type.
9152 while Lo < -B
9153 or else Hi < -B
9154 or else Lo >= B
9155 or else Hi >= B
9156 loop
9157 B := Uint_2 ** S;
9158 S := S + 1;
9159 end loop;
9161 -- Unsigned case
9163 else
9164 -- If both bounds are positive, make sure that both are represen-
9165 -- table in the case where the bounds are crossed. This can happen
9166 -- either because of the way the bounds are declared, or because of
9167 -- the algorithm in Freeze_Fixed_Point_Type.
9169 if Lo > Hi then
9170 Hi := Lo;
9171 end if;
9173 -- S = size, (can accommodate 0 .. (2**size - 1))
9175 S := 0;
9176 while Hi >= Uint_2 ** S loop
9177 S := S + 1;
9178 end loop;
9179 end if;
9181 return S;
9182 end Minimum_Size;
9184 ---------------------------
9185 -- New_Stream_Subprogram --
9186 ---------------------------
9188 procedure New_Stream_Subprogram
9189 (N : Node_Id;
9190 Ent : Entity_Id;
9191 Subp : Entity_Id;
9192 Nam : TSS_Name_Type)
9194 Loc : constant Source_Ptr := Sloc (N);
9195 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
9196 Subp_Id : Entity_Id;
9197 Subp_Decl : Node_Id;
9198 F : Entity_Id;
9199 Etyp : Entity_Id;
9201 Defer_Declaration : constant Boolean :=
9202 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
9203 -- For a tagged type, there is a declaration for each stream attribute
9204 -- at the freeze point, and we must generate only a completion of this
9205 -- declaration. We do the same for private types, because the full view
9206 -- might be tagged. Otherwise we generate a declaration at the point of
9207 -- the attribute definition clause.
9209 function Build_Spec return Node_Id;
9210 -- Used for declaration and renaming declaration, so that this is
9211 -- treated as a renaming_as_body.
9213 ----------------
9214 -- Build_Spec --
9215 ----------------
9217 function Build_Spec return Node_Id is
9218 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
9219 Formals : List_Id;
9220 Spec : Node_Id;
9221 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
9223 begin
9224 Subp_Id := Make_Defining_Identifier (Loc, Sname);
9226 -- S : access Root_Stream_Type'Class
9228 Formals := New_List (
9229 Make_Parameter_Specification (Loc,
9230 Defining_Identifier =>
9231 Make_Defining_Identifier (Loc, Name_S),
9232 Parameter_Type =>
9233 Make_Access_Definition (Loc,
9234 Subtype_Mark =>
9235 New_Reference_To (
9236 Designated_Type (Etype (F)), Loc))));
9238 if Nam = TSS_Stream_Input then
9239 Spec :=
9240 Make_Function_Specification (Loc,
9241 Defining_Unit_Name => Subp_Id,
9242 Parameter_Specifications => Formals,
9243 Result_Definition => T_Ref);
9244 else
9245 -- V : [out] T
9247 Append_To (Formals,
9248 Make_Parameter_Specification (Loc,
9249 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9250 Out_Present => Out_P,
9251 Parameter_Type => T_Ref));
9253 Spec :=
9254 Make_Procedure_Specification (Loc,
9255 Defining_Unit_Name => Subp_Id,
9256 Parameter_Specifications => Formals);
9257 end if;
9259 return Spec;
9260 end Build_Spec;
9262 -- Start of processing for New_Stream_Subprogram
9264 begin
9265 F := First_Formal (Subp);
9267 if Ekind (Subp) = E_Procedure then
9268 Etyp := Etype (Next_Formal (F));
9269 else
9270 Etyp := Etype (Subp);
9271 end if;
9273 -- Prepare subprogram declaration and insert it as an action on the
9274 -- clause node. The visibility for this entity is used to test for
9275 -- visibility of the attribute definition clause (in the sense of
9276 -- 8.3(23) as amended by AI-195).
9278 if not Defer_Declaration then
9279 Subp_Decl :=
9280 Make_Subprogram_Declaration (Loc,
9281 Specification => Build_Spec);
9283 -- For a tagged type, there is always a visible declaration for each
9284 -- stream TSS (it is a predefined primitive operation), and the
9285 -- completion of this declaration occurs at the freeze point, which is
9286 -- not always visible at places where the attribute definition clause is
9287 -- visible. So, we create a dummy entity here for the purpose of
9288 -- tracking the visibility of the attribute definition clause itself.
9290 else
9291 Subp_Id :=
9292 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
9293 Subp_Decl :=
9294 Make_Object_Declaration (Loc,
9295 Defining_Identifier => Subp_Id,
9296 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
9297 end if;
9299 Insert_Action (N, Subp_Decl);
9300 Set_Entity (N, Subp_Id);
9302 Subp_Decl :=
9303 Make_Subprogram_Renaming_Declaration (Loc,
9304 Specification => Build_Spec,
9305 Name => New_Reference_To (Subp, Loc));
9307 if Defer_Declaration then
9308 Set_TSS (Base_Type (Ent), Subp_Id);
9309 else
9310 Insert_Action (N, Subp_Decl);
9311 Copy_TSS (Subp_Id, Base_Type (Ent));
9312 end if;
9313 end New_Stream_Subprogram;
9315 ------------------------
9316 -- Rep_Item_Too_Early --
9317 ------------------------
9319 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
9320 begin
9321 -- Cannot apply non-operational rep items to generic types
9323 if Is_Operational_Item (N) then
9324 return False;
9326 elsif Is_Type (T)
9327 and then Is_Generic_Type (Root_Type (T))
9328 then
9329 Error_Msg_N ("representation item not allowed for generic type", N);
9330 return True;
9331 end if;
9333 -- Otherwise check for incomplete type
9335 if Is_Incomplete_Or_Private_Type (T)
9336 and then No (Underlying_Type (T))
9337 and then
9338 (Nkind (N) /= N_Pragma
9339 or else Get_Pragma_Id (N) /= Pragma_Import)
9340 then
9341 Error_Msg_N
9342 ("representation item must be after full type declaration", N);
9343 return True;
9345 -- If the type has incomplete components, a representation clause is
9346 -- illegal but stream attributes and Convention pragmas are correct.
9348 elsif Has_Private_Component (T) then
9349 if Nkind (N) = N_Pragma then
9350 return False;
9352 else
9353 Error_Msg_N
9354 ("representation item must appear after type is fully defined",
9356 return True;
9357 end if;
9358 else
9359 return False;
9360 end if;
9361 end Rep_Item_Too_Early;
9363 -----------------------
9364 -- Rep_Item_Too_Late --
9365 -----------------------
9367 function Rep_Item_Too_Late
9368 (T : Entity_Id;
9369 N : Node_Id;
9370 FOnly : Boolean := False) return Boolean
9372 S : Entity_Id;
9373 Parent_Type : Entity_Id;
9375 procedure Too_Late;
9376 -- Output the too late message. Note that this is not considered a
9377 -- serious error, since the effect is simply that we ignore the
9378 -- representation clause in this case.
9380 --------------
9381 -- Too_Late --
9382 --------------
9384 procedure Too_Late is
9385 begin
9386 -- Other compilers seem more relaxed about rep items appearing too
9387 -- late. Since analysis tools typically don't care about rep items
9388 -- anyway, no reason to be too strict about this.
9390 if not Relaxed_RM_Semantics then
9391 Error_Msg_N ("|representation item appears too late!", N);
9392 end if;
9393 end Too_Late;
9395 -- Start of processing for Rep_Item_Too_Late
9397 begin
9398 -- First make sure entity is not frozen (RM 13.1(9))
9400 if Is_Frozen (T)
9402 -- Exclude imported types, which may be frozen if they appear in a
9403 -- representation clause for a local type.
9405 and then not From_With_Type (T)
9407 -- Exclude generated entities (not coming from source). The common
9408 -- case is when we generate a renaming which prematurely freezes the
9409 -- renamed internal entity, but we still want to be able to set copies
9410 -- of attribute values such as Size/Alignment.
9412 and then Comes_From_Source (T)
9413 then
9414 Too_Late;
9415 S := First_Subtype (T);
9417 if Present (Freeze_Node (S)) then
9418 Error_Msg_NE
9419 ("??no more representation items for }", Freeze_Node (S), S);
9420 end if;
9422 return True;
9424 -- Check for case of non-tagged derived type whose parent either has
9425 -- primitive operations, or is a by reference type (RM 13.1(10)).
9427 elsif Is_Type (T)
9428 and then not FOnly
9429 and then Is_Derived_Type (T)
9430 and then not Is_Tagged_Type (T)
9431 then
9432 Parent_Type := Etype (Base_Type (T));
9434 if Has_Primitive_Operations (Parent_Type) then
9435 Too_Late;
9436 Error_Msg_NE
9437 ("primitive operations already defined for&!", N, Parent_Type);
9438 return True;
9440 elsif Is_By_Reference_Type (Parent_Type) then
9441 Too_Late;
9442 Error_Msg_NE
9443 ("parent type & is a by reference type!", N, Parent_Type);
9444 return True;
9445 end if;
9446 end if;
9448 -- No error, link item into head of chain of rep items for the entity,
9449 -- but avoid chaining if we have an overloadable entity, and the pragma
9450 -- is one that can apply to multiple overloaded entities.
9452 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
9453 declare
9454 Pname : constant Name_Id := Pragma_Name (N);
9455 begin
9456 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
9457 Name_External, Name_Interface)
9458 then
9459 return False;
9460 end if;
9461 end;
9462 end if;
9464 Record_Rep_Item (T, N);
9465 return False;
9466 end Rep_Item_Too_Late;
9468 -------------------------------------
9469 -- Replace_Type_References_Generic --
9470 -------------------------------------
9472 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
9474 function Replace_Node (N : Node_Id) return Traverse_Result;
9475 -- Processes a single node in the traversal procedure below, checking
9476 -- if node N should be replaced, and if so, doing the replacement.
9478 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
9479 -- This instantiation provides the body of Replace_Type_References
9481 ------------------
9482 -- Replace_Node --
9483 ------------------
9485 function Replace_Node (N : Node_Id) return Traverse_Result is
9486 S : Entity_Id;
9487 P : Node_Id;
9489 begin
9490 -- Case of identifier
9492 if Nkind (N) = N_Identifier then
9494 -- If not the type name, all done with this node
9496 if Chars (N) /= TName then
9497 return Skip;
9499 -- Otherwise do the replacement and we are done with this node
9501 else
9502 Replace_Type_Reference (N);
9503 return Skip;
9504 end if;
9506 -- Case of selected component (which is what a qualification
9507 -- looks like in the unanalyzed tree, which is what we have.
9509 elsif Nkind (N) = N_Selected_Component then
9511 -- If selector name is not our type, keeping going (we might
9512 -- still have an occurrence of the type in the prefix).
9514 if Nkind (Selector_Name (N)) /= N_Identifier
9515 or else Chars (Selector_Name (N)) /= TName
9516 then
9517 return OK;
9519 -- Selector name is our type, check qualification
9521 else
9522 -- Loop through scopes and prefixes, doing comparison
9524 S := Current_Scope;
9525 P := Prefix (N);
9526 loop
9527 -- Continue if no more scopes or scope with no name
9529 if No (S) or else Nkind (S) not in N_Has_Chars then
9530 return OK;
9531 end if;
9533 -- Do replace if prefix is an identifier matching the
9534 -- scope that we are currently looking at.
9536 if Nkind (P) = N_Identifier
9537 and then Chars (P) = Chars (S)
9538 then
9539 Replace_Type_Reference (N);
9540 return Skip;
9541 end if;
9543 -- Go check scope above us if prefix is itself of the
9544 -- form of a selected component, whose selector matches
9545 -- the scope we are currently looking at.
9547 if Nkind (P) = N_Selected_Component
9548 and then Nkind (Selector_Name (P)) = N_Identifier
9549 and then Chars (Selector_Name (P)) = Chars (S)
9550 then
9551 S := Scope (S);
9552 P := Prefix (P);
9554 -- For anything else, we don't have a match, so keep on
9555 -- going, there are still some weird cases where we may
9556 -- still have a replacement within the prefix.
9558 else
9559 return OK;
9560 end if;
9561 end loop;
9562 end if;
9564 -- Continue for any other node kind
9566 else
9567 return OK;
9568 end if;
9569 end Replace_Node;
9571 begin
9572 Replace_Type_Refs (N);
9573 end Replace_Type_References_Generic;
9575 -------------------------
9576 -- Same_Representation --
9577 -------------------------
9579 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
9580 T1 : constant Entity_Id := Underlying_Type (Typ1);
9581 T2 : constant Entity_Id := Underlying_Type (Typ2);
9583 begin
9584 -- A quick check, if base types are the same, then we definitely have
9585 -- the same representation, because the subtype specific representation
9586 -- attributes (Size and Alignment) do not affect representation from
9587 -- the point of view of this test.
9589 if Base_Type (T1) = Base_Type (T2) then
9590 return True;
9592 elsif Is_Private_Type (Base_Type (T2))
9593 and then Base_Type (T1) = Full_View (Base_Type (T2))
9594 then
9595 return True;
9596 end if;
9598 -- Tagged types never have differing representations
9600 if Is_Tagged_Type (T1) then
9601 return True;
9602 end if;
9604 -- Representations are definitely different if conventions differ
9606 if Convention (T1) /= Convention (T2) then
9607 return False;
9608 end if;
9610 -- Representations are different if component alignments or scalar
9611 -- storage orders differ.
9613 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
9614 and then
9615 (Is_Record_Type (T2) or else Is_Array_Type (T2))
9616 and then
9617 (Component_Alignment (T1) /= Component_Alignment (T2)
9618 or else
9619 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
9620 then
9621 return False;
9622 end if;
9624 -- For arrays, the only real issue is component size. If we know the
9625 -- component size for both arrays, and it is the same, then that's
9626 -- good enough to know we don't have a change of representation.
9628 if Is_Array_Type (T1) then
9629 if Known_Component_Size (T1)
9630 and then Known_Component_Size (T2)
9631 and then Component_Size (T1) = Component_Size (T2)
9632 then
9633 if VM_Target = No_VM then
9634 return True;
9636 -- In VM targets the representation of arrays with aliased
9637 -- components differs from arrays with non-aliased components
9639 else
9640 return Has_Aliased_Components (Base_Type (T1))
9642 Has_Aliased_Components (Base_Type (T2));
9643 end if;
9644 end if;
9645 end if;
9647 -- Types definitely have same representation if neither has non-standard
9648 -- representation since default representations are always consistent.
9649 -- If only one has non-standard representation, and the other does not,
9650 -- then we consider that they do not have the same representation. They
9651 -- might, but there is no way of telling early enough.
9653 if Has_Non_Standard_Rep (T1) then
9654 if not Has_Non_Standard_Rep (T2) then
9655 return False;
9656 end if;
9657 else
9658 return not Has_Non_Standard_Rep (T2);
9659 end if;
9661 -- Here the two types both have non-standard representation, and we need
9662 -- to determine if they have the same non-standard representation.
9664 -- For arrays, we simply need to test if the component sizes are the
9665 -- same. Pragma Pack is reflected in modified component sizes, so this
9666 -- check also deals with pragma Pack.
9668 if Is_Array_Type (T1) then
9669 return Component_Size (T1) = Component_Size (T2);
9671 -- Tagged types always have the same representation, because it is not
9672 -- possible to specify different representations for common fields.
9674 elsif Is_Tagged_Type (T1) then
9675 return True;
9677 -- Case of record types
9679 elsif Is_Record_Type (T1) then
9681 -- Packed status must conform
9683 if Is_Packed (T1) /= Is_Packed (T2) then
9684 return False;
9686 -- Otherwise we must check components. Typ2 maybe a constrained
9687 -- subtype with fewer components, so we compare the components
9688 -- of the base types.
9690 else
9691 Record_Case : declare
9692 CD1, CD2 : Entity_Id;
9694 function Same_Rep return Boolean;
9695 -- CD1 and CD2 are either components or discriminants. This
9696 -- function tests whether they have the same representation.
9698 --------------
9699 -- Same_Rep --
9700 --------------
9702 function Same_Rep return Boolean is
9703 begin
9704 if No (Component_Clause (CD1)) then
9705 return No (Component_Clause (CD2));
9706 else
9707 -- Note: at this point, component clauses have been
9708 -- normalized to the default bit order, so that the
9709 -- comparison of Component_Bit_Offsets is meaningful.
9711 return
9712 Present (Component_Clause (CD2))
9713 and then
9714 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
9715 and then
9716 Esize (CD1) = Esize (CD2);
9717 end if;
9718 end Same_Rep;
9720 -- Start of processing for Record_Case
9722 begin
9723 if Has_Discriminants (T1) then
9725 -- The number of discriminants may be different if the
9726 -- derived type has fewer (constrained by values). The
9727 -- invisible discriminants retain the representation of
9728 -- the original, so the discrepancy does not per se
9729 -- indicate a different representation.
9731 CD1 := First_Discriminant (T1);
9732 CD2 := First_Discriminant (T2);
9733 while Present (CD1) and then Present (CD2) loop
9734 if not Same_Rep then
9735 return False;
9736 else
9737 Next_Discriminant (CD1);
9738 Next_Discriminant (CD2);
9739 end if;
9740 end loop;
9741 end if;
9743 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
9744 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
9745 while Present (CD1) loop
9746 if not Same_Rep then
9747 return False;
9748 else
9749 Next_Component (CD1);
9750 Next_Component (CD2);
9751 end if;
9752 end loop;
9754 return True;
9755 end Record_Case;
9756 end if;
9758 -- For enumeration types, we must check each literal to see if the
9759 -- representation is the same. Note that we do not permit enumeration
9760 -- representation clauses for Character and Wide_Character, so these
9761 -- cases were already dealt with.
9763 elsif Is_Enumeration_Type (T1) then
9764 Enumeration_Case : declare
9765 L1, L2 : Entity_Id;
9767 begin
9768 L1 := First_Literal (T1);
9769 L2 := First_Literal (T2);
9770 while Present (L1) loop
9771 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
9772 return False;
9773 else
9774 Next_Literal (L1);
9775 Next_Literal (L2);
9776 end if;
9777 end loop;
9779 return True;
9780 end Enumeration_Case;
9782 -- Any other types have the same representation for these purposes
9784 else
9785 return True;
9786 end if;
9787 end Same_Representation;
9789 ----------------
9790 -- Set_Biased --
9791 ----------------
9793 procedure Set_Biased
9794 (E : Entity_Id;
9795 N : Node_Id;
9796 Msg : String;
9797 Biased : Boolean := True)
9799 begin
9800 if Biased then
9801 Set_Has_Biased_Representation (E);
9803 if Warn_On_Biased_Representation then
9804 Error_Msg_NE
9805 ("?B?" & Msg & " forces biased representation for&", N, E);
9806 end if;
9807 end if;
9808 end Set_Biased;
9810 --------------------
9811 -- Set_Enum_Esize --
9812 --------------------
9814 procedure Set_Enum_Esize (T : Entity_Id) is
9815 Lo : Uint;
9816 Hi : Uint;
9817 Sz : Nat;
9819 begin
9820 Init_Alignment (T);
9822 -- Find the minimum standard size (8,16,32,64) that fits
9824 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
9825 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
9827 if Lo < 0 then
9828 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
9829 Sz := Standard_Character_Size; -- May be > 8 on some targets
9831 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
9832 Sz := 16;
9834 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
9835 Sz := 32;
9837 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
9838 Sz := 64;
9839 end if;
9841 else
9842 if Hi < Uint_2**08 then
9843 Sz := Standard_Character_Size; -- May be > 8 on some targets
9845 elsif Hi < Uint_2**16 then
9846 Sz := 16;
9848 elsif Hi < Uint_2**32 then
9849 Sz := 32;
9851 else pragma Assert (Hi < Uint_2**63);
9852 Sz := 64;
9853 end if;
9854 end if;
9856 -- That minimum is the proper size unless we have a foreign convention
9857 -- and the size required is 32 or less, in which case we bump the size
9858 -- up to 32. This is required for C and C++ and seems reasonable for
9859 -- all other foreign conventions.
9861 if Has_Foreign_Convention (T)
9862 and then Esize (T) < Standard_Integer_Size
9863 then
9864 Init_Esize (T, Standard_Integer_Size);
9865 else
9866 Init_Esize (T, Sz);
9867 end if;
9868 end Set_Enum_Esize;
9870 ------------------------------
9871 -- Validate_Address_Clauses --
9872 ------------------------------
9874 procedure Validate_Address_Clauses is
9875 begin
9876 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
9877 declare
9878 ACCR : Address_Clause_Check_Record
9879 renames Address_Clause_Checks.Table (J);
9881 Expr : Node_Id;
9883 X_Alignment : Uint;
9884 Y_Alignment : Uint;
9886 X_Size : Uint;
9887 Y_Size : Uint;
9889 begin
9890 -- Skip processing of this entry if warning already posted
9892 if not Address_Warning_Posted (ACCR.N) then
9893 Expr := Original_Node (Expression (ACCR.N));
9895 -- Get alignments
9897 X_Alignment := Alignment (ACCR.X);
9898 Y_Alignment := Alignment (ACCR.Y);
9900 -- Similarly obtain sizes
9902 X_Size := Esize (ACCR.X);
9903 Y_Size := Esize (ACCR.Y);
9905 -- Check for large object overlaying smaller one
9907 if Y_Size > Uint_0
9908 and then X_Size > Uint_0
9909 and then X_Size > Y_Size
9910 then
9911 Error_Msg_NE
9912 ("?& overlays smaller object", ACCR.N, ACCR.X);
9913 Error_Msg_N
9914 ("\??program execution may be erroneous", ACCR.N);
9915 Error_Msg_Uint_1 := X_Size;
9916 Error_Msg_NE
9917 ("\??size of & is ^", ACCR.N, ACCR.X);
9918 Error_Msg_Uint_1 := Y_Size;
9919 Error_Msg_NE
9920 ("\??size of & is ^", ACCR.N, ACCR.Y);
9922 -- Check for inadequate alignment, both of the base object
9923 -- and of the offset, if any.
9925 -- Note: we do not check the alignment if we gave a size
9926 -- warning, since it would likely be redundant.
9928 elsif Y_Alignment /= Uint_0
9929 and then (Y_Alignment < X_Alignment
9930 or else (ACCR.Off
9931 and then
9932 Nkind (Expr) = N_Attribute_Reference
9933 and then
9934 Attribute_Name (Expr) = Name_Address
9935 and then
9936 Has_Compatible_Alignment
9937 (ACCR.X, Prefix (Expr))
9938 /= Known_Compatible))
9939 then
9940 Error_Msg_NE
9941 ("??specified address for& may be inconsistent "
9942 & "with alignment", ACCR.N, ACCR.X);
9943 Error_Msg_N
9944 ("\??program execution may be erroneous (RM 13.3(27))",
9945 ACCR.N);
9946 Error_Msg_Uint_1 := X_Alignment;
9947 Error_Msg_NE
9948 ("\??alignment of & is ^", ACCR.N, ACCR.X);
9949 Error_Msg_Uint_1 := Y_Alignment;
9950 Error_Msg_NE
9951 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
9952 if Y_Alignment >= X_Alignment then
9953 Error_Msg_N
9954 ("\??but offset is not multiple of alignment", ACCR.N);
9955 end if;
9956 end if;
9957 end if;
9958 end;
9959 end loop;
9960 end Validate_Address_Clauses;
9962 ---------------------------
9963 -- Validate_Independence --
9964 ---------------------------
9966 procedure Validate_Independence is
9967 SU : constant Uint := UI_From_Int (System_Storage_Unit);
9968 N : Node_Id;
9969 E : Entity_Id;
9970 IC : Boolean;
9971 Comp : Entity_Id;
9972 Addr : Node_Id;
9973 P : Node_Id;
9975 procedure Check_Array_Type (Atyp : Entity_Id);
9976 -- Checks if the array type Atyp has independent components, and
9977 -- if not, outputs an appropriate set of error messages.
9979 procedure No_Independence;
9980 -- Output message that independence cannot be guaranteed
9982 function OK_Component (C : Entity_Id) return Boolean;
9983 -- Checks one component to see if it is independently accessible, and
9984 -- if so yields True, otherwise yields False if independent access
9985 -- cannot be guaranteed. This is a conservative routine, it only
9986 -- returns True if it knows for sure, it returns False if it knows
9987 -- there is a problem, or it cannot be sure there is no problem.
9989 procedure Reason_Bad_Component (C : Entity_Id);
9990 -- Outputs continuation message if a reason can be determined for
9991 -- the component C being bad.
9993 ----------------------
9994 -- Check_Array_Type --
9995 ----------------------
9997 procedure Check_Array_Type (Atyp : Entity_Id) is
9998 Ctyp : constant Entity_Id := Component_Type (Atyp);
10000 begin
10001 -- OK if no alignment clause, no pack, and no component size
10003 if not Has_Component_Size_Clause (Atyp)
10004 and then not Has_Alignment_Clause (Atyp)
10005 and then not Is_Packed (Atyp)
10006 then
10007 return;
10008 end if;
10010 -- Check actual component size
10012 if not Known_Component_Size (Atyp)
10013 or else not (Addressable (Component_Size (Atyp))
10014 and then Component_Size (Atyp) < 64)
10015 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
10016 then
10017 No_Independence;
10019 -- Bad component size, check reason
10021 if Has_Component_Size_Clause (Atyp) then
10022 P := Get_Attribute_Definition_Clause
10023 (Atyp, Attribute_Component_Size);
10025 if Present (P) then
10026 Error_Msg_Sloc := Sloc (P);
10027 Error_Msg_N ("\because of Component_Size clause#", N);
10028 return;
10029 end if;
10030 end if;
10032 if Is_Packed (Atyp) then
10033 P := Get_Rep_Pragma (Atyp, Name_Pack);
10035 if Present (P) then
10036 Error_Msg_Sloc := Sloc (P);
10037 Error_Msg_N ("\because of pragma Pack#", N);
10038 return;
10039 end if;
10040 end if;
10042 -- No reason found, just return
10044 return;
10045 end if;
10047 -- Array type is OK independence-wise
10049 return;
10050 end Check_Array_Type;
10052 ---------------------
10053 -- No_Independence --
10054 ---------------------
10056 procedure No_Independence is
10057 begin
10058 if Pragma_Name (N) = Name_Independent then
10059 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
10060 else
10061 Error_Msg_NE
10062 ("independent components cannot be guaranteed for&", N, E);
10063 end if;
10064 end No_Independence;
10066 ------------------
10067 -- OK_Component --
10068 ------------------
10070 function OK_Component (C : Entity_Id) return Boolean is
10071 Rec : constant Entity_Id := Scope (C);
10072 Ctyp : constant Entity_Id := Etype (C);
10074 begin
10075 -- OK if no component clause, no Pack, and no alignment clause
10077 if No (Component_Clause (C))
10078 and then not Is_Packed (Rec)
10079 and then not Has_Alignment_Clause (Rec)
10080 then
10081 return True;
10082 end if;
10084 -- Here we look at the actual component layout. A component is
10085 -- addressable if its size is a multiple of the Esize of the
10086 -- component type, and its starting position in the record has
10087 -- appropriate alignment, and the record itself has appropriate
10088 -- alignment to guarantee the component alignment.
10090 -- Make sure sizes are static, always assume the worst for any
10091 -- cases where we cannot check static values.
10093 if not (Known_Static_Esize (C)
10094 and then
10095 Known_Static_Esize (Ctyp))
10096 then
10097 return False;
10098 end if;
10100 -- Size of component must be addressable or greater than 64 bits
10101 -- and a multiple of bytes.
10103 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
10104 return False;
10105 end if;
10107 -- Check size is proper multiple
10109 if Esize (C) mod Esize (Ctyp) /= 0 then
10110 return False;
10111 end if;
10113 -- Check alignment of component is OK
10115 if not Known_Component_Bit_Offset (C)
10116 or else Component_Bit_Offset (C) < Uint_0
10117 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
10118 then
10119 return False;
10120 end if;
10122 -- Check alignment of record type is OK
10124 if not Known_Alignment (Rec)
10125 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
10126 then
10127 return False;
10128 end if;
10130 -- All tests passed, component is addressable
10132 return True;
10133 end OK_Component;
10135 --------------------------
10136 -- Reason_Bad_Component --
10137 --------------------------
10139 procedure Reason_Bad_Component (C : Entity_Id) is
10140 Rec : constant Entity_Id := Scope (C);
10141 Ctyp : constant Entity_Id := Etype (C);
10143 begin
10144 -- If component clause present assume that's the problem
10146 if Present (Component_Clause (C)) then
10147 Error_Msg_Sloc := Sloc (Component_Clause (C));
10148 Error_Msg_N ("\because of Component_Clause#", N);
10149 return;
10150 end if;
10152 -- If pragma Pack clause present, assume that's the problem
10154 if Is_Packed (Rec) then
10155 P := Get_Rep_Pragma (Rec, Name_Pack);
10157 if Present (P) then
10158 Error_Msg_Sloc := Sloc (P);
10159 Error_Msg_N ("\because of pragma Pack#", N);
10160 return;
10161 end if;
10162 end if;
10164 -- See if record has bad alignment clause
10166 if Has_Alignment_Clause (Rec)
10167 and then Known_Alignment (Rec)
10168 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
10169 then
10170 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
10172 if Present (P) then
10173 Error_Msg_Sloc := Sloc (P);
10174 Error_Msg_N ("\because of Alignment clause#", N);
10175 end if;
10176 end if;
10178 -- Couldn't find a reason, so return without a message
10180 return;
10181 end Reason_Bad_Component;
10183 -- Start of processing for Validate_Independence
10185 begin
10186 for J in Independence_Checks.First .. Independence_Checks.Last loop
10187 N := Independence_Checks.Table (J).N;
10188 E := Independence_Checks.Table (J).E;
10189 IC := Pragma_Name (N) = Name_Independent_Components;
10191 -- Deal with component case
10193 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
10194 if not OK_Component (E) then
10195 No_Independence;
10196 Reason_Bad_Component (E);
10197 goto Continue;
10198 end if;
10199 end if;
10201 -- Deal with record with Independent_Components
10203 if IC and then Is_Record_Type (E) then
10204 Comp := First_Component_Or_Discriminant (E);
10205 while Present (Comp) loop
10206 if not OK_Component (Comp) then
10207 No_Independence;
10208 Reason_Bad_Component (Comp);
10209 goto Continue;
10210 end if;
10212 Next_Component_Or_Discriminant (Comp);
10213 end loop;
10214 end if;
10216 -- Deal with address clause case
10218 if Is_Object (E) then
10219 Addr := Address_Clause (E);
10221 if Present (Addr) then
10222 No_Independence;
10223 Error_Msg_Sloc := Sloc (Addr);
10224 Error_Msg_N ("\because of Address clause#", N);
10225 goto Continue;
10226 end if;
10227 end if;
10229 -- Deal with independent components for array type
10231 if IC and then Is_Array_Type (E) then
10232 Check_Array_Type (E);
10233 end if;
10235 -- Deal with independent components for array object
10237 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
10238 Check_Array_Type (Etype (E));
10239 end if;
10241 <<Continue>> null;
10242 end loop;
10243 end Validate_Independence;
10245 -----------------------------------
10246 -- Validate_Unchecked_Conversion --
10247 -----------------------------------
10249 procedure Validate_Unchecked_Conversion
10250 (N : Node_Id;
10251 Act_Unit : Entity_Id)
10253 Source : Entity_Id;
10254 Target : Entity_Id;
10255 Vnode : Node_Id;
10257 begin
10258 -- Obtain source and target types. Note that we call Ancestor_Subtype
10259 -- here because the processing for generic instantiation always makes
10260 -- subtypes, and we want the original frozen actual types.
10262 -- If we are dealing with private types, then do the check on their
10263 -- fully declared counterparts if the full declarations have been
10264 -- encountered (they don't have to be visible, but they must exist!)
10266 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
10268 if Is_Private_Type (Source)
10269 and then Present (Underlying_Type (Source))
10270 then
10271 Source := Underlying_Type (Source);
10272 end if;
10274 Target := Ancestor_Subtype (Etype (Act_Unit));
10276 -- If either type is generic, the instantiation happens within a generic
10277 -- unit, and there is nothing to check. The proper check will happen
10278 -- when the enclosing generic is instantiated.
10280 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
10281 return;
10282 end if;
10284 if Is_Private_Type (Target)
10285 and then Present (Underlying_Type (Target))
10286 then
10287 Target := Underlying_Type (Target);
10288 end if;
10290 -- Source may be unconstrained array, but not target
10292 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
10293 Error_Msg_N
10294 ("unchecked conversion to unconstrained array not allowed", N);
10295 return;
10296 end if;
10298 -- Warn if conversion between two different convention pointers
10300 if Is_Access_Type (Target)
10301 and then Is_Access_Type (Source)
10302 and then Convention (Target) /= Convention (Source)
10303 and then Warn_On_Unchecked_Conversion
10304 then
10305 -- Give warnings for subprogram pointers only on most targets. The
10306 -- exception is VMS, where data pointers can have different lengths
10307 -- depending on the pointer convention.
10309 if Is_Access_Subprogram_Type (Target)
10310 or else Is_Access_Subprogram_Type (Source)
10311 or else OpenVMS_On_Target
10312 then
10313 Error_Msg_N
10314 ("?z?conversion between pointers with different conventions!",
10316 end if;
10317 end if;
10319 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
10320 -- warning when compiling GNAT-related sources.
10322 if Warn_On_Unchecked_Conversion
10323 and then not In_Predefined_Unit (N)
10324 and then RTU_Loaded (Ada_Calendar)
10325 and then
10326 (Chars (Source) = Name_Time
10327 or else
10328 Chars (Target) = Name_Time)
10329 then
10330 -- If Ada.Calendar is loaded and the name of one of the operands is
10331 -- Time, there is a good chance that this is Ada.Calendar.Time.
10333 declare
10334 Calendar_Time : constant Entity_Id :=
10335 Full_View (RTE (RO_CA_Time));
10336 begin
10337 pragma Assert (Present (Calendar_Time));
10339 if Source = Calendar_Time or else Target = Calendar_Time then
10340 Error_Msg_N
10341 ("?z?representation of 'Time values may change between " &
10342 "'G'N'A'T versions", N);
10343 end if;
10344 end;
10345 end if;
10347 -- Make entry in unchecked conversion table for later processing by
10348 -- Validate_Unchecked_Conversions, which will check sizes and alignments
10349 -- (using values set by the back-end where possible). This is only done
10350 -- if the appropriate warning is active.
10352 if Warn_On_Unchecked_Conversion then
10353 Unchecked_Conversions.Append
10354 (New_Val => UC_Entry'(Eloc => Sloc (N),
10355 Source => Source,
10356 Target => Target));
10358 -- If both sizes are known statically now, then back end annotation
10359 -- is not required to do a proper check but if either size is not
10360 -- known statically, then we need the annotation.
10362 if Known_Static_RM_Size (Source)
10363 and then
10364 Known_Static_RM_Size (Target)
10365 then
10366 null;
10367 else
10368 Back_Annotate_Rep_Info := True;
10369 end if;
10370 end if;
10372 -- If unchecked conversion to access type, and access type is declared
10373 -- in the same unit as the unchecked conversion, then set the flag
10374 -- No_Strict_Aliasing (no strict aliasing is implicit here)
10376 if Is_Access_Type (Target) and then
10377 In_Same_Source_Unit (Target, N)
10378 then
10379 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
10380 end if;
10382 -- Generate N_Validate_Unchecked_Conversion node for back end in case
10383 -- the back end needs to perform special validation checks.
10385 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
10386 -- have full expansion and the back end is called ???
10388 Vnode :=
10389 Make_Validate_Unchecked_Conversion (Sloc (N));
10390 Set_Source_Type (Vnode, Source);
10391 Set_Target_Type (Vnode, Target);
10393 -- If the unchecked conversion node is in a list, just insert before it.
10394 -- If not we have some strange case, not worth bothering about.
10396 if Is_List_Member (N) then
10397 Insert_After (N, Vnode);
10398 end if;
10399 end Validate_Unchecked_Conversion;
10401 ------------------------------------
10402 -- Validate_Unchecked_Conversions --
10403 ------------------------------------
10405 procedure Validate_Unchecked_Conversions is
10406 begin
10407 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
10408 declare
10409 T : UC_Entry renames Unchecked_Conversions.Table (N);
10411 Eloc : constant Source_Ptr := T.Eloc;
10412 Source : constant Entity_Id := T.Source;
10413 Target : constant Entity_Id := T.Target;
10415 Source_Siz : Uint;
10416 Target_Siz : Uint;
10418 begin
10419 -- This validation check, which warns if we have unequal sizes for
10420 -- unchecked conversion, and thus potentially implementation
10421 -- dependent semantics, is one of the few occasions on which we
10422 -- use the official RM size instead of Esize. See description in
10423 -- Einfo "Handling of Type'Size Values" for details.
10425 if Serious_Errors_Detected = 0
10426 and then Known_Static_RM_Size (Source)
10427 and then Known_Static_RM_Size (Target)
10429 -- Don't do the check if warnings off for either type, note the
10430 -- deliberate use of OR here instead of OR ELSE to get the flag
10431 -- Warnings_Off_Used set for both types if appropriate.
10433 and then not (Has_Warnings_Off (Source)
10435 Has_Warnings_Off (Target))
10436 then
10437 Source_Siz := RM_Size (Source);
10438 Target_Siz := RM_Size (Target);
10440 if Source_Siz /= Target_Siz then
10441 Error_Msg
10442 ("?z?types for unchecked conversion have different sizes!",
10443 Eloc);
10445 if All_Errors_Mode then
10446 Error_Msg_Name_1 := Chars (Source);
10447 Error_Msg_Uint_1 := Source_Siz;
10448 Error_Msg_Name_2 := Chars (Target);
10449 Error_Msg_Uint_2 := Target_Siz;
10450 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
10452 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
10454 if Is_Discrete_Type (Source)
10455 and then
10456 Is_Discrete_Type (Target)
10457 then
10458 if Source_Siz > Target_Siz then
10459 Error_Msg
10460 ("\?z?^ high order bits of source will "
10461 & "be ignored!", Eloc);
10463 elsif Is_Unsigned_Type (Source) then
10464 Error_Msg
10465 ("\?z?source will be extended with ^ high order "
10466 & "zero bits?!", Eloc);
10468 else
10469 Error_Msg
10470 ("\?z?source will be extended with ^ high order "
10471 & "sign bits!", Eloc);
10472 end if;
10474 elsif Source_Siz < Target_Siz then
10475 if Is_Discrete_Type (Target) then
10476 if Bytes_Big_Endian then
10477 Error_Msg
10478 ("\?z?target value will include ^ undefined "
10479 & "low order bits!", Eloc);
10480 else
10481 Error_Msg
10482 ("\?z?target value will include ^ undefined "
10483 & "high order bits!", Eloc);
10484 end if;
10486 else
10487 Error_Msg
10488 ("\?z?^ trailing bits of target value will be "
10489 & "undefined!", Eloc);
10490 end if;
10492 else pragma Assert (Source_Siz > Target_Siz);
10493 Error_Msg
10494 ("\?z?^ trailing bits of source will be ignored!",
10495 Eloc);
10496 end if;
10497 end if;
10498 end if;
10499 end if;
10501 -- If both types are access types, we need to check the alignment.
10502 -- If the alignment of both is specified, we can do it here.
10504 if Serious_Errors_Detected = 0
10505 and then Ekind (Source) in Access_Kind
10506 and then Ekind (Target) in Access_Kind
10507 and then Target_Strict_Alignment
10508 and then Present (Designated_Type (Source))
10509 and then Present (Designated_Type (Target))
10510 then
10511 declare
10512 D_Source : constant Entity_Id := Designated_Type (Source);
10513 D_Target : constant Entity_Id := Designated_Type (Target);
10515 begin
10516 if Known_Alignment (D_Source)
10517 and then
10518 Known_Alignment (D_Target)
10519 then
10520 declare
10521 Source_Align : constant Uint := Alignment (D_Source);
10522 Target_Align : constant Uint := Alignment (D_Target);
10524 begin
10525 if Source_Align < Target_Align
10526 and then not Is_Tagged_Type (D_Source)
10528 -- Suppress warning if warnings suppressed on either
10529 -- type or either designated type. Note the use of
10530 -- OR here instead of OR ELSE. That is intentional,
10531 -- we would like to set flag Warnings_Off_Used in
10532 -- all types for which warnings are suppressed.
10534 and then not (Has_Warnings_Off (D_Source)
10536 Has_Warnings_Off (D_Target)
10538 Has_Warnings_Off (Source)
10540 Has_Warnings_Off (Target))
10541 then
10542 Error_Msg_Uint_1 := Target_Align;
10543 Error_Msg_Uint_2 := Source_Align;
10544 Error_Msg_Node_1 := D_Target;
10545 Error_Msg_Node_2 := D_Source;
10546 Error_Msg
10547 ("?z?alignment of & (^) is stricter than "
10548 & "alignment of & (^)!", Eloc);
10549 Error_Msg
10550 ("\?z?resulting access value may have invalid "
10551 & "alignment!", Eloc);
10552 end if;
10553 end;
10554 end if;
10555 end;
10556 end if;
10557 end;
10558 end loop;
10559 end Validate_Unchecked_Conversions;
10561 end Sem_Ch13;