PR rtl-optimization/57003
[official-gcc.git] / gcc / ada / sem_ch13.adb
bloba73712bfb5f239ea55963baae6b5282bd9c73030
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)
2951 or else Is_True (Static_Boolean (Expr))
2952 then
2953 Set_Uses_Lock_Free (E);
2954 end if;
2956 Record_Rep_Item (E, Aspect);
2957 end if;
2959 goto Continue;
2961 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
2963 -- For the case of aspects Import and Export, we don't
2964 -- consider that we know the entity is never set in the
2965 -- source, since it is is likely modified outside the
2966 -- program.
2968 -- Note: one might think that the analysis of the
2969 -- resulting pragma would take care of that, but
2970 -- that's not the case since it won't be from source.
2972 if Ekind (E) = E_Variable then
2973 Set_Never_Set_In_Source (E, False);
2974 end if;
2976 -- In older versions of Ada the corresponding pragmas
2977 -- specified a Convention. In Ada 2012 the convention is
2978 -- specified as a separate aspect, and it is optional,
2979 -- given that it defaults to Convention_Ada. The code
2980 -- that verifed that there was a matching convention
2981 -- is now obsolete.
2983 -- Resolve the expression of an Import or Export here,
2984 -- and require it to be of type Boolean and static. This
2985 -- is not quite right, because in general this should be
2986 -- delayed, but that seems tricky for these, because
2987 -- normally Boolean aspects are replaced with pragmas at
2988 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
2989 -- but in the case of these aspects we can't generate
2990 -- a simple pragma with just the entity name. ???
2992 if not Present (Expr)
2993 or else Is_True (Static_Boolean (Expr))
2994 then
2995 if A_Id = Aspect_Import then
2996 Set_Is_Imported (E);
2998 -- An imported entity cannot have an explicit
2999 -- initialization.
3001 if Nkind (N) = N_Object_Declaration
3002 and then Present (Expression (N))
3003 then
3004 Error_Msg_N
3005 ("imported entities cannot be initialized "
3006 & "(RM B.1(24))", Expression (N));
3007 end if;
3009 elsif A_Id = Aspect_Export then
3010 Set_Is_Exported (E);
3011 end if;
3012 end if;
3014 goto Continue;
3015 end if;
3017 -- Library unit aspects require special handling in the case
3018 -- of a package declaration, the pragma needs to be inserted
3019 -- in the list of declarations for the associated package.
3020 -- There is no issue of visibility delay for these aspects.
3022 if A_Id in Library_Unit_Aspects
3023 and then
3024 Nkind_In (N, N_Package_Declaration,
3025 N_Generic_Package_Declaration)
3026 and then Nkind (Parent (N)) /= N_Compilation_Unit
3027 then
3028 Error_Msg_N
3029 ("incorrect context for library unit aspect&", Id);
3030 goto Continue;
3031 end if;
3033 -- External property aspects are Boolean by nature, but
3034 -- their pragmas must contain two arguments, the second
3035 -- being the optional Boolean expression.
3037 if A_Id = Aspect_Async_Readers or else
3038 A_Id = Aspect_Async_Writers or else
3039 A_Id = Aspect_Effective_Reads or else
3040 A_Id = Aspect_Effective_Writes
3041 then
3042 declare
3043 Args : List_Id;
3045 begin
3046 -- The first argument of the external property pragma
3047 -- is the related object.
3049 Args :=
3050 New_List (
3051 Make_Pragma_Argument_Association (Sloc (Ent),
3052 Expression => Ent));
3054 -- The second argument is the optional Boolean
3055 -- expression which must be propagated even if it
3056 -- evaluates to False as this has special semantic
3057 -- meaning.
3059 if Present (Expr) then
3060 Append_To (Args,
3061 Make_Pragma_Argument_Association (Loc,
3062 Expression => Relocate_Node (Expr)));
3063 end if;
3065 Make_Aitem_Pragma
3066 (Pragma_Argument_Associations => Args,
3067 Pragma_Name => Nam);
3068 end;
3070 -- Cases where we do not delay, includes all cases where the
3071 -- expression is missing other than the above cases.
3073 elsif not Delay_Required or else No (Expr) then
3074 Make_Aitem_Pragma
3075 (Pragma_Argument_Associations => New_List (
3076 Make_Pragma_Argument_Association (Sloc (Ent),
3077 Expression => Ent)),
3078 Pragma_Name => Chars (Id));
3079 Delay_Required := False;
3081 -- In general cases, the corresponding pragma/attribute
3082 -- definition clause will be inserted later at the freezing
3083 -- point, and we do not need to build it now.
3085 else
3086 Aitem := Empty;
3087 end if;
3089 -- Storage_Size
3091 -- This is special because for access types we need to generate
3092 -- an attribute definition clause. This also works for single
3093 -- task declarations, but it does not work for task type
3094 -- declarations, because we have the case where the expression
3095 -- references a discriminant of the task type. That can't use
3096 -- an attribute definition clause because we would not have
3097 -- visibility on the discriminant. For that case we must
3098 -- generate a pragma in the task definition.
3100 when Aspect_Storage_Size =>
3102 -- Task type case
3104 if Ekind (E) = E_Task_Type then
3105 declare
3106 Decl : constant Node_Id := Declaration_Node (E);
3108 begin
3109 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3111 -- If no task definition, create one
3113 if No (Task_Definition (Decl)) then
3114 Set_Task_Definition (Decl,
3115 Make_Task_Definition (Loc,
3116 Visible_Declarations => Empty_List,
3117 End_Label => Empty));
3118 end if;
3120 -- Create a pragma and put it at the start of the task
3121 -- definition for the task type declaration.
3123 Make_Aitem_Pragma
3124 (Pragma_Argument_Associations => New_List (
3125 Make_Pragma_Argument_Association (Loc,
3126 Expression => Relocate_Node (Expr))),
3127 Pragma_Name => Name_Storage_Size);
3129 Prepend
3130 (Aitem,
3131 Visible_Declarations (Task_Definition (Decl)));
3132 goto Continue;
3133 end;
3135 -- All other cases, generate attribute definition
3137 else
3138 Aitem :=
3139 Make_Attribute_Definition_Clause (Loc,
3140 Name => Ent,
3141 Chars => Chars (Id),
3142 Expression => Relocate_Node (Expr));
3143 end if;
3144 end case;
3146 -- Attach the corresponding pragma/attribute definition clause to
3147 -- the aspect specification node.
3149 if Present (Aitem) then
3150 Set_From_Aspect_Specification (Aitem);
3151 end if;
3153 -- In the context of a compilation unit, we directly put the
3154 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3155 -- node (no delay is required here) except for aspects on a
3156 -- subprogram body (see below) and a generic package, for which we
3157 -- need to introduce the pragma before building the generic copy
3158 -- (see sem_ch12), and for package instantiations, where the
3159 -- library unit pragmas are better handled early.
3161 if Nkind (Parent (N)) = N_Compilation_Unit
3162 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3163 then
3164 declare
3165 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3167 begin
3168 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3170 -- For a Boolean aspect, create the corresponding pragma if
3171 -- no expression or if the value is True.
3173 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3174 if Is_True (Static_Boolean (Expr)) then
3175 Make_Aitem_Pragma
3176 (Pragma_Argument_Associations => New_List (
3177 Make_Pragma_Argument_Association (Sloc (Ent),
3178 Expression => Ent)),
3179 Pragma_Name => Chars (Id));
3181 Set_From_Aspect_Specification (Aitem, True);
3182 Set_Corresponding_Aspect (Aitem, Aspect);
3184 else
3185 goto Continue;
3186 end if;
3187 end if;
3189 -- If the aspect is on a subprogram body (relevant aspect
3190 -- is Inline), add the pragma in front of the declarations.
3192 if Nkind (N) = N_Subprogram_Body then
3193 if No (Declarations (N)) then
3194 Set_Declarations (N, New_List);
3195 end if;
3197 Prepend (Aitem, Declarations (N));
3199 elsif Nkind (N) = N_Generic_Package_Declaration then
3200 if No (Visible_Declarations (Specification (N))) then
3201 Set_Visible_Declarations (Specification (N), New_List);
3202 end if;
3204 Prepend (Aitem,
3205 Visible_Declarations (Specification (N)));
3207 elsif Nkind (N) = N_Package_Instantiation then
3208 declare
3209 Spec : constant Node_Id :=
3210 Specification (Instance_Spec (N));
3211 begin
3212 if No (Visible_Declarations (Spec)) then
3213 Set_Visible_Declarations (Spec, New_List);
3214 end if;
3216 Prepend (Aitem, Visible_Declarations (Spec));
3217 end;
3219 else
3220 if No (Pragmas_After (Aux)) then
3221 Set_Pragmas_After (Aux, New_List);
3222 end if;
3224 Append (Aitem, Pragmas_After (Aux));
3225 end if;
3227 goto Continue;
3228 end;
3229 end if;
3231 -- The evaluation of the aspect is delayed to the freezing point.
3232 -- The pragma or attribute clause if there is one is then attached
3233 -- to the aspect specification which is put in the rep item list.
3235 if Delay_Required then
3236 if Present (Aitem) then
3237 Set_Is_Delayed_Aspect (Aitem);
3238 Set_Aspect_Rep_Item (Aspect, Aitem);
3239 Set_Parent (Aitem, Aspect);
3240 end if;
3242 Set_Is_Delayed_Aspect (Aspect);
3244 -- In the case of Default_Value, link the aspect to base type
3245 -- as well, even though it appears on a first subtype. This is
3246 -- mandated by the semantics of the aspect. Do not establish
3247 -- the link when processing the base type itself as this leads
3248 -- to a rep item circularity. Verify that we are dealing with
3249 -- a scalar type to prevent cascaded errors.
3251 if A_Id = Aspect_Default_Value
3252 and then Is_Scalar_Type (E)
3253 and then Base_Type (E) /= E
3254 then
3255 Set_Has_Delayed_Aspects (Base_Type (E));
3256 Record_Rep_Item (Base_Type (E), Aspect);
3257 end if;
3259 Set_Has_Delayed_Aspects (E);
3260 Record_Rep_Item (E, Aspect);
3262 -- When delay is not required and the context is a package or a
3263 -- subprogram body, insert the pragma in the body declarations.
3265 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3266 if No (Declarations (N)) then
3267 Set_Declarations (N, New_List);
3268 end if;
3270 -- The pragma is added before source declarations
3272 Prepend_To (Declarations (N), Aitem);
3274 -- When delay is not required and the context is not a compilation
3275 -- unit, we simply insert the pragma/attribute definition clause
3276 -- in sequence.
3278 else
3279 Insert_After (Ins_Node, Aitem);
3280 Ins_Node := Aitem;
3281 end if;
3282 end Analyze_One_Aspect;
3284 <<Continue>>
3285 Next (Aspect);
3286 end loop Aspect_Loop;
3288 if Has_Delayed_Aspects (E) then
3289 Ensure_Freeze_Node (E);
3290 end if;
3291 end Analyze_Aspect_Specifications;
3293 -----------------------
3294 -- Analyze_At_Clause --
3295 -----------------------
3297 -- An at clause is replaced by the corresponding Address attribute
3298 -- definition clause that is the preferred approach in Ada 95.
3300 procedure Analyze_At_Clause (N : Node_Id) is
3301 CS : constant Boolean := Comes_From_Source (N);
3303 begin
3304 -- This is an obsolescent feature
3306 Check_Restriction (No_Obsolescent_Features, N);
3308 if Warn_On_Obsolescent_Feature then
3309 Error_Msg_N
3310 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3311 Error_Msg_N
3312 ("\?j?use address attribute definition clause instead", N);
3313 end if;
3315 -- Rewrite as address clause
3317 Rewrite (N,
3318 Make_Attribute_Definition_Clause (Sloc (N),
3319 Name => Identifier (N),
3320 Chars => Name_Address,
3321 Expression => Expression (N)));
3323 -- We preserve Comes_From_Source, since logically the clause still comes
3324 -- from the source program even though it is changed in form.
3326 Set_Comes_From_Source (N, CS);
3328 -- Analyze rewritten clause
3330 Analyze_Attribute_Definition_Clause (N);
3331 end Analyze_At_Clause;
3333 -----------------------------------------
3334 -- Analyze_Attribute_Definition_Clause --
3335 -----------------------------------------
3337 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3338 Loc : constant Source_Ptr := Sloc (N);
3339 Nam : constant Node_Id := Name (N);
3340 Attr : constant Name_Id := Chars (N);
3341 Expr : constant Node_Id := Expression (N);
3342 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3344 Ent : Entity_Id;
3345 -- The entity of Nam after it is analyzed. In the case of an incomplete
3346 -- type, this is the underlying type.
3348 U_Ent : Entity_Id;
3349 -- The underlying entity to which the attribute applies. Generally this
3350 -- is the Underlying_Type of Ent, except in the case where the clause
3351 -- applies to full view of incomplete type or private type in which case
3352 -- U_Ent is just a copy of Ent.
3354 FOnly : Boolean := False;
3355 -- Reset to True for subtype specific attribute (Alignment, Size)
3356 -- and for stream attributes, i.e. those cases where in the call to
3357 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3358 -- are checked. Note that the case of stream attributes is not clear
3359 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3360 -- Storage_Size for derived task types, but that is also clearly
3361 -- unintentional.
3363 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3364 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3365 -- definition clauses.
3367 function Duplicate_Clause return Boolean;
3368 -- This routine checks if the aspect for U_Ent being given by attribute
3369 -- definition clause N is for an aspect that has already been specified,
3370 -- and if so gives an error message. If there is a duplicate, True is
3371 -- returned, otherwise if there is no error, False is returned.
3373 procedure Check_Indexing_Functions;
3374 -- Check that the function in Constant_Indexing or Variable_Indexing
3375 -- attribute has the proper type structure. If the name is overloaded,
3376 -- check that some interpretation is legal.
3378 procedure Check_Iterator_Functions;
3379 -- Check that there is a single function in Default_Iterator attribute
3380 -- has the proper type structure.
3382 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3383 -- Common legality check for the previous two
3385 -----------------------------------
3386 -- Analyze_Stream_TSS_Definition --
3387 -----------------------------------
3389 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3390 Subp : Entity_Id := Empty;
3391 I : Interp_Index;
3392 It : Interp;
3393 Pnam : Entity_Id;
3395 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3396 -- True for Read attribute, false for other attributes
3398 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3399 -- Return true if the entity is a subprogram with an appropriate
3400 -- profile for the attribute being defined.
3402 ----------------------
3403 -- Has_Good_Profile --
3404 ----------------------
3406 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3407 F : Entity_Id;
3408 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3409 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3410 (False => E_Procedure, True => E_Function);
3411 Typ : Entity_Id;
3413 begin
3414 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3415 return False;
3416 end if;
3418 F := First_Formal (Subp);
3420 if No (F)
3421 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3422 or else Designated_Type (Etype (F)) /=
3423 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3424 then
3425 return False;
3426 end if;
3428 if not Is_Function then
3429 Next_Formal (F);
3431 declare
3432 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3433 (False => E_In_Parameter,
3434 True => E_Out_Parameter);
3435 begin
3436 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3437 return False;
3438 end if;
3439 end;
3441 Typ := Etype (F);
3443 -- If the attribute specification comes from an aspect
3444 -- specification for a class-wide stream, the parameter must be
3445 -- a class-wide type of the entity to which the aspect applies.
3447 if From_Aspect_Specification (N)
3448 and then Class_Present (Parent (N))
3449 and then Is_Class_Wide_Type (Typ)
3450 then
3451 Typ := Etype (Typ);
3452 end if;
3454 else
3455 Typ := Etype (Subp);
3456 end if;
3458 -- Verify that the prefix of the attribute and the local name for
3459 -- the type of the formal match.
3461 if Base_Type (Typ) /= Base_Type (Ent)
3462 or else Present ((Next_Formal (F)))
3463 then
3464 return False;
3466 elsif not Is_Scalar_Type (Typ)
3467 and then not Is_First_Subtype (Typ)
3468 and then not Is_Class_Wide_Type (Typ)
3469 then
3470 return False;
3472 else
3473 return True;
3474 end if;
3475 end Has_Good_Profile;
3477 -- Start of processing for Analyze_Stream_TSS_Definition
3479 begin
3480 FOnly := True;
3482 if not Is_Type (U_Ent) then
3483 Error_Msg_N ("local name must be a subtype", Nam);
3484 return;
3486 elsif not Is_First_Subtype (U_Ent) then
3487 Error_Msg_N ("local name must be a first subtype", Nam);
3488 return;
3489 end if;
3491 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3493 -- If Pnam is present, it can be either inherited from an ancestor
3494 -- type (in which case it is legal to redefine it for this type), or
3495 -- be a previous definition of the attribute for the same type (in
3496 -- which case it is illegal).
3498 -- In the first case, it will have been analyzed already, and we
3499 -- can check that its profile does not match the expected profile
3500 -- for a stream attribute of U_Ent. In the second case, either Pnam
3501 -- has been analyzed (and has the expected profile), or it has not
3502 -- been analyzed yet (case of a type that has not been frozen yet
3503 -- and for which the stream attribute has been set using Set_TSS).
3505 if Present (Pnam)
3506 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3507 then
3508 Error_Msg_Sloc := Sloc (Pnam);
3509 Error_Msg_Name_1 := Attr;
3510 Error_Msg_N ("% attribute already defined #", Nam);
3511 return;
3512 end if;
3514 Analyze (Expr);
3516 if Is_Entity_Name (Expr) then
3517 if not Is_Overloaded (Expr) then
3518 if Has_Good_Profile (Entity (Expr)) then
3519 Subp := Entity (Expr);
3520 end if;
3522 else
3523 Get_First_Interp (Expr, I, It);
3524 while Present (It.Nam) loop
3525 if Has_Good_Profile (It.Nam) then
3526 Subp := It.Nam;
3527 exit;
3528 end if;
3530 Get_Next_Interp (I, It);
3531 end loop;
3532 end if;
3533 end if;
3535 if Present (Subp) then
3536 if Is_Abstract_Subprogram (Subp) then
3537 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3538 return;
3540 -- Test for stream subprogram for interface type being non-null
3542 elsif Is_Interface (U_Ent)
3543 and then not Inside_A_Generic
3544 and then Ekind (Subp) = E_Procedure
3545 and then
3546 not Null_Present
3547 (Specification
3548 (Unit_Declaration_Node (Ultimate_Alias (Subp))))
3549 then
3550 Error_Msg_N
3551 ("stream subprogram for interface type "
3552 & "must be null procedure", Expr);
3553 end if;
3555 Set_Entity (Expr, Subp);
3556 Set_Etype (Expr, Etype (Subp));
3558 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3560 else
3561 Error_Msg_Name_1 := Attr;
3562 Error_Msg_N ("incorrect expression for% attribute", Expr);
3563 end if;
3564 end Analyze_Stream_TSS_Definition;
3566 ------------------------------
3567 -- Check_Indexing_Functions --
3568 ------------------------------
3570 procedure Check_Indexing_Functions is
3571 Indexing_Found : Boolean := False;
3573 procedure Check_One_Function (Subp : Entity_Id);
3574 -- Check one possible interpretation. Sets Indexing_Found True if a
3575 -- legal indexing function is found.
3577 procedure Illegal_Indexing (Msg : String);
3578 -- Diagnose illegal indexing function if not overloaded. In the
3579 -- overloaded case indicate that no legal interpretation exists.
3581 ------------------------
3582 -- Check_One_Function --
3583 ------------------------
3585 procedure Check_One_Function (Subp : Entity_Id) is
3586 Default_Element : Node_Id;
3587 Ret_Type : constant Entity_Id := Etype (Subp);
3589 begin
3590 if not Is_Overloadable (Subp) then
3591 Illegal_Indexing ("illegal indexing function for type&");
3592 return;
3594 elsif Scope (Subp) /= Scope (Ent) then
3595 if Nkind (Expr) = N_Expanded_Name then
3597 -- Indexing function can't be declared elsewhere
3599 Illegal_Indexing
3600 ("indexing function must be declared in scope of type&");
3601 end if;
3603 return;
3605 elsif No (First_Formal (Subp)) then
3606 Illegal_Indexing
3607 ("Indexing requires a function that applies to type&");
3608 return;
3610 elsif No (Next_Formal (First_Formal (Subp))) then
3611 Illegal_Indexing
3612 ("indexing function must have at least two parameters");
3613 return;
3615 elsif Is_Derived_Type (Ent) then
3616 if (Attr = Name_Constant_Indexing
3617 and then Present
3618 (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing)))
3620 or else (Attr = Name_Variable_Indexing
3621 and then Present
3622 (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing)))
3623 then
3624 if Debug_Flag_Dot_XX then
3625 null;
3627 else
3628 Illegal_Indexing
3629 ("indexing function already inherited "
3630 & "from parent type");
3631 return;
3632 end if;
3633 end if;
3634 end if;
3636 if not Check_Primitive_Function (Subp)
3637 then
3638 Illegal_Indexing
3639 ("Indexing aspect requires a function that applies to type&");
3640 return;
3641 end if;
3643 -- If partial declaration exists, verify that it is not tagged.
3645 if Ekind (Current_Scope) = E_Package
3646 and then Has_Private_Declaration (Ent)
3647 and then From_Aspect_Specification (N)
3648 and then
3649 List_Containing (Parent (Ent)) =
3650 Private_Declarations
3651 (Specification (Unit_Declaration_Node (Current_Scope)))
3652 and then Nkind (N) = N_Attribute_Definition_Clause
3653 then
3654 declare
3655 Decl : Node_Id;
3657 begin
3658 Decl :=
3659 First (Visible_Declarations
3660 (Specification
3661 (Unit_Declaration_Node (Current_Scope))));
3663 while Present (Decl) loop
3664 if Nkind (Decl) = N_Private_Type_Declaration
3665 and then Ent = Full_View (Defining_Identifier (Decl))
3666 and then Tagged_Present (Decl)
3667 and then No (Aspect_Specifications (Decl))
3668 then
3669 Illegal_Indexing
3670 ("Indexing aspect cannot be specified on full view "
3671 & "if partial view is tagged");
3672 return;
3673 end if;
3675 Next (Decl);
3676 end loop;
3677 end;
3678 end if;
3680 -- An indexing function must return either the default element of
3681 -- the container, or a reference type. For variable indexing it
3682 -- must be the latter.
3684 Default_Element :=
3685 Find_Value_Of_Aspect
3686 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3688 if Present (Default_Element) then
3689 Analyze (Default_Element);
3691 if Is_Entity_Name (Default_Element)
3692 and then not Covers (Entity (Default_Element), Ret_Type)
3693 and then False
3694 then
3695 Illegal_Indexing
3696 ("wrong return type for indexing function");
3697 return;
3698 end if;
3699 end if;
3701 -- For variable_indexing the return type must be a reference type
3703 if Attr = Name_Variable_Indexing then
3704 if not Has_Implicit_Dereference (Ret_Type) then
3705 Illegal_Indexing
3706 ("variable indexing must return a reference type");
3707 return;
3709 elsif Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3710 then
3711 Illegal_Indexing
3712 ("variable indexing must return an access to variable");
3713 return;
3714 end if;
3716 else
3717 if Has_Implicit_Dereference (Ret_Type)
3718 and then not
3719 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3720 then
3721 Illegal_Indexing
3722 ("constant indexing must return an access to constant");
3723 return;
3725 elsif Is_Access_Type (Etype (First_Formal (Subp)))
3726 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
3727 then
3728 Illegal_Indexing
3729 ("constant indexing must apply to an access to constant");
3730 return;
3731 end if;
3732 end if;
3734 -- All checks succeeded.
3736 Indexing_Found := True;
3737 end Check_One_Function;
3739 -----------------------
3740 -- Illegal_Indexing --
3741 -----------------------
3743 procedure Illegal_Indexing (Msg : String) is
3744 begin
3745 Error_Msg_NE (Msg, N, Ent);
3746 end Illegal_Indexing;
3748 -- Start of processing for Check_Indexing_Functions
3750 begin
3751 if In_Instance then
3752 return;
3753 end if;
3755 Analyze (Expr);
3757 if not Is_Overloaded (Expr) then
3758 Check_One_Function (Entity (Expr));
3760 else
3761 declare
3762 I : Interp_Index;
3763 It : Interp;
3765 begin
3766 Indexing_Found := False;
3767 Get_First_Interp (Expr, I, It);
3768 while Present (It.Nam) loop
3770 -- Note that analysis will have added the interpretation
3771 -- that corresponds to the dereference. We only check the
3772 -- subprogram itself.
3774 if Is_Overloadable (It.Nam) then
3775 Check_One_Function (It.Nam);
3776 end if;
3778 Get_Next_Interp (I, It);
3779 end loop;
3780 end;
3781 end if;
3783 if not Indexing_Found and then not Error_Posted (N) then
3784 Error_Msg_NE
3785 ("aspect Indexing requires a local function that "
3786 & "applies to type&", Expr, Ent);
3787 end if;
3788 end Check_Indexing_Functions;
3790 ------------------------------
3791 -- Check_Iterator_Functions --
3792 ------------------------------
3794 procedure Check_Iterator_Functions is
3795 Default : Entity_Id;
3797 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3798 -- Check one possible interpretation for validity
3800 ----------------------------
3801 -- Valid_Default_Iterator --
3802 ----------------------------
3804 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3805 Formal : Entity_Id;
3807 begin
3808 if not Check_Primitive_Function (Subp) then
3809 return False;
3810 else
3811 Formal := First_Formal (Subp);
3812 end if;
3814 -- False if any subsequent formal has no default expression
3816 Formal := Next_Formal (Formal);
3817 while Present (Formal) loop
3818 if No (Expression (Parent (Formal))) then
3819 return False;
3820 end if;
3822 Next_Formal (Formal);
3823 end loop;
3825 -- True if all subsequent formals have default expressions
3827 return True;
3828 end Valid_Default_Iterator;
3830 -- Start of processing for Check_Iterator_Functions
3832 begin
3833 Analyze (Expr);
3835 if not Is_Entity_Name (Expr) then
3836 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3837 end if;
3839 if not Is_Overloaded (Expr) then
3840 if not Check_Primitive_Function (Entity (Expr)) then
3841 Error_Msg_NE
3842 ("aspect Indexing requires a function that applies to type&",
3843 Entity (Expr), Ent);
3844 end if;
3846 if not Valid_Default_Iterator (Entity (Expr)) then
3847 Error_Msg_N ("improper function for default iterator", Expr);
3848 end if;
3850 else
3851 Default := Empty;
3852 declare
3853 I : Interp_Index;
3854 It : Interp;
3856 begin
3857 Get_First_Interp (Expr, I, It);
3858 while Present (It.Nam) loop
3859 if not Check_Primitive_Function (It.Nam)
3860 or else not Valid_Default_Iterator (It.Nam)
3861 then
3862 Remove_Interp (I);
3864 elsif Present (Default) then
3865 Error_Msg_N ("default iterator must be unique", Expr);
3867 else
3868 Default := It.Nam;
3869 end if;
3871 Get_Next_Interp (I, It);
3872 end loop;
3873 end;
3875 if Present (Default) then
3876 Set_Entity (Expr, Default);
3877 Set_Is_Overloaded (Expr, False);
3878 end if;
3879 end if;
3880 end Check_Iterator_Functions;
3882 -------------------------------
3883 -- Check_Primitive_Function --
3884 -------------------------------
3886 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3887 Ctrl : Entity_Id;
3889 begin
3890 if Ekind (Subp) /= E_Function then
3891 return False;
3892 end if;
3894 if No (First_Formal (Subp)) then
3895 return False;
3896 else
3897 Ctrl := Etype (First_Formal (Subp));
3898 end if;
3900 if Ctrl = Ent
3901 or else Ctrl = Class_Wide_Type (Ent)
3902 or else
3903 (Ekind (Ctrl) = E_Anonymous_Access_Type
3904 and then
3905 (Designated_Type (Ctrl) = Ent
3906 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
3907 then
3908 null;
3910 else
3911 return False;
3912 end if;
3914 return True;
3915 end Check_Primitive_Function;
3917 ----------------------
3918 -- Duplicate_Clause --
3919 ----------------------
3921 function Duplicate_Clause return Boolean is
3922 A : Node_Id;
3924 begin
3925 -- Nothing to do if this attribute definition clause comes from
3926 -- an aspect specification, since we could not be duplicating an
3927 -- explicit clause, and we dealt with the case of duplicated aspects
3928 -- in Analyze_Aspect_Specifications.
3930 if From_Aspect_Specification (N) then
3931 return False;
3932 end if;
3934 -- Otherwise current clause may duplicate previous clause, or a
3935 -- previously given pragma or aspect specification for the same
3936 -- aspect.
3938 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
3940 if Present (A) then
3941 Error_Msg_Name_1 := Chars (N);
3942 Error_Msg_Sloc := Sloc (A);
3944 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
3945 return True;
3946 end if;
3948 return False;
3949 end Duplicate_Clause;
3951 -- Start of processing for Analyze_Attribute_Definition_Clause
3953 begin
3954 -- The following code is a defense against recursion. Not clear that
3955 -- this can happen legitimately, but perhaps some error situations can
3956 -- cause it, and we did see this recursion during testing.
3958 if Analyzed (N) then
3959 return;
3960 else
3961 Set_Analyzed (N, True);
3962 end if;
3964 -- Ignore some selected attributes in CodePeer mode since they are not
3965 -- relevant in this context.
3967 if CodePeer_Mode then
3968 case Id is
3970 -- Ignore Component_Size in CodePeer mode, to avoid changing the
3971 -- internal representation of types by implicitly packing them.
3973 when Attribute_Component_Size =>
3974 Rewrite (N, Make_Null_Statement (Sloc (N)));
3975 return;
3977 when others =>
3978 null;
3979 end case;
3980 end if;
3982 -- Process Ignore_Rep_Clauses option
3984 if Ignore_Rep_Clauses then
3985 case Id is
3987 -- The following should be ignored. They do not affect legality
3988 -- and may be target dependent. The basic idea of -gnatI is to
3989 -- ignore any rep clauses that may be target dependent but do not
3990 -- affect legality (except possibly to be rejected because they
3991 -- are incompatible with the compilation target).
3993 when Attribute_Alignment |
3994 Attribute_Bit_Order |
3995 Attribute_Component_Size |
3996 Attribute_Machine_Radix |
3997 Attribute_Object_Size |
3998 Attribute_Size |
3999 Attribute_Small |
4000 Attribute_Stream_Size |
4001 Attribute_Value_Size =>
4002 Kill_Rep_Clause (N);
4003 return;
4005 -- The following should not be ignored, because in the first place
4006 -- they are reasonably portable, and should not cause problems
4007 -- in compiling code from another target, and also they do affect
4008 -- legality, e.g. failing to provide a stream attribute for a type
4009 -- may make a program illegal.
4011 when Attribute_External_Tag |
4012 Attribute_Input |
4013 Attribute_Output |
4014 Attribute_Read |
4015 Attribute_Simple_Storage_Pool |
4016 Attribute_Storage_Pool |
4017 Attribute_Storage_Size |
4018 Attribute_Write =>
4019 null;
4021 -- We do not do anything here with address clauses, they will be
4022 -- removed by Freeze later on, but for now, it works better to
4023 -- keep then in the tree.
4025 when Attribute_Address =>
4026 null;
4028 -- Other cases are errors ("attribute& cannot be set with
4029 -- definition clause"), which will be caught below.
4031 when others =>
4032 null;
4033 end case;
4034 end if;
4036 Analyze (Nam);
4037 Ent := Entity (Nam);
4039 if Rep_Item_Too_Early (Ent, N) then
4040 return;
4041 end if;
4043 -- Rep clause applies to full view of incomplete type or private type if
4044 -- we have one (if not, this is a premature use of the type). However,
4045 -- certain semantic checks need to be done on the specified entity (i.e.
4046 -- the private view), so we save it in Ent.
4048 if Is_Private_Type (Ent)
4049 and then Is_Derived_Type (Ent)
4050 and then not Is_Tagged_Type (Ent)
4051 and then No (Full_View (Ent))
4052 then
4053 -- If this is a private type whose completion is a derivation from
4054 -- another private type, there is no full view, and the attribute
4055 -- belongs to the type itself, not its underlying parent.
4057 U_Ent := Ent;
4059 elsif Ekind (Ent) = E_Incomplete_Type then
4061 -- The attribute applies to the full view, set the entity of the
4062 -- attribute definition accordingly.
4064 Ent := Underlying_Type (Ent);
4065 U_Ent := Ent;
4066 Set_Entity (Nam, Ent);
4068 else
4069 U_Ent := Underlying_Type (Ent);
4070 end if;
4072 -- Avoid cascaded error
4074 if Etype (Nam) = Any_Type then
4075 return;
4077 -- Must be declared in current scope or in case of an aspect
4078 -- specification, must be visible in current scope.
4080 elsif Scope (Ent) /= Current_Scope
4081 and then
4082 not (From_Aspect_Specification (N)
4083 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4084 then
4085 Error_Msg_N ("entity must be declared in this scope", Nam);
4086 return;
4088 -- Must not be a source renaming (we do have some cases where the
4089 -- expander generates a renaming, and those cases are OK, in such
4090 -- cases any attribute applies to the renamed object as well).
4092 elsif Is_Object (Ent)
4093 and then Present (Renamed_Object (Ent))
4094 then
4095 -- Case of renamed object from source, this is an error
4097 if Comes_From_Source (Renamed_Object (Ent)) then
4098 Get_Name_String (Chars (N));
4099 Error_Msg_Strlen := Name_Len;
4100 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4101 Error_Msg_N
4102 ("~ clause not allowed for a renaming declaration "
4103 & "(RM 13.1(6))", Nam);
4104 return;
4106 -- For the case of a compiler generated renaming, the attribute
4107 -- definition clause applies to the renamed object created by the
4108 -- expander. The easiest general way to handle this is to create a
4109 -- copy of the attribute definition clause for this object.
4111 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4112 Insert_Action (N,
4113 Make_Attribute_Definition_Clause (Loc,
4114 Name =>
4115 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4116 Chars => Chars (N),
4117 Expression => Duplicate_Subexpr (Expression (N))));
4119 -- If the renamed object is not an entity, it must be a dereference
4120 -- of an unconstrained function call, and we must introduce a new
4121 -- declaration to capture the expression. This is needed in the case
4122 -- of 'Alignment, where the original declaration must be rewritten.
4124 else
4125 pragma Assert
4126 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4127 null;
4128 end if;
4130 -- If no underlying entity, use entity itself, applies to some
4131 -- previously detected error cases ???
4133 elsif No (U_Ent) then
4134 U_Ent := Ent;
4136 -- Cannot specify for a subtype (exception Object/Value_Size)
4138 elsif Is_Type (U_Ent)
4139 and then not Is_First_Subtype (U_Ent)
4140 and then Id /= Attribute_Object_Size
4141 and then Id /= Attribute_Value_Size
4142 and then not From_At_Mod (N)
4143 then
4144 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4145 return;
4146 end if;
4148 Set_Entity (N, U_Ent);
4149 Check_Restriction_No_Use_Of_Attribute (N);
4151 -- Switch on particular attribute
4153 case Id is
4155 -------------
4156 -- Address --
4157 -------------
4159 -- Address attribute definition clause
4161 when Attribute_Address => Address : begin
4163 -- A little error check, catch for X'Address use X'Address;
4165 if Nkind (Nam) = N_Identifier
4166 and then Nkind (Expr) = N_Attribute_Reference
4167 and then Attribute_Name (Expr) = Name_Address
4168 and then Nkind (Prefix (Expr)) = N_Identifier
4169 and then Chars (Nam) = Chars (Prefix (Expr))
4170 then
4171 Error_Msg_NE
4172 ("address for & is self-referencing", Prefix (Expr), Ent);
4173 return;
4174 end if;
4176 -- Not that special case, carry on with analysis of expression
4178 Analyze_And_Resolve (Expr, RTE (RE_Address));
4180 -- Even when ignoring rep clauses we need to indicate that the
4181 -- entity has an address clause and thus it is legal to declare
4182 -- it imported. Freeze will get rid of the address clause later.
4184 if Ignore_Rep_Clauses then
4185 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4186 Record_Rep_Item (U_Ent, N);
4187 end if;
4189 return;
4190 end if;
4192 if Duplicate_Clause then
4193 null;
4195 -- Case of address clause for subprogram
4197 elsif Is_Subprogram (U_Ent) then
4198 if Has_Homonym (U_Ent) then
4199 Error_Msg_N
4200 ("address clause cannot be given " &
4201 "for overloaded subprogram",
4202 Nam);
4203 return;
4204 end if;
4206 -- For subprograms, all address clauses are permitted, and we
4207 -- mark the subprogram as having a deferred freeze so that Gigi
4208 -- will not elaborate it too soon.
4210 -- Above needs more comments, what is too soon about???
4212 Set_Has_Delayed_Freeze (U_Ent);
4214 -- Case of address clause for entry
4216 elsif Ekind (U_Ent) = E_Entry then
4217 if Nkind (Parent (N)) = N_Task_Body then
4218 Error_Msg_N
4219 ("entry address must be specified in task spec", Nam);
4220 return;
4221 end if;
4223 -- For entries, we require a constant address
4225 Check_Constant_Address_Clause (Expr, U_Ent);
4227 -- Special checks for task types
4229 if Is_Task_Type (Scope (U_Ent))
4230 and then Comes_From_Source (Scope (U_Ent))
4231 then
4232 Error_Msg_N
4233 ("??entry address declared for entry in task type", N);
4234 Error_Msg_N
4235 ("\??only one task can be declared of this type", N);
4236 end if;
4238 -- Entry address clauses are obsolescent
4240 Check_Restriction (No_Obsolescent_Features, N);
4242 if Warn_On_Obsolescent_Feature then
4243 Error_Msg_N
4244 ("?j?attaching interrupt to task entry is an " &
4245 "obsolescent feature (RM J.7.1)", N);
4246 Error_Msg_N
4247 ("\?j?use interrupt procedure instead", N);
4248 end if;
4250 -- Case of an address clause for a controlled object which we
4251 -- consider to be erroneous.
4253 elsif Is_Controlled (Etype (U_Ent))
4254 or else Has_Controlled_Component (Etype (U_Ent))
4255 then
4256 Error_Msg_NE
4257 ("??controlled object& must not be overlaid", Nam, U_Ent);
4258 Error_Msg_N
4259 ("\??Program_Error will be raised at run time", Nam);
4260 Insert_Action (Declaration_Node (U_Ent),
4261 Make_Raise_Program_Error (Loc,
4262 Reason => PE_Overlaid_Controlled_Object));
4263 return;
4265 -- Case of address clause for a (non-controlled) object
4267 elsif
4268 Ekind (U_Ent) = E_Variable
4269 or else
4270 Ekind (U_Ent) = E_Constant
4271 then
4272 declare
4273 Expr : constant Node_Id := Expression (N);
4274 O_Ent : Entity_Id;
4275 Off : Boolean;
4277 begin
4278 -- Exported variables cannot have an address clause, because
4279 -- this cancels the effect of the pragma Export.
4281 if Is_Exported (U_Ent) then
4282 Error_Msg_N
4283 ("cannot export object with address clause", Nam);
4284 return;
4285 end if;
4287 Find_Overlaid_Entity (N, O_Ent, Off);
4289 -- Overlaying controlled objects is erroneous
4291 if Present (O_Ent)
4292 and then (Has_Controlled_Component (Etype (O_Ent))
4293 or else Is_Controlled (Etype (O_Ent)))
4294 then
4295 Error_Msg_N
4296 ("??cannot overlay with controlled object", Expr);
4297 Error_Msg_N
4298 ("\??Program_Error will be raised at run time", Expr);
4299 Insert_Action (Declaration_Node (U_Ent),
4300 Make_Raise_Program_Error (Loc,
4301 Reason => PE_Overlaid_Controlled_Object));
4302 return;
4304 elsif Present (O_Ent)
4305 and then Ekind (U_Ent) = E_Constant
4306 and then not Is_Constant_Object (O_Ent)
4307 then
4308 Error_Msg_N ("??constant overlays a variable", Expr);
4310 -- Imported variables can have an address clause, but then
4311 -- the import is pretty meaningless except to suppress
4312 -- initializations, so we do not need such variables to
4313 -- be statically allocated (and in fact it causes trouble
4314 -- if the address clause is a local value).
4316 elsif Is_Imported (U_Ent) then
4317 Set_Is_Statically_Allocated (U_Ent, False);
4318 end if;
4320 -- We mark a possible modification of a variable with an
4321 -- address clause, since it is likely aliasing is occurring.
4323 Note_Possible_Modification (Nam, Sure => False);
4325 -- Here we are checking for explicit overlap of one variable
4326 -- by another, and if we find this then mark the overlapped
4327 -- variable as also being volatile to prevent unwanted
4328 -- optimizations. This is a significant pessimization so
4329 -- avoid it when there is an offset, i.e. when the object
4330 -- is composite; they cannot be optimized easily anyway.
4332 if Present (O_Ent)
4333 and then Is_Object (O_Ent)
4334 and then not Off
4336 -- The following test is an expedient solution to what
4337 -- is really a problem in CodePeer. Suppressing the
4338 -- Set_Treat_As_Volatile call here prevents later
4339 -- generation (in some cases) of trees that CodePeer
4340 -- should, but currently does not, handle correctly.
4341 -- This test should probably be removed when CodePeer
4342 -- is improved, just because we want the tree CodePeer
4343 -- analyzes to match the tree for which we generate code
4344 -- as closely as is practical. ???
4346 and then not CodePeer_Mode
4347 then
4348 -- ??? O_Ent might not be in current unit
4350 Set_Treat_As_Volatile (O_Ent);
4351 end if;
4353 -- Legality checks on the address clause for initialized
4354 -- objects is deferred until the freeze point, because
4355 -- a subsequent pragma might indicate that the object
4356 -- is imported and thus not initialized. Also, the address
4357 -- clause might involve entities that have yet to be
4358 -- elaborated.
4360 Set_Has_Delayed_Freeze (U_Ent);
4362 -- If an initialization call has been generated for this
4363 -- object, it needs to be deferred to after the freeze node
4364 -- we have just now added, otherwise GIGI will see a
4365 -- reference to the variable (as actual to the IP call)
4366 -- before its definition.
4368 declare
4369 Init_Call : constant Node_Id :=
4370 Remove_Init_Call (U_Ent, N);
4372 begin
4373 if Present (Init_Call) then
4374 Append_Freeze_Action (U_Ent, Init_Call);
4376 -- Reset Initialization_Statements pointer so that
4377 -- if there is a pragma Import further down, it can
4378 -- clear any default initialization.
4380 Set_Initialization_Statements (U_Ent, Init_Call);
4381 end if;
4382 end;
4384 if Is_Exported (U_Ent) then
4385 Error_Msg_N
4386 ("& cannot be exported if an address clause is given",
4387 Nam);
4388 Error_Msg_N
4389 ("\define and export a variable "
4390 & "that holds its address instead", Nam);
4391 end if;
4393 -- Entity has delayed freeze, so we will generate an
4394 -- alignment check at the freeze point unless suppressed.
4396 if not Range_Checks_Suppressed (U_Ent)
4397 and then not Alignment_Checks_Suppressed (U_Ent)
4398 then
4399 Set_Check_Address_Alignment (N);
4400 end if;
4402 -- Kill the size check code, since we are not allocating
4403 -- the variable, it is somewhere else.
4405 Kill_Size_Check_Code (U_Ent);
4407 -- If the address clause is of the form:
4409 -- for Y'Address use X'Address
4411 -- or
4413 -- Const : constant Address := X'Address;
4414 -- ...
4415 -- for Y'Address use Const;
4417 -- then we make an entry in the table for checking the size
4418 -- and alignment of the overlaying variable. We defer this
4419 -- check till after code generation to take full advantage
4420 -- of the annotation done by the back end.
4422 -- If the entity has a generic type, the check will be
4423 -- performed in the instance if the actual type justifies
4424 -- it, and we do not insert the clause in the table to
4425 -- prevent spurious warnings.
4427 -- Note: we used to test Comes_From_Source and only give
4428 -- this warning for source entities, but we have removed
4429 -- this test. It really seems bogus to generate overlays
4430 -- that would trigger this warning in generated code.
4431 -- Furthermore, by removing the test, we handle the
4432 -- aspect case properly.
4434 if Address_Clause_Overlay_Warnings
4435 and then Present (O_Ent)
4436 and then Is_Object (O_Ent)
4437 then
4438 if not Is_Generic_Type (Etype (U_Ent)) then
4439 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4440 end if;
4442 -- If variable overlays a constant view, and we are
4443 -- warning on overlays, then mark the variable as
4444 -- overlaying a constant (we will give warnings later
4445 -- if this variable is assigned).
4447 if Is_Constant_Object (O_Ent)
4448 and then Ekind (U_Ent) = E_Variable
4449 then
4450 Set_Overlays_Constant (U_Ent);
4451 end if;
4452 end if;
4453 end;
4455 -- Not a valid entity for an address clause
4457 else
4458 Error_Msg_N ("address cannot be given for &", Nam);
4459 end if;
4460 end Address;
4462 ---------------
4463 -- Alignment --
4464 ---------------
4466 -- Alignment attribute definition clause
4468 when Attribute_Alignment => Alignment : declare
4469 Align : constant Uint := Get_Alignment_Value (Expr);
4470 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4472 begin
4473 FOnly := True;
4475 if not Is_Type (U_Ent)
4476 and then Ekind (U_Ent) /= E_Variable
4477 and then Ekind (U_Ent) /= E_Constant
4478 then
4479 Error_Msg_N ("alignment cannot be given for &", Nam);
4481 elsif Duplicate_Clause then
4482 null;
4484 elsif Align /= No_Uint then
4485 Set_Has_Alignment_Clause (U_Ent);
4487 -- Tagged type case, check for attempt to set alignment to a
4488 -- value greater than Max_Align, and reset if so.
4490 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4491 Error_Msg_N
4492 ("alignment for & set to Maximum_Aligment??", Nam);
4493 Set_Alignment (U_Ent, Max_Align);
4495 -- All other cases
4497 else
4498 Set_Alignment (U_Ent, Align);
4499 end if;
4501 -- For an array type, U_Ent is the first subtype. In that case,
4502 -- also set the alignment of the anonymous base type so that
4503 -- other subtypes (such as the itypes for aggregates of the
4504 -- type) also receive the expected alignment.
4506 if Is_Array_Type (U_Ent) then
4507 Set_Alignment (Base_Type (U_Ent), Align);
4508 end if;
4509 end if;
4510 end Alignment;
4512 ---------------
4513 -- Bit_Order --
4514 ---------------
4516 -- Bit_Order attribute definition clause
4518 when Attribute_Bit_Order => Bit_Order : declare
4519 begin
4520 if not Is_Record_Type (U_Ent) then
4521 Error_Msg_N
4522 ("Bit_Order can only be defined for record type", Nam);
4524 elsif Duplicate_Clause then
4525 null;
4527 else
4528 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4530 if Etype (Expr) = Any_Type then
4531 return;
4533 elsif not Is_OK_Static_Expression (Expr) then
4534 Flag_Non_Static_Expr
4535 ("Bit_Order requires static expression!", Expr);
4537 else
4538 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4539 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4540 end if;
4541 end if;
4542 end if;
4543 end Bit_Order;
4545 --------------------
4546 -- Component_Size --
4547 --------------------
4549 -- Component_Size attribute definition clause
4551 when Attribute_Component_Size => Component_Size_Case : declare
4552 Csize : constant Uint := Static_Integer (Expr);
4553 Ctyp : Entity_Id;
4554 Btype : Entity_Id;
4555 Biased : Boolean;
4556 New_Ctyp : Entity_Id;
4557 Decl : Node_Id;
4559 begin
4560 if not Is_Array_Type (U_Ent) then
4561 Error_Msg_N ("component size requires array type", Nam);
4562 return;
4563 end if;
4565 Btype := Base_Type (U_Ent);
4566 Ctyp := Component_Type (Btype);
4568 if Duplicate_Clause then
4569 null;
4571 elsif Rep_Item_Too_Early (Btype, N) then
4572 null;
4574 elsif Csize /= No_Uint then
4575 Check_Size (Expr, Ctyp, Csize, Biased);
4577 -- For the biased case, build a declaration for a subtype that
4578 -- will be used to represent the biased subtype that reflects
4579 -- the biased representation of components. We need the subtype
4580 -- to get proper conversions on referencing elements of the
4581 -- array. Note: component size clauses are ignored in VM mode.
4583 if VM_Target = No_VM then
4584 if Biased then
4585 New_Ctyp :=
4586 Make_Defining_Identifier (Loc,
4587 Chars =>
4588 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4590 Decl :=
4591 Make_Subtype_Declaration (Loc,
4592 Defining_Identifier => New_Ctyp,
4593 Subtype_Indication =>
4594 New_Occurrence_Of (Component_Type (Btype), Loc));
4596 Set_Parent (Decl, N);
4597 Analyze (Decl, Suppress => All_Checks);
4599 Set_Has_Delayed_Freeze (New_Ctyp, False);
4600 Set_Esize (New_Ctyp, Csize);
4601 Set_RM_Size (New_Ctyp, Csize);
4602 Init_Alignment (New_Ctyp);
4603 Set_Is_Itype (New_Ctyp, True);
4604 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4606 Set_Component_Type (Btype, New_Ctyp);
4607 Set_Biased (New_Ctyp, N, "component size clause");
4608 end if;
4610 Set_Component_Size (Btype, Csize);
4612 -- For VM case, we ignore component size clauses
4614 else
4615 -- Give a warning unless we are in GNAT mode, in which case
4616 -- the warning is suppressed since it is not useful.
4618 if not GNAT_Mode then
4619 Error_Msg_N
4620 ("component size ignored in this configuration??", N);
4621 end if;
4622 end if;
4624 -- Deal with warning on overridden size
4626 if Warn_On_Overridden_Size
4627 and then Has_Size_Clause (Ctyp)
4628 and then RM_Size (Ctyp) /= Csize
4629 then
4630 Error_Msg_NE
4631 ("component size overrides size clause for&?S?", N, Ctyp);
4632 end if;
4634 Set_Has_Component_Size_Clause (Btype, True);
4635 Set_Has_Non_Standard_Rep (Btype, True);
4636 end if;
4637 end Component_Size_Case;
4639 -----------------------
4640 -- Constant_Indexing --
4641 -----------------------
4643 when Attribute_Constant_Indexing =>
4644 Check_Indexing_Functions;
4646 ---------
4647 -- CPU --
4648 ---------
4650 when Attribute_CPU => CPU :
4651 begin
4652 -- CPU attribute definition clause not allowed except from aspect
4653 -- specification.
4655 if From_Aspect_Specification (N) then
4656 if not Is_Task_Type (U_Ent) then
4657 Error_Msg_N ("CPU can only be defined for task", Nam);
4659 elsif Duplicate_Clause then
4660 null;
4662 else
4663 -- The expression must be analyzed in the special manner
4664 -- described in "Handling of Default and Per-Object
4665 -- Expressions" in sem.ads.
4667 -- The visibility to the discriminants must be restored
4669 Push_Scope_And_Install_Discriminants (U_Ent);
4670 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4671 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4673 if not Is_OK_Static_Expression (Expr) then
4674 Check_Restriction (Static_Priorities, Expr);
4675 end if;
4676 end if;
4678 else
4679 Error_Msg_N
4680 ("attribute& cannot be set with definition clause", N);
4681 end if;
4682 end CPU;
4684 ----------------------
4685 -- Default_Iterator --
4686 ----------------------
4688 when Attribute_Default_Iterator => Default_Iterator : declare
4689 Func : Entity_Id;
4691 begin
4692 if not Is_Tagged_Type (U_Ent) then
4693 Error_Msg_N
4694 ("aspect Default_Iterator applies to tagged type", Nam);
4695 end if;
4697 Check_Iterator_Functions;
4699 Analyze (Expr);
4701 if not Is_Entity_Name (Expr)
4702 or else Ekind (Entity (Expr)) /= E_Function
4703 then
4704 Error_Msg_N ("aspect Iterator must be a function", Expr);
4705 else
4706 Func := Entity (Expr);
4707 end if;
4709 if No (First_Formal (Func))
4710 or else Etype (First_Formal (Func)) /= U_Ent
4711 then
4712 Error_Msg_NE
4713 ("Default Iterator must be a primitive of&", Func, U_Ent);
4714 end if;
4715 end Default_Iterator;
4717 ------------------------
4718 -- Dispatching_Domain --
4719 ------------------------
4721 when Attribute_Dispatching_Domain => Dispatching_Domain :
4722 begin
4723 -- Dispatching_Domain attribute definition clause not allowed
4724 -- except from aspect specification.
4726 if From_Aspect_Specification (N) then
4727 if not Is_Task_Type (U_Ent) then
4728 Error_Msg_N ("Dispatching_Domain can only be defined" &
4729 "for task",
4730 Nam);
4732 elsif Duplicate_Clause then
4733 null;
4735 else
4736 -- The expression must be analyzed in the special manner
4737 -- described in "Handling of Default and Per-Object
4738 -- Expressions" in sem.ads.
4740 -- The visibility to the discriminants must be restored
4742 Push_Scope_And_Install_Discriminants (U_Ent);
4744 Preanalyze_Spec_Expression
4745 (Expr, RTE (RE_Dispatching_Domain));
4747 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4748 end if;
4750 else
4751 Error_Msg_N
4752 ("attribute& cannot be set with definition clause", N);
4753 end if;
4754 end Dispatching_Domain;
4756 ------------------
4757 -- External_Tag --
4758 ------------------
4760 when Attribute_External_Tag => External_Tag :
4761 begin
4762 if not Is_Tagged_Type (U_Ent) then
4763 Error_Msg_N ("should be a tagged type", Nam);
4764 end if;
4766 if Duplicate_Clause then
4767 null;
4769 else
4770 Analyze_And_Resolve (Expr, Standard_String);
4772 if not Is_OK_Static_Expression (Expr) then
4773 Flag_Non_Static_Expr
4774 ("static string required for tag name!", Nam);
4775 end if;
4777 if VM_Target /= No_VM then
4778 Error_Msg_Name_1 := Attr;
4779 Error_Msg_N
4780 ("% attribute unsupported in this configuration", Nam);
4781 end if;
4783 if not Is_Library_Level_Entity (U_Ent) then
4784 Error_Msg_NE
4785 ("??non-unique external tag supplied for &", N, U_Ent);
4786 Error_Msg_N
4787 ("\??same external tag applies to all "
4788 & "subprogram calls", N);
4789 Error_Msg_N
4790 ("\??corresponding internal tag cannot be obtained", N);
4791 end if;
4792 end if;
4793 end External_Tag;
4795 --------------------------
4796 -- Implicit_Dereference --
4797 --------------------------
4799 when Attribute_Implicit_Dereference =>
4801 -- Legality checks already performed at the point of the type
4802 -- declaration, aspect is not delayed.
4804 null;
4806 -----------
4807 -- Input --
4808 -----------
4810 when Attribute_Input =>
4811 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4812 Set_Has_Specified_Stream_Input (Ent);
4814 ------------------------
4815 -- Interrupt_Priority --
4816 ------------------------
4818 when Attribute_Interrupt_Priority => Interrupt_Priority :
4819 begin
4820 -- Interrupt_Priority attribute definition clause not allowed
4821 -- except from aspect specification.
4823 if From_Aspect_Specification (N) then
4824 if not (Is_Protected_Type (U_Ent)
4825 or else Is_Task_Type (U_Ent))
4826 then
4827 Error_Msg_N
4828 ("Interrupt_Priority can only be defined for task" &
4829 "and protected object",
4830 Nam);
4832 elsif Duplicate_Clause then
4833 null;
4835 else
4836 -- The expression must be analyzed in the special manner
4837 -- described in "Handling of Default and Per-Object
4838 -- Expressions" in sem.ads.
4840 -- The visibility to the discriminants must be restored
4842 Push_Scope_And_Install_Discriminants (U_Ent);
4844 Preanalyze_Spec_Expression
4845 (Expr, RTE (RE_Interrupt_Priority));
4847 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4848 end if;
4850 else
4851 Error_Msg_N
4852 ("attribute& cannot be set with definition clause", N);
4853 end if;
4854 end Interrupt_Priority;
4856 --------------
4857 -- Iterable --
4858 --------------
4860 when Attribute_Iterable =>
4861 Analyze (Expr);
4863 if Nkind (Expr) /= N_Aggregate then
4864 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
4865 end if;
4867 declare
4868 Assoc : Node_Id;
4870 begin
4871 Assoc := First (Component_Associations (Expr));
4872 while Present (Assoc) loop
4873 if not Is_Entity_Name (Expression (Assoc)) then
4874 Error_Msg_N ("value must be a function", Assoc);
4875 end if;
4877 Next (Assoc);
4878 end loop;
4879 end;
4881 ----------------------
4882 -- Iterator_Element --
4883 ----------------------
4885 when Attribute_Iterator_Element =>
4886 Analyze (Expr);
4888 if not Is_Entity_Name (Expr)
4889 or else not Is_Type (Entity (Expr))
4890 then
4891 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4892 end if;
4894 -------------------
4895 -- Machine_Radix --
4896 -------------------
4898 -- Machine radix attribute definition clause
4900 when Attribute_Machine_Radix => Machine_Radix : declare
4901 Radix : constant Uint := Static_Integer (Expr);
4903 begin
4904 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
4905 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
4907 elsif Duplicate_Clause then
4908 null;
4910 elsif Radix /= No_Uint then
4911 Set_Has_Machine_Radix_Clause (U_Ent);
4912 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
4914 if Radix = 2 then
4915 null;
4916 elsif Radix = 10 then
4917 Set_Machine_Radix_10 (U_Ent);
4918 else
4919 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
4920 end if;
4921 end if;
4922 end Machine_Radix;
4924 -----------------
4925 -- Object_Size --
4926 -----------------
4928 -- Object_Size attribute definition clause
4930 when Attribute_Object_Size => Object_Size : declare
4931 Size : constant Uint := Static_Integer (Expr);
4933 Biased : Boolean;
4934 pragma Warnings (Off, Biased);
4936 begin
4937 if not Is_Type (U_Ent) then
4938 Error_Msg_N ("Object_Size cannot be given for &", Nam);
4940 elsif Duplicate_Clause then
4941 null;
4943 else
4944 Check_Size (Expr, U_Ent, Size, Biased);
4946 if Is_Scalar_Type (U_Ent) then
4947 if Size /= 8 and then Size /= 16 and then Size /= 32
4948 and then UI_Mod (Size, 64) /= 0
4949 then
4950 Error_Msg_N
4951 ("Object_Size must be 8, 16, 32, or multiple of 64",
4952 Expr);
4953 end if;
4955 elsif Size mod 8 /= 0 then
4956 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
4957 end if;
4959 Set_Esize (U_Ent, Size);
4960 Set_Has_Object_Size_Clause (U_Ent);
4961 Alignment_Check_For_Size_Change (U_Ent, Size);
4962 end if;
4963 end Object_Size;
4965 ------------
4966 -- Output --
4967 ------------
4969 when Attribute_Output =>
4970 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
4971 Set_Has_Specified_Stream_Output (Ent);
4973 --------------
4974 -- Priority --
4975 --------------
4977 when Attribute_Priority => Priority :
4978 begin
4979 -- Priority attribute definition clause not allowed except from
4980 -- aspect specification.
4982 if From_Aspect_Specification (N) then
4983 if not (Is_Protected_Type (U_Ent)
4984 or else Is_Task_Type (U_Ent)
4985 or else Ekind (U_Ent) = E_Procedure)
4986 then
4987 Error_Msg_N
4988 ("Priority can only be defined for task and protected " &
4989 "object",
4990 Nam);
4992 elsif Duplicate_Clause then
4993 null;
4995 else
4996 -- The expression must be analyzed in the special manner
4997 -- described in "Handling of Default and Per-Object
4998 -- Expressions" in sem.ads.
5000 -- The visibility to the discriminants must be restored
5002 Push_Scope_And_Install_Discriminants (U_Ent);
5003 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5004 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5006 if not Is_OK_Static_Expression (Expr) then
5007 Check_Restriction (Static_Priorities, Expr);
5008 end if;
5009 end if;
5011 else
5012 Error_Msg_N
5013 ("attribute& cannot be set with definition clause", N);
5014 end if;
5015 end Priority;
5017 ----------
5018 -- Read --
5019 ----------
5021 when Attribute_Read =>
5022 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5023 Set_Has_Specified_Stream_Read (Ent);
5025 --------------------------
5026 -- Scalar_Storage_Order --
5027 --------------------------
5029 -- Scalar_Storage_Order attribute definition clause
5031 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5032 begin
5033 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5034 Error_Msg_N
5035 ("Scalar_Storage_Order can only be defined for "
5036 & "record or array type", Nam);
5038 elsif Duplicate_Clause then
5039 null;
5041 else
5042 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5044 if Etype (Expr) = Any_Type then
5045 return;
5047 elsif not Is_OK_Static_Expression (Expr) then
5048 Flag_Non_Static_Expr
5049 ("Scalar_Storage_Order requires static expression!", Expr);
5051 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5053 -- Here for the case of a non-default (i.e. non-confirming)
5054 -- Scalar_Storage_Order attribute definition.
5056 if Support_Nondefault_SSO_On_Target then
5057 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5058 else
5059 Error_Msg_N
5060 ("non-default Scalar_Storage_Order "
5061 & "not supported on target", Expr);
5062 end if;
5063 end if;
5065 -- Clear SSO default indications since explicit setting of the
5066 -- order overrides the defaults.
5068 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5069 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5070 end if;
5071 end Scalar_Storage_Order;
5073 ----------
5074 -- Size --
5075 ----------
5077 -- Size attribute definition clause
5079 when Attribute_Size => Size : declare
5080 Size : constant Uint := Static_Integer (Expr);
5081 Etyp : Entity_Id;
5082 Biased : Boolean;
5084 begin
5085 FOnly := True;
5087 if Duplicate_Clause then
5088 null;
5090 elsif not Is_Type (U_Ent)
5091 and then Ekind (U_Ent) /= E_Variable
5092 and then Ekind (U_Ent) /= E_Constant
5093 then
5094 Error_Msg_N ("size cannot be given for &", Nam);
5096 elsif Is_Array_Type (U_Ent)
5097 and then not Is_Constrained (U_Ent)
5098 then
5099 Error_Msg_N
5100 ("size cannot be given for unconstrained array", Nam);
5102 elsif Size /= No_Uint then
5103 if VM_Target /= No_VM and then not GNAT_Mode then
5105 -- Size clause is not handled properly on VM targets.
5106 -- Display a warning unless we are in GNAT mode, in which
5107 -- case this is useless.
5109 Error_Msg_N
5110 ("size clauses are ignored in this configuration??", N);
5111 end if;
5113 if Is_Type (U_Ent) then
5114 Etyp := U_Ent;
5115 else
5116 Etyp := Etype (U_Ent);
5117 end if;
5119 -- Check size, note that Gigi is in charge of checking that the
5120 -- size of an array or record type is OK. Also we do not check
5121 -- the size in the ordinary fixed-point case, since it is too
5122 -- early to do so (there may be subsequent small clause that
5123 -- affects the size). We can check the size if a small clause
5124 -- has already been given.
5126 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5127 or else Has_Small_Clause (U_Ent)
5128 then
5129 Check_Size (Expr, Etyp, Size, Biased);
5130 Set_Biased (U_Ent, N, "size clause", Biased);
5131 end if;
5133 -- For types set RM_Size and Esize if possible
5135 if Is_Type (U_Ent) then
5136 Set_RM_Size (U_Ent, Size);
5138 -- For elementary types, increase Object_Size to power of 2,
5139 -- but not less than a storage unit in any case (normally
5140 -- this means it will be byte addressable).
5142 -- For all other types, nothing else to do, we leave Esize
5143 -- (object size) unset, the back end will set it from the
5144 -- size and alignment in an appropriate manner.
5146 -- In both cases, we check whether the alignment must be
5147 -- reset in the wake of the size change.
5149 if Is_Elementary_Type (U_Ent) then
5150 if Size <= System_Storage_Unit then
5151 Init_Esize (U_Ent, System_Storage_Unit);
5152 elsif Size <= 16 then
5153 Init_Esize (U_Ent, 16);
5154 elsif Size <= 32 then
5155 Init_Esize (U_Ent, 32);
5156 else
5157 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5158 end if;
5160 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5161 else
5162 Alignment_Check_For_Size_Change (U_Ent, Size);
5163 end if;
5165 -- For objects, set Esize only
5167 else
5168 if Is_Elementary_Type (Etyp) then
5169 if Size /= System_Storage_Unit
5170 and then
5171 Size /= System_Storage_Unit * 2
5172 and then
5173 Size /= System_Storage_Unit * 4
5174 and then
5175 Size /= System_Storage_Unit * 8
5176 then
5177 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5178 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5179 Error_Msg_N
5180 ("size for primitive object must be a power of 2"
5181 & " in the range ^-^", N);
5182 end if;
5183 end if;
5185 Set_Esize (U_Ent, Size);
5186 end if;
5188 Set_Has_Size_Clause (U_Ent);
5189 end if;
5190 end Size;
5192 -----------
5193 -- Small --
5194 -----------
5196 -- Small attribute definition clause
5198 when Attribute_Small => Small : declare
5199 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5200 Small : Ureal;
5202 begin
5203 Analyze_And_Resolve (Expr, Any_Real);
5205 if Etype (Expr) = Any_Type then
5206 return;
5208 elsif not Is_OK_Static_Expression (Expr) then
5209 Flag_Non_Static_Expr
5210 ("small requires static expression!", Expr);
5211 return;
5213 else
5214 Small := Expr_Value_R (Expr);
5216 if Small <= Ureal_0 then
5217 Error_Msg_N ("small value must be greater than zero", Expr);
5218 return;
5219 end if;
5221 end if;
5223 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5224 Error_Msg_N
5225 ("small requires an ordinary fixed point type", Nam);
5227 elsif Has_Small_Clause (U_Ent) then
5228 Error_Msg_N ("small already given for &", Nam);
5230 elsif Small > Delta_Value (U_Ent) then
5231 Error_Msg_N
5232 ("small value must not be greater than delta value", Nam);
5234 else
5235 Set_Small_Value (U_Ent, Small);
5236 Set_Small_Value (Implicit_Base, Small);
5237 Set_Has_Small_Clause (U_Ent);
5238 Set_Has_Small_Clause (Implicit_Base);
5239 Set_Has_Non_Standard_Rep (Implicit_Base);
5240 end if;
5241 end Small;
5243 ------------------
5244 -- Storage_Pool --
5245 ------------------
5247 -- Storage_Pool attribute definition clause
5249 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5250 Pool : Entity_Id;
5251 T : Entity_Id;
5253 begin
5254 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5255 Error_Msg_N
5256 ("storage pool cannot be given for access-to-subprogram type",
5257 Nam);
5258 return;
5260 elsif not
5261 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5262 then
5263 Error_Msg_N
5264 ("storage pool can only be given for access types", Nam);
5265 return;
5267 elsif Is_Derived_Type (U_Ent) then
5268 Error_Msg_N
5269 ("storage pool cannot be given for a derived access type",
5270 Nam);
5272 elsif Duplicate_Clause then
5273 return;
5275 elsif Present (Associated_Storage_Pool (U_Ent)) then
5276 Error_Msg_N ("storage pool already given for &", Nam);
5277 return;
5278 end if;
5280 -- Check for Storage_Size previously given
5282 declare
5283 SS : constant Node_Id :=
5284 Get_Attribute_Definition_Clause
5285 (U_Ent, Attribute_Storage_Size);
5286 begin
5287 if Present (SS) then
5288 Check_Pool_Size_Clash (U_Ent, N, SS);
5289 end if;
5290 end;
5292 -- Storage_Pool case
5294 if Id = Attribute_Storage_Pool then
5295 Analyze_And_Resolve
5296 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5298 -- In the Simple_Storage_Pool case, we allow a variable of any
5299 -- simple storage pool type, so we Resolve without imposing an
5300 -- expected type.
5302 else
5303 Analyze_And_Resolve (Expr);
5305 if not Present (Get_Rep_Pragma
5306 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5307 then
5308 Error_Msg_N
5309 ("expression must be of a simple storage pool type", Expr);
5310 end if;
5311 end if;
5313 if not Denotes_Variable (Expr) then
5314 Error_Msg_N ("storage pool must be a variable", Expr);
5315 return;
5316 end if;
5318 if Nkind (Expr) = N_Type_Conversion then
5319 T := Etype (Expression (Expr));
5320 else
5321 T := Etype (Expr);
5322 end if;
5324 -- The Stack_Bounded_Pool is used internally for implementing
5325 -- access types with a Storage_Size. Since it only work properly
5326 -- when used on one specific type, we need to check that it is not
5327 -- hijacked improperly:
5329 -- type T is access Integer;
5330 -- for T'Storage_Size use n;
5331 -- type Q is access Float;
5332 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5334 if RTE_Available (RE_Stack_Bounded_Pool)
5335 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5336 then
5337 Error_Msg_N ("non-shareable internal Pool", Expr);
5338 return;
5339 end if;
5341 -- If the argument is a name that is not an entity name, then
5342 -- we construct a renaming operation to define an entity of
5343 -- type storage pool.
5345 if not Is_Entity_Name (Expr)
5346 and then Is_Object_Reference (Expr)
5347 then
5348 Pool := Make_Temporary (Loc, 'P', Expr);
5350 declare
5351 Rnode : constant Node_Id :=
5352 Make_Object_Renaming_Declaration (Loc,
5353 Defining_Identifier => Pool,
5354 Subtype_Mark =>
5355 New_Occurrence_Of (Etype (Expr), Loc),
5356 Name => Expr);
5358 begin
5359 -- If the attribute definition clause comes from an aspect
5360 -- clause, then insert the renaming before the associated
5361 -- entity's declaration, since the attribute clause has
5362 -- not yet been appended to the declaration list.
5364 if From_Aspect_Specification (N) then
5365 Insert_Before (Parent (Entity (N)), Rnode);
5366 else
5367 Insert_Before (N, Rnode);
5368 end if;
5370 Analyze (Rnode);
5371 Set_Associated_Storage_Pool (U_Ent, Pool);
5372 end;
5374 elsif Is_Entity_Name (Expr) then
5375 Pool := Entity (Expr);
5377 -- If pool is a renamed object, get original one. This can
5378 -- happen with an explicit renaming, and within instances.
5380 while Present (Renamed_Object (Pool))
5381 and then Is_Entity_Name (Renamed_Object (Pool))
5382 loop
5383 Pool := Entity (Renamed_Object (Pool));
5384 end loop;
5386 if Present (Renamed_Object (Pool))
5387 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5388 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5389 then
5390 Pool := Entity (Expression (Renamed_Object (Pool)));
5391 end if;
5393 Set_Associated_Storage_Pool (U_Ent, Pool);
5395 elsif Nkind (Expr) = N_Type_Conversion
5396 and then Is_Entity_Name (Expression (Expr))
5397 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5398 then
5399 Pool := Entity (Expression (Expr));
5400 Set_Associated_Storage_Pool (U_Ent, Pool);
5402 else
5403 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5404 return;
5405 end if;
5406 end;
5408 ------------------
5409 -- Storage_Size --
5410 ------------------
5412 -- Storage_Size attribute definition clause
5414 when Attribute_Storage_Size => Storage_Size : declare
5415 Btype : constant Entity_Id := Base_Type (U_Ent);
5417 begin
5418 if Is_Task_Type (U_Ent) then
5420 -- Check obsolescent (but never obsolescent if from aspect)
5422 if not From_Aspect_Specification (N) then
5423 Check_Restriction (No_Obsolescent_Features, N);
5425 if Warn_On_Obsolescent_Feature then
5426 Error_Msg_N
5427 ("?j?storage size clause for task is an " &
5428 "obsolescent feature (RM J.9)", N);
5429 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5430 end if;
5431 end if;
5433 FOnly := True;
5434 end if;
5436 if not Is_Access_Type (U_Ent)
5437 and then Ekind (U_Ent) /= E_Task_Type
5438 then
5439 Error_Msg_N ("storage size cannot be given for &", Nam);
5441 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5442 Error_Msg_N
5443 ("storage size cannot be given for a derived access type",
5444 Nam);
5446 elsif Duplicate_Clause then
5447 null;
5449 else
5450 Analyze_And_Resolve (Expr, Any_Integer);
5452 if Is_Access_Type (U_Ent) then
5454 -- Check for Storage_Pool previously given
5456 declare
5457 SP : constant Node_Id :=
5458 Get_Attribute_Definition_Clause
5459 (U_Ent, Attribute_Storage_Pool);
5461 begin
5462 if Present (SP) then
5463 Check_Pool_Size_Clash (U_Ent, SP, N);
5464 end if;
5465 end;
5467 -- Special case of for x'Storage_Size use 0
5469 if Is_OK_Static_Expression (Expr)
5470 and then Expr_Value (Expr) = 0
5471 then
5472 Set_No_Pool_Assigned (Btype);
5473 end if;
5474 end if;
5476 Set_Has_Storage_Size_Clause (Btype);
5477 end if;
5478 end Storage_Size;
5480 -----------------
5481 -- Stream_Size --
5482 -----------------
5484 when Attribute_Stream_Size => Stream_Size : declare
5485 Size : constant Uint := Static_Integer (Expr);
5487 begin
5488 if Ada_Version <= Ada_95 then
5489 Check_Restriction (No_Implementation_Attributes, N);
5490 end if;
5492 if Duplicate_Clause then
5493 null;
5495 elsif Is_Elementary_Type (U_Ent) then
5496 if Size /= System_Storage_Unit
5497 and then
5498 Size /= System_Storage_Unit * 2
5499 and then
5500 Size /= System_Storage_Unit * 4
5501 and then
5502 Size /= System_Storage_Unit * 8
5503 then
5504 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5505 Error_Msg_N
5506 ("stream size for elementary type must be a"
5507 & " power of 2 and at least ^", N);
5509 elsif RM_Size (U_Ent) > Size then
5510 Error_Msg_Uint_1 := RM_Size (U_Ent);
5511 Error_Msg_N
5512 ("stream size for elementary type must be a"
5513 & " power of 2 and at least ^", N);
5514 end if;
5516 Set_Has_Stream_Size_Clause (U_Ent);
5518 else
5519 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5520 end if;
5521 end Stream_Size;
5523 ----------------
5524 -- Value_Size --
5525 ----------------
5527 -- Value_Size attribute definition clause
5529 when Attribute_Value_Size => Value_Size : declare
5530 Size : constant Uint := Static_Integer (Expr);
5531 Biased : Boolean;
5533 begin
5534 if not Is_Type (U_Ent) then
5535 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5537 elsif Duplicate_Clause then
5538 null;
5540 elsif Is_Array_Type (U_Ent)
5541 and then not Is_Constrained (U_Ent)
5542 then
5543 Error_Msg_N
5544 ("Value_Size cannot be given for unconstrained array", Nam);
5546 else
5547 if Is_Elementary_Type (U_Ent) then
5548 Check_Size (Expr, U_Ent, Size, Biased);
5549 Set_Biased (U_Ent, N, "value size clause", Biased);
5550 end if;
5552 Set_RM_Size (U_Ent, Size);
5553 end if;
5554 end Value_Size;
5556 -----------------------
5557 -- Variable_Indexing --
5558 -----------------------
5560 when Attribute_Variable_Indexing =>
5561 Check_Indexing_Functions;
5563 -----------
5564 -- Write --
5565 -----------
5567 when Attribute_Write =>
5568 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5569 Set_Has_Specified_Stream_Write (Ent);
5571 -- All other attributes cannot be set
5573 when others =>
5574 Error_Msg_N
5575 ("attribute& cannot be set with definition clause", N);
5576 end case;
5578 -- The test for the type being frozen must be performed after any
5579 -- expression the clause has been analyzed since the expression itself
5580 -- might cause freezing that makes the clause illegal.
5582 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5583 return;
5584 end if;
5585 end Analyze_Attribute_Definition_Clause;
5587 ----------------------------
5588 -- Analyze_Code_Statement --
5589 ----------------------------
5591 procedure Analyze_Code_Statement (N : Node_Id) is
5592 HSS : constant Node_Id := Parent (N);
5593 SBody : constant Node_Id := Parent (HSS);
5594 Subp : constant Entity_Id := Current_Scope;
5595 Stmt : Node_Id;
5596 Decl : Node_Id;
5597 StmtO : Node_Id;
5598 DeclO : Node_Id;
5600 begin
5601 -- Analyze and check we get right type, note that this implements the
5602 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5603 -- is the only way that Asm_Insn could possibly be visible.
5605 Analyze_And_Resolve (Expression (N));
5607 if Etype (Expression (N)) = Any_Type then
5608 return;
5609 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5610 Error_Msg_N ("incorrect type for code statement", N);
5611 return;
5612 end if;
5614 Check_Code_Statement (N);
5616 -- Make sure we appear in the handled statement sequence of a
5617 -- subprogram (RM 13.8(3)).
5619 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5620 or else Nkind (SBody) /= N_Subprogram_Body
5621 then
5622 Error_Msg_N
5623 ("code statement can only appear in body of subprogram", N);
5624 return;
5625 end if;
5627 -- Do remaining checks (RM 13.8(3)) if not already done
5629 if not Is_Machine_Code_Subprogram (Subp) then
5630 Set_Is_Machine_Code_Subprogram (Subp);
5632 -- No exception handlers allowed
5634 if Present (Exception_Handlers (HSS)) then
5635 Error_Msg_N
5636 ("exception handlers not permitted in machine code subprogram",
5637 First (Exception_Handlers (HSS)));
5638 end if;
5640 -- No declarations other than use clauses and pragmas (we allow
5641 -- certain internally generated declarations as well).
5643 Decl := First (Declarations (SBody));
5644 while Present (Decl) loop
5645 DeclO := Original_Node (Decl);
5646 if Comes_From_Source (DeclO)
5647 and not Nkind_In (DeclO, N_Pragma,
5648 N_Use_Package_Clause,
5649 N_Use_Type_Clause,
5650 N_Implicit_Label_Declaration)
5651 then
5652 Error_Msg_N
5653 ("this declaration not allowed in machine code subprogram",
5654 DeclO);
5655 end if;
5657 Next (Decl);
5658 end loop;
5660 -- No statements other than code statements, pragmas, and labels.
5661 -- Again we allow certain internally generated statements.
5663 -- In Ada 2012, qualified expressions are names, and the code
5664 -- statement is initially parsed as a procedure call.
5666 Stmt := First (Statements (HSS));
5667 while Present (Stmt) loop
5668 StmtO := Original_Node (Stmt);
5670 -- A procedure call transformed into a code statement is OK.
5672 if Ada_Version >= Ada_2012
5673 and then Nkind (StmtO) = N_Procedure_Call_Statement
5674 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5675 then
5676 null;
5678 elsif Comes_From_Source (StmtO)
5679 and then not Nkind_In (StmtO, N_Pragma,
5680 N_Label,
5681 N_Code_Statement)
5682 then
5683 Error_Msg_N
5684 ("this statement is not allowed in machine code subprogram",
5685 StmtO);
5686 end if;
5688 Next (Stmt);
5689 end loop;
5690 end if;
5691 end Analyze_Code_Statement;
5693 -----------------------------------------------
5694 -- Analyze_Enumeration_Representation_Clause --
5695 -----------------------------------------------
5697 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5698 Ident : constant Node_Id := Identifier (N);
5699 Aggr : constant Node_Id := Array_Aggregate (N);
5700 Enumtype : Entity_Id;
5701 Elit : Entity_Id;
5702 Expr : Node_Id;
5703 Assoc : Node_Id;
5704 Choice : Node_Id;
5705 Val : Uint;
5707 Err : Boolean := False;
5708 -- Set True to avoid cascade errors and crashes on incorrect source code
5710 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5711 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5712 -- Allowed range of universal integer (= allowed range of enum lit vals)
5714 Min : Uint;
5715 Max : Uint;
5716 -- Minimum and maximum values of entries
5718 Max_Node : Node_Id;
5719 -- Pointer to node for literal providing max value
5721 begin
5722 if Ignore_Rep_Clauses then
5723 Kill_Rep_Clause (N);
5724 return;
5725 end if;
5727 -- Ignore enumeration rep clauses by default in CodePeer mode,
5728 -- unless -gnatd.I is specified, as a work around for potential false
5729 -- positive messages.
5731 if CodePeer_Mode and not Debug_Flag_Dot_II then
5732 return;
5733 end if;
5735 -- First some basic error checks
5737 Find_Type (Ident);
5738 Enumtype := Entity (Ident);
5740 if Enumtype = Any_Type
5741 or else Rep_Item_Too_Early (Enumtype, N)
5742 then
5743 return;
5744 else
5745 Enumtype := Underlying_Type (Enumtype);
5746 end if;
5748 if not Is_Enumeration_Type (Enumtype) then
5749 Error_Msg_NE
5750 ("enumeration type required, found}",
5751 Ident, First_Subtype (Enumtype));
5752 return;
5753 end if;
5755 -- Ignore rep clause on generic actual type. This will already have
5756 -- been flagged on the template as an error, and this is the safest
5757 -- way to ensure we don't get a junk cascaded message in the instance.
5759 if Is_Generic_Actual_Type (Enumtype) then
5760 return;
5762 -- Type must be in current scope
5764 elsif Scope (Enumtype) /= Current_Scope then
5765 Error_Msg_N ("type must be declared in this scope", Ident);
5766 return;
5768 -- Type must be a first subtype
5770 elsif not Is_First_Subtype (Enumtype) then
5771 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5772 return;
5774 -- Ignore duplicate rep clause
5776 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5777 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5778 return;
5780 -- Don't allow rep clause for standard [wide_[wide_]]character
5782 elsif Is_Standard_Character_Type (Enumtype) then
5783 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5784 return;
5786 -- Check that the expression is a proper aggregate (no parentheses)
5788 elsif Paren_Count (Aggr) /= 0 then
5789 Error_Msg
5790 ("extra parentheses surrounding aggregate not allowed",
5791 First_Sloc (Aggr));
5792 return;
5794 -- All tests passed, so set rep clause in place
5796 else
5797 Set_Has_Enumeration_Rep_Clause (Enumtype);
5798 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5799 end if;
5801 -- Now we process the aggregate. Note that we don't use the normal
5802 -- aggregate code for this purpose, because we don't want any of the
5803 -- normal expansion activities, and a number of special semantic
5804 -- rules apply (including the component type being any integer type)
5806 Elit := First_Literal (Enumtype);
5808 -- First the positional entries if any
5810 if Present (Expressions (Aggr)) then
5811 Expr := First (Expressions (Aggr));
5812 while Present (Expr) loop
5813 if No (Elit) then
5814 Error_Msg_N ("too many entries in aggregate", Expr);
5815 return;
5816 end if;
5818 Val := Static_Integer (Expr);
5820 -- Err signals that we found some incorrect entries processing
5821 -- the list. The final checks for completeness and ordering are
5822 -- skipped in this case.
5824 if Val = No_Uint then
5825 Err := True;
5826 elsif Val < Lo or else Hi < Val then
5827 Error_Msg_N ("value outside permitted range", Expr);
5828 Err := True;
5829 end if;
5831 Set_Enumeration_Rep (Elit, Val);
5832 Set_Enumeration_Rep_Expr (Elit, Expr);
5833 Next (Expr);
5834 Next (Elit);
5835 end loop;
5836 end if;
5838 -- Now process the named entries if present
5840 if Present (Component_Associations (Aggr)) then
5841 Assoc := First (Component_Associations (Aggr));
5842 while Present (Assoc) loop
5843 Choice := First (Choices (Assoc));
5845 if Present (Next (Choice)) then
5846 Error_Msg_N
5847 ("multiple choice not allowed here", Next (Choice));
5848 Err := True;
5849 end if;
5851 if Nkind (Choice) = N_Others_Choice then
5852 Error_Msg_N ("others choice not allowed here", Choice);
5853 Err := True;
5855 elsif Nkind (Choice) = N_Range then
5857 -- ??? should allow zero/one element range here
5859 Error_Msg_N ("range not allowed here", Choice);
5860 Err := True;
5862 else
5863 Analyze_And_Resolve (Choice, Enumtype);
5865 if Error_Posted (Choice) then
5866 Err := True;
5867 end if;
5869 if not Err then
5870 if Is_Entity_Name (Choice)
5871 and then Is_Type (Entity (Choice))
5872 then
5873 Error_Msg_N ("subtype name not allowed here", Choice);
5874 Err := True;
5876 -- ??? should allow static subtype with zero/one entry
5878 elsif Etype (Choice) = Base_Type (Enumtype) then
5879 if not Is_OK_Static_Expression (Choice) then
5880 Flag_Non_Static_Expr
5881 ("non-static expression used for choice!", Choice);
5882 Err := True;
5884 else
5885 Elit := Expr_Value_E (Choice);
5887 if Present (Enumeration_Rep_Expr (Elit)) then
5888 Error_Msg_Sloc :=
5889 Sloc (Enumeration_Rep_Expr (Elit));
5890 Error_Msg_NE
5891 ("representation for& previously given#",
5892 Choice, Elit);
5893 Err := True;
5894 end if;
5896 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
5898 Expr := Expression (Assoc);
5899 Val := Static_Integer (Expr);
5901 if Val = No_Uint then
5902 Err := True;
5904 elsif Val < Lo or else Hi < Val then
5905 Error_Msg_N ("value outside permitted range", Expr);
5906 Err := True;
5907 end if;
5909 Set_Enumeration_Rep (Elit, Val);
5910 end if;
5911 end if;
5912 end if;
5913 end if;
5915 Next (Assoc);
5916 end loop;
5917 end if;
5919 -- Aggregate is fully processed. Now we check that a full set of
5920 -- representations was given, and that they are in range and in order.
5921 -- These checks are only done if no other errors occurred.
5923 if not Err then
5924 Min := No_Uint;
5925 Max := No_Uint;
5927 Elit := First_Literal (Enumtype);
5928 while Present (Elit) loop
5929 if No (Enumeration_Rep_Expr (Elit)) then
5930 Error_Msg_NE ("missing representation for&!", N, Elit);
5932 else
5933 Val := Enumeration_Rep (Elit);
5935 if Min = No_Uint then
5936 Min := Val;
5937 end if;
5939 if Val /= No_Uint then
5940 if Max /= No_Uint and then Val <= Max then
5941 Error_Msg_NE
5942 ("enumeration value for& not ordered!",
5943 Enumeration_Rep_Expr (Elit), Elit);
5944 end if;
5946 Max_Node := Enumeration_Rep_Expr (Elit);
5947 Max := Val;
5948 end if;
5950 -- If there is at least one literal whose representation is not
5951 -- equal to the Pos value, then note that this enumeration type
5952 -- has a non-standard representation.
5954 if Val /= Enumeration_Pos (Elit) then
5955 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
5956 end if;
5957 end if;
5959 Next (Elit);
5960 end loop;
5962 -- Now set proper size information
5964 declare
5965 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
5967 begin
5968 if Has_Size_Clause (Enumtype) then
5970 -- All OK, if size is OK now
5972 if RM_Size (Enumtype) >= Minsize then
5973 null;
5975 else
5976 -- Try if we can get by with biasing
5978 Minsize :=
5979 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
5981 -- Error message if even biasing does not work
5983 if RM_Size (Enumtype) < Minsize then
5984 Error_Msg_Uint_1 := RM_Size (Enumtype);
5985 Error_Msg_Uint_2 := Max;
5986 Error_Msg_N
5987 ("previously given size (^) is too small "
5988 & "for this value (^)", Max_Node);
5990 -- If biasing worked, indicate that we now have biased rep
5992 else
5993 Set_Biased
5994 (Enumtype, Size_Clause (Enumtype), "size clause");
5995 end if;
5996 end if;
5998 else
5999 Set_RM_Size (Enumtype, Minsize);
6000 Set_Enum_Esize (Enumtype);
6001 end if;
6003 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6004 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6005 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6006 end;
6007 end if;
6009 -- We repeat the too late test in case it froze itself
6011 if Rep_Item_Too_Late (Enumtype, N) then
6012 null;
6013 end if;
6014 end Analyze_Enumeration_Representation_Clause;
6016 ----------------------------
6017 -- Analyze_Free_Statement --
6018 ----------------------------
6020 procedure Analyze_Free_Statement (N : Node_Id) is
6021 begin
6022 Analyze (Expression (N));
6023 end Analyze_Free_Statement;
6025 ---------------------------
6026 -- Analyze_Freeze_Entity --
6027 ---------------------------
6029 procedure Analyze_Freeze_Entity (N : Node_Id) is
6030 begin
6031 Freeze_Entity_Checks (N);
6032 end Analyze_Freeze_Entity;
6034 -----------------------------------
6035 -- Analyze_Freeze_Generic_Entity --
6036 -----------------------------------
6038 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6039 begin
6040 Freeze_Entity_Checks (N);
6041 end Analyze_Freeze_Generic_Entity;
6043 ------------------------------------------
6044 -- Analyze_Record_Representation_Clause --
6045 ------------------------------------------
6047 -- Note: we check as much as we can here, but we can't do any checks
6048 -- based on the position values (e.g. overlap checks) until freeze time
6049 -- because especially in Ada 2005 (machine scalar mode), the processing
6050 -- for non-standard bit order can substantially change the positions.
6051 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6052 -- for the remainder of this processing.
6054 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6055 Ident : constant Node_Id := Identifier (N);
6056 Biased : Boolean;
6057 CC : Node_Id;
6058 Comp : Entity_Id;
6059 Fbit : Uint;
6060 Hbit : Uint := Uint_0;
6061 Lbit : Uint;
6062 Ocomp : Entity_Id;
6063 Posit : Uint;
6064 Rectype : Entity_Id;
6065 Recdef : Node_Id;
6067 function Is_Inherited (Comp : Entity_Id) return Boolean;
6068 -- True if Comp is an inherited component in a record extension
6070 ------------------
6071 -- Is_Inherited --
6072 ------------------
6074 function Is_Inherited (Comp : Entity_Id) return Boolean is
6075 Comp_Base : Entity_Id;
6077 begin
6078 if Ekind (Rectype) = E_Record_Subtype then
6079 Comp_Base := Original_Record_Component (Comp);
6080 else
6081 Comp_Base := Comp;
6082 end if;
6084 return Comp_Base /= Original_Record_Component (Comp_Base);
6085 end Is_Inherited;
6087 -- Local variables
6089 Is_Record_Extension : Boolean;
6090 -- True if Rectype is a record extension
6092 CR_Pragma : Node_Id := Empty;
6093 -- Points to N_Pragma node if Complete_Representation pragma present
6095 -- Start of processing for Analyze_Record_Representation_Clause
6097 begin
6098 if Ignore_Rep_Clauses then
6099 Kill_Rep_Clause (N);
6100 return;
6101 end if;
6103 Find_Type (Ident);
6104 Rectype := Entity (Ident);
6106 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6107 return;
6108 else
6109 Rectype := Underlying_Type (Rectype);
6110 end if;
6112 -- First some basic error checks
6114 if not Is_Record_Type (Rectype) then
6115 Error_Msg_NE
6116 ("record type required, found}", Ident, First_Subtype (Rectype));
6117 return;
6119 elsif Scope (Rectype) /= Current_Scope then
6120 Error_Msg_N ("type must be declared in this scope", N);
6121 return;
6123 elsif not Is_First_Subtype (Rectype) then
6124 Error_Msg_N ("cannot give record rep clause for subtype", N);
6125 return;
6127 elsif Has_Record_Rep_Clause (Rectype) then
6128 Error_Msg_N ("duplicate record rep clause ignored", N);
6129 return;
6131 elsif Rep_Item_Too_Late (Rectype, N) then
6132 return;
6133 end if;
6135 -- We know we have a first subtype, now possibly go the the anonymous
6136 -- base type to determine whether Rectype is a record extension.
6138 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6139 Is_Record_Extension :=
6140 Nkind (Recdef) = N_Derived_Type_Definition
6141 and then Present (Record_Extension_Part (Recdef));
6143 if Present (Mod_Clause (N)) then
6144 declare
6145 Loc : constant Source_Ptr := Sloc (N);
6146 M : constant Node_Id := Mod_Clause (N);
6147 P : constant List_Id := Pragmas_Before (M);
6148 AtM_Nod : Node_Id;
6150 Mod_Val : Uint;
6151 pragma Warnings (Off, Mod_Val);
6153 begin
6154 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6156 if Warn_On_Obsolescent_Feature then
6157 Error_Msg_N
6158 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6159 Error_Msg_N
6160 ("\?j?use alignment attribute definition clause instead", N);
6161 end if;
6163 if Present (P) then
6164 Analyze_List (P);
6165 end if;
6167 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6168 -- the Mod clause into an alignment clause anyway, so that the
6169 -- back-end can compute and back-annotate properly the size and
6170 -- alignment of types that may include this record.
6172 -- This seems dubious, this destroys the source tree in a manner
6173 -- not detectable by ASIS ???
6175 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6176 AtM_Nod :=
6177 Make_Attribute_Definition_Clause (Loc,
6178 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6179 Chars => Name_Alignment,
6180 Expression => Relocate_Node (Expression (M)));
6182 Set_From_At_Mod (AtM_Nod);
6183 Insert_After (N, AtM_Nod);
6184 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6185 Set_Mod_Clause (N, Empty);
6187 else
6188 -- Get the alignment value to perform error checking
6190 Mod_Val := Get_Alignment_Value (Expression (M));
6191 end if;
6192 end;
6193 end if;
6195 -- For untagged types, clear any existing component clauses for the
6196 -- type. If the type is derived, this is what allows us to override
6197 -- a rep clause for the parent. For type extensions, the representation
6198 -- of the inherited components is inherited, so we want to keep previous
6199 -- component clauses for completeness.
6201 if not Is_Tagged_Type (Rectype) then
6202 Comp := First_Component_Or_Discriminant (Rectype);
6203 while Present (Comp) loop
6204 Set_Component_Clause (Comp, Empty);
6205 Next_Component_Or_Discriminant (Comp);
6206 end loop;
6207 end if;
6209 -- All done if no component clauses
6211 CC := First (Component_Clauses (N));
6213 if No (CC) then
6214 return;
6215 end if;
6217 -- A representation like this applies to the base type
6219 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6220 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6221 Set_Has_Specified_Layout (Base_Type (Rectype));
6223 -- Process the component clauses
6225 while Present (CC) loop
6227 -- Pragma
6229 if Nkind (CC) = N_Pragma then
6230 Analyze (CC);
6232 -- The only pragma of interest is Complete_Representation
6234 if Pragma_Name (CC) = Name_Complete_Representation then
6235 CR_Pragma := CC;
6236 end if;
6238 -- Processing for real component clause
6240 else
6241 Posit := Static_Integer (Position (CC));
6242 Fbit := Static_Integer (First_Bit (CC));
6243 Lbit := Static_Integer (Last_Bit (CC));
6245 if Posit /= No_Uint
6246 and then Fbit /= No_Uint
6247 and then Lbit /= No_Uint
6248 then
6249 if Posit < 0 then
6250 Error_Msg_N
6251 ("position cannot be negative", Position (CC));
6253 elsif Fbit < 0 then
6254 Error_Msg_N
6255 ("first bit cannot be negative", First_Bit (CC));
6257 -- The Last_Bit specified in a component clause must not be
6258 -- less than the First_Bit minus one (RM-13.5.1(10)).
6260 elsif Lbit < Fbit - 1 then
6261 Error_Msg_N
6262 ("last bit cannot be less than first bit minus one",
6263 Last_Bit (CC));
6265 -- Values look OK, so find the corresponding record component
6266 -- Even though the syntax allows an attribute reference for
6267 -- implementation-defined components, GNAT does not allow the
6268 -- tag to get an explicit position.
6270 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6271 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6272 Error_Msg_N ("position of tag cannot be specified", CC);
6273 else
6274 Error_Msg_N ("illegal component name", CC);
6275 end if;
6277 else
6278 Comp := First_Entity (Rectype);
6279 while Present (Comp) loop
6280 exit when Chars (Comp) = Chars (Component_Name (CC));
6281 Next_Entity (Comp);
6282 end loop;
6284 if No (Comp) then
6286 -- Maybe component of base type that is absent from
6287 -- statically constrained first subtype.
6289 Comp := First_Entity (Base_Type (Rectype));
6290 while Present (Comp) loop
6291 exit when Chars (Comp) = Chars (Component_Name (CC));
6292 Next_Entity (Comp);
6293 end loop;
6294 end if;
6296 if No (Comp) then
6297 Error_Msg_N
6298 ("component clause is for non-existent field", CC);
6300 -- Ada 2012 (AI05-0026): Any name that denotes a
6301 -- discriminant of an object of an unchecked union type
6302 -- shall not occur within a record_representation_clause.
6304 -- The general restriction of using record rep clauses on
6305 -- Unchecked_Union types has now been lifted. Since it is
6306 -- possible to introduce a record rep clause which mentions
6307 -- the discriminant of an Unchecked_Union in non-Ada 2012
6308 -- code, this check is applied to all versions of the
6309 -- language.
6311 elsif Ekind (Comp) = E_Discriminant
6312 and then Is_Unchecked_Union (Rectype)
6313 then
6314 Error_Msg_N
6315 ("cannot reference discriminant of unchecked union",
6316 Component_Name (CC));
6318 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6319 Error_Msg_NE
6320 ("component clause not allowed for inherited "
6321 & "component&", CC, Comp);
6323 elsif Present (Component_Clause (Comp)) then
6325 -- Diagnose duplicate rep clause, or check consistency
6326 -- if this is an inherited component. In a double fault,
6327 -- there may be a duplicate inconsistent clause for an
6328 -- inherited component.
6330 if Scope (Original_Record_Component (Comp)) = Rectype
6331 or else Parent (Component_Clause (Comp)) = N
6332 then
6333 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6334 Error_Msg_N ("component clause previously given#", CC);
6336 else
6337 declare
6338 Rep1 : constant Node_Id := Component_Clause (Comp);
6339 begin
6340 if Intval (Position (Rep1)) /=
6341 Intval (Position (CC))
6342 or else Intval (First_Bit (Rep1)) /=
6343 Intval (First_Bit (CC))
6344 or else Intval (Last_Bit (Rep1)) /=
6345 Intval (Last_Bit (CC))
6346 then
6347 Error_Msg_N
6348 ("component clause inconsistent "
6349 & "with representation of ancestor", CC);
6351 elsif Warn_On_Redundant_Constructs then
6352 Error_Msg_N
6353 ("?r?redundant confirming component clause "
6354 & "for component!", CC);
6355 end if;
6356 end;
6357 end if;
6359 -- Normal case where this is the first component clause we
6360 -- have seen for this entity, so set it up properly.
6362 else
6363 -- Make reference for field in record rep clause and set
6364 -- appropriate entity field in the field identifier.
6366 Generate_Reference
6367 (Comp, Component_Name (CC), Set_Ref => False);
6368 Set_Entity (Component_Name (CC), Comp);
6370 -- Update Fbit and Lbit to the actual bit number
6372 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6373 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6375 if Has_Size_Clause (Rectype)
6376 and then RM_Size (Rectype) <= Lbit
6377 then
6378 Error_Msg_N
6379 ("bit number out of range of specified size",
6380 Last_Bit (CC));
6381 else
6382 Set_Component_Clause (Comp, CC);
6383 Set_Component_Bit_Offset (Comp, Fbit);
6384 Set_Esize (Comp, 1 + (Lbit - Fbit));
6385 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6386 Set_Normalized_Position (Comp, Fbit / SSU);
6388 if Warn_On_Overridden_Size
6389 and then Has_Size_Clause (Etype (Comp))
6390 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6391 then
6392 Error_Msg_NE
6393 ("?S?component size overrides size clause for&",
6394 Component_Name (CC), Etype (Comp));
6395 end if;
6397 -- This information is also set in the corresponding
6398 -- component of the base type, found by accessing the
6399 -- Original_Record_Component link if it is present.
6401 Ocomp := Original_Record_Component (Comp);
6403 if Hbit < Lbit then
6404 Hbit := Lbit;
6405 end if;
6407 Check_Size
6408 (Component_Name (CC),
6409 Etype (Comp),
6410 Esize (Comp),
6411 Biased);
6413 Set_Biased
6414 (Comp, First_Node (CC), "component clause", Biased);
6416 if Present (Ocomp) then
6417 Set_Component_Clause (Ocomp, CC);
6418 Set_Component_Bit_Offset (Ocomp, Fbit);
6419 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6420 Set_Normalized_Position (Ocomp, Fbit / SSU);
6421 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6423 Set_Normalized_Position_Max
6424 (Ocomp, Normalized_Position (Ocomp));
6426 -- Note: we don't use Set_Biased here, because we
6427 -- already gave a warning above if needed, and we
6428 -- would get a duplicate for the same name here.
6430 Set_Has_Biased_Representation
6431 (Ocomp, Has_Biased_Representation (Comp));
6432 end if;
6434 if Esize (Comp) < 0 then
6435 Error_Msg_N ("component size is negative", CC);
6436 end if;
6437 end if;
6438 end if;
6439 end if;
6440 end if;
6441 end if;
6443 Next (CC);
6444 end loop;
6446 -- Check missing components if Complete_Representation pragma appeared
6448 if Present (CR_Pragma) then
6449 Comp := First_Component_Or_Discriminant (Rectype);
6450 while Present (Comp) loop
6451 if No (Component_Clause (Comp)) then
6452 Error_Msg_NE
6453 ("missing component clause for &", CR_Pragma, Comp);
6454 end if;
6456 Next_Component_Or_Discriminant (Comp);
6457 end loop;
6459 -- Give missing components warning if required
6461 elsif Warn_On_Unrepped_Components then
6462 declare
6463 Num_Repped_Components : Nat := 0;
6464 Num_Unrepped_Components : Nat := 0;
6466 begin
6467 -- First count number of repped and unrepped components
6469 Comp := First_Component_Or_Discriminant (Rectype);
6470 while Present (Comp) loop
6471 if Present (Component_Clause (Comp)) then
6472 Num_Repped_Components := Num_Repped_Components + 1;
6473 else
6474 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6475 end if;
6477 Next_Component_Or_Discriminant (Comp);
6478 end loop;
6480 -- We are only interested in the case where there is at least one
6481 -- unrepped component, and at least half the components have rep
6482 -- clauses. We figure that if less than half have them, then the
6483 -- partial rep clause is really intentional. If the component
6484 -- type has no underlying type set at this point (as for a generic
6485 -- formal type), we don't know enough to give a warning on the
6486 -- component.
6488 if Num_Unrepped_Components > 0
6489 and then Num_Unrepped_Components < Num_Repped_Components
6490 then
6491 Comp := First_Component_Or_Discriminant (Rectype);
6492 while Present (Comp) loop
6493 if No (Component_Clause (Comp))
6494 and then Comes_From_Source (Comp)
6495 and then Present (Underlying_Type (Etype (Comp)))
6496 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6497 or else Size_Known_At_Compile_Time
6498 (Underlying_Type (Etype (Comp))))
6499 and then not Has_Warnings_Off (Rectype)
6500 then
6501 Error_Msg_Sloc := Sloc (Comp);
6502 Error_Msg_NE
6503 ("?C?no component clause given for & declared #",
6504 N, Comp);
6505 end if;
6507 Next_Component_Or_Discriminant (Comp);
6508 end loop;
6509 end if;
6510 end;
6511 end if;
6512 end Analyze_Record_Representation_Clause;
6514 -------------------------------------
6515 -- Build_Discrete_Static_Predicate --
6516 -------------------------------------
6518 procedure Build_Discrete_Static_Predicate
6519 (Typ : Entity_Id;
6520 Expr : Node_Id;
6521 Nam : Name_Id)
6523 Loc : constant Source_Ptr := Sloc (Expr);
6525 Non_Static : exception;
6526 -- Raised if something non-static is found
6528 Btyp : constant Entity_Id := Base_Type (Typ);
6530 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6531 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6532 -- Low bound and high bound value of base type of Typ
6534 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6535 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6536 -- Low bound and high bound values of static subtype Typ
6538 type REnt is record
6539 Lo, Hi : Uint;
6540 end record;
6541 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6542 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6543 -- value.
6545 type RList is array (Nat range <>) of REnt;
6546 -- A list of ranges. The ranges are sorted in increasing order, and are
6547 -- disjoint (there is a gap of at least one value between each range in
6548 -- the table). A value is in the set of ranges in Rlist if it lies
6549 -- within one of these ranges.
6551 False_Range : constant RList :=
6552 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6553 -- An empty set of ranges represents a range list that can never be
6554 -- satisfied, since there are no ranges in which the value could lie,
6555 -- so it does not lie in any of them. False_Range is a canonical value
6556 -- for this empty set, but general processing should test for an Rlist
6557 -- with length zero (see Is_False predicate), since other null ranges
6558 -- may appear which must be treated as False.
6560 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6561 -- Range representing True, value must be in the base range
6563 function "and" (Left : RList; Right : RList) return RList;
6564 -- And's together two range lists, returning a range list. This is a set
6565 -- intersection operation.
6567 function "or" (Left : RList; Right : RList) return RList;
6568 -- Or's together two range lists, returning a range list. This is a set
6569 -- union operation.
6571 function "not" (Right : RList) return RList;
6572 -- Returns complement of a given range list, i.e. a range list
6573 -- representing all the values in TLo .. THi that are not in the input
6574 -- operand Right.
6576 function Build_Val (V : Uint) return Node_Id;
6577 -- Return an analyzed N_Identifier node referencing this value, suitable
6578 -- for use as an entry in the Static_Discrte_Predicate list. This node
6579 -- is typed with the base type.
6581 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6582 -- Return an analyzed N_Range node referencing this range, suitable for
6583 -- use as an entry in the Static_Discrete_Predicate list. This node is
6584 -- typed with the base type.
6586 function Get_RList (Exp : Node_Id) return RList;
6587 -- This is a recursive routine that converts the given expression into a
6588 -- list of ranges, suitable for use in building the static predicate.
6590 function Is_False (R : RList) return Boolean;
6591 pragma Inline (Is_False);
6592 -- Returns True if the given range list is empty, and thus represents a
6593 -- False list of ranges that can never be satisfied.
6595 function Is_True (R : RList) return Boolean;
6596 -- Returns True if R trivially represents the True predicate by having a
6597 -- single range from BLo to BHi.
6599 function Is_Type_Ref (N : Node_Id) return Boolean;
6600 pragma Inline (Is_Type_Ref);
6601 -- Returns if True if N is a reference to the type for the predicate in
6602 -- the expression (i.e. if it is an identifier whose Chars field matches
6603 -- the Nam given in the call). N must not be parenthesized, if the type
6604 -- name appears in parens, this routine will return False.
6606 function Lo_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 low bound of the range.
6611 function Hi_Val (N : Node_Id) return Uint;
6612 -- Given an entry from a Static_Discrete_Predicate list that is either
6613 -- a static expression or static range, gets either the expression value
6614 -- or the high bound of the range.
6616 function Membership_Entry (N : Node_Id) return RList;
6617 -- Given a single membership entry (range, value, or subtype), returns
6618 -- the corresponding range list. Raises Static_Error if not static.
6620 function Membership_Entries (N : Node_Id) return RList;
6621 -- Given an element on an alternatives list of a membership operation,
6622 -- returns the range list corresponding to this entry and all following
6623 -- entries (i.e. returns the "or" of this list of values).
6625 function Stat_Pred (Typ : Entity_Id) return RList;
6626 -- Given a type, if it has a static predicate, then return the predicate
6627 -- as a range list, otherwise raise Non_Static.
6629 -----------
6630 -- "and" --
6631 -----------
6633 function "and" (Left : RList; Right : RList) return RList is
6634 FEnt : REnt;
6635 -- First range of result
6637 SLeft : Nat := Left'First;
6638 -- Start of rest of left entries
6640 SRight : Nat := Right'First;
6641 -- Start of rest of right entries
6643 begin
6644 -- If either range is True, return the other
6646 if Is_True (Left) then
6647 return Right;
6648 elsif Is_True (Right) then
6649 return Left;
6650 end if;
6652 -- If either range is False, return False
6654 if Is_False (Left) or else Is_False (Right) then
6655 return False_Range;
6656 end if;
6658 -- Loop to remove entries at start that are disjoint, and thus just
6659 -- get discarded from the result entirely.
6661 loop
6662 -- If no operands left in either operand, result is false
6664 if SLeft > Left'Last or else SRight > Right'Last then
6665 return False_Range;
6667 -- Discard first left operand entry if disjoint with right
6669 elsif Left (SLeft).Hi < Right (SRight).Lo then
6670 SLeft := SLeft + 1;
6672 -- Discard first right operand entry if disjoint with left
6674 elsif Right (SRight).Hi < Left (SLeft).Lo then
6675 SRight := SRight + 1;
6677 -- Otherwise we have an overlapping entry
6679 else
6680 exit;
6681 end if;
6682 end loop;
6684 -- Now we have two non-null operands, and first entries overlap. The
6685 -- first entry in the result will be the overlapping part of these
6686 -- two entries.
6688 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6689 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6691 -- Now we can remove the entry that ended at a lower value, since its
6692 -- contribution is entirely contained in Fent.
6694 if Left (SLeft).Hi <= Right (SRight).Hi then
6695 SLeft := SLeft + 1;
6696 else
6697 SRight := SRight + 1;
6698 end if;
6700 -- Compute result by concatenating this first entry with the "and" of
6701 -- the remaining parts of the left and right operands. Note that if
6702 -- either of these is empty, "and" will yield empty, so that we will
6703 -- end up with just Fent, which is what we want in that case.
6705 return
6706 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6707 end "and";
6709 -----------
6710 -- "not" --
6711 -----------
6713 function "not" (Right : RList) return RList is
6714 begin
6715 -- Return True if False range
6717 if Is_False (Right) then
6718 return True_Range;
6719 end if;
6721 -- Return False if True range
6723 if Is_True (Right) then
6724 return False_Range;
6725 end if;
6727 -- Here if not trivial case
6729 declare
6730 Result : RList (1 .. Right'Length + 1);
6731 -- May need one more entry for gap at beginning and end
6733 Count : Nat := 0;
6734 -- Number of entries stored in Result
6736 begin
6737 -- Gap at start
6739 if Right (Right'First).Lo > TLo then
6740 Count := Count + 1;
6741 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6742 end if;
6744 -- Gaps between ranges
6746 for J in Right'First .. Right'Last - 1 loop
6747 Count := Count + 1;
6748 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6749 end loop;
6751 -- Gap at end
6753 if Right (Right'Last).Hi < THi then
6754 Count := Count + 1;
6755 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6756 end if;
6758 return Result (1 .. Count);
6759 end;
6760 end "not";
6762 ----------
6763 -- "or" --
6764 ----------
6766 function "or" (Left : RList; Right : RList) return RList is
6767 FEnt : REnt;
6768 -- First range of result
6770 SLeft : Nat := Left'First;
6771 -- Start of rest of left entries
6773 SRight : Nat := Right'First;
6774 -- Start of rest of right entries
6776 begin
6777 -- If either range is True, return True
6779 if Is_True (Left) or else Is_True (Right) then
6780 return True_Range;
6781 end if;
6783 -- If either range is False (empty), return the other
6785 if Is_False (Left) then
6786 return Right;
6787 elsif Is_False (Right) then
6788 return Left;
6789 end if;
6791 -- Initialize result first entry from left or right operand depending
6792 -- on which starts with the lower range.
6794 if Left (SLeft).Lo < Right (SRight).Lo then
6795 FEnt := Left (SLeft);
6796 SLeft := SLeft + 1;
6797 else
6798 FEnt := Right (SRight);
6799 SRight := SRight + 1;
6800 end if;
6802 -- This loop eats ranges from left and right operands that are
6803 -- contiguous with the first range we are gathering.
6805 loop
6806 -- Eat first entry in left operand if contiguous or overlapped by
6807 -- gathered first operand of result.
6809 if SLeft <= Left'Last
6810 and then Left (SLeft).Lo <= FEnt.Hi + 1
6811 then
6812 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6813 SLeft := SLeft + 1;
6815 -- Eat first entry in right operand if contiguous or overlapped by
6816 -- gathered right operand of result.
6818 elsif SRight <= Right'Last
6819 and then Right (SRight).Lo <= FEnt.Hi + 1
6820 then
6821 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6822 SRight := SRight + 1;
6824 -- All done if no more entries to eat
6826 else
6827 exit;
6828 end if;
6829 end loop;
6831 -- Obtain result as the first entry we just computed, concatenated
6832 -- to the "or" of the remaining results (if one operand is empty,
6833 -- this will just concatenate with the other
6835 return
6836 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6837 end "or";
6839 -----------------
6840 -- Build_Range --
6841 -----------------
6843 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6844 Result : Node_Id;
6845 begin
6846 Result :=
6847 Make_Range (Loc,
6848 Low_Bound => Build_Val (Lo),
6849 High_Bound => Build_Val (Hi));
6850 Set_Etype (Result, Btyp);
6851 Set_Analyzed (Result);
6852 return Result;
6853 end Build_Range;
6855 ---------------
6856 -- Build_Val --
6857 ---------------
6859 function Build_Val (V : Uint) return Node_Id is
6860 Result : Node_Id;
6862 begin
6863 if Is_Enumeration_Type (Typ) then
6864 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6865 else
6866 Result := Make_Integer_Literal (Loc, V);
6867 end if;
6869 Set_Etype (Result, Btyp);
6870 Set_Is_Static_Expression (Result);
6871 Set_Analyzed (Result);
6872 return Result;
6873 end Build_Val;
6875 ---------------
6876 -- Get_RList --
6877 ---------------
6879 function Get_RList (Exp : Node_Id) return RList is
6880 Op : Node_Kind;
6881 Val : Uint;
6883 begin
6884 -- Static expression can only be true or false
6886 if Is_OK_Static_Expression (Exp) then
6887 if Expr_Value (Exp) = 0 then
6888 return False_Range;
6889 else
6890 return True_Range;
6891 end if;
6892 end if;
6894 -- Otherwise test node type
6896 Op := Nkind (Exp);
6898 case Op is
6900 -- And
6902 when N_Op_And | N_And_Then =>
6903 return Get_RList (Left_Opnd (Exp))
6905 Get_RList (Right_Opnd (Exp));
6907 -- Or
6909 when N_Op_Or | N_Or_Else =>
6910 return Get_RList (Left_Opnd (Exp))
6912 Get_RList (Right_Opnd (Exp));
6914 -- Not
6916 when N_Op_Not =>
6917 return not Get_RList (Right_Opnd (Exp));
6919 -- Comparisons of type with static value
6921 when N_Op_Compare =>
6923 -- Type is left operand
6925 if Is_Type_Ref (Left_Opnd (Exp))
6926 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6927 then
6928 Val := Expr_Value (Right_Opnd (Exp));
6930 -- Typ is right operand
6932 elsif Is_Type_Ref (Right_Opnd (Exp))
6933 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6934 then
6935 Val := Expr_Value (Left_Opnd (Exp));
6937 -- Invert sense of comparison
6939 case Op is
6940 when N_Op_Gt => Op := N_Op_Lt;
6941 when N_Op_Lt => Op := N_Op_Gt;
6942 when N_Op_Ge => Op := N_Op_Le;
6943 when N_Op_Le => Op := N_Op_Ge;
6944 when others => null;
6945 end case;
6947 -- Other cases are non-static
6949 else
6950 raise Non_Static;
6951 end if;
6953 -- Construct range according to comparison operation
6955 case Op is
6956 when N_Op_Eq =>
6957 return RList'(1 => REnt'(Val, Val));
6959 when N_Op_Ge =>
6960 return RList'(1 => REnt'(Val, BHi));
6962 when N_Op_Gt =>
6963 return RList'(1 => REnt'(Val + 1, BHi));
6965 when N_Op_Le =>
6966 return RList'(1 => REnt'(BLo, Val));
6968 when N_Op_Lt =>
6969 return RList'(1 => REnt'(BLo, Val - 1));
6971 when N_Op_Ne =>
6972 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
6974 when others =>
6975 raise Program_Error;
6976 end case;
6978 -- Membership (IN)
6980 when N_In =>
6981 if not Is_Type_Ref (Left_Opnd (Exp)) then
6982 raise Non_Static;
6983 end if;
6985 if Present (Right_Opnd (Exp)) then
6986 return Membership_Entry (Right_Opnd (Exp));
6987 else
6988 return Membership_Entries (First (Alternatives (Exp)));
6989 end if;
6991 -- Negative membership (NOT IN)
6993 when N_Not_In =>
6994 if not Is_Type_Ref (Left_Opnd (Exp)) then
6995 raise Non_Static;
6996 end if;
6998 if Present (Right_Opnd (Exp)) then
6999 return not Membership_Entry (Right_Opnd (Exp));
7000 else
7001 return not Membership_Entries (First (Alternatives (Exp)));
7002 end if;
7004 -- Function call, may be call to static predicate
7006 when N_Function_Call =>
7007 if Is_Entity_Name (Name (Exp)) then
7008 declare
7009 Ent : constant Entity_Id := Entity (Name (Exp));
7010 begin
7011 if Is_Predicate_Function (Ent)
7012 or else
7013 Is_Predicate_Function_M (Ent)
7014 then
7015 return Stat_Pred (Etype (First_Formal (Ent)));
7016 end if;
7017 end;
7018 end if;
7020 -- Other function call cases are non-static
7022 raise Non_Static;
7024 -- Qualified expression, dig out the expression
7026 when N_Qualified_Expression =>
7027 return Get_RList (Expression (Exp));
7029 when N_Case_Expression =>
7030 declare
7031 Alt : Node_Id;
7032 Choices : List_Id;
7033 Dep : Node_Id;
7035 begin
7036 if not Is_Entity_Name (Expression (Expr))
7037 or else Etype (Expression (Expr)) /= Typ
7038 then
7039 Error_Msg_N
7040 ("expression must denaote subtype", Expression (Expr));
7041 return False_Range;
7042 end if;
7044 -- Collect discrete choices in all True alternatives
7046 Choices := New_List;
7047 Alt := First (Alternatives (Exp));
7048 while Present (Alt) loop
7049 Dep := Expression (Alt);
7051 if not Is_OK_Static_Expression (Dep) then
7052 raise Non_Static;
7054 elsif Is_True (Expr_Value (Dep)) then
7055 Append_List_To (Choices,
7056 New_Copy_List (Discrete_Choices (Alt)));
7057 end if;
7059 Next (Alt);
7060 end loop;
7062 return Membership_Entries (First (Choices));
7063 end;
7065 -- Expression with actions: if no actions, dig out expression
7067 when N_Expression_With_Actions =>
7068 if Is_Empty_List (Actions (Exp)) then
7069 return Get_RList (Expression (Exp));
7070 else
7071 raise Non_Static;
7072 end if;
7074 -- Xor operator
7076 when N_Op_Xor =>
7077 return (Get_RList (Left_Opnd (Exp))
7078 and not Get_RList (Right_Opnd (Exp)))
7079 or (Get_RList (Right_Opnd (Exp))
7080 and not Get_RList (Left_Opnd (Exp)));
7082 -- Any other node type is non-static
7084 when others =>
7085 raise Non_Static;
7086 end case;
7087 end Get_RList;
7089 ------------
7090 -- Hi_Val --
7091 ------------
7093 function Hi_Val (N : Node_Id) return Uint is
7094 begin
7095 if Is_OK_Static_Expression (N) then
7096 return Expr_Value (N);
7097 else
7098 pragma Assert (Nkind (N) = N_Range);
7099 return Expr_Value (High_Bound (N));
7100 end if;
7101 end Hi_Val;
7103 --------------
7104 -- Is_False --
7105 --------------
7107 function Is_False (R : RList) return Boolean is
7108 begin
7109 return R'Length = 0;
7110 end Is_False;
7112 -------------
7113 -- Is_True --
7114 -------------
7116 function Is_True (R : RList) return Boolean is
7117 begin
7118 return R'Length = 1
7119 and then R (R'First).Lo = BLo
7120 and then R (R'First).Hi = BHi;
7121 end Is_True;
7123 -----------------
7124 -- Is_Type_Ref --
7125 -----------------
7127 function Is_Type_Ref (N : Node_Id) return Boolean is
7128 begin
7129 return Nkind (N) = N_Identifier
7130 and then Chars (N) = Nam
7131 and then Paren_Count (N) = 0;
7132 end Is_Type_Ref;
7134 ------------
7135 -- Lo_Val --
7136 ------------
7138 function Lo_Val (N : Node_Id) return Uint is
7139 begin
7140 if Is_OK_Static_Expression (N) then
7141 return Expr_Value (N);
7142 else
7143 pragma Assert (Nkind (N) = N_Range);
7144 return Expr_Value (Low_Bound (N));
7145 end if;
7146 end Lo_Val;
7148 ------------------------
7149 -- Membership_Entries --
7150 ------------------------
7152 function Membership_Entries (N : Node_Id) return RList is
7153 begin
7154 if No (Next (N)) then
7155 return Membership_Entry (N);
7156 else
7157 return Membership_Entry (N) or Membership_Entries (Next (N));
7158 end if;
7159 end Membership_Entries;
7161 ----------------------
7162 -- Membership_Entry --
7163 ----------------------
7165 function Membership_Entry (N : Node_Id) return RList is
7166 Val : Uint;
7167 SLo : Uint;
7168 SHi : Uint;
7170 begin
7171 -- Range case
7173 if Nkind (N) = N_Range then
7174 if not Is_OK_Static_Expression (Low_Bound (N))
7175 or else
7176 not Is_OK_Static_Expression (High_Bound (N))
7177 then
7178 raise Non_Static;
7179 else
7180 SLo := Expr_Value (Low_Bound (N));
7181 SHi := Expr_Value (High_Bound (N));
7182 return RList'(1 => REnt'(SLo, SHi));
7183 end if;
7185 -- Static expression case
7187 elsif Is_OK_Static_Expression (N) then
7188 Val := Expr_Value (N);
7189 return RList'(1 => REnt'(Val, Val));
7191 -- Identifier (other than static expression) case
7193 else pragma Assert (Nkind (N) = N_Identifier);
7195 -- Type case
7197 if Is_Type (Entity (N)) then
7199 -- If type has predicates, process them
7201 if Has_Predicates (Entity (N)) then
7202 return Stat_Pred (Entity (N));
7204 -- For static subtype without predicates, get range
7206 elsif Is_OK_Static_Subtype (Entity (N)) then
7207 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7208 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7209 return RList'(1 => REnt'(SLo, SHi));
7211 -- Any other type makes us non-static
7213 else
7214 raise Non_Static;
7215 end if;
7217 -- Any other kind of identifier in predicate (e.g. a non-static
7218 -- expression value) means this is not a static predicate.
7220 else
7221 raise Non_Static;
7222 end if;
7223 end if;
7224 end Membership_Entry;
7226 ---------------
7227 -- Stat_Pred --
7228 ---------------
7230 function Stat_Pred (Typ : Entity_Id) return RList is
7231 begin
7232 -- Not static if type does not have static predicates
7234 if not Has_Static_Predicate (Typ) then
7235 raise Non_Static;
7236 end if;
7238 -- Otherwise we convert the predicate list to a range list
7240 declare
7241 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7242 Result : RList (1 .. List_Length (Spred));
7243 P : Node_Id;
7245 begin
7246 P := First (Static_Discrete_Predicate (Typ));
7247 for J in Result'Range loop
7248 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7249 Next (P);
7250 end loop;
7252 return Result;
7253 end;
7254 end Stat_Pred;
7256 -- Start of processing for Build_Discrete_Static_Predicate
7258 begin
7259 -- Analyze the expression to see if it is a static predicate
7261 declare
7262 Ranges : constant RList := Get_RList (Expr);
7263 -- Range list from expression if it is static
7265 Plist : List_Id;
7267 begin
7268 -- Convert range list into a form for the static predicate. In the
7269 -- Ranges array, we just have raw ranges, these must be converted
7270 -- to properly typed and analyzed static expressions or range nodes.
7272 -- Note: here we limit ranges to the ranges of the subtype, so that
7273 -- a predicate is always false for values outside the subtype. That
7274 -- seems fine, such values are invalid anyway, and considering them
7275 -- to fail the predicate seems allowed and friendly, and furthermore
7276 -- simplifies processing for case statements and loops.
7278 Plist := New_List;
7280 for J in Ranges'Range loop
7281 declare
7282 Lo : Uint := Ranges (J).Lo;
7283 Hi : Uint := Ranges (J).Hi;
7285 begin
7286 -- Ignore completely out of range entry
7288 if Hi < TLo or else Lo > THi then
7289 null;
7291 -- Otherwise process entry
7293 else
7294 -- Adjust out of range value to subtype range
7296 if Lo < TLo then
7297 Lo := TLo;
7298 end if;
7300 if Hi > THi then
7301 Hi := THi;
7302 end if;
7304 -- Convert range into required form
7306 Append_To (Plist, Build_Range (Lo, Hi));
7307 end if;
7308 end;
7309 end loop;
7311 -- Processing was successful and all entries were static, so now we
7312 -- can store the result as the predicate list.
7314 Set_Static_Discrete_Predicate (Typ, Plist);
7316 -- The processing for static predicates put the expression into
7317 -- canonical form as a series of ranges. It also eliminated
7318 -- duplicates and collapsed and combined ranges. We might as well
7319 -- replace the alternatives list of the right operand of the
7320 -- membership test with the static predicate list, which will
7321 -- usually be more efficient.
7323 declare
7324 New_Alts : constant List_Id := New_List;
7325 Old_Node : Node_Id;
7326 New_Node : Node_Id;
7328 begin
7329 Old_Node := First (Plist);
7330 while Present (Old_Node) loop
7331 New_Node := New_Copy (Old_Node);
7333 if Nkind (New_Node) = N_Range then
7334 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7335 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7336 end if;
7338 Append_To (New_Alts, New_Node);
7339 Next (Old_Node);
7340 end loop;
7342 -- If empty list, replace by False
7344 if Is_Empty_List (New_Alts) then
7345 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7347 -- Else replace by set membership test
7349 else
7350 Rewrite (Expr,
7351 Make_In (Loc,
7352 Left_Opnd => Make_Identifier (Loc, Nam),
7353 Right_Opnd => Empty,
7354 Alternatives => New_Alts));
7356 -- Resolve new expression in function context
7358 Install_Formals (Predicate_Function (Typ));
7359 Push_Scope (Predicate_Function (Typ));
7360 Analyze_And_Resolve (Expr, Standard_Boolean);
7361 Pop_Scope;
7362 end if;
7363 end;
7364 end;
7366 -- If non-static, return doing nothing
7368 exception
7369 when Non_Static =>
7370 return;
7371 end Build_Discrete_Static_Predicate;
7373 -------------------------------------------
7374 -- Build_Invariant_Procedure_Declaration --
7375 -------------------------------------------
7377 function Build_Invariant_Procedure_Declaration
7378 (Typ : Entity_Id) return Node_Id
7380 Loc : constant Source_Ptr := Sloc (Typ);
7381 Object_Entity : constant Entity_Id :=
7382 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7383 Spec : Node_Id;
7384 SId : Entity_Id;
7386 begin
7387 Set_Etype (Object_Entity, Typ);
7389 -- Check for duplicate definiations.
7391 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7392 return Empty;
7393 end if;
7395 SId :=
7396 Make_Defining_Identifier (Loc,
7397 Chars => New_External_Name (Chars (Typ), "Invariant"));
7398 Set_Has_Invariants (Typ);
7399 Set_Ekind (SId, E_Procedure);
7400 Set_Is_Invariant_Procedure (SId);
7401 Set_Invariant_Procedure (Typ, SId);
7403 Spec :=
7404 Make_Procedure_Specification (Loc,
7405 Defining_Unit_Name => SId,
7406 Parameter_Specifications => New_List (
7407 Make_Parameter_Specification (Loc,
7408 Defining_Identifier => Object_Entity,
7409 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7411 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7412 end Build_Invariant_Procedure_Declaration;
7414 -------------------------------
7415 -- Build_Invariant_Procedure --
7416 -------------------------------
7418 -- The procedure that is constructed here has the form
7420 -- procedure typInvariant (Ixxx : typ) is
7421 -- begin
7422 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7423 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7424 -- ...
7425 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7426 -- ...
7427 -- end typInvariant;
7429 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7430 Loc : constant Source_Ptr := Sloc (Typ);
7431 Stmts : List_Id;
7432 Spec : Node_Id;
7433 SId : Entity_Id;
7434 PDecl : Node_Id;
7435 PBody : Node_Id;
7437 Nam : Name_Id;
7438 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7439 -- if we come from a Type_Invariant aspect, we make sure to build the
7440 -- Check pragma with the right name, so that Check_Policy works right.
7442 Visible_Decls : constant List_Id := Visible_Declarations (N);
7443 Private_Decls : constant List_Id := Private_Declarations (N);
7445 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7446 -- Appends statements to Stmts for any invariants in the rep item chain
7447 -- of the given type. If Inherit is False, then we only process entries
7448 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7449 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7450 -- "inherited" to the exception message and generating an informational
7451 -- message about the inheritance of an invariant.
7453 Object_Name : Name_Id;
7454 -- Name for argument of invariant procedure
7456 Object_Entity : Node_Id;
7457 -- The entity of the formal for the procedure
7459 --------------------
7460 -- Add_Invariants --
7461 --------------------
7463 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7464 Ritem : Node_Id;
7465 Arg1 : Node_Id;
7466 Arg2 : Node_Id;
7467 Arg3 : Node_Id;
7468 Exp : Node_Id;
7469 Loc : Source_Ptr;
7470 Assoc : List_Id;
7471 Str : String_Id;
7473 procedure Replace_Type_Reference (N : Node_Id);
7474 -- Replace a single occurrence N of the subtype name with a reference
7475 -- to the formal of the predicate function. N can be an identifier
7476 -- referencing the subtype, or a selected component, representing an
7477 -- appropriately qualified occurrence of the subtype name.
7479 procedure Replace_Type_References is
7480 new Replace_Type_References_Generic (Replace_Type_Reference);
7481 -- Traverse an expression replacing all occurrences of the subtype
7482 -- name with appropriate references to the object that is the formal
7483 -- parameter of the predicate function. Note that we must ensure
7484 -- that the type and entity information is properly set in the
7485 -- replacement node, since we will do a Preanalyze call of this
7486 -- expression without proper visibility of the procedure argument.
7488 ----------------------------
7489 -- Replace_Type_Reference --
7490 ----------------------------
7492 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7493 -- regarding handling of Sloc and Comes_From_Source.
7495 procedure Replace_Type_Reference (N : Node_Id) is
7496 begin
7498 -- Add semantic information to node to be rewritten, for ASIS
7499 -- navigation needs.
7501 if Nkind (N) = N_Identifier then
7502 Set_Entity (N, T);
7503 Set_Etype (N, T);
7505 elsif Nkind (N) = N_Selected_Component then
7506 Analyze (Prefix (N));
7507 Set_Entity (Selector_Name (N), T);
7508 Set_Etype (Selector_Name (N), T);
7509 end if;
7511 -- Invariant'Class, replace with T'Class (obj)
7513 if Class_Present (Ritem) then
7514 Rewrite (N,
7515 Make_Type_Conversion (Sloc (N),
7516 Subtype_Mark =>
7517 Make_Attribute_Reference (Sloc (N),
7518 Prefix => New_Occurrence_Of (T, Sloc (N)),
7519 Attribute_Name => Name_Class),
7520 Expression => Make_Identifier (Sloc (N), Object_Name)));
7522 Set_Entity (Expression (N), Object_Entity);
7523 Set_Etype (Expression (N), Typ);
7525 -- Invariant, replace with obj
7527 else
7528 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7529 Set_Entity (N, Object_Entity);
7530 Set_Etype (N, Typ);
7531 end if;
7533 Set_Comes_From_Source (N, True);
7534 end Replace_Type_Reference;
7536 -- Start of processing for Add_Invariants
7538 begin
7539 Ritem := First_Rep_Item (T);
7540 while Present (Ritem) loop
7541 if Nkind (Ritem) = N_Pragma
7542 and then Pragma_Name (Ritem) = Name_Invariant
7543 then
7544 Arg1 := First (Pragma_Argument_Associations (Ritem));
7545 Arg2 := Next (Arg1);
7546 Arg3 := Next (Arg2);
7548 Arg1 := Get_Pragma_Arg (Arg1);
7549 Arg2 := Get_Pragma_Arg (Arg2);
7551 -- For Inherit case, ignore Invariant, process only Class case
7553 if Inherit then
7554 if not Class_Present (Ritem) then
7555 goto Continue;
7556 end if;
7558 -- For Inherit false, process only item for right type
7560 else
7561 if Entity (Arg1) /= Typ then
7562 goto Continue;
7563 end if;
7564 end if;
7566 if No (Stmts) then
7567 Stmts := Empty_List;
7568 end if;
7570 Exp := New_Copy_Tree (Arg2);
7572 -- Preserve sloc of original pragma Invariant
7574 Loc := Sloc (Ritem);
7576 -- We need to replace any occurrences of the name of the type
7577 -- with references to the object, converted to type'Class in
7578 -- the case of Invariant'Class aspects.
7580 Replace_Type_References (Exp, T);
7582 -- If this invariant comes from an aspect, find the aspect
7583 -- specification, and replace the saved expression because
7584 -- we need the subtype references replaced for the calls to
7585 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7586 -- and Check_Aspect_At_End_Of_Declarations.
7588 if From_Aspect_Specification (Ritem) then
7589 declare
7590 Aitem : Node_Id;
7592 begin
7593 -- Loop to find corresponding aspect, note that this
7594 -- must be present given the pragma is marked delayed.
7596 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7597 -- this loop does nothing. Furthermore, why isn't this
7598 -- simply Corresponding_Aspect ???
7600 Aitem := Next_Rep_Item (Ritem);
7601 while Present (Aitem) loop
7602 if Nkind (Aitem) = N_Aspect_Specification
7603 and then Aspect_Rep_Item (Aitem) = Ritem
7604 then
7605 Set_Entity
7606 (Identifier (Aitem), New_Copy_Tree (Exp));
7607 exit;
7608 end if;
7610 Aitem := Next_Rep_Item (Aitem);
7611 end loop;
7612 end;
7613 end if;
7615 -- Now we need to preanalyze the expression to properly capture
7616 -- the visibility in the visible part. The expression will not
7617 -- be analyzed for real until the body is analyzed, but that is
7618 -- at the end of the private part and has the wrong visibility.
7620 Set_Parent (Exp, N);
7621 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
7623 -- In ASIS mode, even if assertions are not enabled, we must
7624 -- analyze the original expression in the aspect specification
7625 -- because it is part of the original tree.
7627 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7628 declare
7629 Inv : constant Node_Id :=
7630 Expression (Corresponding_Aspect (Ritem));
7631 begin
7632 Replace_Type_References (Inv, T);
7633 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7634 end;
7635 end if;
7637 -- Get name to be used for Check pragma
7639 if not From_Aspect_Specification (Ritem) then
7640 Nam := Name_Invariant;
7641 else
7642 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7643 end if;
7645 -- Build first two arguments for Check pragma
7647 Assoc :=
7648 New_List (
7649 Make_Pragma_Argument_Association (Loc,
7650 Expression => Make_Identifier (Loc, Chars => Nam)),
7651 Make_Pragma_Argument_Association (Loc,
7652 Expression => Exp));
7654 -- Add message if present in Invariant pragma
7656 if Present (Arg3) then
7657 Str := Strval (Get_Pragma_Arg (Arg3));
7659 -- If inherited case, and message starts "failed invariant",
7660 -- change it to be "failed inherited invariant".
7662 if Inherit then
7663 String_To_Name_Buffer (Str);
7665 if Name_Buffer (1 .. 16) = "failed invariant" then
7666 Insert_Str_In_Name_Buffer ("inherited ", 8);
7667 Str := String_From_Name_Buffer;
7668 end if;
7669 end if;
7671 Append_To (Assoc,
7672 Make_Pragma_Argument_Association (Loc,
7673 Expression => Make_String_Literal (Loc, Str)));
7674 end if;
7676 -- Add Check pragma to list of statements
7678 Append_To (Stmts,
7679 Make_Pragma (Loc,
7680 Pragma_Identifier =>
7681 Make_Identifier (Loc, Name_Check),
7682 Pragma_Argument_Associations => Assoc));
7684 -- If Inherited case and option enabled, output info msg. Note
7685 -- that we know this is a case of Invariant'Class.
7687 if Inherit and Opt.List_Inherited_Aspects then
7688 Error_Msg_Sloc := Sloc (Ritem);
7689 Error_Msg_N
7690 ("info: & inherits `Invariant''Class` aspect from #?L?",
7691 Typ);
7692 end if;
7693 end if;
7695 <<Continue>>
7696 Next_Rep_Item (Ritem);
7697 end loop;
7698 end Add_Invariants;
7700 -- Start of processing for Build_Invariant_Procedure
7702 begin
7703 Stmts := No_List;
7704 PDecl := Empty;
7705 PBody := Empty;
7706 SId := Empty;
7708 -- If the aspect specification exists for some view of the type, the
7709 -- declaration for the procedure has been created.
7711 if Has_Invariants (Typ) then
7712 SId := Invariant_Procedure (Typ);
7713 end if;
7715 -- If the body is already present, nothing to do. This will occur when
7716 -- the type is already frozen, which is the case when the invariant
7717 -- appears in a private part, and the freezing takes place before the
7718 -- final pass over full declarations.
7720 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
7722 if Present (SId) then
7723 PDecl := Unit_Declaration_Node (SId);
7725 if Present (PDecl)
7726 and then Nkind (PDecl) = N_Subprogram_Declaration
7727 and then Present (Corresponding_Body (PDecl))
7728 then
7729 return;
7730 end if;
7732 else
7733 PDecl := Build_Invariant_Procedure_Declaration (Typ);
7734 end if;
7736 -- Recover formal of procedure, for use in the calls to invariant
7737 -- functions (including inherited ones).
7739 Object_Entity :=
7740 Defining_Identifier
7741 (First (Parameter_Specifications (Specification (PDecl))));
7742 Object_Name := Chars (Object_Entity);
7744 -- Add invariants for the current type
7746 Add_Invariants (Typ, Inherit => False);
7748 -- Add invariants for parent types
7750 declare
7751 Current_Typ : Entity_Id;
7752 Parent_Typ : Entity_Id;
7754 begin
7755 Current_Typ := Typ;
7756 loop
7757 Parent_Typ := Etype (Current_Typ);
7759 if Is_Private_Type (Parent_Typ)
7760 and then Present (Full_View (Base_Type (Parent_Typ)))
7761 then
7762 Parent_Typ := Full_View (Base_Type (Parent_Typ));
7763 end if;
7765 exit when Parent_Typ = Current_Typ;
7767 Current_Typ := Parent_Typ;
7768 Add_Invariants (Current_Typ, Inherit => True);
7769 end loop;
7770 end;
7772 -- Build the procedure if we generated at least one Check pragma
7774 if Stmts /= No_List then
7775 Spec := Copy_Separate_Tree (Specification (PDecl));
7777 PBody :=
7778 Make_Subprogram_Body (Loc,
7779 Specification => Spec,
7780 Declarations => Empty_List,
7781 Handled_Statement_Sequence =>
7782 Make_Handled_Sequence_Of_Statements (Loc,
7783 Statements => Stmts));
7785 -- Insert procedure declaration and spec at the appropriate points.
7786 -- If declaration is already analyzed, it was processed by the
7787 -- generated pragma.
7789 if Present (Private_Decls) then
7791 -- The spec goes at the end of visible declarations, but they have
7792 -- already been analyzed, so we need to explicitly do the analyze.
7794 if not Analyzed (PDecl) then
7795 Append_To (Visible_Decls, PDecl);
7796 Analyze (PDecl);
7797 end if;
7799 -- The body goes at the end of the private declarations, which we
7800 -- have not analyzed yet, so we do not need to perform an explicit
7801 -- analyze call. We skip this if there are no private declarations
7802 -- (this is an error that will be caught elsewhere);
7804 Append_To (Private_Decls, PBody);
7806 -- If the invariant appears on the full view of a type, the
7807 -- analysis of the private part is complete, and we must
7808 -- analyze the new body explicitly.
7810 if In_Private_Part (Current_Scope) then
7811 Analyze (PBody);
7812 end if;
7814 -- If there are no private declarations this may be an error that
7815 -- will be diagnosed elsewhere. However, if this is a non-private
7816 -- type that inherits invariants, it needs no completion and there
7817 -- may be no private part. In this case insert invariant procedure
7818 -- at end of current declarative list, and analyze at once, given
7819 -- that the type is about to be frozen.
7821 elsif not Is_Private_Type (Typ) then
7822 Append_To (Visible_Decls, PDecl);
7823 Append_To (Visible_Decls, PBody);
7824 Analyze (PDecl);
7825 Analyze (PBody);
7826 end if;
7827 end if;
7828 end Build_Invariant_Procedure;
7830 -------------------------------
7831 -- Build_Predicate_Functions --
7832 -------------------------------
7834 -- The procedures that are constructed here have the form:
7836 -- function typPredicate (Ixxx : typ) return Boolean is
7837 -- begin
7838 -- return
7839 -- exp1 and then exp2 and then ...
7840 -- and then typ1Predicate (typ1 (Ixxx))
7841 -- and then typ2Predicate (typ2 (Ixxx))
7842 -- and then ...;
7843 -- end typPredicate;
7845 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
7846 -- this is the point at which these expressions get analyzed, providing the
7847 -- required delay, and typ1, typ2, are entities from which predicates are
7848 -- inherited. Note that we do NOT generate Check pragmas, that's because we
7849 -- use this function even if checks are off, e.g. for membership tests.
7851 -- If the expression has at least one Raise_Expression, then we also build
7852 -- the typPredicateM version of the function, in which any occurrence of a
7853 -- Raise_Expression is converted to "return False".
7855 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
7856 Loc : constant Source_Ptr := Sloc (Typ);
7858 Expr : Node_Id;
7859 -- This is the expression for the result of the function. It is
7860 -- is build by connecting the component predicates with AND THEN.
7862 Expr_M : Node_Id;
7863 -- This is the corresponding return expression for the Predicate_M
7864 -- function. It differs in that raise expressions are marked for
7865 -- special expansion (see Process_REs).
7867 Object_Name : constant Name_Id := New_Internal_Name ('I');
7868 -- Name for argument of Predicate procedure. Note that we use the same
7869 -- name for both predicate functions. That way the reference within the
7870 -- predicate expression is the same in both functions.
7872 Object_Entity : constant Entity_Id :=
7873 Make_Defining_Identifier (Loc, Chars => Object_Name);
7874 -- Entity for argument of Predicate procedure
7876 Object_Entity_M : constant Entity_Id :=
7877 Make_Defining_Identifier (Loc, Chars => Object_Name);
7878 -- Entity for argument of Predicate_M procedure
7880 Raise_Expression_Present : Boolean := False;
7881 -- Set True if Expr has at least one Raise_Expression
7883 procedure Add_Call (T : Entity_Id);
7884 -- Includes a call to the predicate function for type T in Expr if T
7885 -- has predicates and Predicate_Function (T) is non-empty.
7887 procedure Add_Predicates;
7888 -- Appends expressions for any Predicate pragmas in the rep item chain
7889 -- Typ to Expr. Note that we look only at items for this exact entity.
7890 -- Inheritance of predicates for the parent type is done by calling the
7891 -- Predicate_Function of the parent type, using Add_Call above.
7893 function Test_RE (N : Node_Id) return Traverse_Result;
7894 -- Used in Test_REs, tests one node for being a raise expression, and if
7895 -- so sets Raise_Expression_Present True.
7897 procedure Test_REs is new Traverse_Proc (Test_RE);
7898 -- Tests to see if Expr contains any raise expressions
7900 function Process_RE (N : Node_Id) return Traverse_Result;
7901 -- Used in Process REs, tests if node N is a raise expression, and if
7902 -- so, marks it to be converted to return False.
7904 procedure Process_REs is new Traverse_Proc (Process_RE);
7905 -- Marks any raise expressions in Expr_M to return False
7907 --------------
7908 -- Add_Call --
7909 --------------
7911 procedure Add_Call (T : Entity_Id) is
7912 Exp : Node_Id;
7914 begin
7915 if Present (T) and then Present (Predicate_Function (T)) then
7916 Set_Has_Predicates (Typ);
7918 -- Build the call to the predicate function of T
7920 Exp :=
7921 Make_Predicate_Call
7922 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
7924 -- Add call to evolving expression, using AND THEN if needed
7926 if No (Expr) then
7927 Expr := Exp;
7929 else
7930 Expr :=
7931 Make_And_Then (Sloc (Expr),
7932 Left_Opnd => Relocate_Node (Expr),
7933 Right_Opnd => Exp);
7934 end if;
7936 -- Output info message on inheritance if required. Note we do not
7937 -- give this information for generic actual types, since it is
7938 -- unwelcome noise in that case in instantiations. We also
7939 -- generally suppress the message in instantiations, and also
7940 -- if it involves internal names.
7942 if Opt.List_Inherited_Aspects
7943 and then not Is_Generic_Actual_Type (Typ)
7944 and then Instantiation_Depth (Sloc (Typ)) = 0
7945 and then not Is_Internal_Name (Chars (T))
7946 and then not Is_Internal_Name (Chars (Typ))
7947 then
7948 Error_Msg_Sloc := Sloc (Predicate_Function (T));
7949 Error_Msg_Node_2 := T;
7950 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
7951 end if;
7952 end if;
7953 end Add_Call;
7955 --------------------
7956 -- Add_Predicates --
7957 --------------------
7959 procedure Add_Predicates is
7960 Ritem : Node_Id;
7961 Arg1 : Node_Id;
7962 Arg2 : Node_Id;
7964 procedure Replace_Type_Reference (N : Node_Id);
7965 -- Replace a single occurrence N of the subtype name with a reference
7966 -- to the formal of the predicate function. N can be an identifier
7967 -- referencing the subtype, or a selected component, representing an
7968 -- appropriately qualified occurrence of the subtype name.
7970 procedure Replace_Type_References is
7971 new Replace_Type_References_Generic (Replace_Type_Reference);
7972 -- Traverse an expression changing every occurrence of an identifier
7973 -- whose name matches the name of the subtype with a reference to
7974 -- the formal parameter of the predicate function.
7976 ----------------------------
7977 -- Replace_Type_Reference --
7978 ----------------------------
7980 procedure Replace_Type_Reference (N : Node_Id) is
7981 begin
7982 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7983 -- Use the Sloc of the usage name, not the defining name
7985 Set_Etype (N, Typ);
7986 Set_Entity (N, Object_Entity);
7988 -- We want to treat the node as if it comes from source, so that
7989 -- ASIS will not ignore it
7991 Set_Comes_From_Source (N, True);
7992 end Replace_Type_Reference;
7994 -- Start of processing for Add_Predicates
7996 begin
7997 Ritem := First_Rep_Item (Typ);
7998 while Present (Ritem) loop
7999 if Nkind (Ritem) = N_Pragma
8000 and then Pragma_Name (Ritem) = Name_Predicate
8001 then
8002 -- Acquire arguments
8004 Arg1 := First (Pragma_Argument_Associations (Ritem));
8005 Arg2 := Next (Arg1);
8007 Arg1 := Get_Pragma_Arg (Arg1);
8008 Arg2 := Get_Pragma_Arg (Arg2);
8010 -- See if this predicate pragma is for the current type or for
8011 -- its full view. A predicate on a private completion is placed
8012 -- on the partial view beause this is the visible entity that
8013 -- is frozen.
8015 if Entity (Arg1) = Typ
8016 or else Full_View (Entity (Arg1)) = Typ
8017 then
8018 -- We have a match, this entry is for our subtype
8020 -- We need to replace any occurrences of the name of the
8021 -- type with references to the object.
8023 Replace_Type_References (Arg2, Typ);
8025 -- If this predicate comes from an aspect, find the aspect
8026 -- specification, and replace the saved expression because
8027 -- we need the subtype references replaced for the calls to
8028 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8029 -- and Check_Aspect_At_End_Of_Declarations.
8031 if From_Aspect_Specification (Ritem) then
8032 declare
8033 Aitem : Node_Id;
8035 begin
8036 -- Loop to find corresponding aspect, note that this
8037 -- must be present given the pragma is marked delayed.
8039 Aitem := Next_Rep_Item (Ritem);
8040 loop
8041 if Nkind (Aitem) = N_Aspect_Specification
8042 and then Aspect_Rep_Item (Aitem) = Ritem
8043 then
8044 Set_Entity
8045 (Identifier (Aitem), New_Copy_Tree (Arg2));
8046 exit;
8047 end if;
8049 Aitem := Next_Rep_Item (Aitem);
8050 end loop;
8051 end;
8052 end if;
8054 -- Now we can add the expression
8056 if No (Expr) then
8057 Expr := Relocate_Node (Arg2);
8059 -- There already was a predicate, so add to it
8061 else
8062 Expr :=
8063 Make_And_Then (Loc,
8064 Left_Opnd => Relocate_Node (Expr),
8065 Right_Opnd => Relocate_Node (Arg2));
8066 end if;
8067 end if;
8068 end if;
8070 Next_Rep_Item (Ritem);
8071 end loop;
8072 end Add_Predicates;
8074 ----------------
8075 -- Process_RE --
8076 ----------------
8078 function Process_RE (N : Node_Id) return Traverse_Result is
8079 begin
8080 if Nkind (N) = N_Raise_Expression then
8081 Set_Convert_To_Return_False (N);
8082 return Skip;
8083 else
8084 return OK;
8085 end if;
8086 end Process_RE;
8088 -------------
8089 -- Test_RE --
8090 -------------
8092 function Test_RE (N : Node_Id) return Traverse_Result is
8093 begin
8094 if Nkind (N) = N_Raise_Expression then
8095 Raise_Expression_Present := True;
8096 return Abandon;
8097 else
8098 return OK;
8099 end if;
8100 end Test_RE;
8102 -- Start of processing for Build_Predicate_Functions
8104 begin
8105 -- Return if already built or if type does not have predicates
8107 if not Has_Predicates (Typ)
8108 or else Present (Predicate_Function (Typ))
8109 then
8110 return;
8111 end if;
8113 -- Prepare to construct predicate expression
8115 Expr := Empty;
8117 -- Add Predicates for the current type
8119 Add_Predicates;
8121 -- Add predicates for ancestor if present
8123 declare
8124 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8125 begin
8126 if Present (Atyp) then
8127 Add_Call (Atyp);
8128 end if;
8129 end;
8131 -- Case where predicates are present
8133 if Present (Expr) then
8135 -- Test for raise expression present
8137 Test_REs (Expr);
8139 -- If raise expression is present, capture a copy of Expr for use
8140 -- in building the predicateM function version later on. For this
8141 -- copy we replace references to Object_Entity by Object_Entity_M.
8143 if Raise_Expression_Present then
8144 declare
8145 Map : constant Elist_Id := New_Elmt_List;
8146 begin
8147 Append_Elmt (Object_Entity, Map);
8148 Append_Elmt (Object_Entity_M, Map);
8149 Expr_M := New_Copy_Tree (Expr, Map => Map);
8150 end;
8151 end if;
8153 -- Build the main predicate function
8155 declare
8156 SId : constant Entity_Id :=
8157 Make_Defining_Identifier (Loc,
8158 Chars => New_External_Name (Chars (Typ), "Predicate"));
8159 -- The entity for the the function spec
8161 SIdB : constant Entity_Id :=
8162 Make_Defining_Identifier (Loc,
8163 Chars => New_External_Name (Chars (Typ), "Predicate"));
8164 -- The entity for the function body
8166 Spec : Node_Id;
8167 FDecl : Node_Id;
8168 FBody : Node_Id;
8170 begin
8171 -- Build function declaration
8173 Set_Ekind (SId, E_Function);
8174 Set_Is_Internal (SId);
8175 Set_Is_Predicate_Function (SId);
8176 Set_Predicate_Function (Typ, SId);
8178 -- The predicate function is shared between views of a type
8180 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8181 Set_Predicate_Function (Full_View (Typ), SId);
8182 end if;
8184 Spec :=
8185 Make_Function_Specification (Loc,
8186 Defining_Unit_Name => SId,
8187 Parameter_Specifications => New_List (
8188 Make_Parameter_Specification (Loc,
8189 Defining_Identifier => Object_Entity,
8190 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8191 Result_Definition =>
8192 New_Occurrence_Of (Standard_Boolean, Loc));
8194 FDecl :=
8195 Make_Subprogram_Declaration (Loc,
8196 Specification => Spec);
8198 -- Build function body
8200 Spec :=
8201 Make_Function_Specification (Loc,
8202 Defining_Unit_Name => SIdB,
8203 Parameter_Specifications => New_List (
8204 Make_Parameter_Specification (Loc,
8205 Defining_Identifier =>
8206 Make_Defining_Identifier (Loc, Object_Name),
8207 Parameter_Type =>
8208 New_Occurrence_Of (Typ, Loc))),
8209 Result_Definition =>
8210 New_Occurrence_Of (Standard_Boolean, Loc));
8212 FBody :=
8213 Make_Subprogram_Body (Loc,
8214 Specification => Spec,
8215 Declarations => Empty_List,
8216 Handled_Statement_Sequence =>
8217 Make_Handled_Sequence_Of_Statements (Loc,
8218 Statements => New_List (
8219 Make_Simple_Return_Statement (Loc,
8220 Expression => Expr))));
8222 -- Insert declaration before freeze node and body after
8224 Insert_Before_And_Analyze (N, FDecl);
8225 Insert_After_And_Analyze (N, FBody);
8226 end;
8228 -- Test for raise expressions present and if so build M version
8230 if Raise_Expression_Present then
8231 declare
8232 SId : constant Entity_Id :=
8233 Make_Defining_Identifier (Loc,
8234 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8235 -- The entity for the the function spec
8237 SIdB : constant Entity_Id :=
8238 Make_Defining_Identifier (Loc,
8239 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8240 -- The entity for the function body
8242 Spec : Node_Id;
8243 FDecl : Node_Id;
8244 FBody : Node_Id;
8245 BTemp : Entity_Id;
8247 begin
8248 -- Mark any raise expressions for special expansion
8250 Process_REs (Expr_M);
8252 -- Build function declaration
8254 Set_Ekind (SId, E_Function);
8255 Set_Is_Predicate_Function_M (SId);
8256 Set_Predicate_Function_M (Typ, SId);
8258 -- The predicate function is shared between views of a type
8260 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8261 Set_Predicate_Function_M (Full_View (Typ), SId);
8262 end if;
8264 Spec :=
8265 Make_Function_Specification (Loc,
8266 Defining_Unit_Name => SId,
8267 Parameter_Specifications => New_List (
8268 Make_Parameter_Specification (Loc,
8269 Defining_Identifier => Object_Entity_M,
8270 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8271 Result_Definition =>
8272 New_Occurrence_Of (Standard_Boolean, Loc));
8274 FDecl :=
8275 Make_Subprogram_Declaration (Loc,
8276 Specification => Spec);
8278 -- Build function body
8280 Spec :=
8281 Make_Function_Specification (Loc,
8282 Defining_Unit_Name => SIdB,
8283 Parameter_Specifications => New_List (
8284 Make_Parameter_Specification (Loc,
8285 Defining_Identifier =>
8286 Make_Defining_Identifier (Loc, Object_Name),
8287 Parameter_Type =>
8288 New_Occurrence_Of (Typ, Loc))),
8289 Result_Definition =>
8290 New_Occurrence_Of (Standard_Boolean, Loc));
8292 -- Build the body, we declare the boolean expression before
8293 -- doing the return, because we are not really confident of
8294 -- what happens if a return appears within a return.
8296 BTemp :=
8297 Make_Defining_Identifier (Loc,
8298 Chars => New_Internal_Name ('B'));
8300 FBody :=
8301 Make_Subprogram_Body (Loc,
8302 Specification => Spec,
8304 Declarations => New_List (
8305 Make_Object_Declaration (Loc,
8306 Defining_Identifier => BTemp,
8307 Constant_Present => True,
8308 Object_Definition =>
8309 New_Occurrence_Of (Standard_Boolean, Loc),
8310 Expression => Expr_M)),
8312 Handled_Statement_Sequence =>
8313 Make_Handled_Sequence_Of_Statements (Loc,
8314 Statements => New_List (
8315 Make_Simple_Return_Statement (Loc,
8316 Expression => New_Occurrence_Of (BTemp, Loc)))));
8318 -- Insert declaration before freeze node and body after
8320 Insert_Before_And_Analyze (N, FDecl);
8321 Insert_After_And_Analyze (N, FBody);
8322 end;
8323 end if;
8325 -- See if we have a static predicate. Note that the answer may be
8326 -- yes even if we have an explicit Dynamic_Predicate present.
8328 declare
8329 PS : Boolean;
8330 EN : Node_Id;
8332 begin
8333 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8334 PS := False;
8335 else
8336 PS := Is_Predicate_Static (Expr, Object_Name);
8337 end if;
8339 -- Case where we have a predicate-static aspect
8341 if PS then
8343 -- We don't set Has_Static_Predicate_Aspect, since we can have
8344 -- any of the three cases (Predicate, Dynamic_Predicate, or
8345 -- Static_Predicate) generating a predicate with an expression
8346 -- that is predicate-static. We just indicate that we have a
8347 -- predicate that can be treated as static.
8349 Set_Has_Static_Predicate (Typ);
8351 -- For discrete subtype, build the static predicate list
8353 if Is_Discrete_Type (Typ) then
8354 if not Is_Static_Subtype (Typ) then
8356 -- This can only happen in the presence of previous
8357 -- semantic errors.
8359 pragma Assert (Serious_Errors_Detected > 0);
8360 return;
8361 end if;
8363 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8365 -- If we don't get a static predicate list, it means that we
8366 -- have a case where this is not possible, most typically in
8367 -- the case where we inherit a dynamic predicate. We do not
8368 -- consider this an error, we just leave the predicate as
8369 -- dynamic. But if we do succeed in building the list, then
8370 -- we mark the predicate as static.
8372 if No (Static_Discrete_Predicate (Typ)) then
8373 Set_Has_Static_Predicate (Typ, False);
8374 end if;
8376 -- For real or string subtype, save predicate expression
8378 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8379 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8380 end if;
8382 -- Case of dynamic predicate (expression is not predicate-static)
8384 else
8385 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8386 -- is only set if we have an explicit Dynamic_Predicate aspect
8387 -- given. Here we may simply have a Predicate aspect where the
8388 -- expression happens not to be predicate-static.
8390 -- Emit an error when the predicate is categorized as static
8391 -- but its expression is not predicate-static.
8393 -- First a little fiddling to get a nice location for the
8394 -- message. If the expression is of the form (A and then B),
8395 -- then use the left operand for the Sloc. This avoids getting
8396 -- confused by a call to a higher-level predicate with a less
8397 -- convenient source location.
8399 EN := Expr;
8400 while Nkind (EN) = N_And_Then loop
8401 EN := Left_Opnd (EN);
8402 end loop;
8404 -- Now post appropriate message
8406 if Has_Static_Predicate_Aspect (Typ) then
8407 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8408 Error_Msg_F
8409 ("expression is not predicate-static (RM 3.2.4(16-22))",
8410 EN);
8411 else
8412 Error_Msg_F
8413 ("static predicate requires scalar or string type", EN);
8414 end if;
8415 end if;
8416 end if;
8417 end;
8418 end if;
8419 end Build_Predicate_Functions;
8421 -----------------------------------------
8422 -- Check_Aspect_At_End_Of_Declarations --
8423 -----------------------------------------
8425 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8426 Ent : constant Entity_Id := Entity (ASN);
8427 Ident : constant Node_Id := Identifier (ASN);
8428 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8430 End_Decl_Expr : constant Node_Id := Entity (Ident);
8431 -- Expression to be analyzed at end of declarations
8433 Freeze_Expr : constant Node_Id := Expression (ASN);
8434 -- Expression from call to Check_Aspect_At_Freeze_Point
8436 T : constant Entity_Id := Etype (Freeze_Expr);
8437 -- Type required for preanalyze call
8439 Err : Boolean;
8440 -- Set False if error
8442 -- On entry to this procedure, Entity (Ident) contains a copy of the
8443 -- original expression from the aspect, saved for this purpose, and
8444 -- but Expression (Ident) is a preanalyzed copy of the expression,
8445 -- preanalyzed just after the freeze point.
8447 procedure Check_Overloaded_Name;
8448 -- For aspects whose expression is simply a name, this routine checks if
8449 -- the name is overloaded or not. If so, it verifies there is an
8450 -- interpretation that matches the entity obtained at the freeze point,
8451 -- otherwise the compiler complains.
8453 ---------------------------
8454 -- Check_Overloaded_Name --
8455 ---------------------------
8457 procedure Check_Overloaded_Name is
8458 begin
8459 if not Is_Overloaded (End_Decl_Expr) then
8460 Err := not Is_Entity_Name (End_Decl_Expr)
8461 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8463 else
8464 Err := True;
8466 declare
8467 Index : Interp_Index;
8468 It : Interp;
8470 begin
8471 Get_First_Interp (End_Decl_Expr, Index, It);
8472 while Present (It.Typ) loop
8473 if It.Nam = Entity (Freeze_Expr) then
8474 Err := False;
8475 exit;
8476 end if;
8478 Get_Next_Interp (Index, It);
8479 end loop;
8480 end;
8481 end if;
8482 end Check_Overloaded_Name;
8484 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8486 begin
8487 -- Case of aspects Dimension, Dimension_System and Synchronization
8489 if A_Id = Aspect_Synchronization then
8490 return;
8492 -- Case of stream attributes, just have to compare entities. However,
8493 -- the expression is just a name (possibly overloaded), and there may
8494 -- be stream operations declared for unrelated types, so we just need
8495 -- to verify that one of these interpretations is the one available at
8496 -- at the freeze point.
8498 elsif A_Id = Aspect_Input or else
8499 A_Id = Aspect_Output or else
8500 A_Id = Aspect_Read or else
8501 A_Id = Aspect_Write
8502 then
8503 Analyze (End_Decl_Expr);
8504 Check_Overloaded_Name;
8506 elsif A_Id = Aspect_Variable_Indexing or else
8507 A_Id = Aspect_Constant_Indexing or else
8508 A_Id = Aspect_Default_Iterator or else
8509 A_Id = Aspect_Iterator_Element
8510 then
8511 -- Make type unfrozen before analysis, to prevent spurious errors
8512 -- about late attributes.
8514 Set_Is_Frozen (Ent, False);
8515 Analyze (End_Decl_Expr);
8516 Set_Is_Frozen (Ent, True);
8518 -- If the end of declarations comes before any other freeze
8519 -- point, the Freeze_Expr is not analyzed: no check needed.
8521 if Analyzed (Freeze_Expr) and then not In_Instance then
8522 Check_Overloaded_Name;
8523 else
8524 Err := False;
8525 end if;
8527 -- All other cases
8529 else
8530 -- Indicate that the expression comes from an aspect specification,
8531 -- which is used in subsequent analysis even if expansion is off.
8533 Set_Parent (End_Decl_Expr, ASN);
8535 -- In a generic context the aspect expressions have not been
8536 -- preanalyzed, so do it now. There are no conformance checks
8537 -- to perform in this case.
8539 if No (T) then
8540 Check_Aspect_At_Freeze_Point (ASN);
8541 return;
8543 -- The default values attributes may be defined in the private part,
8544 -- and the analysis of the expression may take place when only the
8545 -- partial view is visible. The expression must be scalar, so use
8546 -- the full view to resolve.
8548 elsif (A_Id = Aspect_Default_Value
8549 or else
8550 A_Id = Aspect_Default_Component_Value)
8551 and then Is_Private_Type (T)
8552 then
8553 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8555 else
8556 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8557 end if;
8559 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8560 end if;
8562 -- Output error message if error. Force error on aspect specification
8563 -- even if there is an error on the expression itself.
8565 if Err then
8566 Error_Msg_NE
8567 ("!visibility of aspect for& changes after freeze point",
8568 ASN, Ent);
8569 Error_Msg_NE
8570 ("info: & is frozen here, aspects evaluated at this point??",
8571 Freeze_Node (Ent), Ent);
8572 end if;
8573 end Check_Aspect_At_End_Of_Declarations;
8575 ----------------------------------
8576 -- Check_Aspect_At_Freeze_Point --
8577 ----------------------------------
8579 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8580 Ident : constant Node_Id := Identifier (ASN);
8581 -- Identifier (use Entity field to save expression)
8583 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8585 T : Entity_Id := Empty;
8586 -- Type required for preanalyze call
8588 begin
8589 -- On entry to this procedure, Entity (Ident) contains a copy of the
8590 -- original expression from the aspect, saved for this purpose.
8592 -- On exit from this procedure Entity (Ident) is unchanged, still
8593 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8594 -- of the expression, preanalyzed just after the freeze point.
8596 -- Make a copy of the expression to be preanalyzed
8598 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8600 -- Find type for preanalyze call
8602 case A_Id is
8604 -- No_Aspect should be impossible
8606 when No_Aspect =>
8607 raise Program_Error;
8609 -- Aspects taking an optional boolean argument
8611 when Boolean_Aspects |
8612 Library_Unit_Aspects =>
8614 T := Standard_Boolean;
8616 -- Aspects corresponding to attribute definition clauses
8618 when Aspect_Address =>
8619 T := RTE (RE_Address);
8621 when Aspect_Attach_Handler =>
8622 T := RTE (RE_Interrupt_ID);
8624 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8625 T := RTE (RE_Bit_Order);
8627 when Aspect_Convention =>
8628 return;
8630 when Aspect_CPU =>
8631 T := RTE (RE_CPU_Range);
8633 -- Default_Component_Value is resolved with the component type
8635 when Aspect_Default_Component_Value =>
8636 T := Component_Type (Entity (ASN));
8638 -- Default_Value is resolved with the type entity in question
8640 when Aspect_Default_Value =>
8641 T := Entity (ASN);
8643 -- Depends is a delayed aspect because it mentiones names first
8644 -- introduced by aspect Global which is already delayed. There is
8645 -- no action to be taken with respect to the aspect itself as the
8646 -- analysis is done by the corresponding pragma.
8648 when Aspect_Depends =>
8649 return;
8651 when Aspect_Dispatching_Domain =>
8652 T := RTE (RE_Dispatching_Domain);
8654 when Aspect_External_Tag =>
8655 T := Standard_String;
8657 when Aspect_External_Name =>
8658 T := Standard_String;
8660 -- Global is a delayed aspect because it may reference names that
8661 -- have not been declared yet. There is no action to be taken with
8662 -- respect to the aspect itself as the reference checking is done
8663 -- on the corresponding pragma.
8665 when Aspect_Global =>
8666 return;
8668 when Aspect_Link_Name =>
8669 T := Standard_String;
8671 when Aspect_Priority | Aspect_Interrupt_Priority =>
8672 T := Standard_Integer;
8674 when Aspect_Relative_Deadline =>
8675 T := RTE (RE_Time_Span);
8677 when Aspect_Small =>
8678 T := Universal_Real;
8680 -- For a simple storage pool, we have to retrieve the type of the
8681 -- pool object associated with the aspect's corresponding attribute
8682 -- definition clause.
8684 when Aspect_Simple_Storage_Pool =>
8685 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8687 when Aspect_Storage_Pool =>
8688 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8690 when Aspect_Alignment |
8691 Aspect_Component_Size |
8692 Aspect_Machine_Radix |
8693 Aspect_Object_Size |
8694 Aspect_Size |
8695 Aspect_Storage_Size |
8696 Aspect_Stream_Size |
8697 Aspect_Value_Size =>
8698 T := Any_Integer;
8700 when Aspect_Linker_Section =>
8701 T := Standard_String;
8703 when Aspect_Synchronization =>
8704 return;
8706 -- Special case, the expression of these aspects is just an entity
8707 -- that does not need any resolution, so just analyze.
8709 when Aspect_Input |
8710 Aspect_Output |
8711 Aspect_Read |
8712 Aspect_Suppress |
8713 Aspect_Unsuppress |
8714 Aspect_Warnings |
8715 Aspect_Write =>
8716 Analyze (Expression (ASN));
8717 return;
8719 -- Same for Iterator aspects, where the expression is a function
8720 -- name. Legality rules are checked separately.
8722 when Aspect_Constant_Indexing |
8723 Aspect_Default_Iterator |
8724 Aspect_Iterator_Element |
8725 Aspect_Variable_Indexing =>
8726 Analyze (Expression (ASN));
8727 return;
8729 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8731 when Aspect_Iterable =>
8732 T := Entity (ASN);
8734 declare
8735 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8736 Assoc : Node_Id;
8737 Expr : Node_Id;
8739 begin
8740 if Cursor = Any_Type then
8741 return;
8742 end if;
8744 Assoc := First (Component_Associations (Expression (ASN)));
8745 while Present (Assoc) loop
8746 Expr := Expression (Assoc);
8747 Analyze (Expr);
8749 if not Error_Posted (Expr) then
8750 Resolve_Iterable_Operation
8751 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8752 end if;
8754 Next (Assoc);
8755 end loop;
8756 end;
8758 return;
8760 -- Invariant/Predicate take boolean expressions
8762 when Aspect_Dynamic_Predicate |
8763 Aspect_Invariant |
8764 Aspect_Predicate |
8765 Aspect_Static_Predicate |
8766 Aspect_Type_Invariant =>
8767 T := Standard_Boolean;
8769 -- Here is the list of aspects that don't require delay analysis
8771 when Aspect_Abstract_State |
8772 Aspect_Annotate |
8773 Aspect_Contract_Cases |
8774 Aspect_Default_Initial_Condition |
8775 Aspect_Dimension |
8776 Aspect_Dimension_System |
8777 Aspect_Implicit_Dereference |
8778 Aspect_Initial_Condition |
8779 Aspect_Initializes |
8780 Aspect_Obsolescent |
8781 Aspect_Part_Of |
8782 Aspect_Post |
8783 Aspect_Postcondition |
8784 Aspect_Pre |
8785 Aspect_Precondition |
8786 Aspect_Refined_Depends |
8787 Aspect_Refined_Global |
8788 Aspect_Refined_Post |
8789 Aspect_Refined_State |
8790 Aspect_SPARK_Mode |
8791 Aspect_Test_Case =>
8792 raise Program_Error;
8794 end case;
8796 -- Do the preanalyze call
8798 Preanalyze_Spec_Expression (Expression (ASN), T);
8799 end Check_Aspect_At_Freeze_Point;
8801 -----------------------------------
8802 -- Check_Constant_Address_Clause --
8803 -----------------------------------
8805 procedure Check_Constant_Address_Clause
8806 (Expr : Node_Id;
8807 U_Ent : Entity_Id)
8809 procedure Check_At_Constant_Address (Nod : Node_Id);
8810 -- Checks that the given node N represents a name whose 'Address is
8811 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8812 -- address value is the same at the point of declaration of U_Ent and at
8813 -- the time of elaboration of the address clause.
8815 procedure Check_Expr_Constants (Nod : Node_Id);
8816 -- Checks that Nod meets the requirements for a constant address clause
8817 -- in the sense of the enclosing procedure.
8819 procedure Check_List_Constants (Lst : List_Id);
8820 -- Check that all elements of list Lst meet the requirements for a
8821 -- constant address clause in the sense of the enclosing procedure.
8823 -------------------------------
8824 -- Check_At_Constant_Address --
8825 -------------------------------
8827 procedure Check_At_Constant_Address (Nod : Node_Id) is
8828 begin
8829 if Is_Entity_Name (Nod) then
8830 if Present (Address_Clause (Entity ((Nod)))) then
8831 Error_Msg_NE
8832 ("invalid address clause for initialized object &!",
8833 Nod, U_Ent);
8834 Error_Msg_NE
8835 ("address for& cannot" &
8836 " depend on another address clause! (RM 13.1(22))!",
8837 Nod, U_Ent);
8839 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
8840 and then Sloc (U_Ent) < Sloc (Entity (Nod))
8841 then
8842 Error_Msg_NE
8843 ("invalid address clause for initialized object &!",
8844 Nod, U_Ent);
8845 Error_Msg_Node_2 := U_Ent;
8846 Error_Msg_NE
8847 ("\& must be defined before & (RM 13.1(22))!",
8848 Nod, Entity (Nod));
8849 end if;
8851 elsif Nkind (Nod) = N_Selected_Component then
8852 declare
8853 T : constant Entity_Id := Etype (Prefix (Nod));
8855 begin
8856 if (Is_Record_Type (T)
8857 and then Has_Discriminants (T))
8858 or else
8859 (Is_Access_Type (T)
8860 and then Is_Record_Type (Designated_Type (T))
8861 and then Has_Discriminants (Designated_Type (T)))
8862 then
8863 Error_Msg_NE
8864 ("invalid address clause for initialized object &!",
8865 Nod, U_Ent);
8866 Error_Msg_N
8867 ("\address cannot depend on component" &
8868 " of discriminated record (RM 13.1(22))!",
8869 Nod);
8870 else
8871 Check_At_Constant_Address (Prefix (Nod));
8872 end if;
8873 end;
8875 elsif Nkind (Nod) = N_Indexed_Component then
8876 Check_At_Constant_Address (Prefix (Nod));
8877 Check_List_Constants (Expressions (Nod));
8879 else
8880 Check_Expr_Constants (Nod);
8881 end if;
8882 end Check_At_Constant_Address;
8884 --------------------------
8885 -- Check_Expr_Constants --
8886 --------------------------
8888 procedure Check_Expr_Constants (Nod : Node_Id) is
8889 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
8890 Ent : Entity_Id := Empty;
8892 begin
8893 if Nkind (Nod) in N_Has_Etype
8894 and then Etype (Nod) = Any_Type
8895 then
8896 return;
8897 end if;
8899 case Nkind (Nod) is
8900 when N_Empty | N_Error =>
8901 return;
8903 when N_Identifier | N_Expanded_Name =>
8904 Ent := Entity (Nod);
8906 -- We need to look at the original node if it is different
8907 -- from the node, since we may have rewritten things and
8908 -- substituted an identifier representing the rewrite.
8910 if Original_Node (Nod) /= Nod then
8911 Check_Expr_Constants (Original_Node (Nod));
8913 -- If the node is an object declaration without initial
8914 -- value, some code has been expanded, and the expression
8915 -- is not constant, even if the constituents might be
8916 -- acceptable, as in A'Address + offset.
8918 if Ekind (Ent) = E_Variable
8919 and then
8920 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
8921 and then
8922 No (Expression (Declaration_Node (Ent)))
8923 then
8924 Error_Msg_NE
8925 ("invalid address clause for initialized object &!",
8926 Nod, U_Ent);
8928 -- If entity is constant, it may be the result of expanding
8929 -- a check. We must verify that its declaration appears
8930 -- before the object in question, else we also reject the
8931 -- address clause.
8933 elsif Ekind (Ent) = E_Constant
8934 and then In_Same_Source_Unit (Ent, U_Ent)
8935 and then Sloc (Ent) > Loc_U_Ent
8936 then
8937 Error_Msg_NE
8938 ("invalid address clause for initialized object &!",
8939 Nod, U_Ent);
8940 end if;
8942 return;
8943 end if;
8945 -- Otherwise look at the identifier and see if it is OK
8947 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
8948 or else Is_Type (Ent)
8949 then
8950 return;
8952 elsif
8953 Ekind (Ent) = E_Constant
8954 or else
8955 Ekind (Ent) = E_In_Parameter
8956 then
8957 -- This is the case where we must have Ent defined before
8958 -- U_Ent. Clearly if they are in different units this
8959 -- requirement is met since the unit containing Ent is
8960 -- already processed.
8962 if not In_Same_Source_Unit (Ent, U_Ent) then
8963 return;
8965 -- Otherwise location of Ent must be before the location
8966 -- of U_Ent, that's what prior defined means.
8968 elsif Sloc (Ent) < Loc_U_Ent then
8969 return;
8971 else
8972 Error_Msg_NE
8973 ("invalid address clause for initialized object &!",
8974 Nod, U_Ent);
8975 Error_Msg_Node_2 := U_Ent;
8976 Error_Msg_NE
8977 ("\& must be defined before & (RM 13.1(22))!",
8978 Nod, Ent);
8979 end if;
8981 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
8982 Check_Expr_Constants (Original_Node (Nod));
8984 else
8985 Error_Msg_NE
8986 ("invalid address clause for initialized object &!",
8987 Nod, U_Ent);
8989 if Comes_From_Source (Ent) then
8990 Error_Msg_NE
8991 ("\reference to variable& not allowed"
8992 & " (RM 13.1(22))!", Nod, Ent);
8993 else
8994 Error_Msg_N
8995 ("non-static expression not allowed"
8996 & " (RM 13.1(22))!", Nod);
8997 end if;
8998 end if;
9000 when N_Integer_Literal =>
9002 -- If this is a rewritten unchecked conversion, in a system
9003 -- where Address is an integer type, always use the base type
9004 -- for a literal value. This is user-friendly and prevents
9005 -- order-of-elaboration issues with instances of unchecked
9006 -- conversion.
9008 if Nkind (Original_Node (Nod)) = N_Function_Call then
9009 Set_Etype (Nod, Base_Type (Etype (Nod)));
9010 end if;
9012 when N_Real_Literal |
9013 N_String_Literal |
9014 N_Character_Literal =>
9015 return;
9017 when N_Range =>
9018 Check_Expr_Constants (Low_Bound (Nod));
9019 Check_Expr_Constants (High_Bound (Nod));
9021 when N_Explicit_Dereference =>
9022 Check_Expr_Constants (Prefix (Nod));
9024 when N_Indexed_Component =>
9025 Check_Expr_Constants (Prefix (Nod));
9026 Check_List_Constants (Expressions (Nod));
9028 when N_Slice =>
9029 Check_Expr_Constants (Prefix (Nod));
9030 Check_Expr_Constants (Discrete_Range (Nod));
9032 when N_Selected_Component =>
9033 Check_Expr_Constants (Prefix (Nod));
9035 when N_Attribute_Reference =>
9036 if Nam_In (Attribute_Name (Nod), Name_Address,
9037 Name_Access,
9038 Name_Unchecked_Access,
9039 Name_Unrestricted_Access)
9040 then
9041 Check_At_Constant_Address (Prefix (Nod));
9043 else
9044 Check_Expr_Constants (Prefix (Nod));
9045 Check_List_Constants (Expressions (Nod));
9046 end if;
9048 when N_Aggregate =>
9049 Check_List_Constants (Component_Associations (Nod));
9050 Check_List_Constants (Expressions (Nod));
9052 when N_Component_Association =>
9053 Check_Expr_Constants (Expression (Nod));
9055 when N_Extension_Aggregate =>
9056 Check_Expr_Constants (Ancestor_Part (Nod));
9057 Check_List_Constants (Component_Associations (Nod));
9058 Check_List_Constants (Expressions (Nod));
9060 when N_Null =>
9061 return;
9063 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9064 Check_Expr_Constants (Left_Opnd (Nod));
9065 Check_Expr_Constants (Right_Opnd (Nod));
9067 when N_Unary_Op =>
9068 Check_Expr_Constants (Right_Opnd (Nod));
9070 when N_Type_Conversion |
9071 N_Qualified_Expression |
9072 N_Allocator |
9073 N_Unchecked_Type_Conversion =>
9074 Check_Expr_Constants (Expression (Nod));
9076 when N_Function_Call =>
9077 if not Is_Pure (Entity (Name (Nod))) then
9078 Error_Msg_NE
9079 ("invalid address clause for initialized object &!",
9080 Nod, U_Ent);
9082 Error_Msg_NE
9083 ("\function & is not pure (RM 13.1(22))!",
9084 Nod, Entity (Name (Nod)));
9086 else
9087 Check_List_Constants (Parameter_Associations (Nod));
9088 end if;
9090 when N_Parameter_Association =>
9091 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9093 when others =>
9094 Error_Msg_NE
9095 ("invalid address clause for initialized object &!",
9096 Nod, U_Ent);
9097 Error_Msg_NE
9098 ("\must be constant defined before& (RM 13.1(22))!",
9099 Nod, U_Ent);
9100 end case;
9101 end Check_Expr_Constants;
9103 --------------------------
9104 -- Check_List_Constants --
9105 --------------------------
9107 procedure Check_List_Constants (Lst : List_Id) is
9108 Nod1 : Node_Id;
9110 begin
9111 if Present (Lst) then
9112 Nod1 := First (Lst);
9113 while Present (Nod1) loop
9114 Check_Expr_Constants (Nod1);
9115 Next (Nod1);
9116 end loop;
9117 end if;
9118 end Check_List_Constants;
9120 -- Start of processing for Check_Constant_Address_Clause
9122 begin
9123 -- If rep_clauses are to be ignored, no need for legality checks. In
9124 -- particular, no need to pester user about rep clauses that violate
9125 -- the rule on constant addresses, given that these clauses will be
9126 -- removed by Freeze before they reach the back end.
9128 if not Ignore_Rep_Clauses then
9129 Check_Expr_Constants (Expr);
9130 end if;
9131 end Check_Constant_Address_Clause;
9133 ---------------------------
9134 -- Check_Pool_Size_Clash --
9135 ---------------------------
9137 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9138 Post : Node_Id;
9140 begin
9141 -- We need to find out which one came first. Note that in the case of
9142 -- aspects mixed with pragmas there are cases where the processing order
9143 -- is reversed, which is why we do the check here.
9145 if Sloc (SP) < Sloc (SS) then
9146 Error_Msg_Sloc := Sloc (SP);
9147 Post := SS;
9148 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9150 else
9151 Error_Msg_Sloc := Sloc (SS);
9152 Post := SP;
9153 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9154 end if;
9156 Error_Msg_N
9157 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9158 end Check_Pool_Size_Clash;
9160 ----------------------------------------
9161 -- Check_Record_Representation_Clause --
9162 ----------------------------------------
9164 procedure Check_Record_Representation_Clause (N : Node_Id) is
9165 Loc : constant Source_Ptr := Sloc (N);
9166 Ident : constant Node_Id := Identifier (N);
9167 Rectype : Entity_Id;
9168 Fent : Entity_Id;
9169 CC : Node_Id;
9170 Fbit : Uint;
9171 Lbit : Uint;
9172 Hbit : Uint := Uint_0;
9173 Comp : Entity_Id;
9174 Pcomp : Entity_Id;
9176 Max_Bit_So_Far : Uint;
9177 -- Records the maximum bit position so far. If all field positions
9178 -- are monotonically increasing, then we can skip the circuit for
9179 -- checking for overlap, since no overlap is possible.
9181 Tagged_Parent : Entity_Id := Empty;
9182 -- This is set in the case of a derived tagged type for which we have
9183 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9184 -- positioned by record representation clauses). In this case we must
9185 -- check for overlap between components of this tagged type, and the
9186 -- components of its parent. Tagged_Parent will point to this parent
9187 -- type. For all other cases Tagged_Parent is left set to Empty.
9189 Parent_Last_Bit : Uint;
9190 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9191 -- last bit position for any field in the parent type. We only need to
9192 -- check overlap for fields starting below this point.
9194 Overlap_Check_Required : Boolean;
9195 -- Used to keep track of whether or not an overlap check is required
9197 Overlap_Detected : Boolean := False;
9198 -- Set True if an overlap is detected
9200 Ccount : Natural := 0;
9201 -- Number of component clauses in record rep clause
9203 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9204 -- Given two entities for record components or discriminants, checks
9205 -- if they have overlapping component clauses and issues errors if so.
9207 procedure Find_Component;
9208 -- Finds component entity corresponding to current component clause (in
9209 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9210 -- start/stop bits for the field. If there is no matching component or
9211 -- if the matching component does not have a component clause, then
9212 -- that's an error and Comp is set to Empty, but no error message is
9213 -- issued, since the message was already given. Comp is also set to
9214 -- Empty if the current "component clause" is in fact a pragma.
9216 -----------------------------
9217 -- Check_Component_Overlap --
9218 -----------------------------
9220 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9221 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9222 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9224 begin
9225 if Present (CC1) and then Present (CC2) then
9227 -- Exclude odd case where we have two tag components in the same
9228 -- record, both at location zero. This seems a bit strange, but
9229 -- it seems to happen in some circumstances, perhaps on an error.
9231 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9232 return;
9233 end if;
9235 -- Here we check if the two fields overlap
9237 declare
9238 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9239 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9240 E1 : constant Uint := S1 + Esize (C1_Ent);
9241 E2 : constant Uint := S2 + Esize (C2_Ent);
9243 begin
9244 if E2 <= S1 or else E1 <= S2 then
9245 null;
9246 else
9247 Error_Msg_Node_2 := Component_Name (CC2);
9248 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9249 Error_Msg_Node_1 := Component_Name (CC1);
9250 Error_Msg_N
9251 ("component& overlaps & #", Component_Name (CC1));
9252 Overlap_Detected := True;
9253 end if;
9254 end;
9255 end if;
9256 end Check_Component_Overlap;
9258 --------------------
9259 -- Find_Component --
9260 --------------------
9262 procedure Find_Component is
9264 procedure Search_Component (R : Entity_Id);
9265 -- Search components of R for a match. If found, Comp is set
9267 ----------------------
9268 -- Search_Component --
9269 ----------------------
9271 procedure Search_Component (R : Entity_Id) is
9272 begin
9273 Comp := First_Component_Or_Discriminant (R);
9274 while Present (Comp) loop
9276 -- Ignore error of attribute name for component name (we
9277 -- already gave an error message for this, so no need to
9278 -- complain here)
9280 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9281 null;
9282 else
9283 exit when Chars (Comp) = Chars (Component_Name (CC));
9284 end if;
9286 Next_Component_Or_Discriminant (Comp);
9287 end loop;
9288 end Search_Component;
9290 -- Start of processing for Find_Component
9292 begin
9293 -- Return with Comp set to Empty if we have a pragma
9295 if Nkind (CC) = N_Pragma then
9296 Comp := Empty;
9297 return;
9298 end if;
9300 -- Search current record for matching component
9302 Search_Component (Rectype);
9304 -- If not found, maybe component of base type discriminant that is
9305 -- absent from statically constrained first subtype.
9307 if No (Comp) then
9308 Search_Component (Base_Type (Rectype));
9309 end if;
9311 -- If no component, or the component does not reference the component
9312 -- clause in question, then there was some previous error for which
9313 -- we already gave a message, so just return with Comp Empty.
9315 if No (Comp) or else Component_Clause (Comp) /= CC then
9316 Check_Error_Detected;
9317 Comp := Empty;
9319 -- Normal case where we have a component clause
9321 else
9322 Fbit := Component_Bit_Offset (Comp);
9323 Lbit := Fbit + Esize (Comp) - 1;
9324 end if;
9325 end Find_Component;
9327 -- Start of processing for Check_Record_Representation_Clause
9329 begin
9330 Find_Type (Ident);
9331 Rectype := Entity (Ident);
9333 if Rectype = Any_Type then
9334 return;
9335 else
9336 Rectype := Underlying_Type (Rectype);
9337 end if;
9339 -- See if we have a fully repped derived tagged type
9341 declare
9342 PS : constant Entity_Id := Parent_Subtype (Rectype);
9344 begin
9345 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9346 Tagged_Parent := PS;
9348 -- Find maximum bit of any component of the parent type
9350 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9351 Pcomp := First_Entity (Tagged_Parent);
9352 while Present (Pcomp) loop
9353 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9354 if Component_Bit_Offset (Pcomp) /= No_Uint
9355 and then Known_Static_Esize (Pcomp)
9356 then
9357 Parent_Last_Bit :=
9358 UI_Max
9359 (Parent_Last_Bit,
9360 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9361 end if;
9363 Next_Entity (Pcomp);
9364 end if;
9365 end loop;
9366 end if;
9367 end;
9369 -- All done if no component clauses
9371 CC := First (Component_Clauses (N));
9373 if No (CC) then
9374 return;
9375 end if;
9377 -- If a tag is present, then create a component clause that places it
9378 -- at the start of the record (otherwise gigi may place it after other
9379 -- fields that have rep clauses).
9381 Fent := First_Entity (Rectype);
9383 if Nkind (Fent) = N_Defining_Identifier
9384 and then Chars (Fent) = Name_uTag
9385 then
9386 Set_Component_Bit_Offset (Fent, Uint_0);
9387 Set_Normalized_Position (Fent, Uint_0);
9388 Set_Normalized_First_Bit (Fent, Uint_0);
9389 Set_Normalized_Position_Max (Fent, Uint_0);
9390 Init_Esize (Fent, System_Address_Size);
9392 Set_Component_Clause (Fent,
9393 Make_Component_Clause (Loc,
9394 Component_Name => Make_Identifier (Loc, Name_uTag),
9396 Position => Make_Integer_Literal (Loc, Uint_0),
9397 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9398 Last_Bit =>
9399 Make_Integer_Literal (Loc,
9400 UI_From_Int (System_Address_Size))));
9402 Ccount := Ccount + 1;
9403 end if;
9405 Max_Bit_So_Far := Uint_Minus_1;
9406 Overlap_Check_Required := False;
9408 -- Process the component clauses
9410 while Present (CC) loop
9411 Find_Component;
9413 if Present (Comp) then
9414 Ccount := Ccount + 1;
9416 -- We need a full overlap check if record positions non-monotonic
9418 if Fbit <= Max_Bit_So_Far then
9419 Overlap_Check_Required := True;
9420 end if;
9422 Max_Bit_So_Far := Lbit;
9424 -- Check bit position out of range of specified size
9426 if Has_Size_Clause (Rectype)
9427 and then RM_Size (Rectype) <= Lbit
9428 then
9429 Error_Msg_N
9430 ("bit number out of range of specified size",
9431 Last_Bit (CC));
9433 -- Check for overlap with tag component
9435 else
9436 if Is_Tagged_Type (Rectype)
9437 and then Fbit < System_Address_Size
9438 then
9439 Error_Msg_NE
9440 ("component overlaps tag field of&",
9441 Component_Name (CC), Rectype);
9442 Overlap_Detected := True;
9443 end if;
9445 if Hbit < Lbit then
9446 Hbit := Lbit;
9447 end if;
9448 end if;
9450 -- Check parent overlap if component might overlap parent field
9452 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9453 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9454 while Present (Pcomp) loop
9455 if not Is_Tag (Pcomp)
9456 and then Chars (Pcomp) /= Name_uParent
9457 then
9458 Check_Component_Overlap (Comp, Pcomp);
9459 end if;
9461 Next_Component_Or_Discriminant (Pcomp);
9462 end loop;
9463 end if;
9464 end if;
9466 Next (CC);
9467 end loop;
9469 -- Now that we have processed all the component clauses, check for
9470 -- overlap. We have to leave this till last, since the components can
9471 -- appear in any arbitrary order in the representation clause.
9473 -- We do not need this check if all specified ranges were monotonic,
9474 -- as recorded by Overlap_Check_Required being False at this stage.
9476 -- This first section checks if there are any overlapping entries at
9477 -- all. It does this by sorting all entries and then seeing if there are
9478 -- any overlaps. If there are none, then that is decisive, but if there
9479 -- are overlaps, they may still be OK (they may result from fields in
9480 -- different variants).
9482 if Overlap_Check_Required then
9483 Overlap_Check1 : declare
9485 OC_Fbit : array (0 .. Ccount) of Uint;
9486 -- First-bit values for component clauses, the value is the offset
9487 -- of the first bit of the field from start of record. The zero
9488 -- entry is for use in sorting.
9490 OC_Lbit : array (0 .. Ccount) of Uint;
9491 -- Last-bit values for component clauses, the value is the offset
9492 -- of the last bit of the field from start of record. The zero
9493 -- entry is for use in sorting.
9495 OC_Count : Natural := 0;
9496 -- Count of entries in OC_Fbit and OC_Lbit
9498 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9499 -- Compare routine for Sort
9501 procedure OC_Move (From : Natural; To : Natural);
9502 -- Move routine for Sort
9504 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9506 -----------
9507 -- OC_Lt --
9508 -----------
9510 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9511 begin
9512 return OC_Fbit (Op1) < OC_Fbit (Op2);
9513 end OC_Lt;
9515 -------------
9516 -- OC_Move --
9517 -------------
9519 procedure OC_Move (From : Natural; To : Natural) is
9520 begin
9521 OC_Fbit (To) := OC_Fbit (From);
9522 OC_Lbit (To) := OC_Lbit (From);
9523 end OC_Move;
9525 -- Start of processing for Overlap_Check
9527 begin
9528 CC := First (Component_Clauses (N));
9529 while Present (CC) loop
9531 -- Exclude component clause already marked in error
9533 if not Error_Posted (CC) then
9534 Find_Component;
9536 if Present (Comp) then
9537 OC_Count := OC_Count + 1;
9538 OC_Fbit (OC_Count) := Fbit;
9539 OC_Lbit (OC_Count) := Lbit;
9540 end if;
9541 end if;
9543 Next (CC);
9544 end loop;
9546 Sorting.Sort (OC_Count);
9548 Overlap_Check_Required := False;
9549 for J in 1 .. OC_Count - 1 loop
9550 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9551 Overlap_Check_Required := True;
9552 exit;
9553 end if;
9554 end loop;
9555 end Overlap_Check1;
9556 end if;
9558 -- If Overlap_Check_Required is still True, then we have to do the full
9559 -- scale overlap check, since we have at least two fields that do
9560 -- overlap, and we need to know if that is OK since they are in
9561 -- different variant, or whether we have a definite problem.
9563 if Overlap_Check_Required then
9564 Overlap_Check2 : declare
9565 C1_Ent, C2_Ent : Entity_Id;
9566 -- Entities of components being checked for overlap
9568 Clist : Node_Id;
9569 -- Component_List node whose Component_Items are being checked
9571 Citem : Node_Id;
9572 -- Component declaration for component being checked
9574 begin
9575 C1_Ent := First_Entity (Base_Type (Rectype));
9577 -- Loop through all components in record. For each component check
9578 -- for overlap with any of the preceding elements on the component
9579 -- list containing the component and also, if the component is in
9580 -- a variant, check against components outside the case structure.
9581 -- This latter test is repeated recursively up the variant tree.
9583 Main_Component_Loop : while Present (C1_Ent) loop
9584 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9585 goto Continue_Main_Component_Loop;
9586 end if;
9588 -- Skip overlap check if entity has no declaration node. This
9589 -- happens with discriminants in constrained derived types.
9590 -- Possibly we are missing some checks as a result, but that
9591 -- does not seem terribly serious.
9593 if No (Declaration_Node (C1_Ent)) then
9594 goto Continue_Main_Component_Loop;
9595 end if;
9597 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9599 -- Loop through component lists that need checking. Check the
9600 -- current component list and all lists in variants above us.
9602 Component_List_Loop : loop
9604 -- If derived type definition, go to full declaration
9605 -- If at outer level, check discriminants if there are any.
9607 if Nkind (Clist) = N_Derived_Type_Definition then
9608 Clist := Parent (Clist);
9609 end if;
9611 -- Outer level of record definition, check discriminants
9613 if Nkind_In (Clist, N_Full_Type_Declaration,
9614 N_Private_Type_Declaration)
9615 then
9616 if Has_Discriminants (Defining_Identifier (Clist)) then
9617 C2_Ent :=
9618 First_Discriminant (Defining_Identifier (Clist));
9619 while Present (C2_Ent) loop
9620 exit when C1_Ent = C2_Ent;
9621 Check_Component_Overlap (C1_Ent, C2_Ent);
9622 Next_Discriminant (C2_Ent);
9623 end loop;
9624 end if;
9626 -- Record extension case
9628 elsif Nkind (Clist) = N_Derived_Type_Definition then
9629 Clist := Empty;
9631 -- Otherwise check one component list
9633 else
9634 Citem := First (Component_Items (Clist));
9635 while Present (Citem) loop
9636 if Nkind (Citem) = N_Component_Declaration then
9637 C2_Ent := Defining_Identifier (Citem);
9638 exit when C1_Ent = C2_Ent;
9639 Check_Component_Overlap (C1_Ent, C2_Ent);
9640 end if;
9642 Next (Citem);
9643 end loop;
9644 end if;
9646 -- Check for variants above us (the parent of the Clist can
9647 -- be a variant, in which case its parent is a variant part,
9648 -- and the parent of the variant part is a component list
9649 -- whose components must all be checked against the current
9650 -- component for overlap).
9652 if Nkind (Parent (Clist)) = N_Variant then
9653 Clist := Parent (Parent (Parent (Clist)));
9655 -- Check for possible discriminant part in record, this
9656 -- is treated essentially as another level in the
9657 -- recursion. For this case the parent of the component
9658 -- list is the record definition, and its parent is the
9659 -- full type declaration containing the discriminant
9660 -- specifications.
9662 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9663 Clist := Parent (Parent ((Clist)));
9665 -- If neither of these two cases, we are at the top of
9666 -- the tree.
9668 else
9669 exit Component_List_Loop;
9670 end if;
9671 end loop Component_List_Loop;
9673 <<Continue_Main_Component_Loop>>
9674 Next_Entity (C1_Ent);
9676 end loop Main_Component_Loop;
9677 end Overlap_Check2;
9678 end if;
9680 -- The following circuit deals with warning on record holes (gaps). We
9681 -- skip this check if overlap was detected, since it makes sense for the
9682 -- programmer to fix this illegality before worrying about warnings.
9684 if not Overlap_Detected and Warn_On_Record_Holes then
9685 Record_Hole_Check : declare
9686 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9687 -- Full declaration of record type
9689 procedure Check_Component_List
9690 (CL : Node_Id;
9691 Sbit : Uint;
9692 DS : List_Id);
9693 -- Check component list CL for holes. The starting bit should be
9694 -- Sbit. which is zero for the main record component list and set
9695 -- appropriately for recursive calls for variants. DS is set to
9696 -- a list of discriminant specifications to be included in the
9697 -- consideration of components. It is No_List if none to consider.
9699 --------------------------
9700 -- Check_Component_List --
9701 --------------------------
9703 procedure Check_Component_List
9704 (CL : Node_Id;
9705 Sbit : Uint;
9706 DS : List_Id)
9708 Compl : Integer;
9710 begin
9711 Compl := Integer (List_Length (Component_Items (CL)));
9713 if DS /= No_List then
9714 Compl := Compl + Integer (List_Length (DS));
9715 end if;
9717 declare
9718 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9719 -- Gather components (zero entry is for sort routine)
9721 Ncomps : Natural := 0;
9722 -- Number of entries stored in Comps (starting at Comps (1))
9724 Citem : Node_Id;
9725 -- One component item or discriminant specification
9727 Nbit : Uint;
9728 -- Starting bit for next component
9730 CEnt : Entity_Id;
9731 -- Component entity
9733 Variant : Node_Id;
9734 -- One variant
9736 function Lt (Op1, Op2 : Natural) return Boolean;
9737 -- Compare routine for Sort
9739 procedure Move (From : Natural; To : Natural);
9740 -- Move routine for Sort
9742 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9744 --------
9745 -- Lt --
9746 --------
9748 function Lt (Op1, Op2 : Natural) return Boolean is
9749 begin
9750 return Component_Bit_Offset (Comps (Op1))
9752 Component_Bit_Offset (Comps (Op2));
9753 end Lt;
9755 ----------
9756 -- Move --
9757 ----------
9759 procedure Move (From : Natural; To : Natural) is
9760 begin
9761 Comps (To) := Comps (From);
9762 end Move;
9764 begin
9765 -- Gather discriminants into Comp
9767 if DS /= No_List then
9768 Citem := First (DS);
9769 while Present (Citem) loop
9770 if Nkind (Citem) = N_Discriminant_Specification then
9771 declare
9772 Ent : constant Entity_Id :=
9773 Defining_Identifier (Citem);
9774 begin
9775 if Ekind (Ent) = E_Discriminant then
9776 Ncomps := Ncomps + 1;
9777 Comps (Ncomps) := Ent;
9778 end if;
9779 end;
9780 end if;
9782 Next (Citem);
9783 end loop;
9784 end if;
9786 -- Gather component entities into Comp
9788 Citem := First (Component_Items (CL));
9789 while Present (Citem) loop
9790 if Nkind (Citem) = N_Component_Declaration then
9791 Ncomps := Ncomps + 1;
9792 Comps (Ncomps) := Defining_Identifier (Citem);
9793 end if;
9795 Next (Citem);
9796 end loop;
9798 -- Now sort the component entities based on the first bit.
9799 -- Note we already know there are no overlapping components.
9801 Sorting.Sort (Ncomps);
9803 -- Loop through entries checking for holes
9805 Nbit := Sbit;
9806 for J in 1 .. Ncomps loop
9807 CEnt := Comps (J);
9808 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9810 if Error_Msg_Uint_1 > 0 then
9811 Error_Msg_NE
9812 ("?H?^-bit gap before component&",
9813 Component_Name (Component_Clause (CEnt)), CEnt);
9814 end if;
9816 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9817 end loop;
9819 -- Process variant parts recursively if present
9821 if Present (Variant_Part (CL)) then
9822 Variant := First (Variants (Variant_Part (CL)));
9823 while Present (Variant) loop
9824 Check_Component_List
9825 (Component_List (Variant), Nbit, No_List);
9826 Next (Variant);
9827 end loop;
9828 end if;
9829 end;
9830 end Check_Component_List;
9832 -- Start of processing for Record_Hole_Check
9834 begin
9835 declare
9836 Sbit : Uint;
9838 begin
9839 if Is_Tagged_Type (Rectype) then
9840 Sbit := UI_From_Int (System_Address_Size);
9841 else
9842 Sbit := Uint_0;
9843 end if;
9845 if Nkind (Decl) = N_Full_Type_Declaration
9846 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9847 then
9848 Check_Component_List
9849 (Component_List (Type_Definition (Decl)),
9850 Sbit,
9851 Discriminant_Specifications (Decl));
9852 end if;
9853 end;
9854 end Record_Hole_Check;
9855 end if;
9857 -- For records that have component clauses for all components, and whose
9858 -- size is less than or equal to 32, we need to know the size in the
9859 -- front end to activate possible packed array processing where the
9860 -- component type is a record.
9862 -- At this stage Hbit + 1 represents the first unused bit from all the
9863 -- component clauses processed, so if the component clauses are
9864 -- complete, then this is the length of the record.
9866 -- For records longer than System.Storage_Unit, and for those where not
9867 -- all components have component clauses, the back end determines the
9868 -- length (it may for example be appropriate to round up the size
9869 -- to some convenient boundary, based on alignment considerations, etc).
9871 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
9873 -- Nothing to do if at least one component has no component clause
9875 Comp := First_Component_Or_Discriminant (Rectype);
9876 while Present (Comp) loop
9877 exit when No (Component_Clause (Comp));
9878 Next_Component_Or_Discriminant (Comp);
9879 end loop;
9881 -- If we fall out of loop, all components have component clauses
9882 -- and so we can set the size to the maximum value.
9884 if No (Comp) then
9885 Set_RM_Size (Rectype, Hbit + 1);
9886 end if;
9887 end if;
9888 end Check_Record_Representation_Clause;
9890 ----------------
9891 -- Check_Size --
9892 ----------------
9894 procedure Check_Size
9895 (N : Node_Id;
9896 T : Entity_Id;
9897 Siz : Uint;
9898 Biased : out Boolean)
9900 UT : constant Entity_Id := Underlying_Type (T);
9901 M : Uint;
9903 begin
9904 Biased := False;
9906 -- Reject patently improper size values.
9908 if Is_Elementary_Type (T)
9909 and then Siz > UI_From_Int (Int'Last)
9910 then
9911 Error_Msg_N ("Size value too large for elementary type", N);
9913 if Nkind (Original_Node (N)) = N_Op_Expon then
9914 Error_Msg_N
9915 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
9916 end if;
9917 end if;
9919 -- Dismiss generic types
9921 if Is_Generic_Type (T)
9922 or else
9923 Is_Generic_Type (UT)
9924 or else
9925 Is_Generic_Type (Root_Type (UT))
9926 then
9927 return;
9929 -- Guard against previous errors
9931 elsif No (UT) or else UT = Any_Type then
9932 Check_Error_Detected;
9933 return;
9935 -- Check case of bit packed array
9937 elsif Is_Array_Type (UT)
9938 and then Known_Static_Component_Size (UT)
9939 and then Is_Bit_Packed_Array (UT)
9940 then
9941 declare
9942 Asiz : Uint;
9943 Indx : Node_Id;
9944 Ityp : Entity_Id;
9946 begin
9947 Asiz := Component_Size (UT);
9948 Indx := First_Index (UT);
9949 loop
9950 Ityp := Etype (Indx);
9952 -- If non-static bound, then we are not in the business of
9953 -- trying to check the length, and indeed an error will be
9954 -- issued elsewhere, since sizes of non-static array types
9955 -- cannot be set implicitly or explicitly.
9957 if not Is_OK_Static_Subtype (Ityp) then
9958 return;
9959 end if;
9961 -- Otherwise accumulate next dimension
9963 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
9964 Expr_Value (Type_Low_Bound (Ityp)) +
9965 Uint_1);
9967 Next_Index (Indx);
9968 exit when No (Indx);
9969 end loop;
9971 if Asiz <= Siz then
9972 return;
9974 else
9975 Error_Msg_Uint_1 := Asiz;
9976 Error_Msg_NE
9977 ("size for& too small, minimum allowed is ^", N, T);
9978 Set_Esize (T, Asiz);
9979 Set_RM_Size (T, Asiz);
9980 end if;
9981 end;
9983 -- All other composite types are ignored
9985 elsif Is_Composite_Type (UT) then
9986 return;
9988 -- For fixed-point types, don't check minimum if type is not frozen,
9989 -- since we don't know all the characteristics of the type that can
9990 -- affect the size (e.g. a specified small) till freeze time.
9992 elsif Is_Fixed_Point_Type (UT)
9993 and then not Is_Frozen (UT)
9994 then
9995 null;
9997 -- Cases for which a minimum check is required
9999 else
10000 -- Ignore if specified size is correct for the type
10002 if Known_Esize (UT) and then Siz = Esize (UT) then
10003 return;
10004 end if;
10006 -- Otherwise get minimum size
10008 M := UI_From_Int (Minimum_Size (UT));
10010 if Siz < M then
10012 -- Size is less than minimum size, but one possibility remains
10013 -- that we can manage with the new size if we bias the type.
10015 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10017 if Siz < M then
10018 Error_Msg_Uint_1 := M;
10019 Error_Msg_NE
10020 ("size for& too small, minimum allowed is ^", N, T);
10021 Set_Esize (T, M);
10022 Set_RM_Size (T, M);
10023 else
10024 Biased := True;
10025 end if;
10026 end if;
10027 end if;
10028 end Check_Size;
10030 --------------------------
10031 -- Freeze_Entity_Checks --
10032 --------------------------
10034 procedure Freeze_Entity_Checks (N : Node_Id) is
10035 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10036 -- Inspect the primitive operations of type Typ and hide all pairs of
10037 -- implicitly declared non-overridden non-fully conformant homographs
10038 -- (Ada RM 8.3 12.3/2).
10040 -------------------------------------
10041 -- Hide_Non_Overridden_Subprograms --
10042 -------------------------------------
10044 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10045 procedure Hide_Matching_Homographs
10046 (Subp_Id : Entity_Id;
10047 Start_Elmt : Elmt_Id);
10048 -- Inspect a list of primitive operations starting with Start_Elmt
10049 -- and find matching implicitly declared non-overridden non-fully
10050 -- conformant homographs of Subp_Id. If found, all matches along
10051 -- with Subp_Id are hidden from all visibility.
10053 function Is_Non_Overridden_Or_Null_Procedure
10054 (Subp_Id : Entity_Id) return Boolean;
10055 -- Determine whether subprogram Subp_Id is implicitly declared non-
10056 -- overridden subprogram or an implicitly declared null procedure.
10058 ------------------------------
10059 -- Hide_Matching_Homographs --
10060 ------------------------------
10062 procedure Hide_Matching_Homographs
10063 (Subp_Id : Entity_Id;
10064 Start_Elmt : Elmt_Id)
10066 Prim : Entity_Id;
10067 Prim_Elmt : Elmt_Id;
10069 begin
10070 Prim_Elmt := Start_Elmt;
10071 while Present (Prim_Elmt) loop
10072 Prim := Node (Prim_Elmt);
10074 -- The current primitive is implicitly declared non-overridden
10075 -- non-fully conformant homograph of Subp_Id. Both subprograms
10076 -- must be hidden from visibility.
10078 if Chars (Prim) = Chars (Subp_Id)
10079 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10080 and then not Fully_Conformant (Prim, Subp_Id)
10081 then
10082 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10083 Set_Is_Immediately_Visible (Prim, False);
10084 Set_Is_Potentially_Use_Visible (Prim, False);
10086 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10087 Set_Is_Immediately_Visible (Subp_Id, False);
10088 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10089 end if;
10091 Next_Elmt (Prim_Elmt);
10092 end loop;
10093 end Hide_Matching_Homographs;
10095 -----------------------------------------
10096 -- Is_Non_Overridden_Or_Null_Procedure --
10097 -----------------------------------------
10099 function Is_Non_Overridden_Or_Null_Procedure
10100 (Subp_Id : Entity_Id) return Boolean
10102 Alias_Id : Entity_Id;
10104 begin
10105 -- The subprogram is inherited (implicitly declared), it does not
10106 -- override and does not cover a primitive of an interface.
10108 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10109 and then Present (Alias (Subp_Id))
10110 and then No (Interface_Alias (Subp_Id))
10111 and then No (Overridden_Operation (Subp_Id))
10112 then
10113 Alias_Id := Alias (Subp_Id);
10115 if Requires_Overriding (Alias_Id) then
10116 return True;
10118 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10119 and then Null_Present (Parent (Alias_Id))
10120 then
10121 return True;
10122 end if;
10123 end if;
10125 return False;
10126 end Is_Non_Overridden_Or_Null_Procedure;
10128 -- Local variables
10130 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10131 Prim : Entity_Id;
10132 Prim_Elmt : Elmt_Id;
10134 -- Start of processing for Hide_Non_Overridden_Subprograms
10136 begin
10137 -- Inspect the list of primitives looking for non-overridden
10138 -- subprograms.
10140 if Present (Prim_Ops) then
10141 Prim_Elmt := First_Elmt (Prim_Ops);
10142 while Present (Prim_Elmt) loop
10143 Prim := Node (Prim_Elmt);
10144 Next_Elmt (Prim_Elmt);
10146 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10147 Hide_Matching_Homographs
10148 (Subp_Id => Prim,
10149 Start_Elmt => Prim_Elmt);
10150 end if;
10151 end loop;
10152 end if;
10153 end Hide_Non_Overridden_Subprograms;
10155 ---------------------
10156 -- Local variables --
10157 ---------------------
10159 E : constant Entity_Id := Entity (N);
10161 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10162 -- True in non-generic case. Some of the processing here is skipped
10163 -- for the generic case since it is not needed. Basically in the
10164 -- generic case, we only need to do stuff that might generate error
10165 -- messages or warnings.
10167 -- Start of processing for Freeze_Entity_Checks
10169 begin
10170 -- Remember that we are processing a freezing entity. Required to
10171 -- ensure correct decoration of internal entities associated with
10172 -- interfaces (see New_Overloaded_Entity).
10174 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10176 -- For tagged types covering interfaces add internal entities that link
10177 -- the primitives of the interfaces with the primitives that cover them.
10178 -- Note: These entities were originally generated only when generating
10179 -- code because their main purpose was to provide support to initialize
10180 -- the secondary dispatch tables. They are now generated also when
10181 -- compiling with no code generation to provide ASIS the relationship
10182 -- between interface primitives and tagged type primitives. They are
10183 -- also used to locate primitives covering interfaces when processing
10184 -- generics (see Derive_Subprograms).
10186 -- This is not needed in the generic case
10188 if Ada_Version >= Ada_2005
10189 and then Non_Generic_Case
10190 and then Ekind (E) = E_Record_Type
10191 and then Is_Tagged_Type (E)
10192 and then not Is_Interface (E)
10193 and then Has_Interfaces (E)
10194 then
10195 -- This would be a good common place to call the routine that checks
10196 -- overriding of interface primitives (and thus factorize calls to
10197 -- Check_Abstract_Overriding located at different contexts in the
10198 -- compiler). However, this is not possible because it causes
10199 -- spurious errors in case of late overriding.
10201 Add_Internal_Interface_Entities (E);
10202 end if;
10204 -- After all forms of overriding have been resolved, a tagged type may
10205 -- be left with a set of implicitly declared and possibly erroneous
10206 -- abstract subprograms, null procedures and subprograms that require
10207 -- overriding. If this set contains fully conformat homographs, then one
10208 -- is chosen arbitrarily (already done during resolution), otherwise all
10209 -- remaining non-fully conformant homographs are hidden from visibility
10210 -- (Ada RM 8.3 12.3/2).
10212 if Is_Tagged_Type (E) then
10213 Hide_Non_Overridden_Subprograms (E);
10214 end if;
10216 -- Check CPP types
10218 if Ekind (E) = E_Record_Type
10219 and then Is_CPP_Class (E)
10220 and then Is_Tagged_Type (E)
10221 and then Tagged_Type_Expansion
10222 then
10223 if CPP_Num_Prims (E) = 0 then
10225 -- If the CPP type has user defined components then it must import
10226 -- primitives from C++. This is required because if the C++ class
10227 -- has no primitives then the C++ compiler does not added the _tag
10228 -- component to the type.
10230 if First_Entity (E) /= Last_Entity (E) then
10231 Error_Msg_N
10232 ("'C'P'P type must import at least one primitive from C++??",
10234 end if;
10235 end if;
10237 -- Check that all its primitives are abstract or imported from C++.
10238 -- Check also availability of the C++ constructor.
10240 declare
10241 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10242 Elmt : Elmt_Id;
10243 Error_Reported : Boolean := False;
10244 Prim : Node_Id;
10246 begin
10247 Elmt := First_Elmt (Primitive_Operations (E));
10248 while Present (Elmt) loop
10249 Prim := Node (Elmt);
10251 if Comes_From_Source (Prim) then
10252 if Is_Abstract_Subprogram (Prim) then
10253 null;
10255 elsif not Is_Imported (Prim)
10256 or else Convention (Prim) /= Convention_CPP
10257 then
10258 Error_Msg_N
10259 ("primitives of 'C'P'P types must be imported from C++ "
10260 & "or abstract??", Prim);
10262 elsif not Has_Constructors
10263 and then not Error_Reported
10264 then
10265 Error_Msg_Name_1 := Chars (E);
10266 Error_Msg_N
10267 ("??'C'P'P constructor required for type %", Prim);
10268 Error_Reported := True;
10269 end if;
10270 end if;
10272 Next_Elmt (Elmt);
10273 end loop;
10274 end;
10275 end if;
10277 -- Check Ada derivation of CPP type
10279 if Expander_Active -- why? losing errors in -gnatc mode???
10280 and then Tagged_Type_Expansion
10281 and then Ekind (E) = E_Record_Type
10282 and then Etype (E) /= E
10283 and then Is_CPP_Class (Etype (E))
10284 and then CPP_Num_Prims (Etype (E)) > 0
10285 and then not Is_CPP_Class (E)
10286 and then not Has_CPP_Constructors (Etype (E))
10287 then
10288 -- If the parent has C++ primitives but it has no constructor then
10289 -- check that all the primitives are overridden in this derivation;
10290 -- otherwise the constructor of the parent is needed to build the
10291 -- dispatch table.
10293 declare
10294 Elmt : Elmt_Id;
10295 Prim : Node_Id;
10297 begin
10298 Elmt := First_Elmt (Primitive_Operations (E));
10299 while Present (Elmt) loop
10300 Prim := Node (Elmt);
10302 if not Is_Abstract_Subprogram (Prim)
10303 and then No (Interface_Alias (Prim))
10304 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10305 then
10306 Error_Msg_Name_1 := Chars (Etype (E));
10307 Error_Msg_N
10308 ("'C'P'P constructor required for parent type %", E);
10309 exit;
10310 end if;
10312 Next_Elmt (Elmt);
10313 end loop;
10314 end;
10315 end if;
10317 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10319 -- If we have a type with predicates, build predicate function. This
10320 -- is not needed in the generic case, and is not needed within TSS
10321 -- subprograms and other predefined primitives.
10323 if Non_Generic_Case
10324 and then Is_Type (E)
10325 and then Has_Predicates (E)
10326 and then not Within_Internal_Subprogram
10327 then
10328 Build_Predicate_Functions (E, N);
10329 end if;
10331 -- If type has delayed aspects, this is where we do the preanalysis at
10332 -- the freeze point, as part of the consistent visibility check. Note
10333 -- that this must be done after calling Build_Predicate_Functions or
10334 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10335 -- the subtype name in the saved expression so that they will not cause
10336 -- trouble in the preanalysis.
10338 -- This is also not needed in the generic case
10340 if Non_Generic_Case
10341 and then Has_Delayed_Aspects (E)
10342 and then Scope (E) = Current_Scope
10343 then
10344 -- Retrieve the visibility to the discriminants in order to properly
10345 -- analyze the aspects.
10347 Push_Scope_And_Install_Discriminants (E);
10349 declare
10350 Ritem : Node_Id;
10352 begin
10353 -- Look for aspect specification entries for this entity
10355 Ritem := First_Rep_Item (E);
10356 while Present (Ritem) loop
10357 if Nkind (Ritem) = N_Aspect_Specification
10358 and then Entity (Ritem) = E
10359 and then Is_Delayed_Aspect (Ritem)
10360 then
10361 Check_Aspect_At_Freeze_Point (Ritem);
10362 end if;
10364 Next_Rep_Item (Ritem);
10365 end loop;
10366 end;
10368 Uninstall_Discriminants_And_Pop_Scope (E);
10369 end if;
10371 -- For a record type, deal with variant parts. This has to be delayed
10372 -- to this point, because of the issue of statically predicated
10373 -- subtypes, which we have to ensure are frozen before checking
10374 -- choices, since we need to have the static choice list set.
10376 if Is_Record_Type (E) then
10377 Check_Variant_Part : declare
10378 D : constant Node_Id := Declaration_Node (E);
10379 T : Node_Id;
10380 C : Node_Id;
10381 VP : Node_Id;
10383 Others_Present : Boolean;
10384 pragma Warnings (Off, Others_Present);
10385 -- Indicates others present, not used in this case
10387 procedure Non_Static_Choice_Error (Choice : Node_Id);
10388 -- Error routine invoked by the generic instantiation below when
10389 -- the variant part has a non static choice.
10391 procedure Process_Declarations (Variant : Node_Id);
10392 -- Processes declarations associated with a variant. We analyzed
10393 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10394 -- but we still need the recursive call to Check_Choices for any
10395 -- nested variant to get its choices properly processed. This is
10396 -- also where we expand out the choices if expansion is active.
10398 package Variant_Choices_Processing is new
10399 Generic_Check_Choices
10400 (Process_Empty_Choice => No_OP,
10401 Process_Non_Static_Choice => Non_Static_Choice_Error,
10402 Process_Associated_Node => Process_Declarations);
10403 use Variant_Choices_Processing;
10405 -----------------------------
10406 -- Non_Static_Choice_Error --
10407 -----------------------------
10409 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10410 begin
10411 Flag_Non_Static_Expr
10412 ("choice given in variant part is not static!", Choice);
10413 end Non_Static_Choice_Error;
10415 --------------------------
10416 -- Process_Declarations --
10417 --------------------------
10419 procedure Process_Declarations (Variant : Node_Id) is
10420 CL : constant Node_Id := Component_List (Variant);
10421 VP : Node_Id;
10423 begin
10424 -- Check for static predicate present in this variant
10426 if Has_SP_Choice (Variant) then
10428 -- Here we expand. You might expect to find this call in
10429 -- Expand_N_Variant_Part, but that is called when we first
10430 -- see the variant part, and we cannot do this expansion
10431 -- earlier than the freeze point, since for statically
10432 -- predicated subtypes, the predicate is not known till
10433 -- the freeze point.
10435 -- Furthermore, we do this expansion even if the expander
10436 -- is not active, because other semantic processing, e.g.
10437 -- for aggregates, requires the expanded list of choices.
10439 -- If the expander is not active, then we can't just clobber
10440 -- the list since it would invalidate the ASIS -gnatct tree.
10441 -- So we have to rewrite the variant part with a Rewrite
10442 -- call that replaces it with a copy and clobber the copy.
10444 if not Expander_Active then
10445 declare
10446 NewV : constant Node_Id := New_Copy (Variant);
10447 begin
10448 Set_Discrete_Choices
10449 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10450 Rewrite (Variant, NewV);
10451 end;
10452 end if;
10454 Expand_Static_Predicates_In_Choices (Variant);
10455 end if;
10457 -- We don't need to worry about the declarations in the variant
10458 -- (since they were analyzed by Analyze_Choices when we first
10459 -- encountered the variant), but we do need to take care of
10460 -- expansion of any nested variants.
10462 if not Null_Present (CL) then
10463 VP := Variant_Part (CL);
10465 if Present (VP) then
10466 Check_Choices
10467 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10468 end if;
10469 end if;
10470 end Process_Declarations;
10472 -- Start of processing for Check_Variant_Part
10474 begin
10475 -- Find component list
10477 C := Empty;
10479 if Nkind (D) = N_Full_Type_Declaration then
10480 T := Type_Definition (D);
10482 if Nkind (T) = N_Record_Definition then
10483 C := Component_List (T);
10485 elsif Nkind (T) = N_Derived_Type_Definition
10486 and then Present (Record_Extension_Part (T))
10487 then
10488 C := Component_List (Record_Extension_Part (T));
10489 end if;
10490 end if;
10492 -- Case of variant part present
10494 if Present (C) and then Present (Variant_Part (C)) then
10495 VP := Variant_Part (C);
10497 -- Check choices
10499 Check_Choices
10500 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10502 -- If the last variant does not contain the Others choice,
10503 -- replace it with an N_Others_Choice node since Gigi always
10504 -- wants an Others. Note that we do not bother to call Analyze
10505 -- on the modified variant part, since its only effect would be
10506 -- to compute the Others_Discrete_Choices node laboriously, and
10507 -- of course we already know the list of choices corresponding
10508 -- to the others choice (it's the list we're replacing).
10510 -- We only want to do this if the expander is active, since
10511 -- we do not want to clobber the ASIS tree.
10513 if Expander_Active then
10514 declare
10515 Last_Var : constant Node_Id :=
10516 Last_Non_Pragma (Variants (VP));
10518 Others_Node : Node_Id;
10520 begin
10521 if Nkind (First (Discrete_Choices (Last_Var))) /=
10522 N_Others_Choice
10523 then
10524 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10525 Set_Others_Discrete_Choices
10526 (Others_Node, Discrete_Choices (Last_Var));
10527 Set_Discrete_Choices
10528 (Last_Var, New_List (Others_Node));
10529 end if;
10530 end;
10531 end if;
10532 end if;
10533 end Check_Variant_Part;
10534 end if;
10535 end Freeze_Entity_Checks;
10537 -------------------------
10538 -- Get_Alignment_Value --
10539 -------------------------
10541 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10542 Align : constant Uint := Static_Integer (Expr);
10544 begin
10545 if Align = No_Uint then
10546 return No_Uint;
10548 elsif Align <= 0 then
10549 Error_Msg_N ("alignment value must be positive", Expr);
10550 return No_Uint;
10552 else
10553 for J in Int range 0 .. 64 loop
10554 declare
10555 M : constant Uint := Uint_2 ** J;
10557 begin
10558 exit when M = Align;
10560 if M > Align then
10561 Error_Msg_N
10562 ("alignment value must be power of 2", Expr);
10563 return No_Uint;
10564 end if;
10565 end;
10566 end loop;
10568 return Align;
10569 end if;
10570 end Get_Alignment_Value;
10572 -------------------------------------
10573 -- Inherit_Aspects_At_Freeze_Point --
10574 -------------------------------------
10576 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10577 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10578 (Rep_Item : Node_Id) return Boolean;
10579 -- This routine checks if Rep_Item is either a pragma or an aspect
10580 -- specification node whose correponding pragma (if any) is present in
10581 -- the Rep Item chain of the entity it has been specified to.
10583 --------------------------------------------------
10584 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10585 --------------------------------------------------
10587 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10588 (Rep_Item : Node_Id) return Boolean
10590 begin
10591 return
10592 Nkind (Rep_Item) = N_Pragma
10593 or else Present_In_Rep_Item
10594 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10595 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10597 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10599 begin
10600 -- A representation item is either subtype-specific (Size and Alignment
10601 -- clauses) or type-related (all others). Subtype-specific aspects may
10602 -- differ for different subtypes of the same type (RM 13.1.8).
10604 -- A derived type inherits each type-related representation aspect of
10605 -- its parent type that was directly specified before the declaration of
10606 -- the derived type (RM 13.1.15).
10608 -- A derived subtype inherits each subtype-specific representation
10609 -- aspect of its parent subtype that was directly specified before the
10610 -- declaration of the derived type (RM 13.1.15).
10612 -- The general processing involves inheriting a representation aspect
10613 -- from a parent type whenever the first rep item (aspect specification,
10614 -- attribute definition clause, pragma) corresponding to the given
10615 -- representation aspect in the rep item chain of Typ, if any, isn't
10616 -- directly specified to Typ but to one of its parents.
10618 -- ??? Note that, for now, just a limited number of representation
10619 -- aspects have been inherited here so far. Many of them are
10620 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
10621 -- a non- exhaustive list of aspects that likely also need to
10622 -- be moved to this routine: Alignment, Component_Alignment,
10623 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
10624 -- Preelaborable_Initialization, RM_Size and Small.
10626 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
10627 return;
10628 end if;
10630 -- Ada_05/Ada_2005
10632 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
10633 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
10634 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10635 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
10636 then
10637 Set_Is_Ada_2005_Only (Typ);
10638 end if;
10640 -- Ada_12/Ada_2012
10642 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
10643 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
10644 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10645 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
10646 then
10647 Set_Is_Ada_2012_Only (Typ);
10648 end if;
10650 -- Atomic/Shared
10652 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
10653 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
10654 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10655 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
10656 then
10657 Set_Is_Atomic (Typ);
10658 Set_Treat_As_Volatile (Typ);
10659 Set_Is_Volatile (Typ);
10660 end if;
10662 -- Default_Component_Value
10664 if Is_Array_Type (Typ)
10665 and then Is_Base_Type (Typ)
10666 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
10667 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
10668 then
10669 Set_Default_Aspect_Component_Value (Typ,
10670 Default_Aspect_Component_Value
10671 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
10672 end if;
10674 -- Default_Value
10676 if Is_Scalar_Type (Typ)
10677 and then Is_Base_Type (Typ)
10678 and then Has_Rep_Item (Typ, Name_Default_Value, False)
10679 and then Has_Rep_Item (Typ, Name_Default_Value)
10680 then
10681 Set_Default_Aspect_Value (Typ,
10682 Default_Aspect_Value
10683 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
10684 end if;
10686 -- Discard_Names
10688 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
10689 and then Has_Rep_Item (Typ, Name_Discard_Names)
10690 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10691 (Get_Rep_Item (Typ, Name_Discard_Names))
10692 then
10693 Set_Discard_Names (Typ);
10694 end if;
10696 -- Invariants
10698 if not Has_Rep_Item (Typ, Name_Invariant, False)
10699 and then Has_Rep_Item (Typ, Name_Invariant)
10700 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10701 (Get_Rep_Item (Typ, Name_Invariant))
10702 then
10703 Set_Has_Invariants (Typ);
10705 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
10706 Set_Has_Inheritable_Invariants (Typ);
10707 end if;
10708 end if;
10710 -- Volatile
10712 if not Has_Rep_Item (Typ, Name_Volatile, False)
10713 and then Has_Rep_Item (Typ, Name_Volatile)
10714 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10715 (Get_Rep_Item (Typ, Name_Volatile))
10716 then
10717 Set_Treat_As_Volatile (Typ);
10718 Set_Is_Volatile (Typ);
10719 end if;
10721 -- Inheritance for derived types only
10723 if Is_Derived_Type (Typ) then
10724 declare
10725 Bas_Typ : constant Entity_Id := Base_Type (Typ);
10726 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
10728 begin
10729 -- Atomic_Components
10731 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
10732 and then Has_Rep_Item (Typ, Name_Atomic_Components)
10733 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10734 (Get_Rep_Item (Typ, Name_Atomic_Components))
10735 then
10736 Set_Has_Atomic_Components (Imp_Bas_Typ);
10737 end if;
10739 -- Volatile_Components
10741 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
10742 and then Has_Rep_Item (Typ, Name_Volatile_Components)
10743 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10744 (Get_Rep_Item (Typ, Name_Volatile_Components))
10745 then
10746 Set_Has_Volatile_Components (Imp_Bas_Typ);
10747 end if;
10749 -- Finalize_Storage_Only.
10751 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
10752 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
10753 then
10754 Set_Finalize_Storage_Only (Bas_Typ);
10755 end if;
10757 -- Universal_Aliasing
10759 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
10760 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
10761 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10762 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
10763 then
10764 Set_Universal_Aliasing (Imp_Bas_Typ);
10765 end if;
10767 -- Record type specific aspects
10769 if Is_Record_Type (Typ) then
10771 -- Bit_Order
10773 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
10774 and then Has_Rep_Item (Typ, Name_Bit_Order)
10775 then
10776 Set_Reverse_Bit_Order (Bas_Typ,
10777 Reverse_Bit_Order (Entity (Name
10778 (Get_Rep_Item (Typ, Name_Bit_Order)))));
10779 end if;
10781 -- Scalar_Storage_Order
10783 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
10784 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
10785 then
10786 Set_Reverse_Storage_Order (Bas_Typ,
10787 Reverse_Storage_Order (Entity (Name
10788 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
10790 -- Clear default SSO indications, since the inherited aspect
10791 -- which was set explicitly overrides the default.
10793 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
10794 Set_SSO_Set_High_By_Default (Bas_Typ, False);
10795 end if;
10796 end if;
10797 end;
10798 end if;
10799 end Inherit_Aspects_At_Freeze_Point;
10801 ----------------
10802 -- Initialize --
10803 ----------------
10805 procedure Initialize is
10806 begin
10807 Address_Clause_Checks.Init;
10808 Independence_Checks.Init;
10809 Unchecked_Conversions.Init;
10810 end Initialize;
10812 ---------------------------
10813 -- Install_Discriminants --
10814 ---------------------------
10816 procedure Install_Discriminants (E : Entity_Id) is
10817 Disc : Entity_Id;
10818 Prev : Entity_Id;
10819 begin
10820 Disc := First_Discriminant (E);
10821 while Present (Disc) loop
10822 Prev := Current_Entity (Disc);
10823 Set_Current_Entity (Disc);
10824 Set_Is_Immediately_Visible (Disc);
10825 Set_Homonym (Disc, Prev);
10826 Next_Discriminant (Disc);
10827 end loop;
10828 end Install_Discriminants;
10830 -------------------------
10831 -- Is_Operational_Item --
10832 -------------------------
10834 function Is_Operational_Item (N : Node_Id) return Boolean is
10835 begin
10836 if Nkind (N) /= N_Attribute_Definition_Clause then
10837 return False;
10839 else
10840 declare
10841 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
10842 begin
10843 return Id = Attribute_Input
10844 or else Id = Attribute_Output
10845 or else Id = Attribute_Read
10846 or else Id = Attribute_Write
10847 or else Id = Attribute_External_Tag;
10848 end;
10849 end if;
10850 end Is_Operational_Item;
10852 -------------------------
10853 -- Is_Predicate_Static --
10854 -------------------------
10856 -- Note: the basic legality of the expression has already been checked, so
10857 -- we don't need to worry about cases or ranges on strings for example.
10859 function Is_Predicate_Static
10860 (Expr : Node_Id;
10861 Nam : Name_Id) return Boolean
10863 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
10864 -- Given a list of case expression alternatives, returns True if all
10865 -- the alternatives are static (have all static choices, and a static
10866 -- expression).
10868 function All_Static_Choices (L : List_Id) return Boolean;
10869 -- Returns true if all elements of the list are OK static choices
10870 -- as defined below for Is_Static_Choice. Used for case expression
10871 -- alternatives and for the right operand of a membership test. An
10872 -- others_choice is static if the corresponding expression is static.
10873 -- The staticness of the bounds is checked separately.
10875 function Is_Static_Choice (N : Node_Id) return Boolean;
10876 -- Returns True if N represents a static choice (static subtype, or
10877 -- static subtype indication, or static expression, or static range).
10879 -- Note that this is a bit more inclusive than we actually need
10880 -- (in particular membership tests do not allow the use of subtype
10881 -- indications). But that doesn't matter, we have already checked
10882 -- that the construct is legal to get this far.
10884 function Is_Type_Ref (N : Node_Id) return Boolean;
10885 pragma Inline (Is_Type_Ref);
10886 -- Returns True if N is a reference to the type for the predicate in the
10887 -- expression (i.e. if it is an identifier whose Chars field matches the
10888 -- Nam given in the call). N must not be parenthesized, if the type name
10889 -- appears in parens, this routine will return False.
10891 ----------------------------------
10892 -- All_Static_Case_Alternatives --
10893 ----------------------------------
10895 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
10896 N : Node_Id;
10898 begin
10899 N := First (L);
10900 while Present (N) loop
10901 if not (All_Static_Choices (Discrete_Choices (N))
10902 and then Is_OK_Static_Expression (Expression (N)))
10903 then
10904 return False;
10905 end if;
10907 Next (N);
10908 end loop;
10910 return True;
10911 end All_Static_Case_Alternatives;
10913 ------------------------
10914 -- All_Static_Choices --
10915 ------------------------
10917 function All_Static_Choices (L : List_Id) return Boolean is
10918 N : Node_Id;
10920 begin
10921 N := First (L);
10922 while Present (N) loop
10923 if not Is_Static_Choice (N) then
10924 return False;
10925 end if;
10927 Next (N);
10928 end loop;
10930 return True;
10931 end All_Static_Choices;
10933 ----------------------
10934 -- Is_Static_Choice --
10935 ----------------------
10937 function Is_Static_Choice (N : Node_Id) return Boolean is
10938 begin
10939 return Nkind (N) = N_Others_Choice
10940 or else Is_OK_Static_Expression (N)
10941 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
10942 and then Is_OK_Static_Subtype (Entity (N)))
10943 or else (Nkind (N) = N_Subtype_Indication
10944 and then Is_OK_Static_Subtype (Entity (N)))
10945 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
10946 end Is_Static_Choice;
10948 -----------------
10949 -- Is_Type_Ref --
10950 -----------------
10952 function Is_Type_Ref (N : Node_Id) return Boolean is
10953 begin
10954 return Nkind (N) = N_Identifier
10955 and then Chars (N) = Nam
10956 and then Paren_Count (N) = 0;
10957 end Is_Type_Ref;
10959 -- Start of processing for Is_Predicate_Static
10961 begin
10962 -- Predicate_Static means one of the following holds. Numbers are the
10963 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
10965 -- 16: A static expression
10967 if Is_OK_Static_Expression (Expr) then
10968 return True;
10970 -- 17: A membership test whose simple_expression is the current
10971 -- instance, and whose membership_choice_list meets the requirements
10972 -- for a static membership test.
10974 elsif Nkind (Expr) in N_Membership_Test
10975 and then ((Present (Right_Opnd (Expr))
10976 and then Is_Static_Choice (Right_Opnd (Expr)))
10977 or else
10978 (Present (Alternatives (Expr))
10979 and then All_Static_Choices (Alternatives (Expr))))
10980 then
10981 return True;
10983 -- 18. A case_expression whose selecting_expression is the current
10984 -- instance, and whose dependent expressions are static expressions.
10986 elsif Nkind (Expr) = N_Case_Expression
10987 and then Is_Type_Ref (Expression (Expr))
10988 and then All_Static_Case_Alternatives (Alternatives (Expr))
10989 then
10990 return True;
10992 -- 19. A call to a predefined equality or ordering operator, where one
10993 -- operand is the current instance, and the other is a static
10994 -- expression.
10996 -- Note: the RM is clearly wrong here in not excluding string types.
10997 -- Without this exclusion, we would allow expressions like X > "ABC"
10998 -- to be considered as predicate-static, which is clearly not intended,
10999 -- since the idea is for predicate-static to be a subset of normal
11000 -- static expressions (and "DEF" > "ABC" is not a static expression).
11002 -- However, we do allow internally generated (not from source) equality
11003 -- and inequality operations to be valid on strings (this helps deal
11004 -- with cases where we transform A in "ABC" to A = "ABC).
11006 elsif Nkind (Expr) in N_Op_Compare
11007 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11008 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11009 and then not Comes_From_Source (Expr)))
11010 and then ((Is_Type_Ref (Left_Opnd (Expr))
11011 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11012 or else
11013 (Is_Type_Ref (Right_Opnd (Expr))
11014 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11015 then
11016 return True;
11018 -- 20. A call to a predefined boolean logical operator, where each
11019 -- operand is predicate-static.
11021 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11022 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11023 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11024 or else
11025 (Nkind (Expr) = N_Op_Not
11026 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11027 then
11028 return True;
11030 -- 21. A short-circuit control form where both operands are
11031 -- predicate-static.
11033 elsif Nkind (Expr) in N_Short_Circuit
11034 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11035 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11036 then
11037 return True;
11039 -- 22. A parenthesized predicate-static expression. This does not
11040 -- require any special test, since we just ignore paren levels in
11041 -- all the cases above.
11043 -- One more test that is an implementation artifact caused by the fact
11044 -- that we are analyzing not the original expression, but the generated
11045 -- expression in the body of the predicate function. This can include
11046 -- references to inherited predicates, so that the expression we are
11047 -- processing looks like:
11049 -- expression and then xxPredicate (typ (Inns))
11051 -- Where the call is to a Predicate function for an inherited predicate.
11052 -- We simply ignore such a call (which could be to either a dynamic or
11053 -- a static predicate, but remember that we can have a Static_Predicate
11054 -- for a non-static subtype).
11056 elsif Nkind (Expr) = N_Function_Call
11057 and then Is_Predicate_Function (Entity (Name (Expr)))
11058 then
11059 return True;
11061 -- That's an exhaustive list of tests, all other cases are not
11062 -- predicate-static, so we return False.
11064 else
11065 return False;
11066 end if;
11067 end Is_Predicate_Static;
11069 ---------------------
11070 -- Kill_Rep_Clause --
11071 ---------------------
11073 procedure Kill_Rep_Clause (N : Node_Id) is
11074 begin
11075 pragma Assert (Ignore_Rep_Clauses);
11077 -- Note: we use Replace rather than Rewrite, because we don't want
11078 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11079 -- rep clause that is being replaced.
11081 Replace (N, Make_Null_Statement (Sloc (N)));
11083 -- The null statement must be marked as not coming from source. This is
11084 -- so that ASIS ignores it, and also the back end does not expect bogus
11085 -- "from source" null statements in weird places (e.g. in declarative
11086 -- regions where such null statements are not allowed).
11088 Set_Comes_From_Source (N, False);
11089 end Kill_Rep_Clause;
11091 ------------------
11092 -- Minimum_Size --
11093 ------------------
11095 function Minimum_Size
11096 (T : Entity_Id;
11097 Biased : Boolean := False) return Nat
11099 Lo : Uint := No_Uint;
11100 Hi : Uint := No_Uint;
11101 LoR : Ureal := No_Ureal;
11102 HiR : Ureal := No_Ureal;
11103 LoSet : Boolean := False;
11104 HiSet : Boolean := False;
11105 B : Uint;
11106 S : Nat;
11107 Ancest : Entity_Id;
11108 R_Typ : constant Entity_Id := Root_Type (T);
11110 begin
11111 -- If bad type, return 0
11113 if T = Any_Type then
11114 return 0;
11116 -- For generic types, just return zero. There cannot be any legitimate
11117 -- need to know such a size, but this routine may be called with a
11118 -- generic type as part of normal processing.
11120 elsif Is_Generic_Type (R_Typ)
11121 or else R_Typ = Any_Type
11122 then
11123 return 0;
11125 -- Access types (cannot have size smaller than System.Address)
11127 elsif Is_Access_Type (T) then
11128 return System_Address_Size;
11130 -- Floating-point types
11132 elsif Is_Floating_Point_Type (T) then
11133 return UI_To_Int (Esize (R_Typ));
11135 -- Discrete types
11137 elsif Is_Discrete_Type (T) then
11139 -- The following loop is looking for the nearest compile time known
11140 -- bounds following the ancestor subtype chain. The idea is to find
11141 -- the most restrictive known bounds information.
11143 Ancest := T;
11144 loop
11145 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11146 return 0;
11147 end if;
11149 if not LoSet then
11150 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11151 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11152 LoSet := True;
11153 exit when HiSet;
11154 end if;
11155 end if;
11157 if not HiSet then
11158 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11159 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11160 HiSet := True;
11161 exit when LoSet;
11162 end if;
11163 end if;
11165 Ancest := Ancestor_Subtype (Ancest);
11167 if No (Ancest) then
11168 Ancest := Base_Type (T);
11170 if Is_Generic_Type (Ancest) then
11171 return 0;
11172 end if;
11173 end if;
11174 end loop;
11176 -- Fixed-point types. We can't simply use Expr_Value to get the
11177 -- Corresponding_Integer_Value values of the bounds, since these do not
11178 -- get set till the type is frozen, and this routine can be called
11179 -- before the type is frozen. Similarly the test for bounds being static
11180 -- needs to include the case where we have unanalyzed real literals for
11181 -- the same reason.
11183 elsif Is_Fixed_Point_Type (T) then
11185 -- The following loop is looking for the nearest compile time known
11186 -- bounds following the ancestor subtype chain. The idea is to find
11187 -- the most restrictive known bounds information.
11189 Ancest := T;
11190 loop
11191 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11192 return 0;
11193 end if;
11195 -- Note: In the following two tests for LoSet and HiSet, it may
11196 -- seem redundant to test for N_Real_Literal here since normally
11197 -- one would assume that the test for the value being known at
11198 -- compile time includes this case. However, there is a glitch.
11199 -- If the real literal comes from folding a non-static expression,
11200 -- then we don't consider any non- static expression to be known
11201 -- at compile time if we are in configurable run time mode (needed
11202 -- in some cases to give a clearer definition of what is and what
11203 -- is not accepted). So the test is indeed needed. Without it, we
11204 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11206 if not LoSet then
11207 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11208 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11209 then
11210 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11211 LoSet := True;
11212 exit when HiSet;
11213 end if;
11214 end if;
11216 if not HiSet then
11217 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11218 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11219 then
11220 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11221 HiSet := True;
11222 exit when LoSet;
11223 end if;
11224 end if;
11226 Ancest := Ancestor_Subtype (Ancest);
11228 if No (Ancest) then
11229 Ancest := Base_Type (T);
11231 if Is_Generic_Type (Ancest) then
11232 return 0;
11233 end if;
11234 end if;
11235 end loop;
11237 Lo := UR_To_Uint (LoR / Small_Value (T));
11238 Hi := UR_To_Uint (HiR / Small_Value (T));
11240 -- No other types allowed
11242 else
11243 raise Program_Error;
11244 end if;
11246 -- Fall through with Hi and Lo set. Deal with biased case
11248 if (Biased
11249 and then not Is_Fixed_Point_Type (T)
11250 and then not (Is_Enumeration_Type (T)
11251 and then Has_Non_Standard_Rep (T)))
11252 or else Has_Biased_Representation (T)
11253 then
11254 Hi := Hi - Lo;
11255 Lo := Uint_0;
11256 end if;
11258 -- Signed case. Note that we consider types like range 1 .. -1 to be
11259 -- signed for the purpose of computing the size, since the bounds have
11260 -- to be accommodated in the base type.
11262 if Lo < 0 or else Hi < 0 then
11263 S := 1;
11264 B := Uint_1;
11266 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11267 -- Note that we accommodate the case where the bounds cross. This
11268 -- can happen either because of the way the bounds are declared
11269 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11271 while Lo < -B
11272 or else Hi < -B
11273 or else Lo >= B
11274 or else Hi >= B
11275 loop
11276 B := Uint_2 ** S;
11277 S := S + 1;
11278 end loop;
11280 -- Unsigned case
11282 else
11283 -- If both bounds are positive, make sure that both are represen-
11284 -- table in the case where the bounds are crossed. This can happen
11285 -- either because of the way the bounds are declared, or because of
11286 -- the algorithm in Freeze_Fixed_Point_Type.
11288 if Lo > Hi then
11289 Hi := Lo;
11290 end if;
11292 -- S = size, (can accommodate 0 .. (2**size - 1))
11294 S := 0;
11295 while Hi >= Uint_2 ** S loop
11296 S := S + 1;
11297 end loop;
11298 end if;
11300 return S;
11301 end Minimum_Size;
11303 ---------------------------
11304 -- New_Stream_Subprogram --
11305 ---------------------------
11307 procedure New_Stream_Subprogram
11308 (N : Node_Id;
11309 Ent : Entity_Id;
11310 Subp : Entity_Id;
11311 Nam : TSS_Name_Type)
11313 Loc : constant Source_Ptr := Sloc (N);
11314 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11315 Subp_Id : Entity_Id;
11316 Subp_Decl : Node_Id;
11317 F : Entity_Id;
11318 Etyp : Entity_Id;
11320 Defer_Declaration : constant Boolean :=
11321 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11322 -- For a tagged type, there is a declaration for each stream attribute
11323 -- at the freeze point, and we must generate only a completion of this
11324 -- declaration. We do the same for private types, because the full view
11325 -- might be tagged. Otherwise we generate a declaration at the point of
11326 -- the attribute definition clause.
11328 function Build_Spec return Node_Id;
11329 -- Used for declaration and renaming declaration, so that this is
11330 -- treated as a renaming_as_body.
11332 ----------------
11333 -- Build_Spec --
11334 ----------------
11336 function Build_Spec return Node_Id is
11337 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11338 Formals : List_Id;
11339 Spec : Node_Id;
11340 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11342 begin
11343 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11345 -- S : access Root_Stream_Type'Class
11347 Formals := New_List (
11348 Make_Parameter_Specification (Loc,
11349 Defining_Identifier =>
11350 Make_Defining_Identifier (Loc, Name_S),
11351 Parameter_Type =>
11352 Make_Access_Definition (Loc,
11353 Subtype_Mark =>
11354 New_Occurrence_Of (
11355 Designated_Type (Etype (F)), Loc))));
11357 if Nam = TSS_Stream_Input then
11358 Spec :=
11359 Make_Function_Specification (Loc,
11360 Defining_Unit_Name => Subp_Id,
11361 Parameter_Specifications => Formals,
11362 Result_Definition => T_Ref);
11363 else
11364 -- V : [out] T
11366 Append_To (Formals,
11367 Make_Parameter_Specification (Loc,
11368 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11369 Out_Present => Out_P,
11370 Parameter_Type => T_Ref));
11372 Spec :=
11373 Make_Procedure_Specification (Loc,
11374 Defining_Unit_Name => Subp_Id,
11375 Parameter_Specifications => Formals);
11376 end if;
11378 return Spec;
11379 end Build_Spec;
11381 -- Start of processing for New_Stream_Subprogram
11383 begin
11384 F := First_Formal (Subp);
11386 if Ekind (Subp) = E_Procedure then
11387 Etyp := Etype (Next_Formal (F));
11388 else
11389 Etyp := Etype (Subp);
11390 end if;
11392 -- Prepare subprogram declaration and insert it as an action on the
11393 -- clause node. The visibility for this entity is used to test for
11394 -- visibility of the attribute definition clause (in the sense of
11395 -- 8.3(23) as amended by AI-195).
11397 if not Defer_Declaration then
11398 Subp_Decl :=
11399 Make_Subprogram_Declaration (Loc,
11400 Specification => Build_Spec);
11402 -- For a tagged type, there is always a visible declaration for each
11403 -- stream TSS (it is a predefined primitive operation), and the
11404 -- completion of this declaration occurs at the freeze point, which is
11405 -- not always visible at places where the attribute definition clause is
11406 -- visible. So, we create a dummy entity here for the purpose of
11407 -- tracking the visibility of the attribute definition clause itself.
11409 else
11410 Subp_Id :=
11411 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11412 Subp_Decl :=
11413 Make_Object_Declaration (Loc,
11414 Defining_Identifier => Subp_Id,
11415 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11416 end if;
11418 Insert_Action (N, Subp_Decl);
11419 Set_Entity (N, Subp_Id);
11421 Subp_Decl :=
11422 Make_Subprogram_Renaming_Declaration (Loc,
11423 Specification => Build_Spec,
11424 Name => New_Occurrence_Of (Subp, Loc));
11426 if Defer_Declaration then
11427 Set_TSS (Base_Type (Ent), Subp_Id);
11428 else
11429 Insert_Action (N, Subp_Decl);
11430 Copy_TSS (Subp_Id, Base_Type (Ent));
11431 end if;
11432 end New_Stream_Subprogram;
11434 ------------------------------------------
11435 -- Push_Scope_And_Install_Discriminants --
11436 ------------------------------------------
11438 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11439 begin
11440 if Has_Discriminants (E) then
11441 Push_Scope (E);
11443 -- Make discriminants visible for type declarations and protected
11444 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11446 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11447 Install_Discriminants (E);
11448 end if;
11449 end if;
11450 end Push_Scope_And_Install_Discriminants;
11452 ------------------------
11453 -- Rep_Item_Too_Early --
11454 ------------------------
11456 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
11457 begin
11458 -- Cannot apply non-operational rep items to generic types
11460 if Is_Operational_Item (N) then
11461 return False;
11463 elsif Is_Type (T)
11464 and then Is_Generic_Type (Root_Type (T))
11465 then
11466 Error_Msg_N ("representation item not allowed for generic type", N);
11467 return True;
11468 end if;
11470 -- Otherwise check for incomplete type
11472 if Is_Incomplete_Or_Private_Type (T)
11473 and then No (Underlying_Type (T))
11474 and then
11475 (Nkind (N) /= N_Pragma
11476 or else Get_Pragma_Id (N) /= Pragma_Import)
11477 then
11478 Error_Msg_N
11479 ("representation item must be after full type declaration", N);
11480 return True;
11482 -- If the type has incomplete components, a representation clause is
11483 -- illegal but stream attributes and Convention pragmas are correct.
11485 elsif Has_Private_Component (T) then
11486 if Nkind (N) = N_Pragma then
11487 return False;
11489 else
11490 Error_Msg_N
11491 ("representation item must appear after type is fully defined",
11493 return True;
11494 end if;
11495 else
11496 return False;
11497 end if;
11498 end Rep_Item_Too_Early;
11500 -----------------------
11501 -- Rep_Item_Too_Late --
11502 -----------------------
11504 function Rep_Item_Too_Late
11505 (T : Entity_Id;
11506 N : Node_Id;
11507 FOnly : Boolean := False) return Boolean
11509 S : Entity_Id;
11510 Parent_Type : Entity_Id;
11512 procedure No_Type_Rep_Item;
11513 -- Output message indicating that no type-related aspects can be
11514 -- specified due to some property of the parent type.
11516 procedure Too_Late;
11517 -- Output message for an aspect being specified too late
11519 -- Note that neither of the above errors is considered a serious one,
11520 -- since the effect is simply that we ignore the representation clause
11521 -- in these cases.
11522 -- Is this really true? In any case if we make this change we must
11523 -- document the requirement in the spec of Rep_Item_Too_Late that
11524 -- if True is returned, then the rep item must be completely ignored???
11526 ----------------------
11527 -- No_Type_Rep_Item --
11528 ----------------------
11530 procedure No_Type_Rep_Item is
11531 begin
11532 Error_Msg_N ("|type-related representation item not permitted!", N);
11533 end No_Type_Rep_Item;
11535 --------------
11536 -- Too_Late --
11537 --------------
11539 procedure Too_Late is
11540 begin
11541 -- Other compilers seem more relaxed about rep items appearing too
11542 -- late. Since analysis tools typically don't care about rep items
11543 -- anyway, no reason to be too strict about this.
11545 if not Relaxed_RM_Semantics then
11546 Error_Msg_N ("|representation item appears too late!", N);
11547 end if;
11548 end Too_Late;
11550 -- Start of processing for Rep_Item_Too_Late
11552 begin
11553 -- First make sure entity is not frozen (RM 13.1(9))
11555 if Is_Frozen (T)
11557 -- Exclude imported types, which may be frozen if they appear in a
11558 -- representation clause for a local type.
11560 and then not From_Limited_With (T)
11562 -- Exclude generated entities (not coming from source). The common
11563 -- case is when we generate a renaming which prematurely freezes the
11564 -- renamed internal entity, but we still want to be able to set copies
11565 -- of attribute values such as Size/Alignment.
11567 and then Comes_From_Source (T)
11568 then
11569 Too_Late;
11570 S := First_Subtype (T);
11572 if Present (Freeze_Node (S)) then
11573 if not Relaxed_RM_Semantics then
11574 Error_Msg_NE
11575 ("??no more representation items for }", Freeze_Node (S), S);
11576 end if;
11577 end if;
11579 return True;
11581 -- Check for case of untagged derived type whose parent either has
11582 -- primitive operations, or is a by reference type (RM 13.1(10)). In
11583 -- this case we do not output a Too_Late message, since there is no
11584 -- earlier point where the rep item could be placed to make it legal.
11586 elsif Is_Type (T)
11587 and then not FOnly
11588 and then Is_Derived_Type (T)
11589 and then not Is_Tagged_Type (T)
11590 then
11591 Parent_Type := Etype (Base_Type (T));
11593 if Has_Primitive_Operations (Parent_Type) then
11594 No_Type_Rep_Item;
11596 if not Relaxed_RM_Semantics then
11597 Error_Msg_NE
11598 ("\parent type & has primitive operations!", N, Parent_Type);
11599 end if;
11601 return True;
11603 elsif Is_By_Reference_Type (Parent_Type) then
11604 No_Type_Rep_Item;
11606 if not Relaxed_RM_Semantics then
11607 Error_Msg_NE
11608 ("\parent type & is a by reference type!", N, Parent_Type);
11609 end if;
11611 return True;
11612 end if;
11613 end if;
11615 -- No error, but one more warning to consider. The RM (surprisingly)
11616 -- allows this pattern:
11618 -- type S is ...
11619 -- primitive operations for S
11620 -- type R is new S;
11621 -- rep clause for S
11623 -- Meaning that calls on the primitive operations of S for values of
11624 -- type R may require possibly expensive implicit conversion operations.
11625 -- This is not an error, but is worth a warning.
11627 if not Relaxed_RM_Semantics and then Is_Type (T) then
11628 declare
11629 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
11631 begin
11632 if Present (DTL)
11633 and then Has_Primitive_Operations (Base_Type (T))
11635 -- For now, do not generate this warning for the case of aspect
11636 -- specification using Ada 2012 syntax, since we get wrong
11637 -- messages we do not understand. The whole business of derived
11638 -- types and rep items seems a bit confused when aspects are
11639 -- used, since the aspects are not evaluated till freeze time.
11641 and then not From_Aspect_Specification (N)
11642 then
11643 Error_Msg_Sloc := Sloc (DTL);
11644 Error_Msg_N
11645 ("representation item for& appears after derived type "
11646 & "declaration#??", N);
11647 Error_Msg_NE
11648 ("\may result in implicit conversions for primitive "
11649 & "operations of&??", N, T);
11650 Error_Msg_NE
11651 ("\to change representations when called with arguments "
11652 & "of type&??", N, DTL);
11653 end if;
11654 end;
11655 end if;
11657 -- No error, link item into head of chain of rep items for the entity,
11658 -- but avoid chaining if we have an overloadable entity, and the pragma
11659 -- is one that can apply to multiple overloaded entities.
11661 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
11662 declare
11663 Pname : constant Name_Id := Pragma_Name (N);
11664 begin
11665 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
11666 Name_External, Name_Interface)
11667 then
11668 return False;
11669 end if;
11670 end;
11671 end if;
11673 Record_Rep_Item (T, N);
11674 return False;
11675 end Rep_Item_Too_Late;
11677 -------------------------------------
11678 -- Replace_Type_References_Generic --
11679 -------------------------------------
11681 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
11682 TName : constant Name_Id := Chars (T);
11684 function Replace_Node (N : Node_Id) return Traverse_Result;
11685 -- Processes a single node in the traversal procedure below, checking
11686 -- if node N should be replaced, and if so, doing the replacement.
11688 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
11689 -- This instantiation provides the body of Replace_Type_References
11691 ------------------
11692 -- Replace_Node --
11693 ------------------
11695 function Replace_Node (N : Node_Id) return Traverse_Result is
11696 S : Entity_Id;
11697 P : Node_Id;
11699 begin
11700 -- Case of identifier
11702 if Nkind (N) = N_Identifier then
11704 -- If not the type name, check whether it is a reference to
11705 -- some other type, which must be frozen before the predicate
11706 -- function is analyzed, i.e. before the freeze node of the
11707 -- type to which the predicate applies.
11709 if Chars (N) /= TName then
11710 if Present (Current_Entity (N))
11711 and then Is_Type (Current_Entity (N))
11712 then
11713 Freeze_Before (Freeze_Node (T), Current_Entity (N));
11714 end if;
11716 return Skip;
11718 -- Otherwise do the replacement and we are done with this node
11720 else
11721 Replace_Type_Reference (N);
11722 return Skip;
11723 end if;
11725 -- Case of selected component (which is what a qualification
11726 -- looks like in the unanalyzed tree, which is what we have.
11728 elsif Nkind (N) = N_Selected_Component then
11730 -- If selector name is not our type, keeping going (we might
11731 -- still have an occurrence of the type in the prefix).
11733 if Nkind (Selector_Name (N)) /= N_Identifier
11734 or else Chars (Selector_Name (N)) /= TName
11735 then
11736 return OK;
11738 -- Selector name is our type, check qualification
11740 else
11741 -- Loop through scopes and prefixes, doing comparison
11743 S := Current_Scope;
11744 P := Prefix (N);
11745 loop
11746 -- Continue if no more scopes or scope with no name
11748 if No (S) or else Nkind (S) not in N_Has_Chars then
11749 return OK;
11750 end if;
11752 -- Do replace if prefix is an identifier matching the
11753 -- scope that we are currently looking at.
11755 if Nkind (P) = N_Identifier
11756 and then Chars (P) = Chars (S)
11757 then
11758 Replace_Type_Reference (N);
11759 return Skip;
11760 end if;
11762 -- Go check scope above us if prefix is itself of the
11763 -- form of a selected component, whose selector matches
11764 -- the scope we are currently looking at.
11766 if Nkind (P) = N_Selected_Component
11767 and then Nkind (Selector_Name (P)) = N_Identifier
11768 and then Chars (Selector_Name (P)) = Chars (S)
11769 then
11770 S := Scope (S);
11771 P := Prefix (P);
11773 -- For anything else, we don't have a match, so keep on
11774 -- going, there are still some weird cases where we may
11775 -- still have a replacement within the prefix.
11777 else
11778 return OK;
11779 end if;
11780 end loop;
11781 end if;
11783 -- Continue for any other node kind
11785 else
11786 return OK;
11787 end if;
11788 end Replace_Node;
11790 begin
11791 Replace_Type_Refs (N);
11792 end Replace_Type_References_Generic;
11794 -------------------------
11795 -- Same_Representation --
11796 -------------------------
11798 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
11799 T1 : constant Entity_Id := Underlying_Type (Typ1);
11800 T2 : constant Entity_Id := Underlying_Type (Typ2);
11802 begin
11803 -- A quick check, if base types are the same, then we definitely have
11804 -- the same representation, because the subtype specific representation
11805 -- attributes (Size and Alignment) do not affect representation from
11806 -- the point of view of this test.
11808 if Base_Type (T1) = Base_Type (T2) then
11809 return True;
11811 elsif Is_Private_Type (Base_Type (T2))
11812 and then Base_Type (T1) = Full_View (Base_Type (T2))
11813 then
11814 return True;
11815 end if;
11817 -- Tagged types never have differing representations
11819 if Is_Tagged_Type (T1) then
11820 return True;
11821 end if;
11823 -- Representations are definitely different if conventions differ
11825 if Convention (T1) /= Convention (T2) then
11826 return False;
11827 end if;
11829 -- Representations are different if component alignments or scalar
11830 -- storage orders differ.
11832 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
11833 and then
11834 (Is_Record_Type (T2) or else Is_Array_Type (T2))
11835 and then
11836 (Component_Alignment (T1) /= Component_Alignment (T2)
11837 or else
11838 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
11839 then
11840 return False;
11841 end if;
11843 -- For arrays, the only real issue is component size. If we know the
11844 -- component size for both arrays, and it is the same, then that's
11845 -- good enough to know we don't have a change of representation.
11847 if Is_Array_Type (T1) then
11848 if Known_Component_Size (T1)
11849 and then Known_Component_Size (T2)
11850 and then Component_Size (T1) = Component_Size (T2)
11851 then
11852 if VM_Target = No_VM then
11853 return True;
11855 -- In VM targets the representation of arrays with aliased
11856 -- components differs from arrays with non-aliased components
11858 else
11859 return Has_Aliased_Components (Base_Type (T1))
11861 Has_Aliased_Components (Base_Type (T2));
11862 end if;
11863 end if;
11864 end if;
11866 -- Types definitely have same representation if neither has non-standard
11867 -- representation since default representations are always consistent.
11868 -- If only one has non-standard representation, and the other does not,
11869 -- then we consider that they do not have the same representation. They
11870 -- might, but there is no way of telling early enough.
11872 if Has_Non_Standard_Rep (T1) then
11873 if not Has_Non_Standard_Rep (T2) then
11874 return False;
11875 end if;
11876 else
11877 return not Has_Non_Standard_Rep (T2);
11878 end if;
11880 -- Here the two types both have non-standard representation, and we need
11881 -- to determine if they have the same non-standard representation.
11883 -- For arrays, we simply need to test if the component sizes are the
11884 -- same. Pragma Pack is reflected in modified component sizes, so this
11885 -- check also deals with pragma Pack.
11887 if Is_Array_Type (T1) then
11888 return Component_Size (T1) = Component_Size (T2);
11890 -- Tagged types always have the same representation, because it is not
11891 -- possible to specify different representations for common fields.
11893 elsif Is_Tagged_Type (T1) then
11894 return True;
11896 -- Case of record types
11898 elsif Is_Record_Type (T1) then
11900 -- Packed status must conform
11902 if Is_Packed (T1) /= Is_Packed (T2) then
11903 return False;
11905 -- Otherwise we must check components. Typ2 maybe a constrained
11906 -- subtype with fewer components, so we compare the components
11907 -- of the base types.
11909 else
11910 Record_Case : declare
11911 CD1, CD2 : Entity_Id;
11913 function Same_Rep return Boolean;
11914 -- CD1 and CD2 are either components or discriminants. This
11915 -- function tests whether they have the same representation.
11917 --------------
11918 -- Same_Rep --
11919 --------------
11921 function Same_Rep return Boolean is
11922 begin
11923 if No (Component_Clause (CD1)) then
11924 return No (Component_Clause (CD2));
11925 else
11926 -- Note: at this point, component clauses have been
11927 -- normalized to the default bit order, so that the
11928 -- comparison of Component_Bit_Offsets is meaningful.
11930 return
11931 Present (Component_Clause (CD2))
11932 and then
11933 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
11934 and then
11935 Esize (CD1) = Esize (CD2);
11936 end if;
11937 end Same_Rep;
11939 -- Start of processing for Record_Case
11941 begin
11942 if Has_Discriminants (T1) then
11944 -- The number of discriminants may be different if the
11945 -- derived type has fewer (constrained by values). The
11946 -- invisible discriminants retain the representation of
11947 -- the original, so the discrepancy does not per se
11948 -- indicate a different representation.
11950 CD1 := First_Discriminant (T1);
11951 CD2 := First_Discriminant (T2);
11952 while Present (CD1) and then Present (CD2) loop
11953 if not Same_Rep then
11954 return False;
11955 else
11956 Next_Discriminant (CD1);
11957 Next_Discriminant (CD2);
11958 end if;
11959 end loop;
11960 end if;
11962 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
11963 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
11964 while Present (CD1) loop
11965 if not Same_Rep then
11966 return False;
11967 else
11968 Next_Component (CD1);
11969 Next_Component (CD2);
11970 end if;
11971 end loop;
11973 return True;
11974 end Record_Case;
11975 end if;
11977 -- For enumeration types, we must check each literal to see if the
11978 -- representation is the same. Note that we do not permit enumeration
11979 -- representation clauses for Character and Wide_Character, so these
11980 -- cases were already dealt with.
11982 elsif Is_Enumeration_Type (T1) then
11983 Enumeration_Case : declare
11984 L1, L2 : Entity_Id;
11986 begin
11987 L1 := First_Literal (T1);
11988 L2 := First_Literal (T2);
11989 while Present (L1) loop
11990 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
11991 return False;
11992 else
11993 Next_Literal (L1);
11994 Next_Literal (L2);
11995 end if;
11996 end loop;
11998 return True;
11999 end Enumeration_Case;
12001 -- Any other types have the same representation for these purposes
12003 else
12004 return True;
12005 end if;
12006 end Same_Representation;
12008 --------------------------------
12009 -- Resolve_Iterable_Operation --
12010 --------------------------------
12012 procedure Resolve_Iterable_Operation
12013 (N : Node_Id;
12014 Cursor : Entity_Id;
12015 Typ : Entity_Id;
12016 Nam : Name_Id)
12018 Ent : Entity_Id;
12019 F1 : Entity_Id;
12020 F2 : Entity_Id;
12022 begin
12023 if not Is_Overloaded (N) then
12024 if not Is_Entity_Name (N)
12025 or else Ekind (Entity (N)) /= E_Function
12026 or else Scope (Entity (N)) /= Scope (Typ)
12027 or else No (First_Formal (Entity (N)))
12028 or else Etype (First_Formal (Entity (N))) /= Typ
12029 then
12030 Error_Msg_N ("iterable primitive must be local function name "
12031 & "whose first formal is an iterable type", N);
12032 return;
12033 end if;
12035 Ent := Entity (N);
12036 F1 := First_Formal (Ent);
12037 if Nam = Name_First then
12039 -- First (Container) => Cursor
12041 if Etype (Ent) /= Cursor then
12042 Error_Msg_N ("primitive for First must yield a curosr", N);
12043 end if;
12045 elsif Nam = Name_Next then
12047 -- Next (Container, Cursor) => Cursor
12049 F2 := Next_Formal (F1);
12051 if Etype (F2) /= Cursor
12052 or else Etype (Ent) /= Cursor
12053 or else Present (Next_Formal (F2))
12054 then
12055 Error_Msg_N ("no match for Next iterable primitive", N);
12056 end if;
12058 elsif Nam = Name_Has_Element then
12060 -- Has_Element (Container, Cursor) => Boolean
12062 F2 := Next_Formal (F1);
12063 if Etype (F2) /= Cursor
12064 or else Etype (Ent) /= Standard_Boolean
12065 or else Present (Next_Formal (F2))
12066 then
12067 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12068 end if;
12070 elsif Nam = Name_Element then
12071 F2 := Next_Formal (F1);
12073 if No (F2)
12074 or else Etype (F2) /= Cursor
12075 or else Present (Next_Formal (F2))
12076 then
12077 Error_Msg_N ("no match for Element iterable primitive", N);
12078 end if;
12079 null;
12081 else
12082 raise Program_Error;
12083 end if;
12085 else
12086 -- Overloaded case: find subprogram with proper signature.
12087 -- Caller will report error if no match is found.
12089 declare
12090 I : Interp_Index;
12091 It : Interp;
12093 begin
12094 Get_First_Interp (N, I, It);
12095 while Present (It.Typ) loop
12096 if Ekind (It.Nam) = E_Function
12097 and then Scope (It.Nam) = Scope (Typ)
12098 and then Etype (First_Formal (It.Nam)) = Typ
12099 then
12100 F1 := First_Formal (It.Nam);
12102 if Nam = Name_First then
12103 if Etype (It.Nam) = Cursor
12104 and then No (Next_Formal (F1))
12105 then
12106 Set_Entity (N, It.Nam);
12107 exit;
12108 end if;
12110 elsif Nam = Name_Next then
12111 F2 := Next_Formal (F1);
12113 if Present (F2)
12114 and then No (Next_Formal (F2))
12115 and then Etype (F2) = Cursor
12116 and then Etype (It.Nam) = Cursor
12117 then
12118 Set_Entity (N, It.Nam);
12119 exit;
12120 end if;
12122 elsif Nam = Name_Has_Element then
12123 F2 := Next_Formal (F1);
12125 if Present (F2)
12126 and then No (Next_Formal (F2))
12127 and then Etype (F2) = Cursor
12128 and then Etype (It.Nam) = Standard_Boolean
12129 then
12130 Set_Entity (N, It.Nam);
12131 F2 := Next_Formal (F1);
12132 exit;
12133 end if;
12135 elsif Nam = Name_Element then
12136 F2 := Next_Formal (F1);
12138 if Present (F2)
12139 and then No (Next_Formal (F2))
12140 and then Etype (F2) = Cursor
12141 then
12142 Set_Entity (N, It.Nam);
12143 exit;
12144 end if;
12145 end if;
12146 end if;
12148 Get_Next_Interp (I, It);
12149 end loop;
12150 end;
12151 end if;
12152 end Resolve_Iterable_Operation;
12154 ----------------
12155 -- Set_Biased --
12156 ----------------
12158 procedure Set_Biased
12159 (E : Entity_Id;
12160 N : Node_Id;
12161 Msg : String;
12162 Biased : Boolean := True)
12164 begin
12165 if Biased then
12166 Set_Has_Biased_Representation (E);
12168 if Warn_On_Biased_Representation then
12169 Error_Msg_NE
12170 ("?B?" & Msg & " forces biased representation for&", N, E);
12171 end if;
12172 end if;
12173 end Set_Biased;
12175 --------------------
12176 -- Set_Enum_Esize --
12177 --------------------
12179 procedure Set_Enum_Esize (T : Entity_Id) is
12180 Lo : Uint;
12181 Hi : Uint;
12182 Sz : Nat;
12184 begin
12185 Init_Alignment (T);
12187 -- Find the minimum standard size (8,16,32,64) that fits
12189 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12190 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12192 if Lo < 0 then
12193 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12194 Sz := Standard_Character_Size; -- May be > 8 on some targets
12196 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12197 Sz := 16;
12199 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12200 Sz := 32;
12202 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12203 Sz := 64;
12204 end if;
12206 else
12207 if Hi < Uint_2**08 then
12208 Sz := Standard_Character_Size; -- May be > 8 on some targets
12210 elsif Hi < Uint_2**16 then
12211 Sz := 16;
12213 elsif Hi < Uint_2**32 then
12214 Sz := 32;
12216 else pragma Assert (Hi < Uint_2**63);
12217 Sz := 64;
12218 end if;
12219 end if;
12221 -- That minimum is the proper size unless we have a foreign convention
12222 -- and the size required is 32 or less, in which case we bump the size
12223 -- up to 32. This is required for C and C++ and seems reasonable for
12224 -- all other foreign conventions.
12226 if Has_Foreign_Convention (T)
12227 and then Esize (T) < Standard_Integer_Size
12229 -- Don't do this if Short_Enums on target
12231 and then not Target_Short_Enums
12232 then
12233 Init_Esize (T, Standard_Integer_Size);
12234 else
12235 Init_Esize (T, Sz);
12236 end if;
12237 end Set_Enum_Esize;
12239 -----------------------------
12240 -- Uninstall_Discriminants --
12241 -----------------------------
12243 procedure Uninstall_Discriminants (E : Entity_Id) is
12244 Disc : Entity_Id;
12245 Prev : Entity_Id;
12246 Outer : Entity_Id;
12248 begin
12249 -- Discriminants have been made visible for type declarations and
12250 -- protected type declarations, not for subtype declarations.
12252 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12253 Disc := First_Discriminant (E);
12254 while Present (Disc) loop
12255 if Disc /= Current_Entity (Disc) then
12256 Prev := Current_Entity (Disc);
12257 while Present (Prev)
12258 and then Present (Homonym (Prev))
12259 and then Homonym (Prev) /= Disc
12260 loop
12261 Prev := Homonym (Prev);
12262 end loop;
12263 else
12264 Prev := Empty;
12265 end if;
12267 Set_Is_Immediately_Visible (Disc, False);
12269 Outer := Homonym (Disc);
12270 while Present (Outer) and then Scope (Outer) = E loop
12271 Outer := Homonym (Outer);
12272 end loop;
12274 -- Reset homonym link of other entities, but do not modify link
12275 -- between entities in current scope, so that the back-end can
12276 -- have a proper count of local overloadings.
12278 if No (Prev) then
12279 Set_Name_Entity_Id (Chars (Disc), Outer);
12281 elsif Scope (Prev) /= Scope (Disc) then
12282 Set_Homonym (Prev, Outer);
12283 end if;
12285 Next_Discriminant (Disc);
12286 end loop;
12287 end if;
12288 end Uninstall_Discriminants;
12290 -------------------------------------------
12291 -- Uninstall_Discriminants_And_Pop_Scope --
12292 -------------------------------------------
12294 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12295 begin
12296 if Has_Discriminants (E) then
12297 Uninstall_Discriminants (E);
12298 Pop_Scope;
12299 end if;
12300 end Uninstall_Discriminants_And_Pop_Scope;
12302 ------------------------------
12303 -- Validate_Address_Clauses --
12304 ------------------------------
12306 procedure Validate_Address_Clauses is
12307 begin
12308 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12309 declare
12310 ACCR : Address_Clause_Check_Record
12311 renames Address_Clause_Checks.Table (J);
12313 Expr : Node_Id;
12315 X_Alignment : Uint;
12316 Y_Alignment : Uint;
12318 X_Size : Uint;
12319 Y_Size : Uint;
12321 begin
12322 -- Skip processing of this entry if warning already posted
12324 if not Address_Warning_Posted (ACCR.N) then
12325 Expr := Original_Node (Expression (ACCR.N));
12327 -- Get alignments
12329 X_Alignment := Alignment (ACCR.X);
12330 Y_Alignment := Alignment (ACCR.Y);
12332 -- Similarly obtain sizes
12334 X_Size := Esize (ACCR.X);
12335 Y_Size := Esize (ACCR.Y);
12337 -- Check for large object overlaying smaller one
12339 if Y_Size > Uint_0
12340 and then X_Size > Uint_0
12341 and then X_Size > Y_Size
12342 then
12343 Error_Msg_NE
12344 ("??& overlays smaller object", ACCR.N, ACCR.X);
12345 Error_Msg_N
12346 ("\??program execution may be erroneous", ACCR.N);
12347 Error_Msg_Uint_1 := X_Size;
12348 Error_Msg_NE
12349 ("\??size of & is ^", ACCR.N, ACCR.X);
12350 Error_Msg_Uint_1 := Y_Size;
12351 Error_Msg_NE
12352 ("\??size of & is ^", ACCR.N, ACCR.Y);
12354 -- Check for inadequate alignment, both of the base object
12355 -- and of the offset, if any.
12357 -- Note: we do not check the alignment if we gave a size
12358 -- warning, since it would likely be redundant.
12360 elsif Y_Alignment /= Uint_0
12361 and then (Y_Alignment < X_Alignment
12362 or else (ACCR.Off
12363 and then
12364 Nkind (Expr) = N_Attribute_Reference
12365 and then
12366 Attribute_Name (Expr) = Name_Address
12367 and then
12368 Has_Compatible_Alignment
12369 (ACCR.X, Prefix (Expr))
12370 /= Known_Compatible))
12371 then
12372 Error_Msg_NE
12373 ("??specified address for& may be inconsistent "
12374 & "with alignment", ACCR.N, ACCR.X);
12375 Error_Msg_N
12376 ("\??program execution may be erroneous (RM 13.3(27))",
12377 ACCR.N);
12378 Error_Msg_Uint_1 := X_Alignment;
12379 Error_Msg_NE
12380 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12381 Error_Msg_Uint_1 := Y_Alignment;
12382 Error_Msg_NE
12383 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12384 if Y_Alignment >= X_Alignment then
12385 Error_Msg_N
12386 ("\??but offset is not multiple of alignment", ACCR.N);
12387 end if;
12388 end if;
12389 end if;
12390 end;
12391 end loop;
12392 end Validate_Address_Clauses;
12394 ---------------------------
12395 -- Validate_Independence --
12396 ---------------------------
12398 procedure Validate_Independence is
12399 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12400 N : Node_Id;
12401 E : Entity_Id;
12402 IC : Boolean;
12403 Comp : Entity_Id;
12404 Addr : Node_Id;
12405 P : Node_Id;
12407 procedure Check_Array_Type (Atyp : Entity_Id);
12408 -- Checks if the array type Atyp has independent components, and
12409 -- if not, outputs an appropriate set of error messages.
12411 procedure No_Independence;
12412 -- Output message that independence cannot be guaranteed
12414 function OK_Component (C : Entity_Id) return Boolean;
12415 -- Checks one component to see if it is independently accessible, and
12416 -- if so yields True, otherwise yields False if independent access
12417 -- cannot be guaranteed. This is a conservative routine, it only
12418 -- returns True if it knows for sure, it returns False if it knows
12419 -- there is a problem, or it cannot be sure there is no problem.
12421 procedure Reason_Bad_Component (C : Entity_Id);
12422 -- Outputs continuation message if a reason can be determined for
12423 -- the component C being bad.
12425 ----------------------
12426 -- Check_Array_Type --
12427 ----------------------
12429 procedure Check_Array_Type (Atyp : Entity_Id) is
12430 Ctyp : constant Entity_Id := Component_Type (Atyp);
12432 begin
12433 -- OK if no alignment clause, no pack, and no component size
12435 if not Has_Component_Size_Clause (Atyp)
12436 and then not Has_Alignment_Clause (Atyp)
12437 and then not Is_Packed (Atyp)
12438 then
12439 return;
12440 end if;
12442 -- Case of component size is greater than or equal to 64 and the
12443 -- alignment of the array is at least as large as the alignment
12444 -- of the component. We are definitely OK in this situation.
12446 if Known_Component_Size (Atyp)
12447 and then Component_Size (Atyp) >= 64
12448 and then Known_Alignment (Atyp)
12449 and then Known_Alignment (Ctyp)
12450 and then Alignment (Atyp) >= Alignment (Ctyp)
12451 then
12452 return;
12453 end if;
12455 -- Check actual component size
12457 if not Known_Component_Size (Atyp)
12458 or else not (Addressable (Component_Size (Atyp))
12459 and then Component_Size (Atyp) < 64)
12460 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12461 then
12462 No_Independence;
12464 -- Bad component size, check reason
12466 if Has_Component_Size_Clause (Atyp) then
12467 P := Get_Attribute_Definition_Clause
12468 (Atyp, Attribute_Component_Size);
12470 if Present (P) then
12471 Error_Msg_Sloc := Sloc (P);
12472 Error_Msg_N ("\because of Component_Size clause#", N);
12473 return;
12474 end if;
12475 end if;
12477 if Is_Packed (Atyp) then
12478 P := Get_Rep_Pragma (Atyp, Name_Pack);
12480 if Present (P) then
12481 Error_Msg_Sloc := Sloc (P);
12482 Error_Msg_N ("\because of pragma Pack#", N);
12483 return;
12484 end if;
12485 end if;
12487 -- No reason found, just return
12489 return;
12490 end if;
12492 -- Array type is OK independence-wise
12494 return;
12495 end Check_Array_Type;
12497 ---------------------
12498 -- No_Independence --
12499 ---------------------
12501 procedure No_Independence is
12502 begin
12503 if Pragma_Name (N) = Name_Independent then
12504 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
12505 else
12506 Error_Msg_NE
12507 ("independent components cannot be guaranteed for&", N, E);
12508 end if;
12509 end No_Independence;
12511 ------------------
12512 -- OK_Component --
12513 ------------------
12515 function OK_Component (C : Entity_Id) return Boolean is
12516 Rec : constant Entity_Id := Scope (C);
12517 Ctyp : constant Entity_Id := Etype (C);
12519 begin
12520 -- OK if no component clause, no Pack, and no alignment clause
12522 if No (Component_Clause (C))
12523 and then not Is_Packed (Rec)
12524 and then not Has_Alignment_Clause (Rec)
12525 then
12526 return True;
12527 end if;
12529 -- Here we look at the actual component layout. A component is
12530 -- addressable if its size is a multiple of the Esize of the
12531 -- component type, and its starting position in the record has
12532 -- appropriate alignment, and the record itself has appropriate
12533 -- alignment to guarantee the component alignment.
12535 -- Make sure sizes are static, always assume the worst for any
12536 -- cases where we cannot check static values.
12538 if not (Known_Static_Esize (C)
12539 and then
12540 Known_Static_Esize (Ctyp))
12541 then
12542 return False;
12543 end if;
12545 -- Size of component must be addressable or greater than 64 bits
12546 -- and a multiple of bytes.
12548 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
12549 return False;
12550 end if;
12552 -- Check size is proper multiple
12554 if Esize (C) mod Esize (Ctyp) /= 0 then
12555 return False;
12556 end if;
12558 -- Check alignment of component is OK
12560 if not Known_Component_Bit_Offset (C)
12561 or else Component_Bit_Offset (C) < Uint_0
12562 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
12563 then
12564 return False;
12565 end if;
12567 -- Check alignment of record type is OK
12569 if not Known_Alignment (Rec)
12570 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12571 then
12572 return False;
12573 end if;
12575 -- All tests passed, component is addressable
12577 return True;
12578 end OK_Component;
12580 --------------------------
12581 -- Reason_Bad_Component --
12582 --------------------------
12584 procedure Reason_Bad_Component (C : Entity_Id) is
12585 Rec : constant Entity_Id := Scope (C);
12586 Ctyp : constant Entity_Id := Etype (C);
12588 begin
12589 -- If component clause present assume that's the problem
12591 if Present (Component_Clause (C)) then
12592 Error_Msg_Sloc := Sloc (Component_Clause (C));
12593 Error_Msg_N ("\because of Component_Clause#", N);
12594 return;
12595 end if;
12597 -- If pragma Pack clause present, assume that's the problem
12599 if Is_Packed (Rec) then
12600 P := Get_Rep_Pragma (Rec, Name_Pack);
12602 if Present (P) then
12603 Error_Msg_Sloc := Sloc (P);
12604 Error_Msg_N ("\because of pragma Pack#", N);
12605 return;
12606 end if;
12607 end if;
12609 -- See if record has bad alignment clause
12611 if Has_Alignment_Clause (Rec)
12612 and then Known_Alignment (Rec)
12613 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12614 then
12615 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
12617 if Present (P) then
12618 Error_Msg_Sloc := Sloc (P);
12619 Error_Msg_N ("\because of Alignment clause#", N);
12620 end if;
12621 end if;
12623 -- Couldn't find a reason, so return without a message
12625 return;
12626 end Reason_Bad_Component;
12628 -- Start of processing for Validate_Independence
12630 begin
12631 for J in Independence_Checks.First .. Independence_Checks.Last loop
12632 N := Independence_Checks.Table (J).N;
12633 E := Independence_Checks.Table (J).E;
12634 IC := Pragma_Name (N) = Name_Independent_Components;
12636 -- Deal with component case
12638 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
12639 if not OK_Component (E) then
12640 No_Independence;
12641 Reason_Bad_Component (E);
12642 goto Continue;
12643 end if;
12644 end if;
12646 -- Deal with record with Independent_Components
12648 if IC and then Is_Record_Type (E) then
12649 Comp := First_Component_Or_Discriminant (E);
12650 while Present (Comp) loop
12651 if not OK_Component (Comp) then
12652 No_Independence;
12653 Reason_Bad_Component (Comp);
12654 goto Continue;
12655 end if;
12657 Next_Component_Or_Discriminant (Comp);
12658 end loop;
12659 end if;
12661 -- Deal with address clause case
12663 if Is_Object (E) then
12664 Addr := Address_Clause (E);
12666 if Present (Addr) then
12667 No_Independence;
12668 Error_Msg_Sloc := Sloc (Addr);
12669 Error_Msg_N ("\because of Address clause#", N);
12670 goto Continue;
12671 end if;
12672 end if;
12674 -- Deal with independent components for array type
12676 if IC and then Is_Array_Type (E) then
12677 Check_Array_Type (E);
12678 end if;
12680 -- Deal with independent components for array object
12682 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
12683 Check_Array_Type (Etype (E));
12684 end if;
12686 <<Continue>> null;
12687 end loop;
12688 end Validate_Independence;
12690 ------------------------------
12691 -- Validate_Iterable_Aspect --
12692 ------------------------------
12694 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
12695 Assoc : Node_Id;
12696 Expr : Node_Id;
12698 Prim : Node_Id;
12699 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
12701 First_Id : Entity_Id;
12702 Next_Id : Entity_Id;
12703 Has_Element_Id : Entity_Id;
12704 Element_Id : Entity_Id;
12706 begin
12707 -- If previous error aspect is unusable
12709 if Cursor = Any_Type then
12710 return;
12711 end if;
12713 First_Id := Empty;
12714 Next_Id := Empty;
12715 Has_Element_Id := Empty;
12716 Element_Id := Empty;
12718 -- Each expression must resolve to a function with the proper signature
12720 Assoc := First (Component_Associations (Expression (ASN)));
12721 while Present (Assoc) loop
12722 Expr := Expression (Assoc);
12723 Analyze (Expr);
12725 Prim := First (Choices (Assoc));
12727 if Nkind (Prim) /= N_Identifier
12728 or else Present (Next (Prim))
12729 then
12730 Error_Msg_N ("illegal name in association", Prim);
12732 elsif Chars (Prim) = Name_First then
12733 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
12734 First_Id := Entity (Expr);
12736 elsif Chars (Prim) = Name_Next then
12737 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
12738 Next_Id := Entity (Expr);
12740 elsif Chars (Prim) = Name_Has_Element then
12741 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
12742 Has_Element_Id := Entity (Expr);
12744 elsif Chars (Prim) = Name_Element then
12745 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
12746 Element_Id := Entity (Expr);
12748 else
12749 Error_Msg_N ("invalid name for iterable function", Prim);
12750 end if;
12752 Next (Assoc);
12753 end loop;
12755 if No (First_Id) then
12756 Error_Msg_N ("match for First primitive not found", ASN);
12758 elsif No (Next_Id) then
12759 Error_Msg_N ("match for Next primitive not found", ASN);
12761 elsif No (Has_Element_Id) then
12762 Error_Msg_N ("match for Has_Element primitive not found", ASN);
12764 elsif No (Element_Id) then
12765 null; -- Optional.
12766 end if;
12767 end Validate_Iterable_Aspect;
12769 -----------------------------------
12770 -- Validate_Unchecked_Conversion --
12771 -----------------------------------
12773 procedure Validate_Unchecked_Conversion
12774 (N : Node_Id;
12775 Act_Unit : Entity_Id)
12777 Source : Entity_Id;
12778 Target : Entity_Id;
12779 Vnode : Node_Id;
12781 begin
12782 -- Obtain source and target types. Note that we call Ancestor_Subtype
12783 -- here because the processing for generic instantiation always makes
12784 -- subtypes, and we want the original frozen actual types.
12786 -- If we are dealing with private types, then do the check on their
12787 -- fully declared counterparts if the full declarations have been
12788 -- encountered (they don't have to be visible, but they must exist).
12790 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
12792 if Is_Private_Type (Source)
12793 and then Present (Underlying_Type (Source))
12794 then
12795 Source := Underlying_Type (Source);
12796 end if;
12798 Target := Ancestor_Subtype (Etype (Act_Unit));
12800 -- If either type is generic, the instantiation happens within a generic
12801 -- unit, and there is nothing to check. The proper check will happen
12802 -- when the enclosing generic is instantiated.
12804 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
12805 return;
12806 end if;
12808 if Is_Private_Type (Target)
12809 and then Present (Underlying_Type (Target))
12810 then
12811 Target := Underlying_Type (Target);
12812 end if;
12814 -- Source may be unconstrained array, but not target
12816 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
12817 Error_Msg_N
12818 ("unchecked conversion to unconstrained array not allowed", N);
12819 return;
12820 end if;
12822 -- Warn if conversion between two different convention pointers
12824 if Is_Access_Type (Target)
12825 and then Is_Access_Type (Source)
12826 and then Convention (Target) /= Convention (Source)
12827 and then Warn_On_Unchecked_Conversion
12828 then
12829 -- Give warnings for subprogram pointers only on most targets
12831 if Is_Access_Subprogram_Type (Target)
12832 or else Is_Access_Subprogram_Type (Source)
12833 then
12834 Error_Msg_N
12835 ("?z?conversion between pointers with different conventions!",
12837 end if;
12838 end if;
12840 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
12841 -- warning when compiling GNAT-related sources.
12843 if Warn_On_Unchecked_Conversion
12844 and then not In_Predefined_Unit (N)
12845 and then RTU_Loaded (Ada_Calendar)
12846 and then
12847 (Chars (Source) = Name_Time
12848 or else
12849 Chars (Target) = Name_Time)
12850 then
12851 -- If Ada.Calendar is loaded and the name of one of the operands is
12852 -- Time, there is a good chance that this is Ada.Calendar.Time.
12854 declare
12855 Calendar_Time : constant Entity_Id :=
12856 Full_View (RTE (RO_CA_Time));
12857 begin
12858 pragma Assert (Present (Calendar_Time));
12860 if Source = Calendar_Time or else Target = Calendar_Time then
12861 Error_Msg_N
12862 ("?z?representation of 'Time values may change between " &
12863 "'G'N'A'T versions", N);
12864 end if;
12865 end;
12866 end if;
12868 -- Make entry in unchecked conversion table for later processing by
12869 -- Validate_Unchecked_Conversions, which will check sizes and alignments
12870 -- (using values set by the back-end where possible). This is only done
12871 -- if the appropriate warning is active.
12873 if Warn_On_Unchecked_Conversion then
12874 Unchecked_Conversions.Append
12875 (New_Val => UC_Entry'(Eloc => Sloc (N),
12876 Source => Source,
12877 Target => Target,
12878 Act_Unit => Act_Unit));
12880 -- If both sizes are known statically now, then back end annotation
12881 -- is not required to do a proper check but if either size is not
12882 -- known statically, then we need the annotation.
12884 if Known_Static_RM_Size (Source)
12885 and then
12886 Known_Static_RM_Size (Target)
12887 then
12888 null;
12889 else
12890 Back_Annotate_Rep_Info := True;
12891 end if;
12892 end if;
12894 -- If unchecked conversion to access type, and access type is declared
12895 -- in the same unit as the unchecked conversion, then set the flag
12896 -- No_Strict_Aliasing (no strict aliasing is implicit here)
12898 if Is_Access_Type (Target) and then
12899 In_Same_Source_Unit (Target, N)
12900 then
12901 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
12902 end if;
12904 -- Generate N_Validate_Unchecked_Conversion node for back end in case
12905 -- the back end needs to perform special validation checks.
12907 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
12908 -- have full expansion and the back end is called ???
12910 Vnode :=
12911 Make_Validate_Unchecked_Conversion (Sloc (N));
12912 Set_Source_Type (Vnode, Source);
12913 Set_Target_Type (Vnode, Target);
12915 -- If the unchecked conversion node is in a list, just insert before it.
12916 -- If not we have some strange case, not worth bothering about.
12918 if Is_List_Member (N) then
12919 Insert_After (N, Vnode);
12920 end if;
12921 end Validate_Unchecked_Conversion;
12923 ------------------------------------
12924 -- Validate_Unchecked_Conversions --
12925 ------------------------------------
12927 procedure Validate_Unchecked_Conversions is
12928 begin
12929 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
12930 declare
12931 T : UC_Entry renames Unchecked_Conversions.Table (N);
12933 Eloc : constant Source_Ptr := T.Eloc;
12934 Source : constant Entity_Id := T.Source;
12935 Target : constant Entity_Id := T.Target;
12936 Act_Unit : constant Entity_Id := T.Act_Unit;
12938 Source_Siz : Uint;
12939 Target_Siz : Uint;
12941 begin
12942 -- Skip if function marked as warnings off
12944 if Warnings_Off (Act_Unit) then
12945 goto Continue;
12946 end if;
12948 -- This validation check, which warns if we have unequal sizes for
12949 -- unchecked conversion, and thus potentially implementation
12950 -- dependent semantics, is one of the few occasions on which we
12951 -- use the official RM size instead of Esize. See description in
12952 -- Einfo "Handling of Type'Size Values" for details.
12954 if Serious_Errors_Detected = 0
12955 and then Known_Static_RM_Size (Source)
12956 and then Known_Static_RM_Size (Target)
12958 -- Don't do the check if warnings off for either type, note the
12959 -- deliberate use of OR here instead of OR ELSE to get the flag
12960 -- Warnings_Off_Used set for both types if appropriate.
12962 and then not (Has_Warnings_Off (Source)
12964 Has_Warnings_Off (Target))
12965 then
12966 Source_Siz := RM_Size (Source);
12967 Target_Siz := RM_Size (Target);
12969 if Source_Siz /= Target_Siz then
12970 Error_Msg
12971 ("?z?types for unchecked conversion have different sizes!",
12972 Eloc);
12974 if All_Errors_Mode then
12975 Error_Msg_Name_1 := Chars (Source);
12976 Error_Msg_Uint_1 := Source_Siz;
12977 Error_Msg_Name_2 := Chars (Target);
12978 Error_Msg_Uint_2 := Target_Siz;
12979 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
12981 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
12983 if Is_Discrete_Type (Source)
12984 and then
12985 Is_Discrete_Type (Target)
12986 then
12987 if Source_Siz > Target_Siz then
12988 Error_Msg
12989 ("\?z?^ high order bits of source will "
12990 & "be ignored!", Eloc);
12992 elsif Is_Unsigned_Type (Source) then
12993 Error_Msg
12994 ("\?z?source will be extended with ^ high order "
12995 & "zero bits!", Eloc);
12997 else
12998 Error_Msg
12999 ("\?z?source will be extended with ^ high order "
13000 & "sign bits!", Eloc);
13001 end if;
13003 elsif Source_Siz < Target_Siz then
13004 if Is_Discrete_Type (Target) then
13005 if Bytes_Big_Endian then
13006 Error_Msg
13007 ("\?z?target value will include ^ undefined "
13008 & "low order bits!", Eloc);
13009 else
13010 Error_Msg
13011 ("\?z?target value will include ^ undefined "
13012 & "high order bits!", Eloc);
13013 end if;
13015 else
13016 Error_Msg
13017 ("\?z?^ trailing bits of target value will be "
13018 & "undefined!", Eloc);
13019 end if;
13021 else pragma Assert (Source_Siz > Target_Siz);
13022 Error_Msg
13023 ("\?z?^ trailing bits of source will be ignored!",
13024 Eloc);
13025 end if;
13026 end if;
13027 end if;
13028 end if;
13030 -- If both types are access types, we need to check the alignment.
13031 -- If the alignment of both is specified, we can do it here.
13033 if Serious_Errors_Detected = 0
13034 and then Is_Access_Type (Source)
13035 and then Is_Access_Type (Target)
13036 and then Target_Strict_Alignment
13037 and then Present (Designated_Type (Source))
13038 and then Present (Designated_Type (Target))
13039 then
13040 declare
13041 D_Source : constant Entity_Id := Designated_Type (Source);
13042 D_Target : constant Entity_Id := Designated_Type (Target);
13044 begin
13045 if Known_Alignment (D_Source)
13046 and then
13047 Known_Alignment (D_Target)
13048 then
13049 declare
13050 Source_Align : constant Uint := Alignment (D_Source);
13051 Target_Align : constant Uint := Alignment (D_Target);
13053 begin
13054 if Source_Align < Target_Align
13055 and then not Is_Tagged_Type (D_Source)
13057 -- Suppress warning if warnings suppressed on either
13058 -- type or either designated type. Note the use of
13059 -- OR here instead of OR ELSE. That is intentional,
13060 -- we would like to set flag Warnings_Off_Used in
13061 -- all types for which warnings are suppressed.
13063 and then not (Has_Warnings_Off (D_Source)
13065 Has_Warnings_Off (D_Target)
13067 Has_Warnings_Off (Source)
13069 Has_Warnings_Off (Target))
13070 then
13071 Error_Msg_Uint_1 := Target_Align;
13072 Error_Msg_Uint_2 := Source_Align;
13073 Error_Msg_Node_1 := D_Target;
13074 Error_Msg_Node_2 := D_Source;
13075 Error_Msg
13076 ("?z?alignment of & (^) is stricter than "
13077 & "alignment of & (^)!", Eloc);
13078 Error_Msg
13079 ("\?z?resulting access value may have invalid "
13080 & "alignment!", Eloc);
13081 end if;
13082 end;
13083 end if;
13084 end;
13085 end if;
13086 end;
13088 <<Continue>>
13089 null;
13090 end loop;
13091 end Validate_Unchecked_Conversions;
13093 end Sem_Ch13;