2015-01-30 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch13.adb
blob10b0062f3b3b5819d072271908ef056fc95db09a
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-2015, 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 Freeze; use Freeze;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Case; use Sem_Case;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Sinput; use Sinput;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Ttypes; use Ttypes;
67 with Tbuild; use Tbuild;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
71 with GNAT.Heap_Sort_G;
73 package body Sem_Ch13 is
75 SSU : constant Pos := System_Storage_Unit;
76 -- Convenient short hand for commonly used constant
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
83 -- This routine is called after setting one of the sizes of type entity
84 -- Typ to Size. The purpose is to deal with the situation of a derived
85 -- type whose inherited alignment is no longer appropriate for the new
86 -- size value. In this case, we reset the Alignment to unknown.
88 procedure Build_Discrete_Static_Predicate
89 (Typ : Entity_Id;
90 Expr : Node_Id;
91 Nam : Name_Id);
92 -- Given a predicated type Typ, where Typ is a discrete static subtype,
93 -- whose predicate expression is Expr, tests if Expr is a static predicate,
94 -- and if so, builds the predicate range list. Nam is the name of the one
95 -- argument to the predicate function. Occurrences of the type name in the
96 -- predicate expression have been replaced by identifier references to this
97 -- name, which is unique, so any identifier with Chars matching Nam must be
98 -- a reference to the type. If the predicate is non-static, this procedure
99 -- returns doing nothing. If the predicate is static, then the predicate
100 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
101 -- rewritten as a canonicalized membership operation.
103 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
104 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
105 -- then either there are pragma Predicate entries on the rep chain for the
106 -- type (note that Predicate aspects are converted to pragma Predicate), or
107 -- there are inherited aspects from a parent type, or ancestor subtypes.
108 -- This procedure builds the spec and body for the Predicate function that
109 -- tests these predicates. N is the freeze node for the type. The spec of
110 -- the function is inserted before the freeze node, and the body of the
111 -- function is inserted after the freeze node. If the predicate expression
112 -- has at least one Raise_Expression, then this procedure also builds the
113 -- M version of the predicate function for use in membership tests.
115 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
116 -- Called if both Storage_Pool and Storage_Size attribute definition
117 -- clauses (SP and SS) are present for entity Ent. Issue error message.
119 procedure Freeze_Entity_Checks (N : Node_Id);
120 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
121 -- to generate appropriate semantic checks that are delayed until this
122 -- point (they had to be delayed this long for cases of delayed aspects,
123 -- e.g. analysis of statically predicated subtypes in choices, for which
124 -- we have to be sure the subtypes in question are frozen before checking.
126 function Get_Alignment_Value (Expr : Node_Id) return Uint;
127 -- Given the expression for an alignment value, returns the corresponding
128 -- Uint value. If the value is inappropriate, then error messages are
129 -- posted as required, and a value of No_Uint is returned.
131 function Is_Operational_Item (N : Node_Id) return Boolean;
132 -- A specification for a stream attribute is allowed before the full type
133 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
134 -- that do not specify a representation characteristic are operational
135 -- attributes.
137 function Is_Predicate_Static
138 (Expr : Node_Id;
139 Nam : Name_Id) return Boolean;
140 -- Given predicate expression Expr, tests if Expr is predicate-static in
141 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
142 -- name in the predicate expression have been replaced by references to
143 -- an identifier whose Chars field is Nam. This name is unique, so any
144 -- identifier with Chars matching Nam must be a reference to the type.
145 -- Returns True if the expression is predicate-static and False otherwise,
146 -- but is not in the business of setting flags or issuing error messages.
148 -- Only scalar types can have static predicates, so False is always
149 -- returned for non-scalar types.
151 -- Note: the RM seems to suggest that string types can also have static
152 -- predicates. But that really makes lttle sense as very few useful
153 -- predicates can be constructed for strings. Remember that:
155 -- "ABC" < "DEF"
157 -- is not a static expression. So even though the clearly faulty RM wording
158 -- allows the following:
160 -- subtype S is String with Static_Predicate => S < "DEF"
162 -- We can't allow this, otherwise we have predicate-static applying to a
163 -- larger class than static expressions, which was never intended.
165 procedure New_Stream_Subprogram
166 (N : Node_Id;
167 Ent : Entity_Id;
168 Subp : Entity_Id;
169 Nam : TSS_Name_Type);
170 -- Create a subprogram renaming of a given stream attribute to the
171 -- designated subprogram and then in the tagged case, provide this as a
172 -- primitive operation, or in the untagged case make an appropriate TSS
173 -- entry. This is more properly an expansion activity than just semantics,
174 -- but the presence of user-defined stream functions for limited types
175 -- is a legality check, which is why this takes place here rather than in
176 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
177 -- function to be generated.
179 -- To avoid elaboration anomalies with freeze nodes, for untagged types
180 -- we generate both a subprogram declaration and a subprogram renaming
181 -- declaration, so that the attribute specification is handled as a
182 -- renaming_as_body. For tagged types, the specification is one of the
183 -- primitive specs.
185 procedure Resolve_Iterable_Operation
186 (N : Node_Id;
187 Cursor : Entity_Id;
188 Typ : Entity_Id;
189 Nam : Name_Id);
190 -- If the name of a primitive operation for an Iterable aspect is
191 -- overloaded, resolve according to required signature.
193 procedure Set_Biased
194 (E : Entity_Id;
195 N : Node_Id;
196 Msg : String;
197 Biased : Boolean := True);
198 -- If Biased is True, sets Has_Biased_Representation flag for E, and
199 -- outputs a warning message at node N if Warn_On_Biased_Representation is
200 -- is True. This warning inserts the string Msg to describe the construct
201 -- causing biasing.
203 ----------------------------------------------
204 -- Table for Validate_Unchecked_Conversions --
205 ----------------------------------------------
207 -- The following table collects unchecked conversions for validation.
208 -- Entries are made by Validate_Unchecked_Conversion and then the call
209 -- to Validate_Unchecked_Conversions does the actual error checking and
210 -- posting of warnings. The reason for this delayed processing is to take
211 -- advantage of back-annotations of size and alignment values performed by
212 -- the back end.
214 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
215 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
216 -- already have modified all Sloc values if the -gnatD option is set.
218 type UC_Entry is record
219 Eloc : Source_Ptr; -- node used for posting warnings
220 Source : Entity_Id; -- source type for unchecked conversion
221 Target : Entity_Id; -- target type for unchecked conversion
222 Act_Unit : Entity_Id; -- actual function instantiated
223 end record;
225 package Unchecked_Conversions is new Table.Table (
226 Table_Component_Type => UC_Entry,
227 Table_Index_Type => Int,
228 Table_Low_Bound => 1,
229 Table_Initial => 50,
230 Table_Increment => 200,
231 Table_Name => "Unchecked_Conversions");
233 ----------------------------------------
234 -- Table for Validate_Address_Clauses --
235 ----------------------------------------
237 -- If an address clause has the form
239 -- for X'Address use Expr
241 -- where Expr is of the form Y'Address or recursively is a reference to a
242 -- constant of either of these forms, and X and Y are entities of objects,
243 -- then if Y has a smaller alignment than X, that merits a warning about
244 -- possible bad alignment. The following table collects address clauses of
245 -- this kind. We put these in a table so that they can be checked after the
246 -- back end has completed annotation of the alignments of objects, since we
247 -- can catch more cases that way.
249 type Address_Clause_Check_Record is record
250 N : Node_Id;
251 -- The address clause
253 X : Entity_Id;
254 -- The entity of the object overlaying Y
256 Y : Entity_Id;
257 -- The entity of the object being overlaid
259 Off : Boolean;
260 -- Whether the address is offset within Y
261 end record;
263 package Address_Clause_Checks is new Table.Table (
264 Table_Component_Type => Address_Clause_Check_Record,
265 Table_Index_Type => Int,
266 Table_Low_Bound => 1,
267 Table_Initial => 20,
268 Table_Increment => 200,
269 Table_Name => "Address_Clause_Checks");
271 -----------------------------------------
272 -- Adjust_Record_For_Reverse_Bit_Order --
273 -----------------------------------------
275 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
276 Comp : Node_Id;
277 CC : Node_Id;
279 begin
280 -- Processing depends on version of Ada
282 -- For Ada 95, we just renumber bits within a storage unit. We do the
283 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
284 -- Ada 83, and are free to add this extension.
286 if Ada_Version < Ada_2005 then
287 Comp := First_Component_Or_Discriminant (R);
288 while Present (Comp) loop
289 CC := Component_Clause (Comp);
291 -- If component clause is present, then deal with the non-default
292 -- bit order case for Ada 95 mode.
294 -- We only do this processing for the base type, and in fact that
295 -- is important, since otherwise if there are record subtypes, we
296 -- could reverse the bits once for each subtype, which is wrong.
298 if Present (CC) and then Ekind (R) = E_Record_Type then
299 declare
300 CFB : constant Uint := Component_Bit_Offset (Comp);
301 CSZ : constant Uint := Esize (Comp);
302 CLC : constant Node_Id := Component_Clause (Comp);
303 Pos : constant Node_Id := Position (CLC);
304 FB : constant Node_Id := First_Bit (CLC);
306 Storage_Unit_Offset : constant Uint :=
307 CFB / System_Storage_Unit;
309 Start_Bit : constant Uint :=
310 CFB mod System_Storage_Unit;
312 begin
313 -- Cases where field goes over storage unit boundary
315 if Start_Bit + CSZ > System_Storage_Unit then
317 -- Allow multi-byte field but generate warning
319 if Start_Bit mod System_Storage_Unit = 0
320 and then CSZ mod System_Storage_Unit = 0
321 then
322 Error_Msg_N
323 ("info: multi-byte field specified with "
324 & "non-standard Bit_Order?V?", CLC);
326 if Bytes_Big_Endian then
327 Error_Msg_N
328 ("\bytes are not reversed "
329 & "(component is big-endian)?V?", CLC);
330 else
331 Error_Msg_N
332 ("\bytes are not reversed "
333 & "(component is little-endian)?V?", CLC);
334 end if;
336 -- Do not allow non-contiguous field
338 else
339 Error_Msg_N
340 ("attempt to specify non-contiguous field "
341 & "not permitted", CLC);
342 Error_Msg_N
343 ("\caused by non-standard Bit_Order "
344 & "specified", CLC);
345 Error_Msg_N
346 ("\consider possibility of using "
347 & "Ada 2005 mode here", CLC);
348 end if;
350 -- Case where field fits in one storage unit
352 else
353 -- Give warning if suspicious component clause
355 if Intval (FB) >= System_Storage_Unit
356 and then Warn_On_Reverse_Bit_Order
357 then
358 Error_Msg_N
359 ("info: Bit_Order clause does not affect " &
360 "byte ordering?V?", Pos);
361 Error_Msg_Uint_1 :=
362 Intval (Pos) + Intval (FB) /
363 System_Storage_Unit;
364 Error_Msg_N
365 ("info: position normalized to ^ before bit " &
366 "order interpreted?V?", Pos);
367 end if;
369 -- Here is where we fix up the Component_Bit_Offset value
370 -- to account for the reverse bit order. Some examples of
371 -- what needs to be done are:
373 -- First_Bit .. Last_Bit Component_Bit_Offset
374 -- old new old new
376 -- 0 .. 0 7 .. 7 0 7
377 -- 0 .. 1 6 .. 7 0 6
378 -- 0 .. 2 5 .. 7 0 5
379 -- 0 .. 7 0 .. 7 0 4
381 -- 1 .. 1 6 .. 6 1 6
382 -- 1 .. 4 3 .. 6 1 3
383 -- 4 .. 7 0 .. 3 4 0
385 -- The rule is that the first bit is is obtained by
386 -- subtracting the old ending bit from storage_unit - 1.
388 Set_Component_Bit_Offset
389 (Comp,
390 (Storage_Unit_Offset * System_Storage_Unit) +
391 (System_Storage_Unit - 1) -
392 (Start_Bit + CSZ - 1));
394 Set_Normalized_First_Bit
395 (Comp,
396 Component_Bit_Offset (Comp) mod
397 System_Storage_Unit);
398 end if;
399 end;
400 end if;
402 Next_Component_Or_Discriminant (Comp);
403 end loop;
405 -- For Ada 2005, we do machine scalar processing, as fully described In
406 -- AI-133. This involves gathering all components which start at the
407 -- same byte offset and processing them together. Same approach is still
408 -- valid in later versions including Ada 2012.
410 else
411 declare
412 Max_Machine_Scalar_Size : constant Uint :=
413 UI_From_Int
414 (Standard_Long_Long_Integer_Size);
415 -- We use this as the maximum machine scalar size
417 Num_CC : Natural;
418 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
420 begin
421 -- This first loop through components does two things. First it
422 -- deals with the case of components with component clauses whose
423 -- length is greater than the maximum machine scalar size (either
424 -- accepting them or rejecting as needed). Second, it counts the
425 -- number of components with component clauses whose length does
426 -- not exceed this maximum for later processing.
428 Num_CC := 0;
429 Comp := First_Component_Or_Discriminant (R);
430 while Present (Comp) loop
431 CC := Component_Clause (Comp);
433 if Present (CC) then
434 declare
435 Fbit : constant Uint := Static_Integer (First_Bit (CC));
436 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
438 begin
439 -- Case of component with last bit >= max machine scalar
441 if Lbit >= Max_Machine_Scalar_Size then
443 -- This is allowed only if first bit is zero, and
444 -- last bit + 1 is a multiple of storage unit size.
446 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
448 -- This is the case to give a warning if enabled
450 if Warn_On_Reverse_Bit_Order then
451 Error_Msg_N
452 ("info: multi-byte field specified with "
453 & " non-standard Bit_Order?V?", CC);
455 if Bytes_Big_Endian then
456 Error_Msg_N
457 ("\bytes are not reversed "
458 & "(component is big-endian)?V?", CC);
459 else
460 Error_Msg_N
461 ("\bytes are not reversed "
462 & "(component is little-endian)?V?", CC);
463 end if;
464 end if;
466 -- Give error message for RM 13.5.1(10) violation
468 else
469 Error_Msg_FE
470 ("machine scalar rules not followed for&",
471 First_Bit (CC), Comp);
473 Error_Msg_Uint_1 := Lbit;
474 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
475 Error_Msg_F
476 ("\last bit (^) exceeds maximum machine "
477 & "scalar size (^)",
478 First_Bit (CC));
480 if (Lbit + 1) mod SSU /= 0 then
481 Error_Msg_Uint_1 := SSU;
482 Error_Msg_F
483 ("\and is not a multiple of Storage_Unit (^) "
484 & "(RM 13.4.1(10))",
485 First_Bit (CC));
487 else
488 Error_Msg_Uint_1 := Fbit;
489 Error_Msg_F
490 ("\and first bit (^) is non-zero "
491 & "(RM 13.4.1(10))",
492 First_Bit (CC));
493 end if;
494 end if;
496 -- OK case of machine scalar related component clause,
497 -- For now, just count them.
499 else
500 Num_CC := Num_CC + 1;
501 end if;
502 end;
503 end if;
505 Next_Component_Or_Discriminant (Comp);
506 end loop;
508 -- We need to sort the component clauses on the basis of the
509 -- Position values in the clause, so we can group clauses with
510 -- the same Position together to determine the relevant machine
511 -- scalar size.
513 Sort_CC : declare
514 Comps : array (0 .. Num_CC) of Entity_Id;
515 -- Array to collect component and discriminant entities. The
516 -- data starts at index 1, the 0'th entry is for the sort
517 -- routine.
519 function CP_Lt (Op1, Op2 : Natural) return Boolean;
520 -- Compare routine for Sort
522 procedure CP_Move (From : Natural; To : Natural);
523 -- Move routine for Sort
525 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
527 Start : Natural;
528 Stop : Natural;
529 -- Start and stop positions in the component list of the set of
530 -- components with the same starting position (that constitute
531 -- components in a single machine scalar).
533 MaxL : Uint;
534 -- Maximum last bit value of any component in this set
536 MSS : Uint;
537 -- Corresponding machine scalar size
539 -----------
540 -- CP_Lt --
541 -----------
543 function CP_Lt (Op1, Op2 : Natural) return Boolean is
544 begin
545 return Position (Component_Clause (Comps (Op1))) <
546 Position (Component_Clause (Comps (Op2)));
547 end CP_Lt;
549 -------------
550 -- CP_Move --
551 -------------
553 procedure CP_Move (From : Natural; To : Natural) is
554 begin
555 Comps (To) := Comps (From);
556 end CP_Move;
558 -- Start of processing for Sort_CC
560 begin
561 -- Collect the machine scalar relevant component clauses
563 Num_CC := 0;
564 Comp := First_Component_Or_Discriminant (R);
565 while Present (Comp) loop
566 declare
567 CC : constant Node_Id := Component_Clause (Comp);
569 begin
570 -- Collect only component clauses whose last bit is less
571 -- than machine scalar size. Any component clause whose
572 -- last bit exceeds this value does not take part in
573 -- machine scalar layout considerations. The test for
574 -- Error_Posted makes sure we exclude component clauses
575 -- for which we already posted an error.
577 if Present (CC)
578 and then not Error_Posted (Last_Bit (CC))
579 and then Static_Integer (Last_Bit (CC)) <
580 Max_Machine_Scalar_Size
581 then
582 Num_CC := Num_CC + 1;
583 Comps (Num_CC) := Comp;
584 end if;
585 end;
587 Next_Component_Or_Discriminant (Comp);
588 end loop;
590 -- Sort by ascending position number
592 Sorting.Sort (Num_CC);
594 -- We now have all the components whose size does not exceed
595 -- the max machine scalar value, sorted by starting position.
596 -- In this loop we gather groups of clauses starting at the
597 -- same position, to process them in accordance with AI-133.
599 Stop := 0;
600 while Stop < Num_CC loop
601 Start := Stop + 1;
602 Stop := Start;
603 MaxL :=
604 Static_Integer
605 (Last_Bit (Component_Clause (Comps (Start))));
606 while Stop < Num_CC loop
607 if Static_Integer
608 (Position (Component_Clause (Comps (Stop + 1)))) =
609 Static_Integer
610 (Position (Component_Clause (Comps (Stop))))
611 then
612 Stop := Stop + 1;
613 MaxL :=
614 UI_Max
615 (MaxL,
616 Static_Integer
617 (Last_Bit
618 (Component_Clause (Comps (Stop)))));
619 else
620 exit;
621 end if;
622 end loop;
624 -- Now we have a group of component clauses from Start to
625 -- Stop whose positions are identical, and MaxL is the
626 -- maximum last bit value of any of these components.
628 -- We need to determine the corresponding machine scalar
629 -- size. This loop assumes that machine scalar sizes are
630 -- even, and that each possible machine scalar has twice
631 -- as many bits as the next smaller one.
633 MSS := Max_Machine_Scalar_Size;
634 while MSS mod 2 = 0
635 and then (MSS / 2) >= SSU
636 and then (MSS / 2) > MaxL
637 loop
638 MSS := MSS / 2;
639 end loop;
641 -- Here is where we fix up the Component_Bit_Offset value
642 -- to account for the reverse bit order. Some examples of
643 -- what needs to be done for the case of a machine scalar
644 -- size of 8 are:
646 -- First_Bit .. Last_Bit Component_Bit_Offset
647 -- old new old new
649 -- 0 .. 0 7 .. 7 0 7
650 -- 0 .. 1 6 .. 7 0 6
651 -- 0 .. 2 5 .. 7 0 5
652 -- 0 .. 7 0 .. 7 0 4
654 -- 1 .. 1 6 .. 6 1 6
655 -- 1 .. 4 3 .. 6 1 3
656 -- 4 .. 7 0 .. 3 4 0
658 -- The rule is that the first bit is obtained by subtracting
659 -- the old ending bit from machine scalar size - 1.
661 for C in Start .. Stop loop
662 declare
663 Comp : constant Entity_Id := Comps (C);
664 CC : constant Node_Id := Component_Clause (Comp);
666 LB : constant Uint := Static_Integer (Last_Bit (CC));
667 NFB : constant Uint := MSS - Uint_1 - LB;
668 NLB : constant Uint := NFB + Esize (Comp) - 1;
669 Pos : constant Uint := Static_Integer (Position (CC));
671 begin
672 if Warn_On_Reverse_Bit_Order then
673 Error_Msg_Uint_1 := MSS;
674 Error_Msg_N
675 ("info: reverse bit order in machine " &
676 "scalar of length^?V?", First_Bit (CC));
677 Error_Msg_Uint_1 := NFB;
678 Error_Msg_Uint_2 := NLB;
680 if Bytes_Big_Endian then
681 Error_Msg_NE
682 ("\big-endian range for component "
683 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
684 else
685 Error_Msg_NE
686 ("\little-endian range for component"
687 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
688 end if;
689 end if;
691 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
692 Set_Normalized_First_Bit (Comp, NFB mod SSU);
693 end;
694 end loop;
695 end loop;
696 end Sort_CC;
697 end;
698 end if;
699 end Adjust_Record_For_Reverse_Bit_Order;
701 -------------------------------------
702 -- Alignment_Check_For_Size_Change --
703 -------------------------------------
705 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
706 begin
707 -- If the alignment is known, and not set by a rep clause, and is
708 -- inconsistent with the size being set, then reset it to unknown,
709 -- we assume in this case that the size overrides the inherited
710 -- alignment, and that the alignment must be recomputed.
712 if Known_Alignment (Typ)
713 and then not Has_Alignment_Clause (Typ)
714 and then Size mod (Alignment (Typ) * SSU) /= 0
715 then
716 Init_Alignment (Typ);
717 end if;
718 end Alignment_Check_For_Size_Change;
720 -------------------------------------
721 -- Analyze_Aspects_At_Freeze_Point --
722 -------------------------------------
724 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
725 ASN : Node_Id;
726 A_Id : Aspect_Id;
727 Ritem : Node_Id;
729 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
730 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
731 -- the aspect specification node ASN.
733 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
734 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
735 -- a derived type can inherit aspects from its parent which have been
736 -- specified at the time of the derivation using an aspect, as in:
738 -- type A is range 1 .. 10
739 -- with Size => Not_Defined_Yet;
740 -- ..
741 -- type B is new A;
742 -- ..
743 -- Not_Defined_Yet : constant := 64;
745 -- In this example, the Size of A is considered to be specified prior
746 -- to the derivation, and thus inherited, even though the value is not
747 -- known at the time of derivation. To deal with this, we use two entity
748 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
749 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
750 -- the derived type (B here). If this flag is set when the derived type
751 -- is frozen, then this procedure is called to ensure proper inheritance
752 -- of all delayed aspects from the parent type. The derived type is E,
753 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
754 -- aspect specification node in the Rep_Item chain for the parent type.
756 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
757 -- Given an aspect specification node ASN whose expression is an
758 -- optional Boolean, this routines creates the corresponding pragma
759 -- at the freezing point.
761 ----------------------------------
762 -- Analyze_Aspect_Default_Value --
763 ----------------------------------
765 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
766 Ent : constant Entity_Id := Entity (ASN);
767 Expr : constant Node_Id := Expression (ASN);
768 Id : constant Node_Id := Identifier (ASN);
770 begin
771 Error_Msg_Name_1 := Chars (Id);
773 if not Is_Type (Ent) then
774 Error_Msg_N ("aspect% can only apply to a type", Id);
775 return;
777 elsif not Is_First_Subtype (Ent) then
778 Error_Msg_N ("aspect% cannot apply to subtype", Id);
779 return;
781 elsif A_Id = Aspect_Default_Value
782 and then not Is_Scalar_Type (Ent)
783 then
784 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
785 return;
787 elsif A_Id = Aspect_Default_Component_Value then
788 if not Is_Array_Type (Ent) then
789 Error_Msg_N ("aspect% can only be applied to array type", Id);
790 return;
792 elsif not Is_Scalar_Type (Component_Type (Ent)) then
793 Error_Msg_N ("aspect% requires scalar components", Id);
794 return;
795 end if;
796 end if;
798 Set_Has_Default_Aspect (Base_Type (Ent));
800 if Is_Scalar_Type (Ent) then
801 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
802 else
803 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
804 end if;
805 end Analyze_Aspect_Default_Value;
807 ---------------------------------
808 -- Inherit_Delayed_Rep_Aspects --
809 ---------------------------------
811 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
812 P : constant Entity_Id := Entity (ASN);
813 -- Entithy for parent type
815 N : Node_Id;
816 -- Item from Rep_Item chain
818 A : Aspect_Id;
820 begin
821 -- Loop through delayed aspects for the parent type
823 N := ASN;
824 while Present (N) loop
825 if Nkind (N) = N_Aspect_Specification then
826 exit when Entity (N) /= P;
828 if Is_Delayed_Aspect (N) then
829 A := Get_Aspect_Id (Chars (Identifier (N)));
831 -- Process delayed rep aspect. For Boolean attributes it is
832 -- not possible to cancel an attribute once set (the attempt
833 -- to use an aspect with xxx => False is an error) for a
834 -- derived type. So for those cases, we do not have to check
835 -- if a clause has been given for the derived type, since it
836 -- is harmless to set it again if it is already set.
838 case A is
840 -- Alignment
842 when Aspect_Alignment =>
843 if not Has_Alignment_Clause (E) then
844 Set_Alignment (E, Alignment (P));
845 end if;
847 -- Atomic
849 when Aspect_Atomic =>
850 if Is_Atomic (P) then
851 Set_Is_Atomic (E);
852 end if;
854 -- Atomic_Components
856 when Aspect_Atomic_Components =>
857 if Has_Atomic_Components (P) then
858 Set_Has_Atomic_Components (Base_Type (E));
859 end if;
861 -- Bit_Order
863 when Aspect_Bit_Order =>
864 if Is_Record_Type (E)
865 and then No (Get_Attribute_Definition_Clause
866 (E, Attribute_Bit_Order))
867 and then Reverse_Bit_Order (P)
868 then
869 Set_Reverse_Bit_Order (Base_Type (E));
870 end if;
872 -- Component_Size
874 when Aspect_Component_Size =>
875 if Is_Array_Type (E)
876 and then not Has_Component_Size_Clause (E)
877 then
878 Set_Component_Size
879 (Base_Type (E), Component_Size (P));
880 end if;
882 -- Machine_Radix
884 when Aspect_Machine_Radix =>
885 if Is_Decimal_Fixed_Point_Type (E)
886 and then not Has_Machine_Radix_Clause (E)
887 then
888 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
889 end if;
891 -- Object_Size (also Size which also sets Object_Size)
893 when Aspect_Object_Size | Aspect_Size =>
894 if not Has_Size_Clause (E)
895 and then
896 No (Get_Attribute_Definition_Clause
897 (E, Attribute_Object_Size))
898 then
899 Set_Esize (E, Esize (P));
900 end if;
902 -- Pack
904 when Aspect_Pack =>
905 if not Is_Packed (E) then
906 Set_Is_Packed (Base_Type (E));
908 if Is_Bit_Packed_Array (P) then
909 Set_Is_Bit_Packed_Array (Base_Type (E));
910 Set_Packed_Array_Impl_Type
911 (E, Packed_Array_Impl_Type (P));
912 end if;
913 end if;
915 -- Scalar_Storage_Order
917 when Aspect_Scalar_Storage_Order =>
918 if (Is_Record_Type (E) or else Is_Array_Type (E))
919 and then No (Get_Attribute_Definition_Clause
920 (E, Attribute_Scalar_Storage_Order))
921 and then Reverse_Storage_Order (P)
922 then
923 Set_Reverse_Storage_Order (Base_Type (E));
925 -- Clear default SSO indications, since the aspect
926 -- overrides the default.
928 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
929 Set_SSO_Set_High_By_Default (Base_Type (E), False);
930 end if;
932 -- Small
934 when Aspect_Small =>
935 if Is_Fixed_Point_Type (E)
936 and then not Has_Small_Clause (E)
937 then
938 Set_Small_Value (E, Small_Value (P));
939 end if;
941 -- Storage_Size
943 when Aspect_Storage_Size =>
944 if (Is_Access_Type (E) or else Is_Task_Type (E))
945 and then not Has_Storage_Size_Clause (E)
946 then
947 Set_Storage_Size_Variable
948 (Base_Type (E), Storage_Size_Variable (P));
949 end if;
951 -- Value_Size
953 when Aspect_Value_Size =>
955 -- Value_Size is never inherited, it is either set by
956 -- default, or it is explicitly set for the derived
957 -- type. So nothing to do here.
959 null;
961 -- Volatile
963 when Aspect_Volatile =>
964 if Is_Volatile (P) then
965 Set_Is_Volatile (E);
966 end if;
968 -- Volatile_Components
970 when Aspect_Volatile_Components =>
971 if Has_Volatile_Components (P) then
972 Set_Has_Volatile_Components (Base_Type (E));
973 end if;
975 -- That should be all the Rep Aspects
977 when others =>
978 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
979 null;
981 end case;
982 end if;
983 end if;
985 N := Next_Rep_Item (N);
986 end loop;
987 end Inherit_Delayed_Rep_Aspects;
989 -------------------------------------
990 -- Make_Pragma_From_Boolean_Aspect --
991 -------------------------------------
993 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
994 Ident : constant Node_Id := Identifier (ASN);
995 A_Name : constant Name_Id := Chars (Ident);
996 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
997 Ent : constant Entity_Id := Entity (ASN);
998 Expr : constant Node_Id := Expression (ASN);
999 Loc : constant Source_Ptr := Sloc (ASN);
1001 Prag : Node_Id;
1003 procedure Check_False_Aspect_For_Derived_Type;
1004 -- This procedure checks for the case of a false aspect for a derived
1005 -- type, which improperly tries to cancel an aspect inherited from
1006 -- the parent.
1008 -----------------------------------------
1009 -- Check_False_Aspect_For_Derived_Type --
1010 -----------------------------------------
1012 procedure Check_False_Aspect_For_Derived_Type is
1013 Par : Node_Id;
1015 begin
1016 -- We are only checking derived types
1018 if not Is_Derived_Type (E) then
1019 return;
1020 end if;
1022 Par := Nearest_Ancestor (E);
1024 case A_Id is
1025 when Aspect_Atomic | Aspect_Shared =>
1026 if not Is_Atomic (Par) then
1027 return;
1028 end if;
1030 when Aspect_Atomic_Components =>
1031 if not Has_Atomic_Components (Par) then
1032 return;
1033 end if;
1035 when Aspect_Discard_Names =>
1036 if not Discard_Names (Par) then
1037 return;
1038 end if;
1040 when Aspect_Pack =>
1041 if not Is_Packed (Par) then
1042 return;
1043 end if;
1045 when Aspect_Unchecked_Union =>
1046 if not Is_Unchecked_Union (Par) then
1047 return;
1048 end if;
1050 when Aspect_Volatile =>
1051 if not Is_Volatile (Par) then
1052 return;
1053 end if;
1055 when Aspect_Volatile_Components =>
1056 if not Has_Volatile_Components (Par) then
1057 return;
1058 end if;
1060 when others =>
1061 return;
1062 end case;
1064 -- Fall through means we are canceling an inherited aspect
1066 Error_Msg_Name_1 := A_Name;
1067 Error_Msg_NE
1068 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1070 end Check_False_Aspect_For_Derived_Type;
1072 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1074 begin
1075 -- Note that we know Expr is present, because for a missing Expr
1076 -- argument, we knew it was True and did not need to delay the
1077 -- evaluation to the freeze point.
1079 if Is_False (Static_Boolean (Expr)) then
1080 Check_False_Aspect_For_Derived_Type;
1082 else
1083 Prag :=
1084 Make_Pragma (Loc,
1085 Pragma_Argument_Associations => New_List (
1086 Make_Pragma_Argument_Association (Sloc (Ident),
1087 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1089 Pragma_Identifier =>
1090 Make_Identifier (Sloc (Ident), Chars (Ident)));
1092 Set_From_Aspect_Specification (Prag, True);
1093 Set_Corresponding_Aspect (Prag, ASN);
1094 Set_Aspect_Rep_Item (ASN, Prag);
1095 Set_Is_Delayed_Aspect (Prag);
1096 Set_Parent (Prag, ASN);
1097 end if;
1098 end Make_Pragma_From_Boolean_Aspect;
1100 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1102 begin
1103 -- Must be visible in current scope
1105 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1106 return;
1107 end if;
1109 -- Look for aspect specification entries for this entity
1111 ASN := First_Rep_Item (E);
1112 while Present (ASN) loop
1113 if Nkind (ASN) = N_Aspect_Specification then
1114 exit when Entity (ASN) /= E;
1116 if Is_Delayed_Aspect (ASN) then
1117 A_Id := Get_Aspect_Id (ASN);
1119 case A_Id is
1121 -- For aspects whose expression is an optional Boolean, make
1122 -- the corresponding pragma at the freeze point.
1124 when Boolean_Aspects |
1125 Library_Unit_Aspects =>
1126 Make_Pragma_From_Boolean_Aspect (ASN);
1128 -- Special handling for aspects that don't correspond to
1129 -- pragmas/attributes.
1131 when Aspect_Default_Value |
1132 Aspect_Default_Component_Value =>
1133 Analyze_Aspect_Default_Value (ASN);
1135 -- Ditto for iterator aspects, because the corresponding
1136 -- attributes may not have been analyzed yet.
1138 when Aspect_Constant_Indexing |
1139 Aspect_Variable_Indexing |
1140 Aspect_Default_Iterator |
1141 Aspect_Iterator_Element =>
1142 Analyze (Expression (ASN));
1144 if Etype (Expression (ASN)) = Any_Type then
1145 Error_Msg_NE
1146 ("\aspect must be fully defined before & is frozen",
1147 ASN, E);
1148 end if;
1150 when Aspect_Iterable =>
1151 Validate_Iterable_Aspect (E, ASN);
1153 when others =>
1154 null;
1155 end case;
1157 Ritem := Aspect_Rep_Item (ASN);
1159 if Present (Ritem) then
1160 Analyze (Ritem);
1161 end if;
1162 end if;
1163 end if;
1165 Next_Rep_Item (ASN);
1166 end loop;
1168 -- This is where we inherit delayed rep aspects from our parent. Note
1169 -- that if we fell out of the above loop with ASN non-empty, it means
1170 -- we hit an aspect for an entity other than E, and it must be the
1171 -- type from which we were derived.
1173 if May_Inherit_Delayed_Rep_Aspects (E) then
1174 Inherit_Delayed_Rep_Aspects (ASN);
1175 end if;
1176 end Analyze_Aspects_At_Freeze_Point;
1178 -----------------------------------
1179 -- Analyze_Aspect_Specifications --
1180 -----------------------------------
1182 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1183 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1184 -- Establish linkages between an aspect and its corresponding
1185 -- pragma.
1187 procedure Insert_After_SPARK_Mode
1188 (Prag : Node_Id;
1189 Ins_Nod : Node_Id;
1190 Decls : List_Id);
1191 -- Subsidiary to the analysis of aspects Abstract_State, Ghost,
1192 -- Initializes, Initial_Condition and Refined_State. Insert node Prag
1193 -- before node Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip
1194 -- SPARK_Mode. Decls is the associated declarative list where Prag is to
1195 -- reside.
1197 procedure Insert_Pragma (Prag : Node_Id);
1198 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1199 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1200 -- Insert pragma Prag such that it mimics the placement of a source
1201 -- pragma of the same kind.
1203 -- procedure Proc (Formal : ...) with Global => ...;
1205 -- procedure Proc (Formal : ...);
1206 -- pragma Global (...);
1208 --------------
1209 -- Decorate --
1210 --------------
1212 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1213 begin
1214 Set_Aspect_Rep_Item (Asp, Prag);
1215 Set_Corresponding_Aspect (Prag, Asp);
1216 Set_From_Aspect_Specification (Prag);
1217 Set_Parent (Prag, Asp);
1218 end Decorate;
1220 -----------------------------
1221 -- Insert_After_SPARK_Mode --
1222 -----------------------------
1224 procedure Insert_After_SPARK_Mode
1225 (Prag : Node_Id;
1226 Ins_Nod : Node_Id;
1227 Decls : List_Id)
1229 Decl : Node_Id := Ins_Nod;
1231 begin
1232 -- Skip SPARK_Mode
1234 if Present (Decl)
1235 and then Nkind (Decl) = N_Pragma
1236 and then Pragma_Name (Decl) = Name_SPARK_Mode
1237 then
1238 Decl := Next (Decl);
1239 end if;
1241 if Present (Decl) then
1242 Insert_Before (Decl, Prag);
1244 -- Aitem acts as the last declaration
1246 else
1247 Append_To (Decls, Prag);
1248 end if;
1249 end Insert_After_SPARK_Mode;
1251 -------------------
1252 -- Insert_Pragma --
1253 -------------------
1255 procedure Insert_Pragma (Prag : Node_Id) is
1256 Aux : Node_Id;
1257 Decl : Node_Id;
1259 begin
1260 -- When the context is a library unit, the pragma is added to the
1261 -- Pragmas_After list.
1263 if Nkind (Parent (N)) = N_Compilation_Unit then
1264 Aux := Aux_Decls_Node (Parent (N));
1266 if No (Pragmas_After (Aux)) then
1267 Set_Pragmas_After (Aux, New_List);
1268 end if;
1270 Prepend (Prag, Pragmas_After (Aux));
1272 -- Pragmas associated with subprogram bodies are inserted in the
1273 -- declarative part.
1275 elsif Nkind (N) = N_Subprogram_Body then
1276 if Present (Declarations (N)) then
1278 -- Skip other internally generated pragmas from aspects to find
1279 -- the proper insertion point. As a result the order of pragmas
1280 -- is the same as the order of aspects.
1282 -- As precondition pragmas generated from conjuncts in the
1283 -- precondition aspect are presented in reverse order to
1284 -- Insert_Pragma, insert them in the correct order here by not
1285 -- skipping previously inserted precondition pragmas when the
1286 -- current pragma is a precondition.
1288 Decl := First (Declarations (N));
1289 while Present (Decl) loop
1290 if Nkind (Decl) = N_Pragma
1291 and then From_Aspect_Specification (Decl)
1292 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1293 and then
1294 Get_Pragma_Id (Prag) = Pragma_Precondition)
1295 then
1296 Next (Decl);
1297 else
1298 exit;
1299 end if;
1300 end loop;
1302 if Present (Decl) then
1303 Insert_Before (Decl, Prag);
1304 else
1305 Append (Prag, Declarations (N));
1306 end if;
1307 else
1308 Set_Declarations (N, New_List (Prag));
1309 end if;
1311 -- Default
1313 else
1314 Insert_After (N, Prag);
1315 end if;
1316 end Insert_Pragma;
1318 -- Local variables
1320 Aspect : Node_Id;
1321 Aitem : Node_Id;
1322 Ent : Node_Id;
1324 L : constant List_Id := Aspect_Specifications (N);
1326 Ins_Node : Node_Id := N;
1327 -- Insert pragmas/attribute definition clause after this node when no
1328 -- delayed analysis is required.
1330 -- Start of processing for Analyze_Aspect_Specifications
1332 -- The general processing involves building an attribute definition
1333 -- clause or a pragma node that corresponds to the aspect. Then in order
1334 -- to delay the evaluation of this aspect to the freeze point, we attach
1335 -- the corresponding pragma/attribute definition clause to the aspect
1336 -- specification node, which is then placed in the Rep Item chain. In
1337 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1338 -- and we evaluate the rep item at the freeze point. When the aspect
1339 -- doesn't have a corresponding pragma/attribute definition clause, then
1340 -- its analysis is simply delayed at the freeze point.
1342 -- Some special cases don't require delay analysis, thus the aspect is
1343 -- analyzed right now.
1345 -- Note that there is a special handling for Pre, Post, Test_Case,
1346 -- Contract_Cases aspects. In these cases, we do not have to worry
1347 -- about delay issues, since the pragmas themselves deal with delay
1348 -- of visibility for the expression analysis. Thus, we just insert
1349 -- the pragma after the node N.
1351 begin
1352 pragma Assert (Present (L));
1354 -- Loop through aspects
1356 Aspect := First (L);
1357 Aspect_Loop : while Present (Aspect) loop
1358 Analyze_One_Aspect : declare
1359 Expr : constant Node_Id := Expression (Aspect);
1360 Id : constant Node_Id := Identifier (Aspect);
1361 Loc : constant Source_Ptr := Sloc (Aspect);
1362 Nam : constant Name_Id := Chars (Id);
1363 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1364 Anod : Node_Id;
1366 Delay_Required : Boolean;
1367 -- Set False if delay is not required
1369 Eloc : Source_Ptr := No_Location;
1370 -- Source location of expression, modified when we split PPC's. It
1371 -- is set below when Expr is present.
1373 procedure Analyze_Aspect_External_Or_Link_Name;
1374 -- Perform analysis of the External_Name or Link_Name aspects
1376 procedure Analyze_Aspect_Implicit_Dereference;
1377 -- Perform analysis of the Implicit_Dereference aspects
1379 procedure Make_Aitem_Pragma
1380 (Pragma_Argument_Associations : List_Id;
1381 Pragma_Name : Name_Id);
1382 -- This is a wrapper for Make_Pragma used for converting aspects
1383 -- to pragmas. It takes care of Sloc (set from Loc) and building
1384 -- the pragma identifier from the given name. In addition the
1385 -- flags Class_Present and Split_PPC are set from the aspect
1386 -- node, as well as Is_Ignored. This routine also sets the
1387 -- From_Aspect_Specification in the resulting pragma node to
1388 -- True, and sets Corresponding_Aspect to point to the aspect.
1389 -- The resulting pragma is assigned to Aitem.
1391 ------------------------------------------
1392 -- Analyze_Aspect_External_Or_Link_Name --
1393 ------------------------------------------
1395 procedure Analyze_Aspect_External_Or_Link_Name is
1396 begin
1397 -- Verify that there is an Import/Export aspect defined for the
1398 -- entity. The processing of that aspect in turn checks that
1399 -- there is a Convention aspect declared. The pragma is
1400 -- constructed when processing the Convention aspect.
1402 declare
1403 A : Node_Id;
1405 begin
1406 A := First (L);
1407 while Present (A) loop
1408 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1409 Name_Import);
1410 Next (A);
1411 end loop;
1413 if No (A) then
1414 Error_Msg_N
1415 ("missing Import/Export for Link/External name",
1416 Aspect);
1417 end if;
1418 end;
1419 end Analyze_Aspect_External_Or_Link_Name;
1421 -----------------------------------------
1422 -- Analyze_Aspect_Implicit_Dereference --
1423 -----------------------------------------
1425 procedure Analyze_Aspect_Implicit_Dereference is
1426 begin
1427 if not Is_Type (E) or else not Has_Discriminants (E) then
1428 Error_Msg_N
1429 ("aspect must apply to a type with discriminants", N);
1431 else
1432 declare
1433 Disc : Entity_Id;
1435 begin
1436 Disc := First_Discriminant (E);
1437 while Present (Disc) loop
1438 if Chars (Expr) = Chars (Disc)
1439 and then Ekind (Etype (Disc)) =
1440 E_Anonymous_Access_Type
1441 then
1442 Set_Has_Implicit_Dereference (E);
1443 Set_Has_Implicit_Dereference (Disc);
1444 return;
1445 end if;
1447 Next_Discriminant (Disc);
1448 end loop;
1450 -- Error if no proper access discriminant.
1452 Error_Msg_NE
1453 ("not an access discriminant of&", Expr, E);
1454 end;
1455 end if;
1456 end Analyze_Aspect_Implicit_Dereference;
1458 -----------------------
1459 -- Make_Aitem_Pragma --
1460 -----------------------
1462 procedure Make_Aitem_Pragma
1463 (Pragma_Argument_Associations : List_Id;
1464 Pragma_Name : Name_Id)
1466 Args : List_Id := Pragma_Argument_Associations;
1468 begin
1469 -- We should never get here if aspect was disabled
1471 pragma Assert (not Is_Disabled (Aspect));
1473 -- Certain aspects allow for an optional name or expression. Do
1474 -- not generate a pragma with empty argument association list.
1476 if No (Args) or else No (Expression (First (Args))) then
1477 Args := No_List;
1478 end if;
1480 -- Build the pragma
1482 Aitem :=
1483 Make_Pragma (Loc,
1484 Pragma_Argument_Associations => Args,
1485 Pragma_Identifier =>
1486 Make_Identifier (Sloc (Id), Pragma_Name),
1487 Class_Present => Class_Present (Aspect),
1488 Split_PPC => Split_PPC (Aspect));
1490 -- Set additional semantic fields
1492 if Is_Ignored (Aspect) then
1493 Set_Is_Ignored (Aitem);
1494 elsif Is_Checked (Aspect) then
1495 Set_Is_Checked (Aitem);
1496 end if;
1498 Set_Corresponding_Aspect (Aitem, Aspect);
1499 Set_From_Aspect_Specification (Aitem, True);
1500 end Make_Aitem_Pragma;
1502 -- Start of processing for Analyze_One_Aspect
1504 begin
1505 -- Skip aspect if already analyzed, to avoid looping in some cases
1507 if Analyzed (Aspect) then
1508 goto Continue;
1509 end if;
1511 -- Skip looking at aspect if it is totally disabled. Just mark it
1512 -- as such for later reference in the tree. This also sets the
1513 -- Is_Ignored and Is_Checked flags appropriately.
1515 Check_Applicable_Policy (Aspect);
1517 if Is_Disabled (Aspect) then
1518 goto Continue;
1519 end if;
1521 -- Set the source location of expression, used in the case of
1522 -- a failed precondition/postcondition or invariant. Note that
1523 -- the source location of the expression is not usually the best
1524 -- choice here. For example, it gets located on the last AND
1525 -- keyword in a chain of boolean expressiond AND'ed together.
1526 -- It is best to put the message on the first character of the
1527 -- assertion, which is the effect of the First_Node call here.
1529 if Present (Expr) then
1530 Eloc := Sloc (First_Node (Expr));
1531 end if;
1533 -- Check restriction No_Implementation_Aspect_Specifications
1535 if Implementation_Defined_Aspect (A_Id) then
1536 Check_Restriction
1537 (No_Implementation_Aspect_Specifications, Aspect);
1538 end if;
1540 -- Check restriction No_Specification_Of_Aspect
1542 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1544 -- Mark aspect analyzed (actual analysis is delayed till later)
1546 Set_Analyzed (Aspect);
1547 Set_Entity (Aspect, E);
1548 Ent := New_Occurrence_Of (E, Sloc (Id));
1550 -- Check for duplicate aspect. Note that the Comes_From_Source
1551 -- test allows duplicate Pre/Post's that we generate internally
1552 -- to escape being flagged here.
1554 if No_Duplicates_Allowed (A_Id) then
1555 Anod := First (L);
1556 while Anod /= Aspect loop
1557 if Comes_From_Source (Aspect)
1558 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1559 then
1560 Error_Msg_Name_1 := Nam;
1561 Error_Msg_Sloc := Sloc (Anod);
1563 -- Case of same aspect specified twice
1565 if Class_Present (Anod) = Class_Present (Aspect) then
1566 if not Class_Present (Anod) then
1567 Error_Msg_NE
1568 ("aspect% for & previously given#",
1569 Id, E);
1570 else
1571 Error_Msg_NE
1572 ("aspect `%''Class` for & previously given#",
1573 Id, E);
1574 end if;
1575 end if;
1576 end if;
1578 Next (Anod);
1579 end loop;
1580 end if;
1582 -- Check some general restrictions on language defined aspects
1584 if not Implementation_Defined_Aspect (A_Id) then
1585 Error_Msg_Name_1 := Nam;
1587 -- Not allowed for renaming declarations
1589 if Nkind (N) in N_Renaming_Declaration then
1590 Error_Msg_N
1591 ("aspect % not allowed for renaming declaration",
1592 Aspect);
1593 end if;
1595 -- Not allowed for formal type declarations
1597 if Nkind (N) = N_Formal_Type_Declaration then
1598 Error_Msg_N
1599 ("aspect % not allowed for formal type declaration",
1600 Aspect);
1601 end if;
1602 end if;
1604 -- Copy expression for later processing by the procedures
1605 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1607 Set_Entity (Id, New_Copy_Tree (Expr));
1609 -- Set Delay_Required as appropriate to aspect
1611 case Aspect_Delay (A_Id) is
1612 when Always_Delay =>
1613 Delay_Required := True;
1615 when Never_Delay =>
1616 Delay_Required := False;
1618 when Rep_Aspect =>
1620 -- If expression has the form of an integer literal, then
1621 -- do not delay, since we know the value cannot change.
1622 -- This optimization catches most rep clause cases.
1624 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1625 or else (A_Id in Boolean_Aspects and then No (Expr))
1626 then
1627 Delay_Required := False;
1628 else
1629 Delay_Required := True;
1630 Set_Has_Delayed_Rep_Aspects (E);
1631 end if;
1632 end case;
1634 -- Processing based on specific aspect
1636 case A_Id is
1638 -- No_Aspect should be impossible
1640 when No_Aspect =>
1641 raise Program_Error;
1643 -- Case 1: Aspects corresponding to attribute definition
1644 -- clauses.
1646 when Aspect_Address |
1647 Aspect_Alignment |
1648 Aspect_Bit_Order |
1649 Aspect_Component_Size |
1650 Aspect_Constant_Indexing |
1651 Aspect_Default_Iterator |
1652 Aspect_Dispatching_Domain |
1653 Aspect_External_Tag |
1654 Aspect_Input |
1655 Aspect_Iterable |
1656 Aspect_Iterator_Element |
1657 Aspect_Machine_Radix |
1658 Aspect_Object_Size |
1659 Aspect_Output |
1660 Aspect_Read |
1661 Aspect_Scalar_Storage_Order |
1662 Aspect_Size |
1663 Aspect_Small |
1664 Aspect_Simple_Storage_Pool |
1665 Aspect_Storage_Pool |
1666 Aspect_Stream_Size |
1667 Aspect_Value_Size |
1668 Aspect_Variable_Indexing |
1669 Aspect_Write =>
1671 -- Indexing aspects apply only to tagged type
1673 if (A_Id = Aspect_Constant_Indexing
1674 or else
1675 A_Id = Aspect_Variable_Indexing)
1676 and then not (Is_Type (E)
1677 and then Is_Tagged_Type (E))
1678 then
1679 Error_Msg_N
1680 ("indexing aspect can only apply to a tagged type",
1681 Aspect);
1682 goto Continue;
1683 end if;
1685 -- For the case of aspect Address, we don't consider that we
1686 -- know the entity is never set in the source, since it is
1687 -- is likely aliasing is occurring.
1689 -- Note: one might think that the analysis of the resulting
1690 -- attribute definition clause would take care of that, but
1691 -- that's not the case since it won't be from source.
1693 if A_Id = Aspect_Address then
1694 Set_Never_Set_In_Source (E, False);
1695 end if;
1697 -- Correctness of the profile of a stream operation is
1698 -- verified at the freeze point, but we must detect the
1699 -- illegal specification of this aspect for a subtype now,
1700 -- to prevent malformed rep_item chains.
1702 if A_Id = Aspect_Input or else
1703 A_Id = Aspect_Output or else
1704 A_Id = Aspect_Read or else
1705 A_Id = Aspect_Write
1706 then
1707 if not Is_First_Subtype (E) then
1708 Error_Msg_N
1709 ("local name must be a first subtype", Aspect);
1710 goto Continue;
1712 -- If stream aspect applies to the class-wide type,
1713 -- the generated attribute definition applies to the
1714 -- class-wide type as well.
1716 elsif Class_Present (Aspect) then
1717 Ent :=
1718 Make_Attribute_Reference (Loc,
1719 Prefix => Ent,
1720 Attribute_Name => Name_Class);
1721 end if;
1722 end if;
1724 -- Construct the attribute definition clause
1726 Aitem :=
1727 Make_Attribute_Definition_Clause (Loc,
1728 Name => Ent,
1729 Chars => Chars (Id),
1730 Expression => Relocate_Node (Expr));
1732 -- If the address is specified, then we treat the entity as
1733 -- referenced, to avoid spurious warnings. This is analogous
1734 -- to what is done with an attribute definition clause, but
1735 -- here we don't want to generate a reference because this
1736 -- is the point of definition of the entity.
1738 if A_Id = Aspect_Address then
1739 Set_Referenced (E);
1740 end if;
1742 -- Case 2: Aspects corresponding to pragmas
1744 -- Case 2a: Aspects corresponding to pragmas with two
1745 -- arguments, where the first argument is a local name
1746 -- referring to the entity, and the second argument is the
1747 -- aspect definition expression.
1749 -- Linker_Section/Suppress/Unsuppress
1751 when Aspect_Linker_Section |
1752 Aspect_Suppress |
1753 Aspect_Unsuppress =>
1755 Make_Aitem_Pragma
1756 (Pragma_Argument_Associations => New_List (
1757 Make_Pragma_Argument_Association (Loc,
1758 Expression => New_Occurrence_Of (E, Loc)),
1759 Make_Pragma_Argument_Association (Sloc (Expr),
1760 Expression => Relocate_Node (Expr))),
1761 Pragma_Name => Chars (Id));
1763 -- Synchronization
1765 -- Corresponds to pragma Implemented, construct the pragma
1767 when Aspect_Synchronization =>
1768 Make_Aitem_Pragma
1769 (Pragma_Argument_Associations => New_List (
1770 Make_Pragma_Argument_Association (Loc,
1771 Expression => New_Occurrence_Of (E, Loc)),
1772 Make_Pragma_Argument_Association (Sloc (Expr),
1773 Expression => Relocate_Node (Expr))),
1774 Pragma_Name => Name_Implemented);
1776 -- Attach_Handler
1778 when Aspect_Attach_Handler =>
1779 Make_Aitem_Pragma
1780 (Pragma_Argument_Associations => New_List (
1781 Make_Pragma_Argument_Association (Sloc (Ent),
1782 Expression => Ent),
1783 Make_Pragma_Argument_Association (Sloc (Expr),
1784 Expression => Relocate_Node (Expr))),
1785 Pragma_Name => Name_Attach_Handler);
1787 -- We need to insert this pragma into the tree to get proper
1788 -- processing and to look valid from a placement viewpoint.
1790 Insert_Pragma (Aitem);
1791 goto Continue;
1793 -- Dynamic_Predicate, Predicate, Static_Predicate
1795 when Aspect_Dynamic_Predicate |
1796 Aspect_Predicate |
1797 Aspect_Static_Predicate =>
1799 -- These aspects apply only to subtypes
1801 if not Is_Type (E) then
1802 Error_Msg_N
1803 ("predicate can only be specified for a subtype",
1804 Aspect);
1805 goto Continue;
1807 elsif Is_Incomplete_Type (E) then
1808 Error_Msg_N
1809 ("predicate cannot apply to incomplete view", Aspect);
1810 goto Continue;
1811 end if;
1813 -- Construct the pragma (always a pragma Predicate, with
1814 -- flags recording whether it is static/dynamic). We also
1815 -- set flags recording this in the type itself.
1817 Make_Aitem_Pragma
1818 (Pragma_Argument_Associations => New_List (
1819 Make_Pragma_Argument_Association (Sloc (Ent),
1820 Expression => Ent),
1821 Make_Pragma_Argument_Association (Sloc (Expr),
1822 Expression => Relocate_Node (Expr))),
1823 Pragma_Name => Name_Predicate);
1825 -- Mark type has predicates, and remember what kind of
1826 -- aspect lead to this predicate (we need this to access
1827 -- the right set of check policies later on).
1829 Set_Has_Predicates (E);
1831 if A_Id = Aspect_Dynamic_Predicate then
1832 Set_Has_Dynamic_Predicate_Aspect (E);
1833 elsif A_Id = Aspect_Static_Predicate then
1834 Set_Has_Static_Predicate_Aspect (E);
1835 end if;
1837 -- If the type is private, indicate that its completion
1838 -- has a freeze node, because that is the one that will
1839 -- be visible at freeze time.
1841 if Is_Private_Type (E) and then Present (Full_View (E)) then
1842 Set_Has_Predicates (Full_View (E));
1844 if A_Id = Aspect_Dynamic_Predicate then
1845 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1846 elsif A_Id = Aspect_Static_Predicate then
1847 Set_Has_Static_Predicate_Aspect (Full_View (E));
1848 end if;
1850 Set_Has_Delayed_Aspects (Full_View (E));
1851 Ensure_Freeze_Node (Full_View (E));
1852 end if;
1854 -- Case 2b: Aspects corresponding to pragmas with two
1855 -- arguments, where the second argument is a local name
1856 -- referring to the entity, and the first argument is the
1857 -- aspect definition expression.
1859 -- Convention
1861 when Aspect_Convention =>
1863 -- The aspect may be part of the specification of an import
1864 -- or export pragma. Scan the aspect list to gather the
1865 -- other components, if any. The name of the generated
1866 -- pragma is one of Convention/Import/Export.
1868 declare
1869 Args : constant List_Id := New_List (
1870 Make_Pragma_Argument_Association (Sloc (Expr),
1871 Expression => Relocate_Node (Expr)),
1872 Make_Pragma_Argument_Association (Sloc (Ent),
1873 Expression => Ent));
1875 Imp_Exp_Seen : Boolean := False;
1876 -- Flag set when aspect Import or Export has been seen
1878 Imp_Seen : Boolean := False;
1879 -- Flag set when aspect Import has been seen
1881 Asp : Node_Id;
1882 Asp_Nam : Name_Id;
1883 Extern_Arg : Node_Id;
1884 Link_Arg : Node_Id;
1885 Prag_Nam : Name_Id;
1887 begin
1888 Extern_Arg := Empty;
1889 Link_Arg := Empty;
1890 Prag_Nam := Chars (Id);
1892 Asp := First (L);
1893 while Present (Asp) loop
1894 Asp_Nam := Chars (Identifier (Asp));
1896 -- Aspects Import and Export take precedence over
1897 -- aspect Convention. As a result the generated pragma
1898 -- must carry the proper interfacing aspect's name.
1900 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1901 if Imp_Exp_Seen then
1902 Error_Msg_N ("conflicting", Asp);
1903 else
1904 Imp_Exp_Seen := True;
1906 if Asp_Nam = Name_Import then
1907 Imp_Seen := True;
1908 end if;
1909 end if;
1911 Prag_Nam := Asp_Nam;
1913 -- Aspect External_Name adds an extra argument to the
1914 -- generated pragma.
1916 elsif Asp_Nam = Name_External_Name then
1917 Extern_Arg :=
1918 Make_Pragma_Argument_Association (Loc,
1919 Chars => Asp_Nam,
1920 Expression => Relocate_Node (Expression (Asp)));
1922 -- Aspect Link_Name adds an extra argument to the
1923 -- generated pragma.
1925 elsif Asp_Nam = Name_Link_Name then
1926 Link_Arg :=
1927 Make_Pragma_Argument_Association (Loc,
1928 Chars => Asp_Nam,
1929 Expression => Relocate_Node (Expression (Asp)));
1930 end if;
1932 Next (Asp);
1933 end loop;
1935 -- Assemble the full argument list
1937 if Present (Extern_Arg) then
1938 Append_To (Args, Extern_Arg);
1939 end if;
1941 if Present (Link_Arg) then
1942 Append_To (Args, Link_Arg);
1943 end if;
1945 Make_Aitem_Pragma
1946 (Pragma_Argument_Associations => Args,
1947 Pragma_Name => Prag_Nam);
1949 -- Store the generated pragma Import in the related
1950 -- subprogram.
1952 if Imp_Seen and then Is_Subprogram (E) then
1953 Set_Import_Pragma (E, Aitem);
1954 end if;
1955 end;
1957 -- CPU, Interrupt_Priority, Priority
1959 -- These three aspects can be specified for a subprogram spec
1960 -- or body, in which case we analyze the expression and export
1961 -- the value of the aspect.
1963 -- Previously, we generated an equivalent pragma for bodies
1964 -- (note that the specs cannot contain these pragmas). The
1965 -- pragma was inserted ahead of local declarations, rather than
1966 -- after the body. This leads to a certain duplication between
1967 -- the processing performed for the aspect and the pragma, but
1968 -- given the straightforward handling required it is simpler
1969 -- to duplicate than to translate the aspect in the spec into
1970 -- a pragma in the declarative part of the body.
1972 when Aspect_CPU |
1973 Aspect_Interrupt_Priority |
1974 Aspect_Priority =>
1976 if Nkind_In (N, N_Subprogram_Body,
1977 N_Subprogram_Declaration)
1978 then
1979 -- Analyze the aspect expression
1981 Analyze_And_Resolve (Expr, Standard_Integer);
1983 -- Interrupt_Priority aspect not allowed for main
1984 -- subprograms. ARM D.1 does not forbid this explicitly,
1985 -- but ARM J.15.11 (6/3) does not permit pragma
1986 -- Interrupt_Priority for subprograms.
1988 if A_Id = Aspect_Interrupt_Priority then
1989 Error_Msg_N
1990 ("Interrupt_Priority aspect cannot apply to "
1991 & "subprogram", Expr);
1993 -- The expression must be static
1995 elsif not Is_OK_Static_Expression (Expr) then
1996 Flag_Non_Static_Expr
1997 ("aspect requires static expression!", Expr);
1999 -- Check whether this is the main subprogram. Issue a
2000 -- warning only if it is obviously not a main program
2001 -- (when it has parameters or when the subprogram is
2002 -- within a package).
2004 elsif Present (Parameter_Specifications
2005 (Specification (N)))
2006 or else not Is_Compilation_Unit (Defining_Entity (N))
2007 then
2008 -- See ARM D.1 (14/3) and D.16 (12/3)
2010 Error_Msg_N
2011 ("aspect applied to subprogram other than the "
2012 & "main subprogram has no effect??", Expr);
2014 -- Otherwise check in range and export the value
2016 -- For the CPU aspect
2018 elsif A_Id = Aspect_CPU then
2019 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2021 -- Value is correct so we export the value to make
2022 -- it available at execution time.
2024 Set_Main_CPU
2025 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2027 else
2028 Error_Msg_N
2029 ("main subprogram CPU is out of range", Expr);
2030 end if;
2032 -- For the Priority aspect
2034 elsif A_Id = Aspect_Priority then
2035 if Is_In_Range (Expr, RTE (RE_Priority)) then
2037 -- Value is correct so we export the value to make
2038 -- it available at execution time.
2040 Set_Main_Priority
2041 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2043 -- Ignore pragma if Relaxed_RM_Semantics to support
2044 -- other targets/non GNAT compilers.
2046 elsif not Relaxed_RM_Semantics then
2047 Error_Msg_N
2048 ("main subprogram priority is out of range",
2049 Expr);
2050 end if;
2051 end if;
2053 -- Load an arbitrary entity from System.Tasking.Stages
2054 -- or System.Tasking.Restricted.Stages (depending on
2055 -- the supported profile) to make sure that one of these
2056 -- packages is implicitly with'ed, since we need to have
2057 -- the tasking run time active for the pragma Priority to
2058 -- have any effect. Previously we with'ed the package
2059 -- System.Tasking, but this package does not trigger the
2060 -- required initialization of the run-time library.
2062 declare
2063 Discard : Entity_Id;
2064 begin
2065 if Restricted_Profile then
2066 Discard := RTE (RE_Activate_Restricted_Tasks);
2067 else
2068 Discard := RTE (RE_Activate_Tasks);
2069 end if;
2070 end;
2072 -- Handling for these Aspects in subprograms is complete
2074 goto Continue;
2076 -- For tasks
2078 else
2079 -- Pass the aspect as an attribute
2081 Aitem :=
2082 Make_Attribute_Definition_Clause (Loc,
2083 Name => Ent,
2084 Chars => Chars (Id),
2085 Expression => Relocate_Node (Expr));
2086 end if;
2088 -- Warnings
2090 when Aspect_Warnings =>
2091 Make_Aitem_Pragma
2092 (Pragma_Argument_Associations => New_List (
2093 Make_Pragma_Argument_Association (Sloc (Expr),
2094 Expression => Relocate_Node (Expr)),
2095 Make_Pragma_Argument_Association (Loc,
2096 Expression => New_Occurrence_Of (E, Loc))),
2097 Pragma_Name => Chars (Id));
2099 -- Case 2c: Aspects corresponding to pragmas with three
2100 -- arguments.
2102 -- Invariant aspects have a first argument that references the
2103 -- entity, a second argument that is the expression and a third
2104 -- argument that is an appropriate message.
2106 -- Invariant, Type_Invariant
2108 when Aspect_Invariant |
2109 Aspect_Type_Invariant =>
2111 -- Analysis of the pragma will verify placement legality:
2112 -- an invariant must apply to a private type, or appear in
2113 -- the private part of a spec and apply to a completion.
2115 Make_Aitem_Pragma
2116 (Pragma_Argument_Associations => New_List (
2117 Make_Pragma_Argument_Association (Sloc (Ent),
2118 Expression => Ent),
2119 Make_Pragma_Argument_Association (Sloc (Expr),
2120 Expression => Relocate_Node (Expr))),
2121 Pragma_Name => Name_Invariant);
2123 -- Add message unless exception messages are suppressed
2125 if not Opt.Exception_Locations_Suppressed then
2126 Append_To (Pragma_Argument_Associations (Aitem),
2127 Make_Pragma_Argument_Association (Eloc,
2128 Chars => Name_Message,
2129 Expression =>
2130 Make_String_Literal (Eloc,
2131 Strval => "failed invariant from "
2132 & Build_Location_String (Eloc))));
2133 end if;
2135 -- For Invariant case, insert immediately after the entity
2136 -- declaration. We do not have to worry about delay issues
2137 -- since the pragma processing takes care of this.
2139 Delay_Required := False;
2141 -- Case 2d : Aspects that correspond to a pragma with one
2142 -- argument.
2144 -- Abstract_State
2146 -- Aspect Abstract_State introduces implicit declarations for
2147 -- all state abstraction entities it defines. To emulate this
2148 -- behavior, insert the pragma at the beginning of the visible
2149 -- declarations of the related package so that it is analyzed
2150 -- immediately.
2152 when Aspect_Abstract_State => Abstract_State : declare
2153 Context : Node_Id := N;
2154 Decl : Node_Id;
2155 Decls : List_Id;
2157 begin
2158 -- When aspect Abstract_State appears on a generic package,
2159 -- it is propageted to the package instance. The context in
2160 -- this case is the instance spec.
2162 if Nkind (Context) = N_Package_Instantiation then
2163 Context := Instance_Spec (Context);
2164 end if;
2166 if Nkind_In (Context, N_Generic_Package_Declaration,
2167 N_Package_Declaration)
2168 then
2169 Make_Aitem_Pragma
2170 (Pragma_Argument_Associations => New_List (
2171 Make_Pragma_Argument_Association (Loc,
2172 Expression => Relocate_Node (Expr))),
2173 Pragma_Name => Name_Abstract_State);
2174 Decorate (Aspect, Aitem);
2176 Decls := Visible_Declarations (Specification (Context));
2178 -- In general pragma Abstract_State must be at the top
2179 -- of the existing visible declarations to emulate its
2180 -- source counterpart. The only exception to this is a
2181 -- generic instance in which case the pragma must be
2182 -- inserted after the association renamings.
2184 if Present (Decls) then
2185 Decl := First (Decls);
2187 -- The visible declarations of a generic instance have
2188 -- the following structure:
2190 -- <renamings of generic formals>
2191 -- <renamings of internally-generated spec and body>
2192 -- <first source declaration>
2194 -- The pragma must be inserted before the first source
2195 -- declaration, skip the instance "header".
2197 if Is_Generic_Instance (Defining_Entity (Context)) then
2198 while Present (Decl)
2199 and then not Comes_From_Source (Decl)
2200 loop
2201 Decl := Next (Decl);
2202 end loop;
2203 end if;
2205 -- When aspects Abstract_State, Ghost,
2206 -- Initial_Condition and Initializes are out of order,
2207 -- ensure that pragma SPARK_Mode is always at the top
2208 -- of the declarations to properly enabled/suppress
2209 -- errors.
2211 Insert_After_SPARK_Mode
2212 (Prag => Aitem,
2213 Ins_Nod => Decl,
2214 Decls => Decls);
2216 -- Otherwise the pragma forms a new declarative list
2218 else
2219 Set_Visible_Declarations
2220 (Specification (Context), New_List (Aitem));
2221 end if;
2223 else
2224 Error_Msg_NE
2225 ("aspect & must apply to a package declaration",
2226 Aspect, Id);
2227 end if;
2229 goto Continue;
2230 end Abstract_State;
2232 -- Aspect Default_Internal_Condition is never delayed because
2233 -- it is equivalent to a source pragma which appears after the
2234 -- related private type. To deal with forward references, the
2235 -- generated pragma is stored in the rep chain of the related
2236 -- private type as types do not carry contracts. The pragma is
2237 -- wrapped inside of a procedure at the freeze point of the
2238 -- private type's full view.
2240 when Aspect_Default_Initial_Condition =>
2241 Make_Aitem_Pragma
2242 (Pragma_Argument_Associations => New_List (
2243 Make_Pragma_Argument_Association (Loc,
2244 Expression => Relocate_Node (Expr))),
2245 Pragma_Name =>
2246 Name_Default_Initial_Condition);
2248 Decorate (Aspect, Aitem);
2249 Insert_Pragma (Aitem);
2250 goto Continue;
2252 -- Default_Storage_Pool
2254 when Aspect_Default_Storage_Pool =>
2255 Make_Aitem_Pragma
2256 (Pragma_Argument_Associations => New_List (
2257 Make_Pragma_Argument_Association (Loc,
2258 Expression => Relocate_Node (Expr))),
2259 Pragma_Name =>
2260 Name_Default_Storage_Pool);
2262 Decorate (Aspect, Aitem);
2263 Insert_Pragma (Aitem);
2264 goto Continue;
2266 -- Depends
2268 -- Aspect Depends is never delayed because it is equivalent to
2269 -- a source pragma which appears after the related subprogram.
2270 -- To deal with forward references, the generated pragma is
2271 -- stored in the contract of the related subprogram and later
2272 -- analyzed at the end of the declarative region. See routine
2273 -- Analyze_Depends_In_Decl_Part for details.
2275 when Aspect_Depends =>
2276 Make_Aitem_Pragma
2277 (Pragma_Argument_Associations => New_List (
2278 Make_Pragma_Argument_Association (Loc,
2279 Expression => Relocate_Node (Expr))),
2280 Pragma_Name => Name_Depends);
2282 Decorate (Aspect, Aitem);
2283 Insert_Pragma (Aitem);
2284 goto Continue;
2286 -- Aspect Extensions_Visible is never delayed because it is
2287 -- equivalent to a source pragma which appears after the
2288 -- related subprogram.
2290 when Aspect_Extensions_Visible =>
2291 Make_Aitem_Pragma
2292 (Pragma_Argument_Associations => New_List (
2293 Make_Pragma_Argument_Association (Loc,
2294 Expression => Relocate_Node (Expr))),
2295 Pragma_Name => Name_Extensions_Visible);
2297 Decorate (Aspect, Aitem);
2298 Insert_Pragma (Aitem);
2299 goto Continue;
2301 -- Aspect Ghost is never delayed because it is equivalent to a
2302 -- source pragma which appears at the top of [generic] package
2303 -- declarations or after an object, a [generic] subprogram, or
2304 -- a type declaration.
2306 when Aspect_Ghost => Ghost : declare
2307 Decls : List_Id;
2309 begin
2310 Make_Aitem_Pragma
2311 (Pragma_Argument_Associations => New_List (
2312 Make_Pragma_Argument_Association (Loc,
2313 Expression => Relocate_Node (Expr))),
2314 Pragma_Name => Name_Ghost);
2316 Decorate (Aspect, Aitem);
2318 -- When the aspect applies to a [generic] package, insert
2319 -- the pragma at the top of the visible declarations. This
2320 -- emulates the placement of a source pragma.
2322 if Nkind_In (N, N_Generic_Package_Declaration,
2323 N_Package_Declaration)
2324 then
2325 Decls := Visible_Declarations (Specification (N));
2327 if No (Decls) then
2328 Decls := New_List;
2329 Set_Visible_Declarations (N, Decls);
2330 end if;
2332 -- When aspects Abstract_State, Ghost, Initial_Condition
2333 -- and Initializes are out of order, ensure that pragma
2334 -- SPARK_Mode is always at the top of the declarations to
2335 -- properly enabled/suppress errors.
2337 Insert_After_SPARK_Mode
2338 (Prag => Aitem,
2339 Ins_Nod => First (Decls),
2340 Decls => Decls);
2342 -- Otherwise the context is an object, [generic] subprogram
2343 -- or type declaration.
2345 else
2346 Insert_Pragma (Aitem);
2347 end if;
2349 goto Continue;
2350 end Ghost;
2352 -- Global
2354 -- Aspect Global is never delayed because it is equivalent to
2355 -- a source pragma which appears after the related subprogram.
2356 -- To deal with forward references, the generated pragma is
2357 -- stored in the contract of the related subprogram and later
2358 -- analyzed at the end of the declarative region. See routine
2359 -- Analyze_Global_In_Decl_Part for details.
2361 when Aspect_Global =>
2362 Make_Aitem_Pragma
2363 (Pragma_Argument_Associations => New_List (
2364 Make_Pragma_Argument_Association (Loc,
2365 Expression => Relocate_Node (Expr))),
2366 Pragma_Name => Name_Global);
2368 Decorate (Aspect, Aitem);
2369 Insert_Pragma (Aitem);
2370 goto Continue;
2372 -- Initial_Condition
2374 -- Aspect Initial_Condition is never delayed because it is
2375 -- equivalent to a source pragma which appears after the
2376 -- related package. To deal with forward references, the
2377 -- generated pragma is stored in the contract of the related
2378 -- package and later analyzed at the end of the declarative
2379 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2380 -- for details.
2382 when Aspect_Initial_Condition => Initial_Condition : declare
2383 Context : Node_Id := N;
2384 Decls : List_Id;
2386 begin
2387 -- When aspect Initial_Condition appears on a generic
2388 -- package, it is propageted to the package instance. The
2389 -- context in this case is the instance spec.
2391 if Nkind (Context) = N_Package_Instantiation then
2392 Context := Instance_Spec (Context);
2393 end if;
2395 if Nkind_In (Context, N_Generic_Package_Declaration,
2396 N_Package_Declaration)
2397 then
2398 Decls := Visible_Declarations (Specification (Context));
2400 Make_Aitem_Pragma
2401 (Pragma_Argument_Associations => New_List (
2402 Make_Pragma_Argument_Association (Loc,
2403 Expression => Relocate_Node (Expr))),
2404 Pragma_Name =>
2405 Name_Initial_Condition);
2406 Decorate (Aspect, Aitem);
2408 if No (Decls) then
2409 Decls := New_List;
2410 Set_Visible_Declarations (Context, Decls);
2411 end if;
2413 -- When aspects Abstract_State, Ghost, Initial_Condition
2414 -- and Initializes are out of order, ensure that pragma
2415 -- SPARK_Mode is always at the top of the declarations to
2416 -- properly enabled/suppress errors.
2418 Insert_After_SPARK_Mode
2419 (Prag => Aitem,
2420 Ins_Nod => First (Decls),
2421 Decls => Decls);
2423 else
2424 Error_Msg_NE
2425 ("aspect & must apply to a package declaration",
2426 Aspect, Id);
2427 end if;
2429 goto Continue;
2430 end Initial_Condition;
2432 -- Initializes
2434 -- Aspect Initializes is never delayed because it is equivalent
2435 -- to a source pragma appearing after the related package. To
2436 -- deal with forward references, the generated pragma is stored
2437 -- in the contract of the related package and later analyzed at
2438 -- the end of the declarative region. For details, see routine
2439 -- Analyze_Initializes_In_Decl_Part.
2441 when Aspect_Initializes => Initializes : declare
2442 Context : Node_Id := N;
2443 Decls : List_Id;
2445 begin
2446 -- When aspect Initializes appears on a generic package,
2447 -- it is propageted to the package instance. The context
2448 -- in this case is the instance spec.
2450 if Nkind (Context) = N_Package_Instantiation then
2451 Context := Instance_Spec (Context);
2452 end if;
2454 if Nkind_In (Context, N_Generic_Package_Declaration,
2455 N_Package_Declaration)
2456 then
2457 Decls := Visible_Declarations (Specification (Context));
2459 Make_Aitem_Pragma
2460 (Pragma_Argument_Associations => New_List (
2461 Make_Pragma_Argument_Association (Loc,
2462 Expression => Relocate_Node (Expr))),
2463 Pragma_Name => Name_Initializes);
2464 Decorate (Aspect, Aitem);
2466 if No (Decls) then
2467 Decls := New_List;
2468 Set_Visible_Declarations (Context, Decls);
2469 end if;
2471 -- When aspects Abstract_State, Ghost, Initial_Condition
2472 -- and Initializes are out of order, ensure that pragma
2473 -- SPARK_Mode is always at the top of the declarations to
2474 -- properly enabled/suppress errors.
2476 Insert_After_SPARK_Mode
2477 (Prag => Aitem,
2478 Ins_Nod => First (Decls),
2479 Decls => Decls);
2481 else
2482 Error_Msg_NE
2483 ("aspect & must apply to a package declaration",
2484 Aspect, Id);
2485 end if;
2487 goto Continue;
2488 end Initializes;
2490 -- Obsolescent
2492 when Aspect_Obsolescent => declare
2493 Args : List_Id;
2495 begin
2496 if No (Expr) then
2497 Args := No_List;
2498 else
2499 Args := New_List (
2500 Make_Pragma_Argument_Association (Sloc (Expr),
2501 Expression => Relocate_Node (Expr)));
2502 end if;
2504 Make_Aitem_Pragma
2505 (Pragma_Argument_Associations => Args,
2506 Pragma_Name => Chars (Id));
2507 end;
2509 -- Part_Of
2511 when Aspect_Part_Of =>
2512 if Nkind_In (N, N_Object_Declaration,
2513 N_Package_Instantiation)
2514 then
2515 Make_Aitem_Pragma
2516 (Pragma_Argument_Associations => New_List (
2517 Make_Pragma_Argument_Association (Loc,
2518 Expression => Relocate_Node (Expr))),
2519 Pragma_Name => Name_Part_Of);
2521 else
2522 Error_Msg_NE
2523 ("aspect & must apply to a variable or package "
2524 & "instantiation", Aspect, Id);
2525 end if;
2527 -- SPARK_Mode
2529 when Aspect_SPARK_Mode => SPARK_Mode : declare
2530 Decls : List_Id;
2532 begin
2533 Make_Aitem_Pragma
2534 (Pragma_Argument_Associations => New_List (
2535 Make_Pragma_Argument_Association (Loc,
2536 Expression => Relocate_Node (Expr))),
2537 Pragma_Name => Name_SPARK_Mode);
2539 -- When the aspect appears on a package or a subprogram
2540 -- body, insert the generated pragma at the top of the body
2541 -- declarations to emulate the behavior of a source pragma.
2543 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2544 Decorate (Aspect, Aitem);
2546 Decls := Declarations (N);
2548 if No (Decls) then
2549 Decls := New_List;
2550 Set_Declarations (N, Decls);
2551 end if;
2553 Prepend_To (Decls, Aitem);
2554 goto Continue;
2556 -- When the aspect is associated with a [generic] package
2557 -- declaration, insert the generated pragma at the top of
2558 -- the visible declarations to emulate the behavior of a
2559 -- source pragma.
2561 elsif Nkind_In (N, N_Generic_Package_Declaration,
2562 N_Package_Declaration)
2563 then
2564 Decorate (Aspect, Aitem);
2566 Decls := Visible_Declarations (Specification (N));
2568 if No (Decls) then
2569 Decls := New_List;
2570 Set_Visible_Declarations (Specification (N), Decls);
2571 end if;
2573 Prepend_To (Decls, Aitem);
2574 goto Continue;
2575 end if;
2576 end SPARK_Mode;
2578 -- Refined_Depends
2580 -- Aspect Refined_Depends is never delayed because it is
2581 -- equivalent to a source pragma which appears in the
2582 -- declarations of the related subprogram body. To deal with
2583 -- forward references, the generated pragma is stored in the
2584 -- contract of the related subprogram body and later analyzed
2585 -- at the end of the declarative region. For details, see
2586 -- routine Analyze_Refined_Depends_In_Decl_Part.
2588 when Aspect_Refined_Depends =>
2589 Make_Aitem_Pragma
2590 (Pragma_Argument_Associations => New_List (
2591 Make_Pragma_Argument_Association (Loc,
2592 Expression => Relocate_Node (Expr))),
2593 Pragma_Name => Name_Refined_Depends);
2595 Decorate (Aspect, Aitem);
2596 Insert_Pragma (Aitem);
2597 goto Continue;
2599 -- Refined_Global
2601 -- Aspect Refined_Global is never delayed because it is
2602 -- equivalent to a source pragma which appears in the
2603 -- declarations of the related subprogram body. To deal with
2604 -- forward references, the generated pragma is stored in the
2605 -- contract of the related subprogram body and later analyzed
2606 -- at the end of the declarative region. For details, see
2607 -- routine Analyze_Refined_Global_In_Decl_Part.
2609 when Aspect_Refined_Global =>
2610 Make_Aitem_Pragma
2611 (Pragma_Argument_Associations => New_List (
2612 Make_Pragma_Argument_Association (Loc,
2613 Expression => Relocate_Node (Expr))),
2614 Pragma_Name => Name_Refined_Global);
2616 Decorate (Aspect, Aitem);
2617 Insert_Pragma (Aitem);
2618 goto Continue;
2620 -- Refined_Post
2622 when Aspect_Refined_Post =>
2623 Make_Aitem_Pragma
2624 (Pragma_Argument_Associations => New_List (
2625 Make_Pragma_Argument_Association (Loc,
2626 Expression => Relocate_Node (Expr))),
2627 Pragma_Name => Name_Refined_Post);
2629 -- Refined_State
2631 when Aspect_Refined_State => Refined_State : declare
2632 Decls : List_Id;
2634 begin
2635 -- The corresponding pragma for Refined_State is inserted in
2636 -- the declarations of the related package body. This action
2637 -- synchronizes both the source and from-aspect versions of
2638 -- the pragma.
2640 if Nkind (N) = N_Package_Body then
2641 Decls := Declarations (N);
2643 Make_Aitem_Pragma
2644 (Pragma_Argument_Associations => New_List (
2645 Make_Pragma_Argument_Association (Loc,
2646 Expression => Relocate_Node (Expr))),
2647 Pragma_Name => Name_Refined_State);
2648 Decorate (Aspect, Aitem);
2650 if No (Decls) then
2651 Decls := New_List;
2652 Set_Declarations (N, Decls);
2653 end if;
2655 -- Pragma Refined_State must be inserted after pragma
2656 -- SPARK_Mode in the tree. This ensures that any error
2657 -- messages dependent on SPARK_Mode will be properly
2658 -- enabled/suppressed.
2660 Insert_After_SPARK_Mode
2661 (Prag => Aitem,
2662 Ins_Nod => First (Decls),
2663 Decls => Decls);
2665 else
2666 Error_Msg_NE
2667 ("aspect & must apply to a package body", Aspect, Id);
2668 end if;
2670 goto Continue;
2671 end Refined_State;
2673 -- Relative_Deadline
2675 when Aspect_Relative_Deadline =>
2676 Make_Aitem_Pragma
2677 (Pragma_Argument_Associations => New_List (
2678 Make_Pragma_Argument_Association (Loc,
2679 Expression => Relocate_Node (Expr))),
2680 Pragma_Name => Name_Relative_Deadline);
2682 -- If the aspect applies to a task, the corresponding pragma
2683 -- must appear within its declarations, not after.
2685 if Nkind (N) = N_Task_Type_Declaration then
2686 declare
2687 Def : Node_Id;
2688 V : List_Id;
2690 begin
2691 if No (Task_Definition (N)) then
2692 Set_Task_Definition (N,
2693 Make_Task_Definition (Loc,
2694 Visible_Declarations => New_List,
2695 End_Label => Empty));
2696 end if;
2698 Def := Task_Definition (N);
2699 V := Visible_Declarations (Def);
2700 if not Is_Empty_List (V) then
2701 Insert_Before (First (V), Aitem);
2703 else
2704 Set_Visible_Declarations (Def, New_List (Aitem));
2705 end if;
2707 goto Continue;
2708 end;
2709 end if;
2711 -- Case 2e: Annotate aspect
2713 when Aspect_Annotate =>
2714 declare
2715 Args : List_Id;
2716 Pargs : List_Id;
2717 Arg : Node_Id;
2719 begin
2720 -- The argument can be a single identifier
2722 if Nkind (Expr) = N_Identifier then
2724 -- One level of parens is allowed
2726 if Paren_Count (Expr) > 1 then
2727 Error_Msg_F ("extra parentheses ignored", Expr);
2728 end if;
2730 Set_Paren_Count (Expr, 0);
2732 -- Add the single item to the list
2734 Args := New_List (Expr);
2736 -- Otherwise we must have an aggregate
2738 elsif Nkind (Expr) = N_Aggregate then
2740 -- Must be positional
2742 if Present (Component_Associations (Expr)) then
2743 Error_Msg_F
2744 ("purely positional aggregate required", Expr);
2745 goto Continue;
2746 end if;
2748 -- Must not be parenthesized
2750 if Paren_Count (Expr) /= 0 then
2751 Error_Msg_F ("extra parentheses ignored", Expr);
2752 end if;
2754 -- List of arguments is list of aggregate expressions
2756 Args := Expressions (Expr);
2758 -- Anything else is illegal
2760 else
2761 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2762 goto Continue;
2763 end if;
2765 -- Prepare pragma arguments
2767 Pargs := New_List;
2768 Arg := First (Args);
2769 while Present (Arg) loop
2770 Append_To (Pargs,
2771 Make_Pragma_Argument_Association (Sloc (Arg),
2772 Expression => Relocate_Node (Arg)));
2773 Next (Arg);
2774 end loop;
2776 Append_To (Pargs,
2777 Make_Pragma_Argument_Association (Sloc (Ent),
2778 Chars => Name_Entity,
2779 Expression => Ent));
2781 Make_Aitem_Pragma
2782 (Pragma_Argument_Associations => Pargs,
2783 Pragma_Name => Name_Annotate);
2784 end;
2786 -- Case 3 : Aspects that don't correspond to pragma/attribute
2787 -- definition clause.
2789 -- Case 3a: The aspects listed below don't correspond to
2790 -- pragmas/attributes but do require delayed analysis.
2792 -- Default_Value can only apply to a scalar type
2794 when Aspect_Default_Value =>
2795 if not Is_Scalar_Type (E) then
2796 Error_Msg_N
2797 ("aspect Default_Value must apply to a scalar type", N);
2798 end if;
2800 Aitem := Empty;
2802 -- Default_Component_Value can only apply to an array type
2803 -- with scalar components.
2805 when Aspect_Default_Component_Value =>
2806 if not (Is_Array_Type (E)
2807 and then Is_Scalar_Type (Component_Type (E)))
2808 then
2809 Error_Msg_N ("aspect Default_Component_Value can only "
2810 & "apply to an array of scalar components", N);
2811 end if;
2813 Aitem := Empty;
2815 -- Case 3b: The aspects listed below don't correspond to
2816 -- pragmas/attributes and don't need delayed analysis.
2818 -- Implicit_Dereference
2820 -- For Implicit_Dereference, External_Name and Link_Name, only
2821 -- the legality checks are done during the analysis, thus no
2822 -- delay is required.
2824 when Aspect_Implicit_Dereference =>
2825 Analyze_Aspect_Implicit_Dereference;
2826 goto Continue;
2828 -- External_Name, Link_Name
2830 when Aspect_External_Name |
2831 Aspect_Link_Name =>
2832 Analyze_Aspect_External_Or_Link_Name;
2833 goto Continue;
2835 -- Dimension
2837 when Aspect_Dimension =>
2838 Analyze_Aspect_Dimension (N, Id, Expr);
2839 goto Continue;
2841 -- Dimension_System
2843 when Aspect_Dimension_System =>
2844 Analyze_Aspect_Dimension_System (N, Id, Expr);
2845 goto Continue;
2847 -- Case 4: Aspects requiring special handling
2849 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2850 -- pragmas take care of the delay.
2852 -- Pre/Post
2854 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2855 -- with a first argument that is the expression, and a second
2856 -- argument that is an informative message if the test fails.
2857 -- This is inserted right after the declaration, to get the
2858 -- required pragma placement. The processing for the pragmas
2859 -- takes care of the required delay.
2861 when Pre_Post_Aspects => Pre_Post : declare
2862 Pname : Name_Id;
2864 begin
2865 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2866 Pname := Name_Precondition;
2867 else
2868 Pname := Name_Postcondition;
2869 end if;
2871 -- If the expressions is of the form A and then B, then
2872 -- we generate separate Pre/Post aspects for the separate
2873 -- clauses. Since we allow multiple pragmas, there is no
2874 -- problem in allowing multiple Pre/Post aspects internally.
2875 -- These should be treated in reverse order (B first and
2876 -- A second) since they are later inserted just after N in
2877 -- the order they are treated. This way, the pragma for A
2878 -- ends up preceding the pragma for B, which may have an
2879 -- importance for the error raised (either constraint error
2880 -- or precondition error).
2882 -- We do not do this for Pre'Class, since we have to put
2883 -- these conditions together in a complex OR expression.
2885 -- We do not do this in ASIS mode, as ASIS relies on the
2886 -- original node representing the complete expression, when
2887 -- retrieving it through the source aspect table.
2889 if not ASIS_Mode
2890 and then (Pname = Name_Postcondition
2891 or else not Class_Present (Aspect))
2892 then
2893 while Nkind (Expr) = N_And_Then loop
2894 Insert_After (Aspect,
2895 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2896 Identifier => Identifier (Aspect),
2897 Expression => Relocate_Node (Left_Opnd (Expr)),
2898 Class_Present => Class_Present (Aspect),
2899 Split_PPC => True));
2900 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2901 Eloc := Sloc (Expr);
2902 end loop;
2903 end if;
2905 -- Build the precondition/postcondition pragma
2907 -- Add note about why we do NOT need Copy_Tree here???
2909 Make_Aitem_Pragma
2910 (Pragma_Argument_Associations => New_List (
2911 Make_Pragma_Argument_Association (Eloc,
2912 Chars => Name_Check,
2913 Expression => Relocate_Node (Expr))),
2914 Pragma_Name => Pname);
2916 -- Add message unless exception messages are suppressed
2918 if not Opt.Exception_Locations_Suppressed then
2919 Append_To (Pragma_Argument_Associations (Aitem),
2920 Make_Pragma_Argument_Association (Eloc,
2921 Chars => Name_Message,
2922 Expression =>
2923 Make_String_Literal (Eloc,
2924 Strval => "failed "
2925 & Get_Name_String (Pname)
2926 & " from "
2927 & Build_Location_String (Eloc))));
2928 end if;
2930 Set_Is_Delayed_Aspect (Aspect);
2932 -- For Pre/Post cases, insert immediately after the entity
2933 -- declaration, since that is the required pragma placement.
2934 -- Note that for these aspects, we do not have to worry
2935 -- about delay issues, since the pragmas themselves deal
2936 -- with delay of visibility for the expression analysis.
2938 Insert_Pragma (Aitem);
2940 goto Continue;
2941 end Pre_Post;
2943 -- Test_Case
2945 when Aspect_Test_Case => Test_Case : declare
2946 Args : List_Id;
2947 Comp_Expr : Node_Id;
2948 Comp_Assn : Node_Id;
2949 New_Expr : Node_Id;
2951 begin
2952 Args := New_List;
2954 if Nkind (Parent (N)) = N_Compilation_Unit then
2955 Error_Msg_Name_1 := Nam;
2956 Error_Msg_N ("incorrect placement of aspect `%`", E);
2957 goto Continue;
2958 end if;
2960 if Nkind (Expr) /= N_Aggregate then
2961 Error_Msg_Name_1 := Nam;
2962 Error_Msg_NE
2963 ("wrong syntax for aspect `%` for &", Id, E);
2964 goto Continue;
2965 end if;
2967 -- Make pragma expressions refer to the original aspect
2968 -- expressions through the Original_Node link. This is used
2969 -- in semantic analysis for ASIS mode, so that the original
2970 -- expression also gets analyzed.
2972 Comp_Expr := First (Expressions (Expr));
2973 while Present (Comp_Expr) loop
2974 New_Expr := Relocate_Node (Comp_Expr);
2975 Set_Original_Node (New_Expr, Comp_Expr);
2976 Append_To (Args,
2977 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2978 Expression => New_Expr));
2979 Next (Comp_Expr);
2980 end loop;
2982 Comp_Assn := First (Component_Associations (Expr));
2983 while Present (Comp_Assn) loop
2984 if List_Length (Choices (Comp_Assn)) /= 1
2985 or else
2986 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2987 then
2988 Error_Msg_Name_1 := Nam;
2989 Error_Msg_NE
2990 ("wrong syntax for aspect `%` for &", Id, E);
2991 goto Continue;
2992 end if;
2994 New_Expr := Relocate_Node (Expression (Comp_Assn));
2995 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2996 Append_To (Args,
2997 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2998 Chars => Chars (First (Choices (Comp_Assn))),
2999 Expression => New_Expr));
3000 Next (Comp_Assn);
3001 end loop;
3003 -- Build the test-case pragma
3005 Make_Aitem_Pragma
3006 (Pragma_Argument_Associations => Args,
3007 Pragma_Name => Nam);
3008 end Test_Case;
3010 -- Contract_Cases
3012 when Aspect_Contract_Cases =>
3013 Make_Aitem_Pragma
3014 (Pragma_Argument_Associations => New_List (
3015 Make_Pragma_Argument_Association (Loc,
3016 Expression => Relocate_Node (Expr))),
3017 Pragma_Name => Nam);
3019 Decorate (Aspect, Aitem);
3020 Insert_Pragma (Aitem);
3021 goto Continue;
3023 -- Case 5: Special handling for aspects with an optional
3024 -- boolean argument.
3026 -- In the general case, the corresponding pragma cannot be
3027 -- generated yet because the evaluation of the boolean needs
3028 -- to be delayed till the freeze point.
3030 when Boolean_Aspects |
3031 Library_Unit_Aspects =>
3033 Set_Is_Boolean_Aspect (Aspect);
3035 -- Lock_Free aspect only apply to protected objects
3037 if A_Id = Aspect_Lock_Free then
3038 if Ekind (E) /= E_Protected_Type then
3039 Error_Msg_Name_1 := Nam;
3040 Error_Msg_N
3041 ("aspect % only applies to a protected object",
3042 Aspect);
3044 else
3045 -- Set the Uses_Lock_Free flag to True if there is no
3046 -- expression or if the expression is True. The
3047 -- evaluation of this aspect should be delayed to the
3048 -- freeze point (why???)
3050 if No (Expr)
3051 or else Is_True (Static_Boolean (Expr))
3052 then
3053 Set_Uses_Lock_Free (E);
3054 end if;
3056 Record_Rep_Item (E, Aspect);
3057 end if;
3059 goto Continue;
3061 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
3063 -- For the case of aspects Import and Export, we don't
3064 -- consider that we know the entity is never set in the
3065 -- source, since it is is likely modified outside the
3066 -- program.
3068 -- Note: one might think that the analysis of the
3069 -- resulting pragma would take care of that, but
3070 -- that's not the case since it won't be from source.
3072 if Ekind (E) = E_Variable then
3073 Set_Never_Set_In_Source (E, False);
3074 end if;
3076 -- In older versions of Ada the corresponding pragmas
3077 -- specified a Convention. In Ada 2012 the convention is
3078 -- specified as a separate aspect, and it is optional,
3079 -- given that it defaults to Convention_Ada. The code
3080 -- that verifed that there was a matching convention
3081 -- is now obsolete.
3083 -- Resolve the expression of an Import or Export here,
3084 -- and require it to be of type Boolean and static. This
3085 -- is not quite right, because in general this should be
3086 -- delayed, but that seems tricky for these, because
3087 -- normally Boolean aspects are replaced with pragmas at
3088 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
3089 -- but in the case of these aspects we can't generate
3090 -- a simple pragma with just the entity name. ???
3092 if not Present (Expr)
3093 or else Is_True (Static_Boolean (Expr))
3094 then
3095 if A_Id = Aspect_Import then
3096 Set_Is_Imported (E);
3098 -- An imported entity cannot have an explicit
3099 -- initialization.
3101 if Nkind (N) = N_Object_Declaration
3102 and then Present (Expression (N))
3103 then
3104 Error_Msg_N
3105 ("imported entities cannot be initialized "
3106 & "(RM B.1(24))", Expression (N));
3107 end if;
3109 elsif A_Id = Aspect_Export then
3110 Set_Is_Exported (E);
3111 end if;
3112 end if;
3114 goto Continue;
3115 end if;
3117 -- Library unit aspects require special handling in the case
3118 -- of a package declaration, the pragma needs to be inserted
3119 -- in the list of declarations for the associated package.
3120 -- There is no issue of visibility delay for these aspects.
3122 if A_Id in Library_Unit_Aspects
3123 and then
3124 Nkind_In (N, N_Package_Declaration,
3125 N_Generic_Package_Declaration)
3126 and then Nkind (Parent (N)) /= N_Compilation_Unit
3128 -- Aspect is legal on a local instantiation of a library-
3129 -- level generic unit.
3131 and then not Is_Generic_Instance (Defining_Entity (N))
3132 then
3133 Error_Msg_N
3134 ("incorrect context for library unit aspect&", Id);
3135 goto Continue;
3136 end if;
3138 -- External property aspects are Boolean by nature, but
3139 -- their pragmas must contain two arguments, the second
3140 -- being the optional Boolean expression.
3142 if A_Id = Aspect_Async_Readers or else
3143 A_Id = Aspect_Async_Writers or else
3144 A_Id = Aspect_Effective_Reads or else
3145 A_Id = Aspect_Effective_Writes
3146 then
3147 declare
3148 Args : List_Id;
3150 begin
3151 -- The first argument of the external property pragma
3152 -- is the related object.
3154 Args :=
3155 New_List (
3156 Make_Pragma_Argument_Association (Sloc (Ent),
3157 Expression => Ent));
3159 -- The second argument is the optional Boolean
3160 -- expression which must be propagated even if it
3161 -- evaluates to False as this has special semantic
3162 -- meaning.
3164 if Present (Expr) then
3165 Append_To (Args,
3166 Make_Pragma_Argument_Association (Loc,
3167 Expression => Relocate_Node (Expr)));
3168 end if;
3170 Make_Aitem_Pragma
3171 (Pragma_Argument_Associations => Args,
3172 Pragma_Name => Nam);
3173 end;
3175 -- Cases where we do not delay, includes all cases where the
3176 -- expression is missing other than the above cases.
3178 elsif not Delay_Required or else No (Expr) then
3179 Make_Aitem_Pragma
3180 (Pragma_Argument_Associations => New_List (
3181 Make_Pragma_Argument_Association (Sloc (Ent),
3182 Expression => Ent)),
3183 Pragma_Name => Chars (Id));
3184 Delay_Required := False;
3186 -- In general cases, the corresponding pragma/attribute
3187 -- definition clause will be inserted later at the freezing
3188 -- point, and we do not need to build it now.
3190 else
3191 Aitem := Empty;
3192 end if;
3194 -- Storage_Size
3196 -- This is special because for access types we need to generate
3197 -- an attribute definition clause. This also works for single
3198 -- task declarations, but it does not work for task type
3199 -- declarations, because we have the case where the expression
3200 -- references a discriminant of the task type. That can't use
3201 -- an attribute definition clause because we would not have
3202 -- visibility on the discriminant. For that case we must
3203 -- generate a pragma in the task definition.
3205 when Aspect_Storage_Size =>
3207 -- Task type case
3209 if Ekind (E) = E_Task_Type then
3210 declare
3211 Decl : constant Node_Id := Declaration_Node (E);
3213 begin
3214 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3216 -- If no task definition, create one
3218 if No (Task_Definition (Decl)) then
3219 Set_Task_Definition (Decl,
3220 Make_Task_Definition (Loc,
3221 Visible_Declarations => Empty_List,
3222 End_Label => Empty));
3223 end if;
3225 -- Create a pragma and put it at the start of the task
3226 -- definition for the task type declaration.
3228 Make_Aitem_Pragma
3229 (Pragma_Argument_Associations => New_List (
3230 Make_Pragma_Argument_Association (Loc,
3231 Expression => Relocate_Node (Expr))),
3232 Pragma_Name => Name_Storage_Size);
3234 Prepend
3235 (Aitem,
3236 Visible_Declarations (Task_Definition (Decl)));
3237 goto Continue;
3238 end;
3240 -- All other cases, generate attribute definition
3242 else
3243 Aitem :=
3244 Make_Attribute_Definition_Clause (Loc,
3245 Name => Ent,
3246 Chars => Chars (Id),
3247 Expression => Relocate_Node (Expr));
3248 end if;
3249 end case;
3251 -- Attach the corresponding pragma/attribute definition clause to
3252 -- the aspect specification node.
3254 if Present (Aitem) then
3255 Set_From_Aspect_Specification (Aitem);
3256 end if;
3258 -- In the context of a compilation unit, we directly put the
3259 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3260 -- node (no delay is required here) except for aspects on a
3261 -- subprogram body (see below) and a generic package, for which we
3262 -- need to introduce the pragma before building the generic copy
3263 -- (see sem_ch12), and for package instantiations, where the
3264 -- library unit pragmas are better handled early.
3266 if Nkind (Parent (N)) = N_Compilation_Unit
3267 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3268 then
3269 declare
3270 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3272 begin
3273 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3275 -- For a Boolean aspect, create the corresponding pragma if
3276 -- no expression or if the value is True.
3278 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3279 if Is_True (Static_Boolean (Expr)) then
3280 Make_Aitem_Pragma
3281 (Pragma_Argument_Associations => New_List (
3282 Make_Pragma_Argument_Association (Sloc (Ent),
3283 Expression => Ent)),
3284 Pragma_Name => Chars (Id));
3286 Set_From_Aspect_Specification (Aitem, True);
3287 Set_Corresponding_Aspect (Aitem, Aspect);
3289 else
3290 goto Continue;
3291 end if;
3292 end if;
3294 -- If the aspect is on a subprogram body (relevant aspect
3295 -- is Inline), add the pragma in front of the declarations.
3297 if Nkind (N) = N_Subprogram_Body then
3298 if No (Declarations (N)) then
3299 Set_Declarations (N, New_List);
3300 end if;
3302 Prepend (Aitem, Declarations (N));
3304 elsif Nkind (N) = N_Generic_Package_Declaration then
3305 if No (Visible_Declarations (Specification (N))) then
3306 Set_Visible_Declarations (Specification (N), New_List);
3307 end if;
3309 Prepend (Aitem,
3310 Visible_Declarations (Specification (N)));
3312 elsif Nkind (N) = N_Package_Instantiation then
3313 declare
3314 Spec : constant Node_Id :=
3315 Specification (Instance_Spec (N));
3316 begin
3317 if No (Visible_Declarations (Spec)) then
3318 Set_Visible_Declarations (Spec, New_List);
3319 end if;
3321 Prepend (Aitem, Visible_Declarations (Spec));
3322 end;
3324 else
3325 if No (Pragmas_After (Aux)) then
3326 Set_Pragmas_After (Aux, New_List);
3327 end if;
3329 Append (Aitem, Pragmas_After (Aux));
3330 end if;
3332 goto Continue;
3333 end;
3334 end if;
3336 -- The evaluation of the aspect is delayed to the freezing point.
3337 -- The pragma or attribute clause if there is one is then attached
3338 -- to the aspect specification which is put in the rep item list.
3340 if Delay_Required then
3341 if Present (Aitem) then
3342 Set_Is_Delayed_Aspect (Aitem);
3343 Set_Aspect_Rep_Item (Aspect, Aitem);
3344 Set_Parent (Aitem, Aspect);
3345 end if;
3347 Set_Is_Delayed_Aspect (Aspect);
3349 -- In the case of Default_Value, link the aspect to base type
3350 -- as well, even though it appears on a first subtype. This is
3351 -- mandated by the semantics of the aspect. Do not establish
3352 -- the link when processing the base type itself as this leads
3353 -- to a rep item circularity. Verify that we are dealing with
3354 -- a scalar type to prevent cascaded errors.
3356 if A_Id = Aspect_Default_Value
3357 and then Is_Scalar_Type (E)
3358 and then Base_Type (E) /= E
3359 then
3360 Set_Has_Delayed_Aspects (Base_Type (E));
3361 Record_Rep_Item (Base_Type (E), Aspect);
3362 end if;
3364 Set_Has_Delayed_Aspects (E);
3365 Record_Rep_Item (E, Aspect);
3367 -- When delay is not required and the context is a package or a
3368 -- subprogram body, insert the pragma in the body declarations.
3370 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3371 if No (Declarations (N)) then
3372 Set_Declarations (N, New_List);
3373 end if;
3375 -- The pragma is added before source declarations
3377 Prepend_To (Declarations (N), Aitem);
3379 -- When delay is not required and the context is not a compilation
3380 -- unit, we simply insert the pragma/attribute definition clause
3381 -- in sequence.
3383 else
3384 Insert_After (Ins_Node, Aitem);
3385 Ins_Node := Aitem;
3386 end if;
3387 end Analyze_One_Aspect;
3389 <<Continue>>
3390 Next (Aspect);
3391 end loop Aspect_Loop;
3393 if Has_Delayed_Aspects (E) then
3394 Ensure_Freeze_Node (E);
3395 end if;
3396 end Analyze_Aspect_Specifications;
3398 -----------------------
3399 -- Analyze_At_Clause --
3400 -----------------------
3402 -- An at clause is replaced by the corresponding Address attribute
3403 -- definition clause that is the preferred approach in Ada 95.
3405 procedure Analyze_At_Clause (N : Node_Id) is
3406 CS : constant Boolean := Comes_From_Source (N);
3408 begin
3409 -- This is an obsolescent feature
3411 Check_Restriction (No_Obsolescent_Features, N);
3413 if Warn_On_Obsolescent_Feature then
3414 Error_Msg_N
3415 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3416 Error_Msg_N
3417 ("\?j?use address attribute definition clause instead", N);
3418 end if;
3420 -- Rewrite as address clause
3422 Rewrite (N,
3423 Make_Attribute_Definition_Clause (Sloc (N),
3424 Name => Identifier (N),
3425 Chars => Name_Address,
3426 Expression => Expression (N)));
3428 -- We preserve Comes_From_Source, since logically the clause still comes
3429 -- from the source program even though it is changed in form.
3431 Set_Comes_From_Source (N, CS);
3433 -- Analyze rewritten clause
3435 Analyze_Attribute_Definition_Clause (N);
3436 end Analyze_At_Clause;
3438 -----------------------------------------
3439 -- Analyze_Attribute_Definition_Clause --
3440 -----------------------------------------
3442 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3443 Loc : constant Source_Ptr := Sloc (N);
3444 Nam : constant Node_Id := Name (N);
3445 Attr : constant Name_Id := Chars (N);
3446 Expr : constant Node_Id := Expression (N);
3447 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3449 Ent : Entity_Id;
3450 -- The entity of Nam after it is analyzed. In the case of an incomplete
3451 -- type, this is the underlying type.
3453 U_Ent : Entity_Id;
3454 -- The underlying entity to which the attribute applies. Generally this
3455 -- is the Underlying_Type of Ent, except in the case where the clause
3456 -- applies to full view of incomplete type or private type in which case
3457 -- U_Ent is just a copy of Ent.
3459 FOnly : Boolean := False;
3460 -- Reset to True for subtype specific attribute (Alignment, Size)
3461 -- and for stream attributes, i.e. those cases where in the call to
3462 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3463 -- are checked. Note that the case of stream attributes is not clear
3464 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3465 -- Storage_Size for derived task types, but that is also clearly
3466 -- unintentional.
3468 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3469 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3470 -- definition clauses.
3472 function Duplicate_Clause return Boolean;
3473 -- This routine checks if the aspect for U_Ent being given by attribute
3474 -- definition clause N is for an aspect that has already been specified,
3475 -- and if so gives an error message. If there is a duplicate, True is
3476 -- returned, otherwise if there is no error, False is returned.
3478 procedure Check_Indexing_Functions;
3479 -- Check that the function in Constant_Indexing or Variable_Indexing
3480 -- attribute has the proper type structure. If the name is overloaded,
3481 -- check that some interpretation is legal.
3483 procedure Check_Iterator_Functions;
3484 -- Check that there is a single function in Default_Iterator attribute
3485 -- has the proper type structure.
3487 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3488 -- Common legality check for the previous two
3490 -----------------------------------
3491 -- Analyze_Stream_TSS_Definition --
3492 -----------------------------------
3494 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3495 Subp : Entity_Id := Empty;
3496 I : Interp_Index;
3497 It : Interp;
3498 Pnam : Entity_Id;
3500 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3501 -- True for Read attribute, false for other attributes
3503 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3504 -- Return true if the entity is a subprogram with an appropriate
3505 -- profile for the attribute being defined.
3507 ----------------------
3508 -- Has_Good_Profile --
3509 ----------------------
3511 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3512 F : Entity_Id;
3513 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3514 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3515 (False => E_Procedure, True => E_Function);
3516 Typ : Entity_Id;
3518 begin
3519 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3520 return False;
3521 end if;
3523 F := First_Formal (Subp);
3525 if No (F)
3526 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3527 or else Designated_Type (Etype (F)) /=
3528 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3529 then
3530 return False;
3531 end if;
3533 if not Is_Function then
3534 Next_Formal (F);
3536 declare
3537 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3538 (False => E_In_Parameter,
3539 True => E_Out_Parameter);
3540 begin
3541 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3542 return False;
3543 end if;
3544 end;
3546 Typ := Etype (F);
3548 -- If the attribute specification comes from an aspect
3549 -- specification for a class-wide stream, the parameter must be
3550 -- a class-wide type of the entity to which the aspect applies.
3552 if From_Aspect_Specification (N)
3553 and then Class_Present (Parent (N))
3554 and then Is_Class_Wide_Type (Typ)
3555 then
3556 Typ := Etype (Typ);
3557 end if;
3559 else
3560 Typ := Etype (Subp);
3561 end if;
3563 -- Verify that the prefix of the attribute and the local name for
3564 -- the type of the formal match, or one is the class-wide of the
3565 -- other, in the case of a class-wide stream operation.
3567 if Base_Type (Typ) = Base_Type (Ent)
3568 or else (Is_Class_Wide_Type (Typ)
3569 and then Typ = Class_Wide_Type (Base_Type (Ent)))
3570 or else (Is_Class_Wide_Type (Ent)
3571 and then Ent = Class_Wide_Type (Base_Type (Typ)))
3572 then
3573 null;
3574 else
3575 return False;
3576 end if;
3578 if Present ((Next_Formal (F)))
3579 then
3580 return False;
3582 elsif not Is_Scalar_Type (Typ)
3583 and then not Is_First_Subtype (Typ)
3584 and then not Is_Class_Wide_Type (Typ)
3585 then
3586 return False;
3588 else
3589 return True;
3590 end if;
3591 end Has_Good_Profile;
3593 -- Start of processing for Analyze_Stream_TSS_Definition
3595 begin
3596 FOnly := True;
3598 if not Is_Type (U_Ent) then
3599 Error_Msg_N ("local name must be a subtype", Nam);
3600 return;
3602 elsif not Is_First_Subtype (U_Ent) then
3603 Error_Msg_N ("local name must be a first subtype", Nam);
3604 return;
3605 end if;
3607 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3609 -- If Pnam is present, it can be either inherited from an ancestor
3610 -- type (in which case it is legal to redefine it for this type), or
3611 -- be a previous definition of the attribute for the same type (in
3612 -- which case it is illegal).
3614 -- In the first case, it will have been analyzed already, and we
3615 -- can check that its profile does not match the expected profile
3616 -- for a stream attribute of U_Ent. In the second case, either Pnam
3617 -- has been analyzed (and has the expected profile), or it has not
3618 -- been analyzed yet (case of a type that has not been frozen yet
3619 -- and for which the stream attribute has been set using Set_TSS).
3621 if Present (Pnam)
3622 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3623 then
3624 Error_Msg_Sloc := Sloc (Pnam);
3625 Error_Msg_Name_1 := Attr;
3626 Error_Msg_N ("% attribute already defined #", Nam);
3627 return;
3628 end if;
3630 Analyze (Expr);
3632 if Is_Entity_Name (Expr) then
3633 if not Is_Overloaded (Expr) then
3634 if Has_Good_Profile (Entity (Expr)) then
3635 Subp := Entity (Expr);
3636 end if;
3638 else
3639 Get_First_Interp (Expr, I, It);
3640 while Present (It.Nam) loop
3641 if Has_Good_Profile (It.Nam) then
3642 Subp := It.Nam;
3643 exit;
3644 end if;
3646 Get_Next_Interp (I, It);
3647 end loop;
3648 end if;
3649 end if;
3651 if Present (Subp) then
3652 if Is_Abstract_Subprogram (Subp) then
3653 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3654 return;
3656 -- A stream subprogram for an interface type must be a null
3657 -- procedure (RM 13.13.2 (38/3)).
3659 elsif Is_Interface (U_Ent)
3660 and then not Is_Class_Wide_Type (U_Ent)
3661 and then not Inside_A_Generic
3662 and then
3663 (Ekind (Subp) = E_Function
3664 or else
3665 not Null_Present
3666 (Specification
3667 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
3668 then
3669 Error_Msg_N
3670 ("stream subprogram for interface type "
3671 & "must be null procedure", Expr);
3672 end if;
3674 Set_Entity (Expr, Subp);
3675 Set_Etype (Expr, Etype (Subp));
3677 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3679 else
3680 Error_Msg_Name_1 := Attr;
3681 Error_Msg_N ("incorrect expression for% attribute", Expr);
3682 end if;
3683 end Analyze_Stream_TSS_Definition;
3685 ------------------------------
3686 -- Check_Indexing_Functions --
3687 ------------------------------
3689 procedure Check_Indexing_Functions is
3690 Indexing_Found : Boolean := False;
3692 procedure Check_One_Function (Subp : Entity_Id);
3693 -- Check one possible interpretation. Sets Indexing_Found True if a
3694 -- legal indexing function is found.
3696 procedure Illegal_Indexing (Msg : String);
3697 -- Diagnose illegal indexing function if not overloaded. In the
3698 -- overloaded case indicate that no legal interpretation exists.
3700 ------------------------
3701 -- Check_One_Function --
3702 ------------------------
3704 procedure Check_One_Function (Subp : Entity_Id) is
3705 Default_Element : Node_Id;
3706 Ret_Type : constant Entity_Id := Etype (Subp);
3708 begin
3709 if not Is_Overloadable (Subp) then
3710 Illegal_Indexing ("illegal indexing function for type&");
3711 return;
3713 elsif Scope (Subp) /= Scope (Ent) then
3714 if Nkind (Expr) = N_Expanded_Name then
3716 -- Indexing function can't be declared elsewhere
3718 Illegal_Indexing
3719 ("indexing function must be declared in scope of type&");
3720 end if;
3722 return;
3724 elsif No (First_Formal (Subp)) then
3725 Illegal_Indexing
3726 ("Indexing requires a function that applies to type&");
3727 return;
3729 elsif No (Next_Formal (First_Formal (Subp))) then
3730 Illegal_Indexing
3731 ("indexing function must have at least two parameters");
3732 return;
3734 elsif Is_Derived_Type (Ent) then
3735 if (Attr = Name_Constant_Indexing
3736 and then Present
3737 (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing)))
3738 or else
3739 (Attr = Name_Variable_Indexing
3740 and then Present
3741 (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing)))
3742 then
3743 if Debug_Flag_Dot_XX then
3744 null;
3746 else
3747 Illegal_Indexing
3748 ("indexing function already inherited "
3749 & "from parent type");
3750 return;
3751 end if;
3752 end if;
3753 end if;
3755 if not Check_Primitive_Function (Subp) then
3756 Illegal_Indexing
3757 ("Indexing aspect requires a function that applies to type&");
3758 return;
3759 end if;
3761 -- If partial declaration exists, verify that it is not tagged.
3763 if Ekind (Current_Scope) = E_Package
3764 and then Has_Private_Declaration (Ent)
3765 and then From_Aspect_Specification (N)
3766 and then
3767 List_Containing (Parent (Ent)) =
3768 Private_Declarations
3769 (Specification (Unit_Declaration_Node (Current_Scope)))
3770 and then Nkind (N) = N_Attribute_Definition_Clause
3771 then
3772 declare
3773 Decl : Node_Id;
3775 begin
3776 Decl :=
3777 First (Visible_Declarations
3778 (Specification
3779 (Unit_Declaration_Node (Current_Scope))));
3781 while Present (Decl) loop
3782 if Nkind (Decl) = N_Private_Type_Declaration
3783 and then Ent = Full_View (Defining_Identifier (Decl))
3784 and then Tagged_Present (Decl)
3785 and then No (Aspect_Specifications (Decl))
3786 then
3787 Illegal_Indexing
3788 ("Indexing aspect cannot be specified on full view "
3789 & "if partial view is tagged");
3790 return;
3791 end if;
3793 Next (Decl);
3794 end loop;
3795 end;
3796 end if;
3798 -- An indexing function must return either the default element of
3799 -- the container, or a reference type. For variable indexing it
3800 -- must be the latter.
3802 Default_Element :=
3803 Find_Value_Of_Aspect
3804 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3806 if Present (Default_Element) then
3807 Analyze (Default_Element);
3809 if Is_Entity_Name (Default_Element)
3810 and then not Covers (Entity (Default_Element), Ret_Type)
3811 and then False
3812 then
3813 Illegal_Indexing
3814 ("wrong return type for indexing function");
3815 return;
3816 end if;
3817 end if;
3819 -- For variable_indexing the return type must be a reference type
3821 if Attr = Name_Variable_Indexing then
3822 if not Has_Implicit_Dereference (Ret_Type) then
3823 Illegal_Indexing
3824 ("variable indexing must return a reference type");
3825 return;
3827 elsif Is_Access_Constant
3828 (Etype (First_Discriminant (Ret_Type)))
3829 then
3830 Illegal_Indexing
3831 ("variable indexing must return an access to variable");
3832 return;
3833 end if;
3835 else
3836 if Has_Implicit_Dereference (Ret_Type)
3837 and then not
3838 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3839 then
3840 Illegal_Indexing
3841 ("constant indexing must return an access to constant");
3842 return;
3844 elsif Is_Access_Type (Etype (First_Formal (Subp)))
3845 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
3846 then
3847 Illegal_Indexing
3848 ("constant indexing must apply to an access to constant");
3849 return;
3850 end if;
3851 end if;
3853 -- All checks succeeded.
3855 Indexing_Found := True;
3856 end Check_One_Function;
3858 -----------------------
3859 -- Illegal_Indexing --
3860 -----------------------
3862 procedure Illegal_Indexing (Msg : String) is
3863 begin
3864 Error_Msg_NE (Msg, N, Ent);
3865 end Illegal_Indexing;
3867 -- Start of processing for Check_Indexing_Functions
3869 begin
3870 if In_Instance then
3871 return;
3872 end if;
3874 Analyze (Expr);
3876 if not Is_Overloaded (Expr) then
3877 Check_One_Function (Entity (Expr));
3879 else
3880 declare
3881 I : Interp_Index;
3882 It : Interp;
3884 begin
3885 Indexing_Found := False;
3886 Get_First_Interp (Expr, I, It);
3887 while Present (It.Nam) loop
3889 -- Note that analysis will have added the interpretation
3890 -- that corresponds to the dereference. We only check the
3891 -- subprogram itself.
3893 if Is_Overloadable (It.Nam) then
3894 Check_One_Function (It.Nam);
3895 end if;
3897 Get_Next_Interp (I, It);
3898 end loop;
3899 end;
3900 end if;
3902 if not Indexing_Found and then not Error_Posted (N) then
3903 Error_Msg_NE
3904 ("aspect Indexing requires a local function that "
3905 & "applies to type&", Expr, Ent);
3906 end if;
3907 end Check_Indexing_Functions;
3909 ------------------------------
3910 -- Check_Iterator_Functions --
3911 ------------------------------
3913 procedure Check_Iterator_Functions is
3914 Default : Entity_Id;
3916 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3917 -- Check one possible interpretation for validity
3919 ----------------------------
3920 -- Valid_Default_Iterator --
3921 ----------------------------
3923 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3924 Formal : Entity_Id;
3926 begin
3927 if not Check_Primitive_Function (Subp) then
3928 return False;
3929 else
3930 Formal := First_Formal (Subp);
3931 end if;
3933 -- False if any subsequent formal has no default expression
3935 Formal := Next_Formal (Formal);
3936 while Present (Formal) loop
3937 if No (Expression (Parent (Formal))) then
3938 return False;
3939 end if;
3941 Next_Formal (Formal);
3942 end loop;
3944 -- True if all subsequent formals have default expressions
3946 return True;
3947 end Valid_Default_Iterator;
3949 -- Start of processing for Check_Iterator_Functions
3951 begin
3952 Analyze (Expr);
3954 if not Is_Entity_Name (Expr) then
3955 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3956 end if;
3958 if not Is_Overloaded (Expr) then
3959 if not Check_Primitive_Function (Entity (Expr)) then
3960 Error_Msg_NE
3961 ("aspect Indexing requires a function that applies to type&",
3962 Entity (Expr), Ent);
3963 end if;
3965 if not Valid_Default_Iterator (Entity (Expr)) then
3966 Error_Msg_N ("improper function for default iterator", Expr);
3967 end if;
3969 else
3970 Default := Empty;
3971 declare
3972 I : Interp_Index;
3973 It : Interp;
3975 begin
3976 Get_First_Interp (Expr, I, It);
3977 while Present (It.Nam) loop
3978 if not Check_Primitive_Function (It.Nam)
3979 or else not Valid_Default_Iterator (It.Nam)
3980 then
3981 Remove_Interp (I);
3983 elsif Present (Default) then
3984 Error_Msg_N ("default iterator must be unique", Expr);
3986 else
3987 Default := It.Nam;
3988 end if;
3990 Get_Next_Interp (I, It);
3991 end loop;
3992 end;
3994 if Present (Default) then
3995 Set_Entity (Expr, Default);
3996 Set_Is_Overloaded (Expr, False);
3997 end if;
3998 end if;
3999 end Check_Iterator_Functions;
4001 -------------------------------
4002 -- Check_Primitive_Function --
4003 -------------------------------
4005 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4006 Ctrl : Entity_Id;
4008 begin
4009 if Ekind (Subp) /= E_Function then
4010 return False;
4011 end if;
4013 if No (First_Formal (Subp)) then
4014 return False;
4015 else
4016 Ctrl := Etype (First_Formal (Subp));
4017 end if;
4019 -- Type of formal may be the class-wide type, an access to such,
4020 -- or an incomplete view.
4022 if Ctrl = Ent
4023 or else Ctrl = Class_Wide_Type (Ent)
4024 or else
4025 (Ekind (Ctrl) = E_Anonymous_Access_Type
4026 and then (Designated_Type (Ctrl) = Ent
4027 or else
4028 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4029 or else
4030 (Ekind (Ctrl) = E_Incomplete_Type
4031 and then Full_View (Ctrl) = Ent)
4032 then
4033 null;
4034 else
4035 return False;
4036 end if;
4038 return True;
4039 end Check_Primitive_Function;
4041 ----------------------
4042 -- Duplicate_Clause --
4043 ----------------------
4045 function Duplicate_Clause return Boolean is
4046 A : Node_Id;
4048 begin
4049 -- Nothing to do if this attribute definition clause comes from
4050 -- an aspect specification, since we could not be duplicating an
4051 -- explicit clause, and we dealt with the case of duplicated aspects
4052 -- in Analyze_Aspect_Specifications.
4054 if From_Aspect_Specification (N) then
4055 return False;
4056 end if;
4058 -- Otherwise current clause may duplicate previous clause, or a
4059 -- previously given pragma or aspect specification for the same
4060 -- aspect.
4062 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4064 if Present (A) then
4065 Error_Msg_Name_1 := Chars (N);
4066 Error_Msg_Sloc := Sloc (A);
4068 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4069 return True;
4070 end if;
4072 return False;
4073 end Duplicate_Clause;
4075 -- Start of processing for Analyze_Attribute_Definition_Clause
4077 begin
4078 -- The following code is a defense against recursion. Not clear that
4079 -- this can happen legitimately, but perhaps some error situations can
4080 -- cause it, and we did see this recursion during testing.
4082 if Analyzed (N) then
4083 return;
4084 else
4085 Set_Analyzed (N, True);
4086 end if;
4088 -- Ignore some selected attributes in CodePeer mode since they are not
4089 -- relevant in this context.
4091 if CodePeer_Mode then
4092 case Id is
4094 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4095 -- internal representation of types by implicitly packing them.
4097 when Attribute_Component_Size =>
4098 Rewrite (N, Make_Null_Statement (Sloc (N)));
4099 return;
4101 when others =>
4102 null;
4103 end case;
4104 end if;
4106 -- Process Ignore_Rep_Clauses option
4108 if Ignore_Rep_Clauses then
4109 case Id is
4111 -- The following should be ignored. They do not affect legality
4112 -- and may be target dependent. The basic idea of -gnatI is to
4113 -- ignore any rep clauses that may be target dependent but do not
4114 -- affect legality (except possibly to be rejected because they
4115 -- are incompatible with the compilation target).
4117 when Attribute_Alignment |
4118 Attribute_Bit_Order |
4119 Attribute_Component_Size |
4120 Attribute_Machine_Radix |
4121 Attribute_Object_Size |
4122 Attribute_Size |
4123 Attribute_Small |
4124 Attribute_Stream_Size |
4125 Attribute_Value_Size =>
4126 Kill_Rep_Clause (N);
4127 return;
4129 -- The following should not be ignored, because in the first place
4130 -- they are reasonably portable, and should not cause problems
4131 -- in compiling code from another target, and also they do affect
4132 -- legality, e.g. failing to provide a stream attribute for a type
4133 -- may make a program illegal.
4135 when Attribute_External_Tag |
4136 Attribute_Input |
4137 Attribute_Output |
4138 Attribute_Read |
4139 Attribute_Simple_Storage_Pool |
4140 Attribute_Storage_Pool |
4141 Attribute_Storage_Size |
4142 Attribute_Write =>
4143 null;
4145 -- We do not do anything here with address clauses, they will be
4146 -- removed by Freeze later on, but for now, it works better to
4147 -- keep then in the tree.
4149 when Attribute_Address =>
4150 null;
4152 -- Other cases are errors ("attribute& cannot be set with
4153 -- definition clause"), which will be caught below.
4155 when others =>
4156 null;
4157 end case;
4158 end if;
4160 Analyze (Nam);
4161 Ent := Entity (Nam);
4163 if Rep_Item_Too_Early (Ent, N) then
4164 return;
4165 end if;
4167 -- Rep clause applies to full view of incomplete type or private type if
4168 -- we have one (if not, this is a premature use of the type). However,
4169 -- certain semantic checks need to be done on the specified entity (i.e.
4170 -- the private view), so we save it in Ent.
4172 if Is_Private_Type (Ent)
4173 and then Is_Derived_Type (Ent)
4174 and then not Is_Tagged_Type (Ent)
4175 and then No (Full_View (Ent))
4176 then
4177 -- If this is a private type whose completion is a derivation from
4178 -- another private type, there is no full view, and the attribute
4179 -- belongs to the type itself, not its underlying parent.
4181 U_Ent := Ent;
4183 elsif Ekind (Ent) = E_Incomplete_Type then
4185 -- The attribute applies to the full view, set the entity of the
4186 -- attribute definition accordingly.
4188 Ent := Underlying_Type (Ent);
4189 U_Ent := Ent;
4190 Set_Entity (Nam, Ent);
4192 else
4193 U_Ent := Underlying_Type (Ent);
4194 end if;
4196 -- Avoid cascaded error
4198 if Etype (Nam) = Any_Type then
4199 return;
4201 -- Must be declared in current scope or in case of an aspect
4202 -- specification, must be visible in current scope.
4204 elsif Scope (Ent) /= Current_Scope
4205 and then
4206 not (From_Aspect_Specification (N)
4207 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4208 then
4209 Error_Msg_N ("entity must be declared in this scope", Nam);
4210 return;
4212 -- Must not be a source renaming (we do have some cases where the
4213 -- expander generates a renaming, and those cases are OK, in such
4214 -- cases any attribute applies to the renamed object as well).
4216 elsif Is_Object (Ent)
4217 and then Present (Renamed_Object (Ent))
4218 then
4219 -- Case of renamed object from source, this is an error
4221 if Comes_From_Source (Renamed_Object (Ent)) then
4222 Get_Name_String (Chars (N));
4223 Error_Msg_Strlen := Name_Len;
4224 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4225 Error_Msg_N
4226 ("~ clause not allowed for a renaming declaration "
4227 & "(RM 13.1(6))", Nam);
4228 return;
4230 -- For the case of a compiler generated renaming, the attribute
4231 -- definition clause applies to the renamed object created by the
4232 -- expander. The easiest general way to handle this is to create a
4233 -- copy of the attribute definition clause for this object.
4235 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4236 Insert_Action (N,
4237 Make_Attribute_Definition_Clause (Loc,
4238 Name =>
4239 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4240 Chars => Chars (N),
4241 Expression => Duplicate_Subexpr (Expression (N))));
4243 -- If the renamed object is not an entity, it must be a dereference
4244 -- of an unconstrained function call, and we must introduce a new
4245 -- declaration to capture the expression. This is needed in the case
4246 -- of 'Alignment, where the original declaration must be rewritten.
4248 else
4249 pragma Assert
4250 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4251 null;
4252 end if;
4254 -- If no underlying entity, use entity itself, applies to some
4255 -- previously detected error cases ???
4257 elsif No (U_Ent) then
4258 U_Ent := Ent;
4260 -- Cannot specify for a subtype (exception Object/Value_Size)
4262 elsif Is_Type (U_Ent)
4263 and then not Is_First_Subtype (U_Ent)
4264 and then Id /= Attribute_Object_Size
4265 and then Id /= Attribute_Value_Size
4266 and then not From_At_Mod (N)
4267 then
4268 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4269 return;
4270 end if;
4272 Set_Entity (N, U_Ent);
4273 Check_Restriction_No_Use_Of_Attribute (N);
4275 -- Switch on particular attribute
4277 case Id is
4279 -------------
4280 -- Address --
4281 -------------
4283 -- Address attribute definition clause
4285 when Attribute_Address => Address : begin
4287 -- A little error check, catch for X'Address use X'Address;
4289 if Nkind (Nam) = N_Identifier
4290 and then Nkind (Expr) = N_Attribute_Reference
4291 and then Attribute_Name (Expr) = Name_Address
4292 and then Nkind (Prefix (Expr)) = N_Identifier
4293 and then Chars (Nam) = Chars (Prefix (Expr))
4294 then
4295 Error_Msg_NE
4296 ("address for & is self-referencing", Prefix (Expr), Ent);
4297 return;
4298 end if;
4300 -- Not that special case, carry on with analysis of expression
4302 Analyze_And_Resolve (Expr, RTE (RE_Address));
4304 -- Even when ignoring rep clauses we need to indicate that the
4305 -- entity has an address clause and thus it is legal to declare
4306 -- it imported. Freeze will get rid of the address clause later.
4308 if Ignore_Rep_Clauses then
4309 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4310 Record_Rep_Item (U_Ent, N);
4311 end if;
4313 return;
4314 end if;
4316 if Duplicate_Clause then
4317 null;
4319 -- Case of address clause for subprogram
4321 elsif Is_Subprogram (U_Ent) then
4322 if Has_Homonym (U_Ent) then
4323 Error_Msg_N
4324 ("address clause cannot be given " &
4325 "for overloaded subprogram",
4326 Nam);
4327 return;
4328 end if;
4330 -- For subprograms, all address clauses are permitted, and we
4331 -- mark the subprogram as having a deferred freeze so that Gigi
4332 -- will not elaborate it too soon.
4334 -- Above needs more comments, what is too soon about???
4336 Set_Has_Delayed_Freeze (U_Ent);
4338 -- Case of address clause for entry
4340 elsif Ekind (U_Ent) = E_Entry then
4341 if Nkind (Parent (N)) = N_Task_Body then
4342 Error_Msg_N
4343 ("entry address must be specified in task spec", Nam);
4344 return;
4345 end if;
4347 -- For entries, we require a constant address
4349 Check_Constant_Address_Clause (Expr, U_Ent);
4351 -- Special checks for task types
4353 if Is_Task_Type (Scope (U_Ent))
4354 and then Comes_From_Source (Scope (U_Ent))
4355 then
4356 Error_Msg_N
4357 ("??entry address declared for entry in task type", N);
4358 Error_Msg_N
4359 ("\??only one task can be declared of this type", N);
4360 end if;
4362 -- Entry address clauses are obsolescent
4364 Check_Restriction (No_Obsolescent_Features, N);
4366 if Warn_On_Obsolescent_Feature then
4367 Error_Msg_N
4368 ("?j?attaching interrupt to task entry is an " &
4369 "obsolescent feature (RM J.7.1)", N);
4370 Error_Msg_N
4371 ("\?j?use interrupt procedure instead", N);
4372 end if;
4374 -- Case of an address clause for a controlled object which we
4375 -- consider to be erroneous.
4377 elsif Is_Controlled (Etype (U_Ent))
4378 or else Has_Controlled_Component (Etype (U_Ent))
4379 then
4380 Error_Msg_NE
4381 ("??controlled object& must not be overlaid", Nam, U_Ent);
4382 Error_Msg_N
4383 ("\??Program_Error will be raised at run time", Nam);
4384 Insert_Action (Declaration_Node (U_Ent),
4385 Make_Raise_Program_Error (Loc,
4386 Reason => PE_Overlaid_Controlled_Object));
4387 return;
4389 -- Case of address clause for a (non-controlled) object
4391 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4392 declare
4393 Expr : constant Node_Id := Expression (N);
4394 O_Ent : Entity_Id;
4395 Off : Boolean;
4397 begin
4398 -- Exported variables cannot have an address clause, because
4399 -- this cancels the effect of the pragma Export.
4401 if Is_Exported (U_Ent) then
4402 Error_Msg_N
4403 ("cannot export object with address clause", Nam);
4404 return;
4405 end if;
4407 Find_Overlaid_Entity (N, O_Ent, Off);
4409 -- Overlaying controlled objects is erroneous
4411 if Present (O_Ent)
4412 and then (Has_Controlled_Component (Etype (O_Ent))
4413 or else Is_Controlled (Etype (O_Ent)))
4414 then
4415 Error_Msg_N
4416 ("??cannot overlay with controlled object", Expr);
4417 Error_Msg_N
4418 ("\??Program_Error will be raised at run time", Expr);
4419 Insert_Action (Declaration_Node (U_Ent),
4420 Make_Raise_Program_Error (Loc,
4421 Reason => PE_Overlaid_Controlled_Object));
4422 return;
4424 elsif Present (O_Ent)
4425 and then Ekind (U_Ent) = E_Constant
4426 and then not Is_Constant_Object (O_Ent)
4427 then
4428 Error_Msg_N ("??constant overlays a variable", Expr);
4430 -- Imported variables can have an address clause, but then
4431 -- the import is pretty meaningless except to suppress
4432 -- initializations, so we do not need such variables to
4433 -- be statically allocated (and in fact it causes trouble
4434 -- if the address clause is a local value).
4436 elsif Is_Imported (U_Ent) then
4437 Set_Is_Statically_Allocated (U_Ent, False);
4438 end if;
4440 -- We mark a possible modification of a variable with an
4441 -- address clause, since it is likely aliasing is occurring.
4443 Note_Possible_Modification (Nam, Sure => False);
4445 -- Here we are checking for explicit overlap of one variable
4446 -- by another, and if we find this then mark the overlapped
4447 -- variable as also being volatile to prevent unwanted
4448 -- optimizations. This is a significant pessimization so
4449 -- avoid it when there is an offset, i.e. when the object
4450 -- is composite; they cannot be optimized easily anyway.
4452 if Present (O_Ent)
4453 and then Is_Object (O_Ent)
4454 and then not Off
4456 -- The following test is an expedient solution to what
4457 -- is really a problem in CodePeer. Suppressing the
4458 -- Set_Treat_As_Volatile call here prevents later
4459 -- generation (in some cases) of trees that CodePeer
4460 -- should, but currently does not, handle correctly.
4461 -- This test should probably be removed when CodePeer
4462 -- is improved, just because we want the tree CodePeer
4463 -- analyzes to match the tree for which we generate code
4464 -- as closely as is practical. ???
4466 and then not CodePeer_Mode
4467 then
4468 -- ??? O_Ent might not be in current unit
4470 Set_Treat_As_Volatile (O_Ent);
4471 end if;
4473 -- Legality checks on the address clause for initialized
4474 -- objects is deferred until the freeze point, because
4475 -- a subsequent pragma might indicate that the object
4476 -- is imported and thus not initialized. Also, the address
4477 -- clause might involve entities that have yet to be
4478 -- elaborated.
4480 Set_Has_Delayed_Freeze (U_Ent);
4482 -- If an initialization call has been generated for this
4483 -- object, it needs to be deferred to after the freeze node
4484 -- we have just now added, otherwise GIGI will see a
4485 -- reference to the variable (as actual to the IP call)
4486 -- before its definition.
4488 declare
4489 Init_Call : constant Node_Id :=
4490 Remove_Init_Call (U_Ent, N);
4492 begin
4493 if Present (Init_Call) then
4494 Append_Freeze_Action (U_Ent, Init_Call);
4496 -- Reset Initialization_Statements pointer so that
4497 -- if there is a pragma Import further down, it can
4498 -- clear any default initialization.
4500 Set_Initialization_Statements (U_Ent, Init_Call);
4501 end if;
4502 end;
4504 if Is_Exported (U_Ent) then
4505 Error_Msg_N
4506 ("& cannot be exported if an address clause is given",
4507 Nam);
4508 Error_Msg_N
4509 ("\define and export a variable "
4510 & "that holds its address instead", Nam);
4511 end if;
4513 -- Entity has delayed freeze, so we will generate an
4514 -- alignment check at the freeze point unless suppressed.
4516 if not Range_Checks_Suppressed (U_Ent)
4517 and then not Alignment_Checks_Suppressed (U_Ent)
4518 then
4519 Set_Check_Address_Alignment (N);
4520 end if;
4522 -- Kill the size check code, since we are not allocating
4523 -- the variable, it is somewhere else.
4525 Kill_Size_Check_Code (U_Ent);
4527 -- If the address clause is of the form:
4529 -- for Y'Address use X'Address
4531 -- or
4533 -- Const : constant Address := X'Address;
4534 -- ...
4535 -- for Y'Address use Const;
4537 -- then we make an entry in the table for checking the size
4538 -- and alignment of the overlaying variable. We defer this
4539 -- check till after code generation to take full advantage
4540 -- of the annotation done by the back end.
4542 -- If the entity has a generic type, the check will be
4543 -- performed in the instance if the actual type justifies
4544 -- it, and we do not insert the clause in the table to
4545 -- prevent spurious warnings.
4547 -- Note: we used to test Comes_From_Source and only give
4548 -- this warning for source entities, but we have removed
4549 -- this test. It really seems bogus to generate overlays
4550 -- that would trigger this warning in generated code.
4551 -- Furthermore, by removing the test, we handle the
4552 -- aspect case properly.
4554 if Address_Clause_Overlay_Warnings
4555 and then Present (O_Ent)
4556 and then Is_Object (O_Ent)
4557 then
4558 if not Is_Generic_Type (Etype (U_Ent)) then
4559 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4560 end if;
4562 -- If variable overlays a constant view, and we are
4563 -- warning on overlays, then mark the variable as
4564 -- overlaying a constant (we will give warnings later
4565 -- if this variable is assigned).
4567 if Is_Constant_Object (O_Ent)
4568 and then Ekind (U_Ent) = E_Variable
4569 then
4570 Set_Overlays_Constant (U_Ent);
4571 end if;
4572 end if;
4573 end;
4575 -- Not a valid entity for an address clause
4577 else
4578 Error_Msg_N ("address cannot be given for &", Nam);
4579 end if;
4580 end Address;
4582 ---------------
4583 -- Alignment --
4584 ---------------
4586 -- Alignment attribute definition clause
4588 when Attribute_Alignment => Alignment : declare
4589 Align : constant Uint := Get_Alignment_Value (Expr);
4590 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4592 begin
4593 FOnly := True;
4595 if not Is_Type (U_Ent)
4596 and then Ekind (U_Ent) /= E_Variable
4597 and then Ekind (U_Ent) /= E_Constant
4598 then
4599 Error_Msg_N ("alignment cannot be given for &", Nam);
4601 elsif Duplicate_Clause then
4602 null;
4604 elsif Align /= No_Uint then
4605 Set_Has_Alignment_Clause (U_Ent);
4607 -- Tagged type case, check for attempt to set alignment to a
4608 -- value greater than Max_Align, and reset if so.
4610 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4611 Error_Msg_N
4612 ("alignment for & set to Maximum_Aligment??", Nam);
4613 Set_Alignment (U_Ent, Max_Align);
4615 -- All other cases
4617 else
4618 Set_Alignment (U_Ent, Align);
4619 end if;
4621 -- For an array type, U_Ent is the first subtype. In that case,
4622 -- also set the alignment of the anonymous base type so that
4623 -- other subtypes (such as the itypes for aggregates of the
4624 -- type) also receive the expected alignment.
4626 if Is_Array_Type (U_Ent) then
4627 Set_Alignment (Base_Type (U_Ent), Align);
4628 end if;
4629 end if;
4630 end Alignment;
4632 ---------------
4633 -- Bit_Order --
4634 ---------------
4636 -- Bit_Order attribute definition clause
4638 when Attribute_Bit_Order => Bit_Order : declare
4639 begin
4640 if not Is_Record_Type (U_Ent) then
4641 Error_Msg_N
4642 ("Bit_Order can only be defined for record type", Nam);
4644 elsif Duplicate_Clause then
4645 null;
4647 else
4648 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4650 if Etype (Expr) = Any_Type then
4651 return;
4653 elsif not Is_OK_Static_Expression (Expr) then
4654 Flag_Non_Static_Expr
4655 ("Bit_Order requires static expression!", Expr);
4657 else
4658 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4659 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4660 end if;
4661 end if;
4662 end if;
4663 end Bit_Order;
4665 --------------------
4666 -- Component_Size --
4667 --------------------
4669 -- Component_Size attribute definition clause
4671 when Attribute_Component_Size => Component_Size_Case : declare
4672 Csize : constant Uint := Static_Integer (Expr);
4673 Ctyp : Entity_Id;
4674 Btype : Entity_Id;
4675 Biased : Boolean;
4676 New_Ctyp : Entity_Id;
4677 Decl : Node_Id;
4679 begin
4680 if not Is_Array_Type (U_Ent) then
4681 Error_Msg_N ("component size requires array type", Nam);
4682 return;
4683 end if;
4685 Btype := Base_Type (U_Ent);
4686 Ctyp := Component_Type (Btype);
4688 if Duplicate_Clause then
4689 null;
4691 elsif Rep_Item_Too_Early (Btype, N) then
4692 null;
4694 elsif Csize /= No_Uint then
4695 Check_Size (Expr, Ctyp, Csize, Biased);
4697 -- For the biased case, build a declaration for a subtype that
4698 -- will be used to represent the biased subtype that reflects
4699 -- the biased representation of components. We need the subtype
4700 -- to get proper conversions on referencing elements of the
4701 -- array. Note: component size clauses are ignored in VM mode.
4703 if VM_Target = No_VM then
4704 if Biased then
4705 New_Ctyp :=
4706 Make_Defining_Identifier (Loc,
4707 Chars =>
4708 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4710 Decl :=
4711 Make_Subtype_Declaration (Loc,
4712 Defining_Identifier => New_Ctyp,
4713 Subtype_Indication =>
4714 New_Occurrence_Of (Component_Type (Btype), Loc));
4716 Set_Parent (Decl, N);
4717 Analyze (Decl, Suppress => All_Checks);
4719 Set_Has_Delayed_Freeze (New_Ctyp, False);
4720 Set_Esize (New_Ctyp, Csize);
4721 Set_RM_Size (New_Ctyp, Csize);
4722 Init_Alignment (New_Ctyp);
4723 Set_Is_Itype (New_Ctyp, True);
4724 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4726 Set_Component_Type (Btype, New_Ctyp);
4727 Set_Biased (New_Ctyp, N, "component size clause");
4728 end if;
4730 Set_Component_Size (Btype, Csize);
4732 -- For VM case, we ignore component size clauses
4734 else
4735 -- Give a warning unless we are in GNAT mode, in which case
4736 -- the warning is suppressed since it is not useful.
4738 if not GNAT_Mode then
4739 Error_Msg_N
4740 ("component size ignored in this configuration??", N);
4741 end if;
4742 end if;
4744 -- Deal with warning on overridden size
4746 if Warn_On_Overridden_Size
4747 and then Has_Size_Clause (Ctyp)
4748 and then RM_Size (Ctyp) /= Csize
4749 then
4750 Error_Msg_NE
4751 ("component size overrides size clause for&?S?", N, Ctyp);
4752 end if;
4754 Set_Has_Component_Size_Clause (Btype, True);
4755 Set_Has_Non_Standard_Rep (Btype, True);
4756 end if;
4757 end Component_Size_Case;
4759 -----------------------
4760 -- Constant_Indexing --
4761 -----------------------
4763 when Attribute_Constant_Indexing =>
4764 Check_Indexing_Functions;
4766 ---------
4767 -- CPU --
4768 ---------
4770 when Attribute_CPU => CPU :
4771 begin
4772 -- CPU attribute definition clause not allowed except from aspect
4773 -- specification.
4775 if From_Aspect_Specification (N) then
4776 if not Is_Task_Type (U_Ent) then
4777 Error_Msg_N ("CPU can only be defined for task", Nam);
4779 elsif Duplicate_Clause then
4780 null;
4782 else
4783 -- The expression must be analyzed in the special manner
4784 -- described in "Handling of Default and Per-Object
4785 -- Expressions" in sem.ads.
4787 -- The visibility to the discriminants must be restored
4789 Push_Scope_And_Install_Discriminants (U_Ent);
4790 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4791 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4793 if not Is_OK_Static_Expression (Expr) then
4794 Check_Restriction (Static_Priorities, Expr);
4795 end if;
4796 end if;
4798 else
4799 Error_Msg_N
4800 ("attribute& cannot be set with definition clause", N);
4801 end if;
4802 end CPU;
4804 ----------------------
4805 -- Default_Iterator --
4806 ----------------------
4808 when Attribute_Default_Iterator => Default_Iterator : declare
4809 Func : Entity_Id;
4810 Typ : Entity_Id;
4812 begin
4813 if not Is_Tagged_Type (U_Ent) then
4814 Error_Msg_N
4815 ("aspect Default_Iterator applies to tagged type", Nam);
4816 end if;
4818 Check_Iterator_Functions;
4820 Analyze (Expr);
4822 if not Is_Entity_Name (Expr)
4823 or else Ekind (Entity (Expr)) /= E_Function
4824 then
4825 Error_Msg_N ("aspect Iterator must be a function", Expr);
4826 else
4827 Func := Entity (Expr);
4828 end if;
4830 -- The type of the first parameter must be T, T'class, or a
4831 -- corresponding access type (5.5.1 (8/3)
4833 if No (First_Formal (Func)) then
4834 Typ := Empty;
4835 else
4836 Typ := Etype (First_Formal (Func));
4837 end if;
4839 if Typ = U_Ent
4840 or else Typ = Class_Wide_Type (U_Ent)
4841 or else (Is_Access_Type (Typ)
4842 and then Designated_Type (Typ) = U_Ent)
4843 or else (Is_Access_Type (Typ)
4844 and then Designated_Type (Typ) =
4845 Class_Wide_Type (U_Ent))
4846 then
4847 null;
4849 else
4850 Error_Msg_NE
4851 ("Default Iterator must be a primitive of&", Func, U_Ent);
4852 end if;
4853 end Default_Iterator;
4855 ------------------------
4856 -- Dispatching_Domain --
4857 ------------------------
4859 when Attribute_Dispatching_Domain => Dispatching_Domain :
4860 begin
4861 -- Dispatching_Domain attribute definition clause not allowed
4862 -- except from aspect specification.
4864 if From_Aspect_Specification (N) then
4865 if not Is_Task_Type (U_Ent) then
4866 Error_Msg_N
4867 ("Dispatching_Domain can only be defined for task", Nam);
4869 elsif Duplicate_Clause then
4870 null;
4872 else
4873 -- The expression must be analyzed in the special manner
4874 -- described in "Handling of Default and Per-Object
4875 -- Expressions" in sem.ads.
4877 -- The visibility to the discriminants must be restored
4879 Push_Scope_And_Install_Discriminants (U_Ent);
4881 Preanalyze_Spec_Expression
4882 (Expr, RTE (RE_Dispatching_Domain));
4884 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4885 end if;
4887 else
4888 Error_Msg_N
4889 ("attribute& cannot be set with definition clause", N);
4890 end if;
4891 end Dispatching_Domain;
4893 ------------------
4894 -- External_Tag --
4895 ------------------
4897 when Attribute_External_Tag => External_Tag :
4898 begin
4899 if not Is_Tagged_Type (U_Ent) then
4900 Error_Msg_N ("should be a tagged type", Nam);
4901 end if;
4903 if Duplicate_Clause then
4904 null;
4906 else
4907 Analyze_And_Resolve (Expr, Standard_String);
4909 if not Is_OK_Static_Expression (Expr) then
4910 Flag_Non_Static_Expr
4911 ("static string required for tag name!", Nam);
4912 end if;
4914 if VM_Target /= No_VM then
4915 Error_Msg_Name_1 := Attr;
4916 Error_Msg_N
4917 ("% attribute unsupported in this configuration", Nam);
4918 end if;
4920 if not Is_Library_Level_Entity (U_Ent) then
4921 Error_Msg_NE
4922 ("??non-unique external tag supplied for &", N, U_Ent);
4923 Error_Msg_N
4924 ("\??same external tag applies to all "
4925 & "subprogram calls", N);
4926 Error_Msg_N
4927 ("\??corresponding internal tag cannot be obtained", N);
4928 end if;
4929 end if;
4930 end External_Tag;
4932 --------------------------
4933 -- Implicit_Dereference --
4934 --------------------------
4936 when Attribute_Implicit_Dereference =>
4938 -- Legality checks already performed at the point of the type
4939 -- declaration, aspect is not delayed.
4941 null;
4943 -----------
4944 -- Input --
4945 -----------
4947 when Attribute_Input =>
4948 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4949 Set_Has_Specified_Stream_Input (Ent);
4951 ------------------------
4952 -- Interrupt_Priority --
4953 ------------------------
4955 when Attribute_Interrupt_Priority => Interrupt_Priority :
4956 begin
4957 -- Interrupt_Priority attribute definition clause not allowed
4958 -- except from aspect specification.
4960 if From_Aspect_Specification (N) then
4961 if not Is_Concurrent_Type (U_Ent) then
4962 Error_Msg_N
4963 ("Interrupt_Priority can only be defined for task "
4964 & "and protected object", Nam);
4966 elsif Duplicate_Clause then
4967 null;
4969 else
4970 -- The expression must be analyzed in the special manner
4971 -- described in "Handling of Default and Per-Object
4972 -- Expressions" in sem.ads.
4974 -- The visibility to the discriminants must be restored
4976 Push_Scope_And_Install_Discriminants (U_Ent);
4978 Preanalyze_Spec_Expression
4979 (Expr, RTE (RE_Interrupt_Priority));
4981 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4982 end if;
4984 else
4985 Error_Msg_N
4986 ("attribute& cannot be set with definition clause", N);
4987 end if;
4988 end Interrupt_Priority;
4990 --------------
4991 -- Iterable --
4992 --------------
4994 when Attribute_Iterable =>
4995 Analyze (Expr);
4997 if Nkind (Expr) /= N_Aggregate then
4998 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
4999 end if;
5001 declare
5002 Assoc : Node_Id;
5004 begin
5005 Assoc := First (Component_Associations (Expr));
5006 while Present (Assoc) loop
5007 if not Is_Entity_Name (Expression (Assoc)) then
5008 Error_Msg_N ("value must be a function", Assoc);
5009 end if;
5011 Next (Assoc);
5012 end loop;
5013 end;
5015 ----------------------
5016 -- Iterator_Element --
5017 ----------------------
5019 when Attribute_Iterator_Element =>
5020 Analyze (Expr);
5022 if not Is_Entity_Name (Expr)
5023 or else not Is_Type (Entity (Expr))
5024 then
5025 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5026 end if;
5028 -------------------
5029 -- Machine_Radix --
5030 -------------------
5032 -- Machine radix attribute definition clause
5034 when Attribute_Machine_Radix => Machine_Radix : declare
5035 Radix : constant Uint := Static_Integer (Expr);
5037 begin
5038 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5039 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5041 elsif Duplicate_Clause then
5042 null;
5044 elsif Radix /= No_Uint then
5045 Set_Has_Machine_Radix_Clause (U_Ent);
5046 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5048 if Radix = 2 then
5049 null;
5050 elsif Radix = 10 then
5051 Set_Machine_Radix_10 (U_Ent);
5052 else
5053 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5054 end if;
5055 end if;
5056 end Machine_Radix;
5058 -----------------
5059 -- Object_Size --
5060 -----------------
5062 -- Object_Size attribute definition clause
5064 when Attribute_Object_Size => Object_Size : declare
5065 Size : constant Uint := Static_Integer (Expr);
5067 Biased : Boolean;
5068 pragma Warnings (Off, Biased);
5070 begin
5071 if not Is_Type (U_Ent) then
5072 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5074 elsif Duplicate_Clause then
5075 null;
5077 else
5078 Check_Size (Expr, U_Ent, Size, Biased);
5080 if Is_Scalar_Type (U_Ent) then
5081 if Size /= 8 and then Size /= 16 and then Size /= 32
5082 and then UI_Mod (Size, 64) /= 0
5083 then
5084 Error_Msg_N
5085 ("Object_Size must be 8, 16, 32, or multiple of 64",
5086 Expr);
5087 end if;
5089 elsif Size mod 8 /= 0 then
5090 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5091 end if;
5093 Set_Esize (U_Ent, Size);
5094 Set_Has_Object_Size_Clause (U_Ent);
5095 Alignment_Check_For_Size_Change (U_Ent, Size);
5096 end if;
5097 end Object_Size;
5099 ------------
5100 -- Output --
5101 ------------
5103 when Attribute_Output =>
5104 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5105 Set_Has_Specified_Stream_Output (Ent);
5107 --------------
5108 -- Priority --
5109 --------------
5111 when Attribute_Priority => Priority :
5112 begin
5113 -- Priority attribute definition clause not allowed except from
5114 -- aspect specification.
5116 if From_Aspect_Specification (N) then
5117 if not (Is_Concurrent_Type (U_Ent)
5118 or else Ekind (U_Ent) = E_Procedure)
5119 then
5120 Error_Msg_N
5121 ("Priority can only be defined for task and protected "
5122 & "object", Nam);
5124 elsif Duplicate_Clause then
5125 null;
5127 else
5128 -- The expression must be analyzed in the special manner
5129 -- described in "Handling of Default and Per-Object
5130 -- Expressions" in sem.ads.
5132 -- The visibility to the discriminants must be restored
5134 Push_Scope_And_Install_Discriminants (U_Ent);
5135 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5136 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5138 if not Is_OK_Static_Expression (Expr) then
5139 Check_Restriction (Static_Priorities, Expr);
5140 end if;
5141 end if;
5143 else
5144 Error_Msg_N
5145 ("attribute& cannot be set with definition clause", N);
5146 end if;
5147 end Priority;
5149 ----------
5150 -- Read --
5151 ----------
5153 when Attribute_Read =>
5154 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5155 Set_Has_Specified_Stream_Read (Ent);
5157 --------------------------
5158 -- Scalar_Storage_Order --
5159 --------------------------
5161 -- Scalar_Storage_Order attribute definition clause
5163 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5164 begin
5165 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5166 Error_Msg_N
5167 ("Scalar_Storage_Order can only be defined for "
5168 & "record or array type", Nam);
5170 elsif Duplicate_Clause then
5171 null;
5173 else
5174 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5176 if Etype (Expr) = Any_Type then
5177 return;
5179 elsif not Is_OK_Static_Expression (Expr) then
5180 Flag_Non_Static_Expr
5181 ("Scalar_Storage_Order requires static expression!", Expr);
5183 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5185 -- Here for the case of a non-default (i.e. non-confirming)
5186 -- Scalar_Storage_Order attribute definition.
5188 if Support_Nondefault_SSO_On_Target then
5189 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5190 else
5191 Error_Msg_N
5192 ("non-default Scalar_Storage_Order "
5193 & "not supported on target", Expr);
5194 end if;
5195 end if;
5197 -- Clear SSO default indications since explicit setting of the
5198 -- order overrides the defaults.
5200 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5201 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5202 end if;
5203 end Scalar_Storage_Order;
5205 ----------
5206 -- Size --
5207 ----------
5209 -- Size attribute definition clause
5211 when Attribute_Size => Size : declare
5212 Size : constant Uint := Static_Integer (Expr);
5213 Etyp : Entity_Id;
5214 Biased : Boolean;
5216 begin
5217 FOnly := True;
5219 if Duplicate_Clause then
5220 null;
5222 elsif not Is_Type (U_Ent)
5223 and then Ekind (U_Ent) /= E_Variable
5224 and then Ekind (U_Ent) /= E_Constant
5225 then
5226 Error_Msg_N ("size cannot be given for &", Nam);
5228 elsif Is_Array_Type (U_Ent)
5229 and then not Is_Constrained (U_Ent)
5230 then
5231 Error_Msg_N
5232 ("size cannot be given for unconstrained array", Nam);
5234 elsif Size /= No_Uint then
5235 if VM_Target /= No_VM and then not GNAT_Mode then
5237 -- Size clause is not handled properly on VM targets.
5238 -- Display a warning unless we are in GNAT mode, in which
5239 -- case this is useless.
5241 Error_Msg_N
5242 ("size clauses are ignored in this configuration??", N);
5243 end if;
5245 if Is_Type (U_Ent) then
5246 Etyp := U_Ent;
5247 else
5248 Etyp := Etype (U_Ent);
5249 end if;
5251 -- Check size, note that Gigi is in charge of checking that the
5252 -- size of an array or record type is OK. Also we do not check
5253 -- the size in the ordinary fixed-point case, since it is too
5254 -- early to do so (there may be subsequent small clause that
5255 -- affects the size). We can check the size if a small clause
5256 -- has already been given.
5258 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5259 or else Has_Small_Clause (U_Ent)
5260 then
5261 Check_Size (Expr, Etyp, Size, Biased);
5262 Set_Biased (U_Ent, N, "size clause", Biased);
5263 end if;
5265 -- For types set RM_Size and Esize if possible
5267 if Is_Type (U_Ent) then
5268 Set_RM_Size (U_Ent, Size);
5270 -- For elementary types, increase Object_Size to power of 2,
5271 -- but not less than a storage unit in any case (normally
5272 -- this means it will be byte addressable).
5274 -- For all other types, nothing else to do, we leave Esize
5275 -- (object size) unset, the back end will set it from the
5276 -- size and alignment in an appropriate manner.
5278 -- In both cases, we check whether the alignment must be
5279 -- reset in the wake of the size change.
5281 if Is_Elementary_Type (U_Ent) then
5282 if Size <= System_Storage_Unit then
5283 Init_Esize (U_Ent, System_Storage_Unit);
5284 elsif Size <= 16 then
5285 Init_Esize (U_Ent, 16);
5286 elsif Size <= 32 then
5287 Init_Esize (U_Ent, 32);
5288 else
5289 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5290 end if;
5292 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5293 else
5294 Alignment_Check_For_Size_Change (U_Ent, Size);
5295 end if;
5297 -- For objects, set Esize only
5299 else
5300 if Is_Elementary_Type (Etyp) then
5301 if Size /= System_Storage_Unit
5302 and then
5303 Size /= System_Storage_Unit * 2
5304 and then
5305 Size /= System_Storage_Unit * 4
5306 and then
5307 Size /= System_Storage_Unit * 8
5308 then
5309 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5310 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5311 Error_Msg_N
5312 ("size for primitive object must be a power of 2"
5313 & " in the range ^-^", N);
5314 end if;
5315 end if;
5317 Set_Esize (U_Ent, Size);
5318 end if;
5320 Set_Has_Size_Clause (U_Ent);
5321 end if;
5322 end Size;
5324 -----------
5325 -- Small --
5326 -----------
5328 -- Small attribute definition clause
5330 when Attribute_Small => Small : declare
5331 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5332 Small : Ureal;
5334 begin
5335 Analyze_And_Resolve (Expr, Any_Real);
5337 if Etype (Expr) = Any_Type then
5338 return;
5340 elsif not Is_OK_Static_Expression (Expr) then
5341 Flag_Non_Static_Expr
5342 ("small requires static expression!", Expr);
5343 return;
5345 else
5346 Small := Expr_Value_R (Expr);
5348 if Small <= Ureal_0 then
5349 Error_Msg_N ("small value must be greater than zero", Expr);
5350 return;
5351 end if;
5353 end if;
5355 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5356 Error_Msg_N
5357 ("small requires an ordinary fixed point type", Nam);
5359 elsif Has_Small_Clause (U_Ent) then
5360 Error_Msg_N ("small already given for &", Nam);
5362 elsif Small > Delta_Value (U_Ent) then
5363 Error_Msg_N
5364 ("small value must not be greater than delta value", Nam);
5366 else
5367 Set_Small_Value (U_Ent, Small);
5368 Set_Small_Value (Implicit_Base, Small);
5369 Set_Has_Small_Clause (U_Ent);
5370 Set_Has_Small_Clause (Implicit_Base);
5371 Set_Has_Non_Standard_Rep (Implicit_Base);
5372 end if;
5373 end Small;
5375 ------------------
5376 -- Storage_Pool --
5377 ------------------
5379 -- Storage_Pool attribute definition clause
5381 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5382 Pool : Entity_Id;
5383 T : Entity_Id;
5385 begin
5386 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5387 Error_Msg_N
5388 ("storage pool cannot be given for access-to-subprogram type",
5389 Nam);
5390 return;
5392 elsif not
5393 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5394 then
5395 Error_Msg_N
5396 ("storage pool can only be given for access types", Nam);
5397 return;
5399 elsif Is_Derived_Type (U_Ent) then
5400 Error_Msg_N
5401 ("storage pool cannot be given for a derived access type",
5402 Nam);
5404 elsif Duplicate_Clause then
5405 return;
5407 elsif Present (Associated_Storage_Pool (U_Ent)) then
5408 Error_Msg_N ("storage pool already given for &", Nam);
5409 return;
5410 end if;
5412 -- Check for Storage_Size previously given
5414 declare
5415 SS : constant Node_Id :=
5416 Get_Attribute_Definition_Clause
5417 (U_Ent, Attribute_Storage_Size);
5418 begin
5419 if Present (SS) then
5420 Check_Pool_Size_Clash (U_Ent, N, SS);
5421 end if;
5422 end;
5424 -- Storage_Pool case
5426 if Id = Attribute_Storage_Pool then
5427 Analyze_And_Resolve
5428 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5430 -- In the Simple_Storage_Pool case, we allow a variable of any
5431 -- simple storage pool type, so we Resolve without imposing an
5432 -- expected type.
5434 else
5435 Analyze_And_Resolve (Expr);
5437 if not Present (Get_Rep_Pragma
5438 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5439 then
5440 Error_Msg_N
5441 ("expression must be of a simple storage pool type", Expr);
5442 end if;
5443 end if;
5445 if not Denotes_Variable (Expr) then
5446 Error_Msg_N ("storage pool must be a variable", Expr);
5447 return;
5448 end if;
5450 if Nkind (Expr) = N_Type_Conversion then
5451 T := Etype (Expression (Expr));
5452 else
5453 T := Etype (Expr);
5454 end if;
5456 -- The Stack_Bounded_Pool is used internally for implementing
5457 -- access types with a Storage_Size. Since it only work properly
5458 -- when used on one specific type, we need to check that it is not
5459 -- hijacked improperly:
5461 -- type T is access Integer;
5462 -- for T'Storage_Size use n;
5463 -- type Q is access Float;
5464 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5466 if RTE_Available (RE_Stack_Bounded_Pool)
5467 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5468 then
5469 Error_Msg_N ("non-shareable internal Pool", Expr);
5470 return;
5471 end if;
5473 -- If the argument is a name that is not an entity name, then
5474 -- we construct a renaming operation to define an entity of
5475 -- type storage pool.
5477 if not Is_Entity_Name (Expr)
5478 and then Is_Object_Reference (Expr)
5479 then
5480 Pool := Make_Temporary (Loc, 'P', Expr);
5482 declare
5483 Rnode : constant Node_Id :=
5484 Make_Object_Renaming_Declaration (Loc,
5485 Defining_Identifier => Pool,
5486 Subtype_Mark =>
5487 New_Occurrence_Of (Etype (Expr), Loc),
5488 Name => Expr);
5490 begin
5491 -- If the attribute definition clause comes from an aspect
5492 -- clause, then insert the renaming before the associated
5493 -- entity's declaration, since the attribute clause has
5494 -- not yet been appended to the declaration list.
5496 if From_Aspect_Specification (N) then
5497 Insert_Before (Parent (Entity (N)), Rnode);
5498 else
5499 Insert_Before (N, Rnode);
5500 end if;
5502 Analyze (Rnode);
5503 Set_Associated_Storage_Pool (U_Ent, Pool);
5504 end;
5506 elsif Is_Entity_Name (Expr) then
5507 Pool := Entity (Expr);
5509 -- If pool is a renamed object, get original one. This can
5510 -- happen with an explicit renaming, and within instances.
5512 while Present (Renamed_Object (Pool))
5513 and then Is_Entity_Name (Renamed_Object (Pool))
5514 loop
5515 Pool := Entity (Renamed_Object (Pool));
5516 end loop;
5518 if Present (Renamed_Object (Pool))
5519 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5520 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5521 then
5522 Pool := Entity (Expression (Renamed_Object (Pool)));
5523 end if;
5525 Set_Associated_Storage_Pool (U_Ent, Pool);
5527 elsif Nkind (Expr) = N_Type_Conversion
5528 and then Is_Entity_Name (Expression (Expr))
5529 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5530 then
5531 Pool := Entity (Expression (Expr));
5532 Set_Associated_Storage_Pool (U_Ent, Pool);
5534 else
5535 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5536 return;
5537 end if;
5538 end;
5540 ------------------
5541 -- Storage_Size --
5542 ------------------
5544 -- Storage_Size attribute definition clause
5546 when Attribute_Storage_Size => Storage_Size : declare
5547 Btype : constant Entity_Id := Base_Type (U_Ent);
5549 begin
5550 if Is_Task_Type (U_Ent) then
5552 -- Check obsolescent (but never obsolescent if from aspect)
5554 if not From_Aspect_Specification (N) then
5555 Check_Restriction (No_Obsolescent_Features, N);
5557 if Warn_On_Obsolescent_Feature then
5558 Error_Msg_N
5559 ("?j?storage size clause for task is an " &
5560 "obsolescent feature (RM J.9)", N);
5561 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5562 end if;
5563 end if;
5565 FOnly := True;
5566 end if;
5568 if not Is_Access_Type (U_Ent)
5569 and then Ekind (U_Ent) /= E_Task_Type
5570 then
5571 Error_Msg_N ("storage size cannot be given for &", Nam);
5573 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5574 Error_Msg_N
5575 ("storage size cannot be given for a derived access type",
5576 Nam);
5578 elsif Duplicate_Clause then
5579 null;
5581 else
5582 Analyze_And_Resolve (Expr, Any_Integer);
5584 if Is_Access_Type (U_Ent) then
5586 -- Check for Storage_Pool previously given
5588 declare
5589 SP : constant Node_Id :=
5590 Get_Attribute_Definition_Clause
5591 (U_Ent, Attribute_Storage_Pool);
5593 begin
5594 if Present (SP) then
5595 Check_Pool_Size_Clash (U_Ent, SP, N);
5596 end if;
5597 end;
5599 -- Special case of for x'Storage_Size use 0
5601 if Is_OK_Static_Expression (Expr)
5602 and then Expr_Value (Expr) = 0
5603 then
5604 Set_No_Pool_Assigned (Btype);
5605 end if;
5606 end if;
5608 Set_Has_Storage_Size_Clause (Btype);
5609 end if;
5610 end Storage_Size;
5612 -----------------
5613 -- Stream_Size --
5614 -----------------
5616 when Attribute_Stream_Size => Stream_Size : declare
5617 Size : constant Uint := Static_Integer (Expr);
5619 begin
5620 if Ada_Version <= Ada_95 then
5621 Check_Restriction (No_Implementation_Attributes, N);
5622 end if;
5624 if Duplicate_Clause then
5625 null;
5627 elsif Is_Elementary_Type (U_Ent) then
5628 if Size /= System_Storage_Unit
5629 and then
5630 Size /= System_Storage_Unit * 2
5631 and then
5632 Size /= System_Storage_Unit * 4
5633 and then
5634 Size /= System_Storage_Unit * 8
5635 then
5636 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5637 Error_Msg_N
5638 ("stream size for elementary type must be a"
5639 & " power of 2 and at least ^", N);
5641 elsif RM_Size (U_Ent) > Size then
5642 Error_Msg_Uint_1 := RM_Size (U_Ent);
5643 Error_Msg_N
5644 ("stream size for elementary type must be a"
5645 & " power of 2 and at least ^", N);
5646 end if;
5648 Set_Has_Stream_Size_Clause (U_Ent);
5650 else
5651 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5652 end if;
5653 end Stream_Size;
5655 ----------------
5656 -- Value_Size --
5657 ----------------
5659 -- Value_Size attribute definition clause
5661 when Attribute_Value_Size => Value_Size : declare
5662 Size : constant Uint := Static_Integer (Expr);
5663 Biased : Boolean;
5665 begin
5666 if not Is_Type (U_Ent) then
5667 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5669 elsif Duplicate_Clause then
5670 null;
5672 elsif Is_Array_Type (U_Ent)
5673 and then not Is_Constrained (U_Ent)
5674 then
5675 Error_Msg_N
5676 ("Value_Size cannot be given for unconstrained array", Nam);
5678 else
5679 if Is_Elementary_Type (U_Ent) then
5680 Check_Size (Expr, U_Ent, Size, Biased);
5681 Set_Biased (U_Ent, N, "value size clause", Biased);
5682 end if;
5684 Set_RM_Size (U_Ent, Size);
5685 end if;
5686 end Value_Size;
5688 -----------------------
5689 -- Variable_Indexing --
5690 -----------------------
5692 when Attribute_Variable_Indexing =>
5693 Check_Indexing_Functions;
5695 -----------
5696 -- Write --
5697 -----------
5699 when Attribute_Write =>
5700 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5701 Set_Has_Specified_Stream_Write (Ent);
5703 -- All other attributes cannot be set
5705 when others =>
5706 Error_Msg_N
5707 ("attribute& cannot be set with definition clause", N);
5708 end case;
5710 -- The test for the type being frozen must be performed after any
5711 -- expression the clause has been analyzed since the expression itself
5712 -- might cause freezing that makes the clause illegal.
5714 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5715 return;
5716 end if;
5717 end Analyze_Attribute_Definition_Clause;
5719 ----------------------------
5720 -- Analyze_Code_Statement --
5721 ----------------------------
5723 procedure Analyze_Code_Statement (N : Node_Id) is
5724 HSS : constant Node_Id := Parent (N);
5725 SBody : constant Node_Id := Parent (HSS);
5726 Subp : constant Entity_Id := Current_Scope;
5727 Stmt : Node_Id;
5728 Decl : Node_Id;
5729 StmtO : Node_Id;
5730 DeclO : Node_Id;
5732 begin
5733 -- Analyze and check we get right type, note that this implements the
5734 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5735 -- is the only way that Asm_Insn could possibly be visible.
5737 Analyze_And_Resolve (Expression (N));
5739 if Etype (Expression (N)) = Any_Type then
5740 return;
5741 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5742 Error_Msg_N ("incorrect type for code statement", N);
5743 return;
5744 end if;
5746 Check_Code_Statement (N);
5748 -- Make sure we appear in the handled statement sequence of a
5749 -- subprogram (RM 13.8(3)).
5751 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5752 or else Nkind (SBody) /= N_Subprogram_Body
5753 then
5754 Error_Msg_N
5755 ("code statement can only appear in body of subprogram", N);
5756 return;
5757 end if;
5759 -- Do remaining checks (RM 13.8(3)) if not already done
5761 if not Is_Machine_Code_Subprogram (Subp) then
5762 Set_Is_Machine_Code_Subprogram (Subp);
5764 -- No exception handlers allowed
5766 if Present (Exception_Handlers (HSS)) then
5767 Error_Msg_N
5768 ("exception handlers not permitted in machine code subprogram",
5769 First (Exception_Handlers (HSS)));
5770 end if;
5772 -- No declarations other than use clauses and pragmas (we allow
5773 -- certain internally generated declarations as well).
5775 Decl := First (Declarations (SBody));
5776 while Present (Decl) loop
5777 DeclO := Original_Node (Decl);
5778 if Comes_From_Source (DeclO)
5779 and not Nkind_In (DeclO, N_Pragma,
5780 N_Use_Package_Clause,
5781 N_Use_Type_Clause,
5782 N_Implicit_Label_Declaration)
5783 then
5784 Error_Msg_N
5785 ("this declaration not allowed in machine code subprogram",
5786 DeclO);
5787 end if;
5789 Next (Decl);
5790 end loop;
5792 -- No statements other than code statements, pragmas, and labels.
5793 -- Again we allow certain internally generated statements.
5795 -- In Ada 2012, qualified expressions are names, and the code
5796 -- statement is initially parsed as a procedure call.
5798 Stmt := First (Statements (HSS));
5799 while Present (Stmt) loop
5800 StmtO := Original_Node (Stmt);
5802 -- A procedure call transformed into a code statement is OK.
5804 if Ada_Version >= Ada_2012
5805 and then Nkind (StmtO) = N_Procedure_Call_Statement
5806 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5807 then
5808 null;
5810 elsif Comes_From_Source (StmtO)
5811 and then not Nkind_In (StmtO, N_Pragma,
5812 N_Label,
5813 N_Code_Statement)
5814 then
5815 Error_Msg_N
5816 ("this statement is not allowed in machine code subprogram",
5817 StmtO);
5818 end if;
5820 Next (Stmt);
5821 end loop;
5822 end if;
5823 end Analyze_Code_Statement;
5825 -----------------------------------------------
5826 -- Analyze_Enumeration_Representation_Clause --
5827 -----------------------------------------------
5829 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5830 Ident : constant Node_Id := Identifier (N);
5831 Aggr : constant Node_Id := Array_Aggregate (N);
5832 Enumtype : Entity_Id;
5833 Elit : Entity_Id;
5834 Expr : Node_Id;
5835 Assoc : Node_Id;
5836 Choice : Node_Id;
5837 Val : Uint;
5839 Err : Boolean := False;
5840 -- Set True to avoid cascade errors and crashes on incorrect source code
5842 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5843 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5844 -- Allowed range of universal integer (= allowed range of enum lit vals)
5846 Min : Uint;
5847 Max : Uint;
5848 -- Minimum and maximum values of entries
5850 Max_Node : Node_Id;
5851 -- Pointer to node for literal providing max value
5853 begin
5854 if Ignore_Rep_Clauses then
5855 Kill_Rep_Clause (N);
5856 return;
5857 end if;
5859 -- Ignore enumeration rep clauses by default in CodePeer mode,
5860 -- unless -gnatd.I is specified, as a work around for potential false
5861 -- positive messages.
5863 if CodePeer_Mode and not Debug_Flag_Dot_II then
5864 return;
5865 end if;
5867 -- First some basic error checks
5869 Find_Type (Ident);
5870 Enumtype := Entity (Ident);
5872 if Enumtype = Any_Type
5873 or else Rep_Item_Too_Early (Enumtype, N)
5874 then
5875 return;
5876 else
5877 Enumtype := Underlying_Type (Enumtype);
5878 end if;
5880 if not Is_Enumeration_Type (Enumtype) then
5881 Error_Msg_NE
5882 ("enumeration type required, found}",
5883 Ident, First_Subtype (Enumtype));
5884 return;
5885 end if;
5887 -- Ignore rep clause on generic actual type. This will already have
5888 -- been flagged on the template as an error, and this is the safest
5889 -- way to ensure we don't get a junk cascaded message in the instance.
5891 if Is_Generic_Actual_Type (Enumtype) then
5892 return;
5894 -- Type must be in current scope
5896 elsif Scope (Enumtype) /= Current_Scope then
5897 Error_Msg_N ("type must be declared in this scope", Ident);
5898 return;
5900 -- Type must be a first subtype
5902 elsif not Is_First_Subtype (Enumtype) then
5903 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5904 return;
5906 -- Ignore duplicate rep clause
5908 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5909 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5910 return;
5912 -- Don't allow rep clause for standard [wide_[wide_]]character
5914 elsif Is_Standard_Character_Type (Enumtype) then
5915 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5916 return;
5918 -- Check that the expression is a proper aggregate (no parentheses)
5920 elsif Paren_Count (Aggr) /= 0 then
5921 Error_Msg
5922 ("extra parentheses surrounding aggregate not allowed",
5923 First_Sloc (Aggr));
5924 return;
5926 -- All tests passed, so set rep clause in place
5928 else
5929 Set_Has_Enumeration_Rep_Clause (Enumtype);
5930 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5931 end if;
5933 -- Now we process the aggregate. Note that we don't use the normal
5934 -- aggregate code for this purpose, because we don't want any of the
5935 -- normal expansion activities, and a number of special semantic
5936 -- rules apply (including the component type being any integer type)
5938 Elit := First_Literal (Enumtype);
5940 -- First the positional entries if any
5942 if Present (Expressions (Aggr)) then
5943 Expr := First (Expressions (Aggr));
5944 while Present (Expr) loop
5945 if No (Elit) then
5946 Error_Msg_N ("too many entries in aggregate", Expr);
5947 return;
5948 end if;
5950 Val := Static_Integer (Expr);
5952 -- Err signals that we found some incorrect entries processing
5953 -- the list. The final checks for completeness and ordering are
5954 -- skipped in this case.
5956 if Val = No_Uint then
5957 Err := True;
5959 elsif Val < Lo or else Hi < Val then
5960 Error_Msg_N ("value outside permitted range", Expr);
5961 Err := True;
5962 end if;
5964 Set_Enumeration_Rep (Elit, Val);
5965 Set_Enumeration_Rep_Expr (Elit, Expr);
5966 Next (Expr);
5967 Next (Elit);
5968 end loop;
5969 end if;
5971 -- Now process the named entries if present
5973 if Present (Component_Associations (Aggr)) then
5974 Assoc := First (Component_Associations (Aggr));
5975 while Present (Assoc) loop
5976 Choice := First (Choices (Assoc));
5978 if Present (Next (Choice)) then
5979 Error_Msg_N
5980 ("multiple choice not allowed here", Next (Choice));
5981 Err := True;
5982 end if;
5984 if Nkind (Choice) = N_Others_Choice then
5985 Error_Msg_N ("others choice not allowed here", Choice);
5986 Err := True;
5988 elsif Nkind (Choice) = N_Range then
5990 -- ??? should allow zero/one element range here
5992 Error_Msg_N ("range not allowed here", Choice);
5993 Err := True;
5995 else
5996 Analyze_And_Resolve (Choice, Enumtype);
5998 if Error_Posted (Choice) then
5999 Err := True;
6000 end if;
6002 if not Err then
6003 if Is_Entity_Name (Choice)
6004 and then Is_Type (Entity (Choice))
6005 then
6006 Error_Msg_N ("subtype name not allowed here", Choice);
6007 Err := True;
6009 -- ??? should allow static subtype with zero/one entry
6011 elsif Etype (Choice) = Base_Type (Enumtype) then
6012 if not Is_OK_Static_Expression (Choice) then
6013 Flag_Non_Static_Expr
6014 ("non-static expression used for choice!", Choice);
6015 Err := True;
6017 else
6018 Elit := Expr_Value_E (Choice);
6020 if Present (Enumeration_Rep_Expr (Elit)) then
6021 Error_Msg_Sloc :=
6022 Sloc (Enumeration_Rep_Expr (Elit));
6023 Error_Msg_NE
6024 ("representation for& previously given#",
6025 Choice, Elit);
6026 Err := True;
6027 end if;
6029 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6031 Expr := Expression (Assoc);
6032 Val := Static_Integer (Expr);
6034 if Val = No_Uint then
6035 Err := True;
6037 elsif Val < Lo or else Hi < Val then
6038 Error_Msg_N ("value outside permitted range", Expr);
6039 Err := True;
6040 end if;
6042 Set_Enumeration_Rep (Elit, Val);
6043 end if;
6044 end if;
6045 end if;
6046 end if;
6048 Next (Assoc);
6049 end loop;
6050 end if;
6052 -- Aggregate is fully processed. Now we check that a full set of
6053 -- representations was given, and that they are in range and in order.
6054 -- These checks are only done if no other errors occurred.
6056 if not Err then
6057 Min := No_Uint;
6058 Max := No_Uint;
6060 Elit := First_Literal (Enumtype);
6061 while Present (Elit) loop
6062 if No (Enumeration_Rep_Expr (Elit)) then
6063 Error_Msg_NE ("missing representation for&!", N, Elit);
6065 else
6066 Val := Enumeration_Rep (Elit);
6068 if Min = No_Uint then
6069 Min := Val;
6070 end if;
6072 if Val /= No_Uint then
6073 if Max /= No_Uint and then Val <= Max then
6074 Error_Msg_NE
6075 ("enumeration value for& not ordered!",
6076 Enumeration_Rep_Expr (Elit), Elit);
6077 end if;
6079 Max_Node := Enumeration_Rep_Expr (Elit);
6080 Max := Val;
6081 end if;
6083 -- If there is at least one literal whose representation is not
6084 -- equal to the Pos value, then note that this enumeration type
6085 -- has a non-standard representation.
6087 if Val /= Enumeration_Pos (Elit) then
6088 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6089 end if;
6090 end if;
6092 Next (Elit);
6093 end loop;
6095 -- Now set proper size information
6097 declare
6098 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6100 begin
6101 if Has_Size_Clause (Enumtype) then
6103 -- All OK, if size is OK now
6105 if RM_Size (Enumtype) >= Minsize then
6106 null;
6108 else
6109 -- Try if we can get by with biasing
6111 Minsize :=
6112 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6114 -- Error message if even biasing does not work
6116 if RM_Size (Enumtype) < Minsize then
6117 Error_Msg_Uint_1 := RM_Size (Enumtype);
6118 Error_Msg_Uint_2 := Max;
6119 Error_Msg_N
6120 ("previously given size (^) is too small "
6121 & "for this value (^)", Max_Node);
6123 -- If biasing worked, indicate that we now have biased rep
6125 else
6126 Set_Biased
6127 (Enumtype, Size_Clause (Enumtype), "size clause");
6128 end if;
6129 end if;
6131 else
6132 Set_RM_Size (Enumtype, Minsize);
6133 Set_Enum_Esize (Enumtype);
6134 end if;
6136 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6137 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6138 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6139 end;
6140 end if;
6142 -- We repeat the too late test in case it froze itself
6144 if Rep_Item_Too_Late (Enumtype, N) then
6145 null;
6146 end if;
6147 end Analyze_Enumeration_Representation_Clause;
6149 ----------------------------
6150 -- Analyze_Free_Statement --
6151 ----------------------------
6153 procedure Analyze_Free_Statement (N : Node_Id) is
6154 begin
6155 Analyze (Expression (N));
6156 end Analyze_Free_Statement;
6158 ---------------------------
6159 -- Analyze_Freeze_Entity --
6160 ---------------------------
6162 procedure Analyze_Freeze_Entity (N : Node_Id) is
6163 begin
6164 Freeze_Entity_Checks (N);
6165 end Analyze_Freeze_Entity;
6167 -----------------------------------
6168 -- Analyze_Freeze_Generic_Entity --
6169 -----------------------------------
6171 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6172 begin
6173 Freeze_Entity_Checks (N);
6174 end Analyze_Freeze_Generic_Entity;
6176 ------------------------------------------
6177 -- Analyze_Record_Representation_Clause --
6178 ------------------------------------------
6180 -- Note: we check as much as we can here, but we can't do any checks
6181 -- based on the position values (e.g. overlap checks) until freeze time
6182 -- because especially in Ada 2005 (machine scalar mode), the processing
6183 -- for non-standard bit order can substantially change the positions.
6184 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6185 -- for the remainder of this processing.
6187 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6188 Ident : constant Node_Id := Identifier (N);
6189 Biased : Boolean;
6190 CC : Node_Id;
6191 Comp : Entity_Id;
6192 Fbit : Uint;
6193 Hbit : Uint := Uint_0;
6194 Lbit : Uint;
6195 Ocomp : Entity_Id;
6196 Posit : Uint;
6197 Rectype : Entity_Id;
6198 Recdef : Node_Id;
6200 function Is_Inherited (Comp : Entity_Id) return Boolean;
6201 -- True if Comp is an inherited component in a record extension
6203 ------------------
6204 -- Is_Inherited --
6205 ------------------
6207 function Is_Inherited (Comp : Entity_Id) return Boolean is
6208 Comp_Base : Entity_Id;
6210 begin
6211 if Ekind (Rectype) = E_Record_Subtype then
6212 Comp_Base := Original_Record_Component (Comp);
6213 else
6214 Comp_Base := Comp;
6215 end if;
6217 return Comp_Base /= Original_Record_Component (Comp_Base);
6218 end Is_Inherited;
6220 -- Local variables
6222 Is_Record_Extension : Boolean;
6223 -- True if Rectype is a record extension
6225 CR_Pragma : Node_Id := Empty;
6226 -- Points to N_Pragma node if Complete_Representation pragma present
6228 -- Start of processing for Analyze_Record_Representation_Clause
6230 begin
6231 if Ignore_Rep_Clauses then
6232 Kill_Rep_Clause (N);
6233 return;
6234 end if;
6236 Find_Type (Ident);
6237 Rectype := Entity (Ident);
6239 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6240 return;
6241 else
6242 Rectype := Underlying_Type (Rectype);
6243 end if;
6245 -- First some basic error checks
6247 if not Is_Record_Type (Rectype) then
6248 Error_Msg_NE
6249 ("record type required, found}", Ident, First_Subtype (Rectype));
6250 return;
6252 elsif Scope (Rectype) /= Current_Scope then
6253 Error_Msg_N ("type must be declared in this scope", N);
6254 return;
6256 elsif not Is_First_Subtype (Rectype) then
6257 Error_Msg_N ("cannot give record rep clause for subtype", N);
6258 return;
6260 elsif Has_Record_Rep_Clause (Rectype) then
6261 Error_Msg_N ("duplicate record rep clause ignored", N);
6262 return;
6264 elsif Rep_Item_Too_Late (Rectype, N) then
6265 return;
6266 end if;
6268 -- We know we have a first subtype, now possibly go the the anonymous
6269 -- base type to determine whether Rectype is a record extension.
6271 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6272 Is_Record_Extension :=
6273 Nkind (Recdef) = N_Derived_Type_Definition
6274 and then Present (Record_Extension_Part (Recdef));
6276 if Present (Mod_Clause (N)) then
6277 declare
6278 Loc : constant Source_Ptr := Sloc (N);
6279 M : constant Node_Id := Mod_Clause (N);
6280 P : constant List_Id := Pragmas_Before (M);
6281 AtM_Nod : Node_Id;
6283 Mod_Val : Uint;
6284 pragma Warnings (Off, Mod_Val);
6286 begin
6287 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6289 if Warn_On_Obsolescent_Feature then
6290 Error_Msg_N
6291 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6292 Error_Msg_N
6293 ("\?j?use alignment attribute definition clause instead", N);
6294 end if;
6296 if Present (P) then
6297 Analyze_List (P);
6298 end if;
6300 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6301 -- the Mod clause into an alignment clause anyway, so that the
6302 -- back-end can compute and back-annotate properly the size and
6303 -- alignment of types that may include this record.
6305 -- This seems dubious, this destroys the source tree in a manner
6306 -- not detectable by ASIS ???
6308 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6309 AtM_Nod :=
6310 Make_Attribute_Definition_Clause (Loc,
6311 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6312 Chars => Name_Alignment,
6313 Expression => Relocate_Node (Expression (M)));
6315 Set_From_At_Mod (AtM_Nod);
6316 Insert_After (N, AtM_Nod);
6317 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6318 Set_Mod_Clause (N, Empty);
6320 else
6321 -- Get the alignment value to perform error checking
6323 Mod_Val := Get_Alignment_Value (Expression (M));
6324 end if;
6325 end;
6326 end if;
6328 -- For untagged types, clear any existing component clauses for the
6329 -- type. If the type is derived, this is what allows us to override
6330 -- a rep clause for the parent. For type extensions, the representation
6331 -- of the inherited components is inherited, so we want to keep previous
6332 -- component clauses for completeness.
6334 if not Is_Tagged_Type (Rectype) then
6335 Comp := First_Component_Or_Discriminant (Rectype);
6336 while Present (Comp) loop
6337 Set_Component_Clause (Comp, Empty);
6338 Next_Component_Or_Discriminant (Comp);
6339 end loop;
6340 end if;
6342 -- All done if no component clauses
6344 CC := First (Component_Clauses (N));
6346 if No (CC) then
6347 return;
6348 end if;
6350 -- A representation like this applies to the base type
6352 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6353 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6354 Set_Has_Specified_Layout (Base_Type (Rectype));
6356 -- Process the component clauses
6358 while Present (CC) loop
6360 -- Pragma
6362 if Nkind (CC) = N_Pragma then
6363 Analyze (CC);
6365 -- The only pragma of interest is Complete_Representation
6367 if Pragma_Name (CC) = Name_Complete_Representation then
6368 CR_Pragma := CC;
6369 end if;
6371 -- Processing for real component clause
6373 else
6374 Posit := Static_Integer (Position (CC));
6375 Fbit := Static_Integer (First_Bit (CC));
6376 Lbit := Static_Integer (Last_Bit (CC));
6378 if Posit /= No_Uint
6379 and then Fbit /= No_Uint
6380 and then Lbit /= No_Uint
6381 then
6382 if Posit < 0 then
6383 Error_Msg_N
6384 ("position cannot be negative", Position (CC));
6386 elsif Fbit < 0 then
6387 Error_Msg_N
6388 ("first bit cannot be negative", First_Bit (CC));
6390 -- The Last_Bit specified in a component clause must not be
6391 -- less than the First_Bit minus one (RM-13.5.1(10)).
6393 elsif Lbit < Fbit - 1 then
6394 Error_Msg_N
6395 ("last bit cannot be less than first bit minus one",
6396 Last_Bit (CC));
6398 -- Values look OK, so find the corresponding record component
6399 -- Even though the syntax allows an attribute reference for
6400 -- implementation-defined components, GNAT does not allow the
6401 -- tag to get an explicit position.
6403 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6404 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6405 Error_Msg_N ("position of tag cannot be specified", CC);
6406 else
6407 Error_Msg_N ("illegal component name", CC);
6408 end if;
6410 else
6411 Comp := First_Entity (Rectype);
6412 while Present (Comp) loop
6413 exit when Chars (Comp) = Chars (Component_Name (CC));
6414 Next_Entity (Comp);
6415 end loop;
6417 if No (Comp) then
6419 -- Maybe component of base type that is absent from
6420 -- statically constrained first subtype.
6422 Comp := First_Entity (Base_Type (Rectype));
6423 while Present (Comp) loop
6424 exit when Chars (Comp) = Chars (Component_Name (CC));
6425 Next_Entity (Comp);
6426 end loop;
6427 end if;
6429 if No (Comp) then
6430 Error_Msg_N
6431 ("component clause is for non-existent field", CC);
6433 -- Ada 2012 (AI05-0026): Any name that denotes a
6434 -- discriminant of an object of an unchecked union type
6435 -- shall not occur within a record_representation_clause.
6437 -- The general restriction of using record rep clauses on
6438 -- Unchecked_Union types has now been lifted. Since it is
6439 -- possible to introduce a record rep clause which mentions
6440 -- the discriminant of an Unchecked_Union in non-Ada 2012
6441 -- code, this check is applied to all versions of the
6442 -- language.
6444 elsif Ekind (Comp) = E_Discriminant
6445 and then Is_Unchecked_Union (Rectype)
6446 then
6447 Error_Msg_N
6448 ("cannot reference discriminant of unchecked union",
6449 Component_Name (CC));
6451 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6452 Error_Msg_NE
6453 ("component clause not allowed for inherited "
6454 & "component&", CC, Comp);
6456 elsif Present (Component_Clause (Comp)) then
6458 -- Diagnose duplicate rep clause, or check consistency
6459 -- if this is an inherited component. In a double fault,
6460 -- there may be a duplicate inconsistent clause for an
6461 -- inherited component.
6463 if Scope (Original_Record_Component (Comp)) = Rectype
6464 or else Parent (Component_Clause (Comp)) = N
6465 then
6466 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6467 Error_Msg_N ("component clause previously given#", CC);
6469 else
6470 declare
6471 Rep1 : constant Node_Id := Component_Clause (Comp);
6472 begin
6473 if Intval (Position (Rep1)) /=
6474 Intval (Position (CC))
6475 or else Intval (First_Bit (Rep1)) /=
6476 Intval (First_Bit (CC))
6477 or else Intval (Last_Bit (Rep1)) /=
6478 Intval (Last_Bit (CC))
6479 then
6480 Error_Msg_N
6481 ("component clause inconsistent "
6482 & "with representation of ancestor", CC);
6484 elsif Warn_On_Redundant_Constructs then
6485 Error_Msg_N
6486 ("?r?redundant confirming component clause "
6487 & "for component!", CC);
6488 end if;
6489 end;
6490 end if;
6492 -- Normal case where this is the first component clause we
6493 -- have seen for this entity, so set it up properly.
6495 else
6496 -- Make reference for field in record rep clause and set
6497 -- appropriate entity field in the field identifier.
6499 Generate_Reference
6500 (Comp, Component_Name (CC), Set_Ref => False);
6501 Set_Entity (Component_Name (CC), Comp);
6503 -- Update Fbit and Lbit to the actual bit number
6505 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6506 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6508 if Has_Size_Clause (Rectype)
6509 and then RM_Size (Rectype) <= Lbit
6510 then
6511 Error_Msg_N
6512 ("bit number out of range of specified size",
6513 Last_Bit (CC));
6514 else
6515 Set_Component_Clause (Comp, CC);
6516 Set_Component_Bit_Offset (Comp, Fbit);
6517 Set_Esize (Comp, 1 + (Lbit - Fbit));
6518 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6519 Set_Normalized_Position (Comp, Fbit / SSU);
6521 if Warn_On_Overridden_Size
6522 and then Has_Size_Clause (Etype (Comp))
6523 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6524 then
6525 Error_Msg_NE
6526 ("?S?component size overrides size clause for&",
6527 Component_Name (CC), Etype (Comp));
6528 end if;
6530 -- This information is also set in the corresponding
6531 -- component of the base type, found by accessing the
6532 -- Original_Record_Component link if it is present.
6534 Ocomp := Original_Record_Component (Comp);
6536 if Hbit < Lbit then
6537 Hbit := Lbit;
6538 end if;
6540 Check_Size
6541 (Component_Name (CC),
6542 Etype (Comp),
6543 Esize (Comp),
6544 Biased);
6546 Set_Biased
6547 (Comp, First_Node (CC), "component clause", Biased);
6549 if Present (Ocomp) then
6550 Set_Component_Clause (Ocomp, CC);
6551 Set_Component_Bit_Offset (Ocomp, Fbit);
6552 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6553 Set_Normalized_Position (Ocomp, Fbit / SSU);
6554 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6556 Set_Normalized_Position_Max
6557 (Ocomp, Normalized_Position (Ocomp));
6559 -- Note: we don't use Set_Biased here, because we
6560 -- already gave a warning above if needed, and we
6561 -- would get a duplicate for the same name here.
6563 Set_Has_Biased_Representation
6564 (Ocomp, Has_Biased_Representation (Comp));
6565 end if;
6567 if Esize (Comp) < 0 then
6568 Error_Msg_N ("component size is negative", CC);
6569 end if;
6570 end if;
6571 end if;
6572 end if;
6573 end if;
6574 end if;
6576 Next (CC);
6577 end loop;
6579 -- Check missing components if Complete_Representation pragma appeared
6581 if Present (CR_Pragma) then
6582 Comp := First_Component_Or_Discriminant (Rectype);
6583 while Present (Comp) loop
6584 if No (Component_Clause (Comp)) then
6585 Error_Msg_NE
6586 ("missing component clause for &", CR_Pragma, Comp);
6587 end if;
6589 Next_Component_Or_Discriminant (Comp);
6590 end loop;
6592 -- Give missing components warning if required
6594 elsif Warn_On_Unrepped_Components then
6595 declare
6596 Num_Repped_Components : Nat := 0;
6597 Num_Unrepped_Components : Nat := 0;
6599 begin
6600 -- First count number of repped and unrepped components
6602 Comp := First_Component_Or_Discriminant (Rectype);
6603 while Present (Comp) loop
6604 if Present (Component_Clause (Comp)) then
6605 Num_Repped_Components := Num_Repped_Components + 1;
6606 else
6607 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6608 end if;
6610 Next_Component_Or_Discriminant (Comp);
6611 end loop;
6613 -- We are only interested in the case where there is at least one
6614 -- unrepped component, and at least half the components have rep
6615 -- clauses. We figure that if less than half have them, then the
6616 -- partial rep clause is really intentional. If the component
6617 -- type has no underlying type set at this point (as for a generic
6618 -- formal type), we don't know enough to give a warning on the
6619 -- component.
6621 if Num_Unrepped_Components > 0
6622 and then Num_Unrepped_Components < Num_Repped_Components
6623 then
6624 Comp := First_Component_Or_Discriminant (Rectype);
6625 while Present (Comp) loop
6626 if No (Component_Clause (Comp))
6627 and then Comes_From_Source (Comp)
6628 and then Present (Underlying_Type (Etype (Comp)))
6629 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6630 or else Size_Known_At_Compile_Time
6631 (Underlying_Type (Etype (Comp))))
6632 and then not Has_Warnings_Off (Rectype)
6634 -- Ignore discriminant in unchecked union, since it is
6635 -- not there, and cannot have a component clause.
6637 and then (not Is_Unchecked_Union (Rectype)
6638 or else Ekind (Comp) /= E_Discriminant)
6639 then
6640 Error_Msg_Sloc := Sloc (Comp);
6641 Error_Msg_NE
6642 ("?C?no component clause given for & declared #",
6643 N, Comp);
6644 end if;
6646 Next_Component_Or_Discriminant (Comp);
6647 end loop;
6648 end if;
6649 end;
6650 end if;
6651 end Analyze_Record_Representation_Clause;
6653 -------------------------------------
6654 -- Build_Discrete_Static_Predicate --
6655 -------------------------------------
6657 procedure Build_Discrete_Static_Predicate
6658 (Typ : Entity_Id;
6659 Expr : Node_Id;
6660 Nam : Name_Id)
6662 Loc : constant Source_Ptr := Sloc (Expr);
6664 Non_Static : exception;
6665 -- Raised if something non-static is found
6667 Btyp : constant Entity_Id := Base_Type (Typ);
6669 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6670 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6671 -- Low bound and high bound value of base type of Typ
6673 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6674 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6675 -- Low bound and high bound values of static subtype Typ
6677 type REnt is record
6678 Lo, Hi : Uint;
6679 end record;
6680 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6681 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6682 -- value.
6684 type RList is array (Nat range <>) of REnt;
6685 -- A list of ranges. The ranges are sorted in increasing order, and are
6686 -- disjoint (there is a gap of at least one value between each range in
6687 -- the table). A value is in the set of ranges in Rlist if it lies
6688 -- within one of these ranges.
6690 False_Range : constant RList :=
6691 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6692 -- An empty set of ranges represents a range list that can never be
6693 -- satisfied, since there are no ranges in which the value could lie,
6694 -- so it does not lie in any of them. False_Range is a canonical value
6695 -- for this empty set, but general processing should test for an Rlist
6696 -- with length zero (see Is_False predicate), since other null ranges
6697 -- may appear which must be treated as False.
6699 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6700 -- Range representing True, value must be in the base range
6702 function "and" (Left : RList; Right : RList) return RList;
6703 -- And's together two range lists, returning a range list. This is a set
6704 -- intersection operation.
6706 function "or" (Left : RList; Right : RList) return RList;
6707 -- Or's together two range lists, returning a range list. This is a set
6708 -- union operation.
6710 function "not" (Right : RList) return RList;
6711 -- Returns complement of a given range list, i.e. a range list
6712 -- representing all the values in TLo .. THi that are not in the input
6713 -- operand Right.
6715 function Build_Val (V : Uint) return Node_Id;
6716 -- Return an analyzed N_Identifier node referencing this value, suitable
6717 -- for use as an entry in the Static_Discrte_Predicate list. This node
6718 -- is typed with the base type.
6720 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6721 -- Return an analyzed N_Range node referencing this range, suitable for
6722 -- use as an entry in the Static_Discrete_Predicate list. This node is
6723 -- typed with the base type.
6725 function Get_RList (Exp : Node_Id) return RList;
6726 -- This is a recursive routine that converts the given expression into a
6727 -- list of ranges, suitable for use in building the static predicate.
6729 function Is_False (R : RList) return Boolean;
6730 pragma Inline (Is_False);
6731 -- Returns True if the given range list is empty, and thus represents a
6732 -- False list of ranges that can never be satisfied.
6734 function Is_True (R : RList) return Boolean;
6735 -- Returns True if R trivially represents the True predicate by having a
6736 -- single range from BLo to BHi.
6738 function Is_Type_Ref (N : Node_Id) return Boolean;
6739 pragma Inline (Is_Type_Ref);
6740 -- Returns if True if N is a reference to the type for the predicate in
6741 -- the expression (i.e. if it is an identifier whose Chars field matches
6742 -- the Nam given in the call). N must not be parenthesized, if the type
6743 -- name appears in parens, this routine will return False.
6745 function Lo_Val (N : Node_Id) return Uint;
6746 -- Given an entry from a Static_Discrete_Predicate list that is either
6747 -- a static expression or static range, gets either the expression value
6748 -- or the low bound of the range.
6750 function Hi_Val (N : Node_Id) return Uint;
6751 -- Given an entry from a Static_Discrete_Predicate list that is either
6752 -- a static expression or static range, gets either the expression value
6753 -- or the high bound of the range.
6755 function Membership_Entry (N : Node_Id) return RList;
6756 -- Given a single membership entry (range, value, or subtype), returns
6757 -- the corresponding range list. Raises Static_Error if not static.
6759 function Membership_Entries (N : Node_Id) return RList;
6760 -- Given an element on an alternatives list of a membership operation,
6761 -- returns the range list corresponding to this entry and all following
6762 -- entries (i.e. returns the "or" of this list of values).
6764 function Stat_Pred (Typ : Entity_Id) return RList;
6765 -- Given a type, if it has a static predicate, then return the predicate
6766 -- as a range list, otherwise raise Non_Static.
6768 -----------
6769 -- "and" --
6770 -----------
6772 function "and" (Left : RList; Right : RList) return RList is
6773 FEnt : REnt;
6774 -- First range of result
6776 SLeft : Nat := Left'First;
6777 -- Start of rest of left entries
6779 SRight : Nat := Right'First;
6780 -- Start of rest of right entries
6782 begin
6783 -- If either range is True, return the other
6785 if Is_True (Left) then
6786 return Right;
6787 elsif Is_True (Right) then
6788 return Left;
6789 end if;
6791 -- If either range is False, return False
6793 if Is_False (Left) or else Is_False (Right) then
6794 return False_Range;
6795 end if;
6797 -- Loop to remove entries at start that are disjoint, and thus just
6798 -- get discarded from the result entirely.
6800 loop
6801 -- If no operands left in either operand, result is false
6803 if SLeft > Left'Last or else SRight > Right'Last then
6804 return False_Range;
6806 -- Discard first left operand entry if disjoint with right
6808 elsif Left (SLeft).Hi < Right (SRight).Lo then
6809 SLeft := SLeft + 1;
6811 -- Discard first right operand entry if disjoint with left
6813 elsif Right (SRight).Hi < Left (SLeft).Lo then
6814 SRight := SRight + 1;
6816 -- Otherwise we have an overlapping entry
6818 else
6819 exit;
6820 end if;
6821 end loop;
6823 -- Now we have two non-null operands, and first entries overlap. The
6824 -- first entry in the result will be the overlapping part of these
6825 -- two entries.
6827 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6828 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6830 -- Now we can remove the entry that ended at a lower value, since its
6831 -- contribution is entirely contained in Fent.
6833 if Left (SLeft).Hi <= Right (SRight).Hi then
6834 SLeft := SLeft + 1;
6835 else
6836 SRight := SRight + 1;
6837 end if;
6839 -- Compute result by concatenating this first entry with the "and" of
6840 -- the remaining parts of the left and right operands. Note that if
6841 -- either of these is empty, "and" will yield empty, so that we will
6842 -- end up with just Fent, which is what we want in that case.
6844 return
6845 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6846 end "and";
6848 -----------
6849 -- "not" --
6850 -----------
6852 function "not" (Right : RList) return RList is
6853 begin
6854 -- Return True if False range
6856 if Is_False (Right) then
6857 return True_Range;
6858 end if;
6860 -- Return False if True range
6862 if Is_True (Right) then
6863 return False_Range;
6864 end if;
6866 -- Here if not trivial case
6868 declare
6869 Result : RList (1 .. Right'Length + 1);
6870 -- May need one more entry for gap at beginning and end
6872 Count : Nat := 0;
6873 -- Number of entries stored in Result
6875 begin
6876 -- Gap at start
6878 if Right (Right'First).Lo > TLo then
6879 Count := Count + 1;
6880 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6881 end if;
6883 -- Gaps between ranges
6885 for J in Right'First .. Right'Last - 1 loop
6886 Count := Count + 1;
6887 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6888 end loop;
6890 -- Gap at end
6892 if Right (Right'Last).Hi < THi then
6893 Count := Count + 1;
6894 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6895 end if;
6897 return Result (1 .. Count);
6898 end;
6899 end "not";
6901 ----------
6902 -- "or" --
6903 ----------
6905 function "or" (Left : RList; Right : RList) return RList is
6906 FEnt : REnt;
6907 -- First range of result
6909 SLeft : Nat := Left'First;
6910 -- Start of rest of left entries
6912 SRight : Nat := Right'First;
6913 -- Start of rest of right entries
6915 begin
6916 -- If either range is True, return True
6918 if Is_True (Left) or else Is_True (Right) then
6919 return True_Range;
6920 end if;
6922 -- If either range is False (empty), return the other
6924 if Is_False (Left) then
6925 return Right;
6926 elsif Is_False (Right) then
6927 return Left;
6928 end if;
6930 -- Initialize result first entry from left or right operand depending
6931 -- on which starts with the lower range.
6933 if Left (SLeft).Lo < Right (SRight).Lo then
6934 FEnt := Left (SLeft);
6935 SLeft := SLeft + 1;
6936 else
6937 FEnt := Right (SRight);
6938 SRight := SRight + 1;
6939 end if;
6941 -- This loop eats ranges from left and right operands that are
6942 -- contiguous with the first range we are gathering.
6944 loop
6945 -- Eat first entry in left operand if contiguous or overlapped by
6946 -- gathered first operand of result.
6948 if SLeft <= Left'Last
6949 and then Left (SLeft).Lo <= FEnt.Hi + 1
6950 then
6951 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6952 SLeft := SLeft + 1;
6954 -- Eat first entry in right operand if contiguous or overlapped by
6955 -- gathered right operand of result.
6957 elsif SRight <= Right'Last
6958 and then Right (SRight).Lo <= FEnt.Hi + 1
6959 then
6960 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6961 SRight := SRight + 1;
6963 -- All done if no more entries to eat
6965 else
6966 exit;
6967 end if;
6968 end loop;
6970 -- Obtain result as the first entry we just computed, concatenated
6971 -- to the "or" of the remaining results (if one operand is empty,
6972 -- this will just concatenate with the other
6974 return
6975 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6976 end "or";
6978 -----------------
6979 -- Build_Range --
6980 -----------------
6982 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6983 Result : Node_Id;
6984 begin
6985 Result :=
6986 Make_Range (Loc,
6987 Low_Bound => Build_Val (Lo),
6988 High_Bound => Build_Val (Hi));
6989 Set_Etype (Result, Btyp);
6990 Set_Analyzed (Result);
6991 return Result;
6992 end Build_Range;
6994 ---------------
6995 -- Build_Val --
6996 ---------------
6998 function Build_Val (V : Uint) return Node_Id is
6999 Result : Node_Id;
7001 begin
7002 if Is_Enumeration_Type (Typ) then
7003 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7004 else
7005 Result := Make_Integer_Literal (Loc, V);
7006 end if;
7008 Set_Etype (Result, Btyp);
7009 Set_Is_Static_Expression (Result);
7010 Set_Analyzed (Result);
7011 return Result;
7012 end Build_Val;
7014 ---------------
7015 -- Get_RList --
7016 ---------------
7018 function Get_RList (Exp : Node_Id) return RList is
7019 Op : Node_Kind;
7020 Val : Uint;
7022 begin
7023 -- Static expression can only be true or false
7025 if Is_OK_Static_Expression (Exp) then
7026 if Expr_Value (Exp) = 0 then
7027 return False_Range;
7028 else
7029 return True_Range;
7030 end if;
7031 end if;
7033 -- Otherwise test node type
7035 Op := Nkind (Exp);
7037 case Op is
7039 -- And
7041 when N_Op_And | N_And_Then =>
7042 return Get_RList (Left_Opnd (Exp))
7044 Get_RList (Right_Opnd (Exp));
7046 -- Or
7048 when N_Op_Or | N_Or_Else =>
7049 return Get_RList (Left_Opnd (Exp))
7051 Get_RList (Right_Opnd (Exp));
7053 -- Not
7055 when N_Op_Not =>
7056 return not Get_RList (Right_Opnd (Exp));
7058 -- Comparisons of type with static value
7060 when N_Op_Compare =>
7062 -- Type is left operand
7064 if Is_Type_Ref (Left_Opnd (Exp))
7065 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7066 then
7067 Val := Expr_Value (Right_Opnd (Exp));
7069 -- Typ is right operand
7071 elsif Is_Type_Ref (Right_Opnd (Exp))
7072 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7073 then
7074 Val := Expr_Value (Left_Opnd (Exp));
7076 -- Invert sense of comparison
7078 case Op is
7079 when N_Op_Gt => Op := N_Op_Lt;
7080 when N_Op_Lt => Op := N_Op_Gt;
7081 when N_Op_Ge => Op := N_Op_Le;
7082 when N_Op_Le => Op := N_Op_Ge;
7083 when others => null;
7084 end case;
7086 -- Other cases are non-static
7088 else
7089 raise Non_Static;
7090 end if;
7092 -- Construct range according to comparison operation
7094 case Op is
7095 when N_Op_Eq =>
7096 return RList'(1 => REnt'(Val, Val));
7098 when N_Op_Ge =>
7099 return RList'(1 => REnt'(Val, BHi));
7101 when N_Op_Gt =>
7102 return RList'(1 => REnt'(Val + 1, BHi));
7104 when N_Op_Le =>
7105 return RList'(1 => REnt'(BLo, Val));
7107 when N_Op_Lt =>
7108 return RList'(1 => REnt'(BLo, Val - 1));
7110 when N_Op_Ne =>
7111 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7113 when others =>
7114 raise Program_Error;
7115 end case;
7117 -- Membership (IN)
7119 when N_In =>
7120 if not Is_Type_Ref (Left_Opnd (Exp)) then
7121 raise Non_Static;
7122 end if;
7124 if Present (Right_Opnd (Exp)) then
7125 return Membership_Entry (Right_Opnd (Exp));
7126 else
7127 return Membership_Entries (First (Alternatives (Exp)));
7128 end if;
7130 -- Negative membership (NOT IN)
7132 when N_Not_In =>
7133 if not Is_Type_Ref (Left_Opnd (Exp)) then
7134 raise Non_Static;
7135 end if;
7137 if Present (Right_Opnd (Exp)) then
7138 return not Membership_Entry (Right_Opnd (Exp));
7139 else
7140 return not Membership_Entries (First (Alternatives (Exp)));
7141 end if;
7143 -- Function call, may be call to static predicate
7145 when N_Function_Call =>
7146 if Is_Entity_Name (Name (Exp)) then
7147 declare
7148 Ent : constant Entity_Id := Entity (Name (Exp));
7149 begin
7150 if Is_Predicate_Function (Ent)
7151 or else
7152 Is_Predicate_Function_M (Ent)
7153 then
7154 return Stat_Pred (Etype (First_Formal (Ent)));
7155 end if;
7156 end;
7157 end if;
7159 -- Other function call cases are non-static
7161 raise Non_Static;
7163 -- Qualified expression, dig out the expression
7165 when N_Qualified_Expression =>
7166 return Get_RList (Expression (Exp));
7168 when N_Case_Expression =>
7169 declare
7170 Alt : Node_Id;
7171 Choices : List_Id;
7172 Dep : Node_Id;
7174 begin
7175 if not Is_Entity_Name (Expression (Expr))
7176 or else Etype (Expression (Expr)) /= Typ
7177 then
7178 Error_Msg_N
7179 ("expression must denaote subtype", Expression (Expr));
7180 return False_Range;
7181 end if;
7183 -- Collect discrete choices in all True alternatives
7185 Choices := New_List;
7186 Alt := First (Alternatives (Exp));
7187 while Present (Alt) loop
7188 Dep := Expression (Alt);
7190 if not Is_OK_Static_Expression (Dep) then
7191 raise Non_Static;
7193 elsif Is_True (Expr_Value (Dep)) then
7194 Append_List_To (Choices,
7195 New_Copy_List (Discrete_Choices (Alt)));
7196 end if;
7198 Next (Alt);
7199 end loop;
7201 return Membership_Entries (First (Choices));
7202 end;
7204 -- Expression with actions: if no actions, dig out expression
7206 when N_Expression_With_Actions =>
7207 if Is_Empty_List (Actions (Exp)) then
7208 return Get_RList (Expression (Exp));
7209 else
7210 raise Non_Static;
7211 end if;
7213 -- Xor operator
7215 when N_Op_Xor =>
7216 return (Get_RList (Left_Opnd (Exp))
7217 and not Get_RList (Right_Opnd (Exp)))
7218 or (Get_RList (Right_Opnd (Exp))
7219 and not Get_RList (Left_Opnd (Exp)));
7221 -- Any other node type is non-static
7223 when others =>
7224 raise Non_Static;
7225 end case;
7226 end Get_RList;
7228 ------------
7229 -- Hi_Val --
7230 ------------
7232 function Hi_Val (N : Node_Id) return Uint is
7233 begin
7234 if Is_OK_Static_Expression (N) then
7235 return Expr_Value (N);
7236 else
7237 pragma Assert (Nkind (N) = N_Range);
7238 return Expr_Value (High_Bound (N));
7239 end if;
7240 end Hi_Val;
7242 --------------
7243 -- Is_False --
7244 --------------
7246 function Is_False (R : RList) return Boolean is
7247 begin
7248 return R'Length = 0;
7249 end Is_False;
7251 -------------
7252 -- Is_True --
7253 -------------
7255 function Is_True (R : RList) return Boolean is
7256 begin
7257 return R'Length = 1
7258 and then R (R'First).Lo = BLo
7259 and then R (R'First).Hi = BHi;
7260 end Is_True;
7262 -----------------
7263 -- Is_Type_Ref --
7264 -----------------
7266 function Is_Type_Ref (N : Node_Id) return Boolean is
7267 begin
7268 return Nkind (N) = N_Identifier
7269 and then Chars (N) = Nam
7270 and then Paren_Count (N) = 0;
7271 end Is_Type_Ref;
7273 ------------
7274 -- Lo_Val --
7275 ------------
7277 function Lo_Val (N : Node_Id) return Uint is
7278 begin
7279 if Is_OK_Static_Expression (N) then
7280 return Expr_Value (N);
7281 else
7282 pragma Assert (Nkind (N) = N_Range);
7283 return Expr_Value (Low_Bound (N));
7284 end if;
7285 end Lo_Val;
7287 ------------------------
7288 -- Membership_Entries --
7289 ------------------------
7291 function Membership_Entries (N : Node_Id) return RList is
7292 begin
7293 if No (Next (N)) then
7294 return Membership_Entry (N);
7295 else
7296 return Membership_Entry (N) or Membership_Entries (Next (N));
7297 end if;
7298 end Membership_Entries;
7300 ----------------------
7301 -- Membership_Entry --
7302 ----------------------
7304 function Membership_Entry (N : Node_Id) return RList is
7305 Val : Uint;
7306 SLo : Uint;
7307 SHi : Uint;
7309 begin
7310 -- Range case
7312 if Nkind (N) = N_Range then
7313 if not Is_OK_Static_Expression (Low_Bound (N))
7314 or else
7315 not Is_OK_Static_Expression (High_Bound (N))
7316 then
7317 raise Non_Static;
7318 else
7319 SLo := Expr_Value (Low_Bound (N));
7320 SHi := Expr_Value (High_Bound (N));
7321 return RList'(1 => REnt'(SLo, SHi));
7322 end if;
7324 -- Static expression case
7326 elsif Is_OK_Static_Expression (N) then
7327 Val := Expr_Value (N);
7328 return RList'(1 => REnt'(Val, Val));
7330 -- Identifier (other than static expression) case
7332 else pragma Assert (Nkind (N) = N_Identifier);
7334 -- Type case
7336 if Is_Type (Entity (N)) then
7338 -- If type has predicates, process them
7340 if Has_Predicates (Entity (N)) then
7341 return Stat_Pred (Entity (N));
7343 -- For static subtype without predicates, get range
7345 elsif Is_OK_Static_Subtype (Entity (N)) then
7346 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7347 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7348 return RList'(1 => REnt'(SLo, SHi));
7350 -- Any other type makes us non-static
7352 else
7353 raise Non_Static;
7354 end if;
7356 -- Any other kind of identifier in predicate (e.g. a non-static
7357 -- expression value) means this is not a static predicate.
7359 else
7360 raise Non_Static;
7361 end if;
7362 end if;
7363 end Membership_Entry;
7365 ---------------
7366 -- Stat_Pred --
7367 ---------------
7369 function Stat_Pred (Typ : Entity_Id) return RList is
7370 begin
7371 -- Not static if type does not have static predicates
7373 if not Has_Static_Predicate (Typ) then
7374 raise Non_Static;
7375 end if;
7377 -- Otherwise we convert the predicate list to a range list
7379 declare
7380 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7381 Result : RList (1 .. List_Length (Spred));
7382 P : Node_Id;
7384 begin
7385 P := First (Static_Discrete_Predicate (Typ));
7386 for J in Result'Range loop
7387 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7388 Next (P);
7389 end loop;
7391 return Result;
7392 end;
7393 end Stat_Pred;
7395 -- Start of processing for Build_Discrete_Static_Predicate
7397 begin
7398 -- Analyze the expression to see if it is a static predicate
7400 declare
7401 Ranges : constant RList := Get_RList (Expr);
7402 -- Range list from expression if it is static
7404 Plist : List_Id;
7406 begin
7407 -- Convert range list into a form for the static predicate. In the
7408 -- Ranges array, we just have raw ranges, these must be converted
7409 -- to properly typed and analyzed static expressions or range nodes.
7411 -- Note: here we limit ranges to the ranges of the subtype, so that
7412 -- a predicate is always false for values outside the subtype. That
7413 -- seems fine, such values are invalid anyway, and considering them
7414 -- to fail the predicate seems allowed and friendly, and furthermore
7415 -- simplifies processing for case statements and loops.
7417 Plist := New_List;
7419 for J in Ranges'Range loop
7420 declare
7421 Lo : Uint := Ranges (J).Lo;
7422 Hi : Uint := Ranges (J).Hi;
7424 begin
7425 -- Ignore completely out of range entry
7427 if Hi < TLo or else Lo > THi then
7428 null;
7430 -- Otherwise process entry
7432 else
7433 -- Adjust out of range value to subtype range
7435 if Lo < TLo then
7436 Lo := TLo;
7437 end if;
7439 if Hi > THi then
7440 Hi := THi;
7441 end if;
7443 -- Convert range into required form
7445 Append_To (Plist, Build_Range (Lo, Hi));
7446 end if;
7447 end;
7448 end loop;
7450 -- Processing was successful and all entries were static, so now we
7451 -- can store the result as the predicate list.
7453 Set_Static_Discrete_Predicate (Typ, Plist);
7455 -- The processing for static predicates put the expression into
7456 -- canonical form as a series of ranges. It also eliminated
7457 -- duplicates and collapsed and combined ranges. We might as well
7458 -- replace the alternatives list of the right operand of the
7459 -- membership test with the static predicate list, which will
7460 -- usually be more efficient.
7462 declare
7463 New_Alts : constant List_Id := New_List;
7464 Old_Node : Node_Id;
7465 New_Node : Node_Id;
7467 begin
7468 Old_Node := First (Plist);
7469 while Present (Old_Node) loop
7470 New_Node := New_Copy (Old_Node);
7472 if Nkind (New_Node) = N_Range then
7473 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7474 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7475 end if;
7477 Append_To (New_Alts, New_Node);
7478 Next (Old_Node);
7479 end loop;
7481 -- If empty list, replace by False
7483 if Is_Empty_List (New_Alts) then
7484 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7486 -- Else replace by set membership test
7488 else
7489 Rewrite (Expr,
7490 Make_In (Loc,
7491 Left_Opnd => Make_Identifier (Loc, Nam),
7492 Right_Opnd => Empty,
7493 Alternatives => New_Alts));
7495 -- Resolve new expression in function context
7497 Install_Formals (Predicate_Function (Typ));
7498 Push_Scope (Predicate_Function (Typ));
7499 Analyze_And_Resolve (Expr, Standard_Boolean);
7500 Pop_Scope;
7501 end if;
7502 end;
7503 end;
7505 -- If non-static, return doing nothing
7507 exception
7508 when Non_Static =>
7509 return;
7510 end Build_Discrete_Static_Predicate;
7512 -------------------------------------------
7513 -- Build_Invariant_Procedure_Declaration --
7514 -------------------------------------------
7516 function Build_Invariant_Procedure_Declaration
7517 (Typ : Entity_Id) return Node_Id
7519 Loc : constant Source_Ptr := Sloc (Typ);
7520 Object_Entity : constant Entity_Id :=
7521 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7522 Spec : Node_Id;
7523 SId : Entity_Id;
7525 begin
7526 Set_Etype (Object_Entity, Typ);
7528 -- Check for duplicate definiations.
7530 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7531 return Empty;
7532 end if;
7534 SId :=
7535 Make_Defining_Identifier (Loc,
7536 Chars => New_External_Name (Chars (Typ), "Invariant"));
7537 Set_Has_Invariants (Typ);
7538 Set_Ekind (SId, E_Procedure);
7539 Set_Etype (SId, Standard_Void_Type);
7540 Set_Is_Invariant_Procedure (SId);
7541 Set_Invariant_Procedure (Typ, SId);
7543 Spec :=
7544 Make_Procedure_Specification (Loc,
7545 Defining_Unit_Name => SId,
7546 Parameter_Specifications => New_List (
7547 Make_Parameter_Specification (Loc,
7548 Defining_Identifier => Object_Entity,
7549 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7551 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7552 end Build_Invariant_Procedure_Declaration;
7554 -------------------------------
7555 -- Build_Invariant_Procedure --
7556 -------------------------------
7558 -- The procedure that is constructed here has the form
7560 -- procedure typInvariant (Ixxx : typ) is
7561 -- begin
7562 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7563 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7564 -- ...
7565 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7566 -- ...
7567 -- end typInvariant;
7569 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7570 Loc : constant Source_Ptr := Sloc (Typ);
7571 Stmts : List_Id;
7572 Spec : Node_Id;
7573 SId : Entity_Id;
7574 PDecl : Node_Id;
7575 PBody : Node_Id;
7577 Nam : Name_Id;
7578 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7579 -- if we come from a Type_Invariant aspect, we make sure to build the
7580 -- Check pragma with the right name, so that Check_Policy works right.
7582 Visible_Decls : constant List_Id := Visible_Declarations (N);
7583 Private_Decls : constant List_Id := Private_Declarations (N);
7585 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7586 -- Appends statements to Stmts for any invariants in the rep item chain
7587 -- of the given type. If Inherit is False, then we only process entries
7588 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7589 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7590 -- "inherited" to the exception message and generating an informational
7591 -- message about the inheritance of an invariant.
7593 Object_Name : Name_Id;
7594 -- Name for argument of invariant procedure
7596 Object_Entity : Node_Id;
7597 -- The entity of the formal for the procedure
7599 --------------------
7600 -- Add_Invariants --
7601 --------------------
7603 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7604 Ritem : Node_Id;
7605 Arg1 : Node_Id;
7606 Arg2 : Node_Id;
7607 Arg3 : Node_Id;
7608 Exp : Node_Id;
7609 Loc : Source_Ptr;
7610 Assoc : List_Id;
7611 Str : String_Id;
7613 procedure Replace_Type_Reference (N : Node_Id);
7614 -- Replace a single occurrence N of the subtype name with a reference
7615 -- to the formal of the predicate function. N can be an identifier
7616 -- referencing the subtype, or a selected component, representing an
7617 -- appropriately qualified occurrence of the subtype name.
7619 procedure Replace_Type_References is
7620 new Replace_Type_References_Generic (Replace_Type_Reference);
7621 -- Traverse an expression replacing all occurrences of the subtype
7622 -- name with appropriate references to the object that is the formal
7623 -- parameter of the predicate function. Note that we must ensure
7624 -- that the type and entity information is properly set in the
7625 -- replacement node, since we will do a Preanalyze call of this
7626 -- expression without proper visibility of the procedure argument.
7628 ----------------------------
7629 -- Replace_Type_Reference --
7630 ----------------------------
7632 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7633 -- regarding handling of Sloc and Comes_From_Source.
7635 procedure Replace_Type_Reference (N : Node_Id) is
7636 begin
7638 -- Add semantic information to node to be rewritten, for ASIS
7639 -- navigation needs.
7641 if Nkind (N) = N_Identifier then
7642 Set_Entity (N, T);
7643 Set_Etype (N, T);
7645 elsif Nkind (N) = N_Selected_Component then
7646 Analyze (Prefix (N));
7647 Set_Entity (Selector_Name (N), T);
7648 Set_Etype (Selector_Name (N), T);
7649 end if;
7651 -- Invariant'Class, replace with T'Class (obj)
7652 -- In ASIS mode, an inherited item is analyzed already, and the
7653 -- replacement has been done, so do not repeat transformation
7654 -- to prevent ill-formed tree.
7656 if Class_Present (Ritem) then
7657 if ASIS_Mode
7658 and then Nkind (Parent (N)) = N_Attribute_Reference
7659 and then Attribute_Name (Parent (N)) = Name_Class
7660 then
7661 null;
7663 else
7664 Rewrite (N,
7665 Make_Type_Conversion (Sloc (N),
7666 Subtype_Mark =>
7667 Make_Attribute_Reference (Sloc (N),
7668 Prefix => New_Occurrence_Of (T, Sloc (N)),
7669 Attribute_Name => Name_Class),
7670 Expression =>
7671 Make_Identifier (Sloc (N), Object_Name)));
7673 Set_Entity (Expression (N), Object_Entity);
7674 Set_Etype (Expression (N), Typ);
7675 end if;
7677 -- Invariant, replace with obj
7679 else
7680 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7681 Set_Entity (N, Object_Entity);
7682 Set_Etype (N, Typ);
7683 end if;
7685 Set_Comes_From_Source (N, True);
7686 end Replace_Type_Reference;
7688 -- Start of processing for Add_Invariants
7690 begin
7691 Ritem := First_Rep_Item (T);
7692 while Present (Ritem) loop
7693 if Nkind (Ritem) = N_Pragma
7694 and then Pragma_Name (Ritem) = Name_Invariant
7695 then
7696 Arg1 := First (Pragma_Argument_Associations (Ritem));
7697 Arg2 := Next (Arg1);
7698 Arg3 := Next (Arg2);
7700 Arg1 := Get_Pragma_Arg (Arg1);
7701 Arg2 := Get_Pragma_Arg (Arg2);
7703 -- For Inherit case, ignore Invariant, process only Class case
7705 if Inherit then
7706 if not Class_Present (Ritem) then
7707 goto Continue;
7708 end if;
7710 -- For Inherit false, process only item for right type
7712 else
7713 if Entity (Arg1) /= Typ then
7714 goto Continue;
7715 end if;
7716 end if;
7718 if No (Stmts) then
7719 Stmts := Empty_List;
7720 end if;
7722 Exp := New_Copy_Tree (Arg2);
7724 -- Preserve sloc of original pragma Invariant
7726 Loc := Sloc (Ritem);
7728 -- We need to replace any occurrences of the name of the type
7729 -- with references to the object, converted to type'Class in
7730 -- the case of Invariant'Class aspects.
7732 Replace_Type_References (Exp, T);
7734 -- If this invariant comes from an aspect, find the aspect
7735 -- specification, and replace the saved expression because
7736 -- we need the subtype references replaced for the calls to
7737 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7738 -- and Check_Aspect_At_End_Of_Declarations.
7740 if From_Aspect_Specification (Ritem) then
7741 declare
7742 Aitem : Node_Id;
7744 begin
7745 -- Loop to find corresponding aspect, note that this
7746 -- must be present given the pragma is marked delayed.
7748 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7749 -- this loop does nothing. Furthermore, why isn't this
7750 -- simply Corresponding_Aspect ???
7752 Aitem := Next_Rep_Item (Ritem);
7753 while Present (Aitem) loop
7754 if Nkind (Aitem) = N_Aspect_Specification
7755 and then Aspect_Rep_Item (Aitem) = Ritem
7756 then
7757 Set_Entity
7758 (Identifier (Aitem), New_Copy_Tree (Exp));
7759 exit;
7760 end if;
7762 Aitem := Next_Rep_Item (Aitem);
7763 end loop;
7764 end;
7765 end if;
7767 -- Now we need to preanalyze the expression to properly capture
7768 -- the visibility in the visible part. The expression will not
7769 -- be analyzed for real until the body is analyzed, but that is
7770 -- at the end of the private part and has the wrong visibility.
7772 Set_Parent (Exp, N);
7773 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
7775 -- A class-wide invariant may be inherited in a separate unit,
7776 -- where the corresponding expression cannot be resolved by
7777 -- visibility, because it refers to a local function. Propagate
7778 -- semantic information to the original representation item, to
7779 -- be used when an invariant procedure for a derived type is
7780 -- constructed.
7782 -- Unclear how to handle class-wide invariants that are not
7783 -- function calls ???
7785 if not Inherit
7786 and then Class_Present (Ritem)
7787 and then Nkind (Exp) = N_Function_Call
7788 and then Nkind (Arg2) = N_Indexed_Component
7789 then
7790 Rewrite (Arg2,
7791 Make_Function_Call (Loc,
7792 Name =>
7793 New_Occurrence_Of (Entity (Name (Exp)), Loc),
7794 Parameter_Associations =>
7795 New_Copy_List (Expressions (Arg2))));
7796 end if;
7798 -- In ASIS mode, even if assertions are not enabled, we must
7799 -- analyze the original expression in the aspect specification
7800 -- because it is part of the original tree.
7802 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7803 declare
7804 Inv : constant Node_Id :=
7805 Expression (Corresponding_Aspect (Ritem));
7806 begin
7807 Replace_Type_References (Inv, T);
7808 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7809 end;
7810 end if;
7812 -- Get name to be used for Check pragma
7814 if not From_Aspect_Specification (Ritem) then
7815 Nam := Name_Invariant;
7816 else
7817 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7818 end if;
7820 -- Build first two arguments for Check pragma
7822 Assoc :=
7823 New_List (
7824 Make_Pragma_Argument_Association (Loc,
7825 Expression => Make_Identifier (Loc, Chars => Nam)),
7826 Make_Pragma_Argument_Association (Loc,
7827 Expression => Exp));
7829 -- Add message if present in Invariant pragma
7831 if Present (Arg3) then
7832 Str := Strval (Get_Pragma_Arg (Arg3));
7834 -- If inherited case, and message starts "failed invariant",
7835 -- change it to be "failed inherited invariant".
7837 if Inherit then
7838 String_To_Name_Buffer (Str);
7840 if Name_Buffer (1 .. 16) = "failed invariant" then
7841 Insert_Str_In_Name_Buffer ("inherited ", 8);
7842 Str := String_From_Name_Buffer;
7843 end if;
7844 end if;
7846 Append_To (Assoc,
7847 Make_Pragma_Argument_Association (Loc,
7848 Expression => Make_String_Literal (Loc, Str)));
7849 end if;
7851 -- Add Check pragma to list of statements
7853 Append_To (Stmts,
7854 Make_Pragma (Loc,
7855 Pragma_Identifier =>
7856 Make_Identifier (Loc, Name_Check),
7857 Pragma_Argument_Associations => Assoc));
7859 -- If Inherited case and option enabled, output info msg. Note
7860 -- that we know this is a case of Invariant'Class.
7862 if Inherit and Opt.List_Inherited_Aspects then
7863 Error_Msg_Sloc := Sloc (Ritem);
7864 Error_Msg_N
7865 ("info: & inherits `Invariant''Class` aspect from #?L?",
7866 Typ);
7867 end if;
7868 end if;
7870 <<Continue>>
7871 Next_Rep_Item (Ritem);
7872 end loop;
7873 end Add_Invariants;
7875 -- Start of processing for Build_Invariant_Procedure
7877 begin
7878 Stmts := No_List;
7879 PDecl := Empty;
7880 PBody := Empty;
7881 SId := Empty;
7883 -- If the aspect specification exists for some view of the type, the
7884 -- declaration for the procedure has been created.
7886 if Has_Invariants (Typ) then
7887 SId := Invariant_Procedure (Typ);
7888 end if;
7890 -- If the body is already present, nothing to do. This will occur when
7891 -- the type is already frozen, which is the case when the invariant
7892 -- appears in a private part, and the freezing takes place before the
7893 -- final pass over full declarations.
7895 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
7897 if Present (SId) then
7898 PDecl := Unit_Declaration_Node (SId);
7900 if Present (PDecl)
7901 and then Nkind (PDecl) = N_Subprogram_Declaration
7902 and then Present (Corresponding_Body (PDecl))
7903 then
7904 return;
7905 end if;
7907 else
7908 PDecl := Build_Invariant_Procedure_Declaration (Typ);
7909 end if;
7911 -- Recover formal of procedure, for use in the calls to invariant
7912 -- functions (including inherited ones).
7914 Object_Entity :=
7915 Defining_Identifier
7916 (First (Parameter_Specifications (Specification (PDecl))));
7917 Object_Name := Chars (Object_Entity);
7919 -- Add invariants for the current type
7921 Add_Invariants (Typ, Inherit => False);
7923 -- Add invariants for parent types
7925 declare
7926 Current_Typ : Entity_Id;
7927 Parent_Typ : Entity_Id;
7929 begin
7930 Current_Typ := Typ;
7931 loop
7932 Parent_Typ := Etype (Current_Typ);
7934 if Is_Private_Type (Parent_Typ)
7935 and then Present (Full_View (Base_Type (Parent_Typ)))
7936 then
7937 Parent_Typ := Full_View (Base_Type (Parent_Typ));
7938 end if;
7940 exit when Parent_Typ = Current_Typ;
7942 Current_Typ := Parent_Typ;
7943 Add_Invariants (Current_Typ, Inherit => True);
7944 end loop;
7945 end;
7947 -- Build the procedure if we generated at least one Check pragma
7949 if Stmts /= No_List then
7950 Spec := Copy_Separate_Tree (Specification (PDecl));
7952 PBody :=
7953 Make_Subprogram_Body (Loc,
7954 Specification => Spec,
7955 Declarations => Empty_List,
7956 Handled_Statement_Sequence =>
7957 Make_Handled_Sequence_Of_Statements (Loc,
7958 Statements => Stmts));
7960 -- Insert procedure declaration and spec at the appropriate points.
7961 -- If declaration is already analyzed, it was processed by the
7962 -- generated pragma.
7964 if Present (Private_Decls) then
7966 -- The spec goes at the end of visible declarations, but they have
7967 -- already been analyzed, so we need to explicitly do the analyze.
7969 if not Analyzed (PDecl) then
7970 Append_To (Visible_Decls, PDecl);
7971 Analyze (PDecl);
7972 end if;
7974 -- The body goes at the end of the private declarations, which we
7975 -- have not analyzed yet, so we do not need to perform an explicit
7976 -- analyze call. We skip this if there are no private declarations
7977 -- (this is an error that will be caught elsewhere);
7979 Append_To (Private_Decls, PBody);
7981 -- If the invariant appears on the full view of a type, the
7982 -- analysis of the private part is complete, and we must
7983 -- analyze the new body explicitly.
7985 if In_Private_Part (Current_Scope) then
7986 Analyze (PBody);
7987 end if;
7989 -- If there are no private declarations this may be an error that
7990 -- will be diagnosed elsewhere. However, if this is a non-private
7991 -- type that inherits invariants, it needs no completion and there
7992 -- may be no private part. In this case insert invariant procedure
7993 -- at end of current declarative list, and analyze at once, given
7994 -- that the type is about to be frozen.
7996 elsif not Is_Private_Type (Typ) then
7997 Append_To (Visible_Decls, PDecl);
7998 Append_To (Visible_Decls, PBody);
7999 Analyze (PDecl);
8000 Analyze (PBody);
8001 end if;
8002 end if;
8003 end Build_Invariant_Procedure;
8005 -------------------------------
8006 -- Build_Predicate_Functions --
8007 -------------------------------
8009 -- The procedures that are constructed here have the form:
8011 -- function typPredicate (Ixxx : typ) return Boolean is
8012 -- begin
8013 -- return
8014 -- exp1 and then exp2 and then ...
8015 -- and then typ1Predicate (typ1 (Ixxx))
8016 -- and then typ2Predicate (typ2 (Ixxx))
8017 -- and then ...;
8018 -- end typPredicate;
8020 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8021 -- this is the point at which these expressions get analyzed, providing the
8022 -- required delay, and typ1, typ2, are entities from which predicates are
8023 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8024 -- use this function even if checks are off, e.g. for membership tests.
8026 -- If the expression has at least one Raise_Expression, then we also build
8027 -- the typPredicateM version of the function, in which any occurrence of a
8028 -- Raise_Expression is converted to "return False".
8030 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8031 Loc : constant Source_Ptr := Sloc (Typ);
8033 Expr : Node_Id;
8034 -- This is the expression for the result of the function. It is
8035 -- is build by connecting the component predicates with AND THEN.
8037 Expr_M : Node_Id;
8038 -- This is the corresponding return expression for the Predicate_M
8039 -- function. It differs in that raise expressions are marked for
8040 -- special expansion (see Process_REs).
8042 Object_Name : constant Name_Id := New_Internal_Name ('I');
8043 -- Name for argument of Predicate procedure. Note that we use the same
8044 -- name for both predicate functions. That way the reference within the
8045 -- predicate expression is the same in both functions.
8047 Object_Entity : constant Entity_Id :=
8048 Make_Defining_Identifier (Loc, Chars => Object_Name);
8049 -- Entity for argument of Predicate procedure
8051 Object_Entity_M : constant Entity_Id :=
8052 Make_Defining_Identifier (Loc, Chars => Object_Name);
8053 -- Entity for argument of Predicate_M procedure
8055 Raise_Expression_Present : Boolean := False;
8056 -- Set True if Expr has at least one Raise_Expression
8058 procedure Add_Call (T : Entity_Id);
8059 -- Includes a call to the predicate function for type T in Expr if T
8060 -- has predicates and Predicate_Function (T) is non-empty.
8062 procedure Add_Predicates;
8063 -- Appends expressions for any Predicate pragmas in the rep item chain
8064 -- Typ to Expr. Note that we look only at items for this exact entity.
8065 -- Inheritance of predicates for the parent type is done by calling the
8066 -- Predicate_Function of the parent type, using Add_Call above.
8068 function Test_RE (N : Node_Id) return Traverse_Result;
8069 -- Used in Test_REs, tests one node for being a raise expression, and if
8070 -- so sets Raise_Expression_Present True.
8072 procedure Test_REs is new Traverse_Proc (Test_RE);
8073 -- Tests to see if Expr contains any raise expressions
8075 function Process_RE (N : Node_Id) return Traverse_Result;
8076 -- Used in Process REs, tests if node N is a raise expression, and if
8077 -- so, marks it to be converted to return False.
8079 procedure Process_REs is new Traverse_Proc (Process_RE);
8080 -- Marks any raise expressions in Expr_M to return False
8082 --------------
8083 -- Add_Call --
8084 --------------
8086 procedure Add_Call (T : Entity_Id) is
8087 Exp : Node_Id;
8089 begin
8090 if Present (T) and then Present (Predicate_Function (T)) then
8091 Set_Has_Predicates (Typ);
8093 -- Build the call to the predicate function of T
8095 Exp :=
8096 Make_Predicate_Call
8097 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8099 -- Add call to evolving expression, using AND THEN if needed
8101 if No (Expr) then
8102 Expr := Exp;
8104 else
8105 Expr :=
8106 Make_And_Then (Sloc (Expr),
8107 Left_Opnd => Relocate_Node (Expr),
8108 Right_Opnd => Exp);
8109 end if;
8111 -- Output info message on inheritance if required. Note we do not
8112 -- give this information for generic actual types, since it is
8113 -- unwelcome noise in that case in instantiations. We also
8114 -- generally suppress the message in instantiations, and also
8115 -- if it involves internal names.
8117 if Opt.List_Inherited_Aspects
8118 and then not Is_Generic_Actual_Type (Typ)
8119 and then Instantiation_Depth (Sloc (Typ)) = 0
8120 and then not Is_Internal_Name (Chars (T))
8121 and then not Is_Internal_Name (Chars (Typ))
8122 then
8123 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8124 Error_Msg_Node_2 := T;
8125 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8126 end if;
8127 end if;
8128 end Add_Call;
8130 --------------------
8131 -- Add_Predicates --
8132 --------------------
8134 procedure Add_Predicates is
8135 Ritem : Node_Id;
8136 Arg1 : Node_Id;
8137 Arg2 : Node_Id;
8139 procedure Replace_Type_Reference (N : Node_Id);
8140 -- Replace a single occurrence N of the subtype name with a reference
8141 -- to the formal of the predicate function. N can be an identifier
8142 -- referencing the subtype, or a selected component, representing an
8143 -- appropriately qualified occurrence of the subtype name.
8145 procedure Replace_Type_References is
8146 new Replace_Type_References_Generic (Replace_Type_Reference);
8147 -- Traverse an expression changing every occurrence of an identifier
8148 -- whose name matches the name of the subtype with a reference to
8149 -- the formal parameter of the predicate function.
8151 ----------------------------
8152 -- Replace_Type_Reference --
8153 ----------------------------
8155 procedure Replace_Type_Reference (N : Node_Id) is
8156 begin
8157 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8158 -- Use the Sloc of the usage name, not the defining name
8160 Set_Etype (N, Typ);
8161 Set_Entity (N, Object_Entity);
8163 -- We want to treat the node as if it comes from source, so that
8164 -- ASIS will not ignore it
8166 Set_Comes_From_Source (N, True);
8167 end Replace_Type_Reference;
8169 -- Start of processing for Add_Predicates
8171 begin
8172 Ritem := First_Rep_Item (Typ);
8173 while Present (Ritem) loop
8174 if Nkind (Ritem) = N_Pragma
8175 and then Pragma_Name (Ritem) = Name_Predicate
8176 then
8177 -- Acquire arguments
8179 Arg1 := First (Pragma_Argument_Associations (Ritem));
8180 Arg2 := Next (Arg1);
8182 Arg1 := Get_Pragma_Arg (Arg1);
8183 Arg2 := Get_Pragma_Arg (Arg2);
8185 -- See if this predicate pragma is for the current type or for
8186 -- its full view. A predicate on a private completion is placed
8187 -- on the partial view beause this is the visible entity that
8188 -- is frozen.
8190 if Entity (Arg1) = Typ
8191 or else Full_View (Entity (Arg1)) = Typ
8192 then
8193 -- We have a match, this entry is for our subtype
8195 -- We need to replace any occurrences of the name of the
8196 -- type with references to the object.
8198 Replace_Type_References (Arg2, Typ);
8200 -- If this predicate comes from an aspect, find the aspect
8201 -- specification, and replace the saved expression because
8202 -- we need the subtype references replaced for the calls to
8203 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8204 -- and Check_Aspect_At_End_Of_Declarations.
8206 if From_Aspect_Specification (Ritem) then
8207 declare
8208 Aitem : Node_Id;
8210 begin
8211 -- Loop to find corresponding aspect, note that this
8212 -- must be present given the pragma is marked delayed.
8214 Aitem := Next_Rep_Item (Ritem);
8215 loop
8216 if Nkind (Aitem) = N_Aspect_Specification
8217 and then Aspect_Rep_Item (Aitem) = Ritem
8218 then
8219 Set_Entity
8220 (Identifier (Aitem), New_Copy_Tree (Arg2));
8221 exit;
8222 end if;
8224 Aitem := Next_Rep_Item (Aitem);
8225 end loop;
8226 end;
8227 end if;
8229 -- Now we can add the expression
8231 if No (Expr) then
8232 Expr := Relocate_Node (Arg2);
8234 -- There already was a predicate, so add to it
8236 else
8237 Expr :=
8238 Make_And_Then (Loc,
8239 Left_Opnd => Relocate_Node (Expr),
8240 Right_Opnd => Relocate_Node (Arg2));
8241 end if;
8242 end if;
8243 end if;
8245 Next_Rep_Item (Ritem);
8246 end loop;
8247 end Add_Predicates;
8249 ----------------
8250 -- Process_RE --
8251 ----------------
8253 function Process_RE (N : Node_Id) return Traverse_Result is
8254 begin
8255 if Nkind (N) = N_Raise_Expression then
8256 Set_Convert_To_Return_False (N);
8257 return Skip;
8258 else
8259 return OK;
8260 end if;
8261 end Process_RE;
8263 -------------
8264 -- Test_RE --
8265 -------------
8267 function Test_RE (N : Node_Id) return Traverse_Result is
8268 begin
8269 if Nkind (N) = N_Raise_Expression then
8270 Raise_Expression_Present := True;
8271 return Abandon;
8272 else
8273 return OK;
8274 end if;
8275 end Test_RE;
8277 -- Start of processing for Build_Predicate_Functions
8279 begin
8280 -- Return if already built or if type does not have predicates
8282 if not Has_Predicates (Typ)
8283 or else Present (Predicate_Function (Typ))
8284 then
8285 return;
8286 end if;
8288 -- Prepare to construct predicate expression
8290 Expr := Empty;
8292 -- Add Predicates for the current type
8294 Add_Predicates;
8296 -- Add predicates for ancestor if present
8298 declare
8299 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8300 begin
8301 if Present (Atyp) then
8302 Add_Call (Atyp);
8303 end if;
8304 end;
8306 -- Case where predicates are present
8308 if Present (Expr) then
8310 -- Test for raise expression present
8312 Test_REs (Expr);
8314 -- If raise expression is present, capture a copy of Expr for use
8315 -- in building the predicateM function version later on. For this
8316 -- copy we replace references to Object_Entity by Object_Entity_M.
8318 if Raise_Expression_Present then
8319 declare
8320 Map : constant Elist_Id := New_Elmt_List;
8321 New_V : Entity_Id := Empty;
8323 -- The unanalyzed expression will be copied and appear in
8324 -- both functions. Normally expressions do not declare new
8325 -- entities, but quantified expressions do, so we need to
8326 -- create new entities for their bound variables, to prevent
8327 -- multiple definitions in gigi.
8329 function Reset_Loop_Variable (N : Node_Id)
8330 return Traverse_Result;
8332 procedure Collect_Loop_Variables is
8333 new Traverse_Proc (Reset_Loop_Variable);
8335 ------------------------
8336 -- Reset_Loop_Variable --
8337 ------------------------
8339 function Reset_Loop_Variable (N : Node_Id)
8340 return Traverse_Result
8342 begin
8343 if Nkind (N) = N_Iterator_Specification then
8344 New_V := Make_Defining_Identifier
8345 (Sloc (N), Chars (Defining_Identifier (N)));
8347 Set_Defining_Identifier (N, New_V);
8348 end if;
8350 return OK;
8351 end Reset_Loop_Variable;
8353 begin
8354 Append_Elmt (Object_Entity, Map);
8355 Append_Elmt (Object_Entity_M, Map);
8356 Expr_M := New_Copy_Tree (Expr, Map => Map);
8357 Collect_Loop_Variables (Expr_M);
8358 end;
8359 end if;
8361 -- Build the main predicate function
8363 declare
8364 SId : constant Entity_Id :=
8365 Make_Defining_Identifier (Loc,
8366 Chars => New_External_Name (Chars (Typ), "Predicate"));
8367 -- The entity for the the function spec
8369 SIdB : constant Entity_Id :=
8370 Make_Defining_Identifier (Loc,
8371 Chars => New_External_Name (Chars (Typ), "Predicate"));
8372 -- The entity for the function body
8374 Spec : Node_Id;
8375 FDecl : Node_Id;
8376 FBody : Node_Id;
8378 begin
8379 -- Build function declaration
8381 Set_Ekind (SId, E_Function);
8382 Set_Is_Internal (SId);
8383 Set_Is_Predicate_Function (SId);
8384 Set_Predicate_Function (Typ, SId);
8386 -- The predicate function is shared between views of a type
8388 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8389 Set_Predicate_Function (Full_View (Typ), SId);
8390 end if;
8392 Spec :=
8393 Make_Function_Specification (Loc,
8394 Defining_Unit_Name => SId,
8395 Parameter_Specifications => New_List (
8396 Make_Parameter_Specification (Loc,
8397 Defining_Identifier => Object_Entity,
8398 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8399 Result_Definition =>
8400 New_Occurrence_Of (Standard_Boolean, Loc));
8402 FDecl :=
8403 Make_Subprogram_Declaration (Loc,
8404 Specification => Spec);
8406 -- Build function body
8408 Spec :=
8409 Make_Function_Specification (Loc,
8410 Defining_Unit_Name => SIdB,
8411 Parameter_Specifications => New_List (
8412 Make_Parameter_Specification (Loc,
8413 Defining_Identifier =>
8414 Make_Defining_Identifier (Loc, Object_Name),
8415 Parameter_Type =>
8416 New_Occurrence_Of (Typ, Loc))),
8417 Result_Definition =>
8418 New_Occurrence_Of (Standard_Boolean, Loc));
8420 FBody :=
8421 Make_Subprogram_Body (Loc,
8422 Specification => Spec,
8423 Declarations => Empty_List,
8424 Handled_Statement_Sequence =>
8425 Make_Handled_Sequence_Of_Statements (Loc,
8426 Statements => New_List (
8427 Make_Simple_Return_Statement (Loc,
8428 Expression => Expr))));
8430 -- Insert declaration before freeze node and body after
8432 Insert_Before_And_Analyze (N, FDecl);
8433 Insert_After_And_Analyze (N, FBody);
8434 end;
8436 -- Test for raise expressions present and if so build M version
8438 if Raise_Expression_Present then
8439 declare
8440 SId : constant Entity_Id :=
8441 Make_Defining_Identifier (Loc,
8442 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8443 -- The entity for the the function spec
8445 SIdB : constant Entity_Id :=
8446 Make_Defining_Identifier (Loc,
8447 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8448 -- The entity for the function body
8450 Spec : Node_Id;
8451 FDecl : Node_Id;
8452 FBody : Node_Id;
8453 BTemp : Entity_Id;
8455 begin
8456 -- Mark any raise expressions for special expansion
8458 Process_REs (Expr_M);
8460 -- Build function declaration
8462 Set_Ekind (SId, E_Function);
8463 Set_Is_Predicate_Function_M (SId);
8464 Set_Predicate_Function_M (Typ, SId);
8466 -- The predicate function is shared between views of a type
8468 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8469 Set_Predicate_Function_M (Full_View (Typ), SId);
8470 end if;
8472 Spec :=
8473 Make_Function_Specification (Loc,
8474 Defining_Unit_Name => SId,
8475 Parameter_Specifications => New_List (
8476 Make_Parameter_Specification (Loc,
8477 Defining_Identifier => Object_Entity_M,
8478 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8479 Result_Definition =>
8480 New_Occurrence_Of (Standard_Boolean, Loc));
8482 FDecl :=
8483 Make_Subprogram_Declaration (Loc,
8484 Specification => Spec);
8486 -- Build function body
8488 Spec :=
8489 Make_Function_Specification (Loc,
8490 Defining_Unit_Name => SIdB,
8491 Parameter_Specifications => New_List (
8492 Make_Parameter_Specification (Loc,
8493 Defining_Identifier =>
8494 Make_Defining_Identifier (Loc, Object_Name),
8495 Parameter_Type =>
8496 New_Occurrence_Of (Typ, Loc))),
8497 Result_Definition =>
8498 New_Occurrence_Of (Standard_Boolean, Loc));
8500 -- Build the body, we declare the boolean expression before
8501 -- doing the return, because we are not really confident of
8502 -- what happens if a return appears within a return.
8504 BTemp :=
8505 Make_Defining_Identifier (Loc,
8506 Chars => New_Internal_Name ('B'));
8508 FBody :=
8509 Make_Subprogram_Body (Loc,
8510 Specification => Spec,
8512 Declarations => New_List (
8513 Make_Object_Declaration (Loc,
8514 Defining_Identifier => BTemp,
8515 Constant_Present => True,
8516 Object_Definition =>
8517 New_Occurrence_Of (Standard_Boolean, Loc),
8518 Expression => Expr_M)),
8520 Handled_Statement_Sequence =>
8521 Make_Handled_Sequence_Of_Statements (Loc,
8522 Statements => New_List (
8523 Make_Simple_Return_Statement (Loc,
8524 Expression => New_Occurrence_Of (BTemp, Loc)))));
8526 -- Insert declaration before freeze node and body after
8528 Insert_Before_And_Analyze (N, FDecl);
8529 Insert_After_And_Analyze (N, FBody);
8530 end;
8531 end if;
8533 -- See if we have a static predicate. Note that the answer may be
8534 -- yes even if we have an explicit Dynamic_Predicate present.
8536 declare
8537 PS : Boolean;
8538 EN : Node_Id;
8540 begin
8541 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8542 PS := False;
8543 else
8544 PS := Is_Predicate_Static (Expr, Object_Name);
8545 end if;
8547 -- Case where we have a predicate-static aspect
8549 if PS then
8551 -- We don't set Has_Static_Predicate_Aspect, since we can have
8552 -- any of the three cases (Predicate, Dynamic_Predicate, or
8553 -- Static_Predicate) generating a predicate with an expression
8554 -- that is predicate-static. We just indicate that we have a
8555 -- predicate that can be treated as static.
8557 Set_Has_Static_Predicate (Typ);
8559 -- For discrete subtype, build the static predicate list
8561 if Is_Discrete_Type (Typ) then
8562 if not Is_Static_Subtype (Typ) then
8564 -- This can only happen in the presence of previous
8565 -- semantic errors.
8567 pragma Assert (Serious_Errors_Detected > 0);
8568 return;
8569 end if;
8571 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8573 -- If we don't get a static predicate list, it means that we
8574 -- have a case where this is not possible, most typically in
8575 -- the case where we inherit a dynamic predicate. We do not
8576 -- consider this an error, we just leave the predicate as
8577 -- dynamic. But if we do succeed in building the list, then
8578 -- we mark the predicate as static.
8580 if No (Static_Discrete_Predicate (Typ)) then
8581 Set_Has_Static_Predicate (Typ, False);
8582 end if;
8584 -- For real or string subtype, save predicate expression
8586 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8587 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8588 end if;
8590 -- Case of dynamic predicate (expression is not predicate-static)
8592 else
8593 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8594 -- is only set if we have an explicit Dynamic_Predicate aspect
8595 -- given. Here we may simply have a Predicate aspect where the
8596 -- expression happens not to be predicate-static.
8598 -- Emit an error when the predicate is categorized as static
8599 -- but its expression is not predicate-static.
8601 -- First a little fiddling to get a nice location for the
8602 -- message. If the expression is of the form (A and then B),
8603 -- then use the left operand for the Sloc. This avoids getting
8604 -- confused by a call to a higher-level predicate with a less
8605 -- convenient source location.
8607 EN := Expr;
8608 while Nkind (EN) = N_And_Then loop
8609 EN := Left_Opnd (EN);
8610 end loop;
8612 -- Now post appropriate message
8614 if Has_Static_Predicate_Aspect (Typ) then
8615 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8616 Error_Msg_F
8617 ("expression is not predicate-static (RM 3.2.4(16-22))",
8618 EN);
8619 else
8620 Error_Msg_F
8621 ("static predicate requires scalar or string type", EN);
8622 end if;
8623 end if;
8624 end if;
8625 end;
8626 end if;
8627 end Build_Predicate_Functions;
8629 -----------------------------------------
8630 -- Check_Aspect_At_End_Of_Declarations --
8631 -----------------------------------------
8633 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8634 Ent : constant Entity_Id := Entity (ASN);
8635 Ident : constant Node_Id := Identifier (ASN);
8636 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8638 End_Decl_Expr : constant Node_Id := Entity (Ident);
8639 -- Expression to be analyzed at end of declarations
8641 Freeze_Expr : constant Node_Id := Expression (ASN);
8642 -- Expression from call to Check_Aspect_At_Freeze_Point
8644 T : constant Entity_Id := Etype (Freeze_Expr);
8645 -- Type required for preanalyze call
8647 Err : Boolean;
8648 -- Set False if error
8650 -- On entry to this procedure, Entity (Ident) contains a copy of the
8651 -- original expression from the aspect, saved for this purpose, and
8652 -- but Expression (Ident) is a preanalyzed copy of the expression,
8653 -- preanalyzed just after the freeze point.
8655 procedure Check_Overloaded_Name;
8656 -- For aspects whose expression is simply a name, this routine checks if
8657 -- the name is overloaded or not. If so, it verifies there is an
8658 -- interpretation that matches the entity obtained at the freeze point,
8659 -- otherwise the compiler complains.
8661 ---------------------------
8662 -- Check_Overloaded_Name --
8663 ---------------------------
8665 procedure Check_Overloaded_Name is
8666 begin
8667 if not Is_Overloaded (End_Decl_Expr) then
8668 Err := not Is_Entity_Name (End_Decl_Expr)
8669 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8671 else
8672 Err := True;
8674 declare
8675 Index : Interp_Index;
8676 It : Interp;
8678 begin
8679 Get_First_Interp (End_Decl_Expr, Index, It);
8680 while Present (It.Typ) loop
8681 if It.Nam = Entity (Freeze_Expr) then
8682 Err := False;
8683 exit;
8684 end if;
8686 Get_Next_Interp (Index, It);
8687 end loop;
8688 end;
8689 end if;
8690 end Check_Overloaded_Name;
8692 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8694 begin
8695 -- Case of aspects Dimension, Dimension_System and Synchronization
8697 if A_Id = Aspect_Synchronization then
8698 return;
8700 -- Case of stream attributes, just have to compare entities. However,
8701 -- the expression is just a name (possibly overloaded), and there may
8702 -- be stream operations declared for unrelated types, so we just need
8703 -- to verify that one of these interpretations is the one available at
8704 -- at the freeze point.
8706 elsif A_Id = Aspect_Input or else
8707 A_Id = Aspect_Output or else
8708 A_Id = Aspect_Read or else
8709 A_Id = Aspect_Write
8710 then
8711 Analyze (End_Decl_Expr);
8712 Check_Overloaded_Name;
8714 elsif A_Id = Aspect_Variable_Indexing or else
8715 A_Id = Aspect_Constant_Indexing or else
8716 A_Id = Aspect_Default_Iterator or else
8717 A_Id = Aspect_Iterator_Element
8718 then
8719 -- Make type unfrozen before analysis, to prevent spurious errors
8720 -- about late attributes.
8722 Set_Is_Frozen (Ent, False);
8723 Analyze (End_Decl_Expr);
8724 Set_Is_Frozen (Ent, True);
8726 -- If the end of declarations comes before any other freeze
8727 -- point, the Freeze_Expr is not analyzed: no check needed.
8729 if Analyzed (Freeze_Expr) and then not In_Instance then
8730 Check_Overloaded_Name;
8731 else
8732 Err := False;
8733 end if;
8735 -- All other cases
8737 else
8738 -- Indicate that the expression comes from an aspect specification,
8739 -- which is used in subsequent analysis even if expansion is off.
8741 Set_Parent (End_Decl_Expr, ASN);
8743 -- In a generic context the aspect expressions have not been
8744 -- preanalyzed, so do it now. There are no conformance checks
8745 -- to perform in this case.
8747 if No (T) then
8748 Check_Aspect_At_Freeze_Point (ASN);
8749 return;
8751 -- The default values attributes may be defined in the private part,
8752 -- and the analysis of the expression may take place when only the
8753 -- partial view is visible. The expression must be scalar, so use
8754 -- the full view to resolve.
8756 elsif (A_Id = Aspect_Default_Value
8757 or else
8758 A_Id = Aspect_Default_Component_Value)
8759 and then Is_Private_Type (T)
8760 then
8761 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8763 else
8764 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8765 end if;
8767 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8768 end if;
8770 -- Output error message if error. Force error on aspect specification
8771 -- even if there is an error on the expression itself.
8773 if Err then
8774 Error_Msg_NE
8775 ("!visibility of aspect for& changes after freeze point",
8776 ASN, Ent);
8777 Error_Msg_NE
8778 ("info: & is frozen here, aspects evaluated at this point??",
8779 Freeze_Node (Ent), Ent);
8780 end if;
8781 end Check_Aspect_At_End_Of_Declarations;
8783 ----------------------------------
8784 -- Check_Aspect_At_Freeze_Point --
8785 ----------------------------------
8787 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8788 Ident : constant Node_Id := Identifier (ASN);
8789 -- Identifier (use Entity field to save expression)
8791 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8793 T : Entity_Id := Empty;
8794 -- Type required for preanalyze call
8796 begin
8797 -- On entry to this procedure, Entity (Ident) contains a copy of the
8798 -- original expression from the aspect, saved for this purpose.
8800 -- On exit from this procedure Entity (Ident) is unchanged, still
8801 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8802 -- of the expression, preanalyzed just after the freeze point.
8804 -- Make a copy of the expression to be preanalyzed
8806 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8808 -- Find type for preanalyze call
8810 case A_Id is
8812 -- No_Aspect should be impossible
8814 when No_Aspect =>
8815 raise Program_Error;
8817 -- Aspects taking an optional boolean argument
8819 when Boolean_Aspects |
8820 Library_Unit_Aspects =>
8822 T := Standard_Boolean;
8824 -- Aspects corresponding to attribute definition clauses
8826 when Aspect_Address =>
8827 T := RTE (RE_Address);
8829 when Aspect_Attach_Handler =>
8830 T := RTE (RE_Interrupt_ID);
8832 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8833 T := RTE (RE_Bit_Order);
8835 when Aspect_Convention =>
8836 return;
8838 when Aspect_CPU =>
8839 T := RTE (RE_CPU_Range);
8841 -- Default_Component_Value is resolved with the component type
8843 when Aspect_Default_Component_Value =>
8844 T := Component_Type (Entity (ASN));
8846 when Aspect_Default_Storage_Pool =>
8847 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8849 -- Default_Value is resolved with the type entity in question
8851 when Aspect_Default_Value =>
8852 T := Entity (ASN);
8854 when Aspect_Dispatching_Domain =>
8855 T := RTE (RE_Dispatching_Domain);
8857 when Aspect_External_Tag =>
8858 T := Standard_String;
8860 when Aspect_External_Name =>
8861 T := Standard_String;
8863 when Aspect_Link_Name =>
8864 T := Standard_String;
8866 when Aspect_Priority | Aspect_Interrupt_Priority =>
8867 T := Standard_Integer;
8869 when Aspect_Relative_Deadline =>
8870 T := RTE (RE_Time_Span);
8872 when Aspect_Small =>
8873 T := Universal_Real;
8875 -- For a simple storage pool, we have to retrieve the type of the
8876 -- pool object associated with the aspect's corresponding attribute
8877 -- definition clause.
8879 when Aspect_Simple_Storage_Pool =>
8880 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8882 when Aspect_Storage_Pool =>
8883 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8885 when Aspect_Alignment |
8886 Aspect_Component_Size |
8887 Aspect_Machine_Radix |
8888 Aspect_Object_Size |
8889 Aspect_Size |
8890 Aspect_Storage_Size |
8891 Aspect_Stream_Size |
8892 Aspect_Value_Size =>
8893 T := Any_Integer;
8895 when Aspect_Linker_Section =>
8896 T := Standard_String;
8898 when Aspect_Synchronization =>
8899 return;
8901 -- Special case, the expression of these aspects is just an entity
8902 -- that does not need any resolution, so just analyze.
8904 when Aspect_Input |
8905 Aspect_Output |
8906 Aspect_Read |
8907 Aspect_Suppress |
8908 Aspect_Unsuppress |
8909 Aspect_Warnings |
8910 Aspect_Write =>
8911 Analyze (Expression (ASN));
8912 return;
8914 -- Same for Iterator aspects, where the expression is a function
8915 -- name. Legality rules are checked separately.
8917 when Aspect_Constant_Indexing |
8918 Aspect_Default_Iterator |
8919 Aspect_Iterator_Element |
8920 Aspect_Variable_Indexing =>
8921 Analyze (Expression (ASN));
8922 return;
8924 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8926 when Aspect_Iterable =>
8927 T := Entity (ASN);
8929 declare
8930 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8931 Assoc : Node_Id;
8932 Expr : Node_Id;
8934 begin
8935 if Cursor = Any_Type then
8936 return;
8937 end if;
8939 Assoc := First (Component_Associations (Expression (ASN)));
8940 while Present (Assoc) loop
8941 Expr := Expression (Assoc);
8942 Analyze (Expr);
8944 if not Error_Posted (Expr) then
8945 Resolve_Iterable_Operation
8946 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8947 end if;
8949 Next (Assoc);
8950 end loop;
8951 end;
8953 return;
8955 -- Invariant/Predicate take boolean expressions
8957 when Aspect_Dynamic_Predicate |
8958 Aspect_Invariant |
8959 Aspect_Predicate |
8960 Aspect_Static_Predicate |
8961 Aspect_Type_Invariant =>
8962 T := Standard_Boolean;
8964 -- Here is the list of aspects that don't require delay analysis
8966 when Aspect_Abstract_State |
8967 Aspect_Annotate |
8968 Aspect_Contract_Cases |
8969 Aspect_Default_Initial_Condition |
8970 Aspect_Depends |
8971 Aspect_Dimension |
8972 Aspect_Dimension_System |
8973 Aspect_Extensions_Visible |
8974 Aspect_Ghost |
8975 Aspect_Global |
8976 Aspect_Implicit_Dereference |
8977 Aspect_Initial_Condition |
8978 Aspect_Initializes |
8979 Aspect_Obsolescent |
8980 Aspect_Part_Of |
8981 Aspect_Post |
8982 Aspect_Postcondition |
8983 Aspect_Pre |
8984 Aspect_Precondition |
8985 Aspect_Refined_Depends |
8986 Aspect_Refined_Global |
8987 Aspect_Refined_Post |
8988 Aspect_Refined_State |
8989 Aspect_SPARK_Mode |
8990 Aspect_Test_Case =>
8991 raise Program_Error;
8993 end case;
8995 -- Do the preanalyze call
8997 Preanalyze_Spec_Expression (Expression (ASN), T);
8998 end Check_Aspect_At_Freeze_Point;
9000 -----------------------------------
9001 -- Check_Constant_Address_Clause --
9002 -----------------------------------
9004 procedure Check_Constant_Address_Clause
9005 (Expr : Node_Id;
9006 U_Ent : Entity_Id)
9008 procedure Check_At_Constant_Address (Nod : Node_Id);
9009 -- Checks that the given node N represents a name whose 'Address is
9010 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9011 -- address value is the same at the point of declaration of U_Ent and at
9012 -- the time of elaboration of the address clause.
9014 procedure Check_Expr_Constants (Nod : Node_Id);
9015 -- Checks that Nod meets the requirements for a constant address clause
9016 -- in the sense of the enclosing procedure.
9018 procedure Check_List_Constants (Lst : List_Id);
9019 -- Check that all elements of list Lst meet the requirements for a
9020 -- constant address clause in the sense of the enclosing procedure.
9022 -------------------------------
9023 -- Check_At_Constant_Address --
9024 -------------------------------
9026 procedure Check_At_Constant_Address (Nod : Node_Id) is
9027 begin
9028 if Is_Entity_Name (Nod) then
9029 if Present (Address_Clause (Entity ((Nod)))) then
9030 Error_Msg_NE
9031 ("invalid address clause for initialized object &!",
9032 Nod, U_Ent);
9033 Error_Msg_NE
9034 ("address for& cannot" &
9035 " depend on another address clause! (RM 13.1(22))!",
9036 Nod, U_Ent);
9038 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9039 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9040 then
9041 Error_Msg_NE
9042 ("invalid address clause for initialized object &!",
9043 Nod, U_Ent);
9044 Error_Msg_Node_2 := U_Ent;
9045 Error_Msg_NE
9046 ("\& must be defined before & (RM 13.1(22))!",
9047 Nod, Entity (Nod));
9048 end if;
9050 elsif Nkind (Nod) = N_Selected_Component then
9051 declare
9052 T : constant Entity_Id := Etype (Prefix (Nod));
9054 begin
9055 if (Is_Record_Type (T)
9056 and then Has_Discriminants (T))
9057 or else
9058 (Is_Access_Type (T)
9059 and then Is_Record_Type (Designated_Type (T))
9060 and then Has_Discriminants (Designated_Type (T)))
9061 then
9062 Error_Msg_NE
9063 ("invalid address clause for initialized object &!",
9064 Nod, U_Ent);
9065 Error_Msg_N
9066 ("\address cannot depend on component" &
9067 " of discriminated record (RM 13.1(22))!",
9068 Nod);
9069 else
9070 Check_At_Constant_Address (Prefix (Nod));
9071 end if;
9072 end;
9074 elsif Nkind (Nod) = N_Indexed_Component then
9075 Check_At_Constant_Address (Prefix (Nod));
9076 Check_List_Constants (Expressions (Nod));
9078 else
9079 Check_Expr_Constants (Nod);
9080 end if;
9081 end Check_At_Constant_Address;
9083 --------------------------
9084 -- Check_Expr_Constants --
9085 --------------------------
9087 procedure Check_Expr_Constants (Nod : Node_Id) is
9088 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9089 Ent : Entity_Id := Empty;
9091 begin
9092 if Nkind (Nod) in N_Has_Etype
9093 and then Etype (Nod) = Any_Type
9094 then
9095 return;
9096 end if;
9098 case Nkind (Nod) is
9099 when N_Empty | N_Error =>
9100 return;
9102 when N_Identifier | N_Expanded_Name =>
9103 Ent := Entity (Nod);
9105 -- We need to look at the original node if it is different
9106 -- from the node, since we may have rewritten things and
9107 -- substituted an identifier representing the rewrite.
9109 if Original_Node (Nod) /= Nod then
9110 Check_Expr_Constants (Original_Node (Nod));
9112 -- If the node is an object declaration without initial
9113 -- value, some code has been expanded, and the expression
9114 -- is not constant, even if the constituents might be
9115 -- acceptable, as in A'Address + offset.
9117 if Ekind (Ent) = E_Variable
9118 and then
9119 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9120 and then
9121 No (Expression (Declaration_Node (Ent)))
9122 then
9123 Error_Msg_NE
9124 ("invalid address clause for initialized object &!",
9125 Nod, U_Ent);
9127 -- If entity is constant, it may be the result of expanding
9128 -- a check. We must verify that its declaration appears
9129 -- before the object in question, else we also reject the
9130 -- address clause.
9132 elsif Ekind (Ent) = E_Constant
9133 and then In_Same_Source_Unit (Ent, U_Ent)
9134 and then Sloc (Ent) > Loc_U_Ent
9135 then
9136 Error_Msg_NE
9137 ("invalid address clause for initialized object &!",
9138 Nod, U_Ent);
9139 end if;
9141 return;
9142 end if;
9144 -- Otherwise look at the identifier and see if it is OK
9146 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9147 or else Is_Type (Ent)
9148 then
9149 return;
9151 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9153 -- This is the case where we must have Ent defined before
9154 -- U_Ent. Clearly if they are in different units this
9155 -- requirement is met since the unit containing Ent is
9156 -- already processed.
9158 if not In_Same_Source_Unit (Ent, U_Ent) then
9159 return;
9161 -- Otherwise location of Ent must be before the location
9162 -- of U_Ent, that's what prior defined means.
9164 elsif Sloc (Ent) < Loc_U_Ent then
9165 return;
9167 else
9168 Error_Msg_NE
9169 ("invalid address clause for initialized object &!",
9170 Nod, U_Ent);
9171 Error_Msg_Node_2 := U_Ent;
9172 Error_Msg_NE
9173 ("\& must be defined before & (RM 13.1(22))!",
9174 Nod, Ent);
9175 end if;
9177 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9178 Check_Expr_Constants (Original_Node (Nod));
9180 else
9181 Error_Msg_NE
9182 ("invalid address clause for initialized object &!",
9183 Nod, U_Ent);
9185 if Comes_From_Source (Ent) then
9186 Error_Msg_NE
9187 ("\reference to variable& not allowed"
9188 & " (RM 13.1(22))!", Nod, Ent);
9189 else
9190 Error_Msg_N
9191 ("non-static expression not allowed"
9192 & " (RM 13.1(22))!", Nod);
9193 end if;
9194 end if;
9196 when N_Integer_Literal =>
9198 -- If this is a rewritten unchecked conversion, in a system
9199 -- where Address is an integer type, always use the base type
9200 -- for a literal value. This is user-friendly and prevents
9201 -- order-of-elaboration issues with instances of unchecked
9202 -- conversion.
9204 if Nkind (Original_Node (Nod)) = N_Function_Call then
9205 Set_Etype (Nod, Base_Type (Etype (Nod)));
9206 end if;
9208 when N_Real_Literal |
9209 N_String_Literal |
9210 N_Character_Literal =>
9211 return;
9213 when N_Range =>
9214 Check_Expr_Constants (Low_Bound (Nod));
9215 Check_Expr_Constants (High_Bound (Nod));
9217 when N_Explicit_Dereference =>
9218 Check_Expr_Constants (Prefix (Nod));
9220 when N_Indexed_Component =>
9221 Check_Expr_Constants (Prefix (Nod));
9222 Check_List_Constants (Expressions (Nod));
9224 when N_Slice =>
9225 Check_Expr_Constants (Prefix (Nod));
9226 Check_Expr_Constants (Discrete_Range (Nod));
9228 when N_Selected_Component =>
9229 Check_Expr_Constants (Prefix (Nod));
9231 when N_Attribute_Reference =>
9232 if Nam_In (Attribute_Name (Nod), Name_Address,
9233 Name_Access,
9234 Name_Unchecked_Access,
9235 Name_Unrestricted_Access)
9236 then
9237 Check_At_Constant_Address (Prefix (Nod));
9239 else
9240 Check_Expr_Constants (Prefix (Nod));
9241 Check_List_Constants (Expressions (Nod));
9242 end if;
9244 when N_Aggregate =>
9245 Check_List_Constants (Component_Associations (Nod));
9246 Check_List_Constants (Expressions (Nod));
9248 when N_Component_Association =>
9249 Check_Expr_Constants (Expression (Nod));
9251 when N_Extension_Aggregate =>
9252 Check_Expr_Constants (Ancestor_Part (Nod));
9253 Check_List_Constants (Component_Associations (Nod));
9254 Check_List_Constants (Expressions (Nod));
9256 when N_Null =>
9257 return;
9259 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9260 Check_Expr_Constants (Left_Opnd (Nod));
9261 Check_Expr_Constants (Right_Opnd (Nod));
9263 when N_Unary_Op =>
9264 Check_Expr_Constants (Right_Opnd (Nod));
9266 when N_Type_Conversion |
9267 N_Qualified_Expression |
9268 N_Allocator |
9269 N_Unchecked_Type_Conversion =>
9270 Check_Expr_Constants (Expression (Nod));
9272 when N_Function_Call =>
9273 if not Is_Pure (Entity (Name (Nod))) then
9274 Error_Msg_NE
9275 ("invalid address clause for initialized object &!",
9276 Nod, U_Ent);
9278 Error_Msg_NE
9279 ("\function & is not pure (RM 13.1(22))!",
9280 Nod, Entity (Name (Nod)));
9282 else
9283 Check_List_Constants (Parameter_Associations (Nod));
9284 end if;
9286 when N_Parameter_Association =>
9287 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9289 when others =>
9290 Error_Msg_NE
9291 ("invalid address clause for initialized object &!",
9292 Nod, U_Ent);
9293 Error_Msg_NE
9294 ("\must be constant defined before& (RM 13.1(22))!",
9295 Nod, U_Ent);
9296 end case;
9297 end Check_Expr_Constants;
9299 --------------------------
9300 -- Check_List_Constants --
9301 --------------------------
9303 procedure Check_List_Constants (Lst : List_Id) is
9304 Nod1 : Node_Id;
9306 begin
9307 if Present (Lst) then
9308 Nod1 := First (Lst);
9309 while Present (Nod1) loop
9310 Check_Expr_Constants (Nod1);
9311 Next (Nod1);
9312 end loop;
9313 end if;
9314 end Check_List_Constants;
9316 -- Start of processing for Check_Constant_Address_Clause
9318 begin
9319 -- If rep_clauses are to be ignored, no need for legality checks. In
9320 -- particular, no need to pester user about rep clauses that violate the
9321 -- rule on constant addresses, given that these clauses will be removed
9322 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9323 -- we want to relax these checks.
9325 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9326 Check_Expr_Constants (Expr);
9327 end if;
9328 end Check_Constant_Address_Clause;
9330 ---------------------------
9331 -- Check_Pool_Size_Clash --
9332 ---------------------------
9334 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9335 Post : Node_Id;
9337 begin
9338 -- We need to find out which one came first. Note that in the case of
9339 -- aspects mixed with pragmas there are cases where the processing order
9340 -- is reversed, which is why we do the check here.
9342 if Sloc (SP) < Sloc (SS) then
9343 Error_Msg_Sloc := Sloc (SP);
9344 Post := SS;
9345 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9347 else
9348 Error_Msg_Sloc := Sloc (SS);
9349 Post := SP;
9350 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9351 end if;
9353 Error_Msg_N
9354 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9355 end Check_Pool_Size_Clash;
9357 ----------------------------------------
9358 -- Check_Record_Representation_Clause --
9359 ----------------------------------------
9361 procedure Check_Record_Representation_Clause (N : Node_Id) is
9362 Loc : constant Source_Ptr := Sloc (N);
9363 Ident : constant Node_Id := Identifier (N);
9364 Rectype : Entity_Id;
9365 Fent : Entity_Id;
9366 CC : Node_Id;
9367 Fbit : Uint;
9368 Lbit : Uint;
9369 Hbit : Uint := Uint_0;
9370 Comp : Entity_Id;
9371 Pcomp : Entity_Id;
9373 Max_Bit_So_Far : Uint;
9374 -- Records the maximum bit position so far. If all field positions
9375 -- are monotonically increasing, then we can skip the circuit for
9376 -- checking for overlap, since no overlap is possible.
9378 Tagged_Parent : Entity_Id := Empty;
9379 -- This is set in the case of a derived tagged type for which we have
9380 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9381 -- positioned by record representation clauses). In this case we must
9382 -- check for overlap between components of this tagged type, and the
9383 -- components of its parent. Tagged_Parent will point to this parent
9384 -- type. For all other cases Tagged_Parent is left set to Empty.
9386 Parent_Last_Bit : Uint;
9387 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9388 -- last bit position for any field in the parent type. We only need to
9389 -- check overlap for fields starting below this point.
9391 Overlap_Check_Required : Boolean;
9392 -- Used to keep track of whether or not an overlap check is required
9394 Overlap_Detected : Boolean := False;
9395 -- Set True if an overlap is detected
9397 Ccount : Natural := 0;
9398 -- Number of component clauses in record rep clause
9400 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9401 -- Given two entities for record components or discriminants, checks
9402 -- if they have overlapping component clauses and issues errors if so.
9404 procedure Find_Component;
9405 -- Finds component entity corresponding to current component clause (in
9406 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9407 -- start/stop bits for the field. If there is no matching component or
9408 -- if the matching component does not have a component clause, then
9409 -- that's an error and Comp is set to Empty, but no error message is
9410 -- issued, since the message was already given. Comp is also set to
9411 -- Empty if the current "component clause" is in fact a pragma.
9413 -----------------------------
9414 -- Check_Component_Overlap --
9415 -----------------------------
9417 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9418 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9419 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9421 begin
9422 if Present (CC1) and then Present (CC2) then
9424 -- Exclude odd case where we have two tag components in the same
9425 -- record, both at location zero. This seems a bit strange, but
9426 -- it seems to happen in some circumstances, perhaps on an error.
9428 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9429 return;
9430 end if;
9432 -- Here we check if the two fields overlap
9434 declare
9435 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9436 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9437 E1 : constant Uint := S1 + Esize (C1_Ent);
9438 E2 : constant Uint := S2 + Esize (C2_Ent);
9440 begin
9441 if E2 <= S1 or else E1 <= S2 then
9442 null;
9443 else
9444 Error_Msg_Node_2 := Component_Name (CC2);
9445 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9446 Error_Msg_Node_1 := Component_Name (CC1);
9447 Error_Msg_N
9448 ("component& overlaps & #", Component_Name (CC1));
9449 Overlap_Detected := True;
9450 end if;
9451 end;
9452 end if;
9453 end Check_Component_Overlap;
9455 --------------------
9456 -- Find_Component --
9457 --------------------
9459 procedure Find_Component is
9461 procedure Search_Component (R : Entity_Id);
9462 -- Search components of R for a match. If found, Comp is set
9464 ----------------------
9465 -- Search_Component --
9466 ----------------------
9468 procedure Search_Component (R : Entity_Id) is
9469 begin
9470 Comp := First_Component_Or_Discriminant (R);
9471 while Present (Comp) loop
9473 -- Ignore error of attribute name for component name (we
9474 -- already gave an error message for this, so no need to
9475 -- complain here)
9477 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9478 null;
9479 else
9480 exit when Chars (Comp) = Chars (Component_Name (CC));
9481 end if;
9483 Next_Component_Or_Discriminant (Comp);
9484 end loop;
9485 end Search_Component;
9487 -- Start of processing for Find_Component
9489 begin
9490 -- Return with Comp set to Empty if we have a pragma
9492 if Nkind (CC) = N_Pragma then
9493 Comp := Empty;
9494 return;
9495 end if;
9497 -- Search current record for matching component
9499 Search_Component (Rectype);
9501 -- If not found, maybe component of base type discriminant that is
9502 -- absent from statically constrained first subtype.
9504 if No (Comp) then
9505 Search_Component (Base_Type (Rectype));
9506 end if;
9508 -- If no component, or the component does not reference the component
9509 -- clause in question, then there was some previous error for which
9510 -- we already gave a message, so just return with Comp Empty.
9512 if No (Comp) or else Component_Clause (Comp) /= CC then
9513 Check_Error_Detected;
9514 Comp := Empty;
9516 -- Normal case where we have a component clause
9518 else
9519 Fbit := Component_Bit_Offset (Comp);
9520 Lbit := Fbit + Esize (Comp) - 1;
9521 end if;
9522 end Find_Component;
9524 -- Start of processing for Check_Record_Representation_Clause
9526 begin
9527 Find_Type (Ident);
9528 Rectype := Entity (Ident);
9530 if Rectype = Any_Type then
9531 return;
9532 else
9533 Rectype := Underlying_Type (Rectype);
9534 end if;
9536 -- See if we have a fully repped derived tagged type
9538 declare
9539 PS : constant Entity_Id := Parent_Subtype (Rectype);
9541 begin
9542 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9543 Tagged_Parent := PS;
9545 -- Find maximum bit of any component of the parent type
9547 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9548 Pcomp := First_Entity (Tagged_Parent);
9549 while Present (Pcomp) loop
9550 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9551 if Component_Bit_Offset (Pcomp) /= No_Uint
9552 and then Known_Static_Esize (Pcomp)
9553 then
9554 Parent_Last_Bit :=
9555 UI_Max
9556 (Parent_Last_Bit,
9557 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9558 end if;
9560 Next_Entity (Pcomp);
9561 end if;
9562 end loop;
9563 end if;
9564 end;
9566 -- All done if no component clauses
9568 CC := First (Component_Clauses (N));
9570 if No (CC) then
9571 return;
9572 end if;
9574 -- If a tag is present, then create a component clause that places it
9575 -- at the start of the record (otherwise gigi may place it after other
9576 -- fields that have rep clauses).
9578 Fent := First_Entity (Rectype);
9580 if Nkind (Fent) = N_Defining_Identifier
9581 and then Chars (Fent) = Name_uTag
9582 then
9583 Set_Component_Bit_Offset (Fent, Uint_0);
9584 Set_Normalized_Position (Fent, Uint_0);
9585 Set_Normalized_First_Bit (Fent, Uint_0);
9586 Set_Normalized_Position_Max (Fent, Uint_0);
9587 Init_Esize (Fent, System_Address_Size);
9589 Set_Component_Clause (Fent,
9590 Make_Component_Clause (Loc,
9591 Component_Name => Make_Identifier (Loc, Name_uTag),
9593 Position => Make_Integer_Literal (Loc, Uint_0),
9594 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9595 Last_Bit =>
9596 Make_Integer_Literal (Loc,
9597 UI_From_Int (System_Address_Size))));
9599 Ccount := Ccount + 1;
9600 end if;
9602 Max_Bit_So_Far := Uint_Minus_1;
9603 Overlap_Check_Required := False;
9605 -- Process the component clauses
9607 while Present (CC) loop
9608 Find_Component;
9610 if Present (Comp) then
9611 Ccount := Ccount + 1;
9613 -- We need a full overlap check if record positions non-monotonic
9615 if Fbit <= Max_Bit_So_Far then
9616 Overlap_Check_Required := True;
9617 end if;
9619 Max_Bit_So_Far := Lbit;
9621 -- Check bit position out of range of specified size
9623 if Has_Size_Clause (Rectype)
9624 and then RM_Size (Rectype) <= Lbit
9625 then
9626 Error_Msg_N
9627 ("bit number out of range of specified size",
9628 Last_Bit (CC));
9630 -- Check for overlap with tag component
9632 else
9633 if Is_Tagged_Type (Rectype)
9634 and then Fbit < System_Address_Size
9635 then
9636 Error_Msg_NE
9637 ("component overlaps tag field of&",
9638 Component_Name (CC), Rectype);
9639 Overlap_Detected := True;
9640 end if;
9642 if Hbit < Lbit then
9643 Hbit := Lbit;
9644 end if;
9645 end if;
9647 -- Check parent overlap if component might overlap parent field
9649 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9650 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9651 while Present (Pcomp) loop
9652 if not Is_Tag (Pcomp)
9653 and then Chars (Pcomp) /= Name_uParent
9654 then
9655 Check_Component_Overlap (Comp, Pcomp);
9656 end if;
9658 Next_Component_Or_Discriminant (Pcomp);
9659 end loop;
9660 end if;
9661 end if;
9663 Next (CC);
9664 end loop;
9666 -- Now that we have processed all the component clauses, check for
9667 -- overlap. We have to leave this till last, since the components can
9668 -- appear in any arbitrary order in the representation clause.
9670 -- We do not need this check if all specified ranges were monotonic,
9671 -- as recorded by Overlap_Check_Required being False at this stage.
9673 -- This first section checks if there are any overlapping entries at
9674 -- all. It does this by sorting all entries and then seeing if there are
9675 -- any overlaps. If there are none, then that is decisive, but if there
9676 -- are overlaps, they may still be OK (they may result from fields in
9677 -- different variants).
9679 if Overlap_Check_Required then
9680 Overlap_Check1 : declare
9682 OC_Fbit : array (0 .. Ccount) of Uint;
9683 -- First-bit values for component clauses, the value is the offset
9684 -- of the first bit of the field from start of record. The zero
9685 -- entry is for use in sorting.
9687 OC_Lbit : array (0 .. Ccount) of Uint;
9688 -- Last-bit values for component clauses, the value is the offset
9689 -- of the last bit of the field from start of record. The zero
9690 -- entry is for use in sorting.
9692 OC_Count : Natural := 0;
9693 -- Count of entries in OC_Fbit and OC_Lbit
9695 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9696 -- Compare routine for Sort
9698 procedure OC_Move (From : Natural; To : Natural);
9699 -- Move routine for Sort
9701 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9703 -----------
9704 -- OC_Lt --
9705 -----------
9707 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9708 begin
9709 return OC_Fbit (Op1) < OC_Fbit (Op2);
9710 end OC_Lt;
9712 -------------
9713 -- OC_Move --
9714 -------------
9716 procedure OC_Move (From : Natural; To : Natural) is
9717 begin
9718 OC_Fbit (To) := OC_Fbit (From);
9719 OC_Lbit (To) := OC_Lbit (From);
9720 end OC_Move;
9722 -- Start of processing for Overlap_Check
9724 begin
9725 CC := First (Component_Clauses (N));
9726 while Present (CC) loop
9728 -- Exclude component clause already marked in error
9730 if not Error_Posted (CC) then
9731 Find_Component;
9733 if Present (Comp) then
9734 OC_Count := OC_Count + 1;
9735 OC_Fbit (OC_Count) := Fbit;
9736 OC_Lbit (OC_Count) := Lbit;
9737 end if;
9738 end if;
9740 Next (CC);
9741 end loop;
9743 Sorting.Sort (OC_Count);
9745 Overlap_Check_Required := False;
9746 for J in 1 .. OC_Count - 1 loop
9747 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9748 Overlap_Check_Required := True;
9749 exit;
9750 end if;
9751 end loop;
9752 end Overlap_Check1;
9753 end if;
9755 -- If Overlap_Check_Required is still True, then we have to do the full
9756 -- scale overlap check, since we have at least two fields that do
9757 -- overlap, and we need to know if that is OK since they are in
9758 -- different variant, or whether we have a definite problem.
9760 if Overlap_Check_Required then
9761 Overlap_Check2 : declare
9762 C1_Ent, C2_Ent : Entity_Id;
9763 -- Entities of components being checked for overlap
9765 Clist : Node_Id;
9766 -- Component_List node whose Component_Items are being checked
9768 Citem : Node_Id;
9769 -- Component declaration for component being checked
9771 begin
9772 C1_Ent := First_Entity (Base_Type (Rectype));
9774 -- Loop through all components in record. For each component check
9775 -- for overlap with any of the preceding elements on the component
9776 -- list containing the component and also, if the component is in
9777 -- a variant, check against components outside the case structure.
9778 -- This latter test is repeated recursively up the variant tree.
9780 Main_Component_Loop : while Present (C1_Ent) loop
9781 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9782 goto Continue_Main_Component_Loop;
9783 end if;
9785 -- Skip overlap check if entity has no declaration node. This
9786 -- happens with discriminants in constrained derived types.
9787 -- Possibly we are missing some checks as a result, but that
9788 -- does not seem terribly serious.
9790 if No (Declaration_Node (C1_Ent)) then
9791 goto Continue_Main_Component_Loop;
9792 end if;
9794 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9796 -- Loop through component lists that need checking. Check the
9797 -- current component list and all lists in variants above us.
9799 Component_List_Loop : loop
9801 -- If derived type definition, go to full declaration
9802 -- If at outer level, check discriminants if there are any.
9804 if Nkind (Clist) = N_Derived_Type_Definition then
9805 Clist := Parent (Clist);
9806 end if;
9808 -- Outer level of record definition, check discriminants
9810 if Nkind_In (Clist, N_Full_Type_Declaration,
9811 N_Private_Type_Declaration)
9812 then
9813 if Has_Discriminants (Defining_Identifier (Clist)) then
9814 C2_Ent :=
9815 First_Discriminant (Defining_Identifier (Clist));
9816 while Present (C2_Ent) loop
9817 exit when C1_Ent = C2_Ent;
9818 Check_Component_Overlap (C1_Ent, C2_Ent);
9819 Next_Discriminant (C2_Ent);
9820 end loop;
9821 end if;
9823 -- Record extension case
9825 elsif Nkind (Clist) = N_Derived_Type_Definition then
9826 Clist := Empty;
9828 -- Otherwise check one component list
9830 else
9831 Citem := First (Component_Items (Clist));
9832 while Present (Citem) loop
9833 if Nkind (Citem) = N_Component_Declaration then
9834 C2_Ent := Defining_Identifier (Citem);
9835 exit when C1_Ent = C2_Ent;
9836 Check_Component_Overlap (C1_Ent, C2_Ent);
9837 end if;
9839 Next (Citem);
9840 end loop;
9841 end if;
9843 -- Check for variants above us (the parent of the Clist can
9844 -- be a variant, in which case its parent is a variant part,
9845 -- and the parent of the variant part is a component list
9846 -- whose components must all be checked against the current
9847 -- component for overlap).
9849 if Nkind (Parent (Clist)) = N_Variant then
9850 Clist := Parent (Parent (Parent (Clist)));
9852 -- Check for possible discriminant part in record, this
9853 -- is treated essentially as another level in the
9854 -- recursion. For this case the parent of the component
9855 -- list is the record definition, and its parent is the
9856 -- full type declaration containing the discriminant
9857 -- specifications.
9859 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9860 Clist := Parent (Parent ((Clist)));
9862 -- If neither of these two cases, we are at the top of
9863 -- the tree.
9865 else
9866 exit Component_List_Loop;
9867 end if;
9868 end loop Component_List_Loop;
9870 <<Continue_Main_Component_Loop>>
9871 Next_Entity (C1_Ent);
9873 end loop Main_Component_Loop;
9874 end Overlap_Check2;
9875 end if;
9877 -- The following circuit deals with warning on record holes (gaps). We
9878 -- skip this check if overlap was detected, since it makes sense for the
9879 -- programmer to fix this illegality before worrying about warnings.
9881 if not Overlap_Detected and Warn_On_Record_Holes then
9882 Record_Hole_Check : declare
9883 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9884 -- Full declaration of record type
9886 procedure Check_Component_List
9887 (CL : Node_Id;
9888 Sbit : Uint;
9889 DS : List_Id);
9890 -- Check component list CL for holes. The starting bit should be
9891 -- Sbit. which is zero for the main record component list and set
9892 -- appropriately for recursive calls for variants. DS is set to
9893 -- a list of discriminant specifications to be included in the
9894 -- consideration of components. It is No_List if none to consider.
9896 --------------------------
9897 -- Check_Component_List --
9898 --------------------------
9900 procedure Check_Component_List
9901 (CL : Node_Id;
9902 Sbit : Uint;
9903 DS : List_Id)
9905 Compl : Integer;
9907 begin
9908 Compl := Integer (List_Length (Component_Items (CL)));
9910 if DS /= No_List then
9911 Compl := Compl + Integer (List_Length (DS));
9912 end if;
9914 declare
9915 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9916 -- Gather components (zero entry is for sort routine)
9918 Ncomps : Natural := 0;
9919 -- Number of entries stored in Comps (starting at Comps (1))
9921 Citem : Node_Id;
9922 -- One component item or discriminant specification
9924 Nbit : Uint;
9925 -- Starting bit for next component
9927 CEnt : Entity_Id;
9928 -- Component entity
9930 Variant : Node_Id;
9931 -- One variant
9933 function Lt (Op1, Op2 : Natural) return Boolean;
9934 -- Compare routine for Sort
9936 procedure Move (From : Natural; To : Natural);
9937 -- Move routine for Sort
9939 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9941 --------
9942 -- Lt --
9943 --------
9945 function Lt (Op1, Op2 : Natural) return Boolean is
9946 begin
9947 return Component_Bit_Offset (Comps (Op1))
9949 Component_Bit_Offset (Comps (Op2));
9950 end Lt;
9952 ----------
9953 -- Move --
9954 ----------
9956 procedure Move (From : Natural; To : Natural) is
9957 begin
9958 Comps (To) := Comps (From);
9959 end Move;
9961 begin
9962 -- Gather discriminants into Comp
9964 if DS /= No_List then
9965 Citem := First (DS);
9966 while Present (Citem) loop
9967 if Nkind (Citem) = N_Discriminant_Specification then
9968 declare
9969 Ent : constant Entity_Id :=
9970 Defining_Identifier (Citem);
9971 begin
9972 if Ekind (Ent) = E_Discriminant then
9973 Ncomps := Ncomps + 1;
9974 Comps (Ncomps) := Ent;
9975 end if;
9976 end;
9977 end if;
9979 Next (Citem);
9980 end loop;
9981 end if;
9983 -- Gather component entities into Comp
9985 Citem := First (Component_Items (CL));
9986 while Present (Citem) loop
9987 if Nkind (Citem) = N_Component_Declaration then
9988 Ncomps := Ncomps + 1;
9989 Comps (Ncomps) := Defining_Identifier (Citem);
9990 end if;
9992 Next (Citem);
9993 end loop;
9995 -- Now sort the component entities based on the first bit.
9996 -- Note we already know there are no overlapping components.
9998 Sorting.Sort (Ncomps);
10000 -- Loop through entries checking for holes
10002 Nbit := Sbit;
10003 for J in 1 .. Ncomps loop
10004 CEnt := Comps (J);
10005 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
10007 if Error_Msg_Uint_1 > 0 then
10008 Error_Msg_NE
10009 ("?H?^-bit gap before component&",
10010 Component_Name (Component_Clause (CEnt)), CEnt);
10011 end if;
10013 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10014 end loop;
10016 -- Process variant parts recursively if present
10018 if Present (Variant_Part (CL)) then
10019 Variant := First (Variants (Variant_Part (CL)));
10020 while Present (Variant) loop
10021 Check_Component_List
10022 (Component_List (Variant), Nbit, No_List);
10023 Next (Variant);
10024 end loop;
10025 end if;
10026 end;
10027 end Check_Component_List;
10029 -- Start of processing for Record_Hole_Check
10031 begin
10032 declare
10033 Sbit : Uint;
10035 begin
10036 if Is_Tagged_Type (Rectype) then
10037 Sbit := UI_From_Int (System_Address_Size);
10038 else
10039 Sbit := Uint_0;
10040 end if;
10042 if Nkind (Decl) = N_Full_Type_Declaration
10043 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10044 then
10045 Check_Component_List
10046 (Component_List (Type_Definition (Decl)),
10047 Sbit,
10048 Discriminant_Specifications (Decl));
10049 end if;
10050 end;
10051 end Record_Hole_Check;
10052 end if;
10054 -- For records that have component clauses for all components, and whose
10055 -- size is less than or equal to 32, we need to know the size in the
10056 -- front end to activate possible packed array processing where the
10057 -- component type is a record.
10059 -- At this stage Hbit + 1 represents the first unused bit from all the
10060 -- component clauses processed, so if the component clauses are
10061 -- complete, then this is the length of the record.
10063 -- For records longer than System.Storage_Unit, and for those where not
10064 -- all components have component clauses, the back end determines the
10065 -- length (it may for example be appropriate to round up the size
10066 -- to some convenient boundary, based on alignment considerations, etc).
10068 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10070 -- Nothing to do if at least one component has no component clause
10072 Comp := First_Component_Or_Discriminant (Rectype);
10073 while Present (Comp) loop
10074 exit when No (Component_Clause (Comp));
10075 Next_Component_Or_Discriminant (Comp);
10076 end loop;
10078 -- If we fall out of loop, all components have component clauses
10079 -- and so we can set the size to the maximum value.
10081 if No (Comp) then
10082 Set_RM_Size (Rectype, Hbit + 1);
10083 end if;
10084 end if;
10085 end Check_Record_Representation_Clause;
10087 ----------------
10088 -- Check_Size --
10089 ----------------
10091 procedure Check_Size
10092 (N : Node_Id;
10093 T : Entity_Id;
10094 Siz : Uint;
10095 Biased : out Boolean)
10097 UT : constant Entity_Id := Underlying_Type (T);
10098 M : Uint;
10100 begin
10101 Biased := False;
10103 -- Reject patently improper size values.
10105 if Is_Elementary_Type (T)
10106 and then Siz > UI_From_Int (Int'Last)
10107 then
10108 Error_Msg_N ("Size value too large for elementary type", N);
10110 if Nkind (Original_Node (N)) = N_Op_Expon then
10111 Error_Msg_N
10112 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10113 end if;
10114 end if;
10116 -- Dismiss generic types
10118 if Is_Generic_Type (T)
10119 or else
10120 Is_Generic_Type (UT)
10121 or else
10122 Is_Generic_Type (Root_Type (UT))
10123 then
10124 return;
10126 -- Guard against previous errors
10128 elsif No (UT) or else UT = Any_Type then
10129 Check_Error_Detected;
10130 return;
10132 -- Check case of bit packed array
10134 elsif Is_Array_Type (UT)
10135 and then Known_Static_Component_Size (UT)
10136 and then Is_Bit_Packed_Array (UT)
10137 then
10138 declare
10139 Asiz : Uint;
10140 Indx : Node_Id;
10141 Ityp : Entity_Id;
10143 begin
10144 Asiz := Component_Size (UT);
10145 Indx := First_Index (UT);
10146 loop
10147 Ityp := Etype (Indx);
10149 -- If non-static bound, then we are not in the business of
10150 -- trying to check the length, and indeed an error will be
10151 -- issued elsewhere, since sizes of non-static array types
10152 -- cannot be set implicitly or explicitly.
10154 if not Is_OK_Static_Subtype (Ityp) then
10155 return;
10156 end if;
10158 -- Otherwise accumulate next dimension
10160 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10161 Expr_Value (Type_Low_Bound (Ityp)) +
10162 Uint_1);
10164 Next_Index (Indx);
10165 exit when No (Indx);
10166 end loop;
10168 if Asiz <= Siz then
10169 return;
10171 else
10172 Error_Msg_Uint_1 := Asiz;
10173 Error_Msg_NE
10174 ("size for& too small, minimum allowed is ^", N, T);
10175 Set_Esize (T, Asiz);
10176 Set_RM_Size (T, Asiz);
10177 end if;
10178 end;
10180 -- All other composite types are ignored
10182 elsif Is_Composite_Type (UT) then
10183 return;
10185 -- For fixed-point types, don't check minimum if type is not frozen,
10186 -- since we don't know all the characteristics of the type that can
10187 -- affect the size (e.g. a specified small) till freeze time.
10189 elsif Is_Fixed_Point_Type (UT)
10190 and then not Is_Frozen (UT)
10191 then
10192 null;
10194 -- Cases for which a minimum check is required
10196 else
10197 -- Ignore if specified size is correct for the type
10199 if Known_Esize (UT) and then Siz = Esize (UT) then
10200 return;
10201 end if;
10203 -- Otherwise get minimum size
10205 M := UI_From_Int (Minimum_Size (UT));
10207 if Siz < M then
10209 -- Size is less than minimum size, but one possibility remains
10210 -- that we can manage with the new size if we bias the type.
10212 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10214 if Siz < M then
10215 Error_Msg_Uint_1 := M;
10216 Error_Msg_NE
10217 ("size for& too small, minimum allowed is ^", N, T);
10218 Set_Esize (T, M);
10219 Set_RM_Size (T, M);
10220 else
10221 Biased := True;
10222 end if;
10223 end if;
10224 end if;
10225 end Check_Size;
10227 --------------------------
10228 -- Freeze_Entity_Checks --
10229 --------------------------
10231 procedure Freeze_Entity_Checks (N : Node_Id) is
10232 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10233 -- Inspect the primitive operations of type Typ and hide all pairs of
10234 -- implicitly declared non-overridden non-fully conformant homographs
10235 -- (Ada RM 8.3 12.3/2).
10237 -------------------------------------
10238 -- Hide_Non_Overridden_Subprograms --
10239 -------------------------------------
10241 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10242 procedure Hide_Matching_Homographs
10243 (Subp_Id : Entity_Id;
10244 Start_Elmt : Elmt_Id);
10245 -- Inspect a list of primitive operations starting with Start_Elmt
10246 -- and find matching implicitly declared non-overridden non-fully
10247 -- conformant homographs of Subp_Id. If found, all matches along
10248 -- with Subp_Id are hidden from all visibility.
10250 function Is_Non_Overridden_Or_Null_Procedure
10251 (Subp_Id : Entity_Id) return Boolean;
10252 -- Determine whether subprogram Subp_Id is implicitly declared non-
10253 -- overridden subprogram or an implicitly declared null procedure.
10255 ------------------------------
10256 -- Hide_Matching_Homographs --
10257 ------------------------------
10259 procedure Hide_Matching_Homographs
10260 (Subp_Id : Entity_Id;
10261 Start_Elmt : Elmt_Id)
10263 Prim : Entity_Id;
10264 Prim_Elmt : Elmt_Id;
10266 begin
10267 Prim_Elmt := Start_Elmt;
10268 while Present (Prim_Elmt) loop
10269 Prim := Node (Prim_Elmt);
10271 -- The current primitive is implicitly declared non-overridden
10272 -- non-fully conformant homograph of Subp_Id. Both subprograms
10273 -- must be hidden from visibility.
10275 if Chars (Prim) = Chars (Subp_Id)
10276 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10277 and then not Fully_Conformant (Prim, Subp_Id)
10278 then
10279 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10280 Set_Is_Immediately_Visible (Prim, False);
10281 Set_Is_Potentially_Use_Visible (Prim, False);
10283 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10284 Set_Is_Immediately_Visible (Subp_Id, False);
10285 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10286 end if;
10288 Next_Elmt (Prim_Elmt);
10289 end loop;
10290 end Hide_Matching_Homographs;
10292 -----------------------------------------
10293 -- Is_Non_Overridden_Or_Null_Procedure --
10294 -----------------------------------------
10296 function Is_Non_Overridden_Or_Null_Procedure
10297 (Subp_Id : Entity_Id) return Boolean
10299 Alias_Id : Entity_Id;
10301 begin
10302 -- The subprogram is inherited (implicitly declared), it does not
10303 -- override and does not cover a primitive of an interface.
10305 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10306 and then Present (Alias (Subp_Id))
10307 and then No (Interface_Alias (Subp_Id))
10308 and then No (Overridden_Operation (Subp_Id))
10309 then
10310 Alias_Id := Alias (Subp_Id);
10312 if Requires_Overriding (Alias_Id) then
10313 return True;
10315 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10316 and then Null_Present (Parent (Alias_Id))
10317 then
10318 return True;
10319 end if;
10320 end if;
10322 return False;
10323 end Is_Non_Overridden_Or_Null_Procedure;
10325 -- Local variables
10327 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10328 Prim : Entity_Id;
10329 Prim_Elmt : Elmt_Id;
10331 -- Start of processing for Hide_Non_Overridden_Subprograms
10333 begin
10334 -- Inspect the list of primitives looking for non-overridden
10335 -- subprograms.
10337 if Present (Prim_Ops) then
10338 Prim_Elmt := First_Elmt (Prim_Ops);
10339 while Present (Prim_Elmt) loop
10340 Prim := Node (Prim_Elmt);
10341 Next_Elmt (Prim_Elmt);
10343 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10344 Hide_Matching_Homographs
10345 (Subp_Id => Prim,
10346 Start_Elmt => Prim_Elmt);
10347 end if;
10348 end loop;
10349 end if;
10350 end Hide_Non_Overridden_Subprograms;
10352 ---------------------
10353 -- Local variables --
10354 ---------------------
10356 E : constant Entity_Id := Entity (N);
10358 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10359 -- True in non-generic case. Some of the processing here is skipped
10360 -- for the generic case since it is not needed. Basically in the
10361 -- generic case, we only need to do stuff that might generate error
10362 -- messages or warnings.
10364 -- Start of processing for Freeze_Entity_Checks
10366 begin
10367 -- Remember that we are processing a freezing entity. Required to
10368 -- ensure correct decoration of internal entities associated with
10369 -- interfaces (see New_Overloaded_Entity).
10371 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10373 -- For tagged types covering interfaces add internal entities that link
10374 -- the primitives of the interfaces with the primitives that cover them.
10375 -- Note: These entities were originally generated only when generating
10376 -- code because their main purpose was to provide support to initialize
10377 -- the secondary dispatch tables. They are now generated also when
10378 -- compiling with no code generation to provide ASIS the relationship
10379 -- between interface primitives and tagged type primitives. They are
10380 -- also used to locate primitives covering interfaces when processing
10381 -- generics (see Derive_Subprograms).
10383 -- This is not needed in the generic case
10385 if Ada_Version >= Ada_2005
10386 and then Non_Generic_Case
10387 and then Ekind (E) = E_Record_Type
10388 and then Is_Tagged_Type (E)
10389 and then not Is_Interface (E)
10390 and then Has_Interfaces (E)
10391 then
10392 -- This would be a good common place to call the routine that checks
10393 -- overriding of interface primitives (and thus factorize calls to
10394 -- Check_Abstract_Overriding located at different contexts in the
10395 -- compiler). However, this is not possible because it causes
10396 -- spurious errors in case of late overriding.
10398 Add_Internal_Interface_Entities (E);
10399 end if;
10401 -- After all forms of overriding have been resolved, a tagged type may
10402 -- be left with a set of implicitly declared and possibly erroneous
10403 -- abstract subprograms, null procedures and subprograms that require
10404 -- overriding. If this set contains fully conformat homographs, then one
10405 -- is chosen arbitrarily (already done during resolution), otherwise all
10406 -- remaining non-fully conformant homographs are hidden from visibility
10407 -- (Ada RM 8.3 12.3/2).
10409 if Is_Tagged_Type (E) then
10410 Hide_Non_Overridden_Subprograms (E);
10411 end if;
10413 -- Check CPP types
10415 if Ekind (E) = E_Record_Type
10416 and then Is_CPP_Class (E)
10417 and then Is_Tagged_Type (E)
10418 and then Tagged_Type_Expansion
10419 then
10420 if CPP_Num_Prims (E) = 0 then
10422 -- If the CPP type has user defined components then it must import
10423 -- primitives from C++. This is required because if the C++ class
10424 -- has no primitives then the C++ compiler does not added the _tag
10425 -- component to the type.
10427 if First_Entity (E) /= Last_Entity (E) then
10428 Error_Msg_N
10429 ("'C'P'P type must import at least one primitive from C++??",
10431 end if;
10432 end if;
10434 -- Check that all its primitives are abstract or imported from C++.
10435 -- Check also availability of the C++ constructor.
10437 declare
10438 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10439 Elmt : Elmt_Id;
10440 Error_Reported : Boolean := False;
10441 Prim : Node_Id;
10443 begin
10444 Elmt := First_Elmt (Primitive_Operations (E));
10445 while Present (Elmt) loop
10446 Prim := Node (Elmt);
10448 if Comes_From_Source (Prim) then
10449 if Is_Abstract_Subprogram (Prim) then
10450 null;
10452 elsif not Is_Imported (Prim)
10453 or else Convention (Prim) /= Convention_CPP
10454 then
10455 Error_Msg_N
10456 ("primitives of 'C'P'P types must be imported from C++ "
10457 & "or abstract??", Prim);
10459 elsif not Has_Constructors
10460 and then not Error_Reported
10461 then
10462 Error_Msg_Name_1 := Chars (E);
10463 Error_Msg_N
10464 ("??'C'P'P constructor required for type %", Prim);
10465 Error_Reported := True;
10466 end if;
10467 end if;
10469 Next_Elmt (Elmt);
10470 end loop;
10471 end;
10472 end if;
10474 -- Check Ada derivation of CPP type
10476 if Expander_Active -- why? losing errors in -gnatc mode???
10477 and then Present (Etype (E)) -- defend against errors
10478 and then Tagged_Type_Expansion
10479 and then Ekind (E) = E_Record_Type
10480 and then Etype (E) /= E
10481 and then Is_CPP_Class (Etype (E))
10482 and then CPP_Num_Prims (Etype (E)) > 0
10483 and then not Is_CPP_Class (E)
10484 and then not Has_CPP_Constructors (Etype (E))
10485 then
10486 -- If the parent has C++ primitives but it has no constructor then
10487 -- check that all the primitives are overridden in this derivation;
10488 -- otherwise the constructor of the parent is needed to build the
10489 -- dispatch table.
10491 declare
10492 Elmt : Elmt_Id;
10493 Prim : Node_Id;
10495 begin
10496 Elmt := First_Elmt (Primitive_Operations (E));
10497 while Present (Elmt) loop
10498 Prim := Node (Elmt);
10500 if not Is_Abstract_Subprogram (Prim)
10501 and then No (Interface_Alias (Prim))
10502 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10503 then
10504 Error_Msg_Name_1 := Chars (Etype (E));
10505 Error_Msg_N
10506 ("'C'P'P constructor required for parent type %", E);
10507 exit;
10508 end if;
10510 Next_Elmt (Elmt);
10511 end loop;
10512 end;
10513 end if;
10515 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10517 -- If we have a type with predicates, build predicate function. This
10518 -- is not needed in the generic case, and is not needed within TSS
10519 -- subprograms and other predefined primitives.
10521 if Non_Generic_Case
10522 and then Is_Type (E)
10523 and then Has_Predicates (E)
10524 and then not Within_Internal_Subprogram
10525 then
10526 Build_Predicate_Functions (E, N);
10527 end if;
10529 -- If type has delayed aspects, this is where we do the preanalysis at
10530 -- the freeze point, as part of the consistent visibility check. Note
10531 -- that this must be done after calling Build_Predicate_Functions or
10532 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10533 -- the subtype name in the saved expression so that they will not cause
10534 -- trouble in the preanalysis.
10536 -- This is also not needed in the generic case
10538 if Non_Generic_Case
10539 and then Has_Delayed_Aspects (E)
10540 and then Scope (E) = Current_Scope
10541 then
10542 -- Retrieve the visibility to the discriminants in order to properly
10543 -- analyze the aspects.
10545 Push_Scope_And_Install_Discriminants (E);
10547 declare
10548 Ritem : Node_Id;
10550 begin
10551 -- Look for aspect specification entries for this entity
10553 Ritem := First_Rep_Item (E);
10554 while Present (Ritem) loop
10555 if Nkind (Ritem) = N_Aspect_Specification
10556 and then Entity (Ritem) = E
10557 and then Is_Delayed_Aspect (Ritem)
10558 then
10559 Check_Aspect_At_Freeze_Point (Ritem);
10560 end if;
10562 Next_Rep_Item (Ritem);
10563 end loop;
10564 end;
10566 Uninstall_Discriminants_And_Pop_Scope (E);
10567 end if;
10569 -- For a record type, deal with variant parts. This has to be delayed
10570 -- to this point, because of the issue of statically predicated
10571 -- subtypes, which we have to ensure are frozen before checking
10572 -- choices, since we need to have the static choice list set.
10574 if Is_Record_Type (E) then
10575 Check_Variant_Part : declare
10576 D : constant Node_Id := Declaration_Node (E);
10577 T : Node_Id;
10578 C : Node_Id;
10579 VP : Node_Id;
10581 Others_Present : Boolean;
10582 pragma Warnings (Off, Others_Present);
10583 -- Indicates others present, not used in this case
10585 procedure Non_Static_Choice_Error (Choice : Node_Id);
10586 -- Error routine invoked by the generic instantiation below when
10587 -- the variant part has a non static choice.
10589 procedure Process_Declarations (Variant : Node_Id);
10590 -- Processes declarations associated with a variant. We analyzed
10591 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10592 -- but we still need the recursive call to Check_Choices for any
10593 -- nested variant to get its choices properly processed. This is
10594 -- also where we expand out the choices if expansion is active.
10596 package Variant_Choices_Processing is new
10597 Generic_Check_Choices
10598 (Process_Empty_Choice => No_OP,
10599 Process_Non_Static_Choice => Non_Static_Choice_Error,
10600 Process_Associated_Node => Process_Declarations);
10601 use Variant_Choices_Processing;
10603 -----------------------------
10604 -- Non_Static_Choice_Error --
10605 -----------------------------
10607 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10608 begin
10609 Flag_Non_Static_Expr
10610 ("choice given in variant part is not static!", Choice);
10611 end Non_Static_Choice_Error;
10613 --------------------------
10614 -- Process_Declarations --
10615 --------------------------
10617 procedure Process_Declarations (Variant : Node_Id) is
10618 CL : constant Node_Id := Component_List (Variant);
10619 VP : Node_Id;
10621 begin
10622 -- Check for static predicate present in this variant
10624 if Has_SP_Choice (Variant) then
10626 -- Here we expand. You might expect to find this call in
10627 -- Expand_N_Variant_Part, but that is called when we first
10628 -- see the variant part, and we cannot do this expansion
10629 -- earlier than the freeze point, since for statically
10630 -- predicated subtypes, the predicate is not known till
10631 -- the freeze point.
10633 -- Furthermore, we do this expansion even if the expander
10634 -- is not active, because other semantic processing, e.g.
10635 -- for aggregates, requires the expanded list of choices.
10637 -- If the expander is not active, then we can't just clobber
10638 -- the list since it would invalidate the ASIS -gnatct tree.
10639 -- So we have to rewrite the variant part with a Rewrite
10640 -- call that replaces it with a copy and clobber the copy.
10642 if not Expander_Active then
10643 declare
10644 NewV : constant Node_Id := New_Copy (Variant);
10645 begin
10646 Set_Discrete_Choices
10647 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10648 Rewrite (Variant, NewV);
10649 end;
10650 end if;
10652 Expand_Static_Predicates_In_Choices (Variant);
10653 end if;
10655 -- We don't need to worry about the declarations in the variant
10656 -- (since they were analyzed by Analyze_Choices when we first
10657 -- encountered the variant), but we do need to take care of
10658 -- expansion of any nested variants.
10660 if not Null_Present (CL) then
10661 VP := Variant_Part (CL);
10663 if Present (VP) then
10664 Check_Choices
10665 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10666 end if;
10667 end if;
10668 end Process_Declarations;
10670 -- Start of processing for Check_Variant_Part
10672 begin
10673 -- Find component list
10675 C := Empty;
10677 if Nkind (D) = N_Full_Type_Declaration then
10678 T := Type_Definition (D);
10680 if Nkind (T) = N_Record_Definition then
10681 C := Component_List (T);
10683 elsif Nkind (T) = N_Derived_Type_Definition
10684 and then Present (Record_Extension_Part (T))
10685 then
10686 C := Component_List (Record_Extension_Part (T));
10687 end if;
10688 end if;
10690 -- Case of variant part present
10692 if Present (C) and then Present (Variant_Part (C)) then
10693 VP := Variant_Part (C);
10695 -- Check choices
10697 Check_Choices
10698 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10700 -- If the last variant does not contain the Others choice,
10701 -- replace it with an N_Others_Choice node since Gigi always
10702 -- wants an Others. Note that we do not bother to call Analyze
10703 -- on the modified variant part, since its only effect would be
10704 -- to compute the Others_Discrete_Choices node laboriously, and
10705 -- of course we already know the list of choices corresponding
10706 -- to the others choice (it's the list we're replacing).
10708 -- We only want to do this if the expander is active, since
10709 -- we do not want to clobber the ASIS tree.
10711 if Expander_Active then
10712 declare
10713 Last_Var : constant Node_Id :=
10714 Last_Non_Pragma (Variants (VP));
10716 Others_Node : Node_Id;
10718 begin
10719 if Nkind (First (Discrete_Choices (Last_Var))) /=
10720 N_Others_Choice
10721 then
10722 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10723 Set_Others_Discrete_Choices
10724 (Others_Node, Discrete_Choices (Last_Var));
10725 Set_Discrete_Choices
10726 (Last_Var, New_List (Others_Node));
10727 end if;
10728 end;
10729 end if;
10730 end if;
10731 end Check_Variant_Part;
10732 end if;
10733 end Freeze_Entity_Checks;
10735 -------------------------
10736 -- Get_Alignment_Value --
10737 -------------------------
10739 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10740 Align : constant Uint := Static_Integer (Expr);
10742 begin
10743 if Align = No_Uint then
10744 return No_Uint;
10746 elsif Align <= 0 then
10747 Error_Msg_N ("alignment value must be positive", Expr);
10748 return No_Uint;
10750 else
10751 for J in Int range 0 .. 64 loop
10752 declare
10753 M : constant Uint := Uint_2 ** J;
10755 begin
10756 exit when M = Align;
10758 if M > Align then
10759 Error_Msg_N
10760 ("alignment value must be power of 2", Expr);
10761 return No_Uint;
10762 end if;
10763 end;
10764 end loop;
10766 return Align;
10767 end if;
10768 end Get_Alignment_Value;
10770 -------------------------------------
10771 -- Inherit_Aspects_At_Freeze_Point --
10772 -------------------------------------
10774 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10775 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10776 (Rep_Item : Node_Id) return Boolean;
10777 -- This routine checks if Rep_Item is either a pragma or an aspect
10778 -- specification node whose correponding pragma (if any) is present in
10779 -- the Rep Item chain of the entity it has been specified to.
10781 --------------------------------------------------
10782 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10783 --------------------------------------------------
10785 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10786 (Rep_Item : Node_Id) return Boolean
10788 begin
10789 return
10790 Nkind (Rep_Item) = N_Pragma
10791 or else Present_In_Rep_Item
10792 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10793 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10795 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10797 begin
10798 -- A representation item is either subtype-specific (Size and Alignment
10799 -- clauses) or type-related (all others). Subtype-specific aspects may
10800 -- differ for different subtypes of the same type (RM 13.1.8).
10802 -- A derived type inherits each type-related representation aspect of
10803 -- its parent type that was directly specified before the declaration of
10804 -- the derived type (RM 13.1.15).
10806 -- A derived subtype inherits each subtype-specific representation
10807 -- aspect of its parent subtype that was directly specified before the
10808 -- declaration of the derived type (RM 13.1.15).
10810 -- The general processing involves inheriting a representation aspect
10811 -- from a parent type whenever the first rep item (aspect specification,
10812 -- attribute definition clause, pragma) corresponding to the given
10813 -- representation aspect in the rep item chain of Typ, if any, isn't
10814 -- directly specified to Typ but to one of its parents.
10816 -- ??? Note that, for now, just a limited number of representation
10817 -- aspects have been inherited here so far. Many of them are
10818 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
10819 -- a non- exhaustive list of aspects that likely also need to
10820 -- be moved to this routine: Alignment, Component_Alignment,
10821 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
10822 -- Preelaborable_Initialization, RM_Size and Small.
10824 -- In addition, Convention must be propagated from base type to subtype,
10825 -- because the subtype may have been declared on an incomplete view.
10827 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
10828 return;
10829 end if;
10831 -- Ada_05/Ada_2005
10833 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
10834 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
10835 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10836 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
10837 then
10838 Set_Is_Ada_2005_Only (Typ);
10839 end if;
10841 -- Ada_12/Ada_2012
10843 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
10844 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
10845 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10846 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
10847 then
10848 Set_Is_Ada_2012_Only (Typ);
10849 end if;
10851 -- Atomic/Shared
10853 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
10854 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
10855 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10856 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
10857 then
10858 Set_Is_Atomic (Typ);
10859 Set_Treat_As_Volatile (Typ);
10860 Set_Is_Volatile (Typ);
10861 end if;
10863 -- Convention
10865 if Is_Record_Type (Typ)
10866 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
10867 then
10868 Set_Convention (Typ, Convention (Base_Type (Typ)));
10869 end if;
10871 -- Default_Component_Value
10873 if Is_Array_Type (Typ)
10874 and then Is_Base_Type (Typ)
10875 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
10876 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
10877 then
10878 Set_Default_Aspect_Component_Value (Typ,
10879 Default_Aspect_Component_Value
10880 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
10881 end if;
10883 -- Default_Value
10885 if Is_Scalar_Type (Typ)
10886 and then Is_Base_Type (Typ)
10887 and then Has_Rep_Item (Typ, Name_Default_Value, False)
10888 and then Has_Rep_Item (Typ, Name_Default_Value)
10889 then
10890 Set_Default_Aspect_Value (Typ,
10891 Default_Aspect_Value
10892 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
10893 end if;
10895 -- Discard_Names
10897 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
10898 and then Has_Rep_Item (Typ, Name_Discard_Names)
10899 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10900 (Get_Rep_Item (Typ, Name_Discard_Names))
10901 then
10902 Set_Discard_Names (Typ);
10903 end if;
10905 -- Invariants
10907 if not Has_Rep_Item (Typ, Name_Invariant, False)
10908 and then Has_Rep_Item (Typ, Name_Invariant)
10909 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10910 (Get_Rep_Item (Typ, Name_Invariant))
10911 then
10912 Set_Has_Invariants (Typ);
10914 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
10915 Set_Has_Inheritable_Invariants (Typ);
10916 end if;
10918 -- If we have a subtype with invariants, whose base type does not have
10919 -- invariants, copy these invariants to the base type. This happens for
10920 -- the case of implicit base types created for scalar and array types.
10922 elsif Has_Invariants (Typ)
10923 and then not Has_Invariants (Base_Type (Typ))
10924 then
10925 Set_Has_Invariants (Base_Type (Typ));
10926 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
10927 end if;
10929 -- Volatile
10931 if not Has_Rep_Item (Typ, Name_Volatile, False)
10932 and then Has_Rep_Item (Typ, Name_Volatile)
10933 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10934 (Get_Rep_Item (Typ, Name_Volatile))
10935 then
10936 Set_Treat_As_Volatile (Typ);
10937 Set_Is_Volatile (Typ);
10938 end if;
10940 -- Inheritance for derived types only
10942 if Is_Derived_Type (Typ) then
10943 declare
10944 Bas_Typ : constant Entity_Id := Base_Type (Typ);
10945 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
10947 begin
10948 -- Atomic_Components
10950 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
10951 and then Has_Rep_Item (Typ, Name_Atomic_Components)
10952 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10953 (Get_Rep_Item (Typ, Name_Atomic_Components))
10954 then
10955 Set_Has_Atomic_Components (Imp_Bas_Typ);
10956 end if;
10958 -- Volatile_Components
10960 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
10961 and then Has_Rep_Item (Typ, Name_Volatile_Components)
10962 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10963 (Get_Rep_Item (Typ, Name_Volatile_Components))
10964 then
10965 Set_Has_Volatile_Components (Imp_Bas_Typ);
10966 end if;
10968 -- Finalize_Storage_Only
10970 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
10971 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
10972 then
10973 Set_Finalize_Storage_Only (Bas_Typ);
10974 end if;
10976 -- Universal_Aliasing
10978 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
10979 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
10980 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10981 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
10982 then
10983 Set_Universal_Aliasing (Imp_Bas_Typ);
10984 end if;
10986 -- Bit_Order
10988 if Is_Record_Type (Typ) then
10989 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
10990 and then Has_Rep_Item (Typ, Name_Bit_Order)
10991 then
10992 Set_Reverse_Bit_Order (Bas_Typ,
10993 Reverse_Bit_Order (Entity (Name
10994 (Get_Rep_Item (Typ, Name_Bit_Order)))));
10995 end if;
10996 end if;
10998 -- Scalar_Storage_Order
11000 -- Note: the aspect is specified on a first subtype, but recorded
11001 -- in a flag of the base type!
11003 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11004 and then Typ = Bas_Typ
11005 then
11006 -- For a type extension, always inherit from parent; otherwise
11007 -- inherit if no default applies. Note: we do not check for
11008 -- an explicit rep item on the parent type when inheriting,
11009 -- because the parent SSO may itself have been set by default.
11011 if not Has_Rep_Item (First_Subtype (Typ),
11012 Name_Scalar_Storage_Order, False)
11013 and then (Is_Tagged_Type (Bas_Typ)
11014 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11015 or else
11016 SSO_Set_High_By_Default (Bas_Typ)))
11017 then
11018 Set_Reverse_Storage_Order (Bas_Typ,
11019 Reverse_Storage_Order
11020 (Implementation_Base_Type (Etype (Bas_Typ))));
11022 -- Clear default SSO indications, since the inherited aspect
11023 -- which was set explicitly overrides the default.
11025 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11026 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11027 end if;
11028 end if;
11029 end;
11030 end if;
11031 end Inherit_Aspects_At_Freeze_Point;
11033 ----------------
11034 -- Initialize --
11035 ----------------
11037 procedure Initialize is
11038 begin
11039 Address_Clause_Checks.Init;
11040 Independence_Checks.Init;
11041 Unchecked_Conversions.Init;
11042 end Initialize;
11044 ---------------------------
11045 -- Install_Discriminants --
11046 ---------------------------
11048 procedure Install_Discriminants (E : Entity_Id) is
11049 Disc : Entity_Id;
11050 Prev : Entity_Id;
11051 begin
11052 Disc := First_Discriminant (E);
11053 while Present (Disc) loop
11054 Prev := Current_Entity (Disc);
11055 Set_Current_Entity (Disc);
11056 Set_Is_Immediately_Visible (Disc);
11057 Set_Homonym (Disc, Prev);
11058 Next_Discriminant (Disc);
11059 end loop;
11060 end Install_Discriminants;
11062 -------------------------
11063 -- Is_Operational_Item --
11064 -------------------------
11066 function Is_Operational_Item (N : Node_Id) return Boolean is
11067 begin
11068 if Nkind (N) /= N_Attribute_Definition_Clause then
11069 return False;
11071 else
11072 declare
11073 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11074 begin
11075 return Id = Attribute_Input
11076 or else Id = Attribute_Output
11077 or else Id = Attribute_Read
11078 or else Id = Attribute_Write
11079 or else Id = Attribute_External_Tag;
11080 end;
11081 end if;
11082 end Is_Operational_Item;
11084 -------------------------
11085 -- Is_Predicate_Static --
11086 -------------------------
11088 -- Note: the basic legality of the expression has already been checked, so
11089 -- we don't need to worry about cases or ranges on strings for example.
11091 function Is_Predicate_Static
11092 (Expr : Node_Id;
11093 Nam : Name_Id) return Boolean
11095 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11096 -- Given a list of case expression alternatives, returns True if all
11097 -- the alternatives are static (have all static choices, and a static
11098 -- expression).
11100 function All_Static_Choices (L : List_Id) return Boolean;
11101 -- Returns true if all elements of the list are OK static choices
11102 -- as defined below for Is_Static_Choice. Used for case expression
11103 -- alternatives and for the right operand of a membership test. An
11104 -- others_choice is static if the corresponding expression is static.
11105 -- The staticness of the bounds is checked separately.
11107 function Is_Static_Choice (N : Node_Id) return Boolean;
11108 -- Returns True if N represents a static choice (static subtype, or
11109 -- static subtype indication, or static expression, or static range).
11111 -- Note that this is a bit more inclusive than we actually need
11112 -- (in particular membership tests do not allow the use of subtype
11113 -- indications). But that doesn't matter, we have already checked
11114 -- that the construct is legal to get this far.
11116 function Is_Type_Ref (N : Node_Id) return Boolean;
11117 pragma Inline (Is_Type_Ref);
11118 -- Returns True if N is a reference to the type for the predicate in the
11119 -- expression (i.e. if it is an identifier whose Chars field matches the
11120 -- Nam given in the call). N must not be parenthesized, if the type name
11121 -- appears in parens, this routine will return False.
11123 ----------------------------------
11124 -- All_Static_Case_Alternatives --
11125 ----------------------------------
11127 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11128 N : Node_Id;
11130 begin
11131 N := First (L);
11132 while Present (N) loop
11133 if not (All_Static_Choices (Discrete_Choices (N))
11134 and then Is_OK_Static_Expression (Expression (N)))
11135 then
11136 return False;
11137 end if;
11139 Next (N);
11140 end loop;
11142 return True;
11143 end All_Static_Case_Alternatives;
11145 ------------------------
11146 -- All_Static_Choices --
11147 ------------------------
11149 function All_Static_Choices (L : List_Id) return Boolean is
11150 N : Node_Id;
11152 begin
11153 N := First (L);
11154 while Present (N) loop
11155 if not Is_Static_Choice (N) then
11156 return False;
11157 end if;
11159 Next (N);
11160 end loop;
11162 return True;
11163 end All_Static_Choices;
11165 ----------------------
11166 -- Is_Static_Choice --
11167 ----------------------
11169 function Is_Static_Choice (N : Node_Id) return Boolean is
11170 begin
11171 return Nkind (N) = N_Others_Choice
11172 or else Is_OK_Static_Expression (N)
11173 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11174 and then Is_OK_Static_Subtype (Entity (N)))
11175 or else (Nkind (N) = N_Subtype_Indication
11176 and then Is_OK_Static_Subtype (Entity (N)))
11177 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11178 end Is_Static_Choice;
11180 -----------------
11181 -- Is_Type_Ref --
11182 -----------------
11184 function Is_Type_Ref (N : Node_Id) return Boolean is
11185 begin
11186 return Nkind (N) = N_Identifier
11187 and then Chars (N) = Nam
11188 and then Paren_Count (N) = 0;
11189 end Is_Type_Ref;
11191 -- Start of processing for Is_Predicate_Static
11193 begin
11194 -- Predicate_Static means one of the following holds. Numbers are the
11195 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11197 -- 16: A static expression
11199 if Is_OK_Static_Expression (Expr) then
11200 return True;
11202 -- 17: A membership test whose simple_expression is the current
11203 -- instance, and whose membership_choice_list meets the requirements
11204 -- for a static membership test.
11206 elsif Nkind (Expr) in N_Membership_Test
11207 and then ((Present (Right_Opnd (Expr))
11208 and then Is_Static_Choice (Right_Opnd (Expr)))
11209 or else
11210 (Present (Alternatives (Expr))
11211 and then All_Static_Choices (Alternatives (Expr))))
11212 then
11213 return True;
11215 -- 18. A case_expression whose selecting_expression is the current
11216 -- instance, and whose dependent expressions are static expressions.
11218 elsif Nkind (Expr) = N_Case_Expression
11219 and then Is_Type_Ref (Expression (Expr))
11220 and then All_Static_Case_Alternatives (Alternatives (Expr))
11221 then
11222 return True;
11224 -- 19. A call to a predefined equality or ordering operator, where one
11225 -- operand is the current instance, and the other is a static
11226 -- expression.
11228 -- Note: the RM is clearly wrong here in not excluding string types.
11229 -- Without this exclusion, we would allow expressions like X > "ABC"
11230 -- to be considered as predicate-static, which is clearly not intended,
11231 -- since the idea is for predicate-static to be a subset of normal
11232 -- static expressions (and "DEF" > "ABC" is not a static expression).
11234 -- However, we do allow internally generated (not from source) equality
11235 -- and inequality operations to be valid on strings (this helps deal
11236 -- with cases where we transform A in "ABC" to A = "ABC).
11238 elsif Nkind (Expr) in N_Op_Compare
11239 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11240 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11241 and then not Comes_From_Source (Expr)))
11242 and then ((Is_Type_Ref (Left_Opnd (Expr))
11243 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11244 or else
11245 (Is_Type_Ref (Right_Opnd (Expr))
11246 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11247 then
11248 return True;
11250 -- 20. A call to a predefined boolean logical operator, where each
11251 -- operand is predicate-static.
11253 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11254 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11255 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11256 or else
11257 (Nkind (Expr) = N_Op_Not
11258 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11259 then
11260 return True;
11262 -- 21. A short-circuit control form where both operands are
11263 -- predicate-static.
11265 elsif Nkind (Expr) in N_Short_Circuit
11266 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11267 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11268 then
11269 return True;
11271 -- 22. A parenthesized predicate-static expression. This does not
11272 -- require any special test, since we just ignore paren levels in
11273 -- all the cases above.
11275 -- One more test that is an implementation artifact caused by the fact
11276 -- that we are analyzing not the original expression, but the generated
11277 -- expression in the body of the predicate function. This can include
11278 -- references to inherited predicates, so that the expression we are
11279 -- processing looks like:
11281 -- expression and then xxPredicate (typ (Inns))
11283 -- Where the call is to a Predicate function for an inherited predicate.
11284 -- We simply ignore such a call, which could be to either a dynamic or
11285 -- a static predicate. Note that if the parent predicate is dynamic then
11286 -- eventually this type will be marked as dynamic, but you are allowed
11287 -- to specify a static predicate for a subtype which is inheriting a
11288 -- dynamic predicate, so the static predicate validation here ignores
11289 -- the inherited predicate even if it is dynamic.
11291 elsif Nkind (Expr) = N_Function_Call
11292 and then Is_Predicate_Function (Entity (Name (Expr)))
11293 then
11294 return True;
11296 -- That's an exhaustive list of tests, all other cases are not
11297 -- predicate-static, so we return False.
11299 else
11300 return False;
11301 end if;
11302 end Is_Predicate_Static;
11304 ---------------------
11305 -- Kill_Rep_Clause --
11306 ---------------------
11308 procedure Kill_Rep_Clause (N : Node_Id) is
11309 begin
11310 pragma Assert (Ignore_Rep_Clauses);
11312 -- Note: we use Replace rather than Rewrite, because we don't want
11313 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11314 -- rep clause that is being replaced.
11316 Replace (N, Make_Null_Statement (Sloc (N)));
11318 -- The null statement must be marked as not coming from source. This is
11319 -- so that ASIS ignores it, and also the back end does not expect bogus
11320 -- "from source" null statements in weird places (e.g. in declarative
11321 -- regions where such null statements are not allowed).
11323 Set_Comes_From_Source (N, False);
11324 end Kill_Rep_Clause;
11326 ------------------
11327 -- Minimum_Size --
11328 ------------------
11330 function Minimum_Size
11331 (T : Entity_Id;
11332 Biased : Boolean := False) return Nat
11334 Lo : Uint := No_Uint;
11335 Hi : Uint := No_Uint;
11336 LoR : Ureal := No_Ureal;
11337 HiR : Ureal := No_Ureal;
11338 LoSet : Boolean := False;
11339 HiSet : Boolean := False;
11340 B : Uint;
11341 S : Nat;
11342 Ancest : Entity_Id;
11343 R_Typ : constant Entity_Id := Root_Type (T);
11345 begin
11346 -- If bad type, return 0
11348 if T = Any_Type then
11349 return 0;
11351 -- For generic types, just return zero. There cannot be any legitimate
11352 -- need to know such a size, but this routine may be called with a
11353 -- generic type as part of normal processing.
11355 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11356 return 0;
11358 -- Access types (cannot have size smaller than System.Address)
11360 elsif Is_Access_Type (T) then
11361 return System_Address_Size;
11363 -- Floating-point types
11365 elsif Is_Floating_Point_Type (T) then
11366 return UI_To_Int (Esize (R_Typ));
11368 -- Discrete types
11370 elsif Is_Discrete_Type (T) then
11372 -- The following loop is looking for the nearest compile time known
11373 -- bounds following the ancestor subtype chain. The idea is to find
11374 -- the most restrictive known bounds information.
11376 Ancest := T;
11377 loop
11378 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11379 return 0;
11380 end if;
11382 if not LoSet then
11383 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11384 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11385 LoSet := True;
11386 exit when HiSet;
11387 end if;
11388 end if;
11390 if not HiSet then
11391 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11392 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11393 HiSet := True;
11394 exit when LoSet;
11395 end if;
11396 end if;
11398 Ancest := Ancestor_Subtype (Ancest);
11400 if No (Ancest) then
11401 Ancest := Base_Type (T);
11403 if Is_Generic_Type (Ancest) then
11404 return 0;
11405 end if;
11406 end if;
11407 end loop;
11409 -- Fixed-point types. We can't simply use Expr_Value to get the
11410 -- Corresponding_Integer_Value values of the bounds, since these do not
11411 -- get set till the type is frozen, and this routine can be called
11412 -- before the type is frozen. Similarly the test for bounds being static
11413 -- needs to include the case where we have unanalyzed real literals for
11414 -- the same reason.
11416 elsif Is_Fixed_Point_Type (T) then
11418 -- The following loop is looking for the nearest compile time known
11419 -- bounds following the ancestor subtype chain. The idea is to find
11420 -- the most restrictive known bounds information.
11422 Ancest := T;
11423 loop
11424 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11425 return 0;
11426 end if;
11428 -- Note: In the following two tests for LoSet and HiSet, it may
11429 -- seem redundant to test for N_Real_Literal here since normally
11430 -- one would assume that the test for the value being known at
11431 -- compile time includes this case. However, there is a glitch.
11432 -- If the real literal comes from folding a non-static expression,
11433 -- then we don't consider any non- static expression to be known
11434 -- at compile time if we are in configurable run time mode (needed
11435 -- in some cases to give a clearer definition of what is and what
11436 -- is not accepted). So the test is indeed needed. Without it, we
11437 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11439 if not LoSet then
11440 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11441 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11442 then
11443 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11444 LoSet := True;
11445 exit when HiSet;
11446 end if;
11447 end if;
11449 if not HiSet then
11450 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11451 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11452 then
11453 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11454 HiSet := True;
11455 exit when LoSet;
11456 end if;
11457 end if;
11459 Ancest := Ancestor_Subtype (Ancest);
11461 if No (Ancest) then
11462 Ancest := Base_Type (T);
11464 if Is_Generic_Type (Ancest) then
11465 return 0;
11466 end if;
11467 end if;
11468 end loop;
11470 Lo := UR_To_Uint (LoR / Small_Value (T));
11471 Hi := UR_To_Uint (HiR / Small_Value (T));
11473 -- No other types allowed
11475 else
11476 raise Program_Error;
11477 end if;
11479 -- Fall through with Hi and Lo set. Deal with biased case
11481 if (Biased
11482 and then not Is_Fixed_Point_Type (T)
11483 and then not (Is_Enumeration_Type (T)
11484 and then Has_Non_Standard_Rep (T)))
11485 or else Has_Biased_Representation (T)
11486 then
11487 Hi := Hi - Lo;
11488 Lo := Uint_0;
11489 end if;
11491 -- Signed case. Note that we consider types like range 1 .. -1 to be
11492 -- signed for the purpose of computing the size, since the bounds have
11493 -- to be accommodated in the base type.
11495 if Lo < 0 or else Hi < 0 then
11496 S := 1;
11497 B := Uint_1;
11499 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11500 -- Note that we accommodate the case where the bounds cross. This
11501 -- can happen either because of the way the bounds are declared
11502 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11504 while Lo < -B
11505 or else Hi < -B
11506 or else Lo >= B
11507 or else Hi >= B
11508 loop
11509 B := Uint_2 ** S;
11510 S := S + 1;
11511 end loop;
11513 -- Unsigned case
11515 else
11516 -- If both bounds are positive, make sure that both are represen-
11517 -- table in the case where the bounds are crossed. This can happen
11518 -- either because of the way the bounds are declared, or because of
11519 -- the algorithm in Freeze_Fixed_Point_Type.
11521 if Lo > Hi then
11522 Hi := Lo;
11523 end if;
11525 -- S = size, (can accommodate 0 .. (2**size - 1))
11527 S := 0;
11528 while Hi >= Uint_2 ** S loop
11529 S := S + 1;
11530 end loop;
11531 end if;
11533 return S;
11534 end Minimum_Size;
11536 ---------------------------
11537 -- New_Stream_Subprogram --
11538 ---------------------------
11540 procedure New_Stream_Subprogram
11541 (N : Node_Id;
11542 Ent : Entity_Id;
11543 Subp : Entity_Id;
11544 Nam : TSS_Name_Type)
11546 Loc : constant Source_Ptr := Sloc (N);
11547 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11548 Subp_Id : Entity_Id;
11549 Subp_Decl : Node_Id;
11550 F : Entity_Id;
11551 Etyp : Entity_Id;
11553 Defer_Declaration : constant Boolean :=
11554 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11555 -- For a tagged type, there is a declaration for each stream attribute
11556 -- at the freeze point, and we must generate only a completion of this
11557 -- declaration. We do the same for private types, because the full view
11558 -- might be tagged. Otherwise we generate a declaration at the point of
11559 -- the attribute definition clause.
11561 function Build_Spec return Node_Id;
11562 -- Used for declaration and renaming declaration, so that this is
11563 -- treated as a renaming_as_body.
11565 ----------------
11566 -- Build_Spec --
11567 ----------------
11569 function Build_Spec return Node_Id is
11570 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11571 Formals : List_Id;
11572 Spec : Node_Id;
11573 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11575 begin
11576 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11578 -- S : access Root_Stream_Type'Class
11580 Formals := New_List (
11581 Make_Parameter_Specification (Loc,
11582 Defining_Identifier =>
11583 Make_Defining_Identifier (Loc, Name_S),
11584 Parameter_Type =>
11585 Make_Access_Definition (Loc,
11586 Subtype_Mark =>
11587 New_Occurrence_Of (
11588 Designated_Type (Etype (F)), Loc))));
11590 if Nam = TSS_Stream_Input then
11591 Spec :=
11592 Make_Function_Specification (Loc,
11593 Defining_Unit_Name => Subp_Id,
11594 Parameter_Specifications => Formals,
11595 Result_Definition => T_Ref);
11596 else
11597 -- V : [out] T
11599 Append_To (Formals,
11600 Make_Parameter_Specification (Loc,
11601 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11602 Out_Present => Out_P,
11603 Parameter_Type => T_Ref));
11605 Spec :=
11606 Make_Procedure_Specification (Loc,
11607 Defining_Unit_Name => Subp_Id,
11608 Parameter_Specifications => Formals);
11609 end if;
11611 return Spec;
11612 end Build_Spec;
11614 -- Start of processing for New_Stream_Subprogram
11616 begin
11617 F := First_Formal (Subp);
11619 if Ekind (Subp) = E_Procedure then
11620 Etyp := Etype (Next_Formal (F));
11621 else
11622 Etyp := Etype (Subp);
11623 end if;
11625 -- Prepare subprogram declaration and insert it as an action on the
11626 -- clause node. The visibility for this entity is used to test for
11627 -- visibility of the attribute definition clause (in the sense of
11628 -- 8.3(23) as amended by AI-195).
11630 if not Defer_Declaration then
11631 Subp_Decl :=
11632 Make_Subprogram_Declaration (Loc,
11633 Specification => Build_Spec);
11635 -- For a tagged type, there is always a visible declaration for each
11636 -- stream TSS (it is a predefined primitive operation), and the
11637 -- completion of this declaration occurs at the freeze point, which is
11638 -- not always visible at places where the attribute definition clause is
11639 -- visible. So, we create a dummy entity here for the purpose of
11640 -- tracking the visibility of the attribute definition clause itself.
11642 else
11643 Subp_Id :=
11644 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11645 Subp_Decl :=
11646 Make_Object_Declaration (Loc,
11647 Defining_Identifier => Subp_Id,
11648 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11649 end if;
11651 Insert_Action (N, Subp_Decl);
11652 Set_Entity (N, Subp_Id);
11654 Subp_Decl :=
11655 Make_Subprogram_Renaming_Declaration (Loc,
11656 Specification => Build_Spec,
11657 Name => New_Occurrence_Of (Subp, Loc));
11659 if Defer_Declaration then
11660 Set_TSS (Base_Type (Ent), Subp_Id);
11661 else
11662 Insert_Action (N, Subp_Decl);
11663 Copy_TSS (Subp_Id, Base_Type (Ent));
11664 end if;
11665 end New_Stream_Subprogram;
11667 ------------------------------------------
11668 -- Push_Scope_And_Install_Discriminants --
11669 ------------------------------------------
11671 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11672 begin
11673 if Has_Discriminants (E) then
11674 Push_Scope (E);
11676 -- Make discriminants visible for type declarations and protected
11677 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11679 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11680 Install_Discriminants (E);
11681 end if;
11682 end if;
11683 end Push_Scope_And_Install_Discriminants;
11685 ------------------------
11686 -- Rep_Item_Too_Early --
11687 ------------------------
11689 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
11690 begin
11691 -- Cannot apply non-operational rep items to generic types
11693 if Is_Operational_Item (N) then
11694 return False;
11696 elsif Is_Type (T)
11697 and then Is_Generic_Type (Root_Type (T))
11698 then
11699 Error_Msg_N ("representation item not allowed for generic type", N);
11700 return True;
11701 end if;
11703 -- Otherwise check for incomplete type
11705 if Is_Incomplete_Or_Private_Type (T)
11706 and then No (Underlying_Type (T))
11707 and then
11708 (Nkind (N) /= N_Pragma
11709 or else Get_Pragma_Id (N) /= Pragma_Import)
11710 then
11711 Error_Msg_N
11712 ("representation item must be after full type declaration", N);
11713 return True;
11715 -- If the type has incomplete components, a representation clause is
11716 -- illegal but stream attributes and Convention pragmas are correct.
11718 elsif Has_Private_Component (T) then
11719 if Nkind (N) = N_Pragma then
11720 return False;
11722 else
11723 Error_Msg_N
11724 ("representation item must appear after type is fully defined",
11726 return True;
11727 end if;
11728 else
11729 return False;
11730 end if;
11731 end Rep_Item_Too_Early;
11733 -----------------------
11734 -- Rep_Item_Too_Late --
11735 -----------------------
11737 function Rep_Item_Too_Late
11738 (T : Entity_Id;
11739 N : Node_Id;
11740 FOnly : Boolean := False) return Boolean
11742 S : Entity_Id;
11743 Parent_Type : Entity_Id;
11745 procedure No_Type_Rep_Item;
11746 -- Output message indicating that no type-related aspects can be
11747 -- specified due to some property of the parent type.
11749 procedure Too_Late;
11750 -- Output message for an aspect being specified too late
11752 -- Note that neither of the above errors is considered a serious one,
11753 -- since the effect is simply that we ignore the representation clause
11754 -- in these cases.
11755 -- Is this really true? In any case if we make this change we must
11756 -- document the requirement in the spec of Rep_Item_Too_Late that
11757 -- if True is returned, then the rep item must be completely ignored???
11759 ----------------------
11760 -- No_Type_Rep_Item --
11761 ----------------------
11763 procedure No_Type_Rep_Item is
11764 begin
11765 Error_Msg_N ("|type-related representation item not permitted!", N);
11766 end No_Type_Rep_Item;
11768 --------------
11769 -- Too_Late --
11770 --------------
11772 procedure Too_Late is
11773 begin
11774 -- Other compilers seem more relaxed about rep items appearing too
11775 -- late. Since analysis tools typically don't care about rep items
11776 -- anyway, no reason to be too strict about this.
11778 if not Relaxed_RM_Semantics then
11779 Error_Msg_N ("|representation item appears too late!", N);
11780 end if;
11781 end Too_Late;
11783 -- Start of processing for Rep_Item_Too_Late
11785 begin
11786 -- First make sure entity is not frozen (RM 13.1(9))
11788 if Is_Frozen (T)
11790 -- Exclude imported types, which may be frozen if they appear in a
11791 -- representation clause for a local type.
11793 and then not From_Limited_With (T)
11795 -- Exclude generated entities (not coming from source). The common
11796 -- case is when we generate a renaming which prematurely freezes the
11797 -- renamed internal entity, but we still want to be able to set copies
11798 -- of attribute values such as Size/Alignment.
11800 and then Comes_From_Source (T)
11801 then
11802 Too_Late;
11803 S := First_Subtype (T);
11805 if Present (Freeze_Node (S)) then
11806 if not Relaxed_RM_Semantics then
11807 Error_Msg_NE
11808 ("??no more representation items for }", Freeze_Node (S), S);
11809 end if;
11810 end if;
11812 return True;
11814 -- Check for case of untagged derived type whose parent either has
11815 -- primitive operations, or is a by reference type (RM 13.1(10)). In
11816 -- this case we do not output a Too_Late message, since there is no
11817 -- earlier point where the rep item could be placed to make it legal.
11819 elsif Is_Type (T)
11820 and then not FOnly
11821 and then Is_Derived_Type (T)
11822 and then not Is_Tagged_Type (T)
11823 then
11824 Parent_Type := Etype (Base_Type (T));
11826 if Has_Primitive_Operations (Parent_Type) then
11827 No_Type_Rep_Item;
11829 if not Relaxed_RM_Semantics then
11830 Error_Msg_NE
11831 ("\parent type & has primitive operations!", N, Parent_Type);
11832 end if;
11834 return True;
11836 elsif Is_By_Reference_Type (Parent_Type) then
11837 No_Type_Rep_Item;
11839 if not Relaxed_RM_Semantics then
11840 Error_Msg_NE
11841 ("\parent type & is a by reference type!", N, Parent_Type);
11842 end if;
11844 return True;
11845 end if;
11846 end if;
11848 -- No error, but one more warning to consider. The RM (surprisingly)
11849 -- allows this pattern:
11851 -- type S is ...
11852 -- primitive operations for S
11853 -- type R is new S;
11854 -- rep clause for S
11856 -- Meaning that calls on the primitive operations of S for values of
11857 -- type R may require possibly expensive implicit conversion operations.
11858 -- This is not an error, but is worth a warning.
11860 if not Relaxed_RM_Semantics and then Is_Type (T) then
11861 declare
11862 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
11864 begin
11865 if Present (DTL)
11866 and then Has_Primitive_Operations (Base_Type (T))
11868 -- For now, do not generate this warning for the case of aspect
11869 -- specification using Ada 2012 syntax, since we get wrong
11870 -- messages we do not understand. The whole business of derived
11871 -- types and rep items seems a bit confused when aspects are
11872 -- used, since the aspects are not evaluated till freeze time.
11874 and then not From_Aspect_Specification (N)
11875 then
11876 Error_Msg_Sloc := Sloc (DTL);
11877 Error_Msg_N
11878 ("representation item for& appears after derived type "
11879 & "declaration#??", N);
11880 Error_Msg_NE
11881 ("\may result in implicit conversions for primitive "
11882 & "operations of&??", N, T);
11883 Error_Msg_NE
11884 ("\to change representations when called with arguments "
11885 & "of type&??", N, DTL);
11886 end if;
11887 end;
11888 end if;
11890 -- No error, link item into head of chain of rep items for the entity,
11891 -- but avoid chaining if we have an overloadable entity, and the pragma
11892 -- is one that can apply to multiple overloaded entities.
11894 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
11895 declare
11896 Pname : constant Name_Id := Pragma_Name (N);
11897 begin
11898 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
11899 Name_External, Name_Interface)
11900 then
11901 return False;
11902 end if;
11903 end;
11904 end if;
11906 Record_Rep_Item (T, N);
11907 return False;
11908 end Rep_Item_Too_Late;
11910 -------------------------------------
11911 -- Replace_Type_References_Generic --
11912 -------------------------------------
11914 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
11915 TName : constant Name_Id := Chars (T);
11917 function Replace_Node (N : Node_Id) return Traverse_Result;
11918 -- Processes a single node in the traversal procedure below, checking
11919 -- if node N should be replaced, and if so, doing the replacement.
11921 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
11922 -- This instantiation provides the body of Replace_Type_References
11924 ------------------
11925 -- Replace_Node --
11926 ------------------
11928 function Replace_Node (N : Node_Id) return Traverse_Result is
11929 S : Entity_Id;
11930 P : Node_Id;
11932 begin
11933 -- Case of identifier
11935 if Nkind (N) = N_Identifier then
11937 -- If not the type name, check whether it is a reference to
11938 -- some other type, which must be frozen before the predicate
11939 -- function is analyzed, i.e. before the freeze node of the
11940 -- type to which the predicate applies.
11942 if Chars (N) /= TName then
11943 if Present (Current_Entity (N))
11944 and then Is_Type (Current_Entity (N))
11945 then
11946 Freeze_Before (Freeze_Node (T), Current_Entity (N));
11947 end if;
11949 return Skip;
11951 -- Otherwise do the replacement and we are done with this node
11953 else
11954 Replace_Type_Reference (N);
11955 return Skip;
11956 end if;
11958 -- Case of selected component (which is what a qualification
11959 -- looks like in the unanalyzed tree, which is what we have.
11961 elsif Nkind (N) = N_Selected_Component then
11963 -- If selector name is not our type, keeping going (we might
11964 -- still have an occurrence of the type in the prefix).
11966 if Nkind (Selector_Name (N)) /= N_Identifier
11967 or else Chars (Selector_Name (N)) /= TName
11968 then
11969 return OK;
11971 -- Selector name is our type, check qualification
11973 else
11974 -- Loop through scopes and prefixes, doing comparison
11976 S := Current_Scope;
11977 P := Prefix (N);
11978 loop
11979 -- Continue if no more scopes or scope with no name
11981 if No (S) or else Nkind (S) not in N_Has_Chars then
11982 return OK;
11983 end if;
11985 -- Do replace if prefix is an identifier matching the
11986 -- scope that we are currently looking at.
11988 if Nkind (P) = N_Identifier
11989 and then Chars (P) = Chars (S)
11990 then
11991 Replace_Type_Reference (N);
11992 return Skip;
11993 end if;
11995 -- Go check scope above us if prefix is itself of the
11996 -- form of a selected component, whose selector matches
11997 -- the scope we are currently looking at.
11999 if Nkind (P) = N_Selected_Component
12000 and then Nkind (Selector_Name (P)) = N_Identifier
12001 and then Chars (Selector_Name (P)) = Chars (S)
12002 then
12003 S := Scope (S);
12004 P := Prefix (P);
12006 -- For anything else, we don't have a match, so keep on
12007 -- going, there are still some weird cases where we may
12008 -- still have a replacement within the prefix.
12010 else
12011 return OK;
12012 end if;
12013 end loop;
12014 end if;
12016 -- Continue for any other node kind
12018 else
12019 return OK;
12020 end if;
12021 end Replace_Node;
12023 begin
12024 Replace_Type_Refs (N);
12025 end Replace_Type_References_Generic;
12027 -------------------------
12028 -- Same_Representation --
12029 -------------------------
12031 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12032 T1 : constant Entity_Id := Underlying_Type (Typ1);
12033 T2 : constant Entity_Id := Underlying_Type (Typ2);
12035 begin
12036 -- A quick check, if base types are the same, then we definitely have
12037 -- the same representation, because the subtype specific representation
12038 -- attributes (Size and Alignment) do not affect representation from
12039 -- the point of view of this test.
12041 if Base_Type (T1) = Base_Type (T2) then
12042 return True;
12044 elsif Is_Private_Type (Base_Type (T2))
12045 and then Base_Type (T1) = Full_View (Base_Type (T2))
12046 then
12047 return True;
12048 end if;
12050 -- Tagged types never have differing representations
12052 if Is_Tagged_Type (T1) then
12053 return True;
12054 end if;
12056 -- Representations are definitely different if conventions differ
12058 if Convention (T1) /= Convention (T2) then
12059 return False;
12060 end if;
12062 -- Representations are different if component alignments or scalar
12063 -- storage orders differ.
12065 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
12066 and then
12067 (Is_Record_Type (T2) or else Is_Array_Type (T2))
12068 and then
12069 (Component_Alignment (T1) /= Component_Alignment (T2)
12070 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
12071 then
12072 return False;
12073 end if;
12075 -- For arrays, the only real issue is component size. If we know the
12076 -- component size for both arrays, and it is the same, then that's
12077 -- good enough to know we don't have a change of representation.
12079 if Is_Array_Type (T1) then
12080 if Known_Component_Size (T1)
12081 and then Known_Component_Size (T2)
12082 and then Component_Size (T1) = Component_Size (T2)
12083 then
12084 if VM_Target = No_VM then
12085 return True;
12087 -- In VM targets the representation of arrays with aliased
12088 -- components differs from arrays with non-aliased components
12090 else
12091 return Has_Aliased_Components (Base_Type (T1))
12093 Has_Aliased_Components (Base_Type (T2));
12094 end if;
12095 end if;
12096 end if;
12098 -- Types definitely have same representation if neither has non-standard
12099 -- representation since default representations are always consistent.
12100 -- If only one has non-standard representation, and the other does not,
12101 -- then we consider that they do not have the same representation. They
12102 -- might, but there is no way of telling early enough.
12104 if Has_Non_Standard_Rep (T1) then
12105 if not Has_Non_Standard_Rep (T2) then
12106 return False;
12107 end if;
12108 else
12109 return not Has_Non_Standard_Rep (T2);
12110 end if;
12112 -- Here the two types both have non-standard representation, and we need
12113 -- to determine if they have the same non-standard representation.
12115 -- For arrays, we simply need to test if the component sizes are the
12116 -- same. Pragma Pack is reflected in modified component sizes, so this
12117 -- check also deals with pragma Pack.
12119 if Is_Array_Type (T1) then
12120 return Component_Size (T1) = Component_Size (T2);
12122 -- Tagged types always have the same representation, because it is not
12123 -- possible to specify different representations for common fields.
12125 elsif Is_Tagged_Type (T1) then
12126 return True;
12128 -- Case of record types
12130 elsif Is_Record_Type (T1) then
12132 -- Packed status must conform
12134 if Is_Packed (T1) /= Is_Packed (T2) then
12135 return False;
12137 -- Otherwise we must check components. Typ2 maybe a constrained
12138 -- subtype with fewer components, so we compare the components
12139 -- of the base types.
12141 else
12142 Record_Case : declare
12143 CD1, CD2 : Entity_Id;
12145 function Same_Rep return Boolean;
12146 -- CD1 and CD2 are either components or discriminants. This
12147 -- function tests whether they have the same representation.
12149 --------------
12150 -- Same_Rep --
12151 --------------
12153 function Same_Rep return Boolean is
12154 begin
12155 if No (Component_Clause (CD1)) then
12156 return No (Component_Clause (CD2));
12157 else
12158 -- Note: at this point, component clauses have been
12159 -- normalized to the default bit order, so that the
12160 -- comparison of Component_Bit_Offsets is meaningful.
12162 return
12163 Present (Component_Clause (CD2))
12164 and then
12165 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12166 and then
12167 Esize (CD1) = Esize (CD2);
12168 end if;
12169 end Same_Rep;
12171 -- Start of processing for Record_Case
12173 begin
12174 if Has_Discriminants (T1) then
12176 -- The number of discriminants may be different if the
12177 -- derived type has fewer (constrained by values). The
12178 -- invisible discriminants retain the representation of
12179 -- the original, so the discrepancy does not per se
12180 -- indicate a different representation.
12182 CD1 := First_Discriminant (T1);
12183 CD2 := First_Discriminant (T2);
12184 while Present (CD1) and then Present (CD2) loop
12185 if not Same_Rep then
12186 return False;
12187 else
12188 Next_Discriminant (CD1);
12189 Next_Discriminant (CD2);
12190 end if;
12191 end loop;
12192 end if;
12194 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12195 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
12196 while Present (CD1) loop
12197 if not Same_Rep then
12198 return False;
12199 else
12200 Next_Component (CD1);
12201 Next_Component (CD2);
12202 end if;
12203 end loop;
12205 return True;
12206 end Record_Case;
12207 end if;
12209 -- For enumeration types, we must check each literal to see if the
12210 -- representation is the same. Note that we do not permit enumeration
12211 -- representation clauses for Character and Wide_Character, so these
12212 -- cases were already dealt with.
12214 elsif Is_Enumeration_Type (T1) then
12215 Enumeration_Case : declare
12216 L1, L2 : Entity_Id;
12218 begin
12219 L1 := First_Literal (T1);
12220 L2 := First_Literal (T2);
12221 while Present (L1) loop
12222 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12223 return False;
12224 else
12225 Next_Literal (L1);
12226 Next_Literal (L2);
12227 end if;
12228 end loop;
12230 return True;
12231 end Enumeration_Case;
12233 -- Any other types have the same representation for these purposes
12235 else
12236 return True;
12237 end if;
12238 end Same_Representation;
12240 --------------------------------
12241 -- Resolve_Iterable_Operation --
12242 --------------------------------
12244 procedure Resolve_Iterable_Operation
12245 (N : Node_Id;
12246 Cursor : Entity_Id;
12247 Typ : Entity_Id;
12248 Nam : Name_Id)
12250 Ent : Entity_Id;
12251 F1 : Entity_Id;
12252 F2 : Entity_Id;
12254 begin
12255 if not Is_Overloaded (N) then
12256 if not Is_Entity_Name (N)
12257 or else Ekind (Entity (N)) /= E_Function
12258 or else Scope (Entity (N)) /= Scope (Typ)
12259 or else No (First_Formal (Entity (N)))
12260 or else Etype (First_Formal (Entity (N))) /= Typ
12261 then
12262 Error_Msg_N ("iterable primitive must be local function name "
12263 & "whose first formal is an iterable type", N);
12264 return;
12265 end if;
12267 Ent := Entity (N);
12268 F1 := First_Formal (Ent);
12269 if Nam = Name_First then
12271 -- First (Container) => Cursor
12273 if Etype (Ent) /= Cursor then
12274 Error_Msg_N ("primitive for First must yield a curosr", N);
12275 end if;
12277 elsif Nam = Name_Next then
12279 -- Next (Container, Cursor) => Cursor
12281 F2 := Next_Formal (F1);
12283 if Etype (F2) /= Cursor
12284 or else Etype (Ent) /= Cursor
12285 or else Present (Next_Formal (F2))
12286 then
12287 Error_Msg_N ("no match for Next iterable primitive", N);
12288 end if;
12290 elsif Nam = Name_Has_Element then
12292 -- Has_Element (Container, Cursor) => Boolean
12294 F2 := Next_Formal (F1);
12295 if Etype (F2) /= Cursor
12296 or else Etype (Ent) /= Standard_Boolean
12297 or else Present (Next_Formal (F2))
12298 then
12299 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12300 end if;
12302 elsif Nam = Name_Element then
12303 F2 := Next_Formal (F1);
12305 if No (F2)
12306 or else Etype (F2) /= Cursor
12307 or else Present (Next_Formal (F2))
12308 then
12309 Error_Msg_N ("no match for Element iterable primitive", N);
12310 end if;
12311 null;
12313 else
12314 raise Program_Error;
12315 end if;
12317 else
12318 -- Overloaded case: find subprogram with proper signature.
12319 -- Caller will report error if no match is found.
12321 declare
12322 I : Interp_Index;
12323 It : Interp;
12325 begin
12326 Get_First_Interp (N, I, It);
12327 while Present (It.Typ) loop
12328 if Ekind (It.Nam) = E_Function
12329 and then Scope (It.Nam) = Scope (Typ)
12330 and then Etype (First_Formal (It.Nam)) = Typ
12331 then
12332 F1 := First_Formal (It.Nam);
12334 if Nam = Name_First then
12335 if Etype (It.Nam) = Cursor
12336 and then No (Next_Formal (F1))
12337 then
12338 Set_Entity (N, It.Nam);
12339 exit;
12340 end if;
12342 elsif Nam = Name_Next then
12343 F2 := Next_Formal (F1);
12345 if Present (F2)
12346 and then No (Next_Formal (F2))
12347 and then Etype (F2) = Cursor
12348 and then Etype (It.Nam) = Cursor
12349 then
12350 Set_Entity (N, It.Nam);
12351 exit;
12352 end if;
12354 elsif Nam = Name_Has_Element then
12355 F2 := Next_Formal (F1);
12357 if Present (F2)
12358 and then No (Next_Formal (F2))
12359 and then Etype (F2) = Cursor
12360 and then Etype (It.Nam) = Standard_Boolean
12361 then
12362 Set_Entity (N, It.Nam);
12363 F2 := Next_Formal (F1);
12364 exit;
12365 end if;
12367 elsif Nam = Name_Element then
12368 F2 := Next_Formal (F1);
12370 if Present (F2)
12371 and then No (Next_Formal (F2))
12372 and then Etype (F2) = Cursor
12373 then
12374 Set_Entity (N, It.Nam);
12375 exit;
12376 end if;
12377 end if;
12378 end if;
12380 Get_Next_Interp (I, It);
12381 end loop;
12382 end;
12383 end if;
12384 end Resolve_Iterable_Operation;
12386 ----------------
12387 -- Set_Biased --
12388 ----------------
12390 procedure Set_Biased
12391 (E : Entity_Id;
12392 N : Node_Id;
12393 Msg : String;
12394 Biased : Boolean := True)
12396 begin
12397 if Biased then
12398 Set_Has_Biased_Representation (E);
12400 if Warn_On_Biased_Representation then
12401 Error_Msg_NE
12402 ("?B?" & Msg & " forces biased representation for&", N, E);
12403 end if;
12404 end if;
12405 end Set_Biased;
12407 --------------------
12408 -- Set_Enum_Esize --
12409 --------------------
12411 procedure Set_Enum_Esize (T : Entity_Id) is
12412 Lo : Uint;
12413 Hi : Uint;
12414 Sz : Nat;
12416 begin
12417 Init_Alignment (T);
12419 -- Find the minimum standard size (8,16,32,64) that fits
12421 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12422 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12424 if Lo < 0 then
12425 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12426 Sz := Standard_Character_Size; -- May be > 8 on some targets
12428 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12429 Sz := 16;
12431 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12432 Sz := 32;
12434 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12435 Sz := 64;
12436 end if;
12438 else
12439 if Hi < Uint_2**08 then
12440 Sz := Standard_Character_Size; -- May be > 8 on some targets
12442 elsif Hi < Uint_2**16 then
12443 Sz := 16;
12445 elsif Hi < Uint_2**32 then
12446 Sz := 32;
12448 else pragma Assert (Hi < Uint_2**63);
12449 Sz := 64;
12450 end if;
12451 end if;
12453 -- That minimum is the proper size unless we have a foreign convention
12454 -- and the size required is 32 or less, in which case we bump the size
12455 -- up to 32. This is required for C and C++ and seems reasonable for
12456 -- all other foreign conventions.
12458 if Has_Foreign_Convention (T)
12459 and then Esize (T) < Standard_Integer_Size
12461 -- Don't do this if Short_Enums on target
12463 and then not Target_Short_Enums
12464 then
12465 Init_Esize (T, Standard_Integer_Size);
12466 else
12467 Init_Esize (T, Sz);
12468 end if;
12469 end Set_Enum_Esize;
12471 -----------------------------
12472 -- Uninstall_Discriminants --
12473 -----------------------------
12475 procedure Uninstall_Discriminants (E : Entity_Id) is
12476 Disc : Entity_Id;
12477 Prev : Entity_Id;
12478 Outer : Entity_Id;
12480 begin
12481 -- Discriminants have been made visible for type declarations and
12482 -- protected type declarations, not for subtype declarations.
12484 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12485 Disc := First_Discriminant (E);
12486 while Present (Disc) loop
12487 if Disc /= Current_Entity (Disc) then
12488 Prev := Current_Entity (Disc);
12489 while Present (Prev)
12490 and then Present (Homonym (Prev))
12491 and then Homonym (Prev) /= Disc
12492 loop
12493 Prev := Homonym (Prev);
12494 end loop;
12495 else
12496 Prev := Empty;
12497 end if;
12499 Set_Is_Immediately_Visible (Disc, False);
12501 Outer := Homonym (Disc);
12502 while Present (Outer) and then Scope (Outer) = E loop
12503 Outer := Homonym (Outer);
12504 end loop;
12506 -- Reset homonym link of other entities, but do not modify link
12507 -- between entities in current scope, so that the back-end can
12508 -- have a proper count of local overloadings.
12510 if No (Prev) then
12511 Set_Name_Entity_Id (Chars (Disc), Outer);
12513 elsif Scope (Prev) /= Scope (Disc) then
12514 Set_Homonym (Prev, Outer);
12515 end if;
12517 Next_Discriminant (Disc);
12518 end loop;
12519 end if;
12520 end Uninstall_Discriminants;
12522 -------------------------------------------
12523 -- Uninstall_Discriminants_And_Pop_Scope --
12524 -------------------------------------------
12526 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12527 begin
12528 if Has_Discriminants (E) then
12529 Uninstall_Discriminants (E);
12530 Pop_Scope;
12531 end if;
12532 end Uninstall_Discriminants_And_Pop_Scope;
12534 ------------------------------
12535 -- Validate_Address_Clauses --
12536 ------------------------------
12538 procedure Validate_Address_Clauses is
12539 begin
12540 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12541 declare
12542 ACCR : Address_Clause_Check_Record
12543 renames Address_Clause_Checks.Table (J);
12545 Expr : Node_Id;
12547 X_Alignment : Uint;
12548 Y_Alignment : Uint;
12550 X_Size : Uint;
12551 Y_Size : Uint;
12553 begin
12554 -- Skip processing of this entry if warning already posted
12556 if not Address_Warning_Posted (ACCR.N) then
12557 Expr := Original_Node (Expression (ACCR.N));
12559 -- Get alignments
12561 X_Alignment := Alignment (ACCR.X);
12562 Y_Alignment := Alignment (ACCR.Y);
12564 -- Similarly obtain sizes
12566 X_Size := Esize (ACCR.X);
12567 Y_Size := Esize (ACCR.Y);
12569 -- Check for large object overlaying smaller one
12571 if Y_Size > Uint_0
12572 and then X_Size > Uint_0
12573 and then X_Size > Y_Size
12574 then
12575 Error_Msg_NE
12576 ("??& overlays smaller object", ACCR.N, ACCR.X);
12577 Error_Msg_N
12578 ("\??program execution may be erroneous", ACCR.N);
12579 Error_Msg_Uint_1 := X_Size;
12580 Error_Msg_NE
12581 ("\??size of & is ^", ACCR.N, ACCR.X);
12582 Error_Msg_Uint_1 := Y_Size;
12583 Error_Msg_NE
12584 ("\??size of & is ^", ACCR.N, ACCR.Y);
12586 -- Check for inadequate alignment, both of the base object
12587 -- and of the offset, if any.
12589 -- Note: we do not check the alignment if we gave a size
12590 -- warning, since it would likely be redundant.
12592 elsif Y_Alignment /= Uint_0
12593 and then (Y_Alignment < X_Alignment
12594 or else (ACCR.Off
12595 and then
12596 Nkind (Expr) = N_Attribute_Reference
12597 and then
12598 Attribute_Name (Expr) = Name_Address
12599 and then
12600 Has_Compatible_Alignment
12601 (ACCR.X, Prefix (Expr))
12602 /= Known_Compatible))
12603 then
12604 Error_Msg_NE
12605 ("??specified address for& may be inconsistent "
12606 & "with alignment", ACCR.N, ACCR.X);
12607 Error_Msg_N
12608 ("\??program execution may be erroneous (RM 13.3(27))",
12609 ACCR.N);
12610 Error_Msg_Uint_1 := X_Alignment;
12611 Error_Msg_NE
12612 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12613 Error_Msg_Uint_1 := Y_Alignment;
12614 Error_Msg_NE
12615 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12616 if Y_Alignment >= X_Alignment then
12617 Error_Msg_N
12618 ("\??but offset is not multiple of alignment", ACCR.N);
12619 end if;
12620 end if;
12621 end if;
12622 end;
12623 end loop;
12624 end Validate_Address_Clauses;
12626 ---------------------------
12627 -- Validate_Independence --
12628 ---------------------------
12630 procedure Validate_Independence is
12631 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12632 N : Node_Id;
12633 E : Entity_Id;
12634 IC : Boolean;
12635 Comp : Entity_Id;
12636 Addr : Node_Id;
12637 P : Node_Id;
12639 procedure Check_Array_Type (Atyp : Entity_Id);
12640 -- Checks if the array type Atyp has independent components, and
12641 -- if not, outputs an appropriate set of error messages.
12643 procedure No_Independence;
12644 -- Output message that independence cannot be guaranteed
12646 function OK_Component (C : Entity_Id) return Boolean;
12647 -- Checks one component to see if it is independently accessible, and
12648 -- if so yields True, otherwise yields False if independent access
12649 -- cannot be guaranteed. This is a conservative routine, it only
12650 -- returns True if it knows for sure, it returns False if it knows
12651 -- there is a problem, or it cannot be sure there is no problem.
12653 procedure Reason_Bad_Component (C : Entity_Id);
12654 -- Outputs continuation message if a reason can be determined for
12655 -- the component C being bad.
12657 ----------------------
12658 -- Check_Array_Type --
12659 ----------------------
12661 procedure Check_Array_Type (Atyp : Entity_Id) is
12662 Ctyp : constant Entity_Id := Component_Type (Atyp);
12664 begin
12665 -- OK if no alignment clause, no pack, and no component size
12667 if not Has_Component_Size_Clause (Atyp)
12668 and then not Has_Alignment_Clause (Atyp)
12669 and then not Is_Packed (Atyp)
12670 then
12671 return;
12672 end if;
12674 -- Case of component size is greater than or equal to 64 and the
12675 -- alignment of the array is at least as large as the alignment
12676 -- of the component. We are definitely OK in this situation.
12678 if Known_Component_Size (Atyp)
12679 and then Component_Size (Atyp) >= 64
12680 and then Known_Alignment (Atyp)
12681 and then Known_Alignment (Ctyp)
12682 and then Alignment (Atyp) >= Alignment (Ctyp)
12683 then
12684 return;
12685 end if;
12687 -- Check actual component size
12689 if not Known_Component_Size (Atyp)
12690 or else not (Addressable (Component_Size (Atyp))
12691 and then Component_Size (Atyp) < 64)
12692 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12693 then
12694 No_Independence;
12696 -- Bad component size, check reason
12698 if Has_Component_Size_Clause (Atyp) then
12699 P := Get_Attribute_Definition_Clause
12700 (Atyp, Attribute_Component_Size);
12702 if Present (P) then
12703 Error_Msg_Sloc := Sloc (P);
12704 Error_Msg_N ("\because of Component_Size clause#", N);
12705 return;
12706 end if;
12707 end if;
12709 if Is_Packed (Atyp) then
12710 P := Get_Rep_Pragma (Atyp, Name_Pack);
12712 if Present (P) then
12713 Error_Msg_Sloc := Sloc (P);
12714 Error_Msg_N ("\because of pragma Pack#", N);
12715 return;
12716 end if;
12717 end if;
12719 -- No reason found, just return
12721 return;
12722 end if;
12724 -- Array type is OK independence-wise
12726 return;
12727 end Check_Array_Type;
12729 ---------------------
12730 -- No_Independence --
12731 ---------------------
12733 procedure No_Independence is
12734 begin
12735 if Pragma_Name (N) = Name_Independent then
12736 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
12737 else
12738 Error_Msg_NE
12739 ("independent components cannot be guaranteed for&", N, E);
12740 end if;
12741 end No_Independence;
12743 ------------------
12744 -- OK_Component --
12745 ------------------
12747 function OK_Component (C : Entity_Id) return Boolean is
12748 Rec : constant Entity_Id := Scope (C);
12749 Ctyp : constant Entity_Id := Etype (C);
12751 begin
12752 -- OK if no component clause, no Pack, and no alignment clause
12754 if No (Component_Clause (C))
12755 and then not Is_Packed (Rec)
12756 and then not Has_Alignment_Clause (Rec)
12757 then
12758 return True;
12759 end if;
12761 -- Here we look at the actual component layout. A component is
12762 -- addressable if its size is a multiple of the Esize of the
12763 -- component type, and its starting position in the record has
12764 -- appropriate alignment, and the record itself has appropriate
12765 -- alignment to guarantee the component alignment.
12767 -- Make sure sizes are static, always assume the worst for any
12768 -- cases where we cannot check static values.
12770 if not (Known_Static_Esize (C)
12771 and then
12772 Known_Static_Esize (Ctyp))
12773 then
12774 return False;
12775 end if;
12777 -- Size of component must be addressable or greater than 64 bits
12778 -- and a multiple of bytes.
12780 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
12781 return False;
12782 end if;
12784 -- Check size is proper multiple
12786 if Esize (C) mod Esize (Ctyp) /= 0 then
12787 return False;
12788 end if;
12790 -- Check alignment of component is OK
12792 if not Known_Component_Bit_Offset (C)
12793 or else Component_Bit_Offset (C) < Uint_0
12794 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
12795 then
12796 return False;
12797 end if;
12799 -- Check alignment of record type is OK
12801 if not Known_Alignment (Rec)
12802 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12803 then
12804 return False;
12805 end if;
12807 -- All tests passed, component is addressable
12809 return True;
12810 end OK_Component;
12812 --------------------------
12813 -- Reason_Bad_Component --
12814 --------------------------
12816 procedure Reason_Bad_Component (C : Entity_Id) is
12817 Rec : constant Entity_Id := Scope (C);
12818 Ctyp : constant Entity_Id := Etype (C);
12820 begin
12821 -- If component clause present assume that's the problem
12823 if Present (Component_Clause (C)) then
12824 Error_Msg_Sloc := Sloc (Component_Clause (C));
12825 Error_Msg_N ("\because of Component_Clause#", N);
12826 return;
12827 end if;
12829 -- If pragma Pack clause present, assume that's the problem
12831 if Is_Packed (Rec) then
12832 P := Get_Rep_Pragma (Rec, Name_Pack);
12834 if Present (P) then
12835 Error_Msg_Sloc := Sloc (P);
12836 Error_Msg_N ("\because of pragma Pack#", N);
12837 return;
12838 end if;
12839 end if;
12841 -- See if record has bad alignment clause
12843 if Has_Alignment_Clause (Rec)
12844 and then Known_Alignment (Rec)
12845 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12846 then
12847 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
12849 if Present (P) then
12850 Error_Msg_Sloc := Sloc (P);
12851 Error_Msg_N ("\because of Alignment clause#", N);
12852 end if;
12853 end if;
12855 -- Couldn't find a reason, so return without a message
12857 return;
12858 end Reason_Bad_Component;
12860 -- Start of processing for Validate_Independence
12862 begin
12863 for J in Independence_Checks.First .. Independence_Checks.Last loop
12864 N := Independence_Checks.Table (J).N;
12865 E := Independence_Checks.Table (J).E;
12866 IC := Pragma_Name (N) = Name_Independent_Components;
12868 -- Deal with component case
12870 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
12871 if not OK_Component (E) then
12872 No_Independence;
12873 Reason_Bad_Component (E);
12874 goto Continue;
12875 end if;
12876 end if;
12878 -- Deal with record with Independent_Components
12880 if IC and then Is_Record_Type (E) then
12881 Comp := First_Component_Or_Discriminant (E);
12882 while Present (Comp) loop
12883 if not OK_Component (Comp) then
12884 No_Independence;
12885 Reason_Bad_Component (Comp);
12886 goto Continue;
12887 end if;
12889 Next_Component_Or_Discriminant (Comp);
12890 end loop;
12891 end if;
12893 -- Deal with address clause case
12895 if Is_Object (E) then
12896 Addr := Address_Clause (E);
12898 if Present (Addr) then
12899 No_Independence;
12900 Error_Msg_Sloc := Sloc (Addr);
12901 Error_Msg_N ("\because of Address clause#", N);
12902 goto Continue;
12903 end if;
12904 end if;
12906 -- Deal with independent components for array type
12908 if IC and then Is_Array_Type (E) then
12909 Check_Array_Type (E);
12910 end if;
12912 -- Deal with independent components for array object
12914 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
12915 Check_Array_Type (Etype (E));
12916 end if;
12918 <<Continue>> null;
12919 end loop;
12920 end Validate_Independence;
12922 ------------------------------
12923 -- Validate_Iterable_Aspect --
12924 ------------------------------
12926 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
12927 Assoc : Node_Id;
12928 Expr : Node_Id;
12930 Prim : Node_Id;
12931 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
12933 First_Id : Entity_Id;
12934 Next_Id : Entity_Id;
12935 Has_Element_Id : Entity_Id;
12936 Element_Id : Entity_Id;
12938 begin
12939 -- If previous error aspect is unusable
12941 if Cursor = Any_Type then
12942 return;
12943 end if;
12945 First_Id := Empty;
12946 Next_Id := Empty;
12947 Has_Element_Id := Empty;
12948 Element_Id := Empty;
12950 -- Each expression must resolve to a function with the proper signature
12952 Assoc := First (Component_Associations (Expression (ASN)));
12953 while Present (Assoc) loop
12954 Expr := Expression (Assoc);
12955 Analyze (Expr);
12957 Prim := First (Choices (Assoc));
12959 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
12960 Error_Msg_N ("illegal name in association", Prim);
12962 elsif Chars (Prim) = Name_First then
12963 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
12964 First_Id := Entity (Expr);
12966 elsif Chars (Prim) = Name_Next then
12967 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
12968 Next_Id := Entity (Expr);
12970 elsif Chars (Prim) = Name_Has_Element then
12971 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
12972 Has_Element_Id := Entity (Expr);
12974 elsif Chars (Prim) = Name_Element then
12975 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
12976 Element_Id := Entity (Expr);
12978 else
12979 Error_Msg_N ("invalid name for iterable function", Prim);
12980 end if;
12982 Next (Assoc);
12983 end loop;
12985 if No (First_Id) then
12986 Error_Msg_N ("match for First primitive not found", ASN);
12988 elsif No (Next_Id) then
12989 Error_Msg_N ("match for Next primitive not found", ASN);
12991 elsif No (Has_Element_Id) then
12992 Error_Msg_N ("match for Has_Element primitive not found", ASN);
12994 elsif No (Element_Id) then
12995 null; -- Optional.
12996 end if;
12997 end Validate_Iterable_Aspect;
12999 -----------------------------------
13000 -- Validate_Unchecked_Conversion --
13001 -----------------------------------
13003 procedure Validate_Unchecked_Conversion
13004 (N : Node_Id;
13005 Act_Unit : Entity_Id)
13007 Source : Entity_Id;
13008 Target : Entity_Id;
13009 Vnode : Node_Id;
13011 begin
13012 -- Obtain source and target types. Note that we call Ancestor_Subtype
13013 -- here because the processing for generic instantiation always makes
13014 -- subtypes, and we want the original frozen actual types.
13016 -- If we are dealing with private types, then do the check on their
13017 -- fully declared counterparts if the full declarations have been
13018 -- encountered (they don't have to be visible, but they must exist).
13020 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13022 if Is_Private_Type (Source)
13023 and then Present (Underlying_Type (Source))
13024 then
13025 Source := Underlying_Type (Source);
13026 end if;
13028 Target := Ancestor_Subtype (Etype (Act_Unit));
13030 -- If either type is generic, the instantiation happens within a generic
13031 -- unit, and there is nothing to check. The proper check will happen
13032 -- when the enclosing generic is instantiated.
13034 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13035 return;
13036 end if;
13038 if Is_Private_Type (Target)
13039 and then Present (Underlying_Type (Target))
13040 then
13041 Target := Underlying_Type (Target);
13042 end if;
13044 -- Source may be unconstrained array, but not target
13046 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
13047 Error_Msg_N
13048 ("unchecked conversion to unconstrained array not allowed", N);
13049 return;
13050 end if;
13052 -- Warn if conversion between two different convention pointers
13054 if Is_Access_Type (Target)
13055 and then Is_Access_Type (Source)
13056 and then Convention (Target) /= Convention (Source)
13057 and then Warn_On_Unchecked_Conversion
13058 then
13059 -- Give warnings for subprogram pointers only on most targets
13061 if Is_Access_Subprogram_Type (Target)
13062 or else Is_Access_Subprogram_Type (Source)
13063 then
13064 Error_Msg_N
13065 ("?z?conversion between pointers with different conventions!",
13067 end if;
13068 end if;
13070 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13071 -- warning when compiling GNAT-related sources.
13073 if Warn_On_Unchecked_Conversion
13074 and then not In_Predefined_Unit (N)
13075 and then RTU_Loaded (Ada_Calendar)
13076 and then (Chars (Source) = Name_Time
13077 or else
13078 Chars (Target) = Name_Time)
13079 then
13080 -- If Ada.Calendar is loaded and the name of one of the operands is
13081 -- Time, there is a good chance that this is Ada.Calendar.Time.
13083 declare
13084 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
13085 begin
13086 pragma Assert (Present (Calendar_Time));
13088 if Source = Calendar_Time or else Target = Calendar_Time then
13089 Error_Msg_N
13090 ("?z?representation of 'Time values may change between "
13091 & "'G'N'A'T versions", N);
13092 end if;
13093 end;
13094 end if;
13096 -- Make entry in unchecked conversion table for later processing by
13097 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13098 -- (using values set by the back-end where possible). This is only done
13099 -- if the appropriate warning is active.
13101 if Warn_On_Unchecked_Conversion then
13102 Unchecked_Conversions.Append
13103 (New_Val => UC_Entry'(Eloc => Sloc (N),
13104 Source => Source,
13105 Target => Target,
13106 Act_Unit => Act_Unit));
13108 -- If both sizes are known statically now, then back end annotation
13109 -- is not required to do a proper check but if either size is not
13110 -- known statically, then we need the annotation.
13112 if Known_Static_RM_Size (Source)
13113 and then
13114 Known_Static_RM_Size (Target)
13115 then
13116 null;
13117 else
13118 Back_Annotate_Rep_Info := True;
13119 end if;
13120 end if;
13122 -- If unchecked conversion to access type, and access type is declared
13123 -- in the same unit as the unchecked conversion, then set the flag
13124 -- No_Strict_Aliasing (no strict aliasing is implicit here)
13126 if Is_Access_Type (Target) and then
13127 In_Same_Source_Unit (Target, N)
13128 then
13129 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13130 end if;
13132 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13133 -- the back end needs to perform special validation checks.
13135 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13136 -- have full expansion and the back end is called ???
13138 Vnode :=
13139 Make_Validate_Unchecked_Conversion (Sloc (N));
13140 Set_Source_Type (Vnode, Source);
13141 Set_Target_Type (Vnode, Target);
13143 -- If the unchecked conversion node is in a list, just insert before it.
13144 -- If not we have some strange case, not worth bothering about.
13146 if Is_List_Member (N) then
13147 Insert_After (N, Vnode);
13148 end if;
13149 end Validate_Unchecked_Conversion;
13151 ------------------------------------
13152 -- Validate_Unchecked_Conversions --
13153 ------------------------------------
13155 procedure Validate_Unchecked_Conversions is
13156 begin
13157 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13158 declare
13159 T : UC_Entry renames Unchecked_Conversions.Table (N);
13161 Eloc : constant Source_Ptr := T.Eloc;
13162 Source : constant Entity_Id := T.Source;
13163 Target : constant Entity_Id := T.Target;
13164 Act_Unit : constant Entity_Id := T.Act_Unit;
13166 Source_Siz : Uint;
13167 Target_Siz : Uint;
13169 begin
13170 -- Skip if function marked as warnings off
13172 if Warnings_Off (Act_Unit) then
13173 goto Continue;
13174 end if;
13176 -- This validation check, which warns if we have unequal sizes for
13177 -- unchecked conversion, and thus potentially implementation
13178 -- dependent semantics, is one of the few occasions on which we
13179 -- use the official RM size instead of Esize. See description in
13180 -- Einfo "Handling of Type'Size Values" for details.
13182 if Serious_Errors_Detected = 0
13183 and then Known_Static_RM_Size (Source)
13184 and then Known_Static_RM_Size (Target)
13186 -- Don't do the check if warnings off for either type, note the
13187 -- deliberate use of OR here instead of OR ELSE to get the flag
13188 -- Warnings_Off_Used set for both types if appropriate.
13190 and then not (Has_Warnings_Off (Source)
13192 Has_Warnings_Off (Target))
13193 then
13194 Source_Siz := RM_Size (Source);
13195 Target_Siz := RM_Size (Target);
13197 if Source_Siz /= Target_Siz then
13198 Error_Msg
13199 ("?z?types for unchecked conversion have different sizes!",
13200 Eloc);
13202 if All_Errors_Mode then
13203 Error_Msg_Name_1 := Chars (Source);
13204 Error_Msg_Uint_1 := Source_Siz;
13205 Error_Msg_Name_2 := Chars (Target);
13206 Error_Msg_Uint_2 := Target_Siz;
13207 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
13209 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13211 if Is_Discrete_Type (Source)
13212 and then
13213 Is_Discrete_Type (Target)
13214 then
13215 if Source_Siz > Target_Siz then
13216 Error_Msg
13217 ("\?z?^ high order bits of source will "
13218 & "be ignored!", Eloc);
13220 elsif Is_Unsigned_Type (Source) then
13221 Error_Msg
13222 ("\?z?source will be extended with ^ high order "
13223 & "zero bits!", Eloc);
13225 else
13226 Error_Msg
13227 ("\?z?source will be extended with ^ high order "
13228 & "sign bits!", Eloc);
13229 end if;
13231 elsif Source_Siz < Target_Siz then
13232 if Is_Discrete_Type (Target) then
13233 if Bytes_Big_Endian then
13234 Error_Msg
13235 ("\?z?target value will include ^ undefined "
13236 & "low order bits!", Eloc);
13237 else
13238 Error_Msg
13239 ("\?z?target value will include ^ undefined "
13240 & "high order bits!", Eloc);
13241 end if;
13243 else
13244 Error_Msg
13245 ("\?z?^ trailing bits of target value will be "
13246 & "undefined!", Eloc);
13247 end if;
13249 else pragma Assert (Source_Siz > Target_Siz);
13250 Error_Msg
13251 ("\?z?^ trailing bits of source will be ignored!",
13252 Eloc);
13253 end if;
13254 end if;
13255 end if;
13256 end if;
13258 -- If both types are access types, we need to check the alignment.
13259 -- If the alignment of both is specified, we can do it here.
13261 if Serious_Errors_Detected = 0
13262 and then Is_Access_Type (Source)
13263 and then Is_Access_Type (Target)
13264 and then Target_Strict_Alignment
13265 and then Present (Designated_Type (Source))
13266 and then Present (Designated_Type (Target))
13267 then
13268 declare
13269 D_Source : constant Entity_Id := Designated_Type (Source);
13270 D_Target : constant Entity_Id := Designated_Type (Target);
13272 begin
13273 if Known_Alignment (D_Source)
13274 and then
13275 Known_Alignment (D_Target)
13276 then
13277 declare
13278 Source_Align : constant Uint := Alignment (D_Source);
13279 Target_Align : constant Uint := Alignment (D_Target);
13281 begin
13282 if Source_Align < Target_Align
13283 and then not Is_Tagged_Type (D_Source)
13285 -- Suppress warning if warnings suppressed on either
13286 -- type or either designated type. Note the use of
13287 -- OR here instead of OR ELSE. That is intentional,
13288 -- we would like to set flag Warnings_Off_Used in
13289 -- all types for which warnings are suppressed.
13291 and then not (Has_Warnings_Off (D_Source)
13293 Has_Warnings_Off (D_Target)
13295 Has_Warnings_Off (Source)
13297 Has_Warnings_Off (Target))
13298 then
13299 Error_Msg_Uint_1 := Target_Align;
13300 Error_Msg_Uint_2 := Source_Align;
13301 Error_Msg_Node_1 := D_Target;
13302 Error_Msg_Node_2 := D_Source;
13303 Error_Msg
13304 ("?z?alignment of & (^) is stricter than "
13305 & "alignment of & (^)!", Eloc);
13306 Error_Msg
13307 ("\?z?resulting access value may have invalid "
13308 & "alignment!", Eloc);
13309 end if;
13310 end;
13311 end if;
13312 end;
13313 end if;
13314 end;
13316 <<Continue>>
13317 null;
13318 end loop;
13319 end Validate_Unchecked_Conversions;
13321 end Sem_Ch13;