2015-01-06 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch13.adb
blob8443daf6fcd46572737a24f1bf9b967e2ea016c8
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 freeze point.
1124 when Boolean_Aspects |
1125 Library_Unit_Aspects =>
1126 Make_Pragma_From_Boolean_Aspect (ASN);
1128 -- Special handling for aspects that don't correspond to
1129 -- pragmas/attributes.
1131 when Aspect_Default_Value |
1132 Aspect_Default_Component_Value =>
1133 Analyze_Aspect_Default_Value (ASN);
1135 -- Ditto for iterator aspects, because the corresponding
1136 -- attributes may not have been analyzed yet.
1138 when Aspect_Constant_Indexing |
1139 Aspect_Variable_Indexing |
1140 Aspect_Default_Iterator |
1141 Aspect_Iterator_Element =>
1142 Analyze (Expression (ASN));
1144 if Etype (Expression (ASN)) = Any_Type then
1145 Error_Msg_NE
1146 ("\aspect must be fully defined before & is frozen",
1147 ASN, E);
1148 end if;
1150 when Aspect_Iterable =>
1151 Validate_Iterable_Aspect (E, ASN);
1153 when others =>
1154 null;
1155 end case;
1157 Ritem := Aspect_Rep_Item (ASN);
1159 if Present (Ritem) then
1160 Analyze (Ritem);
1161 end if;
1162 end if;
1163 end if;
1165 Next_Rep_Item (ASN);
1166 end loop;
1168 -- This is where we inherit delayed rep aspects from our parent. Note
1169 -- that if we fell out of the above loop with ASN non-empty, it means
1170 -- we hit an aspect for an entity other than E, and it must be the
1171 -- type from which we were derived.
1173 if May_Inherit_Delayed_Rep_Aspects (E) then
1174 Inherit_Delayed_Rep_Aspects (ASN);
1175 end if;
1176 end Analyze_Aspects_At_Freeze_Point;
1178 -----------------------------------
1179 -- Analyze_Aspect_Specifications --
1180 -----------------------------------
1182 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1183 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1184 -- Establish linkages between an aspect and its corresponding
1185 -- pragma.
1187 procedure Insert_After_SPARK_Mode
1188 (Prag : Node_Id;
1189 Ins_Nod : Node_Id;
1190 Decls : List_Id);
1191 -- Subsidiary to the analysis of aspects Abstract_State, Ghost,
1192 -- Initializes, Initial_Condition and Refined_State. Insert node Prag
1193 -- before node Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip
1194 -- SPARK_Mode. Decls is the associated declarative list where Prag is to
1195 -- reside.
1197 procedure Insert_Pragma (Prag : Node_Id);
1198 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1199 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1200 -- Insert pragma Prag such that it mimics the placement of a source
1201 -- pragma of the same kind.
1203 -- procedure Proc (Formal : ...) with Global => ...;
1205 -- procedure Proc (Formal : ...);
1206 -- pragma Global (...);
1208 --------------
1209 -- Decorate --
1210 --------------
1212 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1213 begin
1214 Set_Aspect_Rep_Item (Asp, Prag);
1215 Set_Corresponding_Aspect (Prag, Asp);
1216 Set_From_Aspect_Specification (Prag);
1217 Set_Parent (Prag, Asp);
1218 end Decorate;
1220 -----------------------------
1221 -- Insert_After_SPARK_Mode --
1222 -----------------------------
1224 procedure Insert_After_SPARK_Mode
1225 (Prag : Node_Id;
1226 Ins_Nod : Node_Id;
1227 Decls : List_Id)
1229 Decl : Node_Id := Ins_Nod;
1231 begin
1232 -- Skip SPARK_Mode
1234 if Present (Decl)
1235 and then Nkind (Decl) = N_Pragma
1236 and then Pragma_Name (Decl) = Name_SPARK_Mode
1237 then
1238 Decl := Next (Decl);
1239 end if;
1241 if Present (Decl) then
1242 Insert_Before (Decl, Prag);
1244 -- Aitem acts as the last declaration
1246 else
1247 Append_To (Decls, Prag);
1248 end if;
1249 end Insert_After_SPARK_Mode;
1251 -------------------
1252 -- Insert_Pragma --
1253 -------------------
1255 procedure Insert_Pragma (Prag : Node_Id) is
1256 Aux : Node_Id;
1257 Decl : Node_Id;
1259 begin
1260 -- When the context is a library unit, the pragma is added to the
1261 -- Pragmas_After list.
1263 if Nkind (Parent (N)) = N_Compilation_Unit then
1264 Aux := Aux_Decls_Node (Parent (N));
1266 if No (Pragmas_After (Aux)) then
1267 Set_Pragmas_After (Aux, New_List);
1268 end if;
1270 Prepend (Prag, Pragmas_After (Aux));
1272 -- Pragmas associated with subprogram bodies are inserted in the
1273 -- declarative part.
1275 elsif Nkind (N) = N_Subprogram_Body then
1276 if Present (Declarations (N)) then
1278 -- Skip other internally generated pragmas from aspects to find
1279 -- the proper insertion point. As a result the order of pragmas
1280 -- is the same as the order of aspects.
1282 -- As precondition pragmas generated from conjuncts in the
1283 -- precondition aspect are presented in reverse order to
1284 -- Insert_Pragma, insert them in the correct order here by not
1285 -- skipping previously inserted precondition pragmas when the
1286 -- current pragma is a precondition.
1288 Decl := First (Declarations (N));
1289 while Present (Decl) loop
1290 if Nkind (Decl) = N_Pragma
1291 and then From_Aspect_Specification (Decl)
1292 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1293 and then
1294 Get_Pragma_Id (Prag) = Pragma_Precondition)
1295 then
1296 Next (Decl);
1297 else
1298 exit;
1299 end if;
1300 end loop;
1302 if Present (Decl) then
1303 Insert_Before (Decl, Prag);
1304 else
1305 Append (Prag, Declarations (N));
1306 end if;
1307 else
1308 Set_Declarations (N, New_List (Prag));
1309 end if;
1311 -- Default
1313 else
1314 Insert_After (N, Prag);
1315 end if;
1316 end Insert_Pragma;
1318 -- Local variables
1320 Aspect : Node_Id;
1321 Aitem : Node_Id;
1322 Ent : Node_Id;
1324 L : constant List_Id := Aspect_Specifications (N);
1326 Ins_Node : Node_Id := N;
1327 -- Insert pragmas/attribute definition clause after this node when no
1328 -- delayed analysis is required.
1330 -- Start of processing for Analyze_Aspect_Specifications
1332 -- The general processing involves building an attribute definition
1333 -- clause or a pragma node that corresponds to the aspect. Then in order
1334 -- to delay the evaluation of this aspect to the freeze point, we attach
1335 -- the corresponding pragma/attribute definition clause to the aspect
1336 -- specification node, which is then placed in the Rep Item chain. In
1337 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1338 -- and we evaluate the rep item at the freeze point. When the aspect
1339 -- doesn't have a corresponding pragma/attribute definition clause, then
1340 -- its analysis is simply delayed at the freeze point.
1342 -- Some special cases don't require delay analysis, thus the aspect is
1343 -- analyzed right now.
1345 -- Note that there is a special handling for Pre, Post, Test_Case,
1346 -- Contract_Cases aspects. In these cases, we do not have to worry
1347 -- about delay issues, since the pragmas themselves deal with delay
1348 -- of visibility for the expression analysis. Thus, we just insert
1349 -- the pragma after the node N.
1351 begin
1352 pragma Assert (Present (L));
1354 -- Loop through aspects
1356 Aspect := First (L);
1357 Aspect_Loop : while Present (Aspect) loop
1358 Analyze_One_Aspect : declare
1359 Expr : constant Node_Id := Expression (Aspect);
1360 Id : constant Node_Id := Identifier (Aspect);
1361 Loc : constant Source_Ptr := Sloc (Aspect);
1362 Nam : constant Name_Id := Chars (Id);
1363 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1364 Anod : Node_Id;
1366 Delay_Required : Boolean;
1367 -- Set False if delay is not required
1369 Eloc : Source_Ptr := No_Location;
1370 -- Source location of expression, modified when we split PPC's. It
1371 -- is set below when Expr is present.
1373 procedure Analyze_Aspect_External_Or_Link_Name;
1374 -- Perform analysis of the External_Name or Link_Name aspects
1376 procedure Analyze_Aspect_Implicit_Dereference;
1377 -- Perform analysis of the Implicit_Dereference aspects
1379 procedure Make_Aitem_Pragma
1380 (Pragma_Argument_Associations : List_Id;
1381 Pragma_Name : Name_Id);
1382 -- This is a wrapper for Make_Pragma used for converting aspects
1383 -- to pragmas. It takes care of Sloc (set from Loc) and building
1384 -- the pragma identifier from the given name. In addition the
1385 -- flags Class_Present and Split_PPC are set from the aspect
1386 -- node, as well as Is_Ignored. This routine also sets the
1387 -- From_Aspect_Specification in the resulting pragma node to
1388 -- True, and sets Corresponding_Aspect to point to the aspect.
1389 -- The resulting pragma is assigned to Aitem.
1391 ------------------------------------------
1392 -- Analyze_Aspect_External_Or_Link_Name --
1393 ------------------------------------------
1395 procedure Analyze_Aspect_External_Or_Link_Name is
1396 begin
1397 -- Verify that there is an Import/Export aspect defined for the
1398 -- entity. The processing of that aspect in turn checks that
1399 -- there is a Convention aspect declared. The pragma is
1400 -- constructed when processing the Convention aspect.
1402 declare
1403 A : Node_Id;
1405 begin
1406 A := First (L);
1407 while Present (A) loop
1408 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1409 Name_Import);
1410 Next (A);
1411 end loop;
1413 if No (A) then
1414 Error_Msg_N
1415 ("missing Import/Export for Link/External name",
1416 Aspect);
1417 end if;
1418 end;
1419 end Analyze_Aspect_External_Or_Link_Name;
1421 -----------------------------------------
1422 -- Analyze_Aspect_Implicit_Dereference --
1423 -----------------------------------------
1425 procedure Analyze_Aspect_Implicit_Dereference is
1426 begin
1427 if not Is_Type (E) or else not Has_Discriminants (E) then
1428 Error_Msg_N
1429 ("aspect must apply to a type with discriminants", N);
1431 else
1432 declare
1433 Disc : Entity_Id;
1435 begin
1436 Disc := First_Discriminant (E);
1437 while Present (Disc) loop
1438 if Chars (Expr) = Chars (Disc)
1439 and then Ekind (Etype (Disc)) =
1440 E_Anonymous_Access_Type
1441 then
1442 Set_Has_Implicit_Dereference (E);
1443 Set_Has_Implicit_Dereference (Disc);
1444 return;
1445 end if;
1447 Next_Discriminant (Disc);
1448 end loop;
1450 -- Error if no proper access discriminant.
1452 Error_Msg_NE
1453 ("not an access discriminant of&", Expr, E);
1454 end;
1455 end if;
1456 end Analyze_Aspect_Implicit_Dereference;
1458 -----------------------
1459 -- Make_Aitem_Pragma --
1460 -----------------------
1462 procedure Make_Aitem_Pragma
1463 (Pragma_Argument_Associations : List_Id;
1464 Pragma_Name : Name_Id)
1466 Args : List_Id := Pragma_Argument_Associations;
1468 begin
1469 -- We should never get here if aspect was disabled
1471 pragma Assert (not Is_Disabled (Aspect));
1473 -- Certain aspects allow for an optional name or expression. Do
1474 -- not generate a pragma with empty argument association list.
1476 if No (Args) or else No (Expression (First (Args))) then
1477 Args := No_List;
1478 end if;
1480 -- Build the pragma
1482 Aitem :=
1483 Make_Pragma (Loc,
1484 Pragma_Argument_Associations => Args,
1485 Pragma_Identifier =>
1486 Make_Identifier (Sloc (Id), Pragma_Name),
1487 Class_Present => Class_Present (Aspect),
1488 Split_PPC => Split_PPC (Aspect));
1490 -- Set additional semantic fields
1492 if Is_Ignored (Aspect) then
1493 Set_Is_Ignored (Aitem);
1494 elsif Is_Checked (Aspect) then
1495 Set_Is_Checked (Aitem);
1496 end if;
1498 Set_Corresponding_Aspect (Aitem, Aspect);
1499 Set_From_Aspect_Specification (Aitem, True);
1500 end Make_Aitem_Pragma;
1502 -- Start of processing for Analyze_One_Aspect
1504 begin
1505 -- Skip aspect if already analyzed, to avoid looping in some cases
1507 if Analyzed (Aspect) then
1508 goto Continue;
1509 end if;
1511 -- Skip looking at aspect if it is totally disabled. Just mark it
1512 -- as such for later reference in the tree. This also sets the
1513 -- Is_Ignored and Is_Checked flags appropriately.
1515 Check_Applicable_Policy (Aspect);
1517 if Is_Disabled (Aspect) then
1518 goto Continue;
1519 end if;
1521 -- Set the source location of expression, used in the case of
1522 -- a failed precondition/postcondition or invariant. Note that
1523 -- the source location of the expression is not usually the best
1524 -- choice here. For example, it gets located on the last AND
1525 -- keyword in a chain of boolean expressiond AND'ed together.
1526 -- It is best to put the message on the first character of the
1527 -- assertion, which is the effect of the First_Node call here.
1529 if Present (Expr) then
1530 Eloc := Sloc (First_Node (Expr));
1531 end if;
1533 -- Check restriction No_Implementation_Aspect_Specifications
1535 if Implementation_Defined_Aspect (A_Id) then
1536 Check_Restriction
1537 (No_Implementation_Aspect_Specifications, Aspect);
1538 end if;
1540 -- Check restriction No_Specification_Of_Aspect
1542 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1544 -- Mark aspect analyzed (actual analysis is delayed till later)
1546 Set_Analyzed (Aspect);
1547 Set_Entity (Aspect, E);
1548 Ent := New_Occurrence_Of (E, Sloc (Id));
1550 -- Check for duplicate aspect. Note that the Comes_From_Source
1551 -- test allows duplicate Pre/Post's that we generate internally
1552 -- to escape being flagged here.
1554 if No_Duplicates_Allowed (A_Id) then
1555 Anod := First (L);
1556 while Anod /= Aspect loop
1557 if Comes_From_Source (Aspect)
1558 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1559 then
1560 Error_Msg_Name_1 := Nam;
1561 Error_Msg_Sloc := Sloc (Anod);
1563 -- Case of same aspect specified twice
1565 if Class_Present (Anod) = Class_Present (Aspect) then
1566 if not Class_Present (Anod) then
1567 Error_Msg_NE
1568 ("aspect% for & previously given#",
1569 Id, E);
1570 else
1571 Error_Msg_NE
1572 ("aspect `%''Class` for & previously given#",
1573 Id, E);
1574 end if;
1575 end if;
1576 end if;
1578 Next (Anod);
1579 end loop;
1580 end if;
1582 -- Check some general restrictions on language defined aspects
1584 if not Implementation_Defined_Aspect (A_Id) then
1585 Error_Msg_Name_1 := Nam;
1587 -- Not allowed for renaming declarations
1589 if Nkind (N) in N_Renaming_Declaration then
1590 Error_Msg_N
1591 ("aspect % not allowed for renaming declaration",
1592 Aspect);
1593 end if;
1595 -- Not allowed for formal type declarations
1597 if Nkind (N) = N_Formal_Type_Declaration then
1598 Error_Msg_N
1599 ("aspect % not allowed for formal type declaration",
1600 Aspect);
1601 end if;
1602 end if;
1604 -- Copy expression for later processing by the procedures
1605 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1607 Set_Entity (Id, New_Copy_Tree (Expr));
1609 -- Set Delay_Required as appropriate to aspect
1611 case Aspect_Delay (A_Id) is
1612 when Always_Delay =>
1613 Delay_Required := True;
1615 when Never_Delay =>
1616 Delay_Required := False;
1618 when Rep_Aspect =>
1620 -- If expression has the form of an integer literal, then
1621 -- do not delay, since we know the value cannot change.
1622 -- This optimization catches most rep clause cases.
1624 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1625 or else (A_Id in Boolean_Aspects and then No (Expr))
1626 then
1627 Delay_Required := False;
1628 else
1629 Delay_Required := True;
1630 Set_Has_Delayed_Rep_Aspects (E);
1631 end if;
1632 end case;
1634 -- Processing based on specific aspect
1636 case A_Id is
1638 -- No_Aspect should be impossible
1640 when No_Aspect =>
1641 raise Program_Error;
1643 -- Case 1: Aspects corresponding to attribute definition
1644 -- clauses.
1646 when Aspect_Address |
1647 Aspect_Alignment |
1648 Aspect_Bit_Order |
1649 Aspect_Component_Size |
1650 Aspect_Constant_Indexing |
1651 Aspect_Default_Iterator |
1652 Aspect_Dispatching_Domain |
1653 Aspect_External_Tag |
1654 Aspect_Input |
1655 Aspect_Iterable |
1656 Aspect_Iterator_Element |
1657 Aspect_Machine_Radix |
1658 Aspect_Object_Size |
1659 Aspect_Output |
1660 Aspect_Read |
1661 Aspect_Scalar_Storage_Order |
1662 Aspect_Size |
1663 Aspect_Small |
1664 Aspect_Simple_Storage_Pool |
1665 Aspect_Storage_Pool |
1666 Aspect_Stream_Size |
1667 Aspect_Value_Size |
1668 Aspect_Variable_Indexing |
1669 Aspect_Write =>
1671 -- Indexing aspects apply only to tagged type
1673 if (A_Id = Aspect_Constant_Indexing
1674 or else
1675 A_Id = Aspect_Variable_Indexing)
1676 and then not (Is_Type (E)
1677 and then Is_Tagged_Type (E))
1678 then
1679 Error_Msg_N
1680 ("indexing aspect can only apply to a tagged type",
1681 Aspect);
1682 goto Continue;
1683 end if;
1685 -- For the case of aspect Address, we don't consider that we
1686 -- know the entity is never set in the source, since it is
1687 -- is likely aliasing is occurring.
1689 -- Note: one might think that the analysis of the resulting
1690 -- attribute definition clause would take care of that, but
1691 -- that's not the case since it won't be from source.
1693 if A_Id = Aspect_Address then
1694 Set_Never_Set_In_Source (E, False);
1695 end if;
1697 -- Correctness of the profile of a stream operation is
1698 -- verified at the freeze point, but we must detect the
1699 -- illegal specification of this aspect for a subtype now,
1700 -- to prevent malformed rep_item chains.
1702 if (A_Id = Aspect_Input or else
1703 A_Id = Aspect_Output or else
1704 A_Id = Aspect_Read or else
1705 A_Id = Aspect_Write)
1706 and not Is_First_Subtype (E)
1707 then
1708 Error_Msg_N
1709 ("local name must be a first subtype", Aspect);
1710 goto Continue;
1711 end if;
1713 -- Construct the attribute definition clause
1715 Aitem :=
1716 Make_Attribute_Definition_Clause (Loc,
1717 Name => Ent,
1718 Chars => Chars (Id),
1719 Expression => Relocate_Node (Expr));
1721 -- If the address is specified, then we treat the entity as
1722 -- referenced, to avoid spurious warnings. This is analogous
1723 -- to what is done with an attribute definition clause, but
1724 -- here we don't want to generate a reference because this
1725 -- is the point of definition of the entity.
1727 if A_Id = Aspect_Address then
1728 Set_Referenced (E);
1729 end if;
1731 -- Case 2: Aspects corresponding to pragmas
1733 -- Case 2a: Aspects corresponding to pragmas with two
1734 -- arguments, where the first argument is a local name
1735 -- referring to the entity, and the second argument is the
1736 -- aspect definition expression.
1738 -- Linker_Section/Suppress/Unsuppress
1740 when Aspect_Linker_Section |
1741 Aspect_Suppress |
1742 Aspect_Unsuppress =>
1744 Make_Aitem_Pragma
1745 (Pragma_Argument_Associations => New_List (
1746 Make_Pragma_Argument_Association (Loc,
1747 Expression => New_Occurrence_Of (E, Loc)),
1748 Make_Pragma_Argument_Association (Sloc (Expr),
1749 Expression => Relocate_Node (Expr))),
1750 Pragma_Name => Chars (Id));
1752 -- Synchronization
1754 -- Corresponds to pragma Implemented, construct the pragma
1756 when Aspect_Synchronization =>
1757 Make_Aitem_Pragma
1758 (Pragma_Argument_Associations => New_List (
1759 Make_Pragma_Argument_Association (Loc,
1760 Expression => New_Occurrence_Of (E, Loc)),
1761 Make_Pragma_Argument_Association (Sloc (Expr),
1762 Expression => Relocate_Node (Expr))),
1763 Pragma_Name => Name_Implemented);
1765 -- Attach_Handler
1767 when Aspect_Attach_Handler =>
1768 Make_Aitem_Pragma
1769 (Pragma_Argument_Associations => New_List (
1770 Make_Pragma_Argument_Association (Sloc (Ent),
1771 Expression => Ent),
1772 Make_Pragma_Argument_Association (Sloc (Expr),
1773 Expression => Relocate_Node (Expr))),
1774 Pragma_Name => Name_Attach_Handler);
1776 -- We need to insert this pragma into the tree to get proper
1777 -- processing and to look valid from a placement viewpoint.
1779 Insert_Pragma (Aitem);
1780 goto Continue;
1782 -- Dynamic_Predicate, Predicate, Static_Predicate
1784 when Aspect_Dynamic_Predicate |
1785 Aspect_Predicate |
1786 Aspect_Static_Predicate =>
1788 -- These aspects apply only to subtypes
1790 if not Is_Type (E) then
1791 Error_Msg_N
1792 ("predicate can only be specified for a subtype",
1793 Aspect);
1794 goto Continue;
1796 elsif Is_Incomplete_Type (E) then
1797 Error_Msg_N
1798 ("predicate cannot apply to incomplete view", Aspect);
1799 goto Continue;
1800 end if;
1802 -- Construct the pragma (always a pragma Predicate, with
1803 -- flags recording whether it is static/dynamic). We also
1804 -- set flags recording this in the type itself.
1806 Make_Aitem_Pragma
1807 (Pragma_Argument_Associations => New_List (
1808 Make_Pragma_Argument_Association (Sloc (Ent),
1809 Expression => Ent),
1810 Make_Pragma_Argument_Association (Sloc (Expr),
1811 Expression => Relocate_Node (Expr))),
1812 Pragma_Name => Name_Predicate);
1814 -- Mark type has predicates, and remember what kind of
1815 -- aspect lead to this predicate (we need this to access
1816 -- the right set of check policies later on).
1818 Set_Has_Predicates (E);
1820 if A_Id = Aspect_Dynamic_Predicate then
1821 Set_Has_Dynamic_Predicate_Aspect (E);
1822 elsif A_Id = Aspect_Static_Predicate then
1823 Set_Has_Static_Predicate_Aspect (E);
1824 end if;
1826 -- If the type is private, indicate that its completion
1827 -- has a freeze node, because that is the one that will
1828 -- be visible at freeze time.
1830 if Is_Private_Type (E) and then Present (Full_View (E)) then
1831 Set_Has_Predicates (Full_View (E));
1833 if A_Id = Aspect_Dynamic_Predicate then
1834 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1835 elsif A_Id = Aspect_Static_Predicate then
1836 Set_Has_Static_Predicate_Aspect (Full_View (E));
1837 end if;
1839 Set_Has_Delayed_Aspects (Full_View (E));
1840 Ensure_Freeze_Node (Full_View (E));
1841 end if;
1843 -- Case 2b: Aspects corresponding to pragmas with two
1844 -- arguments, where the second argument is a local name
1845 -- referring to the entity, and the first argument is the
1846 -- aspect definition expression.
1848 -- Convention
1850 when Aspect_Convention =>
1852 -- The aspect may be part of the specification of an import
1853 -- or export pragma. Scan the aspect list to gather the
1854 -- other components, if any. The name of the generated
1855 -- pragma is one of Convention/Import/Export.
1857 declare
1858 Args : constant List_Id := New_List (
1859 Make_Pragma_Argument_Association (Sloc (Expr),
1860 Expression => Relocate_Node (Expr)),
1861 Make_Pragma_Argument_Association (Sloc (Ent),
1862 Expression => Ent));
1864 Imp_Exp_Seen : Boolean := False;
1865 -- Flag set when aspect Import or Export has been seen
1867 Imp_Seen : Boolean := False;
1868 -- Flag set when aspect Import has been seen
1870 Asp : Node_Id;
1871 Asp_Nam : Name_Id;
1872 Extern_Arg : Node_Id;
1873 Link_Arg : Node_Id;
1874 Prag_Nam : Name_Id;
1876 begin
1877 Extern_Arg := Empty;
1878 Link_Arg := Empty;
1879 Prag_Nam := Chars (Id);
1881 Asp := First (L);
1882 while Present (Asp) loop
1883 Asp_Nam := Chars (Identifier (Asp));
1885 -- Aspects Import and Export take precedence over
1886 -- aspect Convention. As a result the generated pragma
1887 -- must carry the proper interfacing aspect's name.
1889 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1890 if Imp_Exp_Seen then
1891 Error_Msg_N ("conflicting", Asp);
1892 else
1893 Imp_Exp_Seen := True;
1895 if Asp_Nam = Name_Import then
1896 Imp_Seen := True;
1897 end if;
1898 end if;
1900 Prag_Nam := Asp_Nam;
1902 -- Aspect External_Name adds an extra argument to the
1903 -- generated pragma.
1905 elsif Asp_Nam = Name_External_Name then
1906 Extern_Arg :=
1907 Make_Pragma_Argument_Association (Loc,
1908 Chars => Asp_Nam,
1909 Expression => Relocate_Node (Expression (Asp)));
1911 -- Aspect Link_Name adds an extra argument to the
1912 -- generated pragma.
1914 elsif Asp_Nam = Name_Link_Name then
1915 Link_Arg :=
1916 Make_Pragma_Argument_Association (Loc,
1917 Chars => Asp_Nam,
1918 Expression => Relocate_Node (Expression (Asp)));
1919 end if;
1921 Next (Asp);
1922 end loop;
1924 -- Assemble the full argument list
1926 if Present (Extern_Arg) then
1927 Append_To (Args, Extern_Arg);
1928 end if;
1930 if Present (Link_Arg) then
1931 Append_To (Args, Link_Arg);
1932 end if;
1934 Make_Aitem_Pragma
1935 (Pragma_Argument_Associations => Args,
1936 Pragma_Name => Prag_Nam);
1938 -- Store the generated pragma Import in the related
1939 -- subprogram.
1941 if Imp_Seen and then Is_Subprogram (E) then
1942 Set_Import_Pragma (E, Aitem);
1943 end if;
1944 end;
1946 -- CPU, Interrupt_Priority, Priority
1948 -- These three aspects can be specified for a subprogram spec
1949 -- or body, in which case we analyze the expression and export
1950 -- the value of the aspect.
1952 -- Previously, we generated an equivalent pragma for bodies
1953 -- (note that the specs cannot contain these pragmas). The
1954 -- pragma was inserted ahead of local declarations, rather than
1955 -- after the body. This leads to a certain duplication between
1956 -- the processing performed for the aspect and the pragma, but
1957 -- given the straightforward handling required it is simpler
1958 -- to duplicate than to translate the aspect in the spec into
1959 -- a pragma in the declarative part of the body.
1961 when Aspect_CPU |
1962 Aspect_Interrupt_Priority |
1963 Aspect_Priority =>
1965 if Nkind_In (N, N_Subprogram_Body,
1966 N_Subprogram_Declaration)
1967 then
1968 -- Analyze the aspect expression
1970 Analyze_And_Resolve (Expr, Standard_Integer);
1972 -- Interrupt_Priority aspect not allowed for main
1973 -- subprograms. ARM D.1 does not forbid this explicitly,
1974 -- but ARM J.15.11 (6/3) does not permit pragma
1975 -- Interrupt_Priority for subprograms.
1977 if A_Id = Aspect_Interrupt_Priority then
1978 Error_Msg_N
1979 ("Interrupt_Priority aspect cannot apply to "
1980 & "subprogram", Expr);
1982 -- The expression must be static
1984 elsif not Is_OK_Static_Expression (Expr) then
1985 Flag_Non_Static_Expr
1986 ("aspect requires static expression!", Expr);
1988 -- Check whether this is the main subprogram. Issue a
1989 -- warning only if it is obviously not a main program
1990 -- (when it has parameters or when the subprogram is
1991 -- within a package).
1993 elsif Present (Parameter_Specifications
1994 (Specification (N)))
1995 or else not Is_Compilation_Unit (Defining_Entity (N))
1996 then
1997 -- See ARM D.1 (14/3) and D.16 (12/3)
1999 Error_Msg_N
2000 ("aspect applied to subprogram other than the "
2001 & "main subprogram has no effect??", Expr);
2003 -- Otherwise check in range and export the value
2005 -- For the CPU aspect
2007 elsif A_Id = Aspect_CPU then
2008 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2010 -- Value is correct so we export the value to make
2011 -- it available at execution time.
2013 Set_Main_CPU
2014 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2016 else
2017 Error_Msg_N
2018 ("main subprogram CPU is out of range", Expr);
2019 end if;
2021 -- For the Priority aspect
2023 elsif A_Id = Aspect_Priority then
2024 if Is_In_Range (Expr, RTE (RE_Priority)) then
2026 -- Value is correct so we export the value to make
2027 -- it available at execution time.
2029 Set_Main_Priority
2030 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2032 -- Ignore pragma if Relaxed_RM_Semantics to support
2033 -- other targets/non GNAT compilers.
2035 elsif not Relaxed_RM_Semantics then
2036 Error_Msg_N
2037 ("main subprogram priority is out of range",
2038 Expr);
2039 end if;
2040 end if;
2042 -- Load an arbitrary entity from System.Tasking.Stages
2043 -- or System.Tasking.Restricted.Stages (depending on
2044 -- the supported profile) to make sure that one of these
2045 -- packages is implicitly with'ed, since we need to have
2046 -- the tasking run time active for the pragma Priority to
2047 -- have any effect. Previously we with'ed the package
2048 -- System.Tasking, but this package does not trigger the
2049 -- required initialization of the run-time library.
2051 declare
2052 Discard : Entity_Id;
2053 begin
2054 if Restricted_Profile then
2055 Discard := RTE (RE_Activate_Restricted_Tasks);
2056 else
2057 Discard := RTE (RE_Activate_Tasks);
2058 end if;
2059 end;
2061 -- Handling for these Aspects in subprograms is complete
2063 goto Continue;
2065 -- For tasks
2067 else
2068 -- Pass the aspect as an attribute
2070 Aitem :=
2071 Make_Attribute_Definition_Clause (Loc,
2072 Name => Ent,
2073 Chars => Chars (Id),
2074 Expression => Relocate_Node (Expr));
2075 end if;
2077 -- Warnings
2079 when Aspect_Warnings =>
2080 Make_Aitem_Pragma
2081 (Pragma_Argument_Associations => New_List (
2082 Make_Pragma_Argument_Association (Sloc (Expr),
2083 Expression => Relocate_Node (Expr)),
2084 Make_Pragma_Argument_Association (Loc,
2085 Expression => New_Occurrence_Of (E, Loc))),
2086 Pragma_Name => Chars (Id));
2088 -- Case 2c: Aspects corresponding to pragmas with three
2089 -- arguments.
2091 -- Invariant aspects have a first argument that references the
2092 -- entity, a second argument that is the expression and a third
2093 -- argument that is an appropriate message.
2095 -- Invariant, Type_Invariant
2097 when Aspect_Invariant |
2098 Aspect_Type_Invariant =>
2100 -- Analysis of the pragma will verify placement legality:
2101 -- an invariant must apply to a private type, or appear in
2102 -- the private part of a spec and apply to a completion.
2104 Make_Aitem_Pragma
2105 (Pragma_Argument_Associations => New_List (
2106 Make_Pragma_Argument_Association (Sloc (Ent),
2107 Expression => Ent),
2108 Make_Pragma_Argument_Association (Sloc (Expr),
2109 Expression => Relocate_Node (Expr))),
2110 Pragma_Name => Name_Invariant);
2112 -- Add message unless exception messages are suppressed
2114 if not Opt.Exception_Locations_Suppressed then
2115 Append_To (Pragma_Argument_Associations (Aitem),
2116 Make_Pragma_Argument_Association (Eloc,
2117 Chars => Name_Message,
2118 Expression =>
2119 Make_String_Literal (Eloc,
2120 Strval => "failed invariant from "
2121 & Build_Location_String (Eloc))));
2122 end if;
2124 -- For Invariant case, insert immediately after the entity
2125 -- declaration. We do not have to worry about delay issues
2126 -- since the pragma processing takes care of this.
2128 Delay_Required := False;
2130 -- Case 2d : Aspects that correspond to a pragma with one
2131 -- argument.
2133 -- Abstract_State
2135 -- Aspect Abstract_State introduces implicit declarations for
2136 -- all state abstraction entities it defines. To emulate this
2137 -- behavior, insert the pragma at the beginning of the visible
2138 -- declarations of the related package so that it is analyzed
2139 -- immediately.
2141 when Aspect_Abstract_State => Abstract_State : declare
2142 Context : Node_Id := N;
2143 Decl : Node_Id;
2144 Decls : List_Id;
2146 begin
2147 -- When aspect Abstract_State appears on a generic package,
2148 -- it is propageted to the package instance. The context in
2149 -- this case is the instance spec.
2151 if Nkind (Context) = N_Package_Instantiation then
2152 Context := Instance_Spec (Context);
2153 end if;
2155 if Nkind_In (Context, N_Generic_Package_Declaration,
2156 N_Package_Declaration)
2157 then
2158 Make_Aitem_Pragma
2159 (Pragma_Argument_Associations => New_List (
2160 Make_Pragma_Argument_Association (Loc,
2161 Expression => Relocate_Node (Expr))),
2162 Pragma_Name => Name_Abstract_State);
2163 Decorate (Aspect, Aitem);
2165 Decls := Visible_Declarations (Specification (Context));
2167 -- In general pragma Abstract_State must be at the top
2168 -- of the existing visible declarations to emulate its
2169 -- source counterpart. The only exception to this is a
2170 -- generic instance in which case the pragma must be
2171 -- inserted after the association renamings.
2173 if Present (Decls) then
2174 Decl := First (Decls);
2176 -- The visible declarations of a generic instance have
2177 -- the following structure:
2179 -- <renamings of generic formals>
2180 -- <renamings of internally-generated spec and body>
2181 -- <first source declaration>
2183 -- The pragma must be inserted before the first source
2184 -- declaration, skip the instance "header".
2186 if Is_Generic_Instance (Defining_Entity (Context)) then
2187 while Present (Decl)
2188 and then not Comes_From_Source (Decl)
2189 loop
2190 Decl := Next (Decl);
2191 end loop;
2192 end if;
2194 -- When aspects Abstract_State, Ghost,
2195 -- Initial_Condition and Initializes are out of order,
2196 -- ensure that pragma SPARK_Mode is always at the top
2197 -- of the declarations to properly enabled/suppress
2198 -- errors.
2200 Insert_After_SPARK_Mode
2201 (Prag => Aitem,
2202 Ins_Nod => Decl,
2203 Decls => Decls);
2205 -- Otherwise the pragma forms a new declarative list
2207 else
2208 Set_Visible_Declarations
2209 (Specification (Context), New_List (Aitem));
2210 end if;
2212 else
2213 Error_Msg_NE
2214 ("aspect & must apply to a package declaration",
2215 Aspect, Id);
2216 end if;
2218 goto Continue;
2219 end Abstract_State;
2221 -- Aspect Default_Internal_Condition is never delayed because
2222 -- it is equivalent to a source pragma which appears after the
2223 -- related private type. To deal with forward references, the
2224 -- generated pragma is stored in the rep chain of the related
2225 -- private type as types do not carry contracts. The pragma is
2226 -- wrapped inside of a procedure at the freeze point of the
2227 -- private type's full view.
2229 when Aspect_Default_Initial_Condition =>
2230 Make_Aitem_Pragma
2231 (Pragma_Argument_Associations => New_List (
2232 Make_Pragma_Argument_Association (Loc,
2233 Expression => Relocate_Node (Expr))),
2234 Pragma_Name =>
2235 Name_Default_Initial_Condition);
2237 Decorate (Aspect, Aitem);
2238 Insert_Pragma (Aitem);
2239 goto Continue;
2241 -- Default_Storage_Pool
2243 when Aspect_Default_Storage_Pool =>
2244 Make_Aitem_Pragma
2245 (Pragma_Argument_Associations => New_List (
2246 Make_Pragma_Argument_Association (Loc,
2247 Expression => Relocate_Node (Expr))),
2248 Pragma_Name =>
2249 Name_Default_Storage_Pool);
2251 Decorate (Aspect, Aitem);
2252 Insert_Pragma (Aitem);
2253 goto Continue;
2255 -- Depends
2257 -- Aspect Depends is never delayed because it is equivalent to
2258 -- a source pragma which appears after the related subprogram.
2259 -- To deal with forward references, the generated pragma is
2260 -- stored in the contract of the related subprogram and later
2261 -- analyzed at the end of the declarative region. See routine
2262 -- Analyze_Depends_In_Decl_Part for details.
2264 when Aspect_Depends =>
2265 Make_Aitem_Pragma
2266 (Pragma_Argument_Associations => New_List (
2267 Make_Pragma_Argument_Association (Loc,
2268 Expression => Relocate_Node (Expr))),
2269 Pragma_Name => Name_Depends);
2271 Decorate (Aspect, Aitem);
2272 Insert_Pragma (Aitem);
2273 goto Continue;
2275 -- Aspect Extensions_Visible is never delayed because it is
2276 -- equivalent to a source pragma which appears after the
2277 -- related subprogram.
2279 when Aspect_Extensions_Visible =>
2280 Make_Aitem_Pragma
2281 (Pragma_Argument_Associations => New_List (
2282 Make_Pragma_Argument_Association (Loc,
2283 Expression => Relocate_Node (Expr))),
2284 Pragma_Name => Name_Extensions_Visible);
2286 Decorate (Aspect, Aitem);
2287 Insert_Pragma (Aitem);
2288 goto Continue;
2290 -- Aspect Ghost is never delayed because it is equivalent to a
2291 -- source pragma which appears at the top of [generic] package
2292 -- declarations or after an object, a [generic] subprogram, or
2293 -- a type declaration.
2295 when Aspect_Ghost => Ghost : declare
2296 Decls : List_Id;
2298 begin
2299 Make_Aitem_Pragma
2300 (Pragma_Argument_Associations => New_List (
2301 Make_Pragma_Argument_Association (Loc,
2302 Expression => Relocate_Node (Expr))),
2303 Pragma_Name => Name_Ghost);
2305 Decorate (Aspect, Aitem);
2307 -- When the aspect applies to a [generic] package, insert
2308 -- the pragma at the top of the visible declarations. This
2309 -- emulates the placement of a source pragma.
2311 if Nkind_In (N, N_Generic_Package_Declaration,
2312 N_Package_Declaration)
2313 then
2314 Decls := Visible_Declarations (Specification (N));
2316 if No (Decls) then
2317 Decls := New_List;
2318 Set_Visible_Declarations (N, Decls);
2319 end if;
2321 -- When aspects Abstract_State, Ghost, Initial_Condition
2322 -- and Initializes are out of order, ensure that pragma
2323 -- SPARK_Mode is always at the top of the declarations to
2324 -- properly enabled/suppress errors.
2326 Insert_After_SPARK_Mode
2327 (Prag => Aitem,
2328 Ins_Nod => First (Decls),
2329 Decls => Decls);
2331 -- Otherwise the context is an object, [generic] subprogram
2332 -- or type declaration.
2334 else
2335 Insert_Pragma (Aitem);
2336 end if;
2338 goto Continue;
2339 end Ghost;
2341 -- Global
2343 -- Aspect Global is never delayed because it is equivalent to
2344 -- a source pragma which appears after the related subprogram.
2345 -- To deal with forward references, the generated pragma is
2346 -- stored in the contract of the related subprogram and later
2347 -- analyzed at the end of the declarative region. See routine
2348 -- Analyze_Global_In_Decl_Part for details.
2350 when Aspect_Global =>
2351 Make_Aitem_Pragma
2352 (Pragma_Argument_Associations => New_List (
2353 Make_Pragma_Argument_Association (Loc,
2354 Expression => Relocate_Node (Expr))),
2355 Pragma_Name => Name_Global);
2357 Decorate (Aspect, Aitem);
2358 Insert_Pragma (Aitem);
2359 goto Continue;
2361 -- Initial_Condition
2363 -- Aspect Initial_Condition is never delayed because it is
2364 -- equivalent to a source pragma which appears after the
2365 -- related package. To deal with forward references, the
2366 -- generated pragma is stored in the contract of the related
2367 -- package and later analyzed at the end of the declarative
2368 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2369 -- for details.
2371 when Aspect_Initial_Condition => Initial_Condition : declare
2372 Context : Node_Id := N;
2373 Decls : List_Id;
2375 begin
2376 -- When aspect Initial_Condition appears on a generic
2377 -- package, it is propageted to the package instance. The
2378 -- context in this case is the instance spec.
2380 if Nkind (Context) = N_Package_Instantiation then
2381 Context := Instance_Spec (Context);
2382 end if;
2384 if Nkind_In (Context, N_Generic_Package_Declaration,
2385 N_Package_Declaration)
2386 then
2387 Decls := Visible_Declarations (Specification (Context));
2389 Make_Aitem_Pragma
2390 (Pragma_Argument_Associations => New_List (
2391 Make_Pragma_Argument_Association (Loc,
2392 Expression => Relocate_Node (Expr))),
2393 Pragma_Name =>
2394 Name_Initial_Condition);
2395 Decorate (Aspect, Aitem);
2397 if No (Decls) then
2398 Decls := New_List;
2399 Set_Visible_Declarations (Context, Decls);
2400 end if;
2402 -- When aspects Abstract_State, Ghost, Initial_Condition
2403 -- and Initializes are out of order, ensure that pragma
2404 -- SPARK_Mode is always at the top of the declarations to
2405 -- properly enabled/suppress errors.
2407 Insert_After_SPARK_Mode
2408 (Prag => Aitem,
2409 Ins_Nod => First (Decls),
2410 Decls => Decls);
2412 else
2413 Error_Msg_NE
2414 ("aspect & must apply to a package declaration",
2415 Aspect, Id);
2416 end if;
2418 goto Continue;
2419 end Initial_Condition;
2421 -- Initializes
2423 -- Aspect Initializes is never delayed because it is equivalent
2424 -- to a source pragma appearing after the related package. To
2425 -- deal with forward references, the generated pragma is stored
2426 -- in the contract of the related package and later analyzed at
2427 -- the end of the declarative region. For details, see routine
2428 -- Analyze_Initializes_In_Decl_Part.
2430 when Aspect_Initializes => Initializes : declare
2431 Context : Node_Id := N;
2432 Decls : List_Id;
2434 begin
2435 -- When aspect Initializes appears on a generic package,
2436 -- it is propageted to the package instance. The context
2437 -- in this case is the instance spec.
2439 if Nkind (Context) = N_Package_Instantiation then
2440 Context := Instance_Spec (Context);
2441 end if;
2443 if Nkind_In (Context, N_Generic_Package_Declaration,
2444 N_Package_Declaration)
2445 then
2446 Decls := Visible_Declarations (Specification (Context));
2448 Make_Aitem_Pragma
2449 (Pragma_Argument_Associations => New_List (
2450 Make_Pragma_Argument_Association (Loc,
2451 Expression => Relocate_Node (Expr))),
2452 Pragma_Name => Name_Initializes);
2453 Decorate (Aspect, Aitem);
2455 if No (Decls) then
2456 Decls := New_List;
2457 Set_Visible_Declarations (Context, Decls);
2458 end if;
2460 -- When aspects Abstract_State, Ghost, Initial_Condition
2461 -- and Initializes are out of order, ensure that pragma
2462 -- SPARK_Mode is always at the top of the declarations to
2463 -- properly enabled/suppress errors.
2465 Insert_After_SPARK_Mode
2466 (Prag => Aitem,
2467 Ins_Nod => First (Decls),
2468 Decls => Decls);
2470 else
2471 Error_Msg_NE
2472 ("aspect & must apply to a package declaration",
2473 Aspect, Id);
2474 end if;
2476 goto Continue;
2477 end Initializes;
2479 -- Obsolescent
2481 when Aspect_Obsolescent => declare
2482 Args : List_Id;
2484 begin
2485 if No (Expr) then
2486 Args := No_List;
2487 else
2488 Args := New_List (
2489 Make_Pragma_Argument_Association (Sloc (Expr),
2490 Expression => Relocate_Node (Expr)));
2491 end if;
2493 Make_Aitem_Pragma
2494 (Pragma_Argument_Associations => Args,
2495 Pragma_Name => Chars (Id));
2496 end;
2498 -- Part_Of
2500 when Aspect_Part_Of =>
2501 if Nkind_In (N, N_Object_Declaration,
2502 N_Package_Instantiation)
2503 then
2504 Make_Aitem_Pragma
2505 (Pragma_Argument_Associations => New_List (
2506 Make_Pragma_Argument_Association (Loc,
2507 Expression => Relocate_Node (Expr))),
2508 Pragma_Name => Name_Part_Of);
2510 else
2511 Error_Msg_NE
2512 ("aspect & must apply to a variable or package "
2513 & "instantiation", Aspect, Id);
2514 end if;
2516 -- SPARK_Mode
2518 when Aspect_SPARK_Mode => SPARK_Mode : declare
2519 Decls : List_Id;
2521 begin
2522 Make_Aitem_Pragma
2523 (Pragma_Argument_Associations => New_List (
2524 Make_Pragma_Argument_Association (Loc,
2525 Expression => Relocate_Node (Expr))),
2526 Pragma_Name => Name_SPARK_Mode);
2528 -- When the aspect appears on a package or a subprogram
2529 -- body, insert the generated pragma at the top of the body
2530 -- declarations to emulate the behavior of a source pragma.
2532 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2533 Decorate (Aspect, Aitem);
2535 Decls := Declarations (N);
2537 if No (Decls) then
2538 Decls := New_List;
2539 Set_Declarations (N, Decls);
2540 end if;
2542 Prepend_To (Decls, Aitem);
2543 goto Continue;
2545 -- When the aspect is associated with a [generic] package
2546 -- declaration, insert the generated pragma at the top of
2547 -- the visible declarations to emulate the behavior of a
2548 -- source pragma.
2550 elsif Nkind_In (N, N_Generic_Package_Declaration,
2551 N_Package_Declaration)
2552 then
2553 Decorate (Aspect, Aitem);
2555 Decls := Visible_Declarations (Specification (N));
2557 if No (Decls) then
2558 Decls := New_List;
2559 Set_Visible_Declarations (Specification (N), Decls);
2560 end if;
2562 Prepend_To (Decls, Aitem);
2563 goto Continue;
2564 end if;
2565 end SPARK_Mode;
2567 -- Refined_Depends
2569 -- Aspect Refined_Depends is never delayed because it is
2570 -- equivalent to a source pragma which appears in the
2571 -- declarations of the related subprogram body. To deal with
2572 -- forward references, the generated pragma is stored in the
2573 -- contract of the related subprogram body and later analyzed
2574 -- at the end of the declarative region. For details, see
2575 -- routine Analyze_Refined_Depends_In_Decl_Part.
2577 when Aspect_Refined_Depends =>
2578 Make_Aitem_Pragma
2579 (Pragma_Argument_Associations => New_List (
2580 Make_Pragma_Argument_Association (Loc,
2581 Expression => Relocate_Node (Expr))),
2582 Pragma_Name => Name_Refined_Depends);
2584 Decorate (Aspect, Aitem);
2585 Insert_Pragma (Aitem);
2586 goto Continue;
2588 -- Refined_Global
2590 -- Aspect Refined_Global is never delayed because it is
2591 -- equivalent to a source pragma which appears in the
2592 -- declarations of the related subprogram body. To deal with
2593 -- forward references, the generated pragma is stored in the
2594 -- contract of the related subprogram body and later analyzed
2595 -- at the end of the declarative region. For details, see
2596 -- routine Analyze_Refined_Global_In_Decl_Part.
2598 when Aspect_Refined_Global =>
2599 Make_Aitem_Pragma
2600 (Pragma_Argument_Associations => New_List (
2601 Make_Pragma_Argument_Association (Loc,
2602 Expression => Relocate_Node (Expr))),
2603 Pragma_Name => Name_Refined_Global);
2605 Decorate (Aspect, Aitem);
2606 Insert_Pragma (Aitem);
2607 goto Continue;
2609 -- Refined_Post
2611 when Aspect_Refined_Post =>
2612 Make_Aitem_Pragma
2613 (Pragma_Argument_Associations => New_List (
2614 Make_Pragma_Argument_Association (Loc,
2615 Expression => Relocate_Node (Expr))),
2616 Pragma_Name => Name_Refined_Post);
2618 -- Refined_State
2620 when Aspect_Refined_State => Refined_State : declare
2621 Decls : List_Id;
2623 begin
2624 -- The corresponding pragma for Refined_State is inserted in
2625 -- the declarations of the related package body. This action
2626 -- synchronizes both the source and from-aspect versions of
2627 -- the pragma.
2629 if Nkind (N) = N_Package_Body then
2630 Decls := Declarations (N);
2632 Make_Aitem_Pragma
2633 (Pragma_Argument_Associations => New_List (
2634 Make_Pragma_Argument_Association (Loc,
2635 Expression => Relocate_Node (Expr))),
2636 Pragma_Name => Name_Refined_State);
2637 Decorate (Aspect, Aitem);
2639 if No (Decls) then
2640 Decls := New_List;
2641 Set_Declarations (N, Decls);
2642 end if;
2644 -- Pragma Refined_State must be inserted after pragma
2645 -- SPARK_Mode in the tree. This ensures that any error
2646 -- messages dependent on SPARK_Mode will be properly
2647 -- enabled/suppressed.
2649 Insert_After_SPARK_Mode
2650 (Prag => Aitem,
2651 Ins_Nod => First (Decls),
2652 Decls => Decls);
2654 else
2655 Error_Msg_NE
2656 ("aspect & must apply to a package body", Aspect, Id);
2657 end if;
2659 goto Continue;
2660 end Refined_State;
2662 -- Relative_Deadline
2664 when Aspect_Relative_Deadline =>
2665 Make_Aitem_Pragma
2666 (Pragma_Argument_Associations => New_List (
2667 Make_Pragma_Argument_Association (Loc,
2668 Expression => Relocate_Node (Expr))),
2669 Pragma_Name => Name_Relative_Deadline);
2671 -- If the aspect applies to a task, the corresponding pragma
2672 -- must appear within its declarations, not after.
2674 if Nkind (N) = N_Task_Type_Declaration then
2675 declare
2676 Def : Node_Id;
2677 V : List_Id;
2679 begin
2680 if No (Task_Definition (N)) then
2681 Set_Task_Definition (N,
2682 Make_Task_Definition (Loc,
2683 Visible_Declarations => New_List,
2684 End_Label => Empty));
2685 end if;
2687 Def := Task_Definition (N);
2688 V := Visible_Declarations (Def);
2689 if not Is_Empty_List (V) then
2690 Insert_Before (First (V), Aitem);
2692 else
2693 Set_Visible_Declarations (Def, New_List (Aitem));
2694 end if;
2696 goto Continue;
2697 end;
2698 end if;
2700 -- Case 2e: Annotate aspect
2702 when Aspect_Annotate =>
2703 declare
2704 Args : List_Id;
2705 Pargs : List_Id;
2706 Arg : Node_Id;
2708 begin
2709 -- The argument can be a single identifier
2711 if Nkind (Expr) = N_Identifier then
2713 -- One level of parens is allowed
2715 if Paren_Count (Expr) > 1 then
2716 Error_Msg_F ("extra parentheses ignored", Expr);
2717 end if;
2719 Set_Paren_Count (Expr, 0);
2721 -- Add the single item to the list
2723 Args := New_List (Expr);
2725 -- Otherwise we must have an aggregate
2727 elsif Nkind (Expr) = N_Aggregate then
2729 -- Must be positional
2731 if Present (Component_Associations (Expr)) then
2732 Error_Msg_F
2733 ("purely positional aggregate required", Expr);
2734 goto Continue;
2735 end if;
2737 -- Must not be parenthesized
2739 if Paren_Count (Expr) /= 0 then
2740 Error_Msg_F ("extra parentheses ignored", Expr);
2741 end if;
2743 -- List of arguments is list of aggregate expressions
2745 Args := Expressions (Expr);
2747 -- Anything else is illegal
2749 else
2750 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2751 goto Continue;
2752 end if;
2754 -- Prepare pragma arguments
2756 Pargs := New_List;
2757 Arg := First (Args);
2758 while Present (Arg) loop
2759 Append_To (Pargs,
2760 Make_Pragma_Argument_Association (Sloc (Arg),
2761 Expression => Relocate_Node (Arg)));
2762 Next (Arg);
2763 end loop;
2765 Append_To (Pargs,
2766 Make_Pragma_Argument_Association (Sloc (Ent),
2767 Chars => Name_Entity,
2768 Expression => Ent));
2770 Make_Aitem_Pragma
2771 (Pragma_Argument_Associations => Pargs,
2772 Pragma_Name => Name_Annotate);
2773 end;
2775 -- Case 3 : Aspects that don't correspond to pragma/attribute
2776 -- definition clause.
2778 -- Case 3a: The aspects listed below don't correspond to
2779 -- pragmas/attributes but do require delayed analysis.
2781 -- Default_Value can only apply to a scalar type
2783 when Aspect_Default_Value =>
2784 if not Is_Scalar_Type (E) then
2785 Error_Msg_N
2786 ("aspect Default_Value must apply to a scalar type", N);
2787 end if;
2789 Aitem := Empty;
2791 -- Default_Component_Value can only apply to an array type
2792 -- with scalar components.
2794 when Aspect_Default_Component_Value =>
2795 if not (Is_Array_Type (E)
2796 and then Is_Scalar_Type (Component_Type (E)))
2797 then
2798 Error_Msg_N ("aspect Default_Component_Value can only "
2799 & "apply to an array of scalar components", N);
2800 end if;
2802 Aitem := Empty;
2804 -- Case 3b: The aspects listed below don't correspond to
2805 -- pragmas/attributes and don't need delayed analysis.
2807 -- Implicit_Dereference
2809 -- For Implicit_Dereference, External_Name and Link_Name, only
2810 -- the legality checks are done during the analysis, thus no
2811 -- delay is required.
2813 when Aspect_Implicit_Dereference =>
2814 Analyze_Aspect_Implicit_Dereference;
2815 goto Continue;
2817 -- External_Name, Link_Name
2819 when Aspect_External_Name |
2820 Aspect_Link_Name =>
2821 Analyze_Aspect_External_Or_Link_Name;
2822 goto Continue;
2824 -- Dimension
2826 when Aspect_Dimension =>
2827 Analyze_Aspect_Dimension (N, Id, Expr);
2828 goto Continue;
2830 -- Dimension_System
2832 when Aspect_Dimension_System =>
2833 Analyze_Aspect_Dimension_System (N, Id, Expr);
2834 goto Continue;
2836 -- Case 4: Aspects requiring special handling
2838 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2839 -- pragmas take care of the delay.
2841 -- Pre/Post
2843 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2844 -- with a first argument that is the expression, and a second
2845 -- argument that is an informative message if the test fails.
2846 -- This is inserted right after the declaration, to get the
2847 -- required pragma placement. The processing for the pragmas
2848 -- takes care of the required delay.
2850 when Pre_Post_Aspects => Pre_Post : declare
2851 Pname : Name_Id;
2853 begin
2854 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2855 Pname := Name_Precondition;
2856 else
2857 Pname := Name_Postcondition;
2858 end if;
2860 -- If the expressions is of the form A and then B, then
2861 -- we generate separate Pre/Post aspects for the separate
2862 -- clauses. Since we allow multiple pragmas, there is no
2863 -- problem in allowing multiple Pre/Post aspects internally.
2864 -- These should be treated in reverse order (B first and
2865 -- A second) since they are later inserted just after N in
2866 -- the order they are treated. This way, the pragma for A
2867 -- ends up preceding the pragma for B, which may have an
2868 -- importance for the error raised (either constraint error
2869 -- or precondition error).
2871 -- We do not do this for Pre'Class, since we have to put
2872 -- these conditions together in a complex OR expression.
2874 -- We do not do this in ASIS mode, as ASIS relies on the
2875 -- original node representing the complete expression, when
2876 -- retrieving it through the source aspect table.
2878 if not ASIS_Mode
2879 and then (Pname = Name_Postcondition
2880 or else not Class_Present (Aspect))
2881 then
2882 while Nkind (Expr) = N_And_Then loop
2883 Insert_After (Aspect,
2884 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2885 Identifier => Identifier (Aspect),
2886 Expression => Relocate_Node (Left_Opnd (Expr)),
2887 Class_Present => Class_Present (Aspect),
2888 Split_PPC => True));
2889 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2890 Eloc := Sloc (Expr);
2891 end loop;
2892 end if;
2894 -- Build the precondition/postcondition pragma
2896 -- Add note about why we do NOT need Copy_Tree here???
2898 Make_Aitem_Pragma
2899 (Pragma_Argument_Associations => New_List (
2900 Make_Pragma_Argument_Association (Eloc,
2901 Chars => Name_Check,
2902 Expression => Relocate_Node (Expr))),
2903 Pragma_Name => Pname);
2905 -- Add message unless exception messages are suppressed
2907 if not Opt.Exception_Locations_Suppressed then
2908 Append_To (Pragma_Argument_Associations (Aitem),
2909 Make_Pragma_Argument_Association (Eloc,
2910 Chars => Name_Message,
2911 Expression =>
2912 Make_String_Literal (Eloc,
2913 Strval => "failed "
2914 & Get_Name_String (Pname)
2915 & " from "
2916 & Build_Location_String (Eloc))));
2917 end if;
2919 Set_Is_Delayed_Aspect (Aspect);
2921 -- For Pre/Post cases, insert immediately after the entity
2922 -- declaration, since that is the required pragma placement.
2923 -- Note that for these aspects, we do not have to worry
2924 -- about delay issues, since the pragmas themselves deal
2925 -- with delay of visibility for the expression analysis.
2927 Insert_Pragma (Aitem);
2929 goto Continue;
2930 end Pre_Post;
2932 -- Test_Case
2934 when Aspect_Test_Case => Test_Case : declare
2935 Args : List_Id;
2936 Comp_Expr : Node_Id;
2937 Comp_Assn : Node_Id;
2938 New_Expr : Node_Id;
2940 begin
2941 Args := New_List;
2943 if Nkind (Parent (N)) = N_Compilation_Unit then
2944 Error_Msg_Name_1 := Nam;
2945 Error_Msg_N ("incorrect placement of aspect `%`", E);
2946 goto Continue;
2947 end if;
2949 if Nkind (Expr) /= N_Aggregate then
2950 Error_Msg_Name_1 := Nam;
2951 Error_Msg_NE
2952 ("wrong syntax for aspect `%` for &", Id, E);
2953 goto Continue;
2954 end if;
2956 -- Make pragma expressions refer to the original aspect
2957 -- expressions through the Original_Node link. This is used
2958 -- in semantic analysis for ASIS mode, so that the original
2959 -- expression also gets analyzed.
2961 Comp_Expr := First (Expressions (Expr));
2962 while Present (Comp_Expr) loop
2963 New_Expr := Relocate_Node (Comp_Expr);
2964 Set_Original_Node (New_Expr, Comp_Expr);
2965 Append_To (Args,
2966 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2967 Expression => New_Expr));
2968 Next (Comp_Expr);
2969 end loop;
2971 Comp_Assn := First (Component_Associations (Expr));
2972 while Present (Comp_Assn) loop
2973 if List_Length (Choices (Comp_Assn)) /= 1
2974 or else
2975 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2976 then
2977 Error_Msg_Name_1 := Nam;
2978 Error_Msg_NE
2979 ("wrong syntax for aspect `%` for &", Id, E);
2980 goto Continue;
2981 end if;
2983 New_Expr := Relocate_Node (Expression (Comp_Assn));
2984 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2985 Append_To (Args,
2986 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2987 Chars => Chars (First (Choices (Comp_Assn))),
2988 Expression => New_Expr));
2989 Next (Comp_Assn);
2990 end loop;
2992 -- Build the test-case pragma
2994 Make_Aitem_Pragma
2995 (Pragma_Argument_Associations => Args,
2996 Pragma_Name => Nam);
2997 end Test_Case;
2999 -- Contract_Cases
3001 when Aspect_Contract_Cases =>
3002 Make_Aitem_Pragma
3003 (Pragma_Argument_Associations => New_List (
3004 Make_Pragma_Argument_Association (Loc,
3005 Expression => Relocate_Node (Expr))),
3006 Pragma_Name => Nam);
3008 Decorate (Aspect, Aitem);
3009 Insert_Pragma (Aitem);
3010 goto Continue;
3012 -- Case 5: Special handling for aspects with an optional
3013 -- boolean argument.
3015 -- In the general case, the corresponding pragma cannot be
3016 -- generated yet because the evaluation of the boolean needs
3017 -- to be delayed till the freeze point.
3019 when Boolean_Aspects |
3020 Library_Unit_Aspects =>
3022 Set_Is_Boolean_Aspect (Aspect);
3024 -- Lock_Free aspect only apply to protected objects
3026 if A_Id = Aspect_Lock_Free then
3027 if Ekind (E) /= E_Protected_Type then
3028 Error_Msg_Name_1 := Nam;
3029 Error_Msg_N
3030 ("aspect % only applies to a protected object",
3031 Aspect);
3033 else
3034 -- Set the Uses_Lock_Free flag to True if there is no
3035 -- expression or if the expression is True. The
3036 -- evaluation of this aspect should be delayed to the
3037 -- freeze point (why???)
3039 if No (Expr)
3040 or else Is_True (Static_Boolean (Expr))
3041 then
3042 Set_Uses_Lock_Free (E);
3043 end if;
3045 Record_Rep_Item (E, Aspect);
3046 end if;
3048 goto Continue;
3050 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
3052 -- For the case of aspects Import and Export, we don't
3053 -- consider that we know the entity is never set in the
3054 -- source, since it is is likely modified outside the
3055 -- program.
3057 -- Note: one might think that the analysis of the
3058 -- resulting pragma would take care of that, but
3059 -- that's not the case since it won't be from source.
3061 if Ekind (E) = E_Variable then
3062 Set_Never_Set_In_Source (E, False);
3063 end if;
3065 -- In older versions of Ada the corresponding pragmas
3066 -- specified a Convention. In Ada 2012 the convention is
3067 -- specified as a separate aspect, and it is optional,
3068 -- given that it defaults to Convention_Ada. The code
3069 -- that verifed that there was a matching convention
3070 -- is now obsolete.
3072 -- Resolve the expression of an Import or Export here,
3073 -- and require it to be of type Boolean and static. This
3074 -- is not quite right, because in general this should be
3075 -- delayed, but that seems tricky for these, because
3076 -- normally Boolean aspects are replaced with pragmas at
3077 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
3078 -- but in the case of these aspects we can't generate
3079 -- a simple pragma with just the entity name. ???
3081 if not Present (Expr)
3082 or else Is_True (Static_Boolean (Expr))
3083 then
3084 if A_Id = Aspect_Import then
3085 Set_Is_Imported (E);
3087 -- An imported entity cannot have an explicit
3088 -- initialization.
3090 if Nkind (N) = N_Object_Declaration
3091 and then Present (Expression (N))
3092 then
3093 Error_Msg_N
3094 ("imported entities cannot be initialized "
3095 & "(RM B.1(24))", Expression (N));
3096 end if;
3098 elsif A_Id = Aspect_Export then
3099 Set_Is_Exported (E);
3100 end if;
3101 end if;
3103 goto Continue;
3104 end if;
3106 -- Library unit aspects require special handling in the case
3107 -- of a package declaration, the pragma needs to be inserted
3108 -- in the list of declarations for the associated package.
3109 -- There is no issue of visibility delay for these aspects.
3111 if A_Id in Library_Unit_Aspects
3112 and then
3113 Nkind_In (N, N_Package_Declaration,
3114 N_Generic_Package_Declaration)
3115 and then Nkind (Parent (N)) /= N_Compilation_Unit
3117 -- Aspect is legal on a local instantiation of a library-
3118 -- level generic unit.
3120 and then not Is_Generic_Instance (Defining_Entity (N))
3121 then
3122 Error_Msg_N
3123 ("incorrect context for library unit aspect&", Id);
3124 goto Continue;
3125 end if;
3127 -- External property aspects are Boolean by nature, but
3128 -- their pragmas must contain two arguments, the second
3129 -- being the optional Boolean expression.
3131 if A_Id = Aspect_Async_Readers or else
3132 A_Id = Aspect_Async_Writers or else
3133 A_Id = Aspect_Effective_Reads or else
3134 A_Id = Aspect_Effective_Writes
3135 then
3136 declare
3137 Args : List_Id;
3139 begin
3140 -- The first argument of the external property pragma
3141 -- is the related object.
3143 Args :=
3144 New_List (
3145 Make_Pragma_Argument_Association (Sloc (Ent),
3146 Expression => Ent));
3148 -- The second argument is the optional Boolean
3149 -- expression which must be propagated even if it
3150 -- evaluates to False as this has special semantic
3151 -- meaning.
3153 if Present (Expr) then
3154 Append_To (Args,
3155 Make_Pragma_Argument_Association (Loc,
3156 Expression => Relocate_Node (Expr)));
3157 end if;
3159 Make_Aitem_Pragma
3160 (Pragma_Argument_Associations => Args,
3161 Pragma_Name => Nam);
3162 end;
3164 -- Cases where we do not delay, includes all cases where the
3165 -- expression is missing other than the above cases.
3167 elsif not Delay_Required or else No (Expr) then
3168 Make_Aitem_Pragma
3169 (Pragma_Argument_Associations => New_List (
3170 Make_Pragma_Argument_Association (Sloc (Ent),
3171 Expression => Ent)),
3172 Pragma_Name => Chars (Id));
3173 Delay_Required := False;
3175 -- In general cases, the corresponding pragma/attribute
3176 -- definition clause will be inserted later at the freezing
3177 -- point, and we do not need to build it now.
3179 else
3180 Aitem := Empty;
3181 end if;
3183 -- Storage_Size
3185 -- This is special because for access types we need to generate
3186 -- an attribute definition clause. This also works for single
3187 -- task declarations, but it does not work for task type
3188 -- declarations, because we have the case where the expression
3189 -- references a discriminant of the task type. That can't use
3190 -- an attribute definition clause because we would not have
3191 -- visibility on the discriminant. For that case we must
3192 -- generate a pragma in the task definition.
3194 when Aspect_Storage_Size =>
3196 -- Task type case
3198 if Ekind (E) = E_Task_Type then
3199 declare
3200 Decl : constant Node_Id := Declaration_Node (E);
3202 begin
3203 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3205 -- If no task definition, create one
3207 if No (Task_Definition (Decl)) then
3208 Set_Task_Definition (Decl,
3209 Make_Task_Definition (Loc,
3210 Visible_Declarations => Empty_List,
3211 End_Label => Empty));
3212 end if;
3214 -- Create a pragma and put it at the start of the task
3215 -- definition for the task type declaration.
3217 Make_Aitem_Pragma
3218 (Pragma_Argument_Associations => New_List (
3219 Make_Pragma_Argument_Association (Loc,
3220 Expression => Relocate_Node (Expr))),
3221 Pragma_Name => Name_Storage_Size);
3223 Prepend
3224 (Aitem,
3225 Visible_Declarations (Task_Definition (Decl)));
3226 goto Continue;
3227 end;
3229 -- All other cases, generate attribute definition
3231 else
3232 Aitem :=
3233 Make_Attribute_Definition_Clause (Loc,
3234 Name => Ent,
3235 Chars => Chars (Id),
3236 Expression => Relocate_Node (Expr));
3237 end if;
3238 end case;
3240 -- Attach the corresponding pragma/attribute definition clause to
3241 -- the aspect specification node.
3243 if Present (Aitem) then
3244 Set_From_Aspect_Specification (Aitem);
3245 end if;
3247 -- In the context of a compilation unit, we directly put the
3248 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3249 -- node (no delay is required here) except for aspects on a
3250 -- subprogram body (see below) and a generic package, for which we
3251 -- need to introduce the pragma before building the generic copy
3252 -- (see sem_ch12), and for package instantiations, where the
3253 -- library unit pragmas are better handled early.
3255 if Nkind (Parent (N)) = N_Compilation_Unit
3256 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3257 then
3258 declare
3259 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3261 begin
3262 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3264 -- For a Boolean aspect, create the corresponding pragma if
3265 -- no expression or if the value is True.
3267 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3268 if Is_True (Static_Boolean (Expr)) then
3269 Make_Aitem_Pragma
3270 (Pragma_Argument_Associations => New_List (
3271 Make_Pragma_Argument_Association (Sloc (Ent),
3272 Expression => Ent)),
3273 Pragma_Name => Chars (Id));
3275 Set_From_Aspect_Specification (Aitem, True);
3276 Set_Corresponding_Aspect (Aitem, Aspect);
3278 else
3279 goto Continue;
3280 end if;
3281 end if;
3283 -- If the aspect is on a subprogram body (relevant aspect
3284 -- is Inline), add the pragma in front of the declarations.
3286 if Nkind (N) = N_Subprogram_Body then
3287 if No (Declarations (N)) then
3288 Set_Declarations (N, New_List);
3289 end if;
3291 Prepend (Aitem, Declarations (N));
3293 elsif Nkind (N) = N_Generic_Package_Declaration then
3294 if No (Visible_Declarations (Specification (N))) then
3295 Set_Visible_Declarations (Specification (N), New_List);
3296 end if;
3298 Prepend (Aitem,
3299 Visible_Declarations (Specification (N)));
3301 elsif Nkind (N) = N_Package_Instantiation then
3302 declare
3303 Spec : constant Node_Id :=
3304 Specification (Instance_Spec (N));
3305 begin
3306 if No (Visible_Declarations (Spec)) then
3307 Set_Visible_Declarations (Spec, New_List);
3308 end if;
3310 Prepend (Aitem, Visible_Declarations (Spec));
3311 end;
3313 else
3314 if No (Pragmas_After (Aux)) then
3315 Set_Pragmas_After (Aux, New_List);
3316 end if;
3318 Append (Aitem, Pragmas_After (Aux));
3319 end if;
3321 goto Continue;
3322 end;
3323 end if;
3325 -- The evaluation of the aspect is delayed to the freezing point.
3326 -- The pragma or attribute clause if there is one is then attached
3327 -- to the aspect specification which is put in the rep item list.
3329 if Delay_Required then
3330 if Present (Aitem) then
3331 Set_Is_Delayed_Aspect (Aitem);
3332 Set_Aspect_Rep_Item (Aspect, Aitem);
3333 Set_Parent (Aitem, Aspect);
3334 end if;
3336 Set_Is_Delayed_Aspect (Aspect);
3338 -- In the case of Default_Value, link the aspect to base type
3339 -- as well, even though it appears on a first subtype. This is
3340 -- mandated by the semantics of the aspect. Do not establish
3341 -- the link when processing the base type itself as this leads
3342 -- to a rep item circularity. Verify that we are dealing with
3343 -- a scalar type to prevent cascaded errors.
3345 if A_Id = Aspect_Default_Value
3346 and then Is_Scalar_Type (E)
3347 and then Base_Type (E) /= E
3348 then
3349 Set_Has_Delayed_Aspects (Base_Type (E));
3350 Record_Rep_Item (Base_Type (E), Aspect);
3351 end if;
3353 Set_Has_Delayed_Aspects (E);
3354 Record_Rep_Item (E, Aspect);
3356 -- When delay is not required and the context is a package or a
3357 -- subprogram body, insert the pragma in the body declarations.
3359 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3360 if No (Declarations (N)) then
3361 Set_Declarations (N, New_List);
3362 end if;
3364 -- The pragma is added before source declarations
3366 Prepend_To (Declarations (N), Aitem);
3368 -- When delay is not required and the context is not a compilation
3369 -- unit, we simply insert the pragma/attribute definition clause
3370 -- in sequence.
3372 else
3373 Insert_After (Ins_Node, Aitem);
3374 Ins_Node := Aitem;
3375 end if;
3376 end Analyze_One_Aspect;
3378 <<Continue>>
3379 Next (Aspect);
3380 end loop Aspect_Loop;
3382 if Has_Delayed_Aspects (E) then
3383 Ensure_Freeze_Node (E);
3384 end if;
3385 end Analyze_Aspect_Specifications;
3387 -----------------------
3388 -- Analyze_At_Clause --
3389 -----------------------
3391 -- An at clause is replaced by the corresponding Address attribute
3392 -- definition clause that is the preferred approach in Ada 95.
3394 procedure Analyze_At_Clause (N : Node_Id) is
3395 CS : constant Boolean := Comes_From_Source (N);
3397 begin
3398 -- This is an obsolescent feature
3400 Check_Restriction (No_Obsolescent_Features, N);
3402 if Warn_On_Obsolescent_Feature then
3403 Error_Msg_N
3404 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3405 Error_Msg_N
3406 ("\?j?use address attribute definition clause instead", N);
3407 end if;
3409 -- Rewrite as address clause
3411 Rewrite (N,
3412 Make_Attribute_Definition_Clause (Sloc (N),
3413 Name => Identifier (N),
3414 Chars => Name_Address,
3415 Expression => Expression (N)));
3417 -- We preserve Comes_From_Source, since logically the clause still comes
3418 -- from the source program even though it is changed in form.
3420 Set_Comes_From_Source (N, CS);
3422 -- Analyze rewritten clause
3424 Analyze_Attribute_Definition_Clause (N);
3425 end Analyze_At_Clause;
3427 -----------------------------------------
3428 -- Analyze_Attribute_Definition_Clause --
3429 -----------------------------------------
3431 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3432 Loc : constant Source_Ptr := Sloc (N);
3433 Nam : constant Node_Id := Name (N);
3434 Attr : constant Name_Id := Chars (N);
3435 Expr : constant Node_Id := Expression (N);
3436 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3438 Ent : Entity_Id;
3439 -- The entity of Nam after it is analyzed. In the case of an incomplete
3440 -- type, this is the underlying type.
3442 U_Ent : Entity_Id;
3443 -- The underlying entity to which the attribute applies. Generally this
3444 -- is the Underlying_Type of Ent, except in the case where the clause
3445 -- applies to full view of incomplete type or private type in which case
3446 -- U_Ent is just a copy of Ent.
3448 FOnly : Boolean := False;
3449 -- Reset to True for subtype specific attribute (Alignment, Size)
3450 -- and for stream attributes, i.e. those cases where in the call to
3451 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3452 -- are checked. Note that the case of stream attributes is not clear
3453 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3454 -- Storage_Size for derived task types, but that is also clearly
3455 -- unintentional.
3457 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3458 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3459 -- definition clauses.
3461 function Duplicate_Clause return Boolean;
3462 -- This routine checks if the aspect for U_Ent being given by attribute
3463 -- definition clause N is for an aspect that has already been specified,
3464 -- and if so gives an error message. If there is a duplicate, True is
3465 -- returned, otherwise if there is no error, False is returned.
3467 procedure Check_Indexing_Functions;
3468 -- Check that the function in Constant_Indexing or Variable_Indexing
3469 -- attribute has the proper type structure. If the name is overloaded,
3470 -- check that some interpretation is legal.
3472 procedure Check_Iterator_Functions;
3473 -- Check that there is a single function in Default_Iterator attribute
3474 -- has the proper type structure.
3476 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3477 -- Common legality check for the previous two
3479 -----------------------------------
3480 -- Analyze_Stream_TSS_Definition --
3481 -----------------------------------
3483 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3484 Subp : Entity_Id := Empty;
3485 I : Interp_Index;
3486 It : Interp;
3487 Pnam : Entity_Id;
3489 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3490 -- True for Read attribute, false for other attributes
3492 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3493 -- Return true if the entity is a subprogram with an appropriate
3494 -- profile for the attribute being defined.
3496 ----------------------
3497 -- Has_Good_Profile --
3498 ----------------------
3500 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3501 F : Entity_Id;
3502 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3503 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3504 (False => E_Procedure, True => E_Function);
3505 Typ : Entity_Id;
3507 begin
3508 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3509 return False;
3510 end if;
3512 F := First_Formal (Subp);
3514 if No (F)
3515 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3516 or else Designated_Type (Etype (F)) /=
3517 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3518 then
3519 return False;
3520 end if;
3522 if not Is_Function then
3523 Next_Formal (F);
3525 declare
3526 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3527 (False => E_In_Parameter,
3528 True => E_Out_Parameter);
3529 begin
3530 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3531 return False;
3532 end if;
3533 end;
3535 Typ := Etype (F);
3537 -- If the attribute specification comes from an aspect
3538 -- specification for a class-wide stream, the parameter must be
3539 -- a class-wide type of the entity to which the aspect applies.
3541 if From_Aspect_Specification (N)
3542 and then Class_Present (Parent (N))
3543 and then Is_Class_Wide_Type (Typ)
3544 then
3545 Typ := Etype (Typ);
3546 end if;
3548 else
3549 Typ := Etype (Subp);
3550 end if;
3552 -- Verify that the prefix of the attribute and the local name for
3553 -- the type of the formal match, or one is the class-wide of the
3554 -- other, in the case of a class-wide stream operation.
3556 if Base_Type (Typ) = Base_Type (Ent)
3557 or else (Is_Class_Wide_Type (Typ)
3558 and then Typ = Class_Wide_Type (Base_Type (Ent)))
3559 then
3560 null;
3561 else
3562 return False;
3563 end if;
3565 if Present ((Next_Formal (F)))
3566 then
3567 return False;
3569 elsif not Is_Scalar_Type (Typ)
3570 and then not Is_First_Subtype (Typ)
3571 and then not Is_Class_Wide_Type (Typ)
3572 then
3573 return False;
3575 else
3576 return True;
3577 end if;
3578 end Has_Good_Profile;
3580 -- Start of processing for Analyze_Stream_TSS_Definition
3582 begin
3583 FOnly := True;
3585 if not Is_Type (U_Ent) then
3586 Error_Msg_N ("local name must be a subtype", Nam);
3587 return;
3589 elsif not Is_First_Subtype (U_Ent) then
3590 Error_Msg_N ("local name must be a first subtype", Nam);
3591 return;
3592 end if;
3594 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3596 -- If Pnam is present, it can be either inherited from an ancestor
3597 -- type (in which case it is legal to redefine it for this type), or
3598 -- be a previous definition of the attribute for the same type (in
3599 -- which case it is illegal).
3601 -- In the first case, it will have been analyzed already, and we
3602 -- can check that its profile does not match the expected profile
3603 -- for a stream attribute of U_Ent. In the second case, either Pnam
3604 -- has been analyzed (and has the expected profile), or it has not
3605 -- been analyzed yet (case of a type that has not been frozen yet
3606 -- and for which the stream attribute has been set using Set_TSS).
3608 if Present (Pnam)
3609 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3610 then
3611 Error_Msg_Sloc := Sloc (Pnam);
3612 Error_Msg_Name_1 := Attr;
3613 Error_Msg_N ("% attribute already defined #", Nam);
3614 return;
3615 end if;
3617 Analyze (Expr);
3619 if Is_Entity_Name (Expr) then
3620 if not Is_Overloaded (Expr) then
3621 if Has_Good_Profile (Entity (Expr)) then
3622 Subp := Entity (Expr);
3623 end if;
3625 else
3626 Get_First_Interp (Expr, I, It);
3627 while Present (It.Nam) loop
3628 if Has_Good_Profile (It.Nam) then
3629 Subp := It.Nam;
3630 exit;
3631 end if;
3633 Get_Next_Interp (I, It);
3634 end loop;
3635 end if;
3636 end if;
3638 if Present (Subp) then
3639 if Is_Abstract_Subprogram (Subp) then
3640 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3641 return;
3643 -- A stream subprogram for an interface type must be a null
3644 -- procedure (RM 13.13.2 (38/3)).
3646 elsif Is_Interface (U_Ent)
3647 and then not Is_Class_Wide_Type (U_Ent)
3648 and then not Inside_A_Generic
3649 and then
3650 (Ekind (Subp) = E_Function
3651 or else
3652 not Null_Present
3653 (Specification
3654 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
3655 then
3656 Error_Msg_N
3657 ("stream subprogram for interface type "
3658 & "must be null procedure", Expr);
3659 end if;
3661 Set_Entity (Expr, Subp);
3662 Set_Etype (Expr, Etype (Subp));
3664 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3666 else
3667 Error_Msg_Name_1 := Attr;
3668 Error_Msg_N ("incorrect expression for% attribute", Expr);
3669 end if;
3670 end Analyze_Stream_TSS_Definition;
3672 ------------------------------
3673 -- Check_Indexing_Functions --
3674 ------------------------------
3676 procedure Check_Indexing_Functions is
3677 Indexing_Found : Boolean := False;
3679 procedure Check_One_Function (Subp : Entity_Id);
3680 -- Check one possible interpretation. Sets Indexing_Found True if a
3681 -- legal indexing function is found.
3683 procedure Illegal_Indexing (Msg : String);
3684 -- Diagnose illegal indexing function if not overloaded. In the
3685 -- overloaded case indicate that no legal interpretation exists.
3687 ------------------------
3688 -- Check_One_Function --
3689 ------------------------
3691 procedure Check_One_Function (Subp : Entity_Id) is
3692 Default_Element : Node_Id;
3693 Ret_Type : constant Entity_Id := Etype (Subp);
3695 begin
3696 if not Is_Overloadable (Subp) then
3697 Illegal_Indexing ("illegal indexing function for type&");
3698 return;
3700 elsif Scope (Subp) /= Scope (Ent) then
3701 if Nkind (Expr) = N_Expanded_Name then
3703 -- Indexing function can't be declared elsewhere
3705 Illegal_Indexing
3706 ("indexing function must be declared in scope of type&");
3707 end if;
3709 return;
3711 elsif No (First_Formal (Subp)) then
3712 Illegal_Indexing
3713 ("Indexing requires a function that applies to type&");
3714 return;
3716 elsif No (Next_Formal (First_Formal (Subp))) then
3717 Illegal_Indexing
3718 ("indexing function must have at least two parameters");
3719 return;
3721 elsif Is_Derived_Type (Ent) then
3722 if (Attr = Name_Constant_Indexing
3723 and then Present
3724 (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing)))
3725 or else
3726 (Attr = Name_Variable_Indexing
3727 and then Present
3728 (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing)))
3729 then
3730 if Debug_Flag_Dot_XX then
3731 null;
3733 else
3734 Illegal_Indexing
3735 ("indexing function already inherited "
3736 & "from parent type");
3737 return;
3738 end if;
3739 end if;
3740 end if;
3742 if not Check_Primitive_Function (Subp) then
3743 Illegal_Indexing
3744 ("Indexing aspect requires a function that applies to type&");
3745 return;
3746 end if;
3748 -- If partial declaration exists, verify that it is not tagged.
3750 if Ekind (Current_Scope) = E_Package
3751 and then Has_Private_Declaration (Ent)
3752 and then From_Aspect_Specification (N)
3753 and then
3754 List_Containing (Parent (Ent)) =
3755 Private_Declarations
3756 (Specification (Unit_Declaration_Node (Current_Scope)))
3757 and then Nkind (N) = N_Attribute_Definition_Clause
3758 then
3759 declare
3760 Decl : Node_Id;
3762 begin
3763 Decl :=
3764 First (Visible_Declarations
3765 (Specification
3766 (Unit_Declaration_Node (Current_Scope))));
3768 while Present (Decl) loop
3769 if Nkind (Decl) = N_Private_Type_Declaration
3770 and then Ent = Full_View (Defining_Identifier (Decl))
3771 and then Tagged_Present (Decl)
3772 and then No (Aspect_Specifications (Decl))
3773 then
3774 Illegal_Indexing
3775 ("Indexing aspect cannot be specified on full view "
3776 & "if partial view is tagged");
3777 return;
3778 end if;
3780 Next (Decl);
3781 end loop;
3782 end;
3783 end if;
3785 -- An indexing function must return either the default element of
3786 -- the container, or a reference type. For variable indexing it
3787 -- must be the latter.
3789 Default_Element :=
3790 Find_Value_Of_Aspect
3791 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3793 if Present (Default_Element) then
3794 Analyze (Default_Element);
3796 if Is_Entity_Name (Default_Element)
3797 and then not Covers (Entity (Default_Element), Ret_Type)
3798 and then False
3799 then
3800 Illegal_Indexing
3801 ("wrong return type for indexing function");
3802 return;
3803 end if;
3804 end if;
3806 -- For variable_indexing the return type must be a reference type
3808 if Attr = Name_Variable_Indexing then
3809 if not Has_Implicit_Dereference (Ret_Type) then
3810 Illegal_Indexing
3811 ("variable indexing must return a reference type");
3812 return;
3814 elsif Is_Access_Constant
3815 (Etype (First_Discriminant (Ret_Type)))
3816 then
3817 Illegal_Indexing
3818 ("variable indexing must return an access to variable");
3819 return;
3820 end if;
3822 else
3823 if Has_Implicit_Dereference (Ret_Type)
3824 and then not
3825 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3826 then
3827 Illegal_Indexing
3828 ("constant indexing must return an access to constant");
3829 return;
3831 elsif Is_Access_Type (Etype (First_Formal (Subp)))
3832 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
3833 then
3834 Illegal_Indexing
3835 ("constant indexing must apply to an access to constant");
3836 return;
3837 end if;
3838 end if;
3840 -- All checks succeeded.
3842 Indexing_Found := True;
3843 end Check_One_Function;
3845 -----------------------
3846 -- Illegal_Indexing --
3847 -----------------------
3849 procedure Illegal_Indexing (Msg : String) is
3850 begin
3851 Error_Msg_NE (Msg, N, Ent);
3852 end Illegal_Indexing;
3854 -- Start of processing for Check_Indexing_Functions
3856 begin
3857 if In_Instance then
3858 return;
3859 end if;
3861 Analyze (Expr);
3863 if not Is_Overloaded (Expr) then
3864 Check_One_Function (Entity (Expr));
3866 else
3867 declare
3868 I : Interp_Index;
3869 It : Interp;
3871 begin
3872 Indexing_Found := False;
3873 Get_First_Interp (Expr, I, It);
3874 while Present (It.Nam) loop
3876 -- Note that analysis will have added the interpretation
3877 -- that corresponds to the dereference. We only check the
3878 -- subprogram itself.
3880 if Is_Overloadable (It.Nam) then
3881 Check_One_Function (It.Nam);
3882 end if;
3884 Get_Next_Interp (I, It);
3885 end loop;
3886 end;
3887 end if;
3889 if not Indexing_Found and then not Error_Posted (N) then
3890 Error_Msg_NE
3891 ("aspect Indexing requires a local function that "
3892 & "applies to type&", Expr, Ent);
3893 end if;
3894 end Check_Indexing_Functions;
3896 ------------------------------
3897 -- Check_Iterator_Functions --
3898 ------------------------------
3900 procedure Check_Iterator_Functions is
3901 Default : Entity_Id;
3903 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3904 -- Check one possible interpretation for validity
3906 ----------------------------
3907 -- Valid_Default_Iterator --
3908 ----------------------------
3910 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3911 Formal : Entity_Id;
3913 begin
3914 if not Check_Primitive_Function (Subp) then
3915 return False;
3916 else
3917 Formal := First_Formal (Subp);
3918 end if;
3920 -- False if any subsequent formal has no default expression
3922 Formal := Next_Formal (Formal);
3923 while Present (Formal) loop
3924 if No (Expression (Parent (Formal))) then
3925 return False;
3926 end if;
3928 Next_Formal (Formal);
3929 end loop;
3931 -- True if all subsequent formals have default expressions
3933 return True;
3934 end Valid_Default_Iterator;
3936 -- Start of processing for Check_Iterator_Functions
3938 begin
3939 Analyze (Expr);
3941 if not Is_Entity_Name (Expr) then
3942 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3943 end if;
3945 if not Is_Overloaded (Expr) then
3946 if not Check_Primitive_Function (Entity (Expr)) then
3947 Error_Msg_NE
3948 ("aspect Indexing requires a function that applies to type&",
3949 Entity (Expr), Ent);
3950 end if;
3952 if not Valid_Default_Iterator (Entity (Expr)) then
3953 Error_Msg_N ("improper function for default iterator", Expr);
3954 end if;
3956 else
3957 Default := Empty;
3958 declare
3959 I : Interp_Index;
3960 It : Interp;
3962 begin
3963 Get_First_Interp (Expr, I, It);
3964 while Present (It.Nam) loop
3965 if not Check_Primitive_Function (It.Nam)
3966 or else not Valid_Default_Iterator (It.Nam)
3967 then
3968 Remove_Interp (I);
3970 elsif Present (Default) then
3971 Error_Msg_N ("default iterator must be unique", Expr);
3973 else
3974 Default := It.Nam;
3975 end if;
3977 Get_Next_Interp (I, It);
3978 end loop;
3979 end;
3981 if Present (Default) then
3982 Set_Entity (Expr, Default);
3983 Set_Is_Overloaded (Expr, False);
3984 end if;
3985 end if;
3986 end Check_Iterator_Functions;
3988 -------------------------------
3989 -- Check_Primitive_Function --
3990 -------------------------------
3992 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3993 Ctrl : Entity_Id;
3995 begin
3996 if Ekind (Subp) /= E_Function then
3997 return False;
3998 end if;
4000 if No (First_Formal (Subp)) then
4001 return False;
4002 else
4003 Ctrl := Etype (First_Formal (Subp));
4004 end if;
4006 -- Type of formal may be the class-wide type, an access to such,
4007 -- or an incomplete view.
4009 if Ctrl = Ent
4010 or else Ctrl = Class_Wide_Type (Ent)
4011 or else
4012 (Ekind (Ctrl) = E_Anonymous_Access_Type
4013 and then (Designated_Type (Ctrl) = Ent
4014 or else
4015 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4016 or else
4017 (Ekind (Ctrl) = E_Incomplete_Type
4018 and then Full_View (Ctrl) = Ent)
4019 then
4020 null;
4021 else
4022 return False;
4023 end if;
4025 return True;
4026 end Check_Primitive_Function;
4028 ----------------------
4029 -- Duplicate_Clause --
4030 ----------------------
4032 function Duplicate_Clause return Boolean is
4033 A : Node_Id;
4035 begin
4036 -- Nothing to do if this attribute definition clause comes from
4037 -- an aspect specification, since we could not be duplicating an
4038 -- explicit clause, and we dealt with the case of duplicated aspects
4039 -- in Analyze_Aspect_Specifications.
4041 if From_Aspect_Specification (N) then
4042 return False;
4043 end if;
4045 -- Otherwise current clause may duplicate previous clause, or a
4046 -- previously given pragma or aspect specification for the same
4047 -- aspect.
4049 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4051 if Present (A) then
4052 Error_Msg_Name_1 := Chars (N);
4053 Error_Msg_Sloc := Sloc (A);
4055 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4056 return True;
4057 end if;
4059 return False;
4060 end Duplicate_Clause;
4062 -- Start of processing for Analyze_Attribute_Definition_Clause
4064 begin
4065 -- The following code is a defense against recursion. Not clear that
4066 -- this can happen legitimately, but perhaps some error situations can
4067 -- cause it, and we did see this recursion during testing.
4069 if Analyzed (N) then
4070 return;
4071 else
4072 Set_Analyzed (N, True);
4073 end if;
4075 -- Ignore some selected attributes in CodePeer mode since they are not
4076 -- relevant in this context.
4078 if CodePeer_Mode then
4079 case Id is
4081 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4082 -- internal representation of types by implicitly packing them.
4084 when Attribute_Component_Size =>
4085 Rewrite (N, Make_Null_Statement (Sloc (N)));
4086 return;
4088 when others =>
4089 null;
4090 end case;
4091 end if;
4093 -- Process Ignore_Rep_Clauses option
4095 if Ignore_Rep_Clauses then
4096 case Id is
4098 -- The following should be ignored. They do not affect legality
4099 -- and may be target dependent. The basic idea of -gnatI is to
4100 -- ignore any rep clauses that may be target dependent but do not
4101 -- affect legality (except possibly to be rejected because they
4102 -- are incompatible with the compilation target).
4104 when Attribute_Alignment |
4105 Attribute_Bit_Order |
4106 Attribute_Component_Size |
4107 Attribute_Machine_Radix |
4108 Attribute_Object_Size |
4109 Attribute_Size |
4110 Attribute_Small |
4111 Attribute_Stream_Size |
4112 Attribute_Value_Size =>
4113 Kill_Rep_Clause (N);
4114 return;
4116 -- The following should not be ignored, because in the first place
4117 -- they are reasonably portable, and should not cause problems
4118 -- in compiling code from another target, and also they do affect
4119 -- legality, e.g. failing to provide a stream attribute for a type
4120 -- may make a program illegal.
4122 when Attribute_External_Tag |
4123 Attribute_Input |
4124 Attribute_Output |
4125 Attribute_Read |
4126 Attribute_Simple_Storage_Pool |
4127 Attribute_Storage_Pool |
4128 Attribute_Storage_Size |
4129 Attribute_Write =>
4130 null;
4132 -- We do not do anything here with address clauses, they will be
4133 -- removed by Freeze later on, but for now, it works better to
4134 -- keep then in the tree.
4136 when Attribute_Address =>
4137 null;
4139 -- Other cases are errors ("attribute& cannot be set with
4140 -- definition clause"), which will be caught below.
4142 when others =>
4143 null;
4144 end case;
4145 end if;
4147 Analyze (Nam);
4148 Ent := Entity (Nam);
4150 if Rep_Item_Too_Early (Ent, N) then
4151 return;
4152 end if;
4154 -- Rep clause applies to full view of incomplete type or private type if
4155 -- we have one (if not, this is a premature use of the type). However,
4156 -- certain semantic checks need to be done on the specified entity (i.e.
4157 -- the private view), so we save it in Ent.
4159 if Is_Private_Type (Ent)
4160 and then Is_Derived_Type (Ent)
4161 and then not Is_Tagged_Type (Ent)
4162 and then No (Full_View (Ent))
4163 then
4164 -- If this is a private type whose completion is a derivation from
4165 -- another private type, there is no full view, and the attribute
4166 -- belongs to the type itself, not its underlying parent.
4168 U_Ent := Ent;
4170 elsif Ekind (Ent) = E_Incomplete_Type then
4172 -- The attribute applies to the full view, set the entity of the
4173 -- attribute definition accordingly.
4175 Ent := Underlying_Type (Ent);
4176 U_Ent := Ent;
4177 Set_Entity (Nam, Ent);
4179 else
4180 U_Ent := Underlying_Type (Ent);
4181 end if;
4183 -- Avoid cascaded error
4185 if Etype (Nam) = Any_Type then
4186 return;
4188 -- Must be declared in current scope or in case of an aspect
4189 -- specification, must be visible in current scope.
4191 elsif Scope (Ent) /= Current_Scope
4192 and then
4193 not (From_Aspect_Specification (N)
4194 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4195 then
4196 Error_Msg_N ("entity must be declared in this scope", Nam);
4197 return;
4199 -- Must not be a source renaming (we do have some cases where the
4200 -- expander generates a renaming, and those cases are OK, in such
4201 -- cases any attribute applies to the renamed object as well).
4203 elsif Is_Object (Ent)
4204 and then Present (Renamed_Object (Ent))
4205 then
4206 -- Case of renamed object from source, this is an error
4208 if Comes_From_Source (Renamed_Object (Ent)) then
4209 Get_Name_String (Chars (N));
4210 Error_Msg_Strlen := Name_Len;
4211 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4212 Error_Msg_N
4213 ("~ clause not allowed for a renaming declaration "
4214 & "(RM 13.1(6))", Nam);
4215 return;
4217 -- For the case of a compiler generated renaming, the attribute
4218 -- definition clause applies to the renamed object created by the
4219 -- expander. The easiest general way to handle this is to create a
4220 -- copy of the attribute definition clause for this object.
4222 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4223 Insert_Action (N,
4224 Make_Attribute_Definition_Clause (Loc,
4225 Name =>
4226 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4227 Chars => Chars (N),
4228 Expression => Duplicate_Subexpr (Expression (N))));
4230 -- If the renamed object is not an entity, it must be a dereference
4231 -- of an unconstrained function call, and we must introduce a new
4232 -- declaration to capture the expression. This is needed in the case
4233 -- of 'Alignment, where the original declaration must be rewritten.
4235 else
4236 pragma Assert
4237 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4238 null;
4239 end if;
4241 -- If no underlying entity, use entity itself, applies to some
4242 -- previously detected error cases ???
4244 elsif No (U_Ent) then
4245 U_Ent := Ent;
4247 -- Cannot specify for a subtype (exception Object/Value_Size)
4249 elsif Is_Type (U_Ent)
4250 and then not Is_First_Subtype (U_Ent)
4251 and then Id /= Attribute_Object_Size
4252 and then Id /= Attribute_Value_Size
4253 and then not From_At_Mod (N)
4254 then
4255 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4256 return;
4257 end if;
4259 Set_Entity (N, U_Ent);
4260 Check_Restriction_No_Use_Of_Attribute (N);
4262 -- Switch on particular attribute
4264 case Id is
4266 -------------
4267 -- Address --
4268 -------------
4270 -- Address attribute definition clause
4272 when Attribute_Address => Address : begin
4274 -- A little error check, catch for X'Address use X'Address;
4276 if Nkind (Nam) = N_Identifier
4277 and then Nkind (Expr) = N_Attribute_Reference
4278 and then Attribute_Name (Expr) = Name_Address
4279 and then Nkind (Prefix (Expr)) = N_Identifier
4280 and then Chars (Nam) = Chars (Prefix (Expr))
4281 then
4282 Error_Msg_NE
4283 ("address for & is self-referencing", Prefix (Expr), Ent);
4284 return;
4285 end if;
4287 -- Not that special case, carry on with analysis of expression
4289 Analyze_And_Resolve (Expr, RTE (RE_Address));
4291 -- Even when ignoring rep clauses we need to indicate that the
4292 -- entity has an address clause and thus it is legal to declare
4293 -- it imported. Freeze will get rid of the address clause later.
4295 if Ignore_Rep_Clauses then
4296 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4297 Record_Rep_Item (U_Ent, N);
4298 end if;
4300 return;
4301 end if;
4303 if Duplicate_Clause then
4304 null;
4306 -- Case of address clause for subprogram
4308 elsif Is_Subprogram (U_Ent) then
4309 if Has_Homonym (U_Ent) then
4310 Error_Msg_N
4311 ("address clause cannot be given " &
4312 "for overloaded subprogram",
4313 Nam);
4314 return;
4315 end if;
4317 -- For subprograms, all address clauses are permitted, and we
4318 -- mark the subprogram as having a deferred freeze so that Gigi
4319 -- will not elaborate it too soon.
4321 -- Above needs more comments, what is too soon about???
4323 Set_Has_Delayed_Freeze (U_Ent);
4325 -- Case of address clause for entry
4327 elsif Ekind (U_Ent) = E_Entry then
4328 if Nkind (Parent (N)) = N_Task_Body then
4329 Error_Msg_N
4330 ("entry address must be specified in task spec", Nam);
4331 return;
4332 end if;
4334 -- For entries, we require a constant address
4336 Check_Constant_Address_Clause (Expr, U_Ent);
4338 -- Special checks for task types
4340 if Is_Task_Type (Scope (U_Ent))
4341 and then Comes_From_Source (Scope (U_Ent))
4342 then
4343 Error_Msg_N
4344 ("??entry address declared for entry in task type", N);
4345 Error_Msg_N
4346 ("\??only one task can be declared of this type", N);
4347 end if;
4349 -- Entry address clauses are obsolescent
4351 Check_Restriction (No_Obsolescent_Features, N);
4353 if Warn_On_Obsolescent_Feature then
4354 Error_Msg_N
4355 ("?j?attaching interrupt to task entry is an " &
4356 "obsolescent feature (RM J.7.1)", N);
4357 Error_Msg_N
4358 ("\?j?use interrupt procedure instead", N);
4359 end if;
4361 -- Case of an address clause for a controlled object which we
4362 -- consider to be erroneous.
4364 elsif Is_Controlled (Etype (U_Ent))
4365 or else Has_Controlled_Component (Etype (U_Ent))
4366 then
4367 Error_Msg_NE
4368 ("??controlled object& must not be overlaid", Nam, U_Ent);
4369 Error_Msg_N
4370 ("\??Program_Error will be raised at run time", Nam);
4371 Insert_Action (Declaration_Node (U_Ent),
4372 Make_Raise_Program_Error (Loc,
4373 Reason => PE_Overlaid_Controlled_Object));
4374 return;
4376 -- Case of address clause for a (non-controlled) object
4378 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4379 declare
4380 Expr : constant Node_Id := Expression (N);
4381 O_Ent : Entity_Id;
4382 Off : Boolean;
4384 begin
4385 -- Exported variables cannot have an address clause, because
4386 -- this cancels the effect of the pragma Export.
4388 if Is_Exported (U_Ent) then
4389 Error_Msg_N
4390 ("cannot export object with address clause", Nam);
4391 return;
4392 end if;
4394 Find_Overlaid_Entity (N, O_Ent, Off);
4396 -- Overlaying controlled objects is erroneous
4398 if Present (O_Ent)
4399 and then (Has_Controlled_Component (Etype (O_Ent))
4400 or else Is_Controlled (Etype (O_Ent)))
4401 then
4402 Error_Msg_N
4403 ("??cannot overlay with controlled object", Expr);
4404 Error_Msg_N
4405 ("\??Program_Error will be raised at run time", Expr);
4406 Insert_Action (Declaration_Node (U_Ent),
4407 Make_Raise_Program_Error (Loc,
4408 Reason => PE_Overlaid_Controlled_Object));
4409 return;
4411 elsif Present (O_Ent)
4412 and then Ekind (U_Ent) = E_Constant
4413 and then not Is_Constant_Object (O_Ent)
4414 then
4415 Error_Msg_N ("??constant overlays a variable", Expr);
4417 -- Imported variables can have an address clause, but then
4418 -- the import is pretty meaningless except to suppress
4419 -- initializations, so we do not need such variables to
4420 -- be statically allocated (and in fact it causes trouble
4421 -- if the address clause is a local value).
4423 elsif Is_Imported (U_Ent) then
4424 Set_Is_Statically_Allocated (U_Ent, False);
4425 end if;
4427 -- We mark a possible modification of a variable with an
4428 -- address clause, since it is likely aliasing is occurring.
4430 Note_Possible_Modification (Nam, Sure => False);
4432 -- Here we are checking for explicit overlap of one variable
4433 -- by another, and if we find this then mark the overlapped
4434 -- variable as also being volatile to prevent unwanted
4435 -- optimizations. This is a significant pessimization so
4436 -- avoid it when there is an offset, i.e. when the object
4437 -- is composite; they cannot be optimized easily anyway.
4439 if Present (O_Ent)
4440 and then Is_Object (O_Ent)
4441 and then not Off
4443 -- The following test is an expedient solution to what
4444 -- is really a problem in CodePeer. Suppressing the
4445 -- Set_Treat_As_Volatile call here prevents later
4446 -- generation (in some cases) of trees that CodePeer
4447 -- should, but currently does not, handle correctly.
4448 -- This test should probably be removed when CodePeer
4449 -- is improved, just because we want the tree CodePeer
4450 -- analyzes to match the tree for which we generate code
4451 -- as closely as is practical. ???
4453 and then not CodePeer_Mode
4454 then
4455 -- ??? O_Ent might not be in current unit
4457 Set_Treat_As_Volatile (O_Ent);
4458 end if;
4460 -- Legality checks on the address clause for initialized
4461 -- objects is deferred until the freeze point, because
4462 -- a subsequent pragma might indicate that the object
4463 -- is imported and thus not initialized. Also, the address
4464 -- clause might involve entities that have yet to be
4465 -- elaborated.
4467 Set_Has_Delayed_Freeze (U_Ent);
4469 -- If an initialization call has been generated for this
4470 -- object, it needs to be deferred to after the freeze node
4471 -- we have just now added, otherwise GIGI will see a
4472 -- reference to the variable (as actual to the IP call)
4473 -- before its definition.
4475 declare
4476 Init_Call : constant Node_Id :=
4477 Remove_Init_Call (U_Ent, N);
4479 begin
4480 if Present (Init_Call) then
4481 Append_Freeze_Action (U_Ent, Init_Call);
4483 -- Reset Initialization_Statements pointer so that
4484 -- if there is a pragma Import further down, it can
4485 -- clear any default initialization.
4487 Set_Initialization_Statements (U_Ent, Init_Call);
4488 end if;
4489 end;
4491 if Is_Exported (U_Ent) then
4492 Error_Msg_N
4493 ("& cannot be exported if an address clause is given",
4494 Nam);
4495 Error_Msg_N
4496 ("\define and export a variable "
4497 & "that holds its address instead", Nam);
4498 end if;
4500 -- Entity has delayed freeze, so we will generate an
4501 -- alignment check at the freeze point unless suppressed.
4503 if not Range_Checks_Suppressed (U_Ent)
4504 and then not Alignment_Checks_Suppressed (U_Ent)
4505 then
4506 Set_Check_Address_Alignment (N);
4507 end if;
4509 -- Kill the size check code, since we are not allocating
4510 -- the variable, it is somewhere else.
4512 Kill_Size_Check_Code (U_Ent);
4514 -- If the address clause is of the form:
4516 -- for Y'Address use X'Address
4518 -- or
4520 -- Const : constant Address := X'Address;
4521 -- ...
4522 -- for Y'Address use Const;
4524 -- then we make an entry in the table for checking the size
4525 -- and alignment of the overlaying variable. We defer this
4526 -- check till after code generation to take full advantage
4527 -- of the annotation done by the back end.
4529 -- If the entity has a generic type, the check will be
4530 -- performed in the instance if the actual type justifies
4531 -- it, and we do not insert the clause in the table to
4532 -- prevent spurious warnings.
4534 -- Note: we used to test Comes_From_Source and only give
4535 -- this warning for source entities, but we have removed
4536 -- this test. It really seems bogus to generate overlays
4537 -- that would trigger this warning in generated code.
4538 -- Furthermore, by removing the test, we handle the
4539 -- aspect case properly.
4541 if Address_Clause_Overlay_Warnings
4542 and then Present (O_Ent)
4543 and then Is_Object (O_Ent)
4544 then
4545 if not Is_Generic_Type (Etype (U_Ent)) then
4546 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4547 end if;
4549 -- If variable overlays a constant view, and we are
4550 -- warning on overlays, then mark the variable as
4551 -- overlaying a constant (we will give warnings later
4552 -- if this variable is assigned).
4554 if Is_Constant_Object (O_Ent)
4555 and then Ekind (U_Ent) = E_Variable
4556 then
4557 Set_Overlays_Constant (U_Ent);
4558 end if;
4559 end if;
4560 end;
4562 -- Not a valid entity for an address clause
4564 else
4565 Error_Msg_N ("address cannot be given for &", Nam);
4566 end if;
4567 end Address;
4569 ---------------
4570 -- Alignment --
4571 ---------------
4573 -- Alignment attribute definition clause
4575 when Attribute_Alignment => Alignment : declare
4576 Align : constant Uint := Get_Alignment_Value (Expr);
4577 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4579 begin
4580 FOnly := True;
4582 if not Is_Type (U_Ent)
4583 and then Ekind (U_Ent) /= E_Variable
4584 and then Ekind (U_Ent) /= E_Constant
4585 then
4586 Error_Msg_N ("alignment cannot be given for &", Nam);
4588 elsif Duplicate_Clause then
4589 null;
4591 elsif Align /= No_Uint then
4592 Set_Has_Alignment_Clause (U_Ent);
4594 -- Tagged type case, check for attempt to set alignment to a
4595 -- value greater than Max_Align, and reset if so.
4597 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4598 Error_Msg_N
4599 ("alignment for & set to Maximum_Aligment??", Nam);
4600 Set_Alignment (U_Ent, Max_Align);
4602 -- All other cases
4604 else
4605 Set_Alignment (U_Ent, Align);
4606 end if;
4608 -- For an array type, U_Ent is the first subtype. In that case,
4609 -- also set the alignment of the anonymous base type so that
4610 -- other subtypes (such as the itypes for aggregates of the
4611 -- type) also receive the expected alignment.
4613 if Is_Array_Type (U_Ent) then
4614 Set_Alignment (Base_Type (U_Ent), Align);
4615 end if;
4616 end if;
4617 end Alignment;
4619 ---------------
4620 -- Bit_Order --
4621 ---------------
4623 -- Bit_Order attribute definition clause
4625 when Attribute_Bit_Order => Bit_Order : declare
4626 begin
4627 if not Is_Record_Type (U_Ent) then
4628 Error_Msg_N
4629 ("Bit_Order can only be defined for record type", Nam);
4631 elsif Duplicate_Clause then
4632 null;
4634 else
4635 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4637 if Etype (Expr) = Any_Type then
4638 return;
4640 elsif not Is_OK_Static_Expression (Expr) then
4641 Flag_Non_Static_Expr
4642 ("Bit_Order requires static expression!", Expr);
4644 else
4645 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4646 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4647 end if;
4648 end if;
4649 end if;
4650 end Bit_Order;
4652 --------------------
4653 -- Component_Size --
4654 --------------------
4656 -- Component_Size attribute definition clause
4658 when Attribute_Component_Size => Component_Size_Case : declare
4659 Csize : constant Uint := Static_Integer (Expr);
4660 Ctyp : Entity_Id;
4661 Btype : Entity_Id;
4662 Biased : Boolean;
4663 New_Ctyp : Entity_Id;
4664 Decl : Node_Id;
4666 begin
4667 if not Is_Array_Type (U_Ent) then
4668 Error_Msg_N ("component size requires array type", Nam);
4669 return;
4670 end if;
4672 Btype := Base_Type (U_Ent);
4673 Ctyp := Component_Type (Btype);
4675 if Duplicate_Clause then
4676 null;
4678 elsif Rep_Item_Too_Early (Btype, N) then
4679 null;
4681 elsif Csize /= No_Uint then
4682 Check_Size (Expr, Ctyp, Csize, Biased);
4684 -- For the biased case, build a declaration for a subtype that
4685 -- will be used to represent the biased subtype that reflects
4686 -- the biased representation of components. We need the subtype
4687 -- to get proper conversions on referencing elements of the
4688 -- array. Note: component size clauses are ignored in VM mode.
4690 if VM_Target = No_VM then
4691 if Biased then
4692 New_Ctyp :=
4693 Make_Defining_Identifier (Loc,
4694 Chars =>
4695 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4697 Decl :=
4698 Make_Subtype_Declaration (Loc,
4699 Defining_Identifier => New_Ctyp,
4700 Subtype_Indication =>
4701 New_Occurrence_Of (Component_Type (Btype), Loc));
4703 Set_Parent (Decl, N);
4704 Analyze (Decl, Suppress => All_Checks);
4706 Set_Has_Delayed_Freeze (New_Ctyp, False);
4707 Set_Esize (New_Ctyp, Csize);
4708 Set_RM_Size (New_Ctyp, Csize);
4709 Init_Alignment (New_Ctyp);
4710 Set_Is_Itype (New_Ctyp, True);
4711 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4713 Set_Component_Type (Btype, New_Ctyp);
4714 Set_Biased (New_Ctyp, N, "component size clause");
4715 end if;
4717 Set_Component_Size (Btype, Csize);
4719 -- For VM case, we ignore component size clauses
4721 else
4722 -- Give a warning unless we are in GNAT mode, in which case
4723 -- the warning is suppressed since it is not useful.
4725 if not GNAT_Mode then
4726 Error_Msg_N
4727 ("component size ignored in this configuration??", N);
4728 end if;
4729 end if;
4731 -- Deal with warning on overridden size
4733 if Warn_On_Overridden_Size
4734 and then Has_Size_Clause (Ctyp)
4735 and then RM_Size (Ctyp) /= Csize
4736 then
4737 Error_Msg_NE
4738 ("component size overrides size clause for&?S?", N, Ctyp);
4739 end if;
4741 Set_Has_Component_Size_Clause (Btype, True);
4742 Set_Has_Non_Standard_Rep (Btype, True);
4743 end if;
4744 end Component_Size_Case;
4746 -----------------------
4747 -- Constant_Indexing --
4748 -----------------------
4750 when Attribute_Constant_Indexing =>
4751 Check_Indexing_Functions;
4753 ---------
4754 -- CPU --
4755 ---------
4757 when Attribute_CPU => CPU :
4758 begin
4759 -- CPU attribute definition clause not allowed except from aspect
4760 -- specification.
4762 if From_Aspect_Specification (N) then
4763 if not Is_Task_Type (U_Ent) then
4764 Error_Msg_N ("CPU can only be defined for task", Nam);
4766 elsif Duplicate_Clause then
4767 null;
4769 else
4770 -- The expression must be analyzed in the special manner
4771 -- described in "Handling of Default and Per-Object
4772 -- Expressions" in sem.ads.
4774 -- The visibility to the discriminants must be restored
4776 Push_Scope_And_Install_Discriminants (U_Ent);
4777 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4778 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4780 if not Is_OK_Static_Expression (Expr) then
4781 Check_Restriction (Static_Priorities, Expr);
4782 end if;
4783 end if;
4785 else
4786 Error_Msg_N
4787 ("attribute& cannot be set with definition clause", N);
4788 end if;
4789 end CPU;
4791 ----------------------
4792 -- Default_Iterator --
4793 ----------------------
4795 when Attribute_Default_Iterator => Default_Iterator : declare
4796 Func : Entity_Id;
4798 begin
4799 if not Is_Tagged_Type (U_Ent) then
4800 Error_Msg_N
4801 ("aspect Default_Iterator applies to tagged type", Nam);
4802 end if;
4804 Check_Iterator_Functions;
4806 Analyze (Expr);
4808 if not Is_Entity_Name (Expr)
4809 or else Ekind (Entity (Expr)) /= E_Function
4810 then
4811 Error_Msg_N ("aspect Iterator must be a function", Expr);
4812 else
4813 Func := Entity (Expr);
4814 end if;
4816 if No (First_Formal (Func))
4817 or else Etype (First_Formal (Func)) /= U_Ent
4818 then
4819 Error_Msg_NE
4820 ("Default Iterator must be a primitive of&", Func, U_Ent);
4821 end if;
4822 end Default_Iterator;
4824 ------------------------
4825 -- Dispatching_Domain --
4826 ------------------------
4828 when Attribute_Dispatching_Domain => Dispatching_Domain :
4829 begin
4830 -- Dispatching_Domain attribute definition clause not allowed
4831 -- except from aspect specification.
4833 if From_Aspect_Specification (N) then
4834 if not Is_Task_Type (U_Ent) then
4835 Error_Msg_N ("Dispatching_Domain can only be defined" &
4836 "for task",
4837 Nam);
4839 elsif Duplicate_Clause then
4840 null;
4842 else
4843 -- The expression must be analyzed in the special manner
4844 -- described in "Handling of Default and Per-Object
4845 -- Expressions" in sem.ads.
4847 -- The visibility to the discriminants must be restored
4849 Push_Scope_And_Install_Discriminants (U_Ent);
4851 Preanalyze_Spec_Expression
4852 (Expr, RTE (RE_Dispatching_Domain));
4854 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4855 end if;
4857 else
4858 Error_Msg_N
4859 ("attribute& cannot be set with definition clause", N);
4860 end if;
4861 end Dispatching_Domain;
4863 ------------------
4864 -- External_Tag --
4865 ------------------
4867 when Attribute_External_Tag => External_Tag :
4868 begin
4869 if not Is_Tagged_Type (U_Ent) then
4870 Error_Msg_N ("should be a tagged type", Nam);
4871 end if;
4873 if Duplicate_Clause then
4874 null;
4876 else
4877 Analyze_And_Resolve (Expr, Standard_String);
4879 if not Is_OK_Static_Expression (Expr) then
4880 Flag_Non_Static_Expr
4881 ("static string required for tag name!", Nam);
4882 end if;
4884 if VM_Target /= No_VM then
4885 Error_Msg_Name_1 := Attr;
4886 Error_Msg_N
4887 ("% attribute unsupported in this configuration", Nam);
4888 end if;
4890 if not Is_Library_Level_Entity (U_Ent) then
4891 Error_Msg_NE
4892 ("??non-unique external tag supplied for &", N, U_Ent);
4893 Error_Msg_N
4894 ("\??same external tag applies to all "
4895 & "subprogram calls", N);
4896 Error_Msg_N
4897 ("\??corresponding internal tag cannot be obtained", N);
4898 end if;
4899 end if;
4900 end External_Tag;
4902 --------------------------
4903 -- Implicit_Dereference --
4904 --------------------------
4906 when Attribute_Implicit_Dereference =>
4908 -- Legality checks already performed at the point of the type
4909 -- declaration, aspect is not delayed.
4911 null;
4913 -----------
4914 -- Input --
4915 -----------
4917 when Attribute_Input =>
4918 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4919 Set_Has_Specified_Stream_Input (Ent);
4921 ------------------------
4922 -- Interrupt_Priority --
4923 ------------------------
4925 when Attribute_Interrupt_Priority => Interrupt_Priority :
4926 begin
4927 -- Interrupt_Priority attribute definition clause not allowed
4928 -- except from aspect specification.
4930 if From_Aspect_Specification (N) then
4931 if not Is_Concurrent_Type (U_Ent) then
4932 Error_Msg_N
4933 ("Interrupt_Priority can only be defined for task "
4934 & "and protected object", Nam);
4936 elsif Duplicate_Clause then
4937 null;
4939 else
4940 -- The expression must be analyzed in the special manner
4941 -- described in "Handling of Default and Per-Object
4942 -- Expressions" in sem.ads.
4944 -- The visibility to the discriminants must be restored
4946 Push_Scope_And_Install_Discriminants (U_Ent);
4948 Preanalyze_Spec_Expression
4949 (Expr, RTE (RE_Interrupt_Priority));
4951 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4952 end if;
4954 else
4955 Error_Msg_N
4956 ("attribute& cannot be set with definition clause", N);
4957 end if;
4958 end Interrupt_Priority;
4960 --------------
4961 -- Iterable --
4962 --------------
4964 when Attribute_Iterable =>
4965 Analyze (Expr);
4967 if Nkind (Expr) /= N_Aggregate then
4968 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
4969 end if;
4971 declare
4972 Assoc : Node_Id;
4974 begin
4975 Assoc := First (Component_Associations (Expr));
4976 while Present (Assoc) loop
4977 if not Is_Entity_Name (Expression (Assoc)) then
4978 Error_Msg_N ("value must be a function", Assoc);
4979 end if;
4981 Next (Assoc);
4982 end loop;
4983 end;
4985 ----------------------
4986 -- Iterator_Element --
4987 ----------------------
4989 when Attribute_Iterator_Element =>
4990 Analyze (Expr);
4992 if not Is_Entity_Name (Expr)
4993 or else not Is_Type (Entity (Expr))
4994 then
4995 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4996 end if;
4998 -------------------
4999 -- Machine_Radix --
5000 -------------------
5002 -- Machine radix attribute definition clause
5004 when Attribute_Machine_Radix => Machine_Radix : declare
5005 Radix : constant Uint := Static_Integer (Expr);
5007 begin
5008 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5009 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5011 elsif Duplicate_Clause then
5012 null;
5014 elsif Radix /= No_Uint then
5015 Set_Has_Machine_Radix_Clause (U_Ent);
5016 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5018 if Radix = 2 then
5019 null;
5020 elsif Radix = 10 then
5021 Set_Machine_Radix_10 (U_Ent);
5022 else
5023 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5024 end if;
5025 end if;
5026 end Machine_Radix;
5028 -----------------
5029 -- Object_Size --
5030 -----------------
5032 -- Object_Size attribute definition clause
5034 when Attribute_Object_Size => Object_Size : declare
5035 Size : constant Uint := Static_Integer (Expr);
5037 Biased : Boolean;
5038 pragma Warnings (Off, Biased);
5040 begin
5041 if not Is_Type (U_Ent) then
5042 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5044 elsif Duplicate_Clause then
5045 null;
5047 else
5048 Check_Size (Expr, U_Ent, Size, Biased);
5050 if Is_Scalar_Type (U_Ent) then
5051 if Size /= 8 and then Size /= 16 and then Size /= 32
5052 and then UI_Mod (Size, 64) /= 0
5053 then
5054 Error_Msg_N
5055 ("Object_Size must be 8, 16, 32, or multiple of 64",
5056 Expr);
5057 end if;
5059 elsif Size mod 8 /= 0 then
5060 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5061 end if;
5063 Set_Esize (U_Ent, Size);
5064 Set_Has_Object_Size_Clause (U_Ent);
5065 Alignment_Check_For_Size_Change (U_Ent, Size);
5066 end if;
5067 end Object_Size;
5069 ------------
5070 -- Output --
5071 ------------
5073 when Attribute_Output =>
5074 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5075 Set_Has_Specified_Stream_Output (Ent);
5077 --------------
5078 -- Priority --
5079 --------------
5081 when Attribute_Priority => Priority :
5082 begin
5083 -- Priority attribute definition clause not allowed except from
5084 -- aspect specification.
5086 if From_Aspect_Specification (N) then
5087 if not (Is_Concurrent_Type (U_Ent)
5088 or else Ekind (U_Ent) = E_Procedure)
5089 then
5090 Error_Msg_N
5091 ("Priority can only be defined for task and protected "
5092 & "object", Nam);
5094 elsif Duplicate_Clause then
5095 null;
5097 else
5098 -- The expression must be analyzed in the special manner
5099 -- described in "Handling of Default and Per-Object
5100 -- Expressions" in sem.ads.
5102 -- The visibility to the discriminants must be restored
5104 Push_Scope_And_Install_Discriminants (U_Ent);
5105 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5106 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5108 if not Is_OK_Static_Expression (Expr) then
5109 Check_Restriction (Static_Priorities, Expr);
5110 end if;
5111 end if;
5113 else
5114 Error_Msg_N
5115 ("attribute& cannot be set with definition clause", N);
5116 end if;
5117 end Priority;
5119 ----------
5120 -- Read --
5121 ----------
5123 when Attribute_Read =>
5124 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5125 Set_Has_Specified_Stream_Read (Ent);
5127 --------------------------
5128 -- Scalar_Storage_Order --
5129 --------------------------
5131 -- Scalar_Storage_Order attribute definition clause
5133 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5134 begin
5135 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5136 Error_Msg_N
5137 ("Scalar_Storage_Order can only be defined for "
5138 & "record or array type", Nam);
5140 elsif Duplicate_Clause then
5141 null;
5143 else
5144 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5146 if Etype (Expr) = Any_Type then
5147 return;
5149 elsif not Is_OK_Static_Expression (Expr) then
5150 Flag_Non_Static_Expr
5151 ("Scalar_Storage_Order requires static expression!", Expr);
5153 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5155 -- Here for the case of a non-default (i.e. non-confirming)
5156 -- Scalar_Storage_Order attribute definition.
5158 if Support_Nondefault_SSO_On_Target then
5159 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5160 else
5161 Error_Msg_N
5162 ("non-default Scalar_Storage_Order "
5163 & "not supported on target", Expr);
5164 end if;
5165 end if;
5167 -- Clear SSO default indications since explicit setting of the
5168 -- order overrides the defaults.
5170 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5171 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5172 end if;
5173 end Scalar_Storage_Order;
5175 ----------
5176 -- Size --
5177 ----------
5179 -- Size attribute definition clause
5181 when Attribute_Size => Size : declare
5182 Size : constant Uint := Static_Integer (Expr);
5183 Etyp : Entity_Id;
5184 Biased : Boolean;
5186 begin
5187 FOnly := True;
5189 if Duplicate_Clause then
5190 null;
5192 elsif not Is_Type (U_Ent)
5193 and then Ekind (U_Ent) /= E_Variable
5194 and then Ekind (U_Ent) /= E_Constant
5195 then
5196 Error_Msg_N ("size cannot be given for &", Nam);
5198 elsif Is_Array_Type (U_Ent)
5199 and then not Is_Constrained (U_Ent)
5200 then
5201 Error_Msg_N
5202 ("size cannot be given for unconstrained array", Nam);
5204 elsif Size /= No_Uint then
5205 if VM_Target /= No_VM and then not GNAT_Mode then
5207 -- Size clause is not handled properly on VM targets.
5208 -- Display a warning unless we are in GNAT mode, in which
5209 -- case this is useless.
5211 Error_Msg_N
5212 ("size clauses are ignored in this configuration??", N);
5213 end if;
5215 if Is_Type (U_Ent) then
5216 Etyp := U_Ent;
5217 else
5218 Etyp := Etype (U_Ent);
5219 end if;
5221 -- Check size, note that Gigi is in charge of checking that the
5222 -- size of an array or record type is OK. Also we do not check
5223 -- the size in the ordinary fixed-point case, since it is too
5224 -- early to do so (there may be subsequent small clause that
5225 -- affects the size). We can check the size if a small clause
5226 -- has already been given.
5228 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5229 or else Has_Small_Clause (U_Ent)
5230 then
5231 Check_Size (Expr, Etyp, Size, Biased);
5232 Set_Biased (U_Ent, N, "size clause", Biased);
5233 end if;
5235 -- For types set RM_Size and Esize if possible
5237 if Is_Type (U_Ent) then
5238 Set_RM_Size (U_Ent, Size);
5240 -- For elementary types, increase Object_Size to power of 2,
5241 -- but not less than a storage unit in any case (normally
5242 -- this means it will be byte addressable).
5244 -- For all other types, nothing else to do, we leave Esize
5245 -- (object size) unset, the back end will set it from the
5246 -- size and alignment in an appropriate manner.
5248 -- In both cases, we check whether the alignment must be
5249 -- reset in the wake of the size change.
5251 if Is_Elementary_Type (U_Ent) then
5252 if Size <= System_Storage_Unit then
5253 Init_Esize (U_Ent, System_Storage_Unit);
5254 elsif Size <= 16 then
5255 Init_Esize (U_Ent, 16);
5256 elsif Size <= 32 then
5257 Init_Esize (U_Ent, 32);
5258 else
5259 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5260 end if;
5262 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5263 else
5264 Alignment_Check_For_Size_Change (U_Ent, Size);
5265 end if;
5267 -- For objects, set Esize only
5269 else
5270 if Is_Elementary_Type (Etyp) then
5271 if Size /= System_Storage_Unit
5272 and then
5273 Size /= System_Storage_Unit * 2
5274 and then
5275 Size /= System_Storage_Unit * 4
5276 and then
5277 Size /= System_Storage_Unit * 8
5278 then
5279 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5280 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5281 Error_Msg_N
5282 ("size for primitive object must be a power of 2"
5283 & " in the range ^-^", N);
5284 end if;
5285 end if;
5287 Set_Esize (U_Ent, Size);
5288 end if;
5290 Set_Has_Size_Clause (U_Ent);
5291 end if;
5292 end Size;
5294 -----------
5295 -- Small --
5296 -----------
5298 -- Small attribute definition clause
5300 when Attribute_Small => Small : declare
5301 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5302 Small : Ureal;
5304 begin
5305 Analyze_And_Resolve (Expr, Any_Real);
5307 if Etype (Expr) = Any_Type then
5308 return;
5310 elsif not Is_OK_Static_Expression (Expr) then
5311 Flag_Non_Static_Expr
5312 ("small requires static expression!", Expr);
5313 return;
5315 else
5316 Small := Expr_Value_R (Expr);
5318 if Small <= Ureal_0 then
5319 Error_Msg_N ("small value must be greater than zero", Expr);
5320 return;
5321 end if;
5323 end if;
5325 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5326 Error_Msg_N
5327 ("small requires an ordinary fixed point type", Nam);
5329 elsif Has_Small_Clause (U_Ent) then
5330 Error_Msg_N ("small already given for &", Nam);
5332 elsif Small > Delta_Value (U_Ent) then
5333 Error_Msg_N
5334 ("small value must not be greater than delta value", Nam);
5336 else
5337 Set_Small_Value (U_Ent, Small);
5338 Set_Small_Value (Implicit_Base, Small);
5339 Set_Has_Small_Clause (U_Ent);
5340 Set_Has_Small_Clause (Implicit_Base);
5341 Set_Has_Non_Standard_Rep (Implicit_Base);
5342 end if;
5343 end Small;
5345 ------------------
5346 -- Storage_Pool --
5347 ------------------
5349 -- Storage_Pool attribute definition clause
5351 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5352 Pool : Entity_Id;
5353 T : Entity_Id;
5355 begin
5356 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5357 Error_Msg_N
5358 ("storage pool cannot be given for access-to-subprogram type",
5359 Nam);
5360 return;
5362 elsif not
5363 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5364 then
5365 Error_Msg_N
5366 ("storage pool can only be given for access types", Nam);
5367 return;
5369 elsif Is_Derived_Type (U_Ent) then
5370 Error_Msg_N
5371 ("storage pool cannot be given for a derived access type",
5372 Nam);
5374 elsif Duplicate_Clause then
5375 return;
5377 elsif Present (Associated_Storage_Pool (U_Ent)) then
5378 Error_Msg_N ("storage pool already given for &", Nam);
5379 return;
5380 end if;
5382 -- Check for Storage_Size previously given
5384 declare
5385 SS : constant Node_Id :=
5386 Get_Attribute_Definition_Clause
5387 (U_Ent, Attribute_Storage_Size);
5388 begin
5389 if Present (SS) then
5390 Check_Pool_Size_Clash (U_Ent, N, SS);
5391 end if;
5392 end;
5394 -- Storage_Pool case
5396 if Id = Attribute_Storage_Pool then
5397 Analyze_And_Resolve
5398 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5400 -- In the Simple_Storage_Pool case, we allow a variable of any
5401 -- simple storage pool type, so we Resolve without imposing an
5402 -- expected type.
5404 else
5405 Analyze_And_Resolve (Expr);
5407 if not Present (Get_Rep_Pragma
5408 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5409 then
5410 Error_Msg_N
5411 ("expression must be of a simple storage pool type", Expr);
5412 end if;
5413 end if;
5415 if not Denotes_Variable (Expr) then
5416 Error_Msg_N ("storage pool must be a variable", Expr);
5417 return;
5418 end if;
5420 if Nkind (Expr) = N_Type_Conversion then
5421 T := Etype (Expression (Expr));
5422 else
5423 T := Etype (Expr);
5424 end if;
5426 -- The Stack_Bounded_Pool is used internally for implementing
5427 -- access types with a Storage_Size. Since it only work properly
5428 -- when used on one specific type, we need to check that it is not
5429 -- hijacked improperly:
5431 -- type T is access Integer;
5432 -- for T'Storage_Size use n;
5433 -- type Q is access Float;
5434 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5436 if RTE_Available (RE_Stack_Bounded_Pool)
5437 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5438 then
5439 Error_Msg_N ("non-shareable internal Pool", Expr);
5440 return;
5441 end if;
5443 -- If the argument is a name that is not an entity name, then
5444 -- we construct a renaming operation to define an entity of
5445 -- type storage pool.
5447 if not Is_Entity_Name (Expr)
5448 and then Is_Object_Reference (Expr)
5449 then
5450 Pool := Make_Temporary (Loc, 'P', Expr);
5452 declare
5453 Rnode : constant Node_Id :=
5454 Make_Object_Renaming_Declaration (Loc,
5455 Defining_Identifier => Pool,
5456 Subtype_Mark =>
5457 New_Occurrence_Of (Etype (Expr), Loc),
5458 Name => Expr);
5460 begin
5461 -- If the attribute definition clause comes from an aspect
5462 -- clause, then insert the renaming before the associated
5463 -- entity's declaration, since the attribute clause has
5464 -- not yet been appended to the declaration list.
5466 if From_Aspect_Specification (N) then
5467 Insert_Before (Parent (Entity (N)), Rnode);
5468 else
5469 Insert_Before (N, Rnode);
5470 end if;
5472 Analyze (Rnode);
5473 Set_Associated_Storage_Pool (U_Ent, Pool);
5474 end;
5476 elsif Is_Entity_Name (Expr) then
5477 Pool := Entity (Expr);
5479 -- If pool is a renamed object, get original one. This can
5480 -- happen with an explicit renaming, and within instances.
5482 while Present (Renamed_Object (Pool))
5483 and then Is_Entity_Name (Renamed_Object (Pool))
5484 loop
5485 Pool := Entity (Renamed_Object (Pool));
5486 end loop;
5488 if Present (Renamed_Object (Pool))
5489 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5490 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5491 then
5492 Pool := Entity (Expression (Renamed_Object (Pool)));
5493 end if;
5495 Set_Associated_Storage_Pool (U_Ent, Pool);
5497 elsif Nkind (Expr) = N_Type_Conversion
5498 and then Is_Entity_Name (Expression (Expr))
5499 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5500 then
5501 Pool := Entity (Expression (Expr));
5502 Set_Associated_Storage_Pool (U_Ent, Pool);
5504 else
5505 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5506 return;
5507 end if;
5508 end;
5510 ------------------
5511 -- Storage_Size --
5512 ------------------
5514 -- Storage_Size attribute definition clause
5516 when Attribute_Storage_Size => Storage_Size : declare
5517 Btype : constant Entity_Id := Base_Type (U_Ent);
5519 begin
5520 if Is_Task_Type (U_Ent) then
5522 -- Check obsolescent (but never obsolescent if from aspect)
5524 if not From_Aspect_Specification (N) then
5525 Check_Restriction (No_Obsolescent_Features, N);
5527 if Warn_On_Obsolescent_Feature then
5528 Error_Msg_N
5529 ("?j?storage size clause for task is an " &
5530 "obsolescent feature (RM J.9)", N);
5531 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5532 end if;
5533 end if;
5535 FOnly := True;
5536 end if;
5538 if not Is_Access_Type (U_Ent)
5539 and then Ekind (U_Ent) /= E_Task_Type
5540 then
5541 Error_Msg_N ("storage size cannot be given for &", Nam);
5543 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5544 Error_Msg_N
5545 ("storage size cannot be given for a derived access type",
5546 Nam);
5548 elsif Duplicate_Clause then
5549 null;
5551 else
5552 Analyze_And_Resolve (Expr, Any_Integer);
5554 if Is_Access_Type (U_Ent) then
5556 -- Check for Storage_Pool previously given
5558 declare
5559 SP : constant Node_Id :=
5560 Get_Attribute_Definition_Clause
5561 (U_Ent, Attribute_Storage_Pool);
5563 begin
5564 if Present (SP) then
5565 Check_Pool_Size_Clash (U_Ent, SP, N);
5566 end if;
5567 end;
5569 -- Special case of for x'Storage_Size use 0
5571 if Is_OK_Static_Expression (Expr)
5572 and then Expr_Value (Expr) = 0
5573 then
5574 Set_No_Pool_Assigned (Btype);
5575 end if;
5576 end if;
5578 Set_Has_Storage_Size_Clause (Btype);
5579 end if;
5580 end Storage_Size;
5582 -----------------
5583 -- Stream_Size --
5584 -----------------
5586 when Attribute_Stream_Size => Stream_Size : declare
5587 Size : constant Uint := Static_Integer (Expr);
5589 begin
5590 if Ada_Version <= Ada_95 then
5591 Check_Restriction (No_Implementation_Attributes, N);
5592 end if;
5594 if Duplicate_Clause then
5595 null;
5597 elsif Is_Elementary_Type (U_Ent) then
5598 if Size /= System_Storage_Unit
5599 and then
5600 Size /= System_Storage_Unit * 2
5601 and then
5602 Size /= System_Storage_Unit * 4
5603 and then
5604 Size /= System_Storage_Unit * 8
5605 then
5606 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5607 Error_Msg_N
5608 ("stream size for elementary type must be a"
5609 & " power of 2 and at least ^", N);
5611 elsif RM_Size (U_Ent) > Size then
5612 Error_Msg_Uint_1 := RM_Size (U_Ent);
5613 Error_Msg_N
5614 ("stream size for elementary type must be a"
5615 & " power of 2 and at least ^", N);
5616 end if;
5618 Set_Has_Stream_Size_Clause (U_Ent);
5620 else
5621 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5622 end if;
5623 end Stream_Size;
5625 ----------------
5626 -- Value_Size --
5627 ----------------
5629 -- Value_Size attribute definition clause
5631 when Attribute_Value_Size => Value_Size : declare
5632 Size : constant Uint := Static_Integer (Expr);
5633 Biased : Boolean;
5635 begin
5636 if not Is_Type (U_Ent) then
5637 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5639 elsif Duplicate_Clause then
5640 null;
5642 elsif Is_Array_Type (U_Ent)
5643 and then not Is_Constrained (U_Ent)
5644 then
5645 Error_Msg_N
5646 ("Value_Size cannot be given for unconstrained array", Nam);
5648 else
5649 if Is_Elementary_Type (U_Ent) then
5650 Check_Size (Expr, U_Ent, Size, Biased);
5651 Set_Biased (U_Ent, N, "value size clause", Biased);
5652 end if;
5654 Set_RM_Size (U_Ent, Size);
5655 end if;
5656 end Value_Size;
5658 -----------------------
5659 -- Variable_Indexing --
5660 -----------------------
5662 when Attribute_Variable_Indexing =>
5663 Check_Indexing_Functions;
5665 -----------
5666 -- Write --
5667 -----------
5669 when Attribute_Write =>
5670 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5671 Set_Has_Specified_Stream_Write (Ent);
5673 -- All other attributes cannot be set
5675 when others =>
5676 Error_Msg_N
5677 ("attribute& cannot be set with definition clause", N);
5678 end case;
5680 -- The test for the type being frozen must be performed after any
5681 -- expression the clause has been analyzed since the expression itself
5682 -- might cause freezing that makes the clause illegal.
5684 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5685 return;
5686 end if;
5687 end Analyze_Attribute_Definition_Clause;
5689 ----------------------------
5690 -- Analyze_Code_Statement --
5691 ----------------------------
5693 procedure Analyze_Code_Statement (N : Node_Id) is
5694 HSS : constant Node_Id := Parent (N);
5695 SBody : constant Node_Id := Parent (HSS);
5696 Subp : constant Entity_Id := Current_Scope;
5697 Stmt : Node_Id;
5698 Decl : Node_Id;
5699 StmtO : Node_Id;
5700 DeclO : Node_Id;
5702 begin
5703 -- Analyze and check we get right type, note that this implements the
5704 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5705 -- is the only way that Asm_Insn could possibly be visible.
5707 Analyze_And_Resolve (Expression (N));
5709 if Etype (Expression (N)) = Any_Type then
5710 return;
5711 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5712 Error_Msg_N ("incorrect type for code statement", N);
5713 return;
5714 end if;
5716 Check_Code_Statement (N);
5718 -- Make sure we appear in the handled statement sequence of a
5719 -- subprogram (RM 13.8(3)).
5721 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5722 or else Nkind (SBody) /= N_Subprogram_Body
5723 then
5724 Error_Msg_N
5725 ("code statement can only appear in body of subprogram", N);
5726 return;
5727 end if;
5729 -- Do remaining checks (RM 13.8(3)) if not already done
5731 if not Is_Machine_Code_Subprogram (Subp) then
5732 Set_Is_Machine_Code_Subprogram (Subp);
5734 -- No exception handlers allowed
5736 if Present (Exception_Handlers (HSS)) then
5737 Error_Msg_N
5738 ("exception handlers not permitted in machine code subprogram",
5739 First (Exception_Handlers (HSS)));
5740 end if;
5742 -- No declarations other than use clauses and pragmas (we allow
5743 -- certain internally generated declarations as well).
5745 Decl := First (Declarations (SBody));
5746 while Present (Decl) loop
5747 DeclO := Original_Node (Decl);
5748 if Comes_From_Source (DeclO)
5749 and not Nkind_In (DeclO, N_Pragma,
5750 N_Use_Package_Clause,
5751 N_Use_Type_Clause,
5752 N_Implicit_Label_Declaration)
5753 then
5754 Error_Msg_N
5755 ("this declaration not allowed in machine code subprogram",
5756 DeclO);
5757 end if;
5759 Next (Decl);
5760 end loop;
5762 -- No statements other than code statements, pragmas, and labels.
5763 -- Again we allow certain internally generated statements.
5765 -- In Ada 2012, qualified expressions are names, and the code
5766 -- statement is initially parsed as a procedure call.
5768 Stmt := First (Statements (HSS));
5769 while Present (Stmt) loop
5770 StmtO := Original_Node (Stmt);
5772 -- A procedure call transformed into a code statement is OK.
5774 if Ada_Version >= Ada_2012
5775 and then Nkind (StmtO) = N_Procedure_Call_Statement
5776 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5777 then
5778 null;
5780 elsif Comes_From_Source (StmtO)
5781 and then not Nkind_In (StmtO, N_Pragma,
5782 N_Label,
5783 N_Code_Statement)
5784 then
5785 Error_Msg_N
5786 ("this statement is not allowed in machine code subprogram",
5787 StmtO);
5788 end if;
5790 Next (Stmt);
5791 end loop;
5792 end if;
5793 end Analyze_Code_Statement;
5795 -----------------------------------------------
5796 -- Analyze_Enumeration_Representation_Clause --
5797 -----------------------------------------------
5799 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5800 Ident : constant Node_Id := Identifier (N);
5801 Aggr : constant Node_Id := Array_Aggregate (N);
5802 Enumtype : Entity_Id;
5803 Elit : Entity_Id;
5804 Expr : Node_Id;
5805 Assoc : Node_Id;
5806 Choice : Node_Id;
5807 Val : Uint;
5809 Err : Boolean := False;
5810 -- Set True to avoid cascade errors and crashes on incorrect source code
5812 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5813 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5814 -- Allowed range of universal integer (= allowed range of enum lit vals)
5816 Min : Uint;
5817 Max : Uint;
5818 -- Minimum and maximum values of entries
5820 Max_Node : Node_Id;
5821 -- Pointer to node for literal providing max value
5823 begin
5824 if Ignore_Rep_Clauses then
5825 Kill_Rep_Clause (N);
5826 return;
5827 end if;
5829 -- Ignore enumeration rep clauses by default in CodePeer mode,
5830 -- unless -gnatd.I is specified, as a work around for potential false
5831 -- positive messages.
5833 if CodePeer_Mode and not Debug_Flag_Dot_II then
5834 return;
5835 end if;
5837 -- First some basic error checks
5839 Find_Type (Ident);
5840 Enumtype := Entity (Ident);
5842 if Enumtype = Any_Type
5843 or else Rep_Item_Too_Early (Enumtype, N)
5844 then
5845 return;
5846 else
5847 Enumtype := Underlying_Type (Enumtype);
5848 end if;
5850 if not Is_Enumeration_Type (Enumtype) then
5851 Error_Msg_NE
5852 ("enumeration type required, found}",
5853 Ident, First_Subtype (Enumtype));
5854 return;
5855 end if;
5857 -- Ignore rep clause on generic actual type. This will already have
5858 -- been flagged on the template as an error, and this is the safest
5859 -- way to ensure we don't get a junk cascaded message in the instance.
5861 if Is_Generic_Actual_Type (Enumtype) then
5862 return;
5864 -- Type must be in current scope
5866 elsif Scope (Enumtype) /= Current_Scope then
5867 Error_Msg_N ("type must be declared in this scope", Ident);
5868 return;
5870 -- Type must be a first subtype
5872 elsif not Is_First_Subtype (Enumtype) then
5873 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5874 return;
5876 -- Ignore duplicate rep clause
5878 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5879 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5880 return;
5882 -- Don't allow rep clause for standard [wide_[wide_]]character
5884 elsif Is_Standard_Character_Type (Enumtype) then
5885 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5886 return;
5888 -- Check that the expression is a proper aggregate (no parentheses)
5890 elsif Paren_Count (Aggr) /= 0 then
5891 Error_Msg
5892 ("extra parentheses surrounding aggregate not allowed",
5893 First_Sloc (Aggr));
5894 return;
5896 -- All tests passed, so set rep clause in place
5898 else
5899 Set_Has_Enumeration_Rep_Clause (Enumtype);
5900 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5901 end if;
5903 -- Now we process the aggregate. Note that we don't use the normal
5904 -- aggregate code for this purpose, because we don't want any of the
5905 -- normal expansion activities, and a number of special semantic
5906 -- rules apply (including the component type being any integer type)
5908 Elit := First_Literal (Enumtype);
5910 -- First the positional entries if any
5912 if Present (Expressions (Aggr)) then
5913 Expr := First (Expressions (Aggr));
5914 while Present (Expr) loop
5915 if No (Elit) then
5916 Error_Msg_N ("too many entries in aggregate", Expr);
5917 return;
5918 end if;
5920 Val := Static_Integer (Expr);
5922 -- Err signals that we found some incorrect entries processing
5923 -- the list. The final checks for completeness and ordering are
5924 -- skipped in this case.
5926 if Val = No_Uint then
5927 Err := True;
5929 elsif Val < Lo or else Hi < Val then
5930 Error_Msg_N ("value outside permitted range", Expr);
5931 Err := True;
5932 end if;
5934 Set_Enumeration_Rep (Elit, Val);
5935 Set_Enumeration_Rep_Expr (Elit, Expr);
5936 Next (Expr);
5937 Next (Elit);
5938 end loop;
5939 end if;
5941 -- Now process the named entries if present
5943 if Present (Component_Associations (Aggr)) then
5944 Assoc := First (Component_Associations (Aggr));
5945 while Present (Assoc) loop
5946 Choice := First (Choices (Assoc));
5948 if Present (Next (Choice)) then
5949 Error_Msg_N
5950 ("multiple choice not allowed here", Next (Choice));
5951 Err := True;
5952 end if;
5954 if Nkind (Choice) = N_Others_Choice then
5955 Error_Msg_N ("others choice not allowed here", Choice);
5956 Err := True;
5958 elsif Nkind (Choice) = N_Range then
5960 -- ??? should allow zero/one element range here
5962 Error_Msg_N ("range not allowed here", Choice);
5963 Err := True;
5965 else
5966 Analyze_And_Resolve (Choice, Enumtype);
5968 if Error_Posted (Choice) then
5969 Err := True;
5970 end if;
5972 if not Err then
5973 if Is_Entity_Name (Choice)
5974 and then Is_Type (Entity (Choice))
5975 then
5976 Error_Msg_N ("subtype name not allowed here", Choice);
5977 Err := True;
5979 -- ??? should allow static subtype with zero/one entry
5981 elsif Etype (Choice) = Base_Type (Enumtype) then
5982 if not Is_OK_Static_Expression (Choice) then
5983 Flag_Non_Static_Expr
5984 ("non-static expression used for choice!", Choice);
5985 Err := True;
5987 else
5988 Elit := Expr_Value_E (Choice);
5990 if Present (Enumeration_Rep_Expr (Elit)) then
5991 Error_Msg_Sloc :=
5992 Sloc (Enumeration_Rep_Expr (Elit));
5993 Error_Msg_NE
5994 ("representation for& previously given#",
5995 Choice, Elit);
5996 Err := True;
5997 end if;
5999 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6001 Expr := Expression (Assoc);
6002 Val := Static_Integer (Expr);
6004 if Val = No_Uint then
6005 Err := True;
6007 elsif Val < Lo or else Hi < Val then
6008 Error_Msg_N ("value outside permitted range", Expr);
6009 Err := True;
6010 end if;
6012 Set_Enumeration_Rep (Elit, Val);
6013 end if;
6014 end if;
6015 end if;
6016 end if;
6018 Next (Assoc);
6019 end loop;
6020 end if;
6022 -- Aggregate is fully processed. Now we check that a full set of
6023 -- representations was given, and that they are in range and in order.
6024 -- These checks are only done if no other errors occurred.
6026 if not Err then
6027 Min := No_Uint;
6028 Max := No_Uint;
6030 Elit := First_Literal (Enumtype);
6031 while Present (Elit) loop
6032 if No (Enumeration_Rep_Expr (Elit)) then
6033 Error_Msg_NE ("missing representation for&!", N, Elit);
6035 else
6036 Val := Enumeration_Rep (Elit);
6038 if Min = No_Uint then
6039 Min := Val;
6040 end if;
6042 if Val /= No_Uint then
6043 if Max /= No_Uint and then Val <= Max then
6044 Error_Msg_NE
6045 ("enumeration value for& not ordered!",
6046 Enumeration_Rep_Expr (Elit), Elit);
6047 end if;
6049 Max_Node := Enumeration_Rep_Expr (Elit);
6050 Max := Val;
6051 end if;
6053 -- If there is at least one literal whose representation is not
6054 -- equal to the Pos value, then note that this enumeration type
6055 -- has a non-standard representation.
6057 if Val /= Enumeration_Pos (Elit) then
6058 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6059 end if;
6060 end if;
6062 Next (Elit);
6063 end loop;
6065 -- Now set proper size information
6067 declare
6068 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6070 begin
6071 if Has_Size_Clause (Enumtype) then
6073 -- All OK, if size is OK now
6075 if RM_Size (Enumtype) >= Minsize then
6076 null;
6078 else
6079 -- Try if we can get by with biasing
6081 Minsize :=
6082 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6084 -- Error message if even biasing does not work
6086 if RM_Size (Enumtype) < Minsize then
6087 Error_Msg_Uint_1 := RM_Size (Enumtype);
6088 Error_Msg_Uint_2 := Max;
6089 Error_Msg_N
6090 ("previously given size (^) is too small "
6091 & "for this value (^)", Max_Node);
6093 -- If biasing worked, indicate that we now have biased rep
6095 else
6096 Set_Biased
6097 (Enumtype, Size_Clause (Enumtype), "size clause");
6098 end if;
6099 end if;
6101 else
6102 Set_RM_Size (Enumtype, Minsize);
6103 Set_Enum_Esize (Enumtype);
6104 end if;
6106 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6107 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6108 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6109 end;
6110 end if;
6112 -- We repeat the too late test in case it froze itself
6114 if Rep_Item_Too_Late (Enumtype, N) then
6115 null;
6116 end if;
6117 end Analyze_Enumeration_Representation_Clause;
6119 ----------------------------
6120 -- Analyze_Free_Statement --
6121 ----------------------------
6123 procedure Analyze_Free_Statement (N : Node_Id) is
6124 begin
6125 Analyze (Expression (N));
6126 end Analyze_Free_Statement;
6128 ---------------------------
6129 -- Analyze_Freeze_Entity --
6130 ---------------------------
6132 procedure Analyze_Freeze_Entity (N : Node_Id) is
6133 begin
6134 Freeze_Entity_Checks (N);
6135 end Analyze_Freeze_Entity;
6137 -----------------------------------
6138 -- Analyze_Freeze_Generic_Entity --
6139 -----------------------------------
6141 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6142 begin
6143 Freeze_Entity_Checks (N);
6144 end Analyze_Freeze_Generic_Entity;
6146 ------------------------------------------
6147 -- Analyze_Record_Representation_Clause --
6148 ------------------------------------------
6150 -- Note: we check as much as we can here, but we can't do any checks
6151 -- based on the position values (e.g. overlap checks) until freeze time
6152 -- because especially in Ada 2005 (machine scalar mode), the processing
6153 -- for non-standard bit order can substantially change the positions.
6154 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6155 -- for the remainder of this processing.
6157 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6158 Ident : constant Node_Id := Identifier (N);
6159 Biased : Boolean;
6160 CC : Node_Id;
6161 Comp : Entity_Id;
6162 Fbit : Uint;
6163 Hbit : Uint := Uint_0;
6164 Lbit : Uint;
6165 Ocomp : Entity_Id;
6166 Posit : Uint;
6167 Rectype : Entity_Id;
6168 Recdef : Node_Id;
6170 function Is_Inherited (Comp : Entity_Id) return Boolean;
6171 -- True if Comp is an inherited component in a record extension
6173 ------------------
6174 -- Is_Inherited --
6175 ------------------
6177 function Is_Inherited (Comp : Entity_Id) return Boolean is
6178 Comp_Base : Entity_Id;
6180 begin
6181 if Ekind (Rectype) = E_Record_Subtype then
6182 Comp_Base := Original_Record_Component (Comp);
6183 else
6184 Comp_Base := Comp;
6185 end if;
6187 return Comp_Base /= Original_Record_Component (Comp_Base);
6188 end Is_Inherited;
6190 -- Local variables
6192 Is_Record_Extension : Boolean;
6193 -- True if Rectype is a record extension
6195 CR_Pragma : Node_Id := Empty;
6196 -- Points to N_Pragma node if Complete_Representation pragma present
6198 -- Start of processing for Analyze_Record_Representation_Clause
6200 begin
6201 if Ignore_Rep_Clauses then
6202 Kill_Rep_Clause (N);
6203 return;
6204 end if;
6206 Find_Type (Ident);
6207 Rectype := Entity (Ident);
6209 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6210 return;
6211 else
6212 Rectype := Underlying_Type (Rectype);
6213 end if;
6215 -- First some basic error checks
6217 if not Is_Record_Type (Rectype) then
6218 Error_Msg_NE
6219 ("record type required, found}", Ident, First_Subtype (Rectype));
6220 return;
6222 elsif Scope (Rectype) /= Current_Scope then
6223 Error_Msg_N ("type must be declared in this scope", N);
6224 return;
6226 elsif not Is_First_Subtype (Rectype) then
6227 Error_Msg_N ("cannot give record rep clause for subtype", N);
6228 return;
6230 elsif Has_Record_Rep_Clause (Rectype) then
6231 Error_Msg_N ("duplicate record rep clause ignored", N);
6232 return;
6234 elsif Rep_Item_Too_Late (Rectype, N) then
6235 return;
6236 end if;
6238 -- We know we have a first subtype, now possibly go the the anonymous
6239 -- base type to determine whether Rectype is a record extension.
6241 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6242 Is_Record_Extension :=
6243 Nkind (Recdef) = N_Derived_Type_Definition
6244 and then Present (Record_Extension_Part (Recdef));
6246 if Present (Mod_Clause (N)) then
6247 declare
6248 Loc : constant Source_Ptr := Sloc (N);
6249 M : constant Node_Id := Mod_Clause (N);
6250 P : constant List_Id := Pragmas_Before (M);
6251 AtM_Nod : Node_Id;
6253 Mod_Val : Uint;
6254 pragma Warnings (Off, Mod_Val);
6256 begin
6257 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6259 if Warn_On_Obsolescent_Feature then
6260 Error_Msg_N
6261 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6262 Error_Msg_N
6263 ("\?j?use alignment attribute definition clause instead", N);
6264 end if;
6266 if Present (P) then
6267 Analyze_List (P);
6268 end if;
6270 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6271 -- the Mod clause into an alignment clause anyway, so that the
6272 -- back-end can compute and back-annotate properly the size and
6273 -- alignment of types that may include this record.
6275 -- This seems dubious, this destroys the source tree in a manner
6276 -- not detectable by ASIS ???
6278 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6279 AtM_Nod :=
6280 Make_Attribute_Definition_Clause (Loc,
6281 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6282 Chars => Name_Alignment,
6283 Expression => Relocate_Node (Expression (M)));
6285 Set_From_At_Mod (AtM_Nod);
6286 Insert_After (N, AtM_Nod);
6287 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6288 Set_Mod_Clause (N, Empty);
6290 else
6291 -- Get the alignment value to perform error checking
6293 Mod_Val := Get_Alignment_Value (Expression (M));
6294 end if;
6295 end;
6296 end if;
6298 -- For untagged types, clear any existing component clauses for the
6299 -- type. If the type is derived, this is what allows us to override
6300 -- a rep clause for the parent. For type extensions, the representation
6301 -- of the inherited components is inherited, so we want to keep previous
6302 -- component clauses for completeness.
6304 if not Is_Tagged_Type (Rectype) then
6305 Comp := First_Component_Or_Discriminant (Rectype);
6306 while Present (Comp) loop
6307 Set_Component_Clause (Comp, Empty);
6308 Next_Component_Or_Discriminant (Comp);
6309 end loop;
6310 end if;
6312 -- All done if no component clauses
6314 CC := First (Component_Clauses (N));
6316 if No (CC) then
6317 return;
6318 end if;
6320 -- A representation like this applies to the base type
6322 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6323 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6324 Set_Has_Specified_Layout (Base_Type (Rectype));
6326 -- Process the component clauses
6328 while Present (CC) loop
6330 -- Pragma
6332 if Nkind (CC) = N_Pragma then
6333 Analyze (CC);
6335 -- The only pragma of interest is Complete_Representation
6337 if Pragma_Name (CC) = Name_Complete_Representation then
6338 CR_Pragma := CC;
6339 end if;
6341 -- Processing for real component clause
6343 else
6344 Posit := Static_Integer (Position (CC));
6345 Fbit := Static_Integer (First_Bit (CC));
6346 Lbit := Static_Integer (Last_Bit (CC));
6348 if Posit /= No_Uint
6349 and then Fbit /= No_Uint
6350 and then Lbit /= No_Uint
6351 then
6352 if Posit < 0 then
6353 Error_Msg_N
6354 ("position cannot be negative", Position (CC));
6356 elsif Fbit < 0 then
6357 Error_Msg_N
6358 ("first bit cannot be negative", First_Bit (CC));
6360 -- The Last_Bit specified in a component clause must not be
6361 -- less than the First_Bit minus one (RM-13.5.1(10)).
6363 elsif Lbit < Fbit - 1 then
6364 Error_Msg_N
6365 ("last bit cannot be less than first bit minus one",
6366 Last_Bit (CC));
6368 -- Values look OK, so find the corresponding record component
6369 -- Even though the syntax allows an attribute reference for
6370 -- implementation-defined components, GNAT does not allow the
6371 -- tag to get an explicit position.
6373 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6374 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6375 Error_Msg_N ("position of tag cannot be specified", CC);
6376 else
6377 Error_Msg_N ("illegal component name", CC);
6378 end if;
6380 else
6381 Comp := First_Entity (Rectype);
6382 while Present (Comp) loop
6383 exit when Chars (Comp) = Chars (Component_Name (CC));
6384 Next_Entity (Comp);
6385 end loop;
6387 if No (Comp) then
6389 -- Maybe component of base type that is absent from
6390 -- statically constrained first subtype.
6392 Comp := First_Entity (Base_Type (Rectype));
6393 while Present (Comp) loop
6394 exit when Chars (Comp) = Chars (Component_Name (CC));
6395 Next_Entity (Comp);
6396 end loop;
6397 end if;
6399 if No (Comp) then
6400 Error_Msg_N
6401 ("component clause is for non-existent field", CC);
6403 -- Ada 2012 (AI05-0026): Any name that denotes a
6404 -- discriminant of an object of an unchecked union type
6405 -- shall not occur within a record_representation_clause.
6407 -- The general restriction of using record rep clauses on
6408 -- Unchecked_Union types has now been lifted. Since it is
6409 -- possible to introduce a record rep clause which mentions
6410 -- the discriminant of an Unchecked_Union in non-Ada 2012
6411 -- code, this check is applied to all versions of the
6412 -- language.
6414 elsif Ekind (Comp) = E_Discriminant
6415 and then Is_Unchecked_Union (Rectype)
6416 then
6417 Error_Msg_N
6418 ("cannot reference discriminant of unchecked union",
6419 Component_Name (CC));
6421 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6422 Error_Msg_NE
6423 ("component clause not allowed for inherited "
6424 & "component&", CC, Comp);
6426 elsif Present (Component_Clause (Comp)) then
6428 -- Diagnose duplicate rep clause, or check consistency
6429 -- if this is an inherited component. In a double fault,
6430 -- there may be a duplicate inconsistent clause for an
6431 -- inherited component.
6433 if Scope (Original_Record_Component (Comp)) = Rectype
6434 or else Parent (Component_Clause (Comp)) = N
6435 then
6436 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6437 Error_Msg_N ("component clause previously given#", CC);
6439 else
6440 declare
6441 Rep1 : constant Node_Id := Component_Clause (Comp);
6442 begin
6443 if Intval (Position (Rep1)) /=
6444 Intval (Position (CC))
6445 or else Intval (First_Bit (Rep1)) /=
6446 Intval (First_Bit (CC))
6447 or else Intval (Last_Bit (Rep1)) /=
6448 Intval (Last_Bit (CC))
6449 then
6450 Error_Msg_N
6451 ("component clause inconsistent "
6452 & "with representation of ancestor", CC);
6454 elsif Warn_On_Redundant_Constructs then
6455 Error_Msg_N
6456 ("?r?redundant confirming component clause "
6457 & "for component!", CC);
6458 end if;
6459 end;
6460 end if;
6462 -- Normal case where this is the first component clause we
6463 -- have seen for this entity, so set it up properly.
6465 else
6466 -- Make reference for field in record rep clause and set
6467 -- appropriate entity field in the field identifier.
6469 Generate_Reference
6470 (Comp, Component_Name (CC), Set_Ref => False);
6471 Set_Entity (Component_Name (CC), Comp);
6473 -- Update Fbit and Lbit to the actual bit number
6475 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6476 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6478 if Has_Size_Clause (Rectype)
6479 and then RM_Size (Rectype) <= Lbit
6480 then
6481 Error_Msg_N
6482 ("bit number out of range of specified size",
6483 Last_Bit (CC));
6484 else
6485 Set_Component_Clause (Comp, CC);
6486 Set_Component_Bit_Offset (Comp, Fbit);
6487 Set_Esize (Comp, 1 + (Lbit - Fbit));
6488 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6489 Set_Normalized_Position (Comp, Fbit / SSU);
6491 if Warn_On_Overridden_Size
6492 and then Has_Size_Clause (Etype (Comp))
6493 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6494 then
6495 Error_Msg_NE
6496 ("?S?component size overrides size clause for&",
6497 Component_Name (CC), Etype (Comp));
6498 end if;
6500 -- This information is also set in the corresponding
6501 -- component of the base type, found by accessing the
6502 -- Original_Record_Component link if it is present.
6504 Ocomp := Original_Record_Component (Comp);
6506 if Hbit < Lbit then
6507 Hbit := Lbit;
6508 end if;
6510 Check_Size
6511 (Component_Name (CC),
6512 Etype (Comp),
6513 Esize (Comp),
6514 Biased);
6516 Set_Biased
6517 (Comp, First_Node (CC), "component clause", Biased);
6519 if Present (Ocomp) then
6520 Set_Component_Clause (Ocomp, CC);
6521 Set_Component_Bit_Offset (Ocomp, Fbit);
6522 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6523 Set_Normalized_Position (Ocomp, Fbit / SSU);
6524 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6526 Set_Normalized_Position_Max
6527 (Ocomp, Normalized_Position (Ocomp));
6529 -- Note: we don't use Set_Biased here, because we
6530 -- already gave a warning above if needed, and we
6531 -- would get a duplicate for the same name here.
6533 Set_Has_Biased_Representation
6534 (Ocomp, Has_Biased_Representation (Comp));
6535 end if;
6537 if Esize (Comp) < 0 then
6538 Error_Msg_N ("component size is negative", CC);
6539 end if;
6540 end if;
6541 end if;
6542 end if;
6543 end if;
6544 end if;
6546 Next (CC);
6547 end loop;
6549 -- Check missing components if Complete_Representation pragma appeared
6551 if Present (CR_Pragma) then
6552 Comp := First_Component_Or_Discriminant (Rectype);
6553 while Present (Comp) loop
6554 if No (Component_Clause (Comp)) then
6555 Error_Msg_NE
6556 ("missing component clause for &", CR_Pragma, Comp);
6557 end if;
6559 Next_Component_Or_Discriminant (Comp);
6560 end loop;
6562 -- Give missing components warning if required
6564 elsif Warn_On_Unrepped_Components then
6565 declare
6566 Num_Repped_Components : Nat := 0;
6567 Num_Unrepped_Components : Nat := 0;
6569 begin
6570 -- First count number of repped and unrepped components
6572 Comp := First_Component_Or_Discriminant (Rectype);
6573 while Present (Comp) loop
6574 if Present (Component_Clause (Comp)) then
6575 Num_Repped_Components := Num_Repped_Components + 1;
6576 else
6577 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6578 end if;
6580 Next_Component_Or_Discriminant (Comp);
6581 end loop;
6583 -- We are only interested in the case where there is at least one
6584 -- unrepped component, and at least half the components have rep
6585 -- clauses. We figure that if less than half have them, then the
6586 -- partial rep clause is really intentional. If the component
6587 -- type has no underlying type set at this point (as for a generic
6588 -- formal type), we don't know enough to give a warning on the
6589 -- component.
6591 if Num_Unrepped_Components > 0
6592 and then Num_Unrepped_Components < Num_Repped_Components
6593 then
6594 Comp := First_Component_Or_Discriminant (Rectype);
6595 while Present (Comp) loop
6596 if No (Component_Clause (Comp))
6597 and then Comes_From_Source (Comp)
6598 and then Present (Underlying_Type (Etype (Comp)))
6599 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6600 or else Size_Known_At_Compile_Time
6601 (Underlying_Type (Etype (Comp))))
6602 and then not Has_Warnings_Off (Rectype)
6604 -- Ignore discriminant in unchecked union, since it is
6605 -- not there, and cannot have a component clause.
6607 and then (not Is_Unchecked_Union (Rectype)
6608 or else Ekind (Comp) /= E_Discriminant)
6609 then
6610 Error_Msg_Sloc := Sloc (Comp);
6611 Error_Msg_NE
6612 ("?C?no component clause given for & declared #",
6613 N, Comp);
6614 end if;
6616 Next_Component_Or_Discriminant (Comp);
6617 end loop;
6618 end if;
6619 end;
6620 end if;
6621 end Analyze_Record_Representation_Clause;
6623 -------------------------------------
6624 -- Build_Discrete_Static_Predicate --
6625 -------------------------------------
6627 procedure Build_Discrete_Static_Predicate
6628 (Typ : Entity_Id;
6629 Expr : Node_Id;
6630 Nam : Name_Id)
6632 Loc : constant Source_Ptr := Sloc (Expr);
6634 Non_Static : exception;
6635 -- Raised if something non-static is found
6637 Btyp : constant Entity_Id := Base_Type (Typ);
6639 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6640 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6641 -- Low bound and high bound value of base type of Typ
6643 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6644 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6645 -- Low bound and high bound values of static subtype Typ
6647 type REnt is record
6648 Lo, Hi : Uint;
6649 end record;
6650 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6651 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6652 -- value.
6654 type RList is array (Nat range <>) of REnt;
6655 -- A list of ranges. The ranges are sorted in increasing order, and are
6656 -- disjoint (there is a gap of at least one value between each range in
6657 -- the table). A value is in the set of ranges in Rlist if it lies
6658 -- within one of these ranges.
6660 False_Range : constant RList :=
6661 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6662 -- An empty set of ranges represents a range list that can never be
6663 -- satisfied, since there are no ranges in which the value could lie,
6664 -- so it does not lie in any of them. False_Range is a canonical value
6665 -- for this empty set, but general processing should test for an Rlist
6666 -- with length zero (see Is_False predicate), since other null ranges
6667 -- may appear which must be treated as False.
6669 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6670 -- Range representing True, value must be in the base range
6672 function "and" (Left : RList; Right : RList) return RList;
6673 -- And's together two range lists, returning a range list. This is a set
6674 -- intersection operation.
6676 function "or" (Left : RList; Right : RList) return RList;
6677 -- Or's together two range lists, returning a range list. This is a set
6678 -- union operation.
6680 function "not" (Right : RList) return RList;
6681 -- Returns complement of a given range list, i.e. a range list
6682 -- representing all the values in TLo .. THi that are not in the input
6683 -- operand Right.
6685 function Build_Val (V : Uint) return Node_Id;
6686 -- Return an analyzed N_Identifier node referencing this value, suitable
6687 -- for use as an entry in the Static_Discrte_Predicate list. This node
6688 -- is typed with the base type.
6690 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6691 -- Return an analyzed N_Range node referencing this range, suitable for
6692 -- use as an entry in the Static_Discrete_Predicate list. This node is
6693 -- typed with the base type.
6695 function Get_RList (Exp : Node_Id) return RList;
6696 -- This is a recursive routine that converts the given expression into a
6697 -- list of ranges, suitable for use in building the static predicate.
6699 function Is_False (R : RList) return Boolean;
6700 pragma Inline (Is_False);
6701 -- Returns True if the given range list is empty, and thus represents a
6702 -- False list of ranges that can never be satisfied.
6704 function Is_True (R : RList) return Boolean;
6705 -- Returns True if R trivially represents the True predicate by having a
6706 -- single range from BLo to BHi.
6708 function Is_Type_Ref (N : Node_Id) return Boolean;
6709 pragma Inline (Is_Type_Ref);
6710 -- Returns if True if N is a reference to the type for the predicate in
6711 -- the expression (i.e. if it is an identifier whose Chars field matches
6712 -- the Nam given in the call). N must not be parenthesized, if the type
6713 -- name appears in parens, this routine will return False.
6715 function Lo_Val (N : Node_Id) return Uint;
6716 -- Given an entry from a Static_Discrete_Predicate list that is either
6717 -- a static expression or static range, gets either the expression value
6718 -- or the low bound of the range.
6720 function Hi_Val (N : Node_Id) return Uint;
6721 -- Given an entry from a Static_Discrete_Predicate list that is either
6722 -- a static expression or static range, gets either the expression value
6723 -- or the high bound of the range.
6725 function Membership_Entry (N : Node_Id) return RList;
6726 -- Given a single membership entry (range, value, or subtype), returns
6727 -- the corresponding range list. Raises Static_Error if not static.
6729 function Membership_Entries (N : Node_Id) return RList;
6730 -- Given an element on an alternatives list of a membership operation,
6731 -- returns the range list corresponding to this entry and all following
6732 -- entries (i.e. returns the "or" of this list of values).
6734 function Stat_Pred (Typ : Entity_Id) return RList;
6735 -- Given a type, if it has a static predicate, then return the predicate
6736 -- as a range list, otherwise raise Non_Static.
6738 -----------
6739 -- "and" --
6740 -----------
6742 function "and" (Left : RList; Right : RList) return RList is
6743 FEnt : REnt;
6744 -- First range of result
6746 SLeft : Nat := Left'First;
6747 -- Start of rest of left entries
6749 SRight : Nat := Right'First;
6750 -- Start of rest of right entries
6752 begin
6753 -- If either range is True, return the other
6755 if Is_True (Left) then
6756 return Right;
6757 elsif Is_True (Right) then
6758 return Left;
6759 end if;
6761 -- If either range is False, return False
6763 if Is_False (Left) or else Is_False (Right) then
6764 return False_Range;
6765 end if;
6767 -- Loop to remove entries at start that are disjoint, and thus just
6768 -- get discarded from the result entirely.
6770 loop
6771 -- If no operands left in either operand, result is false
6773 if SLeft > Left'Last or else SRight > Right'Last then
6774 return False_Range;
6776 -- Discard first left operand entry if disjoint with right
6778 elsif Left (SLeft).Hi < Right (SRight).Lo then
6779 SLeft := SLeft + 1;
6781 -- Discard first right operand entry if disjoint with left
6783 elsif Right (SRight).Hi < Left (SLeft).Lo then
6784 SRight := SRight + 1;
6786 -- Otherwise we have an overlapping entry
6788 else
6789 exit;
6790 end if;
6791 end loop;
6793 -- Now we have two non-null operands, and first entries overlap. The
6794 -- first entry in the result will be the overlapping part of these
6795 -- two entries.
6797 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6798 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6800 -- Now we can remove the entry that ended at a lower value, since its
6801 -- contribution is entirely contained in Fent.
6803 if Left (SLeft).Hi <= Right (SRight).Hi then
6804 SLeft := SLeft + 1;
6805 else
6806 SRight := SRight + 1;
6807 end if;
6809 -- Compute result by concatenating this first entry with the "and" of
6810 -- the remaining parts of the left and right operands. Note that if
6811 -- either of these is empty, "and" will yield empty, so that we will
6812 -- end up with just Fent, which is what we want in that case.
6814 return
6815 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6816 end "and";
6818 -----------
6819 -- "not" --
6820 -----------
6822 function "not" (Right : RList) return RList is
6823 begin
6824 -- Return True if False range
6826 if Is_False (Right) then
6827 return True_Range;
6828 end if;
6830 -- Return False if True range
6832 if Is_True (Right) then
6833 return False_Range;
6834 end if;
6836 -- Here if not trivial case
6838 declare
6839 Result : RList (1 .. Right'Length + 1);
6840 -- May need one more entry for gap at beginning and end
6842 Count : Nat := 0;
6843 -- Number of entries stored in Result
6845 begin
6846 -- Gap at start
6848 if Right (Right'First).Lo > TLo then
6849 Count := Count + 1;
6850 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6851 end if;
6853 -- Gaps between ranges
6855 for J in Right'First .. Right'Last - 1 loop
6856 Count := Count + 1;
6857 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6858 end loop;
6860 -- Gap at end
6862 if Right (Right'Last).Hi < THi then
6863 Count := Count + 1;
6864 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6865 end if;
6867 return Result (1 .. Count);
6868 end;
6869 end "not";
6871 ----------
6872 -- "or" --
6873 ----------
6875 function "or" (Left : RList; Right : RList) return RList is
6876 FEnt : REnt;
6877 -- First range of result
6879 SLeft : Nat := Left'First;
6880 -- Start of rest of left entries
6882 SRight : Nat := Right'First;
6883 -- Start of rest of right entries
6885 begin
6886 -- If either range is True, return True
6888 if Is_True (Left) or else Is_True (Right) then
6889 return True_Range;
6890 end if;
6892 -- If either range is False (empty), return the other
6894 if Is_False (Left) then
6895 return Right;
6896 elsif Is_False (Right) then
6897 return Left;
6898 end if;
6900 -- Initialize result first entry from left or right operand depending
6901 -- on which starts with the lower range.
6903 if Left (SLeft).Lo < Right (SRight).Lo then
6904 FEnt := Left (SLeft);
6905 SLeft := SLeft + 1;
6906 else
6907 FEnt := Right (SRight);
6908 SRight := SRight + 1;
6909 end if;
6911 -- This loop eats ranges from left and right operands that are
6912 -- contiguous with the first range we are gathering.
6914 loop
6915 -- Eat first entry in left operand if contiguous or overlapped by
6916 -- gathered first operand of result.
6918 if SLeft <= Left'Last
6919 and then Left (SLeft).Lo <= FEnt.Hi + 1
6920 then
6921 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6922 SLeft := SLeft + 1;
6924 -- Eat first entry in right operand if contiguous or overlapped by
6925 -- gathered right operand of result.
6927 elsif SRight <= Right'Last
6928 and then Right (SRight).Lo <= FEnt.Hi + 1
6929 then
6930 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6931 SRight := SRight + 1;
6933 -- All done if no more entries to eat
6935 else
6936 exit;
6937 end if;
6938 end loop;
6940 -- Obtain result as the first entry we just computed, concatenated
6941 -- to the "or" of the remaining results (if one operand is empty,
6942 -- this will just concatenate with the other
6944 return
6945 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6946 end "or";
6948 -----------------
6949 -- Build_Range --
6950 -----------------
6952 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6953 Result : Node_Id;
6954 begin
6955 Result :=
6956 Make_Range (Loc,
6957 Low_Bound => Build_Val (Lo),
6958 High_Bound => Build_Val (Hi));
6959 Set_Etype (Result, Btyp);
6960 Set_Analyzed (Result);
6961 return Result;
6962 end Build_Range;
6964 ---------------
6965 -- Build_Val --
6966 ---------------
6968 function Build_Val (V : Uint) return Node_Id is
6969 Result : Node_Id;
6971 begin
6972 if Is_Enumeration_Type (Typ) then
6973 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6974 else
6975 Result := Make_Integer_Literal (Loc, V);
6976 end if;
6978 Set_Etype (Result, Btyp);
6979 Set_Is_Static_Expression (Result);
6980 Set_Analyzed (Result);
6981 return Result;
6982 end Build_Val;
6984 ---------------
6985 -- Get_RList --
6986 ---------------
6988 function Get_RList (Exp : Node_Id) return RList is
6989 Op : Node_Kind;
6990 Val : Uint;
6992 begin
6993 -- Static expression can only be true or false
6995 if Is_OK_Static_Expression (Exp) then
6996 if Expr_Value (Exp) = 0 then
6997 return False_Range;
6998 else
6999 return True_Range;
7000 end if;
7001 end if;
7003 -- Otherwise test node type
7005 Op := Nkind (Exp);
7007 case Op is
7009 -- And
7011 when N_Op_And | N_And_Then =>
7012 return Get_RList (Left_Opnd (Exp))
7014 Get_RList (Right_Opnd (Exp));
7016 -- Or
7018 when N_Op_Or | N_Or_Else =>
7019 return Get_RList (Left_Opnd (Exp))
7021 Get_RList (Right_Opnd (Exp));
7023 -- Not
7025 when N_Op_Not =>
7026 return not Get_RList (Right_Opnd (Exp));
7028 -- Comparisons of type with static value
7030 when N_Op_Compare =>
7032 -- Type is left operand
7034 if Is_Type_Ref (Left_Opnd (Exp))
7035 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7036 then
7037 Val := Expr_Value (Right_Opnd (Exp));
7039 -- Typ is right operand
7041 elsif Is_Type_Ref (Right_Opnd (Exp))
7042 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7043 then
7044 Val := Expr_Value (Left_Opnd (Exp));
7046 -- Invert sense of comparison
7048 case Op is
7049 when N_Op_Gt => Op := N_Op_Lt;
7050 when N_Op_Lt => Op := N_Op_Gt;
7051 when N_Op_Ge => Op := N_Op_Le;
7052 when N_Op_Le => Op := N_Op_Ge;
7053 when others => null;
7054 end case;
7056 -- Other cases are non-static
7058 else
7059 raise Non_Static;
7060 end if;
7062 -- Construct range according to comparison operation
7064 case Op is
7065 when N_Op_Eq =>
7066 return RList'(1 => REnt'(Val, Val));
7068 when N_Op_Ge =>
7069 return RList'(1 => REnt'(Val, BHi));
7071 when N_Op_Gt =>
7072 return RList'(1 => REnt'(Val + 1, BHi));
7074 when N_Op_Le =>
7075 return RList'(1 => REnt'(BLo, Val));
7077 when N_Op_Lt =>
7078 return RList'(1 => REnt'(BLo, Val - 1));
7080 when N_Op_Ne =>
7081 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7083 when others =>
7084 raise Program_Error;
7085 end case;
7087 -- Membership (IN)
7089 when N_In =>
7090 if not Is_Type_Ref (Left_Opnd (Exp)) then
7091 raise Non_Static;
7092 end if;
7094 if Present (Right_Opnd (Exp)) then
7095 return Membership_Entry (Right_Opnd (Exp));
7096 else
7097 return Membership_Entries (First (Alternatives (Exp)));
7098 end if;
7100 -- Negative membership (NOT IN)
7102 when N_Not_In =>
7103 if not Is_Type_Ref (Left_Opnd (Exp)) then
7104 raise Non_Static;
7105 end if;
7107 if Present (Right_Opnd (Exp)) then
7108 return not Membership_Entry (Right_Opnd (Exp));
7109 else
7110 return not Membership_Entries (First (Alternatives (Exp)));
7111 end if;
7113 -- Function call, may be call to static predicate
7115 when N_Function_Call =>
7116 if Is_Entity_Name (Name (Exp)) then
7117 declare
7118 Ent : constant Entity_Id := Entity (Name (Exp));
7119 begin
7120 if Is_Predicate_Function (Ent)
7121 or else
7122 Is_Predicate_Function_M (Ent)
7123 then
7124 return Stat_Pred (Etype (First_Formal (Ent)));
7125 end if;
7126 end;
7127 end if;
7129 -- Other function call cases are non-static
7131 raise Non_Static;
7133 -- Qualified expression, dig out the expression
7135 when N_Qualified_Expression =>
7136 return Get_RList (Expression (Exp));
7138 when N_Case_Expression =>
7139 declare
7140 Alt : Node_Id;
7141 Choices : List_Id;
7142 Dep : Node_Id;
7144 begin
7145 if not Is_Entity_Name (Expression (Expr))
7146 or else Etype (Expression (Expr)) /= Typ
7147 then
7148 Error_Msg_N
7149 ("expression must denaote subtype", Expression (Expr));
7150 return False_Range;
7151 end if;
7153 -- Collect discrete choices in all True alternatives
7155 Choices := New_List;
7156 Alt := First (Alternatives (Exp));
7157 while Present (Alt) loop
7158 Dep := Expression (Alt);
7160 if not Is_OK_Static_Expression (Dep) then
7161 raise Non_Static;
7163 elsif Is_True (Expr_Value (Dep)) then
7164 Append_List_To (Choices,
7165 New_Copy_List (Discrete_Choices (Alt)));
7166 end if;
7168 Next (Alt);
7169 end loop;
7171 return Membership_Entries (First (Choices));
7172 end;
7174 -- Expression with actions: if no actions, dig out expression
7176 when N_Expression_With_Actions =>
7177 if Is_Empty_List (Actions (Exp)) then
7178 return Get_RList (Expression (Exp));
7179 else
7180 raise Non_Static;
7181 end if;
7183 -- Xor operator
7185 when N_Op_Xor =>
7186 return (Get_RList (Left_Opnd (Exp))
7187 and not Get_RList (Right_Opnd (Exp)))
7188 or (Get_RList (Right_Opnd (Exp))
7189 and not Get_RList (Left_Opnd (Exp)));
7191 -- Any other node type is non-static
7193 when others =>
7194 raise Non_Static;
7195 end case;
7196 end Get_RList;
7198 ------------
7199 -- Hi_Val --
7200 ------------
7202 function Hi_Val (N : Node_Id) return Uint is
7203 begin
7204 if Is_OK_Static_Expression (N) then
7205 return Expr_Value (N);
7206 else
7207 pragma Assert (Nkind (N) = N_Range);
7208 return Expr_Value (High_Bound (N));
7209 end if;
7210 end Hi_Val;
7212 --------------
7213 -- Is_False --
7214 --------------
7216 function Is_False (R : RList) return Boolean is
7217 begin
7218 return R'Length = 0;
7219 end Is_False;
7221 -------------
7222 -- Is_True --
7223 -------------
7225 function Is_True (R : RList) return Boolean is
7226 begin
7227 return R'Length = 1
7228 and then R (R'First).Lo = BLo
7229 and then R (R'First).Hi = BHi;
7230 end Is_True;
7232 -----------------
7233 -- Is_Type_Ref --
7234 -----------------
7236 function Is_Type_Ref (N : Node_Id) return Boolean is
7237 begin
7238 return Nkind (N) = N_Identifier
7239 and then Chars (N) = Nam
7240 and then Paren_Count (N) = 0;
7241 end Is_Type_Ref;
7243 ------------
7244 -- Lo_Val --
7245 ------------
7247 function Lo_Val (N : Node_Id) return Uint is
7248 begin
7249 if Is_OK_Static_Expression (N) then
7250 return Expr_Value (N);
7251 else
7252 pragma Assert (Nkind (N) = N_Range);
7253 return Expr_Value (Low_Bound (N));
7254 end if;
7255 end Lo_Val;
7257 ------------------------
7258 -- Membership_Entries --
7259 ------------------------
7261 function Membership_Entries (N : Node_Id) return RList is
7262 begin
7263 if No (Next (N)) then
7264 return Membership_Entry (N);
7265 else
7266 return Membership_Entry (N) or Membership_Entries (Next (N));
7267 end if;
7268 end Membership_Entries;
7270 ----------------------
7271 -- Membership_Entry --
7272 ----------------------
7274 function Membership_Entry (N : Node_Id) return RList is
7275 Val : Uint;
7276 SLo : Uint;
7277 SHi : Uint;
7279 begin
7280 -- Range case
7282 if Nkind (N) = N_Range then
7283 if not Is_OK_Static_Expression (Low_Bound (N))
7284 or else
7285 not Is_OK_Static_Expression (High_Bound (N))
7286 then
7287 raise Non_Static;
7288 else
7289 SLo := Expr_Value (Low_Bound (N));
7290 SHi := Expr_Value (High_Bound (N));
7291 return RList'(1 => REnt'(SLo, SHi));
7292 end if;
7294 -- Static expression case
7296 elsif Is_OK_Static_Expression (N) then
7297 Val := Expr_Value (N);
7298 return RList'(1 => REnt'(Val, Val));
7300 -- Identifier (other than static expression) case
7302 else pragma Assert (Nkind (N) = N_Identifier);
7304 -- Type case
7306 if Is_Type (Entity (N)) then
7308 -- If type has predicates, process them
7310 if Has_Predicates (Entity (N)) then
7311 return Stat_Pred (Entity (N));
7313 -- For static subtype without predicates, get range
7315 elsif Is_OK_Static_Subtype (Entity (N)) then
7316 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7317 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7318 return RList'(1 => REnt'(SLo, SHi));
7320 -- Any other type makes us non-static
7322 else
7323 raise Non_Static;
7324 end if;
7326 -- Any other kind of identifier in predicate (e.g. a non-static
7327 -- expression value) means this is not a static predicate.
7329 else
7330 raise Non_Static;
7331 end if;
7332 end if;
7333 end Membership_Entry;
7335 ---------------
7336 -- Stat_Pred --
7337 ---------------
7339 function Stat_Pred (Typ : Entity_Id) return RList is
7340 begin
7341 -- Not static if type does not have static predicates
7343 if not Has_Static_Predicate (Typ) then
7344 raise Non_Static;
7345 end if;
7347 -- Otherwise we convert the predicate list to a range list
7349 declare
7350 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7351 Result : RList (1 .. List_Length (Spred));
7352 P : Node_Id;
7354 begin
7355 P := First (Static_Discrete_Predicate (Typ));
7356 for J in Result'Range loop
7357 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7358 Next (P);
7359 end loop;
7361 return Result;
7362 end;
7363 end Stat_Pred;
7365 -- Start of processing for Build_Discrete_Static_Predicate
7367 begin
7368 -- Analyze the expression to see if it is a static predicate
7370 declare
7371 Ranges : constant RList := Get_RList (Expr);
7372 -- Range list from expression if it is static
7374 Plist : List_Id;
7376 begin
7377 -- Convert range list into a form for the static predicate. In the
7378 -- Ranges array, we just have raw ranges, these must be converted
7379 -- to properly typed and analyzed static expressions or range nodes.
7381 -- Note: here we limit ranges to the ranges of the subtype, so that
7382 -- a predicate is always false for values outside the subtype. That
7383 -- seems fine, such values are invalid anyway, and considering them
7384 -- to fail the predicate seems allowed and friendly, and furthermore
7385 -- simplifies processing for case statements and loops.
7387 Plist := New_List;
7389 for J in Ranges'Range loop
7390 declare
7391 Lo : Uint := Ranges (J).Lo;
7392 Hi : Uint := Ranges (J).Hi;
7394 begin
7395 -- Ignore completely out of range entry
7397 if Hi < TLo or else Lo > THi then
7398 null;
7400 -- Otherwise process entry
7402 else
7403 -- Adjust out of range value to subtype range
7405 if Lo < TLo then
7406 Lo := TLo;
7407 end if;
7409 if Hi > THi then
7410 Hi := THi;
7411 end if;
7413 -- Convert range into required form
7415 Append_To (Plist, Build_Range (Lo, Hi));
7416 end if;
7417 end;
7418 end loop;
7420 -- Processing was successful and all entries were static, so now we
7421 -- can store the result as the predicate list.
7423 Set_Static_Discrete_Predicate (Typ, Plist);
7425 -- The processing for static predicates put the expression into
7426 -- canonical form as a series of ranges. It also eliminated
7427 -- duplicates and collapsed and combined ranges. We might as well
7428 -- replace the alternatives list of the right operand of the
7429 -- membership test with the static predicate list, which will
7430 -- usually be more efficient.
7432 declare
7433 New_Alts : constant List_Id := New_List;
7434 Old_Node : Node_Id;
7435 New_Node : Node_Id;
7437 begin
7438 Old_Node := First (Plist);
7439 while Present (Old_Node) loop
7440 New_Node := New_Copy (Old_Node);
7442 if Nkind (New_Node) = N_Range then
7443 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7444 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7445 end if;
7447 Append_To (New_Alts, New_Node);
7448 Next (Old_Node);
7449 end loop;
7451 -- If empty list, replace by False
7453 if Is_Empty_List (New_Alts) then
7454 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7456 -- Else replace by set membership test
7458 else
7459 Rewrite (Expr,
7460 Make_In (Loc,
7461 Left_Opnd => Make_Identifier (Loc, Nam),
7462 Right_Opnd => Empty,
7463 Alternatives => New_Alts));
7465 -- Resolve new expression in function context
7467 Install_Formals (Predicate_Function (Typ));
7468 Push_Scope (Predicate_Function (Typ));
7469 Analyze_And_Resolve (Expr, Standard_Boolean);
7470 Pop_Scope;
7471 end if;
7472 end;
7473 end;
7475 -- If non-static, return doing nothing
7477 exception
7478 when Non_Static =>
7479 return;
7480 end Build_Discrete_Static_Predicate;
7482 -------------------------------------------
7483 -- Build_Invariant_Procedure_Declaration --
7484 -------------------------------------------
7486 function Build_Invariant_Procedure_Declaration
7487 (Typ : Entity_Id) return Node_Id
7489 Loc : constant Source_Ptr := Sloc (Typ);
7490 Object_Entity : constant Entity_Id :=
7491 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7492 Spec : Node_Id;
7493 SId : Entity_Id;
7495 begin
7496 Set_Etype (Object_Entity, Typ);
7498 -- Check for duplicate definiations.
7500 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7501 return Empty;
7502 end if;
7504 SId :=
7505 Make_Defining_Identifier (Loc,
7506 Chars => New_External_Name (Chars (Typ), "Invariant"));
7507 Set_Has_Invariants (Typ);
7508 Set_Ekind (SId, E_Procedure);
7509 Set_Etype (SId, Standard_Void_Type);
7510 Set_Is_Invariant_Procedure (SId);
7511 Set_Invariant_Procedure (Typ, SId);
7513 Spec :=
7514 Make_Procedure_Specification (Loc,
7515 Defining_Unit_Name => SId,
7516 Parameter_Specifications => New_List (
7517 Make_Parameter_Specification (Loc,
7518 Defining_Identifier => Object_Entity,
7519 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7521 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7522 end Build_Invariant_Procedure_Declaration;
7524 -------------------------------
7525 -- Build_Invariant_Procedure --
7526 -------------------------------
7528 -- The procedure that is constructed here has the form
7530 -- procedure typInvariant (Ixxx : typ) is
7531 -- begin
7532 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7533 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7534 -- ...
7535 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7536 -- ...
7537 -- end typInvariant;
7539 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7540 Loc : constant Source_Ptr := Sloc (Typ);
7541 Stmts : List_Id;
7542 Spec : Node_Id;
7543 SId : Entity_Id;
7544 PDecl : Node_Id;
7545 PBody : Node_Id;
7547 Nam : Name_Id;
7548 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7549 -- if we come from a Type_Invariant aspect, we make sure to build the
7550 -- Check pragma with the right name, so that Check_Policy works right.
7552 Visible_Decls : constant List_Id := Visible_Declarations (N);
7553 Private_Decls : constant List_Id := Private_Declarations (N);
7555 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7556 -- Appends statements to Stmts for any invariants in the rep item chain
7557 -- of the given type. If Inherit is False, then we only process entries
7558 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7559 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7560 -- "inherited" to the exception message and generating an informational
7561 -- message about the inheritance of an invariant.
7563 Object_Name : Name_Id;
7564 -- Name for argument of invariant procedure
7566 Object_Entity : Node_Id;
7567 -- The entity of the formal for the procedure
7569 --------------------
7570 -- Add_Invariants --
7571 --------------------
7573 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7574 Ritem : Node_Id;
7575 Arg1 : Node_Id;
7576 Arg2 : Node_Id;
7577 Arg3 : Node_Id;
7578 Exp : Node_Id;
7579 Loc : Source_Ptr;
7580 Assoc : List_Id;
7581 Str : String_Id;
7583 procedure Replace_Type_Reference (N : Node_Id);
7584 -- Replace a single occurrence N of the subtype name with a reference
7585 -- to the formal of the predicate function. N can be an identifier
7586 -- referencing the subtype, or a selected component, representing an
7587 -- appropriately qualified occurrence of the subtype name.
7589 procedure Replace_Type_References is
7590 new Replace_Type_References_Generic (Replace_Type_Reference);
7591 -- Traverse an expression replacing all occurrences of the subtype
7592 -- name with appropriate references to the object that is the formal
7593 -- parameter of the predicate function. Note that we must ensure
7594 -- that the type and entity information is properly set in the
7595 -- replacement node, since we will do a Preanalyze call of this
7596 -- expression without proper visibility of the procedure argument.
7598 ----------------------------
7599 -- Replace_Type_Reference --
7600 ----------------------------
7602 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7603 -- regarding handling of Sloc and Comes_From_Source.
7605 procedure Replace_Type_Reference (N : Node_Id) is
7606 begin
7608 -- Add semantic information to node to be rewritten, for ASIS
7609 -- navigation needs.
7611 if Nkind (N) = N_Identifier then
7612 Set_Entity (N, T);
7613 Set_Etype (N, T);
7615 elsif Nkind (N) = N_Selected_Component then
7616 Analyze (Prefix (N));
7617 Set_Entity (Selector_Name (N), T);
7618 Set_Etype (Selector_Name (N), T);
7619 end if;
7621 -- Invariant'Class, replace with T'Class (obj)
7622 -- In ASIS mode, an inherited item is analyzed already, and the
7623 -- replacement has been done, so do not repeat transformation
7624 -- to prevent ill-formed tree.
7626 if Class_Present (Ritem) then
7627 if ASIS_Mode
7628 and then Nkind (Parent (N)) = N_Attribute_Reference
7629 and then Attribute_Name (Parent (N)) = Name_Class
7630 then
7631 null;
7633 else
7634 Rewrite (N,
7635 Make_Type_Conversion (Sloc (N),
7636 Subtype_Mark =>
7637 Make_Attribute_Reference (Sloc (N),
7638 Prefix => New_Occurrence_Of (T, Sloc (N)),
7639 Attribute_Name => Name_Class),
7640 Expression =>
7641 Make_Identifier (Sloc (N), Object_Name)));
7643 Set_Entity (Expression (N), Object_Entity);
7644 Set_Etype (Expression (N), Typ);
7645 end if;
7647 -- Invariant, replace with obj
7649 else
7650 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7651 Set_Entity (N, Object_Entity);
7652 Set_Etype (N, Typ);
7653 end if;
7655 Set_Comes_From_Source (N, True);
7656 end Replace_Type_Reference;
7658 -- Start of processing for Add_Invariants
7660 begin
7661 Ritem := First_Rep_Item (T);
7662 while Present (Ritem) loop
7663 if Nkind (Ritem) = N_Pragma
7664 and then Pragma_Name (Ritem) = Name_Invariant
7665 then
7666 Arg1 := First (Pragma_Argument_Associations (Ritem));
7667 Arg2 := Next (Arg1);
7668 Arg3 := Next (Arg2);
7670 Arg1 := Get_Pragma_Arg (Arg1);
7671 Arg2 := Get_Pragma_Arg (Arg2);
7673 -- For Inherit case, ignore Invariant, process only Class case
7675 if Inherit then
7676 if not Class_Present (Ritem) then
7677 goto Continue;
7678 end if;
7680 -- For Inherit false, process only item for right type
7682 else
7683 if Entity (Arg1) /= Typ then
7684 goto Continue;
7685 end if;
7686 end if;
7688 if No (Stmts) then
7689 Stmts := Empty_List;
7690 end if;
7692 Exp := New_Copy_Tree (Arg2);
7694 -- Preserve sloc of original pragma Invariant
7696 Loc := Sloc (Ritem);
7698 -- We need to replace any occurrences of the name of the type
7699 -- with references to the object, converted to type'Class in
7700 -- the case of Invariant'Class aspects.
7702 Replace_Type_References (Exp, T);
7704 -- If this invariant comes from an aspect, find the aspect
7705 -- specification, and replace the saved expression because
7706 -- we need the subtype references replaced for the calls to
7707 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7708 -- and Check_Aspect_At_End_Of_Declarations.
7710 if From_Aspect_Specification (Ritem) then
7711 declare
7712 Aitem : Node_Id;
7714 begin
7715 -- Loop to find corresponding aspect, note that this
7716 -- must be present given the pragma is marked delayed.
7718 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7719 -- this loop does nothing. Furthermore, why isn't this
7720 -- simply Corresponding_Aspect ???
7722 Aitem := Next_Rep_Item (Ritem);
7723 while Present (Aitem) loop
7724 if Nkind (Aitem) = N_Aspect_Specification
7725 and then Aspect_Rep_Item (Aitem) = Ritem
7726 then
7727 Set_Entity
7728 (Identifier (Aitem), New_Copy_Tree (Exp));
7729 exit;
7730 end if;
7732 Aitem := Next_Rep_Item (Aitem);
7733 end loop;
7734 end;
7735 end if;
7737 -- Now we need to preanalyze the expression to properly capture
7738 -- the visibility in the visible part. The expression will not
7739 -- be analyzed for real until the body is analyzed, but that is
7740 -- at the end of the private part and has the wrong visibility.
7742 Set_Parent (Exp, N);
7743 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
7745 -- A class-wide invariant may be inherited in a separate unit,
7746 -- where the corresponding expression cannot be resolved by
7747 -- visibility, because it refers to a local function. Propagate
7748 -- semantic information to the original representation item, to
7749 -- be used when an invariant procedure for a derived type is
7750 -- constructed.
7752 -- Unclear how to handle class-wide invariants that are not
7753 -- function calls ???
7755 if not Inherit
7756 and then Class_Present (Ritem)
7757 and then Nkind (Exp) = N_Function_Call
7758 and then Nkind (Arg2) = N_Indexed_Component
7759 then
7760 Rewrite (Arg2,
7761 Make_Function_Call (Loc,
7762 Name =>
7763 New_Occurrence_Of (Entity (Name (Exp)), Loc),
7764 Parameter_Associations =>
7765 New_Copy_List (Expressions (Arg2))));
7766 end if;
7768 -- In ASIS mode, even if assertions are not enabled, we must
7769 -- analyze the original expression in the aspect specification
7770 -- because it is part of the original tree.
7772 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7773 declare
7774 Inv : constant Node_Id :=
7775 Expression (Corresponding_Aspect (Ritem));
7776 begin
7777 Replace_Type_References (Inv, T);
7778 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7779 end;
7780 end if;
7782 -- Get name to be used for Check pragma
7784 if not From_Aspect_Specification (Ritem) then
7785 Nam := Name_Invariant;
7786 else
7787 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7788 end if;
7790 -- Build first two arguments for Check pragma
7792 Assoc :=
7793 New_List (
7794 Make_Pragma_Argument_Association (Loc,
7795 Expression => Make_Identifier (Loc, Chars => Nam)),
7796 Make_Pragma_Argument_Association (Loc,
7797 Expression => Exp));
7799 -- Add message if present in Invariant pragma
7801 if Present (Arg3) then
7802 Str := Strval (Get_Pragma_Arg (Arg3));
7804 -- If inherited case, and message starts "failed invariant",
7805 -- change it to be "failed inherited invariant".
7807 if Inherit then
7808 String_To_Name_Buffer (Str);
7810 if Name_Buffer (1 .. 16) = "failed invariant" then
7811 Insert_Str_In_Name_Buffer ("inherited ", 8);
7812 Str := String_From_Name_Buffer;
7813 end if;
7814 end if;
7816 Append_To (Assoc,
7817 Make_Pragma_Argument_Association (Loc,
7818 Expression => Make_String_Literal (Loc, Str)));
7819 end if;
7821 -- Add Check pragma to list of statements
7823 Append_To (Stmts,
7824 Make_Pragma (Loc,
7825 Pragma_Identifier =>
7826 Make_Identifier (Loc, Name_Check),
7827 Pragma_Argument_Associations => Assoc));
7829 -- If Inherited case and option enabled, output info msg. Note
7830 -- that we know this is a case of Invariant'Class.
7832 if Inherit and Opt.List_Inherited_Aspects then
7833 Error_Msg_Sloc := Sloc (Ritem);
7834 Error_Msg_N
7835 ("info: & inherits `Invariant''Class` aspect from #?L?",
7836 Typ);
7837 end if;
7838 end if;
7840 <<Continue>>
7841 Next_Rep_Item (Ritem);
7842 end loop;
7843 end Add_Invariants;
7845 -- Start of processing for Build_Invariant_Procedure
7847 begin
7848 Stmts := No_List;
7849 PDecl := Empty;
7850 PBody := Empty;
7851 SId := Empty;
7853 -- If the aspect specification exists for some view of the type, the
7854 -- declaration for the procedure has been created.
7856 if Has_Invariants (Typ) then
7857 SId := Invariant_Procedure (Typ);
7858 end if;
7860 -- If the body is already present, nothing to do. This will occur when
7861 -- the type is already frozen, which is the case when the invariant
7862 -- appears in a private part, and the freezing takes place before the
7863 -- final pass over full declarations.
7865 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
7867 if Present (SId) then
7868 PDecl := Unit_Declaration_Node (SId);
7870 if Present (PDecl)
7871 and then Nkind (PDecl) = N_Subprogram_Declaration
7872 and then Present (Corresponding_Body (PDecl))
7873 then
7874 return;
7875 end if;
7877 else
7878 PDecl := Build_Invariant_Procedure_Declaration (Typ);
7879 end if;
7881 -- Recover formal of procedure, for use in the calls to invariant
7882 -- functions (including inherited ones).
7884 Object_Entity :=
7885 Defining_Identifier
7886 (First (Parameter_Specifications (Specification (PDecl))));
7887 Object_Name := Chars (Object_Entity);
7889 -- Add invariants for the current type
7891 Add_Invariants (Typ, Inherit => False);
7893 -- Add invariants for parent types
7895 declare
7896 Current_Typ : Entity_Id;
7897 Parent_Typ : Entity_Id;
7899 begin
7900 Current_Typ := Typ;
7901 loop
7902 Parent_Typ := Etype (Current_Typ);
7904 if Is_Private_Type (Parent_Typ)
7905 and then Present (Full_View (Base_Type (Parent_Typ)))
7906 then
7907 Parent_Typ := Full_View (Base_Type (Parent_Typ));
7908 end if;
7910 exit when Parent_Typ = Current_Typ;
7912 Current_Typ := Parent_Typ;
7913 Add_Invariants (Current_Typ, Inherit => True);
7914 end loop;
7915 end;
7917 -- Build the procedure if we generated at least one Check pragma
7919 if Stmts /= No_List then
7920 Spec := Copy_Separate_Tree (Specification (PDecl));
7922 PBody :=
7923 Make_Subprogram_Body (Loc,
7924 Specification => Spec,
7925 Declarations => Empty_List,
7926 Handled_Statement_Sequence =>
7927 Make_Handled_Sequence_Of_Statements (Loc,
7928 Statements => Stmts));
7930 -- Insert procedure declaration and spec at the appropriate points.
7931 -- If declaration is already analyzed, it was processed by the
7932 -- generated pragma.
7934 if Present (Private_Decls) then
7936 -- The spec goes at the end of visible declarations, but they have
7937 -- already been analyzed, so we need to explicitly do the analyze.
7939 if not Analyzed (PDecl) then
7940 Append_To (Visible_Decls, PDecl);
7941 Analyze (PDecl);
7942 end if;
7944 -- The body goes at the end of the private declarations, which we
7945 -- have not analyzed yet, so we do not need to perform an explicit
7946 -- analyze call. We skip this if there are no private declarations
7947 -- (this is an error that will be caught elsewhere);
7949 Append_To (Private_Decls, PBody);
7951 -- If the invariant appears on the full view of a type, the
7952 -- analysis of the private part is complete, and we must
7953 -- analyze the new body explicitly.
7955 if In_Private_Part (Current_Scope) then
7956 Analyze (PBody);
7957 end if;
7959 -- If there are no private declarations this may be an error that
7960 -- will be diagnosed elsewhere. However, if this is a non-private
7961 -- type that inherits invariants, it needs no completion and there
7962 -- may be no private part. In this case insert invariant procedure
7963 -- at end of current declarative list, and analyze at once, given
7964 -- that the type is about to be frozen.
7966 elsif not Is_Private_Type (Typ) then
7967 Append_To (Visible_Decls, PDecl);
7968 Append_To (Visible_Decls, PBody);
7969 Analyze (PDecl);
7970 Analyze (PBody);
7971 end if;
7972 end if;
7973 end Build_Invariant_Procedure;
7975 -------------------------------
7976 -- Build_Predicate_Functions --
7977 -------------------------------
7979 -- The procedures that are constructed here have the form:
7981 -- function typPredicate (Ixxx : typ) return Boolean is
7982 -- begin
7983 -- return
7984 -- exp1 and then exp2 and then ...
7985 -- and then typ1Predicate (typ1 (Ixxx))
7986 -- and then typ2Predicate (typ2 (Ixxx))
7987 -- and then ...;
7988 -- end typPredicate;
7990 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
7991 -- this is the point at which these expressions get analyzed, providing the
7992 -- required delay, and typ1, typ2, are entities from which predicates are
7993 -- inherited. Note that we do NOT generate Check pragmas, that's because we
7994 -- use this function even if checks are off, e.g. for membership tests.
7996 -- If the expression has at least one Raise_Expression, then we also build
7997 -- the typPredicateM version of the function, in which any occurrence of a
7998 -- Raise_Expression is converted to "return False".
8000 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8001 Loc : constant Source_Ptr := Sloc (Typ);
8003 Expr : Node_Id;
8004 -- This is the expression for the result of the function. It is
8005 -- is build by connecting the component predicates with AND THEN.
8007 Expr_M : Node_Id;
8008 -- This is the corresponding return expression for the Predicate_M
8009 -- function. It differs in that raise expressions are marked for
8010 -- special expansion (see Process_REs).
8012 Object_Name : constant Name_Id := New_Internal_Name ('I');
8013 -- Name for argument of Predicate procedure. Note that we use the same
8014 -- name for both predicate functions. That way the reference within the
8015 -- predicate expression is the same in both functions.
8017 Object_Entity : constant Entity_Id :=
8018 Make_Defining_Identifier (Loc, Chars => Object_Name);
8019 -- Entity for argument of Predicate procedure
8021 Object_Entity_M : constant Entity_Id :=
8022 Make_Defining_Identifier (Loc, Chars => Object_Name);
8023 -- Entity for argument of Predicate_M procedure
8025 Raise_Expression_Present : Boolean := False;
8026 -- Set True if Expr has at least one Raise_Expression
8028 procedure Add_Call (T : Entity_Id);
8029 -- Includes a call to the predicate function for type T in Expr if T
8030 -- has predicates and Predicate_Function (T) is non-empty.
8032 procedure Add_Predicates;
8033 -- Appends expressions for any Predicate pragmas in the rep item chain
8034 -- Typ to Expr. Note that we look only at items for this exact entity.
8035 -- Inheritance of predicates for the parent type is done by calling the
8036 -- Predicate_Function of the parent type, using Add_Call above.
8038 function Test_RE (N : Node_Id) return Traverse_Result;
8039 -- Used in Test_REs, tests one node for being a raise expression, and if
8040 -- so sets Raise_Expression_Present True.
8042 procedure Test_REs is new Traverse_Proc (Test_RE);
8043 -- Tests to see if Expr contains any raise expressions
8045 function Process_RE (N : Node_Id) return Traverse_Result;
8046 -- Used in Process REs, tests if node N is a raise expression, and if
8047 -- so, marks it to be converted to return False.
8049 procedure Process_REs is new Traverse_Proc (Process_RE);
8050 -- Marks any raise expressions in Expr_M to return False
8052 --------------
8053 -- Add_Call --
8054 --------------
8056 procedure Add_Call (T : Entity_Id) is
8057 Exp : Node_Id;
8059 begin
8060 if Present (T) and then Present (Predicate_Function (T)) then
8061 Set_Has_Predicates (Typ);
8063 -- Build the call to the predicate function of T
8065 Exp :=
8066 Make_Predicate_Call
8067 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8069 -- Add call to evolving expression, using AND THEN if needed
8071 if No (Expr) then
8072 Expr := Exp;
8074 else
8075 Expr :=
8076 Make_And_Then (Sloc (Expr),
8077 Left_Opnd => Relocate_Node (Expr),
8078 Right_Opnd => Exp);
8079 end if;
8081 -- Output info message on inheritance if required. Note we do not
8082 -- give this information for generic actual types, since it is
8083 -- unwelcome noise in that case in instantiations. We also
8084 -- generally suppress the message in instantiations, and also
8085 -- if it involves internal names.
8087 if Opt.List_Inherited_Aspects
8088 and then not Is_Generic_Actual_Type (Typ)
8089 and then Instantiation_Depth (Sloc (Typ)) = 0
8090 and then not Is_Internal_Name (Chars (T))
8091 and then not Is_Internal_Name (Chars (Typ))
8092 then
8093 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8094 Error_Msg_Node_2 := T;
8095 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8096 end if;
8097 end if;
8098 end Add_Call;
8100 --------------------
8101 -- Add_Predicates --
8102 --------------------
8104 procedure Add_Predicates is
8105 Ritem : Node_Id;
8106 Arg1 : Node_Id;
8107 Arg2 : Node_Id;
8109 procedure Replace_Type_Reference (N : Node_Id);
8110 -- Replace a single occurrence N of the subtype name with a reference
8111 -- to the formal of the predicate function. N can be an identifier
8112 -- referencing the subtype, or a selected component, representing an
8113 -- appropriately qualified occurrence of the subtype name.
8115 procedure Replace_Type_References is
8116 new Replace_Type_References_Generic (Replace_Type_Reference);
8117 -- Traverse an expression changing every occurrence of an identifier
8118 -- whose name matches the name of the subtype with a reference to
8119 -- the formal parameter of the predicate function.
8121 ----------------------------
8122 -- Replace_Type_Reference --
8123 ----------------------------
8125 procedure Replace_Type_Reference (N : Node_Id) is
8126 begin
8127 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8128 -- Use the Sloc of the usage name, not the defining name
8130 Set_Etype (N, Typ);
8131 Set_Entity (N, Object_Entity);
8133 -- We want to treat the node as if it comes from source, so that
8134 -- ASIS will not ignore it
8136 Set_Comes_From_Source (N, True);
8137 end Replace_Type_Reference;
8139 -- Start of processing for Add_Predicates
8141 begin
8142 Ritem := First_Rep_Item (Typ);
8143 while Present (Ritem) loop
8144 if Nkind (Ritem) = N_Pragma
8145 and then Pragma_Name (Ritem) = Name_Predicate
8146 then
8147 -- Acquire arguments
8149 Arg1 := First (Pragma_Argument_Associations (Ritem));
8150 Arg2 := Next (Arg1);
8152 Arg1 := Get_Pragma_Arg (Arg1);
8153 Arg2 := Get_Pragma_Arg (Arg2);
8155 -- See if this predicate pragma is for the current type or for
8156 -- its full view. A predicate on a private completion is placed
8157 -- on the partial view beause this is the visible entity that
8158 -- is frozen.
8160 if Entity (Arg1) = Typ
8161 or else Full_View (Entity (Arg1)) = Typ
8162 then
8163 -- We have a match, this entry is for our subtype
8165 -- We need to replace any occurrences of the name of the
8166 -- type with references to the object.
8168 Replace_Type_References (Arg2, Typ);
8170 -- If this predicate comes from an aspect, find the aspect
8171 -- specification, and replace the saved expression because
8172 -- we need the subtype references replaced for the calls to
8173 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8174 -- and Check_Aspect_At_End_Of_Declarations.
8176 if From_Aspect_Specification (Ritem) then
8177 declare
8178 Aitem : Node_Id;
8180 begin
8181 -- Loop to find corresponding aspect, note that this
8182 -- must be present given the pragma is marked delayed.
8184 Aitem := Next_Rep_Item (Ritem);
8185 loop
8186 if Nkind (Aitem) = N_Aspect_Specification
8187 and then Aspect_Rep_Item (Aitem) = Ritem
8188 then
8189 Set_Entity
8190 (Identifier (Aitem), New_Copy_Tree (Arg2));
8191 exit;
8192 end if;
8194 Aitem := Next_Rep_Item (Aitem);
8195 end loop;
8196 end;
8197 end if;
8199 -- Now we can add the expression
8201 if No (Expr) then
8202 Expr := Relocate_Node (Arg2);
8204 -- There already was a predicate, so add to it
8206 else
8207 Expr :=
8208 Make_And_Then (Loc,
8209 Left_Opnd => Relocate_Node (Expr),
8210 Right_Opnd => Relocate_Node (Arg2));
8211 end if;
8212 end if;
8213 end if;
8215 Next_Rep_Item (Ritem);
8216 end loop;
8217 end Add_Predicates;
8219 ----------------
8220 -- Process_RE --
8221 ----------------
8223 function Process_RE (N : Node_Id) return Traverse_Result is
8224 begin
8225 if Nkind (N) = N_Raise_Expression then
8226 Set_Convert_To_Return_False (N);
8227 return Skip;
8228 else
8229 return OK;
8230 end if;
8231 end Process_RE;
8233 -------------
8234 -- Test_RE --
8235 -------------
8237 function Test_RE (N : Node_Id) return Traverse_Result is
8238 begin
8239 if Nkind (N) = N_Raise_Expression then
8240 Raise_Expression_Present := True;
8241 return Abandon;
8242 else
8243 return OK;
8244 end if;
8245 end Test_RE;
8247 -- Start of processing for Build_Predicate_Functions
8249 begin
8250 -- Return if already built or if type does not have predicates
8252 if not Has_Predicates (Typ)
8253 or else Present (Predicate_Function (Typ))
8254 then
8255 return;
8256 end if;
8258 -- Prepare to construct predicate expression
8260 Expr := Empty;
8262 -- Add Predicates for the current type
8264 Add_Predicates;
8266 -- Add predicates for ancestor if present
8268 declare
8269 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8270 begin
8271 if Present (Atyp) then
8272 Add_Call (Atyp);
8273 end if;
8274 end;
8276 -- Case where predicates are present
8278 if Present (Expr) then
8280 -- Test for raise expression present
8282 Test_REs (Expr);
8284 -- If raise expression is present, capture a copy of Expr for use
8285 -- in building the predicateM function version later on. For this
8286 -- copy we replace references to Object_Entity by Object_Entity_M.
8288 if Raise_Expression_Present then
8289 declare
8290 Map : constant Elist_Id := New_Elmt_List;
8291 New_V : Entity_Id := Empty;
8293 -- The unanalyzed expression will be copied and appear in
8294 -- both functions. Normally expressions do not declare new
8295 -- entities, but quantified expressions do, so we need to
8296 -- create new entities for their bound variables, to prevent
8297 -- multiple definitions in gigi.
8299 function Reset_Loop_Variable (N : Node_Id)
8300 return Traverse_Result;
8302 procedure Collect_Loop_Variables is
8303 new Traverse_Proc (Reset_Loop_Variable);
8305 ------------------------
8306 -- Reset_Loop_Variable --
8307 ------------------------
8309 function Reset_Loop_Variable (N : Node_Id)
8310 return Traverse_Result
8312 begin
8313 if Nkind (N) = N_Iterator_Specification then
8314 New_V := Make_Defining_Identifier
8315 (Sloc (N), Chars (Defining_Identifier (N)));
8317 Set_Defining_Identifier (N, New_V);
8318 end if;
8320 return OK;
8321 end Reset_Loop_Variable;
8323 begin
8324 Append_Elmt (Object_Entity, Map);
8325 Append_Elmt (Object_Entity_M, Map);
8326 Expr_M := New_Copy_Tree (Expr, Map => Map);
8327 Collect_Loop_Variables (Expr_M);
8328 end;
8329 end if;
8331 -- Build the main predicate function
8333 declare
8334 SId : constant Entity_Id :=
8335 Make_Defining_Identifier (Loc,
8336 Chars => New_External_Name (Chars (Typ), "Predicate"));
8337 -- The entity for the the function spec
8339 SIdB : constant Entity_Id :=
8340 Make_Defining_Identifier (Loc,
8341 Chars => New_External_Name (Chars (Typ), "Predicate"));
8342 -- The entity for the function body
8344 Spec : Node_Id;
8345 FDecl : Node_Id;
8346 FBody : Node_Id;
8348 begin
8349 -- Build function declaration
8351 Set_Ekind (SId, E_Function);
8352 Set_Is_Internal (SId);
8353 Set_Is_Predicate_Function (SId);
8354 Set_Predicate_Function (Typ, SId);
8356 -- The predicate function is shared between views of a type
8358 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8359 Set_Predicate_Function (Full_View (Typ), SId);
8360 end if;
8362 Spec :=
8363 Make_Function_Specification (Loc,
8364 Defining_Unit_Name => SId,
8365 Parameter_Specifications => New_List (
8366 Make_Parameter_Specification (Loc,
8367 Defining_Identifier => Object_Entity,
8368 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8369 Result_Definition =>
8370 New_Occurrence_Of (Standard_Boolean, Loc));
8372 FDecl :=
8373 Make_Subprogram_Declaration (Loc,
8374 Specification => Spec);
8376 -- Build function body
8378 Spec :=
8379 Make_Function_Specification (Loc,
8380 Defining_Unit_Name => SIdB,
8381 Parameter_Specifications => New_List (
8382 Make_Parameter_Specification (Loc,
8383 Defining_Identifier =>
8384 Make_Defining_Identifier (Loc, Object_Name),
8385 Parameter_Type =>
8386 New_Occurrence_Of (Typ, Loc))),
8387 Result_Definition =>
8388 New_Occurrence_Of (Standard_Boolean, Loc));
8390 FBody :=
8391 Make_Subprogram_Body (Loc,
8392 Specification => Spec,
8393 Declarations => Empty_List,
8394 Handled_Statement_Sequence =>
8395 Make_Handled_Sequence_Of_Statements (Loc,
8396 Statements => New_List (
8397 Make_Simple_Return_Statement (Loc,
8398 Expression => Expr))));
8400 -- Insert declaration before freeze node and body after
8402 Insert_Before_And_Analyze (N, FDecl);
8403 Insert_After_And_Analyze (N, FBody);
8404 end;
8406 -- Test for raise expressions present and if so build M version
8408 if Raise_Expression_Present then
8409 declare
8410 SId : constant Entity_Id :=
8411 Make_Defining_Identifier (Loc,
8412 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8413 -- The entity for the the function spec
8415 SIdB : constant Entity_Id :=
8416 Make_Defining_Identifier (Loc,
8417 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8418 -- The entity for the function body
8420 Spec : Node_Id;
8421 FDecl : Node_Id;
8422 FBody : Node_Id;
8423 BTemp : Entity_Id;
8425 begin
8426 -- Mark any raise expressions for special expansion
8428 Process_REs (Expr_M);
8430 -- Build function declaration
8432 Set_Ekind (SId, E_Function);
8433 Set_Is_Predicate_Function_M (SId);
8434 Set_Predicate_Function_M (Typ, SId);
8436 -- The predicate function is shared between views of a type
8438 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8439 Set_Predicate_Function_M (Full_View (Typ), SId);
8440 end if;
8442 Spec :=
8443 Make_Function_Specification (Loc,
8444 Defining_Unit_Name => SId,
8445 Parameter_Specifications => New_List (
8446 Make_Parameter_Specification (Loc,
8447 Defining_Identifier => Object_Entity_M,
8448 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8449 Result_Definition =>
8450 New_Occurrence_Of (Standard_Boolean, Loc));
8452 FDecl :=
8453 Make_Subprogram_Declaration (Loc,
8454 Specification => Spec);
8456 -- Build function body
8458 Spec :=
8459 Make_Function_Specification (Loc,
8460 Defining_Unit_Name => SIdB,
8461 Parameter_Specifications => New_List (
8462 Make_Parameter_Specification (Loc,
8463 Defining_Identifier =>
8464 Make_Defining_Identifier (Loc, Object_Name),
8465 Parameter_Type =>
8466 New_Occurrence_Of (Typ, Loc))),
8467 Result_Definition =>
8468 New_Occurrence_Of (Standard_Boolean, Loc));
8470 -- Build the body, we declare the boolean expression before
8471 -- doing the return, because we are not really confident of
8472 -- what happens if a return appears within a return.
8474 BTemp :=
8475 Make_Defining_Identifier (Loc,
8476 Chars => New_Internal_Name ('B'));
8478 FBody :=
8479 Make_Subprogram_Body (Loc,
8480 Specification => Spec,
8482 Declarations => New_List (
8483 Make_Object_Declaration (Loc,
8484 Defining_Identifier => BTemp,
8485 Constant_Present => True,
8486 Object_Definition =>
8487 New_Occurrence_Of (Standard_Boolean, Loc),
8488 Expression => Expr_M)),
8490 Handled_Statement_Sequence =>
8491 Make_Handled_Sequence_Of_Statements (Loc,
8492 Statements => New_List (
8493 Make_Simple_Return_Statement (Loc,
8494 Expression => New_Occurrence_Of (BTemp, Loc)))));
8496 -- Insert declaration before freeze node and body after
8498 Insert_Before_And_Analyze (N, FDecl);
8499 Insert_After_And_Analyze (N, FBody);
8500 end;
8501 end if;
8503 -- See if we have a static predicate. Note that the answer may be
8504 -- yes even if we have an explicit Dynamic_Predicate present.
8506 declare
8507 PS : Boolean;
8508 EN : Node_Id;
8510 begin
8511 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8512 PS := False;
8513 else
8514 PS := Is_Predicate_Static (Expr, Object_Name);
8515 end if;
8517 -- Case where we have a predicate-static aspect
8519 if PS then
8521 -- We don't set Has_Static_Predicate_Aspect, since we can have
8522 -- any of the three cases (Predicate, Dynamic_Predicate, or
8523 -- Static_Predicate) generating a predicate with an expression
8524 -- that is predicate-static. We just indicate that we have a
8525 -- predicate that can be treated as static.
8527 Set_Has_Static_Predicate (Typ);
8529 -- For discrete subtype, build the static predicate list
8531 if Is_Discrete_Type (Typ) then
8532 if not Is_Static_Subtype (Typ) then
8534 -- This can only happen in the presence of previous
8535 -- semantic errors.
8537 pragma Assert (Serious_Errors_Detected > 0);
8538 return;
8539 end if;
8541 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8543 -- If we don't get a static predicate list, it means that we
8544 -- have a case where this is not possible, most typically in
8545 -- the case where we inherit a dynamic predicate. We do not
8546 -- consider this an error, we just leave the predicate as
8547 -- dynamic. But if we do succeed in building the list, then
8548 -- we mark the predicate as static.
8550 if No (Static_Discrete_Predicate (Typ)) then
8551 Set_Has_Static_Predicate (Typ, False);
8552 end if;
8554 -- For real or string subtype, save predicate expression
8556 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8557 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8558 end if;
8560 -- Case of dynamic predicate (expression is not predicate-static)
8562 else
8563 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8564 -- is only set if we have an explicit Dynamic_Predicate aspect
8565 -- given. Here we may simply have a Predicate aspect where the
8566 -- expression happens not to be predicate-static.
8568 -- Emit an error when the predicate is categorized as static
8569 -- but its expression is not predicate-static.
8571 -- First a little fiddling to get a nice location for the
8572 -- message. If the expression is of the form (A and then B),
8573 -- then use the left operand for the Sloc. This avoids getting
8574 -- confused by a call to a higher-level predicate with a less
8575 -- convenient source location.
8577 EN := Expr;
8578 while Nkind (EN) = N_And_Then loop
8579 EN := Left_Opnd (EN);
8580 end loop;
8582 -- Now post appropriate message
8584 if Has_Static_Predicate_Aspect (Typ) then
8585 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8586 Error_Msg_F
8587 ("expression is not predicate-static (RM 3.2.4(16-22))",
8588 EN);
8589 else
8590 Error_Msg_F
8591 ("static predicate requires scalar or string type", EN);
8592 end if;
8593 end if;
8594 end if;
8595 end;
8596 end if;
8597 end Build_Predicate_Functions;
8599 -----------------------------------------
8600 -- Check_Aspect_At_End_Of_Declarations --
8601 -----------------------------------------
8603 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8604 Ent : constant Entity_Id := Entity (ASN);
8605 Ident : constant Node_Id := Identifier (ASN);
8606 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8608 End_Decl_Expr : constant Node_Id := Entity (Ident);
8609 -- Expression to be analyzed at end of declarations
8611 Freeze_Expr : constant Node_Id := Expression (ASN);
8612 -- Expression from call to Check_Aspect_At_Freeze_Point
8614 T : constant Entity_Id := Etype (Freeze_Expr);
8615 -- Type required for preanalyze call
8617 Err : Boolean;
8618 -- Set False if error
8620 -- On entry to this procedure, Entity (Ident) contains a copy of the
8621 -- original expression from the aspect, saved for this purpose, and
8622 -- but Expression (Ident) is a preanalyzed copy of the expression,
8623 -- preanalyzed just after the freeze point.
8625 procedure Check_Overloaded_Name;
8626 -- For aspects whose expression is simply a name, this routine checks if
8627 -- the name is overloaded or not. If so, it verifies there is an
8628 -- interpretation that matches the entity obtained at the freeze point,
8629 -- otherwise the compiler complains.
8631 ---------------------------
8632 -- Check_Overloaded_Name --
8633 ---------------------------
8635 procedure Check_Overloaded_Name is
8636 begin
8637 if not Is_Overloaded (End_Decl_Expr) then
8638 Err := not Is_Entity_Name (End_Decl_Expr)
8639 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8641 else
8642 Err := True;
8644 declare
8645 Index : Interp_Index;
8646 It : Interp;
8648 begin
8649 Get_First_Interp (End_Decl_Expr, Index, It);
8650 while Present (It.Typ) loop
8651 if It.Nam = Entity (Freeze_Expr) then
8652 Err := False;
8653 exit;
8654 end if;
8656 Get_Next_Interp (Index, It);
8657 end loop;
8658 end;
8659 end if;
8660 end Check_Overloaded_Name;
8662 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8664 begin
8665 -- Case of aspects Dimension, Dimension_System and Synchronization
8667 if A_Id = Aspect_Synchronization then
8668 return;
8670 -- Case of stream attributes, just have to compare entities. However,
8671 -- the expression is just a name (possibly overloaded), and there may
8672 -- be stream operations declared for unrelated types, so we just need
8673 -- to verify that one of these interpretations is the one available at
8674 -- at the freeze point.
8676 elsif A_Id = Aspect_Input or else
8677 A_Id = Aspect_Output or else
8678 A_Id = Aspect_Read or else
8679 A_Id = Aspect_Write
8680 then
8681 Analyze (End_Decl_Expr);
8682 Check_Overloaded_Name;
8684 elsif A_Id = Aspect_Variable_Indexing or else
8685 A_Id = Aspect_Constant_Indexing or else
8686 A_Id = Aspect_Default_Iterator or else
8687 A_Id = Aspect_Iterator_Element
8688 then
8689 -- Make type unfrozen before analysis, to prevent spurious errors
8690 -- about late attributes.
8692 Set_Is_Frozen (Ent, False);
8693 Analyze (End_Decl_Expr);
8694 Set_Is_Frozen (Ent, True);
8696 -- If the end of declarations comes before any other freeze
8697 -- point, the Freeze_Expr is not analyzed: no check needed.
8699 if Analyzed (Freeze_Expr) and then not In_Instance then
8700 Check_Overloaded_Name;
8701 else
8702 Err := False;
8703 end if;
8705 -- All other cases
8707 else
8708 -- Indicate that the expression comes from an aspect specification,
8709 -- which is used in subsequent analysis even if expansion is off.
8711 Set_Parent (End_Decl_Expr, ASN);
8713 -- In a generic context the aspect expressions have not been
8714 -- preanalyzed, so do it now. There are no conformance checks
8715 -- to perform in this case.
8717 if No (T) then
8718 Check_Aspect_At_Freeze_Point (ASN);
8719 return;
8721 -- The default values attributes may be defined in the private part,
8722 -- and the analysis of the expression may take place when only the
8723 -- partial view is visible. The expression must be scalar, so use
8724 -- the full view to resolve.
8726 elsif (A_Id = Aspect_Default_Value
8727 or else
8728 A_Id = Aspect_Default_Component_Value)
8729 and then Is_Private_Type (T)
8730 then
8731 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8733 else
8734 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8735 end if;
8737 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8738 end if;
8740 -- Output error message if error. Force error on aspect specification
8741 -- even if there is an error on the expression itself.
8743 if Err then
8744 Error_Msg_NE
8745 ("!visibility of aspect for& changes after freeze point",
8746 ASN, Ent);
8747 Error_Msg_NE
8748 ("info: & is frozen here, aspects evaluated at this point??",
8749 Freeze_Node (Ent), Ent);
8750 end if;
8751 end Check_Aspect_At_End_Of_Declarations;
8753 ----------------------------------
8754 -- Check_Aspect_At_Freeze_Point --
8755 ----------------------------------
8757 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8758 Ident : constant Node_Id := Identifier (ASN);
8759 -- Identifier (use Entity field to save expression)
8761 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8763 T : Entity_Id := Empty;
8764 -- Type required for preanalyze call
8766 begin
8767 -- On entry to this procedure, Entity (Ident) contains a copy of the
8768 -- original expression from the aspect, saved for this purpose.
8770 -- On exit from this procedure Entity (Ident) is unchanged, still
8771 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8772 -- of the expression, preanalyzed just after the freeze point.
8774 -- Make a copy of the expression to be preanalyzed
8776 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8778 -- Find type for preanalyze call
8780 case A_Id is
8782 -- No_Aspect should be impossible
8784 when No_Aspect =>
8785 raise Program_Error;
8787 -- Aspects taking an optional boolean argument
8789 when Boolean_Aspects |
8790 Library_Unit_Aspects =>
8792 T := Standard_Boolean;
8794 -- Aspects corresponding to attribute definition clauses
8796 when Aspect_Address =>
8797 T := RTE (RE_Address);
8799 when Aspect_Attach_Handler =>
8800 T := RTE (RE_Interrupt_ID);
8802 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8803 T := RTE (RE_Bit_Order);
8805 when Aspect_Convention =>
8806 return;
8808 when Aspect_CPU =>
8809 T := RTE (RE_CPU_Range);
8811 -- Default_Component_Value is resolved with the component type
8813 when Aspect_Default_Component_Value =>
8814 T := Component_Type (Entity (ASN));
8816 when Aspect_Default_Storage_Pool =>
8817 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8819 -- Default_Value is resolved with the type entity in question
8821 when Aspect_Default_Value =>
8822 T := Entity (ASN);
8824 when Aspect_Dispatching_Domain =>
8825 T := RTE (RE_Dispatching_Domain);
8827 when Aspect_External_Tag =>
8828 T := Standard_String;
8830 when Aspect_External_Name =>
8831 T := Standard_String;
8833 when Aspect_Link_Name =>
8834 T := Standard_String;
8836 when Aspect_Priority | Aspect_Interrupt_Priority =>
8837 T := Standard_Integer;
8839 when Aspect_Relative_Deadline =>
8840 T := RTE (RE_Time_Span);
8842 when Aspect_Small =>
8843 T := Universal_Real;
8845 -- For a simple storage pool, we have to retrieve the type of the
8846 -- pool object associated with the aspect's corresponding attribute
8847 -- definition clause.
8849 when Aspect_Simple_Storage_Pool =>
8850 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8852 when Aspect_Storage_Pool =>
8853 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8855 when Aspect_Alignment |
8856 Aspect_Component_Size |
8857 Aspect_Machine_Radix |
8858 Aspect_Object_Size |
8859 Aspect_Size |
8860 Aspect_Storage_Size |
8861 Aspect_Stream_Size |
8862 Aspect_Value_Size =>
8863 T := Any_Integer;
8865 when Aspect_Linker_Section =>
8866 T := Standard_String;
8868 when Aspect_Synchronization =>
8869 return;
8871 -- Special case, the expression of these aspects is just an entity
8872 -- that does not need any resolution, so just analyze.
8874 when Aspect_Input |
8875 Aspect_Output |
8876 Aspect_Read |
8877 Aspect_Suppress |
8878 Aspect_Unsuppress |
8879 Aspect_Warnings |
8880 Aspect_Write =>
8881 Analyze (Expression (ASN));
8882 return;
8884 -- Same for Iterator aspects, where the expression is a function
8885 -- name. Legality rules are checked separately.
8887 when Aspect_Constant_Indexing |
8888 Aspect_Default_Iterator |
8889 Aspect_Iterator_Element |
8890 Aspect_Variable_Indexing =>
8891 Analyze (Expression (ASN));
8892 return;
8894 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8896 when Aspect_Iterable =>
8897 T := Entity (ASN);
8899 declare
8900 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8901 Assoc : Node_Id;
8902 Expr : Node_Id;
8904 begin
8905 if Cursor = Any_Type then
8906 return;
8907 end if;
8909 Assoc := First (Component_Associations (Expression (ASN)));
8910 while Present (Assoc) loop
8911 Expr := Expression (Assoc);
8912 Analyze (Expr);
8914 if not Error_Posted (Expr) then
8915 Resolve_Iterable_Operation
8916 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8917 end if;
8919 Next (Assoc);
8920 end loop;
8921 end;
8923 return;
8925 -- Invariant/Predicate take boolean expressions
8927 when Aspect_Dynamic_Predicate |
8928 Aspect_Invariant |
8929 Aspect_Predicate |
8930 Aspect_Static_Predicate |
8931 Aspect_Type_Invariant =>
8932 T := Standard_Boolean;
8934 -- Here is the list of aspects that don't require delay analysis
8936 when Aspect_Abstract_State |
8937 Aspect_Annotate |
8938 Aspect_Contract_Cases |
8939 Aspect_Default_Initial_Condition |
8940 Aspect_Depends |
8941 Aspect_Dimension |
8942 Aspect_Dimension_System |
8943 Aspect_Extensions_Visible |
8944 Aspect_Ghost |
8945 Aspect_Global |
8946 Aspect_Implicit_Dereference |
8947 Aspect_Initial_Condition |
8948 Aspect_Initializes |
8949 Aspect_Obsolescent |
8950 Aspect_Part_Of |
8951 Aspect_Post |
8952 Aspect_Postcondition |
8953 Aspect_Pre |
8954 Aspect_Precondition |
8955 Aspect_Refined_Depends |
8956 Aspect_Refined_Global |
8957 Aspect_Refined_Post |
8958 Aspect_Refined_State |
8959 Aspect_SPARK_Mode |
8960 Aspect_Test_Case =>
8961 raise Program_Error;
8963 end case;
8965 -- Do the preanalyze call
8967 Preanalyze_Spec_Expression (Expression (ASN), T);
8968 end Check_Aspect_At_Freeze_Point;
8970 -----------------------------------
8971 -- Check_Constant_Address_Clause --
8972 -----------------------------------
8974 procedure Check_Constant_Address_Clause
8975 (Expr : Node_Id;
8976 U_Ent : Entity_Id)
8978 procedure Check_At_Constant_Address (Nod : Node_Id);
8979 -- Checks that the given node N represents a name whose 'Address is
8980 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8981 -- address value is the same at the point of declaration of U_Ent and at
8982 -- the time of elaboration of the address clause.
8984 procedure Check_Expr_Constants (Nod : Node_Id);
8985 -- Checks that Nod meets the requirements for a constant address clause
8986 -- in the sense of the enclosing procedure.
8988 procedure Check_List_Constants (Lst : List_Id);
8989 -- Check that all elements of list Lst meet the requirements for a
8990 -- constant address clause in the sense of the enclosing procedure.
8992 -------------------------------
8993 -- Check_At_Constant_Address --
8994 -------------------------------
8996 procedure Check_At_Constant_Address (Nod : Node_Id) is
8997 begin
8998 if Is_Entity_Name (Nod) then
8999 if Present (Address_Clause (Entity ((Nod)))) then
9000 Error_Msg_NE
9001 ("invalid address clause for initialized object &!",
9002 Nod, U_Ent);
9003 Error_Msg_NE
9004 ("address for& cannot" &
9005 " depend on another address clause! (RM 13.1(22))!",
9006 Nod, U_Ent);
9008 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9009 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9010 then
9011 Error_Msg_NE
9012 ("invalid address clause for initialized object &!",
9013 Nod, U_Ent);
9014 Error_Msg_Node_2 := U_Ent;
9015 Error_Msg_NE
9016 ("\& must be defined before & (RM 13.1(22))!",
9017 Nod, Entity (Nod));
9018 end if;
9020 elsif Nkind (Nod) = N_Selected_Component then
9021 declare
9022 T : constant Entity_Id := Etype (Prefix (Nod));
9024 begin
9025 if (Is_Record_Type (T)
9026 and then Has_Discriminants (T))
9027 or else
9028 (Is_Access_Type (T)
9029 and then Is_Record_Type (Designated_Type (T))
9030 and then Has_Discriminants (Designated_Type (T)))
9031 then
9032 Error_Msg_NE
9033 ("invalid address clause for initialized object &!",
9034 Nod, U_Ent);
9035 Error_Msg_N
9036 ("\address cannot depend on component" &
9037 " of discriminated record (RM 13.1(22))!",
9038 Nod);
9039 else
9040 Check_At_Constant_Address (Prefix (Nod));
9041 end if;
9042 end;
9044 elsif Nkind (Nod) = N_Indexed_Component then
9045 Check_At_Constant_Address (Prefix (Nod));
9046 Check_List_Constants (Expressions (Nod));
9048 else
9049 Check_Expr_Constants (Nod);
9050 end if;
9051 end Check_At_Constant_Address;
9053 --------------------------
9054 -- Check_Expr_Constants --
9055 --------------------------
9057 procedure Check_Expr_Constants (Nod : Node_Id) is
9058 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9059 Ent : Entity_Id := Empty;
9061 begin
9062 if Nkind (Nod) in N_Has_Etype
9063 and then Etype (Nod) = Any_Type
9064 then
9065 return;
9066 end if;
9068 case Nkind (Nod) is
9069 when N_Empty | N_Error =>
9070 return;
9072 when N_Identifier | N_Expanded_Name =>
9073 Ent := Entity (Nod);
9075 -- We need to look at the original node if it is different
9076 -- from the node, since we may have rewritten things and
9077 -- substituted an identifier representing the rewrite.
9079 if Original_Node (Nod) /= Nod then
9080 Check_Expr_Constants (Original_Node (Nod));
9082 -- If the node is an object declaration without initial
9083 -- value, some code has been expanded, and the expression
9084 -- is not constant, even if the constituents might be
9085 -- acceptable, as in A'Address + offset.
9087 if Ekind (Ent) = E_Variable
9088 and then
9089 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9090 and then
9091 No (Expression (Declaration_Node (Ent)))
9092 then
9093 Error_Msg_NE
9094 ("invalid address clause for initialized object &!",
9095 Nod, U_Ent);
9097 -- If entity is constant, it may be the result of expanding
9098 -- a check. We must verify that its declaration appears
9099 -- before the object in question, else we also reject the
9100 -- address clause.
9102 elsif Ekind (Ent) = E_Constant
9103 and then In_Same_Source_Unit (Ent, U_Ent)
9104 and then Sloc (Ent) > Loc_U_Ent
9105 then
9106 Error_Msg_NE
9107 ("invalid address clause for initialized object &!",
9108 Nod, U_Ent);
9109 end if;
9111 return;
9112 end if;
9114 -- Otherwise look at the identifier and see if it is OK
9116 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9117 or else Is_Type (Ent)
9118 then
9119 return;
9121 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9123 -- This is the case where we must have Ent defined before
9124 -- U_Ent. Clearly if they are in different units this
9125 -- requirement is met since the unit containing Ent is
9126 -- already processed.
9128 if not In_Same_Source_Unit (Ent, U_Ent) then
9129 return;
9131 -- Otherwise location of Ent must be before the location
9132 -- of U_Ent, that's what prior defined means.
9134 elsif Sloc (Ent) < Loc_U_Ent then
9135 return;
9137 else
9138 Error_Msg_NE
9139 ("invalid address clause for initialized object &!",
9140 Nod, U_Ent);
9141 Error_Msg_Node_2 := U_Ent;
9142 Error_Msg_NE
9143 ("\& must be defined before & (RM 13.1(22))!",
9144 Nod, Ent);
9145 end if;
9147 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9148 Check_Expr_Constants (Original_Node (Nod));
9150 else
9151 Error_Msg_NE
9152 ("invalid address clause for initialized object &!",
9153 Nod, U_Ent);
9155 if Comes_From_Source (Ent) then
9156 Error_Msg_NE
9157 ("\reference to variable& not allowed"
9158 & " (RM 13.1(22))!", Nod, Ent);
9159 else
9160 Error_Msg_N
9161 ("non-static expression not allowed"
9162 & " (RM 13.1(22))!", Nod);
9163 end if;
9164 end if;
9166 when N_Integer_Literal =>
9168 -- If this is a rewritten unchecked conversion, in a system
9169 -- where Address is an integer type, always use the base type
9170 -- for a literal value. This is user-friendly and prevents
9171 -- order-of-elaboration issues with instances of unchecked
9172 -- conversion.
9174 if Nkind (Original_Node (Nod)) = N_Function_Call then
9175 Set_Etype (Nod, Base_Type (Etype (Nod)));
9176 end if;
9178 when N_Real_Literal |
9179 N_String_Literal |
9180 N_Character_Literal =>
9181 return;
9183 when N_Range =>
9184 Check_Expr_Constants (Low_Bound (Nod));
9185 Check_Expr_Constants (High_Bound (Nod));
9187 when N_Explicit_Dereference =>
9188 Check_Expr_Constants (Prefix (Nod));
9190 when N_Indexed_Component =>
9191 Check_Expr_Constants (Prefix (Nod));
9192 Check_List_Constants (Expressions (Nod));
9194 when N_Slice =>
9195 Check_Expr_Constants (Prefix (Nod));
9196 Check_Expr_Constants (Discrete_Range (Nod));
9198 when N_Selected_Component =>
9199 Check_Expr_Constants (Prefix (Nod));
9201 when N_Attribute_Reference =>
9202 if Nam_In (Attribute_Name (Nod), Name_Address,
9203 Name_Access,
9204 Name_Unchecked_Access,
9205 Name_Unrestricted_Access)
9206 then
9207 Check_At_Constant_Address (Prefix (Nod));
9209 else
9210 Check_Expr_Constants (Prefix (Nod));
9211 Check_List_Constants (Expressions (Nod));
9212 end if;
9214 when N_Aggregate =>
9215 Check_List_Constants (Component_Associations (Nod));
9216 Check_List_Constants (Expressions (Nod));
9218 when N_Component_Association =>
9219 Check_Expr_Constants (Expression (Nod));
9221 when N_Extension_Aggregate =>
9222 Check_Expr_Constants (Ancestor_Part (Nod));
9223 Check_List_Constants (Component_Associations (Nod));
9224 Check_List_Constants (Expressions (Nod));
9226 when N_Null =>
9227 return;
9229 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9230 Check_Expr_Constants (Left_Opnd (Nod));
9231 Check_Expr_Constants (Right_Opnd (Nod));
9233 when N_Unary_Op =>
9234 Check_Expr_Constants (Right_Opnd (Nod));
9236 when N_Type_Conversion |
9237 N_Qualified_Expression |
9238 N_Allocator |
9239 N_Unchecked_Type_Conversion =>
9240 Check_Expr_Constants (Expression (Nod));
9242 when N_Function_Call =>
9243 if not Is_Pure (Entity (Name (Nod))) then
9244 Error_Msg_NE
9245 ("invalid address clause for initialized object &!",
9246 Nod, U_Ent);
9248 Error_Msg_NE
9249 ("\function & is not pure (RM 13.1(22))!",
9250 Nod, Entity (Name (Nod)));
9252 else
9253 Check_List_Constants (Parameter_Associations (Nod));
9254 end if;
9256 when N_Parameter_Association =>
9257 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9259 when others =>
9260 Error_Msg_NE
9261 ("invalid address clause for initialized object &!",
9262 Nod, U_Ent);
9263 Error_Msg_NE
9264 ("\must be constant defined before& (RM 13.1(22))!",
9265 Nod, U_Ent);
9266 end case;
9267 end Check_Expr_Constants;
9269 --------------------------
9270 -- Check_List_Constants --
9271 --------------------------
9273 procedure Check_List_Constants (Lst : List_Id) is
9274 Nod1 : Node_Id;
9276 begin
9277 if Present (Lst) then
9278 Nod1 := First (Lst);
9279 while Present (Nod1) loop
9280 Check_Expr_Constants (Nod1);
9281 Next (Nod1);
9282 end loop;
9283 end if;
9284 end Check_List_Constants;
9286 -- Start of processing for Check_Constant_Address_Clause
9288 begin
9289 -- If rep_clauses are to be ignored, no need for legality checks. In
9290 -- particular, no need to pester user about rep clauses that violate the
9291 -- rule on constant addresses, given that these clauses will be removed
9292 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9293 -- we want to relax these checks.
9295 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9296 Check_Expr_Constants (Expr);
9297 end if;
9298 end Check_Constant_Address_Clause;
9300 ---------------------------
9301 -- Check_Pool_Size_Clash --
9302 ---------------------------
9304 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9305 Post : Node_Id;
9307 begin
9308 -- We need to find out which one came first. Note that in the case of
9309 -- aspects mixed with pragmas there are cases where the processing order
9310 -- is reversed, which is why we do the check here.
9312 if Sloc (SP) < Sloc (SS) then
9313 Error_Msg_Sloc := Sloc (SP);
9314 Post := SS;
9315 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9317 else
9318 Error_Msg_Sloc := Sloc (SS);
9319 Post := SP;
9320 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9321 end if;
9323 Error_Msg_N
9324 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9325 end Check_Pool_Size_Clash;
9327 ----------------------------------------
9328 -- Check_Record_Representation_Clause --
9329 ----------------------------------------
9331 procedure Check_Record_Representation_Clause (N : Node_Id) is
9332 Loc : constant Source_Ptr := Sloc (N);
9333 Ident : constant Node_Id := Identifier (N);
9334 Rectype : Entity_Id;
9335 Fent : Entity_Id;
9336 CC : Node_Id;
9337 Fbit : Uint;
9338 Lbit : Uint;
9339 Hbit : Uint := Uint_0;
9340 Comp : Entity_Id;
9341 Pcomp : Entity_Id;
9343 Max_Bit_So_Far : Uint;
9344 -- Records the maximum bit position so far. If all field positions
9345 -- are monotonically increasing, then we can skip the circuit for
9346 -- checking for overlap, since no overlap is possible.
9348 Tagged_Parent : Entity_Id := Empty;
9349 -- This is set in the case of a derived tagged type for which we have
9350 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9351 -- positioned by record representation clauses). In this case we must
9352 -- check for overlap between components of this tagged type, and the
9353 -- components of its parent. Tagged_Parent will point to this parent
9354 -- type. For all other cases Tagged_Parent is left set to Empty.
9356 Parent_Last_Bit : Uint;
9357 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9358 -- last bit position for any field in the parent type. We only need to
9359 -- check overlap for fields starting below this point.
9361 Overlap_Check_Required : Boolean;
9362 -- Used to keep track of whether or not an overlap check is required
9364 Overlap_Detected : Boolean := False;
9365 -- Set True if an overlap is detected
9367 Ccount : Natural := 0;
9368 -- Number of component clauses in record rep clause
9370 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9371 -- Given two entities for record components or discriminants, checks
9372 -- if they have overlapping component clauses and issues errors if so.
9374 procedure Find_Component;
9375 -- Finds component entity corresponding to current component clause (in
9376 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9377 -- start/stop bits for the field. If there is no matching component or
9378 -- if the matching component does not have a component clause, then
9379 -- that's an error and Comp is set to Empty, but no error message is
9380 -- issued, since the message was already given. Comp is also set to
9381 -- Empty if the current "component clause" is in fact a pragma.
9383 -----------------------------
9384 -- Check_Component_Overlap --
9385 -----------------------------
9387 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9388 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9389 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9391 begin
9392 if Present (CC1) and then Present (CC2) then
9394 -- Exclude odd case where we have two tag components in the same
9395 -- record, both at location zero. This seems a bit strange, but
9396 -- it seems to happen in some circumstances, perhaps on an error.
9398 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9399 return;
9400 end if;
9402 -- Here we check if the two fields overlap
9404 declare
9405 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9406 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9407 E1 : constant Uint := S1 + Esize (C1_Ent);
9408 E2 : constant Uint := S2 + Esize (C2_Ent);
9410 begin
9411 if E2 <= S1 or else E1 <= S2 then
9412 null;
9413 else
9414 Error_Msg_Node_2 := Component_Name (CC2);
9415 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9416 Error_Msg_Node_1 := Component_Name (CC1);
9417 Error_Msg_N
9418 ("component& overlaps & #", Component_Name (CC1));
9419 Overlap_Detected := True;
9420 end if;
9421 end;
9422 end if;
9423 end Check_Component_Overlap;
9425 --------------------
9426 -- Find_Component --
9427 --------------------
9429 procedure Find_Component is
9431 procedure Search_Component (R : Entity_Id);
9432 -- Search components of R for a match. If found, Comp is set
9434 ----------------------
9435 -- Search_Component --
9436 ----------------------
9438 procedure Search_Component (R : Entity_Id) is
9439 begin
9440 Comp := First_Component_Or_Discriminant (R);
9441 while Present (Comp) loop
9443 -- Ignore error of attribute name for component name (we
9444 -- already gave an error message for this, so no need to
9445 -- complain here)
9447 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9448 null;
9449 else
9450 exit when Chars (Comp) = Chars (Component_Name (CC));
9451 end if;
9453 Next_Component_Or_Discriminant (Comp);
9454 end loop;
9455 end Search_Component;
9457 -- Start of processing for Find_Component
9459 begin
9460 -- Return with Comp set to Empty if we have a pragma
9462 if Nkind (CC) = N_Pragma then
9463 Comp := Empty;
9464 return;
9465 end if;
9467 -- Search current record for matching component
9469 Search_Component (Rectype);
9471 -- If not found, maybe component of base type discriminant that is
9472 -- absent from statically constrained first subtype.
9474 if No (Comp) then
9475 Search_Component (Base_Type (Rectype));
9476 end if;
9478 -- If no component, or the component does not reference the component
9479 -- clause in question, then there was some previous error for which
9480 -- we already gave a message, so just return with Comp Empty.
9482 if No (Comp) or else Component_Clause (Comp) /= CC then
9483 Check_Error_Detected;
9484 Comp := Empty;
9486 -- Normal case where we have a component clause
9488 else
9489 Fbit := Component_Bit_Offset (Comp);
9490 Lbit := Fbit + Esize (Comp) - 1;
9491 end if;
9492 end Find_Component;
9494 -- Start of processing for Check_Record_Representation_Clause
9496 begin
9497 Find_Type (Ident);
9498 Rectype := Entity (Ident);
9500 if Rectype = Any_Type then
9501 return;
9502 else
9503 Rectype := Underlying_Type (Rectype);
9504 end if;
9506 -- See if we have a fully repped derived tagged type
9508 declare
9509 PS : constant Entity_Id := Parent_Subtype (Rectype);
9511 begin
9512 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9513 Tagged_Parent := PS;
9515 -- Find maximum bit of any component of the parent type
9517 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9518 Pcomp := First_Entity (Tagged_Parent);
9519 while Present (Pcomp) loop
9520 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9521 if Component_Bit_Offset (Pcomp) /= No_Uint
9522 and then Known_Static_Esize (Pcomp)
9523 then
9524 Parent_Last_Bit :=
9525 UI_Max
9526 (Parent_Last_Bit,
9527 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9528 end if;
9530 Next_Entity (Pcomp);
9531 end if;
9532 end loop;
9533 end if;
9534 end;
9536 -- All done if no component clauses
9538 CC := First (Component_Clauses (N));
9540 if No (CC) then
9541 return;
9542 end if;
9544 -- If a tag is present, then create a component clause that places it
9545 -- at the start of the record (otherwise gigi may place it after other
9546 -- fields that have rep clauses).
9548 Fent := First_Entity (Rectype);
9550 if Nkind (Fent) = N_Defining_Identifier
9551 and then Chars (Fent) = Name_uTag
9552 then
9553 Set_Component_Bit_Offset (Fent, Uint_0);
9554 Set_Normalized_Position (Fent, Uint_0);
9555 Set_Normalized_First_Bit (Fent, Uint_0);
9556 Set_Normalized_Position_Max (Fent, Uint_0);
9557 Init_Esize (Fent, System_Address_Size);
9559 Set_Component_Clause (Fent,
9560 Make_Component_Clause (Loc,
9561 Component_Name => Make_Identifier (Loc, Name_uTag),
9563 Position => Make_Integer_Literal (Loc, Uint_0),
9564 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9565 Last_Bit =>
9566 Make_Integer_Literal (Loc,
9567 UI_From_Int (System_Address_Size))));
9569 Ccount := Ccount + 1;
9570 end if;
9572 Max_Bit_So_Far := Uint_Minus_1;
9573 Overlap_Check_Required := False;
9575 -- Process the component clauses
9577 while Present (CC) loop
9578 Find_Component;
9580 if Present (Comp) then
9581 Ccount := Ccount + 1;
9583 -- We need a full overlap check if record positions non-monotonic
9585 if Fbit <= Max_Bit_So_Far then
9586 Overlap_Check_Required := True;
9587 end if;
9589 Max_Bit_So_Far := Lbit;
9591 -- Check bit position out of range of specified size
9593 if Has_Size_Clause (Rectype)
9594 and then RM_Size (Rectype) <= Lbit
9595 then
9596 Error_Msg_N
9597 ("bit number out of range of specified size",
9598 Last_Bit (CC));
9600 -- Check for overlap with tag component
9602 else
9603 if Is_Tagged_Type (Rectype)
9604 and then Fbit < System_Address_Size
9605 then
9606 Error_Msg_NE
9607 ("component overlaps tag field of&",
9608 Component_Name (CC), Rectype);
9609 Overlap_Detected := True;
9610 end if;
9612 if Hbit < Lbit then
9613 Hbit := Lbit;
9614 end if;
9615 end if;
9617 -- Check parent overlap if component might overlap parent field
9619 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9620 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9621 while Present (Pcomp) loop
9622 if not Is_Tag (Pcomp)
9623 and then Chars (Pcomp) /= Name_uParent
9624 then
9625 Check_Component_Overlap (Comp, Pcomp);
9626 end if;
9628 Next_Component_Or_Discriminant (Pcomp);
9629 end loop;
9630 end if;
9631 end if;
9633 Next (CC);
9634 end loop;
9636 -- Now that we have processed all the component clauses, check for
9637 -- overlap. We have to leave this till last, since the components can
9638 -- appear in any arbitrary order in the representation clause.
9640 -- We do not need this check if all specified ranges were monotonic,
9641 -- as recorded by Overlap_Check_Required being False at this stage.
9643 -- This first section checks if there are any overlapping entries at
9644 -- all. It does this by sorting all entries and then seeing if there are
9645 -- any overlaps. If there are none, then that is decisive, but if there
9646 -- are overlaps, they may still be OK (they may result from fields in
9647 -- different variants).
9649 if Overlap_Check_Required then
9650 Overlap_Check1 : declare
9652 OC_Fbit : array (0 .. Ccount) of Uint;
9653 -- First-bit values for component clauses, the value is the offset
9654 -- of the first bit of the field from start of record. The zero
9655 -- entry is for use in sorting.
9657 OC_Lbit : array (0 .. Ccount) of Uint;
9658 -- Last-bit values for component clauses, the value is the offset
9659 -- of the last bit of the field from start of record. The zero
9660 -- entry is for use in sorting.
9662 OC_Count : Natural := 0;
9663 -- Count of entries in OC_Fbit and OC_Lbit
9665 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9666 -- Compare routine for Sort
9668 procedure OC_Move (From : Natural; To : Natural);
9669 -- Move routine for Sort
9671 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9673 -----------
9674 -- OC_Lt --
9675 -----------
9677 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9678 begin
9679 return OC_Fbit (Op1) < OC_Fbit (Op2);
9680 end OC_Lt;
9682 -------------
9683 -- OC_Move --
9684 -------------
9686 procedure OC_Move (From : Natural; To : Natural) is
9687 begin
9688 OC_Fbit (To) := OC_Fbit (From);
9689 OC_Lbit (To) := OC_Lbit (From);
9690 end OC_Move;
9692 -- Start of processing for Overlap_Check
9694 begin
9695 CC := First (Component_Clauses (N));
9696 while Present (CC) loop
9698 -- Exclude component clause already marked in error
9700 if not Error_Posted (CC) then
9701 Find_Component;
9703 if Present (Comp) then
9704 OC_Count := OC_Count + 1;
9705 OC_Fbit (OC_Count) := Fbit;
9706 OC_Lbit (OC_Count) := Lbit;
9707 end if;
9708 end if;
9710 Next (CC);
9711 end loop;
9713 Sorting.Sort (OC_Count);
9715 Overlap_Check_Required := False;
9716 for J in 1 .. OC_Count - 1 loop
9717 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9718 Overlap_Check_Required := True;
9719 exit;
9720 end if;
9721 end loop;
9722 end Overlap_Check1;
9723 end if;
9725 -- If Overlap_Check_Required is still True, then we have to do the full
9726 -- scale overlap check, since we have at least two fields that do
9727 -- overlap, and we need to know if that is OK since they are in
9728 -- different variant, or whether we have a definite problem.
9730 if Overlap_Check_Required then
9731 Overlap_Check2 : declare
9732 C1_Ent, C2_Ent : Entity_Id;
9733 -- Entities of components being checked for overlap
9735 Clist : Node_Id;
9736 -- Component_List node whose Component_Items are being checked
9738 Citem : Node_Id;
9739 -- Component declaration for component being checked
9741 begin
9742 C1_Ent := First_Entity (Base_Type (Rectype));
9744 -- Loop through all components in record. For each component check
9745 -- for overlap with any of the preceding elements on the component
9746 -- list containing the component and also, if the component is in
9747 -- a variant, check against components outside the case structure.
9748 -- This latter test is repeated recursively up the variant tree.
9750 Main_Component_Loop : while Present (C1_Ent) loop
9751 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9752 goto Continue_Main_Component_Loop;
9753 end if;
9755 -- Skip overlap check if entity has no declaration node. This
9756 -- happens with discriminants in constrained derived types.
9757 -- Possibly we are missing some checks as a result, but that
9758 -- does not seem terribly serious.
9760 if No (Declaration_Node (C1_Ent)) then
9761 goto Continue_Main_Component_Loop;
9762 end if;
9764 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9766 -- Loop through component lists that need checking. Check the
9767 -- current component list and all lists in variants above us.
9769 Component_List_Loop : loop
9771 -- If derived type definition, go to full declaration
9772 -- If at outer level, check discriminants if there are any.
9774 if Nkind (Clist) = N_Derived_Type_Definition then
9775 Clist := Parent (Clist);
9776 end if;
9778 -- Outer level of record definition, check discriminants
9780 if Nkind_In (Clist, N_Full_Type_Declaration,
9781 N_Private_Type_Declaration)
9782 then
9783 if Has_Discriminants (Defining_Identifier (Clist)) then
9784 C2_Ent :=
9785 First_Discriminant (Defining_Identifier (Clist));
9786 while Present (C2_Ent) loop
9787 exit when C1_Ent = C2_Ent;
9788 Check_Component_Overlap (C1_Ent, C2_Ent);
9789 Next_Discriminant (C2_Ent);
9790 end loop;
9791 end if;
9793 -- Record extension case
9795 elsif Nkind (Clist) = N_Derived_Type_Definition then
9796 Clist := Empty;
9798 -- Otherwise check one component list
9800 else
9801 Citem := First (Component_Items (Clist));
9802 while Present (Citem) loop
9803 if Nkind (Citem) = N_Component_Declaration then
9804 C2_Ent := Defining_Identifier (Citem);
9805 exit when C1_Ent = C2_Ent;
9806 Check_Component_Overlap (C1_Ent, C2_Ent);
9807 end if;
9809 Next (Citem);
9810 end loop;
9811 end if;
9813 -- Check for variants above us (the parent of the Clist can
9814 -- be a variant, in which case its parent is a variant part,
9815 -- and the parent of the variant part is a component list
9816 -- whose components must all be checked against the current
9817 -- component for overlap).
9819 if Nkind (Parent (Clist)) = N_Variant then
9820 Clist := Parent (Parent (Parent (Clist)));
9822 -- Check for possible discriminant part in record, this
9823 -- is treated essentially as another level in the
9824 -- recursion. For this case the parent of the component
9825 -- list is the record definition, and its parent is the
9826 -- full type declaration containing the discriminant
9827 -- specifications.
9829 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9830 Clist := Parent (Parent ((Clist)));
9832 -- If neither of these two cases, we are at the top of
9833 -- the tree.
9835 else
9836 exit Component_List_Loop;
9837 end if;
9838 end loop Component_List_Loop;
9840 <<Continue_Main_Component_Loop>>
9841 Next_Entity (C1_Ent);
9843 end loop Main_Component_Loop;
9844 end Overlap_Check2;
9845 end if;
9847 -- The following circuit deals with warning on record holes (gaps). We
9848 -- skip this check if overlap was detected, since it makes sense for the
9849 -- programmer to fix this illegality before worrying about warnings.
9851 if not Overlap_Detected and Warn_On_Record_Holes then
9852 Record_Hole_Check : declare
9853 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9854 -- Full declaration of record type
9856 procedure Check_Component_List
9857 (CL : Node_Id;
9858 Sbit : Uint;
9859 DS : List_Id);
9860 -- Check component list CL for holes. The starting bit should be
9861 -- Sbit. which is zero for the main record component list and set
9862 -- appropriately for recursive calls for variants. DS is set to
9863 -- a list of discriminant specifications to be included in the
9864 -- consideration of components. It is No_List if none to consider.
9866 --------------------------
9867 -- Check_Component_List --
9868 --------------------------
9870 procedure Check_Component_List
9871 (CL : Node_Id;
9872 Sbit : Uint;
9873 DS : List_Id)
9875 Compl : Integer;
9877 begin
9878 Compl := Integer (List_Length (Component_Items (CL)));
9880 if DS /= No_List then
9881 Compl := Compl + Integer (List_Length (DS));
9882 end if;
9884 declare
9885 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9886 -- Gather components (zero entry is for sort routine)
9888 Ncomps : Natural := 0;
9889 -- Number of entries stored in Comps (starting at Comps (1))
9891 Citem : Node_Id;
9892 -- One component item or discriminant specification
9894 Nbit : Uint;
9895 -- Starting bit for next component
9897 CEnt : Entity_Id;
9898 -- Component entity
9900 Variant : Node_Id;
9901 -- One variant
9903 function Lt (Op1, Op2 : Natural) return Boolean;
9904 -- Compare routine for Sort
9906 procedure Move (From : Natural; To : Natural);
9907 -- Move routine for Sort
9909 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9911 --------
9912 -- Lt --
9913 --------
9915 function Lt (Op1, Op2 : Natural) return Boolean is
9916 begin
9917 return Component_Bit_Offset (Comps (Op1))
9919 Component_Bit_Offset (Comps (Op2));
9920 end Lt;
9922 ----------
9923 -- Move --
9924 ----------
9926 procedure Move (From : Natural; To : Natural) is
9927 begin
9928 Comps (To) := Comps (From);
9929 end Move;
9931 begin
9932 -- Gather discriminants into Comp
9934 if DS /= No_List then
9935 Citem := First (DS);
9936 while Present (Citem) loop
9937 if Nkind (Citem) = N_Discriminant_Specification then
9938 declare
9939 Ent : constant Entity_Id :=
9940 Defining_Identifier (Citem);
9941 begin
9942 if Ekind (Ent) = E_Discriminant then
9943 Ncomps := Ncomps + 1;
9944 Comps (Ncomps) := Ent;
9945 end if;
9946 end;
9947 end if;
9949 Next (Citem);
9950 end loop;
9951 end if;
9953 -- Gather component entities into Comp
9955 Citem := First (Component_Items (CL));
9956 while Present (Citem) loop
9957 if Nkind (Citem) = N_Component_Declaration then
9958 Ncomps := Ncomps + 1;
9959 Comps (Ncomps) := Defining_Identifier (Citem);
9960 end if;
9962 Next (Citem);
9963 end loop;
9965 -- Now sort the component entities based on the first bit.
9966 -- Note we already know there are no overlapping components.
9968 Sorting.Sort (Ncomps);
9970 -- Loop through entries checking for holes
9972 Nbit := Sbit;
9973 for J in 1 .. Ncomps loop
9974 CEnt := Comps (J);
9975 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9977 if Error_Msg_Uint_1 > 0 then
9978 Error_Msg_NE
9979 ("?H?^-bit gap before component&",
9980 Component_Name (Component_Clause (CEnt)), CEnt);
9981 end if;
9983 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9984 end loop;
9986 -- Process variant parts recursively if present
9988 if Present (Variant_Part (CL)) then
9989 Variant := First (Variants (Variant_Part (CL)));
9990 while Present (Variant) loop
9991 Check_Component_List
9992 (Component_List (Variant), Nbit, No_List);
9993 Next (Variant);
9994 end loop;
9995 end if;
9996 end;
9997 end Check_Component_List;
9999 -- Start of processing for Record_Hole_Check
10001 begin
10002 declare
10003 Sbit : Uint;
10005 begin
10006 if Is_Tagged_Type (Rectype) then
10007 Sbit := UI_From_Int (System_Address_Size);
10008 else
10009 Sbit := Uint_0;
10010 end if;
10012 if Nkind (Decl) = N_Full_Type_Declaration
10013 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10014 then
10015 Check_Component_List
10016 (Component_List (Type_Definition (Decl)),
10017 Sbit,
10018 Discriminant_Specifications (Decl));
10019 end if;
10020 end;
10021 end Record_Hole_Check;
10022 end if;
10024 -- For records that have component clauses for all components, and whose
10025 -- size is less than or equal to 32, we need to know the size in the
10026 -- front end to activate possible packed array processing where the
10027 -- component type is a record.
10029 -- At this stage Hbit + 1 represents the first unused bit from all the
10030 -- component clauses processed, so if the component clauses are
10031 -- complete, then this is the length of the record.
10033 -- For records longer than System.Storage_Unit, and for those where not
10034 -- all components have component clauses, the back end determines the
10035 -- length (it may for example be appropriate to round up the size
10036 -- to some convenient boundary, based on alignment considerations, etc).
10038 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10040 -- Nothing to do if at least one component has no component clause
10042 Comp := First_Component_Or_Discriminant (Rectype);
10043 while Present (Comp) loop
10044 exit when No (Component_Clause (Comp));
10045 Next_Component_Or_Discriminant (Comp);
10046 end loop;
10048 -- If we fall out of loop, all components have component clauses
10049 -- and so we can set the size to the maximum value.
10051 if No (Comp) then
10052 Set_RM_Size (Rectype, Hbit + 1);
10053 end if;
10054 end if;
10055 end Check_Record_Representation_Clause;
10057 ----------------
10058 -- Check_Size --
10059 ----------------
10061 procedure Check_Size
10062 (N : Node_Id;
10063 T : Entity_Id;
10064 Siz : Uint;
10065 Biased : out Boolean)
10067 UT : constant Entity_Id := Underlying_Type (T);
10068 M : Uint;
10070 begin
10071 Biased := False;
10073 -- Reject patently improper size values.
10075 if Is_Elementary_Type (T)
10076 and then Siz > UI_From_Int (Int'Last)
10077 then
10078 Error_Msg_N ("Size value too large for elementary type", N);
10080 if Nkind (Original_Node (N)) = N_Op_Expon then
10081 Error_Msg_N
10082 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10083 end if;
10084 end if;
10086 -- Dismiss generic types
10088 if Is_Generic_Type (T)
10089 or else
10090 Is_Generic_Type (UT)
10091 or else
10092 Is_Generic_Type (Root_Type (UT))
10093 then
10094 return;
10096 -- Guard against previous errors
10098 elsif No (UT) or else UT = Any_Type then
10099 Check_Error_Detected;
10100 return;
10102 -- Check case of bit packed array
10104 elsif Is_Array_Type (UT)
10105 and then Known_Static_Component_Size (UT)
10106 and then Is_Bit_Packed_Array (UT)
10107 then
10108 declare
10109 Asiz : Uint;
10110 Indx : Node_Id;
10111 Ityp : Entity_Id;
10113 begin
10114 Asiz := Component_Size (UT);
10115 Indx := First_Index (UT);
10116 loop
10117 Ityp := Etype (Indx);
10119 -- If non-static bound, then we are not in the business of
10120 -- trying to check the length, and indeed an error will be
10121 -- issued elsewhere, since sizes of non-static array types
10122 -- cannot be set implicitly or explicitly.
10124 if not Is_OK_Static_Subtype (Ityp) then
10125 return;
10126 end if;
10128 -- Otherwise accumulate next dimension
10130 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10131 Expr_Value (Type_Low_Bound (Ityp)) +
10132 Uint_1);
10134 Next_Index (Indx);
10135 exit when No (Indx);
10136 end loop;
10138 if Asiz <= Siz then
10139 return;
10141 else
10142 Error_Msg_Uint_1 := Asiz;
10143 Error_Msg_NE
10144 ("size for& too small, minimum allowed is ^", N, T);
10145 Set_Esize (T, Asiz);
10146 Set_RM_Size (T, Asiz);
10147 end if;
10148 end;
10150 -- All other composite types are ignored
10152 elsif Is_Composite_Type (UT) then
10153 return;
10155 -- For fixed-point types, don't check minimum if type is not frozen,
10156 -- since we don't know all the characteristics of the type that can
10157 -- affect the size (e.g. a specified small) till freeze time.
10159 elsif Is_Fixed_Point_Type (UT)
10160 and then not Is_Frozen (UT)
10161 then
10162 null;
10164 -- Cases for which a minimum check is required
10166 else
10167 -- Ignore if specified size is correct for the type
10169 if Known_Esize (UT) and then Siz = Esize (UT) then
10170 return;
10171 end if;
10173 -- Otherwise get minimum size
10175 M := UI_From_Int (Minimum_Size (UT));
10177 if Siz < M then
10179 -- Size is less than minimum size, but one possibility remains
10180 -- that we can manage with the new size if we bias the type.
10182 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10184 if Siz < M then
10185 Error_Msg_Uint_1 := M;
10186 Error_Msg_NE
10187 ("size for& too small, minimum allowed is ^", N, T);
10188 Set_Esize (T, M);
10189 Set_RM_Size (T, M);
10190 else
10191 Biased := True;
10192 end if;
10193 end if;
10194 end if;
10195 end Check_Size;
10197 --------------------------
10198 -- Freeze_Entity_Checks --
10199 --------------------------
10201 procedure Freeze_Entity_Checks (N : Node_Id) is
10202 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10203 -- Inspect the primitive operations of type Typ and hide all pairs of
10204 -- implicitly declared non-overridden non-fully conformant homographs
10205 -- (Ada RM 8.3 12.3/2).
10207 -------------------------------------
10208 -- Hide_Non_Overridden_Subprograms --
10209 -------------------------------------
10211 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10212 procedure Hide_Matching_Homographs
10213 (Subp_Id : Entity_Id;
10214 Start_Elmt : Elmt_Id);
10215 -- Inspect a list of primitive operations starting with Start_Elmt
10216 -- and find matching implicitly declared non-overridden non-fully
10217 -- conformant homographs of Subp_Id. If found, all matches along
10218 -- with Subp_Id are hidden from all visibility.
10220 function Is_Non_Overridden_Or_Null_Procedure
10221 (Subp_Id : Entity_Id) return Boolean;
10222 -- Determine whether subprogram Subp_Id is implicitly declared non-
10223 -- overridden subprogram or an implicitly declared null procedure.
10225 ------------------------------
10226 -- Hide_Matching_Homographs --
10227 ------------------------------
10229 procedure Hide_Matching_Homographs
10230 (Subp_Id : Entity_Id;
10231 Start_Elmt : Elmt_Id)
10233 Prim : Entity_Id;
10234 Prim_Elmt : Elmt_Id;
10236 begin
10237 Prim_Elmt := Start_Elmt;
10238 while Present (Prim_Elmt) loop
10239 Prim := Node (Prim_Elmt);
10241 -- The current primitive is implicitly declared non-overridden
10242 -- non-fully conformant homograph of Subp_Id. Both subprograms
10243 -- must be hidden from visibility.
10245 if Chars (Prim) = Chars (Subp_Id)
10246 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10247 and then not Fully_Conformant (Prim, Subp_Id)
10248 then
10249 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10250 Set_Is_Immediately_Visible (Prim, False);
10251 Set_Is_Potentially_Use_Visible (Prim, False);
10253 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10254 Set_Is_Immediately_Visible (Subp_Id, False);
10255 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10256 end if;
10258 Next_Elmt (Prim_Elmt);
10259 end loop;
10260 end Hide_Matching_Homographs;
10262 -----------------------------------------
10263 -- Is_Non_Overridden_Or_Null_Procedure --
10264 -----------------------------------------
10266 function Is_Non_Overridden_Or_Null_Procedure
10267 (Subp_Id : Entity_Id) return Boolean
10269 Alias_Id : Entity_Id;
10271 begin
10272 -- The subprogram is inherited (implicitly declared), it does not
10273 -- override and does not cover a primitive of an interface.
10275 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10276 and then Present (Alias (Subp_Id))
10277 and then No (Interface_Alias (Subp_Id))
10278 and then No (Overridden_Operation (Subp_Id))
10279 then
10280 Alias_Id := Alias (Subp_Id);
10282 if Requires_Overriding (Alias_Id) then
10283 return True;
10285 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10286 and then Null_Present (Parent (Alias_Id))
10287 then
10288 return True;
10289 end if;
10290 end if;
10292 return False;
10293 end Is_Non_Overridden_Or_Null_Procedure;
10295 -- Local variables
10297 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10298 Prim : Entity_Id;
10299 Prim_Elmt : Elmt_Id;
10301 -- Start of processing for Hide_Non_Overridden_Subprograms
10303 begin
10304 -- Inspect the list of primitives looking for non-overridden
10305 -- subprograms.
10307 if Present (Prim_Ops) then
10308 Prim_Elmt := First_Elmt (Prim_Ops);
10309 while Present (Prim_Elmt) loop
10310 Prim := Node (Prim_Elmt);
10311 Next_Elmt (Prim_Elmt);
10313 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10314 Hide_Matching_Homographs
10315 (Subp_Id => Prim,
10316 Start_Elmt => Prim_Elmt);
10317 end if;
10318 end loop;
10319 end if;
10320 end Hide_Non_Overridden_Subprograms;
10322 ---------------------
10323 -- Local variables --
10324 ---------------------
10326 E : constant Entity_Id := Entity (N);
10328 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10329 -- True in non-generic case. Some of the processing here is skipped
10330 -- for the generic case since it is not needed. Basically in the
10331 -- generic case, we only need to do stuff that might generate error
10332 -- messages or warnings.
10334 -- Start of processing for Freeze_Entity_Checks
10336 begin
10337 -- Remember that we are processing a freezing entity. Required to
10338 -- ensure correct decoration of internal entities associated with
10339 -- interfaces (see New_Overloaded_Entity).
10341 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10343 -- For tagged types covering interfaces add internal entities that link
10344 -- the primitives of the interfaces with the primitives that cover them.
10345 -- Note: These entities were originally generated only when generating
10346 -- code because their main purpose was to provide support to initialize
10347 -- the secondary dispatch tables. They are now generated also when
10348 -- compiling with no code generation to provide ASIS the relationship
10349 -- between interface primitives and tagged type primitives. They are
10350 -- also used to locate primitives covering interfaces when processing
10351 -- generics (see Derive_Subprograms).
10353 -- This is not needed in the generic case
10355 if Ada_Version >= Ada_2005
10356 and then Non_Generic_Case
10357 and then Ekind (E) = E_Record_Type
10358 and then Is_Tagged_Type (E)
10359 and then not Is_Interface (E)
10360 and then Has_Interfaces (E)
10361 then
10362 -- This would be a good common place to call the routine that checks
10363 -- overriding of interface primitives (and thus factorize calls to
10364 -- Check_Abstract_Overriding located at different contexts in the
10365 -- compiler). However, this is not possible because it causes
10366 -- spurious errors in case of late overriding.
10368 Add_Internal_Interface_Entities (E);
10369 end if;
10371 -- After all forms of overriding have been resolved, a tagged type may
10372 -- be left with a set of implicitly declared and possibly erroneous
10373 -- abstract subprograms, null procedures and subprograms that require
10374 -- overriding. If this set contains fully conformat homographs, then one
10375 -- is chosen arbitrarily (already done during resolution), otherwise all
10376 -- remaining non-fully conformant homographs are hidden from visibility
10377 -- (Ada RM 8.3 12.3/2).
10379 if Is_Tagged_Type (E) then
10380 Hide_Non_Overridden_Subprograms (E);
10381 end if;
10383 -- Check CPP types
10385 if Ekind (E) = E_Record_Type
10386 and then Is_CPP_Class (E)
10387 and then Is_Tagged_Type (E)
10388 and then Tagged_Type_Expansion
10389 then
10390 if CPP_Num_Prims (E) = 0 then
10392 -- If the CPP type has user defined components then it must import
10393 -- primitives from C++. This is required because if the C++ class
10394 -- has no primitives then the C++ compiler does not added the _tag
10395 -- component to the type.
10397 if First_Entity (E) /= Last_Entity (E) then
10398 Error_Msg_N
10399 ("'C'P'P type must import at least one primitive from C++??",
10401 end if;
10402 end if;
10404 -- Check that all its primitives are abstract or imported from C++.
10405 -- Check also availability of the C++ constructor.
10407 declare
10408 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10409 Elmt : Elmt_Id;
10410 Error_Reported : Boolean := False;
10411 Prim : Node_Id;
10413 begin
10414 Elmt := First_Elmt (Primitive_Operations (E));
10415 while Present (Elmt) loop
10416 Prim := Node (Elmt);
10418 if Comes_From_Source (Prim) then
10419 if Is_Abstract_Subprogram (Prim) then
10420 null;
10422 elsif not Is_Imported (Prim)
10423 or else Convention (Prim) /= Convention_CPP
10424 then
10425 Error_Msg_N
10426 ("primitives of 'C'P'P types must be imported from C++ "
10427 & "or abstract??", Prim);
10429 elsif not Has_Constructors
10430 and then not Error_Reported
10431 then
10432 Error_Msg_Name_1 := Chars (E);
10433 Error_Msg_N
10434 ("??'C'P'P constructor required for type %", Prim);
10435 Error_Reported := True;
10436 end if;
10437 end if;
10439 Next_Elmt (Elmt);
10440 end loop;
10441 end;
10442 end if;
10444 -- Check Ada derivation of CPP type
10446 if Expander_Active -- why? losing errors in -gnatc mode???
10447 and then Present (Etype (E)) -- defend against errors
10448 and then Tagged_Type_Expansion
10449 and then Ekind (E) = E_Record_Type
10450 and then Etype (E) /= E
10451 and then Is_CPP_Class (Etype (E))
10452 and then CPP_Num_Prims (Etype (E)) > 0
10453 and then not Is_CPP_Class (E)
10454 and then not Has_CPP_Constructors (Etype (E))
10455 then
10456 -- If the parent has C++ primitives but it has no constructor then
10457 -- check that all the primitives are overridden in this derivation;
10458 -- otherwise the constructor of the parent is needed to build the
10459 -- dispatch table.
10461 declare
10462 Elmt : Elmt_Id;
10463 Prim : Node_Id;
10465 begin
10466 Elmt := First_Elmt (Primitive_Operations (E));
10467 while Present (Elmt) loop
10468 Prim := Node (Elmt);
10470 if not Is_Abstract_Subprogram (Prim)
10471 and then No (Interface_Alias (Prim))
10472 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10473 then
10474 Error_Msg_Name_1 := Chars (Etype (E));
10475 Error_Msg_N
10476 ("'C'P'P constructor required for parent type %", E);
10477 exit;
10478 end if;
10480 Next_Elmt (Elmt);
10481 end loop;
10482 end;
10483 end if;
10485 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10487 -- If we have a type with predicates, build predicate function. This
10488 -- is not needed in the generic case, and is not needed within TSS
10489 -- subprograms and other predefined primitives.
10491 if Non_Generic_Case
10492 and then Is_Type (E)
10493 and then Has_Predicates (E)
10494 and then not Within_Internal_Subprogram
10495 then
10496 Build_Predicate_Functions (E, N);
10497 end if;
10499 -- If type has delayed aspects, this is where we do the preanalysis at
10500 -- the freeze point, as part of the consistent visibility check. Note
10501 -- that this must be done after calling Build_Predicate_Functions or
10502 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10503 -- the subtype name in the saved expression so that they will not cause
10504 -- trouble in the preanalysis.
10506 -- This is also not needed in the generic case
10508 if Non_Generic_Case
10509 and then Has_Delayed_Aspects (E)
10510 and then Scope (E) = Current_Scope
10511 then
10512 -- Retrieve the visibility to the discriminants in order to properly
10513 -- analyze the aspects.
10515 Push_Scope_And_Install_Discriminants (E);
10517 declare
10518 Ritem : Node_Id;
10520 begin
10521 -- Look for aspect specification entries for this entity
10523 Ritem := First_Rep_Item (E);
10524 while Present (Ritem) loop
10525 if Nkind (Ritem) = N_Aspect_Specification
10526 and then Entity (Ritem) = E
10527 and then Is_Delayed_Aspect (Ritem)
10528 then
10529 Check_Aspect_At_Freeze_Point (Ritem);
10530 end if;
10532 Next_Rep_Item (Ritem);
10533 end loop;
10534 end;
10536 Uninstall_Discriminants_And_Pop_Scope (E);
10537 end if;
10539 -- For a record type, deal with variant parts. This has to be delayed
10540 -- to this point, because of the issue of statically predicated
10541 -- subtypes, which we have to ensure are frozen before checking
10542 -- choices, since we need to have the static choice list set.
10544 if Is_Record_Type (E) then
10545 Check_Variant_Part : declare
10546 D : constant Node_Id := Declaration_Node (E);
10547 T : Node_Id;
10548 C : Node_Id;
10549 VP : Node_Id;
10551 Others_Present : Boolean;
10552 pragma Warnings (Off, Others_Present);
10553 -- Indicates others present, not used in this case
10555 procedure Non_Static_Choice_Error (Choice : Node_Id);
10556 -- Error routine invoked by the generic instantiation below when
10557 -- the variant part has a non static choice.
10559 procedure Process_Declarations (Variant : Node_Id);
10560 -- Processes declarations associated with a variant. We analyzed
10561 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10562 -- but we still need the recursive call to Check_Choices for any
10563 -- nested variant to get its choices properly processed. This is
10564 -- also where we expand out the choices if expansion is active.
10566 package Variant_Choices_Processing is new
10567 Generic_Check_Choices
10568 (Process_Empty_Choice => No_OP,
10569 Process_Non_Static_Choice => Non_Static_Choice_Error,
10570 Process_Associated_Node => Process_Declarations);
10571 use Variant_Choices_Processing;
10573 -----------------------------
10574 -- Non_Static_Choice_Error --
10575 -----------------------------
10577 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10578 begin
10579 Flag_Non_Static_Expr
10580 ("choice given in variant part is not static!", Choice);
10581 end Non_Static_Choice_Error;
10583 --------------------------
10584 -- Process_Declarations --
10585 --------------------------
10587 procedure Process_Declarations (Variant : Node_Id) is
10588 CL : constant Node_Id := Component_List (Variant);
10589 VP : Node_Id;
10591 begin
10592 -- Check for static predicate present in this variant
10594 if Has_SP_Choice (Variant) then
10596 -- Here we expand. You might expect to find this call in
10597 -- Expand_N_Variant_Part, but that is called when we first
10598 -- see the variant part, and we cannot do this expansion
10599 -- earlier than the freeze point, since for statically
10600 -- predicated subtypes, the predicate is not known till
10601 -- the freeze point.
10603 -- Furthermore, we do this expansion even if the expander
10604 -- is not active, because other semantic processing, e.g.
10605 -- for aggregates, requires the expanded list of choices.
10607 -- If the expander is not active, then we can't just clobber
10608 -- the list since it would invalidate the ASIS -gnatct tree.
10609 -- So we have to rewrite the variant part with a Rewrite
10610 -- call that replaces it with a copy and clobber the copy.
10612 if not Expander_Active then
10613 declare
10614 NewV : constant Node_Id := New_Copy (Variant);
10615 begin
10616 Set_Discrete_Choices
10617 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10618 Rewrite (Variant, NewV);
10619 end;
10620 end if;
10622 Expand_Static_Predicates_In_Choices (Variant);
10623 end if;
10625 -- We don't need to worry about the declarations in the variant
10626 -- (since they were analyzed by Analyze_Choices when we first
10627 -- encountered the variant), but we do need to take care of
10628 -- expansion of any nested variants.
10630 if not Null_Present (CL) then
10631 VP := Variant_Part (CL);
10633 if Present (VP) then
10634 Check_Choices
10635 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10636 end if;
10637 end if;
10638 end Process_Declarations;
10640 -- Start of processing for Check_Variant_Part
10642 begin
10643 -- Find component list
10645 C := Empty;
10647 if Nkind (D) = N_Full_Type_Declaration then
10648 T := Type_Definition (D);
10650 if Nkind (T) = N_Record_Definition then
10651 C := Component_List (T);
10653 elsif Nkind (T) = N_Derived_Type_Definition
10654 and then Present (Record_Extension_Part (T))
10655 then
10656 C := Component_List (Record_Extension_Part (T));
10657 end if;
10658 end if;
10660 -- Case of variant part present
10662 if Present (C) and then Present (Variant_Part (C)) then
10663 VP := Variant_Part (C);
10665 -- Check choices
10667 Check_Choices
10668 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10670 -- If the last variant does not contain the Others choice,
10671 -- replace it with an N_Others_Choice node since Gigi always
10672 -- wants an Others. Note that we do not bother to call Analyze
10673 -- on the modified variant part, since its only effect would be
10674 -- to compute the Others_Discrete_Choices node laboriously, and
10675 -- of course we already know the list of choices corresponding
10676 -- to the others choice (it's the list we're replacing).
10678 -- We only want to do this if the expander is active, since
10679 -- we do not want to clobber the ASIS tree.
10681 if Expander_Active then
10682 declare
10683 Last_Var : constant Node_Id :=
10684 Last_Non_Pragma (Variants (VP));
10686 Others_Node : Node_Id;
10688 begin
10689 if Nkind (First (Discrete_Choices (Last_Var))) /=
10690 N_Others_Choice
10691 then
10692 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10693 Set_Others_Discrete_Choices
10694 (Others_Node, Discrete_Choices (Last_Var));
10695 Set_Discrete_Choices
10696 (Last_Var, New_List (Others_Node));
10697 end if;
10698 end;
10699 end if;
10700 end if;
10701 end Check_Variant_Part;
10702 end if;
10703 end Freeze_Entity_Checks;
10705 -------------------------
10706 -- Get_Alignment_Value --
10707 -------------------------
10709 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10710 Align : constant Uint := Static_Integer (Expr);
10712 begin
10713 if Align = No_Uint then
10714 return No_Uint;
10716 elsif Align <= 0 then
10717 Error_Msg_N ("alignment value must be positive", Expr);
10718 return No_Uint;
10720 else
10721 for J in Int range 0 .. 64 loop
10722 declare
10723 M : constant Uint := Uint_2 ** J;
10725 begin
10726 exit when M = Align;
10728 if M > Align then
10729 Error_Msg_N
10730 ("alignment value must be power of 2", Expr);
10731 return No_Uint;
10732 end if;
10733 end;
10734 end loop;
10736 return Align;
10737 end if;
10738 end Get_Alignment_Value;
10740 -------------------------------------
10741 -- Inherit_Aspects_At_Freeze_Point --
10742 -------------------------------------
10744 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10745 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10746 (Rep_Item : Node_Id) return Boolean;
10747 -- This routine checks if Rep_Item is either a pragma or an aspect
10748 -- specification node whose correponding pragma (if any) is present in
10749 -- the Rep Item chain of the entity it has been specified to.
10751 --------------------------------------------------
10752 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10753 --------------------------------------------------
10755 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10756 (Rep_Item : Node_Id) return Boolean
10758 begin
10759 return
10760 Nkind (Rep_Item) = N_Pragma
10761 or else Present_In_Rep_Item
10762 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10763 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10765 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10767 begin
10768 -- A representation item is either subtype-specific (Size and Alignment
10769 -- clauses) or type-related (all others). Subtype-specific aspects may
10770 -- differ for different subtypes of the same type (RM 13.1.8).
10772 -- A derived type inherits each type-related representation aspect of
10773 -- its parent type that was directly specified before the declaration of
10774 -- the derived type (RM 13.1.15).
10776 -- A derived subtype inherits each subtype-specific representation
10777 -- aspect of its parent subtype that was directly specified before the
10778 -- declaration of the derived type (RM 13.1.15).
10780 -- The general processing involves inheriting a representation aspect
10781 -- from a parent type whenever the first rep item (aspect specification,
10782 -- attribute definition clause, pragma) corresponding to the given
10783 -- representation aspect in the rep item chain of Typ, if any, isn't
10784 -- directly specified to Typ but to one of its parents.
10786 -- ??? Note that, for now, just a limited number of representation
10787 -- aspects have been inherited here so far. Many of them are
10788 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
10789 -- a non- exhaustive list of aspects that likely also need to
10790 -- be moved to this routine: Alignment, Component_Alignment,
10791 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
10792 -- Preelaborable_Initialization, RM_Size and Small.
10794 -- In addition, Convention must be propagated from base type to subtype,
10795 -- because the subtype may have been declared on an incomplete view.
10797 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
10798 return;
10799 end if;
10801 -- Ada_05/Ada_2005
10803 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
10804 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
10805 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10806 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
10807 then
10808 Set_Is_Ada_2005_Only (Typ);
10809 end if;
10811 -- Ada_12/Ada_2012
10813 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
10814 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
10815 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10816 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
10817 then
10818 Set_Is_Ada_2012_Only (Typ);
10819 end if;
10821 -- Atomic/Shared
10823 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
10824 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
10825 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10826 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
10827 then
10828 Set_Is_Atomic (Typ);
10829 Set_Treat_As_Volatile (Typ);
10830 Set_Is_Volatile (Typ);
10831 end if;
10833 -- Convention
10835 if Is_Record_Type (Typ)
10836 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
10837 then
10838 Set_Convention (Typ, Convention (Base_Type (Typ)));
10839 end if;
10841 -- Default_Component_Value
10843 if Is_Array_Type (Typ)
10844 and then Is_Base_Type (Typ)
10845 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
10846 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
10847 then
10848 Set_Default_Aspect_Component_Value (Typ,
10849 Default_Aspect_Component_Value
10850 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
10851 end if;
10853 -- Default_Value
10855 if Is_Scalar_Type (Typ)
10856 and then Is_Base_Type (Typ)
10857 and then Has_Rep_Item (Typ, Name_Default_Value, False)
10858 and then Has_Rep_Item (Typ, Name_Default_Value)
10859 then
10860 Set_Default_Aspect_Value (Typ,
10861 Default_Aspect_Value
10862 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
10863 end if;
10865 -- Discard_Names
10867 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
10868 and then Has_Rep_Item (Typ, Name_Discard_Names)
10869 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10870 (Get_Rep_Item (Typ, Name_Discard_Names))
10871 then
10872 Set_Discard_Names (Typ);
10873 end if;
10875 -- Invariants
10877 if not Has_Rep_Item (Typ, Name_Invariant, False)
10878 and then Has_Rep_Item (Typ, Name_Invariant)
10879 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10880 (Get_Rep_Item (Typ, Name_Invariant))
10881 then
10882 Set_Has_Invariants (Typ);
10884 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
10885 Set_Has_Inheritable_Invariants (Typ);
10886 end if;
10888 -- If we have a subtype with invariants, whose base type does not have
10889 -- invariants, copy these invariants to the base type. This happens for
10890 -- the case of implicit base types created for scalar and array types.
10892 elsif Has_Invariants (Typ)
10893 and then not Has_Invariants (Base_Type (Typ))
10894 then
10895 Set_Has_Invariants (Base_Type (Typ));
10896 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
10897 end if;
10899 -- Volatile
10901 if not Has_Rep_Item (Typ, Name_Volatile, False)
10902 and then Has_Rep_Item (Typ, Name_Volatile)
10903 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10904 (Get_Rep_Item (Typ, Name_Volatile))
10905 then
10906 Set_Treat_As_Volatile (Typ);
10907 Set_Is_Volatile (Typ);
10908 end if;
10910 -- Inheritance for derived types only
10912 if Is_Derived_Type (Typ) then
10913 declare
10914 Bas_Typ : constant Entity_Id := Base_Type (Typ);
10915 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
10917 begin
10918 -- Atomic_Components
10920 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
10921 and then Has_Rep_Item (Typ, Name_Atomic_Components)
10922 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10923 (Get_Rep_Item (Typ, Name_Atomic_Components))
10924 then
10925 Set_Has_Atomic_Components (Imp_Bas_Typ);
10926 end if;
10928 -- Volatile_Components
10930 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
10931 and then Has_Rep_Item (Typ, Name_Volatile_Components)
10932 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10933 (Get_Rep_Item (Typ, Name_Volatile_Components))
10934 then
10935 Set_Has_Volatile_Components (Imp_Bas_Typ);
10936 end if;
10938 -- Finalize_Storage_Only
10940 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
10941 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
10942 then
10943 Set_Finalize_Storage_Only (Bas_Typ);
10944 end if;
10946 -- Universal_Aliasing
10948 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
10949 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
10950 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10951 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
10952 then
10953 Set_Universal_Aliasing (Imp_Bas_Typ);
10954 end if;
10956 -- Bit_Order
10958 if Is_Record_Type (Typ) then
10959 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
10960 and then Has_Rep_Item (Typ, Name_Bit_Order)
10961 then
10962 Set_Reverse_Bit_Order (Bas_Typ,
10963 Reverse_Bit_Order (Entity (Name
10964 (Get_Rep_Item (Typ, Name_Bit_Order)))));
10965 end if;
10966 end if;
10968 -- Scalar_Storage_Order
10970 -- Note: the aspect is specified on a first subtype, but recorded
10971 -- in a flag of the base type!
10973 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
10974 and then Typ = Bas_Typ
10975 then
10976 -- For a type extension, always inherit from parent; otherwise
10977 -- inherit if no default applies. Note: we do not check for
10978 -- an explicit rep item on the parent type when inheriting,
10979 -- because the parent SSO may itself have been set by default.
10981 if not Has_Rep_Item (First_Subtype (Typ),
10982 Name_Scalar_Storage_Order, False)
10983 and then (Is_Tagged_Type (Bas_Typ)
10984 or else not (SSO_Set_Low_By_Default (Bas_Typ)
10985 or else
10986 SSO_Set_High_By_Default (Bas_Typ)))
10987 then
10988 Set_Reverse_Storage_Order (Bas_Typ,
10989 Reverse_Storage_Order
10990 (Implementation_Base_Type (Etype (Bas_Typ))));
10992 -- Clear default SSO indications, since the inherited aspect
10993 -- which was set explicitly overrides the default.
10995 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
10996 Set_SSO_Set_High_By_Default (Bas_Typ, False);
10997 end if;
10998 end if;
10999 end;
11000 end if;
11001 end Inherit_Aspects_At_Freeze_Point;
11003 ----------------
11004 -- Initialize --
11005 ----------------
11007 procedure Initialize is
11008 begin
11009 Address_Clause_Checks.Init;
11010 Independence_Checks.Init;
11011 Unchecked_Conversions.Init;
11012 end Initialize;
11014 ---------------------------
11015 -- Install_Discriminants --
11016 ---------------------------
11018 procedure Install_Discriminants (E : Entity_Id) is
11019 Disc : Entity_Id;
11020 Prev : Entity_Id;
11021 begin
11022 Disc := First_Discriminant (E);
11023 while Present (Disc) loop
11024 Prev := Current_Entity (Disc);
11025 Set_Current_Entity (Disc);
11026 Set_Is_Immediately_Visible (Disc);
11027 Set_Homonym (Disc, Prev);
11028 Next_Discriminant (Disc);
11029 end loop;
11030 end Install_Discriminants;
11032 -------------------------
11033 -- Is_Operational_Item --
11034 -------------------------
11036 function Is_Operational_Item (N : Node_Id) return Boolean is
11037 begin
11038 if Nkind (N) /= N_Attribute_Definition_Clause then
11039 return False;
11041 else
11042 declare
11043 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11044 begin
11045 return Id = Attribute_Input
11046 or else Id = Attribute_Output
11047 or else Id = Attribute_Read
11048 or else Id = Attribute_Write
11049 or else Id = Attribute_External_Tag;
11050 end;
11051 end if;
11052 end Is_Operational_Item;
11054 -------------------------
11055 -- Is_Predicate_Static --
11056 -------------------------
11058 -- Note: the basic legality of the expression has already been checked, so
11059 -- we don't need to worry about cases or ranges on strings for example.
11061 function Is_Predicate_Static
11062 (Expr : Node_Id;
11063 Nam : Name_Id) return Boolean
11065 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11066 -- Given a list of case expression alternatives, returns True if all
11067 -- the alternatives are static (have all static choices, and a static
11068 -- expression).
11070 function All_Static_Choices (L : List_Id) return Boolean;
11071 -- Returns true if all elements of the list are OK static choices
11072 -- as defined below for Is_Static_Choice. Used for case expression
11073 -- alternatives and for the right operand of a membership test. An
11074 -- others_choice is static if the corresponding expression is static.
11075 -- The staticness of the bounds is checked separately.
11077 function Is_Static_Choice (N : Node_Id) return Boolean;
11078 -- Returns True if N represents a static choice (static subtype, or
11079 -- static subtype indication, or static expression, or static range).
11081 -- Note that this is a bit more inclusive than we actually need
11082 -- (in particular membership tests do not allow the use of subtype
11083 -- indications). But that doesn't matter, we have already checked
11084 -- that the construct is legal to get this far.
11086 function Is_Type_Ref (N : Node_Id) return Boolean;
11087 pragma Inline (Is_Type_Ref);
11088 -- Returns True if N is a reference to the type for the predicate in the
11089 -- expression (i.e. if it is an identifier whose Chars field matches the
11090 -- Nam given in the call). N must not be parenthesized, if the type name
11091 -- appears in parens, this routine will return False.
11093 ----------------------------------
11094 -- All_Static_Case_Alternatives --
11095 ----------------------------------
11097 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11098 N : Node_Id;
11100 begin
11101 N := First (L);
11102 while Present (N) loop
11103 if not (All_Static_Choices (Discrete_Choices (N))
11104 and then Is_OK_Static_Expression (Expression (N)))
11105 then
11106 return False;
11107 end if;
11109 Next (N);
11110 end loop;
11112 return True;
11113 end All_Static_Case_Alternatives;
11115 ------------------------
11116 -- All_Static_Choices --
11117 ------------------------
11119 function All_Static_Choices (L : List_Id) return Boolean is
11120 N : Node_Id;
11122 begin
11123 N := First (L);
11124 while Present (N) loop
11125 if not Is_Static_Choice (N) then
11126 return False;
11127 end if;
11129 Next (N);
11130 end loop;
11132 return True;
11133 end All_Static_Choices;
11135 ----------------------
11136 -- Is_Static_Choice --
11137 ----------------------
11139 function Is_Static_Choice (N : Node_Id) return Boolean is
11140 begin
11141 return Nkind (N) = N_Others_Choice
11142 or else Is_OK_Static_Expression (N)
11143 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11144 and then Is_OK_Static_Subtype (Entity (N)))
11145 or else (Nkind (N) = N_Subtype_Indication
11146 and then Is_OK_Static_Subtype (Entity (N)))
11147 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11148 end Is_Static_Choice;
11150 -----------------
11151 -- Is_Type_Ref --
11152 -----------------
11154 function Is_Type_Ref (N : Node_Id) return Boolean is
11155 begin
11156 return Nkind (N) = N_Identifier
11157 and then Chars (N) = Nam
11158 and then Paren_Count (N) = 0;
11159 end Is_Type_Ref;
11161 -- Start of processing for Is_Predicate_Static
11163 begin
11164 -- Predicate_Static means one of the following holds. Numbers are the
11165 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11167 -- 16: A static expression
11169 if Is_OK_Static_Expression (Expr) then
11170 return True;
11172 -- 17: A membership test whose simple_expression is the current
11173 -- instance, and whose membership_choice_list meets the requirements
11174 -- for a static membership test.
11176 elsif Nkind (Expr) in N_Membership_Test
11177 and then ((Present (Right_Opnd (Expr))
11178 and then Is_Static_Choice (Right_Opnd (Expr)))
11179 or else
11180 (Present (Alternatives (Expr))
11181 and then All_Static_Choices (Alternatives (Expr))))
11182 then
11183 return True;
11185 -- 18. A case_expression whose selecting_expression is the current
11186 -- instance, and whose dependent expressions are static expressions.
11188 elsif Nkind (Expr) = N_Case_Expression
11189 and then Is_Type_Ref (Expression (Expr))
11190 and then All_Static_Case_Alternatives (Alternatives (Expr))
11191 then
11192 return True;
11194 -- 19. A call to a predefined equality or ordering operator, where one
11195 -- operand is the current instance, and the other is a static
11196 -- expression.
11198 -- Note: the RM is clearly wrong here in not excluding string types.
11199 -- Without this exclusion, we would allow expressions like X > "ABC"
11200 -- to be considered as predicate-static, which is clearly not intended,
11201 -- since the idea is for predicate-static to be a subset of normal
11202 -- static expressions (and "DEF" > "ABC" is not a static expression).
11204 -- However, we do allow internally generated (not from source) equality
11205 -- and inequality operations to be valid on strings (this helps deal
11206 -- with cases where we transform A in "ABC" to A = "ABC).
11208 elsif Nkind (Expr) in N_Op_Compare
11209 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11210 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11211 and then not Comes_From_Source (Expr)))
11212 and then ((Is_Type_Ref (Left_Opnd (Expr))
11213 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11214 or else
11215 (Is_Type_Ref (Right_Opnd (Expr))
11216 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11217 then
11218 return True;
11220 -- 20. A call to a predefined boolean logical operator, where each
11221 -- operand is predicate-static.
11223 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11224 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11225 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11226 or else
11227 (Nkind (Expr) = N_Op_Not
11228 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11229 then
11230 return True;
11232 -- 21. A short-circuit control form where both operands are
11233 -- predicate-static.
11235 elsif Nkind (Expr) in N_Short_Circuit
11236 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11237 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11238 then
11239 return True;
11241 -- 22. A parenthesized predicate-static expression. This does not
11242 -- require any special test, since we just ignore paren levels in
11243 -- all the cases above.
11245 -- One more test that is an implementation artifact caused by the fact
11246 -- that we are analyzing not the original expression, but the generated
11247 -- expression in the body of the predicate function. This can include
11248 -- references to inherited predicates, so that the expression we are
11249 -- processing looks like:
11251 -- expression and then xxPredicate (typ (Inns))
11253 -- Where the call is to a Predicate function for an inherited predicate.
11254 -- We simply ignore such a call (which could be to either a dynamic or
11255 -- a static predicate, but remember that we can have a Static_Predicate
11256 -- for a non-static subtype).
11258 elsif Nkind (Expr) = N_Function_Call
11259 and then Is_Predicate_Function (Entity (Name (Expr)))
11260 then
11261 return True;
11263 -- That's an exhaustive list of tests, all other cases are not
11264 -- predicate-static, so we return False.
11266 else
11267 return False;
11268 end if;
11269 end Is_Predicate_Static;
11271 ---------------------
11272 -- Kill_Rep_Clause --
11273 ---------------------
11275 procedure Kill_Rep_Clause (N : Node_Id) is
11276 begin
11277 pragma Assert (Ignore_Rep_Clauses);
11279 -- Note: we use Replace rather than Rewrite, because we don't want
11280 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11281 -- rep clause that is being replaced.
11283 Replace (N, Make_Null_Statement (Sloc (N)));
11285 -- The null statement must be marked as not coming from source. This is
11286 -- so that ASIS ignores it, and also the back end does not expect bogus
11287 -- "from source" null statements in weird places (e.g. in declarative
11288 -- regions where such null statements are not allowed).
11290 Set_Comes_From_Source (N, False);
11291 end Kill_Rep_Clause;
11293 ------------------
11294 -- Minimum_Size --
11295 ------------------
11297 function Minimum_Size
11298 (T : Entity_Id;
11299 Biased : Boolean := False) return Nat
11301 Lo : Uint := No_Uint;
11302 Hi : Uint := No_Uint;
11303 LoR : Ureal := No_Ureal;
11304 HiR : Ureal := No_Ureal;
11305 LoSet : Boolean := False;
11306 HiSet : Boolean := False;
11307 B : Uint;
11308 S : Nat;
11309 Ancest : Entity_Id;
11310 R_Typ : constant Entity_Id := Root_Type (T);
11312 begin
11313 -- If bad type, return 0
11315 if T = Any_Type then
11316 return 0;
11318 -- For generic types, just return zero. There cannot be any legitimate
11319 -- need to know such a size, but this routine may be called with a
11320 -- generic type as part of normal processing.
11322 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11323 return 0;
11325 -- Access types (cannot have size smaller than System.Address)
11327 elsif Is_Access_Type (T) then
11328 return System_Address_Size;
11330 -- Floating-point types
11332 elsif Is_Floating_Point_Type (T) then
11333 return UI_To_Int (Esize (R_Typ));
11335 -- Discrete types
11337 elsif Is_Discrete_Type (T) then
11339 -- The following loop is looking for the nearest compile time known
11340 -- bounds following the ancestor subtype chain. The idea is to find
11341 -- the most restrictive known bounds information.
11343 Ancest := T;
11344 loop
11345 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11346 return 0;
11347 end if;
11349 if not LoSet then
11350 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11351 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11352 LoSet := True;
11353 exit when HiSet;
11354 end if;
11355 end if;
11357 if not HiSet then
11358 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11359 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11360 HiSet := True;
11361 exit when LoSet;
11362 end if;
11363 end if;
11365 Ancest := Ancestor_Subtype (Ancest);
11367 if No (Ancest) then
11368 Ancest := Base_Type (T);
11370 if Is_Generic_Type (Ancest) then
11371 return 0;
11372 end if;
11373 end if;
11374 end loop;
11376 -- Fixed-point types. We can't simply use Expr_Value to get the
11377 -- Corresponding_Integer_Value values of the bounds, since these do not
11378 -- get set till the type is frozen, and this routine can be called
11379 -- before the type is frozen. Similarly the test for bounds being static
11380 -- needs to include the case where we have unanalyzed real literals for
11381 -- the same reason.
11383 elsif Is_Fixed_Point_Type (T) then
11385 -- The following loop is looking for the nearest compile time known
11386 -- bounds following the ancestor subtype chain. The idea is to find
11387 -- the most restrictive known bounds information.
11389 Ancest := T;
11390 loop
11391 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11392 return 0;
11393 end if;
11395 -- Note: In the following two tests for LoSet and HiSet, it may
11396 -- seem redundant to test for N_Real_Literal here since normally
11397 -- one would assume that the test for the value being known at
11398 -- compile time includes this case. However, there is a glitch.
11399 -- If the real literal comes from folding a non-static expression,
11400 -- then we don't consider any non- static expression to be known
11401 -- at compile time if we are in configurable run time mode (needed
11402 -- in some cases to give a clearer definition of what is and what
11403 -- is not accepted). So the test is indeed needed. Without it, we
11404 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11406 if not LoSet then
11407 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11408 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11409 then
11410 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11411 LoSet := True;
11412 exit when HiSet;
11413 end if;
11414 end if;
11416 if not HiSet then
11417 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11418 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11419 then
11420 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11421 HiSet := True;
11422 exit when LoSet;
11423 end if;
11424 end if;
11426 Ancest := Ancestor_Subtype (Ancest);
11428 if No (Ancest) then
11429 Ancest := Base_Type (T);
11431 if Is_Generic_Type (Ancest) then
11432 return 0;
11433 end if;
11434 end if;
11435 end loop;
11437 Lo := UR_To_Uint (LoR / Small_Value (T));
11438 Hi := UR_To_Uint (HiR / Small_Value (T));
11440 -- No other types allowed
11442 else
11443 raise Program_Error;
11444 end if;
11446 -- Fall through with Hi and Lo set. Deal with biased case
11448 if (Biased
11449 and then not Is_Fixed_Point_Type (T)
11450 and then not (Is_Enumeration_Type (T)
11451 and then Has_Non_Standard_Rep (T)))
11452 or else Has_Biased_Representation (T)
11453 then
11454 Hi := Hi - Lo;
11455 Lo := Uint_0;
11456 end if;
11458 -- Signed case. Note that we consider types like range 1 .. -1 to be
11459 -- signed for the purpose of computing the size, since the bounds have
11460 -- to be accommodated in the base type.
11462 if Lo < 0 or else Hi < 0 then
11463 S := 1;
11464 B := Uint_1;
11466 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11467 -- Note that we accommodate the case where the bounds cross. This
11468 -- can happen either because of the way the bounds are declared
11469 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11471 while Lo < -B
11472 or else Hi < -B
11473 or else Lo >= B
11474 or else Hi >= B
11475 loop
11476 B := Uint_2 ** S;
11477 S := S + 1;
11478 end loop;
11480 -- Unsigned case
11482 else
11483 -- If both bounds are positive, make sure that both are represen-
11484 -- table in the case where the bounds are crossed. This can happen
11485 -- either because of the way the bounds are declared, or because of
11486 -- the algorithm in Freeze_Fixed_Point_Type.
11488 if Lo > Hi then
11489 Hi := Lo;
11490 end if;
11492 -- S = size, (can accommodate 0 .. (2**size - 1))
11494 S := 0;
11495 while Hi >= Uint_2 ** S loop
11496 S := S + 1;
11497 end loop;
11498 end if;
11500 return S;
11501 end Minimum_Size;
11503 ---------------------------
11504 -- New_Stream_Subprogram --
11505 ---------------------------
11507 procedure New_Stream_Subprogram
11508 (N : Node_Id;
11509 Ent : Entity_Id;
11510 Subp : Entity_Id;
11511 Nam : TSS_Name_Type)
11513 Loc : constant Source_Ptr := Sloc (N);
11514 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11515 Subp_Id : Entity_Id;
11516 Subp_Decl : Node_Id;
11517 F : Entity_Id;
11518 Etyp : Entity_Id;
11520 Defer_Declaration : constant Boolean :=
11521 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11522 -- For a tagged type, there is a declaration for each stream attribute
11523 -- at the freeze point, and we must generate only a completion of this
11524 -- declaration. We do the same for private types, because the full view
11525 -- might be tagged. Otherwise we generate a declaration at the point of
11526 -- the attribute definition clause.
11528 function Build_Spec return Node_Id;
11529 -- Used for declaration and renaming declaration, so that this is
11530 -- treated as a renaming_as_body.
11532 ----------------
11533 -- Build_Spec --
11534 ----------------
11536 function Build_Spec return Node_Id is
11537 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11538 Formals : List_Id;
11539 Spec : Node_Id;
11540 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11542 begin
11543 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11545 -- S : access Root_Stream_Type'Class
11547 Formals := New_List (
11548 Make_Parameter_Specification (Loc,
11549 Defining_Identifier =>
11550 Make_Defining_Identifier (Loc, Name_S),
11551 Parameter_Type =>
11552 Make_Access_Definition (Loc,
11553 Subtype_Mark =>
11554 New_Occurrence_Of (
11555 Designated_Type (Etype (F)), Loc))));
11557 if Nam = TSS_Stream_Input then
11558 Spec :=
11559 Make_Function_Specification (Loc,
11560 Defining_Unit_Name => Subp_Id,
11561 Parameter_Specifications => Formals,
11562 Result_Definition => T_Ref);
11563 else
11564 -- V : [out] T
11566 Append_To (Formals,
11567 Make_Parameter_Specification (Loc,
11568 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11569 Out_Present => Out_P,
11570 Parameter_Type => T_Ref));
11572 Spec :=
11573 Make_Procedure_Specification (Loc,
11574 Defining_Unit_Name => Subp_Id,
11575 Parameter_Specifications => Formals);
11576 end if;
11578 return Spec;
11579 end Build_Spec;
11581 -- Start of processing for New_Stream_Subprogram
11583 begin
11584 F := First_Formal (Subp);
11586 if Ekind (Subp) = E_Procedure then
11587 Etyp := Etype (Next_Formal (F));
11588 else
11589 Etyp := Etype (Subp);
11590 end if;
11592 -- Prepare subprogram declaration and insert it as an action on the
11593 -- clause node. The visibility for this entity is used to test for
11594 -- visibility of the attribute definition clause (in the sense of
11595 -- 8.3(23) as amended by AI-195).
11597 if not Defer_Declaration then
11598 Subp_Decl :=
11599 Make_Subprogram_Declaration (Loc,
11600 Specification => Build_Spec);
11602 -- For a tagged type, there is always a visible declaration for each
11603 -- stream TSS (it is a predefined primitive operation), and the
11604 -- completion of this declaration occurs at the freeze point, which is
11605 -- not always visible at places where the attribute definition clause is
11606 -- visible. So, we create a dummy entity here for the purpose of
11607 -- tracking the visibility of the attribute definition clause itself.
11609 else
11610 Subp_Id :=
11611 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11612 Subp_Decl :=
11613 Make_Object_Declaration (Loc,
11614 Defining_Identifier => Subp_Id,
11615 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11616 end if;
11618 Insert_Action (N, Subp_Decl);
11619 Set_Entity (N, Subp_Id);
11621 Subp_Decl :=
11622 Make_Subprogram_Renaming_Declaration (Loc,
11623 Specification => Build_Spec,
11624 Name => New_Occurrence_Of (Subp, Loc));
11626 if Defer_Declaration then
11627 Set_TSS (Base_Type (Ent), Subp_Id);
11628 else
11629 Insert_Action (N, Subp_Decl);
11630 Copy_TSS (Subp_Id, Base_Type (Ent));
11631 end if;
11632 end New_Stream_Subprogram;
11634 ------------------------------------------
11635 -- Push_Scope_And_Install_Discriminants --
11636 ------------------------------------------
11638 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11639 begin
11640 if Has_Discriminants (E) then
11641 Push_Scope (E);
11643 -- Make discriminants visible for type declarations and protected
11644 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11646 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11647 Install_Discriminants (E);
11648 end if;
11649 end if;
11650 end Push_Scope_And_Install_Discriminants;
11652 ------------------------
11653 -- Rep_Item_Too_Early --
11654 ------------------------
11656 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
11657 begin
11658 -- Cannot apply non-operational rep items to generic types
11660 if Is_Operational_Item (N) then
11661 return False;
11663 elsif Is_Type (T)
11664 and then Is_Generic_Type (Root_Type (T))
11665 then
11666 Error_Msg_N ("representation item not allowed for generic type", N);
11667 return True;
11668 end if;
11670 -- Otherwise check for incomplete type
11672 if Is_Incomplete_Or_Private_Type (T)
11673 and then No (Underlying_Type (T))
11674 and then
11675 (Nkind (N) /= N_Pragma
11676 or else Get_Pragma_Id (N) /= Pragma_Import)
11677 then
11678 Error_Msg_N
11679 ("representation item must be after full type declaration", N);
11680 return True;
11682 -- If the type has incomplete components, a representation clause is
11683 -- illegal but stream attributes and Convention pragmas are correct.
11685 elsif Has_Private_Component (T) then
11686 if Nkind (N) = N_Pragma then
11687 return False;
11689 else
11690 Error_Msg_N
11691 ("representation item must appear after type is fully defined",
11693 return True;
11694 end if;
11695 else
11696 return False;
11697 end if;
11698 end Rep_Item_Too_Early;
11700 -----------------------
11701 -- Rep_Item_Too_Late --
11702 -----------------------
11704 function Rep_Item_Too_Late
11705 (T : Entity_Id;
11706 N : Node_Id;
11707 FOnly : Boolean := False) return Boolean
11709 S : Entity_Id;
11710 Parent_Type : Entity_Id;
11712 procedure No_Type_Rep_Item;
11713 -- Output message indicating that no type-related aspects can be
11714 -- specified due to some property of the parent type.
11716 procedure Too_Late;
11717 -- Output message for an aspect being specified too late
11719 -- Note that neither of the above errors is considered a serious one,
11720 -- since the effect is simply that we ignore the representation clause
11721 -- in these cases.
11722 -- Is this really true? In any case if we make this change we must
11723 -- document the requirement in the spec of Rep_Item_Too_Late that
11724 -- if True is returned, then the rep item must be completely ignored???
11726 ----------------------
11727 -- No_Type_Rep_Item --
11728 ----------------------
11730 procedure No_Type_Rep_Item is
11731 begin
11732 Error_Msg_N ("|type-related representation item not permitted!", N);
11733 end No_Type_Rep_Item;
11735 --------------
11736 -- Too_Late --
11737 --------------
11739 procedure Too_Late is
11740 begin
11741 -- Other compilers seem more relaxed about rep items appearing too
11742 -- late. Since analysis tools typically don't care about rep items
11743 -- anyway, no reason to be too strict about this.
11745 if not Relaxed_RM_Semantics then
11746 Error_Msg_N ("|representation item appears too late!", N);
11747 end if;
11748 end Too_Late;
11750 -- Start of processing for Rep_Item_Too_Late
11752 begin
11753 -- First make sure entity is not frozen (RM 13.1(9))
11755 if Is_Frozen (T)
11757 -- Exclude imported types, which may be frozen if they appear in a
11758 -- representation clause for a local type.
11760 and then not From_Limited_With (T)
11762 -- Exclude generated entities (not coming from source). The common
11763 -- case is when we generate a renaming which prematurely freezes the
11764 -- renamed internal entity, but we still want to be able to set copies
11765 -- of attribute values such as Size/Alignment.
11767 and then Comes_From_Source (T)
11768 then
11769 Too_Late;
11770 S := First_Subtype (T);
11772 if Present (Freeze_Node (S)) then
11773 if not Relaxed_RM_Semantics then
11774 Error_Msg_NE
11775 ("??no more representation items for }", Freeze_Node (S), S);
11776 end if;
11777 end if;
11779 return True;
11781 -- Check for case of untagged derived type whose parent either has
11782 -- primitive operations, or is a by reference type (RM 13.1(10)). In
11783 -- this case we do not output a Too_Late message, since there is no
11784 -- earlier point where the rep item could be placed to make it legal.
11786 elsif Is_Type (T)
11787 and then not FOnly
11788 and then Is_Derived_Type (T)
11789 and then not Is_Tagged_Type (T)
11790 then
11791 Parent_Type := Etype (Base_Type (T));
11793 if Has_Primitive_Operations (Parent_Type) then
11794 No_Type_Rep_Item;
11796 if not Relaxed_RM_Semantics then
11797 Error_Msg_NE
11798 ("\parent type & has primitive operations!", N, Parent_Type);
11799 end if;
11801 return True;
11803 elsif Is_By_Reference_Type (Parent_Type) then
11804 No_Type_Rep_Item;
11806 if not Relaxed_RM_Semantics then
11807 Error_Msg_NE
11808 ("\parent type & is a by reference type!", N, Parent_Type);
11809 end if;
11811 return True;
11812 end if;
11813 end if;
11815 -- No error, but one more warning to consider. The RM (surprisingly)
11816 -- allows this pattern:
11818 -- type S is ...
11819 -- primitive operations for S
11820 -- type R is new S;
11821 -- rep clause for S
11823 -- Meaning that calls on the primitive operations of S for values of
11824 -- type R may require possibly expensive implicit conversion operations.
11825 -- This is not an error, but is worth a warning.
11827 if not Relaxed_RM_Semantics and then Is_Type (T) then
11828 declare
11829 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
11831 begin
11832 if Present (DTL)
11833 and then Has_Primitive_Operations (Base_Type (T))
11835 -- For now, do not generate this warning for the case of aspect
11836 -- specification using Ada 2012 syntax, since we get wrong
11837 -- messages we do not understand. The whole business of derived
11838 -- types and rep items seems a bit confused when aspects are
11839 -- used, since the aspects are not evaluated till freeze time.
11841 and then not From_Aspect_Specification (N)
11842 then
11843 Error_Msg_Sloc := Sloc (DTL);
11844 Error_Msg_N
11845 ("representation item for& appears after derived type "
11846 & "declaration#??", N);
11847 Error_Msg_NE
11848 ("\may result in implicit conversions for primitive "
11849 & "operations of&??", N, T);
11850 Error_Msg_NE
11851 ("\to change representations when called with arguments "
11852 & "of type&??", N, DTL);
11853 end if;
11854 end;
11855 end if;
11857 -- No error, link item into head of chain of rep items for the entity,
11858 -- but avoid chaining if we have an overloadable entity, and the pragma
11859 -- is one that can apply to multiple overloaded entities.
11861 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
11862 declare
11863 Pname : constant Name_Id := Pragma_Name (N);
11864 begin
11865 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
11866 Name_External, Name_Interface)
11867 then
11868 return False;
11869 end if;
11870 end;
11871 end if;
11873 Record_Rep_Item (T, N);
11874 return False;
11875 end Rep_Item_Too_Late;
11877 -------------------------------------
11878 -- Replace_Type_References_Generic --
11879 -------------------------------------
11881 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
11882 TName : constant Name_Id := Chars (T);
11884 function Replace_Node (N : Node_Id) return Traverse_Result;
11885 -- Processes a single node in the traversal procedure below, checking
11886 -- if node N should be replaced, and if so, doing the replacement.
11888 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
11889 -- This instantiation provides the body of Replace_Type_References
11891 ------------------
11892 -- Replace_Node --
11893 ------------------
11895 function Replace_Node (N : Node_Id) return Traverse_Result is
11896 S : Entity_Id;
11897 P : Node_Id;
11899 begin
11900 -- Case of identifier
11902 if Nkind (N) = N_Identifier then
11904 -- If not the type name, check whether it is a reference to
11905 -- some other type, which must be frozen before the predicate
11906 -- function is analyzed, i.e. before the freeze node of the
11907 -- type to which the predicate applies.
11909 if Chars (N) /= TName then
11910 if Present (Current_Entity (N))
11911 and then Is_Type (Current_Entity (N))
11912 then
11913 Freeze_Before (Freeze_Node (T), Current_Entity (N));
11914 end if;
11916 return Skip;
11918 -- Otherwise do the replacement and we are done with this node
11920 else
11921 Replace_Type_Reference (N);
11922 return Skip;
11923 end if;
11925 -- Case of selected component (which is what a qualification
11926 -- looks like in the unanalyzed tree, which is what we have.
11928 elsif Nkind (N) = N_Selected_Component then
11930 -- If selector name is not our type, keeping going (we might
11931 -- still have an occurrence of the type in the prefix).
11933 if Nkind (Selector_Name (N)) /= N_Identifier
11934 or else Chars (Selector_Name (N)) /= TName
11935 then
11936 return OK;
11938 -- Selector name is our type, check qualification
11940 else
11941 -- Loop through scopes and prefixes, doing comparison
11943 S := Current_Scope;
11944 P := Prefix (N);
11945 loop
11946 -- Continue if no more scopes or scope with no name
11948 if No (S) or else Nkind (S) not in N_Has_Chars then
11949 return OK;
11950 end if;
11952 -- Do replace if prefix is an identifier matching the
11953 -- scope that we are currently looking at.
11955 if Nkind (P) = N_Identifier
11956 and then Chars (P) = Chars (S)
11957 then
11958 Replace_Type_Reference (N);
11959 return Skip;
11960 end if;
11962 -- Go check scope above us if prefix is itself of the
11963 -- form of a selected component, whose selector matches
11964 -- the scope we are currently looking at.
11966 if Nkind (P) = N_Selected_Component
11967 and then Nkind (Selector_Name (P)) = N_Identifier
11968 and then Chars (Selector_Name (P)) = Chars (S)
11969 then
11970 S := Scope (S);
11971 P := Prefix (P);
11973 -- For anything else, we don't have a match, so keep on
11974 -- going, there are still some weird cases where we may
11975 -- still have a replacement within the prefix.
11977 else
11978 return OK;
11979 end if;
11980 end loop;
11981 end if;
11983 -- Continue for any other node kind
11985 else
11986 return OK;
11987 end if;
11988 end Replace_Node;
11990 begin
11991 Replace_Type_Refs (N);
11992 end Replace_Type_References_Generic;
11994 -------------------------
11995 -- Same_Representation --
11996 -------------------------
11998 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
11999 T1 : constant Entity_Id := Underlying_Type (Typ1);
12000 T2 : constant Entity_Id := Underlying_Type (Typ2);
12002 begin
12003 -- A quick check, if base types are the same, then we definitely have
12004 -- the same representation, because the subtype specific representation
12005 -- attributes (Size and Alignment) do not affect representation from
12006 -- the point of view of this test.
12008 if Base_Type (T1) = Base_Type (T2) then
12009 return True;
12011 elsif Is_Private_Type (Base_Type (T2))
12012 and then Base_Type (T1) = Full_View (Base_Type (T2))
12013 then
12014 return True;
12015 end if;
12017 -- Tagged types never have differing representations
12019 if Is_Tagged_Type (T1) then
12020 return True;
12021 end if;
12023 -- Representations are definitely different if conventions differ
12025 if Convention (T1) /= Convention (T2) then
12026 return False;
12027 end if;
12029 -- Representations are different if component alignments or scalar
12030 -- storage orders differ.
12032 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
12033 and then
12034 (Is_Record_Type (T2) or else Is_Array_Type (T2))
12035 and then
12036 (Component_Alignment (T1) /= Component_Alignment (T2)
12037 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
12038 then
12039 return False;
12040 end if;
12042 -- For arrays, the only real issue is component size. If we know the
12043 -- component size for both arrays, and it is the same, then that's
12044 -- good enough to know we don't have a change of representation.
12046 if Is_Array_Type (T1) then
12047 if Known_Component_Size (T1)
12048 and then Known_Component_Size (T2)
12049 and then Component_Size (T1) = Component_Size (T2)
12050 then
12051 if VM_Target = No_VM then
12052 return True;
12054 -- In VM targets the representation of arrays with aliased
12055 -- components differs from arrays with non-aliased components
12057 else
12058 return Has_Aliased_Components (Base_Type (T1))
12060 Has_Aliased_Components (Base_Type (T2));
12061 end if;
12062 end if;
12063 end if;
12065 -- Types definitely have same representation if neither has non-standard
12066 -- representation since default representations are always consistent.
12067 -- If only one has non-standard representation, and the other does not,
12068 -- then we consider that they do not have the same representation. They
12069 -- might, but there is no way of telling early enough.
12071 if Has_Non_Standard_Rep (T1) then
12072 if not Has_Non_Standard_Rep (T2) then
12073 return False;
12074 end if;
12075 else
12076 return not Has_Non_Standard_Rep (T2);
12077 end if;
12079 -- Here the two types both have non-standard representation, and we need
12080 -- to determine if they have the same non-standard representation.
12082 -- For arrays, we simply need to test if the component sizes are the
12083 -- same. Pragma Pack is reflected in modified component sizes, so this
12084 -- check also deals with pragma Pack.
12086 if Is_Array_Type (T1) then
12087 return Component_Size (T1) = Component_Size (T2);
12089 -- Tagged types always have the same representation, because it is not
12090 -- possible to specify different representations for common fields.
12092 elsif Is_Tagged_Type (T1) then
12093 return True;
12095 -- Case of record types
12097 elsif Is_Record_Type (T1) then
12099 -- Packed status must conform
12101 if Is_Packed (T1) /= Is_Packed (T2) then
12102 return False;
12104 -- Otherwise we must check components. Typ2 maybe a constrained
12105 -- subtype with fewer components, so we compare the components
12106 -- of the base types.
12108 else
12109 Record_Case : declare
12110 CD1, CD2 : Entity_Id;
12112 function Same_Rep return Boolean;
12113 -- CD1 and CD2 are either components or discriminants. This
12114 -- function tests whether they have the same representation.
12116 --------------
12117 -- Same_Rep --
12118 --------------
12120 function Same_Rep return Boolean is
12121 begin
12122 if No (Component_Clause (CD1)) then
12123 return No (Component_Clause (CD2));
12124 else
12125 -- Note: at this point, component clauses have been
12126 -- normalized to the default bit order, so that the
12127 -- comparison of Component_Bit_Offsets is meaningful.
12129 return
12130 Present (Component_Clause (CD2))
12131 and then
12132 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12133 and then
12134 Esize (CD1) = Esize (CD2);
12135 end if;
12136 end Same_Rep;
12138 -- Start of processing for Record_Case
12140 begin
12141 if Has_Discriminants (T1) then
12143 -- The number of discriminants may be different if the
12144 -- derived type has fewer (constrained by values). The
12145 -- invisible discriminants retain the representation of
12146 -- the original, so the discrepancy does not per se
12147 -- indicate a different representation.
12149 CD1 := First_Discriminant (T1);
12150 CD2 := First_Discriminant (T2);
12151 while Present (CD1) and then Present (CD2) loop
12152 if not Same_Rep then
12153 return False;
12154 else
12155 Next_Discriminant (CD1);
12156 Next_Discriminant (CD2);
12157 end if;
12158 end loop;
12159 end if;
12161 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12162 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
12163 while Present (CD1) loop
12164 if not Same_Rep then
12165 return False;
12166 else
12167 Next_Component (CD1);
12168 Next_Component (CD2);
12169 end if;
12170 end loop;
12172 return True;
12173 end Record_Case;
12174 end if;
12176 -- For enumeration types, we must check each literal to see if the
12177 -- representation is the same. Note that we do not permit enumeration
12178 -- representation clauses for Character and Wide_Character, so these
12179 -- cases were already dealt with.
12181 elsif Is_Enumeration_Type (T1) then
12182 Enumeration_Case : declare
12183 L1, L2 : Entity_Id;
12185 begin
12186 L1 := First_Literal (T1);
12187 L2 := First_Literal (T2);
12188 while Present (L1) loop
12189 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12190 return False;
12191 else
12192 Next_Literal (L1);
12193 Next_Literal (L2);
12194 end if;
12195 end loop;
12197 return True;
12198 end Enumeration_Case;
12200 -- Any other types have the same representation for these purposes
12202 else
12203 return True;
12204 end if;
12205 end Same_Representation;
12207 --------------------------------
12208 -- Resolve_Iterable_Operation --
12209 --------------------------------
12211 procedure Resolve_Iterable_Operation
12212 (N : Node_Id;
12213 Cursor : Entity_Id;
12214 Typ : Entity_Id;
12215 Nam : Name_Id)
12217 Ent : Entity_Id;
12218 F1 : Entity_Id;
12219 F2 : Entity_Id;
12221 begin
12222 if not Is_Overloaded (N) then
12223 if not Is_Entity_Name (N)
12224 or else Ekind (Entity (N)) /= E_Function
12225 or else Scope (Entity (N)) /= Scope (Typ)
12226 or else No (First_Formal (Entity (N)))
12227 or else Etype (First_Formal (Entity (N))) /= Typ
12228 then
12229 Error_Msg_N ("iterable primitive must be local function name "
12230 & "whose first formal is an iterable type", N);
12231 return;
12232 end if;
12234 Ent := Entity (N);
12235 F1 := First_Formal (Ent);
12236 if Nam = Name_First then
12238 -- First (Container) => Cursor
12240 if Etype (Ent) /= Cursor then
12241 Error_Msg_N ("primitive for First must yield a curosr", N);
12242 end if;
12244 elsif Nam = Name_Next then
12246 -- Next (Container, Cursor) => Cursor
12248 F2 := Next_Formal (F1);
12250 if Etype (F2) /= Cursor
12251 or else Etype (Ent) /= Cursor
12252 or else Present (Next_Formal (F2))
12253 then
12254 Error_Msg_N ("no match for Next iterable primitive", N);
12255 end if;
12257 elsif Nam = Name_Has_Element then
12259 -- Has_Element (Container, Cursor) => Boolean
12261 F2 := Next_Formal (F1);
12262 if Etype (F2) /= Cursor
12263 or else Etype (Ent) /= Standard_Boolean
12264 or else Present (Next_Formal (F2))
12265 then
12266 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12267 end if;
12269 elsif Nam = Name_Element then
12270 F2 := Next_Formal (F1);
12272 if No (F2)
12273 or else Etype (F2) /= Cursor
12274 or else Present (Next_Formal (F2))
12275 then
12276 Error_Msg_N ("no match for Element iterable primitive", N);
12277 end if;
12278 null;
12280 else
12281 raise Program_Error;
12282 end if;
12284 else
12285 -- Overloaded case: find subprogram with proper signature.
12286 -- Caller will report error if no match is found.
12288 declare
12289 I : Interp_Index;
12290 It : Interp;
12292 begin
12293 Get_First_Interp (N, I, It);
12294 while Present (It.Typ) loop
12295 if Ekind (It.Nam) = E_Function
12296 and then Scope (It.Nam) = Scope (Typ)
12297 and then Etype (First_Formal (It.Nam)) = Typ
12298 then
12299 F1 := First_Formal (It.Nam);
12301 if Nam = Name_First then
12302 if Etype (It.Nam) = Cursor
12303 and then No (Next_Formal (F1))
12304 then
12305 Set_Entity (N, It.Nam);
12306 exit;
12307 end if;
12309 elsif Nam = Name_Next then
12310 F2 := Next_Formal (F1);
12312 if Present (F2)
12313 and then No (Next_Formal (F2))
12314 and then Etype (F2) = Cursor
12315 and then Etype (It.Nam) = Cursor
12316 then
12317 Set_Entity (N, It.Nam);
12318 exit;
12319 end if;
12321 elsif Nam = Name_Has_Element then
12322 F2 := Next_Formal (F1);
12324 if Present (F2)
12325 and then No (Next_Formal (F2))
12326 and then Etype (F2) = Cursor
12327 and then Etype (It.Nam) = Standard_Boolean
12328 then
12329 Set_Entity (N, It.Nam);
12330 F2 := Next_Formal (F1);
12331 exit;
12332 end if;
12334 elsif Nam = Name_Element then
12335 F2 := Next_Formal (F1);
12337 if Present (F2)
12338 and then No (Next_Formal (F2))
12339 and then Etype (F2) = Cursor
12340 then
12341 Set_Entity (N, It.Nam);
12342 exit;
12343 end if;
12344 end if;
12345 end if;
12347 Get_Next_Interp (I, It);
12348 end loop;
12349 end;
12350 end if;
12351 end Resolve_Iterable_Operation;
12353 ----------------
12354 -- Set_Biased --
12355 ----------------
12357 procedure Set_Biased
12358 (E : Entity_Id;
12359 N : Node_Id;
12360 Msg : String;
12361 Biased : Boolean := True)
12363 begin
12364 if Biased then
12365 Set_Has_Biased_Representation (E);
12367 if Warn_On_Biased_Representation then
12368 Error_Msg_NE
12369 ("?B?" & Msg & " forces biased representation for&", N, E);
12370 end if;
12371 end if;
12372 end Set_Biased;
12374 --------------------
12375 -- Set_Enum_Esize --
12376 --------------------
12378 procedure Set_Enum_Esize (T : Entity_Id) is
12379 Lo : Uint;
12380 Hi : Uint;
12381 Sz : Nat;
12383 begin
12384 Init_Alignment (T);
12386 -- Find the minimum standard size (8,16,32,64) that fits
12388 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12389 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12391 if Lo < 0 then
12392 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12393 Sz := Standard_Character_Size; -- May be > 8 on some targets
12395 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12396 Sz := 16;
12398 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12399 Sz := 32;
12401 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12402 Sz := 64;
12403 end if;
12405 else
12406 if Hi < Uint_2**08 then
12407 Sz := Standard_Character_Size; -- May be > 8 on some targets
12409 elsif Hi < Uint_2**16 then
12410 Sz := 16;
12412 elsif Hi < Uint_2**32 then
12413 Sz := 32;
12415 else pragma Assert (Hi < Uint_2**63);
12416 Sz := 64;
12417 end if;
12418 end if;
12420 -- That minimum is the proper size unless we have a foreign convention
12421 -- and the size required is 32 or less, in which case we bump the size
12422 -- up to 32. This is required for C and C++ and seems reasonable for
12423 -- all other foreign conventions.
12425 if Has_Foreign_Convention (T)
12426 and then Esize (T) < Standard_Integer_Size
12428 -- Don't do this if Short_Enums on target
12430 and then not Target_Short_Enums
12431 then
12432 Init_Esize (T, Standard_Integer_Size);
12433 else
12434 Init_Esize (T, Sz);
12435 end if;
12436 end Set_Enum_Esize;
12438 -----------------------------
12439 -- Uninstall_Discriminants --
12440 -----------------------------
12442 procedure Uninstall_Discriminants (E : Entity_Id) is
12443 Disc : Entity_Id;
12444 Prev : Entity_Id;
12445 Outer : Entity_Id;
12447 begin
12448 -- Discriminants have been made visible for type declarations and
12449 -- protected type declarations, not for subtype declarations.
12451 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12452 Disc := First_Discriminant (E);
12453 while Present (Disc) loop
12454 if Disc /= Current_Entity (Disc) then
12455 Prev := Current_Entity (Disc);
12456 while Present (Prev)
12457 and then Present (Homonym (Prev))
12458 and then Homonym (Prev) /= Disc
12459 loop
12460 Prev := Homonym (Prev);
12461 end loop;
12462 else
12463 Prev := Empty;
12464 end if;
12466 Set_Is_Immediately_Visible (Disc, False);
12468 Outer := Homonym (Disc);
12469 while Present (Outer) and then Scope (Outer) = E loop
12470 Outer := Homonym (Outer);
12471 end loop;
12473 -- Reset homonym link of other entities, but do not modify link
12474 -- between entities in current scope, so that the back-end can
12475 -- have a proper count of local overloadings.
12477 if No (Prev) then
12478 Set_Name_Entity_Id (Chars (Disc), Outer);
12480 elsif Scope (Prev) /= Scope (Disc) then
12481 Set_Homonym (Prev, Outer);
12482 end if;
12484 Next_Discriminant (Disc);
12485 end loop;
12486 end if;
12487 end Uninstall_Discriminants;
12489 -------------------------------------------
12490 -- Uninstall_Discriminants_And_Pop_Scope --
12491 -------------------------------------------
12493 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12494 begin
12495 if Has_Discriminants (E) then
12496 Uninstall_Discriminants (E);
12497 Pop_Scope;
12498 end if;
12499 end Uninstall_Discriminants_And_Pop_Scope;
12501 ------------------------------
12502 -- Validate_Address_Clauses --
12503 ------------------------------
12505 procedure Validate_Address_Clauses is
12506 begin
12507 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12508 declare
12509 ACCR : Address_Clause_Check_Record
12510 renames Address_Clause_Checks.Table (J);
12512 Expr : Node_Id;
12514 X_Alignment : Uint;
12515 Y_Alignment : Uint;
12517 X_Size : Uint;
12518 Y_Size : Uint;
12520 begin
12521 -- Skip processing of this entry if warning already posted
12523 if not Address_Warning_Posted (ACCR.N) then
12524 Expr := Original_Node (Expression (ACCR.N));
12526 -- Get alignments
12528 X_Alignment := Alignment (ACCR.X);
12529 Y_Alignment := Alignment (ACCR.Y);
12531 -- Similarly obtain sizes
12533 X_Size := Esize (ACCR.X);
12534 Y_Size := Esize (ACCR.Y);
12536 -- Check for large object overlaying smaller one
12538 if Y_Size > Uint_0
12539 and then X_Size > Uint_0
12540 and then X_Size > Y_Size
12541 then
12542 Error_Msg_NE
12543 ("??& overlays smaller object", ACCR.N, ACCR.X);
12544 Error_Msg_N
12545 ("\??program execution may be erroneous", ACCR.N);
12546 Error_Msg_Uint_1 := X_Size;
12547 Error_Msg_NE
12548 ("\??size of & is ^", ACCR.N, ACCR.X);
12549 Error_Msg_Uint_1 := Y_Size;
12550 Error_Msg_NE
12551 ("\??size of & is ^", ACCR.N, ACCR.Y);
12553 -- Check for inadequate alignment, both of the base object
12554 -- and of the offset, if any.
12556 -- Note: we do not check the alignment if we gave a size
12557 -- warning, since it would likely be redundant.
12559 elsif Y_Alignment /= Uint_0
12560 and then (Y_Alignment < X_Alignment
12561 or else (ACCR.Off
12562 and then
12563 Nkind (Expr) = N_Attribute_Reference
12564 and then
12565 Attribute_Name (Expr) = Name_Address
12566 and then
12567 Has_Compatible_Alignment
12568 (ACCR.X, Prefix (Expr))
12569 /= Known_Compatible))
12570 then
12571 Error_Msg_NE
12572 ("??specified address for& may be inconsistent "
12573 & "with alignment", ACCR.N, ACCR.X);
12574 Error_Msg_N
12575 ("\??program execution may be erroneous (RM 13.3(27))",
12576 ACCR.N);
12577 Error_Msg_Uint_1 := X_Alignment;
12578 Error_Msg_NE
12579 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12580 Error_Msg_Uint_1 := Y_Alignment;
12581 Error_Msg_NE
12582 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12583 if Y_Alignment >= X_Alignment then
12584 Error_Msg_N
12585 ("\??but offset is not multiple of alignment", ACCR.N);
12586 end if;
12587 end if;
12588 end if;
12589 end;
12590 end loop;
12591 end Validate_Address_Clauses;
12593 ---------------------------
12594 -- Validate_Independence --
12595 ---------------------------
12597 procedure Validate_Independence is
12598 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12599 N : Node_Id;
12600 E : Entity_Id;
12601 IC : Boolean;
12602 Comp : Entity_Id;
12603 Addr : Node_Id;
12604 P : Node_Id;
12606 procedure Check_Array_Type (Atyp : Entity_Id);
12607 -- Checks if the array type Atyp has independent components, and
12608 -- if not, outputs an appropriate set of error messages.
12610 procedure No_Independence;
12611 -- Output message that independence cannot be guaranteed
12613 function OK_Component (C : Entity_Id) return Boolean;
12614 -- Checks one component to see if it is independently accessible, and
12615 -- if so yields True, otherwise yields False if independent access
12616 -- cannot be guaranteed. This is a conservative routine, it only
12617 -- returns True if it knows for sure, it returns False if it knows
12618 -- there is a problem, or it cannot be sure there is no problem.
12620 procedure Reason_Bad_Component (C : Entity_Id);
12621 -- Outputs continuation message if a reason can be determined for
12622 -- the component C being bad.
12624 ----------------------
12625 -- Check_Array_Type --
12626 ----------------------
12628 procedure Check_Array_Type (Atyp : Entity_Id) is
12629 Ctyp : constant Entity_Id := Component_Type (Atyp);
12631 begin
12632 -- OK if no alignment clause, no pack, and no component size
12634 if not Has_Component_Size_Clause (Atyp)
12635 and then not Has_Alignment_Clause (Atyp)
12636 and then not Is_Packed (Atyp)
12637 then
12638 return;
12639 end if;
12641 -- Case of component size is greater than or equal to 64 and the
12642 -- alignment of the array is at least as large as the alignment
12643 -- of the component. We are definitely OK in this situation.
12645 if Known_Component_Size (Atyp)
12646 and then Component_Size (Atyp) >= 64
12647 and then Known_Alignment (Atyp)
12648 and then Known_Alignment (Ctyp)
12649 and then Alignment (Atyp) >= Alignment (Ctyp)
12650 then
12651 return;
12652 end if;
12654 -- Check actual component size
12656 if not Known_Component_Size (Atyp)
12657 or else not (Addressable (Component_Size (Atyp))
12658 and then Component_Size (Atyp) < 64)
12659 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12660 then
12661 No_Independence;
12663 -- Bad component size, check reason
12665 if Has_Component_Size_Clause (Atyp) then
12666 P := Get_Attribute_Definition_Clause
12667 (Atyp, Attribute_Component_Size);
12669 if Present (P) then
12670 Error_Msg_Sloc := Sloc (P);
12671 Error_Msg_N ("\because of Component_Size clause#", N);
12672 return;
12673 end if;
12674 end if;
12676 if Is_Packed (Atyp) then
12677 P := Get_Rep_Pragma (Atyp, Name_Pack);
12679 if Present (P) then
12680 Error_Msg_Sloc := Sloc (P);
12681 Error_Msg_N ("\because of pragma Pack#", N);
12682 return;
12683 end if;
12684 end if;
12686 -- No reason found, just return
12688 return;
12689 end if;
12691 -- Array type is OK independence-wise
12693 return;
12694 end Check_Array_Type;
12696 ---------------------
12697 -- No_Independence --
12698 ---------------------
12700 procedure No_Independence is
12701 begin
12702 if Pragma_Name (N) = Name_Independent then
12703 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
12704 else
12705 Error_Msg_NE
12706 ("independent components cannot be guaranteed for&", N, E);
12707 end if;
12708 end No_Independence;
12710 ------------------
12711 -- OK_Component --
12712 ------------------
12714 function OK_Component (C : Entity_Id) return Boolean is
12715 Rec : constant Entity_Id := Scope (C);
12716 Ctyp : constant Entity_Id := Etype (C);
12718 begin
12719 -- OK if no component clause, no Pack, and no alignment clause
12721 if No (Component_Clause (C))
12722 and then not Is_Packed (Rec)
12723 and then not Has_Alignment_Clause (Rec)
12724 then
12725 return True;
12726 end if;
12728 -- Here we look at the actual component layout. A component is
12729 -- addressable if its size is a multiple of the Esize of the
12730 -- component type, and its starting position in the record has
12731 -- appropriate alignment, and the record itself has appropriate
12732 -- alignment to guarantee the component alignment.
12734 -- Make sure sizes are static, always assume the worst for any
12735 -- cases where we cannot check static values.
12737 if not (Known_Static_Esize (C)
12738 and then
12739 Known_Static_Esize (Ctyp))
12740 then
12741 return False;
12742 end if;
12744 -- Size of component must be addressable or greater than 64 bits
12745 -- and a multiple of bytes.
12747 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
12748 return False;
12749 end if;
12751 -- Check size is proper multiple
12753 if Esize (C) mod Esize (Ctyp) /= 0 then
12754 return False;
12755 end if;
12757 -- Check alignment of component is OK
12759 if not Known_Component_Bit_Offset (C)
12760 or else Component_Bit_Offset (C) < Uint_0
12761 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
12762 then
12763 return False;
12764 end if;
12766 -- Check alignment of record type is OK
12768 if not Known_Alignment (Rec)
12769 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12770 then
12771 return False;
12772 end if;
12774 -- All tests passed, component is addressable
12776 return True;
12777 end OK_Component;
12779 --------------------------
12780 -- Reason_Bad_Component --
12781 --------------------------
12783 procedure Reason_Bad_Component (C : Entity_Id) is
12784 Rec : constant Entity_Id := Scope (C);
12785 Ctyp : constant Entity_Id := Etype (C);
12787 begin
12788 -- If component clause present assume that's the problem
12790 if Present (Component_Clause (C)) then
12791 Error_Msg_Sloc := Sloc (Component_Clause (C));
12792 Error_Msg_N ("\because of Component_Clause#", N);
12793 return;
12794 end if;
12796 -- If pragma Pack clause present, assume that's the problem
12798 if Is_Packed (Rec) then
12799 P := Get_Rep_Pragma (Rec, Name_Pack);
12801 if Present (P) then
12802 Error_Msg_Sloc := Sloc (P);
12803 Error_Msg_N ("\because of pragma Pack#", N);
12804 return;
12805 end if;
12806 end if;
12808 -- See if record has bad alignment clause
12810 if Has_Alignment_Clause (Rec)
12811 and then Known_Alignment (Rec)
12812 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12813 then
12814 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
12816 if Present (P) then
12817 Error_Msg_Sloc := Sloc (P);
12818 Error_Msg_N ("\because of Alignment clause#", N);
12819 end if;
12820 end if;
12822 -- Couldn't find a reason, so return without a message
12824 return;
12825 end Reason_Bad_Component;
12827 -- Start of processing for Validate_Independence
12829 begin
12830 for J in Independence_Checks.First .. Independence_Checks.Last loop
12831 N := Independence_Checks.Table (J).N;
12832 E := Independence_Checks.Table (J).E;
12833 IC := Pragma_Name (N) = Name_Independent_Components;
12835 -- Deal with component case
12837 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
12838 if not OK_Component (E) then
12839 No_Independence;
12840 Reason_Bad_Component (E);
12841 goto Continue;
12842 end if;
12843 end if;
12845 -- Deal with record with Independent_Components
12847 if IC and then Is_Record_Type (E) then
12848 Comp := First_Component_Or_Discriminant (E);
12849 while Present (Comp) loop
12850 if not OK_Component (Comp) then
12851 No_Independence;
12852 Reason_Bad_Component (Comp);
12853 goto Continue;
12854 end if;
12856 Next_Component_Or_Discriminant (Comp);
12857 end loop;
12858 end if;
12860 -- Deal with address clause case
12862 if Is_Object (E) then
12863 Addr := Address_Clause (E);
12865 if Present (Addr) then
12866 No_Independence;
12867 Error_Msg_Sloc := Sloc (Addr);
12868 Error_Msg_N ("\because of Address clause#", N);
12869 goto Continue;
12870 end if;
12871 end if;
12873 -- Deal with independent components for array type
12875 if IC and then Is_Array_Type (E) then
12876 Check_Array_Type (E);
12877 end if;
12879 -- Deal with independent components for array object
12881 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
12882 Check_Array_Type (Etype (E));
12883 end if;
12885 <<Continue>> null;
12886 end loop;
12887 end Validate_Independence;
12889 ------------------------------
12890 -- Validate_Iterable_Aspect --
12891 ------------------------------
12893 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
12894 Assoc : Node_Id;
12895 Expr : Node_Id;
12897 Prim : Node_Id;
12898 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
12900 First_Id : Entity_Id;
12901 Next_Id : Entity_Id;
12902 Has_Element_Id : Entity_Id;
12903 Element_Id : Entity_Id;
12905 begin
12906 -- If previous error aspect is unusable
12908 if Cursor = Any_Type then
12909 return;
12910 end if;
12912 First_Id := Empty;
12913 Next_Id := Empty;
12914 Has_Element_Id := Empty;
12915 Element_Id := Empty;
12917 -- Each expression must resolve to a function with the proper signature
12919 Assoc := First (Component_Associations (Expression (ASN)));
12920 while Present (Assoc) loop
12921 Expr := Expression (Assoc);
12922 Analyze (Expr);
12924 Prim := First (Choices (Assoc));
12926 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
12927 Error_Msg_N ("illegal name in association", Prim);
12929 elsif Chars (Prim) = Name_First then
12930 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
12931 First_Id := Entity (Expr);
12933 elsif Chars (Prim) = Name_Next then
12934 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
12935 Next_Id := Entity (Expr);
12937 elsif Chars (Prim) = Name_Has_Element then
12938 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
12939 Has_Element_Id := Entity (Expr);
12941 elsif Chars (Prim) = Name_Element then
12942 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
12943 Element_Id := Entity (Expr);
12945 else
12946 Error_Msg_N ("invalid name for iterable function", Prim);
12947 end if;
12949 Next (Assoc);
12950 end loop;
12952 if No (First_Id) then
12953 Error_Msg_N ("match for First primitive not found", ASN);
12955 elsif No (Next_Id) then
12956 Error_Msg_N ("match for Next primitive not found", ASN);
12958 elsif No (Has_Element_Id) then
12959 Error_Msg_N ("match for Has_Element primitive not found", ASN);
12961 elsif No (Element_Id) then
12962 null; -- Optional.
12963 end if;
12964 end Validate_Iterable_Aspect;
12966 -----------------------------------
12967 -- Validate_Unchecked_Conversion --
12968 -----------------------------------
12970 procedure Validate_Unchecked_Conversion
12971 (N : Node_Id;
12972 Act_Unit : Entity_Id)
12974 Source : Entity_Id;
12975 Target : Entity_Id;
12976 Vnode : Node_Id;
12978 begin
12979 -- Obtain source and target types. Note that we call Ancestor_Subtype
12980 -- here because the processing for generic instantiation always makes
12981 -- subtypes, and we want the original frozen actual types.
12983 -- If we are dealing with private types, then do the check on their
12984 -- fully declared counterparts if the full declarations have been
12985 -- encountered (they don't have to be visible, but they must exist).
12987 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
12989 if Is_Private_Type (Source)
12990 and then Present (Underlying_Type (Source))
12991 then
12992 Source := Underlying_Type (Source);
12993 end if;
12995 Target := Ancestor_Subtype (Etype (Act_Unit));
12997 -- If either type is generic, the instantiation happens within a generic
12998 -- unit, and there is nothing to check. The proper check will happen
12999 -- when the enclosing generic is instantiated.
13001 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13002 return;
13003 end if;
13005 if Is_Private_Type (Target)
13006 and then Present (Underlying_Type (Target))
13007 then
13008 Target := Underlying_Type (Target);
13009 end if;
13011 -- Source may be unconstrained array, but not target
13013 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
13014 Error_Msg_N
13015 ("unchecked conversion to unconstrained array not allowed", N);
13016 return;
13017 end if;
13019 -- Warn if conversion between two different convention pointers
13021 if Is_Access_Type (Target)
13022 and then Is_Access_Type (Source)
13023 and then Convention (Target) /= Convention (Source)
13024 and then Warn_On_Unchecked_Conversion
13025 then
13026 -- Give warnings for subprogram pointers only on most targets
13028 if Is_Access_Subprogram_Type (Target)
13029 or else Is_Access_Subprogram_Type (Source)
13030 then
13031 Error_Msg_N
13032 ("?z?conversion between pointers with different conventions!",
13034 end if;
13035 end if;
13037 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13038 -- warning when compiling GNAT-related sources.
13040 if Warn_On_Unchecked_Conversion
13041 and then not In_Predefined_Unit (N)
13042 and then RTU_Loaded (Ada_Calendar)
13043 and then (Chars (Source) = Name_Time
13044 or else
13045 Chars (Target) = Name_Time)
13046 then
13047 -- If Ada.Calendar is loaded and the name of one of the operands is
13048 -- Time, there is a good chance that this is Ada.Calendar.Time.
13050 declare
13051 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
13052 begin
13053 pragma Assert (Present (Calendar_Time));
13055 if Source = Calendar_Time or else Target = Calendar_Time then
13056 Error_Msg_N
13057 ("?z?representation of 'Time values may change between "
13058 & "'G'N'A'T versions", N);
13059 end if;
13060 end;
13061 end if;
13063 -- Make entry in unchecked conversion table for later processing by
13064 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13065 -- (using values set by the back-end where possible). This is only done
13066 -- if the appropriate warning is active.
13068 if Warn_On_Unchecked_Conversion then
13069 Unchecked_Conversions.Append
13070 (New_Val => UC_Entry'(Eloc => Sloc (N),
13071 Source => Source,
13072 Target => Target,
13073 Act_Unit => Act_Unit));
13075 -- If both sizes are known statically now, then back end annotation
13076 -- is not required to do a proper check but if either size is not
13077 -- known statically, then we need the annotation.
13079 if Known_Static_RM_Size (Source)
13080 and then
13081 Known_Static_RM_Size (Target)
13082 then
13083 null;
13084 else
13085 Back_Annotate_Rep_Info := True;
13086 end if;
13087 end if;
13089 -- If unchecked conversion to access type, and access type is declared
13090 -- in the same unit as the unchecked conversion, then set the flag
13091 -- No_Strict_Aliasing (no strict aliasing is implicit here)
13093 if Is_Access_Type (Target) and then
13094 In_Same_Source_Unit (Target, N)
13095 then
13096 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13097 end if;
13099 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13100 -- the back end needs to perform special validation checks.
13102 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13103 -- have full expansion and the back end is called ???
13105 Vnode :=
13106 Make_Validate_Unchecked_Conversion (Sloc (N));
13107 Set_Source_Type (Vnode, Source);
13108 Set_Target_Type (Vnode, Target);
13110 -- If the unchecked conversion node is in a list, just insert before it.
13111 -- If not we have some strange case, not worth bothering about.
13113 if Is_List_Member (N) then
13114 Insert_After (N, Vnode);
13115 end if;
13116 end Validate_Unchecked_Conversion;
13118 ------------------------------------
13119 -- Validate_Unchecked_Conversions --
13120 ------------------------------------
13122 procedure Validate_Unchecked_Conversions is
13123 begin
13124 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13125 declare
13126 T : UC_Entry renames Unchecked_Conversions.Table (N);
13128 Eloc : constant Source_Ptr := T.Eloc;
13129 Source : constant Entity_Id := T.Source;
13130 Target : constant Entity_Id := T.Target;
13131 Act_Unit : constant Entity_Id := T.Act_Unit;
13133 Source_Siz : Uint;
13134 Target_Siz : Uint;
13136 begin
13137 -- Skip if function marked as warnings off
13139 if Warnings_Off (Act_Unit) then
13140 goto Continue;
13141 end if;
13143 -- This validation check, which warns if we have unequal sizes for
13144 -- unchecked conversion, and thus potentially implementation
13145 -- dependent semantics, is one of the few occasions on which we
13146 -- use the official RM size instead of Esize. See description in
13147 -- Einfo "Handling of Type'Size Values" for details.
13149 if Serious_Errors_Detected = 0
13150 and then Known_Static_RM_Size (Source)
13151 and then Known_Static_RM_Size (Target)
13153 -- Don't do the check if warnings off for either type, note the
13154 -- deliberate use of OR here instead of OR ELSE to get the flag
13155 -- Warnings_Off_Used set for both types if appropriate.
13157 and then not (Has_Warnings_Off (Source)
13159 Has_Warnings_Off (Target))
13160 then
13161 Source_Siz := RM_Size (Source);
13162 Target_Siz := RM_Size (Target);
13164 if Source_Siz /= Target_Siz then
13165 Error_Msg
13166 ("?z?types for unchecked conversion have different sizes!",
13167 Eloc);
13169 if All_Errors_Mode then
13170 Error_Msg_Name_1 := Chars (Source);
13171 Error_Msg_Uint_1 := Source_Siz;
13172 Error_Msg_Name_2 := Chars (Target);
13173 Error_Msg_Uint_2 := Target_Siz;
13174 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
13176 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13178 if Is_Discrete_Type (Source)
13179 and then
13180 Is_Discrete_Type (Target)
13181 then
13182 if Source_Siz > Target_Siz then
13183 Error_Msg
13184 ("\?z?^ high order bits of source will "
13185 & "be ignored!", Eloc);
13187 elsif Is_Unsigned_Type (Source) then
13188 Error_Msg
13189 ("\?z?source will be extended with ^ high order "
13190 & "zero bits!", Eloc);
13192 else
13193 Error_Msg
13194 ("\?z?source will be extended with ^ high order "
13195 & "sign bits!", Eloc);
13196 end if;
13198 elsif Source_Siz < Target_Siz then
13199 if Is_Discrete_Type (Target) then
13200 if Bytes_Big_Endian then
13201 Error_Msg
13202 ("\?z?target value will include ^ undefined "
13203 & "low order bits!", Eloc);
13204 else
13205 Error_Msg
13206 ("\?z?target value will include ^ undefined "
13207 & "high order bits!", Eloc);
13208 end if;
13210 else
13211 Error_Msg
13212 ("\?z?^ trailing bits of target value will be "
13213 & "undefined!", Eloc);
13214 end if;
13216 else pragma Assert (Source_Siz > Target_Siz);
13217 Error_Msg
13218 ("\?z?^ trailing bits of source will be ignored!",
13219 Eloc);
13220 end if;
13221 end if;
13222 end if;
13223 end if;
13225 -- If both types are access types, we need to check the alignment.
13226 -- If the alignment of both is specified, we can do it here.
13228 if Serious_Errors_Detected = 0
13229 and then Is_Access_Type (Source)
13230 and then Is_Access_Type (Target)
13231 and then Target_Strict_Alignment
13232 and then Present (Designated_Type (Source))
13233 and then Present (Designated_Type (Target))
13234 then
13235 declare
13236 D_Source : constant Entity_Id := Designated_Type (Source);
13237 D_Target : constant Entity_Id := Designated_Type (Target);
13239 begin
13240 if Known_Alignment (D_Source)
13241 and then
13242 Known_Alignment (D_Target)
13243 then
13244 declare
13245 Source_Align : constant Uint := Alignment (D_Source);
13246 Target_Align : constant Uint := Alignment (D_Target);
13248 begin
13249 if Source_Align < Target_Align
13250 and then not Is_Tagged_Type (D_Source)
13252 -- Suppress warning if warnings suppressed on either
13253 -- type or either designated type. Note the use of
13254 -- OR here instead of OR ELSE. That is intentional,
13255 -- we would like to set flag Warnings_Off_Used in
13256 -- all types for which warnings are suppressed.
13258 and then not (Has_Warnings_Off (D_Source)
13260 Has_Warnings_Off (D_Target)
13262 Has_Warnings_Off (Source)
13264 Has_Warnings_Off (Target))
13265 then
13266 Error_Msg_Uint_1 := Target_Align;
13267 Error_Msg_Uint_2 := Source_Align;
13268 Error_Msg_Node_1 := D_Target;
13269 Error_Msg_Node_2 := D_Source;
13270 Error_Msg
13271 ("?z?alignment of & (^) is stricter than "
13272 & "alignment of & (^)!", Eloc);
13273 Error_Msg
13274 ("\?z?resulting access value may have invalid "
13275 & "alignment!", Eloc);
13276 end if;
13277 end;
13278 end if;
13279 end;
13280 end if;
13281 end;
13283 <<Continue>>
13284 null;
13285 end loop;
13286 end Validate_Unchecked_Conversions;
13288 end Sem_Ch13;