2014-10-17 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch13.adb
blobba5a1ee9c224f92c013f97e3bae5af18a28ff5cd
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-2014, 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 freezing 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 when Aspect_Iterable =>
1145 Validate_Iterable_Aspect (E, ASN);
1147 when others =>
1148 null;
1149 end case;
1151 Ritem := Aspect_Rep_Item (ASN);
1153 if Present (Ritem) then
1154 Analyze (Ritem);
1155 end if;
1156 end if;
1157 end if;
1159 Next_Rep_Item (ASN);
1160 end loop;
1162 -- This is where we inherit delayed rep aspects from our parent. Note
1163 -- that if we fell out of the above loop with ASN non-empty, it means
1164 -- we hit an aspect for an entity other than E, and it must be the
1165 -- type from which we were derived.
1167 if May_Inherit_Delayed_Rep_Aspects (E) then
1168 Inherit_Delayed_Rep_Aspects (ASN);
1169 end if;
1170 end Analyze_Aspects_At_Freeze_Point;
1172 -----------------------------------
1173 -- Analyze_Aspect_Specifications --
1174 -----------------------------------
1176 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1177 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1178 -- Establish linkages between an aspect and its corresponding
1179 -- pragma.
1181 procedure Insert_After_SPARK_Mode
1182 (Prag : Node_Id;
1183 Ins_Nod : Node_Id;
1184 Decls : List_Id);
1185 -- Subsidiary to the analysis of aspects Abstract_State, Initializes,
1186 -- Initial_Condition and Refined_State. Insert node Prag before node
1187 -- Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip SPARK_Mode.
1188 -- Decls is the associated declarative list where Prag is to reside.
1190 procedure Insert_Pragma (Prag : Node_Id);
1191 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1192 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1193 -- Insert pragma Prag such that it mimics the placement of a source
1194 -- pragma of the same kind.
1196 -- procedure Proc (Formal : ...) with Global => ...;
1198 -- procedure Proc (Formal : ...);
1199 -- pragma Global (...);
1201 --------------
1202 -- Decorate --
1203 --------------
1205 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1206 begin
1207 Set_Aspect_Rep_Item (Asp, Prag);
1208 Set_Corresponding_Aspect (Prag, Asp);
1209 Set_From_Aspect_Specification (Prag);
1210 Set_Parent (Prag, Asp);
1211 end Decorate;
1213 -----------------------------
1214 -- Insert_After_SPARK_Mode --
1215 -----------------------------
1217 procedure Insert_After_SPARK_Mode
1218 (Prag : Node_Id;
1219 Ins_Nod : Node_Id;
1220 Decls : List_Id)
1222 Decl : Node_Id := Ins_Nod;
1224 begin
1225 -- Skip SPARK_Mode
1227 if Present (Decl)
1228 and then Nkind (Decl) = N_Pragma
1229 and then Pragma_Name (Decl) = Name_SPARK_Mode
1230 then
1231 Decl := Next (Decl);
1232 end if;
1234 if Present (Decl) then
1235 Insert_Before (Decl, Prag);
1237 -- Aitem acts as the last declaration
1239 else
1240 Append_To (Decls, Prag);
1241 end if;
1242 end Insert_After_SPARK_Mode;
1244 -------------------
1245 -- Insert_Pragma --
1246 -------------------
1248 procedure Insert_Pragma (Prag : Node_Id) is
1249 Aux : Node_Id;
1250 Decl : Node_Id;
1252 begin
1253 -- When the context is a library unit, the pragma is added to the
1254 -- Pragmas_After list.
1256 if Nkind (Parent (N)) = N_Compilation_Unit then
1257 Aux := Aux_Decls_Node (Parent (N));
1259 if No (Pragmas_After (Aux)) then
1260 Set_Pragmas_After (Aux, New_List);
1261 end if;
1263 Prepend (Prag, Pragmas_After (Aux));
1265 -- Pragmas associated with subprogram bodies are inserted in the
1266 -- declarative part.
1268 elsif Nkind (N) = N_Subprogram_Body then
1269 if Present (Declarations (N)) then
1271 -- Skip other internally generated pragmas from aspects to find
1272 -- the proper insertion point. As a result the order of pragmas
1273 -- is the same as the order of aspects.
1275 -- As precondition pragmas generated from conjuncts in the
1276 -- precondition aspect are presented in reverse order to
1277 -- Insert_Pragma, insert them in the correct order here by not
1278 -- skipping previously inserted precondition pragmas when the
1279 -- current pragma is a precondition.
1281 Decl := First (Declarations (N));
1282 while Present (Decl) loop
1283 if Nkind (Decl) = N_Pragma
1284 and then From_Aspect_Specification (Decl)
1285 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1286 and then
1287 Get_Pragma_Id (Prag) = Pragma_Precondition)
1288 then
1289 Next (Decl);
1290 else
1291 exit;
1292 end if;
1293 end loop;
1295 if Present (Decl) then
1296 Insert_Before (Decl, Prag);
1297 else
1298 Append (Prag, Declarations (N));
1299 end if;
1300 else
1301 Set_Declarations (N, New_List (Prag));
1302 end if;
1304 -- Default
1306 else
1307 Insert_After (N, Prag);
1308 end if;
1309 end Insert_Pragma;
1311 -- Local variables
1313 Aspect : Node_Id;
1314 Aitem : Node_Id;
1315 Ent : Node_Id;
1317 L : constant List_Id := Aspect_Specifications (N);
1319 Ins_Node : Node_Id := N;
1320 -- Insert pragmas/attribute definition clause after this node when no
1321 -- delayed analysis is required.
1323 -- Start of processing for Analyze_Aspect_Specifications
1325 -- The general processing involves building an attribute definition
1326 -- clause or a pragma node that corresponds to the aspect. Then in order
1327 -- to delay the evaluation of this aspect to the freeze point, we attach
1328 -- the corresponding pragma/attribute definition clause to the aspect
1329 -- specification node, which is then placed in the Rep Item chain. In
1330 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1331 -- and we evaluate the rep item at the freeze point. When the aspect
1332 -- doesn't have a corresponding pragma/attribute definition clause, then
1333 -- its analysis is simply delayed at the freeze point.
1335 -- Some special cases don't require delay analysis, thus the aspect is
1336 -- analyzed right now.
1338 -- Note that there is a special handling for Pre, Post, Test_Case,
1339 -- Contract_Cases aspects. In these cases, we do not have to worry
1340 -- about delay issues, since the pragmas themselves deal with delay
1341 -- of visibility for the expression analysis. Thus, we just insert
1342 -- the pragma after the node N.
1344 begin
1345 pragma Assert (Present (L));
1347 -- Loop through aspects
1349 Aspect := First (L);
1350 Aspect_Loop : while Present (Aspect) loop
1351 Analyze_One_Aspect : declare
1352 Expr : constant Node_Id := Expression (Aspect);
1353 Id : constant Node_Id := Identifier (Aspect);
1354 Loc : constant Source_Ptr := Sloc (Aspect);
1355 Nam : constant Name_Id := Chars (Id);
1356 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1357 Anod : Node_Id;
1359 Delay_Required : Boolean;
1360 -- Set False if delay is not required
1362 Eloc : Source_Ptr := No_Location;
1363 -- Source location of expression, modified when we split PPC's. It
1364 -- is set below when Expr is present.
1366 procedure Analyze_Aspect_External_Or_Link_Name;
1367 -- Perform analysis of the External_Name or Link_Name aspects
1369 procedure Analyze_Aspect_Implicit_Dereference;
1370 -- Perform analysis of the Implicit_Dereference aspects
1372 procedure Make_Aitem_Pragma
1373 (Pragma_Argument_Associations : List_Id;
1374 Pragma_Name : Name_Id);
1375 -- This is a wrapper for Make_Pragma used for converting aspects
1376 -- to pragmas. It takes care of Sloc (set from Loc) and building
1377 -- the pragma identifier from the given name. In addition the
1378 -- flags Class_Present and Split_PPC are set from the aspect
1379 -- node, as well as Is_Ignored. This routine also sets the
1380 -- From_Aspect_Specification in the resulting pragma node to
1381 -- True, and sets Corresponding_Aspect to point to the aspect.
1382 -- The resulting pragma is assigned to Aitem.
1384 ------------------------------------------
1385 -- Analyze_Aspect_External_Or_Link_Name --
1386 ------------------------------------------
1388 procedure Analyze_Aspect_External_Or_Link_Name is
1389 begin
1390 -- Verify that there is an Import/Export aspect defined for the
1391 -- entity. The processing of that aspect in turn checks that
1392 -- there is a Convention aspect declared. The pragma is
1393 -- constructed when processing the Convention aspect.
1395 declare
1396 A : Node_Id;
1398 begin
1399 A := First (L);
1400 while Present (A) loop
1401 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1402 Name_Import);
1403 Next (A);
1404 end loop;
1406 if No (A) then
1407 Error_Msg_N
1408 ("missing Import/Export for Link/External name",
1409 Aspect);
1410 end if;
1411 end;
1412 end Analyze_Aspect_External_Or_Link_Name;
1414 -----------------------------------------
1415 -- Analyze_Aspect_Implicit_Dereference --
1416 -----------------------------------------
1418 procedure Analyze_Aspect_Implicit_Dereference is
1419 begin
1420 if not Is_Type (E) or else not Has_Discriminants (E) then
1421 Error_Msg_N
1422 ("aspect must apply to a type with discriminants", N);
1424 else
1425 declare
1426 Disc : Entity_Id;
1428 begin
1429 Disc := First_Discriminant (E);
1430 while Present (Disc) loop
1431 if Chars (Expr) = Chars (Disc)
1432 and then Ekind (Etype (Disc)) =
1433 E_Anonymous_Access_Type
1434 then
1435 Set_Has_Implicit_Dereference (E);
1436 Set_Has_Implicit_Dereference (Disc);
1437 return;
1438 end if;
1440 Next_Discriminant (Disc);
1441 end loop;
1443 -- Error if no proper access discriminant.
1445 Error_Msg_NE
1446 ("not an access discriminant of&", Expr, E);
1447 end;
1448 end if;
1449 end Analyze_Aspect_Implicit_Dereference;
1451 -----------------------
1452 -- Make_Aitem_Pragma --
1453 -----------------------
1455 procedure Make_Aitem_Pragma
1456 (Pragma_Argument_Associations : List_Id;
1457 Pragma_Name : Name_Id)
1459 Args : List_Id := Pragma_Argument_Associations;
1461 begin
1462 -- We should never get here if aspect was disabled
1464 pragma Assert (not Is_Disabled (Aspect));
1466 -- Certain aspects allow for an optional name or expression. Do
1467 -- not generate a pragma with empty argument association list.
1469 if No (Args) or else No (Expression (First (Args))) then
1470 Args := No_List;
1471 end if;
1473 -- Build the pragma
1475 Aitem :=
1476 Make_Pragma (Loc,
1477 Pragma_Argument_Associations => Args,
1478 Pragma_Identifier =>
1479 Make_Identifier (Sloc (Id), Pragma_Name),
1480 Class_Present => Class_Present (Aspect),
1481 Split_PPC => Split_PPC (Aspect));
1483 -- Set additional semantic fields
1485 if Is_Ignored (Aspect) then
1486 Set_Is_Ignored (Aitem);
1487 elsif Is_Checked (Aspect) then
1488 Set_Is_Checked (Aitem);
1489 end if;
1491 Set_Corresponding_Aspect (Aitem, Aspect);
1492 Set_From_Aspect_Specification (Aitem, True);
1493 end Make_Aitem_Pragma;
1495 -- Start of processing for Analyze_One_Aspect
1497 begin
1498 -- Skip aspect if already analyzed, to avoid looping in some cases
1500 if Analyzed (Aspect) then
1501 goto Continue;
1502 end if;
1504 -- Skip looking at aspect if it is totally disabled. Just mark it
1505 -- as such for later reference in the tree. This also sets the
1506 -- Is_Ignored and Is_Checked flags appropriately.
1508 Check_Applicable_Policy (Aspect);
1510 if Is_Disabled (Aspect) then
1511 goto Continue;
1512 end if;
1514 -- Set the source location of expression, used in the case of
1515 -- a failed precondition/postcondition or invariant. Note that
1516 -- the source location of the expression is not usually the best
1517 -- choice here. For example, it gets located on the last AND
1518 -- keyword in a chain of boolean expressiond AND'ed together.
1519 -- It is best to put the message on the first character of the
1520 -- assertion, which is the effect of the First_Node call here.
1522 if Present (Expr) then
1523 Eloc := Sloc (First_Node (Expr));
1524 end if;
1526 -- Check restriction No_Implementation_Aspect_Specifications
1528 if Implementation_Defined_Aspect (A_Id) then
1529 Check_Restriction
1530 (No_Implementation_Aspect_Specifications, Aspect);
1531 end if;
1533 -- Check restriction No_Specification_Of_Aspect
1535 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1537 -- Mark aspect analyzed (actual analysis is delayed till later)
1539 Set_Analyzed (Aspect);
1540 Set_Entity (Aspect, E);
1541 Ent := New_Occurrence_Of (E, Sloc (Id));
1543 -- Check for duplicate aspect. Note that the Comes_From_Source
1544 -- test allows duplicate Pre/Post's that we generate internally
1545 -- to escape being flagged here.
1547 if No_Duplicates_Allowed (A_Id) then
1548 Anod := First (L);
1549 while Anod /= Aspect loop
1550 if Comes_From_Source (Aspect)
1551 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1552 then
1553 Error_Msg_Name_1 := Nam;
1554 Error_Msg_Sloc := Sloc (Anod);
1556 -- Case of same aspect specified twice
1558 if Class_Present (Anod) = Class_Present (Aspect) then
1559 if not Class_Present (Anod) then
1560 Error_Msg_NE
1561 ("aspect% for & previously given#",
1562 Id, E);
1563 else
1564 Error_Msg_NE
1565 ("aspect `%''Class` for & previously given#",
1566 Id, E);
1567 end if;
1568 end if;
1569 end if;
1571 Next (Anod);
1572 end loop;
1573 end if;
1575 -- Check some general restrictions on language defined aspects
1577 if not Implementation_Defined_Aspect (A_Id) then
1578 Error_Msg_Name_1 := Nam;
1580 -- Not allowed for renaming declarations
1582 if Nkind (N) in N_Renaming_Declaration then
1583 Error_Msg_N
1584 ("aspect % not allowed for renaming declaration",
1585 Aspect);
1586 end if;
1588 -- Not allowed for formal type declarations
1590 if Nkind (N) = N_Formal_Type_Declaration then
1591 Error_Msg_N
1592 ("aspect % not allowed for formal type declaration",
1593 Aspect);
1594 end if;
1595 end if;
1597 -- Copy expression for later processing by the procedures
1598 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1600 Set_Entity (Id, New_Copy_Tree (Expr));
1602 -- Set Delay_Required as appropriate to aspect
1604 case Aspect_Delay (A_Id) is
1605 when Always_Delay =>
1606 Delay_Required := True;
1608 when Never_Delay =>
1609 Delay_Required := False;
1611 when Rep_Aspect =>
1613 -- If expression has the form of an integer literal, then
1614 -- do not delay, since we know the value cannot change.
1615 -- This optimization catches most rep clause cases.
1617 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1618 or else (A_Id in Boolean_Aspects and then No (Expr))
1619 then
1620 Delay_Required := False;
1621 else
1622 Delay_Required := True;
1623 Set_Has_Delayed_Rep_Aspects (E);
1624 end if;
1625 end case;
1627 -- Processing based on specific aspect
1629 case A_Id is
1631 -- No_Aspect should be impossible
1633 when No_Aspect =>
1634 raise Program_Error;
1636 -- Case 1: Aspects corresponding to attribute definition
1637 -- clauses.
1639 when Aspect_Address |
1640 Aspect_Alignment |
1641 Aspect_Bit_Order |
1642 Aspect_Component_Size |
1643 Aspect_Constant_Indexing |
1644 Aspect_Default_Iterator |
1645 Aspect_Dispatching_Domain |
1646 Aspect_External_Tag |
1647 Aspect_Input |
1648 Aspect_Iterable |
1649 Aspect_Iterator_Element |
1650 Aspect_Machine_Radix |
1651 Aspect_Object_Size |
1652 Aspect_Output |
1653 Aspect_Read |
1654 Aspect_Scalar_Storage_Order |
1655 Aspect_Size |
1656 Aspect_Small |
1657 Aspect_Simple_Storage_Pool |
1658 Aspect_Storage_Pool |
1659 Aspect_Stream_Size |
1660 Aspect_Value_Size |
1661 Aspect_Variable_Indexing |
1662 Aspect_Write =>
1664 -- Indexing aspects apply only to tagged type
1666 if (A_Id = Aspect_Constant_Indexing
1667 or else
1668 A_Id = Aspect_Variable_Indexing)
1669 and then not (Is_Type (E)
1670 and then Is_Tagged_Type (E))
1671 then
1672 Error_Msg_N
1673 ("indexing aspect can only apply to a tagged type",
1674 Aspect);
1675 goto Continue;
1676 end if;
1678 -- For the case of aspect Address, we don't consider that we
1679 -- know the entity is never set in the source, since it is
1680 -- is likely aliasing is occurring.
1682 -- Note: one might think that the analysis of the resulting
1683 -- attribute definition clause would take care of that, but
1684 -- that's not the case since it won't be from source.
1686 if A_Id = Aspect_Address then
1687 Set_Never_Set_In_Source (E, False);
1688 end if;
1690 -- Correctness of the profile of a stream operation is
1691 -- verified at the freeze point, but we must detect the
1692 -- illegal specification of this aspect for a subtype now,
1693 -- to prevent malformed rep_item chains.
1695 if (A_Id = Aspect_Input or else
1696 A_Id = Aspect_Output or else
1697 A_Id = Aspect_Read or else
1698 A_Id = Aspect_Write)
1699 and not Is_First_Subtype (E)
1700 then
1701 Error_Msg_N
1702 ("local name must be a first subtype", Aspect);
1703 goto Continue;
1704 end if;
1706 -- Construct the attribute definition clause
1708 Aitem :=
1709 Make_Attribute_Definition_Clause (Loc,
1710 Name => Ent,
1711 Chars => Chars (Id),
1712 Expression => Relocate_Node (Expr));
1714 -- If the address is specified, then we treat the entity as
1715 -- referenced, to avoid spurious warnings. This is analogous
1716 -- to what is done with an attribute definition clause, but
1717 -- here we don't want to generate a reference because this
1718 -- is the point of definition of the entity.
1720 if A_Id = Aspect_Address then
1721 Set_Referenced (E);
1722 end if;
1724 -- Case 2: Aspects corresponding to pragmas
1726 -- Case 2a: Aspects corresponding to pragmas with two
1727 -- arguments, where the first argument is a local name
1728 -- referring to the entity, and the second argument is the
1729 -- aspect definition expression.
1731 -- Linker_Section/Suppress/Unsuppress
1733 when Aspect_Linker_Section |
1734 Aspect_Suppress |
1735 Aspect_Unsuppress =>
1737 Make_Aitem_Pragma
1738 (Pragma_Argument_Associations => New_List (
1739 Make_Pragma_Argument_Association (Loc,
1740 Expression => New_Occurrence_Of (E, Loc)),
1741 Make_Pragma_Argument_Association (Sloc (Expr),
1742 Expression => Relocate_Node (Expr))),
1743 Pragma_Name => Chars (Id));
1745 -- Synchronization
1747 -- Corresponds to pragma Implemented, construct the pragma
1749 when Aspect_Synchronization =>
1750 Make_Aitem_Pragma
1751 (Pragma_Argument_Associations => New_List (
1752 Make_Pragma_Argument_Association (Loc,
1753 Expression => New_Occurrence_Of (E, Loc)),
1754 Make_Pragma_Argument_Association (Sloc (Expr),
1755 Expression => Relocate_Node (Expr))),
1756 Pragma_Name => Name_Implemented);
1758 -- Attach_Handler
1760 when Aspect_Attach_Handler =>
1761 Make_Aitem_Pragma
1762 (Pragma_Argument_Associations => New_List (
1763 Make_Pragma_Argument_Association (Sloc (Ent),
1764 Expression => Ent),
1765 Make_Pragma_Argument_Association (Sloc (Expr),
1766 Expression => Relocate_Node (Expr))),
1767 Pragma_Name => Name_Attach_Handler);
1769 -- We need to insert this pragma into the tree to get proper
1770 -- processing and to look valid from a placement viewpoint.
1772 Insert_Pragma (Aitem);
1773 goto Continue;
1775 -- Dynamic_Predicate, Predicate, Static_Predicate
1777 when Aspect_Dynamic_Predicate |
1778 Aspect_Predicate |
1779 Aspect_Static_Predicate =>
1781 -- These aspects apply only to subtypes
1783 if not Is_Type (E) then
1784 Error_Msg_N
1785 ("predicate can only be specified for a subtype",
1786 Aspect);
1787 goto Continue;
1789 elsif Is_Incomplete_Type (E) then
1790 Error_Msg_N
1791 ("predicate cannot apply to incomplete view", Aspect);
1792 goto Continue;
1793 end if;
1795 -- Construct the pragma (always a pragma Predicate, with
1796 -- flags recording whether it is static/dynamic). We also
1797 -- set flags recording this in the type itself.
1799 Make_Aitem_Pragma
1800 (Pragma_Argument_Associations => New_List (
1801 Make_Pragma_Argument_Association (Sloc (Ent),
1802 Expression => Ent),
1803 Make_Pragma_Argument_Association (Sloc (Expr),
1804 Expression => Relocate_Node (Expr))),
1805 Pragma_Name => Name_Predicate);
1807 -- Mark type has predicates, and remember what kind of
1808 -- aspect lead to this predicate (we need this to access
1809 -- the right set of check policies later on).
1811 Set_Has_Predicates (E);
1813 if A_Id = Aspect_Dynamic_Predicate then
1814 Set_Has_Dynamic_Predicate_Aspect (E);
1815 elsif A_Id = Aspect_Static_Predicate then
1816 Set_Has_Static_Predicate_Aspect (E);
1817 end if;
1819 -- If the type is private, indicate that its completion
1820 -- has a freeze node, because that is the one that will
1821 -- be visible at freeze time.
1823 if Is_Private_Type (E) and then Present (Full_View (E)) then
1824 Set_Has_Predicates (Full_View (E));
1826 if A_Id = Aspect_Dynamic_Predicate then
1827 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1828 elsif A_Id = Aspect_Static_Predicate then
1829 Set_Has_Static_Predicate_Aspect (Full_View (E));
1830 end if;
1832 Set_Has_Delayed_Aspects (Full_View (E));
1833 Ensure_Freeze_Node (Full_View (E));
1834 end if;
1836 -- Case 2b: Aspects corresponding to pragmas with two
1837 -- arguments, where the second argument is a local name
1838 -- referring to the entity, and the first argument is the
1839 -- aspect definition expression.
1841 -- Convention
1843 when Aspect_Convention =>
1845 -- The aspect may be part of the specification of an import
1846 -- or export pragma. Scan the aspect list to gather the
1847 -- other components, if any. The name of the generated
1848 -- pragma is one of Convention/Import/Export.
1850 declare
1851 Args : constant List_Id := New_List (
1852 Make_Pragma_Argument_Association (Sloc (Expr),
1853 Expression => Relocate_Node (Expr)),
1854 Make_Pragma_Argument_Association (Sloc (Ent),
1855 Expression => Ent));
1857 Imp_Exp_Seen : Boolean := False;
1858 -- Flag set when aspect Import or Export has been seen
1860 Imp_Seen : Boolean := False;
1861 -- Flag set when aspect Import has been seen
1863 Asp : Node_Id;
1864 Asp_Nam : Name_Id;
1865 Extern_Arg : Node_Id;
1866 Link_Arg : Node_Id;
1867 Prag_Nam : Name_Id;
1869 begin
1870 Extern_Arg := Empty;
1871 Link_Arg := Empty;
1872 Prag_Nam := Chars (Id);
1874 Asp := First (L);
1875 while Present (Asp) loop
1876 Asp_Nam := Chars (Identifier (Asp));
1878 -- Aspects Import and Export take precedence over
1879 -- aspect Convention. As a result the generated pragma
1880 -- must carry the proper interfacing aspect's name.
1882 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1883 if Imp_Exp_Seen then
1884 Error_Msg_N ("conflicting", Asp);
1885 else
1886 Imp_Exp_Seen := True;
1888 if Asp_Nam = Name_Import then
1889 Imp_Seen := True;
1890 end if;
1891 end if;
1893 Prag_Nam := Asp_Nam;
1895 -- Aspect External_Name adds an extra argument to the
1896 -- generated pragma.
1898 elsif Asp_Nam = Name_External_Name then
1899 Extern_Arg :=
1900 Make_Pragma_Argument_Association (Loc,
1901 Chars => Asp_Nam,
1902 Expression => Relocate_Node (Expression (Asp)));
1904 -- Aspect Link_Name adds an extra argument to the
1905 -- generated pragma.
1907 elsif Asp_Nam = Name_Link_Name then
1908 Link_Arg :=
1909 Make_Pragma_Argument_Association (Loc,
1910 Chars => Asp_Nam,
1911 Expression => Relocate_Node (Expression (Asp)));
1912 end if;
1914 Next (Asp);
1915 end loop;
1917 -- Assemble the full argument list
1919 if Present (Extern_Arg) then
1920 Append_To (Args, Extern_Arg);
1921 end if;
1923 if Present (Link_Arg) then
1924 Append_To (Args, Link_Arg);
1925 end if;
1927 Make_Aitem_Pragma
1928 (Pragma_Argument_Associations => Args,
1929 Pragma_Name => Prag_Nam);
1931 -- Store the generated pragma Import in the related
1932 -- subprogram.
1934 if Imp_Seen and then Is_Subprogram (E) then
1935 Set_Import_Pragma (E, Aitem);
1936 end if;
1937 end;
1939 -- CPU, Interrupt_Priority, Priority
1941 -- These three aspects can be specified for a subprogram spec
1942 -- or body, in which case we analyze the expression and export
1943 -- the value of the aspect.
1945 -- Previously, we generated an equivalent pragma for bodies
1946 -- (note that the specs cannot contain these pragmas). The
1947 -- pragma was inserted ahead of local declarations, rather than
1948 -- after the body. This leads to a certain duplication between
1949 -- the processing performed for the aspect and the pragma, but
1950 -- given the straightforward handling required it is simpler
1951 -- to duplicate than to translate the aspect in the spec into
1952 -- a pragma in the declarative part of the body.
1954 when Aspect_CPU |
1955 Aspect_Interrupt_Priority |
1956 Aspect_Priority =>
1958 if Nkind_In (N, N_Subprogram_Body,
1959 N_Subprogram_Declaration)
1960 then
1961 -- Analyze the aspect expression
1963 Analyze_And_Resolve (Expr, Standard_Integer);
1965 -- Interrupt_Priority aspect not allowed for main
1966 -- subprograms. ARM D.1 does not forbid this explicitly,
1967 -- but ARM J.15.11 (6/3) does not permit pragma
1968 -- Interrupt_Priority for subprograms.
1970 if A_Id = Aspect_Interrupt_Priority then
1971 Error_Msg_N
1972 ("Interrupt_Priority aspect cannot apply to "
1973 & "subprogram", Expr);
1975 -- The expression must be static
1977 elsif not Is_OK_Static_Expression (Expr) then
1978 Flag_Non_Static_Expr
1979 ("aspect requires static expression!", Expr);
1981 -- Check whether this is the main subprogram. Issue a
1982 -- warning only if it is obviously not a main program
1983 -- (when it has parameters or when the subprogram is
1984 -- within a package).
1986 elsif Present (Parameter_Specifications
1987 (Specification (N)))
1988 or else not Is_Compilation_Unit (Defining_Entity (N))
1989 then
1990 -- See ARM D.1 (14/3) and D.16 (12/3)
1992 Error_Msg_N
1993 ("aspect applied to subprogram other than the "
1994 & "main subprogram has no effect??", Expr);
1996 -- Otherwise check in range and export the value
1998 -- For the CPU aspect
2000 elsif A_Id = Aspect_CPU then
2001 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2003 -- Value is correct so we export the value to make
2004 -- it available at execution time.
2006 Set_Main_CPU
2007 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2009 else
2010 Error_Msg_N
2011 ("main subprogram CPU is out of range", Expr);
2012 end if;
2014 -- For the Priority aspect
2016 elsif A_Id = Aspect_Priority then
2017 if Is_In_Range (Expr, RTE (RE_Priority)) then
2019 -- Value is correct so we export the value to make
2020 -- it available at execution time.
2022 Set_Main_Priority
2023 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2025 -- Ignore pragma if Relaxed_RM_Semantics to support
2026 -- other targets/non GNAT compilers.
2028 elsif not Relaxed_RM_Semantics then
2029 Error_Msg_N
2030 ("main subprogram priority is out of range",
2031 Expr);
2032 end if;
2033 end if;
2035 -- Load an arbitrary entity from System.Tasking.Stages
2036 -- or System.Tasking.Restricted.Stages (depending on
2037 -- the supported profile) to make sure that one of these
2038 -- packages is implicitly with'ed, since we need to have
2039 -- the tasking run time active for the pragma Priority to
2040 -- have any effect. Previously we with'ed the package
2041 -- System.Tasking, but this package does not trigger the
2042 -- required initialization of the run-time library.
2044 declare
2045 Discard : Entity_Id;
2046 begin
2047 if Restricted_Profile then
2048 Discard := RTE (RE_Activate_Restricted_Tasks);
2049 else
2050 Discard := RTE (RE_Activate_Tasks);
2051 end if;
2052 end;
2054 -- Handling for these Aspects in subprograms is complete
2056 goto Continue;
2058 -- For tasks
2060 else
2061 -- Pass the aspect as an attribute
2063 Aitem :=
2064 Make_Attribute_Definition_Clause (Loc,
2065 Name => Ent,
2066 Chars => Chars (Id),
2067 Expression => Relocate_Node (Expr));
2068 end if;
2070 -- Warnings
2072 when Aspect_Warnings =>
2073 Make_Aitem_Pragma
2074 (Pragma_Argument_Associations => New_List (
2075 Make_Pragma_Argument_Association (Sloc (Expr),
2076 Expression => Relocate_Node (Expr)),
2077 Make_Pragma_Argument_Association (Loc,
2078 Expression => New_Occurrence_Of (E, Loc))),
2079 Pragma_Name => Chars (Id));
2081 -- Case 2c: Aspects corresponding to pragmas with three
2082 -- arguments.
2084 -- Invariant aspects have a first argument that references the
2085 -- entity, a second argument that is the expression and a third
2086 -- argument that is an appropriate message.
2088 -- Invariant, Type_Invariant
2090 when Aspect_Invariant |
2091 Aspect_Type_Invariant =>
2093 -- Analysis of the pragma will verify placement legality:
2094 -- an invariant must apply to a private type, or appear in
2095 -- the private part of a spec and apply to a completion.
2097 Make_Aitem_Pragma
2098 (Pragma_Argument_Associations => New_List (
2099 Make_Pragma_Argument_Association (Sloc (Ent),
2100 Expression => Ent),
2101 Make_Pragma_Argument_Association (Sloc (Expr),
2102 Expression => Relocate_Node (Expr))),
2103 Pragma_Name => Name_Invariant);
2105 -- Add message unless exception messages are suppressed
2107 if not Opt.Exception_Locations_Suppressed then
2108 Append_To (Pragma_Argument_Associations (Aitem),
2109 Make_Pragma_Argument_Association (Eloc,
2110 Chars => Name_Message,
2111 Expression =>
2112 Make_String_Literal (Eloc,
2113 Strval => "failed invariant from "
2114 & Build_Location_String (Eloc))));
2115 end if;
2117 -- For Invariant case, insert immediately after the entity
2118 -- declaration. We do not have to worry about delay issues
2119 -- since the pragma processing takes care of this.
2121 Delay_Required := False;
2123 -- Case 2d : Aspects that correspond to a pragma with one
2124 -- argument.
2126 -- Abstract_State
2128 -- Aspect Abstract_State introduces implicit declarations for
2129 -- all state abstraction entities it defines. To emulate this
2130 -- behavior, insert the pragma at the beginning of the visible
2131 -- declarations of the related package so that it is analyzed
2132 -- immediately.
2134 when Aspect_Abstract_State => Abstract_State : declare
2135 Context : Node_Id := N;
2136 Decl : Node_Id;
2137 Decls : List_Id;
2139 begin
2140 -- When aspect Abstract_State appears on a generic package,
2141 -- it is propageted to the package instance. The context in
2142 -- this case is the instance spec.
2144 if Nkind (Context) = N_Package_Instantiation then
2145 Context := Instance_Spec (Context);
2146 end if;
2148 if Nkind_In (Context, N_Generic_Package_Declaration,
2149 N_Package_Declaration)
2150 then
2151 Make_Aitem_Pragma
2152 (Pragma_Argument_Associations => New_List (
2153 Make_Pragma_Argument_Association (Loc,
2154 Expression => Relocate_Node (Expr))),
2155 Pragma_Name => Name_Abstract_State);
2156 Decorate (Aspect, Aitem);
2158 Decls := Visible_Declarations (Specification (Context));
2160 -- In general pragma Abstract_State must be at the top
2161 -- of the existing visible declarations to emulate its
2162 -- source counterpart. The only exception to this is a
2163 -- generic instance in which case the pragma must be
2164 -- inserted after the association renamings.
2166 if Present (Decls) then
2167 Decl := First (Decls);
2169 -- The visible declarations of a generic instance have
2170 -- the following structure:
2172 -- <renamings of generic formals>
2173 -- <renamings of internally-generated spec and body>
2174 -- <first source declaration>
2176 -- The pragma must be inserted before the first source
2177 -- declaration, skip the instance "header".
2179 if Is_Generic_Instance (Defining_Entity (Context)) then
2180 while Present (Decl)
2181 and then not Comes_From_Source (Decl)
2182 loop
2183 Decl := Next (Decl);
2184 end loop;
2185 end if;
2187 -- Pragma Abstract_State must be inserted after pragma
2188 -- SPARK_Mode in the tree. This ensures that any error
2189 -- messages dependent on SPARK_Mode will be properly
2190 -- enabled/suppressed.
2192 Insert_After_SPARK_Mode
2193 (Prag => Aitem,
2194 Ins_Nod => Decl,
2195 Decls => Decls);
2197 -- Otherwise the pragma forms a new declarative list
2199 else
2200 Set_Visible_Declarations
2201 (Specification (Context), New_List (Aitem));
2202 end if;
2204 else
2205 Error_Msg_NE
2206 ("aspect & must apply to a package declaration",
2207 Aspect, Id);
2208 end if;
2210 goto Continue;
2211 end Abstract_State;
2213 -- Aspect Default_Internal_Condition is never delayed because
2214 -- it is equivalent to a source pragma which appears after the
2215 -- related private type. To deal with forward references, the
2216 -- generated pragma is stored in the rep chain of the related
2217 -- private type as types do not carry contracts. The pragma is
2218 -- wrapped inside of a procedure at the freeze point of the
2219 -- private type's full view.
2221 when Aspect_Default_Initial_Condition =>
2222 Make_Aitem_Pragma
2223 (Pragma_Argument_Associations => New_List (
2224 Make_Pragma_Argument_Association (Loc,
2225 Expression => Relocate_Node (Expr))),
2226 Pragma_Name =>
2227 Name_Default_Initial_Condition);
2229 Decorate (Aspect, Aitem);
2230 Insert_Pragma (Aitem);
2231 goto Continue;
2233 -- Depends
2235 -- Aspect Depends is never delayed because it is equivalent to
2236 -- a source pragma which appears after the related subprogram.
2237 -- To deal with forward references, the generated pragma is
2238 -- stored in the contract of the related subprogram and later
2239 -- analyzed at the end of the declarative region. See routine
2240 -- Analyze_Depends_In_Decl_Part for details.
2242 when Aspect_Depends =>
2243 Make_Aitem_Pragma
2244 (Pragma_Argument_Associations => New_List (
2245 Make_Pragma_Argument_Association (Loc,
2246 Expression => Relocate_Node (Expr))),
2247 Pragma_Name => Name_Depends);
2249 Decorate (Aspect, Aitem);
2250 Insert_Pragma (Aitem);
2251 goto Continue;
2253 -- Global
2255 -- Aspect Global is never delayed because it is equivalent to
2256 -- a source pragma which appears after the related subprogram.
2257 -- To deal with forward references, the generated pragma is
2258 -- stored in the contract of the related subprogram and later
2259 -- analyzed at the end of the declarative region. See routine
2260 -- Analyze_Global_In_Decl_Part for details.
2262 when Aspect_Global =>
2263 Make_Aitem_Pragma
2264 (Pragma_Argument_Associations => New_List (
2265 Make_Pragma_Argument_Association (Loc,
2266 Expression => Relocate_Node (Expr))),
2267 Pragma_Name => Name_Global);
2269 Decorate (Aspect, Aitem);
2270 Insert_Pragma (Aitem);
2271 goto Continue;
2273 -- Initial_Condition
2275 -- Aspect Initial_Condition is never delayed because it is
2276 -- equivalent to a source pragma which appears after the
2277 -- related package. To deal with forward references, the
2278 -- generated pragma is stored in the contract of the related
2279 -- package and later analyzed at the end of the declarative
2280 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2281 -- for details.
2283 when Aspect_Initial_Condition => Initial_Condition : declare
2284 Context : Node_Id := N;
2285 Decls : List_Id;
2287 begin
2288 -- When aspect Initial_Condition appears on a generic
2289 -- package, it is propageted to the package instance. The
2290 -- context in this case is the instance spec.
2292 if Nkind (Context) = N_Package_Instantiation then
2293 Context := Instance_Spec (Context);
2294 end if;
2296 if Nkind_In (Context, N_Generic_Package_Declaration,
2297 N_Package_Declaration)
2298 then
2299 Decls := Visible_Declarations (Specification (Context));
2301 Make_Aitem_Pragma
2302 (Pragma_Argument_Associations => New_List (
2303 Make_Pragma_Argument_Association (Loc,
2304 Expression => Relocate_Node (Expr))),
2305 Pragma_Name =>
2306 Name_Initial_Condition);
2307 Decorate (Aspect, Aitem);
2309 if No (Decls) then
2310 Decls := New_List;
2311 Set_Visible_Declarations (Context, Decls);
2312 end if;
2314 -- When aspects Abstract_State, Initial_Condition and
2315 -- Initializes are out of order, ensure that pragma
2316 -- SPARK_Mode is always at the top of the declarative
2317 -- list to properly enable/suppress errors.
2319 Insert_After_SPARK_Mode
2320 (Prag => Aitem,
2321 Ins_Nod => First (Decls),
2322 Decls => Decls);
2324 else
2325 Error_Msg_NE
2326 ("aspect & must apply to a package declaration",
2327 Aspect, Id);
2328 end if;
2330 goto Continue;
2331 end Initial_Condition;
2333 -- Initializes
2335 -- Aspect Initializes is never delayed because it is equivalent
2336 -- to a source pragma appearing after the related package. To
2337 -- deal with forward references, the generated pragma is stored
2338 -- in the contract of the related package and later analyzed at
2339 -- the end of the declarative region. For details, see routine
2340 -- Analyze_Initializes_In_Decl_Part.
2342 when Aspect_Initializes => Initializes : declare
2343 Context : Node_Id := N;
2344 Decls : List_Id;
2346 begin
2347 -- When aspect Initializes appears on a generic package,
2348 -- it is propageted to the package instance. The context
2349 -- in this case is the instance spec.
2351 if Nkind (Context) = N_Package_Instantiation then
2352 Context := Instance_Spec (Context);
2353 end if;
2355 if Nkind_In (Context, N_Generic_Package_Declaration,
2356 N_Package_Declaration)
2357 then
2358 Decls := Visible_Declarations (Specification (Context));
2360 Make_Aitem_Pragma
2361 (Pragma_Argument_Associations => New_List (
2362 Make_Pragma_Argument_Association (Loc,
2363 Expression => Relocate_Node (Expr))),
2364 Pragma_Name => Name_Initializes);
2365 Decorate (Aspect, Aitem);
2367 if No (Decls) then
2368 Decls := New_List;
2369 Set_Visible_Declarations (Context, Decls);
2370 end if;
2372 -- When aspects Abstract_State, Initial_Condition and
2373 -- Initializes are out of order, ensure that pragma
2374 -- SPARK_Mode is always at the top of the declarative
2375 -- list to properly enable/suppress errors.
2377 Insert_After_SPARK_Mode
2378 (Prag => Aitem,
2379 Ins_Nod => First (Decls),
2380 Decls => Decls);
2382 else
2383 Error_Msg_NE
2384 ("aspect & must apply to a package declaration",
2385 Aspect, Id);
2386 end if;
2388 goto Continue;
2389 end Initializes;
2391 -- Obsolescent
2393 when Aspect_Obsolescent => declare
2394 Args : List_Id;
2396 begin
2397 if No (Expr) then
2398 Args := No_List;
2399 else
2400 Args := New_List (
2401 Make_Pragma_Argument_Association (Sloc (Expr),
2402 Expression => Relocate_Node (Expr)));
2403 end if;
2405 Make_Aitem_Pragma
2406 (Pragma_Argument_Associations => Args,
2407 Pragma_Name => Chars (Id));
2408 end;
2410 -- Part_Of
2412 when Aspect_Part_Of =>
2413 if Nkind_In (N, N_Object_Declaration,
2414 N_Package_Instantiation)
2415 then
2416 Make_Aitem_Pragma
2417 (Pragma_Argument_Associations => New_List (
2418 Make_Pragma_Argument_Association (Loc,
2419 Expression => Relocate_Node (Expr))),
2420 Pragma_Name => Name_Part_Of);
2422 else
2423 Error_Msg_NE
2424 ("aspect & must apply to a variable or package "
2425 & "instantiation", Aspect, Id);
2426 end if;
2428 -- SPARK_Mode
2430 when Aspect_SPARK_Mode => SPARK_Mode : declare
2431 Decls : List_Id;
2433 begin
2434 Make_Aitem_Pragma
2435 (Pragma_Argument_Associations => New_List (
2436 Make_Pragma_Argument_Association (Loc,
2437 Expression => Relocate_Node (Expr))),
2438 Pragma_Name => Name_SPARK_Mode);
2440 -- When the aspect appears on a package or a subprogram
2441 -- body, insert the generated pragma at the top of the body
2442 -- declarations to emulate the behavior of a source pragma.
2444 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2445 Decorate (Aspect, Aitem);
2447 Decls := Declarations (N);
2449 if No (Decls) then
2450 Decls := New_List;
2451 Set_Declarations (N, Decls);
2452 end if;
2454 Prepend_To (Decls, Aitem);
2455 goto Continue;
2457 -- When the aspect is associated with a [generic] package
2458 -- declaration, insert the generated pragma at the top of
2459 -- the visible declarations to emulate the behavior of a
2460 -- source pragma.
2462 elsif Nkind_In (N, N_Generic_Package_Declaration,
2463 N_Package_Declaration)
2464 then
2465 Decorate (Aspect, Aitem);
2467 Decls := Visible_Declarations (Specification (N));
2469 if No (Decls) then
2470 Decls := New_List;
2471 Set_Visible_Declarations (Specification (N), Decls);
2472 end if;
2474 Prepend_To (Decls, Aitem);
2475 goto Continue;
2476 end if;
2477 end SPARK_Mode;
2479 -- Refined_Depends
2481 -- Aspect Refined_Depends is never delayed because it is
2482 -- equivalent to a source pragma which appears in the
2483 -- declarations of the related subprogram body. To deal with
2484 -- forward references, the generated pragma is stored in the
2485 -- contract of the related subprogram body and later analyzed
2486 -- at the end of the declarative region. For details, see
2487 -- routine Analyze_Refined_Depends_In_Decl_Part.
2489 when Aspect_Refined_Depends =>
2490 Make_Aitem_Pragma
2491 (Pragma_Argument_Associations => New_List (
2492 Make_Pragma_Argument_Association (Loc,
2493 Expression => Relocate_Node (Expr))),
2494 Pragma_Name => Name_Refined_Depends);
2496 Decorate (Aspect, Aitem);
2497 Insert_Pragma (Aitem);
2498 goto Continue;
2500 -- Refined_Global
2502 -- Aspect Refined_Global is never delayed because it is
2503 -- equivalent to a source pragma which appears in the
2504 -- declarations of the related subprogram body. To deal with
2505 -- forward references, the generated pragma is stored in the
2506 -- contract of the related subprogram body and later analyzed
2507 -- at the end of the declarative region. For details, see
2508 -- routine Analyze_Refined_Global_In_Decl_Part.
2510 when Aspect_Refined_Global =>
2511 Make_Aitem_Pragma
2512 (Pragma_Argument_Associations => New_List (
2513 Make_Pragma_Argument_Association (Loc,
2514 Expression => Relocate_Node (Expr))),
2515 Pragma_Name => Name_Refined_Global);
2517 Decorate (Aspect, Aitem);
2518 Insert_Pragma (Aitem);
2519 goto Continue;
2521 -- Refined_Post
2523 when Aspect_Refined_Post =>
2524 Make_Aitem_Pragma
2525 (Pragma_Argument_Associations => New_List (
2526 Make_Pragma_Argument_Association (Loc,
2527 Expression => Relocate_Node (Expr))),
2528 Pragma_Name => Name_Refined_Post);
2530 -- Refined_State
2532 when Aspect_Refined_State => Refined_State : declare
2533 Decls : List_Id;
2535 begin
2536 -- The corresponding pragma for Refined_State is inserted in
2537 -- the declarations of the related package body. This action
2538 -- synchronizes both the source and from-aspect versions of
2539 -- the pragma.
2541 if Nkind (N) = N_Package_Body then
2542 Decls := Declarations (N);
2544 Make_Aitem_Pragma
2545 (Pragma_Argument_Associations => New_List (
2546 Make_Pragma_Argument_Association (Loc,
2547 Expression => Relocate_Node (Expr))),
2548 Pragma_Name => Name_Refined_State);
2549 Decorate (Aspect, Aitem);
2551 if No (Decls) then
2552 Decls := New_List;
2553 Set_Declarations (N, Decls);
2554 end if;
2556 -- Pragma Refined_State must be inserted after pragma
2557 -- SPARK_Mode in the tree. This ensures that any error
2558 -- messages dependent on SPARK_Mode will be properly
2559 -- enabled/suppressed.
2561 Insert_After_SPARK_Mode
2562 (Prag => Aitem,
2563 Ins_Nod => First (Decls),
2564 Decls => Decls);
2566 else
2567 Error_Msg_NE
2568 ("aspect & must apply to a package body", Aspect, Id);
2569 end if;
2571 goto Continue;
2572 end Refined_State;
2574 -- Relative_Deadline
2576 when Aspect_Relative_Deadline =>
2577 Make_Aitem_Pragma
2578 (Pragma_Argument_Associations => New_List (
2579 Make_Pragma_Argument_Association (Loc,
2580 Expression => Relocate_Node (Expr))),
2581 Pragma_Name => Name_Relative_Deadline);
2583 -- If the aspect applies to a task, the corresponding pragma
2584 -- must appear within its declarations, not after.
2586 if Nkind (N) = N_Task_Type_Declaration then
2587 declare
2588 Def : Node_Id;
2589 V : List_Id;
2591 begin
2592 if No (Task_Definition (N)) then
2593 Set_Task_Definition (N,
2594 Make_Task_Definition (Loc,
2595 Visible_Declarations => New_List,
2596 End_Label => Empty));
2597 end if;
2599 Def := Task_Definition (N);
2600 V := Visible_Declarations (Def);
2601 if not Is_Empty_List (V) then
2602 Insert_Before (First (V), Aitem);
2604 else
2605 Set_Visible_Declarations (Def, New_List (Aitem));
2606 end if;
2608 goto Continue;
2609 end;
2610 end if;
2612 -- Case 2e: Annotate aspect
2614 when Aspect_Annotate =>
2615 declare
2616 Args : List_Id;
2617 Pargs : List_Id;
2618 Arg : Node_Id;
2620 begin
2621 -- The argument can be a single identifier
2623 if Nkind (Expr) = N_Identifier then
2625 -- One level of parens is allowed
2627 if Paren_Count (Expr) > 1 then
2628 Error_Msg_F ("extra parentheses ignored", Expr);
2629 end if;
2631 Set_Paren_Count (Expr, 0);
2633 -- Add the single item to the list
2635 Args := New_List (Expr);
2637 -- Otherwise we must have an aggregate
2639 elsif Nkind (Expr) = N_Aggregate then
2641 -- Must be positional
2643 if Present (Component_Associations (Expr)) then
2644 Error_Msg_F
2645 ("purely positional aggregate required", Expr);
2646 goto Continue;
2647 end if;
2649 -- Must not be parenthesized
2651 if Paren_Count (Expr) /= 0 then
2652 Error_Msg_F ("extra parentheses ignored", Expr);
2653 end if;
2655 -- List of arguments is list of aggregate expressions
2657 Args := Expressions (Expr);
2659 -- Anything else is illegal
2661 else
2662 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2663 goto Continue;
2664 end if;
2666 -- Prepare pragma arguments
2668 Pargs := New_List;
2669 Arg := First (Args);
2670 while Present (Arg) loop
2671 Append_To (Pargs,
2672 Make_Pragma_Argument_Association (Sloc (Arg),
2673 Expression => Relocate_Node (Arg)));
2674 Next (Arg);
2675 end loop;
2677 Append_To (Pargs,
2678 Make_Pragma_Argument_Association (Sloc (Ent),
2679 Chars => Name_Entity,
2680 Expression => Ent));
2682 Make_Aitem_Pragma
2683 (Pragma_Argument_Associations => Pargs,
2684 Pragma_Name => Name_Annotate);
2685 end;
2687 -- Case 3 : Aspects that don't correspond to pragma/attribute
2688 -- definition clause.
2690 -- Case 3a: The aspects listed below don't correspond to
2691 -- pragmas/attributes but do require delayed analysis.
2693 -- Default_Value can only apply to a scalar type
2695 when Aspect_Default_Value =>
2696 if not Is_Scalar_Type (E) then
2697 Error_Msg_N
2698 ("aspect Default_Value must apply to a scalar_Type", N);
2699 end if;
2701 Aitem := Empty;
2703 -- Default_Component_Value can only apply to an array type
2704 -- with scalar components.
2706 when Aspect_Default_Component_Value =>
2707 if not (Is_Array_Type (E)
2708 and then Is_Scalar_Type (Component_Type (E)))
2709 then
2710 Error_Msg_N ("aspect Default_Component_Value can only "
2711 & "apply to an array of scalar components", N);
2712 end if;
2714 Aitem := Empty;
2716 -- Case 3b: The aspects listed below don't correspond to
2717 -- pragmas/attributes and don't need delayed analysis.
2719 -- Implicit_Dereference
2721 -- For Implicit_Dereference, External_Name and Link_Name, only
2722 -- the legality checks are done during the analysis, thus no
2723 -- delay is required.
2725 when Aspect_Implicit_Dereference =>
2726 Analyze_Aspect_Implicit_Dereference;
2727 goto Continue;
2729 -- External_Name, Link_Name
2731 when Aspect_External_Name |
2732 Aspect_Link_Name =>
2733 Analyze_Aspect_External_Or_Link_Name;
2734 goto Continue;
2736 -- Dimension
2738 when Aspect_Dimension =>
2739 Analyze_Aspect_Dimension (N, Id, Expr);
2740 goto Continue;
2742 -- Dimension_System
2744 when Aspect_Dimension_System =>
2745 Analyze_Aspect_Dimension_System (N, Id, Expr);
2746 goto Continue;
2748 -- Case 4: Aspects requiring special handling
2750 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2751 -- pragmas take care of the delay.
2753 -- Pre/Post
2755 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2756 -- with a first argument that is the expression, and a second
2757 -- argument that is an informative message if the test fails.
2758 -- This is inserted right after the declaration, to get the
2759 -- required pragma placement. The processing for the pragmas
2760 -- takes care of the required delay.
2762 when Pre_Post_Aspects => Pre_Post : declare
2763 Pname : Name_Id;
2765 begin
2766 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2767 Pname := Name_Precondition;
2768 else
2769 Pname := Name_Postcondition;
2770 end if;
2772 -- If the expressions is of the form A and then B, then
2773 -- we generate separate Pre/Post aspects for the separate
2774 -- clauses. Since we allow multiple pragmas, there is no
2775 -- problem in allowing multiple Pre/Post aspects internally.
2776 -- These should be treated in reverse order (B first and
2777 -- A second) since they are later inserted just after N in
2778 -- the order they are treated. This way, the pragma for A
2779 -- ends up preceding the pragma for B, which may have an
2780 -- importance for the error raised (either constraint error
2781 -- or precondition error).
2783 -- We do not do this for Pre'Class, since we have to put
2784 -- these conditions together in a complex OR expression.
2786 -- We do not do this in ASIS mode, as ASIS relies on the
2787 -- original node representing the complete expression, when
2788 -- retrieving it through the source aspect table.
2790 if not ASIS_Mode
2791 and then (Pname = Name_Postcondition
2792 or else not Class_Present (Aspect))
2793 then
2794 while Nkind (Expr) = N_And_Then loop
2795 Insert_After (Aspect,
2796 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2797 Identifier => Identifier (Aspect),
2798 Expression => Relocate_Node (Left_Opnd (Expr)),
2799 Class_Present => Class_Present (Aspect),
2800 Split_PPC => True));
2801 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2802 Eloc := Sloc (Expr);
2803 end loop;
2804 end if;
2806 -- Build the precondition/postcondition pragma
2808 -- Add note about why we do NOT need Copy_Tree here???
2810 Make_Aitem_Pragma
2811 (Pragma_Argument_Associations => New_List (
2812 Make_Pragma_Argument_Association (Eloc,
2813 Chars => Name_Check,
2814 Expression => Relocate_Node (Expr))),
2815 Pragma_Name => Pname);
2817 -- Add message unless exception messages are suppressed
2819 if not Opt.Exception_Locations_Suppressed then
2820 Append_To (Pragma_Argument_Associations (Aitem),
2821 Make_Pragma_Argument_Association (Eloc,
2822 Chars => Name_Message,
2823 Expression =>
2824 Make_String_Literal (Eloc,
2825 Strval => "failed "
2826 & Get_Name_String (Pname)
2827 & " from "
2828 & Build_Location_String (Eloc))));
2829 end if;
2831 Set_Is_Delayed_Aspect (Aspect);
2833 -- For Pre/Post cases, insert immediately after the entity
2834 -- declaration, since that is the required pragma placement.
2835 -- Note that for these aspects, we do not have to worry
2836 -- about delay issues, since the pragmas themselves deal
2837 -- with delay of visibility for the expression analysis.
2839 Insert_Pragma (Aitem);
2840 goto Continue;
2841 end Pre_Post;
2843 -- Test_Case
2845 when Aspect_Test_Case => Test_Case : declare
2846 Args : List_Id;
2847 Comp_Expr : Node_Id;
2848 Comp_Assn : Node_Id;
2849 New_Expr : Node_Id;
2851 begin
2852 Args := New_List;
2854 if Nkind (Parent (N)) = N_Compilation_Unit then
2855 Error_Msg_Name_1 := Nam;
2856 Error_Msg_N ("incorrect placement of aspect `%`", E);
2857 goto Continue;
2858 end if;
2860 if Nkind (Expr) /= N_Aggregate then
2861 Error_Msg_Name_1 := Nam;
2862 Error_Msg_NE
2863 ("wrong syntax for aspect `%` for &", Id, E);
2864 goto Continue;
2865 end if;
2867 -- Make pragma expressions refer to the original aspect
2868 -- expressions through the Original_Node link. This is used
2869 -- in semantic analysis for ASIS mode, so that the original
2870 -- expression also gets analyzed.
2872 Comp_Expr := First (Expressions (Expr));
2873 while Present (Comp_Expr) loop
2874 New_Expr := Relocate_Node (Comp_Expr);
2875 Set_Original_Node (New_Expr, Comp_Expr);
2876 Append_To (Args,
2877 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2878 Expression => New_Expr));
2879 Next (Comp_Expr);
2880 end loop;
2882 Comp_Assn := First (Component_Associations (Expr));
2883 while Present (Comp_Assn) loop
2884 if List_Length (Choices (Comp_Assn)) /= 1
2885 or else
2886 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2887 then
2888 Error_Msg_Name_1 := Nam;
2889 Error_Msg_NE
2890 ("wrong syntax for aspect `%` for &", Id, E);
2891 goto Continue;
2892 end if;
2894 New_Expr := Relocate_Node (Expression (Comp_Assn));
2895 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2896 Append_To (Args,
2897 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2898 Chars => Chars (First (Choices (Comp_Assn))),
2899 Expression => New_Expr));
2900 Next (Comp_Assn);
2901 end loop;
2903 -- Build the test-case pragma
2905 Make_Aitem_Pragma
2906 (Pragma_Argument_Associations => Args,
2907 Pragma_Name => Nam);
2908 end Test_Case;
2910 -- Contract_Cases
2912 when Aspect_Contract_Cases =>
2913 Make_Aitem_Pragma
2914 (Pragma_Argument_Associations => New_List (
2915 Make_Pragma_Argument_Association (Loc,
2916 Expression => Relocate_Node (Expr))),
2917 Pragma_Name => Nam);
2919 Decorate (Aspect, Aitem);
2920 Insert_Pragma (Aitem);
2921 goto Continue;
2923 -- Case 5: Special handling for aspects with an optional
2924 -- boolean argument.
2926 -- In the general case, the corresponding pragma cannot be
2927 -- generated yet because the evaluation of the boolean needs
2928 -- to be delayed till the freeze point.
2930 when Boolean_Aspects |
2931 Library_Unit_Aspects =>
2933 Set_Is_Boolean_Aspect (Aspect);
2935 -- Lock_Free aspect only apply to protected objects
2937 if A_Id = Aspect_Lock_Free then
2938 if Ekind (E) /= E_Protected_Type then
2939 Error_Msg_Name_1 := Nam;
2940 Error_Msg_N
2941 ("aspect % only applies to a protected object",
2942 Aspect);
2944 else
2945 -- Set the Uses_Lock_Free flag to True if there is no
2946 -- expression or if the expression is True. The
2947 -- evaluation of this aspect should be delayed to the
2948 -- freeze point (why???)
2950 if No (Expr) or else Is_True (Static_Boolean (Expr))
2951 then
2952 Set_Uses_Lock_Free (E);
2953 end if;
2955 Record_Rep_Item (E, Aspect);
2956 end if;
2958 goto Continue;
2960 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
2962 -- For the case of aspects Import and Export, we don't
2963 -- consider that we know the entity is never set in the
2964 -- source, since it is is likely modified outside the
2965 -- program.
2967 -- Note: one might think that the analysis of the
2968 -- resulting pragma would take care of that, but
2969 -- that's not the case since it won't be from source.
2971 if Ekind (E) = E_Variable then
2972 Set_Never_Set_In_Source (E, False);
2973 end if;
2975 -- In older versions of Ada the corresponding pragmas
2976 -- specified a Convention. In Ada 2012 the convention is
2977 -- specified as a separate aspect, and it is optional,
2978 -- given that it defaults to Convention_Ada. The code
2979 -- that verifed that there was a matching convention
2980 -- is now obsolete.
2982 -- Resolve the expression of an Import or Export here,
2983 -- and require it to be of type Boolean and static. This
2984 -- is not quite right, because in general this should be
2985 -- delayed, but that seems tricky for these, because
2986 -- normally Boolean aspects are replaced with pragmas at
2987 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
2988 -- but in the case of these aspects we can't generate
2989 -- a simple pragma with just the entity name. ???
2991 if not Present (Expr)
2992 or else Is_True (Static_Boolean (Expr))
2993 then
2994 if A_Id = Aspect_Import then
2995 Set_Is_Imported (E);
2997 -- An imported entity cannot have an explicit
2998 -- initialization.
3000 if Nkind (N) = N_Object_Declaration
3001 and then Present (Expression (N))
3002 then
3003 Error_Msg_N
3004 ("imported entities cannot be initialized "
3005 & "(RM B.1(24))", Expression (N));
3006 end if;
3008 elsif A_Id = Aspect_Export then
3009 Set_Is_Exported (E);
3010 end if;
3011 end if;
3013 goto Continue;
3014 end if;
3016 -- Library unit aspects require special handling in the case
3017 -- of a package declaration, the pragma needs to be inserted
3018 -- in the list of declarations for the associated package.
3019 -- There is no issue of visibility delay for these aspects.
3021 if A_Id in Library_Unit_Aspects
3022 and then
3023 Nkind_In (N, N_Package_Declaration,
3024 N_Generic_Package_Declaration)
3025 and then Nkind (Parent (N)) /= N_Compilation_Unit
3027 -- Aspect is legal on a local instantiation of a library-
3028 -- level generic unit.
3030 and then not Is_Generic_Instance (Defining_Entity (N))
3031 then
3032 Error_Msg_N
3033 ("incorrect context for library unit aspect&", Id);
3034 goto Continue;
3035 end if;
3037 -- External property aspects are Boolean by nature, but
3038 -- their pragmas must contain two arguments, the second
3039 -- being the optional Boolean expression.
3041 if A_Id = Aspect_Async_Readers or else
3042 A_Id = Aspect_Async_Writers or else
3043 A_Id = Aspect_Effective_Reads or else
3044 A_Id = Aspect_Effective_Writes
3045 then
3046 declare
3047 Args : List_Id;
3049 begin
3050 -- The first argument of the external property pragma
3051 -- is the related object.
3053 Args :=
3054 New_List (
3055 Make_Pragma_Argument_Association (Sloc (Ent),
3056 Expression => Ent));
3058 -- The second argument is the optional Boolean
3059 -- expression which must be propagated even if it
3060 -- evaluates to False as this has special semantic
3061 -- meaning.
3063 if Present (Expr) then
3064 Append_To (Args,
3065 Make_Pragma_Argument_Association (Loc,
3066 Expression => Relocate_Node (Expr)));
3067 end if;
3069 Make_Aitem_Pragma
3070 (Pragma_Argument_Associations => Args,
3071 Pragma_Name => Nam);
3072 end;
3074 -- Cases where we do not delay, includes all cases where the
3075 -- expression is missing other than the above cases.
3077 elsif not Delay_Required or else No (Expr) then
3078 Make_Aitem_Pragma
3079 (Pragma_Argument_Associations => New_List (
3080 Make_Pragma_Argument_Association (Sloc (Ent),
3081 Expression => Ent)),
3082 Pragma_Name => Chars (Id));
3083 Delay_Required := False;
3085 -- In general cases, the corresponding pragma/attribute
3086 -- definition clause will be inserted later at the freezing
3087 -- point, and we do not need to build it now.
3089 else
3090 Aitem := Empty;
3091 end if;
3093 -- Storage_Size
3095 -- This is special because for access types we need to generate
3096 -- an attribute definition clause. This also works for single
3097 -- task declarations, but it does not work for task type
3098 -- declarations, because we have the case where the expression
3099 -- references a discriminant of the task type. That can't use
3100 -- an attribute definition clause because we would not have
3101 -- visibility on the discriminant. For that case we must
3102 -- generate a pragma in the task definition.
3104 when Aspect_Storage_Size =>
3106 -- Task type case
3108 if Ekind (E) = E_Task_Type then
3109 declare
3110 Decl : constant Node_Id := Declaration_Node (E);
3112 begin
3113 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3115 -- If no task definition, create one
3117 if No (Task_Definition (Decl)) then
3118 Set_Task_Definition (Decl,
3119 Make_Task_Definition (Loc,
3120 Visible_Declarations => Empty_List,
3121 End_Label => Empty));
3122 end if;
3124 -- Create a pragma and put it at the start of the task
3125 -- definition for the task type declaration.
3127 Make_Aitem_Pragma
3128 (Pragma_Argument_Associations => New_List (
3129 Make_Pragma_Argument_Association (Loc,
3130 Expression => Relocate_Node (Expr))),
3131 Pragma_Name => Name_Storage_Size);
3133 Prepend
3134 (Aitem,
3135 Visible_Declarations (Task_Definition (Decl)));
3136 goto Continue;
3137 end;
3139 -- All other cases, generate attribute definition
3141 else
3142 Aitem :=
3143 Make_Attribute_Definition_Clause (Loc,
3144 Name => Ent,
3145 Chars => Chars (Id),
3146 Expression => Relocate_Node (Expr));
3147 end if;
3148 end case;
3150 -- Attach the corresponding pragma/attribute definition clause to
3151 -- the aspect specification node.
3153 if Present (Aitem) then
3154 Set_From_Aspect_Specification (Aitem);
3155 end if;
3157 -- In the context of a compilation unit, we directly put the
3158 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3159 -- node (no delay is required here) except for aspects on a
3160 -- subprogram body (see below) and a generic package, for which we
3161 -- need to introduce the pragma before building the generic copy
3162 -- (see sem_ch12), and for package instantiations, where the
3163 -- library unit pragmas are better handled early.
3165 if Nkind (Parent (N)) = N_Compilation_Unit
3166 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3167 then
3168 declare
3169 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3171 begin
3172 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3174 -- For a Boolean aspect, create the corresponding pragma if
3175 -- no expression or if the value is True.
3177 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3178 if Is_True (Static_Boolean (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));
3185 Set_From_Aspect_Specification (Aitem, True);
3186 Set_Corresponding_Aspect (Aitem, Aspect);
3188 else
3189 goto Continue;
3190 end if;
3191 end if;
3193 -- If the aspect is on a subprogram body (relevant aspect
3194 -- is Inline), add the pragma in front of the declarations.
3196 if Nkind (N) = N_Subprogram_Body then
3197 if No (Declarations (N)) then
3198 Set_Declarations (N, New_List);
3199 end if;
3201 Prepend (Aitem, Declarations (N));
3203 elsif Nkind (N) = N_Generic_Package_Declaration then
3204 if No (Visible_Declarations (Specification (N))) then
3205 Set_Visible_Declarations (Specification (N), New_List);
3206 end if;
3208 Prepend (Aitem,
3209 Visible_Declarations (Specification (N)));
3211 elsif Nkind (N) = N_Package_Instantiation then
3212 declare
3213 Spec : constant Node_Id :=
3214 Specification (Instance_Spec (N));
3215 begin
3216 if No (Visible_Declarations (Spec)) then
3217 Set_Visible_Declarations (Spec, New_List);
3218 end if;
3220 Prepend (Aitem, Visible_Declarations (Spec));
3221 end;
3223 else
3224 if No (Pragmas_After (Aux)) then
3225 Set_Pragmas_After (Aux, New_List);
3226 end if;
3228 Append (Aitem, Pragmas_After (Aux));
3229 end if;
3231 goto Continue;
3232 end;
3233 end if;
3235 -- The evaluation of the aspect is delayed to the freezing point.
3236 -- The pragma or attribute clause if there is one is then attached
3237 -- to the aspect specification which is put in the rep item list.
3239 if Delay_Required then
3240 if Present (Aitem) then
3241 Set_Is_Delayed_Aspect (Aitem);
3242 Set_Aspect_Rep_Item (Aspect, Aitem);
3243 Set_Parent (Aitem, Aspect);
3244 end if;
3246 Set_Is_Delayed_Aspect (Aspect);
3248 -- In the case of Default_Value, link the aspect to base type
3249 -- as well, even though it appears on a first subtype. This is
3250 -- mandated by the semantics of the aspect. Do not establish
3251 -- the link when processing the base type itself as this leads
3252 -- to a rep item circularity. Verify that we are dealing with
3253 -- a scalar type to prevent cascaded errors.
3255 if A_Id = Aspect_Default_Value
3256 and then Is_Scalar_Type (E)
3257 and then Base_Type (E) /= E
3258 then
3259 Set_Has_Delayed_Aspects (Base_Type (E));
3260 Record_Rep_Item (Base_Type (E), Aspect);
3261 end if;
3263 Set_Has_Delayed_Aspects (E);
3264 Record_Rep_Item (E, Aspect);
3266 -- When delay is not required and the context is a package or a
3267 -- subprogram body, insert the pragma in the body declarations.
3269 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3270 if No (Declarations (N)) then
3271 Set_Declarations (N, New_List);
3272 end if;
3274 -- The pragma is added before source declarations
3276 Prepend_To (Declarations (N), Aitem);
3278 -- When delay is not required and the context is not a compilation
3279 -- unit, we simply insert the pragma/attribute definition clause
3280 -- in sequence.
3282 else
3283 Insert_After (Ins_Node, Aitem);
3284 Ins_Node := Aitem;
3285 end if;
3286 end Analyze_One_Aspect;
3288 <<Continue>>
3289 Next (Aspect);
3290 end loop Aspect_Loop;
3292 if Has_Delayed_Aspects (E) then
3293 Ensure_Freeze_Node (E);
3294 end if;
3295 end Analyze_Aspect_Specifications;
3297 -----------------------
3298 -- Analyze_At_Clause --
3299 -----------------------
3301 -- An at clause is replaced by the corresponding Address attribute
3302 -- definition clause that is the preferred approach in Ada 95.
3304 procedure Analyze_At_Clause (N : Node_Id) is
3305 CS : constant Boolean := Comes_From_Source (N);
3307 begin
3308 -- This is an obsolescent feature
3310 Check_Restriction (No_Obsolescent_Features, N);
3312 if Warn_On_Obsolescent_Feature then
3313 Error_Msg_N
3314 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3315 Error_Msg_N
3316 ("\?j?use address attribute definition clause instead", N);
3317 end if;
3319 -- Rewrite as address clause
3321 Rewrite (N,
3322 Make_Attribute_Definition_Clause (Sloc (N),
3323 Name => Identifier (N),
3324 Chars => Name_Address,
3325 Expression => Expression (N)));
3327 -- We preserve Comes_From_Source, since logically the clause still comes
3328 -- from the source program even though it is changed in form.
3330 Set_Comes_From_Source (N, CS);
3332 -- Analyze rewritten clause
3334 Analyze_Attribute_Definition_Clause (N);
3335 end Analyze_At_Clause;
3337 -----------------------------------------
3338 -- Analyze_Attribute_Definition_Clause --
3339 -----------------------------------------
3341 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3342 Loc : constant Source_Ptr := Sloc (N);
3343 Nam : constant Node_Id := Name (N);
3344 Attr : constant Name_Id := Chars (N);
3345 Expr : constant Node_Id := Expression (N);
3346 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3348 Ent : Entity_Id;
3349 -- The entity of Nam after it is analyzed. In the case of an incomplete
3350 -- type, this is the underlying type.
3352 U_Ent : Entity_Id;
3353 -- The underlying entity to which the attribute applies. Generally this
3354 -- is the Underlying_Type of Ent, except in the case where the clause
3355 -- applies to full view of incomplete type or private type in which case
3356 -- U_Ent is just a copy of Ent.
3358 FOnly : Boolean := False;
3359 -- Reset to True for subtype specific attribute (Alignment, Size)
3360 -- and for stream attributes, i.e. those cases where in the call to
3361 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3362 -- are checked. Note that the case of stream attributes is not clear
3363 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3364 -- Storage_Size for derived task types, but that is also clearly
3365 -- unintentional.
3367 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3368 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3369 -- definition clauses.
3371 function Duplicate_Clause return Boolean;
3372 -- This routine checks if the aspect for U_Ent being given by attribute
3373 -- definition clause N is for an aspect that has already been specified,
3374 -- and if so gives an error message. If there is a duplicate, True is
3375 -- returned, otherwise if there is no error, False is returned.
3377 procedure Check_Indexing_Functions;
3378 -- Check that the function in Constant_Indexing or Variable_Indexing
3379 -- attribute has the proper type structure. If the name is overloaded,
3380 -- check that some interpretation is legal.
3382 procedure Check_Iterator_Functions;
3383 -- Check that there is a single function in Default_Iterator attribute
3384 -- has the proper type structure.
3386 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3387 -- Common legality check for the previous two
3389 -----------------------------------
3390 -- Analyze_Stream_TSS_Definition --
3391 -----------------------------------
3393 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3394 Subp : Entity_Id := Empty;
3395 I : Interp_Index;
3396 It : Interp;
3397 Pnam : Entity_Id;
3399 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3400 -- True for Read attribute, false for other attributes
3402 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3403 -- Return true if the entity is a subprogram with an appropriate
3404 -- profile for the attribute being defined.
3406 ----------------------
3407 -- Has_Good_Profile --
3408 ----------------------
3410 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3411 F : Entity_Id;
3412 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3413 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3414 (False => E_Procedure, True => E_Function);
3415 Typ : Entity_Id;
3417 begin
3418 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3419 return False;
3420 end if;
3422 F := First_Formal (Subp);
3424 if No (F)
3425 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3426 or else Designated_Type (Etype (F)) /=
3427 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3428 then
3429 return False;
3430 end if;
3432 if not Is_Function then
3433 Next_Formal (F);
3435 declare
3436 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3437 (False => E_In_Parameter,
3438 True => E_Out_Parameter);
3439 begin
3440 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3441 return False;
3442 end if;
3443 end;
3445 Typ := Etype (F);
3447 -- If the attribute specification comes from an aspect
3448 -- specification for a class-wide stream, the parameter must be
3449 -- a class-wide type of the entity to which the aspect applies.
3451 if From_Aspect_Specification (N)
3452 and then Class_Present (Parent (N))
3453 and then Is_Class_Wide_Type (Typ)
3454 then
3455 Typ := Etype (Typ);
3456 end if;
3458 else
3459 Typ := Etype (Subp);
3460 end if;
3462 -- Verify that the prefix of the attribute and the local name for
3463 -- the type of the formal match.
3465 if Base_Type (Typ) /= Base_Type (Ent)
3466 or else Present ((Next_Formal (F)))
3467 then
3468 return False;
3470 elsif not Is_Scalar_Type (Typ)
3471 and then not Is_First_Subtype (Typ)
3472 and then not Is_Class_Wide_Type (Typ)
3473 then
3474 return False;
3476 else
3477 return True;
3478 end if;
3479 end Has_Good_Profile;
3481 -- Start of processing for Analyze_Stream_TSS_Definition
3483 begin
3484 FOnly := True;
3486 if not Is_Type (U_Ent) then
3487 Error_Msg_N ("local name must be a subtype", Nam);
3488 return;
3490 elsif not Is_First_Subtype (U_Ent) then
3491 Error_Msg_N ("local name must be a first subtype", Nam);
3492 return;
3493 end if;
3495 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3497 -- If Pnam is present, it can be either inherited from an ancestor
3498 -- type (in which case it is legal to redefine it for this type), or
3499 -- be a previous definition of the attribute for the same type (in
3500 -- which case it is illegal).
3502 -- In the first case, it will have been analyzed already, and we
3503 -- can check that its profile does not match the expected profile
3504 -- for a stream attribute of U_Ent. In the second case, either Pnam
3505 -- has been analyzed (and has the expected profile), or it has not
3506 -- been analyzed yet (case of a type that has not been frozen yet
3507 -- and for which the stream attribute has been set using Set_TSS).
3509 if Present (Pnam)
3510 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3511 then
3512 Error_Msg_Sloc := Sloc (Pnam);
3513 Error_Msg_Name_1 := Attr;
3514 Error_Msg_N ("% attribute already defined #", Nam);
3515 return;
3516 end if;
3518 Analyze (Expr);
3520 if Is_Entity_Name (Expr) then
3521 if not Is_Overloaded (Expr) then
3522 if Has_Good_Profile (Entity (Expr)) then
3523 Subp := Entity (Expr);
3524 end if;
3526 else
3527 Get_First_Interp (Expr, I, It);
3528 while Present (It.Nam) loop
3529 if Has_Good_Profile (It.Nam) then
3530 Subp := It.Nam;
3531 exit;
3532 end if;
3534 Get_Next_Interp (I, It);
3535 end loop;
3536 end if;
3537 end if;
3539 if Present (Subp) then
3540 if Is_Abstract_Subprogram (Subp) then
3541 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3542 return;
3544 -- Test for stream subprogram for interface type being non-null
3546 elsif Is_Interface (U_Ent)
3547 and then not Inside_A_Generic
3548 and then Ekind (Subp) = E_Procedure
3549 and then
3550 not Null_Present
3551 (Specification
3552 (Unit_Declaration_Node (Ultimate_Alias (Subp))))
3553 then
3554 Error_Msg_N
3555 ("stream subprogram for interface type "
3556 & "must be null procedure", Expr);
3557 end if;
3559 Set_Entity (Expr, Subp);
3560 Set_Etype (Expr, Etype (Subp));
3562 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3564 else
3565 Error_Msg_Name_1 := Attr;
3566 Error_Msg_N ("incorrect expression for% attribute", Expr);
3567 end if;
3568 end Analyze_Stream_TSS_Definition;
3570 ------------------------------
3571 -- Check_Indexing_Functions --
3572 ------------------------------
3574 procedure Check_Indexing_Functions is
3575 Indexing_Found : Boolean := False;
3577 procedure Check_One_Function (Subp : Entity_Id);
3578 -- Check one possible interpretation. Sets Indexing_Found True if a
3579 -- legal indexing function is found.
3581 procedure Illegal_Indexing (Msg : String);
3582 -- Diagnose illegal indexing function if not overloaded. In the
3583 -- overloaded case indicate that no legal interpretation exists.
3585 ------------------------
3586 -- Check_One_Function --
3587 ------------------------
3589 procedure Check_One_Function (Subp : Entity_Id) is
3590 Default_Element : Node_Id;
3591 Ret_Type : constant Entity_Id := Etype (Subp);
3593 begin
3594 if not Is_Overloadable (Subp) then
3595 Illegal_Indexing ("illegal indexing function for type&");
3596 return;
3598 elsif Scope (Subp) /= Scope (Ent) then
3599 if Nkind (Expr) = N_Expanded_Name then
3601 -- Indexing function can't be declared elsewhere
3603 Illegal_Indexing
3604 ("indexing function must be declared in scope of type&");
3605 end if;
3607 return;
3609 elsif No (First_Formal (Subp)) then
3610 Illegal_Indexing
3611 ("Indexing requires a function that applies to type&");
3612 return;
3614 elsif No (Next_Formal (First_Formal (Subp))) then
3615 Illegal_Indexing
3616 ("indexing function must have at least two parameters");
3617 return;
3619 elsif Is_Derived_Type (Ent) then
3620 if (Attr = Name_Constant_Indexing
3621 and then Present
3622 (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing)))
3623 or else
3624 (Attr = Name_Variable_Indexing
3625 and then Present
3626 (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing)))
3627 then
3628 if Debug_Flag_Dot_XX then
3629 null;
3631 else
3632 Illegal_Indexing
3633 ("indexing function already inherited "
3634 & "from parent type");
3635 return;
3636 end if;
3637 end if;
3638 end if;
3640 if not Check_Primitive_Function (Subp)
3641 then
3642 Illegal_Indexing
3643 ("Indexing aspect requires a function that applies to type&");
3644 return;
3645 end if;
3647 -- If partial declaration exists, verify that it is not tagged.
3649 if Ekind (Current_Scope) = E_Package
3650 and then Has_Private_Declaration (Ent)
3651 and then From_Aspect_Specification (N)
3652 and then
3653 List_Containing (Parent (Ent)) =
3654 Private_Declarations
3655 (Specification (Unit_Declaration_Node (Current_Scope)))
3656 and then Nkind (N) = N_Attribute_Definition_Clause
3657 then
3658 declare
3659 Decl : Node_Id;
3661 begin
3662 Decl :=
3663 First (Visible_Declarations
3664 (Specification
3665 (Unit_Declaration_Node (Current_Scope))));
3667 while Present (Decl) loop
3668 if Nkind (Decl) = N_Private_Type_Declaration
3669 and then Ent = Full_View (Defining_Identifier (Decl))
3670 and then Tagged_Present (Decl)
3671 and then No (Aspect_Specifications (Decl))
3672 then
3673 Illegal_Indexing
3674 ("Indexing aspect cannot be specified on full view "
3675 & "if partial view is tagged");
3676 return;
3677 end if;
3679 Next (Decl);
3680 end loop;
3681 end;
3682 end if;
3684 -- An indexing function must return either the default element of
3685 -- the container, or a reference type. For variable indexing it
3686 -- must be the latter.
3688 Default_Element :=
3689 Find_Value_Of_Aspect
3690 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3692 if Present (Default_Element) then
3693 Analyze (Default_Element);
3695 if Is_Entity_Name (Default_Element)
3696 and then not Covers (Entity (Default_Element), Ret_Type)
3697 and then False
3698 then
3699 Illegal_Indexing
3700 ("wrong return type for indexing function");
3701 return;
3702 end if;
3703 end if;
3705 -- For variable_indexing the return type must be a reference type
3707 if Attr = Name_Variable_Indexing then
3708 if not Has_Implicit_Dereference (Ret_Type) then
3709 Illegal_Indexing
3710 ("variable indexing must return a reference type");
3711 return;
3713 elsif Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3714 then
3715 Illegal_Indexing
3716 ("variable indexing must return an access to variable");
3717 return;
3718 end if;
3720 else
3721 if Has_Implicit_Dereference (Ret_Type)
3722 and then not
3723 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3724 then
3725 Illegal_Indexing
3726 ("constant indexing must return an access to constant");
3727 return;
3729 elsif Is_Access_Type (Etype (First_Formal (Subp)))
3730 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
3731 then
3732 Illegal_Indexing
3733 ("constant indexing must apply to an access to constant");
3734 return;
3735 end if;
3736 end if;
3738 -- All checks succeeded.
3740 Indexing_Found := True;
3741 end Check_One_Function;
3743 -----------------------
3744 -- Illegal_Indexing --
3745 -----------------------
3747 procedure Illegal_Indexing (Msg : String) is
3748 begin
3749 Error_Msg_NE (Msg, N, Ent);
3750 end Illegal_Indexing;
3752 -- Start of processing for Check_Indexing_Functions
3754 begin
3755 if In_Instance then
3756 return;
3757 end if;
3759 Analyze (Expr);
3761 if not Is_Overloaded (Expr) then
3762 Check_One_Function (Entity (Expr));
3764 else
3765 declare
3766 I : Interp_Index;
3767 It : Interp;
3769 begin
3770 Indexing_Found := False;
3771 Get_First_Interp (Expr, I, It);
3772 while Present (It.Nam) loop
3774 -- Note that analysis will have added the interpretation
3775 -- that corresponds to the dereference. We only check the
3776 -- subprogram itself.
3778 if Is_Overloadable (It.Nam) then
3779 Check_One_Function (It.Nam);
3780 end if;
3782 Get_Next_Interp (I, It);
3783 end loop;
3784 end;
3785 end if;
3787 if not Indexing_Found and then not Error_Posted (N) then
3788 Error_Msg_NE
3789 ("aspect Indexing requires a local function that "
3790 & "applies to type&", Expr, Ent);
3791 end if;
3792 end Check_Indexing_Functions;
3794 ------------------------------
3795 -- Check_Iterator_Functions --
3796 ------------------------------
3798 procedure Check_Iterator_Functions is
3799 Default : Entity_Id;
3801 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3802 -- Check one possible interpretation for validity
3804 ----------------------------
3805 -- Valid_Default_Iterator --
3806 ----------------------------
3808 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3809 Formal : Entity_Id;
3811 begin
3812 if not Check_Primitive_Function (Subp) then
3813 return False;
3814 else
3815 Formal := First_Formal (Subp);
3816 end if;
3818 -- False if any subsequent formal has no default expression
3820 Formal := Next_Formal (Formal);
3821 while Present (Formal) loop
3822 if No (Expression (Parent (Formal))) then
3823 return False;
3824 end if;
3826 Next_Formal (Formal);
3827 end loop;
3829 -- True if all subsequent formals have default expressions
3831 return True;
3832 end Valid_Default_Iterator;
3834 -- Start of processing for Check_Iterator_Functions
3836 begin
3837 Analyze (Expr);
3839 if not Is_Entity_Name (Expr) then
3840 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3841 end if;
3843 if not Is_Overloaded (Expr) then
3844 if not Check_Primitive_Function (Entity (Expr)) then
3845 Error_Msg_NE
3846 ("aspect Indexing requires a function that applies to type&",
3847 Entity (Expr), Ent);
3848 end if;
3850 if not Valid_Default_Iterator (Entity (Expr)) then
3851 Error_Msg_N ("improper function for default iterator", Expr);
3852 end if;
3854 else
3855 Default := Empty;
3856 declare
3857 I : Interp_Index;
3858 It : Interp;
3860 begin
3861 Get_First_Interp (Expr, I, It);
3862 while Present (It.Nam) loop
3863 if not Check_Primitive_Function (It.Nam)
3864 or else not Valid_Default_Iterator (It.Nam)
3865 then
3866 Remove_Interp (I);
3868 elsif Present (Default) then
3869 Error_Msg_N ("default iterator must be unique", Expr);
3871 else
3872 Default := It.Nam;
3873 end if;
3875 Get_Next_Interp (I, It);
3876 end loop;
3877 end;
3879 if Present (Default) then
3880 Set_Entity (Expr, Default);
3881 Set_Is_Overloaded (Expr, False);
3882 end if;
3883 end if;
3884 end Check_Iterator_Functions;
3886 -------------------------------
3887 -- Check_Primitive_Function --
3888 -------------------------------
3890 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3891 Ctrl : Entity_Id;
3893 begin
3894 if Ekind (Subp) /= E_Function then
3895 return False;
3896 end if;
3898 if No (First_Formal (Subp)) then
3899 return False;
3900 else
3901 Ctrl := Etype (First_Formal (Subp));
3902 end if;
3904 if Ctrl = Ent
3905 or else Ctrl = Class_Wide_Type (Ent)
3906 or else
3907 (Ekind (Ctrl) = E_Anonymous_Access_Type
3908 and then (Designated_Type (Ctrl) = Ent
3909 or else
3910 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
3911 then
3912 null;
3913 else
3914 return False;
3915 end if;
3917 return True;
3918 end Check_Primitive_Function;
3920 ----------------------
3921 -- Duplicate_Clause --
3922 ----------------------
3924 function Duplicate_Clause return Boolean is
3925 A : Node_Id;
3927 begin
3928 -- Nothing to do if this attribute definition clause comes from
3929 -- an aspect specification, since we could not be duplicating an
3930 -- explicit clause, and we dealt with the case of duplicated aspects
3931 -- in Analyze_Aspect_Specifications.
3933 if From_Aspect_Specification (N) then
3934 return False;
3935 end if;
3937 -- Otherwise current clause may duplicate previous clause, or a
3938 -- previously given pragma or aspect specification for the same
3939 -- aspect.
3941 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
3943 if Present (A) then
3944 Error_Msg_Name_1 := Chars (N);
3945 Error_Msg_Sloc := Sloc (A);
3947 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
3948 return True;
3949 end if;
3951 return False;
3952 end Duplicate_Clause;
3954 -- Start of processing for Analyze_Attribute_Definition_Clause
3956 begin
3957 -- The following code is a defense against recursion. Not clear that
3958 -- this can happen legitimately, but perhaps some error situations can
3959 -- cause it, and we did see this recursion during testing.
3961 if Analyzed (N) then
3962 return;
3963 else
3964 Set_Analyzed (N, True);
3965 end if;
3967 -- Ignore some selected attributes in CodePeer mode since they are not
3968 -- relevant in this context.
3970 if CodePeer_Mode then
3971 case Id is
3973 -- Ignore Component_Size in CodePeer mode, to avoid changing the
3974 -- internal representation of types by implicitly packing them.
3976 when Attribute_Component_Size =>
3977 Rewrite (N, Make_Null_Statement (Sloc (N)));
3978 return;
3980 when others =>
3981 null;
3982 end case;
3983 end if;
3985 -- Process Ignore_Rep_Clauses option
3987 if Ignore_Rep_Clauses then
3988 case Id is
3990 -- The following should be ignored. They do not affect legality
3991 -- and may be target dependent. The basic idea of -gnatI is to
3992 -- ignore any rep clauses that may be target dependent but do not
3993 -- affect legality (except possibly to be rejected because they
3994 -- are incompatible with the compilation target).
3996 when Attribute_Alignment |
3997 Attribute_Bit_Order |
3998 Attribute_Component_Size |
3999 Attribute_Machine_Radix |
4000 Attribute_Object_Size |
4001 Attribute_Size |
4002 Attribute_Small |
4003 Attribute_Stream_Size |
4004 Attribute_Value_Size =>
4005 Kill_Rep_Clause (N);
4006 return;
4008 -- The following should not be ignored, because in the first place
4009 -- they are reasonably portable, and should not cause problems
4010 -- in compiling code from another target, and also they do affect
4011 -- legality, e.g. failing to provide a stream attribute for a type
4012 -- may make a program illegal.
4014 when Attribute_External_Tag |
4015 Attribute_Input |
4016 Attribute_Output |
4017 Attribute_Read |
4018 Attribute_Simple_Storage_Pool |
4019 Attribute_Storage_Pool |
4020 Attribute_Storage_Size |
4021 Attribute_Write =>
4022 null;
4024 -- We do not do anything here with address clauses, they will be
4025 -- removed by Freeze later on, but for now, it works better to
4026 -- keep then in the tree.
4028 when Attribute_Address =>
4029 null;
4031 -- Other cases are errors ("attribute& cannot be set with
4032 -- definition clause"), which will be caught below.
4034 when others =>
4035 null;
4036 end case;
4037 end if;
4039 Analyze (Nam);
4040 Ent := Entity (Nam);
4042 if Rep_Item_Too_Early (Ent, N) then
4043 return;
4044 end if;
4046 -- Rep clause applies to full view of incomplete type or private type if
4047 -- we have one (if not, this is a premature use of the type). However,
4048 -- certain semantic checks need to be done on the specified entity (i.e.
4049 -- the private view), so we save it in Ent.
4051 if Is_Private_Type (Ent)
4052 and then Is_Derived_Type (Ent)
4053 and then not Is_Tagged_Type (Ent)
4054 and then No (Full_View (Ent))
4055 then
4056 -- If this is a private type whose completion is a derivation from
4057 -- another private type, there is no full view, and the attribute
4058 -- belongs to the type itself, not its underlying parent.
4060 U_Ent := Ent;
4062 elsif Ekind (Ent) = E_Incomplete_Type then
4064 -- The attribute applies to the full view, set the entity of the
4065 -- attribute definition accordingly.
4067 Ent := Underlying_Type (Ent);
4068 U_Ent := Ent;
4069 Set_Entity (Nam, Ent);
4071 else
4072 U_Ent := Underlying_Type (Ent);
4073 end if;
4075 -- Avoid cascaded error
4077 if Etype (Nam) = Any_Type then
4078 return;
4080 -- Must be declared in current scope or in case of an aspect
4081 -- specification, must be visible in current scope.
4083 elsif Scope (Ent) /= Current_Scope
4084 and then
4085 not (From_Aspect_Specification (N)
4086 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4087 then
4088 Error_Msg_N ("entity must be declared in this scope", Nam);
4089 return;
4091 -- Must not be a source renaming (we do have some cases where the
4092 -- expander generates a renaming, and those cases are OK, in such
4093 -- cases any attribute applies to the renamed object as well).
4095 elsif Is_Object (Ent)
4096 and then Present (Renamed_Object (Ent))
4097 then
4098 -- Case of renamed object from source, this is an error
4100 if Comes_From_Source (Renamed_Object (Ent)) then
4101 Get_Name_String (Chars (N));
4102 Error_Msg_Strlen := Name_Len;
4103 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4104 Error_Msg_N
4105 ("~ clause not allowed for a renaming declaration "
4106 & "(RM 13.1(6))", Nam);
4107 return;
4109 -- For the case of a compiler generated renaming, the attribute
4110 -- definition clause applies to the renamed object created by the
4111 -- expander. The easiest general way to handle this is to create a
4112 -- copy of the attribute definition clause for this object.
4114 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4115 Insert_Action (N,
4116 Make_Attribute_Definition_Clause (Loc,
4117 Name =>
4118 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4119 Chars => Chars (N),
4120 Expression => Duplicate_Subexpr (Expression (N))));
4122 -- If the renamed object is not an entity, it must be a dereference
4123 -- of an unconstrained function call, and we must introduce a new
4124 -- declaration to capture the expression. This is needed in the case
4125 -- of 'Alignment, where the original declaration must be rewritten.
4127 else
4128 pragma Assert
4129 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4130 null;
4131 end if;
4133 -- If no underlying entity, use entity itself, applies to some
4134 -- previously detected error cases ???
4136 elsif No (U_Ent) then
4137 U_Ent := Ent;
4139 -- Cannot specify for a subtype (exception Object/Value_Size)
4141 elsif Is_Type (U_Ent)
4142 and then not Is_First_Subtype (U_Ent)
4143 and then Id /= Attribute_Object_Size
4144 and then Id /= Attribute_Value_Size
4145 and then not From_At_Mod (N)
4146 then
4147 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4148 return;
4149 end if;
4151 Set_Entity (N, U_Ent);
4152 Check_Restriction_No_Use_Of_Attribute (N);
4154 -- Switch on particular attribute
4156 case Id is
4158 -------------
4159 -- Address --
4160 -------------
4162 -- Address attribute definition clause
4164 when Attribute_Address => Address : begin
4166 -- A little error check, catch for X'Address use X'Address;
4168 if Nkind (Nam) = N_Identifier
4169 and then Nkind (Expr) = N_Attribute_Reference
4170 and then Attribute_Name (Expr) = Name_Address
4171 and then Nkind (Prefix (Expr)) = N_Identifier
4172 and then Chars (Nam) = Chars (Prefix (Expr))
4173 then
4174 Error_Msg_NE
4175 ("address for & is self-referencing", Prefix (Expr), Ent);
4176 return;
4177 end if;
4179 -- Not that special case, carry on with analysis of expression
4181 Analyze_And_Resolve (Expr, RTE (RE_Address));
4183 -- Even when ignoring rep clauses we need to indicate that the
4184 -- entity has an address clause and thus it is legal to declare
4185 -- it imported. Freeze will get rid of the address clause later.
4187 if Ignore_Rep_Clauses then
4188 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4189 Record_Rep_Item (U_Ent, N);
4190 end if;
4192 return;
4193 end if;
4195 if Duplicate_Clause then
4196 null;
4198 -- Case of address clause for subprogram
4200 elsif Is_Subprogram (U_Ent) then
4201 if Has_Homonym (U_Ent) then
4202 Error_Msg_N
4203 ("address clause cannot be given " &
4204 "for overloaded subprogram",
4205 Nam);
4206 return;
4207 end if;
4209 -- For subprograms, all address clauses are permitted, and we
4210 -- mark the subprogram as having a deferred freeze so that Gigi
4211 -- will not elaborate it too soon.
4213 -- Above needs more comments, what is too soon about???
4215 Set_Has_Delayed_Freeze (U_Ent);
4217 -- Case of address clause for entry
4219 elsif Ekind (U_Ent) = E_Entry then
4220 if Nkind (Parent (N)) = N_Task_Body then
4221 Error_Msg_N
4222 ("entry address must be specified in task spec", Nam);
4223 return;
4224 end if;
4226 -- For entries, we require a constant address
4228 Check_Constant_Address_Clause (Expr, U_Ent);
4230 -- Special checks for task types
4232 if Is_Task_Type (Scope (U_Ent))
4233 and then Comes_From_Source (Scope (U_Ent))
4234 then
4235 Error_Msg_N
4236 ("??entry address declared for entry in task type", N);
4237 Error_Msg_N
4238 ("\??only one task can be declared of this type", N);
4239 end if;
4241 -- Entry address clauses are obsolescent
4243 Check_Restriction (No_Obsolescent_Features, N);
4245 if Warn_On_Obsolescent_Feature then
4246 Error_Msg_N
4247 ("?j?attaching interrupt to task entry is an " &
4248 "obsolescent feature (RM J.7.1)", N);
4249 Error_Msg_N
4250 ("\?j?use interrupt procedure instead", N);
4251 end if;
4253 -- Case of an address clause for a controlled object which we
4254 -- consider to be erroneous.
4256 elsif Is_Controlled (Etype (U_Ent))
4257 or else Has_Controlled_Component (Etype (U_Ent))
4258 then
4259 Error_Msg_NE
4260 ("??controlled object& must not be overlaid", Nam, U_Ent);
4261 Error_Msg_N
4262 ("\??Program_Error will be raised at run time", Nam);
4263 Insert_Action (Declaration_Node (U_Ent),
4264 Make_Raise_Program_Error (Loc,
4265 Reason => PE_Overlaid_Controlled_Object));
4266 return;
4268 -- Case of address clause for a (non-controlled) object
4270 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4271 declare
4272 Expr : constant Node_Id := Expression (N);
4273 O_Ent : Entity_Id;
4274 Off : Boolean;
4276 begin
4277 -- Exported variables cannot have an address clause, because
4278 -- this cancels the effect of the pragma Export.
4280 if Is_Exported (U_Ent) then
4281 Error_Msg_N
4282 ("cannot export object with address clause", Nam);
4283 return;
4284 end if;
4286 Find_Overlaid_Entity (N, O_Ent, Off);
4288 -- Overlaying controlled objects is erroneous
4290 if Present (O_Ent)
4291 and then (Has_Controlled_Component (Etype (O_Ent))
4292 or else Is_Controlled (Etype (O_Ent)))
4293 then
4294 Error_Msg_N
4295 ("??cannot overlay with controlled object", Expr);
4296 Error_Msg_N
4297 ("\??Program_Error will be raised at run time", Expr);
4298 Insert_Action (Declaration_Node (U_Ent),
4299 Make_Raise_Program_Error (Loc,
4300 Reason => PE_Overlaid_Controlled_Object));
4301 return;
4303 elsif Present (O_Ent)
4304 and then Ekind (U_Ent) = E_Constant
4305 and then not Is_Constant_Object (O_Ent)
4306 then
4307 Error_Msg_N ("??constant overlays a variable", Expr);
4309 -- Imported variables can have an address clause, but then
4310 -- the import is pretty meaningless except to suppress
4311 -- initializations, so we do not need such variables to
4312 -- be statically allocated (and in fact it causes trouble
4313 -- if the address clause is a local value).
4315 elsif Is_Imported (U_Ent) then
4316 Set_Is_Statically_Allocated (U_Ent, False);
4317 end if;
4319 -- We mark a possible modification of a variable with an
4320 -- address clause, since it is likely aliasing is occurring.
4322 Note_Possible_Modification (Nam, Sure => False);
4324 -- Here we are checking for explicit overlap of one variable
4325 -- by another, and if we find this then mark the overlapped
4326 -- variable as also being volatile to prevent unwanted
4327 -- optimizations. This is a significant pessimization so
4328 -- avoid it when there is an offset, i.e. when the object
4329 -- is composite; they cannot be optimized easily anyway.
4331 if Present (O_Ent)
4332 and then Is_Object (O_Ent)
4333 and then not Off
4335 -- The following test is an expedient solution to what
4336 -- is really a problem in CodePeer. Suppressing the
4337 -- Set_Treat_As_Volatile call here prevents later
4338 -- generation (in some cases) of trees that CodePeer
4339 -- should, but currently does not, handle correctly.
4340 -- This test should probably be removed when CodePeer
4341 -- is improved, just because we want the tree CodePeer
4342 -- analyzes to match the tree for which we generate code
4343 -- as closely as is practical. ???
4345 and then not CodePeer_Mode
4346 then
4347 -- ??? O_Ent might not be in current unit
4349 Set_Treat_As_Volatile (O_Ent);
4350 end if;
4352 -- Legality checks on the address clause for initialized
4353 -- objects is deferred until the freeze point, because
4354 -- a subsequent pragma might indicate that the object
4355 -- is imported and thus not initialized. Also, the address
4356 -- clause might involve entities that have yet to be
4357 -- elaborated.
4359 Set_Has_Delayed_Freeze (U_Ent);
4361 -- If an initialization call has been generated for this
4362 -- object, it needs to be deferred to after the freeze node
4363 -- we have just now added, otherwise GIGI will see a
4364 -- reference to the variable (as actual to the IP call)
4365 -- before its definition.
4367 declare
4368 Init_Call : constant Node_Id :=
4369 Remove_Init_Call (U_Ent, N);
4371 begin
4372 if Present (Init_Call) then
4373 Append_Freeze_Action (U_Ent, Init_Call);
4375 -- Reset Initialization_Statements pointer so that
4376 -- if there is a pragma Import further down, it can
4377 -- clear any default initialization.
4379 Set_Initialization_Statements (U_Ent, Init_Call);
4380 end if;
4381 end;
4383 if Is_Exported (U_Ent) then
4384 Error_Msg_N
4385 ("& cannot be exported if an address clause is given",
4386 Nam);
4387 Error_Msg_N
4388 ("\define and export a variable "
4389 & "that holds its address instead", Nam);
4390 end if;
4392 -- Entity has delayed freeze, so we will generate an
4393 -- alignment check at the freeze point unless suppressed.
4395 if not Range_Checks_Suppressed (U_Ent)
4396 and then not Alignment_Checks_Suppressed (U_Ent)
4397 then
4398 Set_Check_Address_Alignment (N);
4399 end if;
4401 -- Kill the size check code, since we are not allocating
4402 -- the variable, it is somewhere else.
4404 Kill_Size_Check_Code (U_Ent);
4406 -- If the address clause is of the form:
4408 -- for Y'Address use X'Address
4410 -- or
4412 -- Const : constant Address := X'Address;
4413 -- ...
4414 -- for Y'Address use Const;
4416 -- then we make an entry in the table for checking the size
4417 -- and alignment of the overlaying variable. We defer this
4418 -- check till after code generation to take full advantage
4419 -- of the annotation done by the back end.
4421 -- If the entity has a generic type, the check will be
4422 -- performed in the instance if the actual type justifies
4423 -- it, and we do not insert the clause in the table to
4424 -- prevent spurious warnings.
4426 -- Note: we used to test Comes_From_Source and only give
4427 -- this warning for source entities, but we have removed
4428 -- this test. It really seems bogus to generate overlays
4429 -- that would trigger this warning in generated code.
4430 -- Furthermore, by removing the test, we handle the
4431 -- aspect case properly.
4433 if Address_Clause_Overlay_Warnings
4434 and then Present (O_Ent)
4435 and then Is_Object (O_Ent)
4436 then
4437 if not Is_Generic_Type (Etype (U_Ent)) then
4438 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4439 end if;
4441 -- If variable overlays a constant view, and we are
4442 -- warning on overlays, then mark the variable as
4443 -- overlaying a constant (we will give warnings later
4444 -- if this variable is assigned).
4446 if Is_Constant_Object (O_Ent)
4447 and then Ekind (U_Ent) = E_Variable
4448 then
4449 Set_Overlays_Constant (U_Ent);
4450 end if;
4451 end if;
4452 end;
4454 -- Not a valid entity for an address clause
4456 else
4457 Error_Msg_N ("address cannot be given for &", Nam);
4458 end if;
4459 end Address;
4461 ---------------
4462 -- Alignment --
4463 ---------------
4465 -- Alignment attribute definition clause
4467 when Attribute_Alignment => Alignment : declare
4468 Align : constant Uint := Get_Alignment_Value (Expr);
4469 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4471 begin
4472 FOnly := True;
4474 if not Is_Type (U_Ent)
4475 and then Ekind (U_Ent) /= E_Variable
4476 and then Ekind (U_Ent) /= E_Constant
4477 then
4478 Error_Msg_N ("alignment cannot be given for &", Nam);
4480 elsif Duplicate_Clause then
4481 null;
4483 elsif Align /= No_Uint then
4484 Set_Has_Alignment_Clause (U_Ent);
4486 -- Tagged type case, check for attempt to set alignment to a
4487 -- value greater than Max_Align, and reset if so.
4489 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4490 Error_Msg_N
4491 ("alignment for & set to Maximum_Aligment??", Nam);
4492 Set_Alignment (U_Ent, Max_Align);
4494 -- All other cases
4496 else
4497 Set_Alignment (U_Ent, Align);
4498 end if;
4500 -- For an array type, U_Ent is the first subtype. In that case,
4501 -- also set the alignment of the anonymous base type so that
4502 -- other subtypes (such as the itypes for aggregates of the
4503 -- type) also receive the expected alignment.
4505 if Is_Array_Type (U_Ent) then
4506 Set_Alignment (Base_Type (U_Ent), Align);
4507 end if;
4508 end if;
4509 end Alignment;
4511 ---------------
4512 -- Bit_Order --
4513 ---------------
4515 -- Bit_Order attribute definition clause
4517 when Attribute_Bit_Order => Bit_Order : declare
4518 begin
4519 if not Is_Record_Type (U_Ent) then
4520 Error_Msg_N
4521 ("Bit_Order can only be defined for record type", Nam);
4523 elsif Duplicate_Clause then
4524 null;
4526 else
4527 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4529 if Etype (Expr) = Any_Type then
4530 return;
4532 elsif not Is_OK_Static_Expression (Expr) then
4533 Flag_Non_Static_Expr
4534 ("Bit_Order requires static expression!", Expr);
4536 else
4537 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4538 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4539 end if;
4540 end if;
4541 end if;
4542 end Bit_Order;
4544 --------------------
4545 -- Component_Size --
4546 --------------------
4548 -- Component_Size attribute definition clause
4550 when Attribute_Component_Size => Component_Size_Case : declare
4551 Csize : constant Uint := Static_Integer (Expr);
4552 Ctyp : Entity_Id;
4553 Btype : Entity_Id;
4554 Biased : Boolean;
4555 New_Ctyp : Entity_Id;
4556 Decl : Node_Id;
4558 begin
4559 if not Is_Array_Type (U_Ent) then
4560 Error_Msg_N ("component size requires array type", Nam);
4561 return;
4562 end if;
4564 Btype := Base_Type (U_Ent);
4565 Ctyp := Component_Type (Btype);
4567 if Duplicate_Clause then
4568 null;
4570 elsif Rep_Item_Too_Early (Btype, N) then
4571 null;
4573 elsif Csize /= No_Uint then
4574 Check_Size (Expr, Ctyp, Csize, Biased);
4576 -- For the biased case, build a declaration for a subtype that
4577 -- will be used to represent the biased subtype that reflects
4578 -- the biased representation of components. We need the subtype
4579 -- to get proper conversions on referencing elements of the
4580 -- array. Note: component size clauses are ignored in VM mode.
4582 if VM_Target = No_VM then
4583 if Biased then
4584 New_Ctyp :=
4585 Make_Defining_Identifier (Loc,
4586 Chars =>
4587 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4589 Decl :=
4590 Make_Subtype_Declaration (Loc,
4591 Defining_Identifier => New_Ctyp,
4592 Subtype_Indication =>
4593 New_Occurrence_Of (Component_Type (Btype), Loc));
4595 Set_Parent (Decl, N);
4596 Analyze (Decl, Suppress => All_Checks);
4598 Set_Has_Delayed_Freeze (New_Ctyp, False);
4599 Set_Esize (New_Ctyp, Csize);
4600 Set_RM_Size (New_Ctyp, Csize);
4601 Init_Alignment (New_Ctyp);
4602 Set_Is_Itype (New_Ctyp, True);
4603 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4605 Set_Component_Type (Btype, New_Ctyp);
4606 Set_Biased (New_Ctyp, N, "component size clause");
4607 end if;
4609 Set_Component_Size (Btype, Csize);
4611 -- For VM case, we ignore component size clauses
4613 else
4614 -- Give a warning unless we are in GNAT mode, in which case
4615 -- the warning is suppressed since it is not useful.
4617 if not GNAT_Mode then
4618 Error_Msg_N
4619 ("component size ignored in this configuration??", N);
4620 end if;
4621 end if;
4623 -- Deal with warning on overridden size
4625 if Warn_On_Overridden_Size
4626 and then Has_Size_Clause (Ctyp)
4627 and then RM_Size (Ctyp) /= Csize
4628 then
4629 Error_Msg_NE
4630 ("component size overrides size clause for&?S?", N, Ctyp);
4631 end if;
4633 Set_Has_Component_Size_Clause (Btype, True);
4634 Set_Has_Non_Standard_Rep (Btype, True);
4635 end if;
4636 end Component_Size_Case;
4638 -----------------------
4639 -- Constant_Indexing --
4640 -----------------------
4642 when Attribute_Constant_Indexing =>
4643 Check_Indexing_Functions;
4645 ---------
4646 -- CPU --
4647 ---------
4649 when Attribute_CPU => CPU :
4650 begin
4651 -- CPU attribute definition clause not allowed except from aspect
4652 -- specification.
4654 if From_Aspect_Specification (N) then
4655 if not Is_Task_Type (U_Ent) then
4656 Error_Msg_N ("CPU can only be defined for task", Nam);
4658 elsif Duplicate_Clause then
4659 null;
4661 else
4662 -- The expression must be analyzed in the special manner
4663 -- described in "Handling of Default and Per-Object
4664 -- Expressions" in sem.ads.
4666 -- The visibility to the discriminants must be restored
4668 Push_Scope_And_Install_Discriminants (U_Ent);
4669 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4670 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4672 if not Is_OK_Static_Expression (Expr) then
4673 Check_Restriction (Static_Priorities, Expr);
4674 end if;
4675 end if;
4677 else
4678 Error_Msg_N
4679 ("attribute& cannot be set with definition clause", N);
4680 end if;
4681 end CPU;
4683 ----------------------
4684 -- Default_Iterator --
4685 ----------------------
4687 when Attribute_Default_Iterator => Default_Iterator : declare
4688 Func : Entity_Id;
4690 begin
4691 if not Is_Tagged_Type (U_Ent) then
4692 Error_Msg_N
4693 ("aspect Default_Iterator applies to tagged type", Nam);
4694 end if;
4696 Check_Iterator_Functions;
4698 Analyze (Expr);
4700 if not Is_Entity_Name (Expr)
4701 or else Ekind (Entity (Expr)) /= E_Function
4702 then
4703 Error_Msg_N ("aspect Iterator must be a function", Expr);
4704 else
4705 Func := Entity (Expr);
4706 end if;
4708 if No (First_Formal (Func))
4709 or else Etype (First_Formal (Func)) /= U_Ent
4710 then
4711 Error_Msg_NE
4712 ("Default Iterator must be a primitive of&", Func, U_Ent);
4713 end if;
4714 end Default_Iterator;
4716 ------------------------
4717 -- Dispatching_Domain --
4718 ------------------------
4720 when Attribute_Dispatching_Domain => Dispatching_Domain :
4721 begin
4722 -- Dispatching_Domain attribute definition clause not allowed
4723 -- except from aspect specification.
4725 if From_Aspect_Specification (N) then
4726 if not Is_Task_Type (U_Ent) then
4727 Error_Msg_N ("Dispatching_Domain can only be defined" &
4728 "for task",
4729 Nam);
4731 elsif Duplicate_Clause then
4732 null;
4734 else
4735 -- The expression must be analyzed in the special manner
4736 -- described in "Handling of Default and Per-Object
4737 -- Expressions" in sem.ads.
4739 -- The visibility to the discriminants must be restored
4741 Push_Scope_And_Install_Discriminants (U_Ent);
4743 Preanalyze_Spec_Expression
4744 (Expr, RTE (RE_Dispatching_Domain));
4746 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4747 end if;
4749 else
4750 Error_Msg_N
4751 ("attribute& cannot be set with definition clause", N);
4752 end if;
4753 end Dispatching_Domain;
4755 ------------------
4756 -- External_Tag --
4757 ------------------
4759 when Attribute_External_Tag => External_Tag :
4760 begin
4761 if not Is_Tagged_Type (U_Ent) then
4762 Error_Msg_N ("should be a tagged type", Nam);
4763 end if;
4765 if Duplicate_Clause then
4766 null;
4768 else
4769 Analyze_And_Resolve (Expr, Standard_String);
4771 if not Is_OK_Static_Expression (Expr) then
4772 Flag_Non_Static_Expr
4773 ("static string required for tag name!", Nam);
4774 end if;
4776 if VM_Target /= No_VM then
4777 Error_Msg_Name_1 := Attr;
4778 Error_Msg_N
4779 ("% attribute unsupported in this configuration", Nam);
4780 end if;
4782 if not Is_Library_Level_Entity (U_Ent) then
4783 Error_Msg_NE
4784 ("??non-unique external tag supplied for &", N, U_Ent);
4785 Error_Msg_N
4786 ("\??same external tag applies to all "
4787 & "subprogram calls", N);
4788 Error_Msg_N
4789 ("\??corresponding internal tag cannot be obtained", N);
4790 end if;
4791 end if;
4792 end External_Tag;
4794 --------------------------
4795 -- Implicit_Dereference --
4796 --------------------------
4798 when Attribute_Implicit_Dereference =>
4800 -- Legality checks already performed at the point of the type
4801 -- declaration, aspect is not delayed.
4803 null;
4805 -----------
4806 -- Input --
4807 -----------
4809 when Attribute_Input =>
4810 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4811 Set_Has_Specified_Stream_Input (Ent);
4813 ------------------------
4814 -- Interrupt_Priority --
4815 ------------------------
4817 when Attribute_Interrupt_Priority => Interrupt_Priority :
4818 begin
4819 -- Interrupt_Priority attribute definition clause not allowed
4820 -- except from aspect specification.
4822 if From_Aspect_Specification (N) then
4823 if not Is_Concurrent_Type (U_Ent) then
4824 Error_Msg_N
4825 ("Interrupt_Priority can only be defined for task "
4826 & "and protected object", Nam);
4828 elsif Duplicate_Clause then
4829 null;
4831 else
4832 -- The expression must be analyzed in the special manner
4833 -- described in "Handling of Default and Per-Object
4834 -- Expressions" in sem.ads.
4836 -- The visibility to the discriminants must be restored
4838 Push_Scope_And_Install_Discriminants (U_Ent);
4840 Preanalyze_Spec_Expression
4841 (Expr, RTE (RE_Interrupt_Priority));
4843 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4844 end if;
4846 else
4847 Error_Msg_N
4848 ("attribute& cannot be set with definition clause", N);
4849 end if;
4850 end Interrupt_Priority;
4852 --------------
4853 -- Iterable --
4854 --------------
4856 when Attribute_Iterable =>
4857 Analyze (Expr);
4859 if Nkind (Expr) /= N_Aggregate then
4860 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
4861 end if;
4863 declare
4864 Assoc : Node_Id;
4866 begin
4867 Assoc := First (Component_Associations (Expr));
4868 while Present (Assoc) loop
4869 if not Is_Entity_Name (Expression (Assoc)) then
4870 Error_Msg_N ("value must be a function", Assoc);
4871 end if;
4873 Next (Assoc);
4874 end loop;
4875 end;
4877 ----------------------
4878 -- Iterator_Element --
4879 ----------------------
4881 when Attribute_Iterator_Element =>
4882 Analyze (Expr);
4884 if not Is_Entity_Name (Expr)
4885 or else not Is_Type (Entity (Expr))
4886 then
4887 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4888 end if;
4890 -------------------
4891 -- Machine_Radix --
4892 -------------------
4894 -- Machine radix attribute definition clause
4896 when Attribute_Machine_Radix => Machine_Radix : declare
4897 Radix : constant Uint := Static_Integer (Expr);
4899 begin
4900 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
4901 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
4903 elsif Duplicate_Clause then
4904 null;
4906 elsif Radix /= No_Uint then
4907 Set_Has_Machine_Radix_Clause (U_Ent);
4908 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
4910 if Radix = 2 then
4911 null;
4912 elsif Radix = 10 then
4913 Set_Machine_Radix_10 (U_Ent);
4914 else
4915 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
4916 end if;
4917 end if;
4918 end Machine_Radix;
4920 -----------------
4921 -- Object_Size --
4922 -----------------
4924 -- Object_Size attribute definition clause
4926 when Attribute_Object_Size => Object_Size : declare
4927 Size : constant Uint := Static_Integer (Expr);
4929 Biased : Boolean;
4930 pragma Warnings (Off, Biased);
4932 begin
4933 if not Is_Type (U_Ent) then
4934 Error_Msg_N ("Object_Size cannot be given for &", Nam);
4936 elsif Duplicate_Clause then
4937 null;
4939 else
4940 Check_Size (Expr, U_Ent, Size, Biased);
4942 if Is_Scalar_Type (U_Ent) then
4943 if Size /= 8 and then Size /= 16 and then Size /= 32
4944 and then UI_Mod (Size, 64) /= 0
4945 then
4946 Error_Msg_N
4947 ("Object_Size must be 8, 16, 32, or multiple of 64",
4948 Expr);
4949 end if;
4951 elsif Size mod 8 /= 0 then
4952 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
4953 end if;
4955 Set_Esize (U_Ent, Size);
4956 Set_Has_Object_Size_Clause (U_Ent);
4957 Alignment_Check_For_Size_Change (U_Ent, Size);
4958 end if;
4959 end Object_Size;
4961 ------------
4962 -- Output --
4963 ------------
4965 when Attribute_Output =>
4966 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
4967 Set_Has_Specified_Stream_Output (Ent);
4969 --------------
4970 -- Priority --
4971 --------------
4973 when Attribute_Priority => Priority :
4974 begin
4975 -- Priority attribute definition clause not allowed except from
4976 -- aspect specification.
4978 if From_Aspect_Specification (N) then
4979 if not (Is_Concurrent_Type (U_Ent)
4980 or else Ekind (U_Ent) = E_Procedure)
4981 then
4982 Error_Msg_N
4983 ("Priority can only be defined for task and protected "
4984 & "object", Nam);
4986 elsif Duplicate_Clause then
4987 null;
4989 else
4990 -- The expression must be analyzed in the special manner
4991 -- described in "Handling of Default and Per-Object
4992 -- Expressions" in sem.ads.
4994 -- The visibility to the discriminants must be restored
4996 Push_Scope_And_Install_Discriminants (U_Ent);
4997 Preanalyze_Spec_Expression (Expr, Standard_Integer);
4998 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5000 if not Is_OK_Static_Expression (Expr) then
5001 Check_Restriction (Static_Priorities, Expr);
5002 end if;
5003 end if;
5005 else
5006 Error_Msg_N
5007 ("attribute& cannot be set with definition clause", N);
5008 end if;
5009 end Priority;
5011 ----------
5012 -- Read --
5013 ----------
5015 when Attribute_Read =>
5016 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5017 Set_Has_Specified_Stream_Read (Ent);
5019 --------------------------
5020 -- Scalar_Storage_Order --
5021 --------------------------
5023 -- Scalar_Storage_Order attribute definition clause
5025 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5026 begin
5027 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5028 Error_Msg_N
5029 ("Scalar_Storage_Order can only be defined for "
5030 & "record or array type", Nam);
5032 elsif Duplicate_Clause then
5033 null;
5035 else
5036 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5038 if Etype (Expr) = Any_Type then
5039 return;
5041 elsif not Is_OK_Static_Expression (Expr) then
5042 Flag_Non_Static_Expr
5043 ("Scalar_Storage_Order requires static expression!", Expr);
5045 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5047 -- Here for the case of a non-default (i.e. non-confirming)
5048 -- Scalar_Storage_Order attribute definition.
5050 if Support_Nondefault_SSO_On_Target then
5051 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5052 else
5053 Error_Msg_N
5054 ("non-default Scalar_Storage_Order "
5055 & "not supported on target", Expr);
5056 end if;
5057 end if;
5059 -- Clear SSO default indications since explicit setting of the
5060 -- order overrides the defaults.
5062 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5063 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5064 end if;
5065 end Scalar_Storage_Order;
5067 ----------
5068 -- Size --
5069 ----------
5071 -- Size attribute definition clause
5073 when Attribute_Size => Size : declare
5074 Size : constant Uint := Static_Integer (Expr);
5075 Etyp : Entity_Id;
5076 Biased : Boolean;
5078 begin
5079 FOnly := True;
5081 if Duplicate_Clause then
5082 null;
5084 elsif not Is_Type (U_Ent)
5085 and then Ekind (U_Ent) /= E_Variable
5086 and then Ekind (U_Ent) /= E_Constant
5087 then
5088 Error_Msg_N ("size cannot be given for &", Nam);
5090 elsif Is_Array_Type (U_Ent)
5091 and then not Is_Constrained (U_Ent)
5092 then
5093 Error_Msg_N
5094 ("size cannot be given for unconstrained array", Nam);
5096 elsif Size /= No_Uint then
5097 if VM_Target /= No_VM and then not GNAT_Mode then
5099 -- Size clause is not handled properly on VM targets.
5100 -- Display a warning unless we are in GNAT mode, in which
5101 -- case this is useless.
5103 Error_Msg_N
5104 ("size clauses are ignored in this configuration??", N);
5105 end if;
5107 if Is_Type (U_Ent) then
5108 Etyp := U_Ent;
5109 else
5110 Etyp := Etype (U_Ent);
5111 end if;
5113 -- Check size, note that Gigi is in charge of checking that the
5114 -- size of an array or record type is OK. Also we do not check
5115 -- the size in the ordinary fixed-point case, since it is too
5116 -- early to do so (there may be subsequent small clause that
5117 -- affects the size). We can check the size if a small clause
5118 -- has already been given.
5120 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5121 or else Has_Small_Clause (U_Ent)
5122 then
5123 Check_Size (Expr, Etyp, Size, Biased);
5124 Set_Biased (U_Ent, N, "size clause", Biased);
5125 end if;
5127 -- For types set RM_Size and Esize if possible
5129 if Is_Type (U_Ent) then
5130 Set_RM_Size (U_Ent, Size);
5132 -- For elementary types, increase Object_Size to power of 2,
5133 -- but not less than a storage unit in any case (normally
5134 -- this means it will be byte addressable).
5136 -- For all other types, nothing else to do, we leave Esize
5137 -- (object size) unset, the back end will set it from the
5138 -- size and alignment in an appropriate manner.
5140 -- In both cases, we check whether the alignment must be
5141 -- reset in the wake of the size change.
5143 if Is_Elementary_Type (U_Ent) then
5144 if Size <= System_Storage_Unit then
5145 Init_Esize (U_Ent, System_Storage_Unit);
5146 elsif Size <= 16 then
5147 Init_Esize (U_Ent, 16);
5148 elsif Size <= 32 then
5149 Init_Esize (U_Ent, 32);
5150 else
5151 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5152 end if;
5154 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5155 else
5156 Alignment_Check_For_Size_Change (U_Ent, Size);
5157 end if;
5159 -- For objects, set Esize only
5161 else
5162 if Is_Elementary_Type (Etyp) then
5163 if Size /= System_Storage_Unit
5164 and then
5165 Size /= System_Storage_Unit * 2
5166 and then
5167 Size /= System_Storage_Unit * 4
5168 and then
5169 Size /= System_Storage_Unit * 8
5170 then
5171 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5172 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5173 Error_Msg_N
5174 ("size for primitive object must be a power of 2"
5175 & " in the range ^-^", N);
5176 end if;
5177 end if;
5179 Set_Esize (U_Ent, Size);
5180 end if;
5182 Set_Has_Size_Clause (U_Ent);
5183 end if;
5184 end Size;
5186 -----------
5187 -- Small --
5188 -----------
5190 -- Small attribute definition clause
5192 when Attribute_Small => Small : declare
5193 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5194 Small : Ureal;
5196 begin
5197 Analyze_And_Resolve (Expr, Any_Real);
5199 if Etype (Expr) = Any_Type then
5200 return;
5202 elsif not Is_OK_Static_Expression (Expr) then
5203 Flag_Non_Static_Expr
5204 ("small requires static expression!", Expr);
5205 return;
5207 else
5208 Small := Expr_Value_R (Expr);
5210 if Small <= Ureal_0 then
5211 Error_Msg_N ("small value must be greater than zero", Expr);
5212 return;
5213 end if;
5215 end if;
5217 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5218 Error_Msg_N
5219 ("small requires an ordinary fixed point type", Nam);
5221 elsif Has_Small_Clause (U_Ent) then
5222 Error_Msg_N ("small already given for &", Nam);
5224 elsif Small > Delta_Value (U_Ent) then
5225 Error_Msg_N
5226 ("small value must not be greater than delta value", Nam);
5228 else
5229 Set_Small_Value (U_Ent, Small);
5230 Set_Small_Value (Implicit_Base, Small);
5231 Set_Has_Small_Clause (U_Ent);
5232 Set_Has_Small_Clause (Implicit_Base);
5233 Set_Has_Non_Standard_Rep (Implicit_Base);
5234 end if;
5235 end Small;
5237 ------------------
5238 -- Storage_Pool --
5239 ------------------
5241 -- Storage_Pool attribute definition clause
5243 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5244 Pool : Entity_Id;
5245 T : Entity_Id;
5247 begin
5248 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5249 Error_Msg_N
5250 ("storage pool cannot be given for access-to-subprogram type",
5251 Nam);
5252 return;
5254 elsif not
5255 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5256 then
5257 Error_Msg_N
5258 ("storage pool can only be given for access types", Nam);
5259 return;
5261 elsif Is_Derived_Type (U_Ent) then
5262 Error_Msg_N
5263 ("storage pool cannot be given for a derived access type",
5264 Nam);
5266 elsif Duplicate_Clause then
5267 return;
5269 elsif Present (Associated_Storage_Pool (U_Ent)) then
5270 Error_Msg_N ("storage pool already given for &", Nam);
5271 return;
5272 end if;
5274 -- Check for Storage_Size previously given
5276 declare
5277 SS : constant Node_Id :=
5278 Get_Attribute_Definition_Clause
5279 (U_Ent, Attribute_Storage_Size);
5280 begin
5281 if Present (SS) then
5282 Check_Pool_Size_Clash (U_Ent, N, SS);
5283 end if;
5284 end;
5286 -- Storage_Pool case
5288 if Id = Attribute_Storage_Pool then
5289 Analyze_And_Resolve
5290 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5292 -- In the Simple_Storage_Pool case, we allow a variable of any
5293 -- simple storage pool type, so we Resolve without imposing an
5294 -- expected type.
5296 else
5297 Analyze_And_Resolve (Expr);
5299 if not Present (Get_Rep_Pragma
5300 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5301 then
5302 Error_Msg_N
5303 ("expression must be of a simple storage pool type", Expr);
5304 end if;
5305 end if;
5307 if not Denotes_Variable (Expr) then
5308 Error_Msg_N ("storage pool must be a variable", Expr);
5309 return;
5310 end if;
5312 if Nkind (Expr) = N_Type_Conversion then
5313 T := Etype (Expression (Expr));
5314 else
5315 T := Etype (Expr);
5316 end if;
5318 -- The Stack_Bounded_Pool is used internally for implementing
5319 -- access types with a Storage_Size. Since it only work properly
5320 -- when used on one specific type, we need to check that it is not
5321 -- hijacked improperly:
5323 -- type T is access Integer;
5324 -- for T'Storage_Size use n;
5325 -- type Q is access Float;
5326 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5328 if RTE_Available (RE_Stack_Bounded_Pool)
5329 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5330 then
5331 Error_Msg_N ("non-shareable internal Pool", Expr);
5332 return;
5333 end if;
5335 -- If the argument is a name that is not an entity name, then
5336 -- we construct a renaming operation to define an entity of
5337 -- type storage pool.
5339 if not Is_Entity_Name (Expr)
5340 and then Is_Object_Reference (Expr)
5341 then
5342 Pool := Make_Temporary (Loc, 'P', Expr);
5344 declare
5345 Rnode : constant Node_Id :=
5346 Make_Object_Renaming_Declaration (Loc,
5347 Defining_Identifier => Pool,
5348 Subtype_Mark =>
5349 New_Occurrence_Of (Etype (Expr), Loc),
5350 Name => Expr);
5352 begin
5353 -- If the attribute definition clause comes from an aspect
5354 -- clause, then insert the renaming before the associated
5355 -- entity's declaration, since the attribute clause has
5356 -- not yet been appended to the declaration list.
5358 if From_Aspect_Specification (N) then
5359 Insert_Before (Parent (Entity (N)), Rnode);
5360 else
5361 Insert_Before (N, Rnode);
5362 end if;
5364 Analyze (Rnode);
5365 Set_Associated_Storage_Pool (U_Ent, Pool);
5366 end;
5368 elsif Is_Entity_Name (Expr) then
5369 Pool := Entity (Expr);
5371 -- If pool is a renamed object, get original one. This can
5372 -- happen with an explicit renaming, and within instances.
5374 while Present (Renamed_Object (Pool))
5375 and then Is_Entity_Name (Renamed_Object (Pool))
5376 loop
5377 Pool := Entity (Renamed_Object (Pool));
5378 end loop;
5380 if Present (Renamed_Object (Pool))
5381 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5382 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5383 then
5384 Pool := Entity (Expression (Renamed_Object (Pool)));
5385 end if;
5387 Set_Associated_Storage_Pool (U_Ent, Pool);
5389 elsif Nkind (Expr) = N_Type_Conversion
5390 and then Is_Entity_Name (Expression (Expr))
5391 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5392 then
5393 Pool := Entity (Expression (Expr));
5394 Set_Associated_Storage_Pool (U_Ent, Pool);
5396 else
5397 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5398 return;
5399 end if;
5400 end;
5402 ------------------
5403 -- Storage_Size --
5404 ------------------
5406 -- Storage_Size attribute definition clause
5408 when Attribute_Storage_Size => Storage_Size : declare
5409 Btype : constant Entity_Id := Base_Type (U_Ent);
5411 begin
5412 if Is_Task_Type (U_Ent) then
5414 -- Check obsolescent (but never obsolescent if from aspect)
5416 if not From_Aspect_Specification (N) then
5417 Check_Restriction (No_Obsolescent_Features, N);
5419 if Warn_On_Obsolescent_Feature then
5420 Error_Msg_N
5421 ("?j?storage size clause for task is an " &
5422 "obsolescent feature (RM J.9)", N);
5423 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5424 end if;
5425 end if;
5427 FOnly := True;
5428 end if;
5430 if not Is_Access_Type (U_Ent)
5431 and then Ekind (U_Ent) /= E_Task_Type
5432 then
5433 Error_Msg_N ("storage size cannot be given for &", Nam);
5435 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5436 Error_Msg_N
5437 ("storage size cannot be given for a derived access type",
5438 Nam);
5440 elsif Duplicate_Clause then
5441 null;
5443 else
5444 Analyze_And_Resolve (Expr, Any_Integer);
5446 if Is_Access_Type (U_Ent) then
5448 -- Check for Storage_Pool previously given
5450 declare
5451 SP : constant Node_Id :=
5452 Get_Attribute_Definition_Clause
5453 (U_Ent, Attribute_Storage_Pool);
5455 begin
5456 if Present (SP) then
5457 Check_Pool_Size_Clash (U_Ent, SP, N);
5458 end if;
5459 end;
5461 -- Special case of for x'Storage_Size use 0
5463 if Is_OK_Static_Expression (Expr)
5464 and then Expr_Value (Expr) = 0
5465 then
5466 Set_No_Pool_Assigned (Btype);
5467 end if;
5468 end if;
5470 Set_Has_Storage_Size_Clause (Btype);
5471 end if;
5472 end Storage_Size;
5474 -----------------
5475 -- Stream_Size --
5476 -----------------
5478 when Attribute_Stream_Size => Stream_Size : declare
5479 Size : constant Uint := Static_Integer (Expr);
5481 begin
5482 if Ada_Version <= Ada_95 then
5483 Check_Restriction (No_Implementation_Attributes, N);
5484 end if;
5486 if Duplicate_Clause then
5487 null;
5489 elsif Is_Elementary_Type (U_Ent) then
5490 if Size /= System_Storage_Unit
5491 and then
5492 Size /= System_Storage_Unit * 2
5493 and then
5494 Size /= System_Storage_Unit * 4
5495 and then
5496 Size /= System_Storage_Unit * 8
5497 then
5498 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5499 Error_Msg_N
5500 ("stream size for elementary type must be a"
5501 & " power of 2 and at least ^", N);
5503 elsif RM_Size (U_Ent) > Size then
5504 Error_Msg_Uint_1 := RM_Size (U_Ent);
5505 Error_Msg_N
5506 ("stream size for elementary type must be a"
5507 & " power of 2 and at least ^", N);
5508 end if;
5510 Set_Has_Stream_Size_Clause (U_Ent);
5512 else
5513 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5514 end if;
5515 end Stream_Size;
5517 ----------------
5518 -- Value_Size --
5519 ----------------
5521 -- Value_Size attribute definition clause
5523 when Attribute_Value_Size => Value_Size : declare
5524 Size : constant Uint := Static_Integer (Expr);
5525 Biased : Boolean;
5527 begin
5528 if not Is_Type (U_Ent) then
5529 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5531 elsif Duplicate_Clause then
5532 null;
5534 elsif Is_Array_Type (U_Ent)
5535 and then not Is_Constrained (U_Ent)
5536 then
5537 Error_Msg_N
5538 ("Value_Size cannot be given for unconstrained array", Nam);
5540 else
5541 if Is_Elementary_Type (U_Ent) then
5542 Check_Size (Expr, U_Ent, Size, Biased);
5543 Set_Biased (U_Ent, N, "value size clause", Biased);
5544 end if;
5546 Set_RM_Size (U_Ent, Size);
5547 end if;
5548 end Value_Size;
5550 -----------------------
5551 -- Variable_Indexing --
5552 -----------------------
5554 when Attribute_Variable_Indexing =>
5555 Check_Indexing_Functions;
5557 -----------
5558 -- Write --
5559 -----------
5561 when Attribute_Write =>
5562 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5563 Set_Has_Specified_Stream_Write (Ent);
5565 -- All other attributes cannot be set
5567 when others =>
5568 Error_Msg_N
5569 ("attribute& cannot be set with definition clause", N);
5570 end case;
5572 -- The test for the type being frozen must be performed after any
5573 -- expression the clause has been analyzed since the expression itself
5574 -- might cause freezing that makes the clause illegal.
5576 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5577 return;
5578 end if;
5579 end Analyze_Attribute_Definition_Clause;
5581 ----------------------------
5582 -- Analyze_Code_Statement --
5583 ----------------------------
5585 procedure Analyze_Code_Statement (N : Node_Id) is
5586 HSS : constant Node_Id := Parent (N);
5587 SBody : constant Node_Id := Parent (HSS);
5588 Subp : constant Entity_Id := Current_Scope;
5589 Stmt : Node_Id;
5590 Decl : Node_Id;
5591 StmtO : Node_Id;
5592 DeclO : Node_Id;
5594 begin
5595 -- Analyze and check we get right type, note that this implements the
5596 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5597 -- is the only way that Asm_Insn could possibly be visible.
5599 Analyze_And_Resolve (Expression (N));
5601 if Etype (Expression (N)) = Any_Type then
5602 return;
5603 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5604 Error_Msg_N ("incorrect type for code statement", N);
5605 return;
5606 end if;
5608 Check_Code_Statement (N);
5610 -- Make sure we appear in the handled statement sequence of a
5611 -- subprogram (RM 13.8(3)).
5613 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5614 or else Nkind (SBody) /= N_Subprogram_Body
5615 then
5616 Error_Msg_N
5617 ("code statement can only appear in body of subprogram", N);
5618 return;
5619 end if;
5621 -- Do remaining checks (RM 13.8(3)) if not already done
5623 if not Is_Machine_Code_Subprogram (Subp) then
5624 Set_Is_Machine_Code_Subprogram (Subp);
5626 -- No exception handlers allowed
5628 if Present (Exception_Handlers (HSS)) then
5629 Error_Msg_N
5630 ("exception handlers not permitted in machine code subprogram",
5631 First (Exception_Handlers (HSS)));
5632 end if;
5634 -- No declarations other than use clauses and pragmas (we allow
5635 -- certain internally generated declarations as well).
5637 Decl := First (Declarations (SBody));
5638 while Present (Decl) loop
5639 DeclO := Original_Node (Decl);
5640 if Comes_From_Source (DeclO)
5641 and not Nkind_In (DeclO, N_Pragma,
5642 N_Use_Package_Clause,
5643 N_Use_Type_Clause,
5644 N_Implicit_Label_Declaration)
5645 then
5646 Error_Msg_N
5647 ("this declaration not allowed in machine code subprogram",
5648 DeclO);
5649 end if;
5651 Next (Decl);
5652 end loop;
5654 -- No statements other than code statements, pragmas, and labels.
5655 -- Again we allow certain internally generated statements.
5657 -- In Ada 2012, qualified expressions are names, and the code
5658 -- statement is initially parsed as a procedure call.
5660 Stmt := First (Statements (HSS));
5661 while Present (Stmt) loop
5662 StmtO := Original_Node (Stmt);
5664 -- A procedure call transformed into a code statement is OK.
5666 if Ada_Version >= Ada_2012
5667 and then Nkind (StmtO) = N_Procedure_Call_Statement
5668 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5669 then
5670 null;
5672 elsif Comes_From_Source (StmtO)
5673 and then not Nkind_In (StmtO, N_Pragma,
5674 N_Label,
5675 N_Code_Statement)
5676 then
5677 Error_Msg_N
5678 ("this statement is not allowed in machine code subprogram",
5679 StmtO);
5680 end if;
5682 Next (Stmt);
5683 end loop;
5684 end if;
5685 end Analyze_Code_Statement;
5687 -----------------------------------------------
5688 -- Analyze_Enumeration_Representation_Clause --
5689 -----------------------------------------------
5691 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5692 Ident : constant Node_Id := Identifier (N);
5693 Aggr : constant Node_Id := Array_Aggregate (N);
5694 Enumtype : Entity_Id;
5695 Elit : Entity_Id;
5696 Expr : Node_Id;
5697 Assoc : Node_Id;
5698 Choice : Node_Id;
5699 Val : Uint;
5701 Err : Boolean := False;
5702 -- Set True to avoid cascade errors and crashes on incorrect source code
5704 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5705 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5706 -- Allowed range of universal integer (= allowed range of enum lit vals)
5708 Min : Uint;
5709 Max : Uint;
5710 -- Minimum and maximum values of entries
5712 Max_Node : Node_Id;
5713 -- Pointer to node for literal providing max value
5715 begin
5716 if Ignore_Rep_Clauses then
5717 Kill_Rep_Clause (N);
5718 return;
5719 end if;
5721 -- Ignore enumeration rep clauses by default in CodePeer mode,
5722 -- unless -gnatd.I is specified, as a work around for potential false
5723 -- positive messages.
5725 if CodePeer_Mode and not Debug_Flag_Dot_II then
5726 return;
5727 end if;
5729 -- First some basic error checks
5731 Find_Type (Ident);
5732 Enumtype := Entity (Ident);
5734 if Enumtype = Any_Type
5735 or else Rep_Item_Too_Early (Enumtype, N)
5736 then
5737 return;
5738 else
5739 Enumtype := Underlying_Type (Enumtype);
5740 end if;
5742 if not Is_Enumeration_Type (Enumtype) then
5743 Error_Msg_NE
5744 ("enumeration type required, found}",
5745 Ident, First_Subtype (Enumtype));
5746 return;
5747 end if;
5749 -- Ignore rep clause on generic actual type. This will already have
5750 -- been flagged on the template as an error, and this is the safest
5751 -- way to ensure we don't get a junk cascaded message in the instance.
5753 if Is_Generic_Actual_Type (Enumtype) then
5754 return;
5756 -- Type must be in current scope
5758 elsif Scope (Enumtype) /= Current_Scope then
5759 Error_Msg_N ("type must be declared in this scope", Ident);
5760 return;
5762 -- Type must be a first subtype
5764 elsif not Is_First_Subtype (Enumtype) then
5765 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5766 return;
5768 -- Ignore duplicate rep clause
5770 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5771 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5772 return;
5774 -- Don't allow rep clause for standard [wide_[wide_]]character
5776 elsif Is_Standard_Character_Type (Enumtype) then
5777 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5778 return;
5780 -- Check that the expression is a proper aggregate (no parentheses)
5782 elsif Paren_Count (Aggr) /= 0 then
5783 Error_Msg
5784 ("extra parentheses surrounding aggregate not allowed",
5785 First_Sloc (Aggr));
5786 return;
5788 -- All tests passed, so set rep clause in place
5790 else
5791 Set_Has_Enumeration_Rep_Clause (Enumtype);
5792 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5793 end if;
5795 -- Now we process the aggregate. Note that we don't use the normal
5796 -- aggregate code for this purpose, because we don't want any of the
5797 -- normal expansion activities, and a number of special semantic
5798 -- rules apply (including the component type being any integer type)
5800 Elit := First_Literal (Enumtype);
5802 -- First the positional entries if any
5804 if Present (Expressions (Aggr)) then
5805 Expr := First (Expressions (Aggr));
5806 while Present (Expr) loop
5807 if No (Elit) then
5808 Error_Msg_N ("too many entries in aggregate", Expr);
5809 return;
5810 end if;
5812 Val := Static_Integer (Expr);
5814 -- Err signals that we found some incorrect entries processing
5815 -- the list. The final checks for completeness and ordering are
5816 -- skipped in this case.
5818 if Val = No_Uint then
5819 Err := True;
5821 elsif Val < Lo or else Hi < Val then
5822 Error_Msg_N ("value outside permitted range", Expr);
5823 Err := True;
5824 end if;
5826 Set_Enumeration_Rep (Elit, Val);
5827 Set_Enumeration_Rep_Expr (Elit, Expr);
5828 Next (Expr);
5829 Next (Elit);
5830 end loop;
5831 end if;
5833 -- Now process the named entries if present
5835 if Present (Component_Associations (Aggr)) then
5836 Assoc := First (Component_Associations (Aggr));
5837 while Present (Assoc) loop
5838 Choice := First (Choices (Assoc));
5840 if Present (Next (Choice)) then
5841 Error_Msg_N
5842 ("multiple choice not allowed here", Next (Choice));
5843 Err := True;
5844 end if;
5846 if Nkind (Choice) = N_Others_Choice then
5847 Error_Msg_N ("others choice not allowed here", Choice);
5848 Err := True;
5850 elsif Nkind (Choice) = N_Range then
5852 -- ??? should allow zero/one element range here
5854 Error_Msg_N ("range not allowed here", Choice);
5855 Err := True;
5857 else
5858 Analyze_And_Resolve (Choice, Enumtype);
5860 if Error_Posted (Choice) then
5861 Err := True;
5862 end if;
5864 if not Err then
5865 if Is_Entity_Name (Choice)
5866 and then Is_Type (Entity (Choice))
5867 then
5868 Error_Msg_N ("subtype name not allowed here", Choice);
5869 Err := True;
5871 -- ??? should allow static subtype with zero/one entry
5873 elsif Etype (Choice) = Base_Type (Enumtype) then
5874 if not Is_OK_Static_Expression (Choice) then
5875 Flag_Non_Static_Expr
5876 ("non-static expression used for choice!", Choice);
5877 Err := True;
5879 else
5880 Elit := Expr_Value_E (Choice);
5882 if Present (Enumeration_Rep_Expr (Elit)) then
5883 Error_Msg_Sloc :=
5884 Sloc (Enumeration_Rep_Expr (Elit));
5885 Error_Msg_NE
5886 ("representation for& previously given#",
5887 Choice, Elit);
5888 Err := True;
5889 end if;
5891 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
5893 Expr := Expression (Assoc);
5894 Val := Static_Integer (Expr);
5896 if Val = No_Uint then
5897 Err := True;
5899 elsif Val < Lo or else Hi < Val then
5900 Error_Msg_N ("value outside permitted range", Expr);
5901 Err := True;
5902 end if;
5904 Set_Enumeration_Rep (Elit, Val);
5905 end if;
5906 end if;
5907 end if;
5908 end if;
5910 Next (Assoc);
5911 end loop;
5912 end if;
5914 -- Aggregate is fully processed. Now we check that a full set of
5915 -- representations was given, and that they are in range and in order.
5916 -- These checks are only done if no other errors occurred.
5918 if not Err then
5919 Min := No_Uint;
5920 Max := No_Uint;
5922 Elit := First_Literal (Enumtype);
5923 while Present (Elit) loop
5924 if No (Enumeration_Rep_Expr (Elit)) then
5925 Error_Msg_NE ("missing representation for&!", N, Elit);
5927 else
5928 Val := Enumeration_Rep (Elit);
5930 if Min = No_Uint then
5931 Min := Val;
5932 end if;
5934 if Val /= No_Uint then
5935 if Max /= No_Uint and then Val <= Max then
5936 Error_Msg_NE
5937 ("enumeration value for& not ordered!",
5938 Enumeration_Rep_Expr (Elit), Elit);
5939 end if;
5941 Max_Node := Enumeration_Rep_Expr (Elit);
5942 Max := Val;
5943 end if;
5945 -- If there is at least one literal whose representation is not
5946 -- equal to the Pos value, then note that this enumeration type
5947 -- has a non-standard representation.
5949 if Val /= Enumeration_Pos (Elit) then
5950 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
5951 end if;
5952 end if;
5954 Next (Elit);
5955 end loop;
5957 -- Now set proper size information
5959 declare
5960 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
5962 begin
5963 if Has_Size_Clause (Enumtype) then
5965 -- All OK, if size is OK now
5967 if RM_Size (Enumtype) >= Minsize then
5968 null;
5970 else
5971 -- Try if we can get by with biasing
5973 Minsize :=
5974 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
5976 -- Error message if even biasing does not work
5978 if RM_Size (Enumtype) < Minsize then
5979 Error_Msg_Uint_1 := RM_Size (Enumtype);
5980 Error_Msg_Uint_2 := Max;
5981 Error_Msg_N
5982 ("previously given size (^) is too small "
5983 & "for this value (^)", Max_Node);
5985 -- If biasing worked, indicate that we now have biased rep
5987 else
5988 Set_Biased
5989 (Enumtype, Size_Clause (Enumtype), "size clause");
5990 end if;
5991 end if;
5993 else
5994 Set_RM_Size (Enumtype, Minsize);
5995 Set_Enum_Esize (Enumtype);
5996 end if;
5998 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
5999 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6000 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6001 end;
6002 end if;
6004 -- We repeat the too late test in case it froze itself
6006 if Rep_Item_Too_Late (Enumtype, N) then
6007 null;
6008 end if;
6009 end Analyze_Enumeration_Representation_Clause;
6011 ----------------------------
6012 -- Analyze_Free_Statement --
6013 ----------------------------
6015 procedure Analyze_Free_Statement (N : Node_Id) is
6016 begin
6017 Analyze (Expression (N));
6018 end Analyze_Free_Statement;
6020 ---------------------------
6021 -- Analyze_Freeze_Entity --
6022 ---------------------------
6024 procedure Analyze_Freeze_Entity (N : Node_Id) is
6025 begin
6026 Freeze_Entity_Checks (N);
6027 end Analyze_Freeze_Entity;
6029 -----------------------------------
6030 -- Analyze_Freeze_Generic_Entity --
6031 -----------------------------------
6033 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6034 begin
6035 Freeze_Entity_Checks (N);
6036 end Analyze_Freeze_Generic_Entity;
6038 ------------------------------------------
6039 -- Analyze_Record_Representation_Clause --
6040 ------------------------------------------
6042 -- Note: we check as much as we can here, but we can't do any checks
6043 -- based on the position values (e.g. overlap checks) until freeze time
6044 -- because especially in Ada 2005 (machine scalar mode), the processing
6045 -- for non-standard bit order can substantially change the positions.
6046 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6047 -- for the remainder of this processing.
6049 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6050 Ident : constant Node_Id := Identifier (N);
6051 Biased : Boolean;
6052 CC : Node_Id;
6053 Comp : Entity_Id;
6054 Fbit : Uint;
6055 Hbit : Uint := Uint_0;
6056 Lbit : Uint;
6057 Ocomp : Entity_Id;
6058 Posit : Uint;
6059 Rectype : Entity_Id;
6060 Recdef : Node_Id;
6062 function Is_Inherited (Comp : Entity_Id) return Boolean;
6063 -- True if Comp is an inherited component in a record extension
6065 ------------------
6066 -- Is_Inherited --
6067 ------------------
6069 function Is_Inherited (Comp : Entity_Id) return Boolean is
6070 Comp_Base : Entity_Id;
6072 begin
6073 if Ekind (Rectype) = E_Record_Subtype then
6074 Comp_Base := Original_Record_Component (Comp);
6075 else
6076 Comp_Base := Comp;
6077 end if;
6079 return Comp_Base /= Original_Record_Component (Comp_Base);
6080 end Is_Inherited;
6082 -- Local variables
6084 Is_Record_Extension : Boolean;
6085 -- True if Rectype is a record extension
6087 CR_Pragma : Node_Id := Empty;
6088 -- Points to N_Pragma node if Complete_Representation pragma present
6090 -- Start of processing for Analyze_Record_Representation_Clause
6092 begin
6093 if Ignore_Rep_Clauses then
6094 Kill_Rep_Clause (N);
6095 return;
6096 end if;
6098 Find_Type (Ident);
6099 Rectype := Entity (Ident);
6101 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6102 return;
6103 else
6104 Rectype := Underlying_Type (Rectype);
6105 end if;
6107 -- First some basic error checks
6109 if not Is_Record_Type (Rectype) then
6110 Error_Msg_NE
6111 ("record type required, found}", Ident, First_Subtype (Rectype));
6112 return;
6114 elsif Scope (Rectype) /= Current_Scope then
6115 Error_Msg_N ("type must be declared in this scope", N);
6116 return;
6118 elsif not Is_First_Subtype (Rectype) then
6119 Error_Msg_N ("cannot give record rep clause for subtype", N);
6120 return;
6122 elsif Has_Record_Rep_Clause (Rectype) then
6123 Error_Msg_N ("duplicate record rep clause ignored", N);
6124 return;
6126 elsif Rep_Item_Too_Late (Rectype, N) then
6127 return;
6128 end if;
6130 -- We know we have a first subtype, now possibly go the the anonymous
6131 -- base type to determine whether Rectype is a record extension.
6133 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6134 Is_Record_Extension :=
6135 Nkind (Recdef) = N_Derived_Type_Definition
6136 and then Present (Record_Extension_Part (Recdef));
6138 if Present (Mod_Clause (N)) then
6139 declare
6140 Loc : constant Source_Ptr := Sloc (N);
6141 M : constant Node_Id := Mod_Clause (N);
6142 P : constant List_Id := Pragmas_Before (M);
6143 AtM_Nod : Node_Id;
6145 Mod_Val : Uint;
6146 pragma Warnings (Off, Mod_Val);
6148 begin
6149 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6151 if Warn_On_Obsolescent_Feature then
6152 Error_Msg_N
6153 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6154 Error_Msg_N
6155 ("\?j?use alignment attribute definition clause instead", N);
6156 end if;
6158 if Present (P) then
6159 Analyze_List (P);
6160 end if;
6162 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6163 -- the Mod clause into an alignment clause anyway, so that the
6164 -- back-end can compute and back-annotate properly the size and
6165 -- alignment of types that may include this record.
6167 -- This seems dubious, this destroys the source tree in a manner
6168 -- not detectable by ASIS ???
6170 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6171 AtM_Nod :=
6172 Make_Attribute_Definition_Clause (Loc,
6173 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6174 Chars => Name_Alignment,
6175 Expression => Relocate_Node (Expression (M)));
6177 Set_From_At_Mod (AtM_Nod);
6178 Insert_After (N, AtM_Nod);
6179 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6180 Set_Mod_Clause (N, Empty);
6182 else
6183 -- Get the alignment value to perform error checking
6185 Mod_Val := Get_Alignment_Value (Expression (M));
6186 end if;
6187 end;
6188 end if;
6190 -- For untagged types, clear any existing component clauses for the
6191 -- type. If the type is derived, this is what allows us to override
6192 -- a rep clause for the parent. For type extensions, the representation
6193 -- of the inherited components is inherited, so we want to keep previous
6194 -- component clauses for completeness.
6196 if not Is_Tagged_Type (Rectype) then
6197 Comp := First_Component_Or_Discriminant (Rectype);
6198 while Present (Comp) loop
6199 Set_Component_Clause (Comp, Empty);
6200 Next_Component_Or_Discriminant (Comp);
6201 end loop;
6202 end if;
6204 -- All done if no component clauses
6206 CC := First (Component_Clauses (N));
6208 if No (CC) then
6209 return;
6210 end if;
6212 -- A representation like this applies to the base type
6214 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6215 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6216 Set_Has_Specified_Layout (Base_Type (Rectype));
6218 -- Process the component clauses
6220 while Present (CC) loop
6222 -- Pragma
6224 if Nkind (CC) = N_Pragma then
6225 Analyze (CC);
6227 -- The only pragma of interest is Complete_Representation
6229 if Pragma_Name (CC) = Name_Complete_Representation then
6230 CR_Pragma := CC;
6231 end if;
6233 -- Processing for real component clause
6235 else
6236 Posit := Static_Integer (Position (CC));
6237 Fbit := Static_Integer (First_Bit (CC));
6238 Lbit := Static_Integer (Last_Bit (CC));
6240 if Posit /= No_Uint
6241 and then Fbit /= No_Uint
6242 and then Lbit /= No_Uint
6243 then
6244 if Posit < 0 then
6245 Error_Msg_N
6246 ("position cannot be negative", Position (CC));
6248 elsif Fbit < 0 then
6249 Error_Msg_N
6250 ("first bit cannot be negative", First_Bit (CC));
6252 -- The Last_Bit specified in a component clause must not be
6253 -- less than the First_Bit minus one (RM-13.5.1(10)).
6255 elsif Lbit < Fbit - 1 then
6256 Error_Msg_N
6257 ("last bit cannot be less than first bit minus one",
6258 Last_Bit (CC));
6260 -- Values look OK, so find the corresponding record component
6261 -- Even though the syntax allows an attribute reference for
6262 -- implementation-defined components, GNAT does not allow the
6263 -- tag to get an explicit position.
6265 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6266 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6267 Error_Msg_N ("position of tag cannot be specified", CC);
6268 else
6269 Error_Msg_N ("illegal component name", CC);
6270 end if;
6272 else
6273 Comp := First_Entity (Rectype);
6274 while Present (Comp) loop
6275 exit when Chars (Comp) = Chars (Component_Name (CC));
6276 Next_Entity (Comp);
6277 end loop;
6279 if No (Comp) then
6281 -- Maybe component of base type that is absent from
6282 -- statically constrained first subtype.
6284 Comp := First_Entity (Base_Type (Rectype));
6285 while Present (Comp) loop
6286 exit when Chars (Comp) = Chars (Component_Name (CC));
6287 Next_Entity (Comp);
6288 end loop;
6289 end if;
6291 if No (Comp) then
6292 Error_Msg_N
6293 ("component clause is for non-existent field", CC);
6295 -- Ada 2012 (AI05-0026): Any name that denotes a
6296 -- discriminant of an object of an unchecked union type
6297 -- shall not occur within a record_representation_clause.
6299 -- The general restriction of using record rep clauses on
6300 -- Unchecked_Union types has now been lifted. Since it is
6301 -- possible to introduce a record rep clause which mentions
6302 -- the discriminant of an Unchecked_Union in non-Ada 2012
6303 -- code, this check is applied to all versions of the
6304 -- language.
6306 elsif Ekind (Comp) = E_Discriminant
6307 and then Is_Unchecked_Union (Rectype)
6308 then
6309 Error_Msg_N
6310 ("cannot reference discriminant of unchecked union",
6311 Component_Name (CC));
6313 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6314 Error_Msg_NE
6315 ("component clause not allowed for inherited "
6316 & "component&", CC, Comp);
6318 elsif Present (Component_Clause (Comp)) then
6320 -- Diagnose duplicate rep clause, or check consistency
6321 -- if this is an inherited component. In a double fault,
6322 -- there may be a duplicate inconsistent clause for an
6323 -- inherited component.
6325 if Scope (Original_Record_Component (Comp)) = Rectype
6326 or else Parent (Component_Clause (Comp)) = N
6327 then
6328 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6329 Error_Msg_N ("component clause previously given#", CC);
6331 else
6332 declare
6333 Rep1 : constant Node_Id := Component_Clause (Comp);
6334 begin
6335 if Intval (Position (Rep1)) /=
6336 Intval (Position (CC))
6337 or else Intval (First_Bit (Rep1)) /=
6338 Intval (First_Bit (CC))
6339 or else Intval (Last_Bit (Rep1)) /=
6340 Intval (Last_Bit (CC))
6341 then
6342 Error_Msg_N
6343 ("component clause inconsistent "
6344 & "with representation of ancestor", CC);
6346 elsif Warn_On_Redundant_Constructs then
6347 Error_Msg_N
6348 ("?r?redundant confirming component clause "
6349 & "for component!", CC);
6350 end if;
6351 end;
6352 end if;
6354 -- Normal case where this is the first component clause we
6355 -- have seen for this entity, so set it up properly.
6357 else
6358 -- Make reference for field in record rep clause and set
6359 -- appropriate entity field in the field identifier.
6361 Generate_Reference
6362 (Comp, Component_Name (CC), Set_Ref => False);
6363 Set_Entity (Component_Name (CC), Comp);
6365 -- Update Fbit and Lbit to the actual bit number
6367 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6368 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6370 if Has_Size_Clause (Rectype)
6371 and then RM_Size (Rectype) <= Lbit
6372 then
6373 Error_Msg_N
6374 ("bit number out of range of specified size",
6375 Last_Bit (CC));
6376 else
6377 Set_Component_Clause (Comp, CC);
6378 Set_Component_Bit_Offset (Comp, Fbit);
6379 Set_Esize (Comp, 1 + (Lbit - Fbit));
6380 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6381 Set_Normalized_Position (Comp, Fbit / SSU);
6383 if Warn_On_Overridden_Size
6384 and then Has_Size_Clause (Etype (Comp))
6385 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6386 then
6387 Error_Msg_NE
6388 ("?S?component size overrides size clause for&",
6389 Component_Name (CC), Etype (Comp));
6390 end if;
6392 -- This information is also set in the corresponding
6393 -- component of the base type, found by accessing the
6394 -- Original_Record_Component link if it is present.
6396 Ocomp := Original_Record_Component (Comp);
6398 if Hbit < Lbit then
6399 Hbit := Lbit;
6400 end if;
6402 Check_Size
6403 (Component_Name (CC),
6404 Etype (Comp),
6405 Esize (Comp),
6406 Biased);
6408 Set_Biased
6409 (Comp, First_Node (CC), "component clause", Biased);
6411 if Present (Ocomp) then
6412 Set_Component_Clause (Ocomp, CC);
6413 Set_Component_Bit_Offset (Ocomp, Fbit);
6414 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6415 Set_Normalized_Position (Ocomp, Fbit / SSU);
6416 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6418 Set_Normalized_Position_Max
6419 (Ocomp, Normalized_Position (Ocomp));
6421 -- Note: we don't use Set_Biased here, because we
6422 -- already gave a warning above if needed, and we
6423 -- would get a duplicate for the same name here.
6425 Set_Has_Biased_Representation
6426 (Ocomp, Has_Biased_Representation (Comp));
6427 end if;
6429 if Esize (Comp) < 0 then
6430 Error_Msg_N ("component size is negative", CC);
6431 end if;
6432 end if;
6433 end if;
6434 end if;
6435 end if;
6436 end if;
6438 Next (CC);
6439 end loop;
6441 -- Check missing components if Complete_Representation pragma appeared
6443 if Present (CR_Pragma) then
6444 Comp := First_Component_Or_Discriminant (Rectype);
6445 while Present (Comp) loop
6446 if No (Component_Clause (Comp)) then
6447 Error_Msg_NE
6448 ("missing component clause for &", CR_Pragma, Comp);
6449 end if;
6451 Next_Component_Or_Discriminant (Comp);
6452 end loop;
6454 -- Give missing components warning if required
6456 elsif Warn_On_Unrepped_Components then
6457 declare
6458 Num_Repped_Components : Nat := 0;
6459 Num_Unrepped_Components : Nat := 0;
6461 begin
6462 -- First count number of repped and unrepped components
6464 Comp := First_Component_Or_Discriminant (Rectype);
6465 while Present (Comp) loop
6466 if Present (Component_Clause (Comp)) then
6467 Num_Repped_Components := Num_Repped_Components + 1;
6468 else
6469 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6470 end if;
6472 Next_Component_Or_Discriminant (Comp);
6473 end loop;
6475 -- We are only interested in the case where there is at least one
6476 -- unrepped component, and at least half the components have rep
6477 -- clauses. We figure that if less than half have them, then the
6478 -- partial rep clause is really intentional. If the component
6479 -- type has no underlying type set at this point (as for a generic
6480 -- formal type), we don't know enough to give a warning on the
6481 -- component.
6483 if Num_Unrepped_Components > 0
6484 and then Num_Unrepped_Components < Num_Repped_Components
6485 then
6486 Comp := First_Component_Or_Discriminant (Rectype);
6487 while Present (Comp) loop
6488 if No (Component_Clause (Comp))
6489 and then Comes_From_Source (Comp)
6490 and then Present (Underlying_Type (Etype (Comp)))
6491 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6492 or else Size_Known_At_Compile_Time
6493 (Underlying_Type (Etype (Comp))))
6494 and then not Has_Warnings_Off (Rectype)
6495 then
6496 Error_Msg_Sloc := Sloc (Comp);
6497 Error_Msg_NE
6498 ("?C?no component clause given for & declared #",
6499 N, Comp);
6500 end if;
6502 Next_Component_Or_Discriminant (Comp);
6503 end loop;
6504 end if;
6505 end;
6506 end if;
6507 end Analyze_Record_Representation_Clause;
6509 -------------------------------------
6510 -- Build_Discrete_Static_Predicate --
6511 -------------------------------------
6513 procedure Build_Discrete_Static_Predicate
6514 (Typ : Entity_Id;
6515 Expr : Node_Id;
6516 Nam : Name_Id)
6518 Loc : constant Source_Ptr := Sloc (Expr);
6520 Non_Static : exception;
6521 -- Raised if something non-static is found
6523 Btyp : constant Entity_Id := Base_Type (Typ);
6525 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6526 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6527 -- Low bound and high bound value of base type of Typ
6529 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6530 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6531 -- Low bound and high bound values of static subtype Typ
6533 type REnt is record
6534 Lo, Hi : Uint;
6535 end record;
6536 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6537 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6538 -- value.
6540 type RList is array (Nat range <>) of REnt;
6541 -- A list of ranges. The ranges are sorted in increasing order, and are
6542 -- disjoint (there is a gap of at least one value between each range in
6543 -- the table). A value is in the set of ranges in Rlist if it lies
6544 -- within one of these ranges.
6546 False_Range : constant RList :=
6547 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6548 -- An empty set of ranges represents a range list that can never be
6549 -- satisfied, since there are no ranges in which the value could lie,
6550 -- so it does not lie in any of them. False_Range is a canonical value
6551 -- for this empty set, but general processing should test for an Rlist
6552 -- with length zero (see Is_False predicate), since other null ranges
6553 -- may appear which must be treated as False.
6555 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6556 -- Range representing True, value must be in the base range
6558 function "and" (Left : RList; Right : RList) return RList;
6559 -- And's together two range lists, returning a range list. This is a set
6560 -- intersection operation.
6562 function "or" (Left : RList; Right : RList) return RList;
6563 -- Or's together two range lists, returning a range list. This is a set
6564 -- union operation.
6566 function "not" (Right : RList) return RList;
6567 -- Returns complement of a given range list, i.e. a range list
6568 -- representing all the values in TLo .. THi that are not in the input
6569 -- operand Right.
6571 function Build_Val (V : Uint) return Node_Id;
6572 -- Return an analyzed N_Identifier node referencing this value, suitable
6573 -- for use as an entry in the Static_Discrte_Predicate list. This node
6574 -- is typed with the base type.
6576 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6577 -- Return an analyzed N_Range node referencing this range, suitable for
6578 -- use as an entry in the Static_Discrete_Predicate list. This node is
6579 -- typed with the base type.
6581 function Get_RList (Exp : Node_Id) return RList;
6582 -- This is a recursive routine that converts the given expression into a
6583 -- list of ranges, suitable for use in building the static predicate.
6585 function Is_False (R : RList) return Boolean;
6586 pragma Inline (Is_False);
6587 -- Returns True if the given range list is empty, and thus represents a
6588 -- False list of ranges that can never be satisfied.
6590 function Is_True (R : RList) return Boolean;
6591 -- Returns True if R trivially represents the True predicate by having a
6592 -- single range from BLo to BHi.
6594 function Is_Type_Ref (N : Node_Id) return Boolean;
6595 pragma Inline (Is_Type_Ref);
6596 -- Returns if True if N is a reference to the type for the predicate in
6597 -- the expression (i.e. if it is an identifier whose Chars field matches
6598 -- the Nam given in the call). N must not be parenthesized, if the type
6599 -- name appears in parens, this routine will return False.
6601 function Lo_Val (N : Node_Id) return Uint;
6602 -- Given an entry from a Static_Discrete_Predicate list that is either
6603 -- a static expression or static range, gets either the expression value
6604 -- or the low bound of the range.
6606 function Hi_Val (N : Node_Id) return Uint;
6607 -- Given an entry from a Static_Discrete_Predicate list that is either
6608 -- a static expression or static range, gets either the expression value
6609 -- or the high bound of the range.
6611 function Membership_Entry (N : Node_Id) return RList;
6612 -- Given a single membership entry (range, value, or subtype), returns
6613 -- the corresponding range list. Raises Static_Error if not static.
6615 function Membership_Entries (N : Node_Id) return RList;
6616 -- Given an element on an alternatives list of a membership operation,
6617 -- returns the range list corresponding to this entry and all following
6618 -- entries (i.e. returns the "or" of this list of values).
6620 function Stat_Pred (Typ : Entity_Id) return RList;
6621 -- Given a type, if it has a static predicate, then return the predicate
6622 -- as a range list, otherwise raise Non_Static.
6624 -----------
6625 -- "and" --
6626 -----------
6628 function "and" (Left : RList; Right : RList) return RList is
6629 FEnt : REnt;
6630 -- First range of result
6632 SLeft : Nat := Left'First;
6633 -- Start of rest of left entries
6635 SRight : Nat := Right'First;
6636 -- Start of rest of right entries
6638 begin
6639 -- If either range is True, return the other
6641 if Is_True (Left) then
6642 return Right;
6643 elsif Is_True (Right) then
6644 return Left;
6645 end if;
6647 -- If either range is False, return False
6649 if Is_False (Left) or else Is_False (Right) then
6650 return False_Range;
6651 end if;
6653 -- Loop to remove entries at start that are disjoint, and thus just
6654 -- get discarded from the result entirely.
6656 loop
6657 -- If no operands left in either operand, result is false
6659 if SLeft > Left'Last or else SRight > Right'Last then
6660 return False_Range;
6662 -- Discard first left operand entry if disjoint with right
6664 elsif Left (SLeft).Hi < Right (SRight).Lo then
6665 SLeft := SLeft + 1;
6667 -- Discard first right operand entry if disjoint with left
6669 elsif Right (SRight).Hi < Left (SLeft).Lo then
6670 SRight := SRight + 1;
6672 -- Otherwise we have an overlapping entry
6674 else
6675 exit;
6676 end if;
6677 end loop;
6679 -- Now we have two non-null operands, and first entries overlap. The
6680 -- first entry in the result will be the overlapping part of these
6681 -- two entries.
6683 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6684 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6686 -- Now we can remove the entry that ended at a lower value, since its
6687 -- contribution is entirely contained in Fent.
6689 if Left (SLeft).Hi <= Right (SRight).Hi then
6690 SLeft := SLeft + 1;
6691 else
6692 SRight := SRight + 1;
6693 end if;
6695 -- Compute result by concatenating this first entry with the "and" of
6696 -- the remaining parts of the left and right operands. Note that if
6697 -- either of these is empty, "and" will yield empty, so that we will
6698 -- end up with just Fent, which is what we want in that case.
6700 return
6701 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6702 end "and";
6704 -----------
6705 -- "not" --
6706 -----------
6708 function "not" (Right : RList) return RList is
6709 begin
6710 -- Return True if False range
6712 if Is_False (Right) then
6713 return True_Range;
6714 end if;
6716 -- Return False if True range
6718 if Is_True (Right) then
6719 return False_Range;
6720 end if;
6722 -- Here if not trivial case
6724 declare
6725 Result : RList (1 .. Right'Length + 1);
6726 -- May need one more entry for gap at beginning and end
6728 Count : Nat := 0;
6729 -- Number of entries stored in Result
6731 begin
6732 -- Gap at start
6734 if Right (Right'First).Lo > TLo then
6735 Count := Count + 1;
6736 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6737 end if;
6739 -- Gaps between ranges
6741 for J in Right'First .. Right'Last - 1 loop
6742 Count := Count + 1;
6743 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6744 end loop;
6746 -- Gap at end
6748 if Right (Right'Last).Hi < THi then
6749 Count := Count + 1;
6750 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6751 end if;
6753 return Result (1 .. Count);
6754 end;
6755 end "not";
6757 ----------
6758 -- "or" --
6759 ----------
6761 function "or" (Left : RList; Right : RList) return RList is
6762 FEnt : REnt;
6763 -- First range of result
6765 SLeft : Nat := Left'First;
6766 -- Start of rest of left entries
6768 SRight : Nat := Right'First;
6769 -- Start of rest of right entries
6771 begin
6772 -- If either range is True, return True
6774 if Is_True (Left) or else Is_True (Right) then
6775 return True_Range;
6776 end if;
6778 -- If either range is False (empty), return the other
6780 if Is_False (Left) then
6781 return Right;
6782 elsif Is_False (Right) then
6783 return Left;
6784 end if;
6786 -- Initialize result first entry from left or right operand depending
6787 -- on which starts with the lower range.
6789 if Left (SLeft).Lo < Right (SRight).Lo then
6790 FEnt := Left (SLeft);
6791 SLeft := SLeft + 1;
6792 else
6793 FEnt := Right (SRight);
6794 SRight := SRight + 1;
6795 end if;
6797 -- This loop eats ranges from left and right operands that are
6798 -- contiguous with the first range we are gathering.
6800 loop
6801 -- Eat first entry in left operand if contiguous or overlapped by
6802 -- gathered first operand of result.
6804 if SLeft <= Left'Last
6805 and then Left (SLeft).Lo <= FEnt.Hi + 1
6806 then
6807 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6808 SLeft := SLeft + 1;
6810 -- Eat first entry in right operand if contiguous or overlapped by
6811 -- gathered right operand of result.
6813 elsif SRight <= Right'Last
6814 and then Right (SRight).Lo <= FEnt.Hi + 1
6815 then
6816 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6817 SRight := SRight + 1;
6819 -- All done if no more entries to eat
6821 else
6822 exit;
6823 end if;
6824 end loop;
6826 -- Obtain result as the first entry we just computed, concatenated
6827 -- to the "or" of the remaining results (if one operand is empty,
6828 -- this will just concatenate with the other
6830 return
6831 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6832 end "or";
6834 -----------------
6835 -- Build_Range --
6836 -----------------
6838 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6839 Result : Node_Id;
6840 begin
6841 Result :=
6842 Make_Range (Loc,
6843 Low_Bound => Build_Val (Lo),
6844 High_Bound => Build_Val (Hi));
6845 Set_Etype (Result, Btyp);
6846 Set_Analyzed (Result);
6847 return Result;
6848 end Build_Range;
6850 ---------------
6851 -- Build_Val --
6852 ---------------
6854 function Build_Val (V : Uint) return Node_Id is
6855 Result : Node_Id;
6857 begin
6858 if Is_Enumeration_Type (Typ) then
6859 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6860 else
6861 Result := Make_Integer_Literal (Loc, V);
6862 end if;
6864 Set_Etype (Result, Btyp);
6865 Set_Is_Static_Expression (Result);
6866 Set_Analyzed (Result);
6867 return Result;
6868 end Build_Val;
6870 ---------------
6871 -- Get_RList --
6872 ---------------
6874 function Get_RList (Exp : Node_Id) return RList is
6875 Op : Node_Kind;
6876 Val : Uint;
6878 begin
6879 -- Static expression can only be true or false
6881 if Is_OK_Static_Expression (Exp) then
6882 if Expr_Value (Exp) = 0 then
6883 return False_Range;
6884 else
6885 return True_Range;
6886 end if;
6887 end if;
6889 -- Otherwise test node type
6891 Op := Nkind (Exp);
6893 case Op is
6895 -- And
6897 when N_Op_And | N_And_Then =>
6898 return Get_RList (Left_Opnd (Exp))
6900 Get_RList (Right_Opnd (Exp));
6902 -- Or
6904 when N_Op_Or | N_Or_Else =>
6905 return Get_RList (Left_Opnd (Exp))
6907 Get_RList (Right_Opnd (Exp));
6909 -- Not
6911 when N_Op_Not =>
6912 return not Get_RList (Right_Opnd (Exp));
6914 -- Comparisons of type with static value
6916 when N_Op_Compare =>
6918 -- Type is left operand
6920 if Is_Type_Ref (Left_Opnd (Exp))
6921 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6922 then
6923 Val := Expr_Value (Right_Opnd (Exp));
6925 -- Typ is right operand
6927 elsif Is_Type_Ref (Right_Opnd (Exp))
6928 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6929 then
6930 Val := Expr_Value (Left_Opnd (Exp));
6932 -- Invert sense of comparison
6934 case Op is
6935 when N_Op_Gt => Op := N_Op_Lt;
6936 when N_Op_Lt => Op := N_Op_Gt;
6937 when N_Op_Ge => Op := N_Op_Le;
6938 when N_Op_Le => Op := N_Op_Ge;
6939 when others => null;
6940 end case;
6942 -- Other cases are non-static
6944 else
6945 raise Non_Static;
6946 end if;
6948 -- Construct range according to comparison operation
6950 case Op is
6951 when N_Op_Eq =>
6952 return RList'(1 => REnt'(Val, Val));
6954 when N_Op_Ge =>
6955 return RList'(1 => REnt'(Val, BHi));
6957 when N_Op_Gt =>
6958 return RList'(1 => REnt'(Val + 1, BHi));
6960 when N_Op_Le =>
6961 return RList'(1 => REnt'(BLo, Val));
6963 when N_Op_Lt =>
6964 return RList'(1 => REnt'(BLo, Val - 1));
6966 when N_Op_Ne =>
6967 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
6969 when others =>
6970 raise Program_Error;
6971 end case;
6973 -- Membership (IN)
6975 when N_In =>
6976 if not Is_Type_Ref (Left_Opnd (Exp)) then
6977 raise Non_Static;
6978 end if;
6980 if Present (Right_Opnd (Exp)) then
6981 return Membership_Entry (Right_Opnd (Exp));
6982 else
6983 return Membership_Entries (First (Alternatives (Exp)));
6984 end if;
6986 -- Negative membership (NOT IN)
6988 when N_Not_In =>
6989 if not Is_Type_Ref (Left_Opnd (Exp)) then
6990 raise Non_Static;
6991 end if;
6993 if Present (Right_Opnd (Exp)) then
6994 return not Membership_Entry (Right_Opnd (Exp));
6995 else
6996 return not Membership_Entries (First (Alternatives (Exp)));
6997 end if;
6999 -- Function call, may be call to static predicate
7001 when N_Function_Call =>
7002 if Is_Entity_Name (Name (Exp)) then
7003 declare
7004 Ent : constant Entity_Id := Entity (Name (Exp));
7005 begin
7006 if Is_Predicate_Function (Ent)
7007 or else
7008 Is_Predicate_Function_M (Ent)
7009 then
7010 return Stat_Pred (Etype (First_Formal (Ent)));
7011 end if;
7012 end;
7013 end if;
7015 -- Other function call cases are non-static
7017 raise Non_Static;
7019 -- Qualified expression, dig out the expression
7021 when N_Qualified_Expression =>
7022 return Get_RList (Expression (Exp));
7024 when N_Case_Expression =>
7025 declare
7026 Alt : Node_Id;
7027 Choices : List_Id;
7028 Dep : Node_Id;
7030 begin
7031 if not Is_Entity_Name (Expression (Expr))
7032 or else Etype (Expression (Expr)) /= Typ
7033 then
7034 Error_Msg_N
7035 ("expression must denaote subtype", Expression (Expr));
7036 return False_Range;
7037 end if;
7039 -- Collect discrete choices in all True alternatives
7041 Choices := New_List;
7042 Alt := First (Alternatives (Exp));
7043 while Present (Alt) loop
7044 Dep := Expression (Alt);
7046 if not Is_OK_Static_Expression (Dep) then
7047 raise Non_Static;
7049 elsif Is_True (Expr_Value (Dep)) then
7050 Append_List_To (Choices,
7051 New_Copy_List (Discrete_Choices (Alt)));
7052 end if;
7054 Next (Alt);
7055 end loop;
7057 return Membership_Entries (First (Choices));
7058 end;
7060 -- Expression with actions: if no actions, dig out expression
7062 when N_Expression_With_Actions =>
7063 if Is_Empty_List (Actions (Exp)) then
7064 return Get_RList (Expression (Exp));
7065 else
7066 raise Non_Static;
7067 end if;
7069 -- Xor operator
7071 when N_Op_Xor =>
7072 return (Get_RList (Left_Opnd (Exp))
7073 and not Get_RList (Right_Opnd (Exp)))
7074 or (Get_RList (Right_Opnd (Exp))
7075 and not Get_RList (Left_Opnd (Exp)));
7077 -- Any other node type is non-static
7079 when others =>
7080 raise Non_Static;
7081 end case;
7082 end Get_RList;
7084 ------------
7085 -- Hi_Val --
7086 ------------
7088 function Hi_Val (N : Node_Id) return Uint is
7089 begin
7090 if Is_OK_Static_Expression (N) then
7091 return Expr_Value (N);
7092 else
7093 pragma Assert (Nkind (N) = N_Range);
7094 return Expr_Value (High_Bound (N));
7095 end if;
7096 end Hi_Val;
7098 --------------
7099 -- Is_False --
7100 --------------
7102 function Is_False (R : RList) return Boolean is
7103 begin
7104 return R'Length = 0;
7105 end Is_False;
7107 -------------
7108 -- Is_True --
7109 -------------
7111 function Is_True (R : RList) return Boolean is
7112 begin
7113 return R'Length = 1
7114 and then R (R'First).Lo = BLo
7115 and then R (R'First).Hi = BHi;
7116 end Is_True;
7118 -----------------
7119 -- Is_Type_Ref --
7120 -----------------
7122 function Is_Type_Ref (N : Node_Id) return Boolean is
7123 begin
7124 return Nkind (N) = N_Identifier
7125 and then Chars (N) = Nam
7126 and then Paren_Count (N) = 0;
7127 end Is_Type_Ref;
7129 ------------
7130 -- Lo_Val --
7131 ------------
7133 function Lo_Val (N : Node_Id) return Uint is
7134 begin
7135 if Is_OK_Static_Expression (N) then
7136 return Expr_Value (N);
7137 else
7138 pragma Assert (Nkind (N) = N_Range);
7139 return Expr_Value (Low_Bound (N));
7140 end if;
7141 end Lo_Val;
7143 ------------------------
7144 -- Membership_Entries --
7145 ------------------------
7147 function Membership_Entries (N : Node_Id) return RList is
7148 begin
7149 if No (Next (N)) then
7150 return Membership_Entry (N);
7151 else
7152 return Membership_Entry (N) or Membership_Entries (Next (N));
7153 end if;
7154 end Membership_Entries;
7156 ----------------------
7157 -- Membership_Entry --
7158 ----------------------
7160 function Membership_Entry (N : Node_Id) return RList is
7161 Val : Uint;
7162 SLo : Uint;
7163 SHi : Uint;
7165 begin
7166 -- Range case
7168 if Nkind (N) = N_Range then
7169 if not Is_OK_Static_Expression (Low_Bound (N))
7170 or else
7171 not Is_OK_Static_Expression (High_Bound (N))
7172 then
7173 raise Non_Static;
7174 else
7175 SLo := Expr_Value (Low_Bound (N));
7176 SHi := Expr_Value (High_Bound (N));
7177 return RList'(1 => REnt'(SLo, SHi));
7178 end if;
7180 -- Static expression case
7182 elsif Is_OK_Static_Expression (N) then
7183 Val := Expr_Value (N);
7184 return RList'(1 => REnt'(Val, Val));
7186 -- Identifier (other than static expression) case
7188 else pragma Assert (Nkind (N) = N_Identifier);
7190 -- Type case
7192 if Is_Type (Entity (N)) then
7194 -- If type has predicates, process them
7196 if Has_Predicates (Entity (N)) then
7197 return Stat_Pred (Entity (N));
7199 -- For static subtype without predicates, get range
7201 elsif Is_OK_Static_Subtype (Entity (N)) then
7202 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7203 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7204 return RList'(1 => REnt'(SLo, SHi));
7206 -- Any other type makes us non-static
7208 else
7209 raise Non_Static;
7210 end if;
7212 -- Any other kind of identifier in predicate (e.g. a non-static
7213 -- expression value) means this is not a static predicate.
7215 else
7216 raise Non_Static;
7217 end if;
7218 end if;
7219 end Membership_Entry;
7221 ---------------
7222 -- Stat_Pred --
7223 ---------------
7225 function Stat_Pred (Typ : Entity_Id) return RList is
7226 begin
7227 -- Not static if type does not have static predicates
7229 if not Has_Static_Predicate (Typ) then
7230 raise Non_Static;
7231 end if;
7233 -- Otherwise we convert the predicate list to a range list
7235 declare
7236 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7237 Result : RList (1 .. List_Length (Spred));
7238 P : Node_Id;
7240 begin
7241 P := First (Static_Discrete_Predicate (Typ));
7242 for J in Result'Range loop
7243 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7244 Next (P);
7245 end loop;
7247 return Result;
7248 end;
7249 end Stat_Pred;
7251 -- Start of processing for Build_Discrete_Static_Predicate
7253 begin
7254 -- Analyze the expression to see if it is a static predicate
7256 declare
7257 Ranges : constant RList := Get_RList (Expr);
7258 -- Range list from expression if it is static
7260 Plist : List_Id;
7262 begin
7263 -- Convert range list into a form for the static predicate. In the
7264 -- Ranges array, we just have raw ranges, these must be converted
7265 -- to properly typed and analyzed static expressions or range nodes.
7267 -- Note: here we limit ranges to the ranges of the subtype, so that
7268 -- a predicate is always false for values outside the subtype. That
7269 -- seems fine, such values are invalid anyway, and considering them
7270 -- to fail the predicate seems allowed and friendly, and furthermore
7271 -- simplifies processing for case statements and loops.
7273 Plist := New_List;
7275 for J in Ranges'Range loop
7276 declare
7277 Lo : Uint := Ranges (J).Lo;
7278 Hi : Uint := Ranges (J).Hi;
7280 begin
7281 -- Ignore completely out of range entry
7283 if Hi < TLo or else Lo > THi then
7284 null;
7286 -- Otherwise process entry
7288 else
7289 -- Adjust out of range value to subtype range
7291 if Lo < TLo then
7292 Lo := TLo;
7293 end if;
7295 if Hi > THi then
7296 Hi := THi;
7297 end if;
7299 -- Convert range into required form
7301 Append_To (Plist, Build_Range (Lo, Hi));
7302 end if;
7303 end;
7304 end loop;
7306 -- Processing was successful and all entries were static, so now we
7307 -- can store the result as the predicate list.
7309 Set_Static_Discrete_Predicate (Typ, Plist);
7311 -- The processing for static predicates put the expression into
7312 -- canonical form as a series of ranges. It also eliminated
7313 -- duplicates and collapsed and combined ranges. We might as well
7314 -- replace the alternatives list of the right operand of the
7315 -- membership test with the static predicate list, which will
7316 -- usually be more efficient.
7318 declare
7319 New_Alts : constant List_Id := New_List;
7320 Old_Node : Node_Id;
7321 New_Node : Node_Id;
7323 begin
7324 Old_Node := First (Plist);
7325 while Present (Old_Node) loop
7326 New_Node := New_Copy (Old_Node);
7328 if Nkind (New_Node) = N_Range then
7329 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7330 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7331 end if;
7333 Append_To (New_Alts, New_Node);
7334 Next (Old_Node);
7335 end loop;
7337 -- If empty list, replace by False
7339 if Is_Empty_List (New_Alts) then
7340 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7342 -- Else replace by set membership test
7344 else
7345 Rewrite (Expr,
7346 Make_In (Loc,
7347 Left_Opnd => Make_Identifier (Loc, Nam),
7348 Right_Opnd => Empty,
7349 Alternatives => New_Alts));
7351 -- Resolve new expression in function context
7353 Install_Formals (Predicate_Function (Typ));
7354 Push_Scope (Predicate_Function (Typ));
7355 Analyze_And_Resolve (Expr, Standard_Boolean);
7356 Pop_Scope;
7357 end if;
7358 end;
7359 end;
7361 -- If non-static, return doing nothing
7363 exception
7364 when Non_Static =>
7365 return;
7366 end Build_Discrete_Static_Predicate;
7368 -------------------------------------------
7369 -- Build_Invariant_Procedure_Declaration --
7370 -------------------------------------------
7372 function Build_Invariant_Procedure_Declaration
7373 (Typ : Entity_Id) return Node_Id
7375 Loc : constant Source_Ptr := Sloc (Typ);
7376 Object_Entity : constant Entity_Id :=
7377 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7378 Spec : Node_Id;
7379 SId : Entity_Id;
7381 begin
7382 Set_Etype (Object_Entity, Typ);
7384 -- Check for duplicate definiations.
7386 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7387 return Empty;
7388 end if;
7390 SId :=
7391 Make_Defining_Identifier (Loc,
7392 Chars => New_External_Name (Chars (Typ), "Invariant"));
7393 Set_Has_Invariants (Typ);
7394 Set_Ekind (SId, E_Procedure);
7395 Set_Etype (SId, Standard_Void_Type);
7396 Set_Is_Invariant_Procedure (SId);
7397 Set_Invariant_Procedure (Typ, SId);
7399 Spec :=
7400 Make_Procedure_Specification (Loc,
7401 Defining_Unit_Name => SId,
7402 Parameter_Specifications => New_List (
7403 Make_Parameter_Specification (Loc,
7404 Defining_Identifier => Object_Entity,
7405 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7407 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7408 end Build_Invariant_Procedure_Declaration;
7410 -------------------------------
7411 -- Build_Invariant_Procedure --
7412 -------------------------------
7414 -- The procedure that is constructed here has the form
7416 -- procedure typInvariant (Ixxx : typ) is
7417 -- begin
7418 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7419 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7420 -- ...
7421 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7422 -- ...
7423 -- end typInvariant;
7425 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7426 Loc : constant Source_Ptr := Sloc (Typ);
7427 Stmts : List_Id;
7428 Spec : Node_Id;
7429 SId : Entity_Id;
7430 PDecl : Node_Id;
7431 PBody : Node_Id;
7433 Nam : Name_Id;
7434 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7435 -- if we come from a Type_Invariant aspect, we make sure to build the
7436 -- Check pragma with the right name, so that Check_Policy works right.
7438 Visible_Decls : constant List_Id := Visible_Declarations (N);
7439 Private_Decls : constant List_Id := Private_Declarations (N);
7441 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7442 -- Appends statements to Stmts for any invariants in the rep item chain
7443 -- of the given type. If Inherit is False, then we only process entries
7444 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7445 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7446 -- "inherited" to the exception message and generating an informational
7447 -- message about the inheritance of an invariant.
7449 Object_Name : Name_Id;
7450 -- Name for argument of invariant procedure
7452 Object_Entity : Node_Id;
7453 -- The entity of the formal for the procedure
7455 --------------------
7456 -- Add_Invariants --
7457 --------------------
7459 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7460 Ritem : Node_Id;
7461 Arg1 : Node_Id;
7462 Arg2 : Node_Id;
7463 Arg3 : Node_Id;
7464 Exp : Node_Id;
7465 Loc : Source_Ptr;
7466 Assoc : List_Id;
7467 Str : String_Id;
7469 procedure Replace_Type_Reference (N : Node_Id);
7470 -- Replace a single occurrence N of the subtype name with a reference
7471 -- to the formal of the predicate function. N can be an identifier
7472 -- referencing the subtype, or a selected component, representing an
7473 -- appropriately qualified occurrence of the subtype name.
7475 procedure Replace_Type_References is
7476 new Replace_Type_References_Generic (Replace_Type_Reference);
7477 -- Traverse an expression replacing all occurrences of the subtype
7478 -- name with appropriate references to the object that is the formal
7479 -- parameter of the predicate function. Note that we must ensure
7480 -- that the type and entity information is properly set in the
7481 -- replacement node, since we will do a Preanalyze call of this
7482 -- expression without proper visibility of the procedure argument.
7484 ----------------------------
7485 -- Replace_Type_Reference --
7486 ----------------------------
7488 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7489 -- regarding handling of Sloc and Comes_From_Source.
7491 procedure Replace_Type_Reference (N : Node_Id) is
7492 begin
7494 -- Add semantic information to node to be rewritten, for ASIS
7495 -- navigation needs.
7497 if Nkind (N) = N_Identifier then
7498 Set_Entity (N, T);
7499 Set_Etype (N, T);
7501 elsif Nkind (N) = N_Selected_Component then
7502 Analyze (Prefix (N));
7503 Set_Entity (Selector_Name (N), T);
7504 Set_Etype (Selector_Name (N), T);
7505 end if;
7507 -- Invariant'Class, replace with T'Class (obj)
7509 if Class_Present (Ritem) then
7510 Rewrite (N,
7511 Make_Type_Conversion (Sloc (N),
7512 Subtype_Mark =>
7513 Make_Attribute_Reference (Sloc (N),
7514 Prefix => New_Occurrence_Of (T, Sloc (N)),
7515 Attribute_Name => Name_Class),
7516 Expression => Make_Identifier (Sloc (N), Object_Name)));
7518 Set_Entity (Expression (N), Object_Entity);
7519 Set_Etype (Expression (N), Typ);
7521 -- Invariant, replace with obj
7523 else
7524 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7525 Set_Entity (N, Object_Entity);
7526 Set_Etype (N, Typ);
7527 end if;
7529 Set_Comes_From_Source (N, True);
7530 end Replace_Type_Reference;
7532 -- Start of processing for Add_Invariants
7534 begin
7535 Ritem := First_Rep_Item (T);
7536 while Present (Ritem) loop
7537 if Nkind (Ritem) = N_Pragma
7538 and then Pragma_Name (Ritem) = Name_Invariant
7539 then
7540 Arg1 := First (Pragma_Argument_Associations (Ritem));
7541 Arg2 := Next (Arg1);
7542 Arg3 := Next (Arg2);
7544 Arg1 := Get_Pragma_Arg (Arg1);
7545 Arg2 := Get_Pragma_Arg (Arg2);
7547 -- For Inherit case, ignore Invariant, process only Class case
7549 if Inherit then
7550 if not Class_Present (Ritem) then
7551 goto Continue;
7552 end if;
7554 -- For Inherit false, process only item for right type
7556 else
7557 if Entity (Arg1) /= Typ then
7558 goto Continue;
7559 end if;
7560 end if;
7562 if No (Stmts) then
7563 Stmts := Empty_List;
7564 end if;
7566 Exp := New_Copy_Tree (Arg2);
7568 -- Preserve sloc of original pragma Invariant
7570 Loc := Sloc (Ritem);
7572 -- We need to replace any occurrences of the name of the type
7573 -- with references to the object, converted to type'Class in
7574 -- the case of Invariant'Class aspects.
7576 Replace_Type_References (Exp, T);
7578 -- If this invariant comes from an aspect, find the aspect
7579 -- specification, and replace the saved expression because
7580 -- we need the subtype references replaced for the calls to
7581 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7582 -- and Check_Aspect_At_End_Of_Declarations.
7584 if From_Aspect_Specification (Ritem) then
7585 declare
7586 Aitem : Node_Id;
7588 begin
7589 -- Loop to find corresponding aspect, note that this
7590 -- must be present given the pragma is marked delayed.
7592 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7593 -- this loop does nothing. Furthermore, why isn't this
7594 -- simply Corresponding_Aspect ???
7596 Aitem := Next_Rep_Item (Ritem);
7597 while Present (Aitem) loop
7598 if Nkind (Aitem) = N_Aspect_Specification
7599 and then Aspect_Rep_Item (Aitem) = Ritem
7600 then
7601 Set_Entity
7602 (Identifier (Aitem), New_Copy_Tree (Exp));
7603 exit;
7604 end if;
7606 Aitem := Next_Rep_Item (Aitem);
7607 end loop;
7608 end;
7609 end if;
7611 -- Now we need to preanalyze the expression to properly capture
7612 -- the visibility in the visible part. The expression will not
7613 -- be analyzed for real until the body is analyzed, but that is
7614 -- at the end of the private part and has the wrong visibility.
7616 Set_Parent (Exp, N);
7617 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
7619 -- A class-wide invariant may be inherited in a separate unit,
7620 -- where the corresponding expression cannot be resolved by
7621 -- visibility, because it refers to a local function. Propagate
7622 -- semantic information to the original representation item, to
7623 -- be used when an invariant procedure for a derived type is
7624 -- constructed.
7626 -- Unclear how to handle class-wide invariants that are not
7627 -- function calls ???
7629 if not Inherit
7630 and then Class_Present (Ritem)
7631 and then Nkind (Exp) = N_Function_Call
7632 and then Nkind (Arg2) = N_Indexed_Component
7633 then
7634 Rewrite (Arg2,
7635 Make_Function_Call (Loc,
7636 Name =>
7637 New_Occurrence_Of (Entity (Name (Exp)), Loc),
7638 Parameter_Associations =>
7639 New_Copy_List (Expressions (Arg2))));
7640 end if;
7642 -- In ASIS mode, even if assertions are not enabled, we must
7643 -- analyze the original expression in the aspect specification
7644 -- because it is part of the original tree.
7646 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7647 declare
7648 Inv : constant Node_Id :=
7649 Expression (Corresponding_Aspect (Ritem));
7650 begin
7651 Replace_Type_References (Inv, T);
7652 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7653 end;
7654 end if;
7656 -- Get name to be used for Check pragma
7658 if not From_Aspect_Specification (Ritem) then
7659 Nam := Name_Invariant;
7660 else
7661 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7662 end if;
7664 -- Build first two arguments for Check pragma
7666 Assoc :=
7667 New_List (
7668 Make_Pragma_Argument_Association (Loc,
7669 Expression => Make_Identifier (Loc, Chars => Nam)),
7670 Make_Pragma_Argument_Association (Loc,
7671 Expression => Exp));
7673 -- Add message if present in Invariant pragma
7675 if Present (Arg3) then
7676 Str := Strval (Get_Pragma_Arg (Arg3));
7678 -- If inherited case, and message starts "failed invariant",
7679 -- change it to be "failed inherited invariant".
7681 if Inherit then
7682 String_To_Name_Buffer (Str);
7684 if Name_Buffer (1 .. 16) = "failed invariant" then
7685 Insert_Str_In_Name_Buffer ("inherited ", 8);
7686 Str := String_From_Name_Buffer;
7687 end if;
7688 end if;
7690 Append_To (Assoc,
7691 Make_Pragma_Argument_Association (Loc,
7692 Expression => Make_String_Literal (Loc, Str)));
7693 end if;
7695 -- Add Check pragma to list of statements
7697 Append_To (Stmts,
7698 Make_Pragma (Loc,
7699 Pragma_Identifier =>
7700 Make_Identifier (Loc, Name_Check),
7701 Pragma_Argument_Associations => Assoc));
7703 -- If Inherited case and option enabled, output info msg. Note
7704 -- that we know this is a case of Invariant'Class.
7706 if Inherit and Opt.List_Inherited_Aspects then
7707 Error_Msg_Sloc := Sloc (Ritem);
7708 Error_Msg_N
7709 ("info: & inherits `Invariant''Class` aspect from #?L?",
7710 Typ);
7711 end if;
7712 end if;
7714 <<Continue>>
7715 Next_Rep_Item (Ritem);
7716 end loop;
7717 end Add_Invariants;
7719 -- Start of processing for Build_Invariant_Procedure
7721 begin
7722 Stmts := No_List;
7723 PDecl := Empty;
7724 PBody := Empty;
7725 SId := Empty;
7727 -- If the aspect specification exists for some view of the type, the
7728 -- declaration for the procedure has been created.
7730 if Has_Invariants (Typ) then
7731 SId := Invariant_Procedure (Typ);
7732 end if;
7734 -- If the body is already present, nothing to do. This will occur when
7735 -- the type is already frozen, which is the case when the invariant
7736 -- appears in a private part, and the freezing takes place before the
7737 -- final pass over full declarations.
7739 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
7741 if Present (SId) then
7742 PDecl := Unit_Declaration_Node (SId);
7744 if Present (PDecl)
7745 and then Nkind (PDecl) = N_Subprogram_Declaration
7746 and then Present (Corresponding_Body (PDecl))
7747 then
7748 return;
7749 end if;
7751 else
7752 PDecl := Build_Invariant_Procedure_Declaration (Typ);
7753 end if;
7755 -- Recover formal of procedure, for use in the calls to invariant
7756 -- functions (including inherited ones).
7758 Object_Entity :=
7759 Defining_Identifier
7760 (First (Parameter_Specifications (Specification (PDecl))));
7761 Object_Name := Chars (Object_Entity);
7763 -- Add invariants for the current type
7765 Add_Invariants (Typ, Inherit => False);
7767 -- Add invariants for parent types
7769 declare
7770 Current_Typ : Entity_Id;
7771 Parent_Typ : Entity_Id;
7773 begin
7774 Current_Typ := Typ;
7775 loop
7776 Parent_Typ := Etype (Current_Typ);
7778 if Is_Private_Type (Parent_Typ)
7779 and then Present (Full_View (Base_Type (Parent_Typ)))
7780 then
7781 Parent_Typ := Full_View (Base_Type (Parent_Typ));
7782 end if;
7784 exit when Parent_Typ = Current_Typ;
7786 Current_Typ := Parent_Typ;
7787 Add_Invariants (Current_Typ, Inherit => True);
7788 end loop;
7789 end;
7791 -- Build the procedure if we generated at least one Check pragma
7793 if Stmts /= No_List then
7794 Spec := Copy_Separate_Tree (Specification (PDecl));
7796 PBody :=
7797 Make_Subprogram_Body (Loc,
7798 Specification => Spec,
7799 Declarations => Empty_List,
7800 Handled_Statement_Sequence =>
7801 Make_Handled_Sequence_Of_Statements (Loc,
7802 Statements => Stmts));
7804 -- Insert procedure declaration and spec at the appropriate points.
7805 -- If declaration is already analyzed, it was processed by the
7806 -- generated pragma.
7808 if Present (Private_Decls) then
7810 -- The spec goes at the end of visible declarations, but they have
7811 -- already been analyzed, so we need to explicitly do the analyze.
7813 if not Analyzed (PDecl) then
7814 Append_To (Visible_Decls, PDecl);
7815 Analyze (PDecl);
7816 end if;
7818 -- The body goes at the end of the private declarations, which we
7819 -- have not analyzed yet, so we do not need to perform an explicit
7820 -- analyze call. We skip this if there are no private declarations
7821 -- (this is an error that will be caught elsewhere);
7823 Append_To (Private_Decls, PBody);
7825 -- If the invariant appears on the full view of a type, the
7826 -- analysis of the private part is complete, and we must
7827 -- analyze the new body explicitly.
7829 if In_Private_Part (Current_Scope) then
7830 Analyze (PBody);
7831 end if;
7833 -- If there are no private declarations this may be an error that
7834 -- will be diagnosed elsewhere. However, if this is a non-private
7835 -- type that inherits invariants, it needs no completion and there
7836 -- may be no private part. In this case insert invariant procedure
7837 -- at end of current declarative list, and analyze at once, given
7838 -- that the type is about to be frozen.
7840 elsif not Is_Private_Type (Typ) then
7841 Append_To (Visible_Decls, PDecl);
7842 Append_To (Visible_Decls, PBody);
7843 Analyze (PDecl);
7844 Analyze (PBody);
7845 end if;
7846 end if;
7847 end Build_Invariant_Procedure;
7849 -------------------------------
7850 -- Build_Predicate_Functions --
7851 -------------------------------
7853 -- The procedures that are constructed here have the form:
7855 -- function typPredicate (Ixxx : typ) return Boolean is
7856 -- begin
7857 -- return
7858 -- exp1 and then exp2 and then ...
7859 -- and then typ1Predicate (typ1 (Ixxx))
7860 -- and then typ2Predicate (typ2 (Ixxx))
7861 -- and then ...;
7862 -- end typPredicate;
7864 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
7865 -- this is the point at which these expressions get analyzed, providing the
7866 -- required delay, and typ1, typ2, are entities from which predicates are
7867 -- inherited. Note that we do NOT generate Check pragmas, that's because we
7868 -- use this function even if checks are off, e.g. for membership tests.
7870 -- If the expression has at least one Raise_Expression, then we also build
7871 -- the typPredicateM version of the function, in which any occurrence of a
7872 -- Raise_Expression is converted to "return False".
7874 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
7875 Loc : constant Source_Ptr := Sloc (Typ);
7877 Expr : Node_Id;
7878 -- This is the expression for the result of the function. It is
7879 -- is build by connecting the component predicates with AND THEN.
7881 Expr_M : Node_Id;
7882 -- This is the corresponding return expression for the Predicate_M
7883 -- function. It differs in that raise expressions are marked for
7884 -- special expansion (see Process_REs).
7886 Object_Name : constant Name_Id := New_Internal_Name ('I');
7887 -- Name for argument of Predicate procedure. Note that we use the same
7888 -- name for both predicate functions. That way the reference within the
7889 -- predicate expression is the same in both functions.
7891 Object_Entity : constant Entity_Id :=
7892 Make_Defining_Identifier (Loc, Chars => Object_Name);
7893 -- Entity for argument of Predicate procedure
7895 Object_Entity_M : constant Entity_Id :=
7896 Make_Defining_Identifier (Loc, Chars => Object_Name);
7897 -- Entity for argument of Predicate_M procedure
7899 Raise_Expression_Present : Boolean := False;
7900 -- Set True if Expr has at least one Raise_Expression
7902 procedure Add_Call (T : Entity_Id);
7903 -- Includes a call to the predicate function for type T in Expr if T
7904 -- has predicates and Predicate_Function (T) is non-empty.
7906 procedure Add_Predicates;
7907 -- Appends expressions for any Predicate pragmas in the rep item chain
7908 -- Typ to Expr. Note that we look only at items for this exact entity.
7909 -- Inheritance of predicates for the parent type is done by calling the
7910 -- Predicate_Function of the parent type, using Add_Call above.
7912 function Test_RE (N : Node_Id) return Traverse_Result;
7913 -- Used in Test_REs, tests one node for being a raise expression, and if
7914 -- so sets Raise_Expression_Present True.
7916 procedure Test_REs is new Traverse_Proc (Test_RE);
7917 -- Tests to see if Expr contains any raise expressions
7919 function Process_RE (N : Node_Id) return Traverse_Result;
7920 -- Used in Process REs, tests if node N is a raise expression, and if
7921 -- so, marks it to be converted to return False.
7923 procedure Process_REs is new Traverse_Proc (Process_RE);
7924 -- Marks any raise expressions in Expr_M to return False
7926 --------------
7927 -- Add_Call --
7928 --------------
7930 procedure Add_Call (T : Entity_Id) is
7931 Exp : Node_Id;
7933 begin
7934 if Present (T) and then Present (Predicate_Function (T)) then
7935 Set_Has_Predicates (Typ);
7937 -- Build the call to the predicate function of T
7939 Exp :=
7940 Make_Predicate_Call
7941 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
7943 -- Add call to evolving expression, using AND THEN if needed
7945 if No (Expr) then
7946 Expr := Exp;
7948 else
7949 Expr :=
7950 Make_And_Then (Sloc (Expr),
7951 Left_Opnd => Relocate_Node (Expr),
7952 Right_Opnd => Exp);
7953 end if;
7955 -- Output info message on inheritance if required. Note we do not
7956 -- give this information for generic actual types, since it is
7957 -- unwelcome noise in that case in instantiations. We also
7958 -- generally suppress the message in instantiations, and also
7959 -- if it involves internal names.
7961 if Opt.List_Inherited_Aspects
7962 and then not Is_Generic_Actual_Type (Typ)
7963 and then Instantiation_Depth (Sloc (Typ)) = 0
7964 and then not Is_Internal_Name (Chars (T))
7965 and then not Is_Internal_Name (Chars (Typ))
7966 then
7967 Error_Msg_Sloc := Sloc (Predicate_Function (T));
7968 Error_Msg_Node_2 := T;
7969 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
7970 end if;
7971 end if;
7972 end Add_Call;
7974 --------------------
7975 -- Add_Predicates --
7976 --------------------
7978 procedure Add_Predicates is
7979 Ritem : Node_Id;
7980 Arg1 : Node_Id;
7981 Arg2 : Node_Id;
7983 procedure Replace_Type_Reference (N : Node_Id);
7984 -- Replace a single occurrence N of the subtype name with a reference
7985 -- to the formal of the predicate function. N can be an identifier
7986 -- referencing the subtype, or a selected component, representing an
7987 -- appropriately qualified occurrence of the subtype name.
7989 procedure Replace_Type_References is
7990 new Replace_Type_References_Generic (Replace_Type_Reference);
7991 -- Traverse an expression changing every occurrence of an identifier
7992 -- whose name matches the name of the subtype with a reference to
7993 -- the formal parameter of the predicate function.
7995 ----------------------------
7996 -- Replace_Type_Reference --
7997 ----------------------------
7999 procedure Replace_Type_Reference (N : Node_Id) is
8000 begin
8001 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8002 -- Use the Sloc of the usage name, not the defining name
8004 Set_Etype (N, Typ);
8005 Set_Entity (N, Object_Entity);
8007 -- We want to treat the node as if it comes from source, so that
8008 -- ASIS will not ignore it
8010 Set_Comes_From_Source (N, True);
8011 end Replace_Type_Reference;
8013 -- Start of processing for Add_Predicates
8015 begin
8016 Ritem := First_Rep_Item (Typ);
8017 while Present (Ritem) loop
8018 if Nkind (Ritem) = N_Pragma
8019 and then Pragma_Name (Ritem) = Name_Predicate
8020 then
8021 -- Acquire arguments
8023 Arg1 := First (Pragma_Argument_Associations (Ritem));
8024 Arg2 := Next (Arg1);
8026 Arg1 := Get_Pragma_Arg (Arg1);
8027 Arg2 := Get_Pragma_Arg (Arg2);
8029 -- See if this predicate pragma is for the current type or for
8030 -- its full view. A predicate on a private completion is placed
8031 -- on the partial view beause this is the visible entity that
8032 -- is frozen.
8034 if Entity (Arg1) = Typ
8035 or else Full_View (Entity (Arg1)) = Typ
8036 then
8037 -- We have a match, this entry is for our subtype
8039 -- We need to replace any occurrences of the name of the
8040 -- type with references to the object.
8042 Replace_Type_References (Arg2, Typ);
8044 -- If this predicate comes from an aspect, find the aspect
8045 -- specification, and replace the saved expression because
8046 -- we need the subtype references replaced for the calls to
8047 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8048 -- and Check_Aspect_At_End_Of_Declarations.
8050 if From_Aspect_Specification (Ritem) then
8051 declare
8052 Aitem : Node_Id;
8054 begin
8055 -- Loop to find corresponding aspect, note that this
8056 -- must be present given the pragma is marked delayed.
8058 Aitem := Next_Rep_Item (Ritem);
8059 loop
8060 if Nkind (Aitem) = N_Aspect_Specification
8061 and then Aspect_Rep_Item (Aitem) = Ritem
8062 then
8063 Set_Entity
8064 (Identifier (Aitem), New_Copy_Tree (Arg2));
8065 exit;
8066 end if;
8068 Aitem := Next_Rep_Item (Aitem);
8069 end loop;
8070 end;
8071 end if;
8073 -- Now we can add the expression
8075 if No (Expr) then
8076 Expr := Relocate_Node (Arg2);
8078 -- There already was a predicate, so add to it
8080 else
8081 Expr :=
8082 Make_And_Then (Loc,
8083 Left_Opnd => Relocate_Node (Expr),
8084 Right_Opnd => Relocate_Node (Arg2));
8085 end if;
8086 end if;
8087 end if;
8089 Next_Rep_Item (Ritem);
8090 end loop;
8091 end Add_Predicates;
8093 ----------------
8094 -- Process_RE --
8095 ----------------
8097 function Process_RE (N : Node_Id) return Traverse_Result is
8098 begin
8099 if Nkind (N) = N_Raise_Expression then
8100 Set_Convert_To_Return_False (N);
8101 return Skip;
8102 else
8103 return OK;
8104 end if;
8105 end Process_RE;
8107 -------------
8108 -- Test_RE --
8109 -------------
8111 function Test_RE (N : Node_Id) return Traverse_Result is
8112 begin
8113 if Nkind (N) = N_Raise_Expression then
8114 Raise_Expression_Present := True;
8115 return Abandon;
8116 else
8117 return OK;
8118 end if;
8119 end Test_RE;
8121 -- Start of processing for Build_Predicate_Functions
8123 begin
8124 -- Return if already built or if type does not have predicates
8126 if not Has_Predicates (Typ)
8127 or else Present (Predicate_Function (Typ))
8128 then
8129 return;
8130 end if;
8132 -- Prepare to construct predicate expression
8134 Expr := Empty;
8136 -- Add Predicates for the current type
8138 Add_Predicates;
8140 -- Add predicates for ancestor if present
8142 declare
8143 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8144 begin
8145 if Present (Atyp) then
8146 Add_Call (Atyp);
8147 end if;
8148 end;
8150 -- Case where predicates are present
8152 if Present (Expr) then
8154 -- Test for raise expression present
8156 Test_REs (Expr);
8158 -- If raise expression is present, capture a copy of Expr for use
8159 -- in building the predicateM function version later on. For this
8160 -- copy we replace references to Object_Entity by Object_Entity_M.
8162 if Raise_Expression_Present then
8163 declare
8164 Map : constant Elist_Id := New_Elmt_List;
8165 begin
8166 Append_Elmt (Object_Entity, Map);
8167 Append_Elmt (Object_Entity_M, Map);
8168 Expr_M := New_Copy_Tree (Expr, Map => Map);
8169 end;
8170 end if;
8172 -- Build the main predicate function
8174 declare
8175 SId : constant Entity_Id :=
8176 Make_Defining_Identifier (Loc,
8177 Chars => New_External_Name (Chars (Typ), "Predicate"));
8178 -- The entity for the the function spec
8180 SIdB : constant Entity_Id :=
8181 Make_Defining_Identifier (Loc,
8182 Chars => New_External_Name (Chars (Typ), "Predicate"));
8183 -- The entity for the function body
8185 Spec : Node_Id;
8186 FDecl : Node_Id;
8187 FBody : Node_Id;
8189 begin
8190 -- Build function declaration
8192 Set_Ekind (SId, E_Function);
8193 Set_Is_Internal (SId);
8194 Set_Is_Predicate_Function (SId);
8195 Set_Predicate_Function (Typ, SId);
8197 -- The predicate function is shared between views of a type
8199 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8200 Set_Predicate_Function (Full_View (Typ), SId);
8201 end if;
8203 Spec :=
8204 Make_Function_Specification (Loc,
8205 Defining_Unit_Name => SId,
8206 Parameter_Specifications => New_List (
8207 Make_Parameter_Specification (Loc,
8208 Defining_Identifier => Object_Entity,
8209 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8210 Result_Definition =>
8211 New_Occurrence_Of (Standard_Boolean, Loc));
8213 FDecl :=
8214 Make_Subprogram_Declaration (Loc,
8215 Specification => Spec);
8217 -- Build function body
8219 Spec :=
8220 Make_Function_Specification (Loc,
8221 Defining_Unit_Name => SIdB,
8222 Parameter_Specifications => New_List (
8223 Make_Parameter_Specification (Loc,
8224 Defining_Identifier =>
8225 Make_Defining_Identifier (Loc, Object_Name),
8226 Parameter_Type =>
8227 New_Occurrence_Of (Typ, Loc))),
8228 Result_Definition =>
8229 New_Occurrence_Of (Standard_Boolean, Loc));
8231 FBody :=
8232 Make_Subprogram_Body (Loc,
8233 Specification => Spec,
8234 Declarations => Empty_List,
8235 Handled_Statement_Sequence =>
8236 Make_Handled_Sequence_Of_Statements (Loc,
8237 Statements => New_List (
8238 Make_Simple_Return_Statement (Loc,
8239 Expression => Expr))));
8241 -- Insert declaration before freeze node and body after
8243 Insert_Before_And_Analyze (N, FDecl);
8244 Insert_After_And_Analyze (N, FBody);
8245 end;
8247 -- Test for raise expressions present and if so build M version
8249 if Raise_Expression_Present then
8250 declare
8251 SId : constant Entity_Id :=
8252 Make_Defining_Identifier (Loc,
8253 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8254 -- The entity for the the function spec
8256 SIdB : constant Entity_Id :=
8257 Make_Defining_Identifier (Loc,
8258 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8259 -- The entity for the function body
8261 Spec : Node_Id;
8262 FDecl : Node_Id;
8263 FBody : Node_Id;
8264 BTemp : Entity_Id;
8266 begin
8267 -- Mark any raise expressions for special expansion
8269 Process_REs (Expr_M);
8271 -- Build function declaration
8273 Set_Ekind (SId, E_Function);
8274 Set_Is_Predicate_Function_M (SId);
8275 Set_Predicate_Function_M (Typ, SId);
8277 -- The predicate function is shared between views of a type
8279 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8280 Set_Predicate_Function_M (Full_View (Typ), SId);
8281 end if;
8283 Spec :=
8284 Make_Function_Specification (Loc,
8285 Defining_Unit_Name => SId,
8286 Parameter_Specifications => New_List (
8287 Make_Parameter_Specification (Loc,
8288 Defining_Identifier => Object_Entity_M,
8289 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8290 Result_Definition =>
8291 New_Occurrence_Of (Standard_Boolean, Loc));
8293 FDecl :=
8294 Make_Subprogram_Declaration (Loc,
8295 Specification => Spec);
8297 -- Build function body
8299 Spec :=
8300 Make_Function_Specification (Loc,
8301 Defining_Unit_Name => SIdB,
8302 Parameter_Specifications => New_List (
8303 Make_Parameter_Specification (Loc,
8304 Defining_Identifier =>
8305 Make_Defining_Identifier (Loc, Object_Name),
8306 Parameter_Type =>
8307 New_Occurrence_Of (Typ, Loc))),
8308 Result_Definition =>
8309 New_Occurrence_Of (Standard_Boolean, Loc));
8311 -- Build the body, we declare the boolean expression before
8312 -- doing the return, because we are not really confident of
8313 -- what happens if a return appears within a return.
8315 BTemp :=
8316 Make_Defining_Identifier (Loc,
8317 Chars => New_Internal_Name ('B'));
8319 FBody :=
8320 Make_Subprogram_Body (Loc,
8321 Specification => Spec,
8323 Declarations => New_List (
8324 Make_Object_Declaration (Loc,
8325 Defining_Identifier => BTemp,
8326 Constant_Present => True,
8327 Object_Definition =>
8328 New_Occurrence_Of (Standard_Boolean, Loc),
8329 Expression => Expr_M)),
8331 Handled_Statement_Sequence =>
8332 Make_Handled_Sequence_Of_Statements (Loc,
8333 Statements => New_List (
8334 Make_Simple_Return_Statement (Loc,
8335 Expression => New_Occurrence_Of (BTemp, Loc)))));
8337 -- Insert declaration before freeze node and body after
8339 Insert_Before_And_Analyze (N, FDecl);
8340 Insert_After_And_Analyze (N, FBody);
8341 end;
8342 end if;
8344 -- See if we have a static predicate. Note that the answer may be
8345 -- yes even if we have an explicit Dynamic_Predicate present.
8347 declare
8348 PS : Boolean;
8349 EN : Node_Id;
8351 begin
8352 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8353 PS := False;
8354 else
8355 PS := Is_Predicate_Static (Expr, Object_Name);
8356 end if;
8358 -- Case where we have a predicate-static aspect
8360 if PS then
8362 -- We don't set Has_Static_Predicate_Aspect, since we can have
8363 -- any of the three cases (Predicate, Dynamic_Predicate, or
8364 -- Static_Predicate) generating a predicate with an expression
8365 -- that is predicate-static. We just indicate that we have a
8366 -- predicate that can be treated as static.
8368 Set_Has_Static_Predicate (Typ);
8370 -- For discrete subtype, build the static predicate list
8372 if Is_Discrete_Type (Typ) then
8373 if not Is_Static_Subtype (Typ) then
8375 -- This can only happen in the presence of previous
8376 -- semantic errors.
8378 pragma Assert (Serious_Errors_Detected > 0);
8379 return;
8380 end if;
8382 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8384 -- If we don't get a static predicate list, it means that we
8385 -- have a case where this is not possible, most typically in
8386 -- the case where we inherit a dynamic predicate. We do not
8387 -- consider this an error, we just leave the predicate as
8388 -- dynamic. But if we do succeed in building the list, then
8389 -- we mark the predicate as static.
8391 if No (Static_Discrete_Predicate (Typ)) then
8392 Set_Has_Static_Predicate (Typ, False);
8393 end if;
8395 -- For real or string subtype, save predicate expression
8397 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8398 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8399 end if;
8401 -- Case of dynamic predicate (expression is not predicate-static)
8403 else
8404 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8405 -- is only set if we have an explicit Dynamic_Predicate aspect
8406 -- given. Here we may simply have a Predicate aspect where the
8407 -- expression happens not to be predicate-static.
8409 -- Emit an error when the predicate is categorized as static
8410 -- but its expression is not predicate-static.
8412 -- First a little fiddling to get a nice location for the
8413 -- message. If the expression is of the form (A and then B),
8414 -- then use the left operand for the Sloc. This avoids getting
8415 -- confused by a call to a higher-level predicate with a less
8416 -- convenient source location.
8418 EN := Expr;
8419 while Nkind (EN) = N_And_Then loop
8420 EN := Left_Opnd (EN);
8421 end loop;
8423 -- Now post appropriate message
8425 if Has_Static_Predicate_Aspect (Typ) then
8426 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8427 Error_Msg_F
8428 ("expression is not predicate-static (RM 3.2.4(16-22))",
8429 EN);
8430 else
8431 Error_Msg_F
8432 ("static predicate requires scalar or string type", EN);
8433 end if;
8434 end if;
8435 end if;
8436 end;
8437 end if;
8438 end Build_Predicate_Functions;
8440 -----------------------------------------
8441 -- Check_Aspect_At_End_Of_Declarations --
8442 -----------------------------------------
8444 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8445 Ent : constant Entity_Id := Entity (ASN);
8446 Ident : constant Node_Id := Identifier (ASN);
8447 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8449 End_Decl_Expr : constant Node_Id := Entity (Ident);
8450 -- Expression to be analyzed at end of declarations
8452 Freeze_Expr : constant Node_Id := Expression (ASN);
8453 -- Expression from call to Check_Aspect_At_Freeze_Point
8455 T : constant Entity_Id := Etype (Freeze_Expr);
8456 -- Type required for preanalyze call
8458 Err : Boolean;
8459 -- Set False if error
8461 -- On entry to this procedure, Entity (Ident) contains a copy of the
8462 -- original expression from the aspect, saved for this purpose, and
8463 -- but Expression (Ident) is a preanalyzed copy of the expression,
8464 -- preanalyzed just after the freeze point.
8466 procedure Check_Overloaded_Name;
8467 -- For aspects whose expression is simply a name, this routine checks if
8468 -- the name is overloaded or not. If so, it verifies there is an
8469 -- interpretation that matches the entity obtained at the freeze point,
8470 -- otherwise the compiler complains.
8472 ---------------------------
8473 -- Check_Overloaded_Name --
8474 ---------------------------
8476 procedure Check_Overloaded_Name is
8477 begin
8478 if not Is_Overloaded (End_Decl_Expr) then
8479 Err := not Is_Entity_Name (End_Decl_Expr)
8480 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8482 else
8483 Err := True;
8485 declare
8486 Index : Interp_Index;
8487 It : Interp;
8489 begin
8490 Get_First_Interp (End_Decl_Expr, Index, It);
8491 while Present (It.Typ) loop
8492 if It.Nam = Entity (Freeze_Expr) then
8493 Err := False;
8494 exit;
8495 end if;
8497 Get_Next_Interp (Index, It);
8498 end loop;
8499 end;
8500 end if;
8501 end Check_Overloaded_Name;
8503 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8505 begin
8506 -- Case of aspects Dimension, Dimension_System and Synchronization
8508 if A_Id = Aspect_Synchronization then
8509 return;
8511 -- Case of stream attributes, just have to compare entities. However,
8512 -- the expression is just a name (possibly overloaded), and there may
8513 -- be stream operations declared for unrelated types, so we just need
8514 -- to verify that one of these interpretations is the one available at
8515 -- at the freeze point.
8517 elsif A_Id = Aspect_Input or else
8518 A_Id = Aspect_Output or else
8519 A_Id = Aspect_Read or else
8520 A_Id = Aspect_Write
8521 then
8522 Analyze (End_Decl_Expr);
8523 Check_Overloaded_Name;
8525 elsif A_Id = Aspect_Variable_Indexing or else
8526 A_Id = Aspect_Constant_Indexing or else
8527 A_Id = Aspect_Default_Iterator or else
8528 A_Id = Aspect_Iterator_Element
8529 then
8530 -- Make type unfrozen before analysis, to prevent spurious errors
8531 -- about late attributes.
8533 Set_Is_Frozen (Ent, False);
8534 Analyze (End_Decl_Expr);
8535 Set_Is_Frozen (Ent, True);
8537 -- If the end of declarations comes before any other freeze
8538 -- point, the Freeze_Expr is not analyzed: no check needed.
8540 if Analyzed (Freeze_Expr) and then not In_Instance then
8541 Check_Overloaded_Name;
8542 else
8543 Err := False;
8544 end if;
8546 -- All other cases
8548 else
8549 -- Indicate that the expression comes from an aspect specification,
8550 -- which is used in subsequent analysis even if expansion is off.
8552 Set_Parent (End_Decl_Expr, ASN);
8554 -- In a generic context the aspect expressions have not been
8555 -- preanalyzed, so do it now. There are no conformance checks
8556 -- to perform in this case.
8558 if No (T) then
8559 Check_Aspect_At_Freeze_Point (ASN);
8560 return;
8562 -- The default values attributes may be defined in the private part,
8563 -- and the analysis of the expression may take place when only the
8564 -- partial view is visible. The expression must be scalar, so use
8565 -- the full view to resolve.
8567 elsif (A_Id = Aspect_Default_Value
8568 or else
8569 A_Id = Aspect_Default_Component_Value)
8570 and then Is_Private_Type (T)
8571 then
8572 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8574 else
8575 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8576 end if;
8578 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8579 end if;
8581 -- Output error message if error. Force error on aspect specification
8582 -- even if there is an error on the expression itself.
8584 if Err then
8585 Error_Msg_NE
8586 ("!visibility of aspect for& changes after freeze point",
8587 ASN, Ent);
8588 Error_Msg_NE
8589 ("info: & is frozen here, aspects evaluated at this point??",
8590 Freeze_Node (Ent), Ent);
8591 end if;
8592 end Check_Aspect_At_End_Of_Declarations;
8594 ----------------------------------
8595 -- Check_Aspect_At_Freeze_Point --
8596 ----------------------------------
8598 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8599 Ident : constant Node_Id := Identifier (ASN);
8600 -- Identifier (use Entity field to save expression)
8602 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8604 T : Entity_Id := Empty;
8605 -- Type required for preanalyze call
8607 begin
8608 -- On entry to this procedure, Entity (Ident) contains a copy of the
8609 -- original expression from the aspect, saved for this purpose.
8611 -- On exit from this procedure Entity (Ident) is unchanged, still
8612 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8613 -- of the expression, preanalyzed just after the freeze point.
8615 -- Make a copy of the expression to be preanalyzed
8617 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8619 -- Find type for preanalyze call
8621 case A_Id is
8623 -- No_Aspect should be impossible
8625 when No_Aspect =>
8626 raise Program_Error;
8628 -- Aspects taking an optional boolean argument
8630 when Boolean_Aspects |
8631 Library_Unit_Aspects =>
8633 T := Standard_Boolean;
8635 -- Aspects corresponding to attribute definition clauses
8637 when Aspect_Address =>
8638 T := RTE (RE_Address);
8640 when Aspect_Attach_Handler =>
8641 T := RTE (RE_Interrupt_ID);
8643 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8644 T := RTE (RE_Bit_Order);
8646 when Aspect_Convention =>
8647 return;
8649 when Aspect_CPU =>
8650 T := RTE (RE_CPU_Range);
8652 -- Default_Component_Value is resolved with the component type
8654 when Aspect_Default_Component_Value =>
8655 T := Component_Type (Entity (ASN));
8657 -- Default_Value is resolved with the type entity in question
8659 when Aspect_Default_Value =>
8660 T := Entity (ASN);
8662 -- Depends is a delayed aspect because it mentiones names first
8663 -- introduced by aspect Global which is already delayed. There is
8664 -- no action to be taken with respect to the aspect itself as the
8665 -- analysis is done by the corresponding pragma.
8667 when Aspect_Depends =>
8668 return;
8670 when Aspect_Dispatching_Domain =>
8671 T := RTE (RE_Dispatching_Domain);
8673 when Aspect_External_Tag =>
8674 T := Standard_String;
8676 when Aspect_External_Name =>
8677 T := Standard_String;
8679 -- Global is a delayed aspect because it may reference names that
8680 -- have not been declared yet. There is no action to be taken with
8681 -- respect to the aspect itself as the reference checking is done
8682 -- on the corresponding pragma.
8684 when Aspect_Global =>
8685 return;
8687 when Aspect_Link_Name =>
8688 T := Standard_String;
8690 when Aspect_Priority | Aspect_Interrupt_Priority =>
8691 T := Standard_Integer;
8693 when Aspect_Relative_Deadline =>
8694 T := RTE (RE_Time_Span);
8696 when Aspect_Small =>
8697 T := Universal_Real;
8699 -- For a simple storage pool, we have to retrieve the type of the
8700 -- pool object associated with the aspect's corresponding attribute
8701 -- definition clause.
8703 when Aspect_Simple_Storage_Pool =>
8704 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8706 when Aspect_Storage_Pool =>
8707 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8709 when Aspect_Alignment |
8710 Aspect_Component_Size |
8711 Aspect_Machine_Radix |
8712 Aspect_Object_Size |
8713 Aspect_Size |
8714 Aspect_Storage_Size |
8715 Aspect_Stream_Size |
8716 Aspect_Value_Size =>
8717 T := Any_Integer;
8719 when Aspect_Linker_Section =>
8720 T := Standard_String;
8722 when Aspect_Synchronization =>
8723 return;
8725 -- Special case, the expression of these aspects is just an entity
8726 -- that does not need any resolution, so just analyze.
8728 when Aspect_Input |
8729 Aspect_Output |
8730 Aspect_Read |
8731 Aspect_Suppress |
8732 Aspect_Unsuppress |
8733 Aspect_Warnings |
8734 Aspect_Write =>
8735 Analyze (Expression (ASN));
8736 return;
8738 -- Same for Iterator aspects, where the expression is a function
8739 -- name. Legality rules are checked separately.
8741 when Aspect_Constant_Indexing |
8742 Aspect_Default_Iterator |
8743 Aspect_Iterator_Element |
8744 Aspect_Variable_Indexing =>
8745 Analyze (Expression (ASN));
8746 return;
8748 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8750 when Aspect_Iterable =>
8751 T := Entity (ASN);
8753 declare
8754 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8755 Assoc : Node_Id;
8756 Expr : Node_Id;
8758 begin
8759 if Cursor = Any_Type then
8760 return;
8761 end if;
8763 Assoc := First (Component_Associations (Expression (ASN)));
8764 while Present (Assoc) loop
8765 Expr := Expression (Assoc);
8766 Analyze (Expr);
8768 if not Error_Posted (Expr) then
8769 Resolve_Iterable_Operation
8770 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8771 end if;
8773 Next (Assoc);
8774 end loop;
8775 end;
8777 return;
8779 -- Invariant/Predicate take boolean expressions
8781 when Aspect_Dynamic_Predicate |
8782 Aspect_Invariant |
8783 Aspect_Predicate |
8784 Aspect_Static_Predicate |
8785 Aspect_Type_Invariant =>
8786 T := Standard_Boolean;
8788 -- Here is the list of aspects that don't require delay analysis
8790 when Aspect_Abstract_State |
8791 Aspect_Annotate |
8792 Aspect_Contract_Cases |
8793 Aspect_Default_Initial_Condition |
8794 Aspect_Dimension |
8795 Aspect_Dimension_System |
8796 Aspect_Implicit_Dereference |
8797 Aspect_Initial_Condition |
8798 Aspect_Initializes |
8799 Aspect_Obsolescent |
8800 Aspect_Part_Of |
8801 Aspect_Post |
8802 Aspect_Postcondition |
8803 Aspect_Pre |
8804 Aspect_Precondition |
8805 Aspect_Refined_Depends |
8806 Aspect_Refined_Global |
8807 Aspect_Refined_Post |
8808 Aspect_Refined_State |
8809 Aspect_SPARK_Mode |
8810 Aspect_Test_Case =>
8811 raise Program_Error;
8813 end case;
8815 -- Do the preanalyze call
8817 Preanalyze_Spec_Expression (Expression (ASN), T);
8818 end Check_Aspect_At_Freeze_Point;
8820 -----------------------------------
8821 -- Check_Constant_Address_Clause --
8822 -----------------------------------
8824 procedure Check_Constant_Address_Clause
8825 (Expr : Node_Id;
8826 U_Ent : Entity_Id)
8828 procedure Check_At_Constant_Address (Nod : Node_Id);
8829 -- Checks that the given node N represents a name whose 'Address is
8830 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8831 -- address value is the same at the point of declaration of U_Ent and at
8832 -- the time of elaboration of the address clause.
8834 procedure Check_Expr_Constants (Nod : Node_Id);
8835 -- Checks that Nod meets the requirements for a constant address clause
8836 -- in the sense of the enclosing procedure.
8838 procedure Check_List_Constants (Lst : List_Id);
8839 -- Check that all elements of list Lst meet the requirements for a
8840 -- constant address clause in the sense of the enclosing procedure.
8842 -------------------------------
8843 -- Check_At_Constant_Address --
8844 -------------------------------
8846 procedure Check_At_Constant_Address (Nod : Node_Id) is
8847 begin
8848 if Is_Entity_Name (Nod) then
8849 if Present (Address_Clause (Entity ((Nod)))) then
8850 Error_Msg_NE
8851 ("invalid address clause for initialized object &!",
8852 Nod, U_Ent);
8853 Error_Msg_NE
8854 ("address for& cannot" &
8855 " depend on another address clause! (RM 13.1(22))!",
8856 Nod, U_Ent);
8858 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
8859 and then Sloc (U_Ent) < Sloc (Entity (Nod))
8860 then
8861 Error_Msg_NE
8862 ("invalid address clause for initialized object &!",
8863 Nod, U_Ent);
8864 Error_Msg_Node_2 := U_Ent;
8865 Error_Msg_NE
8866 ("\& must be defined before & (RM 13.1(22))!",
8867 Nod, Entity (Nod));
8868 end if;
8870 elsif Nkind (Nod) = N_Selected_Component then
8871 declare
8872 T : constant Entity_Id := Etype (Prefix (Nod));
8874 begin
8875 if (Is_Record_Type (T)
8876 and then Has_Discriminants (T))
8877 or else
8878 (Is_Access_Type (T)
8879 and then Is_Record_Type (Designated_Type (T))
8880 and then Has_Discriminants (Designated_Type (T)))
8881 then
8882 Error_Msg_NE
8883 ("invalid address clause for initialized object &!",
8884 Nod, U_Ent);
8885 Error_Msg_N
8886 ("\address cannot depend on component" &
8887 " of discriminated record (RM 13.1(22))!",
8888 Nod);
8889 else
8890 Check_At_Constant_Address (Prefix (Nod));
8891 end if;
8892 end;
8894 elsif Nkind (Nod) = N_Indexed_Component then
8895 Check_At_Constant_Address (Prefix (Nod));
8896 Check_List_Constants (Expressions (Nod));
8898 else
8899 Check_Expr_Constants (Nod);
8900 end if;
8901 end Check_At_Constant_Address;
8903 --------------------------
8904 -- Check_Expr_Constants --
8905 --------------------------
8907 procedure Check_Expr_Constants (Nod : Node_Id) is
8908 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
8909 Ent : Entity_Id := Empty;
8911 begin
8912 if Nkind (Nod) in N_Has_Etype
8913 and then Etype (Nod) = Any_Type
8914 then
8915 return;
8916 end if;
8918 case Nkind (Nod) is
8919 when N_Empty | N_Error =>
8920 return;
8922 when N_Identifier | N_Expanded_Name =>
8923 Ent := Entity (Nod);
8925 -- We need to look at the original node if it is different
8926 -- from the node, since we may have rewritten things and
8927 -- substituted an identifier representing the rewrite.
8929 if Original_Node (Nod) /= Nod then
8930 Check_Expr_Constants (Original_Node (Nod));
8932 -- If the node is an object declaration without initial
8933 -- value, some code has been expanded, and the expression
8934 -- is not constant, even if the constituents might be
8935 -- acceptable, as in A'Address + offset.
8937 if Ekind (Ent) = E_Variable
8938 and then
8939 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
8940 and then
8941 No (Expression (Declaration_Node (Ent)))
8942 then
8943 Error_Msg_NE
8944 ("invalid address clause for initialized object &!",
8945 Nod, U_Ent);
8947 -- If entity is constant, it may be the result of expanding
8948 -- a check. We must verify that its declaration appears
8949 -- before the object in question, else we also reject the
8950 -- address clause.
8952 elsif Ekind (Ent) = E_Constant
8953 and then In_Same_Source_Unit (Ent, U_Ent)
8954 and then Sloc (Ent) > Loc_U_Ent
8955 then
8956 Error_Msg_NE
8957 ("invalid address clause for initialized object &!",
8958 Nod, U_Ent);
8959 end if;
8961 return;
8962 end if;
8964 -- Otherwise look at the identifier and see if it is OK
8966 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
8967 or else Is_Type (Ent)
8968 then
8969 return;
8971 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
8973 -- This is the case where we must have Ent defined before
8974 -- U_Ent. Clearly if they are in different units this
8975 -- requirement is met since the unit containing Ent is
8976 -- already processed.
8978 if not In_Same_Source_Unit (Ent, U_Ent) then
8979 return;
8981 -- Otherwise location of Ent must be before the location
8982 -- of U_Ent, that's what prior defined means.
8984 elsif Sloc (Ent) < Loc_U_Ent then
8985 return;
8987 else
8988 Error_Msg_NE
8989 ("invalid address clause for initialized object &!",
8990 Nod, U_Ent);
8991 Error_Msg_Node_2 := U_Ent;
8992 Error_Msg_NE
8993 ("\& must be defined before & (RM 13.1(22))!",
8994 Nod, Ent);
8995 end if;
8997 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
8998 Check_Expr_Constants (Original_Node (Nod));
9000 else
9001 Error_Msg_NE
9002 ("invalid address clause for initialized object &!",
9003 Nod, U_Ent);
9005 if Comes_From_Source (Ent) then
9006 Error_Msg_NE
9007 ("\reference to variable& not allowed"
9008 & " (RM 13.1(22))!", Nod, Ent);
9009 else
9010 Error_Msg_N
9011 ("non-static expression not allowed"
9012 & " (RM 13.1(22))!", Nod);
9013 end if;
9014 end if;
9016 when N_Integer_Literal =>
9018 -- If this is a rewritten unchecked conversion, in a system
9019 -- where Address is an integer type, always use the base type
9020 -- for a literal value. This is user-friendly and prevents
9021 -- order-of-elaboration issues with instances of unchecked
9022 -- conversion.
9024 if Nkind (Original_Node (Nod)) = N_Function_Call then
9025 Set_Etype (Nod, Base_Type (Etype (Nod)));
9026 end if;
9028 when N_Real_Literal |
9029 N_String_Literal |
9030 N_Character_Literal =>
9031 return;
9033 when N_Range =>
9034 Check_Expr_Constants (Low_Bound (Nod));
9035 Check_Expr_Constants (High_Bound (Nod));
9037 when N_Explicit_Dereference =>
9038 Check_Expr_Constants (Prefix (Nod));
9040 when N_Indexed_Component =>
9041 Check_Expr_Constants (Prefix (Nod));
9042 Check_List_Constants (Expressions (Nod));
9044 when N_Slice =>
9045 Check_Expr_Constants (Prefix (Nod));
9046 Check_Expr_Constants (Discrete_Range (Nod));
9048 when N_Selected_Component =>
9049 Check_Expr_Constants (Prefix (Nod));
9051 when N_Attribute_Reference =>
9052 if Nam_In (Attribute_Name (Nod), Name_Address,
9053 Name_Access,
9054 Name_Unchecked_Access,
9055 Name_Unrestricted_Access)
9056 then
9057 Check_At_Constant_Address (Prefix (Nod));
9059 else
9060 Check_Expr_Constants (Prefix (Nod));
9061 Check_List_Constants (Expressions (Nod));
9062 end if;
9064 when N_Aggregate =>
9065 Check_List_Constants (Component_Associations (Nod));
9066 Check_List_Constants (Expressions (Nod));
9068 when N_Component_Association =>
9069 Check_Expr_Constants (Expression (Nod));
9071 when N_Extension_Aggregate =>
9072 Check_Expr_Constants (Ancestor_Part (Nod));
9073 Check_List_Constants (Component_Associations (Nod));
9074 Check_List_Constants (Expressions (Nod));
9076 when N_Null =>
9077 return;
9079 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9080 Check_Expr_Constants (Left_Opnd (Nod));
9081 Check_Expr_Constants (Right_Opnd (Nod));
9083 when N_Unary_Op =>
9084 Check_Expr_Constants (Right_Opnd (Nod));
9086 when N_Type_Conversion |
9087 N_Qualified_Expression |
9088 N_Allocator |
9089 N_Unchecked_Type_Conversion =>
9090 Check_Expr_Constants (Expression (Nod));
9092 when N_Function_Call =>
9093 if not Is_Pure (Entity (Name (Nod))) then
9094 Error_Msg_NE
9095 ("invalid address clause for initialized object &!",
9096 Nod, U_Ent);
9098 Error_Msg_NE
9099 ("\function & is not pure (RM 13.1(22))!",
9100 Nod, Entity (Name (Nod)));
9102 else
9103 Check_List_Constants (Parameter_Associations (Nod));
9104 end if;
9106 when N_Parameter_Association =>
9107 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9109 when others =>
9110 Error_Msg_NE
9111 ("invalid address clause for initialized object &!",
9112 Nod, U_Ent);
9113 Error_Msg_NE
9114 ("\must be constant defined before& (RM 13.1(22))!",
9115 Nod, U_Ent);
9116 end case;
9117 end Check_Expr_Constants;
9119 --------------------------
9120 -- Check_List_Constants --
9121 --------------------------
9123 procedure Check_List_Constants (Lst : List_Id) is
9124 Nod1 : Node_Id;
9126 begin
9127 if Present (Lst) then
9128 Nod1 := First (Lst);
9129 while Present (Nod1) loop
9130 Check_Expr_Constants (Nod1);
9131 Next (Nod1);
9132 end loop;
9133 end if;
9134 end Check_List_Constants;
9136 -- Start of processing for Check_Constant_Address_Clause
9138 begin
9139 -- If rep_clauses are to be ignored, no need for legality checks. In
9140 -- particular, no need to pester user about rep clauses that violate
9141 -- the rule on constant addresses, given that these clauses will be
9142 -- removed by Freeze before they reach the back end.
9144 if not Ignore_Rep_Clauses then
9145 Check_Expr_Constants (Expr);
9146 end if;
9147 end Check_Constant_Address_Clause;
9149 ---------------------------
9150 -- Check_Pool_Size_Clash --
9151 ---------------------------
9153 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9154 Post : Node_Id;
9156 begin
9157 -- We need to find out which one came first. Note that in the case of
9158 -- aspects mixed with pragmas there are cases where the processing order
9159 -- is reversed, which is why we do the check here.
9161 if Sloc (SP) < Sloc (SS) then
9162 Error_Msg_Sloc := Sloc (SP);
9163 Post := SS;
9164 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9166 else
9167 Error_Msg_Sloc := Sloc (SS);
9168 Post := SP;
9169 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9170 end if;
9172 Error_Msg_N
9173 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9174 end Check_Pool_Size_Clash;
9176 ----------------------------------------
9177 -- Check_Record_Representation_Clause --
9178 ----------------------------------------
9180 procedure Check_Record_Representation_Clause (N : Node_Id) is
9181 Loc : constant Source_Ptr := Sloc (N);
9182 Ident : constant Node_Id := Identifier (N);
9183 Rectype : Entity_Id;
9184 Fent : Entity_Id;
9185 CC : Node_Id;
9186 Fbit : Uint;
9187 Lbit : Uint;
9188 Hbit : Uint := Uint_0;
9189 Comp : Entity_Id;
9190 Pcomp : Entity_Id;
9192 Max_Bit_So_Far : Uint;
9193 -- Records the maximum bit position so far. If all field positions
9194 -- are monotonically increasing, then we can skip the circuit for
9195 -- checking for overlap, since no overlap is possible.
9197 Tagged_Parent : Entity_Id := Empty;
9198 -- This is set in the case of a derived tagged type for which we have
9199 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9200 -- positioned by record representation clauses). In this case we must
9201 -- check for overlap between components of this tagged type, and the
9202 -- components of its parent. Tagged_Parent will point to this parent
9203 -- type. For all other cases Tagged_Parent is left set to Empty.
9205 Parent_Last_Bit : Uint;
9206 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9207 -- last bit position for any field in the parent type. We only need to
9208 -- check overlap for fields starting below this point.
9210 Overlap_Check_Required : Boolean;
9211 -- Used to keep track of whether or not an overlap check is required
9213 Overlap_Detected : Boolean := False;
9214 -- Set True if an overlap is detected
9216 Ccount : Natural := 0;
9217 -- Number of component clauses in record rep clause
9219 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9220 -- Given two entities for record components or discriminants, checks
9221 -- if they have overlapping component clauses and issues errors if so.
9223 procedure Find_Component;
9224 -- Finds component entity corresponding to current component clause (in
9225 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9226 -- start/stop bits for the field. If there is no matching component or
9227 -- if the matching component does not have a component clause, then
9228 -- that's an error and Comp is set to Empty, but no error message is
9229 -- issued, since the message was already given. Comp is also set to
9230 -- Empty if the current "component clause" is in fact a pragma.
9232 -----------------------------
9233 -- Check_Component_Overlap --
9234 -----------------------------
9236 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9237 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9238 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9240 begin
9241 if Present (CC1) and then Present (CC2) then
9243 -- Exclude odd case where we have two tag components in the same
9244 -- record, both at location zero. This seems a bit strange, but
9245 -- it seems to happen in some circumstances, perhaps on an error.
9247 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9248 return;
9249 end if;
9251 -- Here we check if the two fields overlap
9253 declare
9254 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9255 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9256 E1 : constant Uint := S1 + Esize (C1_Ent);
9257 E2 : constant Uint := S2 + Esize (C2_Ent);
9259 begin
9260 if E2 <= S1 or else E1 <= S2 then
9261 null;
9262 else
9263 Error_Msg_Node_2 := Component_Name (CC2);
9264 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9265 Error_Msg_Node_1 := Component_Name (CC1);
9266 Error_Msg_N
9267 ("component& overlaps & #", Component_Name (CC1));
9268 Overlap_Detected := True;
9269 end if;
9270 end;
9271 end if;
9272 end Check_Component_Overlap;
9274 --------------------
9275 -- Find_Component --
9276 --------------------
9278 procedure Find_Component is
9280 procedure Search_Component (R : Entity_Id);
9281 -- Search components of R for a match. If found, Comp is set
9283 ----------------------
9284 -- Search_Component --
9285 ----------------------
9287 procedure Search_Component (R : Entity_Id) is
9288 begin
9289 Comp := First_Component_Or_Discriminant (R);
9290 while Present (Comp) loop
9292 -- Ignore error of attribute name for component name (we
9293 -- already gave an error message for this, so no need to
9294 -- complain here)
9296 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9297 null;
9298 else
9299 exit when Chars (Comp) = Chars (Component_Name (CC));
9300 end if;
9302 Next_Component_Or_Discriminant (Comp);
9303 end loop;
9304 end Search_Component;
9306 -- Start of processing for Find_Component
9308 begin
9309 -- Return with Comp set to Empty if we have a pragma
9311 if Nkind (CC) = N_Pragma then
9312 Comp := Empty;
9313 return;
9314 end if;
9316 -- Search current record for matching component
9318 Search_Component (Rectype);
9320 -- If not found, maybe component of base type discriminant that is
9321 -- absent from statically constrained first subtype.
9323 if No (Comp) then
9324 Search_Component (Base_Type (Rectype));
9325 end if;
9327 -- If no component, or the component does not reference the component
9328 -- clause in question, then there was some previous error for which
9329 -- we already gave a message, so just return with Comp Empty.
9331 if No (Comp) or else Component_Clause (Comp) /= CC then
9332 Check_Error_Detected;
9333 Comp := Empty;
9335 -- Normal case where we have a component clause
9337 else
9338 Fbit := Component_Bit_Offset (Comp);
9339 Lbit := Fbit + Esize (Comp) - 1;
9340 end if;
9341 end Find_Component;
9343 -- Start of processing for Check_Record_Representation_Clause
9345 begin
9346 Find_Type (Ident);
9347 Rectype := Entity (Ident);
9349 if Rectype = Any_Type then
9350 return;
9351 else
9352 Rectype := Underlying_Type (Rectype);
9353 end if;
9355 -- See if we have a fully repped derived tagged type
9357 declare
9358 PS : constant Entity_Id := Parent_Subtype (Rectype);
9360 begin
9361 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9362 Tagged_Parent := PS;
9364 -- Find maximum bit of any component of the parent type
9366 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9367 Pcomp := First_Entity (Tagged_Parent);
9368 while Present (Pcomp) loop
9369 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9370 if Component_Bit_Offset (Pcomp) /= No_Uint
9371 and then Known_Static_Esize (Pcomp)
9372 then
9373 Parent_Last_Bit :=
9374 UI_Max
9375 (Parent_Last_Bit,
9376 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9377 end if;
9379 Next_Entity (Pcomp);
9380 end if;
9381 end loop;
9382 end if;
9383 end;
9385 -- All done if no component clauses
9387 CC := First (Component_Clauses (N));
9389 if No (CC) then
9390 return;
9391 end if;
9393 -- If a tag is present, then create a component clause that places it
9394 -- at the start of the record (otherwise gigi may place it after other
9395 -- fields that have rep clauses).
9397 Fent := First_Entity (Rectype);
9399 if Nkind (Fent) = N_Defining_Identifier
9400 and then Chars (Fent) = Name_uTag
9401 then
9402 Set_Component_Bit_Offset (Fent, Uint_0);
9403 Set_Normalized_Position (Fent, Uint_0);
9404 Set_Normalized_First_Bit (Fent, Uint_0);
9405 Set_Normalized_Position_Max (Fent, Uint_0);
9406 Init_Esize (Fent, System_Address_Size);
9408 Set_Component_Clause (Fent,
9409 Make_Component_Clause (Loc,
9410 Component_Name => Make_Identifier (Loc, Name_uTag),
9412 Position => Make_Integer_Literal (Loc, Uint_0),
9413 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9414 Last_Bit =>
9415 Make_Integer_Literal (Loc,
9416 UI_From_Int (System_Address_Size))));
9418 Ccount := Ccount + 1;
9419 end if;
9421 Max_Bit_So_Far := Uint_Minus_1;
9422 Overlap_Check_Required := False;
9424 -- Process the component clauses
9426 while Present (CC) loop
9427 Find_Component;
9429 if Present (Comp) then
9430 Ccount := Ccount + 1;
9432 -- We need a full overlap check if record positions non-monotonic
9434 if Fbit <= Max_Bit_So_Far then
9435 Overlap_Check_Required := True;
9436 end if;
9438 Max_Bit_So_Far := Lbit;
9440 -- Check bit position out of range of specified size
9442 if Has_Size_Clause (Rectype)
9443 and then RM_Size (Rectype) <= Lbit
9444 then
9445 Error_Msg_N
9446 ("bit number out of range of specified size",
9447 Last_Bit (CC));
9449 -- Check for overlap with tag component
9451 else
9452 if Is_Tagged_Type (Rectype)
9453 and then Fbit < System_Address_Size
9454 then
9455 Error_Msg_NE
9456 ("component overlaps tag field of&",
9457 Component_Name (CC), Rectype);
9458 Overlap_Detected := True;
9459 end if;
9461 if Hbit < Lbit then
9462 Hbit := Lbit;
9463 end if;
9464 end if;
9466 -- Check parent overlap if component might overlap parent field
9468 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9469 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9470 while Present (Pcomp) loop
9471 if not Is_Tag (Pcomp)
9472 and then Chars (Pcomp) /= Name_uParent
9473 then
9474 Check_Component_Overlap (Comp, Pcomp);
9475 end if;
9477 Next_Component_Or_Discriminant (Pcomp);
9478 end loop;
9479 end if;
9480 end if;
9482 Next (CC);
9483 end loop;
9485 -- Now that we have processed all the component clauses, check for
9486 -- overlap. We have to leave this till last, since the components can
9487 -- appear in any arbitrary order in the representation clause.
9489 -- We do not need this check if all specified ranges were monotonic,
9490 -- as recorded by Overlap_Check_Required being False at this stage.
9492 -- This first section checks if there are any overlapping entries at
9493 -- all. It does this by sorting all entries and then seeing if there are
9494 -- any overlaps. If there are none, then that is decisive, but if there
9495 -- are overlaps, they may still be OK (they may result from fields in
9496 -- different variants).
9498 if Overlap_Check_Required then
9499 Overlap_Check1 : declare
9501 OC_Fbit : array (0 .. Ccount) of Uint;
9502 -- First-bit values for component clauses, the value is the offset
9503 -- of the first bit of the field from start of record. The zero
9504 -- entry is for use in sorting.
9506 OC_Lbit : array (0 .. Ccount) of Uint;
9507 -- Last-bit values for component clauses, the value is the offset
9508 -- of the last bit of the field from start of record. The zero
9509 -- entry is for use in sorting.
9511 OC_Count : Natural := 0;
9512 -- Count of entries in OC_Fbit and OC_Lbit
9514 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9515 -- Compare routine for Sort
9517 procedure OC_Move (From : Natural; To : Natural);
9518 -- Move routine for Sort
9520 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9522 -----------
9523 -- OC_Lt --
9524 -----------
9526 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9527 begin
9528 return OC_Fbit (Op1) < OC_Fbit (Op2);
9529 end OC_Lt;
9531 -------------
9532 -- OC_Move --
9533 -------------
9535 procedure OC_Move (From : Natural; To : Natural) is
9536 begin
9537 OC_Fbit (To) := OC_Fbit (From);
9538 OC_Lbit (To) := OC_Lbit (From);
9539 end OC_Move;
9541 -- Start of processing for Overlap_Check
9543 begin
9544 CC := First (Component_Clauses (N));
9545 while Present (CC) loop
9547 -- Exclude component clause already marked in error
9549 if not Error_Posted (CC) then
9550 Find_Component;
9552 if Present (Comp) then
9553 OC_Count := OC_Count + 1;
9554 OC_Fbit (OC_Count) := Fbit;
9555 OC_Lbit (OC_Count) := Lbit;
9556 end if;
9557 end if;
9559 Next (CC);
9560 end loop;
9562 Sorting.Sort (OC_Count);
9564 Overlap_Check_Required := False;
9565 for J in 1 .. OC_Count - 1 loop
9566 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9567 Overlap_Check_Required := True;
9568 exit;
9569 end if;
9570 end loop;
9571 end Overlap_Check1;
9572 end if;
9574 -- If Overlap_Check_Required is still True, then we have to do the full
9575 -- scale overlap check, since we have at least two fields that do
9576 -- overlap, and we need to know if that is OK since they are in
9577 -- different variant, or whether we have a definite problem.
9579 if Overlap_Check_Required then
9580 Overlap_Check2 : declare
9581 C1_Ent, C2_Ent : Entity_Id;
9582 -- Entities of components being checked for overlap
9584 Clist : Node_Id;
9585 -- Component_List node whose Component_Items are being checked
9587 Citem : Node_Id;
9588 -- Component declaration for component being checked
9590 begin
9591 C1_Ent := First_Entity (Base_Type (Rectype));
9593 -- Loop through all components in record. For each component check
9594 -- for overlap with any of the preceding elements on the component
9595 -- list containing the component and also, if the component is in
9596 -- a variant, check against components outside the case structure.
9597 -- This latter test is repeated recursively up the variant tree.
9599 Main_Component_Loop : while Present (C1_Ent) loop
9600 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9601 goto Continue_Main_Component_Loop;
9602 end if;
9604 -- Skip overlap check if entity has no declaration node. This
9605 -- happens with discriminants in constrained derived types.
9606 -- Possibly we are missing some checks as a result, but that
9607 -- does not seem terribly serious.
9609 if No (Declaration_Node (C1_Ent)) then
9610 goto Continue_Main_Component_Loop;
9611 end if;
9613 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9615 -- Loop through component lists that need checking. Check the
9616 -- current component list and all lists in variants above us.
9618 Component_List_Loop : loop
9620 -- If derived type definition, go to full declaration
9621 -- If at outer level, check discriminants if there are any.
9623 if Nkind (Clist) = N_Derived_Type_Definition then
9624 Clist := Parent (Clist);
9625 end if;
9627 -- Outer level of record definition, check discriminants
9629 if Nkind_In (Clist, N_Full_Type_Declaration,
9630 N_Private_Type_Declaration)
9631 then
9632 if Has_Discriminants (Defining_Identifier (Clist)) then
9633 C2_Ent :=
9634 First_Discriminant (Defining_Identifier (Clist));
9635 while Present (C2_Ent) loop
9636 exit when C1_Ent = C2_Ent;
9637 Check_Component_Overlap (C1_Ent, C2_Ent);
9638 Next_Discriminant (C2_Ent);
9639 end loop;
9640 end if;
9642 -- Record extension case
9644 elsif Nkind (Clist) = N_Derived_Type_Definition then
9645 Clist := Empty;
9647 -- Otherwise check one component list
9649 else
9650 Citem := First (Component_Items (Clist));
9651 while Present (Citem) loop
9652 if Nkind (Citem) = N_Component_Declaration then
9653 C2_Ent := Defining_Identifier (Citem);
9654 exit when C1_Ent = C2_Ent;
9655 Check_Component_Overlap (C1_Ent, C2_Ent);
9656 end if;
9658 Next (Citem);
9659 end loop;
9660 end if;
9662 -- Check for variants above us (the parent of the Clist can
9663 -- be a variant, in which case its parent is a variant part,
9664 -- and the parent of the variant part is a component list
9665 -- whose components must all be checked against the current
9666 -- component for overlap).
9668 if Nkind (Parent (Clist)) = N_Variant then
9669 Clist := Parent (Parent (Parent (Clist)));
9671 -- Check for possible discriminant part in record, this
9672 -- is treated essentially as another level in the
9673 -- recursion. For this case the parent of the component
9674 -- list is the record definition, and its parent is the
9675 -- full type declaration containing the discriminant
9676 -- specifications.
9678 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9679 Clist := Parent (Parent ((Clist)));
9681 -- If neither of these two cases, we are at the top of
9682 -- the tree.
9684 else
9685 exit Component_List_Loop;
9686 end if;
9687 end loop Component_List_Loop;
9689 <<Continue_Main_Component_Loop>>
9690 Next_Entity (C1_Ent);
9692 end loop Main_Component_Loop;
9693 end Overlap_Check2;
9694 end if;
9696 -- The following circuit deals with warning on record holes (gaps). We
9697 -- skip this check if overlap was detected, since it makes sense for the
9698 -- programmer to fix this illegality before worrying about warnings.
9700 if not Overlap_Detected and Warn_On_Record_Holes then
9701 Record_Hole_Check : declare
9702 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9703 -- Full declaration of record type
9705 procedure Check_Component_List
9706 (CL : Node_Id;
9707 Sbit : Uint;
9708 DS : List_Id);
9709 -- Check component list CL for holes. The starting bit should be
9710 -- Sbit. which is zero for the main record component list and set
9711 -- appropriately for recursive calls for variants. DS is set to
9712 -- a list of discriminant specifications to be included in the
9713 -- consideration of components. It is No_List if none to consider.
9715 --------------------------
9716 -- Check_Component_List --
9717 --------------------------
9719 procedure Check_Component_List
9720 (CL : Node_Id;
9721 Sbit : Uint;
9722 DS : List_Id)
9724 Compl : Integer;
9726 begin
9727 Compl := Integer (List_Length (Component_Items (CL)));
9729 if DS /= No_List then
9730 Compl := Compl + Integer (List_Length (DS));
9731 end if;
9733 declare
9734 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9735 -- Gather components (zero entry is for sort routine)
9737 Ncomps : Natural := 0;
9738 -- Number of entries stored in Comps (starting at Comps (1))
9740 Citem : Node_Id;
9741 -- One component item or discriminant specification
9743 Nbit : Uint;
9744 -- Starting bit for next component
9746 CEnt : Entity_Id;
9747 -- Component entity
9749 Variant : Node_Id;
9750 -- One variant
9752 function Lt (Op1, Op2 : Natural) return Boolean;
9753 -- Compare routine for Sort
9755 procedure Move (From : Natural; To : Natural);
9756 -- Move routine for Sort
9758 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9760 --------
9761 -- Lt --
9762 --------
9764 function Lt (Op1, Op2 : Natural) return Boolean is
9765 begin
9766 return Component_Bit_Offset (Comps (Op1))
9768 Component_Bit_Offset (Comps (Op2));
9769 end Lt;
9771 ----------
9772 -- Move --
9773 ----------
9775 procedure Move (From : Natural; To : Natural) is
9776 begin
9777 Comps (To) := Comps (From);
9778 end Move;
9780 begin
9781 -- Gather discriminants into Comp
9783 if DS /= No_List then
9784 Citem := First (DS);
9785 while Present (Citem) loop
9786 if Nkind (Citem) = N_Discriminant_Specification then
9787 declare
9788 Ent : constant Entity_Id :=
9789 Defining_Identifier (Citem);
9790 begin
9791 if Ekind (Ent) = E_Discriminant then
9792 Ncomps := Ncomps + 1;
9793 Comps (Ncomps) := Ent;
9794 end if;
9795 end;
9796 end if;
9798 Next (Citem);
9799 end loop;
9800 end if;
9802 -- Gather component entities into Comp
9804 Citem := First (Component_Items (CL));
9805 while Present (Citem) loop
9806 if Nkind (Citem) = N_Component_Declaration then
9807 Ncomps := Ncomps + 1;
9808 Comps (Ncomps) := Defining_Identifier (Citem);
9809 end if;
9811 Next (Citem);
9812 end loop;
9814 -- Now sort the component entities based on the first bit.
9815 -- Note we already know there are no overlapping components.
9817 Sorting.Sort (Ncomps);
9819 -- Loop through entries checking for holes
9821 Nbit := Sbit;
9822 for J in 1 .. Ncomps loop
9823 CEnt := Comps (J);
9824 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9826 if Error_Msg_Uint_1 > 0 then
9827 Error_Msg_NE
9828 ("?H?^-bit gap before component&",
9829 Component_Name (Component_Clause (CEnt)), CEnt);
9830 end if;
9832 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9833 end loop;
9835 -- Process variant parts recursively if present
9837 if Present (Variant_Part (CL)) then
9838 Variant := First (Variants (Variant_Part (CL)));
9839 while Present (Variant) loop
9840 Check_Component_List
9841 (Component_List (Variant), Nbit, No_List);
9842 Next (Variant);
9843 end loop;
9844 end if;
9845 end;
9846 end Check_Component_List;
9848 -- Start of processing for Record_Hole_Check
9850 begin
9851 declare
9852 Sbit : Uint;
9854 begin
9855 if Is_Tagged_Type (Rectype) then
9856 Sbit := UI_From_Int (System_Address_Size);
9857 else
9858 Sbit := Uint_0;
9859 end if;
9861 if Nkind (Decl) = N_Full_Type_Declaration
9862 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9863 then
9864 Check_Component_List
9865 (Component_List (Type_Definition (Decl)),
9866 Sbit,
9867 Discriminant_Specifications (Decl));
9868 end if;
9869 end;
9870 end Record_Hole_Check;
9871 end if;
9873 -- For records that have component clauses for all components, and whose
9874 -- size is less than or equal to 32, we need to know the size in the
9875 -- front end to activate possible packed array processing where the
9876 -- component type is a record.
9878 -- At this stage Hbit + 1 represents the first unused bit from all the
9879 -- component clauses processed, so if the component clauses are
9880 -- complete, then this is the length of the record.
9882 -- For records longer than System.Storage_Unit, and for those where not
9883 -- all components have component clauses, the back end determines the
9884 -- length (it may for example be appropriate to round up the size
9885 -- to some convenient boundary, based on alignment considerations, etc).
9887 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
9889 -- Nothing to do if at least one component has no component clause
9891 Comp := First_Component_Or_Discriminant (Rectype);
9892 while Present (Comp) loop
9893 exit when No (Component_Clause (Comp));
9894 Next_Component_Or_Discriminant (Comp);
9895 end loop;
9897 -- If we fall out of loop, all components have component clauses
9898 -- and so we can set the size to the maximum value.
9900 if No (Comp) then
9901 Set_RM_Size (Rectype, Hbit + 1);
9902 end if;
9903 end if;
9904 end Check_Record_Representation_Clause;
9906 ----------------
9907 -- Check_Size --
9908 ----------------
9910 procedure Check_Size
9911 (N : Node_Id;
9912 T : Entity_Id;
9913 Siz : Uint;
9914 Biased : out Boolean)
9916 UT : constant Entity_Id := Underlying_Type (T);
9917 M : Uint;
9919 begin
9920 Biased := False;
9922 -- Reject patently improper size values.
9924 if Is_Elementary_Type (T)
9925 and then Siz > UI_From_Int (Int'Last)
9926 then
9927 Error_Msg_N ("Size value too large for elementary type", N);
9929 if Nkind (Original_Node (N)) = N_Op_Expon then
9930 Error_Msg_N
9931 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
9932 end if;
9933 end if;
9935 -- Dismiss generic types
9937 if Is_Generic_Type (T)
9938 or else
9939 Is_Generic_Type (UT)
9940 or else
9941 Is_Generic_Type (Root_Type (UT))
9942 then
9943 return;
9945 -- Guard against previous errors
9947 elsif No (UT) or else UT = Any_Type then
9948 Check_Error_Detected;
9949 return;
9951 -- Check case of bit packed array
9953 elsif Is_Array_Type (UT)
9954 and then Known_Static_Component_Size (UT)
9955 and then Is_Bit_Packed_Array (UT)
9956 then
9957 declare
9958 Asiz : Uint;
9959 Indx : Node_Id;
9960 Ityp : Entity_Id;
9962 begin
9963 Asiz := Component_Size (UT);
9964 Indx := First_Index (UT);
9965 loop
9966 Ityp := Etype (Indx);
9968 -- If non-static bound, then we are not in the business of
9969 -- trying to check the length, and indeed an error will be
9970 -- issued elsewhere, since sizes of non-static array types
9971 -- cannot be set implicitly or explicitly.
9973 if not Is_OK_Static_Subtype (Ityp) then
9974 return;
9975 end if;
9977 -- Otherwise accumulate next dimension
9979 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
9980 Expr_Value (Type_Low_Bound (Ityp)) +
9981 Uint_1);
9983 Next_Index (Indx);
9984 exit when No (Indx);
9985 end loop;
9987 if Asiz <= Siz then
9988 return;
9990 else
9991 Error_Msg_Uint_1 := Asiz;
9992 Error_Msg_NE
9993 ("size for& too small, minimum allowed is ^", N, T);
9994 Set_Esize (T, Asiz);
9995 Set_RM_Size (T, Asiz);
9996 end if;
9997 end;
9999 -- All other composite types are ignored
10001 elsif Is_Composite_Type (UT) then
10002 return;
10004 -- For fixed-point types, don't check minimum if type is not frozen,
10005 -- since we don't know all the characteristics of the type that can
10006 -- affect the size (e.g. a specified small) till freeze time.
10008 elsif Is_Fixed_Point_Type (UT)
10009 and then not Is_Frozen (UT)
10010 then
10011 null;
10013 -- Cases for which a minimum check is required
10015 else
10016 -- Ignore if specified size is correct for the type
10018 if Known_Esize (UT) and then Siz = Esize (UT) then
10019 return;
10020 end if;
10022 -- Otherwise get minimum size
10024 M := UI_From_Int (Minimum_Size (UT));
10026 if Siz < M then
10028 -- Size is less than minimum size, but one possibility remains
10029 -- that we can manage with the new size if we bias the type.
10031 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10033 if Siz < M then
10034 Error_Msg_Uint_1 := M;
10035 Error_Msg_NE
10036 ("size for& too small, minimum allowed is ^", N, T);
10037 Set_Esize (T, M);
10038 Set_RM_Size (T, M);
10039 else
10040 Biased := True;
10041 end if;
10042 end if;
10043 end if;
10044 end Check_Size;
10046 --------------------------
10047 -- Freeze_Entity_Checks --
10048 --------------------------
10050 procedure Freeze_Entity_Checks (N : Node_Id) is
10051 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10052 -- Inspect the primitive operations of type Typ and hide all pairs of
10053 -- implicitly declared non-overridden non-fully conformant homographs
10054 -- (Ada RM 8.3 12.3/2).
10056 -------------------------------------
10057 -- Hide_Non_Overridden_Subprograms --
10058 -------------------------------------
10060 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10061 procedure Hide_Matching_Homographs
10062 (Subp_Id : Entity_Id;
10063 Start_Elmt : Elmt_Id);
10064 -- Inspect a list of primitive operations starting with Start_Elmt
10065 -- and find matching implicitly declared non-overridden non-fully
10066 -- conformant homographs of Subp_Id. If found, all matches along
10067 -- with Subp_Id are hidden from all visibility.
10069 function Is_Non_Overridden_Or_Null_Procedure
10070 (Subp_Id : Entity_Id) return Boolean;
10071 -- Determine whether subprogram Subp_Id is implicitly declared non-
10072 -- overridden subprogram or an implicitly declared null procedure.
10074 ------------------------------
10075 -- Hide_Matching_Homographs --
10076 ------------------------------
10078 procedure Hide_Matching_Homographs
10079 (Subp_Id : Entity_Id;
10080 Start_Elmt : Elmt_Id)
10082 Prim : Entity_Id;
10083 Prim_Elmt : Elmt_Id;
10085 begin
10086 Prim_Elmt := Start_Elmt;
10087 while Present (Prim_Elmt) loop
10088 Prim := Node (Prim_Elmt);
10090 -- The current primitive is implicitly declared non-overridden
10091 -- non-fully conformant homograph of Subp_Id. Both subprograms
10092 -- must be hidden from visibility.
10094 if Chars (Prim) = Chars (Subp_Id)
10095 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10096 and then not Fully_Conformant (Prim, Subp_Id)
10097 then
10098 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10099 Set_Is_Immediately_Visible (Prim, False);
10100 Set_Is_Potentially_Use_Visible (Prim, False);
10102 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10103 Set_Is_Immediately_Visible (Subp_Id, False);
10104 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10105 end if;
10107 Next_Elmt (Prim_Elmt);
10108 end loop;
10109 end Hide_Matching_Homographs;
10111 -----------------------------------------
10112 -- Is_Non_Overridden_Or_Null_Procedure --
10113 -----------------------------------------
10115 function Is_Non_Overridden_Or_Null_Procedure
10116 (Subp_Id : Entity_Id) return Boolean
10118 Alias_Id : Entity_Id;
10120 begin
10121 -- The subprogram is inherited (implicitly declared), it does not
10122 -- override and does not cover a primitive of an interface.
10124 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10125 and then Present (Alias (Subp_Id))
10126 and then No (Interface_Alias (Subp_Id))
10127 and then No (Overridden_Operation (Subp_Id))
10128 then
10129 Alias_Id := Alias (Subp_Id);
10131 if Requires_Overriding (Alias_Id) then
10132 return True;
10134 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10135 and then Null_Present (Parent (Alias_Id))
10136 then
10137 return True;
10138 end if;
10139 end if;
10141 return False;
10142 end Is_Non_Overridden_Or_Null_Procedure;
10144 -- Local variables
10146 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10147 Prim : Entity_Id;
10148 Prim_Elmt : Elmt_Id;
10150 -- Start of processing for Hide_Non_Overridden_Subprograms
10152 begin
10153 -- Inspect the list of primitives looking for non-overridden
10154 -- subprograms.
10156 if Present (Prim_Ops) then
10157 Prim_Elmt := First_Elmt (Prim_Ops);
10158 while Present (Prim_Elmt) loop
10159 Prim := Node (Prim_Elmt);
10160 Next_Elmt (Prim_Elmt);
10162 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10163 Hide_Matching_Homographs
10164 (Subp_Id => Prim,
10165 Start_Elmt => Prim_Elmt);
10166 end if;
10167 end loop;
10168 end if;
10169 end Hide_Non_Overridden_Subprograms;
10171 ---------------------
10172 -- Local variables --
10173 ---------------------
10175 E : constant Entity_Id := Entity (N);
10177 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10178 -- True in non-generic case. Some of the processing here is skipped
10179 -- for the generic case since it is not needed. Basically in the
10180 -- generic case, we only need to do stuff that might generate error
10181 -- messages or warnings.
10183 -- Start of processing for Freeze_Entity_Checks
10185 begin
10186 -- Remember that we are processing a freezing entity. Required to
10187 -- ensure correct decoration of internal entities associated with
10188 -- interfaces (see New_Overloaded_Entity).
10190 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10192 -- For tagged types covering interfaces add internal entities that link
10193 -- the primitives of the interfaces with the primitives that cover them.
10194 -- Note: These entities were originally generated only when generating
10195 -- code because their main purpose was to provide support to initialize
10196 -- the secondary dispatch tables. They are now generated also when
10197 -- compiling with no code generation to provide ASIS the relationship
10198 -- between interface primitives and tagged type primitives. They are
10199 -- also used to locate primitives covering interfaces when processing
10200 -- generics (see Derive_Subprograms).
10202 -- This is not needed in the generic case
10204 if Ada_Version >= Ada_2005
10205 and then Non_Generic_Case
10206 and then Ekind (E) = E_Record_Type
10207 and then Is_Tagged_Type (E)
10208 and then not Is_Interface (E)
10209 and then Has_Interfaces (E)
10210 then
10211 -- This would be a good common place to call the routine that checks
10212 -- overriding of interface primitives (and thus factorize calls to
10213 -- Check_Abstract_Overriding located at different contexts in the
10214 -- compiler). However, this is not possible because it causes
10215 -- spurious errors in case of late overriding.
10217 Add_Internal_Interface_Entities (E);
10218 end if;
10220 -- After all forms of overriding have been resolved, a tagged type may
10221 -- be left with a set of implicitly declared and possibly erroneous
10222 -- abstract subprograms, null procedures and subprograms that require
10223 -- overriding. If this set contains fully conformat homographs, then one
10224 -- is chosen arbitrarily (already done during resolution), otherwise all
10225 -- remaining non-fully conformant homographs are hidden from visibility
10226 -- (Ada RM 8.3 12.3/2).
10228 if Is_Tagged_Type (E) then
10229 Hide_Non_Overridden_Subprograms (E);
10230 end if;
10232 -- Check CPP types
10234 if Ekind (E) = E_Record_Type
10235 and then Is_CPP_Class (E)
10236 and then Is_Tagged_Type (E)
10237 and then Tagged_Type_Expansion
10238 then
10239 if CPP_Num_Prims (E) = 0 then
10241 -- If the CPP type has user defined components then it must import
10242 -- primitives from C++. This is required because if the C++ class
10243 -- has no primitives then the C++ compiler does not added the _tag
10244 -- component to the type.
10246 if First_Entity (E) /= Last_Entity (E) then
10247 Error_Msg_N
10248 ("'C'P'P type must import at least one primitive from C++??",
10250 end if;
10251 end if;
10253 -- Check that all its primitives are abstract or imported from C++.
10254 -- Check also availability of the C++ constructor.
10256 declare
10257 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10258 Elmt : Elmt_Id;
10259 Error_Reported : Boolean := False;
10260 Prim : Node_Id;
10262 begin
10263 Elmt := First_Elmt (Primitive_Operations (E));
10264 while Present (Elmt) loop
10265 Prim := Node (Elmt);
10267 if Comes_From_Source (Prim) then
10268 if Is_Abstract_Subprogram (Prim) then
10269 null;
10271 elsif not Is_Imported (Prim)
10272 or else Convention (Prim) /= Convention_CPP
10273 then
10274 Error_Msg_N
10275 ("primitives of 'C'P'P types must be imported from C++ "
10276 & "or abstract??", Prim);
10278 elsif not Has_Constructors
10279 and then not Error_Reported
10280 then
10281 Error_Msg_Name_1 := Chars (E);
10282 Error_Msg_N
10283 ("??'C'P'P constructor required for type %", Prim);
10284 Error_Reported := True;
10285 end if;
10286 end if;
10288 Next_Elmt (Elmt);
10289 end loop;
10290 end;
10291 end if;
10293 -- Check Ada derivation of CPP type
10295 if Expander_Active -- why? losing errors in -gnatc mode???
10296 and then Tagged_Type_Expansion
10297 and then Ekind (E) = E_Record_Type
10298 and then Etype (E) /= E
10299 and then Is_CPP_Class (Etype (E))
10300 and then CPP_Num_Prims (Etype (E)) > 0
10301 and then not Is_CPP_Class (E)
10302 and then not Has_CPP_Constructors (Etype (E))
10303 then
10304 -- If the parent has C++ primitives but it has no constructor then
10305 -- check that all the primitives are overridden in this derivation;
10306 -- otherwise the constructor of the parent is needed to build the
10307 -- dispatch table.
10309 declare
10310 Elmt : Elmt_Id;
10311 Prim : Node_Id;
10313 begin
10314 Elmt := First_Elmt (Primitive_Operations (E));
10315 while Present (Elmt) loop
10316 Prim := Node (Elmt);
10318 if not Is_Abstract_Subprogram (Prim)
10319 and then No (Interface_Alias (Prim))
10320 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10321 then
10322 Error_Msg_Name_1 := Chars (Etype (E));
10323 Error_Msg_N
10324 ("'C'P'P constructor required for parent type %", E);
10325 exit;
10326 end if;
10328 Next_Elmt (Elmt);
10329 end loop;
10330 end;
10331 end if;
10333 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10335 -- If we have a type with predicates, build predicate function. This
10336 -- is not needed in the generic case, and is not needed within TSS
10337 -- subprograms and other predefined primitives.
10339 if Non_Generic_Case
10340 and then Is_Type (E)
10341 and then Has_Predicates (E)
10342 and then not Within_Internal_Subprogram
10343 then
10344 Build_Predicate_Functions (E, N);
10345 end if;
10347 -- If type has delayed aspects, this is where we do the preanalysis at
10348 -- the freeze point, as part of the consistent visibility check. Note
10349 -- that this must be done after calling Build_Predicate_Functions or
10350 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10351 -- the subtype name in the saved expression so that they will not cause
10352 -- trouble in the preanalysis.
10354 -- This is also not needed in the generic case
10356 if Non_Generic_Case
10357 and then Has_Delayed_Aspects (E)
10358 and then Scope (E) = Current_Scope
10359 then
10360 -- Retrieve the visibility to the discriminants in order to properly
10361 -- analyze the aspects.
10363 Push_Scope_And_Install_Discriminants (E);
10365 declare
10366 Ritem : Node_Id;
10368 begin
10369 -- Look for aspect specification entries for this entity
10371 Ritem := First_Rep_Item (E);
10372 while Present (Ritem) loop
10373 if Nkind (Ritem) = N_Aspect_Specification
10374 and then Entity (Ritem) = E
10375 and then Is_Delayed_Aspect (Ritem)
10376 then
10377 Check_Aspect_At_Freeze_Point (Ritem);
10378 end if;
10380 Next_Rep_Item (Ritem);
10381 end loop;
10382 end;
10384 Uninstall_Discriminants_And_Pop_Scope (E);
10385 end if;
10387 -- For a record type, deal with variant parts. This has to be delayed
10388 -- to this point, because of the issue of statically predicated
10389 -- subtypes, which we have to ensure are frozen before checking
10390 -- choices, since we need to have the static choice list set.
10392 if Is_Record_Type (E) then
10393 Check_Variant_Part : declare
10394 D : constant Node_Id := Declaration_Node (E);
10395 T : Node_Id;
10396 C : Node_Id;
10397 VP : Node_Id;
10399 Others_Present : Boolean;
10400 pragma Warnings (Off, Others_Present);
10401 -- Indicates others present, not used in this case
10403 procedure Non_Static_Choice_Error (Choice : Node_Id);
10404 -- Error routine invoked by the generic instantiation below when
10405 -- the variant part has a non static choice.
10407 procedure Process_Declarations (Variant : Node_Id);
10408 -- Processes declarations associated with a variant. We analyzed
10409 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10410 -- but we still need the recursive call to Check_Choices for any
10411 -- nested variant to get its choices properly processed. This is
10412 -- also where we expand out the choices if expansion is active.
10414 package Variant_Choices_Processing is new
10415 Generic_Check_Choices
10416 (Process_Empty_Choice => No_OP,
10417 Process_Non_Static_Choice => Non_Static_Choice_Error,
10418 Process_Associated_Node => Process_Declarations);
10419 use Variant_Choices_Processing;
10421 -----------------------------
10422 -- Non_Static_Choice_Error --
10423 -----------------------------
10425 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10426 begin
10427 Flag_Non_Static_Expr
10428 ("choice given in variant part is not static!", Choice);
10429 end Non_Static_Choice_Error;
10431 --------------------------
10432 -- Process_Declarations --
10433 --------------------------
10435 procedure Process_Declarations (Variant : Node_Id) is
10436 CL : constant Node_Id := Component_List (Variant);
10437 VP : Node_Id;
10439 begin
10440 -- Check for static predicate present in this variant
10442 if Has_SP_Choice (Variant) then
10444 -- Here we expand. You might expect to find this call in
10445 -- Expand_N_Variant_Part, but that is called when we first
10446 -- see the variant part, and we cannot do this expansion
10447 -- earlier than the freeze point, since for statically
10448 -- predicated subtypes, the predicate is not known till
10449 -- the freeze point.
10451 -- Furthermore, we do this expansion even if the expander
10452 -- is not active, because other semantic processing, e.g.
10453 -- for aggregates, requires the expanded list of choices.
10455 -- If the expander is not active, then we can't just clobber
10456 -- the list since it would invalidate the ASIS -gnatct tree.
10457 -- So we have to rewrite the variant part with a Rewrite
10458 -- call that replaces it with a copy and clobber the copy.
10460 if not Expander_Active then
10461 declare
10462 NewV : constant Node_Id := New_Copy (Variant);
10463 begin
10464 Set_Discrete_Choices
10465 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10466 Rewrite (Variant, NewV);
10467 end;
10468 end if;
10470 Expand_Static_Predicates_In_Choices (Variant);
10471 end if;
10473 -- We don't need to worry about the declarations in the variant
10474 -- (since they were analyzed by Analyze_Choices when we first
10475 -- encountered the variant), but we do need to take care of
10476 -- expansion of any nested variants.
10478 if not Null_Present (CL) then
10479 VP := Variant_Part (CL);
10481 if Present (VP) then
10482 Check_Choices
10483 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10484 end if;
10485 end if;
10486 end Process_Declarations;
10488 -- Start of processing for Check_Variant_Part
10490 begin
10491 -- Find component list
10493 C := Empty;
10495 if Nkind (D) = N_Full_Type_Declaration then
10496 T := Type_Definition (D);
10498 if Nkind (T) = N_Record_Definition then
10499 C := Component_List (T);
10501 elsif Nkind (T) = N_Derived_Type_Definition
10502 and then Present (Record_Extension_Part (T))
10503 then
10504 C := Component_List (Record_Extension_Part (T));
10505 end if;
10506 end if;
10508 -- Case of variant part present
10510 if Present (C) and then Present (Variant_Part (C)) then
10511 VP := Variant_Part (C);
10513 -- Check choices
10515 Check_Choices
10516 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10518 -- If the last variant does not contain the Others choice,
10519 -- replace it with an N_Others_Choice node since Gigi always
10520 -- wants an Others. Note that we do not bother to call Analyze
10521 -- on the modified variant part, since its only effect would be
10522 -- to compute the Others_Discrete_Choices node laboriously, and
10523 -- of course we already know the list of choices corresponding
10524 -- to the others choice (it's the list we're replacing).
10526 -- We only want to do this if the expander is active, since
10527 -- we do not want to clobber the ASIS tree.
10529 if Expander_Active then
10530 declare
10531 Last_Var : constant Node_Id :=
10532 Last_Non_Pragma (Variants (VP));
10534 Others_Node : Node_Id;
10536 begin
10537 if Nkind (First (Discrete_Choices (Last_Var))) /=
10538 N_Others_Choice
10539 then
10540 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10541 Set_Others_Discrete_Choices
10542 (Others_Node, Discrete_Choices (Last_Var));
10543 Set_Discrete_Choices
10544 (Last_Var, New_List (Others_Node));
10545 end if;
10546 end;
10547 end if;
10548 end if;
10549 end Check_Variant_Part;
10550 end if;
10551 end Freeze_Entity_Checks;
10553 -------------------------
10554 -- Get_Alignment_Value --
10555 -------------------------
10557 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10558 Align : constant Uint := Static_Integer (Expr);
10560 begin
10561 if Align = No_Uint then
10562 return No_Uint;
10564 elsif Align <= 0 then
10565 Error_Msg_N ("alignment value must be positive", Expr);
10566 return No_Uint;
10568 else
10569 for J in Int range 0 .. 64 loop
10570 declare
10571 M : constant Uint := Uint_2 ** J;
10573 begin
10574 exit when M = Align;
10576 if M > Align then
10577 Error_Msg_N
10578 ("alignment value must be power of 2", Expr);
10579 return No_Uint;
10580 end if;
10581 end;
10582 end loop;
10584 return Align;
10585 end if;
10586 end Get_Alignment_Value;
10588 -------------------------------------
10589 -- Inherit_Aspects_At_Freeze_Point --
10590 -------------------------------------
10592 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10593 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10594 (Rep_Item : Node_Id) return Boolean;
10595 -- This routine checks if Rep_Item is either a pragma or an aspect
10596 -- specification node whose correponding pragma (if any) is present in
10597 -- the Rep Item chain of the entity it has been specified to.
10599 --------------------------------------------------
10600 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10601 --------------------------------------------------
10603 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10604 (Rep_Item : Node_Id) return Boolean
10606 begin
10607 return
10608 Nkind (Rep_Item) = N_Pragma
10609 or else Present_In_Rep_Item
10610 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10611 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10613 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10615 begin
10616 -- A representation item is either subtype-specific (Size and Alignment
10617 -- clauses) or type-related (all others). Subtype-specific aspects may
10618 -- differ for different subtypes of the same type (RM 13.1.8).
10620 -- A derived type inherits each type-related representation aspect of
10621 -- its parent type that was directly specified before the declaration of
10622 -- the derived type (RM 13.1.15).
10624 -- A derived subtype inherits each subtype-specific representation
10625 -- aspect of its parent subtype that was directly specified before the
10626 -- declaration of the derived type (RM 13.1.15).
10628 -- The general processing involves inheriting a representation aspect
10629 -- from a parent type whenever the first rep item (aspect specification,
10630 -- attribute definition clause, pragma) corresponding to the given
10631 -- representation aspect in the rep item chain of Typ, if any, isn't
10632 -- directly specified to Typ but to one of its parents.
10634 -- ??? Note that, for now, just a limited number of representation
10635 -- aspects have been inherited here so far. Many of them are
10636 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
10637 -- a non- exhaustive list of aspects that likely also need to
10638 -- be moved to this routine: Alignment, Component_Alignment,
10639 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
10640 -- Preelaborable_Initialization, RM_Size and Small.
10642 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
10643 return;
10644 end if;
10646 -- Ada_05/Ada_2005
10648 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
10649 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
10650 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10651 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
10652 then
10653 Set_Is_Ada_2005_Only (Typ);
10654 end if;
10656 -- Ada_12/Ada_2012
10658 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
10659 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
10660 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10661 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
10662 then
10663 Set_Is_Ada_2012_Only (Typ);
10664 end if;
10666 -- Atomic/Shared
10668 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
10669 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
10670 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10671 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
10672 then
10673 Set_Is_Atomic (Typ);
10674 Set_Treat_As_Volatile (Typ);
10675 Set_Is_Volatile (Typ);
10676 end if;
10678 -- Default_Component_Value
10680 if Is_Array_Type (Typ)
10681 and then Is_Base_Type (Typ)
10682 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
10683 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
10684 then
10685 Set_Default_Aspect_Component_Value (Typ,
10686 Default_Aspect_Component_Value
10687 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
10688 end if;
10690 -- Default_Value
10692 if Is_Scalar_Type (Typ)
10693 and then Is_Base_Type (Typ)
10694 and then Has_Rep_Item (Typ, Name_Default_Value, False)
10695 and then Has_Rep_Item (Typ, Name_Default_Value)
10696 then
10697 Set_Default_Aspect_Value (Typ,
10698 Default_Aspect_Value
10699 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
10700 end if;
10702 -- Discard_Names
10704 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
10705 and then Has_Rep_Item (Typ, Name_Discard_Names)
10706 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10707 (Get_Rep_Item (Typ, Name_Discard_Names))
10708 then
10709 Set_Discard_Names (Typ);
10710 end if;
10712 -- Invariants
10714 if not Has_Rep_Item (Typ, Name_Invariant, False)
10715 and then Has_Rep_Item (Typ, Name_Invariant)
10716 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10717 (Get_Rep_Item (Typ, Name_Invariant))
10718 then
10719 Set_Has_Invariants (Typ);
10721 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
10722 Set_Has_Inheritable_Invariants (Typ);
10723 end if;
10725 -- If we have a subtype with invariants, whose base type does not have
10726 -- invariants, copy these invariants to the base type. This happens for
10727 -- the case of implicit base types created for scalar and array types.
10729 elsif Has_Invariants (Typ)
10730 and then not Has_Invariants (Base_Type (Typ))
10731 then
10732 Set_Has_Invariants (Base_Type (Typ));
10733 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
10734 end if;
10736 -- Volatile
10738 if not Has_Rep_Item (Typ, Name_Volatile, False)
10739 and then Has_Rep_Item (Typ, Name_Volatile)
10740 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10741 (Get_Rep_Item (Typ, Name_Volatile))
10742 then
10743 Set_Treat_As_Volatile (Typ);
10744 Set_Is_Volatile (Typ);
10745 end if;
10747 -- Inheritance for derived types only
10749 if Is_Derived_Type (Typ) then
10750 declare
10751 Bas_Typ : constant Entity_Id := Base_Type (Typ);
10752 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
10754 begin
10755 -- Atomic_Components
10757 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
10758 and then Has_Rep_Item (Typ, Name_Atomic_Components)
10759 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10760 (Get_Rep_Item (Typ, Name_Atomic_Components))
10761 then
10762 Set_Has_Atomic_Components (Imp_Bas_Typ);
10763 end if;
10765 -- Volatile_Components
10767 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
10768 and then Has_Rep_Item (Typ, Name_Volatile_Components)
10769 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10770 (Get_Rep_Item (Typ, Name_Volatile_Components))
10771 then
10772 Set_Has_Volatile_Components (Imp_Bas_Typ);
10773 end if;
10775 -- Finalize_Storage_Only.
10777 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
10778 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
10779 then
10780 Set_Finalize_Storage_Only (Bas_Typ);
10781 end if;
10783 -- Universal_Aliasing
10785 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
10786 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
10787 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10788 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
10789 then
10790 Set_Universal_Aliasing (Imp_Bas_Typ);
10791 end if;
10793 -- Record type specific aspects
10795 if Is_Record_Type (Typ) then
10797 -- Bit_Order
10799 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
10800 and then Has_Rep_Item (Typ, Name_Bit_Order)
10801 then
10802 Set_Reverse_Bit_Order (Bas_Typ,
10803 Reverse_Bit_Order (Entity (Name
10804 (Get_Rep_Item (Typ, Name_Bit_Order)))));
10805 end if;
10807 -- Scalar_Storage_Order
10809 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
10810 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
10811 then
10812 Set_Reverse_Storage_Order (Bas_Typ,
10813 Reverse_Storage_Order (Entity (Name
10814 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
10816 -- Clear default SSO indications, since the inherited aspect
10817 -- which was set explicitly overrides the default.
10819 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
10820 Set_SSO_Set_High_By_Default (Bas_Typ, False);
10821 end if;
10822 end if;
10823 end;
10824 end if;
10825 end Inherit_Aspects_At_Freeze_Point;
10827 ----------------
10828 -- Initialize --
10829 ----------------
10831 procedure Initialize is
10832 begin
10833 Address_Clause_Checks.Init;
10834 Independence_Checks.Init;
10835 Unchecked_Conversions.Init;
10836 end Initialize;
10838 ---------------------------
10839 -- Install_Discriminants --
10840 ---------------------------
10842 procedure Install_Discriminants (E : Entity_Id) is
10843 Disc : Entity_Id;
10844 Prev : Entity_Id;
10845 begin
10846 Disc := First_Discriminant (E);
10847 while Present (Disc) loop
10848 Prev := Current_Entity (Disc);
10849 Set_Current_Entity (Disc);
10850 Set_Is_Immediately_Visible (Disc);
10851 Set_Homonym (Disc, Prev);
10852 Next_Discriminant (Disc);
10853 end loop;
10854 end Install_Discriminants;
10856 -------------------------
10857 -- Is_Operational_Item --
10858 -------------------------
10860 function Is_Operational_Item (N : Node_Id) return Boolean is
10861 begin
10862 if Nkind (N) /= N_Attribute_Definition_Clause then
10863 return False;
10865 else
10866 declare
10867 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
10868 begin
10869 return Id = Attribute_Input
10870 or else Id = Attribute_Output
10871 or else Id = Attribute_Read
10872 or else Id = Attribute_Write
10873 or else Id = Attribute_External_Tag;
10874 end;
10875 end if;
10876 end Is_Operational_Item;
10878 -------------------------
10879 -- Is_Predicate_Static --
10880 -------------------------
10882 -- Note: the basic legality of the expression has already been checked, so
10883 -- we don't need to worry about cases or ranges on strings for example.
10885 function Is_Predicate_Static
10886 (Expr : Node_Id;
10887 Nam : Name_Id) return Boolean
10889 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
10890 -- Given a list of case expression alternatives, returns True if all
10891 -- the alternatives are static (have all static choices, and a static
10892 -- expression).
10894 function All_Static_Choices (L : List_Id) return Boolean;
10895 -- Returns true if all elements of the list are OK static choices
10896 -- as defined below for Is_Static_Choice. Used for case expression
10897 -- alternatives and for the right operand of a membership test. An
10898 -- others_choice is static if the corresponding expression is static.
10899 -- The staticness of the bounds is checked separately.
10901 function Is_Static_Choice (N : Node_Id) return Boolean;
10902 -- Returns True if N represents a static choice (static subtype, or
10903 -- static subtype indication, or static expression, or static range).
10905 -- Note that this is a bit more inclusive than we actually need
10906 -- (in particular membership tests do not allow the use of subtype
10907 -- indications). But that doesn't matter, we have already checked
10908 -- that the construct is legal to get this far.
10910 function Is_Type_Ref (N : Node_Id) return Boolean;
10911 pragma Inline (Is_Type_Ref);
10912 -- Returns True if N is a reference to the type for the predicate in the
10913 -- expression (i.e. if it is an identifier whose Chars field matches the
10914 -- Nam given in the call). N must not be parenthesized, if the type name
10915 -- appears in parens, this routine will return False.
10917 ----------------------------------
10918 -- All_Static_Case_Alternatives --
10919 ----------------------------------
10921 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
10922 N : Node_Id;
10924 begin
10925 N := First (L);
10926 while Present (N) loop
10927 if not (All_Static_Choices (Discrete_Choices (N))
10928 and then Is_OK_Static_Expression (Expression (N)))
10929 then
10930 return False;
10931 end if;
10933 Next (N);
10934 end loop;
10936 return True;
10937 end All_Static_Case_Alternatives;
10939 ------------------------
10940 -- All_Static_Choices --
10941 ------------------------
10943 function All_Static_Choices (L : List_Id) return Boolean is
10944 N : Node_Id;
10946 begin
10947 N := First (L);
10948 while Present (N) loop
10949 if not Is_Static_Choice (N) then
10950 return False;
10951 end if;
10953 Next (N);
10954 end loop;
10956 return True;
10957 end All_Static_Choices;
10959 ----------------------
10960 -- Is_Static_Choice --
10961 ----------------------
10963 function Is_Static_Choice (N : Node_Id) return Boolean is
10964 begin
10965 return Nkind (N) = N_Others_Choice
10966 or else Is_OK_Static_Expression (N)
10967 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
10968 and then Is_OK_Static_Subtype (Entity (N)))
10969 or else (Nkind (N) = N_Subtype_Indication
10970 and then Is_OK_Static_Subtype (Entity (N)))
10971 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
10972 end Is_Static_Choice;
10974 -----------------
10975 -- Is_Type_Ref --
10976 -----------------
10978 function Is_Type_Ref (N : Node_Id) return Boolean is
10979 begin
10980 return Nkind (N) = N_Identifier
10981 and then Chars (N) = Nam
10982 and then Paren_Count (N) = 0;
10983 end Is_Type_Ref;
10985 -- Start of processing for Is_Predicate_Static
10987 begin
10988 -- Predicate_Static means one of the following holds. Numbers are the
10989 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
10991 -- 16: A static expression
10993 if Is_OK_Static_Expression (Expr) then
10994 return True;
10996 -- 17: A membership test whose simple_expression is the current
10997 -- instance, and whose membership_choice_list meets the requirements
10998 -- for a static membership test.
11000 elsif Nkind (Expr) in N_Membership_Test
11001 and then ((Present (Right_Opnd (Expr))
11002 and then Is_Static_Choice (Right_Opnd (Expr)))
11003 or else
11004 (Present (Alternatives (Expr))
11005 and then All_Static_Choices (Alternatives (Expr))))
11006 then
11007 return True;
11009 -- 18. A case_expression whose selecting_expression is the current
11010 -- instance, and whose dependent expressions are static expressions.
11012 elsif Nkind (Expr) = N_Case_Expression
11013 and then Is_Type_Ref (Expression (Expr))
11014 and then All_Static_Case_Alternatives (Alternatives (Expr))
11015 then
11016 return True;
11018 -- 19. A call to a predefined equality or ordering operator, where one
11019 -- operand is the current instance, and the other is a static
11020 -- expression.
11022 -- Note: the RM is clearly wrong here in not excluding string types.
11023 -- Without this exclusion, we would allow expressions like X > "ABC"
11024 -- to be considered as predicate-static, which is clearly not intended,
11025 -- since the idea is for predicate-static to be a subset of normal
11026 -- static expressions (and "DEF" > "ABC" is not a static expression).
11028 -- However, we do allow internally generated (not from source) equality
11029 -- and inequality operations to be valid on strings (this helps deal
11030 -- with cases where we transform A in "ABC" to A = "ABC).
11032 elsif Nkind (Expr) in N_Op_Compare
11033 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11034 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11035 and then not Comes_From_Source (Expr)))
11036 and then ((Is_Type_Ref (Left_Opnd (Expr))
11037 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11038 or else
11039 (Is_Type_Ref (Right_Opnd (Expr))
11040 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11041 then
11042 return True;
11044 -- 20. A call to a predefined boolean logical operator, where each
11045 -- operand is predicate-static.
11047 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11048 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11049 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11050 or else
11051 (Nkind (Expr) = N_Op_Not
11052 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11053 then
11054 return True;
11056 -- 21. A short-circuit control form where both operands are
11057 -- predicate-static.
11059 elsif Nkind (Expr) in N_Short_Circuit
11060 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11061 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11062 then
11063 return True;
11065 -- 22. A parenthesized predicate-static expression. This does not
11066 -- require any special test, since we just ignore paren levels in
11067 -- all the cases above.
11069 -- One more test that is an implementation artifact caused by the fact
11070 -- that we are analyzing not the original expression, but the generated
11071 -- expression in the body of the predicate function. This can include
11072 -- references to inherited predicates, so that the expression we are
11073 -- processing looks like:
11075 -- expression and then xxPredicate (typ (Inns))
11077 -- Where the call is to a Predicate function for an inherited predicate.
11078 -- We simply ignore such a call (which could be to either a dynamic or
11079 -- a static predicate, but remember that we can have a Static_Predicate
11080 -- for a non-static subtype).
11082 elsif Nkind (Expr) = N_Function_Call
11083 and then Is_Predicate_Function (Entity (Name (Expr)))
11084 then
11085 return True;
11087 -- That's an exhaustive list of tests, all other cases are not
11088 -- predicate-static, so we return False.
11090 else
11091 return False;
11092 end if;
11093 end Is_Predicate_Static;
11095 ---------------------
11096 -- Kill_Rep_Clause --
11097 ---------------------
11099 procedure Kill_Rep_Clause (N : Node_Id) is
11100 begin
11101 pragma Assert (Ignore_Rep_Clauses);
11103 -- Note: we use Replace rather than Rewrite, because we don't want
11104 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11105 -- rep clause that is being replaced.
11107 Replace (N, Make_Null_Statement (Sloc (N)));
11109 -- The null statement must be marked as not coming from source. This is
11110 -- so that ASIS ignores it, and also the back end does not expect bogus
11111 -- "from source" null statements in weird places (e.g. in declarative
11112 -- regions where such null statements are not allowed).
11114 Set_Comes_From_Source (N, False);
11115 end Kill_Rep_Clause;
11117 ------------------
11118 -- Minimum_Size --
11119 ------------------
11121 function Minimum_Size
11122 (T : Entity_Id;
11123 Biased : Boolean := False) return Nat
11125 Lo : Uint := No_Uint;
11126 Hi : Uint := No_Uint;
11127 LoR : Ureal := No_Ureal;
11128 HiR : Ureal := No_Ureal;
11129 LoSet : Boolean := False;
11130 HiSet : Boolean := False;
11131 B : Uint;
11132 S : Nat;
11133 Ancest : Entity_Id;
11134 R_Typ : constant Entity_Id := Root_Type (T);
11136 begin
11137 -- If bad type, return 0
11139 if T = Any_Type then
11140 return 0;
11142 -- For generic types, just return zero. There cannot be any legitimate
11143 -- need to know such a size, but this routine may be called with a
11144 -- generic type as part of normal processing.
11146 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11147 return 0;
11149 -- Access types (cannot have size smaller than System.Address)
11151 elsif Is_Access_Type (T) then
11152 return System_Address_Size;
11154 -- Floating-point types
11156 elsif Is_Floating_Point_Type (T) then
11157 return UI_To_Int (Esize (R_Typ));
11159 -- Discrete types
11161 elsif Is_Discrete_Type (T) then
11163 -- The following loop is looking for the nearest compile time known
11164 -- bounds following the ancestor subtype chain. The idea is to find
11165 -- the most restrictive known bounds information.
11167 Ancest := T;
11168 loop
11169 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11170 return 0;
11171 end if;
11173 if not LoSet then
11174 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11175 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11176 LoSet := True;
11177 exit when HiSet;
11178 end if;
11179 end if;
11181 if not HiSet then
11182 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11183 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11184 HiSet := True;
11185 exit when LoSet;
11186 end if;
11187 end if;
11189 Ancest := Ancestor_Subtype (Ancest);
11191 if No (Ancest) then
11192 Ancest := Base_Type (T);
11194 if Is_Generic_Type (Ancest) then
11195 return 0;
11196 end if;
11197 end if;
11198 end loop;
11200 -- Fixed-point types. We can't simply use Expr_Value to get the
11201 -- Corresponding_Integer_Value values of the bounds, since these do not
11202 -- get set till the type is frozen, and this routine can be called
11203 -- before the type is frozen. Similarly the test for bounds being static
11204 -- needs to include the case where we have unanalyzed real literals for
11205 -- the same reason.
11207 elsif Is_Fixed_Point_Type (T) then
11209 -- The following loop is looking for the nearest compile time known
11210 -- bounds following the ancestor subtype chain. The idea is to find
11211 -- the most restrictive known bounds information.
11213 Ancest := T;
11214 loop
11215 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11216 return 0;
11217 end if;
11219 -- Note: In the following two tests for LoSet and HiSet, it may
11220 -- seem redundant to test for N_Real_Literal here since normally
11221 -- one would assume that the test for the value being known at
11222 -- compile time includes this case. However, there is a glitch.
11223 -- If the real literal comes from folding a non-static expression,
11224 -- then we don't consider any non- static expression to be known
11225 -- at compile time if we are in configurable run time mode (needed
11226 -- in some cases to give a clearer definition of what is and what
11227 -- is not accepted). So the test is indeed needed. Without it, we
11228 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11230 if not LoSet then
11231 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11232 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11233 then
11234 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11235 LoSet := True;
11236 exit when HiSet;
11237 end if;
11238 end if;
11240 if not HiSet then
11241 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11242 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11243 then
11244 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11245 HiSet := True;
11246 exit when LoSet;
11247 end if;
11248 end if;
11250 Ancest := Ancestor_Subtype (Ancest);
11252 if No (Ancest) then
11253 Ancest := Base_Type (T);
11255 if Is_Generic_Type (Ancest) then
11256 return 0;
11257 end if;
11258 end if;
11259 end loop;
11261 Lo := UR_To_Uint (LoR / Small_Value (T));
11262 Hi := UR_To_Uint (HiR / Small_Value (T));
11264 -- No other types allowed
11266 else
11267 raise Program_Error;
11268 end if;
11270 -- Fall through with Hi and Lo set. Deal with biased case
11272 if (Biased
11273 and then not Is_Fixed_Point_Type (T)
11274 and then not (Is_Enumeration_Type (T)
11275 and then Has_Non_Standard_Rep (T)))
11276 or else Has_Biased_Representation (T)
11277 then
11278 Hi := Hi - Lo;
11279 Lo := Uint_0;
11280 end if;
11282 -- Signed case. Note that we consider types like range 1 .. -1 to be
11283 -- signed for the purpose of computing the size, since the bounds have
11284 -- to be accommodated in the base type.
11286 if Lo < 0 or else Hi < 0 then
11287 S := 1;
11288 B := Uint_1;
11290 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11291 -- Note that we accommodate the case where the bounds cross. This
11292 -- can happen either because of the way the bounds are declared
11293 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11295 while Lo < -B
11296 or else Hi < -B
11297 or else Lo >= B
11298 or else Hi >= B
11299 loop
11300 B := Uint_2 ** S;
11301 S := S + 1;
11302 end loop;
11304 -- Unsigned case
11306 else
11307 -- If both bounds are positive, make sure that both are represen-
11308 -- table in the case where the bounds are crossed. This can happen
11309 -- either because of the way the bounds are declared, or because of
11310 -- the algorithm in Freeze_Fixed_Point_Type.
11312 if Lo > Hi then
11313 Hi := Lo;
11314 end if;
11316 -- S = size, (can accommodate 0 .. (2**size - 1))
11318 S := 0;
11319 while Hi >= Uint_2 ** S loop
11320 S := S + 1;
11321 end loop;
11322 end if;
11324 return S;
11325 end Minimum_Size;
11327 ---------------------------
11328 -- New_Stream_Subprogram --
11329 ---------------------------
11331 procedure New_Stream_Subprogram
11332 (N : Node_Id;
11333 Ent : Entity_Id;
11334 Subp : Entity_Id;
11335 Nam : TSS_Name_Type)
11337 Loc : constant Source_Ptr := Sloc (N);
11338 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11339 Subp_Id : Entity_Id;
11340 Subp_Decl : Node_Id;
11341 F : Entity_Id;
11342 Etyp : Entity_Id;
11344 Defer_Declaration : constant Boolean :=
11345 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11346 -- For a tagged type, there is a declaration for each stream attribute
11347 -- at the freeze point, and we must generate only a completion of this
11348 -- declaration. We do the same for private types, because the full view
11349 -- might be tagged. Otherwise we generate a declaration at the point of
11350 -- the attribute definition clause.
11352 function Build_Spec return Node_Id;
11353 -- Used for declaration and renaming declaration, so that this is
11354 -- treated as a renaming_as_body.
11356 ----------------
11357 -- Build_Spec --
11358 ----------------
11360 function Build_Spec return Node_Id is
11361 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11362 Formals : List_Id;
11363 Spec : Node_Id;
11364 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11366 begin
11367 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11369 -- S : access Root_Stream_Type'Class
11371 Formals := New_List (
11372 Make_Parameter_Specification (Loc,
11373 Defining_Identifier =>
11374 Make_Defining_Identifier (Loc, Name_S),
11375 Parameter_Type =>
11376 Make_Access_Definition (Loc,
11377 Subtype_Mark =>
11378 New_Occurrence_Of (
11379 Designated_Type (Etype (F)), Loc))));
11381 if Nam = TSS_Stream_Input then
11382 Spec :=
11383 Make_Function_Specification (Loc,
11384 Defining_Unit_Name => Subp_Id,
11385 Parameter_Specifications => Formals,
11386 Result_Definition => T_Ref);
11387 else
11388 -- V : [out] T
11390 Append_To (Formals,
11391 Make_Parameter_Specification (Loc,
11392 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11393 Out_Present => Out_P,
11394 Parameter_Type => T_Ref));
11396 Spec :=
11397 Make_Procedure_Specification (Loc,
11398 Defining_Unit_Name => Subp_Id,
11399 Parameter_Specifications => Formals);
11400 end if;
11402 return Spec;
11403 end Build_Spec;
11405 -- Start of processing for New_Stream_Subprogram
11407 begin
11408 F := First_Formal (Subp);
11410 if Ekind (Subp) = E_Procedure then
11411 Etyp := Etype (Next_Formal (F));
11412 else
11413 Etyp := Etype (Subp);
11414 end if;
11416 -- Prepare subprogram declaration and insert it as an action on the
11417 -- clause node. The visibility for this entity is used to test for
11418 -- visibility of the attribute definition clause (in the sense of
11419 -- 8.3(23) as amended by AI-195).
11421 if not Defer_Declaration then
11422 Subp_Decl :=
11423 Make_Subprogram_Declaration (Loc,
11424 Specification => Build_Spec);
11426 -- For a tagged type, there is always a visible declaration for each
11427 -- stream TSS (it is a predefined primitive operation), and the
11428 -- completion of this declaration occurs at the freeze point, which is
11429 -- not always visible at places where the attribute definition clause is
11430 -- visible. So, we create a dummy entity here for the purpose of
11431 -- tracking the visibility of the attribute definition clause itself.
11433 else
11434 Subp_Id :=
11435 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11436 Subp_Decl :=
11437 Make_Object_Declaration (Loc,
11438 Defining_Identifier => Subp_Id,
11439 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11440 end if;
11442 Insert_Action (N, Subp_Decl);
11443 Set_Entity (N, Subp_Id);
11445 Subp_Decl :=
11446 Make_Subprogram_Renaming_Declaration (Loc,
11447 Specification => Build_Spec,
11448 Name => New_Occurrence_Of (Subp, Loc));
11450 if Defer_Declaration then
11451 Set_TSS (Base_Type (Ent), Subp_Id);
11452 else
11453 Insert_Action (N, Subp_Decl);
11454 Copy_TSS (Subp_Id, Base_Type (Ent));
11455 end if;
11456 end New_Stream_Subprogram;
11458 ------------------------------------------
11459 -- Push_Scope_And_Install_Discriminants --
11460 ------------------------------------------
11462 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11463 begin
11464 if Has_Discriminants (E) then
11465 Push_Scope (E);
11467 -- Make discriminants visible for type declarations and protected
11468 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11470 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11471 Install_Discriminants (E);
11472 end if;
11473 end if;
11474 end Push_Scope_And_Install_Discriminants;
11476 ------------------------
11477 -- Rep_Item_Too_Early --
11478 ------------------------
11480 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
11481 begin
11482 -- Cannot apply non-operational rep items to generic types
11484 if Is_Operational_Item (N) then
11485 return False;
11487 elsif Is_Type (T)
11488 and then Is_Generic_Type (Root_Type (T))
11489 then
11490 Error_Msg_N ("representation item not allowed for generic type", N);
11491 return True;
11492 end if;
11494 -- Otherwise check for incomplete type
11496 if Is_Incomplete_Or_Private_Type (T)
11497 and then No (Underlying_Type (T))
11498 and then
11499 (Nkind (N) /= N_Pragma
11500 or else Get_Pragma_Id (N) /= Pragma_Import)
11501 then
11502 Error_Msg_N
11503 ("representation item must be after full type declaration", N);
11504 return True;
11506 -- If the type has incomplete components, a representation clause is
11507 -- illegal but stream attributes and Convention pragmas are correct.
11509 elsif Has_Private_Component (T) then
11510 if Nkind (N) = N_Pragma then
11511 return False;
11513 else
11514 Error_Msg_N
11515 ("representation item must appear after type is fully defined",
11517 return True;
11518 end if;
11519 else
11520 return False;
11521 end if;
11522 end Rep_Item_Too_Early;
11524 -----------------------
11525 -- Rep_Item_Too_Late --
11526 -----------------------
11528 function Rep_Item_Too_Late
11529 (T : Entity_Id;
11530 N : Node_Id;
11531 FOnly : Boolean := False) return Boolean
11533 S : Entity_Id;
11534 Parent_Type : Entity_Id;
11536 procedure No_Type_Rep_Item;
11537 -- Output message indicating that no type-related aspects can be
11538 -- specified due to some property of the parent type.
11540 procedure Too_Late;
11541 -- Output message for an aspect being specified too late
11543 -- Note that neither of the above errors is considered a serious one,
11544 -- since the effect is simply that we ignore the representation clause
11545 -- in these cases.
11546 -- Is this really true? In any case if we make this change we must
11547 -- document the requirement in the spec of Rep_Item_Too_Late that
11548 -- if True is returned, then the rep item must be completely ignored???
11550 ----------------------
11551 -- No_Type_Rep_Item --
11552 ----------------------
11554 procedure No_Type_Rep_Item is
11555 begin
11556 Error_Msg_N ("|type-related representation item not permitted!", N);
11557 end No_Type_Rep_Item;
11559 --------------
11560 -- Too_Late --
11561 --------------
11563 procedure Too_Late is
11564 begin
11565 -- Other compilers seem more relaxed about rep items appearing too
11566 -- late. Since analysis tools typically don't care about rep items
11567 -- anyway, no reason to be too strict about this.
11569 if not Relaxed_RM_Semantics then
11570 Error_Msg_N ("|representation item appears too late!", N);
11571 end if;
11572 end Too_Late;
11574 -- Start of processing for Rep_Item_Too_Late
11576 begin
11577 -- First make sure entity is not frozen (RM 13.1(9))
11579 if Is_Frozen (T)
11581 -- Exclude imported types, which may be frozen if they appear in a
11582 -- representation clause for a local type.
11584 and then not From_Limited_With (T)
11586 -- Exclude generated entities (not coming from source). The common
11587 -- case is when we generate a renaming which prematurely freezes the
11588 -- renamed internal entity, but we still want to be able to set copies
11589 -- of attribute values such as Size/Alignment.
11591 and then Comes_From_Source (T)
11592 then
11593 Too_Late;
11594 S := First_Subtype (T);
11596 if Present (Freeze_Node (S)) then
11597 if not Relaxed_RM_Semantics then
11598 Error_Msg_NE
11599 ("??no more representation items for }", Freeze_Node (S), S);
11600 end if;
11601 end if;
11603 return True;
11605 -- Check for case of untagged derived type whose parent either has
11606 -- primitive operations, or is a by reference type (RM 13.1(10)). In
11607 -- this case we do not output a Too_Late message, since there is no
11608 -- earlier point where the rep item could be placed to make it legal.
11610 elsif Is_Type (T)
11611 and then not FOnly
11612 and then Is_Derived_Type (T)
11613 and then not Is_Tagged_Type (T)
11614 then
11615 Parent_Type := Etype (Base_Type (T));
11617 if Has_Primitive_Operations (Parent_Type) then
11618 No_Type_Rep_Item;
11620 if not Relaxed_RM_Semantics then
11621 Error_Msg_NE
11622 ("\parent type & has primitive operations!", N, Parent_Type);
11623 end if;
11625 return True;
11627 elsif Is_By_Reference_Type (Parent_Type) then
11628 No_Type_Rep_Item;
11630 if not Relaxed_RM_Semantics then
11631 Error_Msg_NE
11632 ("\parent type & is a by reference type!", N, Parent_Type);
11633 end if;
11635 return True;
11636 end if;
11637 end if;
11639 -- No error, but one more warning to consider. The RM (surprisingly)
11640 -- allows this pattern:
11642 -- type S is ...
11643 -- primitive operations for S
11644 -- type R is new S;
11645 -- rep clause for S
11647 -- Meaning that calls on the primitive operations of S for values of
11648 -- type R may require possibly expensive implicit conversion operations.
11649 -- This is not an error, but is worth a warning.
11651 if not Relaxed_RM_Semantics and then Is_Type (T) then
11652 declare
11653 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
11655 begin
11656 if Present (DTL)
11657 and then Has_Primitive_Operations (Base_Type (T))
11659 -- For now, do not generate this warning for the case of aspect
11660 -- specification using Ada 2012 syntax, since we get wrong
11661 -- messages we do not understand. The whole business of derived
11662 -- types and rep items seems a bit confused when aspects are
11663 -- used, since the aspects are not evaluated till freeze time.
11665 and then not From_Aspect_Specification (N)
11666 then
11667 Error_Msg_Sloc := Sloc (DTL);
11668 Error_Msg_N
11669 ("representation item for& appears after derived type "
11670 & "declaration#??", N);
11671 Error_Msg_NE
11672 ("\may result in implicit conversions for primitive "
11673 & "operations of&??", N, T);
11674 Error_Msg_NE
11675 ("\to change representations when called with arguments "
11676 & "of type&??", N, DTL);
11677 end if;
11678 end;
11679 end if;
11681 -- No error, link item into head of chain of rep items for the entity,
11682 -- but avoid chaining if we have an overloadable entity, and the pragma
11683 -- is one that can apply to multiple overloaded entities.
11685 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
11686 declare
11687 Pname : constant Name_Id := Pragma_Name (N);
11688 begin
11689 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
11690 Name_External, Name_Interface)
11691 then
11692 return False;
11693 end if;
11694 end;
11695 end if;
11697 Record_Rep_Item (T, N);
11698 return False;
11699 end Rep_Item_Too_Late;
11701 -------------------------------------
11702 -- Replace_Type_References_Generic --
11703 -------------------------------------
11705 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
11706 TName : constant Name_Id := Chars (T);
11708 function Replace_Node (N : Node_Id) return Traverse_Result;
11709 -- Processes a single node in the traversal procedure below, checking
11710 -- if node N should be replaced, and if so, doing the replacement.
11712 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
11713 -- This instantiation provides the body of Replace_Type_References
11715 ------------------
11716 -- Replace_Node --
11717 ------------------
11719 function Replace_Node (N : Node_Id) return Traverse_Result is
11720 S : Entity_Id;
11721 P : Node_Id;
11723 begin
11724 -- Case of identifier
11726 if Nkind (N) = N_Identifier then
11728 -- If not the type name, check whether it is a reference to
11729 -- some other type, which must be frozen before the predicate
11730 -- function is analyzed, i.e. before the freeze node of the
11731 -- type to which the predicate applies.
11733 if Chars (N) /= TName then
11734 if Present (Current_Entity (N))
11735 and then Is_Type (Current_Entity (N))
11736 then
11737 Freeze_Before (Freeze_Node (T), Current_Entity (N));
11738 end if;
11740 return Skip;
11742 -- Otherwise do the replacement and we are done with this node
11744 else
11745 Replace_Type_Reference (N);
11746 return Skip;
11747 end if;
11749 -- Case of selected component (which is what a qualification
11750 -- looks like in the unanalyzed tree, which is what we have.
11752 elsif Nkind (N) = N_Selected_Component then
11754 -- If selector name is not our type, keeping going (we might
11755 -- still have an occurrence of the type in the prefix).
11757 if Nkind (Selector_Name (N)) /= N_Identifier
11758 or else Chars (Selector_Name (N)) /= TName
11759 then
11760 return OK;
11762 -- Selector name is our type, check qualification
11764 else
11765 -- Loop through scopes and prefixes, doing comparison
11767 S := Current_Scope;
11768 P := Prefix (N);
11769 loop
11770 -- Continue if no more scopes or scope with no name
11772 if No (S) or else Nkind (S) not in N_Has_Chars then
11773 return OK;
11774 end if;
11776 -- Do replace if prefix is an identifier matching the
11777 -- scope that we are currently looking at.
11779 if Nkind (P) = N_Identifier
11780 and then Chars (P) = Chars (S)
11781 then
11782 Replace_Type_Reference (N);
11783 return Skip;
11784 end if;
11786 -- Go check scope above us if prefix is itself of the
11787 -- form of a selected component, whose selector matches
11788 -- the scope we are currently looking at.
11790 if Nkind (P) = N_Selected_Component
11791 and then Nkind (Selector_Name (P)) = N_Identifier
11792 and then Chars (Selector_Name (P)) = Chars (S)
11793 then
11794 S := Scope (S);
11795 P := Prefix (P);
11797 -- For anything else, we don't have a match, so keep on
11798 -- going, there are still some weird cases where we may
11799 -- still have a replacement within the prefix.
11801 else
11802 return OK;
11803 end if;
11804 end loop;
11805 end if;
11807 -- Continue for any other node kind
11809 else
11810 return OK;
11811 end if;
11812 end Replace_Node;
11814 begin
11815 Replace_Type_Refs (N);
11816 end Replace_Type_References_Generic;
11818 -------------------------
11819 -- Same_Representation --
11820 -------------------------
11822 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
11823 T1 : constant Entity_Id := Underlying_Type (Typ1);
11824 T2 : constant Entity_Id := Underlying_Type (Typ2);
11826 begin
11827 -- A quick check, if base types are the same, then we definitely have
11828 -- the same representation, because the subtype specific representation
11829 -- attributes (Size and Alignment) do not affect representation from
11830 -- the point of view of this test.
11832 if Base_Type (T1) = Base_Type (T2) then
11833 return True;
11835 elsif Is_Private_Type (Base_Type (T2))
11836 and then Base_Type (T1) = Full_View (Base_Type (T2))
11837 then
11838 return True;
11839 end if;
11841 -- Tagged types never have differing representations
11843 if Is_Tagged_Type (T1) then
11844 return True;
11845 end if;
11847 -- Representations are definitely different if conventions differ
11849 if Convention (T1) /= Convention (T2) then
11850 return False;
11851 end if;
11853 -- Representations are different if component alignments or scalar
11854 -- storage orders differ.
11856 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
11857 and then
11858 (Is_Record_Type (T2) or else Is_Array_Type (T2))
11859 and then
11860 (Component_Alignment (T1) /= Component_Alignment (T2)
11861 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
11862 then
11863 return False;
11864 end if;
11866 -- For arrays, the only real issue is component size. If we know the
11867 -- component size for both arrays, and it is the same, then that's
11868 -- good enough to know we don't have a change of representation.
11870 if Is_Array_Type (T1) then
11871 if Known_Component_Size (T1)
11872 and then Known_Component_Size (T2)
11873 and then Component_Size (T1) = Component_Size (T2)
11874 then
11875 if VM_Target = No_VM then
11876 return True;
11878 -- In VM targets the representation of arrays with aliased
11879 -- components differs from arrays with non-aliased components
11881 else
11882 return Has_Aliased_Components (Base_Type (T1))
11884 Has_Aliased_Components (Base_Type (T2));
11885 end if;
11886 end if;
11887 end if;
11889 -- Types definitely have same representation if neither has non-standard
11890 -- representation since default representations are always consistent.
11891 -- If only one has non-standard representation, and the other does not,
11892 -- then we consider that they do not have the same representation. They
11893 -- might, but there is no way of telling early enough.
11895 if Has_Non_Standard_Rep (T1) then
11896 if not Has_Non_Standard_Rep (T2) then
11897 return False;
11898 end if;
11899 else
11900 return not Has_Non_Standard_Rep (T2);
11901 end if;
11903 -- Here the two types both have non-standard representation, and we need
11904 -- to determine if they have the same non-standard representation.
11906 -- For arrays, we simply need to test if the component sizes are the
11907 -- same. Pragma Pack is reflected in modified component sizes, so this
11908 -- check also deals with pragma Pack.
11910 if Is_Array_Type (T1) then
11911 return Component_Size (T1) = Component_Size (T2);
11913 -- Tagged types always have the same representation, because it is not
11914 -- possible to specify different representations for common fields.
11916 elsif Is_Tagged_Type (T1) then
11917 return True;
11919 -- Case of record types
11921 elsif Is_Record_Type (T1) then
11923 -- Packed status must conform
11925 if Is_Packed (T1) /= Is_Packed (T2) then
11926 return False;
11928 -- Otherwise we must check components. Typ2 maybe a constrained
11929 -- subtype with fewer components, so we compare the components
11930 -- of the base types.
11932 else
11933 Record_Case : declare
11934 CD1, CD2 : Entity_Id;
11936 function Same_Rep return Boolean;
11937 -- CD1 and CD2 are either components or discriminants. This
11938 -- function tests whether they have the same representation.
11940 --------------
11941 -- Same_Rep --
11942 --------------
11944 function Same_Rep return Boolean is
11945 begin
11946 if No (Component_Clause (CD1)) then
11947 return No (Component_Clause (CD2));
11948 else
11949 -- Note: at this point, component clauses have been
11950 -- normalized to the default bit order, so that the
11951 -- comparison of Component_Bit_Offsets is meaningful.
11953 return
11954 Present (Component_Clause (CD2))
11955 and then
11956 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
11957 and then
11958 Esize (CD1) = Esize (CD2);
11959 end if;
11960 end Same_Rep;
11962 -- Start of processing for Record_Case
11964 begin
11965 if Has_Discriminants (T1) then
11967 -- The number of discriminants may be different if the
11968 -- derived type has fewer (constrained by values). The
11969 -- invisible discriminants retain the representation of
11970 -- the original, so the discrepancy does not per se
11971 -- indicate a different representation.
11973 CD1 := First_Discriminant (T1);
11974 CD2 := First_Discriminant (T2);
11975 while Present (CD1) and then Present (CD2) loop
11976 if not Same_Rep then
11977 return False;
11978 else
11979 Next_Discriminant (CD1);
11980 Next_Discriminant (CD2);
11981 end if;
11982 end loop;
11983 end if;
11985 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
11986 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
11987 while Present (CD1) loop
11988 if not Same_Rep then
11989 return False;
11990 else
11991 Next_Component (CD1);
11992 Next_Component (CD2);
11993 end if;
11994 end loop;
11996 return True;
11997 end Record_Case;
11998 end if;
12000 -- For enumeration types, we must check each literal to see if the
12001 -- representation is the same. Note that we do not permit enumeration
12002 -- representation clauses for Character and Wide_Character, so these
12003 -- cases were already dealt with.
12005 elsif Is_Enumeration_Type (T1) then
12006 Enumeration_Case : declare
12007 L1, L2 : Entity_Id;
12009 begin
12010 L1 := First_Literal (T1);
12011 L2 := First_Literal (T2);
12012 while Present (L1) loop
12013 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12014 return False;
12015 else
12016 Next_Literal (L1);
12017 Next_Literal (L2);
12018 end if;
12019 end loop;
12021 return True;
12022 end Enumeration_Case;
12024 -- Any other types have the same representation for these purposes
12026 else
12027 return True;
12028 end if;
12029 end Same_Representation;
12031 --------------------------------
12032 -- Resolve_Iterable_Operation --
12033 --------------------------------
12035 procedure Resolve_Iterable_Operation
12036 (N : Node_Id;
12037 Cursor : Entity_Id;
12038 Typ : Entity_Id;
12039 Nam : Name_Id)
12041 Ent : Entity_Id;
12042 F1 : Entity_Id;
12043 F2 : Entity_Id;
12045 begin
12046 if not Is_Overloaded (N) then
12047 if not Is_Entity_Name (N)
12048 or else Ekind (Entity (N)) /= E_Function
12049 or else Scope (Entity (N)) /= Scope (Typ)
12050 or else No (First_Formal (Entity (N)))
12051 or else Etype (First_Formal (Entity (N))) /= Typ
12052 then
12053 Error_Msg_N ("iterable primitive must be local function name "
12054 & "whose first formal is an iterable type", N);
12055 return;
12056 end if;
12058 Ent := Entity (N);
12059 F1 := First_Formal (Ent);
12060 if Nam = Name_First then
12062 -- First (Container) => Cursor
12064 if Etype (Ent) /= Cursor then
12065 Error_Msg_N ("primitive for First must yield a curosr", N);
12066 end if;
12068 elsif Nam = Name_Next then
12070 -- Next (Container, Cursor) => Cursor
12072 F2 := Next_Formal (F1);
12074 if Etype (F2) /= Cursor
12075 or else Etype (Ent) /= Cursor
12076 or else Present (Next_Formal (F2))
12077 then
12078 Error_Msg_N ("no match for Next iterable primitive", N);
12079 end if;
12081 elsif Nam = Name_Has_Element then
12083 -- Has_Element (Container, Cursor) => Boolean
12085 F2 := Next_Formal (F1);
12086 if Etype (F2) /= Cursor
12087 or else Etype (Ent) /= Standard_Boolean
12088 or else Present (Next_Formal (F2))
12089 then
12090 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12091 end if;
12093 elsif Nam = Name_Element then
12094 F2 := Next_Formal (F1);
12096 if No (F2)
12097 or else Etype (F2) /= Cursor
12098 or else Present (Next_Formal (F2))
12099 then
12100 Error_Msg_N ("no match for Element iterable primitive", N);
12101 end if;
12102 null;
12104 else
12105 raise Program_Error;
12106 end if;
12108 else
12109 -- Overloaded case: find subprogram with proper signature.
12110 -- Caller will report error if no match is found.
12112 declare
12113 I : Interp_Index;
12114 It : Interp;
12116 begin
12117 Get_First_Interp (N, I, It);
12118 while Present (It.Typ) loop
12119 if Ekind (It.Nam) = E_Function
12120 and then Scope (It.Nam) = Scope (Typ)
12121 and then Etype (First_Formal (It.Nam)) = Typ
12122 then
12123 F1 := First_Formal (It.Nam);
12125 if Nam = Name_First then
12126 if Etype (It.Nam) = Cursor
12127 and then No (Next_Formal (F1))
12128 then
12129 Set_Entity (N, It.Nam);
12130 exit;
12131 end if;
12133 elsif Nam = Name_Next then
12134 F2 := Next_Formal (F1);
12136 if Present (F2)
12137 and then No (Next_Formal (F2))
12138 and then Etype (F2) = Cursor
12139 and then Etype (It.Nam) = Cursor
12140 then
12141 Set_Entity (N, It.Nam);
12142 exit;
12143 end if;
12145 elsif Nam = Name_Has_Element then
12146 F2 := Next_Formal (F1);
12148 if Present (F2)
12149 and then No (Next_Formal (F2))
12150 and then Etype (F2) = Cursor
12151 and then Etype (It.Nam) = Standard_Boolean
12152 then
12153 Set_Entity (N, It.Nam);
12154 F2 := Next_Formal (F1);
12155 exit;
12156 end if;
12158 elsif Nam = Name_Element then
12159 F2 := Next_Formal (F1);
12161 if Present (F2)
12162 and then No (Next_Formal (F2))
12163 and then Etype (F2) = Cursor
12164 then
12165 Set_Entity (N, It.Nam);
12166 exit;
12167 end if;
12168 end if;
12169 end if;
12171 Get_Next_Interp (I, It);
12172 end loop;
12173 end;
12174 end if;
12175 end Resolve_Iterable_Operation;
12177 ----------------
12178 -- Set_Biased --
12179 ----------------
12181 procedure Set_Biased
12182 (E : Entity_Id;
12183 N : Node_Id;
12184 Msg : String;
12185 Biased : Boolean := True)
12187 begin
12188 if Biased then
12189 Set_Has_Biased_Representation (E);
12191 if Warn_On_Biased_Representation then
12192 Error_Msg_NE
12193 ("?B?" & Msg & " forces biased representation for&", N, E);
12194 end if;
12195 end if;
12196 end Set_Biased;
12198 --------------------
12199 -- Set_Enum_Esize --
12200 --------------------
12202 procedure Set_Enum_Esize (T : Entity_Id) is
12203 Lo : Uint;
12204 Hi : Uint;
12205 Sz : Nat;
12207 begin
12208 Init_Alignment (T);
12210 -- Find the minimum standard size (8,16,32,64) that fits
12212 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12213 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12215 if Lo < 0 then
12216 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12217 Sz := Standard_Character_Size; -- May be > 8 on some targets
12219 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12220 Sz := 16;
12222 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12223 Sz := 32;
12225 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12226 Sz := 64;
12227 end if;
12229 else
12230 if Hi < Uint_2**08 then
12231 Sz := Standard_Character_Size; -- May be > 8 on some targets
12233 elsif Hi < Uint_2**16 then
12234 Sz := 16;
12236 elsif Hi < Uint_2**32 then
12237 Sz := 32;
12239 else pragma Assert (Hi < Uint_2**63);
12240 Sz := 64;
12241 end if;
12242 end if;
12244 -- That minimum is the proper size unless we have a foreign convention
12245 -- and the size required is 32 or less, in which case we bump the size
12246 -- up to 32. This is required for C and C++ and seems reasonable for
12247 -- all other foreign conventions.
12249 if Has_Foreign_Convention (T)
12250 and then Esize (T) < Standard_Integer_Size
12252 -- Don't do this if Short_Enums on target
12254 and then not Target_Short_Enums
12255 then
12256 Init_Esize (T, Standard_Integer_Size);
12257 else
12258 Init_Esize (T, Sz);
12259 end if;
12260 end Set_Enum_Esize;
12262 -----------------------------
12263 -- Uninstall_Discriminants --
12264 -----------------------------
12266 procedure Uninstall_Discriminants (E : Entity_Id) is
12267 Disc : Entity_Id;
12268 Prev : Entity_Id;
12269 Outer : Entity_Id;
12271 begin
12272 -- Discriminants have been made visible for type declarations and
12273 -- protected type declarations, not for subtype declarations.
12275 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12276 Disc := First_Discriminant (E);
12277 while Present (Disc) loop
12278 if Disc /= Current_Entity (Disc) then
12279 Prev := Current_Entity (Disc);
12280 while Present (Prev)
12281 and then Present (Homonym (Prev))
12282 and then Homonym (Prev) /= Disc
12283 loop
12284 Prev := Homonym (Prev);
12285 end loop;
12286 else
12287 Prev := Empty;
12288 end if;
12290 Set_Is_Immediately_Visible (Disc, False);
12292 Outer := Homonym (Disc);
12293 while Present (Outer) and then Scope (Outer) = E loop
12294 Outer := Homonym (Outer);
12295 end loop;
12297 -- Reset homonym link of other entities, but do not modify link
12298 -- between entities in current scope, so that the back-end can
12299 -- have a proper count of local overloadings.
12301 if No (Prev) then
12302 Set_Name_Entity_Id (Chars (Disc), Outer);
12304 elsif Scope (Prev) /= Scope (Disc) then
12305 Set_Homonym (Prev, Outer);
12306 end if;
12308 Next_Discriminant (Disc);
12309 end loop;
12310 end if;
12311 end Uninstall_Discriminants;
12313 -------------------------------------------
12314 -- Uninstall_Discriminants_And_Pop_Scope --
12315 -------------------------------------------
12317 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12318 begin
12319 if Has_Discriminants (E) then
12320 Uninstall_Discriminants (E);
12321 Pop_Scope;
12322 end if;
12323 end Uninstall_Discriminants_And_Pop_Scope;
12325 ------------------------------
12326 -- Validate_Address_Clauses --
12327 ------------------------------
12329 procedure Validate_Address_Clauses is
12330 begin
12331 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12332 declare
12333 ACCR : Address_Clause_Check_Record
12334 renames Address_Clause_Checks.Table (J);
12336 Expr : Node_Id;
12338 X_Alignment : Uint;
12339 Y_Alignment : Uint;
12341 X_Size : Uint;
12342 Y_Size : Uint;
12344 begin
12345 -- Skip processing of this entry if warning already posted
12347 if not Address_Warning_Posted (ACCR.N) then
12348 Expr := Original_Node (Expression (ACCR.N));
12350 -- Get alignments
12352 X_Alignment := Alignment (ACCR.X);
12353 Y_Alignment := Alignment (ACCR.Y);
12355 -- Similarly obtain sizes
12357 X_Size := Esize (ACCR.X);
12358 Y_Size := Esize (ACCR.Y);
12360 -- Check for large object overlaying smaller one
12362 if Y_Size > Uint_0
12363 and then X_Size > Uint_0
12364 and then X_Size > Y_Size
12365 then
12366 Error_Msg_NE
12367 ("??& overlays smaller object", ACCR.N, ACCR.X);
12368 Error_Msg_N
12369 ("\??program execution may be erroneous", ACCR.N);
12370 Error_Msg_Uint_1 := X_Size;
12371 Error_Msg_NE
12372 ("\??size of & is ^", ACCR.N, ACCR.X);
12373 Error_Msg_Uint_1 := Y_Size;
12374 Error_Msg_NE
12375 ("\??size of & is ^", ACCR.N, ACCR.Y);
12377 -- Check for inadequate alignment, both of the base object
12378 -- and of the offset, if any.
12380 -- Note: we do not check the alignment if we gave a size
12381 -- warning, since it would likely be redundant.
12383 elsif Y_Alignment /= Uint_0
12384 and then (Y_Alignment < X_Alignment
12385 or else (ACCR.Off
12386 and then
12387 Nkind (Expr) = N_Attribute_Reference
12388 and then
12389 Attribute_Name (Expr) = Name_Address
12390 and then
12391 Has_Compatible_Alignment
12392 (ACCR.X, Prefix (Expr))
12393 /= Known_Compatible))
12394 then
12395 Error_Msg_NE
12396 ("??specified address for& may be inconsistent "
12397 & "with alignment", ACCR.N, ACCR.X);
12398 Error_Msg_N
12399 ("\??program execution may be erroneous (RM 13.3(27))",
12400 ACCR.N);
12401 Error_Msg_Uint_1 := X_Alignment;
12402 Error_Msg_NE
12403 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12404 Error_Msg_Uint_1 := Y_Alignment;
12405 Error_Msg_NE
12406 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12407 if Y_Alignment >= X_Alignment then
12408 Error_Msg_N
12409 ("\??but offset is not multiple of alignment", ACCR.N);
12410 end if;
12411 end if;
12412 end if;
12413 end;
12414 end loop;
12415 end Validate_Address_Clauses;
12417 ---------------------------
12418 -- Validate_Independence --
12419 ---------------------------
12421 procedure Validate_Independence is
12422 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12423 N : Node_Id;
12424 E : Entity_Id;
12425 IC : Boolean;
12426 Comp : Entity_Id;
12427 Addr : Node_Id;
12428 P : Node_Id;
12430 procedure Check_Array_Type (Atyp : Entity_Id);
12431 -- Checks if the array type Atyp has independent components, and
12432 -- if not, outputs an appropriate set of error messages.
12434 procedure No_Independence;
12435 -- Output message that independence cannot be guaranteed
12437 function OK_Component (C : Entity_Id) return Boolean;
12438 -- Checks one component to see if it is independently accessible, and
12439 -- if so yields True, otherwise yields False if independent access
12440 -- cannot be guaranteed. This is a conservative routine, it only
12441 -- returns True if it knows for sure, it returns False if it knows
12442 -- there is a problem, or it cannot be sure there is no problem.
12444 procedure Reason_Bad_Component (C : Entity_Id);
12445 -- Outputs continuation message if a reason can be determined for
12446 -- the component C being bad.
12448 ----------------------
12449 -- Check_Array_Type --
12450 ----------------------
12452 procedure Check_Array_Type (Atyp : Entity_Id) is
12453 Ctyp : constant Entity_Id := Component_Type (Atyp);
12455 begin
12456 -- OK if no alignment clause, no pack, and no component size
12458 if not Has_Component_Size_Clause (Atyp)
12459 and then not Has_Alignment_Clause (Atyp)
12460 and then not Is_Packed (Atyp)
12461 then
12462 return;
12463 end if;
12465 -- Case of component size is greater than or equal to 64 and the
12466 -- alignment of the array is at least as large as the alignment
12467 -- of the component. We are definitely OK in this situation.
12469 if Known_Component_Size (Atyp)
12470 and then Component_Size (Atyp) >= 64
12471 and then Known_Alignment (Atyp)
12472 and then Known_Alignment (Ctyp)
12473 and then Alignment (Atyp) >= Alignment (Ctyp)
12474 then
12475 return;
12476 end if;
12478 -- Check actual component size
12480 if not Known_Component_Size (Atyp)
12481 or else not (Addressable (Component_Size (Atyp))
12482 and then Component_Size (Atyp) < 64)
12483 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12484 then
12485 No_Independence;
12487 -- Bad component size, check reason
12489 if Has_Component_Size_Clause (Atyp) then
12490 P := Get_Attribute_Definition_Clause
12491 (Atyp, Attribute_Component_Size);
12493 if Present (P) then
12494 Error_Msg_Sloc := Sloc (P);
12495 Error_Msg_N ("\because of Component_Size clause#", N);
12496 return;
12497 end if;
12498 end if;
12500 if Is_Packed (Atyp) then
12501 P := Get_Rep_Pragma (Atyp, Name_Pack);
12503 if Present (P) then
12504 Error_Msg_Sloc := Sloc (P);
12505 Error_Msg_N ("\because of pragma Pack#", N);
12506 return;
12507 end if;
12508 end if;
12510 -- No reason found, just return
12512 return;
12513 end if;
12515 -- Array type is OK independence-wise
12517 return;
12518 end Check_Array_Type;
12520 ---------------------
12521 -- No_Independence --
12522 ---------------------
12524 procedure No_Independence is
12525 begin
12526 if Pragma_Name (N) = Name_Independent then
12527 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
12528 else
12529 Error_Msg_NE
12530 ("independent components cannot be guaranteed for&", N, E);
12531 end if;
12532 end No_Independence;
12534 ------------------
12535 -- OK_Component --
12536 ------------------
12538 function OK_Component (C : Entity_Id) return Boolean is
12539 Rec : constant Entity_Id := Scope (C);
12540 Ctyp : constant Entity_Id := Etype (C);
12542 begin
12543 -- OK if no component clause, no Pack, and no alignment clause
12545 if No (Component_Clause (C))
12546 and then not Is_Packed (Rec)
12547 and then not Has_Alignment_Clause (Rec)
12548 then
12549 return True;
12550 end if;
12552 -- Here we look at the actual component layout. A component is
12553 -- addressable if its size is a multiple of the Esize of the
12554 -- component type, and its starting position in the record has
12555 -- appropriate alignment, and the record itself has appropriate
12556 -- alignment to guarantee the component alignment.
12558 -- Make sure sizes are static, always assume the worst for any
12559 -- cases where we cannot check static values.
12561 if not (Known_Static_Esize (C)
12562 and then
12563 Known_Static_Esize (Ctyp))
12564 then
12565 return False;
12566 end if;
12568 -- Size of component must be addressable or greater than 64 bits
12569 -- and a multiple of bytes.
12571 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
12572 return False;
12573 end if;
12575 -- Check size is proper multiple
12577 if Esize (C) mod Esize (Ctyp) /= 0 then
12578 return False;
12579 end if;
12581 -- Check alignment of component is OK
12583 if not Known_Component_Bit_Offset (C)
12584 or else Component_Bit_Offset (C) < Uint_0
12585 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
12586 then
12587 return False;
12588 end if;
12590 -- Check alignment of record type is OK
12592 if not Known_Alignment (Rec)
12593 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12594 then
12595 return False;
12596 end if;
12598 -- All tests passed, component is addressable
12600 return True;
12601 end OK_Component;
12603 --------------------------
12604 -- Reason_Bad_Component --
12605 --------------------------
12607 procedure Reason_Bad_Component (C : Entity_Id) is
12608 Rec : constant Entity_Id := Scope (C);
12609 Ctyp : constant Entity_Id := Etype (C);
12611 begin
12612 -- If component clause present assume that's the problem
12614 if Present (Component_Clause (C)) then
12615 Error_Msg_Sloc := Sloc (Component_Clause (C));
12616 Error_Msg_N ("\because of Component_Clause#", N);
12617 return;
12618 end if;
12620 -- If pragma Pack clause present, assume that's the problem
12622 if Is_Packed (Rec) then
12623 P := Get_Rep_Pragma (Rec, Name_Pack);
12625 if Present (P) then
12626 Error_Msg_Sloc := Sloc (P);
12627 Error_Msg_N ("\because of pragma Pack#", N);
12628 return;
12629 end if;
12630 end if;
12632 -- See if record has bad alignment clause
12634 if Has_Alignment_Clause (Rec)
12635 and then Known_Alignment (Rec)
12636 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12637 then
12638 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
12640 if Present (P) then
12641 Error_Msg_Sloc := Sloc (P);
12642 Error_Msg_N ("\because of Alignment clause#", N);
12643 end if;
12644 end if;
12646 -- Couldn't find a reason, so return without a message
12648 return;
12649 end Reason_Bad_Component;
12651 -- Start of processing for Validate_Independence
12653 begin
12654 for J in Independence_Checks.First .. Independence_Checks.Last loop
12655 N := Independence_Checks.Table (J).N;
12656 E := Independence_Checks.Table (J).E;
12657 IC := Pragma_Name (N) = Name_Independent_Components;
12659 -- Deal with component case
12661 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
12662 if not OK_Component (E) then
12663 No_Independence;
12664 Reason_Bad_Component (E);
12665 goto Continue;
12666 end if;
12667 end if;
12669 -- Deal with record with Independent_Components
12671 if IC and then Is_Record_Type (E) then
12672 Comp := First_Component_Or_Discriminant (E);
12673 while Present (Comp) loop
12674 if not OK_Component (Comp) then
12675 No_Independence;
12676 Reason_Bad_Component (Comp);
12677 goto Continue;
12678 end if;
12680 Next_Component_Or_Discriminant (Comp);
12681 end loop;
12682 end if;
12684 -- Deal with address clause case
12686 if Is_Object (E) then
12687 Addr := Address_Clause (E);
12689 if Present (Addr) then
12690 No_Independence;
12691 Error_Msg_Sloc := Sloc (Addr);
12692 Error_Msg_N ("\because of Address clause#", N);
12693 goto Continue;
12694 end if;
12695 end if;
12697 -- Deal with independent components for array type
12699 if IC and then Is_Array_Type (E) then
12700 Check_Array_Type (E);
12701 end if;
12703 -- Deal with independent components for array object
12705 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
12706 Check_Array_Type (Etype (E));
12707 end if;
12709 <<Continue>> null;
12710 end loop;
12711 end Validate_Independence;
12713 ------------------------------
12714 -- Validate_Iterable_Aspect --
12715 ------------------------------
12717 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
12718 Assoc : Node_Id;
12719 Expr : Node_Id;
12721 Prim : Node_Id;
12722 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
12724 First_Id : Entity_Id;
12725 Next_Id : Entity_Id;
12726 Has_Element_Id : Entity_Id;
12727 Element_Id : Entity_Id;
12729 begin
12730 -- If previous error aspect is unusable
12732 if Cursor = Any_Type then
12733 return;
12734 end if;
12736 First_Id := Empty;
12737 Next_Id := Empty;
12738 Has_Element_Id := Empty;
12739 Element_Id := Empty;
12741 -- Each expression must resolve to a function with the proper signature
12743 Assoc := First (Component_Associations (Expression (ASN)));
12744 while Present (Assoc) loop
12745 Expr := Expression (Assoc);
12746 Analyze (Expr);
12748 Prim := First (Choices (Assoc));
12750 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
12751 Error_Msg_N ("illegal name in association", Prim);
12753 elsif Chars (Prim) = Name_First then
12754 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
12755 First_Id := Entity (Expr);
12757 elsif Chars (Prim) = Name_Next then
12758 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
12759 Next_Id := Entity (Expr);
12761 elsif Chars (Prim) = Name_Has_Element then
12762 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
12763 Has_Element_Id := Entity (Expr);
12765 elsif Chars (Prim) = Name_Element then
12766 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
12767 Element_Id := Entity (Expr);
12769 else
12770 Error_Msg_N ("invalid name for iterable function", Prim);
12771 end if;
12773 Next (Assoc);
12774 end loop;
12776 if No (First_Id) then
12777 Error_Msg_N ("match for First primitive not found", ASN);
12779 elsif No (Next_Id) then
12780 Error_Msg_N ("match for Next primitive not found", ASN);
12782 elsif No (Has_Element_Id) then
12783 Error_Msg_N ("match for Has_Element primitive not found", ASN);
12785 elsif No (Element_Id) then
12786 null; -- Optional.
12787 end if;
12788 end Validate_Iterable_Aspect;
12790 -----------------------------------
12791 -- Validate_Unchecked_Conversion --
12792 -----------------------------------
12794 procedure Validate_Unchecked_Conversion
12795 (N : Node_Id;
12796 Act_Unit : Entity_Id)
12798 Source : Entity_Id;
12799 Target : Entity_Id;
12800 Vnode : Node_Id;
12802 begin
12803 -- Obtain source and target types. Note that we call Ancestor_Subtype
12804 -- here because the processing for generic instantiation always makes
12805 -- subtypes, and we want the original frozen actual types.
12807 -- If we are dealing with private types, then do the check on their
12808 -- fully declared counterparts if the full declarations have been
12809 -- encountered (they don't have to be visible, but they must exist).
12811 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
12813 if Is_Private_Type (Source)
12814 and then Present (Underlying_Type (Source))
12815 then
12816 Source := Underlying_Type (Source);
12817 end if;
12819 Target := Ancestor_Subtype (Etype (Act_Unit));
12821 -- If either type is generic, the instantiation happens within a generic
12822 -- unit, and there is nothing to check. The proper check will happen
12823 -- when the enclosing generic is instantiated.
12825 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
12826 return;
12827 end if;
12829 if Is_Private_Type (Target)
12830 and then Present (Underlying_Type (Target))
12831 then
12832 Target := Underlying_Type (Target);
12833 end if;
12835 -- Source may be unconstrained array, but not target
12837 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
12838 Error_Msg_N
12839 ("unchecked conversion to unconstrained array not allowed", N);
12840 return;
12841 end if;
12843 -- Warn if conversion between two different convention pointers
12845 if Is_Access_Type (Target)
12846 and then Is_Access_Type (Source)
12847 and then Convention (Target) /= Convention (Source)
12848 and then Warn_On_Unchecked_Conversion
12849 then
12850 -- Give warnings for subprogram pointers only on most targets
12852 if Is_Access_Subprogram_Type (Target)
12853 or else Is_Access_Subprogram_Type (Source)
12854 then
12855 Error_Msg_N
12856 ("?z?conversion between pointers with different conventions!",
12858 end if;
12859 end if;
12861 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
12862 -- warning when compiling GNAT-related sources.
12864 if Warn_On_Unchecked_Conversion
12865 and then not In_Predefined_Unit (N)
12866 and then RTU_Loaded (Ada_Calendar)
12867 and then (Chars (Source) = Name_Time
12868 or else
12869 Chars (Target) = Name_Time)
12870 then
12871 -- If Ada.Calendar is loaded and the name of one of the operands is
12872 -- Time, there is a good chance that this is Ada.Calendar.Time.
12874 declare
12875 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
12876 begin
12877 pragma Assert (Present (Calendar_Time));
12879 if Source = Calendar_Time or else Target = Calendar_Time then
12880 Error_Msg_N
12881 ("?z?representation of 'Time values may change between "
12882 & "'G'N'A'T versions", N);
12883 end if;
12884 end;
12885 end if;
12887 -- Make entry in unchecked conversion table for later processing by
12888 -- Validate_Unchecked_Conversions, which will check sizes and alignments
12889 -- (using values set by the back-end where possible). This is only done
12890 -- if the appropriate warning is active.
12892 if Warn_On_Unchecked_Conversion then
12893 Unchecked_Conversions.Append
12894 (New_Val => UC_Entry'(Eloc => Sloc (N),
12895 Source => Source,
12896 Target => Target,
12897 Act_Unit => Act_Unit));
12899 -- If both sizes are known statically now, then back end annotation
12900 -- is not required to do a proper check but if either size is not
12901 -- known statically, then we need the annotation.
12903 if Known_Static_RM_Size (Source)
12904 and then
12905 Known_Static_RM_Size (Target)
12906 then
12907 null;
12908 else
12909 Back_Annotate_Rep_Info := True;
12910 end if;
12911 end if;
12913 -- If unchecked conversion to access type, and access type is declared
12914 -- in the same unit as the unchecked conversion, then set the flag
12915 -- No_Strict_Aliasing (no strict aliasing is implicit here)
12917 if Is_Access_Type (Target) and then
12918 In_Same_Source_Unit (Target, N)
12919 then
12920 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
12921 end if;
12923 -- Generate N_Validate_Unchecked_Conversion node for back end in case
12924 -- the back end needs to perform special validation checks.
12926 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
12927 -- have full expansion and the back end is called ???
12929 Vnode :=
12930 Make_Validate_Unchecked_Conversion (Sloc (N));
12931 Set_Source_Type (Vnode, Source);
12932 Set_Target_Type (Vnode, Target);
12934 -- If the unchecked conversion node is in a list, just insert before it.
12935 -- If not we have some strange case, not worth bothering about.
12937 if Is_List_Member (N) then
12938 Insert_After (N, Vnode);
12939 end if;
12940 end Validate_Unchecked_Conversion;
12942 ------------------------------------
12943 -- Validate_Unchecked_Conversions --
12944 ------------------------------------
12946 procedure Validate_Unchecked_Conversions is
12947 begin
12948 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
12949 declare
12950 T : UC_Entry renames Unchecked_Conversions.Table (N);
12952 Eloc : constant Source_Ptr := T.Eloc;
12953 Source : constant Entity_Id := T.Source;
12954 Target : constant Entity_Id := T.Target;
12955 Act_Unit : constant Entity_Id := T.Act_Unit;
12957 Source_Siz : Uint;
12958 Target_Siz : Uint;
12960 begin
12961 -- Skip if function marked as warnings off
12963 if Warnings_Off (Act_Unit) then
12964 goto Continue;
12965 end if;
12967 -- This validation check, which warns if we have unequal sizes for
12968 -- unchecked conversion, and thus potentially implementation
12969 -- dependent semantics, is one of the few occasions on which we
12970 -- use the official RM size instead of Esize. See description in
12971 -- Einfo "Handling of Type'Size Values" for details.
12973 if Serious_Errors_Detected = 0
12974 and then Known_Static_RM_Size (Source)
12975 and then Known_Static_RM_Size (Target)
12977 -- Don't do the check if warnings off for either type, note the
12978 -- deliberate use of OR here instead of OR ELSE to get the flag
12979 -- Warnings_Off_Used set for both types if appropriate.
12981 and then not (Has_Warnings_Off (Source)
12983 Has_Warnings_Off (Target))
12984 then
12985 Source_Siz := RM_Size (Source);
12986 Target_Siz := RM_Size (Target);
12988 if Source_Siz /= Target_Siz then
12989 Error_Msg
12990 ("?z?types for unchecked conversion have different sizes!",
12991 Eloc);
12993 if All_Errors_Mode then
12994 Error_Msg_Name_1 := Chars (Source);
12995 Error_Msg_Uint_1 := Source_Siz;
12996 Error_Msg_Name_2 := Chars (Target);
12997 Error_Msg_Uint_2 := Target_Siz;
12998 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
13000 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13002 if Is_Discrete_Type (Source)
13003 and then
13004 Is_Discrete_Type (Target)
13005 then
13006 if Source_Siz > Target_Siz then
13007 Error_Msg
13008 ("\?z?^ high order bits of source will "
13009 & "be ignored!", Eloc);
13011 elsif Is_Unsigned_Type (Source) then
13012 Error_Msg
13013 ("\?z?source will be extended with ^ high order "
13014 & "zero bits!", Eloc);
13016 else
13017 Error_Msg
13018 ("\?z?source will be extended with ^ high order "
13019 & "sign bits!", Eloc);
13020 end if;
13022 elsif Source_Siz < Target_Siz then
13023 if Is_Discrete_Type (Target) then
13024 if Bytes_Big_Endian then
13025 Error_Msg
13026 ("\?z?target value will include ^ undefined "
13027 & "low order bits!", Eloc);
13028 else
13029 Error_Msg
13030 ("\?z?target value will include ^ undefined "
13031 & "high order bits!", Eloc);
13032 end if;
13034 else
13035 Error_Msg
13036 ("\?z?^ trailing bits of target value will be "
13037 & "undefined!", Eloc);
13038 end if;
13040 else pragma Assert (Source_Siz > Target_Siz);
13041 Error_Msg
13042 ("\?z?^ trailing bits of source will be ignored!",
13043 Eloc);
13044 end if;
13045 end if;
13046 end if;
13047 end if;
13049 -- If both types are access types, we need to check the alignment.
13050 -- If the alignment of both is specified, we can do it here.
13052 if Serious_Errors_Detected = 0
13053 and then Is_Access_Type (Source)
13054 and then Is_Access_Type (Target)
13055 and then Target_Strict_Alignment
13056 and then Present (Designated_Type (Source))
13057 and then Present (Designated_Type (Target))
13058 then
13059 declare
13060 D_Source : constant Entity_Id := Designated_Type (Source);
13061 D_Target : constant Entity_Id := Designated_Type (Target);
13063 begin
13064 if Known_Alignment (D_Source)
13065 and then
13066 Known_Alignment (D_Target)
13067 then
13068 declare
13069 Source_Align : constant Uint := Alignment (D_Source);
13070 Target_Align : constant Uint := Alignment (D_Target);
13072 begin
13073 if Source_Align < Target_Align
13074 and then not Is_Tagged_Type (D_Source)
13076 -- Suppress warning if warnings suppressed on either
13077 -- type or either designated type. Note the use of
13078 -- OR here instead of OR ELSE. That is intentional,
13079 -- we would like to set flag Warnings_Off_Used in
13080 -- all types for which warnings are suppressed.
13082 and then not (Has_Warnings_Off (D_Source)
13084 Has_Warnings_Off (D_Target)
13086 Has_Warnings_Off (Source)
13088 Has_Warnings_Off (Target))
13089 then
13090 Error_Msg_Uint_1 := Target_Align;
13091 Error_Msg_Uint_2 := Source_Align;
13092 Error_Msg_Node_1 := D_Target;
13093 Error_Msg_Node_2 := D_Source;
13094 Error_Msg
13095 ("?z?alignment of & (^) is stricter than "
13096 & "alignment of & (^)!", Eloc);
13097 Error_Msg
13098 ("\?z?resulting access value may have invalid "
13099 & "alignment!", Eloc);
13100 end if;
13101 end;
13102 end if;
13103 end;
13104 end if;
13105 end;
13107 <<Continue>>
13108 null;
13109 end loop;
13110 end Validate_Unchecked_Conversions;
13112 end Sem_Ch13;