2014-10-17 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch13.adb
blob9ab019a064834313279a3bee26c6c8ad9b740842
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Case; use Sem_Case;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Sinput; use Sinput;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Ttypes; use Ttypes;
67 with Tbuild; use Tbuild;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
71 with GNAT.Heap_Sort_G;
73 package body Sem_Ch13 is
75 SSU : constant Pos := System_Storage_Unit;
76 -- Convenient short hand for commonly used constant
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
83 -- This routine is called after setting one of the sizes of type entity
84 -- Typ to Size. The purpose is to deal with the situation of a derived
85 -- type whose inherited alignment is no longer appropriate for the new
86 -- size value. In this case, we reset the Alignment to unknown.
88 procedure Build_Discrete_Static_Predicate
89 (Typ : Entity_Id;
90 Expr : Node_Id;
91 Nam : Name_Id);
92 -- Given a predicated type Typ, where Typ is a discrete static subtype,
93 -- whose predicate expression is Expr, tests if Expr is a static predicate,
94 -- and if so, builds the predicate range list. Nam is the name of the one
95 -- argument to the predicate function. Occurrences of the type name in the
96 -- predicate expression have been replaced by identifier references to this
97 -- name, which is unique, so any identifier with Chars matching Nam must be
98 -- a reference to the type. If the predicate is non-static, this procedure
99 -- returns doing nothing. If the predicate is static, then the predicate
100 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
101 -- rewritten as a canonicalized membership operation.
103 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
104 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
105 -- then either there are pragma Predicate entries on the rep chain for the
106 -- type (note that Predicate aspects are converted to pragma Predicate), or
107 -- there are inherited aspects from a parent type, or ancestor subtypes.
108 -- This procedure builds the spec and body for the Predicate function that
109 -- tests these predicates. N is the freeze node for the type. The spec of
110 -- the function is inserted before the freeze node, and the body of the
111 -- function is inserted after the freeze node. If the predicate expression
112 -- has at least one Raise_Expression, then this procedure also builds the
113 -- M version of the predicate function for use in membership tests.
115 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
116 -- Called if both Storage_Pool and Storage_Size attribute definition
117 -- clauses (SP and SS) are present for entity Ent. Issue error message.
119 procedure Freeze_Entity_Checks (N : Node_Id);
120 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
121 -- to generate appropriate semantic checks that are delayed until this
122 -- point (they had to be delayed this long for cases of delayed aspects,
123 -- e.g. analysis of statically predicated subtypes in choices, for which
124 -- we have to be sure the subtypes in question are frozen before checking.
126 function Get_Alignment_Value (Expr : Node_Id) return Uint;
127 -- Given the expression for an alignment value, returns the corresponding
128 -- Uint value. If the value is inappropriate, then error messages are
129 -- posted as required, and a value of No_Uint is returned.
131 function Is_Operational_Item (N : Node_Id) return Boolean;
132 -- A specification for a stream attribute is allowed before the full type
133 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
134 -- that do not specify a representation characteristic are operational
135 -- attributes.
137 function Is_Predicate_Static
138 (Expr : Node_Id;
139 Nam : Name_Id) return Boolean;
140 -- Given predicate expression Expr, tests if Expr is predicate-static in
141 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
142 -- name in the predicate expression have been replaced by references to
143 -- an identifier whose Chars field is Nam. This name is unique, so any
144 -- identifier with Chars matching Nam must be a reference to the type.
145 -- Returns True if the expression is predicate-static and False otherwise,
146 -- but is not in the business of setting flags or issuing error messages.
148 -- Only scalar types can have static predicates, so False is always
149 -- returned for non-scalar types.
151 -- Note: the RM seems to suggest that string types can also have static
152 -- predicates. But that really makes lttle sense as very few useful
153 -- predicates can be constructed for strings. Remember that:
155 -- "ABC" < "DEF"
157 -- is not a static expression. So even though the clearly faulty RM wording
158 -- allows the following:
160 -- subtype S is String with Static_Predicate => S < "DEF"
162 -- We can't allow this, otherwise we have predicate-static applying to a
163 -- larger class than static expressions, which was never intended.
165 procedure New_Stream_Subprogram
166 (N : Node_Id;
167 Ent : Entity_Id;
168 Subp : Entity_Id;
169 Nam : TSS_Name_Type);
170 -- Create a subprogram renaming of a given stream attribute to the
171 -- designated subprogram and then in the tagged case, provide this as a
172 -- primitive operation, or in the untagged case make an appropriate TSS
173 -- entry. This is more properly an expansion activity than just semantics,
174 -- but the presence of user-defined stream functions for limited types
175 -- is a legality check, which is why this takes place here rather than in
176 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
177 -- function to be generated.
179 -- To avoid elaboration anomalies with freeze nodes, for untagged types
180 -- we generate both a subprogram declaration and a subprogram renaming
181 -- declaration, so that the attribute specification is handled as a
182 -- renaming_as_body. For tagged types, the specification is one of the
183 -- primitive specs.
185 procedure Resolve_Iterable_Operation
186 (N : Node_Id;
187 Cursor : Entity_Id;
188 Typ : Entity_Id;
189 Nam : Name_Id);
190 -- If the name of a primitive operation for an Iterable aspect is
191 -- overloaded, resolve according to required signature.
193 procedure Set_Biased
194 (E : Entity_Id;
195 N : Node_Id;
196 Msg : String;
197 Biased : Boolean := True);
198 -- If Biased is True, sets Has_Biased_Representation flag for E, and
199 -- outputs a warning message at node N if Warn_On_Biased_Representation is
200 -- is True. This warning inserts the string Msg to describe the construct
201 -- causing biasing.
203 ----------------------------------------------
204 -- Table for Validate_Unchecked_Conversions --
205 ----------------------------------------------
207 -- The following table collects unchecked conversions for validation.
208 -- Entries are made by Validate_Unchecked_Conversion and then the call
209 -- to Validate_Unchecked_Conversions does the actual error checking and
210 -- posting of warnings. The reason for this delayed processing is to take
211 -- advantage of back-annotations of size and alignment values performed by
212 -- the back end.
214 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
215 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
216 -- already have modified all Sloc values if the -gnatD option is set.
218 type UC_Entry is record
219 Eloc : Source_Ptr; -- node used for posting warnings
220 Source : Entity_Id; -- source type for unchecked conversion
221 Target : Entity_Id; -- target type for unchecked conversion
222 Act_Unit : Entity_Id; -- actual function instantiated
223 end record;
225 package Unchecked_Conversions is new Table.Table (
226 Table_Component_Type => UC_Entry,
227 Table_Index_Type => Int,
228 Table_Low_Bound => 1,
229 Table_Initial => 50,
230 Table_Increment => 200,
231 Table_Name => "Unchecked_Conversions");
233 ----------------------------------------
234 -- Table for Validate_Address_Clauses --
235 ----------------------------------------
237 -- If an address clause has the form
239 -- for X'Address use Expr
241 -- where Expr is of the form Y'Address or recursively is a reference to a
242 -- constant of either of these forms, and X and Y are entities of objects,
243 -- then if Y has a smaller alignment than X, that merits a warning about
244 -- possible bad alignment. The following table collects address clauses of
245 -- this kind. We put these in a table so that they can be checked after the
246 -- back end has completed annotation of the alignments of objects, since we
247 -- can catch more cases that way.
249 type Address_Clause_Check_Record is record
250 N : Node_Id;
251 -- The address clause
253 X : Entity_Id;
254 -- The entity of the object overlaying Y
256 Y : Entity_Id;
257 -- The entity of the object being overlaid
259 Off : Boolean;
260 -- Whether the address is offset within Y
261 end record;
263 package Address_Clause_Checks is new Table.Table (
264 Table_Component_Type => Address_Clause_Check_Record,
265 Table_Index_Type => Int,
266 Table_Low_Bound => 1,
267 Table_Initial => 20,
268 Table_Increment => 200,
269 Table_Name => "Address_Clause_Checks");
271 -----------------------------------------
272 -- Adjust_Record_For_Reverse_Bit_Order --
273 -----------------------------------------
275 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
276 Comp : Node_Id;
277 CC : Node_Id;
279 begin
280 -- Processing depends on version of Ada
282 -- For Ada 95, we just renumber bits within a storage unit. We do the
283 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
284 -- Ada 83, and are free to add this extension.
286 if Ada_Version < Ada_2005 then
287 Comp := First_Component_Or_Discriminant (R);
288 while Present (Comp) loop
289 CC := Component_Clause (Comp);
291 -- If component clause is present, then deal with the non-default
292 -- bit order case for Ada 95 mode.
294 -- We only do this processing for the base type, and in fact that
295 -- is important, since otherwise if there are record subtypes, we
296 -- could reverse the bits once for each subtype, which is wrong.
298 if Present (CC) and then Ekind (R) = E_Record_Type then
299 declare
300 CFB : constant Uint := Component_Bit_Offset (Comp);
301 CSZ : constant Uint := Esize (Comp);
302 CLC : constant Node_Id := Component_Clause (Comp);
303 Pos : constant Node_Id := Position (CLC);
304 FB : constant Node_Id := First_Bit (CLC);
306 Storage_Unit_Offset : constant Uint :=
307 CFB / System_Storage_Unit;
309 Start_Bit : constant Uint :=
310 CFB mod System_Storage_Unit;
312 begin
313 -- Cases where field goes over storage unit boundary
315 if Start_Bit + CSZ > System_Storage_Unit then
317 -- Allow multi-byte field but generate warning
319 if Start_Bit mod System_Storage_Unit = 0
320 and then CSZ mod System_Storage_Unit = 0
321 then
322 Error_Msg_N
323 ("info: multi-byte field specified with "
324 & "non-standard Bit_Order?V?", CLC);
326 if Bytes_Big_Endian then
327 Error_Msg_N
328 ("\bytes are not reversed "
329 & "(component is big-endian)?V?", CLC);
330 else
331 Error_Msg_N
332 ("\bytes are not reversed "
333 & "(component is little-endian)?V?", CLC);
334 end if;
336 -- Do not allow non-contiguous field
338 else
339 Error_Msg_N
340 ("attempt to specify non-contiguous field "
341 & "not permitted", CLC);
342 Error_Msg_N
343 ("\caused by non-standard Bit_Order "
344 & "specified", CLC);
345 Error_Msg_N
346 ("\consider possibility of using "
347 & "Ada 2005 mode here", CLC);
348 end if;
350 -- Case where field fits in one storage unit
352 else
353 -- Give warning if suspicious component clause
355 if Intval (FB) >= System_Storage_Unit
356 and then Warn_On_Reverse_Bit_Order
357 then
358 Error_Msg_N
359 ("info: Bit_Order clause does not affect " &
360 "byte ordering?V?", Pos);
361 Error_Msg_Uint_1 :=
362 Intval (Pos) + Intval (FB) /
363 System_Storage_Unit;
364 Error_Msg_N
365 ("info: position normalized to ^ before bit " &
366 "order interpreted?V?", Pos);
367 end if;
369 -- Here is where we fix up the Component_Bit_Offset value
370 -- to account for the reverse bit order. Some examples of
371 -- what needs to be done are:
373 -- First_Bit .. Last_Bit Component_Bit_Offset
374 -- old new old new
376 -- 0 .. 0 7 .. 7 0 7
377 -- 0 .. 1 6 .. 7 0 6
378 -- 0 .. 2 5 .. 7 0 5
379 -- 0 .. 7 0 .. 7 0 4
381 -- 1 .. 1 6 .. 6 1 6
382 -- 1 .. 4 3 .. 6 1 3
383 -- 4 .. 7 0 .. 3 4 0
385 -- The rule is that the first bit is is obtained by
386 -- subtracting the old ending bit from storage_unit - 1.
388 Set_Component_Bit_Offset
389 (Comp,
390 (Storage_Unit_Offset * System_Storage_Unit) +
391 (System_Storage_Unit - 1) -
392 (Start_Bit + CSZ - 1));
394 Set_Normalized_First_Bit
395 (Comp,
396 Component_Bit_Offset (Comp) mod
397 System_Storage_Unit);
398 end if;
399 end;
400 end if;
402 Next_Component_Or_Discriminant (Comp);
403 end loop;
405 -- For Ada 2005, we do machine scalar processing, as fully described In
406 -- AI-133. This involves gathering all components which start at the
407 -- same byte offset and processing them together. Same approach is still
408 -- valid in later versions including Ada 2012.
410 else
411 declare
412 Max_Machine_Scalar_Size : constant Uint :=
413 UI_From_Int
414 (Standard_Long_Long_Integer_Size);
415 -- We use this as the maximum machine scalar size
417 Num_CC : Natural;
418 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
420 begin
421 -- This first loop through components does two things. First it
422 -- deals with the case of components with component clauses whose
423 -- length is greater than the maximum machine scalar size (either
424 -- accepting them or rejecting as needed). Second, it counts the
425 -- number of components with component clauses whose length does
426 -- not exceed this maximum for later processing.
428 Num_CC := 0;
429 Comp := First_Component_Or_Discriminant (R);
430 while Present (Comp) loop
431 CC := Component_Clause (Comp);
433 if Present (CC) then
434 declare
435 Fbit : constant Uint := Static_Integer (First_Bit (CC));
436 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
438 begin
439 -- Case of component with last bit >= max machine scalar
441 if Lbit >= Max_Machine_Scalar_Size then
443 -- This is allowed only if first bit is zero, and
444 -- last bit + 1 is a multiple of storage unit size.
446 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
448 -- This is the case to give a warning if enabled
450 if Warn_On_Reverse_Bit_Order then
451 Error_Msg_N
452 ("info: multi-byte field specified with "
453 & " non-standard Bit_Order?V?", CC);
455 if Bytes_Big_Endian then
456 Error_Msg_N
457 ("\bytes are not reversed "
458 & "(component is big-endian)?V?", CC);
459 else
460 Error_Msg_N
461 ("\bytes are not reversed "
462 & "(component is little-endian)?V?", CC);
463 end if;
464 end if;
466 -- Give error message for RM 13.5.1(10) violation
468 else
469 Error_Msg_FE
470 ("machine scalar rules not followed for&",
471 First_Bit (CC), Comp);
473 Error_Msg_Uint_1 := Lbit;
474 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
475 Error_Msg_F
476 ("\last bit (^) exceeds maximum machine "
477 & "scalar size (^)",
478 First_Bit (CC));
480 if (Lbit + 1) mod SSU /= 0 then
481 Error_Msg_Uint_1 := SSU;
482 Error_Msg_F
483 ("\and is not a multiple of Storage_Unit (^) "
484 & "(RM 13.4.1(10))",
485 First_Bit (CC));
487 else
488 Error_Msg_Uint_1 := Fbit;
489 Error_Msg_F
490 ("\and first bit (^) is non-zero "
491 & "(RM 13.4.1(10))",
492 First_Bit (CC));
493 end if;
494 end if;
496 -- OK case of machine scalar related component clause,
497 -- For now, just count them.
499 else
500 Num_CC := Num_CC + 1;
501 end if;
502 end;
503 end if;
505 Next_Component_Or_Discriminant (Comp);
506 end loop;
508 -- We need to sort the component clauses on the basis of the
509 -- Position values in the clause, so we can group clauses with
510 -- the same Position together to determine the relevant machine
511 -- scalar size.
513 Sort_CC : declare
514 Comps : array (0 .. Num_CC) of Entity_Id;
515 -- Array to collect component and discriminant entities. The
516 -- data starts at index 1, the 0'th entry is for the sort
517 -- routine.
519 function CP_Lt (Op1, Op2 : Natural) return Boolean;
520 -- Compare routine for Sort
522 procedure CP_Move (From : Natural; To : Natural);
523 -- Move routine for Sort
525 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
527 Start : Natural;
528 Stop : Natural;
529 -- Start and stop positions in the component list of the set of
530 -- components with the same starting position (that constitute
531 -- components in a single machine scalar).
533 MaxL : Uint;
534 -- Maximum last bit value of any component in this set
536 MSS : Uint;
537 -- Corresponding machine scalar size
539 -----------
540 -- CP_Lt --
541 -----------
543 function CP_Lt (Op1, Op2 : Natural) return Boolean is
544 begin
545 return Position (Component_Clause (Comps (Op1))) <
546 Position (Component_Clause (Comps (Op2)));
547 end CP_Lt;
549 -------------
550 -- CP_Move --
551 -------------
553 procedure CP_Move (From : Natural; To : Natural) is
554 begin
555 Comps (To) := Comps (From);
556 end CP_Move;
558 -- Start of processing for Sort_CC
560 begin
561 -- Collect the machine scalar relevant component clauses
563 Num_CC := 0;
564 Comp := First_Component_Or_Discriminant (R);
565 while Present (Comp) loop
566 declare
567 CC : constant Node_Id := Component_Clause (Comp);
569 begin
570 -- Collect only component clauses whose last bit is less
571 -- than machine scalar size. Any component clause whose
572 -- last bit exceeds this value does not take part in
573 -- machine scalar layout considerations. The test for
574 -- Error_Posted makes sure we exclude component clauses
575 -- for which we already posted an error.
577 if Present (CC)
578 and then not Error_Posted (Last_Bit (CC))
579 and then Static_Integer (Last_Bit (CC)) <
580 Max_Machine_Scalar_Size
581 then
582 Num_CC := Num_CC + 1;
583 Comps (Num_CC) := Comp;
584 end if;
585 end;
587 Next_Component_Or_Discriminant (Comp);
588 end loop;
590 -- Sort by ascending position number
592 Sorting.Sort (Num_CC);
594 -- We now have all the components whose size does not exceed
595 -- the max machine scalar value, sorted by starting position.
596 -- In this loop we gather groups of clauses starting at the
597 -- same position, to process them in accordance with AI-133.
599 Stop := 0;
600 while Stop < Num_CC loop
601 Start := Stop + 1;
602 Stop := Start;
603 MaxL :=
604 Static_Integer
605 (Last_Bit (Component_Clause (Comps (Start))));
606 while Stop < Num_CC loop
607 if Static_Integer
608 (Position (Component_Clause (Comps (Stop + 1)))) =
609 Static_Integer
610 (Position (Component_Clause (Comps (Stop))))
611 then
612 Stop := Stop + 1;
613 MaxL :=
614 UI_Max
615 (MaxL,
616 Static_Integer
617 (Last_Bit
618 (Component_Clause (Comps (Stop)))));
619 else
620 exit;
621 end if;
622 end loop;
624 -- Now we have a group of component clauses from Start to
625 -- Stop whose positions are identical, and MaxL is the
626 -- maximum last bit value of any of these components.
628 -- We need to determine the corresponding machine scalar
629 -- size. This loop assumes that machine scalar sizes are
630 -- even, and that each possible machine scalar has twice
631 -- as many bits as the next smaller one.
633 MSS := Max_Machine_Scalar_Size;
634 while MSS mod 2 = 0
635 and then (MSS / 2) >= SSU
636 and then (MSS / 2) > MaxL
637 loop
638 MSS := MSS / 2;
639 end loop;
641 -- Here is where we fix up the Component_Bit_Offset value
642 -- to account for the reverse bit order. Some examples of
643 -- what needs to be done for the case of a machine scalar
644 -- size of 8 are:
646 -- First_Bit .. Last_Bit Component_Bit_Offset
647 -- old new old new
649 -- 0 .. 0 7 .. 7 0 7
650 -- 0 .. 1 6 .. 7 0 6
651 -- 0 .. 2 5 .. 7 0 5
652 -- 0 .. 7 0 .. 7 0 4
654 -- 1 .. 1 6 .. 6 1 6
655 -- 1 .. 4 3 .. 6 1 3
656 -- 4 .. 7 0 .. 3 4 0
658 -- The rule is that the first bit is obtained by subtracting
659 -- the old ending bit from machine scalar size - 1.
661 for C in Start .. Stop loop
662 declare
663 Comp : constant Entity_Id := Comps (C);
664 CC : constant Node_Id := Component_Clause (Comp);
666 LB : constant Uint := Static_Integer (Last_Bit (CC));
667 NFB : constant Uint := MSS - Uint_1 - LB;
668 NLB : constant Uint := NFB + Esize (Comp) - 1;
669 Pos : constant Uint := Static_Integer (Position (CC));
671 begin
672 if Warn_On_Reverse_Bit_Order then
673 Error_Msg_Uint_1 := MSS;
674 Error_Msg_N
675 ("info: reverse bit order in machine " &
676 "scalar of length^?V?", First_Bit (CC));
677 Error_Msg_Uint_1 := NFB;
678 Error_Msg_Uint_2 := NLB;
680 if Bytes_Big_Endian then
681 Error_Msg_NE
682 ("\big-endian range for component "
683 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
684 else
685 Error_Msg_NE
686 ("\little-endian range for component"
687 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
688 end if;
689 end if;
691 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
692 Set_Normalized_First_Bit (Comp, NFB mod SSU);
693 end;
694 end loop;
695 end loop;
696 end Sort_CC;
697 end;
698 end if;
699 end Adjust_Record_For_Reverse_Bit_Order;
701 -------------------------------------
702 -- Alignment_Check_For_Size_Change --
703 -------------------------------------
705 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
706 begin
707 -- If the alignment is known, and not set by a rep clause, and is
708 -- inconsistent with the size being set, then reset it to unknown,
709 -- we assume in this case that the size overrides the inherited
710 -- alignment, and that the alignment must be recomputed.
712 if Known_Alignment (Typ)
713 and then not Has_Alignment_Clause (Typ)
714 and then Size mod (Alignment (Typ) * SSU) /= 0
715 then
716 Init_Alignment (Typ);
717 end if;
718 end Alignment_Check_For_Size_Change;
720 -------------------------------------
721 -- Analyze_Aspects_At_Freeze_Point --
722 -------------------------------------
724 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
725 ASN : Node_Id;
726 A_Id : Aspect_Id;
727 Ritem : Node_Id;
729 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
730 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
731 -- the aspect specification node ASN.
733 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
734 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
735 -- a derived type can inherit aspects from its parent which have been
736 -- specified at the time of the derivation using an aspect, as in:
738 -- type A is range 1 .. 10
739 -- with Size => Not_Defined_Yet;
740 -- ..
741 -- type B is new A;
742 -- ..
743 -- Not_Defined_Yet : constant := 64;
745 -- In this example, the Size of A is considered to be specified prior
746 -- to the derivation, and thus inherited, even though the value is not
747 -- known at the time of derivation. To deal with this, we use two entity
748 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
749 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
750 -- the derived type (B here). If this flag is set when the derived type
751 -- is frozen, then this procedure is called to ensure proper inheritance
752 -- of all delayed aspects from the parent type. The derived type is E,
753 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
754 -- aspect specification node in the Rep_Item chain for the parent type.
756 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
757 -- Given an aspect specification node ASN whose expression is an
758 -- optional Boolean, this routines creates the corresponding pragma
759 -- at the freezing point.
761 ----------------------------------
762 -- Analyze_Aspect_Default_Value --
763 ----------------------------------
765 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
766 Ent : constant Entity_Id := Entity (ASN);
767 Expr : constant Node_Id := Expression (ASN);
768 Id : constant Node_Id := Identifier (ASN);
770 begin
771 Error_Msg_Name_1 := Chars (Id);
773 if not Is_Type (Ent) then
774 Error_Msg_N ("aspect% can only apply to a type", Id);
775 return;
777 elsif not Is_First_Subtype (Ent) then
778 Error_Msg_N ("aspect% cannot apply to subtype", Id);
779 return;
781 elsif A_Id = Aspect_Default_Value
782 and then not Is_Scalar_Type (Ent)
783 then
784 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
785 return;
787 elsif A_Id = Aspect_Default_Component_Value then
788 if not Is_Array_Type (Ent) then
789 Error_Msg_N ("aspect% can only be applied to array type", Id);
790 return;
792 elsif not Is_Scalar_Type (Component_Type (Ent)) then
793 Error_Msg_N ("aspect% requires scalar components", Id);
794 return;
795 end if;
796 end if;
798 Set_Has_Default_Aspect (Base_Type (Ent));
800 if Is_Scalar_Type (Ent) then
801 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
802 else
803 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
804 end if;
805 end Analyze_Aspect_Default_Value;
807 ---------------------------------
808 -- Inherit_Delayed_Rep_Aspects --
809 ---------------------------------
811 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
812 P : constant Entity_Id := Entity (ASN);
813 -- Entithy for parent type
815 N : Node_Id;
816 -- Item from Rep_Item chain
818 A : Aspect_Id;
820 begin
821 -- Loop through delayed aspects for the parent type
823 N := ASN;
824 while Present (N) loop
825 if Nkind (N) = N_Aspect_Specification then
826 exit when Entity (N) /= P;
828 if Is_Delayed_Aspect (N) then
829 A := Get_Aspect_Id (Chars (Identifier (N)));
831 -- Process delayed rep aspect. For Boolean attributes it is
832 -- not possible to cancel an attribute once set (the attempt
833 -- to use an aspect with xxx => False is an error) for a
834 -- derived type. So for those cases, we do not have to check
835 -- if a clause has been given for the derived type, since it
836 -- is harmless to set it again if it is already set.
838 case A is
840 -- Alignment
842 when Aspect_Alignment =>
843 if not Has_Alignment_Clause (E) then
844 Set_Alignment (E, Alignment (P));
845 end if;
847 -- Atomic
849 when Aspect_Atomic =>
850 if Is_Atomic (P) then
851 Set_Is_Atomic (E);
852 end if;
854 -- Atomic_Components
856 when Aspect_Atomic_Components =>
857 if Has_Atomic_Components (P) then
858 Set_Has_Atomic_Components (Base_Type (E));
859 end if;
861 -- Bit_Order
863 when Aspect_Bit_Order =>
864 if Is_Record_Type (E)
865 and then No (Get_Attribute_Definition_Clause
866 (E, Attribute_Bit_Order))
867 and then Reverse_Bit_Order (P)
868 then
869 Set_Reverse_Bit_Order (Base_Type (E));
870 end if;
872 -- Component_Size
874 when Aspect_Component_Size =>
875 if Is_Array_Type (E)
876 and then not Has_Component_Size_Clause (E)
877 then
878 Set_Component_Size
879 (Base_Type (E), Component_Size (P));
880 end if;
882 -- Machine_Radix
884 when Aspect_Machine_Radix =>
885 if Is_Decimal_Fixed_Point_Type (E)
886 and then not Has_Machine_Radix_Clause (E)
887 then
888 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
889 end if;
891 -- Object_Size (also Size which also sets Object_Size)
893 when Aspect_Object_Size | Aspect_Size =>
894 if not Has_Size_Clause (E)
895 and then
896 No (Get_Attribute_Definition_Clause
897 (E, Attribute_Object_Size))
898 then
899 Set_Esize (E, Esize (P));
900 end if;
902 -- Pack
904 when Aspect_Pack =>
905 if not Is_Packed (E) then
906 Set_Is_Packed (Base_Type (E));
908 if Is_Bit_Packed_Array (P) then
909 Set_Is_Bit_Packed_Array (Base_Type (E));
910 Set_Packed_Array_Impl_Type
911 (E, Packed_Array_Impl_Type (P));
912 end if;
913 end if;
915 -- Scalar_Storage_Order
917 when Aspect_Scalar_Storage_Order =>
918 if (Is_Record_Type (E) or else Is_Array_Type (E))
919 and then No (Get_Attribute_Definition_Clause
920 (E, Attribute_Scalar_Storage_Order))
921 and then Reverse_Storage_Order (P)
922 then
923 Set_Reverse_Storage_Order (Base_Type (E));
925 -- Clear default SSO indications, since the aspect
926 -- overrides the default.
928 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
929 Set_SSO_Set_High_By_Default (Base_Type (E), False);
930 end if;
932 -- Small
934 when Aspect_Small =>
935 if Is_Fixed_Point_Type (E)
936 and then not Has_Small_Clause (E)
937 then
938 Set_Small_Value (E, Small_Value (P));
939 end if;
941 -- Storage_Size
943 when Aspect_Storage_Size =>
944 if (Is_Access_Type (E) or else Is_Task_Type (E))
945 and then not Has_Storage_Size_Clause (E)
946 then
947 Set_Storage_Size_Variable
948 (Base_Type (E), Storage_Size_Variable (P));
949 end if;
951 -- Value_Size
953 when Aspect_Value_Size =>
955 -- Value_Size is never inherited, it is either set by
956 -- default, or it is explicitly set for the derived
957 -- type. So nothing to do here.
959 null;
961 -- Volatile
963 when Aspect_Volatile =>
964 if Is_Volatile (P) then
965 Set_Is_Volatile (E);
966 end if;
968 -- Volatile_Components
970 when Aspect_Volatile_Components =>
971 if Has_Volatile_Components (P) then
972 Set_Has_Volatile_Components (Base_Type (E));
973 end if;
975 -- That should be all the Rep Aspects
977 when others =>
978 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
979 null;
981 end case;
982 end if;
983 end if;
985 N := Next_Rep_Item (N);
986 end loop;
987 end Inherit_Delayed_Rep_Aspects;
989 -------------------------------------
990 -- Make_Pragma_From_Boolean_Aspect --
991 -------------------------------------
993 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
994 Ident : constant Node_Id := Identifier (ASN);
995 A_Name : constant Name_Id := Chars (Ident);
996 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
997 Ent : constant Entity_Id := Entity (ASN);
998 Expr : constant Node_Id := Expression (ASN);
999 Loc : constant Source_Ptr := Sloc (ASN);
1001 Prag : Node_Id;
1003 procedure Check_False_Aspect_For_Derived_Type;
1004 -- This procedure checks for the case of a false aspect for a derived
1005 -- type, which improperly tries to cancel an aspect inherited from
1006 -- the parent.
1008 -----------------------------------------
1009 -- Check_False_Aspect_For_Derived_Type --
1010 -----------------------------------------
1012 procedure Check_False_Aspect_For_Derived_Type is
1013 Par : Node_Id;
1015 begin
1016 -- We are only checking derived types
1018 if not Is_Derived_Type (E) then
1019 return;
1020 end if;
1022 Par := Nearest_Ancestor (E);
1024 case A_Id is
1025 when Aspect_Atomic | Aspect_Shared =>
1026 if not Is_Atomic (Par) then
1027 return;
1028 end if;
1030 when Aspect_Atomic_Components =>
1031 if not Has_Atomic_Components (Par) then
1032 return;
1033 end if;
1035 when Aspect_Discard_Names =>
1036 if not Discard_Names (Par) then
1037 return;
1038 end if;
1040 when Aspect_Pack =>
1041 if not Is_Packed (Par) then
1042 return;
1043 end if;
1045 when Aspect_Unchecked_Union =>
1046 if not Is_Unchecked_Union (Par) then
1047 return;
1048 end if;
1050 when Aspect_Volatile =>
1051 if not Is_Volatile (Par) then
1052 return;
1053 end if;
1055 when Aspect_Volatile_Components =>
1056 if not Has_Volatile_Components (Par) then
1057 return;
1058 end if;
1060 when others =>
1061 return;
1062 end case;
1064 -- Fall through means we are canceling an inherited aspect
1066 Error_Msg_Name_1 := A_Name;
1067 Error_Msg_NE
1068 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1070 end Check_False_Aspect_For_Derived_Type;
1072 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1074 begin
1075 -- Note that we know Expr is present, because for a missing Expr
1076 -- argument, we knew it was True and did not need to delay the
1077 -- evaluation to the freeze point.
1079 if Is_False (Static_Boolean (Expr)) then
1080 Check_False_Aspect_For_Derived_Type;
1082 else
1083 Prag :=
1084 Make_Pragma (Loc,
1085 Pragma_Argument_Associations => New_List (
1086 Make_Pragma_Argument_Association (Sloc (Ident),
1087 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1089 Pragma_Identifier =>
1090 Make_Identifier (Sloc (Ident), Chars (Ident)));
1092 Set_From_Aspect_Specification (Prag, True);
1093 Set_Corresponding_Aspect (Prag, ASN);
1094 Set_Aspect_Rep_Item (ASN, Prag);
1095 Set_Is_Delayed_Aspect (Prag);
1096 Set_Parent (Prag, ASN);
1097 end if;
1098 end Make_Pragma_From_Boolean_Aspect;
1100 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1102 begin
1103 -- Must be visible in current scope
1105 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1106 return;
1107 end if;
1109 -- Look for aspect specification entries for this entity
1111 ASN := First_Rep_Item (E);
1112 while Present (ASN) loop
1113 if Nkind (ASN) = N_Aspect_Specification then
1114 exit when Entity (ASN) /= E;
1116 if Is_Delayed_Aspect (ASN) then
1117 A_Id := Get_Aspect_Id (ASN);
1119 case A_Id is
1121 -- For aspects whose expression is an optional Boolean, make
1122 -- the corresponding pragma at the freezing point.
1124 when Boolean_Aspects |
1125 Library_Unit_Aspects =>
1126 Make_Pragma_From_Boolean_Aspect (ASN);
1128 -- Special handling for aspects that don't correspond to
1129 -- pragmas/attributes.
1131 when Aspect_Default_Value |
1132 Aspect_Default_Component_Value =>
1133 Analyze_Aspect_Default_Value (ASN);
1135 -- Ditto for iterator aspects, because the corresponding
1136 -- attributes may not have been analyzed yet.
1138 when Aspect_Constant_Indexing |
1139 Aspect_Variable_Indexing |
1140 Aspect_Default_Iterator |
1141 Aspect_Iterator_Element =>
1142 Analyze (Expression (ASN));
1144 when Aspect_Iterable =>
1145 Validate_Iterable_Aspect (E, ASN);
1147 when others =>
1148 null;
1149 end case;
1151 Ritem := Aspect_Rep_Item (ASN);
1153 if Present (Ritem) then
1154 Analyze (Ritem);
1155 end if;
1156 end if;
1157 end if;
1159 Next_Rep_Item (ASN);
1160 end loop;
1162 -- This is where we inherit delayed rep aspects from our parent. Note
1163 -- that if we fell out of the above loop with ASN non-empty, it means
1164 -- we hit an aspect for an entity other than E, and it must be the
1165 -- type from which we were derived.
1167 if May_Inherit_Delayed_Rep_Aspects (E) then
1168 Inherit_Delayed_Rep_Aspects (ASN);
1169 end if;
1170 end Analyze_Aspects_At_Freeze_Point;
1172 -----------------------------------
1173 -- Analyze_Aspect_Specifications --
1174 -----------------------------------
1176 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1177 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1178 -- Establish linkages between an aspect and its corresponding
1179 -- pragma.
1181 procedure Insert_After_SPARK_Mode
1182 (Prag : Node_Id;
1183 Ins_Nod : Node_Id;
1184 Decls : List_Id);
1185 -- Subsidiary to the analysis of aspects Abstract_State, Initializes,
1186 -- Initial_Condition and Refined_State. Insert node Prag before node
1187 -- Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip SPARK_Mode.
1188 -- Decls is the associated declarative list where Prag is to reside.
1190 procedure Insert_Pragma (Prag : Node_Id);
1191 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1192 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1193 -- Insert pragma Prag such that it mimics the placement of a source
1194 -- pragma of the same kind.
1196 -- procedure Proc (Formal : ...) with Global => ...;
1198 -- procedure Proc (Formal : ...);
1199 -- pragma Global (...);
1201 --------------
1202 -- Decorate --
1203 --------------
1205 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1206 begin
1207 Set_Aspect_Rep_Item (Asp, Prag);
1208 Set_Corresponding_Aspect (Prag, Asp);
1209 Set_From_Aspect_Specification (Prag);
1210 Set_Parent (Prag, Asp);
1211 end Decorate;
1213 -----------------------------
1214 -- Insert_After_SPARK_Mode --
1215 -----------------------------
1217 procedure Insert_After_SPARK_Mode
1218 (Prag : Node_Id;
1219 Ins_Nod : Node_Id;
1220 Decls : List_Id)
1222 Decl : Node_Id := Ins_Nod;
1224 begin
1225 -- Skip SPARK_Mode
1227 if Present (Decl)
1228 and then Nkind (Decl) = N_Pragma
1229 and then Pragma_Name (Decl) = Name_SPARK_Mode
1230 then
1231 Decl := Next (Decl);
1232 end if;
1234 if Present (Decl) then
1235 Insert_Before (Decl, Prag);
1237 -- Aitem acts as the last declaration
1239 else
1240 Append_To (Decls, Prag);
1241 end if;
1242 end Insert_After_SPARK_Mode;
1244 -------------------
1245 -- Insert_Pragma --
1246 -------------------
1248 procedure Insert_Pragma (Prag : Node_Id) is
1249 Aux : Node_Id;
1250 Decl : Node_Id;
1252 begin
1253 -- When the context is a library unit, the pragma is added to the
1254 -- Pragmas_After list.
1256 if Nkind (Parent (N)) = N_Compilation_Unit then
1257 Aux := Aux_Decls_Node (Parent (N));
1259 if No (Pragmas_After (Aux)) then
1260 Set_Pragmas_After (Aux, New_List);
1261 end if;
1263 Prepend (Prag, Pragmas_After (Aux));
1265 -- Pragmas associated with subprogram bodies are inserted in the
1266 -- declarative part.
1268 elsif Nkind (N) = N_Subprogram_Body then
1269 if Present (Declarations (N)) then
1271 -- Skip other internally generated pragmas from aspects to find
1272 -- the proper insertion point. As a result the order of pragmas
1273 -- is the same as the order of aspects.
1275 -- As precondition pragmas generated from conjuncts in the
1276 -- precondition aspect are presented in reverse order to
1277 -- Insert_Pragma, insert them in the correct order here by not
1278 -- skipping previously inserted precondition pragmas when the
1279 -- current pragma is a precondition.
1281 Decl := First (Declarations (N));
1282 while Present (Decl) loop
1283 if Nkind (Decl) = N_Pragma
1284 and then From_Aspect_Specification (Decl)
1285 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1286 and then
1287 Get_Pragma_Id (Prag) = Pragma_Precondition)
1288 then
1289 Next (Decl);
1290 else
1291 exit;
1292 end if;
1293 end loop;
1295 if Present (Decl) then
1296 Insert_Before (Decl, Prag);
1297 else
1298 Append (Prag, Declarations (N));
1299 end if;
1300 else
1301 Set_Declarations (N, New_List (Prag));
1302 end if;
1304 -- Default
1306 else
1307 Insert_After (N, Prag);
1308 end if;
1309 end Insert_Pragma;
1311 -- Local variables
1313 Aspect : Node_Id;
1314 Aitem : Node_Id;
1315 Ent : Node_Id;
1317 L : constant List_Id := Aspect_Specifications (N);
1319 Ins_Node : Node_Id := N;
1320 -- Insert pragmas/attribute definition clause after this node when no
1321 -- delayed analysis is required.
1323 -- Start of processing for Analyze_Aspect_Specifications
1325 -- The general processing involves building an attribute definition
1326 -- clause or a pragma node that corresponds to the aspect. Then in order
1327 -- to delay the evaluation of this aspect to the freeze point, we attach
1328 -- the corresponding pragma/attribute definition clause to the aspect
1329 -- specification node, which is then placed in the Rep Item chain. In
1330 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1331 -- and we evaluate the rep item at the freeze point. When the aspect
1332 -- doesn't have a corresponding pragma/attribute definition clause, then
1333 -- its analysis is simply delayed at the freeze point.
1335 -- Some special cases don't require delay analysis, thus the aspect is
1336 -- analyzed right now.
1338 -- Note that there is a special handling for Pre, Post, Test_Case,
1339 -- Contract_Cases aspects. In these cases, we do not have to worry
1340 -- about delay issues, since the pragmas themselves deal with delay
1341 -- of visibility for the expression analysis. Thus, we just insert
1342 -- the pragma after the node N.
1344 begin
1345 pragma Assert (Present (L));
1347 -- Loop through aspects
1349 Aspect := First (L);
1350 Aspect_Loop : while Present (Aspect) loop
1351 Analyze_One_Aspect : declare
1352 Expr : constant Node_Id := Expression (Aspect);
1353 Id : constant Node_Id := Identifier (Aspect);
1354 Loc : constant Source_Ptr := Sloc (Aspect);
1355 Nam : constant Name_Id := Chars (Id);
1356 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1357 Anod : Node_Id;
1359 Delay_Required : Boolean;
1360 -- Set False if delay is not required
1362 Eloc : Source_Ptr := No_Location;
1363 -- Source location of expression, modified when we split PPC's. It
1364 -- is set below when Expr is present.
1366 procedure Analyze_Aspect_External_Or_Link_Name;
1367 -- Perform analysis of the External_Name or Link_Name aspects
1369 procedure Analyze_Aspect_Implicit_Dereference;
1370 -- Perform analysis of the Implicit_Dereference aspects
1372 procedure Make_Aitem_Pragma
1373 (Pragma_Argument_Associations : List_Id;
1374 Pragma_Name : Name_Id);
1375 -- This is a wrapper for Make_Pragma used for converting aspects
1376 -- to pragmas. It takes care of Sloc (set from Loc) and building
1377 -- the pragma identifier from the given name. In addition the
1378 -- flags Class_Present and Split_PPC are set from the aspect
1379 -- node, as well as Is_Ignored. This routine also sets the
1380 -- From_Aspect_Specification in the resulting pragma node to
1381 -- True, and sets Corresponding_Aspect to point to the aspect.
1382 -- The resulting pragma is assigned to Aitem.
1384 ------------------------------------------
1385 -- Analyze_Aspect_External_Or_Link_Name --
1386 ------------------------------------------
1388 procedure Analyze_Aspect_External_Or_Link_Name is
1389 begin
1390 -- Verify that there is an Import/Export aspect defined for the
1391 -- entity. The processing of that aspect in turn checks that
1392 -- there is a Convention aspect declared. The pragma is
1393 -- constructed when processing the Convention aspect.
1395 declare
1396 A : Node_Id;
1398 begin
1399 A := First (L);
1400 while Present (A) loop
1401 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1402 Name_Import);
1403 Next (A);
1404 end loop;
1406 if No (A) then
1407 Error_Msg_N
1408 ("missing Import/Export for Link/External name",
1409 Aspect);
1410 end if;
1411 end;
1412 end Analyze_Aspect_External_Or_Link_Name;
1414 -----------------------------------------
1415 -- Analyze_Aspect_Implicit_Dereference --
1416 -----------------------------------------
1418 procedure Analyze_Aspect_Implicit_Dereference is
1419 begin
1420 if not Is_Type (E) or else not Has_Discriminants (E) then
1421 Error_Msg_N
1422 ("aspect must apply to a type with discriminants", N);
1424 else
1425 declare
1426 Disc : Entity_Id;
1428 begin
1429 Disc := First_Discriminant (E);
1430 while Present (Disc) loop
1431 if Chars (Expr) = Chars (Disc)
1432 and then Ekind (Etype (Disc)) =
1433 E_Anonymous_Access_Type
1434 then
1435 Set_Has_Implicit_Dereference (E);
1436 Set_Has_Implicit_Dereference (Disc);
1437 return;
1438 end if;
1440 Next_Discriminant (Disc);
1441 end loop;
1443 -- Error if no proper access discriminant.
1445 Error_Msg_NE
1446 ("not an access discriminant of&", Expr, E);
1447 end;
1448 end if;
1449 end Analyze_Aspect_Implicit_Dereference;
1451 -----------------------
1452 -- Make_Aitem_Pragma --
1453 -----------------------
1455 procedure Make_Aitem_Pragma
1456 (Pragma_Argument_Associations : List_Id;
1457 Pragma_Name : Name_Id)
1459 Args : List_Id := Pragma_Argument_Associations;
1461 begin
1462 -- We should never get here if aspect was disabled
1464 pragma Assert (not Is_Disabled (Aspect));
1466 -- Certain aspects allow for an optional name or expression. Do
1467 -- not generate a pragma with empty argument association list.
1469 if No (Args) or else No (Expression (First (Args))) then
1470 Args := No_List;
1471 end if;
1473 -- Build the pragma
1475 Aitem :=
1476 Make_Pragma (Loc,
1477 Pragma_Argument_Associations => Args,
1478 Pragma_Identifier =>
1479 Make_Identifier (Sloc (Id), Pragma_Name),
1480 Class_Present => Class_Present (Aspect),
1481 Split_PPC => Split_PPC (Aspect));
1483 -- Set additional semantic fields
1485 if Is_Ignored (Aspect) then
1486 Set_Is_Ignored (Aitem);
1487 elsif Is_Checked (Aspect) then
1488 Set_Is_Checked (Aitem);
1489 end if;
1491 Set_Corresponding_Aspect (Aitem, Aspect);
1492 Set_From_Aspect_Specification (Aitem, True);
1493 end Make_Aitem_Pragma;
1495 -- Start of processing for Analyze_One_Aspect
1497 begin
1498 -- Skip aspect if already analyzed, to avoid looping in some cases
1500 if Analyzed (Aspect) then
1501 goto Continue;
1502 end if;
1504 -- Skip looking at aspect if it is totally disabled. Just mark it
1505 -- as such for later reference in the tree. This also sets the
1506 -- Is_Ignored and Is_Checked flags appropriately.
1508 Check_Applicable_Policy (Aspect);
1510 if Is_Disabled (Aspect) then
1511 goto Continue;
1512 end if;
1514 -- Set the source location of expression, used in the case of
1515 -- a failed precondition/postcondition or invariant. Note that
1516 -- the source location of the expression is not usually the best
1517 -- choice here. For example, it gets located on the last AND
1518 -- keyword in a chain of boolean expressiond AND'ed together.
1519 -- It is best to put the message on the first character of the
1520 -- assertion, which is the effect of the First_Node call here.
1522 if Present (Expr) then
1523 Eloc := Sloc (First_Node (Expr));
1524 end if;
1526 -- Check restriction No_Implementation_Aspect_Specifications
1528 if Implementation_Defined_Aspect (A_Id) then
1529 Check_Restriction
1530 (No_Implementation_Aspect_Specifications, Aspect);
1531 end if;
1533 -- Check restriction No_Specification_Of_Aspect
1535 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1537 -- Mark aspect analyzed (actual analysis is delayed till later)
1539 Set_Analyzed (Aspect);
1540 Set_Entity (Aspect, E);
1541 Ent := New_Occurrence_Of (E, Sloc (Id));
1543 -- Check for duplicate aspect. Note that the Comes_From_Source
1544 -- test allows duplicate Pre/Post's that we generate internally
1545 -- to escape being flagged here.
1547 if No_Duplicates_Allowed (A_Id) then
1548 Anod := First (L);
1549 while Anod /= Aspect loop
1550 if Comes_From_Source (Aspect)
1551 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1552 then
1553 Error_Msg_Name_1 := Nam;
1554 Error_Msg_Sloc := Sloc (Anod);
1556 -- Case of same aspect specified twice
1558 if Class_Present (Anod) = Class_Present (Aspect) then
1559 if not Class_Present (Anod) then
1560 Error_Msg_NE
1561 ("aspect% for & previously given#",
1562 Id, E);
1563 else
1564 Error_Msg_NE
1565 ("aspect `%''Class` for & previously given#",
1566 Id, E);
1567 end if;
1568 end if;
1569 end if;
1571 Next (Anod);
1572 end loop;
1573 end if;
1575 -- Check some general restrictions on language defined aspects
1577 if not Implementation_Defined_Aspect (A_Id) then
1578 Error_Msg_Name_1 := Nam;
1580 -- Not allowed for renaming declarations
1582 if Nkind (N) in N_Renaming_Declaration then
1583 Error_Msg_N
1584 ("aspect % not allowed for renaming declaration",
1585 Aspect);
1586 end if;
1588 -- Not allowed for formal type declarations
1590 if Nkind (N) = N_Formal_Type_Declaration then
1591 Error_Msg_N
1592 ("aspect % not allowed for formal type declaration",
1593 Aspect);
1594 end if;
1595 end if;
1597 -- Copy expression for later processing by the procedures
1598 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1600 Set_Entity (Id, New_Copy_Tree (Expr));
1602 -- Set Delay_Required as appropriate to aspect
1604 case Aspect_Delay (A_Id) is
1605 when Always_Delay =>
1606 Delay_Required := True;
1608 when Never_Delay =>
1609 Delay_Required := False;
1611 when Rep_Aspect =>
1613 -- If expression has the form of an integer literal, then
1614 -- do not delay, since we know the value cannot change.
1615 -- This optimization catches most rep clause cases.
1617 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1618 or else (A_Id in Boolean_Aspects and then No (Expr))
1619 then
1620 Delay_Required := False;
1621 else
1622 Delay_Required := True;
1623 Set_Has_Delayed_Rep_Aspects (E);
1624 end if;
1625 end case;
1627 -- Processing based on specific aspect
1629 case A_Id is
1631 -- No_Aspect should be impossible
1633 when No_Aspect =>
1634 raise Program_Error;
1636 -- Case 1: Aspects corresponding to attribute definition
1637 -- clauses.
1639 when Aspect_Address |
1640 Aspect_Alignment |
1641 Aspect_Bit_Order |
1642 Aspect_Component_Size |
1643 Aspect_Constant_Indexing |
1644 Aspect_Default_Iterator |
1645 Aspect_Dispatching_Domain |
1646 Aspect_External_Tag |
1647 Aspect_Input |
1648 Aspect_Iterable |
1649 Aspect_Iterator_Element |
1650 Aspect_Machine_Radix |
1651 Aspect_Object_Size |
1652 Aspect_Output |
1653 Aspect_Read |
1654 Aspect_Scalar_Storage_Order |
1655 Aspect_Size |
1656 Aspect_Small |
1657 Aspect_Simple_Storage_Pool |
1658 Aspect_Storage_Pool |
1659 Aspect_Stream_Size |
1660 Aspect_Value_Size |
1661 Aspect_Variable_Indexing |
1662 Aspect_Write =>
1664 -- Indexing aspects apply only to tagged type
1666 if (A_Id = Aspect_Constant_Indexing
1667 or else
1668 A_Id = Aspect_Variable_Indexing)
1669 and then not (Is_Type (E)
1670 and then Is_Tagged_Type (E))
1671 then
1672 Error_Msg_N
1673 ("indexing aspect can only apply to a tagged type",
1674 Aspect);
1675 goto Continue;
1676 end if;
1678 -- For the case of aspect Address, we don't consider that we
1679 -- know the entity is never set in the source, since it is
1680 -- is likely aliasing is occurring.
1682 -- Note: one might think that the analysis of the resulting
1683 -- attribute definition clause would take care of that, but
1684 -- that's not the case since it won't be from source.
1686 if A_Id = Aspect_Address then
1687 Set_Never_Set_In_Source (E, False);
1688 end if;
1690 -- Correctness of the profile of a stream operation is
1691 -- verified at the freeze point, but we must detect the
1692 -- illegal specification of this aspect for a subtype now,
1693 -- to prevent malformed rep_item chains.
1695 if (A_Id = Aspect_Input or else
1696 A_Id = Aspect_Output or else
1697 A_Id = Aspect_Read or else
1698 A_Id = Aspect_Write)
1699 and not Is_First_Subtype (E)
1700 then
1701 Error_Msg_N
1702 ("local name must be a first subtype", Aspect);
1703 goto Continue;
1704 end if;
1706 -- Construct the attribute definition clause
1708 Aitem :=
1709 Make_Attribute_Definition_Clause (Loc,
1710 Name => Ent,
1711 Chars => Chars (Id),
1712 Expression => Relocate_Node (Expr));
1714 -- If the address is specified, then we treat the entity as
1715 -- referenced, to avoid spurious warnings. This is analogous
1716 -- to what is done with an attribute definition clause, but
1717 -- here we don't want to generate a reference because this
1718 -- is the point of definition of the entity.
1720 if A_Id = Aspect_Address then
1721 Set_Referenced (E);
1722 end if;
1724 -- Case 2: Aspects corresponding to pragmas
1726 -- Case 2a: Aspects corresponding to pragmas with two
1727 -- arguments, where the first argument is a local name
1728 -- referring to the entity, and the second argument is the
1729 -- aspect definition expression.
1731 -- Linker_Section/Suppress/Unsuppress
1733 when Aspect_Linker_Section |
1734 Aspect_Suppress |
1735 Aspect_Unsuppress =>
1737 Make_Aitem_Pragma
1738 (Pragma_Argument_Associations => New_List (
1739 Make_Pragma_Argument_Association (Loc,
1740 Expression => New_Occurrence_Of (E, Loc)),
1741 Make_Pragma_Argument_Association (Sloc (Expr),
1742 Expression => Relocate_Node (Expr))),
1743 Pragma_Name => Chars (Id));
1745 -- Synchronization
1747 -- Corresponds to pragma Implemented, construct the pragma
1749 when Aspect_Synchronization =>
1750 Make_Aitem_Pragma
1751 (Pragma_Argument_Associations => New_List (
1752 Make_Pragma_Argument_Association (Loc,
1753 Expression => New_Occurrence_Of (E, Loc)),
1754 Make_Pragma_Argument_Association (Sloc (Expr),
1755 Expression => Relocate_Node (Expr))),
1756 Pragma_Name => Name_Implemented);
1758 -- Attach_Handler
1760 when Aspect_Attach_Handler =>
1761 Make_Aitem_Pragma
1762 (Pragma_Argument_Associations => New_List (
1763 Make_Pragma_Argument_Association (Sloc (Ent),
1764 Expression => Ent),
1765 Make_Pragma_Argument_Association (Sloc (Expr),
1766 Expression => Relocate_Node (Expr))),
1767 Pragma_Name => Name_Attach_Handler);
1769 -- We need to insert this pragma into the tree to get proper
1770 -- processing and to look valid from a placement viewpoint.
1772 Insert_Pragma (Aitem);
1773 goto Continue;
1775 -- Dynamic_Predicate, Predicate, Static_Predicate
1777 when Aspect_Dynamic_Predicate |
1778 Aspect_Predicate |
1779 Aspect_Static_Predicate =>
1781 -- These aspects apply only to subtypes
1783 if not Is_Type (E) then
1784 Error_Msg_N
1785 ("predicate can only be specified for a subtype",
1786 Aspect);
1787 goto Continue;
1789 elsif Is_Incomplete_Type (E) then
1790 Error_Msg_N
1791 ("predicate cannot apply to incomplete view", Aspect);
1792 goto Continue;
1793 end if;
1795 -- Construct the pragma (always a pragma Predicate, with
1796 -- flags recording whether it is static/dynamic). We also
1797 -- set flags recording this in the type itself.
1799 Make_Aitem_Pragma
1800 (Pragma_Argument_Associations => New_List (
1801 Make_Pragma_Argument_Association (Sloc (Ent),
1802 Expression => Ent),
1803 Make_Pragma_Argument_Association (Sloc (Expr),
1804 Expression => Relocate_Node (Expr))),
1805 Pragma_Name => Name_Predicate);
1807 -- Mark type has predicates, and remember what kind of
1808 -- aspect lead to this predicate (we need this to access
1809 -- the right set of check policies later on).
1811 Set_Has_Predicates (E);
1813 if A_Id = Aspect_Dynamic_Predicate then
1814 Set_Has_Dynamic_Predicate_Aspect (E);
1815 elsif A_Id = Aspect_Static_Predicate then
1816 Set_Has_Static_Predicate_Aspect (E);
1817 end if;
1819 -- If the type is private, indicate that its completion
1820 -- has a freeze node, because that is the one that will
1821 -- be visible at freeze time.
1823 if Is_Private_Type (E) and then Present (Full_View (E)) then
1824 Set_Has_Predicates (Full_View (E));
1826 if A_Id = Aspect_Dynamic_Predicate then
1827 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1828 elsif A_Id = Aspect_Static_Predicate then
1829 Set_Has_Static_Predicate_Aspect (Full_View (E));
1830 end if;
1832 Set_Has_Delayed_Aspects (Full_View (E));
1833 Ensure_Freeze_Node (Full_View (E));
1834 end if;
1836 -- Case 2b: Aspects corresponding to pragmas with two
1837 -- arguments, where the second argument is a local name
1838 -- referring to the entity, and the first argument is the
1839 -- aspect definition expression.
1841 -- Convention
1843 when Aspect_Convention =>
1845 -- The aspect may be part of the specification of an import
1846 -- or export pragma. Scan the aspect list to gather the
1847 -- other components, if any. The name of the generated
1848 -- pragma is one of Convention/Import/Export.
1850 declare
1851 Args : constant List_Id := New_List (
1852 Make_Pragma_Argument_Association (Sloc (Expr),
1853 Expression => Relocate_Node (Expr)),
1854 Make_Pragma_Argument_Association (Sloc (Ent),
1855 Expression => Ent));
1857 Imp_Exp_Seen : Boolean := False;
1858 -- Flag set when aspect Import or Export has been seen
1860 Imp_Seen : Boolean := False;
1861 -- Flag set when aspect Import has been seen
1863 Asp : Node_Id;
1864 Asp_Nam : Name_Id;
1865 Extern_Arg : Node_Id;
1866 Link_Arg : Node_Id;
1867 Prag_Nam : Name_Id;
1869 begin
1870 Extern_Arg := Empty;
1871 Link_Arg := Empty;
1872 Prag_Nam := Chars (Id);
1874 Asp := First (L);
1875 while Present (Asp) loop
1876 Asp_Nam := Chars (Identifier (Asp));
1878 -- Aspects Import and Export take precedence over
1879 -- aspect Convention. As a result the generated pragma
1880 -- must carry the proper interfacing aspect's name.
1882 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1883 if Imp_Exp_Seen then
1884 Error_Msg_N ("conflicting", Asp);
1885 else
1886 Imp_Exp_Seen := True;
1888 if Asp_Nam = Name_Import then
1889 Imp_Seen := True;
1890 end if;
1891 end if;
1893 Prag_Nam := Asp_Nam;
1895 -- Aspect External_Name adds an extra argument to the
1896 -- generated pragma.
1898 elsif Asp_Nam = Name_External_Name then
1899 Extern_Arg :=
1900 Make_Pragma_Argument_Association (Loc,
1901 Chars => Asp_Nam,
1902 Expression => Relocate_Node (Expression (Asp)));
1904 -- Aspect Link_Name adds an extra argument to the
1905 -- generated pragma.
1907 elsif Asp_Nam = Name_Link_Name then
1908 Link_Arg :=
1909 Make_Pragma_Argument_Association (Loc,
1910 Chars => Asp_Nam,
1911 Expression => Relocate_Node (Expression (Asp)));
1912 end if;
1914 Next (Asp);
1915 end loop;
1917 -- Assemble the full argument list
1919 if Present (Extern_Arg) then
1920 Append_To (Args, Extern_Arg);
1921 end if;
1923 if Present (Link_Arg) then
1924 Append_To (Args, Link_Arg);
1925 end if;
1927 Make_Aitem_Pragma
1928 (Pragma_Argument_Associations => Args,
1929 Pragma_Name => Prag_Nam);
1931 -- Store the generated pragma Import in the related
1932 -- subprogram.
1934 if Imp_Seen and then Is_Subprogram (E) then
1935 Set_Import_Pragma (E, Aitem);
1936 end if;
1937 end;
1939 -- CPU, Interrupt_Priority, Priority
1941 -- These three aspects can be specified for a subprogram spec
1942 -- or body, in which case we analyze the expression and export
1943 -- the value of the aspect.
1945 -- Previously, we generated an equivalent pragma for bodies
1946 -- (note that the specs cannot contain these pragmas). The
1947 -- pragma was inserted ahead of local declarations, rather than
1948 -- after the body. This leads to a certain duplication between
1949 -- the processing performed for the aspect and the pragma, but
1950 -- given the straightforward handling required it is simpler
1951 -- to duplicate than to translate the aspect in the spec into
1952 -- a pragma in the declarative part of the body.
1954 when Aspect_CPU |
1955 Aspect_Interrupt_Priority |
1956 Aspect_Priority =>
1958 if Nkind_In (N, N_Subprogram_Body,
1959 N_Subprogram_Declaration)
1960 then
1961 -- Analyze the aspect expression
1963 Analyze_And_Resolve (Expr, Standard_Integer);
1965 -- Interrupt_Priority aspect not allowed for main
1966 -- subprograms. ARM D.1 does not forbid this explicitly,
1967 -- but ARM J.15.11 (6/3) does not permit pragma
1968 -- Interrupt_Priority for subprograms.
1970 if A_Id = Aspect_Interrupt_Priority then
1971 Error_Msg_N
1972 ("Interrupt_Priority aspect cannot apply to "
1973 & "subprogram", Expr);
1975 -- The expression must be static
1977 elsif not Is_OK_Static_Expression (Expr) then
1978 Flag_Non_Static_Expr
1979 ("aspect requires static expression!", Expr);
1981 -- Check whether this is the main subprogram. Issue a
1982 -- warning only if it is obviously not a main program
1983 -- (when it has parameters or when the subprogram is
1984 -- within a package).
1986 elsif Present (Parameter_Specifications
1987 (Specification (N)))
1988 or else not Is_Compilation_Unit (Defining_Entity (N))
1989 then
1990 -- See ARM D.1 (14/3) and D.16 (12/3)
1992 Error_Msg_N
1993 ("aspect applied to subprogram other than the "
1994 & "main subprogram has no effect??", Expr);
1996 -- Otherwise check in range and export the value
1998 -- For the CPU aspect
2000 elsif A_Id = Aspect_CPU then
2001 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2003 -- Value is correct so we export the value to make
2004 -- it available at execution time.
2006 Set_Main_CPU
2007 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2009 else
2010 Error_Msg_N
2011 ("main subprogram CPU is out of range", Expr);
2012 end if;
2014 -- For the Priority aspect
2016 elsif A_Id = Aspect_Priority then
2017 if Is_In_Range (Expr, RTE (RE_Priority)) then
2019 -- Value is correct so we export the value to make
2020 -- it available at execution time.
2022 Set_Main_Priority
2023 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2025 -- Ignore pragma if Relaxed_RM_Semantics to support
2026 -- other targets/non GNAT compilers.
2028 elsif not Relaxed_RM_Semantics then
2029 Error_Msg_N
2030 ("main subprogram priority is out of range",
2031 Expr);
2032 end if;
2033 end if;
2035 -- Load an arbitrary entity from System.Tasking.Stages
2036 -- or System.Tasking.Restricted.Stages (depending on
2037 -- the supported profile) to make sure that one of these
2038 -- packages is implicitly with'ed, since we need to have
2039 -- the tasking run time active for the pragma Priority to
2040 -- have any effect. Previously we with'ed the package
2041 -- System.Tasking, but this package does not trigger the
2042 -- required initialization of the run-time library.
2044 declare
2045 Discard : Entity_Id;
2046 begin
2047 if Restricted_Profile then
2048 Discard := RTE (RE_Activate_Restricted_Tasks);
2049 else
2050 Discard := RTE (RE_Activate_Tasks);
2051 end if;
2052 end;
2054 -- Handling for these Aspects in subprograms is complete
2056 goto Continue;
2058 -- For tasks
2060 else
2061 -- Pass the aspect as an attribute
2063 Aitem :=
2064 Make_Attribute_Definition_Clause (Loc,
2065 Name => Ent,
2066 Chars => Chars (Id),
2067 Expression => Relocate_Node (Expr));
2068 end if;
2070 -- Warnings
2072 when Aspect_Warnings =>
2073 Make_Aitem_Pragma
2074 (Pragma_Argument_Associations => New_List (
2075 Make_Pragma_Argument_Association (Sloc (Expr),
2076 Expression => Relocate_Node (Expr)),
2077 Make_Pragma_Argument_Association (Loc,
2078 Expression => New_Occurrence_Of (E, Loc))),
2079 Pragma_Name => Chars (Id));
2081 -- Case 2c: Aspects corresponding to pragmas with three
2082 -- arguments.
2084 -- Invariant aspects have a first argument that references the
2085 -- entity, a second argument that is the expression and a third
2086 -- argument that is an appropriate message.
2088 -- Invariant, Type_Invariant
2090 when Aspect_Invariant |
2091 Aspect_Type_Invariant =>
2093 -- Analysis of the pragma will verify placement legality:
2094 -- an invariant must apply to a private type, or appear in
2095 -- the private part of a spec and apply to a completion.
2097 Make_Aitem_Pragma
2098 (Pragma_Argument_Associations => New_List (
2099 Make_Pragma_Argument_Association (Sloc (Ent),
2100 Expression => Ent),
2101 Make_Pragma_Argument_Association (Sloc (Expr),
2102 Expression => Relocate_Node (Expr))),
2103 Pragma_Name => Name_Invariant);
2105 -- Add message unless exception messages are suppressed
2107 if not Opt.Exception_Locations_Suppressed then
2108 Append_To (Pragma_Argument_Associations (Aitem),
2109 Make_Pragma_Argument_Association (Eloc,
2110 Chars => Name_Message,
2111 Expression =>
2112 Make_String_Literal (Eloc,
2113 Strval => "failed invariant from "
2114 & Build_Location_String (Eloc))));
2115 end if;
2117 -- For Invariant case, insert immediately after the entity
2118 -- declaration. We do not have to worry about delay issues
2119 -- since the pragma processing takes care of this.
2121 Delay_Required := False;
2123 -- Case 2d : Aspects that correspond to a pragma with one
2124 -- argument.
2126 -- Abstract_State
2128 -- Aspect Abstract_State introduces implicit declarations for
2129 -- all state abstraction entities it defines. To emulate this
2130 -- behavior, insert the pragma at the beginning of the visible
2131 -- declarations of the related package so that it is analyzed
2132 -- immediately.
2134 when Aspect_Abstract_State => Abstract_State : declare
2135 Context : Node_Id := N;
2136 Decl : Node_Id;
2137 Decls : List_Id;
2139 begin
2140 -- When aspect Abstract_State appears on a generic package,
2141 -- it is propageted to the package instance. The context in
2142 -- this case is the instance spec.
2144 if Nkind (Context) = N_Package_Instantiation then
2145 Context := Instance_Spec (Context);
2146 end if;
2148 if Nkind_In (Context, N_Generic_Package_Declaration,
2149 N_Package_Declaration)
2150 then
2151 Make_Aitem_Pragma
2152 (Pragma_Argument_Associations => New_List (
2153 Make_Pragma_Argument_Association (Loc,
2154 Expression => Relocate_Node (Expr))),
2155 Pragma_Name => Name_Abstract_State);
2156 Decorate (Aspect, Aitem);
2158 Decls := Visible_Declarations (Specification (Context));
2160 -- In general pragma Abstract_State must be at the top
2161 -- of the existing visible declarations to emulate its
2162 -- source counterpart. The only exception to this is a
2163 -- generic instance in which case the pragma must be
2164 -- inserted after the association renamings.
2166 if Present (Decls) then
2167 Decl := First (Decls);
2169 -- The visible declarations of a generic instance have
2170 -- the following structure:
2172 -- <renamings of generic formals>
2173 -- <renamings of internally-generated spec and body>
2174 -- <first source declaration>
2176 -- The pragma must be inserted before the first source
2177 -- declaration, skip the instance "header".
2179 if Is_Generic_Instance (Defining_Entity (Context)) then
2180 while Present (Decl)
2181 and then not Comes_From_Source (Decl)
2182 loop
2183 Decl := Next (Decl);
2184 end loop;
2185 end if;
2187 -- Pragma Abstract_State must be inserted after pragma
2188 -- SPARK_Mode in the tree. This ensures that any error
2189 -- messages dependent on SPARK_Mode will be properly
2190 -- enabled/suppressed.
2192 Insert_After_SPARK_Mode
2193 (Prag => Aitem,
2194 Ins_Nod => Decl,
2195 Decls => Decls);
2197 -- Otherwise the pragma forms a new declarative list
2199 else
2200 Set_Visible_Declarations
2201 (Specification (Context), New_List (Aitem));
2202 end if;
2204 else
2205 Error_Msg_NE
2206 ("aspect & must apply to a package declaration",
2207 Aspect, Id);
2208 end if;
2210 goto Continue;
2211 end Abstract_State;
2213 -- Aspect Default_Internal_Condition is never delayed because
2214 -- it is equivalent to a source pragma which appears after the
2215 -- related private type. To deal with forward references, the
2216 -- generated pragma is stored in the rep chain of the related
2217 -- private type as types do not carry contracts. The pragma is
2218 -- wrapped inside of a procedure at the freeze point of the
2219 -- private type's full view.
2221 when Aspect_Default_Initial_Condition =>
2222 Make_Aitem_Pragma
2223 (Pragma_Argument_Associations => New_List (
2224 Make_Pragma_Argument_Association (Loc,
2225 Expression => Relocate_Node (Expr))),
2226 Pragma_Name =>
2227 Name_Default_Initial_Condition);
2229 Decorate (Aspect, Aitem);
2230 Insert_Pragma (Aitem);
2231 goto Continue;
2233 -- Depends
2235 -- Aspect Depends is never delayed because it is equivalent to
2236 -- a source pragma which appears after the related subprogram.
2237 -- To deal with forward references, the generated pragma is
2238 -- stored in the contract of the related subprogram and later
2239 -- analyzed at the end of the declarative region. See routine
2240 -- Analyze_Depends_In_Decl_Part for details.
2242 when Aspect_Depends =>
2243 Make_Aitem_Pragma
2244 (Pragma_Argument_Associations => New_List (
2245 Make_Pragma_Argument_Association (Loc,
2246 Expression => Relocate_Node (Expr))),
2247 Pragma_Name => Name_Depends);
2249 Decorate (Aspect, Aitem);
2250 Insert_Pragma (Aitem);
2251 goto Continue;
2253 -- Global
2255 -- Aspect Global is never delayed because it is equivalent to
2256 -- a source pragma which appears after the related subprogram.
2257 -- To deal with forward references, the generated pragma is
2258 -- stored in the contract of the related subprogram and later
2259 -- analyzed at the end of the declarative region. See routine
2260 -- Analyze_Global_In_Decl_Part for details.
2262 when Aspect_Global =>
2263 Make_Aitem_Pragma
2264 (Pragma_Argument_Associations => New_List (
2265 Make_Pragma_Argument_Association (Loc,
2266 Expression => Relocate_Node (Expr))),
2267 Pragma_Name => Name_Global);
2269 Decorate (Aspect, Aitem);
2270 Insert_Pragma (Aitem);
2271 goto Continue;
2273 -- Initial_Condition
2275 -- Aspect Initial_Condition is never delayed because it is
2276 -- equivalent to a source pragma which appears after the
2277 -- related package. To deal with forward references, the
2278 -- generated pragma is stored in the contract of the related
2279 -- package and later analyzed at the end of the declarative
2280 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2281 -- for details.
2283 when Aspect_Initial_Condition => Initial_Condition : declare
2284 Context : Node_Id := N;
2285 Decls : List_Id;
2287 begin
2288 -- When aspect Initial_Condition appears on a generic
2289 -- package, it is propageted to the package instance. The
2290 -- context in this case is the instance spec.
2292 if Nkind (Context) = N_Package_Instantiation then
2293 Context := Instance_Spec (Context);
2294 end if;
2296 if Nkind_In (Context, N_Generic_Package_Declaration,
2297 N_Package_Declaration)
2298 then
2299 Decls := Visible_Declarations (Specification (Context));
2301 Make_Aitem_Pragma
2302 (Pragma_Argument_Associations => New_List (
2303 Make_Pragma_Argument_Association (Loc,
2304 Expression => Relocate_Node (Expr))),
2305 Pragma_Name =>
2306 Name_Initial_Condition);
2307 Decorate (Aspect, Aitem);
2309 if No (Decls) then
2310 Decls := New_List;
2311 Set_Visible_Declarations (Context, Decls);
2312 end if;
2314 -- When aspects Abstract_State, Initial_Condition and
2315 -- Initializes are out of order, ensure that pragma
2316 -- SPARK_Mode is always at the top of the declarative
2317 -- list to properly enable/suppress errors.
2319 Insert_After_SPARK_Mode
2320 (Prag => Aitem,
2321 Ins_Nod => First (Decls),
2322 Decls => Decls);
2324 else
2325 Error_Msg_NE
2326 ("aspect & must apply to a package declaration",
2327 Aspect, Id);
2328 end if;
2330 goto Continue;
2331 end Initial_Condition;
2333 -- Initializes
2335 -- Aspect Initializes is never delayed because it is equivalent
2336 -- to a source pragma appearing after the related package. To
2337 -- deal with forward references, the generated pragma is stored
2338 -- in the contract of the related package and later analyzed at
2339 -- the end of the declarative region. For details, see routine
2340 -- Analyze_Initializes_In_Decl_Part.
2342 when Aspect_Initializes => Initializes : declare
2343 Context : Node_Id := N;
2344 Decls : List_Id;
2346 begin
2347 -- When aspect Initializes appears on a generic package,
2348 -- it is propageted to the package instance. The context
2349 -- in this case is the instance spec.
2351 if Nkind (Context) = N_Package_Instantiation then
2352 Context := Instance_Spec (Context);
2353 end if;
2355 if Nkind_In (Context, N_Generic_Package_Declaration,
2356 N_Package_Declaration)
2357 then
2358 Decls := Visible_Declarations (Specification (Context));
2360 Make_Aitem_Pragma
2361 (Pragma_Argument_Associations => New_List (
2362 Make_Pragma_Argument_Association (Loc,
2363 Expression => Relocate_Node (Expr))),
2364 Pragma_Name => Name_Initializes);
2365 Decorate (Aspect, Aitem);
2367 if No (Decls) then
2368 Decls := New_List;
2369 Set_Visible_Declarations (Context, Decls);
2370 end if;
2372 -- When aspects Abstract_State, Initial_Condition and
2373 -- Initializes are out of order, ensure that pragma
2374 -- SPARK_Mode is always at the top of the declarative
2375 -- list to properly enable/suppress errors.
2377 Insert_After_SPARK_Mode
2378 (Prag => Aitem,
2379 Ins_Nod => First (Decls),
2380 Decls => Decls);
2382 else
2383 Error_Msg_NE
2384 ("aspect & must apply to a package declaration",
2385 Aspect, Id);
2386 end if;
2388 goto Continue;
2389 end Initializes;
2391 -- Obsolescent
2393 when Aspect_Obsolescent => declare
2394 Args : List_Id;
2396 begin
2397 if No (Expr) then
2398 Args := No_List;
2399 else
2400 Args := New_List (
2401 Make_Pragma_Argument_Association (Sloc (Expr),
2402 Expression => Relocate_Node (Expr)));
2403 end if;
2405 Make_Aitem_Pragma
2406 (Pragma_Argument_Associations => Args,
2407 Pragma_Name => Chars (Id));
2408 end;
2410 -- Part_Of
2412 when Aspect_Part_Of =>
2413 if Nkind_In (N, N_Object_Declaration,
2414 N_Package_Instantiation)
2415 then
2416 Make_Aitem_Pragma
2417 (Pragma_Argument_Associations => New_List (
2418 Make_Pragma_Argument_Association (Loc,
2419 Expression => Relocate_Node (Expr))),
2420 Pragma_Name => Name_Part_Of);
2422 else
2423 Error_Msg_NE
2424 ("aspect & must apply to a variable or package "
2425 & "instantiation", Aspect, Id);
2426 end if;
2428 -- SPARK_Mode
2430 when Aspect_SPARK_Mode => SPARK_Mode : declare
2431 Decls : List_Id;
2433 begin
2434 Make_Aitem_Pragma
2435 (Pragma_Argument_Associations => New_List (
2436 Make_Pragma_Argument_Association (Loc,
2437 Expression => Relocate_Node (Expr))),
2438 Pragma_Name => Name_SPARK_Mode);
2440 -- When the aspect appears on a package or a subprogram
2441 -- body, insert the generated pragma at the top of the body
2442 -- declarations to emulate the behavior of a source pragma.
2444 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2445 Decorate (Aspect, Aitem);
2447 Decls := Declarations (N);
2449 if No (Decls) then
2450 Decls := New_List;
2451 Set_Declarations (N, Decls);
2452 end if;
2454 Prepend_To (Decls, Aitem);
2455 goto Continue;
2457 -- When the aspect is associated with a [generic] package
2458 -- declaration, insert the generated pragma at the top of
2459 -- the visible declarations to emulate the behavior of a
2460 -- source pragma.
2462 elsif Nkind_In (N, N_Generic_Package_Declaration,
2463 N_Package_Declaration)
2464 then
2465 Decorate (Aspect, Aitem);
2467 Decls := Visible_Declarations (Specification (N));
2469 if No (Decls) then
2470 Decls := New_List;
2471 Set_Visible_Declarations (Specification (N), Decls);
2472 end if;
2474 Prepend_To (Decls, Aitem);
2475 goto Continue;
2476 end if;
2477 end SPARK_Mode;
2479 -- Refined_Depends
2481 -- Aspect Refined_Depends is never delayed because it is
2482 -- equivalent to a source pragma which appears in the
2483 -- declarations of the related subprogram body. To deal with
2484 -- forward references, the generated pragma is stored in the
2485 -- contract of the related subprogram body and later analyzed
2486 -- at the end of the declarative region. For details, see
2487 -- routine Analyze_Refined_Depends_In_Decl_Part.
2489 when Aspect_Refined_Depends =>
2490 Make_Aitem_Pragma
2491 (Pragma_Argument_Associations => New_List (
2492 Make_Pragma_Argument_Association (Loc,
2493 Expression => Relocate_Node (Expr))),
2494 Pragma_Name => Name_Refined_Depends);
2496 Decorate (Aspect, Aitem);
2497 Insert_Pragma (Aitem);
2498 goto Continue;
2500 -- Refined_Global
2502 -- Aspect Refined_Global is never delayed because it is
2503 -- equivalent to a source pragma which appears in the
2504 -- declarations of the related subprogram body. To deal with
2505 -- forward references, the generated pragma is stored in the
2506 -- contract of the related subprogram body and later analyzed
2507 -- at the end of the declarative region. For details, see
2508 -- routine Analyze_Refined_Global_In_Decl_Part.
2510 when Aspect_Refined_Global =>
2511 Make_Aitem_Pragma
2512 (Pragma_Argument_Associations => New_List (
2513 Make_Pragma_Argument_Association (Loc,
2514 Expression => Relocate_Node (Expr))),
2515 Pragma_Name => Name_Refined_Global);
2517 Decorate (Aspect, Aitem);
2518 Insert_Pragma (Aitem);
2519 goto Continue;
2521 -- Refined_Post
2523 when Aspect_Refined_Post =>
2524 Make_Aitem_Pragma
2525 (Pragma_Argument_Associations => New_List (
2526 Make_Pragma_Argument_Association (Loc,
2527 Expression => Relocate_Node (Expr))),
2528 Pragma_Name => Name_Refined_Post);
2530 -- Refined_State
2532 when Aspect_Refined_State => Refined_State : declare
2533 Decls : List_Id;
2535 begin
2536 -- The corresponding pragma for Refined_State is inserted in
2537 -- the declarations of the related package body. This action
2538 -- synchronizes both the source and from-aspect versions of
2539 -- the pragma.
2541 if Nkind (N) = N_Package_Body then
2542 Decls := Declarations (N);
2544 Make_Aitem_Pragma
2545 (Pragma_Argument_Associations => New_List (
2546 Make_Pragma_Argument_Association (Loc,
2547 Expression => Relocate_Node (Expr))),
2548 Pragma_Name => Name_Refined_State);
2549 Decorate (Aspect, Aitem);
2551 if No (Decls) then
2552 Decls := New_List;
2553 Set_Declarations (N, Decls);
2554 end if;
2556 -- Pragma Refined_State must be inserted after pragma
2557 -- SPARK_Mode in the tree. This ensures that any error
2558 -- messages dependent on SPARK_Mode will be properly
2559 -- enabled/suppressed.
2561 Insert_After_SPARK_Mode
2562 (Prag => Aitem,
2563 Ins_Nod => First (Decls),
2564 Decls => Decls);
2566 else
2567 Error_Msg_NE
2568 ("aspect & must apply to a package body", Aspect, Id);
2569 end if;
2571 goto Continue;
2572 end Refined_State;
2574 -- Relative_Deadline
2576 when Aspect_Relative_Deadline =>
2577 Make_Aitem_Pragma
2578 (Pragma_Argument_Associations => New_List (
2579 Make_Pragma_Argument_Association (Loc,
2580 Expression => Relocate_Node (Expr))),
2581 Pragma_Name => Name_Relative_Deadline);
2583 -- If the aspect applies to a task, the corresponding pragma
2584 -- must appear within its declarations, not after.
2586 if Nkind (N) = N_Task_Type_Declaration then
2587 declare
2588 Def : Node_Id;
2589 V : List_Id;
2591 begin
2592 if No (Task_Definition (N)) then
2593 Set_Task_Definition (N,
2594 Make_Task_Definition (Loc,
2595 Visible_Declarations => New_List,
2596 End_Label => Empty));
2597 end if;
2599 Def := Task_Definition (N);
2600 V := Visible_Declarations (Def);
2601 if not Is_Empty_List (V) then
2602 Insert_Before (First (V), Aitem);
2604 else
2605 Set_Visible_Declarations (Def, New_List (Aitem));
2606 end if;
2608 goto Continue;
2609 end;
2610 end if;
2612 -- Case 2e: Annotate aspect
2614 when Aspect_Annotate =>
2615 declare
2616 Args : List_Id;
2617 Pargs : List_Id;
2618 Arg : Node_Id;
2620 begin
2621 -- The argument can be a single identifier
2623 if Nkind (Expr) = N_Identifier then
2625 -- One level of parens is allowed
2627 if Paren_Count (Expr) > 1 then
2628 Error_Msg_F ("extra parentheses ignored", Expr);
2629 end if;
2631 Set_Paren_Count (Expr, 0);
2633 -- Add the single item to the list
2635 Args := New_List (Expr);
2637 -- Otherwise we must have an aggregate
2639 elsif Nkind (Expr) = N_Aggregate then
2641 -- Must be positional
2643 if Present (Component_Associations (Expr)) then
2644 Error_Msg_F
2645 ("purely positional aggregate required", Expr);
2646 goto Continue;
2647 end if;
2649 -- Must not be parenthesized
2651 if Paren_Count (Expr) /= 0 then
2652 Error_Msg_F ("extra parentheses ignored", Expr);
2653 end if;
2655 -- List of arguments is list of aggregate expressions
2657 Args := Expressions (Expr);
2659 -- Anything else is illegal
2661 else
2662 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2663 goto Continue;
2664 end if;
2666 -- Prepare pragma arguments
2668 Pargs := New_List;
2669 Arg := First (Args);
2670 while Present (Arg) loop
2671 Append_To (Pargs,
2672 Make_Pragma_Argument_Association (Sloc (Arg),
2673 Expression => Relocate_Node (Arg)));
2674 Next (Arg);
2675 end loop;
2677 Append_To (Pargs,
2678 Make_Pragma_Argument_Association (Sloc (Ent),
2679 Chars => Name_Entity,
2680 Expression => Ent));
2682 Make_Aitem_Pragma
2683 (Pragma_Argument_Associations => Pargs,
2684 Pragma_Name => Name_Annotate);
2685 end;
2687 -- Case 3 : Aspects that don't correspond to pragma/attribute
2688 -- definition clause.
2690 -- Case 3a: The aspects listed below don't correspond to
2691 -- pragmas/attributes but do require delayed analysis.
2693 -- Default_Value can only apply to a scalar type
2695 when Aspect_Default_Value =>
2696 if not Is_Scalar_Type (E) then
2697 Error_Msg_N
2698 ("aspect Default_Value must apply to a scalar_Type", N);
2699 end if;
2701 Aitem := Empty;
2703 -- Default_Component_Value can only apply to an array type
2704 -- with scalar components.
2706 when Aspect_Default_Component_Value =>
2707 if not (Is_Array_Type (E)
2708 and then Is_Scalar_Type (Component_Type (E)))
2709 then
2710 Error_Msg_N ("aspect Default_Component_Value can only "
2711 & "apply to an array of scalar components", N);
2712 end if;
2714 Aitem := Empty;
2716 -- Case 3b: The aspects listed below don't correspond to
2717 -- pragmas/attributes and don't need delayed analysis.
2719 -- Implicit_Dereference
2721 -- For Implicit_Dereference, External_Name and Link_Name, only
2722 -- the legality checks are done during the analysis, thus no
2723 -- delay is required.
2725 when Aspect_Implicit_Dereference =>
2726 Analyze_Aspect_Implicit_Dereference;
2727 goto Continue;
2729 -- External_Name, Link_Name
2731 when Aspect_External_Name |
2732 Aspect_Link_Name =>
2733 Analyze_Aspect_External_Or_Link_Name;
2734 goto Continue;
2736 -- Dimension
2738 when Aspect_Dimension =>
2739 Analyze_Aspect_Dimension (N, Id, Expr);
2740 goto Continue;
2742 -- Dimension_System
2744 when Aspect_Dimension_System =>
2745 Analyze_Aspect_Dimension_System (N, Id, Expr);
2746 goto Continue;
2748 -- Case 4: Aspects requiring special handling
2750 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2751 -- pragmas take care of the delay.
2753 -- Pre/Post
2755 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2756 -- with a first argument that is the expression, and a second
2757 -- argument that is an informative message if the test fails.
2758 -- This is inserted right after the declaration, to get the
2759 -- required pragma placement. The processing for the pragmas
2760 -- takes care of the required delay.
2762 when Pre_Post_Aspects => Pre_Post : declare
2763 Pname : Name_Id;
2765 begin
2766 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2767 Pname := Name_Precondition;
2768 else
2769 Pname := Name_Postcondition;
2770 end if;
2772 -- If the expressions is of the form A and then B, then
2773 -- we generate separate Pre/Post aspects for the separate
2774 -- clauses. Since we allow multiple pragmas, there is no
2775 -- problem in allowing multiple Pre/Post aspects internally.
2776 -- These should be treated in reverse order (B first and
2777 -- A second) since they are later inserted just after N in
2778 -- the order they are treated. This way, the pragma for A
2779 -- ends up preceding the pragma for B, which may have an
2780 -- importance for the error raised (either constraint error
2781 -- or precondition error).
2783 -- We do not do this for Pre'Class, since we have to put
2784 -- these conditions together in a complex OR expression.
2786 -- We do not do this in ASIS mode, as ASIS relies on the
2787 -- original node representing the complete expression, when
2788 -- retrieving it through the source aspect table.
2790 if not ASIS_Mode
2791 and then (Pname = Name_Postcondition
2792 or else not Class_Present (Aspect))
2793 then
2794 while Nkind (Expr) = N_And_Then loop
2795 Insert_After (Aspect,
2796 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2797 Identifier => Identifier (Aspect),
2798 Expression => Relocate_Node (Left_Opnd (Expr)),
2799 Class_Present => Class_Present (Aspect),
2800 Split_PPC => True));
2801 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2802 Eloc := Sloc (Expr);
2803 end loop;
2804 end if;
2806 -- Build the precondition/postcondition pragma
2808 -- Add note about why we do NOT need Copy_Tree here???
2810 Make_Aitem_Pragma
2811 (Pragma_Argument_Associations => New_List (
2812 Make_Pragma_Argument_Association (Eloc,
2813 Chars => Name_Check,
2814 Expression => Relocate_Node (Expr))),
2815 Pragma_Name => Pname);
2817 -- Add message unless exception messages are suppressed
2819 if not Opt.Exception_Locations_Suppressed then
2820 Append_To (Pragma_Argument_Associations (Aitem),
2821 Make_Pragma_Argument_Association (Eloc,
2822 Chars => Name_Message,
2823 Expression =>
2824 Make_String_Literal (Eloc,
2825 Strval => "failed "
2826 & Get_Name_String (Pname)
2827 & " from "
2828 & Build_Location_String (Eloc))));
2829 end if;
2831 Set_Is_Delayed_Aspect (Aspect);
2833 -- For Pre/Post cases, insert immediately after the entity
2834 -- declaration, since that is the required pragma placement.
2835 -- Note that for these aspects, we do not have to worry
2836 -- about delay issues, since the pragmas themselves deal
2837 -- with delay of visibility for the expression analysis.
2839 Insert_Pragma (Aitem);
2840 goto Continue;
2841 end Pre_Post;
2843 -- Test_Case
2845 when Aspect_Test_Case => Test_Case : declare
2846 Args : List_Id;
2847 Comp_Expr : Node_Id;
2848 Comp_Assn : Node_Id;
2849 New_Expr : Node_Id;
2851 begin
2852 Args := New_List;
2854 if Nkind (Parent (N)) = N_Compilation_Unit then
2855 Error_Msg_Name_1 := Nam;
2856 Error_Msg_N ("incorrect placement of aspect `%`", E);
2857 goto Continue;
2858 end if;
2860 if Nkind (Expr) /= N_Aggregate then
2861 Error_Msg_Name_1 := Nam;
2862 Error_Msg_NE
2863 ("wrong syntax for aspect `%` for &", Id, E);
2864 goto Continue;
2865 end if;
2867 -- Make pragma expressions refer to the original aspect
2868 -- expressions through the Original_Node link. This is used
2869 -- in semantic analysis for ASIS mode, so that the original
2870 -- expression also gets analyzed.
2872 Comp_Expr := First (Expressions (Expr));
2873 while Present (Comp_Expr) loop
2874 New_Expr := Relocate_Node (Comp_Expr);
2875 Set_Original_Node (New_Expr, Comp_Expr);
2876 Append_To (Args,
2877 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2878 Expression => New_Expr));
2879 Next (Comp_Expr);
2880 end loop;
2882 Comp_Assn := First (Component_Associations (Expr));
2883 while Present (Comp_Assn) loop
2884 if List_Length (Choices (Comp_Assn)) /= 1
2885 or else
2886 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2887 then
2888 Error_Msg_Name_1 := Nam;
2889 Error_Msg_NE
2890 ("wrong syntax for aspect `%` for &", Id, E);
2891 goto Continue;
2892 end if;
2894 New_Expr := Relocate_Node (Expression (Comp_Assn));
2895 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2896 Append_To (Args,
2897 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2898 Chars => Chars (First (Choices (Comp_Assn))),
2899 Expression => New_Expr));
2900 Next (Comp_Assn);
2901 end loop;
2903 -- Build the test-case pragma
2905 Make_Aitem_Pragma
2906 (Pragma_Argument_Associations => Args,
2907 Pragma_Name => Nam);
2908 end Test_Case;
2910 -- Contract_Cases
2912 when Aspect_Contract_Cases =>
2913 Make_Aitem_Pragma
2914 (Pragma_Argument_Associations => New_List (
2915 Make_Pragma_Argument_Association (Loc,
2916 Expression => Relocate_Node (Expr))),
2917 Pragma_Name => Nam);
2919 Decorate (Aspect, Aitem);
2920 Insert_Pragma (Aitem);
2921 goto Continue;
2923 -- Case 5: Special handling for aspects with an optional
2924 -- boolean argument.
2926 -- In the general case, the corresponding pragma cannot be
2927 -- generated yet because the evaluation of the boolean needs
2928 -- to be delayed till the freeze point.
2930 when Boolean_Aspects |
2931 Library_Unit_Aspects =>
2933 Set_Is_Boolean_Aspect (Aspect);
2935 -- Lock_Free aspect only apply to protected objects
2937 if A_Id = Aspect_Lock_Free then
2938 if Ekind (E) /= E_Protected_Type then
2939 Error_Msg_Name_1 := Nam;
2940 Error_Msg_N
2941 ("aspect % only applies to a protected object",
2942 Aspect);
2944 else
2945 -- Set the Uses_Lock_Free flag to True if there is no
2946 -- expression or if the expression is True. The
2947 -- evaluation of this aspect should be delayed to the
2948 -- freeze point (why???)
2950 if No (Expr) or else Is_True (Static_Boolean (Expr))
2951 then
2952 Set_Uses_Lock_Free (E);
2953 end if;
2955 Record_Rep_Item (E, Aspect);
2956 end if;
2958 goto Continue;
2960 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
2962 -- For the case of aspects Import and Export, we don't
2963 -- consider that we know the entity is never set in the
2964 -- source, since it is is likely modified outside the
2965 -- program.
2967 -- Note: one might think that the analysis of the
2968 -- resulting pragma would take care of that, but
2969 -- that's not the case since it won't be from source.
2971 if Ekind (E) = E_Variable then
2972 Set_Never_Set_In_Source (E, False);
2973 end if;
2975 -- In older versions of Ada the corresponding pragmas
2976 -- specified a Convention. In Ada 2012 the convention is
2977 -- specified as a separate aspect, and it is optional,
2978 -- given that it defaults to Convention_Ada. The code
2979 -- that verifed that there was a matching convention
2980 -- is now obsolete.
2982 -- Resolve the expression of an Import or Export here,
2983 -- and require it to be of type Boolean and static. This
2984 -- is not quite right, because in general this should be
2985 -- delayed, but that seems tricky for these, because
2986 -- normally Boolean aspects are replaced with pragmas at
2987 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
2988 -- but in the case of these aspects we can't generate
2989 -- a simple pragma with just the entity name. ???
2991 if not Present (Expr)
2992 or else Is_True (Static_Boolean (Expr))
2993 then
2994 if A_Id = Aspect_Import then
2995 Set_Is_Imported (E);
2997 -- An imported entity cannot have an explicit
2998 -- initialization.
3000 if Nkind (N) = N_Object_Declaration
3001 and then Present (Expression (N))
3002 then
3003 Error_Msg_N
3004 ("imported entities cannot be initialized "
3005 & "(RM B.1(24))", Expression (N));
3006 end if;
3008 elsif A_Id = Aspect_Export then
3009 Set_Is_Exported (E);
3010 end if;
3011 end if;
3013 goto Continue;
3014 end if;
3016 -- Library unit aspects require special handling in the case
3017 -- of a package declaration, the pragma needs to be inserted
3018 -- in the list of declarations for the associated package.
3019 -- There is no issue of visibility delay for these aspects.
3021 if A_Id in Library_Unit_Aspects
3022 and then
3023 Nkind_In (N, N_Package_Declaration,
3024 N_Generic_Package_Declaration)
3025 and then Nkind (Parent (N)) /= N_Compilation_Unit
3027 -- Aspect is legal on a local instantiation of a library-
3028 -- level generic unit.
3030 and then not Is_Generic_Instance (Defining_Entity (N))
3031 then
3032 Error_Msg_N
3033 ("incorrect context for library unit aspect&", Id);
3034 goto Continue;
3035 end if;
3037 -- External property aspects are Boolean by nature, but
3038 -- their pragmas must contain two arguments, the second
3039 -- being the optional Boolean expression.
3041 if A_Id = Aspect_Async_Readers or else
3042 A_Id = Aspect_Async_Writers or else
3043 A_Id = Aspect_Effective_Reads or else
3044 A_Id = Aspect_Effective_Writes
3045 then
3046 declare
3047 Args : List_Id;
3049 begin
3050 -- The first argument of the external property pragma
3051 -- is the related object.
3053 Args :=
3054 New_List (
3055 Make_Pragma_Argument_Association (Sloc (Ent),
3056 Expression => Ent));
3058 -- The second argument is the optional Boolean
3059 -- expression which must be propagated even if it
3060 -- evaluates to False as this has special semantic
3061 -- meaning.
3063 if Present (Expr) then
3064 Append_To (Args,
3065 Make_Pragma_Argument_Association (Loc,
3066 Expression => Relocate_Node (Expr)));
3067 end if;
3069 Make_Aitem_Pragma
3070 (Pragma_Argument_Associations => Args,
3071 Pragma_Name => Nam);
3072 end;
3074 -- Cases where we do not delay, includes all cases where the
3075 -- expression is missing other than the above cases.
3077 elsif not Delay_Required or else No (Expr) then
3078 Make_Aitem_Pragma
3079 (Pragma_Argument_Associations => New_List (
3080 Make_Pragma_Argument_Association (Sloc (Ent),
3081 Expression => Ent)),
3082 Pragma_Name => Chars (Id));
3083 Delay_Required := False;
3085 -- In general cases, the corresponding pragma/attribute
3086 -- definition clause will be inserted later at the freezing
3087 -- point, and we do not need to build it now.
3089 else
3090 Aitem := Empty;
3091 end if;
3093 -- Storage_Size
3095 -- This is special because for access types we need to generate
3096 -- an attribute definition clause. This also works for single
3097 -- task declarations, but it does not work for task type
3098 -- declarations, because we have the case where the expression
3099 -- references a discriminant of the task type. That can't use
3100 -- an attribute definition clause because we would not have
3101 -- visibility on the discriminant. For that case we must
3102 -- generate a pragma in the task definition.
3104 when Aspect_Storage_Size =>
3106 -- Task type case
3108 if Ekind (E) = E_Task_Type then
3109 declare
3110 Decl : constant Node_Id := Declaration_Node (E);
3112 begin
3113 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3115 -- If no task definition, create one
3117 if No (Task_Definition (Decl)) then
3118 Set_Task_Definition (Decl,
3119 Make_Task_Definition (Loc,
3120 Visible_Declarations => Empty_List,
3121 End_Label => Empty));
3122 end if;
3124 -- Create a pragma and put it at the start of the task
3125 -- definition for the task type declaration.
3127 Make_Aitem_Pragma
3128 (Pragma_Argument_Associations => New_List (
3129 Make_Pragma_Argument_Association (Loc,
3130 Expression => Relocate_Node (Expr))),
3131 Pragma_Name => Name_Storage_Size);
3133 Prepend
3134 (Aitem,
3135 Visible_Declarations (Task_Definition (Decl)));
3136 goto Continue;
3137 end;
3139 -- All other cases, generate attribute definition
3141 else
3142 Aitem :=
3143 Make_Attribute_Definition_Clause (Loc,
3144 Name => Ent,
3145 Chars => Chars (Id),
3146 Expression => Relocate_Node (Expr));
3147 end if;
3148 end case;
3150 -- Attach the corresponding pragma/attribute definition clause to
3151 -- the aspect specification node.
3153 if Present (Aitem) then
3154 Set_From_Aspect_Specification (Aitem);
3155 end if;
3157 -- In the context of a compilation unit, we directly put the
3158 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3159 -- node (no delay is required here) except for aspects on a
3160 -- subprogram body (see below) and a generic package, for which we
3161 -- need to introduce the pragma before building the generic copy
3162 -- (see sem_ch12), and for package instantiations, where the
3163 -- library unit pragmas are better handled early.
3165 if Nkind (Parent (N)) = N_Compilation_Unit
3166 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3167 then
3168 declare
3169 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3171 begin
3172 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3174 -- For a Boolean aspect, create the corresponding pragma if
3175 -- no expression or if the value is True.
3177 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3178 if Is_True (Static_Boolean (Expr)) then
3179 Make_Aitem_Pragma
3180 (Pragma_Argument_Associations => New_List (
3181 Make_Pragma_Argument_Association (Sloc (Ent),
3182 Expression => Ent)),
3183 Pragma_Name => Chars (Id));
3185 Set_From_Aspect_Specification (Aitem, True);
3186 Set_Corresponding_Aspect (Aitem, Aspect);
3188 else
3189 goto Continue;
3190 end if;
3191 end if;
3193 -- If the aspect is on a subprogram body (relevant aspect
3194 -- is Inline), add the pragma in front of the declarations.
3196 if Nkind (N) = N_Subprogram_Body then
3197 if No (Declarations (N)) then
3198 Set_Declarations (N, New_List);
3199 end if;
3201 Prepend (Aitem, Declarations (N));
3203 elsif Nkind (N) = N_Generic_Package_Declaration then
3204 if No (Visible_Declarations (Specification (N))) then
3205 Set_Visible_Declarations (Specification (N), New_List);
3206 end if;
3208 Prepend (Aitem,
3209 Visible_Declarations (Specification (N)));
3211 elsif Nkind (N) = N_Package_Instantiation then
3212 declare
3213 Spec : constant Node_Id :=
3214 Specification (Instance_Spec (N));
3215 begin
3216 if No (Visible_Declarations (Spec)) then
3217 Set_Visible_Declarations (Spec, New_List);
3218 end if;
3220 Prepend (Aitem, Visible_Declarations (Spec));
3221 end;
3223 else
3224 if No (Pragmas_After (Aux)) then
3225 Set_Pragmas_After (Aux, New_List);
3226 end if;
3228 Append (Aitem, Pragmas_After (Aux));
3229 end if;
3231 goto Continue;
3232 end;
3233 end if;
3235 -- The evaluation of the aspect is delayed to the freezing point.
3236 -- The pragma or attribute clause if there is one is then attached
3237 -- to the aspect specification which is put in the rep item list.
3239 if Delay_Required then
3240 if Present (Aitem) then
3241 Set_Is_Delayed_Aspect (Aitem);
3242 Set_Aspect_Rep_Item (Aspect, Aitem);
3243 Set_Parent (Aitem, Aspect);
3244 end if;
3246 Set_Is_Delayed_Aspect (Aspect);
3248 -- In the case of Default_Value, link the aspect to base type
3249 -- as well, even though it appears on a first subtype. This is
3250 -- mandated by the semantics of the aspect. Do not establish
3251 -- the link when processing the base type itself as this leads
3252 -- to a rep item circularity. Verify that we are dealing with
3253 -- a scalar type to prevent cascaded errors.
3255 if A_Id = Aspect_Default_Value
3256 and then Is_Scalar_Type (E)
3257 and then Base_Type (E) /= E
3258 then
3259 Set_Has_Delayed_Aspects (Base_Type (E));
3260 Record_Rep_Item (Base_Type (E), Aspect);
3261 end if;
3263 Set_Has_Delayed_Aspects (E);
3264 Record_Rep_Item (E, Aspect);
3266 -- When delay is not required and the context is a package or a
3267 -- subprogram body, insert the pragma in the body declarations.
3269 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3270 if No (Declarations (N)) then
3271 Set_Declarations (N, New_List);
3272 end if;
3274 -- The pragma is added before source declarations
3276 Prepend_To (Declarations (N), Aitem);
3278 -- When delay is not required and the context is not a compilation
3279 -- unit, we simply insert the pragma/attribute definition clause
3280 -- in sequence.
3282 else
3283 Insert_After (Ins_Node, Aitem);
3284 Ins_Node := Aitem;
3285 end if;
3286 end Analyze_One_Aspect;
3288 <<Continue>>
3289 Next (Aspect);
3290 end loop Aspect_Loop;
3292 if Has_Delayed_Aspects (E) then
3293 Ensure_Freeze_Node (E);
3294 end if;
3295 end Analyze_Aspect_Specifications;
3297 -----------------------
3298 -- Analyze_At_Clause --
3299 -----------------------
3301 -- An at clause is replaced by the corresponding Address attribute
3302 -- definition clause that is the preferred approach in Ada 95.
3304 procedure Analyze_At_Clause (N : Node_Id) is
3305 CS : constant Boolean := Comes_From_Source (N);
3307 begin
3308 -- This is an obsolescent feature
3310 Check_Restriction (No_Obsolescent_Features, N);
3312 if Warn_On_Obsolescent_Feature then
3313 Error_Msg_N
3314 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3315 Error_Msg_N
3316 ("\?j?use address attribute definition clause instead", N);
3317 end if;
3319 -- Rewrite as address clause
3321 Rewrite (N,
3322 Make_Attribute_Definition_Clause (Sloc (N),
3323 Name => Identifier (N),
3324 Chars => Name_Address,
3325 Expression => Expression (N)));
3327 -- We preserve Comes_From_Source, since logically the clause still comes
3328 -- from the source program even though it is changed in form.
3330 Set_Comes_From_Source (N, CS);
3332 -- Analyze rewritten clause
3334 Analyze_Attribute_Definition_Clause (N);
3335 end Analyze_At_Clause;
3337 -----------------------------------------
3338 -- Analyze_Attribute_Definition_Clause --
3339 -----------------------------------------
3341 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3342 Loc : constant Source_Ptr := Sloc (N);
3343 Nam : constant Node_Id := Name (N);
3344 Attr : constant Name_Id := Chars (N);
3345 Expr : constant Node_Id := Expression (N);
3346 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3348 Ent : Entity_Id;
3349 -- The entity of Nam after it is analyzed. In the case of an incomplete
3350 -- type, this is the underlying type.
3352 U_Ent : Entity_Id;
3353 -- The underlying entity to which the attribute applies. Generally this
3354 -- is the Underlying_Type of Ent, except in the case where the clause
3355 -- applies to full view of incomplete type or private type in which case
3356 -- U_Ent is just a copy of Ent.
3358 FOnly : Boolean := False;
3359 -- Reset to True for subtype specific attribute (Alignment, Size)
3360 -- and for stream attributes, i.e. those cases where in the call to
3361 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3362 -- are checked. Note that the case of stream attributes is not clear
3363 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3364 -- Storage_Size for derived task types, but that is also clearly
3365 -- unintentional.
3367 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3368 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3369 -- definition clauses.
3371 function Duplicate_Clause return Boolean;
3372 -- This routine checks if the aspect for U_Ent being given by attribute
3373 -- definition clause N is for an aspect that has already been specified,
3374 -- and if so gives an error message. If there is a duplicate, True is
3375 -- returned, otherwise if there is no error, False is returned.
3377 procedure Check_Indexing_Functions;
3378 -- Check that the function in Constant_Indexing or Variable_Indexing
3379 -- attribute has the proper type structure. If the name is overloaded,
3380 -- check that some interpretation is legal.
3382 procedure Check_Iterator_Functions;
3383 -- Check that there is a single function in Default_Iterator attribute
3384 -- has the proper type structure.
3386 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3387 -- Common legality check for the previous two
3389 -----------------------------------
3390 -- Analyze_Stream_TSS_Definition --
3391 -----------------------------------
3393 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3394 Subp : Entity_Id := Empty;
3395 I : Interp_Index;
3396 It : Interp;
3397 Pnam : Entity_Id;
3399 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3400 -- True for Read attribute, false for other attributes
3402 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3403 -- Return true if the entity is a subprogram with an appropriate
3404 -- profile for the attribute being defined.
3406 ----------------------
3407 -- Has_Good_Profile --
3408 ----------------------
3410 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3411 F : Entity_Id;
3412 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3413 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3414 (False => E_Procedure, True => E_Function);
3415 Typ : Entity_Id;
3417 begin
3418 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3419 return False;
3420 end if;
3422 F := First_Formal (Subp);
3424 if No (F)
3425 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3426 or else Designated_Type (Etype (F)) /=
3427 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3428 then
3429 return False;
3430 end if;
3432 if not Is_Function then
3433 Next_Formal (F);
3435 declare
3436 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3437 (False => E_In_Parameter,
3438 True => E_Out_Parameter);
3439 begin
3440 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3441 return False;
3442 end if;
3443 end;
3445 Typ := Etype (F);
3447 -- If the attribute specification comes from an aspect
3448 -- specification for a class-wide stream, the parameter must be
3449 -- a class-wide type of the entity to which the aspect applies.
3451 if From_Aspect_Specification (N)
3452 and then Class_Present (Parent (N))
3453 and then Is_Class_Wide_Type (Typ)
3454 then
3455 Typ := Etype (Typ);
3456 end if;
3458 else
3459 Typ := Etype (Subp);
3460 end if;
3462 -- Verify that the prefix of the attribute and the local name for
3463 -- the type of the formal match.
3465 if Base_Type (Typ) /= Base_Type (Ent)
3466 or else Present ((Next_Formal (F)))
3467 then
3468 return False;
3470 elsif not Is_Scalar_Type (Typ)
3471 and then not Is_First_Subtype (Typ)
3472 and then not Is_Class_Wide_Type (Typ)
3473 then
3474 return False;
3476 else
3477 return True;
3478 end if;
3479 end Has_Good_Profile;
3481 -- Start of processing for Analyze_Stream_TSS_Definition
3483 begin
3484 FOnly := True;
3486 if not Is_Type (U_Ent) then
3487 Error_Msg_N ("local name must be a subtype", Nam);
3488 return;
3490 elsif not Is_First_Subtype (U_Ent) then
3491 Error_Msg_N ("local name must be a first subtype", Nam);
3492 return;
3493 end if;
3495 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3497 -- If Pnam is present, it can be either inherited from an ancestor
3498 -- type (in which case it is legal to redefine it for this type), or
3499 -- be a previous definition of the attribute for the same type (in
3500 -- which case it is illegal).
3502 -- In the first case, it will have been analyzed already, and we
3503 -- can check that its profile does not match the expected profile
3504 -- for a stream attribute of U_Ent. In the second case, either Pnam
3505 -- has been analyzed (and has the expected profile), or it has not
3506 -- been analyzed yet (case of a type that has not been frozen yet
3507 -- and for which the stream attribute has been set using Set_TSS).
3509 if Present (Pnam)
3510 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3511 then
3512 Error_Msg_Sloc := Sloc (Pnam);
3513 Error_Msg_Name_1 := Attr;
3514 Error_Msg_N ("% attribute already defined #", Nam);
3515 return;
3516 end if;
3518 Analyze (Expr);
3520 if Is_Entity_Name (Expr) then
3521 if not Is_Overloaded (Expr) then
3522 if Has_Good_Profile (Entity (Expr)) then
3523 Subp := Entity (Expr);
3524 end if;
3526 else
3527 Get_First_Interp (Expr, I, It);
3528 while Present (It.Nam) loop
3529 if Has_Good_Profile (It.Nam) then
3530 Subp := It.Nam;
3531 exit;
3532 end if;
3534 Get_Next_Interp (I, It);
3535 end loop;
3536 end if;
3537 end if;
3539 if Present (Subp) then
3540 if Is_Abstract_Subprogram (Subp) then
3541 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3542 return;
3544 -- Test for stream subprogram for interface type being non-null
3546 elsif Is_Interface (U_Ent)
3547 and then not Inside_A_Generic
3548 and then Ekind (Subp) = E_Procedure
3549 and then
3550 not Null_Present
3551 (Specification
3552 (Unit_Declaration_Node (Ultimate_Alias (Subp))))
3553 then
3554 Error_Msg_N
3555 ("stream subprogram for interface type "
3556 & "must be null procedure", Expr);
3557 end if;
3559 Set_Entity (Expr, Subp);
3560 Set_Etype (Expr, Etype (Subp));
3562 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3564 else
3565 Error_Msg_Name_1 := Attr;
3566 Error_Msg_N ("incorrect expression for% attribute", Expr);
3567 end if;
3568 end Analyze_Stream_TSS_Definition;
3570 ------------------------------
3571 -- Check_Indexing_Functions --
3572 ------------------------------
3574 procedure Check_Indexing_Functions is
3575 Indexing_Found : Boolean := False;
3577 procedure Check_One_Function (Subp : Entity_Id);
3578 -- Check one possible interpretation. Sets Indexing_Found True if a
3579 -- legal indexing function is found.
3581 procedure Illegal_Indexing (Msg : String);
3582 -- Diagnose illegal indexing function if not overloaded. In the
3583 -- overloaded case indicate that no legal interpretation exists.
3585 ------------------------
3586 -- Check_One_Function --
3587 ------------------------
3589 procedure Check_One_Function (Subp : Entity_Id) is
3590 Default_Element : Node_Id;
3591 Ret_Type : constant Entity_Id := Etype (Subp);
3593 begin
3594 if not Is_Overloadable (Subp) then
3595 Illegal_Indexing ("illegal indexing function for type&");
3596 return;
3598 elsif Scope (Subp) /= Scope (Ent) then
3599 if Nkind (Expr) = N_Expanded_Name then
3601 -- Indexing function can't be declared elsewhere
3603 Illegal_Indexing
3604 ("indexing function must be declared in scope of type&");
3605 end if;
3607 return;
3609 elsif No (First_Formal (Subp)) then
3610 Illegal_Indexing
3611 ("Indexing requires a function that applies to type&");
3612 return;
3614 elsif No (Next_Formal (First_Formal (Subp))) then
3615 Illegal_Indexing
3616 ("indexing function must have at least two parameters");
3617 return;
3619 elsif Is_Derived_Type (Ent) then
3620 if (Attr = Name_Constant_Indexing
3621 and then Present
3622 (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing)))
3623 or else
3624 (Attr = Name_Variable_Indexing
3625 and then Present
3626 (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing)))
3627 then
3628 if Debug_Flag_Dot_XX then
3629 null;
3631 else
3632 Illegal_Indexing
3633 ("indexing function already inherited "
3634 & "from parent type");
3635 return;
3636 end if;
3637 end if;
3638 end if;
3640 if not Check_Primitive_Function (Subp)
3641 then
3642 Illegal_Indexing
3643 ("Indexing aspect requires a function that applies to type&");
3644 return;
3645 end if;
3647 -- If partial declaration exists, verify that it is not tagged.
3649 if Ekind (Current_Scope) = E_Package
3650 and then Has_Private_Declaration (Ent)
3651 and then From_Aspect_Specification (N)
3652 and then
3653 List_Containing (Parent (Ent)) =
3654 Private_Declarations
3655 (Specification (Unit_Declaration_Node (Current_Scope)))
3656 and then Nkind (N) = N_Attribute_Definition_Clause
3657 then
3658 declare
3659 Decl : Node_Id;
3661 begin
3662 Decl :=
3663 First (Visible_Declarations
3664 (Specification
3665 (Unit_Declaration_Node (Current_Scope))));
3667 while Present (Decl) loop
3668 if Nkind (Decl) = N_Private_Type_Declaration
3669 and then Ent = Full_View (Defining_Identifier (Decl))
3670 and then Tagged_Present (Decl)
3671 and then No (Aspect_Specifications (Decl))
3672 then
3673 Illegal_Indexing
3674 ("Indexing aspect cannot be specified on full view "
3675 & "if partial view is tagged");
3676 return;
3677 end if;
3679 Next (Decl);
3680 end loop;
3681 end;
3682 end if;
3684 -- An indexing function must return either the default element of
3685 -- the container, or a reference type. For variable indexing it
3686 -- must be the latter.
3688 Default_Element :=
3689 Find_Value_Of_Aspect
3690 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3692 if Present (Default_Element) then
3693 Analyze (Default_Element);
3695 if Is_Entity_Name (Default_Element)
3696 and then not Covers (Entity (Default_Element), Ret_Type)
3697 and then False
3698 then
3699 Illegal_Indexing
3700 ("wrong return type for indexing function");
3701 return;
3702 end if;
3703 end if;
3705 -- For variable_indexing the return type must be a reference type
3707 if Attr = Name_Variable_Indexing then
3708 if not Has_Implicit_Dereference (Ret_Type) then
3709 Illegal_Indexing
3710 ("variable indexing must return a reference type");
3711 return;
3713 elsif Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3714 then
3715 Illegal_Indexing
3716 ("variable indexing must return an access to variable");
3717 return;
3718 end if;
3720 else
3721 if Has_Implicit_Dereference (Ret_Type)
3722 and then not
3723 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3724 then
3725 Illegal_Indexing
3726 ("constant indexing must return an access to constant");
3727 return;
3729 elsif Is_Access_Type (Etype (First_Formal (Subp)))
3730 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
3731 then
3732 Illegal_Indexing
3733 ("constant indexing must apply to an access to constant");
3734 return;
3735 end if;
3736 end if;
3738 -- All checks succeeded.
3740 Indexing_Found := True;
3741 end Check_One_Function;
3743 -----------------------
3744 -- Illegal_Indexing --
3745 -----------------------
3747 procedure Illegal_Indexing (Msg : String) is
3748 begin
3749 Error_Msg_NE (Msg, N, Ent);
3750 end Illegal_Indexing;
3752 -- Start of processing for Check_Indexing_Functions
3754 begin
3755 if In_Instance then
3756 return;
3757 end if;
3759 Analyze (Expr);
3761 if not Is_Overloaded (Expr) then
3762 Check_One_Function (Entity (Expr));
3764 else
3765 declare
3766 I : Interp_Index;
3767 It : Interp;
3769 begin
3770 Indexing_Found := False;
3771 Get_First_Interp (Expr, I, It);
3772 while Present (It.Nam) loop
3774 -- Note that analysis will have added the interpretation
3775 -- that corresponds to the dereference. We only check the
3776 -- subprogram itself.
3778 if Is_Overloadable (It.Nam) then
3779 Check_One_Function (It.Nam);
3780 end if;
3782 Get_Next_Interp (I, It);
3783 end loop;
3784 end;
3785 end if;
3787 if not Indexing_Found and then not Error_Posted (N) then
3788 Error_Msg_NE
3789 ("aspect Indexing requires a local function that "
3790 & "applies to type&", Expr, Ent);
3791 end if;
3792 end Check_Indexing_Functions;
3794 ------------------------------
3795 -- Check_Iterator_Functions --
3796 ------------------------------
3798 procedure Check_Iterator_Functions is
3799 Default : Entity_Id;
3801 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3802 -- Check one possible interpretation for validity
3804 ----------------------------
3805 -- Valid_Default_Iterator --
3806 ----------------------------
3808 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3809 Formal : Entity_Id;
3811 begin
3812 if not Check_Primitive_Function (Subp) then
3813 return False;
3814 else
3815 Formal := First_Formal (Subp);
3816 end if;
3818 -- False if any subsequent formal has no default expression
3820 Formal := Next_Formal (Formal);
3821 while Present (Formal) loop
3822 if No (Expression (Parent (Formal))) then
3823 return False;
3824 end if;
3826 Next_Formal (Formal);
3827 end loop;
3829 -- True if all subsequent formals have default expressions
3831 return True;
3832 end Valid_Default_Iterator;
3834 -- Start of processing for Check_Iterator_Functions
3836 begin
3837 Analyze (Expr);
3839 if not Is_Entity_Name (Expr) then
3840 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3841 end if;
3843 if not Is_Overloaded (Expr) then
3844 if not Check_Primitive_Function (Entity (Expr)) then
3845 Error_Msg_NE
3846 ("aspect Indexing requires a function that applies to type&",
3847 Entity (Expr), Ent);
3848 end if;
3850 if not Valid_Default_Iterator (Entity (Expr)) then
3851 Error_Msg_N ("improper function for default iterator", Expr);
3852 end if;
3854 else
3855 Default := Empty;
3856 declare
3857 I : Interp_Index;
3858 It : Interp;
3860 begin
3861 Get_First_Interp (Expr, I, It);
3862 while Present (It.Nam) loop
3863 if not Check_Primitive_Function (It.Nam)
3864 or else not Valid_Default_Iterator (It.Nam)
3865 then
3866 Remove_Interp (I);
3868 elsif Present (Default) then
3869 Error_Msg_N ("default iterator must be unique", Expr);
3871 else
3872 Default := It.Nam;
3873 end if;
3875 Get_Next_Interp (I, It);
3876 end loop;
3877 end;
3879 if Present (Default) then
3880 Set_Entity (Expr, Default);
3881 Set_Is_Overloaded (Expr, False);
3882 end if;
3883 end if;
3884 end Check_Iterator_Functions;
3886 -------------------------------
3887 -- Check_Primitive_Function --
3888 -------------------------------
3890 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3891 Ctrl : Entity_Id;
3893 begin
3894 if Ekind (Subp) /= E_Function then
3895 return False;
3896 end if;
3898 if No (First_Formal (Subp)) then
3899 return False;
3900 else
3901 Ctrl := Etype (First_Formal (Subp));
3902 end if;
3904 if Ctrl = Ent
3905 or else Ctrl = Class_Wide_Type (Ent)
3906 or else
3907 (Ekind (Ctrl) = E_Anonymous_Access_Type
3908 and then (Designated_Type (Ctrl) = Ent
3909 or else
3910 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
3911 then
3912 null;
3913 else
3914 return False;
3915 end if;
3917 return True;
3918 end Check_Primitive_Function;
3920 ----------------------
3921 -- Duplicate_Clause --
3922 ----------------------
3924 function Duplicate_Clause return Boolean is
3925 A : Node_Id;
3927 begin
3928 -- Nothing to do if this attribute definition clause comes from
3929 -- an aspect specification, since we could not be duplicating an
3930 -- explicit clause, and we dealt with the case of duplicated aspects
3931 -- in Analyze_Aspect_Specifications.
3933 if From_Aspect_Specification (N) then
3934 return False;
3935 end if;
3937 -- Otherwise current clause may duplicate previous clause, or a
3938 -- previously given pragma or aspect specification for the same
3939 -- aspect.
3941 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
3943 if Present (A) then
3944 Error_Msg_Name_1 := Chars (N);
3945 Error_Msg_Sloc := Sloc (A);
3947 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
3948 return True;
3949 end if;
3951 return False;
3952 end Duplicate_Clause;
3954 -- Start of processing for Analyze_Attribute_Definition_Clause
3956 begin
3957 -- The following code is a defense against recursion. Not clear that
3958 -- this can happen legitimately, but perhaps some error situations can
3959 -- cause it, and we did see this recursion during testing.
3961 if Analyzed (N) then
3962 return;
3963 else
3964 Set_Analyzed (N, True);
3965 end if;
3967 -- Ignore some selected attributes in CodePeer mode since they are not
3968 -- relevant in this context.
3970 if CodePeer_Mode then
3971 case Id is
3973 -- Ignore Component_Size in CodePeer mode, to avoid changing the
3974 -- internal representation of types by implicitly packing them.
3976 when Attribute_Component_Size =>
3977 Rewrite (N, Make_Null_Statement (Sloc (N)));
3978 return;
3980 when others =>
3981 null;
3982 end case;
3983 end if;
3985 -- Process Ignore_Rep_Clauses option
3987 if Ignore_Rep_Clauses then
3988 case Id is
3990 -- The following should be ignored. They do not affect legality
3991 -- and may be target dependent. The basic idea of -gnatI is to
3992 -- ignore any rep clauses that may be target dependent but do not
3993 -- affect legality (except possibly to be rejected because they
3994 -- are incompatible with the compilation target).
3996 when Attribute_Alignment |
3997 Attribute_Bit_Order |
3998 Attribute_Component_Size |
3999 Attribute_Machine_Radix |
4000 Attribute_Object_Size |
4001 Attribute_Size |
4002 Attribute_Small |
4003 Attribute_Stream_Size |
4004 Attribute_Value_Size =>
4005 Kill_Rep_Clause (N);
4006 return;
4008 -- The following should not be ignored, because in the first place
4009 -- they are reasonably portable, and should not cause problems
4010 -- in compiling code from another target, and also they do affect
4011 -- legality, e.g. failing to provide a stream attribute for a type
4012 -- may make a program illegal.
4014 when Attribute_External_Tag |
4015 Attribute_Input |
4016 Attribute_Output |
4017 Attribute_Read |
4018 Attribute_Simple_Storage_Pool |
4019 Attribute_Storage_Pool |
4020 Attribute_Storage_Size |
4021 Attribute_Write =>
4022 null;
4024 -- We do not do anything here with address clauses, they will be
4025 -- removed by Freeze later on, but for now, it works better to
4026 -- keep then in the tree.
4028 when Attribute_Address =>
4029 null;
4031 -- Other cases are errors ("attribute& cannot be set with
4032 -- definition clause"), which will be caught below.
4034 when others =>
4035 null;
4036 end case;
4037 end if;
4039 Analyze (Nam);
4040 Ent := Entity (Nam);
4042 if Rep_Item_Too_Early (Ent, N) then
4043 return;
4044 end if;
4046 -- Rep clause applies to full view of incomplete type or private type if
4047 -- we have one (if not, this is a premature use of the type). However,
4048 -- certain semantic checks need to be done on the specified entity (i.e.
4049 -- the private view), so we save it in Ent.
4051 if Is_Private_Type (Ent)
4052 and then Is_Derived_Type (Ent)
4053 and then not Is_Tagged_Type (Ent)
4054 and then No (Full_View (Ent))
4055 then
4056 -- If this is a private type whose completion is a derivation from
4057 -- another private type, there is no full view, and the attribute
4058 -- belongs to the type itself, not its underlying parent.
4060 U_Ent := Ent;
4062 elsif Ekind (Ent) = E_Incomplete_Type then
4064 -- The attribute applies to the full view, set the entity of the
4065 -- attribute definition accordingly.
4067 Ent := Underlying_Type (Ent);
4068 U_Ent := Ent;
4069 Set_Entity (Nam, Ent);
4071 else
4072 U_Ent := Underlying_Type (Ent);
4073 end if;
4075 -- Avoid cascaded error
4077 if Etype (Nam) = Any_Type then
4078 return;
4080 -- Must be declared in current scope or in case of an aspect
4081 -- specification, must be visible in current scope.
4083 elsif Scope (Ent) /= Current_Scope
4084 and then
4085 not (From_Aspect_Specification (N)
4086 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4087 then
4088 Error_Msg_N ("entity must be declared in this scope", Nam);
4089 return;
4091 -- Must not be a source renaming (we do have some cases where the
4092 -- expander generates a renaming, and those cases are OK, in such
4093 -- cases any attribute applies to the renamed object as well).
4095 elsif Is_Object (Ent)
4096 and then Present (Renamed_Object (Ent))
4097 then
4098 -- Case of renamed object from source, this is an error
4100 if Comes_From_Source (Renamed_Object (Ent)) then
4101 Get_Name_String (Chars (N));
4102 Error_Msg_Strlen := Name_Len;
4103 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4104 Error_Msg_N
4105 ("~ clause not allowed for a renaming declaration "
4106 & "(RM 13.1(6))", Nam);
4107 return;
4109 -- For the case of a compiler generated renaming, the attribute
4110 -- definition clause applies to the renamed object created by the
4111 -- expander. The easiest general way to handle this is to create a
4112 -- copy of the attribute definition clause for this object.
4114 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4115 Insert_Action (N,
4116 Make_Attribute_Definition_Clause (Loc,
4117 Name =>
4118 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4119 Chars => Chars (N),
4120 Expression => Duplicate_Subexpr (Expression (N))));
4122 -- If the renamed object is not an entity, it must be a dereference
4123 -- of an unconstrained function call, and we must introduce a new
4124 -- declaration to capture the expression. This is needed in the case
4125 -- of 'Alignment, where the original declaration must be rewritten.
4127 else
4128 pragma Assert
4129 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4130 null;
4131 end if;
4133 -- If no underlying entity, use entity itself, applies to some
4134 -- previously detected error cases ???
4136 elsif No (U_Ent) then
4137 U_Ent := Ent;
4139 -- Cannot specify for a subtype (exception Object/Value_Size)
4141 elsif Is_Type (U_Ent)
4142 and then not Is_First_Subtype (U_Ent)
4143 and then Id /= Attribute_Object_Size
4144 and then Id /= Attribute_Value_Size
4145 and then not From_At_Mod (N)
4146 then
4147 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4148 return;
4149 end if;
4151 Set_Entity (N, U_Ent);
4152 Check_Restriction_No_Use_Of_Attribute (N);
4154 -- Switch on particular attribute
4156 case Id is
4158 -------------
4159 -- Address --
4160 -------------
4162 -- Address attribute definition clause
4164 when Attribute_Address => Address : begin
4166 -- A little error check, catch for X'Address use X'Address;
4168 if Nkind (Nam) = N_Identifier
4169 and then Nkind (Expr) = N_Attribute_Reference
4170 and then Attribute_Name (Expr) = Name_Address
4171 and then Nkind (Prefix (Expr)) = N_Identifier
4172 and then Chars (Nam) = Chars (Prefix (Expr))
4173 then
4174 Error_Msg_NE
4175 ("address for & is self-referencing", Prefix (Expr), Ent);
4176 return;
4177 end if;
4179 -- Not that special case, carry on with analysis of expression
4181 Analyze_And_Resolve (Expr, RTE (RE_Address));
4183 -- Even when ignoring rep clauses we need to indicate that the
4184 -- entity has an address clause and thus it is legal to declare
4185 -- it imported. Freeze will get rid of the address clause later.
4187 if Ignore_Rep_Clauses then
4188 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4189 Record_Rep_Item (U_Ent, N);
4190 end if;
4192 return;
4193 end if;
4195 if Duplicate_Clause then
4196 null;
4198 -- Case of address clause for subprogram
4200 elsif Is_Subprogram (U_Ent) then
4201 if Has_Homonym (U_Ent) then
4202 Error_Msg_N
4203 ("address clause cannot be given " &
4204 "for overloaded subprogram",
4205 Nam);
4206 return;
4207 end if;
4209 -- For subprograms, all address clauses are permitted, and we
4210 -- mark the subprogram as having a deferred freeze so that Gigi
4211 -- will not elaborate it too soon.
4213 -- Above needs more comments, what is too soon about???
4215 Set_Has_Delayed_Freeze (U_Ent);
4217 -- Case of address clause for entry
4219 elsif Ekind (U_Ent) = E_Entry then
4220 if Nkind (Parent (N)) = N_Task_Body then
4221 Error_Msg_N
4222 ("entry address must be specified in task spec", Nam);
4223 return;
4224 end if;
4226 -- For entries, we require a constant address
4228 Check_Constant_Address_Clause (Expr, U_Ent);
4230 -- Special checks for task types
4232 if Is_Task_Type (Scope (U_Ent))
4233 and then Comes_From_Source (Scope (U_Ent))
4234 then
4235 Error_Msg_N
4236 ("??entry address declared for entry in task type", N);
4237 Error_Msg_N
4238 ("\??only one task can be declared of this type", N);
4239 end if;
4241 -- Entry address clauses are obsolescent
4243 Check_Restriction (No_Obsolescent_Features, N);
4245 if Warn_On_Obsolescent_Feature then
4246 Error_Msg_N
4247 ("?j?attaching interrupt to task entry is an " &
4248 "obsolescent feature (RM J.7.1)", N);
4249 Error_Msg_N
4250 ("\?j?use interrupt procedure instead", N);
4251 end if;
4253 -- Case of an address clause for a controlled object which we
4254 -- consider to be erroneous.
4256 elsif Is_Controlled (Etype (U_Ent))
4257 or else Has_Controlled_Component (Etype (U_Ent))
4258 then
4259 Error_Msg_NE
4260 ("??controlled object& must not be overlaid", Nam, U_Ent);
4261 Error_Msg_N
4262 ("\??Program_Error will be raised at run time", Nam);
4263 Insert_Action (Declaration_Node (U_Ent),
4264 Make_Raise_Program_Error (Loc,
4265 Reason => PE_Overlaid_Controlled_Object));
4266 return;
4268 -- Case of address clause for a (non-controlled) object
4270 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4271 declare
4272 Expr : constant Node_Id := Expression (N);
4273 O_Ent : Entity_Id;
4274 Off : Boolean;
4276 begin
4277 -- Exported variables cannot have an address clause, because
4278 -- this cancels the effect of the pragma Export.
4280 if Is_Exported (U_Ent) then
4281 Error_Msg_N
4282 ("cannot export object with address clause", Nam);
4283 return;
4284 end if;
4286 Find_Overlaid_Entity (N, O_Ent, Off);
4288 -- Overlaying controlled objects is erroneous
4290 if Present (O_Ent)
4291 and then (Has_Controlled_Component (Etype (O_Ent))
4292 or else Is_Controlled (Etype (O_Ent)))
4293 then
4294 Error_Msg_N
4295 ("??cannot overlay with controlled object", Expr);
4296 Error_Msg_N
4297 ("\??Program_Error will be raised at run time", Expr);
4298 Insert_Action (Declaration_Node (U_Ent),
4299 Make_Raise_Program_Error (Loc,
4300 Reason => PE_Overlaid_Controlled_Object));
4301 return;
4303 elsif Present (O_Ent)
4304 and then Ekind (U_Ent) = E_Constant
4305 and then not Is_Constant_Object (O_Ent)
4306 then
4307 Error_Msg_N ("??constant overlays a variable", Expr);
4309 -- Imported variables can have an address clause, but then
4310 -- the import is pretty meaningless except to suppress
4311 -- initializations, so we do not need such variables to
4312 -- be statically allocated (and in fact it causes trouble
4313 -- if the address clause is a local value).
4315 elsif Is_Imported (U_Ent) then
4316 Set_Is_Statically_Allocated (U_Ent, False);
4317 end if;
4319 -- We mark a possible modification of a variable with an
4320 -- address clause, since it is likely aliasing is occurring.
4322 Note_Possible_Modification (Nam, Sure => False);
4324 -- Here we are checking for explicit overlap of one variable
4325 -- by another, and if we find this then mark the overlapped
4326 -- variable as also being volatile to prevent unwanted
4327 -- optimizations. This is a significant pessimization so
4328 -- avoid it when there is an offset, i.e. when the object
4329 -- is composite; they cannot be optimized easily anyway.
4331 if Present (O_Ent)
4332 and then Is_Object (O_Ent)
4333 and then not Off
4335 -- The following test is an expedient solution to what
4336 -- is really a problem in CodePeer. Suppressing the
4337 -- Set_Treat_As_Volatile call here prevents later
4338 -- generation (in some cases) of trees that CodePeer
4339 -- should, but currently does not, handle correctly.
4340 -- This test should probably be removed when CodePeer
4341 -- is improved, just because we want the tree CodePeer
4342 -- analyzes to match the tree for which we generate code
4343 -- as closely as is practical. ???
4345 and then not CodePeer_Mode
4346 then
4347 -- ??? O_Ent might not be in current unit
4349 Set_Treat_As_Volatile (O_Ent);
4350 end if;
4352 -- Legality checks on the address clause for initialized
4353 -- objects is deferred until the freeze point, because
4354 -- a subsequent pragma might indicate that the object
4355 -- is imported and thus not initialized. Also, the address
4356 -- clause might involve entities that have yet to be
4357 -- elaborated.
4359 Set_Has_Delayed_Freeze (U_Ent);
4361 -- If an initialization call has been generated for this
4362 -- object, it needs to be deferred to after the freeze node
4363 -- we have just now added, otherwise GIGI will see a
4364 -- reference to the variable (as actual to the IP call)
4365 -- before its definition.
4367 declare
4368 Init_Call : constant Node_Id :=
4369 Remove_Init_Call (U_Ent, N);
4371 begin
4372 if Present (Init_Call) then
4373 Append_Freeze_Action (U_Ent, Init_Call);
4375 -- Reset Initialization_Statements pointer so that
4376 -- if there is a pragma Import further down, it can
4377 -- clear any default initialization.
4379 Set_Initialization_Statements (U_Ent, Init_Call);
4380 end if;
4381 end;
4383 if Is_Exported (U_Ent) then
4384 Error_Msg_N
4385 ("& cannot be exported if an address clause is given",
4386 Nam);
4387 Error_Msg_N
4388 ("\define and export a variable "
4389 & "that holds its address instead", Nam);
4390 end if;
4392 -- Entity has delayed freeze, so we will generate an
4393 -- alignment check at the freeze point unless suppressed.
4395 if not Range_Checks_Suppressed (U_Ent)
4396 and then not Alignment_Checks_Suppressed (U_Ent)
4397 then
4398 Set_Check_Address_Alignment (N);
4399 end if;
4401 -- Kill the size check code, since we are not allocating
4402 -- the variable, it is somewhere else.
4404 Kill_Size_Check_Code (U_Ent);
4406 -- If the address clause is of the form:
4408 -- for Y'Address use X'Address
4410 -- or
4412 -- Const : constant Address := X'Address;
4413 -- ...
4414 -- for Y'Address use Const;
4416 -- then we make an entry in the table for checking the size
4417 -- and alignment of the overlaying variable. We defer this
4418 -- check till after code generation to take full advantage
4419 -- of the annotation done by the back end.
4421 -- If the entity has a generic type, the check will be
4422 -- performed in the instance if the actual type justifies
4423 -- it, and we do not insert the clause in the table to
4424 -- prevent spurious warnings.
4426 -- Note: we used to test Comes_From_Source and only give
4427 -- this warning for source entities, but we have removed
4428 -- this test. It really seems bogus to generate overlays
4429 -- that would trigger this warning in generated code.
4430 -- Furthermore, by removing the test, we handle the
4431 -- aspect case properly.
4433 if Address_Clause_Overlay_Warnings
4434 and then Present (O_Ent)
4435 and then Is_Object (O_Ent)
4436 then
4437 if not Is_Generic_Type (Etype (U_Ent)) then
4438 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4439 end if;
4441 -- If variable overlays a constant view, and we are
4442 -- warning on overlays, then mark the variable as
4443 -- overlaying a constant (we will give warnings later
4444 -- if this variable is assigned).
4446 if Is_Constant_Object (O_Ent)
4447 and then Ekind (U_Ent) = E_Variable
4448 then
4449 Set_Overlays_Constant (U_Ent);
4450 end if;
4451 end if;
4452 end;
4454 -- Not a valid entity for an address clause
4456 else
4457 Error_Msg_N ("address cannot be given for &", Nam);
4458 end if;
4459 end Address;
4461 ---------------
4462 -- Alignment --
4463 ---------------
4465 -- Alignment attribute definition clause
4467 when Attribute_Alignment => Alignment : declare
4468 Align : constant Uint := Get_Alignment_Value (Expr);
4469 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4471 begin
4472 FOnly := True;
4474 if not Is_Type (U_Ent)
4475 and then Ekind (U_Ent) /= E_Variable
4476 and then Ekind (U_Ent) /= E_Constant
4477 then
4478 Error_Msg_N ("alignment cannot be given for &", Nam);
4480 elsif Duplicate_Clause then
4481 null;
4483 elsif Align /= No_Uint then
4484 Set_Has_Alignment_Clause (U_Ent);
4486 -- Tagged type case, check for attempt to set alignment to a
4487 -- value greater than Max_Align, and reset if so.
4489 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4490 Error_Msg_N
4491 ("alignment for & set to Maximum_Aligment??", Nam);
4492 Set_Alignment (U_Ent, Max_Align);
4494 -- All other cases
4496 else
4497 Set_Alignment (U_Ent, Align);
4498 end if;
4500 -- For an array type, U_Ent is the first subtype. In that case,
4501 -- also set the alignment of the anonymous base type so that
4502 -- other subtypes (such as the itypes for aggregates of the
4503 -- type) also receive the expected alignment.
4505 if Is_Array_Type (U_Ent) then
4506 Set_Alignment (Base_Type (U_Ent), Align);
4507 end if;
4508 end if;
4509 end Alignment;
4511 ---------------
4512 -- Bit_Order --
4513 ---------------
4515 -- Bit_Order attribute definition clause
4517 when Attribute_Bit_Order => Bit_Order : declare
4518 begin
4519 if not Is_Record_Type (U_Ent) then
4520 Error_Msg_N
4521 ("Bit_Order can only be defined for record type", Nam);
4523 elsif Duplicate_Clause then
4524 null;
4526 else
4527 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4529 if Etype (Expr) = Any_Type then
4530 return;
4532 elsif not Is_OK_Static_Expression (Expr) then
4533 Flag_Non_Static_Expr
4534 ("Bit_Order requires static expression!", Expr);
4536 else
4537 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4538 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4539 end if;
4540 end if;
4541 end if;
4542 end Bit_Order;
4544 --------------------
4545 -- Component_Size --
4546 --------------------
4548 -- Component_Size attribute definition clause
4550 when Attribute_Component_Size => Component_Size_Case : declare
4551 Csize : constant Uint := Static_Integer (Expr);
4552 Ctyp : Entity_Id;
4553 Btype : Entity_Id;
4554 Biased : Boolean;
4555 New_Ctyp : Entity_Id;
4556 Decl : Node_Id;
4558 begin
4559 if not Is_Array_Type (U_Ent) then
4560 Error_Msg_N ("component size requires array type", Nam);
4561 return;
4562 end if;
4564 Btype := Base_Type (U_Ent);
4565 Ctyp := Component_Type (Btype);
4567 if Duplicate_Clause then
4568 null;
4570 elsif Rep_Item_Too_Early (Btype, N) then
4571 null;
4573 elsif Csize /= No_Uint then
4574 Check_Size (Expr, Ctyp, Csize, Biased);
4576 -- For the biased case, build a declaration for a subtype that
4577 -- will be used to represent the biased subtype that reflects
4578 -- the biased representation of components. We need the subtype
4579 -- to get proper conversions on referencing elements of the
4580 -- array. Note: component size clauses are ignored in VM mode.
4582 if VM_Target = No_VM then
4583 if Biased then
4584 New_Ctyp :=
4585 Make_Defining_Identifier (Loc,
4586 Chars =>
4587 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4589 Decl :=
4590 Make_Subtype_Declaration (Loc,
4591 Defining_Identifier => New_Ctyp,
4592 Subtype_Indication =>
4593 New_Occurrence_Of (Component_Type (Btype), Loc));
4595 Set_Parent (Decl, N);
4596 Analyze (Decl, Suppress => All_Checks);
4598 Set_Has_Delayed_Freeze (New_Ctyp, False);
4599 Set_Esize (New_Ctyp, Csize);
4600 Set_RM_Size (New_Ctyp, Csize);
4601 Init_Alignment (New_Ctyp);
4602 Set_Is_Itype (New_Ctyp, True);
4603 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4605 Set_Component_Type (Btype, New_Ctyp);
4606 Set_Biased (New_Ctyp, N, "component size clause");
4607 end if;
4609 Set_Component_Size (Btype, Csize);
4611 -- For VM case, we ignore component size clauses
4613 else
4614 -- Give a warning unless we are in GNAT mode, in which case
4615 -- the warning is suppressed since it is not useful.
4617 if not GNAT_Mode then
4618 Error_Msg_N
4619 ("component size ignored in this configuration??", N);
4620 end if;
4621 end if;
4623 -- Deal with warning on overridden size
4625 if Warn_On_Overridden_Size
4626 and then Has_Size_Clause (Ctyp)
4627 and then RM_Size (Ctyp) /= Csize
4628 then
4629 Error_Msg_NE
4630 ("component size overrides size clause for&?S?", N, Ctyp);
4631 end if;
4633 Set_Has_Component_Size_Clause (Btype, True);
4634 Set_Has_Non_Standard_Rep (Btype, True);
4635 end if;
4636 end Component_Size_Case;
4638 -----------------------
4639 -- Constant_Indexing --
4640 -----------------------
4642 when Attribute_Constant_Indexing =>
4643 Check_Indexing_Functions;
4645 ---------
4646 -- CPU --
4647 ---------
4649 when Attribute_CPU => CPU :
4650 begin
4651 -- CPU attribute definition clause not allowed except from aspect
4652 -- specification.
4654 if From_Aspect_Specification (N) then
4655 if not Is_Task_Type (U_Ent) then
4656 Error_Msg_N ("CPU can only be defined for task", Nam);
4658 elsif Duplicate_Clause then
4659 null;
4661 else
4662 -- The expression must be analyzed in the special manner
4663 -- described in "Handling of Default and Per-Object
4664 -- Expressions" in sem.ads.
4666 -- The visibility to the discriminants must be restored
4668 Push_Scope_And_Install_Discriminants (U_Ent);
4669 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4670 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4672 if not Is_OK_Static_Expression (Expr) then
4673 Check_Restriction (Static_Priorities, Expr);
4674 end if;
4675 end if;
4677 else
4678 Error_Msg_N
4679 ("attribute& cannot be set with definition clause", N);
4680 end if;
4681 end CPU;
4683 ----------------------
4684 -- Default_Iterator --
4685 ----------------------
4687 when Attribute_Default_Iterator => Default_Iterator : declare
4688 Func : Entity_Id;
4690 begin
4691 if not Is_Tagged_Type (U_Ent) then
4692 Error_Msg_N
4693 ("aspect Default_Iterator applies to tagged type", Nam);
4694 end if;
4696 Check_Iterator_Functions;
4698 Analyze (Expr);
4700 if not Is_Entity_Name (Expr)
4701 or else Ekind (Entity (Expr)) /= E_Function
4702 then
4703 Error_Msg_N ("aspect Iterator must be a function", Expr);
4704 else
4705 Func := Entity (Expr);
4706 end if;
4708 if No (First_Formal (Func))
4709 or else Etype (First_Formal (Func)) /= U_Ent
4710 then
4711 Error_Msg_NE
4712 ("Default Iterator must be a primitive of&", Func, U_Ent);
4713 end if;
4714 end Default_Iterator;
4716 ------------------------
4717 -- Dispatching_Domain --
4718 ------------------------
4720 when Attribute_Dispatching_Domain => Dispatching_Domain :
4721 begin
4722 -- Dispatching_Domain attribute definition clause not allowed
4723 -- except from aspect specification.
4725 if From_Aspect_Specification (N) then
4726 if not Is_Task_Type (U_Ent) then
4727 Error_Msg_N ("Dispatching_Domain can only be defined" &
4728 "for task",
4729 Nam);
4731 elsif Duplicate_Clause then
4732 null;
4734 else
4735 -- The expression must be analyzed in the special manner
4736 -- described in "Handling of Default and Per-Object
4737 -- Expressions" in sem.ads.
4739 -- The visibility to the discriminants must be restored
4741 Push_Scope_And_Install_Discriminants (U_Ent);
4743 Preanalyze_Spec_Expression
4744 (Expr, RTE (RE_Dispatching_Domain));
4746 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4747 end if;
4749 else
4750 Error_Msg_N
4751 ("attribute& cannot be set with definition clause", N);
4752 end if;
4753 end Dispatching_Domain;
4755 ------------------
4756 -- External_Tag --
4757 ------------------
4759 when Attribute_External_Tag => External_Tag :
4760 begin
4761 if not Is_Tagged_Type (U_Ent) then
4762 Error_Msg_N ("should be a tagged type", Nam);
4763 end if;
4765 if Duplicate_Clause then
4766 null;
4768 else
4769 Analyze_And_Resolve (Expr, Standard_String);
4771 if not Is_OK_Static_Expression (Expr) then
4772 Flag_Non_Static_Expr
4773 ("static string required for tag name!", Nam);
4774 end if;
4776 if VM_Target /= No_VM then
4777 Error_Msg_Name_1 := Attr;
4778 Error_Msg_N
4779 ("% attribute unsupported in this configuration", Nam);
4780 end if;
4782 if not Is_Library_Level_Entity (U_Ent) then
4783 Error_Msg_NE
4784 ("??non-unique external tag supplied for &", N, U_Ent);
4785 Error_Msg_N
4786 ("\??same external tag applies to all "
4787 & "subprogram calls", N);
4788 Error_Msg_N
4789 ("\??corresponding internal tag cannot be obtained", N);
4790 end if;
4791 end if;
4792 end External_Tag;
4794 --------------------------
4795 -- Implicit_Dereference --
4796 --------------------------
4798 when Attribute_Implicit_Dereference =>
4800 -- Legality checks already performed at the point of the type
4801 -- declaration, aspect is not delayed.
4803 null;
4805 -----------
4806 -- Input --
4807 -----------
4809 when Attribute_Input =>
4810 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4811 Set_Has_Specified_Stream_Input (Ent);
4813 ------------------------
4814 -- Interrupt_Priority --
4815 ------------------------
4817 when Attribute_Interrupt_Priority => Interrupt_Priority :
4818 begin
4819 -- Interrupt_Priority attribute definition clause not allowed
4820 -- except from aspect specification.
4822 if From_Aspect_Specification (N) then
4823 if not Is_Concurrent_Type (U_Ent) then
4824 Error_Msg_N
4825 ("Interrupt_Priority can only be defined for task "
4826 & "and protected object", Nam);
4828 elsif Duplicate_Clause then
4829 null;
4831 else
4832 -- The expression must be analyzed in the special manner
4833 -- described in "Handling of Default and Per-Object
4834 -- Expressions" in sem.ads.
4836 -- The visibility to the discriminants must be restored
4838 Push_Scope_And_Install_Discriminants (U_Ent);
4840 Preanalyze_Spec_Expression
4841 (Expr, RTE (RE_Interrupt_Priority));
4843 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4844 end if;
4846 else
4847 Error_Msg_N
4848 ("attribute& cannot be set with definition clause", N);
4849 end if;
4850 end Interrupt_Priority;
4852 --------------
4853 -- Iterable --
4854 --------------
4856 when Attribute_Iterable =>
4857 Analyze (Expr);
4859 if Nkind (Expr) /= N_Aggregate then
4860 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
4861 end if;
4863 declare
4864 Assoc : Node_Id;
4866 begin
4867 Assoc := First (Component_Associations (Expr));
4868 while Present (Assoc) loop
4869 if not Is_Entity_Name (Expression (Assoc)) then
4870 Error_Msg_N ("value must be a function", Assoc);
4871 end if;
4873 Next (Assoc);
4874 end loop;
4875 end;
4877 ----------------------
4878 -- Iterator_Element --
4879 ----------------------
4881 when Attribute_Iterator_Element =>
4882 Analyze (Expr);
4884 if not Is_Entity_Name (Expr)
4885 or else not Is_Type (Entity (Expr))
4886 then
4887 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4888 end if;
4890 -------------------
4891 -- Machine_Radix --
4892 -------------------
4894 -- Machine radix attribute definition clause
4896 when Attribute_Machine_Radix => Machine_Radix : declare
4897 Radix : constant Uint := Static_Integer (Expr);
4899 begin
4900 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
4901 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
4903 elsif Duplicate_Clause then
4904 null;
4906 elsif Radix /= No_Uint then
4907 Set_Has_Machine_Radix_Clause (U_Ent);
4908 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
4910 if Radix = 2 then
4911 null;
4912 elsif Radix = 10 then
4913 Set_Machine_Radix_10 (U_Ent);
4914 else
4915 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
4916 end if;
4917 end if;
4918 end Machine_Radix;
4920 -----------------
4921 -- Object_Size --
4922 -----------------
4924 -- Object_Size attribute definition clause
4926 when Attribute_Object_Size => Object_Size : declare
4927 Size : constant Uint := Static_Integer (Expr);
4929 Biased : Boolean;
4930 pragma Warnings (Off, Biased);
4932 begin
4933 if not Is_Type (U_Ent) then
4934 Error_Msg_N ("Object_Size cannot be given for &", Nam);
4936 elsif Duplicate_Clause then
4937 null;
4939 else
4940 Check_Size (Expr, U_Ent, Size, Biased);
4942 if Is_Scalar_Type (U_Ent) then
4943 if Size /= 8 and then Size /= 16 and then Size /= 32
4944 and then UI_Mod (Size, 64) /= 0
4945 then
4946 Error_Msg_N
4947 ("Object_Size must be 8, 16, 32, or multiple of 64",
4948 Expr);
4949 end if;
4951 elsif Size mod 8 /= 0 then
4952 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
4953 end if;
4955 Set_Esize (U_Ent, Size);
4956 Set_Has_Object_Size_Clause (U_Ent);
4957 Alignment_Check_For_Size_Change (U_Ent, Size);
4958 end if;
4959 end Object_Size;
4961 ------------
4962 -- Output --
4963 ------------
4965 when Attribute_Output =>
4966 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
4967 Set_Has_Specified_Stream_Output (Ent);
4969 --------------
4970 -- Priority --
4971 --------------
4973 when Attribute_Priority => Priority :
4974 begin
4975 -- Priority attribute definition clause not allowed except from
4976 -- aspect specification.
4978 if From_Aspect_Specification (N) then
4979 if not (Is_Concurrent_Type (U_Ent)
4980 or else Ekind (U_Ent) = E_Procedure)
4981 then
4982 Error_Msg_N
4983 ("Priority can only be defined for task and protected "
4984 & "object", Nam);
4986 elsif Duplicate_Clause then
4987 null;
4989 else
4990 -- The expression must be analyzed in the special manner
4991 -- described in "Handling of Default and Per-Object
4992 -- Expressions" in sem.ads.
4994 -- The visibility to the discriminants must be restored
4996 Push_Scope_And_Install_Discriminants (U_Ent);
4997 Preanalyze_Spec_Expression (Expr, Standard_Integer);
4998 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5000 if not Is_OK_Static_Expression (Expr) then
5001 Check_Restriction (Static_Priorities, Expr);
5002 end if;
5003 end if;
5005 else
5006 Error_Msg_N
5007 ("attribute& cannot be set with definition clause", N);
5008 end if;
5009 end Priority;
5011 ----------
5012 -- Read --
5013 ----------
5015 when Attribute_Read =>
5016 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5017 Set_Has_Specified_Stream_Read (Ent);
5019 --------------------------
5020 -- Scalar_Storage_Order --
5021 --------------------------
5023 -- Scalar_Storage_Order attribute definition clause
5025 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5026 begin
5027 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5028 Error_Msg_N
5029 ("Scalar_Storage_Order can only be defined for "
5030 & "record or array type", Nam);
5032 elsif Duplicate_Clause then
5033 null;
5035 else
5036 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5038 if Etype (Expr) = Any_Type then
5039 return;
5041 elsif not Is_OK_Static_Expression (Expr) then
5042 Flag_Non_Static_Expr
5043 ("Scalar_Storage_Order requires static expression!", Expr);
5045 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5047 -- Here for the case of a non-default (i.e. non-confirming)
5048 -- Scalar_Storage_Order attribute definition.
5050 if Support_Nondefault_SSO_On_Target then
5051 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5052 else
5053 Error_Msg_N
5054 ("non-default Scalar_Storage_Order "
5055 & "not supported on target", Expr);
5056 end if;
5057 end if;
5059 -- Clear SSO default indications since explicit setting of the
5060 -- order overrides the defaults.
5062 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5063 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5064 end if;
5065 end Scalar_Storage_Order;
5067 ----------
5068 -- Size --
5069 ----------
5071 -- Size attribute definition clause
5073 when Attribute_Size => Size : declare
5074 Size : constant Uint := Static_Integer (Expr);
5075 Etyp : Entity_Id;
5076 Biased : Boolean;
5078 begin
5079 FOnly := True;
5081 if Duplicate_Clause then
5082 null;
5084 elsif not Is_Type (U_Ent)
5085 and then Ekind (U_Ent) /= E_Variable
5086 and then Ekind (U_Ent) /= E_Constant
5087 then
5088 Error_Msg_N ("size cannot be given for &", Nam);
5090 elsif Is_Array_Type (U_Ent)
5091 and then not Is_Constrained (U_Ent)
5092 then
5093 Error_Msg_N
5094 ("size cannot be given for unconstrained array", Nam);
5096 elsif Size /= No_Uint then
5097 if VM_Target /= No_VM and then not GNAT_Mode then
5099 -- Size clause is not handled properly on VM targets.
5100 -- Display a warning unless we are in GNAT mode, in which
5101 -- case this is useless.
5103 Error_Msg_N
5104 ("size clauses are ignored in this configuration??", N);
5105 end if;
5107 if Is_Type (U_Ent) then
5108 Etyp := U_Ent;
5109 else
5110 Etyp := Etype (U_Ent);
5111 end if;
5113 -- Check size, note that Gigi is in charge of checking that the
5114 -- size of an array or record type is OK. Also we do not check
5115 -- the size in the ordinary fixed-point case, since it is too
5116 -- early to do so (there may be subsequent small clause that
5117 -- affects the size). We can check the size if a small clause
5118 -- has already been given.
5120 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5121 or else Has_Small_Clause (U_Ent)
5122 then
5123 Check_Size (Expr, Etyp, Size, Biased);
5124 Set_Biased (U_Ent, N, "size clause", Biased);
5125 end if;
5127 -- For types set RM_Size and Esize if possible
5129 if Is_Type (U_Ent) then
5130 Set_RM_Size (U_Ent, Size);
5132 -- For elementary types, increase Object_Size to power of 2,
5133 -- but not less than a storage unit in any case (normally
5134 -- this means it will be byte addressable).
5136 -- For all other types, nothing else to do, we leave Esize
5137 -- (object size) unset, the back end will set it from the
5138 -- size and alignment in an appropriate manner.
5140 -- In both cases, we check whether the alignment must be
5141 -- reset in the wake of the size change.
5143 if Is_Elementary_Type (U_Ent) then
5144 if Size <= System_Storage_Unit then
5145 Init_Esize (U_Ent, System_Storage_Unit);
5146 elsif Size <= 16 then
5147 Init_Esize (U_Ent, 16);
5148 elsif Size <= 32 then
5149 Init_Esize (U_Ent, 32);
5150 else
5151 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5152 end if;
5154 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5155 else
5156 Alignment_Check_For_Size_Change (U_Ent, Size);
5157 end if;
5159 -- For objects, set Esize only
5161 else
5162 if Is_Elementary_Type (Etyp) then
5163 if Size /= System_Storage_Unit
5164 and then
5165 Size /= System_Storage_Unit * 2
5166 and then
5167 Size /= System_Storage_Unit * 4
5168 and then
5169 Size /= System_Storage_Unit * 8
5170 then
5171 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5172 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5173 Error_Msg_N
5174 ("size for primitive object must be a power of 2"
5175 & " in the range ^-^", N);
5176 end if;
5177 end if;
5179 Set_Esize (U_Ent, Size);
5180 end if;
5182 Set_Has_Size_Clause (U_Ent);
5183 end if;
5184 end Size;
5186 -----------
5187 -- Small --
5188 -----------
5190 -- Small attribute definition clause
5192 when Attribute_Small => Small : declare
5193 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5194 Small : Ureal;
5196 begin
5197 Analyze_And_Resolve (Expr, Any_Real);
5199 if Etype (Expr) = Any_Type then
5200 return;
5202 elsif not Is_OK_Static_Expression (Expr) then
5203 Flag_Non_Static_Expr
5204 ("small requires static expression!", Expr);
5205 return;
5207 else
5208 Small := Expr_Value_R (Expr);
5210 if Small <= Ureal_0 then
5211 Error_Msg_N ("small value must be greater than zero", Expr);
5212 return;
5213 end if;
5215 end if;
5217 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5218 Error_Msg_N
5219 ("small requires an ordinary fixed point type", Nam);
5221 elsif Has_Small_Clause (U_Ent) then
5222 Error_Msg_N ("small already given for &", Nam);
5224 elsif Small > Delta_Value (U_Ent) then
5225 Error_Msg_N
5226 ("small value must not be greater than delta value", Nam);
5228 else
5229 Set_Small_Value (U_Ent, Small);
5230 Set_Small_Value (Implicit_Base, Small);
5231 Set_Has_Small_Clause (U_Ent);
5232 Set_Has_Small_Clause (Implicit_Base);
5233 Set_Has_Non_Standard_Rep (Implicit_Base);
5234 end if;
5235 end Small;
5237 ------------------
5238 -- Storage_Pool --
5239 ------------------
5241 -- Storage_Pool attribute definition clause
5243 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5244 Pool : Entity_Id;
5245 T : Entity_Id;
5247 begin
5248 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5249 Error_Msg_N
5250 ("storage pool cannot be given for access-to-subprogram type",
5251 Nam);
5252 return;
5254 elsif not
5255 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5256 then
5257 Error_Msg_N
5258 ("storage pool can only be given for access types", Nam);
5259 return;
5261 elsif Is_Derived_Type (U_Ent) then
5262 Error_Msg_N
5263 ("storage pool cannot be given for a derived access type",
5264 Nam);
5266 elsif Duplicate_Clause then
5267 return;
5269 elsif Present (Associated_Storage_Pool (U_Ent)) then
5270 Error_Msg_N ("storage pool already given for &", Nam);
5271 return;
5272 end if;
5274 -- Check for Storage_Size previously given
5276 declare
5277 SS : constant Node_Id :=
5278 Get_Attribute_Definition_Clause
5279 (U_Ent, Attribute_Storage_Size);
5280 begin
5281 if Present (SS) then
5282 Check_Pool_Size_Clash (U_Ent, N, SS);
5283 end if;
5284 end;
5286 -- Storage_Pool case
5288 if Id = Attribute_Storage_Pool then
5289 Analyze_And_Resolve
5290 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5292 -- In the Simple_Storage_Pool case, we allow a variable of any
5293 -- simple storage pool type, so we Resolve without imposing an
5294 -- expected type.
5296 else
5297 Analyze_And_Resolve (Expr);
5299 if not Present (Get_Rep_Pragma
5300 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5301 then
5302 Error_Msg_N
5303 ("expression must be of a simple storage pool type", Expr);
5304 end if;
5305 end if;
5307 if not Denotes_Variable (Expr) then
5308 Error_Msg_N ("storage pool must be a variable", Expr);
5309 return;
5310 end if;
5312 if Nkind (Expr) = N_Type_Conversion then
5313 T := Etype (Expression (Expr));
5314 else
5315 T := Etype (Expr);
5316 end if;
5318 -- The Stack_Bounded_Pool is used internally for implementing
5319 -- access types with a Storage_Size. Since it only work properly
5320 -- when used on one specific type, we need to check that it is not
5321 -- hijacked improperly:
5323 -- type T is access Integer;
5324 -- for T'Storage_Size use n;
5325 -- type Q is access Float;
5326 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5328 if RTE_Available (RE_Stack_Bounded_Pool)
5329 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5330 then
5331 Error_Msg_N ("non-shareable internal Pool", Expr);
5332 return;
5333 end if;
5335 -- If the argument is a name that is not an entity name, then
5336 -- we construct a renaming operation to define an entity of
5337 -- type storage pool.
5339 if not Is_Entity_Name (Expr)
5340 and then Is_Object_Reference (Expr)
5341 then
5342 Pool := Make_Temporary (Loc, 'P', Expr);
5344 declare
5345 Rnode : constant Node_Id :=
5346 Make_Object_Renaming_Declaration (Loc,
5347 Defining_Identifier => Pool,
5348 Subtype_Mark =>
5349 New_Occurrence_Of (Etype (Expr), Loc),
5350 Name => Expr);
5352 begin
5353 -- If the attribute definition clause comes from an aspect
5354 -- clause, then insert the renaming before the associated
5355 -- entity's declaration, since the attribute clause has
5356 -- not yet been appended to the declaration list.
5358 if From_Aspect_Specification (N) then
5359 Insert_Before (Parent (Entity (N)), Rnode);
5360 else
5361 Insert_Before (N, Rnode);
5362 end if;
5364 Analyze (Rnode);
5365 Set_Associated_Storage_Pool (U_Ent, Pool);
5366 end;
5368 elsif Is_Entity_Name (Expr) then
5369 Pool := Entity (Expr);
5371 -- If pool is a renamed object, get original one. This can
5372 -- happen with an explicit renaming, and within instances.
5374 while Present (Renamed_Object (Pool))
5375 and then Is_Entity_Name (Renamed_Object (Pool))
5376 loop
5377 Pool := Entity (Renamed_Object (Pool));
5378 end loop;
5380 if Present (Renamed_Object (Pool))
5381 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5382 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5383 then
5384 Pool := Entity (Expression (Renamed_Object (Pool)));
5385 end if;
5387 Set_Associated_Storage_Pool (U_Ent, Pool);
5389 elsif Nkind (Expr) = N_Type_Conversion
5390 and then Is_Entity_Name (Expression (Expr))
5391 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5392 then
5393 Pool := Entity (Expression (Expr));
5394 Set_Associated_Storage_Pool (U_Ent, Pool);
5396 else
5397 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5398 return;
5399 end if;
5400 end;
5402 ------------------
5403 -- Storage_Size --
5404 ------------------
5406 -- Storage_Size attribute definition clause
5408 when Attribute_Storage_Size => Storage_Size : declare
5409 Btype : constant Entity_Id := Base_Type (U_Ent);
5411 begin
5412 if Is_Task_Type (U_Ent) then
5414 -- Check obsolescent (but never obsolescent if from aspect)
5416 if not From_Aspect_Specification (N) then
5417 Check_Restriction (No_Obsolescent_Features, N);
5419 if Warn_On_Obsolescent_Feature then
5420 Error_Msg_N
5421 ("?j?storage size clause for task is an " &
5422 "obsolescent feature (RM J.9)", N);
5423 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5424 end if;
5425 end if;
5427 FOnly := True;
5428 end if;
5430 if not Is_Access_Type (U_Ent)
5431 and then Ekind (U_Ent) /= E_Task_Type
5432 then
5433 Error_Msg_N ("storage size cannot be given for &", Nam);
5435 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5436 Error_Msg_N
5437 ("storage size cannot be given for a derived access type",
5438 Nam);
5440 elsif Duplicate_Clause then
5441 null;
5443 else
5444 Analyze_And_Resolve (Expr, Any_Integer);
5446 if Is_Access_Type (U_Ent) then
5448 -- Check for Storage_Pool previously given
5450 declare
5451 SP : constant Node_Id :=
5452 Get_Attribute_Definition_Clause
5453 (U_Ent, Attribute_Storage_Pool);
5455 begin
5456 if Present (SP) then
5457 Check_Pool_Size_Clash (U_Ent, SP, N);
5458 end if;
5459 end;
5461 -- Special case of for x'Storage_Size use 0
5463 if Is_OK_Static_Expression (Expr)
5464 and then Expr_Value (Expr) = 0
5465 then
5466 Set_No_Pool_Assigned (Btype);
5467 end if;
5468 end if;
5470 Set_Has_Storage_Size_Clause (Btype);
5471 end if;
5472 end Storage_Size;
5474 -----------------
5475 -- Stream_Size --
5476 -----------------
5478 when Attribute_Stream_Size => Stream_Size : declare
5479 Size : constant Uint := Static_Integer (Expr);
5481 begin
5482 if Ada_Version <= Ada_95 then
5483 Check_Restriction (No_Implementation_Attributes, N);
5484 end if;
5486 if Duplicate_Clause then
5487 null;
5489 elsif Is_Elementary_Type (U_Ent) then
5490 if Size /= System_Storage_Unit
5491 and then
5492 Size /= System_Storage_Unit * 2
5493 and then
5494 Size /= System_Storage_Unit * 4
5495 and then
5496 Size /= System_Storage_Unit * 8
5497 then
5498 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5499 Error_Msg_N
5500 ("stream size for elementary type must be a"
5501 & " power of 2 and at least ^", N);
5503 elsif RM_Size (U_Ent) > Size then
5504 Error_Msg_Uint_1 := RM_Size (U_Ent);
5505 Error_Msg_N
5506 ("stream size for elementary type must be a"
5507 & " power of 2 and at least ^", N);
5508 end if;
5510 Set_Has_Stream_Size_Clause (U_Ent);
5512 else
5513 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5514 end if;
5515 end Stream_Size;
5517 ----------------
5518 -- Value_Size --
5519 ----------------
5521 -- Value_Size attribute definition clause
5523 when Attribute_Value_Size => Value_Size : declare
5524 Size : constant Uint := Static_Integer (Expr);
5525 Biased : Boolean;
5527 begin
5528 if not Is_Type (U_Ent) then
5529 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5531 elsif Duplicate_Clause then
5532 null;
5534 elsif Is_Array_Type (U_Ent)
5535 and then not Is_Constrained (U_Ent)
5536 then
5537 Error_Msg_N
5538 ("Value_Size cannot be given for unconstrained array", Nam);
5540 else
5541 if Is_Elementary_Type (U_Ent) then
5542 Check_Size (Expr, U_Ent, Size, Biased);
5543 Set_Biased (U_Ent, N, "value size clause", Biased);
5544 end if;
5546 Set_RM_Size (U_Ent, Size);
5547 end if;
5548 end Value_Size;
5550 -----------------------
5551 -- Variable_Indexing --
5552 -----------------------
5554 when Attribute_Variable_Indexing =>
5555 Check_Indexing_Functions;
5557 -----------
5558 -- Write --
5559 -----------
5561 when Attribute_Write =>
5562 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5563 Set_Has_Specified_Stream_Write (Ent);
5565 -- All other attributes cannot be set
5567 when others =>
5568 Error_Msg_N
5569 ("attribute& cannot be set with definition clause", N);
5570 end case;
5572 -- The test for the type being frozen must be performed after any
5573 -- expression the clause has been analyzed since the expression itself
5574 -- might cause freezing that makes the clause illegal.
5576 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5577 return;
5578 end if;
5579 end Analyze_Attribute_Definition_Clause;
5581 ----------------------------
5582 -- Analyze_Code_Statement --
5583 ----------------------------
5585 procedure Analyze_Code_Statement (N : Node_Id) is
5586 HSS : constant Node_Id := Parent (N);
5587 SBody : constant Node_Id := Parent (HSS);
5588 Subp : constant Entity_Id := Current_Scope;
5589 Stmt : Node_Id;
5590 Decl : Node_Id;
5591 StmtO : Node_Id;
5592 DeclO : Node_Id;
5594 begin
5595 -- Analyze and check we get right type, note that this implements the
5596 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5597 -- is the only way that Asm_Insn could possibly be visible.
5599 Analyze_And_Resolve (Expression (N));
5601 if Etype (Expression (N)) = Any_Type then
5602 return;
5603 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5604 Error_Msg_N ("incorrect type for code statement", N);
5605 return;
5606 end if;
5608 Check_Code_Statement (N);
5610 -- Make sure we appear in the handled statement sequence of a
5611 -- subprogram (RM 13.8(3)).
5613 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5614 or else Nkind (SBody) /= N_Subprogram_Body
5615 then
5616 Error_Msg_N
5617 ("code statement can only appear in body of subprogram", N);
5618 return;
5619 end if;
5621 -- Do remaining checks (RM 13.8(3)) if not already done
5623 if not Is_Machine_Code_Subprogram (Subp) then
5624 Set_Is_Machine_Code_Subprogram (Subp);
5626 -- No exception handlers allowed
5628 if Present (Exception_Handlers (HSS)) then
5629 Error_Msg_N
5630 ("exception handlers not permitted in machine code subprogram",
5631 First (Exception_Handlers (HSS)));
5632 end if;
5634 -- No declarations other than use clauses and pragmas (we allow
5635 -- certain internally generated declarations as well).
5637 Decl := First (Declarations (SBody));
5638 while Present (Decl) loop
5639 DeclO := Original_Node (Decl);
5640 if Comes_From_Source (DeclO)
5641 and not Nkind_In (DeclO, N_Pragma,
5642 N_Use_Package_Clause,
5643 N_Use_Type_Clause,
5644 N_Implicit_Label_Declaration)
5645 then
5646 Error_Msg_N
5647 ("this declaration not allowed in machine code subprogram",
5648 DeclO);
5649 end if;
5651 Next (Decl);
5652 end loop;
5654 -- No statements other than code statements, pragmas, and labels.
5655 -- Again we allow certain internally generated statements.
5657 -- In Ada 2012, qualified expressions are names, and the code
5658 -- statement is initially parsed as a procedure call.
5660 Stmt := First (Statements (HSS));
5661 while Present (Stmt) loop
5662 StmtO := Original_Node (Stmt);
5664 -- A procedure call transformed into a code statement is OK.
5666 if Ada_Version >= Ada_2012
5667 and then Nkind (StmtO) = N_Procedure_Call_Statement
5668 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5669 then
5670 null;
5672 elsif Comes_From_Source (StmtO)
5673 and then not Nkind_In (StmtO, N_Pragma,
5674 N_Label,
5675 N_Code_Statement)
5676 then
5677 Error_Msg_N
5678 ("this statement is not allowed in machine code subprogram",
5679 StmtO);
5680 end if;
5682 Next (Stmt);
5683 end loop;
5684 end if;
5685 end Analyze_Code_Statement;
5687 -----------------------------------------------
5688 -- Analyze_Enumeration_Representation_Clause --
5689 -----------------------------------------------
5691 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5692 Ident : constant Node_Id := Identifier (N);
5693 Aggr : constant Node_Id := Array_Aggregate (N);
5694 Enumtype : Entity_Id;
5695 Elit : Entity_Id;
5696 Expr : Node_Id;
5697 Assoc : Node_Id;
5698 Choice : Node_Id;
5699 Val : Uint;
5701 Err : Boolean := False;
5702 -- Set True to avoid cascade errors and crashes on incorrect source code
5704 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5705 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5706 -- Allowed range of universal integer (= allowed range of enum lit vals)
5708 Min : Uint;
5709 Max : Uint;
5710 -- Minimum and maximum values of entries
5712 Max_Node : Node_Id;
5713 -- Pointer to node for literal providing max value
5715 begin
5716 if Ignore_Rep_Clauses then
5717 Kill_Rep_Clause (N);
5718 return;
5719 end if;
5721 -- Ignore enumeration rep clauses by default in CodePeer mode,
5722 -- unless -gnatd.I is specified, as a work around for potential false
5723 -- positive messages.
5725 if CodePeer_Mode and not Debug_Flag_Dot_II then
5726 return;
5727 end if;
5729 -- First some basic error checks
5731 Find_Type (Ident);
5732 Enumtype := Entity (Ident);
5734 if Enumtype = Any_Type
5735 or else Rep_Item_Too_Early (Enumtype, N)
5736 then
5737 return;
5738 else
5739 Enumtype := Underlying_Type (Enumtype);
5740 end if;
5742 if not Is_Enumeration_Type (Enumtype) then
5743 Error_Msg_NE
5744 ("enumeration type required, found}",
5745 Ident, First_Subtype (Enumtype));
5746 return;
5747 end if;
5749 -- Ignore rep clause on generic actual type. This will already have
5750 -- been flagged on the template as an error, and this is the safest
5751 -- way to ensure we don't get a junk cascaded message in the instance.
5753 if Is_Generic_Actual_Type (Enumtype) then
5754 return;
5756 -- Type must be in current scope
5758 elsif Scope (Enumtype) /= Current_Scope then
5759 Error_Msg_N ("type must be declared in this scope", Ident);
5760 return;
5762 -- Type must be a first subtype
5764 elsif not Is_First_Subtype (Enumtype) then
5765 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5766 return;
5768 -- Ignore duplicate rep clause
5770 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5771 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5772 return;
5774 -- Don't allow rep clause for standard [wide_[wide_]]character
5776 elsif Is_Standard_Character_Type (Enumtype) then
5777 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5778 return;
5780 -- Check that the expression is a proper aggregate (no parentheses)
5782 elsif Paren_Count (Aggr) /= 0 then
5783 Error_Msg
5784 ("extra parentheses surrounding aggregate not allowed",
5785 First_Sloc (Aggr));
5786 return;
5788 -- All tests passed, so set rep clause in place
5790 else
5791 Set_Has_Enumeration_Rep_Clause (Enumtype);
5792 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5793 end if;
5795 -- Now we process the aggregate. Note that we don't use the normal
5796 -- aggregate code for this purpose, because we don't want any of the
5797 -- normal expansion activities, and a number of special semantic
5798 -- rules apply (including the component type being any integer type)
5800 Elit := First_Literal (Enumtype);
5802 -- First the positional entries if any
5804 if Present (Expressions (Aggr)) then
5805 Expr := First (Expressions (Aggr));
5806 while Present (Expr) loop
5807 if No (Elit) then
5808 Error_Msg_N ("too many entries in aggregate", Expr);
5809 return;
5810 end if;
5812 Val := Static_Integer (Expr);
5814 -- Err signals that we found some incorrect entries processing
5815 -- the list. The final checks for completeness and ordering are
5816 -- skipped in this case.
5818 if Val = No_Uint then
5819 Err := True;
5821 elsif Val < Lo or else Hi < Val then
5822 Error_Msg_N ("value outside permitted range", Expr);
5823 Err := True;
5824 end if;
5826 Set_Enumeration_Rep (Elit, Val);
5827 Set_Enumeration_Rep_Expr (Elit, Expr);
5828 Next (Expr);
5829 Next (Elit);
5830 end loop;
5831 end if;
5833 -- Now process the named entries if present
5835 if Present (Component_Associations (Aggr)) then
5836 Assoc := First (Component_Associations (Aggr));
5837 while Present (Assoc) loop
5838 Choice := First (Choices (Assoc));
5840 if Present (Next (Choice)) then
5841 Error_Msg_N
5842 ("multiple choice not allowed here", Next (Choice));
5843 Err := True;
5844 end if;
5846 if Nkind (Choice) = N_Others_Choice then
5847 Error_Msg_N ("others choice not allowed here", Choice);
5848 Err := True;
5850 elsif Nkind (Choice) = N_Range then
5852 -- ??? should allow zero/one element range here
5854 Error_Msg_N ("range not allowed here", Choice);
5855 Err := True;
5857 else
5858 Analyze_And_Resolve (Choice, Enumtype);
5860 if Error_Posted (Choice) then
5861 Err := True;
5862 end if;
5864 if not Err then
5865 if Is_Entity_Name (Choice)
5866 and then Is_Type (Entity (Choice))
5867 then
5868 Error_Msg_N ("subtype name not allowed here", Choice);
5869 Err := True;
5871 -- ??? should allow static subtype with zero/one entry
5873 elsif Etype (Choice) = Base_Type (Enumtype) then
5874 if not Is_OK_Static_Expression (Choice) then
5875 Flag_Non_Static_Expr
5876 ("non-static expression used for choice!", Choice);
5877 Err := True;
5879 else
5880 Elit := Expr_Value_E (Choice);
5882 if Present (Enumeration_Rep_Expr (Elit)) then
5883 Error_Msg_Sloc :=
5884 Sloc (Enumeration_Rep_Expr (Elit));
5885 Error_Msg_NE
5886 ("representation for& previously given#",
5887 Choice, Elit);
5888 Err := True;
5889 end if;
5891 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
5893 Expr := Expression (Assoc);
5894 Val := Static_Integer (Expr);
5896 if Val = No_Uint then
5897 Err := True;
5899 elsif Val < Lo or else Hi < Val then
5900 Error_Msg_N ("value outside permitted range", Expr);
5901 Err := True;
5902 end if;
5904 Set_Enumeration_Rep (Elit, Val);
5905 end if;
5906 end if;
5907 end if;
5908 end if;
5910 Next (Assoc);
5911 end loop;
5912 end if;
5914 -- Aggregate is fully processed. Now we check that a full set of
5915 -- representations was given, and that they are in range and in order.
5916 -- These checks are only done if no other errors occurred.
5918 if not Err then
5919 Min := No_Uint;
5920 Max := No_Uint;
5922 Elit := First_Literal (Enumtype);
5923 while Present (Elit) loop
5924 if No (Enumeration_Rep_Expr (Elit)) then
5925 Error_Msg_NE ("missing representation for&!", N, Elit);
5927 else
5928 Val := Enumeration_Rep (Elit);
5930 if Min = No_Uint then
5931 Min := Val;
5932 end if;
5934 if Val /= No_Uint then
5935 if Max /= No_Uint and then Val <= Max then
5936 Error_Msg_NE
5937 ("enumeration value for& not ordered!",
5938 Enumeration_Rep_Expr (Elit), Elit);
5939 end if;
5941 Max_Node := Enumeration_Rep_Expr (Elit);
5942 Max := Val;
5943 end if;
5945 -- If there is at least one literal whose representation is not
5946 -- equal to the Pos value, then note that this enumeration type
5947 -- has a non-standard representation.
5949 if Val /= Enumeration_Pos (Elit) then
5950 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
5951 end if;
5952 end if;
5954 Next (Elit);
5955 end loop;
5957 -- Now set proper size information
5959 declare
5960 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
5962 begin
5963 if Has_Size_Clause (Enumtype) then
5965 -- All OK, if size is OK now
5967 if RM_Size (Enumtype) >= Minsize then
5968 null;
5970 else
5971 -- Try if we can get by with biasing
5973 Minsize :=
5974 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
5976 -- Error message if even biasing does not work
5978 if RM_Size (Enumtype) < Minsize then
5979 Error_Msg_Uint_1 := RM_Size (Enumtype);
5980 Error_Msg_Uint_2 := Max;
5981 Error_Msg_N
5982 ("previously given size (^) is too small "
5983 & "for this value (^)", Max_Node);
5985 -- If biasing worked, indicate that we now have biased rep
5987 else
5988 Set_Biased
5989 (Enumtype, Size_Clause (Enumtype), "size clause");
5990 end if;
5991 end if;
5993 else
5994 Set_RM_Size (Enumtype, Minsize);
5995 Set_Enum_Esize (Enumtype);
5996 end if;
5998 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
5999 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6000 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6001 end;
6002 end if;
6004 -- We repeat the too late test in case it froze itself
6006 if Rep_Item_Too_Late (Enumtype, N) then
6007 null;
6008 end if;
6009 end Analyze_Enumeration_Representation_Clause;
6011 ----------------------------
6012 -- Analyze_Free_Statement --
6013 ----------------------------
6015 procedure Analyze_Free_Statement (N : Node_Id) is
6016 begin
6017 Analyze (Expression (N));
6018 end Analyze_Free_Statement;
6020 ---------------------------
6021 -- Analyze_Freeze_Entity --
6022 ---------------------------
6024 procedure Analyze_Freeze_Entity (N : Node_Id) is
6025 begin
6026 Freeze_Entity_Checks (N);
6027 end Analyze_Freeze_Entity;
6029 -----------------------------------
6030 -- Analyze_Freeze_Generic_Entity --
6031 -----------------------------------
6033 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6034 begin
6035 Freeze_Entity_Checks (N);
6036 end Analyze_Freeze_Generic_Entity;
6038 ------------------------------------------
6039 -- Analyze_Record_Representation_Clause --
6040 ------------------------------------------
6042 -- Note: we check as much as we can here, but we can't do any checks
6043 -- based on the position values (e.g. overlap checks) until freeze time
6044 -- because especially in Ada 2005 (machine scalar mode), the processing
6045 -- for non-standard bit order can substantially change the positions.
6046 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6047 -- for the remainder of this processing.
6049 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6050 Ident : constant Node_Id := Identifier (N);
6051 Biased : Boolean;
6052 CC : Node_Id;
6053 Comp : Entity_Id;
6054 Fbit : Uint;
6055 Hbit : Uint := Uint_0;
6056 Lbit : Uint;
6057 Ocomp : Entity_Id;
6058 Posit : Uint;
6059 Rectype : Entity_Id;
6060 Recdef : Node_Id;
6062 function Is_Inherited (Comp : Entity_Id) return Boolean;
6063 -- True if Comp is an inherited component in a record extension
6065 ------------------
6066 -- Is_Inherited --
6067 ------------------
6069 function Is_Inherited (Comp : Entity_Id) return Boolean is
6070 Comp_Base : Entity_Id;
6072 begin
6073 if Ekind (Rectype) = E_Record_Subtype then
6074 Comp_Base := Original_Record_Component (Comp);
6075 else
6076 Comp_Base := Comp;
6077 end if;
6079 return Comp_Base /= Original_Record_Component (Comp_Base);
6080 end Is_Inherited;
6082 -- Local variables
6084 Is_Record_Extension : Boolean;
6085 -- True if Rectype is a record extension
6087 CR_Pragma : Node_Id := Empty;
6088 -- Points to N_Pragma node if Complete_Representation pragma present
6090 -- Start of processing for Analyze_Record_Representation_Clause
6092 begin
6093 if Ignore_Rep_Clauses then
6094 Kill_Rep_Clause (N);
6095 return;
6096 end if;
6098 Find_Type (Ident);
6099 Rectype := Entity (Ident);
6101 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6102 return;
6103 else
6104 Rectype := Underlying_Type (Rectype);
6105 end if;
6107 -- First some basic error checks
6109 if not Is_Record_Type (Rectype) then
6110 Error_Msg_NE
6111 ("record type required, found}", Ident, First_Subtype (Rectype));
6112 return;
6114 elsif Scope (Rectype) /= Current_Scope then
6115 Error_Msg_N ("type must be declared in this scope", N);
6116 return;
6118 elsif not Is_First_Subtype (Rectype) then
6119 Error_Msg_N ("cannot give record rep clause for subtype", N);
6120 return;
6122 elsif Has_Record_Rep_Clause (Rectype) then
6123 Error_Msg_N ("duplicate record rep clause ignored", N);
6124 return;
6126 elsif Rep_Item_Too_Late (Rectype, N) then
6127 return;
6128 end if;
6130 -- We know we have a first subtype, now possibly go the the anonymous
6131 -- base type to determine whether Rectype is a record extension.
6133 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6134 Is_Record_Extension :=
6135 Nkind (Recdef) = N_Derived_Type_Definition
6136 and then Present (Record_Extension_Part (Recdef));
6138 if Present (Mod_Clause (N)) then
6139 declare
6140 Loc : constant Source_Ptr := Sloc (N);
6141 M : constant Node_Id := Mod_Clause (N);
6142 P : constant List_Id := Pragmas_Before (M);
6143 AtM_Nod : Node_Id;
6145 Mod_Val : Uint;
6146 pragma Warnings (Off, Mod_Val);
6148 begin
6149 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6151 if Warn_On_Obsolescent_Feature then
6152 Error_Msg_N
6153 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6154 Error_Msg_N
6155 ("\?j?use alignment attribute definition clause instead", N);
6156 end if;
6158 if Present (P) then
6159 Analyze_List (P);
6160 end if;
6162 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6163 -- the Mod clause into an alignment clause anyway, so that the
6164 -- back-end can compute and back-annotate properly the size and
6165 -- alignment of types that may include this record.
6167 -- This seems dubious, this destroys the source tree in a manner
6168 -- not detectable by ASIS ???
6170 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6171 AtM_Nod :=
6172 Make_Attribute_Definition_Clause (Loc,
6173 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6174 Chars => Name_Alignment,
6175 Expression => Relocate_Node (Expression (M)));
6177 Set_From_At_Mod (AtM_Nod);
6178 Insert_After (N, AtM_Nod);
6179 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6180 Set_Mod_Clause (N, Empty);
6182 else
6183 -- Get the alignment value to perform error checking
6185 Mod_Val := Get_Alignment_Value (Expression (M));
6186 end if;
6187 end;
6188 end if;
6190 -- For untagged types, clear any existing component clauses for the
6191 -- type. If the type is derived, this is what allows us to override
6192 -- a rep clause for the parent. For type extensions, the representation
6193 -- of the inherited components is inherited, so we want to keep previous
6194 -- component clauses for completeness.
6196 if not Is_Tagged_Type (Rectype) then
6197 Comp := First_Component_Or_Discriminant (Rectype);
6198 while Present (Comp) loop
6199 Set_Component_Clause (Comp, Empty);
6200 Next_Component_Or_Discriminant (Comp);
6201 end loop;
6202 end if;
6204 -- All done if no component clauses
6206 CC := First (Component_Clauses (N));
6208 if No (CC) then
6209 return;
6210 end if;
6212 -- A representation like this applies to the base type
6214 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6215 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6216 Set_Has_Specified_Layout (Base_Type (Rectype));
6218 -- Process the component clauses
6220 while Present (CC) loop
6222 -- Pragma
6224 if Nkind (CC) = N_Pragma then
6225 Analyze (CC);
6227 -- The only pragma of interest is Complete_Representation
6229 if Pragma_Name (CC) = Name_Complete_Representation then
6230 CR_Pragma := CC;
6231 end if;
6233 -- Processing for real component clause
6235 else
6236 Posit := Static_Integer (Position (CC));
6237 Fbit := Static_Integer (First_Bit (CC));
6238 Lbit := Static_Integer (Last_Bit (CC));
6240 if Posit /= No_Uint
6241 and then Fbit /= No_Uint
6242 and then Lbit /= No_Uint
6243 then
6244 if Posit < 0 then
6245 Error_Msg_N
6246 ("position cannot be negative", Position (CC));
6248 elsif Fbit < 0 then
6249 Error_Msg_N
6250 ("first bit cannot be negative", First_Bit (CC));
6252 -- The Last_Bit specified in a component clause must not be
6253 -- less than the First_Bit minus one (RM-13.5.1(10)).
6255 elsif Lbit < Fbit - 1 then
6256 Error_Msg_N
6257 ("last bit cannot be less than first bit minus one",
6258 Last_Bit (CC));
6260 -- Values look OK, so find the corresponding record component
6261 -- Even though the syntax allows an attribute reference for
6262 -- implementation-defined components, GNAT does not allow the
6263 -- tag to get an explicit position.
6265 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6266 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6267 Error_Msg_N ("position of tag cannot be specified", CC);
6268 else
6269 Error_Msg_N ("illegal component name", CC);
6270 end if;
6272 else
6273 Comp := First_Entity (Rectype);
6274 while Present (Comp) loop
6275 exit when Chars (Comp) = Chars (Component_Name (CC));
6276 Next_Entity (Comp);
6277 end loop;
6279 if No (Comp) then
6281 -- Maybe component of base type that is absent from
6282 -- statically constrained first subtype.
6284 Comp := First_Entity (Base_Type (Rectype));
6285 while Present (Comp) loop
6286 exit when Chars (Comp) = Chars (Component_Name (CC));
6287 Next_Entity (Comp);
6288 end loop;
6289 end if;
6291 if No (Comp) then
6292 Error_Msg_N
6293 ("component clause is for non-existent field", CC);
6295 -- Ada 2012 (AI05-0026): Any name that denotes a
6296 -- discriminant of an object of an unchecked union type
6297 -- shall not occur within a record_representation_clause.
6299 -- The general restriction of using record rep clauses on
6300 -- Unchecked_Union types has now been lifted. Since it is
6301 -- possible to introduce a record rep clause which mentions
6302 -- the discriminant of an Unchecked_Union in non-Ada 2012
6303 -- code, this check is applied to all versions of the
6304 -- language.
6306 elsif Ekind (Comp) = E_Discriminant
6307 and then Is_Unchecked_Union (Rectype)
6308 then
6309 Error_Msg_N
6310 ("cannot reference discriminant of unchecked union",
6311 Component_Name (CC));
6313 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6314 Error_Msg_NE
6315 ("component clause not allowed for inherited "
6316 & "component&", CC, Comp);
6318 elsif Present (Component_Clause (Comp)) then
6320 -- Diagnose duplicate rep clause, or check consistency
6321 -- if this is an inherited component. In a double fault,
6322 -- there may be a duplicate inconsistent clause for an
6323 -- inherited component.
6325 if Scope (Original_Record_Component (Comp)) = Rectype
6326 or else Parent (Component_Clause (Comp)) = N
6327 then
6328 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6329 Error_Msg_N ("component clause previously given#", CC);
6331 else
6332 declare
6333 Rep1 : constant Node_Id := Component_Clause (Comp);
6334 begin
6335 if Intval (Position (Rep1)) /=
6336 Intval (Position (CC))
6337 or else Intval (First_Bit (Rep1)) /=
6338 Intval (First_Bit (CC))
6339 or else Intval (Last_Bit (Rep1)) /=
6340 Intval (Last_Bit (CC))
6341 then
6342 Error_Msg_N
6343 ("component clause inconsistent "
6344 & "with representation of ancestor", CC);
6346 elsif Warn_On_Redundant_Constructs then
6347 Error_Msg_N
6348 ("?r?redundant confirming component clause "
6349 & "for component!", CC);
6350 end if;
6351 end;
6352 end if;
6354 -- Normal case where this is the first component clause we
6355 -- have seen for this entity, so set it up properly.
6357 else
6358 -- Make reference for field in record rep clause and set
6359 -- appropriate entity field in the field identifier.
6361 Generate_Reference
6362 (Comp, Component_Name (CC), Set_Ref => False);
6363 Set_Entity (Component_Name (CC), Comp);
6365 -- Update Fbit and Lbit to the actual bit number
6367 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6368 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6370 if Has_Size_Clause (Rectype)
6371 and then RM_Size (Rectype) <= Lbit
6372 then
6373 Error_Msg_N
6374 ("bit number out of range of specified size",
6375 Last_Bit (CC));
6376 else
6377 Set_Component_Clause (Comp, CC);
6378 Set_Component_Bit_Offset (Comp, Fbit);
6379 Set_Esize (Comp, 1 + (Lbit - Fbit));
6380 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6381 Set_Normalized_Position (Comp, Fbit / SSU);
6383 if Warn_On_Overridden_Size
6384 and then Has_Size_Clause (Etype (Comp))
6385 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6386 then
6387 Error_Msg_NE
6388 ("?S?component size overrides size clause for&",
6389 Component_Name (CC), Etype (Comp));
6390 end if;
6392 -- This information is also set in the corresponding
6393 -- component of the base type, found by accessing the
6394 -- Original_Record_Component link if it is present.
6396 Ocomp := Original_Record_Component (Comp);
6398 if Hbit < Lbit then
6399 Hbit := Lbit;
6400 end if;
6402 Check_Size
6403 (Component_Name (CC),
6404 Etype (Comp),
6405 Esize (Comp),
6406 Biased);
6408 Set_Biased
6409 (Comp, First_Node (CC), "component clause", Biased);
6411 if Present (Ocomp) then
6412 Set_Component_Clause (Ocomp, CC);
6413 Set_Component_Bit_Offset (Ocomp, Fbit);
6414 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6415 Set_Normalized_Position (Ocomp, Fbit / SSU);
6416 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6418 Set_Normalized_Position_Max
6419 (Ocomp, Normalized_Position (Ocomp));
6421 -- Note: we don't use Set_Biased here, because we
6422 -- already gave a warning above if needed, and we
6423 -- would get a duplicate for the same name here.
6425 Set_Has_Biased_Representation
6426 (Ocomp, Has_Biased_Representation (Comp));
6427 end if;
6429 if Esize (Comp) < 0 then
6430 Error_Msg_N ("component size is negative", CC);
6431 end if;
6432 end if;
6433 end if;
6434 end if;
6435 end if;
6436 end if;
6438 Next (CC);
6439 end loop;
6441 -- Check missing components if Complete_Representation pragma appeared
6443 if Present (CR_Pragma) then
6444 Comp := First_Component_Or_Discriminant (Rectype);
6445 while Present (Comp) loop
6446 if No (Component_Clause (Comp)) then
6447 Error_Msg_NE
6448 ("missing component clause for &", CR_Pragma, Comp);
6449 end if;
6451 Next_Component_Or_Discriminant (Comp);
6452 end loop;
6454 -- Give missing components warning if required
6456 elsif Warn_On_Unrepped_Components then
6457 declare
6458 Num_Repped_Components : Nat := 0;
6459 Num_Unrepped_Components : Nat := 0;
6461 begin
6462 -- First count number of repped and unrepped components
6464 Comp := First_Component_Or_Discriminant (Rectype);
6465 while Present (Comp) loop
6466 if Present (Component_Clause (Comp)) then
6467 Num_Repped_Components := Num_Repped_Components + 1;
6468 else
6469 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6470 end if;
6472 Next_Component_Or_Discriminant (Comp);
6473 end loop;
6475 -- We are only interested in the case where there is at least one
6476 -- unrepped component, and at least half the components have rep
6477 -- clauses. We figure that if less than half have them, then the
6478 -- partial rep clause is really intentional. If the component
6479 -- type has no underlying type set at this point (as for a generic
6480 -- formal type), we don't know enough to give a warning on the
6481 -- component.
6483 if Num_Unrepped_Components > 0
6484 and then Num_Unrepped_Components < Num_Repped_Components
6485 then
6486 Comp := First_Component_Or_Discriminant (Rectype);
6487 while Present (Comp) loop
6488 if No (Component_Clause (Comp))
6489 and then Comes_From_Source (Comp)
6490 and then Present (Underlying_Type (Etype (Comp)))
6491 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6492 or else Size_Known_At_Compile_Time
6493 (Underlying_Type (Etype (Comp))))
6494 and then not Has_Warnings_Off (Rectype)
6495 then
6496 Error_Msg_Sloc := Sloc (Comp);
6497 Error_Msg_NE
6498 ("?C?no component clause given for & declared #",
6499 N, Comp);
6500 end if;
6502 Next_Component_Or_Discriminant (Comp);
6503 end loop;
6504 end if;
6505 end;
6506 end if;
6507 end Analyze_Record_Representation_Clause;
6509 -------------------------------------
6510 -- Build_Discrete_Static_Predicate --
6511 -------------------------------------
6513 procedure Build_Discrete_Static_Predicate
6514 (Typ : Entity_Id;
6515 Expr : Node_Id;
6516 Nam : Name_Id)
6518 Loc : constant Source_Ptr := Sloc (Expr);
6520 Non_Static : exception;
6521 -- Raised if something non-static is found
6523 Btyp : constant Entity_Id := Base_Type (Typ);
6525 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6526 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6527 -- Low bound and high bound value of base type of Typ
6529 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6530 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6531 -- Low bound and high bound values of static subtype Typ
6533 type REnt is record
6534 Lo, Hi : Uint;
6535 end record;
6536 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6537 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6538 -- value.
6540 type RList is array (Nat range <>) of REnt;
6541 -- A list of ranges. The ranges are sorted in increasing order, and are
6542 -- disjoint (there is a gap of at least one value between each range in
6543 -- the table). A value is in the set of ranges in Rlist if it lies
6544 -- within one of these ranges.
6546 False_Range : constant RList :=
6547 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6548 -- An empty set of ranges represents a range list that can never be
6549 -- satisfied, since there are no ranges in which the value could lie,
6550 -- so it does not lie in any of them. False_Range is a canonical value
6551 -- for this empty set, but general processing should test for an Rlist
6552 -- with length zero (see Is_False predicate), since other null ranges
6553 -- may appear which must be treated as False.
6555 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6556 -- Range representing True, value must be in the base range
6558 function "and" (Left : RList; Right : RList) return RList;
6559 -- And's together two range lists, returning a range list. This is a set
6560 -- intersection operation.
6562 function "or" (Left : RList; Right : RList) return RList;
6563 -- Or's together two range lists, returning a range list. This is a set
6564 -- union operation.
6566 function "not" (Right : RList) return RList;
6567 -- Returns complement of a given range list, i.e. a range list
6568 -- representing all the values in TLo .. THi that are not in the input
6569 -- operand Right.
6571 function Build_Val (V : Uint) return Node_Id;
6572 -- Return an analyzed N_Identifier node referencing this value, suitable
6573 -- for use as an entry in the Static_Discrte_Predicate list. This node
6574 -- is typed with the base type.
6576 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6577 -- Return an analyzed N_Range node referencing this range, suitable for
6578 -- use as an entry in the Static_Discrete_Predicate list. This node is
6579 -- typed with the base type.
6581 function Get_RList (Exp : Node_Id) return RList;
6582 -- This is a recursive routine that converts the given expression into a
6583 -- list of ranges, suitable for use in building the static predicate.
6585 function Is_False (R : RList) return Boolean;
6586 pragma Inline (Is_False);
6587 -- Returns True if the given range list is empty, and thus represents a
6588 -- False list of ranges that can never be satisfied.
6590 function Is_True (R : RList) return Boolean;
6591 -- Returns True if R trivially represents the True predicate by having a
6592 -- single range from BLo to BHi.
6594 function Is_Type_Ref (N : Node_Id) return Boolean;
6595 pragma Inline (Is_Type_Ref);
6596 -- Returns if True if N is a reference to the type for the predicate in
6597 -- the expression (i.e. if it is an identifier whose Chars field matches
6598 -- the Nam given in the call). N must not be parenthesized, if the type
6599 -- name appears in parens, this routine will return False.
6601 function Lo_Val (N : Node_Id) return Uint;
6602 -- Given an entry from a Static_Discrete_Predicate list that is either
6603 -- a static expression or static range, gets either the expression value
6604 -- or the low bound of the range.
6606 function Hi_Val (N : Node_Id) return Uint;
6607 -- Given an entry from a Static_Discrete_Predicate list that is either
6608 -- a static expression or static range, gets either the expression value
6609 -- or the high bound of the range.
6611 function Membership_Entry (N : Node_Id) return RList;
6612 -- Given a single membership entry (range, value, or subtype), returns
6613 -- the corresponding range list. Raises Static_Error if not static.
6615 function Membership_Entries (N : Node_Id) return RList;
6616 -- Given an element on an alternatives list of a membership operation,
6617 -- returns the range list corresponding to this entry and all following
6618 -- entries (i.e. returns the "or" of this list of values).
6620 function Stat_Pred (Typ : Entity_Id) return RList;
6621 -- Given a type, if it has a static predicate, then return the predicate
6622 -- as a range list, otherwise raise Non_Static.
6624 -----------
6625 -- "and" --
6626 -----------
6628 function "and" (Left : RList; Right : RList) return RList is
6629 FEnt : REnt;
6630 -- First range of result
6632 SLeft : Nat := Left'First;
6633 -- Start of rest of left entries
6635 SRight : Nat := Right'First;
6636 -- Start of rest of right entries
6638 begin
6639 -- If either range is True, return the other
6641 if Is_True (Left) then
6642 return Right;
6643 elsif Is_True (Right) then
6644 return Left;
6645 end if;
6647 -- If either range is False, return False
6649 if Is_False (Left) or else Is_False (Right) then
6650 return False_Range;
6651 end if;
6653 -- Loop to remove entries at start that are disjoint, and thus just
6654 -- get discarded from the result entirely.
6656 loop
6657 -- If no operands left in either operand, result is false
6659 if SLeft > Left'Last or else SRight > Right'Last then
6660 return False_Range;
6662 -- Discard first left operand entry if disjoint with right
6664 elsif Left (SLeft).Hi < Right (SRight).Lo then
6665 SLeft := SLeft + 1;
6667 -- Discard first right operand entry if disjoint with left
6669 elsif Right (SRight).Hi < Left (SLeft).Lo then
6670 SRight := SRight + 1;
6672 -- Otherwise we have an overlapping entry
6674 else
6675 exit;
6676 end if;
6677 end loop;
6679 -- Now we have two non-null operands, and first entries overlap. The
6680 -- first entry in the result will be the overlapping part of these
6681 -- two entries.
6683 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6684 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6686 -- Now we can remove the entry that ended at a lower value, since its
6687 -- contribution is entirely contained in Fent.
6689 if Left (SLeft).Hi <= Right (SRight).Hi then
6690 SLeft := SLeft + 1;
6691 else
6692 SRight := SRight + 1;
6693 end if;
6695 -- Compute result by concatenating this first entry with the "and" of
6696 -- the remaining parts of the left and right operands. Note that if
6697 -- either of these is empty, "and" will yield empty, so that we will
6698 -- end up with just Fent, which is what we want in that case.
6700 return
6701 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6702 end "and";
6704 -----------
6705 -- "not" --
6706 -----------
6708 function "not" (Right : RList) return RList is
6709 begin
6710 -- Return True if False range
6712 if Is_False (Right) then
6713 return True_Range;
6714 end if;
6716 -- Return False if True range
6718 if Is_True (Right) then
6719 return False_Range;
6720 end if;
6722 -- Here if not trivial case
6724 declare
6725 Result : RList (1 .. Right'Length + 1);
6726 -- May need one more entry for gap at beginning and end
6728 Count : Nat := 0;
6729 -- Number of entries stored in Result
6731 begin
6732 -- Gap at start
6734 if Right (Right'First).Lo > TLo then
6735 Count := Count + 1;
6736 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6737 end if;
6739 -- Gaps between ranges
6741 for J in Right'First .. Right'Last - 1 loop
6742 Count := Count + 1;
6743 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6744 end loop;
6746 -- Gap at end
6748 if Right (Right'Last).Hi < THi then
6749 Count := Count + 1;
6750 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6751 end if;
6753 return Result (1 .. Count);
6754 end;
6755 end "not";
6757 ----------
6758 -- "or" --
6759 ----------
6761 function "or" (Left : RList; Right : RList) return RList is
6762 FEnt : REnt;
6763 -- First range of result
6765 SLeft : Nat := Left'First;
6766 -- Start of rest of left entries
6768 SRight : Nat := Right'First;
6769 -- Start of rest of right entries
6771 begin
6772 -- If either range is True, return True
6774 if Is_True (Left) or else Is_True (Right) then
6775 return True_Range;
6776 end if;
6778 -- If either range is False (empty), return the other
6780 if Is_False (Left) then
6781 return Right;
6782 elsif Is_False (Right) then
6783 return Left;
6784 end if;
6786 -- Initialize result first entry from left or right operand depending
6787 -- on which starts with the lower range.
6789 if Left (SLeft).Lo < Right (SRight).Lo then
6790 FEnt := Left (SLeft);
6791 SLeft := SLeft + 1;
6792 else
6793 FEnt := Right (SRight);
6794 SRight := SRight + 1;
6795 end if;
6797 -- This loop eats ranges from left and right operands that are
6798 -- contiguous with the first range we are gathering.
6800 loop
6801 -- Eat first entry in left operand if contiguous or overlapped by
6802 -- gathered first operand of result.
6804 if SLeft <= Left'Last
6805 and then Left (SLeft).Lo <= FEnt.Hi + 1
6806 then
6807 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6808 SLeft := SLeft + 1;
6810 -- Eat first entry in right operand if contiguous or overlapped by
6811 -- gathered right operand of result.
6813 elsif SRight <= Right'Last
6814 and then Right (SRight).Lo <= FEnt.Hi + 1
6815 then
6816 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6817 SRight := SRight + 1;
6819 -- All done if no more entries to eat
6821 else
6822 exit;
6823 end if;
6824 end loop;
6826 -- Obtain result as the first entry we just computed, concatenated
6827 -- to the "or" of the remaining results (if one operand is empty,
6828 -- this will just concatenate with the other
6830 return
6831 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6832 end "or";
6834 -----------------
6835 -- Build_Range --
6836 -----------------
6838 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6839 Result : Node_Id;
6840 begin
6841 Result :=
6842 Make_Range (Loc,
6843 Low_Bound => Build_Val (Lo),
6844 High_Bound => Build_Val (Hi));
6845 Set_Etype (Result, Btyp);
6846 Set_Analyzed (Result);
6847 return Result;
6848 end Build_Range;
6850 ---------------
6851 -- Build_Val --
6852 ---------------
6854 function Build_Val (V : Uint) return Node_Id is
6855 Result : Node_Id;
6857 begin
6858 if Is_Enumeration_Type (Typ) then
6859 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6860 else
6861 Result := Make_Integer_Literal (Loc, V);
6862 end if;
6864 Set_Etype (Result, Btyp);
6865 Set_Is_Static_Expression (Result);
6866 Set_Analyzed (Result);
6867 return Result;
6868 end Build_Val;
6870 ---------------
6871 -- Get_RList --
6872 ---------------
6874 function Get_RList (Exp : Node_Id) return RList is
6875 Op : Node_Kind;
6876 Val : Uint;
6878 begin
6879 -- Static expression can only be true or false
6881 if Is_OK_Static_Expression (Exp) then
6882 if Expr_Value (Exp) = 0 then
6883 return False_Range;
6884 else
6885 return True_Range;
6886 end if;
6887 end if;
6889 -- Otherwise test node type
6891 Op := Nkind (Exp);
6893 case Op is
6895 -- And
6897 when N_Op_And | N_And_Then =>
6898 return Get_RList (Left_Opnd (Exp))
6900 Get_RList (Right_Opnd (Exp));
6902 -- Or
6904 when N_Op_Or | N_Or_Else =>
6905 return Get_RList (Left_Opnd (Exp))
6907 Get_RList (Right_Opnd (Exp));
6909 -- Not
6911 when N_Op_Not =>
6912 return not Get_RList (Right_Opnd (Exp));
6914 -- Comparisons of type with static value
6916 when N_Op_Compare =>
6918 -- Type is left operand
6920 if Is_Type_Ref (Left_Opnd (Exp))
6921 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6922 then
6923 Val := Expr_Value (Right_Opnd (Exp));
6925 -- Typ is right operand
6927 elsif Is_Type_Ref (Right_Opnd (Exp))
6928 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6929 then
6930 Val := Expr_Value (Left_Opnd (Exp));
6932 -- Invert sense of comparison
6934 case Op is
6935 when N_Op_Gt => Op := N_Op_Lt;
6936 when N_Op_Lt => Op := N_Op_Gt;
6937 when N_Op_Ge => Op := N_Op_Le;
6938 when N_Op_Le => Op := N_Op_Ge;
6939 when others => null;
6940 end case;
6942 -- Other cases are non-static
6944 else
6945 raise Non_Static;
6946 end if;
6948 -- Construct range according to comparison operation
6950 case Op is
6951 when N_Op_Eq =>
6952 return RList'(1 => REnt'(Val, Val));
6954 when N_Op_Ge =>
6955 return RList'(1 => REnt'(Val, BHi));
6957 when N_Op_Gt =>
6958 return RList'(1 => REnt'(Val + 1, BHi));
6960 when N_Op_Le =>
6961 return RList'(1 => REnt'(BLo, Val));
6963 when N_Op_Lt =>
6964 return RList'(1 => REnt'(BLo, Val - 1));
6966 when N_Op_Ne =>
6967 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
6969 when others =>
6970 raise Program_Error;
6971 end case;
6973 -- Membership (IN)
6975 when N_In =>
6976 if not Is_Type_Ref (Left_Opnd (Exp)) then
6977 raise Non_Static;
6978 end if;
6980 if Present (Right_Opnd (Exp)) then
6981 return Membership_Entry (Right_Opnd (Exp));
6982 else
6983 return Membership_Entries (First (Alternatives (Exp)));
6984 end if;
6986 -- Negative membership (NOT IN)
6988 when N_Not_In =>
6989 if not Is_Type_Ref (Left_Opnd (Exp)) then
6990 raise Non_Static;
6991 end if;
6993 if Present (Right_Opnd (Exp)) then
6994 return not Membership_Entry (Right_Opnd (Exp));
6995 else
6996 return not Membership_Entries (First (Alternatives (Exp)));
6997 end if;
6999 -- Function call, may be call to static predicate
7001 when N_Function_Call =>
7002 if Is_Entity_Name (Name (Exp)) then
7003 declare
7004 Ent : constant Entity_Id := Entity (Name (Exp));
7005 begin
7006 if Is_Predicate_Function (Ent)
7007 or else
7008 Is_Predicate_Function_M (Ent)
7009 then
7010 return Stat_Pred (Etype (First_Formal (Ent)));
7011 end if;
7012 end;
7013 end if;
7015 -- Other function call cases are non-static
7017 raise Non_Static;
7019 -- Qualified expression, dig out the expression
7021 when N_Qualified_Expression =>
7022 return Get_RList (Expression (Exp));
7024 when N_Case_Expression =>
7025 declare
7026 Alt : Node_Id;
7027 Choices : List_Id;
7028 Dep : Node_Id;
7030 begin
7031 if not Is_Entity_Name (Expression (Expr))
7032 or else Etype (Expression (Expr)) /= Typ
7033 then
7034 Error_Msg_N
7035 ("expression must denaote subtype", Expression (Expr));
7036 return False_Range;
7037 end if;
7039 -- Collect discrete choices in all True alternatives
7041 Choices := New_List;
7042 Alt := First (Alternatives (Exp));
7043 while Present (Alt) loop
7044 Dep := Expression (Alt);
7046 if not Is_OK_Static_Expression (Dep) then
7047 raise Non_Static;
7049 elsif Is_True (Expr_Value (Dep)) then
7050 Append_List_To (Choices,
7051 New_Copy_List (Discrete_Choices (Alt)));
7052 end if;
7054 Next (Alt);
7055 end loop;
7057 return Membership_Entries (First (Choices));
7058 end;
7060 -- Expression with actions: if no actions, dig out expression
7062 when N_Expression_With_Actions =>
7063 if Is_Empty_List (Actions (Exp)) then
7064 return Get_RList (Expression (Exp));
7065 else
7066 raise Non_Static;
7067 end if;
7069 -- Xor operator
7071 when N_Op_Xor =>
7072 return (Get_RList (Left_Opnd (Exp))
7073 and not Get_RList (Right_Opnd (Exp)))
7074 or (Get_RList (Right_Opnd (Exp))
7075 and not Get_RList (Left_Opnd (Exp)));
7077 -- Any other node type is non-static
7079 when others =>
7080 raise Non_Static;
7081 end case;
7082 end Get_RList;
7084 ------------
7085 -- Hi_Val --
7086 ------------
7088 function Hi_Val (N : Node_Id) return Uint is
7089 begin
7090 if Is_OK_Static_Expression (N) then
7091 return Expr_Value (N);
7092 else
7093 pragma Assert (Nkind (N) = N_Range);
7094 return Expr_Value (High_Bound (N));
7095 end if;
7096 end Hi_Val;
7098 --------------
7099 -- Is_False --
7100 --------------
7102 function Is_False (R : RList) return Boolean is
7103 begin
7104 return R'Length = 0;
7105 end Is_False;
7107 -------------
7108 -- Is_True --
7109 -------------
7111 function Is_True (R : RList) return Boolean is
7112 begin
7113 return R'Length = 1
7114 and then R (R'First).Lo = BLo
7115 and then R (R'First).Hi = BHi;
7116 end Is_True;
7118 -----------------
7119 -- Is_Type_Ref --
7120 -----------------
7122 function Is_Type_Ref (N : Node_Id) return Boolean is
7123 begin
7124 return Nkind (N) = N_Identifier
7125 and then Chars (N) = Nam
7126 and then Paren_Count (N) = 0;
7127 end Is_Type_Ref;
7129 ------------
7130 -- Lo_Val --
7131 ------------
7133 function Lo_Val (N : Node_Id) return Uint is
7134 begin
7135 if Is_OK_Static_Expression (N) then
7136 return Expr_Value (N);
7137 else
7138 pragma Assert (Nkind (N) = N_Range);
7139 return Expr_Value (Low_Bound (N));
7140 end if;
7141 end Lo_Val;
7143 ------------------------
7144 -- Membership_Entries --
7145 ------------------------
7147 function Membership_Entries (N : Node_Id) return RList is
7148 begin
7149 if No (Next (N)) then
7150 return Membership_Entry (N);
7151 else
7152 return Membership_Entry (N) or Membership_Entries (Next (N));
7153 end if;
7154 end Membership_Entries;
7156 ----------------------
7157 -- Membership_Entry --
7158 ----------------------
7160 function Membership_Entry (N : Node_Id) return RList is
7161 Val : Uint;
7162 SLo : Uint;
7163 SHi : Uint;
7165 begin
7166 -- Range case
7168 if Nkind (N) = N_Range then
7169 if not Is_OK_Static_Expression (Low_Bound (N))
7170 or else
7171 not Is_OK_Static_Expression (High_Bound (N))
7172 then
7173 raise Non_Static;
7174 else
7175 SLo := Expr_Value (Low_Bound (N));
7176 SHi := Expr_Value (High_Bound (N));
7177 return RList'(1 => REnt'(SLo, SHi));
7178 end if;
7180 -- Static expression case
7182 elsif Is_OK_Static_Expression (N) then
7183 Val := Expr_Value (N);
7184 return RList'(1 => REnt'(Val, Val));
7186 -- Identifier (other than static expression) case
7188 else pragma Assert (Nkind (N) = N_Identifier);
7190 -- Type case
7192 if Is_Type (Entity (N)) then
7194 -- If type has predicates, process them
7196 if Has_Predicates (Entity (N)) then
7197 return Stat_Pred (Entity (N));
7199 -- For static subtype without predicates, get range
7201 elsif Is_OK_Static_Subtype (Entity (N)) then
7202 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7203 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7204 return RList'(1 => REnt'(SLo, SHi));
7206 -- Any other type makes us non-static
7208 else
7209 raise Non_Static;
7210 end if;
7212 -- Any other kind of identifier in predicate (e.g. a non-static
7213 -- expression value) means this is not a static predicate.
7215 else
7216 raise Non_Static;
7217 end if;
7218 end if;
7219 end Membership_Entry;
7221 ---------------
7222 -- Stat_Pred --
7223 ---------------
7225 function Stat_Pred (Typ : Entity_Id) return RList is
7226 begin
7227 -- Not static if type does not have static predicates
7229 if not Has_Static_Predicate (Typ) then
7230 raise Non_Static;
7231 end if;
7233 -- Otherwise we convert the predicate list to a range list
7235 declare
7236 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7237 Result : RList (1 .. List_Length (Spred));
7238 P : Node_Id;
7240 begin
7241 P := First (Static_Discrete_Predicate (Typ));
7242 for J in Result'Range loop
7243 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7244 Next (P);
7245 end loop;
7247 return Result;
7248 end;
7249 end Stat_Pred;
7251 -- Start of processing for Build_Discrete_Static_Predicate
7253 begin
7254 -- Analyze the expression to see if it is a static predicate
7256 declare
7257 Ranges : constant RList := Get_RList (Expr);
7258 -- Range list from expression if it is static
7260 Plist : List_Id;
7262 begin
7263 -- Convert range list into a form for the static predicate. In the
7264 -- Ranges array, we just have raw ranges, these must be converted
7265 -- to properly typed and analyzed static expressions or range nodes.
7267 -- Note: here we limit ranges to the ranges of the subtype, so that
7268 -- a predicate is always false for values outside the subtype. That
7269 -- seems fine, such values are invalid anyway, and considering them
7270 -- to fail the predicate seems allowed and friendly, and furthermore
7271 -- simplifies processing for case statements and loops.
7273 Plist := New_List;
7275 for J in Ranges'Range loop
7276 declare
7277 Lo : Uint := Ranges (J).Lo;
7278 Hi : Uint := Ranges (J).Hi;
7280 begin
7281 -- Ignore completely out of range entry
7283 if Hi < TLo or else Lo > THi then
7284 null;
7286 -- Otherwise process entry
7288 else
7289 -- Adjust out of range value to subtype range
7291 if Lo < TLo then
7292 Lo := TLo;
7293 end if;
7295 if Hi > THi then
7296 Hi := THi;
7297 end if;
7299 -- Convert range into required form
7301 Append_To (Plist, Build_Range (Lo, Hi));
7302 end if;
7303 end;
7304 end loop;
7306 -- Processing was successful and all entries were static, so now we
7307 -- can store the result as the predicate list.
7309 Set_Static_Discrete_Predicate (Typ, Plist);
7311 -- The processing for static predicates put the expression into
7312 -- canonical form as a series of ranges. It also eliminated
7313 -- duplicates and collapsed and combined ranges. We might as well
7314 -- replace the alternatives list of the right operand of the
7315 -- membership test with the static predicate list, which will
7316 -- usually be more efficient.
7318 declare
7319 New_Alts : constant List_Id := New_List;
7320 Old_Node : Node_Id;
7321 New_Node : Node_Id;
7323 begin
7324 Old_Node := First (Plist);
7325 while Present (Old_Node) loop
7326 New_Node := New_Copy (Old_Node);
7328 if Nkind (New_Node) = N_Range then
7329 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7330 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7331 end if;
7333 Append_To (New_Alts, New_Node);
7334 Next (Old_Node);
7335 end loop;
7337 -- If empty list, replace by False
7339 if Is_Empty_List (New_Alts) then
7340 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7342 -- Else replace by set membership test
7344 else
7345 Rewrite (Expr,
7346 Make_In (Loc,
7347 Left_Opnd => Make_Identifier (Loc, Nam),
7348 Right_Opnd => Empty,
7349 Alternatives => New_Alts));
7351 -- Resolve new expression in function context
7353 Install_Formals (Predicate_Function (Typ));
7354 Push_Scope (Predicate_Function (Typ));
7355 Analyze_And_Resolve (Expr, Standard_Boolean);
7356 Pop_Scope;
7357 end if;
7358 end;
7359 end;
7361 -- If non-static, return doing nothing
7363 exception
7364 when Non_Static =>
7365 return;
7366 end Build_Discrete_Static_Predicate;
7368 -------------------------------------------
7369 -- Build_Invariant_Procedure_Declaration --
7370 -------------------------------------------
7372 function Build_Invariant_Procedure_Declaration
7373 (Typ : Entity_Id) return Node_Id
7375 Loc : constant Source_Ptr := Sloc (Typ);
7376 Object_Entity : constant Entity_Id :=
7377 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7378 Spec : Node_Id;
7379 SId : Entity_Id;
7381 begin
7382 Set_Etype (Object_Entity, Typ);
7384 -- Check for duplicate definiations.
7386 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7387 return Empty;
7388 end if;
7390 SId :=
7391 Make_Defining_Identifier (Loc,
7392 Chars => New_External_Name (Chars (Typ), "Invariant"));
7393 Set_Has_Invariants (Typ);
7394 Set_Ekind (SId, E_Procedure);
7395 Set_Etype (SId, Standard_Void_Type);
7396 Set_Is_Invariant_Procedure (SId);
7397 Set_Invariant_Procedure (Typ, SId);
7399 Spec :=
7400 Make_Procedure_Specification (Loc,
7401 Defining_Unit_Name => SId,
7402 Parameter_Specifications => New_List (
7403 Make_Parameter_Specification (Loc,
7404 Defining_Identifier => Object_Entity,
7405 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7407 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7408 end Build_Invariant_Procedure_Declaration;
7410 -------------------------------
7411 -- Build_Invariant_Procedure --
7412 -------------------------------
7414 -- The procedure that is constructed here has the form
7416 -- procedure typInvariant (Ixxx : typ) is
7417 -- begin
7418 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7419 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7420 -- ...
7421 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7422 -- ...
7423 -- end typInvariant;
7425 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7426 Loc : constant Source_Ptr := Sloc (Typ);
7427 Stmts : List_Id;
7428 Spec : Node_Id;
7429 SId : Entity_Id;
7430 PDecl : Node_Id;
7431 PBody : Node_Id;
7433 Nam : Name_Id;
7434 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7435 -- if we come from a Type_Invariant aspect, we make sure to build the
7436 -- Check pragma with the right name, so that Check_Policy works right.
7438 Visible_Decls : constant List_Id := Visible_Declarations (N);
7439 Private_Decls : constant List_Id := Private_Declarations (N);
7441 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7442 -- Appends statements to Stmts for any invariants in the rep item chain
7443 -- of the given type. If Inherit is False, then we only process entries
7444 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7445 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7446 -- "inherited" to the exception message and generating an informational
7447 -- message about the inheritance of an invariant.
7449 Object_Name : Name_Id;
7450 -- Name for argument of invariant procedure
7452 Object_Entity : Node_Id;
7453 -- The entity of the formal for the procedure
7455 --------------------
7456 -- Add_Invariants --
7457 --------------------
7459 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7460 Ritem : Node_Id;
7461 Arg1 : Node_Id;
7462 Arg2 : Node_Id;
7463 Arg3 : Node_Id;
7464 Exp : Node_Id;
7465 Loc : Source_Ptr;
7466 Assoc : List_Id;
7467 Str : String_Id;
7469 procedure Replace_Type_Reference (N : Node_Id);
7470 -- Replace a single occurrence N of the subtype name with a reference
7471 -- to the formal of the predicate function. N can be an identifier
7472 -- referencing the subtype, or a selected component, representing an
7473 -- appropriately qualified occurrence of the subtype name.
7475 procedure Replace_Type_References is
7476 new Replace_Type_References_Generic (Replace_Type_Reference);
7477 -- Traverse an expression replacing all occurrences of the subtype
7478 -- name with appropriate references to the object that is the formal
7479 -- parameter of the predicate function. Note that we must ensure
7480 -- that the type and entity information is properly set in the
7481 -- replacement node, since we will do a Preanalyze call of this
7482 -- expression without proper visibility of the procedure argument.
7484 ----------------------------
7485 -- Replace_Type_Reference --
7486 ----------------------------
7488 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7489 -- regarding handling of Sloc and Comes_From_Source.
7491 procedure Replace_Type_Reference (N : Node_Id) is
7492 begin
7494 -- Add semantic information to node to be rewritten, for ASIS
7495 -- navigation needs.
7497 if Nkind (N) = N_Identifier then
7498 Set_Entity (N, T);
7499 Set_Etype (N, T);
7501 elsif Nkind (N) = N_Selected_Component then
7502 Analyze (Prefix (N));
7503 Set_Entity (Selector_Name (N), T);
7504 Set_Etype (Selector_Name (N), T);
7505 end if;
7507 -- Invariant'Class, replace with T'Class (obj)
7508 -- In ASIS mode, an inherited item is analyzed already, and the
7509 -- replacement has been done, so do not repeat transformation
7510 -- to prevent ill-formed tree.
7512 if Class_Present (Ritem) then
7513 if ASIS_Mode
7514 and then Nkind (Parent (N)) = N_Attribute_Reference
7515 and then Attribute_Name (Parent (N)) = Name_Class
7516 then
7517 null;
7519 else
7520 Rewrite (N,
7521 Make_Type_Conversion (Sloc (N),
7522 Subtype_Mark =>
7523 Make_Attribute_Reference (Sloc (N),
7524 Prefix => New_Occurrence_Of (T, Sloc (N)),
7525 Attribute_Name => Name_Class),
7526 Expression =>
7527 Make_Identifier (Sloc (N), Object_Name)));
7529 Set_Entity (Expression (N), Object_Entity);
7530 Set_Etype (Expression (N), Typ);
7531 end if;
7533 -- Invariant, replace with obj
7535 else
7536 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7537 Set_Entity (N, Object_Entity);
7538 Set_Etype (N, Typ);
7539 end if;
7541 Set_Comes_From_Source (N, True);
7542 end Replace_Type_Reference;
7544 -- Start of processing for Add_Invariants
7546 begin
7547 Ritem := First_Rep_Item (T);
7548 while Present (Ritem) loop
7549 if Nkind (Ritem) = N_Pragma
7550 and then Pragma_Name (Ritem) = Name_Invariant
7551 then
7552 Arg1 := First (Pragma_Argument_Associations (Ritem));
7553 Arg2 := Next (Arg1);
7554 Arg3 := Next (Arg2);
7556 Arg1 := Get_Pragma_Arg (Arg1);
7557 Arg2 := Get_Pragma_Arg (Arg2);
7559 -- For Inherit case, ignore Invariant, process only Class case
7561 if Inherit then
7562 if not Class_Present (Ritem) then
7563 goto Continue;
7564 end if;
7566 -- For Inherit false, process only item for right type
7568 else
7569 if Entity (Arg1) /= Typ then
7570 goto Continue;
7571 end if;
7572 end if;
7574 if No (Stmts) then
7575 Stmts := Empty_List;
7576 end if;
7578 Exp := New_Copy_Tree (Arg2);
7580 -- Preserve sloc of original pragma Invariant
7582 Loc := Sloc (Ritem);
7584 -- We need to replace any occurrences of the name of the type
7585 -- with references to the object, converted to type'Class in
7586 -- the case of Invariant'Class aspects.
7588 Replace_Type_References (Exp, T);
7590 -- If this invariant comes from an aspect, find the aspect
7591 -- specification, and replace the saved expression because
7592 -- we need the subtype references replaced for the calls to
7593 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7594 -- and Check_Aspect_At_End_Of_Declarations.
7596 if From_Aspect_Specification (Ritem) then
7597 declare
7598 Aitem : Node_Id;
7600 begin
7601 -- Loop to find corresponding aspect, note that this
7602 -- must be present given the pragma is marked delayed.
7604 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7605 -- this loop does nothing. Furthermore, why isn't this
7606 -- simply Corresponding_Aspect ???
7608 Aitem := Next_Rep_Item (Ritem);
7609 while Present (Aitem) loop
7610 if Nkind (Aitem) = N_Aspect_Specification
7611 and then Aspect_Rep_Item (Aitem) = Ritem
7612 then
7613 Set_Entity
7614 (Identifier (Aitem), New_Copy_Tree (Exp));
7615 exit;
7616 end if;
7618 Aitem := Next_Rep_Item (Aitem);
7619 end loop;
7620 end;
7621 end if;
7623 -- Now we need to preanalyze the expression to properly capture
7624 -- the visibility in the visible part. The expression will not
7625 -- be analyzed for real until the body is analyzed, but that is
7626 -- at the end of the private part and has the wrong visibility.
7628 Set_Parent (Exp, N);
7629 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
7631 -- A class-wide invariant may be inherited in a separate unit,
7632 -- where the corresponding expression cannot be resolved by
7633 -- visibility, because it refers to a local function. Propagate
7634 -- semantic information to the original representation item, to
7635 -- be used when an invariant procedure for a derived type is
7636 -- constructed.
7638 -- Unclear how to handle class-wide invariants that are not
7639 -- function calls ???
7641 if not Inherit
7642 and then Class_Present (Ritem)
7643 and then Nkind (Exp) = N_Function_Call
7644 and then Nkind (Arg2) = N_Indexed_Component
7645 then
7646 Rewrite (Arg2,
7647 Make_Function_Call (Loc,
7648 Name =>
7649 New_Occurrence_Of (Entity (Name (Exp)), Loc),
7650 Parameter_Associations =>
7651 New_Copy_List (Expressions (Arg2))));
7652 end if;
7654 -- In ASIS mode, even if assertions are not enabled, we must
7655 -- analyze the original expression in the aspect specification
7656 -- because it is part of the original tree.
7658 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7659 declare
7660 Inv : constant Node_Id :=
7661 Expression (Corresponding_Aspect (Ritem));
7662 begin
7663 Replace_Type_References (Inv, T);
7664 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7665 end;
7666 end if;
7668 -- Get name to be used for Check pragma
7670 if not From_Aspect_Specification (Ritem) then
7671 Nam := Name_Invariant;
7672 else
7673 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7674 end if;
7676 -- Build first two arguments for Check pragma
7678 Assoc :=
7679 New_List (
7680 Make_Pragma_Argument_Association (Loc,
7681 Expression => Make_Identifier (Loc, Chars => Nam)),
7682 Make_Pragma_Argument_Association (Loc,
7683 Expression => Exp));
7685 -- Add message if present in Invariant pragma
7687 if Present (Arg3) then
7688 Str := Strval (Get_Pragma_Arg (Arg3));
7690 -- If inherited case, and message starts "failed invariant",
7691 -- change it to be "failed inherited invariant".
7693 if Inherit then
7694 String_To_Name_Buffer (Str);
7696 if Name_Buffer (1 .. 16) = "failed invariant" then
7697 Insert_Str_In_Name_Buffer ("inherited ", 8);
7698 Str := String_From_Name_Buffer;
7699 end if;
7700 end if;
7702 Append_To (Assoc,
7703 Make_Pragma_Argument_Association (Loc,
7704 Expression => Make_String_Literal (Loc, Str)));
7705 end if;
7707 -- Add Check pragma to list of statements
7709 Append_To (Stmts,
7710 Make_Pragma (Loc,
7711 Pragma_Identifier =>
7712 Make_Identifier (Loc, Name_Check),
7713 Pragma_Argument_Associations => Assoc));
7715 -- If Inherited case and option enabled, output info msg. Note
7716 -- that we know this is a case of Invariant'Class.
7718 if Inherit and Opt.List_Inherited_Aspects then
7719 Error_Msg_Sloc := Sloc (Ritem);
7720 Error_Msg_N
7721 ("info: & inherits `Invariant''Class` aspect from #?L?",
7722 Typ);
7723 end if;
7724 end if;
7726 <<Continue>>
7727 Next_Rep_Item (Ritem);
7728 end loop;
7729 end Add_Invariants;
7731 -- Start of processing for Build_Invariant_Procedure
7733 begin
7734 Stmts := No_List;
7735 PDecl := Empty;
7736 PBody := Empty;
7737 SId := Empty;
7739 -- If the aspect specification exists for some view of the type, the
7740 -- declaration for the procedure has been created.
7742 if Has_Invariants (Typ) then
7743 SId := Invariant_Procedure (Typ);
7744 end if;
7746 -- If the body is already present, nothing to do. This will occur when
7747 -- the type is already frozen, which is the case when the invariant
7748 -- appears in a private part, and the freezing takes place before the
7749 -- final pass over full declarations.
7751 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
7753 if Present (SId) then
7754 PDecl := Unit_Declaration_Node (SId);
7756 if Present (PDecl)
7757 and then Nkind (PDecl) = N_Subprogram_Declaration
7758 and then Present (Corresponding_Body (PDecl))
7759 then
7760 return;
7761 end if;
7763 else
7764 PDecl := Build_Invariant_Procedure_Declaration (Typ);
7765 end if;
7767 -- Recover formal of procedure, for use in the calls to invariant
7768 -- functions (including inherited ones).
7770 Object_Entity :=
7771 Defining_Identifier
7772 (First (Parameter_Specifications (Specification (PDecl))));
7773 Object_Name := Chars (Object_Entity);
7775 -- Add invariants for the current type
7777 Add_Invariants (Typ, Inherit => False);
7779 -- Add invariants for parent types
7781 declare
7782 Current_Typ : Entity_Id;
7783 Parent_Typ : Entity_Id;
7785 begin
7786 Current_Typ := Typ;
7787 loop
7788 Parent_Typ := Etype (Current_Typ);
7790 if Is_Private_Type (Parent_Typ)
7791 and then Present (Full_View (Base_Type (Parent_Typ)))
7792 then
7793 Parent_Typ := Full_View (Base_Type (Parent_Typ));
7794 end if;
7796 exit when Parent_Typ = Current_Typ;
7798 Current_Typ := Parent_Typ;
7799 Add_Invariants (Current_Typ, Inherit => True);
7800 end loop;
7801 end;
7803 -- Build the procedure if we generated at least one Check pragma
7805 if Stmts /= No_List then
7806 Spec := Copy_Separate_Tree (Specification (PDecl));
7808 PBody :=
7809 Make_Subprogram_Body (Loc,
7810 Specification => Spec,
7811 Declarations => Empty_List,
7812 Handled_Statement_Sequence =>
7813 Make_Handled_Sequence_Of_Statements (Loc,
7814 Statements => Stmts));
7816 -- Insert procedure declaration and spec at the appropriate points.
7817 -- If declaration is already analyzed, it was processed by the
7818 -- generated pragma.
7820 if Present (Private_Decls) then
7822 -- The spec goes at the end of visible declarations, but they have
7823 -- already been analyzed, so we need to explicitly do the analyze.
7825 if not Analyzed (PDecl) then
7826 Append_To (Visible_Decls, PDecl);
7827 Analyze (PDecl);
7828 end if;
7830 -- The body goes at the end of the private declarations, which we
7831 -- have not analyzed yet, so we do not need to perform an explicit
7832 -- analyze call. We skip this if there are no private declarations
7833 -- (this is an error that will be caught elsewhere);
7835 Append_To (Private_Decls, PBody);
7837 -- If the invariant appears on the full view of a type, the
7838 -- analysis of the private part is complete, and we must
7839 -- analyze the new body explicitly.
7841 if In_Private_Part (Current_Scope) then
7842 Analyze (PBody);
7843 end if;
7845 -- If there are no private declarations this may be an error that
7846 -- will be diagnosed elsewhere. However, if this is a non-private
7847 -- type that inherits invariants, it needs no completion and there
7848 -- may be no private part. In this case insert invariant procedure
7849 -- at end of current declarative list, and analyze at once, given
7850 -- that the type is about to be frozen.
7852 elsif not Is_Private_Type (Typ) then
7853 Append_To (Visible_Decls, PDecl);
7854 Append_To (Visible_Decls, PBody);
7855 Analyze (PDecl);
7856 Analyze (PBody);
7857 end if;
7858 end if;
7859 end Build_Invariant_Procedure;
7861 -------------------------------
7862 -- Build_Predicate_Functions --
7863 -------------------------------
7865 -- The procedures that are constructed here have the form:
7867 -- function typPredicate (Ixxx : typ) return Boolean is
7868 -- begin
7869 -- return
7870 -- exp1 and then exp2 and then ...
7871 -- and then typ1Predicate (typ1 (Ixxx))
7872 -- and then typ2Predicate (typ2 (Ixxx))
7873 -- and then ...;
7874 -- end typPredicate;
7876 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
7877 -- this is the point at which these expressions get analyzed, providing the
7878 -- required delay, and typ1, typ2, are entities from which predicates are
7879 -- inherited. Note that we do NOT generate Check pragmas, that's because we
7880 -- use this function even if checks are off, e.g. for membership tests.
7882 -- If the expression has at least one Raise_Expression, then we also build
7883 -- the typPredicateM version of the function, in which any occurrence of a
7884 -- Raise_Expression is converted to "return False".
7886 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
7887 Loc : constant Source_Ptr := Sloc (Typ);
7889 Expr : Node_Id;
7890 -- This is the expression for the result of the function. It is
7891 -- is build by connecting the component predicates with AND THEN.
7893 Expr_M : Node_Id;
7894 -- This is the corresponding return expression for the Predicate_M
7895 -- function. It differs in that raise expressions are marked for
7896 -- special expansion (see Process_REs).
7898 Object_Name : constant Name_Id := New_Internal_Name ('I');
7899 -- Name for argument of Predicate procedure. Note that we use the same
7900 -- name for both predicate functions. That way the reference within the
7901 -- predicate expression is the same in both functions.
7903 Object_Entity : constant Entity_Id :=
7904 Make_Defining_Identifier (Loc, Chars => Object_Name);
7905 -- Entity for argument of Predicate procedure
7907 Object_Entity_M : constant Entity_Id :=
7908 Make_Defining_Identifier (Loc, Chars => Object_Name);
7909 -- Entity for argument of Predicate_M procedure
7911 Raise_Expression_Present : Boolean := False;
7912 -- Set True if Expr has at least one Raise_Expression
7914 procedure Add_Call (T : Entity_Id);
7915 -- Includes a call to the predicate function for type T in Expr if T
7916 -- has predicates and Predicate_Function (T) is non-empty.
7918 procedure Add_Predicates;
7919 -- Appends expressions for any Predicate pragmas in the rep item chain
7920 -- Typ to Expr. Note that we look only at items for this exact entity.
7921 -- Inheritance of predicates for the parent type is done by calling the
7922 -- Predicate_Function of the parent type, using Add_Call above.
7924 function Test_RE (N : Node_Id) return Traverse_Result;
7925 -- Used in Test_REs, tests one node for being a raise expression, and if
7926 -- so sets Raise_Expression_Present True.
7928 procedure Test_REs is new Traverse_Proc (Test_RE);
7929 -- Tests to see if Expr contains any raise expressions
7931 function Process_RE (N : Node_Id) return Traverse_Result;
7932 -- Used in Process REs, tests if node N is a raise expression, and if
7933 -- so, marks it to be converted to return False.
7935 procedure Process_REs is new Traverse_Proc (Process_RE);
7936 -- Marks any raise expressions in Expr_M to return False
7938 --------------
7939 -- Add_Call --
7940 --------------
7942 procedure Add_Call (T : Entity_Id) is
7943 Exp : Node_Id;
7945 begin
7946 if Present (T) and then Present (Predicate_Function (T)) then
7947 Set_Has_Predicates (Typ);
7949 -- Build the call to the predicate function of T
7951 Exp :=
7952 Make_Predicate_Call
7953 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
7955 -- Add call to evolving expression, using AND THEN if needed
7957 if No (Expr) then
7958 Expr := Exp;
7960 else
7961 Expr :=
7962 Make_And_Then (Sloc (Expr),
7963 Left_Opnd => Relocate_Node (Expr),
7964 Right_Opnd => Exp);
7965 end if;
7967 -- Output info message on inheritance if required. Note we do not
7968 -- give this information for generic actual types, since it is
7969 -- unwelcome noise in that case in instantiations. We also
7970 -- generally suppress the message in instantiations, and also
7971 -- if it involves internal names.
7973 if Opt.List_Inherited_Aspects
7974 and then not Is_Generic_Actual_Type (Typ)
7975 and then Instantiation_Depth (Sloc (Typ)) = 0
7976 and then not Is_Internal_Name (Chars (T))
7977 and then not Is_Internal_Name (Chars (Typ))
7978 then
7979 Error_Msg_Sloc := Sloc (Predicate_Function (T));
7980 Error_Msg_Node_2 := T;
7981 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
7982 end if;
7983 end if;
7984 end Add_Call;
7986 --------------------
7987 -- Add_Predicates --
7988 --------------------
7990 procedure Add_Predicates is
7991 Ritem : Node_Id;
7992 Arg1 : Node_Id;
7993 Arg2 : Node_Id;
7995 procedure Replace_Type_Reference (N : Node_Id);
7996 -- Replace a single occurrence N of the subtype name with a reference
7997 -- to the formal of the predicate function. N can be an identifier
7998 -- referencing the subtype, or a selected component, representing an
7999 -- appropriately qualified occurrence of the subtype name.
8001 procedure Replace_Type_References is
8002 new Replace_Type_References_Generic (Replace_Type_Reference);
8003 -- Traverse an expression changing every occurrence of an identifier
8004 -- whose name matches the name of the subtype with a reference to
8005 -- the formal parameter of the predicate function.
8007 ----------------------------
8008 -- Replace_Type_Reference --
8009 ----------------------------
8011 procedure Replace_Type_Reference (N : Node_Id) is
8012 begin
8013 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8014 -- Use the Sloc of the usage name, not the defining name
8016 Set_Etype (N, Typ);
8017 Set_Entity (N, Object_Entity);
8019 -- We want to treat the node as if it comes from source, so that
8020 -- ASIS will not ignore it
8022 Set_Comes_From_Source (N, True);
8023 end Replace_Type_Reference;
8025 -- Start of processing for Add_Predicates
8027 begin
8028 Ritem := First_Rep_Item (Typ);
8029 while Present (Ritem) loop
8030 if Nkind (Ritem) = N_Pragma
8031 and then Pragma_Name (Ritem) = Name_Predicate
8032 then
8033 -- Acquire arguments
8035 Arg1 := First (Pragma_Argument_Associations (Ritem));
8036 Arg2 := Next (Arg1);
8038 Arg1 := Get_Pragma_Arg (Arg1);
8039 Arg2 := Get_Pragma_Arg (Arg2);
8041 -- See if this predicate pragma is for the current type or for
8042 -- its full view. A predicate on a private completion is placed
8043 -- on the partial view beause this is the visible entity that
8044 -- is frozen.
8046 if Entity (Arg1) = Typ
8047 or else Full_View (Entity (Arg1)) = Typ
8048 then
8049 -- We have a match, this entry is for our subtype
8051 -- We need to replace any occurrences of the name of the
8052 -- type with references to the object.
8054 Replace_Type_References (Arg2, Typ);
8056 -- If this predicate comes from an aspect, find the aspect
8057 -- specification, and replace the saved expression because
8058 -- we need the subtype references replaced for the calls to
8059 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8060 -- and Check_Aspect_At_End_Of_Declarations.
8062 if From_Aspect_Specification (Ritem) then
8063 declare
8064 Aitem : Node_Id;
8066 begin
8067 -- Loop to find corresponding aspect, note that this
8068 -- must be present given the pragma is marked delayed.
8070 Aitem := Next_Rep_Item (Ritem);
8071 loop
8072 if Nkind (Aitem) = N_Aspect_Specification
8073 and then Aspect_Rep_Item (Aitem) = Ritem
8074 then
8075 Set_Entity
8076 (Identifier (Aitem), New_Copy_Tree (Arg2));
8077 exit;
8078 end if;
8080 Aitem := Next_Rep_Item (Aitem);
8081 end loop;
8082 end;
8083 end if;
8085 -- Now we can add the expression
8087 if No (Expr) then
8088 Expr := Relocate_Node (Arg2);
8090 -- There already was a predicate, so add to it
8092 else
8093 Expr :=
8094 Make_And_Then (Loc,
8095 Left_Opnd => Relocate_Node (Expr),
8096 Right_Opnd => Relocate_Node (Arg2));
8097 end if;
8098 end if;
8099 end if;
8101 Next_Rep_Item (Ritem);
8102 end loop;
8103 end Add_Predicates;
8105 ----------------
8106 -- Process_RE --
8107 ----------------
8109 function Process_RE (N : Node_Id) return Traverse_Result is
8110 begin
8111 if Nkind (N) = N_Raise_Expression then
8112 Set_Convert_To_Return_False (N);
8113 return Skip;
8114 else
8115 return OK;
8116 end if;
8117 end Process_RE;
8119 -------------
8120 -- Test_RE --
8121 -------------
8123 function Test_RE (N : Node_Id) return Traverse_Result is
8124 begin
8125 if Nkind (N) = N_Raise_Expression then
8126 Raise_Expression_Present := True;
8127 return Abandon;
8128 else
8129 return OK;
8130 end if;
8131 end Test_RE;
8133 -- Start of processing for Build_Predicate_Functions
8135 begin
8136 -- Return if already built or if type does not have predicates
8138 if not Has_Predicates (Typ)
8139 or else Present (Predicate_Function (Typ))
8140 then
8141 return;
8142 end if;
8144 -- Prepare to construct predicate expression
8146 Expr := Empty;
8148 -- Add Predicates for the current type
8150 Add_Predicates;
8152 -- Add predicates for ancestor if present
8154 declare
8155 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8156 begin
8157 if Present (Atyp) then
8158 Add_Call (Atyp);
8159 end if;
8160 end;
8162 -- Case where predicates are present
8164 if Present (Expr) then
8166 -- Test for raise expression present
8168 Test_REs (Expr);
8170 -- If raise expression is present, capture a copy of Expr for use
8171 -- in building the predicateM function version later on. For this
8172 -- copy we replace references to Object_Entity by Object_Entity_M.
8174 if Raise_Expression_Present then
8175 declare
8176 Map : constant Elist_Id := New_Elmt_List;
8177 begin
8178 Append_Elmt (Object_Entity, Map);
8179 Append_Elmt (Object_Entity_M, Map);
8180 Expr_M := New_Copy_Tree (Expr, Map => Map);
8181 end;
8182 end if;
8184 -- Build the main predicate function
8186 declare
8187 SId : constant Entity_Id :=
8188 Make_Defining_Identifier (Loc,
8189 Chars => New_External_Name (Chars (Typ), "Predicate"));
8190 -- The entity for the the function spec
8192 SIdB : constant Entity_Id :=
8193 Make_Defining_Identifier (Loc,
8194 Chars => New_External_Name (Chars (Typ), "Predicate"));
8195 -- The entity for the function body
8197 Spec : Node_Id;
8198 FDecl : Node_Id;
8199 FBody : Node_Id;
8201 begin
8202 -- Build function declaration
8204 Set_Ekind (SId, E_Function);
8205 Set_Is_Internal (SId);
8206 Set_Is_Predicate_Function (SId);
8207 Set_Predicate_Function (Typ, SId);
8209 -- The predicate function is shared between views of a type
8211 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8212 Set_Predicate_Function (Full_View (Typ), SId);
8213 end if;
8215 Spec :=
8216 Make_Function_Specification (Loc,
8217 Defining_Unit_Name => SId,
8218 Parameter_Specifications => New_List (
8219 Make_Parameter_Specification (Loc,
8220 Defining_Identifier => Object_Entity,
8221 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8222 Result_Definition =>
8223 New_Occurrence_Of (Standard_Boolean, Loc));
8225 FDecl :=
8226 Make_Subprogram_Declaration (Loc,
8227 Specification => Spec);
8229 -- Build function body
8231 Spec :=
8232 Make_Function_Specification (Loc,
8233 Defining_Unit_Name => SIdB,
8234 Parameter_Specifications => New_List (
8235 Make_Parameter_Specification (Loc,
8236 Defining_Identifier =>
8237 Make_Defining_Identifier (Loc, Object_Name),
8238 Parameter_Type =>
8239 New_Occurrence_Of (Typ, Loc))),
8240 Result_Definition =>
8241 New_Occurrence_Of (Standard_Boolean, Loc));
8243 FBody :=
8244 Make_Subprogram_Body (Loc,
8245 Specification => Spec,
8246 Declarations => Empty_List,
8247 Handled_Statement_Sequence =>
8248 Make_Handled_Sequence_Of_Statements (Loc,
8249 Statements => New_List (
8250 Make_Simple_Return_Statement (Loc,
8251 Expression => Expr))));
8253 -- Insert declaration before freeze node and body after
8255 Insert_Before_And_Analyze (N, FDecl);
8256 Insert_After_And_Analyze (N, FBody);
8257 end;
8259 -- Test for raise expressions present and if so build M version
8261 if Raise_Expression_Present then
8262 declare
8263 SId : constant Entity_Id :=
8264 Make_Defining_Identifier (Loc,
8265 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8266 -- The entity for the the function spec
8268 SIdB : constant Entity_Id :=
8269 Make_Defining_Identifier (Loc,
8270 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8271 -- The entity for the function body
8273 Spec : Node_Id;
8274 FDecl : Node_Id;
8275 FBody : Node_Id;
8276 BTemp : Entity_Id;
8278 begin
8279 -- Mark any raise expressions for special expansion
8281 Process_REs (Expr_M);
8283 -- Build function declaration
8285 Set_Ekind (SId, E_Function);
8286 Set_Is_Predicate_Function_M (SId);
8287 Set_Predicate_Function_M (Typ, SId);
8289 -- The predicate function is shared between views of a type
8291 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8292 Set_Predicate_Function_M (Full_View (Typ), SId);
8293 end if;
8295 Spec :=
8296 Make_Function_Specification (Loc,
8297 Defining_Unit_Name => SId,
8298 Parameter_Specifications => New_List (
8299 Make_Parameter_Specification (Loc,
8300 Defining_Identifier => Object_Entity_M,
8301 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8302 Result_Definition =>
8303 New_Occurrence_Of (Standard_Boolean, Loc));
8305 FDecl :=
8306 Make_Subprogram_Declaration (Loc,
8307 Specification => Spec);
8309 -- Build function body
8311 Spec :=
8312 Make_Function_Specification (Loc,
8313 Defining_Unit_Name => SIdB,
8314 Parameter_Specifications => New_List (
8315 Make_Parameter_Specification (Loc,
8316 Defining_Identifier =>
8317 Make_Defining_Identifier (Loc, Object_Name),
8318 Parameter_Type =>
8319 New_Occurrence_Of (Typ, Loc))),
8320 Result_Definition =>
8321 New_Occurrence_Of (Standard_Boolean, Loc));
8323 -- Build the body, we declare the boolean expression before
8324 -- doing the return, because we are not really confident of
8325 -- what happens if a return appears within a return.
8327 BTemp :=
8328 Make_Defining_Identifier (Loc,
8329 Chars => New_Internal_Name ('B'));
8331 FBody :=
8332 Make_Subprogram_Body (Loc,
8333 Specification => Spec,
8335 Declarations => New_List (
8336 Make_Object_Declaration (Loc,
8337 Defining_Identifier => BTemp,
8338 Constant_Present => True,
8339 Object_Definition =>
8340 New_Occurrence_Of (Standard_Boolean, Loc),
8341 Expression => Expr_M)),
8343 Handled_Statement_Sequence =>
8344 Make_Handled_Sequence_Of_Statements (Loc,
8345 Statements => New_List (
8346 Make_Simple_Return_Statement (Loc,
8347 Expression => New_Occurrence_Of (BTemp, Loc)))));
8349 -- Insert declaration before freeze node and body after
8351 Insert_Before_And_Analyze (N, FDecl);
8352 Insert_After_And_Analyze (N, FBody);
8353 end;
8354 end if;
8356 -- See if we have a static predicate. Note that the answer may be
8357 -- yes even if we have an explicit Dynamic_Predicate present.
8359 declare
8360 PS : Boolean;
8361 EN : Node_Id;
8363 begin
8364 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8365 PS := False;
8366 else
8367 PS := Is_Predicate_Static (Expr, Object_Name);
8368 end if;
8370 -- Case where we have a predicate-static aspect
8372 if PS then
8374 -- We don't set Has_Static_Predicate_Aspect, since we can have
8375 -- any of the three cases (Predicate, Dynamic_Predicate, or
8376 -- Static_Predicate) generating a predicate with an expression
8377 -- that is predicate-static. We just indicate that we have a
8378 -- predicate that can be treated as static.
8380 Set_Has_Static_Predicate (Typ);
8382 -- For discrete subtype, build the static predicate list
8384 if Is_Discrete_Type (Typ) then
8385 if not Is_Static_Subtype (Typ) then
8387 -- This can only happen in the presence of previous
8388 -- semantic errors.
8390 pragma Assert (Serious_Errors_Detected > 0);
8391 return;
8392 end if;
8394 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8396 -- If we don't get a static predicate list, it means that we
8397 -- have a case where this is not possible, most typically in
8398 -- the case where we inherit a dynamic predicate. We do not
8399 -- consider this an error, we just leave the predicate as
8400 -- dynamic. But if we do succeed in building the list, then
8401 -- we mark the predicate as static.
8403 if No (Static_Discrete_Predicate (Typ)) then
8404 Set_Has_Static_Predicate (Typ, False);
8405 end if;
8407 -- For real or string subtype, save predicate expression
8409 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8410 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8411 end if;
8413 -- Case of dynamic predicate (expression is not predicate-static)
8415 else
8416 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8417 -- is only set if we have an explicit Dynamic_Predicate aspect
8418 -- given. Here we may simply have a Predicate aspect where the
8419 -- expression happens not to be predicate-static.
8421 -- Emit an error when the predicate is categorized as static
8422 -- but its expression is not predicate-static.
8424 -- First a little fiddling to get a nice location for the
8425 -- message. If the expression is of the form (A and then B),
8426 -- then use the left operand for the Sloc. This avoids getting
8427 -- confused by a call to a higher-level predicate with a less
8428 -- convenient source location.
8430 EN := Expr;
8431 while Nkind (EN) = N_And_Then loop
8432 EN := Left_Opnd (EN);
8433 end loop;
8435 -- Now post appropriate message
8437 if Has_Static_Predicate_Aspect (Typ) then
8438 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8439 Error_Msg_F
8440 ("expression is not predicate-static (RM 3.2.4(16-22))",
8441 EN);
8442 else
8443 Error_Msg_F
8444 ("static predicate requires scalar or string type", EN);
8445 end if;
8446 end if;
8447 end if;
8448 end;
8449 end if;
8450 end Build_Predicate_Functions;
8452 -----------------------------------------
8453 -- Check_Aspect_At_End_Of_Declarations --
8454 -----------------------------------------
8456 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8457 Ent : constant Entity_Id := Entity (ASN);
8458 Ident : constant Node_Id := Identifier (ASN);
8459 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8461 End_Decl_Expr : constant Node_Id := Entity (Ident);
8462 -- Expression to be analyzed at end of declarations
8464 Freeze_Expr : constant Node_Id := Expression (ASN);
8465 -- Expression from call to Check_Aspect_At_Freeze_Point
8467 T : constant Entity_Id := Etype (Freeze_Expr);
8468 -- Type required for preanalyze call
8470 Err : Boolean;
8471 -- Set False if error
8473 -- On entry to this procedure, Entity (Ident) contains a copy of the
8474 -- original expression from the aspect, saved for this purpose, and
8475 -- but Expression (Ident) is a preanalyzed copy of the expression,
8476 -- preanalyzed just after the freeze point.
8478 procedure Check_Overloaded_Name;
8479 -- For aspects whose expression is simply a name, this routine checks if
8480 -- the name is overloaded or not. If so, it verifies there is an
8481 -- interpretation that matches the entity obtained at the freeze point,
8482 -- otherwise the compiler complains.
8484 ---------------------------
8485 -- Check_Overloaded_Name --
8486 ---------------------------
8488 procedure Check_Overloaded_Name is
8489 begin
8490 if not Is_Overloaded (End_Decl_Expr) then
8491 Err := not Is_Entity_Name (End_Decl_Expr)
8492 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8494 else
8495 Err := True;
8497 declare
8498 Index : Interp_Index;
8499 It : Interp;
8501 begin
8502 Get_First_Interp (End_Decl_Expr, Index, It);
8503 while Present (It.Typ) loop
8504 if It.Nam = Entity (Freeze_Expr) then
8505 Err := False;
8506 exit;
8507 end if;
8509 Get_Next_Interp (Index, It);
8510 end loop;
8511 end;
8512 end if;
8513 end Check_Overloaded_Name;
8515 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8517 begin
8518 -- Case of aspects Dimension, Dimension_System and Synchronization
8520 if A_Id = Aspect_Synchronization then
8521 return;
8523 -- Case of stream attributes, just have to compare entities. However,
8524 -- the expression is just a name (possibly overloaded), and there may
8525 -- be stream operations declared for unrelated types, so we just need
8526 -- to verify that one of these interpretations is the one available at
8527 -- at the freeze point.
8529 elsif A_Id = Aspect_Input or else
8530 A_Id = Aspect_Output or else
8531 A_Id = Aspect_Read or else
8532 A_Id = Aspect_Write
8533 then
8534 Analyze (End_Decl_Expr);
8535 Check_Overloaded_Name;
8537 elsif A_Id = Aspect_Variable_Indexing or else
8538 A_Id = Aspect_Constant_Indexing or else
8539 A_Id = Aspect_Default_Iterator or else
8540 A_Id = Aspect_Iterator_Element
8541 then
8542 -- Make type unfrozen before analysis, to prevent spurious errors
8543 -- about late attributes.
8545 Set_Is_Frozen (Ent, False);
8546 Analyze (End_Decl_Expr);
8547 Set_Is_Frozen (Ent, True);
8549 -- If the end of declarations comes before any other freeze
8550 -- point, the Freeze_Expr is not analyzed: no check needed.
8552 if Analyzed (Freeze_Expr) and then not In_Instance then
8553 Check_Overloaded_Name;
8554 else
8555 Err := False;
8556 end if;
8558 -- All other cases
8560 else
8561 -- Indicate that the expression comes from an aspect specification,
8562 -- which is used in subsequent analysis even if expansion is off.
8564 Set_Parent (End_Decl_Expr, ASN);
8566 -- In a generic context the aspect expressions have not been
8567 -- preanalyzed, so do it now. There are no conformance checks
8568 -- to perform in this case.
8570 if No (T) then
8571 Check_Aspect_At_Freeze_Point (ASN);
8572 return;
8574 -- The default values attributes may be defined in the private part,
8575 -- and the analysis of the expression may take place when only the
8576 -- partial view is visible. The expression must be scalar, so use
8577 -- the full view to resolve.
8579 elsif (A_Id = Aspect_Default_Value
8580 or else
8581 A_Id = Aspect_Default_Component_Value)
8582 and then Is_Private_Type (T)
8583 then
8584 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8586 else
8587 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8588 end if;
8590 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8591 end if;
8593 -- Output error message if error. Force error on aspect specification
8594 -- even if there is an error on the expression itself.
8596 if Err then
8597 Error_Msg_NE
8598 ("!visibility of aspect for& changes after freeze point",
8599 ASN, Ent);
8600 Error_Msg_NE
8601 ("info: & is frozen here, aspects evaluated at this point??",
8602 Freeze_Node (Ent), Ent);
8603 end if;
8604 end Check_Aspect_At_End_Of_Declarations;
8606 ----------------------------------
8607 -- Check_Aspect_At_Freeze_Point --
8608 ----------------------------------
8610 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8611 Ident : constant Node_Id := Identifier (ASN);
8612 -- Identifier (use Entity field to save expression)
8614 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8616 T : Entity_Id := Empty;
8617 -- Type required for preanalyze call
8619 begin
8620 -- On entry to this procedure, Entity (Ident) contains a copy of the
8621 -- original expression from the aspect, saved for this purpose.
8623 -- On exit from this procedure Entity (Ident) is unchanged, still
8624 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8625 -- of the expression, preanalyzed just after the freeze point.
8627 -- Make a copy of the expression to be preanalyzed
8629 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8631 -- Find type for preanalyze call
8633 case A_Id is
8635 -- No_Aspect should be impossible
8637 when No_Aspect =>
8638 raise Program_Error;
8640 -- Aspects taking an optional boolean argument
8642 when Boolean_Aspects |
8643 Library_Unit_Aspects =>
8645 T := Standard_Boolean;
8647 -- Aspects corresponding to attribute definition clauses
8649 when Aspect_Address =>
8650 T := RTE (RE_Address);
8652 when Aspect_Attach_Handler =>
8653 T := RTE (RE_Interrupt_ID);
8655 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8656 T := RTE (RE_Bit_Order);
8658 when Aspect_Convention =>
8659 return;
8661 when Aspect_CPU =>
8662 T := RTE (RE_CPU_Range);
8664 -- Default_Component_Value is resolved with the component type
8666 when Aspect_Default_Component_Value =>
8667 T := Component_Type (Entity (ASN));
8669 -- Default_Value is resolved with the type entity in question
8671 when Aspect_Default_Value =>
8672 T := Entity (ASN);
8674 -- Depends is a delayed aspect because it mentiones names first
8675 -- introduced by aspect Global which is already delayed. There is
8676 -- no action to be taken with respect to the aspect itself as the
8677 -- analysis is done by the corresponding pragma.
8679 when Aspect_Depends =>
8680 return;
8682 when Aspect_Dispatching_Domain =>
8683 T := RTE (RE_Dispatching_Domain);
8685 when Aspect_External_Tag =>
8686 T := Standard_String;
8688 when Aspect_External_Name =>
8689 T := Standard_String;
8691 -- Global is a delayed aspect because it may reference names that
8692 -- have not been declared yet. There is no action to be taken with
8693 -- respect to the aspect itself as the reference checking is done
8694 -- on the corresponding pragma.
8696 when Aspect_Global =>
8697 return;
8699 when Aspect_Link_Name =>
8700 T := Standard_String;
8702 when Aspect_Priority | Aspect_Interrupt_Priority =>
8703 T := Standard_Integer;
8705 when Aspect_Relative_Deadline =>
8706 T := RTE (RE_Time_Span);
8708 when Aspect_Small =>
8709 T := Universal_Real;
8711 -- For a simple storage pool, we have to retrieve the type of the
8712 -- pool object associated with the aspect's corresponding attribute
8713 -- definition clause.
8715 when Aspect_Simple_Storage_Pool =>
8716 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8718 when Aspect_Storage_Pool =>
8719 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8721 when Aspect_Alignment |
8722 Aspect_Component_Size |
8723 Aspect_Machine_Radix |
8724 Aspect_Object_Size |
8725 Aspect_Size |
8726 Aspect_Storage_Size |
8727 Aspect_Stream_Size |
8728 Aspect_Value_Size =>
8729 T := Any_Integer;
8731 when Aspect_Linker_Section =>
8732 T := Standard_String;
8734 when Aspect_Synchronization =>
8735 return;
8737 -- Special case, the expression of these aspects is just an entity
8738 -- that does not need any resolution, so just analyze.
8740 when Aspect_Input |
8741 Aspect_Output |
8742 Aspect_Read |
8743 Aspect_Suppress |
8744 Aspect_Unsuppress |
8745 Aspect_Warnings |
8746 Aspect_Write =>
8747 Analyze (Expression (ASN));
8748 return;
8750 -- Same for Iterator aspects, where the expression is a function
8751 -- name. Legality rules are checked separately.
8753 when Aspect_Constant_Indexing |
8754 Aspect_Default_Iterator |
8755 Aspect_Iterator_Element |
8756 Aspect_Variable_Indexing =>
8757 Analyze (Expression (ASN));
8758 return;
8760 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8762 when Aspect_Iterable =>
8763 T := Entity (ASN);
8765 declare
8766 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8767 Assoc : Node_Id;
8768 Expr : Node_Id;
8770 begin
8771 if Cursor = Any_Type then
8772 return;
8773 end if;
8775 Assoc := First (Component_Associations (Expression (ASN)));
8776 while Present (Assoc) loop
8777 Expr := Expression (Assoc);
8778 Analyze (Expr);
8780 if not Error_Posted (Expr) then
8781 Resolve_Iterable_Operation
8782 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8783 end if;
8785 Next (Assoc);
8786 end loop;
8787 end;
8789 return;
8791 -- Invariant/Predicate take boolean expressions
8793 when Aspect_Dynamic_Predicate |
8794 Aspect_Invariant |
8795 Aspect_Predicate |
8796 Aspect_Static_Predicate |
8797 Aspect_Type_Invariant =>
8798 T := Standard_Boolean;
8800 -- Here is the list of aspects that don't require delay analysis
8802 when Aspect_Abstract_State |
8803 Aspect_Annotate |
8804 Aspect_Contract_Cases |
8805 Aspect_Default_Initial_Condition |
8806 Aspect_Dimension |
8807 Aspect_Dimension_System |
8808 Aspect_Implicit_Dereference |
8809 Aspect_Initial_Condition |
8810 Aspect_Initializes |
8811 Aspect_Obsolescent |
8812 Aspect_Part_Of |
8813 Aspect_Post |
8814 Aspect_Postcondition |
8815 Aspect_Pre |
8816 Aspect_Precondition |
8817 Aspect_Refined_Depends |
8818 Aspect_Refined_Global |
8819 Aspect_Refined_Post |
8820 Aspect_Refined_State |
8821 Aspect_SPARK_Mode |
8822 Aspect_Test_Case =>
8823 raise Program_Error;
8825 end case;
8827 -- Do the preanalyze call
8829 Preanalyze_Spec_Expression (Expression (ASN), T);
8830 end Check_Aspect_At_Freeze_Point;
8832 -----------------------------------
8833 -- Check_Constant_Address_Clause --
8834 -----------------------------------
8836 procedure Check_Constant_Address_Clause
8837 (Expr : Node_Id;
8838 U_Ent : Entity_Id)
8840 procedure Check_At_Constant_Address (Nod : Node_Id);
8841 -- Checks that the given node N represents a name whose 'Address is
8842 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8843 -- address value is the same at the point of declaration of U_Ent and at
8844 -- the time of elaboration of the address clause.
8846 procedure Check_Expr_Constants (Nod : Node_Id);
8847 -- Checks that Nod meets the requirements for a constant address clause
8848 -- in the sense of the enclosing procedure.
8850 procedure Check_List_Constants (Lst : List_Id);
8851 -- Check that all elements of list Lst meet the requirements for a
8852 -- constant address clause in the sense of the enclosing procedure.
8854 -------------------------------
8855 -- Check_At_Constant_Address --
8856 -------------------------------
8858 procedure Check_At_Constant_Address (Nod : Node_Id) is
8859 begin
8860 if Is_Entity_Name (Nod) then
8861 if Present (Address_Clause (Entity ((Nod)))) then
8862 Error_Msg_NE
8863 ("invalid address clause for initialized object &!",
8864 Nod, U_Ent);
8865 Error_Msg_NE
8866 ("address for& cannot" &
8867 " depend on another address clause! (RM 13.1(22))!",
8868 Nod, U_Ent);
8870 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
8871 and then Sloc (U_Ent) < Sloc (Entity (Nod))
8872 then
8873 Error_Msg_NE
8874 ("invalid address clause for initialized object &!",
8875 Nod, U_Ent);
8876 Error_Msg_Node_2 := U_Ent;
8877 Error_Msg_NE
8878 ("\& must be defined before & (RM 13.1(22))!",
8879 Nod, Entity (Nod));
8880 end if;
8882 elsif Nkind (Nod) = N_Selected_Component then
8883 declare
8884 T : constant Entity_Id := Etype (Prefix (Nod));
8886 begin
8887 if (Is_Record_Type (T)
8888 and then Has_Discriminants (T))
8889 or else
8890 (Is_Access_Type (T)
8891 and then Is_Record_Type (Designated_Type (T))
8892 and then Has_Discriminants (Designated_Type (T)))
8893 then
8894 Error_Msg_NE
8895 ("invalid address clause for initialized object &!",
8896 Nod, U_Ent);
8897 Error_Msg_N
8898 ("\address cannot depend on component" &
8899 " of discriminated record (RM 13.1(22))!",
8900 Nod);
8901 else
8902 Check_At_Constant_Address (Prefix (Nod));
8903 end if;
8904 end;
8906 elsif Nkind (Nod) = N_Indexed_Component then
8907 Check_At_Constant_Address (Prefix (Nod));
8908 Check_List_Constants (Expressions (Nod));
8910 else
8911 Check_Expr_Constants (Nod);
8912 end if;
8913 end Check_At_Constant_Address;
8915 --------------------------
8916 -- Check_Expr_Constants --
8917 --------------------------
8919 procedure Check_Expr_Constants (Nod : Node_Id) is
8920 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
8921 Ent : Entity_Id := Empty;
8923 begin
8924 if Nkind (Nod) in N_Has_Etype
8925 and then Etype (Nod) = Any_Type
8926 then
8927 return;
8928 end if;
8930 case Nkind (Nod) is
8931 when N_Empty | N_Error =>
8932 return;
8934 when N_Identifier | N_Expanded_Name =>
8935 Ent := Entity (Nod);
8937 -- We need to look at the original node if it is different
8938 -- from the node, since we may have rewritten things and
8939 -- substituted an identifier representing the rewrite.
8941 if Original_Node (Nod) /= Nod then
8942 Check_Expr_Constants (Original_Node (Nod));
8944 -- If the node is an object declaration without initial
8945 -- value, some code has been expanded, and the expression
8946 -- is not constant, even if the constituents might be
8947 -- acceptable, as in A'Address + offset.
8949 if Ekind (Ent) = E_Variable
8950 and then
8951 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
8952 and then
8953 No (Expression (Declaration_Node (Ent)))
8954 then
8955 Error_Msg_NE
8956 ("invalid address clause for initialized object &!",
8957 Nod, U_Ent);
8959 -- If entity is constant, it may be the result of expanding
8960 -- a check. We must verify that its declaration appears
8961 -- before the object in question, else we also reject the
8962 -- address clause.
8964 elsif Ekind (Ent) = E_Constant
8965 and then In_Same_Source_Unit (Ent, U_Ent)
8966 and then Sloc (Ent) > Loc_U_Ent
8967 then
8968 Error_Msg_NE
8969 ("invalid address clause for initialized object &!",
8970 Nod, U_Ent);
8971 end if;
8973 return;
8974 end if;
8976 -- Otherwise look at the identifier and see if it is OK
8978 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
8979 or else Is_Type (Ent)
8980 then
8981 return;
8983 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
8985 -- This is the case where we must have Ent defined before
8986 -- U_Ent. Clearly if they are in different units this
8987 -- requirement is met since the unit containing Ent is
8988 -- already processed.
8990 if not In_Same_Source_Unit (Ent, U_Ent) then
8991 return;
8993 -- Otherwise location of Ent must be before the location
8994 -- of U_Ent, that's what prior defined means.
8996 elsif Sloc (Ent) < Loc_U_Ent then
8997 return;
8999 else
9000 Error_Msg_NE
9001 ("invalid address clause for initialized object &!",
9002 Nod, U_Ent);
9003 Error_Msg_Node_2 := U_Ent;
9004 Error_Msg_NE
9005 ("\& must be defined before & (RM 13.1(22))!",
9006 Nod, Ent);
9007 end if;
9009 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9010 Check_Expr_Constants (Original_Node (Nod));
9012 else
9013 Error_Msg_NE
9014 ("invalid address clause for initialized object &!",
9015 Nod, U_Ent);
9017 if Comes_From_Source (Ent) then
9018 Error_Msg_NE
9019 ("\reference to variable& not allowed"
9020 & " (RM 13.1(22))!", Nod, Ent);
9021 else
9022 Error_Msg_N
9023 ("non-static expression not allowed"
9024 & " (RM 13.1(22))!", Nod);
9025 end if;
9026 end if;
9028 when N_Integer_Literal =>
9030 -- If this is a rewritten unchecked conversion, in a system
9031 -- where Address is an integer type, always use the base type
9032 -- for a literal value. This is user-friendly and prevents
9033 -- order-of-elaboration issues with instances of unchecked
9034 -- conversion.
9036 if Nkind (Original_Node (Nod)) = N_Function_Call then
9037 Set_Etype (Nod, Base_Type (Etype (Nod)));
9038 end if;
9040 when N_Real_Literal |
9041 N_String_Literal |
9042 N_Character_Literal =>
9043 return;
9045 when N_Range =>
9046 Check_Expr_Constants (Low_Bound (Nod));
9047 Check_Expr_Constants (High_Bound (Nod));
9049 when N_Explicit_Dereference =>
9050 Check_Expr_Constants (Prefix (Nod));
9052 when N_Indexed_Component =>
9053 Check_Expr_Constants (Prefix (Nod));
9054 Check_List_Constants (Expressions (Nod));
9056 when N_Slice =>
9057 Check_Expr_Constants (Prefix (Nod));
9058 Check_Expr_Constants (Discrete_Range (Nod));
9060 when N_Selected_Component =>
9061 Check_Expr_Constants (Prefix (Nod));
9063 when N_Attribute_Reference =>
9064 if Nam_In (Attribute_Name (Nod), Name_Address,
9065 Name_Access,
9066 Name_Unchecked_Access,
9067 Name_Unrestricted_Access)
9068 then
9069 Check_At_Constant_Address (Prefix (Nod));
9071 else
9072 Check_Expr_Constants (Prefix (Nod));
9073 Check_List_Constants (Expressions (Nod));
9074 end if;
9076 when N_Aggregate =>
9077 Check_List_Constants (Component_Associations (Nod));
9078 Check_List_Constants (Expressions (Nod));
9080 when N_Component_Association =>
9081 Check_Expr_Constants (Expression (Nod));
9083 when N_Extension_Aggregate =>
9084 Check_Expr_Constants (Ancestor_Part (Nod));
9085 Check_List_Constants (Component_Associations (Nod));
9086 Check_List_Constants (Expressions (Nod));
9088 when N_Null =>
9089 return;
9091 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9092 Check_Expr_Constants (Left_Opnd (Nod));
9093 Check_Expr_Constants (Right_Opnd (Nod));
9095 when N_Unary_Op =>
9096 Check_Expr_Constants (Right_Opnd (Nod));
9098 when N_Type_Conversion |
9099 N_Qualified_Expression |
9100 N_Allocator |
9101 N_Unchecked_Type_Conversion =>
9102 Check_Expr_Constants (Expression (Nod));
9104 when N_Function_Call =>
9105 if not Is_Pure (Entity (Name (Nod))) then
9106 Error_Msg_NE
9107 ("invalid address clause for initialized object &!",
9108 Nod, U_Ent);
9110 Error_Msg_NE
9111 ("\function & is not pure (RM 13.1(22))!",
9112 Nod, Entity (Name (Nod)));
9114 else
9115 Check_List_Constants (Parameter_Associations (Nod));
9116 end if;
9118 when N_Parameter_Association =>
9119 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9121 when others =>
9122 Error_Msg_NE
9123 ("invalid address clause for initialized object &!",
9124 Nod, U_Ent);
9125 Error_Msg_NE
9126 ("\must be constant defined before& (RM 13.1(22))!",
9127 Nod, U_Ent);
9128 end case;
9129 end Check_Expr_Constants;
9131 --------------------------
9132 -- Check_List_Constants --
9133 --------------------------
9135 procedure Check_List_Constants (Lst : List_Id) is
9136 Nod1 : Node_Id;
9138 begin
9139 if Present (Lst) then
9140 Nod1 := First (Lst);
9141 while Present (Nod1) loop
9142 Check_Expr_Constants (Nod1);
9143 Next (Nod1);
9144 end loop;
9145 end if;
9146 end Check_List_Constants;
9148 -- Start of processing for Check_Constant_Address_Clause
9150 begin
9151 -- If rep_clauses are to be ignored, no need for legality checks. In
9152 -- particular, no need to pester user about rep clauses that violate
9153 -- the rule on constant addresses, given that these clauses will be
9154 -- removed by Freeze before they reach the back end.
9156 if not Ignore_Rep_Clauses then
9157 Check_Expr_Constants (Expr);
9158 end if;
9159 end Check_Constant_Address_Clause;
9161 ---------------------------
9162 -- Check_Pool_Size_Clash --
9163 ---------------------------
9165 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9166 Post : Node_Id;
9168 begin
9169 -- We need to find out which one came first. Note that in the case of
9170 -- aspects mixed with pragmas there are cases where the processing order
9171 -- is reversed, which is why we do the check here.
9173 if Sloc (SP) < Sloc (SS) then
9174 Error_Msg_Sloc := Sloc (SP);
9175 Post := SS;
9176 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9178 else
9179 Error_Msg_Sloc := Sloc (SS);
9180 Post := SP;
9181 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9182 end if;
9184 Error_Msg_N
9185 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9186 end Check_Pool_Size_Clash;
9188 ----------------------------------------
9189 -- Check_Record_Representation_Clause --
9190 ----------------------------------------
9192 procedure Check_Record_Representation_Clause (N : Node_Id) is
9193 Loc : constant Source_Ptr := Sloc (N);
9194 Ident : constant Node_Id := Identifier (N);
9195 Rectype : Entity_Id;
9196 Fent : Entity_Id;
9197 CC : Node_Id;
9198 Fbit : Uint;
9199 Lbit : Uint;
9200 Hbit : Uint := Uint_0;
9201 Comp : Entity_Id;
9202 Pcomp : Entity_Id;
9204 Max_Bit_So_Far : Uint;
9205 -- Records the maximum bit position so far. If all field positions
9206 -- are monotonically increasing, then we can skip the circuit for
9207 -- checking for overlap, since no overlap is possible.
9209 Tagged_Parent : Entity_Id := Empty;
9210 -- This is set in the case of a derived tagged type for which we have
9211 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9212 -- positioned by record representation clauses). In this case we must
9213 -- check for overlap between components of this tagged type, and the
9214 -- components of its parent. Tagged_Parent will point to this parent
9215 -- type. For all other cases Tagged_Parent is left set to Empty.
9217 Parent_Last_Bit : Uint;
9218 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9219 -- last bit position for any field in the parent type. We only need to
9220 -- check overlap for fields starting below this point.
9222 Overlap_Check_Required : Boolean;
9223 -- Used to keep track of whether or not an overlap check is required
9225 Overlap_Detected : Boolean := False;
9226 -- Set True if an overlap is detected
9228 Ccount : Natural := 0;
9229 -- Number of component clauses in record rep clause
9231 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9232 -- Given two entities for record components or discriminants, checks
9233 -- if they have overlapping component clauses and issues errors if so.
9235 procedure Find_Component;
9236 -- Finds component entity corresponding to current component clause (in
9237 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9238 -- start/stop bits for the field. If there is no matching component or
9239 -- if the matching component does not have a component clause, then
9240 -- that's an error and Comp is set to Empty, but no error message is
9241 -- issued, since the message was already given. Comp is also set to
9242 -- Empty if the current "component clause" is in fact a pragma.
9244 -----------------------------
9245 -- Check_Component_Overlap --
9246 -----------------------------
9248 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9249 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9250 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9252 begin
9253 if Present (CC1) and then Present (CC2) then
9255 -- Exclude odd case where we have two tag components in the same
9256 -- record, both at location zero. This seems a bit strange, but
9257 -- it seems to happen in some circumstances, perhaps on an error.
9259 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9260 return;
9261 end if;
9263 -- Here we check if the two fields overlap
9265 declare
9266 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9267 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9268 E1 : constant Uint := S1 + Esize (C1_Ent);
9269 E2 : constant Uint := S2 + Esize (C2_Ent);
9271 begin
9272 if E2 <= S1 or else E1 <= S2 then
9273 null;
9274 else
9275 Error_Msg_Node_2 := Component_Name (CC2);
9276 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9277 Error_Msg_Node_1 := Component_Name (CC1);
9278 Error_Msg_N
9279 ("component& overlaps & #", Component_Name (CC1));
9280 Overlap_Detected := True;
9281 end if;
9282 end;
9283 end if;
9284 end Check_Component_Overlap;
9286 --------------------
9287 -- Find_Component --
9288 --------------------
9290 procedure Find_Component is
9292 procedure Search_Component (R : Entity_Id);
9293 -- Search components of R for a match. If found, Comp is set
9295 ----------------------
9296 -- Search_Component --
9297 ----------------------
9299 procedure Search_Component (R : Entity_Id) is
9300 begin
9301 Comp := First_Component_Or_Discriminant (R);
9302 while Present (Comp) loop
9304 -- Ignore error of attribute name for component name (we
9305 -- already gave an error message for this, so no need to
9306 -- complain here)
9308 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9309 null;
9310 else
9311 exit when Chars (Comp) = Chars (Component_Name (CC));
9312 end if;
9314 Next_Component_Or_Discriminant (Comp);
9315 end loop;
9316 end Search_Component;
9318 -- Start of processing for Find_Component
9320 begin
9321 -- Return with Comp set to Empty if we have a pragma
9323 if Nkind (CC) = N_Pragma then
9324 Comp := Empty;
9325 return;
9326 end if;
9328 -- Search current record for matching component
9330 Search_Component (Rectype);
9332 -- If not found, maybe component of base type discriminant that is
9333 -- absent from statically constrained first subtype.
9335 if No (Comp) then
9336 Search_Component (Base_Type (Rectype));
9337 end if;
9339 -- If no component, or the component does not reference the component
9340 -- clause in question, then there was some previous error for which
9341 -- we already gave a message, so just return with Comp Empty.
9343 if No (Comp) or else Component_Clause (Comp) /= CC then
9344 Check_Error_Detected;
9345 Comp := Empty;
9347 -- Normal case where we have a component clause
9349 else
9350 Fbit := Component_Bit_Offset (Comp);
9351 Lbit := Fbit + Esize (Comp) - 1;
9352 end if;
9353 end Find_Component;
9355 -- Start of processing for Check_Record_Representation_Clause
9357 begin
9358 Find_Type (Ident);
9359 Rectype := Entity (Ident);
9361 if Rectype = Any_Type then
9362 return;
9363 else
9364 Rectype := Underlying_Type (Rectype);
9365 end if;
9367 -- See if we have a fully repped derived tagged type
9369 declare
9370 PS : constant Entity_Id := Parent_Subtype (Rectype);
9372 begin
9373 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9374 Tagged_Parent := PS;
9376 -- Find maximum bit of any component of the parent type
9378 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9379 Pcomp := First_Entity (Tagged_Parent);
9380 while Present (Pcomp) loop
9381 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9382 if Component_Bit_Offset (Pcomp) /= No_Uint
9383 and then Known_Static_Esize (Pcomp)
9384 then
9385 Parent_Last_Bit :=
9386 UI_Max
9387 (Parent_Last_Bit,
9388 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9389 end if;
9391 Next_Entity (Pcomp);
9392 end if;
9393 end loop;
9394 end if;
9395 end;
9397 -- All done if no component clauses
9399 CC := First (Component_Clauses (N));
9401 if No (CC) then
9402 return;
9403 end if;
9405 -- If a tag is present, then create a component clause that places it
9406 -- at the start of the record (otherwise gigi may place it after other
9407 -- fields that have rep clauses).
9409 Fent := First_Entity (Rectype);
9411 if Nkind (Fent) = N_Defining_Identifier
9412 and then Chars (Fent) = Name_uTag
9413 then
9414 Set_Component_Bit_Offset (Fent, Uint_0);
9415 Set_Normalized_Position (Fent, Uint_0);
9416 Set_Normalized_First_Bit (Fent, Uint_0);
9417 Set_Normalized_Position_Max (Fent, Uint_0);
9418 Init_Esize (Fent, System_Address_Size);
9420 Set_Component_Clause (Fent,
9421 Make_Component_Clause (Loc,
9422 Component_Name => Make_Identifier (Loc, Name_uTag),
9424 Position => Make_Integer_Literal (Loc, Uint_0),
9425 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9426 Last_Bit =>
9427 Make_Integer_Literal (Loc,
9428 UI_From_Int (System_Address_Size))));
9430 Ccount := Ccount + 1;
9431 end if;
9433 Max_Bit_So_Far := Uint_Minus_1;
9434 Overlap_Check_Required := False;
9436 -- Process the component clauses
9438 while Present (CC) loop
9439 Find_Component;
9441 if Present (Comp) then
9442 Ccount := Ccount + 1;
9444 -- We need a full overlap check if record positions non-monotonic
9446 if Fbit <= Max_Bit_So_Far then
9447 Overlap_Check_Required := True;
9448 end if;
9450 Max_Bit_So_Far := Lbit;
9452 -- Check bit position out of range of specified size
9454 if Has_Size_Clause (Rectype)
9455 and then RM_Size (Rectype) <= Lbit
9456 then
9457 Error_Msg_N
9458 ("bit number out of range of specified size",
9459 Last_Bit (CC));
9461 -- Check for overlap with tag component
9463 else
9464 if Is_Tagged_Type (Rectype)
9465 and then Fbit < System_Address_Size
9466 then
9467 Error_Msg_NE
9468 ("component overlaps tag field of&",
9469 Component_Name (CC), Rectype);
9470 Overlap_Detected := True;
9471 end if;
9473 if Hbit < Lbit then
9474 Hbit := Lbit;
9475 end if;
9476 end if;
9478 -- Check parent overlap if component might overlap parent field
9480 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9481 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9482 while Present (Pcomp) loop
9483 if not Is_Tag (Pcomp)
9484 and then Chars (Pcomp) /= Name_uParent
9485 then
9486 Check_Component_Overlap (Comp, Pcomp);
9487 end if;
9489 Next_Component_Or_Discriminant (Pcomp);
9490 end loop;
9491 end if;
9492 end if;
9494 Next (CC);
9495 end loop;
9497 -- Now that we have processed all the component clauses, check for
9498 -- overlap. We have to leave this till last, since the components can
9499 -- appear in any arbitrary order in the representation clause.
9501 -- We do not need this check if all specified ranges were monotonic,
9502 -- as recorded by Overlap_Check_Required being False at this stage.
9504 -- This first section checks if there are any overlapping entries at
9505 -- all. It does this by sorting all entries and then seeing if there are
9506 -- any overlaps. If there are none, then that is decisive, but if there
9507 -- are overlaps, they may still be OK (they may result from fields in
9508 -- different variants).
9510 if Overlap_Check_Required then
9511 Overlap_Check1 : declare
9513 OC_Fbit : array (0 .. Ccount) of Uint;
9514 -- First-bit values for component clauses, the value is the offset
9515 -- of the first bit of the field from start of record. The zero
9516 -- entry is for use in sorting.
9518 OC_Lbit : array (0 .. Ccount) of Uint;
9519 -- Last-bit values for component clauses, the value is the offset
9520 -- of the last bit of the field from start of record. The zero
9521 -- entry is for use in sorting.
9523 OC_Count : Natural := 0;
9524 -- Count of entries in OC_Fbit and OC_Lbit
9526 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9527 -- Compare routine for Sort
9529 procedure OC_Move (From : Natural; To : Natural);
9530 -- Move routine for Sort
9532 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9534 -----------
9535 -- OC_Lt --
9536 -----------
9538 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9539 begin
9540 return OC_Fbit (Op1) < OC_Fbit (Op2);
9541 end OC_Lt;
9543 -------------
9544 -- OC_Move --
9545 -------------
9547 procedure OC_Move (From : Natural; To : Natural) is
9548 begin
9549 OC_Fbit (To) := OC_Fbit (From);
9550 OC_Lbit (To) := OC_Lbit (From);
9551 end OC_Move;
9553 -- Start of processing for Overlap_Check
9555 begin
9556 CC := First (Component_Clauses (N));
9557 while Present (CC) loop
9559 -- Exclude component clause already marked in error
9561 if not Error_Posted (CC) then
9562 Find_Component;
9564 if Present (Comp) then
9565 OC_Count := OC_Count + 1;
9566 OC_Fbit (OC_Count) := Fbit;
9567 OC_Lbit (OC_Count) := Lbit;
9568 end if;
9569 end if;
9571 Next (CC);
9572 end loop;
9574 Sorting.Sort (OC_Count);
9576 Overlap_Check_Required := False;
9577 for J in 1 .. OC_Count - 1 loop
9578 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9579 Overlap_Check_Required := True;
9580 exit;
9581 end if;
9582 end loop;
9583 end Overlap_Check1;
9584 end if;
9586 -- If Overlap_Check_Required is still True, then we have to do the full
9587 -- scale overlap check, since we have at least two fields that do
9588 -- overlap, and we need to know if that is OK since they are in
9589 -- different variant, or whether we have a definite problem.
9591 if Overlap_Check_Required then
9592 Overlap_Check2 : declare
9593 C1_Ent, C2_Ent : Entity_Id;
9594 -- Entities of components being checked for overlap
9596 Clist : Node_Id;
9597 -- Component_List node whose Component_Items are being checked
9599 Citem : Node_Id;
9600 -- Component declaration for component being checked
9602 begin
9603 C1_Ent := First_Entity (Base_Type (Rectype));
9605 -- Loop through all components in record. For each component check
9606 -- for overlap with any of the preceding elements on the component
9607 -- list containing the component and also, if the component is in
9608 -- a variant, check against components outside the case structure.
9609 -- This latter test is repeated recursively up the variant tree.
9611 Main_Component_Loop : while Present (C1_Ent) loop
9612 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9613 goto Continue_Main_Component_Loop;
9614 end if;
9616 -- Skip overlap check if entity has no declaration node. This
9617 -- happens with discriminants in constrained derived types.
9618 -- Possibly we are missing some checks as a result, but that
9619 -- does not seem terribly serious.
9621 if No (Declaration_Node (C1_Ent)) then
9622 goto Continue_Main_Component_Loop;
9623 end if;
9625 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9627 -- Loop through component lists that need checking. Check the
9628 -- current component list and all lists in variants above us.
9630 Component_List_Loop : loop
9632 -- If derived type definition, go to full declaration
9633 -- If at outer level, check discriminants if there are any.
9635 if Nkind (Clist) = N_Derived_Type_Definition then
9636 Clist := Parent (Clist);
9637 end if;
9639 -- Outer level of record definition, check discriminants
9641 if Nkind_In (Clist, N_Full_Type_Declaration,
9642 N_Private_Type_Declaration)
9643 then
9644 if Has_Discriminants (Defining_Identifier (Clist)) then
9645 C2_Ent :=
9646 First_Discriminant (Defining_Identifier (Clist));
9647 while Present (C2_Ent) loop
9648 exit when C1_Ent = C2_Ent;
9649 Check_Component_Overlap (C1_Ent, C2_Ent);
9650 Next_Discriminant (C2_Ent);
9651 end loop;
9652 end if;
9654 -- Record extension case
9656 elsif Nkind (Clist) = N_Derived_Type_Definition then
9657 Clist := Empty;
9659 -- Otherwise check one component list
9661 else
9662 Citem := First (Component_Items (Clist));
9663 while Present (Citem) loop
9664 if Nkind (Citem) = N_Component_Declaration then
9665 C2_Ent := Defining_Identifier (Citem);
9666 exit when C1_Ent = C2_Ent;
9667 Check_Component_Overlap (C1_Ent, C2_Ent);
9668 end if;
9670 Next (Citem);
9671 end loop;
9672 end if;
9674 -- Check for variants above us (the parent of the Clist can
9675 -- be a variant, in which case its parent is a variant part,
9676 -- and the parent of the variant part is a component list
9677 -- whose components must all be checked against the current
9678 -- component for overlap).
9680 if Nkind (Parent (Clist)) = N_Variant then
9681 Clist := Parent (Parent (Parent (Clist)));
9683 -- Check for possible discriminant part in record, this
9684 -- is treated essentially as another level in the
9685 -- recursion. For this case the parent of the component
9686 -- list is the record definition, and its parent is the
9687 -- full type declaration containing the discriminant
9688 -- specifications.
9690 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9691 Clist := Parent (Parent ((Clist)));
9693 -- If neither of these two cases, we are at the top of
9694 -- the tree.
9696 else
9697 exit Component_List_Loop;
9698 end if;
9699 end loop Component_List_Loop;
9701 <<Continue_Main_Component_Loop>>
9702 Next_Entity (C1_Ent);
9704 end loop Main_Component_Loop;
9705 end Overlap_Check2;
9706 end if;
9708 -- The following circuit deals with warning on record holes (gaps). We
9709 -- skip this check if overlap was detected, since it makes sense for the
9710 -- programmer to fix this illegality before worrying about warnings.
9712 if not Overlap_Detected and Warn_On_Record_Holes then
9713 Record_Hole_Check : declare
9714 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9715 -- Full declaration of record type
9717 procedure Check_Component_List
9718 (CL : Node_Id;
9719 Sbit : Uint;
9720 DS : List_Id);
9721 -- Check component list CL for holes. The starting bit should be
9722 -- Sbit. which is zero for the main record component list and set
9723 -- appropriately for recursive calls for variants. DS is set to
9724 -- a list of discriminant specifications to be included in the
9725 -- consideration of components. It is No_List if none to consider.
9727 --------------------------
9728 -- Check_Component_List --
9729 --------------------------
9731 procedure Check_Component_List
9732 (CL : Node_Id;
9733 Sbit : Uint;
9734 DS : List_Id)
9736 Compl : Integer;
9738 begin
9739 Compl := Integer (List_Length (Component_Items (CL)));
9741 if DS /= No_List then
9742 Compl := Compl + Integer (List_Length (DS));
9743 end if;
9745 declare
9746 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9747 -- Gather components (zero entry is for sort routine)
9749 Ncomps : Natural := 0;
9750 -- Number of entries stored in Comps (starting at Comps (1))
9752 Citem : Node_Id;
9753 -- One component item or discriminant specification
9755 Nbit : Uint;
9756 -- Starting bit for next component
9758 CEnt : Entity_Id;
9759 -- Component entity
9761 Variant : Node_Id;
9762 -- One variant
9764 function Lt (Op1, Op2 : Natural) return Boolean;
9765 -- Compare routine for Sort
9767 procedure Move (From : Natural; To : Natural);
9768 -- Move routine for Sort
9770 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9772 --------
9773 -- Lt --
9774 --------
9776 function Lt (Op1, Op2 : Natural) return Boolean is
9777 begin
9778 return Component_Bit_Offset (Comps (Op1))
9780 Component_Bit_Offset (Comps (Op2));
9781 end Lt;
9783 ----------
9784 -- Move --
9785 ----------
9787 procedure Move (From : Natural; To : Natural) is
9788 begin
9789 Comps (To) := Comps (From);
9790 end Move;
9792 begin
9793 -- Gather discriminants into Comp
9795 if DS /= No_List then
9796 Citem := First (DS);
9797 while Present (Citem) loop
9798 if Nkind (Citem) = N_Discriminant_Specification then
9799 declare
9800 Ent : constant Entity_Id :=
9801 Defining_Identifier (Citem);
9802 begin
9803 if Ekind (Ent) = E_Discriminant then
9804 Ncomps := Ncomps + 1;
9805 Comps (Ncomps) := Ent;
9806 end if;
9807 end;
9808 end if;
9810 Next (Citem);
9811 end loop;
9812 end if;
9814 -- Gather component entities into Comp
9816 Citem := First (Component_Items (CL));
9817 while Present (Citem) loop
9818 if Nkind (Citem) = N_Component_Declaration then
9819 Ncomps := Ncomps + 1;
9820 Comps (Ncomps) := Defining_Identifier (Citem);
9821 end if;
9823 Next (Citem);
9824 end loop;
9826 -- Now sort the component entities based on the first bit.
9827 -- Note we already know there are no overlapping components.
9829 Sorting.Sort (Ncomps);
9831 -- Loop through entries checking for holes
9833 Nbit := Sbit;
9834 for J in 1 .. Ncomps loop
9835 CEnt := Comps (J);
9836 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9838 if Error_Msg_Uint_1 > 0 then
9839 Error_Msg_NE
9840 ("?H?^-bit gap before component&",
9841 Component_Name (Component_Clause (CEnt)), CEnt);
9842 end if;
9844 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9845 end loop;
9847 -- Process variant parts recursively if present
9849 if Present (Variant_Part (CL)) then
9850 Variant := First (Variants (Variant_Part (CL)));
9851 while Present (Variant) loop
9852 Check_Component_List
9853 (Component_List (Variant), Nbit, No_List);
9854 Next (Variant);
9855 end loop;
9856 end if;
9857 end;
9858 end Check_Component_List;
9860 -- Start of processing for Record_Hole_Check
9862 begin
9863 declare
9864 Sbit : Uint;
9866 begin
9867 if Is_Tagged_Type (Rectype) then
9868 Sbit := UI_From_Int (System_Address_Size);
9869 else
9870 Sbit := Uint_0;
9871 end if;
9873 if Nkind (Decl) = N_Full_Type_Declaration
9874 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9875 then
9876 Check_Component_List
9877 (Component_List (Type_Definition (Decl)),
9878 Sbit,
9879 Discriminant_Specifications (Decl));
9880 end if;
9881 end;
9882 end Record_Hole_Check;
9883 end if;
9885 -- For records that have component clauses for all components, and whose
9886 -- size is less than or equal to 32, we need to know the size in the
9887 -- front end to activate possible packed array processing where the
9888 -- component type is a record.
9890 -- At this stage Hbit + 1 represents the first unused bit from all the
9891 -- component clauses processed, so if the component clauses are
9892 -- complete, then this is the length of the record.
9894 -- For records longer than System.Storage_Unit, and for those where not
9895 -- all components have component clauses, the back end determines the
9896 -- length (it may for example be appropriate to round up the size
9897 -- to some convenient boundary, based on alignment considerations, etc).
9899 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
9901 -- Nothing to do if at least one component has no component clause
9903 Comp := First_Component_Or_Discriminant (Rectype);
9904 while Present (Comp) loop
9905 exit when No (Component_Clause (Comp));
9906 Next_Component_Or_Discriminant (Comp);
9907 end loop;
9909 -- If we fall out of loop, all components have component clauses
9910 -- and so we can set the size to the maximum value.
9912 if No (Comp) then
9913 Set_RM_Size (Rectype, Hbit + 1);
9914 end if;
9915 end if;
9916 end Check_Record_Representation_Clause;
9918 ----------------
9919 -- Check_Size --
9920 ----------------
9922 procedure Check_Size
9923 (N : Node_Id;
9924 T : Entity_Id;
9925 Siz : Uint;
9926 Biased : out Boolean)
9928 UT : constant Entity_Id := Underlying_Type (T);
9929 M : Uint;
9931 begin
9932 Biased := False;
9934 -- Reject patently improper size values.
9936 if Is_Elementary_Type (T)
9937 and then Siz > UI_From_Int (Int'Last)
9938 then
9939 Error_Msg_N ("Size value too large for elementary type", N);
9941 if Nkind (Original_Node (N)) = N_Op_Expon then
9942 Error_Msg_N
9943 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
9944 end if;
9945 end if;
9947 -- Dismiss generic types
9949 if Is_Generic_Type (T)
9950 or else
9951 Is_Generic_Type (UT)
9952 or else
9953 Is_Generic_Type (Root_Type (UT))
9954 then
9955 return;
9957 -- Guard against previous errors
9959 elsif No (UT) or else UT = Any_Type then
9960 Check_Error_Detected;
9961 return;
9963 -- Check case of bit packed array
9965 elsif Is_Array_Type (UT)
9966 and then Known_Static_Component_Size (UT)
9967 and then Is_Bit_Packed_Array (UT)
9968 then
9969 declare
9970 Asiz : Uint;
9971 Indx : Node_Id;
9972 Ityp : Entity_Id;
9974 begin
9975 Asiz := Component_Size (UT);
9976 Indx := First_Index (UT);
9977 loop
9978 Ityp := Etype (Indx);
9980 -- If non-static bound, then we are not in the business of
9981 -- trying to check the length, and indeed an error will be
9982 -- issued elsewhere, since sizes of non-static array types
9983 -- cannot be set implicitly or explicitly.
9985 if not Is_OK_Static_Subtype (Ityp) then
9986 return;
9987 end if;
9989 -- Otherwise accumulate next dimension
9991 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
9992 Expr_Value (Type_Low_Bound (Ityp)) +
9993 Uint_1);
9995 Next_Index (Indx);
9996 exit when No (Indx);
9997 end loop;
9999 if Asiz <= Siz then
10000 return;
10002 else
10003 Error_Msg_Uint_1 := Asiz;
10004 Error_Msg_NE
10005 ("size for& too small, minimum allowed is ^", N, T);
10006 Set_Esize (T, Asiz);
10007 Set_RM_Size (T, Asiz);
10008 end if;
10009 end;
10011 -- All other composite types are ignored
10013 elsif Is_Composite_Type (UT) then
10014 return;
10016 -- For fixed-point types, don't check minimum if type is not frozen,
10017 -- since we don't know all the characteristics of the type that can
10018 -- affect the size (e.g. a specified small) till freeze time.
10020 elsif Is_Fixed_Point_Type (UT)
10021 and then not Is_Frozen (UT)
10022 then
10023 null;
10025 -- Cases for which a minimum check is required
10027 else
10028 -- Ignore if specified size is correct for the type
10030 if Known_Esize (UT) and then Siz = Esize (UT) then
10031 return;
10032 end if;
10034 -- Otherwise get minimum size
10036 M := UI_From_Int (Minimum_Size (UT));
10038 if Siz < M then
10040 -- Size is less than minimum size, but one possibility remains
10041 -- that we can manage with the new size if we bias the type.
10043 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10045 if Siz < M then
10046 Error_Msg_Uint_1 := M;
10047 Error_Msg_NE
10048 ("size for& too small, minimum allowed is ^", N, T);
10049 Set_Esize (T, M);
10050 Set_RM_Size (T, M);
10051 else
10052 Biased := True;
10053 end if;
10054 end if;
10055 end if;
10056 end Check_Size;
10058 --------------------------
10059 -- Freeze_Entity_Checks --
10060 --------------------------
10062 procedure Freeze_Entity_Checks (N : Node_Id) is
10063 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10064 -- Inspect the primitive operations of type Typ and hide all pairs of
10065 -- implicitly declared non-overridden non-fully conformant homographs
10066 -- (Ada RM 8.3 12.3/2).
10068 -------------------------------------
10069 -- Hide_Non_Overridden_Subprograms --
10070 -------------------------------------
10072 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10073 procedure Hide_Matching_Homographs
10074 (Subp_Id : Entity_Id;
10075 Start_Elmt : Elmt_Id);
10076 -- Inspect a list of primitive operations starting with Start_Elmt
10077 -- and find matching implicitly declared non-overridden non-fully
10078 -- conformant homographs of Subp_Id. If found, all matches along
10079 -- with Subp_Id are hidden from all visibility.
10081 function Is_Non_Overridden_Or_Null_Procedure
10082 (Subp_Id : Entity_Id) return Boolean;
10083 -- Determine whether subprogram Subp_Id is implicitly declared non-
10084 -- overridden subprogram or an implicitly declared null procedure.
10086 ------------------------------
10087 -- Hide_Matching_Homographs --
10088 ------------------------------
10090 procedure Hide_Matching_Homographs
10091 (Subp_Id : Entity_Id;
10092 Start_Elmt : Elmt_Id)
10094 Prim : Entity_Id;
10095 Prim_Elmt : Elmt_Id;
10097 begin
10098 Prim_Elmt := Start_Elmt;
10099 while Present (Prim_Elmt) loop
10100 Prim := Node (Prim_Elmt);
10102 -- The current primitive is implicitly declared non-overridden
10103 -- non-fully conformant homograph of Subp_Id. Both subprograms
10104 -- must be hidden from visibility.
10106 if Chars (Prim) = Chars (Subp_Id)
10107 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10108 and then not Fully_Conformant (Prim, Subp_Id)
10109 then
10110 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10111 Set_Is_Immediately_Visible (Prim, False);
10112 Set_Is_Potentially_Use_Visible (Prim, False);
10114 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10115 Set_Is_Immediately_Visible (Subp_Id, False);
10116 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10117 end if;
10119 Next_Elmt (Prim_Elmt);
10120 end loop;
10121 end Hide_Matching_Homographs;
10123 -----------------------------------------
10124 -- Is_Non_Overridden_Or_Null_Procedure --
10125 -----------------------------------------
10127 function Is_Non_Overridden_Or_Null_Procedure
10128 (Subp_Id : Entity_Id) return Boolean
10130 Alias_Id : Entity_Id;
10132 begin
10133 -- The subprogram is inherited (implicitly declared), it does not
10134 -- override and does not cover a primitive of an interface.
10136 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10137 and then Present (Alias (Subp_Id))
10138 and then No (Interface_Alias (Subp_Id))
10139 and then No (Overridden_Operation (Subp_Id))
10140 then
10141 Alias_Id := Alias (Subp_Id);
10143 if Requires_Overriding (Alias_Id) then
10144 return True;
10146 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10147 and then Null_Present (Parent (Alias_Id))
10148 then
10149 return True;
10150 end if;
10151 end if;
10153 return False;
10154 end Is_Non_Overridden_Or_Null_Procedure;
10156 -- Local variables
10158 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10159 Prim : Entity_Id;
10160 Prim_Elmt : Elmt_Id;
10162 -- Start of processing for Hide_Non_Overridden_Subprograms
10164 begin
10165 -- Inspect the list of primitives looking for non-overridden
10166 -- subprograms.
10168 if Present (Prim_Ops) then
10169 Prim_Elmt := First_Elmt (Prim_Ops);
10170 while Present (Prim_Elmt) loop
10171 Prim := Node (Prim_Elmt);
10172 Next_Elmt (Prim_Elmt);
10174 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10175 Hide_Matching_Homographs
10176 (Subp_Id => Prim,
10177 Start_Elmt => Prim_Elmt);
10178 end if;
10179 end loop;
10180 end if;
10181 end Hide_Non_Overridden_Subprograms;
10183 ---------------------
10184 -- Local variables --
10185 ---------------------
10187 E : constant Entity_Id := Entity (N);
10189 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10190 -- True in non-generic case. Some of the processing here is skipped
10191 -- for the generic case since it is not needed. Basically in the
10192 -- generic case, we only need to do stuff that might generate error
10193 -- messages or warnings.
10195 -- Start of processing for Freeze_Entity_Checks
10197 begin
10198 -- Remember that we are processing a freezing entity. Required to
10199 -- ensure correct decoration of internal entities associated with
10200 -- interfaces (see New_Overloaded_Entity).
10202 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10204 -- For tagged types covering interfaces add internal entities that link
10205 -- the primitives of the interfaces with the primitives that cover them.
10206 -- Note: These entities were originally generated only when generating
10207 -- code because their main purpose was to provide support to initialize
10208 -- the secondary dispatch tables. They are now generated also when
10209 -- compiling with no code generation to provide ASIS the relationship
10210 -- between interface primitives and tagged type primitives. They are
10211 -- also used to locate primitives covering interfaces when processing
10212 -- generics (see Derive_Subprograms).
10214 -- This is not needed in the generic case
10216 if Ada_Version >= Ada_2005
10217 and then Non_Generic_Case
10218 and then Ekind (E) = E_Record_Type
10219 and then Is_Tagged_Type (E)
10220 and then not Is_Interface (E)
10221 and then Has_Interfaces (E)
10222 then
10223 -- This would be a good common place to call the routine that checks
10224 -- overriding of interface primitives (and thus factorize calls to
10225 -- Check_Abstract_Overriding located at different contexts in the
10226 -- compiler). However, this is not possible because it causes
10227 -- spurious errors in case of late overriding.
10229 Add_Internal_Interface_Entities (E);
10230 end if;
10232 -- After all forms of overriding have been resolved, a tagged type may
10233 -- be left with a set of implicitly declared and possibly erroneous
10234 -- abstract subprograms, null procedures and subprograms that require
10235 -- overriding. If this set contains fully conformat homographs, then one
10236 -- is chosen arbitrarily (already done during resolution), otherwise all
10237 -- remaining non-fully conformant homographs are hidden from visibility
10238 -- (Ada RM 8.3 12.3/2).
10240 if Is_Tagged_Type (E) then
10241 Hide_Non_Overridden_Subprograms (E);
10242 end if;
10244 -- Check CPP types
10246 if Ekind (E) = E_Record_Type
10247 and then Is_CPP_Class (E)
10248 and then Is_Tagged_Type (E)
10249 and then Tagged_Type_Expansion
10250 then
10251 if CPP_Num_Prims (E) = 0 then
10253 -- If the CPP type has user defined components then it must import
10254 -- primitives from C++. This is required because if the C++ class
10255 -- has no primitives then the C++ compiler does not added the _tag
10256 -- component to the type.
10258 if First_Entity (E) /= Last_Entity (E) then
10259 Error_Msg_N
10260 ("'C'P'P type must import at least one primitive from C++??",
10262 end if;
10263 end if;
10265 -- Check that all its primitives are abstract or imported from C++.
10266 -- Check also availability of the C++ constructor.
10268 declare
10269 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10270 Elmt : Elmt_Id;
10271 Error_Reported : Boolean := False;
10272 Prim : Node_Id;
10274 begin
10275 Elmt := First_Elmt (Primitive_Operations (E));
10276 while Present (Elmt) loop
10277 Prim := Node (Elmt);
10279 if Comes_From_Source (Prim) then
10280 if Is_Abstract_Subprogram (Prim) then
10281 null;
10283 elsif not Is_Imported (Prim)
10284 or else Convention (Prim) /= Convention_CPP
10285 then
10286 Error_Msg_N
10287 ("primitives of 'C'P'P types must be imported from C++ "
10288 & "or abstract??", Prim);
10290 elsif not Has_Constructors
10291 and then not Error_Reported
10292 then
10293 Error_Msg_Name_1 := Chars (E);
10294 Error_Msg_N
10295 ("??'C'P'P constructor required for type %", Prim);
10296 Error_Reported := True;
10297 end if;
10298 end if;
10300 Next_Elmt (Elmt);
10301 end loop;
10302 end;
10303 end if;
10305 -- Check Ada derivation of CPP type
10307 if Expander_Active -- why? losing errors in -gnatc mode???
10308 and then Tagged_Type_Expansion
10309 and then Ekind (E) = E_Record_Type
10310 and then Etype (E) /= E
10311 and then Is_CPP_Class (Etype (E))
10312 and then CPP_Num_Prims (Etype (E)) > 0
10313 and then not Is_CPP_Class (E)
10314 and then not Has_CPP_Constructors (Etype (E))
10315 then
10316 -- If the parent has C++ primitives but it has no constructor then
10317 -- check that all the primitives are overridden in this derivation;
10318 -- otherwise the constructor of the parent is needed to build the
10319 -- dispatch table.
10321 declare
10322 Elmt : Elmt_Id;
10323 Prim : Node_Id;
10325 begin
10326 Elmt := First_Elmt (Primitive_Operations (E));
10327 while Present (Elmt) loop
10328 Prim := Node (Elmt);
10330 if not Is_Abstract_Subprogram (Prim)
10331 and then No (Interface_Alias (Prim))
10332 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10333 then
10334 Error_Msg_Name_1 := Chars (Etype (E));
10335 Error_Msg_N
10336 ("'C'P'P constructor required for parent type %", E);
10337 exit;
10338 end if;
10340 Next_Elmt (Elmt);
10341 end loop;
10342 end;
10343 end if;
10345 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10347 -- If we have a type with predicates, build predicate function. This
10348 -- is not needed in the generic case, and is not needed within TSS
10349 -- subprograms and other predefined primitives.
10351 if Non_Generic_Case
10352 and then Is_Type (E)
10353 and then Has_Predicates (E)
10354 and then not Within_Internal_Subprogram
10355 then
10356 Build_Predicate_Functions (E, N);
10357 end if;
10359 -- If type has delayed aspects, this is where we do the preanalysis at
10360 -- the freeze point, as part of the consistent visibility check. Note
10361 -- that this must be done after calling Build_Predicate_Functions or
10362 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10363 -- the subtype name in the saved expression so that they will not cause
10364 -- trouble in the preanalysis.
10366 -- This is also not needed in the generic case
10368 if Non_Generic_Case
10369 and then Has_Delayed_Aspects (E)
10370 and then Scope (E) = Current_Scope
10371 then
10372 -- Retrieve the visibility to the discriminants in order to properly
10373 -- analyze the aspects.
10375 Push_Scope_And_Install_Discriminants (E);
10377 declare
10378 Ritem : Node_Id;
10380 begin
10381 -- Look for aspect specification entries for this entity
10383 Ritem := First_Rep_Item (E);
10384 while Present (Ritem) loop
10385 if Nkind (Ritem) = N_Aspect_Specification
10386 and then Entity (Ritem) = E
10387 and then Is_Delayed_Aspect (Ritem)
10388 then
10389 Check_Aspect_At_Freeze_Point (Ritem);
10390 end if;
10392 Next_Rep_Item (Ritem);
10393 end loop;
10394 end;
10396 Uninstall_Discriminants_And_Pop_Scope (E);
10397 end if;
10399 -- For a record type, deal with variant parts. This has to be delayed
10400 -- to this point, because of the issue of statically predicated
10401 -- subtypes, which we have to ensure are frozen before checking
10402 -- choices, since we need to have the static choice list set.
10404 if Is_Record_Type (E) then
10405 Check_Variant_Part : declare
10406 D : constant Node_Id := Declaration_Node (E);
10407 T : Node_Id;
10408 C : Node_Id;
10409 VP : Node_Id;
10411 Others_Present : Boolean;
10412 pragma Warnings (Off, Others_Present);
10413 -- Indicates others present, not used in this case
10415 procedure Non_Static_Choice_Error (Choice : Node_Id);
10416 -- Error routine invoked by the generic instantiation below when
10417 -- the variant part has a non static choice.
10419 procedure Process_Declarations (Variant : Node_Id);
10420 -- Processes declarations associated with a variant. We analyzed
10421 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10422 -- but we still need the recursive call to Check_Choices for any
10423 -- nested variant to get its choices properly processed. This is
10424 -- also where we expand out the choices if expansion is active.
10426 package Variant_Choices_Processing is new
10427 Generic_Check_Choices
10428 (Process_Empty_Choice => No_OP,
10429 Process_Non_Static_Choice => Non_Static_Choice_Error,
10430 Process_Associated_Node => Process_Declarations);
10431 use Variant_Choices_Processing;
10433 -----------------------------
10434 -- Non_Static_Choice_Error --
10435 -----------------------------
10437 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10438 begin
10439 Flag_Non_Static_Expr
10440 ("choice given in variant part is not static!", Choice);
10441 end Non_Static_Choice_Error;
10443 --------------------------
10444 -- Process_Declarations --
10445 --------------------------
10447 procedure Process_Declarations (Variant : Node_Id) is
10448 CL : constant Node_Id := Component_List (Variant);
10449 VP : Node_Id;
10451 begin
10452 -- Check for static predicate present in this variant
10454 if Has_SP_Choice (Variant) then
10456 -- Here we expand. You might expect to find this call in
10457 -- Expand_N_Variant_Part, but that is called when we first
10458 -- see the variant part, and we cannot do this expansion
10459 -- earlier than the freeze point, since for statically
10460 -- predicated subtypes, the predicate is not known till
10461 -- the freeze point.
10463 -- Furthermore, we do this expansion even if the expander
10464 -- is not active, because other semantic processing, e.g.
10465 -- for aggregates, requires the expanded list of choices.
10467 -- If the expander is not active, then we can't just clobber
10468 -- the list since it would invalidate the ASIS -gnatct tree.
10469 -- So we have to rewrite the variant part with a Rewrite
10470 -- call that replaces it with a copy and clobber the copy.
10472 if not Expander_Active then
10473 declare
10474 NewV : constant Node_Id := New_Copy (Variant);
10475 begin
10476 Set_Discrete_Choices
10477 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10478 Rewrite (Variant, NewV);
10479 end;
10480 end if;
10482 Expand_Static_Predicates_In_Choices (Variant);
10483 end if;
10485 -- We don't need to worry about the declarations in the variant
10486 -- (since they were analyzed by Analyze_Choices when we first
10487 -- encountered the variant), but we do need to take care of
10488 -- expansion of any nested variants.
10490 if not Null_Present (CL) then
10491 VP := Variant_Part (CL);
10493 if Present (VP) then
10494 Check_Choices
10495 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10496 end if;
10497 end if;
10498 end Process_Declarations;
10500 -- Start of processing for Check_Variant_Part
10502 begin
10503 -- Find component list
10505 C := Empty;
10507 if Nkind (D) = N_Full_Type_Declaration then
10508 T := Type_Definition (D);
10510 if Nkind (T) = N_Record_Definition then
10511 C := Component_List (T);
10513 elsif Nkind (T) = N_Derived_Type_Definition
10514 and then Present (Record_Extension_Part (T))
10515 then
10516 C := Component_List (Record_Extension_Part (T));
10517 end if;
10518 end if;
10520 -- Case of variant part present
10522 if Present (C) and then Present (Variant_Part (C)) then
10523 VP := Variant_Part (C);
10525 -- Check choices
10527 Check_Choices
10528 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10530 -- If the last variant does not contain the Others choice,
10531 -- replace it with an N_Others_Choice node since Gigi always
10532 -- wants an Others. Note that we do not bother to call Analyze
10533 -- on the modified variant part, since its only effect would be
10534 -- to compute the Others_Discrete_Choices node laboriously, and
10535 -- of course we already know the list of choices corresponding
10536 -- to the others choice (it's the list we're replacing).
10538 -- We only want to do this if the expander is active, since
10539 -- we do not want to clobber the ASIS tree.
10541 if Expander_Active then
10542 declare
10543 Last_Var : constant Node_Id :=
10544 Last_Non_Pragma (Variants (VP));
10546 Others_Node : Node_Id;
10548 begin
10549 if Nkind (First (Discrete_Choices (Last_Var))) /=
10550 N_Others_Choice
10551 then
10552 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10553 Set_Others_Discrete_Choices
10554 (Others_Node, Discrete_Choices (Last_Var));
10555 Set_Discrete_Choices
10556 (Last_Var, New_List (Others_Node));
10557 end if;
10558 end;
10559 end if;
10560 end if;
10561 end Check_Variant_Part;
10562 end if;
10563 end Freeze_Entity_Checks;
10565 -------------------------
10566 -- Get_Alignment_Value --
10567 -------------------------
10569 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10570 Align : constant Uint := Static_Integer (Expr);
10572 begin
10573 if Align = No_Uint then
10574 return No_Uint;
10576 elsif Align <= 0 then
10577 Error_Msg_N ("alignment value must be positive", Expr);
10578 return No_Uint;
10580 else
10581 for J in Int range 0 .. 64 loop
10582 declare
10583 M : constant Uint := Uint_2 ** J;
10585 begin
10586 exit when M = Align;
10588 if M > Align then
10589 Error_Msg_N
10590 ("alignment value must be power of 2", Expr);
10591 return No_Uint;
10592 end if;
10593 end;
10594 end loop;
10596 return Align;
10597 end if;
10598 end Get_Alignment_Value;
10600 -------------------------------------
10601 -- Inherit_Aspects_At_Freeze_Point --
10602 -------------------------------------
10604 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10605 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10606 (Rep_Item : Node_Id) return Boolean;
10607 -- This routine checks if Rep_Item is either a pragma or an aspect
10608 -- specification node whose correponding pragma (if any) is present in
10609 -- the Rep Item chain of the entity it has been specified to.
10611 --------------------------------------------------
10612 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10613 --------------------------------------------------
10615 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10616 (Rep_Item : Node_Id) return Boolean
10618 begin
10619 return
10620 Nkind (Rep_Item) = N_Pragma
10621 or else Present_In_Rep_Item
10622 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10623 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10625 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10627 begin
10628 -- A representation item is either subtype-specific (Size and Alignment
10629 -- clauses) or type-related (all others). Subtype-specific aspects may
10630 -- differ for different subtypes of the same type (RM 13.1.8).
10632 -- A derived type inherits each type-related representation aspect of
10633 -- its parent type that was directly specified before the declaration of
10634 -- the derived type (RM 13.1.15).
10636 -- A derived subtype inherits each subtype-specific representation
10637 -- aspect of its parent subtype that was directly specified before the
10638 -- declaration of the derived type (RM 13.1.15).
10640 -- The general processing involves inheriting a representation aspect
10641 -- from a parent type whenever the first rep item (aspect specification,
10642 -- attribute definition clause, pragma) corresponding to the given
10643 -- representation aspect in the rep item chain of Typ, if any, isn't
10644 -- directly specified to Typ but to one of its parents.
10646 -- ??? Note that, for now, just a limited number of representation
10647 -- aspects have been inherited here so far. Many of them are
10648 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
10649 -- a non- exhaustive list of aspects that likely also need to
10650 -- be moved to this routine: Alignment, Component_Alignment,
10651 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
10652 -- Preelaborable_Initialization, RM_Size and Small.
10654 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
10655 return;
10656 end if;
10658 -- Ada_05/Ada_2005
10660 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
10661 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
10662 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10663 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
10664 then
10665 Set_Is_Ada_2005_Only (Typ);
10666 end if;
10668 -- Ada_12/Ada_2012
10670 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
10671 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
10672 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10673 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
10674 then
10675 Set_Is_Ada_2012_Only (Typ);
10676 end if;
10678 -- Atomic/Shared
10680 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
10681 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
10682 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10683 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
10684 then
10685 Set_Is_Atomic (Typ);
10686 Set_Treat_As_Volatile (Typ);
10687 Set_Is_Volatile (Typ);
10688 end if;
10690 -- Default_Component_Value
10692 if Is_Array_Type (Typ)
10693 and then Is_Base_Type (Typ)
10694 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
10695 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
10696 then
10697 Set_Default_Aspect_Component_Value (Typ,
10698 Default_Aspect_Component_Value
10699 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
10700 end if;
10702 -- Default_Value
10704 if Is_Scalar_Type (Typ)
10705 and then Is_Base_Type (Typ)
10706 and then Has_Rep_Item (Typ, Name_Default_Value, False)
10707 and then Has_Rep_Item (Typ, Name_Default_Value)
10708 then
10709 Set_Default_Aspect_Value (Typ,
10710 Default_Aspect_Value
10711 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
10712 end if;
10714 -- Discard_Names
10716 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
10717 and then Has_Rep_Item (Typ, Name_Discard_Names)
10718 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10719 (Get_Rep_Item (Typ, Name_Discard_Names))
10720 then
10721 Set_Discard_Names (Typ);
10722 end if;
10724 -- Invariants
10726 if not Has_Rep_Item (Typ, Name_Invariant, False)
10727 and then Has_Rep_Item (Typ, Name_Invariant)
10728 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10729 (Get_Rep_Item (Typ, Name_Invariant))
10730 then
10731 Set_Has_Invariants (Typ);
10733 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
10734 Set_Has_Inheritable_Invariants (Typ);
10735 end if;
10737 -- If we have a subtype with invariants, whose base type does not have
10738 -- invariants, copy these invariants to the base type. This happens for
10739 -- the case of implicit base types created for scalar and array types.
10741 elsif Has_Invariants (Typ)
10742 and then not Has_Invariants (Base_Type (Typ))
10743 then
10744 Set_Has_Invariants (Base_Type (Typ));
10745 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
10746 end if;
10748 -- Volatile
10750 if not Has_Rep_Item (Typ, Name_Volatile, False)
10751 and then Has_Rep_Item (Typ, Name_Volatile)
10752 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10753 (Get_Rep_Item (Typ, Name_Volatile))
10754 then
10755 Set_Treat_As_Volatile (Typ);
10756 Set_Is_Volatile (Typ);
10757 end if;
10759 -- Inheritance for derived types only
10761 if Is_Derived_Type (Typ) then
10762 declare
10763 Bas_Typ : constant Entity_Id := Base_Type (Typ);
10764 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
10766 begin
10767 -- Atomic_Components
10769 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
10770 and then Has_Rep_Item (Typ, Name_Atomic_Components)
10771 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10772 (Get_Rep_Item (Typ, Name_Atomic_Components))
10773 then
10774 Set_Has_Atomic_Components (Imp_Bas_Typ);
10775 end if;
10777 -- Volatile_Components
10779 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
10780 and then Has_Rep_Item (Typ, Name_Volatile_Components)
10781 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10782 (Get_Rep_Item (Typ, Name_Volatile_Components))
10783 then
10784 Set_Has_Volatile_Components (Imp_Bas_Typ);
10785 end if;
10787 -- Finalize_Storage_Only.
10789 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
10790 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
10791 then
10792 Set_Finalize_Storage_Only (Bas_Typ);
10793 end if;
10795 -- Universal_Aliasing
10797 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
10798 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
10799 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10800 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
10801 then
10802 Set_Universal_Aliasing (Imp_Bas_Typ);
10803 end if;
10805 -- Record type specific aspects
10807 if Is_Record_Type (Typ) then
10809 -- Bit_Order
10811 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
10812 and then Has_Rep_Item (Typ, Name_Bit_Order)
10813 then
10814 Set_Reverse_Bit_Order (Bas_Typ,
10815 Reverse_Bit_Order (Entity (Name
10816 (Get_Rep_Item (Typ, Name_Bit_Order)))));
10817 end if;
10819 -- Scalar_Storage_Order
10821 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
10822 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
10823 then
10824 Set_Reverse_Storage_Order (Bas_Typ,
10825 Reverse_Storage_Order (Entity (Name
10826 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
10828 -- Clear default SSO indications, since the inherited aspect
10829 -- which was set explicitly overrides the default.
10831 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
10832 Set_SSO_Set_High_By_Default (Bas_Typ, False);
10833 end if;
10834 end if;
10835 end;
10836 end if;
10837 end Inherit_Aspects_At_Freeze_Point;
10839 ----------------
10840 -- Initialize --
10841 ----------------
10843 procedure Initialize is
10844 begin
10845 Address_Clause_Checks.Init;
10846 Independence_Checks.Init;
10847 Unchecked_Conversions.Init;
10848 end Initialize;
10850 ---------------------------
10851 -- Install_Discriminants --
10852 ---------------------------
10854 procedure Install_Discriminants (E : Entity_Id) is
10855 Disc : Entity_Id;
10856 Prev : Entity_Id;
10857 begin
10858 Disc := First_Discriminant (E);
10859 while Present (Disc) loop
10860 Prev := Current_Entity (Disc);
10861 Set_Current_Entity (Disc);
10862 Set_Is_Immediately_Visible (Disc);
10863 Set_Homonym (Disc, Prev);
10864 Next_Discriminant (Disc);
10865 end loop;
10866 end Install_Discriminants;
10868 -------------------------
10869 -- Is_Operational_Item --
10870 -------------------------
10872 function Is_Operational_Item (N : Node_Id) return Boolean is
10873 begin
10874 if Nkind (N) /= N_Attribute_Definition_Clause then
10875 return False;
10877 else
10878 declare
10879 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
10880 begin
10881 return Id = Attribute_Input
10882 or else Id = Attribute_Output
10883 or else Id = Attribute_Read
10884 or else Id = Attribute_Write
10885 or else Id = Attribute_External_Tag;
10886 end;
10887 end if;
10888 end Is_Operational_Item;
10890 -------------------------
10891 -- Is_Predicate_Static --
10892 -------------------------
10894 -- Note: the basic legality of the expression has already been checked, so
10895 -- we don't need to worry about cases or ranges on strings for example.
10897 function Is_Predicate_Static
10898 (Expr : Node_Id;
10899 Nam : Name_Id) return Boolean
10901 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
10902 -- Given a list of case expression alternatives, returns True if all
10903 -- the alternatives are static (have all static choices, and a static
10904 -- expression).
10906 function All_Static_Choices (L : List_Id) return Boolean;
10907 -- Returns true if all elements of the list are OK static choices
10908 -- as defined below for Is_Static_Choice. Used for case expression
10909 -- alternatives and for the right operand of a membership test. An
10910 -- others_choice is static if the corresponding expression is static.
10911 -- The staticness of the bounds is checked separately.
10913 function Is_Static_Choice (N : Node_Id) return Boolean;
10914 -- Returns True if N represents a static choice (static subtype, or
10915 -- static subtype indication, or static expression, or static range).
10917 -- Note that this is a bit more inclusive than we actually need
10918 -- (in particular membership tests do not allow the use of subtype
10919 -- indications). But that doesn't matter, we have already checked
10920 -- that the construct is legal to get this far.
10922 function Is_Type_Ref (N : Node_Id) return Boolean;
10923 pragma Inline (Is_Type_Ref);
10924 -- Returns True if N is a reference to the type for the predicate in the
10925 -- expression (i.e. if it is an identifier whose Chars field matches the
10926 -- Nam given in the call). N must not be parenthesized, if the type name
10927 -- appears in parens, this routine will return False.
10929 ----------------------------------
10930 -- All_Static_Case_Alternatives --
10931 ----------------------------------
10933 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
10934 N : Node_Id;
10936 begin
10937 N := First (L);
10938 while Present (N) loop
10939 if not (All_Static_Choices (Discrete_Choices (N))
10940 and then Is_OK_Static_Expression (Expression (N)))
10941 then
10942 return False;
10943 end if;
10945 Next (N);
10946 end loop;
10948 return True;
10949 end All_Static_Case_Alternatives;
10951 ------------------------
10952 -- All_Static_Choices --
10953 ------------------------
10955 function All_Static_Choices (L : List_Id) return Boolean is
10956 N : Node_Id;
10958 begin
10959 N := First (L);
10960 while Present (N) loop
10961 if not Is_Static_Choice (N) then
10962 return False;
10963 end if;
10965 Next (N);
10966 end loop;
10968 return True;
10969 end All_Static_Choices;
10971 ----------------------
10972 -- Is_Static_Choice --
10973 ----------------------
10975 function Is_Static_Choice (N : Node_Id) return Boolean is
10976 begin
10977 return Nkind (N) = N_Others_Choice
10978 or else Is_OK_Static_Expression (N)
10979 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
10980 and then Is_OK_Static_Subtype (Entity (N)))
10981 or else (Nkind (N) = N_Subtype_Indication
10982 and then Is_OK_Static_Subtype (Entity (N)))
10983 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
10984 end Is_Static_Choice;
10986 -----------------
10987 -- Is_Type_Ref --
10988 -----------------
10990 function Is_Type_Ref (N : Node_Id) return Boolean is
10991 begin
10992 return Nkind (N) = N_Identifier
10993 and then Chars (N) = Nam
10994 and then Paren_Count (N) = 0;
10995 end Is_Type_Ref;
10997 -- Start of processing for Is_Predicate_Static
10999 begin
11000 -- Predicate_Static means one of the following holds. Numbers are the
11001 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11003 -- 16: A static expression
11005 if Is_OK_Static_Expression (Expr) then
11006 return True;
11008 -- 17: A membership test whose simple_expression is the current
11009 -- instance, and whose membership_choice_list meets the requirements
11010 -- for a static membership test.
11012 elsif Nkind (Expr) in N_Membership_Test
11013 and then ((Present (Right_Opnd (Expr))
11014 and then Is_Static_Choice (Right_Opnd (Expr)))
11015 or else
11016 (Present (Alternatives (Expr))
11017 and then All_Static_Choices (Alternatives (Expr))))
11018 then
11019 return True;
11021 -- 18. A case_expression whose selecting_expression is the current
11022 -- instance, and whose dependent expressions are static expressions.
11024 elsif Nkind (Expr) = N_Case_Expression
11025 and then Is_Type_Ref (Expression (Expr))
11026 and then All_Static_Case_Alternatives (Alternatives (Expr))
11027 then
11028 return True;
11030 -- 19. A call to a predefined equality or ordering operator, where one
11031 -- operand is the current instance, and the other is a static
11032 -- expression.
11034 -- Note: the RM is clearly wrong here in not excluding string types.
11035 -- Without this exclusion, we would allow expressions like X > "ABC"
11036 -- to be considered as predicate-static, which is clearly not intended,
11037 -- since the idea is for predicate-static to be a subset of normal
11038 -- static expressions (and "DEF" > "ABC" is not a static expression).
11040 -- However, we do allow internally generated (not from source) equality
11041 -- and inequality operations to be valid on strings (this helps deal
11042 -- with cases where we transform A in "ABC" to A = "ABC).
11044 elsif Nkind (Expr) in N_Op_Compare
11045 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11046 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11047 and then not Comes_From_Source (Expr)))
11048 and then ((Is_Type_Ref (Left_Opnd (Expr))
11049 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11050 or else
11051 (Is_Type_Ref (Right_Opnd (Expr))
11052 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11053 then
11054 return True;
11056 -- 20. A call to a predefined boolean logical operator, where each
11057 -- operand is predicate-static.
11059 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11060 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11061 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11062 or else
11063 (Nkind (Expr) = N_Op_Not
11064 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11065 then
11066 return True;
11068 -- 21. A short-circuit control form where both operands are
11069 -- predicate-static.
11071 elsif Nkind (Expr) in N_Short_Circuit
11072 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11073 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11074 then
11075 return True;
11077 -- 22. A parenthesized predicate-static expression. This does not
11078 -- require any special test, since we just ignore paren levels in
11079 -- all the cases above.
11081 -- One more test that is an implementation artifact caused by the fact
11082 -- that we are analyzing not the original expression, but the generated
11083 -- expression in the body of the predicate function. This can include
11084 -- references to inherited predicates, so that the expression we are
11085 -- processing looks like:
11087 -- expression and then xxPredicate (typ (Inns))
11089 -- Where the call is to a Predicate function for an inherited predicate.
11090 -- We simply ignore such a call (which could be to either a dynamic or
11091 -- a static predicate, but remember that we can have a Static_Predicate
11092 -- for a non-static subtype).
11094 elsif Nkind (Expr) = N_Function_Call
11095 and then Is_Predicate_Function (Entity (Name (Expr)))
11096 then
11097 return True;
11099 -- That's an exhaustive list of tests, all other cases are not
11100 -- predicate-static, so we return False.
11102 else
11103 return False;
11104 end if;
11105 end Is_Predicate_Static;
11107 ---------------------
11108 -- Kill_Rep_Clause --
11109 ---------------------
11111 procedure Kill_Rep_Clause (N : Node_Id) is
11112 begin
11113 pragma Assert (Ignore_Rep_Clauses);
11115 -- Note: we use Replace rather than Rewrite, because we don't want
11116 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11117 -- rep clause that is being replaced.
11119 Replace (N, Make_Null_Statement (Sloc (N)));
11121 -- The null statement must be marked as not coming from source. This is
11122 -- so that ASIS ignores it, and also the back end does not expect bogus
11123 -- "from source" null statements in weird places (e.g. in declarative
11124 -- regions where such null statements are not allowed).
11126 Set_Comes_From_Source (N, False);
11127 end Kill_Rep_Clause;
11129 ------------------
11130 -- Minimum_Size --
11131 ------------------
11133 function Minimum_Size
11134 (T : Entity_Id;
11135 Biased : Boolean := False) return Nat
11137 Lo : Uint := No_Uint;
11138 Hi : Uint := No_Uint;
11139 LoR : Ureal := No_Ureal;
11140 HiR : Ureal := No_Ureal;
11141 LoSet : Boolean := False;
11142 HiSet : Boolean := False;
11143 B : Uint;
11144 S : Nat;
11145 Ancest : Entity_Id;
11146 R_Typ : constant Entity_Id := Root_Type (T);
11148 begin
11149 -- If bad type, return 0
11151 if T = Any_Type then
11152 return 0;
11154 -- For generic types, just return zero. There cannot be any legitimate
11155 -- need to know such a size, but this routine may be called with a
11156 -- generic type as part of normal processing.
11158 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11159 return 0;
11161 -- Access types (cannot have size smaller than System.Address)
11163 elsif Is_Access_Type (T) then
11164 return System_Address_Size;
11166 -- Floating-point types
11168 elsif Is_Floating_Point_Type (T) then
11169 return UI_To_Int (Esize (R_Typ));
11171 -- Discrete types
11173 elsif Is_Discrete_Type (T) then
11175 -- The following loop is looking for the nearest compile time known
11176 -- bounds following the ancestor subtype chain. The idea is to find
11177 -- the most restrictive known bounds information.
11179 Ancest := T;
11180 loop
11181 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11182 return 0;
11183 end if;
11185 if not LoSet then
11186 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11187 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11188 LoSet := True;
11189 exit when HiSet;
11190 end if;
11191 end if;
11193 if not HiSet then
11194 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11195 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11196 HiSet := True;
11197 exit when LoSet;
11198 end if;
11199 end if;
11201 Ancest := Ancestor_Subtype (Ancest);
11203 if No (Ancest) then
11204 Ancest := Base_Type (T);
11206 if Is_Generic_Type (Ancest) then
11207 return 0;
11208 end if;
11209 end if;
11210 end loop;
11212 -- Fixed-point types. We can't simply use Expr_Value to get the
11213 -- Corresponding_Integer_Value values of the bounds, since these do not
11214 -- get set till the type is frozen, and this routine can be called
11215 -- before the type is frozen. Similarly the test for bounds being static
11216 -- needs to include the case where we have unanalyzed real literals for
11217 -- the same reason.
11219 elsif Is_Fixed_Point_Type (T) then
11221 -- The following loop is looking for the nearest compile time known
11222 -- bounds following the ancestor subtype chain. The idea is to find
11223 -- the most restrictive known bounds information.
11225 Ancest := T;
11226 loop
11227 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11228 return 0;
11229 end if;
11231 -- Note: In the following two tests for LoSet and HiSet, it may
11232 -- seem redundant to test for N_Real_Literal here since normally
11233 -- one would assume that the test for the value being known at
11234 -- compile time includes this case. However, there is a glitch.
11235 -- If the real literal comes from folding a non-static expression,
11236 -- then we don't consider any non- static expression to be known
11237 -- at compile time if we are in configurable run time mode (needed
11238 -- in some cases to give a clearer definition of what is and what
11239 -- is not accepted). So the test is indeed needed. Without it, we
11240 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11242 if not LoSet then
11243 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11244 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11245 then
11246 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11247 LoSet := True;
11248 exit when HiSet;
11249 end if;
11250 end if;
11252 if not HiSet then
11253 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11254 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11255 then
11256 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11257 HiSet := True;
11258 exit when LoSet;
11259 end if;
11260 end if;
11262 Ancest := Ancestor_Subtype (Ancest);
11264 if No (Ancest) then
11265 Ancest := Base_Type (T);
11267 if Is_Generic_Type (Ancest) then
11268 return 0;
11269 end if;
11270 end if;
11271 end loop;
11273 Lo := UR_To_Uint (LoR / Small_Value (T));
11274 Hi := UR_To_Uint (HiR / Small_Value (T));
11276 -- No other types allowed
11278 else
11279 raise Program_Error;
11280 end if;
11282 -- Fall through with Hi and Lo set. Deal with biased case
11284 if (Biased
11285 and then not Is_Fixed_Point_Type (T)
11286 and then not (Is_Enumeration_Type (T)
11287 and then Has_Non_Standard_Rep (T)))
11288 or else Has_Biased_Representation (T)
11289 then
11290 Hi := Hi - Lo;
11291 Lo := Uint_0;
11292 end if;
11294 -- Signed case. Note that we consider types like range 1 .. -1 to be
11295 -- signed for the purpose of computing the size, since the bounds have
11296 -- to be accommodated in the base type.
11298 if Lo < 0 or else Hi < 0 then
11299 S := 1;
11300 B := Uint_1;
11302 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11303 -- Note that we accommodate the case where the bounds cross. This
11304 -- can happen either because of the way the bounds are declared
11305 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11307 while Lo < -B
11308 or else Hi < -B
11309 or else Lo >= B
11310 or else Hi >= B
11311 loop
11312 B := Uint_2 ** S;
11313 S := S + 1;
11314 end loop;
11316 -- Unsigned case
11318 else
11319 -- If both bounds are positive, make sure that both are represen-
11320 -- table in the case where the bounds are crossed. This can happen
11321 -- either because of the way the bounds are declared, or because of
11322 -- the algorithm in Freeze_Fixed_Point_Type.
11324 if Lo > Hi then
11325 Hi := Lo;
11326 end if;
11328 -- S = size, (can accommodate 0 .. (2**size - 1))
11330 S := 0;
11331 while Hi >= Uint_2 ** S loop
11332 S := S + 1;
11333 end loop;
11334 end if;
11336 return S;
11337 end Minimum_Size;
11339 ---------------------------
11340 -- New_Stream_Subprogram --
11341 ---------------------------
11343 procedure New_Stream_Subprogram
11344 (N : Node_Id;
11345 Ent : Entity_Id;
11346 Subp : Entity_Id;
11347 Nam : TSS_Name_Type)
11349 Loc : constant Source_Ptr := Sloc (N);
11350 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11351 Subp_Id : Entity_Id;
11352 Subp_Decl : Node_Id;
11353 F : Entity_Id;
11354 Etyp : Entity_Id;
11356 Defer_Declaration : constant Boolean :=
11357 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11358 -- For a tagged type, there is a declaration for each stream attribute
11359 -- at the freeze point, and we must generate only a completion of this
11360 -- declaration. We do the same for private types, because the full view
11361 -- might be tagged. Otherwise we generate a declaration at the point of
11362 -- the attribute definition clause.
11364 function Build_Spec return Node_Id;
11365 -- Used for declaration and renaming declaration, so that this is
11366 -- treated as a renaming_as_body.
11368 ----------------
11369 -- Build_Spec --
11370 ----------------
11372 function Build_Spec return Node_Id is
11373 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11374 Formals : List_Id;
11375 Spec : Node_Id;
11376 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11378 begin
11379 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11381 -- S : access Root_Stream_Type'Class
11383 Formals := New_List (
11384 Make_Parameter_Specification (Loc,
11385 Defining_Identifier =>
11386 Make_Defining_Identifier (Loc, Name_S),
11387 Parameter_Type =>
11388 Make_Access_Definition (Loc,
11389 Subtype_Mark =>
11390 New_Occurrence_Of (
11391 Designated_Type (Etype (F)), Loc))));
11393 if Nam = TSS_Stream_Input then
11394 Spec :=
11395 Make_Function_Specification (Loc,
11396 Defining_Unit_Name => Subp_Id,
11397 Parameter_Specifications => Formals,
11398 Result_Definition => T_Ref);
11399 else
11400 -- V : [out] T
11402 Append_To (Formals,
11403 Make_Parameter_Specification (Loc,
11404 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11405 Out_Present => Out_P,
11406 Parameter_Type => T_Ref));
11408 Spec :=
11409 Make_Procedure_Specification (Loc,
11410 Defining_Unit_Name => Subp_Id,
11411 Parameter_Specifications => Formals);
11412 end if;
11414 return Spec;
11415 end Build_Spec;
11417 -- Start of processing for New_Stream_Subprogram
11419 begin
11420 F := First_Formal (Subp);
11422 if Ekind (Subp) = E_Procedure then
11423 Etyp := Etype (Next_Formal (F));
11424 else
11425 Etyp := Etype (Subp);
11426 end if;
11428 -- Prepare subprogram declaration and insert it as an action on the
11429 -- clause node. The visibility for this entity is used to test for
11430 -- visibility of the attribute definition clause (in the sense of
11431 -- 8.3(23) as amended by AI-195).
11433 if not Defer_Declaration then
11434 Subp_Decl :=
11435 Make_Subprogram_Declaration (Loc,
11436 Specification => Build_Spec);
11438 -- For a tagged type, there is always a visible declaration for each
11439 -- stream TSS (it is a predefined primitive operation), and the
11440 -- completion of this declaration occurs at the freeze point, which is
11441 -- not always visible at places where the attribute definition clause is
11442 -- visible. So, we create a dummy entity here for the purpose of
11443 -- tracking the visibility of the attribute definition clause itself.
11445 else
11446 Subp_Id :=
11447 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11448 Subp_Decl :=
11449 Make_Object_Declaration (Loc,
11450 Defining_Identifier => Subp_Id,
11451 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11452 end if;
11454 Insert_Action (N, Subp_Decl);
11455 Set_Entity (N, Subp_Id);
11457 Subp_Decl :=
11458 Make_Subprogram_Renaming_Declaration (Loc,
11459 Specification => Build_Spec,
11460 Name => New_Occurrence_Of (Subp, Loc));
11462 if Defer_Declaration then
11463 Set_TSS (Base_Type (Ent), Subp_Id);
11464 else
11465 Insert_Action (N, Subp_Decl);
11466 Copy_TSS (Subp_Id, Base_Type (Ent));
11467 end if;
11468 end New_Stream_Subprogram;
11470 ------------------------------------------
11471 -- Push_Scope_And_Install_Discriminants --
11472 ------------------------------------------
11474 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11475 begin
11476 if Has_Discriminants (E) then
11477 Push_Scope (E);
11479 -- Make discriminants visible for type declarations and protected
11480 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11482 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11483 Install_Discriminants (E);
11484 end if;
11485 end if;
11486 end Push_Scope_And_Install_Discriminants;
11488 ------------------------
11489 -- Rep_Item_Too_Early --
11490 ------------------------
11492 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
11493 begin
11494 -- Cannot apply non-operational rep items to generic types
11496 if Is_Operational_Item (N) then
11497 return False;
11499 elsif Is_Type (T)
11500 and then Is_Generic_Type (Root_Type (T))
11501 then
11502 Error_Msg_N ("representation item not allowed for generic type", N);
11503 return True;
11504 end if;
11506 -- Otherwise check for incomplete type
11508 if Is_Incomplete_Or_Private_Type (T)
11509 and then No (Underlying_Type (T))
11510 and then
11511 (Nkind (N) /= N_Pragma
11512 or else Get_Pragma_Id (N) /= Pragma_Import)
11513 then
11514 Error_Msg_N
11515 ("representation item must be after full type declaration", N);
11516 return True;
11518 -- If the type has incomplete components, a representation clause is
11519 -- illegal but stream attributes and Convention pragmas are correct.
11521 elsif Has_Private_Component (T) then
11522 if Nkind (N) = N_Pragma then
11523 return False;
11525 else
11526 Error_Msg_N
11527 ("representation item must appear after type is fully defined",
11529 return True;
11530 end if;
11531 else
11532 return False;
11533 end if;
11534 end Rep_Item_Too_Early;
11536 -----------------------
11537 -- Rep_Item_Too_Late --
11538 -----------------------
11540 function Rep_Item_Too_Late
11541 (T : Entity_Id;
11542 N : Node_Id;
11543 FOnly : Boolean := False) return Boolean
11545 S : Entity_Id;
11546 Parent_Type : Entity_Id;
11548 procedure No_Type_Rep_Item;
11549 -- Output message indicating that no type-related aspects can be
11550 -- specified due to some property of the parent type.
11552 procedure Too_Late;
11553 -- Output message for an aspect being specified too late
11555 -- Note that neither of the above errors is considered a serious one,
11556 -- since the effect is simply that we ignore the representation clause
11557 -- in these cases.
11558 -- Is this really true? In any case if we make this change we must
11559 -- document the requirement in the spec of Rep_Item_Too_Late that
11560 -- if True is returned, then the rep item must be completely ignored???
11562 ----------------------
11563 -- No_Type_Rep_Item --
11564 ----------------------
11566 procedure No_Type_Rep_Item is
11567 begin
11568 Error_Msg_N ("|type-related representation item not permitted!", N);
11569 end No_Type_Rep_Item;
11571 --------------
11572 -- Too_Late --
11573 --------------
11575 procedure Too_Late is
11576 begin
11577 -- Other compilers seem more relaxed about rep items appearing too
11578 -- late. Since analysis tools typically don't care about rep items
11579 -- anyway, no reason to be too strict about this.
11581 if not Relaxed_RM_Semantics then
11582 Error_Msg_N ("|representation item appears too late!", N);
11583 end if;
11584 end Too_Late;
11586 -- Start of processing for Rep_Item_Too_Late
11588 begin
11589 -- First make sure entity is not frozen (RM 13.1(9))
11591 if Is_Frozen (T)
11593 -- Exclude imported types, which may be frozen if they appear in a
11594 -- representation clause for a local type.
11596 and then not From_Limited_With (T)
11598 -- Exclude generated entities (not coming from source). The common
11599 -- case is when we generate a renaming which prematurely freezes the
11600 -- renamed internal entity, but we still want to be able to set copies
11601 -- of attribute values such as Size/Alignment.
11603 and then Comes_From_Source (T)
11604 then
11605 Too_Late;
11606 S := First_Subtype (T);
11608 if Present (Freeze_Node (S)) then
11609 if not Relaxed_RM_Semantics then
11610 Error_Msg_NE
11611 ("??no more representation items for }", Freeze_Node (S), S);
11612 end if;
11613 end if;
11615 return True;
11617 -- Check for case of untagged derived type whose parent either has
11618 -- primitive operations, or is a by reference type (RM 13.1(10)). In
11619 -- this case we do not output a Too_Late message, since there is no
11620 -- earlier point where the rep item could be placed to make it legal.
11622 elsif Is_Type (T)
11623 and then not FOnly
11624 and then Is_Derived_Type (T)
11625 and then not Is_Tagged_Type (T)
11626 then
11627 Parent_Type := Etype (Base_Type (T));
11629 if Has_Primitive_Operations (Parent_Type) then
11630 No_Type_Rep_Item;
11632 if not Relaxed_RM_Semantics then
11633 Error_Msg_NE
11634 ("\parent type & has primitive operations!", N, Parent_Type);
11635 end if;
11637 return True;
11639 elsif Is_By_Reference_Type (Parent_Type) then
11640 No_Type_Rep_Item;
11642 if not Relaxed_RM_Semantics then
11643 Error_Msg_NE
11644 ("\parent type & is a by reference type!", N, Parent_Type);
11645 end if;
11647 return True;
11648 end if;
11649 end if;
11651 -- No error, but one more warning to consider. The RM (surprisingly)
11652 -- allows this pattern:
11654 -- type S is ...
11655 -- primitive operations for S
11656 -- type R is new S;
11657 -- rep clause for S
11659 -- Meaning that calls on the primitive operations of S for values of
11660 -- type R may require possibly expensive implicit conversion operations.
11661 -- This is not an error, but is worth a warning.
11663 if not Relaxed_RM_Semantics and then Is_Type (T) then
11664 declare
11665 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
11667 begin
11668 if Present (DTL)
11669 and then Has_Primitive_Operations (Base_Type (T))
11671 -- For now, do not generate this warning for the case of aspect
11672 -- specification using Ada 2012 syntax, since we get wrong
11673 -- messages we do not understand. The whole business of derived
11674 -- types and rep items seems a bit confused when aspects are
11675 -- used, since the aspects are not evaluated till freeze time.
11677 and then not From_Aspect_Specification (N)
11678 then
11679 Error_Msg_Sloc := Sloc (DTL);
11680 Error_Msg_N
11681 ("representation item for& appears after derived type "
11682 & "declaration#??", N);
11683 Error_Msg_NE
11684 ("\may result in implicit conversions for primitive "
11685 & "operations of&??", N, T);
11686 Error_Msg_NE
11687 ("\to change representations when called with arguments "
11688 & "of type&??", N, DTL);
11689 end if;
11690 end;
11691 end if;
11693 -- No error, link item into head of chain of rep items for the entity,
11694 -- but avoid chaining if we have an overloadable entity, and the pragma
11695 -- is one that can apply to multiple overloaded entities.
11697 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
11698 declare
11699 Pname : constant Name_Id := Pragma_Name (N);
11700 begin
11701 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
11702 Name_External, Name_Interface)
11703 then
11704 return False;
11705 end if;
11706 end;
11707 end if;
11709 Record_Rep_Item (T, N);
11710 return False;
11711 end Rep_Item_Too_Late;
11713 -------------------------------------
11714 -- Replace_Type_References_Generic --
11715 -------------------------------------
11717 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
11718 TName : constant Name_Id := Chars (T);
11720 function Replace_Node (N : Node_Id) return Traverse_Result;
11721 -- Processes a single node in the traversal procedure below, checking
11722 -- if node N should be replaced, and if so, doing the replacement.
11724 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
11725 -- This instantiation provides the body of Replace_Type_References
11727 ------------------
11728 -- Replace_Node --
11729 ------------------
11731 function Replace_Node (N : Node_Id) return Traverse_Result is
11732 S : Entity_Id;
11733 P : Node_Id;
11735 begin
11736 -- Case of identifier
11738 if Nkind (N) = N_Identifier then
11740 -- If not the type name, check whether it is a reference to
11741 -- some other type, which must be frozen before the predicate
11742 -- function is analyzed, i.e. before the freeze node of the
11743 -- type to which the predicate applies.
11745 if Chars (N) /= TName then
11746 if Present (Current_Entity (N))
11747 and then Is_Type (Current_Entity (N))
11748 then
11749 Freeze_Before (Freeze_Node (T), Current_Entity (N));
11750 end if;
11752 return Skip;
11754 -- Otherwise do the replacement and we are done with this node
11756 else
11757 Replace_Type_Reference (N);
11758 return Skip;
11759 end if;
11761 -- Case of selected component (which is what a qualification
11762 -- looks like in the unanalyzed tree, which is what we have.
11764 elsif Nkind (N) = N_Selected_Component then
11766 -- If selector name is not our type, keeping going (we might
11767 -- still have an occurrence of the type in the prefix).
11769 if Nkind (Selector_Name (N)) /= N_Identifier
11770 or else Chars (Selector_Name (N)) /= TName
11771 then
11772 return OK;
11774 -- Selector name is our type, check qualification
11776 else
11777 -- Loop through scopes and prefixes, doing comparison
11779 S := Current_Scope;
11780 P := Prefix (N);
11781 loop
11782 -- Continue if no more scopes or scope with no name
11784 if No (S) or else Nkind (S) not in N_Has_Chars then
11785 return OK;
11786 end if;
11788 -- Do replace if prefix is an identifier matching the
11789 -- scope that we are currently looking at.
11791 if Nkind (P) = N_Identifier
11792 and then Chars (P) = Chars (S)
11793 then
11794 Replace_Type_Reference (N);
11795 return Skip;
11796 end if;
11798 -- Go check scope above us if prefix is itself of the
11799 -- form of a selected component, whose selector matches
11800 -- the scope we are currently looking at.
11802 if Nkind (P) = N_Selected_Component
11803 and then Nkind (Selector_Name (P)) = N_Identifier
11804 and then Chars (Selector_Name (P)) = Chars (S)
11805 then
11806 S := Scope (S);
11807 P := Prefix (P);
11809 -- For anything else, we don't have a match, so keep on
11810 -- going, there are still some weird cases where we may
11811 -- still have a replacement within the prefix.
11813 else
11814 return OK;
11815 end if;
11816 end loop;
11817 end if;
11819 -- Continue for any other node kind
11821 else
11822 return OK;
11823 end if;
11824 end Replace_Node;
11826 begin
11827 Replace_Type_Refs (N);
11828 end Replace_Type_References_Generic;
11830 -------------------------
11831 -- Same_Representation --
11832 -------------------------
11834 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
11835 T1 : constant Entity_Id := Underlying_Type (Typ1);
11836 T2 : constant Entity_Id := Underlying_Type (Typ2);
11838 begin
11839 -- A quick check, if base types are the same, then we definitely have
11840 -- the same representation, because the subtype specific representation
11841 -- attributes (Size and Alignment) do not affect representation from
11842 -- the point of view of this test.
11844 if Base_Type (T1) = Base_Type (T2) then
11845 return True;
11847 elsif Is_Private_Type (Base_Type (T2))
11848 and then Base_Type (T1) = Full_View (Base_Type (T2))
11849 then
11850 return True;
11851 end if;
11853 -- Tagged types never have differing representations
11855 if Is_Tagged_Type (T1) then
11856 return True;
11857 end if;
11859 -- Representations are definitely different if conventions differ
11861 if Convention (T1) /= Convention (T2) then
11862 return False;
11863 end if;
11865 -- Representations are different if component alignments or scalar
11866 -- storage orders differ.
11868 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
11869 and then
11870 (Is_Record_Type (T2) or else Is_Array_Type (T2))
11871 and then
11872 (Component_Alignment (T1) /= Component_Alignment (T2)
11873 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
11874 then
11875 return False;
11876 end if;
11878 -- For arrays, the only real issue is component size. If we know the
11879 -- component size for both arrays, and it is the same, then that's
11880 -- good enough to know we don't have a change of representation.
11882 if Is_Array_Type (T1) then
11883 if Known_Component_Size (T1)
11884 and then Known_Component_Size (T2)
11885 and then Component_Size (T1) = Component_Size (T2)
11886 then
11887 if VM_Target = No_VM then
11888 return True;
11890 -- In VM targets the representation of arrays with aliased
11891 -- components differs from arrays with non-aliased components
11893 else
11894 return Has_Aliased_Components (Base_Type (T1))
11896 Has_Aliased_Components (Base_Type (T2));
11897 end if;
11898 end if;
11899 end if;
11901 -- Types definitely have same representation if neither has non-standard
11902 -- representation since default representations are always consistent.
11903 -- If only one has non-standard representation, and the other does not,
11904 -- then we consider that they do not have the same representation. They
11905 -- might, but there is no way of telling early enough.
11907 if Has_Non_Standard_Rep (T1) then
11908 if not Has_Non_Standard_Rep (T2) then
11909 return False;
11910 end if;
11911 else
11912 return not Has_Non_Standard_Rep (T2);
11913 end if;
11915 -- Here the two types both have non-standard representation, and we need
11916 -- to determine if they have the same non-standard representation.
11918 -- For arrays, we simply need to test if the component sizes are the
11919 -- same. Pragma Pack is reflected in modified component sizes, so this
11920 -- check also deals with pragma Pack.
11922 if Is_Array_Type (T1) then
11923 return Component_Size (T1) = Component_Size (T2);
11925 -- Tagged types always have the same representation, because it is not
11926 -- possible to specify different representations for common fields.
11928 elsif Is_Tagged_Type (T1) then
11929 return True;
11931 -- Case of record types
11933 elsif Is_Record_Type (T1) then
11935 -- Packed status must conform
11937 if Is_Packed (T1) /= Is_Packed (T2) then
11938 return False;
11940 -- Otherwise we must check components. Typ2 maybe a constrained
11941 -- subtype with fewer components, so we compare the components
11942 -- of the base types.
11944 else
11945 Record_Case : declare
11946 CD1, CD2 : Entity_Id;
11948 function Same_Rep return Boolean;
11949 -- CD1 and CD2 are either components or discriminants. This
11950 -- function tests whether they have the same representation.
11952 --------------
11953 -- Same_Rep --
11954 --------------
11956 function Same_Rep return Boolean is
11957 begin
11958 if No (Component_Clause (CD1)) then
11959 return No (Component_Clause (CD2));
11960 else
11961 -- Note: at this point, component clauses have been
11962 -- normalized to the default bit order, so that the
11963 -- comparison of Component_Bit_Offsets is meaningful.
11965 return
11966 Present (Component_Clause (CD2))
11967 and then
11968 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
11969 and then
11970 Esize (CD1) = Esize (CD2);
11971 end if;
11972 end Same_Rep;
11974 -- Start of processing for Record_Case
11976 begin
11977 if Has_Discriminants (T1) then
11979 -- The number of discriminants may be different if the
11980 -- derived type has fewer (constrained by values). The
11981 -- invisible discriminants retain the representation of
11982 -- the original, so the discrepancy does not per se
11983 -- indicate a different representation.
11985 CD1 := First_Discriminant (T1);
11986 CD2 := First_Discriminant (T2);
11987 while Present (CD1) and then Present (CD2) loop
11988 if not Same_Rep then
11989 return False;
11990 else
11991 Next_Discriminant (CD1);
11992 Next_Discriminant (CD2);
11993 end if;
11994 end loop;
11995 end if;
11997 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
11998 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
11999 while Present (CD1) loop
12000 if not Same_Rep then
12001 return False;
12002 else
12003 Next_Component (CD1);
12004 Next_Component (CD2);
12005 end if;
12006 end loop;
12008 return True;
12009 end Record_Case;
12010 end if;
12012 -- For enumeration types, we must check each literal to see if the
12013 -- representation is the same. Note that we do not permit enumeration
12014 -- representation clauses for Character and Wide_Character, so these
12015 -- cases were already dealt with.
12017 elsif Is_Enumeration_Type (T1) then
12018 Enumeration_Case : declare
12019 L1, L2 : Entity_Id;
12021 begin
12022 L1 := First_Literal (T1);
12023 L2 := First_Literal (T2);
12024 while Present (L1) loop
12025 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12026 return False;
12027 else
12028 Next_Literal (L1);
12029 Next_Literal (L2);
12030 end if;
12031 end loop;
12033 return True;
12034 end Enumeration_Case;
12036 -- Any other types have the same representation for these purposes
12038 else
12039 return True;
12040 end if;
12041 end Same_Representation;
12043 --------------------------------
12044 -- Resolve_Iterable_Operation --
12045 --------------------------------
12047 procedure Resolve_Iterable_Operation
12048 (N : Node_Id;
12049 Cursor : Entity_Id;
12050 Typ : Entity_Id;
12051 Nam : Name_Id)
12053 Ent : Entity_Id;
12054 F1 : Entity_Id;
12055 F2 : Entity_Id;
12057 begin
12058 if not Is_Overloaded (N) then
12059 if not Is_Entity_Name (N)
12060 or else Ekind (Entity (N)) /= E_Function
12061 or else Scope (Entity (N)) /= Scope (Typ)
12062 or else No (First_Formal (Entity (N)))
12063 or else Etype (First_Formal (Entity (N))) /= Typ
12064 then
12065 Error_Msg_N ("iterable primitive must be local function name "
12066 & "whose first formal is an iterable type", N);
12067 return;
12068 end if;
12070 Ent := Entity (N);
12071 F1 := First_Formal (Ent);
12072 if Nam = Name_First then
12074 -- First (Container) => Cursor
12076 if Etype (Ent) /= Cursor then
12077 Error_Msg_N ("primitive for First must yield a curosr", N);
12078 end if;
12080 elsif Nam = Name_Next then
12082 -- Next (Container, Cursor) => Cursor
12084 F2 := Next_Formal (F1);
12086 if Etype (F2) /= Cursor
12087 or else Etype (Ent) /= Cursor
12088 or else Present (Next_Formal (F2))
12089 then
12090 Error_Msg_N ("no match for Next iterable primitive", N);
12091 end if;
12093 elsif Nam = Name_Has_Element then
12095 -- Has_Element (Container, Cursor) => Boolean
12097 F2 := Next_Formal (F1);
12098 if Etype (F2) /= Cursor
12099 or else Etype (Ent) /= Standard_Boolean
12100 or else Present (Next_Formal (F2))
12101 then
12102 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12103 end if;
12105 elsif Nam = Name_Element then
12106 F2 := Next_Formal (F1);
12108 if No (F2)
12109 or else Etype (F2) /= Cursor
12110 or else Present (Next_Formal (F2))
12111 then
12112 Error_Msg_N ("no match for Element iterable primitive", N);
12113 end if;
12114 null;
12116 else
12117 raise Program_Error;
12118 end if;
12120 else
12121 -- Overloaded case: find subprogram with proper signature.
12122 -- Caller will report error if no match is found.
12124 declare
12125 I : Interp_Index;
12126 It : Interp;
12128 begin
12129 Get_First_Interp (N, I, It);
12130 while Present (It.Typ) loop
12131 if Ekind (It.Nam) = E_Function
12132 and then Scope (It.Nam) = Scope (Typ)
12133 and then Etype (First_Formal (It.Nam)) = Typ
12134 then
12135 F1 := First_Formal (It.Nam);
12137 if Nam = Name_First then
12138 if Etype (It.Nam) = Cursor
12139 and then No (Next_Formal (F1))
12140 then
12141 Set_Entity (N, It.Nam);
12142 exit;
12143 end if;
12145 elsif Nam = Name_Next 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) = Cursor
12152 then
12153 Set_Entity (N, It.Nam);
12154 exit;
12155 end if;
12157 elsif Nam = Name_Has_Element then
12158 F2 := Next_Formal (F1);
12160 if Present (F2)
12161 and then No (Next_Formal (F2))
12162 and then Etype (F2) = Cursor
12163 and then Etype (It.Nam) = Standard_Boolean
12164 then
12165 Set_Entity (N, It.Nam);
12166 F2 := Next_Formal (F1);
12167 exit;
12168 end if;
12170 elsif Nam = Name_Element then
12171 F2 := Next_Formal (F1);
12173 if Present (F2)
12174 and then No (Next_Formal (F2))
12175 and then Etype (F2) = Cursor
12176 then
12177 Set_Entity (N, It.Nam);
12178 exit;
12179 end if;
12180 end if;
12181 end if;
12183 Get_Next_Interp (I, It);
12184 end loop;
12185 end;
12186 end if;
12187 end Resolve_Iterable_Operation;
12189 ----------------
12190 -- Set_Biased --
12191 ----------------
12193 procedure Set_Biased
12194 (E : Entity_Id;
12195 N : Node_Id;
12196 Msg : String;
12197 Biased : Boolean := True)
12199 begin
12200 if Biased then
12201 Set_Has_Biased_Representation (E);
12203 if Warn_On_Biased_Representation then
12204 Error_Msg_NE
12205 ("?B?" & Msg & " forces biased representation for&", N, E);
12206 end if;
12207 end if;
12208 end Set_Biased;
12210 --------------------
12211 -- Set_Enum_Esize --
12212 --------------------
12214 procedure Set_Enum_Esize (T : Entity_Id) is
12215 Lo : Uint;
12216 Hi : Uint;
12217 Sz : Nat;
12219 begin
12220 Init_Alignment (T);
12222 -- Find the minimum standard size (8,16,32,64) that fits
12224 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12225 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12227 if Lo < 0 then
12228 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12229 Sz := Standard_Character_Size; -- May be > 8 on some targets
12231 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12232 Sz := 16;
12234 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12235 Sz := 32;
12237 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12238 Sz := 64;
12239 end if;
12241 else
12242 if Hi < Uint_2**08 then
12243 Sz := Standard_Character_Size; -- May be > 8 on some targets
12245 elsif Hi < Uint_2**16 then
12246 Sz := 16;
12248 elsif Hi < Uint_2**32 then
12249 Sz := 32;
12251 else pragma Assert (Hi < Uint_2**63);
12252 Sz := 64;
12253 end if;
12254 end if;
12256 -- That minimum is the proper size unless we have a foreign convention
12257 -- and the size required is 32 or less, in which case we bump the size
12258 -- up to 32. This is required for C and C++ and seems reasonable for
12259 -- all other foreign conventions.
12261 if Has_Foreign_Convention (T)
12262 and then Esize (T) < Standard_Integer_Size
12264 -- Don't do this if Short_Enums on target
12266 and then not Target_Short_Enums
12267 then
12268 Init_Esize (T, Standard_Integer_Size);
12269 else
12270 Init_Esize (T, Sz);
12271 end if;
12272 end Set_Enum_Esize;
12274 -----------------------------
12275 -- Uninstall_Discriminants --
12276 -----------------------------
12278 procedure Uninstall_Discriminants (E : Entity_Id) is
12279 Disc : Entity_Id;
12280 Prev : Entity_Id;
12281 Outer : Entity_Id;
12283 begin
12284 -- Discriminants have been made visible for type declarations and
12285 -- protected type declarations, not for subtype declarations.
12287 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12288 Disc := First_Discriminant (E);
12289 while Present (Disc) loop
12290 if Disc /= Current_Entity (Disc) then
12291 Prev := Current_Entity (Disc);
12292 while Present (Prev)
12293 and then Present (Homonym (Prev))
12294 and then Homonym (Prev) /= Disc
12295 loop
12296 Prev := Homonym (Prev);
12297 end loop;
12298 else
12299 Prev := Empty;
12300 end if;
12302 Set_Is_Immediately_Visible (Disc, False);
12304 Outer := Homonym (Disc);
12305 while Present (Outer) and then Scope (Outer) = E loop
12306 Outer := Homonym (Outer);
12307 end loop;
12309 -- Reset homonym link of other entities, but do not modify link
12310 -- between entities in current scope, so that the back-end can
12311 -- have a proper count of local overloadings.
12313 if No (Prev) then
12314 Set_Name_Entity_Id (Chars (Disc), Outer);
12316 elsif Scope (Prev) /= Scope (Disc) then
12317 Set_Homonym (Prev, Outer);
12318 end if;
12320 Next_Discriminant (Disc);
12321 end loop;
12322 end if;
12323 end Uninstall_Discriminants;
12325 -------------------------------------------
12326 -- Uninstall_Discriminants_And_Pop_Scope --
12327 -------------------------------------------
12329 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12330 begin
12331 if Has_Discriminants (E) then
12332 Uninstall_Discriminants (E);
12333 Pop_Scope;
12334 end if;
12335 end Uninstall_Discriminants_And_Pop_Scope;
12337 ------------------------------
12338 -- Validate_Address_Clauses --
12339 ------------------------------
12341 procedure Validate_Address_Clauses is
12342 begin
12343 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12344 declare
12345 ACCR : Address_Clause_Check_Record
12346 renames Address_Clause_Checks.Table (J);
12348 Expr : Node_Id;
12350 X_Alignment : Uint;
12351 Y_Alignment : Uint;
12353 X_Size : Uint;
12354 Y_Size : Uint;
12356 begin
12357 -- Skip processing of this entry if warning already posted
12359 if not Address_Warning_Posted (ACCR.N) then
12360 Expr := Original_Node (Expression (ACCR.N));
12362 -- Get alignments
12364 X_Alignment := Alignment (ACCR.X);
12365 Y_Alignment := Alignment (ACCR.Y);
12367 -- Similarly obtain sizes
12369 X_Size := Esize (ACCR.X);
12370 Y_Size := Esize (ACCR.Y);
12372 -- Check for large object overlaying smaller one
12374 if Y_Size > Uint_0
12375 and then X_Size > Uint_0
12376 and then X_Size > Y_Size
12377 then
12378 Error_Msg_NE
12379 ("??& overlays smaller object", ACCR.N, ACCR.X);
12380 Error_Msg_N
12381 ("\??program execution may be erroneous", ACCR.N);
12382 Error_Msg_Uint_1 := X_Size;
12383 Error_Msg_NE
12384 ("\??size of & is ^", ACCR.N, ACCR.X);
12385 Error_Msg_Uint_1 := Y_Size;
12386 Error_Msg_NE
12387 ("\??size of & is ^", ACCR.N, ACCR.Y);
12389 -- Check for inadequate alignment, both of the base object
12390 -- and of the offset, if any.
12392 -- Note: we do not check the alignment if we gave a size
12393 -- warning, since it would likely be redundant.
12395 elsif Y_Alignment /= Uint_0
12396 and then (Y_Alignment < X_Alignment
12397 or else (ACCR.Off
12398 and then
12399 Nkind (Expr) = N_Attribute_Reference
12400 and then
12401 Attribute_Name (Expr) = Name_Address
12402 and then
12403 Has_Compatible_Alignment
12404 (ACCR.X, Prefix (Expr))
12405 /= Known_Compatible))
12406 then
12407 Error_Msg_NE
12408 ("??specified address for& may be inconsistent "
12409 & "with alignment", ACCR.N, ACCR.X);
12410 Error_Msg_N
12411 ("\??program execution may be erroneous (RM 13.3(27))",
12412 ACCR.N);
12413 Error_Msg_Uint_1 := X_Alignment;
12414 Error_Msg_NE
12415 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12416 Error_Msg_Uint_1 := Y_Alignment;
12417 Error_Msg_NE
12418 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12419 if Y_Alignment >= X_Alignment then
12420 Error_Msg_N
12421 ("\??but offset is not multiple of alignment", ACCR.N);
12422 end if;
12423 end if;
12424 end if;
12425 end;
12426 end loop;
12427 end Validate_Address_Clauses;
12429 ---------------------------
12430 -- Validate_Independence --
12431 ---------------------------
12433 procedure Validate_Independence is
12434 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12435 N : Node_Id;
12436 E : Entity_Id;
12437 IC : Boolean;
12438 Comp : Entity_Id;
12439 Addr : Node_Id;
12440 P : Node_Id;
12442 procedure Check_Array_Type (Atyp : Entity_Id);
12443 -- Checks if the array type Atyp has independent components, and
12444 -- if not, outputs an appropriate set of error messages.
12446 procedure No_Independence;
12447 -- Output message that independence cannot be guaranteed
12449 function OK_Component (C : Entity_Id) return Boolean;
12450 -- Checks one component to see if it is independently accessible, and
12451 -- if so yields True, otherwise yields False if independent access
12452 -- cannot be guaranteed. This is a conservative routine, it only
12453 -- returns True if it knows for sure, it returns False if it knows
12454 -- there is a problem, or it cannot be sure there is no problem.
12456 procedure Reason_Bad_Component (C : Entity_Id);
12457 -- Outputs continuation message if a reason can be determined for
12458 -- the component C being bad.
12460 ----------------------
12461 -- Check_Array_Type --
12462 ----------------------
12464 procedure Check_Array_Type (Atyp : Entity_Id) is
12465 Ctyp : constant Entity_Id := Component_Type (Atyp);
12467 begin
12468 -- OK if no alignment clause, no pack, and no component size
12470 if not Has_Component_Size_Clause (Atyp)
12471 and then not Has_Alignment_Clause (Atyp)
12472 and then not Is_Packed (Atyp)
12473 then
12474 return;
12475 end if;
12477 -- Case of component size is greater than or equal to 64 and the
12478 -- alignment of the array is at least as large as the alignment
12479 -- of the component. We are definitely OK in this situation.
12481 if Known_Component_Size (Atyp)
12482 and then Component_Size (Atyp) >= 64
12483 and then Known_Alignment (Atyp)
12484 and then Known_Alignment (Ctyp)
12485 and then Alignment (Atyp) >= Alignment (Ctyp)
12486 then
12487 return;
12488 end if;
12490 -- Check actual component size
12492 if not Known_Component_Size (Atyp)
12493 or else not (Addressable (Component_Size (Atyp))
12494 and then Component_Size (Atyp) < 64)
12495 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12496 then
12497 No_Independence;
12499 -- Bad component size, check reason
12501 if Has_Component_Size_Clause (Atyp) then
12502 P := Get_Attribute_Definition_Clause
12503 (Atyp, Attribute_Component_Size);
12505 if Present (P) then
12506 Error_Msg_Sloc := Sloc (P);
12507 Error_Msg_N ("\because of Component_Size clause#", N);
12508 return;
12509 end if;
12510 end if;
12512 if Is_Packed (Atyp) then
12513 P := Get_Rep_Pragma (Atyp, Name_Pack);
12515 if Present (P) then
12516 Error_Msg_Sloc := Sloc (P);
12517 Error_Msg_N ("\because of pragma Pack#", N);
12518 return;
12519 end if;
12520 end if;
12522 -- No reason found, just return
12524 return;
12525 end if;
12527 -- Array type is OK independence-wise
12529 return;
12530 end Check_Array_Type;
12532 ---------------------
12533 -- No_Independence --
12534 ---------------------
12536 procedure No_Independence is
12537 begin
12538 if Pragma_Name (N) = Name_Independent then
12539 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
12540 else
12541 Error_Msg_NE
12542 ("independent components cannot be guaranteed for&", N, E);
12543 end if;
12544 end No_Independence;
12546 ------------------
12547 -- OK_Component --
12548 ------------------
12550 function OK_Component (C : Entity_Id) return Boolean is
12551 Rec : constant Entity_Id := Scope (C);
12552 Ctyp : constant Entity_Id := Etype (C);
12554 begin
12555 -- OK if no component clause, no Pack, and no alignment clause
12557 if No (Component_Clause (C))
12558 and then not Is_Packed (Rec)
12559 and then not Has_Alignment_Clause (Rec)
12560 then
12561 return True;
12562 end if;
12564 -- Here we look at the actual component layout. A component is
12565 -- addressable if its size is a multiple of the Esize of the
12566 -- component type, and its starting position in the record has
12567 -- appropriate alignment, and the record itself has appropriate
12568 -- alignment to guarantee the component alignment.
12570 -- Make sure sizes are static, always assume the worst for any
12571 -- cases where we cannot check static values.
12573 if not (Known_Static_Esize (C)
12574 and then
12575 Known_Static_Esize (Ctyp))
12576 then
12577 return False;
12578 end if;
12580 -- Size of component must be addressable or greater than 64 bits
12581 -- and a multiple of bytes.
12583 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
12584 return False;
12585 end if;
12587 -- Check size is proper multiple
12589 if Esize (C) mod Esize (Ctyp) /= 0 then
12590 return False;
12591 end if;
12593 -- Check alignment of component is OK
12595 if not Known_Component_Bit_Offset (C)
12596 or else Component_Bit_Offset (C) < Uint_0
12597 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
12598 then
12599 return False;
12600 end if;
12602 -- Check alignment of record type is OK
12604 if not Known_Alignment (Rec)
12605 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12606 then
12607 return False;
12608 end if;
12610 -- All tests passed, component is addressable
12612 return True;
12613 end OK_Component;
12615 --------------------------
12616 -- Reason_Bad_Component --
12617 --------------------------
12619 procedure Reason_Bad_Component (C : Entity_Id) is
12620 Rec : constant Entity_Id := Scope (C);
12621 Ctyp : constant Entity_Id := Etype (C);
12623 begin
12624 -- If component clause present assume that's the problem
12626 if Present (Component_Clause (C)) then
12627 Error_Msg_Sloc := Sloc (Component_Clause (C));
12628 Error_Msg_N ("\because of Component_Clause#", N);
12629 return;
12630 end if;
12632 -- If pragma Pack clause present, assume that's the problem
12634 if Is_Packed (Rec) then
12635 P := Get_Rep_Pragma (Rec, Name_Pack);
12637 if Present (P) then
12638 Error_Msg_Sloc := Sloc (P);
12639 Error_Msg_N ("\because of pragma Pack#", N);
12640 return;
12641 end if;
12642 end if;
12644 -- See if record has bad alignment clause
12646 if Has_Alignment_Clause (Rec)
12647 and then Known_Alignment (Rec)
12648 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12649 then
12650 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
12652 if Present (P) then
12653 Error_Msg_Sloc := Sloc (P);
12654 Error_Msg_N ("\because of Alignment clause#", N);
12655 end if;
12656 end if;
12658 -- Couldn't find a reason, so return without a message
12660 return;
12661 end Reason_Bad_Component;
12663 -- Start of processing for Validate_Independence
12665 begin
12666 for J in Independence_Checks.First .. Independence_Checks.Last loop
12667 N := Independence_Checks.Table (J).N;
12668 E := Independence_Checks.Table (J).E;
12669 IC := Pragma_Name (N) = Name_Independent_Components;
12671 -- Deal with component case
12673 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
12674 if not OK_Component (E) then
12675 No_Independence;
12676 Reason_Bad_Component (E);
12677 goto Continue;
12678 end if;
12679 end if;
12681 -- Deal with record with Independent_Components
12683 if IC and then Is_Record_Type (E) then
12684 Comp := First_Component_Or_Discriminant (E);
12685 while Present (Comp) loop
12686 if not OK_Component (Comp) then
12687 No_Independence;
12688 Reason_Bad_Component (Comp);
12689 goto Continue;
12690 end if;
12692 Next_Component_Or_Discriminant (Comp);
12693 end loop;
12694 end if;
12696 -- Deal with address clause case
12698 if Is_Object (E) then
12699 Addr := Address_Clause (E);
12701 if Present (Addr) then
12702 No_Independence;
12703 Error_Msg_Sloc := Sloc (Addr);
12704 Error_Msg_N ("\because of Address clause#", N);
12705 goto Continue;
12706 end if;
12707 end if;
12709 -- Deal with independent components for array type
12711 if IC and then Is_Array_Type (E) then
12712 Check_Array_Type (E);
12713 end if;
12715 -- Deal with independent components for array object
12717 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
12718 Check_Array_Type (Etype (E));
12719 end if;
12721 <<Continue>> null;
12722 end loop;
12723 end Validate_Independence;
12725 ------------------------------
12726 -- Validate_Iterable_Aspect --
12727 ------------------------------
12729 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
12730 Assoc : Node_Id;
12731 Expr : Node_Id;
12733 Prim : Node_Id;
12734 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
12736 First_Id : Entity_Id;
12737 Next_Id : Entity_Id;
12738 Has_Element_Id : Entity_Id;
12739 Element_Id : Entity_Id;
12741 begin
12742 -- If previous error aspect is unusable
12744 if Cursor = Any_Type then
12745 return;
12746 end if;
12748 First_Id := Empty;
12749 Next_Id := Empty;
12750 Has_Element_Id := Empty;
12751 Element_Id := Empty;
12753 -- Each expression must resolve to a function with the proper signature
12755 Assoc := First (Component_Associations (Expression (ASN)));
12756 while Present (Assoc) loop
12757 Expr := Expression (Assoc);
12758 Analyze (Expr);
12760 Prim := First (Choices (Assoc));
12762 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
12763 Error_Msg_N ("illegal name in association", Prim);
12765 elsif Chars (Prim) = Name_First then
12766 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
12767 First_Id := Entity (Expr);
12769 elsif Chars (Prim) = Name_Next then
12770 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
12771 Next_Id := Entity (Expr);
12773 elsif Chars (Prim) = Name_Has_Element then
12774 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
12775 Has_Element_Id := Entity (Expr);
12777 elsif Chars (Prim) = Name_Element then
12778 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
12779 Element_Id := Entity (Expr);
12781 else
12782 Error_Msg_N ("invalid name for iterable function", Prim);
12783 end if;
12785 Next (Assoc);
12786 end loop;
12788 if No (First_Id) then
12789 Error_Msg_N ("match for First primitive not found", ASN);
12791 elsif No (Next_Id) then
12792 Error_Msg_N ("match for Next primitive not found", ASN);
12794 elsif No (Has_Element_Id) then
12795 Error_Msg_N ("match for Has_Element primitive not found", ASN);
12797 elsif No (Element_Id) then
12798 null; -- Optional.
12799 end if;
12800 end Validate_Iterable_Aspect;
12802 -----------------------------------
12803 -- Validate_Unchecked_Conversion --
12804 -----------------------------------
12806 procedure Validate_Unchecked_Conversion
12807 (N : Node_Id;
12808 Act_Unit : Entity_Id)
12810 Source : Entity_Id;
12811 Target : Entity_Id;
12812 Vnode : Node_Id;
12814 begin
12815 -- Obtain source and target types. Note that we call Ancestor_Subtype
12816 -- here because the processing for generic instantiation always makes
12817 -- subtypes, and we want the original frozen actual types.
12819 -- If we are dealing with private types, then do the check on their
12820 -- fully declared counterparts if the full declarations have been
12821 -- encountered (they don't have to be visible, but they must exist).
12823 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
12825 if Is_Private_Type (Source)
12826 and then Present (Underlying_Type (Source))
12827 then
12828 Source := Underlying_Type (Source);
12829 end if;
12831 Target := Ancestor_Subtype (Etype (Act_Unit));
12833 -- If either type is generic, the instantiation happens within a generic
12834 -- unit, and there is nothing to check. The proper check will happen
12835 -- when the enclosing generic is instantiated.
12837 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
12838 return;
12839 end if;
12841 if Is_Private_Type (Target)
12842 and then Present (Underlying_Type (Target))
12843 then
12844 Target := Underlying_Type (Target);
12845 end if;
12847 -- Source may be unconstrained array, but not target
12849 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
12850 Error_Msg_N
12851 ("unchecked conversion to unconstrained array not allowed", N);
12852 return;
12853 end if;
12855 -- Warn if conversion between two different convention pointers
12857 if Is_Access_Type (Target)
12858 and then Is_Access_Type (Source)
12859 and then Convention (Target) /= Convention (Source)
12860 and then Warn_On_Unchecked_Conversion
12861 then
12862 -- Give warnings for subprogram pointers only on most targets
12864 if Is_Access_Subprogram_Type (Target)
12865 or else Is_Access_Subprogram_Type (Source)
12866 then
12867 Error_Msg_N
12868 ("?z?conversion between pointers with different conventions!",
12870 end if;
12871 end if;
12873 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
12874 -- warning when compiling GNAT-related sources.
12876 if Warn_On_Unchecked_Conversion
12877 and then not In_Predefined_Unit (N)
12878 and then RTU_Loaded (Ada_Calendar)
12879 and then (Chars (Source) = Name_Time
12880 or else
12881 Chars (Target) = Name_Time)
12882 then
12883 -- If Ada.Calendar is loaded and the name of one of the operands is
12884 -- Time, there is a good chance that this is Ada.Calendar.Time.
12886 declare
12887 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
12888 begin
12889 pragma Assert (Present (Calendar_Time));
12891 if Source = Calendar_Time or else Target = Calendar_Time then
12892 Error_Msg_N
12893 ("?z?representation of 'Time values may change between "
12894 & "'G'N'A'T versions", N);
12895 end if;
12896 end;
12897 end if;
12899 -- Make entry in unchecked conversion table for later processing by
12900 -- Validate_Unchecked_Conversions, which will check sizes and alignments
12901 -- (using values set by the back-end where possible). This is only done
12902 -- if the appropriate warning is active.
12904 if Warn_On_Unchecked_Conversion then
12905 Unchecked_Conversions.Append
12906 (New_Val => UC_Entry'(Eloc => Sloc (N),
12907 Source => Source,
12908 Target => Target,
12909 Act_Unit => Act_Unit));
12911 -- If both sizes are known statically now, then back end annotation
12912 -- is not required to do a proper check but if either size is not
12913 -- known statically, then we need the annotation.
12915 if Known_Static_RM_Size (Source)
12916 and then
12917 Known_Static_RM_Size (Target)
12918 then
12919 null;
12920 else
12921 Back_Annotate_Rep_Info := True;
12922 end if;
12923 end if;
12925 -- If unchecked conversion to access type, and access type is declared
12926 -- in the same unit as the unchecked conversion, then set the flag
12927 -- No_Strict_Aliasing (no strict aliasing is implicit here)
12929 if Is_Access_Type (Target) and then
12930 In_Same_Source_Unit (Target, N)
12931 then
12932 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
12933 end if;
12935 -- Generate N_Validate_Unchecked_Conversion node for back end in case
12936 -- the back end needs to perform special validation checks.
12938 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
12939 -- have full expansion and the back end is called ???
12941 Vnode :=
12942 Make_Validate_Unchecked_Conversion (Sloc (N));
12943 Set_Source_Type (Vnode, Source);
12944 Set_Target_Type (Vnode, Target);
12946 -- If the unchecked conversion node is in a list, just insert before it.
12947 -- If not we have some strange case, not worth bothering about.
12949 if Is_List_Member (N) then
12950 Insert_After (N, Vnode);
12951 end if;
12952 end Validate_Unchecked_Conversion;
12954 ------------------------------------
12955 -- Validate_Unchecked_Conversions --
12956 ------------------------------------
12958 procedure Validate_Unchecked_Conversions is
12959 begin
12960 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
12961 declare
12962 T : UC_Entry renames Unchecked_Conversions.Table (N);
12964 Eloc : constant Source_Ptr := T.Eloc;
12965 Source : constant Entity_Id := T.Source;
12966 Target : constant Entity_Id := T.Target;
12967 Act_Unit : constant Entity_Id := T.Act_Unit;
12969 Source_Siz : Uint;
12970 Target_Siz : Uint;
12972 begin
12973 -- Skip if function marked as warnings off
12975 if Warnings_Off (Act_Unit) then
12976 goto Continue;
12977 end if;
12979 -- This validation check, which warns if we have unequal sizes for
12980 -- unchecked conversion, and thus potentially implementation
12981 -- dependent semantics, is one of the few occasions on which we
12982 -- use the official RM size instead of Esize. See description in
12983 -- Einfo "Handling of Type'Size Values" for details.
12985 if Serious_Errors_Detected = 0
12986 and then Known_Static_RM_Size (Source)
12987 and then Known_Static_RM_Size (Target)
12989 -- Don't do the check if warnings off for either type, note the
12990 -- deliberate use of OR here instead of OR ELSE to get the flag
12991 -- Warnings_Off_Used set for both types if appropriate.
12993 and then not (Has_Warnings_Off (Source)
12995 Has_Warnings_Off (Target))
12996 then
12997 Source_Siz := RM_Size (Source);
12998 Target_Siz := RM_Size (Target);
13000 if Source_Siz /= Target_Siz then
13001 Error_Msg
13002 ("?z?types for unchecked conversion have different sizes!",
13003 Eloc);
13005 if All_Errors_Mode then
13006 Error_Msg_Name_1 := Chars (Source);
13007 Error_Msg_Uint_1 := Source_Siz;
13008 Error_Msg_Name_2 := Chars (Target);
13009 Error_Msg_Uint_2 := Target_Siz;
13010 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
13012 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13014 if Is_Discrete_Type (Source)
13015 and then
13016 Is_Discrete_Type (Target)
13017 then
13018 if Source_Siz > Target_Siz then
13019 Error_Msg
13020 ("\?z?^ high order bits of source will "
13021 & "be ignored!", Eloc);
13023 elsif Is_Unsigned_Type (Source) then
13024 Error_Msg
13025 ("\?z?source will be extended with ^ high order "
13026 & "zero bits!", Eloc);
13028 else
13029 Error_Msg
13030 ("\?z?source will be extended with ^ high order "
13031 & "sign bits!", Eloc);
13032 end if;
13034 elsif Source_Siz < Target_Siz then
13035 if Is_Discrete_Type (Target) then
13036 if Bytes_Big_Endian then
13037 Error_Msg
13038 ("\?z?target value will include ^ undefined "
13039 & "low order bits!", Eloc);
13040 else
13041 Error_Msg
13042 ("\?z?target value will include ^ undefined "
13043 & "high order bits!", Eloc);
13044 end if;
13046 else
13047 Error_Msg
13048 ("\?z?^ trailing bits of target value will be "
13049 & "undefined!", Eloc);
13050 end if;
13052 else pragma Assert (Source_Siz > Target_Siz);
13053 Error_Msg
13054 ("\?z?^ trailing bits of source will be ignored!",
13055 Eloc);
13056 end if;
13057 end if;
13058 end if;
13059 end if;
13061 -- If both types are access types, we need to check the alignment.
13062 -- If the alignment of both is specified, we can do it here.
13064 if Serious_Errors_Detected = 0
13065 and then Is_Access_Type (Source)
13066 and then Is_Access_Type (Target)
13067 and then Target_Strict_Alignment
13068 and then Present (Designated_Type (Source))
13069 and then Present (Designated_Type (Target))
13070 then
13071 declare
13072 D_Source : constant Entity_Id := Designated_Type (Source);
13073 D_Target : constant Entity_Id := Designated_Type (Target);
13075 begin
13076 if Known_Alignment (D_Source)
13077 and then
13078 Known_Alignment (D_Target)
13079 then
13080 declare
13081 Source_Align : constant Uint := Alignment (D_Source);
13082 Target_Align : constant Uint := Alignment (D_Target);
13084 begin
13085 if Source_Align < Target_Align
13086 and then not Is_Tagged_Type (D_Source)
13088 -- Suppress warning if warnings suppressed on either
13089 -- type or either designated type. Note the use of
13090 -- OR here instead of OR ELSE. That is intentional,
13091 -- we would like to set flag Warnings_Off_Used in
13092 -- all types for which warnings are suppressed.
13094 and then not (Has_Warnings_Off (D_Source)
13096 Has_Warnings_Off (D_Target)
13098 Has_Warnings_Off (Source)
13100 Has_Warnings_Off (Target))
13101 then
13102 Error_Msg_Uint_1 := Target_Align;
13103 Error_Msg_Uint_2 := Source_Align;
13104 Error_Msg_Node_1 := D_Target;
13105 Error_Msg_Node_2 := D_Source;
13106 Error_Msg
13107 ("?z?alignment of & (^) is stricter than "
13108 & "alignment of & (^)!", Eloc);
13109 Error_Msg
13110 ("\?z?resulting access value may have invalid "
13111 & "alignment!", Eloc);
13112 end if;
13113 end;
13114 end if;
13115 end;
13116 end if;
13117 end;
13119 <<Continue>>
13120 null;
13121 end loop;
13122 end Validate_Unchecked_Conversions;
13124 end Sem_Ch13;