2014-11-20 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch13.adb
blob2ca48ef46dd9cc02f6fc3221a8148b6003868e5a
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Case; use Sem_Case;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Sinput; use Sinput;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Ttypes; use Ttypes;
67 with Tbuild; use Tbuild;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
71 with GNAT.Heap_Sort_G;
73 package body Sem_Ch13 is
75 SSU : constant Pos := System_Storage_Unit;
76 -- Convenient short hand for commonly used constant
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
83 -- This routine is called after setting one of the sizes of type entity
84 -- Typ to Size. The purpose is to deal with the situation of a derived
85 -- type whose inherited alignment is no longer appropriate for the new
86 -- size value. In this case, we reset the Alignment to unknown.
88 procedure Build_Discrete_Static_Predicate
89 (Typ : Entity_Id;
90 Expr : Node_Id;
91 Nam : Name_Id);
92 -- Given a predicated type Typ, where Typ is a discrete static subtype,
93 -- whose predicate expression is Expr, tests if Expr is a static predicate,
94 -- and if so, builds the predicate range list. Nam is the name of the one
95 -- argument to the predicate function. Occurrences of the type name in the
96 -- predicate expression have been replaced by identifier references to this
97 -- name, which is unique, so any identifier with Chars matching Nam must be
98 -- a reference to the type. If the predicate is non-static, this procedure
99 -- returns doing nothing. If the predicate is static, then the predicate
100 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
101 -- rewritten as a canonicalized membership operation.
103 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
104 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
105 -- then either there are pragma Predicate entries on the rep chain for the
106 -- type (note that Predicate aspects are converted to pragma Predicate), or
107 -- there are inherited aspects from a parent type, or ancestor subtypes.
108 -- This procedure builds the spec and body for the Predicate function that
109 -- tests these predicates. N is the freeze node for the type. The spec of
110 -- the function is inserted before the freeze node, and the body of the
111 -- function is inserted after the freeze node. If the predicate expression
112 -- has at least one Raise_Expression, then this procedure also builds the
113 -- M version of the predicate function for use in membership tests.
115 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
116 -- Called if both Storage_Pool and Storage_Size attribute definition
117 -- clauses (SP and SS) are present for entity Ent. Issue error message.
119 procedure Freeze_Entity_Checks (N : Node_Id);
120 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
121 -- to generate appropriate semantic checks that are delayed until this
122 -- point (they had to be delayed this long for cases of delayed aspects,
123 -- e.g. analysis of statically predicated subtypes in choices, for which
124 -- we have to be sure the subtypes in question are frozen before checking.
126 function Get_Alignment_Value (Expr : Node_Id) return Uint;
127 -- Given the expression for an alignment value, returns the corresponding
128 -- Uint value. If the value is inappropriate, then error messages are
129 -- posted as required, and a value of No_Uint is returned.
131 function Is_Operational_Item (N : Node_Id) return Boolean;
132 -- A specification for a stream attribute is allowed before the full type
133 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
134 -- that do not specify a representation characteristic are operational
135 -- attributes.
137 function Is_Predicate_Static
138 (Expr : Node_Id;
139 Nam : Name_Id) return Boolean;
140 -- Given predicate expression Expr, tests if Expr is predicate-static in
141 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
142 -- name in the predicate expression have been replaced by references to
143 -- an identifier whose Chars field is Nam. This name is unique, so any
144 -- identifier with Chars matching Nam must be a reference to the type.
145 -- Returns True if the expression is predicate-static and False otherwise,
146 -- but is not in the business of setting flags or issuing error messages.
148 -- Only scalar types can have static predicates, so False is always
149 -- returned for non-scalar types.
151 -- Note: the RM seems to suggest that string types can also have static
152 -- predicates. But that really makes lttle sense as very few useful
153 -- predicates can be constructed for strings. Remember that:
155 -- "ABC" < "DEF"
157 -- is not a static expression. So even though the clearly faulty RM wording
158 -- allows the following:
160 -- subtype S is String with Static_Predicate => S < "DEF"
162 -- We can't allow this, otherwise we have predicate-static applying to a
163 -- larger class than static expressions, which was never intended.
165 procedure New_Stream_Subprogram
166 (N : Node_Id;
167 Ent : Entity_Id;
168 Subp : Entity_Id;
169 Nam : TSS_Name_Type);
170 -- Create a subprogram renaming of a given stream attribute to the
171 -- designated subprogram and then in the tagged case, provide this as a
172 -- primitive operation, or in the untagged case make an appropriate TSS
173 -- entry. This is more properly an expansion activity than just semantics,
174 -- but the presence of user-defined stream functions for limited types
175 -- is a legality check, which is why this takes place here rather than in
176 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
177 -- function to be generated.
179 -- To avoid elaboration anomalies with freeze nodes, for untagged types
180 -- we generate both a subprogram declaration and a subprogram renaming
181 -- declaration, so that the attribute specification is handled as a
182 -- renaming_as_body. For tagged types, the specification is one of the
183 -- primitive specs.
185 procedure Resolve_Iterable_Operation
186 (N : Node_Id;
187 Cursor : Entity_Id;
188 Typ : Entity_Id;
189 Nam : Name_Id);
190 -- If the name of a primitive operation for an Iterable aspect is
191 -- overloaded, resolve according to required signature.
193 procedure Set_Biased
194 (E : Entity_Id;
195 N : Node_Id;
196 Msg : String;
197 Biased : Boolean := True);
198 -- If Biased is True, sets Has_Biased_Representation flag for E, and
199 -- outputs a warning message at node N if Warn_On_Biased_Representation is
200 -- is True. This warning inserts the string Msg to describe the construct
201 -- causing biasing.
203 ----------------------------------------------
204 -- Table for Validate_Unchecked_Conversions --
205 ----------------------------------------------
207 -- The following table collects unchecked conversions for validation.
208 -- Entries are made by Validate_Unchecked_Conversion and then the call
209 -- to Validate_Unchecked_Conversions does the actual error checking and
210 -- posting of warnings. The reason for this delayed processing is to take
211 -- advantage of back-annotations of size and alignment values performed by
212 -- the back end.
214 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
215 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
216 -- already have modified all Sloc values if the -gnatD option is set.
218 type UC_Entry is record
219 Eloc : Source_Ptr; -- node used for posting warnings
220 Source : Entity_Id; -- source type for unchecked conversion
221 Target : Entity_Id; -- target type for unchecked conversion
222 Act_Unit : Entity_Id; -- actual function instantiated
223 end record;
225 package Unchecked_Conversions is new Table.Table (
226 Table_Component_Type => UC_Entry,
227 Table_Index_Type => Int,
228 Table_Low_Bound => 1,
229 Table_Initial => 50,
230 Table_Increment => 200,
231 Table_Name => "Unchecked_Conversions");
233 ----------------------------------------
234 -- Table for Validate_Address_Clauses --
235 ----------------------------------------
237 -- If an address clause has the form
239 -- for X'Address use Expr
241 -- where Expr is of the form Y'Address or recursively is a reference to a
242 -- constant of either of these forms, and X and Y are entities of objects,
243 -- then if Y has a smaller alignment than X, that merits a warning about
244 -- possible bad alignment. The following table collects address clauses of
245 -- this kind. We put these in a table so that they can be checked after the
246 -- back end has completed annotation of the alignments of objects, since we
247 -- can catch more cases that way.
249 type Address_Clause_Check_Record is record
250 N : Node_Id;
251 -- The address clause
253 X : Entity_Id;
254 -- The entity of the object overlaying Y
256 Y : Entity_Id;
257 -- The entity of the object being overlaid
259 Off : Boolean;
260 -- Whether the address is offset within Y
261 end record;
263 package Address_Clause_Checks is new Table.Table (
264 Table_Component_Type => Address_Clause_Check_Record,
265 Table_Index_Type => Int,
266 Table_Low_Bound => 1,
267 Table_Initial => 20,
268 Table_Increment => 200,
269 Table_Name => "Address_Clause_Checks");
271 -----------------------------------------
272 -- Adjust_Record_For_Reverse_Bit_Order --
273 -----------------------------------------
275 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
276 Comp : Node_Id;
277 CC : Node_Id;
279 begin
280 -- Processing depends on version of Ada
282 -- For Ada 95, we just renumber bits within a storage unit. We do the
283 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
284 -- Ada 83, and are free to add this extension.
286 if Ada_Version < Ada_2005 then
287 Comp := First_Component_Or_Discriminant (R);
288 while Present (Comp) loop
289 CC := Component_Clause (Comp);
291 -- If component clause is present, then deal with the non-default
292 -- bit order case for Ada 95 mode.
294 -- We only do this processing for the base type, and in fact that
295 -- is important, since otherwise if there are record subtypes, we
296 -- could reverse the bits once for each subtype, which is wrong.
298 if Present (CC) and then Ekind (R) = E_Record_Type then
299 declare
300 CFB : constant Uint := Component_Bit_Offset (Comp);
301 CSZ : constant Uint := Esize (Comp);
302 CLC : constant Node_Id := Component_Clause (Comp);
303 Pos : constant Node_Id := Position (CLC);
304 FB : constant Node_Id := First_Bit (CLC);
306 Storage_Unit_Offset : constant Uint :=
307 CFB / System_Storage_Unit;
309 Start_Bit : constant Uint :=
310 CFB mod System_Storage_Unit;
312 begin
313 -- Cases where field goes over storage unit boundary
315 if Start_Bit + CSZ > System_Storage_Unit then
317 -- Allow multi-byte field but generate warning
319 if Start_Bit mod System_Storage_Unit = 0
320 and then CSZ mod System_Storage_Unit = 0
321 then
322 Error_Msg_N
323 ("info: multi-byte field specified with "
324 & "non-standard Bit_Order?V?", CLC);
326 if Bytes_Big_Endian then
327 Error_Msg_N
328 ("\bytes are not reversed "
329 & "(component is big-endian)?V?", CLC);
330 else
331 Error_Msg_N
332 ("\bytes are not reversed "
333 & "(component is little-endian)?V?", CLC);
334 end if;
336 -- Do not allow non-contiguous field
338 else
339 Error_Msg_N
340 ("attempt to specify non-contiguous field "
341 & "not permitted", CLC);
342 Error_Msg_N
343 ("\caused by non-standard Bit_Order "
344 & "specified", CLC);
345 Error_Msg_N
346 ("\consider possibility of using "
347 & "Ada 2005 mode here", CLC);
348 end if;
350 -- Case where field fits in one storage unit
352 else
353 -- Give warning if suspicious component clause
355 if Intval (FB) >= System_Storage_Unit
356 and then Warn_On_Reverse_Bit_Order
357 then
358 Error_Msg_N
359 ("info: Bit_Order clause does not affect " &
360 "byte ordering?V?", Pos);
361 Error_Msg_Uint_1 :=
362 Intval (Pos) + Intval (FB) /
363 System_Storage_Unit;
364 Error_Msg_N
365 ("info: position normalized to ^ before bit " &
366 "order interpreted?V?", Pos);
367 end if;
369 -- Here is where we fix up the Component_Bit_Offset value
370 -- to account for the reverse bit order. Some examples of
371 -- what needs to be done are:
373 -- First_Bit .. Last_Bit Component_Bit_Offset
374 -- old new old new
376 -- 0 .. 0 7 .. 7 0 7
377 -- 0 .. 1 6 .. 7 0 6
378 -- 0 .. 2 5 .. 7 0 5
379 -- 0 .. 7 0 .. 7 0 4
381 -- 1 .. 1 6 .. 6 1 6
382 -- 1 .. 4 3 .. 6 1 3
383 -- 4 .. 7 0 .. 3 4 0
385 -- The rule is that the first bit is is obtained by
386 -- subtracting the old ending bit from storage_unit - 1.
388 Set_Component_Bit_Offset
389 (Comp,
390 (Storage_Unit_Offset * System_Storage_Unit) +
391 (System_Storage_Unit - 1) -
392 (Start_Bit + CSZ - 1));
394 Set_Normalized_First_Bit
395 (Comp,
396 Component_Bit_Offset (Comp) mod
397 System_Storage_Unit);
398 end if;
399 end;
400 end if;
402 Next_Component_Or_Discriminant (Comp);
403 end loop;
405 -- For Ada 2005, we do machine scalar processing, as fully described In
406 -- AI-133. This involves gathering all components which start at the
407 -- same byte offset and processing them together. Same approach is still
408 -- valid in later versions including Ada 2012.
410 else
411 declare
412 Max_Machine_Scalar_Size : constant Uint :=
413 UI_From_Int
414 (Standard_Long_Long_Integer_Size);
415 -- We use this as the maximum machine scalar size
417 Num_CC : Natural;
418 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
420 begin
421 -- This first loop through components does two things. First it
422 -- deals with the case of components with component clauses whose
423 -- length is greater than the maximum machine scalar size (either
424 -- accepting them or rejecting as needed). Second, it counts the
425 -- number of components with component clauses whose length does
426 -- not exceed this maximum for later processing.
428 Num_CC := 0;
429 Comp := First_Component_Or_Discriminant (R);
430 while Present (Comp) loop
431 CC := Component_Clause (Comp);
433 if Present (CC) then
434 declare
435 Fbit : constant Uint := Static_Integer (First_Bit (CC));
436 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
438 begin
439 -- Case of component with last bit >= max machine scalar
441 if Lbit >= Max_Machine_Scalar_Size then
443 -- This is allowed only if first bit is zero, and
444 -- last bit + 1 is a multiple of storage unit size.
446 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
448 -- This is the case to give a warning if enabled
450 if Warn_On_Reverse_Bit_Order then
451 Error_Msg_N
452 ("info: multi-byte field specified with "
453 & " non-standard Bit_Order?V?", CC);
455 if Bytes_Big_Endian then
456 Error_Msg_N
457 ("\bytes are not reversed "
458 & "(component is big-endian)?V?", CC);
459 else
460 Error_Msg_N
461 ("\bytes are not reversed "
462 & "(component is little-endian)?V?", CC);
463 end if;
464 end if;
466 -- Give error message for RM 13.5.1(10) violation
468 else
469 Error_Msg_FE
470 ("machine scalar rules not followed for&",
471 First_Bit (CC), Comp);
473 Error_Msg_Uint_1 := Lbit;
474 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
475 Error_Msg_F
476 ("\last bit (^) exceeds maximum machine "
477 & "scalar size (^)",
478 First_Bit (CC));
480 if (Lbit + 1) mod SSU /= 0 then
481 Error_Msg_Uint_1 := SSU;
482 Error_Msg_F
483 ("\and is not a multiple of Storage_Unit (^) "
484 & "(RM 13.4.1(10))",
485 First_Bit (CC));
487 else
488 Error_Msg_Uint_1 := Fbit;
489 Error_Msg_F
490 ("\and first bit (^) is non-zero "
491 & "(RM 13.4.1(10))",
492 First_Bit (CC));
493 end if;
494 end if;
496 -- OK case of machine scalar related component clause,
497 -- For now, just count them.
499 else
500 Num_CC := Num_CC + 1;
501 end if;
502 end;
503 end if;
505 Next_Component_Or_Discriminant (Comp);
506 end loop;
508 -- We need to sort the component clauses on the basis of the
509 -- Position values in the clause, so we can group clauses with
510 -- the same Position together to determine the relevant machine
511 -- scalar size.
513 Sort_CC : declare
514 Comps : array (0 .. Num_CC) of Entity_Id;
515 -- Array to collect component and discriminant entities. The
516 -- data starts at index 1, the 0'th entry is for the sort
517 -- routine.
519 function CP_Lt (Op1, Op2 : Natural) return Boolean;
520 -- Compare routine for Sort
522 procedure CP_Move (From : Natural; To : Natural);
523 -- Move routine for Sort
525 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
527 Start : Natural;
528 Stop : Natural;
529 -- Start and stop positions in the component list of the set of
530 -- components with the same starting position (that constitute
531 -- components in a single machine scalar).
533 MaxL : Uint;
534 -- Maximum last bit value of any component in this set
536 MSS : Uint;
537 -- Corresponding machine scalar size
539 -----------
540 -- CP_Lt --
541 -----------
543 function CP_Lt (Op1, Op2 : Natural) return Boolean is
544 begin
545 return Position (Component_Clause (Comps (Op1))) <
546 Position (Component_Clause (Comps (Op2)));
547 end CP_Lt;
549 -------------
550 -- CP_Move --
551 -------------
553 procedure CP_Move (From : Natural; To : Natural) is
554 begin
555 Comps (To) := Comps (From);
556 end CP_Move;
558 -- Start of processing for Sort_CC
560 begin
561 -- Collect the machine scalar relevant component clauses
563 Num_CC := 0;
564 Comp := First_Component_Or_Discriminant (R);
565 while Present (Comp) loop
566 declare
567 CC : constant Node_Id := Component_Clause (Comp);
569 begin
570 -- Collect only component clauses whose last bit is less
571 -- than machine scalar size. Any component clause whose
572 -- last bit exceeds this value does not take part in
573 -- machine scalar layout considerations. The test for
574 -- Error_Posted makes sure we exclude component clauses
575 -- for which we already posted an error.
577 if Present (CC)
578 and then not Error_Posted (Last_Bit (CC))
579 and then Static_Integer (Last_Bit (CC)) <
580 Max_Machine_Scalar_Size
581 then
582 Num_CC := Num_CC + 1;
583 Comps (Num_CC) := Comp;
584 end if;
585 end;
587 Next_Component_Or_Discriminant (Comp);
588 end loop;
590 -- Sort by ascending position number
592 Sorting.Sort (Num_CC);
594 -- We now have all the components whose size does not exceed
595 -- the max machine scalar value, sorted by starting position.
596 -- In this loop we gather groups of clauses starting at the
597 -- same position, to process them in accordance with AI-133.
599 Stop := 0;
600 while Stop < Num_CC loop
601 Start := Stop + 1;
602 Stop := Start;
603 MaxL :=
604 Static_Integer
605 (Last_Bit (Component_Clause (Comps (Start))));
606 while Stop < Num_CC loop
607 if Static_Integer
608 (Position (Component_Clause (Comps (Stop + 1)))) =
609 Static_Integer
610 (Position (Component_Clause (Comps (Stop))))
611 then
612 Stop := Stop + 1;
613 MaxL :=
614 UI_Max
615 (MaxL,
616 Static_Integer
617 (Last_Bit
618 (Component_Clause (Comps (Stop)))));
619 else
620 exit;
621 end if;
622 end loop;
624 -- Now we have a group of component clauses from Start to
625 -- Stop whose positions are identical, and MaxL is the
626 -- maximum last bit value of any of these components.
628 -- We need to determine the corresponding machine scalar
629 -- size. This loop assumes that machine scalar sizes are
630 -- even, and that each possible machine scalar has twice
631 -- as many bits as the next smaller one.
633 MSS := Max_Machine_Scalar_Size;
634 while MSS mod 2 = 0
635 and then (MSS / 2) >= SSU
636 and then (MSS / 2) > MaxL
637 loop
638 MSS := MSS / 2;
639 end loop;
641 -- Here is where we fix up the Component_Bit_Offset value
642 -- to account for the reverse bit order. Some examples of
643 -- what needs to be done for the case of a machine scalar
644 -- size of 8 are:
646 -- First_Bit .. Last_Bit Component_Bit_Offset
647 -- old new old new
649 -- 0 .. 0 7 .. 7 0 7
650 -- 0 .. 1 6 .. 7 0 6
651 -- 0 .. 2 5 .. 7 0 5
652 -- 0 .. 7 0 .. 7 0 4
654 -- 1 .. 1 6 .. 6 1 6
655 -- 1 .. 4 3 .. 6 1 3
656 -- 4 .. 7 0 .. 3 4 0
658 -- The rule is that the first bit is obtained by subtracting
659 -- the old ending bit from machine scalar size - 1.
661 for C in Start .. Stop loop
662 declare
663 Comp : constant Entity_Id := Comps (C);
664 CC : constant Node_Id := Component_Clause (Comp);
666 LB : constant Uint := Static_Integer (Last_Bit (CC));
667 NFB : constant Uint := MSS - Uint_1 - LB;
668 NLB : constant Uint := NFB + Esize (Comp) - 1;
669 Pos : constant Uint := Static_Integer (Position (CC));
671 begin
672 if Warn_On_Reverse_Bit_Order then
673 Error_Msg_Uint_1 := MSS;
674 Error_Msg_N
675 ("info: reverse bit order in machine " &
676 "scalar of length^?V?", First_Bit (CC));
677 Error_Msg_Uint_1 := NFB;
678 Error_Msg_Uint_2 := NLB;
680 if Bytes_Big_Endian then
681 Error_Msg_NE
682 ("\big-endian range for component "
683 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
684 else
685 Error_Msg_NE
686 ("\little-endian range for component"
687 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
688 end if;
689 end if;
691 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
692 Set_Normalized_First_Bit (Comp, NFB mod SSU);
693 end;
694 end loop;
695 end loop;
696 end Sort_CC;
697 end;
698 end if;
699 end Adjust_Record_For_Reverse_Bit_Order;
701 -------------------------------------
702 -- Alignment_Check_For_Size_Change --
703 -------------------------------------
705 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
706 begin
707 -- If the alignment is known, and not set by a rep clause, and is
708 -- inconsistent with the size being set, then reset it to unknown,
709 -- we assume in this case that the size overrides the inherited
710 -- alignment, and that the alignment must be recomputed.
712 if Known_Alignment (Typ)
713 and then not Has_Alignment_Clause (Typ)
714 and then Size mod (Alignment (Typ) * SSU) /= 0
715 then
716 Init_Alignment (Typ);
717 end if;
718 end Alignment_Check_For_Size_Change;
720 -------------------------------------
721 -- Analyze_Aspects_At_Freeze_Point --
722 -------------------------------------
724 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
725 ASN : Node_Id;
726 A_Id : Aspect_Id;
727 Ritem : Node_Id;
729 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
730 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
731 -- the aspect specification node ASN.
733 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
734 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
735 -- a derived type can inherit aspects from its parent which have been
736 -- specified at the time of the derivation using an aspect, as in:
738 -- type A is range 1 .. 10
739 -- with Size => Not_Defined_Yet;
740 -- ..
741 -- type B is new A;
742 -- ..
743 -- Not_Defined_Yet : constant := 64;
745 -- In this example, the Size of A is considered to be specified prior
746 -- to the derivation, and thus inherited, even though the value is not
747 -- known at the time of derivation. To deal with this, we use two entity
748 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
749 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
750 -- the derived type (B here). If this flag is set when the derived type
751 -- is frozen, then this procedure is called to ensure proper inheritance
752 -- of all delayed aspects from the parent type. The derived type is E,
753 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
754 -- aspect specification node in the Rep_Item chain for the parent type.
756 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
757 -- Given an aspect specification node ASN whose expression is an
758 -- optional Boolean, this routines creates the corresponding pragma
759 -- at the freezing point.
761 ----------------------------------
762 -- Analyze_Aspect_Default_Value --
763 ----------------------------------
765 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
766 Ent : constant Entity_Id := Entity (ASN);
767 Expr : constant Node_Id := Expression (ASN);
768 Id : constant Node_Id := Identifier (ASN);
770 begin
771 Error_Msg_Name_1 := Chars (Id);
773 if not Is_Type (Ent) then
774 Error_Msg_N ("aspect% can only apply to a type", Id);
775 return;
777 elsif not Is_First_Subtype (Ent) then
778 Error_Msg_N ("aspect% cannot apply to subtype", Id);
779 return;
781 elsif A_Id = Aspect_Default_Value
782 and then not Is_Scalar_Type (Ent)
783 then
784 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
785 return;
787 elsif A_Id = Aspect_Default_Component_Value then
788 if not Is_Array_Type (Ent) then
789 Error_Msg_N ("aspect% can only be applied to array type", Id);
790 return;
792 elsif not Is_Scalar_Type (Component_Type (Ent)) then
793 Error_Msg_N ("aspect% requires scalar components", Id);
794 return;
795 end if;
796 end if;
798 Set_Has_Default_Aspect (Base_Type (Ent));
800 if Is_Scalar_Type (Ent) then
801 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
802 else
803 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
804 end if;
805 end Analyze_Aspect_Default_Value;
807 ---------------------------------
808 -- Inherit_Delayed_Rep_Aspects --
809 ---------------------------------
811 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
812 P : constant Entity_Id := Entity (ASN);
813 -- Entithy for parent type
815 N : Node_Id;
816 -- Item from Rep_Item chain
818 A : Aspect_Id;
820 begin
821 -- Loop through delayed aspects for the parent type
823 N := ASN;
824 while Present (N) loop
825 if Nkind (N) = N_Aspect_Specification then
826 exit when Entity (N) /= P;
828 if Is_Delayed_Aspect (N) then
829 A := Get_Aspect_Id (Chars (Identifier (N)));
831 -- Process delayed rep aspect. For Boolean attributes it is
832 -- not possible to cancel an attribute once set (the attempt
833 -- to use an aspect with xxx => False is an error) for a
834 -- derived type. So for those cases, we do not have to check
835 -- if a clause has been given for the derived type, since it
836 -- is harmless to set it again if it is already set.
838 case A is
840 -- Alignment
842 when Aspect_Alignment =>
843 if not Has_Alignment_Clause (E) then
844 Set_Alignment (E, Alignment (P));
845 end if;
847 -- Atomic
849 when Aspect_Atomic =>
850 if Is_Atomic (P) then
851 Set_Is_Atomic (E);
852 end if;
854 -- Atomic_Components
856 when Aspect_Atomic_Components =>
857 if Has_Atomic_Components (P) then
858 Set_Has_Atomic_Components (Base_Type (E));
859 end if;
861 -- Bit_Order
863 when Aspect_Bit_Order =>
864 if Is_Record_Type (E)
865 and then No (Get_Attribute_Definition_Clause
866 (E, Attribute_Bit_Order))
867 and then Reverse_Bit_Order (P)
868 then
869 Set_Reverse_Bit_Order (Base_Type (E));
870 end if;
872 -- Component_Size
874 when Aspect_Component_Size =>
875 if Is_Array_Type (E)
876 and then not Has_Component_Size_Clause (E)
877 then
878 Set_Component_Size
879 (Base_Type (E), Component_Size (P));
880 end if;
882 -- Machine_Radix
884 when Aspect_Machine_Radix =>
885 if Is_Decimal_Fixed_Point_Type (E)
886 and then not Has_Machine_Radix_Clause (E)
887 then
888 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
889 end if;
891 -- Object_Size (also Size which also sets Object_Size)
893 when Aspect_Object_Size | Aspect_Size =>
894 if not Has_Size_Clause (E)
895 and then
896 No (Get_Attribute_Definition_Clause
897 (E, Attribute_Object_Size))
898 then
899 Set_Esize (E, Esize (P));
900 end if;
902 -- Pack
904 when Aspect_Pack =>
905 if not Is_Packed (E) then
906 Set_Is_Packed (Base_Type (E));
908 if Is_Bit_Packed_Array (P) then
909 Set_Is_Bit_Packed_Array (Base_Type (E));
910 Set_Packed_Array_Impl_Type
911 (E, Packed_Array_Impl_Type (P));
912 end if;
913 end if;
915 -- Scalar_Storage_Order
917 when Aspect_Scalar_Storage_Order =>
918 if (Is_Record_Type (E) or else Is_Array_Type (E))
919 and then No (Get_Attribute_Definition_Clause
920 (E, Attribute_Scalar_Storage_Order))
921 and then Reverse_Storage_Order (P)
922 then
923 Set_Reverse_Storage_Order (Base_Type (E));
925 -- Clear default SSO indications, since the aspect
926 -- overrides the default.
928 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
929 Set_SSO_Set_High_By_Default (Base_Type (E), False);
930 end if;
932 -- Small
934 when Aspect_Small =>
935 if Is_Fixed_Point_Type (E)
936 and then not Has_Small_Clause (E)
937 then
938 Set_Small_Value (E, Small_Value (P));
939 end if;
941 -- Storage_Size
943 when Aspect_Storage_Size =>
944 if (Is_Access_Type (E) or else Is_Task_Type (E))
945 and then not Has_Storage_Size_Clause (E)
946 then
947 Set_Storage_Size_Variable
948 (Base_Type (E), Storage_Size_Variable (P));
949 end if;
951 -- Value_Size
953 when Aspect_Value_Size =>
955 -- Value_Size is never inherited, it is either set by
956 -- default, or it is explicitly set for the derived
957 -- type. So nothing to do here.
959 null;
961 -- Volatile
963 when Aspect_Volatile =>
964 if Is_Volatile (P) then
965 Set_Is_Volatile (E);
966 end if;
968 -- Volatile_Components
970 when Aspect_Volatile_Components =>
971 if Has_Volatile_Components (P) then
972 Set_Has_Volatile_Components (Base_Type (E));
973 end if;
975 -- That should be all the Rep Aspects
977 when others =>
978 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
979 null;
981 end case;
982 end if;
983 end if;
985 N := Next_Rep_Item (N);
986 end loop;
987 end Inherit_Delayed_Rep_Aspects;
989 -------------------------------------
990 -- Make_Pragma_From_Boolean_Aspect --
991 -------------------------------------
993 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
994 Ident : constant Node_Id := Identifier (ASN);
995 A_Name : constant Name_Id := Chars (Ident);
996 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
997 Ent : constant Entity_Id := Entity (ASN);
998 Expr : constant Node_Id := Expression (ASN);
999 Loc : constant Source_Ptr := Sloc (ASN);
1001 Prag : Node_Id;
1003 procedure Check_False_Aspect_For_Derived_Type;
1004 -- This procedure checks for the case of a false aspect for a derived
1005 -- type, which improperly tries to cancel an aspect inherited from
1006 -- the parent.
1008 -----------------------------------------
1009 -- Check_False_Aspect_For_Derived_Type --
1010 -----------------------------------------
1012 procedure Check_False_Aspect_For_Derived_Type is
1013 Par : Node_Id;
1015 begin
1016 -- We are only checking derived types
1018 if not Is_Derived_Type (E) then
1019 return;
1020 end if;
1022 Par := Nearest_Ancestor (E);
1024 case A_Id is
1025 when Aspect_Atomic | Aspect_Shared =>
1026 if not Is_Atomic (Par) then
1027 return;
1028 end if;
1030 when Aspect_Atomic_Components =>
1031 if not Has_Atomic_Components (Par) then
1032 return;
1033 end if;
1035 when Aspect_Discard_Names =>
1036 if not Discard_Names (Par) then
1037 return;
1038 end if;
1040 when Aspect_Pack =>
1041 if not Is_Packed (Par) then
1042 return;
1043 end if;
1045 when Aspect_Unchecked_Union =>
1046 if not Is_Unchecked_Union (Par) then
1047 return;
1048 end if;
1050 when Aspect_Volatile =>
1051 if not Is_Volatile (Par) then
1052 return;
1053 end if;
1055 when Aspect_Volatile_Components =>
1056 if not Has_Volatile_Components (Par) then
1057 return;
1058 end if;
1060 when others =>
1061 return;
1062 end case;
1064 -- Fall through means we are canceling an inherited aspect
1066 Error_Msg_Name_1 := A_Name;
1067 Error_Msg_NE
1068 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1070 end Check_False_Aspect_For_Derived_Type;
1072 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1074 begin
1075 -- Note that we know Expr is present, because for a missing Expr
1076 -- argument, we knew it was True and did not need to delay the
1077 -- evaluation to the freeze point.
1079 if Is_False (Static_Boolean (Expr)) then
1080 Check_False_Aspect_For_Derived_Type;
1082 else
1083 Prag :=
1084 Make_Pragma (Loc,
1085 Pragma_Argument_Associations => New_List (
1086 Make_Pragma_Argument_Association (Sloc (Ident),
1087 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1089 Pragma_Identifier =>
1090 Make_Identifier (Sloc (Ident), Chars (Ident)));
1092 Set_From_Aspect_Specification (Prag, True);
1093 Set_Corresponding_Aspect (Prag, ASN);
1094 Set_Aspect_Rep_Item (ASN, Prag);
1095 Set_Is_Delayed_Aspect (Prag);
1096 Set_Parent (Prag, ASN);
1097 end if;
1098 end Make_Pragma_From_Boolean_Aspect;
1100 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1102 begin
1103 -- Must be visible in current scope
1105 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1106 return;
1107 end if;
1109 -- Look for aspect specification entries for this entity
1111 ASN := First_Rep_Item (E);
1112 while Present (ASN) loop
1113 if Nkind (ASN) = N_Aspect_Specification then
1114 exit when Entity (ASN) /= E;
1116 if Is_Delayed_Aspect (ASN) then
1117 A_Id := Get_Aspect_Id (ASN);
1119 case A_Id is
1121 -- For aspects whose expression is an optional Boolean, make
1122 -- the corresponding pragma at the freeze point.
1124 when Boolean_Aspects |
1125 Library_Unit_Aspects =>
1126 Make_Pragma_From_Boolean_Aspect (ASN);
1128 -- Special handling for aspects that don't correspond to
1129 -- pragmas/attributes.
1131 when Aspect_Default_Value |
1132 Aspect_Default_Component_Value =>
1133 Analyze_Aspect_Default_Value (ASN);
1135 -- Ditto for iterator aspects, because the corresponding
1136 -- attributes may not have been analyzed yet.
1138 when Aspect_Constant_Indexing |
1139 Aspect_Variable_Indexing |
1140 Aspect_Default_Iterator |
1141 Aspect_Iterator_Element =>
1142 Analyze (Expression (ASN));
1144 if Etype (Expression (ASN)) = Any_Type then
1145 Error_Msg_NE
1146 ("\aspect must be fully defined before & is frozen",
1147 ASN, E);
1148 end if;
1150 when Aspect_Iterable =>
1151 Validate_Iterable_Aspect (E, ASN);
1153 when others =>
1154 null;
1155 end case;
1157 Ritem := Aspect_Rep_Item (ASN);
1159 if Present (Ritem) then
1160 Analyze (Ritem);
1161 end if;
1162 end if;
1163 end if;
1165 Next_Rep_Item (ASN);
1166 end loop;
1168 -- This is where we inherit delayed rep aspects from our parent. Note
1169 -- that if we fell out of the above loop with ASN non-empty, it means
1170 -- we hit an aspect for an entity other than E, and it must be the
1171 -- type from which we were derived.
1173 if May_Inherit_Delayed_Rep_Aspects (E) then
1174 Inherit_Delayed_Rep_Aspects (ASN);
1175 end if;
1176 end Analyze_Aspects_At_Freeze_Point;
1178 -----------------------------------
1179 -- Analyze_Aspect_Specifications --
1180 -----------------------------------
1182 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1183 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1184 -- Establish linkages between an aspect and its corresponding
1185 -- pragma.
1187 procedure Insert_After_SPARK_Mode
1188 (Prag : Node_Id;
1189 Ins_Nod : Node_Id;
1190 Decls : List_Id);
1191 -- Subsidiary to the analysis of aspects Abstract_State, Ghost,
1192 -- Initializes, Initial_Condition and Refined_State. Insert node Prag
1193 -- before node Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip
1194 -- SPARK_Mode. Decls is the associated declarative list where Prag is to
1195 -- reside.
1197 procedure Insert_Pragma (Prag : Node_Id);
1198 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1199 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1200 -- Insert pragma Prag such that it mimics the placement of a source
1201 -- pragma of the same kind.
1203 -- procedure Proc (Formal : ...) with Global => ...;
1205 -- procedure Proc (Formal : ...);
1206 -- pragma Global (...);
1208 --------------
1209 -- Decorate --
1210 --------------
1212 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1213 begin
1214 Set_Aspect_Rep_Item (Asp, Prag);
1215 Set_Corresponding_Aspect (Prag, Asp);
1216 Set_From_Aspect_Specification (Prag);
1217 Set_Parent (Prag, Asp);
1218 end Decorate;
1220 -----------------------------
1221 -- Insert_After_SPARK_Mode --
1222 -----------------------------
1224 procedure Insert_After_SPARK_Mode
1225 (Prag : Node_Id;
1226 Ins_Nod : Node_Id;
1227 Decls : List_Id)
1229 Decl : Node_Id := Ins_Nod;
1231 begin
1232 -- Skip SPARK_Mode
1234 if Present (Decl)
1235 and then Nkind (Decl) = N_Pragma
1236 and then Pragma_Name (Decl) = Name_SPARK_Mode
1237 then
1238 Decl := Next (Decl);
1239 end if;
1241 if Present (Decl) then
1242 Insert_Before (Decl, Prag);
1244 -- Aitem acts as the last declaration
1246 else
1247 Append_To (Decls, Prag);
1248 end if;
1249 end Insert_After_SPARK_Mode;
1251 -------------------
1252 -- Insert_Pragma --
1253 -------------------
1255 procedure Insert_Pragma (Prag : Node_Id) is
1256 Aux : Node_Id;
1257 Decl : Node_Id;
1259 begin
1260 -- When the context is a library unit, the pragma is added to the
1261 -- Pragmas_After list.
1263 if Nkind (Parent (N)) = N_Compilation_Unit then
1264 Aux := Aux_Decls_Node (Parent (N));
1266 if No (Pragmas_After (Aux)) then
1267 Set_Pragmas_After (Aux, New_List);
1268 end if;
1270 Prepend (Prag, Pragmas_After (Aux));
1272 -- Pragmas associated with subprogram bodies are inserted in the
1273 -- declarative part.
1275 elsif Nkind (N) = N_Subprogram_Body then
1276 if Present (Declarations (N)) then
1278 -- Skip other internally generated pragmas from aspects to find
1279 -- the proper insertion point. As a result the order of pragmas
1280 -- is the same as the order of aspects.
1282 -- As precondition pragmas generated from conjuncts in the
1283 -- precondition aspect are presented in reverse order to
1284 -- Insert_Pragma, insert them in the correct order here by not
1285 -- skipping previously inserted precondition pragmas when the
1286 -- current pragma is a precondition.
1288 Decl := First (Declarations (N));
1289 while Present (Decl) loop
1290 if Nkind (Decl) = N_Pragma
1291 and then From_Aspect_Specification (Decl)
1292 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1293 and then
1294 Get_Pragma_Id (Prag) = Pragma_Precondition)
1295 then
1296 Next (Decl);
1297 else
1298 exit;
1299 end if;
1300 end loop;
1302 if Present (Decl) then
1303 Insert_Before (Decl, Prag);
1304 else
1305 Append (Prag, Declarations (N));
1306 end if;
1307 else
1308 Set_Declarations (N, New_List (Prag));
1309 end if;
1311 -- Default
1313 else
1314 Insert_After (N, Prag);
1315 end if;
1316 end Insert_Pragma;
1318 -- Local variables
1320 Aspect : Node_Id;
1321 Aitem : Node_Id;
1322 Ent : Node_Id;
1324 L : constant List_Id := Aspect_Specifications (N);
1326 Ins_Node : Node_Id := N;
1327 -- Insert pragmas/attribute definition clause after this node when no
1328 -- delayed analysis is required.
1330 -- Start of processing for Analyze_Aspect_Specifications
1332 -- The general processing involves building an attribute definition
1333 -- clause or a pragma node that corresponds to the aspect. Then in order
1334 -- to delay the evaluation of this aspect to the freeze point, we attach
1335 -- the corresponding pragma/attribute definition clause to the aspect
1336 -- specification node, which is then placed in the Rep Item chain. In
1337 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1338 -- and we evaluate the rep item at the freeze point. When the aspect
1339 -- doesn't have a corresponding pragma/attribute definition clause, then
1340 -- its analysis is simply delayed at the freeze point.
1342 -- Some special cases don't require delay analysis, thus the aspect is
1343 -- analyzed right now.
1345 -- Note that there is a special handling for Pre, Post, Test_Case,
1346 -- Contract_Cases aspects. In these cases, we do not have to worry
1347 -- about delay issues, since the pragmas themselves deal with delay
1348 -- of visibility for the expression analysis. Thus, we just insert
1349 -- the pragma after the node N.
1351 begin
1352 pragma Assert (Present (L));
1354 -- Loop through aspects
1356 Aspect := First (L);
1357 Aspect_Loop : while Present (Aspect) loop
1358 Analyze_One_Aspect : declare
1359 Expr : constant Node_Id := Expression (Aspect);
1360 Id : constant Node_Id := Identifier (Aspect);
1361 Loc : constant Source_Ptr := Sloc (Aspect);
1362 Nam : constant Name_Id := Chars (Id);
1363 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1364 Anod : Node_Id;
1366 Delay_Required : Boolean;
1367 -- Set False if delay is not required
1369 Eloc : Source_Ptr := No_Location;
1370 -- Source location of expression, modified when we split PPC's. It
1371 -- is set below when Expr is present.
1373 procedure Analyze_Aspect_External_Or_Link_Name;
1374 -- Perform analysis of the External_Name or Link_Name aspects
1376 procedure Analyze_Aspect_Implicit_Dereference;
1377 -- Perform analysis of the Implicit_Dereference aspects
1379 procedure Make_Aitem_Pragma
1380 (Pragma_Argument_Associations : List_Id;
1381 Pragma_Name : Name_Id);
1382 -- This is a wrapper for Make_Pragma used for converting aspects
1383 -- to pragmas. It takes care of Sloc (set from Loc) and building
1384 -- the pragma identifier from the given name. In addition the
1385 -- flags Class_Present and Split_PPC are set from the aspect
1386 -- node, as well as Is_Ignored. This routine also sets the
1387 -- From_Aspect_Specification in the resulting pragma node to
1388 -- True, and sets Corresponding_Aspect to point to the aspect.
1389 -- The resulting pragma is assigned to Aitem.
1391 ------------------------------------------
1392 -- Analyze_Aspect_External_Or_Link_Name --
1393 ------------------------------------------
1395 procedure Analyze_Aspect_External_Or_Link_Name is
1396 begin
1397 -- Verify that there is an Import/Export aspect defined for the
1398 -- entity. The processing of that aspect in turn checks that
1399 -- there is a Convention aspect declared. The pragma is
1400 -- constructed when processing the Convention aspect.
1402 declare
1403 A : Node_Id;
1405 begin
1406 A := First (L);
1407 while Present (A) loop
1408 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1409 Name_Import);
1410 Next (A);
1411 end loop;
1413 if No (A) then
1414 Error_Msg_N
1415 ("missing Import/Export for Link/External name",
1416 Aspect);
1417 end if;
1418 end;
1419 end Analyze_Aspect_External_Or_Link_Name;
1421 -----------------------------------------
1422 -- Analyze_Aspect_Implicit_Dereference --
1423 -----------------------------------------
1425 procedure Analyze_Aspect_Implicit_Dereference is
1426 begin
1427 if not Is_Type (E) or else not Has_Discriminants (E) then
1428 Error_Msg_N
1429 ("aspect must apply to a type with discriminants", N);
1431 else
1432 declare
1433 Disc : Entity_Id;
1435 begin
1436 Disc := First_Discriminant (E);
1437 while Present (Disc) loop
1438 if Chars (Expr) = Chars (Disc)
1439 and then Ekind (Etype (Disc)) =
1440 E_Anonymous_Access_Type
1441 then
1442 Set_Has_Implicit_Dereference (E);
1443 Set_Has_Implicit_Dereference (Disc);
1444 return;
1445 end if;
1447 Next_Discriminant (Disc);
1448 end loop;
1450 -- Error if no proper access discriminant.
1452 Error_Msg_NE
1453 ("not an access discriminant of&", Expr, E);
1454 end;
1455 end if;
1456 end Analyze_Aspect_Implicit_Dereference;
1458 -----------------------
1459 -- Make_Aitem_Pragma --
1460 -----------------------
1462 procedure Make_Aitem_Pragma
1463 (Pragma_Argument_Associations : List_Id;
1464 Pragma_Name : Name_Id)
1466 Args : List_Id := Pragma_Argument_Associations;
1468 begin
1469 -- We should never get here if aspect was disabled
1471 pragma Assert (not Is_Disabled (Aspect));
1473 -- Certain aspects allow for an optional name or expression. Do
1474 -- not generate a pragma with empty argument association list.
1476 if No (Args) or else No (Expression (First (Args))) then
1477 Args := No_List;
1478 end if;
1480 -- Build the pragma
1482 Aitem :=
1483 Make_Pragma (Loc,
1484 Pragma_Argument_Associations => Args,
1485 Pragma_Identifier =>
1486 Make_Identifier (Sloc (Id), Pragma_Name),
1487 Class_Present => Class_Present (Aspect),
1488 Split_PPC => Split_PPC (Aspect));
1490 -- Set additional semantic fields
1492 if Is_Ignored (Aspect) then
1493 Set_Is_Ignored (Aitem);
1494 elsif Is_Checked (Aspect) then
1495 Set_Is_Checked (Aitem);
1496 end if;
1498 Set_Corresponding_Aspect (Aitem, Aspect);
1499 Set_From_Aspect_Specification (Aitem, True);
1500 end Make_Aitem_Pragma;
1502 -- Start of processing for Analyze_One_Aspect
1504 begin
1505 -- Skip aspect if already analyzed, to avoid looping in some cases
1507 if Analyzed (Aspect) then
1508 goto Continue;
1509 end if;
1511 -- Skip looking at aspect if it is totally disabled. Just mark it
1512 -- as such for later reference in the tree. This also sets the
1513 -- Is_Ignored and Is_Checked flags appropriately.
1515 Check_Applicable_Policy (Aspect);
1517 if Is_Disabled (Aspect) then
1518 goto Continue;
1519 end if;
1521 -- Set the source location of expression, used in the case of
1522 -- a failed precondition/postcondition or invariant. Note that
1523 -- the source location of the expression is not usually the best
1524 -- choice here. For example, it gets located on the last AND
1525 -- keyword in a chain of boolean expressiond AND'ed together.
1526 -- It is best to put the message on the first character of the
1527 -- assertion, which is the effect of the First_Node call here.
1529 if Present (Expr) then
1530 Eloc := Sloc (First_Node (Expr));
1531 end if;
1533 -- Check restriction No_Implementation_Aspect_Specifications
1535 if Implementation_Defined_Aspect (A_Id) then
1536 Check_Restriction
1537 (No_Implementation_Aspect_Specifications, Aspect);
1538 end if;
1540 -- Check restriction No_Specification_Of_Aspect
1542 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1544 -- Mark aspect analyzed (actual analysis is delayed till later)
1546 Set_Analyzed (Aspect);
1547 Set_Entity (Aspect, E);
1548 Ent := New_Occurrence_Of (E, Sloc (Id));
1550 -- Check for duplicate aspect. Note that the Comes_From_Source
1551 -- test allows duplicate Pre/Post's that we generate internally
1552 -- to escape being flagged here.
1554 if No_Duplicates_Allowed (A_Id) then
1555 Anod := First (L);
1556 while Anod /= Aspect loop
1557 if Comes_From_Source (Aspect)
1558 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1559 then
1560 Error_Msg_Name_1 := Nam;
1561 Error_Msg_Sloc := Sloc (Anod);
1563 -- Case of same aspect specified twice
1565 if Class_Present (Anod) = Class_Present (Aspect) then
1566 if not Class_Present (Anod) then
1567 Error_Msg_NE
1568 ("aspect% for & previously given#",
1569 Id, E);
1570 else
1571 Error_Msg_NE
1572 ("aspect `%''Class` for & previously given#",
1573 Id, E);
1574 end if;
1575 end if;
1576 end if;
1578 Next (Anod);
1579 end loop;
1580 end if;
1582 -- Check some general restrictions on language defined aspects
1584 if not Implementation_Defined_Aspect (A_Id) then
1585 Error_Msg_Name_1 := Nam;
1587 -- Not allowed for renaming declarations
1589 if Nkind (N) in N_Renaming_Declaration then
1590 Error_Msg_N
1591 ("aspect % not allowed for renaming declaration",
1592 Aspect);
1593 end if;
1595 -- Not allowed for formal type declarations
1597 if Nkind (N) = N_Formal_Type_Declaration then
1598 Error_Msg_N
1599 ("aspect % not allowed for formal type declaration",
1600 Aspect);
1601 end if;
1602 end if;
1604 -- Copy expression for later processing by the procedures
1605 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1607 Set_Entity (Id, New_Copy_Tree (Expr));
1609 -- Set Delay_Required as appropriate to aspect
1611 case Aspect_Delay (A_Id) is
1612 when Always_Delay =>
1613 Delay_Required := True;
1615 when Never_Delay =>
1616 Delay_Required := False;
1618 when Rep_Aspect =>
1620 -- If expression has the form of an integer literal, then
1621 -- do not delay, since we know the value cannot change.
1622 -- This optimization catches most rep clause cases.
1624 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1625 or else (A_Id in Boolean_Aspects and then No (Expr))
1626 then
1627 Delay_Required := False;
1628 else
1629 Delay_Required := True;
1630 Set_Has_Delayed_Rep_Aspects (E);
1631 end if;
1632 end case;
1634 -- Processing based on specific aspect
1636 case A_Id is
1638 -- No_Aspect should be impossible
1640 when No_Aspect =>
1641 raise Program_Error;
1643 -- Case 1: Aspects corresponding to attribute definition
1644 -- clauses.
1646 when Aspect_Address |
1647 Aspect_Alignment |
1648 Aspect_Bit_Order |
1649 Aspect_Component_Size |
1650 Aspect_Constant_Indexing |
1651 Aspect_Default_Iterator |
1652 Aspect_Dispatching_Domain |
1653 Aspect_External_Tag |
1654 Aspect_Input |
1655 Aspect_Iterable |
1656 Aspect_Iterator_Element |
1657 Aspect_Machine_Radix |
1658 Aspect_Object_Size |
1659 Aspect_Output |
1660 Aspect_Read |
1661 Aspect_Scalar_Storage_Order |
1662 Aspect_Size |
1663 Aspect_Small |
1664 Aspect_Simple_Storage_Pool |
1665 Aspect_Storage_Pool |
1666 Aspect_Stream_Size |
1667 Aspect_Value_Size |
1668 Aspect_Variable_Indexing |
1669 Aspect_Write =>
1671 -- Indexing aspects apply only to tagged type
1673 if (A_Id = Aspect_Constant_Indexing
1674 or else
1675 A_Id = Aspect_Variable_Indexing)
1676 and then not (Is_Type (E)
1677 and then Is_Tagged_Type (E))
1678 then
1679 Error_Msg_N
1680 ("indexing aspect can only apply to a tagged type",
1681 Aspect);
1682 goto Continue;
1683 end if;
1685 -- For the case of aspect Address, we don't consider that we
1686 -- know the entity is never set in the source, since it is
1687 -- is likely aliasing is occurring.
1689 -- Note: one might think that the analysis of the resulting
1690 -- attribute definition clause would take care of that, but
1691 -- that's not the case since it won't be from source.
1693 if A_Id = Aspect_Address then
1694 Set_Never_Set_In_Source (E, False);
1695 end if;
1697 -- Correctness of the profile of a stream operation is
1698 -- verified at the freeze point, but we must detect the
1699 -- illegal specification of this aspect for a subtype now,
1700 -- to prevent malformed rep_item chains.
1702 if (A_Id = Aspect_Input or else
1703 A_Id = Aspect_Output or else
1704 A_Id = Aspect_Read or else
1705 A_Id = Aspect_Write)
1706 and not Is_First_Subtype (E)
1707 then
1708 Error_Msg_N
1709 ("local name must be a first subtype", Aspect);
1710 goto Continue;
1711 end if;
1713 -- Construct the attribute definition clause
1715 Aitem :=
1716 Make_Attribute_Definition_Clause (Loc,
1717 Name => Ent,
1718 Chars => Chars (Id),
1719 Expression => Relocate_Node (Expr));
1721 -- If the address is specified, then we treat the entity as
1722 -- referenced, to avoid spurious warnings. This is analogous
1723 -- to what is done with an attribute definition clause, but
1724 -- here we don't want to generate a reference because this
1725 -- is the point of definition of the entity.
1727 if A_Id = Aspect_Address then
1728 Set_Referenced (E);
1729 end if;
1731 -- Case 2: Aspects corresponding to pragmas
1733 -- Case 2a: Aspects corresponding to pragmas with two
1734 -- arguments, where the first argument is a local name
1735 -- referring to the entity, and the second argument is the
1736 -- aspect definition expression.
1738 -- Linker_Section/Suppress/Unsuppress
1740 when Aspect_Linker_Section |
1741 Aspect_Suppress |
1742 Aspect_Unsuppress =>
1744 Make_Aitem_Pragma
1745 (Pragma_Argument_Associations => New_List (
1746 Make_Pragma_Argument_Association (Loc,
1747 Expression => New_Occurrence_Of (E, Loc)),
1748 Make_Pragma_Argument_Association (Sloc (Expr),
1749 Expression => Relocate_Node (Expr))),
1750 Pragma_Name => Chars (Id));
1752 -- Synchronization
1754 -- Corresponds to pragma Implemented, construct the pragma
1756 when Aspect_Synchronization =>
1757 Make_Aitem_Pragma
1758 (Pragma_Argument_Associations => New_List (
1759 Make_Pragma_Argument_Association (Loc,
1760 Expression => New_Occurrence_Of (E, Loc)),
1761 Make_Pragma_Argument_Association (Sloc (Expr),
1762 Expression => Relocate_Node (Expr))),
1763 Pragma_Name => Name_Implemented);
1765 -- Attach_Handler
1767 when Aspect_Attach_Handler =>
1768 Make_Aitem_Pragma
1769 (Pragma_Argument_Associations => New_List (
1770 Make_Pragma_Argument_Association (Sloc (Ent),
1771 Expression => Ent),
1772 Make_Pragma_Argument_Association (Sloc (Expr),
1773 Expression => Relocate_Node (Expr))),
1774 Pragma_Name => Name_Attach_Handler);
1776 -- We need to insert this pragma into the tree to get proper
1777 -- processing and to look valid from a placement viewpoint.
1779 Insert_Pragma (Aitem);
1780 goto Continue;
1782 -- Dynamic_Predicate, Predicate, Static_Predicate
1784 when Aspect_Dynamic_Predicate |
1785 Aspect_Predicate |
1786 Aspect_Static_Predicate =>
1788 -- These aspects apply only to subtypes
1790 if not Is_Type (E) then
1791 Error_Msg_N
1792 ("predicate can only be specified for a subtype",
1793 Aspect);
1794 goto Continue;
1796 elsif Is_Incomplete_Type (E) then
1797 Error_Msg_N
1798 ("predicate cannot apply to incomplete view", Aspect);
1799 goto Continue;
1800 end if;
1802 -- Construct the pragma (always a pragma Predicate, with
1803 -- flags recording whether it is static/dynamic). We also
1804 -- set flags recording this in the type itself.
1806 Make_Aitem_Pragma
1807 (Pragma_Argument_Associations => New_List (
1808 Make_Pragma_Argument_Association (Sloc (Ent),
1809 Expression => Ent),
1810 Make_Pragma_Argument_Association (Sloc (Expr),
1811 Expression => Relocate_Node (Expr))),
1812 Pragma_Name => Name_Predicate);
1814 -- Mark type has predicates, and remember what kind of
1815 -- aspect lead to this predicate (we need this to access
1816 -- the right set of check policies later on).
1818 Set_Has_Predicates (E);
1820 if A_Id = Aspect_Dynamic_Predicate then
1821 Set_Has_Dynamic_Predicate_Aspect (E);
1822 elsif A_Id = Aspect_Static_Predicate then
1823 Set_Has_Static_Predicate_Aspect (E);
1824 end if;
1826 -- If the type is private, indicate that its completion
1827 -- has a freeze node, because that is the one that will
1828 -- be visible at freeze time.
1830 if Is_Private_Type (E) and then Present (Full_View (E)) then
1831 Set_Has_Predicates (Full_View (E));
1833 if A_Id = Aspect_Dynamic_Predicate then
1834 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1835 elsif A_Id = Aspect_Static_Predicate then
1836 Set_Has_Static_Predicate_Aspect (Full_View (E));
1837 end if;
1839 Set_Has_Delayed_Aspects (Full_View (E));
1840 Ensure_Freeze_Node (Full_View (E));
1841 end if;
1843 -- Case 2b: Aspects corresponding to pragmas with two
1844 -- arguments, where the second argument is a local name
1845 -- referring to the entity, and the first argument is the
1846 -- aspect definition expression.
1848 -- Convention
1850 when Aspect_Convention =>
1852 -- The aspect may be part of the specification of an import
1853 -- or export pragma. Scan the aspect list to gather the
1854 -- other components, if any. The name of the generated
1855 -- pragma is one of Convention/Import/Export.
1857 declare
1858 Args : constant List_Id := New_List (
1859 Make_Pragma_Argument_Association (Sloc (Expr),
1860 Expression => Relocate_Node (Expr)),
1861 Make_Pragma_Argument_Association (Sloc (Ent),
1862 Expression => Ent));
1864 Imp_Exp_Seen : Boolean := False;
1865 -- Flag set when aspect Import or Export has been seen
1867 Imp_Seen : Boolean := False;
1868 -- Flag set when aspect Import has been seen
1870 Asp : Node_Id;
1871 Asp_Nam : Name_Id;
1872 Extern_Arg : Node_Id;
1873 Link_Arg : Node_Id;
1874 Prag_Nam : Name_Id;
1876 begin
1877 Extern_Arg := Empty;
1878 Link_Arg := Empty;
1879 Prag_Nam := Chars (Id);
1881 Asp := First (L);
1882 while Present (Asp) loop
1883 Asp_Nam := Chars (Identifier (Asp));
1885 -- Aspects Import and Export take precedence over
1886 -- aspect Convention. As a result the generated pragma
1887 -- must carry the proper interfacing aspect's name.
1889 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1890 if Imp_Exp_Seen then
1891 Error_Msg_N ("conflicting", Asp);
1892 else
1893 Imp_Exp_Seen := True;
1895 if Asp_Nam = Name_Import then
1896 Imp_Seen := True;
1897 end if;
1898 end if;
1900 Prag_Nam := Asp_Nam;
1902 -- Aspect External_Name adds an extra argument to the
1903 -- generated pragma.
1905 elsif Asp_Nam = Name_External_Name then
1906 Extern_Arg :=
1907 Make_Pragma_Argument_Association (Loc,
1908 Chars => Asp_Nam,
1909 Expression => Relocate_Node (Expression (Asp)));
1911 -- Aspect Link_Name adds an extra argument to the
1912 -- generated pragma.
1914 elsif Asp_Nam = Name_Link_Name then
1915 Link_Arg :=
1916 Make_Pragma_Argument_Association (Loc,
1917 Chars => Asp_Nam,
1918 Expression => Relocate_Node (Expression (Asp)));
1919 end if;
1921 Next (Asp);
1922 end loop;
1924 -- Assemble the full argument list
1926 if Present (Extern_Arg) then
1927 Append_To (Args, Extern_Arg);
1928 end if;
1930 if Present (Link_Arg) then
1931 Append_To (Args, Link_Arg);
1932 end if;
1934 Make_Aitem_Pragma
1935 (Pragma_Argument_Associations => Args,
1936 Pragma_Name => Prag_Nam);
1938 -- Store the generated pragma Import in the related
1939 -- subprogram.
1941 if Imp_Seen and then Is_Subprogram (E) then
1942 Set_Import_Pragma (E, Aitem);
1943 end if;
1944 end;
1946 -- CPU, Interrupt_Priority, Priority
1948 -- These three aspects can be specified for a subprogram spec
1949 -- or body, in which case we analyze the expression and export
1950 -- the value of the aspect.
1952 -- Previously, we generated an equivalent pragma for bodies
1953 -- (note that the specs cannot contain these pragmas). The
1954 -- pragma was inserted ahead of local declarations, rather than
1955 -- after the body. This leads to a certain duplication between
1956 -- the processing performed for the aspect and the pragma, but
1957 -- given the straightforward handling required it is simpler
1958 -- to duplicate than to translate the aspect in the spec into
1959 -- a pragma in the declarative part of the body.
1961 when Aspect_CPU |
1962 Aspect_Interrupt_Priority |
1963 Aspect_Priority =>
1965 if Nkind_In (N, N_Subprogram_Body,
1966 N_Subprogram_Declaration)
1967 then
1968 -- Analyze the aspect expression
1970 Analyze_And_Resolve (Expr, Standard_Integer);
1972 -- Interrupt_Priority aspect not allowed for main
1973 -- subprograms. ARM D.1 does not forbid this explicitly,
1974 -- but ARM J.15.11 (6/3) does not permit pragma
1975 -- Interrupt_Priority for subprograms.
1977 if A_Id = Aspect_Interrupt_Priority then
1978 Error_Msg_N
1979 ("Interrupt_Priority aspect cannot apply to "
1980 & "subprogram", Expr);
1982 -- The expression must be static
1984 elsif not Is_OK_Static_Expression (Expr) then
1985 Flag_Non_Static_Expr
1986 ("aspect requires static expression!", Expr);
1988 -- Check whether this is the main subprogram. Issue a
1989 -- warning only if it is obviously not a main program
1990 -- (when it has parameters or when the subprogram is
1991 -- within a package).
1993 elsif Present (Parameter_Specifications
1994 (Specification (N)))
1995 or else not Is_Compilation_Unit (Defining_Entity (N))
1996 then
1997 -- See ARM D.1 (14/3) and D.16 (12/3)
1999 Error_Msg_N
2000 ("aspect applied to subprogram other than the "
2001 & "main subprogram has no effect??", Expr);
2003 -- Otherwise check in range and export the value
2005 -- For the CPU aspect
2007 elsif A_Id = Aspect_CPU then
2008 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2010 -- Value is correct so we export the value to make
2011 -- it available at execution time.
2013 Set_Main_CPU
2014 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2016 else
2017 Error_Msg_N
2018 ("main subprogram CPU is out of range", Expr);
2019 end if;
2021 -- For the Priority aspect
2023 elsif A_Id = Aspect_Priority then
2024 if Is_In_Range (Expr, RTE (RE_Priority)) then
2026 -- Value is correct so we export the value to make
2027 -- it available at execution time.
2029 Set_Main_Priority
2030 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2032 -- Ignore pragma if Relaxed_RM_Semantics to support
2033 -- other targets/non GNAT compilers.
2035 elsif not Relaxed_RM_Semantics then
2036 Error_Msg_N
2037 ("main subprogram priority is out of range",
2038 Expr);
2039 end if;
2040 end if;
2042 -- Load an arbitrary entity from System.Tasking.Stages
2043 -- or System.Tasking.Restricted.Stages (depending on
2044 -- the supported profile) to make sure that one of these
2045 -- packages is implicitly with'ed, since we need to have
2046 -- the tasking run time active for the pragma Priority to
2047 -- have any effect. Previously we with'ed the package
2048 -- System.Tasking, but this package does not trigger the
2049 -- required initialization of the run-time library.
2051 declare
2052 Discard : Entity_Id;
2053 begin
2054 if Restricted_Profile then
2055 Discard := RTE (RE_Activate_Restricted_Tasks);
2056 else
2057 Discard := RTE (RE_Activate_Tasks);
2058 end if;
2059 end;
2061 -- Handling for these Aspects in subprograms is complete
2063 goto Continue;
2065 -- For tasks
2067 else
2068 -- Pass the aspect as an attribute
2070 Aitem :=
2071 Make_Attribute_Definition_Clause (Loc,
2072 Name => Ent,
2073 Chars => Chars (Id),
2074 Expression => Relocate_Node (Expr));
2075 end if;
2077 -- Warnings
2079 when Aspect_Warnings =>
2080 Make_Aitem_Pragma
2081 (Pragma_Argument_Associations => New_List (
2082 Make_Pragma_Argument_Association (Sloc (Expr),
2083 Expression => Relocate_Node (Expr)),
2084 Make_Pragma_Argument_Association (Loc,
2085 Expression => New_Occurrence_Of (E, Loc))),
2086 Pragma_Name => Chars (Id));
2088 -- Case 2c: Aspects corresponding to pragmas with three
2089 -- arguments.
2091 -- Invariant aspects have a first argument that references the
2092 -- entity, a second argument that is the expression and a third
2093 -- argument that is an appropriate message.
2095 -- Invariant, Type_Invariant
2097 when Aspect_Invariant |
2098 Aspect_Type_Invariant =>
2100 -- Analysis of the pragma will verify placement legality:
2101 -- an invariant must apply to a private type, or appear in
2102 -- the private part of a spec and apply to a completion.
2104 Make_Aitem_Pragma
2105 (Pragma_Argument_Associations => New_List (
2106 Make_Pragma_Argument_Association (Sloc (Ent),
2107 Expression => Ent),
2108 Make_Pragma_Argument_Association (Sloc (Expr),
2109 Expression => Relocate_Node (Expr))),
2110 Pragma_Name => Name_Invariant);
2112 -- Add message unless exception messages are suppressed
2114 if not Opt.Exception_Locations_Suppressed then
2115 Append_To (Pragma_Argument_Associations (Aitem),
2116 Make_Pragma_Argument_Association (Eloc,
2117 Chars => Name_Message,
2118 Expression =>
2119 Make_String_Literal (Eloc,
2120 Strval => "failed invariant from "
2121 & Build_Location_String (Eloc))));
2122 end if;
2124 -- For Invariant case, insert immediately after the entity
2125 -- declaration. We do not have to worry about delay issues
2126 -- since the pragma processing takes care of this.
2128 Delay_Required := False;
2130 -- Case 2d : Aspects that correspond to a pragma with one
2131 -- argument.
2133 -- Abstract_State
2135 -- Aspect Abstract_State introduces implicit declarations for
2136 -- all state abstraction entities it defines. To emulate this
2137 -- behavior, insert the pragma at the beginning of the visible
2138 -- declarations of the related package so that it is analyzed
2139 -- immediately.
2141 when Aspect_Abstract_State => Abstract_State : declare
2142 Context : Node_Id := N;
2143 Decl : Node_Id;
2144 Decls : List_Id;
2146 begin
2147 -- When aspect Abstract_State appears on a generic package,
2148 -- it is propageted to the package instance. The context in
2149 -- this case is the instance spec.
2151 if Nkind (Context) = N_Package_Instantiation then
2152 Context := Instance_Spec (Context);
2153 end if;
2155 if Nkind_In (Context, N_Generic_Package_Declaration,
2156 N_Package_Declaration)
2157 then
2158 Make_Aitem_Pragma
2159 (Pragma_Argument_Associations => New_List (
2160 Make_Pragma_Argument_Association (Loc,
2161 Expression => Relocate_Node (Expr))),
2162 Pragma_Name => Name_Abstract_State);
2163 Decorate (Aspect, Aitem);
2165 Decls := Visible_Declarations (Specification (Context));
2167 -- In general pragma Abstract_State must be at the top
2168 -- of the existing visible declarations to emulate its
2169 -- source counterpart. The only exception to this is a
2170 -- generic instance in which case the pragma must be
2171 -- inserted after the association renamings.
2173 if Present (Decls) then
2174 Decl := First (Decls);
2176 -- The visible declarations of a generic instance have
2177 -- the following structure:
2179 -- <renamings of generic formals>
2180 -- <renamings of internally-generated spec and body>
2181 -- <first source declaration>
2183 -- The pragma must be inserted before the first source
2184 -- declaration, skip the instance "header".
2186 if Is_Generic_Instance (Defining_Entity (Context)) then
2187 while Present (Decl)
2188 and then not Comes_From_Source (Decl)
2189 loop
2190 Decl := Next (Decl);
2191 end loop;
2192 end if;
2194 -- When aspects Abstract_State, Ghost,
2195 -- Initial_Condition and Initializes are out of order,
2196 -- ensure that pragma SPARK_Mode is always at the top
2197 -- of the declarations to properly enabled/suppress
2198 -- errors.
2200 Insert_After_SPARK_Mode
2201 (Prag => Aitem,
2202 Ins_Nod => Decl,
2203 Decls => Decls);
2205 -- Otherwise the pragma forms a new declarative list
2207 else
2208 Set_Visible_Declarations
2209 (Specification (Context), New_List (Aitem));
2210 end if;
2212 else
2213 Error_Msg_NE
2214 ("aspect & must apply to a package declaration",
2215 Aspect, Id);
2216 end if;
2218 goto Continue;
2219 end Abstract_State;
2221 -- Aspect Default_Internal_Condition is never delayed because
2222 -- it is equivalent to a source pragma which appears after the
2223 -- related private type. To deal with forward references, the
2224 -- generated pragma is stored in the rep chain of the related
2225 -- private type as types do not carry contracts. The pragma is
2226 -- wrapped inside of a procedure at the freeze point of the
2227 -- private type's full view.
2229 when Aspect_Default_Initial_Condition =>
2230 Make_Aitem_Pragma
2231 (Pragma_Argument_Associations => New_List (
2232 Make_Pragma_Argument_Association (Loc,
2233 Expression => Relocate_Node (Expr))),
2234 Pragma_Name =>
2235 Name_Default_Initial_Condition);
2237 Decorate (Aspect, Aitem);
2238 Insert_Pragma (Aitem);
2239 goto Continue;
2241 -- Default_Storage_Pool
2243 when Aspect_Default_Storage_Pool =>
2244 Make_Aitem_Pragma
2245 (Pragma_Argument_Associations => New_List (
2246 Make_Pragma_Argument_Association (Loc,
2247 Expression => Relocate_Node (Expr))),
2248 Pragma_Name =>
2249 Name_Default_Storage_Pool);
2251 Decorate (Aspect, Aitem);
2252 Insert_Pragma (Aitem);
2253 goto Continue;
2255 -- Depends
2257 -- Aspect Depends is never delayed because it is equivalent to
2258 -- a source pragma which appears after the related subprogram.
2259 -- To deal with forward references, the generated pragma is
2260 -- stored in the contract of the related subprogram and later
2261 -- analyzed at the end of the declarative region. See routine
2262 -- Analyze_Depends_In_Decl_Part for details.
2264 when Aspect_Depends =>
2265 Make_Aitem_Pragma
2266 (Pragma_Argument_Associations => New_List (
2267 Make_Pragma_Argument_Association (Loc,
2268 Expression => Relocate_Node (Expr))),
2269 Pragma_Name => Name_Depends);
2271 Decorate (Aspect, Aitem);
2272 Insert_Pragma (Aitem);
2273 goto Continue;
2275 -- Aspect Extensions_Visible is never delayed because it is
2276 -- equivalent to a source pragma which appears after the
2277 -- related subprogram.
2279 when Aspect_Extensions_Visible =>
2280 Make_Aitem_Pragma
2281 (Pragma_Argument_Associations => New_List (
2282 Make_Pragma_Argument_Association (Loc,
2283 Expression => Relocate_Node (Expr))),
2284 Pragma_Name => Name_Extensions_Visible);
2286 Decorate (Aspect, Aitem);
2287 Insert_Pragma (Aitem);
2288 goto Continue;
2290 -- Aspect Ghost is never delayed because it is equivalent to a
2291 -- source pragma which appears at the top of [generic] package
2292 -- declarations or after an object, a [generic] subprogram, or
2293 -- a type declaration.
2295 when Aspect_Ghost => Ghost : declare
2296 Decls : List_Id;
2298 begin
2299 Make_Aitem_Pragma
2300 (Pragma_Argument_Associations => New_List (
2301 Make_Pragma_Argument_Association (Loc,
2302 Expression => Relocate_Node (Expr))),
2303 Pragma_Name => Name_Ghost);
2305 Decorate (Aspect, Aitem);
2307 -- When the aspect applies to a [generic] package, insert
2308 -- the pragma at the top of the visible declarations. This
2309 -- emulates the placement of a source pragma.
2311 if Nkind_In (N, N_Generic_Package_Declaration,
2312 N_Package_Declaration)
2313 then
2314 Decls := Visible_Declarations (Specification (N));
2316 if No (Decls) then
2317 Decls := New_List;
2318 Set_Visible_Declarations (N, Decls);
2319 end if;
2321 -- When aspects Abstract_State, Ghost, Initial_Condition
2322 -- and Initializes are out of order, ensure that pragma
2323 -- SPARK_Mode is always at the top of the declarations to
2324 -- properly enabled/suppress errors.
2326 Insert_After_SPARK_Mode
2327 (Prag => Aitem,
2328 Ins_Nod => First (Decls),
2329 Decls => Decls);
2331 -- Otherwise the context is an object, [generic] subprogram
2332 -- or type declaration.
2334 else
2335 Insert_Pragma (Aitem);
2336 end if;
2338 goto Continue;
2339 end Ghost;
2341 -- Global
2343 -- Aspect Global is never delayed because it is equivalent to
2344 -- a source pragma which appears after the related subprogram.
2345 -- To deal with forward references, the generated pragma is
2346 -- stored in the contract of the related subprogram and later
2347 -- analyzed at the end of the declarative region. See routine
2348 -- Analyze_Global_In_Decl_Part for details.
2350 when Aspect_Global =>
2351 Make_Aitem_Pragma
2352 (Pragma_Argument_Associations => New_List (
2353 Make_Pragma_Argument_Association (Loc,
2354 Expression => Relocate_Node (Expr))),
2355 Pragma_Name => Name_Global);
2357 Decorate (Aspect, Aitem);
2358 Insert_Pragma (Aitem);
2359 goto Continue;
2361 -- Initial_Condition
2363 -- Aspect Initial_Condition is never delayed because it is
2364 -- equivalent to a source pragma which appears after the
2365 -- related package. To deal with forward references, the
2366 -- generated pragma is stored in the contract of the related
2367 -- package and later analyzed at the end of the declarative
2368 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2369 -- for details.
2371 when Aspect_Initial_Condition => Initial_Condition : declare
2372 Context : Node_Id := N;
2373 Decls : List_Id;
2375 begin
2376 -- When aspect Initial_Condition appears on a generic
2377 -- package, it is propageted to the package instance. The
2378 -- context in this case is the instance spec.
2380 if Nkind (Context) = N_Package_Instantiation then
2381 Context := Instance_Spec (Context);
2382 end if;
2384 if Nkind_In (Context, N_Generic_Package_Declaration,
2385 N_Package_Declaration)
2386 then
2387 Decls := Visible_Declarations (Specification (Context));
2389 Make_Aitem_Pragma
2390 (Pragma_Argument_Associations => New_List (
2391 Make_Pragma_Argument_Association (Loc,
2392 Expression => Relocate_Node (Expr))),
2393 Pragma_Name =>
2394 Name_Initial_Condition);
2395 Decorate (Aspect, Aitem);
2397 if No (Decls) then
2398 Decls := New_List;
2399 Set_Visible_Declarations (Context, Decls);
2400 end if;
2402 -- When aspects Abstract_State, Ghost, Initial_Condition
2403 -- and Initializes are out of order, ensure that pragma
2404 -- SPARK_Mode is always at the top of the declarations to
2405 -- properly enabled/suppress errors.
2407 Insert_After_SPARK_Mode
2408 (Prag => Aitem,
2409 Ins_Nod => First (Decls),
2410 Decls => Decls);
2412 else
2413 Error_Msg_NE
2414 ("aspect & must apply to a package declaration",
2415 Aspect, Id);
2416 end if;
2418 goto Continue;
2419 end Initial_Condition;
2421 -- Initializes
2423 -- Aspect Initializes is never delayed because it is equivalent
2424 -- to a source pragma appearing after the related package. To
2425 -- deal with forward references, the generated pragma is stored
2426 -- in the contract of the related package and later analyzed at
2427 -- the end of the declarative region. For details, see routine
2428 -- Analyze_Initializes_In_Decl_Part.
2430 when Aspect_Initializes => Initializes : declare
2431 Context : Node_Id := N;
2432 Decls : List_Id;
2434 begin
2435 -- When aspect Initializes appears on a generic package,
2436 -- it is propageted to the package instance. The context
2437 -- in this case is the instance spec.
2439 if Nkind (Context) = N_Package_Instantiation then
2440 Context := Instance_Spec (Context);
2441 end if;
2443 if Nkind_In (Context, N_Generic_Package_Declaration,
2444 N_Package_Declaration)
2445 then
2446 Decls := Visible_Declarations (Specification (Context));
2448 Make_Aitem_Pragma
2449 (Pragma_Argument_Associations => New_List (
2450 Make_Pragma_Argument_Association (Loc,
2451 Expression => Relocate_Node (Expr))),
2452 Pragma_Name => Name_Initializes);
2453 Decorate (Aspect, Aitem);
2455 if No (Decls) then
2456 Decls := New_List;
2457 Set_Visible_Declarations (Context, Decls);
2458 end if;
2460 -- When aspects Abstract_State, Ghost, Initial_Condition
2461 -- and Initializes are out of order, ensure that pragma
2462 -- SPARK_Mode is always at the top of the declarations to
2463 -- properly enabled/suppress errors.
2465 Insert_After_SPARK_Mode
2466 (Prag => Aitem,
2467 Ins_Nod => First (Decls),
2468 Decls => Decls);
2470 else
2471 Error_Msg_NE
2472 ("aspect & must apply to a package declaration",
2473 Aspect, Id);
2474 end if;
2476 goto Continue;
2477 end Initializes;
2479 -- Obsolescent
2481 when Aspect_Obsolescent => declare
2482 Args : List_Id;
2484 begin
2485 if No (Expr) then
2486 Args := No_List;
2487 else
2488 Args := New_List (
2489 Make_Pragma_Argument_Association (Sloc (Expr),
2490 Expression => Relocate_Node (Expr)));
2491 end if;
2493 Make_Aitem_Pragma
2494 (Pragma_Argument_Associations => Args,
2495 Pragma_Name => Chars (Id));
2496 end;
2498 -- Part_Of
2500 when Aspect_Part_Of =>
2501 if Nkind_In (N, N_Object_Declaration,
2502 N_Package_Instantiation)
2503 then
2504 Make_Aitem_Pragma
2505 (Pragma_Argument_Associations => New_List (
2506 Make_Pragma_Argument_Association (Loc,
2507 Expression => Relocate_Node (Expr))),
2508 Pragma_Name => Name_Part_Of);
2510 else
2511 Error_Msg_NE
2512 ("aspect & must apply to a variable or package "
2513 & "instantiation", Aspect, Id);
2514 end if;
2516 -- SPARK_Mode
2518 when Aspect_SPARK_Mode => SPARK_Mode : declare
2519 Decls : List_Id;
2521 begin
2522 Make_Aitem_Pragma
2523 (Pragma_Argument_Associations => New_List (
2524 Make_Pragma_Argument_Association (Loc,
2525 Expression => Relocate_Node (Expr))),
2526 Pragma_Name => Name_SPARK_Mode);
2528 -- When the aspect appears on a package or a subprogram
2529 -- body, insert the generated pragma at the top of the body
2530 -- declarations to emulate the behavior of a source pragma.
2532 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2533 Decorate (Aspect, Aitem);
2535 Decls := Declarations (N);
2537 if No (Decls) then
2538 Decls := New_List;
2539 Set_Declarations (N, Decls);
2540 end if;
2542 Prepend_To (Decls, Aitem);
2543 goto Continue;
2545 -- When the aspect is associated with a [generic] package
2546 -- declaration, insert the generated pragma at the top of
2547 -- the visible declarations to emulate the behavior of a
2548 -- source pragma.
2550 elsif Nkind_In (N, N_Generic_Package_Declaration,
2551 N_Package_Declaration)
2552 then
2553 Decorate (Aspect, Aitem);
2555 Decls := Visible_Declarations (Specification (N));
2557 if No (Decls) then
2558 Decls := New_List;
2559 Set_Visible_Declarations (Specification (N), Decls);
2560 end if;
2562 Prepend_To (Decls, Aitem);
2563 goto Continue;
2564 end if;
2565 end SPARK_Mode;
2567 -- Refined_Depends
2569 -- Aspect Refined_Depends is never delayed because it is
2570 -- equivalent to a source pragma which appears in the
2571 -- declarations of the related subprogram body. To deal with
2572 -- forward references, the generated pragma is stored in the
2573 -- contract of the related subprogram body and later analyzed
2574 -- at the end of the declarative region. For details, see
2575 -- routine Analyze_Refined_Depends_In_Decl_Part.
2577 when Aspect_Refined_Depends =>
2578 Make_Aitem_Pragma
2579 (Pragma_Argument_Associations => New_List (
2580 Make_Pragma_Argument_Association (Loc,
2581 Expression => Relocate_Node (Expr))),
2582 Pragma_Name => Name_Refined_Depends);
2584 Decorate (Aspect, Aitem);
2585 Insert_Pragma (Aitem);
2586 goto Continue;
2588 -- Refined_Global
2590 -- Aspect Refined_Global is never delayed because it is
2591 -- equivalent to a source pragma which appears in the
2592 -- declarations of the related subprogram body. To deal with
2593 -- forward references, the generated pragma is stored in the
2594 -- contract of the related subprogram body and later analyzed
2595 -- at the end of the declarative region. For details, see
2596 -- routine Analyze_Refined_Global_In_Decl_Part.
2598 when Aspect_Refined_Global =>
2599 Make_Aitem_Pragma
2600 (Pragma_Argument_Associations => New_List (
2601 Make_Pragma_Argument_Association (Loc,
2602 Expression => Relocate_Node (Expr))),
2603 Pragma_Name => Name_Refined_Global);
2605 Decorate (Aspect, Aitem);
2606 Insert_Pragma (Aitem);
2607 goto Continue;
2609 -- Refined_Post
2611 when Aspect_Refined_Post =>
2612 Make_Aitem_Pragma
2613 (Pragma_Argument_Associations => New_List (
2614 Make_Pragma_Argument_Association (Loc,
2615 Expression => Relocate_Node (Expr))),
2616 Pragma_Name => Name_Refined_Post);
2618 -- Refined_State
2620 when Aspect_Refined_State => Refined_State : declare
2621 Decls : List_Id;
2623 begin
2624 -- The corresponding pragma for Refined_State is inserted in
2625 -- the declarations of the related package body. This action
2626 -- synchronizes both the source and from-aspect versions of
2627 -- the pragma.
2629 if Nkind (N) = N_Package_Body then
2630 Decls := Declarations (N);
2632 Make_Aitem_Pragma
2633 (Pragma_Argument_Associations => New_List (
2634 Make_Pragma_Argument_Association (Loc,
2635 Expression => Relocate_Node (Expr))),
2636 Pragma_Name => Name_Refined_State);
2637 Decorate (Aspect, Aitem);
2639 if No (Decls) then
2640 Decls := New_List;
2641 Set_Declarations (N, Decls);
2642 end if;
2644 -- Pragma Refined_State must be inserted after pragma
2645 -- SPARK_Mode in the tree. This ensures that any error
2646 -- messages dependent on SPARK_Mode will be properly
2647 -- enabled/suppressed.
2649 Insert_After_SPARK_Mode
2650 (Prag => Aitem,
2651 Ins_Nod => First (Decls),
2652 Decls => Decls);
2654 else
2655 Error_Msg_NE
2656 ("aspect & must apply to a package body", Aspect, Id);
2657 end if;
2659 goto Continue;
2660 end Refined_State;
2662 -- Relative_Deadline
2664 when Aspect_Relative_Deadline =>
2665 Make_Aitem_Pragma
2666 (Pragma_Argument_Associations => New_List (
2667 Make_Pragma_Argument_Association (Loc,
2668 Expression => Relocate_Node (Expr))),
2669 Pragma_Name => Name_Relative_Deadline);
2671 -- If the aspect applies to a task, the corresponding pragma
2672 -- must appear within its declarations, not after.
2674 if Nkind (N) = N_Task_Type_Declaration then
2675 declare
2676 Def : Node_Id;
2677 V : List_Id;
2679 begin
2680 if No (Task_Definition (N)) then
2681 Set_Task_Definition (N,
2682 Make_Task_Definition (Loc,
2683 Visible_Declarations => New_List,
2684 End_Label => Empty));
2685 end if;
2687 Def := Task_Definition (N);
2688 V := Visible_Declarations (Def);
2689 if not Is_Empty_List (V) then
2690 Insert_Before (First (V), Aitem);
2692 else
2693 Set_Visible_Declarations (Def, New_List (Aitem));
2694 end if;
2696 goto Continue;
2697 end;
2698 end if;
2700 -- Case 2e: Annotate aspect
2702 when Aspect_Annotate =>
2703 declare
2704 Args : List_Id;
2705 Pargs : List_Id;
2706 Arg : Node_Id;
2708 begin
2709 -- The argument can be a single identifier
2711 if Nkind (Expr) = N_Identifier then
2713 -- One level of parens is allowed
2715 if Paren_Count (Expr) > 1 then
2716 Error_Msg_F ("extra parentheses ignored", Expr);
2717 end if;
2719 Set_Paren_Count (Expr, 0);
2721 -- Add the single item to the list
2723 Args := New_List (Expr);
2725 -- Otherwise we must have an aggregate
2727 elsif Nkind (Expr) = N_Aggregate then
2729 -- Must be positional
2731 if Present (Component_Associations (Expr)) then
2732 Error_Msg_F
2733 ("purely positional aggregate required", Expr);
2734 goto Continue;
2735 end if;
2737 -- Must not be parenthesized
2739 if Paren_Count (Expr) /= 0 then
2740 Error_Msg_F ("extra parentheses ignored", Expr);
2741 end if;
2743 -- List of arguments is list of aggregate expressions
2745 Args := Expressions (Expr);
2747 -- Anything else is illegal
2749 else
2750 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2751 goto Continue;
2752 end if;
2754 -- Prepare pragma arguments
2756 Pargs := New_List;
2757 Arg := First (Args);
2758 while Present (Arg) loop
2759 Append_To (Pargs,
2760 Make_Pragma_Argument_Association (Sloc (Arg),
2761 Expression => Relocate_Node (Arg)));
2762 Next (Arg);
2763 end loop;
2765 Append_To (Pargs,
2766 Make_Pragma_Argument_Association (Sloc (Ent),
2767 Chars => Name_Entity,
2768 Expression => Ent));
2770 Make_Aitem_Pragma
2771 (Pragma_Argument_Associations => Pargs,
2772 Pragma_Name => Name_Annotate);
2773 end;
2775 -- Case 3 : Aspects that don't correspond to pragma/attribute
2776 -- definition clause.
2778 -- Case 3a: The aspects listed below don't correspond to
2779 -- pragmas/attributes but do require delayed analysis.
2781 -- Default_Value can only apply to a scalar type
2783 when Aspect_Default_Value =>
2784 if not Is_Scalar_Type (E) then
2785 Error_Msg_N
2786 ("aspect Default_Value must apply to a scalar type", N);
2787 end if;
2789 Aitem := Empty;
2791 -- Default_Component_Value can only apply to an array type
2792 -- with scalar components.
2794 when Aspect_Default_Component_Value =>
2795 if not (Is_Array_Type (E)
2796 and then Is_Scalar_Type (Component_Type (E)))
2797 then
2798 Error_Msg_N ("aspect Default_Component_Value can only "
2799 & "apply to an array of scalar components", N);
2800 end if;
2802 Aitem := Empty;
2804 -- Case 3b: The aspects listed below don't correspond to
2805 -- pragmas/attributes and don't need delayed analysis.
2807 -- Implicit_Dereference
2809 -- For Implicit_Dereference, External_Name and Link_Name, only
2810 -- the legality checks are done during the analysis, thus no
2811 -- delay is required.
2813 when Aspect_Implicit_Dereference =>
2814 Analyze_Aspect_Implicit_Dereference;
2815 goto Continue;
2817 -- External_Name, Link_Name
2819 when Aspect_External_Name |
2820 Aspect_Link_Name =>
2821 Analyze_Aspect_External_Or_Link_Name;
2822 goto Continue;
2824 -- Dimension
2826 when Aspect_Dimension =>
2827 Analyze_Aspect_Dimension (N, Id, Expr);
2828 goto Continue;
2830 -- Dimension_System
2832 when Aspect_Dimension_System =>
2833 Analyze_Aspect_Dimension_System (N, Id, Expr);
2834 goto Continue;
2836 -- Case 4: Aspects requiring special handling
2838 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2839 -- pragmas take care of the delay.
2841 -- Pre/Post
2843 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2844 -- with a first argument that is the expression, and a second
2845 -- argument that is an informative message if the test fails.
2846 -- This is inserted right after the declaration, to get the
2847 -- required pragma placement. The processing for the pragmas
2848 -- takes care of the required delay.
2850 when Pre_Post_Aspects => Pre_Post : declare
2851 Pname : Name_Id;
2853 begin
2854 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2855 Pname := Name_Precondition;
2856 else
2857 Pname := Name_Postcondition;
2858 end if;
2860 -- If the expressions is of the form A and then B, then
2861 -- we generate separate Pre/Post aspects for the separate
2862 -- clauses. Since we allow multiple pragmas, there is no
2863 -- problem in allowing multiple Pre/Post aspects internally.
2864 -- These should be treated in reverse order (B first and
2865 -- A second) since they are later inserted just after N in
2866 -- the order they are treated. This way, the pragma for A
2867 -- ends up preceding the pragma for B, which may have an
2868 -- importance for the error raised (either constraint error
2869 -- or precondition error).
2871 -- We do not do this for Pre'Class, since we have to put
2872 -- these conditions together in a complex OR expression.
2874 -- We do not do this in ASIS mode, as ASIS relies on the
2875 -- original node representing the complete expression, when
2876 -- retrieving it through the source aspect table.
2878 if not ASIS_Mode
2879 and then (Pname = Name_Postcondition
2880 or else not Class_Present (Aspect))
2881 then
2882 while Nkind (Expr) = N_And_Then loop
2883 Insert_After (Aspect,
2884 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2885 Identifier => Identifier (Aspect),
2886 Expression => Relocate_Node (Left_Opnd (Expr)),
2887 Class_Present => Class_Present (Aspect),
2888 Split_PPC => True));
2889 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2890 Eloc := Sloc (Expr);
2891 end loop;
2892 end if;
2894 -- Build the precondition/postcondition pragma
2896 -- Add note about why we do NOT need Copy_Tree here???
2898 Make_Aitem_Pragma
2899 (Pragma_Argument_Associations => New_List (
2900 Make_Pragma_Argument_Association (Eloc,
2901 Chars => Name_Check,
2902 Expression => Relocate_Node (Expr))),
2903 Pragma_Name => Pname);
2905 -- Add message unless exception messages are suppressed
2907 if not Opt.Exception_Locations_Suppressed then
2908 Append_To (Pragma_Argument_Associations (Aitem),
2909 Make_Pragma_Argument_Association (Eloc,
2910 Chars => Name_Message,
2911 Expression =>
2912 Make_String_Literal (Eloc,
2913 Strval => "failed "
2914 & Get_Name_String (Pname)
2915 & " from "
2916 & Build_Location_String (Eloc))));
2917 end if;
2919 Set_Is_Delayed_Aspect (Aspect);
2921 -- For Pre/Post cases, insert immediately after the entity
2922 -- declaration, since that is the required pragma placement.
2923 -- Note that for these aspects, we do not have to worry
2924 -- about delay issues, since the pragmas themselves deal
2925 -- with delay of visibility for the expression analysis.
2927 Insert_Pragma (Aitem);
2928 goto Continue;
2929 end Pre_Post;
2931 -- Test_Case
2933 when Aspect_Test_Case => Test_Case : declare
2934 Args : List_Id;
2935 Comp_Expr : Node_Id;
2936 Comp_Assn : Node_Id;
2937 New_Expr : Node_Id;
2939 begin
2940 Args := New_List;
2942 if Nkind (Parent (N)) = N_Compilation_Unit then
2943 Error_Msg_Name_1 := Nam;
2944 Error_Msg_N ("incorrect placement of aspect `%`", E);
2945 goto Continue;
2946 end if;
2948 if Nkind (Expr) /= N_Aggregate then
2949 Error_Msg_Name_1 := Nam;
2950 Error_Msg_NE
2951 ("wrong syntax for aspect `%` for &", Id, E);
2952 goto Continue;
2953 end if;
2955 -- Make pragma expressions refer to the original aspect
2956 -- expressions through the Original_Node link. This is used
2957 -- in semantic analysis for ASIS mode, so that the original
2958 -- expression also gets analyzed.
2960 Comp_Expr := First (Expressions (Expr));
2961 while Present (Comp_Expr) loop
2962 New_Expr := Relocate_Node (Comp_Expr);
2963 Set_Original_Node (New_Expr, Comp_Expr);
2964 Append_To (Args,
2965 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2966 Expression => New_Expr));
2967 Next (Comp_Expr);
2968 end loop;
2970 Comp_Assn := First (Component_Associations (Expr));
2971 while Present (Comp_Assn) loop
2972 if List_Length (Choices (Comp_Assn)) /= 1
2973 or else
2974 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2975 then
2976 Error_Msg_Name_1 := Nam;
2977 Error_Msg_NE
2978 ("wrong syntax for aspect `%` for &", Id, E);
2979 goto Continue;
2980 end if;
2982 New_Expr := Relocate_Node (Expression (Comp_Assn));
2983 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2984 Append_To (Args,
2985 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2986 Chars => Chars (First (Choices (Comp_Assn))),
2987 Expression => New_Expr));
2988 Next (Comp_Assn);
2989 end loop;
2991 -- Build the test-case pragma
2993 Make_Aitem_Pragma
2994 (Pragma_Argument_Associations => Args,
2995 Pragma_Name => Nam);
2996 end Test_Case;
2998 -- Contract_Cases
3000 when Aspect_Contract_Cases =>
3001 Make_Aitem_Pragma
3002 (Pragma_Argument_Associations => New_List (
3003 Make_Pragma_Argument_Association (Loc,
3004 Expression => Relocate_Node (Expr))),
3005 Pragma_Name => Nam);
3007 Decorate (Aspect, Aitem);
3008 Insert_Pragma (Aitem);
3009 goto Continue;
3011 -- Case 5: Special handling for aspects with an optional
3012 -- boolean argument.
3014 -- In the general case, the corresponding pragma cannot be
3015 -- generated yet because the evaluation of the boolean needs
3016 -- to be delayed till the freeze point.
3018 when Boolean_Aspects |
3019 Library_Unit_Aspects =>
3021 Set_Is_Boolean_Aspect (Aspect);
3023 -- Lock_Free aspect only apply to protected objects
3025 if A_Id = Aspect_Lock_Free then
3026 if Ekind (E) /= E_Protected_Type then
3027 Error_Msg_Name_1 := Nam;
3028 Error_Msg_N
3029 ("aspect % only applies to a protected object",
3030 Aspect);
3032 else
3033 -- Set the Uses_Lock_Free flag to True if there is no
3034 -- expression or if the expression is True. The
3035 -- evaluation of this aspect should be delayed to the
3036 -- freeze point (why???)
3038 if No (Expr) or else Is_True (Static_Boolean (Expr))
3039 then
3040 Set_Uses_Lock_Free (E);
3041 end if;
3043 Record_Rep_Item (E, Aspect);
3044 end if;
3046 goto Continue;
3048 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
3050 -- For the case of aspects Import and Export, we don't
3051 -- consider that we know the entity is never set in the
3052 -- source, since it is is likely modified outside the
3053 -- program.
3055 -- Note: one might think that the analysis of the
3056 -- resulting pragma would take care of that, but
3057 -- that's not the case since it won't be from source.
3059 if Ekind (E) = E_Variable then
3060 Set_Never_Set_In_Source (E, False);
3061 end if;
3063 -- In older versions of Ada the corresponding pragmas
3064 -- specified a Convention. In Ada 2012 the convention is
3065 -- specified as a separate aspect, and it is optional,
3066 -- given that it defaults to Convention_Ada. The code
3067 -- that verifed that there was a matching convention
3068 -- is now obsolete.
3070 -- Resolve the expression of an Import or Export here,
3071 -- and require it to be of type Boolean and static. This
3072 -- is not quite right, because in general this should be
3073 -- delayed, but that seems tricky for these, because
3074 -- normally Boolean aspects are replaced with pragmas at
3075 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
3076 -- but in the case of these aspects we can't generate
3077 -- a simple pragma with just the entity name. ???
3079 if not Present (Expr)
3080 or else Is_True (Static_Boolean (Expr))
3081 then
3082 if A_Id = Aspect_Import then
3083 Set_Is_Imported (E);
3085 -- An imported entity cannot have an explicit
3086 -- initialization.
3088 if Nkind (N) = N_Object_Declaration
3089 and then Present (Expression (N))
3090 then
3091 Error_Msg_N
3092 ("imported entities cannot be initialized "
3093 & "(RM B.1(24))", Expression (N));
3094 end if;
3096 elsif A_Id = Aspect_Export then
3097 Set_Is_Exported (E);
3098 end if;
3099 end if;
3101 goto Continue;
3102 end if;
3104 -- Library unit aspects require special handling in the case
3105 -- of a package declaration, the pragma needs to be inserted
3106 -- in the list of declarations for the associated package.
3107 -- There is no issue of visibility delay for these aspects.
3109 if A_Id in Library_Unit_Aspects
3110 and then
3111 Nkind_In (N, N_Package_Declaration,
3112 N_Generic_Package_Declaration)
3113 and then Nkind (Parent (N)) /= N_Compilation_Unit
3115 -- Aspect is legal on a local instantiation of a library-
3116 -- level generic unit.
3118 and then not Is_Generic_Instance (Defining_Entity (N))
3119 then
3120 Error_Msg_N
3121 ("incorrect context for library unit aspect&", Id);
3122 goto Continue;
3123 end if;
3125 -- External property aspects are Boolean by nature, but
3126 -- their pragmas must contain two arguments, the second
3127 -- being the optional Boolean expression.
3129 if A_Id = Aspect_Async_Readers or else
3130 A_Id = Aspect_Async_Writers or else
3131 A_Id = Aspect_Effective_Reads or else
3132 A_Id = Aspect_Effective_Writes
3133 then
3134 declare
3135 Args : List_Id;
3137 begin
3138 -- The first argument of the external property pragma
3139 -- is the related object.
3141 Args :=
3142 New_List (
3143 Make_Pragma_Argument_Association (Sloc (Ent),
3144 Expression => Ent));
3146 -- The second argument is the optional Boolean
3147 -- expression which must be propagated even if it
3148 -- evaluates to False as this has special semantic
3149 -- meaning.
3151 if Present (Expr) then
3152 Append_To (Args,
3153 Make_Pragma_Argument_Association (Loc,
3154 Expression => Relocate_Node (Expr)));
3155 end if;
3157 Make_Aitem_Pragma
3158 (Pragma_Argument_Associations => Args,
3159 Pragma_Name => Nam);
3160 end;
3162 -- Cases where we do not delay, includes all cases where the
3163 -- expression is missing other than the above cases.
3165 elsif not Delay_Required or else No (Expr) then
3166 Make_Aitem_Pragma
3167 (Pragma_Argument_Associations => New_List (
3168 Make_Pragma_Argument_Association (Sloc (Ent),
3169 Expression => Ent)),
3170 Pragma_Name => Chars (Id));
3171 Delay_Required := False;
3173 -- In general cases, the corresponding pragma/attribute
3174 -- definition clause will be inserted later at the freezing
3175 -- point, and we do not need to build it now.
3177 else
3178 Aitem := Empty;
3179 end if;
3181 -- Storage_Size
3183 -- This is special because for access types we need to generate
3184 -- an attribute definition clause. This also works for single
3185 -- task declarations, but it does not work for task type
3186 -- declarations, because we have the case where the expression
3187 -- references a discriminant of the task type. That can't use
3188 -- an attribute definition clause because we would not have
3189 -- visibility on the discriminant. For that case we must
3190 -- generate a pragma in the task definition.
3192 when Aspect_Storage_Size =>
3194 -- Task type case
3196 if Ekind (E) = E_Task_Type then
3197 declare
3198 Decl : constant Node_Id := Declaration_Node (E);
3200 begin
3201 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3203 -- If no task definition, create one
3205 if No (Task_Definition (Decl)) then
3206 Set_Task_Definition (Decl,
3207 Make_Task_Definition (Loc,
3208 Visible_Declarations => Empty_List,
3209 End_Label => Empty));
3210 end if;
3212 -- Create a pragma and put it at the start of the task
3213 -- definition for the task type declaration.
3215 Make_Aitem_Pragma
3216 (Pragma_Argument_Associations => New_List (
3217 Make_Pragma_Argument_Association (Loc,
3218 Expression => Relocate_Node (Expr))),
3219 Pragma_Name => Name_Storage_Size);
3221 Prepend
3222 (Aitem,
3223 Visible_Declarations (Task_Definition (Decl)));
3224 goto Continue;
3225 end;
3227 -- All other cases, generate attribute definition
3229 else
3230 Aitem :=
3231 Make_Attribute_Definition_Clause (Loc,
3232 Name => Ent,
3233 Chars => Chars (Id),
3234 Expression => Relocate_Node (Expr));
3235 end if;
3236 end case;
3238 -- Attach the corresponding pragma/attribute definition clause to
3239 -- the aspect specification node.
3241 if Present (Aitem) then
3242 Set_From_Aspect_Specification (Aitem);
3243 end if;
3245 -- In the context of a compilation unit, we directly put the
3246 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3247 -- node (no delay is required here) except for aspects on a
3248 -- subprogram body (see below) and a generic package, for which we
3249 -- need to introduce the pragma before building the generic copy
3250 -- (see sem_ch12), and for package instantiations, where the
3251 -- library unit pragmas are better handled early.
3253 if Nkind (Parent (N)) = N_Compilation_Unit
3254 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3255 then
3256 declare
3257 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3259 begin
3260 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3262 -- For a Boolean aspect, create the corresponding pragma if
3263 -- no expression or if the value is True.
3265 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3266 if Is_True (Static_Boolean (Expr)) then
3267 Make_Aitem_Pragma
3268 (Pragma_Argument_Associations => New_List (
3269 Make_Pragma_Argument_Association (Sloc (Ent),
3270 Expression => Ent)),
3271 Pragma_Name => Chars (Id));
3273 Set_From_Aspect_Specification (Aitem, True);
3274 Set_Corresponding_Aspect (Aitem, Aspect);
3276 else
3277 goto Continue;
3278 end if;
3279 end if;
3281 -- If the aspect is on a subprogram body (relevant aspect
3282 -- is Inline), add the pragma in front of the declarations.
3284 if Nkind (N) = N_Subprogram_Body then
3285 if No (Declarations (N)) then
3286 Set_Declarations (N, New_List);
3287 end if;
3289 Prepend (Aitem, Declarations (N));
3291 elsif Nkind (N) = N_Generic_Package_Declaration then
3292 if No (Visible_Declarations (Specification (N))) then
3293 Set_Visible_Declarations (Specification (N), New_List);
3294 end if;
3296 Prepend (Aitem,
3297 Visible_Declarations (Specification (N)));
3299 elsif Nkind (N) = N_Package_Instantiation then
3300 declare
3301 Spec : constant Node_Id :=
3302 Specification (Instance_Spec (N));
3303 begin
3304 if No (Visible_Declarations (Spec)) then
3305 Set_Visible_Declarations (Spec, New_List);
3306 end if;
3308 Prepend (Aitem, Visible_Declarations (Spec));
3309 end;
3311 else
3312 if No (Pragmas_After (Aux)) then
3313 Set_Pragmas_After (Aux, New_List);
3314 end if;
3316 Append (Aitem, Pragmas_After (Aux));
3317 end if;
3319 goto Continue;
3320 end;
3321 end if;
3323 -- The evaluation of the aspect is delayed to the freezing point.
3324 -- The pragma or attribute clause if there is one is then attached
3325 -- to the aspect specification which is put in the rep item list.
3327 if Delay_Required then
3328 if Present (Aitem) then
3329 Set_Is_Delayed_Aspect (Aitem);
3330 Set_Aspect_Rep_Item (Aspect, Aitem);
3331 Set_Parent (Aitem, Aspect);
3332 end if;
3334 Set_Is_Delayed_Aspect (Aspect);
3336 -- In the case of Default_Value, link the aspect to base type
3337 -- as well, even though it appears on a first subtype. This is
3338 -- mandated by the semantics of the aspect. Do not establish
3339 -- the link when processing the base type itself as this leads
3340 -- to a rep item circularity. Verify that we are dealing with
3341 -- a scalar type to prevent cascaded errors.
3343 if A_Id = Aspect_Default_Value
3344 and then Is_Scalar_Type (E)
3345 and then Base_Type (E) /= E
3346 then
3347 Set_Has_Delayed_Aspects (Base_Type (E));
3348 Record_Rep_Item (Base_Type (E), Aspect);
3349 end if;
3351 Set_Has_Delayed_Aspects (E);
3352 Record_Rep_Item (E, Aspect);
3354 -- When delay is not required and the context is a package or a
3355 -- subprogram body, insert the pragma in the body declarations.
3357 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3358 if No (Declarations (N)) then
3359 Set_Declarations (N, New_List);
3360 end if;
3362 -- The pragma is added before source declarations
3364 Prepend_To (Declarations (N), Aitem);
3366 -- When delay is not required and the context is not a compilation
3367 -- unit, we simply insert the pragma/attribute definition clause
3368 -- in sequence.
3370 else
3371 Insert_After (Ins_Node, Aitem);
3372 Ins_Node := Aitem;
3373 end if;
3374 end Analyze_One_Aspect;
3376 <<Continue>>
3377 Next (Aspect);
3378 end loop Aspect_Loop;
3380 if Has_Delayed_Aspects (E) then
3381 Ensure_Freeze_Node (E);
3382 end if;
3383 end Analyze_Aspect_Specifications;
3385 -----------------------
3386 -- Analyze_At_Clause --
3387 -----------------------
3389 -- An at clause is replaced by the corresponding Address attribute
3390 -- definition clause that is the preferred approach in Ada 95.
3392 procedure Analyze_At_Clause (N : Node_Id) is
3393 CS : constant Boolean := Comes_From_Source (N);
3395 begin
3396 -- This is an obsolescent feature
3398 Check_Restriction (No_Obsolescent_Features, N);
3400 if Warn_On_Obsolescent_Feature then
3401 Error_Msg_N
3402 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3403 Error_Msg_N
3404 ("\?j?use address attribute definition clause instead", N);
3405 end if;
3407 -- Rewrite as address clause
3409 Rewrite (N,
3410 Make_Attribute_Definition_Clause (Sloc (N),
3411 Name => Identifier (N),
3412 Chars => Name_Address,
3413 Expression => Expression (N)));
3415 -- We preserve Comes_From_Source, since logically the clause still comes
3416 -- from the source program even though it is changed in form.
3418 Set_Comes_From_Source (N, CS);
3420 -- Analyze rewritten clause
3422 Analyze_Attribute_Definition_Clause (N);
3423 end Analyze_At_Clause;
3425 -----------------------------------------
3426 -- Analyze_Attribute_Definition_Clause --
3427 -----------------------------------------
3429 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3430 Loc : constant Source_Ptr := Sloc (N);
3431 Nam : constant Node_Id := Name (N);
3432 Attr : constant Name_Id := Chars (N);
3433 Expr : constant Node_Id := Expression (N);
3434 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3436 Ent : Entity_Id;
3437 -- The entity of Nam after it is analyzed. In the case of an incomplete
3438 -- type, this is the underlying type.
3440 U_Ent : Entity_Id;
3441 -- The underlying entity to which the attribute applies. Generally this
3442 -- is the Underlying_Type of Ent, except in the case where the clause
3443 -- applies to full view of incomplete type or private type in which case
3444 -- U_Ent is just a copy of Ent.
3446 FOnly : Boolean := False;
3447 -- Reset to True for subtype specific attribute (Alignment, Size)
3448 -- and for stream attributes, i.e. those cases where in the call to
3449 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3450 -- are checked. Note that the case of stream attributes is not clear
3451 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3452 -- Storage_Size for derived task types, but that is also clearly
3453 -- unintentional.
3455 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3456 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3457 -- definition clauses.
3459 function Duplicate_Clause return Boolean;
3460 -- This routine checks if the aspect for U_Ent being given by attribute
3461 -- definition clause N is for an aspect that has already been specified,
3462 -- and if so gives an error message. If there is a duplicate, True is
3463 -- returned, otherwise if there is no error, False is returned.
3465 procedure Check_Indexing_Functions;
3466 -- Check that the function in Constant_Indexing or Variable_Indexing
3467 -- attribute has the proper type structure. If the name is overloaded,
3468 -- check that some interpretation is legal.
3470 procedure Check_Iterator_Functions;
3471 -- Check that there is a single function in Default_Iterator attribute
3472 -- has the proper type structure.
3474 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3475 -- Common legality check for the previous two
3477 -----------------------------------
3478 -- Analyze_Stream_TSS_Definition --
3479 -----------------------------------
3481 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3482 Subp : Entity_Id := Empty;
3483 I : Interp_Index;
3484 It : Interp;
3485 Pnam : Entity_Id;
3487 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3488 -- True for Read attribute, false for other attributes
3490 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3491 -- Return true if the entity is a subprogram with an appropriate
3492 -- profile for the attribute being defined.
3494 ----------------------
3495 -- Has_Good_Profile --
3496 ----------------------
3498 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3499 F : Entity_Id;
3500 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3501 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3502 (False => E_Procedure, True => E_Function);
3503 Typ : Entity_Id;
3505 begin
3506 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3507 return False;
3508 end if;
3510 F := First_Formal (Subp);
3512 if No (F)
3513 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3514 or else Designated_Type (Etype (F)) /=
3515 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3516 then
3517 return False;
3518 end if;
3520 if not Is_Function then
3521 Next_Formal (F);
3523 declare
3524 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3525 (False => E_In_Parameter,
3526 True => E_Out_Parameter);
3527 begin
3528 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3529 return False;
3530 end if;
3531 end;
3533 Typ := Etype (F);
3535 -- If the attribute specification comes from an aspect
3536 -- specification for a class-wide stream, the parameter must be
3537 -- a class-wide type of the entity to which the aspect applies.
3539 if From_Aspect_Specification (N)
3540 and then Class_Present (Parent (N))
3541 and then Is_Class_Wide_Type (Typ)
3542 then
3543 Typ := Etype (Typ);
3544 end if;
3546 else
3547 Typ := Etype (Subp);
3548 end if;
3550 -- Verify that the prefix of the attribute and the local name for
3551 -- the type of the formal match.
3553 if Base_Type (Typ) /= Base_Type (Ent)
3554 or else Present ((Next_Formal (F)))
3555 then
3556 return False;
3558 elsif not Is_Scalar_Type (Typ)
3559 and then not Is_First_Subtype (Typ)
3560 and then not Is_Class_Wide_Type (Typ)
3561 then
3562 return False;
3564 else
3565 return True;
3566 end if;
3567 end Has_Good_Profile;
3569 -- Start of processing for Analyze_Stream_TSS_Definition
3571 begin
3572 FOnly := True;
3574 if not Is_Type (U_Ent) then
3575 Error_Msg_N ("local name must be a subtype", Nam);
3576 return;
3578 elsif not Is_First_Subtype (U_Ent) then
3579 Error_Msg_N ("local name must be a first subtype", Nam);
3580 return;
3581 end if;
3583 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3585 -- If Pnam is present, it can be either inherited from an ancestor
3586 -- type (in which case it is legal to redefine it for this type), or
3587 -- be a previous definition of the attribute for the same type (in
3588 -- which case it is illegal).
3590 -- In the first case, it will have been analyzed already, and we
3591 -- can check that its profile does not match the expected profile
3592 -- for a stream attribute of U_Ent. In the second case, either Pnam
3593 -- has been analyzed (and has the expected profile), or it has not
3594 -- been analyzed yet (case of a type that has not been frozen yet
3595 -- and for which the stream attribute has been set using Set_TSS).
3597 if Present (Pnam)
3598 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3599 then
3600 Error_Msg_Sloc := Sloc (Pnam);
3601 Error_Msg_Name_1 := Attr;
3602 Error_Msg_N ("% attribute already defined #", Nam);
3603 return;
3604 end if;
3606 Analyze (Expr);
3608 if Is_Entity_Name (Expr) then
3609 if not Is_Overloaded (Expr) then
3610 if Has_Good_Profile (Entity (Expr)) then
3611 Subp := Entity (Expr);
3612 end if;
3614 else
3615 Get_First_Interp (Expr, I, It);
3616 while Present (It.Nam) loop
3617 if Has_Good_Profile (It.Nam) then
3618 Subp := It.Nam;
3619 exit;
3620 end if;
3622 Get_Next_Interp (I, It);
3623 end loop;
3624 end if;
3625 end if;
3627 if Present (Subp) then
3628 if Is_Abstract_Subprogram (Subp) then
3629 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3630 return;
3632 -- Test for stream subprogram for interface type being non-null
3634 elsif Is_Interface (U_Ent)
3635 and then not Inside_A_Generic
3636 and then Ekind (Subp) = E_Procedure
3637 and then
3638 not Null_Present
3639 (Specification
3640 (Unit_Declaration_Node (Ultimate_Alias (Subp))))
3641 then
3642 Error_Msg_N
3643 ("stream subprogram for interface type "
3644 & "must be null procedure", Expr);
3645 end if;
3647 Set_Entity (Expr, Subp);
3648 Set_Etype (Expr, Etype (Subp));
3650 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3652 else
3653 Error_Msg_Name_1 := Attr;
3654 Error_Msg_N ("incorrect expression for% attribute", Expr);
3655 end if;
3656 end Analyze_Stream_TSS_Definition;
3658 ------------------------------
3659 -- Check_Indexing_Functions --
3660 ------------------------------
3662 procedure Check_Indexing_Functions is
3663 Indexing_Found : Boolean := False;
3665 procedure Check_One_Function (Subp : Entity_Id);
3666 -- Check one possible interpretation. Sets Indexing_Found True if a
3667 -- legal indexing function is found.
3669 procedure Illegal_Indexing (Msg : String);
3670 -- Diagnose illegal indexing function if not overloaded. In the
3671 -- overloaded case indicate that no legal interpretation exists.
3673 ------------------------
3674 -- Check_One_Function --
3675 ------------------------
3677 procedure Check_One_Function (Subp : Entity_Id) is
3678 Default_Element : Node_Id;
3679 Ret_Type : constant Entity_Id := Etype (Subp);
3681 begin
3682 if not Is_Overloadable (Subp) then
3683 Illegal_Indexing ("illegal indexing function for type&");
3684 return;
3686 elsif Scope (Subp) /= Scope (Ent) then
3687 if Nkind (Expr) = N_Expanded_Name then
3689 -- Indexing function can't be declared elsewhere
3691 Illegal_Indexing
3692 ("indexing function must be declared in scope of type&");
3693 end if;
3695 return;
3697 elsif No (First_Formal (Subp)) then
3698 Illegal_Indexing
3699 ("Indexing requires a function that applies to type&");
3700 return;
3702 elsif No (Next_Formal (First_Formal (Subp))) then
3703 Illegal_Indexing
3704 ("indexing function must have at least two parameters");
3705 return;
3707 elsif Is_Derived_Type (Ent) then
3708 if (Attr = Name_Constant_Indexing
3709 and then Present
3710 (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing)))
3711 or else
3712 (Attr = Name_Variable_Indexing
3713 and then Present
3714 (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing)))
3715 then
3716 if Debug_Flag_Dot_XX then
3717 null;
3719 else
3720 Illegal_Indexing
3721 ("indexing function already inherited "
3722 & "from parent type");
3723 return;
3724 end if;
3725 end if;
3726 end if;
3728 if not Check_Primitive_Function (Subp)
3729 then
3730 Illegal_Indexing
3731 ("Indexing aspect requires a function that applies to type&");
3732 return;
3733 end if;
3735 -- If partial declaration exists, verify that it is not tagged.
3737 if Ekind (Current_Scope) = E_Package
3738 and then Has_Private_Declaration (Ent)
3739 and then From_Aspect_Specification (N)
3740 and then
3741 List_Containing (Parent (Ent)) =
3742 Private_Declarations
3743 (Specification (Unit_Declaration_Node (Current_Scope)))
3744 and then Nkind (N) = N_Attribute_Definition_Clause
3745 then
3746 declare
3747 Decl : Node_Id;
3749 begin
3750 Decl :=
3751 First (Visible_Declarations
3752 (Specification
3753 (Unit_Declaration_Node (Current_Scope))));
3755 while Present (Decl) loop
3756 if Nkind (Decl) = N_Private_Type_Declaration
3757 and then Ent = Full_View (Defining_Identifier (Decl))
3758 and then Tagged_Present (Decl)
3759 and then No (Aspect_Specifications (Decl))
3760 then
3761 Illegal_Indexing
3762 ("Indexing aspect cannot be specified on full view "
3763 & "if partial view is tagged");
3764 return;
3765 end if;
3767 Next (Decl);
3768 end loop;
3769 end;
3770 end if;
3772 -- An indexing function must return either the default element of
3773 -- the container, or a reference type. For variable indexing it
3774 -- must be the latter.
3776 Default_Element :=
3777 Find_Value_Of_Aspect
3778 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3780 if Present (Default_Element) then
3781 Analyze (Default_Element);
3783 if Is_Entity_Name (Default_Element)
3784 and then not Covers (Entity (Default_Element), Ret_Type)
3785 and then False
3786 then
3787 Illegal_Indexing
3788 ("wrong return type for indexing function");
3789 return;
3790 end if;
3791 end if;
3793 -- For variable_indexing the return type must be a reference type
3795 if Attr = Name_Variable_Indexing then
3796 if not Has_Implicit_Dereference (Ret_Type) then
3797 Illegal_Indexing
3798 ("variable indexing must return a reference type");
3799 return;
3801 elsif Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3802 then
3803 Illegal_Indexing
3804 ("variable indexing must return an access to variable");
3805 return;
3806 end if;
3808 else
3809 if Has_Implicit_Dereference (Ret_Type)
3810 and then not
3811 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
3812 then
3813 Illegal_Indexing
3814 ("constant indexing must return an access to constant");
3815 return;
3817 elsif Is_Access_Type (Etype (First_Formal (Subp)))
3818 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
3819 then
3820 Illegal_Indexing
3821 ("constant indexing must apply to an access to constant");
3822 return;
3823 end if;
3824 end if;
3826 -- All checks succeeded.
3828 Indexing_Found := True;
3829 end Check_One_Function;
3831 -----------------------
3832 -- Illegal_Indexing --
3833 -----------------------
3835 procedure Illegal_Indexing (Msg : String) is
3836 begin
3837 Error_Msg_NE (Msg, N, Ent);
3838 end Illegal_Indexing;
3840 -- Start of processing for Check_Indexing_Functions
3842 begin
3843 if In_Instance then
3844 return;
3845 end if;
3847 Analyze (Expr);
3849 if not Is_Overloaded (Expr) then
3850 Check_One_Function (Entity (Expr));
3852 else
3853 declare
3854 I : Interp_Index;
3855 It : Interp;
3857 begin
3858 Indexing_Found := False;
3859 Get_First_Interp (Expr, I, It);
3860 while Present (It.Nam) loop
3862 -- Note that analysis will have added the interpretation
3863 -- that corresponds to the dereference. We only check the
3864 -- subprogram itself.
3866 if Is_Overloadable (It.Nam) then
3867 Check_One_Function (It.Nam);
3868 end if;
3870 Get_Next_Interp (I, It);
3871 end loop;
3872 end;
3873 end if;
3875 if not Indexing_Found and then not Error_Posted (N) then
3876 Error_Msg_NE
3877 ("aspect Indexing requires a local function that "
3878 & "applies to type&", Expr, Ent);
3879 end if;
3880 end Check_Indexing_Functions;
3882 ------------------------------
3883 -- Check_Iterator_Functions --
3884 ------------------------------
3886 procedure Check_Iterator_Functions is
3887 Default : Entity_Id;
3889 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3890 -- Check one possible interpretation for validity
3892 ----------------------------
3893 -- Valid_Default_Iterator --
3894 ----------------------------
3896 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3897 Formal : Entity_Id;
3899 begin
3900 if not Check_Primitive_Function (Subp) then
3901 return False;
3902 else
3903 Formal := First_Formal (Subp);
3904 end if;
3906 -- False if any subsequent formal has no default expression
3908 Formal := Next_Formal (Formal);
3909 while Present (Formal) loop
3910 if No (Expression (Parent (Formal))) then
3911 return False;
3912 end if;
3914 Next_Formal (Formal);
3915 end loop;
3917 -- True if all subsequent formals have default expressions
3919 return True;
3920 end Valid_Default_Iterator;
3922 -- Start of processing for Check_Iterator_Functions
3924 begin
3925 Analyze (Expr);
3927 if not Is_Entity_Name (Expr) then
3928 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3929 end if;
3931 if not Is_Overloaded (Expr) then
3932 if not Check_Primitive_Function (Entity (Expr)) then
3933 Error_Msg_NE
3934 ("aspect Indexing requires a function that applies to type&",
3935 Entity (Expr), Ent);
3936 end if;
3938 if not Valid_Default_Iterator (Entity (Expr)) then
3939 Error_Msg_N ("improper function for default iterator", Expr);
3940 end if;
3942 else
3943 Default := Empty;
3944 declare
3945 I : Interp_Index;
3946 It : Interp;
3948 begin
3949 Get_First_Interp (Expr, I, It);
3950 while Present (It.Nam) loop
3951 if not Check_Primitive_Function (It.Nam)
3952 or else not Valid_Default_Iterator (It.Nam)
3953 then
3954 Remove_Interp (I);
3956 elsif Present (Default) then
3957 Error_Msg_N ("default iterator must be unique", Expr);
3959 else
3960 Default := It.Nam;
3961 end if;
3963 Get_Next_Interp (I, It);
3964 end loop;
3965 end;
3967 if Present (Default) then
3968 Set_Entity (Expr, Default);
3969 Set_Is_Overloaded (Expr, False);
3970 end if;
3971 end if;
3972 end Check_Iterator_Functions;
3974 -------------------------------
3975 -- Check_Primitive_Function --
3976 -------------------------------
3978 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3979 Ctrl : Entity_Id;
3981 begin
3982 if Ekind (Subp) /= E_Function then
3983 return False;
3984 end if;
3986 if No (First_Formal (Subp)) then
3987 return False;
3988 else
3989 Ctrl := Etype (First_Formal (Subp));
3990 end if;
3992 -- Type of formal may be the class-wide type, an access to such,
3993 -- or an incomplete view.
3995 if Ctrl = Ent
3996 or else Ctrl = Class_Wide_Type (Ent)
3997 or else
3998 (Ekind (Ctrl) = E_Anonymous_Access_Type
3999 and then (Designated_Type (Ctrl) = Ent
4000 or else
4001 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4002 or else
4003 (Ekind (Ctrl) = E_Incomplete_Type
4004 and then Full_View (Ctrl) = Ent)
4005 then
4006 null;
4007 else
4008 return False;
4009 end if;
4011 return True;
4012 end Check_Primitive_Function;
4014 ----------------------
4015 -- Duplicate_Clause --
4016 ----------------------
4018 function Duplicate_Clause return Boolean is
4019 A : Node_Id;
4021 begin
4022 -- Nothing to do if this attribute definition clause comes from
4023 -- an aspect specification, since we could not be duplicating an
4024 -- explicit clause, and we dealt with the case of duplicated aspects
4025 -- in Analyze_Aspect_Specifications.
4027 if From_Aspect_Specification (N) then
4028 return False;
4029 end if;
4031 -- Otherwise current clause may duplicate previous clause, or a
4032 -- previously given pragma or aspect specification for the same
4033 -- aspect.
4035 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4037 if Present (A) then
4038 Error_Msg_Name_1 := Chars (N);
4039 Error_Msg_Sloc := Sloc (A);
4041 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4042 return True;
4043 end if;
4045 return False;
4046 end Duplicate_Clause;
4048 -- Start of processing for Analyze_Attribute_Definition_Clause
4050 begin
4051 -- The following code is a defense against recursion. Not clear that
4052 -- this can happen legitimately, but perhaps some error situations can
4053 -- cause it, and we did see this recursion during testing.
4055 if Analyzed (N) then
4056 return;
4057 else
4058 Set_Analyzed (N, True);
4059 end if;
4061 -- Ignore some selected attributes in CodePeer mode since they are not
4062 -- relevant in this context.
4064 if CodePeer_Mode then
4065 case Id is
4067 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4068 -- internal representation of types by implicitly packing them.
4070 when Attribute_Component_Size =>
4071 Rewrite (N, Make_Null_Statement (Sloc (N)));
4072 return;
4074 when others =>
4075 null;
4076 end case;
4077 end if;
4079 -- Process Ignore_Rep_Clauses option
4081 if Ignore_Rep_Clauses then
4082 case Id is
4084 -- The following should be ignored. They do not affect legality
4085 -- and may be target dependent. The basic idea of -gnatI is to
4086 -- ignore any rep clauses that may be target dependent but do not
4087 -- affect legality (except possibly to be rejected because they
4088 -- are incompatible with the compilation target).
4090 when Attribute_Alignment |
4091 Attribute_Bit_Order |
4092 Attribute_Component_Size |
4093 Attribute_Machine_Radix |
4094 Attribute_Object_Size |
4095 Attribute_Size |
4096 Attribute_Small |
4097 Attribute_Stream_Size |
4098 Attribute_Value_Size =>
4099 Kill_Rep_Clause (N);
4100 return;
4102 -- The following should not be ignored, because in the first place
4103 -- they are reasonably portable, and should not cause problems
4104 -- in compiling code from another target, and also they do affect
4105 -- legality, e.g. failing to provide a stream attribute for a type
4106 -- may make a program illegal.
4108 when Attribute_External_Tag |
4109 Attribute_Input |
4110 Attribute_Output |
4111 Attribute_Read |
4112 Attribute_Simple_Storage_Pool |
4113 Attribute_Storage_Pool |
4114 Attribute_Storage_Size |
4115 Attribute_Write =>
4116 null;
4118 -- We do not do anything here with address clauses, they will be
4119 -- removed by Freeze later on, but for now, it works better to
4120 -- keep then in the tree.
4122 when Attribute_Address =>
4123 null;
4125 -- Other cases are errors ("attribute& cannot be set with
4126 -- definition clause"), which will be caught below.
4128 when others =>
4129 null;
4130 end case;
4131 end if;
4133 Analyze (Nam);
4134 Ent := Entity (Nam);
4136 if Rep_Item_Too_Early (Ent, N) then
4137 return;
4138 end if;
4140 -- Rep clause applies to full view of incomplete type or private type if
4141 -- we have one (if not, this is a premature use of the type). However,
4142 -- certain semantic checks need to be done on the specified entity (i.e.
4143 -- the private view), so we save it in Ent.
4145 if Is_Private_Type (Ent)
4146 and then Is_Derived_Type (Ent)
4147 and then not Is_Tagged_Type (Ent)
4148 and then No (Full_View (Ent))
4149 then
4150 -- If this is a private type whose completion is a derivation from
4151 -- another private type, there is no full view, and the attribute
4152 -- belongs to the type itself, not its underlying parent.
4154 U_Ent := Ent;
4156 elsif Ekind (Ent) = E_Incomplete_Type then
4158 -- The attribute applies to the full view, set the entity of the
4159 -- attribute definition accordingly.
4161 Ent := Underlying_Type (Ent);
4162 U_Ent := Ent;
4163 Set_Entity (Nam, Ent);
4165 else
4166 U_Ent := Underlying_Type (Ent);
4167 end if;
4169 -- Avoid cascaded error
4171 if Etype (Nam) = Any_Type then
4172 return;
4174 -- Must be declared in current scope or in case of an aspect
4175 -- specification, must be visible in current scope.
4177 elsif Scope (Ent) /= Current_Scope
4178 and then
4179 not (From_Aspect_Specification (N)
4180 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4181 then
4182 Error_Msg_N ("entity must be declared in this scope", Nam);
4183 return;
4185 -- Must not be a source renaming (we do have some cases where the
4186 -- expander generates a renaming, and those cases are OK, in such
4187 -- cases any attribute applies to the renamed object as well).
4189 elsif Is_Object (Ent)
4190 and then Present (Renamed_Object (Ent))
4191 then
4192 -- Case of renamed object from source, this is an error
4194 if Comes_From_Source (Renamed_Object (Ent)) then
4195 Get_Name_String (Chars (N));
4196 Error_Msg_Strlen := Name_Len;
4197 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4198 Error_Msg_N
4199 ("~ clause not allowed for a renaming declaration "
4200 & "(RM 13.1(6))", Nam);
4201 return;
4203 -- For the case of a compiler generated renaming, the attribute
4204 -- definition clause applies to the renamed object created by the
4205 -- expander. The easiest general way to handle this is to create a
4206 -- copy of the attribute definition clause for this object.
4208 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4209 Insert_Action (N,
4210 Make_Attribute_Definition_Clause (Loc,
4211 Name =>
4212 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4213 Chars => Chars (N),
4214 Expression => Duplicate_Subexpr (Expression (N))));
4216 -- If the renamed object is not an entity, it must be a dereference
4217 -- of an unconstrained function call, and we must introduce a new
4218 -- declaration to capture the expression. This is needed in the case
4219 -- of 'Alignment, where the original declaration must be rewritten.
4221 else
4222 pragma Assert
4223 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4224 null;
4225 end if;
4227 -- If no underlying entity, use entity itself, applies to some
4228 -- previously detected error cases ???
4230 elsif No (U_Ent) then
4231 U_Ent := Ent;
4233 -- Cannot specify for a subtype (exception Object/Value_Size)
4235 elsif Is_Type (U_Ent)
4236 and then not Is_First_Subtype (U_Ent)
4237 and then Id /= Attribute_Object_Size
4238 and then Id /= Attribute_Value_Size
4239 and then not From_At_Mod (N)
4240 then
4241 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4242 return;
4243 end if;
4245 Set_Entity (N, U_Ent);
4246 Check_Restriction_No_Use_Of_Attribute (N);
4248 -- Switch on particular attribute
4250 case Id is
4252 -------------
4253 -- Address --
4254 -------------
4256 -- Address attribute definition clause
4258 when Attribute_Address => Address : begin
4260 -- A little error check, catch for X'Address use X'Address;
4262 if Nkind (Nam) = N_Identifier
4263 and then Nkind (Expr) = N_Attribute_Reference
4264 and then Attribute_Name (Expr) = Name_Address
4265 and then Nkind (Prefix (Expr)) = N_Identifier
4266 and then Chars (Nam) = Chars (Prefix (Expr))
4267 then
4268 Error_Msg_NE
4269 ("address for & is self-referencing", Prefix (Expr), Ent);
4270 return;
4271 end if;
4273 -- Not that special case, carry on with analysis of expression
4275 Analyze_And_Resolve (Expr, RTE (RE_Address));
4277 -- Even when ignoring rep clauses we need to indicate that the
4278 -- entity has an address clause and thus it is legal to declare
4279 -- it imported. Freeze will get rid of the address clause later.
4281 if Ignore_Rep_Clauses then
4282 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4283 Record_Rep_Item (U_Ent, N);
4284 end if;
4286 return;
4287 end if;
4289 if Duplicate_Clause then
4290 null;
4292 -- Case of address clause for subprogram
4294 elsif Is_Subprogram (U_Ent) then
4295 if Has_Homonym (U_Ent) then
4296 Error_Msg_N
4297 ("address clause cannot be given " &
4298 "for overloaded subprogram",
4299 Nam);
4300 return;
4301 end if;
4303 -- For subprograms, all address clauses are permitted, and we
4304 -- mark the subprogram as having a deferred freeze so that Gigi
4305 -- will not elaborate it too soon.
4307 -- Above needs more comments, what is too soon about???
4309 Set_Has_Delayed_Freeze (U_Ent);
4311 -- Case of address clause for entry
4313 elsif Ekind (U_Ent) = E_Entry then
4314 if Nkind (Parent (N)) = N_Task_Body then
4315 Error_Msg_N
4316 ("entry address must be specified in task spec", Nam);
4317 return;
4318 end if;
4320 -- For entries, we require a constant address
4322 Check_Constant_Address_Clause (Expr, U_Ent);
4324 -- Special checks for task types
4326 if Is_Task_Type (Scope (U_Ent))
4327 and then Comes_From_Source (Scope (U_Ent))
4328 then
4329 Error_Msg_N
4330 ("??entry address declared for entry in task type", N);
4331 Error_Msg_N
4332 ("\??only one task can be declared of this type", N);
4333 end if;
4335 -- Entry address clauses are obsolescent
4337 Check_Restriction (No_Obsolescent_Features, N);
4339 if Warn_On_Obsolescent_Feature then
4340 Error_Msg_N
4341 ("?j?attaching interrupt to task entry is an " &
4342 "obsolescent feature (RM J.7.1)", N);
4343 Error_Msg_N
4344 ("\?j?use interrupt procedure instead", N);
4345 end if;
4347 -- Case of an address clause for a controlled object which we
4348 -- consider to be erroneous.
4350 elsif Is_Controlled (Etype (U_Ent))
4351 or else Has_Controlled_Component (Etype (U_Ent))
4352 then
4353 Error_Msg_NE
4354 ("??controlled object& must not be overlaid", Nam, U_Ent);
4355 Error_Msg_N
4356 ("\??Program_Error will be raised at run time", Nam);
4357 Insert_Action (Declaration_Node (U_Ent),
4358 Make_Raise_Program_Error (Loc,
4359 Reason => PE_Overlaid_Controlled_Object));
4360 return;
4362 -- Case of address clause for a (non-controlled) object
4364 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4365 declare
4366 Expr : constant Node_Id := Expression (N);
4367 O_Ent : Entity_Id;
4368 Off : Boolean;
4370 begin
4371 -- Exported variables cannot have an address clause, because
4372 -- this cancels the effect of the pragma Export.
4374 if Is_Exported (U_Ent) then
4375 Error_Msg_N
4376 ("cannot export object with address clause", Nam);
4377 return;
4378 end if;
4380 Find_Overlaid_Entity (N, O_Ent, Off);
4382 -- Overlaying controlled objects is erroneous
4384 if Present (O_Ent)
4385 and then (Has_Controlled_Component (Etype (O_Ent))
4386 or else Is_Controlled (Etype (O_Ent)))
4387 then
4388 Error_Msg_N
4389 ("??cannot overlay with controlled object", Expr);
4390 Error_Msg_N
4391 ("\??Program_Error will be raised at run time", Expr);
4392 Insert_Action (Declaration_Node (U_Ent),
4393 Make_Raise_Program_Error (Loc,
4394 Reason => PE_Overlaid_Controlled_Object));
4395 return;
4397 elsif Present (O_Ent)
4398 and then Ekind (U_Ent) = E_Constant
4399 and then not Is_Constant_Object (O_Ent)
4400 then
4401 Error_Msg_N ("??constant overlays a variable", Expr);
4403 -- Imported variables can have an address clause, but then
4404 -- the import is pretty meaningless except to suppress
4405 -- initializations, so we do not need such variables to
4406 -- be statically allocated (and in fact it causes trouble
4407 -- if the address clause is a local value).
4409 elsif Is_Imported (U_Ent) then
4410 Set_Is_Statically_Allocated (U_Ent, False);
4411 end if;
4413 -- We mark a possible modification of a variable with an
4414 -- address clause, since it is likely aliasing is occurring.
4416 Note_Possible_Modification (Nam, Sure => False);
4418 -- Here we are checking for explicit overlap of one variable
4419 -- by another, and if we find this then mark the overlapped
4420 -- variable as also being volatile to prevent unwanted
4421 -- optimizations. This is a significant pessimization so
4422 -- avoid it when there is an offset, i.e. when the object
4423 -- is composite; they cannot be optimized easily anyway.
4425 if Present (O_Ent)
4426 and then Is_Object (O_Ent)
4427 and then not Off
4429 -- The following test is an expedient solution to what
4430 -- is really a problem in CodePeer. Suppressing the
4431 -- Set_Treat_As_Volatile call here prevents later
4432 -- generation (in some cases) of trees that CodePeer
4433 -- should, but currently does not, handle correctly.
4434 -- This test should probably be removed when CodePeer
4435 -- is improved, just because we want the tree CodePeer
4436 -- analyzes to match the tree for which we generate code
4437 -- as closely as is practical. ???
4439 and then not CodePeer_Mode
4440 then
4441 -- ??? O_Ent might not be in current unit
4443 Set_Treat_As_Volatile (O_Ent);
4444 end if;
4446 -- Legality checks on the address clause for initialized
4447 -- objects is deferred until the freeze point, because
4448 -- a subsequent pragma might indicate that the object
4449 -- is imported and thus not initialized. Also, the address
4450 -- clause might involve entities that have yet to be
4451 -- elaborated.
4453 Set_Has_Delayed_Freeze (U_Ent);
4455 -- If an initialization call has been generated for this
4456 -- object, it needs to be deferred to after the freeze node
4457 -- we have just now added, otherwise GIGI will see a
4458 -- reference to the variable (as actual to the IP call)
4459 -- before its definition.
4461 declare
4462 Init_Call : constant Node_Id :=
4463 Remove_Init_Call (U_Ent, N);
4465 begin
4466 if Present (Init_Call) then
4467 Append_Freeze_Action (U_Ent, Init_Call);
4469 -- Reset Initialization_Statements pointer so that
4470 -- if there is a pragma Import further down, it can
4471 -- clear any default initialization.
4473 Set_Initialization_Statements (U_Ent, Init_Call);
4474 end if;
4475 end;
4477 if Is_Exported (U_Ent) then
4478 Error_Msg_N
4479 ("& cannot be exported if an address clause is given",
4480 Nam);
4481 Error_Msg_N
4482 ("\define and export a variable "
4483 & "that holds its address instead", Nam);
4484 end if;
4486 -- Entity has delayed freeze, so we will generate an
4487 -- alignment check at the freeze point unless suppressed.
4489 if not Range_Checks_Suppressed (U_Ent)
4490 and then not Alignment_Checks_Suppressed (U_Ent)
4491 then
4492 Set_Check_Address_Alignment (N);
4493 end if;
4495 -- Kill the size check code, since we are not allocating
4496 -- the variable, it is somewhere else.
4498 Kill_Size_Check_Code (U_Ent);
4500 -- If the address clause is of the form:
4502 -- for Y'Address use X'Address
4504 -- or
4506 -- Const : constant Address := X'Address;
4507 -- ...
4508 -- for Y'Address use Const;
4510 -- then we make an entry in the table for checking the size
4511 -- and alignment of the overlaying variable. We defer this
4512 -- check till after code generation to take full advantage
4513 -- of the annotation done by the back end.
4515 -- If the entity has a generic type, the check will be
4516 -- performed in the instance if the actual type justifies
4517 -- it, and we do not insert the clause in the table to
4518 -- prevent spurious warnings.
4520 -- Note: we used to test Comes_From_Source and only give
4521 -- this warning for source entities, but we have removed
4522 -- this test. It really seems bogus to generate overlays
4523 -- that would trigger this warning in generated code.
4524 -- Furthermore, by removing the test, we handle the
4525 -- aspect case properly.
4527 if Address_Clause_Overlay_Warnings
4528 and then Present (O_Ent)
4529 and then Is_Object (O_Ent)
4530 then
4531 if not Is_Generic_Type (Etype (U_Ent)) then
4532 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4533 end if;
4535 -- If variable overlays a constant view, and we are
4536 -- warning on overlays, then mark the variable as
4537 -- overlaying a constant (we will give warnings later
4538 -- if this variable is assigned).
4540 if Is_Constant_Object (O_Ent)
4541 and then Ekind (U_Ent) = E_Variable
4542 then
4543 Set_Overlays_Constant (U_Ent);
4544 end if;
4545 end if;
4546 end;
4548 -- Not a valid entity for an address clause
4550 else
4551 Error_Msg_N ("address cannot be given for &", Nam);
4552 end if;
4553 end Address;
4555 ---------------
4556 -- Alignment --
4557 ---------------
4559 -- Alignment attribute definition clause
4561 when Attribute_Alignment => Alignment : declare
4562 Align : constant Uint := Get_Alignment_Value (Expr);
4563 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4565 begin
4566 FOnly := True;
4568 if not Is_Type (U_Ent)
4569 and then Ekind (U_Ent) /= E_Variable
4570 and then Ekind (U_Ent) /= E_Constant
4571 then
4572 Error_Msg_N ("alignment cannot be given for &", Nam);
4574 elsif Duplicate_Clause then
4575 null;
4577 elsif Align /= No_Uint then
4578 Set_Has_Alignment_Clause (U_Ent);
4580 -- Tagged type case, check for attempt to set alignment to a
4581 -- value greater than Max_Align, and reset if so.
4583 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4584 Error_Msg_N
4585 ("alignment for & set to Maximum_Aligment??", Nam);
4586 Set_Alignment (U_Ent, Max_Align);
4588 -- All other cases
4590 else
4591 Set_Alignment (U_Ent, Align);
4592 end if;
4594 -- For an array type, U_Ent is the first subtype. In that case,
4595 -- also set the alignment of the anonymous base type so that
4596 -- other subtypes (such as the itypes for aggregates of the
4597 -- type) also receive the expected alignment.
4599 if Is_Array_Type (U_Ent) then
4600 Set_Alignment (Base_Type (U_Ent), Align);
4601 end if;
4602 end if;
4603 end Alignment;
4605 ---------------
4606 -- Bit_Order --
4607 ---------------
4609 -- Bit_Order attribute definition clause
4611 when Attribute_Bit_Order => Bit_Order : declare
4612 begin
4613 if not Is_Record_Type (U_Ent) then
4614 Error_Msg_N
4615 ("Bit_Order can only be defined for record type", Nam);
4617 elsif Duplicate_Clause then
4618 null;
4620 else
4621 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4623 if Etype (Expr) = Any_Type then
4624 return;
4626 elsif not Is_OK_Static_Expression (Expr) then
4627 Flag_Non_Static_Expr
4628 ("Bit_Order requires static expression!", Expr);
4630 else
4631 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4632 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4633 end if;
4634 end if;
4635 end if;
4636 end Bit_Order;
4638 --------------------
4639 -- Component_Size --
4640 --------------------
4642 -- Component_Size attribute definition clause
4644 when Attribute_Component_Size => Component_Size_Case : declare
4645 Csize : constant Uint := Static_Integer (Expr);
4646 Ctyp : Entity_Id;
4647 Btype : Entity_Id;
4648 Biased : Boolean;
4649 New_Ctyp : Entity_Id;
4650 Decl : Node_Id;
4652 begin
4653 if not Is_Array_Type (U_Ent) then
4654 Error_Msg_N ("component size requires array type", Nam);
4655 return;
4656 end if;
4658 Btype := Base_Type (U_Ent);
4659 Ctyp := Component_Type (Btype);
4661 if Duplicate_Clause then
4662 null;
4664 elsif Rep_Item_Too_Early (Btype, N) then
4665 null;
4667 elsif Csize /= No_Uint then
4668 Check_Size (Expr, Ctyp, Csize, Biased);
4670 -- For the biased case, build a declaration for a subtype that
4671 -- will be used to represent the biased subtype that reflects
4672 -- the biased representation of components. We need the subtype
4673 -- to get proper conversions on referencing elements of the
4674 -- array. Note: component size clauses are ignored in VM mode.
4676 if VM_Target = No_VM then
4677 if Biased then
4678 New_Ctyp :=
4679 Make_Defining_Identifier (Loc,
4680 Chars =>
4681 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4683 Decl :=
4684 Make_Subtype_Declaration (Loc,
4685 Defining_Identifier => New_Ctyp,
4686 Subtype_Indication =>
4687 New_Occurrence_Of (Component_Type (Btype), Loc));
4689 Set_Parent (Decl, N);
4690 Analyze (Decl, Suppress => All_Checks);
4692 Set_Has_Delayed_Freeze (New_Ctyp, False);
4693 Set_Esize (New_Ctyp, Csize);
4694 Set_RM_Size (New_Ctyp, Csize);
4695 Init_Alignment (New_Ctyp);
4696 Set_Is_Itype (New_Ctyp, True);
4697 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4699 Set_Component_Type (Btype, New_Ctyp);
4700 Set_Biased (New_Ctyp, N, "component size clause");
4701 end if;
4703 Set_Component_Size (Btype, Csize);
4705 -- For VM case, we ignore component size clauses
4707 else
4708 -- Give a warning unless we are in GNAT mode, in which case
4709 -- the warning is suppressed since it is not useful.
4711 if not GNAT_Mode then
4712 Error_Msg_N
4713 ("component size ignored in this configuration??", N);
4714 end if;
4715 end if;
4717 -- Deal with warning on overridden size
4719 if Warn_On_Overridden_Size
4720 and then Has_Size_Clause (Ctyp)
4721 and then RM_Size (Ctyp) /= Csize
4722 then
4723 Error_Msg_NE
4724 ("component size overrides size clause for&?S?", N, Ctyp);
4725 end if;
4727 Set_Has_Component_Size_Clause (Btype, True);
4728 Set_Has_Non_Standard_Rep (Btype, True);
4729 end if;
4730 end Component_Size_Case;
4732 -----------------------
4733 -- Constant_Indexing --
4734 -----------------------
4736 when Attribute_Constant_Indexing =>
4737 Check_Indexing_Functions;
4739 ---------
4740 -- CPU --
4741 ---------
4743 when Attribute_CPU => CPU :
4744 begin
4745 -- CPU attribute definition clause not allowed except from aspect
4746 -- specification.
4748 if From_Aspect_Specification (N) then
4749 if not Is_Task_Type (U_Ent) then
4750 Error_Msg_N ("CPU can only be defined for task", Nam);
4752 elsif Duplicate_Clause then
4753 null;
4755 else
4756 -- The expression must be analyzed in the special manner
4757 -- described in "Handling of Default and Per-Object
4758 -- Expressions" in sem.ads.
4760 -- The visibility to the discriminants must be restored
4762 Push_Scope_And_Install_Discriminants (U_Ent);
4763 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4764 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4766 if not Is_OK_Static_Expression (Expr) then
4767 Check_Restriction (Static_Priorities, Expr);
4768 end if;
4769 end if;
4771 else
4772 Error_Msg_N
4773 ("attribute& cannot be set with definition clause", N);
4774 end if;
4775 end CPU;
4777 ----------------------
4778 -- Default_Iterator --
4779 ----------------------
4781 when Attribute_Default_Iterator => Default_Iterator : declare
4782 Func : Entity_Id;
4784 begin
4785 if not Is_Tagged_Type (U_Ent) then
4786 Error_Msg_N
4787 ("aspect Default_Iterator applies to tagged type", Nam);
4788 end if;
4790 Check_Iterator_Functions;
4792 Analyze (Expr);
4794 if not Is_Entity_Name (Expr)
4795 or else Ekind (Entity (Expr)) /= E_Function
4796 then
4797 Error_Msg_N ("aspect Iterator must be a function", Expr);
4798 else
4799 Func := Entity (Expr);
4800 end if;
4802 if No (First_Formal (Func))
4803 or else Etype (First_Formal (Func)) /= U_Ent
4804 then
4805 Error_Msg_NE
4806 ("Default Iterator must be a primitive of&", Func, U_Ent);
4807 end if;
4808 end Default_Iterator;
4810 ------------------------
4811 -- Dispatching_Domain --
4812 ------------------------
4814 when Attribute_Dispatching_Domain => Dispatching_Domain :
4815 begin
4816 -- Dispatching_Domain attribute definition clause not allowed
4817 -- except from aspect specification.
4819 if From_Aspect_Specification (N) then
4820 if not Is_Task_Type (U_Ent) then
4821 Error_Msg_N ("Dispatching_Domain can only be defined" &
4822 "for task",
4823 Nam);
4825 elsif Duplicate_Clause then
4826 null;
4828 else
4829 -- The expression must be analyzed in the special manner
4830 -- described in "Handling of Default and Per-Object
4831 -- Expressions" in sem.ads.
4833 -- The visibility to the discriminants must be restored
4835 Push_Scope_And_Install_Discriminants (U_Ent);
4837 Preanalyze_Spec_Expression
4838 (Expr, RTE (RE_Dispatching_Domain));
4840 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4841 end if;
4843 else
4844 Error_Msg_N
4845 ("attribute& cannot be set with definition clause", N);
4846 end if;
4847 end Dispatching_Domain;
4849 ------------------
4850 -- External_Tag --
4851 ------------------
4853 when Attribute_External_Tag => External_Tag :
4854 begin
4855 if not Is_Tagged_Type (U_Ent) then
4856 Error_Msg_N ("should be a tagged type", Nam);
4857 end if;
4859 if Duplicate_Clause then
4860 null;
4862 else
4863 Analyze_And_Resolve (Expr, Standard_String);
4865 if not Is_OK_Static_Expression (Expr) then
4866 Flag_Non_Static_Expr
4867 ("static string required for tag name!", Nam);
4868 end if;
4870 if VM_Target /= No_VM then
4871 Error_Msg_Name_1 := Attr;
4872 Error_Msg_N
4873 ("% attribute unsupported in this configuration", Nam);
4874 end if;
4876 if not Is_Library_Level_Entity (U_Ent) then
4877 Error_Msg_NE
4878 ("??non-unique external tag supplied for &", N, U_Ent);
4879 Error_Msg_N
4880 ("\??same external tag applies to all "
4881 & "subprogram calls", N);
4882 Error_Msg_N
4883 ("\??corresponding internal tag cannot be obtained", N);
4884 end if;
4885 end if;
4886 end External_Tag;
4888 --------------------------
4889 -- Implicit_Dereference --
4890 --------------------------
4892 when Attribute_Implicit_Dereference =>
4894 -- Legality checks already performed at the point of the type
4895 -- declaration, aspect is not delayed.
4897 null;
4899 -----------
4900 -- Input --
4901 -----------
4903 when Attribute_Input =>
4904 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4905 Set_Has_Specified_Stream_Input (Ent);
4907 ------------------------
4908 -- Interrupt_Priority --
4909 ------------------------
4911 when Attribute_Interrupt_Priority => Interrupt_Priority :
4912 begin
4913 -- Interrupt_Priority attribute definition clause not allowed
4914 -- except from aspect specification.
4916 if From_Aspect_Specification (N) then
4917 if not Is_Concurrent_Type (U_Ent) then
4918 Error_Msg_N
4919 ("Interrupt_Priority can only be defined for task "
4920 & "and protected object", Nam);
4922 elsif Duplicate_Clause then
4923 null;
4925 else
4926 -- The expression must be analyzed in the special manner
4927 -- described in "Handling of Default and Per-Object
4928 -- Expressions" in sem.ads.
4930 -- The visibility to the discriminants must be restored
4932 Push_Scope_And_Install_Discriminants (U_Ent);
4934 Preanalyze_Spec_Expression
4935 (Expr, RTE (RE_Interrupt_Priority));
4937 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4938 end if;
4940 else
4941 Error_Msg_N
4942 ("attribute& cannot be set with definition clause", N);
4943 end if;
4944 end Interrupt_Priority;
4946 --------------
4947 -- Iterable --
4948 --------------
4950 when Attribute_Iterable =>
4951 Analyze (Expr);
4953 if Nkind (Expr) /= N_Aggregate then
4954 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
4955 end if;
4957 declare
4958 Assoc : Node_Id;
4960 begin
4961 Assoc := First (Component_Associations (Expr));
4962 while Present (Assoc) loop
4963 if not Is_Entity_Name (Expression (Assoc)) then
4964 Error_Msg_N ("value must be a function", Assoc);
4965 end if;
4967 Next (Assoc);
4968 end loop;
4969 end;
4971 ----------------------
4972 -- Iterator_Element --
4973 ----------------------
4975 when Attribute_Iterator_Element =>
4976 Analyze (Expr);
4978 if not Is_Entity_Name (Expr)
4979 or else not Is_Type (Entity (Expr))
4980 then
4981 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4982 end if;
4984 -------------------
4985 -- Machine_Radix --
4986 -------------------
4988 -- Machine radix attribute definition clause
4990 when Attribute_Machine_Radix => Machine_Radix : declare
4991 Radix : constant Uint := Static_Integer (Expr);
4993 begin
4994 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
4995 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
4997 elsif Duplicate_Clause then
4998 null;
5000 elsif Radix /= No_Uint then
5001 Set_Has_Machine_Radix_Clause (U_Ent);
5002 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5004 if Radix = 2 then
5005 null;
5006 elsif Radix = 10 then
5007 Set_Machine_Radix_10 (U_Ent);
5008 else
5009 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5010 end if;
5011 end if;
5012 end Machine_Radix;
5014 -----------------
5015 -- Object_Size --
5016 -----------------
5018 -- Object_Size attribute definition clause
5020 when Attribute_Object_Size => Object_Size : declare
5021 Size : constant Uint := Static_Integer (Expr);
5023 Biased : Boolean;
5024 pragma Warnings (Off, Biased);
5026 begin
5027 if not Is_Type (U_Ent) then
5028 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5030 elsif Duplicate_Clause then
5031 null;
5033 else
5034 Check_Size (Expr, U_Ent, Size, Biased);
5036 if Is_Scalar_Type (U_Ent) then
5037 if Size /= 8 and then Size /= 16 and then Size /= 32
5038 and then UI_Mod (Size, 64) /= 0
5039 then
5040 Error_Msg_N
5041 ("Object_Size must be 8, 16, 32, or multiple of 64",
5042 Expr);
5043 end if;
5045 elsif Size mod 8 /= 0 then
5046 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5047 end if;
5049 Set_Esize (U_Ent, Size);
5050 Set_Has_Object_Size_Clause (U_Ent);
5051 Alignment_Check_For_Size_Change (U_Ent, Size);
5052 end if;
5053 end Object_Size;
5055 ------------
5056 -- Output --
5057 ------------
5059 when Attribute_Output =>
5060 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5061 Set_Has_Specified_Stream_Output (Ent);
5063 --------------
5064 -- Priority --
5065 --------------
5067 when Attribute_Priority => Priority :
5068 begin
5069 -- Priority attribute definition clause not allowed except from
5070 -- aspect specification.
5072 if From_Aspect_Specification (N) then
5073 if not (Is_Concurrent_Type (U_Ent)
5074 or else Ekind (U_Ent) = E_Procedure)
5075 then
5076 Error_Msg_N
5077 ("Priority can only be defined for task and protected "
5078 & "object", Nam);
5080 elsif Duplicate_Clause then
5081 null;
5083 else
5084 -- The expression must be analyzed in the special manner
5085 -- described in "Handling of Default and Per-Object
5086 -- Expressions" in sem.ads.
5088 -- The visibility to the discriminants must be restored
5090 Push_Scope_And_Install_Discriminants (U_Ent);
5091 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5092 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5094 if not Is_OK_Static_Expression (Expr) then
5095 Check_Restriction (Static_Priorities, Expr);
5096 end if;
5097 end if;
5099 else
5100 Error_Msg_N
5101 ("attribute& cannot be set with definition clause", N);
5102 end if;
5103 end Priority;
5105 ----------
5106 -- Read --
5107 ----------
5109 when Attribute_Read =>
5110 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5111 Set_Has_Specified_Stream_Read (Ent);
5113 --------------------------
5114 -- Scalar_Storage_Order --
5115 --------------------------
5117 -- Scalar_Storage_Order attribute definition clause
5119 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5120 begin
5121 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5122 Error_Msg_N
5123 ("Scalar_Storage_Order can only be defined for "
5124 & "record or array type", Nam);
5126 elsif Duplicate_Clause then
5127 null;
5129 else
5130 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5132 if Etype (Expr) = Any_Type then
5133 return;
5135 elsif not Is_OK_Static_Expression (Expr) then
5136 Flag_Non_Static_Expr
5137 ("Scalar_Storage_Order requires static expression!", Expr);
5139 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5141 -- Here for the case of a non-default (i.e. non-confirming)
5142 -- Scalar_Storage_Order attribute definition.
5144 if Support_Nondefault_SSO_On_Target then
5145 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5146 else
5147 Error_Msg_N
5148 ("non-default Scalar_Storage_Order "
5149 & "not supported on target", Expr);
5150 end if;
5151 end if;
5153 -- Clear SSO default indications since explicit setting of the
5154 -- order overrides the defaults.
5156 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5157 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5158 end if;
5159 end Scalar_Storage_Order;
5161 ----------
5162 -- Size --
5163 ----------
5165 -- Size attribute definition clause
5167 when Attribute_Size => Size : declare
5168 Size : constant Uint := Static_Integer (Expr);
5169 Etyp : Entity_Id;
5170 Biased : Boolean;
5172 begin
5173 FOnly := True;
5175 if Duplicate_Clause then
5176 null;
5178 elsif not Is_Type (U_Ent)
5179 and then Ekind (U_Ent) /= E_Variable
5180 and then Ekind (U_Ent) /= E_Constant
5181 then
5182 Error_Msg_N ("size cannot be given for &", Nam);
5184 elsif Is_Array_Type (U_Ent)
5185 and then not Is_Constrained (U_Ent)
5186 then
5187 Error_Msg_N
5188 ("size cannot be given for unconstrained array", Nam);
5190 elsif Size /= No_Uint then
5191 if VM_Target /= No_VM and then not GNAT_Mode then
5193 -- Size clause is not handled properly on VM targets.
5194 -- Display a warning unless we are in GNAT mode, in which
5195 -- case this is useless.
5197 Error_Msg_N
5198 ("size clauses are ignored in this configuration??", N);
5199 end if;
5201 if Is_Type (U_Ent) then
5202 Etyp := U_Ent;
5203 else
5204 Etyp := Etype (U_Ent);
5205 end if;
5207 -- Check size, note that Gigi is in charge of checking that the
5208 -- size of an array or record type is OK. Also we do not check
5209 -- the size in the ordinary fixed-point case, since it is too
5210 -- early to do so (there may be subsequent small clause that
5211 -- affects the size). We can check the size if a small clause
5212 -- has already been given.
5214 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5215 or else Has_Small_Clause (U_Ent)
5216 then
5217 Check_Size (Expr, Etyp, Size, Biased);
5218 Set_Biased (U_Ent, N, "size clause", Biased);
5219 end if;
5221 -- For types set RM_Size and Esize if possible
5223 if Is_Type (U_Ent) then
5224 Set_RM_Size (U_Ent, Size);
5226 -- For elementary types, increase Object_Size to power of 2,
5227 -- but not less than a storage unit in any case (normally
5228 -- this means it will be byte addressable).
5230 -- For all other types, nothing else to do, we leave Esize
5231 -- (object size) unset, the back end will set it from the
5232 -- size and alignment in an appropriate manner.
5234 -- In both cases, we check whether the alignment must be
5235 -- reset in the wake of the size change.
5237 if Is_Elementary_Type (U_Ent) then
5238 if Size <= System_Storage_Unit then
5239 Init_Esize (U_Ent, System_Storage_Unit);
5240 elsif Size <= 16 then
5241 Init_Esize (U_Ent, 16);
5242 elsif Size <= 32 then
5243 Init_Esize (U_Ent, 32);
5244 else
5245 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5246 end if;
5248 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5249 else
5250 Alignment_Check_For_Size_Change (U_Ent, Size);
5251 end if;
5253 -- For objects, set Esize only
5255 else
5256 if Is_Elementary_Type (Etyp) then
5257 if Size /= System_Storage_Unit
5258 and then
5259 Size /= System_Storage_Unit * 2
5260 and then
5261 Size /= System_Storage_Unit * 4
5262 and then
5263 Size /= System_Storage_Unit * 8
5264 then
5265 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5266 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5267 Error_Msg_N
5268 ("size for primitive object must be a power of 2"
5269 & " in the range ^-^", N);
5270 end if;
5271 end if;
5273 Set_Esize (U_Ent, Size);
5274 end if;
5276 Set_Has_Size_Clause (U_Ent);
5277 end if;
5278 end Size;
5280 -----------
5281 -- Small --
5282 -----------
5284 -- Small attribute definition clause
5286 when Attribute_Small => Small : declare
5287 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5288 Small : Ureal;
5290 begin
5291 Analyze_And_Resolve (Expr, Any_Real);
5293 if Etype (Expr) = Any_Type then
5294 return;
5296 elsif not Is_OK_Static_Expression (Expr) then
5297 Flag_Non_Static_Expr
5298 ("small requires static expression!", Expr);
5299 return;
5301 else
5302 Small := Expr_Value_R (Expr);
5304 if Small <= Ureal_0 then
5305 Error_Msg_N ("small value must be greater than zero", Expr);
5306 return;
5307 end if;
5309 end if;
5311 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5312 Error_Msg_N
5313 ("small requires an ordinary fixed point type", Nam);
5315 elsif Has_Small_Clause (U_Ent) then
5316 Error_Msg_N ("small already given for &", Nam);
5318 elsif Small > Delta_Value (U_Ent) then
5319 Error_Msg_N
5320 ("small value must not be greater than delta value", Nam);
5322 else
5323 Set_Small_Value (U_Ent, Small);
5324 Set_Small_Value (Implicit_Base, Small);
5325 Set_Has_Small_Clause (U_Ent);
5326 Set_Has_Small_Clause (Implicit_Base);
5327 Set_Has_Non_Standard_Rep (Implicit_Base);
5328 end if;
5329 end Small;
5331 ------------------
5332 -- Storage_Pool --
5333 ------------------
5335 -- Storage_Pool attribute definition clause
5337 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5338 Pool : Entity_Id;
5339 T : Entity_Id;
5341 begin
5342 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5343 Error_Msg_N
5344 ("storage pool cannot be given for access-to-subprogram type",
5345 Nam);
5346 return;
5348 elsif not
5349 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5350 then
5351 Error_Msg_N
5352 ("storage pool can only be given for access types", Nam);
5353 return;
5355 elsif Is_Derived_Type (U_Ent) then
5356 Error_Msg_N
5357 ("storage pool cannot be given for a derived access type",
5358 Nam);
5360 elsif Duplicate_Clause then
5361 return;
5363 elsif Present (Associated_Storage_Pool (U_Ent)) then
5364 Error_Msg_N ("storage pool already given for &", Nam);
5365 return;
5366 end if;
5368 -- Check for Storage_Size previously given
5370 declare
5371 SS : constant Node_Id :=
5372 Get_Attribute_Definition_Clause
5373 (U_Ent, Attribute_Storage_Size);
5374 begin
5375 if Present (SS) then
5376 Check_Pool_Size_Clash (U_Ent, N, SS);
5377 end if;
5378 end;
5380 -- Storage_Pool case
5382 if Id = Attribute_Storage_Pool then
5383 Analyze_And_Resolve
5384 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5386 -- In the Simple_Storage_Pool case, we allow a variable of any
5387 -- simple storage pool type, so we Resolve without imposing an
5388 -- expected type.
5390 else
5391 Analyze_And_Resolve (Expr);
5393 if not Present (Get_Rep_Pragma
5394 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5395 then
5396 Error_Msg_N
5397 ("expression must be of a simple storage pool type", Expr);
5398 end if;
5399 end if;
5401 if not Denotes_Variable (Expr) then
5402 Error_Msg_N ("storage pool must be a variable", Expr);
5403 return;
5404 end if;
5406 if Nkind (Expr) = N_Type_Conversion then
5407 T := Etype (Expression (Expr));
5408 else
5409 T := Etype (Expr);
5410 end if;
5412 -- The Stack_Bounded_Pool is used internally for implementing
5413 -- access types with a Storage_Size. Since it only work properly
5414 -- when used on one specific type, we need to check that it is not
5415 -- hijacked improperly:
5417 -- type T is access Integer;
5418 -- for T'Storage_Size use n;
5419 -- type Q is access Float;
5420 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5422 if RTE_Available (RE_Stack_Bounded_Pool)
5423 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5424 then
5425 Error_Msg_N ("non-shareable internal Pool", Expr);
5426 return;
5427 end if;
5429 -- If the argument is a name that is not an entity name, then
5430 -- we construct a renaming operation to define an entity of
5431 -- type storage pool.
5433 if not Is_Entity_Name (Expr)
5434 and then Is_Object_Reference (Expr)
5435 then
5436 Pool := Make_Temporary (Loc, 'P', Expr);
5438 declare
5439 Rnode : constant Node_Id :=
5440 Make_Object_Renaming_Declaration (Loc,
5441 Defining_Identifier => Pool,
5442 Subtype_Mark =>
5443 New_Occurrence_Of (Etype (Expr), Loc),
5444 Name => Expr);
5446 begin
5447 -- If the attribute definition clause comes from an aspect
5448 -- clause, then insert the renaming before the associated
5449 -- entity's declaration, since the attribute clause has
5450 -- not yet been appended to the declaration list.
5452 if From_Aspect_Specification (N) then
5453 Insert_Before (Parent (Entity (N)), Rnode);
5454 else
5455 Insert_Before (N, Rnode);
5456 end if;
5458 Analyze (Rnode);
5459 Set_Associated_Storage_Pool (U_Ent, Pool);
5460 end;
5462 elsif Is_Entity_Name (Expr) then
5463 Pool := Entity (Expr);
5465 -- If pool is a renamed object, get original one. This can
5466 -- happen with an explicit renaming, and within instances.
5468 while Present (Renamed_Object (Pool))
5469 and then Is_Entity_Name (Renamed_Object (Pool))
5470 loop
5471 Pool := Entity (Renamed_Object (Pool));
5472 end loop;
5474 if Present (Renamed_Object (Pool))
5475 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5476 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5477 then
5478 Pool := Entity (Expression (Renamed_Object (Pool)));
5479 end if;
5481 Set_Associated_Storage_Pool (U_Ent, Pool);
5483 elsif Nkind (Expr) = N_Type_Conversion
5484 and then Is_Entity_Name (Expression (Expr))
5485 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5486 then
5487 Pool := Entity (Expression (Expr));
5488 Set_Associated_Storage_Pool (U_Ent, Pool);
5490 else
5491 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5492 return;
5493 end if;
5494 end;
5496 ------------------
5497 -- Storage_Size --
5498 ------------------
5500 -- Storage_Size attribute definition clause
5502 when Attribute_Storage_Size => Storage_Size : declare
5503 Btype : constant Entity_Id := Base_Type (U_Ent);
5505 begin
5506 if Is_Task_Type (U_Ent) then
5508 -- Check obsolescent (but never obsolescent if from aspect)
5510 if not From_Aspect_Specification (N) then
5511 Check_Restriction (No_Obsolescent_Features, N);
5513 if Warn_On_Obsolescent_Feature then
5514 Error_Msg_N
5515 ("?j?storage size clause for task is an " &
5516 "obsolescent feature (RM J.9)", N);
5517 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5518 end if;
5519 end if;
5521 FOnly := True;
5522 end if;
5524 if not Is_Access_Type (U_Ent)
5525 and then Ekind (U_Ent) /= E_Task_Type
5526 then
5527 Error_Msg_N ("storage size cannot be given for &", Nam);
5529 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5530 Error_Msg_N
5531 ("storage size cannot be given for a derived access type",
5532 Nam);
5534 elsif Duplicate_Clause then
5535 null;
5537 else
5538 Analyze_And_Resolve (Expr, Any_Integer);
5540 if Is_Access_Type (U_Ent) then
5542 -- Check for Storage_Pool previously given
5544 declare
5545 SP : constant Node_Id :=
5546 Get_Attribute_Definition_Clause
5547 (U_Ent, Attribute_Storage_Pool);
5549 begin
5550 if Present (SP) then
5551 Check_Pool_Size_Clash (U_Ent, SP, N);
5552 end if;
5553 end;
5555 -- Special case of for x'Storage_Size use 0
5557 if Is_OK_Static_Expression (Expr)
5558 and then Expr_Value (Expr) = 0
5559 then
5560 Set_No_Pool_Assigned (Btype);
5561 end if;
5562 end if;
5564 Set_Has_Storage_Size_Clause (Btype);
5565 end if;
5566 end Storage_Size;
5568 -----------------
5569 -- Stream_Size --
5570 -----------------
5572 when Attribute_Stream_Size => Stream_Size : declare
5573 Size : constant Uint := Static_Integer (Expr);
5575 begin
5576 if Ada_Version <= Ada_95 then
5577 Check_Restriction (No_Implementation_Attributes, N);
5578 end if;
5580 if Duplicate_Clause then
5581 null;
5583 elsif Is_Elementary_Type (U_Ent) then
5584 if Size /= System_Storage_Unit
5585 and then
5586 Size /= System_Storage_Unit * 2
5587 and then
5588 Size /= System_Storage_Unit * 4
5589 and then
5590 Size /= System_Storage_Unit * 8
5591 then
5592 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5593 Error_Msg_N
5594 ("stream size for elementary type must be a"
5595 & " power of 2 and at least ^", N);
5597 elsif RM_Size (U_Ent) > Size then
5598 Error_Msg_Uint_1 := RM_Size (U_Ent);
5599 Error_Msg_N
5600 ("stream size for elementary type must be a"
5601 & " power of 2 and at least ^", N);
5602 end if;
5604 Set_Has_Stream_Size_Clause (U_Ent);
5606 else
5607 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5608 end if;
5609 end Stream_Size;
5611 ----------------
5612 -- Value_Size --
5613 ----------------
5615 -- Value_Size attribute definition clause
5617 when Attribute_Value_Size => Value_Size : declare
5618 Size : constant Uint := Static_Integer (Expr);
5619 Biased : Boolean;
5621 begin
5622 if not Is_Type (U_Ent) then
5623 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5625 elsif Duplicate_Clause then
5626 null;
5628 elsif Is_Array_Type (U_Ent)
5629 and then not Is_Constrained (U_Ent)
5630 then
5631 Error_Msg_N
5632 ("Value_Size cannot be given for unconstrained array", Nam);
5634 else
5635 if Is_Elementary_Type (U_Ent) then
5636 Check_Size (Expr, U_Ent, Size, Biased);
5637 Set_Biased (U_Ent, N, "value size clause", Biased);
5638 end if;
5640 Set_RM_Size (U_Ent, Size);
5641 end if;
5642 end Value_Size;
5644 -----------------------
5645 -- Variable_Indexing --
5646 -----------------------
5648 when Attribute_Variable_Indexing =>
5649 Check_Indexing_Functions;
5651 -----------
5652 -- Write --
5653 -----------
5655 when Attribute_Write =>
5656 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5657 Set_Has_Specified_Stream_Write (Ent);
5659 -- All other attributes cannot be set
5661 when others =>
5662 Error_Msg_N
5663 ("attribute& cannot be set with definition clause", N);
5664 end case;
5666 -- The test for the type being frozen must be performed after any
5667 -- expression the clause has been analyzed since the expression itself
5668 -- might cause freezing that makes the clause illegal.
5670 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5671 return;
5672 end if;
5673 end Analyze_Attribute_Definition_Clause;
5675 ----------------------------
5676 -- Analyze_Code_Statement --
5677 ----------------------------
5679 procedure Analyze_Code_Statement (N : Node_Id) is
5680 HSS : constant Node_Id := Parent (N);
5681 SBody : constant Node_Id := Parent (HSS);
5682 Subp : constant Entity_Id := Current_Scope;
5683 Stmt : Node_Id;
5684 Decl : Node_Id;
5685 StmtO : Node_Id;
5686 DeclO : Node_Id;
5688 begin
5689 -- Analyze and check we get right type, note that this implements the
5690 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5691 -- is the only way that Asm_Insn could possibly be visible.
5693 Analyze_And_Resolve (Expression (N));
5695 if Etype (Expression (N)) = Any_Type then
5696 return;
5697 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5698 Error_Msg_N ("incorrect type for code statement", N);
5699 return;
5700 end if;
5702 Check_Code_Statement (N);
5704 -- Make sure we appear in the handled statement sequence of a
5705 -- subprogram (RM 13.8(3)).
5707 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5708 or else Nkind (SBody) /= N_Subprogram_Body
5709 then
5710 Error_Msg_N
5711 ("code statement can only appear in body of subprogram", N);
5712 return;
5713 end if;
5715 -- Do remaining checks (RM 13.8(3)) if not already done
5717 if not Is_Machine_Code_Subprogram (Subp) then
5718 Set_Is_Machine_Code_Subprogram (Subp);
5720 -- No exception handlers allowed
5722 if Present (Exception_Handlers (HSS)) then
5723 Error_Msg_N
5724 ("exception handlers not permitted in machine code subprogram",
5725 First (Exception_Handlers (HSS)));
5726 end if;
5728 -- No declarations other than use clauses and pragmas (we allow
5729 -- certain internally generated declarations as well).
5731 Decl := First (Declarations (SBody));
5732 while Present (Decl) loop
5733 DeclO := Original_Node (Decl);
5734 if Comes_From_Source (DeclO)
5735 and not Nkind_In (DeclO, N_Pragma,
5736 N_Use_Package_Clause,
5737 N_Use_Type_Clause,
5738 N_Implicit_Label_Declaration)
5739 then
5740 Error_Msg_N
5741 ("this declaration not allowed in machine code subprogram",
5742 DeclO);
5743 end if;
5745 Next (Decl);
5746 end loop;
5748 -- No statements other than code statements, pragmas, and labels.
5749 -- Again we allow certain internally generated statements.
5751 -- In Ada 2012, qualified expressions are names, and the code
5752 -- statement is initially parsed as a procedure call.
5754 Stmt := First (Statements (HSS));
5755 while Present (Stmt) loop
5756 StmtO := Original_Node (Stmt);
5758 -- A procedure call transformed into a code statement is OK.
5760 if Ada_Version >= Ada_2012
5761 and then Nkind (StmtO) = N_Procedure_Call_Statement
5762 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5763 then
5764 null;
5766 elsif Comes_From_Source (StmtO)
5767 and then not Nkind_In (StmtO, N_Pragma,
5768 N_Label,
5769 N_Code_Statement)
5770 then
5771 Error_Msg_N
5772 ("this statement is not allowed in machine code subprogram",
5773 StmtO);
5774 end if;
5776 Next (Stmt);
5777 end loop;
5778 end if;
5779 end Analyze_Code_Statement;
5781 -----------------------------------------------
5782 -- Analyze_Enumeration_Representation_Clause --
5783 -----------------------------------------------
5785 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5786 Ident : constant Node_Id := Identifier (N);
5787 Aggr : constant Node_Id := Array_Aggregate (N);
5788 Enumtype : Entity_Id;
5789 Elit : Entity_Id;
5790 Expr : Node_Id;
5791 Assoc : Node_Id;
5792 Choice : Node_Id;
5793 Val : Uint;
5795 Err : Boolean := False;
5796 -- Set True to avoid cascade errors and crashes on incorrect source code
5798 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5799 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5800 -- Allowed range of universal integer (= allowed range of enum lit vals)
5802 Min : Uint;
5803 Max : Uint;
5804 -- Minimum and maximum values of entries
5806 Max_Node : Node_Id;
5807 -- Pointer to node for literal providing max value
5809 begin
5810 if Ignore_Rep_Clauses then
5811 Kill_Rep_Clause (N);
5812 return;
5813 end if;
5815 -- Ignore enumeration rep clauses by default in CodePeer mode,
5816 -- unless -gnatd.I is specified, as a work around for potential false
5817 -- positive messages.
5819 if CodePeer_Mode and not Debug_Flag_Dot_II then
5820 return;
5821 end if;
5823 -- First some basic error checks
5825 Find_Type (Ident);
5826 Enumtype := Entity (Ident);
5828 if Enumtype = Any_Type
5829 or else Rep_Item_Too_Early (Enumtype, N)
5830 then
5831 return;
5832 else
5833 Enumtype := Underlying_Type (Enumtype);
5834 end if;
5836 if not Is_Enumeration_Type (Enumtype) then
5837 Error_Msg_NE
5838 ("enumeration type required, found}",
5839 Ident, First_Subtype (Enumtype));
5840 return;
5841 end if;
5843 -- Ignore rep clause on generic actual type. This will already have
5844 -- been flagged on the template as an error, and this is the safest
5845 -- way to ensure we don't get a junk cascaded message in the instance.
5847 if Is_Generic_Actual_Type (Enumtype) then
5848 return;
5850 -- Type must be in current scope
5852 elsif Scope (Enumtype) /= Current_Scope then
5853 Error_Msg_N ("type must be declared in this scope", Ident);
5854 return;
5856 -- Type must be a first subtype
5858 elsif not Is_First_Subtype (Enumtype) then
5859 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5860 return;
5862 -- Ignore duplicate rep clause
5864 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5865 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5866 return;
5868 -- Don't allow rep clause for standard [wide_[wide_]]character
5870 elsif Is_Standard_Character_Type (Enumtype) then
5871 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5872 return;
5874 -- Check that the expression is a proper aggregate (no parentheses)
5876 elsif Paren_Count (Aggr) /= 0 then
5877 Error_Msg
5878 ("extra parentheses surrounding aggregate not allowed",
5879 First_Sloc (Aggr));
5880 return;
5882 -- All tests passed, so set rep clause in place
5884 else
5885 Set_Has_Enumeration_Rep_Clause (Enumtype);
5886 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5887 end if;
5889 -- Now we process the aggregate. Note that we don't use the normal
5890 -- aggregate code for this purpose, because we don't want any of the
5891 -- normal expansion activities, and a number of special semantic
5892 -- rules apply (including the component type being any integer type)
5894 Elit := First_Literal (Enumtype);
5896 -- First the positional entries if any
5898 if Present (Expressions (Aggr)) then
5899 Expr := First (Expressions (Aggr));
5900 while Present (Expr) loop
5901 if No (Elit) then
5902 Error_Msg_N ("too many entries in aggregate", Expr);
5903 return;
5904 end if;
5906 Val := Static_Integer (Expr);
5908 -- Err signals that we found some incorrect entries processing
5909 -- the list. The final checks for completeness and ordering are
5910 -- skipped in this case.
5912 if Val = No_Uint then
5913 Err := True;
5915 elsif Val < Lo or else Hi < Val then
5916 Error_Msg_N ("value outside permitted range", Expr);
5917 Err := True;
5918 end if;
5920 Set_Enumeration_Rep (Elit, Val);
5921 Set_Enumeration_Rep_Expr (Elit, Expr);
5922 Next (Expr);
5923 Next (Elit);
5924 end loop;
5925 end if;
5927 -- Now process the named entries if present
5929 if Present (Component_Associations (Aggr)) then
5930 Assoc := First (Component_Associations (Aggr));
5931 while Present (Assoc) loop
5932 Choice := First (Choices (Assoc));
5934 if Present (Next (Choice)) then
5935 Error_Msg_N
5936 ("multiple choice not allowed here", Next (Choice));
5937 Err := True;
5938 end if;
5940 if Nkind (Choice) = N_Others_Choice then
5941 Error_Msg_N ("others choice not allowed here", Choice);
5942 Err := True;
5944 elsif Nkind (Choice) = N_Range then
5946 -- ??? should allow zero/one element range here
5948 Error_Msg_N ("range not allowed here", Choice);
5949 Err := True;
5951 else
5952 Analyze_And_Resolve (Choice, Enumtype);
5954 if Error_Posted (Choice) then
5955 Err := True;
5956 end if;
5958 if not Err then
5959 if Is_Entity_Name (Choice)
5960 and then Is_Type (Entity (Choice))
5961 then
5962 Error_Msg_N ("subtype name not allowed here", Choice);
5963 Err := True;
5965 -- ??? should allow static subtype with zero/one entry
5967 elsif Etype (Choice) = Base_Type (Enumtype) then
5968 if not Is_OK_Static_Expression (Choice) then
5969 Flag_Non_Static_Expr
5970 ("non-static expression used for choice!", Choice);
5971 Err := True;
5973 else
5974 Elit := Expr_Value_E (Choice);
5976 if Present (Enumeration_Rep_Expr (Elit)) then
5977 Error_Msg_Sloc :=
5978 Sloc (Enumeration_Rep_Expr (Elit));
5979 Error_Msg_NE
5980 ("representation for& previously given#",
5981 Choice, Elit);
5982 Err := True;
5983 end if;
5985 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
5987 Expr := Expression (Assoc);
5988 Val := Static_Integer (Expr);
5990 if Val = No_Uint then
5991 Err := True;
5993 elsif Val < Lo or else Hi < Val then
5994 Error_Msg_N ("value outside permitted range", Expr);
5995 Err := True;
5996 end if;
5998 Set_Enumeration_Rep (Elit, Val);
5999 end if;
6000 end if;
6001 end if;
6002 end if;
6004 Next (Assoc);
6005 end loop;
6006 end if;
6008 -- Aggregate is fully processed. Now we check that a full set of
6009 -- representations was given, and that they are in range and in order.
6010 -- These checks are only done if no other errors occurred.
6012 if not Err then
6013 Min := No_Uint;
6014 Max := No_Uint;
6016 Elit := First_Literal (Enumtype);
6017 while Present (Elit) loop
6018 if No (Enumeration_Rep_Expr (Elit)) then
6019 Error_Msg_NE ("missing representation for&!", N, Elit);
6021 else
6022 Val := Enumeration_Rep (Elit);
6024 if Min = No_Uint then
6025 Min := Val;
6026 end if;
6028 if Val /= No_Uint then
6029 if Max /= No_Uint and then Val <= Max then
6030 Error_Msg_NE
6031 ("enumeration value for& not ordered!",
6032 Enumeration_Rep_Expr (Elit), Elit);
6033 end if;
6035 Max_Node := Enumeration_Rep_Expr (Elit);
6036 Max := Val;
6037 end if;
6039 -- If there is at least one literal whose representation is not
6040 -- equal to the Pos value, then note that this enumeration type
6041 -- has a non-standard representation.
6043 if Val /= Enumeration_Pos (Elit) then
6044 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6045 end if;
6046 end if;
6048 Next (Elit);
6049 end loop;
6051 -- Now set proper size information
6053 declare
6054 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6056 begin
6057 if Has_Size_Clause (Enumtype) then
6059 -- All OK, if size is OK now
6061 if RM_Size (Enumtype) >= Minsize then
6062 null;
6064 else
6065 -- Try if we can get by with biasing
6067 Minsize :=
6068 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6070 -- Error message if even biasing does not work
6072 if RM_Size (Enumtype) < Minsize then
6073 Error_Msg_Uint_1 := RM_Size (Enumtype);
6074 Error_Msg_Uint_2 := Max;
6075 Error_Msg_N
6076 ("previously given size (^) is too small "
6077 & "for this value (^)", Max_Node);
6079 -- If biasing worked, indicate that we now have biased rep
6081 else
6082 Set_Biased
6083 (Enumtype, Size_Clause (Enumtype), "size clause");
6084 end if;
6085 end if;
6087 else
6088 Set_RM_Size (Enumtype, Minsize);
6089 Set_Enum_Esize (Enumtype);
6090 end if;
6092 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6093 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6094 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6095 end;
6096 end if;
6098 -- We repeat the too late test in case it froze itself
6100 if Rep_Item_Too_Late (Enumtype, N) then
6101 null;
6102 end if;
6103 end Analyze_Enumeration_Representation_Clause;
6105 ----------------------------
6106 -- Analyze_Free_Statement --
6107 ----------------------------
6109 procedure Analyze_Free_Statement (N : Node_Id) is
6110 begin
6111 Analyze (Expression (N));
6112 end Analyze_Free_Statement;
6114 ---------------------------
6115 -- Analyze_Freeze_Entity --
6116 ---------------------------
6118 procedure Analyze_Freeze_Entity (N : Node_Id) is
6119 begin
6120 Freeze_Entity_Checks (N);
6121 end Analyze_Freeze_Entity;
6123 -----------------------------------
6124 -- Analyze_Freeze_Generic_Entity --
6125 -----------------------------------
6127 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6128 begin
6129 Freeze_Entity_Checks (N);
6130 end Analyze_Freeze_Generic_Entity;
6132 ------------------------------------------
6133 -- Analyze_Record_Representation_Clause --
6134 ------------------------------------------
6136 -- Note: we check as much as we can here, but we can't do any checks
6137 -- based on the position values (e.g. overlap checks) until freeze time
6138 -- because especially in Ada 2005 (machine scalar mode), the processing
6139 -- for non-standard bit order can substantially change the positions.
6140 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6141 -- for the remainder of this processing.
6143 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6144 Ident : constant Node_Id := Identifier (N);
6145 Biased : Boolean;
6146 CC : Node_Id;
6147 Comp : Entity_Id;
6148 Fbit : Uint;
6149 Hbit : Uint := Uint_0;
6150 Lbit : Uint;
6151 Ocomp : Entity_Id;
6152 Posit : Uint;
6153 Rectype : Entity_Id;
6154 Recdef : Node_Id;
6156 function Is_Inherited (Comp : Entity_Id) return Boolean;
6157 -- True if Comp is an inherited component in a record extension
6159 ------------------
6160 -- Is_Inherited --
6161 ------------------
6163 function Is_Inherited (Comp : Entity_Id) return Boolean is
6164 Comp_Base : Entity_Id;
6166 begin
6167 if Ekind (Rectype) = E_Record_Subtype then
6168 Comp_Base := Original_Record_Component (Comp);
6169 else
6170 Comp_Base := Comp;
6171 end if;
6173 return Comp_Base /= Original_Record_Component (Comp_Base);
6174 end Is_Inherited;
6176 -- Local variables
6178 Is_Record_Extension : Boolean;
6179 -- True if Rectype is a record extension
6181 CR_Pragma : Node_Id := Empty;
6182 -- Points to N_Pragma node if Complete_Representation pragma present
6184 -- Start of processing for Analyze_Record_Representation_Clause
6186 begin
6187 if Ignore_Rep_Clauses then
6188 Kill_Rep_Clause (N);
6189 return;
6190 end if;
6192 Find_Type (Ident);
6193 Rectype := Entity (Ident);
6195 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6196 return;
6197 else
6198 Rectype := Underlying_Type (Rectype);
6199 end if;
6201 -- First some basic error checks
6203 if not Is_Record_Type (Rectype) then
6204 Error_Msg_NE
6205 ("record type required, found}", Ident, First_Subtype (Rectype));
6206 return;
6208 elsif Scope (Rectype) /= Current_Scope then
6209 Error_Msg_N ("type must be declared in this scope", N);
6210 return;
6212 elsif not Is_First_Subtype (Rectype) then
6213 Error_Msg_N ("cannot give record rep clause for subtype", N);
6214 return;
6216 elsif Has_Record_Rep_Clause (Rectype) then
6217 Error_Msg_N ("duplicate record rep clause ignored", N);
6218 return;
6220 elsif Rep_Item_Too_Late (Rectype, N) then
6221 return;
6222 end if;
6224 -- We know we have a first subtype, now possibly go the the anonymous
6225 -- base type to determine whether Rectype is a record extension.
6227 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6228 Is_Record_Extension :=
6229 Nkind (Recdef) = N_Derived_Type_Definition
6230 and then Present (Record_Extension_Part (Recdef));
6232 if Present (Mod_Clause (N)) then
6233 declare
6234 Loc : constant Source_Ptr := Sloc (N);
6235 M : constant Node_Id := Mod_Clause (N);
6236 P : constant List_Id := Pragmas_Before (M);
6237 AtM_Nod : Node_Id;
6239 Mod_Val : Uint;
6240 pragma Warnings (Off, Mod_Val);
6242 begin
6243 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6245 if Warn_On_Obsolescent_Feature then
6246 Error_Msg_N
6247 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6248 Error_Msg_N
6249 ("\?j?use alignment attribute definition clause instead", N);
6250 end if;
6252 if Present (P) then
6253 Analyze_List (P);
6254 end if;
6256 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6257 -- the Mod clause into an alignment clause anyway, so that the
6258 -- back-end can compute and back-annotate properly the size and
6259 -- alignment of types that may include this record.
6261 -- This seems dubious, this destroys the source tree in a manner
6262 -- not detectable by ASIS ???
6264 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6265 AtM_Nod :=
6266 Make_Attribute_Definition_Clause (Loc,
6267 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6268 Chars => Name_Alignment,
6269 Expression => Relocate_Node (Expression (M)));
6271 Set_From_At_Mod (AtM_Nod);
6272 Insert_After (N, AtM_Nod);
6273 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6274 Set_Mod_Clause (N, Empty);
6276 else
6277 -- Get the alignment value to perform error checking
6279 Mod_Val := Get_Alignment_Value (Expression (M));
6280 end if;
6281 end;
6282 end if;
6284 -- For untagged types, clear any existing component clauses for the
6285 -- type. If the type is derived, this is what allows us to override
6286 -- a rep clause for the parent. For type extensions, the representation
6287 -- of the inherited components is inherited, so we want to keep previous
6288 -- component clauses for completeness.
6290 if not Is_Tagged_Type (Rectype) then
6291 Comp := First_Component_Or_Discriminant (Rectype);
6292 while Present (Comp) loop
6293 Set_Component_Clause (Comp, Empty);
6294 Next_Component_Or_Discriminant (Comp);
6295 end loop;
6296 end if;
6298 -- All done if no component clauses
6300 CC := First (Component_Clauses (N));
6302 if No (CC) then
6303 return;
6304 end if;
6306 -- A representation like this applies to the base type
6308 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6309 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6310 Set_Has_Specified_Layout (Base_Type (Rectype));
6312 -- Process the component clauses
6314 while Present (CC) loop
6316 -- Pragma
6318 if Nkind (CC) = N_Pragma then
6319 Analyze (CC);
6321 -- The only pragma of interest is Complete_Representation
6323 if Pragma_Name (CC) = Name_Complete_Representation then
6324 CR_Pragma := CC;
6325 end if;
6327 -- Processing for real component clause
6329 else
6330 Posit := Static_Integer (Position (CC));
6331 Fbit := Static_Integer (First_Bit (CC));
6332 Lbit := Static_Integer (Last_Bit (CC));
6334 if Posit /= No_Uint
6335 and then Fbit /= No_Uint
6336 and then Lbit /= No_Uint
6337 then
6338 if Posit < 0 then
6339 Error_Msg_N
6340 ("position cannot be negative", Position (CC));
6342 elsif Fbit < 0 then
6343 Error_Msg_N
6344 ("first bit cannot be negative", First_Bit (CC));
6346 -- The Last_Bit specified in a component clause must not be
6347 -- less than the First_Bit minus one (RM-13.5.1(10)).
6349 elsif Lbit < Fbit - 1 then
6350 Error_Msg_N
6351 ("last bit cannot be less than first bit minus one",
6352 Last_Bit (CC));
6354 -- Values look OK, so find the corresponding record component
6355 -- Even though the syntax allows an attribute reference for
6356 -- implementation-defined components, GNAT does not allow the
6357 -- tag to get an explicit position.
6359 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6360 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6361 Error_Msg_N ("position of tag cannot be specified", CC);
6362 else
6363 Error_Msg_N ("illegal component name", CC);
6364 end if;
6366 else
6367 Comp := First_Entity (Rectype);
6368 while Present (Comp) loop
6369 exit when Chars (Comp) = Chars (Component_Name (CC));
6370 Next_Entity (Comp);
6371 end loop;
6373 if No (Comp) then
6375 -- Maybe component of base type that is absent from
6376 -- statically constrained first subtype.
6378 Comp := First_Entity (Base_Type (Rectype));
6379 while Present (Comp) loop
6380 exit when Chars (Comp) = Chars (Component_Name (CC));
6381 Next_Entity (Comp);
6382 end loop;
6383 end if;
6385 if No (Comp) then
6386 Error_Msg_N
6387 ("component clause is for non-existent field", CC);
6389 -- Ada 2012 (AI05-0026): Any name that denotes a
6390 -- discriminant of an object of an unchecked union type
6391 -- shall not occur within a record_representation_clause.
6393 -- The general restriction of using record rep clauses on
6394 -- Unchecked_Union types has now been lifted. Since it is
6395 -- possible to introduce a record rep clause which mentions
6396 -- the discriminant of an Unchecked_Union in non-Ada 2012
6397 -- code, this check is applied to all versions of the
6398 -- language.
6400 elsif Ekind (Comp) = E_Discriminant
6401 and then Is_Unchecked_Union (Rectype)
6402 then
6403 Error_Msg_N
6404 ("cannot reference discriminant of unchecked union",
6405 Component_Name (CC));
6407 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6408 Error_Msg_NE
6409 ("component clause not allowed for inherited "
6410 & "component&", CC, Comp);
6412 elsif Present (Component_Clause (Comp)) then
6414 -- Diagnose duplicate rep clause, or check consistency
6415 -- if this is an inherited component. In a double fault,
6416 -- there may be a duplicate inconsistent clause for an
6417 -- inherited component.
6419 if Scope (Original_Record_Component (Comp)) = Rectype
6420 or else Parent (Component_Clause (Comp)) = N
6421 then
6422 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6423 Error_Msg_N ("component clause previously given#", CC);
6425 else
6426 declare
6427 Rep1 : constant Node_Id := Component_Clause (Comp);
6428 begin
6429 if Intval (Position (Rep1)) /=
6430 Intval (Position (CC))
6431 or else Intval (First_Bit (Rep1)) /=
6432 Intval (First_Bit (CC))
6433 or else Intval (Last_Bit (Rep1)) /=
6434 Intval (Last_Bit (CC))
6435 then
6436 Error_Msg_N
6437 ("component clause inconsistent "
6438 & "with representation of ancestor", CC);
6440 elsif Warn_On_Redundant_Constructs then
6441 Error_Msg_N
6442 ("?r?redundant confirming component clause "
6443 & "for component!", CC);
6444 end if;
6445 end;
6446 end if;
6448 -- Normal case where this is the first component clause we
6449 -- have seen for this entity, so set it up properly.
6451 else
6452 -- Make reference for field in record rep clause and set
6453 -- appropriate entity field in the field identifier.
6455 Generate_Reference
6456 (Comp, Component_Name (CC), Set_Ref => False);
6457 Set_Entity (Component_Name (CC), Comp);
6459 -- Update Fbit and Lbit to the actual bit number
6461 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6462 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6464 if Has_Size_Clause (Rectype)
6465 and then RM_Size (Rectype) <= Lbit
6466 then
6467 Error_Msg_N
6468 ("bit number out of range of specified size",
6469 Last_Bit (CC));
6470 else
6471 Set_Component_Clause (Comp, CC);
6472 Set_Component_Bit_Offset (Comp, Fbit);
6473 Set_Esize (Comp, 1 + (Lbit - Fbit));
6474 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6475 Set_Normalized_Position (Comp, Fbit / SSU);
6477 if Warn_On_Overridden_Size
6478 and then Has_Size_Clause (Etype (Comp))
6479 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6480 then
6481 Error_Msg_NE
6482 ("?S?component size overrides size clause for&",
6483 Component_Name (CC), Etype (Comp));
6484 end if;
6486 -- This information is also set in the corresponding
6487 -- component of the base type, found by accessing the
6488 -- Original_Record_Component link if it is present.
6490 Ocomp := Original_Record_Component (Comp);
6492 if Hbit < Lbit then
6493 Hbit := Lbit;
6494 end if;
6496 Check_Size
6497 (Component_Name (CC),
6498 Etype (Comp),
6499 Esize (Comp),
6500 Biased);
6502 Set_Biased
6503 (Comp, First_Node (CC), "component clause", Biased);
6505 if Present (Ocomp) then
6506 Set_Component_Clause (Ocomp, CC);
6507 Set_Component_Bit_Offset (Ocomp, Fbit);
6508 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6509 Set_Normalized_Position (Ocomp, Fbit / SSU);
6510 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6512 Set_Normalized_Position_Max
6513 (Ocomp, Normalized_Position (Ocomp));
6515 -- Note: we don't use Set_Biased here, because we
6516 -- already gave a warning above if needed, and we
6517 -- would get a duplicate for the same name here.
6519 Set_Has_Biased_Representation
6520 (Ocomp, Has_Biased_Representation (Comp));
6521 end if;
6523 if Esize (Comp) < 0 then
6524 Error_Msg_N ("component size is negative", CC);
6525 end if;
6526 end if;
6527 end if;
6528 end if;
6529 end if;
6530 end if;
6532 Next (CC);
6533 end loop;
6535 -- Check missing components if Complete_Representation pragma appeared
6537 if Present (CR_Pragma) then
6538 Comp := First_Component_Or_Discriminant (Rectype);
6539 while Present (Comp) loop
6540 if No (Component_Clause (Comp)) then
6541 Error_Msg_NE
6542 ("missing component clause for &", CR_Pragma, Comp);
6543 end if;
6545 Next_Component_Or_Discriminant (Comp);
6546 end loop;
6548 -- Give missing components warning if required
6550 elsif Warn_On_Unrepped_Components then
6551 declare
6552 Num_Repped_Components : Nat := 0;
6553 Num_Unrepped_Components : Nat := 0;
6555 begin
6556 -- First count number of repped and unrepped components
6558 Comp := First_Component_Or_Discriminant (Rectype);
6559 while Present (Comp) loop
6560 if Present (Component_Clause (Comp)) then
6561 Num_Repped_Components := Num_Repped_Components + 1;
6562 else
6563 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6564 end if;
6566 Next_Component_Or_Discriminant (Comp);
6567 end loop;
6569 -- We are only interested in the case where there is at least one
6570 -- unrepped component, and at least half the components have rep
6571 -- clauses. We figure that if less than half have them, then the
6572 -- partial rep clause is really intentional. If the component
6573 -- type has no underlying type set at this point (as for a generic
6574 -- formal type), we don't know enough to give a warning on the
6575 -- component.
6577 if Num_Unrepped_Components > 0
6578 and then Num_Unrepped_Components < Num_Repped_Components
6579 then
6580 Comp := First_Component_Or_Discriminant (Rectype);
6581 while Present (Comp) loop
6582 if No (Component_Clause (Comp))
6583 and then Comes_From_Source (Comp)
6584 and then Present (Underlying_Type (Etype (Comp)))
6585 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6586 or else Size_Known_At_Compile_Time
6587 (Underlying_Type (Etype (Comp))))
6588 and then not Has_Warnings_Off (Rectype)
6589 then
6590 Error_Msg_Sloc := Sloc (Comp);
6591 Error_Msg_NE
6592 ("?C?no component clause given for & declared #",
6593 N, Comp);
6594 end if;
6596 Next_Component_Or_Discriminant (Comp);
6597 end loop;
6598 end if;
6599 end;
6600 end if;
6601 end Analyze_Record_Representation_Clause;
6603 -------------------------------------
6604 -- Build_Discrete_Static_Predicate --
6605 -------------------------------------
6607 procedure Build_Discrete_Static_Predicate
6608 (Typ : Entity_Id;
6609 Expr : Node_Id;
6610 Nam : Name_Id)
6612 Loc : constant Source_Ptr := Sloc (Expr);
6614 Non_Static : exception;
6615 -- Raised if something non-static is found
6617 Btyp : constant Entity_Id := Base_Type (Typ);
6619 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6620 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6621 -- Low bound and high bound value of base type of Typ
6623 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6624 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6625 -- Low bound and high bound values of static subtype Typ
6627 type REnt is record
6628 Lo, Hi : Uint;
6629 end record;
6630 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6631 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6632 -- value.
6634 type RList is array (Nat range <>) of REnt;
6635 -- A list of ranges. The ranges are sorted in increasing order, and are
6636 -- disjoint (there is a gap of at least one value between each range in
6637 -- the table). A value is in the set of ranges in Rlist if it lies
6638 -- within one of these ranges.
6640 False_Range : constant RList :=
6641 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6642 -- An empty set of ranges represents a range list that can never be
6643 -- satisfied, since there are no ranges in which the value could lie,
6644 -- so it does not lie in any of them. False_Range is a canonical value
6645 -- for this empty set, but general processing should test for an Rlist
6646 -- with length zero (see Is_False predicate), since other null ranges
6647 -- may appear which must be treated as False.
6649 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6650 -- Range representing True, value must be in the base range
6652 function "and" (Left : RList; Right : RList) return RList;
6653 -- And's together two range lists, returning a range list. This is a set
6654 -- intersection operation.
6656 function "or" (Left : RList; Right : RList) return RList;
6657 -- Or's together two range lists, returning a range list. This is a set
6658 -- union operation.
6660 function "not" (Right : RList) return RList;
6661 -- Returns complement of a given range list, i.e. a range list
6662 -- representing all the values in TLo .. THi that are not in the input
6663 -- operand Right.
6665 function Build_Val (V : Uint) return Node_Id;
6666 -- Return an analyzed N_Identifier node referencing this value, suitable
6667 -- for use as an entry in the Static_Discrte_Predicate list. This node
6668 -- is typed with the base type.
6670 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6671 -- Return an analyzed N_Range node referencing this range, suitable for
6672 -- use as an entry in the Static_Discrete_Predicate list. This node is
6673 -- typed with the base type.
6675 function Get_RList (Exp : Node_Id) return RList;
6676 -- This is a recursive routine that converts the given expression into a
6677 -- list of ranges, suitable for use in building the static predicate.
6679 function Is_False (R : RList) return Boolean;
6680 pragma Inline (Is_False);
6681 -- Returns True if the given range list is empty, and thus represents a
6682 -- False list of ranges that can never be satisfied.
6684 function Is_True (R : RList) return Boolean;
6685 -- Returns True if R trivially represents the True predicate by having a
6686 -- single range from BLo to BHi.
6688 function Is_Type_Ref (N : Node_Id) return Boolean;
6689 pragma Inline (Is_Type_Ref);
6690 -- Returns if True if N is a reference to the type for the predicate in
6691 -- the expression (i.e. if it is an identifier whose Chars field matches
6692 -- the Nam given in the call). N must not be parenthesized, if the type
6693 -- name appears in parens, this routine will return False.
6695 function Lo_Val (N : Node_Id) return Uint;
6696 -- Given an entry from a Static_Discrete_Predicate list that is either
6697 -- a static expression or static range, gets either the expression value
6698 -- or the low bound of the range.
6700 function Hi_Val (N : Node_Id) return Uint;
6701 -- Given an entry from a Static_Discrete_Predicate list that is either
6702 -- a static expression or static range, gets either the expression value
6703 -- or the high bound of the range.
6705 function Membership_Entry (N : Node_Id) return RList;
6706 -- Given a single membership entry (range, value, or subtype), returns
6707 -- the corresponding range list. Raises Static_Error if not static.
6709 function Membership_Entries (N : Node_Id) return RList;
6710 -- Given an element on an alternatives list of a membership operation,
6711 -- returns the range list corresponding to this entry and all following
6712 -- entries (i.e. returns the "or" of this list of values).
6714 function Stat_Pred (Typ : Entity_Id) return RList;
6715 -- Given a type, if it has a static predicate, then return the predicate
6716 -- as a range list, otherwise raise Non_Static.
6718 -----------
6719 -- "and" --
6720 -----------
6722 function "and" (Left : RList; Right : RList) return RList is
6723 FEnt : REnt;
6724 -- First range of result
6726 SLeft : Nat := Left'First;
6727 -- Start of rest of left entries
6729 SRight : Nat := Right'First;
6730 -- Start of rest of right entries
6732 begin
6733 -- If either range is True, return the other
6735 if Is_True (Left) then
6736 return Right;
6737 elsif Is_True (Right) then
6738 return Left;
6739 end if;
6741 -- If either range is False, return False
6743 if Is_False (Left) or else Is_False (Right) then
6744 return False_Range;
6745 end if;
6747 -- Loop to remove entries at start that are disjoint, and thus just
6748 -- get discarded from the result entirely.
6750 loop
6751 -- If no operands left in either operand, result is false
6753 if SLeft > Left'Last or else SRight > Right'Last then
6754 return False_Range;
6756 -- Discard first left operand entry if disjoint with right
6758 elsif Left (SLeft).Hi < Right (SRight).Lo then
6759 SLeft := SLeft + 1;
6761 -- Discard first right operand entry if disjoint with left
6763 elsif Right (SRight).Hi < Left (SLeft).Lo then
6764 SRight := SRight + 1;
6766 -- Otherwise we have an overlapping entry
6768 else
6769 exit;
6770 end if;
6771 end loop;
6773 -- Now we have two non-null operands, and first entries overlap. The
6774 -- first entry in the result will be the overlapping part of these
6775 -- two entries.
6777 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6778 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6780 -- Now we can remove the entry that ended at a lower value, since its
6781 -- contribution is entirely contained in Fent.
6783 if Left (SLeft).Hi <= Right (SRight).Hi then
6784 SLeft := SLeft + 1;
6785 else
6786 SRight := SRight + 1;
6787 end if;
6789 -- Compute result by concatenating this first entry with the "and" of
6790 -- the remaining parts of the left and right operands. Note that if
6791 -- either of these is empty, "and" will yield empty, so that we will
6792 -- end up with just Fent, which is what we want in that case.
6794 return
6795 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6796 end "and";
6798 -----------
6799 -- "not" --
6800 -----------
6802 function "not" (Right : RList) return RList is
6803 begin
6804 -- Return True if False range
6806 if Is_False (Right) then
6807 return True_Range;
6808 end if;
6810 -- Return False if True range
6812 if Is_True (Right) then
6813 return False_Range;
6814 end if;
6816 -- Here if not trivial case
6818 declare
6819 Result : RList (1 .. Right'Length + 1);
6820 -- May need one more entry for gap at beginning and end
6822 Count : Nat := 0;
6823 -- Number of entries stored in Result
6825 begin
6826 -- Gap at start
6828 if Right (Right'First).Lo > TLo then
6829 Count := Count + 1;
6830 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6831 end if;
6833 -- Gaps between ranges
6835 for J in Right'First .. Right'Last - 1 loop
6836 Count := Count + 1;
6837 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6838 end loop;
6840 -- Gap at end
6842 if Right (Right'Last).Hi < THi then
6843 Count := Count + 1;
6844 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6845 end if;
6847 return Result (1 .. Count);
6848 end;
6849 end "not";
6851 ----------
6852 -- "or" --
6853 ----------
6855 function "or" (Left : RList; Right : RList) return RList is
6856 FEnt : REnt;
6857 -- First range of result
6859 SLeft : Nat := Left'First;
6860 -- Start of rest of left entries
6862 SRight : Nat := Right'First;
6863 -- Start of rest of right entries
6865 begin
6866 -- If either range is True, return True
6868 if Is_True (Left) or else Is_True (Right) then
6869 return True_Range;
6870 end if;
6872 -- If either range is False (empty), return the other
6874 if Is_False (Left) then
6875 return Right;
6876 elsif Is_False (Right) then
6877 return Left;
6878 end if;
6880 -- Initialize result first entry from left or right operand depending
6881 -- on which starts with the lower range.
6883 if Left (SLeft).Lo < Right (SRight).Lo then
6884 FEnt := Left (SLeft);
6885 SLeft := SLeft + 1;
6886 else
6887 FEnt := Right (SRight);
6888 SRight := SRight + 1;
6889 end if;
6891 -- This loop eats ranges from left and right operands that are
6892 -- contiguous with the first range we are gathering.
6894 loop
6895 -- Eat first entry in left operand if contiguous or overlapped by
6896 -- gathered first operand of result.
6898 if SLeft <= Left'Last
6899 and then Left (SLeft).Lo <= FEnt.Hi + 1
6900 then
6901 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6902 SLeft := SLeft + 1;
6904 -- Eat first entry in right operand if contiguous or overlapped by
6905 -- gathered right operand of result.
6907 elsif SRight <= Right'Last
6908 and then Right (SRight).Lo <= FEnt.Hi + 1
6909 then
6910 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6911 SRight := SRight + 1;
6913 -- All done if no more entries to eat
6915 else
6916 exit;
6917 end if;
6918 end loop;
6920 -- Obtain result as the first entry we just computed, concatenated
6921 -- to the "or" of the remaining results (if one operand is empty,
6922 -- this will just concatenate with the other
6924 return
6925 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6926 end "or";
6928 -----------------
6929 -- Build_Range --
6930 -----------------
6932 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6933 Result : Node_Id;
6934 begin
6935 Result :=
6936 Make_Range (Loc,
6937 Low_Bound => Build_Val (Lo),
6938 High_Bound => Build_Val (Hi));
6939 Set_Etype (Result, Btyp);
6940 Set_Analyzed (Result);
6941 return Result;
6942 end Build_Range;
6944 ---------------
6945 -- Build_Val --
6946 ---------------
6948 function Build_Val (V : Uint) return Node_Id is
6949 Result : Node_Id;
6951 begin
6952 if Is_Enumeration_Type (Typ) then
6953 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6954 else
6955 Result := Make_Integer_Literal (Loc, V);
6956 end if;
6958 Set_Etype (Result, Btyp);
6959 Set_Is_Static_Expression (Result);
6960 Set_Analyzed (Result);
6961 return Result;
6962 end Build_Val;
6964 ---------------
6965 -- Get_RList --
6966 ---------------
6968 function Get_RList (Exp : Node_Id) return RList is
6969 Op : Node_Kind;
6970 Val : Uint;
6972 begin
6973 -- Static expression can only be true or false
6975 if Is_OK_Static_Expression (Exp) then
6976 if Expr_Value (Exp) = 0 then
6977 return False_Range;
6978 else
6979 return True_Range;
6980 end if;
6981 end if;
6983 -- Otherwise test node type
6985 Op := Nkind (Exp);
6987 case Op is
6989 -- And
6991 when N_Op_And | N_And_Then =>
6992 return Get_RList (Left_Opnd (Exp))
6994 Get_RList (Right_Opnd (Exp));
6996 -- Or
6998 when N_Op_Or | N_Or_Else =>
6999 return Get_RList (Left_Opnd (Exp))
7001 Get_RList (Right_Opnd (Exp));
7003 -- Not
7005 when N_Op_Not =>
7006 return not Get_RList (Right_Opnd (Exp));
7008 -- Comparisons of type with static value
7010 when N_Op_Compare =>
7012 -- Type is left operand
7014 if Is_Type_Ref (Left_Opnd (Exp))
7015 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7016 then
7017 Val := Expr_Value (Right_Opnd (Exp));
7019 -- Typ is right operand
7021 elsif Is_Type_Ref (Right_Opnd (Exp))
7022 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7023 then
7024 Val := Expr_Value (Left_Opnd (Exp));
7026 -- Invert sense of comparison
7028 case Op is
7029 when N_Op_Gt => Op := N_Op_Lt;
7030 when N_Op_Lt => Op := N_Op_Gt;
7031 when N_Op_Ge => Op := N_Op_Le;
7032 when N_Op_Le => Op := N_Op_Ge;
7033 when others => null;
7034 end case;
7036 -- Other cases are non-static
7038 else
7039 raise Non_Static;
7040 end if;
7042 -- Construct range according to comparison operation
7044 case Op is
7045 when N_Op_Eq =>
7046 return RList'(1 => REnt'(Val, Val));
7048 when N_Op_Ge =>
7049 return RList'(1 => REnt'(Val, BHi));
7051 when N_Op_Gt =>
7052 return RList'(1 => REnt'(Val + 1, BHi));
7054 when N_Op_Le =>
7055 return RList'(1 => REnt'(BLo, Val));
7057 when N_Op_Lt =>
7058 return RList'(1 => REnt'(BLo, Val - 1));
7060 when N_Op_Ne =>
7061 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7063 when others =>
7064 raise Program_Error;
7065 end case;
7067 -- Membership (IN)
7069 when N_In =>
7070 if not Is_Type_Ref (Left_Opnd (Exp)) then
7071 raise Non_Static;
7072 end if;
7074 if Present (Right_Opnd (Exp)) then
7075 return Membership_Entry (Right_Opnd (Exp));
7076 else
7077 return Membership_Entries (First (Alternatives (Exp)));
7078 end if;
7080 -- Negative membership (NOT IN)
7082 when N_Not_In =>
7083 if not Is_Type_Ref (Left_Opnd (Exp)) then
7084 raise Non_Static;
7085 end if;
7087 if Present (Right_Opnd (Exp)) then
7088 return not Membership_Entry (Right_Opnd (Exp));
7089 else
7090 return not Membership_Entries (First (Alternatives (Exp)));
7091 end if;
7093 -- Function call, may be call to static predicate
7095 when N_Function_Call =>
7096 if Is_Entity_Name (Name (Exp)) then
7097 declare
7098 Ent : constant Entity_Id := Entity (Name (Exp));
7099 begin
7100 if Is_Predicate_Function (Ent)
7101 or else
7102 Is_Predicate_Function_M (Ent)
7103 then
7104 return Stat_Pred (Etype (First_Formal (Ent)));
7105 end if;
7106 end;
7107 end if;
7109 -- Other function call cases are non-static
7111 raise Non_Static;
7113 -- Qualified expression, dig out the expression
7115 when N_Qualified_Expression =>
7116 return Get_RList (Expression (Exp));
7118 when N_Case_Expression =>
7119 declare
7120 Alt : Node_Id;
7121 Choices : List_Id;
7122 Dep : Node_Id;
7124 begin
7125 if not Is_Entity_Name (Expression (Expr))
7126 or else Etype (Expression (Expr)) /= Typ
7127 then
7128 Error_Msg_N
7129 ("expression must denaote subtype", Expression (Expr));
7130 return False_Range;
7131 end if;
7133 -- Collect discrete choices in all True alternatives
7135 Choices := New_List;
7136 Alt := First (Alternatives (Exp));
7137 while Present (Alt) loop
7138 Dep := Expression (Alt);
7140 if not Is_OK_Static_Expression (Dep) then
7141 raise Non_Static;
7143 elsif Is_True (Expr_Value (Dep)) then
7144 Append_List_To (Choices,
7145 New_Copy_List (Discrete_Choices (Alt)));
7146 end if;
7148 Next (Alt);
7149 end loop;
7151 return Membership_Entries (First (Choices));
7152 end;
7154 -- Expression with actions: if no actions, dig out expression
7156 when N_Expression_With_Actions =>
7157 if Is_Empty_List (Actions (Exp)) then
7158 return Get_RList (Expression (Exp));
7159 else
7160 raise Non_Static;
7161 end if;
7163 -- Xor operator
7165 when N_Op_Xor =>
7166 return (Get_RList (Left_Opnd (Exp))
7167 and not Get_RList (Right_Opnd (Exp)))
7168 or (Get_RList (Right_Opnd (Exp))
7169 and not Get_RList (Left_Opnd (Exp)));
7171 -- Any other node type is non-static
7173 when others =>
7174 raise Non_Static;
7175 end case;
7176 end Get_RList;
7178 ------------
7179 -- Hi_Val --
7180 ------------
7182 function Hi_Val (N : Node_Id) return Uint is
7183 begin
7184 if Is_OK_Static_Expression (N) then
7185 return Expr_Value (N);
7186 else
7187 pragma Assert (Nkind (N) = N_Range);
7188 return Expr_Value (High_Bound (N));
7189 end if;
7190 end Hi_Val;
7192 --------------
7193 -- Is_False --
7194 --------------
7196 function Is_False (R : RList) return Boolean is
7197 begin
7198 return R'Length = 0;
7199 end Is_False;
7201 -------------
7202 -- Is_True --
7203 -------------
7205 function Is_True (R : RList) return Boolean is
7206 begin
7207 return R'Length = 1
7208 and then R (R'First).Lo = BLo
7209 and then R (R'First).Hi = BHi;
7210 end Is_True;
7212 -----------------
7213 -- Is_Type_Ref --
7214 -----------------
7216 function Is_Type_Ref (N : Node_Id) return Boolean is
7217 begin
7218 return Nkind (N) = N_Identifier
7219 and then Chars (N) = Nam
7220 and then Paren_Count (N) = 0;
7221 end Is_Type_Ref;
7223 ------------
7224 -- Lo_Val --
7225 ------------
7227 function Lo_Val (N : Node_Id) return Uint is
7228 begin
7229 if Is_OK_Static_Expression (N) then
7230 return Expr_Value (N);
7231 else
7232 pragma Assert (Nkind (N) = N_Range);
7233 return Expr_Value (Low_Bound (N));
7234 end if;
7235 end Lo_Val;
7237 ------------------------
7238 -- Membership_Entries --
7239 ------------------------
7241 function Membership_Entries (N : Node_Id) return RList is
7242 begin
7243 if No (Next (N)) then
7244 return Membership_Entry (N);
7245 else
7246 return Membership_Entry (N) or Membership_Entries (Next (N));
7247 end if;
7248 end Membership_Entries;
7250 ----------------------
7251 -- Membership_Entry --
7252 ----------------------
7254 function Membership_Entry (N : Node_Id) return RList is
7255 Val : Uint;
7256 SLo : Uint;
7257 SHi : Uint;
7259 begin
7260 -- Range case
7262 if Nkind (N) = N_Range then
7263 if not Is_OK_Static_Expression (Low_Bound (N))
7264 or else
7265 not Is_OK_Static_Expression (High_Bound (N))
7266 then
7267 raise Non_Static;
7268 else
7269 SLo := Expr_Value (Low_Bound (N));
7270 SHi := Expr_Value (High_Bound (N));
7271 return RList'(1 => REnt'(SLo, SHi));
7272 end if;
7274 -- Static expression case
7276 elsif Is_OK_Static_Expression (N) then
7277 Val := Expr_Value (N);
7278 return RList'(1 => REnt'(Val, Val));
7280 -- Identifier (other than static expression) case
7282 else pragma Assert (Nkind (N) = N_Identifier);
7284 -- Type case
7286 if Is_Type (Entity (N)) then
7288 -- If type has predicates, process them
7290 if Has_Predicates (Entity (N)) then
7291 return Stat_Pred (Entity (N));
7293 -- For static subtype without predicates, get range
7295 elsif Is_OK_Static_Subtype (Entity (N)) then
7296 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7297 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7298 return RList'(1 => REnt'(SLo, SHi));
7300 -- Any other type makes us non-static
7302 else
7303 raise Non_Static;
7304 end if;
7306 -- Any other kind of identifier in predicate (e.g. a non-static
7307 -- expression value) means this is not a static predicate.
7309 else
7310 raise Non_Static;
7311 end if;
7312 end if;
7313 end Membership_Entry;
7315 ---------------
7316 -- Stat_Pred --
7317 ---------------
7319 function Stat_Pred (Typ : Entity_Id) return RList is
7320 begin
7321 -- Not static if type does not have static predicates
7323 if not Has_Static_Predicate (Typ) then
7324 raise Non_Static;
7325 end if;
7327 -- Otherwise we convert the predicate list to a range list
7329 declare
7330 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7331 Result : RList (1 .. List_Length (Spred));
7332 P : Node_Id;
7334 begin
7335 P := First (Static_Discrete_Predicate (Typ));
7336 for J in Result'Range loop
7337 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7338 Next (P);
7339 end loop;
7341 return Result;
7342 end;
7343 end Stat_Pred;
7345 -- Start of processing for Build_Discrete_Static_Predicate
7347 begin
7348 -- Analyze the expression to see if it is a static predicate
7350 declare
7351 Ranges : constant RList := Get_RList (Expr);
7352 -- Range list from expression if it is static
7354 Plist : List_Id;
7356 begin
7357 -- Convert range list into a form for the static predicate. In the
7358 -- Ranges array, we just have raw ranges, these must be converted
7359 -- to properly typed and analyzed static expressions or range nodes.
7361 -- Note: here we limit ranges to the ranges of the subtype, so that
7362 -- a predicate is always false for values outside the subtype. That
7363 -- seems fine, such values are invalid anyway, and considering them
7364 -- to fail the predicate seems allowed and friendly, and furthermore
7365 -- simplifies processing for case statements and loops.
7367 Plist := New_List;
7369 for J in Ranges'Range loop
7370 declare
7371 Lo : Uint := Ranges (J).Lo;
7372 Hi : Uint := Ranges (J).Hi;
7374 begin
7375 -- Ignore completely out of range entry
7377 if Hi < TLo or else Lo > THi then
7378 null;
7380 -- Otherwise process entry
7382 else
7383 -- Adjust out of range value to subtype range
7385 if Lo < TLo then
7386 Lo := TLo;
7387 end if;
7389 if Hi > THi then
7390 Hi := THi;
7391 end if;
7393 -- Convert range into required form
7395 Append_To (Plist, Build_Range (Lo, Hi));
7396 end if;
7397 end;
7398 end loop;
7400 -- Processing was successful and all entries were static, so now we
7401 -- can store the result as the predicate list.
7403 Set_Static_Discrete_Predicate (Typ, Plist);
7405 -- The processing for static predicates put the expression into
7406 -- canonical form as a series of ranges. It also eliminated
7407 -- duplicates and collapsed and combined ranges. We might as well
7408 -- replace the alternatives list of the right operand of the
7409 -- membership test with the static predicate list, which will
7410 -- usually be more efficient.
7412 declare
7413 New_Alts : constant List_Id := New_List;
7414 Old_Node : Node_Id;
7415 New_Node : Node_Id;
7417 begin
7418 Old_Node := First (Plist);
7419 while Present (Old_Node) loop
7420 New_Node := New_Copy (Old_Node);
7422 if Nkind (New_Node) = N_Range then
7423 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7424 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7425 end if;
7427 Append_To (New_Alts, New_Node);
7428 Next (Old_Node);
7429 end loop;
7431 -- If empty list, replace by False
7433 if Is_Empty_List (New_Alts) then
7434 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7436 -- Else replace by set membership test
7438 else
7439 Rewrite (Expr,
7440 Make_In (Loc,
7441 Left_Opnd => Make_Identifier (Loc, Nam),
7442 Right_Opnd => Empty,
7443 Alternatives => New_Alts));
7445 -- Resolve new expression in function context
7447 Install_Formals (Predicate_Function (Typ));
7448 Push_Scope (Predicate_Function (Typ));
7449 Analyze_And_Resolve (Expr, Standard_Boolean);
7450 Pop_Scope;
7451 end if;
7452 end;
7453 end;
7455 -- If non-static, return doing nothing
7457 exception
7458 when Non_Static =>
7459 return;
7460 end Build_Discrete_Static_Predicate;
7462 -------------------------------------------
7463 -- Build_Invariant_Procedure_Declaration --
7464 -------------------------------------------
7466 function Build_Invariant_Procedure_Declaration
7467 (Typ : Entity_Id) return Node_Id
7469 Loc : constant Source_Ptr := Sloc (Typ);
7470 Object_Entity : constant Entity_Id :=
7471 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7472 Spec : Node_Id;
7473 SId : Entity_Id;
7475 begin
7476 Set_Etype (Object_Entity, Typ);
7478 -- Check for duplicate definiations.
7480 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7481 return Empty;
7482 end if;
7484 SId :=
7485 Make_Defining_Identifier (Loc,
7486 Chars => New_External_Name (Chars (Typ), "Invariant"));
7487 Set_Has_Invariants (Typ);
7488 Set_Ekind (SId, E_Procedure);
7489 Set_Etype (SId, Standard_Void_Type);
7490 Set_Is_Invariant_Procedure (SId);
7491 Set_Invariant_Procedure (Typ, SId);
7493 Spec :=
7494 Make_Procedure_Specification (Loc,
7495 Defining_Unit_Name => SId,
7496 Parameter_Specifications => New_List (
7497 Make_Parameter_Specification (Loc,
7498 Defining_Identifier => Object_Entity,
7499 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7501 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7502 end Build_Invariant_Procedure_Declaration;
7504 -------------------------------
7505 -- Build_Invariant_Procedure --
7506 -------------------------------
7508 -- The procedure that is constructed here has the form
7510 -- procedure typInvariant (Ixxx : typ) is
7511 -- begin
7512 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7513 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7514 -- ...
7515 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7516 -- ...
7517 -- end typInvariant;
7519 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7520 Loc : constant Source_Ptr := Sloc (Typ);
7521 Stmts : List_Id;
7522 Spec : Node_Id;
7523 SId : Entity_Id;
7524 PDecl : Node_Id;
7525 PBody : Node_Id;
7527 Nam : Name_Id;
7528 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7529 -- if we come from a Type_Invariant aspect, we make sure to build the
7530 -- Check pragma with the right name, so that Check_Policy works right.
7532 Visible_Decls : constant List_Id := Visible_Declarations (N);
7533 Private_Decls : constant List_Id := Private_Declarations (N);
7535 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7536 -- Appends statements to Stmts for any invariants in the rep item chain
7537 -- of the given type. If Inherit is False, then we only process entries
7538 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7539 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7540 -- "inherited" to the exception message and generating an informational
7541 -- message about the inheritance of an invariant.
7543 Object_Name : Name_Id;
7544 -- Name for argument of invariant procedure
7546 Object_Entity : Node_Id;
7547 -- The entity of the formal for the procedure
7549 --------------------
7550 -- Add_Invariants --
7551 --------------------
7553 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7554 Ritem : Node_Id;
7555 Arg1 : Node_Id;
7556 Arg2 : Node_Id;
7557 Arg3 : Node_Id;
7558 Exp : Node_Id;
7559 Loc : Source_Ptr;
7560 Assoc : List_Id;
7561 Str : String_Id;
7563 procedure Replace_Type_Reference (N : Node_Id);
7564 -- Replace a single occurrence N of the subtype name with a reference
7565 -- to the formal of the predicate function. N can be an identifier
7566 -- referencing the subtype, or a selected component, representing an
7567 -- appropriately qualified occurrence of the subtype name.
7569 procedure Replace_Type_References is
7570 new Replace_Type_References_Generic (Replace_Type_Reference);
7571 -- Traverse an expression replacing all occurrences of the subtype
7572 -- name with appropriate references to the object that is the formal
7573 -- parameter of the predicate function. Note that we must ensure
7574 -- that the type and entity information is properly set in the
7575 -- replacement node, since we will do a Preanalyze call of this
7576 -- expression without proper visibility of the procedure argument.
7578 ----------------------------
7579 -- Replace_Type_Reference --
7580 ----------------------------
7582 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7583 -- regarding handling of Sloc and Comes_From_Source.
7585 procedure Replace_Type_Reference (N : Node_Id) is
7586 begin
7588 -- Add semantic information to node to be rewritten, for ASIS
7589 -- navigation needs.
7591 if Nkind (N) = N_Identifier then
7592 Set_Entity (N, T);
7593 Set_Etype (N, T);
7595 elsif Nkind (N) = N_Selected_Component then
7596 Analyze (Prefix (N));
7597 Set_Entity (Selector_Name (N), T);
7598 Set_Etype (Selector_Name (N), T);
7599 end if;
7601 -- Invariant'Class, replace with T'Class (obj)
7602 -- In ASIS mode, an inherited item is analyzed already, and the
7603 -- replacement has been done, so do not repeat transformation
7604 -- to prevent ill-formed tree.
7606 if Class_Present (Ritem) then
7607 if ASIS_Mode
7608 and then Nkind (Parent (N)) = N_Attribute_Reference
7609 and then Attribute_Name (Parent (N)) = Name_Class
7610 then
7611 null;
7613 else
7614 Rewrite (N,
7615 Make_Type_Conversion (Sloc (N),
7616 Subtype_Mark =>
7617 Make_Attribute_Reference (Sloc (N),
7618 Prefix => New_Occurrence_Of (T, Sloc (N)),
7619 Attribute_Name => Name_Class),
7620 Expression =>
7621 Make_Identifier (Sloc (N), Object_Name)));
7623 Set_Entity (Expression (N), Object_Entity);
7624 Set_Etype (Expression (N), Typ);
7625 end if;
7627 -- Invariant, replace with obj
7629 else
7630 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7631 Set_Entity (N, Object_Entity);
7632 Set_Etype (N, Typ);
7633 end if;
7635 Set_Comes_From_Source (N, True);
7636 end Replace_Type_Reference;
7638 -- Start of processing for Add_Invariants
7640 begin
7641 Ritem := First_Rep_Item (T);
7642 while Present (Ritem) loop
7643 if Nkind (Ritem) = N_Pragma
7644 and then Pragma_Name (Ritem) = Name_Invariant
7645 then
7646 Arg1 := First (Pragma_Argument_Associations (Ritem));
7647 Arg2 := Next (Arg1);
7648 Arg3 := Next (Arg2);
7650 Arg1 := Get_Pragma_Arg (Arg1);
7651 Arg2 := Get_Pragma_Arg (Arg2);
7653 -- For Inherit case, ignore Invariant, process only Class case
7655 if Inherit then
7656 if not Class_Present (Ritem) then
7657 goto Continue;
7658 end if;
7660 -- For Inherit false, process only item for right type
7662 else
7663 if Entity (Arg1) /= Typ then
7664 goto Continue;
7665 end if;
7666 end if;
7668 if No (Stmts) then
7669 Stmts := Empty_List;
7670 end if;
7672 Exp := New_Copy_Tree (Arg2);
7674 -- Preserve sloc of original pragma Invariant
7676 Loc := Sloc (Ritem);
7678 -- We need to replace any occurrences of the name of the type
7679 -- with references to the object, converted to type'Class in
7680 -- the case of Invariant'Class aspects.
7682 Replace_Type_References (Exp, T);
7684 -- If this invariant comes from an aspect, find the aspect
7685 -- specification, and replace the saved expression because
7686 -- we need the subtype references replaced for the calls to
7687 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7688 -- and Check_Aspect_At_End_Of_Declarations.
7690 if From_Aspect_Specification (Ritem) then
7691 declare
7692 Aitem : Node_Id;
7694 begin
7695 -- Loop to find corresponding aspect, note that this
7696 -- must be present given the pragma is marked delayed.
7698 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7699 -- this loop does nothing. Furthermore, why isn't this
7700 -- simply Corresponding_Aspect ???
7702 Aitem := Next_Rep_Item (Ritem);
7703 while Present (Aitem) loop
7704 if Nkind (Aitem) = N_Aspect_Specification
7705 and then Aspect_Rep_Item (Aitem) = Ritem
7706 then
7707 Set_Entity
7708 (Identifier (Aitem), New_Copy_Tree (Exp));
7709 exit;
7710 end if;
7712 Aitem := Next_Rep_Item (Aitem);
7713 end loop;
7714 end;
7715 end if;
7717 -- Now we need to preanalyze the expression to properly capture
7718 -- the visibility in the visible part. The expression will not
7719 -- be analyzed for real until the body is analyzed, but that is
7720 -- at the end of the private part and has the wrong visibility.
7722 Set_Parent (Exp, N);
7723 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
7725 -- A class-wide invariant may be inherited in a separate unit,
7726 -- where the corresponding expression cannot be resolved by
7727 -- visibility, because it refers to a local function. Propagate
7728 -- semantic information to the original representation item, to
7729 -- be used when an invariant procedure for a derived type is
7730 -- constructed.
7732 -- Unclear how to handle class-wide invariants that are not
7733 -- function calls ???
7735 if not Inherit
7736 and then Class_Present (Ritem)
7737 and then Nkind (Exp) = N_Function_Call
7738 and then Nkind (Arg2) = N_Indexed_Component
7739 then
7740 Rewrite (Arg2,
7741 Make_Function_Call (Loc,
7742 Name =>
7743 New_Occurrence_Of (Entity (Name (Exp)), Loc),
7744 Parameter_Associations =>
7745 New_Copy_List (Expressions (Arg2))));
7746 end if;
7748 -- In ASIS mode, even if assertions are not enabled, we must
7749 -- analyze the original expression in the aspect specification
7750 -- because it is part of the original tree.
7752 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7753 declare
7754 Inv : constant Node_Id :=
7755 Expression (Corresponding_Aspect (Ritem));
7756 begin
7757 Replace_Type_References (Inv, T);
7758 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7759 end;
7760 end if;
7762 -- Get name to be used for Check pragma
7764 if not From_Aspect_Specification (Ritem) then
7765 Nam := Name_Invariant;
7766 else
7767 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7768 end if;
7770 -- Build first two arguments for Check pragma
7772 Assoc :=
7773 New_List (
7774 Make_Pragma_Argument_Association (Loc,
7775 Expression => Make_Identifier (Loc, Chars => Nam)),
7776 Make_Pragma_Argument_Association (Loc,
7777 Expression => Exp));
7779 -- Add message if present in Invariant pragma
7781 if Present (Arg3) then
7782 Str := Strval (Get_Pragma_Arg (Arg3));
7784 -- If inherited case, and message starts "failed invariant",
7785 -- change it to be "failed inherited invariant".
7787 if Inherit then
7788 String_To_Name_Buffer (Str);
7790 if Name_Buffer (1 .. 16) = "failed invariant" then
7791 Insert_Str_In_Name_Buffer ("inherited ", 8);
7792 Str := String_From_Name_Buffer;
7793 end if;
7794 end if;
7796 Append_To (Assoc,
7797 Make_Pragma_Argument_Association (Loc,
7798 Expression => Make_String_Literal (Loc, Str)));
7799 end if;
7801 -- Add Check pragma to list of statements
7803 Append_To (Stmts,
7804 Make_Pragma (Loc,
7805 Pragma_Identifier =>
7806 Make_Identifier (Loc, Name_Check),
7807 Pragma_Argument_Associations => Assoc));
7809 -- If Inherited case and option enabled, output info msg. Note
7810 -- that we know this is a case of Invariant'Class.
7812 if Inherit and Opt.List_Inherited_Aspects then
7813 Error_Msg_Sloc := Sloc (Ritem);
7814 Error_Msg_N
7815 ("info: & inherits `Invariant''Class` aspect from #?L?",
7816 Typ);
7817 end if;
7818 end if;
7820 <<Continue>>
7821 Next_Rep_Item (Ritem);
7822 end loop;
7823 end Add_Invariants;
7825 -- Start of processing for Build_Invariant_Procedure
7827 begin
7828 Stmts := No_List;
7829 PDecl := Empty;
7830 PBody := Empty;
7831 SId := Empty;
7833 -- If the aspect specification exists for some view of the type, the
7834 -- declaration for the procedure has been created.
7836 if Has_Invariants (Typ) then
7837 SId := Invariant_Procedure (Typ);
7838 end if;
7840 -- If the body is already present, nothing to do. This will occur when
7841 -- the type is already frozen, which is the case when the invariant
7842 -- appears in a private part, and the freezing takes place before the
7843 -- final pass over full declarations.
7845 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
7847 if Present (SId) then
7848 PDecl := Unit_Declaration_Node (SId);
7850 if Present (PDecl)
7851 and then Nkind (PDecl) = N_Subprogram_Declaration
7852 and then Present (Corresponding_Body (PDecl))
7853 then
7854 return;
7855 end if;
7857 else
7858 PDecl := Build_Invariant_Procedure_Declaration (Typ);
7859 end if;
7861 -- Recover formal of procedure, for use in the calls to invariant
7862 -- functions (including inherited ones).
7864 Object_Entity :=
7865 Defining_Identifier
7866 (First (Parameter_Specifications (Specification (PDecl))));
7867 Object_Name := Chars (Object_Entity);
7869 -- Add invariants for the current type
7871 Add_Invariants (Typ, Inherit => False);
7873 -- Add invariants for parent types
7875 declare
7876 Current_Typ : Entity_Id;
7877 Parent_Typ : Entity_Id;
7879 begin
7880 Current_Typ := Typ;
7881 loop
7882 Parent_Typ := Etype (Current_Typ);
7884 if Is_Private_Type (Parent_Typ)
7885 and then Present (Full_View (Base_Type (Parent_Typ)))
7886 then
7887 Parent_Typ := Full_View (Base_Type (Parent_Typ));
7888 end if;
7890 exit when Parent_Typ = Current_Typ;
7892 Current_Typ := Parent_Typ;
7893 Add_Invariants (Current_Typ, Inherit => True);
7894 end loop;
7895 end;
7897 -- Build the procedure if we generated at least one Check pragma
7899 if Stmts /= No_List then
7900 Spec := Copy_Separate_Tree (Specification (PDecl));
7902 PBody :=
7903 Make_Subprogram_Body (Loc,
7904 Specification => Spec,
7905 Declarations => Empty_List,
7906 Handled_Statement_Sequence =>
7907 Make_Handled_Sequence_Of_Statements (Loc,
7908 Statements => Stmts));
7910 -- Insert procedure declaration and spec at the appropriate points.
7911 -- If declaration is already analyzed, it was processed by the
7912 -- generated pragma.
7914 if Present (Private_Decls) then
7916 -- The spec goes at the end of visible declarations, but they have
7917 -- already been analyzed, so we need to explicitly do the analyze.
7919 if not Analyzed (PDecl) then
7920 Append_To (Visible_Decls, PDecl);
7921 Analyze (PDecl);
7922 end if;
7924 -- The body goes at the end of the private declarations, which we
7925 -- have not analyzed yet, so we do not need to perform an explicit
7926 -- analyze call. We skip this if there are no private declarations
7927 -- (this is an error that will be caught elsewhere);
7929 Append_To (Private_Decls, PBody);
7931 -- If the invariant appears on the full view of a type, the
7932 -- analysis of the private part is complete, and we must
7933 -- analyze the new body explicitly.
7935 if In_Private_Part (Current_Scope) then
7936 Analyze (PBody);
7937 end if;
7939 -- If there are no private declarations this may be an error that
7940 -- will be diagnosed elsewhere. However, if this is a non-private
7941 -- type that inherits invariants, it needs no completion and there
7942 -- may be no private part. In this case insert invariant procedure
7943 -- at end of current declarative list, and analyze at once, given
7944 -- that the type is about to be frozen.
7946 elsif not Is_Private_Type (Typ) then
7947 Append_To (Visible_Decls, PDecl);
7948 Append_To (Visible_Decls, PBody);
7949 Analyze (PDecl);
7950 Analyze (PBody);
7951 end if;
7952 end if;
7953 end Build_Invariant_Procedure;
7955 -------------------------------
7956 -- Build_Predicate_Functions --
7957 -------------------------------
7959 -- The procedures that are constructed here have the form:
7961 -- function typPredicate (Ixxx : typ) return Boolean is
7962 -- begin
7963 -- return
7964 -- exp1 and then exp2 and then ...
7965 -- and then typ1Predicate (typ1 (Ixxx))
7966 -- and then typ2Predicate (typ2 (Ixxx))
7967 -- and then ...;
7968 -- end typPredicate;
7970 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
7971 -- this is the point at which these expressions get analyzed, providing the
7972 -- required delay, and typ1, typ2, are entities from which predicates are
7973 -- inherited. Note that we do NOT generate Check pragmas, that's because we
7974 -- use this function even if checks are off, e.g. for membership tests.
7976 -- If the expression has at least one Raise_Expression, then we also build
7977 -- the typPredicateM version of the function, in which any occurrence of a
7978 -- Raise_Expression is converted to "return False".
7980 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
7981 Loc : constant Source_Ptr := Sloc (Typ);
7983 Expr : Node_Id;
7984 -- This is the expression for the result of the function. It is
7985 -- is build by connecting the component predicates with AND THEN.
7987 Expr_M : Node_Id;
7988 -- This is the corresponding return expression for the Predicate_M
7989 -- function. It differs in that raise expressions are marked for
7990 -- special expansion (see Process_REs).
7992 Object_Name : constant Name_Id := New_Internal_Name ('I');
7993 -- Name for argument of Predicate procedure. Note that we use the same
7994 -- name for both predicate functions. That way the reference within the
7995 -- predicate expression is the same in both functions.
7997 Object_Entity : constant Entity_Id :=
7998 Make_Defining_Identifier (Loc, Chars => Object_Name);
7999 -- Entity for argument of Predicate procedure
8001 Object_Entity_M : constant Entity_Id :=
8002 Make_Defining_Identifier (Loc, Chars => Object_Name);
8003 -- Entity for argument of Predicate_M procedure
8005 Raise_Expression_Present : Boolean := False;
8006 -- Set True if Expr has at least one Raise_Expression
8008 procedure Add_Call (T : Entity_Id);
8009 -- Includes a call to the predicate function for type T in Expr if T
8010 -- has predicates and Predicate_Function (T) is non-empty.
8012 procedure Add_Predicates;
8013 -- Appends expressions for any Predicate pragmas in the rep item chain
8014 -- Typ to Expr. Note that we look only at items for this exact entity.
8015 -- Inheritance of predicates for the parent type is done by calling the
8016 -- Predicate_Function of the parent type, using Add_Call above.
8018 function Test_RE (N : Node_Id) return Traverse_Result;
8019 -- Used in Test_REs, tests one node for being a raise expression, and if
8020 -- so sets Raise_Expression_Present True.
8022 procedure Test_REs is new Traverse_Proc (Test_RE);
8023 -- Tests to see if Expr contains any raise expressions
8025 function Process_RE (N : Node_Id) return Traverse_Result;
8026 -- Used in Process REs, tests if node N is a raise expression, and if
8027 -- so, marks it to be converted to return False.
8029 procedure Process_REs is new Traverse_Proc (Process_RE);
8030 -- Marks any raise expressions in Expr_M to return False
8032 --------------
8033 -- Add_Call --
8034 --------------
8036 procedure Add_Call (T : Entity_Id) is
8037 Exp : Node_Id;
8039 begin
8040 if Present (T) and then Present (Predicate_Function (T)) then
8041 Set_Has_Predicates (Typ);
8043 -- Build the call to the predicate function of T
8045 Exp :=
8046 Make_Predicate_Call
8047 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8049 -- Add call to evolving expression, using AND THEN if needed
8051 if No (Expr) then
8052 Expr := Exp;
8054 else
8055 Expr :=
8056 Make_And_Then (Sloc (Expr),
8057 Left_Opnd => Relocate_Node (Expr),
8058 Right_Opnd => Exp);
8059 end if;
8061 -- Output info message on inheritance if required. Note we do not
8062 -- give this information for generic actual types, since it is
8063 -- unwelcome noise in that case in instantiations. We also
8064 -- generally suppress the message in instantiations, and also
8065 -- if it involves internal names.
8067 if Opt.List_Inherited_Aspects
8068 and then not Is_Generic_Actual_Type (Typ)
8069 and then Instantiation_Depth (Sloc (Typ)) = 0
8070 and then not Is_Internal_Name (Chars (T))
8071 and then not Is_Internal_Name (Chars (Typ))
8072 then
8073 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8074 Error_Msg_Node_2 := T;
8075 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8076 end if;
8077 end if;
8078 end Add_Call;
8080 --------------------
8081 -- Add_Predicates --
8082 --------------------
8084 procedure Add_Predicates is
8085 Ritem : Node_Id;
8086 Arg1 : Node_Id;
8087 Arg2 : Node_Id;
8089 procedure Replace_Type_Reference (N : Node_Id);
8090 -- Replace a single occurrence N of the subtype name with a reference
8091 -- to the formal of the predicate function. N can be an identifier
8092 -- referencing the subtype, or a selected component, representing an
8093 -- appropriately qualified occurrence of the subtype name.
8095 procedure Replace_Type_References is
8096 new Replace_Type_References_Generic (Replace_Type_Reference);
8097 -- Traverse an expression changing every occurrence of an identifier
8098 -- whose name matches the name of the subtype with a reference to
8099 -- the formal parameter of the predicate function.
8101 ----------------------------
8102 -- Replace_Type_Reference --
8103 ----------------------------
8105 procedure Replace_Type_Reference (N : Node_Id) is
8106 begin
8107 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8108 -- Use the Sloc of the usage name, not the defining name
8110 Set_Etype (N, Typ);
8111 Set_Entity (N, Object_Entity);
8113 -- We want to treat the node as if it comes from source, so that
8114 -- ASIS will not ignore it
8116 Set_Comes_From_Source (N, True);
8117 end Replace_Type_Reference;
8119 -- Start of processing for Add_Predicates
8121 begin
8122 Ritem := First_Rep_Item (Typ);
8123 while Present (Ritem) loop
8124 if Nkind (Ritem) = N_Pragma
8125 and then Pragma_Name (Ritem) = Name_Predicate
8126 then
8127 -- Acquire arguments
8129 Arg1 := First (Pragma_Argument_Associations (Ritem));
8130 Arg2 := Next (Arg1);
8132 Arg1 := Get_Pragma_Arg (Arg1);
8133 Arg2 := Get_Pragma_Arg (Arg2);
8135 -- See if this predicate pragma is for the current type or for
8136 -- its full view. A predicate on a private completion is placed
8137 -- on the partial view beause this is the visible entity that
8138 -- is frozen.
8140 if Entity (Arg1) = Typ
8141 or else Full_View (Entity (Arg1)) = Typ
8142 then
8143 -- We have a match, this entry is for our subtype
8145 -- We need to replace any occurrences of the name of the
8146 -- type with references to the object.
8148 Replace_Type_References (Arg2, Typ);
8150 -- If this predicate comes from an aspect, find the aspect
8151 -- specification, and replace the saved expression because
8152 -- we need the subtype references replaced for the calls to
8153 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8154 -- and Check_Aspect_At_End_Of_Declarations.
8156 if From_Aspect_Specification (Ritem) then
8157 declare
8158 Aitem : Node_Id;
8160 begin
8161 -- Loop to find corresponding aspect, note that this
8162 -- must be present given the pragma is marked delayed.
8164 Aitem := Next_Rep_Item (Ritem);
8165 loop
8166 if Nkind (Aitem) = N_Aspect_Specification
8167 and then Aspect_Rep_Item (Aitem) = Ritem
8168 then
8169 Set_Entity
8170 (Identifier (Aitem), New_Copy_Tree (Arg2));
8171 exit;
8172 end if;
8174 Aitem := Next_Rep_Item (Aitem);
8175 end loop;
8176 end;
8177 end if;
8179 -- Now we can add the expression
8181 if No (Expr) then
8182 Expr := Relocate_Node (Arg2);
8184 -- There already was a predicate, so add to it
8186 else
8187 Expr :=
8188 Make_And_Then (Loc,
8189 Left_Opnd => Relocate_Node (Expr),
8190 Right_Opnd => Relocate_Node (Arg2));
8191 end if;
8192 end if;
8193 end if;
8195 Next_Rep_Item (Ritem);
8196 end loop;
8197 end Add_Predicates;
8199 ----------------
8200 -- Process_RE --
8201 ----------------
8203 function Process_RE (N : Node_Id) return Traverse_Result is
8204 begin
8205 if Nkind (N) = N_Raise_Expression then
8206 Set_Convert_To_Return_False (N);
8207 return Skip;
8208 else
8209 return OK;
8210 end if;
8211 end Process_RE;
8213 -------------
8214 -- Test_RE --
8215 -------------
8217 function Test_RE (N : Node_Id) return Traverse_Result is
8218 begin
8219 if Nkind (N) = N_Raise_Expression then
8220 Raise_Expression_Present := True;
8221 return Abandon;
8222 else
8223 return OK;
8224 end if;
8225 end Test_RE;
8227 -- Start of processing for Build_Predicate_Functions
8229 begin
8230 -- Return if already built or if type does not have predicates
8232 if not Has_Predicates (Typ)
8233 or else Present (Predicate_Function (Typ))
8234 then
8235 return;
8236 end if;
8238 -- Prepare to construct predicate expression
8240 Expr := Empty;
8242 -- Add Predicates for the current type
8244 Add_Predicates;
8246 -- Add predicates for ancestor if present
8248 declare
8249 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8250 begin
8251 if Present (Atyp) then
8252 Add_Call (Atyp);
8253 end if;
8254 end;
8256 -- Case where predicates are present
8258 if Present (Expr) then
8260 -- Test for raise expression present
8262 Test_REs (Expr);
8264 -- If raise expression is present, capture a copy of Expr for use
8265 -- in building the predicateM function version later on. For this
8266 -- copy we replace references to Object_Entity by Object_Entity_M.
8268 if Raise_Expression_Present then
8269 declare
8270 Map : constant Elist_Id := New_Elmt_List;
8271 begin
8272 Append_Elmt (Object_Entity, Map);
8273 Append_Elmt (Object_Entity_M, Map);
8274 Expr_M := New_Copy_Tree (Expr, Map => Map);
8275 end;
8276 end if;
8278 -- Build the main predicate function
8280 declare
8281 SId : constant Entity_Id :=
8282 Make_Defining_Identifier (Loc,
8283 Chars => New_External_Name (Chars (Typ), "Predicate"));
8284 -- The entity for the the function spec
8286 SIdB : constant Entity_Id :=
8287 Make_Defining_Identifier (Loc,
8288 Chars => New_External_Name (Chars (Typ), "Predicate"));
8289 -- The entity for the function body
8291 Spec : Node_Id;
8292 FDecl : Node_Id;
8293 FBody : Node_Id;
8295 begin
8296 -- Build function declaration
8298 Set_Ekind (SId, E_Function);
8299 Set_Is_Internal (SId);
8300 Set_Is_Predicate_Function (SId);
8301 Set_Predicate_Function (Typ, SId);
8303 -- The predicate function is shared between views of a type
8305 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8306 Set_Predicate_Function (Full_View (Typ), SId);
8307 end if;
8309 Spec :=
8310 Make_Function_Specification (Loc,
8311 Defining_Unit_Name => SId,
8312 Parameter_Specifications => New_List (
8313 Make_Parameter_Specification (Loc,
8314 Defining_Identifier => Object_Entity,
8315 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8316 Result_Definition =>
8317 New_Occurrence_Of (Standard_Boolean, Loc));
8319 FDecl :=
8320 Make_Subprogram_Declaration (Loc,
8321 Specification => Spec);
8323 -- Build function body
8325 Spec :=
8326 Make_Function_Specification (Loc,
8327 Defining_Unit_Name => SIdB,
8328 Parameter_Specifications => New_List (
8329 Make_Parameter_Specification (Loc,
8330 Defining_Identifier =>
8331 Make_Defining_Identifier (Loc, Object_Name),
8332 Parameter_Type =>
8333 New_Occurrence_Of (Typ, Loc))),
8334 Result_Definition =>
8335 New_Occurrence_Of (Standard_Boolean, Loc));
8337 FBody :=
8338 Make_Subprogram_Body (Loc,
8339 Specification => Spec,
8340 Declarations => Empty_List,
8341 Handled_Statement_Sequence =>
8342 Make_Handled_Sequence_Of_Statements (Loc,
8343 Statements => New_List (
8344 Make_Simple_Return_Statement (Loc,
8345 Expression => Expr))));
8347 -- Insert declaration before freeze node and body after
8349 Insert_Before_And_Analyze (N, FDecl);
8350 Insert_After_And_Analyze (N, FBody);
8351 end;
8353 -- Test for raise expressions present and if so build M version
8355 if Raise_Expression_Present then
8356 declare
8357 SId : constant Entity_Id :=
8358 Make_Defining_Identifier (Loc,
8359 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8360 -- The entity for the the function spec
8362 SIdB : constant Entity_Id :=
8363 Make_Defining_Identifier (Loc,
8364 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8365 -- The entity for the function body
8367 Spec : Node_Id;
8368 FDecl : Node_Id;
8369 FBody : Node_Id;
8370 BTemp : Entity_Id;
8372 begin
8373 -- Mark any raise expressions for special expansion
8375 Process_REs (Expr_M);
8377 -- Build function declaration
8379 Set_Ekind (SId, E_Function);
8380 Set_Is_Predicate_Function_M (SId);
8381 Set_Predicate_Function_M (Typ, SId);
8383 -- The predicate function is shared between views of a type
8385 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8386 Set_Predicate_Function_M (Full_View (Typ), SId);
8387 end if;
8389 Spec :=
8390 Make_Function_Specification (Loc,
8391 Defining_Unit_Name => SId,
8392 Parameter_Specifications => New_List (
8393 Make_Parameter_Specification (Loc,
8394 Defining_Identifier => Object_Entity_M,
8395 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8396 Result_Definition =>
8397 New_Occurrence_Of (Standard_Boolean, Loc));
8399 FDecl :=
8400 Make_Subprogram_Declaration (Loc,
8401 Specification => Spec);
8403 -- Build function body
8405 Spec :=
8406 Make_Function_Specification (Loc,
8407 Defining_Unit_Name => SIdB,
8408 Parameter_Specifications => New_List (
8409 Make_Parameter_Specification (Loc,
8410 Defining_Identifier =>
8411 Make_Defining_Identifier (Loc, Object_Name),
8412 Parameter_Type =>
8413 New_Occurrence_Of (Typ, Loc))),
8414 Result_Definition =>
8415 New_Occurrence_Of (Standard_Boolean, Loc));
8417 -- Build the body, we declare the boolean expression before
8418 -- doing the return, because we are not really confident of
8419 -- what happens if a return appears within a return.
8421 BTemp :=
8422 Make_Defining_Identifier (Loc,
8423 Chars => New_Internal_Name ('B'));
8425 FBody :=
8426 Make_Subprogram_Body (Loc,
8427 Specification => Spec,
8429 Declarations => New_List (
8430 Make_Object_Declaration (Loc,
8431 Defining_Identifier => BTemp,
8432 Constant_Present => True,
8433 Object_Definition =>
8434 New_Occurrence_Of (Standard_Boolean, Loc),
8435 Expression => Expr_M)),
8437 Handled_Statement_Sequence =>
8438 Make_Handled_Sequence_Of_Statements (Loc,
8439 Statements => New_List (
8440 Make_Simple_Return_Statement (Loc,
8441 Expression => New_Occurrence_Of (BTemp, Loc)))));
8443 -- Insert declaration before freeze node and body after
8445 Insert_Before_And_Analyze (N, FDecl);
8446 Insert_After_And_Analyze (N, FBody);
8447 end;
8448 end if;
8450 -- See if we have a static predicate. Note that the answer may be
8451 -- yes even if we have an explicit Dynamic_Predicate present.
8453 declare
8454 PS : Boolean;
8455 EN : Node_Id;
8457 begin
8458 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8459 PS := False;
8460 else
8461 PS := Is_Predicate_Static (Expr, Object_Name);
8462 end if;
8464 -- Case where we have a predicate-static aspect
8466 if PS then
8468 -- We don't set Has_Static_Predicate_Aspect, since we can have
8469 -- any of the three cases (Predicate, Dynamic_Predicate, or
8470 -- Static_Predicate) generating a predicate with an expression
8471 -- that is predicate-static. We just indicate that we have a
8472 -- predicate that can be treated as static.
8474 Set_Has_Static_Predicate (Typ);
8476 -- For discrete subtype, build the static predicate list
8478 if Is_Discrete_Type (Typ) then
8479 if not Is_Static_Subtype (Typ) then
8481 -- This can only happen in the presence of previous
8482 -- semantic errors.
8484 pragma Assert (Serious_Errors_Detected > 0);
8485 return;
8486 end if;
8488 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8490 -- If we don't get a static predicate list, it means that we
8491 -- have a case where this is not possible, most typically in
8492 -- the case where we inherit a dynamic predicate. We do not
8493 -- consider this an error, we just leave the predicate as
8494 -- dynamic. But if we do succeed in building the list, then
8495 -- we mark the predicate as static.
8497 if No (Static_Discrete_Predicate (Typ)) then
8498 Set_Has_Static_Predicate (Typ, False);
8499 end if;
8501 -- For real or string subtype, save predicate expression
8503 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8504 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8505 end if;
8507 -- Case of dynamic predicate (expression is not predicate-static)
8509 else
8510 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8511 -- is only set if we have an explicit Dynamic_Predicate aspect
8512 -- given. Here we may simply have a Predicate aspect where the
8513 -- expression happens not to be predicate-static.
8515 -- Emit an error when the predicate is categorized as static
8516 -- but its expression is not predicate-static.
8518 -- First a little fiddling to get a nice location for the
8519 -- message. If the expression is of the form (A and then B),
8520 -- then use the left operand for the Sloc. This avoids getting
8521 -- confused by a call to a higher-level predicate with a less
8522 -- convenient source location.
8524 EN := Expr;
8525 while Nkind (EN) = N_And_Then loop
8526 EN := Left_Opnd (EN);
8527 end loop;
8529 -- Now post appropriate message
8531 if Has_Static_Predicate_Aspect (Typ) then
8532 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8533 Error_Msg_F
8534 ("expression is not predicate-static (RM 3.2.4(16-22))",
8535 EN);
8536 else
8537 Error_Msg_F
8538 ("static predicate requires scalar or string type", EN);
8539 end if;
8540 end if;
8541 end if;
8542 end;
8543 end if;
8544 end Build_Predicate_Functions;
8546 -----------------------------------------
8547 -- Check_Aspect_At_End_Of_Declarations --
8548 -----------------------------------------
8550 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8551 Ent : constant Entity_Id := Entity (ASN);
8552 Ident : constant Node_Id := Identifier (ASN);
8553 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8555 End_Decl_Expr : constant Node_Id := Entity (Ident);
8556 -- Expression to be analyzed at end of declarations
8558 Freeze_Expr : constant Node_Id := Expression (ASN);
8559 -- Expression from call to Check_Aspect_At_Freeze_Point
8561 T : constant Entity_Id := Etype (Freeze_Expr);
8562 -- Type required for preanalyze call
8564 Err : Boolean;
8565 -- Set False if error
8567 -- On entry to this procedure, Entity (Ident) contains a copy of the
8568 -- original expression from the aspect, saved for this purpose, and
8569 -- but Expression (Ident) is a preanalyzed copy of the expression,
8570 -- preanalyzed just after the freeze point.
8572 procedure Check_Overloaded_Name;
8573 -- For aspects whose expression is simply a name, this routine checks if
8574 -- the name is overloaded or not. If so, it verifies there is an
8575 -- interpretation that matches the entity obtained at the freeze point,
8576 -- otherwise the compiler complains.
8578 ---------------------------
8579 -- Check_Overloaded_Name --
8580 ---------------------------
8582 procedure Check_Overloaded_Name is
8583 begin
8584 if not Is_Overloaded (End_Decl_Expr) then
8585 Err := not Is_Entity_Name (End_Decl_Expr)
8586 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8588 else
8589 Err := True;
8591 declare
8592 Index : Interp_Index;
8593 It : Interp;
8595 begin
8596 Get_First_Interp (End_Decl_Expr, Index, It);
8597 while Present (It.Typ) loop
8598 if It.Nam = Entity (Freeze_Expr) then
8599 Err := False;
8600 exit;
8601 end if;
8603 Get_Next_Interp (Index, It);
8604 end loop;
8605 end;
8606 end if;
8607 end Check_Overloaded_Name;
8609 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8611 begin
8612 -- Case of aspects Dimension, Dimension_System and Synchronization
8614 if A_Id = Aspect_Synchronization then
8615 return;
8617 -- Case of stream attributes, just have to compare entities. However,
8618 -- the expression is just a name (possibly overloaded), and there may
8619 -- be stream operations declared for unrelated types, so we just need
8620 -- to verify that one of these interpretations is the one available at
8621 -- at the freeze point.
8623 elsif A_Id = Aspect_Input or else
8624 A_Id = Aspect_Output or else
8625 A_Id = Aspect_Read or else
8626 A_Id = Aspect_Write
8627 then
8628 Analyze (End_Decl_Expr);
8629 Check_Overloaded_Name;
8631 elsif A_Id = Aspect_Variable_Indexing or else
8632 A_Id = Aspect_Constant_Indexing or else
8633 A_Id = Aspect_Default_Iterator or else
8634 A_Id = Aspect_Iterator_Element
8635 then
8636 -- Make type unfrozen before analysis, to prevent spurious errors
8637 -- about late attributes.
8639 Set_Is_Frozen (Ent, False);
8640 Analyze (End_Decl_Expr);
8641 Set_Is_Frozen (Ent, True);
8643 -- If the end of declarations comes before any other freeze
8644 -- point, the Freeze_Expr is not analyzed: no check needed.
8646 if Analyzed (Freeze_Expr) and then not In_Instance then
8647 Check_Overloaded_Name;
8648 else
8649 Err := False;
8650 end if;
8652 -- All other cases
8654 else
8655 -- Indicate that the expression comes from an aspect specification,
8656 -- which is used in subsequent analysis even if expansion is off.
8658 Set_Parent (End_Decl_Expr, ASN);
8660 -- In a generic context the aspect expressions have not been
8661 -- preanalyzed, so do it now. There are no conformance checks
8662 -- to perform in this case.
8664 if No (T) then
8665 Check_Aspect_At_Freeze_Point (ASN);
8666 return;
8668 -- The default values attributes may be defined in the private part,
8669 -- and the analysis of the expression may take place when only the
8670 -- partial view is visible. The expression must be scalar, so use
8671 -- the full view to resolve.
8673 elsif (A_Id = Aspect_Default_Value
8674 or else
8675 A_Id = Aspect_Default_Component_Value)
8676 and then Is_Private_Type (T)
8677 then
8678 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8680 else
8681 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8682 end if;
8684 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8685 end if;
8687 -- Output error message if error. Force error on aspect specification
8688 -- even if there is an error on the expression itself.
8690 if Err then
8691 Error_Msg_NE
8692 ("!visibility of aspect for& changes after freeze point",
8693 ASN, Ent);
8694 Error_Msg_NE
8695 ("info: & is frozen here, aspects evaluated at this point??",
8696 Freeze_Node (Ent), Ent);
8697 end if;
8698 end Check_Aspect_At_End_Of_Declarations;
8700 ----------------------------------
8701 -- Check_Aspect_At_Freeze_Point --
8702 ----------------------------------
8704 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8705 Ident : constant Node_Id := Identifier (ASN);
8706 -- Identifier (use Entity field to save expression)
8708 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8710 T : Entity_Id := Empty;
8711 -- Type required for preanalyze call
8713 begin
8714 -- On entry to this procedure, Entity (Ident) contains a copy of the
8715 -- original expression from the aspect, saved for this purpose.
8717 -- On exit from this procedure Entity (Ident) is unchanged, still
8718 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8719 -- of the expression, preanalyzed just after the freeze point.
8721 -- Make a copy of the expression to be preanalyzed
8723 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8725 -- Find type for preanalyze call
8727 case A_Id is
8729 -- No_Aspect should be impossible
8731 when No_Aspect =>
8732 raise Program_Error;
8734 -- Aspects taking an optional boolean argument
8736 when Boolean_Aspects |
8737 Library_Unit_Aspects =>
8739 T := Standard_Boolean;
8741 -- Aspects corresponding to attribute definition clauses
8743 when Aspect_Address =>
8744 T := RTE (RE_Address);
8746 when Aspect_Attach_Handler =>
8747 T := RTE (RE_Interrupt_ID);
8749 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8750 T := RTE (RE_Bit_Order);
8752 when Aspect_Convention =>
8753 return;
8755 when Aspect_CPU =>
8756 T := RTE (RE_CPU_Range);
8758 -- Default_Component_Value is resolved with the component type
8760 when Aspect_Default_Component_Value =>
8761 T := Component_Type (Entity (ASN));
8763 when Aspect_Default_Storage_Pool =>
8764 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8766 -- Default_Value is resolved with the type entity in question
8768 when Aspect_Default_Value =>
8769 T := Entity (ASN);
8771 when Aspect_Dispatching_Domain =>
8772 T := RTE (RE_Dispatching_Domain);
8774 when Aspect_External_Tag =>
8775 T := Standard_String;
8777 when Aspect_External_Name =>
8778 T := Standard_String;
8780 when Aspect_Link_Name =>
8781 T := Standard_String;
8783 when Aspect_Priority | Aspect_Interrupt_Priority =>
8784 T := Standard_Integer;
8786 when Aspect_Relative_Deadline =>
8787 T := RTE (RE_Time_Span);
8789 when Aspect_Small =>
8790 T := Universal_Real;
8792 -- For a simple storage pool, we have to retrieve the type of the
8793 -- pool object associated with the aspect's corresponding attribute
8794 -- definition clause.
8796 when Aspect_Simple_Storage_Pool =>
8797 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8799 when Aspect_Storage_Pool =>
8800 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8802 when Aspect_Alignment |
8803 Aspect_Component_Size |
8804 Aspect_Machine_Radix |
8805 Aspect_Object_Size |
8806 Aspect_Size |
8807 Aspect_Storage_Size |
8808 Aspect_Stream_Size |
8809 Aspect_Value_Size =>
8810 T := Any_Integer;
8812 when Aspect_Linker_Section =>
8813 T := Standard_String;
8815 when Aspect_Synchronization =>
8816 return;
8818 -- Special case, the expression of these aspects is just an entity
8819 -- that does not need any resolution, so just analyze.
8821 when Aspect_Input |
8822 Aspect_Output |
8823 Aspect_Read |
8824 Aspect_Suppress |
8825 Aspect_Unsuppress |
8826 Aspect_Warnings |
8827 Aspect_Write =>
8828 Analyze (Expression (ASN));
8829 return;
8831 -- Same for Iterator aspects, where the expression is a function
8832 -- name. Legality rules are checked separately.
8834 when Aspect_Constant_Indexing |
8835 Aspect_Default_Iterator |
8836 Aspect_Iterator_Element |
8837 Aspect_Variable_Indexing =>
8838 Analyze (Expression (ASN));
8839 return;
8841 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8843 when Aspect_Iterable =>
8844 T := Entity (ASN);
8846 declare
8847 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8848 Assoc : Node_Id;
8849 Expr : Node_Id;
8851 begin
8852 if Cursor = Any_Type then
8853 return;
8854 end if;
8856 Assoc := First (Component_Associations (Expression (ASN)));
8857 while Present (Assoc) loop
8858 Expr := Expression (Assoc);
8859 Analyze (Expr);
8861 if not Error_Posted (Expr) then
8862 Resolve_Iterable_Operation
8863 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8864 end if;
8866 Next (Assoc);
8867 end loop;
8868 end;
8870 return;
8872 -- Invariant/Predicate take boolean expressions
8874 when Aspect_Dynamic_Predicate |
8875 Aspect_Invariant |
8876 Aspect_Predicate |
8877 Aspect_Static_Predicate |
8878 Aspect_Type_Invariant =>
8879 T := Standard_Boolean;
8881 -- Here is the list of aspects that don't require delay analysis
8883 when Aspect_Abstract_State |
8884 Aspect_Annotate |
8885 Aspect_Contract_Cases |
8886 Aspect_Default_Initial_Condition |
8887 Aspect_Depends |
8888 Aspect_Dimension |
8889 Aspect_Dimension_System |
8890 Aspect_Extensions_Visible |
8891 Aspect_Ghost |
8892 Aspect_Global |
8893 Aspect_Implicit_Dereference |
8894 Aspect_Initial_Condition |
8895 Aspect_Initializes |
8896 Aspect_Obsolescent |
8897 Aspect_Part_Of |
8898 Aspect_Post |
8899 Aspect_Postcondition |
8900 Aspect_Pre |
8901 Aspect_Precondition |
8902 Aspect_Refined_Depends |
8903 Aspect_Refined_Global |
8904 Aspect_Refined_Post |
8905 Aspect_Refined_State |
8906 Aspect_SPARK_Mode |
8907 Aspect_Test_Case =>
8908 raise Program_Error;
8910 end case;
8912 -- Do the preanalyze call
8914 Preanalyze_Spec_Expression (Expression (ASN), T);
8915 end Check_Aspect_At_Freeze_Point;
8917 -----------------------------------
8918 -- Check_Constant_Address_Clause --
8919 -----------------------------------
8921 procedure Check_Constant_Address_Clause
8922 (Expr : Node_Id;
8923 U_Ent : Entity_Id)
8925 procedure Check_At_Constant_Address (Nod : Node_Id);
8926 -- Checks that the given node N represents a name whose 'Address is
8927 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8928 -- address value is the same at the point of declaration of U_Ent and at
8929 -- the time of elaboration of the address clause.
8931 procedure Check_Expr_Constants (Nod : Node_Id);
8932 -- Checks that Nod meets the requirements for a constant address clause
8933 -- in the sense of the enclosing procedure.
8935 procedure Check_List_Constants (Lst : List_Id);
8936 -- Check that all elements of list Lst meet the requirements for a
8937 -- constant address clause in the sense of the enclosing procedure.
8939 -------------------------------
8940 -- Check_At_Constant_Address --
8941 -------------------------------
8943 procedure Check_At_Constant_Address (Nod : Node_Id) is
8944 begin
8945 if Is_Entity_Name (Nod) then
8946 if Present (Address_Clause (Entity ((Nod)))) then
8947 Error_Msg_NE
8948 ("invalid address clause for initialized object &!",
8949 Nod, U_Ent);
8950 Error_Msg_NE
8951 ("address for& cannot" &
8952 " depend on another address clause! (RM 13.1(22))!",
8953 Nod, U_Ent);
8955 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
8956 and then Sloc (U_Ent) < Sloc (Entity (Nod))
8957 then
8958 Error_Msg_NE
8959 ("invalid address clause for initialized object &!",
8960 Nod, U_Ent);
8961 Error_Msg_Node_2 := U_Ent;
8962 Error_Msg_NE
8963 ("\& must be defined before & (RM 13.1(22))!",
8964 Nod, Entity (Nod));
8965 end if;
8967 elsif Nkind (Nod) = N_Selected_Component then
8968 declare
8969 T : constant Entity_Id := Etype (Prefix (Nod));
8971 begin
8972 if (Is_Record_Type (T)
8973 and then Has_Discriminants (T))
8974 or else
8975 (Is_Access_Type (T)
8976 and then Is_Record_Type (Designated_Type (T))
8977 and then Has_Discriminants (Designated_Type (T)))
8978 then
8979 Error_Msg_NE
8980 ("invalid address clause for initialized object &!",
8981 Nod, U_Ent);
8982 Error_Msg_N
8983 ("\address cannot depend on component" &
8984 " of discriminated record (RM 13.1(22))!",
8985 Nod);
8986 else
8987 Check_At_Constant_Address (Prefix (Nod));
8988 end if;
8989 end;
8991 elsif Nkind (Nod) = N_Indexed_Component then
8992 Check_At_Constant_Address (Prefix (Nod));
8993 Check_List_Constants (Expressions (Nod));
8995 else
8996 Check_Expr_Constants (Nod);
8997 end if;
8998 end Check_At_Constant_Address;
9000 --------------------------
9001 -- Check_Expr_Constants --
9002 --------------------------
9004 procedure Check_Expr_Constants (Nod : Node_Id) is
9005 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9006 Ent : Entity_Id := Empty;
9008 begin
9009 if Nkind (Nod) in N_Has_Etype
9010 and then Etype (Nod) = Any_Type
9011 then
9012 return;
9013 end if;
9015 case Nkind (Nod) is
9016 when N_Empty | N_Error =>
9017 return;
9019 when N_Identifier | N_Expanded_Name =>
9020 Ent := Entity (Nod);
9022 -- We need to look at the original node if it is different
9023 -- from the node, since we may have rewritten things and
9024 -- substituted an identifier representing the rewrite.
9026 if Original_Node (Nod) /= Nod then
9027 Check_Expr_Constants (Original_Node (Nod));
9029 -- If the node is an object declaration without initial
9030 -- value, some code has been expanded, and the expression
9031 -- is not constant, even if the constituents might be
9032 -- acceptable, as in A'Address + offset.
9034 if Ekind (Ent) = E_Variable
9035 and then
9036 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9037 and then
9038 No (Expression (Declaration_Node (Ent)))
9039 then
9040 Error_Msg_NE
9041 ("invalid address clause for initialized object &!",
9042 Nod, U_Ent);
9044 -- If entity is constant, it may be the result of expanding
9045 -- a check. We must verify that its declaration appears
9046 -- before the object in question, else we also reject the
9047 -- address clause.
9049 elsif Ekind (Ent) = E_Constant
9050 and then In_Same_Source_Unit (Ent, U_Ent)
9051 and then Sloc (Ent) > Loc_U_Ent
9052 then
9053 Error_Msg_NE
9054 ("invalid address clause for initialized object &!",
9055 Nod, U_Ent);
9056 end if;
9058 return;
9059 end if;
9061 -- Otherwise look at the identifier and see if it is OK
9063 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9064 or else Is_Type (Ent)
9065 then
9066 return;
9068 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9070 -- This is the case where we must have Ent defined before
9071 -- U_Ent. Clearly if they are in different units this
9072 -- requirement is met since the unit containing Ent is
9073 -- already processed.
9075 if not In_Same_Source_Unit (Ent, U_Ent) then
9076 return;
9078 -- Otherwise location of Ent must be before the location
9079 -- of U_Ent, that's what prior defined means.
9081 elsif Sloc (Ent) < Loc_U_Ent then
9082 return;
9084 else
9085 Error_Msg_NE
9086 ("invalid address clause for initialized object &!",
9087 Nod, U_Ent);
9088 Error_Msg_Node_2 := U_Ent;
9089 Error_Msg_NE
9090 ("\& must be defined before & (RM 13.1(22))!",
9091 Nod, Ent);
9092 end if;
9094 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9095 Check_Expr_Constants (Original_Node (Nod));
9097 else
9098 Error_Msg_NE
9099 ("invalid address clause for initialized object &!",
9100 Nod, U_Ent);
9102 if Comes_From_Source (Ent) then
9103 Error_Msg_NE
9104 ("\reference to variable& not allowed"
9105 & " (RM 13.1(22))!", Nod, Ent);
9106 else
9107 Error_Msg_N
9108 ("non-static expression not allowed"
9109 & " (RM 13.1(22))!", Nod);
9110 end if;
9111 end if;
9113 when N_Integer_Literal =>
9115 -- If this is a rewritten unchecked conversion, in a system
9116 -- where Address is an integer type, always use the base type
9117 -- for a literal value. This is user-friendly and prevents
9118 -- order-of-elaboration issues with instances of unchecked
9119 -- conversion.
9121 if Nkind (Original_Node (Nod)) = N_Function_Call then
9122 Set_Etype (Nod, Base_Type (Etype (Nod)));
9123 end if;
9125 when N_Real_Literal |
9126 N_String_Literal |
9127 N_Character_Literal =>
9128 return;
9130 when N_Range =>
9131 Check_Expr_Constants (Low_Bound (Nod));
9132 Check_Expr_Constants (High_Bound (Nod));
9134 when N_Explicit_Dereference =>
9135 Check_Expr_Constants (Prefix (Nod));
9137 when N_Indexed_Component =>
9138 Check_Expr_Constants (Prefix (Nod));
9139 Check_List_Constants (Expressions (Nod));
9141 when N_Slice =>
9142 Check_Expr_Constants (Prefix (Nod));
9143 Check_Expr_Constants (Discrete_Range (Nod));
9145 when N_Selected_Component =>
9146 Check_Expr_Constants (Prefix (Nod));
9148 when N_Attribute_Reference =>
9149 if Nam_In (Attribute_Name (Nod), Name_Address,
9150 Name_Access,
9151 Name_Unchecked_Access,
9152 Name_Unrestricted_Access)
9153 then
9154 Check_At_Constant_Address (Prefix (Nod));
9156 else
9157 Check_Expr_Constants (Prefix (Nod));
9158 Check_List_Constants (Expressions (Nod));
9159 end if;
9161 when N_Aggregate =>
9162 Check_List_Constants (Component_Associations (Nod));
9163 Check_List_Constants (Expressions (Nod));
9165 when N_Component_Association =>
9166 Check_Expr_Constants (Expression (Nod));
9168 when N_Extension_Aggregate =>
9169 Check_Expr_Constants (Ancestor_Part (Nod));
9170 Check_List_Constants (Component_Associations (Nod));
9171 Check_List_Constants (Expressions (Nod));
9173 when N_Null =>
9174 return;
9176 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9177 Check_Expr_Constants (Left_Opnd (Nod));
9178 Check_Expr_Constants (Right_Opnd (Nod));
9180 when N_Unary_Op =>
9181 Check_Expr_Constants (Right_Opnd (Nod));
9183 when N_Type_Conversion |
9184 N_Qualified_Expression |
9185 N_Allocator |
9186 N_Unchecked_Type_Conversion =>
9187 Check_Expr_Constants (Expression (Nod));
9189 when N_Function_Call =>
9190 if not Is_Pure (Entity (Name (Nod))) then
9191 Error_Msg_NE
9192 ("invalid address clause for initialized object &!",
9193 Nod, U_Ent);
9195 Error_Msg_NE
9196 ("\function & is not pure (RM 13.1(22))!",
9197 Nod, Entity (Name (Nod)));
9199 else
9200 Check_List_Constants (Parameter_Associations (Nod));
9201 end if;
9203 when N_Parameter_Association =>
9204 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9206 when others =>
9207 Error_Msg_NE
9208 ("invalid address clause for initialized object &!",
9209 Nod, U_Ent);
9210 Error_Msg_NE
9211 ("\must be constant defined before& (RM 13.1(22))!",
9212 Nod, U_Ent);
9213 end case;
9214 end Check_Expr_Constants;
9216 --------------------------
9217 -- Check_List_Constants --
9218 --------------------------
9220 procedure Check_List_Constants (Lst : List_Id) is
9221 Nod1 : Node_Id;
9223 begin
9224 if Present (Lst) then
9225 Nod1 := First (Lst);
9226 while Present (Nod1) loop
9227 Check_Expr_Constants (Nod1);
9228 Next (Nod1);
9229 end loop;
9230 end if;
9231 end Check_List_Constants;
9233 -- Start of processing for Check_Constant_Address_Clause
9235 begin
9236 -- If rep_clauses are to be ignored, no need for legality checks. In
9237 -- particular, no need to pester user about rep clauses that violate the
9238 -- rule on constant addresses, given that these clauses will be removed
9239 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9240 -- we want to relax these checks.
9242 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9243 Check_Expr_Constants (Expr);
9244 end if;
9245 end Check_Constant_Address_Clause;
9247 ---------------------------
9248 -- Check_Pool_Size_Clash --
9249 ---------------------------
9251 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9252 Post : Node_Id;
9254 begin
9255 -- We need to find out which one came first. Note that in the case of
9256 -- aspects mixed with pragmas there are cases where the processing order
9257 -- is reversed, which is why we do the check here.
9259 if Sloc (SP) < Sloc (SS) then
9260 Error_Msg_Sloc := Sloc (SP);
9261 Post := SS;
9262 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9264 else
9265 Error_Msg_Sloc := Sloc (SS);
9266 Post := SP;
9267 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9268 end if;
9270 Error_Msg_N
9271 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9272 end Check_Pool_Size_Clash;
9274 ----------------------------------------
9275 -- Check_Record_Representation_Clause --
9276 ----------------------------------------
9278 procedure Check_Record_Representation_Clause (N : Node_Id) is
9279 Loc : constant Source_Ptr := Sloc (N);
9280 Ident : constant Node_Id := Identifier (N);
9281 Rectype : Entity_Id;
9282 Fent : Entity_Id;
9283 CC : Node_Id;
9284 Fbit : Uint;
9285 Lbit : Uint;
9286 Hbit : Uint := Uint_0;
9287 Comp : Entity_Id;
9288 Pcomp : Entity_Id;
9290 Max_Bit_So_Far : Uint;
9291 -- Records the maximum bit position so far. If all field positions
9292 -- are monotonically increasing, then we can skip the circuit for
9293 -- checking for overlap, since no overlap is possible.
9295 Tagged_Parent : Entity_Id := Empty;
9296 -- This is set in the case of a derived tagged type for which we have
9297 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9298 -- positioned by record representation clauses). In this case we must
9299 -- check for overlap between components of this tagged type, and the
9300 -- components of its parent. Tagged_Parent will point to this parent
9301 -- type. For all other cases Tagged_Parent is left set to Empty.
9303 Parent_Last_Bit : Uint;
9304 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9305 -- last bit position for any field in the parent type. We only need to
9306 -- check overlap for fields starting below this point.
9308 Overlap_Check_Required : Boolean;
9309 -- Used to keep track of whether or not an overlap check is required
9311 Overlap_Detected : Boolean := False;
9312 -- Set True if an overlap is detected
9314 Ccount : Natural := 0;
9315 -- Number of component clauses in record rep clause
9317 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9318 -- Given two entities for record components or discriminants, checks
9319 -- if they have overlapping component clauses and issues errors if so.
9321 procedure Find_Component;
9322 -- Finds component entity corresponding to current component clause (in
9323 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9324 -- start/stop bits for the field. If there is no matching component or
9325 -- if the matching component does not have a component clause, then
9326 -- that's an error and Comp is set to Empty, but no error message is
9327 -- issued, since the message was already given. Comp is also set to
9328 -- Empty if the current "component clause" is in fact a pragma.
9330 -----------------------------
9331 -- Check_Component_Overlap --
9332 -----------------------------
9334 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9335 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9336 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9338 begin
9339 if Present (CC1) and then Present (CC2) then
9341 -- Exclude odd case where we have two tag components in the same
9342 -- record, both at location zero. This seems a bit strange, but
9343 -- it seems to happen in some circumstances, perhaps on an error.
9345 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9346 return;
9347 end if;
9349 -- Here we check if the two fields overlap
9351 declare
9352 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9353 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9354 E1 : constant Uint := S1 + Esize (C1_Ent);
9355 E2 : constant Uint := S2 + Esize (C2_Ent);
9357 begin
9358 if E2 <= S1 or else E1 <= S2 then
9359 null;
9360 else
9361 Error_Msg_Node_2 := Component_Name (CC2);
9362 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9363 Error_Msg_Node_1 := Component_Name (CC1);
9364 Error_Msg_N
9365 ("component& overlaps & #", Component_Name (CC1));
9366 Overlap_Detected := True;
9367 end if;
9368 end;
9369 end if;
9370 end Check_Component_Overlap;
9372 --------------------
9373 -- Find_Component --
9374 --------------------
9376 procedure Find_Component is
9378 procedure Search_Component (R : Entity_Id);
9379 -- Search components of R for a match. If found, Comp is set
9381 ----------------------
9382 -- Search_Component --
9383 ----------------------
9385 procedure Search_Component (R : Entity_Id) is
9386 begin
9387 Comp := First_Component_Or_Discriminant (R);
9388 while Present (Comp) loop
9390 -- Ignore error of attribute name for component name (we
9391 -- already gave an error message for this, so no need to
9392 -- complain here)
9394 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9395 null;
9396 else
9397 exit when Chars (Comp) = Chars (Component_Name (CC));
9398 end if;
9400 Next_Component_Or_Discriminant (Comp);
9401 end loop;
9402 end Search_Component;
9404 -- Start of processing for Find_Component
9406 begin
9407 -- Return with Comp set to Empty if we have a pragma
9409 if Nkind (CC) = N_Pragma then
9410 Comp := Empty;
9411 return;
9412 end if;
9414 -- Search current record for matching component
9416 Search_Component (Rectype);
9418 -- If not found, maybe component of base type discriminant that is
9419 -- absent from statically constrained first subtype.
9421 if No (Comp) then
9422 Search_Component (Base_Type (Rectype));
9423 end if;
9425 -- If no component, or the component does not reference the component
9426 -- clause in question, then there was some previous error for which
9427 -- we already gave a message, so just return with Comp Empty.
9429 if No (Comp) or else Component_Clause (Comp) /= CC then
9430 Check_Error_Detected;
9431 Comp := Empty;
9433 -- Normal case where we have a component clause
9435 else
9436 Fbit := Component_Bit_Offset (Comp);
9437 Lbit := Fbit + Esize (Comp) - 1;
9438 end if;
9439 end Find_Component;
9441 -- Start of processing for Check_Record_Representation_Clause
9443 begin
9444 Find_Type (Ident);
9445 Rectype := Entity (Ident);
9447 if Rectype = Any_Type then
9448 return;
9449 else
9450 Rectype := Underlying_Type (Rectype);
9451 end if;
9453 -- See if we have a fully repped derived tagged type
9455 declare
9456 PS : constant Entity_Id := Parent_Subtype (Rectype);
9458 begin
9459 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9460 Tagged_Parent := PS;
9462 -- Find maximum bit of any component of the parent type
9464 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9465 Pcomp := First_Entity (Tagged_Parent);
9466 while Present (Pcomp) loop
9467 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9468 if Component_Bit_Offset (Pcomp) /= No_Uint
9469 and then Known_Static_Esize (Pcomp)
9470 then
9471 Parent_Last_Bit :=
9472 UI_Max
9473 (Parent_Last_Bit,
9474 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9475 end if;
9477 Next_Entity (Pcomp);
9478 end if;
9479 end loop;
9480 end if;
9481 end;
9483 -- All done if no component clauses
9485 CC := First (Component_Clauses (N));
9487 if No (CC) then
9488 return;
9489 end if;
9491 -- If a tag is present, then create a component clause that places it
9492 -- at the start of the record (otherwise gigi may place it after other
9493 -- fields that have rep clauses).
9495 Fent := First_Entity (Rectype);
9497 if Nkind (Fent) = N_Defining_Identifier
9498 and then Chars (Fent) = Name_uTag
9499 then
9500 Set_Component_Bit_Offset (Fent, Uint_0);
9501 Set_Normalized_Position (Fent, Uint_0);
9502 Set_Normalized_First_Bit (Fent, Uint_0);
9503 Set_Normalized_Position_Max (Fent, Uint_0);
9504 Init_Esize (Fent, System_Address_Size);
9506 Set_Component_Clause (Fent,
9507 Make_Component_Clause (Loc,
9508 Component_Name => Make_Identifier (Loc, Name_uTag),
9510 Position => Make_Integer_Literal (Loc, Uint_0),
9511 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9512 Last_Bit =>
9513 Make_Integer_Literal (Loc,
9514 UI_From_Int (System_Address_Size))));
9516 Ccount := Ccount + 1;
9517 end if;
9519 Max_Bit_So_Far := Uint_Minus_1;
9520 Overlap_Check_Required := False;
9522 -- Process the component clauses
9524 while Present (CC) loop
9525 Find_Component;
9527 if Present (Comp) then
9528 Ccount := Ccount + 1;
9530 -- We need a full overlap check if record positions non-monotonic
9532 if Fbit <= Max_Bit_So_Far then
9533 Overlap_Check_Required := True;
9534 end if;
9536 Max_Bit_So_Far := Lbit;
9538 -- Check bit position out of range of specified size
9540 if Has_Size_Clause (Rectype)
9541 and then RM_Size (Rectype) <= Lbit
9542 then
9543 Error_Msg_N
9544 ("bit number out of range of specified size",
9545 Last_Bit (CC));
9547 -- Check for overlap with tag component
9549 else
9550 if Is_Tagged_Type (Rectype)
9551 and then Fbit < System_Address_Size
9552 then
9553 Error_Msg_NE
9554 ("component overlaps tag field of&",
9555 Component_Name (CC), Rectype);
9556 Overlap_Detected := True;
9557 end if;
9559 if Hbit < Lbit then
9560 Hbit := Lbit;
9561 end if;
9562 end if;
9564 -- Check parent overlap if component might overlap parent field
9566 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9567 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9568 while Present (Pcomp) loop
9569 if not Is_Tag (Pcomp)
9570 and then Chars (Pcomp) /= Name_uParent
9571 then
9572 Check_Component_Overlap (Comp, Pcomp);
9573 end if;
9575 Next_Component_Or_Discriminant (Pcomp);
9576 end loop;
9577 end if;
9578 end if;
9580 Next (CC);
9581 end loop;
9583 -- Now that we have processed all the component clauses, check for
9584 -- overlap. We have to leave this till last, since the components can
9585 -- appear in any arbitrary order in the representation clause.
9587 -- We do not need this check if all specified ranges were monotonic,
9588 -- as recorded by Overlap_Check_Required being False at this stage.
9590 -- This first section checks if there are any overlapping entries at
9591 -- all. It does this by sorting all entries and then seeing if there are
9592 -- any overlaps. If there are none, then that is decisive, but if there
9593 -- are overlaps, they may still be OK (they may result from fields in
9594 -- different variants).
9596 if Overlap_Check_Required then
9597 Overlap_Check1 : declare
9599 OC_Fbit : array (0 .. Ccount) of Uint;
9600 -- First-bit values for component clauses, the value is the offset
9601 -- of the first bit of the field from start of record. The zero
9602 -- entry is for use in sorting.
9604 OC_Lbit : array (0 .. Ccount) of Uint;
9605 -- Last-bit values for component clauses, the value is the offset
9606 -- of the last bit of the field from start of record. The zero
9607 -- entry is for use in sorting.
9609 OC_Count : Natural := 0;
9610 -- Count of entries in OC_Fbit and OC_Lbit
9612 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9613 -- Compare routine for Sort
9615 procedure OC_Move (From : Natural; To : Natural);
9616 -- Move routine for Sort
9618 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9620 -----------
9621 -- OC_Lt --
9622 -----------
9624 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9625 begin
9626 return OC_Fbit (Op1) < OC_Fbit (Op2);
9627 end OC_Lt;
9629 -------------
9630 -- OC_Move --
9631 -------------
9633 procedure OC_Move (From : Natural; To : Natural) is
9634 begin
9635 OC_Fbit (To) := OC_Fbit (From);
9636 OC_Lbit (To) := OC_Lbit (From);
9637 end OC_Move;
9639 -- Start of processing for Overlap_Check
9641 begin
9642 CC := First (Component_Clauses (N));
9643 while Present (CC) loop
9645 -- Exclude component clause already marked in error
9647 if not Error_Posted (CC) then
9648 Find_Component;
9650 if Present (Comp) then
9651 OC_Count := OC_Count + 1;
9652 OC_Fbit (OC_Count) := Fbit;
9653 OC_Lbit (OC_Count) := Lbit;
9654 end if;
9655 end if;
9657 Next (CC);
9658 end loop;
9660 Sorting.Sort (OC_Count);
9662 Overlap_Check_Required := False;
9663 for J in 1 .. OC_Count - 1 loop
9664 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9665 Overlap_Check_Required := True;
9666 exit;
9667 end if;
9668 end loop;
9669 end Overlap_Check1;
9670 end if;
9672 -- If Overlap_Check_Required is still True, then we have to do the full
9673 -- scale overlap check, since we have at least two fields that do
9674 -- overlap, and we need to know if that is OK since they are in
9675 -- different variant, or whether we have a definite problem.
9677 if Overlap_Check_Required then
9678 Overlap_Check2 : declare
9679 C1_Ent, C2_Ent : Entity_Id;
9680 -- Entities of components being checked for overlap
9682 Clist : Node_Id;
9683 -- Component_List node whose Component_Items are being checked
9685 Citem : Node_Id;
9686 -- Component declaration for component being checked
9688 begin
9689 C1_Ent := First_Entity (Base_Type (Rectype));
9691 -- Loop through all components in record. For each component check
9692 -- for overlap with any of the preceding elements on the component
9693 -- list containing the component and also, if the component is in
9694 -- a variant, check against components outside the case structure.
9695 -- This latter test is repeated recursively up the variant tree.
9697 Main_Component_Loop : while Present (C1_Ent) loop
9698 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9699 goto Continue_Main_Component_Loop;
9700 end if;
9702 -- Skip overlap check if entity has no declaration node. This
9703 -- happens with discriminants in constrained derived types.
9704 -- Possibly we are missing some checks as a result, but that
9705 -- does not seem terribly serious.
9707 if No (Declaration_Node (C1_Ent)) then
9708 goto Continue_Main_Component_Loop;
9709 end if;
9711 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9713 -- Loop through component lists that need checking. Check the
9714 -- current component list and all lists in variants above us.
9716 Component_List_Loop : loop
9718 -- If derived type definition, go to full declaration
9719 -- If at outer level, check discriminants if there are any.
9721 if Nkind (Clist) = N_Derived_Type_Definition then
9722 Clist := Parent (Clist);
9723 end if;
9725 -- Outer level of record definition, check discriminants
9727 if Nkind_In (Clist, N_Full_Type_Declaration,
9728 N_Private_Type_Declaration)
9729 then
9730 if Has_Discriminants (Defining_Identifier (Clist)) then
9731 C2_Ent :=
9732 First_Discriminant (Defining_Identifier (Clist));
9733 while Present (C2_Ent) loop
9734 exit when C1_Ent = C2_Ent;
9735 Check_Component_Overlap (C1_Ent, C2_Ent);
9736 Next_Discriminant (C2_Ent);
9737 end loop;
9738 end if;
9740 -- Record extension case
9742 elsif Nkind (Clist) = N_Derived_Type_Definition then
9743 Clist := Empty;
9745 -- Otherwise check one component list
9747 else
9748 Citem := First (Component_Items (Clist));
9749 while Present (Citem) loop
9750 if Nkind (Citem) = N_Component_Declaration then
9751 C2_Ent := Defining_Identifier (Citem);
9752 exit when C1_Ent = C2_Ent;
9753 Check_Component_Overlap (C1_Ent, C2_Ent);
9754 end if;
9756 Next (Citem);
9757 end loop;
9758 end if;
9760 -- Check for variants above us (the parent of the Clist can
9761 -- be a variant, in which case its parent is a variant part,
9762 -- and the parent of the variant part is a component list
9763 -- whose components must all be checked against the current
9764 -- component for overlap).
9766 if Nkind (Parent (Clist)) = N_Variant then
9767 Clist := Parent (Parent (Parent (Clist)));
9769 -- Check for possible discriminant part in record, this
9770 -- is treated essentially as another level in the
9771 -- recursion. For this case the parent of the component
9772 -- list is the record definition, and its parent is the
9773 -- full type declaration containing the discriminant
9774 -- specifications.
9776 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9777 Clist := Parent (Parent ((Clist)));
9779 -- If neither of these two cases, we are at the top of
9780 -- the tree.
9782 else
9783 exit Component_List_Loop;
9784 end if;
9785 end loop Component_List_Loop;
9787 <<Continue_Main_Component_Loop>>
9788 Next_Entity (C1_Ent);
9790 end loop Main_Component_Loop;
9791 end Overlap_Check2;
9792 end if;
9794 -- The following circuit deals with warning on record holes (gaps). We
9795 -- skip this check if overlap was detected, since it makes sense for the
9796 -- programmer to fix this illegality before worrying about warnings.
9798 if not Overlap_Detected and Warn_On_Record_Holes then
9799 Record_Hole_Check : declare
9800 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9801 -- Full declaration of record type
9803 procedure Check_Component_List
9804 (CL : Node_Id;
9805 Sbit : Uint;
9806 DS : List_Id);
9807 -- Check component list CL for holes. The starting bit should be
9808 -- Sbit. which is zero for the main record component list and set
9809 -- appropriately for recursive calls for variants. DS is set to
9810 -- a list of discriminant specifications to be included in the
9811 -- consideration of components. It is No_List if none to consider.
9813 --------------------------
9814 -- Check_Component_List --
9815 --------------------------
9817 procedure Check_Component_List
9818 (CL : Node_Id;
9819 Sbit : Uint;
9820 DS : List_Id)
9822 Compl : Integer;
9824 begin
9825 Compl := Integer (List_Length (Component_Items (CL)));
9827 if DS /= No_List then
9828 Compl := Compl + Integer (List_Length (DS));
9829 end if;
9831 declare
9832 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9833 -- Gather components (zero entry is for sort routine)
9835 Ncomps : Natural := 0;
9836 -- Number of entries stored in Comps (starting at Comps (1))
9838 Citem : Node_Id;
9839 -- One component item or discriminant specification
9841 Nbit : Uint;
9842 -- Starting bit for next component
9844 CEnt : Entity_Id;
9845 -- Component entity
9847 Variant : Node_Id;
9848 -- One variant
9850 function Lt (Op1, Op2 : Natural) return Boolean;
9851 -- Compare routine for Sort
9853 procedure Move (From : Natural; To : Natural);
9854 -- Move routine for Sort
9856 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9858 --------
9859 -- Lt --
9860 --------
9862 function Lt (Op1, Op2 : Natural) return Boolean is
9863 begin
9864 return Component_Bit_Offset (Comps (Op1))
9866 Component_Bit_Offset (Comps (Op2));
9867 end Lt;
9869 ----------
9870 -- Move --
9871 ----------
9873 procedure Move (From : Natural; To : Natural) is
9874 begin
9875 Comps (To) := Comps (From);
9876 end Move;
9878 begin
9879 -- Gather discriminants into Comp
9881 if DS /= No_List then
9882 Citem := First (DS);
9883 while Present (Citem) loop
9884 if Nkind (Citem) = N_Discriminant_Specification then
9885 declare
9886 Ent : constant Entity_Id :=
9887 Defining_Identifier (Citem);
9888 begin
9889 if Ekind (Ent) = E_Discriminant then
9890 Ncomps := Ncomps + 1;
9891 Comps (Ncomps) := Ent;
9892 end if;
9893 end;
9894 end if;
9896 Next (Citem);
9897 end loop;
9898 end if;
9900 -- Gather component entities into Comp
9902 Citem := First (Component_Items (CL));
9903 while Present (Citem) loop
9904 if Nkind (Citem) = N_Component_Declaration then
9905 Ncomps := Ncomps + 1;
9906 Comps (Ncomps) := Defining_Identifier (Citem);
9907 end if;
9909 Next (Citem);
9910 end loop;
9912 -- Now sort the component entities based on the first bit.
9913 -- Note we already know there are no overlapping components.
9915 Sorting.Sort (Ncomps);
9917 -- Loop through entries checking for holes
9919 Nbit := Sbit;
9920 for J in 1 .. Ncomps loop
9921 CEnt := Comps (J);
9922 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9924 if Error_Msg_Uint_1 > 0 then
9925 Error_Msg_NE
9926 ("?H?^-bit gap before component&",
9927 Component_Name (Component_Clause (CEnt)), CEnt);
9928 end if;
9930 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9931 end loop;
9933 -- Process variant parts recursively if present
9935 if Present (Variant_Part (CL)) then
9936 Variant := First (Variants (Variant_Part (CL)));
9937 while Present (Variant) loop
9938 Check_Component_List
9939 (Component_List (Variant), Nbit, No_List);
9940 Next (Variant);
9941 end loop;
9942 end if;
9943 end;
9944 end Check_Component_List;
9946 -- Start of processing for Record_Hole_Check
9948 begin
9949 declare
9950 Sbit : Uint;
9952 begin
9953 if Is_Tagged_Type (Rectype) then
9954 Sbit := UI_From_Int (System_Address_Size);
9955 else
9956 Sbit := Uint_0;
9957 end if;
9959 if Nkind (Decl) = N_Full_Type_Declaration
9960 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9961 then
9962 Check_Component_List
9963 (Component_List (Type_Definition (Decl)),
9964 Sbit,
9965 Discriminant_Specifications (Decl));
9966 end if;
9967 end;
9968 end Record_Hole_Check;
9969 end if;
9971 -- For records that have component clauses for all components, and whose
9972 -- size is less than or equal to 32, we need to know the size in the
9973 -- front end to activate possible packed array processing where the
9974 -- component type is a record.
9976 -- At this stage Hbit + 1 represents the first unused bit from all the
9977 -- component clauses processed, so if the component clauses are
9978 -- complete, then this is the length of the record.
9980 -- For records longer than System.Storage_Unit, and for those where not
9981 -- all components have component clauses, the back end determines the
9982 -- length (it may for example be appropriate to round up the size
9983 -- to some convenient boundary, based on alignment considerations, etc).
9985 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
9987 -- Nothing to do if at least one component has no component clause
9989 Comp := First_Component_Or_Discriminant (Rectype);
9990 while Present (Comp) loop
9991 exit when No (Component_Clause (Comp));
9992 Next_Component_Or_Discriminant (Comp);
9993 end loop;
9995 -- If we fall out of loop, all components have component clauses
9996 -- and so we can set the size to the maximum value.
9998 if No (Comp) then
9999 Set_RM_Size (Rectype, Hbit + 1);
10000 end if;
10001 end if;
10002 end Check_Record_Representation_Clause;
10004 ----------------
10005 -- Check_Size --
10006 ----------------
10008 procedure Check_Size
10009 (N : Node_Id;
10010 T : Entity_Id;
10011 Siz : Uint;
10012 Biased : out Boolean)
10014 UT : constant Entity_Id := Underlying_Type (T);
10015 M : Uint;
10017 begin
10018 Biased := False;
10020 -- Reject patently improper size values.
10022 if Is_Elementary_Type (T)
10023 and then Siz > UI_From_Int (Int'Last)
10024 then
10025 Error_Msg_N ("Size value too large for elementary type", N);
10027 if Nkind (Original_Node (N)) = N_Op_Expon then
10028 Error_Msg_N
10029 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10030 end if;
10031 end if;
10033 -- Dismiss generic types
10035 if Is_Generic_Type (T)
10036 or else
10037 Is_Generic_Type (UT)
10038 or else
10039 Is_Generic_Type (Root_Type (UT))
10040 then
10041 return;
10043 -- Guard against previous errors
10045 elsif No (UT) or else UT = Any_Type then
10046 Check_Error_Detected;
10047 return;
10049 -- Check case of bit packed array
10051 elsif Is_Array_Type (UT)
10052 and then Known_Static_Component_Size (UT)
10053 and then Is_Bit_Packed_Array (UT)
10054 then
10055 declare
10056 Asiz : Uint;
10057 Indx : Node_Id;
10058 Ityp : Entity_Id;
10060 begin
10061 Asiz := Component_Size (UT);
10062 Indx := First_Index (UT);
10063 loop
10064 Ityp := Etype (Indx);
10066 -- If non-static bound, then we are not in the business of
10067 -- trying to check the length, and indeed an error will be
10068 -- issued elsewhere, since sizes of non-static array types
10069 -- cannot be set implicitly or explicitly.
10071 if not Is_OK_Static_Subtype (Ityp) then
10072 return;
10073 end if;
10075 -- Otherwise accumulate next dimension
10077 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10078 Expr_Value (Type_Low_Bound (Ityp)) +
10079 Uint_1);
10081 Next_Index (Indx);
10082 exit when No (Indx);
10083 end loop;
10085 if Asiz <= Siz then
10086 return;
10088 else
10089 Error_Msg_Uint_1 := Asiz;
10090 Error_Msg_NE
10091 ("size for& too small, minimum allowed is ^", N, T);
10092 Set_Esize (T, Asiz);
10093 Set_RM_Size (T, Asiz);
10094 end if;
10095 end;
10097 -- All other composite types are ignored
10099 elsif Is_Composite_Type (UT) then
10100 return;
10102 -- For fixed-point types, don't check minimum if type is not frozen,
10103 -- since we don't know all the characteristics of the type that can
10104 -- affect the size (e.g. a specified small) till freeze time.
10106 elsif Is_Fixed_Point_Type (UT)
10107 and then not Is_Frozen (UT)
10108 then
10109 null;
10111 -- Cases for which a minimum check is required
10113 else
10114 -- Ignore if specified size is correct for the type
10116 if Known_Esize (UT) and then Siz = Esize (UT) then
10117 return;
10118 end if;
10120 -- Otherwise get minimum size
10122 M := UI_From_Int (Minimum_Size (UT));
10124 if Siz < M then
10126 -- Size is less than minimum size, but one possibility remains
10127 -- that we can manage with the new size if we bias the type.
10129 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10131 if Siz < M then
10132 Error_Msg_Uint_1 := M;
10133 Error_Msg_NE
10134 ("size for& too small, minimum allowed is ^", N, T);
10135 Set_Esize (T, M);
10136 Set_RM_Size (T, M);
10137 else
10138 Biased := True;
10139 end if;
10140 end if;
10141 end if;
10142 end Check_Size;
10144 --------------------------
10145 -- Freeze_Entity_Checks --
10146 --------------------------
10148 procedure Freeze_Entity_Checks (N : Node_Id) is
10149 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10150 -- Inspect the primitive operations of type Typ and hide all pairs of
10151 -- implicitly declared non-overridden non-fully conformant homographs
10152 -- (Ada RM 8.3 12.3/2).
10154 -------------------------------------
10155 -- Hide_Non_Overridden_Subprograms --
10156 -------------------------------------
10158 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10159 procedure Hide_Matching_Homographs
10160 (Subp_Id : Entity_Id;
10161 Start_Elmt : Elmt_Id);
10162 -- Inspect a list of primitive operations starting with Start_Elmt
10163 -- and find matching implicitly declared non-overridden non-fully
10164 -- conformant homographs of Subp_Id. If found, all matches along
10165 -- with Subp_Id are hidden from all visibility.
10167 function Is_Non_Overridden_Or_Null_Procedure
10168 (Subp_Id : Entity_Id) return Boolean;
10169 -- Determine whether subprogram Subp_Id is implicitly declared non-
10170 -- overridden subprogram or an implicitly declared null procedure.
10172 ------------------------------
10173 -- Hide_Matching_Homographs --
10174 ------------------------------
10176 procedure Hide_Matching_Homographs
10177 (Subp_Id : Entity_Id;
10178 Start_Elmt : Elmt_Id)
10180 Prim : Entity_Id;
10181 Prim_Elmt : Elmt_Id;
10183 begin
10184 Prim_Elmt := Start_Elmt;
10185 while Present (Prim_Elmt) loop
10186 Prim := Node (Prim_Elmt);
10188 -- The current primitive is implicitly declared non-overridden
10189 -- non-fully conformant homograph of Subp_Id. Both subprograms
10190 -- must be hidden from visibility.
10192 if Chars (Prim) = Chars (Subp_Id)
10193 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10194 and then not Fully_Conformant (Prim, Subp_Id)
10195 then
10196 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10197 Set_Is_Immediately_Visible (Prim, False);
10198 Set_Is_Potentially_Use_Visible (Prim, False);
10200 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10201 Set_Is_Immediately_Visible (Subp_Id, False);
10202 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10203 end if;
10205 Next_Elmt (Prim_Elmt);
10206 end loop;
10207 end Hide_Matching_Homographs;
10209 -----------------------------------------
10210 -- Is_Non_Overridden_Or_Null_Procedure --
10211 -----------------------------------------
10213 function Is_Non_Overridden_Or_Null_Procedure
10214 (Subp_Id : Entity_Id) return Boolean
10216 Alias_Id : Entity_Id;
10218 begin
10219 -- The subprogram is inherited (implicitly declared), it does not
10220 -- override and does not cover a primitive of an interface.
10222 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10223 and then Present (Alias (Subp_Id))
10224 and then No (Interface_Alias (Subp_Id))
10225 and then No (Overridden_Operation (Subp_Id))
10226 then
10227 Alias_Id := Alias (Subp_Id);
10229 if Requires_Overriding (Alias_Id) then
10230 return True;
10232 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10233 and then Null_Present (Parent (Alias_Id))
10234 then
10235 return True;
10236 end if;
10237 end if;
10239 return False;
10240 end Is_Non_Overridden_Or_Null_Procedure;
10242 -- Local variables
10244 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10245 Prim : Entity_Id;
10246 Prim_Elmt : Elmt_Id;
10248 -- Start of processing for Hide_Non_Overridden_Subprograms
10250 begin
10251 -- Inspect the list of primitives looking for non-overridden
10252 -- subprograms.
10254 if Present (Prim_Ops) then
10255 Prim_Elmt := First_Elmt (Prim_Ops);
10256 while Present (Prim_Elmt) loop
10257 Prim := Node (Prim_Elmt);
10258 Next_Elmt (Prim_Elmt);
10260 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10261 Hide_Matching_Homographs
10262 (Subp_Id => Prim,
10263 Start_Elmt => Prim_Elmt);
10264 end if;
10265 end loop;
10266 end if;
10267 end Hide_Non_Overridden_Subprograms;
10269 ---------------------
10270 -- Local variables --
10271 ---------------------
10273 E : constant Entity_Id := Entity (N);
10275 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10276 -- True in non-generic case. Some of the processing here is skipped
10277 -- for the generic case since it is not needed. Basically in the
10278 -- generic case, we only need to do stuff that might generate error
10279 -- messages or warnings.
10281 -- Start of processing for Freeze_Entity_Checks
10283 begin
10284 -- Remember that we are processing a freezing entity. Required to
10285 -- ensure correct decoration of internal entities associated with
10286 -- interfaces (see New_Overloaded_Entity).
10288 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10290 -- For tagged types covering interfaces add internal entities that link
10291 -- the primitives of the interfaces with the primitives that cover them.
10292 -- Note: These entities were originally generated only when generating
10293 -- code because their main purpose was to provide support to initialize
10294 -- the secondary dispatch tables. They are now generated also when
10295 -- compiling with no code generation to provide ASIS the relationship
10296 -- between interface primitives and tagged type primitives. They are
10297 -- also used to locate primitives covering interfaces when processing
10298 -- generics (see Derive_Subprograms).
10300 -- This is not needed in the generic case
10302 if Ada_Version >= Ada_2005
10303 and then Non_Generic_Case
10304 and then Ekind (E) = E_Record_Type
10305 and then Is_Tagged_Type (E)
10306 and then not Is_Interface (E)
10307 and then Has_Interfaces (E)
10308 then
10309 -- This would be a good common place to call the routine that checks
10310 -- overriding of interface primitives (and thus factorize calls to
10311 -- Check_Abstract_Overriding located at different contexts in the
10312 -- compiler). However, this is not possible because it causes
10313 -- spurious errors in case of late overriding.
10315 Add_Internal_Interface_Entities (E);
10316 end if;
10318 -- After all forms of overriding have been resolved, a tagged type may
10319 -- be left with a set of implicitly declared and possibly erroneous
10320 -- abstract subprograms, null procedures and subprograms that require
10321 -- overriding. If this set contains fully conformat homographs, then one
10322 -- is chosen arbitrarily (already done during resolution), otherwise all
10323 -- remaining non-fully conformant homographs are hidden from visibility
10324 -- (Ada RM 8.3 12.3/2).
10326 if Is_Tagged_Type (E) then
10327 Hide_Non_Overridden_Subprograms (E);
10328 end if;
10330 -- Check CPP types
10332 if Ekind (E) = E_Record_Type
10333 and then Is_CPP_Class (E)
10334 and then Is_Tagged_Type (E)
10335 and then Tagged_Type_Expansion
10336 then
10337 if CPP_Num_Prims (E) = 0 then
10339 -- If the CPP type has user defined components then it must import
10340 -- primitives from C++. This is required because if the C++ class
10341 -- has no primitives then the C++ compiler does not added the _tag
10342 -- component to the type.
10344 if First_Entity (E) /= Last_Entity (E) then
10345 Error_Msg_N
10346 ("'C'P'P type must import at least one primitive from C++??",
10348 end if;
10349 end if;
10351 -- Check that all its primitives are abstract or imported from C++.
10352 -- Check also availability of the C++ constructor.
10354 declare
10355 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10356 Elmt : Elmt_Id;
10357 Error_Reported : Boolean := False;
10358 Prim : Node_Id;
10360 begin
10361 Elmt := First_Elmt (Primitive_Operations (E));
10362 while Present (Elmt) loop
10363 Prim := Node (Elmt);
10365 if Comes_From_Source (Prim) then
10366 if Is_Abstract_Subprogram (Prim) then
10367 null;
10369 elsif not Is_Imported (Prim)
10370 or else Convention (Prim) /= Convention_CPP
10371 then
10372 Error_Msg_N
10373 ("primitives of 'C'P'P types must be imported from C++ "
10374 & "or abstract??", Prim);
10376 elsif not Has_Constructors
10377 and then not Error_Reported
10378 then
10379 Error_Msg_Name_1 := Chars (E);
10380 Error_Msg_N
10381 ("??'C'P'P constructor required for type %", Prim);
10382 Error_Reported := True;
10383 end if;
10384 end if;
10386 Next_Elmt (Elmt);
10387 end loop;
10388 end;
10389 end if;
10391 -- Check Ada derivation of CPP type
10393 if Expander_Active -- why? losing errors in -gnatc mode???
10394 and then Present (Etype (E)) -- defend against errors
10395 and then Tagged_Type_Expansion
10396 and then Ekind (E) = E_Record_Type
10397 and then Etype (E) /= E
10398 and then Is_CPP_Class (Etype (E))
10399 and then CPP_Num_Prims (Etype (E)) > 0
10400 and then not Is_CPP_Class (E)
10401 and then not Has_CPP_Constructors (Etype (E))
10402 then
10403 -- If the parent has C++ primitives but it has no constructor then
10404 -- check that all the primitives are overridden in this derivation;
10405 -- otherwise the constructor of the parent is needed to build the
10406 -- dispatch table.
10408 declare
10409 Elmt : Elmt_Id;
10410 Prim : Node_Id;
10412 begin
10413 Elmt := First_Elmt (Primitive_Operations (E));
10414 while Present (Elmt) loop
10415 Prim := Node (Elmt);
10417 if not Is_Abstract_Subprogram (Prim)
10418 and then No (Interface_Alias (Prim))
10419 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10420 then
10421 Error_Msg_Name_1 := Chars (Etype (E));
10422 Error_Msg_N
10423 ("'C'P'P constructor required for parent type %", E);
10424 exit;
10425 end if;
10427 Next_Elmt (Elmt);
10428 end loop;
10429 end;
10430 end if;
10432 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10434 -- If we have a type with predicates, build predicate function. This
10435 -- is not needed in the generic case, and is not needed within TSS
10436 -- subprograms and other predefined primitives.
10438 if Non_Generic_Case
10439 and then Is_Type (E)
10440 and then Has_Predicates (E)
10441 and then not Within_Internal_Subprogram
10442 then
10443 Build_Predicate_Functions (E, N);
10444 end if;
10446 -- If type has delayed aspects, this is where we do the preanalysis at
10447 -- the freeze point, as part of the consistent visibility check. Note
10448 -- that this must be done after calling Build_Predicate_Functions or
10449 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10450 -- the subtype name in the saved expression so that they will not cause
10451 -- trouble in the preanalysis.
10453 -- This is also not needed in the generic case
10455 if Non_Generic_Case
10456 and then Has_Delayed_Aspects (E)
10457 and then Scope (E) = Current_Scope
10458 then
10459 -- Retrieve the visibility to the discriminants in order to properly
10460 -- analyze the aspects.
10462 Push_Scope_And_Install_Discriminants (E);
10464 declare
10465 Ritem : Node_Id;
10467 begin
10468 -- Look for aspect specification entries for this entity
10470 Ritem := First_Rep_Item (E);
10471 while Present (Ritem) loop
10472 if Nkind (Ritem) = N_Aspect_Specification
10473 and then Entity (Ritem) = E
10474 and then Is_Delayed_Aspect (Ritem)
10475 then
10476 Check_Aspect_At_Freeze_Point (Ritem);
10477 end if;
10479 Next_Rep_Item (Ritem);
10480 end loop;
10481 end;
10483 Uninstall_Discriminants_And_Pop_Scope (E);
10484 end if;
10486 -- For a record type, deal with variant parts. This has to be delayed
10487 -- to this point, because of the issue of statically predicated
10488 -- subtypes, which we have to ensure are frozen before checking
10489 -- choices, since we need to have the static choice list set.
10491 if Is_Record_Type (E) then
10492 Check_Variant_Part : declare
10493 D : constant Node_Id := Declaration_Node (E);
10494 T : Node_Id;
10495 C : Node_Id;
10496 VP : Node_Id;
10498 Others_Present : Boolean;
10499 pragma Warnings (Off, Others_Present);
10500 -- Indicates others present, not used in this case
10502 procedure Non_Static_Choice_Error (Choice : Node_Id);
10503 -- Error routine invoked by the generic instantiation below when
10504 -- the variant part has a non static choice.
10506 procedure Process_Declarations (Variant : Node_Id);
10507 -- Processes declarations associated with a variant. We analyzed
10508 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10509 -- but we still need the recursive call to Check_Choices for any
10510 -- nested variant to get its choices properly processed. This is
10511 -- also where we expand out the choices if expansion is active.
10513 package Variant_Choices_Processing is new
10514 Generic_Check_Choices
10515 (Process_Empty_Choice => No_OP,
10516 Process_Non_Static_Choice => Non_Static_Choice_Error,
10517 Process_Associated_Node => Process_Declarations);
10518 use Variant_Choices_Processing;
10520 -----------------------------
10521 -- Non_Static_Choice_Error --
10522 -----------------------------
10524 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10525 begin
10526 Flag_Non_Static_Expr
10527 ("choice given in variant part is not static!", Choice);
10528 end Non_Static_Choice_Error;
10530 --------------------------
10531 -- Process_Declarations --
10532 --------------------------
10534 procedure Process_Declarations (Variant : Node_Id) is
10535 CL : constant Node_Id := Component_List (Variant);
10536 VP : Node_Id;
10538 begin
10539 -- Check for static predicate present in this variant
10541 if Has_SP_Choice (Variant) then
10543 -- Here we expand. You might expect to find this call in
10544 -- Expand_N_Variant_Part, but that is called when we first
10545 -- see the variant part, and we cannot do this expansion
10546 -- earlier than the freeze point, since for statically
10547 -- predicated subtypes, the predicate is not known till
10548 -- the freeze point.
10550 -- Furthermore, we do this expansion even if the expander
10551 -- is not active, because other semantic processing, e.g.
10552 -- for aggregates, requires the expanded list of choices.
10554 -- If the expander is not active, then we can't just clobber
10555 -- the list since it would invalidate the ASIS -gnatct tree.
10556 -- So we have to rewrite the variant part with a Rewrite
10557 -- call that replaces it with a copy and clobber the copy.
10559 if not Expander_Active then
10560 declare
10561 NewV : constant Node_Id := New_Copy (Variant);
10562 begin
10563 Set_Discrete_Choices
10564 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10565 Rewrite (Variant, NewV);
10566 end;
10567 end if;
10569 Expand_Static_Predicates_In_Choices (Variant);
10570 end if;
10572 -- We don't need to worry about the declarations in the variant
10573 -- (since they were analyzed by Analyze_Choices when we first
10574 -- encountered the variant), but we do need to take care of
10575 -- expansion of any nested variants.
10577 if not Null_Present (CL) then
10578 VP := Variant_Part (CL);
10580 if Present (VP) then
10581 Check_Choices
10582 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10583 end if;
10584 end if;
10585 end Process_Declarations;
10587 -- Start of processing for Check_Variant_Part
10589 begin
10590 -- Find component list
10592 C := Empty;
10594 if Nkind (D) = N_Full_Type_Declaration then
10595 T := Type_Definition (D);
10597 if Nkind (T) = N_Record_Definition then
10598 C := Component_List (T);
10600 elsif Nkind (T) = N_Derived_Type_Definition
10601 and then Present (Record_Extension_Part (T))
10602 then
10603 C := Component_List (Record_Extension_Part (T));
10604 end if;
10605 end if;
10607 -- Case of variant part present
10609 if Present (C) and then Present (Variant_Part (C)) then
10610 VP := Variant_Part (C);
10612 -- Check choices
10614 Check_Choices
10615 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10617 -- If the last variant does not contain the Others choice,
10618 -- replace it with an N_Others_Choice node since Gigi always
10619 -- wants an Others. Note that we do not bother to call Analyze
10620 -- on the modified variant part, since its only effect would be
10621 -- to compute the Others_Discrete_Choices node laboriously, and
10622 -- of course we already know the list of choices corresponding
10623 -- to the others choice (it's the list we're replacing).
10625 -- We only want to do this if the expander is active, since
10626 -- we do not want to clobber the ASIS tree.
10628 if Expander_Active then
10629 declare
10630 Last_Var : constant Node_Id :=
10631 Last_Non_Pragma (Variants (VP));
10633 Others_Node : Node_Id;
10635 begin
10636 if Nkind (First (Discrete_Choices (Last_Var))) /=
10637 N_Others_Choice
10638 then
10639 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10640 Set_Others_Discrete_Choices
10641 (Others_Node, Discrete_Choices (Last_Var));
10642 Set_Discrete_Choices
10643 (Last_Var, New_List (Others_Node));
10644 end if;
10645 end;
10646 end if;
10647 end if;
10648 end Check_Variant_Part;
10649 end if;
10650 end Freeze_Entity_Checks;
10652 -------------------------
10653 -- Get_Alignment_Value --
10654 -------------------------
10656 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10657 Align : constant Uint := Static_Integer (Expr);
10659 begin
10660 if Align = No_Uint then
10661 return No_Uint;
10663 elsif Align <= 0 then
10664 Error_Msg_N ("alignment value must be positive", Expr);
10665 return No_Uint;
10667 else
10668 for J in Int range 0 .. 64 loop
10669 declare
10670 M : constant Uint := Uint_2 ** J;
10672 begin
10673 exit when M = Align;
10675 if M > Align then
10676 Error_Msg_N
10677 ("alignment value must be power of 2", Expr);
10678 return No_Uint;
10679 end if;
10680 end;
10681 end loop;
10683 return Align;
10684 end if;
10685 end Get_Alignment_Value;
10687 -------------------------------------
10688 -- Inherit_Aspects_At_Freeze_Point --
10689 -------------------------------------
10691 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10692 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10693 (Rep_Item : Node_Id) return Boolean;
10694 -- This routine checks if Rep_Item is either a pragma or an aspect
10695 -- specification node whose correponding pragma (if any) is present in
10696 -- the Rep Item chain of the entity it has been specified to.
10698 --------------------------------------------------
10699 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10700 --------------------------------------------------
10702 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10703 (Rep_Item : Node_Id) return Boolean
10705 begin
10706 return
10707 Nkind (Rep_Item) = N_Pragma
10708 or else Present_In_Rep_Item
10709 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10710 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10712 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10714 begin
10715 -- A representation item is either subtype-specific (Size and Alignment
10716 -- clauses) or type-related (all others). Subtype-specific aspects may
10717 -- differ for different subtypes of the same type (RM 13.1.8).
10719 -- A derived type inherits each type-related representation aspect of
10720 -- its parent type that was directly specified before the declaration of
10721 -- the derived type (RM 13.1.15).
10723 -- A derived subtype inherits each subtype-specific representation
10724 -- aspect of its parent subtype that was directly specified before the
10725 -- declaration of the derived type (RM 13.1.15).
10727 -- The general processing involves inheriting a representation aspect
10728 -- from a parent type whenever the first rep item (aspect specification,
10729 -- attribute definition clause, pragma) corresponding to the given
10730 -- representation aspect in the rep item chain of Typ, if any, isn't
10731 -- directly specified to Typ but to one of its parents.
10733 -- ??? Note that, for now, just a limited number of representation
10734 -- aspects have been inherited here so far. Many of them are
10735 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
10736 -- a non- exhaustive list of aspects that likely also need to
10737 -- be moved to this routine: Alignment, Component_Alignment,
10738 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
10739 -- Preelaborable_Initialization, RM_Size and Small.
10741 -- In addition, Convention must be propagated from base type to subtype,
10742 -- because the subtype may have been declared on an incomplete view.
10744 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
10745 return;
10746 end if;
10748 -- Ada_05/Ada_2005
10750 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
10751 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
10752 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10753 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
10754 then
10755 Set_Is_Ada_2005_Only (Typ);
10756 end if;
10758 -- Ada_12/Ada_2012
10760 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
10761 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
10762 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10763 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
10764 then
10765 Set_Is_Ada_2012_Only (Typ);
10766 end if;
10768 -- Atomic/Shared
10770 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
10771 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
10772 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10773 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
10774 then
10775 Set_Is_Atomic (Typ);
10776 Set_Treat_As_Volatile (Typ);
10777 Set_Is_Volatile (Typ);
10778 end if;
10780 -- Convention
10782 if Is_Record_Type (Typ)
10783 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
10784 then
10785 Set_Convention (Typ, Convention (Base_Type (Typ)));
10786 end if;
10788 -- Default_Component_Value
10790 if Is_Array_Type (Typ)
10791 and then Is_Base_Type (Typ)
10792 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
10793 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
10794 then
10795 Set_Default_Aspect_Component_Value (Typ,
10796 Default_Aspect_Component_Value
10797 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
10798 end if;
10800 -- Default_Value
10802 if Is_Scalar_Type (Typ)
10803 and then Is_Base_Type (Typ)
10804 and then Has_Rep_Item (Typ, Name_Default_Value, False)
10805 and then Has_Rep_Item (Typ, Name_Default_Value)
10806 then
10807 Set_Default_Aspect_Value (Typ,
10808 Default_Aspect_Value
10809 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
10810 end if;
10812 -- Discard_Names
10814 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
10815 and then Has_Rep_Item (Typ, Name_Discard_Names)
10816 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10817 (Get_Rep_Item (Typ, Name_Discard_Names))
10818 then
10819 Set_Discard_Names (Typ);
10820 end if;
10822 -- Invariants
10824 if not Has_Rep_Item (Typ, Name_Invariant, False)
10825 and then Has_Rep_Item (Typ, Name_Invariant)
10826 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10827 (Get_Rep_Item (Typ, Name_Invariant))
10828 then
10829 Set_Has_Invariants (Typ);
10831 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
10832 Set_Has_Inheritable_Invariants (Typ);
10833 end if;
10835 -- If we have a subtype with invariants, whose base type does not have
10836 -- invariants, copy these invariants to the base type. This happens for
10837 -- the case of implicit base types created for scalar and array types.
10839 elsif Has_Invariants (Typ)
10840 and then not Has_Invariants (Base_Type (Typ))
10841 then
10842 Set_Has_Invariants (Base_Type (Typ));
10843 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
10844 end if;
10846 -- Volatile
10848 if not Has_Rep_Item (Typ, Name_Volatile, False)
10849 and then Has_Rep_Item (Typ, Name_Volatile)
10850 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10851 (Get_Rep_Item (Typ, Name_Volatile))
10852 then
10853 Set_Treat_As_Volatile (Typ);
10854 Set_Is_Volatile (Typ);
10855 end if;
10857 -- Inheritance for derived types only
10859 if Is_Derived_Type (Typ) then
10860 declare
10861 Bas_Typ : constant Entity_Id := Base_Type (Typ);
10862 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
10864 begin
10865 -- Atomic_Components
10867 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
10868 and then Has_Rep_Item (Typ, Name_Atomic_Components)
10869 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10870 (Get_Rep_Item (Typ, Name_Atomic_Components))
10871 then
10872 Set_Has_Atomic_Components (Imp_Bas_Typ);
10873 end if;
10875 -- Volatile_Components
10877 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
10878 and then Has_Rep_Item (Typ, Name_Volatile_Components)
10879 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10880 (Get_Rep_Item (Typ, Name_Volatile_Components))
10881 then
10882 Set_Has_Volatile_Components (Imp_Bas_Typ);
10883 end if;
10885 -- Finalize_Storage_Only.
10887 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
10888 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
10889 then
10890 Set_Finalize_Storage_Only (Bas_Typ);
10891 end if;
10893 -- Universal_Aliasing
10895 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
10896 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
10897 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10898 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
10899 then
10900 Set_Universal_Aliasing (Imp_Bas_Typ);
10901 end if;
10903 -- Record type specific aspects
10905 if Is_Record_Type (Typ) then
10907 -- Bit_Order
10909 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
10910 and then Has_Rep_Item (Typ, Name_Bit_Order)
10911 then
10912 Set_Reverse_Bit_Order (Bas_Typ,
10913 Reverse_Bit_Order (Entity (Name
10914 (Get_Rep_Item (Typ, Name_Bit_Order)))));
10915 end if;
10917 -- Scalar_Storage_Order
10919 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
10920 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
10921 then
10922 Set_Reverse_Storage_Order (Bas_Typ,
10923 Reverse_Storage_Order (Entity (Name
10924 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
10926 -- Clear default SSO indications, since the inherited aspect
10927 -- which was set explicitly overrides the default.
10929 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
10930 Set_SSO_Set_High_By_Default (Bas_Typ, False);
10931 end if;
10932 end if;
10933 end;
10934 end if;
10935 end Inherit_Aspects_At_Freeze_Point;
10937 ----------------
10938 -- Initialize --
10939 ----------------
10941 procedure Initialize is
10942 begin
10943 Address_Clause_Checks.Init;
10944 Independence_Checks.Init;
10945 Unchecked_Conversions.Init;
10946 end Initialize;
10948 ---------------------------
10949 -- Install_Discriminants --
10950 ---------------------------
10952 procedure Install_Discriminants (E : Entity_Id) is
10953 Disc : Entity_Id;
10954 Prev : Entity_Id;
10955 begin
10956 Disc := First_Discriminant (E);
10957 while Present (Disc) loop
10958 Prev := Current_Entity (Disc);
10959 Set_Current_Entity (Disc);
10960 Set_Is_Immediately_Visible (Disc);
10961 Set_Homonym (Disc, Prev);
10962 Next_Discriminant (Disc);
10963 end loop;
10964 end Install_Discriminants;
10966 -------------------------
10967 -- Is_Operational_Item --
10968 -------------------------
10970 function Is_Operational_Item (N : Node_Id) return Boolean is
10971 begin
10972 if Nkind (N) /= N_Attribute_Definition_Clause then
10973 return False;
10975 else
10976 declare
10977 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
10978 begin
10979 return Id = Attribute_Input
10980 or else Id = Attribute_Output
10981 or else Id = Attribute_Read
10982 or else Id = Attribute_Write
10983 or else Id = Attribute_External_Tag;
10984 end;
10985 end if;
10986 end Is_Operational_Item;
10988 -------------------------
10989 -- Is_Predicate_Static --
10990 -------------------------
10992 -- Note: the basic legality of the expression has already been checked, so
10993 -- we don't need to worry about cases or ranges on strings for example.
10995 function Is_Predicate_Static
10996 (Expr : Node_Id;
10997 Nam : Name_Id) return Boolean
10999 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11000 -- Given a list of case expression alternatives, returns True if all
11001 -- the alternatives are static (have all static choices, and a static
11002 -- expression).
11004 function All_Static_Choices (L : List_Id) return Boolean;
11005 -- Returns true if all elements of the list are OK static choices
11006 -- as defined below for Is_Static_Choice. Used for case expression
11007 -- alternatives and for the right operand of a membership test. An
11008 -- others_choice is static if the corresponding expression is static.
11009 -- The staticness of the bounds is checked separately.
11011 function Is_Static_Choice (N : Node_Id) return Boolean;
11012 -- Returns True if N represents a static choice (static subtype, or
11013 -- static subtype indication, or static expression, or static range).
11015 -- Note that this is a bit more inclusive than we actually need
11016 -- (in particular membership tests do not allow the use of subtype
11017 -- indications). But that doesn't matter, we have already checked
11018 -- that the construct is legal to get this far.
11020 function Is_Type_Ref (N : Node_Id) return Boolean;
11021 pragma Inline (Is_Type_Ref);
11022 -- Returns True if N is a reference to the type for the predicate in the
11023 -- expression (i.e. if it is an identifier whose Chars field matches the
11024 -- Nam given in the call). N must not be parenthesized, if the type name
11025 -- appears in parens, this routine will return False.
11027 ----------------------------------
11028 -- All_Static_Case_Alternatives --
11029 ----------------------------------
11031 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11032 N : Node_Id;
11034 begin
11035 N := First (L);
11036 while Present (N) loop
11037 if not (All_Static_Choices (Discrete_Choices (N))
11038 and then Is_OK_Static_Expression (Expression (N)))
11039 then
11040 return False;
11041 end if;
11043 Next (N);
11044 end loop;
11046 return True;
11047 end All_Static_Case_Alternatives;
11049 ------------------------
11050 -- All_Static_Choices --
11051 ------------------------
11053 function All_Static_Choices (L : List_Id) return Boolean is
11054 N : Node_Id;
11056 begin
11057 N := First (L);
11058 while Present (N) loop
11059 if not Is_Static_Choice (N) then
11060 return False;
11061 end if;
11063 Next (N);
11064 end loop;
11066 return True;
11067 end All_Static_Choices;
11069 ----------------------
11070 -- Is_Static_Choice --
11071 ----------------------
11073 function Is_Static_Choice (N : Node_Id) return Boolean is
11074 begin
11075 return Nkind (N) = N_Others_Choice
11076 or else Is_OK_Static_Expression (N)
11077 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11078 and then Is_OK_Static_Subtype (Entity (N)))
11079 or else (Nkind (N) = N_Subtype_Indication
11080 and then Is_OK_Static_Subtype (Entity (N)))
11081 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11082 end Is_Static_Choice;
11084 -----------------
11085 -- Is_Type_Ref --
11086 -----------------
11088 function Is_Type_Ref (N : Node_Id) return Boolean is
11089 begin
11090 return Nkind (N) = N_Identifier
11091 and then Chars (N) = Nam
11092 and then Paren_Count (N) = 0;
11093 end Is_Type_Ref;
11095 -- Start of processing for Is_Predicate_Static
11097 begin
11098 -- Predicate_Static means one of the following holds. Numbers are the
11099 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11101 -- 16: A static expression
11103 if Is_OK_Static_Expression (Expr) then
11104 return True;
11106 -- 17: A membership test whose simple_expression is the current
11107 -- instance, and whose membership_choice_list meets the requirements
11108 -- for a static membership test.
11110 elsif Nkind (Expr) in N_Membership_Test
11111 and then ((Present (Right_Opnd (Expr))
11112 and then Is_Static_Choice (Right_Opnd (Expr)))
11113 or else
11114 (Present (Alternatives (Expr))
11115 and then All_Static_Choices (Alternatives (Expr))))
11116 then
11117 return True;
11119 -- 18. A case_expression whose selecting_expression is the current
11120 -- instance, and whose dependent expressions are static expressions.
11122 elsif Nkind (Expr) = N_Case_Expression
11123 and then Is_Type_Ref (Expression (Expr))
11124 and then All_Static_Case_Alternatives (Alternatives (Expr))
11125 then
11126 return True;
11128 -- 19. A call to a predefined equality or ordering operator, where one
11129 -- operand is the current instance, and the other is a static
11130 -- expression.
11132 -- Note: the RM is clearly wrong here in not excluding string types.
11133 -- Without this exclusion, we would allow expressions like X > "ABC"
11134 -- to be considered as predicate-static, which is clearly not intended,
11135 -- since the idea is for predicate-static to be a subset of normal
11136 -- static expressions (and "DEF" > "ABC" is not a static expression).
11138 -- However, we do allow internally generated (not from source) equality
11139 -- and inequality operations to be valid on strings (this helps deal
11140 -- with cases where we transform A in "ABC" to A = "ABC).
11142 elsif Nkind (Expr) in N_Op_Compare
11143 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11144 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11145 and then not Comes_From_Source (Expr)))
11146 and then ((Is_Type_Ref (Left_Opnd (Expr))
11147 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11148 or else
11149 (Is_Type_Ref (Right_Opnd (Expr))
11150 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11151 then
11152 return True;
11154 -- 20. A call to a predefined boolean logical operator, where each
11155 -- operand is predicate-static.
11157 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11158 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11159 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11160 or else
11161 (Nkind (Expr) = N_Op_Not
11162 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11163 then
11164 return True;
11166 -- 21. A short-circuit control form where both operands are
11167 -- predicate-static.
11169 elsif Nkind (Expr) in N_Short_Circuit
11170 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11171 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11172 then
11173 return True;
11175 -- 22. A parenthesized predicate-static expression. This does not
11176 -- require any special test, since we just ignore paren levels in
11177 -- all the cases above.
11179 -- One more test that is an implementation artifact caused by the fact
11180 -- that we are analyzing not the original expression, but the generated
11181 -- expression in the body of the predicate function. This can include
11182 -- references to inherited predicates, so that the expression we are
11183 -- processing looks like:
11185 -- expression and then xxPredicate (typ (Inns))
11187 -- Where the call is to a Predicate function for an inherited predicate.
11188 -- We simply ignore such a call (which could be to either a dynamic or
11189 -- a static predicate, but remember that we can have a Static_Predicate
11190 -- for a non-static subtype).
11192 elsif Nkind (Expr) = N_Function_Call
11193 and then Is_Predicate_Function (Entity (Name (Expr)))
11194 then
11195 return True;
11197 -- That's an exhaustive list of tests, all other cases are not
11198 -- predicate-static, so we return False.
11200 else
11201 return False;
11202 end if;
11203 end Is_Predicate_Static;
11205 ---------------------
11206 -- Kill_Rep_Clause --
11207 ---------------------
11209 procedure Kill_Rep_Clause (N : Node_Id) is
11210 begin
11211 pragma Assert (Ignore_Rep_Clauses);
11213 -- Note: we use Replace rather than Rewrite, because we don't want
11214 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11215 -- rep clause that is being replaced.
11217 Replace (N, Make_Null_Statement (Sloc (N)));
11219 -- The null statement must be marked as not coming from source. This is
11220 -- so that ASIS ignores it, and also the back end does not expect bogus
11221 -- "from source" null statements in weird places (e.g. in declarative
11222 -- regions where such null statements are not allowed).
11224 Set_Comes_From_Source (N, False);
11225 end Kill_Rep_Clause;
11227 ------------------
11228 -- Minimum_Size --
11229 ------------------
11231 function Minimum_Size
11232 (T : Entity_Id;
11233 Biased : Boolean := False) return Nat
11235 Lo : Uint := No_Uint;
11236 Hi : Uint := No_Uint;
11237 LoR : Ureal := No_Ureal;
11238 HiR : Ureal := No_Ureal;
11239 LoSet : Boolean := False;
11240 HiSet : Boolean := False;
11241 B : Uint;
11242 S : Nat;
11243 Ancest : Entity_Id;
11244 R_Typ : constant Entity_Id := Root_Type (T);
11246 begin
11247 -- If bad type, return 0
11249 if T = Any_Type then
11250 return 0;
11252 -- For generic types, just return zero. There cannot be any legitimate
11253 -- need to know such a size, but this routine may be called with a
11254 -- generic type as part of normal processing.
11256 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11257 return 0;
11259 -- Access types (cannot have size smaller than System.Address)
11261 elsif Is_Access_Type (T) then
11262 return System_Address_Size;
11264 -- Floating-point types
11266 elsif Is_Floating_Point_Type (T) then
11267 return UI_To_Int (Esize (R_Typ));
11269 -- Discrete types
11271 elsif Is_Discrete_Type (T) then
11273 -- The following loop is looking for the nearest compile time known
11274 -- bounds following the ancestor subtype chain. The idea is to find
11275 -- the most restrictive known bounds information.
11277 Ancest := T;
11278 loop
11279 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11280 return 0;
11281 end if;
11283 if not LoSet then
11284 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11285 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11286 LoSet := True;
11287 exit when HiSet;
11288 end if;
11289 end if;
11291 if not HiSet then
11292 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11293 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11294 HiSet := True;
11295 exit when LoSet;
11296 end if;
11297 end if;
11299 Ancest := Ancestor_Subtype (Ancest);
11301 if No (Ancest) then
11302 Ancest := Base_Type (T);
11304 if Is_Generic_Type (Ancest) then
11305 return 0;
11306 end if;
11307 end if;
11308 end loop;
11310 -- Fixed-point types. We can't simply use Expr_Value to get the
11311 -- Corresponding_Integer_Value values of the bounds, since these do not
11312 -- get set till the type is frozen, and this routine can be called
11313 -- before the type is frozen. Similarly the test for bounds being static
11314 -- needs to include the case where we have unanalyzed real literals for
11315 -- the same reason.
11317 elsif Is_Fixed_Point_Type (T) then
11319 -- The following loop is looking for the nearest compile time known
11320 -- bounds following the ancestor subtype chain. The idea is to find
11321 -- the most restrictive known bounds information.
11323 Ancest := T;
11324 loop
11325 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11326 return 0;
11327 end if;
11329 -- Note: In the following two tests for LoSet and HiSet, it may
11330 -- seem redundant to test for N_Real_Literal here since normally
11331 -- one would assume that the test for the value being known at
11332 -- compile time includes this case. However, there is a glitch.
11333 -- If the real literal comes from folding a non-static expression,
11334 -- then we don't consider any non- static expression to be known
11335 -- at compile time if we are in configurable run time mode (needed
11336 -- in some cases to give a clearer definition of what is and what
11337 -- is not accepted). So the test is indeed needed. Without it, we
11338 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11340 if not LoSet then
11341 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11342 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11343 then
11344 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11345 LoSet := True;
11346 exit when HiSet;
11347 end if;
11348 end if;
11350 if not HiSet then
11351 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11352 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11353 then
11354 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11355 HiSet := True;
11356 exit when LoSet;
11357 end if;
11358 end if;
11360 Ancest := Ancestor_Subtype (Ancest);
11362 if No (Ancest) then
11363 Ancest := Base_Type (T);
11365 if Is_Generic_Type (Ancest) then
11366 return 0;
11367 end if;
11368 end if;
11369 end loop;
11371 Lo := UR_To_Uint (LoR / Small_Value (T));
11372 Hi := UR_To_Uint (HiR / Small_Value (T));
11374 -- No other types allowed
11376 else
11377 raise Program_Error;
11378 end if;
11380 -- Fall through with Hi and Lo set. Deal with biased case
11382 if (Biased
11383 and then not Is_Fixed_Point_Type (T)
11384 and then not (Is_Enumeration_Type (T)
11385 and then Has_Non_Standard_Rep (T)))
11386 or else Has_Biased_Representation (T)
11387 then
11388 Hi := Hi - Lo;
11389 Lo := Uint_0;
11390 end if;
11392 -- Signed case. Note that we consider types like range 1 .. -1 to be
11393 -- signed for the purpose of computing the size, since the bounds have
11394 -- to be accommodated in the base type.
11396 if Lo < 0 or else Hi < 0 then
11397 S := 1;
11398 B := Uint_1;
11400 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11401 -- Note that we accommodate the case where the bounds cross. This
11402 -- can happen either because of the way the bounds are declared
11403 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11405 while Lo < -B
11406 or else Hi < -B
11407 or else Lo >= B
11408 or else Hi >= B
11409 loop
11410 B := Uint_2 ** S;
11411 S := S + 1;
11412 end loop;
11414 -- Unsigned case
11416 else
11417 -- If both bounds are positive, make sure that both are represen-
11418 -- table in the case where the bounds are crossed. This can happen
11419 -- either because of the way the bounds are declared, or because of
11420 -- the algorithm in Freeze_Fixed_Point_Type.
11422 if Lo > Hi then
11423 Hi := Lo;
11424 end if;
11426 -- S = size, (can accommodate 0 .. (2**size - 1))
11428 S := 0;
11429 while Hi >= Uint_2 ** S loop
11430 S := S + 1;
11431 end loop;
11432 end if;
11434 return S;
11435 end Minimum_Size;
11437 ---------------------------
11438 -- New_Stream_Subprogram --
11439 ---------------------------
11441 procedure New_Stream_Subprogram
11442 (N : Node_Id;
11443 Ent : Entity_Id;
11444 Subp : Entity_Id;
11445 Nam : TSS_Name_Type)
11447 Loc : constant Source_Ptr := Sloc (N);
11448 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11449 Subp_Id : Entity_Id;
11450 Subp_Decl : Node_Id;
11451 F : Entity_Id;
11452 Etyp : Entity_Id;
11454 Defer_Declaration : constant Boolean :=
11455 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11456 -- For a tagged type, there is a declaration for each stream attribute
11457 -- at the freeze point, and we must generate only a completion of this
11458 -- declaration. We do the same for private types, because the full view
11459 -- might be tagged. Otherwise we generate a declaration at the point of
11460 -- the attribute definition clause.
11462 function Build_Spec return Node_Id;
11463 -- Used for declaration and renaming declaration, so that this is
11464 -- treated as a renaming_as_body.
11466 ----------------
11467 -- Build_Spec --
11468 ----------------
11470 function Build_Spec return Node_Id is
11471 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11472 Formals : List_Id;
11473 Spec : Node_Id;
11474 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11476 begin
11477 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11479 -- S : access Root_Stream_Type'Class
11481 Formals := New_List (
11482 Make_Parameter_Specification (Loc,
11483 Defining_Identifier =>
11484 Make_Defining_Identifier (Loc, Name_S),
11485 Parameter_Type =>
11486 Make_Access_Definition (Loc,
11487 Subtype_Mark =>
11488 New_Occurrence_Of (
11489 Designated_Type (Etype (F)), Loc))));
11491 if Nam = TSS_Stream_Input then
11492 Spec :=
11493 Make_Function_Specification (Loc,
11494 Defining_Unit_Name => Subp_Id,
11495 Parameter_Specifications => Formals,
11496 Result_Definition => T_Ref);
11497 else
11498 -- V : [out] T
11500 Append_To (Formals,
11501 Make_Parameter_Specification (Loc,
11502 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11503 Out_Present => Out_P,
11504 Parameter_Type => T_Ref));
11506 Spec :=
11507 Make_Procedure_Specification (Loc,
11508 Defining_Unit_Name => Subp_Id,
11509 Parameter_Specifications => Formals);
11510 end if;
11512 return Spec;
11513 end Build_Spec;
11515 -- Start of processing for New_Stream_Subprogram
11517 begin
11518 F := First_Formal (Subp);
11520 if Ekind (Subp) = E_Procedure then
11521 Etyp := Etype (Next_Formal (F));
11522 else
11523 Etyp := Etype (Subp);
11524 end if;
11526 -- Prepare subprogram declaration and insert it as an action on the
11527 -- clause node. The visibility for this entity is used to test for
11528 -- visibility of the attribute definition clause (in the sense of
11529 -- 8.3(23) as amended by AI-195).
11531 if not Defer_Declaration then
11532 Subp_Decl :=
11533 Make_Subprogram_Declaration (Loc,
11534 Specification => Build_Spec);
11536 -- For a tagged type, there is always a visible declaration for each
11537 -- stream TSS (it is a predefined primitive operation), and the
11538 -- completion of this declaration occurs at the freeze point, which is
11539 -- not always visible at places where the attribute definition clause is
11540 -- visible. So, we create a dummy entity here for the purpose of
11541 -- tracking the visibility of the attribute definition clause itself.
11543 else
11544 Subp_Id :=
11545 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11546 Subp_Decl :=
11547 Make_Object_Declaration (Loc,
11548 Defining_Identifier => Subp_Id,
11549 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11550 end if;
11552 Insert_Action (N, Subp_Decl);
11553 Set_Entity (N, Subp_Id);
11555 Subp_Decl :=
11556 Make_Subprogram_Renaming_Declaration (Loc,
11557 Specification => Build_Spec,
11558 Name => New_Occurrence_Of (Subp, Loc));
11560 if Defer_Declaration then
11561 Set_TSS (Base_Type (Ent), Subp_Id);
11562 else
11563 Insert_Action (N, Subp_Decl);
11564 Copy_TSS (Subp_Id, Base_Type (Ent));
11565 end if;
11566 end New_Stream_Subprogram;
11568 ------------------------------------------
11569 -- Push_Scope_And_Install_Discriminants --
11570 ------------------------------------------
11572 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11573 begin
11574 if Has_Discriminants (E) then
11575 Push_Scope (E);
11577 -- Make discriminants visible for type declarations and protected
11578 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11580 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11581 Install_Discriminants (E);
11582 end if;
11583 end if;
11584 end Push_Scope_And_Install_Discriminants;
11586 ------------------------
11587 -- Rep_Item_Too_Early --
11588 ------------------------
11590 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
11591 begin
11592 -- Cannot apply non-operational rep items to generic types
11594 if Is_Operational_Item (N) then
11595 return False;
11597 elsif Is_Type (T)
11598 and then Is_Generic_Type (Root_Type (T))
11599 then
11600 Error_Msg_N ("representation item not allowed for generic type", N);
11601 return True;
11602 end if;
11604 -- Otherwise check for incomplete type
11606 if Is_Incomplete_Or_Private_Type (T)
11607 and then No (Underlying_Type (T))
11608 and then
11609 (Nkind (N) /= N_Pragma
11610 or else Get_Pragma_Id (N) /= Pragma_Import)
11611 then
11612 Error_Msg_N
11613 ("representation item must be after full type declaration", N);
11614 return True;
11616 -- If the type has incomplete components, a representation clause is
11617 -- illegal but stream attributes and Convention pragmas are correct.
11619 elsif Has_Private_Component (T) then
11620 if Nkind (N) = N_Pragma then
11621 return False;
11623 else
11624 Error_Msg_N
11625 ("representation item must appear after type is fully defined",
11627 return True;
11628 end if;
11629 else
11630 return False;
11631 end if;
11632 end Rep_Item_Too_Early;
11634 -----------------------
11635 -- Rep_Item_Too_Late --
11636 -----------------------
11638 function Rep_Item_Too_Late
11639 (T : Entity_Id;
11640 N : Node_Id;
11641 FOnly : Boolean := False) return Boolean
11643 S : Entity_Id;
11644 Parent_Type : Entity_Id;
11646 procedure No_Type_Rep_Item;
11647 -- Output message indicating that no type-related aspects can be
11648 -- specified due to some property of the parent type.
11650 procedure Too_Late;
11651 -- Output message for an aspect being specified too late
11653 -- Note that neither of the above errors is considered a serious one,
11654 -- since the effect is simply that we ignore the representation clause
11655 -- in these cases.
11656 -- Is this really true? In any case if we make this change we must
11657 -- document the requirement in the spec of Rep_Item_Too_Late that
11658 -- if True is returned, then the rep item must be completely ignored???
11660 ----------------------
11661 -- No_Type_Rep_Item --
11662 ----------------------
11664 procedure No_Type_Rep_Item is
11665 begin
11666 Error_Msg_N ("|type-related representation item not permitted!", N);
11667 end No_Type_Rep_Item;
11669 --------------
11670 -- Too_Late --
11671 --------------
11673 procedure Too_Late is
11674 begin
11675 -- Other compilers seem more relaxed about rep items appearing too
11676 -- late. Since analysis tools typically don't care about rep items
11677 -- anyway, no reason to be too strict about this.
11679 if not Relaxed_RM_Semantics then
11680 Error_Msg_N ("|representation item appears too late!", N);
11681 end if;
11682 end Too_Late;
11684 -- Start of processing for Rep_Item_Too_Late
11686 begin
11687 -- First make sure entity is not frozen (RM 13.1(9))
11689 if Is_Frozen (T)
11691 -- Exclude imported types, which may be frozen if they appear in a
11692 -- representation clause for a local type.
11694 and then not From_Limited_With (T)
11696 -- Exclude generated entities (not coming from source). The common
11697 -- case is when we generate a renaming which prematurely freezes the
11698 -- renamed internal entity, but we still want to be able to set copies
11699 -- of attribute values such as Size/Alignment.
11701 and then Comes_From_Source (T)
11702 then
11703 Too_Late;
11704 S := First_Subtype (T);
11706 if Present (Freeze_Node (S)) then
11707 if not Relaxed_RM_Semantics then
11708 Error_Msg_NE
11709 ("??no more representation items for }", Freeze_Node (S), S);
11710 end if;
11711 end if;
11713 return True;
11715 -- Check for case of untagged derived type whose parent either has
11716 -- primitive operations, or is a by reference type (RM 13.1(10)). In
11717 -- this case we do not output a Too_Late message, since there is no
11718 -- earlier point where the rep item could be placed to make it legal.
11720 elsif Is_Type (T)
11721 and then not FOnly
11722 and then Is_Derived_Type (T)
11723 and then not Is_Tagged_Type (T)
11724 then
11725 Parent_Type := Etype (Base_Type (T));
11727 if Has_Primitive_Operations (Parent_Type) then
11728 No_Type_Rep_Item;
11730 if not Relaxed_RM_Semantics then
11731 Error_Msg_NE
11732 ("\parent type & has primitive operations!", N, Parent_Type);
11733 end if;
11735 return True;
11737 elsif Is_By_Reference_Type (Parent_Type) then
11738 No_Type_Rep_Item;
11740 if not Relaxed_RM_Semantics then
11741 Error_Msg_NE
11742 ("\parent type & is a by reference type!", N, Parent_Type);
11743 end if;
11745 return True;
11746 end if;
11747 end if;
11749 -- No error, but one more warning to consider. The RM (surprisingly)
11750 -- allows this pattern:
11752 -- type S is ...
11753 -- primitive operations for S
11754 -- type R is new S;
11755 -- rep clause for S
11757 -- Meaning that calls on the primitive operations of S for values of
11758 -- type R may require possibly expensive implicit conversion operations.
11759 -- This is not an error, but is worth a warning.
11761 if not Relaxed_RM_Semantics and then Is_Type (T) then
11762 declare
11763 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
11765 begin
11766 if Present (DTL)
11767 and then Has_Primitive_Operations (Base_Type (T))
11769 -- For now, do not generate this warning for the case of aspect
11770 -- specification using Ada 2012 syntax, since we get wrong
11771 -- messages we do not understand. The whole business of derived
11772 -- types and rep items seems a bit confused when aspects are
11773 -- used, since the aspects are not evaluated till freeze time.
11775 and then not From_Aspect_Specification (N)
11776 then
11777 Error_Msg_Sloc := Sloc (DTL);
11778 Error_Msg_N
11779 ("representation item for& appears after derived type "
11780 & "declaration#??", N);
11781 Error_Msg_NE
11782 ("\may result in implicit conversions for primitive "
11783 & "operations of&??", N, T);
11784 Error_Msg_NE
11785 ("\to change representations when called with arguments "
11786 & "of type&??", N, DTL);
11787 end if;
11788 end;
11789 end if;
11791 -- No error, link item into head of chain of rep items for the entity,
11792 -- but avoid chaining if we have an overloadable entity, and the pragma
11793 -- is one that can apply to multiple overloaded entities.
11795 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
11796 declare
11797 Pname : constant Name_Id := Pragma_Name (N);
11798 begin
11799 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
11800 Name_External, Name_Interface)
11801 then
11802 return False;
11803 end if;
11804 end;
11805 end if;
11807 Record_Rep_Item (T, N);
11808 return False;
11809 end Rep_Item_Too_Late;
11811 -------------------------------------
11812 -- Replace_Type_References_Generic --
11813 -------------------------------------
11815 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
11816 TName : constant Name_Id := Chars (T);
11818 function Replace_Node (N : Node_Id) return Traverse_Result;
11819 -- Processes a single node in the traversal procedure below, checking
11820 -- if node N should be replaced, and if so, doing the replacement.
11822 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
11823 -- This instantiation provides the body of Replace_Type_References
11825 ------------------
11826 -- Replace_Node --
11827 ------------------
11829 function Replace_Node (N : Node_Id) return Traverse_Result is
11830 S : Entity_Id;
11831 P : Node_Id;
11833 begin
11834 -- Case of identifier
11836 if Nkind (N) = N_Identifier then
11838 -- If not the type name, check whether it is a reference to
11839 -- some other type, which must be frozen before the predicate
11840 -- function is analyzed, i.e. before the freeze node of the
11841 -- type to which the predicate applies.
11843 if Chars (N) /= TName then
11844 if Present (Current_Entity (N))
11845 and then Is_Type (Current_Entity (N))
11846 then
11847 Freeze_Before (Freeze_Node (T), Current_Entity (N));
11848 end if;
11850 return Skip;
11852 -- Otherwise do the replacement and we are done with this node
11854 else
11855 Replace_Type_Reference (N);
11856 return Skip;
11857 end if;
11859 -- Case of selected component (which is what a qualification
11860 -- looks like in the unanalyzed tree, which is what we have.
11862 elsif Nkind (N) = N_Selected_Component then
11864 -- If selector name is not our type, keeping going (we might
11865 -- still have an occurrence of the type in the prefix).
11867 if Nkind (Selector_Name (N)) /= N_Identifier
11868 or else Chars (Selector_Name (N)) /= TName
11869 then
11870 return OK;
11872 -- Selector name is our type, check qualification
11874 else
11875 -- Loop through scopes and prefixes, doing comparison
11877 S := Current_Scope;
11878 P := Prefix (N);
11879 loop
11880 -- Continue if no more scopes or scope with no name
11882 if No (S) or else Nkind (S) not in N_Has_Chars then
11883 return OK;
11884 end if;
11886 -- Do replace if prefix is an identifier matching the
11887 -- scope that we are currently looking at.
11889 if Nkind (P) = N_Identifier
11890 and then Chars (P) = Chars (S)
11891 then
11892 Replace_Type_Reference (N);
11893 return Skip;
11894 end if;
11896 -- Go check scope above us if prefix is itself of the
11897 -- form of a selected component, whose selector matches
11898 -- the scope we are currently looking at.
11900 if Nkind (P) = N_Selected_Component
11901 and then Nkind (Selector_Name (P)) = N_Identifier
11902 and then Chars (Selector_Name (P)) = Chars (S)
11903 then
11904 S := Scope (S);
11905 P := Prefix (P);
11907 -- For anything else, we don't have a match, so keep on
11908 -- going, there are still some weird cases where we may
11909 -- still have a replacement within the prefix.
11911 else
11912 return OK;
11913 end if;
11914 end loop;
11915 end if;
11917 -- Continue for any other node kind
11919 else
11920 return OK;
11921 end if;
11922 end Replace_Node;
11924 begin
11925 Replace_Type_Refs (N);
11926 end Replace_Type_References_Generic;
11928 -------------------------
11929 -- Same_Representation --
11930 -------------------------
11932 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
11933 T1 : constant Entity_Id := Underlying_Type (Typ1);
11934 T2 : constant Entity_Id := Underlying_Type (Typ2);
11936 begin
11937 -- A quick check, if base types are the same, then we definitely have
11938 -- the same representation, because the subtype specific representation
11939 -- attributes (Size and Alignment) do not affect representation from
11940 -- the point of view of this test.
11942 if Base_Type (T1) = Base_Type (T2) then
11943 return True;
11945 elsif Is_Private_Type (Base_Type (T2))
11946 and then Base_Type (T1) = Full_View (Base_Type (T2))
11947 then
11948 return True;
11949 end if;
11951 -- Tagged types never have differing representations
11953 if Is_Tagged_Type (T1) then
11954 return True;
11955 end if;
11957 -- Representations are definitely different if conventions differ
11959 if Convention (T1) /= Convention (T2) then
11960 return False;
11961 end if;
11963 -- Representations are different if component alignments or scalar
11964 -- storage orders differ.
11966 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
11967 and then
11968 (Is_Record_Type (T2) or else Is_Array_Type (T2))
11969 and then
11970 (Component_Alignment (T1) /= Component_Alignment (T2)
11971 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
11972 then
11973 return False;
11974 end if;
11976 -- For arrays, the only real issue is component size. If we know the
11977 -- component size for both arrays, and it is the same, then that's
11978 -- good enough to know we don't have a change of representation.
11980 if Is_Array_Type (T1) then
11981 if Known_Component_Size (T1)
11982 and then Known_Component_Size (T2)
11983 and then Component_Size (T1) = Component_Size (T2)
11984 then
11985 if VM_Target = No_VM then
11986 return True;
11988 -- In VM targets the representation of arrays with aliased
11989 -- components differs from arrays with non-aliased components
11991 else
11992 return Has_Aliased_Components (Base_Type (T1))
11994 Has_Aliased_Components (Base_Type (T2));
11995 end if;
11996 end if;
11997 end if;
11999 -- Types definitely have same representation if neither has non-standard
12000 -- representation since default representations are always consistent.
12001 -- If only one has non-standard representation, and the other does not,
12002 -- then we consider that they do not have the same representation. They
12003 -- might, but there is no way of telling early enough.
12005 if Has_Non_Standard_Rep (T1) then
12006 if not Has_Non_Standard_Rep (T2) then
12007 return False;
12008 end if;
12009 else
12010 return not Has_Non_Standard_Rep (T2);
12011 end if;
12013 -- Here the two types both have non-standard representation, and we need
12014 -- to determine if they have the same non-standard representation.
12016 -- For arrays, we simply need to test if the component sizes are the
12017 -- same. Pragma Pack is reflected in modified component sizes, so this
12018 -- check also deals with pragma Pack.
12020 if Is_Array_Type (T1) then
12021 return Component_Size (T1) = Component_Size (T2);
12023 -- Tagged types always have the same representation, because it is not
12024 -- possible to specify different representations for common fields.
12026 elsif Is_Tagged_Type (T1) then
12027 return True;
12029 -- Case of record types
12031 elsif Is_Record_Type (T1) then
12033 -- Packed status must conform
12035 if Is_Packed (T1) /= Is_Packed (T2) then
12036 return False;
12038 -- Otherwise we must check components. Typ2 maybe a constrained
12039 -- subtype with fewer components, so we compare the components
12040 -- of the base types.
12042 else
12043 Record_Case : declare
12044 CD1, CD2 : Entity_Id;
12046 function Same_Rep return Boolean;
12047 -- CD1 and CD2 are either components or discriminants. This
12048 -- function tests whether they have the same representation.
12050 --------------
12051 -- Same_Rep --
12052 --------------
12054 function Same_Rep return Boolean is
12055 begin
12056 if No (Component_Clause (CD1)) then
12057 return No (Component_Clause (CD2));
12058 else
12059 -- Note: at this point, component clauses have been
12060 -- normalized to the default bit order, so that the
12061 -- comparison of Component_Bit_Offsets is meaningful.
12063 return
12064 Present (Component_Clause (CD2))
12065 and then
12066 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12067 and then
12068 Esize (CD1) = Esize (CD2);
12069 end if;
12070 end Same_Rep;
12072 -- Start of processing for Record_Case
12074 begin
12075 if Has_Discriminants (T1) then
12077 -- The number of discriminants may be different if the
12078 -- derived type has fewer (constrained by values). The
12079 -- invisible discriminants retain the representation of
12080 -- the original, so the discrepancy does not per se
12081 -- indicate a different representation.
12083 CD1 := First_Discriminant (T1);
12084 CD2 := First_Discriminant (T2);
12085 while Present (CD1) and then Present (CD2) loop
12086 if not Same_Rep then
12087 return False;
12088 else
12089 Next_Discriminant (CD1);
12090 Next_Discriminant (CD2);
12091 end if;
12092 end loop;
12093 end if;
12095 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12096 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
12097 while Present (CD1) loop
12098 if not Same_Rep then
12099 return False;
12100 else
12101 Next_Component (CD1);
12102 Next_Component (CD2);
12103 end if;
12104 end loop;
12106 return True;
12107 end Record_Case;
12108 end if;
12110 -- For enumeration types, we must check each literal to see if the
12111 -- representation is the same. Note that we do not permit enumeration
12112 -- representation clauses for Character and Wide_Character, so these
12113 -- cases were already dealt with.
12115 elsif Is_Enumeration_Type (T1) then
12116 Enumeration_Case : declare
12117 L1, L2 : Entity_Id;
12119 begin
12120 L1 := First_Literal (T1);
12121 L2 := First_Literal (T2);
12122 while Present (L1) loop
12123 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12124 return False;
12125 else
12126 Next_Literal (L1);
12127 Next_Literal (L2);
12128 end if;
12129 end loop;
12131 return True;
12132 end Enumeration_Case;
12134 -- Any other types have the same representation for these purposes
12136 else
12137 return True;
12138 end if;
12139 end Same_Representation;
12141 --------------------------------
12142 -- Resolve_Iterable_Operation --
12143 --------------------------------
12145 procedure Resolve_Iterable_Operation
12146 (N : Node_Id;
12147 Cursor : Entity_Id;
12148 Typ : Entity_Id;
12149 Nam : Name_Id)
12151 Ent : Entity_Id;
12152 F1 : Entity_Id;
12153 F2 : Entity_Id;
12155 begin
12156 if not Is_Overloaded (N) then
12157 if not Is_Entity_Name (N)
12158 or else Ekind (Entity (N)) /= E_Function
12159 or else Scope (Entity (N)) /= Scope (Typ)
12160 or else No (First_Formal (Entity (N)))
12161 or else Etype (First_Formal (Entity (N))) /= Typ
12162 then
12163 Error_Msg_N ("iterable primitive must be local function name "
12164 & "whose first formal is an iterable type", N);
12165 return;
12166 end if;
12168 Ent := Entity (N);
12169 F1 := First_Formal (Ent);
12170 if Nam = Name_First then
12172 -- First (Container) => Cursor
12174 if Etype (Ent) /= Cursor then
12175 Error_Msg_N ("primitive for First must yield a curosr", N);
12176 end if;
12178 elsif Nam = Name_Next then
12180 -- Next (Container, Cursor) => Cursor
12182 F2 := Next_Formal (F1);
12184 if Etype (F2) /= Cursor
12185 or else Etype (Ent) /= Cursor
12186 or else Present (Next_Formal (F2))
12187 then
12188 Error_Msg_N ("no match for Next iterable primitive", N);
12189 end if;
12191 elsif Nam = Name_Has_Element then
12193 -- Has_Element (Container, Cursor) => Boolean
12195 F2 := Next_Formal (F1);
12196 if Etype (F2) /= Cursor
12197 or else Etype (Ent) /= Standard_Boolean
12198 or else Present (Next_Formal (F2))
12199 then
12200 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12201 end if;
12203 elsif Nam = Name_Element then
12204 F2 := Next_Formal (F1);
12206 if No (F2)
12207 or else Etype (F2) /= Cursor
12208 or else Present (Next_Formal (F2))
12209 then
12210 Error_Msg_N ("no match for Element iterable primitive", N);
12211 end if;
12212 null;
12214 else
12215 raise Program_Error;
12216 end if;
12218 else
12219 -- Overloaded case: find subprogram with proper signature.
12220 -- Caller will report error if no match is found.
12222 declare
12223 I : Interp_Index;
12224 It : Interp;
12226 begin
12227 Get_First_Interp (N, I, It);
12228 while Present (It.Typ) loop
12229 if Ekind (It.Nam) = E_Function
12230 and then Scope (It.Nam) = Scope (Typ)
12231 and then Etype (First_Formal (It.Nam)) = Typ
12232 then
12233 F1 := First_Formal (It.Nam);
12235 if Nam = Name_First then
12236 if Etype (It.Nam) = Cursor
12237 and then No (Next_Formal (F1))
12238 then
12239 Set_Entity (N, It.Nam);
12240 exit;
12241 end if;
12243 elsif Nam = Name_Next then
12244 F2 := Next_Formal (F1);
12246 if Present (F2)
12247 and then No (Next_Formal (F2))
12248 and then Etype (F2) = Cursor
12249 and then Etype (It.Nam) = Cursor
12250 then
12251 Set_Entity (N, It.Nam);
12252 exit;
12253 end if;
12255 elsif Nam = Name_Has_Element then
12256 F2 := Next_Formal (F1);
12258 if Present (F2)
12259 and then No (Next_Formal (F2))
12260 and then Etype (F2) = Cursor
12261 and then Etype (It.Nam) = Standard_Boolean
12262 then
12263 Set_Entity (N, It.Nam);
12264 F2 := Next_Formal (F1);
12265 exit;
12266 end if;
12268 elsif Nam = Name_Element then
12269 F2 := Next_Formal (F1);
12271 if Present (F2)
12272 and then No (Next_Formal (F2))
12273 and then Etype (F2) = Cursor
12274 then
12275 Set_Entity (N, It.Nam);
12276 exit;
12277 end if;
12278 end if;
12279 end if;
12281 Get_Next_Interp (I, It);
12282 end loop;
12283 end;
12284 end if;
12285 end Resolve_Iterable_Operation;
12287 ----------------
12288 -- Set_Biased --
12289 ----------------
12291 procedure Set_Biased
12292 (E : Entity_Id;
12293 N : Node_Id;
12294 Msg : String;
12295 Biased : Boolean := True)
12297 begin
12298 if Biased then
12299 Set_Has_Biased_Representation (E);
12301 if Warn_On_Biased_Representation then
12302 Error_Msg_NE
12303 ("?B?" & Msg & " forces biased representation for&", N, E);
12304 end if;
12305 end if;
12306 end Set_Biased;
12308 --------------------
12309 -- Set_Enum_Esize --
12310 --------------------
12312 procedure Set_Enum_Esize (T : Entity_Id) is
12313 Lo : Uint;
12314 Hi : Uint;
12315 Sz : Nat;
12317 begin
12318 Init_Alignment (T);
12320 -- Find the minimum standard size (8,16,32,64) that fits
12322 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12323 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12325 if Lo < 0 then
12326 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12327 Sz := Standard_Character_Size; -- May be > 8 on some targets
12329 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12330 Sz := 16;
12332 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12333 Sz := 32;
12335 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12336 Sz := 64;
12337 end if;
12339 else
12340 if Hi < Uint_2**08 then
12341 Sz := Standard_Character_Size; -- May be > 8 on some targets
12343 elsif Hi < Uint_2**16 then
12344 Sz := 16;
12346 elsif Hi < Uint_2**32 then
12347 Sz := 32;
12349 else pragma Assert (Hi < Uint_2**63);
12350 Sz := 64;
12351 end if;
12352 end if;
12354 -- That minimum is the proper size unless we have a foreign convention
12355 -- and the size required is 32 or less, in which case we bump the size
12356 -- up to 32. This is required for C and C++ and seems reasonable for
12357 -- all other foreign conventions.
12359 if Has_Foreign_Convention (T)
12360 and then Esize (T) < Standard_Integer_Size
12362 -- Don't do this if Short_Enums on target
12364 and then not Target_Short_Enums
12365 then
12366 Init_Esize (T, Standard_Integer_Size);
12367 else
12368 Init_Esize (T, Sz);
12369 end if;
12370 end Set_Enum_Esize;
12372 -----------------------------
12373 -- Uninstall_Discriminants --
12374 -----------------------------
12376 procedure Uninstall_Discriminants (E : Entity_Id) is
12377 Disc : Entity_Id;
12378 Prev : Entity_Id;
12379 Outer : Entity_Id;
12381 begin
12382 -- Discriminants have been made visible for type declarations and
12383 -- protected type declarations, not for subtype declarations.
12385 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12386 Disc := First_Discriminant (E);
12387 while Present (Disc) loop
12388 if Disc /= Current_Entity (Disc) then
12389 Prev := Current_Entity (Disc);
12390 while Present (Prev)
12391 and then Present (Homonym (Prev))
12392 and then Homonym (Prev) /= Disc
12393 loop
12394 Prev := Homonym (Prev);
12395 end loop;
12396 else
12397 Prev := Empty;
12398 end if;
12400 Set_Is_Immediately_Visible (Disc, False);
12402 Outer := Homonym (Disc);
12403 while Present (Outer) and then Scope (Outer) = E loop
12404 Outer := Homonym (Outer);
12405 end loop;
12407 -- Reset homonym link of other entities, but do not modify link
12408 -- between entities in current scope, so that the back-end can
12409 -- have a proper count of local overloadings.
12411 if No (Prev) then
12412 Set_Name_Entity_Id (Chars (Disc), Outer);
12414 elsif Scope (Prev) /= Scope (Disc) then
12415 Set_Homonym (Prev, Outer);
12416 end if;
12418 Next_Discriminant (Disc);
12419 end loop;
12420 end if;
12421 end Uninstall_Discriminants;
12423 -------------------------------------------
12424 -- Uninstall_Discriminants_And_Pop_Scope --
12425 -------------------------------------------
12427 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12428 begin
12429 if Has_Discriminants (E) then
12430 Uninstall_Discriminants (E);
12431 Pop_Scope;
12432 end if;
12433 end Uninstall_Discriminants_And_Pop_Scope;
12435 ------------------------------
12436 -- Validate_Address_Clauses --
12437 ------------------------------
12439 procedure Validate_Address_Clauses is
12440 begin
12441 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12442 declare
12443 ACCR : Address_Clause_Check_Record
12444 renames Address_Clause_Checks.Table (J);
12446 Expr : Node_Id;
12448 X_Alignment : Uint;
12449 Y_Alignment : Uint;
12451 X_Size : Uint;
12452 Y_Size : Uint;
12454 begin
12455 -- Skip processing of this entry if warning already posted
12457 if not Address_Warning_Posted (ACCR.N) then
12458 Expr := Original_Node (Expression (ACCR.N));
12460 -- Get alignments
12462 X_Alignment := Alignment (ACCR.X);
12463 Y_Alignment := Alignment (ACCR.Y);
12465 -- Similarly obtain sizes
12467 X_Size := Esize (ACCR.X);
12468 Y_Size := Esize (ACCR.Y);
12470 -- Check for large object overlaying smaller one
12472 if Y_Size > Uint_0
12473 and then X_Size > Uint_0
12474 and then X_Size > Y_Size
12475 then
12476 Error_Msg_NE
12477 ("??& overlays smaller object", ACCR.N, ACCR.X);
12478 Error_Msg_N
12479 ("\??program execution may be erroneous", ACCR.N);
12480 Error_Msg_Uint_1 := X_Size;
12481 Error_Msg_NE
12482 ("\??size of & is ^", ACCR.N, ACCR.X);
12483 Error_Msg_Uint_1 := Y_Size;
12484 Error_Msg_NE
12485 ("\??size of & is ^", ACCR.N, ACCR.Y);
12487 -- Check for inadequate alignment, both of the base object
12488 -- and of the offset, if any.
12490 -- Note: we do not check the alignment if we gave a size
12491 -- warning, since it would likely be redundant.
12493 elsif Y_Alignment /= Uint_0
12494 and then (Y_Alignment < X_Alignment
12495 or else (ACCR.Off
12496 and then
12497 Nkind (Expr) = N_Attribute_Reference
12498 and then
12499 Attribute_Name (Expr) = Name_Address
12500 and then
12501 Has_Compatible_Alignment
12502 (ACCR.X, Prefix (Expr))
12503 /= Known_Compatible))
12504 then
12505 Error_Msg_NE
12506 ("??specified address for& may be inconsistent "
12507 & "with alignment", ACCR.N, ACCR.X);
12508 Error_Msg_N
12509 ("\??program execution may be erroneous (RM 13.3(27))",
12510 ACCR.N);
12511 Error_Msg_Uint_1 := X_Alignment;
12512 Error_Msg_NE
12513 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12514 Error_Msg_Uint_1 := Y_Alignment;
12515 Error_Msg_NE
12516 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12517 if Y_Alignment >= X_Alignment then
12518 Error_Msg_N
12519 ("\??but offset is not multiple of alignment", ACCR.N);
12520 end if;
12521 end if;
12522 end if;
12523 end;
12524 end loop;
12525 end Validate_Address_Clauses;
12527 ---------------------------
12528 -- Validate_Independence --
12529 ---------------------------
12531 procedure Validate_Independence is
12532 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12533 N : Node_Id;
12534 E : Entity_Id;
12535 IC : Boolean;
12536 Comp : Entity_Id;
12537 Addr : Node_Id;
12538 P : Node_Id;
12540 procedure Check_Array_Type (Atyp : Entity_Id);
12541 -- Checks if the array type Atyp has independent components, and
12542 -- if not, outputs an appropriate set of error messages.
12544 procedure No_Independence;
12545 -- Output message that independence cannot be guaranteed
12547 function OK_Component (C : Entity_Id) return Boolean;
12548 -- Checks one component to see if it is independently accessible, and
12549 -- if so yields True, otherwise yields False if independent access
12550 -- cannot be guaranteed. This is a conservative routine, it only
12551 -- returns True if it knows for sure, it returns False if it knows
12552 -- there is a problem, or it cannot be sure there is no problem.
12554 procedure Reason_Bad_Component (C : Entity_Id);
12555 -- Outputs continuation message if a reason can be determined for
12556 -- the component C being bad.
12558 ----------------------
12559 -- Check_Array_Type --
12560 ----------------------
12562 procedure Check_Array_Type (Atyp : Entity_Id) is
12563 Ctyp : constant Entity_Id := Component_Type (Atyp);
12565 begin
12566 -- OK if no alignment clause, no pack, and no component size
12568 if not Has_Component_Size_Clause (Atyp)
12569 and then not Has_Alignment_Clause (Atyp)
12570 and then not Is_Packed (Atyp)
12571 then
12572 return;
12573 end if;
12575 -- Case of component size is greater than or equal to 64 and the
12576 -- alignment of the array is at least as large as the alignment
12577 -- of the component. We are definitely OK in this situation.
12579 if Known_Component_Size (Atyp)
12580 and then Component_Size (Atyp) >= 64
12581 and then Known_Alignment (Atyp)
12582 and then Known_Alignment (Ctyp)
12583 and then Alignment (Atyp) >= Alignment (Ctyp)
12584 then
12585 return;
12586 end if;
12588 -- Check actual component size
12590 if not Known_Component_Size (Atyp)
12591 or else not (Addressable (Component_Size (Atyp))
12592 and then Component_Size (Atyp) < 64)
12593 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12594 then
12595 No_Independence;
12597 -- Bad component size, check reason
12599 if Has_Component_Size_Clause (Atyp) then
12600 P := Get_Attribute_Definition_Clause
12601 (Atyp, Attribute_Component_Size);
12603 if Present (P) then
12604 Error_Msg_Sloc := Sloc (P);
12605 Error_Msg_N ("\because of Component_Size clause#", N);
12606 return;
12607 end if;
12608 end if;
12610 if Is_Packed (Atyp) then
12611 P := Get_Rep_Pragma (Atyp, Name_Pack);
12613 if Present (P) then
12614 Error_Msg_Sloc := Sloc (P);
12615 Error_Msg_N ("\because of pragma Pack#", N);
12616 return;
12617 end if;
12618 end if;
12620 -- No reason found, just return
12622 return;
12623 end if;
12625 -- Array type is OK independence-wise
12627 return;
12628 end Check_Array_Type;
12630 ---------------------
12631 -- No_Independence --
12632 ---------------------
12634 procedure No_Independence is
12635 begin
12636 if Pragma_Name (N) = Name_Independent then
12637 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
12638 else
12639 Error_Msg_NE
12640 ("independent components cannot be guaranteed for&", N, E);
12641 end if;
12642 end No_Independence;
12644 ------------------
12645 -- OK_Component --
12646 ------------------
12648 function OK_Component (C : Entity_Id) return Boolean is
12649 Rec : constant Entity_Id := Scope (C);
12650 Ctyp : constant Entity_Id := Etype (C);
12652 begin
12653 -- OK if no component clause, no Pack, and no alignment clause
12655 if No (Component_Clause (C))
12656 and then not Is_Packed (Rec)
12657 and then not Has_Alignment_Clause (Rec)
12658 then
12659 return True;
12660 end if;
12662 -- Here we look at the actual component layout. A component is
12663 -- addressable if its size is a multiple of the Esize of the
12664 -- component type, and its starting position in the record has
12665 -- appropriate alignment, and the record itself has appropriate
12666 -- alignment to guarantee the component alignment.
12668 -- Make sure sizes are static, always assume the worst for any
12669 -- cases where we cannot check static values.
12671 if not (Known_Static_Esize (C)
12672 and then
12673 Known_Static_Esize (Ctyp))
12674 then
12675 return False;
12676 end if;
12678 -- Size of component must be addressable or greater than 64 bits
12679 -- and a multiple of bytes.
12681 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
12682 return False;
12683 end if;
12685 -- Check size is proper multiple
12687 if Esize (C) mod Esize (Ctyp) /= 0 then
12688 return False;
12689 end if;
12691 -- Check alignment of component is OK
12693 if not Known_Component_Bit_Offset (C)
12694 or else Component_Bit_Offset (C) < Uint_0
12695 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
12696 then
12697 return False;
12698 end if;
12700 -- Check alignment of record type is OK
12702 if not Known_Alignment (Rec)
12703 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12704 then
12705 return False;
12706 end if;
12708 -- All tests passed, component is addressable
12710 return True;
12711 end OK_Component;
12713 --------------------------
12714 -- Reason_Bad_Component --
12715 --------------------------
12717 procedure Reason_Bad_Component (C : Entity_Id) is
12718 Rec : constant Entity_Id := Scope (C);
12719 Ctyp : constant Entity_Id := Etype (C);
12721 begin
12722 -- If component clause present assume that's the problem
12724 if Present (Component_Clause (C)) then
12725 Error_Msg_Sloc := Sloc (Component_Clause (C));
12726 Error_Msg_N ("\because of Component_Clause#", N);
12727 return;
12728 end if;
12730 -- If pragma Pack clause present, assume that's the problem
12732 if Is_Packed (Rec) then
12733 P := Get_Rep_Pragma (Rec, Name_Pack);
12735 if Present (P) then
12736 Error_Msg_Sloc := Sloc (P);
12737 Error_Msg_N ("\because of pragma Pack#", N);
12738 return;
12739 end if;
12740 end if;
12742 -- See if record has bad alignment clause
12744 if Has_Alignment_Clause (Rec)
12745 and then Known_Alignment (Rec)
12746 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
12747 then
12748 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
12750 if Present (P) then
12751 Error_Msg_Sloc := Sloc (P);
12752 Error_Msg_N ("\because of Alignment clause#", N);
12753 end if;
12754 end if;
12756 -- Couldn't find a reason, so return without a message
12758 return;
12759 end Reason_Bad_Component;
12761 -- Start of processing for Validate_Independence
12763 begin
12764 for J in Independence_Checks.First .. Independence_Checks.Last loop
12765 N := Independence_Checks.Table (J).N;
12766 E := Independence_Checks.Table (J).E;
12767 IC := Pragma_Name (N) = Name_Independent_Components;
12769 -- Deal with component case
12771 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
12772 if not OK_Component (E) then
12773 No_Independence;
12774 Reason_Bad_Component (E);
12775 goto Continue;
12776 end if;
12777 end if;
12779 -- Deal with record with Independent_Components
12781 if IC and then Is_Record_Type (E) then
12782 Comp := First_Component_Or_Discriminant (E);
12783 while Present (Comp) loop
12784 if not OK_Component (Comp) then
12785 No_Independence;
12786 Reason_Bad_Component (Comp);
12787 goto Continue;
12788 end if;
12790 Next_Component_Or_Discriminant (Comp);
12791 end loop;
12792 end if;
12794 -- Deal with address clause case
12796 if Is_Object (E) then
12797 Addr := Address_Clause (E);
12799 if Present (Addr) then
12800 No_Independence;
12801 Error_Msg_Sloc := Sloc (Addr);
12802 Error_Msg_N ("\because of Address clause#", N);
12803 goto Continue;
12804 end if;
12805 end if;
12807 -- Deal with independent components for array type
12809 if IC and then Is_Array_Type (E) then
12810 Check_Array_Type (E);
12811 end if;
12813 -- Deal with independent components for array object
12815 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
12816 Check_Array_Type (Etype (E));
12817 end if;
12819 <<Continue>> null;
12820 end loop;
12821 end Validate_Independence;
12823 ------------------------------
12824 -- Validate_Iterable_Aspect --
12825 ------------------------------
12827 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
12828 Assoc : Node_Id;
12829 Expr : Node_Id;
12831 Prim : Node_Id;
12832 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
12834 First_Id : Entity_Id;
12835 Next_Id : Entity_Id;
12836 Has_Element_Id : Entity_Id;
12837 Element_Id : Entity_Id;
12839 begin
12840 -- If previous error aspect is unusable
12842 if Cursor = Any_Type then
12843 return;
12844 end if;
12846 First_Id := Empty;
12847 Next_Id := Empty;
12848 Has_Element_Id := Empty;
12849 Element_Id := Empty;
12851 -- Each expression must resolve to a function with the proper signature
12853 Assoc := First (Component_Associations (Expression (ASN)));
12854 while Present (Assoc) loop
12855 Expr := Expression (Assoc);
12856 Analyze (Expr);
12858 Prim := First (Choices (Assoc));
12860 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
12861 Error_Msg_N ("illegal name in association", Prim);
12863 elsif Chars (Prim) = Name_First then
12864 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
12865 First_Id := Entity (Expr);
12867 elsif Chars (Prim) = Name_Next then
12868 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
12869 Next_Id := Entity (Expr);
12871 elsif Chars (Prim) = Name_Has_Element then
12872 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
12873 Has_Element_Id := Entity (Expr);
12875 elsif Chars (Prim) = Name_Element then
12876 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
12877 Element_Id := Entity (Expr);
12879 else
12880 Error_Msg_N ("invalid name for iterable function", Prim);
12881 end if;
12883 Next (Assoc);
12884 end loop;
12886 if No (First_Id) then
12887 Error_Msg_N ("match for First primitive not found", ASN);
12889 elsif No (Next_Id) then
12890 Error_Msg_N ("match for Next primitive not found", ASN);
12892 elsif No (Has_Element_Id) then
12893 Error_Msg_N ("match for Has_Element primitive not found", ASN);
12895 elsif No (Element_Id) then
12896 null; -- Optional.
12897 end if;
12898 end Validate_Iterable_Aspect;
12900 -----------------------------------
12901 -- Validate_Unchecked_Conversion --
12902 -----------------------------------
12904 procedure Validate_Unchecked_Conversion
12905 (N : Node_Id;
12906 Act_Unit : Entity_Id)
12908 Source : Entity_Id;
12909 Target : Entity_Id;
12910 Vnode : Node_Id;
12912 begin
12913 -- Obtain source and target types. Note that we call Ancestor_Subtype
12914 -- here because the processing for generic instantiation always makes
12915 -- subtypes, and we want the original frozen actual types.
12917 -- If we are dealing with private types, then do the check on their
12918 -- fully declared counterparts if the full declarations have been
12919 -- encountered (they don't have to be visible, but they must exist).
12921 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
12923 if Is_Private_Type (Source)
12924 and then Present (Underlying_Type (Source))
12925 then
12926 Source := Underlying_Type (Source);
12927 end if;
12929 Target := Ancestor_Subtype (Etype (Act_Unit));
12931 -- If either type is generic, the instantiation happens within a generic
12932 -- unit, and there is nothing to check. The proper check will happen
12933 -- when the enclosing generic is instantiated.
12935 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
12936 return;
12937 end if;
12939 if Is_Private_Type (Target)
12940 and then Present (Underlying_Type (Target))
12941 then
12942 Target := Underlying_Type (Target);
12943 end if;
12945 -- Source may be unconstrained array, but not target
12947 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
12948 Error_Msg_N
12949 ("unchecked conversion to unconstrained array not allowed", N);
12950 return;
12951 end if;
12953 -- Warn if conversion between two different convention pointers
12955 if Is_Access_Type (Target)
12956 and then Is_Access_Type (Source)
12957 and then Convention (Target) /= Convention (Source)
12958 and then Warn_On_Unchecked_Conversion
12959 then
12960 -- Give warnings for subprogram pointers only on most targets
12962 if Is_Access_Subprogram_Type (Target)
12963 or else Is_Access_Subprogram_Type (Source)
12964 then
12965 Error_Msg_N
12966 ("?z?conversion between pointers with different conventions!",
12968 end if;
12969 end if;
12971 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
12972 -- warning when compiling GNAT-related sources.
12974 if Warn_On_Unchecked_Conversion
12975 and then not In_Predefined_Unit (N)
12976 and then RTU_Loaded (Ada_Calendar)
12977 and then (Chars (Source) = Name_Time
12978 or else
12979 Chars (Target) = Name_Time)
12980 then
12981 -- If Ada.Calendar is loaded and the name of one of the operands is
12982 -- Time, there is a good chance that this is Ada.Calendar.Time.
12984 declare
12985 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
12986 begin
12987 pragma Assert (Present (Calendar_Time));
12989 if Source = Calendar_Time or else Target = Calendar_Time then
12990 Error_Msg_N
12991 ("?z?representation of 'Time values may change between "
12992 & "'G'N'A'T versions", N);
12993 end if;
12994 end;
12995 end if;
12997 -- Make entry in unchecked conversion table for later processing by
12998 -- Validate_Unchecked_Conversions, which will check sizes and alignments
12999 -- (using values set by the back-end where possible). This is only done
13000 -- if the appropriate warning is active.
13002 if Warn_On_Unchecked_Conversion then
13003 Unchecked_Conversions.Append
13004 (New_Val => UC_Entry'(Eloc => Sloc (N),
13005 Source => Source,
13006 Target => Target,
13007 Act_Unit => Act_Unit));
13009 -- If both sizes are known statically now, then back end annotation
13010 -- is not required to do a proper check but if either size is not
13011 -- known statically, then we need the annotation.
13013 if Known_Static_RM_Size (Source)
13014 and then
13015 Known_Static_RM_Size (Target)
13016 then
13017 null;
13018 else
13019 Back_Annotate_Rep_Info := True;
13020 end if;
13021 end if;
13023 -- If unchecked conversion to access type, and access type is declared
13024 -- in the same unit as the unchecked conversion, then set the flag
13025 -- No_Strict_Aliasing (no strict aliasing is implicit here)
13027 if Is_Access_Type (Target) and then
13028 In_Same_Source_Unit (Target, N)
13029 then
13030 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13031 end if;
13033 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13034 -- the back end needs to perform special validation checks.
13036 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13037 -- have full expansion and the back end is called ???
13039 Vnode :=
13040 Make_Validate_Unchecked_Conversion (Sloc (N));
13041 Set_Source_Type (Vnode, Source);
13042 Set_Target_Type (Vnode, Target);
13044 -- If the unchecked conversion node is in a list, just insert before it.
13045 -- If not we have some strange case, not worth bothering about.
13047 if Is_List_Member (N) then
13048 Insert_After (N, Vnode);
13049 end if;
13050 end Validate_Unchecked_Conversion;
13052 ------------------------------------
13053 -- Validate_Unchecked_Conversions --
13054 ------------------------------------
13056 procedure Validate_Unchecked_Conversions is
13057 begin
13058 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13059 declare
13060 T : UC_Entry renames Unchecked_Conversions.Table (N);
13062 Eloc : constant Source_Ptr := T.Eloc;
13063 Source : constant Entity_Id := T.Source;
13064 Target : constant Entity_Id := T.Target;
13065 Act_Unit : constant Entity_Id := T.Act_Unit;
13067 Source_Siz : Uint;
13068 Target_Siz : Uint;
13070 begin
13071 -- Skip if function marked as warnings off
13073 if Warnings_Off (Act_Unit) then
13074 goto Continue;
13075 end if;
13077 -- This validation check, which warns if we have unequal sizes for
13078 -- unchecked conversion, and thus potentially implementation
13079 -- dependent semantics, is one of the few occasions on which we
13080 -- use the official RM size instead of Esize. See description in
13081 -- Einfo "Handling of Type'Size Values" for details.
13083 if Serious_Errors_Detected = 0
13084 and then Known_Static_RM_Size (Source)
13085 and then Known_Static_RM_Size (Target)
13087 -- Don't do the check if warnings off for either type, note the
13088 -- deliberate use of OR here instead of OR ELSE to get the flag
13089 -- Warnings_Off_Used set for both types if appropriate.
13091 and then not (Has_Warnings_Off (Source)
13093 Has_Warnings_Off (Target))
13094 then
13095 Source_Siz := RM_Size (Source);
13096 Target_Siz := RM_Size (Target);
13098 if Source_Siz /= Target_Siz then
13099 Error_Msg
13100 ("?z?types for unchecked conversion have different sizes!",
13101 Eloc);
13103 if All_Errors_Mode then
13104 Error_Msg_Name_1 := Chars (Source);
13105 Error_Msg_Uint_1 := Source_Siz;
13106 Error_Msg_Name_2 := Chars (Target);
13107 Error_Msg_Uint_2 := Target_Siz;
13108 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
13110 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13112 if Is_Discrete_Type (Source)
13113 and then
13114 Is_Discrete_Type (Target)
13115 then
13116 if Source_Siz > Target_Siz then
13117 Error_Msg
13118 ("\?z?^ high order bits of source will "
13119 & "be ignored!", Eloc);
13121 elsif Is_Unsigned_Type (Source) then
13122 Error_Msg
13123 ("\?z?source will be extended with ^ high order "
13124 & "zero bits!", Eloc);
13126 else
13127 Error_Msg
13128 ("\?z?source will be extended with ^ high order "
13129 & "sign bits!", Eloc);
13130 end if;
13132 elsif Source_Siz < Target_Siz then
13133 if Is_Discrete_Type (Target) then
13134 if Bytes_Big_Endian then
13135 Error_Msg
13136 ("\?z?target value will include ^ undefined "
13137 & "low order bits!", Eloc);
13138 else
13139 Error_Msg
13140 ("\?z?target value will include ^ undefined "
13141 & "high order bits!", Eloc);
13142 end if;
13144 else
13145 Error_Msg
13146 ("\?z?^ trailing bits of target value will be "
13147 & "undefined!", Eloc);
13148 end if;
13150 else pragma Assert (Source_Siz > Target_Siz);
13151 Error_Msg
13152 ("\?z?^ trailing bits of source will be ignored!",
13153 Eloc);
13154 end if;
13155 end if;
13156 end if;
13157 end if;
13159 -- If both types are access types, we need to check the alignment.
13160 -- If the alignment of both is specified, we can do it here.
13162 if Serious_Errors_Detected = 0
13163 and then Is_Access_Type (Source)
13164 and then Is_Access_Type (Target)
13165 and then Target_Strict_Alignment
13166 and then Present (Designated_Type (Source))
13167 and then Present (Designated_Type (Target))
13168 then
13169 declare
13170 D_Source : constant Entity_Id := Designated_Type (Source);
13171 D_Target : constant Entity_Id := Designated_Type (Target);
13173 begin
13174 if Known_Alignment (D_Source)
13175 and then
13176 Known_Alignment (D_Target)
13177 then
13178 declare
13179 Source_Align : constant Uint := Alignment (D_Source);
13180 Target_Align : constant Uint := Alignment (D_Target);
13182 begin
13183 if Source_Align < Target_Align
13184 and then not Is_Tagged_Type (D_Source)
13186 -- Suppress warning if warnings suppressed on either
13187 -- type or either designated type. Note the use of
13188 -- OR here instead of OR ELSE. That is intentional,
13189 -- we would like to set flag Warnings_Off_Used in
13190 -- all types for which warnings are suppressed.
13192 and then not (Has_Warnings_Off (D_Source)
13194 Has_Warnings_Off (D_Target)
13196 Has_Warnings_Off (Source)
13198 Has_Warnings_Off (Target))
13199 then
13200 Error_Msg_Uint_1 := Target_Align;
13201 Error_Msg_Uint_2 := Source_Align;
13202 Error_Msg_Node_1 := D_Target;
13203 Error_Msg_Node_2 := D_Source;
13204 Error_Msg
13205 ("?z?alignment of & (^) is stricter than "
13206 & "alignment of & (^)!", Eloc);
13207 Error_Msg
13208 ("\?z?resulting access value may have invalid "
13209 & "alignment!", Eloc);
13210 end if;
13211 end;
13212 end if;
13213 end;
13214 end if;
13215 end;
13217 <<Continue>>
13218 null;
13219 end loop;
13220 end Validate_Unchecked_Conversions;
13222 end Sem_Ch13;