2015-05-12 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch13.adb
blob8e4eb0be93ed1431ecb5850b4a45f502be485d6e
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Case; use Sem_Case;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Sinput; use Sinput;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Ttypes; use Ttypes;
67 with Tbuild; use Tbuild;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
71 with GNAT.Heap_Sort_G;
73 package body Sem_Ch13 is
75 SSU : constant Pos := System_Storage_Unit;
76 -- Convenient short hand for commonly used constant
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
83 -- This routine is called after setting one of the sizes of type entity
84 -- Typ to Size. The purpose is to deal with the situation of a derived
85 -- type whose inherited alignment is no longer appropriate for the new
86 -- size value. In this case, we reset the Alignment to unknown.
88 procedure Build_Discrete_Static_Predicate
89 (Typ : Entity_Id;
90 Expr : Node_Id;
91 Nam : Name_Id);
92 -- Given a predicated type Typ, where Typ is a discrete static subtype,
93 -- whose predicate expression is Expr, tests if Expr is a static predicate,
94 -- and if so, builds the predicate range list. Nam is the name of the one
95 -- argument to the predicate function. Occurrences of the type name in the
96 -- predicate expression have been replaced by identifier references to this
97 -- name, which is unique, so any identifier with Chars matching Nam must be
98 -- a reference to the type. If the predicate is non-static, this procedure
99 -- returns doing nothing. If the predicate is static, then the predicate
100 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
101 -- rewritten as a canonicalized membership operation.
103 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
104 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
105 -- then either there are pragma Predicate entries on the rep chain for the
106 -- type (note that Predicate aspects are converted to pragma Predicate), or
107 -- there are inherited aspects from a parent type, or ancestor subtypes.
108 -- This procedure builds the spec and body for the Predicate function that
109 -- tests these predicates. N is the freeze node for the type. The spec of
110 -- the function is inserted before the freeze node, and the body of the
111 -- function is inserted after the freeze node. If the predicate expression
112 -- has at least one Raise_Expression, then this procedure also builds the
113 -- M version of the predicate function for use in membership tests.
115 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
116 -- Called if both Storage_Pool and Storage_Size attribute definition
117 -- clauses (SP and SS) are present for entity Ent. Issue error message.
119 procedure Freeze_Entity_Checks (N : Node_Id);
120 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
121 -- to generate appropriate semantic checks that are delayed until this
122 -- point (they had to be delayed this long for cases of delayed aspects,
123 -- e.g. analysis of statically predicated subtypes in choices, for which
124 -- we have to be sure the subtypes in question are frozen before checking.
126 function Get_Alignment_Value (Expr : Node_Id) return Uint;
127 -- Given the expression for an alignment value, returns the corresponding
128 -- Uint value. If the value is inappropriate, then error messages are
129 -- posted as required, and a value of No_Uint is returned.
131 function Is_Operational_Item (N : Node_Id) return Boolean;
132 -- A specification for a stream attribute is allowed before the full type
133 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
134 -- that do not specify a representation characteristic are operational
135 -- attributes.
137 function Is_Predicate_Static
138 (Expr : Node_Id;
139 Nam : Name_Id) return Boolean;
140 -- Given predicate expression Expr, tests if Expr is predicate-static in
141 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
142 -- name in the predicate expression have been replaced by references to
143 -- an identifier whose Chars field is Nam. This name is unique, so any
144 -- identifier with Chars matching Nam must be a reference to the type.
145 -- Returns True if the expression is predicate-static and False otherwise,
146 -- but is not in the business of setting flags or issuing error messages.
148 -- Only scalar types can have static predicates, so False is always
149 -- returned for non-scalar types.
151 -- Note: the RM seems to suggest that string types can also have static
152 -- predicates. But that really makes lttle sense as very few useful
153 -- predicates can be constructed for strings. Remember that:
155 -- "ABC" < "DEF"
157 -- is not a static expression. So even though the clearly faulty RM wording
158 -- allows the following:
160 -- subtype S is String with Static_Predicate => S < "DEF"
162 -- We can't allow this, otherwise we have predicate-static applying to a
163 -- larger class than static expressions, which was never intended.
165 procedure New_Stream_Subprogram
166 (N : Node_Id;
167 Ent : Entity_Id;
168 Subp : Entity_Id;
169 Nam : TSS_Name_Type);
170 -- Create a subprogram renaming of a given stream attribute to the
171 -- designated subprogram and then in the tagged case, provide this as a
172 -- primitive operation, or in the untagged case make an appropriate TSS
173 -- entry. This is more properly an expansion activity than just semantics,
174 -- but the presence of user-defined stream functions for limited types
175 -- is a legality check, which is why this takes place here rather than in
176 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
177 -- function to be generated.
179 -- To avoid elaboration anomalies with freeze nodes, for untagged types
180 -- we generate both a subprogram declaration and a subprogram renaming
181 -- declaration, so that the attribute specification is handled as a
182 -- renaming_as_body. For tagged types, the specification is one of the
183 -- primitive specs.
185 procedure Resolve_Iterable_Operation
186 (N : Node_Id;
187 Cursor : Entity_Id;
188 Typ : Entity_Id;
189 Nam : Name_Id);
190 -- If the name of a primitive operation for an Iterable aspect is
191 -- overloaded, resolve according to required signature.
193 procedure Set_Biased
194 (E : Entity_Id;
195 N : Node_Id;
196 Msg : String;
197 Biased : Boolean := True);
198 -- If Biased is True, sets Has_Biased_Representation flag for E, and
199 -- outputs a warning message at node N if Warn_On_Biased_Representation is
200 -- is True. This warning inserts the string Msg to describe the construct
201 -- causing biasing.
203 ----------------------------------------------
204 -- Table for Validate_Unchecked_Conversions --
205 ----------------------------------------------
207 -- The following table collects unchecked conversions for validation.
208 -- Entries are made by Validate_Unchecked_Conversion and then the call
209 -- to Validate_Unchecked_Conversions does the actual error checking and
210 -- posting of warnings. The reason for this delayed processing is to take
211 -- advantage of back-annotations of size and alignment values performed by
212 -- the back end.
214 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
215 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
216 -- already have modified all Sloc values if the -gnatD option is set.
218 type UC_Entry is record
219 Eloc : Source_Ptr; -- node used for posting warnings
220 Source : Entity_Id; -- source type for unchecked conversion
221 Target : Entity_Id; -- target type for unchecked conversion
222 Act_Unit : Entity_Id; -- actual function instantiated
223 end record;
225 package Unchecked_Conversions is new Table.Table (
226 Table_Component_Type => UC_Entry,
227 Table_Index_Type => Int,
228 Table_Low_Bound => 1,
229 Table_Initial => 50,
230 Table_Increment => 200,
231 Table_Name => "Unchecked_Conversions");
233 ----------------------------------------
234 -- Table for Validate_Address_Clauses --
235 ----------------------------------------
237 -- If an address clause has the form
239 -- for X'Address use Expr
241 -- where Expr is of the form Y'Address or recursively is a reference to a
242 -- constant of either of these forms, and X and Y are entities of objects,
243 -- then if Y has a smaller alignment than X, that merits a warning about
244 -- possible bad alignment. The following table collects address clauses of
245 -- this kind. We put these in a table so that they can be checked after the
246 -- back end has completed annotation of the alignments of objects, since we
247 -- can catch more cases that way.
249 type Address_Clause_Check_Record is record
250 N : Node_Id;
251 -- The address clause
253 X : Entity_Id;
254 -- The entity of the object overlaying Y
256 Y : Entity_Id;
257 -- The entity of the object being overlaid
259 Off : Boolean;
260 -- Whether the address is offset within Y
261 end record;
263 package Address_Clause_Checks is new Table.Table (
264 Table_Component_Type => Address_Clause_Check_Record,
265 Table_Index_Type => Int,
266 Table_Low_Bound => 1,
267 Table_Initial => 20,
268 Table_Increment => 200,
269 Table_Name => "Address_Clause_Checks");
271 -----------------------------------------
272 -- Adjust_Record_For_Reverse_Bit_Order --
273 -----------------------------------------
275 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
276 Comp : Node_Id;
277 CC : Node_Id;
279 begin
280 -- Processing depends on version of Ada
282 -- For Ada 95, we just renumber bits within a storage unit. We do the
283 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
284 -- Ada 83, and are free to add this extension.
286 if Ada_Version < Ada_2005 then
287 Comp := First_Component_Or_Discriminant (R);
288 while Present (Comp) loop
289 CC := Component_Clause (Comp);
291 -- If component clause is present, then deal with the non-default
292 -- bit order case for Ada 95 mode.
294 -- We only do this processing for the base type, and in fact that
295 -- is important, since otherwise if there are record subtypes, we
296 -- could reverse the bits once for each subtype, which is wrong.
298 if Present (CC) and then Ekind (R) = E_Record_Type then
299 declare
300 CFB : constant Uint := Component_Bit_Offset (Comp);
301 CSZ : constant Uint := Esize (Comp);
302 CLC : constant Node_Id := Component_Clause (Comp);
303 Pos : constant Node_Id := Position (CLC);
304 FB : constant Node_Id := First_Bit (CLC);
306 Storage_Unit_Offset : constant Uint :=
307 CFB / System_Storage_Unit;
309 Start_Bit : constant Uint :=
310 CFB mod System_Storage_Unit;
312 begin
313 -- Cases where field goes over storage unit boundary
315 if Start_Bit + CSZ > System_Storage_Unit then
317 -- Allow multi-byte field but generate warning
319 if Start_Bit mod System_Storage_Unit = 0
320 and then CSZ mod System_Storage_Unit = 0
321 then
322 Error_Msg_N
323 ("info: multi-byte field specified with "
324 & "non-standard Bit_Order?V?", CLC);
326 if Bytes_Big_Endian then
327 Error_Msg_N
328 ("\bytes are not reversed "
329 & "(component is big-endian)?V?", CLC);
330 else
331 Error_Msg_N
332 ("\bytes are not reversed "
333 & "(component is little-endian)?V?", CLC);
334 end if;
336 -- Do not allow non-contiguous field
338 else
339 Error_Msg_N
340 ("attempt to specify non-contiguous field "
341 & "not permitted", CLC);
342 Error_Msg_N
343 ("\caused by non-standard Bit_Order "
344 & "specified", CLC);
345 Error_Msg_N
346 ("\consider possibility of using "
347 & "Ada 2005 mode here", CLC);
348 end if;
350 -- Case where field fits in one storage unit
352 else
353 -- Give warning if suspicious component clause
355 if Intval (FB) >= System_Storage_Unit
356 and then Warn_On_Reverse_Bit_Order
357 then
358 Error_Msg_N
359 ("info: Bit_Order clause does not affect " &
360 "byte ordering?V?", Pos);
361 Error_Msg_Uint_1 :=
362 Intval (Pos) + Intval (FB) /
363 System_Storage_Unit;
364 Error_Msg_N
365 ("info: position normalized to ^ before bit " &
366 "order interpreted?V?", Pos);
367 end if;
369 -- Here is where we fix up the Component_Bit_Offset value
370 -- to account for the reverse bit order. Some examples of
371 -- what needs to be done are:
373 -- First_Bit .. Last_Bit Component_Bit_Offset
374 -- old new old new
376 -- 0 .. 0 7 .. 7 0 7
377 -- 0 .. 1 6 .. 7 0 6
378 -- 0 .. 2 5 .. 7 0 5
379 -- 0 .. 7 0 .. 7 0 4
381 -- 1 .. 1 6 .. 6 1 6
382 -- 1 .. 4 3 .. 6 1 3
383 -- 4 .. 7 0 .. 3 4 0
385 -- The rule is that the first bit is is obtained by
386 -- subtracting the old ending bit from storage_unit - 1.
388 Set_Component_Bit_Offset
389 (Comp,
390 (Storage_Unit_Offset * System_Storage_Unit) +
391 (System_Storage_Unit - 1) -
392 (Start_Bit + CSZ - 1));
394 Set_Normalized_First_Bit
395 (Comp,
396 Component_Bit_Offset (Comp) mod
397 System_Storage_Unit);
398 end if;
399 end;
400 end if;
402 Next_Component_Or_Discriminant (Comp);
403 end loop;
405 -- For Ada 2005, we do machine scalar processing, as fully described In
406 -- AI-133. This involves gathering all components which start at the
407 -- same byte offset and processing them together. Same approach is still
408 -- valid in later versions including Ada 2012.
410 else
411 declare
412 Max_Machine_Scalar_Size : constant Uint :=
413 UI_From_Int
414 (Standard_Long_Long_Integer_Size);
415 -- We use this as the maximum machine scalar size
417 Num_CC : Natural;
418 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
420 begin
421 -- This first loop through components does two things. First it
422 -- deals with the case of components with component clauses whose
423 -- length is greater than the maximum machine scalar size (either
424 -- accepting them or rejecting as needed). Second, it counts the
425 -- number of components with component clauses whose length does
426 -- not exceed this maximum for later processing.
428 Num_CC := 0;
429 Comp := First_Component_Or_Discriminant (R);
430 while Present (Comp) loop
431 CC := Component_Clause (Comp);
433 if Present (CC) then
434 declare
435 Fbit : constant Uint := Static_Integer (First_Bit (CC));
436 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
438 begin
439 -- Case of component with last bit >= max machine scalar
441 if Lbit >= Max_Machine_Scalar_Size then
443 -- This is allowed only if first bit is zero, and
444 -- last bit + 1 is a multiple of storage unit size.
446 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
448 -- This is the case to give a warning if enabled
450 if Warn_On_Reverse_Bit_Order then
451 Error_Msg_N
452 ("info: multi-byte field specified with "
453 & " non-standard Bit_Order?V?", CC);
455 if Bytes_Big_Endian then
456 Error_Msg_N
457 ("\bytes are not reversed "
458 & "(component is big-endian)?V?", CC);
459 else
460 Error_Msg_N
461 ("\bytes are not reversed "
462 & "(component is little-endian)?V?", CC);
463 end if;
464 end if;
466 -- Give error message for RM 13.5.1(10) violation
468 else
469 Error_Msg_FE
470 ("machine scalar rules not followed for&",
471 First_Bit (CC), Comp);
473 Error_Msg_Uint_1 := Lbit;
474 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
475 Error_Msg_F
476 ("\last bit (^) exceeds maximum machine "
477 & "scalar size (^)",
478 First_Bit (CC));
480 if (Lbit + 1) mod SSU /= 0 then
481 Error_Msg_Uint_1 := SSU;
482 Error_Msg_F
483 ("\and is not a multiple of Storage_Unit (^) "
484 & "(RM 13.4.1(10))",
485 First_Bit (CC));
487 else
488 Error_Msg_Uint_1 := Fbit;
489 Error_Msg_F
490 ("\and first bit (^) is non-zero "
491 & "(RM 13.4.1(10))",
492 First_Bit (CC));
493 end if;
494 end if;
496 -- OK case of machine scalar related component clause,
497 -- For now, just count them.
499 else
500 Num_CC := Num_CC + 1;
501 end if;
502 end;
503 end if;
505 Next_Component_Or_Discriminant (Comp);
506 end loop;
508 -- We need to sort the component clauses on the basis of the
509 -- Position values in the clause, so we can group clauses with
510 -- the same Position together to determine the relevant machine
511 -- scalar size.
513 Sort_CC : declare
514 Comps : array (0 .. Num_CC) of Entity_Id;
515 -- Array to collect component and discriminant entities. The
516 -- data starts at index 1, the 0'th entry is for the sort
517 -- routine.
519 function CP_Lt (Op1, Op2 : Natural) return Boolean;
520 -- Compare routine for Sort
522 procedure CP_Move (From : Natural; To : Natural);
523 -- Move routine for Sort
525 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
527 Start : Natural;
528 Stop : Natural;
529 -- Start and stop positions in the component list of the set of
530 -- components with the same starting position (that constitute
531 -- components in a single machine scalar).
533 MaxL : Uint;
534 -- Maximum last bit value of any component in this set
536 MSS : Uint;
537 -- Corresponding machine scalar size
539 -----------
540 -- CP_Lt --
541 -----------
543 function CP_Lt (Op1, Op2 : Natural) return Boolean is
544 begin
545 return Position (Component_Clause (Comps (Op1))) <
546 Position (Component_Clause (Comps (Op2)));
547 end CP_Lt;
549 -------------
550 -- CP_Move --
551 -------------
553 procedure CP_Move (From : Natural; To : Natural) is
554 begin
555 Comps (To) := Comps (From);
556 end CP_Move;
558 -- Start of processing for Sort_CC
560 begin
561 -- Collect the machine scalar relevant component clauses
563 Num_CC := 0;
564 Comp := First_Component_Or_Discriminant (R);
565 while Present (Comp) loop
566 declare
567 CC : constant Node_Id := Component_Clause (Comp);
569 begin
570 -- Collect only component clauses whose last bit is less
571 -- than machine scalar size. Any component clause whose
572 -- last bit exceeds this value does not take part in
573 -- machine scalar layout considerations. The test for
574 -- Error_Posted makes sure we exclude component clauses
575 -- for which we already posted an error.
577 if Present (CC)
578 and then not Error_Posted (Last_Bit (CC))
579 and then Static_Integer (Last_Bit (CC)) <
580 Max_Machine_Scalar_Size
581 then
582 Num_CC := Num_CC + 1;
583 Comps (Num_CC) := Comp;
584 end if;
585 end;
587 Next_Component_Or_Discriminant (Comp);
588 end loop;
590 -- Sort by ascending position number
592 Sorting.Sort (Num_CC);
594 -- We now have all the components whose size does not exceed
595 -- the max machine scalar value, sorted by starting position.
596 -- In this loop we gather groups of clauses starting at the
597 -- same position, to process them in accordance with AI-133.
599 Stop := 0;
600 while Stop < Num_CC loop
601 Start := Stop + 1;
602 Stop := Start;
603 MaxL :=
604 Static_Integer
605 (Last_Bit (Component_Clause (Comps (Start))));
606 while Stop < Num_CC loop
607 if Static_Integer
608 (Position (Component_Clause (Comps (Stop + 1)))) =
609 Static_Integer
610 (Position (Component_Clause (Comps (Stop))))
611 then
612 Stop := Stop + 1;
613 MaxL :=
614 UI_Max
615 (MaxL,
616 Static_Integer
617 (Last_Bit
618 (Component_Clause (Comps (Stop)))));
619 else
620 exit;
621 end if;
622 end loop;
624 -- Now we have a group of component clauses from Start to
625 -- Stop whose positions are identical, and MaxL is the
626 -- maximum last bit value of any of these components.
628 -- We need to determine the corresponding machine scalar
629 -- size. This loop assumes that machine scalar sizes are
630 -- even, and that each possible machine scalar has twice
631 -- as many bits as the next smaller one.
633 MSS := Max_Machine_Scalar_Size;
634 while MSS mod 2 = 0
635 and then (MSS / 2) >= SSU
636 and then (MSS / 2) > MaxL
637 loop
638 MSS := MSS / 2;
639 end loop;
641 -- Here is where we fix up the Component_Bit_Offset value
642 -- to account for the reverse bit order. Some examples of
643 -- what needs to be done for the case of a machine scalar
644 -- size of 8 are:
646 -- First_Bit .. Last_Bit Component_Bit_Offset
647 -- old new old new
649 -- 0 .. 0 7 .. 7 0 7
650 -- 0 .. 1 6 .. 7 0 6
651 -- 0 .. 2 5 .. 7 0 5
652 -- 0 .. 7 0 .. 7 0 4
654 -- 1 .. 1 6 .. 6 1 6
655 -- 1 .. 4 3 .. 6 1 3
656 -- 4 .. 7 0 .. 3 4 0
658 -- The rule is that the first bit is obtained by subtracting
659 -- the old ending bit from machine scalar size - 1.
661 for C in Start .. Stop loop
662 declare
663 Comp : constant Entity_Id := Comps (C);
664 CC : constant Node_Id := Component_Clause (Comp);
666 LB : constant Uint := Static_Integer (Last_Bit (CC));
667 NFB : constant Uint := MSS - Uint_1 - LB;
668 NLB : constant Uint := NFB + Esize (Comp) - 1;
669 Pos : constant Uint := Static_Integer (Position (CC));
671 begin
672 if Warn_On_Reverse_Bit_Order then
673 Error_Msg_Uint_1 := MSS;
674 Error_Msg_N
675 ("info: reverse bit order in machine " &
676 "scalar of length^?V?", First_Bit (CC));
677 Error_Msg_Uint_1 := NFB;
678 Error_Msg_Uint_2 := NLB;
680 if Bytes_Big_Endian then
681 Error_Msg_NE
682 ("\big-endian range for component "
683 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
684 else
685 Error_Msg_NE
686 ("\little-endian range for component"
687 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
688 end if;
689 end if;
691 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
692 Set_Normalized_First_Bit (Comp, NFB mod SSU);
693 end;
694 end loop;
695 end loop;
696 end Sort_CC;
697 end;
698 end if;
699 end Adjust_Record_For_Reverse_Bit_Order;
701 -------------------------------------
702 -- Alignment_Check_For_Size_Change --
703 -------------------------------------
705 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
706 begin
707 -- If the alignment is known, and not set by a rep clause, and is
708 -- inconsistent with the size being set, then reset it to unknown,
709 -- we assume in this case that the size overrides the inherited
710 -- alignment, and that the alignment must be recomputed.
712 if Known_Alignment (Typ)
713 and then not Has_Alignment_Clause (Typ)
714 and then Size mod (Alignment (Typ) * SSU) /= 0
715 then
716 Init_Alignment (Typ);
717 end if;
718 end Alignment_Check_For_Size_Change;
720 -------------------------------------
721 -- Analyze_Aspects_At_Freeze_Point --
722 -------------------------------------
724 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
725 ASN : Node_Id;
726 A_Id : Aspect_Id;
727 Ritem : Node_Id;
729 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
730 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
731 -- the aspect specification node ASN.
733 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
734 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
735 -- a derived type can inherit aspects from its parent which have been
736 -- specified at the time of the derivation using an aspect, as in:
738 -- type A is range 1 .. 10
739 -- with Size => Not_Defined_Yet;
740 -- ..
741 -- type B is new A;
742 -- ..
743 -- Not_Defined_Yet : constant := 64;
745 -- In this example, the Size of A is considered to be specified prior
746 -- to the derivation, and thus inherited, even though the value is not
747 -- known at the time of derivation. To deal with this, we use two entity
748 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
749 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
750 -- the derived type (B here). If this flag is set when the derived type
751 -- is frozen, then this procedure is called to ensure proper inheritance
752 -- of all delayed aspects from the parent type. The derived type is E,
753 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
754 -- aspect specification node in the Rep_Item chain for the parent type.
756 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
757 -- Given an aspect specification node ASN whose expression is an
758 -- optional Boolean, this routines creates the corresponding pragma
759 -- at the freezing point.
761 ----------------------------------
762 -- Analyze_Aspect_Default_Value --
763 ----------------------------------
765 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
766 Ent : constant Entity_Id := Entity (ASN);
767 Expr : constant Node_Id := Expression (ASN);
768 Id : constant Node_Id := Identifier (ASN);
770 begin
771 Error_Msg_Name_1 := Chars (Id);
773 if not Is_Type (Ent) then
774 Error_Msg_N ("aspect% can only apply to a type", Id);
775 return;
777 elsif not Is_First_Subtype (Ent) then
778 Error_Msg_N ("aspect% cannot apply to subtype", Id);
779 return;
781 elsif A_Id = Aspect_Default_Value
782 and then not Is_Scalar_Type (Ent)
783 then
784 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
785 return;
787 elsif A_Id = Aspect_Default_Component_Value then
788 if not Is_Array_Type (Ent) then
789 Error_Msg_N ("aspect% can only be applied to array type", Id);
790 return;
792 elsif not Is_Scalar_Type (Component_Type (Ent)) then
793 Error_Msg_N ("aspect% requires scalar components", Id);
794 return;
795 end if;
796 end if;
798 Set_Has_Default_Aspect (Base_Type (Ent));
800 if Is_Scalar_Type (Ent) then
801 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
802 else
803 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
804 end if;
805 end Analyze_Aspect_Default_Value;
807 ---------------------------------
808 -- Inherit_Delayed_Rep_Aspects --
809 ---------------------------------
811 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
812 P : constant Entity_Id := Entity (ASN);
813 -- Entithy for parent type
815 N : Node_Id;
816 -- Item from Rep_Item chain
818 A : Aspect_Id;
820 begin
821 -- Loop through delayed aspects for the parent type
823 N := ASN;
824 while Present (N) loop
825 if Nkind (N) = N_Aspect_Specification then
826 exit when Entity (N) /= P;
828 if Is_Delayed_Aspect (N) then
829 A := Get_Aspect_Id (Chars (Identifier (N)));
831 -- Process delayed rep aspect. For Boolean attributes it is
832 -- not possible to cancel an attribute once set (the attempt
833 -- to use an aspect with xxx => False is an error) for a
834 -- derived type. So for those cases, we do not have to check
835 -- if a clause has been given for the derived type, since it
836 -- is harmless to set it again if it is already set.
838 case A is
840 -- Alignment
842 when Aspect_Alignment =>
843 if not Has_Alignment_Clause (E) then
844 Set_Alignment (E, Alignment (P));
845 end if;
847 -- Atomic
849 when Aspect_Atomic =>
850 if Is_Atomic (P) then
851 Set_Is_Atomic (E);
852 end if;
854 -- Atomic_Components
856 when Aspect_Atomic_Components =>
857 if Has_Atomic_Components (P) then
858 Set_Has_Atomic_Components (Base_Type (E));
859 end if;
861 -- Bit_Order
863 when Aspect_Bit_Order =>
864 if Is_Record_Type (E)
865 and then No (Get_Attribute_Definition_Clause
866 (E, Attribute_Bit_Order))
867 and then Reverse_Bit_Order (P)
868 then
869 Set_Reverse_Bit_Order (Base_Type (E));
870 end if;
872 -- Component_Size
874 when Aspect_Component_Size =>
875 if Is_Array_Type (E)
876 and then not Has_Component_Size_Clause (E)
877 then
878 Set_Component_Size
879 (Base_Type (E), Component_Size (P));
880 end if;
882 -- Machine_Radix
884 when Aspect_Machine_Radix =>
885 if Is_Decimal_Fixed_Point_Type (E)
886 and then not Has_Machine_Radix_Clause (E)
887 then
888 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
889 end if;
891 -- Object_Size (also Size which also sets Object_Size)
893 when Aspect_Object_Size | Aspect_Size =>
894 if not Has_Size_Clause (E)
895 and then
896 No (Get_Attribute_Definition_Clause
897 (E, Attribute_Object_Size))
898 then
899 Set_Esize (E, Esize (P));
900 end if;
902 -- Pack
904 when Aspect_Pack =>
905 if not Is_Packed (E) then
906 Set_Is_Packed (Base_Type (E));
908 if Is_Bit_Packed_Array (P) then
909 Set_Is_Bit_Packed_Array (Base_Type (E));
910 Set_Packed_Array_Impl_Type
911 (E, Packed_Array_Impl_Type (P));
912 end if;
913 end if;
915 -- Scalar_Storage_Order
917 when Aspect_Scalar_Storage_Order =>
918 if (Is_Record_Type (E) or else Is_Array_Type (E))
919 and then No (Get_Attribute_Definition_Clause
920 (E, Attribute_Scalar_Storage_Order))
921 and then Reverse_Storage_Order (P)
922 then
923 Set_Reverse_Storage_Order (Base_Type (E));
925 -- Clear default SSO indications, since the aspect
926 -- overrides the default.
928 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
929 Set_SSO_Set_High_By_Default (Base_Type (E), False);
930 end if;
932 -- Small
934 when Aspect_Small =>
935 if Is_Fixed_Point_Type (E)
936 and then not Has_Small_Clause (E)
937 then
938 Set_Small_Value (E, Small_Value (P));
939 end if;
941 -- Storage_Size
943 when Aspect_Storage_Size =>
944 if (Is_Access_Type (E) or else Is_Task_Type (E))
945 and then not Has_Storage_Size_Clause (E)
946 then
947 Set_Storage_Size_Variable
948 (Base_Type (E), Storage_Size_Variable (P));
949 end if;
951 -- Value_Size
953 when Aspect_Value_Size =>
955 -- Value_Size is never inherited, it is either set by
956 -- default, or it is explicitly set for the derived
957 -- type. So nothing to do here.
959 null;
961 -- Volatile
963 when Aspect_Volatile =>
964 if Is_Volatile (P) then
965 Set_Is_Volatile (E);
966 end if;
968 -- Volatile_Components
970 when Aspect_Volatile_Components =>
971 if Has_Volatile_Components (P) then
972 Set_Has_Volatile_Components (Base_Type (E));
973 end if;
975 -- That should be all the Rep Aspects
977 when others =>
978 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
979 null;
981 end case;
982 end if;
983 end if;
985 N := Next_Rep_Item (N);
986 end loop;
987 end Inherit_Delayed_Rep_Aspects;
989 -------------------------------------
990 -- Make_Pragma_From_Boolean_Aspect --
991 -------------------------------------
993 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
994 Ident : constant Node_Id := Identifier (ASN);
995 A_Name : constant Name_Id := Chars (Ident);
996 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
997 Ent : constant Entity_Id := Entity (ASN);
998 Expr : constant Node_Id := Expression (ASN);
999 Loc : constant Source_Ptr := Sloc (ASN);
1001 Prag : Node_Id;
1003 procedure Check_False_Aspect_For_Derived_Type;
1004 -- This procedure checks for the case of a false aspect for a derived
1005 -- type, which improperly tries to cancel an aspect inherited from
1006 -- the parent.
1008 -----------------------------------------
1009 -- Check_False_Aspect_For_Derived_Type --
1010 -----------------------------------------
1012 procedure Check_False_Aspect_For_Derived_Type is
1013 Par : Node_Id;
1015 begin
1016 -- We are only checking derived types
1018 if not Is_Derived_Type (E) then
1019 return;
1020 end if;
1022 Par := Nearest_Ancestor (E);
1024 case A_Id is
1025 when Aspect_Atomic | Aspect_Shared =>
1026 if not Is_Atomic (Par) then
1027 return;
1028 end if;
1030 when Aspect_Atomic_Components =>
1031 if not Has_Atomic_Components (Par) then
1032 return;
1033 end if;
1035 when Aspect_Discard_Names =>
1036 if not Discard_Names (Par) then
1037 return;
1038 end if;
1040 when Aspect_Pack =>
1041 if not Is_Packed (Par) then
1042 return;
1043 end if;
1045 when Aspect_Unchecked_Union =>
1046 if not Is_Unchecked_Union (Par) then
1047 return;
1048 end if;
1050 when Aspect_Volatile =>
1051 if not Is_Volatile (Par) then
1052 return;
1053 end if;
1055 when Aspect_Volatile_Components =>
1056 if not Has_Volatile_Components (Par) then
1057 return;
1058 end if;
1060 when others =>
1061 return;
1062 end case;
1064 -- Fall through means we are canceling an inherited aspect
1066 Error_Msg_Name_1 := A_Name;
1067 Error_Msg_NE
1068 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1070 end Check_False_Aspect_For_Derived_Type;
1072 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1074 begin
1075 -- Note that we know Expr is present, because for a missing Expr
1076 -- argument, we knew it was True and did not need to delay the
1077 -- evaluation to the freeze point.
1079 if Is_False (Static_Boolean (Expr)) then
1080 Check_False_Aspect_For_Derived_Type;
1082 else
1083 Prag :=
1084 Make_Pragma (Loc,
1085 Pragma_Argument_Associations => New_List (
1086 Make_Pragma_Argument_Association (Sloc (Ident),
1087 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1089 Pragma_Identifier =>
1090 Make_Identifier (Sloc (Ident), Chars (Ident)));
1092 Set_From_Aspect_Specification (Prag, True);
1093 Set_Corresponding_Aspect (Prag, ASN);
1094 Set_Aspect_Rep_Item (ASN, Prag);
1095 Set_Is_Delayed_Aspect (Prag);
1096 Set_Parent (Prag, ASN);
1097 end if;
1098 end Make_Pragma_From_Boolean_Aspect;
1100 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1102 begin
1103 -- Must be visible in current scope
1105 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1106 return;
1107 end if;
1109 -- Look for aspect specification entries for this entity
1111 ASN := First_Rep_Item (E);
1112 while Present (ASN) loop
1113 if Nkind (ASN) = N_Aspect_Specification then
1114 exit when Entity (ASN) /= E;
1116 if Is_Delayed_Aspect (ASN) then
1117 A_Id := Get_Aspect_Id (ASN);
1119 case A_Id is
1121 -- For aspects whose expression is an optional Boolean, make
1122 -- the corresponding pragma at the freeze point.
1124 when Boolean_Aspects |
1125 Library_Unit_Aspects =>
1126 Make_Pragma_From_Boolean_Aspect (ASN);
1128 -- Special handling for aspects that don't correspond to
1129 -- pragmas/attributes.
1131 when Aspect_Default_Value |
1132 Aspect_Default_Component_Value =>
1134 -- Do not inherit aspect for anonymous base type of a
1135 -- scalar or array type, because they apply to the first
1136 -- subtype of the type, and will be processed when that
1137 -- first subtype is frozen.
1139 if Is_Derived_Type (E)
1140 and then not Comes_From_Source (E)
1141 and then E /= First_Subtype (E)
1142 then
1143 null;
1144 else
1145 Analyze_Aspect_Default_Value (ASN);
1146 end if;
1148 -- Ditto for iterator aspects, because the corresponding
1149 -- attributes may not have been analyzed yet.
1151 when Aspect_Constant_Indexing |
1152 Aspect_Variable_Indexing |
1153 Aspect_Default_Iterator |
1154 Aspect_Iterator_Element =>
1155 Analyze (Expression (ASN));
1157 if Etype (Expression (ASN)) = Any_Type then
1158 Error_Msg_NE
1159 ("\aspect must be fully defined before & is frozen",
1160 ASN, E);
1161 end if;
1163 when Aspect_Iterable =>
1164 Validate_Iterable_Aspect (E, ASN);
1166 when others =>
1167 null;
1168 end case;
1170 Ritem := Aspect_Rep_Item (ASN);
1172 if Present (Ritem) then
1173 Analyze (Ritem);
1174 end if;
1175 end if;
1176 end if;
1178 Next_Rep_Item (ASN);
1179 end loop;
1181 -- This is where we inherit delayed rep aspects from our parent. Note
1182 -- that if we fell out of the above loop with ASN non-empty, it means
1183 -- we hit an aspect for an entity other than E, and it must be the
1184 -- type from which we were derived.
1186 if May_Inherit_Delayed_Rep_Aspects (E) then
1187 Inherit_Delayed_Rep_Aspects (ASN);
1188 end if;
1189 end Analyze_Aspects_At_Freeze_Point;
1191 -----------------------------------
1192 -- Analyze_Aspect_Specifications --
1193 -----------------------------------
1195 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1196 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1197 -- Establish linkages between an aspect and its corresponding
1198 -- pragma.
1200 procedure Insert_After_SPARK_Mode
1201 (Prag : Node_Id;
1202 Ins_Nod : Node_Id;
1203 Decls : List_Id);
1204 -- Subsidiary to the analysis of aspects Abstract_State, Ghost,
1205 -- Initializes, Initial_Condition and Refined_State. Insert node Prag
1206 -- before node Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip
1207 -- SPARK_Mode. Decls is the associated declarative list where Prag is to
1208 -- reside.
1210 procedure Insert_Pragma (Prag : Node_Id);
1211 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1212 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1213 -- Insert pragma Prag such that it mimics the placement of a source
1214 -- pragma of the same kind.
1216 -- procedure Proc (Formal : ...) with Global => ...;
1218 -- procedure Proc (Formal : ...);
1219 -- pragma Global (...);
1221 --------------
1222 -- Decorate --
1223 --------------
1225 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1226 begin
1227 Set_Aspect_Rep_Item (Asp, Prag);
1228 Set_Corresponding_Aspect (Prag, Asp);
1229 Set_From_Aspect_Specification (Prag);
1230 Set_Parent (Prag, Asp);
1231 end Decorate;
1233 -----------------------------
1234 -- Insert_After_SPARK_Mode --
1235 -----------------------------
1237 procedure Insert_After_SPARK_Mode
1238 (Prag : Node_Id;
1239 Ins_Nod : Node_Id;
1240 Decls : List_Id)
1242 Decl : Node_Id := Ins_Nod;
1244 begin
1245 -- Skip SPARK_Mode
1247 if Present (Decl)
1248 and then Nkind (Decl) = N_Pragma
1249 and then Pragma_Name (Decl) = Name_SPARK_Mode
1250 then
1251 Decl := Next (Decl);
1252 end if;
1254 if Present (Decl) then
1255 Insert_Before (Decl, Prag);
1257 -- Aitem acts as the last declaration
1259 else
1260 Append_To (Decls, Prag);
1261 end if;
1262 end Insert_After_SPARK_Mode;
1264 -------------------
1265 -- Insert_Pragma --
1266 -------------------
1268 procedure Insert_Pragma (Prag : Node_Id) is
1269 Aux : Node_Id;
1270 Decl : Node_Id;
1272 begin
1273 if Nkind (N) = N_Subprogram_Body then
1274 if Present (Declarations (N)) then
1276 -- Skip other internally generated pragmas from aspects to find
1277 -- the proper insertion point. As a result the order of pragmas
1278 -- is the same as the order of aspects.
1280 -- As precondition pragmas generated from conjuncts in the
1281 -- precondition aspect are presented in reverse order to
1282 -- Insert_Pragma, insert them in the correct order here by not
1283 -- skipping previously inserted precondition pragmas when the
1284 -- current pragma is a precondition.
1286 Decl := First (Declarations (N));
1287 while Present (Decl) loop
1288 if Nkind (Decl) = N_Pragma
1289 and then From_Aspect_Specification (Decl)
1290 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1291 and then
1292 Get_Pragma_Id (Prag) = Pragma_Precondition)
1293 then
1294 Next (Decl);
1295 else
1296 exit;
1297 end if;
1298 end loop;
1300 if Present (Decl) then
1301 Insert_Before (Decl, Prag);
1302 else
1303 Append (Prag, Declarations (N));
1304 end if;
1305 else
1306 Set_Declarations (N, New_List (Prag));
1307 end if;
1309 -- When the context is a library unit, the pragma is added to the
1310 -- Pragmas_After list.
1312 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1313 Aux := Aux_Decls_Node (Parent (N));
1315 if No (Pragmas_After (Aux)) then
1316 Set_Pragmas_After (Aux, New_List);
1317 end if;
1319 Prepend (Prag, Pragmas_After (Aux));
1321 -- Default
1323 else
1324 Insert_After (N, Prag);
1325 end if;
1326 end Insert_Pragma;
1328 -- Local variables
1330 Aspect : Node_Id;
1331 Aitem : Node_Id;
1332 Ent : Node_Id;
1334 L : constant List_Id := Aspect_Specifications (N);
1336 Ins_Node : Node_Id := N;
1337 -- Insert pragmas/attribute definition clause after this node when no
1338 -- delayed analysis is required.
1340 -- Start of processing for Analyze_Aspect_Specifications
1342 -- The general processing involves building an attribute definition
1343 -- clause or a pragma node that corresponds to the aspect. Then in order
1344 -- to delay the evaluation of this aspect to the freeze point, we attach
1345 -- the corresponding pragma/attribute definition clause to the aspect
1346 -- specification node, which is then placed in the Rep Item chain. In
1347 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1348 -- and we evaluate the rep item at the freeze point. When the aspect
1349 -- doesn't have a corresponding pragma/attribute definition clause, then
1350 -- its analysis is simply delayed at the freeze point.
1352 -- Some special cases don't require delay analysis, thus the aspect is
1353 -- analyzed right now.
1355 -- Note that there is a special handling for Pre, Post, Test_Case,
1356 -- Contract_Cases aspects. In these cases, we do not have to worry
1357 -- about delay issues, since the pragmas themselves deal with delay
1358 -- of visibility for the expression analysis. Thus, we just insert
1359 -- the pragma after the node N.
1361 begin
1362 pragma Assert (Present (L));
1364 -- Loop through aspects
1366 Aspect := First (L);
1367 Aspect_Loop : while Present (Aspect) loop
1368 Analyze_One_Aspect : declare
1369 Expr : constant Node_Id := Expression (Aspect);
1370 Id : constant Node_Id := Identifier (Aspect);
1371 Loc : constant Source_Ptr := Sloc (Aspect);
1372 Nam : constant Name_Id := Chars (Id);
1373 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1374 Anod : Node_Id;
1376 Delay_Required : Boolean;
1377 -- Set False if delay is not required
1379 Eloc : Source_Ptr := No_Location;
1380 -- Source location of expression, modified when we split PPC's. It
1381 -- is set below when Expr is present.
1383 procedure Analyze_Aspect_External_Or_Link_Name;
1384 -- Perform analysis of the External_Name or Link_Name aspects
1386 procedure Analyze_Aspect_Implicit_Dereference;
1387 -- Perform analysis of the Implicit_Dereference aspects
1389 procedure Make_Aitem_Pragma
1390 (Pragma_Argument_Associations : List_Id;
1391 Pragma_Name : Name_Id);
1392 -- This is a wrapper for Make_Pragma used for converting aspects
1393 -- to pragmas. It takes care of Sloc (set from Loc) and building
1394 -- the pragma identifier from the given name. In addition the
1395 -- flags Class_Present and Split_PPC are set from the aspect
1396 -- node, as well as Is_Ignored. This routine also sets the
1397 -- From_Aspect_Specification in the resulting pragma node to
1398 -- True, and sets Corresponding_Aspect to point to the aspect.
1399 -- The resulting pragma is assigned to Aitem.
1401 ------------------------------------------
1402 -- Analyze_Aspect_External_Or_Link_Name --
1403 ------------------------------------------
1405 procedure Analyze_Aspect_External_Or_Link_Name is
1406 begin
1407 -- Verify that there is an Import/Export aspect defined for the
1408 -- entity. The processing of that aspect in turn checks that
1409 -- there is a Convention aspect declared. The pragma is
1410 -- constructed when processing the Convention aspect.
1412 declare
1413 A : Node_Id;
1415 begin
1416 A := First (L);
1417 while Present (A) loop
1418 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1419 Name_Import);
1420 Next (A);
1421 end loop;
1423 if No (A) then
1424 Error_Msg_N
1425 ("missing Import/Export for Link/External name",
1426 Aspect);
1427 end if;
1428 end;
1429 end Analyze_Aspect_External_Or_Link_Name;
1431 -----------------------------------------
1432 -- Analyze_Aspect_Implicit_Dereference --
1433 -----------------------------------------
1435 procedure Analyze_Aspect_Implicit_Dereference is
1436 begin
1437 if not Is_Type (E) or else not Has_Discriminants (E) then
1438 Error_Msg_N
1439 ("aspect must apply to a type with discriminants", N);
1441 else
1442 declare
1443 Disc : Entity_Id;
1445 begin
1446 Disc := First_Discriminant (E);
1447 while Present (Disc) loop
1448 if Chars (Expr) = Chars (Disc)
1449 and then Ekind (Etype (Disc)) =
1450 E_Anonymous_Access_Type
1451 then
1452 Set_Has_Implicit_Dereference (E);
1453 Set_Has_Implicit_Dereference (Disc);
1454 return;
1455 end if;
1457 Next_Discriminant (Disc);
1458 end loop;
1460 -- Error if no proper access discriminant.
1462 Error_Msg_NE
1463 ("not an access discriminant of&", Expr, E);
1464 end;
1465 end if;
1466 end Analyze_Aspect_Implicit_Dereference;
1468 -----------------------
1469 -- Make_Aitem_Pragma --
1470 -----------------------
1472 procedure Make_Aitem_Pragma
1473 (Pragma_Argument_Associations : List_Id;
1474 Pragma_Name : Name_Id)
1476 Args : List_Id := Pragma_Argument_Associations;
1478 begin
1479 -- We should never get here if aspect was disabled
1481 pragma Assert (not Is_Disabled (Aspect));
1483 -- Certain aspects allow for an optional name or expression. Do
1484 -- not generate a pragma with empty argument association list.
1486 if No (Args) or else No (Expression (First (Args))) then
1487 Args := No_List;
1488 end if;
1490 -- Build the pragma
1492 Aitem :=
1493 Make_Pragma (Loc,
1494 Pragma_Argument_Associations => Args,
1495 Pragma_Identifier =>
1496 Make_Identifier (Sloc (Id), Pragma_Name),
1497 Class_Present => Class_Present (Aspect),
1498 Split_PPC => Split_PPC (Aspect));
1500 -- Set additional semantic fields
1502 if Is_Ignored (Aspect) then
1503 Set_Is_Ignored (Aitem);
1504 elsif Is_Checked (Aspect) then
1505 Set_Is_Checked (Aitem);
1506 end if;
1508 Set_Corresponding_Aspect (Aitem, Aspect);
1509 Set_From_Aspect_Specification (Aitem, True);
1510 end Make_Aitem_Pragma;
1512 -- Start of processing for Analyze_One_Aspect
1514 begin
1515 -- Skip aspect if already analyzed, to avoid looping in some cases
1517 if Analyzed (Aspect) then
1518 goto Continue;
1519 end if;
1521 -- Skip looking at aspect if it is totally disabled. Just mark it
1522 -- as such for later reference in the tree. This also sets the
1523 -- Is_Ignored and Is_Checked flags appropriately.
1525 Check_Applicable_Policy (Aspect);
1527 if Is_Disabled (Aspect) then
1528 goto Continue;
1529 end if;
1531 -- Set the source location of expression, used in the case of
1532 -- a failed precondition/postcondition or invariant. Note that
1533 -- the source location of the expression is not usually the best
1534 -- choice here. For example, it gets located on the last AND
1535 -- keyword in a chain of boolean expressiond AND'ed together.
1536 -- It is best to put the message on the first character of the
1537 -- assertion, which is the effect of the First_Node call here.
1539 if Present (Expr) then
1540 Eloc := Sloc (First_Node (Expr));
1541 end if;
1543 -- Check restriction No_Implementation_Aspect_Specifications
1545 if Implementation_Defined_Aspect (A_Id) then
1546 Check_Restriction
1547 (No_Implementation_Aspect_Specifications, Aspect);
1548 end if;
1550 -- Check restriction No_Specification_Of_Aspect
1552 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1554 -- Mark aspect analyzed (actual analysis is delayed till later)
1556 Set_Analyzed (Aspect);
1557 Set_Entity (Aspect, E);
1558 Ent := New_Occurrence_Of (E, Sloc (Id));
1560 -- Check for duplicate aspect. Note that the Comes_From_Source
1561 -- test allows duplicate Pre/Post's that we generate internally
1562 -- to escape being flagged here.
1564 if No_Duplicates_Allowed (A_Id) then
1565 Anod := First (L);
1566 while Anod /= Aspect loop
1567 if Comes_From_Source (Aspect)
1568 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1569 then
1570 Error_Msg_Name_1 := Nam;
1571 Error_Msg_Sloc := Sloc (Anod);
1573 -- Case of same aspect specified twice
1575 if Class_Present (Anod) = Class_Present (Aspect) then
1576 if not Class_Present (Anod) then
1577 Error_Msg_NE
1578 ("aspect% for & previously given#",
1579 Id, E);
1580 else
1581 Error_Msg_NE
1582 ("aspect `%''Class` for & previously given#",
1583 Id, E);
1584 end if;
1585 end if;
1586 end if;
1588 Next (Anod);
1589 end loop;
1590 end if;
1592 -- Check some general restrictions on language defined aspects
1594 if not Implementation_Defined_Aspect (A_Id) then
1595 Error_Msg_Name_1 := Nam;
1597 -- Not allowed for renaming declarations
1599 if Nkind (N) in N_Renaming_Declaration then
1600 Error_Msg_N
1601 ("aspect % not allowed for renaming declaration",
1602 Aspect);
1603 end if;
1605 -- Not allowed for formal type declarations
1607 if Nkind (N) = N_Formal_Type_Declaration then
1608 Error_Msg_N
1609 ("aspect % not allowed for formal type declaration",
1610 Aspect);
1611 end if;
1612 end if;
1614 -- Copy expression for later processing by the procedures
1615 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1617 Set_Entity (Id, New_Copy_Tree (Expr));
1619 -- Set Delay_Required as appropriate to aspect
1621 case Aspect_Delay (A_Id) is
1622 when Always_Delay =>
1623 Delay_Required := True;
1625 when Never_Delay =>
1626 Delay_Required := False;
1628 when Rep_Aspect =>
1630 -- If expression has the form of an integer literal, then
1631 -- do not delay, since we know the value cannot change.
1632 -- This optimization catches most rep clause cases.
1634 -- For Boolean aspects, don't delay if no expression
1636 if A_Id in Boolean_Aspects and then No (Expr) then
1637 Delay_Required := False;
1639 -- For non-Boolean aspects, don't delay if integer literal
1641 elsif A_Id not in Boolean_Aspects
1642 and then Present (Expr)
1643 and then Nkind (Expr) = N_Integer_Literal
1644 then
1645 Delay_Required := False;
1647 -- All other cases are delayed
1649 else
1650 Delay_Required := True;
1651 Set_Has_Delayed_Rep_Aspects (E);
1652 end if;
1653 end case;
1655 -- Processing based on specific aspect
1657 case A_Id is
1658 when Aspect_Unimplemented =>
1659 null; -- ??? temp for now
1661 -- No_Aspect should be impossible
1663 when No_Aspect =>
1664 raise Program_Error;
1666 -- Case 1: Aspects corresponding to attribute definition
1667 -- clauses.
1669 when Aspect_Address |
1670 Aspect_Alignment |
1671 Aspect_Bit_Order |
1672 Aspect_Component_Size |
1673 Aspect_Constant_Indexing |
1674 Aspect_Default_Iterator |
1675 Aspect_Dispatching_Domain |
1676 Aspect_External_Tag |
1677 Aspect_Input |
1678 Aspect_Iterable |
1679 Aspect_Iterator_Element |
1680 Aspect_Machine_Radix |
1681 Aspect_Object_Size |
1682 Aspect_Output |
1683 Aspect_Read |
1684 Aspect_Scalar_Storage_Order |
1685 Aspect_Size |
1686 Aspect_Small |
1687 Aspect_Simple_Storage_Pool |
1688 Aspect_Storage_Pool |
1689 Aspect_Stream_Size |
1690 Aspect_Value_Size |
1691 Aspect_Variable_Indexing |
1692 Aspect_Write =>
1694 -- Indexing aspects apply only to tagged type
1696 if (A_Id = Aspect_Constant_Indexing
1697 or else
1698 A_Id = Aspect_Variable_Indexing)
1699 and then not (Is_Type (E)
1700 and then Is_Tagged_Type (E))
1701 then
1702 Error_Msg_N
1703 ("indexing aspect can only apply to a tagged type",
1704 Aspect);
1705 goto Continue;
1706 end if;
1708 -- For the case of aspect Address, we don't consider that we
1709 -- know the entity is never set in the source, since it is
1710 -- is likely aliasing is occurring.
1712 -- Note: one might think that the analysis of the resulting
1713 -- attribute definition clause would take care of that, but
1714 -- that's not the case since it won't be from source.
1716 if A_Id = Aspect_Address then
1717 Set_Never_Set_In_Source (E, False);
1718 end if;
1720 -- Correctness of the profile of a stream operation is
1721 -- verified at the freeze point, but we must detect the
1722 -- illegal specification of this aspect for a subtype now,
1723 -- to prevent malformed rep_item chains.
1725 if A_Id = Aspect_Input or else
1726 A_Id = Aspect_Output or else
1727 A_Id = Aspect_Read or else
1728 A_Id = Aspect_Write
1729 then
1730 if not Is_First_Subtype (E) then
1731 Error_Msg_N
1732 ("local name must be a first subtype", Aspect);
1733 goto Continue;
1735 -- If stream aspect applies to the class-wide type,
1736 -- the generated attribute definition applies to the
1737 -- class-wide type as well.
1739 elsif Class_Present (Aspect) then
1740 Ent :=
1741 Make_Attribute_Reference (Loc,
1742 Prefix => Ent,
1743 Attribute_Name => Name_Class);
1744 end if;
1745 end if;
1747 -- Construct the attribute definition clause
1749 Aitem :=
1750 Make_Attribute_Definition_Clause (Loc,
1751 Name => Ent,
1752 Chars => Chars (Id),
1753 Expression => Relocate_Node (Expr));
1755 -- If the address is specified, then we treat the entity as
1756 -- referenced, to avoid spurious warnings. This is analogous
1757 -- to what is done with an attribute definition clause, but
1758 -- here we don't want to generate a reference because this
1759 -- is the point of definition of the entity.
1761 if A_Id = Aspect_Address then
1762 Set_Referenced (E);
1763 end if;
1765 -- Case 2: Aspects corresponding to pragmas
1767 -- Case 2a: Aspects corresponding to pragmas with two
1768 -- arguments, where the first argument is a local name
1769 -- referring to the entity, and the second argument is the
1770 -- aspect definition expression.
1772 -- Linker_Section/Suppress/Unsuppress
1774 when Aspect_Linker_Section |
1775 Aspect_Suppress |
1776 Aspect_Unsuppress =>
1778 Make_Aitem_Pragma
1779 (Pragma_Argument_Associations => New_List (
1780 Make_Pragma_Argument_Association (Loc,
1781 Expression => New_Occurrence_Of (E, Loc)),
1782 Make_Pragma_Argument_Association (Sloc (Expr),
1783 Expression => Relocate_Node (Expr))),
1784 Pragma_Name => Chars (Id));
1786 -- Synchronization
1788 -- Corresponds to pragma Implemented, construct the pragma
1790 when Aspect_Synchronization =>
1791 Make_Aitem_Pragma
1792 (Pragma_Argument_Associations => New_List (
1793 Make_Pragma_Argument_Association (Loc,
1794 Expression => New_Occurrence_Of (E, Loc)),
1795 Make_Pragma_Argument_Association (Sloc (Expr),
1796 Expression => Relocate_Node (Expr))),
1797 Pragma_Name => Name_Implemented);
1799 -- Attach_Handler
1801 when Aspect_Attach_Handler =>
1802 Make_Aitem_Pragma
1803 (Pragma_Argument_Associations => New_List (
1804 Make_Pragma_Argument_Association (Sloc (Ent),
1805 Expression => Ent),
1806 Make_Pragma_Argument_Association (Sloc (Expr),
1807 Expression => Relocate_Node (Expr))),
1808 Pragma_Name => Name_Attach_Handler);
1810 -- We need to insert this pragma into the tree to get proper
1811 -- processing and to look valid from a placement viewpoint.
1813 Insert_Pragma (Aitem);
1814 goto Continue;
1816 -- Dynamic_Predicate, Predicate, Static_Predicate
1818 when Aspect_Dynamic_Predicate |
1819 Aspect_Predicate |
1820 Aspect_Static_Predicate =>
1822 -- These aspects apply only to subtypes
1824 if not Is_Type (E) then
1825 Error_Msg_N
1826 ("predicate can only be specified for a subtype",
1827 Aspect);
1828 goto Continue;
1830 elsif Is_Incomplete_Type (E) then
1831 Error_Msg_N
1832 ("predicate cannot apply to incomplete view", Aspect);
1833 goto Continue;
1834 end if;
1836 -- Construct the pragma (always a pragma Predicate, with
1837 -- flags recording whether it is static/dynamic). We also
1838 -- set flags recording this in the type itself.
1840 Make_Aitem_Pragma
1841 (Pragma_Argument_Associations => New_List (
1842 Make_Pragma_Argument_Association (Sloc (Ent),
1843 Expression => Ent),
1844 Make_Pragma_Argument_Association (Sloc (Expr),
1845 Expression => Relocate_Node (Expr))),
1846 Pragma_Name => Name_Predicate);
1848 -- Mark type has predicates, and remember what kind of
1849 -- aspect lead to this predicate (we need this to access
1850 -- the right set of check policies later on).
1852 Set_Has_Predicates (E);
1854 if A_Id = Aspect_Dynamic_Predicate then
1855 Set_Has_Dynamic_Predicate_Aspect (E);
1856 elsif A_Id = Aspect_Static_Predicate then
1857 Set_Has_Static_Predicate_Aspect (E);
1858 end if;
1860 -- If the type is private, indicate that its completion
1861 -- has a freeze node, because that is the one that will
1862 -- be visible at freeze time.
1864 if Is_Private_Type (E) and then Present (Full_View (E)) then
1865 Set_Has_Predicates (Full_View (E));
1867 if A_Id = Aspect_Dynamic_Predicate then
1868 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1869 elsif A_Id = Aspect_Static_Predicate then
1870 Set_Has_Static_Predicate_Aspect (Full_View (E));
1871 end if;
1873 Set_Has_Delayed_Aspects (Full_View (E));
1874 Ensure_Freeze_Node (Full_View (E));
1875 end if;
1877 -- Case 2b: Aspects corresponding to pragmas with two
1878 -- arguments, where the second argument is a local name
1879 -- referring to the entity, and the first argument is the
1880 -- aspect definition expression.
1882 -- Convention
1884 when Aspect_Convention =>
1886 -- The aspect may be part of the specification of an import
1887 -- or export pragma. Scan the aspect list to gather the
1888 -- other components, if any. The name of the generated
1889 -- pragma is one of Convention/Import/Export.
1891 declare
1892 Args : constant List_Id := New_List (
1893 Make_Pragma_Argument_Association (Sloc (Expr),
1894 Expression => Relocate_Node (Expr)),
1895 Make_Pragma_Argument_Association (Sloc (Ent),
1896 Expression => Ent));
1898 Imp_Exp_Seen : Boolean := False;
1899 -- Flag set when aspect Import or Export has been seen
1901 Imp_Seen : Boolean := False;
1902 -- Flag set when aspect Import has been seen
1904 Asp : Node_Id;
1905 Asp_Nam : Name_Id;
1906 Extern_Arg : Node_Id;
1907 Link_Arg : Node_Id;
1908 Prag_Nam : Name_Id;
1910 begin
1911 Extern_Arg := Empty;
1912 Link_Arg := Empty;
1913 Prag_Nam := Chars (Id);
1915 Asp := First (L);
1916 while Present (Asp) loop
1917 Asp_Nam := Chars (Identifier (Asp));
1919 -- Aspects Import and Export take precedence over
1920 -- aspect Convention. As a result the generated pragma
1921 -- must carry the proper interfacing aspect's name.
1923 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1924 if Imp_Exp_Seen then
1925 Error_Msg_N ("conflicting", Asp);
1926 else
1927 Imp_Exp_Seen := True;
1929 if Asp_Nam = Name_Import then
1930 Imp_Seen := True;
1931 end if;
1932 end if;
1934 Prag_Nam := Asp_Nam;
1936 -- Aspect External_Name adds an extra argument to the
1937 -- generated pragma.
1939 elsif Asp_Nam = Name_External_Name then
1940 Extern_Arg :=
1941 Make_Pragma_Argument_Association (Loc,
1942 Chars => Asp_Nam,
1943 Expression => Relocate_Node (Expression (Asp)));
1945 -- Aspect Link_Name adds an extra argument to the
1946 -- generated pragma.
1948 elsif Asp_Nam = Name_Link_Name then
1949 Link_Arg :=
1950 Make_Pragma_Argument_Association (Loc,
1951 Chars => Asp_Nam,
1952 Expression => Relocate_Node (Expression (Asp)));
1953 end if;
1955 Next (Asp);
1956 end loop;
1958 -- Assemble the full argument list
1960 if Present (Extern_Arg) then
1961 Append_To (Args, Extern_Arg);
1962 end if;
1964 if Present (Link_Arg) then
1965 Append_To (Args, Link_Arg);
1966 end if;
1968 Make_Aitem_Pragma
1969 (Pragma_Argument_Associations => Args,
1970 Pragma_Name => Prag_Nam);
1972 -- Store the generated pragma Import in the related
1973 -- subprogram.
1975 if Imp_Seen and then Is_Subprogram (E) then
1976 Set_Import_Pragma (E, Aitem);
1977 end if;
1978 end;
1980 -- CPU, Interrupt_Priority, Priority
1982 -- These three aspects can be specified for a subprogram spec
1983 -- or body, in which case we analyze the expression and export
1984 -- the value of the aspect.
1986 -- Previously, we generated an equivalent pragma for bodies
1987 -- (note that the specs cannot contain these pragmas). The
1988 -- pragma was inserted ahead of local declarations, rather than
1989 -- after the body. This leads to a certain duplication between
1990 -- the processing performed for the aspect and the pragma, but
1991 -- given the straightforward handling required it is simpler
1992 -- to duplicate than to translate the aspect in the spec into
1993 -- a pragma in the declarative part of the body.
1995 when Aspect_CPU |
1996 Aspect_Interrupt_Priority |
1997 Aspect_Priority =>
1999 if Nkind_In (N, N_Subprogram_Body,
2000 N_Subprogram_Declaration)
2001 then
2002 -- Analyze the aspect expression
2004 Analyze_And_Resolve (Expr, Standard_Integer);
2006 -- Interrupt_Priority aspect not allowed for main
2007 -- subprograms. ARM D.1 does not forbid this explicitly,
2008 -- but ARM J.15.11 (6/3) does not permit pragma
2009 -- Interrupt_Priority for subprograms.
2011 if A_Id = Aspect_Interrupt_Priority then
2012 Error_Msg_N
2013 ("Interrupt_Priority aspect cannot apply to "
2014 & "subprogram", Expr);
2016 -- The expression must be static
2018 elsif not Is_OK_Static_Expression (Expr) then
2019 Flag_Non_Static_Expr
2020 ("aspect requires static expression!", Expr);
2022 -- Check whether this is the main subprogram. Issue a
2023 -- warning only if it is obviously not a main program
2024 -- (when it has parameters or when the subprogram is
2025 -- within a package).
2027 elsif Present (Parameter_Specifications
2028 (Specification (N)))
2029 or else not Is_Compilation_Unit (Defining_Entity (N))
2030 then
2031 -- See ARM D.1 (14/3) and D.16 (12/3)
2033 Error_Msg_N
2034 ("aspect applied to subprogram other than the "
2035 & "main subprogram has no effect??", Expr);
2037 -- Otherwise check in range and export the value
2039 -- For the CPU aspect
2041 elsif A_Id = Aspect_CPU then
2042 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2044 -- Value is correct so we export the value to make
2045 -- it available at execution time.
2047 Set_Main_CPU
2048 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2050 else
2051 Error_Msg_N
2052 ("main subprogram CPU is out of range", Expr);
2053 end if;
2055 -- For the Priority aspect
2057 elsif A_Id = Aspect_Priority then
2058 if Is_In_Range (Expr, RTE (RE_Priority)) then
2060 -- Value is correct so we export the value to make
2061 -- it available at execution time.
2063 Set_Main_Priority
2064 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2066 -- Ignore pragma if Relaxed_RM_Semantics to support
2067 -- other targets/non GNAT compilers.
2069 elsif not Relaxed_RM_Semantics then
2070 Error_Msg_N
2071 ("main subprogram priority is out of range",
2072 Expr);
2073 end if;
2074 end if;
2076 -- Load an arbitrary entity from System.Tasking.Stages
2077 -- or System.Tasking.Restricted.Stages (depending on
2078 -- the supported profile) to make sure that one of these
2079 -- packages is implicitly with'ed, since we need to have
2080 -- the tasking run time active for the pragma Priority to
2081 -- have any effect. Previously we with'ed the package
2082 -- System.Tasking, but this package does not trigger the
2083 -- required initialization of the run-time library.
2085 declare
2086 Discard : Entity_Id;
2087 begin
2088 if Restricted_Profile then
2089 Discard := RTE (RE_Activate_Restricted_Tasks);
2090 else
2091 Discard := RTE (RE_Activate_Tasks);
2092 end if;
2093 end;
2095 -- Handling for these Aspects in subprograms is complete
2097 goto Continue;
2099 -- For tasks
2101 else
2102 -- Pass the aspect as an attribute
2104 Aitem :=
2105 Make_Attribute_Definition_Clause (Loc,
2106 Name => Ent,
2107 Chars => Chars (Id),
2108 Expression => Relocate_Node (Expr));
2109 end if;
2111 -- Warnings
2113 when Aspect_Warnings =>
2114 Make_Aitem_Pragma
2115 (Pragma_Argument_Associations => New_List (
2116 Make_Pragma_Argument_Association (Sloc (Expr),
2117 Expression => Relocate_Node (Expr)),
2118 Make_Pragma_Argument_Association (Loc,
2119 Expression => New_Occurrence_Of (E, Loc))),
2120 Pragma_Name => Chars (Id));
2122 -- Case 2c: Aspects corresponding to pragmas with three
2123 -- arguments.
2125 -- Invariant aspects have a first argument that references the
2126 -- entity, a second argument that is the expression and a third
2127 -- argument that is an appropriate message.
2129 -- Invariant, Type_Invariant
2131 when Aspect_Invariant |
2132 Aspect_Type_Invariant =>
2134 -- Analysis of the pragma will verify placement legality:
2135 -- an invariant must apply to a private type, or appear in
2136 -- the private part of a spec and apply to a completion.
2138 Make_Aitem_Pragma
2139 (Pragma_Argument_Associations => New_List (
2140 Make_Pragma_Argument_Association (Sloc (Ent),
2141 Expression => Ent),
2142 Make_Pragma_Argument_Association (Sloc (Expr),
2143 Expression => Relocate_Node (Expr))),
2144 Pragma_Name => Name_Invariant);
2146 -- Add message unless exception messages are suppressed
2148 if not Opt.Exception_Locations_Suppressed then
2149 Append_To (Pragma_Argument_Associations (Aitem),
2150 Make_Pragma_Argument_Association (Eloc,
2151 Chars => Name_Message,
2152 Expression =>
2153 Make_String_Literal (Eloc,
2154 Strval => "failed invariant from "
2155 & Build_Location_String (Eloc))));
2156 end if;
2158 -- For Invariant case, insert immediately after the entity
2159 -- declaration. We do not have to worry about delay issues
2160 -- since the pragma processing takes care of this.
2162 Delay_Required := False;
2164 -- Case 2d : Aspects that correspond to a pragma with one
2165 -- argument.
2167 -- Abstract_State
2169 -- Aspect Abstract_State introduces implicit declarations for
2170 -- all state abstraction entities it defines. To emulate this
2171 -- behavior, insert the pragma at the beginning of the visible
2172 -- declarations of the related package so that it is analyzed
2173 -- immediately.
2175 when Aspect_Abstract_State => Abstract_State : declare
2176 Context : Node_Id := N;
2177 Decl : Node_Id;
2178 Decls : List_Id;
2180 begin
2181 -- When aspect Abstract_State appears on a generic package,
2182 -- it is propageted to the package instance. The context in
2183 -- this case is the instance spec.
2185 if Nkind (Context) = N_Package_Instantiation then
2186 Context := Instance_Spec (Context);
2187 end if;
2189 if Nkind_In (Context, N_Generic_Package_Declaration,
2190 N_Package_Declaration)
2191 then
2192 Make_Aitem_Pragma
2193 (Pragma_Argument_Associations => New_List (
2194 Make_Pragma_Argument_Association (Loc,
2195 Expression => Relocate_Node (Expr))),
2196 Pragma_Name => Name_Abstract_State);
2197 Decorate (Aspect, Aitem);
2199 Decls := Visible_Declarations (Specification (Context));
2201 -- In general pragma Abstract_State must be at the top
2202 -- of the existing visible declarations to emulate its
2203 -- source counterpart. The only exception to this is a
2204 -- generic instance in which case the pragma must be
2205 -- inserted after the association renamings.
2207 if Present (Decls) then
2208 Decl := First (Decls);
2210 -- The visible declarations of a generic instance have
2211 -- the following structure:
2213 -- <renamings of generic formals>
2214 -- <renamings of internally-generated spec and body>
2215 -- <first source declaration>
2217 -- The pragma must be inserted before the first source
2218 -- declaration, skip the instance "header".
2220 if Is_Generic_Instance (Defining_Entity (Context)) then
2221 while Present (Decl)
2222 and then not Comes_From_Source (Decl)
2223 loop
2224 Decl := Next (Decl);
2225 end loop;
2226 end if;
2228 -- When aspects Abstract_State, Ghost,
2229 -- Initial_Condition and Initializes are out of order,
2230 -- ensure that pragma SPARK_Mode is always at the top
2231 -- of the declarations to properly enabled/suppress
2232 -- errors.
2234 Insert_After_SPARK_Mode
2235 (Prag => Aitem,
2236 Ins_Nod => Decl,
2237 Decls => Decls);
2239 -- Otherwise the pragma forms a new declarative list
2241 else
2242 Set_Visible_Declarations
2243 (Specification (Context), New_List (Aitem));
2244 end if;
2246 else
2247 Error_Msg_NE
2248 ("aspect & must apply to a package declaration",
2249 Aspect, Id);
2250 end if;
2252 goto Continue;
2253 end Abstract_State;
2255 -- Aspect Default_Internal_Condition is never delayed because
2256 -- it is equivalent to a source pragma which appears after the
2257 -- related private type. To deal with forward references, the
2258 -- generated pragma is stored in the rep chain of the related
2259 -- private type as types do not carry contracts. The pragma is
2260 -- wrapped inside of a procedure at the freeze point of the
2261 -- private type's full view.
2263 when Aspect_Default_Initial_Condition =>
2264 Make_Aitem_Pragma
2265 (Pragma_Argument_Associations => New_List (
2266 Make_Pragma_Argument_Association (Loc,
2267 Expression => Relocate_Node (Expr))),
2268 Pragma_Name =>
2269 Name_Default_Initial_Condition);
2271 Decorate (Aspect, Aitem);
2272 Insert_Pragma (Aitem);
2273 goto Continue;
2275 -- Default_Storage_Pool
2277 when Aspect_Default_Storage_Pool =>
2278 Make_Aitem_Pragma
2279 (Pragma_Argument_Associations => New_List (
2280 Make_Pragma_Argument_Association (Loc,
2281 Expression => Relocate_Node (Expr))),
2282 Pragma_Name =>
2283 Name_Default_Storage_Pool);
2285 Decorate (Aspect, Aitem);
2286 Insert_Pragma (Aitem);
2287 goto Continue;
2289 -- Depends
2291 -- Aspect Depends is never delayed because it is equivalent to
2292 -- a source pragma which appears after the related subprogram.
2293 -- To deal with forward references, the generated pragma is
2294 -- stored in the contract of the related subprogram and later
2295 -- analyzed at the end of the declarative region. See routine
2296 -- Analyze_Depends_In_Decl_Part for details.
2298 when Aspect_Depends =>
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_Depends);
2305 Decorate (Aspect, Aitem);
2306 Insert_Pragma (Aitem);
2307 goto Continue;
2309 -- Aspect Extensions_Visible is never delayed because it is
2310 -- equivalent to a source pragma which appears after the
2311 -- related subprogram.
2313 when Aspect_Extensions_Visible =>
2314 Make_Aitem_Pragma
2315 (Pragma_Argument_Associations => New_List (
2316 Make_Pragma_Argument_Association (Loc,
2317 Expression => Relocate_Node (Expr))),
2318 Pragma_Name => Name_Extensions_Visible);
2320 Decorate (Aspect, Aitem);
2321 Insert_Pragma (Aitem);
2322 goto Continue;
2324 -- Aspect Ghost is never delayed because it is equivalent to a
2325 -- source pragma which appears at the top of [generic] package
2326 -- declarations or after an object, a [generic] subprogram, or
2327 -- a type declaration.
2329 when Aspect_Ghost => Ghost : declare
2330 Decls : List_Id;
2332 begin
2333 Make_Aitem_Pragma
2334 (Pragma_Argument_Associations => New_List (
2335 Make_Pragma_Argument_Association (Loc,
2336 Expression => Relocate_Node (Expr))),
2337 Pragma_Name => Name_Ghost);
2339 Decorate (Aspect, Aitem);
2341 -- When the aspect applies to a [generic] package, insert
2342 -- the pragma at the top of the visible declarations. This
2343 -- emulates the placement of a source pragma.
2345 if Nkind_In (N, N_Generic_Package_Declaration,
2346 N_Package_Declaration)
2347 then
2348 Decls := Visible_Declarations (Specification (N));
2350 if No (Decls) then
2351 Decls := New_List;
2352 Set_Visible_Declarations (N, Decls);
2353 end if;
2355 -- When aspects Abstract_State, Ghost, Initial_Condition
2356 -- and Initializes are out of order, ensure that pragma
2357 -- SPARK_Mode is always at the top of the declarations to
2358 -- properly enabled/suppress errors.
2360 Insert_After_SPARK_Mode
2361 (Prag => Aitem,
2362 Ins_Nod => First (Decls),
2363 Decls => Decls);
2365 -- Otherwise the context is an object, [generic] subprogram
2366 -- or type declaration.
2368 else
2369 Insert_Pragma (Aitem);
2370 end if;
2372 goto Continue;
2373 end Ghost;
2375 -- Global
2377 -- Aspect Global is never delayed because it is equivalent to
2378 -- a source pragma which appears after the related subprogram.
2379 -- To deal with forward references, the generated pragma is
2380 -- stored in the contract of the related subprogram and later
2381 -- analyzed at the end of the declarative region. See routine
2382 -- Analyze_Global_In_Decl_Part for details.
2384 when Aspect_Global =>
2385 Make_Aitem_Pragma
2386 (Pragma_Argument_Associations => New_List (
2387 Make_Pragma_Argument_Association (Loc,
2388 Expression => Relocate_Node (Expr))),
2389 Pragma_Name => Name_Global);
2391 Decorate (Aspect, Aitem);
2392 Insert_Pragma (Aitem);
2393 goto Continue;
2395 -- Initial_Condition
2397 -- Aspect Initial_Condition is never delayed because it is
2398 -- equivalent to a source pragma which appears after the
2399 -- related package. To deal with forward references, the
2400 -- generated pragma is stored in the contract of the related
2401 -- package and later analyzed at the end of the declarative
2402 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2403 -- for details.
2405 when Aspect_Initial_Condition => Initial_Condition : declare
2406 Context : Node_Id := N;
2407 Decls : List_Id;
2409 begin
2410 -- When aspect Initial_Condition appears on a generic
2411 -- package, it is propageted to the package instance. The
2412 -- context in this case is the instance spec.
2414 if Nkind (Context) = N_Package_Instantiation then
2415 Context := Instance_Spec (Context);
2416 end if;
2418 if Nkind_In (Context, N_Generic_Package_Declaration,
2419 N_Package_Declaration)
2420 then
2421 Decls := Visible_Declarations (Specification (Context));
2423 Make_Aitem_Pragma
2424 (Pragma_Argument_Associations => New_List (
2425 Make_Pragma_Argument_Association (Loc,
2426 Expression => Relocate_Node (Expr))),
2427 Pragma_Name =>
2428 Name_Initial_Condition);
2429 Decorate (Aspect, Aitem);
2431 if No (Decls) then
2432 Decls := New_List;
2433 Set_Visible_Declarations (Context, Decls);
2434 end if;
2436 -- When aspects Abstract_State, Ghost, Initial_Condition
2437 -- and Initializes are out of order, ensure that pragma
2438 -- SPARK_Mode is always at the top of the declarations to
2439 -- properly enabled/suppress errors.
2441 Insert_After_SPARK_Mode
2442 (Prag => Aitem,
2443 Ins_Nod => First (Decls),
2444 Decls => Decls);
2446 else
2447 Error_Msg_NE
2448 ("aspect & must apply to a package declaration",
2449 Aspect, Id);
2450 end if;
2452 goto Continue;
2453 end Initial_Condition;
2455 -- Initializes
2457 -- Aspect Initializes is never delayed because it is equivalent
2458 -- to a source pragma appearing after the related package. To
2459 -- deal with forward references, the generated pragma is stored
2460 -- in the contract of the related package and later analyzed at
2461 -- the end of the declarative region. For details, see routine
2462 -- Analyze_Initializes_In_Decl_Part.
2464 when Aspect_Initializes => Initializes : declare
2465 Context : Node_Id := N;
2466 Decls : List_Id;
2468 begin
2469 -- When aspect Initializes appears on a generic package,
2470 -- it is propageted to the package instance. The context
2471 -- in this case is the instance spec.
2473 if Nkind (Context) = N_Package_Instantiation then
2474 Context := Instance_Spec (Context);
2475 end if;
2477 if Nkind_In (Context, N_Generic_Package_Declaration,
2478 N_Package_Declaration)
2479 then
2480 Decls := Visible_Declarations (Specification (Context));
2482 Make_Aitem_Pragma
2483 (Pragma_Argument_Associations => New_List (
2484 Make_Pragma_Argument_Association (Loc,
2485 Expression => Relocate_Node (Expr))),
2486 Pragma_Name => Name_Initializes);
2487 Decorate (Aspect, Aitem);
2489 if No (Decls) then
2490 Decls := New_List;
2491 Set_Visible_Declarations (Context, Decls);
2492 end if;
2494 -- When aspects Abstract_State, Ghost, Initial_Condition
2495 -- and Initializes are out of order, ensure that pragma
2496 -- SPARK_Mode is always at the top of the declarations to
2497 -- properly enabled/suppress errors.
2499 Insert_After_SPARK_Mode
2500 (Prag => Aitem,
2501 Ins_Nod => First (Decls),
2502 Decls => Decls);
2504 else
2505 Error_Msg_NE
2506 ("aspect & must apply to a package declaration",
2507 Aspect, Id);
2508 end if;
2510 goto Continue;
2511 end Initializes;
2513 -- Obsolescent
2515 when Aspect_Obsolescent => declare
2516 Args : List_Id;
2518 begin
2519 if No (Expr) then
2520 Args := No_List;
2521 else
2522 Args := New_List (
2523 Make_Pragma_Argument_Association (Sloc (Expr),
2524 Expression => Relocate_Node (Expr)));
2525 end if;
2527 Make_Aitem_Pragma
2528 (Pragma_Argument_Associations => Args,
2529 Pragma_Name => Chars (Id));
2530 end;
2532 -- Part_Of
2534 when Aspect_Part_Of =>
2535 if Nkind_In (N, N_Object_Declaration,
2536 N_Package_Instantiation)
2537 then
2538 Make_Aitem_Pragma
2539 (Pragma_Argument_Associations => New_List (
2540 Make_Pragma_Argument_Association (Loc,
2541 Expression => Relocate_Node (Expr))),
2542 Pragma_Name => Name_Part_Of);
2544 else
2545 Error_Msg_NE
2546 ("aspect & must apply to a variable or package "
2547 & "instantiation", Aspect, Id);
2548 end if;
2550 -- SPARK_Mode
2552 when Aspect_SPARK_Mode => SPARK_Mode : declare
2553 Decls : List_Id;
2555 begin
2556 Make_Aitem_Pragma
2557 (Pragma_Argument_Associations => New_List (
2558 Make_Pragma_Argument_Association (Loc,
2559 Expression => Relocate_Node (Expr))),
2560 Pragma_Name => Name_SPARK_Mode);
2562 -- When the aspect appears on a package or a subprogram
2563 -- body, insert the generated pragma at the top of the body
2564 -- declarations to emulate the behavior of a source pragma.
2566 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2567 Decorate (Aspect, Aitem);
2569 Decls := Declarations (N);
2571 if No (Decls) then
2572 Decls := New_List;
2573 Set_Declarations (N, Decls);
2574 end if;
2576 Prepend_To (Decls, Aitem);
2577 goto Continue;
2579 -- When the aspect is associated with a [generic] package
2580 -- declaration, insert the generated pragma at the top of
2581 -- the visible declarations to emulate the behavior of a
2582 -- source pragma.
2584 elsif Nkind_In (N, N_Generic_Package_Declaration,
2585 N_Package_Declaration)
2586 then
2587 Decorate (Aspect, Aitem);
2589 Decls := Visible_Declarations (Specification (N));
2591 if No (Decls) then
2592 Decls := New_List;
2593 Set_Visible_Declarations (Specification (N), Decls);
2594 end if;
2596 Prepend_To (Decls, Aitem);
2597 goto Continue;
2598 end if;
2599 end SPARK_Mode;
2601 -- Refined_Depends
2603 -- Aspect Refined_Depends is never delayed because it is
2604 -- equivalent to a source pragma which appears in the
2605 -- declarations of the related subprogram body. To deal with
2606 -- forward references, the generated pragma is stored in the
2607 -- contract of the related subprogram body and later analyzed
2608 -- at the end of the declarative region. For details, see
2609 -- routine Analyze_Refined_Depends_In_Decl_Part.
2611 when Aspect_Refined_Depends =>
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_Depends);
2618 Decorate (Aspect, Aitem);
2619 Insert_Pragma (Aitem);
2620 goto Continue;
2622 -- Refined_Global
2624 -- Aspect Refined_Global is never delayed because it is
2625 -- equivalent to a source pragma which appears in the
2626 -- declarations of the related subprogram body. To deal with
2627 -- forward references, the generated pragma is stored in the
2628 -- contract of the related subprogram body and later analyzed
2629 -- at the end of the declarative region. For details, see
2630 -- routine Analyze_Refined_Global_In_Decl_Part.
2632 when Aspect_Refined_Global =>
2633 Make_Aitem_Pragma
2634 (Pragma_Argument_Associations => New_List (
2635 Make_Pragma_Argument_Association (Loc,
2636 Expression => Relocate_Node (Expr))),
2637 Pragma_Name => Name_Refined_Global);
2639 Decorate (Aspect, Aitem);
2640 Insert_Pragma (Aitem);
2641 goto Continue;
2643 -- Refined_Post
2645 when Aspect_Refined_Post =>
2646 Make_Aitem_Pragma
2647 (Pragma_Argument_Associations => New_List (
2648 Make_Pragma_Argument_Association (Loc,
2649 Expression => Relocate_Node (Expr))),
2650 Pragma_Name => Name_Refined_Post);
2652 -- Refined_State
2654 when Aspect_Refined_State => Refined_State : declare
2655 Decls : List_Id;
2657 begin
2658 -- The corresponding pragma for Refined_State is inserted in
2659 -- the declarations of the related package body. This action
2660 -- synchronizes both the source and from-aspect versions of
2661 -- the pragma.
2663 if Nkind (N) = N_Package_Body then
2664 Decls := Declarations (N);
2666 Make_Aitem_Pragma
2667 (Pragma_Argument_Associations => New_List (
2668 Make_Pragma_Argument_Association (Loc,
2669 Expression => Relocate_Node (Expr))),
2670 Pragma_Name => Name_Refined_State);
2671 Decorate (Aspect, Aitem);
2673 if No (Decls) then
2674 Decls := New_List;
2675 Set_Declarations (N, Decls);
2676 end if;
2678 -- Pragma Refined_State must be inserted after pragma
2679 -- SPARK_Mode in the tree. This ensures that any error
2680 -- messages dependent on SPARK_Mode will be properly
2681 -- enabled/suppressed.
2683 Insert_After_SPARK_Mode
2684 (Prag => Aitem,
2685 Ins_Nod => First (Decls),
2686 Decls => Decls);
2688 else
2689 Error_Msg_NE
2690 ("aspect & must apply to a package body", Aspect, Id);
2691 end if;
2693 goto Continue;
2694 end Refined_State;
2696 -- Relative_Deadline
2698 when Aspect_Relative_Deadline =>
2699 Make_Aitem_Pragma
2700 (Pragma_Argument_Associations => New_List (
2701 Make_Pragma_Argument_Association (Loc,
2702 Expression => Relocate_Node (Expr))),
2703 Pragma_Name => Name_Relative_Deadline);
2705 -- If the aspect applies to a task, the corresponding pragma
2706 -- must appear within its declarations, not after.
2708 if Nkind (N) = N_Task_Type_Declaration then
2709 declare
2710 Def : Node_Id;
2711 V : List_Id;
2713 begin
2714 if No (Task_Definition (N)) then
2715 Set_Task_Definition (N,
2716 Make_Task_Definition (Loc,
2717 Visible_Declarations => New_List,
2718 End_Label => Empty));
2719 end if;
2721 Def := Task_Definition (N);
2722 V := Visible_Declarations (Def);
2723 if not Is_Empty_List (V) then
2724 Insert_Before (First (V), Aitem);
2726 else
2727 Set_Visible_Declarations (Def, New_List (Aitem));
2728 end if;
2730 goto Continue;
2731 end;
2732 end if;
2734 -- Case 2e: Annotate aspect
2736 when Aspect_Annotate =>
2737 declare
2738 Args : List_Id;
2739 Pargs : List_Id;
2740 Arg : Node_Id;
2742 begin
2743 -- The argument can be a single identifier
2745 if Nkind (Expr) = N_Identifier then
2747 -- One level of parens is allowed
2749 if Paren_Count (Expr) > 1 then
2750 Error_Msg_F ("extra parentheses ignored", Expr);
2751 end if;
2753 Set_Paren_Count (Expr, 0);
2755 -- Add the single item to the list
2757 Args := New_List (Expr);
2759 -- Otherwise we must have an aggregate
2761 elsif Nkind (Expr) = N_Aggregate then
2763 -- Must be positional
2765 if Present (Component_Associations (Expr)) then
2766 Error_Msg_F
2767 ("purely positional aggregate required", Expr);
2768 goto Continue;
2769 end if;
2771 -- Must not be parenthesized
2773 if Paren_Count (Expr) /= 0 then
2774 Error_Msg_F ("extra parentheses ignored", Expr);
2775 end if;
2777 -- List of arguments is list of aggregate expressions
2779 Args := Expressions (Expr);
2781 -- Anything else is illegal
2783 else
2784 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2785 goto Continue;
2786 end if;
2788 -- Prepare pragma arguments
2790 Pargs := New_List;
2791 Arg := First (Args);
2792 while Present (Arg) loop
2793 Append_To (Pargs,
2794 Make_Pragma_Argument_Association (Sloc (Arg),
2795 Expression => Relocate_Node (Arg)));
2796 Next (Arg);
2797 end loop;
2799 Append_To (Pargs,
2800 Make_Pragma_Argument_Association (Sloc (Ent),
2801 Chars => Name_Entity,
2802 Expression => Ent));
2804 Make_Aitem_Pragma
2805 (Pragma_Argument_Associations => Pargs,
2806 Pragma_Name => Name_Annotate);
2807 end;
2809 -- Case 3 : Aspects that don't correspond to pragma/attribute
2810 -- definition clause.
2812 -- Case 3a: The aspects listed below don't correspond to
2813 -- pragmas/attributes but do require delayed analysis.
2815 -- Default_Value can only apply to a scalar type
2817 when Aspect_Default_Value =>
2818 if not Is_Scalar_Type (E) then
2819 Error_Msg_N
2820 ("aspect Default_Value must apply to a scalar type", N);
2821 end if;
2823 Aitem := Empty;
2825 -- Default_Component_Value can only apply to an array type
2826 -- with scalar components.
2828 when Aspect_Default_Component_Value =>
2829 if not (Is_Array_Type (E)
2830 and then Is_Scalar_Type (Component_Type (E)))
2831 then
2832 Error_Msg_N ("aspect Default_Component_Value can only "
2833 & "apply to an array of scalar components", N);
2834 end if;
2836 Aitem := Empty;
2838 -- Case 3b: The aspects listed below don't correspond to
2839 -- pragmas/attributes and don't need delayed analysis.
2841 -- Implicit_Dereference
2843 -- For Implicit_Dereference, External_Name and Link_Name, only
2844 -- the legality checks are done during the analysis, thus no
2845 -- delay is required.
2847 when Aspect_Implicit_Dereference =>
2848 Analyze_Aspect_Implicit_Dereference;
2849 goto Continue;
2851 -- External_Name, Link_Name
2853 when Aspect_External_Name |
2854 Aspect_Link_Name =>
2855 Analyze_Aspect_External_Or_Link_Name;
2856 goto Continue;
2858 -- Dimension
2860 when Aspect_Dimension =>
2861 Analyze_Aspect_Dimension (N, Id, Expr);
2862 goto Continue;
2864 -- Dimension_System
2866 when Aspect_Dimension_System =>
2867 Analyze_Aspect_Dimension_System (N, Id, Expr);
2868 goto Continue;
2870 -- Case 4: Aspects requiring special handling
2872 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2873 -- pragmas take care of the delay.
2875 -- Pre/Post
2877 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2878 -- with a first argument that is the expression, and a second
2879 -- argument that is an informative message if the test fails.
2880 -- This is inserted right after the declaration, to get the
2881 -- required pragma placement. The processing for the pragmas
2882 -- takes care of the required delay.
2884 when Pre_Post_Aspects => Pre_Post : declare
2885 Pname : Name_Id;
2887 begin
2888 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2889 Pname := Name_Precondition;
2890 else
2891 Pname := Name_Postcondition;
2892 end if;
2894 -- If the expressions is of the form A and then B, then
2895 -- we generate separate Pre/Post aspects for the separate
2896 -- clauses. Since we allow multiple pragmas, there is no
2897 -- problem in allowing multiple Pre/Post aspects internally.
2898 -- These should be treated in reverse order (B first and
2899 -- A second) since they are later inserted just after N in
2900 -- the order they are treated. This way, the pragma for A
2901 -- ends up preceding the pragma for B, which may have an
2902 -- importance for the error raised (either constraint error
2903 -- or precondition error).
2905 -- We do not do this for Pre'Class, since we have to put
2906 -- these conditions together in a complex OR expression.
2908 -- We do not do this in ASIS mode, as ASIS relies on the
2909 -- original node representing the complete expression, when
2910 -- retrieving it through the source aspect table.
2912 if not ASIS_Mode
2913 and then (Pname = Name_Postcondition
2914 or else not Class_Present (Aspect))
2915 then
2916 while Nkind (Expr) = N_And_Then loop
2917 Insert_After (Aspect,
2918 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2919 Identifier => Identifier (Aspect),
2920 Expression => Relocate_Node (Left_Opnd (Expr)),
2921 Class_Present => Class_Present (Aspect),
2922 Split_PPC => True));
2923 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2924 Eloc := Sloc (Expr);
2925 end loop;
2926 end if;
2928 -- Build the precondition/postcondition pragma
2930 -- Add note about why we do NOT need Copy_Tree here???
2932 Make_Aitem_Pragma
2933 (Pragma_Argument_Associations => New_List (
2934 Make_Pragma_Argument_Association (Eloc,
2935 Chars => Name_Check,
2936 Expression => Relocate_Node (Expr))),
2937 Pragma_Name => Pname);
2939 -- Add message unless exception messages are suppressed
2941 if not Opt.Exception_Locations_Suppressed then
2942 Append_To (Pragma_Argument_Associations (Aitem),
2943 Make_Pragma_Argument_Association (Eloc,
2944 Chars => Name_Message,
2945 Expression =>
2946 Make_String_Literal (Eloc,
2947 Strval => "failed "
2948 & Get_Name_String (Pname)
2949 & " from "
2950 & Build_Location_String (Eloc))));
2951 end if;
2953 Set_Is_Delayed_Aspect (Aspect);
2955 -- For Pre/Post cases, insert immediately after the entity
2956 -- declaration, since that is the required pragma placement.
2957 -- Note that for these aspects, we do not have to worry
2958 -- about delay issues, since the pragmas themselves deal
2959 -- with delay of visibility for the expression analysis.
2961 Insert_Pragma (Aitem);
2963 goto Continue;
2964 end Pre_Post;
2966 -- Test_Case
2968 when Aspect_Test_Case => Test_Case : declare
2969 Args : List_Id;
2970 Comp_Expr : Node_Id;
2971 Comp_Assn : Node_Id;
2972 New_Expr : Node_Id;
2974 begin
2975 Args := New_List;
2977 if Nkind (Parent (N)) = N_Compilation_Unit then
2978 Error_Msg_Name_1 := Nam;
2979 Error_Msg_N ("incorrect placement of aspect `%`", E);
2980 goto Continue;
2981 end if;
2983 if Nkind (Expr) /= N_Aggregate then
2984 Error_Msg_Name_1 := Nam;
2985 Error_Msg_NE
2986 ("wrong syntax for aspect `%` for &", Id, E);
2987 goto Continue;
2988 end if;
2990 -- Make pragma expressions refer to the original aspect
2991 -- expressions through the Original_Node link. This is used
2992 -- in semantic analysis for ASIS mode, so that the original
2993 -- expression also gets analyzed.
2995 Comp_Expr := First (Expressions (Expr));
2996 while Present (Comp_Expr) loop
2997 New_Expr := Relocate_Node (Comp_Expr);
2998 Append_To (Args,
2999 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3000 Expression => New_Expr));
3001 Next (Comp_Expr);
3002 end loop;
3004 Comp_Assn := First (Component_Associations (Expr));
3005 while Present (Comp_Assn) loop
3006 if List_Length (Choices (Comp_Assn)) /= 1
3007 or else
3008 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3009 then
3010 Error_Msg_Name_1 := Nam;
3011 Error_Msg_NE
3012 ("wrong syntax for aspect `%` for &", Id, E);
3013 goto Continue;
3014 end if;
3016 Append_To (Args,
3017 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3018 Chars => Chars (First (Choices (Comp_Assn))),
3019 Expression =>
3020 Relocate_Node (Expression (Comp_Assn))));
3021 Next (Comp_Assn);
3022 end loop;
3024 -- Build the test-case pragma
3026 Make_Aitem_Pragma
3027 (Pragma_Argument_Associations => Args,
3028 Pragma_Name => Nam);
3029 end Test_Case;
3031 -- Contract_Cases
3033 when Aspect_Contract_Cases =>
3034 Make_Aitem_Pragma
3035 (Pragma_Argument_Associations => New_List (
3036 Make_Pragma_Argument_Association (Loc,
3037 Expression => Relocate_Node (Expr))),
3038 Pragma_Name => Nam);
3040 Decorate (Aspect, Aitem);
3041 Insert_Pragma (Aitem);
3042 goto Continue;
3044 -- Case 5: Special handling for aspects with an optional
3045 -- boolean argument.
3047 -- In the general case, the corresponding pragma cannot be
3048 -- generated yet because the evaluation of the boolean needs
3049 -- to be delayed till the freeze point.
3051 when Boolean_Aspects |
3052 Library_Unit_Aspects =>
3054 Set_Is_Boolean_Aspect (Aspect);
3056 -- Lock_Free aspect only apply to protected objects
3058 if A_Id = Aspect_Lock_Free then
3059 if Ekind (E) /= E_Protected_Type then
3060 Error_Msg_Name_1 := Nam;
3061 Error_Msg_N
3062 ("aspect % only applies to a protected object",
3063 Aspect);
3065 else
3066 -- Set the Uses_Lock_Free flag to True if there is no
3067 -- expression or if the expression is True. The
3068 -- evaluation of this aspect should be delayed to the
3069 -- freeze point (why???)
3071 if No (Expr)
3072 or else Is_True (Static_Boolean (Expr))
3073 then
3074 Set_Uses_Lock_Free (E);
3075 end if;
3077 Record_Rep_Item (E, Aspect);
3078 end if;
3080 goto Continue;
3082 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
3084 -- For the case of aspects Import and Export, we don't
3085 -- consider that we know the entity is never set in the
3086 -- source, since it is is likely modified outside the
3087 -- program.
3089 -- Note: one might think that the analysis of the
3090 -- resulting pragma would take care of that, but
3091 -- that's not the case since it won't be from source.
3093 if Ekind (E) = E_Variable then
3094 Set_Never_Set_In_Source (E, False);
3095 end if;
3097 -- In older versions of Ada the corresponding pragmas
3098 -- specified a Convention. In Ada 2012 the convention is
3099 -- specified as a separate aspect, and it is optional,
3100 -- given that it defaults to Convention_Ada. The code
3101 -- that verifed that there was a matching convention
3102 -- is now obsolete.
3104 -- Resolve the expression of an Import or Export here,
3105 -- and require it to be of type Boolean and static. This
3106 -- is not quite right, because in general this should be
3107 -- delayed, but that seems tricky for these, because
3108 -- normally Boolean aspects are replaced with pragmas at
3109 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
3110 -- but in the case of these aspects we can't generate
3111 -- a simple pragma with just the entity name. ???
3113 if not Present (Expr)
3114 or else Is_True (Static_Boolean (Expr))
3115 then
3116 if A_Id = Aspect_Import then
3117 Set_Is_Imported (E);
3119 -- An imported entity cannot have an explicit
3120 -- initialization.
3122 if Nkind (N) = N_Object_Declaration
3123 and then Present (Expression (N))
3124 then
3125 Error_Msg_N
3126 ("imported entities cannot be initialized "
3127 & "(RM B.1(24))", Expression (N));
3128 end if;
3130 elsif A_Id = Aspect_Export then
3131 Set_Is_Exported (E);
3132 end if;
3133 end if;
3135 goto Continue;
3136 end if;
3138 -- Library unit aspects require special handling in the case
3139 -- of a package declaration, the pragma needs to be inserted
3140 -- in the list of declarations for the associated package.
3141 -- There is no issue of visibility delay for these aspects.
3143 if A_Id in Library_Unit_Aspects
3144 and then
3145 Nkind_In (N, N_Package_Declaration,
3146 N_Generic_Package_Declaration)
3147 and then Nkind (Parent (N)) /= N_Compilation_Unit
3149 -- Aspect is legal on a local instantiation of a library-
3150 -- level generic unit.
3152 and then not Is_Generic_Instance (Defining_Entity (N))
3153 then
3154 Error_Msg_N
3155 ("incorrect context for library unit aspect&", Id);
3156 goto Continue;
3157 end if;
3159 -- External property aspects are Boolean by nature, but
3160 -- their pragmas must contain two arguments, the second
3161 -- being the optional Boolean expression.
3163 if A_Id = Aspect_Async_Readers or else
3164 A_Id = Aspect_Async_Writers or else
3165 A_Id = Aspect_Effective_Reads or else
3166 A_Id = Aspect_Effective_Writes
3167 then
3168 declare
3169 Args : List_Id;
3171 begin
3172 -- The first argument of the external property pragma
3173 -- is the related object.
3175 Args :=
3176 New_List (
3177 Make_Pragma_Argument_Association (Sloc (Ent),
3178 Expression => Ent));
3180 -- The second argument is the optional Boolean
3181 -- expression which must be propagated even if it
3182 -- evaluates to False as this has special semantic
3183 -- meaning.
3185 if Present (Expr) then
3186 Append_To (Args,
3187 Make_Pragma_Argument_Association (Loc,
3188 Expression => Relocate_Node (Expr)));
3189 end if;
3191 Make_Aitem_Pragma
3192 (Pragma_Argument_Associations => Args,
3193 Pragma_Name => Nam);
3194 end;
3196 -- Cases where we do not delay, includes all cases where the
3197 -- expression is missing other than the above cases.
3199 elsif not Delay_Required or else No (Expr) then
3200 Make_Aitem_Pragma
3201 (Pragma_Argument_Associations => New_List (
3202 Make_Pragma_Argument_Association (Sloc (Ent),
3203 Expression => Ent)),
3204 Pragma_Name => Chars (Id));
3205 Delay_Required := False;
3207 -- In general cases, the corresponding pragma/attribute
3208 -- definition clause will be inserted later at the freezing
3209 -- point, and we do not need to build it now.
3211 else
3212 Aitem := Empty;
3213 end if;
3215 -- Storage_Size
3217 -- This is special because for access types we need to generate
3218 -- an attribute definition clause. This also works for single
3219 -- task declarations, but it does not work for task type
3220 -- declarations, because we have the case where the expression
3221 -- references a discriminant of the task type. That can't use
3222 -- an attribute definition clause because we would not have
3223 -- visibility on the discriminant. For that case we must
3224 -- generate a pragma in the task definition.
3226 when Aspect_Storage_Size =>
3228 -- Task type case
3230 if Ekind (E) = E_Task_Type then
3231 declare
3232 Decl : constant Node_Id := Declaration_Node (E);
3234 begin
3235 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3237 -- If no task definition, create one
3239 if No (Task_Definition (Decl)) then
3240 Set_Task_Definition (Decl,
3241 Make_Task_Definition (Loc,
3242 Visible_Declarations => Empty_List,
3243 End_Label => Empty));
3244 end if;
3246 -- Create a pragma and put it at the start of the task
3247 -- definition for the task type declaration.
3249 Make_Aitem_Pragma
3250 (Pragma_Argument_Associations => New_List (
3251 Make_Pragma_Argument_Association (Loc,
3252 Expression => Relocate_Node (Expr))),
3253 Pragma_Name => Name_Storage_Size);
3255 Prepend
3256 (Aitem,
3257 Visible_Declarations (Task_Definition (Decl)));
3258 goto Continue;
3259 end;
3261 -- All other cases, generate attribute definition
3263 else
3264 Aitem :=
3265 Make_Attribute_Definition_Clause (Loc,
3266 Name => Ent,
3267 Chars => Chars (Id),
3268 Expression => Relocate_Node (Expr));
3269 end if;
3270 end case;
3272 -- Attach the corresponding pragma/attribute definition clause to
3273 -- the aspect specification node.
3275 if Present (Aitem) then
3276 Set_From_Aspect_Specification (Aitem);
3277 end if;
3279 -- In the context of a compilation unit, we directly put the
3280 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3281 -- node (no delay is required here) except for aspects on a
3282 -- subprogram body (see below) and a generic package, for which we
3283 -- need to introduce the pragma before building the generic copy
3284 -- (see sem_ch12), and for package instantiations, where the
3285 -- library unit pragmas are better handled early.
3287 if Nkind (Parent (N)) = N_Compilation_Unit
3288 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3289 then
3290 declare
3291 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3293 begin
3294 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3296 -- For a Boolean aspect, create the corresponding pragma if
3297 -- no expression or if the value is True.
3299 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3300 if Is_True (Static_Boolean (Expr)) then
3301 Make_Aitem_Pragma
3302 (Pragma_Argument_Associations => New_List (
3303 Make_Pragma_Argument_Association (Sloc (Ent),
3304 Expression => Ent)),
3305 Pragma_Name => Chars (Id));
3307 Set_From_Aspect_Specification (Aitem, True);
3308 Set_Corresponding_Aspect (Aitem, Aspect);
3310 else
3311 goto Continue;
3312 end if;
3313 end if;
3315 -- If the aspect is on a subprogram body (relevant aspect
3316 -- is Inline), add the pragma in front of the declarations.
3318 if Nkind (N) = N_Subprogram_Body then
3319 if No (Declarations (N)) then
3320 Set_Declarations (N, New_List);
3321 end if;
3323 Prepend (Aitem, Declarations (N));
3325 elsif Nkind (N) = N_Generic_Package_Declaration then
3326 if No (Visible_Declarations (Specification (N))) then
3327 Set_Visible_Declarations (Specification (N), New_List);
3328 end if;
3330 Prepend (Aitem,
3331 Visible_Declarations (Specification (N)));
3333 elsif Nkind (N) = N_Package_Instantiation then
3334 declare
3335 Spec : constant Node_Id :=
3336 Specification (Instance_Spec (N));
3337 begin
3338 if No (Visible_Declarations (Spec)) then
3339 Set_Visible_Declarations (Spec, New_List);
3340 end if;
3342 Prepend (Aitem, Visible_Declarations (Spec));
3343 end;
3345 else
3346 if No (Pragmas_After (Aux)) then
3347 Set_Pragmas_After (Aux, New_List);
3348 end if;
3350 Append (Aitem, Pragmas_After (Aux));
3351 end if;
3353 goto Continue;
3354 end;
3355 end if;
3357 -- The evaluation of the aspect is delayed to the freezing point.
3358 -- The pragma or attribute clause if there is one is then attached
3359 -- to the aspect specification which is put in the rep item list.
3361 if Delay_Required then
3362 if Present (Aitem) then
3363 Set_Is_Delayed_Aspect (Aitem);
3364 Set_Aspect_Rep_Item (Aspect, Aitem);
3365 Set_Parent (Aitem, Aspect);
3366 end if;
3368 Set_Is_Delayed_Aspect (Aspect);
3370 -- In the case of Default_Value, link the aspect to base type
3371 -- as well, even though it appears on a first subtype. This is
3372 -- mandated by the semantics of the aspect. Do not establish
3373 -- the link when processing the base type itself as this leads
3374 -- to a rep item circularity. Verify that we are dealing with
3375 -- a scalar type to prevent cascaded errors.
3377 if A_Id = Aspect_Default_Value
3378 and then Is_Scalar_Type (E)
3379 and then Base_Type (E) /= E
3380 then
3381 Set_Has_Delayed_Aspects (Base_Type (E));
3382 Record_Rep_Item (Base_Type (E), Aspect);
3383 end if;
3385 Set_Has_Delayed_Aspects (E);
3386 Record_Rep_Item (E, Aspect);
3388 -- When delay is not required and the context is a package or a
3389 -- subprogram body, insert the pragma in the body declarations.
3391 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3392 if No (Declarations (N)) then
3393 Set_Declarations (N, New_List);
3394 end if;
3396 -- The pragma is added before source declarations
3398 Prepend_To (Declarations (N), Aitem);
3400 -- When delay is not required and the context is not a compilation
3401 -- unit, we simply insert the pragma/attribute definition clause
3402 -- in sequence.
3404 else
3405 Insert_After (Ins_Node, Aitem);
3406 Ins_Node := Aitem;
3407 end if;
3408 end Analyze_One_Aspect;
3410 <<Continue>>
3411 Next (Aspect);
3412 end loop Aspect_Loop;
3414 if Has_Delayed_Aspects (E) then
3415 Ensure_Freeze_Node (E);
3416 end if;
3417 end Analyze_Aspect_Specifications;
3419 -----------------------
3420 -- Analyze_At_Clause --
3421 -----------------------
3423 -- An at clause is replaced by the corresponding Address attribute
3424 -- definition clause that is the preferred approach in Ada 95.
3426 procedure Analyze_At_Clause (N : Node_Id) is
3427 CS : constant Boolean := Comes_From_Source (N);
3429 begin
3430 -- This is an obsolescent feature
3432 Check_Restriction (No_Obsolescent_Features, N);
3434 if Warn_On_Obsolescent_Feature then
3435 Error_Msg_N
3436 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3437 Error_Msg_N
3438 ("\?j?use address attribute definition clause instead", N);
3439 end if;
3441 -- Rewrite as address clause
3443 Rewrite (N,
3444 Make_Attribute_Definition_Clause (Sloc (N),
3445 Name => Identifier (N),
3446 Chars => Name_Address,
3447 Expression => Expression (N)));
3449 -- We preserve Comes_From_Source, since logically the clause still comes
3450 -- from the source program even though it is changed in form.
3452 Set_Comes_From_Source (N, CS);
3454 -- Analyze rewritten clause
3456 Analyze_Attribute_Definition_Clause (N);
3457 end Analyze_At_Clause;
3459 -----------------------------------------
3460 -- Analyze_Attribute_Definition_Clause --
3461 -----------------------------------------
3463 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3464 Loc : constant Source_Ptr := Sloc (N);
3465 Nam : constant Node_Id := Name (N);
3466 Attr : constant Name_Id := Chars (N);
3467 Expr : constant Node_Id := Expression (N);
3468 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3470 Ent : Entity_Id;
3471 -- The entity of Nam after it is analyzed. In the case of an incomplete
3472 -- type, this is the underlying type.
3474 U_Ent : Entity_Id;
3475 -- The underlying entity to which the attribute applies. Generally this
3476 -- is the Underlying_Type of Ent, except in the case where the clause
3477 -- applies to full view of incomplete type or private type in which case
3478 -- U_Ent is just a copy of Ent.
3480 FOnly : Boolean := False;
3481 -- Reset to True for subtype specific attribute (Alignment, Size)
3482 -- and for stream attributes, i.e. those cases where in the call to
3483 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3484 -- are checked. Note that the case of stream attributes is not clear
3485 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3486 -- Storage_Size for derived task types, but that is also clearly
3487 -- unintentional.
3489 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3490 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3491 -- definition clauses.
3493 function Duplicate_Clause return Boolean;
3494 -- This routine checks if the aspect for U_Ent being given by attribute
3495 -- definition clause N is for an aspect that has already been specified,
3496 -- and if so gives an error message. If there is a duplicate, True is
3497 -- returned, otherwise if there is no error, False is returned.
3499 procedure Check_Indexing_Functions;
3500 -- Check that the function in Constant_Indexing or Variable_Indexing
3501 -- attribute has the proper type structure. If the name is overloaded,
3502 -- check that some interpretation is legal.
3504 procedure Check_Iterator_Functions;
3505 -- Check that there is a single function in Default_Iterator attribute
3506 -- has the proper type structure.
3508 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3509 -- Common legality check for the previous two
3511 -----------------------------------
3512 -- Analyze_Stream_TSS_Definition --
3513 -----------------------------------
3515 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3516 Subp : Entity_Id := Empty;
3517 I : Interp_Index;
3518 It : Interp;
3519 Pnam : Entity_Id;
3521 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3522 -- True for Read attribute, false for other attributes
3524 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3525 -- Return true if the entity is a subprogram with an appropriate
3526 -- profile for the attribute being defined.
3528 ----------------------
3529 -- Has_Good_Profile --
3530 ----------------------
3532 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3533 F : Entity_Id;
3534 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3535 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3536 (False => E_Procedure, True => E_Function);
3537 Typ : Entity_Id;
3539 begin
3540 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3541 return False;
3542 end if;
3544 F := First_Formal (Subp);
3546 if No (F)
3547 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3548 or else Designated_Type (Etype (F)) /=
3549 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3550 then
3551 return False;
3552 end if;
3554 if not Is_Function then
3555 Next_Formal (F);
3557 declare
3558 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3559 (False => E_In_Parameter,
3560 True => E_Out_Parameter);
3561 begin
3562 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3563 return False;
3564 end if;
3565 end;
3567 Typ := Etype (F);
3569 -- If the attribute specification comes from an aspect
3570 -- specification for a class-wide stream, the parameter must be
3571 -- a class-wide type of the entity to which the aspect applies.
3573 if From_Aspect_Specification (N)
3574 and then Class_Present (Parent (N))
3575 and then Is_Class_Wide_Type (Typ)
3576 then
3577 Typ := Etype (Typ);
3578 end if;
3580 else
3581 Typ := Etype (Subp);
3582 end if;
3584 -- Verify that the prefix of the attribute and the local name for
3585 -- the type of the formal match, or one is the class-wide of the
3586 -- other, in the case of a class-wide stream operation.
3588 if Base_Type (Typ) = Base_Type (Ent)
3589 or else (Is_Class_Wide_Type (Typ)
3590 and then Typ = Class_Wide_Type (Base_Type (Ent)))
3591 or else (Is_Class_Wide_Type (Ent)
3592 and then Ent = Class_Wide_Type (Base_Type (Typ)))
3593 then
3594 null;
3595 else
3596 return False;
3597 end if;
3599 if Present ((Next_Formal (F)))
3600 then
3601 return False;
3603 elsif not Is_Scalar_Type (Typ)
3604 and then not Is_First_Subtype (Typ)
3605 and then not Is_Class_Wide_Type (Typ)
3606 then
3607 return False;
3609 else
3610 return True;
3611 end if;
3612 end Has_Good_Profile;
3614 -- Start of processing for Analyze_Stream_TSS_Definition
3616 begin
3617 FOnly := True;
3619 if not Is_Type (U_Ent) then
3620 Error_Msg_N ("local name must be a subtype", Nam);
3621 return;
3623 elsif not Is_First_Subtype (U_Ent) then
3624 Error_Msg_N ("local name must be a first subtype", Nam);
3625 return;
3626 end if;
3628 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3630 -- If Pnam is present, it can be either inherited from an ancestor
3631 -- type (in which case it is legal to redefine it for this type), or
3632 -- be a previous definition of the attribute for the same type (in
3633 -- which case it is illegal).
3635 -- In the first case, it will have been analyzed already, and we
3636 -- can check that its profile does not match the expected profile
3637 -- for a stream attribute of U_Ent. In the second case, either Pnam
3638 -- has been analyzed (and has the expected profile), or it has not
3639 -- been analyzed yet (case of a type that has not been frozen yet
3640 -- and for which the stream attribute has been set using Set_TSS).
3642 if Present (Pnam)
3643 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3644 then
3645 Error_Msg_Sloc := Sloc (Pnam);
3646 Error_Msg_Name_1 := Attr;
3647 Error_Msg_N ("% attribute already defined #", Nam);
3648 return;
3649 end if;
3651 Analyze (Expr);
3653 if Is_Entity_Name (Expr) then
3654 if not Is_Overloaded (Expr) then
3655 if Has_Good_Profile (Entity (Expr)) then
3656 Subp := Entity (Expr);
3657 end if;
3659 else
3660 Get_First_Interp (Expr, I, It);
3661 while Present (It.Nam) loop
3662 if Has_Good_Profile (It.Nam) then
3663 Subp := It.Nam;
3664 exit;
3665 end if;
3667 Get_Next_Interp (I, It);
3668 end loop;
3669 end if;
3670 end if;
3672 if Present (Subp) then
3673 if Is_Abstract_Subprogram (Subp) then
3674 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3675 return;
3677 -- A stream subprogram for an interface type must be a null
3678 -- procedure (RM 13.13.2 (38/3)).
3680 elsif Is_Interface (U_Ent)
3681 and then not Is_Class_Wide_Type (U_Ent)
3682 and then not Inside_A_Generic
3683 and then
3684 (Ekind (Subp) = E_Function
3685 or else
3686 not Null_Present
3687 (Specification
3688 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
3689 then
3690 Error_Msg_N
3691 ("stream subprogram for interface type "
3692 & "must be null procedure", Expr);
3693 end if;
3695 Set_Entity (Expr, Subp);
3696 Set_Etype (Expr, Etype (Subp));
3698 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3700 else
3701 Error_Msg_Name_1 := Attr;
3702 Error_Msg_N ("incorrect expression for% attribute", Expr);
3703 end if;
3704 end Analyze_Stream_TSS_Definition;
3706 ------------------------------
3707 -- Check_Indexing_Functions --
3708 ------------------------------
3710 procedure Check_Indexing_Functions is
3711 Indexing_Found : Boolean := False;
3713 procedure Check_One_Function (Subp : Entity_Id);
3714 -- Check one possible interpretation. Sets Indexing_Found True if a
3715 -- legal indexing function is found.
3717 procedure Illegal_Indexing (Msg : String);
3718 -- Diagnose illegal indexing function if not overloaded. In the
3719 -- overloaded case indicate that no legal interpretation exists.
3721 ------------------------
3722 -- Check_One_Function --
3723 ------------------------
3725 procedure Check_One_Function (Subp : Entity_Id) is
3726 Default_Element : Node_Id;
3727 Ret_Type : constant Entity_Id := Etype (Subp);
3729 begin
3730 if not Is_Overloadable (Subp) then
3731 Illegal_Indexing ("illegal indexing function for type&");
3732 return;
3734 elsif Scope (Subp) /= Scope (Ent) then
3735 if Nkind (Expr) = N_Expanded_Name then
3737 -- Indexing function can't be declared elsewhere
3739 Illegal_Indexing
3740 ("indexing function must be declared in scope of type&");
3741 end if;
3743 return;
3745 elsif No (First_Formal (Subp)) then
3746 Illegal_Indexing
3747 ("Indexing requires a function that applies to type&");
3748 return;
3750 elsif No (Next_Formal (First_Formal (Subp))) then
3751 Illegal_Indexing
3752 ("indexing function must have at least two parameters");
3753 return;
3755 elsif Is_Derived_Type (Ent) then
3756 if (Attr = Name_Constant_Indexing
3757 and then Present
3758 (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing)))
3759 or else
3760 (Attr = Name_Variable_Indexing
3761 and then Present
3762 (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing)))
3763 then
3764 if Debug_Flag_Dot_XX then
3765 null;
3767 else
3768 Illegal_Indexing
3769 ("indexing function already inherited "
3770 & "from parent type");
3771 return;
3772 end if;
3773 end if;
3774 end if;
3776 if not Check_Primitive_Function (Subp) then
3777 Illegal_Indexing
3778 ("Indexing aspect requires a function that applies to type&");
3779 return;
3780 end if;
3782 -- If partial declaration exists, verify that it is not tagged.
3784 if Ekind (Current_Scope) = E_Package
3785 and then Has_Private_Declaration (Ent)
3786 and then From_Aspect_Specification (N)
3787 and then
3788 List_Containing (Parent (Ent)) =
3789 Private_Declarations
3790 (Specification (Unit_Declaration_Node (Current_Scope)))
3791 and then Nkind (N) = N_Attribute_Definition_Clause
3792 then
3793 declare
3794 Decl : Node_Id;
3796 begin
3797 Decl :=
3798 First (Visible_Declarations
3799 (Specification
3800 (Unit_Declaration_Node (Current_Scope))));
3802 while Present (Decl) loop
3803 if Nkind (Decl) = N_Private_Type_Declaration
3804 and then Ent = Full_View (Defining_Identifier (Decl))
3805 and then Tagged_Present (Decl)
3806 and then No (Aspect_Specifications (Decl))
3807 then
3808 Illegal_Indexing
3809 ("Indexing aspect cannot be specified on full view "
3810 & "if partial view is tagged");
3811 return;
3812 end if;
3814 Next (Decl);
3815 end loop;
3816 end;
3817 end if;
3819 -- An indexing function must return either the default element of
3820 -- the container, or a reference type. For variable indexing it
3821 -- must be the latter.
3823 Default_Element :=
3824 Find_Value_Of_Aspect
3825 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3827 if Present (Default_Element) then
3828 Analyze (Default_Element);
3830 if Is_Entity_Name (Default_Element)
3831 and then not Covers (Entity (Default_Element), Ret_Type)
3832 and then False
3833 then
3834 Illegal_Indexing
3835 ("wrong return type for indexing function");
3836 return;
3837 end if;
3838 end if;
3840 -- For variable_indexing the return type must be a reference type
3842 if Attr = Name_Variable_Indexing then
3843 if not Has_Implicit_Dereference (Ret_Type) then
3844 Illegal_Indexing
3845 ("variable indexing must return a reference type");
3846 return;
3848 elsif Is_Access_Constant
3849 (Etype (First_Discriminant (Ret_Type)))
3850 then
3851 Illegal_Indexing
3852 ("variable indexing must return an access to variable");
3853 return;
3854 end if;
3856 else
3857 if Has_Implicit_Dereference (Ret_Type)
3858 and then not
3859 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3860 then
3861 Illegal_Indexing
3862 ("constant indexing must return an access to constant");
3863 return;
3865 elsif Is_Access_Type (Etype (First_Formal (Subp)))
3866 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
3867 then
3868 Illegal_Indexing
3869 ("constant indexing must apply to an access to constant");
3870 return;
3871 end if;
3872 end if;
3874 -- All checks succeeded.
3876 Indexing_Found := True;
3877 end Check_One_Function;
3879 -----------------------
3880 -- Illegal_Indexing --
3881 -----------------------
3883 procedure Illegal_Indexing (Msg : String) is
3884 begin
3885 Error_Msg_NE (Msg, N, Ent);
3886 end Illegal_Indexing;
3888 -- Start of processing for Check_Indexing_Functions
3890 begin
3891 if In_Instance then
3892 return;
3893 end if;
3895 Analyze (Expr);
3897 if not Is_Overloaded (Expr) then
3898 Check_One_Function (Entity (Expr));
3900 else
3901 declare
3902 I : Interp_Index;
3903 It : Interp;
3905 begin
3906 Indexing_Found := False;
3907 Get_First_Interp (Expr, I, It);
3908 while Present (It.Nam) loop
3910 -- Note that analysis will have added the interpretation
3911 -- that corresponds to the dereference. We only check the
3912 -- subprogram itself.
3914 if Is_Overloadable (It.Nam) then
3915 Check_One_Function (It.Nam);
3916 end if;
3918 Get_Next_Interp (I, It);
3919 end loop;
3920 end;
3921 end if;
3923 if not Indexing_Found and then not Error_Posted (N) then
3924 Error_Msg_NE
3925 ("aspect Indexing requires a local function that "
3926 & "applies to type&", Expr, Ent);
3927 end if;
3928 end Check_Indexing_Functions;
3930 ------------------------------
3931 -- Check_Iterator_Functions --
3932 ------------------------------
3934 procedure Check_Iterator_Functions is
3935 Default : Entity_Id;
3937 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3938 -- Check one possible interpretation for validity
3940 ----------------------------
3941 -- Valid_Default_Iterator --
3942 ----------------------------
3944 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3945 Formal : Entity_Id;
3947 begin
3948 if not Check_Primitive_Function (Subp) then
3949 return False;
3950 else
3951 Formal := First_Formal (Subp);
3952 end if;
3954 -- False if any subsequent formal has no default expression
3956 Formal := Next_Formal (Formal);
3957 while Present (Formal) loop
3958 if No (Expression (Parent (Formal))) then
3959 return False;
3960 end if;
3962 Next_Formal (Formal);
3963 end loop;
3965 -- True if all subsequent formals have default expressions
3967 return True;
3968 end Valid_Default_Iterator;
3970 -- Start of processing for Check_Iterator_Functions
3972 begin
3973 Analyze (Expr);
3975 if not Is_Entity_Name (Expr) then
3976 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3977 end if;
3979 if not Is_Overloaded (Expr) then
3980 if not Check_Primitive_Function (Entity (Expr)) then
3981 Error_Msg_NE
3982 ("aspect Indexing requires a function that applies to type&",
3983 Entity (Expr), Ent);
3984 end if;
3986 if not Valid_Default_Iterator (Entity (Expr)) then
3987 Error_Msg_N ("improper function for default iterator", Expr);
3988 end if;
3990 else
3991 Default := Empty;
3992 declare
3993 I : Interp_Index;
3994 It : Interp;
3996 begin
3997 Get_First_Interp (Expr, I, It);
3998 while Present (It.Nam) loop
3999 if not Check_Primitive_Function (It.Nam)
4000 or else not Valid_Default_Iterator (It.Nam)
4001 then
4002 Remove_Interp (I);
4004 elsif Present (Default) then
4005 Error_Msg_N ("default iterator must be unique", Expr);
4007 else
4008 Default := It.Nam;
4009 end if;
4011 Get_Next_Interp (I, It);
4012 end loop;
4013 end;
4015 if Present (Default) then
4016 Set_Entity (Expr, Default);
4017 Set_Is_Overloaded (Expr, False);
4018 end if;
4019 end if;
4020 end Check_Iterator_Functions;
4022 -------------------------------
4023 -- Check_Primitive_Function --
4024 -------------------------------
4026 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4027 Ctrl : Entity_Id;
4029 begin
4030 if Ekind (Subp) /= E_Function then
4031 return False;
4032 end if;
4034 if No (First_Formal (Subp)) then
4035 return False;
4036 else
4037 Ctrl := Etype (First_Formal (Subp));
4038 end if;
4040 -- Type of formal may be the class-wide type, an access to such,
4041 -- or an incomplete view.
4043 if Ctrl = Ent
4044 or else Ctrl = Class_Wide_Type (Ent)
4045 or else
4046 (Ekind (Ctrl) = E_Anonymous_Access_Type
4047 and then (Designated_Type (Ctrl) = Ent
4048 or else
4049 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4050 or else
4051 (Ekind (Ctrl) = E_Incomplete_Type
4052 and then Full_View (Ctrl) = Ent)
4053 then
4054 null;
4055 else
4056 return False;
4057 end if;
4059 return True;
4060 end Check_Primitive_Function;
4062 ----------------------
4063 -- Duplicate_Clause --
4064 ----------------------
4066 function Duplicate_Clause return Boolean is
4067 A : Node_Id;
4069 begin
4070 -- Nothing to do if this attribute definition clause comes from
4071 -- an aspect specification, since we could not be duplicating an
4072 -- explicit clause, and we dealt with the case of duplicated aspects
4073 -- in Analyze_Aspect_Specifications.
4075 if From_Aspect_Specification (N) then
4076 return False;
4077 end if;
4079 -- Otherwise current clause may duplicate previous clause, or a
4080 -- previously given pragma or aspect specification for the same
4081 -- aspect.
4083 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4085 if Present (A) then
4086 Error_Msg_Name_1 := Chars (N);
4087 Error_Msg_Sloc := Sloc (A);
4089 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4090 return True;
4091 end if;
4093 return False;
4094 end Duplicate_Clause;
4096 -- Start of processing for Analyze_Attribute_Definition_Clause
4098 begin
4099 -- The following code is a defense against recursion. Not clear that
4100 -- this can happen legitimately, but perhaps some error situations can
4101 -- cause it, and we did see this recursion during testing.
4103 if Analyzed (N) then
4104 return;
4105 else
4106 Set_Analyzed (N, True);
4107 end if;
4109 -- Ignore some selected attributes in CodePeer mode since they are not
4110 -- relevant in this context.
4112 if CodePeer_Mode then
4113 case Id is
4115 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4116 -- internal representation of types by implicitly packing them.
4118 when Attribute_Component_Size =>
4119 Rewrite (N, Make_Null_Statement (Sloc (N)));
4120 return;
4122 when others =>
4123 null;
4124 end case;
4125 end if;
4127 -- Process Ignore_Rep_Clauses option
4129 if Ignore_Rep_Clauses then
4130 case Id is
4132 -- The following should be ignored. They do not affect legality
4133 -- and may be target dependent. The basic idea of -gnatI is to
4134 -- ignore any rep clauses that may be target dependent but do not
4135 -- affect legality (except possibly to be rejected because they
4136 -- are incompatible with the compilation target).
4138 when Attribute_Alignment |
4139 Attribute_Bit_Order |
4140 Attribute_Component_Size |
4141 Attribute_Machine_Radix |
4142 Attribute_Object_Size |
4143 Attribute_Size |
4144 Attribute_Small |
4145 Attribute_Stream_Size |
4146 Attribute_Value_Size =>
4147 Kill_Rep_Clause (N);
4148 return;
4150 -- The following should not be ignored, because in the first place
4151 -- they are reasonably portable, and should not cause problems
4152 -- in compiling code from another target, and also they do affect
4153 -- legality, e.g. failing to provide a stream attribute for a type
4154 -- may make a program illegal.
4156 when Attribute_External_Tag |
4157 Attribute_Input |
4158 Attribute_Output |
4159 Attribute_Read |
4160 Attribute_Simple_Storage_Pool |
4161 Attribute_Storage_Pool |
4162 Attribute_Storage_Size |
4163 Attribute_Write =>
4164 null;
4166 -- We do not do anything here with address clauses, they will be
4167 -- removed by Freeze later on, but for now, it works better to
4168 -- keep then in the tree.
4170 when Attribute_Address =>
4171 null;
4173 -- Other cases are errors ("attribute& cannot be set with
4174 -- definition clause"), which will be caught below.
4176 when others =>
4177 null;
4178 end case;
4179 end if;
4181 Analyze (Nam);
4182 Ent := Entity (Nam);
4184 if Rep_Item_Too_Early (Ent, N) then
4185 return;
4186 end if;
4188 -- Rep clause applies to full view of incomplete type or private type if
4189 -- we have one (if not, this is a premature use of the type). However,
4190 -- certain semantic checks need to be done on the specified entity (i.e.
4191 -- the private view), so we save it in Ent.
4193 if Is_Private_Type (Ent)
4194 and then Is_Derived_Type (Ent)
4195 and then not Is_Tagged_Type (Ent)
4196 and then No (Full_View (Ent))
4197 then
4198 -- If this is a private type whose completion is a derivation from
4199 -- another private type, there is no full view, and the attribute
4200 -- belongs to the type itself, not its underlying parent.
4202 U_Ent := Ent;
4204 elsif Ekind (Ent) = E_Incomplete_Type then
4206 -- The attribute applies to the full view, set the entity of the
4207 -- attribute definition accordingly.
4209 Ent := Underlying_Type (Ent);
4210 U_Ent := Ent;
4211 Set_Entity (Nam, Ent);
4213 else
4214 U_Ent := Underlying_Type (Ent);
4215 end if;
4217 -- Avoid cascaded error
4219 if Etype (Nam) = Any_Type then
4220 return;
4222 -- Must be declared in current scope or in case of an aspect
4223 -- specification, must be visible in current scope.
4225 elsif Scope (Ent) /= Current_Scope
4226 and then
4227 not (From_Aspect_Specification (N)
4228 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4229 then
4230 Error_Msg_N ("entity must be declared in this scope", Nam);
4231 return;
4233 -- Must not be a source renaming (we do have some cases where the
4234 -- expander generates a renaming, and those cases are OK, in such
4235 -- cases any attribute applies to the renamed object as well).
4237 elsif Is_Object (Ent)
4238 and then Present (Renamed_Object (Ent))
4239 then
4240 -- Case of renamed object from source, this is an error
4242 if Comes_From_Source (Renamed_Object (Ent)) then
4243 Get_Name_String (Chars (N));
4244 Error_Msg_Strlen := Name_Len;
4245 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4246 Error_Msg_N
4247 ("~ clause not allowed for a renaming declaration "
4248 & "(RM 13.1(6))", Nam);
4249 return;
4251 -- For the case of a compiler generated renaming, the attribute
4252 -- definition clause applies to the renamed object created by the
4253 -- expander. The easiest general way to handle this is to create a
4254 -- copy of the attribute definition clause for this object.
4256 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4257 Insert_Action (N,
4258 Make_Attribute_Definition_Clause (Loc,
4259 Name =>
4260 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4261 Chars => Chars (N),
4262 Expression => Duplicate_Subexpr (Expression (N))));
4264 -- If the renamed object is not an entity, it must be a dereference
4265 -- of an unconstrained function call, and we must introduce a new
4266 -- declaration to capture the expression. This is needed in the case
4267 -- of 'Alignment, where the original declaration must be rewritten.
4269 else
4270 pragma Assert
4271 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4272 null;
4273 end if;
4275 -- If no underlying entity, use entity itself, applies to some
4276 -- previously detected error cases ???
4278 elsif No (U_Ent) then
4279 U_Ent := Ent;
4281 -- Cannot specify for a subtype (exception Object/Value_Size)
4283 elsif Is_Type (U_Ent)
4284 and then not Is_First_Subtype (U_Ent)
4285 and then Id /= Attribute_Object_Size
4286 and then Id /= Attribute_Value_Size
4287 and then not From_At_Mod (N)
4288 then
4289 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4290 return;
4291 end if;
4293 Set_Entity (N, U_Ent);
4294 Check_Restriction_No_Use_Of_Attribute (N);
4296 -- Switch on particular attribute
4298 case Id is
4300 -------------
4301 -- Address --
4302 -------------
4304 -- Address attribute definition clause
4306 when Attribute_Address => Address : begin
4308 -- A little error check, catch for X'Address use X'Address;
4310 if Nkind (Nam) = N_Identifier
4311 and then Nkind (Expr) = N_Attribute_Reference
4312 and then Attribute_Name (Expr) = Name_Address
4313 and then Nkind (Prefix (Expr)) = N_Identifier
4314 and then Chars (Nam) = Chars (Prefix (Expr))
4315 then
4316 Error_Msg_NE
4317 ("address for & is self-referencing", Prefix (Expr), Ent);
4318 return;
4319 end if;
4321 -- Not that special case, carry on with analysis of expression
4323 Analyze_And_Resolve (Expr, RTE (RE_Address));
4325 -- Even when ignoring rep clauses we need to indicate that the
4326 -- entity has an address clause and thus it is legal to declare
4327 -- it imported. Freeze will get rid of the address clause later.
4329 if Ignore_Rep_Clauses then
4330 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4331 Record_Rep_Item (U_Ent, N);
4332 end if;
4334 return;
4335 end if;
4337 if Duplicate_Clause then
4338 null;
4340 -- Case of address clause for subprogram
4342 elsif Is_Subprogram (U_Ent) then
4343 if Has_Homonym (U_Ent) then
4344 Error_Msg_N
4345 ("address clause cannot be given " &
4346 "for overloaded subprogram",
4347 Nam);
4348 return;
4349 end if;
4351 -- For subprograms, all address clauses are permitted, and we
4352 -- mark the subprogram as having a deferred freeze so that Gigi
4353 -- will not elaborate it too soon.
4355 -- Above needs more comments, what is too soon about???
4357 Set_Has_Delayed_Freeze (U_Ent);
4359 -- Case of address clause for entry
4361 elsif Ekind (U_Ent) = E_Entry then
4362 if Nkind (Parent (N)) = N_Task_Body then
4363 Error_Msg_N
4364 ("entry address must be specified in task spec", Nam);
4365 return;
4366 end if;
4368 -- For entries, we require a constant address
4370 Check_Constant_Address_Clause (Expr, U_Ent);
4372 -- Special checks for task types
4374 if Is_Task_Type (Scope (U_Ent))
4375 and then Comes_From_Source (Scope (U_Ent))
4376 then
4377 Error_Msg_N
4378 ("??entry address declared for entry in task type", N);
4379 Error_Msg_N
4380 ("\??only one task can be declared of this type", N);
4381 end if;
4383 -- Entry address clauses are obsolescent
4385 Check_Restriction (No_Obsolescent_Features, N);
4387 if Warn_On_Obsolescent_Feature then
4388 Error_Msg_N
4389 ("?j?attaching interrupt to task entry is an " &
4390 "obsolescent feature (RM J.7.1)", N);
4391 Error_Msg_N
4392 ("\?j?use interrupt procedure instead", N);
4393 end if;
4395 -- Case of an address clause for a controlled object which we
4396 -- consider to be erroneous.
4398 elsif Is_Controlled (Etype (U_Ent))
4399 or else Has_Controlled_Component (Etype (U_Ent))
4400 then
4401 Error_Msg_NE
4402 ("??controlled object& must not be overlaid", Nam, U_Ent);
4403 Error_Msg_N
4404 ("\??Program_Error will be raised at run time", Nam);
4405 Insert_Action (Declaration_Node (U_Ent),
4406 Make_Raise_Program_Error (Loc,
4407 Reason => PE_Overlaid_Controlled_Object));
4408 return;
4410 -- Case of address clause for a (non-controlled) object
4412 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4413 declare
4414 Expr : constant Node_Id := Expression (N);
4415 O_Ent : Entity_Id;
4416 Off : Boolean;
4418 begin
4419 -- Exported variables cannot have an address clause, because
4420 -- this cancels the effect of the pragma Export.
4422 if Is_Exported (U_Ent) then
4423 Error_Msg_N
4424 ("cannot export object with address clause", Nam);
4425 return;
4426 end if;
4428 Find_Overlaid_Entity (N, O_Ent, Off);
4430 -- Overlaying controlled objects is erroneous
4432 if Present (O_Ent)
4433 and then (Has_Controlled_Component (Etype (O_Ent))
4434 or else Is_Controlled (Etype (O_Ent)))
4435 then
4436 Error_Msg_N
4437 ("??cannot overlay with controlled object", Expr);
4438 Error_Msg_N
4439 ("\??Program_Error will be raised at run time", Expr);
4440 Insert_Action (Declaration_Node (U_Ent),
4441 Make_Raise_Program_Error (Loc,
4442 Reason => PE_Overlaid_Controlled_Object));
4443 return;
4445 elsif Present (O_Ent)
4446 and then Ekind (U_Ent) = E_Constant
4447 and then not Is_Constant_Object (O_Ent)
4448 then
4449 Error_Msg_N ("??constant overlays a variable", Expr);
4451 -- Imported variables can have an address clause, but then
4452 -- the import is pretty meaningless except to suppress
4453 -- initializations, so we do not need such variables to
4454 -- be statically allocated (and in fact it causes trouble
4455 -- if the address clause is a local value).
4457 elsif Is_Imported (U_Ent) then
4458 Set_Is_Statically_Allocated (U_Ent, False);
4459 end if;
4461 -- We mark a possible modification of a variable with an
4462 -- address clause, since it is likely aliasing is occurring.
4464 Note_Possible_Modification (Nam, Sure => False);
4466 -- Here we are checking for explicit overlap of one variable
4467 -- by another, and if we find this then mark the overlapped
4468 -- variable as also being volatile to prevent unwanted
4469 -- optimizations. This is a significant pessimization so
4470 -- avoid it when there is an offset, i.e. when the object
4471 -- is composite; they cannot be optimized easily anyway.
4473 if Present (O_Ent)
4474 and then Is_Object (O_Ent)
4475 and then not Off
4477 -- The following test is an expedient solution to what
4478 -- is really a problem in CodePeer. Suppressing the
4479 -- Set_Treat_As_Volatile call here prevents later
4480 -- generation (in some cases) of trees that CodePeer
4481 -- should, but currently does not, handle correctly.
4482 -- This test should probably be removed when CodePeer
4483 -- is improved, just because we want the tree CodePeer
4484 -- analyzes to match the tree for which we generate code
4485 -- as closely as is practical. ???
4487 and then not CodePeer_Mode
4488 then
4489 -- ??? O_Ent might not be in current unit
4491 Set_Treat_As_Volatile (O_Ent);
4492 end if;
4494 -- Legality checks on the address clause for initialized
4495 -- objects is deferred until the freeze point, because
4496 -- a subsequent pragma might indicate that the object
4497 -- is imported and thus not initialized. Also, the address
4498 -- clause might involve entities that have yet to be
4499 -- elaborated.
4501 Set_Has_Delayed_Freeze (U_Ent);
4503 -- If an initialization call has been generated for this
4504 -- object, it needs to be deferred to after the freeze node
4505 -- we have just now added, otherwise GIGI will see a
4506 -- reference to the variable (as actual to the IP call)
4507 -- before its definition.
4509 declare
4510 Init_Call : constant Node_Id :=
4511 Remove_Init_Call (U_Ent, N);
4513 begin
4514 if Present (Init_Call) then
4515 Append_Freeze_Action (U_Ent, Init_Call);
4517 -- Reset Initialization_Statements pointer so that
4518 -- if there is a pragma Import further down, it can
4519 -- clear any default initialization.
4521 Set_Initialization_Statements (U_Ent, Init_Call);
4522 end if;
4523 end;
4525 if Is_Exported (U_Ent) then
4526 Error_Msg_N
4527 ("& cannot be exported if an address clause is given",
4528 Nam);
4529 Error_Msg_N
4530 ("\define and export a variable "
4531 & "that holds its address instead", Nam);
4532 end if;
4534 -- Entity has delayed freeze, so we will generate an
4535 -- alignment check at the freeze point unless suppressed.
4537 if not Range_Checks_Suppressed (U_Ent)
4538 and then not Alignment_Checks_Suppressed (U_Ent)
4539 then
4540 Set_Check_Address_Alignment (N);
4541 end if;
4543 -- Kill the size check code, since we are not allocating
4544 -- the variable, it is somewhere else.
4546 Kill_Size_Check_Code (U_Ent);
4548 -- If the address clause is of the form:
4550 -- for Y'Address use X'Address
4552 -- or
4554 -- Const : constant Address := X'Address;
4555 -- ...
4556 -- for Y'Address use Const;
4558 -- then we make an entry in the table for checking the size
4559 -- and alignment of the overlaying variable. We defer this
4560 -- check till after code generation to take full advantage
4561 -- of the annotation done by the back end.
4563 -- If the entity has a generic type, the check will be
4564 -- performed in the instance if the actual type justifies
4565 -- it, and we do not insert the clause in the table to
4566 -- prevent spurious warnings.
4568 -- Note: we used to test Comes_From_Source and only give
4569 -- this warning for source entities, but we have removed
4570 -- this test. It really seems bogus to generate overlays
4571 -- that would trigger this warning in generated code.
4572 -- Furthermore, by removing the test, we handle the
4573 -- aspect case properly.
4575 if Address_Clause_Overlay_Warnings
4576 and then Present (O_Ent)
4577 and then Is_Object (O_Ent)
4578 then
4579 if not Is_Generic_Type (Etype (U_Ent)) then
4580 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4581 end if;
4583 -- If variable overlays a constant view, and we are
4584 -- warning on overlays, then mark the variable as
4585 -- overlaying a constant (we will give warnings later
4586 -- if this variable is assigned).
4588 if Is_Constant_Object (O_Ent)
4589 and then Ekind (U_Ent) = E_Variable
4590 then
4591 Set_Overlays_Constant (U_Ent);
4592 end if;
4593 end if;
4594 end;
4596 -- Not a valid entity for an address clause
4598 else
4599 Error_Msg_N ("address cannot be given for &", Nam);
4600 end if;
4601 end Address;
4603 ---------------
4604 -- Alignment --
4605 ---------------
4607 -- Alignment attribute definition clause
4609 when Attribute_Alignment => Alignment : declare
4610 Align : constant Uint := Get_Alignment_Value (Expr);
4611 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4613 begin
4614 FOnly := True;
4616 if not Is_Type (U_Ent)
4617 and then Ekind (U_Ent) /= E_Variable
4618 and then Ekind (U_Ent) /= E_Constant
4619 then
4620 Error_Msg_N ("alignment cannot be given for &", Nam);
4622 elsif Duplicate_Clause then
4623 null;
4625 elsif Align /= No_Uint then
4626 Set_Has_Alignment_Clause (U_Ent);
4628 -- Tagged type case, check for attempt to set alignment to a
4629 -- value greater than Max_Align, and reset if so.
4631 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4632 Error_Msg_N
4633 ("alignment for & set to Maximum_Aligment??", Nam);
4634 Set_Alignment (U_Ent, Max_Align);
4636 -- All other cases
4638 else
4639 Set_Alignment (U_Ent, Align);
4640 end if;
4642 -- For an array type, U_Ent is the first subtype. In that case,
4643 -- also set the alignment of the anonymous base type so that
4644 -- other subtypes (such as the itypes for aggregates of the
4645 -- type) also receive the expected alignment.
4647 if Is_Array_Type (U_Ent) then
4648 Set_Alignment (Base_Type (U_Ent), Align);
4649 end if;
4650 end if;
4651 end Alignment;
4653 ---------------
4654 -- Bit_Order --
4655 ---------------
4657 -- Bit_Order attribute definition clause
4659 when Attribute_Bit_Order => Bit_Order : declare
4660 begin
4661 if not Is_Record_Type (U_Ent) then
4662 Error_Msg_N
4663 ("Bit_Order can only be defined for record type", Nam);
4665 elsif Duplicate_Clause then
4666 null;
4668 else
4669 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4671 if Etype (Expr) = Any_Type then
4672 return;
4674 elsif not Is_OK_Static_Expression (Expr) then
4675 Flag_Non_Static_Expr
4676 ("Bit_Order requires static expression!", Expr);
4678 else
4679 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4680 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4681 end if;
4682 end if;
4683 end if;
4684 end Bit_Order;
4686 --------------------
4687 -- Component_Size --
4688 --------------------
4690 -- Component_Size attribute definition clause
4692 when Attribute_Component_Size => Component_Size_Case : declare
4693 Csize : constant Uint := Static_Integer (Expr);
4694 Ctyp : Entity_Id;
4695 Btype : Entity_Id;
4696 Biased : Boolean;
4697 New_Ctyp : Entity_Id;
4698 Decl : Node_Id;
4700 begin
4701 if not Is_Array_Type (U_Ent) then
4702 Error_Msg_N ("component size requires array type", Nam);
4703 return;
4704 end if;
4706 Btype := Base_Type (U_Ent);
4707 Ctyp := Component_Type (Btype);
4709 if Duplicate_Clause then
4710 null;
4712 elsif Rep_Item_Too_Early (Btype, N) then
4713 null;
4715 elsif Csize /= No_Uint then
4716 Check_Size (Expr, Ctyp, Csize, Biased);
4718 -- For the biased case, build a declaration for a subtype that
4719 -- will be used to represent the biased subtype that reflects
4720 -- the biased representation of components. We need the subtype
4721 -- to get proper conversions on referencing elements of the
4722 -- array. Note: component size clauses are ignored in VM mode.
4724 if VM_Target = No_VM then
4725 if Biased then
4726 New_Ctyp :=
4727 Make_Defining_Identifier (Loc,
4728 Chars =>
4729 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4731 Decl :=
4732 Make_Subtype_Declaration (Loc,
4733 Defining_Identifier => New_Ctyp,
4734 Subtype_Indication =>
4735 New_Occurrence_Of (Component_Type (Btype), Loc));
4737 Set_Parent (Decl, N);
4738 Analyze (Decl, Suppress => All_Checks);
4740 Set_Has_Delayed_Freeze (New_Ctyp, False);
4741 Set_Esize (New_Ctyp, Csize);
4742 Set_RM_Size (New_Ctyp, Csize);
4743 Init_Alignment (New_Ctyp);
4744 Set_Is_Itype (New_Ctyp, True);
4745 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4747 Set_Component_Type (Btype, New_Ctyp);
4748 Set_Biased (New_Ctyp, N, "component size clause");
4749 end if;
4751 Set_Component_Size (Btype, Csize);
4753 -- For VM case, we ignore component size clauses
4755 else
4756 -- Give a warning unless we are in GNAT mode, in which case
4757 -- the warning is suppressed since it is not useful.
4759 if not GNAT_Mode then
4760 Error_Msg_N
4761 ("component size ignored in this configuration??", N);
4762 end if;
4763 end if;
4765 -- Deal with warning on overridden size
4767 if Warn_On_Overridden_Size
4768 and then Has_Size_Clause (Ctyp)
4769 and then RM_Size (Ctyp) /= Csize
4770 then
4771 Error_Msg_NE
4772 ("component size overrides size clause for&?S?", N, Ctyp);
4773 end if;
4775 Set_Has_Component_Size_Clause (Btype, True);
4776 Set_Has_Non_Standard_Rep (Btype, True);
4777 end if;
4778 end Component_Size_Case;
4780 -----------------------
4781 -- Constant_Indexing --
4782 -----------------------
4784 when Attribute_Constant_Indexing =>
4785 Check_Indexing_Functions;
4787 ---------
4788 -- CPU --
4789 ---------
4791 when Attribute_CPU => CPU :
4792 begin
4793 -- CPU attribute definition clause not allowed except from aspect
4794 -- specification.
4796 if From_Aspect_Specification (N) then
4797 if not Is_Task_Type (U_Ent) then
4798 Error_Msg_N ("CPU can only be defined for task", Nam);
4800 elsif Duplicate_Clause then
4801 null;
4803 else
4804 -- The expression must be analyzed in the special manner
4805 -- described in "Handling of Default and Per-Object
4806 -- Expressions" in sem.ads.
4808 -- The visibility to the discriminants must be restored
4810 Push_Scope_And_Install_Discriminants (U_Ent);
4811 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4812 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4814 if not Is_OK_Static_Expression (Expr) then
4815 Check_Restriction (Static_Priorities, Expr);
4816 end if;
4817 end if;
4819 else
4820 Error_Msg_N
4821 ("attribute& cannot be set with definition clause", N);
4822 end if;
4823 end CPU;
4825 ----------------------
4826 -- Default_Iterator --
4827 ----------------------
4829 when Attribute_Default_Iterator => Default_Iterator : declare
4830 Func : Entity_Id;
4831 Typ : Entity_Id;
4833 begin
4834 if not Is_Tagged_Type (U_Ent) then
4835 Error_Msg_N
4836 ("aspect Default_Iterator applies to tagged type", Nam);
4837 end if;
4839 Check_Iterator_Functions;
4841 Analyze (Expr);
4843 if not Is_Entity_Name (Expr)
4844 or else Ekind (Entity (Expr)) /= E_Function
4845 then
4846 Error_Msg_N ("aspect Iterator must be a function", Expr);
4847 else
4848 Func := Entity (Expr);
4849 end if;
4851 -- The type of the first parameter must be T, T'class, or a
4852 -- corresponding access type (5.5.1 (8/3)
4854 if No (First_Formal (Func)) then
4855 Typ := Empty;
4856 else
4857 Typ := Etype (First_Formal (Func));
4858 end if;
4860 if Typ = U_Ent
4861 or else Typ = Class_Wide_Type (U_Ent)
4862 or else (Is_Access_Type (Typ)
4863 and then Designated_Type (Typ) = U_Ent)
4864 or else (Is_Access_Type (Typ)
4865 and then Designated_Type (Typ) =
4866 Class_Wide_Type (U_Ent))
4867 then
4868 null;
4870 else
4871 Error_Msg_NE
4872 ("Default Iterator must be a primitive of&", Func, U_Ent);
4873 end if;
4874 end Default_Iterator;
4876 ------------------------
4877 -- Dispatching_Domain --
4878 ------------------------
4880 when Attribute_Dispatching_Domain => Dispatching_Domain :
4881 begin
4882 -- Dispatching_Domain attribute definition clause not allowed
4883 -- except from aspect specification.
4885 if From_Aspect_Specification (N) then
4886 if not Is_Task_Type (U_Ent) then
4887 Error_Msg_N
4888 ("Dispatching_Domain can only be defined for task", Nam);
4890 elsif Duplicate_Clause then
4891 null;
4893 else
4894 -- The expression must be analyzed in the special manner
4895 -- described in "Handling of Default and Per-Object
4896 -- Expressions" in sem.ads.
4898 -- The visibility to the discriminants must be restored
4900 Push_Scope_And_Install_Discriminants (U_Ent);
4902 Preanalyze_Spec_Expression
4903 (Expr, RTE (RE_Dispatching_Domain));
4905 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4906 end if;
4908 else
4909 Error_Msg_N
4910 ("attribute& cannot be set with definition clause", N);
4911 end if;
4912 end Dispatching_Domain;
4914 ------------------
4915 -- External_Tag --
4916 ------------------
4918 when Attribute_External_Tag => External_Tag :
4919 begin
4920 if not Is_Tagged_Type (U_Ent) then
4921 Error_Msg_N ("should be a tagged type", Nam);
4922 end if;
4924 if Duplicate_Clause then
4925 null;
4927 else
4928 Analyze_And_Resolve (Expr, Standard_String);
4930 if not Is_OK_Static_Expression (Expr) then
4931 Flag_Non_Static_Expr
4932 ("static string required for tag name!", Nam);
4933 end if;
4935 if VM_Target /= No_VM then
4936 Error_Msg_Name_1 := Attr;
4937 Error_Msg_N
4938 ("% attribute unsupported in this configuration", Nam);
4939 end if;
4941 if not Is_Library_Level_Entity (U_Ent) then
4942 Error_Msg_NE
4943 ("??non-unique external tag supplied for &", N, U_Ent);
4944 Error_Msg_N
4945 ("\??same external tag applies to all "
4946 & "subprogram calls", N);
4947 Error_Msg_N
4948 ("\??corresponding internal tag cannot be obtained", N);
4949 end if;
4950 end if;
4951 end External_Tag;
4953 --------------------------
4954 -- Implicit_Dereference --
4955 --------------------------
4957 when Attribute_Implicit_Dereference =>
4959 -- Legality checks already performed at the point of the type
4960 -- declaration, aspect is not delayed.
4962 null;
4964 -----------
4965 -- Input --
4966 -----------
4968 when Attribute_Input =>
4969 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4970 Set_Has_Specified_Stream_Input (Ent);
4972 ------------------------
4973 -- Interrupt_Priority --
4974 ------------------------
4976 when Attribute_Interrupt_Priority => Interrupt_Priority :
4977 begin
4978 -- Interrupt_Priority attribute definition clause not allowed
4979 -- except from aspect specification.
4981 if From_Aspect_Specification (N) then
4982 if not Is_Concurrent_Type (U_Ent) then
4983 Error_Msg_N
4984 ("Interrupt_Priority can only be defined for task "
4985 & "and protected object", Nam);
4987 elsif Duplicate_Clause then
4988 null;
4990 else
4991 -- The expression must be analyzed in the special manner
4992 -- described in "Handling of Default and Per-Object
4993 -- Expressions" in sem.ads.
4995 -- The visibility to the discriminants must be restored
4997 Push_Scope_And_Install_Discriminants (U_Ent);
4999 Preanalyze_Spec_Expression
5000 (Expr, RTE (RE_Interrupt_Priority));
5002 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5003 end if;
5005 else
5006 Error_Msg_N
5007 ("attribute& cannot be set with definition clause", N);
5008 end if;
5009 end Interrupt_Priority;
5011 --------------
5012 -- Iterable --
5013 --------------
5015 when Attribute_Iterable =>
5016 Analyze (Expr);
5018 if Nkind (Expr) /= N_Aggregate then
5019 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5020 end if;
5022 declare
5023 Assoc : Node_Id;
5025 begin
5026 Assoc := First (Component_Associations (Expr));
5027 while Present (Assoc) loop
5028 if not Is_Entity_Name (Expression (Assoc)) then
5029 Error_Msg_N ("value must be a function", Assoc);
5030 end if;
5032 Next (Assoc);
5033 end loop;
5034 end;
5036 ----------------------
5037 -- Iterator_Element --
5038 ----------------------
5040 when Attribute_Iterator_Element =>
5041 Analyze (Expr);
5043 if not Is_Entity_Name (Expr)
5044 or else not Is_Type (Entity (Expr))
5045 then
5046 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5047 end if;
5049 -------------------
5050 -- Machine_Radix --
5051 -------------------
5053 -- Machine radix attribute definition clause
5055 when Attribute_Machine_Radix => Machine_Radix : declare
5056 Radix : constant Uint := Static_Integer (Expr);
5058 begin
5059 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5060 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5062 elsif Duplicate_Clause then
5063 null;
5065 elsif Radix /= No_Uint then
5066 Set_Has_Machine_Radix_Clause (U_Ent);
5067 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5069 if Radix = 2 then
5070 null;
5071 elsif Radix = 10 then
5072 Set_Machine_Radix_10 (U_Ent);
5073 else
5074 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5075 end if;
5076 end if;
5077 end Machine_Radix;
5079 -----------------
5080 -- Object_Size --
5081 -----------------
5083 -- Object_Size attribute definition clause
5085 when Attribute_Object_Size => Object_Size : declare
5086 Size : constant Uint := Static_Integer (Expr);
5088 Biased : Boolean;
5089 pragma Warnings (Off, Biased);
5091 begin
5092 if not Is_Type (U_Ent) then
5093 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5095 elsif Duplicate_Clause then
5096 null;
5098 else
5099 Check_Size (Expr, U_Ent, Size, Biased);
5101 if Is_Scalar_Type (U_Ent) then
5102 if Size /= 8 and then Size /= 16 and then Size /= 32
5103 and then UI_Mod (Size, 64) /= 0
5104 then
5105 Error_Msg_N
5106 ("Object_Size must be 8, 16, 32, or multiple of 64",
5107 Expr);
5108 end if;
5110 elsif Size mod 8 /= 0 then
5111 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5112 end if;
5114 Set_Esize (U_Ent, Size);
5115 Set_Has_Object_Size_Clause (U_Ent);
5116 Alignment_Check_For_Size_Change (U_Ent, Size);
5117 end if;
5118 end Object_Size;
5120 ------------
5121 -- Output --
5122 ------------
5124 when Attribute_Output =>
5125 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5126 Set_Has_Specified_Stream_Output (Ent);
5128 --------------
5129 -- Priority --
5130 --------------
5132 when Attribute_Priority => Priority :
5133 begin
5134 -- Priority attribute definition clause not allowed except from
5135 -- aspect specification.
5137 if From_Aspect_Specification (N) then
5138 if not (Is_Concurrent_Type (U_Ent)
5139 or else Ekind (U_Ent) = E_Procedure)
5140 then
5141 Error_Msg_N
5142 ("Priority can only be defined for task and protected "
5143 & "object", Nam);
5145 elsif Duplicate_Clause then
5146 null;
5148 else
5149 -- The expression must be analyzed in the special manner
5150 -- described in "Handling of Default and Per-Object
5151 -- Expressions" in sem.ads.
5153 -- The visibility to the discriminants must be restored
5155 Push_Scope_And_Install_Discriminants (U_Ent);
5156 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5157 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5159 if not Is_OK_Static_Expression (Expr) then
5160 Check_Restriction (Static_Priorities, Expr);
5161 end if;
5162 end if;
5164 else
5165 Error_Msg_N
5166 ("attribute& cannot be set with definition clause", N);
5167 end if;
5168 end Priority;
5170 ----------
5171 -- Read --
5172 ----------
5174 when Attribute_Read =>
5175 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5176 Set_Has_Specified_Stream_Read (Ent);
5178 --------------------------
5179 -- Scalar_Storage_Order --
5180 --------------------------
5182 -- Scalar_Storage_Order attribute definition clause
5184 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5185 begin
5186 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5187 Error_Msg_N
5188 ("Scalar_Storage_Order can only be defined for "
5189 & "record or array type", Nam);
5191 elsif Duplicate_Clause then
5192 null;
5194 else
5195 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5197 if Etype (Expr) = Any_Type then
5198 return;
5200 elsif not Is_OK_Static_Expression (Expr) then
5201 Flag_Non_Static_Expr
5202 ("Scalar_Storage_Order requires static expression!", Expr);
5204 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5206 -- Here for the case of a non-default (i.e. non-confirming)
5207 -- Scalar_Storage_Order attribute definition.
5209 if Support_Nondefault_SSO_On_Target then
5210 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5211 else
5212 Error_Msg_N
5213 ("non-default Scalar_Storage_Order "
5214 & "not supported on target", Expr);
5215 end if;
5216 end if;
5218 -- Clear SSO default indications since explicit setting of the
5219 -- order overrides the defaults.
5221 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5222 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5223 end if;
5224 end Scalar_Storage_Order;
5226 ----------
5227 -- Size --
5228 ----------
5230 -- Size attribute definition clause
5232 when Attribute_Size => Size : declare
5233 Size : constant Uint := Static_Integer (Expr);
5234 Etyp : Entity_Id;
5235 Biased : Boolean;
5237 begin
5238 FOnly := True;
5240 if Duplicate_Clause then
5241 null;
5243 elsif not Is_Type (U_Ent)
5244 and then Ekind (U_Ent) /= E_Variable
5245 and then Ekind (U_Ent) /= E_Constant
5246 then
5247 Error_Msg_N ("size cannot be given for &", Nam);
5249 elsif Is_Array_Type (U_Ent)
5250 and then not Is_Constrained (U_Ent)
5251 then
5252 Error_Msg_N
5253 ("size cannot be given for unconstrained array", Nam);
5255 elsif Size /= No_Uint then
5256 if VM_Target /= No_VM and then not GNAT_Mode then
5258 -- Size clause is not handled properly on VM targets.
5259 -- Display a warning unless we are in GNAT mode, in which
5260 -- case this is useless.
5262 Error_Msg_N
5263 ("size clauses are ignored in this configuration??", N);
5264 end if;
5266 if Is_Type (U_Ent) then
5267 Etyp := U_Ent;
5268 else
5269 Etyp := Etype (U_Ent);
5270 end if;
5272 -- Check size, note that Gigi is in charge of checking that the
5273 -- size of an array or record type is OK. Also we do not check
5274 -- the size in the ordinary fixed-point case, since it is too
5275 -- early to do so (there may be subsequent small clause that
5276 -- affects the size). We can check the size if a small clause
5277 -- has already been given.
5279 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5280 or else Has_Small_Clause (U_Ent)
5281 then
5282 Check_Size (Expr, Etyp, Size, Biased);
5283 Set_Biased (U_Ent, N, "size clause", Biased);
5284 end if;
5286 -- For types set RM_Size and Esize if possible
5288 if Is_Type (U_Ent) then
5289 Set_RM_Size (U_Ent, Size);
5291 -- For elementary types, increase Object_Size to power of 2,
5292 -- but not less than a storage unit in any case (normally
5293 -- this means it will be byte addressable).
5295 -- For all other types, nothing else to do, we leave Esize
5296 -- (object size) unset, the back end will set it from the
5297 -- size and alignment in an appropriate manner.
5299 -- In both cases, we check whether the alignment must be
5300 -- reset in the wake of the size change.
5302 if Is_Elementary_Type (U_Ent) then
5303 if Size <= System_Storage_Unit then
5304 Init_Esize (U_Ent, System_Storage_Unit);
5305 elsif Size <= 16 then
5306 Init_Esize (U_Ent, 16);
5307 elsif Size <= 32 then
5308 Init_Esize (U_Ent, 32);
5309 else
5310 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5311 end if;
5313 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5314 else
5315 Alignment_Check_For_Size_Change (U_Ent, Size);
5316 end if;
5318 -- For objects, set Esize only
5320 else
5321 if Is_Elementary_Type (Etyp) then
5322 if Size /= System_Storage_Unit
5323 and then
5324 Size /= System_Storage_Unit * 2
5325 and then
5326 Size /= System_Storage_Unit * 4
5327 and then
5328 Size /= System_Storage_Unit * 8
5329 then
5330 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5331 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5332 Error_Msg_N
5333 ("size for primitive object must be a power of 2"
5334 & " in the range ^-^", N);
5335 end if;
5336 end if;
5338 Set_Esize (U_Ent, Size);
5339 end if;
5341 Set_Has_Size_Clause (U_Ent);
5342 end if;
5343 end Size;
5345 -----------
5346 -- Small --
5347 -----------
5349 -- Small attribute definition clause
5351 when Attribute_Small => Small : declare
5352 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5353 Small : Ureal;
5355 begin
5356 Analyze_And_Resolve (Expr, Any_Real);
5358 if Etype (Expr) = Any_Type then
5359 return;
5361 elsif not Is_OK_Static_Expression (Expr) then
5362 Flag_Non_Static_Expr
5363 ("small requires static expression!", Expr);
5364 return;
5366 else
5367 Small := Expr_Value_R (Expr);
5369 if Small <= Ureal_0 then
5370 Error_Msg_N ("small value must be greater than zero", Expr);
5371 return;
5372 end if;
5374 end if;
5376 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5377 Error_Msg_N
5378 ("small requires an ordinary fixed point type", Nam);
5380 elsif Has_Small_Clause (U_Ent) then
5381 Error_Msg_N ("small already given for &", Nam);
5383 elsif Small > Delta_Value (U_Ent) then
5384 Error_Msg_N
5385 ("small value must not be greater than delta value", Nam);
5387 else
5388 Set_Small_Value (U_Ent, Small);
5389 Set_Small_Value (Implicit_Base, Small);
5390 Set_Has_Small_Clause (U_Ent);
5391 Set_Has_Small_Clause (Implicit_Base);
5392 Set_Has_Non_Standard_Rep (Implicit_Base);
5393 end if;
5394 end Small;
5396 ------------------
5397 -- Storage_Pool --
5398 ------------------
5400 -- Storage_Pool attribute definition clause
5402 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5403 Pool : Entity_Id;
5404 T : Entity_Id;
5406 begin
5407 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5408 Error_Msg_N
5409 ("storage pool cannot be given for access-to-subprogram type",
5410 Nam);
5411 return;
5413 elsif not
5414 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5415 then
5416 Error_Msg_N
5417 ("storage pool can only be given for access types", Nam);
5418 return;
5420 elsif Is_Derived_Type (U_Ent) then
5421 Error_Msg_N
5422 ("storage pool cannot be given for a derived access type",
5423 Nam);
5425 elsif Duplicate_Clause then
5426 return;
5428 elsif Present (Associated_Storage_Pool (U_Ent)) then
5429 Error_Msg_N ("storage pool already given for &", Nam);
5430 return;
5431 end if;
5433 -- Check for Storage_Size previously given
5435 declare
5436 SS : constant Node_Id :=
5437 Get_Attribute_Definition_Clause
5438 (U_Ent, Attribute_Storage_Size);
5439 begin
5440 if Present (SS) then
5441 Check_Pool_Size_Clash (U_Ent, N, SS);
5442 end if;
5443 end;
5445 -- Storage_Pool case
5447 if Id = Attribute_Storage_Pool then
5448 Analyze_And_Resolve
5449 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5451 -- In the Simple_Storage_Pool case, we allow a variable of any
5452 -- simple storage pool type, so we Resolve without imposing an
5453 -- expected type.
5455 else
5456 Analyze_And_Resolve (Expr);
5458 if not Present (Get_Rep_Pragma
5459 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5460 then
5461 Error_Msg_N
5462 ("expression must be of a simple storage pool type", Expr);
5463 end if;
5464 end if;
5466 if not Denotes_Variable (Expr) then
5467 Error_Msg_N ("storage pool must be a variable", Expr);
5468 return;
5469 end if;
5471 if Nkind (Expr) = N_Type_Conversion then
5472 T := Etype (Expression (Expr));
5473 else
5474 T := Etype (Expr);
5475 end if;
5477 -- The Stack_Bounded_Pool is used internally for implementing
5478 -- access types with a Storage_Size. Since it only work properly
5479 -- when used on one specific type, we need to check that it is not
5480 -- hijacked improperly:
5482 -- type T is access Integer;
5483 -- for T'Storage_Size use n;
5484 -- type Q is access Float;
5485 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5487 if RTE_Available (RE_Stack_Bounded_Pool)
5488 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5489 then
5490 Error_Msg_N ("non-shareable internal Pool", Expr);
5491 return;
5492 end if;
5494 -- If the argument is a name that is not an entity name, then
5495 -- we construct a renaming operation to define an entity of
5496 -- type storage pool.
5498 if not Is_Entity_Name (Expr)
5499 and then Is_Object_Reference (Expr)
5500 then
5501 Pool := Make_Temporary (Loc, 'P', Expr);
5503 declare
5504 Rnode : constant Node_Id :=
5505 Make_Object_Renaming_Declaration (Loc,
5506 Defining_Identifier => Pool,
5507 Subtype_Mark =>
5508 New_Occurrence_Of (Etype (Expr), Loc),
5509 Name => Expr);
5511 begin
5512 -- If the attribute definition clause comes from an aspect
5513 -- clause, then insert the renaming before the associated
5514 -- entity's declaration, since the attribute clause has
5515 -- not yet been appended to the declaration list.
5517 if From_Aspect_Specification (N) then
5518 Insert_Before (Parent (Entity (N)), Rnode);
5519 else
5520 Insert_Before (N, Rnode);
5521 end if;
5523 Analyze (Rnode);
5524 Set_Associated_Storage_Pool (U_Ent, Pool);
5525 end;
5527 elsif Is_Entity_Name (Expr) then
5528 Pool := Entity (Expr);
5530 -- If pool is a renamed object, get original one. This can
5531 -- happen with an explicit renaming, and within instances.
5533 while Present (Renamed_Object (Pool))
5534 and then Is_Entity_Name (Renamed_Object (Pool))
5535 loop
5536 Pool := Entity (Renamed_Object (Pool));
5537 end loop;
5539 if Present (Renamed_Object (Pool))
5540 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5541 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5542 then
5543 Pool := Entity (Expression (Renamed_Object (Pool)));
5544 end if;
5546 Set_Associated_Storage_Pool (U_Ent, Pool);
5548 elsif Nkind (Expr) = N_Type_Conversion
5549 and then Is_Entity_Name (Expression (Expr))
5550 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5551 then
5552 Pool := Entity (Expression (Expr));
5553 Set_Associated_Storage_Pool (U_Ent, Pool);
5555 else
5556 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5557 return;
5558 end if;
5559 end;
5561 ------------------
5562 -- Storage_Size --
5563 ------------------
5565 -- Storage_Size attribute definition clause
5567 when Attribute_Storage_Size => Storage_Size : declare
5568 Btype : constant Entity_Id := Base_Type (U_Ent);
5570 begin
5571 if Is_Task_Type (U_Ent) then
5573 -- Check obsolescent (but never obsolescent if from aspect)
5575 if not From_Aspect_Specification (N) then
5576 Check_Restriction (No_Obsolescent_Features, N);
5578 if Warn_On_Obsolescent_Feature then
5579 Error_Msg_N
5580 ("?j?storage size clause for task is an " &
5581 "obsolescent feature (RM J.9)", N);
5582 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5583 end if;
5584 end if;
5586 FOnly := True;
5587 end if;
5589 if not Is_Access_Type (U_Ent)
5590 and then Ekind (U_Ent) /= E_Task_Type
5591 then
5592 Error_Msg_N ("storage size cannot be given for &", Nam);
5594 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5595 Error_Msg_N
5596 ("storage size cannot be given for a derived access type",
5597 Nam);
5599 elsif Duplicate_Clause then
5600 null;
5602 else
5603 Analyze_And_Resolve (Expr, Any_Integer);
5605 if Is_Access_Type (U_Ent) then
5607 -- Check for Storage_Pool previously given
5609 declare
5610 SP : constant Node_Id :=
5611 Get_Attribute_Definition_Clause
5612 (U_Ent, Attribute_Storage_Pool);
5614 begin
5615 if Present (SP) then
5616 Check_Pool_Size_Clash (U_Ent, SP, N);
5617 end if;
5618 end;
5620 -- Special case of for x'Storage_Size use 0
5622 if Is_OK_Static_Expression (Expr)
5623 and then Expr_Value (Expr) = 0
5624 then
5625 Set_No_Pool_Assigned (Btype);
5626 end if;
5627 end if;
5629 Set_Has_Storage_Size_Clause (Btype);
5630 end if;
5631 end Storage_Size;
5633 -----------------
5634 -- Stream_Size --
5635 -----------------
5637 when Attribute_Stream_Size => Stream_Size : declare
5638 Size : constant Uint := Static_Integer (Expr);
5640 begin
5641 if Ada_Version <= Ada_95 then
5642 Check_Restriction (No_Implementation_Attributes, N);
5643 end if;
5645 if Duplicate_Clause then
5646 null;
5648 elsif Is_Elementary_Type (U_Ent) then
5649 if Size /= System_Storage_Unit
5650 and then
5651 Size /= System_Storage_Unit * 2
5652 and then
5653 Size /= System_Storage_Unit * 4
5654 and then
5655 Size /= System_Storage_Unit * 8
5656 then
5657 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5658 Error_Msg_N
5659 ("stream size for elementary type must be a"
5660 & " power of 2 and at least ^", N);
5662 elsif RM_Size (U_Ent) > Size then
5663 Error_Msg_Uint_1 := RM_Size (U_Ent);
5664 Error_Msg_N
5665 ("stream size for elementary type must be a"
5666 & " power of 2 and at least ^", N);
5667 end if;
5669 Set_Has_Stream_Size_Clause (U_Ent);
5671 else
5672 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5673 end if;
5674 end Stream_Size;
5676 ----------------
5677 -- Value_Size --
5678 ----------------
5680 -- Value_Size attribute definition clause
5682 when Attribute_Value_Size => Value_Size : declare
5683 Size : constant Uint := Static_Integer (Expr);
5684 Biased : Boolean;
5686 begin
5687 if not Is_Type (U_Ent) then
5688 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5690 elsif Duplicate_Clause then
5691 null;
5693 elsif Is_Array_Type (U_Ent)
5694 and then not Is_Constrained (U_Ent)
5695 then
5696 Error_Msg_N
5697 ("Value_Size cannot be given for unconstrained array", Nam);
5699 else
5700 if Is_Elementary_Type (U_Ent) then
5701 Check_Size (Expr, U_Ent, Size, Biased);
5702 Set_Biased (U_Ent, N, "value size clause", Biased);
5703 end if;
5705 Set_RM_Size (U_Ent, Size);
5706 end if;
5707 end Value_Size;
5709 -----------------------
5710 -- Variable_Indexing --
5711 -----------------------
5713 when Attribute_Variable_Indexing =>
5714 Check_Indexing_Functions;
5716 -----------
5717 -- Write --
5718 -----------
5720 when Attribute_Write =>
5721 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5722 Set_Has_Specified_Stream_Write (Ent);
5724 -- All other attributes cannot be set
5726 when others =>
5727 Error_Msg_N
5728 ("attribute& cannot be set with definition clause", N);
5729 end case;
5731 -- The test for the type being frozen must be performed after any
5732 -- expression the clause has been analyzed since the expression itself
5733 -- might cause freezing that makes the clause illegal.
5735 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5736 return;
5737 end if;
5738 end Analyze_Attribute_Definition_Clause;
5740 ----------------------------
5741 -- Analyze_Code_Statement --
5742 ----------------------------
5744 procedure Analyze_Code_Statement (N : Node_Id) is
5745 HSS : constant Node_Id := Parent (N);
5746 SBody : constant Node_Id := Parent (HSS);
5747 Subp : constant Entity_Id := Current_Scope;
5748 Stmt : Node_Id;
5749 Decl : Node_Id;
5750 StmtO : Node_Id;
5751 DeclO : Node_Id;
5753 begin
5754 -- Analyze and check we get right type, note that this implements the
5755 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5756 -- is the only way that Asm_Insn could possibly be visible.
5758 Analyze_And_Resolve (Expression (N));
5760 if Etype (Expression (N)) = Any_Type then
5761 return;
5762 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5763 Error_Msg_N ("incorrect type for code statement", N);
5764 return;
5765 end if;
5767 Check_Code_Statement (N);
5769 -- Make sure we appear in the handled statement sequence of a
5770 -- subprogram (RM 13.8(3)).
5772 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5773 or else Nkind (SBody) /= N_Subprogram_Body
5774 then
5775 Error_Msg_N
5776 ("code statement can only appear in body of subprogram", N);
5777 return;
5778 end if;
5780 -- Do remaining checks (RM 13.8(3)) if not already done
5782 if not Is_Machine_Code_Subprogram (Subp) then
5783 Set_Is_Machine_Code_Subprogram (Subp);
5785 -- No exception handlers allowed
5787 if Present (Exception_Handlers (HSS)) then
5788 Error_Msg_N
5789 ("exception handlers not permitted in machine code subprogram",
5790 First (Exception_Handlers (HSS)));
5791 end if;
5793 -- No declarations other than use clauses and pragmas (we allow
5794 -- certain internally generated declarations as well).
5796 Decl := First (Declarations (SBody));
5797 while Present (Decl) loop
5798 DeclO := Original_Node (Decl);
5799 if Comes_From_Source (DeclO)
5800 and not Nkind_In (DeclO, N_Pragma,
5801 N_Use_Package_Clause,
5802 N_Use_Type_Clause,
5803 N_Implicit_Label_Declaration)
5804 then
5805 Error_Msg_N
5806 ("this declaration not allowed in machine code subprogram",
5807 DeclO);
5808 end if;
5810 Next (Decl);
5811 end loop;
5813 -- No statements other than code statements, pragmas, and labels.
5814 -- Again we allow certain internally generated statements.
5816 -- In Ada 2012, qualified expressions are names, and the code
5817 -- statement is initially parsed as a procedure call.
5819 Stmt := First (Statements (HSS));
5820 while Present (Stmt) loop
5821 StmtO := Original_Node (Stmt);
5823 -- A procedure call transformed into a code statement is OK.
5825 if Ada_Version >= Ada_2012
5826 and then Nkind (StmtO) = N_Procedure_Call_Statement
5827 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5828 then
5829 null;
5831 elsif Comes_From_Source (StmtO)
5832 and then not Nkind_In (StmtO, N_Pragma,
5833 N_Label,
5834 N_Code_Statement)
5835 then
5836 Error_Msg_N
5837 ("this statement is not allowed in machine code subprogram",
5838 StmtO);
5839 end if;
5841 Next (Stmt);
5842 end loop;
5843 end if;
5844 end Analyze_Code_Statement;
5846 -----------------------------------------------
5847 -- Analyze_Enumeration_Representation_Clause --
5848 -----------------------------------------------
5850 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5851 Ident : constant Node_Id := Identifier (N);
5852 Aggr : constant Node_Id := Array_Aggregate (N);
5853 Enumtype : Entity_Id;
5854 Elit : Entity_Id;
5855 Expr : Node_Id;
5856 Assoc : Node_Id;
5857 Choice : Node_Id;
5858 Val : Uint;
5860 Err : Boolean := False;
5861 -- Set True to avoid cascade errors and crashes on incorrect source code
5863 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5864 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5865 -- Allowed range of universal integer (= allowed range of enum lit vals)
5867 Min : Uint;
5868 Max : Uint;
5869 -- Minimum and maximum values of entries
5871 Max_Node : Node_Id;
5872 -- Pointer to node for literal providing max value
5874 begin
5875 if Ignore_Rep_Clauses then
5876 Kill_Rep_Clause (N);
5877 return;
5878 end if;
5880 -- Ignore enumeration rep clauses by default in CodePeer mode,
5881 -- unless -gnatd.I is specified, as a work around for potential false
5882 -- positive messages.
5884 if CodePeer_Mode and not Debug_Flag_Dot_II then
5885 return;
5886 end if;
5888 -- First some basic error checks
5890 Find_Type (Ident);
5891 Enumtype := Entity (Ident);
5893 if Enumtype = Any_Type
5894 or else Rep_Item_Too_Early (Enumtype, N)
5895 then
5896 return;
5897 else
5898 Enumtype := Underlying_Type (Enumtype);
5899 end if;
5901 if not Is_Enumeration_Type (Enumtype) then
5902 Error_Msg_NE
5903 ("enumeration type required, found}",
5904 Ident, First_Subtype (Enumtype));
5905 return;
5906 end if;
5908 -- Ignore rep clause on generic actual type. This will already have
5909 -- been flagged on the template as an error, and this is the safest
5910 -- way to ensure we don't get a junk cascaded message in the instance.
5912 if Is_Generic_Actual_Type (Enumtype) then
5913 return;
5915 -- Type must be in current scope
5917 elsif Scope (Enumtype) /= Current_Scope then
5918 Error_Msg_N ("type must be declared in this scope", Ident);
5919 return;
5921 -- Type must be a first subtype
5923 elsif not Is_First_Subtype (Enumtype) then
5924 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5925 return;
5927 -- Ignore duplicate rep clause
5929 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5930 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5931 return;
5933 -- Don't allow rep clause for standard [wide_[wide_]]character
5935 elsif Is_Standard_Character_Type (Enumtype) then
5936 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5937 return;
5939 -- Check that the expression is a proper aggregate (no parentheses)
5941 elsif Paren_Count (Aggr) /= 0 then
5942 Error_Msg
5943 ("extra parentheses surrounding aggregate not allowed",
5944 First_Sloc (Aggr));
5945 return;
5947 -- All tests passed, so set rep clause in place
5949 else
5950 Set_Has_Enumeration_Rep_Clause (Enumtype);
5951 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5952 end if;
5954 -- Now we process the aggregate. Note that we don't use the normal
5955 -- aggregate code for this purpose, because we don't want any of the
5956 -- normal expansion activities, and a number of special semantic
5957 -- rules apply (including the component type being any integer type)
5959 Elit := First_Literal (Enumtype);
5961 -- First the positional entries if any
5963 if Present (Expressions (Aggr)) then
5964 Expr := First (Expressions (Aggr));
5965 while Present (Expr) loop
5966 if No (Elit) then
5967 Error_Msg_N ("too many entries in aggregate", Expr);
5968 return;
5969 end if;
5971 Val := Static_Integer (Expr);
5973 -- Err signals that we found some incorrect entries processing
5974 -- the list. The final checks for completeness and ordering are
5975 -- skipped in this case.
5977 if Val = No_Uint then
5978 Err := True;
5980 elsif Val < Lo or else Hi < Val then
5981 Error_Msg_N ("value outside permitted range", Expr);
5982 Err := True;
5983 end if;
5985 Set_Enumeration_Rep (Elit, Val);
5986 Set_Enumeration_Rep_Expr (Elit, Expr);
5987 Next (Expr);
5988 Next (Elit);
5989 end loop;
5990 end if;
5992 -- Now process the named entries if present
5994 if Present (Component_Associations (Aggr)) then
5995 Assoc := First (Component_Associations (Aggr));
5996 while Present (Assoc) loop
5997 Choice := First (Choices (Assoc));
5999 if Present (Next (Choice)) then
6000 Error_Msg_N
6001 ("multiple choice not allowed here", Next (Choice));
6002 Err := True;
6003 end if;
6005 if Nkind (Choice) = N_Others_Choice then
6006 Error_Msg_N ("others choice not allowed here", Choice);
6007 Err := True;
6009 elsif Nkind (Choice) = N_Range then
6011 -- ??? should allow zero/one element range here
6013 Error_Msg_N ("range not allowed here", Choice);
6014 Err := True;
6016 else
6017 Analyze_And_Resolve (Choice, Enumtype);
6019 if Error_Posted (Choice) then
6020 Err := True;
6021 end if;
6023 if not Err then
6024 if Is_Entity_Name (Choice)
6025 and then Is_Type (Entity (Choice))
6026 then
6027 Error_Msg_N ("subtype name not allowed here", Choice);
6028 Err := True;
6030 -- ??? should allow static subtype with zero/one entry
6032 elsif Etype (Choice) = Base_Type (Enumtype) then
6033 if not Is_OK_Static_Expression (Choice) then
6034 Flag_Non_Static_Expr
6035 ("non-static expression used for choice!", Choice);
6036 Err := True;
6038 else
6039 Elit := Expr_Value_E (Choice);
6041 if Present (Enumeration_Rep_Expr (Elit)) then
6042 Error_Msg_Sloc :=
6043 Sloc (Enumeration_Rep_Expr (Elit));
6044 Error_Msg_NE
6045 ("representation for& previously given#",
6046 Choice, Elit);
6047 Err := True;
6048 end if;
6050 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6052 Expr := Expression (Assoc);
6053 Val := Static_Integer (Expr);
6055 if Val = No_Uint then
6056 Err := True;
6058 elsif Val < Lo or else Hi < Val then
6059 Error_Msg_N ("value outside permitted range", Expr);
6060 Err := True;
6061 end if;
6063 Set_Enumeration_Rep (Elit, Val);
6064 end if;
6065 end if;
6066 end if;
6067 end if;
6069 Next (Assoc);
6070 end loop;
6071 end if;
6073 -- Aggregate is fully processed. Now we check that a full set of
6074 -- representations was given, and that they are in range and in order.
6075 -- These checks are only done if no other errors occurred.
6077 if not Err then
6078 Min := No_Uint;
6079 Max := No_Uint;
6081 Elit := First_Literal (Enumtype);
6082 while Present (Elit) loop
6083 if No (Enumeration_Rep_Expr (Elit)) then
6084 Error_Msg_NE ("missing representation for&!", N, Elit);
6086 else
6087 Val := Enumeration_Rep (Elit);
6089 if Min = No_Uint then
6090 Min := Val;
6091 end if;
6093 if Val /= No_Uint then
6094 if Max /= No_Uint and then Val <= Max then
6095 Error_Msg_NE
6096 ("enumeration value for& not ordered!",
6097 Enumeration_Rep_Expr (Elit), Elit);
6098 end if;
6100 Max_Node := Enumeration_Rep_Expr (Elit);
6101 Max := Val;
6102 end if;
6104 -- If there is at least one literal whose representation is not
6105 -- equal to the Pos value, then note that this enumeration type
6106 -- has a non-standard representation.
6108 if Val /= Enumeration_Pos (Elit) then
6109 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6110 end if;
6111 end if;
6113 Next (Elit);
6114 end loop;
6116 -- Now set proper size information
6118 declare
6119 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6121 begin
6122 if Has_Size_Clause (Enumtype) then
6124 -- All OK, if size is OK now
6126 if RM_Size (Enumtype) >= Minsize then
6127 null;
6129 else
6130 -- Try if we can get by with biasing
6132 Minsize :=
6133 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6135 -- Error message if even biasing does not work
6137 if RM_Size (Enumtype) < Minsize then
6138 Error_Msg_Uint_1 := RM_Size (Enumtype);
6139 Error_Msg_Uint_2 := Max;
6140 Error_Msg_N
6141 ("previously given size (^) is too small "
6142 & "for this value (^)", Max_Node);
6144 -- If biasing worked, indicate that we now have biased rep
6146 else
6147 Set_Biased
6148 (Enumtype, Size_Clause (Enumtype), "size clause");
6149 end if;
6150 end if;
6152 else
6153 Set_RM_Size (Enumtype, Minsize);
6154 Set_Enum_Esize (Enumtype);
6155 end if;
6157 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6158 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6159 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6160 end;
6161 end if;
6163 -- We repeat the too late test in case it froze itself
6165 if Rep_Item_Too_Late (Enumtype, N) then
6166 null;
6167 end if;
6168 end Analyze_Enumeration_Representation_Clause;
6170 ----------------------------
6171 -- Analyze_Free_Statement --
6172 ----------------------------
6174 procedure Analyze_Free_Statement (N : Node_Id) is
6175 begin
6176 Analyze (Expression (N));
6177 end Analyze_Free_Statement;
6179 ---------------------------
6180 -- Analyze_Freeze_Entity --
6181 ---------------------------
6183 procedure Analyze_Freeze_Entity (N : Node_Id) is
6184 begin
6185 Freeze_Entity_Checks (N);
6186 end Analyze_Freeze_Entity;
6188 -----------------------------------
6189 -- Analyze_Freeze_Generic_Entity --
6190 -----------------------------------
6192 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6193 begin
6194 Freeze_Entity_Checks (N);
6195 end Analyze_Freeze_Generic_Entity;
6197 ------------------------------------------
6198 -- Analyze_Record_Representation_Clause --
6199 ------------------------------------------
6201 -- Note: we check as much as we can here, but we can't do any checks
6202 -- based on the position values (e.g. overlap checks) until freeze time
6203 -- because especially in Ada 2005 (machine scalar mode), the processing
6204 -- for non-standard bit order can substantially change the positions.
6205 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6206 -- for the remainder of this processing.
6208 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6209 Ident : constant Node_Id := Identifier (N);
6210 Biased : Boolean;
6211 CC : Node_Id;
6212 Comp : Entity_Id;
6213 Fbit : Uint;
6214 Hbit : Uint := Uint_0;
6215 Lbit : Uint;
6216 Ocomp : Entity_Id;
6217 Posit : Uint;
6218 Rectype : Entity_Id;
6219 Recdef : Node_Id;
6221 function Is_Inherited (Comp : Entity_Id) return Boolean;
6222 -- True if Comp is an inherited component in a record extension
6224 ------------------
6225 -- Is_Inherited --
6226 ------------------
6228 function Is_Inherited (Comp : Entity_Id) return Boolean is
6229 Comp_Base : Entity_Id;
6231 begin
6232 if Ekind (Rectype) = E_Record_Subtype then
6233 Comp_Base := Original_Record_Component (Comp);
6234 else
6235 Comp_Base := Comp;
6236 end if;
6238 return Comp_Base /= Original_Record_Component (Comp_Base);
6239 end Is_Inherited;
6241 -- Local variables
6243 Is_Record_Extension : Boolean;
6244 -- True if Rectype is a record extension
6246 CR_Pragma : Node_Id := Empty;
6247 -- Points to N_Pragma node if Complete_Representation pragma present
6249 -- Start of processing for Analyze_Record_Representation_Clause
6251 begin
6252 if Ignore_Rep_Clauses then
6253 Kill_Rep_Clause (N);
6254 return;
6255 end if;
6257 Find_Type (Ident);
6258 Rectype := Entity (Ident);
6260 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6261 return;
6262 else
6263 Rectype := Underlying_Type (Rectype);
6264 end if;
6266 -- First some basic error checks
6268 if not Is_Record_Type (Rectype) then
6269 Error_Msg_NE
6270 ("record type required, found}", Ident, First_Subtype (Rectype));
6271 return;
6273 elsif Scope (Rectype) /= Current_Scope then
6274 Error_Msg_N ("type must be declared in this scope", N);
6275 return;
6277 elsif not Is_First_Subtype (Rectype) then
6278 Error_Msg_N ("cannot give record rep clause for subtype", N);
6279 return;
6281 elsif Has_Record_Rep_Clause (Rectype) then
6282 Error_Msg_N ("duplicate record rep clause ignored", N);
6283 return;
6285 elsif Rep_Item_Too_Late (Rectype, N) then
6286 return;
6287 end if;
6289 -- We know we have a first subtype, now possibly go the the anonymous
6290 -- base type to determine whether Rectype is a record extension.
6292 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6293 Is_Record_Extension :=
6294 Nkind (Recdef) = N_Derived_Type_Definition
6295 and then Present (Record_Extension_Part (Recdef));
6297 if Present (Mod_Clause (N)) then
6298 declare
6299 Loc : constant Source_Ptr := Sloc (N);
6300 M : constant Node_Id := Mod_Clause (N);
6301 P : constant List_Id := Pragmas_Before (M);
6302 AtM_Nod : Node_Id;
6304 Mod_Val : Uint;
6305 pragma Warnings (Off, Mod_Val);
6307 begin
6308 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6310 if Warn_On_Obsolescent_Feature then
6311 Error_Msg_N
6312 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6313 Error_Msg_N
6314 ("\?j?use alignment attribute definition clause instead", N);
6315 end if;
6317 if Present (P) then
6318 Analyze_List (P);
6319 end if;
6321 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6322 -- the Mod clause into an alignment clause anyway, so that the
6323 -- back-end can compute and back-annotate properly the size and
6324 -- alignment of types that may include this record.
6326 -- This seems dubious, this destroys the source tree in a manner
6327 -- not detectable by ASIS ???
6329 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6330 AtM_Nod :=
6331 Make_Attribute_Definition_Clause (Loc,
6332 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6333 Chars => Name_Alignment,
6334 Expression => Relocate_Node (Expression (M)));
6336 Set_From_At_Mod (AtM_Nod);
6337 Insert_After (N, AtM_Nod);
6338 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6339 Set_Mod_Clause (N, Empty);
6341 else
6342 -- Get the alignment value to perform error checking
6344 Mod_Val := Get_Alignment_Value (Expression (M));
6345 end if;
6346 end;
6347 end if;
6349 -- For untagged types, clear any existing component clauses for the
6350 -- type. If the type is derived, this is what allows us to override
6351 -- a rep clause for the parent. For type extensions, the representation
6352 -- of the inherited components is inherited, so we want to keep previous
6353 -- component clauses for completeness.
6355 if not Is_Tagged_Type (Rectype) then
6356 Comp := First_Component_Or_Discriminant (Rectype);
6357 while Present (Comp) loop
6358 Set_Component_Clause (Comp, Empty);
6359 Next_Component_Or_Discriminant (Comp);
6360 end loop;
6361 end if;
6363 -- All done if no component clauses
6365 CC := First (Component_Clauses (N));
6367 if No (CC) then
6368 return;
6369 end if;
6371 -- A representation like this applies to the base type
6373 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6374 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6375 Set_Has_Specified_Layout (Base_Type (Rectype));
6377 -- Process the component clauses
6379 while Present (CC) loop
6381 -- Pragma
6383 if Nkind (CC) = N_Pragma then
6384 Analyze (CC);
6386 -- The only pragma of interest is Complete_Representation
6388 if Pragma_Name (CC) = Name_Complete_Representation then
6389 CR_Pragma := CC;
6390 end if;
6392 -- Processing for real component clause
6394 else
6395 Posit := Static_Integer (Position (CC));
6396 Fbit := Static_Integer (First_Bit (CC));
6397 Lbit := Static_Integer (Last_Bit (CC));
6399 if Posit /= No_Uint
6400 and then Fbit /= No_Uint
6401 and then Lbit /= No_Uint
6402 then
6403 if Posit < 0 then
6404 Error_Msg_N
6405 ("position cannot be negative", Position (CC));
6407 elsif Fbit < 0 then
6408 Error_Msg_N
6409 ("first bit cannot be negative", First_Bit (CC));
6411 -- The Last_Bit specified in a component clause must not be
6412 -- less than the First_Bit minus one (RM-13.5.1(10)).
6414 elsif Lbit < Fbit - 1 then
6415 Error_Msg_N
6416 ("last bit cannot be less than first bit minus one",
6417 Last_Bit (CC));
6419 -- Values look OK, so find the corresponding record component
6420 -- Even though the syntax allows an attribute reference for
6421 -- implementation-defined components, GNAT does not allow the
6422 -- tag to get an explicit position.
6424 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6425 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6426 Error_Msg_N ("position of tag cannot be specified", CC);
6427 else
6428 Error_Msg_N ("illegal component name", CC);
6429 end if;
6431 else
6432 Comp := First_Entity (Rectype);
6433 while Present (Comp) loop
6434 exit when Chars (Comp) = Chars (Component_Name (CC));
6435 Next_Entity (Comp);
6436 end loop;
6438 if No (Comp) then
6440 -- Maybe component of base type that is absent from
6441 -- statically constrained first subtype.
6443 Comp := First_Entity (Base_Type (Rectype));
6444 while Present (Comp) loop
6445 exit when Chars (Comp) = Chars (Component_Name (CC));
6446 Next_Entity (Comp);
6447 end loop;
6448 end if;
6450 if No (Comp) then
6451 Error_Msg_N
6452 ("component clause is for non-existent field", CC);
6454 -- Ada 2012 (AI05-0026): Any name that denotes a
6455 -- discriminant of an object of an unchecked union type
6456 -- shall not occur within a record_representation_clause.
6458 -- The general restriction of using record rep clauses on
6459 -- Unchecked_Union types has now been lifted. Since it is
6460 -- possible to introduce a record rep clause which mentions
6461 -- the discriminant of an Unchecked_Union in non-Ada 2012
6462 -- code, this check is applied to all versions of the
6463 -- language.
6465 elsif Ekind (Comp) = E_Discriminant
6466 and then Is_Unchecked_Union (Rectype)
6467 then
6468 Error_Msg_N
6469 ("cannot reference discriminant of unchecked union",
6470 Component_Name (CC));
6472 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6473 Error_Msg_NE
6474 ("component clause not allowed for inherited "
6475 & "component&", CC, Comp);
6477 elsif Present (Component_Clause (Comp)) then
6479 -- Diagnose duplicate rep clause, or check consistency
6480 -- if this is an inherited component. In a double fault,
6481 -- there may be a duplicate inconsistent clause for an
6482 -- inherited component.
6484 if Scope (Original_Record_Component (Comp)) = Rectype
6485 or else Parent (Component_Clause (Comp)) = N
6486 then
6487 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6488 Error_Msg_N ("component clause previously given#", CC);
6490 else
6491 declare
6492 Rep1 : constant Node_Id := Component_Clause (Comp);
6493 begin
6494 if Intval (Position (Rep1)) /=
6495 Intval (Position (CC))
6496 or else Intval (First_Bit (Rep1)) /=
6497 Intval (First_Bit (CC))
6498 or else Intval (Last_Bit (Rep1)) /=
6499 Intval (Last_Bit (CC))
6500 then
6501 Error_Msg_N
6502 ("component clause inconsistent "
6503 & "with representation of ancestor", CC);
6505 elsif Warn_On_Redundant_Constructs then
6506 Error_Msg_N
6507 ("?r?redundant confirming component clause "
6508 & "for component!", CC);
6509 end if;
6510 end;
6511 end if;
6513 -- Normal case where this is the first component clause we
6514 -- have seen for this entity, so set it up properly.
6516 else
6517 -- Make reference for field in record rep clause and set
6518 -- appropriate entity field in the field identifier.
6520 Generate_Reference
6521 (Comp, Component_Name (CC), Set_Ref => False);
6522 Set_Entity (Component_Name (CC), Comp);
6524 -- Update Fbit and Lbit to the actual bit number
6526 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6527 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6529 if Has_Size_Clause (Rectype)
6530 and then RM_Size (Rectype) <= Lbit
6531 then
6532 Error_Msg_N
6533 ("bit number out of range of specified size",
6534 Last_Bit (CC));
6535 else
6536 Set_Component_Clause (Comp, CC);
6537 Set_Component_Bit_Offset (Comp, Fbit);
6538 Set_Esize (Comp, 1 + (Lbit - Fbit));
6539 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6540 Set_Normalized_Position (Comp, Fbit / SSU);
6542 if Warn_On_Overridden_Size
6543 and then Has_Size_Clause (Etype (Comp))
6544 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6545 then
6546 Error_Msg_NE
6547 ("?S?component size overrides size clause for&",
6548 Component_Name (CC), Etype (Comp));
6549 end if;
6551 -- This information is also set in the corresponding
6552 -- component of the base type, found by accessing the
6553 -- Original_Record_Component link if it is present.
6555 Ocomp := Original_Record_Component (Comp);
6557 if Hbit < Lbit then
6558 Hbit := Lbit;
6559 end if;
6561 Check_Size
6562 (Component_Name (CC),
6563 Etype (Comp),
6564 Esize (Comp),
6565 Biased);
6567 Set_Biased
6568 (Comp, First_Node (CC), "component clause", Biased);
6570 if Present (Ocomp) then
6571 Set_Component_Clause (Ocomp, CC);
6572 Set_Component_Bit_Offset (Ocomp, Fbit);
6573 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6574 Set_Normalized_Position (Ocomp, Fbit / SSU);
6575 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6577 Set_Normalized_Position_Max
6578 (Ocomp, Normalized_Position (Ocomp));
6580 -- Note: we don't use Set_Biased here, because we
6581 -- already gave a warning above if needed, and we
6582 -- would get a duplicate for the same name here.
6584 Set_Has_Biased_Representation
6585 (Ocomp, Has_Biased_Representation (Comp));
6586 end if;
6588 if Esize (Comp) < 0 then
6589 Error_Msg_N ("component size is negative", CC);
6590 end if;
6591 end if;
6592 end if;
6593 end if;
6594 end if;
6595 end if;
6597 Next (CC);
6598 end loop;
6600 -- Check missing components if Complete_Representation pragma appeared
6602 if Present (CR_Pragma) then
6603 Comp := First_Component_Or_Discriminant (Rectype);
6604 while Present (Comp) loop
6605 if No (Component_Clause (Comp)) then
6606 Error_Msg_NE
6607 ("missing component clause for &", CR_Pragma, Comp);
6608 end if;
6610 Next_Component_Or_Discriminant (Comp);
6611 end loop;
6613 -- Give missing components warning if required
6615 elsif Warn_On_Unrepped_Components then
6616 declare
6617 Num_Repped_Components : Nat := 0;
6618 Num_Unrepped_Components : Nat := 0;
6620 begin
6621 -- First count number of repped and unrepped components
6623 Comp := First_Component_Or_Discriminant (Rectype);
6624 while Present (Comp) loop
6625 if Present (Component_Clause (Comp)) then
6626 Num_Repped_Components := Num_Repped_Components + 1;
6627 else
6628 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6629 end if;
6631 Next_Component_Or_Discriminant (Comp);
6632 end loop;
6634 -- We are only interested in the case where there is at least one
6635 -- unrepped component, and at least half the components have rep
6636 -- clauses. We figure that if less than half have them, then the
6637 -- partial rep clause is really intentional. If the component
6638 -- type has no underlying type set at this point (as for a generic
6639 -- formal type), we don't know enough to give a warning on the
6640 -- component.
6642 if Num_Unrepped_Components > 0
6643 and then Num_Unrepped_Components < Num_Repped_Components
6644 then
6645 Comp := First_Component_Or_Discriminant (Rectype);
6646 while Present (Comp) loop
6647 if No (Component_Clause (Comp))
6648 and then Comes_From_Source (Comp)
6649 and then Present (Underlying_Type (Etype (Comp)))
6650 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6651 or else Size_Known_At_Compile_Time
6652 (Underlying_Type (Etype (Comp))))
6653 and then not Has_Warnings_Off (Rectype)
6655 -- Ignore discriminant in unchecked union, since it is
6656 -- not there, and cannot have a component clause.
6658 and then (not Is_Unchecked_Union (Rectype)
6659 or else Ekind (Comp) /= E_Discriminant)
6660 then
6661 Error_Msg_Sloc := Sloc (Comp);
6662 Error_Msg_NE
6663 ("?C?no component clause given for & declared #",
6664 N, Comp);
6665 end if;
6667 Next_Component_Or_Discriminant (Comp);
6668 end loop;
6669 end if;
6670 end;
6671 end if;
6672 end Analyze_Record_Representation_Clause;
6674 -------------------------------------
6675 -- Build_Discrete_Static_Predicate --
6676 -------------------------------------
6678 procedure Build_Discrete_Static_Predicate
6679 (Typ : Entity_Id;
6680 Expr : Node_Id;
6681 Nam : Name_Id)
6683 Loc : constant Source_Ptr := Sloc (Expr);
6685 Non_Static : exception;
6686 -- Raised if something non-static is found
6688 Btyp : constant Entity_Id := Base_Type (Typ);
6690 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6691 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6692 -- Low bound and high bound value of base type of Typ
6694 TLo : Uint;
6695 THi : Uint;
6696 -- Bounds for constructing the static predicate. We use the bound of the
6697 -- subtype if it is static, otherwise the corresponding base type bound.
6698 -- Note: a non-static subtype can have a static predicate.
6700 type REnt is record
6701 Lo, Hi : Uint;
6702 end record;
6703 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6704 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6705 -- value.
6707 type RList is array (Nat range <>) of REnt;
6708 -- A list of ranges. The ranges are sorted in increasing order, and are
6709 -- disjoint (there is a gap of at least one value between each range in
6710 -- the table). A value is in the set of ranges in Rlist if it lies
6711 -- within one of these ranges.
6713 False_Range : constant RList :=
6714 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6715 -- An empty set of ranges represents a range list that can never be
6716 -- satisfied, since there are no ranges in which the value could lie,
6717 -- so it does not lie in any of them. False_Range is a canonical value
6718 -- for this empty set, but general processing should test for an Rlist
6719 -- with length zero (see Is_False predicate), since other null ranges
6720 -- may appear which must be treated as False.
6722 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6723 -- Range representing True, value must be in the base range
6725 function "and" (Left : RList; Right : RList) return RList;
6726 -- And's together two range lists, returning a range list. This is a set
6727 -- intersection operation.
6729 function "or" (Left : RList; Right : RList) return RList;
6730 -- Or's together two range lists, returning a range list. This is a set
6731 -- union operation.
6733 function "not" (Right : RList) return RList;
6734 -- Returns complement of a given range list, i.e. a range list
6735 -- representing all the values in TLo .. THi that are not in the input
6736 -- operand Right.
6738 function Build_Val (V : Uint) return Node_Id;
6739 -- Return an analyzed N_Identifier node referencing this value, suitable
6740 -- for use as an entry in the Static_Discrte_Predicate list. This node
6741 -- is typed with the base type.
6743 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6744 -- Return an analyzed N_Range node referencing this range, suitable for
6745 -- use as an entry in the Static_Discrete_Predicate list. This node is
6746 -- typed with the base type.
6748 function Get_RList (Exp : Node_Id) return RList;
6749 -- This is a recursive routine that converts the given expression into a
6750 -- list of ranges, suitable for use in building the static predicate.
6752 function Is_False (R : RList) return Boolean;
6753 pragma Inline (Is_False);
6754 -- Returns True if the given range list is empty, and thus represents a
6755 -- False list of ranges that can never be satisfied.
6757 function Is_True (R : RList) return Boolean;
6758 -- Returns True if R trivially represents the True predicate by having a
6759 -- single range from BLo to BHi.
6761 function Is_Type_Ref (N : Node_Id) return Boolean;
6762 pragma Inline (Is_Type_Ref);
6763 -- Returns if True if N is a reference to the type for the predicate in
6764 -- the expression (i.e. if it is an identifier whose Chars field matches
6765 -- the Nam given in the call). N must not be parenthesized, if the type
6766 -- name appears in parens, this routine will return False.
6768 function Lo_Val (N : Node_Id) return Uint;
6769 -- Given an entry from a Static_Discrete_Predicate list that is either
6770 -- a static expression or static range, gets either the expression value
6771 -- or the low bound of the range.
6773 function Hi_Val (N : Node_Id) return Uint;
6774 -- Given an entry from a Static_Discrete_Predicate list that is either
6775 -- a static expression or static range, gets either the expression value
6776 -- or the high bound of the range.
6778 function Membership_Entry (N : Node_Id) return RList;
6779 -- Given a single membership entry (range, value, or subtype), returns
6780 -- the corresponding range list. Raises Static_Error if not static.
6782 function Membership_Entries (N : Node_Id) return RList;
6783 -- Given an element on an alternatives list of a membership operation,
6784 -- returns the range list corresponding to this entry and all following
6785 -- entries (i.e. returns the "or" of this list of values).
6787 function Stat_Pred (Typ : Entity_Id) return RList;
6788 -- Given a type, if it has a static predicate, then return the predicate
6789 -- as a range list, otherwise raise Non_Static.
6791 -----------
6792 -- "and" --
6793 -----------
6795 function "and" (Left : RList; Right : RList) return RList is
6796 FEnt : REnt;
6797 -- First range of result
6799 SLeft : Nat := Left'First;
6800 -- Start of rest of left entries
6802 SRight : Nat := Right'First;
6803 -- Start of rest of right entries
6805 begin
6806 -- If either range is True, return the other
6808 if Is_True (Left) then
6809 return Right;
6810 elsif Is_True (Right) then
6811 return Left;
6812 end if;
6814 -- If either range is False, return False
6816 if Is_False (Left) or else Is_False (Right) then
6817 return False_Range;
6818 end if;
6820 -- Loop to remove entries at start that are disjoint, and thus just
6821 -- get discarded from the result entirely.
6823 loop
6824 -- If no operands left in either operand, result is false
6826 if SLeft > Left'Last or else SRight > Right'Last then
6827 return False_Range;
6829 -- Discard first left operand entry if disjoint with right
6831 elsif Left (SLeft).Hi < Right (SRight).Lo then
6832 SLeft := SLeft + 1;
6834 -- Discard first right operand entry if disjoint with left
6836 elsif Right (SRight).Hi < Left (SLeft).Lo then
6837 SRight := SRight + 1;
6839 -- Otherwise we have an overlapping entry
6841 else
6842 exit;
6843 end if;
6844 end loop;
6846 -- Now we have two non-null operands, and first entries overlap. The
6847 -- first entry in the result will be the overlapping part of these
6848 -- two entries.
6850 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6851 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6853 -- Now we can remove the entry that ended at a lower value, since its
6854 -- contribution is entirely contained in Fent.
6856 if Left (SLeft).Hi <= Right (SRight).Hi then
6857 SLeft := SLeft + 1;
6858 else
6859 SRight := SRight + 1;
6860 end if;
6862 -- Compute result by concatenating this first entry with the "and" of
6863 -- the remaining parts of the left and right operands. Note that if
6864 -- either of these is empty, "and" will yield empty, so that we will
6865 -- end up with just Fent, which is what we want in that case.
6867 return
6868 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6869 end "and";
6871 -----------
6872 -- "not" --
6873 -----------
6875 function "not" (Right : RList) return RList is
6876 begin
6877 -- Return True if False range
6879 if Is_False (Right) then
6880 return True_Range;
6881 end if;
6883 -- Return False if True range
6885 if Is_True (Right) then
6886 return False_Range;
6887 end if;
6889 -- Here if not trivial case
6891 declare
6892 Result : RList (1 .. Right'Length + 1);
6893 -- May need one more entry for gap at beginning and end
6895 Count : Nat := 0;
6896 -- Number of entries stored in Result
6898 begin
6899 -- Gap at start
6901 if Right (Right'First).Lo > TLo then
6902 Count := Count + 1;
6903 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6904 end if;
6906 -- Gaps between ranges
6908 for J in Right'First .. Right'Last - 1 loop
6909 Count := Count + 1;
6910 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6911 end loop;
6913 -- Gap at end
6915 if Right (Right'Last).Hi < THi then
6916 Count := Count + 1;
6917 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6918 end if;
6920 return Result (1 .. Count);
6921 end;
6922 end "not";
6924 ----------
6925 -- "or" --
6926 ----------
6928 function "or" (Left : RList; Right : RList) return RList is
6929 FEnt : REnt;
6930 -- First range of result
6932 SLeft : Nat := Left'First;
6933 -- Start of rest of left entries
6935 SRight : Nat := Right'First;
6936 -- Start of rest of right entries
6938 begin
6939 -- If either range is True, return True
6941 if Is_True (Left) or else Is_True (Right) then
6942 return True_Range;
6943 end if;
6945 -- If either range is False (empty), return the other
6947 if Is_False (Left) then
6948 return Right;
6949 elsif Is_False (Right) then
6950 return Left;
6951 end if;
6953 -- Initialize result first entry from left or right operand depending
6954 -- on which starts with the lower range.
6956 if Left (SLeft).Lo < Right (SRight).Lo then
6957 FEnt := Left (SLeft);
6958 SLeft := SLeft + 1;
6959 else
6960 FEnt := Right (SRight);
6961 SRight := SRight + 1;
6962 end if;
6964 -- This loop eats ranges from left and right operands that are
6965 -- contiguous with the first range we are gathering.
6967 loop
6968 -- Eat first entry in left operand if contiguous or overlapped by
6969 -- gathered first operand of result.
6971 if SLeft <= Left'Last
6972 and then Left (SLeft).Lo <= FEnt.Hi + 1
6973 then
6974 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6975 SLeft := SLeft + 1;
6977 -- Eat first entry in right operand if contiguous or overlapped by
6978 -- gathered right operand of result.
6980 elsif SRight <= Right'Last
6981 and then Right (SRight).Lo <= FEnt.Hi + 1
6982 then
6983 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6984 SRight := SRight + 1;
6986 -- All done if no more entries to eat
6988 else
6989 exit;
6990 end if;
6991 end loop;
6993 -- Obtain result as the first entry we just computed, concatenated
6994 -- to the "or" of the remaining results (if one operand is empty,
6995 -- this will just concatenate with the other
6997 return
6998 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6999 end "or";
7001 -----------------
7002 -- Build_Range --
7003 -----------------
7005 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7006 Result : Node_Id;
7007 begin
7008 Result :=
7009 Make_Range (Loc,
7010 Low_Bound => Build_Val (Lo),
7011 High_Bound => Build_Val (Hi));
7012 Set_Etype (Result, Btyp);
7013 Set_Analyzed (Result);
7014 return Result;
7015 end Build_Range;
7017 ---------------
7018 -- Build_Val --
7019 ---------------
7021 function Build_Val (V : Uint) return Node_Id is
7022 Result : Node_Id;
7024 begin
7025 if Is_Enumeration_Type (Typ) then
7026 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7027 else
7028 Result := Make_Integer_Literal (Loc, V);
7029 end if;
7031 Set_Etype (Result, Btyp);
7032 Set_Is_Static_Expression (Result);
7033 Set_Analyzed (Result);
7034 return Result;
7035 end Build_Val;
7037 ---------------
7038 -- Get_RList --
7039 ---------------
7041 function Get_RList (Exp : Node_Id) return RList is
7042 Op : Node_Kind;
7043 Val : Uint;
7045 begin
7046 -- Static expression can only be true or false
7048 if Is_OK_Static_Expression (Exp) then
7049 if Expr_Value (Exp) = 0 then
7050 return False_Range;
7051 else
7052 return True_Range;
7053 end if;
7054 end if;
7056 -- Otherwise test node type
7058 Op := Nkind (Exp);
7060 case Op is
7062 -- And
7064 when N_Op_And | N_And_Then =>
7065 return Get_RList (Left_Opnd (Exp))
7067 Get_RList (Right_Opnd (Exp));
7069 -- Or
7071 when N_Op_Or | N_Or_Else =>
7072 return Get_RList (Left_Opnd (Exp))
7074 Get_RList (Right_Opnd (Exp));
7076 -- Not
7078 when N_Op_Not =>
7079 return not Get_RList (Right_Opnd (Exp));
7081 -- Comparisons of type with static value
7083 when N_Op_Compare =>
7085 -- Type is left operand
7087 if Is_Type_Ref (Left_Opnd (Exp))
7088 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7089 then
7090 Val := Expr_Value (Right_Opnd (Exp));
7092 -- Typ is right operand
7094 elsif Is_Type_Ref (Right_Opnd (Exp))
7095 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7096 then
7097 Val := Expr_Value (Left_Opnd (Exp));
7099 -- Invert sense of comparison
7101 case Op is
7102 when N_Op_Gt => Op := N_Op_Lt;
7103 when N_Op_Lt => Op := N_Op_Gt;
7104 when N_Op_Ge => Op := N_Op_Le;
7105 when N_Op_Le => Op := N_Op_Ge;
7106 when others => null;
7107 end case;
7109 -- Other cases are non-static
7111 else
7112 raise Non_Static;
7113 end if;
7115 -- Construct range according to comparison operation
7117 case Op is
7118 when N_Op_Eq =>
7119 return RList'(1 => REnt'(Val, Val));
7121 when N_Op_Ge =>
7122 return RList'(1 => REnt'(Val, BHi));
7124 when N_Op_Gt =>
7125 return RList'(1 => REnt'(Val + 1, BHi));
7127 when N_Op_Le =>
7128 return RList'(1 => REnt'(BLo, Val));
7130 when N_Op_Lt =>
7131 return RList'(1 => REnt'(BLo, Val - 1));
7133 when N_Op_Ne =>
7134 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7136 when others =>
7137 raise Program_Error;
7138 end case;
7140 -- Membership (IN)
7142 when N_In =>
7143 if not Is_Type_Ref (Left_Opnd (Exp)) then
7144 raise Non_Static;
7145 end if;
7147 if Present (Right_Opnd (Exp)) then
7148 return Membership_Entry (Right_Opnd (Exp));
7149 else
7150 return Membership_Entries (First (Alternatives (Exp)));
7151 end if;
7153 -- Negative membership (NOT IN)
7155 when N_Not_In =>
7156 if not Is_Type_Ref (Left_Opnd (Exp)) then
7157 raise Non_Static;
7158 end if;
7160 if Present (Right_Opnd (Exp)) then
7161 return not Membership_Entry (Right_Opnd (Exp));
7162 else
7163 return not Membership_Entries (First (Alternatives (Exp)));
7164 end if;
7166 -- Function call, may be call to static predicate
7168 when N_Function_Call =>
7169 if Is_Entity_Name (Name (Exp)) then
7170 declare
7171 Ent : constant Entity_Id := Entity (Name (Exp));
7172 begin
7173 if Is_Predicate_Function (Ent)
7174 or else
7175 Is_Predicate_Function_M (Ent)
7176 then
7177 return Stat_Pred (Etype (First_Formal (Ent)));
7178 end if;
7179 end;
7180 end if;
7182 -- Other function call cases are non-static
7184 raise Non_Static;
7186 -- Qualified expression, dig out the expression
7188 when N_Qualified_Expression =>
7189 return Get_RList (Expression (Exp));
7191 when N_Case_Expression =>
7192 declare
7193 Alt : Node_Id;
7194 Choices : List_Id;
7195 Dep : Node_Id;
7197 begin
7198 if not Is_Entity_Name (Expression (Expr))
7199 or else Etype (Expression (Expr)) /= Typ
7200 then
7201 Error_Msg_N
7202 ("expression must denaote subtype", Expression (Expr));
7203 return False_Range;
7204 end if;
7206 -- Collect discrete choices in all True alternatives
7208 Choices := New_List;
7209 Alt := First (Alternatives (Exp));
7210 while Present (Alt) loop
7211 Dep := Expression (Alt);
7213 if not Is_OK_Static_Expression (Dep) then
7214 raise Non_Static;
7216 elsif Is_True (Expr_Value (Dep)) then
7217 Append_List_To (Choices,
7218 New_Copy_List (Discrete_Choices (Alt)));
7219 end if;
7221 Next (Alt);
7222 end loop;
7224 return Membership_Entries (First (Choices));
7225 end;
7227 -- Expression with actions: if no actions, dig out expression
7229 when N_Expression_With_Actions =>
7230 if Is_Empty_List (Actions (Exp)) then
7231 return Get_RList (Expression (Exp));
7232 else
7233 raise Non_Static;
7234 end if;
7236 -- Xor operator
7238 when N_Op_Xor =>
7239 return (Get_RList (Left_Opnd (Exp))
7240 and not Get_RList (Right_Opnd (Exp)))
7241 or (Get_RList (Right_Opnd (Exp))
7242 and not Get_RList (Left_Opnd (Exp)));
7244 -- Any other node type is non-static
7246 when others =>
7247 raise Non_Static;
7248 end case;
7249 end Get_RList;
7251 ------------
7252 -- Hi_Val --
7253 ------------
7255 function Hi_Val (N : Node_Id) return Uint is
7256 begin
7257 if Is_OK_Static_Expression (N) then
7258 return Expr_Value (N);
7259 else
7260 pragma Assert (Nkind (N) = N_Range);
7261 return Expr_Value (High_Bound (N));
7262 end if;
7263 end Hi_Val;
7265 --------------
7266 -- Is_False --
7267 --------------
7269 function Is_False (R : RList) return Boolean is
7270 begin
7271 return R'Length = 0;
7272 end Is_False;
7274 -------------
7275 -- Is_True --
7276 -------------
7278 function Is_True (R : RList) return Boolean is
7279 begin
7280 return R'Length = 1
7281 and then R (R'First).Lo = BLo
7282 and then R (R'First).Hi = BHi;
7283 end Is_True;
7285 -----------------
7286 -- Is_Type_Ref --
7287 -----------------
7289 function Is_Type_Ref (N : Node_Id) return Boolean is
7290 begin
7291 return Nkind (N) = N_Identifier
7292 and then Chars (N) = Nam
7293 and then Paren_Count (N) = 0;
7294 end Is_Type_Ref;
7296 ------------
7297 -- Lo_Val --
7298 ------------
7300 function Lo_Val (N : Node_Id) return Uint is
7301 begin
7302 if Is_OK_Static_Expression (N) then
7303 return Expr_Value (N);
7304 else
7305 pragma Assert (Nkind (N) = N_Range);
7306 return Expr_Value (Low_Bound (N));
7307 end if;
7308 end Lo_Val;
7310 ------------------------
7311 -- Membership_Entries --
7312 ------------------------
7314 function Membership_Entries (N : Node_Id) return RList is
7315 begin
7316 if No (Next (N)) then
7317 return Membership_Entry (N);
7318 else
7319 return Membership_Entry (N) or Membership_Entries (Next (N));
7320 end if;
7321 end Membership_Entries;
7323 ----------------------
7324 -- Membership_Entry --
7325 ----------------------
7327 function Membership_Entry (N : Node_Id) return RList is
7328 Val : Uint;
7329 SLo : Uint;
7330 SHi : Uint;
7332 begin
7333 -- Range case
7335 if Nkind (N) = N_Range then
7336 if not Is_OK_Static_Expression (Low_Bound (N))
7337 or else
7338 not Is_OK_Static_Expression (High_Bound (N))
7339 then
7340 raise Non_Static;
7341 else
7342 SLo := Expr_Value (Low_Bound (N));
7343 SHi := Expr_Value (High_Bound (N));
7344 return RList'(1 => REnt'(SLo, SHi));
7345 end if;
7347 -- Static expression case
7349 elsif Is_OK_Static_Expression (N) then
7350 Val := Expr_Value (N);
7351 return RList'(1 => REnt'(Val, Val));
7353 -- Identifier (other than static expression) case
7355 else pragma Assert (Nkind (N) = N_Identifier);
7357 -- Type case
7359 if Is_Type (Entity (N)) then
7361 -- If type has predicates, process them
7363 if Has_Predicates (Entity (N)) then
7364 return Stat_Pred (Entity (N));
7366 -- For static subtype without predicates, get range
7368 elsif Is_OK_Static_Subtype (Entity (N)) then
7369 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7370 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7371 return RList'(1 => REnt'(SLo, SHi));
7373 -- Any other type makes us non-static
7375 else
7376 raise Non_Static;
7377 end if;
7379 -- Any other kind of identifier in predicate (e.g. a non-static
7380 -- expression value) means this is not a static predicate.
7382 else
7383 raise Non_Static;
7384 end if;
7385 end if;
7386 end Membership_Entry;
7388 ---------------
7389 -- Stat_Pred --
7390 ---------------
7392 function Stat_Pred (Typ : Entity_Id) return RList is
7393 begin
7394 -- Not static if type does not have static predicates
7396 if not Has_Static_Predicate (Typ) then
7397 raise Non_Static;
7398 end if;
7400 -- Otherwise we convert the predicate list to a range list
7402 declare
7403 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7404 Result : RList (1 .. List_Length (Spred));
7405 P : Node_Id;
7407 begin
7408 P := First (Static_Discrete_Predicate (Typ));
7409 for J in Result'Range loop
7410 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7411 Next (P);
7412 end loop;
7414 return Result;
7415 end;
7416 end Stat_Pred;
7418 -- Start of processing for Build_Discrete_Static_Predicate
7420 begin
7421 -- Establish bounds for the predicate
7423 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7424 TLo := Expr_Value (Type_Low_Bound (Typ));
7425 else
7426 TLo := BLo;
7427 end if;
7429 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7430 THi := Expr_Value (Type_High_Bound (Typ));
7431 else
7432 THi := BHi;
7433 end if;
7435 -- Analyze the expression to see if it is a static predicate
7437 declare
7438 Ranges : constant RList := Get_RList (Expr);
7439 -- Range list from expression if it is static
7441 Plist : List_Id;
7443 begin
7444 -- Convert range list into a form for the static predicate. In the
7445 -- Ranges array, we just have raw ranges, these must be converted
7446 -- to properly typed and analyzed static expressions or range nodes.
7448 -- Note: here we limit ranges to the ranges of the subtype, so that
7449 -- a predicate is always false for values outside the subtype. That
7450 -- seems fine, such values are invalid anyway, and considering them
7451 -- to fail the predicate seems allowed and friendly, and furthermore
7452 -- simplifies processing for case statements and loops.
7454 Plist := New_List;
7456 for J in Ranges'Range loop
7457 declare
7458 Lo : Uint := Ranges (J).Lo;
7459 Hi : Uint := Ranges (J).Hi;
7461 begin
7462 -- Ignore completely out of range entry
7464 if Hi < TLo or else Lo > THi then
7465 null;
7467 -- Otherwise process entry
7469 else
7470 -- Adjust out of range value to subtype range
7472 if Lo < TLo then
7473 Lo := TLo;
7474 end if;
7476 if Hi > THi then
7477 Hi := THi;
7478 end if;
7480 -- Convert range into required form
7482 Append_To (Plist, Build_Range (Lo, Hi));
7483 end if;
7484 end;
7485 end loop;
7487 -- Processing was successful and all entries were static, so now we
7488 -- can store the result as the predicate list.
7490 Set_Static_Discrete_Predicate (Typ, Plist);
7492 -- The processing for static predicates put the expression into
7493 -- canonical form as a series of ranges. It also eliminated
7494 -- duplicates and collapsed and combined ranges. We might as well
7495 -- replace the alternatives list of the right operand of the
7496 -- membership test with the static predicate list, which will
7497 -- usually be more efficient.
7499 declare
7500 New_Alts : constant List_Id := New_List;
7501 Old_Node : Node_Id;
7502 New_Node : Node_Id;
7504 begin
7505 Old_Node := First (Plist);
7506 while Present (Old_Node) loop
7507 New_Node := New_Copy (Old_Node);
7509 if Nkind (New_Node) = N_Range then
7510 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7511 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7512 end if;
7514 Append_To (New_Alts, New_Node);
7515 Next (Old_Node);
7516 end loop;
7518 -- If empty list, replace by False
7520 if Is_Empty_List (New_Alts) then
7521 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7523 -- Else replace by set membership test
7525 else
7526 Rewrite (Expr,
7527 Make_In (Loc,
7528 Left_Opnd => Make_Identifier (Loc, Nam),
7529 Right_Opnd => Empty,
7530 Alternatives => New_Alts));
7532 -- Resolve new expression in function context
7534 Install_Formals (Predicate_Function (Typ));
7535 Push_Scope (Predicate_Function (Typ));
7536 Analyze_And_Resolve (Expr, Standard_Boolean);
7537 Pop_Scope;
7538 end if;
7539 end;
7540 end;
7542 -- If non-static, return doing nothing
7544 exception
7545 when Non_Static =>
7546 return;
7547 end Build_Discrete_Static_Predicate;
7549 -------------------------------------------
7550 -- Build_Invariant_Procedure_Declaration --
7551 -------------------------------------------
7553 function Build_Invariant_Procedure_Declaration
7554 (Typ : Entity_Id) return Node_Id
7556 Loc : constant Source_Ptr := Sloc (Typ);
7557 Object_Entity : constant Entity_Id :=
7558 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7559 Spec : Node_Id;
7560 SId : Entity_Id;
7562 begin
7563 Set_Etype (Object_Entity, Typ);
7565 -- Check for duplicate definiations.
7567 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7568 return Empty;
7569 end if;
7571 SId :=
7572 Make_Defining_Identifier (Loc,
7573 Chars => New_External_Name (Chars (Typ), "Invariant"));
7574 Set_Has_Invariants (Typ);
7575 Set_Ekind (SId, E_Procedure);
7576 Set_Etype (SId, Standard_Void_Type);
7577 Set_Is_Invariant_Procedure (SId);
7578 Set_Invariant_Procedure (Typ, SId);
7580 Spec :=
7581 Make_Procedure_Specification (Loc,
7582 Defining_Unit_Name => SId,
7583 Parameter_Specifications => New_List (
7584 Make_Parameter_Specification (Loc,
7585 Defining_Identifier => Object_Entity,
7586 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7588 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7589 end Build_Invariant_Procedure_Declaration;
7591 -------------------------------
7592 -- Build_Invariant_Procedure --
7593 -------------------------------
7595 -- The procedure that is constructed here has the form
7597 -- procedure typInvariant (Ixxx : typ) is
7598 -- begin
7599 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7600 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7601 -- ...
7602 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7603 -- ...
7604 -- end typInvariant;
7606 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7607 Loc : constant Source_Ptr := Sloc (Typ);
7608 Stmts : List_Id;
7609 Spec : Node_Id;
7610 SId : Entity_Id;
7611 PDecl : Node_Id;
7612 PBody : Node_Id;
7614 Nam : Name_Id;
7615 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7616 -- if we come from a Type_Invariant aspect, we make sure to build the
7617 -- Check pragma with the right name, so that Check_Policy works right.
7619 Visible_Decls : constant List_Id := Visible_Declarations (N);
7620 Private_Decls : constant List_Id := Private_Declarations (N);
7622 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7623 -- Appends statements to Stmts for any invariants in the rep item chain
7624 -- of the given type. If Inherit is False, then we only process entries
7625 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7626 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7627 -- "inherited" to the exception message and generating an informational
7628 -- message about the inheritance of an invariant.
7630 Object_Name : Name_Id;
7631 -- Name for argument of invariant procedure
7633 Object_Entity : Node_Id;
7634 -- The entity of the formal for the procedure
7636 --------------------
7637 -- Add_Invariants --
7638 --------------------
7640 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7641 Ritem : Node_Id;
7642 Arg1 : Node_Id;
7643 Arg2 : Node_Id;
7644 Arg3 : Node_Id;
7645 Exp : Node_Id;
7646 Loc : Source_Ptr;
7647 Assoc : List_Id;
7648 Str : String_Id;
7650 procedure Replace_Type_Reference (N : Node_Id);
7651 -- Replace a single occurrence N of the subtype name with a reference
7652 -- to the formal of the predicate function. N can be an identifier
7653 -- referencing the subtype, or a selected component, representing an
7654 -- appropriately qualified occurrence of the subtype name.
7656 procedure Replace_Type_References is
7657 new Replace_Type_References_Generic (Replace_Type_Reference);
7658 -- Traverse an expression replacing all occurrences of the subtype
7659 -- name with appropriate references to the object that is the formal
7660 -- parameter of the predicate function. Note that we must ensure
7661 -- that the type and entity information is properly set in the
7662 -- replacement node, since we will do a Preanalyze call of this
7663 -- expression without proper visibility of the procedure argument.
7665 ----------------------------
7666 -- Replace_Type_Reference --
7667 ----------------------------
7669 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7670 -- regarding handling of Sloc and Comes_From_Source.
7672 procedure Replace_Type_Reference (N : Node_Id) is
7673 begin
7675 -- Add semantic information to node to be rewritten, for ASIS
7676 -- navigation needs.
7678 if Nkind (N) = N_Identifier then
7679 Set_Entity (N, T);
7680 Set_Etype (N, T);
7682 elsif Nkind (N) = N_Selected_Component then
7683 Analyze (Prefix (N));
7684 Set_Entity (Selector_Name (N), T);
7685 Set_Etype (Selector_Name (N), T);
7686 end if;
7688 -- Invariant'Class, replace with T'Class (obj)
7689 -- In ASIS mode, an inherited item is analyzed already, and the
7690 -- replacement has been done, so do not repeat transformation
7691 -- to prevent ill-formed tree.
7693 if Class_Present (Ritem) then
7694 if ASIS_Mode
7695 and then Nkind (Parent (N)) = N_Attribute_Reference
7696 and then Attribute_Name (Parent (N)) = Name_Class
7697 then
7698 null;
7700 else
7701 Rewrite (N,
7702 Make_Type_Conversion (Sloc (N),
7703 Subtype_Mark =>
7704 Make_Attribute_Reference (Sloc (N),
7705 Prefix => New_Occurrence_Of (T, Sloc (N)),
7706 Attribute_Name => Name_Class),
7707 Expression =>
7708 Make_Identifier (Sloc (N), Object_Name)));
7710 Set_Entity (Expression (N), Object_Entity);
7711 Set_Etype (Expression (N), Typ);
7712 end if;
7714 -- Invariant, replace with obj
7716 else
7717 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7718 Set_Entity (N, Object_Entity);
7719 Set_Etype (N, Typ);
7720 end if;
7722 Set_Comes_From_Source (N, True);
7723 end Replace_Type_Reference;
7725 -- Start of processing for Add_Invariants
7727 begin
7728 Ritem := First_Rep_Item (T);
7729 while Present (Ritem) loop
7730 if Nkind (Ritem) = N_Pragma
7731 and then Pragma_Name (Ritem) = Name_Invariant
7732 then
7733 Arg1 := First (Pragma_Argument_Associations (Ritem));
7734 Arg2 := Next (Arg1);
7735 Arg3 := Next (Arg2);
7737 Arg1 := Get_Pragma_Arg (Arg1);
7738 Arg2 := Get_Pragma_Arg (Arg2);
7740 -- For Inherit case, ignore Invariant, process only Class case
7742 if Inherit then
7743 if not Class_Present (Ritem) then
7744 goto Continue;
7745 end if;
7747 -- For Inherit false, process only item for right type
7749 else
7750 if Entity (Arg1) /= Typ then
7751 goto Continue;
7752 end if;
7753 end if;
7755 if No (Stmts) then
7756 Stmts := Empty_List;
7757 end if;
7759 Exp := New_Copy_Tree (Arg2);
7761 -- Preserve sloc of original pragma Invariant
7763 Loc := Sloc (Ritem);
7765 -- We need to replace any occurrences of the name of the type
7766 -- with references to the object, converted to type'Class in
7767 -- the case of Invariant'Class aspects.
7769 Replace_Type_References (Exp, T);
7771 -- If this invariant comes from an aspect, find the aspect
7772 -- specification, and replace the saved expression because
7773 -- we need the subtype references replaced for the calls to
7774 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7775 -- and Check_Aspect_At_End_Of_Declarations.
7777 if From_Aspect_Specification (Ritem) then
7778 declare
7779 Aitem : Node_Id;
7781 begin
7782 -- Loop to find corresponding aspect, note that this
7783 -- must be present given the pragma is marked delayed.
7785 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7786 -- this loop does nothing. Furthermore, why isn't this
7787 -- simply Corresponding_Aspect ???
7789 Aitem := Next_Rep_Item (Ritem);
7790 while Present (Aitem) loop
7791 if Nkind (Aitem) = N_Aspect_Specification
7792 and then Aspect_Rep_Item (Aitem) = Ritem
7793 then
7794 Set_Entity
7795 (Identifier (Aitem), New_Copy_Tree (Exp));
7796 exit;
7797 end if;
7799 Aitem := Next_Rep_Item (Aitem);
7800 end loop;
7801 end;
7802 end if;
7804 -- Now we need to preanalyze the expression to properly capture
7805 -- the visibility in the visible part. The expression will not
7806 -- be analyzed for real until the body is analyzed, but that is
7807 -- at the end of the private part and has the wrong visibility.
7809 Set_Parent (Exp, N);
7810 Preanalyze_Assert_Expression (Exp, Any_Boolean);
7812 -- A class-wide invariant may be inherited in a separate unit,
7813 -- where the corresponding expression cannot be resolved by
7814 -- visibility, because it refers to a local function. Propagate
7815 -- semantic information to the original representation item, to
7816 -- be used when an invariant procedure for a derived type is
7817 -- constructed.
7819 -- Unclear how to handle class-wide invariants that are not
7820 -- function calls ???
7822 if not Inherit
7823 and then Class_Present (Ritem)
7824 and then Nkind (Exp) = N_Function_Call
7825 and then Nkind (Arg2) = N_Indexed_Component
7826 then
7827 Rewrite (Arg2,
7828 Make_Function_Call (Loc,
7829 Name =>
7830 New_Occurrence_Of (Entity (Name (Exp)), Loc),
7831 Parameter_Associations =>
7832 New_Copy_List (Expressions (Arg2))));
7833 end if;
7835 -- In ASIS mode, even if assertions are not enabled, we must
7836 -- analyze the original expression in the aspect specification
7837 -- because it is part of the original tree.
7839 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7840 declare
7841 Inv : constant Node_Id :=
7842 Expression (Corresponding_Aspect (Ritem));
7843 begin
7844 Replace_Type_References (Inv, T);
7845 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7846 end;
7847 end if;
7849 -- Get name to be used for Check pragma
7851 if not From_Aspect_Specification (Ritem) then
7852 Nam := Name_Invariant;
7853 else
7854 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7855 end if;
7857 -- Build first two arguments for Check pragma
7859 Assoc :=
7860 New_List (
7861 Make_Pragma_Argument_Association (Loc,
7862 Expression => Make_Identifier (Loc, Chars => Nam)),
7863 Make_Pragma_Argument_Association (Loc,
7864 Expression => Exp));
7866 -- Add message if present in Invariant pragma
7868 if Present (Arg3) then
7869 Str := Strval (Get_Pragma_Arg (Arg3));
7871 -- If inherited case, and message starts "failed invariant",
7872 -- change it to be "failed inherited invariant".
7874 if Inherit then
7875 String_To_Name_Buffer (Str);
7877 if Name_Buffer (1 .. 16) = "failed invariant" then
7878 Insert_Str_In_Name_Buffer ("inherited ", 8);
7879 Str := String_From_Name_Buffer;
7880 end if;
7881 end if;
7883 Append_To (Assoc,
7884 Make_Pragma_Argument_Association (Loc,
7885 Expression => Make_String_Literal (Loc, Str)));
7886 end if;
7888 -- Add Check pragma to list of statements
7890 Append_To (Stmts,
7891 Make_Pragma (Loc,
7892 Pragma_Identifier =>
7893 Make_Identifier (Loc, Name_Check),
7894 Pragma_Argument_Associations => Assoc));
7896 -- If Inherited case and option enabled, output info msg. Note
7897 -- that we know this is a case of Invariant'Class.
7899 if Inherit and Opt.List_Inherited_Aspects then
7900 Error_Msg_Sloc := Sloc (Ritem);
7901 Error_Msg_N
7902 ("info: & inherits `Invariant''Class` aspect from #?L?",
7903 Typ);
7904 end if;
7905 end if;
7907 <<Continue>>
7908 Next_Rep_Item (Ritem);
7909 end loop;
7910 end Add_Invariants;
7912 -- Start of processing for Build_Invariant_Procedure
7914 begin
7915 Stmts := No_List;
7916 PDecl := Empty;
7917 PBody := Empty;
7918 SId := Empty;
7920 -- If the aspect specification exists for some view of the type, the
7921 -- declaration for the procedure has been created.
7923 if Has_Invariants (Typ) then
7924 SId := Invariant_Procedure (Typ);
7925 end if;
7927 -- If the body is already present, nothing to do. This will occur when
7928 -- the type is already frozen, which is the case when the invariant
7929 -- appears in a private part, and the freezing takes place before the
7930 -- final pass over full declarations.
7932 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
7934 if Present (SId) then
7935 PDecl := Unit_Declaration_Node (SId);
7937 if Present (PDecl)
7938 and then Nkind (PDecl) = N_Subprogram_Declaration
7939 and then Present (Corresponding_Body (PDecl))
7940 then
7941 return;
7942 end if;
7944 else
7945 PDecl := Build_Invariant_Procedure_Declaration (Typ);
7946 end if;
7948 -- Recover formal of procedure, for use in the calls to invariant
7949 -- functions (including inherited ones).
7951 Object_Entity :=
7952 Defining_Identifier
7953 (First (Parameter_Specifications (Specification (PDecl))));
7954 Object_Name := Chars (Object_Entity);
7956 -- Add invariants for the current type
7958 Add_Invariants (Typ, Inherit => False);
7960 -- Add invariants for parent types
7962 declare
7963 Current_Typ : Entity_Id;
7964 Parent_Typ : Entity_Id;
7966 begin
7967 Current_Typ := Typ;
7968 loop
7969 Parent_Typ := Etype (Current_Typ);
7971 if Is_Private_Type (Parent_Typ)
7972 and then Present (Full_View (Base_Type (Parent_Typ)))
7973 then
7974 Parent_Typ := Full_View (Base_Type (Parent_Typ));
7975 end if;
7977 exit when Parent_Typ = Current_Typ;
7979 Current_Typ := Parent_Typ;
7980 Add_Invariants (Current_Typ, Inherit => True);
7981 end loop;
7982 end;
7984 -- Add invariants of progenitors
7986 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
7987 declare
7988 Ifaces_List : Elist_Id;
7989 AI : Elmt_Id;
7990 Iface : Entity_Id;
7992 begin
7993 Collect_Interfaces (Typ, Ifaces_List);
7995 AI := First_Elmt (Ifaces_List);
7996 while Present (AI) loop
7997 Iface := Node (AI);
7999 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8000 Add_Invariants (Iface, Inherit => True);
8001 end if;
8003 Next_Elmt (AI);
8004 end loop;
8005 end;
8006 end if;
8008 -- Build the procedure if we generated at least one Check pragma
8010 if Stmts /= No_List then
8011 Spec := Copy_Separate_Tree (Specification (PDecl));
8013 PBody :=
8014 Make_Subprogram_Body (Loc,
8015 Specification => Spec,
8016 Declarations => Empty_List,
8017 Handled_Statement_Sequence =>
8018 Make_Handled_Sequence_Of_Statements (Loc,
8019 Statements => Stmts));
8021 -- Insert procedure declaration and spec at the appropriate points.
8022 -- If declaration is already analyzed, it was processed by the
8023 -- generated pragma.
8025 if Present (Private_Decls) then
8027 -- The spec goes at the end of visible declarations, but they have
8028 -- already been analyzed, so we need to explicitly do the analyze.
8030 if not Analyzed (PDecl) then
8031 Append_To (Visible_Decls, PDecl);
8032 Analyze (PDecl);
8033 end if;
8035 -- The body goes at the end of the private declarations, which we
8036 -- have not analyzed yet, so we do not need to perform an explicit
8037 -- analyze call. We skip this if there are no private declarations
8038 -- (this is an error that will be caught elsewhere);
8040 Append_To (Private_Decls, PBody);
8042 -- If the invariant appears on the full view of a type, the
8043 -- analysis of the private part is complete, and we must
8044 -- analyze the new body explicitly.
8046 if In_Private_Part (Current_Scope) then
8047 Analyze (PBody);
8048 end if;
8050 -- If there are no private declarations this may be an error that
8051 -- will be diagnosed elsewhere. However, if this is a non-private
8052 -- type that inherits invariants, it needs no completion and there
8053 -- may be no private part. In this case insert invariant procedure
8054 -- at end of current declarative list, and analyze at once, given
8055 -- that the type is about to be frozen.
8057 elsif not Is_Private_Type (Typ) then
8058 Append_To (Visible_Decls, PDecl);
8059 Append_To (Visible_Decls, PBody);
8060 Analyze (PDecl);
8061 Analyze (PBody);
8062 end if;
8063 end if;
8064 end Build_Invariant_Procedure;
8066 -------------------------------
8067 -- Build_Predicate_Functions --
8068 -------------------------------
8070 -- The procedures that are constructed here have the form:
8072 -- function typPredicate (Ixxx : typ) return Boolean is
8073 -- begin
8074 -- return
8075 -- exp1 and then exp2 and then ...
8076 -- and then typ1Predicate (typ1 (Ixxx))
8077 -- and then typ2Predicate (typ2 (Ixxx))
8078 -- and then ...;
8079 -- end typPredicate;
8081 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8082 -- this is the point at which these expressions get analyzed, providing the
8083 -- required delay, and typ1, typ2, are entities from which predicates are
8084 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8085 -- use this function even if checks are off, e.g. for membership tests.
8087 -- If the expression has at least one Raise_Expression, then we also build
8088 -- the typPredicateM version of the function, in which any occurrence of a
8089 -- Raise_Expression is converted to "return False".
8091 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8092 Loc : constant Source_Ptr := Sloc (Typ);
8094 Expr : Node_Id;
8095 -- This is the expression for the result of the function. It is
8096 -- is build by connecting the component predicates with AND THEN.
8098 Expr_M : Node_Id;
8099 -- This is the corresponding return expression for the Predicate_M
8100 -- function. It differs in that raise expressions are marked for
8101 -- special expansion (see Process_REs).
8103 Object_Name : constant Name_Id := New_Internal_Name ('I');
8104 -- Name for argument of Predicate procedure. Note that we use the same
8105 -- name for both predicate functions. That way the reference within the
8106 -- predicate expression is the same in both functions.
8108 Object_Entity : constant Entity_Id :=
8109 Make_Defining_Identifier (Loc, Chars => Object_Name);
8110 -- Entity for argument of Predicate procedure
8112 Object_Entity_M : constant Entity_Id :=
8113 Make_Defining_Identifier (Loc, Chars => Object_Name);
8114 -- Entity for argument of Predicate_M procedure
8116 Raise_Expression_Present : Boolean := False;
8117 -- Set True if Expr has at least one Raise_Expression
8119 procedure Add_Call (T : Entity_Id);
8120 -- Includes a call to the predicate function for type T in Expr if T
8121 -- has predicates and Predicate_Function (T) is non-empty.
8123 procedure Add_Predicates;
8124 -- Appends expressions for any Predicate pragmas in the rep item chain
8125 -- Typ to Expr. Note that we look only at items for this exact entity.
8126 -- Inheritance of predicates for the parent type is done by calling the
8127 -- Predicate_Function of the parent type, using Add_Call above.
8129 function Test_RE (N : Node_Id) return Traverse_Result;
8130 -- Used in Test_REs, tests one node for being a raise expression, and if
8131 -- so sets Raise_Expression_Present True.
8133 procedure Test_REs is new Traverse_Proc (Test_RE);
8134 -- Tests to see if Expr contains any raise expressions
8136 function Process_RE (N : Node_Id) return Traverse_Result;
8137 -- Used in Process REs, tests if node N is a raise expression, and if
8138 -- so, marks it to be converted to return False.
8140 procedure Process_REs is new Traverse_Proc (Process_RE);
8141 -- Marks any raise expressions in Expr_M to return False
8143 --------------
8144 -- Add_Call --
8145 --------------
8147 procedure Add_Call (T : Entity_Id) is
8148 Exp : Node_Id;
8150 begin
8151 if Present (T) and then Present (Predicate_Function (T)) then
8152 Set_Has_Predicates (Typ);
8154 -- Build the call to the predicate function of T
8156 Exp :=
8157 Make_Predicate_Call
8158 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8160 -- Add call to evolving expression, using AND THEN if needed
8162 if No (Expr) then
8163 Expr := Exp;
8165 else
8166 Expr :=
8167 Make_And_Then (Sloc (Expr),
8168 Left_Opnd => Relocate_Node (Expr),
8169 Right_Opnd => Exp);
8170 end if;
8172 -- Output info message on inheritance if required. Note we do not
8173 -- give this information for generic actual types, since it is
8174 -- unwelcome noise in that case in instantiations. We also
8175 -- generally suppress the message in instantiations, and also
8176 -- if it involves internal names.
8178 if Opt.List_Inherited_Aspects
8179 and then not Is_Generic_Actual_Type (Typ)
8180 and then Instantiation_Depth (Sloc (Typ)) = 0
8181 and then not Is_Internal_Name (Chars (T))
8182 and then not Is_Internal_Name (Chars (Typ))
8183 then
8184 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8185 Error_Msg_Node_2 := T;
8186 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8187 end if;
8188 end if;
8189 end Add_Call;
8191 --------------------
8192 -- Add_Predicates --
8193 --------------------
8195 procedure Add_Predicates is
8196 Ritem : Node_Id;
8197 Arg1 : Node_Id;
8198 Arg2 : Node_Id;
8200 procedure Replace_Type_Reference (N : Node_Id);
8201 -- Replace a single occurrence N of the subtype name with a reference
8202 -- to the formal of the predicate function. N can be an identifier
8203 -- referencing the subtype, or a selected component, representing an
8204 -- appropriately qualified occurrence of the subtype name.
8206 procedure Replace_Type_References is
8207 new Replace_Type_References_Generic (Replace_Type_Reference);
8208 -- Traverse an expression changing every occurrence of an identifier
8209 -- whose name matches the name of the subtype with a reference to
8210 -- the formal parameter of the predicate function.
8212 ----------------------------
8213 -- Replace_Type_Reference --
8214 ----------------------------
8216 procedure Replace_Type_Reference (N : Node_Id) is
8217 begin
8218 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8219 -- Use the Sloc of the usage name, not the defining name
8221 Set_Etype (N, Typ);
8222 Set_Entity (N, Object_Entity);
8224 -- We want to treat the node as if it comes from source, so that
8225 -- ASIS will not ignore it
8227 Set_Comes_From_Source (N, True);
8228 end Replace_Type_Reference;
8230 -- Start of processing for Add_Predicates
8232 begin
8233 Ritem := First_Rep_Item (Typ);
8234 while Present (Ritem) loop
8235 if Nkind (Ritem) = N_Pragma
8236 and then Pragma_Name (Ritem) = Name_Predicate
8237 then
8238 -- Acquire arguments
8240 Arg1 := First (Pragma_Argument_Associations (Ritem));
8241 Arg2 := Next (Arg1);
8243 Arg1 := Get_Pragma_Arg (Arg1);
8244 Arg2 := Get_Pragma_Arg (Arg2);
8246 -- See if this predicate pragma is for the current type or for
8247 -- its full view. A predicate on a private completion is placed
8248 -- on the partial view beause this is the visible entity that
8249 -- is frozen.
8251 if Entity (Arg1) = Typ
8252 or else Full_View (Entity (Arg1)) = Typ
8253 then
8254 -- We have a match, this entry is for our subtype
8256 -- We need to replace any occurrences of the name of the
8257 -- type with references to the object.
8259 Replace_Type_References (Arg2, Typ);
8261 -- If this predicate comes from an aspect, find the aspect
8262 -- specification, and replace the saved expression because
8263 -- we need the subtype references replaced for the calls to
8264 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8265 -- and Check_Aspect_At_End_Of_Declarations.
8267 if From_Aspect_Specification (Ritem) then
8268 declare
8269 Aitem : Node_Id;
8271 begin
8272 -- Loop to find corresponding aspect, note that this
8273 -- must be present given the pragma is marked delayed.
8275 Aitem := Next_Rep_Item (Ritem);
8276 loop
8277 if Nkind (Aitem) = N_Aspect_Specification
8278 and then Aspect_Rep_Item (Aitem) = Ritem
8279 then
8280 Set_Entity
8281 (Identifier (Aitem), New_Copy_Tree (Arg2));
8282 exit;
8283 end if;
8285 Aitem := Next_Rep_Item (Aitem);
8286 end loop;
8287 end;
8288 end if;
8290 -- Now we can add the expression
8292 if No (Expr) then
8293 Expr := Relocate_Node (Arg2);
8295 -- There already was a predicate, so add to it
8297 else
8298 Expr :=
8299 Make_And_Then (Loc,
8300 Left_Opnd => Relocate_Node (Expr),
8301 Right_Opnd => Relocate_Node (Arg2));
8302 end if;
8303 end if;
8304 end if;
8306 Next_Rep_Item (Ritem);
8307 end loop;
8308 end Add_Predicates;
8310 ----------------
8311 -- Process_RE --
8312 ----------------
8314 function Process_RE (N : Node_Id) return Traverse_Result is
8315 begin
8316 if Nkind (N) = N_Raise_Expression then
8317 Set_Convert_To_Return_False (N);
8318 return Skip;
8319 else
8320 return OK;
8321 end if;
8322 end Process_RE;
8324 -------------
8325 -- Test_RE --
8326 -------------
8328 function Test_RE (N : Node_Id) return Traverse_Result is
8329 begin
8330 if Nkind (N) = N_Raise_Expression then
8331 Raise_Expression_Present := True;
8332 return Abandon;
8333 else
8334 return OK;
8335 end if;
8336 end Test_RE;
8338 -- Start of processing for Build_Predicate_Functions
8340 begin
8341 -- Return if already built or if type does not have predicates
8343 if not Has_Predicates (Typ)
8344 or else Present (Predicate_Function (Typ))
8345 then
8346 return;
8347 end if;
8349 -- Prepare to construct predicate expression
8351 Expr := Empty;
8353 -- Add Predicates for the current type
8355 Add_Predicates;
8357 -- Add predicates for ancestor if present
8359 declare
8360 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8361 begin
8362 if Present (Atyp) then
8363 Add_Call (Atyp);
8364 end if;
8365 end;
8367 -- Case where predicates are present
8369 if Present (Expr) then
8371 -- Test for raise expression present
8373 Test_REs (Expr);
8375 -- If raise expression is present, capture a copy of Expr for use
8376 -- in building the predicateM function version later on. For this
8377 -- copy we replace references to Object_Entity by Object_Entity_M.
8379 if Raise_Expression_Present then
8380 declare
8381 Map : constant Elist_Id := New_Elmt_List;
8382 New_V : Entity_Id := Empty;
8384 -- The unanalyzed expression will be copied and appear in
8385 -- both functions. Normally expressions do not declare new
8386 -- entities, but quantified expressions do, so we need to
8387 -- create new entities for their bound variables, to prevent
8388 -- multiple definitions in gigi.
8390 function Reset_Loop_Variable (N : Node_Id)
8391 return Traverse_Result;
8393 procedure Collect_Loop_Variables is
8394 new Traverse_Proc (Reset_Loop_Variable);
8396 ------------------------
8397 -- Reset_Loop_Variable --
8398 ------------------------
8400 function Reset_Loop_Variable (N : Node_Id)
8401 return Traverse_Result
8403 begin
8404 if Nkind (N) = N_Iterator_Specification then
8405 New_V := Make_Defining_Identifier
8406 (Sloc (N), Chars (Defining_Identifier (N)));
8408 Set_Defining_Identifier (N, New_V);
8409 end if;
8411 return OK;
8412 end Reset_Loop_Variable;
8414 begin
8415 Append_Elmt (Object_Entity, Map);
8416 Append_Elmt (Object_Entity_M, Map);
8417 Expr_M := New_Copy_Tree (Expr, Map => Map);
8418 Collect_Loop_Variables (Expr_M);
8419 end;
8420 end if;
8422 -- Build the main predicate function
8424 declare
8425 SId : constant Entity_Id :=
8426 Make_Defining_Identifier (Loc,
8427 Chars => New_External_Name (Chars (Typ), "Predicate"));
8428 -- The entity for the the function spec
8430 SIdB : constant Entity_Id :=
8431 Make_Defining_Identifier (Loc,
8432 Chars => New_External_Name (Chars (Typ), "Predicate"));
8433 -- The entity for the function body
8435 Spec : Node_Id;
8436 FDecl : Node_Id;
8437 FBody : Node_Id;
8439 begin
8440 -- Build function declaration
8442 Set_Ekind (SId, E_Function);
8443 Set_Is_Internal (SId);
8444 Set_Is_Predicate_Function (SId);
8445 Set_Predicate_Function (Typ, SId);
8447 -- The predicate function is shared between views of a type
8449 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8450 Set_Predicate_Function (Full_View (Typ), SId);
8451 end if;
8453 Spec :=
8454 Make_Function_Specification (Loc,
8455 Defining_Unit_Name => SId,
8456 Parameter_Specifications => New_List (
8457 Make_Parameter_Specification (Loc,
8458 Defining_Identifier => Object_Entity,
8459 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8460 Result_Definition =>
8461 New_Occurrence_Of (Standard_Boolean, Loc));
8463 FDecl :=
8464 Make_Subprogram_Declaration (Loc,
8465 Specification => Spec);
8467 -- Build function body
8469 Spec :=
8470 Make_Function_Specification (Loc,
8471 Defining_Unit_Name => SIdB,
8472 Parameter_Specifications => New_List (
8473 Make_Parameter_Specification (Loc,
8474 Defining_Identifier =>
8475 Make_Defining_Identifier (Loc, Object_Name),
8476 Parameter_Type =>
8477 New_Occurrence_Of (Typ, Loc))),
8478 Result_Definition =>
8479 New_Occurrence_Of (Standard_Boolean, Loc));
8481 FBody :=
8482 Make_Subprogram_Body (Loc,
8483 Specification => Spec,
8484 Declarations => Empty_List,
8485 Handled_Statement_Sequence =>
8486 Make_Handled_Sequence_Of_Statements (Loc,
8487 Statements => New_List (
8488 Make_Simple_Return_Statement (Loc,
8489 Expression => Expr))));
8491 -- Insert declaration before freeze node and body after
8493 Insert_Before_And_Analyze (N, FDecl);
8494 Insert_After_And_Analyze (N, FBody);
8495 end;
8497 -- Test for raise expressions present and if so build M version
8499 if Raise_Expression_Present then
8500 declare
8501 SId : constant Entity_Id :=
8502 Make_Defining_Identifier (Loc,
8503 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8504 -- The entity for the the function spec
8506 SIdB : constant Entity_Id :=
8507 Make_Defining_Identifier (Loc,
8508 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8509 -- The entity for the function body
8511 Spec : Node_Id;
8512 FDecl : Node_Id;
8513 FBody : Node_Id;
8514 BTemp : Entity_Id;
8516 begin
8517 -- Mark any raise expressions for special expansion
8519 Process_REs (Expr_M);
8521 -- Build function declaration
8523 Set_Ekind (SId, E_Function);
8524 Set_Is_Predicate_Function_M (SId);
8525 Set_Predicate_Function_M (Typ, SId);
8527 -- The predicate function is shared between views of a type
8529 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8530 Set_Predicate_Function_M (Full_View (Typ), SId);
8531 end if;
8533 Spec :=
8534 Make_Function_Specification (Loc,
8535 Defining_Unit_Name => SId,
8536 Parameter_Specifications => New_List (
8537 Make_Parameter_Specification (Loc,
8538 Defining_Identifier => Object_Entity_M,
8539 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8540 Result_Definition =>
8541 New_Occurrence_Of (Standard_Boolean, Loc));
8543 FDecl :=
8544 Make_Subprogram_Declaration (Loc,
8545 Specification => Spec);
8547 -- Build function body
8549 Spec :=
8550 Make_Function_Specification (Loc,
8551 Defining_Unit_Name => SIdB,
8552 Parameter_Specifications => New_List (
8553 Make_Parameter_Specification (Loc,
8554 Defining_Identifier =>
8555 Make_Defining_Identifier (Loc, Object_Name),
8556 Parameter_Type =>
8557 New_Occurrence_Of (Typ, Loc))),
8558 Result_Definition =>
8559 New_Occurrence_Of (Standard_Boolean, Loc));
8561 -- Build the body, we declare the boolean expression before
8562 -- doing the return, because we are not really confident of
8563 -- what happens if a return appears within a return.
8565 BTemp :=
8566 Make_Defining_Identifier (Loc,
8567 Chars => New_Internal_Name ('B'));
8569 FBody :=
8570 Make_Subprogram_Body (Loc,
8571 Specification => Spec,
8573 Declarations => New_List (
8574 Make_Object_Declaration (Loc,
8575 Defining_Identifier => BTemp,
8576 Constant_Present => True,
8577 Object_Definition =>
8578 New_Occurrence_Of (Standard_Boolean, Loc),
8579 Expression => Expr_M)),
8581 Handled_Statement_Sequence =>
8582 Make_Handled_Sequence_Of_Statements (Loc,
8583 Statements => New_List (
8584 Make_Simple_Return_Statement (Loc,
8585 Expression => New_Occurrence_Of (BTemp, Loc)))));
8587 -- Insert declaration before freeze node and body after
8589 Insert_Before_And_Analyze (N, FDecl);
8590 Insert_After_And_Analyze (N, FBody);
8591 end;
8592 end if;
8594 -- See if we have a static predicate. Note that the answer may be
8595 -- yes even if we have an explicit Dynamic_Predicate present.
8597 declare
8598 PS : Boolean;
8599 EN : Node_Id;
8601 begin
8602 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8603 PS := False;
8604 else
8605 PS := Is_Predicate_Static (Expr, Object_Name);
8606 end if;
8608 -- Case where we have a predicate-static aspect
8610 if PS then
8612 -- We don't set Has_Static_Predicate_Aspect, since we can have
8613 -- any of the three cases (Predicate, Dynamic_Predicate, or
8614 -- Static_Predicate) generating a predicate with an expression
8615 -- that is predicate-static. We just indicate that we have a
8616 -- predicate that can be treated as static.
8618 Set_Has_Static_Predicate (Typ);
8620 -- For discrete subtype, build the static predicate list
8622 if Is_Discrete_Type (Typ) then
8623 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8625 -- If we don't get a static predicate list, it means that we
8626 -- have a case where this is not possible, most typically in
8627 -- the case where we inherit a dynamic predicate. We do not
8628 -- consider this an error, we just leave the predicate as
8629 -- dynamic. But if we do succeed in building the list, then
8630 -- we mark the predicate as static.
8632 if No (Static_Discrete_Predicate (Typ)) then
8633 Set_Has_Static_Predicate (Typ, False);
8634 end if;
8636 -- For real or string subtype, save predicate expression
8638 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8639 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8640 end if;
8642 -- Case of dynamic predicate (expression is not predicate-static)
8644 else
8645 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8646 -- is only set if we have an explicit Dynamic_Predicate aspect
8647 -- given. Here we may simply have a Predicate aspect where the
8648 -- expression happens not to be predicate-static.
8650 -- Emit an error when the predicate is categorized as static
8651 -- but its expression is not predicate-static.
8653 -- First a little fiddling to get a nice location for the
8654 -- message. If the expression is of the form (A and then B),
8655 -- then use the left operand for the Sloc. This avoids getting
8656 -- confused by a call to a higher-level predicate with a less
8657 -- convenient source location.
8659 EN := Expr;
8660 while Nkind (EN) = N_And_Then loop
8661 EN := Left_Opnd (EN);
8662 end loop;
8664 -- Now post appropriate message
8666 if Has_Static_Predicate_Aspect (Typ) then
8667 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8668 Error_Msg_F
8669 ("expression is not predicate-static (RM 3.2.4(16-22))",
8670 EN);
8671 else
8672 Error_Msg_F
8673 ("static predicate requires scalar or string type", EN);
8674 end if;
8675 end if;
8676 end if;
8677 end;
8678 end if;
8679 end Build_Predicate_Functions;
8681 -----------------------------------------
8682 -- Check_Aspect_At_End_Of_Declarations --
8683 -----------------------------------------
8685 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8686 Ent : constant Entity_Id := Entity (ASN);
8687 Ident : constant Node_Id := Identifier (ASN);
8688 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8690 End_Decl_Expr : constant Node_Id := Entity (Ident);
8691 -- Expression to be analyzed at end of declarations
8693 Freeze_Expr : constant Node_Id := Expression (ASN);
8694 -- Expression from call to Check_Aspect_At_Freeze_Point
8696 T : constant Entity_Id := Etype (Freeze_Expr);
8697 -- Type required for preanalyze call
8699 Err : Boolean;
8700 -- Set False if error
8702 -- On entry to this procedure, Entity (Ident) contains a copy of the
8703 -- original expression from the aspect, saved for this purpose, and
8704 -- but Expression (Ident) is a preanalyzed copy of the expression,
8705 -- preanalyzed just after the freeze point.
8707 procedure Check_Overloaded_Name;
8708 -- For aspects whose expression is simply a name, this routine checks if
8709 -- the name is overloaded or not. If so, it verifies there is an
8710 -- interpretation that matches the entity obtained at the freeze point,
8711 -- otherwise the compiler complains.
8713 ---------------------------
8714 -- Check_Overloaded_Name --
8715 ---------------------------
8717 procedure Check_Overloaded_Name is
8718 begin
8719 if not Is_Overloaded (End_Decl_Expr) then
8720 Err := not Is_Entity_Name (End_Decl_Expr)
8721 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8723 else
8724 Err := True;
8726 declare
8727 Index : Interp_Index;
8728 It : Interp;
8730 begin
8731 Get_First_Interp (End_Decl_Expr, Index, It);
8732 while Present (It.Typ) loop
8733 if It.Nam = Entity (Freeze_Expr) then
8734 Err := False;
8735 exit;
8736 end if;
8738 Get_Next_Interp (Index, It);
8739 end loop;
8740 end;
8741 end if;
8742 end Check_Overloaded_Name;
8744 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8746 begin
8747 -- Case of aspects Dimension, Dimension_System and Synchronization
8749 if A_Id = Aspect_Synchronization then
8750 return;
8752 -- Case of stream attributes, just have to compare entities. However,
8753 -- the expression is just a name (possibly overloaded), and there may
8754 -- be stream operations declared for unrelated types, so we just need
8755 -- to verify that one of these interpretations is the one available at
8756 -- at the freeze point.
8758 elsif A_Id = Aspect_Input or else
8759 A_Id = Aspect_Output or else
8760 A_Id = Aspect_Read or else
8761 A_Id = Aspect_Write
8762 then
8763 Analyze (End_Decl_Expr);
8764 Check_Overloaded_Name;
8766 elsif A_Id = Aspect_Variable_Indexing or else
8767 A_Id = Aspect_Constant_Indexing or else
8768 A_Id = Aspect_Default_Iterator or else
8769 A_Id = Aspect_Iterator_Element
8770 then
8771 -- Make type unfrozen before analysis, to prevent spurious errors
8772 -- about late attributes.
8774 Set_Is_Frozen (Ent, False);
8775 Analyze (End_Decl_Expr);
8776 Set_Is_Frozen (Ent, True);
8778 -- If the end of declarations comes before any other freeze
8779 -- point, the Freeze_Expr is not analyzed: no check needed.
8781 if Analyzed (Freeze_Expr) and then not In_Instance then
8782 Check_Overloaded_Name;
8783 else
8784 Err := False;
8785 end if;
8787 -- All other cases
8789 else
8790 -- Indicate that the expression comes from an aspect specification,
8791 -- which is used in subsequent analysis even if expansion is off.
8793 Set_Parent (End_Decl_Expr, ASN);
8795 -- In a generic context the aspect expressions have not been
8796 -- preanalyzed, so do it now. There are no conformance checks
8797 -- to perform in this case.
8799 if No (T) then
8800 Check_Aspect_At_Freeze_Point (ASN);
8801 return;
8803 -- The default values attributes may be defined in the private part,
8804 -- and the analysis of the expression may take place when only the
8805 -- partial view is visible. The expression must be scalar, so use
8806 -- the full view to resolve.
8808 elsif (A_Id = Aspect_Default_Value
8809 or else
8810 A_Id = Aspect_Default_Component_Value)
8811 and then Is_Private_Type (T)
8812 then
8813 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8815 else
8816 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8817 end if;
8819 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8820 end if;
8822 -- Output error message if error. Force error on aspect specification
8823 -- even if there is an error on the expression itself.
8825 if Err then
8826 Error_Msg_NE
8827 ("!visibility of aspect for& changes after freeze point",
8828 ASN, Ent);
8829 Error_Msg_NE
8830 ("info: & is frozen here, aspects evaluated at this point??",
8831 Freeze_Node (Ent), Ent);
8832 end if;
8833 end Check_Aspect_At_End_Of_Declarations;
8835 ----------------------------------
8836 -- Check_Aspect_At_Freeze_Point --
8837 ----------------------------------
8839 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8840 Ident : constant Node_Id := Identifier (ASN);
8841 -- Identifier (use Entity field to save expression)
8843 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8845 T : Entity_Id := Empty;
8846 -- Type required for preanalyze call
8848 begin
8849 -- On entry to this procedure, Entity (Ident) contains a copy of the
8850 -- original expression from the aspect, saved for this purpose.
8852 -- On exit from this procedure Entity (Ident) is unchanged, still
8853 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8854 -- of the expression, preanalyzed just after the freeze point.
8856 -- Make a copy of the expression to be preanalyzed
8858 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8860 -- Find type for preanalyze call
8862 case A_Id is
8864 -- No_Aspect should be impossible
8866 when No_Aspect =>
8867 raise Program_Error;
8869 -- Aspects taking an optional boolean argument
8871 when Boolean_Aspects |
8872 Library_Unit_Aspects =>
8874 T := Standard_Boolean;
8876 -- Aspects corresponding to attribute definition clauses
8878 when Aspect_Address =>
8879 T := RTE (RE_Address);
8881 when Aspect_Attach_Handler =>
8882 T := RTE (RE_Interrupt_ID);
8884 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8885 T := RTE (RE_Bit_Order);
8887 when Aspect_Convention =>
8888 return;
8890 when Aspect_CPU =>
8891 T := RTE (RE_CPU_Range);
8893 -- Default_Component_Value is resolved with the component type
8895 when Aspect_Default_Component_Value =>
8896 T := Component_Type (Entity (ASN));
8898 when Aspect_Default_Storage_Pool =>
8899 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8901 -- Default_Value is resolved with the type entity in question
8903 when Aspect_Default_Value =>
8904 T := Entity (ASN);
8906 when Aspect_Dispatching_Domain =>
8907 T := RTE (RE_Dispatching_Domain);
8909 when Aspect_External_Tag =>
8910 T := Standard_String;
8912 when Aspect_External_Name =>
8913 T := Standard_String;
8915 when Aspect_Link_Name =>
8916 T := Standard_String;
8918 when Aspect_Priority | Aspect_Interrupt_Priority =>
8919 T := Standard_Integer;
8921 when Aspect_Relative_Deadline =>
8922 T := RTE (RE_Time_Span);
8924 when Aspect_Small =>
8925 T := Universal_Real;
8927 -- For a simple storage pool, we have to retrieve the type of the
8928 -- pool object associated with the aspect's corresponding attribute
8929 -- definition clause.
8931 when Aspect_Simple_Storage_Pool =>
8932 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8934 when Aspect_Storage_Pool =>
8935 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8937 when Aspect_Alignment |
8938 Aspect_Component_Size |
8939 Aspect_Machine_Radix |
8940 Aspect_Object_Size |
8941 Aspect_Size |
8942 Aspect_Storage_Size |
8943 Aspect_Stream_Size |
8944 Aspect_Value_Size =>
8945 T := Any_Integer;
8947 when Aspect_Linker_Section =>
8948 T := Standard_String;
8950 when Aspect_Synchronization =>
8951 return;
8953 -- Special case, the expression of these aspects is just an entity
8954 -- that does not need any resolution, so just analyze.
8956 when Aspect_Input |
8957 Aspect_Output |
8958 Aspect_Read |
8959 Aspect_Suppress |
8960 Aspect_Unsuppress |
8961 Aspect_Warnings |
8962 Aspect_Write =>
8963 Analyze (Expression (ASN));
8964 return;
8966 -- Same for Iterator aspects, where the expression is a function
8967 -- name. Legality rules are checked separately.
8969 when Aspect_Constant_Indexing |
8970 Aspect_Default_Iterator |
8971 Aspect_Iterator_Element |
8972 Aspect_Variable_Indexing =>
8973 Analyze (Expression (ASN));
8974 return;
8976 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8978 when Aspect_Iterable =>
8979 T := Entity (ASN);
8981 declare
8982 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8983 Assoc : Node_Id;
8984 Expr : Node_Id;
8986 begin
8987 if Cursor = Any_Type then
8988 return;
8989 end if;
8991 Assoc := First (Component_Associations (Expression (ASN)));
8992 while Present (Assoc) loop
8993 Expr := Expression (Assoc);
8994 Analyze (Expr);
8996 if not Error_Posted (Expr) then
8997 Resolve_Iterable_Operation
8998 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8999 end if;
9001 Next (Assoc);
9002 end loop;
9003 end;
9005 return;
9007 -- Invariant/Predicate take boolean expressions
9009 when Aspect_Dynamic_Predicate |
9010 Aspect_Invariant |
9011 Aspect_Predicate |
9012 Aspect_Static_Predicate |
9013 Aspect_Type_Invariant =>
9014 T := Standard_Boolean;
9016 -- Here is the list of aspects that don't require delay analysis
9018 when Aspect_Abstract_State |
9019 Aspect_Annotate |
9020 Aspect_Contract_Cases |
9021 Aspect_Default_Initial_Condition |
9022 Aspect_Depends |
9023 Aspect_Dimension |
9024 Aspect_Dimension_System |
9025 Aspect_Extensions_Visible |
9026 Aspect_Ghost |
9027 Aspect_Global |
9028 Aspect_Implicit_Dereference |
9029 Aspect_Initial_Condition |
9030 Aspect_Initializes |
9031 Aspect_Obsolescent |
9032 Aspect_Part_Of |
9033 Aspect_Post |
9034 Aspect_Postcondition |
9035 Aspect_Pre |
9036 Aspect_Precondition |
9037 Aspect_Refined_Depends |
9038 Aspect_Refined_Global |
9039 Aspect_Refined_Post |
9040 Aspect_Refined_State |
9041 Aspect_SPARK_Mode |
9042 Aspect_Test_Case |
9043 Aspect_Unimplemented =>
9044 raise Program_Error;
9046 end case;
9048 -- Do the preanalyze call
9050 Preanalyze_Spec_Expression (Expression (ASN), T);
9051 end Check_Aspect_At_Freeze_Point;
9053 -----------------------------------
9054 -- Check_Constant_Address_Clause --
9055 -----------------------------------
9057 procedure Check_Constant_Address_Clause
9058 (Expr : Node_Id;
9059 U_Ent : Entity_Id)
9061 procedure Check_At_Constant_Address (Nod : Node_Id);
9062 -- Checks that the given node N represents a name whose 'Address is
9063 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9064 -- address value is the same at the point of declaration of U_Ent and at
9065 -- the time of elaboration of the address clause.
9067 procedure Check_Expr_Constants (Nod : Node_Id);
9068 -- Checks that Nod meets the requirements for a constant address clause
9069 -- in the sense of the enclosing procedure.
9071 procedure Check_List_Constants (Lst : List_Id);
9072 -- Check that all elements of list Lst meet the requirements for a
9073 -- constant address clause in the sense of the enclosing procedure.
9075 -------------------------------
9076 -- Check_At_Constant_Address --
9077 -------------------------------
9079 procedure Check_At_Constant_Address (Nod : Node_Id) is
9080 begin
9081 if Is_Entity_Name (Nod) then
9082 if Present (Address_Clause (Entity ((Nod)))) then
9083 Error_Msg_NE
9084 ("invalid address clause for initialized object &!",
9085 Nod, U_Ent);
9086 Error_Msg_NE
9087 ("address for& cannot" &
9088 " depend on another address clause! (RM 13.1(22))!",
9089 Nod, U_Ent);
9091 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9092 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9093 then
9094 Error_Msg_NE
9095 ("invalid address clause for initialized object &!",
9096 Nod, U_Ent);
9097 Error_Msg_Node_2 := U_Ent;
9098 Error_Msg_NE
9099 ("\& must be defined before & (RM 13.1(22))!",
9100 Nod, Entity (Nod));
9101 end if;
9103 elsif Nkind (Nod) = N_Selected_Component then
9104 declare
9105 T : constant Entity_Id := Etype (Prefix (Nod));
9107 begin
9108 if (Is_Record_Type (T)
9109 and then Has_Discriminants (T))
9110 or else
9111 (Is_Access_Type (T)
9112 and then Is_Record_Type (Designated_Type (T))
9113 and then Has_Discriminants (Designated_Type (T)))
9114 then
9115 Error_Msg_NE
9116 ("invalid address clause for initialized object &!",
9117 Nod, U_Ent);
9118 Error_Msg_N
9119 ("\address cannot depend on component" &
9120 " of discriminated record (RM 13.1(22))!",
9121 Nod);
9122 else
9123 Check_At_Constant_Address (Prefix (Nod));
9124 end if;
9125 end;
9127 elsif Nkind (Nod) = N_Indexed_Component then
9128 Check_At_Constant_Address (Prefix (Nod));
9129 Check_List_Constants (Expressions (Nod));
9131 else
9132 Check_Expr_Constants (Nod);
9133 end if;
9134 end Check_At_Constant_Address;
9136 --------------------------
9137 -- Check_Expr_Constants --
9138 --------------------------
9140 procedure Check_Expr_Constants (Nod : Node_Id) is
9141 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9142 Ent : Entity_Id := Empty;
9144 begin
9145 if Nkind (Nod) in N_Has_Etype
9146 and then Etype (Nod) = Any_Type
9147 then
9148 return;
9149 end if;
9151 case Nkind (Nod) is
9152 when N_Empty | N_Error =>
9153 return;
9155 when N_Identifier | N_Expanded_Name =>
9156 Ent := Entity (Nod);
9158 -- We need to look at the original node if it is different
9159 -- from the node, since we may have rewritten things and
9160 -- substituted an identifier representing the rewrite.
9162 if Original_Node (Nod) /= Nod then
9163 Check_Expr_Constants (Original_Node (Nod));
9165 -- If the node is an object declaration without initial
9166 -- value, some code has been expanded, and the expression
9167 -- is not constant, even if the constituents might be
9168 -- acceptable, as in A'Address + offset.
9170 if Ekind (Ent) = E_Variable
9171 and then
9172 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9173 and then
9174 No (Expression (Declaration_Node (Ent)))
9175 then
9176 Error_Msg_NE
9177 ("invalid address clause for initialized object &!",
9178 Nod, U_Ent);
9180 -- If entity is constant, it may be the result of expanding
9181 -- a check. We must verify that its declaration appears
9182 -- before the object in question, else we also reject the
9183 -- address clause.
9185 elsif Ekind (Ent) = E_Constant
9186 and then In_Same_Source_Unit (Ent, U_Ent)
9187 and then Sloc (Ent) > Loc_U_Ent
9188 then
9189 Error_Msg_NE
9190 ("invalid address clause for initialized object &!",
9191 Nod, U_Ent);
9192 end if;
9194 return;
9195 end if;
9197 -- Otherwise look at the identifier and see if it is OK
9199 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9200 or else Is_Type (Ent)
9201 then
9202 return;
9204 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9206 -- This is the case where we must have Ent defined before
9207 -- U_Ent. Clearly if they are in different units this
9208 -- requirement is met since the unit containing Ent is
9209 -- already processed.
9211 if not In_Same_Source_Unit (Ent, U_Ent) then
9212 return;
9214 -- Otherwise location of Ent must be before the location
9215 -- of U_Ent, that's what prior defined means.
9217 elsif Sloc (Ent) < Loc_U_Ent then
9218 return;
9220 else
9221 Error_Msg_NE
9222 ("invalid address clause for initialized object &!",
9223 Nod, U_Ent);
9224 Error_Msg_Node_2 := U_Ent;
9225 Error_Msg_NE
9226 ("\& must be defined before & (RM 13.1(22))!",
9227 Nod, Ent);
9228 end if;
9230 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9231 Check_Expr_Constants (Original_Node (Nod));
9233 else
9234 Error_Msg_NE
9235 ("invalid address clause for initialized object &!",
9236 Nod, U_Ent);
9238 if Comes_From_Source (Ent) then
9239 Error_Msg_NE
9240 ("\reference to variable& not allowed"
9241 & " (RM 13.1(22))!", Nod, Ent);
9242 else
9243 Error_Msg_N
9244 ("non-static expression not allowed"
9245 & " (RM 13.1(22))!", Nod);
9246 end if;
9247 end if;
9249 when N_Integer_Literal =>
9251 -- If this is a rewritten unchecked conversion, in a system
9252 -- where Address is an integer type, always use the base type
9253 -- for a literal value. This is user-friendly and prevents
9254 -- order-of-elaboration issues with instances of unchecked
9255 -- conversion.
9257 if Nkind (Original_Node (Nod)) = N_Function_Call then
9258 Set_Etype (Nod, Base_Type (Etype (Nod)));
9259 end if;
9261 when N_Real_Literal |
9262 N_String_Literal |
9263 N_Character_Literal =>
9264 return;
9266 when N_Range =>
9267 Check_Expr_Constants (Low_Bound (Nod));
9268 Check_Expr_Constants (High_Bound (Nod));
9270 when N_Explicit_Dereference =>
9271 Check_Expr_Constants (Prefix (Nod));
9273 when N_Indexed_Component =>
9274 Check_Expr_Constants (Prefix (Nod));
9275 Check_List_Constants (Expressions (Nod));
9277 when N_Slice =>
9278 Check_Expr_Constants (Prefix (Nod));
9279 Check_Expr_Constants (Discrete_Range (Nod));
9281 when N_Selected_Component =>
9282 Check_Expr_Constants (Prefix (Nod));
9284 when N_Attribute_Reference =>
9285 if Nam_In (Attribute_Name (Nod), Name_Address,
9286 Name_Access,
9287 Name_Unchecked_Access,
9288 Name_Unrestricted_Access)
9289 then
9290 Check_At_Constant_Address (Prefix (Nod));
9292 else
9293 Check_Expr_Constants (Prefix (Nod));
9294 Check_List_Constants (Expressions (Nod));
9295 end if;
9297 when N_Aggregate =>
9298 Check_List_Constants (Component_Associations (Nod));
9299 Check_List_Constants (Expressions (Nod));
9301 when N_Component_Association =>
9302 Check_Expr_Constants (Expression (Nod));
9304 when N_Extension_Aggregate =>
9305 Check_Expr_Constants (Ancestor_Part (Nod));
9306 Check_List_Constants (Component_Associations (Nod));
9307 Check_List_Constants (Expressions (Nod));
9309 when N_Null =>
9310 return;
9312 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9313 Check_Expr_Constants (Left_Opnd (Nod));
9314 Check_Expr_Constants (Right_Opnd (Nod));
9316 when N_Unary_Op =>
9317 Check_Expr_Constants (Right_Opnd (Nod));
9319 when N_Type_Conversion |
9320 N_Qualified_Expression |
9321 N_Allocator |
9322 N_Unchecked_Type_Conversion =>
9323 Check_Expr_Constants (Expression (Nod));
9325 when N_Function_Call =>
9326 if not Is_Pure (Entity (Name (Nod))) then
9327 Error_Msg_NE
9328 ("invalid address clause for initialized object &!",
9329 Nod, U_Ent);
9331 Error_Msg_NE
9332 ("\function & is not pure (RM 13.1(22))!",
9333 Nod, Entity (Name (Nod)));
9335 else
9336 Check_List_Constants (Parameter_Associations (Nod));
9337 end if;
9339 when N_Parameter_Association =>
9340 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9342 when others =>
9343 Error_Msg_NE
9344 ("invalid address clause for initialized object &!",
9345 Nod, U_Ent);
9346 Error_Msg_NE
9347 ("\must be constant defined before& (RM 13.1(22))!",
9348 Nod, U_Ent);
9349 end case;
9350 end Check_Expr_Constants;
9352 --------------------------
9353 -- Check_List_Constants --
9354 --------------------------
9356 procedure Check_List_Constants (Lst : List_Id) is
9357 Nod1 : Node_Id;
9359 begin
9360 if Present (Lst) then
9361 Nod1 := First (Lst);
9362 while Present (Nod1) loop
9363 Check_Expr_Constants (Nod1);
9364 Next (Nod1);
9365 end loop;
9366 end if;
9367 end Check_List_Constants;
9369 -- Start of processing for Check_Constant_Address_Clause
9371 begin
9372 -- If rep_clauses are to be ignored, no need for legality checks. In
9373 -- particular, no need to pester user about rep clauses that violate the
9374 -- rule on constant addresses, given that these clauses will be removed
9375 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9376 -- we want to relax these checks.
9378 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9379 Check_Expr_Constants (Expr);
9380 end if;
9381 end Check_Constant_Address_Clause;
9383 ---------------------------
9384 -- Check_Pool_Size_Clash --
9385 ---------------------------
9387 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9388 Post : Node_Id;
9390 begin
9391 -- We need to find out which one came first. Note that in the case of
9392 -- aspects mixed with pragmas there are cases where the processing order
9393 -- is reversed, which is why we do the check here.
9395 if Sloc (SP) < Sloc (SS) then
9396 Error_Msg_Sloc := Sloc (SP);
9397 Post := SS;
9398 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9400 else
9401 Error_Msg_Sloc := Sloc (SS);
9402 Post := SP;
9403 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9404 end if;
9406 Error_Msg_N
9407 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9408 end Check_Pool_Size_Clash;
9410 ----------------------------------------
9411 -- Check_Record_Representation_Clause --
9412 ----------------------------------------
9414 procedure Check_Record_Representation_Clause (N : Node_Id) is
9415 Loc : constant Source_Ptr := Sloc (N);
9416 Ident : constant Node_Id := Identifier (N);
9417 Rectype : Entity_Id;
9418 Fent : Entity_Id;
9419 CC : Node_Id;
9420 Fbit : Uint;
9421 Lbit : Uint;
9422 Hbit : Uint := Uint_0;
9423 Comp : Entity_Id;
9424 Pcomp : Entity_Id;
9426 Max_Bit_So_Far : Uint;
9427 -- Records the maximum bit position so far. If all field positions
9428 -- are monotonically increasing, then we can skip the circuit for
9429 -- checking for overlap, since no overlap is possible.
9431 Tagged_Parent : Entity_Id := Empty;
9432 -- This is set in the case of a derived tagged type for which we have
9433 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9434 -- positioned by record representation clauses). In this case we must
9435 -- check for overlap between components of this tagged type, and the
9436 -- components of its parent. Tagged_Parent will point to this parent
9437 -- type. For all other cases Tagged_Parent is left set to Empty.
9439 Parent_Last_Bit : Uint;
9440 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9441 -- last bit position for any field in the parent type. We only need to
9442 -- check overlap for fields starting below this point.
9444 Overlap_Check_Required : Boolean;
9445 -- Used to keep track of whether or not an overlap check is required
9447 Overlap_Detected : Boolean := False;
9448 -- Set True if an overlap is detected
9450 Ccount : Natural := 0;
9451 -- Number of component clauses in record rep clause
9453 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9454 -- Given two entities for record components or discriminants, checks
9455 -- if they have overlapping component clauses and issues errors if so.
9457 procedure Find_Component;
9458 -- Finds component entity corresponding to current component clause (in
9459 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9460 -- start/stop bits for the field. If there is no matching component or
9461 -- if the matching component does not have a component clause, then
9462 -- that's an error and Comp is set to Empty, but no error message is
9463 -- issued, since the message was already given. Comp is also set to
9464 -- Empty if the current "component clause" is in fact a pragma.
9466 -----------------------------
9467 -- Check_Component_Overlap --
9468 -----------------------------
9470 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9471 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9472 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9474 begin
9475 if Present (CC1) and then Present (CC2) then
9477 -- Exclude odd case where we have two tag components in the same
9478 -- record, both at location zero. This seems a bit strange, but
9479 -- it seems to happen in some circumstances, perhaps on an error.
9481 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9482 return;
9483 end if;
9485 -- Here we check if the two fields overlap
9487 declare
9488 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9489 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9490 E1 : constant Uint := S1 + Esize (C1_Ent);
9491 E2 : constant Uint := S2 + Esize (C2_Ent);
9493 begin
9494 if E2 <= S1 or else E1 <= S2 then
9495 null;
9496 else
9497 Error_Msg_Node_2 := Component_Name (CC2);
9498 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9499 Error_Msg_Node_1 := Component_Name (CC1);
9500 Error_Msg_N
9501 ("component& overlaps & #", Component_Name (CC1));
9502 Overlap_Detected := True;
9503 end if;
9504 end;
9505 end if;
9506 end Check_Component_Overlap;
9508 --------------------
9509 -- Find_Component --
9510 --------------------
9512 procedure Find_Component is
9514 procedure Search_Component (R : Entity_Id);
9515 -- Search components of R for a match. If found, Comp is set
9517 ----------------------
9518 -- Search_Component --
9519 ----------------------
9521 procedure Search_Component (R : Entity_Id) is
9522 begin
9523 Comp := First_Component_Or_Discriminant (R);
9524 while Present (Comp) loop
9526 -- Ignore error of attribute name for component name (we
9527 -- already gave an error message for this, so no need to
9528 -- complain here)
9530 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9531 null;
9532 else
9533 exit when Chars (Comp) = Chars (Component_Name (CC));
9534 end if;
9536 Next_Component_Or_Discriminant (Comp);
9537 end loop;
9538 end Search_Component;
9540 -- Start of processing for Find_Component
9542 begin
9543 -- Return with Comp set to Empty if we have a pragma
9545 if Nkind (CC) = N_Pragma then
9546 Comp := Empty;
9547 return;
9548 end if;
9550 -- Search current record for matching component
9552 Search_Component (Rectype);
9554 -- If not found, maybe component of base type discriminant that is
9555 -- absent from statically constrained first subtype.
9557 if No (Comp) then
9558 Search_Component (Base_Type (Rectype));
9559 end if;
9561 -- If no component, or the component does not reference the component
9562 -- clause in question, then there was some previous error for which
9563 -- we already gave a message, so just return with Comp Empty.
9565 if No (Comp) or else Component_Clause (Comp) /= CC then
9566 Check_Error_Detected;
9567 Comp := Empty;
9569 -- Normal case where we have a component clause
9571 else
9572 Fbit := Component_Bit_Offset (Comp);
9573 Lbit := Fbit + Esize (Comp) - 1;
9574 end if;
9575 end Find_Component;
9577 -- Start of processing for Check_Record_Representation_Clause
9579 begin
9580 Find_Type (Ident);
9581 Rectype := Entity (Ident);
9583 if Rectype = Any_Type then
9584 return;
9585 else
9586 Rectype := Underlying_Type (Rectype);
9587 end if;
9589 -- See if we have a fully repped derived tagged type
9591 declare
9592 PS : constant Entity_Id := Parent_Subtype (Rectype);
9594 begin
9595 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9596 Tagged_Parent := PS;
9598 -- Find maximum bit of any component of the parent type
9600 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9601 Pcomp := First_Entity (Tagged_Parent);
9602 while Present (Pcomp) loop
9603 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9604 if Component_Bit_Offset (Pcomp) /= No_Uint
9605 and then Known_Static_Esize (Pcomp)
9606 then
9607 Parent_Last_Bit :=
9608 UI_Max
9609 (Parent_Last_Bit,
9610 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9611 end if;
9613 Next_Entity (Pcomp);
9614 end if;
9615 end loop;
9616 end if;
9617 end;
9619 -- All done if no component clauses
9621 CC := First (Component_Clauses (N));
9623 if No (CC) then
9624 return;
9625 end if;
9627 -- If a tag is present, then create a component clause that places it
9628 -- at the start of the record (otherwise gigi may place it after other
9629 -- fields that have rep clauses).
9631 Fent := First_Entity (Rectype);
9633 if Nkind (Fent) = N_Defining_Identifier
9634 and then Chars (Fent) = Name_uTag
9635 then
9636 Set_Component_Bit_Offset (Fent, Uint_0);
9637 Set_Normalized_Position (Fent, Uint_0);
9638 Set_Normalized_First_Bit (Fent, Uint_0);
9639 Set_Normalized_Position_Max (Fent, Uint_0);
9640 Init_Esize (Fent, System_Address_Size);
9642 Set_Component_Clause (Fent,
9643 Make_Component_Clause (Loc,
9644 Component_Name => Make_Identifier (Loc, Name_uTag),
9646 Position => Make_Integer_Literal (Loc, Uint_0),
9647 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9648 Last_Bit =>
9649 Make_Integer_Literal (Loc,
9650 UI_From_Int (System_Address_Size))));
9652 Ccount := Ccount + 1;
9653 end if;
9655 Max_Bit_So_Far := Uint_Minus_1;
9656 Overlap_Check_Required := False;
9658 -- Process the component clauses
9660 while Present (CC) loop
9661 Find_Component;
9663 if Present (Comp) then
9664 Ccount := Ccount + 1;
9666 -- We need a full overlap check if record positions non-monotonic
9668 if Fbit <= Max_Bit_So_Far then
9669 Overlap_Check_Required := True;
9670 end if;
9672 Max_Bit_So_Far := Lbit;
9674 -- Check bit position out of range of specified size
9676 if Has_Size_Clause (Rectype)
9677 and then RM_Size (Rectype) <= Lbit
9678 then
9679 Error_Msg_N
9680 ("bit number out of range of specified size",
9681 Last_Bit (CC));
9683 -- Check for overlap with tag component
9685 else
9686 if Is_Tagged_Type (Rectype)
9687 and then Fbit < System_Address_Size
9688 then
9689 Error_Msg_NE
9690 ("component overlaps tag field of&",
9691 Component_Name (CC), Rectype);
9692 Overlap_Detected := True;
9693 end if;
9695 if Hbit < Lbit then
9696 Hbit := Lbit;
9697 end if;
9698 end if;
9700 -- Check parent overlap if component might overlap parent field
9702 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9703 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9704 while Present (Pcomp) loop
9705 if not Is_Tag (Pcomp)
9706 and then Chars (Pcomp) /= Name_uParent
9707 then
9708 Check_Component_Overlap (Comp, Pcomp);
9709 end if;
9711 Next_Component_Or_Discriminant (Pcomp);
9712 end loop;
9713 end if;
9714 end if;
9716 Next (CC);
9717 end loop;
9719 -- Now that we have processed all the component clauses, check for
9720 -- overlap. We have to leave this till last, since the components can
9721 -- appear in any arbitrary order in the representation clause.
9723 -- We do not need this check if all specified ranges were monotonic,
9724 -- as recorded by Overlap_Check_Required being False at this stage.
9726 -- This first section checks if there are any overlapping entries at
9727 -- all. It does this by sorting all entries and then seeing if there are
9728 -- any overlaps. If there are none, then that is decisive, but if there
9729 -- are overlaps, they may still be OK (they may result from fields in
9730 -- different variants).
9732 if Overlap_Check_Required then
9733 Overlap_Check1 : declare
9735 OC_Fbit : array (0 .. Ccount) of Uint;
9736 -- First-bit values for component clauses, the value is the offset
9737 -- of the first bit of the field from start of record. The zero
9738 -- entry is for use in sorting.
9740 OC_Lbit : array (0 .. Ccount) of Uint;
9741 -- Last-bit values for component clauses, the value is the offset
9742 -- of the last bit of the field from start of record. The zero
9743 -- entry is for use in sorting.
9745 OC_Count : Natural := 0;
9746 -- Count of entries in OC_Fbit and OC_Lbit
9748 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9749 -- Compare routine for Sort
9751 procedure OC_Move (From : Natural; To : Natural);
9752 -- Move routine for Sort
9754 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9756 -----------
9757 -- OC_Lt --
9758 -----------
9760 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9761 begin
9762 return OC_Fbit (Op1) < OC_Fbit (Op2);
9763 end OC_Lt;
9765 -------------
9766 -- OC_Move --
9767 -------------
9769 procedure OC_Move (From : Natural; To : Natural) is
9770 begin
9771 OC_Fbit (To) := OC_Fbit (From);
9772 OC_Lbit (To) := OC_Lbit (From);
9773 end OC_Move;
9775 -- Start of processing for Overlap_Check
9777 begin
9778 CC := First (Component_Clauses (N));
9779 while Present (CC) loop
9781 -- Exclude component clause already marked in error
9783 if not Error_Posted (CC) then
9784 Find_Component;
9786 if Present (Comp) then
9787 OC_Count := OC_Count + 1;
9788 OC_Fbit (OC_Count) := Fbit;
9789 OC_Lbit (OC_Count) := Lbit;
9790 end if;
9791 end if;
9793 Next (CC);
9794 end loop;
9796 Sorting.Sort (OC_Count);
9798 Overlap_Check_Required := False;
9799 for J in 1 .. OC_Count - 1 loop
9800 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9801 Overlap_Check_Required := True;
9802 exit;
9803 end if;
9804 end loop;
9805 end Overlap_Check1;
9806 end if;
9808 -- If Overlap_Check_Required is still True, then we have to do the full
9809 -- scale overlap check, since we have at least two fields that do
9810 -- overlap, and we need to know if that is OK since they are in
9811 -- different variant, or whether we have a definite problem.
9813 if Overlap_Check_Required then
9814 Overlap_Check2 : declare
9815 C1_Ent, C2_Ent : Entity_Id;
9816 -- Entities of components being checked for overlap
9818 Clist : Node_Id;
9819 -- Component_List node whose Component_Items are being checked
9821 Citem : Node_Id;
9822 -- Component declaration for component being checked
9824 begin
9825 C1_Ent := First_Entity (Base_Type (Rectype));
9827 -- Loop through all components in record. For each component check
9828 -- for overlap with any of the preceding elements on the component
9829 -- list containing the component and also, if the component is in
9830 -- a variant, check against components outside the case structure.
9831 -- This latter test is repeated recursively up the variant tree.
9833 Main_Component_Loop : while Present (C1_Ent) loop
9834 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9835 goto Continue_Main_Component_Loop;
9836 end if;
9838 -- Skip overlap check if entity has no declaration node. This
9839 -- happens with discriminants in constrained derived types.
9840 -- Possibly we are missing some checks as a result, but that
9841 -- does not seem terribly serious.
9843 if No (Declaration_Node (C1_Ent)) then
9844 goto Continue_Main_Component_Loop;
9845 end if;
9847 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9849 -- Loop through component lists that need checking. Check the
9850 -- current component list and all lists in variants above us.
9852 Component_List_Loop : loop
9854 -- If derived type definition, go to full declaration
9855 -- If at outer level, check discriminants if there are any.
9857 if Nkind (Clist) = N_Derived_Type_Definition then
9858 Clist := Parent (Clist);
9859 end if;
9861 -- Outer level of record definition, check discriminants
9863 if Nkind_In (Clist, N_Full_Type_Declaration,
9864 N_Private_Type_Declaration)
9865 then
9866 if Has_Discriminants (Defining_Identifier (Clist)) then
9867 C2_Ent :=
9868 First_Discriminant (Defining_Identifier (Clist));
9869 while Present (C2_Ent) loop
9870 exit when C1_Ent = C2_Ent;
9871 Check_Component_Overlap (C1_Ent, C2_Ent);
9872 Next_Discriminant (C2_Ent);
9873 end loop;
9874 end if;
9876 -- Record extension case
9878 elsif Nkind (Clist) = N_Derived_Type_Definition then
9879 Clist := Empty;
9881 -- Otherwise check one component list
9883 else
9884 Citem := First (Component_Items (Clist));
9885 while Present (Citem) loop
9886 if Nkind (Citem) = N_Component_Declaration then
9887 C2_Ent := Defining_Identifier (Citem);
9888 exit when C1_Ent = C2_Ent;
9889 Check_Component_Overlap (C1_Ent, C2_Ent);
9890 end if;
9892 Next (Citem);
9893 end loop;
9894 end if;
9896 -- Check for variants above us (the parent of the Clist can
9897 -- be a variant, in which case its parent is a variant part,
9898 -- and the parent of the variant part is a component list
9899 -- whose components must all be checked against the current
9900 -- component for overlap).
9902 if Nkind (Parent (Clist)) = N_Variant then
9903 Clist := Parent (Parent (Parent (Clist)));
9905 -- Check for possible discriminant part in record, this
9906 -- is treated essentially as another level in the
9907 -- recursion. For this case the parent of the component
9908 -- list is the record definition, and its parent is the
9909 -- full type declaration containing the discriminant
9910 -- specifications.
9912 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9913 Clist := Parent (Parent ((Clist)));
9915 -- If neither of these two cases, we are at the top of
9916 -- the tree.
9918 else
9919 exit Component_List_Loop;
9920 end if;
9921 end loop Component_List_Loop;
9923 <<Continue_Main_Component_Loop>>
9924 Next_Entity (C1_Ent);
9926 end loop Main_Component_Loop;
9927 end Overlap_Check2;
9928 end if;
9930 -- The following circuit deals with warning on record holes (gaps). We
9931 -- skip this check if overlap was detected, since it makes sense for the
9932 -- programmer to fix this illegality before worrying about warnings.
9934 if not Overlap_Detected and Warn_On_Record_Holes then
9935 Record_Hole_Check : declare
9936 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9937 -- Full declaration of record type
9939 procedure Check_Component_List
9940 (CL : Node_Id;
9941 Sbit : Uint;
9942 DS : List_Id);
9943 -- Check component list CL for holes. The starting bit should be
9944 -- Sbit. which is zero for the main record component list and set
9945 -- appropriately for recursive calls for variants. DS is set to
9946 -- a list of discriminant specifications to be included in the
9947 -- consideration of components. It is No_List if none to consider.
9949 --------------------------
9950 -- Check_Component_List --
9951 --------------------------
9953 procedure Check_Component_List
9954 (CL : Node_Id;
9955 Sbit : Uint;
9956 DS : List_Id)
9958 Compl : Integer;
9960 begin
9961 Compl := Integer (List_Length (Component_Items (CL)));
9963 if DS /= No_List then
9964 Compl := Compl + Integer (List_Length (DS));
9965 end if;
9967 declare
9968 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9969 -- Gather components (zero entry is for sort routine)
9971 Ncomps : Natural := 0;
9972 -- Number of entries stored in Comps (starting at Comps (1))
9974 Citem : Node_Id;
9975 -- One component item or discriminant specification
9977 Nbit : Uint;
9978 -- Starting bit for next component
9980 CEnt : Entity_Id;
9981 -- Component entity
9983 Variant : Node_Id;
9984 -- One variant
9986 function Lt (Op1, Op2 : Natural) return Boolean;
9987 -- Compare routine for Sort
9989 procedure Move (From : Natural; To : Natural);
9990 -- Move routine for Sort
9992 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9994 --------
9995 -- Lt --
9996 --------
9998 function Lt (Op1, Op2 : Natural) return Boolean is
9999 begin
10000 return Component_Bit_Offset (Comps (Op1))
10002 Component_Bit_Offset (Comps (Op2));
10003 end Lt;
10005 ----------
10006 -- Move --
10007 ----------
10009 procedure Move (From : Natural; To : Natural) is
10010 begin
10011 Comps (To) := Comps (From);
10012 end Move;
10014 begin
10015 -- Gather discriminants into Comp
10017 if DS /= No_List then
10018 Citem := First (DS);
10019 while Present (Citem) loop
10020 if Nkind (Citem) = N_Discriminant_Specification then
10021 declare
10022 Ent : constant Entity_Id :=
10023 Defining_Identifier (Citem);
10024 begin
10025 if Ekind (Ent) = E_Discriminant then
10026 Ncomps := Ncomps + 1;
10027 Comps (Ncomps) := Ent;
10028 end if;
10029 end;
10030 end if;
10032 Next (Citem);
10033 end loop;
10034 end if;
10036 -- Gather component entities into Comp
10038 Citem := First (Component_Items (CL));
10039 while Present (Citem) loop
10040 if Nkind (Citem) = N_Component_Declaration then
10041 Ncomps := Ncomps + 1;
10042 Comps (Ncomps) := Defining_Identifier (Citem);
10043 end if;
10045 Next (Citem);
10046 end loop;
10048 -- Now sort the component entities based on the first bit.
10049 -- Note we already know there are no overlapping components.
10051 Sorting.Sort (Ncomps);
10053 -- Loop through entries checking for holes
10055 Nbit := Sbit;
10056 for J in 1 .. Ncomps loop
10057 CEnt := Comps (J);
10058 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
10060 if Error_Msg_Uint_1 > 0 then
10061 Error_Msg_NE
10062 ("?H?^-bit gap before component&",
10063 Component_Name (Component_Clause (CEnt)), CEnt);
10064 end if;
10066 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10067 end loop;
10069 -- Process variant parts recursively if present
10071 if Present (Variant_Part (CL)) then
10072 Variant := First (Variants (Variant_Part (CL)));
10073 while Present (Variant) loop
10074 Check_Component_List
10075 (Component_List (Variant), Nbit, No_List);
10076 Next (Variant);
10077 end loop;
10078 end if;
10079 end;
10080 end Check_Component_List;
10082 -- Start of processing for Record_Hole_Check
10084 begin
10085 declare
10086 Sbit : Uint;
10088 begin
10089 if Is_Tagged_Type (Rectype) then
10090 Sbit := UI_From_Int (System_Address_Size);
10091 else
10092 Sbit := Uint_0;
10093 end if;
10095 if Nkind (Decl) = N_Full_Type_Declaration
10096 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10097 then
10098 Check_Component_List
10099 (Component_List (Type_Definition (Decl)),
10100 Sbit,
10101 Discriminant_Specifications (Decl));
10102 end if;
10103 end;
10104 end Record_Hole_Check;
10105 end if;
10107 -- For records that have component clauses for all components, and whose
10108 -- size is less than or equal to 32, we need to know the size in the
10109 -- front end to activate possible packed array processing where the
10110 -- component type is a record.
10112 -- At this stage Hbit + 1 represents the first unused bit from all the
10113 -- component clauses processed, so if the component clauses are
10114 -- complete, then this is the length of the record.
10116 -- For records longer than System.Storage_Unit, and for those where not
10117 -- all components have component clauses, the back end determines the
10118 -- length (it may for example be appropriate to round up the size
10119 -- to some convenient boundary, based on alignment considerations, etc).
10121 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10123 -- Nothing to do if at least one component has no component clause
10125 Comp := First_Component_Or_Discriminant (Rectype);
10126 while Present (Comp) loop
10127 exit when No (Component_Clause (Comp));
10128 Next_Component_Or_Discriminant (Comp);
10129 end loop;
10131 -- If we fall out of loop, all components have component clauses
10132 -- and so we can set the size to the maximum value.
10134 if No (Comp) then
10135 Set_RM_Size (Rectype, Hbit + 1);
10136 end if;
10137 end if;
10138 end Check_Record_Representation_Clause;
10140 ----------------
10141 -- Check_Size --
10142 ----------------
10144 procedure Check_Size
10145 (N : Node_Id;
10146 T : Entity_Id;
10147 Siz : Uint;
10148 Biased : out Boolean)
10150 UT : constant Entity_Id := Underlying_Type (T);
10151 M : Uint;
10153 begin
10154 Biased := False;
10156 -- Reject patently improper size values.
10158 if Is_Elementary_Type (T)
10159 and then Siz > UI_From_Int (Int'Last)
10160 then
10161 Error_Msg_N ("Size value too large for elementary type", N);
10163 if Nkind (Original_Node (N)) = N_Op_Expon then
10164 Error_Msg_N
10165 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10166 end if;
10167 end if;
10169 -- Dismiss generic types
10171 if Is_Generic_Type (T)
10172 or else
10173 Is_Generic_Type (UT)
10174 or else
10175 Is_Generic_Type (Root_Type (UT))
10176 then
10177 return;
10179 -- Guard against previous errors
10181 elsif No (UT) or else UT = Any_Type then
10182 Check_Error_Detected;
10183 return;
10185 -- Check case of bit packed array
10187 elsif Is_Array_Type (UT)
10188 and then Known_Static_Component_Size (UT)
10189 and then Is_Bit_Packed_Array (UT)
10190 then
10191 declare
10192 Asiz : Uint;
10193 Indx : Node_Id;
10194 Ityp : Entity_Id;
10196 begin
10197 Asiz := Component_Size (UT);
10198 Indx := First_Index (UT);
10199 loop
10200 Ityp := Etype (Indx);
10202 -- If non-static bound, then we are not in the business of
10203 -- trying to check the length, and indeed an error will be
10204 -- issued elsewhere, since sizes of non-static array types
10205 -- cannot be set implicitly or explicitly.
10207 if not Is_OK_Static_Subtype (Ityp) then
10208 return;
10209 end if;
10211 -- Otherwise accumulate next dimension
10213 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10214 Expr_Value (Type_Low_Bound (Ityp)) +
10215 Uint_1);
10217 Next_Index (Indx);
10218 exit when No (Indx);
10219 end loop;
10221 if Asiz <= Siz then
10222 return;
10224 else
10225 Error_Msg_Uint_1 := Asiz;
10226 Error_Msg_NE
10227 ("size for& too small, minimum allowed is ^", N, T);
10228 Set_Esize (T, Asiz);
10229 Set_RM_Size (T, Asiz);
10230 end if;
10231 end;
10233 -- All other composite types are ignored
10235 elsif Is_Composite_Type (UT) then
10236 return;
10238 -- For fixed-point types, don't check minimum if type is not frozen,
10239 -- since we don't know all the characteristics of the type that can
10240 -- affect the size (e.g. a specified small) till freeze time.
10242 elsif Is_Fixed_Point_Type (UT)
10243 and then not Is_Frozen (UT)
10244 then
10245 null;
10247 -- Cases for which a minimum check is required
10249 else
10250 -- Ignore if specified size is correct for the type
10252 if Known_Esize (UT) and then Siz = Esize (UT) then
10253 return;
10254 end if;
10256 -- Otherwise get minimum size
10258 M := UI_From_Int (Minimum_Size (UT));
10260 if Siz < M then
10262 -- Size is less than minimum size, but one possibility remains
10263 -- that we can manage with the new size if we bias the type.
10265 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10267 if Siz < M then
10268 Error_Msg_Uint_1 := M;
10269 Error_Msg_NE
10270 ("size for& too small, minimum allowed is ^", N, T);
10271 Set_Esize (T, M);
10272 Set_RM_Size (T, M);
10273 else
10274 Biased := True;
10275 end if;
10276 end if;
10277 end if;
10278 end Check_Size;
10280 --------------------------
10281 -- Freeze_Entity_Checks --
10282 --------------------------
10284 procedure Freeze_Entity_Checks (N : Node_Id) is
10285 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10286 -- Inspect the primitive operations of type Typ and hide all pairs of
10287 -- implicitly declared non-overridden non-fully conformant homographs
10288 -- (Ada RM 8.3 12.3/2).
10290 -------------------------------------
10291 -- Hide_Non_Overridden_Subprograms --
10292 -------------------------------------
10294 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10295 procedure Hide_Matching_Homographs
10296 (Subp_Id : Entity_Id;
10297 Start_Elmt : Elmt_Id);
10298 -- Inspect a list of primitive operations starting with Start_Elmt
10299 -- and find matching implicitly declared non-overridden non-fully
10300 -- conformant homographs of Subp_Id. If found, all matches along
10301 -- with Subp_Id are hidden from all visibility.
10303 function Is_Non_Overridden_Or_Null_Procedure
10304 (Subp_Id : Entity_Id) return Boolean;
10305 -- Determine whether subprogram Subp_Id is implicitly declared non-
10306 -- overridden subprogram or an implicitly declared null procedure.
10308 ------------------------------
10309 -- Hide_Matching_Homographs --
10310 ------------------------------
10312 procedure Hide_Matching_Homographs
10313 (Subp_Id : Entity_Id;
10314 Start_Elmt : Elmt_Id)
10316 Prim : Entity_Id;
10317 Prim_Elmt : Elmt_Id;
10319 begin
10320 Prim_Elmt := Start_Elmt;
10321 while Present (Prim_Elmt) loop
10322 Prim := Node (Prim_Elmt);
10324 -- The current primitive is implicitly declared non-overridden
10325 -- non-fully conformant homograph of Subp_Id. Both subprograms
10326 -- must be hidden from visibility.
10328 if Chars (Prim) = Chars (Subp_Id)
10329 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10330 and then not Fully_Conformant (Prim, Subp_Id)
10331 then
10332 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10333 Set_Is_Immediately_Visible (Prim, False);
10334 Set_Is_Potentially_Use_Visible (Prim, False);
10336 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10337 Set_Is_Immediately_Visible (Subp_Id, False);
10338 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10339 end if;
10341 Next_Elmt (Prim_Elmt);
10342 end loop;
10343 end Hide_Matching_Homographs;
10345 -----------------------------------------
10346 -- Is_Non_Overridden_Or_Null_Procedure --
10347 -----------------------------------------
10349 function Is_Non_Overridden_Or_Null_Procedure
10350 (Subp_Id : Entity_Id) return Boolean
10352 Alias_Id : Entity_Id;
10354 begin
10355 -- The subprogram is inherited (implicitly declared), it does not
10356 -- override and does not cover a primitive of an interface.
10358 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10359 and then Present (Alias (Subp_Id))
10360 and then No (Interface_Alias (Subp_Id))
10361 and then No (Overridden_Operation (Subp_Id))
10362 then
10363 Alias_Id := Alias (Subp_Id);
10365 if Requires_Overriding (Alias_Id) then
10366 return True;
10368 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10369 and then Null_Present (Parent (Alias_Id))
10370 then
10371 return True;
10372 end if;
10373 end if;
10375 return False;
10376 end Is_Non_Overridden_Or_Null_Procedure;
10378 -- Local variables
10380 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10381 Prim : Entity_Id;
10382 Prim_Elmt : Elmt_Id;
10384 -- Start of processing for Hide_Non_Overridden_Subprograms
10386 begin
10387 -- Inspect the list of primitives looking for non-overridden
10388 -- subprograms.
10390 if Present (Prim_Ops) then
10391 Prim_Elmt := First_Elmt (Prim_Ops);
10392 while Present (Prim_Elmt) loop
10393 Prim := Node (Prim_Elmt);
10394 Next_Elmt (Prim_Elmt);
10396 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10397 Hide_Matching_Homographs
10398 (Subp_Id => Prim,
10399 Start_Elmt => Prim_Elmt);
10400 end if;
10401 end loop;
10402 end if;
10403 end Hide_Non_Overridden_Subprograms;
10405 ---------------------
10406 -- Local variables --
10407 ---------------------
10409 E : constant Entity_Id := Entity (N);
10411 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10412 -- True in non-generic case. Some of the processing here is skipped
10413 -- for the generic case since it is not needed. Basically in the
10414 -- generic case, we only need to do stuff that might generate error
10415 -- messages or warnings.
10417 -- Start of processing for Freeze_Entity_Checks
10419 begin
10420 -- Remember that we are processing a freezing entity. Required to
10421 -- ensure correct decoration of internal entities associated with
10422 -- interfaces (see New_Overloaded_Entity).
10424 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10426 -- For tagged types covering interfaces add internal entities that link
10427 -- the primitives of the interfaces with the primitives that cover them.
10428 -- Note: These entities were originally generated only when generating
10429 -- code because their main purpose was to provide support to initialize
10430 -- the secondary dispatch tables. They are now generated also when
10431 -- compiling with no code generation to provide ASIS the relationship
10432 -- between interface primitives and tagged type primitives. They are
10433 -- also used to locate primitives covering interfaces when processing
10434 -- generics (see Derive_Subprograms).
10436 -- This is not needed in the generic case
10438 if Ada_Version >= Ada_2005
10439 and then Non_Generic_Case
10440 and then Ekind (E) = E_Record_Type
10441 and then Is_Tagged_Type (E)
10442 and then not Is_Interface (E)
10443 and then Has_Interfaces (E)
10444 then
10445 -- This would be a good common place to call the routine that checks
10446 -- overriding of interface primitives (and thus factorize calls to
10447 -- Check_Abstract_Overriding located at different contexts in the
10448 -- compiler). However, this is not possible because it causes
10449 -- spurious errors in case of late overriding.
10451 Add_Internal_Interface_Entities (E);
10452 end if;
10454 -- After all forms of overriding have been resolved, a tagged type may
10455 -- be left with a set of implicitly declared and possibly erroneous
10456 -- abstract subprograms, null procedures and subprograms that require
10457 -- overriding. If this set contains fully conformat homographs, then one
10458 -- is chosen arbitrarily (already done during resolution), otherwise all
10459 -- remaining non-fully conformant homographs are hidden from visibility
10460 -- (Ada RM 8.3 12.3/2).
10462 if Is_Tagged_Type (E) then
10463 Hide_Non_Overridden_Subprograms (E);
10464 end if;
10466 -- Check CPP types
10468 if Ekind (E) = E_Record_Type
10469 and then Is_CPP_Class (E)
10470 and then Is_Tagged_Type (E)
10471 and then Tagged_Type_Expansion
10472 then
10473 if CPP_Num_Prims (E) = 0 then
10475 -- If the CPP type has user defined components then it must import
10476 -- primitives from C++. This is required because if the C++ class
10477 -- has no primitives then the C++ compiler does not added the _tag
10478 -- component to the type.
10480 if First_Entity (E) /= Last_Entity (E) then
10481 Error_Msg_N
10482 ("'C'P'P type must import at least one primitive from C++??",
10484 end if;
10485 end if;
10487 -- Check that all its primitives are abstract or imported from C++.
10488 -- Check also availability of the C++ constructor.
10490 declare
10491 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10492 Elmt : Elmt_Id;
10493 Error_Reported : Boolean := False;
10494 Prim : Node_Id;
10496 begin
10497 Elmt := First_Elmt (Primitive_Operations (E));
10498 while Present (Elmt) loop
10499 Prim := Node (Elmt);
10501 if Comes_From_Source (Prim) then
10502 if Is_Abstract_Subprogram (Prim) then
10503 null;
10505 elsif not Is_Imported (Prim)
10506 or else Convention (Prim) /= Convention_CPP
10507 then
10508 Error_Msg_N
10509 ("primitives of 'C'P'P types must be imported from C++ "
10510 & "or abstract??", Prim);
10512 elsif not Has_Constructors
10513 and then not Error_Reported
10514 then
10515 Error_Msg_Name_1 := Chars (E);
10516 Error_Msg_N
10517 ("??'C'P'P constructor required for type %", Prim);
10518 Error_Reported := True;
10519 end if;
10520 end if;
10522 Next_Elmt (Elmt);
10523 end loop;
10524 end;
10525 end if;
10527 -- Check Ada derivation of CPP type
10529 if Expander_Active -- why? losing errors in -gnatc mode???
10530 and then Present (Etype (E)) -- defend against errors
10531 and then Tagged_Type_Expansion
10532 and then Ekind (E) = E_Record_Type
10533 and then Etype (E) /= E
10534 and then Is_CPP_Class (Etype (E))
10535 and then CPP_Num_Prims (Etype (E)) > 0
10536 and then not Is_CPP_Class (E)
10537 and then not Has_CPP_Constructors (Etype (E))
10538 then
10539 -- If the parent has C++ primitives but it has no constructor then
10540 -- check that all the primitives are overridden in this derivation;
10541 -- otherwise the constructor of the parent is needed to build the
10542 -- dispatch table.
10544 declare
10545 Elmt : Elmt_Id;
10546 Prim : Node_Id;
10548 begin
10549 Elmt := First_Elmt (Primitive_Operations (E));
10550 while Present (Elmt) loop
10551 Prim := Node (Elmt);
10553 if not Is_Abstract_Subprogram (Prim)
10554 and then No (Interface_Alias (Prim))
10555 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10556 then
10557 Error_Msg_Name_1 := Chars (Etype (E));
10558 Error_Msg_N
10559 ("'C'P'P constructor required for parent type %", E);
10560 exit;
10561 end if;
10563 Next_Elmt (Elmt);
10564 end loop;
10565 end;
10566 end if;
10568 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10570 -- If we have a type with predicates, build predicate function. This
10571 -- is not needed in the generic case, and is not needed within TSS
10572 -- subprograms and other predefined primitives.
10574 if Non_Generic_Case
10575 and then Is_Type (E)
10576 and then Has_Predicates (E)
10577 and then not Within_Internal_Subprogram
10578 then
10579 Build_Predicate_Functions (E, N);
10580 end if;
10582 -- If type has delayed aspects, this is where we do the preanalysis at
10583 -- the freeze point, as part of the consistent visibility check. Note
10584 -- that this must be done after calling Build_Predicate_Functions or
10585 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10586 -- the subtype name in the saved expression so that they will not cause
10587 -- trouble in the preanalysis.
10589 -- This is also not needed in the generic case
10591 if Non_Generic_Case
10592 and then Has_Delayed_Aspects (E)
10593 and then Scope (E) = Current_Scope
10594 then
10595 -- Retrieve the visibility to the discriminants in order to properly
10596 -- analyze the aspects.
10598 Push_Scope_And_Install_Discriminants (E);
10600 declare
10601 Ritem : Node_Id;
10603 begin
10604 -- Look for aspect specification entries for this entity
10606 Ritem := First_Rep_Item (E);
10607 while Present (Ritem) loop
10608 if Nkind (Ritem) = N_Aspect_Specification
10609 and then Entity (Ritem) = E
10610 and then Is_Delayed_Aspect (Ritem)
10611 then
10612 Check_Aspect_At_Freeze_Point (Ritem);
10613 end if;
10615 Next_Rep_Item (Ritem);
10616 end loop;
10617 end;
10619 Uninstall_Discriminants_And_Pop_Scope (E);
10620 end if;
10622 -- For a record type, deal with variant parts. This has to be delayed
10623 -- to this point, because of the issue of statically predicated
10624 -- subtypes, which we have to ensure are frozen before checking
10625 -- choices, since we need to have the static choice list set.
10627 if Is_Record_Type (E) then
10628 Check_Variant_Part : declare
10629 D : constant Node_Id := Declaration_Node (E);
10630 T : Node_Id;
10631 C : Node_Id;
10632 VP : Node_Id;
10634 Others_Present : Boolean;
10635 pragma Warnings (Off, Others_Present);
10636 -- Indicates others present, not used in this case
10638 procedure Non_Static_Choice_Error (Choice : Node_Id);
10639 -- Error routine invoked by the generic instantiation below when
10640 -- the variant part has a non static choice.
10642 procedure Process_Declarations (Variant : Node_Id);
10643 -- Processes declarations associated with a variant. We analyzed
10644 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10645 -- but we still need the recursive call to Check_Choices for any
10646 -- nested variant to get its choices properly processed. This is
10647 -- also where we expand out the choices if expansion is active.
10649 package Variant_Choices_Processing is new
10650 Generic_Check_Choices
10651 (Process_Empty_Choice => No_OP,
10652 Process_Non_Static_Choice => Non_Static_Choice_Error,
10653 Process_Associated_Node => Process_Declarations);
10654 use Variant_Choices_Processing;
10656 -----------------------------
10657 -- Non_Static_Choice_Error --
10658 -----------------------------
10660 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10661 begin
10662 Flag_Non_Static_Expr
10663 ("choice given in variant part is not static!", Choice);
10664 end Non_Static_Choice_Error;
10666 --------------------------
10667 -- Process_Declarations --
10668 --------------------------
10670 procedure Process_Declarations (Variant : Node_Id) is
10671 CL : constant Node_Id := Component_List (Variant);
10672 VP : Node_Id;
10674 begin
10675 -- Check for static predicate present in this variant
10677 if Has_SP_Choice (Variant) then
10679 -- Here we expand. You might expect to find this call in
10680 -- Expand_N_Variant_Part, but that is called when we first
10681 -- see the variant part, and we cannot do this expansion
10682 -- earlier than the freeze point, since for statically
10683 -- predicated subtypes, the predicate is not known till
10684 -- the freeze point.
10686 -- Furthermore, we do this expansion even if the expander
10687 -- is not active, because other semantic processing, e.g.
10688 -- for aggregates, requires the expanded list of choices.
10690 -- If the expander is not active, then we can't just clobber
10691 -- the list since it would invalidate the ASIS -gnatct tree.
10692 -- So we have to rewrite the variant part with a Rewrite
10693 -- call that replaces it with a copy and clobber the copy.
10695 if not Expander_Active then
10696 declare
10697 NewV : constant Node_Id := New_Copy (Variant);
10698 begin
10699 Set_Discrete_Choices
10700 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10701 Rewrite (Variant, NewV);
10702 end;
10703 end if;
10705 Expand_Static_Predicates_In_Choices (Variant);
10706 end if;
10708 -- We don't need to worry about the declarations in the variant
10709 -- (since they were analyzed by Analyze_Choices when we first
10710 -- encountered the variant), but we do need to take care of
10711 -- expansion of any nested variants.
10713 if not Null_Present (CL) then
10714 VP := Variant_Part (CL);
10716 if Present (VP) then
10717 Check_Choices
10718 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10719 end if;
10720 end if;
10721 end Process_Declarations;
10723 -- Start of processing for Check_Variant_Part
10725 begin
10726 -- Find component list
10728 C := Empty;
10730 if Nkind (D) = N_Full_Type_Declaration then
10731 T := Type_Definition (D);
10733 if Nkind (T) = N_Record_Definition then
10734 C := Component_List (T);
10736 elsif Nkind (T) = N_Derived_Type_Definition
10737 and then Present (Record_Extension_Part (T))
10738 then
10739 C := Component_List (Record_Extension_Part (T));
10740 end if;
10741 end if;
10743 -- Case of variant part present
10745 if Present (C) and then Present (Variant_Part (C)) then
10746 VP := Variant_Part (C);
10748 -- Check choices
10750 Check_Choices
10751 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10753 -- If the last variant does not contain the Others choice,
10754 -- replace it with an N_Others_Choice node since Gigi always
10755 -- wants an Others. Note that we do not bother to call Analyze
10756 -- on the modified variant part, since its only effect would be
10757 -- to compute the Others_Discrete_Choices node laboriously, and
10758 -- of course we already know the list of choices corresponding
10759 -- to the others choice (it's the list we're replacing).
10761 -- We only want to do this if the expander is active, since
10762 -- we do not want to clobber the ASIS tree.
10764 if Expander_Active then
10765 declare
10766 Last_Var : constant Node_Id :=
10767 Last_Non_Pragma (Variants (VP));
10769 Others_Node : Node_Id;
10771 begin
10772 if Nkind (First (Discrete_Choices (Last_Var))) /=
10773 N_Others_Choice
10774 then
10775 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10776 Set_Others_Discrete_Choices
10777 (Others_Node, Discrete_Choices (Last_Var));
10778 Set_Discrete_Choices
10779 (Last_Var, New_List (Others_Node));
10780 end if;
10781 end;
10782 end if;
10783 end if;
10784 end Check_Variant_Part;
10785 end if;
10786 end Freeze_Entity_Checks;
10788 -------------------------
10789 -- Get_Alignment_Value --
10790 -------------------------
10792 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10793 Align : constant Uint := Static_Integer (Expr);
10795 begin
10796 if Align = No_Uint then
10797 return No_Uint;
10799 elsif Align <= 0 then
10800 Error_Msg_N ("alignment value must be positive", Expr);
10801 return No_Uint;
10803 else
10804 for J in Int range 0 .. 64 loop
10805 declare
10806 M : constant Uint := Uint_2 ** J;
10808 begin
10809 exit when M = Align;
10811 if M > Align then
10812 Error_Msg_N
10813 ("alignment value must be power of 2", Expr);
10814 return No_Uint;
10815 end if;
10816 end;
10817 end loop;
10819 return Align;
10820 end if;
10821 end Get_Alignment_Value;
10823 -------------------------------------
10824 -- Inherit_Aspects_At_Freeze_Point --
10825 -------------------------------------
10827 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10828 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10829 (Rep_Item : Node_Id) return Boolean;
10830 -- This routine checks if Rep_Item is either a pragma or an aspect
10831 -- specification node whose correponding pragma (if any) is present in
10832 -- the Rep Item chain of the entity it has been specified to.
10834 --------------------------------------------------
10835 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10836 --------------------------------------------------
10838 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10839 (Rep_Item : Node_Id) return Boolean
10841 begin
10842 return
10843 Nkind (Rep_Item) = N_Pragma
10844 or else Present_In_Rep_Item
10845 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10846 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10848 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10850 begin
10851 -- A representation item is either subtype-specific (Size and Alignment
10852 -- clauses) or type-related (all others). Subtype-specific aspects may
10853 -- differ for different subtypes of the same type (RM 13.1.8).
10855 -- A derived type inherits each type-related representation aspect of
10856 -- its parent type that was directly specified before the declaration of
10857 -- the derived type (RM 13.1.15).
10859 -- A derived subtype inherits each subtype-specific representation
10860 -- aspect of its parent subtype that was directly specified before the
10861 -- declaration of the derived type (RM 13.1.15).
10863 -- The general processing involves inheriting a representation aspect
10864 -- from a parent type whenever the first rep item (aspect specification,
10865 -- attribute definition clause, pragma) corresponding to the given
10866 -- representation aspect in the rep item chain of Typ, if any, isn't
10867 -- directly specified to Typ but to one of its parents.
10869 -- ??? Note that, for now, just a limited number of representation
10870 -- aspects have been inherited here so far. Many of them are
10871 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
10872 -- a non- exhaustive list of aspects that likely also need to
10873 -- be moved to this routine: Alignment, Component_Alignment,
10874 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
10875 -- Preelaborable_Initialization, RM_Size and Small.
10877 -- In addition, Convention must be propagated from base type to subtype,
10878 -- because the subtype may have been declared on an incomplete view.
10880 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
10881 return;
10882 end if;
10884 -- Ada_05/Ada_2005
10886 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
10887 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
10888 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10889 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
10890 then
10891 Set_Is_Ada_2005_Only (Typ);
10892 end if;
10894 -- Ada_12/Ada_2012
10896 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
10897 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
10898 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10899 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
10900 then
10901 Set_Is_Ada_2012_Only (Typ);
10902 end if;
10904 -- Atomic/Shared
10906 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
10907 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
10908 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10909 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
10910 then
10911 Set_Is_Atomic (Typ);
10912 Set_Treat_As_Volatile (Typ);
10913 Set_Is_Volatile (Typ);
10914 end if;
10916 -- Convention
10918 if Is_Record_Type (Typ)
10919 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
10920 then
10921 Set_Convention (Typ, Convention (Base_Type (Typ)));
10922 end if;
10924 -- Default_Component_Value
10926 -- Verify that there is no rep_item declared for the type, and there
10927 -- is one coming from an ancestor.
10929 if Is_Array_Type (Typ)
10930 and then Is_Base_Type (Typ)
10931 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
10932 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
10933 then
10934 Set_Default_Aspect_Component_Value (Typ,
10935 Default_Aspect_Component_Value
10936 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
10937 end if;
10939 -- Default_Value
10941 if Is_Scalar_Type (Typ)
10942 and then Is_Base_Type (Typ)
10943 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
10944 and then Has_Rep_Item (Typ, Name_Default_Value)
10945 then
10946 Set_Has_Default_Aspect (Typ);
10947 Set_Default_Aspect_Value (Typ,
10948 Default_Aspect_Value
10949 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
10950 end if;
10952 -- Discard_Names
10954 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
10955 and then Has_Rep_Item (Typ, Name_Discard_Names)
10956 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10957 (Get_Rep_Item (Typ, Name_Discard_Names))
10958 then
10959 Set_Discard_Names (Typ);
10960 end if;
10962 -- Invariants
10964 if not Has_Rep_Item (Typ, Name_Invariant, False)
10965 and then Has_Rep_Item (Typ, Name_Invariant)
10966 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10967 (Get_Rep_Item (Typ, Name_Invariant))
10968 then
10969 Set_Has_Invariants (Typ);
10971 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
10972 Set_Has_Inheritable_Invariants (Typ);
10973 end if;
10975 -- If we have a subtype with invariants, whose base type does not have
10976 -- invariants, copy these invariants to the base type. This happens for
10977 -- the case of implicit base types created for scalar and array types.
10979 elsif Has_Invariants (Typ)
10980 and then not Has_Invariants (Base_Type (Typ))
10981 then
10982 Set_Has_Invariants (Base_Type (Typ));
10983 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
10984 end if;
10986 -- Volatile
10988 if not Has_Rep_Item (Typ, Name_Volatile, False)
10989 and then Has_Rep_Item (Typ, Name_Volatile)
10990 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10991 (Get_Rep_Item (Typ, Name_Volatile))
10992 then
10993 Set_Treat_As_Volatile (Typ);
10994 Set_Is_Volatile (Typ);
10995 end if;
10997 -- Inheritance for derived types only
10999 if Is_Derived_Type (Typ) then
11000 declare
11001 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11002 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11004 begin
11005 -- Atomic_Components
11007 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11008 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11009 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11010 (Get_Rep_Item (Typ, Name_Atomic_Components))
11011 then
11012 Set_Has_Atomic_Components (Imp_Bas_Typ);
11013 end if;
11015 -- Volatile_Components
11017 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11018 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11019 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11020 (Get_Rep_Item (Typ, Name_Volatile_Components))
11021 then
11022 Set_Has_Volatile_Components (Imp_Bas_Typ);
11023 end if;
11025 -- Finalize_Storage_Only
11027 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11028 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11029 then
11030 Set_Finalize_Storage_Only (Bas_Typ);
11031 end if;
11033 -- Universal_Aliasing
11035 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11036 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11037 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11038 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11039 then
11040 Set_Universal_Aliasing (Imp_Bas_Typ);
11041 end if;
11043 -- Bit_Order
11045 if Is_Record_Type (Typ) then
11046 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11047 and then Has_Rep_Item (Typ, Name_Bit_Order)
11048 then
11049 Set_Reverse_Bit_Order (Bas_Typ,
11050 Reverse_Bit_Order (Entity (Name
11051 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11052 end if;
11053 end if;
11055 -- Scalar_Storage_Order
11057 -- Note: the aspect is specified on a first subtype, but recorded
11058 -- in a flag of the base type!
11060 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11061 and then Typ = Bas_Typ
11062 then
11063 -- For a type extension, always inherit from parent; otherwise
11064 -- inherit if no default applies. Note: we do not check for
11065 -- an explicit rep item on the parent type when inheriting,
11066 -- because the parent SSO may itself have been set by default.
11068 if not Has_Rep_Item (First_Subtype (Typ),
11069 Name_Scalar_Storage_Order, False)
11070 and then (Is_Tagged_Type (Bas_Typ)
11071 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11072 or else
11073 SSO_Set_High_By_Default (Bas_Typ)))
11074 then
11075 Set_Reverse_Storage_Order (Bas_Typ,
11076 Reverse_Storage_Order
11077 (Implementation_Base_Type (Etype (Bas_Typ))));
11079 -- Clear default SSO indications, since the inherited aspect
11080 -- which was set explicitly overrides the default.
11082 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11083 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11084 end if;
11085 end if;
11086 end;
11087 end if;
11088 end Inherit_Aspects_At_Freeze_Point;
11090 ----------------
11091 -- Initialize --
11092 ----------------
11094 procedure Initialize is
11095 begin
11096 Address_Clause_Checks.Init;
11097 Unchecked_Conversions.Init;
11099 if VM_Target /= No_VM or else AAMP_On_Target then
11100 Independence_Checks.Init;
11101 end if;
11102 end Initialize;
11104 ---------------------------
11105 -- Install_Discriminants --
11106 ---------------------------
11108 procedure Install_Discriminants (E : Entity_Id) is
11109 Disc : Entity_Id;
11110 Prev : Entity_Id;
11111 begin
11112 Disc := First_Discriminant (E);
11113 while Present (Disc) loop
11114 Prev := Current_Entity (Disc);
11115 Set_Current_Entity (Disc);
11116 Set_Is_Immediately_Visible (Disc);
11117 Set_Homonym (Disc, Prev);
11118 Next_Discriminant (Disc);
11119 end loop;
11120 end Install_Discriminants;
11122 -------------------------
11123 -- Is_Operational_Item --
11124 -------------------------
11126 function Is_Operational_Item (N : Node_Id) return Boolean is
11127 begin
11128 if Nkind (N) /= N_Attribute_Definition_Clause then
11129 return False;
11131 else
11132 declare
11133 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11134 begin
11135 return Id = Attribute_Input
11136 or else Id = Attribute_Output
11137 or else Id = Attribute_Read
11138 or else Id = Attribute_Write
11139 or else Id = Attribute_External_Tag;
11140 end;
11141 end if;
11142 end Is_Operational_Item;
11144 -------------------------
11145 -- Is_Predicate_Static --
11146 -------------------------
11148 -- Note: the basic legality of the expression has already been checked, so
11149 -- we don't need to worry about cases or ranges on strings for example.
11151 function Is_Predicate_Static
11152 (Expr : Node_Id;
11153 Nam : Name_Id) return Boolean
11155 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11156 -- Given a list of case expression alternatives, returns True if all
11157 -- the alternatives are static (have all static choices, and a static
11158 -- expression).
11160 function All_Static_Choices (L : List_Id) return Boolean;
11161 -- Returns true if all elements of the list are OK static choices
11162 -- as defined below for Is_Static_Choice. Used for case expression
11163 -- alternatives and for the right operand of a membership test. An
11164 -- others_choice is static if the corresponding expression is static.
11165 -- The staticness of the bounds is checked separately.
11167 function Is_Static_Choice (N : Node_Id) return Boolean;
11168 -- Returns True if N represents a static choice (static subtype, or
11169 -- static subtype indication, or static expression, or static range).
11171 -- Note that this is a bit more inclusive than we actually need
11172 -- (in particular membership tests do not allow the use of subtype
11173 -- indications). But that doesn't matter, we have already checked
11174 -- that the construct is legal to get this far.
11176 function Is_Type_Ref (N : Node_Id) return Boolean;
11177 pragma Inline (Is_Type_Ref);
11178 -- Returns True if N is a reference to the type for the predicate in the
11179 -- expression (i.e. if it is an identifier whose Chars field matches the
11180 -- Nam given in the call). N must not be parenthesized, if the type name
11181 -- appears in parens, this routine will return False.
11183 ----------------------------------
11184 -- All_Static_Case_Alternatives --
11185 ----------------------------------
11187 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11188 N : Node_Id;
11190 begin
11191 N := First (L);
11192 while Present (N) loop
11193 if not (All_Static_Choices (Discrete_Choices (N))
11194 and then Is_OK_Static_Expression (Expression (N)))
11195 then
11196 return False;
11197 end if;
11199 Next (N);
11200 end loop;
11202 return True;
11203 end All_Static_Case_Alternatives;
11205 ------------------------
11206 -- All_Static_Choices --
11207 ------------------------
11209 function All_Static_Choices (L : List_Id) return Boolean is
11210 N : Node_Id;
11212 begin
11213 N := First (L);
11214 while Present (N) loop
11215 if not Is_Static_Choice (N) then
11216 return False;
11217 end if;
11219 Next (N);
11220 end loop;
11222 return True;
11223 end All_Static_Choices;
11225 ----------------------
11226 -- Is_Static_Choice --
11227 ----------------------
11229 function Is_Static_Choice (N : Node_Id) return Boolean is
11230 begin
11231 return Nkind (N) = N_Others_Choice
11232 or else Is_OK_Static_Expression (N)
11233 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11234 and then Is_OK_Static_Subtype (Entity (N)))
11235 or else (Nkind (N) = N_Subtype_Indication
11236 and then Is_OK_Static_Subtype (Entity (N)))
11237 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11238 end Is_Static_Choice;
11240 -----------------
11241 -- Is_Type_Ref --
11242 -----------------
11244 function Is_Type_Ref (N : Node_Id) return Boolean is
11245 begin
11246 return Nkind (N) = N_Identifier
11247 and then Chars (N) = Nam
11248 and then Paren_Count (N) = 0;
11249 end Is_Type_Ref;
11251 -- Start of processing for Is_Predicate_Static
11253 begin
11254 -- Predicate_Static means one of the following holds. Numbers are the
11255 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11257 -- 16: A static expression
11259 if Is_OK_Static_Expression (Expr) then
11260 return True;
11262 -- 17: A membership test whose simple_expression is the current
11263 -- instance, and whose membership_choice_list meets the requirements
11264 -- for a static membership test.
11266 elsif Nkind (Expr) in N_Membership_Test
11267 and then ((Present (Right_Opnd (Expr))
11268 and then Is_Static_Choice (Right_Opnd (Expr)))
11269 or else
11270 (Present (Alternatives (Expr))
11271 and then All_Static_Choices (Alternatives (Expr))))
11272 then
11273 return True;
11275 -- 18. A case_expression whose selecting_expression is the current
11276 -- instance, and whose dependent expressions are static expressions.
11278 elsif Nkind (Expr) = N_Case_Expression
11279 and then Is_Type_Ref (Expression (Expr))
11280 and then All_Static_Case_Alternatives (Alternatives (Expr))
11281 then
11282 return True;
11284 -- 19. A call to a predefined equality or ordering operator, where one
11285 -- operand is the current instance, and the other is a static
11286 -- expression.
11288 -- Note: the RM is clearly wrong here in not excluding string types.
11289 -- Without this exclusion, we would allow expressions like X > "ABC"
11290 -- to be considered as predicate-static, which is clearly not intended,
11291 -- since the idea is for predicate-static to be a subset of normal
11292 -- static expressions (and "DEF" > "ABC" is not a static expression).
11294 -- However, we do allow internally generated (not from source) equality
11295 -- and inequality operations to be valid on strings (this helps deal
11296 -- with cases where we transform A in "ABC" to A = "ABC).
11298 elsif Nkind (Expr) in N_Op_Compare
11299 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11300 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11301 and then not Comes_From_Source (Expr)))
11302 and then ((Is_Type_Ref (Left_Opnd (Expr))
11303 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11304 or else
11305 (Is_Type_Ref (Right_Opnd (Expr))
11306 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11307 then
11308 return True;
11310 -- 20. A call to a predefined boolean logical operator, where each
11311 -- operand is predicate-static.
11313 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11314 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11315 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11316 or else
11317 (Nkind (Expr) = N_Op_Not
11318 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11319 then
11320 return True;
11322 -- 21. A short-circuit control form where both operands are
11323 -- predicate-static.
11325 elsif Nkind (Expr) in N_Short_Circuit
11326 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11327 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11328 then
11329 return True;
11331 -- 22. A parenthesized predicate-static expression. This does not
11332 -- require any special test, since we just ignore paren levels in
11333 -- all the cases above.
11335 -- One more test that is an implementation artifact caused by the fact
11336 -- that we are analyzing not the original expression, but the generated
11337 -- expression in the body of the predicate function. This can include
11338 -- references to inherited predicates, so that the expression we are
11339 -- processing looks like:
11341 -- expression and then xxPredicate (typ (Inns))
11343 -- Where the call is to a Predicate function for an inherited predicate.
11344 -- We simply ignore such a call, which could be to either a dynamic or
11345 -- a static predicate. Note that if the parent predicate is dynamic then
11346 -- eventually this type will be marked as dynamic, but you are allowed
11347 -- to specify a static predicate for a subtype which is inheriting a
11348 -- dynamic predicate, so the static predicate validation here ignores
11349 -- the inherited predicate even if it is dynamic.
11351 elsif Nkind (Expr) = N_Function_Call
11352 and then Is_Predicate_Function (Entity (Name (Expr)))
11353 then
11354 return True;
11356 -- That's an exhaustive list of tests, all other cases are not
11357 -- predicate-static, so we return False.
11359 else
11360 return False;
11361 end if;
11362 end Is_Predicate_Static;
11364 ---------------------
11365 -- Kill_Rep_Clause --
11366 ---------------------
11368 procedure Kill_Rep_Clause (N : Node_Id) is
11369 begin
11370 pragma Assert (Ignore_Rep_Clauses);
11372 -- Note: we use Replace rather than Rewrite, because we don't want
11373 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11374 -- rep clause that is being replaced.
11376 Replace (N, Make_Null_Statement (Sloc (N)));
11378 -- The null statement must be marked as not coming from source. This is
11379 -- so that ASIS ignores it, and also the back end does not expect bogus
11380 -- "from source" null statements in weird places (e.g. in declarative
11381 -- regions where such null statements are not allowed).
11383 Set_Comes_From_Source (N, False);
11384 end Kill_Rep_Clause;
11386 ------------------
11387 -- Minimum_Size --
11388 ------------------
11390 function Minimum_Size
11391 (T : Entity_Id;
11392 Biased : Boolean := False) return Nat
11394 Lo : Uint := No_Uint;
11395 Hi : Uint := No_Uint;
11396 LoR : Ureal := No_Ureal;
11397 HiR : Ureal := No_Ureal;
11398 LoSet : Boolean := False;
11399 HiSet : Boolean := False;
11400 B : Uint;
11401 S : Nat;
11402 Ancest : Entity_Id;
11403 R_Typ : constant Entity_Id := Root_Type (T);
11405 begin
11406 -- If bad type, return 0
11408 if T = Any_Type then
11409 return 0;
11411 -- For generic types, just return zero. There cannot be any legitimate
11412 -- need to know such a size, but this routine may be called with a
11413 -- generic type as part of normal processing.
11415 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11416 return 0;
11418 -- Access types (cannot have size smaller than System.Address)
11420 elsif Is_Access_Type (T) then
11421 return System_Address_Size;
11423 -- Floating-point types
11425 elsif Is_Floating_Point_Type (T) then
11426 return UI_To_Int (Esize (R_Typ));
11428 -- Discrete types
11430 elsif Is_Discrete_Type (T) then
11432 -- The following loop is looking for the nearest compile time known
11433 -- bounds following the ancestor subtype chain. The idea is to find
11434 -- the most restrictive known bounds information.
11436 Ancest := T;
11437 loop
11438 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11439 return 0;
11440 end if;
11442 if not LoSet then
11443 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11444 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11445 LoSet := True;
11446 exit when HiSet;
11447 end if;
11448 end if;
11450 if not HiSet then
11451 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11452 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11453 HiSet := True;
11454 exit when LoSet;
11455 end if;
11456 end if;
11458 Ancest := Ancestor_Subtype (Ancest);
11460 if No (Ancest) then
11461 Ancest := Base_Type (T);
11463 if Is_Generic_Type (Ancest) then
11464 return 0;
11465 end if;
11466 end if;
11467 end loop;
11469 -- Fixed-point types. We can't simply use Expr_Value to get the
11470 -- Corresponding_Integer_Value values of the bounds, since these do not
11471 -- get set till the type is frozen, and this routine can be called
11472 -- before the type is frozen. Similarly the test for bounds being static
11473 -- needs to include the case where we have unanalyzed real literals for
11474 -- the same reason.
11476 elsif Is_Fixed_Point_Type (T) then
11478 -- The following loop is looking for the nearest compile time known
11479 -- bounds following the ancestor subtype chain. The idea is to find
11480 -- the most restrictive known bounds information.
11482 Ancest := T;
11483 loop
11484 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11485 return 0;
11486 end if;
11488 -- Note: In the following two tests for LoSet and HiSet, it may
11489 -- seem redundant to test for N_Real_Literal here since normally
11490 -- one would assume that the test for the value being known at
11491 -- compile time includes this case. However, there is a glitch.
11492 -- If the real literal comes from folding a non-static expression,
11493 -- then we don't consider any non- static expression to be known
11494 -- at compile time if we are in configurable run time mode (needed
11495 -- in some cases to give a clearer definition of what is and what
11496 -- is not accepted). So the test is indeed needed. Without it, we
11497 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11499 if not LoSet then
11500 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11501 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11502 then
11503 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11504 LoSet := True;
11505 exit when HiSet;
11506 end if;
11507 end if;
11509 if not HiSet then
11510 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11511 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11512 then
11513 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11514 HiSet := True;
11515 exit when LoSet;
11516 end if;
11517 end if;
11519 Ancest := Ancestor_Subtype (Ancest);
11521 if No (Ancest) then
11522 Ancest := Base_Type (T);
11524 if Is_Generic_Type (Ancest) then
11525 return 0;
11526 end if;
11527 end if;
11528 end loop;
11530 Lo := UR_To_Uint (LoR / Small_Value (T));
11531 Hi := UR_To_Uint (HiR / Small_Value (T));
11533 -- No other types allowed
11535 else
11536 raise Program_Error;
11537 end if;
11539 -- Fall through with Hi and Lo set. Deal with biased case
11541 if (Biased
11542 and then not Is_Fixed_Point_Type (T)
11543 and then not (Is_Enumeration_Type (T)
11544 and then Has_Non_Standard_Rep (T)))
11545 or else Has_Biased_Representation (T)
11546 then
11547 Hi := Hi - Lo;
11548 Lo := Uint_0;
11549 end if;
11551 -- Signed case. Note that we consider types like range 1 .. -1 to be
11552 -- signed for the purpose of computing the size, since the bounds have
11553 -- to be accommodated in the base type.
11555 if Lo < 0 or else Hi < 0 then
11556 S := 1;
11557 B := Uint_1;
11559 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11560 -- Note that we accommodate the case where the bounds cross. This
11561 -- can happen either because of the way the bounds are declared
11562 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11564 while Lo < -B
11565 or else Hi < -B
11566 or else Lo >= B
11567 or else Hi >= B
11568 loop
11569 B := Uint_2 ** S;
11570 S := S + 1;
11571 end loop;
11573 -- Unsigned case
11575 else
11576 -- If both bounds are positive, make sure that both are represen-
11577 -- table in the case where the bounds are crossed. This can happen
11578 -- either because of the way the bounds are declared, or because of
11579 -- the algorithm in Freeze_Fixed_Point_Type.
11581 if Lo > Hi then
11582 Hi := Lo;
11583 end if;
11585 -- S = size, (can accommodate 0 .. (2**size - 1))
11587 S := 0;
11588 while Hi >= Uint_2 ** S loop
11589 S := S + 1;
11590 end loop;
11591 end if;
11593 return S;
11594 end Minimum_Size;
11596 ---------------------------
11597 -- New_Stream_Subprogram --
11598 ---------------------------
11600 procedure New_Stream_Subprogram
11601 (N : Node_Id;
11602 Ent : Entity_Id;
11603 Subp : Entity_Id;
11604 Nam : TSS_Name_Type)
11606 Loc : constant Source_Ptr := Sloc (N);
11607 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11608 Subp_Id : Entity_Id;
11609 Subp_Decl : Node_Id;
11610 F : Entity_Id;
11611 Etyp : Entity_Id;
11613 Defer_Declaration : constant Boolean :=
11614 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11615 -- For a tagged type, there is a declaration for each stream attribute
11616 -- at the freeze point, and we must generate only a completion of this
11617 -- declaration. We do the same for private types, because the full view
11618 -- might be tagged. Otherwise we generate a declaration at the point of
11619 -- the attribute definition clause.
11621 function Build_Spec return Node_Id;
11622 -- Used for declaration and renaming declaration, so that this is
11623 -- treated as a renaming_as_body.
11625 ----------------
11626 -- Build_Spec --
11627 ----------------
11629 function Build_Spec return Node_Id is
11630 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11631 Formals : List_Id;
11632 Spec : Node_Id;
11633 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11635 begin
11636 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11638 -- S : access Root_Stream_Type'Class
11640 Formals := New_List (
11641 Make_Parameter_Specification (Loc,
11642 Defining_Identifier =>
11643 Make_Defining_Identifier (Loc, Name_S),
11644 Parameter_Type =>
11645 Make_Access_Definition (Loc,
11646 Subtype_Mark =>
11647 New_Occurrence_Of (
11648 Designated_Type (Etype (F)), Loc))));
11650 if Nam = TSS_Stream_Input then
11651 Spec :=
11652 Make_Function_Specification (Loc,
11653 Defining_Unit_Name => Subp_Id,
11654 Parameter_Specifications => Formals,
11655 Result_Definition => T_Ref);
11656 else
11657 -- V : [out] T
11659 Append_To (Formals,
11660 Make_Parameter_Specification (Loc,
11661 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11662 Out_Present => Out_P,
11663 Parameter_Type => T_Ref));
11665 Spec :=
11666 Make_Procedure_Specification (Loc,
11667 Defining_Unit_Name => Subp_Id,
11668 Parameter_Specifications => Formals);
11669 end if;
11671 return Spec;
11672 end Build_Spec;
11674 -- Start of processing for New_Stream_Subprogram
11676 begin
11677 F := First_Formal (Subp);
11679 if Ekind (Subp) = E_Procedure then
11680 Etyp := Etype (Next_Formal (F));
11681 else
11682 Etyp := Etype (Subp);
11683 end if;
11685 -- Prepare subprogram declaration and insert it as an action on the
11686 -- clause node. The visibility for this entity is used to test for
11687 -- visibility of the attribute definition clause (in the sense of
11688 -- 8.3(23) as amended by AI-195).
11690 if not Defer_Declaration then
11691 Subp_Decl :=
11692 Make_Subprogram_Declaration (Loc,
11693 Specification => Build_Spec);
11695 -- For a tagged type, there is always a visible declaration for each
11696 -- stream TSS (it is a predefined primitive operation), and the
11697 -- completion of this declaration occurs at the freeze point, which is
11698 -- not always visible at places where the attribute definition clause is
11699 -- visible. So, we create a dummy entity here for the purpose of
11700 -- tracking the visibility of the attribute definition clause itself.
11702 else
11703 Subp_Id :=
11704 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11705 Subp_Decl :=
11706 Make_Object_Declaration (Loc,
11707 Defining_Identifier => Subp_Id,
11708 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11709 end if;
11711 Insert_Action (N, Subp_Decl);
11712 Set_Entity (N, Subp_Id);
11714 Subp_Decl :=
11715 Make_Subprogram_Renaming_Declaration (Loc,
11716 Specification => Build_Spec,
11717 Name => New_Occurrence_Of (Subp, Loc));
11719 if Defer_Declaration then
11720 Set_TSS (Base_Type (Ent), Subp_Id);
11721 else
11722 Insert_Action (N, Subp_Decl);
11723 Copy_TSS (Subp_Id, Base_Type (Ent));
11724 end if;
11725 end New_Stream_Subprogram;
11727 ------------------------------------------
11728 -- Push_Scope_And_Install_Discriminants --
11729 ------------------------------------------
11731 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11732 begin
11733 if Has_Discriminants (E) then
11734 Push_Scope (E);
11736 -- Make discriminants visible for type declarations and protected
11737 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11739 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11740 Install_Discriminants (E);
11741 end if;
11742 end if;
11743 end Push_Scope_And_Install_Discriminants;
11745 ------------------------
11746 -- Rep_Item_Too_Early --
11747 ------------------------
11749 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
11750 begin
11751 -- Cannot apply non-operational rep items to generic types
11753 if Is_Operational_Item (N) then
11754 return False;
11756 elsif Is_Type (T)
11757 and then Is_Generic_Type (Root_Type (T))
11758 and then (Nkind (N) /= N_Pragma
11759 or else Get_Pragma_Id (N) /= Pragma_Convention)
11760 then
11761 Error_Msg_N ("representation item not allowed for generic type", N);
11762 return True;
11763 end if;
11765 -- Otherwise check for incomplete type
11767 if Is_Incomplete_Or_Private_Type (T)
11768 and then No (Underlying_Type (T))
11769 and then
11770 (Nkind (N) /= N_Pragma
11771 or else Get_Pragma_Id (N) /= Pragma_Import)
11772 then
11773 Error_Msg_N
11774 ("representation item must be after full type declaration", N);
11775 return True;
11777 -- If the type has incomplete components, a representation clause is
11778 -- illegal but stream attributes and Convention pragmas are correct.
11780 elsif Has_Private_Component (T) then
11781 if Nkind (N) = N_Pragma then
11782 return False;
11784 else
11785 Error_Msg_N
11786 ("representation item must appear after type is fully defined",
11788 return True;
11789 end if;
11790 else
11791 return False;
11792 end if;
11793 end Rep_Item_Too_Early;
11795 -----------------------
11796 -- Rep_Item_Too_Late --
11797 -----------------------
11799 function Rep_Item_Too_Late
11800 (T : Entity_Id;
11801 N : Node_Id;
11802 FOnly : Boolean := False) return Boolean
11804 S : Entity_Id;
11805 Parent_Type : Entity_Id;
11807 procedure No_Type_Rep_Item;
11808 -- Output message indicating that no type-related aspects can be
11809 -- specified due to some property of the parent type.
11811 procedure Too_Late;
11812 -- Output message for an aspect being specified too late
11814 -- Note that neither of the above errors is considered a serious one,
11815 -- since the effect is simply that we ignore the representation clause
11816 -- in these cases.
11817 -- Is this really true? In any case if we make this change we must
11818 -- document the requirement in the spec of Rep_Item_Too_Late that
11819 -- if True is returned, then the rep item must be completely ignored???
11821 ----------------------
11822 -- No_Type_Rep_Item --
11823 ----------------------
11825 procedure No_Type_Rep_Item is
11826 begin
11827 Error_Msg_N ("|type-related representation item not permitted!", N);
11828 end No_Type_Rep_Item;
11830 --------------
11831 -- Too_Late --
11832 --------------
11834 procedure Too_Late is
11835 begin
11836 -- Other compilers seem more relaxed about rep items appearing too
11837 -- late. Since analysis tools typically don't care about rep items
11838 -- anyway, no reason to be too strict about this.
11840 if not Relaxed_RM_Semantics then
11841 Error_Msg_N ("|representation item appears too late!", N);
11842 end if;
11843 end Too_Late;
11845 -- Start of processing for Rep_Item_Too_Late
11847 begin
11848 -- First make sure entity is not frozen (RM 13.1(9))
11850 if Is_Frozen (T)
11852 -- Exclude imported types, which may be frozen if they appear in a
11853 -- representation clause for a local type.
11855 and then not From_Limited_With (T)
11857 -- Exclude generated entities (not coming from source). The common
11858 -- case is when we generate a renaming which prematurely freezes the
11859 -- renamed internal entity, but we still want to be able to set copies
11860 -- of attribute values such as Size/Alignment.
11862 and then Comes_From_Source (T)
11863 then
11864 Too_Late;
11865 S := First_Subtype (T);
11867 if Present (Freeze_Node (S)) then
11868 if not Relaxed_RM_Semantics then
11869 Error_Msg_NE
11870 ("??no more representation items for }", Freeze_Node (S), S);
11871 end if;
11872 end if;
11874 return True;
11876 -- Check for case of untagged derived type whose parent either has
11877 -- primitive operations, or is a by reference type (RM 13.1(10)). In
11878 -- this case we do not output a Too_Late message, since there is no
11879 -- earlier point where the rep item could be placed to make it legal.
11881 elsif Is_Type (T)
11882 and then not FOnly
11883 and then Is_Derived_Type (T)
11884 and then not Is_Tagged_Type (T)
11885 then
11886 Parent_Type := Etype (Base_Type (T));
11888 if Has_Primitive_Operations (Parent_Type) then
11889 No_Type_Rep_Item;
11891 if not Relaxed_RM_Semantics then
11892 Error_Msg_NE
11893 ("\parent type & has primitive operations!", N, Parent_Type);
11894 end if;
11896 return True;
11898 elsif Is_By_Reference_Type (Parent_Type) then
11899 No_Type_Rep_Item;
11901 if not Relaxed_RM_Semantics then
11902 Error_Msg_NE
11903 ("\parent type & is a by reference type!", N, Parent_Type);
11904 end if;
11906 return True;
11907 end if;
11908 end if;
11910 -- No error, but one more warning to consider. The RM (surprisingly)
11911 -- allows this pattern:
11913 -- type S is ...
11914 -- primitive operations for S
11915 -- type R is new S;
11916 -- rep clause for S
11918 -- Meaning that calls on the primitive operations of S for values of
11919 -- type R may require possibly expensive implicit conversion operations.
11920 -- This is not an error, but is worth a warning.
11922 if not Relaxed_RM_Semantics and then Is_Type (T) then
11923 declare
11924 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
11926 begin
11927 if Present (DTL)
11928 and then Has_Primitive_Operations (Base_Type (T))
11930 -- For now, do not generate this warning for the case of aspect
11931 -- specification using Ada 2012 syntax, since we get wrong
11932 -- messages we do not understand. The whole business of derived
11933 -- types and rep items seems a bit confused when aspects are
11934 -- used, since the aspects are not evaluated till freeze time.
11936 and then not From_Aspect_Specification (N)
11937 then
11938 Error_Msg_Sloc := Sloc (DTL);
11939 Error_Msg_N
11940 ("representation item for& appears after derived type "
11941 & "declaration#??", N);
11942 Error_Msg_NE
11943 ("\may result in implicit conversions for primitive "
11944 & "operations of&??", N, T);
11945 Error_Msg_NE
11946 ("\to change representations when called with arguments "
11947 & "of type&??", N, DTL);
11948 end if;
11949 end;
11950 end if;
11952 -- No error, link item into head of chain of rep items for the entity,
11953 -- but avoid chaining if we have an overloadable entity, and the pragma
11954 -- is one that can apply to multiple overloaded entities.
11956 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
11957 declare
11958 Pname : constant Name_Id := Pragma_Name (N);
11959 begin
11960 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
11961 Name_External, Name_Interface)
11962 then
11963 return False;
11964 end if;
11965 end;
11966 end if;
11968 Record_Rep_Item (T, N);
11969 return False;
11970 end Rep_Item_Too_Late;
11972 -------------------------------------
11973 -- Replace_Type_References_Generic --
11974 -------------------------------------
11976 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
11977 TName : constant Name_Id := Chars (T);
11979 function Replace_Node (N : Node_Id) return Traverse_Result;
11980 -- Processes a single node in the traversal procedure below, checking
11981 -- if node N should be replaced, and if so, doing the replacement.
11983 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
11984 -- This instantiation provides the body of Replace_Type_References
11986 ------------------
11987 -- Replace_Node --
11988 ------------------
11990 function Replace_Node (N : Node_Id) return Traverse_Result is
11991 S : Entity_Id;
11992 P : Node_Id;
11994 begin
11995 -- Case of identifier
11997 if Nkind (N) = N_Identifier then
11999 -- If not the type name, check whether it is a reference to
12000 -- some other type, which must be frozen before the predicate
12001 -- function is analyzed, i.e. before the freeze node of the
12002 -- type to which the predicate applies.
12004 if Chars (N) /= TName then
12005 if Present (Current_Entity (N))
12006 and then Is_Type (Current_Entity (N))
12007 then
12008 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12009 end if;
12011 return Skip;
12013 -- Otherwise do the replacement and we are done with this node
12015 else
12016 Replace_Type_Reference (N);
12017 return Skip;
12018 end if;
12020 -- Case of selected component (which is what a qualification
12021 -- looks like in the unanalyzed tree, which is what we have.
12023 elsif Nkind (N) = N_Selected_Component then
12025 -- If selector name is not our type, keeping going (we might
12026 -- still have an occurrence of the type in the prefix).
12028 if Nkind (Selector_Name (N)) /= N_Identifier
12029 or else Chars (Selector_Name (N)) /= TName
12030 then
12031 return OK;
12033 -- Selector name is our type, check qualification
12035 else
12036 -- Loop through scopes and prefixes, doing comparison
12038 S := Current_Scope;
12039 P := Prefix (N);
12040 loop
12041 -- Continue if no more scopes or scope with no name
12043 if No (S) or else Nkind (S) not in N_Has_Chars then
12044 return OK;
12045 end if;
12047 -- Do replace if prefix is an identifier matching the
12048 -- scope that we are currently looking at.
12050 if Nkind (P) = N_Identifier
12051 and then Chars (P) = Chars (S)
12052 then
12053 Replace_Type_Reference (N);
12054 return Skip;
12055 end if;
12057 -- Go check scope above us if prefix is itself of the
12058 -- form of a selected component, whose selector matches
12059 -- the scope we are currently looking at.
12061 if Nkind (P) = N_Selected_Component
12062 and then Nkind (Selector_Name (P)) = N_Identifier
12063 and then Chars (Selector_Name (P)) = Chars (S)
12064 then
12065 S := Scope (S);
12066 P := Prefix (P);
12068 -- For anything else, we don't have a match, so keep on
12069 -- going, there are still some weird cases where we may
12070 -- still have a replacement within the prefix.
12072 else
12073 return OK;
12074 end if;
12075 end loop;
12076 end if;
12078 -- Continue for any other node kind
12080 else
12081 return OK;
12082 end if;
12083 end Replace_Node;
12085 begin
12086 Replace_Type_Refs (N);
12087 end Replace_Type_References_Generic;
12089 -------------------------
12090 -- Same_Representation --
12091 -------------------------
12093 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12094 T1 : constant Entity_Id := Underlying_Type (Typ1);
12095 T2 : constant Entity_Id := Underlying_Type (Typ2);
12097 begin
12098 -- A quick check, if base types are the same, then we definitely have
12099 -- the same representation, because the subtype specific representation
12100 -- attributes (Size and Alignment) do not affect representation from
12101 -- the point of view of this test.
12103 if Base_Type (T1) = Base_Type (T2) then
12104 return True;
12106 elsif Is_Private_Type (Base_Type (T2))
12107 and then Base_Type (T1) = Full_View (Base_Type (T2))
12108 then
12109 return True;
12110 end if;
12112 -- Tagged types never have differing representations
12114 if Is_Tagged_Type (T1) then
12115 return True;
12116 end if;
12118 -- Representations are definitely different if conventions differ
12120 if Convention (T1) /= Convention (T2) then
12121 return False;
12122 end if;
12124 -- Representations are different if component alignments or scalar
12125 -- storage orders differ.
12127 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
12128 and then
12129 (Is_Record_Type (T2) or else Is_Array_Type (T2))
12130 and then
12131 (Component_Alignment (T1) /= Component_Alignment (T2)
12132 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
12133 then
12134 return False;
12135 end if;
12137 -- For arrays, the only real issue is component size. If we know the
12138 -- component size for both arrays, and it is the same, then that's
12139 -- good enough to know we don't have a change of representation.
12141 if Is_Array_Type (T1) then
12142 if Known_Component_Size (T1)
12143 and then Known_Component_Size (T2)
12144 and then Component_Size (T1) = Component_Size (T2)
12145 then
12146 if VM_Target = No_VM then
12147 return True;
12149 -- In VM targets the representation of arrays with aliased
12150 -- components differs from arrays with non-aliased components
12152 else
12153 return Has_Aliased_Components (Base_Type (T1))
12155 Has_Aliased_Components (Base_Type (T2));
12156 end if;
12157 end if;
12158 end if;
12160 -- Types definitely have same representation if neither has non-standard
12161 -- representation since default representations are always consistent.
12162 -- If only one has non-standard representation, and the other does not,
12163 -- then we consider that they do not have the same representation. They
12164 -- might, but there is no way of telling early enough.
12166 if Has_Non_Standard_Rep (T1) then
12167 if not Has_Non_Standard_Rep (T2) then
12168 return False;
12169 end if;
12170 else
12171 return not Has_Non_Standard_Rep (T2);
12172 end if;
12174 -- Here the two types both have non-standard representation, and we need
12175 -- to determine if they have the same non-standard representation.
12177 -- For arrays, we simply need to test if the component sizes are the
12178 -- same. Pragma Pack is reflected in modified component sizes, so this
12179 -- check also deals with pragma Pack.
12181 if Is_Array_Type (T1) then
12182 return Component_Size (T1) = Component_Size (T2);
12184 -- Tagged types always have the same representation, because it is not
12185 -- possible to specify different representations for common fields.
12187 elsif Is_Tagged_Type (T1) then
12188 return True;
12190 -- Case of record types
12192 elsif Is_Record_Type (T1) then
12194 -- Packed status must conform
12196 if Is_Packed (T1) /= Is_Packed (T2) then
12197 return False;
12199 -- Otherwise we must check components. Typ2 maybe a constrained
12200 -- subtype with fewer components, so we compare the components
12201 -- of the base types.
12203 else
12204 Record_Case : declare
12205 CD1, CD2 : Entity_Id;
12207 function Same_Rep return Boolean;
12208 -- CD1 and CD2 are either components or discriminants. This
12209 -- function tests whether they have the same representation.
12211 --------------
12212 -- Same_Rep --
12213 --------------
12215 function Same_Rep return Boolean is
12216 begin
12217 if No (Component_Clause (CD1)) then
12218 return No (Component_Clause (CD2));
12219 else
12220 -- Note: at this point, component clauses have been
12221 -- normalized to the default bit order, so that the
12222 -- comparison of Component_Bit_Offsets is meaningful.
12224 return
12225 Present (Component_Clause (CD2))
12226 and then
12227 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12228 and then
12229 Esize (CD1) = Esize (CD2);
12230 end if;
12231 end Same_Rep;
12233 -- Start of processing for Record_Case
12235 begin
12236 if Has_Discriminants (T1) then
12238 -- The number of discriminants may be different if the
12239 -- derived type has fewer (constrained by values). The
12240 -- invisible discriminants retain the representation of
12241 -- the original, so the discrepancy does not per se
12242 -- indicate a different representation.
12244 CD1 := First_Discriminant (T1);
12245 CD2 := First_Discriminant (T2);
12246 while Present (CD1) and then Present (CD2) loop
12247 if not Same_Rep then
12248 return False;
12249 else
12250 Next_Discriminant (CD1);
12251 Next_Discriminant (CD2);
12252 end if;
12253 end loop;
12254 end if;
12256 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12257 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
12258 while Present (CD1) loop
12259 if not Same_Rep then
12260 return False;
12261 else
12262 Next_Component (CD1);
12263 Next_Component (CD2);
12264 end if;
12265 end loop;
12267 return True;
12268 end Record_Case;
12269 end if;
12271 -- For enumeration types, we must check each literal to see if the
12272 -- representation is the same. Note that we do not permit enumeration
12273 -- representation clauses for Character and Wide_Character, so these
12274 -- cases were already dealt with.
12276 elsif Is_Enumeration_Type (T1) then
12277 Enumeration_Case : declare
12278 L1, L2 : Entity_Id;
12280 begin
12281 L1 := First_Literal (T1);
12282 L2 := First_Literal (T2);
12283 while Present (L1) loop
12284 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12285 return False;
12286 else
12287 Next_Literal (L1);
12288 Next_Literal (L2);
12289 end if;
12290 end loop;
12292 return True;
12293 end Enumeration_Case;
12295 -- Any other types have the same representation for these purposes
12297 else
12298 return True;
12299 end if;
12300 end Same_Representation;
12302 --------------------------------
12303 -- Resolve_Iterable_Operation --
12304 --------------------------------
12306 procedure Resolve_Iterable_Operation
12307 (N : Node_Id;
12308 Cursor : Entity_Id;
12309 Typ : Entity_Id;
12310 Nam : Name_Id)
12312 Ent : Entity_Id;
12313 F1 : Entity_Id;
12314 F2 : Entity_Id;
12316 begin
12317 if not Is_Overloaded (N) then
12318 if not Is_Entity_Name (N)
12319 or else Ekind (Entity (N)) /= E_Function
12320 or else Scope (Entity (N)) /= Scope (Typ)
12321 or else No (First_Formal (Entity (N)))
12322 or else Etype (First_Formal (Entity (N))) /= Typ
12323 then
12324 Error_Msg_N ("iterable primitive must be local function name "
12325 & "whose first formal is an iterable type", N);
12326 return;
12327 end if;
12329 Ent := Entity (N);
12330 F1 := First_Formal (Ent);
12331 if Nam = Name_First then
12333 -- First (Container) => Cursor
12335 if Etype (Ent) /= Cursor then
12336 Error_Msg_N ("primitive for First must yield a curosr", N);
12337 end if;
12339 elsif Nam = Name_Next then
12341 -- Next (Container, Cursor) => Cursor
12343 F2 := Next_Formal (F1);
12345 if Etype (F2) /= Cursor
12346 or else Etype (Ent) /= Cursor
12347 or else Present (Next_Formal (F2))
12348 then
12349 Error_Msg_N ("no match for Next iterable primitive", N);
12350 end if;
12352 elsif Nam = Name_Has_Element then
12354 -- Has_Element (Container, Cursor) => Boolean
12356 F2 := Next_Formal (F1);
12357 if Etype (F2) /= Cursor
12358 or else Etype (Ent) /= Standard_Boolean
12359 or else Present (Next_Formal (F2))
12360 then
12361 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12362 end if;
12364 elsif Nam = Name_Element then
12365 F2 := Next_Formal (F1);
12367 if No (F2)
12368 or else Etype (F2) /= Cursor
12369 or else Present (Next_Formal (F2))
12370 then
12371 Error_Msg_N ("no match for Element iterable primitive", N);
12372 end if;
12373 null;
12375 else
12376 raise Program_Error;
12377 end if;
12379 else
12380 -- Overloaded case: find subprogram with proper signature.
12381 -- Caller will report error if no match is found.
12383 declare
12384 I : Interp_Index;
12385 It : Interp;
12387 begin
12388 Get_First_Interp (N, I, It);
12389 while Present (It.Typ) loop
12390 if Ekind (It.Nam) = E_Function
12391 and then Scope (It.Nam) = Scope (Typ)
12392 and then Etype (First_Formal (It.Nam)) = Typ
12393 then
12394 F1 := First_Formal (It.Nam);
12396 if Nam = Name_First then
12397 if Etype (It.Nam) = Cursor
12398 and then No (Next_Formal (F1))
12399 then
12400 Set_Entity (N, It.Nam);
12401 exit;
12402 end if;
12404 elsif Nam = Name_Next then
12405 F2 := Next_Formal (F1);
12407 if Present (F2)
12408 and then No (Next_Formal (F2))
12409 and then Etype (F2) = Cursor
12410 and then Etype (It.Nam) = Cursor
12411 then
12412 Set_Entity (N, It.Nam);
12413 exit;
12414 end if;
12416 elsif Nam = Name_Has_Element then
12417 F2 := Next_Formal (F1);
12419 if Present (F2)
12420 and then No (Next_Formal (F2))
12421 and then Etype (F2) = Cursor
12422 and then Etype (It.Nam) = Standard_Boolean
12423 then
12424 Set_Entity (N, It.Nam);
12425 F2 := Next_Formal (F1);
12426 exit;
12427 end if;
12429 elsif Nam = Name_Element then
12430 F2 := Next_Formal (F1);
12432 if Present (F2)
12433 and then No (Next_Formal (F2))
12434 and then Etype (F2) = Cursor
12435 then
12436 Set_Entity (N, It.Nam);
12437 exit;
12438 end if;
12439 end if;
12440 end if;
12442 Get_Next_Interp (I, It);
12443 end loop;
12444 end;
12445 end if;
12446 end Resolve_Iterable_Operation;
12448 ----------------
12449 -- Set_Biased --
12450 ----------------
12452 procedure Set_Biased
12453 (E : Entity_Id;
12454 N : Node_Id;
12455 Msg : String;
12456 Biased : Boolean := True)
12458 begin
12459 if Biased then
12460 Set_Has_Biased_Representation (E);
12462 if Warn_On_Biased_Representation then
12463 Error_Msg_NE
12464 ("?B?" & Msg & " forces biased representation for&", N, E);
12465 end if;
12466 end if;
12467 end Set_Biased;
12469 --------------------
12470 -- Set_Enum_Esize --
12471 --------------------
12473 procedure Set_Enum_Esize (T : Entity_Id) is
12474 Lo : Uint;
12475 Hi : Uint;
12476 Sz : Nat;
12478 begin
12479 Init_Alignment (T);
12481 -- Find the minimum standard size (8,16,32,64) that fits
12483 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12484 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12486 if Lo < 0 then
12487 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12488 Sz := Standard_Character_Size; -- May be > 8 on some targets
12490 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12491 Sz := 16;
12493 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12494 Sz := 32;
12496 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12497 Sz := 64;
12498 end if;
12500 else
12501 if Hi < Uint_2**08 then
12502 Sz := Standard_Character_Size; -- May be > 8 on some targets
12504 elsif Hi < Uint_2**16 then
12505 Sz := 16;
12507 elsif Hi < Uint_2**32 then
12508 Sz := 32;
12510 else pragma Assert (Hi < Uint_2**63);
12511 Sz := 64;
12512 end if;
12513 end if;
12515 -- That minimum is the proper size unless we have a foreign convention
12516 -- and the size required is 32 or less, in which case we bump the size
12517 -- up to 32. This is required for C and C++ and seems reasonable for
12518 -- all other foreign conventions.
12520 if Has_Foreign_Convention (T)
12521 and then Esize (T) < Standard_Integer_Size
12523 -- Don't do this if Short_Enums on target
12525 and then not Target_Short_Enums
12526 then
12527 Init_Esize (T, Standard_Integer_Size);
12528 else
12529 Init_Esize (T, Sz);
12530 end if;
12531 end Set_Enum_Esize;
12533 -----------------------------
12534 -- Uninstall_Discriminants --
12535 -----------------------------
12537 procedure Uninstall_Discriminants (E : Entity_Id) is
12538 Disc : Entity_Id;
12539 Prev : Entity_Id;
12540 Outer : Entity_Id;
12542 begin
12543 -- Discriminants have been made visible for type declarations and
12544 -- protected type declarations, not for subtype declarations.
12546 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12547 Disc := First_Discriminant (E);
12548 while Present (Disc) loop
12549 if Disc /= Current_Entity (Disc) then
12550 Prev := Current_Entity (Disc);
12551 while Present (Prev)
12552 and then Present (Homonym (Prev))
12553 and then Homonym (Prev) /= Disc
12554 loop
12555 Prev := Homonym (Prev);
12556 end loop;
12557 else
12558 Prev := Empty;
12559 end if;
12561 Set_Is_Immediately_Visible (Disc, False);
12563 Outer := Homonym (Disc);
12564 while Present (Outer) and then Scope (Outer) = E loop
12565 Outer := Homonym (Outer);
12566 end loop;
12568 -- Reset homonym link of other entities, but do not modify link
12569 -- between entities in current scope, so that the back-end can
12570 -- have a proper count of local overloadings.
12572 if No (Prev) then
12573 Set_Name_Entity_Id (Chars (Disc), Outer);
12575 elsif Scope (Prev) /= Scope (Disc) then
12576 Set_Homonym (Prev, Outer);
12577 end if;
12579 Next_Discriminant (Disc);
12580 end loop;
12581 end if;
12582 end Uninstall_Discriminants;
12584 -------------------------------------------
12585 -- Uninstall_Discriminants_And_Pop_Scope --
12586 -------------------------------------------
12588 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12589 begin
12590 if Has_Discriminants (E) then
12591 Uninstall_Discriminants (E);
12592 Pop_Scope;
12593 end if;
12594 end Uninstall_Discriminants_And_Pop_Scope;
12596 ------------------------------
12597 -- Validate_Address_Clauses --
12598 ------------------------------
12600 procedure Validate_Address_Clauses is
12601 begin
12602 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12603 declare
12604 ACCR : Address_Clause_Check_Record
12605 renames Address_Clause_Checks.Table (J);
12607 Expr : Node_Id;
12609 X_Alignment : Uint;
12610 Y_Alignment : Uint;
12612 X_Size : Uint;
12613 Y_Size : Uint;
12615 begin
12616 -- Skip processing of this entry if warning already posted
12618 if not Address_Warning_Posted (ACCR.N) then
12619 Expr := Original_Node (Expression (ACCR.N));
12621 -- Get alignments
12623 X_Alignment := Alignment (ACCR.X);
12624 Y_Alignment := Alignment (ACCR.Y);
12626 -- Similarly obtain sizes
12628 X_Size := Esize (ACCR.X);
12629 Y_Size := Esize (ACCR.Y);
12631 -- Check for large object overlaying smaller one
12633 if Y_Size > Uint_0
12634 and then X_Size > Uint_0
12635 and then X_Size > Y_Size
12636 then
12637 Error_Msg_NE
12638 ("??& overlays smaller object", ACCR.N, ACCR.X);
12639 Error_Msg_N
12640 ("\??program execution may be erroneous", ACCR.N);
12641 Error_Msg_Uint_1 := X_Size;
12642 Error_Msg_NE
12643 ("\??size of & is ^", ACCR.N, ACCR.X);
12644 Error_Msg_Uint_1 := Y_Size;
12645 Error_Msg_NE
12646 ("\??size of & is ^", ACCR.N, ACCR.Y);
12648 -- Check for inadequate alignment, both of the base object
12649 -- and of the offset, if any.
12651 -- Note: we do not check the alignment if we gave a size
12652 -- warning, since it would likely be redundant.
12654 elsif Y_Alignment /= Uint_0
12655 and then (Y_Alignment < X_Alignment
12656 or else (ACCR.Off
12657 and then
12658 Nkind (Expr) = N_Attribute_Reference
12659 and then
12660 Attribute_Name (Expr) = Name_Address
12661 and then
12662 Has_Compatible_Alignment
12663 (ACCR.X, Prefix (Expr))
12664 /= Known_Compatible))
12665 then
12666 Error_Msg_NE
12667 ("??specified address for& may be inconsistent "
12668 & "with alignment", ACCR.N, ACCR.X);
12669 Error_Msg_N
12670 ("\??program execution may be erroneous (RM 13.3(27))",
12671 ACCR.N);
12672 Error_Msg_Uint_1 := X_Alignment;
12673 Error_Msg_NE
12674 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12675 Error_Msg_Uint_1 := Y_Alignment;
12676 Error_Msg_NE
12677 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12678 if Y_Alignment >= X_Alignment then
12679 Error_Msg_N
12680 ("\??but offset is not multiple of alignment", ACCR.N);
12681 end if;
12682 end if;
12683 end if;
12684 end;
12685 end loop;
12686 end Validate_Address_Clauses;
12688 ---------------------------
12689 -- Validate_Independence --
12690 ---------------------------
12692 procedure Validate_Independence is
12693 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12694 N : Node_Id;
12695 E : Entity_Id;
12696 IC : Boolean;
12697 Comp : Entity_Id;
12698 Addr : Node_Id;
12699 P : Node_Id;
12701 procedure Check_Array_Type (Atyp : Entity_Id);
12702 -- Checks if the array type Atyp has independent components, and
12703 -- if not, outputs an appropriate set of error messages.
12705 procedure No_Independence;
12706 -- Output message that independence cannot be guaranteed
12708 function OK_Component (C : Entity_Id) return Boolean;
12709 -- Checks one component to see if it is independently accessible, and
12710 -- if so yields True, otherwise yields False if independent access
12711 -- cannot be guaranteed. This is a conservative routine, it only
12712 -- returns True if it knows for sure, it returns False if it knows
12713 -- there is a problem, or it cannot be sure there is no problem.
12715 procedure Reason_Bad_Component (C : Entity_Id);
12716 -- Outputs continuation message if a reason can be determined for
12717 -- the component C being bad.
12719 ----------------------
12720 -- Check_Array_Type --
12721 ----------------------
12723 procedure Check_Array_Type (Atyp : Entity_Id) is
12724 Ctyp : constant Entity_Id := Component_Type (Atyp);
12726 begin
12727 -- OK if no alignment clause, no pack, and no component size
12729 if not Has_Component_Size_Clause (Atyp)
12730 and then not Has_Alignment_Clause (Atyp)
12731 and then not Is_Packed (Atyp)
12732 then
12733 return;
12734 end if;
12736 -- Case of component size is greater than or equal to 64 and the
12737 -- alignment of the array is at least as large as the alignment
12738 -- of the component. We are definitely OK in this situation.
12740 if Known_Component_Size (Atyp)
12741 and then Component_Size (Atyp) >= 64
12742 and then Known_Alignment (Atyp)
12743 and then Known_Alignment (Ctyp)
12744 and then Alignment (Atyp) >= Alignment (Ctyp)
12745 then
12746 return;
12747 end if;
12749 -- Check actual component size
12751 if not Known_Component_Size (Atyp)
12752 or else not (Addressable (Component_Size (Atyp))
12753 and then Component_Size (Atyp) < 64)
12754 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12755 then
12756 No_Independence;
12758 -- Bad component size, check reason
12760 if Has_Component_Size_Clause (Atyp) then
12761 P := Get_Attribute_Definition_Clause
12762 (Atyp, Attribute_Component_Size);
12764 if Present (P) then
12765 Error_Msg_Sloc := Sloc (P);
12766 Error_Msg_N ("\because of Component_Size clause#", N);
12767 return;
12768 end if;
12769 end if;
12771 if Is_Packed (Atyp) then
12772 P := Get_Rep_Pragma (Atyp, Name_Pack);
12774 if Present (P) then
12775 Error_Msg_Sloc := Sloc (P);
12776 Error_Msg_N ("\because of pragma Pack#", N);
12777 return;
12778 end if;
12779 end if;
12781 -- No reason found, just return
12783 return;
12784 end if;
12786 -- Array type is OK independence-wise
12788 return;
12789 end Check_Array_Type;
12791 ---------------------
12792 -- No_Independence --
12793 ---------------------
12795 procedure No_Independence is
12796 begin
12797 if Pragma_Name (N) = Name_Independent then
12798 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
12799 else
12800 Error_Msg_NE
12801 ("independent components cannot be guaranteed for&", N, E);
12802 end if;
12803 end No_Independence;
12805 ------------------
12806 -- OK_Component --
12807 ------------------
12809 function OK_Component (C : Entity_Id) return Boolean is
12810 Rec : constant Entity_Id := Scope (C);
12811 Ctyp : constant Entity_Id := Etype (C);
12813 begin
12814 -- OK if no component clause, no Pack, and no alignment clause
12816 if No (Component_Clause (C))
12817 and then not Is_Packed (Rec)
12818 and then not Has_Alignment_Clause (Rec)
12819 then
12820 return True;
12821 end if;
12823 -- Here we look at the actual component layout. A component is
12824 -- addressable if its size is a multiple of the Esize of the
12825 -- component type, and its starting position in the record has
12826 -- appropriate alignment, and the record itself has appropriate
12827 -- alignment to guarantee the component alignment.
12829 -- Make sure sizes are static, always assume the worst for any
12830 -- cases where we cannot check static values.
12832 if not (Known_Static_Esize (C)
12833 and then
12834 Known_Static_Esize (Ctyp))
12835 then
12836 return False;
12837 end if;
12839 -- Size of component must be addressable or greater than 64 bits
12840 -- and a multiple of bytes.
12842 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
12843 return False;
12844 end if;
12846 -- Check size is proper multiple
12848 if Esize (C) mod Esize (Ctyp) /= 0 then
12849 return False;
12850 end if;
12852 -- Check alignment of component is OK
12854 if not Known_Component_Bit_Offset (C)
12855 or else Component_Bit_Offset (C) < Uint_0
12856 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
12857 then
12858 return False;
12859 end if;
12861 -- Check alignment of record type is OK
12863 if not Known_Alignment (Rec)
12864 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12865 then
12866 return False;
12867 end if;
12869 -- All tests passed, component is addressable
12871 return True;
12872 end OK_Component;
12874 --------------------------
12875 -- Reason_Bad_Component --
12876 --------------------------
12878 procedure Reason_Bad_Component (C : Entity_Id) is
12879 Rec : constant Entity_Id := Scope (C);
12880 Ctyp : constant Entity_Id := Etype (C);
12882 begin
12883 -- If component clause present assume that's the problem
12885 if Present (Component_Clause (C)) then
12886 Error_Msg_Sloc := Sloc (Component_Clause (C));
12887 Error_Msg_N ("\because of Component_Clause#", N);
12888 return;
12889 end if;
12891 -- If pragma Pack clause present, assume that's the problem
12893 if Is_Packed (Rec) then
12894 P := Get_Rep_Pragma (Rec, Name_Pack);
12896 if Present (P) then
12897 Error_Msg_Sloc := Sloc (P);
12898 Error_Msg_N ("\because of pragma Pack#", N);
12899 return;
12900 end if;
12901 end if;
12903 -- See if record has bad alignment clause
12905 if Has_Alignment_Clause (Rec)
12906 and then Known_Alignment (Rec)
12907 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12908 then
12909 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
12911 if Present (P) then
12912 Error_Msg_Sloc := Sloc (P);
12913 Error_Msg_N ("\because of Alignment clause#", N);
12914 end if;
12915 end if;
12917 -- Couldn't find a reason, so return without a message
12919 return;
12920 end Reason_Bad_Component;
12922 -- Start of processing for Validate_Independence
12924 begin
12925 for J in Independence_Checks.First .. Independence_Checks.Last loop
12926 N := Independence_Checks.Table (J).N;
12927 E := Independence_Checks.Table (J).E;
12928 IC := Pragma_Name (N) = Name_Independent_Components;
12930 -- Deal with component case
12932 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
12933 if not OK_Component (E) then
12934 No_Independence;
12935 Reason_Bad_Component (E);
12936 goto Continue;
12937 end if;
12938 end if;
12940 -- Deal with record with Independent_Components
12942 if IC and then Is_Record_Type (E) then
12943 Comp := First_Component_Or_Discriminant (E);
12944 while Present (Comp) loop
12945 if not OK_Component (Comp) then
12946 No_Independence;
12947 Reason_Bad_Component (Comp);
12948 goto Continue;
12949 end if;
12951 Next_Component_Or_Discriminant (Comp);
12952 end loop;
12953 end if;
12955 -- Deal with address clause case
12957 if Is_Object (E) then
12958 Addr := Address_Clause (E);
12960 if Present (Addr) then
12961 No_Independence;
12962 Error_Msg_Sloc := Sloc (Addr);
12963 Error_Msg_N ("\because of Address clause#", N);
12964 goto Continue;
12965 end if;
12966 end if;
12968 -- Deal with independent components for array type
12970 if IC and then Is_Array_Type (E) then
12971 Check_Array_Type (E);
12972 end if;
12974 -- Deal with independent components for array object
12976 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
12977 Check_Array_Type (Etype (E));
12978 end if;
12980 <<Continue>> null;
12981 end loop;
12982 end Validate_Independence;
12984 ------------------------------
12985 -- Validate_Iterable_Aspect --
12986 ------------------------------
12988 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
12989 Assoc : Node_Id;
12990 Expr : Node_Id;
12992 Prim : Node_Id;
12993 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
12995 First_Id : Entity_Id;
12996 Next_Id : Entity_Id;
12997 Has_Element_Id : Entity_Id;
12998 Element_Id : Entity_Id;
13000 begin
13001 -- If previous error aspect is unusable
13003 if Cursor = Any_Type then
13004 return;
13005 end if;
13007 First_Id := Empty;
13008 Next_Id := Empty;
13009 Has_Element_Id := Empty;
13010 Element_Id := Empty;
13012 -- Each expression must resolve to a function with the proper signature
13014 Assoc := First (Component_Associations (Expression (ASN)));
13015 while Present (Assoc) loop
13016 Expr := Expression (Assoc);
13017 Analyze (Expr);
13019 Prim := First (Choices (Assoc));
13021 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
13022 Error_Msg_N ("illegal name in association", Prim);
13024 elsif Chars (Prim) = Name_First then
13025 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
13026 First_Id := Entity (Expr);
13028 elsif Chars (Prim) = Name_Next then
13029 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
13030 Next_Id := Entity (Expr);
13032 elsif Chars (Prim) = Name_Has_Element then
13033 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
13034 Has_Element_Id := Entity (Expr);
13036 elsif Chars (Prim) = Name_Element then
13037 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
13038 Element_Id := Entity (Expr);
13040 else
13041 Error_Msg_N ("invalid name for iterable function", Prim);
13042 end if;
13044 Next (Assoc);
13045 end loop;
13047 if No (First_Id) then
13048 Error_Msg_N ("match for First primitive not found", ASN);
13050 elsif No (Next_Id) then
13051 Error_Msg_N ("match for Next primitive not found", ASN);
13053 elsif No (Has_Element_Id) then
13054 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13056 elsif No (Element_Id) then
13057 null; -- Optional.
13058 end if;
13059 end Validate_Iterable_Aspect;
13061 -----------------------------------
13062 -- Validate_Unchecked_Conversion --
13063 -----------------------------------
13065 procedure Validate_Unchecked_Conversion
13066 (N : Node_Id;
13067 Act_Unit : Entity_Id)
13069 Source : Entity_Id;
13070 Target : Entity_Id;
13071 Vnode : Node_Id;
13073 begin
13074 -- Obtain source and target types. Note that we call Ancestor_Subtype
13075 -- here because the processing for generic instantiation always makes
13076 -- subtypes, and we want the original frozen actual types.
13078 -- If we are dealing with private types, then do the check on their
13079 -- fully declared counterparts if the full declarations have been
13080 -- encountered (they don't have to be visible, but they must exist).
13082 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13084 if Is_Private_Type (Source)
13085 and then Present (Underlying_Type (Source))
13086 then
13087 Source := Underlying_Type (Source);
13088 end if;
13090 Target := Ancestor_Subtype (Etype (Act_Unit));
13092 -- If either type is generic, the instantiation happens within a generic
13093 -- unit, and there is nothing to check. The proper check will happen
13094 -- when the enclosing generic is instantiated.
13096 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13097 return;
13098 end if;
13100 if Is_Private_Type (Target)
13101 and then Present (Underlying_Type (Target))
13102 then
13103 Target := Underlying_Type (Target);
13104 end if;
13106 -- Source may be unconstrained array, but not target
13108 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
13109 Error_Msg_N
13110 ("unchecked conversion to unconstrained array not allowed", N);
13111 return;
13112 end if;
13114 -- Warn if conversion between two different convention pointers
13116 if Is_Access_Type (Target)
13117 and then Is_Access_Type (Source)
13118 and then Convention (Target) /= Convention (Source)
13119 and then Warn_On_Unchecked_Conversion
13120 then
13121 -- Give warnings for subprogram pointers only on most targets
13123 if Is_Access_Subprogram_Type (Target)
13124 or else Is_Access_Subprogram_Type (Source)
13125 then
13126 Error_Msg_N
13127 ("?z?conversion between pointers with different conventions!",
13129 end if;
13130 end if;
13132 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13133 -- warning when compiling GNAT-related sources.
13135 if Warn_On_Unchecked_Conversion
13136 and then not In_Predefined_Unit (N)
13137 and then RTU_Loaded (Ada_Calendar)
13138 and then (Chars (Source) = Name_Time
13139 or else
13140 Chars (Target) = Name_Time)
13141 then
13142 -- If Ada.Calendar is loaded and the name of one of the operands is
13143 -- Time, there is a good chance that this is Ada.Calendar.Time.
13145 declare
13146 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
13147 begin
13148 pragma Assert (Present (Calendar_Time));
13150 if Source = Calendar_Time or else Target = Calendar_Time then
13151 Error_Msg_N
13152 ("?z?representation of 'Time values may change between "
13153 & "'G'N'A'T versions", N);
13154 end if;
13155 end;
13156 end if;
13158 -- Make entry in unchecked conversion table for later processing by
13159 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13160 -- (using values set by the back-end where possible). This is only done
13161 -- if the appropriate warning is active.
13163 if Warn_On_Unchecked_Conversion then
13164 Unchecked_Conversions.Append
13165 (New_Val => UC_Entry'(Eloc => Sloc (N),
13166 Source => Source,
13167 Target => Target,
13168 Act_Unit => Act_Unit));
13170 -- If both sizes are known statically now, then back end annotation
13171 -- is not required to do a proper check but if either size is not
13172 -- known statically, then we need the annotation.
13174 if Known_Static_RM_Size (Source)
13175 and then
13176 Known_Static_RM_Size (Target)
13177 then
13178 null;
13179 else
13180 Back_Annotate_Rep_Info := True;
13181 end if;
13182 end if;
13184 -- If unchecked conversion to access type, and access type is declared
13185 -- in the same unit as the unchecked conversion, then set the flag
13186 -- No_Strict_Aliasing (no strict aliasing is implicit here)
13188 if Is_Access_Type (Target) and then
13189 In_Same_Source_Unit (Target, N)
13190 then
13191 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13192 end if;
13194 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13195 -- the back end needs to perform special validation checks.
13197 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13198 -- have full expansion and the back end is called ???
13200 Vnode :=
13201 Make_Validate_Unchecked_Conversion (Sloc (N));
13202 Set_Source_Type (Vnode, Source);
13203 Set_Target_Type (Vnode, Target);
13205 -- If the unchecked conversion node is in a list, just insert before it.
13206 -- If not we have some strange case, not worth bothering about.
13208 if Is_List_Member (N) then
13209 Insert_After (N, Vnode);
13210 end if;
13211 end Validate_Unchecked_Conversion;
13213 ------------------------------------
13214 -- Validate_Unchecked_Conversions --
13215 ------------------------------------
13217 procedure Validate_Unchecked_Conversions is
13218 begin
13219 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13220 declare
13221 T : UC_Entry renames Unchecked_Conversions.Table (N);
13223 Eloc : constant Source_Ptr := T.Eloc;
13224 Source : constant Entity_Id := T.Source;
13225 Target : constant Entity_Id := T.Target;
13226 Act_Unit : constant Entity_Id := T.Act_Unit;
13228 Source_Siz : Uint;
13229 Target_Siz : Uint;
13231 begin
13232 -- Skip if function marked as warnings off
13234 if Warnings_Off (Act_Unit) then
13235 goto Continue;
13236 end if;
13238 -- This validation check, which warns if we have unequal sizes for
13239 -- unchecked conversion, and thus potentially implementation
13240 -- dependent semantics, is one of the few occasions on which we
13241 -- use the official RM size instead of Esize. See description in
13242 -- Einfo "Handling of Type'Size Values" for details.
13244 if Serious_Errors_Detected = 0
13245 and then Known_Static_RM_Size (Source)
13246 and then Known_Static_RM_Size (Target)
13248 -- Don't do the check if warnings off for either type, note the
13249 -- deliberate use of OR here instead of OR ELSE to get the flag
13250 -- Warnings_Off_Used set for both types if appropriate.
13252 and then not (Has_Warnings_Off (Source)
13254 Has_Warnings_Off (Target))
13255 then
13256 Source_Siz := RM_Size (Source);
13257 Target_Siz := RM_Size (Target);
13259 if Source_Siz /= Target_Siz then
13260 Error_Msg
13261 ("?z?types for unchecked conversion have different sizes!",
13262 Eloc);
13264 if All_Errors_Mode then
13265 Error_Msg_Name_1 := Chars (Source);
13266 Error_Msg_Uint_1 := Source_Siz;
13267 Error_Msg_Name_2 := Chars (Target);
13268 Error_Msg_Uint_2 := Target_Siz;
13269 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
13271 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13273 if Is_Discrete_Type (Source)
13274 and then
13275 Is_Discrete_Type (Target)
13276 then
13277 if Source_Siz > Target_Siz then
13278 Error_Msg
13279 ("\?z?^ high order bits of source will "
13280 & "be ignored!", Eloc);
13282 elsif Is_Unsigned_Type (Source) then
13283 Error_Msg
13284 ("\?z?source will be extended with ^ high order "
13285 & "zero bits!", Eloc);
13287 else
13288 Error_Msg
13289 ("\?z?source will be extended with ^ high order "
13290 & "sign bits!", Eloc);
13291 end if;
13293 elsif Source_Siz < Target_Siz then
13294 if Is_Discrete_Type (Target) then
13295 if Bytes_Big_Endian then
13296 Error_Msg
13297 ("\?z?target value will include ^ undefined "
13298 & "low order bits!", Eloc);
13299 else
13300 Error_Msg
13301 ("\?z?target value will include ^ undefined "
13302 & "high order bits!", Eloc);
13303 end if;
13305 else
13306 Error_Msg
13307 ("\?z?^ trailing bits of target value will be "
13308 & "undefined!", Eloc);
13309 end if;
13311 else pragma Assert (Source_Siz > Target_Siz);
13312 Error_Msg
13313 ("\?z?^ trailing bits of source will be ignored!",
13314 Eloc);
13315 end if;
13316 end if;
13317 end if;
13318 end if;
13320 -- If both types are access types, we need to check the alignment.
13321 -- If the alignment of both is specified, we can do it here.
13323 if Serious_Errors_Detected = 0
13324 and then Is_Access_Type (Source)
13325 and then Is_Access_Type (Target)
13326 and then Target_Strict_Alignment
13327 and then Present (Designated_Type (Source))
13328 and then Present (Designated_Type (Target))
13329 then
13330 declare
13331 D_Source : constant Entity_Id := Designated_Type (Source);
13332 D_Target : constant Entity_Id := Designated_Type (Target);
13334 begin
13335 if Known_Alignment (D_Source)
13336 and then
13337 Known_Alignment (D_Target)
13338 then
13339 declare
13340 Source_Align : constant Uint := Alignment (D_Source);
13341 Target_Align : constant Uint := Alignment (D_Target);
13343 begin
13344 if Source_Align < Target_Align
13345 and then not Is_Tagged_Type (D_Source)
13347 -- Suppress warning if warnings suppressed on either
13348 -- type or either designated type. Note the use of
13349 -- OR here instead of OR ELSE. That is intentional,
13350 -- we would like to set flag Warnings_Off_Used in
13351 -- all types for which warnings are suppressed.
13353 and then not (Has_Warnings_Off (D_Source)
13355 Has_Warnings_Off (D_Target)
13357 Has_Warnings_Off (Source)
13359 Has_Warnings_Off (Target))
13360 then
13361 Error_Msg_Uint_1 := Target_Align;
13362 Error_Msg_Uint_2 := Source_Align;
13363 Error_Msg_Node_1 := D_Target;
13364 Error_Msg_Node_2 := D_Source;
13365 Error_Msg
13366 ("?z?alignment of & (^) is stricter than "
13367 & "alignment of & (^)!", Eloc);
13368 Error_Msg
13369 ("\?z?resulting access value may have invalid "
13370 & "alignment!", Eloc);
13371 end if;
13372 end;
13373 end if;
13374 end;
13375 end if;
13376 end;
13378 <<Continue>>
13379 null;
13380 end loop;
13381 end Validate_Unchecked_Conversions;
13383 end Sem_Ch13;