2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / sem_ch13.adb
blob5494d3321841c4f03d7d18ef7dc2c6a84c45d6df
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Case; use Sem_Case;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Dim; use Sem_Dim;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Prag; use Sem_Prag;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Sinput; use Sinput;
62 with Snames; use Snames;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Stringt; use Stringt;
66 with Targparm; use Targparm;
67 with Ttypes; use Ttypes;
68 with Tbuild; use Tbuild;
69 with Urealp; use Urealp;
70 with Warnsw; use Warnsw;
72 with GNAT.Heap_Sort_G;
74 package body Sem_Ch13 is
76 SSU : constant Pos := System_Storage_Unit;
77 -- Convenient short hand for commonly used constant
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
84 -- This routine is called after setting one of the sizes of type entity
85 -- Typ to Size. The purpose is to deal with the situation of a derived
86 -- type whose inherited alignment is no longer appropriate for the new
87 -- size value. In this case, we reset the Alignment to unknown.
89 procedure Build_Discrete_Static_Predicate
90 (Typ : Entity_Id;
91 Expr : Node_Id;
92 Nam : Name_Id);
93 -- Given a predicated type Typ, where Typ is a discrete static subtype,
94 -- whose predicate expression is Expr, tests if Expr is a static predicate,
95 -- and if so, builds the predicate range list. Nam is the name of the one
96 -- argument to the predicate function. Occurrences of the type name in the
97 -- predicate expression have been replaced by identifier references to this
98 -- name, which is unique, so any identifier with Chars matching Nam must be
99 -- a reference to the type. If the predicate is non-static, this procedure
100 -- returns doing nothing. If the predicate is static, then the predicate
101 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
102 -- rewritten as a canonicalized membership operation.
104 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
105 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
106 -- then either there are pragma Predicate entries on the rep chain for the
107 -- type (note that Predicate aspects are converted to pragma Predicate), or
108 -- there are inherited aspects from a parent type, or ancestor subtypes.
109 -- This procedure builds the spec and body for the Predicate function that
110 -- tests these predicates. N is the freeze node for the type. The spec of
111 -- the function is inserted before the freeze node, and the body of the
112 -- function is inserted after the freeze node. If the predicate expression
113 -- has at least one Raise_Expression, then this procedure also builds the
114 -- M version of the predicate function for use in membership tests.
116 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
117 -- Called if both Storage_Pool and Storage_Size attribute definition
118 -- clauses (SP and SS) are present for entity Ent. Issue error message.
120 procedure Freeze_Entity_Checks (N : Node_Id);
121 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
122 -- to generate appropriate semantic checks that are delayed until this
123 -- point (they had to be delayed this long for cases of delayed aspects,
124 -- e.g. analysis of statically predicated subtypes in choices, for which
125 -- we have to be sure the subtypes in question are frozen before checking.
127 function Get_Alignment_Value (Expr : Node_Id) return Uint;
128 -- Given the expression for an alignment value, returns the corresponding
129 -- Uint value. If the value is inappropriate, then error messages are
130 -- posted as required, and a value of No_Uint is returned.
132 function Is_Operational_Item (N : Node_Id) return Boolean;
133 -- A specification for a stream attribute is allowed before the full type
134 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
135 -- that do not specify a representation characteristic are operational
136 -- attributes.
138 function Is_Predicate_Static
139 (Expr : Node_Id;
140 Nam : Name_Id) return Boolean;
141 -- Given predicate expression Expr, tests if Expr is predicate-static in
142 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
143 -- name in the predicate expression have been replaced by references to
144 -- an identifier whose Chars field is Nam. This name is unique, so any
145 -- identifier with Chars matching Nam must be a reference to the type.
146 -- Returns True if the expression is predicate-static and False otherwise,
147 -- but is not in the business of setting flags or issuing error messages.
149 -- Only scalar types can have static predicates, so False is always
150 -- returned for non-scalar types.
152 -- Note: the RM seems to suggest that string types can also have static
153 -- predicates. But that really makes lttle sense as very few useful
154 -- predicates can be constructed for strings. Remember that:
156 -- "ABC" < "DEF"
158 -- is not a static expression. So even though the clearly faulty RM wording
159 -- allows the following:
161 -- subtype S is String with Static_Predicate => S < "DEF"
163 -- We can't allow this, otherwise we have predicate-static applying to a
164 -- larger class than static expressions, which was never intended.
166 procedure New_Stream_Subprogram
167 (N : Node_Id;
168 Ent : Entity_Id;
169 Subp : Entity_Id;
170 Nam : TSS_Name_Type);
171 -- Create a subprogram renaming of a given stream attribute to the
172 -- designated subprogram and then in the tagged case, provide this as a
173 -- primitive operation, or in the untagged case make an appropriate TSS
174 -- entry. This is more properly an expansion activity than just semantics,
175 -- but the presence of user-defined stream functions for limited types
176 -- is a legality check, which is why this takes place here rather than in
177 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
178 -- function to be generated.
180 -- To avoid elaboration anomalies with freeze nodes, for untagged types
181 -- we generate both a subprogram declaration and a subprogram renaming
182 -- declaration, so that the attribute specification is handled as a
183 -- renaming_as_body. For tagged types, the specification is one of the
184 -- primitive specs.
186 procedure Resolve_Iterable_Operation
187 (N : Node_Id;
188 Cursor : Entity_Id;
189 Typ : Entity_Id;
190 Nam : Name_Id);
191 -- If the name of a primitive operation for an Iterable aspect is
192 -- overloaded, resolve according to required signature.
194 procedure Set_Biased
195 (E : Entity_Id;
196 N : Node_Id;
197 Msg : String;
198 Biased : Boolean := True);
199 -- If Biased is True, sets Has_Biased_Representation flag for E, and
200 -- outputs a warning message at node N if Warn_On_Biased_Representation is
201 -- is True. This warning inserts the string Msg to describe the construct
202 -- causing biasing.
204 ----------------------------------------------
205 -- Table for Validate_Unchecked_Conversions --
206 ----------------------------------------------
208 -- The following table collects unchecked conversions for validation.
209 -- Entries are made by Validate_Unchecked_Conversion and then the call
210 -- to Validate_Unchecked_Conversions does the actual error checking and
211 -- posting of warnings. The reason for this delayed processing is to take
212 -- advantage of back-annotations of size and alignment values performed by
213 -- the back end.
215 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
216 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
217 -- already have modified all Sloc values if the -gnatD option is set.
219 type UC_Entry is record
220 Eloc : Source_Ptr; -- node used for posting warnings
221 Source : Entity_Id; -- source type for unchecked conversion
222 Target : Entity_Id; -- target type for unchecked conversion
223 Act_Unit : Entity_Id; -- actual function instantiated
224 end record;
226 package Unchecked_Conversions is new Table.Table (
227 Table_Component_Type => UC_Entry,
228 Table_Index_Type => Int,
229 Table_Low_Bound => 1,
230 Table_Initial => 50,
231 Table_Increment => 200,
232 Table_Name => "Unchecked_Conversions");
234 ----------------------------------------
235 -- Table for Validate_Address_Clauses --
236 ----------------------------------------
238 -- If an address clause has the form
240 -- for X'Address use Expr
242 -- where Expr is of the form Y'Address or recursively is a reference to a
243 -- constant of either of these forms, and X and Y are entities of objects,
244 -- then if Y has a smaller alignment than X, that merits a warning about
245 -- possible bad alignment. The following table collects address clauses of
246 -- this kind. We put these in a table so that they can be checked after the
247 -- back end has completed annotation of the alignments of objects, since we
248 -- can catch more cases that way.
250 type Address_Clause_Check_Record is record
251 N : Node_Id;
252 -- The address clause
254 X : Entity_Id;
255 -- The entity of the object overlaying Y
257 Y : Entity_Id;
258 -- The entity of the object being overlaid
260 Off : Boolean;
261 -- Whether the address is offset within Y
262 end record;
264 package Address_Clause_Checks is new Table.Table (
265 Table_Component_Type => Address_Clause_Check_Record,
266 Table_Index_Type => Int,
267 Table_Low_Bound => 1,
268 Table_Initial => 20,
269 Table_Increment => 200,
270 Table_Name => "Address_Clause_Checks");
272 -----------------------------------------
273 -- Adjust_Record_For_Reverse_Bit_Order --
274 -----------------------------------------
276 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
277 Comp : Node_Id;
278 CC : Node_Id;
280 begin
281 -- Processing depends on version of Ada
283 -- For Ada 95, we just renumber bits within a storage unit. We do the
284 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
285 -- Ada 83, and are free to add this extension.
287 if Ada_Version < Ada_2005 then
288 Comp := First_Component_Or_Discriminant (R);
289 while Present (Comp) loop
290 CC := Component_Clause (Comp);
292 -- If component clause is present, then deal with the non-default
293 -- bit order case for Ada 95 mode.
295 -- We only do this processing for the base type, and in fact that
296 -- is important, since otherwise if there are record subtypes, we
297 -- could reverse the bits once for each subtype, which is wrong.
299 if Present (CC) and then Ekind (R) = E_Record_Type then
300 declare
301 CFB : constant Uint := Component_Bit_Offset (Comp);
302 CSZ : constant Uint := Esize (Comp);
303 CLC : constant Node_Id := Component_Clause (Comp);
304 Pos : constant Node_Id := Position (CLC);
305 FB : constant Node_Id := First_Bit (CLC);
307 Storage_Unit_Offset : constant Uint :=
308 CFB / System_Storage_Unit;
310 Start_Bit : constant Uint :=
311 CFB mod System_Storage_Unit;
313 begin
314 -- Cases where field goes over storage unit boundary
316 if Start_Bit + CSZ > System_Storage_Unit then
318 -- Allow multi-byte field but generate warning
320 if Start_Bit mod System_Storage_Unit = 0
321 and then CSZ mod System_Storage_Unit = 0
322 then
323 Error_Msg_N
324 ("info: multi-byte field specified with "
325 & "non-standard Bit_Order?V?", CLC);
327 if Bytes_Big_Endian then
328 Error_Msg_N
329 ("\bytes are not reversed "
330 & "(component is big-endian)?V?", CLC);
331 else
332 Error_Msg_N
333 ("\bytes are not reversed "
334 & "(component is little-endian)?V?", CLC);
335 end if;
337 -- Do not allow non-contiguous field
339 else
340 Error_Msg_N
341 ("attempt to specify non-contiguous field "
342 & "not permitted", CLC);
343 Error_Msg_N
344 ("\caused by non-standard Bit_Order "
345 & "specified", CLC);
346 Error_Msg_N
347 ("\consider possibility of using "
348 & "Ada 2005 mode here", CLC);
349 end if;
351 -- Case where field fits in one storage unit
353 else
354 -- Give warning if suspicious component clause
356 if Intval (FB) >= System_Storage_Unit
357 and then Warn_On_Reverse_Bit_Order
358 then
359 Error_Msg_N
360 ("info: Bit_Order clause does not affect " &
361 "byte ordering?V?", Pos);
362 Error_Msg_Uint_1 :=
363 Intval (Pos) + Intval (FB) /
364 System_Storage_Unit;
365 Error_Msg_N
366 ("info: position normalized to ^ before bit " &
367 "order interpreted?V?", Pos);
368 end if;
370 -- Here is where we fix up the Component_Bit_Offset value
371 -- to account for the reverse bit order. Some examples of
372 -- what needs to be done are:
374 -- First_Bit .. Last_Bit Component_Bit_Offset
375 -- old new old new
377 -- 0 .. 0 7 .. 7 0 7
378 -- 0 .. 1 6 .. 7 0 6
379 -- 0 .. 2 5 .. 7 0 5
380 -- 0 .. 7 0 .. 7 0 4
382 -- 1 .. 1 6 .. 6 1 6
383 -- 1 .. 4 3 .. 6 1 3
384 -- 4 .. 7 0 .. 3 4 0
386 -- The rule is that the first bit is is obtained by
387 -- subtracting the old ending bit from storage_unit - 1.
389 Set_Component_Bit_Offset
390 (Comp,
391 (Storage_Unit_Offset * System_Storage_Unit) +
392 (System_Storage_Unit - 1) -
393 (Start_Bit + CSZ - 1));
395 Set_Normalized_First_Bit
396 (Comp,
397 Component_Bit_Offset (Comp) mod
398 System_Storage_Unit);
399 end if;
400 end;
401 end if;
403 Next_Component_Or_Discriminant (Comp);
404 end loop;
406 -- For Ada 2005, we do machine scalar processing, as fully described In
407 -- AI-133. This involves gathering all components which start at the
408 -- same byte offset and processing them together. Same approach is still
409 -- valid in later versions including Ada 2012.
411 else
412 declare
413 Max_Machine_Scalar_Size : constant Uint :=
414 UI_From_Int
415 (Standard_Long_Long_Integer_Size);
416 -- We use this as the maximum machine scalar size
418 Num_CC : Natural;
419 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
421 begin
422 -- This first loop through components does two things. First it
423 -- deals with the case of components with component clauses whose
424 -- length is greater than the maximum machine scalar size (either
425 -- accepting them or rejecting as needed). Second, it counts the
426 -- number of components with component clauses whose length does
427 -- not exceed this maximum for later processing.
429 Num_CC := 0;
430 Comp := First_Component_Or_Discriminant (R);
431 while Present (Comp) loop
432 CC := Component_Clause (Comp);
434 if Present (CC) then
435 declare
436 Fbit : constant Uint := Static_Integer (First_Bit (CC));
437 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
439 begin
440 -- Case of component with last bit >= max machine scalar
442 if Lbit >= Max_Machine_Scalar_Size then
444 -- This is allowed only if first bit is zero, and
445 -- last bit + 1 is a multiple of storage unit size.
447 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
449 -- This is the case to give a warning if enabled
451 if Warn_On_Reverse_Bit_Order then
452 Error_Msg_N
453 ("info: multi-byte field specified with "
454 & " non-standard Bit_Order?V?", CC);
456 if Bytes_Big_Endian then
457 Error_Msg_N
458 ("\bytes are not reversed "
459 & "(component is big-endian)?V?", CC);
460 else
461 Error_Msg_N
462 ("\bytes are not reversed "
463 & "(component is little-endian)?V?", CC);
464 end if;
465 end if;
467 -- Give error message for RM 13.5.1(10) violation
469 else
470 Error_Msg_FE
471 ("machine scalar rules not followed for&",
472 First_Bit (CC), Comp);
474 Error_Msg_Uint_1 := Lbit;
475 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
476 Error_Msg_F
477 ("\last bit (^) exceeds maximum machine "
478 & "scalar size (^)",
479 First_Bit (CC));
481 if (Lbit + 1) mod SSU /= 0 then
482 Error_Msg_Uint_1 := SSU;
483 Error_Msg_F
484 ("\and is not a multiple of Storage_Unit (^) "
485 & "(RM 13.4.1(10))",
486 First_Bit (CC));
488 else
489 Error_Msg_Uint_1 := Fbit;
490 Error_Msg_F
491 ("\and first bit (^) is non-zero "
492 & "(RM 13.4.1(10))",
493 First_Bit (CC));
494 end if;
495 end if;
497 -- OK case of machine scalar related component clause,
498 -- For now, just count them.
500 else
501 Num_CC := Num_CC + 1;
502 end if;
503 end;
504 end if;
506 Next_Component_Or_Discriminant (Comp);
507 end loop;
509 -- We need to sort the component clauses on the basis of the
510 -- Position values in the clause, so we can group clauses with
511 -- the same Position together to determine the relevant machine
512 -- scalar size.
514 Sort_CC : declare
515 Comps : array (0 .. Num_CC) of Entity_Id;
516 -- Array to collect component and discriminant entities. The
517 -- data starts at index 1, the 0'th entry is for the sort
518 -- routine.
520 function CP_Lt (Op1, Op2 : Natural) return Boolean;
521 -- Compare routine for Sort
523 procedure CP_Move (From : Natural; To : Natural);
524 -- Move routine for Sort
526 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
528 Start : Natural;
529 Stop : Natural;
530 -- Start and stop positions in the component list of the set of
531 -- components with the same starting position (that constitute
532 -- components in a single machine scalar).
534 MaxL : Uint;
535 -- Maximum last bit value of any component in this set
537 MSS : Uint;
538 -- Corresponding machine scalar size
540 -----------
541 -- CP_Lt --
542 -----------
544 function CP_Lt (Op1, Op2 : Natural) return Boolean is
545 begin
546 return Position (Component_Clause (Comps (Op1))) <
547 Position (Component_Clause (Comps (Op2)));
548 end CP_Lt;
550 -------------
551 -- CP_Move --
552 -------------
554 procedure CP_Move (From : Natural; To : Natural) is
555 begin
556 Comps (To) := Comps (From);
557 end CP_Move;
559 -- Start of processing for Sort_CC
561 begin
562 -- Collect the machine scalar relevant component clauses
564 Num_CC := 0;
565 Comp := First_Component_Or_Discriminant (R);
566 while Present (Comp) loop
567 declare
568 CC : constant Node_Id := Component_Clause (Comp);
570 begin
571 -- Collect only component clauses whose last bit is less
572 -- than machine scalar size. Any component clause whose
573 -- last bit exceeds this value does not take part in
574 -- machine scalar layout considerations. The test for
575 -- Error_Posted makes sure we exclude component clauses
576 -- for which we already posted an error.
578 if Present (CC)
579 and then not Error_Posted (Last_Bit (CC))
580 and then Static_Integer (Last_Bit (CC)) <
581 Max_Machine_Scalar_Size
582 then
583 Num_CC := Num_CC + 1;
584 Comps (Num_CC) := Comp;
585 end if;
586 end;
588 Next_Component_Or_Discriminant (Comp);
589 end loop;
591 -- Sort by ascending position number
593 Sorting.Sort (Num_CC);
595 -- We now have all the components whose size does not exceed
596 -- the max machine scalar value, sorted by starting position.
597 -- In this loop we gather groups of clauses starting at the
598 -- same position, to process them in accordance with AI-133.
600 Stop := 0;
601 while Stop < Num_CC loop
602 Start := Stop + 1;
603 Stop := Start;
604 MaxL :=
605 Static_Integer
606 (Last_Bit (Component_Clause (Comps (Start))));
607 while Stop < Num_CC loop
608 if Static_Integer
609 (Position (Component_Clause (Comps (Stop + 1)))) =
610 Static_Integer
611 (Position (Component_Clause (Comps (Stop))))
612 then
613 Stop := Stop + 1;
614 MaxL :=
615 UI_Max
616 (MaxL,
617 Static_Integer
618 (Last_Bit
619 (Component_Clause (Comps (Stop)))));
620 else
621 exit;
622 end if;
623 end loop;
625 -- Now we have a group of component clauses from Start to
626 -- Stop whose positions are identical, and MaxL is the
627 -- maximum last bit value of any of these components.
629 -- We need to determine the corresponding machine scalar
630 -- size. This loop assumes that machine scalar sizes are
631 -- even, and that each possible machine scalar has twice
632 -- as many bits as the next smaller one.
634 MSS := Max_Machine_Scalar_Size;
635 while MSS mod 2 = 0
636 and then (MSS / 2) >= SSU
637 and then (MSS / 2) > MaxL
638 loop
639 MSS := MSS / 2;
640 end loop;
642 -- Here is where we fix up the Component_Bit_Offset value
643 -- to account for the reverse bit order. Some examples of
644 -- what needs to be done for the case of a machine scalar
645 -- size of 8 are:
647 -- First_Bit .. Last_Bit Component_Bit_Offset
648 -- old new old new
650 -- 0 .. 0 7 .. 7 0 7
651 -- 0 .. 1 6 .. 7 0 6
652 -- 0 .. 2 5 .. 7 0 5
653 -- 0 .. 7 0 .. 7 0 4
655 -- 1 .. 1 6 .. 6 1 6
656 -- 1 .. 4 3 .. 6 1 3
657 -- 4 .. 7 0 .. 3 4 0
659 -- The rule is that the first bit is obtained by subtracting
660 -- the old ending bit from machine scalar size - 1.
662 for C in Start .. Stop loop
663 declare
664 Comp : constant Entity_Id := Comps (C);
665 CC : constant Node_Id := Component_Clause (Comp);
667 LB : constant Uint := Static_Integer (Last_Bit (CC));
668 NFB : constant Uint := MSS - Uint_1 - LB;
669 NLB : constant Uint := NFB + Esize (Comp) - 1;
670 Pos : constant Uint := Static_Integer (Position (CC));
672 begin
673 if Warn_On_Reverse_Bit_Order then
674 Error_Msg_Uint_1 := MSS;
675 Error_Msg_N
676 ("info: reverse bit order in machine " &
677 "scalar of length^?V?", First_Bit (CC));
678 Error_Msg_Uint_1 := NFB;
679 Error_Msg_Uint_2 := NLB;
681 if Bytes_Big_Endian then
682 Error_Msg_NE
683 ("\big-endian range for component "
684 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
685 else
686 Error_Msg_NE
687 ("\little-endian range for component"
688 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
689 end if;
690 end if;
692 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
693 Set_Normalized_First_Bit (Comp, NFB mod SSU);
694 end;
695 end loop;
696 end loop;
697 end Sort_CC;
698 end;
699 end if;
700 end Adjust_Record_For_Reverse_Bit_Order;
702 -------------------------------------
703 -- Alignment_Check_For_Size_Change --
704 -------------------------------------
706 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
707 begin
708 -- If the alignment is known, and not set by a rep clause, and is
709 -- inconsistent with the size being set, then reset it to unknown,
710 -- we assume in this case that the size overrides the inherited
711 -- alignment, and that the alignment must be recomputed.
713 if Known_Alignment (Typ)
714 and then not Has_Alignment_Clause (Typ)
715 and then Size mod (Alignment (Typ) * SSU) /= 0
716 then
717 Init_Alignment (Typ);
718 end if;
719 end Alignment_Check_For_Size_Change;
721 -------------------------------------
722 -- Analyze_Aspects_At_Freeze_Point --
723 -------------------------------------
725 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
726 ASN : Node_Id;
727 A_Id : Aspect_Id;
728 Ritem : Node_Id;
730 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
731 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
732 -- the aspect specification node ASN.
734 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
735 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
736 -- a derived type can inherit aspects from its parent which have been
737 -- specified at the time of the derivation using an aspect, as in:
739 -- type A is range 1 .. 10
740 -- with Size => Not_Defined_Yet;
741 -- ..
742 -- type B is new A;
743 -- ..
744 -- Not_Defined_Yet : constant := 64;
746 -- In this example, the Size of A is considered to be specified prior
747 -- to the derivation, and thus inherited, even though the value is not
748 -- known at the time of derivation. To deal with this, we use two entity
749 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
750 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
751 -- the derived type (B here). If this flag is set when the derived type
752 -- is frozen, then this procedure is called to ensure proper inheritance
753 -- of all delayed aspects from the parent type. The derived type is E,
754 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
755 -- aspect specification node in the Rep_Item chain for the parent type.
757 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
758 -- Given an aspect specification node ASN whose expression is an
759 -- optional Boolean, this routines creates the corresponding pragma
760 -- at the freezing point.
762 ----------------------------------
763 -- Analyze_Aspect_Default_Value --
764 ----------------------------------
766 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
767 Ent : constant Entity_Id := Entity (ASN);
768 Expr : constant Node_Id := Expression (ASN);
769 Id : constant Node_Id := Identifier (ASN);
771 begin
772 Error_Msg_Name_1 := Chars (Id);
774 if not Is_Type (Ent) then
775 Error_Msg_N ("aspect% can only apply to a type", Id);
776 return;
778 elsif not Is_First_Subtype (Ent) then
779 Error_Msg_N ("aspect% cannot apply to subtype", Id);
780 return;
782 elsif A_Id = Aspect_Default_Value
783 and then not Is_Scalar_Type (Ent)
784 then
785 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
786 return;
788 elsif A_Id = Aspect_Default_Component_Value then
789 if not Is_Array_Type (Ent) then
790 Error_Msg_N ("aspect% can only be applied to array type", Id);
791 return;
793 elsif not Is_Scalar_Type (Component_Type (Ent)) then
794 Error_Msg_N ("aspect% requires scalar components", Id);
795 return;
796 end if;
797 end if;
799 Set_Has_Default_Aspect (Base_Type (Ent));
801 if Is_Scalar_Type (Ent) then
802 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
803 else
804 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
805 end if;
806 end Analyze_Aspect_Default_Value;
808 ---------------------------------
809 -- Inherit_Delayed_Rep_Aspects --
810 ---------------------------------
812 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
813 P : constant Entity_Id := Entity (ASN);
814 -- Entithy for parent type
816 N : Node_Id;
817 -- Item from Rep_Item chain
819 A : Aspect_Id;
821 begin
822 -- Loop through delayed aspects for the parent type
824 N := ASN;
825 while Present (N) loop
826 if Nkind (N) = N_Aspect_Specification then
827 exit when Entity (N) /= P;
829 if Is_Delayed_Aspect (N) then
830 A := Get_Aspect_Id (Chars (Identifier (N)));
832 -- Process delayed rep aspect. For Boolean attributes it is
833 -- not possible to cancel an attribute once set (the attempt
834 -- to use an aspect with xxx => False is an error) for a
835 -- derived type. So for those cases, we do not have to check
836 -- if a clause has been given for the derived type, since it
837 -- is harmless to set it again if it is already set.
839 case A is
841 -- Alignment
843 when Aspect_Alignment =>
844 if not Has_Alignment_Clause (E) then
845 Set_Alignment (E, Alignment (P));
846 end if;
848 -- Atomic
850 when Aspect_Atomic =>
851 if Is_Atomic (P) then
852 Set_Is_Atomic (E);
853 end if;
855 -- Atomic_Components
857 when Aspect_Atomic_Components =>
858 if Has_Atomic_Components (P) then
859 Set_Has_Atomic_Components (Base_Type (E));
860 end if;
862 -- Bit_Order
864 when Aspect_Bit_Order =>
865 if Is_Record_Type (E)
866 and then No (Get_Attribute_Definition_Clause
867 (E, Attribute_Bit_Order))
868 and then Reverse_Bit_Order (P)
869 then
870 Set_Reverse_Bit_Order (Base_Type (E));
871 end if;
873 -- Component_Size
875 when Aspect_Component_Size =>
876 if Is_Array_Type (E)
877 and then not Has_Component_Size_Clause (E)
878 then
879 Set_Component_Size
880 (Base_Type (E), Component_Size (P));
881 end if;
883 -- Machine_Radix
885 when Aspect_Machine_Radix =>
886 if Is_Decimal_Fixed_Point_Type (E)
887 and then not Has_Machine_Radix_Clause (E)
888 then
889 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
890 end if;
892 -- Object_Size (also Size which also sets Object_Size)
894 when Aspect_Object_Size | Aspect_Size =>
895 if not Has_Size_Clause (E)
896 and then
897 No (Get_Attribute_Definition_Clause
898 (E, Attribute_Object_Size))
899 then
900 Set_Esize (E, Esize (P));
901 end if;
903 -- Pack
905 when Aspect_Pack =>
906 if not Is_Packed (E) then
907 Set_Is_Packed (Base_Type (E));
909 if Is_Bit_Packed_Array (P) then
910 Set_Is_Bit_Packed_Array (Base_Type (E));
911 Set_Packed_Array_Impl_Type
912 (E, Packed_Array_Impl_Type (P));
913 end if;
914 end if;
916 -- Scalar_Storage_Order
918 when Aspect_Scalar_Storage_Order =>
919 if (Is_Record_Type (E) or else Is_Array_Type (E))
920 and then No (Get_Attribute_Definition_Clause
921 (E, Attribute_Scalar_Storage_Order))
922 and then Reverse_Storage_Order (P)
923 then
924 Set_Reverse_Storage_Order (Base_Type (E));
926 -- Clear default SSO indications, since the aspect
927 -- overrides the default.
929 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
930 Set_SSO_Set_High_By_Default (Base_Type (E), False);
931 end if;
933 -- Small
935 when Aspect_Small =>
936 if Is_Fixed_Point_Type (E)
937 and then not Has_Small_Clause (E)
938 then
939 Set_Small_Value (E, Small_Value (P));
940 end if;
942 -- Storage_Size
944 when Aspect_Storage_Size =>
945 if (Is_Access_Type (E) or else Is_Task_Type (E))
946 and then not Has_Storage_Size_Clause (E)
947 then
948 Set_Storage_Size_Variable
949 (Base_Type (E), Storage_Size_Variable (P));
950 end if;
952 -- Value_Size
954 when Aspect_Value_Size =>
956 -- Value_Size is never inherited, it is either set by
957 -- default, or it is explicitly set for the derived
958 -- type. So nothing to do here.
960 null;
962 -- Volatile
964 when Aspect_Volatile =>
965 if Is_Volatile (P) then
966 Set_Is_Volatile (E);
967 end if;
969 -- Volatile_Full_Access
971 when Aspect_Volatile_Full_Access =>
972 if Is_Volatile_Full_Access (P) then
973 Set_Is_Volatile_Full_Access (E);
974 end if;
976 -- Volatile_Components
978 when Aspect_Volatile_Components =>
979 if Has_Volatile_Components (P) then
980 Set_Has_Volatile_Components (Base_Type (E));
981 end if;
983 -- That should be all the Rep Aspects
985 when others =>
986 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
987 null;
989 end case;
990 end if;
991 end if;
993 N := Next_Rep_Item (N);
994 end loop;
995 end Inherit_Delayed_Rep_Aspects;
997 -------------------------------------
998 -- Make_Pragma_From_Boolean_Aspect --
999 -------------------------------------
1001 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1002 Ident : constant Node_Id := Identifier (ASN);
1003 A_Name : constant Name_Id := Chars (Ident);
1004 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1005 Ent : constant Entity_Id := Entity (ASN);
1006 Expr : constant Node_Id := Expression (ASN);
1007 Loc : constant Source_Ptr := Sloc (ASN);
1009 Prag : Node_Id;
1011 procedure Check_False_Aspect_For_Derived_Type;
1012 -- This procedure checks for the case of a false aspect for a derived
1013 -- type, which improperly tries to cancel an aspect inherited from
1014 -- the parent.
1016 -----------------------------------------
1017 -- Check_False_Aspect_For_Derived_Type --
1018 -----------------------------------------
1020 procedure Check_False_Aspect_For_Derived_Type is
1021 Par : Node_Id;
1023 begin
1024 -- We are only checking derived types
1026 if not Is_Derived_Type (E) then
1027 return;
1028 end if;
1030 Par := Nearest_Ancestor (E);
1032 case A_Id is
1033 when Aspect_Atomic | Aspect_Shared =>
1034 if not Is_Atomic (Par) then
1035 return;
1036 end if;
1038 when Aspect_Atomic_Components =>
1039 if not Has_Atomic_Components (Par) then
1040 return;
1041 end if;
1043 when Aspect_Discard_Names =>
1044 if not Discard_Names (Par) then
1045 return;
1046 end if;
1048 when Aspect_Pack =>
1049 if not Is_Packed (Par) then
1050 return;
1051 end if;
1053 when Aspect_Unchecked_Union =>
1054 if not Is_Unchecked_Union (Par) then
1055 return;
1056 end if;
1058 when Aspect_Volatile =>
1059 if not Is_Volatile (Par) then
1060 return;
1061 end if;
1063 when Aspect_Volatile_Components =>
1064 if not Has_Volatile_Components (Par) then
1065 return;
1066 end if;
1068 when Aspect_Volatile_Full_Access =>
1069 if not Is_Volatile_Full_Access (Par) then
1070 return;
1071 end if;
1073 when others =>
1074 return;
1075 end case;
1077 -- Fall through means we are canceling an inherited aspect
1079 Error_Msg_Name_1 := A_Name;
1080 Error_Msg_NE
1081 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1082 end Check_False_Aspect_For_Derived_Type;
1084 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1086 begin
1087 -- Note that we know Expr is present, because for a missing Expr
1088 -- argument, we knew it was True and did not need to delay the
1089 -- evaluation to the freeze point.
1091 if Is_False (Static_Boolean (Expr)) then
1092 Check_False_Aspect_For_Derived_Type;
1094 else
1095 Prag :=
1096 Make_Pragma (Loc,
1097 Pragma_Argument_Associations => New_List (
1098 Make_Pragma_Argument_Association (Sloc (Ident),
1099 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1101 Pragma_Identifier =>
1102 Make_Identifier (Sloc (Ident), Chars (Ident)));
1104 Set_From_Aspect_Specification (Prag, True);
1105 Set_Corresponding_Aspect (Prag, ASN);
1106 Set_Aspect_Rep_Item (ASN, Prag);
1107 Set_Is_Delayed_Aspect (Prag);
1108 Set_Parent (Prag, ASN);
1109 end if;
1110 end Make_Pragma_From_Boolean_Aspect;
1112 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1114 begin
1115 -- Must be visible in current scope
1117 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1118 return;
1119 end if;
1121 -- Look for aspect specification entries for this entity
1123 ASN := First_Rep_Item (E);
1124 while Present (ASN) loop
1125 if Nkind (ASN) = N_Aspect_Specification then
1126 exit when Entity (ASN) /= E;
1128 if Is_Delayed_Aspect (ASN) then
1129 A_Id := Get_Aspect_Id (ASN);
1131 case A_Id is
1133 -- For aspects whose expression is an optional Boolean, make
1134 -- the corresponding pragma at the freeze point.
1136 when Boolean_Aspects |
1137 Library_Unit_Aspects =>
1138 Make_Pragma_From_Boolean_Aspect (ASN);
1140 -- Special handling for aspects that don't correspond to
1141 -- pragmas/attributes.
1143 when Aspect_Default_Value |
1144 Aspect_Default_Component_Value =>
1146 -- Do not inherit aspect for anonymous base type of a
1147 -- scalar or array type, because they apply to the first
1148 -- subtype of the type, and will be processed when that
1149 -- first subtype is frozen.
1151 if Is_Derived_Type (E)
1152 and then not Comes_From_Source (E)
1153 and then E /= First_Subtype (E)
1154 then
1155 null;
1156 else
1157 Analyze_Aspect_Default_Value (ASN);
1158 end if;
1160 -- Ditto for iterator aspects, because the corresponding
1161 -- attributes may not have been analyzed yet.
1163 when Aspect_Constant_Indexing |
1164 Aspect_Variable_Indexing |
1165 Aspect_Default_Iterator |
1166 Aspect_Iterator_Element =>
1167 Analyze (Expression (ASN));
1169 if Etype (Expression (ASN)) = Any_Type then
1170 Error_Msg_NE
1171 ("\aspect must be fully defined before & is frozen",
1172 ASN, E);
1173 end if;
1175 when Aspect_Iterable =>
1176 Validate_Iterable_Aspect (E, ASN);
1178 when others =>
1179 null;
1180 end case;
1182 Ritem := Aspect_Rep_Item (ASN);
1184 if Present (Ritem) then
1185 Analyze (Ritem);
1186 end if;
1187 end if;
1188 end if;
1190 Next_Rep_Item (ASN);
1191 end loop;
1193 -- This is where we inherit delayed rep aspects from our parent. Note
1194 -- that if we fell out of the above loop with ASN non-empty, it means
1195 -- we hit an aspect for an entity other than E, and it must be the
1196 -- type from which we were derived.
1198 if May_Inherit_Delayed_Rep_Aspects (E) then
1199 Inherit_Delayed_Rep_Aspects (ASN);
1200 end if;
1201 end Analyze_Aspects_At_Freeze_Point;
1203 -----------------------------------
1204 -- Analyze_Aspect_Specifications --
1205 -----------------------------------
1207 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1208 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1209 -- Establish linkages between an aspect and its corresponding pragma
1211 procedure Insert_After_SPARK_Mode
1212 (Prag : Node_Id;
1213 Ins_Nod : Node_Id;
1214 Decls : List_Id);
1215 -- Subsidiary to the analysis of aspects Abstract_State, Ghost,
1216 -- Initializes, Initial_Condition and Refined_State. Insert node Prag
1217 -- before node Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip
1218 -- SPARK_Mode. Decls is the associated declarative list where Prag is to
1219 -- reside.
1221 procedure Insert_Pragma (Prag : Node_Id);
1222 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1223 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1224 -- Insert pragma Prag such that it mimics the placement of a source
1225 -- pragma of the same kind.
1227 -- procedure Proc (Formal : ...) with Global => ...;
1229 -- procedure Proc (Formal : ...);
1230 -- pragma Global (...);
1232 --------------
1233 -- Decorate --
1234 --------------
1236 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1237 begin
1238 Set_Aspect_Rep_Item (Asp, Prag);
1239 Set_Corresponding_Aspect (Prag, Asp);
1240 Set_From_Aspect_Specification (Prag);
1241 Set_Parent (Prag, Asp);
1242 end Decorate;
1244 -----------------------------
1245 -- Insert_After_SPARK_Mode --
1246 -----------------------------
1248 procedure Insert_After_SPARK_Mode
1249 (Prag : Node_Id;
1250 Ins_Nod : Node_Id;
1251 Decls : List_Id)
1253 Decl : Node_Id := Ins_Nod;
1255 begin
1256 -- Skip SPARK_Mode
1258 if Present (Decl)
1259 and then Nkind (Decl) = N_Pragma
1260 and then Pragma_Name (Decl) = Name_SPARK_Mode
1261 then
1262 Decl := Next (Decl);
1263 end if;
1265 if Present (Decl) then
1266 Insert_Before (Decl, Prag);
1268 -- Aitem acts as the last declaration
1270 else
1271 Append_To (Decls, Prag);
1272 end if;
1273 end Insert_After_SPARK_Mode;
1275 -------------------
1276 -- Insert_Pragma --
1277 -------------------
1279 procedure Insert_Pragma (Prag : Node_Id) is
1280 Aux : Node_Id;
1281 Decl : Node_Id;
1283 begin
1284 if Nkind (N) = N_Subprogram_Body then
1285 if Present (Declarations (N)) then
1287 -- Skip other internally generated pragmas from aspects to find
1288 -- the proper insertion point. As a result the order of pragmas
1289 -- is the same as the order of aspects.
1291 -- As precondition pragmas generated from conjuncts in the
1292 -- precondition aspect are presented in reverse order to
1293 -- Insert_Pragma, insert them in the correct order here by not
1294 -- skipping previously inserted precondition pragmas when the
1295 -- current pragma is a precondition.
1297 Decl := First (Declarations (N));
1298 while Present (Decl) loop
1299 if Nkind (Decl) = N_Pragma
1300 and then From_Aspect_Specification (Decl)
1301 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1302 and then
1303 Get_Pragma_Id (Prag) = Pragma_Precondition)
1304 then
1305 Next (Decl);
1306 else
1307 exit;
1308 end if;
1309 end loop;
1311 if Present (Decl) then
1312 Insert_Before (Decl, Prag);
1313 else
1314 Append (Prag, Declarations (N));
1315 end if;
1316 else
1317 Set_Declarations (N, New_List (Prag));
1318 end if;
1320 -- When the context is a library unit, the pragma is added to the
1321 -- Pragmas_After list.
1323 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1324 Aux := Aux_Decls_Node (Parent (N));
1326 if No (Pragmas_After (Aux)) then
1327 Set_Pragmas_After (Aux, New_List);
1328 end if;
1330 Prepend (Prag, Pragmas_After (Aux));
1332 -- Default
1334 else
1335 Insert_After (N, Prag);
1336 end if;
1337 end Insert_Pragma;
1339 -- Local variables
1341 Aspect : Node_Id;
1342 Aitem : Node_Id;
1343 Ent : Node_Id;
1345 L : constant List_Id := Aspect_Specifications (N);
1347 Ins_Node : Node_Id := N;
1348 -- Insert pragmas/attribute definition clause after this node when no
1349 -- delayed analysis is required.
1351 -- Start of processing for Analyze_Aspect_Specifications
1353 -- The general processing involves building an attribute definition
1354 -- clause or a pragma node that corresponds to the aspect. Then in order
1355 -- to delay the evaluation of this aspect to the freeze point, we attach
1356 -- the corresponding pragma/attribute definition clause to the aspect
1357 -- specification node, which is then placed in the Rep Item chain. In
1358 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1359 -- and we evaluate the rep item at the freeze point. When the aspect
1360 -- doesn't have a corresponding pragma/attribute definition clause, then
1361 -- its analysis is simply delayed at the freeze point.
1363 -- Some special cases don't require delay analysis, thus the aspect is
1364 -- analyzed right now.
1366 -- Note that there is a special handling for Pre, Post, Test_Case,
1367 -- Contract_Cases aspects. In these cases, we do not have to worry
1368 -- about delay issues, since the pragmas themselves deal with delay
1369 -- of visibility for the expression analysis. Thus, we just insert
1370 -- the pragma after the node N.
1372 begin
1373 pragma Assert (Present (L));
1375 -- Loop through aspects
1377 Aspect := First (L);
1378 Aspect_Loop : while Present (Aspect) loop
1379 Analyze_One_Aspect : declare
1380 Expr : constant Node_Id := Expression (Aspect);
1381 Id : constant Node_Id := Identifier (Aspect);
1382 Loc : constant Source_Ptr := Sloc (Aspect);
1383 Nam : constant Name_Id := Chars (Id);
1384 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1385 Anod : Node_Id;
1387 Delay_Required : Boolean;
1388 -- Set False if delay is not required
1390 Eloc : Source_Ptr := No_Location;
1391 -- Source location of expression, modified when we split PPC's. It
1392 -- is set below when Expr is present.
1394 procedure Analyze_Aspect_External_Or_Link_Name;
1395 -- Perform analysis of the External_Name or Link_Name aspects
1397 procedure Analyze_Aspect_Implicit_Dereference;
1398 -- Perform analysis of the Implicit_Dereference aspects
1400 procedure Make_Aitem_Pragma
1401 (Pragma_Argument_Associations : List_Id;
1402 Pragma_Name : Name_Id);
1403 -- This is a wrapper for Make_Pragma used for converting aspects
1404 -- to pragmas. It takes care of Sloc (set from Loc) and building
1405 -- the pragma identifier from the given name. In addition the
1406 -- flags Class_Present and Split_PPC are set from the aspect
1407 -- node, as well as Is_Ignored. This routine also sets the
1408 -- From_Aspect_Specification in the resulting pragma node to
1409 -- True, and sets Corresponding_Aspect to point to the aspect.
1410 -- The resulting pragma is assigned to Aitem.
1412 ------------------------------------------
1413 -- Analyze_Aspect_External_Or_Link_Name --
1414 ------------------------------------------
1416 procedure Analyze_Aspect_External_Or_Link_Name is
1417 begin
1418 -- Verify that there is an Import/Export aspect defined for the
1419 -- entity. The processing of that aspect in turn checks that
1420 -- there is a Convention aspect declared. The pragma is
1421 -- constructed when processing the Convention aspect.
1423 declare
1424 A : Node_Id;
1426 begin
1427 A := First (L);
1428 while Present (A) loop
1429 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1430 Name_Import);
1431 Next (A);
1432 end loop;
1434 if No (A) then
1435 Error_Msg_N
1436 ("missing Import/Export for Link/External name",
1437 Aspect);
1438 end if;
1439 end;
1440 end Analyze_Aspect_External_Or_Link_Name;
1442 -----------------------------------------
1443 -- Analyze_Aspect_Implicit_Dereference --
1444 -----------------------------------------
1446 procedure Analyze_Aspect_Implicit_Dereference is
1447 begin
1448 if not Is_Type (E) or else not Has_Discriminants (E) then
1449 Error_Msg_N
1450 ("aspect must apply to a type with discriminants", N);
1452 else
1453 declare
1454 Disc : Entity_Id;
1456 begin
1457 Disc := First_Discriminant (E);
1458 while Present (Disc) loop
1459 if Chars (Expr) = Chars (Disc)
1460 and then Ekind (Etype (Disc)) =
1461 E_Anonymous_Access_Type
1462 then
1463 Set_Has_Implicit_Dereference (E);
1464 Set_Has_Implicit_Dereference (Disc);
1465 return;
1466 end if;
1468 Next_Discriminant (Disc);
1469 end loop;
1471 -- Error if no proper access discriminant.
1473 Error_Msg_NE
1474 ("not an access discriminant of&", Expr, E);
1475 end;
1476 end if;
1477 end Analyze_Aspect_Implicit_Dereference;
1479 -----------------------
1480 -- Make_Aitem_Pragma --
1481 -----------------------
1483 procedure Make_Aitem_Pragma
1484 (Pragma_Argument_Associations : List_Id;
1485 Pragma_Name : Name_Id)
1487 Args : List_Id := Pragma_Argument_Associations;
1489 begin
1490 -- We should never get here if aspect was disabled
1492 pragma Assert (not Is_Disabled (Aspect));
1494 -- Certain aspects allow for an optional name or expression. Do
1495 -- not generate a pragma with empty argument association list.
1497 if No (Args) or else No (Expression (First (Args))) then
1498 Args := No_List;
1499 end if;
1501 -- Build the pragma
1503 Aitem :=
1504 Make_Pragma (Loc,
1505 Pragma_Argument_Associations => Args,
1506 Pragma_Identifier =>
1507 Make_Identifier (Sloc (Id), Pragma_Name),
1508 Class_Present => Class_Present (Aspect),
1509 Split_PPC => Split_PPC (Aspect));
1511 -- Set additional semantic fields
1513 if Is_Ignored (Aspect) then
1514 Set_Is_Ignored (Aitem);
1515 elsif Is_Checked (Aspect) then
1516 Set_Is_Checked (Aitem);
1517 end if;
1519 Set_Corresponding_Aspect (Aitem, Aspect);
1520 Set_From_Aspect_Specification (Aitem, True);
1521 end Make_Aitem_Pragma;
1523 -- Start of processing for Analyze_One_Aspect
1525 begin
1526 -- Skip aspect if already analyzed, to avoid looping in some cases
1528 if Analyzed (Aspect) then
1529 goto Continue;
1530 end if;
1532 -- Skip looking at aspect if it is totally disabled. Just mark it
1533 -- as such for later reference in the tree. This also sets the
1534 -- Is_Ignored and Is_Checked flags appropriately.
1536 Check_Applicable_Policy (Aspect);
1538 if Is_Disabled (Aspect) then
1539 goto Continue;
1540 end if;
1542 -- Set the source location of expression, used in the case of
1543 -- a failed precondition/postcondition or invariant. Note that
1544 -- the source location of the expression is not usually the best
1545 -- choice here. For example, it gets located on the last AND
1546 -- keyword in a chain of boolean expressiond AND'ed together.
1547 -- It is best to put the message on the first character of the
1548 -- assertion, which is the effect of the First_Node call here.
1550 if Present (Expr) then
1551 Eloc := Sloc (First_Node (Expr));
1552 end if;
1554 -- Check restriction No_Implementation_Aspect_Specifications
1556 if Implementation_Defined_Aspect (A_Id) then
1557 Check_Restriction
1558 (No_Implementation_Aspect_Specifications, Aspect);
1559 end if;
1561 -- Check restriction No_Specification_Of_Aspect
1563 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1565 -- Mark aspect analyzed (actual analysis is delayed till later)
1567 Set_Analyzed (Aspect);
1568 Set_Entity (Aspect, E);
1569 Ent := New_Occurrence_Of (E, Sloc (Id));
1571 -- Check for duplicate aspect. Note that the Comes_From_Source
1572 -- test allows duplicate Pre/Post's that we generate internally
1573 -- to escape being flagged here.
1575 if No_Duplicates_Allowed (A_Id) then
1576 Anod := First (L);
1577 while Anod /= Aspect loop
1578 if Comes_From_Source (Aspect)
1579 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1580 then
1581 Error_Msg_Name_1 := Nam;
1582 Error_Msg_Sloc := Sloc (Anod);
1584 -- Case of same aspect specified twice
1586 if Class_Present (Anod) = Class_Present (Aspect) then
1587 if not Class_Present (Anod) then
1588 Error_Msg_NE
1589 ("aspect% for & previously given#",
1590 Id, E);
1591 else
1592 Error_Msg_NE
1593 ("aspect `%''Class` for & previously given#",
1594 Id, E);
1595 end if;
1596 end if;
1597 end if;
1599 Next (Anod);
1600 end loop;
1601 end if;
1603 -- Check some general restrictions on language defined aspects
1605 if not Implementation_Defined_Aspect (A_Id) then
1606 Error_Msg_Name_1 := Nam;
1608 -- Not allowed for renaming declarations
1610 if Nkind (N) in N_Renaming_Declaration then
1611 Error_Msg_N
1612 ("aspect % not allowed for renaming declaration",
1613 Aspect);
1614 end if;
1616 -- Not allowed for formal type declarations
1618 if Nkind (N) = N_Formal_Type_Declaration then
1619 Error_Msg_N
1620 ("aspect % not allowed for formal type declaration",
1621 Aspect);
1622 end if;
1623 end if;
1625 -- Copy expression for later processing by the procedures
1626 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1628 Set_Entity (Id, New_Copy_Tree (Expr));
1630 -- Set Delay_Required as appropriate to aspect
1632 case Aspect_Delay (A_Id) is
1633 when Always_Delay =>
1634 Delay_Required := True;
1636 when Never_Delay =>
1637 Delay_Required := False;
1639 when Rep_Aspect =>
1641 -- If expression has the form of an integer literal, then
1642 -- do not delay, since we know the value cannot change.
1643 -- This optimization catches most rep clause cases.
1645 -- For Boolean aspects, don't delay if no expression
1647 if A_Id in Boolean_Aspects and then No (Expr) then
1648 Delay_Required := False;
1650 -- For non-Boolean aspects, don't delay if integer literal
1652 elsif A_Id not in Boolean_Aspects
1653 and then Present (Expr)
1654 and then Nkind (Expr) = N_Integer_Literal
1655 then
1656 Delay_Required := False;
1658 -- All other cases are delayed
1660 else
1661 Delay_Required := True;
1662 Set_Has_Delayed_Rep_Aspects (E);
1663 end if;
1664 end case;
1666 -- Processing based on specific aspect
1668 case A_Id is
1669 when Aspect_Unimplemented =>
1670 null; -- ??? temp for now
1672 -- No_Aspect should be impossible
1674 when No_Aspect =>
1675 raise Program_Error;
1677 -- Case 1: Aspects corresponding to attribute definition
1678 -- clauses.
1680 when Aspect_Address |
1681 Aspect_Alignment |
1682 Aspect_Bit_Order |
1683 Aspect_Component_Size |
1684 Aspect_Constant_Indexing |
1685 Aspect_Default_Iterator |
1686 Aspect_Dispatching_Domain |
1687 Aspect_External_Tag |
1688 Aspect_Input |
1689 Aspect_Iterable |
1690 Aspect_Iterator_Element |
1691 Aspect_Machine_Radix |
1692 Aspect_Object_Size |
1693 Aspect_Output |
1694 Aspect_Read |
1695 Aspect_Scalar_Storage_Order |
1696 Aspect_Size |
1697 Aspect_Small |
1698 Aspect_Simple_Storage_Pool |
1699 Aspect_Storage_Pool |
1700 Aspect_Stream_Size |
1701 Aspect_Value_Size |
1702 Aspect_Variable_Indexing |
1703 Aspect_Write =>
1705 -- Indexing aspects apply only to tagged type
1707 if (A_Id = Aspect_Constant_Indexing
1708 or else
1709 A_Id = Aspect_Variable_Indexing)
1710 and then not (Is_Type (E)
1711 and then Is_Tagged_Type (E))
1712 then
1713 Error_Msg_N
1714 ("indexing aspect can only apply to a tagged type",
1715 Aspect);
1716 goto Continue;
1717 end if;
1719 -- For the case of aspect Address, we don't consider that we
1720 -- know the entity is never set in the source, since it is
1721 -- is likely aliasing is occurring.
1723 -- Note: one might think that the analysis of the resulting
1724 -- attribute definition clause would take care of that, but
1725 -- that's not the case since it won't be from source.
1727 if A_Id = Aspect_Address then
1728 Set_Never_Set_In_Source (E, False);
1729 end if;
1731 -- Correctness of the profile of a stream operation is
1732 -- verified at the freeze point, but we must detect the
1733 -- illegal specification of this aspect for a subtype now,
1734 -- to prevent malformed rep_item chains.
1736 if A_Id = Aspect_Input or else
1737 A_Id = Aspect_Output or else
1738 A_Id = Aspect_Read or else
1739 A_Id = Aspect_Write
1740 then
1741 if not Is_First_Subtype (E) then
1742 Error_Msg_N
1743 ("local name must be a first subtype", Aspect);
1744 goto Continue;
1746 -- If stream aspect applies to the class-wide type,
1747 -- the generated attribute definition applies to the
1748 -- class-wide type as well.
1750 elsif Class_Present (Aspect) then
1751 Ent :=
1752 Make_Attribute_Reference (Loc,
1753 Prefix => Ent,
1754 Attribute_Name => Name_Class);
1755 end if;
1756 end if;
1758 -- Construct the attribute definition clause
1760 Aitem :=
1761 Make_Attribute_Definition_Clause (Loc,
1762 Name => Ent,
1763 Chars => Chars (Id),
1764 Expression => Relocate_Node (Expr));
1766 -- If the address is specified, then we treat the entity as
1767 -- referenced, to avoid spurious warnings. This is analogous
1768 -- to what is done with an attribute definition clause, but
1769 -- here we don't want to generate a reference because this
1770 -- is the point of definition of the entity.
1772 if A_Id = Aspect_Address then
1773 Set_Referenced (E);
1774 end if;
1776 -- Case 2: Aspects corresponding to pragmas
1778 -- Case 2a: Aspects corresponding to pragmas with two
1779 -- arguments, where the first argument is a local name
1780 -- referring to the entity, and the second argument is the
1781 -- aspect definition expression.
1783 -- Linker_Section/Suppress/Unsuppress
1785 when Aspect_Linker_Section |
1786 Aspect_Suppress |
1787 Aspect_Unsuppress =>
1789 Make_Aitem_Pragma
1790 (Pragma_Argument_Associations => New_List (
1791 Make_Pragma_Argument_Association (Loc,
1792 Expression => New_Occurrence_Of (E, Loc)),
1793 Make_Pragma_Argument_Association (Sloc (Expr),
1794 Expression => Relocate_Node (Expr))),
1795 Pragma_Name => Chars (Id));
1797 -- Synchronization
1799 -- Corresponds to pragma Implemented, construct the pragma
1801 when Aspect_Synchronization =>
1802 Make_Aitem_Pragma
1803 (Pragma_Argument_Associations => New_List (
1804 Make_Pragma_Argument_Association (Loc,
1805 Expression => New_Occurrence_Of (E, Loc)),
1806 Make_Pragma_Argument_Association (Sloc (Expr),
1807 Expression => Relocate_Node (Expr))),
1808 Pragma_Name => Name_Implemented);
1810 -- Attach_Handler
1812 when Aspect_Attach_Handler =>
1813 Make_Aitem_Pragma
1814 (Pragma_Argument_Associations => New_List (
1815 Make_Pragma_Argument_Association (Sloc (Ent),
1816 Expression => Ent),
1817 Make_Pragma_Argument_Association (Sloc (Expr),
1818 Expression => Relocate_Node (Expr))),
1819 Pragma_Name => Name_Attach_Handler);
1821 -- We need to insert this pragma into the tree to get proper
1822 -- processing and to look valid from a placement viewpoint.
1824 Insert_Pragma (Aitem);
1825 goto Continue;
1827 -- Dynamic_Predicate, Predicate, Static_Predicate
1829 when Aspect_Dynamic_Predicate |
1830 Aspect_Predicate |
1831 Aspect_Static_Predicate =>
1833 -- These aspects apply only to subtypes
1835 if not Is_Type (E) then
1836 Error_Msg_N
1837 ("predicate can only be specified for a subtype",
1838 Aspect);
1839 goto Continue;
1841 elsif Is_Incomplete_Type (E) then
1842 Error_Msg_N
1843 ("predicate cannot apply to incomplete view", Aspect);
1844 goto Continue;
1845 end if;
1847 -- Construct the pragma (always a pragma Predicate, with
1848 -- flags recording whether it is static/dynamic). We also
1849 -- set flags recording this in the type itself.
1851 Make_Aitem_Pragma
1852 (Pragma_Argument_Associations => New_List (
1853 Make_Pragma_Argument_Association (Sloc (Ent),
1854 Expression => Ent),
1855 Make_Pragma_Argument_Association (Sloc (Expr),
1856 Expression => Relocate_Node (Expr))),
1857 Pragma_Name => Name_Predicate);
1859 -- Mark type has predicates, and remember what kind of
1860 -- aspect lead to this predicate (we need this to access
1861 -- the right set of check policies later on).
1863 Set_Has_Predicates (E);
1865 if A_Id = Aspect_Dynamic_Predicate then
1866 Set_Has_Dynamic_Predicate_Aspect (E);
1867 elsif A_Id = Aspect_Static_Predicate then
1868 Set_Has_Static_Predicate_Aspect (E);
1869 end if;
1871 -- If the type is private, indicate that its completion
1872 -- has a freeze node, because that is the one that will
1873 -- be visible at freeze time.
1875 if Is_Private_Type (E) and then Present (Full_View (E)) then
1876 Set_Has_Predicates (Full_View (E));
1878 if A_Id = Aspect_Dynamic_Predicate then
1879 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1880 elsif A_Id = Aspect_Static_Predicate then
1881 Set_Has_Static_Predicate_Aspect (Full_View (E));
1882 end if;
1884 Set_Has_Delayed_Aspects (Full_View (E));
1885 Ensure_Freeze_Node (Full_View (E));
1886 end if;
1888 -- Case 2b: Aspects corresponding to pragmas with two
1889 -- arguments, where the second argument is a local name
1890 -- referring to the entity, and the first argument is the
1891 -- aspect definition expression.
1893 -- Convention
1895 when Aspect_Convention =>
1897 -- The aspect may be part of the specification of an import
1898 -- or export pragma. Scan the aspect list to gather the
1899 -- other components, if any. The name of the generated
1900 -- pragma is one of Convention/Import/Export.
1902 declare
1903 Args : constant List_Id := New_List (
1904 Make_Pragma_Argument_Association (Sloc (Expr),
1905 Expression => Relocate_Node (Expr)),
1906 Make_Pragma_Argument_Association (Sloc (Ent),
1907 Expression => Ent));
1909 Imp_Exp_Seen : Boolean := False;
1910 -- Flag set when aspect Import or Export has been seen
1912 Imp_Seen : Boolean := False;
1913 -- Flag set when aspect Import has been seen
1915 Asp : Node_Id;
1916 Asp_Nam : Name_Id;
1917 Extern_Arg : Node_Id;
1918 Link_Arg : Node_Id;
1919 Prag_Nam : Name_Id;
1921 begin
1922 Extern_Arg := Empty;
1923 Link_Arg := Empty;
1924 Prag_Nam := Chars (Id);
1926 Asp := First (L);
1927 while Present (Asp) loop
1928 Asp_Nam := Chars (Identifier (Asp));
1930 -- Aspects Import and Export take precedence over
1931 -- aspect Convention. As a result the generated pragma
1932 -- must carry the proper interfacing aspect's name.
1934 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1935 if Imp_Exp_Seen then
1936 Error_Msg_N ("conflicting", Asp);
1937 else
1938 Imp_Exp_Seen := True;
1940 if Asp_Nam = Name_Import then
1941 Imp_Seen := True;
1942 end if;
1943 end if;
1945 Prag_Nam := Asp_Nam;
1947 -- Aspect External_Name adds an extra argument to the
1948 -- generated pragma.
1950 elsif Asp_Nam = Name_External_Name then
1951 Extern_Arg :=
1952 Make_Pragma_Argument_Association (Loc,
1953 Chars => Asp_Nam,
1954 Expression => Relocate_Node (Expression (Asp)));
1956 -- Aspect Link_Name adds an extra argument to the
1957 -- generated pragma.
1959 elsif Asp_Nam = Name_Link_Name then
1960 Link_Arg :=
1961 Make_Pragma_Argument_Association (Loc,
1962 Chars => Asp_Nam,
1963 Expression => Relocate_Node (Expression (Asp)));
1964 end if;
1966 Next (Asp);
1967 end loop;
1969 -- Assemble the full argument list
1971 if Present (Extern_Arg) then
1972 Append_To (Args, Extern_Arg);
1973 end if;
1975 if Present (Link_Arg) then
1976 Append_To (Args, Link_Arg);
1977 end if;
1979 Make_Aitem_Pragma
1980 (Pragma_Argument_Associations => Args,
1981 Pragma_Name => Prag_Nam);
1983 -- Store the generated pragma Import in the related
1984 -- subprogram.
1986 if Imp_Seen and then Is_Subprogram (E) then
1987 Set_Import_Pragma (E, Aitem);
1988 end if;
1989 end;
1991 -- CPU, Interrupt_Priority, Priority
1993 -- These three aspects can be specified for a subprogram spec
1994 -- or body, in which case we analyze the expression and export
1995 -- the value of the aspect.
1997 -- Previously, we generated an equivalent pragma for bodies
1998 -- (note that the specs cannot contain these pragmas). The
1999 -- pragma was inserted ahead of local declarations, rather than
2000 -- after the body. This leads to a certain duplication between
2001 -- the processing performed for the aspect and the pragma, but
2002 -- given the straightforward handling required it is simpler
2003 -- to duplicate than to translate the aspect in the spec into
2004 -- a pragma in the declarative part of the body.
2006 when Aspect_CPU |
2007 Aspect_Interrupt_Priority |
2008 Aspect_Priority =>
2010 if Nkind_In (N, N_Subprogram_Body,
2011 N_Subprogram_Declaration)
2012 then
2013 -- Analyze the aspect expression
2015 Analyze_And_Resolve (Expr, Standard_Integer);
2017 -- Interrupt_Priority aspect not allowed for main
2018 -- subprograms. ARM D.1 does not forbid this explicitly,
2019 -- but ARM J.15.11 (6/3) does not permit pragma
2020 -- Interrupt_Priority for subprograms.
2022 if A_Id = Aspect_Interrupt_Priority then
2023 Error_Msg_N
2024 ("Interrupt_Priority aspect cannot apply to "
2025 & "subprogram", Expr);
2027 -- The expression must be static
2029 elsif not Is_OK_Static_Expression (Expr) then
2030 Flag_Non_Static_Expr
2031 ("aspect requires static expression!", Expr);
2033 -- Check whether this is the main subprogram. Issue a
2034 -- warning only if it is obviously not a main program
2035 -- (when it has parameters or when the subprogram is
2036 -- within a package).
2038 elsif Present (Parameter_Specifications
2039 (Specification (N)))
2040 or else not Is_Compilation_Unit (Defining_Entity (N))
2041 then
2042 -- See ARM D.1 (14/3) and D.16 (12/3)
2044 Error_Msg_N
2045 ("aspect applied to subprogram other than the "
2046 & "main subprogram has no effect??", Expr);
2048 -- Otherwise check in range and export the value
2050 -- For the CPU aspect
2052 elsif A_Id = Aspect_CPU then
2053 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2055 -- Value is correct so we export the value to make
2056 -- it available at execution time.
2058 Set_Main_CPU
2059 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2061 else
2062 Error_Msg_N
2063 ("main subprogram CPU is out of range", Expr);
2064 end if;
2066 -- For the Priority aspect
2068 elsif A_Id = Aspect_Priority then
2069 if Is_In_Range (Expr, RTE (RE_Priority)) then
2071 -- Value is correct so we export the value to make
2072 -- it available at execution time.
2074 Set_Main_Priority
2075 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2077 -- Ignore pragma if Relaxed_RM_Semantics to support
2078 -- other targets/non GNAT compilers.
2080 elsif not Relaxed_RM_Semantics then
2081 Error_Msg_N
2082 ("main subprogram priority is out of range",
2083 Expr);
2084 end if;
2085 end if;
2087 -- Load an arbitrary entity from System.Tasking.Stages
2088 -- or System.Tasking.Restricted.Stages (depending on
2089 -- the supported profile) to make sure that one of these
2090 -- packages is implicitly with'ed, since we need to have
2091 -- the tasking run time active for the pragma Priority to
2092 -- have any effect. Previously we with'ed the package
2093 -- System.Tasking, but this package does not trigger the
2094 -- required initialization of the run-time library.
2096 declare
2097 Discard : Entity_Id;
2098 begin
2099 if Restricted_Profile then
2100 Discard := RTE (RE_Activate_Restricted_Tasks);
2101 else
2102 Discard := RTE (RE_Activate_Tasks);
2103 end if;
2104 end;
2106 -- Handling for these Aspects in subprograms is complete
2108 goto Continue;
2110 -- For tasks
2112 else
2113 -- Pass the aspect as an attribute
2115 Aitem :=
2116 Make_Attribute_Definition_Clause (Loc,
2117 Name => Ent,
2118 Chars => Chars (Id),
2119 Expression => Relocate_Node (Expr));
2120 end if;
2122 -- Warnings
2124 when Aspect_Warnings =>
2125 Make_Aitem_Pragma
2126 (Pragma_Argument_Associations => New_List (
2127 Make_Pragma_Argument_Association (Sloc (Expr),
2128 Expression => Relocate_Node (Expr)),
2129 Make_Pragma_Argument_Association (Loc,
2130 Expression => New_Occurrence_Of (E, Loc))),
2131 Pragma_Name => Chars (Id));
2133 -- Case 2c: Aspects corresponding to pragmas with three
2134 -- arguments.
2136 -- Invariant aspects have a first argument that references the
2137 -- entity, a second argument that is the expression and a third
2138 -- argument that is an appropriate message.
2140 -- Invariant, Type_Invariant
2142 when Aspect_Invariant |
2143 Aspect_Type_Invariant =>
2145 -- Analysis of the pragma will verify placement legality:
2146 -- an invariant must apply to a private type, or appear in
2147 -- the private part of a spec and apply to a completion.
2149 Make_Aitem_Pragma
2150 (Pragma_Argument_Associations => New_List (
2151 Make_Pragma_Argument_Association (Sloc (Ent),
2152 Expression => Ent),
2153 Make_Pragma_Argument_Association (Sloc (Expr),
2154 Expression => Relocate_Node (Expr))),
2155 Pragma_Name => Name_Invariant);
2157 -- Add message unless exception messages are suppressed
2159 if not Opt.Exception_Locations_Suppressed then
2160 Append_To (Pragma_Argument_Associations (Aitem),
2161 Make_Pragma_Argument_Association (Eloc,
2162 Chars => Name_Message,
2163 Expression =>
2164 Make_String_Literal (Eloc,
2165 Strval => "failed invariant from "
2166 & Build_Location_String (Eloc))));
2167 end if;
2169 -- For Invariant case, insert immediately after the entity
2170 -- declaration. We do not have to worry about delay issues
2171 -- since the pragma processing takes care of this.
2173 Delay_Required := False;
2175 -- Case 2d : Aspects that correspond to a pragma with one
2176 -- argument.
2178 -- Abstract_State
2180 -- Aspect Abstract_State introduces implicit declarations for
2181 -- all state abstraction entities it defines. To emulate this
2182 -- behavior, insert the pragma at the beginning of the visible
2183 -- declarations of the related package so that it is analyzed
2184 -- immediately.
2186 when Aspect_Abstract_State => Abstract_State : declare
2187 Context : Node_Id := N;
2188 Decl : Node_Id;
2189 Decls : List_Id;
2191 begin
2192 -- When aspect Abstract_State appears on a generic package,
2193 -- it is propageted to the package instance. The context in
2194 -- this case is the instance spec.
2196 if Nkind (Context) = N_Package_Instantiation then
2197 Context := Instance_Spec (Context);
2198 end if;
2200 if Nkind_In (Context, N_Generic_Package_Declaration,
2201 N_Package_Declaration)
2202 then
2203 Make_Aitem_Pragma
2204 (Pragma_Argument_Associations => New_List (
2205 Make_Pragma_Argument_Association (Loc,
2206 Expression => Relocate_Node (Expr))),
2207 Pragma_Name => Name_Abstract_State);
2208 Decorate (Aspect, Aitem);
2210 Decls := Visible_Declarations (Specification (Context));
2212 -- In general pragma Abstract_State must be at the top
2213 -- of the existing visible declarations to emulate its
2214 -- source counterpart. The only exception to this is a
2215 -- generic instance in which case the pragma must be
2216 -- inserted after the association renamings.
2218 if Present (Decls) then
2219 Decl := First (Decls);
2221 -- The visible declarations of a generic instance have
2222 -- the following structure:
2224 -- <renamings of generic formals>
2225 -- <renamings of internally-generated spec and body>
2226 -- <first source declaration>
2228 -- The pragma must be inserted before the first source
2229 -- declaration, skip the instance "header".
2231 if Is_Generic_Instance (Defining_Entity (Context)) then
2232 while Present (Decl)
2233 and then not Comes_From_Source (Decl)
2234 loop
2235 Decl := Next (Decl);
2236 end loop;
2237 end if;
2239 -- When aspects Abstract_State, Ghost,
2240 -- Initial_Condition and Initializes are out of order,
2241 -- ensure that pragma SPARK_Mode is always at the top
2242 -- of the declarations to properly enabled/suppress
2243 -- errors.
2245 Insert_After_SPARK_Mode
2246 (Prag => Aitem,
2247 Ins_Nod => Decl,
2248 Decls => Decls);
2250 -- Otherwise the pragma forms a new declarative list
2252 else
2253 Set_Visible_Declarations
2254 (Specification (Context), New_List (Aitem));
2255 end if;
2257 else
2258 Error_Msg_NE
2259 ("aspect & must apply to a package declaration",
2260 Aspect, Id);
2261 end if;
2263 goto Continue;
2264 end Abstract_State;
2266 -- Aspect Default_Internal_Condition is never delayed because
2267 -- it is equivalent to a source pragma which appears after the
2268 -- related private type. To deal with forward references, the
2269 -- generated pragma is stored in the rep chain of the related
2270 -- private type as types do not carry contracts. The pragma is
2271 -- wrapped inside of a procedure at the freeze point of the
2272 -- private type's full view.
2274 when Aspect_Default_Initial_Condition =>
2275 Make_Aitem_Pragma
2276 (Pragma_Argument_Associations => New_List (
2277 Make_Pragma_Argument_Association (Loc,
2278 Expression => Relocate_Node (Expr))),
2279 Pragma_Name =>
2280 Name_Default_Initial_Condition);
2282 Decorate (Aspect, Aitem);
2283 Insert_Pragma (Aitem);
2284 goto Continue;
2286 -- Default_Storage_Pool
2288 when Aspect_Default_Storage_Pool =>
2289 Make_Aitem_Pragma
2290 (Pragma_Argument_Associations => New_List (
2291 Make_Pragma_Argument_Association (Loc,
2292 Expression => Relocate_Node (Expr))),
2293 Pragma_Name =>
2294 Name_Default_Storage_Pool);
2296 Decorate (Aspect, Aitem);
2297 Insert_Pragma (Aitem);
2298 goto Continue;
2300 -- Depends
2302 -- Aspect Depends is never delayed because it is equivalent to
2303 -- a source pragma which appears after the related subprogram.
2304 -- To deal with forward references, the generated pragma is
2305 -- stored in the contract of the related subprogram and later
2306 -- analyzed at the end of the declarative region. See routine
2307 -- Analyze_Depends_In_Decl_Part for details.
2309 when Aspect_Depends =>
2310 Make_Aitem_Pragma
2311 (Pragma_Argument_Associations => New_List (
2312 Make_Pragma_Argument_Association (Loc,
2313 Expression => Relocate_Node (Expr))),
2314 Pragma_Name => Name_Depends);
2316 Decorate (Aspect, Aitem);
2317 Insert_Pragma (Aitem);
2318 goto Continue;
2320 -- Aspect Extensions_Visible is never delayed because it is
2321 -- equivalent to a source pragma which appears after the
2322 -- related subprogram.
2324 when Aspect_Extensions_Visible =>
2325 Make_Aitem_Pragma
2326 (Pragma_Argument_Associations => New_List (
2327 Make_Pragma_Argument_Association (Loc,
2328 Expression => Relocate_Node (Expr))),
2329 Pragma_Name => Name_Extensions_Visible);
2331 Decorate (Aspect, Aitem);
2332 Insert_Pragma (Aitem);
2333 goto Continue;
2335 -- Aspect Ghost is never delayed because it is equivalent to a
2336 -- source pragma which appears at the top of [generic] package
2337 -- declarations or after an object, a [generic] subprogram, or
2338 -- a type declaration.
2340 when Aspect_Ghost => Ghost : declare
2341 Decls : List_Id;
2343 begin
2344 Make_Aitem_Pragma
2345 (Pragma_Argument_Associations => New_List (
2346 Make_Pragma_Argument_Association (Loc,
2347 Expression => Relocate_Node (Expr))),
2348 Pragma_Name => Name_Ghost);
2350 Decorate (Aspect, Aitem);
2352 -- When the aspect applies to a [generic] package, insert
2353 -- the pragma at the top of the visible declarations. This
2354 -- emulates the placement of a source pragma.
2356 if Nkind_In (N, N_Generic_Package_Declaration,
2357 N_Package_Declaration)
2358 then
2359 Decls := Visible_Declarations (Specification (N));
2361 if No (Decls) then
2362 Decls := New_List;
2363 Set_Visible_Declarations (N, Decls);
2364 end if;
2366 -- When aspects Abstract_State, Ghost, Initial_Condition
2367 -- and Initializes are out of order, ensure that pragma
2368 -- SPARK_Mode is always at the top of the declarations to
2369 -- properly enabled/suppress errors.
2371 Insert_After_SPARK_Mode
2372 (Prag => Aitem,
2373 Ins_Nod => First (Decls),
2374 Decls => Decls);
2376 -- Otherwise the context is an object, [generic] subprogram
2377 -- or type declaration.
2379 else
2380 Insert_Pragma (Aitem);
2381 end if;
2383 goto Continue;
2384 end Ghost;
2386 -- Global
2388 -- Aspect Global is never delayed because it is equivalent to
2389 -- a source pragma which appears after the related subprogram.
2390 -- To deal with forward references, the generated pragma is
2391 -- stored in the contract of the related subprogram and later
2392 -- analyzed at the end of the declarative region. See routine
2393 -- Analyze_Global_In_Decl_Part for details.
2395 when Aspect_Global =>
2396 Make_Aitem_Pragma
2397 (Pragma_Argument_Associations => New_List (
2398 Make_Pragma_Argument_Association (Loc,
2399 Expression => Relocate_Node (Expr))),
2400 Pragma_Name => Name_Global);
2402 Decorate (Aspect, Aitem);
2403 Insert_Pragma (Aitem);
2404 goto Continue;
2406 -- Initial_Condition
2408 -- Aspect Initial_Condition is never delayed because it is
2409 -- equivalent to a source pragma which appears after the
2410 -- related package. To deal with forward references, the
2411 -- generated pragma is stored in the contract of the related
2412 -- package and later analyzed at the end of the declarative
2413 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2414 -- for details.
2416 when Aspect_Initial_Condition => Initial_Condition : declare
2417 Context : Node_Id := N;
2418 Decls : List_Id;
2420 begin
2421 -- When aspect Initial_Condition appears on a generic
2422 -- package, it is propageted to the package instance. The
2423 -- context in this case is the instance spec.
2425 if Nkind (Context) = N_Package_Instantiation then
2426 Context := Instance_Spec (Context);
2427 end if;
2429 if Nkind_In (Context, N_Generic_Package_Declaration,
2430 N_Package_Declaration)
2431 then
2432 Decls := Visible_Declarations (Specification (Context));
2434 Make_Aitem_Pragma
2435 (Pragma_Argument_Associations => New_List (
2436 Make_Pragma_Argument_Association (Loc,
2437 Expression => Relocate_Node (Expr))),
2438 Pragma_Name =>
2439 Name_Initial_Condition);
2440 Decorate (Aspect, Aitem);
2442 if No (Decls) then
2443 Decls := New_List;
2444 Set_Visible_Declarations (Context, Decls);
2445 end if;
2447 -- When aspects Abstract_State, Ghost, Initial_Condition
2448 -- and Initializes are out of order, ensure that pragma
2449 -- SPARK_Mode is always at the top of the declarations to
2450 -- properly enabled/suppress errors.
2452 Insert_After_SPARK_Mode
2453 (Prag => Aitem,
2454 Ins_Nod => First (Decls),
2455 Decls => Decls);
2457 else
2458 Error_Msg_NE
2459 ("aspect & must apply to a package declaration",
2460 Aspect, Id);
2461 end if;
2463 goto Continue;
2464 end Initial_Condition;
2466 -- Initializes
2468 -- Aspect Initializes is never delayed because it is equivalent
2469 -- to a source pragma appearing after the related package. To
2470 -- deal with forward references, the generated pragma is stored
2471 -- in the contract of the related package and later analyzed at
2472 -- the end of the declarative region. For details, see routine
2473 -- Analyze_Initializes_In_Decl_Part.
2475 when Aspect_Initializes => Initializes : declare
2476 Context : Node_Id := N;
2477 Decls : List_Id;
2479 begin
2480 -- When aspect Initializes appears on a generic package,
2481 -- it is propageted to the package instance. The context
2482 -- in this case is the instance spec.
2484 if Nkind (Context) = N_Package_Instantiation then
2485 Context := Instance_Spec (Context);
2486 end if;
2488 if Nkind_In (Context, N_Generic_Package_Declaration,
2489 N_Package_Declaration)
2490 then
2491 Decls := Visible_Declarations (Specification (Context));
2493 Make_Aitem_Pragma
2494 (Pragma_Argument_Associations => New_List (
2495 Make_Pragma_Argument_Association (Loc,
2496 Expression => Relocate_Node (Expr))),
2497 Pragma_Name => Name_Initializes);
2498 Decorate (Aspect, Aitem);
2500 if No (Decls) then
2501 Decls := New_List;
2502 Set_Visible_Declarations (Context, Decls);
2503 end if;
2505 -- When aspects Abstract_State, Ghost, Initial_Condition
2506 -- and Initializes are out of order, ensure that pragma
2507 -- SPARK_Mode is always at the top of the declarations to
2508 -- properly enabled/suppress errors.
2510 Insert_After_SPARK_Mode
2511 (Prag => Aitem,
2512 Ins_Nod => First (Decls),
2513 Decls => Decls);
2515 else
2516 Error_Msg_NE
2517 ("aspect & must apply to a package declaration",
2518 Aspect, Id);
2519 end if;
2521 goto Continue;
2522 end Initializes;
2524 -- Obsolescent
2526 when Aspect_Obsolescent => declare
2527 Args : List_Id;
2529 begin
2530 if No (Expr) then
2531 Args := No_List;
2532 else
2533 Args := New_List (
2534 Make_Pragma_Argument_Association (Sloc (Expr),
2535 Expression => Relocate_Node (Expr)));
2536 end if;
2538 Make_Aitem_Pragma
2539 (Pragma_Argument_Associations => Args,
2540 Pragma_Name => Chars (Id));
2541 end;
2543 -- Part_Of
2545 when Aspect_Part_Of =>
2546 if Nkind_In (N, N_Object_Declaration,
2547 N_Package_Instantiation)
2548 then
2549 Make_Aitem_Pragma
2550 (Pragma_Argument_Associations => New_List (
2551 Make_Pragma_Argument_Association (Loc,
2552 Expression => Relocate_Node (Expr))),
2553 Pragma_Name => Name_Part_Of);
2555 else
2556 Error_Msg_NE
2557 ("aspect & must apply to a variable or package "
2558 & "instantiation", Aspect, Id);
2559 end if;
2561 -- SPARK_Mode
2563 when Aspect_SPARK_Mode => SPARK_Mode : declare
2564 Decls : List_Id;
2566 begin
2567 Make_Aitem_Pragma
2568 (Pragma_Argument_Associations => New_List (
2569 Make_Pragma_Argument_Association (Loc,
2570 Expression => Relocate_Node (Expr))),
2571 Pragma_Name => Name_SPARK_Mode);
2573 -- When the aspect appears on a package or a subprogram
2574 -- body, insert the generated pragma at the top of the body
2575 -- declarations to emulate the behavior of a source pragma.
2577 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2578 Decorate (Aspect, Aitem);
2580 Decls := Declarations (N);
2582 if No (Decls) then
2583 Decls := New_List;
2584 Set_Declarations (N, Decls);
2585 end if;
2587 Prepend_To (Decls, Aitem);
2588 goto Continue;
2590 -- When the aspect is associated with a [generic] package
2591 -- declaration, insert the generated pragma at the top of
2592 -- the visible declarations to emulate the behavior of a
2593 -- source pragma.
2595 elsif Nkind_In (N, N_Generic_Package_Declaration,
2596 N_Package_Declaration)
2597 then
2598 Decorate (Aspect, Aitem);
2600 Decls := Visible_Declarations (Specification (N));
2602 if No (Decls) then
2603 Decls := New_List;
2604 Set_Visible_Declarations (Specification (N), Decls);
2605 end if;
2607 Prepend_To (Decls, Aitem);
2608 goto Continue;
2609 end if;
2610 end SPARK_Mode;
2612 -- Refined_Depends
2614 -- Aspect Refined_Depends is never delayed because it is
2615 -- equivalent to a source pragma which appears in the
2616 -- declarations of the related subprogram body. To deal with
2617 -- forward references, the generated pragma is stored in the
2618 -- contract of the related subprogram body and later analyzed
2619 -- at the end of the declarative region. For details, see
2620 -- routine Analyze_Refined_Depends_In_Decl_Part.
2622 when Aspect_Refined_Depends =>
2623 Make_Aitem_Pragma
2624 (Pragma_Argument_Associations => New_List (
2625 Make_Pragma_Argument_Association (Loc,
2626 Expression => Relocate_Node (Expr))),
2627 Pragma_Name => Name_Refined_Depends);
2629 Decorate (Aspect, Aitem);
2630 Insert_Pragma (Aitem);
2631 goto Continue;
2633 -- Refined_Global
2635 -- Aspect Refined_Global is never delayed because it is
2636 -- equivalent to a source pragma which appears in the
2637 -- declarations of the related subprogram body. To deal with
2638 -- forward references, the generated pragma is stored in the
2639 -- contract of the related subprogram body and later analyzed
2640 -- at the end of the declarative region. For details, see
2641 -- routine Analyze_Refined_Global_In_Decl_Part.
2643 when Aspect_Refined_Global =>
2644 Make_Aitem_Pragma
2645 (Pragma_Argument_Associations => New_List (
2646 Make_Pragma_Argument_Association (Loc,
2647 Expression => Relocate_Node (Expr))),
2648 Pragma_Name => Name_Refined_Global);
2650 Decorate (Aspect, Aitem);
2651 Insert_Pragma (Aitem);
2652 goto Continue;
2654 -- Refined_Post
2656 when Aspect_Refined_Post =>
2657 Make_Aitem_Pragma
2658 (Pragma_Argument_Associations => New_List (
2659 Make_Pragma_Argument_Association (Loc,
2660 Expression => Relocate_Node (Expr))),
2661 Pragma_Name => Name_Refined_Post);
2663 -- Refined_State
2665 when Aspect_Refined_State => Refined_State : declare
2666 Decls : List_Id;
2668 begin
2669 -- The corresponding pragma for Refined_State is inserted in
2670 -- the declarations of the related package body. This action
2671 -- synchronizes both the source and from-aspect versions of
2672 -- the pragma.
2674 if Nkind (N) = N_Package_Body then
2675 Decls := Declarations (N);
2677 Make_Aitem_Pragma
2678 (Pragma_Argument_Associations => New_List (
2679 Make_Pragma_Argument_Association (Loc,
2680 Expression => Relocate_Node (Expr))),
2681 Pragma_Name => Name_Refined_State);
2682 Decorate (Aspect, Aitem);
2684 if No (Decls) then
2685 Decls := New_List;
2686 Set_Declarations (N, Decls);
2687 end if;
2689 -- Pragma Refined_State must be inserted after pragma
2690 -- SPARK_Mode in the tree. This ensures that any error
2691 -- messages dependent on SPARK_Mode will be properly
2692 -- enabled/suppressed.
2694 Insert_After_SPARK_Mode
2695 (Prag => Aitem,
2696 Ins_Nod => First (Decls),
2697 Decls => Decls);
2699 else
2700 Error_Msg_NE
2701 ("aspect & must apply to a package body", Aspect, Id);
2702 end if;
2704 goto Continue;
2705 end Refined_State;
2707 -- Relative_Deadline
2709 when Aspect_Relative_Deadline =>
2710 Make_Aitem_Pragma
2711 (Pragma_Argument_Associations => New_List (
2712 Make_Pragma_Argument_Association (Loc,
2713 Expression => Relocate_Node (Expr))),
2714 Pragma_Name => Name_Relative_Deadline);
2716 -- If the aspect applies to a task, the corresponding pragma
2717 -- must appear within its declarations, not after.
2719 if Nkind (N) = N_Task_Type_Declaration then
2720 declare
2721 Def : Node_Id;
2722 V : List_Id;
2724 begin
2725 if No (Task_Definition (N)) then
2726 Set_Task_Definition (N,
2727 Make_Task_Definition (Loc,
2728 Visible_Declarations => New_List,
2729 End_Label => Empty));
2730 end if;
2732 Def := Task_Definition (N);
2733 V := Visible_Declarations (Def);
2734 if not Is_Empty_List (V) then
2735 Insert_Before (First (V), Aitem);
2737 else
2738 Set_Visible_Declarations (Def, New_List (Aitem));
2739 end if;
2741 goto Continue;
2742 end;
2743 end if;
2745 -- Case 2e: Annotate aspect
2747 when Aspect_Annotate =>
2748 declare
2749 Args : List_Id;
2750 Pargs : List_Id;
2751 Arg : Node_Id;
2753 begin
2754 -- The argument can be a single identifier
2756 if Nkind (Expr) = N_Identifier then
2758 -- One level of parens is allowed
2760 if Paren_Count (Expr) > 1 then
2761 Error_Msg_F ("extra parentheses ignored", Expr);
2762 end if;
2764 Set_Paren_Count (Expr, 0);
2766 -- Add the single item to the list
2768 Args := New_List (Expr);
2770 -- Otherwise we must have an aggregate
2772 elsif Nkind (Expr) = N_Aggregate then
2774 -- Must be positional
2776 if Present (Component_Associations (Expr)) then
2777 Error_Msg_F
2778 ("purely positional aggregate required", Expr);
2779 goto Continue;
2780 end if;
2782 -- Must not be parenthesized
2784 if Paren_Count (Expr) /= 0 then
2785 Error_Msg_F ("extra parentheses ignored", Expr);
2786 end if;
2788 -- List of arguments is list of aggregate expressions
2790 Args := Expressions (Expr);
2792 -- Anything else is illegal
2794 else
2795 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2796 goto Continue;
2797 end if;
2799 -- Prepare pragma arguments
2801 Pargs := New_List;
2802 Arg := First (Args);
2803 while Present (Arg) loop
2804 Append_To (Pargs,
2805 Make_Pragma_Argument_Association (Sloc (Arg),
2806 Expression => Relocate_Node (Arg)));
2807 Next (Arg);
2808 end loop;
2810 Append_To (Pargs,
2811 Make_Pragma_Argument_Association (Sloc (Ent),
2812 Chars => Name_Entity,
2813 Expression => Ent));
2815 Make_Aitem_Pragma
2816 (Pragma_Argument_Associations => Pargs,
2817 Pragma_Name => Name_Annotate);
2818 end;
2820 -- Case 3 : Aspects that don't correspond to pragma/attribute
2821 -- definition clause.
2823 -- Case 3a: The aspects listed below don't correspond to
2824 -- pragmas/attributes but do require delayed analysis.
2826 -- Default_Value can only apply to a scalar type
2828 when Aspect_Default_Value =>
2829 if not Is_Scalar_Type (E) then
2830 Error_Msg_N
2831 ("aspect Default_Value must apply to a scalar type", N);
2832 end if;
2834 Aitem := Empty;
2836 -- Default_Component_Value can only apply to an array type
2837 -- with scalar components.
2839 when Aspect_Default_Component_Value =>
2840 if not (Is_Array_Type (E)
2841 and then Is_Scalar_Type (Component_Type (E)))
2842 then
2843 Error_Msg_N ("aspect Default_Component_Value can only "
2844 & "apply to an array of scalar components", N);
2845 end if;
2847 Aitem := Empty;
2849 -- Case 3b: The aspects listed below don't correspond to
2850 -- pragmas/attributes and don't need delayed analysis.
2852 -- Implicit_Dereference
2854 -- For Implicit_Dereference, External_Name and Link_Name, only
2855 -- the legality checks are done during the analysis, thus no
2856 -- delay is required.
2858 when Aspect_Implicit_Dereference =>
2859 Analyze_Aspect_Implicit_Dereference;
2860 goto Continue;
2862 -- External_Name, Link_Name
2864 when Aspect_External_Name |
2865 Aspect_Link_Name =>
2866 Analyze_Aspect_External_Or_Link_Name;
2867 goto Continue;
2869 -- Dimension
2871 when Aspect_Dimension =>
2872 Analyze_Aspect_Dimension (N, Id, Expr);
2873 goto Continue;
2875 -- Dimension_System
2877 when Aspect_Dimension_System =>
2878 Analyze_Aspect_Dimension_System (N, Id, Expr);
2879 goto Continue;
2881 -- Case 4: Aspects requiring special handling
2883 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2884 -- pragmas take care of the delay.
2886 -- Pre/Post
2888 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2889 -- with a first argument that is the expression, and a second
2890 -- argument that is an informative message if the test fails.
2891 -- This is inserted right after the declaration, to get the
2892 -- required pragma placement. The processing for the pragmas
2893 -- takes care of the required delay.
2895 when Pre_Post_Aspects => Pre_Post : declare
2896 Pname : Name_Id;
2898 begin
2899 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2900 Pname := Name_Precondition;
2901 else
2902 Pname := Name_Postcondition;
2903 end if;
2905 -- If the expressions is of the form A and then B, then
2906 -- we generate separate Pre/Post aspects for the separate
2907 -- clauses. Since we allow multiple pragmas, there is no
2908 -- problem in allowing multiple Pre/Post aspects internally.
2909 -- These should be treated in reverse order (B first and
2910 -- A second) since they are later inserted just after N in
2911 -- the order they are treated. This way, the pragma for A
2912 -- ends up preceding the pragma for B, which may have an
2913 -- importance for the error raised (either constraint error
2914 -- or precondition error).
2916 -- We do not do this for Pre'Class, since we have to put
2917 -- these conditions together in a complex OR expression.
2919 -- We do not do this in ASIS mode, as ASIS relies on the
2920 -- original node representing the complete expression, when
2921 -- retrieving it through the source aspect table.
2923 if not ASIS_Mode
2924 and then (Pname = Name_Postcondition
2925 or else not Class_Present (Aspect))
2926 then
2927 while Nkind (Expr) = N_And_Then loop
2928 Insert_After (Aspect,
2929 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2930 Identifier => Identifier (Aspect),
2931 Expression => Relocate_Node (Left_Opnd (Expr)),
2932 Class_Present => Class_Present (Aspect),
2933 Split_PPC => True));
2934 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2935 Eloc := Sloc (Expr);
2936 end loop;
2937 end if;
2939 -- Build the precondition/postcondition pragma
2941 -- Add note about why we do NOT need Copy_Tree here???
2943 Make_Aitem_Pragma
2944 (Pragma_Argument_Associations => New_List (
2945 Make_Pragma_Argument_Association (Eloc,
2946 Chars => Name_Check,
2947 Expression => Relocate_Node (Expr))),
2948 Pragma_Name => Pname);
2950 -- Add message unless exception messages are suppressed
2952 if not Opt.Exception_Locations_Suppressed then
2953 Append_To (Pragma_Argument_Associations (Aitem),
2954 Make_Pragma_Argument_Association (Eloc,
2955 Chars => Name_Message,
2956 Expression =>
2957 Make_String_Literal (Eloc,
2958 Strval => "failed "
2959 & Get_Name_String (Pname)
2960 & " from "
2961 & Build_Location_String (Eloc))));
2962 end if;
2964 Set_Is_Delayed_Aspect (Aspect);
2966 -- For Pre/Post cases, insert immediately after the entity
2967 -- declaration, since that is the required pragma placement.
2968 -- Note that for these aspects, we do not have to worry
2969 -- about delay issues, since the pragmas themselves deal
2970 -- with delay of visibility for the expression analysis.
2972 Insert_Pragma (Aitem);
2974 goto Continue;
2975 end Pre_Post;
2977 -- Test_Case
2979 when Aspect_Test_Case => Test_Case : declare
2980 Args : List_Id;
2981 Comp_Expr : Node_Id;
2982 Comp_Assn : Node_Id;
2983 New_Expr : Node_Id;
2985 begin
2986 Args := New_List;
2988 if Nkind (Parent (N)) = N_Compilation_Unit then
2989 Error_Msg_Name_1 := Nam;
2990 Error_Msg_N ("incorrect placement of aspect `%`", E);
2991 goto Continue;
2992 end if;
2994 if Nkind (Expr) /= N_Aggregate then
2995 Error_Msg_Name_1 := Nam;
2996 Error_Msg_NE
2997 ("wrong syntax for aspect `%` for &", Id, E);
2998 goto Continue;
2999 end if;
3001 -- Make pragma expressions refer to the original aspect
3002 -- expressions through the Original_Node link. This is used
3003 -- in semantic analysis for ASIS mode, so that the original
3004 -- expression also gets analyzed.
3006 Comp_Expr := First (Expressions (Expr));
3007 while Present (Comp_Expr) loop
3008 New_Expr := Relocate_Node (Comp_Expr);
3009 Append_To (Args,
3010 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3011 Expression => New_Expr));
3012 Next (Comp_Expr);
3013 end loop;
3015 Comp_Assn := First (Component_Associations (Expr));
3016 while Present (Comp_Assn) loop
3017 if List_Length (Choices (Comp_Assn)) /= 1
3018 or else
3019 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3020 then
3021 Error_Msg_Name_1 := Nam;
3022 Error_Msg_NE
3023 ("wrong syntax for aspect `%` for &", Id, E);
3024 goto Continue;
3025 end if;
3027 Append_To (Args,
3028 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3029 Chars => Chars (First (Choices (Comp_Assn))),
3030 Expression =>
3031 Relocate_Node (Expression (Comp_Assn))));
3032 Next (Comp_Assn);
3033 end loop;
3035 -- Build the test-case pragma
3037 Make_Aitem_Pragma
3038 (Pragma_Argument_Associations => Args,
3039 Pragma_Name => Nam);
3040 end Test_Case;
3042 -- Contract_Cases
3044 when Aspect_Contract_Cases =>
3045 Make_Aitem_Pragma
3046 (Pragma_Argument_Associations => New_List (
3047 Make_Pragma_Argument_Association (Loc,
3048 Expression => Relocate_Node (Expr))),
3049 Pragma_Name => Nam);
3051 Decorate (Aspect, Aitem);
3052 Insert_Pragma (Aitem);
3053 goto Continue;
3055 -- Case 5: Special handling for aspects with an optional
3056 -- boolean argument.
3058 -- In the delayed case, the corresponding pragma cannot be
3059 -- generated yet because the evaluation of the boolean needs
3060 -- to be delayed till the freeze point.
3062 when Boolean_Aspects |
3063 Library_Unit_Aspects =>
3065 Set_Is_Boolean_Aspect (Aspect);
3067 -- Lock_Free aspect only apply to protected objects
3069 if A_Id = Aspect_Lock_Free then
3070 if Ekind (E) /= E_Protected_Type then
3071 Error_Msg_Name_1 := Nam;
3072 Error_Msg_N
3073 ("aspect % only applies to a protected object",
3074 Aspect);
3076 else
3077 -- Set the Uses_Lock_Free flag to True if there is no
3078 -- expression or if the expression is True. The
3079 -- evaluation of this aspect should be delayed to the
3080 -- freeze point (why???)
3082 if No (Expr)
3083 or else Is_True (Static_Boolean (Expr))
3084 then
3085 Set_Uses_Lock_Free (E);
3086 end if;
3088 Record_Rep_Item (E, Aspect);
3089 end if;
3091 goto Continue;
3093 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
3095 -- For the case of aspects Import and Export, we don't
3096 -- consider that we know the entity is never set in the
3097 -- source, since it is is likely modified outside the
3098 -- program.
3100 -- Note: one might think that the analysis of the
3101 -- resulting pragma would take care of that, but
3102 -- that's not the case since it won't be from source.
3104 if Ekind (E) = E_Variable then
3105 Set_Never_Set_In_Source (E, False);
3106 end if;
3108 -- In older versions of Ada the corresponding pragmas
3109 -- specified a Convention. In Ada 2012 the convention is
3110 -- specified as a separate aspect, and it is optional,
3111 -- given that it defaults to Convention_Ada. The code
3112 -- that verifed that there was a matching convention
3113 -- is now obsolete.
3115 -- Resolve the expression of an Import or Export here,
3116 -- and require it to be of type Boolean and static. This
3117 -- is not quite right, because in general this should be
3118 -- delayed, but that seems tricky for these, because
3119 -- normally Boolean aspects are replaced with pragmas at
3120 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
3121 -- but in the case of these aspects we can't generate
3122 -- a simple pragma with just the entity name. ???
3124 if not Present (Expr)
3125 or else Is_True (Static_Boolean (Expr))
3126 then
3127 if A_Id = Aspect_Import then
3128 Set_Is_Imported (E);
3129 Set_Has_Completion (E);
3131 -- An imported entity cannot have an explicit
3132 -- initialization.
3134 if Nkind (N) = N_Object_Declaration
3135 and then Present (Expression (N))
3136 then
3137 Error_Msg_N
3138 ("imported entities cannot be initialized "
3139 & "(RM B.1(24))", Expression (N));
3140 end if;
3142 elsif A_Id = Aspect_Export then
3143 Set_Is_Exported (E);
3144 end if;
3145 end if;
3147 goto Continue;
3149 -- Disable_Controlled
3151 elsif A_Id = Aspect_Disable_Controlled then
3152 if Ekind (E) /= E_Record_Type
3153 or else not Is_Controlled (E)
3154 then
3155 Error_Msg_N
3156 ("aspect % requires controlled record type", Aspect);
3157 goto Continue;
3158 end if;
3160 Analyze_And_Resolve (Expr, Standard_Boolean);
3162 -- If we're in a generic template, we don't want to try
3163 -- to disable controlled types, because typical usage is
3164 -- "Disable_Controlled => not <some_check>'Enabled", and
3165 -- the value of Enabled is not known until we see a
3166 -- particular instance.
3168 if Expander_Active then
3169 if not Present (Expr)
3170 or else Is_True (Static_Boolean (Expr))
3171 then
3172 Set_Disable_Controlled (E);
3173 end if;
3174 end if;
3176 goto Continue;
3177 end if;
3179 -- Library unit aspects require special handling in the case
3180 -- of a package declaration, the pragma needs to be inserted
3181 -- in the list of declarations for the associated package.
3182 -- There is no issue of visibility delay for these aspects.
3184 if A_Id in Library_Unit_Aspects
3185 and then
3186 Nkind_In (N, N_Package_Declaration,
3187 N_Generic_Package_Declaration)
3188 and then Nkind (Parent (N)) /= N_Compilation_Unit
3190 -- Aspect is legal on a local instantiation of a library-
3191 -- level generic unit.
3193 and then not Is_Generic_Instance (Defining_Entity (N))
3194 then
3195 Error_Msg_N
3196 ("incorrect context for library unit aspect&", Id);
3197 goto Continue;
3198 end if;
3200 -- External property aspects are Boolean by nature, but
3201 -- their pragmas must contain two arguments, the second
3202 -- being the optional Boolean expression.
3204 if A_Id = Aspect_Async_Readers or else
3205 A_Id = Aspect_Async_Writers or else
3206 A_Id = Aspect_Effective_Reads or else
3207 A_Id = Aspect_Effective_Writes
3208 then
3209 declare
3210 Args : List_Id;
3212 begin
3213 -- The first argument of the external property pragma
3214 -- is the related object.
3216 Args :=
3217 New_List (
3218 Make_Pragma_Argument_Association (Sloc (Ent),
3219 Expression => Ent));
3221 -- The second argument is the optional Boolean
3222 -- expression which must be propagated even if it
3223 -- evaluates to False as this has special semantic
3224 -- meaning.
3226 if Present (Expr) then
3227 Append_To (Args,
3228 Make_Pragma_Argument_Association (Loc,
3229 Expression => Relocate_Node (Expr)));
3230 end if;
3232 Make_Aitem_Pragma
3233 (Pragma_Argument_Associations => Args,
3234 Pragma_Name => Nam);
3235 end;
3237 -- Cases where we do not delay, includes all cases where the
3238 -- expression is missing other than the above cases.
3240 elsif not Delay_Required or else No (Expr) then
3241 Make_Aitem_Pragma
3242 (Pragma_Argument_Associations => New_List (
3243 Make_Pragma_Argument_Association (Sloc (Ent),
3244 Expression => Ent)),
3245 Pragma_Name => Chars (Id));
3246 Delay_Required := False;
3248 -- In general cases, the corresponding pragma/attribute
3249 -- definition clause will be inserted later at the freezing
3250 -- point, and we do not need to build it now.
3252 else
3253 Aitem := Empty;
3254 end if;
3256 -- Storage_Size
3258 -- This is special because for access types we need to generate
3259 -- an attribute definition clause. This also works for single
3260 -- task declarations, but it does not work for task type
3261 -- declarations, because we have the case where the expression
3262 -- references a discriminant of the task type. That can't use
3263 -- an attribute definition clause because we would not have
3264 -- visibility on the discriminant. For that case we must
3265 -- generate a pragma in the task definition.
3267 when Aspect_Storage_Size =>
3269 -- Task type case
3271 if Ekind (E) = E_Task_Type then
3272 declare
3273 Decl : constant Node_Id := Declaration_Node (E);
3275 begin
3276 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3278 -- If no task definition, create one
3280 if No (Task_Definition (Decl)) then
3281 Set_Task_Definition (Decl,
3282 Make_Task_Definition (Loc,
3283 Visible_Declarations => Empty_List,
3284 End_Label => Empty));
3285 end if;
3287 -- Create a pragma and put it at the start of the task
3288 -- definition for the task type declaration.
3290 Make_Aitem_Pragma
3291 (Pragma_Argument_Associations => New_List (
3292 Make_Pragma_Argument_Association (Loc,
3293 Expression => Relocate_Node (Expr))),
3294 Pragma_Name => Name_Storage_Size);
3296 Prepend
3297 (Aitem,
3298 Visible_Declarations (Task_Definition (Decl)));
3299 goto Continue;
3300 end;
3302 -- All other cases, generate attribute definition
3304 else
3305 Aitem :=
3306 Make_Attribute_Definition_Clause (Loc,
3307 Name => Ent,
3308 Chars => Chars (Id),
3309 Expression => Relocate_Node (Expr));
3310 end if;
3311 end case;
3313 -- Attach the corresponding pragma/attribute definition clause to
3314 -- the aspect specification node.
3316 if Present (Aitem) then
3317 Set_From_Aspect_Specification (Aitem);
3318 end if;
3320 -- In the context of a compilation unit, we directly put the
3321 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3322 -- node (no delay is required here) except for aspects on a
3323 -- subprogram body (see below) and a generic package, for which we
3324 -- need to introduce the pragma before building the generic copy
3325 -- (see sem_ch12), and for package instantiations, where the
3326 -- library unit pragmas are better handled early.
3328 if Nkind (Parent (N)) = N_Compilation_Unit
3329 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3330 then
3331 declare
3332 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3334 begin
3335 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3337 -- For a Boolean aspect, create the corresponding pragma if
3338 -- no expression or if the value is True.
3340 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3341 if Is_True (Static_Boolean (Expr)) then
3342 Make_Aitem_Pragma
3343 (Pragma_Argument_Associations => New_List (
3344 Make_Pragma_Argument_Association (Sloc (Ent),
3345 Expression => Ent)),
3346 Pragma_Name => Chars (Id));
3348 Set_From_Aspect_Specification (Aitem, True);
3349 Set_Corresponding_Aspect (Aitem, Aspect);
3351 else
3352 goto Continue;
3353 end if;
3354 end if;
3356 -- If the aspect is on a subprogram body (relevant aspect
3357 -- is Inline), add the pragma in front of the declarations.
3359 if Nkind (N) = N_Subprogram_Body then
3360 if No (Declarations (N)) then
3361 Set_Declarations (N, New_List);
3362 end if;
3364 Prepend (Aitem, Declarations (N));
3366 elsif Nkind (N) = N_Generic_Package_Declaration then
3367 if No (Visible_Declarations (Specification (N))) then
3368 Set_Visible_Declarations (Specification (N), New_List);
3369 end if;
3371 Prepend (Aitem,
3372 Visible_Declarations (Specification (N)));
3374 elsif Nkind (N) = N_Package_Instantiation then
3375 declare
3376 Spec : constant Node_Id :=
3377 Specification (Instance_Spec (N));
3378 begin
3379 if No (Visible_Declarations (Spec)) then
3380 Set_Visible_Declarations (Spec, New_List);
3381 end if;
3383 Prepend (Aitem, Visible_Declarations (Spec));
3384 end;
3386 else
3387 if No (Pragmas_After (Aux)) then
3388 Set_Pragmas_After (Aux, New_List);
3389 end if;
3391 Append (Aitem, Pragmas_After (Aux));
3392 end if;
3394 goto Continue;
3395 end;
3396 end if;
3398 -- The evaluation of the aspect is delayed to the freezing point.
3399 -- The pragma or attribute clause if there is one is then attached
3400 -- to the aspect specification which is put in the rep item list.
3402 if Delay_Required then
3403 if Present (Aitem) then
3404 Set_Is_Delayed_Aspect (Aitem);
3405 Set_Aspect_Rep_Item (Aspect, Aitem);
3406 Set_Parent (Aitem, Aspect);
3407 end if;
3409 Set_Is_Delayed_Aspect (Aspect);
3411 -- In the case of Default_Value, link the aspect to base type
3412 -- as well, even though it appears on a first subtype. This is
3413 -- mandated by the semantics of the aspect. Do not establish
3414 -- the link when processing the base type itself as this leads
3415 -- to a rep item circularity. Verify that we are dealing with
3416 -- a scalar type to prevent cascaded errors.
3418 if A_Id = Aspect_Default_Value
3419 and then Is_Scalar_Type (E)
3420 and then Base_Type (E) /= E
3421 then
3422 Set_Has_Delayed_Aspects (Base_Type (E));
3423 Record_Rep_Item (Base_Type (E), Aspect);
3424 end if;
3426 Set_Has_Delayed_Aspects (E);
3427 Record_Rep_Item (E, Aspect);
3429 -- When delay is not required and the context is a package or a
3430 -- subprogram body, insert the pragma in the body declarations.
3432 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3433 if No (Declarations (N)) then
3434 Set_Declarations (N, New_List);
3435 end if;
3437 -- The pragma is added before source declarations
3439 Prepend_To (Declarations (N), Aitem);
3441 -- When delay is not required and the context is not a compilation
3442 -- unit, we simply insert the pragma/attribute definition clause
3443 -- in sequence.
3445 else
3446 Insert_After (Ins_Node, Aitem);
3447 Ins_Node := Aitem;
3448 end if;
3449 end Analyze_One_Aspect;
3451 <<Continue>>
3452 Next (Aspect);
3453 end loop Aspect_Loop;
3455 if Has_Delayed_Aspects (E) then
3456 Ensure_Freeze_Node (E);
3457 end if;
3458 end Analyze_Aspect_Specifications;
3460 ---------------------------------------------------
3461 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3462 ---------------------------------------------------
3464 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3465 Body_Id : constant Entity_Id := Defining_Entity (N);
3467 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3468 -- Subprogram body [stub] N has aspects, but they are not properly
3469 -- placed. Emit an error message depending on the aspects involved.
3470 -- Spec_Id is the entity of the corresponding spec.
3472 --------------------------------
3473 -- Diagnose_Misplaced_Aspects --
3474 --------------------------------
3476 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3477 procedure Misplaced_Aspect_Error
3478 (Asp : Node_Id;
3479 Ref_Nam : Name_Id);
3480 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3481 -- the name of the refined version of the aspect.
3483 ----------------------------
3484 -- Misplaced_Aspect_Error --
3485 ----------------------------
3487 procedure Misplaced_Aspect_Error
3488 (Asp : Node_Id;
3489 Ref_Nam : Name_Id)
3491 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3492 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3494 begin
3495 -- The corresponding spec already contains the aspect in question
3496 -- and the one appearing on the body must be the refined form:
3498 -- procedure P with Global ...;
3499 -- procedure P with Global ... is ... end P;
3500 -- ^
3501 -- Refined_Global
3503 if Has_Aspect (Spec_Id, Asp_Id) then
3504 Error_Msg_Name_1 := Asp_Nam;
3506 -- Subunits cannot carry aspects that apply to a subprogram
3507 -- declaration.
3509 if Nkind (Parent (N)) = N_Subunit then
3510 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3512 -- Otherwise suggest the refined form
3514 else
3515 Error_Msg_Name_2 := Ref_Nam;
3516 Error_Msg_N ("aspect % should be %", Asp);
3517 end if;
3519 -- Otherwise the aspect must appear on the spec, not on the body
3521 -- procedure P;
3522 -- procedure P with Global ... is ... end P;
3524 else
3525 Error_Msg_N
3526 ("aspect specification must appear in subprogram declaration",
3527 Asp);
3528 end if;
3529 end Misplaced_Aspect_Error;
3531 -- Local variables
3533 Asp : Node_Id;
3534 Asp_Nam : Name_Id;
3536 -- Start of processing for Diagnose_Misplaced_Aspects
3538 begin
3539 -- Iterate over the aspect specifications and emit specific errors
3540 -- where applicable.
3542 Asp := First (Aspect_Specifications (N));
3543 while Present (Asp) loop
3544 Asp_Nam := Chars (Identifier (Asp));
3546 -- Do not emit errors on aspects that can appear on a subprogram
3547 -- body. This scenario occurs when the aspect specification list
3548 -- contains both misplaced and properly placed aspects.
3550 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3551 null;
3553 -- Special diagnostics for SPARK aspects
3555 elsif Asp_Nam = Name_Depends then
3556 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3558 elsif Asp_Nam = Name_Global then
3559 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3561 elsif Asp_Nam = Name_Post then
3562 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3564 -- Otherwise a language-defined aspect is misplaced
3566 else
3567 Error_Msg_N
3568 ("aspect specification must appear in subprogram declaration",
3569 Asp);
3570 end if;
3572 Next (Asp);
3573 end loop;
3574 end Diagnose_Misplaced_Aspects;
3576 -- Local variables
3578 Spec_Id : Entity_Id;
3580 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3582 begin
3583 if Nkind (N) = N_Subprogram_Body_Stub then
3584 Spec_Id := Corresponding_Spec_Of_Stub (N);
3585 else
3586 Spec_Id := Corresponding_Spec (N);
3587 end if;
3589 -- Language-defined aspects cannot be associated with a subprogram body
3590 -- [stub] if the subprogram has a spec. Certain implementation defined
3591 -- aspects are allowed to break this rule (for all applicable cases, see
3592 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3594 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
3595 Diagnose_Misplaced_Aspects (Spec_Id);
3596 else
3597 Analyze_Aspect_Specifications (N, Body_Id);
3598 end if;
3599 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3601 -----------------------
3602 -- Analyze_At_Clause --
3603 -----------------------
3605 -- An at clause is replaced by the corresponding Address attribute
3606 -- definition clause that is the preferred approach in Ada 95.
3608 procedure Analyze_At_Clause (N : Node_Id) is
3609 CS : constant Boolean := Comes_From_Source (N);
3611 begin
3612 -- This is an obsolescent feature
3614 Check_Restriction (No_Obsolescent_Features, N);
3616 if Warn_On_Obsolescent_Feature then
3617 Error_Msg_N
3618 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3619 Error_Msg_N
3620 ("\?j?use address attribute definition clause instead", N);
3621 end if;
3623 -- Rewrite as address clause
3625 Rewrite (N,
3626 Make_Attribute_Definition_Clause (Sloc (N),
3627 Name => Identifier (N),
3628 Chars => Name_Address,
3629 Expression => Expression (N)));
3631 -- We preserve Comes_From_Source, since logically the clause still comes
3632 -- from the source program even though it is changed in form.
3634 Set_Comes_From_Source (N, CS);
3636 -- Analyze rewritten clause
3638 Analyze_Attribute_Definition_Clause (N);
3639 end Analyze_At_Clause;
3641 -----------------------------------------
3642 -- Analyze_Attribute_Definition_Clause --
3643 -----------------------------------------
3645 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3646 Loc : constant Source_Ptr := Sloc (N);
3647 Nam : constant Node_Id := Name (N);
3648 Attr : constant Name_Id := Chars (N);
3649 Expr : constant Node_Id := Expression (N);
3650 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3652 Ent : Entity_Id;
3653 -- The entity of Nam after it is analyzed. In the case of an incomplete
3654 -- type, this is the underlying type.
3656 U_Ent : Entity_Id;
3657 -- The underlying entity to which the attribute applies. Generally this
3658 -- is the Underlying_Type of Ent, except in the case where the clause
3659 -- applies to full view of incomplete type or private type in which case
3660 -- U_Ent is just a copy of Ent.
3662 FOnly : Boolean := False;
3663 -- Reset to True for subtype specific attribute (Alignment, Size)
3664 -- and for stream attributes, i.e. those cases where in the call to
3665 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3666 -- are checked. Note that the case of stream attributes is not clear
3667 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3668 -- Storage_Size for derived task types, but that is also clearly
3669 -- unintentional.
3671 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3672 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3673 -- definition clauses.
3675 function Duplicate_Clause return Boolean;
3676 -- This routine checks if the aspect for U_Ent being given by attribute
3677 -- definition clause N is for an aspect that has already been specified,
3678 -- and if so gives an error message. If there is a duplicate, True is
3679 -- returned, otherwise if there is no error, False is returned.
3681 procedure Check_Indexing_Functions;
3682 -- Check that the function in Constant_Indexing or Variable_Indexing
3683 -- attribute has the proper type structure. If the name is overloaded,
3684 -- check that some interpretation is legal.
3686 procedure Check_Iterator_Functions;
3687 -- Check that there is a single function in Default_Iterator attribute
3688 -- has the proper type structure.
3690 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3691 -- Common legality check for the previous two
3693 -----------------------------------
3694 -- Analyze_Stream_TSS_Definition --
3695 -----------------------------------
3697 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3698 Subp : Entity_Id := Empty;
3699 I : Interp_Index;
3700 It : Interp;
3701 Pnam : Entity_Id;
3703 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3704 -- True for Read attribute, false for other attributes
3706 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3707 -- Return true if the entity is a subprogram with an appropriate
3708 -- profile for the attribute being defined.
3710 ----------------------
3711 -- Has_Good_Profile --
3712 ----------------------
3714 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3715 F : Entity_Id;
3716 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3717 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3718 (False => E_Procedure, True => E_Function);
3719 Typ : Entity_Id;
3721 begin
3722 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3723 return False;
3724 end if;
3726 F := First_Formal (Subp);
3728 if No (F)
3729 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3730 or else Designated_Type (Etype (F)) /=
3731 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3732 then
3733 return False;
3734 end if;
3736 if not Is_Function then
3737 Next_Formal (F);
3739 declare
3740 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3741 (False => E_In_Parameter,
3742 True => E_Out_Parameter);
3743 begin
3744 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3745 return False;
3746 end if;
3747 end;
3749 Typ := Etype (F);
3751 -- If the attribute specification comes from an aspect
3752 -- specification for a class-wide stream, the parameter must be
3753 -- a class-wide type of the entity to which the aspect applies.
3755 if From_Aspect_Specification (N)
3756 and then Class_Present (Parent (N))
3757 and then Is_Class_Wide_Type (Typ)
3758 then
3759 Typ := Etype (Typ);
3760 end if;
3762 else
3763 Typ := Etype (Subp);
3764 end if;
3766 -- Verify that the prefix of the attribute and the local name for
3767 -- the type of the formal match, or one is the class-wide of the
3768 -- other, in the case of a class-wide stream operation.
3770 if Base_Type (Typ) = Base_Type (Ent)
3771 or else (Is_Class_Wide_Type (Typ)
3772 and then Typ = Class_Wide_Type (Base_Type (Ent)))
3773 or else (Is_Class_Wide_Type (Ent)
3774 and then Ent = Class_Wide_Type (Base_Type (Typ)))
3775 then
3776 null;
3777 else
3778 return False;
3779 end if;
3781 if Present ((Next_Formal (F)))
3782 then
3783 return False;
3785 elsif not Is_Scalar_Type (Typ)
3786 and then not Is_First_Subtype (Typ)
3787 and then not Is_Class_Wide_Type (Typ)
3788 then
3789 return False;
3791 else
3792 return True;
3793 end if;
3794 end Has_Good_Profile;
3796 -- Start of processing for Analyze_Stream_TSS_Definition
3798 begin
3799 FOnly := True;
3801 if not Is_Type (U_Ent) then
3802 Error_Msg_N ("local name must be a subtype", Nam);
3803 return;
3805 elsif not Is_First_Subtype (U_Ent) then
3806 Error_Msg_N ("local name must be a first subtype", Nam);
3807 return;
3808 end if;
3810 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3812 -- If Pnam is present, it can be either inherited from an ancestor
3813 -- type (in which case it is legal to redefine it for this type), or
3814 -- be a previous definition of the attribute for the same type (in
3815 -- which case it is illegal).
3817 -- In the first case, it will have been analyzed already, and we
3818 -- can check that its profile does not match the expected profile
3819 -- for a stream attribute of U_Ent. In the second case, either Pnam
3820 -- has been analyzed (and has the expected profile), or it has not
3821 -- been analyzed yet (case of a type that has not been frozen yet
3822 -- and for which the stream attribute has been set using Set_TSS).
3824 if Present (Pnam)
3825 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3826 then
3827 Error_Msg_Sloc := Sloc (Pnam);
3828 Error_Msg_Name_1 := Attr;
3829 Error_Msg_N ("% attribute already defined #", Nam);
3830 return;
3831 end if;
3833 Analyze (Expr);
3835 if Is_Entity_Name (Expr) then
3836 if not Is_Overloaded (Expr) then
3837 if Has_Good_Profile (Entity (Expr)) then
3838 Subp := Entity (Expr);
3839 end if;
3841 else
3842 Get_First_Interp (Expr, I, It);
3843 while Present (It.Nam) loop
3844 if Has_Good_Profile (It.Nam) then
3845 Subp := It.Nam;
3846 exit;
3847 end if;
3849 Get_Next_Interp (I, It);
3850 end loop;
3851 end if;
3852 end if;
3854 if Present (Subp) then
3855 if Is_Abstract_Subprogram (Subp) then
3856 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3857 return;
3859 -- A stream subprogram for an interface type must be a null
3860 -- procedure (RM 13.13.2 (38/3)).
3862 elsif Is_Interface (U_Ent)
3863 and then not Is_Class_Wide_Type (U_Ent)
3864 and then not Inside_A_Generic
3865 and then
3866 (Ekind (Subp) = E_Function
3867 or else
3868 not Null_Present
3869 (Specification
3870 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
3871 then
3872 Error_Msg_N
3873 ("stream subprogram for interface type "
3874 & "must be null procedure", Expr);
3875 end if;
3877 Set_Entity (Expr, Subp);
3878 Set_Etype (Expr, Etype (Subp));
3880 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3882 else
3883 Error_Msg_Name_1 := Attr;
3884 Error_Msg_N ("incorrect expression for% attribute", Expr);
3885 end if;
3886 end Analyze_Stream_TSS_Definition;
3888 ------------------------------
3889 -- Check_Indexing_Functions --
3890 ------------------------------
3892 procedure Check_Indexing_Functions is
3893 Indexing_Found : Boolean := False;
3895 procedure Check_One_Function (Subp : Entity_Id);
3896 -- Check one possible interpretation. Sets Indexing_Found True if a
3897 -- legal indexing function is found.
3899 procedure Illegal_Indexing (Msg : String);
3900 -- Diagnose illegal indexing function if not overloaded. In the
3901 -- overloaded case indicate that no legal interpretation exists.
3903 ------------------------
3904 -- Check_One_Function --
3905 ------------------------
3907 procedure Check_One_Function (Subp : Entity_Id) is
3908 Default_Element : Node_Id;
3909 Ret_Type : constant Entity_Id := Etype (Subp);
3911 begin
3912 if not Is_Overloadable (Subp) then
3913 Illegal_Indexing ("illegal indexing function for type&");
3914 return;
3916 elsif Scope (Subp) /= Scope (Ent) then
3917 if Nkind (Expr) = N_Expanded_Name then
3919 -- Indexing function can't be declared elsewhere
3921 Illegal_Indexing
3922 ("indexing function must be declared in scope of type&");
3923 end if;
3925 return;
3927 elsif No (First_Formal (Subp)) then
3928 Illegal_Indexing
3929 ("Indexing requires a function that applies to type&");
3930 return;
3932 elsif No (Next_Formal (First_Formal (Subp))) then
3933 Illegal_Indexing
3934 ("indexing function must have at least two parameters");
3935 return;
3937 -- For a derived type, check that no indexing aspect is specified
3938 -- for the type if it is also inherited
3940 elsif Is_Derived_Type (Ent) then
3941 declare
3942 Inherited : Node_Id;
3944 begin
3945 if Attr = Name_Constant_Indexing then
3946 Inherited :=
3947 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
3948 else pragma Assert (Attr = Name_Variable_Indexing);
3949 Inherited :=
3950 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
3951 end if;
3953 if Present (Inherited) then
3954 if Debug_Flag_Dot_XX then
3955 null;
3957 -- Indicate the operation that must be overridden, rather
3958 -- than redefining the indexing aspect
3960 else
3961 Illegal_Indexing
3962 ("indexing function already inherited "
3963 & "from parent type");
3964 Error_Msg_NE
3965 ("!override & instead",
3966 N, Entity (Expression (Inherited)));
3967 return;
3968 end if;
3969 end if;
3970 end;
3971 end if;
3973 if not Check_Primitive_Function (Subp) then
3974 Illegal_Indexing
3975 ("Indexing aspect requires a function that applies to type&");
3976 return;
3977 end if;
3979 -- If partial declaration exists, verify that it is not tagged.
3981 if Ekind (Current_Scope) = E_Package
3982 and then Has_Private_Declaration (Ent)
3983 and then From_Aspect_Specification (N)
3984 and then
3985 List_Containing (Parent (Ent)) =
3986 Private_Declarations
3987 (Specification (Unit_Declaration_Node (Current_Scope)))
3988 and then Nkind (N) = N_Attribute_Definition_Clause
3989 then
3990 declare
3991 Decl : Node_Id;
3993 begin
3994 Decl :=
3995 First (Visible_Declarations
3996 (Specification
3997 (Unit_Declaration_Node (Current_Scope))));
3999 while Present (Decl) loop
4000 if Nkind (Decl) = N_Private_Type_Declaration
4001 and then Ent = Full_View (Defining_Identifier (Decl))
4002 and then Tagged_Present (Decl)
4003 and then No (Aspect_Specifications (Decl))
4004 then
4005 Illegal_Indexing
4006 ("Indexing aspect cannot be specified on full view "
4007 & "if partial view is tagged");
4008 return;
4009 end if;
4011 Next (Decl);
4012 end loop;
4013 end;
4014 end if;
4016 -- An indexing function must return either the default element of
4017 -- the container, or a reference type. For variable indexing it
4018 -- must be the latter.
4020 Default_Element :=
4021 Find_Value_Of_Aspect
4022 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4024 if Present (Default_Element) then
4025 Analyze (Default_Element);
4027 if Is_Entity_Name (Default_Element)
4028 and then not Covers (Entity (Default_Element), Ret_Type)
4029 and then False
4030 then
4031 Illegal_Indexing
4032 ("wrong return type for indexing function");
4033 return;
4034 end if;
4035 end if;
4037 -- For variable_indexing the return type must be a reference type
4039 if Attr = Name_Variable_Indexing then
4040 if not Has_Implicit_Dereference (Ret_Type) then
4041 Illegal_Indexing
4042 ("variable indexing must return a reference type");
4043 return;
4045 elsif Is_Access_Constant
4046 (Etype (First_Discriminant (Ret_Type)))
4047 then
4048 Illegal_Indexing
4049 ("variable indexing must return an access to variable");
4050 return;
4051 end if;
4053 else
4054 if Has_Implicit_Dereference (Ret_Type)
4055 and then not
4056 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4057 then
4058 Illegal_Indexing
4059 ("constant indexing must return an access to constant");
4060 return;
4062 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4063 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4064 then
4065 Illegal_Indexing
4066 ("constant indexing must apply to an access to constant");
4067 return;
4068 end if;
4069 end if;
4071 -- All checks succeeded.
4073 Indexing_Found := True;
4074 end Check_One_Function;
4076 -----------------------
4077 -- Illegal_Indexing --
4078 -----------------------
4080 procedure Illegal_Indexing (Msg : String) is
4081 begin
4082 Error_Msg_NE (Msg, N, Ent);
4083 end Illegal_Indexing;
4085 -- Start of processing for Check_Indexing_Functions
4087 begin
4088 if In_Instance then
4089 return;
4090 end if;
4092 Analyze (Expr);
4094 if not Is_Overloaded (Expr) then
4095 Check_One_Function (Entity (Expr));
4097 else
4098 declare
4099 I : Interp_Index;
4100 It : Interp;
4102 begin
4103 Indexing_Found := False;
4104 Get_First_Interp (Expr, I, It);
4105 while Present (It.Nam) loop
4107 -- Note that analysis will have added the interpretation
4108 -- that corresponds to the dereference. We only check the
4109 -- subprogram itself.
4111 if Is_Overloadable (It.Nam) then
4112 Check_One_Function (It.Nam);
4113 end if;
4115 Get_Next_Interp (I, It);
4116 end loop;
4117 end;
4118 end if;
4120 if not Indexing_Found and then not Error_Posted (N) then
4121 Error_Msg_NE
4122 ("aspect Indexing requires a local function that "
4123 & "applies to type&", Expr, Ent);
4124 end if;
4125 end Check_Indexing_Functions;
4127 ------------------------------
4128 -- Check_Iterator_Functions --
4129 ------------------------------
4131 procedure Check_Iterator_Functions is
4132 Default : Entity_Id;
4134 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
4135 -- Check one possible interpretation for validity
4137 ----------------------------
4138 -- Valid_Default_Iterator --
4139 ----------------------------
4141 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4142 Formal : Entity_Id;
4144 begin
4145 if not Check_Primitive_Function (Subp) then
4146 return False;
4147 else
4148 Formal := First_Formal (Subp);
4149 end if;
4151 -- False if any subsequent formal has no default expression
4153 Formal := Next_Formal (Formal);
4154 while Present (Formal) loop
4155 if No (Expression (Parent (Formal))) then
4156 return False;
4157 end if;
4159 Next_Formal (Formal);
4160 end loop;
4162 -- True if all subsequent formals have default expressions
4164 return True;
4165 end Valid_Default_Iterator;
4167 -- Start of processing for Check_Iterator_Functions
4169 begin
4170 Analyze (Expr);
4172 if not Is_Entity_Name (Expr) then
4173 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4174 end if;
4176 if not Is_Overloaded (Expr) then
4177 if not Check_Primitive_Function (Entity (Expr)) then
4178 Error_Msg_NE
4179 ("aspect Indexing requires a function that applies to type&",
4180 Entity (Expr), Ent);
4181 end if;
4183 -- Flag the default_iterator as well as the denoted function.
4185 if not Valid_Default_Iterator (Entity (Expr)) then
4186 Error_Msg_N ("improper function for default iterator!", Expr);
4187 end if;
4189 else
4190 Default := Empty;
4191 declare
4192 I : Interp_Index;
4193 It : Interp;
4195 begin
4196 Get_First_Interp (Expr, I, It);
4197 while Present (It.Nam) loop
4198 if not Check_Primitive_Function (It.Nam)
4199 or else not Valid_Default_Iterator (It.Nam)
4200 then
4201 Remove_Interp (I);
4203 elsif Present (Default) then
4204 Error_Msg_N ("default iterator must be unique", Expr);
4206 else
4207 Default := It.Nam;
4208 end if;
4210 Get_Next_Interp (I, It);
4211 end loop;
4212 end;
4214 if Present (Default) then
4215 Set_Entity (Expr, Default);
4216 Set_Is_Overloaded (Expr, False);
4217 end if;
4218 end if;
4219 end Check_Iterator_Functions;
4221 -------------------------------
4222 -- Check_Primitive_Function --
4223 -------------------------------
4225 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4226 Ctrl : Entity_Id;
4228 begin
4229 if Ekind (Subp) /= E_Function then
4230 return False;
4231 end if;
4233 if No (First_Formal (Subp)) then
4234 return False;
4235 else
4236 Ctrl := Etype (First_Formal (Subp));
4237 end if;
4239 -- To be a primitive operation subprogram has to be in same scope.
4241 if Scope (Ctrl) /= Scope (Subp) then
4242 return False;
4243 end if;
4245 -- Type of formal may be the class-wide type, an access to such,
4246 -- or an incomplete view.
4248 if Ctrl = Ent
4249 or else Ctrl = Class_Wide_Type (Ent)
4250 or else
4251 (Ekind (Ctrl) = E_Anonymous_Access_Type
4252 and then (Designated_Type (Ctrl) = Ent
4253 or else
4254 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4255 or else
4256 (Ekind (Ctrl) = E_Incomplete_Type
4257 and then Full_View (Ctrl) = Ent)
4258 then
4259 null;
4260 else
4261 return False;
4262 end if;
4264 return True;
4265 end Check_Primitive_Function;
4267 ----------------------
4268 -- Duplicate_Clause --
4269 ----------------------
4271 function Duplicate_Clause return Boolean is
4272 A : Node_Id;
4274 begin
4275 -- Nothing to do if this attribute definition clause comes from
4276 -- an aspect specification, since we could not be duplicating an
4277 -- explicit clause, and we dealt with the case of duplicated aspects
4278 -- in Analyze_Aspect_Specifications.
4280 if From_Aspect_Specification (N) then
4281 return False;
4282 end if;
4284 -- Otherwise current clause may duplicate previous clause, or a
4285 -- previously given pragma or aspect specification for the same
4286 -- aspect.
4288 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4290 if Present (A) then
4291 Error_Msg_Name_1 := Chars (N);
4292 Error_Msg_Sloc := Sloc (A);
4294 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4295 return True;
4296 end if;
4298 return False;
4299 end Duplicate_Clause;
4301 -- Start of processing for Analyze_Attribute_Definition_Clause
4303 begin
4304 -- The following code is a defense against recursion. Not clear that
4305 -- this can happen legitimately, but perhaps some error situations can
4306 -- cause it, and we did see this recursion during testing.
4308 if Analyzed (N) then
4309 return;
4310 else
4311 Set_Analyzed (N, True);
4312 end if;
4314 -- Ignore some selected attributes in CodePeer mode since they are not
4315 -- relevant in this context.
4317 if CodePeer_Mode then
4318 case Id is
4320 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4321 -- internal representation of types by implicitly packing them.
4323 when Attribute_Component_Size =>
4324 Rewrite (N, Make_Null_Statement (Sloc (N)));
4325 return;
4327 when others =>
4328 null;
4329 end case;
4330 end if;
4332 -- Process Ignore_Rep_Clauses option
4334 if Ignore_Rep_Clauses then
4335 case Id is
4337 -- The following should be ignored. They do not affect legality
4338 -- and may be target dependent. The basic idea of -gnatI is to
4339 -- ignore any rep clauses that may be target dependent but do not
4340 -- affect legality (except possibly to be rejected because they
4341 -- are incompatible with the compilation target).
4343 when Attribute_Alignment |
4344 Attribute_Bit_Order |
4345 Attribute_Component_Size |
4346 Attribute_Machine_Radix |
4347 Attribute_Object_Size |
4348 Attribute_Size |
4349 Attribute_Small |
4350 Attribute_Stream_Size |
4351 Attribute_Value_Size =>
4352 Kill_Rep_Clause (N);
4353 return;
4355 -- The following should not be ignored, because in the first place
4356 -- they are reasonably portable, and should not cause problems
4357 -- in compiling code from another target, and also they do affect
4358 -- legality, e.g. failing to provide a stream attribute for a type
4359 -- may make a program illegal.
4361 when Attribute_External_Tag |
4362 Attribute_Input |
4363 Attribute_Output |
4364 Attribute_Read |
4365 Attribute_Simple_Storage_Pool |
4366 Attribute_Storage_Pool |
4367 Attribute_Storage_Size |
4368 Attribute_Write =>
4369 null;
4371 -- We do not do anything here with address clauses, they will be
4372 -- removed by Freeze later on, but for now, it works better to
4373 -- keep then in the tree.
4375 when Attribute_Address =>
4376 null;
4378 -- Other cases are errors ("attribute& cannot be set with
4379 -- definition clause"), which will be caught below.
4381 when others =>
4382 null;
4383 end case;
4384 end if;
4386 Analyze (Nam);
4387 Ent := Entity (Nam);
4389 if Rep_Item_Too_Early (Ent, N) then
4390 return;
4391 end if;
4393 -- Rep clause applies to full view of incomplete type or private type if
4394 -- we have one (if not, this is a premature use of the type). However,
4395 -- certain semantic checks need to be done on the specified entity (i.e.
4396 -- the private view), so we save it in Ent.
4398 if Is_Private_Type (Ent)
4399 and then Is_Derived_Type (Ent)
4400 and then not Is_Tagged_Type (Ent)
4401 and then No (Full_View (Ent))
4402 then
4403 -- If this is a private type whose completion is a derivation from
4404 -- another private type, there is no full view, and the attribute
4405 -- belongs to the type itself, not its underlying parent.
4407 U_Ent := Ent;
4409 elsif Ekind (Ent) = E_Incomplete_Type then
4411 -- The attribute applies to the full view, set the entity of the
4412 -- attribute definition accordingly.
4414 Ent := Underlying_Type (Ent);
4415 U_Ent := Ent;
4416 Set_Entity (Nam, Ent);
4418 else
4419 U_Ent := Underlying_Type (Ent);
4420 end if;
4422 -- Avoid cascaded error
4424 if Etype (Nam) = Any_Type then
4425 return;
4427 -- Must be declared in current scope or in case of an aspect
4428 -- specification, must be visible in current scope.
4430 elsif Scope (Ent) /= Current_Scope
4431 and then
4432 not (From_Aspect_Specification (N)
4433 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4434 then
4435 Error_Msg_N ("entity must be declared in this scope", Nam);
4436 return;
4438 -- Must not be a source renaming (we do have some cases where the
4439 -- expander generates a renaming, and those cases are OK, in such
4440 -- cases any attribute applies to the renamed object as well).
4442 elsif Is_Object (Ent)
4443 and then Present (Renamed_Object (Ent))
4444 then
4445 -- Case of renamed object from source, this is an error
4447 if Comes_From_Source (Renamed_Object (Ent)) then
4448 Get_Name_String (Chars (N));
4449 Error_Msg_Strlen := Name_Len;
4450 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4451 Error_Msg_N
4452 ("~ clause not allowed for a renaming declaration "
4453 & "(RM 13.1(6))", Nam);
4454 return;
4456 -- For the case of a compiler generated renaming, the attribute
4457 -- definition clause applies to the renamed object created by the
4458 -- expander. The easiest general way to handle this is to create a
4459 -- copy of the attribute definition clause for this object.
4461 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4462 Insert_Action (N,
4463 Make_Attribute_Definition_Clause (Loc,
4464 Name =>
4465 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4466 Chars => Chars (N),
4467 Expression => Duplicate_Subexpr (Expression (N))));
4469 -- If the renamed object is not an entity, it must be a dereference
4470 -- of an unconstrained function call, and we must introduce a new
4471 -- declaration to capture the expression. This is needed in the case
4472 -- of 'Alignment, where the original declaration must be rewritten.
4474 else
4475 pragma Assert
4476 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4477 null;
4478 end if;
4480 -- If no underlying entity, use entity itself, applies to some
4481 -- previously detected error cases ???
4483 elsif No (U_Ent) then
4484 U_Ent := Ent;
4486 -- Cannot specify for a subtype (exception Object/Value_Size)
4488 elsif Is_Type (U_Ent)
4489 and then not Is_First_Subtype (U_Ent)
4490 and then Id /= Attribute_Object_Size
4491 and then Id /= Attribute_Value_Size
4492 and then not From_At_Mod (N)
4493 then
4494 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4495 return;
4496 end if;
4498 Set_Entity (N, U_Ent);
4499 Check_Restriction_No_Use_Of_Attribute (N);
4501 -- Switch on particular attribute
4503 case Id is
4505 -------------
4506 -- Address --
4507 -------------
4509 -- Address attribute definition clause
4511 when Attribute_Address => Address : begin
4513 -- A little error check, catch for X'Address use X'Address;
4515 if Nkind (Nam) = N_Identifier
4516 and then Nkind (Expr) = N_Attribute_Reference
4517 and then Attribute_Name (Expr) = Name_Address
4518 and then Nkind (Prefix (Expr)) = N_Identifier
4519 and then Chars (Nam) = Chars (Prefix (Expr))
4520 then
4521 Error_Msg_NE
4522 ("address for & is self-referencing", Prefix (Expr), Ent);
4523 return;
4524 end if;
4526 -- Not that special case, carry on with analysis of expression
4528 Analyze_And_Resolve (Expr, RTE (RE_Address));
4530 -- Even when ignoring rep clauses we need to indicate that the
4531 -- entity has an address clause and thus it is legal to declare
4532 -- it imported. Freeze will get rid of the address clause later.
4534 if Ignore_Rep_Clauses then
4535 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4536 Record_Rep_Item (U_Ent, N);
4537 end if;
4539 return;
4540 end if;
4542 if Duplicate_Clause then
4543 null;
4545 -- Case of address clause for subprogram
4547 elsif Is_Subprogram (U_Ent) then
4548 if Has_Homonym (U_Ent) then
4549 Error_Msg_N
4550 ("address clause cannot be given " &
4551 "for overloaded subprogram",
4552 Nam);
4553 return;
4554 end if;
4556 -- For subprograms, all address clauses are permitted, and we
4557 -- mark the subprogram as having a deferred freeze so that Gigi
4558 -- will not elaborate it too soon.
4560 -- Above needs more comments, what is too soon about???
4562 Set_Has_Delayed_Freeze (U_Ent);
4564 -- Case of address clause for entry
4566 elsif Ekind (U_Ent) = E_Entry then
4567 if Nkind (Parent (N)) = N_Task_Body then
4568 Error_Msg_N
4569 ("entry address must be specified in task spec", Nam);
4570 return;
4571 end if;
4573 -- For entries, we require a constant address
4575 Check_Constant_Address_Clause (Expr, U_Ent);
4577 -- Special checks for task types
4579 if Is_Task_Type (Scope (U_Ent))
4580 and then Comes_From_Source (Scope (U_Ent))
4581 then
4582 Error_Msg_N
4583 ("??entry address declared for entry in task type", N);
4584 Error_Msg_N
4585 ("\??only one task can be declared of this type", N);
4586 end if;
4588 -- Entry address clauses are obsolescent
4590 Check_Restriction (No_Obsolescent_Features, N);
4592 if Warn_On_Obsolescent_Feature then
4593 Error_Msg_N
4594 ("?j?attaching interrupt to task entry is an " &
4595 "obsolescent feature (RM J.7.1)", N);
4596 Error_Msg_N
4597 ("\?j?use interrupt procedure instead", N);
4598 end if;
4600 -- Case of an address clause for a controlled object which we
4601 -- consider to be erroneous.
4603 elsif Is_Controlled (Etype (U_Ent))
4604 or else Has_Controlled_Component (Etype (U_Ent))
4605 then
4606 Error_Msg_NE
4607 ("??controlled object& must not be overlaid", Nam, U_Ent);
4608 Error_Msg_N
4609 ("\??Program_Error will be raised at run time", Nam);
4610 Insert_Action (Declaration_Node (U_Ent),
4611 Make_Raise_Program_Error (Loc,
4612 Reason => PE_Overlaid_Controlled_Object));
4613 return;
4615 -- Case of address clause for a (non-controlled) object
4617 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4618 declare
4619 Expr : constant Node_Id := Expression (N);
4620 O_Ent : Entity_Id;
4621 Off : Boolean;
4623 begin
4624 -- Exported variables cannot have an address clause, because
4625 -- this cancels the effect of the pragma Export.
4627 if Is_Exported (U_Ent) then
4628 Error_Msg_N
4629 ("cannot export object with address clause", Nam);
4630 return;
4631 end if;
4633 Find_Overlaid_Entity (N, O_Ent, Off);
4635 -- Overlaying controlled objects is erroneous
4637 if Present (O_Ent)
4638 and then (Has_Controlled_Component (Etype (O_Ent))
4639 or else Is_Controlled (Etype (O_Ent)))
4640 then
4641 Error_Msg_N
4642 ("??cannot overlay with controlled object", Expr);
4643 Error_Msg_N
4644 ("\??Program_Error will be raised at run time", Expr);
4645 Insert_Action (Declaration_Node (U_Ent),
4646 Make_Raise_Program_Error (Loc,
4647 Reason => PE_Overlaid_Controlled_Object));
4648 return;
4650 elsif Present (O_Ent)
4651 and then Ekind (U_Ent) = E_Constant
4652 and then not Is_Constant_Object (O_Ent)
4653 then
4654 Error_Msg_N ("??constant overlays a variable", Expr);
4656 -- Imported variables can have an address clause, but then
4657 -- the import is pretty meaningless except to suppress
4658 -- initializations, so we do not need such variables to
4659 -- be statically allocated (and in fact it causes trouble
4660 -- if the address clause is a local value).
4662 elsif Is_Imported (U_Ent) then
4663 Set_Is_Statically_Allocated (U_Ent, False);
4664 end if;
4666 -- We mark a possible modification of a variable with an
4667 -- address clause, since it is likely aliasing is occurring.
4669 Note_Possible_Modification (Nam, Sure => False);
4671 -- Here we are checking for explicit overlap of one variable
4672 -- by another, and if we find this then mark the overlapped
4673 -- variable as also being volatile to prevent unwanted
4674 -- optimizations. This is a significant pessimization so
4675 -- avoid it when there is an offset, i.e. when the object
4676 -- is composite; they cannot be optimized easily anyway.
4678 if Present (O_Ent)
4679 and then Is_Object (O_Ent)
4680 and then not Off
4682 -- The following test is an expedient solution to what
4683 -- is really a problem in CodePeer. Suppressing the
4684 -- Set_Treat_As_Volatile call here prevents later
4685 -- generation (in some cases) of trees that CodePeer
4686 -- should, but currently does not, handle correctly.
4687 -- This test should probably be removed when CodePeer
4688 -- is improved, just because we want the tree CodePeer
4689 -- analyzes to match the tree for which we generate code
4690 -- as closely as is practical. ???
4692 and then not CodePeer_Mode
4693 then
4694 -- ??? O_Ent might not be in current unit
4696 Set_Treat_As_Volatile (O_Ent);
4697 end if;
4699 -- Legality checks on the address clause for initialized
4700 -- objects is deferred until the freeze point, because
4701 -- a subsequent pragma might indicate that the object
4702 -- is imported and thus not initialized. Also, the address
4703 -- clause might involve entities that have yet to be
4704 -- elaborated.
4706 Set_Has_Delayed_Freeze (U_Ent);
4708 -- If an initialization call has been generated for this
4709 -- object, it needs to be deferred to after the freeze node
4710 -- we have just now added, otherwise GIGI will see a
4711 -- reference to the variable (as actual to the IP call)
4712 -- before its definition.
4714 declare
4715 Init_Call : constant Node_Id :=
4716 Remove_Init_Call (U_Ent, N);
4718 begin
4719 if Present (Init_Call) then
4720 Append_Freeze_Action (U_Ent, Init_Call);
4722 -- Reset Initialization_Statements pointer so that
4723 -- if there is a pragma Import further down, it can
4724 -- clear any default initialization.
4726 Set_Initialization_Statements (U_Ent, Init_Call);
4727 end if;
4728 end;
4730 if Is_Exported (U_Ent) then
4731 Error_Msg_N
4732 ("& cannot be exported if an address clause is given",
4733 Nam);
4734 Error_Msg_N
4735 ("\define and export a variable "
4736 & "that holds its address instead", Nam);
4737 end if;
4739 -- Entity has delayed freeze, so we will generate an
4740 -- alignment check at the freeze point unless suppressed.
4742 if not Range_Checks_Suppressed (U_Ent)
4743 and then not Alignment_Checks_Suppressed (U_Ent)
4744 then
4745 Set_Check_Address_Alignment (N);
4746 end if;
4748 -- Kill the size check code, since we are not allocating
4749 -- the variable, it is somewhere else.
4751 Kill_Size_Check_Code (U_Ent);
4753 -- If the address clause is of the form:
4755 -- for Y'Address use X'Address
4757 -- or
4759 -- Const : constant Address := X'Address;
4760 -- ...
4761 -- for Y'Address use Const;
4763 -- then we make an entry in the table for checking the size
4764 -- and alignment of the overlaying variable. We defer this
4765 -- check till after code generation to take full advantage
4766 -- of the annotation done by the back end.
4768 -- If the entity has a generic type, the check will be
4769 -- performed in the instance if the actual type justifies
4770 -- it, and we do not insert the clause in the table to
4771 -- prevent spurious warnings.
4773 -- Note: we used to test Comes_From_Source and only give
4774 -- this warning for source entities, but we have removed
4775 -- this test. It really seems bogus to generate overlays
4776 -- that would trigger this warning in generated code.
4777 -- Furthermore, by removing the test, we handle the
4778 -- aspect case properly.
4780 if Address_Clause_Overlay_Warnings
4781 and then Present (O_Ent)
4782 and then Is_Object (O_Ent)
4783 then
4784 if not Is_Generic_Type (Etype (U_Ent)) then
4785 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4786 end if;
4788 -- If variable overlays a constant view, and we are
4789 -- warning on overlays, then mark the variable as
4790 -- overlaying a constant (we will give warnings later
4791 -- if this variable is assigned).
4793 if Is_Constant_Object (O_Ent)
4794 and then Ekind (U_Ent) = E_Variable
4795 then
4796 Set_Overlays_Constant (U_Ent);
4797 end if;
4798 end if;
4799 end;
4801 -- Not a valid entity for an address clause
4803 else
4804 Error_Msg_N ("address cannot be given for &", Nam);
4805 end if;
4806 end Address;
4808 ---------------
4809 -- Alignment --
4810 ---------------
4812 -- Alignment attribute definition clause
4814 when Attribute_Alignment => Alignment : declare
4815 Align : constant Uint := Get_Alignment_Value (Expr);
4816 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4818 begin
4819 FOnly := True;
4821 if not Is_Type (U_Ent)
4822 and then Ekind (U_Ent) /= E_Variable
4823 and then Ekind (U_Ent) /= E_Constant
4824 then
4825 Error_Msg_N ("alignment cannot be given for &", Nam);
4827 elsif Duplicate_Clause then
4828 null;
4830 elsif Align /= No_Uint then
4831 Set_Has_Alignment_Clause (U_Ent);
4833 -- Tagged type case, check for attempt to set alignment to a
4834 -- value greater than Max_Align, and reset if so.
4836 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4837 Error_Msg_N
4838 ("alignment for & set to Maximum_Aligment??", Nam);
4839 Set_Alignment (U_Ent, Max_Align);
4841 -- All other cases
4843 else
4844 Set_Alignment (U_Ent, Align);
4845 end if;
4847 -- For an array type, U_Ent is the first subtype. In that case,
4848 -- also set the alignment of the anonymous base type so that
4849 -- other subtypes (such as the itypes for aggregates of the
4850 -- type) also receive the expected alignment.
4852 if Is_Array_Type (U_Ent) then
4853 Set_Alignment (Base_Type (U_Ent), Align);
4854 end if;
4855 end if;
4856 end Alignment;
4858 ---------------
4859 -- Bit_Order --
4860 ---------------
4862 -- Bit_Order attribute definition clause
4864 when Attribute_Bit_Order => Bit_Order : declare
4865 begin
4866 if not Is_Record_Type (U_Ent) then
4867 Error_Msg_N
4868 ("Bit_Order can only be defined for record type", Nam);
4870 elsif Duplicate_Clause then
4871 null;
4873 else
4874 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4876 if Etype (Expr) = Any_Type then
4877 return;
4879 elsif not Is_OK_Static_Expression (Expr) then
4880 Flag_Non_Static_Expr
4881 ("Bit_Order requires static expression!", Expr);
4883 else
4884 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4885 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4886 end if;
4887 end if;
4888 end if;
4889 end Bit_Order;
4891 --------------------
4892 -- Component_Size --
4893 --------------------
4895 -- Component_Size attribute definition clause
4897 when Attribute_Component_Size => Component_Size_Case : declare
4898 Csize : constant Uint := Static_Integer (Expr);
4899 Ctyp : Entity_Id;
4900 Btype : Entity_Id;
4901 Biased : Boolean;
4902 New_Ctyp : Entity_Id;
4903 Decl : Node_Id;
4905 begin
4906 if not Is_Array_Type (U_Ent) then
4907 Error_Msg_N ("component size requires array type", Nam);
4908 return;
4909 end if;
4911 Btype := Base_Type (U_Ent);
4912 Ctyp := Component_Type (Btype);
4914 if Duplicate_Clause then
4915 null;
4917 elsif Rep_Item_Too_Early (Btype, N) then
4918 null;
4920 elsif Csize /= No_Uint then
4921 Check_Size (Expr, Ctyp, Csize, Biased);
4923 -- For the biased case, build a declaration for a subtype that
4924 -- will be used to represent the biased subtype that reflects
4925 -- the biased representation of components. We need the subtype
4926 -- to get proper conversions on referencing elements of the
4927 -- array. Note: component size clauses are ignored in VM mode.
4929 if VM_Target = No_VM then
4930 if Biased then
4931 New_Ctyp :=
4932 Make_Defining_Identifier (Loc,
4933 Chars =>
4934 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4936 Decl :=
4937 Make_Subtype_Declaration (Loc,
4938 Defining_Identifier => New_Ctyp,
4939 Subtype_Indication =>
4940 New_Occurrence_Of (Component_Type (Btype), Loc));
4942 Set_Parent (Decl, N);
4943 Analyze (Decl, Suppress => All_Checks);
4945 Set_Has_Delayed_Freeze (New_Ctyp, False);
4946 Set_Esize (New_Ctyp, Csize);
4947 Set_RM_Size (New_Ctyp, Csize);
4948 Init_Alignment (New_Ctyp);
4949 Set_Is_Itype (New_Ctyp, True);
4950 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4952 Set_Component_Type (Btype, New_Ctyp);
4953 Set_Biased (New_Ctyp, N, "component size clause");
4954 end if;
4956 Set_Component_Size (Btype, Csize);
4958 -- For VM case, we ignore component size clauses
4960 else
4961 -- Give a warning unless we are in GNAT mode, in which case
4962 -- the warning is suppressed since it is not useful.
4964 if not GNAT_Mode then
4965 Error_Msg_N
4966 ("component size ignored in this configuration??", N);
4967 end if;
4968 end if;
4970 -- Deal with warning on overridden size
4972 if Warn_On_Overridden_Size
4973 and then Has_Size_Clause (Ctyp)
4974 and then RM_Size (Ctyp) /= Csize
4975 then
4976 Error_Msg_NE
4977 ("component size overrides size clause for&?S?", N, Ctyp);
4978 end if;
4980 Set_Has_Component_Size_Clause (Btype, True);
4981 Set_Has_Non_Standard_Rep (Btype, True);
4982 end if;
4983 end Component_Size_Case;
4985 -----------------------
4986 -- Constant_Indexing --
4987 -----------------------
4989 when Attribute_Constant_Indexing =>
4990 Check_Indexing_Functions;
4992 ---------
4993 -- CPU --
4994 ---------
4996 when Attribute_CPU => CPU :
4997 begin
4998 -- CPU attribute definition clause not allowed except from aspect
4999 -- specification.
5001 if From_Aspect_Specification (N) then
5002 if not Is_Task_Type (U_Ent) then
5003 Error_Msg_N ("CPU can only be defined for task", Nam);
5005 elsif Duplicate_Clause then
5006 null;
5008 else
5009 -- The expression must be analyzed in the special manner
5010 -- described in "Handling of Default and Per-Object
5011 -- Expressions" in sem.ads.
5013 -- The visibility to the discriminants must be restored
5015 Push_Scope_And_Install_Discriminants (U_Ent);
5016 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5017 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5019 if not Is_OK_Static_Expression (Expr) then
5020 Check_Restriction (Static_Priorities, Expr);
5021 end if;
5022 end if;
5024 else
5025 Error_Msg_N
5026 ("attribute& cannot be set with definition clause", N);
5027 end if;
5028 end CPU;
5030 ----------------------
5031 -- Default_Iterator --
5032 ----------------------
5034 when Attribute_Default_Iterator => Default_Iterator : declare
5035 Func : Entity_Id;
5036 Typ : Entity_Id;
5038 begin
5039 -- If target type is untagged, further checks are irrelevant
5041 if not Is_Tagged_Type (U_Ent) then
5042 Error_Msg_N
5043 ("aspect Default_Iterator applies to tagged type", Nam);
5044 return;
5045 end if;
5047 Check_Iterator_Functions;
5049 Analyze (Expr);
5051 if not Is_Entity_Name (Expr)
5052 or else Ekind (Entity (Expr)) /= E_Function
5053 then
5054 Error_Msg_N ("aspect Iterator must be a function", Expr);
5055 return;
5056 else
5057 Func := Entity (Expr);
5058 end if;
5060 -- The type of the first parameter must be T, T'class, or a
5061 -- corresponding access type (5.5.1 (8/3). If function is
5062 -- parameterless label type accordingly.
5064 if No (First_Formal (Func)) then
5065 Typ := Any_Type;
5066 else
5067 Typ := Etype (First_Formal (Func));
5068 end if;
5070 if Typ = U_Ent
5071 or else Typ = Class_Wide_Type (U_Ent)
5072 or else (Is_Access_Type (Typ)
5073 and then Designated_Type (Typ) = U_Ent)
5074 or else (Is_Access_Type (Typ)
5075 and then Designated_Type (Typ) =
5076 Class_Wide_Type (U_Ent))
5077 then
5078 null;
5080 else
5081 Error_Msg_NE
5082 ("Default Iterator must be a primitive of&", Func, U_Ent);
5083 end if;
5084 end Default_Iterator;
5086 ------------------------
5087 -- Dispatching_Domain --
5088 ------------------------
5090 when Attribute_Dispatching_Domain => Dispatching_Domain :
5091 begin
5092 -- Dispatching_Domain attribute definition clause not allowed
5093 -- except from aspect specification.
5095 if From_Aspect_Specification (N) then
5096 if not Is_Task_Type (U_Ent) then
5097 Error_Msg_N
5098 ("Dispatching_Domain can only be defined for task", Nam);
5100 elsif Duplicate_Clause then
5101 null;
5103 else
5104 -- The expression must be analyzed in the special manner
5105 -- described in "Handling of Default and Per-Object
5106 -- Expressions" in sem.ads.
5108 -- The visibility to the discriminants must be restored
5110 Push_Scope_And_Install_Discriminants (U_Ent);
5112 Preanalyze_Spec_Expression
5113 (Expr, RTE (RE_Dispatching_Domain));
5115 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5116 end if;
5118 else
5119 Error_Msg_N
5120 ("attribute& cannot be set with definition clause", N);
5121 end if;
5122 end Dispatching_Domain;
5124 ------------------
5125 -- External_Tag --
5126 ------------------
5128 when Attribute_External_Tag => External_Tag :
5129 begin
5130 if not Is_Tagged_Type (U_Ent) then
5131 Error_Msg_N ("should be a tagged type", Nam);
5132 end if;
5134 if Duplicate_Clause then
5135 null;
5137 else
5138 Analyze_And_Resolve (Expr, Standard_String);
5140 if not Is_OK_Static_Expression (Expr) then
5141 Flag_Non_Static_Expr
5142 ("static string required for tag name!", Nam);
5143 end if;
5145 if VM_Target /= No_VM then
5146 Error_Msg_Name_1 := Attr;
5147 Error_Msg_N
5148 ("% attribute unsupported in this configuration", Nam);
5149 end if;
5151 if not Is_Library_Level_Entity (U_Ent) then
5152 Error_Msg_NE
5153 ("??non-unique external tag supplied for &", N, U_Ent);
5154 Error_Msg_N
5155 ("\??same external tag applies to all "
5156 & "subprogram calls", N);
5157 Error_Msg_N
5158 ("\??corresponding internal tag cannot be obtained", N);
5159 end if;
5160 end if;
5161 end External_Tag;
5163 --------------------------
5164 -- Implicit_Dereference --
5165 --------------------------
5167 when Attribute_Implicit_Dereference =>
5169 -- Legality checks already performed at the point of the type
5170 -- declaration, aspect is not delayed.
5172 null;
5174 -----------
5175 -- Input --
5176 -----------
5178 when Attribute_Input =>
5179 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5180 Set_Has_Specified_Stream_Input (Ent);
5182 ------------------------
5183 -- Interrupt_Priority --
5184 ------------------------
5186 when Attribute_Interrupt_Priority => Interrupt_Priority :
5187 begin
5188 -- Interrupt_Priority attribute definition clause not allowed
5189 -- except from aspect specification.
5191 if From_Aspect_Specification (N) then
5192 if not Is_Concurrent_Type (U_Ent) then
5193 Error_Msg_N
5194 ("Interrupt_Priority can only be defined for task "
5195 & "and protected object", Nam);
5197 elsif Duplicate_Clause then
5198 null;
5200 else
5201 -- The expression must be analyzed in the special manner
5202 -- described in "Handling of Default and Per-Object
5203 -- Expressions" in sem.ads.
5205 -- The visibility to the discriminants must be restored
5207 Push_Scope_And_Install_Discriminants (U_Ent);
5209 Preanalyze_Spec_Expression
5210 (Expr, RTE (RE_Interrupt_Priority));
5212 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5213 end if;
5215 else
5216 Error_Msg_N
5217 ("attribute& cannot be set with definition clause", N);
5218 end if;
5219 end Interrupt_Priority;
5221 --------------
5222 -- Iterable --
5223 --------------
5225 when Attribute_Iterable =>
5226 Analyze (Expr);
5228 if Nkind (Expr) /= N_Aggregate then
5229 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5230 end if;
5232 declare
5233 Assoc : Node_Id;
5235 begin
5236 Assoc := First (Component_Associations (Expr));
5237 while Present (Assoc) loop
5238 if not Is_Entity_Name (Expression (Assoc)) then
5239 Error_Msg_N ("value must be a function", Assoc);
5240 end if;
5242 Next (Assoc);
5243 end loop;
5244 end;
5246 ----------------------
5247 -- Iterator_Element --
5248 ----------------------
5250 when Attribute_Iterator_Element =>
5251 Analyze (Expr);
5253 if not Is_Entity_Name (Expr)
5254 or else not Is_Type (Entity (Expr))
5255 then
5256 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5257 end if;
5259 -------------------
5260 -- Machine_Radix --
5261 -------------------
5263 -- Machine radix attribute definition clause
5265 when Attribute_Machine_Radix => Machine_Radix : declare
5266 Radix : constant Uint := Static_Integer (Expr);
5268 begin
5269 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5270 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5272 elsif Duplicate_Clause then
5273 null;
5275 elsif Radix /= No_Uint then
5276 Set_Has_Machine_Radix_Clause (U_Ent);
5277 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5279 if Radix = 2 then
5280 null;
5281 elsif Radix = 10 then
5282 Set_Machine_Radix_10 (U_Ent);
5283 else
5284 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5285 end if;
5286 end if;
5287 end Machine_Radix;
5289 -----------------
5290 -- Object_Size --
5291 -----------------
5293 -- Object_Size attribute definition clause
5295 when Attribute_Object_Size => Object_Size : declare
5296 Size : constant Uint := Static_Integer (Expr);
5298 Biased : Boolean;
5299 pragma Warnings (Off, Biased);
5301 begin
5302 if not Is_Type (U_Ent) then
5303 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5305 elsif Duplicate_Clause then
5306 null;
5308 else
5309 Check_Size (Expr, U_Ent, Size, Biased);
5311 if Is_Scalar_Type (U_Ent) then
5312 if Size /= 8 and then Size /= 16 and then Size /= 32
5313 and then UI_Mod (Size, 64) /= 0
5314 then
5315 Error_Msg_N
5316 ("Object_Size must be 8, 16, 32, or multiple of 64",
5317 Expr);
5318 end if;
5320 elsif Size mod 8 /= 0 then
5321 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5322 end if;
5324 Set_Esize (U_Ent, Size);
5325 Set_Has_Object_Size_Clause (U_Ent);
5326 Alignment_Check_For_Size_Change (U_Ent, Size);
5327 end if;
5328 end Object_Size;
5330 ------------
5331 -- Output --
5332 ------------
5334 when Attribute_Output =>
5335 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5336 Set_Has_Specified_Stream_Output (Ent);
5338 --------------
5339 -- Priority --
5340 --------------
5342 when Attribute_Priority => Priority :
5343 begin
5344 -- Priority attribute definition clause not allowed except from
5345 -- aspect specification.
5347 if From_Aspect_Specification (N) then
5348 if not (Is_Concurrent_Type (U_Ent)
5349 or else Ekind (U_Ent) = E_Procedure)
5350 then
5351 Error_Msg_N
5352 ("Priority can only be defined for task and protected "
5353 & "object", Nam);
5355 elsif Duplicate_Clause then
5356 null;
5358 else
5359 -- The expression must be analyzed in the special manner
5360 -- described in "Handling of Default and Per-Object
5361 -- Expressions" in sem.ads.
5363 -- The visibility to the discriminants must be restored
5365 Push_Scope_And_Install_Discriminants (U_Ent);
5366 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5367 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5369 if not Is_OK_Static_Expression (Expr) then
5370 Check_Restriction (Static_Priorities, Expr);
5371 end if;
5372 end if;
5374 else
5375 Error_Msg_N
5376 ("attribute& cannot be set with definition clause", N);
5377 end if;
5378 end Priority;
5380 ----------
5381 -- Read --
5382 ----------
5384 when Attribute_Read =>
5385 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5386 Set_Has_Specified_Stream_Read (Ent);
5388 --------------------------
5389 -- Scalar_Storage_Order --
5390 --------------------------
5392 -- Scalar_Storage_Order attribute definition clause
5394 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5395 begin
5396 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5397 Error_Msg_N
5398 ("Scalar_Storage_Order can only be defined for "
5399 & "record or array type", Nam);
5401 elsif Duplicate_Clause then
5402 null;
5404 else
5405 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5407 if Etype (Expr) = Any_Type then
5408 return;
5410 elsif not Is_OK_Static_Expression (Expr) then
5411 Flag_Non_Static_Expr
5412 ("Scalar_Storage_Order requires static expression!", Expr);
5414 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5416 -- Here for the case of a non-default (i.e. non-confirming)
5417 -- Scalar_Storage_Order attribute definition.
5419 if Support_Nondefault_SSO_On_Target then
5420 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5421 else
5422 Error_Msg_N
5423 ("non-default Scalar_Storage_Order "
5424 & "not supported on target", Expr);
5425 end if;
5426 end if;
5428 -- Clear SSO default indications since explicit setting of the
5429 -- order overrides the defaults.
5431 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5432 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5433 end if;
5434 end Scalar_Storage_Order;
5436 ----------
5437 -- Size --
5438 ----------
5440 -- Size attribute definition clause
5442 when Attribute_Size => Size : declare
5443 Size : constant Uint := Static_Integer (Expr);
5444 Etyp : Entity_Id;
5445 Biased : Boolean;
5447 begin
5448 FOnly := True;
5450 if Duplicate_Clause then
5451 null;
5453 elsif not Is_Type (U_Ent)
5454 and then Ekind (U_Ent) /= E_Variable
5455 and then Ekind (U_Ent) /= E_Constant
5456 then
5457 Error_Msg_N ("size cannot be given for &", Nam);
5459 elsif Is_Array_Type (U_Ent)
5460 and then not Is_Constrained (U_Ent)
5461 then
5462 Error_Msg_N
5463 ("size cannot be given for unconstrained array", Nam);
5465 elsif Size /= No_Uint then
5466 if VM_Target /= No_VM and then not GNAT_Mode then
5468 -- Size clause is not handled properly on VM targets.
5469 -- Display a warning unless we are in GNAT mode, in which
5470 -- case this is useless.
5472 Error_Msg_N
5473 ("size clauses are ignored in this configuration??", N);
5474 end if;
5476 if Is_Type (U_Ent) then
5477 Etyp := U_Ent;
5478 else
5479 Etyp := Etype (U_Ent);
5480 end if;
5482 -- Check size, note that Gigi is in charge of checking that the
5483 -- size of an array or record type is OK. Also we do not check
5484 -- the size in the ordinary fixed-point case, since it is too
5485 -- early to do so (there may be subsequent small clause that
5486 -- affects the size). We can check the size if a small clause
5487 -- has already been given.
5489 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5490 or else Has_Small_Clause (U_Ent)
5491 then
5492 Check_Size (Expr, Etyp, Size, Biased);
5493 Set_Biased (U_Ent, N, "size clause", Biased);
5494 end if;
5496 -- For types set RM_Size and Esize if possible
5498 if Is_Type (U_Ent) then
5499 Set_RM_Size (U_Ent, Size);
5501 -- For elementary types, increase Object_Size to power of 2,
5502 -- but not less than a storage unit in any case (normally
5503 -- this means it will be byte addressable).
5505 -- For all other types, nothing else to do, we leave Esize
5506 -- (object size) unset, the back end will set it from the
5507 -- size and alignment in an appropriate manner.
5509 -- In both cases, we check whether the alignment must be
5510 -- reset in the wake of the size change.
5512 if Is_Elementary_Type (U_Ent) then
5513 if Size <= System_Storage_Unit then
5514 Init_Esize (U_Ent, System_Storage_Unit);
5515 elsif Size <= 16 then
5516 Init_Esize (U_Ent, 16);
5517 elsif Size <= 32 then
5518 Init_Esize (U_Ent, 32);
5519 else
5520 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5521 end if;
5523 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5524 else
5525 Alignment_Check_For_Size_Change (U_Ent, Size);
5526 end if;
5528 -- For objects, set Esize only
5530 else
5531 if Is_Elementary_Type (Etyp) then
5532 if Size /= System_Storage_Unit
5533 and then
5534 Size /= System_Storage_Unit * 2
5535 and then
5536 Size /= System_Storage_Unit * 4
5537 and then
5538 Size /= System_Storage_Unit * 8
5539 then
5540 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5541 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5542 Error_Msg_N
5543 ("size for primitive object must be a power of 2"
5544 & " in the range ^-^", N);
5545 end if;
5546 end if;
5548 Set_Esize (U_Ent, Size);
5549 end if;
5551 Set_Has_Size_Clause (U_Ent);
5552 end if;
5553 end Size;
5555 -----------
5556 -- Small --
5557 -----------
5559 -- Small attribute definition clause
5561 when Attribute_Small => Small : declare
5562 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5563 Small : Ureal;
5565 begin
5566 Analyze_And_Resolve (Expr, Any_Real);
5568 if Etype (Expr) = Any_Type then
5569 return;
5571 elsif not Is_OK_Static_Expression (Expr) then
5572 Flag_Non_Static_Expr
5573 ("small requires static expression!", Expr);
5574 return;
5576 else
5577 Small := Expr_Value_R (Expr);
5579 if Small <= Ureal_0 then
5580 Error_Msg_N ("small value must be greater than zero", Expr);
5581 return;
5582 end if;
5584 end if;
5586 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5587 Error_Msg_N
5588 ("small requires an ordinary fixed point type", Nam);
5590 elsif Has_Small_Clause (U_Ent) then
5591 Error_Msg_N ("small already given for &", Nam);
5593 elsif Small > Delta_Value (U_Ent) then
5594 Error_Msg_N
5595 ("small value must not be greater than delta value", Nam);
5597 else
5598 Set_Small_Value (U_Ent, Small);
5599 Set_Small_Value (Implicit_Base, Small);
5600 Set_Has_Small_Clause (U_Ent);
5601 Set_Has_Small_Clause (Implicit_Base);
5602 Set_Has_Non_Standard_Rep (Implicit_Base);
5603 end if;
5604 end Small;
5606 ------------------
5607 -- Storage_Pool --
5608 ------------------
5610 -- Storage_Pool attribute definition clause
5612 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5613 Pool : Entity_Id;
5614 T : Entity_Id;
5616 begin
5617 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5618 Error_Msg_N
5619 ("storage pool cannot be given for access-to-subprogram type",
5620 Nam);
5621 return;
5623 elsif not
5624 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5625 then
5626 Error_Msg_N
5627 ("storage pool can only be given for access types", Nam);
5628 return;
5630 elsif Is_Derived_Type (U_Ent) then
5631 Error_Msg_N
5632 ("storage pool cannot be given for a derived access type",
5633 Nam);
5635 elsif Duplicate_Clause then
5636 return;
5638 elsif Present (Associated_Storage_Pool (U_Ent)) then
5639 Error_Msg_N ("storage pool already given for &", Nam);
5640 return;
5641 end if;
5643 -- Check for Storage_Size previously given
5645 declare
5646 SS : constant Node_Id :=
5647 Get_Attribute_Definition_Clause
5648 (U_Ent, Attribute_Storage_Size);
5649 begin
5650 if Present (SS) then
5651 Check_Pool_Size_Clash (U_Ent, N, SS);
5652 end if;
5653 end;
5655 -- Storage_Pool case
5657 if Id = Attribute_Storage_Pool then
5658 Analyze_And_Resolve
5659 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5661 -- In the Simple_Storage_Pool case, we allow a variable of any
5662 -- simple storage pool type, so we Resolve without imposing an
5663 -- expected type.
5665 else
5666 Analyze_And_Resolve (Expr);
5668 if not Present (Get_Rep_Pragma
5669 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5670 then
5671 Error_Msg_N
5672 ("expression must be of a simple storage pool type", Expr);
5673 end if;
5674 end if;
5676 if not Denotes_Variable (Expr) then
5677 Error_Msg_N ("storage pool must be a variable", Expr);
5678 return;
5679 end if;
5681 if Nkind (Expr) = N_Type_Conversion then
5682 T := Etype (Expression (Expr));
5683 else
5684 T := Etype (Expr);
5685 end if;
5687 -- The Stack_Bounded_Pool is used internally for implementing
5688 -- access types with a Storage_Size. Since it only work properly
5689 -- when used on one specific type, we need to check that it is not
5690 -- hijacked improperly:
5692 -- type T is access Integer;
5693 -- for T'Storage_Size use n;
5694 -- type Q is access Float;
5695 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5697 if RTE_Available (RE_Stack_Bounded_Pool)
5698 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5699 then
5700 Error_Msg_N ("non-shareable internal Pool", Expr);
5701 return;
5702 end if;
5704 -- If the argument is a name that is not an entity name, then
5705 -- we construct a renaming operation to define an entity of
5706 -- type storage pool.
5708 if not Is_Entity_Name (Expr)
5709 and then Is_Object_Reference (Expr)
5710 then
5711 Pool := Make_Temporary (Loc, 'P', Expr);
5713 declare
5714 Rnode : constant Node_Id :=
5715 Make_Object_Renaming_Declaration (Loc,
5716 Defining_Identifier => Pool,
5717 Subtype_Mark =>
5718 New_Occurrence_Of (Etype (Expr), Loc),
5719 Name => Expr);
5721 begin
5722 -- If the attribute definition clause comes from an aspect
5723 -- clause, then insert the renaming before the associated
5724 -- entity's declaration, since the attribute clause has
5725 -- not yet been appended to the declaration list.
5727 if From_Aspect_Specification (N) then
5728 Insert_Before (Parent (Entity (N)), Rnode);
5729 else
5730 Insert_Before (N, Rnode);
5731 end if;
5733 Analyze (Rnode);
5734 Set_Associated_Storage_Pool (U_Ent, Pool);
5735 end;
5737 elsif Is_Entity_Name (Expr) then
5738 Pool := Entity (Expr);
5740 -- If pool is a renamed object, get original one. This can
5741 -- happen with an explicit renaming, and within instances.
5743 while Present (Renamed_Object (Pool))
5744 and then Is_Entity_Name (Renamed_Object (Pool))
5745 loop
5746 Pool := Entity (Renamed_Object (Pool));
5747 end loop;
5749 if Present (Renamed_Object (Pool))
5750 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5751 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5752 then
5753 Pool := Entity (Expression (Renamed_Object (Pool)));
5754 end if;
5756 Set_Associated_Storage_Pool (U_Ent, Pool);
5758 elsif Nkind (Expr) = N_Type_Conversion
5759 and then Is_Entity_Name (Expression (Expr))
5760 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5761 then
5762 Pool := Entity (Expression (Expr));
5763 Set_Associated_Storage_Pool (U_Ent, Pool);
5765 else
5766 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5767 return;
5768 end if;
5769 end;
5771 ------------------
5772 -- Storage_Size --
5773 ------------------
5775 -- Storage_Size attribute definition clause
5777 when Attribute_Storage_Size => Storage_Size : declare
5778 Btype : constant Entity_Id := Base_Type (U_Ent);
5780 begin
5781 if Is_Task_Type (U_Ent) then
5783 -- Check obsolescent (but never obsolescent if from aspect)
5785 if not From_Aspect_Specification (N) then
5786 Check_Restriction (No_Obsolescent_Features, N);
5788 if Warn_On_Obsolescent_Feature then
5789 Error_Msg_N
5790 ("?j?storage size clause for task is an " &
5791 "obsolescent feature (RM J.9)", N);
5792 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5793 end if;
5794 end if;
5796 FOnly := True;
5797 end if;
5799 if not Is_Access_Type (U_Ent)
5800 and then Ekind (U_Ent) /= E_Task_Type
5801 then
5802 Error_Msg_N ("storage size cannot be given for &", Nam);
5804 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5805 Error_Msg_N
5806 ("storage size cannot be given for a derived access type",
5807 Nam);
5809 elsif Duplicate_Clause then
5810 null;
5812 else
5813 Analyze_And_Resolve (Expr, Any_Integer);
5815 if Is_Access_Type (U_Ent) then
5817 -- Check for Storage_Pool previously given
5819 declare
5820 SP : constant Node_Id :=
5821 Get_Attribute_Definition_Clause
5822 (U_Ent, Attribute_Storage_Pool);
5824 begin
5825 if Present (SP) then
5826 Check_Pool_Size_Clash (U_Ent, SP, N);
5827 end if;
5828 end;
5830 -- Special case of for x'Storage_Size use 0
5832 if Is_OK_Static_Expression (Expr)
5833 and then Expr_Value (Expr) = 0
5834 then
5835 Set_No_Pool_Assigned (Btype);
5836 end if;
5837 end if;
5839 Set_Has_Storage_Size_Clause (Btype);
5840 end if;
5841 end Storage_Size;
5843 -----------------
5844 -- Stream_Size --
5845 -----------------
5847 when Attribute_Stream_Size => Stream_Size : declare
5848 Size : constant Uint := Static_Integer (Expr);
5850 begin
5851 if Ada_Version <= Ada_95 then
5852 Check_Restriction (No_Implementation_Attributes, N);
5853 end if;
5855 if Duplicate_Clause then
5856 null;
5858 elsif Is_Elementary_Type (U_Ent) then
5859 if Size /= System_Storage_Unit
5860 and then
5861 Size /= System_Storage_Unit * 2
5862 and then
5863 Size /= System_Storage_Unit * 4
5864 and then
5865 Size /= System_Storage_Unit * 8
5866 then
5867 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5868 Error_Msg_N
5869 ("stream size for elementary type must be a"
5870 & " power of 2 and at least ^", N);
5872 elsif RM_Size (U_Ent) > Size then
5873 Error_Msg_Uint_1 := RM_Size (U_Ent);
5874 Error_Msg_N
5875 ("stream size for elementary type must be a"
5876 & " power of 2 and at least ^", N);
5877 end if;
5879 Set_Has_Stream_Size_Clause (U_Ent);
5881 else
5882 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5883 end if;
5884 end Stream_Size;
5886 ----------------
5887 -- Value_Size --
5888 ----------------
5890 -- Value_Size attribute definition clause
5892 when Attribute_Value_Size => Value_Size : declare
5893 Size : constant Uint := Static_Integer (Expr);
5894 Biased : Boolean;
5896 begin
5897 if not Is_Type (U_Ent) then
5898 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5900 elsif Duplicate_Clause then
5901 null;
5903 elsif Is_Array_Type (U_Ent)
5904 and then not Is_Constrained (U_Ent)
5905 then
5906 Error_Msg_N
5907 ("Value_Size cannot be given for unconstrained array", Nam);
5909 else
5910 if Is_Elementary_Type (U_Ent) then
5911 Check_Size (Expr, U_Ent, Size, Biased);
5912 Set_Biased (U_Ent, N, "value size clause", Biased);
5913 end if;
5915 Set_RM_Size (U_Ent, Size);
5916 end if;
5917 end Value_Size;
5919 -----------------------
5920 -- Variable_Indexing --
5921 -----------------------
5923 when Attribute_Variable_Indexing =>
5924 Check_Indexing_Functions;
5926 -----------
5927 -- Write --
5928 -----------
5930 when Attribute_Write =>
5931 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5932 Set_Has_Specified_Stream_Write (Ent);
5934 -- All other attributes cannot be set
5936 when others =>
5937 Error_Msg_N
5938 ("attribute& cannot be set with definition clause", N);
5939 end case;
5941 -- The test for the type being frozen must be performed after any
5942 -- expression the clause has been analyzed since the expression itself
5943 -- might cause freezing that makes the clause illegal.
5945 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5946 return;
5947 end if;
5948 end Analyze_Attribute_Definition_Clause;
5950 ----------------------------
5951 -- Analyze_Code_Statement --
5952 ----------------------------
5954 procedure Analyze_Code_Statement (N : Node_Id) is
5955 HSS : constant Node_Id := Parent (N);
5956 SBody : constant Node_Id := Parent (HSS);
5957 Subp : constant Entity_Id := Current_Scope;
5958 Stmt : Node_Id;
5959 Decl : Node_Id;
5960 StmtO : Node_Id;
5961 DeclO : Node_Id;
5963 begin
5964 -- Analyze and check we get right type, note that this implements the
5965 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5966 -- is the only way that Asm_Insn could possibly be visible.
5968 Analyze_And_Resolve (Expression (N));
5970 if Etype (Expression (N)) = Any_Type then
5971 return;
5972 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5973 Error_Msg_N ("incorrect type for code statement", N);
5974 return;
5975 end if;
5977 Check_Code_Statement (N);
5979 -- Make sure we appear in the handled statement sequence of a
5980 -- subprogram (RM 13.8(3)).
5982 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5983 or else Nkind (SBody) /= N_Subprogram_Body
5984 then
5985 Error_Msg_N
5986 ("code statement can only appear in body of subprogram", N);
5987 return;
5988 end if;
5990 -- Do remaining checks (RM 13.8(3)) if not already done
5992 if not Is_Machine_Code_Subprogram (Subp) then
5993 Set_Is_Machine_Code_Subprogram (Subp);
5995 -- No exception handlers allowed
5997 if Present (Exception_Handlers (HSS)) then
5998 Error_Msg_N
5999 ("exception handlers not permitted in machine code subprogram",
6000 First (Exception_Handlers (HSS)));
6001 end if;
6003 -- No declarations other than use clauses and pragmas (we allow
6004 -- certain internally generated declarations as well).
6006 Decl := First (Declarations (SBody));
6007 while Present (Decl) loop
6008 DeclO := Original_Node (Decl);
6009 if Comes_From_Source (DeclO)
6010 and not Nkind_In (DeclO, N_Pragma,
6011 N_Use_Package_Clause,
6012 N_Use_Type_Clause,
6013 N_Implicit_Label_Declaration)
6014 then
6015 Error_Msg_N
6016 ("this declaration not allowed in machine code subprogram",
6017 DeclO);
6018 end if;
6020 Next (Decl);
6021 end loop;
6023 -- No statements other than code statements, pragmas, and labels.
6024 -- Again we allow certain internally generated statements.
6026 -- In Ada 2012, qualified expressions are names, and the code
6027 -- statement is initially parsed as a procedure call.
6029 Stmt := First (Statements (HSS));
6030 while Present (Stmt) loop
6031 StmtO := Original_Node (Stmt);
6033 -- A procedure call transformed into a code statement is OK.
6035 if Ada_Version >= Ada_2012
6036 and then Nkind (StmtO) = N_Procedure_Call_Statement
6037 and then Nkind (Name (StmtO)) = N_Qualified_Expression
6038 then
6039 null;
6041 elsif Comes_From_Source (StmtO)
6042 and then not Nkind_In (StmtO, N_Pragma,
6043 N_Label,
6044 N_Code_Statement)
6045 then
6046 Error_Msg_N
6047 ("this statement is not allowed in machine code subprogram",
6048 StmtO);
6049 end if;
6051 Next (Stmt);
6052 end loop;
6053 end if;
6054 end Analyze_Code_Statement;
6056 -----------------------------------------------
6057 -- Analyze_Enumeration_Representation_Clause --
6058 -----------------------------------------------
6060 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6061 Ident : constant Node_Id := Identifier (N);
6062 Aggr : constant Node_Id := Array_Aggregate (N);
6063 Enumtype : Entity_Id;
6064 Elit : Entity_Id;
6065 Expr : Node_Id;
6066 Assoc : Node_Id;
6067 Choice : Node_Id;
6068 Val : Uint;
6070 Err : Boolean := False;
6071 -- Set True to avoid cascade errors and crashes on incorrect source code
6073 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6074 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6075 -- Allowed range of universal integer (= allowed range of enum lit vals)
6077 Min : Uint;
6078 Max : Uint;
6079 -- Minimum and maximum values of entries
6081 Max_Node : Node_Id;
6082 -- Pointer to node for literal providing max value
6084 begin
6085 if Ignore_Rep_Clauses then
6086 Kill_Rep_Clause (N);
6087 return;
6088 end if;
6090 -- Ignore enumeration rep clauses by default in CodePeer mode,
6091 -- unless -gnatd.I is specified, as a work around for potential false
6092 -- positive messages.
6094 if CodePeer_Mode and not Debug_Flag_Dot_II then
6095 return;
6096 end if;
6098 -- First some basic error checks
6100 Find_Type (Ident);
6101 Enumtype := Entity (Ident);
6103 if Enumtype = Any_Type
6104 or else Rep_Item_Too_Early (Enumtype, N)
6105 then
6106 return;
6107 else
6108 Enumtype := Underlying_Type (Enumtype);
6109 end if;
6111 if not Is_Enumeration_Type (Enumtype) then
6112 Error_Msg_NE
6113 ("enumeration type required, found}",
6114 Ident, First_Subtype (Enumtype));
6115 return;
6116 end if;
6118 -- Ignore rep clause on generic actual type. This will already have
6119 -- been flagged on the template as an error, and this is the safest
6120 -- way to ensure we don't get a junk cascaded message in the instance.
6122 if Is_Generic_Actual_Type (Enumtype) then
6123 return;
6125 -- Type must be in current scope
6127 elsif Scope (Enumtype) /= Current_Scope then
6128 Error_Msg_N ("type must be declared in this scope", Ident);
6129 return;
6131 -- Type must be a first subtype
6133 elsif not Is_First_Subtype (Enumtype) then
6134 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6135 return;
6137 -- Ignore duplicate rep clause
6139 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6140 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6141 return;
6143 -- Don't allow rep clause for standard [wide_[wide_]]character
6145 elsif Is_Standard_Character_Type (Enumtype) then
6146 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
6147 return;
6149 -- Check that the expression is a proper aggregate (no parentheses)
6151 elsif Paren_Count (Aggr) /= 0 then
6152 Error_Msg
6153 ("extra parentheses surrounding aggregate not allowed",
6154 First_Sloc (Aggr));
6155 return;
6157 -- All tests passed, so set rep clause in place
6159 else
6160 Set_Has_Enumeration_Rep_Clause (Enumtype);
6161 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6162 end if;
6164 -- Now we process the aggregate. Note that we don't use the normal
6165 -- aggregate code for this purpose, because we don't want any of the
6166 -- normal expansion activities, and a number of special semantic
6167 -- rules apply (including the component type being any integer type)
6169 Elit := First_Literal (Enumtype);
6171 -- First the positional entries if any
6173 if Present (Expressions (Aggr)) then
6174 Expr := First (Expressions (Aggr));
6175 while Present (Expr) loop
6176 if No (Elit) then
6177 Error_Msg_N ("too many entries in aggregate", Expr);
6178 return;
6179 end if;
6181 Val := Static_Integer (Expr);
6183 -- Err signals that we found some incorrect entries processing
6184 -- the list. The final checks for completeness and ordering are
6185 -- skipped in this case.
6187 if Val = No_Uint then
6188 Err := True;
6190 elsif Val < Lo or else Hi < Val then
6191 Error_Msg_N ("value outside permitted range", Expr);
6192 Err := True;
6193 end if;
6195 Set_Enumeration_Rep (Elit, Val);
6196 Set_Enumeration_Rep_Expr (Elit, Expr);
6197 Next (Expr);
6198 Next (Elit);
6199 end loop;
6200 end if;
6202 -- Now process the named entries if present
6204 if Present (Component_Associations (Aggr)) then
6205 Assoc := First (Component_Associations (Aggr));
6206 while Present (Assoc) loop
6207 Choice := First (Choices (Assoc));
6209 if Present (Next (Choice)) then
6210 Error_Msg_N
6211 ("multiple choice not allowed here", Next (Choice));
6212 Err := True;
6213 end if;
6215 if Nkind (Choice) = N_Others_Choice then
6216 Error_Msg_N ("others choice not allowed here", Choice);
6217 Err := True;
6219 elsif Nkind (Choice) = N_Range then
6221 -- ??? should allow zero/one element range here
6223 Error_Msg_N ("range not allowed here", Choice);
6224 Err := True;
6226 else
6227 Analyze_And_Resolve (Choice, Enumtype);
6229 if Error_Posted (Choice) then
6230 Err := True;
6231 end if;
6233 if not Err then
6234 if Is_Entity_Name (Choice)
6235 and then Is_Type (Entity (Choice))
6236 then
6237 Error_Msg_N ("subtype name not allowed here", Choice);
6238 Err := True;
6240 -- ??? should allow static subtype with zero/one entry
6242 elsif Etype (Choice) = Base_Type (Enumtype) then
6243 if not Is_OK_Static_Expression (Choice) then
6244 Flag_Non_Static_Expr
6245 ("non-static expression used for choice!", Choice);
6246 Err := True;
6248 else
6249 Elit := Expr_Value_E (Choice);
6251 if Present (Enumeration_Rep_Expr (Elit)) then
6252 Error_Msg_Sloc :=
6253 Sloc (Enumeration_Rep_Expr (Elit));
6254 Error_Msg_NE
6255 ("representation for& previously given#",
6256 Choice, Elit);
6257 Err := True;
6258 end if;
6260 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6262 Expr := Expression (Assoc);
6263 Val := Static_Integer (Expr);
6265 if Val = No_Uint then
6266 Err := True;
6268 elsif Val < Lo or else Hi < Val then
6269 Error_Msg_N ("value outside permitted range", Expr);
6270 Err := True;
6271 end if;
6273 Set_Enumeration_Rep (Elit, Val);
6274 end if;
6275 end if;
6276 end if;
6277 end if;
6279 Next (Assoc);
6280 end loop;
6281 end if;
6283 -- Aggregate is fully processed. Now we check that a full set of
6284 -- representations was given, and that they are in range and in order.
6285 -- These checks are only done if no other errors occurred.
6287 if not Err then
6288 Min := No_Uint;
6289 Max := No_Uint;
6291 Elit := First_Literal (Enumtype);
6292 while Present (Elit) loop
6293 if No (Enumeration_Rep_Expr (Elit)) then
6294 Error_Msg_NE ("missing representation for&!", N, Elit);
6296 else
6297 Val := Enumeration_Rep (Elit);
6299 if Min = No_Uint then
6300 Min := Val;
6301 end if;
6303 if Val /= No_Uint then
6304 if Max /= No_Uint and then Val <= Max then
6305 Error_Msg_NE
6306 ("enumeration value for& not ordered!",
6307 Enumeration_Rep_Expr (Elit), Elit);
6308 end if;
6310 Max_Node := Enumeration_Rep_Expr (Elit);
6311 Max := Val;
6312 end if;
6314 -- If there is at least one literal whose representation is not
6315 -- equal to the Pos value, then note that this enumeration type
6316 -- has a non-standard representation.
6318 if Val /= Enumeration_Pos (Elit) then
6319 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6320 end if;
6321 end if;
6323 Next (Elit);
6324 end loop;
6326 -- Now set proper size information
6328 declare
6329 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6331 begin
6332 if Has_Size_Clause (Enumtype) then
6334 -- All OK, if size is OK now
6336 if RM_Size (Enumtype) >= Minsize then
6337 null;
6339 else
6340 -- Try if we can get by with biasing
6342 Minsize :=
6343 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6345 -- Error message if even biasing does not work
6347 if RM_Size (Enumtype) < Minsize then
6348 Error_Msg_Uint_1 := RM_Size (Enumtype);
6349 Error_Msg_Uint_2 := Max;
6350 Error_Msg_N
6351 ("previously given size (^) is too small "
6352 & "for this value (^)", Max_Node);
6354 -- If biasing worked, indicate that we now have biased rep
6356 else
6357 Set_Biased
6358 (Enumtype, Size_Clause (Enumtype), "size clause");
6359 end if;
6360 end if;
6362 else
6363 Set_RM_Size (Enumtype, Minsize);
6364 Set_Enum_Esize (Enumtype);
6365 end if;
6367 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6368 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6369 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6370 end;
6371 end if;
6373 -- We repeat the too late test in case it froze itself
6375 if Rep_Item_Too_Late (Enumtype, N) then
6376 null;
6377 end if;
6378 end Analyze_Enumeration_Representation_Clause;
6380 ----------------------------
6381 -- Analyze_Free_Statement --
6382 ----------------------------
6384 procedure Analyze_Free_Statement (N : Node_Id) is
6385 begin
6386 Analyze (Expression (N));
6387 end Analyze_Free_Statement;
6389 ---------------------------
6390 -- Analyze_Freeze_Entity --
6391 ---------------------------
6393 procedure Analyze_Freeze_Entity (N : Node_Id) is
6394 begin
6395 Freeze_Entity_Checks (N);
6396 end Analyze_Freeze_Entity;
6398 -----------------------------------
6399 -- Analyze_Freeze_Generic_Entity --
6400 -----------------------------------
6402 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6403 begin
6404 Freeze_Entity_Checks (N);
6405 end Analyze_Freeze_Generic_Entity;
6407 ------------------------------------------
6408 -- Analyze_Record_Representation_Clause --
6409 ------------------------------------------
6411 -- Note: we check as much as we can here, but we can't do any checks
6412 -- based on the position values (e.g. overlap checks) until freeze time
6413 -- because especially in Ada 2005 (machine scalar mode), the processing
6414 -- for non-standard bit order can substantially change the positions.
6415 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6416 -- for the remainder of this processing.
6418 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6419 Ident : constant Node_Id := Identifier (N);
6420 Biased : Boolean;
6421 CC : Node_Id;
6422 Comp : Entity_Id;
6423 Fbit : Uint;
6424 Hbit : Uint := Uint_0;
6425 Lbit : Uint;
6426 Ocomp : Entity_Id;
6427 Posit : Uint;
6428 Rectype : Entity_Id;
6429 Recdef : Node_Id;
6431 function Is_Inherited (Comp : Entity_Id) return Boolean;
6432 -- True if Comp is an inherited component in a record extension
6434 ------------------
6435 -- Is_Inherited --
6436 ------------------
6438 function Is_Inherited (Comp : Entity_Id) return Boolean is
6439 Comp_Base : Entity_Id;
6441 begin
6442 if Ekind (Rectype) = E_Record_Subtype then
6443 Comp_Base := Original_Record_Component (Comp);
6444 else
6445 Comp_Base := Comp;
6446 end if;
6448 return Comp_Base /= Original_Record_Component (Comp_Base);
6449 end Is_Inherited;
6451 -- Local variables
6453 Is_Record_Extension : Boolean;
6454 -- True if Rectype is a record extension
6456 CR_Pragma : Node_Id := Empty;
6457 -- Points to N_Pragma node if Complete_Representation pragma present
6459 -- Start of processing for Analyze_Record_Representation_Clause
6461 begin
6462 if Ignore_Rep_Clauses then
6463 Kill_Rep_Clause (N);
6464 return;
6465 end if;
6467 Find_Type (Ident);
6468 Rectype := Entity (Ident);
6470 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6471 return;
6472 else
6473 Rectype := Underlying_Type (Rectype);
6474 end if;
6476 -- First some basic error checks
6478 if not Is_Record_Type (Rectype) then
6479 Error_Msg_NE
6480 ("record type required, found}", Ident, First_Subtype (Rectype));
6481 return;
6483 elsif Scope (Rectype) /= Current_Scope then
6484 Error_Msg_N ("type must be declared in this scope", N);
6485 return;
6487 elsif not Is_First_Subtype (Rectype) then
6488 Error_Msg_N ("cannot give record rep clause for subtype", N);
6489 return;
6491 elsif Has_Record_Rep_Clause (Rectype) then
6492 Error_Msg_N ("duplicate record rep clause ignored", N);
6493 return;
6495 elsif Rep_Item_Too_Late (Rectype, N) then
6496 return;
6497 end if;
6499 -- We know we have a first subtype, now possibly go to the anonymous
6500 -- base type to determine whether Rectype is a record extension.
6502 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6503 Is_Record_Extension :=
6504 Nkind (Recdef) = N_Derived_Type_Definition
6505 and then Present (Record_Extension_Part (Recdef));
6507 if Present (Mod_Clause (N)) then
6508 declare
6509 Loc : constant Source_Ptr := Sloc (N);
6510 M : constant Node_Id := Mod_Clause (N);
6511 P : constant List_Id := Pragmas_Before (M);
6512 AtM_Nod : Node_Id;
6514 Mod_Val : Uint;
6515 pragma Warnings (Off, Mod_Val);
6517 begin
6518 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6520 if Warn_On_Obsolescent_Feature then
6521 Error_Msg_N
6522 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6523 Error_Msg_N
6524 ("\?j?use alignment attribute definition clause instead", N);
6525 end if;
6527 if Present (P) then
6528 Analyze_List (P);
6529 end if;
6531 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6532 -- the Mod clause into an alignment clause anyway, so that the
6533 -- back-end can compute and back-annotate properly the size and
6534 -- alignment of types that may include this record.
6536 -- This seems dubious, this destroys the source tree in a manner
6537 -- not detectable by ASIS ???
6539 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6540 AtM_Nod :=
6541 Make_Attribute_Definition_Clause (Loc,
6542 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6543 Chars => Name_Alignment,
6544 Expression => Relocate_Node (Expression (M)));
6546 Set_From_At_Mod (AtM_Nod);
6547 Insert_After (N, AtM_Nod);
6548 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6549 Set_Mod_Clause (N, Empty);
6551 else
6552 -- Get the alignment value to perform error checking
6554 Mod_Val := Get_Alignment_Value (Expression (M));
6555 end if;
6556 end;
6557 end if;
6559 -- For untagged types, clear any existing component clauses for the
6560 -- type. If the type is derived, this is what allows us to override
6561 -- a rep clause for the parent. For type extensions, the representation
6562 -- of the inherited components is inherited, so we want to keep previous
6563 -- component clauses for completeness.
6565 if not Is_Tagged_Type (Rectype) then
6566 Comp := First_Component_Or_Discriminant (Rectype);
6567 while Present (Comp) loop
6568 Set_Component_Clause (Comp, Empty);
6569 Next_Component_Or_Discriminant (Comp);
6570 end loop;
6571 end if;
6573 -- All done if no component clauses
6575 CC := First (Component_Clauses (N));
6577 if No (CC) then
6578 return;
6579 end if;
6581 -- A representation like this applies to the base type
6583 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6584 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6585 Set_Has_Specified_Layout (Base_Type (Rectype));
6587 -- Process the component clauses
6589 while Present (CC) loop
6591 -- Pragma
6593 if Nkind (CC) = N_Pragma then
6594 Analyze (CC);
6596 -- The only pragma of interest is Complete_Representation
6598 if Pragma_Name (CC) = Name_Complete_Representation then
6599 CR_Pragma := CC;
6600 end if;
6602 -- Processing for real component clause
6604 else
6605 Posit := Static_Integer (Position (CC));
6606 Fbit := Static_Integer (First_Bit (CC));
6607 Lbit := Static_Integer (Last_Bit (CC));
6609 if Posit /= No_Uint
6610 and then Fbit /= No_Uint
6611 and then Lbit /= No_Uint
6612 then
6613 if Posit < 0 then
6614 Error_Msg_N
6615 ("position cannot be negative", Position (CC));
6617 elsif Fbit < 0 then
6618 Error_Msg_N
6619 ("first bit cannot be negative", First_Bit (CC));
6621 -- The Last_Bit specified in a component clause must not be
6622 -- less than the First_Bit minus one (RM-13.5.1(10)).
6624 elsif Lbit < Fbit - 1 then
6625 Error_Msg_N
6626 ("last bit cannot be less than first bit minus one",
6627 Last_Bit (CC));
6629 -- Values look OK, so find the corresponding record component
6630 -- Even though the syntax allows an attribute reference for
6631 -- implementation-defined components, GNAT does not allow the
6632 -- tag to get an explicit position.
6634 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6635 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6636 Error_Msg_N ("position of tag cannot be specified", CC);
6637 else
6638 Error_Msg_N ("illegal component name", CC);
6639 end if;
6641 else
6642 Comp := First_Entity (Rectype);
6643 while Present (Comp) loop
6644 exit when Chars (Comp) = Chars (Component_Name (CC));
6645 Next_Entity (Comp);
6646 end loop;
6648 if No (Comp) then
6650 -- Maybe component of base type that is absent from
6651 -- statically constrained first subtype.
6653 Comp := First_Entity (Base_Type (Rectype));
6654 while Present (Comp) loop
6655 exit when Chars (Comp) = Chars (Component_Name (CC));
6656 Next_Entity (Comp);
6657 end loop;
6658 end if;
6660 if No (Comp) then
6661 Error_Msg_N
6662 ("component clause is for non-existent field", CC);
6664 -- Ada 2012 (AI05-0026): Any name that denotes a
6665 -- discriminant of an object of an unchecked union type
6666 -- shall not occur within a record_representation_clause.
6668 -- The general restriction of using record rep clauses on
6669 -- Unchecked_Union types has now been lifted. Since it is
6670 -- possible to introduce a record rep clause which mentions
6671 -- the discriminant of an Unchecked_Union in non-Ada 2012
6672 -- code, this check is applied to all versions of the
6673 -- language.
6675 elsif Ekind (Comp) = E_Discriminant
6676 and then Is_Unchecked_Union (Rectype)
6677 then
6678 Error_Msg_N
6679 ("cannot reference discriminant of unchecked union",
6680 Component_Name (CC));
6682 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6683 Error_Msg_NE
6684 ("component clause not allowed for inherited "
6685 & "component&", CC, Comp);
6687 elsif Present (Component_Clause (Comp)) then
6689 -- Diagnose duplicate rep clause, or check consistency
6690 -- if this is an inherited component. In a double fault,
6691 -- there may be a duplicate inconsistent clause for an
6692 -- inherited component.
6694 if Scope (Original_Record_Component (Comp)) = Rectype
6695 or else Parent (Component_Clause (Comp)) = N
6696 then
6697 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6698 Error_Msg_N ("component clause previously given#", CC);
6700 else
6701 declare
6702 Rep1 : constant Node_Id := Component_Clause (Comp);
6703 begin
6704 if Intval (Position (Rep1)) /=
6705 Intval (Position (CC))
6706 or else Intval (First_Bit (Rep1)) /=
6707 Intval (First_Bit (CC))
6708 or else Intval (Last_Bit (Rep1)) /=
6709 Intval (Last_Bit (CC))
6710 then
6711 Error_Msg_N
6712 ("component clause inconsistent "
6713 & "with representation of ancestor", CC);
6715 elsif Warn_On_Redundant_Constructs then
6716 Error_Msg_N
6717 ("?r?redundant confirming component clause "
6718 & "for component!", CC);
6719 end if;
6720 end;
6721 end if;
6723 -- Normal case where this is the first component clause we
6724 -- have seen for this entity, so set it up properly.
6726 else
6727 -- Make reference for field in record rep clause and set
6728 -- appropriate entity field in the field identifier.
6730 Generate_Reference
6731 (Comp, Component_Name (CC), Set_Ref => False);
6732 Set_Entity (Component_Name (CC), Comp);
6734 -- Update Fbit and Lbit to the actual bit number
6736 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6737 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6739 if Has_Size_Clause (Rectype)
6740 and then RM_Size (Rectype) <= Lbit
6741 then
6742 Error_Msg_N
6743 ("bit number out of range of specified size",
6744 Last_Bit (CC));
6745 else
6746 Set_Component_Clause (Comp, CC);
6747 Set_Component_Bit_Offset (Comp, Fbit);
6748 Set_Esize (Comp, 1 + (Lbit - Fbit));
6749 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6750 Set_Normalized_Position (Comp, Fbit / SSU);
6752 if Warn_On_Overridden_Size
6753 and then Has_Size_Clause (Etype (Comp))
6754 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6755 then
6756 Error_Msg_NE
6757 ("?S?component size overrides size clause for&",
6758 Component_Name (CC), Etype (Comp));
6759 end if;
6761 -- This information is also set in the corresponding
6762 -- component of the base type, found by accessing the
6763 -- Original_Record_Component link if it is present.
6765 Ocomp := Original_Record_Component (Comp);
6767 if Hbit < Lbit then
6768 Hbit := Lbit;
6769 end if;
6771 Check_Size
6772 (Component_Name (CC),
6773 Etype (Comp),
6774 Esize (Comp),
6775 Biased);
6777 Set_Biased
6778 (Comp, First_Node (CC), "component clause", Biased);
6780 if Present (Ocomp) then
6781 Set_Component_Clause (Ocomp, CC);
6782 Set_Component_Bit_Offset (Ocomp, Fbit);
6783 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6784 Set_Normalized_Position (Ocomp, Fbit / SSU);
6785 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6787 Set_Normalized_Position_Max
6788 (Ocomp, Normalized_Position (Ocomp));
6790 -- Note: we don't use Set_Biased here, because we
6791 -- already gave a warning above if needed, and we
6792 -- would get a duplicate for the same name here.
6794 Set_Has_Biased_Representation
6795 (Ocomp, Has_Biased_Representation (Comp));
6796 end if;
6798 if Esize (Comp) < 0 then
6799 Error_Msg_N ("component size is negative", CC);
6800 end if;
6801 end if;
6802 end if;
6803 end if;
6804 end if;
6805 end if;
6807 Next (CC);
6808 end loop;
6810 -- Check missing components if Complete_Representation pragma appeared
6812 if Present (CR_Pragma) then
6813 Comp := First_Component_Or_Discriminant (Rectype);
6814 while Present (Comp) loop
6815 if No (Component_Clause (Comp)) then
6816 Error_Msg_NE
6817 ("missing component clause for &", CR_Pragma, Comp);
6818 end if;
6820 Next_Component_Or_Discriminant (Comp);
6821 end loop;
6823 -- Give missing components warning if required
6825 elsif Warn_On_Unrepped_Components then
6826 declare
6827 Num_Repped_Components : Nat := 0;
6828 Num_Unrepped_Components : Nat := 0;
6830 begin
6831 -- First count number of repped and unrepped components
6833 Comp := First_Component_Or_Discriminant (Rectype);
6834 while Present (Comp) loop
6835 if Present (Component_Clause (Comp)) then
6836 Num_Repped_Components := Num_Repped_Components + 1;
6837 else
6838 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6839 end if;
6841 Next_Component_Or_Discriminant (Comp);
6842 end loop;
6844 -- We are only interested in the case where there is at least one
6845 -- unrepped component, and at least half the components have rep
6846 -- clauses. We figure that if less than half have them, then the
6847 -- partial rep clause is really intentional. If the component
6848 -- type has no underlying type set at this point (as for a generic
6849 -- formal type), we don't know enough to give a warning on the
6850 -- component.
6852 if Num_Unrepped_Components > 0
6853 and then Num_Unrepped_Components < Num_Repped_Components
6854 then
6855 Comp := First_Component_Or_Discriminant (Rectype);
6856 while Present (Comp) loop
6857 if No (Component_Clause (Comp))
6858 and then Comes_From_Source (Comp)
6859 and then Present (Underlying_Type (Etype (Comp)))
6860 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6861 or else Size_Known_At_Compile_Time
6862 (Underlying_Type (Etype (Comp))))
6863 and then not Has_Warnings_Off (Rectype)
6865 -- Ignore discriminant in unchecked union, since it is
6866 -- not there, and cannot have a component clause.
6868 and then (not Is_Unchecked_Union (Rectype)
6869 or else Ekind (Comp) /= E_Discriminant)
6870 then
6871 Error_Msg_Sloc := Sloc (Comp);
6872 Error_Msg_NE
6873 ("?C?no component clause given for & declared #",
6874 N, Comp);
6875 end if;
6877 Next_Component_Or_Discriminant (Comp);
6878 end loop;
6879 end if;
6880 end;
6881 end if;
6882 end Analyze_Record_Representation_Clause;
6884 -------------------------------------
6885 -- Build_Discrete_Static_Predicate --
6886 -------------------------------------
6888 procedure Build_Discrete_Static_Predicate
6889 (Typ : Entity_Id;
6890 Expr : Node_Id;
6891 Nam : Name_Id)
6893 Loc : constant Source_Ptr := Sloc (Expr);
6895 Non_Static : exception;
6896 -- Raised if something non-static is found
6898 Btyp : constant Entity_Id := Base_Type (Typ);
6900 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6901 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6902 -- Low bound and high bound value of base type of Typ
6904 TLo : Uint;
6905 THi : Uint;
6906 -- Bounds for constructing the static predicate. We use the bound of the
6907 -- subtype if it is static, otherwise the corresponding base type bound.
6908 -- Note: a non-static subtype can have a static predicate.
6910 type REnt is record
6911 Lo, Hi : Uint;
6912 end record;
6913 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6914 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6915 -- value.
6917 type RList is array (Nat range <>) of REnt;
6918 -- A list of ranges. The ranges are sorted in increasing order, and are
6919 -- disjoint (there is a gap of at least one value between each range in
6920 -- the table). A value is in the set of ranges in Rlist if it lies
6921 -- within one of these ranges.
6923 False_Range : constant RList :=
6924 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6925 -- An empty set of ranges represents a range list that can never be
6926 -- satisfied, since there are no ranges in which the value could lie,
6927 -- so it does not lie in any of them. False_Range is a canonical value
6928 -- for this empty set, but general processing should test for an Rlist
6929 -- with length zero (see Is_False predicate), since other null ranges
6930 -- may appear which must be treated as False.
6932 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6933 -- Range representing True, value must be in the base range
6935 function "and" (Left : RList; Right : RList) return RList;
6936 -- And's together two range lists, returning a range list. This is a set
6937 -- intersection operation.
6939 function "or" (Left : RList; Right : RList) return RList;
6940 -- Or's together two range lists, returning a range list. This is a set
6941 -- union operation.
6943 function "not" (Right : RList) return RList;
6944 -- Returns complement of a given range list, i.e. a range list
6945 -- representing all the values in TLo .. THi that are not in the input
6946 -- operand Right.
6948 function Build_Val (V : Uint) return Node_Id;
6949 -- Return an analyzed N_Identifier node referencing this value, suitable
6950 -- for use as an entry in the Static_Discrte_Predicate list. This node
6951 -- is typed with the base type.
6953 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6954 -- Return an analyzed N_Range node referencing this range, suitable for
6955 -- use as an entry in the Static_Discrete_Predicate list. This node is
6956 -- typed with the base type.
6958 function Get_RList (Exp : Node_Id) return RList;
6959 -- This is a recursive routine that converts the given expression into a
6960 -- list of ranges, suitable for use in building the static predicate.
6962 function Is_False (R : RList) return Boolean;
6963 pragma Inline (Is_False);
6964 -- Returns True if the given range list is empty, and thus represents a
6965 -- False list of ranges that can never be satisfied.
6967 function Is_True (R : RList) return Boolean;
6968 -- Returns True if R trivially represents the True predicate by having a
6969 -- single range from BLo to BHi.
6971 function Is_Type_Ref (N : Node_Id) return Boolean;
6972 pragma Inline (Is_Type_Ref);
6973 -- Returns if True if N is a reference to the type for the predicate in
6974 -- the expression (i.e. if it is an identifier whose Chars field matches
6975 -- the Nam given in the call). N must not be parenthesized, if the type
6976 -- name appears in parens, this routine will return False.
6978 function Lo_Val (N : Node_Id) return Uint;
6979 -- Given an entry from a Static_Discrete_Predicate list that is either
6980 -- a static expression or static range, gets either the expression value
6981 -- or the low bound of the range.
6983 function Hi_Val (N : Node_Id) return Uint;
6984 -- Given an entry from a Static_Discrete_Predicate list that is either
6985 -- a static expression or static range, gets either the expression value
6986 -- or the high bound of the range.
6988 function Membership_Entry (N : Node_Id) return RList;
6989 -- Given a single membership entry (range, value, or subtype), returns
6990 -- the corresponding range list. Raises Static_Error if not static.
6992 function Membership_Entries (N : Node_Id) return RList;
6993 -- Given an element on an alternatives list of a membership operation,
6994 -- returns the range list corresponding to this entry and all following
6995 -- entries (i.e. returns the "or" of this list of values).
6997 function Stat_Pred (Typ : Entity_Id) return RList;
6998 -- Given a type, if it has a static predicate, then return the predicate
6999 -- as a range list, otherwise raise Non_Static.
7001 -----------
7002 -- "and" --
7003 -----------
7005 function "and" (Left : RList; Right : RList) return RList is
7006 FEnt : REnt;
7007 -- First range of result
7009 SLeft : Nat := Left'First;
7010 -- Start of rest of left entries
7012 SRight : Nat := Right'First;
7013 -- Start of rest of right entries
7015 begin
7016 -- If either range is True, return the other
7018 if Is_True (Left) then
7019 return Right;
7020 elsif Is_True (Right) then
7021 return Left;
7022 end if;
7024 -- If either range is False, return False
7026 if Is_False (Left) or else Is_False (Right) then
7027 return False_Range;
7028 end if;
7030 -- Loop to remove entries at start that are disjoint, and thus just
7031 -- get discarded from the result entirely.
7033 loop
7034 -- If no operands left in either operand, result is false
7036 if SLeft > Left'Last or else SRight > Right'Last then
7037 return False_Range;
7039 -- Discard first left operand entry if disjoint with right
7041 elsif Left (SLeft).Hi < Right (SRight).Lo then
7042 SLeft := SLeft + 1;
7044 -- Discard first right operand entry if disjoint with left
7046 elsif Right (SRight).Hi < Left (SLeft).Lo then
7047 SRight := SRight + 1;
7049 -- Otherwise we have an overlapping entry
7051 else
7052 exit;
7053 end if;
7054 end loop;
7056 -- Now we have two non-null operands, and first entries overlap. The
7057 -- first entry in the result will be the overlapping part of these
7058 -- two entries.
7060 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7061 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7063 -- Now we can remove the entry that ended at a lower value, since its
7064 -- contribution is entirely contained in Fent.
7066 if Left (SLeft).Hi <= Right (SRight).Hi then
7067 SLeft := SLeft + 1;
7068 else
7069 SRight := SRight + 1;
7070 end if;
7072 -- Compute result by concatenating this first entry with the "and" of
7073 -- the remaining parts of the left and right operands. Note that if
7074 -- either of these is empty, "and" will yield empty, so that we will
7075 -- end up with just Fent, which is what we want in that case.
7077 return
7078 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7079 end "and";
7081 -----------
7082 -- "not" --
7083 -----------
7085 function "not" (Right : RList) return RList is
7086 begin
7087 -- Return True if False range
7089 if Is_False (Right) then
7090 return True_Range;
7091 end if;
7093 -- Return False if True range
7095 if Is_True (Right) then
7096 return False_Range;
7097 end if;
7099 -- Here if not trivial case
7101 declare
7102 Result : RList (1 .. Right'Length + 1);
7103 -- May need one more entry for gap at beginning and end
7105 Count : Nat := 0;
7106 -- Number of entries stored in Result
7108 begin
7109 -- Gap at start
7111 if Right (Right'First).Lo > TLo then
7112 Count := Count + 1;
7113 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7114 end if;
7116 -- Gaps between ranges
7118 for J in Right'First .. Right'Last - 1 loop
7119 Count := Count + 1;
7120 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7121 end loop;
7123 -- Gap at end
7125 if Right (Right'Last).Hi < THi then
7126 Count := Count + 1;
7127 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7128 end if;
7130 return Result (1 .. Count);
7131 end;
7132 end "not";
7134 ----------
7135 -- "or" --
7136 ----------
7138 function "or" (Left : RList; Right : RList) return RList is
7139 FEnt : REnt;
7140 -- First range of result
7142 SLeft : Nat := Left'First;
7143 -- Start of rest of left entries
7145 SRight : Nat := Right'First;
7146 -- Start of rest of right entries
7148 begin
7149 -- If either range is True, return True
7151 if Is_True (Left) or else Is_True (Right) then
7152 return True_Range;
7153 end if;
7155 -- If either range is False (empty), return the other
7157 if Is_False (Left) then
7158 return Right;
7159 elsif Is_False (Right) then
7160 return Left;
7161 end if;
7163 -- Initialize result first entry from left or right operand depending
7164 -- on which starts with the lower range.
7166 if Left (SLeft).Lo < Right (SRight).Lo then
7167 FEnt := Left (SLeft);
7168 SLeft := SLeft + 1;
7169 else
7170 FEnt := Right (SRight);
7171 SRight := SRight + 1;
7172 end if;
7174 -- This loop eats ranges from left and right operands that are
7175 -- contiguous with the first range we are gathering.
7177 loop
7178 -- Eat first entry in left operand if contiguous or overlapped by
7179 -- gathered first operand of result.
7181 if SLeft <= Left'Last
7182 and then Left (SLeft).Lo <= FEnt.Hi + 1
7183 then
7184 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7185 SLeft := SLeft + 1;
7187 -- Eat first entry in right operand if contiguous or overlapped by
7188 -- gathered right operand of result.
7190 elsif SRight <= Right'Last
7191 and then Right (SRight).Lo <= FEnt.Hi + 1
7192 then
7193 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7194 SRight := SRight + 1;
7196 -- All done if no more entries to eat
7198 else
7199 exit;
7200 end if;
7201 end loop;
7203 -- Obtain result as the first entry we just computed, concatenated
7204 -- to the "or" of the remaining results (if one operand is empty,
7205 -- this will just concatenate with the other
7207 return
7208 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7209 end "or";
7211 -----------------
7212 -- Build_Range --
7213 -----------------
7215 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7216 Result : Node_Id;
7217 begin
7218 Result :=
7219 Make_Range (Loc,
7220 Low_Bound => Build_Val (Lo),
7221 High_Bound => Build_Val (Hi));
7222 Set_Etype (Result, Btyp);
7223 Set_Analyzed (Result);
7224 return Result;
7225 end Build_Range;
7227 ---------------
7228 -- Build_Val --
7229 ---------------
7231 function Build_Val (V : Uint) return Node_Id is
7232 Result : Node_Id;
7234 begin
7235 if Is_Enumeration_Type (Typ) then
7236 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7237 else
7238 Result := Make_Integer_Literal (Loc, V);
7239 end if;
7241 Set_Etype (Result, Btyp);
7242 Set_Is_Static_Expression (Result);
7243 Set_Analyzed (Result);
7244 return Result;
7245 end Build_Val;
7247 ---------------
7248 -- Get_RList --
7249 ---------------
7251 function Get_RList (Exp : Node_Id) return RList is
7252 Op : Node_Kind;
7253 Val : Uint;
7255 begin
7256 -- Static expression can only be true or false
7258 if Is_OK_Static_Expression (Exp) then
7259 if Expr_Value (Exp) = 0 then
7260 return False_Range;
7261 else
7262 return True_Range;
7263 end if;
7264 end if;
7266 -- Otherwise test node type
7268 Op := Nkind (Exp);
7270 case Op is
7272 -- And
7274 when N_Op_And | N_And_Then =>
7275 return Get_RList (Left_Opnd (Exp))
7277 Get_RList (Right_Opnd (Exp));
7279 -- Or
7281 when N_Op_Or | N_Or_Else =>
7282 return Get_RList (Left_Opnd (Exp))
7284 Get_RList (Right_Opnd (Exp));
7286 -- Not
7288 when N_Op_Not =>
7289 return not Get_RList (Right_Opnd (Exp));
7291 -- Comparisons of type with static value
7293 when N_Op_Compare =>
7295 -- Type is left operand
7297 if Is_Type_Ref (Left_Opnd (Exp))
7298 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7299 then
7300 Val := Expr_Value (Right_Opnd (Exp));
7302 -- Typ is right operand
7304 elsif Is_Type_Ref (Right_Opnd (Exp))
7305 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7306 then
7307 Val := Expr_Value (Left_Opnd (Exp));
7309 -- Invert sense of comparison
7311 case Op is
7312 when N_Op_Gt => Op := N_Op_Lt;
7313 when N_Op_Lt => Op := N_Op_Gt;
7314 when N_Op_Ge => Op := N_Op_Le;
7315 when N_Op_Le => Op := N_Op_Ge;
7316 when others => null;
7317 end case;
7319 -- Other cases are non-static
7321 else
7322 raise Non_Static;
7323 end if;
7325 -- Construct range according to comparison operation
7327 case Op is
7328 when N_Op_Eq =>
7329 return RList'(1 => REnt'(Val, Val));
7331 when N_Op_Ge =>
7332 return RList'(1 => REnt'(Val, BHi));
7334 when N_Op_Gt =>
7335 return RList'(1 => REnt'(Val + 1, BHi));
7337 when N_Op_Le =>
7338 return RList'(1 => REnt'(BLo, Val));
7340 when N_Op_Lt =>
7341 return RList'(1 => REnt'(BLo, Val - 1));
7343 when N_Op_Ne =>
7344 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7346 when others =>
7347 raise Program_Error;
7348 end case;
7350 -- Membership (IN)
7352 when N_In =>
7353 if not Is_Type_Ref (Left_Opnd (Exp)) then
7354 raise Non_Static;
7355 end if;
7357 if Present (Right_Opnd (Exp)) then
7358 return Membership_Entry (Right_Opnd (Exp));
7359 else
7360 return Membership_Entries (First (Alternatives (Exp)));
7361 end if;
7363 -- Negative membership (NOT IN)
7365 when N_Not_In =>
7366 if not Is_Type_Ref (Left_Opnd (Exp)) then
7367 raise Non_Static;
7368 end if;
7370 if Present (Right_Opnd (Exp)) then
7371 return not Membership_Entry (Right_Opnd (Exp));
7372 else
7373 return not Membership_Entries (First (Alternatives (Exp)));
7374 end if;
7376 -- Function call, may be call to static predicate
7378 when N_Function_Call =>
7379 if Is_Entity_Name (Name (Exp)) then
7380 declare
7381 Ent : constant Entity_Id := Entity (Name (Exp));
7382 begin
7383 if Is_Predicate_Function (Ent)
7384 or else
7385 Is_Predicate_Function_M (Ent)
7386 then
7387 return Stat_Pred (Etype (First_Formal (Ent)));
7388 end if;
7389 end;
7390 end if;
7392 -- Other function call cases are non-static
7394 raise Non_Static;
7396 -- Qualified expression, dig out the expression
7398 when N_Qualified_Expression =>
7399 return Get_RList (Expression (Exp));
7401 when N_Case_Expression =>
7402 declare
7403 Alt : Node_Id;
7404 Choices : List_Id;
7405 Dep : Node_Id;
7407 begin
7408 if not Is_Entity_Name (Expression (Expr))
7409 or else Etype (Expression (Expr)) /= Typ
7410 then
7411 Error_Msg_N
7412 ("expression must denaote subtype", Expression (Expr));
7413 return False_Range;
7414 end if;
7416 -- Collect discrete choices in all True alternatives
7418 Choices := New_List;
7419 Alt := First (Alternatives (Exp));
7420 while Present (Alt) loop
7421 Dep := Expression (Alt);
7423 if not Is_OK_Static_Expression (Dep) then
7424 raise Non_Static;
7426 elsif Is_True (Expr_Value (Dep)) then
7427 Append_List_To (Choices,
7428 New_Copy_List (Discrete_Choices (Alt)));
7429 end if;
7431 Next (Alt);
7432 end loop;
7434 return Membership_Entries (First (Choices));
7435 end;
7437 -- Expression with actions: if no actions, dig out expression
7439 when N_Expression_With_Actions =>
7440 if Is_Empty_List (Actions (Exp)) then
7441 return Get_RList (Expression (Exp));
7442 else
7443 raise Non_Static;
7444 end if;
7446 -- Xor operator
7448 when N_Op_Xor =>
7449 return (Get_RList (Left_Opnd (Exp))
7450 and not Get_RList (Right_Opnd (Exp)))
7451 or (Get_RList (Right_Opnd (Exp))
7452 and not Get_RList (Left_Opnd (Exp)));
7454 -- Any other node type is non-static
7456 when others =>
7457 raise Non_Static;
7458 end case;
7459 end Get_RList;
7461 ------------
7462 -- Hi_Val --
7463 ------------
7465 function Hi_Val (N : Node_Id) return Uint is
7466 begin
7467 if Is_OK_Static_Expression (N) then
7468 return Expr_Value (N);
7469 else
7470 pragma Assert (Nkind (N) = N_Range);
7471 return Expr_Value (High_Bound (N));
7472 end if;
7473 end Hi_Val;
7475 --------------
7476 -- Is_False --
7477 --------------
7479 function Is_False (R : RList) return Boolean is
7480 begin
7481 return R'Length = 0;
7482 end Is_False;
7484 -------------
7485 -- Is_True --
7486 -------------
7488 function Is_True (R : RList) return Boolean is
7489 begin
7490 return R'Length = 1
7491 and then R (R'First).Lo = BLo
7492 and then R (R'First).Hi = BHi;
7493 end Is_True;
7495 -----------------
7496 -- Is_Type_Ref --
7497 -----------------
7499 function Is_Type_Ref (N : Node_Id) return Boolean is
7500 begin
7501 return Nkind (N) = N_Identifier
7502 and then Chars (N) = Nam
7503 and then Paren_Count (N) = 0;
7504 end Is_Type_Ref;
7506 ------------
7507 -- Lo_Val --
7508 ------------
7510 function Lo_Val (N : Node_Id) return Uint is
7511 begin
7512 if Is_OK_Static_Expression (N) then
7513 return Expr_Value (N);
7514 else
7515 pragma Assert (Nkind (N) = N_Range);
7516 return Expr_Value (Low_Bound (N));
7517 end if;
7518 end Lo_Val;
7520 ------------------------
7521 -- Membership_Entries --
7522 ------------------------
7524 function Membership_Entries (N : Node_Id) return RList is
7525 begin
7526 if No (Next (N)) then
7527 return Membership_Entry (N);
7528 else
7529 return Membership_Entry (N) or Membership_Entries (Next (N));
7530 end if;
7531 end Membership_Entries;
7533 ----------------------
7534 -- Membership_Entry --
7535 ----------------------
7537 function Membership_Entry (N : Node_Id) return RList is
7538 Val : Uint;
7539 SLo : Uint;
7540 SHi : Uint;
7542 begin
7543 -- Range case
7545 if Nkind (N) = N_Range then
7546 if not Is_OK_Static_Expression (Low_Bound (N))
7547 or else
7548 not Is_OK_Static_Expression (High_Bound (N))
7549 then
7550 raise Non_Static;
7551 else
7552 SLo := Expr_Value (Low_Bound (N));
7553 SHi := Expr_Value (High_Bound (N));
7554 return RList'(1 => REnt'(SLo, SHi));
7555 end if;
7557 -- Static expression case
7559 elsif Is_OK_Static_Expression (N) then
7560 Val := Expr_Value (N);
7561 return RList'(1 => REnt'(Val, Val));
7563 -- Identifier (other than static expression) case
7565 else pragma Assert (Nkind (N) = N_Identifier);
7567 -- Type case
7569 if Is_Type (Entity (N)) then
7571 -- If type has predicates, process them
7573 if Has_Predicates (Entity (N)) then
7574 return Stat_Pred (Entity (N));
7576 -- For static subtype without predicates, get range
7578 elsif Is_OK_Static_Subtype (Entity (N)) then
7579 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7580 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7581 return RList'(1 => REnt'(SLo, SHi));
7583 -- Any other type makes us non-static
7585 else
7586 raise Non_Static;
7587 end if;
7589 -- Any other kind of identifier in predicate (e.g. a non-static
7590 -- expression value) means this is not a static predicate.
7592 else
7593 raise Non_Static;
7594 end if;
7595 end if;
7596 end Membership_Entry;
7598 ---------------
7599 -- Stat_Pred --
7600 ---------------
7602 function Stat_Pred (Typ : Entity_Id) return RList is
7603 begin
7604 -- Not static if type does not have static predicates
7606 if not Has_Static_Predicate (Typ) then
7607 raise Non_Static;
7608 end if;
7610 -- Otherwise we convert the predicate list to a range list
7612 declare
7613 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7614 Result : RList (1 .. List_Length (Spred));
7615 P : Node_Id;
7617 begin
7618 P := First (Static_Discrete_Predicate (Typ));
7619 for J in Result'Range loop
7620 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7621 Next (P);
7622 end loop;
7624 return Result;
7625 end;
7626 end Stat_Pred;
7628 -- Start of processing for Build_Discrete_Static_Predicate
7630 begin
7631 -- Establish bounds for the predicate
7633 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7634 TLo := Expr_Value (Type_Low_Bound (Typ));
7635 else
7636 TLo := BLo;
7637 end if;
7639 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7640 THi := Expr_Value (Type_High_Bound (Typ));
7641 else
7642 THi := BHi;
7643 end if;
7645 -- Analyze the expression to see if it is a static predicate
7647 declare
7648 Ranges : constant RList := Get_RList (Expr);
7649 -- Range list from expression if it is static
7651 Plist : List_Id;
7653 begin
7654 -- Convert range list into a form for the static predicate. In the
7655 -- Ranges array, we just have raw ranges, these must be converted
7656 -- to properly typed and analyzed static expressions or range nodes.
7658 -- Note: here we limit ranges to the ranges of the subtype, so that
7659 -- a predicate is always false for values outside the subtype. That
7660 -- seems fine, such values are invalid anyway, and considering them
7661 -- to fail the predicate seems allowed and friendly, and furthermore
7662 -- simplifies processing for case statements and loops.
7664 Plist := New_List;
7666 for J in Ranges'Range loop
7667 declare
7668 Lo : Uint := Ranges (J).Lo;
7669 Hi : Uint := Ranges (J).Hi;
7671 begin
7672 -- Ignore completely out of range entry
7674 if Hi < TLo or else Lo > THi then
7675 null;
7677 -- Otherwise process entry
7679 else
7680 -- Adjust out of range value to subtype range
7682 if Lo < TLo then
7683 Lo := TLo;
7684 end if;
7686 if Hi > THi then
7687 Hi := THi;
7688 end if;
7690 -- Convert range into required form
7692 Append_To (Plist, Build_Range (Lo, Hi));
7693 end if;
7694 end;
7695 end loop;
7697 -- Processing was successful and all entries were static, so now we
7698 -- can store the result as the predicate list.
7700 Set_Static_Discrete_Predicate (Typ, Plist);
7702 -- The processing for static predicates put the expression into
7703 -- canonical form as a series of ranges. It also eliminated
7704 -- duplicates and collapsed and combined ranges. We might as well
7705 -- replace the alternatives list of the right operand of the
7706 -- membership test with the static predicate list, which will
7707 -- usually be more efficient.
7709 declare
7710 New_Alts : constant List_Id := New_List;
7711 Old_Node : Node_Id;
7712 New_Node : Node_Id;
7714 begin
7715 Old_Node := First (Plist);
7716 while Present (Old_Node) loop
7717 New_Node := New_Copy (Old_Node);
7719 if Nkind (New_Node) = N_Range then
7720 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7721 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7722 end if;
7724 Append_To (New_Alts, New_Node);
7725 Next (Old_Node);
7726 end loop;
7728 -- If empty list, replace by False
7730 if Is_Empty_List (New_Alts) then
7731 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7733 -- Else replace by set membership test
7735 else
7736 Rewrite (Expr,
7737 Make_In (Loc,
7738 Left_Opnd => Make_Identifier (Loc, Nam),
7739 Right_Opnd => Empty,
7740 Alternatives => New_Alts));
7742 -- Resolve new expression in function context
7744 Install_Formals (Predicate_Function (Typ));
7745 Push_Scope (Predicate_Function (Typ));
7746 Analyze_And_Resolve (Expr, Standard_Boolean);
7747 Pop_Scope;
7748 end if;
7749 end;
7750 end;
7752 -- If non-static, return doing nothing
7754 exception
7755 when Non_Static =>
7756 return;
7757 end Build_Discrete_Static_Predicate;
7759 -------------------------------------------
7760 -- Build_Invariant_Procedure_Declaration --
7761 -------------------------------------------
7763 function Build_Invariant_Procedure_Declaration
7764 (Typ : Entity_Id) return Node_Id
7766 Loc : constant Source_Ptr := Sloc (Typ);
7767 GM : constant Ghost_Mode_Type := Ghost_Mode;
7768 Decl : Node_Id;
7769 Obj_Id : Entity_Id;
7770 SId : Entity_Id;
7772 begin
7773 -- Check for duplicate definitions
7775 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7776 return Empty;
7777 end if;
7779 -- The related type may be subject to pragma Ghost with policy Ignore.
7780 -- Set the mode now to ensure that the predicate functions are properly
7781 -- flagged as ignored Ghost.
7783 Set_Ghost_Mode_From_Entity (Typ);
7785 SId :=
7786 Make_Defining_Identifier (Loc,
7787 Chars => New_External_Name (Chars (Typ), "Invariant"));
7788 Set_Has_Invariants (Typ);
7789 Set_Ekind (SId, E_Procedure);
7790 Set_Etype (SId, Standard_Void_Type);
7791 Set_Is_Invariant_Procedure (SId);
7792 Set_Invariant_Procedure (Typ, SId);
7794 -- Mark the invariant procedure explicitly as Ghost because it does not
7795 -- come from source.
7797 if Ghost_Mode > None then
7798 Set_Is_Ghost_Entity (SId);
7799 end if;
7801 Obj_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7802 Set_Etype (Obj_Id, Typ);
7804 Decl :=
7805 Make_Subprogram_Declaration (Loc,
7806 Make_Procedure_Specification (Loc,
7807 Defining_Unit_Name => SId,
7808 Parameter_Specifications => New_List (
7809 Make_Parameter_Specification (Loc,
7810 Defining_Identifier => Obj_Id,
7811 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
7813 -- Restore the original Ghost mode once analysis and expansion have
7814 -- taken place.
7816 Ghost_Mode := GM;
7818 return Decl;
7819 end Build_Invariant_Procedure_Declaration;
7821 -------------------------------
7822 -- Build_Invariant_Procedure --
7823 -------------------------------
7825 -- The procedure that is constructed here has the form
7827 -- procedure typInvariant (Ixxx : typ) is
7828 -- begin
7829 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7830 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7831 -- ...
7832 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7833 -- ...
7834 -- end typInvariant;
7836 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7837 Priv_Decls : constant List_Id := Private_Declarations (N);
7838 Vis_Decls : constant List_Id := Visible_Declarations (N);
7840 Loc : constant Source_Ptr := Sloc (Typ);
7841 Stmts : List_Id;
7842 Spec : Node_Id;
7843 SId : Entity_Id;
7844 PDecl : Node_Id;
7845 PBody : Node_Id;
7847 Object_Entity : Node_Id;
7848 -- The entity of the formal for the procedure
7850 Object_Name : Name_Id;
7851 -- Name for argument of invariant procedure
7853 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7854 -- Appends statements to Stmts for any invariants in the rep item chain
7855 -- of the given type. If Inherit is False, then we only process entries
7856 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7857 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7858 -- "inherited" to the exception message and generating an informational
7859 -- message about the inheritance of an invariant.
7861 --------------------
7862 -- Add_Invariants --
7863 --------------------
7865 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7866 procedure Add_Invariant (Prag : Node_Id);
7867 -- Create a runtime check to verify the exression of invariant pragma
7868 -- Prag. All generated code is added to list Stmts.
7870 -------------------
7871 -- Add_Invariant --
7872 -------------------
7874 procedure Add_Invariant (Prag : Node_Id) is
7875 procedure Replace_Type_Reference (N : Node_Id);
7876 -- Replace a single occurrence N of the subtype name with a
7877 -- reference to the formal of the predicate function. N can be an
7878 -- identifier referencing the subtype, or a selected component,
7879 -- representing an appropriately qualified occurrence of the
7880 -- subtype name.
7882 procedure Replace_Type_References is
7883 new Replace_Type_References_Generic (Replace_Type_Reference);
7884 -- Traverse an expression replacing all occurrences of the subtype
7885 -- name with appropriate references to the formal of the predicate
7886 -- function. Note that we must ensure that the type and entity
7887 -- information is properly set in the replacement node, since we
7888 -- will do a Preanalyze call of this expression without proper
7889 -- visibility of the procedure argument.
7891 ----------------------------
7892 -- Replace_Type_Reference --
7893 ----------------------------
7895 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7896 -- regarding handling of Sloc and Comes_From_Source.
7898 procedure Replace_Type_Reference (N : Node_Id) is
7899 Nloc : constant Source_Ptr := Sloc (N);
7901 begin
7902 -- Add semantic information to node to be rewritten, for ASIS
7903 -- navigation needs.
7905 if Nkind (N) = N_Identifier then
7906 Set_Entity (N, T);
7907 Set_Etype (N, T);
7909 elsif Nkind (N) = N_Selected_Component then
7910 Analyze (Prefix (N));
7911 Set_Entity (Selector_Name (N), T);
7912 Set_Etype (Selector_Name (N), T);
7913 end if;
7915 -- Invariant'Class, replace with T'Class (obj)
7917 if Class_Present (Prag) then
7919 -- In ASIS mode, an inherited item is already analyzed,
7920 -- and the replacement has been done, so do not repeat
7921 -- the transformation to prevent a malformed tree.
7923 if ASIS_Mode
7924 and then Nkind (Parent (N)) = N_Attribute_Reference
7925 and then Attribute_Name (Parent (N)) = Name_Class
7926 then
7927 null;
7929 else
7930 Rewrite (N,
7931 Make_Type_Conversion (Nloc,
7932 Subtype_Mark =>
7933 Make_Attribute_Reference (Nloc,
7934 Prefix => New_Occurrence_Of (T, Nloc),
7935 Attribute_Name => Name_Class),
7936 Expression => Make_Identifier (Nloc, Object_Name)));
7938 Set_Entity (Expression (N), Object_Entity);
7939 Set_Etype (Expression (N), Typ);
7940 end if;
7942 -- Invariant, replace with obj
7944 else
7945 Rewrite (N, Make_Identifier (Nloc, Object_Name));
7946 Set_Entity (N, Object_Entity);
7947 Set_Etype (N, Typ);
7948 end if;
7950 Set_Comes_From_Source (N, True);
7951 end Replace_Type_Reference;
7953 -- Local variables
7955 Asp : constant Node_Id := Corresponding_Aspect (Prag);
7956 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
7957 Ploc : constant Source_Ptr := Sloc (Prag);
7958 Arg1 : Node_Id;
7959 Arg2 : Node_Id;
7960 Arg3 : Node_Id;
7961 Assoc : List_Id;
7962 Expr : Node_Id;
7963 Str : String_Id;
7965 -- Start of processing for Add_Invariant
7967 begin
7968 -- Extract the arguments of the invariant pragma
7970 Arg1 := First (Pragma_Argument_Associations (Prag));
7971 Arg2 := Next (Arg1);
7972 Arg3 := Next (Arg2);
7974 Arg1 := Get_Pragma_Arg (Arg1);
7975 Arg2 := Get_Pragma_Arg (Arg2);
7977 -- The caller requests processing of all Invariant'Class pragmas,
7978 -- but the current pragma does not fall in this category. Return
7979 -- as there is nothing left to do.
7981 if Inherit then
7982 if not Class_Present (Prag) then
7983 return;
7984 end if;
7986 -- Otherwise the pragma must apply to the current type
7988 elsif Entity (Arg1) /= T then
7989 return;
7990 end if;
7992 Expr := New_Copy_Tree (Arg2);
7994 -- Replace all occurrences of the type's name with references to
7995 -- the formal parameter of the invariant procedure.
7997 Replace_Type_References (Expr, T);
7999 -- If the invariant pragma comes from an aspect, replace the saved
8000 -- expression because we need the subtype references replaced for
8001 -- the calls to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
8002 -- routines.
8004 if Present (Asp) then
8005 Set_Entity (Identifier (Asp), New_Copy_Tree (Expr));
8006 end if;
8008 -- Preanalyze the invariant expression to capture the visibility
8009 -- of the proper package part. In general the expression is not
8010 -- fully analyzed until the body of the invariant procedure is
8011 -- analyzed at the end of the private part, but that yields the
8012 -- wrong visibility.
8014 -- Historical note: we used to set N as the parent, but a package
8015 -- specification as the parent of an expression is bizarre.
8017 Set_Parent (Expr, Parent (Arg2));
8018 Preanalyze_Assert_Expression (Expr, Any_Boolean);
8020 -- A class-wide invariant may be inherited in a separate unit,
8021 -- where the corresponding expression cannot be resolved by
8022 -- visibility, because it refers to a local function. Propagate
8023 -- semantic information to the original representation item, to
8024 -- be used when an invariant procedure for a derived type is
8025 -- constructed.
8027 -- ??? Unclear how to handle class-wide invariants that are not
8028 -- function calls.
8030 if not Inherit
8031 and then Class_Present (Prag)
8032 and then Nkind (Expr) = N_Function_Call
8033 and then Nkind (Arg2) = N_Indexed_Component
8034 then
8035 Rewrite (Arg2,
8036 Make_Function_Call (Ploc,
8037 Name =>
8038 New_Occurrence_Of (Entity (Name (Expr)), Ploc),
8039 Parameter_Associations =>
8040 New_Copy_List (Expressions (Arg2))));
8041 end if;
8043 -- In ASIS mode, even if assertions are not enabled, we must
8044 -- analyze the original expression in the aspect specification
8045 -- because it is part of the original tree.
8047 if ASIS_Mode and then Present (Asp) then
8048 declare
8049 Orig_Expr : constant Node_Id := Expression (Asp);
8050 begin
8051 Replace_Type_References (Orig_Expr, T);
8052 Preanalyze_Assert_Expression (Orig_Expr, Any_Boolean);
8053 end;
8054 end if;
8056 -- An ignored invariant must not generate a runtime check. Add a
8057 -- null statement to ensure that the invariant procedure does get
8058 -- a completing body.
8060 if No (Stmts) then
8061 Stmts := Empty_List;
8062 end if;
8064 if Is_Ignored (Prag) then
8065 Append_To (Stmts, Make_Null_Statement (Ploc));
8067 -- Otherwise the invariant is checked. Build a Check pragma to
8068 -- verify the expression at runtime.
8070 else
8071 Assoc := New_List (
8072 Make_Pragma_Argument_Association (Ploc,
8073 Expression => Make_Identifier (Ploc, Nam)),
8074 Make_Pragma_Argument_Association (Ploc,
8075 Expression => Expr));
8077 -- Handle the String argument (if any)
8079 if Present (Arg3) then
8080 Str := Strval (Get_Pragma_Arg (Arg3));
8082 -- When inheriting an invariant, modify the message from
8083 -- "failed invariant" to "failed inherited invariant".
8085 if Inherit then
8086 String_To_Name_Buffer (Str);
8088 if Name_Buffer (1 .. 16) = "failed invariant" then
8089 Insert_Str_In_Name_Buffer ("inherited ", 8);
8090 Str := String_From_Name_Buffer;
8091 end if;
8092 end if;
8094 Append_To (Assoc,
8095 Make_Pragma_Argument_Association (Ploc,
8096 Expression => Make_String_Literal (Ploc, Str)));
8097 end if;
8099 -- Generate:
8100 -- pragma Check (Nam, Expr, Str);
8102 Append_To (Stmts,
8103 Make_Pragma (Ploc,
8104 Pragma_Identifier =>
8105 Make_Identifier (Ploc, Name_Check),
8106 Pragma_Argument_Associations => Assoc));
8107 end if;
8109 -- Output an info message when inheriting an invariant and the
8110 -- listing option is enabled.
8112 if Inherit and Opt.List_Inherited_Aspects then
8113 Error_Msg_Sloc := Sloc (Prag);
8114 Error_Msg_N
8115 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
8116 end if;
8117 end Add_Invariant;
8119 -- Local variables
8121 Ritem : Node_Id;
8123 -- Start of processing for Add_Invariants
8125 begin
8126 Ritem := First_Rep_Item (T);
8127 while Present (Ritem) loop
8128 if Nkind (Ritem) = N_Pragma
8129 and then Pragma_Name (Ritem) = Name_Invariant
8130 then
8131 Add_Invariant (Ritem);
8132 end if;
8134 Next_Rep_Item (Ritem);
8135 end loop;
8136 end Add_Invariants;
8138 -- Start of processing for Build_Invariant_Procedure
8140 begin
8141 Stmts := No_List;
8142 PDecl := Empty;
8143 PBody := Empty;
8144 SId := Empty;
8146 -- If the aspect specification exists for some view of the type, the
8147 -- declaration for the procedure has been created.
8149 if Has_Invariants (Typ) then
8150 SId := Invariant_Procedure (Typ);
8151 end if;
8153 -- If the body is already present, nothing to do. This will occur when
8154 -- the type is already frozen, which is the case when the invariant
8155 -- appears in a private part, and the freezing takes place before the
8156 -- final pass over full declarations.
8158 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
8160 if Present (SId) then
8161 PDecl := Unit_Declaration_Node (SId);
8163 if Present (PDecl)
8164 and then Nkind (PDecl) = N_Subprogram_Declaration
8165 and then Present (Corresponding_Body (PDecl))
8166 then
8167 return;
8168 end if;
8170 else
8171 PDecl := Build_Invariant_Procedure_Declaration (Typ);
8172 end if;
8174 -- Recover formal of procedure, for use in the calls to invariant
8175 -- functions (including inherited ones).
8177 Object_Entity :=
8178 Defining_Identifier
8179 (First (Parameter_Specifications (Specification (PDecl))));
8180 Object_Name := Chars (Object_Entity);
8182 -- Add invariants for the current type
8184 Add_Invariants (Typ, Inherit => False);
8186 -- Add invariants for parent types
8188 declare
8189 Current_Typ : Entity_Id;
8190 Parent_Typ : Entity_Id;
8192 begin
8193 Current_Typ := Typ;
8194 loop
8195 Parent_Typ := Etype (Current_Typ);
8197 if Is_Private_Type (Parent_Typ)
8198 and then Present (Full_View (Base_Type (Parent_Typ)))
8199 then
8200 Parent_Typ := Full_View (Base_Type (Parent_Typ));
8201 end if;
8203 exit when Parent_Typ = Current_Typ;
8205 Current_Typ := Parent_Typ;
8206 Add_Invariants (Current_Typ, Inherit => True);
8207 end loop;
8208 end;
8210 -- Add invariants of progenitors
8212 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
8213 declare
8214 Ifaces_List : Elist_Id;
8215 AI : Elmt_Id;
8216 Iface : Entity_Id;
8218 begin
8219 Collect_Interfaces (Typ, Ifaces_List);
8221 AI := First_Elmt (Ifaces_List);
8222 while Present (AI) loop
8223 Iface := Node (AI);
8225 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8226 Add_Invariants (Iface, Inherit => True);
8227 end if;
8229 Next_Elmt (AI);
8230 end loop;
8231 end;
8232 end if;
8234 -- Build the procedure if we generated at least one Check pragma
8236 if Stmts /= No_List then
8237 Spec := Copy_Separate_Tree (Specification (PDecl));
8239 PBody :=
8240 Make_Subprogram_Body (Loc,
8241 Specification => Spec,
8242 Declarations => Empty_List,
8243 Handled_Statement_Sequence =>
8244 Make_Handled_Sequence_Of_Statements (Loc,
8245 Statements => Stmts));
8247 -- Insert procedure declaration and spec at the appropriate points.
8248 -- If declaration is already analyzed, it was processed by the
8249 -- generated pragma.
8251 if Present (Priv_Decls) then
8253 -- The spec goes at the end of visible declarations, but they have
8254 -- already been analyzed, so we need to explicitly do the analyze.
8256 if not Analyzed (PDecl) then
8257 Append_To (Vis_Decls, PDecl);
8258 Analyze (PDecl);
8259 end if;
8261 -- The body goes at the end of the private declarations, which we
8262 -- have not analyzed yet, so we do not need to perform an explicit
8263 -- analyze call. We skip this if there are no private declarations
8264 -- (this is an error that will be caught elsewhere);
8266 Append_To (Priv_Decls, PBody);
8268 -- If the invariant appears on the full view of a type, the
8269 -- analysis of the private part is complete, and we must
8270 -- analyze the new body explicitly.
8272 if In_Private_Part (Current_Scope) then
8273 Analyze (PBody);
8274 end if;
8276 -- If there are no private declarations this may be an error that
8277 -- will be diagnosed elsewhere. However, if this is a non-private
8278 -- type that inherits invariants, it needs no completion and there
8279 -- may be no private part. In this case insert invariant procedure
8280 -- at end of current declarative list, and analyze at once, given
8281 -- that the type is about to be frozen.
8283 elsif not Is_Private_Type (Typ) then
8284 Append_To (Vis_Decls, PDecl);
8285 Append_To (Vis_Decls, PBody);
8286 Analyze (PDecl);
8287 Analyze (PBody);
8288 end if;
8289 end if;
8290 end Build_Invariant_Procedure;
8292 -------------------------------
8293 -- Build_Predicate_Functions --
8294 -------------------------------
8296 -- The procedures that are constructed here have the form:
8298 -- function typPredicate (Ixxx : typ) return Boolean is
8299 -- begin
8300 -- return
8301 -- exp1 and then exp2 and then ...
8302 -- and then typ1Predicate (typ1 (Ixxx))
8303 -- and then typ2Predicate (typ2 (Ixxx))
8304 -- and then ...;
8305 -- end typPredicate;
8307 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8308 -- this is the point at which these expressions get analyzed, providing the
8309 -- required delay, and typ1, typ2, are entities from which predicates are
8310 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8311 -- use this function even if checks are off, e.g. for membership tests.
8313 -- If the expression has at least one Raise_Expression, then we also build
8314 -- the typPredicateM version of the function, in which any occurrence of a
8315 -- Raise_Expression is converted to "return False".
8317 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8318 Loc : constant Source_Ptr := Sloc (Typ);
8320 Expr : Node_Id;
8321 -- This is the expression for the result of the function. It is
8322 -- is build by connecting the component predicates with AND THEN.
8324 Expr_M : Node_Id;
8325 -- This is the corresponding return expression for the Predicate_M
8326 -- function. It differs in that raise expressions are marked for
8327 -- special expansion (see Process_REs).
8329 Object_Name : constant Name_Id := New_Internal_Name ('I');
8330 -- Name for argument of Predicate procedure. Note that we use the same
8331 -- name for both predicate functions. That way the reference within the
8332 -- predicate expression is the same in both functions.
8334 Object_Entity : constant Entity_Id :=
8335 Make_Defining_Identifier (Loc, Chars => Object_Name);
8336 -- Entity for argument of Predicate procedure
8338 Object_Entity_M : constant Entity_Id :=
8339 Make_Defining_Identifier (Loc, Chars => Object_Name);
8340 -- Entity for argument of Predicate_M procedure
8342 Raise_Expression_Present : Boolean := False;
8343 -- Set True if Expr has at least one Raise_Expression
8345 procedure Add_Call (T : Entity_Id);
8346 -- Includes a call to the predicate function for type T in Expr if T
8347 -- has predicates and Predicate_Function (T) is non-empty.
8349 procedure Add_Predicates;
8350 -- Appends expressions for any Predicate pragmas in the rep item chain
8351 -- Typ to Expr. Note that we look only at items for this exact entity.
8352 -- Inheritance of predicates for the parent type is done by calling the
8353 -- Predicate_Function of the parent type, using Add_Call above.
8355 function Process_RE (N : Node_Id) return Traverse_Result;
8356 -- Used in Process REs, tests if node N is a raise expression, and if
8357 -- so, marks it to be converted to return False.
8359 procedure Process_REs is new Traverse_Proc (Process_RE);
8360 -- Marks any raise expressions in Expr_M to return False
8362 function Test_RE (N : Node_Id) return Traverse_Result;
8363 -- Used in Test_REs, tests one node for being a raise expression, and if
8364 -- so sets Raise_Expression_Present True.
8366 procedure Test_REs is new Traverse_Proc (Test_RE);
8367 -- Tests to see if Expr contains any raise expressions
8369 --------------
8370 -- Add_Call --
8371 --------------
8373 procedure Add_Call (T : Entity_Id) is
8374 Exp : Node_Id;
8376 begin
8377 if Present (T) and then Present (Predicate_Function (T)) then
8378 Set_Has_Predicates (Typ);
8380 -- Build the call to the predicate function of T
8382 Exp :=
8383 Make_Predicate_Call
8384 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8386 -- Add call to evolving expression, using AND THEN if needed
8388 if No (Expr) then
8389 Expr := Exp;
8391 else
8392 Expr :=
8393 Make_And_Then (Sloc (Expr),
8394 Left_Opnd => Relocate_Node (Expr),
8395 Right_Opnd => Exp);
8396 end if;
8398 -- Output info message on inheritance if required. Note we do not
8399 -- give this information for generic actual types, since it is
8400 -- unwelcome noise in that case in instantiations. We also
8401 -- generally suppress the message in instantiations, and also
8402 -- if it involves internal names.
8404 if Opt.List_Inherited_Aspects
8405 and then not Is_Generic_Actual_Type (Typ)
8406 and then Instantiation_Depth (Sloc (Typ)) = 0
8407 and then not Is_Internal_Name (Chars (T))
8408 and then not Is_Internal_Name (Chars (Typ))
8409 then
8410 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8411 Error_Msg_Node_2 := T;
8412 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8413 end if;
8414 end if;
8415 end Add_Call;
8417 --------------------
8418 -- Add_Predicates --
8419 --------------------
8421 procedure Add_Predicates is
8422 procedure Add_Predicate (Prag : Node_Id);
8423 -- Concatenate the expression of predicate pragma Prag to Expr by
8424 -- using a short circuit "and then" operator.
8426 -------------------
8427 -- Add_Predicate --
8428 -------------------
8430 procedure Add_Predicate (Prag : Node_Id) is
8431 procedure Replace_Type_Reference (N : Node_Id);
8432 -- Replace a single occurrence N of the subtype name with a
8433 -- reference to the formal of the predicate function. N can be an
8434 -- identifier referencing the subtype, or a selected component,
8435 -- representing an appropriately qualified occurrence of the
8436 -- subtype name.
8438 procedure Replace_Type_References is
8439 new Replace_Type_References_Generic (Replace_Type_Reference);
8440 -- Traverse an expression changing every occurrence of an
8441 -- identifier whose name matches the name of the subtype with a
8442 -- reference to the formal parameter of the predicate function.
8444 ----------------------------
8445 -- Replace_Type_Reference --
8446 ----------------------------
8448 procedure Replace_Type_Reference (N : Node_Id) is
8449 begin
8450 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8451 -- Use the Sloc of the usage name, not the defining name
8453 Set_Etype (N, Typ);
8454 Set_Entity (N, Object_Entity);
8456 -- We want to treat the node as if it comes from source, so
8457 -- that ASIS will not ignore it.
8459 Set_Comes_From_Source (N, True);
8460 end Replace_Type_Reference;
8462 -- Local variables
8464 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8465 Arg1 : Node_Id;
8466 Arg2 : Node_Id;
8468 -- Start of processing for Add_Predicate
8470 begin
8471 -- Extract the arguments of the pragma. The expression itself
8472 -- is copied for use in the predicate function, to preserve the
8473 -- original version for ASIS use.
8475 Arg1 := First (Pragma_Argument_Associations (Prag));
8476 Arg2 := Next (Arg1);
8478 Arg1 := Get_Pragma_Arg (Arg1);
8479 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
8481 -- When the predicate pragma applies to the current type or its
8482 -- full view, replace all occurrences of the subtype name with
8483 -- references to the formal parameter of the predicate function.
8485 if Entity (Arg1) = Typ
8486 or else Full_View (Entity (Arg1)) = Typ
8487 then
8488 Replace_Type_References (Arg2, Typ);
8490 -- If the predicate pragma comes from an aspect, replace the
8491 -- saved expression because we need the subtype references
8492 -- replaced for the calls to Preanalyze_Spec_Expression in
8493 -- Check_Aspect_At_xxx routines.
8495 if Present (Asp) then
8497 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8498 end if;
8500 -- Concatenate to the existing predicate expressions by using
8501 -- "and then".
8503 if Present (Expr) then
8504 Expr :=
8505 Make_And_Then (Loc,
8506 Left_Opnd => Relocate_Node (Expr),
8507 Right_Opnd => Relocate_Node (Arg2));
8509 -- Otherwise this is the first predicate expression
8511 else
8512 Expr := Relocate_Node (Arg2);
8513 end if;
8514 end if;
8515 end Add_Predicate;
8517 -- Local variables
8519 Ritem : Node_Id;
8521 -- Start of processing for Add_Predicates
8523 begin
8524 Ritem := First_Rep_Item (Typ);
8525 while Present (Ritem) loop
8526 if Nkind (Ritem) = N_Pragma
8527 and then Pragma_Name (Ritem) = Name_Predicate
8528 then
8529 Add_Predicate (Ritem);
8530 end if;
8532 Next_Rep_Item (Ritem);
8533 end loop;
8534 end Add_Predicates;
8536 ----------------
8537 -- Process_RE --
8538 ----------------
8540 function Process_RE (N : Node_Id) return Traverse_Result is
8541 begin
8542 if Nkind (N) = N_Raise_Expression then
8543 Set_Convert_To_Return_False (N);
8544 return Skip;
8545 else
8546 return OK;
8547 end if;
8548 end Process_RE;
8550 -------------
8551 -- Test_RE --
8552 -------------
8554 function Test_RE (N : Node_Id) return Traverse_Result is
8555 begin
8556 if Nkind (N) = N_Raise_Expression then
8557 Raise_Expression_Present := True;
8558 return Abandon;
8559 else
8560 return OK;
8561 end if;
8562 end Test_RE;
8564 -- Local variables
8566 GM : constant Ghost_Mode_Type := Ghost_Mode;
8568 -- Start of processing for Build_Predicate_Functions
8570 begin
8571 -- Return if already built or if type does not have predicates
8573 if not Has_Predicates (Typ)
8574 or else Present (Predicate_Function (Typ))
8575 then
8576 return;
8577 end if;
8579 -- The related type may be subject to pragma Ghost with policy Ignore.
8580 -- Set the mode now to ensure that the predicate functions are properly
8581 -- flagged as ignored Ghost.
8583 Set_Ghost_Mode_From_Entity (Typ);
8585 -- Prepare to construct predicate expression
8587 Expr := Empty;
8589 -- Add Predicates for the current type
8591 Add_Predicates;
8593 -- Add predicates for ancestor if present
8595 declare
8596 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8597 begin
8598 if Present (Atyp) then
8599 Add_Call (Atyp);
8600 end if;
8601 end;
8603 -- Case where predicates are present
8605 if Present (Expr) then
8607 -- Test for raise expression present
8609 Test_REs (Expr);
8611 -- If raise expression is present, capture a copy of Expr for use
8612 -- in building the predicateM function version later on. For this
8613 -- copy we replace references to Object_Entity by Object_Entity_M.
8615 if Raise_Expression_Present then
8616 declare
8617 Map : constant Elist_Id := New_Elmt_List;
8618 New_V : Entity_Id := Empty;
8620 -- The unanalyzed expression will be copied and appear in
8621 -- both functions. Normally expressions do not declare new
8622 -- entities, but quantified expressions do, so we need to
8623 -- create new entities for their bound variables, to prevent
8624 -- multiple definitions in gigi.
8626 function Reset_Loop_Variable (N : Node_Id)
8627 return Traverse_Result;
8629 procedure Collect_Loop_Variables is
8630 new Traverse_Proc (Reset_Loop_Variable);
8632 ------------------------
8633 -- Reset_Loop_Variable --
8634 ------------------------
8636 function Reset_Loop_Variable (N : Node_Id)
8637 return Traverse_Result
8639 begin
8640 if Nkind (N) = N_Iterator_Specification then
8641 New_V := Make_Defining_Identifier
8642 (Sloc (N), Chars (Defining_Identifier (N)));
8644 Set_Defining_Identifier (N, New_V);
8645 end if;
8647 return OK;
8648 end Reset_Loop_Variable;
8650 begin
8651 Append_Elmt (Object_Entity, Map);
8652 Append_Elmt (Object_Entity_M, Map);
8653 Expr_M := New_Copy_Tree (Expr, Map => Map);
8654 Collect_Loop_Variables (Expr_M);
8655 end;
8656 end if;
8658 -- Build the main predicate function
8660 declare
8661 SId : constant Entity_Id :=
8662 Make_Defining_Identifier (Loc,
8663 Chars => New_External_Name (Chars (Typ), "Predicate"));
8664 -- The entity for the function spec
8666 SIdB : constant Entity_Id :=
8667 Make_Defining_Identifier (Loc,
8668 Chars => New_External_Name (Chars (Typ), "Predicate"));
8669 -- The entity for the function body
8671 Spec : Node_Id;
8672 FDecl : Node_Id;
8673 FBody : Node_Id;
8675 begin
8676 -- Build function declaration
8678 Set_Ekind (SId, E_Function);
8679 Set_Is_Internal (SId);
8680 Set_Is_Predicate_Function (SId);
8681 Set_Predicate_Function (Typ, SId);
8683 -- The predicate function is shared between views of a type
8685 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8686 Set_Predicate_Function (Full_View (Typ), SId);
8687 end if;
8689 -- Mark the predicate function explicitly as Ghost because it does
8690 -- not come from source.
8692 if Ghost_Mode > None then
8693 Set_Is_Ghost_Entity (SId);
8694 end if;
8696 Spec :=
8697 Make_Function_Specification (Loc,
8698 Defining_Unit_Name => SId,
8699 Parameter_Specifications => New_List (
8700 Make_Parameter_Specification (Loc,
8701 Defining_Identifier => Object_Entity,
8702 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8703 Result_Definition =>
8704 New_Occurrence_Of (Standard_Boolean, Loc));
8706 FDecl :=
8707 Make_Subprogram_Declaration (Loc,
8708 Specification => Spec);
8710 -- Build function body
8712 Spec :=
8713 Make_Function_Specification (Loc,
8714 Defining_Unit_Name => SIdB,
8715 Parameter_Specifications => New_List (
8716 Make_Parameter_Specification (Loc,
8717 Defining_Identifier =>
8718 Make_Defining_Identifier (Loc, Object_Name),
8719 Parameter_Type =>
8720 New_Occurrence_Of (Typ, Loc))),
8721 Result_Definition =>
8722 New_Occurrence_Of (Standard_Boolean, Loc));
8724 FBody :=
8725 Make_Subprogram_Body (Loc,
8726 Specification => Spec,
8727 Declarations => Empty_List,
8728 Handled_Statement_Sequence =>
8729 Make_Handled_Sequence_Of_Statements (Loc,
8730 Statements => New_List (
8731 Make_Simple_Return_Statement (Loc,
8732 Expression => Expr))));
8734 -- Insert declaration before freeze node and body after
8736 Insert_Before_And_Analyze (N, FDecl);
8737 Insert_After_And_Analyze (N, FBody);
8738 end;
8740 -- Test for raise expressions present and if so build M version
8742 if Raise_Expression_Present then
8743 declare
8744 SId : constant Entity_Id :=
8745 Make_Defining_Identifier (Loc,
8746 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8747 -- The entity for the function spec
8749 SIdB : constant Entity_Id :=
8750 Make_Defining_Identifier (Loc,
8751 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8752 -- The entity for the function body
8754 Spec : Node_Id;
8755 FDecl : Node_Id;
8756 FBody : Node_Id;
8757 BTemp : Entity_Id;
8759 begin
8760 -- Mark any raise expressions for special expansion
8762 Process_REs (Expr_M);
8764 -- Build function declaration
8766 Set_Ekind (SId, E_Function);
8767 Set_Is_Predicate_Function_M (SId);
8768 Set_Predicate_Function_M (Typ, SId);
8770 -- The predicate function is shared between views of a type
8772 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8773 Set_Predicate_Function_M (Full_View (Typ), SId);
8774 end if;
8776 -- Mark the predicate function explicitly as Ghost because it
8777 -- does not come from source.
8779 if Ghost_Mode > None then
8780 Set_Is_Ghost_Entity (SId);
8781 end if;
8783 Spec :=
8784 Make_Function_Specification (Loc,
8785 Defining_Unit_Name => SId,
8786 Parameter_Specifications => New_List (
8787 Make_Parameter_Specification (Loc,
8788 Defining_Identifier => Object_Entity_M,
8789 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8790 Result_Definition =>
8791 New_Occurrence_Of (Standard_Boolean, Loc));
8793 FDecl :=
8794 Make_Subprogram_Declaration (Loc,
8795 Specification => Spec);
8797 -- Build function body
8799 Spec :=
8800 Make_Function_Specification (Loc,
8801 Defining_Unit_Name => SIdB,
8802 Parameter_Specifications => New_List (
8803 Make_Parameter_Specification (Loc,
8804 Defining_Identifier =>
8805 Make_Defining_Identifier (Loc, Object_Name),
8806 Parameter_Type =>
8807 New_Occurrence_Of (Typ, Loc))),
8808 Result_Definition =>
8809 New_Occurrence_Of (Standard_Boolean, Loc));
8811 -- Build the body, we declare the boolean expression before
8812 -- doing the return, because we are not really confident of
8813 -- what happens if a return appears within a return.
8815 BTemp :=
8816 Make_Defining_Identifier (Loc,
8817 Chars => New_Internal_Name ('B'));
8819 FBody :=
8820 Make_Subprogram_Body (Loc,
8821 Specification => Spec,
8823 Declarations => New_List (
8824 Make_Object_Declaration (Loc,
8825 Defining_Identifier => BTemp,
8826 Constant_Present => True,
8827 Object_Definition =>
8828 New_Occurrence_Of (Standard_Boolean, Loc),
8829 Expression => Expr_M)),
8831 Handled_Statement_Sequence =>
8832 Make_Handled_Sequence_Of_Statements (Loc,
8833 Statements => New_List (
8834 Make_Simple_Return_Statement (Loc,
8835 Expression => New_Occurrence_Of (BTemp, Loc)))));
8837 -- Insert declaration before freeze node and body after
8839 Insert_Before_And_Analyze (N, FDecl);
8840 Insert_After_And_Analyze (N, FBody);
8841 end;
8842 end if;
8844 -- See if we have a static predicate. Note that the answer may be
8845 -- yes even if we have an explicit Dynamic_Predicate present.
8847 declare
8848 PS : Boolean;
8849 EN : Node_Id;
8851 begin
8852 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8853 PS := False;
8854 else
8855 PS := Is_Predicate_Static (Expr, Object_Name);
8856 end if;
8858 -- Case where we have a predicate-static aspect
8860 if PS then
8862 -- We don't set Has_Static_Predicate_Aspect, since we can have
8863 -- any of the three cases (Predicate, Dynamic_Predicate, or
8864 -- Static_Predicate) generating a predicate with an expression
8865 -- that is predicate-static. We just indicate that we have a
8866 -- predicate that can be treated as static.
8868 Set_Has_Static_Predicate (Typ);
8870 -- For discrete subtype, build the static predicate list
8872 if Is_Discrete_Type (Typ) then
8873 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8875 -- If we don't get a static predicate list, it means that we
8876 -- have a case where this is not possible, most typically in
8877 -- the case where we inherit a dynamic predicate. We do not
8878 -- consider this an error, we just leave the predicate as
8879 -- dynamic. But if we do succeed in building the list, then
8880 -- we mark the predicate as static.
8882 if No (Static_Discrete_Predicate (Typ)) then
8883 Set_Has_Static_Predicate (Typ, False);
8884 end if;
8886 -- For real or string subtype, save predicate expression
8888 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8889 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8890 end if;
8892 -- Case of dynamic predicate (expression is not predicate-static)
8894 else
8895 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8896 -- is only set if we have an explicit Dynamic_Predicate aspect
8897 -- given. Here we may simply have a Predicate aspect where the
8898 -- expression happens not to be predicate-static.
8900 -- Emit an error when the predicate is categorized as static
8901 -- but its expression is not predicate-static.
8903 -- First a little fiddling to get a nice location for the
8904 -- message. If the expression is of the form (A and then B),
8905 -- then use the left operand for the Sloc. This avoids getting
8906 -- confused by a call to a higher-level predicate with a less
8907 -- convenient source location.
8909 EN := Expr;
8910 while Nkind (EN) = N_And_Then loop
8911 EN := Left_Opnd (EN);
8912 end loop;
8914 -- Now post appropriate message
8916 if Has_Static_Predicate_Aspect (Typ) then
8917 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8918 Error_Msg_F
8919 ("expression is not predicate-static (RM 3.2.4(16-22))",
8920 EN);
8921 else
8922 Error_Msg_F
8923 ("static predicate requires scalar or string type", EN);
8924 end if;
8925 end if;
8926 end if;
8927 end;
8928 end if;
8930 -- Restore the original Ghost mode once analysis and expansion have
8931 -- taken place.
8933 Ghost_Mode := GM;
8934 end Build_Predicate_Functions;
8936 -----------------------------------------
8937 -- Check_Aspect_At_End_Of_Declarations --
8938 -----------------------------------------
8940 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8941 Ent : constant Entity_Id := Entity (ASN);
8942 Ident : constant Node_Id := Identifier (ASN);
8943 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8945 End_Decl_Expr : constant Node_Id := Entity (Ident);
8946 -- Expression to be analyzed at end of declarations
8948 Freeze_Expr : constant Node_Id := Expression (ASN);
8949 -- Expression from call to Check_Aspect_At_Freeze_Point
8951 T : constant Entity_Id := Etype (Freeze_Expr);
8952 -- Type required for preanalyze call
8954 Err : Boolean;
8955 -- Set False if error
8957 -- On entry to this procedure, Entity (Ident) contains a copy of the
8958 -- original expression from the aspect, saved for this purpose, and
8959 -- but Expression (Ident) is a preanalyzed copy of the expression,
8960 -- preanalyzed just after the freeze point.
8962 procedure Check_Overloaded_Name;
8963 -- For aspects whose expression is simply a name, this routine checks if
8964 -- the name is overloaded or not. If so, it verifies there is an
8965 -- interpretation that matches the entity obtained at the freeze point,
8966 -- otherwise the compiler complains.
8968 ---------------------------
8969 -- Check_Overloaded_Name --
8970 ---------------------------
8972 procedure Check_Overloaded_Name is
8973 begin
8974 if not Is_Overloaded (End_Decl_Expr) then
8975 Err := not Is_Entity_Name (End_Decl_Expr)
8976 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8978 else
8979 Err := True;
8981 declare
8982 Index : Interp_Index;
8983 It : Interp;
8985 begin
8986 Get_First_Interp (End_Decl_Expr, Index, It);
8987 while Present (It.Typ) loop
8988 if It.Nam = Entity (Freeze_Expr) then
8989 Err := False;
8990 exit;
8991 end if;
8993 Get_Next_Interp (Index, It);
8994 end loop;
8995 end;
8996 end if;
8997 end Check_Overloaded_Name;
8999 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9001 begin
9002 -- Case of aspects Dimension, Dimension_System and Synchronization
9004 if A_Id = Aspect_Synchronization then
9005 return;
9007 -- Case of stream attributes, just have to compare entities. However,
9008 -- the expression is just a name (possibly overloaded), and there may
9009 -- be stream operations declared for unrelated types, so we just need
9010 -- to verify that one of these interpretations is the one available at
9011 -- at the freeze point.
9013 elsif A_Id = Aspect_Input or else
9014 A_Id = Aspect_Output or else
9015 A_Id = Aspect_Read or else
9016 A_Id = Aspect_Write
9017 then
9018 Analyze (End_Decl_Expr);
9019 Check_Overloaded_Name;
9021 elsif A_Id = Aspect_Variable_Indexing or else
9022 A_Id = Aspect_Constant_Indexing or else
9023 A_Id = Aspect_Default_Iterator or else
9024 A_Id = Aspect_Iterator_Element
9025 then
9026 -- Make type unfrozen before analysis, to prevent spurious errors
9027 -- about late attributes.
9029 Set_Is_Frozen (Ent, False);
9030 Analyze (End_Decl_Expr);
9031 Set_Is_Frozen (Ent, True);
9033 -- If the end of declarations comes before any other freeze
9034 -- point, the Freeze_Expr is not analyzed: no check needed.
9036 if Analyzed (Freeze_Expr) and then not In_Instance then
9037 Check_Overloaded_Name;
9038 else
9039 Err := False;
9040 end if;
9042 -- All other cases
9044 else
9045 -- Indicate that the expression comes from an aspect specification,
9046 -- which is used in subsequent analysis even if expansion is off.
9048 Set_Parent (End_Decl_Expr, ASN);
9050 -- In a generic context the aspect expressions have not been
9051 -- preanalyzed, so do it now. There are no conformance checks
9052 -- to perform in this case.
9054 if No (T) then
9055 Check_Aspect_At_Freeze_Point (ASN);
9056 return;
9058 -- The default values attributes may be defined in the private part,
9059 -- and the analysis of the expression may take place when only the
9060 -- partial view is visible. The expression must be scalar, so use
9061 -- the full view to resolve.
9063 elsif (A_Id = Aspect_Default_Value
9064 or else
9065 A_Id = Aspect_Default_Component_Value)
9066 and then Is_Private_Type (T)
9067 then
9068 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
9070 else
9071 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9072 end if;
9074 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9075 end if;
9077 -- Output error message if error. Force error on aspect specification
9078 -- even if there is an error on the expression itself.
9080 if Err then
9081 Error_Msg_NE
9082 ("!visibility of aspect for& changes after freeze point",
9083 ASN, Ent);
9084 Error_Msg_NE
9085 ("info: & is frozen here, aspects evaluated at this point??",
9086 Freeze_Node (Ent), Ent);
9087 end if;
9088 end Check_Aspect_At_End_Of_Declarations;
9090 ----------------------------------
9091 -- Check_Aspect_At_Freeze_Point --
9092 ----------------------------------
9094 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9095 Ident : constant Node_Id := Identifier (ASN);
9096 -- Identifier (use Entity field to save expression)
9098 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9100 T : Entity_Id := Empty;
9101 -- Type required for preanalyze call
9103 begin
9104 -- On entry to this procedure, Entity (Ident) contains a copy of the
9105 -- original expression from the aspect, saved for this purpose.
9107 -- On exit from this procedure Entity (Ident) is unchanged, still
9108 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9109 -- of the expression, preanalyzed just after the freeze point.
9111 -- Make a copy of the expression to be preanalyzed
9113 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
9115 -- Find type for preanalyze call
9117 case A_Id is
9119 -- No_Aspect should be impossible
9121 when No_Aspect =>
9122 raise Program_Error;
9124 -- Aspects taking an optional boolean argument
9126 when Boolean_Aspects |
9127 Library_Unit_Aspects =>
9129 T := Standard_Boolean;
9131 -- Aspects corresponding to attribute definition clauses
9133 when Aspect_Address =>
9134 T := RTE (RE_Address);
9136 when Aspect_Attach_Handler =>
9137 T := RTE (RE_Interrupt_ID);
9139 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9140 T := RTE (RE_Bit_Order);
9142 when Aspect_Convention =>
9143 return;
9145 when Aspect_CPU =>
9146 T := RTE (RE_CPU_Range);
9148 -- Default_Component_Value is resolved with the component type
9150 when Aspect_Default_Component_Value =>
9151 T := Component_Type (Entity (ASN));
9153 when Aspect_Default_Storage_Pool =>
9154 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9156 -- Default_Value is resolved with the type entity in question
9158 when Aspect_Default_Value =>
9159 T := Entity (ASN);
9161 when Aspect_Dispatching_Domain =>
9162 T := RTE (RE_Dispatching_Domain);
9164 when Aspect_External_Tag =>
9165 T := Standard_String;
9167 when Aspect_External_Name =>
9168 T := Standard_String;
9170 when Aspect_Link_Name =>
9171 T := Standard_String;
9173 when Aspect_Priority | Aspect_Interrupt_Priority =>
9174 T := Standard_Integer;
9176 when Aspect_Relative_Deadline =>
9177 T := RTE (RE_Time_Span);
9179 when Aspect_Small =>
9180 T := Universal_Real;
9182 -- For a simple storage pool, we have to retrieve the type of the
9183 -- pool object associated with the aspect's corresponding attribute
9184 -- definition clause.
9186 when Aspect_Simple_Storage_Pool =>
9187 T := Etype (Expression (Aspect_Rep_Item (ASN)));
9189 when Aspect_Storage_Pool =>
9190 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9192 when Aspect_Alignment |
9193 Aspect_Component_Size |
9194 Aspect_Machine_Radix |
9195 Aspect_Object_Size |
9196 Aspect_Size |
9197 Aspect_Storage_Size |
9198 Aspect_Stream_Size |
9199 Aspect_Value_Size =>
9200 T := Any_Integer;
9202 when Aspect_Linker_Section =>
9203 T := Standard_String;
9205 when Aspect_Synchronization =>
9206 return;
9208 -- Special case, the expression of these aspects is just an entity
9209 -- that does not need any resolution, so just analyze.
9211 when Aspect_Input |
9212 Aspect_Output |
9213 Aspect_Read |
9214 Aspect_Suppress |
9215 Aspect_Unsuppress |
9216 Aspect_Warnings |
9217 Aspect_Write =>
9218 Analyze (Expression (ASN));
9219 return;
9221 -- Same for Iterator aspects, where the expression is a function
9222 -- name. Legality rules are checked separately.
9224 when Aspect_Constant_Indexing |
9225 Aspect_Default_Iterator |
9226 Aspect_Iterator_Element |
9227 Aspect_Variable_Indexing =>
9228 Analyze (Expression (ASN));
9229 return;
9231 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9233 when Aspect_Iterable =>
9234 T := Entity (ASN);
9236 declare
9237 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
9238 Assoc : Node_Id;
9239 Expr : Node_Id;
9241 begin
9242 if Cursor = Any_Type then
9243 return;
9244 end if;
9246 Assoc := First (Component_Associations (Expression (ASN)));
9247 while Present (Assoc) loop
9248 Expr := Expression (Assoc);
9249 Analyze (Expr);
9251 if not Error_Posted (Expr) then
9252 Resolve_Iterable_Operation
9253 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9254 end if;
9256 Next (Assoc);
9257 end loop;
9258 end;
9260 return;
9262 -- Invariant/Predicate take boolean expressions
9264 when Aspect_Dynamic_Predicate |
9265 Aspect_Invariant |
9266 Aspect_Predicate |
9267 Aspect_Static_Predicate |
9268 Aspect_Type_Invariant =>
9269 T := Standard_Boolean;
9271 -- Here is the list of aspects that don't require delay analysis
9273 when Aspect_Abstract_State |
9274 Aspect_Annotate |
9275 Aspect_Contract_Cases |
9276 Aspect_Default_Initial_Condition |
9277 Aspect_Depends |
9278 Aspect_Dimension |
9279 Aspect_Dimension_System |
9280 Aspect_Extensions_Visible |
9281 Aspect_Ghost |
9282 Aspect_Global |
9283 Aspect_Implicit_Dereference |
9284 Aspect_Initial_Condition |
9285 Aspect_Initializes |
9286 Aspect_Obsolescent |
9287 Aspect_Part_Of |
9288 Aspect_Post |
9289 Aspect_Postcondition |
9290 Aspect_Pre |
9291 Aspect_Precondition |
9292 Aspect_Refined_Depends |
9293 Aspect_Refined_Global |
9294 Aspect_Refined_Post |
9295 Aspect_Refined_State |
9296 Aspect_SPARK_Mode |
9297 Aspect_Test_Case |
9298 Aspect_Unimplemented =>
9299 raise Program_Error;
9301 end case;
9303 -- Do the preanalyze call
9305 Preanalyze_Spec_Expression (Expression (ASN), T);
9306 end Check_Aspect_At_Freeze_Point;
9308 -----------------------------------
9309 -- Check_Constant_Address_Clause --
9310 -----------------------------------
9312 procedure Check_Constant_Address_Clause
9313 (Expr : Node_Id;
9314 U_Ent : Entity_Id)
9316 procedure Check_At_Constant_Address (Nod : Node_Id);
9317 -- Checks that the given node N represents a name whose 'Address is
9318 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9319 -- address value is the same at the point of declaration of U_Ent and at
9320 -- the time of elaboration of the address clause.
9322 procedure Check_Expr_Constants (Nod : Node_Id);
9323 -- Checks that Nod meets the requirements for a constant address clause
9324 -- in the sense of the enclosing procedure.
9326 procedure Check_List_Constants (Lst : List_Id);
9327 -- Check that all elements of list Lst meet the requirements for a
9328 -- constant address clause in the sense of the enclosing procedure.
9330 -------------------------------
9331 -- Check_At_Constant_Address --
9332 -------------------------------
9334 procedure Check_At_Constant_Address (Nod : Node_Id) is
9335 begin
9336 if Is_Entity_Name (Nod) then
9337 if Present (Address_Clause (Entity ((Nod)))) then
9338 Error_Msg_NE
9339 ("invalid address clause for initialized object &!",
9340 Nod, U_Ent);
9341 Error_Msg_NE
9342 ("address for& cannot" &
9343 " depend on another address clause! (RM 13.1(22))!",
9344 Nod, U_Ent);
9346 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9347 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9348 then
9349 Error_Msg_NE
9350 ("invalid address clause for initialized object &!",
9351 Nod, U_Ent);
9352 Error_Msg_Node_2 := U_Ent;
9353 Error_Msg_NE
9354 ("\& must be defined before & (RM 13.1(22))!",
9355 Nod, Entity (Nod));
9356 end if;
9358 elsif Nkind (Nod) = N_Selected_Component then
9359 declare
9360 T : constant Entity_Id := Etype (Prefix (Nod));
9362 begin
9363 if (Is_Record_Type (T)
9364 and then Has_Discriminants (T))
9365 or else
9366 (Is_Access_Type (T)
9367 and then Is_Record_Type (Designated_Type (T))
9368 and then Has_Discriminants (Designated_Type (T)))
9369 then
9370 Error_Msg_NE
9371 ("invalid address clause for initialized object &!",
9372 Nod, U_Ent);
9373 Error_Msg_N
9374 ("\address cannot depend on component" &
9375 " of discriminated record (RM 13.1(22))!",
9376 Nod);
9377 else
9378 Check_At_Constant_Address (Prefix (Nod));
9379 end if;
9380 end;
9382 elsif Nkind (Nod) = N_Indexed_Component then
9383 Check_At_Constant_Address (Prefix (Nod));
9384 Check_List_Constants (Expressions (Nod));
9386 else
9387 Check_Expr_Constants (Nod);
9388 end if;
9389 end Check_At_Constant_Address;
9391 --------------------------
9392 -- Check_Expr_Constants --
9393 --------------------------
9395 procedure Check_Expr_Constants (Nod : Node_Id) is
9396 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9397 Ent : Entity_Id := Empty;
9399 begin
9400 if Nkind (Nod) in N_Has_Etype
9401 and then Etype (Nod) = Any_Type
9402 then
9403 return;
9404 end if;
9406 case Nkind (Nod) is
9407 when N_Empty | N_Error =>
9408 return;
9410 when N_Identifier | N_Expanded_Name =>
9411 Ent := Entity (Nod);
9413 -- We need to look at the original node if it is different
9414 -- from the node, since we may have rewritten things and
9415 -- substituted an identifier representing the rewrite.
9417 if Original_Node (Nod) /= Nod then
9418 Check_Expr_Constants (Original_Node (Nod));
9420 -- If the node is an object declaration without initial
9421 -- value, some code has been expanded, and the expression
9422 -- is not constant, even if the constituents might be
9423 -- acceptable, as in A'Address + offset.
9425 if Ekind (Ent) = E_Variable
9426 and then
9427 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9428 and then
9429 No (Expression (Declaration_Node (Ent)))
9430 then
9431 Error_Msg_NE
9432 ("invalid address clause for initialized object &!",
9433 Nod, U_Ent);
9435 -- If entity is constant, it may be the result of expanding
9436 -- a check. We must verify that its declaration appears
9437 -- before the object in question, else we also reject the
9438 -- address clause.
9440 elsif Ekind (Ent) = E_Constant
9441 and then In_Same_Source_Unit (Ent, U_Ent)
9442 and then Sloc (Ent) > Loc_U_Ent
9443 then
9444 Error_Msg_NE
9445 ("invalid address clause for initialized object &!",
9446 Nod, U_Ent);
9447 end if;
9449 return;
9450 end if;
9452 -- Otherwise look at the identifier and see if it is OK
9454 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9455 or else Is_Type (Ent)
9456 then
9457 return;
9459 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9461 -- This is the case where we must have Ent defined before
9462 -- U_Ent. Clearly if they are in different units this
9463 -- requirement is met since the unit containing Ent is
9464 -- already processed.
9466 if not In_Same_Source_Unit (Ent, U_Ent) then
9467 return;
9469 -- Otherwise location of Ent must be before the location
9470 -- of U_Ent, that's what prior defined means.
9472 elsif Sloc (Ent) < Loc_U_Ent then
9473 return;
9475 else
9476 Error_Msg_NE
9477 ("invalid address clause for initialized object &!",
9478 Nod, U_Ent);
9479 Error_Msg_Node_2 := U_Ent;
9480 Error_Msg_NE
9481 ("\& must be defined before & (RM 13.1(22))!",
9482 Nod, Ent);
9483 end if;
9485 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9486 Check_Expr_Constants (Original_Node (Nod));
9488 else
9489 Error_Msg_NE
9490 ("invalid address clause for initialized object &!",
9491 Nod, U_Ent);
9493 if Comes_From_Source (Ent) then
9494 Error_Msg_NE
9495 ("\reference to variable& not allowed"
9496 & " (RM 13.1(22))!", Nod, Ent);
9497 else
9498 Error_Msg_N
9499 ("non-static expression not allowed"
9500 & " (RM 13.1(22))!", Nod);
9501 end if;
9502 end if;
9504 when N_Integer_Literal =>
9506 -- If this is a rewritten unchecked conversion, in a system
9507 -- where Address is an integer type, always use the base type
9508 -- for a literal value. This is user-friendly and prevents
9509 -- order-of-elaboration issues with instances of unchecked
9510 -- conversion.
9512 if Nkind (Original_Node (Nod)) = N_Function_Call then
9513 Set_Etype (Nod, Base_Type (Etype (Nod)));
9514 end if;
9516 when N_Real_Literal |
9517 N_String_Literal |
9518 N_Character_Literal =>
9519 return;
9521 when N_Range =>
9522 Check_Expr_Constants (Low_Bound (Nod));
9523 Check_Expr_Constants (High_Bound (Nod));
9525 when N_Explicit_Dereference =>
9526 Check_Expr_Constants (Prefix (Nod));
9528 when N_Indexed_Component =>
9529 Check_Expr_Constants (Prefix (Nod));
9530 Check_List_Constants (Expressions (Nod));
9532 when N_Slice =>
9533 Check_Expr_Constants (Prefix (Nod));
9534 Check_Expr_Constants (Discrete_Range (Nod));
9536 when N_Selected_Component =>
9537 Check_Expr_Constants (Prefix (Nod));
9539 when N_Attribute_Reference =>
9540 if Nam_In (Attribute_Name (Nod), Name_Address,
9541 Name_Access,
9542 Name_Unchecked_Access,
9543 Name_Unrestricted_Access)
9544 then
9545 Check_At_Constant_Address (Prefix (Nod));
9547 else
9548 Check_Expr_Constants (Prefix (Nod));
9549 Check_List_Constants (Expressions (Nod));
9550 end if;
9552 when N_Aggregate =>
9553 Check_List_Constants (Component_Associations (Nod));
9554 Check_List_Constants (Expressions (Nod));
9556 when N_Component_Association =>
9557 Check_Expr_Constants (Expression (Nod));
9559 when N_Extension_Aggregate =>
9560 Check_Expr_Constants (Ancestor_Part (Nod));
9561 Check_List_Constants (Component_Associations (Nod));
9562 Check_List_Constants (Expressions (Nod));
9564 when N_Null =>
9565 return;
9567 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9568 Check_Expr_Constants (Left_Opnd (Nod));
9569 Check_Expr_Constants (Right_Opnd (Nod));
9571 when N_Unary_Op =>
9572 Check_Expr_Constants (Right_Opnd (Nod));
9574 when N_Type_Conversion |
9575 N_Qualified_Expression |
9576 N_Allocator |
9577 N_Unchecked_Type_Conversion =>
9578 Check_Expr_Constants (Expression (Nod));
9580 when N_Function_Call =>
9581 if not Is_Pure (Entity (Name (Nod))) then
9582 Error_Msg_NE
9583 ("invalid address clause for initialized object &!",
9584 Nod, U_Ent);
9586 Error_Msg_NE
9587 ("\function & is not pure (RM 13.1(22))!",
9588 Nod, Entity (Name (Nod)));
9590 else
9591 Check_List_Constants (Parameter_Associations (Nod));
9592 end if;
9594 when N_Parameter_Association =>
9595 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9597 when others =>
9598 Error_Msg_NE
9599 ("invalid address clause for initialized object &!",
9600 Nod, U_Ent);
9601 Error_Msg_NE
9602 ("\must be constant defined before& (RM 13.1(22))!",
9603 Nod, U_Ent);
9604 end case;
9605 end Check_Expr_Constants;
9607 --------------------------
9608 -- Check_List_Constants --
9609 --------------------------
9611 procedure Check_List_Constants (Lst : List_Id) is
9612 Nod1 : Node_Id;
9614 begin
9615 if Present (Lst) then
9616 Nod1 := First (Lst);
9617 while Present (Nod1) loop
9618 Check_Expr_Constants (Nod1);
9619 Next (Nod1);
9620 end loop;
9621 end if;
9622 end Check_List_Constants;
9624 -- Start of processing for Check_Constant_Address_Clause
9626 begin
9627 -- If rep_clauses are to be ignored, no need for legality checks. In
9628 -- particular, no need to pester user about rep clauses that violate the
9629 -- rule on constant addresses, given that these clauses will be removed
9630 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9631 -- we want to relax these checks.
9633 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9634 Check_Expr_Constants (Expr);
9635 end if;
9636 end Check_Constant_Address_Clause;
9638 ---------------------------
9639 -- Check_Pool_Size_Clash --
9640 ---------------------------
9642 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9643 Post : Node_Id;
9645 begin
9646 -- We need to find out which one came first. Note that in the case of
9647 -- aspects mixed with pragmas there are cases where the processing order
9648 -- is reversed, which is why we do the check here.
9650 if Sloc (SP) < Sloc (SS) then
9651 Error_Msg_Sloc := Sloc (SP);
9652 Post := SS;
9653 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9655 else
9656 Error_Msg_Sloc := Sloc (SS);
9657 Post := SP;
9658 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9659 end if;
9661 Error_Msg_N
9662 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9663 end Check_Pool_Size_Clash;
9665 ----------------------------------------
9666 -- Check_Record_Representation_Clause --
9667 ----------------------------------------
9669 procedure Check_Record_Representation_Clause (N : Node_Id) is
9670 Loc : constant Source_Ptr := Sloc (N);
9671 Ident : constant Node_Id := Identifier (N);
9672 Rectype : Entity_Id;
9673 Fent : Entity_Id;
9674 CC : Node_Id;
9675 Fbit : Uint;
9676 Lbit : Uint;
9677 Hbit : Uint := Uint_0;
9678 Comp : Entity_Id;
9679 Pcomp : Entity_Id;
9681 Max_Bit_So_Far : Uint;
9682 -- Records the maximum bit position so far. If all field positions
9683 -- are monotonically increasing, then we can skip the circuit for
9684 -- checking for overlap, since no overlap is possible.
9686 Tagged_Parent : Entity_Id := Empty;
9687 -- This is set in the case of a derived tagged type for which we have
9688 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9689 -- positioned by record representation clauses). In this case we must
9690 -- check for overlap between components of this tagged type, and the
9691 -- components of its parent. Tagged_Parent will point to this parent
9692 -- type. For all other cases Tagged_Parent is left set to Empty.
9694 Parent_Last_Bit : Uint;
9695 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9696 -- last bit position for any field in the parent type. We only need to
9697 -- check overlap for fields starting below this point.
9699 Overlap_Check_Required : Boolean;
9700 -- Used to keep track of whether or not an overlap check is required
9702 Overlap_Detected : Boolean := False;
9703 -- Set True if an overlap is detected
9705 Ccount : Natural := 0;
9706 -- Number of component clauses in record rep clause
9708 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9709 -- Given two entities for record components or discriminants, checks
9710 -- if they have overlapping component clauses and issues errors if so.
9712 procedure Find_Component;
9713 -- Finds component entity corresponding to current component clause (in
9714 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9715 -- start/stop bits for the field. If there is no matching component or
9716 -- if the matching component does not have a component clause, then
9717 -- that's an error and Comp is set to Empty, but no error message is
9718 -- issued, since the message was already given. Comp is also set to
9719 -- Empty if the current "component clause" is in fact a pragma.
9721 -----------------------------
9722 -- Check_Component_Overlap --
9723 -----------------------------
9725 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9726 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9727 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9729 begin
9730 if Present (CC1) and then Present (CC2) then
9732 -- Exclude odd case where we have two tag components in the same
9733 -- record, both at location zero. This seems a bit strange, but
9734 -- it seems to happen in some circumstances, perhaps on an error.
9736 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9737 return;
9738 end if;
9740 -- Here we check if the two fields overlap
9742 declare
9743 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9744 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9745 E1 : constant Uint := S1 + Esize (C1_Ent);
9746 E2 : constant Uint := S2 + Esize (C2_Ent);
9748 begin
9749 if E2 <= S1 or else E1 <= S2 then
9750 null;
9751 else
9752 Error_Msg_Node_2 := Component_Name (CC2);
9753 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9754 Error_Msg_Node_1 := Component_Name (CC1);
9755 Error_Msg_N
9756 ("component& overlaps & #", Component_Name (CC1));
9757 Overlap_Detected := True;
9758 end if;
9759 end;
9760 end if;
9761 end Check_Component_Overlap;
9763 --------------------
9764 -- Find_Component --
9765 --------------------
9767 procedure Find_Component is
9769 procedure Search_Component (R : Entity_Id);
9770 -- Search components of R for a match. If found, Comp is set
9772 ----------------------
9773 -- Search_Component --
9774 ----------------------
9776 procedure Search_Component (R : Entity_Id) is
9777 begin
9778 Comp := First_Component_Or_Discriminant (R);
9779 while Present (Comp) loop
9781 -- Ignore error of attribute name for component name (we
9782 -- already gave an error message for this, so no need to
9783 -- complain here)
9785 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9786 null;
9787 else
9788 exit when Chars (Comp) = Chars (Component_Name (CC));
9789 end if;
9791 Next_Component_Or_Discriminant (Comp);
9792 end loop;
9793 end Search_Component;
9795 -- Start of processing for Find_Component
9797 begin
9798 -- Return with Comp set to Empty if we have a pragma
9800 if Nkind (CC) = N_Pragma then
9801 Comp := Empty;
9802 return;
9803 end if;
9805 -- Search current record for matching component
9807 Search_Component (Rectype);
9809 -- If not found, maybe component of base type discriminant that is
9810 -- absent from statically constrained first subtype.
9812 if No (Comp) then
9813 Search_Component (Base_Type (Rectype));
9814 end if;
9816 -- If no component, or the component does not reference the component
9817 -- clause in question, then there was some previous error for which
9818 -- we already gave a message, so just return with Comp Empty.
9820 if No (Comp) or else Component_Clause (Comp) /= CC then
9821 Check_Error_Detected;
9822 Comp := Empty;
9824 -- Normal case where we have a component clause
9826 else
9827 Fbit := Component_Bit_Offset (Comp);
9828 Lbit := Fbit + Esize (Comp) - 1;
9829 end if;
9830 end Find_Component;
9832 -- Start of processing for Check_Record_Representation_Clause
9834 begin
9835 Find_Type (Ident);
9836 Rectype := Entity (Ident);
9838 if Rectype = Any_Type then
9839 return;
9840 else
9841 Rectype := Underlying_Type (Rectype);
9842 end if;
9844 -- See if we have a fully repped derived tagged type
9846 declare
9847 PS : constant Entity_Id := Parent_Subtype (Rectype);
9849 begin
9850 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9851 Tagged_Parent := PS;
9853 -- Find maximum bit of any component of the parent type
9855 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9856 Pcomp := First_Entity (Tagged_Parent);
9857 while Present (Pcomp) loop
9858 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9859 if Component_Bit_Offset (Pcomp) /= No_Uint
9860 and then Known_Static_Esize (Pcomp)
9861 then
9862 Parent_Last_Bit :=
9863 UI_Max
9864 (Parent_Last_Bit,
9865 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9866 end if;
9868 Next_Entity (Pcomp);
9869 end if;
9870 end loop;
9871 end if;
9872 end;
9874 -- All done if no component clauses
9876 CC := First (Component_Clauses (N));
9878 if No (CC) then
9879 return;
9880 end if;
9882 -- If a tag is present, then create a component clause that places it
9883 -- at the start of the record (otherwise gigi may place it after other
9884 -- fields that have rep clauses).
9886 Fent := First_Entity (Rectype);
9888 if Nkind (Fent) = N_Defining_Identifier
9889 and then Chars (Fent) = Name_uTag
9890 then
9891 Set_Component_Bit_Offset (Fent, Uint_0);
9892 Set_Normalized_Position (Fent, Uint_0);
9893 Set_Normalized_First_Bit (Fent, Uint_0);
9894 Set_Normalized_Position_Max (Fent, Uint_0);
9895 Init_Esize (Fent, System_Address_Size);
9897 Set_Component_Clause (Fent,
9898 Make_Component_Clause (Loc,
9899 Component_Name => Make_Identifier (Loc, Name_uTag),
9901 Position => Make_Integer_Literal (Loc, Uint_0),
9902 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9903 Last_Bit =>
9904 Make_Integer_Literal (Loc,
9905 UI_From_Int (System_Address_Size))));
9907 Ccount := Ccount + 1;
9908 end if;
9910 Max_Bit_So_Far := Uint_Minus_1;
9911 Overlap_Check_Required := False;
9913 -- Process the component clauses
9915 while Present (CC) loop
9916 Find_Component;
9918 if Present (Comp) then
9919 Ccount := Ccount + 1;
9921 -- We need a full overlap check if record positions non-monotonic
9923 if Fbit <= Max_Bit_So_Far then
9924 Overlap_Check_Required := True;
9925 end if;
9927 Max_Bit_So_Far := Lbit;
9929 -- Check bit position out of range of specified size
9931 if Has_Size_Clause (Rectype)
9932 and then RM_Size (Rectype) <= Lbit
9933 then
9934 Error_Msg_N
9935 ("bit number out of range of specified size",
9936 Last_Bit (CC));
9938 -- Check for overlap with tag component
9940 else
9941 if Is_Tagged_Type (Rectype)
9942 and then Fbit < System_Address_Size
9943 then
9944 Error_Msg_NE
9945 ("component overlaps tag field of&",
9946 Component_Name (CC), Rectype);
9947 Overlap_Detected := True;
9948 end if;
9950 if Hbit < Lbit then
9951 Hbit := Lbit;
9952 end if;
9953 end if;
9955 -- Check parent overlap if component might overlap parent field
9957 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9958 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9959 while Present (Pcomp) loop
9960 if not Is_Tag (Pcomp)
9961 and then Chars (Pcomp) /= Name_uParent
9962 then
9963 Check_Component_Overlap (Comp, Pcomp);
9964 end if;
9966 Next_Component_Or_Discriminant (Pcomp);
9967 end loop;
9968 end if;
9969 end if;
9971 Next (CC);
9972 end loop;
9974 -- Now that we have processed all the component clauses, check for
9975 -- overlap. We have to leave this till last, since the components can
9976 -- appear in any arbitrary order in the representation clause.
9978 -- We do not need this check if all specified ranges were monotonic,
9979 -- as recorded by Overlap_Check_Required being False at this stage.
9981 -- This first section checks if there are any overlapping entries at
9982 -- all. It does this by sorting all entries and then seeing if there are
9983 -- any overlaps. If there are none, then that is decisive, but if there
9984 -- are overlaps, they may still be OK (they may result from fields in
9985 -- different variants).
9987 if Overlap_Check_Required then
9988 Overlap_Check1 : declare
9990 OC_Fbit : array (0 .. Ccount) of Uint;
9991 -- First-bit values for component clauses, the value is the offset
9992 -- of the first bit of the field from start of record. The zero
9993 -- entry is for use in sorting.
9995 OC_Lbit : array (0 .. Ccount) of Uint;
9996 -- Last-bit values for component clauses, the value is the offset
9997 -- of the last bit of the field from start of record. The zero
9998 -- entry is for use in sorting.
10000 OC_Count : Natural := 0;
10001 -- Count of entries in OC_Fbit and OC_Lbit
10003 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10004 -- Compare routine for Sort
10006 procedure OC_Move (From : Natural; To : Natural);
10007 -- Move routine for Sort
10009 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
10011 -----------
10012 -- OC_Lt --
10013 -----------
10015 function OC_Lt (Op1, Op2 : Natural) return Boolean is
10016 begin
10017 return OC_Fbit (Op1) < OC_Fbit (Op2);
10018 end OC_Lt;
10020 -------------
10021 -- OC_Move --
10022 -------------
10024 procedure OC_Move (From : Natural; To : Natural) is
10025 begin
10026 OC_Fbit (To) := OC_Fbit (From);
10027 OC_Lbit (To) := OC_Lbit (From);
10028 end OC_Move;
10030 -- Start of processing for Overlap_Check
10032 begin
10033 CC := First (Component_Clauses (N));
10034 while Present (CC) loop
10036 -- Exclude component clause already marked in error
10038 if not Error_Posted (CC) then
10039 Find_Component;
10041 if Present (Comp) then
10042 OC_Count := OC_Count + 1;
10043 OC_Fbit (OC_Count) := Fbit;
10044 OC_Lbit (OC_Count) := Lbit;
10045 end if;
10046 end if;
10048 Next (CC);
10049 end loop;
10051 Sorting.Sort (OC_Count);
10053 Overlap_Check_Required := False;
10054 for J in 1 .. OC_Count - 1 loop
10055 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10056 Overlap_Check_Required := True;
10057 exit;
10058 end if;
10059 end loop;
10060 end Overlap_Check1;
10061 end if;
10063 -- If Overlap_Check_Required is still True, then we have to do the full
10064 -- scale overlap check, since we have at least two fields that do
10065 -- overlap, and we need to know if that is OK since they are in
10066 -- different variant, or whether we have a definite problem.
10068 if Overlap_Check_Required then
10069 Overlap_Check2 : declare
10070 C1_Ent, C2_Ent : Entity_Id;
10071 -- Entities of components being checked for overlap
10073 Clist : Node_Id;
10074 -- Component_List node whose Component_Items are being checked
10076 Citem : Node_Id;
10077 -- Component declaration for component being checked
10079 begin
10080 C1_Ent := First_Entity (Base_Type (Rectype));
10082 -- Loop through all components in record. For each component check
10083 -- for overlap with any of the preceding elements on the component
10084 -- list containing the component and also, if the component is in
10085 -- a variant, check against components outside the case structure.
10086 -- This latter test is repeated recursively up the variant tree.
10088 Main_Component_Loop : while Present (C1_Ent) loop
10089 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10090 goto Continue_Main_Component_Loop;
10091 end if;
10093 -- Skip overlap check if entity has no declaration node. This
10094 -- happens with discriminants in constrained derived types.
10095 -- Possibly we are missing some checks as a result, but that
10096 -- does not seem terribly serious.
10098 if No (Declaration_Node (C1_Ent)) then
10099 goto Continue_Main_Component_Loop;
10100 end if;
10102 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
10104 -- Loop through component lists that need checking. Check the
10105 -- current component list and all lists in variants above us.
10107 Component_List_Loop : loop
10109 -- If derived type definition, go to full declaration
10110 -- If at outer level, check discriminants if there are any.
10112 if Nkind (Clist) = N_Derived_Type_Definition then
10113 Clist := Parent (Clist);
10114 end if;
10116 -- Outer level of record definition, check discriminants
10118 if Nkind_In (Clist, N_Full_Type_Declaration,
10119 N_Private_Type_Declaration)
10120 then
10121 if Has_Discriminants (Defining_Identifier (Clist)) then
10122 C2_Ent :=
10123 First_Discriminant (Defining_Identifier (Clist));
10124 while Present (C2_Ent) loop
10125 exit when C1_Ent = C2_Ent;
10126 Check_Component_Overlap (C1_Ent, C2_Ent);
10127 Next_Discriminant (C2_Ent);
10128 end loop;
10129 end if;
10131 -- Record extension case
10133 elsif Nkind (Clist) = N_Derived_Type_Definition then
10134 Clist := Empty;
10136 -- Otherwise check one component list
10138 else
10139 Citem := First (Component_Items (Clist));
10140 while Present (Citem) loop
10141 if Nkind (Citem) = N_Component_Declaration then
10142 C2_Ent := Defining_Identifier (Citem);
10143 exit when C1_Ent = C2_Ent;
10144 Check_Component_Overlap (C1_Ent, C2_Ent);
10145 end if;
10147 Next (Citem);
10148 end loop;
10149 end if;
10151 -- Check for variants above us (the parent of the Clist can
10152 -- be a variant, in which case its parent is a variant part,
10153 -- and the parent of the variant part is a component list
10154 -- whose components must all be checked against the current
10155 -- component for overlap).
10157 if Nkind (Parent (Clist)) = N_Variant then
10158 Clist := Parent (Parent (Parent (Clist)));
10160 -- Check for possible discriminant part in record, this
10161 -- is treated essentially as another level in the
10162 -- recursion. For this case the parent of the component
10163 -- list is the record definition, and its parent is the
10164 -- full type declaration containing the discriminant
10165 -- specifications.
10167 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10168 Clist := Parent (Parent ((Clist)));
10170 -- If neither of these two cases, we are at the top of
10171 -- the tree.
10173 else
10174 exit Component_List_Loop;
10175 end if;
10176 end loop Component_List_Loop;
10178 <<Continue_Main_Component_Loop>>
10179 Next_Entity (C1_Ent);
10181 end loop Main_Component_Loop;
10182 end Overlap_Check2;
10183 end if;
10185 -- The following circuit deals with warning on record holes (gaps). We
10186 -- skip this check if overlap was detected, since it makes sense for the
10187 -- programmer to fix this illegality before worrying about warnings.
10189 if not Overlap_Detected and Warn_On_Record_Holes then
10190 Record_Hole_Check : declare
10191 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10192 -- Full declaration of record type
10194 procedure Check_Component_List
10195 (CL : Node_Id;
10196 Sbit : Uint;
10197 DS : List_Id);
10198 -- Check component list CL for holes. The starting bit should be
10199 -- Sbit. which is zero for the main record component list and set
10200 -- appropriately for recursive calls for variants. DS is set to
10201 -- a list of discriminant specifications to be included in the
10202 -- consideration of components. It is No_List if none to consider.
10204 --------------------------
10205 -- Check_Component_List --
10206 --------------------------
10208 procedure Check_Component_List
10209 (CL : Node_Id;
10210 Sbit : Uint;
10211 DS : List_Id)
10213 Compl : Integer;
10215 begin
10216 Compl := Integer (List_Length (Component_Items (CL)));
10218 if DS /= No_List then
10219 Compl := Compl + Integer (List_Length (DS));
10220 end if;
10222 declare
10223 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10224 -- Gather components (zero entry is for sort routine)
10226 Ncomps : Natural := 0;
10227 -- Number of entries stored in Comps (starting at Comps (1))
10229 Citem : Node_Id;
10230 -- One component item or discriminant specification
10232 Nbit : Uint;
10233 -- Starting bit for next component
10235 CEnt : Entity_Id;
10236 -- Component entity
10238 Variant : Node_Id;
10239 -- One variant
10241 function Lt (Op1, Op2 : Natural) return Boolean;
10242 -- Compare routine for Sort
10244 procedure Move (From : Natural; To : Natural);
10245 -- Move routine for Sort
10247 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
10249 --------
10250 -- Lt --
10251 --------
10253 function Lt (Op1, Op2 : Natural) return Boolean is
10254 begin
10255 return Component_Bit_Offset (Comps (Op1))
10257 Component_Bit_Offset (Comps (Op2));
10258 end Lt;
10260 ----------
10261 -- Move --
10262 ----------
10264 procedure Move (From : Natural; To : Natural) is
10265 begin
10266 Comps (To) := Comps (From);
10267 end Move;
10269 begin
10270 -- Gather discriminants into Comp
10272 if DS /= No_List then
10273 Citem := First (DS);
10274 while Present (Citem) loop
10275 if Nkind (Citem) = N_Discriminant_Specification then
10276 declare
10277 Ent : constant Entity_Id :=
10278 Defining_Identifier (Citem);
10279 begin
10280 if Ekind (Ent) = E_Discriminant then
10281 Ncomps := Ncomps + 1;
10282 Comps (Ncomps) := Ent;
10283 end if;
10284 end;
10285 end if;
10287 Next (Citem);
10288 end loop;
10289 end if;
10291 -- Gather component entities into Comp
10293 Citem := First (Component_Items (CL));
10294 while Present (Citem) loop
10295 if Nkind (Citem) = N_Component_Declaration then
10296 Ncomps := Ncomps + 1;
10297 Comps (Ncomps) := Defining_Identifier (Citem);
10298 end if;
10300 Next (Citem);
10301 end loop;
10303 -- Now sort the component entities based on the first bit.
10304 -- Note we already know there are no overlapping components.
10306 Sorting.Sort (Ncomps);
10308 -- Loop through entries checking for holes
10310 Nbit := Sbit;
10311 for J in 1 .. Ncomps loop
10312 CEnt := Comps (J);
10313 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
10315 if Error_Msg_Uint_1 > 0 then
10316 Error_Msg_NE
10317 ("?H?^-bit gap before component&",
10318 Component_Name (Component_Clause (CEnt)), CEnt);
10319 end if;
10321 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10322 end loop;
10324 -- Process variant parts recursively if present
10326 if Present (Variant_Part (CL)) then
10327 Variant := First (Variants (Variant_Part (CL)));
10328 while Present (Variant) loop
10329 Check_Component_List
10330 (Component_List (Variant), Nbit, No_List);
10331 Next (Variant);
10332 end loop;
10333 end if;
10334 end;
10335 end Check_Component_List;
10337 -- Start of processing for Record_Hole_Check
10339 begin
10340 declare
10341 Sbit : Uint;
10343 begin
10344 if Is_Tagged_Type (Rectype) then
10345 Sbit := UI_From_Int (System_Address_Size);
10346 else
10347 Sbit := Uint_0;
10348 end if;
10350 if Nkind (Decl) = N_Full_Type_Declaration
10351 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10352 then
10353 Check_Component_List
10354 (Component_List (Type_Definition (Decl)),
10355 Sbit,
10356 Discriminant_Specifications (Decl));
10357 end if;
10358 end;
10359 end Record_Hole_Check;
10360 end if;
10362 -- For records that have component clauses for all components, and whose
10363 -- size is less than or equal to 32, we need to know the size in the
10364 -- front end to activate possible packed array processing where the
10365 -- component type is a record.
10367 -- At this stage Hbit + 1 represents the first unused bit from all the
10368 -- component clauses processed, so if the component clauses are
10369 -- complete, then this is the length of the record.
10371 -- For records longer than System.Storage_Unit, and for those where not
10372 -- all components have component clauses, the back end determines the
10373 -- length (it may for example be appropriate to round up the size
10374 -- to some convenient boundary, based on alignment considerations, etc).
10376 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10378 -- Nothing to do if at least one component has no component clause
10380 Comp := First_Component_Or_Discriminant (Rectype);
10381 while Present (Comp) loop
10382 exit when No (Component_Clause (Comp));
10383 Next_Component_Or_Discriminant (Comp);
10384 end loop;
10386 -- If we fall out of loop, all components have component clauses
10387 -- and so we can set the size to the maximum value.
10389 if No (Comp) then
10390 Set_RM_Size (Rectype, Hbit + 1);
10391 end if;
10392 end if;
10393 end Check_Record_Representation_Clause;
10395 ----------------
10396 -- Check_Size --
10397 ----------------
10399 procedure Check_Size
10400 (N : Node_Id;
10401 T : Entity_Id;
10402 Siz : Uint;
10403 Biased : out Boolean)
10405 UT : constant Entity_Id := Underlying_Type (T);
10406 M : Uint;
10408 begin
10409 Biased := False;
10411 -- Reject patently improper size values.
10413 if Is_Elementary_Type (T)
10414 and then Siz > UI_From_Int (Int'Last)
10415 then
10416 Error_Msg_N ("Size value too large for elementary type", N);
10418 if Nkind (Original_Node (N)) = N_Op_Expon then
10419 Error_Msg_N
10420 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10421 end if;
10422 end if;
10424 -- Dismiss generic types
10426 if Is_Generic_Type (T)
10427 or else
10428 Is_Generic_Type (UT)
10429 or else
10430 Is_Generic_Type (Root_Type (UT))
10431 then
10432 return;
10434 -- Guard against previous errors
10436 elsif No (UT) or else UT = Any_Type then
10437 Check_Error_Detected;
10438 return;
10440 -- Check case of bit packed array
10442 elsif Is_Array_Type (UT)
10443 and then Known_Static_Component_Size (UT)
10444 and then Is_Bit_Packed_Array (UT)
10445 then
10446 declare
10447 Asiz : Uint;
10448 Indx : Node_Id;
10449 Ityp : Entity_Id;
10451 begin
10452 Asiz := Component_Size (UT);
10453 Indx := First_Index (UT);
10454 loop
10455 Ityp := Etype (Indx);
10457 -- If non-static bound, then we are not in the business of
10458 -- trying to check the length, and indeed an error will be
10459 -- issued elsewhere, since sizes of non-static array types
10460 -- cannot be set implicitly or explicitly.
10462 if not Is_OK_Static_Subtype (Ityp) then
10463 return;
10464 end if;
10466 -- Otherwise accumulate next dimension
10468 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10469 Expr_Value (Type_Low_Bound (Ityp)) +
10470 Uint_1);
10472 Next_Index (Indx);
10473 exit when No (Indx);
10474 end loop;
10476 if Asiz <= Siz then
10477 return;
10479 else
10480 Error_Msg_Uint_1 := Asiz;
10481 Error_Msg_NE
10482 ("size for& too small, minimum allowed is ^", N, T);
10483 Set_Esize (T, Asiz);
10484 Set_RM_Size (T, Asiz);
10485 end if;
10486 end;
10488 -- All other composite types are ignored
10490 elsif Is_Composite_Type (UT) then
10491 return;
10493 -- For fixed-point types, don't check minimum if type is not frozen,
10494 -- since we don't know all the characteristics of the type that can
10495 -- affect the size (e.g. a specified small) till freeze time.
10497 elsif Is_Fixed_Point_Type (UT)
10498 and then not Is_Frozen (UT)
10499 then
10500 null;
10502 -- Cases for which a minimum check is required
10504 else
10505 -- Ignore if specified size is correct for the type
10507 if Known_Esize (UT) and then Siz = Esize (UT) then
10508 return;
10509 end if;
10511 -- Otherwise get minimum size
10513 M := UI_From_Int (Minimum_Size (UT));
10515 if Siz < M then
10517 -- Size is less than minimum size, but one possibility remains
10518 -- that we can manage with the new size if we bias the type.
10520 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10522 if Siz < M then
10523 Error_Msg_Uint_1 := M;
10524 Error_Msg_NE
10525 ("size for& too small, minimum allowed is ^", N, T);
10526 Set_Esize (T, M);
10527 Set_RM_Size (T, M);
10528 else
10529 Biased := True;
10530 end if;
10531 end if;
10532 end if;
10533 end Check_Size;
10535 --------------------------
10536 -- Freeze_Entity_Checks --
10537 --------------------------
10539 procedure Freeze_Entity_Checks (N : Node_Id) is
10540 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10541 -- Inspect the primitive operations of type Typ and hide all pairs of
10542 -- implicitly declared non-overridden non-fully conformant homographs
10543 -- (Ada RM 8.3 12.3/2).
10545 -------------------------------------
10546 -- Hide_Non_Overridden_Subprograms --
10547 -------------------------------------
10549 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10550 procedure Hide_Matching_Homographs
10551 (Subp_Id : Entity_Id;
10552 Start_Elmt : Elmt_Id);
10553 -- Inspect a list of primitive operations starting with Start_Elmt
10554 -- and find matching implicitly declared non-overridden non-fully
10555 -- conformant homographs of Subp_Id. If found, all matches along
10556 -- with Subp_Id are hidden from all visibility.
10558 function Is_Non_Overridden_Or_Null_Procedure
10559 (Subp_Id : Entity_Id) return Boolean;
10560 -- Determine whether subprogram Subp_Id is implicitly declared non-
10561 -- overridden subprogram or an implicitly declared null procedure.
10563 ------------------------------
10564 -- Hide_Matching_Homographs --
10565 ------------------------------
10567 procedure Hide_Matching_Homographs
10568 (Subp_Id : Entity_Id;
10569 Start_Elmt : Elmt_Id)
10571 Prim : Entity_Id;
10572 Prim_Elmt : Elmt_Id;
10574 begin
10575 Prim_Elmt := Start_Elmt;
10576 while Present (Prim_Elmt) loop
10577 Prim := Node (Prim_Elmt);
10579 -- The current primitive is implicitly declared non-overridden
10580 -- non-fully conformant homograph of Subp_Id. Both subprograms
10581 -- must be hidden from visibility.
10583 if Chars (Prim) = Chars (Subp_Id)
10584 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10585 and then not Fully_Conformant (Prim, Subp_Id)
10586 then
10587 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10588 Set_Is_Immediately_Visible (Prim, False);
10589 Set_Is_Potentially_Use_Visible (Prim, False);
10591 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10592 Set_Is_Immediately_Visible (Subp_Id, False);
10593 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10594 end if;
10596 Next_Elmt (Prim_Elmt);
10597 end loop;
10598 end Hide_Matching_Homographs;
10600 -----------------------------------------
10601 -- Is_Non_Overridden_Or_Null_Procedure --
10602 -----------------------------------------
10604 function Is_Non_Overridden_Or_Null_Procedure
10605 (Subp_Id : Entity_Id) return Boolean
10607 Alias_Id : Entity_Id;
10609 begin
10610 -- The subprogram is inherited (implicitly declared), it does not
10611 -- override and does not cover a primitive of an interface.
10613 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10614 and then Present (Alias (Subp_Id))
10615 and then No (Interface_Alias (Subp_Id))
10616 and then No (Overridden_Operation (Subp_Id))
10617 then
10618 Alias_Id := Alias (Subp_Id);
10620 if Requires_Overriding (Alias_Id) then
10621 return True;
10623 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10624 and then Null_Present (Parent (Alias_Id))
10625 then
10626 return True;
10627 end if;
10628 end if;
10630 return False;
10631 end Is_Non_Overridden_Or_Null_Procedure;
10633 -- Local variables
10635 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10636 Prim : Entity_Id;
10637 Prim_Elmt : Elmt_Id;
10639 -- Start of processing for Hide_Non_Overridden_Subprograms
10641 begin
10642 -- Inspect the list of primitives looking for non-overridden
10643 -- subprograms.
10645 if Present (Prim_Ops) then
10646 Prim_Elmt := First_Elmt (Prim_Ops);
10647 while Present (Prim_Elmt) loop
10648 Prim := Node (Prim_Elmt);
10649 Next_Elmt (Prim_Elmt);
10651 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10652 Hide_Matching_Homographs
10653 (Subp_Id => Prim,
10654 Start_Elmt => Prim_Elmt);
10655 end if;
10656 end loop;
10657 end if;
10658 end Hide_Non_Overridden_Subprograms;
10660 ---------------------
10661 -- Local variables --
10662 ---------------------
10664 E : constant Entity_Id := Entity (N);
10666 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10667 -- True in non-generic case. Some of the processing here is skipped
10668 -- for the generic case since it is not needed. Basically in the
10669 -- generic case, we only need to do stuff that might generate error
10670 -- messages or warnings.
10672 -- Start of processing for Freeze_Entity_Checks
10674 begin
10675 -- Remember that we are processing a freezing entity. Required to
10676 -- ensure correct decoration of internal entities associated with
10677 -- interfaces (see New_Overloaded_Entity).
10679 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10681 -- For tagged types covering interfaces add internal entities that link
10682 -- the primitives of the interfaces with the primitives that cover them.
10683 -- Note: These entities were originally generated only when generating
10684 -- code because their main purpose was to provide support to initialize
10685 -- the secondary dispatch tables. They are now generated also when
10686 -- compiling with no code generation to provide ASIS the relationship
10687 -- between interface primitives and tagged type primitives. They are
10688 -- also used to locate primitives covering interfaces when processing
10689 -- generics (see Derive_Subprograms).
10691 -- This is not needed in the generic case
10693 if Ada_Version >= Ada_2005
10694 and then Non_Generic_Case
10695 and then Ekind (E) = E_Record_Type
10696 and then Is_Tagged_Type (E)
10697 and then not Is_Interface (E)
10698 and then Has_Interfaces (E)
10699 then
10700 -- This would be a good common place to call the routine that checks
10701 -- overriding of interface primitives (and thus factorize calls to
10702 -- Check_Abstract_Overriding located at different contexts in the
10703 -- compiler). However, this is not possible because it causes
10704 -- spurious errors in case of late overriding.
10706 Add_Internal_Interface_Entities (E);
10707 end if;
10709 -- After all forms of overriding have been resolved, a tagged type may
10710 -- be left with a set of implicitly declared and possibly erroneous
10711 -- abstract subprograms, null procedures and subprograms that require
10712 -- overriding. If this set contains fully conformat homographs, then one
10713 -- is chosen arbitrarily (already done during resolution), otherwise all
10714 -- remaining non-fully conformant homographs are hidden from visibility
10715 -- (Ada RM 8.3 12.3/2).
10717 if Is_Tagged_Type (E) then
10718 Hide_Non_Overridden_Subprograms (E);
10719 end if;
10721 -- Check CPP types
10723 if Ekind (E) = E_Record_Type
10724 and then Is_CPP_Class (E)
10725 and then Is_Tagged_Type (E)
10726 and then Tagged_Type_Expansion
10727 then
10728 if CPP_Num_Prims (E) = 0 then
10730 -- If the CPP type has user defined components then it must import
10731 -- primitives from C++. This is required because if the C++ class
10732 -- has no primitives then the C++ compiler does not added the _tag
10733 -- component to the type.
10735 if First_Entity (E) /= Last_Entity (E) then
10736 Error_Msg_N
10737 ("'C'P'P type must import at least one primitive from C++??",
10739 end if;
10740 end if;
10742 -- Check that all its primitives are abstract or imported from C++.
10743 -- Check also availability of the C++ constructor.
10745 declare
10746 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10747 Elmt : Elmt_Id;
10748 Error_Reported : Boolean := False;
10749 Prim : Node_Id;
10751 begin
10752 Elmt := First_Elmt (Primitive_Operations (E));
10753 while Present (Elmt) loop
10754 Prim := Node (Elmt);
10756 if Comes_From_Source (Prim) then
10757 if Is_Abstract_Subprogram (Prim) then
10758 null;
10760 elsif not Is_Imported (Prim)
10761 or else Convention (Prim) /= Convention_CPP
10762 then
10763 Error_Msg_N
10764 ("primitives of 'C'P'P types must be imported from C++ "
10765 & "or abstract??", Prim);
10767 elsif not Has_Constructors
10768 and then not Error_Reported
10769 then
10770 Error_Msg_Name_1 := Chars (E);
10771 Error_Msg_N
10772 ("??'C'P'P constructor required for type %", Prim);
10773 Error_Reported := True;
10774 end if;
10775 end if;
10777 Next_Elmt (Elmt);
10778 end loop;
10779 end;
10780 end if;
10782 -- Check Ada derivation of CPP type
10784 if Expander_Active -- why? losing errors in -gnatc mode???
10785 and then Present (Etype (E)) -- defend against errors
10786 and then Tagged_Type_Expansion
10787 and then Ekind (E) = E_Record_Type
10788 and then Etype (E) /= E
10789 and then Is_CPP_Class (Etype (E))
10790 and then CPP_Num_Prims (Etype (E)) > 0
10791 and then not Is_CPP_Class (E)
10792 and then not Has_CPP_Constructors (Etype (E))
10793 then
10794 -- If the parent has C++ primitives but it has no constructor then
10795 -- check that all the primitives are overridden in this derivation;
10796 -- otherwise the constructor of the parent is needed to build the
10797 -- dispatch table.
10799 declare
10800 Elmt : Elmt_Id;
10801 Prim : Node_Id;
10803 begin
10804 Elmt := First_Elmt (Primitive_Operations (E));
10805 while Present (Elmt) loop
10806 Prim := Node (Elmt);
10808 if not Is_Abstract_Subprogram (Prim)
10809 and then No (Interface_Alias (Prim))
10810 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10811 then
10812 Error_Msg_Name_1 := Chars (Etype (E));
10813 Error_Msg_N
10814 ("'C'P'P constructor required for parent type %", E);
10815 exit;
10816 end if;
10818 Next_Elmt (Elmt);
10819 end loop;
10820 end;
10821 end if;
10823 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10825 -- If we have a type with predicates, build predicate function. This
10826 -- is not needed in the generic case, and is not needed within TSS
10827 -- subprograms and other predefined primitives.
10829 if Non_Generic_Case
10830 and then Is_Type (E)
10831 and then Has_Predicates (E)
10832 and then not Within_Internal_Subprogram
10833 then
10834 Build_Predicate_Functions (E, N);
10835 end if;
10837 -- If type has delayed aspects, this is where we do the preanalysis at
10838 -- the freeze point, as part of the consistent visibility check. Note
10839 -- that this must be done after calling Build_Predicate_Functions or
10840 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10841 -- the subtype name in the saved expression so that they will not cause
10842 -- trouble in the preanalysis.
10844 -- This is also not needed in the generic case
10846 if Non_Generic_Case
10847 and then Has_Delayed_Aspects (E)
10848 and then Scope (E) = Current_Scope
10849 then
10850 -- Retrieve the visibility to the discriminants in order to properly
10851 -- analyze the aspects.
10853 Push_Scope_And_Install_Discriminants (E);
10855 declare
10856 Ritem : Node_Id;
10858 begin
10859 -- Look for aspect specification entries for this entity
10861 Ritem := First_Rep_Item (E);
10862 while Present (Ritem) loop
10863 if Nkind (Ritem) = N_Aspect_Specification
10864 and then Entity (Ritem) = E
10865 and then Is_Delayed_Aspect (Ritem)
10866 then
10867 Check_Aspect_At_Freeze_Point (Ritem);
10868 end if;
10870 Next_Rep_Item (Ritem);
10871 end loop;
10872 end;
10874 Uninstall_Discriminants_And_Pop_Scope (E);
10875 end if;
10877 -- For a record type, deal with variant parts. This has to be delayed
10878 -- to this point, because of the issue of statically predicated
10879 -- subtypes, which we have to ensure are frozen before checking
10880 -- choices, since we need to have the static choice list set.
10882 if Is_Record_Type (E) then
10883 Check_Variant_Part : declare
10884 D : constant Node_Id := Declaration_Node (E);
10885 T : Node_Id;
10886 C : Node_Id;
10887 VP : Node_Id;
10889 Others_Present : Boolean;
10890 pragma Warnings (Off, Others_Present);
10891 -- Indicates others present, not used in this case
10893 procedure Non_Static_Choice_Error (Choice : Node_Id);
10894 -- Error routine invoked by the generic instantiation below when
10895 -- the variant part has a non static choice.
10897 procedure Process_Declarations (Variant : Node_Id);
10898 -- Processes declarations associated with a variant. We analyzed
10899 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10900 -- but we still need the recursive call to Check_Choices for any
10901 -- nested variant to get its choices properly processed. This is
10902 -- also where we expand out the choices if expansion is active.
10904 package Variant_Choices_Processing is new
10905 Generic_Check_Choices
10906 (Process_Empty_Choice => No_OP,
10907 Process_Non_Static_Choice => Non_Static_Choice_Error,
10908 Process_Associated_Node => Process_Declarations);
10909 use Variant_Choices_Processing;
10911 -----------------------------
10912 -- Non_Static_Choice_Error --
10913 -----------------------------
10915 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10916 begin
10917 Flag_Non_Static_Expr
10918 ("choice given in variant part is not static!", Choice);
10919 end Non_Static_Choice_Error;
10921 --------------------------
10922 -- Process_Declarations --
10923 --------------------------
10925 procedure Process_Declarations (Variant : Node_Id) is
10926 CL : constant Node_Id := Component_List (Variant);
10927 VP : Node_Id;
10929 begin
10930 -- Check for static predicate present in this variant
10932 if Has_SP_Choice (Variant) then
10934 -- Here we expand. You might expect to find this call in
10935 -- Expand_N_Variant_Part, but that is called when we first
10936 -- see the variant part, and we cannot do this expansion
10937 -- earlier than the freeze point, since for statically
10938 -- predicated subtypes, the predicate is not known till
10939 -- the freeze point.
10941 -- Furthermore, we do this expansion even if the expander
10942 -- is not active, because other semantic processing, e.g.
10943 -- for aggregates, requires the expanded list of choices.
10945 -- If the expander is not active, then we can't just clobber
10946 -- the list since it would invalidate the ASIS -gnatct tree.
10947 -- So we have to rewrite the variant part with a Rewrite
10948 -- call that replaces it with a copy and clobber the copy.
10950 if not Expander_Active then
10951 declare
10952 NewV : constant Node_Id := New_Copy (Variant);
10953 begin
10954 Set_Discrete_Choices
10955 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10956 Rewrite (Variant, NewV);
10957 end;
10958 end if;
10960 Expand_Static_Predicates_In_Choices (Variant);
10961 end if;
10963 -- We don't need to worry about the declarations in the variant
10964 -- (since they were analyzed by Analyze_Choices when we first
10965 -- encountered the variant), but we do need to take care of
10966 -- expansion of any nested variants.
10968 if not Null_Present (CL) then
10969 VP := Variant_Part (CL);
10971 if Present (VP) then
10972 Check_Choices
10973 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10974 end if;
10975 end if;
10976 end Process_Declarations;
10978 -- Start of processing for Check_Variant_Part
10980 begin
10981 -- Find component list
10983 C := Empty;
10985 if Nkind (D) = N_Full_Type_Declaration then
10986 T := Type_Definition (D);
10988 if Nkind (T) = N_Record_Definition then
10989 C := Component_List (T);
10991 elsif Nkind (T) = N_Derived_Type_Definition
10992 and then Present (Record_Extension_Part (T))
10993 then
10994 C := Component_List (Record_Extension_Part (T));
10995 end if;
10996 end if;
10998 -- Case of variant part present
11000 if Present (C) and then Present (Variant_Part (C)) then
11001 VP := Variant_Part (C);
11003 -- Check choices
11005 Check_Choices
11006 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11008 -- If the last variant does not contain the Others choice,
11009 -- replace it with an N_Others_Choice node since Gigi always
11010 -- wants an Others. Note that we do not bother to call Analyze
11011 -- on the modified variant part, since its only effect would be
11012 -- to compute the Others_Discrete_Choices node laboriously, and
11013 -- of course we already know the list of choices corresponding
11014 -- to the others choice (it's the list we're replacing).
11016 -- We only want to do this if the expander is active, since
11017 -- we do not want to clobber the ASIS tree.
11019 if Expander_Active then
11020 declare
11021 Last_Var : constant Node_Id :=
11022 Last_Non_Pragma (Variants (VP));
11024 Others_Node : Node_Id;
11026 begin
11027 if Nkind (First (Discrete_Choices (Last_Var))) /=
11028 N_Others_Choice
11029 then
11030 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11031 Set_Others_Discrete_Choices
11032 (Others_Node, Discrete_Choices (Last_Var));
11033 Set_Discrete_Choices
11034 (Last_Var, New_List (Others_Node));
11035 end if;
11036 end;
11037 end if;
11038 end if;
11039 end Check_Variant_Part;
11040 end if;
11041 end Freeze_Entity_Checks;
11043 -------------------------
11044 -- Get_Alignment_Value --
11045 -------------------------
11047 function Get_Alignment_Value (Expr : Node_Id) return Uint is
11048 Align : constant Uint := Static_Integer (Expr);
11050 begin
11051 if Align = No_Uint then
11052 return No_Uint;
11054 elsif Align <= 0 then
11055 Error_Msg_N ("alignment value must be positive", Expr);
11056 return No_Uint;
11058 else
11059 for J in Int range 0 .. 64 loop
11060 declare
11061 M : constant Uint := Uint_2 ** J;
11063 begin
11064 exit when M = Align;
11066 if M > Align then
11067 Error_Msg_N
11068 ("alignment value must be power of 2", Expr);
11069 return No_Uint;
11070 end if;
11071 end;
11072 end loop;
11074 return Align;
11075 end if;
11076 end Get_Alignment_Value;
11078 -------------------------------------
11079 -- Inherit_Aspects_At_Freeze_Point --
11080 -------------------------------------
11082 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11083 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11084 (Rep_Item : Node_Id) return Boolean;
11085 -- This routine checks if Rep_Item is either a pragma or an aspect
11086 -- specification node whose correponding pragma (if any) is present in
11087 -- the Rep Item chain of the entity it has been specified to.
11089 --------------------------------------------------
11090 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11091 --------------------------------------------------
11093 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11094 (Rep_Item : Node_Id) return Boolean
11096 begin
11097 return
11098 Nkind (Rep_Item) = N_Pragma
11099 or else Present_In_Rep_Item
11100 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
11101 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11103 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11105 begin
11106 -- A representation item is either subtype-specific (Size and Alignment
11107 -- clauses) or type-related (all others). Subtype-specific aspects may
11108 -- differ for different subtypes of the same type (RM 13.1.8).
11110 -- A derived type inherits each type-related representation aspect of
11111 -- its parent type that was directly specified before the declaration of
11112 -- the derived type (RM 13.1.15).
11114 -- A derived subtype inherits each subtype-specific representation
11115 -- aspect of its parent subtype that was directly specified before the
11116 -- declaration of the derived type (RM 13.1.15).
11118 -- The general processing involves inheriting a representation aspect
11119 -- from a parent type whenever the first rep item (aspect specification,
11120 -- attribute definition clause, pragma) corresponding to the given
11121 -- representation aspect in the rep item chain of Typ, if any, isn't
11122 -- directly specified to Typ but to one of its parents.
11124 -- ??? Note that, for now, just a limited number of representation
11125 -- aspects have been inherited here so far. Many of them are
11126 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11127 -- a non- exhaustive list of aspects that likely also need to
11128 -- be moved to this routine: Alignment, Component_Alignment,
11129 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
11130 -- Preelaborable_Initialization, RM_Size and Small.
11132 -- In addition, Convention must be propagated from base type to subtype,
11133 -- because the subtype may have been declared on an incomplete view.
11135 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11136 return;
11137 end if;
11139 -- Ada_05/Ada_2005
11141 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11142 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11143 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11144 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11145 then
11146 Set_Is_Ada_2005_Only (Typ);
11147 end if;
11149 -- Ada_12/Ada_2012
11151 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11152 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11153 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11154 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11155 then
11156 Set_Is_Ada_2012_Only (Typ);
11157 end if;
11159 -- Atomic/Shared
11161 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11162 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11163 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11164 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11165 then
11166 Set_Is_Atomic (Typ);
11167 Set_Is_Volatile (Typ);
11168 Set_Treat_As_Volatile (Typ);
11169 end if;
11171 -- Convention
11173 if Is_Record_Type (Typ)
11174 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11175 then
11176 Set_Convention (Typ, Convention (Base_Type (Typ)));
11177 end if;
11179 -- Default_Component_Value
11181 -- Verify that there is no rep_item declared for the type, and there
11182 -- is one coming from an ancestor.
11184 if Is_Array_Type (Typ)
11185 and then Is_Base_Type (Typ)
11186 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
11187 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11188 then
11189 Set_Default_Aspect_Component_Value (Typ,
11190 Default_Aspect_Component_Value
11191 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11192 end if;
11194 -- Default_Value
11196 if Is_Scalar_Type (Typ)
11197 and then Is_Base_Type (Typ)
11198 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
11199 and then Has_Rep_Item (Typ, Name_Default_Value)
11200 then
11201 Set_Has_Default_Aspect (Typ);
11202 Set_Default_Aspect_Value (Typ,
11203 Default_Aspect_Value
11204 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11205 end if;
11207 -- Discard_Names
11209 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11210 and then Has_Rep_Item (Typ, Name_Discard_Names)
11211 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11212 (Get_Rep_Item (Typ, Name_Discard_Names))
11213 then
11214 Set_Discard_Names (Typ);
11215 end if;
11217 -- Invariants
11219 if not Has_Rep_Item (Typ, Name_Invariant, False)
11220 and then Has_Rep_Item (Typ, Name_Invariant)
11221 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11222 (Get_Rep_Item (Typ, Name_Invariant))
11223 then
11224 Set_Has_Invariants (Typ);
11226 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
11227 Set_Has_Inheritable_Invariants (Typ);
11228 end if;
11230 -- If we have a subtype with invariants, whose base type does not have
11231 -- invariants, copy these invariants to the base type. This happens for
11232 -- the case of implicit base types created for scalar and array types.
11234 elsif Has_Invariants (Typ)
11235 and then not Has_Invariants (Base_Type (Typ))
11236 then
11237 Set_Has_Invariants (Base_Type (Typ));
11238 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
11239 end if;
11241 -- Volatile
11243 if not Has_Rep_Item (Typ, Name_Volatile, False)
11244 and then Has_Rep_Item (Typ, Name_Volatile)
11245 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11246 (Get_Rep_Item (Typ, Name_Volatile))
11247 then
11248 Set_Is_Volatile (Typ);
11249 Set_Treat_As_Volatile (Typ);
11250 end if;
11252 -- Volatile_Full_Access
11254 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11255 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11256 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11257 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11258 then
11259 Set_Is_Volatile_Full_Access (Typ);
11260 Set_Is_Volatile (Typ);
11261 Set_Treat_As_Volatile (Typ);
11262 end if;
11264 -- Inheritance for derived types only
11266 if Is_Derived_Type (Typ) then
11267 declare
11268 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11269 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11271 begin
11272 -- Atomic_Components
11274 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11275 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11276 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11277 (Get_Rep_Item (Typ, Name_Atomic_Components))
11278 then
11279 Set_Has_Atomic_Components (Imp_Bas_Typ);
11280 end if;
11282 -- Volatile_Components
11284 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11285 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11286 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11287 (Get_Rep_Item (Typ, Name_Volatile_Components))
11288 then
11289 Set_Has_Volatile_Components (Imp_Bas_Typ);
11290 end if;
11292 -- Finalize_Storage_Only
11294 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11295 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11296 then
11297 Set_Finalize_Storage_Only (Bas_Typ);
11298 end if;
11300 -- Universal_Aliasing
11302 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11303 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11304 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11305 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11306 then
11307 Set_Universal_Aliasing (Imp_Bas_Typ);
11308 end if;
11310 -- Bit_Order
11312 if Is_Record_Type (Typ) then
11313 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11314 and then Has_Rep_Item (Typ, Name_Bit_Order)
11315 then
11316 Set_Reverse_Bit_Order (Bas_Typ,
11317 Reverse_Bit_Order (Entity (Name
11318 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11319 end if;
11320 end if;
11322 -- Scalar_Storage_Order
11324 -- Note: the aspect is specified on a first subtype, but recorded
11325 -- in a flag of the base type!
11327 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11328 and then Typ = Bas_Typ
11329 then
11330 -- For a type extension, always inherit from parent; otherwise
11331 -- inherit if no default applies. Note: we do not check for
11332 -- an explicit rep item on the parent type when inheriting,
11333 -- because the parent SSO may itself have been set by default.
11335 if not Has_Rep_Item (First_Subtype (Typ),
11336 Name_Scalar_Storage_Order, False)
11337 and then (Is_Tagged_Type (Bas_Typ)
11338 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11339 or else
11340 SSO_Set_High_By_Default (Bas_Typ)))
11341 then
11342 Set_Reverse_Storage_Order (Bas_Typ,
11343 Reverse_Storage_Order
11344 (Implementation_Base_Type (Etype (Bas_Typ))));
11346 -- Clear default SSO indications, since the inherited aspect
11347 -- which was set explicitly overrides the default.
11349 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11350 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11351 end if;
11352 end if;
11353 end;
11354 end if;
11355 end Inherit_Aspects_At_Freeze_Point;
11357 ----------------
11358 -- Initialize --
11359 ----------------
11361 procedure Initialize is
11362 begin
11363 Address_Clause_Checks.Init;
11364 Unchecked_Conversions.Init;
11366 if VM_Target /= No_VM or else AAMP_On_Target then
11367 Independence_Checks.Init;
11368 end if;
11369 end Initialize;
11371 ---------------------------
11372 -- Install_Discriminants --
11373 ---------------------------
11375 procedure Install_Discriminants (E : Entity_Id) is
11376 Disc : Entity_Id;
11377 Prev : Entity_Id;
11378 begin
11379 Disc := First_Discriminant (E);
11380 while Present (Disc) loop
11381 Prev := Current_Entity (Disc);
11382 Set_Current_Entity (Disc);
11383 Set_Is_Immediately_Visible (Disc);
11384 Set_Homonym (Disc, Prev);
11385 Next_Discriminant (Disc);
11386 end loop;
11387 end Install_Discriminants;
11389 -------------------------
11390 -- Is_Operational_Item --
11391 -------------------------
11393 function Is_Operational_Item (N : Node_Id) return Boolean is
11394 begin
11395 if Nkind (N) /= N_Attribute_Definition_Clause then
11396 return False;
11398 else
11399 declare
11400 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11401 begin
11402 return Id = Attribute_Input
11403 or else Id = Attribute_Output
11404 or else Id = Attribute_Read
11405 or else Id = Attribute_Write
11406 or else Id = Attribute_External_Tag;
11407 end;
11408 end if;
11409 end Is_Operational_Item;
11411 -------------------------
11412 -- Is_Predicate_Static --
11413 -------------------------
11415 -- Note: the basic legality of the expression has already been checked, so
11416 -- we don't need to worry about cases or ranges on strings for example.
11418 function Is_Predicate_Static
11419 (Expr : Node_Id;
11420 Nam : Name_Id) return Boolean
11422 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11423 -- Given a list of case expression alternatives, returns True if all
11424 -- the alternatives are static (have all static choices, and a static
11425 -- expression).
11427 function All_Static_Choices (L : List_Id) return Boolean;
11428 -- Returns true if all elements of the list are OK static choices
11429 -- as defined below for Is_Static_Choice. Used for case expression
11430 -- alternatives and for the right operand of a membership test. An
11431 -- others_choice is static if the corresponding expression is static.
11432 -- The staticness of the bounds is checked separately.
11434 function Is_Static_Choice (N : Node_Id) return Boolean;
11435 -- Returns True if N represents a static choice (static subtype, or
11436 -- static subtype indication, or static expression, or static range).
11438 -- Note that this is a bit more inclusive than we actually need
11439 -- (in particular membership tests do not allow the use of subtype
11440 -- indications). But that doesn't matter, we have already checked
11441 -- that the construct is legal to get this far.
11443 function Is_Type_Ref (N : Node_Id) return Boolean;
11444 pragma Inline (Is_Type_Ref);
11445 -- Returns True if N is a reference to the type for the predicate in the
11446 -- expression (i.e. if it is an identifier whose Chars field matches the
11447 -- Nam given in the call). N must not be parenthesized, if the type name
11448 -- appears in parens, this routine will return False.
11450 ----------------------------------
11451 -- All_Static_Case_Alternatives --
11452 ----------------------------------
11454 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11455 N : Node_Id;
11457 begin
11458 N := First (L);
11459 while Present (N) loop
11460 if not (All_Static_Choices (Discrete_Choices (N))
11461 and then Is_OK_Static_Expression (Expression (N)))
11462 then
11463 return False;
11464 end if;
11466 Next (N);
11467 end loop;
11469 return True;
11470 end All_Static_Case_Alternatives;
11472 ------------------------
11473 -- All_Static_Choices --
11474 ------------------------
11476 function All_Static_Choices (L : List_Id) return Boolean is
11477 N : Node_Id;
11479 begin
11480 N := First (L);
11481 while Present (N) loop
11482 if not Is_Static_Choice (N) then
11483 return False;
11484 end if;
11486 Next (N);
11487 end loop;
11489 return True;
11490 end All_Static_Choices;
11492 ----------------------
11493 -- Is_Static_Choice --
11494 ----------------------
11496 function Is_Static_Choice (N : Node_Id) return Boolean is
11497 begin
11498 return Nkind (N) = N_Others_Choice
11499 or else Is_OK_Static_Expression (N)
11500 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11501 and then Is_OK_Static_Subtype (Entity (N)))
11502 or else (Nkind (N) = N_Subtype_Indication
11503 and then Is_OK_Static_Subtype (Entity (N)))
11504 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11505 end Is_Static_Choice;
11507 -----------------
11508 -- Is_Type_Ref --
11509 -----------------
11511 function Is_Type_Ref (N : Node_Id) return Boolean is
11512 begin
11513 return Nkind (N) = N_Identifier
11514 and then Chars (N) = Nam
11515 and then Paren_Count (N) = 0;
11516 end Is_Type_Ref;
11518 -- Start of processing for Is_Predicate_Static
11520 begin
11521 -- Predicate_Static means one of the following holds. Numbers are the
11522 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11524 -- 16: A static expression
11526 if Is_OK_Static_Expression (Expr) then
11527 return True;
11529 -- 17: A membership test whose simple_expression is the current
11530 -- instance, and whose membership_choice_list meets the requirements
11531 -- for a static membership test.
11533 elsif Nkind (Expr) in N_Membership_Test
11534 and then ((Present (Right_Opnd (Expr))
11535 and then Is_Static_Choice (Right_Opnd (Expr)))
11536 or else
11537 (Present (Alternatives (Expr))
11538 and then All_Static_Choices (Alternatives (Expr))))
11539 then
11540 return True;
11542 -- 18. A case_expression whose selecting_expression is the current
11543 -- instance, and whose dependent expressions are static expressions.
11545 elsif Nkind (Expr) = N_Case_Expression
11546 and then Is_Type_Ref (Expression (Expr))
11547 and then All_Static_Case_Alternatives (Alternatives (Expr))
11548 then
11549 return True;
11551 -- 19. A call to a predefined equality or ordering operator, where one
11552 -- operand is the current instance, and the other is a static
11553 -- expression.
11555 -- Note: the RM is clearly wrong here in not excluding string types.
11556 -- Without this exclusion, we would allow expressions like X > "ABC"
11557 -- to be considered as predicate-static, which is clearly not intended,
11558 -- since the idea is for predicate-static to be a subset of normal
11559 -- static expressions (and "DEF" > "ABC" is not a static expression).
11561 -- However, we do allow internally generated (not from source) equality
11562 -- and inequality operations to be valid on strings (this helps deal
11563 -- with cases where we transform A in "ABC" to A = "ABC).
11565 elsif Nkind (Expr) in N_Op_Compare
11566 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11567 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11568 and then not Comes_From_Source (Expr)))
11569 and then ((Is_Type_Ref (Left_Opnd (Expr))
11570 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11571 or else
11572 (Is_Type_Ref (Right_Opnd (Expr))
11573 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11574 then
11575 return True;
11577 -- 20. A call to a predefined boolean logical operator, where each
11578 -- operand is predicate-static.
11580 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11581 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11582 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11583 or else
11584 (Nkind (Expr) = N_Op_Not
11585 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11586 then
11587 return True;
11589 -- 21. A short-circuit control form where both operands are
11590 -- predicate-static.
11592 elsif Nkind (Expr) in N_Short_Circuit
11593 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11594 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11595 then
11596 return True;
11598 -- 22. A parenthesized predicate-static expression. This does not
11599 -- require any special test, since we just ignore paren levels in
11600 -- all the cases above.
11602 -- One more test that is an implementation artifact caused by the fact
11603 -- that we are analyzing not the original expression, but the generated
11604 -- expression in the body of the predicate function. This can include
11605 -- references to inherited predicates, so that the expression we are
11606 -- processing looks like:
11608 -- expression and then xxPredicate (typ (Inns))
11610 -- Where the call is to a Predicate function for an inherited predicate.
11611 -- We simply ignore such a call, which could be to either a dynamic or
11612 -- a static predicate. Note that if the parent predicate is dynamic then
11613 -- eventually this type will be marked as dynamic, but you are allowed
11614 -- to specify a static predicate for a subtype which is inheriting a
11615 -- dynamic predicate, so the static predicate validation here ignores
11616 -- the inherited predicate even if it is dynamic.
11618 elsif Nkind (Expr) = N_Function_Call
11619 and then Is_Predicate_Function (Entity (Name (Expr)))
11620 then
11621 return True;
11623 -- That's an exhaustive list of tests, all other cases are not
11624 -- predicate-static, so we return False.
11626 else
11627 return False;
11628 end if;
11629 end Is_Predicate_Static;
11631 ---------------------
11632 -- Kill_Rep_Clause --
11633 ---------------------
11635 procedure Kill_Rep_Clause (N : Node_Id) is
11636 begin
11637 pragma Assert (Ignore_Rep_Clauses);
11639 -- Note: we use Replace rather than Rewrite, because we don't want
11640 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11641 -- rep clause that is being replaced.
11643 Replace (N, Make_Null_Statement (Sloc (N)));
11645 -- The null statement must be marked as not coming from source. This is
11646 -- so that ASIS ignores it, and also the back end does not expect bogus
11647 -- "from source" null statements in weird places (e.g. in declarative
11648 -- regions where such null statements are not allowed).
11650 Set_Comes_From_Source (N, False);
11651 end Kill_Rep_Clause;
11653 ------------------
11654 -- Minimum_Size --
11655 ------------------
11657 function Minimum_Size
11658 (T : Entity_Id;
11659 Biased : Boolean := False) return Nat
11661 Lo : Uint := No_Uint;
11662 Hi : Uint := No_Uint;
11663 LoR : Ureal := No_Ureal;
11664 HiR : Ureal := No_Ureal;
11665 LoSet : Boolean := False;
11666 HiSet : Boolean := False;
11667 B : Uint;
11668 S : Nat;
11669 Ancest : Entity_Id;
11670 R_Typ : constant Entity_Id := Root_Type (T);
11672 begin
11673 -- If bad type, return 0
11675 if T = Any_Type then
11676 return 0;
11678 -- For generic types, just return zero. There cannot be any legitimate
11679 -- need to know such a size, but this routine may be called with a
11680 -- generic type as part of normal processing.
11682 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11683 return 0;
11685 -- Access types (cannot have size smaller than System.Address)
11687 elsif Is_Access_Type (T) then
11688 return System_Address_Size;
11690 -- Floating-point types
11692 elsif Is_Floating_Point_Type (T) then
11693 return UI_To_Int (Esize (R_Typ));
11695 -- Discrete types
11697 elsif Is_Discrete_Type (T) then
11699 -- The following loop is looking for the nearest compile time known
11700 -- bounds following the ancestor subtype chain. The idea is to find
11701 -- the most restrictive known bounds information.
11703 Ancest := T;
11704 loop
11705 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11706 return 0;
11707 end if;
11709 if not LoSet then
11710 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11711 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11712 LoSet := True;
11713 exit when HiSet;
11714 end if;
11715 end if;
11717 if not HiSet then
11718 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11719 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11720 HiSet := True;
11721 exit when LoSet;
11722 end if;
11723 end if;
11725 Ancest := Ancestor_Subtype (Ancest);
11727 if No (Ancest) then
11728 Ancest := Base_Type (T);
11730 if Is_Generic_Type (Ancest) then
11731 return 0;
11732 end if;
11733 end if;
11734 end loop;
11736 -- Fixed-point types. We can't simply use Expr_Value to get the
11737 -- Corresponding_Integer_Value values of the bounds, since these do not
11738 -- get set till the type is frozen, and this routine can be called
11739 -- before the type is frozen. Similarly the test for bounds being static
11740 -- needs to include the case where we have unanalyzed real literals for
11741 -- the same reason.
11743 elsif Is_Fixed_Point_Type (T) then
11745 -- The following loop is looking for the nearest compile time known
11746 -- bounds following the ancestor subtype chain. The idea is to find
11747 -- the most restrictive known bounds information.
11749 Ancest := T;
11750 loop
11751 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11752 return 0;
11753 end if;
11755 -- Note: In the following two tests for LoSet and HiSet, it may
11756 -- seem redundant to test for N_Real_Literal here since normally
11757 -- one would assume that the test for the value being known at
11758 -- compile time includes this case. However, there is a glitch.
11759 -- If the real literal comes from folding a non-static expression,
11760 -- then we don't consider any non- static expression to be known
11761 -- at compile time if we are in configurable run time mode (needed
11762 -- in some cases to give a clearer definition of what is and what
11763 -- is not accepted). So the test is indeed needed. Without it, we
11764 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11766 if not LoSet then
11767 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11768 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11769 then
11770 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11771 LoSet := True;
11772 exit when HiSet;
11773 end if;
11774 end if;
11776 if not HiSet then
11777 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11778 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11779 then
11780 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11781 HiSet := True;
11782 exit when LoSet;
11783 end if;
11784 end if;
11786 Ancest := Ancestor_Subtype (Ancest);
11788 if No (Ancest) then
11789 Ancest := Base_Type (T);
11791 if Is_Generic_Type (Ancest) then
11792 return 0;
11793 end if;
11794 end if;
11795 end loop;
11797 Lo := UR_To_Uint (LoR / Small_Value (T));
11798 Hi := UR_To_Uint (HiR / Small_Value (T));
11800 -- No other types allowed
11802 else
11803 raise Program_Error;
11804 end if;
11806 -- Fall through with Hi and Lo set. Deal with biased case
11808 if (Biased
11809 and then not Is_Fixed_Point_Type (T)
11810 and then not (Is_Enumeration_Type (T)
11811 and then Has_Non_Standard_Rep (T)))
11812 or else Has_Biased_Representation (T)
11813 then
11814 Hi := Hi - Lo;
11815 Lo := Uint_0;
11816 end if;
11818 -- Null range case, size is always zero. We only do this in the discrete
11819 -- type case, since that's the odd case that came up. Probably we should
11820 -- also do this in the fixed-point case, but doing so causes peculiar
11821 -- gigi failures, and it is not worth worrying about this incredibly
11822 -- marginal case (explicit null-range fixed-point type declarations)???
11824 if Lo > Hi and then Is_Discrete_Type (T) then
11825 S := 0;
11827 -- Signed case. Note that we consider types like range 1 .. -1 to be
11828 -- signed for the purpose of computing the size, since the bounds have
11829 -- to be accommodated in the base type.
11831 elsif Lo < 0 or else Hi < 0 then
11832 S := 1;
11833 B := Uint_1;
11835 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11836 -- Note that we accommodate the case where the bounds cross. This
11837 -- can happen either because of the way the bounds are declared
11838 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11840 while Lo < -B
11841 or else Hi < -B
11842 or else Lo >= B
11843 or else Hi >= B
11844 loop
11845 B := Uint_2 ** S;
11846 S := S + 1;
11847 end loop;
11849 -- Unsigned case
11851 else
11852 -- If both bounds are positive, make sure that both are represen-
11853 -- table in the case where the bounds are crossed. This can happen
11854 -- either because of the way the bounds are declared, or because of
11855 -- the algorithm in Freeze_Fixed_Point_Type.
11857 if Lo > Hi then
11858 Hi := Lo;
11859 end if;
11861 -- S = size, (can accommodate 0 .. (2**size - 1))
11863 S := 0;
11864 while Hi >= Uint_2 ** S loop
11865 S := S + 1;
11866 end loop;
11867 end if;
11869 return S;
11870 end Minimum_Size;
11872 ---------------------------
11873 -- New_Stream_Subprogram --
11874 ---------------------------
11876 procedure New_Stream_Subprogram
11877 (N : Node_Id;
11878 Ent : Entity_Id;
11879 Subp : Entity_Id;
11880 Nam : TSS_Name_Type)
11882 Loc : constant Source_Ptr := Sloc (N);
11883 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11884 Subp_Id : Entity_Id;
11885 Subp_Decl : Node_Id;
11886 F : Entity_Id;
11887 Etyp : Entity_Id;
11889 Defer_Declaration : constant Boolean :=
11890 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11891 -- For a tagged type, there is a declaration for each stream attribute
11892 -- at the freeze point, and we must generate only a completion of this
11893 -- declaration. We do the same for private types, because the full view
11894 -- might be tagged. Otherwise we generate a declaration at the point of
11895 -- the attribute definition clause.
11897 function Build_Spec return Node_Id;
11898 -- Used for declaration and renaming declaration, so that this is
11899 -- treated as a renaming_as_body.
11901 ----------------
11902 -- Build_Spec --
11903 ----------------
11905 function Build_Spec return Node_Id is
11906 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11907 Formals : List_Id;
11908 Spec : Node_Id;
11909 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11911 begin
11912 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11914 -- S : access Root_Stream_Type'Class
11916 Formals := New_List (
11917 Make_Parameter_Specification (Loc,
11918 Defining_Identifier =>
11919 Make_Defining_Identifier (Loc, Name_S),
11920 Parameter_Type =>
11921 Make_Access_Definition (Loc,
11922 Subtype_Mark =>
11923 New_Occurrence_Of (
11924 Designated_Type (Etype (F)), Loc))));
11926 if Nam = TSS_Stream_Input then
11927 Spec :=
11928 Make_Function_Specification (Loc,
11929 Defining_Unit_Name => Subp_Id,
11930 Parameter_Specifications => Formals,
11931 Result_Definition => T_Ref);
11932 else
11933 -- V : [out] T
11935 Append_To (Formals,
11936 Make_Parameter_Specification (Loc,
11937 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11938 Out_Present => Out_P,
11939 Parameter_Type => T_Ref));
11941 Spec :=
11942 Make_Procedure_Specification (Loc,
11943 Defining_Unit_Name => Subp_Id,
11944 Parameter_Specifications => Formals);
11945 end if;
11947 return Spec;
11948 end Build_Spec;
11950 -- Start of processing for New_Stream_Subprogram
11952 begin
11953 F := First_Formal (Subp);
11955 if Ekind (Subp) = E_Procedure then
11956 Etyp := Etype (Next_Formal (F));
11957 else
11958 Etyp := Etype (Subp);
11959 end if;
11961 -- Prepare subprogram declaration and insert it as an action on the
11962 -- clause node. The visibility for this entity is used to test for
11963 -- visibility of the attribute definition clause (in the sense of
11964 -- 8.3(23) as amended by AI-195).
11966 if not Defer_Declaration then
11967 Subp_Decl :=
11968 Make_Subprogram_Declaration (Loc,
11969 Specification => Build_Spec);
11971 -- For a tagged type, there is always a visible declaration for each
11972 -- stream TSS (it is a predefined primitive operation), and the
11973 -- completion of this declaration occurs at the freeze point, which is
11974 -- not always visible at places where the attribute definition clause is
11975 -- visible. So, we create a dummy entity here for the purpose of
11976 -- tracking the visibility of the attribute definition clause itself.
11978 else
11979 Subp_Id :=
11980 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11981 Subp_Decl :=
11982 Make_Object_Declaration (Loc,
11983 Defining_Identifier => Subp_Id,
11984 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11985 end if;
11987 Insert_Action (N, Subp_Decl);
11988 Set_Entity (N, Subp_Id);
11990 Subp_Decl :=
11991 Make_Subprogram_Renaming_Declaration (Loc,
11992 Specification => Build_Spec,
11993 Name => New_Occurrence_Of (Subp, Loc));
11995 if Defer_Declaration then
11996 Set_TSS (Base_Type (Ent), Subp_Id);
11997 else
11998 Insert_Action (N, Subp_Decl);
11999 Copy_TSS (Subp_Id, Base_Type (Ent));
12000 end if;
12001 end New_Stream_Subprogram;
12003 ------------------------------------------
12004 -- Push_Scope_And_Install_Discriminants --
12005 ------------------------------------------
12007 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12008 begin
12009 if Has_Discriminants (E) then
12010 Push_Scope (E);
12012 -- Make discriminants visible for type declarations and protected
12013 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12015 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12016 Install_Discriminants (E);
12017 end if;
12018 end if;
12019 end Push_Scope_And_Install_Discriminants;
12021 ------------------------
12022 -- Rep_Item_Too_Early --
12023 ------------------------
12025 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
12026 begin
12027 -- Cannot apply non-operational rep items to generic types
12029 if Is_Operational_Item (N) then
12030 return False;
12032 elsif Is_Type (T)
12033 and then Is_Generic_Type (Root_Type (T))
12034 and then (Nkind (N) /= N_Pragma
12035 or else Get_Pragma_Id (N) /= Pragma_Convention)
12036 then
12037 Error_Msg_N ("representation item not allowed for generic type", N);
12038 return True;
12039 end if;
12041 -- Otherwise check for incomplete type
12043 if Is_Incomplete_Or_Private_Type (T)
12044 and then No (Underlying_Type (T))
12045 and then
12046 (Nkind (N) /= N_Pragma
12047 or else Get_Pragma_Id (N) /= Pragma_Import)
12048 then
12049 Error_Msg_N
12050 ("representation item must be after full type declaration", N);
12051 return True;
12053 -- If the type has incomplete components, a representation clause is
12054 -- illegal but stream attributes and Convention pragmas are correct.
12056 elsif Has_Private_Component (T) then
12057 if Nkind (N) = N_Pragma then
12058 return False;
12060 else
12061 Error_Msg_N
12062 ("representation item must appear after type is fully defined",
12064 return True;
12065 end if;
12066 else
12067 return False;
12068 end if;
12069 end Rep_Item_Too_Early;
12071 -----------------------
12072 -- Rep_Item_Too_Late --
12073 -----------------------
12075 function Rep_Item_Too_Late
12076 (T : Entity_Id;
12077 N : Node_Id;
12078 FOnly : Boolean := False) return Boolean
12080 S : Entity_Id;
12081 Parent_Type : Entity_Id;
12083 procedure No_Type_Rep_Item;
12084 -- Output message indicating that no type-related aspects can be
12085 -- specified due to some property of the parent type.
12087 procedure Too_Late;
12088 -- Output message for an aspect being specified too late
12090 -- Note that neither of the above errors is considered a serious one,
12091 -- since the effect is simply that we ignore the representation clause
12092 -- in these cases.
12093 -- Is this really true? In any case if we make this change we must
12094 -- document the requirement in the spec of Rep_Item_Too_Late that
12095 -- if True is returned, then the rep item must be completely ignored???
12097 ----------------------
12098 -- No_Type_Rep_Item --
12099 ----------------------
12101 procedure No_Type_Rep_Item is
12102 begin
12103 Error_Msg_N ("|type-related representation item not permitted!", N);
12104 end No_Type_Rep_Item;
12106 --------------
12107 -- Too_Late --
12108 --------------
12110 procedure Too_Late is
12111 begin
12112 -- Other compilers seem more relaxed about rep items appearing too
12113 -- late. Since analysis tools typically don't care about rep items
12114 -- anyway, no reason to be too strict about this.
12116 if not Relaxed_RM_Semantics then
12117 Error_Msg_N ("|representation item appears too late!", N);
12118 end if;
12119 end Too_Late;
12121 -- Start of processing for Rep_Item_Too_Late
12123 begin
12124 -- First make sure entity is not frozen (RM 13.1(9))
12126 if Is_Frozen (T)
12128 -- Exclude imported types, which may be frozen if they appear in a
12129 -- representation clause for a local type.
12131 and then not From_Limited_With (T)
12133 -- Exclude generated entities (not coming from source). The common
12134 -- case is when we generate a renaming which prematurely freezes the
12135 -- renamed internal entity, but we still want to be able to set copies
12136 -- of attribute values such as Size/Alignment.
12138 and then Comes_From_Source (T)
12139 then
12140 Too_Late;
12141 S := First_Subtype (T);
12143 if Present (Freeze_Node (S)) then
12144 if not Relaxed_RM_Semantics then
12145 Error_Msg_NE
12146 ("??no more representation items for }", Freeze_Node (S), S);
12147 end if;
12148 end if;
12150 return True;
12152 -- Check for case of untagged derived type whose parent either has
12153 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12154 -- this case we do not output a Too_Late message, since there is no
12155 -- earlier point where the rep item could be placed to make it legal.
12157 elsif Is_Type (T)
12158 and then not FOnly
12159 and then Is_Derived_Type (T)
12160 and then not Is_Tagged_Type (T)
12161 then
12162 Parent_Type := Etype (Base_Type (T));
12164 if Has_Primitive_Operations (Parent_Type) then
12165 No_Type_Rep_Item;
12167 if not Relaxed_RM_Semantics then
12168 Error_Msg_NE
12169 ("\parent type & has primitive operations!", N, Parent_Type);
12170 end if;
12172 return True;
12174 elsif Is_By_Reference_Type (Parent_Type) then
12175 No_Type_Rep_Item;
12177 if not Relaxed_RM_Semantics then
12178 Error_Msg_NE
12179 ("\parent type & is a by reference type!", N, Parent_Type);
12180 end if;
12182 return True;
12183 end if;
12184 end if;
12186 -- No error, but one more warning to consider. The RM (surprisingly)
12187 -- allows this pattern:
12189 -- type S is ...
12190 -- primitive operations for S
12191 -- type R is new S;
12192 -- rep clause for S
12194 -- Meaning that calls on the primitive operations of S for values of
12195 -- type R may require possibly expensive implicit conversion operations.
12196 -- This is not an error, but is worth a warning.
12198 if not Relaxed_RM_Semantics and then Is_Type (T) then
12199 declare
12200 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12202 begin
12203 if Present (DTL)
12204 and then Has_Primitive_Operations (Base_Type (T))
12206 -- For now, do not generate this warning for the case of aspect
12207 -- specification using Ada 2012 syntax, since we get wrong
12208 -- messages we do not understand. The whole business of derived
12209 -- types and rep items seems a bit confused when aspects are
12210 -- used, since the aspects are not evaluated till freeze time.
12212 and then not From_Aspect_Specification (N)
12213 then
12214 Error_Msg_Sloc := Sloc (DTL);
12215 Error_Msg_N
12216 ("representation item for& appears after derived type "
12217 & "declaration#??", N);
12218 Error_Msg_NE
12219 ("\may result in implicit conversions for primitive "
12220 & "operations of&??", N, T);
12221 Error_Msg_NE
12222 ("\to change representations when called with arguments "
12223 & "of type&??", N, DTL);
12224 end if;
12225 end;
12226 end if;
12228 -- No error, link item into head of chain of rep items for the entity,
12229 -- but avoid chaining if we have an overloadable entity, and the pragma
12230 -- is one that can apply to multiple overloaded entities.
12232 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
12233 declare
12234 Pname : constant Name_Id := Pragma_Name (N);
12235 begin
12236 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12237 Name_External, Name_Interface)
12238 then
12239 return False;
12240 end if;
12241 end;
12242 end if;
12244 Record_Rep_Item (T, N);
12245 return False;
12246 end Rep_Item_Too_Late;
12248 -------------------------------------
12249 -- Replace_Type_References_Generic --
12250 -------------------------------------
12252 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12253 TName : constant Name_Id := Chars (T);
12255 function Replace_Node (N : Node_Id) return Traverse_Result;
12256 -- Processes a single node in the traversal procedure below, checking
12257 -- if node N should be replaced, and if so, doing the replacement.
12259 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
12260 -- This instantiation provides the body of Replace_Type_References
12262 ------------------
12263 -- Replace_Node --
12264 ------------------
12266 function Replace_Node (N : Node_Id) return Traverse_Result is
12267 S : Entity_Id;
12268 P : Node_Id;
12270 begin
12271 -- Case of identifier
12273 if Nkind (N) = N_Identifier then
12275 -- If not the type name, check whether it is a reference to
12276 -- some other type, which must be frozen before the predicate
12277 -- function is analyzed, i.e. before the freeze node of the
12278 -- type to which the predicate applies.
12280 if Chars (N) /= TName then
12281 if Present (Current_Entity (N))
12282 and then Is_Type (Current_Entity (N))
12283 then
12284 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12285 end if;
12287 return Skip;
12289 -- Otherwise do the replacement and we are done with this node
12291 else
12292 Replace_Type_Reference (N);
12293 return Skip;
12294 end if;
12296 -- Case of selected component (which is what a qualification
12297 -- looks like in the unanalyzed tree, which is what we have.
12299 elsif Nkind (N) = N_Selected_Component then
12301 -- If selector name is not our type, keeping going (we might
12302 -- still have an occurrence of the type in the prefix).
12304 if Nkind (Selector_Name (N)) /= N_Identifier
12305 or else Chars (Selector_Name (N)) /= TName
12306 then
12307 return OK;
12309 -- Selector name is our type, check qualification
12311 else
12312 -- Loop through scopes and prefixes, doing comparison
12314 S := Current_Scope;
12315 P := Prefix (N);
12316 loop
12317 -- Continue if no more scopes or scope with no name
12319 if No (S) or else Nkind (S) not in N_Has_Chars then
12320 return OK;
12321 end if;
12323 -- Do replace if prefix is an identifier matching the
12324 -- scope that we are currently looking at.
12326 if Nkind (P) = N_Identifier
12327 and then Chars (P) = Chars (S)
12328 then
12329 Replace_Type_Reference (N);
12330 return Skip;
12331 end if;
12333 -- Go check scope above us if prefix is itself of the
12334 -- form of a selected component, whose selector matches
12335 -- the scope we are currently looking at.
12337 if Nkind (P) = N_Selected_Component
12338 and then Nkind (Selector_Name (P)) = N_Identifier
12339 and then Chars (Selector_Name (P)) = Chars (S)
12340 then
12341 S := Scope (S);
12342 P := Prefix (P);
12344 -- For anything else, we don't have a match, so keep on
12345 -- going, there are still some weird cases where we may
12346 -- still have a replacement within the prefix.
12348 else
12349 return OK;
12350 end if;
12351 end loop;
12352 end if;
12354 -- Continue for any other node kind
12356 else
12357 return OK;
12358 end if;
12359 end Replace_Node;
12361 begin
12362 Replace_Type_Refs (N);
12363 end Replace_Type_References_Generic;
12365 -------------------------
12366 -- Same_Representation --
12367 -------------------------
12369 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12370 T1 : constant Entity_Id := Underlying_Type (Typ1);
12371 T2 : constant Entity_Id := Underlying_Type (Typ2);
12373 begin
12374 -- A quick check, if base types are the same, then we definitely have
12375 -- the same representation, because the subtype specific representation
12376 -- attributes (Size and Alignment) do not affect representation from
12377 -- the point of view of this test.
12379 if Base_Type (T1) = Base_Type (T2) then
12380 return True;
12382 elsif Is_Private_Type (Base_Type (T2))
12383 and then Base_Type (T1) = Full_View (Base_Type (T2))
12384 then
12385 return True;
12386 end if;
12388 -- Tagged types never have differing representations
12390 if Is_Tagged_Type (T1) then
12391 return True;
12392 end if;
12394 -- Representations are definitely different if conventions differ
12396 if Convention (T1) /= Convention (T2) then
12397 return False;
12398 end if;
12400 -- Representations are different if component alignments or scalar
12401 -- storage orders differ.
12403 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
12404 and then
12405 (Is_Record_Type (T2) or else Is_Array_Type (T2))
12406 and then
12407 (Component_Alignment (T1) /= Component_Alignment (T2)
12408 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
12409 then
12410 return False;
12411 end if;
12413 -- For arrays, the only real issue is component size. If we know the
12414 -- component size for both arrays, and it is the same, then that's
12415 -- good enough to know we don't have a change of representation.
12417 if Is_Array_Type (T1) then
12418 if Known_Component_Size (T1)
12419 and then Known_Component_Size (T2)
12420 and then Component_Size (T1) = Component_Size (T2)
12421 then
12422 if VM_Target = No_VM then
12423 return True;
12425 -- In VM targets the representation of arrays with aliased
12426 -- components differs from arrays with non-aliased components
12428 else
12429 return Has_Aliased_Components (Base_Type (T1))
12431 Has_Aliased_Components (Base_Type (T2));
12432 end if;
12433 end if;
12434 end if;
12436 -- Types definitely have same representation if neither has non-standard
12437 -- representation since default representations are always consistent.
12438 -- If only one has non-standard representation, and the other does not,
12439 -- then we consider that they do not have the same representation. They
12440 -- might, but there is no way of telling early enough.
12442 if Has_Non_Standard_Rep (T1) then
12443 if not Has_Non_Standard_Rep (T2) then
12444 return False;
12445 end if;
12446 else
12447 return not Has_Non_Standard_Rep (T2);
12448 end if;
12450 -- Here the two types both have non-standard representation, and we need
12451 -- to determine if they have the same non-standard representation.
12453 -- For arrays, we simply need to test if the component sizes are the
12454 -- same. Pragma Pack is reflected in modified component sizes, so this
12455 -- check also deals with pragma Pack.
12457 if Is_Array_Type (T1) then
12458 return Component_Size (T1) = Component_Size (T2);
12460 -- Tagged types always have the same representation, because it is not
12461 -- possible to specify different representations for common fields.
12463 elsif Is_Tagged_Type (T1) then
12464 return True;
12466 -- Case of record types
12468 elsif Is_Record_Type (T1) then
12470 -- Packed status must conform
12472 if Is_Packed (T1) /= Is_Packed (T2) then
12473 return False;
12475 -- Otherwise we must check components. Typ2 maybe a constrained
12476 -- subtype with fewer components, so we compare the components
12477 -- of the base types.
12479 else
12480 Record_Case : declare
12481 CD1, CD2 : Entity_Id;
12483 function Same_Rep return Boolean;
12484 -- CD1 and CD2 are either components or discriminants. This
12485 -- function tests whether they have the same representation.
12487 --------------
12488 -- Same_Rep --
12489 --------------
12491 function Same_Rep return Boolean is
12492 begin
12493 if No (Component_Clause (CD1)) then
12494 return No (Component_Clause (CD2));
12495 else
12496 -- Note: at this point, component clauses have been
12497 -- normalized to the default bit order, so that the
12498 -- comparison of Component_Bit_Offsets is meaningful.
12500 return
12501 Present (Component_Clause (CD2))
12502 and then
12503 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12504 and then
12505 Esize (CD1) = Esize (CD2);
12506 end if;
12507 end Same_Rep;
12509 -- Start of processing for Record_Case
12511 begin
12512 if Has_Discriminants (T1) then
12514 -- The number of discriminants may be different if the
12515 -- derived type has fewer (constrained by values). The
12516 -- invisible discriminants retain the representation of
12517 -- the original, so the discrepancy does not per se
12518 -- indicate a different representation.
12520 CD1 := First_Discriminant (T1);
12521 CD2 := First_Discriminant (T2);
12522 while Present (CD1) and then Present (CD2) loop
12523 if not Same_Rep then
12524 return False;
12525 else
12526 Next_Discriminant (CD1);
12527 Next_Discriminant (CD2);
12528 end if;
12529 end loop;
12530 end if;
12532 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12533 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
12534 while Present (CD1) loop
12535 if not Same_Rep then
12536 return False;
12537 else
12538 Next_Component (CD1);
12539 Next_Component (CD2);
12540 end if;
12541 end loop;
12543 return True;
12544 end Record_Case;
12545 end if;
12547 -- For enumeration types, we must check each literal to see if the
12548 -- representation is the same. Note that we do not permit enumeration
12549 -- representation clauses for Character and Wide_Character, so these
12550 -- cases were already dealt with.
12552 elsif Is_Enumeration_Type (T1) then
12553 Enumeration_Case : declare
12554 L1, L2 : Entity_Id;
12556 begin
12557 L1 := First_Literal (T1);
12558 L2 := First_Literal (T2);
12559 while Present (L1) loop
12560 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12561 return False;
12562 else
12563 Next_Literal (L1);
12564 Next_Literal (L2);
12565 end if;
12566 end loop;
12568 return True;
12569 end Enumeration_Case;
12571 -- Any other types have the same representation for these purposes
12573 else
12574 return True;
12575 end if;
12576 end Same_Representation;
12578 --------------------------------
12579 -- Resolve_Iterable_Operation --
12580 --------------------------------
12582 procedure Resolve_Iterable_Operation
12583 (N : Node_Id;
12584 Cursor : Entity_Id;
12585 Typ : Entity_Id;
12586 Nam : Name_Id)
12588 Ent : Entity_Id;
12589 F1 : Entity_Id;
12590 F2 : Entity_Id;
12592 begin
12593 if not Is_Overloaded (N) then
12594 if not Is_Entity_Name (N)
12595 or else Ekind (Entity (N)) /= E_Function
12596 or else Scope (Entity (N)) /= Scope (Typ)
12597 or else No (First_Formal (Entity (N)))
12598 or else Etype (First_Formal (Entity (N))) /= Typ
12599 then
12600 Error_Msg_N ("iterable primitive must be local function name "
12601 & "whose first formal is an iterable type", N);
12602 return;
12603 end if;
12605 Ent := Entity (N);
12606 F1 := First_Formal (Ent);
12607 if Nam = Name_First then
12609 -- First (Container) => Cursor
12611 if Etype (Ent) /= Cursor then
12612 Error_Msg_N ("primitive for First must yield a curosr", N);
12613 end if;
12615 elsif Nam = Name_Next then
12617 -- Next (Container, Cursor) => Cursor
12619 F2 := Next_Formal (F1);
12621 if Etype (F2) /= Cursor
12622 or else Etype (Ent) /= Cursor
12623 or else Present (Next_Formal (F2))
12624 then
12625 Error_Msg_N ("no match for Next iterable primitive", N);
12626 end if;
12628 elsif Nam = Name_Has_Element then
12630 -- Has_Element (Container, Cursor) => Boolean
12632 F2 := Next_Formal (F1);
12633 if Etype (F2) /= Cursor
12634 or else Etype (Ent) /= Standard_Boolean
12635 or else Present (Next_Formal (F2))
12636 then
12637 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12638 end if;
12640 elsif Nam = Name_Element then
12641 F2 := Next_Formal (F1);
12643 if No (F2)
12644 or else Etype (F2) /= Cursor
12645 or else Present (Next_Formal (F2))
12646 then
12647 Error_Msg_N ("no match for Element iterable primitive", N);
12648 end if;
12649 null;
12651 else
12652 raise Program_Error;
12653 end if;
12655 else
12656 -- Overloaded case: find subprogram with proper signature.
12657 -- Caller will report error if no match is found.
12659 declare
12660 I : Interp_Index;
12661 It : Interp;
12663 begin
12664 Get_First_Interp (N, I, It);
12665 while Present (It.Typ) loop
12666 if Ekind (It.Nam) = E_Function
12667 and then Scope (It.Nam) = Scope (Typ)
12668 and then Etype (First_Formal (It.Nam)) = Typ
12669 then
12670 F1 := First_Formal (It.Nam);
12672 if Nam = Name_First then
12673 if Etype (It.Nam) = Cursor
12674 and then No (Next_Formal (F1))
12675 then
12676 Set_Entity (N, It.Nam);
12677 exit;
12678 end if;
12680 elsif Nam = Name_Next then
12681 F2 := Next_Formal (F1);
12683 if Present (F2)
12684 and then No (Next_Formal (F2))
12685 and then Etype (F2) = Cursor
12686 and then Etype (It.Nam) = Cursor
12687 then
12688 Set_Entity (N, It.Nam);
12689 exit;
12690 end if;
12692 elsif Nam = Name_Has_Element then
12693 F2 := Next_Formal (F1);
12695 if Present (F2)
12696 and then No (Next_Formal (F2))
12697 and then Etype (F2) = Cursor
12698 and then Etype (It.Nam) = Standard_Boolean
12699 then
12700 Set_Entity (N, It.Nam);
12701 F2 := Next_Formal (F1);
12702 exit;
12703 end if;
12705 elsif Nam = Name_Element then
12706 F2 := Next_Formal (F1);
12708 if Present (F2)
12709 and then No (Next_Formal (F2))
12710 and then Etype (F2) = Cursor
12711 then
12712 Set_Entity (N, It.Nam);
12713 exit;
12714 end if;
12715 end if;
12716 end if;
12718 Get_Next_Interp (I, It);
12719 end loop;
12720 end;
12721 end if;
12722 end Resolve_Iterable_Operation;
12724 ----------------
12725 -- Set_Biased --
12726 ----------------
12728 procedure Set_Biased
12729 (E : Entity_Id;
12730 N : Node_Id;
12731 Msg : String;
12732 Biased : Boolean := True)
12734 begin
12735 if Biased then
12736 Set_Has_Biased_Representation (E);
12738 if Warn_On_Biased_Representation then
12739 Error_Msg_NE
12740 ("?B?" & Msg & " forces biased representation for&", N, E);
12741 end if;
12742 end if;
12743 end Set_Biased;
12745 --------------------
12746 -- Set_Enum_Esize --
12747 --------------------
12749 procedure Set_Enum_Esize (T : Entity_Id) is
12750 Lo : Uint;
12751 Hi : Uint;
12752 Sz : Nat;
12754 begin
12755 Init_Alignment (T);
12757 -- Find the minimum standard size (8,16,32,64) that fits
12759 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12760 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12762 if Lo < 0 then
12763 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12764 Sz := Standard_Character_Size; -- May be > 8 on some targets
12766 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12767 Sz := 16;
12769 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12770 Sz := 32;
12772 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12773 Sz := 64;
12774 end if;
12776 else
12777 if Hi < Uint_2**08 then
12778 Sz := Standard_Character_Size; -- May be > 8 on some targets
12780 elsif Hi < Uint_2**16 then
12781 Sz := 16;
12783 elsif Hi < Uint_2**32 then
12784 Sz := 32;
12786 else pragma Assert (Hi < Uint_2**63);
12787 Sz := 64;
12788 end if;
12789 end if;
12791 -- That minimum is the proper size unless we have a foreign convention
12792 -- and the size required is 32 or less, in which case we bump the size
12793 -- up to 32. This is required for C and C++ and seems reasonable for
12794 -- all other foreign conventions.
12796 if Has_Foreign_Convention (T)
12797 and then Esize (T) < Standard_Integer_Size
12799 -- Don't do this if Short_Enums on target
12801 and then not Target_Short_Enums
12802 then
12803 Init_Esize (T, Standard_Integer_Size);
12804 else
12805 Init_Esize (T, Sz);
12806 end if;
12807 end Set_Enum_Esize;
12809 -----------------------------
12810 -- Uninstall_Discriminants --
12811 -----------------------------
12813 procedure Uninstall_Discriminants (E : Entity_Id) is
12814 Disc : Entity_Id;
12815 Prev : Entity_Id;
12816 Outer : Entity_Id;
12818 begin
12819 -- Discriminants have been made visible for type declarations and
12820 -- protected type declarations, not for subtype declarations.
12822 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12823 Disc := First_Discriminant (E);
12824 while Present (Disc) loop
12825 if Disc /= Current_Entity (Disc) then
12826 Prev := Current_Entity (Disc);
12827 while Present (Prev)
12828 and then Present (Homonym (Prev))
12829 and then Homonym (Prev) /= Disc
12830 loop
12831 Prev := Homonym (Prev);
12832 end loop;
12833 else
12834 Prev := Empty;
12835 end if;
12837 Set_Is_Immediately_Visible (Disc, False);
12839 Outer := Homonym (Disc);
12840 while Present (Outer) and then Scope (Outer) = E loop
12841 Outer := Homonym (Outer);
12842 end loop;
12844 -- Reset homonym link of other entities, but do not modify link
12845 -- between entities in current scope, so that the back-end can
12846 -- have a proper count of local overloadings.
12848 if No (Prev) then
12849 Set_Name_Entity_Id (Chars (Disc), Outer);
12851 elsif Scope (Prev) /= Scope (Disc) then
12852 Set_Homonym (Prev, Outer);
12853 end if;
12855 Next_Discriminant (Disc);
12856 end loop;
12857 end if;
12858 end Uninstall_Discriminants;
12860 -------------------------------------------
12861 -- Uninstall_Discriminants_And_Pop_Scope --
12862 -------------------------------------------
12864 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12865 begin
12866 if Has_Discriminants (E) then
12867 Uninstall_Discriminants (E);
12868 Pop_Scope;
12869 end if;
12870 end Uninstall_Discriminants_And_Pop_Scope;
12872 ------------------------------
12873 -- Validate_Address_Clauses --
12874 ------------------------------
12876 procedure Validate_Address_Clauses is
12877 begin
12878 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12879 declare
12880 ACCR : Address_Clause_Check_Record
12881 renames Address_Clause_Checks.Table (J);
12883 Expr : Node_Id;
12885 X_Alignment : Uint;
12886 Y_Alignment : Uint;
12888 X_Size : Uint;
12889 Y_Size : Uint;
12891 begin
12892 -- Skip processing of this entry if warning already posted
12894 if not Address_Warning_Posted (ACCR.N) then
12895 Expr := Original_Node (Expression (ACCR.N));
12897 -- Get alignments
12899 X_Alignment := Alignment (ACCR.X);
12900 Y_Alignment := Alignment (ACCR.Y);
12902 -- Similarly obtain sizes
12904 X_Size := Esize (ACCR.X);
12905 Y_Size := Esize (ACCR.Y);
12907 -- Check for large object overlaying smaller one
12909 if Y_Size > Uint_0
12910 and then X_Size > Uint_0
12911 and then X_Size > Y_Size
12912 then
12913 Error_Msg_NE
12914 ("??& overlays smaller object", ACCR.N, ACCR.X);
12915 Error_Msg_N
12916 ("\??program execution may be erroneous", ACCR.N);
12917 Error_Msg_Uint_1 := X_Size;
12918 Error_Msg_NE
12919 ("\??size of & is ^", ACCR.N, ACCR.X);
12920 Error_Msg_Uint_1 := Y_Size;
12921 Error_Msg_NE
12922 ("\??size of & is ^", ACCR.N, ACCR.Y);
12924 -- Check for inadequate alignment, both of the base object
12925 -- and of the offset, if any. We only do this check if the
12926 -- run-time Alignment_Check is active. No point in warning
12927 -- if this check has been suppressed (or is suppressed by
12928 -- default in the non-strict alignment machine case).
12930 -- Note: we do not check the alignment if we gave a size
12931 -- warning, since it would likely be redundant.
12933 elsif not Alignment_Checks_Suppressed (ACCR.Y)
12934 and then Y_Alignment /= Uint_0
12935 and then (Y_Alignment < X_Alignment
12936 or else (ACCR.Off
12937 and then
12938 Nkind (Expr) = N_Attribute_Reference
12939 and then
12940 Attribute_Name (Expr) = Name_Address
12941 and then
12942 Has_Compatible_Alignment
12943 (ACCR.X, Prefix (Expr))
12944 /= Known_Compatible))
12945 then
12946 Error_Msg_NE
12947 ("??specified address for& may be inconsistent "
12948 & "with alignment", ACCR.N, ACCR.X);
12949 Error_Msg_N
12950 ("\??program execution may be erroneous (RM 13.3(27))",
12951 ACCR.N);
12952 Error_Msg_Uint_1 := X_Alignment;
12953 Error_Msg_NE
12954 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12955 Error_Msg_Uint_1 := Y_Alignment;
12956 Error_Msg_NE
12957 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12958 if Y_Alignment >= X_Alignment then
12959 Error_Msg_N
12960 ("\??but offset is not multiple of alignment", ACCR.N);
12961 end if;
12962 end if;
12963 end if;
12964 end;
12965 end loop;
12966 end Validate_Address_Clauses;
12968 ---------------------------
12969 -- Validate_Independence --
12970 ---------------------------
12972 procedure Validate_Independence is
12973 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12974 N : Node_Id;
12975 E : Entity_Id;
12976 IC : Boolean;
12977 Comp : Entity_Id;
12978 Addr : Node_Id;
12979 P : Node_Id;
12981 procedure Check_Array_Type (Atyp : Entity_Id);
12982 -- Checks if the array type Atyp has independent components, and
12983 -- if not, outputs an appropriate set of error messages.
12985 procedure No_Independence;
12986 -- Output message that independence cannot be guaranteed
12988 function OK_Component (C : Entity_Id) return Boolean;
12989 -- Checks one component to see if it is independently accessible, and
12990 -- if so yields True, otherwise yields False if independent access
12991 -- cannot be guaranteed. This is a conservative routine, it only
12992 -- returns True if it knows for sure, it returns False if it knows
12993 -- there is a problem, or it cannot be sure there is no problem.
12995 procedure Reason_Bad_Component (C : Entity_Id);
12996 -- Outputs continuation message if a reason can be determined for
12997 -- the component C being bad.
12999 ----------------------
13000 -- Check_Array_Type --
13001 ----------------------
13003 procedure Check_Array_Type (Atyp : Entity_Id) is
13004 Ctyp : constant Entity_Id := Component_Type (Atyp);
13006 begin
13007 -- OK if no alignment clause, no pack, and no component size
13009 if not Has_Component_Size_Clause (Atyp)
13010 and then not Has_Alignment_Clause (Atyp)
13011 and then not Is_Packed (Atyp)
13012 then
13013 return;
13014 end if;
13016 -- Case of component size is greater than or equal to 64 and the
13017 -- alignment of the array is at least as large as the alignment
13018 -- of the component. We are definitely OK in this situation.
13020 if Known_Component_Size (Atyp)
13021 and then Component_Size (Atyp) >= 64
13022 and then Known_Alignment (Atyp)
13023 and then Known_Alignment (Ctyp)
13024 and then Alignment (Atyp) >= Alignment (Ctyp)
13025 then
13026 return;
13027 end if;
13029 -- Check actual component size
13031 if not Known_Component_Size (Atyp)
13032 or else not (Addressable (Component_Size (Atyp))
13033 and then Component_Size (Atyp) < 64)
13034 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13035 then
13036 No_Independence;
13038 -- Bad component size, check reason
13040 if Has_Component_Size_Clause (Atyp) then
13041 P := Get_Attribute_Definition_Clause
13042 (Atyp, Attribute_Component_Size);
13044 if Present (P) then
13045 Error_Msg_Sloc := Sloc (P);
13046 Error_Msg_N ("\because of Component_Size clause#", N);
13047 return;
13048 end if;
13049 end if;
13051 if Is_Packed (Atyp) then
13052 P := Get_Rep_Pragma (Atyp, Name_Pack);
13054 if Present (P) then
13055 Error_Msg_Sloc := Sloc (P);
13056 Error_Msg_N ("\because of pragma Pack#", N);
13057 return;
13058 end if;
13059 end if;
13061 -- No reason found, just return
13063 return;
13064 end if;
13066 -- Array type is OK independence-wise
13068 return;
13069 end Check_Array_Type;
13071 ---------------------
13072 -- No_Independence --
13073 ---------------------
13075 procedure No_Independence is
13076 begin
13077 if Pragma_Name (N) = Name_Independent then
13078 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
13079 else
13080 Error_Msg_NE
13081 ("independent components cannot be guaranteed for&", N, E);
13082 end if;
13083 end No_Independence;
13085 ------------------
13086 -- OK_Component --
13087 ------------------
13089 function OK_Component (C : Entity_Id) return Boolean is
13090 Rec : constant Entity_Id := Scope (C);
13091 Ctyp : constant Entity_Id := Etype (C);
13093 begin
13094 -- OK if no component clause, no Pack, and no alignment clause
13096 if No (Component_Clause (C))
13097 and then not Is_Packed (Rec)
13098 and then not Has_Alignment_Clause (Rec)
13099 then
13100 return True;
13101 end if;
13103 -- Here we look at the actual component layout. A component is
13104 -- addressable if its size is a multiple of the Esize of the
13105 -- component type, and its starting position in the record has
13106 -- appropriate alignment, and the record itself has appropriate
13107 -- alignment to guarantee the component alignment.
13109 -- Make sure sizes are static, always assume the worst for any
13110 -- cases where we cannot check static values.
13112 if not (Known_Static_Esize (C)
13113 and then
13114 Known_Static_Esize (Ctyp))
13115 then
13116 return False;
13117 end if;
13119 -- Size of component must be addressable or greater than 64 bits
13120 -- and a multiple of bytes.
13122 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
13123 return False;
13124 end if;
13126 -- Check size is proper multiple
13128 if Esize (C) mod Esize (Ctyp) /= 0 then
13129 return False;
13130 end if;
13132 -- Check alignment of component is OK
13134 if not Known_Component_Bit_Offset (C)
13135 or else Component_Bit_Offset (C) < Uint_0
13136 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13137 then
13138 return False;
13139 end if;
13141 -- Check alignment of record type is OK
13143 if not Known_Alignment (Rec)
13144 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13145 then
13146 return False;
13147 end if;
13149 -- All tests passed, component is addressable
13151 return True;
13152 end OK_Component;
13154 --------------------------
13155 -- Reason_Bad_Component --
13156 --------------------------
13158 procedure Reason_Bad_Component (C : Entity_Id) is
13159 Rec : constant Entity_Id := Scope (C);
13160 Ctyp : constant Entity_Id := Etype (C);
13162 begin
13163 -- If component clause present assume that's the problem
13165 if Present (Component_Clause (C)) then
13166 Error_Msg_Sloc := Sloc (Component_Clause (C));
13167 Error_Msg_N ("\because of Component_Clause#", N);
13168 return;
13169 end if;
13171 -- If pragma Pack clause present, assume that's the problem
13173 if Is_Packed (Rec) then
13174 P := Get_Rep_Pragma (Rec, Name_Pack);
13176 if Present (P) then
13177 Error_Msg_Sloc := Sloc (P);
13178 Error_Msg_N ("\because of pragma Pack#", N);
13179 return;
13180 end if;
13181 end if;
13183 -- See if record has bad alignment clause
13185 if Has_Alignment_Clause (Rec)
13186 and then Known_Alignment (Rec)
13187 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13188 then
13189 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13191 if Present (P) then
13192 Error_Msg_Sloc := Sloc (P);
13193 Error_Msg_N ("\because of Alignment clause#", N);
13194 end if;
13195 end if;
13197 -- Couldn't find a reason, so return without a message
13199 return;
13200 end Reason_Bad_Component;
13202 -- Start of processing for Validate_Independence
13204 begin
13205 for J in Independence_Checks.First .. Independence_Checks.Last loop
13206 N := Independence_Checks.Table (J).N;
13207 E := Independence_Checks.Table (J).E;
13208 IC := Pragma_Name (N) = Name_Independent_Components;
13210 -- Deal with component case
13212 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13213 if not OK_Component (E) then
13214 No_Independence;
13215 Reason_Bad_Component (E);
13216 goto Continue;
13217 end if;
13218 end if;
13220 -- Deal with record with Independent_Components
13222 if IC and then Is_Record_Type (E) then
13223 Comp := First_Component_Or_Discriminant (E);
13224 while Present (Comp) loop
13225 if not OK_Component (Comp) then
13226 No_Independence;
13227 Reason_Bad_Component (Comp);
13228 goto Continue;
13229 end if;
13231 Next_Component_Or_Discriminant (Comp);
13232 end loop;
13233 end if;
13235 -- Deal with address clause case
13237 if Is_Object (E) then
13238 Addr := Address_Clause (E);
13240 if Present (Addr) then
13241 No_Independence;
13242 Error_Msg_Sloc := Sloc (Addr);
13243 Error_Msg_N ("\because of Address clause#", N);
13244 goto Continue;
13245 end if;
13246 end if;
13248 -- Deal with independent components for array type
13250 if IC and then Is_Array_Type (E) then
13251 Check_Array_Type (E);
13252 end if;
13254 -- Deal with independent components for array object
13256 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13257 Check_Array_Type (Etype (E));
13258 end if;
13260 <<Continue>> null;
13261 end loop;
13262 end Validate_Independence;
13264 ------------------------------
13265 -- Validate_Iterable_Aspect --
13266 ------------------------------
13268 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
13269 Assoc : Node_Id;
13270 Expr : Node_Id;
13272 Prim : Node_Id;
13273 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
13275 First_Id : Entity_Id;
13276 Next_Id : Entity_Id;
13277 Has_Element_Id : Entity_Id;
13278 Element_Id : Entity_Id;
13280 begin
13281 -- If previous error aspect is unusable
13283 if Cursor = Any_Type then
13284 return;
13285 end if;
13287 First_Id := Empty;
13288 Next_Id := Empty;
13289 Has_Element_Id := Empty;
13290 Element_Id := Empty;
13292 -- Each expression must resolve to a function with the proper signature
13294 Assoc := First (Component_Associations (Expression (ASN)));
13295 while Present (Assoc) loop
13296 Expr := Expression (Assoc);
13297 Analyze (Expr);
13299 Prim := First (Choices (Assoc));
13301 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
13302 Error_Msg_N ("illegal name in association", Prim);
13304 elsif Chars (Prim) = Name_First then
13305 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
13306 First_Id := Entity (Expr);
13308 elsif Chars (Prim) = Name_Next then
13309 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
13310 Next_Id := Entity (Expr);
13312 elsif Chars (Prim) = Name_Has_Element then
13313 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
13314 Has_Element_Id := Entity (Expr);
13316 elsif Chars (Prim) = Name_Element then
13317 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
13318 Element_Id := Entity (Expr);
13320 else
13321 Error_Msg_N ("invalid name for iterable function", Prim);
13322 end if;
13324 Next (Assoc);
13325 end loop;
13327 if No (First_Id) then
13328 Error_Msg_N ("match for First primitive not found", ASN);
13330 elsif No (Next_Id) then
13331 Error_Msg_N ("match for Next primitive not found", ASN);
13333 elsif No (Has_Element_Id) then
13334 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13336 elsif No (Element_Id) then
13337 null; -- Optional.
13338 end if;
13339 end Validate_Iterable_Aspect;
13341 -----------------------------------
13342 -- Validate_Unchecked_Conversion --
13343 -----------------------------------
13345 procedure Validate_Unchecked_Conversion
13346 (N : Node_Id;
13347 Act_Unit : Entity_Id)
13349 Source : Entity_Id;
13350 Target : Entity_Id;
13351 Vnode : Node_Id;
13353 begin
13354 -- Obtain source and target types. Note that we call Ancestor_Subtype
13355 -- here because the processing for generic instantiation always makes
13356 -- subtypes, and we want the original frozen actual types.
13358 -- If we are dealing with private types, then do the check on their
13359 -- fully declared counterparts if the full declarations have been
13360 -- encountered (they don't have to be visible, but they must exist).
13362 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13364 if Is_Private_Type (Source)
13365 and then Present (Underlying_Type (Source))
13366 then
13367 Source := Underlying_Type (Source);
13368 end if;
13370 Target := Ancestor_Subtype (Etype (Act_Unit));
13372 -- If either type is generic, the instantiation happens within a generic
13373 -- unit, and there is nothing to check. The proper check will happen
13374 -- when the enclosing generic is instantiated.
13376 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13377 return;
13378 end if;
13380 if Is_Private_Type (Target)
13381 and then Present (Underlying_Type (Target))
13382 then
13383 Target := Underlying_Type (Target);
13384 end if;
13386 -- Source may be unconstrained array, but not target
13388 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
13389 Error_Msg_N
13390 ("unchecked conversion to unconstrained array not allowed", N);
13391 return;
13392 end if;
13394 -- Warn if conversion between two different convention pointers
13396 if Is_Access_Type (Target)
13397 and then Is_Access_Type (Source)
13398 and then Convention (Target) /= Convention (Source)
13399 and then Warn_On_Unchecked_Conversion
13400 then
13401 -- Give warnings for subprogram pointers only on most targets
13403 if Is_Access_Subprogram_Type (Target)
13404 or else Is_Access_Subprogram_Type (Source)
13405 then
13406 Error_Msg_N
13407 ("?z?conversion between pointers with different conventions!",
13409 end if;
13410 end if;
13412 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13413 -- warning when compiling GNAT-related sources.
13415 if Warn_On_Unchecked_Conversion
13416 and then not In_Predefined_Unit (N)
13417 and then RTU_Loaded (Ada_Calendar)
13418 and then (Chars (Source) = Name_Time
13419 or else
13420 Chars (Target) = Name_Time)
13421 then
13422 -- If Ada.Calendar is loaded and the name of one of the operands is
13423 -- Time, there is a good chance that this is Ada.Calendar.Time.
13425 declare
13426 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
13427 begin
13428 pragma Assert (Present (Calendar_Time));
13430 if Source = Calendar_Time or else Target = Calendar_Time then
13431 Error_Msg_N
13432 ("?z?representation of 'Time values may change between "
13433 & "'G'N'A'T versions", N);
13434 end if;
13435 end;
13436 end if;
13438 -- Make entry in unchecked conversion table for later processing by
13439 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13440 -- (using values set by the back-end where possible). This is only done
13441 -- if the appropriate warning is active.
13443 if Warn_On_Unchecked_Conversion then
13444 Unchecked_Conversions.Append
13445 (New_Val => UC_Entry'(Eloc => Sloc (N),
13446 Source => Source,
13447 Target => Target,
13448 Act_Unit => Act_Unit));
13450 -- If both sizes are known statically now, then back end annotation
13451 -- is not required to do a proper check but if either size is not
13452 -- known statically, then we need the annotation.
13454 if Known_Static_RM_Size (Source)
13455 and then
13456 Known_Static_RM_Size (Target)
13457 then
13458 null;
13459 else
13460 Back_Annotate_Rep_Info := True;
13461 end if;
13462 end if;
13464 -- If unchecked conversion to access type, and access type is declared
13465 -- in the same unit as the unchecked conversion, then set the flag
13466 -- No_Strict_Aliasing (no strict aliasing is implicit here)
13468 if Is_Access_Type (Target) and then
13469 In_Same_Source_Unit (Target, N)
13470 then
13471 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13472 end if;
13474 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13475 -- the back end needs to perform special validation checks.
13477 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13478 -- have full expansion and the back end is called ???
13480 Vnode :=
13481 Make_Validate_Unchecked_Conversion (Sloc (N));
13482 Set_Source_Type (Vnode, Source);
13483 Set_Target_Type (Vnode, Target);
13485 -- If the unchecked conversion node is in a list, just insert before it.
13486 -- If not we have some strange case, not worth bothering about.
13488 if Is_List_Member (N) then
13489 Insert_After (N, Vnode);
13490 end if;
13491 end Validate_Unchecked_Conversion;
13493 ------------------------------------
13494 -- Validate_Unchecked_Conversions --
13495 ------------------------------------
13497 procedure Validate_Unchecked_Conversions is
13498 begin
13499 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13500 declare
13501 T : UC_Entry renames Unchecked_Conversions.Table (N);
13503 Eloc : constant Source_Ptr := T.Eloc;
13504 Source : constant Entity_Id := T.Source;
13505 Target : constant Entity_Id := T.Target;
13506 Act_Unit : constant Entity_Id := T.Act_Unit;
13508 Source_Siz : Uint;
13509 Target_Siz : Uint;
13511 begin
13512 -- Skip if function marked as warnings off
13514 if Warnings_Off (Act_Unit) then
13515 goto Continue;
13516 end if;
13518 -- This validation check, which warns if we have unequal sizes for
13519 -- unchecked conversion, and thus potentially implementation
13520 -- dependent semantics, is one of the few occasions on which we
13521 -- use the official RM size instead of Esize. See description in
13522 -- Einfo "Handling of Type'Size Values" for details.
13524 if Serious_Errors_Detected = 0
13525 and then Known_Static_RM_Size (Source)
13526 and then Known_Static_RM_Size (Target)
13528 -- Don't do the check if warnings off for either type, note the
13529 -- deliberate use of OR here instead of OR ELSE to get the flag
13530 -- Warnings_Off_Used set for both types if appropriate.
13532 and then not (Has_Warnings_Off (Source)
13534 Has_Warnings_Off (Target))
13535 then
13536 Source_Siz := RM_Size (Source);
13537 Target_Siz := RM_Size (Target);
13539 if Source_Siz /= Target_Siz then
13540 Error_Msg
13541 ("?z?types for unchecked conversion have different sizes!",
13542 Eloc);
13544 if All_Errors_Mode then
13545 Error_Msg_Name_1 := Chars (Source);
13546 Error_Msg_Uint_1 := Source_Siz;
13547 Error_Msg_Name_2 := Chars (Target);
13548 Error_Msg_Uint_2 := Target_Siz;
13549 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
13551 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13553 if Is_Discrete_Type (Source)
13554 and then
13555 Is_Discrete_Type (Target)
13556 then
13557 if Source_Siz > Target_Siz then
13558 Error_Msg
13559 ("\?z?^ high order bits of source will "
13560 & "be ignored!", Eloc);
13562 elsif Is_Unsigned_Type (Source) then
13563 Error_Msg
13564 ("\?z?source will be extended with ^ high order "
13565 & "zero bits!", Eloc);
13567 else
13568 Error_Msg
13569 ("\?z?source will be extended with ^ high order "
13570 & "sign bits!", Eloc);
13571 end if;
13573 elsif Source_Siz < Target_Siz then
13574 if Is_Discrete_Type (Target) then
13575 if Bytes_Big_Endian then
13576 Error_Msg
13577 ("\?z?target value will include ^ undefined "
13578 & "low order bits!", Eloc);
13579 else
13580 Error_Msg
13581 ("\?z?target value will include ^ undefined "
13582 & "high order bits!", Eloc);
13583 end if;
13585 else
13586 Error_Msg
13587 ("\?z?^ trailing bits of target value will be "
13588 & "undefined!", Eloc);
13589 end if;
13591 else pragma Assert (Source_Siz > Target_Siz);
13592 if Is_Discrete_Type (Source) then
13593 if Bytes_Big_Endian then
13594 Error_Msg
13595 ("\?z?^ low order bits of source will be "
13596 & "ignored!", Eloc);
13597 else
13598 Error_Msg
13599 ("\?z?^ high order bits of source will be "
13600 & "ignored!", Eloc);
13601 end if;
13603 else
13604 Error_Msg
13605 ("\?z?^ trailing bits of source will be "
13606 & "ignored!", Eloc);
13607 end if;
13608 end if;
13609 end if;
13610 end if;
13611 end if;
13613 -- If both types are access types, we need to check the alignment.
13614 -- If the alignment of both is specified, we can do it here.
13616 if Serious_Errors_Detected = 0
13617 and then Is_Access_Type (Source)
13618 and then Is_Access_Type (Target)
13619 and then Target_Strict_Alignment
13620 and then Present (Designated_Type (Source))
13621 and then Present (Designated_Type (Target))
13622 then
13623 declare
13624 D_Source : constant Entity_Id := Designated_Type (Source);
13625 D_Target : constant Entity_Id := Designated_Type (Target);
13627 begin
13628 if Known_Alignment (D_Source)
13629 and then
13630 Known_Alignment (D_Target)
13631 then
13632 declare
13633 Source_Align : constant Uint := Alignment (D_Source);
13634 Target_Align : constant Uint := Alignment (D_Target);
13636 begin
13637 if Source_Align < Target_Align
13638 and then not Is_Tagged_Type (D_Source)
13640 -- Suppress warning if warnings suppressed on either
13641 -- type or either designated type. Note the use of
13642 -- OR here instead of OR ELSE. That is intentional,
13643 -- we would like to set flag Warnings_Off_Used in
13644 -- all types for which warnings are suppressed.
13646 and then not (Has_Warnings_Off (D_Source)
13648 Has_Warnings_Off (D_Target)
13650 Has_Warnings_Off (Source)
13652 Has_Warnings_Off (Target))
13653 then
13654 Error_Msg_Uint_1 := Target_Align;
13655 Error_Msg_Uint_2 := Source_Align;
13656 Error_Msg_Node_1 := D_Target;
13657 Error_Msg_Node_2 := D_Source;
13658 Error_Msg
13659 ("?z?alignment of & (^) is stricter than "
13660 & "alignment of & (^)!", Eloc);
13661 Error_Msg
13662 ("\?z?resulting access value may have invalid "
13663 & "alignment!", Eloc);
13664 end if;
13665 end;
13666 end if;
13667 end;
13668 end if;
13669 end;
13671 <<Continue>>
13672 null;
13673 end loop;
13674 end Validate_Unchecked_Conversions;
13676 end Sem_Ch13;