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