2016-04-27 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch13.adb
blobd42b7cad79e3f9e71fe17989dad037630cc7885a
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-2016, 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 function Build_Export_Import_Pragma
105 (Asp : Node_Id;
106 Id : Entity_Id) return Node_Id;
107 -- Create the corresponding pragma for aspect Export or Import denoted by
108 -- Asp. Id is the related entity subject to the aspect. Return Empty when
109 -- the expression of aspect Asp evaluates to False or is erroneous.
111 function Build_Predicate_Function_Declaration
112 (Typ : Entity_Id) return Node_Id;
113 -- Build the declaration for a predicate function. The declaration is built
114 -- at the end of the declarative part containing the type definition, which
115 -- may be before the freeze point of the type. The predicate expression is
116 -- pre-analyzed at this point, to catch visibility errors.
118 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
119 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
120 -- then either there are pragma Predicate entries on the rep chain for the
121 -- type (note that Predicate aspects are converted to pragma Predicate), or
122 -- there are inherited aspects from a parent type, or ancestor subtypes.
123 -- This procedure builds body for the Predicate function that tests these
124 -- predicates. N is the freeze node for the type. The spec of the function
125 -- is inserted before the freeze node, and the body of the function is
126 -- inserted after the freeze node. If the predicate expression has a least
127 -- one Raise_Expression, then this procedure also builds the M version of
128 -- the predicate function for use in membership tests.
130 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
131 -- Called if both Storage_Pool and Storage_Size attribute definition
132 -- clauses (SP and SS) are present for entity Ent. Issue error message.
134 procedure Freeze_Entity_Checks (N : Node_Id);
135 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
136 -- to generate appropriate semantic checks that are delayed until this
137 -- point (they had to be delayed this long for cases of delayed aspects,
138 -- e.g. analysis of statically predicated subtypes in choices, for which
139 -- we have to be sure the subtypes in question are frozen before checking).
141 function Get_Alignment_Value (Expr : Node_Id) return Uint;
142 -- Given the expression for an alignment value, returns the corresponding
143 -- Uint value. If the value is inappropriate, then error messages are
144 -- posted as required, and a value of No_Uint is returned.
146 procedure Get_Interfacing_Aspects
147 (Iface_Asp : Node_Id;
148 Conv_Asp : out Node_Id;
149 EN_Asp : out Node_Id;
150 Expo_Asp : out Node_Id;
151 Imp_Asp : out Node_Id;
152 LN_Asp : out Node_Id;
153 Do_Checks : Boolean := False);
154 -- Given a single interfacing aspect Iface_Asp, retrieve other interfacing
155 -- aspects that apply to the same related entity. The aspects considered by
156 -- this routine are as follows:
158 -- Conv_Asp - aspect Convention
159 -- EN_Asp - aspect External_Name
160 -- Expo_Asp - aspect Export
161 -- Imp_Asp - aspect Import
162 -- LN_Asp - aspect Link_Name
164 -- When flag Do_Checks is set, this routine will flag duplicate uses of
165 -- aspects.
167 function Is_Operational_Item (N : Node_Id) return Boolean;
168 -- A specification for a stream attribute is allowed before the full type
169 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
170 -- that do not specify a representation characteristic are operational
171 -- attributes.
173 function Is_Predicate_Static
174 (Expr : Node_Id;
175 Nam : Name_Id) return Boolean;
176 -- Given predicate expression Expr, tests if Expr is predicate-static in
177 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
178 -- name in the predicate expression have been replaced by references to
179 -- an identifier whose Chars field is Nam. This name is unique, so any
180 -- identifier with Chars matching Nam must be a reference to the type.
181 -- Returns True if the expression is predicate-static and False otherwise,
182 -- but is not in the business of setting flags or issuing error messages.
184 -- Only scalar types can have static predicates, so False is always
185 -- returned for non-scalar types.
187 -- Note: the RM seems to suggest that string types can also have static
188 -- predicates. But that really makes lttle sense as very few useful
189 -- predicates can be constructed for strings. Remember that:
191 -- "ABC" < "DEF"
193 -- is not a static expression. So even though the clearly faulty RM wording
194 -- allows the following:
196 -- subtype S is String with Static_Predicate => S < "DEF"
198 -- We can't allow this, otherwise we have predicate-static applying to a
199 -- larger class than static expressions, which was never intended.
201 procedure New_Stream_Subprogram
202 (N : Node_Id;
203 Ent : Entity_Id;
204 Subp : Entity_Id;
205 Nam : TSS_Name_Type);
206 -- Create a subprogram renaming of a given stream attribute to the
207 -- designated subprogram and then in the tagged case, provide this as a
208 -- primitive operation, or in the untagged case make an appropriate TSS
209 -- entry. This is more properly an expansion activity than just semantics,
210 -- but the presence of user-defined stream functions for limited types
211 -- is a legality check, which is why this takes place here rather than in
212 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
213 -- function to be generated.
215 -- To avoid elaboration anomalies with freeze nodes, for untagged types
216 -- we generate both a subprogram declaration and a subprogram renaming
217 -- declaration, so that the attribute specification is handled as a
218 -- renaming_as_body. For tagged types, the specification is one of the
219 -- primitive specs.
221 procedure Resolve_Iterable_Operation
222 (N : Node_Id;
223 Cursor : Entity_Id;
224 Typ : Entity_Id;
225 Nam : Name_Id);
226 -- If the name of a primitive operation for an Iterable aspect is
227 -- overloaded, resolve according to required signature.
229 procedure Set_Biased
230 (E : Entity_Id;
231 N : Node_Id;
232 Msg : String;
233 Biased : Boolean := True);
234 -- If Biased is True, sets Has_Biased_Representation flag for E, and
235 -- outputs a warning message at node N if Warn_On_Biased_Representation is
236 -- is True. This warning inserts the string Msg to describe the construct
237 -- causing biasing.
239 ----------------------------------------------
240 -- Table for Validate_Unchecked_Conversions --
241 ----------------------------------------------
243 -- The following table collects unchecked conversions for validation.
244 -- Entries are made by Validate_Unchecked_Conversion and then the call
245 -- to Validate_Unchecked_Conversions does the actual error checking and
246 -- posting of warnings. The reason for this delayed processing is to take
247 -- advantage of back-annotations of size and alignment values performed by
248 -- the back end.
250 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
251 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
252 -- already have modified all Sloc values if the -gnatD option is set.
254 type UC_Entry is record
255 Eloc : Source_Ptr; -- node used for posting warnings
256 Source : Entity_Id; -- source type for unchecked conversion
257 Target : Entity_Id; -- target type for unchecked conversion
258 Act_Unit : Entity_Id; -- actual function instantiated
259 end record;
261 package Unchecked_Conversions is new Table.Table (
262 Table_Component_Type => UC_Entry,
263 Table_Index_Type => Int,
264 Table_Low_Bound => 1,
265 Table_Initial => 50,
266 Table_Increment => 200,
267 Table_Name => "Unchecked_Conversions");
269 ----------------------------------------
270 -- Table for Validate_Address_Clauses --
271 ----------------------------------------
273 -- If an address clause has the form
275 -- for X'Address use Expr
277 -- where Expr is of the form Y'Address or recursively is a reference to a
278 -- constant of either of these forms, and X and Y are entities of objects,
279 -- then if Y has a smaller alignment than X, that merits a warning about
280 -- possible bad alignment. The following table collects address clauses of
281 -- this kind. We put these in a table so that they can be checked after the
282 -- back end has completed annotation of the alignments of objects, since we
283 -- can catch more cases that way.
285 type Address_Clause_Check_Record is record
286 N : Node_Id;
287 -- The address clause
289 X : Entity_Id;
290 -- The entity of the object overlaying Y
292 Y : Entity_Id;
293 -- The entity of the object being overlaid
295 Off : Boolean;
296 -- Whether the address is offset within Y
297 end record;
299 package Address_Clause_Checks is new Table.Table (
300 Table_Component_Type => Address_Clause_Check_Record,
301 Table_Index_Type => Int,
302 Table_Low_Bound => 1,
303 Table_Initial => 20,
304 Table_Increment => 200,
305 Table_Name => "Address_Clause_Checks");
307 -----------------------------------------
308 -- Adjust_Record_For_Reverse_Bit_Order --
309 -----------------------------------------
311 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
312 Comp : Node_Id;
313 CC : Node_Id;
315 begin
316 -- Processing depends on version of Ada
318 -- For Ada 95, we just renumber bits within a storage unit. We do the
319 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
320 -- Ada 83, and are free to add this extension.
322 if Ada_Version < Ada_2005 then
323 Comp := First_Component_Or_Discriminant (R);
324 while Present (Comp) loop
325 CC := Component_Clause (Comp);
327 -- If component clause is present, then deal with the non-default
328 -- bit order case for Ada 95 mode.
330 -- We only do this processing for the base type, and in fact that
331 -- is important, since otherwise if there are record subtypes, we
332 -- could reverse the bits once for each subtype, which is wrong.
334 if Present (CC) and then Ekind (R) = E_Record_Type then
335 declare
336 CFB : constant Uint := Component_Bit_Offset (Comp);
337 CSZ : constant Uint := Esize (Comp);
338 CLC : constant Node_Id := Component_Clause (Comp);
339 Pos : constant Node_Id := Position (CLC);
340 FB : constant Node_Id := First_Bit (CLC);
342 Storage_Unit_Offset : constant Uint :=
343 CFB / System_Storage_Unit;
345 Start_Bit : constant Uint :=
346 CFB mod System_Storage_Unit;
348 begin
349 -- Cases where field goes over storage unit boundary
351 if Start_Bit + CSZ > System_Storage_Unit then
353 -- Allow multi-byte field but generate warning
355 if Start_Bit mod System_Storage_Unit = 0
356 and then CSZ mod System_Storage_Unit = 0
357 then
358 Error_Msg_N
359 ("info: multi-byte field specified with "
360 & "non-standard Bit_Order?V?", CLC);
362 if Bytes_Big_Endian then
363 Error_Msg_N
364 ("\bytes are not reversed "
365 & "(component is big-endian)?V?", CLC);
366 else
367 Error_Msg_N
368 ("\bytes are not reversed "
369 & "(component is little-endian)?V?", CLC);
370 end if;
372 -- Do not allow non-contiguous field
374 else
375 Error_Msg_N
376 ("attempt to specify non-contiguous field "
377 & "not permitted", CLC);
378 Error_Msg_N
379 ("\caused by non-standard Bit_Order "
380 & "specified", CLC);
381 Error_Msg_N
382 ("\consider possibility of using "
383 & "Ada 2005 mode here", CLC);
384 end if;
386 -- Case where field fits in one storage unit
388 else
389 -- Give warning if suspicious component clause
391 if Intval (FB) >= System_Storage_Unit
392 and then Warn_On_Reverse_Bit_Order
393 then
394 Error_Msg_N
395 ("info: Bit_Order clause does not affect " &
396 "byte ordering?V?", Pos);
397 Error_Msg_Uint_1 :=
398 Intval (Pos) + Intval (FB) /
399 System_Storage_Unit;
400 Error_Msg_N
401 ("info: position normalized to ^ before bit " &
402 "order interpreted?V?", Pos);
403 end if;
405 -- Here is where we fix up the Component_Bit_Offset value
406 -- to account for the reverse bit order. Some examples of
407 -- what needs to be done are:
409 -- First_Bit .. Last_Bit Component_Bit_Offset
410 -- old new old new
412 -- 0 .. 0 7 .. 7 0 7
413 -- 0 .. 1 6 .. 7 0 6
414 -- 0 .. 2 5 .. 7 0 5
415 -- 0 .. 7 0 .. 7 0 4
417 -- 1 .. 1 6 .. 6 1 6
418 -- 1 .. 4 3 .. 6 1 3
419 -- 4 .. 7 0 .. 3 4 0
421 -- The rule is that the first bit is is obtained by
422 -- subtracting the old ending bit from storage_unit - 1.
424 Set_Component_Bit_Offset
425 (Comp,
426 (Storage_Unit_Offset * System_Storage_Unit) +
427 (System_Storage_Unit - 1) -
428 (Start_Bit + CSZ - 1));
430 Set_Normalized_First_Bit
431 (Comp,
432 Component_Bit_Offset (Comp) mod
433 System_Storage_Unit);
434 end if;
435 end;
436 end if;
438 Next_Component_Or_Discriminant (Comp);
439 end loop;
441 -- For Ada 2005, we do machine scalar processing, as fully described In
442 -- AI-133. This involves gathering all components which start at the
443 -- same byte offset and processing them together. Same approach is still
444 -- valid in later versions including Ada 2012.
446 else
447 declare
448 Max_Machine_Scalar_Size : constant Uint :=
449 UI_From_Int
450 (Standard_Long_Long_Integer_Size);
451 -- We use this as the maximum machine scalar size
453 Num_CC : Natural;
454 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
456 begin
457 -- This first loop through components does two things. First it
458 -- deals with the case of components with component clauses whose
459 -- length is greater than the maximum machine scalar size (either
460 -- accepting them or rejecting as needed). Second, it counts the
461 -- number of components with component clauses whose length does
462 -- not exceed this maximum for later processing.
464 Num_CC := 0;
465 Comp := First_Component_Or_Discriminant (R);
466 while Present (Comp) loop
467 CC := Component_Clause (Comp);
469 if Present (CC) then
470 declare
471 Fbit : constant Uint := Static_Integer (First_Bit (CC));
472 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
474 begin
475 -- Case of component with last bit >= max machine scalar
477 if Lbit >= Max_Machine_Scalar_Size then
479 -- This is allowed only if first bit is zero, and
480 -- last bit + 1 is a multiple of storage unit size.
482 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
484 -- This is the case to give a warning if enabled
486 if Warn_On_Reverse_Bit_Order then
487 Error_Msg_N
488 ("info: multi-byte field specified with "
489 & "non-standard Bit_Order?V?", CC);
491 if Bytes_Big_Endian then
492 Error_Msg_N
493 ("\bytes are not reversed "
494 & "(component is big-endian)?V?", CC);
495 else
496 Error_Msg_N
497 ("\bytes are not reversed "
498 & "(component is little-endian)?V?", CC);
499 end if;
500 end if;
502 -- Give error message for RM 13.5.1(10) violation
504 else
505 Error_Msg_FE
506 ("machine scalar rules not followed for&",
507 First_Bit (CC), Comp);
509 Error_Msg_Uint_1 := Lbit + 1;
510 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
511 Error_Msg_F
512 ("\last bit + 1 (^) exceeds maximum machine "
513 & "scalar size (^)",
514 First_Bit (CC));
516 if (Lbit + 1) mod SSU /= 0 then
517 Error_Msg_Uint_1 := SSU;
518 Error_Msg_F
519 ("\and is not a multiple of Storage_Unit (^) "
520 & "(RM 13.5.1(10))",
521 First_Bit (CC));
523 else
524 Error_Msg_Uint_1 := Fbit;
525 Error_Msg_F
526 ("\and first bit (^) is non-zero "
527 & "(RM 13.4.1(10))",
528 First_Bit (CC));
529 end if;
530 end if;
532 -- OK case of machine scalar related component clause,
533 -- For now, just count them.
535 else
536 Num_CC := Num_CC + 1;
537 end if;
538 end;
539 end if;
541 Next_Component_Or_Discriminant (Comp);
542 end loop;
544 -- We need to sort the component clauses on the basis of the
545 -- Position values in the clause, so we can group clauses with
546 -- the same Position together to determine the relevant machine
547 -- scalar size.
549 Sort_CC : declare
550 Comps : array (0 .. Num_CC) of Entity_Id;
551 -- Array to collect component and discriminant entities. The
552 -- data starts at index 1, the 0'th entry is for the sort
553 -- routine.
555 function CP_Lt (Op1, Op2 : Natural) return Boolean;
556 -- Compare routine for Sort
558 procedure CP_Move (From : Natural; To : Natural);
559 -- Move routine for Sort
561 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
563 Start : Natural;
564 Stop : Natural;
565 -- Start and stop positions in the component list of the set of
566 -- components with the same starting position (that constitute
567 -- components in a single machine scalar).
569 MaxL : Uint;
570 -- Maximum last bit value of any component in this set
572 MSS : Uint;
573 -- Corresponding machine scalar size
575 -----------
576 -- CP_Lt --
577 -----------
579 function CP_Lt (Op1, Op2 : Natural) return Boolean is
580 begin
581 return Position (Component_Clause (Comps (Op1))) <
582 Position (Component_Clause (Comps (Op2)));
583 end CP_Lt;
585 -------------
586 -- CP_Move --
587 -------------
589 procedure CP_Move (From : Natural; To : Natural) is
590 begin
591 Comps (To) := Comps (From);
592 end CP_Move;
594 -- Start of processing for Sort_CC
596 begin
597 -- Collect the machine scalar relevant component clauses
599 Num_CC := 0;
600 Comp := First_Component_Or_Discriminant (R);
601 while Present (Comp) loop
602 declare
603 CC : constant Node_Id := Component_Clause (Comp);
605 begin
606 -- Collect only component clauses whose last bit is less
607 -- than machine scalar size. Any component clause whose
608 -- last bit exceeds this value does not take part in
609 -- machine scalar layout considerations. The test for
610 -- Error_Posted makes sure we exclude component clauses
611 -- for which we already posted an error.
613 if Present (CC)
614 and then not Error_Posted (Last_Bit (CC))
615 and then Static_Integer (Last_Bit (CC)) <
616 Max_Machine_Scalar_Size
617 then
618 Num_CC := Num_CC + 1;
619 Comps (Num_CC) := Comp;
620 end if;
621 end;
623 Next_Component_Or_Discriminant (Comp);
624 end loop;
626 -- Sort by ascending position number
628 Sorting.Sort (Num_CC);
630 -- We now have all the components whose size does not exceed
631 -- the max machine scalar value, sorted by starting position.
632 -- In this loop we gather groups of clauses starting at the
633 -- same position, to process them in accordance with AI-133.
635 Stop := 0;
636 while Stop < Num_CC loop
637 Start := Stop + 1;
638 Stop := Start;
639 MaxL :=
640 Static_Integer
641 (Last_Bit (Component_Clause (Comps (Start))));
642 while Stop < Num_CC loop
643 if Static_Integer
644 (Position (Component_Clause (Comps (Stop + 1)))) =
645 Static_Integer
646 (Position (Component_Clause (Comps (Stop))))
647 then
648 Stop := Stop + 1;
649 MaxL :=
650 UI_Max
651 (MaxL,
652 Static_Integer
653 (Last_Bit
654 (Component_Clause (Comps (Stop)))));
655 else
656 exit;
657 end if;
658 end loop;
660 -- Now we have a group of component clauses from Start to
661 -- Stop whose positions are identical, and MaxL is the
662 -- maximum last bit value of any of these components.
664 -- We need to determine the corresponding machine scalar
665 -- size. This loop assumes that machine scalar sizes are
666 -- even, and that each possible machine scalar has twice
667 -- as many bits as the next smaller one.
669 MSS := Max_Machine_Scalar_Size;
670 while MSS mod 2 = 0
671 and then (MSS / 2) >= SSU
672 and then (MSS / 2) > MaxL
673 loop
674 MSS := MSS / 2;
675 end loop;
677 -- Here is where we fix up the Component_Bit_Offset value
678 -- to account for the reverse bit order. Some examples of
679 -- what needs to be done for the case of a machine scalar
680 -- size of 8 are:
682 -- First_Bit .. Last_Bit Component_Bit_Offset
683 -- old new old new
685 -- 0 .. 0 7 .. 7 0 7
686 -- 0 .. 1 6 .. 7 0 6
687 -- 0 .. 2 5 .. 7 0 5
688 -- 0 .. 7 0 .. 7 0 4
690 -- 1 .. 1 6 .. 6 1 6
691 -- 1 .. 4 3 .. 6 1 3
692 -- 4 .. 7 0 .. 3 4 0
694 -- The rule is that the first bit is obtained by subtracting
695 -- the old ending bit from machine scalar size - 1.
697 for C in Start .. Stop loop
698 declare
699 Comp : constant Entity_Id := Comps (C);
700 CC : constant Node_Id := Component_Clause (Comp);
702 LB : constant Uint := Static_Integer (Last_Bit (CC));
703 NFB : constant Uint := MSS - Uint_1 - LB;
704 NLB : constant Uint := NFB + Esize (Comp) - 1;
705 Pos : constant Uint := Static_Integer (Position (CC));
707 begin
708 if Warn_On_Reverse_Bit_Order then
709 Error_Msg_Uint_1 := MSS;
710 Error_Msg_N
711 ("info: reverse bit order in machine " &
712 "scalar of length^?V?", First_Bit (CC));
713 Error_Msg_Uint_1 := NFB;
714 Error_Msg_Uint_2 := NLB;
716 if Bytes_Big_Endian then
717 Error_Msg_NE
718 ("\big-endian range for component "
719 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
720 else
721 Error_Msg_NE
722 ("\little-endian range for component"
723 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
724 end if;
725 end if;
727 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
728 Set_Normalized_First_Bit (Comp, NFB mod SSU);
729 end;
730 end loop;
731 end loop;
732 end Sort_CC;
733 end;
734 end if;
735 end Adjust_Record_For_Reverse_Bit_Order;
737 -------------------------------------
738 -- Alignment_Check_For_Size_Change --
739 -------------------------------------
741 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
742 begin
743 -- If the alignment is known, and not set by a rep clause, and is
744 -- inconsistent with the size being set, then reset it to unknown,
745 -- we assume in this case that the size overrides the inherited
746 -- alignment, and that the alignment must be recomputed.
748 if Known_Alignment (Typ)
749 and then not Has_Alignment_Clause (Typ)
750 and then Size mod (Alignment (Typ) * SSU) /= 0
751 then
752 Init_Alignment (Typ);
753 end if;
754 end Alignment_Check_For_Size_Change;
756 -------------------------------------
757 -- Analyze_Aspects_At_Freeze_Point --
758 -------------------------------------
760 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
761 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
762 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
763 -- the aspect specification node ASN.
765 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
766 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
767 -- a derived type can inherit aspects from its parent which have been
768 -- specified at the time of the derivation using an aspect, as in:
770 -- type A is range 1 .. 10
771 -- with Size => Not_Defined_Yet;
772 -- ..
773 -- type B is new A;
774 -- ..
775 -- Not_Defined_Yet : constant := 64;
777 -- In this example, the Size of A is considered to be specified prior
778 -- to the derivation, and thus inherited, even though the value is not
779 -- known at the time of derivation. To deal with this, we use two entity
780 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
781 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
782 -- the derived type (B here). If this flag is set when the derived type
783 -- is frozen, then this procedure is called to ensure proper inheritance
784 -- of all delayed aspects from the parent type. The derived type is E,
785 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
786 -- aspect specification node in the Rep_Item chain for the parent type.
788 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
789 -- Given an aspect specification node ASN whose expression is an
790 -- optional Boolean, this routines creates the corresponding pragma
791 -- at the freezing point.
793 ----------------------------------
794 -- Analyze_Aspect_Default_Value --
795 ----------------------------------
797 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
798 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
799 Ent : constant Entity_Id := Entity (ASN);
800 Expr : constant Node_Id := Expression (ASN);
801 Id : constant Node_Id := Identifier (ASN);
803 begin
804 Error_Msg_Name_1 := Chars (Id);
806 if not Is_Type (Ent) then
807 Error_Msg_N ("aspect% can only apply to a type", Id);
808 return;
810 elsif not Is_First_Subtype (Ent) then
811 Error_Msg_N ("aspect% cannot apply to subtype", Id);
812 return;
814 elsif A_Id = Aspect_Default_Value
815 and then not Is_Scalar_Type (Ent)
816 then
817 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
818 return;
820 elsif A_Id = Aspect_Default_Component_Value then
821 if not Is_Array_Type (Ent) then
822 Error_Msg_N ("aspect% can only be applied to array type", Id);
823 return;
825 elsif not Is_Scalar_Type (Component_Type (Ent)) then
826 Error_Msg_N ("aspect% requires scalar components", Id);
827 return;
828 end if;
829 end if;
831 Set_Has_Default_Aspect (Base_Type (Ent));
833 if Is_Scalar_Type (Ent) then
834 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
835 else
836 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
837 end if;
838 end Analyze_Aspect_Default_Value;
840 ---------------------------------
841 -- Inherit_Delayed_Rep_Aspects --
842 ---------------------------------
844 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
845 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
846 P : constant Entity_Id := Entity (ASN);
847 -- Entithy for parent type
849 N : Node_Id;
850 -- Item from Rep_Item chain
852 A : Aspect_Id;
854 begin
855 -- Loop through delayed aspects for the parent type
857 N := ASN;
858 while Present (N) loop
859 if Nkind (N) = N_Aspect_Specification then
860 exit when Entity (N) /= P;
862 if Is_Delayed_Aspect (N) then
863 A := Get_Aspect_Id (Chars (Identifier (N)));
865 -- Process delayed rep aspect. For Boolean attributes it is
866 -- not possible to cancel an attribute once set (the attempt
867 -- to use an aspect with xxx => False is an error) for a
868 -- derived type. So for those cases, we do not have to check
869 -- if a clause has been given for the derived type, since it
870 -- is harmless to set it again if it is already set.
872 case A is
874 -- Alignment
876 when Aspect_Alignment =>
877 if not Has_Alignment_Clause (E) then
878 Set_Alignment (E, Alignment (P));
879 end if;
881 -- Atomic
883 when Aspect_Atomic =>
884 if Is_Atomic (P) then
885 Set_Is_Atomic (E);
886 end if;
888 -- Atomic_Components
890 when Aspect_Atomic_Components =>
891 if Has_Atomic_Components (P) then
892 Set_Has_Atomic_Components (Base_Type (E));
893 end if;
895 -- Bit_Order
897 when Aspect_Bit_Order =>
898 if Is_Record_Type (E)
899 and then No (Get_Attribute_Definition_Clause
900 (E, Attribute_Bit_Order))
901 and then Reverse_Bit_Order (P)
902 then
903 Set_Reverse_Bit_Order (Base_Type (E));
904 end if;
906 -- Component_Size
908 when Aspect_Component_Size =>
909 if Is_Array_Type (E)
910 and then not Has_Component_Size_Clause (E)
911 then
912 Set_Component_Size
913 (Base_Type (E), Component_Size (P));
914 end if;
916 -- Machine_Radix
918 when Aspect_Machine_Radix =>
919 if Is_Decimal_Fixed_Point_Type (E)
920 and then not Has_Machine_Radix_Clause (E)
921 then
922 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
923 end if;
925 -- Object_Size (also Size which also sets Object_Size)
927 when Aspect_Object_Size | Aspect_Size =>
928 if not Has_Size_Clause (E)
929 and then
930 No (Get_Attribute_Definition_Clause
931 (E, Attribute_Object_Size))
932 then
933 Set_Esize (E, Esize (P));
934 end if;
936 -- Pack
938 when Aspect_Pack =>
939 if not Is_Packed (E) then
940 Set_Is_Packed (Base_Type (E));
942 if Is_Bit_Packed_Array (P) then
943 Set_Is_Bit_Packed_Array (Base_Type (E));
944 Set_Packed_Array_Impl_Type
945 (E, Packed_Array_Impl_Type (P));
946 end if;
947 end if;
949 -- Scalar_Storage_Order
951 when Aspect_Scalar_Storage_Order =>
952 if (Is_Record_Type (E) or else Is_Array_Type (E))
953 and then No (Get_Attribute_Definition_Clause
954 (E, Attribute_Scalar_Storage_Order))
955 and then Reverse_Storage_Order (P)
956 then
957 Set_Reverse_Storage_Order (Base_Type (E));
959 -- Clear default SSO indications, since the aspect
960 -- overrides the default.
962 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
963 Set_SSO_Set_High_By_Default (Base_Type (E), False);
964 end if;
966 -- Small
968 when Aspect_Small =>
969 if Is_Fixed_Point_Type (E)
970 and then not Has_Small_Clause (E)
971 then
972 Set_Small_Value (E, Small_Value (P));
973 end if;
975 -- Storage_Size
977 when Aspect_Storage_Size =>
978 if (Is_Access_Type (E) or else Is_Task_Type (E))
979 and then not Has_Storage_Size_Clause (E)
980 then
981 Set_Storage_Size_Variable
982 (Base_Type (E), Storage_Size_Variable (P));
983 end if;
985 -- Value_Size
987 when Aspect_Value_Size =>
989 -- Value_Size is never inherited, it is either set by
990 -- default, or it is explicitly set for the derived
991 -- type. So nothing to do here.
993 null;
995 -- Volatile
997 when Aspect_Volatile =>
998 if Is_Volatile (P) then
999 Set_Is_Volatile (E);
1000 end if;
1002 -- Volatile_Full_Access
1004 when Aspect_Volatile_Full_Access =>
1005 if Is_Volatile_Full_Access (P) then
1006 Set_Is_Volatile_Full_Access (E);
1007 end if;
1009 -- Volatile_Components
1011 when Aspect_Volatile_Components =>
1012 if Has_Volatile_Components (P) then
1013 Set_Has_Volatile_Components (Base_Type (E));
1014 end if;
1016 -- That should be all the Rep Aspects
1018 when others =>
1019 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1020 null;
1022 end case;
1023 end if;
1024 end if;
1026 N := Next_Rep_Item (N);
1027 end loop;
1028 end Inherit_Delayed_Rep_Aspects;
1030 -------------------------------------
1031 -- Make_Pragma_From_Boolean_Aspect --
1032 -------------------------------------
1034 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1035 Ident : constant Node_Id := Identifier (ASN);
1036 A_Name : constant Name_Id := Chars (Ident);
1037 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1038 Ent : constant Entity_Id := Entity (ASN);
1039 Expr : constant Node_Id := Expression (ASN);
1040 Loc : constant Source_Ptr := Sloc (ASN);
1042 procedure Check_False_Aspect_For_Derived_Type;
1043 -- This procedure checks for the case of a false aspect for a derived
1044 -- type, which improperly tries to cancel an aspect inherited from
1045 -- the parent.
1047 -----------------------------------------
1048 -- Check_False_Aspect_For_Derived_Type --
1049 -----------------------------------------
1051 procedure Check_False_Aspect_For_Derived_Type is
1052 Par : Node_Id;
1054 begin
1055 -- We are only checking derived types
1057 if not Is_Derived_Type (E) then
1058 return;
1059 end if;
1061 Par := Nearest_Ancestor (E);
1063 case A_Id is
1064 when Aspect_Atomic | Aspect_Shared =>
1065 if not Is_Atomic (Par) then
1066 return;
1067 end if;
1069 when Aspect_Atomic_Components =>
1070 if not Has_Atomic_Components (Par) then
1071 return;
1072 end if;
1074 when Aspect_Discard_Names =>
1075 if not Discard_Names (Par) then
1076 return;
1077 end if;
1079 when Aspect_Pack =>
1080 if not Is_Packed (Par) then
1081 return;
1082 end if;
1084 when Aspect_Unchecked_Union =>
1085 if not Is_Unchecked_Union (Par) then
1086 return;
1087 end if;
1089 when Aspect_Volatile =>
1090 if not Is_Volatile (Par) then
1091 return;
1092 end if;
1094 when Aspect_Volatile_Components =>
1095 if not Has_Volatile_Components (Par) then
1096 return;
1097 end if;
1099 when Aspect_Volatile_Full_Access =>
1100 if not Is_Volatile_Full_Access (Par) then
1101 return;
1102 end if;
1104 when others =>
1105 return;
1106 end case;
1108 -- Fall through means we are canceling an inherited aspect
1110 Error_Msg_Name_1 := A_Name;
1111 Error_Msg_NE
1112 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1113 end Check_False_Aspect_For_Derived_Type;
1115 -- Local variables
1117 Prag : Node_Id;
1119 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1121 begin
1122 -- Note that we know Expr is present, because for a missing Expr
1123 -- argument, we knew it was True and did not need to delay the
1124 -- evaluation to the freeze point.
1126 if Is_False (Static_Boolean (Expr)) then
1127 Check_False_Aspect_For_Derived_Type;
1129 else
1130 Prag :=
1131 Make_Pragma (Loc,
1132 Pragma_Identifier =>
1133 Make_Identifier (Sloc (Ident), Chars (Ident)),
1134 Pragma_Argument_Associations => New_List (
1135 Make_Pragma_Argument_Association (Sloc (Ident),
1136 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
1138 Set_From_Aspect_Specification (Prag, True);
1139 Set_Corresponding_Aspect (Prag, ASN);
1140 Set_Aspect_Rep_Item (ASN, Prag);
1141 Set_Is_Delayed_Aspect (Prag);
1142 Set_Parent (Prag, ASN);
1143 end if;
1144 end Make_Pragma_From_Boolean_Aspect;
1146 -- Local variables
1148 A_Id : Aspect_Id;
1149 ASN : Node_Id;
1150 Ritem : Node_Id;
1152 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1154 begin
1155 -- Must be visible in current scope
1157 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1158 return;
1159 end if;
1161 -- Look for aspect specification entries for this entity
1163 ASN := First_Rep_Item (E);
1164 while Present (ASN) loop
1165 if Nkind (ASN) = N_Aspect_Specification then
1166 exit when Entity (ASN) /= E;
1168 if Is_Delayed_Aspect (ASN) then
1169 A_Id := Get_Aspect_Id (ASN);
1171 case A_Id is
1173 -- For aspects whose expression is an optional Boolean, make
1174 -- the corresponding pragma at the freeze point.
1176 when Boolean_Aspects |
1177 Library_Unit_Aspects =>
1179 -- Aspects Export and Import require special handling.
1180 -- Both are by definition Boolean and may benefit from
1181 -- forward references, however their expressions are
1182 -- treated as static. In addition, the syntax of their
1183 -- corresponding pragmas requires extra "pieces" which
1184 -- may also contain forward references. To account for
1185 -- all of this, the corresponding pragma is created by
1186 -- Analyze_Aspect_Export_Import, but is not analyzed as
1187 -- the complete analysis must happen now.
1189 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1190 null;
1192 -- Otherwise create a corresponding pragma
1194 else
1195 Make_Pragma_From_Boolean_Aspect (ASN);
1196 end if;
1198 -- Special handling for aspects that don't correspond to
1199 -- pragmas/attributes.
1201 when Aspect_Default_Value |
1202 Aspect_Default_Component_Value =>
1204 -- Do not inherit aspect for anonymous base type of a
1205 -- scalar or array type, because they apply to the first
1206 -- subtype of the type, and will be processed when that
1207 -- first subtype is frozen.
1209 if Is_Derived_Type (E)
1210 and then not Comes_From_Source (E)
1211 and then E /= First_Subtype (E)
1212 then
1213 null;
1214 else
1215 Analyze_Aspect_Default_Value (ASN);
1216 end if;
1218 -- Ditto for iterator aspects, because the corresponding
1219 -- attributes may not have been analyzed yet.
1221 when Aspect_Constant_Indexing |
1222 Aspect_Variable_Indexing |
1223 Aspect_Default_Iterator |
1224 Aspect_Iterator_Element =>
1225 Analyze (Expression (ASN));
1227 if Etype (Expression (ASN)) = Any_Type then
1228 Error_Msg_NE
1229 ("\aspect must be fully defined before & is frozen",
1230 ASN, E);
1231 end if;
1233 when Aspect_Iterable =>
1234 Validate_Iterable_Aspect (E, ASN);
1236 when others =>
1237 null;
1238 end case;
1240 Ritem := Aspect_Rep_Item (ASN);
1242 if Present (Ritem) then
1243 Analyze (Ritem);
1244 end if;
1245 end if;
1246 end if;
1248 Next_Rep_Item (ASN);
1249 end loop;
1251 -- This is where we inherit delayed rep aspects from our parent. Note
1252 -- that if we fell out of the above loop with ASN non-empty, it means
1253 -- we hit an aspect for an entity other than E, and it must be the
1254 -- type from which we were derived.
1256 if May_Inherit_Delayed_Rep_Aspects (E) then
1257 Inherit_Delayed_Rep_Aspects (ASN);
1258 end if;
1259 end Analyze_Aspects_At_Freeze_Point;
1261 -----------------------------------
1262 -- Analyze_Aspect_Specifications --
1263 -----------------------------------
1265 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1266 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1267 -- Establish linkages between an aspect and its corresponding pragma
1269 procedure Insert_Pragma
1270 (Prag : Node_Id;
1271 Is_Instance : Boolean := False);
1272 -- Subsidiary to the analysis of aspects
1273 -- Abstract_State
1274 -- Attach_Handler
1275 -- Contract_Cases
1276 -- Depends
1277 -- Ghost
1278 -- Global
1279 -- Initial_Condition
1280 -- Initializes
1281 -- Post
1282 -- Pre
1283 -- Refined_Depends
1284 -- Refined_Global
1285 -- Refined_State
1286 -- SPARK_Mode
1287 -- Warnings
1288 -- Insert pragma Prag such that it mimics the placement of a source
1289 -- pragma of the same kind. Flag Is_Generic should be set when the
1290 -- context denotes a generic instance.
1292 --------------
1293 -- Decorate --
1294 --------------
1296 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1297 begin
1298 Set_Aspect_Rep_Item (Asp, Prag);
1299 Set_Corresponding_Aspect (Prag, Asp);
1300 Set_From_Aspect_Specification (Prag);
1301 Set_Parent (Prag, Asp);
1302 end Decorate;
1304 -------------------
1305 -- Insert_Pragma --
1306 -------------------
1308 procedure Insert_Pragma
1309 (Prag : Node_Id;
1310 Is_Instance : Boolean := False)
1312 Aux : Node_Id;
1313 Decl : Node_Id;
1314 Decls : List_Id;
1315 Def : Node_Id;
1316 Inserted : Boolean := False;
1318 begin
1319 -- When the aspect appears on an entry, package, protected unit,
1320 -- subprogram, or task unit body, insert the generated pragma at the
1321 -- top of the body declarations to emulate the behavior of a source
1322 -- pragma.
1324 -- package body Pack with Aspect is
1326 -- package body Pack is
1327 -- pragma Prag;
1329 if Nkind_In (N, N_Entry_Body,
1330 N_Package_Body,
1331 N_Protected_Body,
1332 N_Subprogram_Body,
1333 N_Task_Body)
1334 then
1335 Decls := Declarations (N);
1337 if No (Decls) then
1338 Decls := New_List;
1339 Set_Declarations (N, Decls);
1340 end if;
1342 Prepend_To (Decls, Prag);
1344 -- When the aspect is associated with a [generic] package declaration
1345 -- insert the generated pragma at the top of the visible declarations
1346 -- to emulate the behavior of a source pragma.
1348 -- package Pack with Aspect is
1350 -- package Pack is
1351 -- pragma Prag;
1353 elsif Nkind_In (N, N_Generic_Package_Declaration,
1354 N_Package_Declaration)
1355 then
1356 Decls := Visible_Declarations (Specification (N));
1358 if No (Decls) then
1359 Decls := New_List;
1360 Set_Visible_Declarations (Specification (N), Decls);
1361 end if;
1363 -- The visible declarations of a generic instance have the
1364 -- following structure:
1366 -- <renamings of generic formals>
1367 -- <renamings of internally-generated spec and body>
1368 -- <first source declaration>
1370 -- Insert the pragma before the first source declaration by
1371 -- skipping the instance "header" to ensure proper visibility of
1372 -- all formals.
1374 if Is_Instance then
1375 Decl := First (Decls);
1376 while Present (Decl) loop
1377 if Comes_From_Source (Decl) then
1378 Insert_Before (Decl, Prag);
1379 Inserted := True;
1380 exit;
1381 else
1382 Next (Decl);
1383 end if;
1384 end loop;
1386 -- The pragma is placed after the instance "header"
1388 if not Inserted then
1389 Append_To (Decls, Prag);
1390 end if;
1392 -- Otherwise this is not a generic instance
1394 else
1395 Prepend_To (Decls, Prag);
1396 end if;
1398 -- When the aspect is associated with a protected unit declaration,
1399 -- insert the generated pragma at the top of the visible declarations
1400 -- the emulate the behavior of a source pragma.
1402 -- protected [type] Prot with Aspect is
1404 -- protected [type] Prot is
1405 -- pragma Prag;
1407 elsif Nkind (N) = N_Protected_Type_Declaration then
1408 Def := Protected_Definition (N);
1410 if No (Def) then
1411 Def :=
1412 Make_Protected_Definition (Sloc (N),
1413 Visible_Declarations => New_List,
1414 End_Label => Empty);
1416 Set_Protected_Definition (N, Def);
1417 end if;
1419 Decls := Visible_Declarations (Def);
1421 if No (Decls) then
1422 Decls := New_List;
1423 Set_Visible_Declarations (Def, Decls);
1424 end if;
1426 Prepend_To (Decls, Prag);
1428 -- When the aspect is associated with a task unit declaration, insert
1429 -- insert the generated pragma at the top of the visible declarations
1430 -- the emulate the behavior of a source pragma.
1432 -- task [type] Prot with Aspect is
1434 -- task [type] Prot is
1435 -- pragma Prag;
1437 elsif Nkind (N) = N_Task_Type_Declaration then
1438 Def := Task_Definition (N);
1440 if No (Def) then
1441 Def :=
1442 Make_Task_Definition (Sloc (N),
1443 Visible_Declarations => New_List,
1444 End_Label => Empty);
1446 Set_Task_Definition (N, Def);
1447 end if;
1449 Decls := Visible_Declarations (Def);
1451 if No (Decls) then
1452 Decls := New_List;
1453 Set_Visible_Declarations (Def, Decls);
1454 end if;
1456 Prepend_To (Decls, Prag);
1458 -- When the context is a library unit, the pragma is added to the
1459 -- Pragmas_After list.
1461 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1462 Aux := Aux_Decls_Node (Parent (N));
1464 if No (Pragmas_After (Aux)) then
1465 Set_Pragmas_After (Aux, New_List);
1466 end if;
1468 Prepend (Prag, Pragmas_After (Aux));
1470 -- Default, the pragma is inserted after the context
1472 else
1473 Insert_After (N, Prag);
1474 end if;
1475 end Insert_Pragma;
1477 -- Local variables
1479 Aspect : Node_Id;
1480 Aitem : Node_Id;
1481 Ent : Node_Id;
1483 L : constant List_Id := Aspect_Specifications (N);
1485 Ins_Node : Node_Id := N;
1486 -- Insert pragmas/attribute definition clause after this node when no
1487 -- delayed analysis is required.
1489 -- Start of processing for Analyze_Aspect_Specifications
1491 begin
1492 -- The general processing involves building an attribute definition
1493 -- clause or a pragma node that corresponds to the aspect. Then in order
1494 -- to delay the evaluation of this aspect to the freeze point, we attach
1495 -- the corresponding pragma/attribute definition clause to the aspect
1496 -- specification node, which is then placed in the Rep Item chain. In
1497 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1498 -- and we evaluate the rep item at the freeze point. When the aspect
1499 -- doesn't have a corresponding pragma/attribute definition clause, then
1500 -- its analysis is simply delayed at the freeze point.
1502 -- Some special cases don't require delay analysis, thus the aspect is
1503 -- analyzed right now.
1505 -- Note that there is a special handling for Pre, Post, Test_Case,
1506 -- Contract_Cases aspects. In these cases, we do not have to worry
1507 -- about delay issues, since the pragmas themselves deal with delay
1508 -- of visibility for the expression analysis. Thus, we just insert
1509 -- the pragma after the node N.
1511 pragma Assert (Present (L));
1513 -- Loop through aspects
1515 Aspect := First (L);
1516 Aspect_Loop : while Present (Aspect) loop
1517 Analyze_One_Aspect : declare
1518 Expr : constant Node_Id := Expression (Aspect);
1519 Id : constant Node_Id := Identifier (Aspect);
1520 Loc : constant Source_Ptr := Sloc (Aspect);
1521 Nam : constant Name_Id := Chars (Id);
1522 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1523 Anod : Node_Id;
1525 Delay_Required : Boolean;
1526 -- Set False if delay is not required
1528 Eloc : Source_Ptr := No_Location;
1529 -- Source location of expression, modified when we split PPC's. It
1530 -- is set below when Expr is present.
1532 procedure Analyze_Aspect_Convention;
1533 -- Perform analysis of aspect Convention
1535 procedure Analyze_Aspect_Export_Import;
1536 -- Perform analysis of aspects Export or Import
1538 procedure Analyze_Aspect_External_Link_Name;
1539 -- Perform analysis of aspects External_Name or Link_Name
1541 procedure Analyze_Aspect_Implicit_Dereference;
1542 -- Perform analysis of the Implicit_Dereference aspects
1544 procedure Make_Aitem_Pragma
1545 (Pragma_Argument_Associations : List_Id;
1546 Pragma_Name : Name_Id);
1547 -- This is a wrapper for Make_Pragma used for converting aspects
1548 -- to pragmas. It takes care of Sloc (set from Loc) and building
1549 -- the pragma identifier from the given name. In addition the
1550 -- flags Class_Present and Split_PPC are set from the aspect
1551 -- node, as well as Is_Ignored. This routine also sets the
1552 -- From_Aspect_Specification in the resulting pragma node to
1553 -- True, and sets Corresponding_Aspect to point to the aspect.
1554 -- The resulting pragma is assigned to Aitem.
1556 -------------------------------
1557 -- Analyze_Aspect_Convention --
1558 -------------------------------
1560 procedure Analyze_Aspect_Convention is
1561 Conv : Node_Id;
1562 Dummy_1 : Node_Id;
1563 Dummy_2 : Node_Id;
1564 Dummy_3 : Node_Id;
1565 Expo : Node_Id;
1566 Imp : Node_Id;
1568 begin
1569 -- Obtain all interfacing aspects that apply to the related
1570 -- entity.
1572 Get_Interfacing_Aspects
1573 (Iface_Asp => Aspect,
1574 Conv_Asp => Dummy_1,
1575 EN_Asp => Dummy_2,
1576 Expo_Asp => Expo,
1577 Imp_Asp => Imp,
1578 LN_Asp => Dummy_3,
1579 Do_Checks => True);
1581 -- The related entity is subject to aspect Export or Import.
1582 -- Do not process Convention now because it must be analysed
1583 -- as part of Export or Import.
1585 if Present (Expo) or else Present (Imp) then
1586 return;
1588 -- Otherwise Convention appears by itself
1590 else
1591 -- The aspect specifies a particular convention
1593 if Present (Expr) then
1594 Conv := New_Copy_Tree (Expr);
1596 -- Otherwise assume convention Ada
1598 else
1599 Conv := Make_Identifier (Loc, Name_Ada);
1600 end if;
1602 -- Generate:
1603 -- pragma Convention (<Conv>, <E>);
1605 Make_Aitem_Pragma
1606 (Pragma_Name => Name_Convention,
1607 Pragma_Argument_Associations => New_List (
1608 Make_Pragma_Argument_Association (Loc,
1609 Expression => Conv),
1610 Make_Pragma_Argument_Association (Loc,
1611 Expression => New_Occurrence_Of (E, Loc))));
1613 Decorate (Aspect, Aitem);
1614 Insert_Pragma (Aitem);
1615 end if;
1616 end Analyze_Aspect_Convention;
1618 ----------------------------------
1619 -- Analyze_Aspect_Export_Import --
1620 ----------------------------------
1622 procedure Analyze_Aspect_Export_Import is
1623 Dummy_1 : Node_Id;
1624 Dummy_2 : Node_Id;
1625 Dummy_3 : Node_Id;
1626 Expo : Node_Id;
1627 Imp : Node_Id;
1629 begin
1630 -- Obtain all interfacing aspects that apply to the related
1631 -- entity.
1633 Get_Interfacing_Aspects
1634 (Iface_Asp => Aspect,
1635 Conv_Asp => Dummy_1,
1636 EN_Asp => Dummy_2,
1637 Expo_Asp => Expo,
1638 Imp_Asp => Imp,
1639 LN_Asp => Dummy_3,
1640 Do_Checks => True);
1642 -- The related entity cannot be subject to both aspects Export
1643 -- and Import.
1645 if Present (Expo) and then Present (Imp) then
1646 Error_Msg_N
1647 ("incompatible interfacing aspects given for &", E);
1648 Error_Msg_Sloc := Sloc (Expo);
1649 Error_Msg_N ("\aspect `Export` #", E);
1650 Error_Msg_Sloc := Sloc (Imp);
1651 Error_Msg_N ("\aspect `Import` #", E);
1652 end if;
1654 -- A variable is most likely modified from the outside. Take
1655 -- Take the optimistic approach to avoid spurious errors.
1657 if Ekind (E) = E_Variable then
1658 Set_Never_Set_In_Source (E, False);
1659 end if;
1661 -- Resolve the expression of an Import or Export here, and
1662 -- require it to be of type Boolean and static. This is not
1663 -- quite right, because in general this should be delayed,
1664 -- but that seems tricky for these, because normally Boolean
1665 -- aspects are replaced with pragmas at the freeze point in
1666 -- Make_Pragma_From_Boolean_Aspect.
1668 if not Present (Expr)
1669 or else Is_True (Static_Boolean (Expr))
1670 then
1671 if A_Id = Aspect_Import then
1672 Set_Has_Completion (E);
1673 Set_Is_Imported (E);
1675 -- An imported object cannot be explicitly initialized
1677 if Nkind (N) = N_Object_Declaration
1678 and then Present (Expression (N))
1679 then
1680 Error_Msg_N
1681 ("imported entities cannot be initialized "
1682 & "(RM B.1(24))", Expression (N));
1683 end if;
1685 else
1686 pragma Assert (A_Id = Aspect_Export);
1687 Set_Is_Exported (E);
1688 end if;
1690 -- Create the proper form of pragma Export or Import taking
1691 -- into account Conversion, External_Name, and Link_Name.
1693 Aitem := Build_Export_Import_Pragma (Aspect, E);
1695 -- Otherwise the expression is either False or erroneous. There
1696 -- is no corresponding pragma.
1698 else
1699 Aitem := Empty;
1700 end if;
1701 end Analyze_Aspect_Export_Import;
1703 ---------------------------------------
1704 -- Analyze_Aspect_External_Link_Name --
1705 ---------------------------------------
1707 procedure Analyze_Aspect_External_Link_Name is
1708 Dummy_1 : Node_Id;
1709 Dummy_2 : Node_Id;
1710 Dummy_3 : Node_Id;
1711 Expo : Node_Id;
1712 Imp : Node_Id;
1714 begin
1715 -- Obtain all interfacing aspects that apply to the related
1716 -- entity.
1718 Get_Interfacing_Aspects
1719 (Iface_Asp => Aspect,
1720 Conv_Asp => Dummy_1,
1721 EN_Asp => Dummy_2,
1722 Expo_Asp => Expo,
1723 Imp_Asp => Imp,
1724 LN_Asp => Dummy_3,
1725 Do_Checks => True);
1727 -- Ensure that aspect External_Name applies to aspect Export or
1728 -- Import.
1730 if A_Id = Aspect_External_Name then
1731 if No (Expo) and then No (Imp) then
1732 Error_Msg_N
1733 ("aspect `External_Name` requires aspect `Import` or "
1734 & "`Export`", Aspect);
1735 end if;
1737 -- Otherwise ensure that aspect Link_Name applies to aspect
1738 -- Export or Import.
1740 else
1741 pragma Assert (A_Id = Aspect_Link_Name);
1742 if No (Expo) and then No (Imp) then
1743 Error_Msg_N
1744 ("aspect `Link_Name` requires aspect `Import` or "
1745 & "`Export`", Aspect);
1746 end if;
1747 end if;
1748 end Analyze_Aspect_External_Link_Name;
1750 -----------------------------------------
1751 -- Analyze_Aspect_Implicit_Dereference --
1752 -----------------------------------------
1754 procedure Analyze_Aspect_Implicit_Dereference is
1755 Disc : Entity_Id;
1756 Parent_Disc : Entity_Id;
1758 begin
1759 if not Is_Type (E) or else not Has_Discriminants (E) then
1760 Error_Msg_N
1761 ("aspect must apply to a type with discriminants", Expr);
1763 elsif not Is_Entity_Name (Expr) then
1764 Error_Msg_N
1765 ("aspect must name a discriminant of current type", Expr);
1767 else
1768 Disc := First_Discriminant (E);
1769 while Present (Disc) loop
1770 if Chars (Expr) = Chars (Disc)
1771 and then Ekind (Etype (Disc)) =
1772 E_Anonymous_Access_Type
1773 then
1774 Set_Has_Implicit_Dereference (E);
1775 Set_Has_Implicit_Dereference (Disc);
1776 exit;
1777 end if;
1779 Next_Discriminant (Disc);
1780 end loop;
1782 -- Error if no proper access discriminant
1784 if No (Disc) then
1785 Error_Msg_NE ("not an access discriminant of&", Expr, E);
1786 return;
1787 end if;
1788 end if;
1790 -- For a type extension, check whether parent has a
1791 -- reference discriminant, to verify that use is proper.
1793 if Is_Derived_Type (E)
1794 and then Has_Discriminants (Etype (E))
1795 then
1796 Parent_Disc := Get_Reference_Discriminant (Etype (E));
1798 if Present (Parent_Disc)
1799 and then Corresponding_Discriminant (Disc) /= Parent_Disc
1800 then
1801 Error_Msg_N
1802 ("reference discriminant does not match discriminant "
1803 & "of parent type", Expr);
1804 end if;
1805 end if;
1806 end Analyze_Aspect_Implicit_Dereference;
1808 -----------------------
1809 -- Make_Aitem_Pragma --
1810 -----------------------
1812 procedure Make_Aitem_Pragma
1813 (Pragma_Argument_Associations : List_Id;
1814 Pragma_Name : Name_Id)
1816 Args : List_Id := Pragma_Argument_Associations;
1818 begin
1819 -- We should never get here if aspect was disabled
1821 pragma Assert (not Is_Disabled (Aspect));
1823 -- Certain aspects allow for an optional name or expression. Do
1824 -- not generate a pragma with empty argument association list.
1826 if No (Args) or else No (Expression (First (Args))) then
1827 Args := No_List;
1828 end if;
1830 -- Build the pragma
1832 Aitem :=
1833 Make_Pragma (Loc,
1834 Pragma_Argument_Associations => Args,
1835 Pragma_Identifier =>
1836 Make_Identifier (Sloc (Id), Pragma_Name),
1837 Class_Present => Class_Present (Aspect),
1838 Split_PPC => Split_PPC (Aspect));
1840 -- Set additional semantic fields
1842 if Is_Ignored (Aspect) then
1843 Set_Is_Ignored (Aitem);
1844 elsif Is_Checked (Aspect) then
1845 Set_Is_Checked (Aitem);
1846 end if;
1848 Set_Corresponding_Aspect (Aitem, Aspect);
1849 Set_From_Aspect_Specification (Aitem);
1850 end Make_Aitem_Pragma;
1852 -- Start of processing for Analyze_One_Aspect
1854 begin
1855 -- Skip aspect if already analyzed, to avoid looping in some cases
1857 if Analyzed (Aspect) then
1858 goto Continue;
1859 end if;
1861 -- Skip looking at aspect if it is totally disabled. Just mark it
1862 -- as such for later reference in the tree. This also sets the
1863 -- Is_Ignored and Is_Checked flags appropriately.
1865 Check_Applicable_Policy (Aspect);
1867 if Is_Disabled (Aspect) then
1868 goto Continue;
1869 end if;
1871 -- Set the source location of expression, used in the case of
1872 -- a failed precondition/postcondition or invariant. Note that
1873 -- the source location of the expression is not usually the best
1874 -- choice here. For example, it gets located on the last AND
1875 -- keyword in a chain of boolean expressiond AND'ed together.
1876 -- It is best to put the message on the first character of the
1877 -- assertion, which is the effect of the First_Node call here.
1879 if Present (Expr) then
1880 Eloc := Sloc (First_Node (Expr));
1881 end if;
1883 -- Check restriction No_Implementation_Aspect_Specifications
1885 if Implementation_Defined_Aspect (A_Id) then
1886 Check_Restriction
1887 (No_Implementation_Aspect_Specifications, Aspect);
1888 end if;
1890 -- Check restriction No_Specification_Of_Aspect
1892 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1894 -- Mark aspect analyzed (actual analysis is delayed till later)
1896 Set_Analyzed (Aspect);
1897 Set_Entity (Aspect, E);
1898 Ent := New_Occurrence_Of (E, Sloc (Id));
1900 -- Check for duplicate aspect. Note that the Comes_From_Source
1901 -- test allows duplicate Pre/Post's that we generate internally
1902 -- to escape being flagged here.
1904 if No_Duplicates_Allowed (A_Id) then
1905 Anod := First (L);
1906 while Anod /= Aspect loop
1907 if Comes_From_Source (Aspect)
1908 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1909 then
1910 Error_Msg_Name_1 := Nam;
1911 Error_Msg_Sloc := Sloc (Anod);
1913 -- Case of same aspect specified twice
1915 if Class_Present (Anod) = Class_Present (Aspect) then
1916 if not Class_Present (Anod) then
1917 Error_Msg_NE
1918 ("aspect% for & previously given#",
1919 Id, E);
1920 else
1921 Error_Msg_NE
1922 ("aspect `%''Class` for & previously given#",
1923 Id, E);
1924 end if;
1925 end if;
1926 end if;
1928 Next (Anod);
1929 end loop;
1930 end if;
1932 -- Check some general restrictions on language defined aspects
1934 if not Implementation_Defined_Aspect (A_Id) then
1935 Error_Msg_Name_1 := Nam;
1937 -- Not allowed for renaming declarations
1939 if Nkind (N) in N_Renaming_Declaration then
1940 Error_Msg_N
1941 ("aspect % not allowed for renaming declaration",
1942 Aspect);
1943 end if;
1945 -- Not allowed for formal type declarations
1947 if Nkind (N) = N_Formal_Type_Declaration then
1948 Error_Msg_N
1949 ("aspect % not allowed for formal type declaration",
1950 Aspect);
1951 end if;
1952 end if;
1954 -- Copy expression for later processing by the procedures
1955 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1957 Set_Entity (Id, New_Copy_Tree (Expr));
1959 -- Set Delay_Required as appropriate to aspect
1961 case Aspect_Delay (A_Id) is
1962 when Always_Delay =>
1963 Delay_Required := True;
1965 when Never_Delay =>
1966 Delay_Required := False;
1968 when Rep_Aspect =>
1970 -- If expression has the form of an integer literal, then
1971 -- do not delay, since we know the value cannot change.
1972 -- This optimization catches most rep clause cases.
1974 -- For Boolean aspects, don't delay if no expression
1976 if A_Id in Boolean_Aspects and then No (Expr) then
1977 Delay_Required := False;
1979 -- For non-Boolean aspects, don't delay if integer literal
1981 elsif A_Id not in Boolean_Aspects
1982 and then Present (Expr)
1983 and then Nkind (Expr) = N_Integer_Literal
1984 then
1985 Delay_Required := False;
1987 -- All other cases are delayed
1989 else
1990 Delay_Required := True;
1991 Set_Has_Delayed_Rep_Aspects (E);
1992 end if;
1993 end case;
1995 -- Processing based on specific aspect
1997 case A_Id is
1998 when Aspect_Unimplemented =>
1999 null; -- ??? temp for now
2001 -- No_Aspect should be impossible
2003 when No_Aspect =>
2004 raise Program_Error;
2006 -- Case 1: Aspects corresponding to attribute definition
2007 -- clauses.
2009 when Aspect_Address |
2010 Aspect_Alignment |
2011 Aspect_Bit_Order |
2012 Aspect_Component_Size |
2013 Aspect_Constant_Indexing |
2014 Aspect_Default_Iterator |
2015 Aspect_Dispatching_Domain |
2016 Aspect_External_Tag |
2017 Aspect_Input |
2018 Aspect_Iterable |
2019 Aspect_Iterator_Element |
2020 Aspect_Machine_Radix |
2021 Aspect_Object_Size |
2022 Aspect_Output |
2023 Aspect_Read |
2024 Aspect_Scalar_Storage_Order |
2025 Aspect_Size |
2026 Aspect_Small |
2027 Aspect_Simple_Storage_Pool |
2028 Aspect_Storage_Pool |
2029 Aspect_Stream_Size |
2030 Aspect_Value_Size |
2031 Aspect_Variable_Indexing |
2032 Aspect_Write =>
2034 -- Indexing aspects apply only to tagged type
2036 if (A_Id = Aspect_Constant_Indexing
2037 or else
2038 A_Id = Aspect_Variable_Indexing)
2039 and then not (Is_Type (E)
2040 and then Is_Tagged_Type (E))
2041 then
2042 Error_Msg_N
2043 ("indexing aspect can only apply to a tagged type",
2044 Aspect);
2045 goto Continue;
2046 end if;
2048 -- For the case of aspect Address, we don't consider that we
2049 -- know the entity is never set in the source, since it is
2050 -- is likely aliasing is occurring.
2052 -- Note: one might think that the analysis of the resulting
2053 -- attribute definition clause would take care of that, but
2054 -- that's not the case since it won't be from source.
2056 if A_Id = Aspect_Address then
2057 Set_Never_Set_In_Source (E, False);
2058 end if;
2060 -- Correctness of the profile of a stream operation is
2061 -- verified at the freeze point, but we must detect the
2062 -- illegal specification of this aspect for a subtype now,
2063 -- to prevent malformed rep_item chains.
2065 if A_Id = Aspect_Input or else
2066 A_Id = Aspect_Output or else
2067 A_Id = Aspect_Read or else
2068 A_Id = Aspect_Write
2069 then
2070 if not Is_First_Subtype (E) then
2071 Error_Msg_N
2072 ("local name must be a first subtype", Aspect);
2073 goto Continue;
2075 -- If stream aspect applies to the class-wide type,
2076 -- the generated attribute definition applies to the
2077 -- class-wide type as well.
2079 elsif Class_Present (Aspect) then
2080 Ent :=
2081 Make_Attribute_Reference (Loc,
2082 Prefix => Ent,
2083 Attribute_Name => Name_Class);
2084 end if;
2085 end if;
2087 -- Construct the attribute definition clause
2089 Aitem :=
2090 Make_Attribute_Definition_Clause (Loc,
2091 Name => Ent,
2092 Chars => Chars (Id),
2093 Expression => Relocate_Node (Expr));
2095 -- If the address is specified, then we treat the entity as
2096 -- referenced, to avoid spurious warnings. This is analogous
2097 -- to what is done with an attribute definition clause, but
2098 -- here we don't want to generate a reference because this
2099 -- is the point of definition of the entity.
2101 if A_Id = Aspect_Address then
2102 Set_Referenced (E);
2103 end if;
2105 -- Case 2: Aspects corresponding to pragmas
2107 -- Case 2a: Aspects corresponding to pragmas with two
2108 -- arguments, where the first argument is a local name
2109 -- referring to the entity, and the second argument is the
2110 -- aspect definition expression.
2112 -- Linker_Section/Suppress/Unsuppress
2114 when Aspect_Linker_Section |
2115 Aspect_Suppress |
2116 Aspect_Unsuppress =>
2118 Make_Aitem_Pragma
2119 (Pragma_Argument_Associations => New_List (
2120 Make_Pragma_Argument_Association (Loc,
2121 Expression => New_Occurrence_Of (E, Loc)),
2122 Make_Pragma_Argument_Association (Sloc (Expr),
2123 Expression => Relocate_Node (Expr))),
2124 Pragma_Name => Chars (Id));
2126 -- Synchronization
2128 -- Corresponds to pragma Implemented, construct the pragma
2130 when Aspect_Synchronization =>
2131 Make_Aitem_Pragma
2132 (Pragma_Argument_Associations => New_List (
2133 Make_Pragma_Argument_Association (Loc,
2134 Expression => New_Occurrence_Of (E, Loc)),
2135 Make_Pragma_Argument_Association (Sloc (Expr),
2136 Expression => Relocate_Node (Expr))),
2137 Pragma_Name => Name_Implemented);
2139 -- Attach_Handler
2141 when Aspect_Attach_Handler =>
2142 Make_Aitem_Pragma
2143 (Pragma_Argument_Associations => New_List (
2144 Make_Pragma_Argument_Association (Sloc (Ent),
2145 Expression => Ent),
2146 Make_Pragma_Argument_Association (Sloc (Expr),
2147 Expression => Relocate_Node (Expr))),
2148 Pragma_Name => Name_Attach_Handler);
2150 -- We need to insert this pragma into the tree to get proper
2151 -- processing and to look valid from a placement viewpoint.
2153 Insert_Pragma (Aitem);
2154 goto Continue;
2156 -- Dynamic_Predicate, Predicate, Static_Predicate
2158 when Aspect_Dynamic_Predicate |
2159 Aspect_Predicate |
2160 Aspect_Static_Predicate =>
2162 -- These aspects apply only to subtypes
2164 if not Is_Type (E) then
2165 Error_Msg_N
2166 ("predicate can only be specified for a subtype",
2167 Aspect);
2168 goto Continue;
2170 elsif Is_Incomplete_Type (E) then
2171 Error_Msg_N
2172 ("predicate cannot apply to incomplete view", Aspect);
2173 goto Continue;
2174 end if;
2176 -- Construct the pragma (always a pragma Predicate, with
2177 -- flags recording whether it is static/dynamic). We also
2178 -- set flags recording this in the type itself.
2180 Make_Aitem_Pragma
2181 (Pragma_Argument_Associations => New_List (
2182 Make_Pragma_Argument_Association (Sloc (Ent),
2183 Expression => Ent),
2184 Make_Pragma_Argument_Association (Sloc (Expr),
2185 Expression => Relocate_Node (Expr))),
2186 Pragma_Name => Name_Predicate);
2188 -- Mark type has predicates, and remember what kind of
2189 -- aspect lead to this predicate (we need this to access
2190 -- the right set of check policies later on).
2192 Set_Has_Predicates (E);
2194 if A_Id = Aspect_Dynamic_Predicate then
2195 Set_Has_Dynamic_Predicate_Aspect (E);
2196 elsif A_Id = Aspect_Static_Predicate then
2197 Set_Has_Static_Predicate_Aspect (E);
2198 end if;
2200 -- If the type is private, indicate that its completion
2201 -- has a freeze node, because that is the one that will
2202 -- be visible at freeze time.
2204 if Is_Private_Type (E) and then Present (Full_View (E)) then
2205 Set_Has_Predicates (Full_View (E));
2207 if A_Id = Aspect_Dynamic_Predicate then
2208 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2209 elsif A_Id = Aspect_Static_Predicate then
2210 Set_Has_Static_Predicate_Aspect (Full_View (E));
2211 end if;
2213 Set_Has_Delayed_Aspects (Full_View (E));
2214 Ensure_Freeze_Node (Full_View (E));
2215 end if;
2217 -- Predicate_Failure
2219 when Aspect_Predicate_Failure =>
2221 -- This aspect applies only to subtypes
2223 if not Is_Type (E) then
2224 Error_Msg_N
2225 ("predicate can only be specified for a subtype",
2226 Aspect);
2227 goto Continue;
2229 elsif Is_Incomplete_Type (E) then
2230 Error_Msg_N
2231 ("predicate cannot apply to incomplete view", Aspect);
2232 goto Continue;
2233 end if;
2235 -- Construct the pragma
2237 Make_Aitem_Pragma
2238 (Pragma_Argument_Associations => New_List (
2239 Make_Pragma_Argument_Association (Sloc (Ent),
2240 Expression => Ent),
2241 Make_Pragma_Argument_Association (Sloc (Expr),
2242 Expression => Relocate_Node (Expr))),
2243 Pragma_Name => Name_Predicate_Failure);
2245 Set_Has_Predicates (E);
2247 -- If the type is private, indicate that its completion
2248 -- has a freeze node, because that is the one that will
2249 -- be visible at freeze time.
2251 if Is_Private_Type (E) and then Present (Full_View (E)) then
2252 Set_Has_Predicates (Full_View (E));
2253 Set_Has_Delayed_Aspects (Full_View (E));
2254 Ensure_Freeze_Node (Full_View (E));
2255 end if;
2257 -- Case 2b: Aspects corresponding to pragmas with two
2258 -- arguments, where the second argument is a local name
2259 -- referring to the entity, and the first argument is the
2260 -- aspect definition expression.
2262 -- Convention
2264 when Aspect_Convention =>
2265 Analyze_Aspect_Convention;
2266 goto Continue;
2268 -- External_Name, Link_Name
2270 when Aspect_External_Name |
2271 Aspect_Link_Name =>
2272 Analyze_Aspect_External_Link_Name;
2273 goto Continue;
2275 -- CPU, Interrupt_Priority, Priority
2277 -- These three aspects can be specified for a subprogram spec
2278 -- or body, in which case we analyze the expression and export
2279 -- the value of the aspect.
2281 -- Previously, we generated an equivalent pragma for bodies
2282 -- (note that the specs cannot contain these pragmas). The
2283 -- pragma was inserted ahead of local declarations, rather than
2284 -- after the body. This leads to a certain duplication between
2285 -- the processing performed for the aspect and the pragma, but
2286 -- given the straightforward handling required it is simpler
2287 -- to duplicate than to translate the aspect in the spec into
2288 -- a pragma in the declarative part of the body.
2290 when Aspect_CPU |
2291 Aspect_Interrupt_Priority |
2292 Aspect_Priority =>
2294 if Nkind_In (N, N_Subprogram_Body,
2295 N_Subprogram_Declaration)
2296 then
2297 -- Analyze the aspect expression
2299 Analyze_And_Resolve (Expr, Standard_Integer);
2301 -- Interrupt_Priority aspect not allowed for main
2302 -- subprograms. RM D.1 does not forbid this explicitly,
2303 -- but RM J.15.11(6/3) does not permit pragma
2304 -- Interrupt_Priority for subprograms.
2306 if A_Id = Aspect_Interrupt_Priority then
2307 Error_Msg_N
2308 ("Interrupt_Priority aspect cannot apply to "
2309 & "subprogram", Expr);
2311 -- The expression must be static
2313 elsif not Is_OK_Static_Expression (Expr) then
2314 Flag_Non_Static_Expr
2315 ("aspect requires static expression!", Expr);
2317 -- Check whether this is the main subprogram. Issue a
2318 -- warning only if it is obviously not a main program
2319 -- (when it has parameters or when the subprogram is
2320 -- within a package).
2322 elsif Present (Parameter_Specifications
2323 (Specification (N)))
2324 or else not Is_Compilation_Unit (Defining_Entity (N))
2325 then
2326 -- See RM D.1(14/3) and D.16(12/3)
2328 Error_Msg_N
2329 ("aspect applied to subprogram other than the "
2330 & "main subprogram has no effect??", Expr);
2332 -- Otherwise check in range and export the value
2334 -- For the CPU aspect
2336 elsif A_Id = Aspect_CPU then
2337 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2339 -- Value is correct so we export the value to make
2340 -- it available at execution time.
2342 Set_Main_CPU
2343 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2345 else
2346 Error_Msg_N
2347 ("main subprogram CPU is out of range", Expr);
2348 end if;
2350 -- For the Priority aspect
2352 elsif A_Id = Aspect_Priority then
2353 if Is_In_Range (Expr, RTE (RE_Priority)) then
2355 -- Value is correct so we export the value to make
2356 -- it available at execution time.
2358 Set_Main_Priority
2359 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2361 -- Ignore pragma if Relaxed_RM_Semantics to support
2362 -- other targets/non GNAT compilers.
2364 elsif not Relaxed_RM_Semantics then
2365 Error_Msg_N
2366 ("main subprogram priority is out of range",
2367 Expr);
2368 end if;
2369 end if;
2371 -- Load an arbitrary entity from System.Tasking.Stages
2372 -- or System.Tasking.Restricted.Stages (depending on
2373 -- the supported profile) to make sure that one of these
2374 -- packages is implicitly with'ed, since we need to have
2375 -- the tasking run time active for the pragma Priority to
2376 -- have any effect. Previously we with'ed the package
2377 -- System.Tasking, but this package does not trigger the
2378 -- required initialization of the run-time library.
2380 declare
2381 Discard : Entity_Id;
2382 begin
2383 if Restricted_Profile then
2384 Discard := RTE (RE_Activate_Restricted_Tasks);
2385 else
2386 Discard := RTE (RE_Activate_Tasks);
2387 end if;
2388 end;
2390 -- Handling for these Aspects in subprograms is complete
2392 goto Continue;
2394 -- For tasks pass the aspect as an attribute
2396 else
2397 Aitem :=
2398 Make_Attribute_Definition_Clause (Loc,
2399 Name => Ent,
2400 Chars => Chars (Id),
2401 Expression => Relocate_Node (Expr));
2402 end if;
2404 -- Warnings
2406 when Aspect_Warnings =>
2407 Make_Aitem_Pragma
2408 (Pragma_Argument_Associations => New_List (
2409 Make_Pragma_Argument_Association (Sloc (Expr),
2410 Expression => Relocate_Node (Expr)),
2411 Make_Pragma_Argument_Association (Loc,
2412 Expression => New_Occurrence_Of (E, Loc))),
2413 Pragma_Name => Chars (Id));
2415 Decorate (Aspect, Aitem);
2416 Insert_Pragma (Aitem);
2417 goto Continue;
2419 -- Case 2c: Aspects corresponding to pragmas with three
2420 -- arguments.
2422 -- Invariant aspects have a first argument that references the
2423 -- entity, a second argument that is the expression and a third
2424 -- argument that is an appropriate message.
2426 -- Invariant, Type_Invariant
2428 when Aspect_Invariant |
2429 Aspect_Type_Invariant =>
2431 -- Analysis of the pragma will verify placement legality:
2432 -- an invariant must apply to a private type, or appear in
2433 -- the private part of a spec and apply to a completion.
2435 Make_Aitem_Pragma
2436 (Pragma_Argument_Associations => New_List (
2437 Make_Pragma_Argument_Association (Sloc (Ent),
2438 Expression => Ent),
2439 Make_Pragma_Argument_Association (Sloc (Expr),
2440 Expression => Relocate_Node (Expr))),
2441 Pragma_Name => Name_Invariant);
2443 -- Add message unless exception messages are suppressed
2445 if not Opt.Exception_Locations_Suppressed then
2446 Append_To (Pragma_Argument_Associations (Aitem),
2447 Make_Pragma_Argument_Association (Eloc,
2448 Chars => Name_Message,
2449 Expression =>
2450 Make_String_Literal (Eloc,
2451 Strval => "failed invariant from "
2452 & Build_Location_String (Eloc))));
2453 end if;
2455 -- For Invariant case, insert immediately after the entity
2456 -- declaration. We do not have to worry about delay issues
2457 -- since the pragma processing takes care of this.
2459 Delay_Required := False;
2461 -- Case 2d : Aspects that correspond to a pragma with one
2462 -- argument.
2464 -- Abstract_State
2466 -- Aspect Abstract_State introduces implicit declarations for
2467 -- all state abstraction entities it defines. To emulate this
2468 -- behavior, insert the pragma at the beginning of the visible
2469 -- declarations of the related package so that it is analyzed
2470 -- immediately.
2472 when Aspect_Abstract_State => Abstract_State : declare
2473 Context : Node_Id := N;
2475 begin
2476 -- When aspect Abstract_State appears on a generic package,
2477 -- it is propageted to the package instance. The context in
2478 -- this case is the instance spec.
2480 if Nkind (Context) = N_Package_Instantiation then
2481 Context := Instance_Spec (Context);
2482 end if;
2484 if Nkind_In (Context, N_Generic_Package_Declaration,
2485 N_Package_Declaration)
2486 then
2487 Make_Aitem_Pragma
2488 (Pragma_Argument_Associations => New_List (
2489 Make_Pragma_Argument_Association (Loc,
2490 Expression => Relocate_Node (Expr))),
2491 Pragma_Name => Name_Abstract_State);
2493 Decorate (Aspect, Aitem);
2494 Insert_Pragma
2495 (Prag => Aitem,
2496 Is_Instance =>
2497 Is_Generic_Instance (Defining_Entity (Context)));
2499 else
2500 Error_Msg_NE
2501 ("aspect & must apply to a package declaration",
2502 Aspect, Id);
2503 end if;
2505 goto Continue;
2506 end Abstract_State;
2508 -- Aspect Async_Readers is never delayed because it is
2509 -- equivalent to a source pragma which appears after the
2510 -- related object declaration.
2512 when Aspect_Async_Readers =>
2513 Make_Aitem_Pragma
2514 (Pragma_Argument_Associations => New_List (
2515 Make_Pragma_Argument_Association (Loc,
2516 Expression => Relocate_Node (Expr))),
2517 Pragma_Name => Name_Async_Readers);
2519 Decorate (Aspect, Aitem);
2520 Insert_Pragma (Aitem);
2521 goto Continue;
2523 -- Aspect Async_Writers is never delayed because it is
2524 -- equivalent to a source pragma which appears after the
2525 -- related object declaration.
2527 when Aspect_Async_Writers =>
2528 Make_Aitem_Pragma
2529 (Pragma_Argument_Associations => New_List (
2530 Make_Pragma_Argument_Association (Loc,
2531 Expression => Relocate_Node (Expr))),
2532 Pragma_Name => Name_Async_Writers);
2534 Decorate (Aspect, Aitem);
2535 Insert_Pragma (Aitem);
2536 goto Continue;
2538 -- Aspect Constant_After_Elaboration is never delayed because
2539 -- it is equivalent to a source pragma which appears after the
2540 -- related object declaration.
2542 when Aspect_Constant_After_Elaboration =>
2543 Make_Aitem_Pragma
2544 (Pragma_Argument_Associations => New_List (
2545 Make_Pragma_Argument_Association (Loc,
2546 Expression => Relocate_Node (Expr))),
2547 Pragma_Name =>
2548 Name_Constant_After_Elaboration);
2550 Decorate (Aspect, Aitem);
2551 Insert_Pragma (Aitem);
2552 goto Continue;
2554 -- Aspect Default_Internal_Condition is never delayed because
2555 -- it is equivalent to a source pragma which appears after the
2556 -- related private type. To deal with forward references, the
2557 -- generated pragma is stored in the rep chain of the related
2558 -- private type as types do not carry contracts. The pragma is
2559 -- wrapped inside of a procedure at the freeze point of the
2560 -- private type's full view.
2562 when Aspect_Default_Initial_Condition =>
2563 Make_Aitem_Pragma
2564 (Pragma_Argument_Associations => New_List (
2565 Make_Pragma_Argument_Association (Loc,
2566 Expression => Relocate_Node (Expr))),
2567 Pragma_Name =>
2568 Name_Default_Initial_Condition);
2570 Decorate (Aspect, Aitem);
2571 Insert_Pragma (Aitem);
2572 goto Continue;
2574 -- Default_Storage_Pool
2576 when Aspect_Default_Storage_Pool =>
2577 Make_Aitem_Pragma
2578 (Pragma_Argument_Associations => New_List (
2579 Make_Pragma_Argument_Association (Loc,
2580 Expression => Relocate_Node (Expr))),
2581 Pragma_Name =>
2582 Name_Default_Storage_Pool);
2584 Decorate (Aspect, Aitem);
2585 Insert_Pragma (Aitem);
2586 goto Continue;
2588 -- Depends
2590 -- Aspect Depends is never delayed because it is equivalent to
2591 -- a source pragma which appears after the related subprogram.
2592 -- To deal with forward references, the generated pragma is
2593 -- stored in the contract of the related subprogram and later
2594 -- analyzed at the end of the declarative region. See routine
2595 -- Analyze_Depends_In_Decl_Part for details.
2597 when Aspect_Depends =>
2598 Make_Aitem_Pragma
2599 (Pragma_Argument_Associations => New_List (
2600 Make_Pragma_Argument_Association (Loc,
2601 Expression => Relocate_Node (Expr))),
2602 Pragma_Name => Name_Depends);
2604 Decorate (Aspect, Aitem);
2605 Insert_Pragma (Aitem);
2606 goto Continue;
2608 -- Aspect Effecitve_Reads is never delayed because it is
2609 -- equivalent to a source pragma which appears after the
2610 -- related object declaration.
2612 when Aspect_Effective_Reads =>
2613 Make_Aitem_Pragma
2614 (Pragma_Argument_Associations => New_List (
2615 Make_Pragma_Argument_Association (Loc,
2616 Expression => Relocate_Node (Expr))),
2617 Pragma_Name => Name_Effective_Reads);
2619 Decorate (Aspect, Aitem);
2620 Insert_Pragma (Aitem);
2621 goto Continue;
2623 -- Aspect Effective_Writes is never delayed because it is
2624 -- equivalent to a source pragma which appears after the
2625 -- related object declaration.
2627 when Aspect_Effective_Writes =>
2628 Make_Aitem_Pragma
2629 (Pragma_Argument_Associations => New_List (
2630 Make_Pragma_Argument_Association (Loc,
2631 Expression => Relocate_Node (Expr))),
2632 Pragma_Name => Name_Effective_Writes);
2634 Decorate (Aspect, Aitem);
2635 Insert_Pragma (Aitem);
2636 goto Continue;
2638 -- Aspect Extensions_Visible is never delayed because it is
2639 -- equivalent to a source pragma which appears after the
2640 -- related subprogram.
2642 when Aspect_Extensions_Visible =>
2643 Make_Aitem_Pragma
2644 (Pragma_Argument_Associations => New_List (
2645 Make_Pragma_Argument_Association (Loc,
2646 Expression => Relocate_Node (Expr))),
2647 Pragma_Name => Name_Extensions_Visible);
2649 Decorate (Aspect, Aitem);
2650 Insert_Pragma (Aitem);
2651 goto Continue;
2653 -- Aspect Ghost is never delayed because it is equivalent to a
2654 -- source pragma which appears at the top of [generic] package
2655 -- declarations or after an object, a [generic] subprogram, or
2656 -- a type declaration.
2658 when Aspect_Ghost =>
2659 Make_Aitem_Pragma
2660 (Pragma_Argument_Associations => New_List (
2661 Make_Pragma_Argument_Association (Loc,
2662 Expression => Relocate_Node (Expr))),
2663 Pragma_Name => Name_Ghost);
2665 Decorate (Aspect, Aitem);
2666 Insert_Pragma (Aitem);
2667 goto Continue;
2669 -- Global
2671 -- Aspect Global is never delayed because it is equivalent to
2672 -- a source pragma which appears after the related subprogram.
2673 -- To deal with forward references, the generated pragma is
2674 -- stored in the contract of the related subprogram and later
2675 -- analyzed at the end of the declarative region. See routine
2676 -- Analyze_Global_In_Decl_Part for details.
2678 when Aspect_Global =>
2679 Make_Aitem_Pragma
2680 (Pragma_Argument_Associations => New_List (
2681 Make_Pragma_Argument_Association (Loc,
2682 Expression => Relocate_Node (Expr))),
2683 Pragma_Name => Name_Global);
2685 Decorate (Aspect, Aitem);
2686 Insert_Pragma (Aitem);
2687 goto Continue;
2689 -- Initial_Condition
2691 -- Aspect Initial_Condition is never delayed because it is
2692 -- equivalent to a source pragma which appears after the
2693 -- related package. To deal with forward references, the
2694 -- generated pragma is stored in the contract of the related
2695 -- package and later analyzed at the end of the declarative
2696 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2697 -- for details.
2699 when Aspect_Initial_Condition => Initial_Condition : declare
2700 Context : Node_Id := N;
2702 begin
2703 -- When aspect Initial_Condition appears on a generic
2704 -- package, it is propageted to the package instance. The
2705 -- context in this case is the instance spec.
2707 if Nkind (Context) = N_Package_Instantiation then
2708 Context := Instance_Spec (Context);
2709 end if;
2711 if Nkind_In (Context, N_Generic_Package_Declaration,
2712 N_Package_Declaration)
2713 then
2714 Make_Aitem_Pragma
2715 (Pragma_Argument_Associations => New_List (
2716 Make_Pragma_Argument_Association (Loc,
2717 Expression => Relocate_Node (Expr))),
2718 Pragma_Name =>
2719 Name_Initial_Condition);
2721 Decorate (Aspect, Aitem);
2722 Insert_Pragma
2723 (Prag => Aitem,
2724 Is_Instance =>
2725 Is_Generic_Instance (Defining_Entity (Context)));
2727 -- Otherwise the context is illegal
2729 else
2730 Error_Msg_NE
2731 ("aspect & must apply to a package declaration",
2732 Aspect, Id);
2733 end if;
2735 goto Continue;
2736 end Initial_Condition;
2738 -- Initializes
2740 -- Aspect Initializes is never delayed because it is equivalent
2741 -- to a source pragma appearing after the related package. To
2742 -- deal with forward references, the generated pragma is stored
2743 -- in the contract of the related package and later analyzed at
2744 -- the end of the declarative region. For details, see routine
2745 -- Analyze_Initializes_In_Decl_Part.
2747 when Aspect_Initializes => Initializes : declare
2748 Context : Node_Id := N;
2750 begin
2751 -- When aspect Initializes appears on a generic package,
2752 -- it is propageted to the package instance. The context
2753 -- in this case is the instance spec.
2755 if Nkind (Context) = N_Package_Instantiation then
2756 Context := Instance_Spec (Context);
2757 end if;
2759 if Nkind_In (Context, N_Generic_Package_Declaration,
2760 N_Package_Declaration)
2761 then
2762 Make_Aitem_Pragma
2763 (Pragma_Argument_Associations => New_List (
2764 Make_Pragma_Argument_Association (Loc,
2765 Expression => Relocate_Node (Expr))),
2766 Pragma_Name => Name_Initializes);
2768 Decorate (Aspect, Aitem);
2769 Insert_Pragma
2770 (Prag => Aitem,
2771 Is_Instance =>
2772 Is_Generic_Instance (Defining_Entity (Context)));
2774 -- Otherwise the context is illegal
2776 else
2777 Error_Msg_NE
2778 ("aspect & must apply to a package declaration",
2779 Aspect, Id);
2780 end if;
2782 goto Continue;
2783 end Initializes;
2785 -- Obsolescent
2787 when Aspect_Obsolescent => declare
2788 Args : List_Id;
2790 begin
2791 if No (Expr) then
2792 Args := No_List;
2793 else
2794 Args := New_List (
2795 Make_Pragma_Argument_Association (Sloc (Expr),
2796 Expression => Relocate_Node (Expr)));
2797 end if;
2799 Make_Aitem_Pragma
2800 (Pragma_Argument_Associations => Args,
2801 Pragma_Name => Chars (Id));
2802 end;
2804 -- Part_Of
2806 when Aspect_Part_Of =>
2807 if Nkind_In (N, N_Object_Declaration,
2808 N_Package_Instantiation)
2809 or else Is_Single_Concurrent_Type_Declaration (N)
2810 then
2811 Make_Aitem_Pragma
2812 (Pragma_Argument_Associations => New_List (
2813 Make_Pragma_Argument_Association (Loc,
2814 Expression => Relocate_Node (Expr))),
2815 Pragma_Name => Name_Part_Of);
2817 Decorate (Aspect, Aitem);
2818 Insert_Pragma (Aitem);
2820 else
2821 Error_Msg_NE
2822 ("aspect & must apply to package instantiation, "
2823 & "object, single protected type or single task type",
2824 Aspect, Id);
2825 end if;
2827 goto Continue;
2829 -- SPARK_Mode
2831 when Aspect_SPARK_Mode =>
2832 Make_Aitem_Pragma
2833 (Pragma_Argument_Associations => New_List (
2834 Make_Pragma_Argument_Association (Loc,
2835 Expression => Relocate_Node (Expr))),
2836 Pragma_Name => Name_SPARK_Mode);
2838 Decorate (Aspect, Aitem);
2839 Insert_Pragma (Aitem);
2840 goto Continue;
2842 -- Refined_Depends
2844 -- Aspect Refined_Depends is never delayed because it is
2845 -- equivalent to a source pragma which appears in the
2846 -- declarations of the related subprogram body. To deal with
2847 -- forward references, the generated pragma is stored in the
2848 -- contract of the related subprogram body and later analyzed
2849 -- at the end of the declarative region. For details, see
2850 -- routine Analyze_Refined_Depends_In_Decl_Part.
2852 when Aspect_Refined_Depends =>
2853 Make_Aitem_Pragma
2854 (Pragma_Argument_Associations => New_List (
2855 Make_Pragma_Argument_Association (Loc,
2856 Expression => Relocate_Node (Expr))),
2857 Pragma_Name => Name_Refined_Depends);
2859 Decorate (Aspect, Aitem);
2860 Insert_Pragma (Aitem);
2861 goto Continue;
2863 -- Refined_Global
2865 -- Aspect Refined_Global is never delayed because it is
2866 -- equivalent to a source pragma which appears in the
2867 -- declarations of the related subprogram body. To deal with
2868 -- forward references, the generated pragma is stored in the
2869 -- contract of the related subprogram body and later analyzed
2870 -- at the end of the declarative region. For details, see
2871 -- routine Analyze_Refined_Global_In_Decl_Part.
2873 when Aspect_Refined_Global =>
2874 Make_Aitem_Pragma
2875 (Pragma_Argument_Associations => New_List (
2876 Make_Pragma_Argument_Association (Loc,
2877 Expression => Relocate_Node (Expr))),
2878 Pragma_Name => Name_Refined_Global);
2880 Decorate (Aspect, Aitem);
2881 Insert_Pragma (Aitem);
2882 goto Continue;
2884 -- Refined_Post
2886 when Aspect_Refined_Post =>
2887 Make_Aitem_Pragma
2888 (Pragma_Argument_Associations => New_List (
2889 Make_Pragma_Argument_Association (Loc,
2890 Expression => Relocate_Node (Expr))),
2891 Pragma_Name => Name_Refined_Post);
2893 Decorate (Aspect, Aitem);
2894 Insert_Pragma (Aitem);
2895 goto Continue;
2897 -- Refined_State
2899 when Aspect_Refined_State =>
2901 -- The corresponding pragma for Refined_State is inserted in
2902 -- the declarations of the related package body. This action
2903 -- synchronizes both the source and from-aspect versions of
2904 -- the pragma.
2906 if Nkind (N) = N_Package_Body then
2907 Make_Aitem_Pragma
2908 (Pragma_Argument_Associations => New_List (
2909 Make_Pragma_Argument_Association (Loc,
2910 Expression => Relocate_Node (Expr))),
2911 Pragma_Name => Name_Refined_State);
2913 Decorate (Aspect, Aitem);
2914 Insert_Pragma (Aitem);
2916 -- Otherwise the context is illegal
2918 else
2919 Error_Msg_NE
2920 ("aspect & must apply to a package body", Aspect, Id);
2921 end if;
2923 goto Continue;
2925 -- Relative_Deadline
2927 when Aspect_Relative_Deadline =>
2928 Make_Aitem_Pragma
2929 (Pragma_Argument_Associations => New_List (
2930 Make_Pragma_Argument_Association (Loc,
2931 Expression => Relocate_Node (Expr))),
2932 Pragma_Name => Name_Relative_Deadline);
2934 -- If the aspect applies to a task, the corresponding pragma
2935 -- must appear within its declarations, not after.
2937 if Nkind (N) = N_Task_Type_Declaration then
2938 declare
2939 Def : Node_Id;
2940 V : List_Id;
2942 begin
2943 if No (Task_Definition (N)) then
2944 Set_Task_Definition (N,
2945 Make_Task_Definition (Loc,
2946 Visible_Declarations => New_List,
2947 End_Label => Empty));
2948 end if;
2950 Def := Task_Definition (N);
2951 V := Visible_Declarations (Def);
2952 if not Is_Empty_List (V) then
2953 Insert_Before (First (V), Aitem);
2955 else
2956 Set_Visible_Declarations (Def, New_List (Aitem));
2957 end if;
2959 goto Continue;
2960 end;
2961 end if;
2963 -- Aspect Volatile_Function is never delayed because it is
2964 -- equivalent to a source pragma which appears after the
2965 -- related subprogram.
2967 when Aspect_Volatile_Function =>
2968 Make_Aitem_Pragma
2969 (Pragma_Argument_Associations => New_List (
2970 Make_Pragma_Argument_Association (Loc,
2971 Expression => Relocate_Node (Expr))),
2972 Pragma_Name => Name_Volatile_Function);
2974 Decorate (Aspect, Aitem);
2975 Insert_Pragma (Aitem);
2976 goto Continue;
2978 -- Case 2e: Annotate aspect
2980 when Aspect_Annotate =>
2981 declare
2982 Args : List_Id;
2983 Pargs : List_Id;
2984 Arg : Node_Id;
2986 begin
2987 -- The argument can be a single identifier
2989 if Nkind (Expr) = N_Identifier then
2991 -- One level of parens is allowed
2993 if Paren_Count (Expr) > 1 then
2994 Error_Msg_F ("extra parentheses ignored", Expr);
2995 end if;
2997 Set_Paren_Count (Expr, 0);
2999 -- Add the single item to the list
3001 Args := New_List (Expr);
3003 -- Otherwise we must have an aggregate
3005 elsif Nkind (Expr) = N_Aggregate then
3007 -- Must be positional
3009 if Present (Component_Associations (Expr)) then
3010 Error_Msg_F
3011 ("purely positional aggregate required", Expr);
3012 goto Continue;
3013 end if;
3015 -- Must not be parenthesized
3017 if Paren_Count (Expr) /= 0 then
3018 Error_Msg_F ("extra parentheses ignored", Expr);
3019 end if;
3021 -- List of arguments is list of aggregate expressions
3023 Args := Expressions (Expr);
3025 -- Anything else is illegal
3027 else
3028 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3029 goto Continue;
3030 end if;
3032 -- Prepare pragma arguments
3034 Pargs := New_List;
3035 Arg := First (Args);
3036 while Present (Arg) loop
3037 Append_To (Pargs,
3038 Make_Pragma_Argument_Association (Sloc (Arg),
3039 Expression => Relocate_Node (Arg)));
3040 Next (Arg);
3041 end loop;
3043 Append_To (Pargs,
3044 Make_Pragma_Argument_Association (Sloc (Ent),
3045 Chars => Name_Entity,
3046 Expression => Ent));
3048 Make_Aitem_Pragma
3049 (Pragma_Argument_Associations => Pargs,
3050 Pragma_Name => Name_Annotate);
3051 end;
3053 -- Case 3 : Aspects that don't correspond to pragma/attribute
3054 -- definition clause.
3056 -- Case 3a: The aspects listed below don't correspond to
3057 -- pragmas/attributes but do require delayed analysis.
3059 -- Default_Value can only apply to a scalar type
3061 when Aspect_Default_Value =>
3062 if not Is_Scalar_Type (E) then
3063 Error_Msg_N
3064 ("aspect Default_Value must apply to a scalar type", N);
3065 end if;
3067 Aitem := Empty;
3069 -- Default_Component_Value can only apply to an array type
3070 -- with scalar components.
3072 when Aspect_Default_Component_Value =>
3073 if not (Is_Array_Type (E)
3074 and then Is_Scalar_Type (Component_Type (E)))
3075 then
3076 Error_Msg_N
3077 ("aspect Default_Component_Value can only apply to an "
3078 & "array of scalar components", N);
3079 end if;
3081 Aitem := Empty;
3083 -- Case 3b: The aspects listed below don't correspond to
3084 -- pragmas/attributes and don't need delayed analysis.
3086 -- Implicit_Dereference
3088 -- For Implicit_Dereference, External_Name and Link_Name, only
3089 -- the legality checks are done during the analysis, thus no
3090 -- delay is required.
3092 when Aspect_Implicit_Dereference =>
3093 Analyze_Aspect_Implicit_Dereference;
3094 goto Continue;
3096 -- Dimension
3098 when Aspect_Dimension =>
3099 Analyze_Aspect_Dimension (N, Id, Expr);
3100 goto Continue;
3102 -- Dimension_System
3104 when Aspect_Dimension_System =>
3105 Analyze_Aspect_Dimension_System (N, Id, Expr);
3106 goto Continue;
3108 -- Case 4: Aspects requiring special handling
3110 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3111 -- pragmas take care of the delay.
3113 -- Pre/Post
3115 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3116 -- with a first argument that is the expression, and a second
3117 -- argument that is an informative message if the test fails.
3118 -- This is inserted right after the declaration, to get the
3119 -- required pragma placement. The processing for the pragmas
3120 -- takes care of the required delay.
3122 when Pre_Post_Aspects => Pre_Post : declare
3123 Pname : Name_Id;
3125 begin
3126 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
3127 Pname := Name_Precondition;
3128 else
3129 Pname := Name_Postcondition;
3130 end if;
3132 -- If the expressions is of the form A and then B, then
3133 -- we generate separate Pre/Post aspects for the separate
3134 -- clauses. Since we allow multiple pragmas, there is no
3135 -- problem in allowing multiple Pre/Post aspects internally.
3136 -- These should be treated in reverse order (B first and
3137 -- A second) since they are later inserted just after N in
3138 -- the order they are treated. This way, the pragma for A
3139 -- ends up preceding the pragma for B, which may have an
3140 -- importance for the error raised (either constraint error
3141 -- or precondition error).
3143 -- We do not do this for Pre'Class, since we have to put
3144 -- these conditions together in a complex OR expression.
3146 -- We do not do this in ASIS mode, as ASIS relies on the
3147 -- original node representing the complete expression, when
3148 -- retrieving it through the source aspect table.
3150 if not ASIS_Mode
3151 and then (Pname = Name_Postcondition
3152 or else not Class_Present (Aspect))
3153 then
3154 while Nkind (Expr) = N_And_Then loop
3155 Insert_After (Aspect,
3156 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
3157 Identifier => Identifier (Aspect),
3158 Expression => Relocate_Node (Left_Opnd (Expr)),
3159 Class_Present => Class_Present (Aspect),
3160 Split_PPC => True));
3161 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
3162 Eloc := Sloc (Expr);
3163 end loop;
3164 end if;
3166 -- Build the precondition/postcondition pragma
3168 -- Add note about why we do NOT need Copy_Tree here???
3170 Make_Aitem_Pragma
3171 (Pragma_Argument_Associations => New_List (
3172 Make_Pragma_Argument_Association (Eloc,
3173 Chars => Name_Check,
3174 Expression => Relocate_Node (Expr))),
3175 Pragma_Name => Pname);
3177 -- Add message unless exception messages are suppressed
3179 if not Opt.Exception_Locations_Suppressed then
3180 Append_To (Pragma_Argument_Associations (Aitem),
3181 Make_Pragma_Argument_Association (Eloc,
3182 Chars => Name_Message,
3183 Expression =>
3184 Make_String_Literal (Eloc,
3185 Strval => "failed "
3186 & Get_Name_String (Pname)
3187 & " from "
3188 & Build_Location_String (Eloc))));
3189 end if;
3191 Set_Is_Delayed_Aspect (Aspect);
3193 -- For Pre/Post cases, insert immediately after the entity
3194 -- declaration, since that is the required pragma placement.
3195 -- Note that for these aspects, we do not have to worry
3196 -- about delay issues, since the pragmas themselves deal
3197 -- with delay of visibility for the expression analysis.
3199 Insert_Pragma (Aitem);
3201 goto Continue;
3202 end Pre_Post;
3204 -- Test_Case
3206 when Aspect_Test_Case => Test_Case : declare
3207 Args : List_Id;
3208 Comp_Expr : Node_Id;
3209 Comp_Assn : Node_Id;
3210 New_Expr : Node_Id;
3212 begin
3213 Args := New_List;
3215 if Nkind (Parent (N)) = N_Compilation_Unit then
3216 Error_Msg_Name_1 := Nam;
3217 Error_Msg_N ("incorrect placement of aspect `%`", E);
3218 goto Continue;
3219 end if;
3221 if Nkind (Expr) /= N_Aggregate then
3222 Error_Msg_Name_1 := Nam;
3223 Error_Msg_NE
3224 ("wrong syntax for aspect `%` for &", Id, E);
3225 goto Continue;
3226 end if;
3228 -- Make pragma expressions refer to the original aspect
3229 -- expressions through the Original_Node link. This is used
3230 -- in semantic analysis for ASIS mode, so that the original
3231 -- expression also gets analyzed.
3233 Comp_Expr := First (Expressions (Expr));
3234 while Present (Comp_Expr) loop
3235 New_Expr := Relocate_Node (Comp_Expr);
3236 Append_To (Args,
3237 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3238 Expression => New_Expr));
3239 Next (Comp_Expr);
3240 end loop;
3242 Comp_Assn := First (Component_Associations (Expr));
3243 while Present (Comp_Assn) loop
3244 if List_Length (Choices (Comp_Assn)) /= 1
3245 or else
3246 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3247 then
3248 Error_Msg_Name_1 := Nam;
3249 Error_Msg_NE
3250 ("wrong syntax for aspect `%` for &", Id, E);
3251 goto Continue;
3252 end if;
3254 Append_To (Args,
3255 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3256 Chars => Chars (First (Choices (Comp_Assn))),
3257 Expression =>
3258 Relocate_Node (Expression (Comp_Assn))));
3259 Next (Comp_Assn);
3260 end loop;
3262 -- Build the test-case pragma
3264 Make_Aitem_Pragma
3265 (Pragma_Argument_Associations => Args,
3266 Pragma_Name => Nam);
3267 end Test_Case;
3269 -- Contract_Cases
3271 when Aspect_Contract_Cases =>
3272 Make_Aitem_Pragma
3273 (Pragma_Argument_Associations => New_List (
3274 Make_Pragma_Argument_Association (Loc,
3275 Expression => Relocate_Node (Expr))),
3276 Pragma_Name => Nam);
3278 Decorate (Aspect, Aitem);
3279 Insert_Pragma (Aitem);
3280 goto Continue;
3282 -- Case 5: Special handling for aspects with an optional
3283 -- boolean argument.
3285 -- In the delayed case, the corresponding pragma cannot be
3286 -- generated yet because the evaluation of the boolean needs
3287 -- to be delayed till the freeze point.
3289 when Boolean_Aspects |
3290 Library_Unit_Aspects =>
3292 Set_Is_Boolean_Aspect (Aspect);
3294 -- Lock_Free aspect only apply to protected objects
3296 if A_Id = Aspect_Lock_Free then
3297 if Ekind (E) /= E_Protected_Type then
3298 Error_Msg_Name_1 := Nam;
3299 Error_Msg_N
3300 ("aspect % only applies to a protected object",
3301 Aspect);
3303 else
3304 -- Set the Uses_Lock_Free flag to True if there is no
3305 -- expression or if the expression is True. The
3306 -- evaluation of this aspect should be delayed to the
3307 -- freeze point (why???)
3309 if No (Expr)
3310 or else Is_True (Static_Boolean (Expr))
3311 then
3312 Set_Uses_Lock_Free (E);
3313 end if;
3315 Record_Rep_Item (E, Aspect);
3316 end if;
3318 goto Continue;
3320 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3321 Analyze_Aspect_Export_Import;
3323 -- Disable_Controlled
3325 elsif A_Id = Aspect_Disable_Controlled then
3326 if Ekind (E) /= E_Record_Type
3327 or else not Is_Controlled (E)
3328 then
3329 Error_Msg_N
3330 ("aspect % requires controlled record type", Aspect);
3331 goto Continue;
3332 end if;
3334 -- If we're in a generic template, we don't want to try
3335 -- to disable controlled types, because typical usage is
3336 -- "Disable_Controlled => not <some_check>'Enabled", and
3337 -- the value of Enabled is not known until we see a
3338 -- particular instance. In such a context, we just need
3339 -- to preanalyze the expression for legality.
3341 if Expander_Active then
3342 Analyze_And_Resolve (Expr, Standard_Boolean);
3344 if not Present (Expr)
3345 or else Is_True (Static_Boolean (Expr))
3346 then
3347 Set_Disable_Controlled (E);
3348 end if;
3350 elsif Serious_Errors_Detected = 0 then
3351 Preanalyze_And_Resolve (Expr, Standard_Boolean);
3352 end if;
3354 goto Continue;
3355 end if;
3357 -- Library unit aspects require special handling in the case
3358 -- of a package declaration, the pragma needs to be inserted
3359 -- in the list of declarations for the associated package.
3360 -- There is no issue of visibility delay for these aspects.
3362 if A_Id in Library_Unit_Aspects
3363 and then
3364 Nkind_In (N, N_Package_Declaration,
3365 N_Generic_Package_Declaration)
3366 and then Nkind (Parent (N)) /= N_Compilation_Unit
3368 -- Aspect is legal on a local instantiation of a library-
3369 -- level generic unit.
3371 and then not Is_Generic_Instance (Defining_Entity (N))
3372 then
3373 Error_Msg_N
3374 ("incorrect context for library unit aspect&", Id);
3375 goto Continue;
3376 end if;
3378 -- Cases where we do not delay, includes all cases where the
3379 -- expression is missing other than the above cases.
3381 if not Delay_Required or else No (Expr) then
3383 -- Exclude aspects Export and Import because their pragma
3384 -- syntax does not map directly to a Boolean aspect.
3386 if A_Id /= Aspect_Export
3387 and then A_Id /= Aspect_Import
3388 then
3389 Make_Aitem_Pragma
3390 (Pragma_Argument_Associations => New_List (
3391 Make_Pragma_Argument_Association (Sloc (Ent),
3392 Expression => Ent)),
3393 Pragma_Name => Chars (Id));
3394 end if;
3396 Delay_Required := False;
3398 -- In general cases, the corresponding pragma/attribute
3399 -- definition clause will be inserted later at the freezing
3400 -- point, and we do not need to build it now.
3402 else
3403 Aitem := Empty;
3404 end if;
3406 -- Storage_Size
3408 -- This is special because for access types we need to generate
3409 -- an attribute definition clause. This also works for single
3410 -- task declarations, but it does not work for task type
3411 -- declarations, because we have the case where the expression
3412 -- references a discriminant of the task type. That can't use
3413 -- an attribute definition clause because we would not have
3414 -- visibility on the discriminant. For that case we must
3415 -- generate a pragma in the task definition.
3417 when Aspect_Storage_Size =>
3419 -- Task type case
3421 if Ekind (E) = E_Task_Type then
3422 declare
3423 Decl : constant Node_Id := Declaration_Node (E);
3425 begin
3426 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3428 -- If no task definition, create one
3430 if No (Task_Definition (Decl)) then
3431 Set_Task_Definition (Decl,
3432 Make_Task_Definition (Loc,
3433 Visible_Declarations => Empty_List,
3434 End_Label => Empty));
3435 end if;
3437 -- Create a pragma and put it at the start of the task
3438 -- definition for the task type declaration.
3440 Make_Aitem_Pragma
3441 (Pragma_Argument_Associations => New_List (
3442 Make_Pragma_Argument_Association (Loc,
3443 Expression => Relocate_Node (Expr))),
3444 Pragma_Name => Name_Storage_Size);
3446 Prepend
3447 (Aitem,
3448 Visible_Declarations (Task_Definition (Decl)));
3449 goto Continue;
3450 end;
3452 -- All other cases, generate attribute definition
3454 else
3455 Aitem :=
3456 Make_Attribute_Definition_Clause (Loc,
3457 Name => Ent,
3458 Chars => Chars (Id),
3459 Expression => Relocate_Node (Expr));
3460 end if;
3461 end case;
3463 -- Attach the corresponding pragma/attribute definition clause to
3464 -- the aspect specification node.
3466 if Present (Aitem) then
3467 Set_From_Aspect_Specification (Aitem);
3468 end if;
3470 -- In the context of a compilation unit, we directly put the
3471 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3472 -- node (no delay is required here) except for aspects on a
3473 -- subprogram body (see below) and a generic package, for which we
3474 -- need to introduce the pragma before building the generic copy
3475 -- (see sem_ch12), and for package instantiations, where the
3476 -- library unit pragmas are better handled early.
3478 if Nkind (Parent (N)) = N_Compilation_Unit
3479 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3480 then
3481 declare
3482 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3484 begin
3485 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3487 -- For a Boolean aspect, create the corresponding pragma if
3488 -- no expression or if the value is True.
3490 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3491 if Is_True (Static_Boolean (Expr)) then
3492 Make_Aitem_Pragma
3493 (Pragma_Argument_Associations => New_List (
3494 Make_Pragma_Argument_Association (Sloc (Ent),
3495 Expression => Ent)),
3496 Pragma_Name => Chars (Id));
3498 Set_From_Aspect_Specification (Aitem, True);
3499 Set_Corresponding_Aspect (Aitem, Aspect);
3501 else
3502 goto Continue;
3503 end if;
3504 end if;
3506 -- If the aspect is on a subprogram body (relevant aspect
3507 -- is Inline), add the pragma in front of the declarations.
3509 if Nkind (N) = N_Subprogram_Body then
3510 if No (Declarations (N)) then
3511 Set_Declarations (N, New_List);
3512 end if;
3514 Prepend (Aitem, Declarations (N));
3516 elsif Nkind (N) = N_Generic_Package_Declaration then
3517 if No (Visible_Declarations (Specification (N))) then
3518 Set_Visible_Declarations (Specification (N), New_List);
3519 end if;
3521 Prepend (Aitem,
3522 Visible_Declarations (Specification (N)));
3524 elsif Nkind (N) = N_Package_Instantiation then
3525 declare
3526 Spec : constant Node_Id :=
3527 Specification (Instance_Spec (N));
3528 begin
3529 if No (Visible_Declarations (Spec)) then
3530 Set_Visible_Declarations (Spec, New_List);
3531 end if;
3533 Prepend (Aitem, Visible_Declarations (Spec));
3534 end;
3536 else
3537 if No (Pragmas_After (Aux)) then
3538 Set_Pragmas_After (Aux, New_List);
3539 end if;
3541 Append (Aitem, Pragmas_After (Aux));
3542 end if;
3544 goto Continue;
3545 end;
3546 end if;
3548 -- The evaluation of the aspect is delayed to the freezing point.
3549 -- The pragma or attribute clause if there is one is then attached
3550 -- to the aspect specification which is put in the rep item list.
3552 if Delay_Required then
3553 if Present (Aitem) then
3554 Set_Is_Delayed_Aspect (Aitem);
3555 Set_Aspect_Rep_Item (Aspect, Aitem);
3556 Set_Parent (Aitem, Aspect);
3557 end if;
3559 Set_Is_Delayed_Aspect (Aspect);
3561 -- In the case of Default_Value, link the aspect to base type
3562 -- as well, even though it appears on a first subtype. This is
3563 -- mandated by the semantics of the aspect. Do not establish
3564 -- the link when processing the base type itself as this leads
3565 -- to a rep item circularity. Verify that we are dealing with
3566 -- a scalar type to prevent cascaded errors.
3568 if A_Id = Aspect_Default_Value
3569 and then Is_Scalar_Type (E)
3570 and then Base_Type (E) /= E
3571 then
3572 Set_Has_Delayed_Aspects (Base_Type (E));
3573 Record_Rep_Item (Base_Type (E), Aspect);
3574 end if;
3576 Set_Has_Delayed_Aspects (E);
3577 Record_Rep_Item (E, Aspect);
3579 -- When delay is not required and the context is a package or a
3580 -- subprogram body, insert the pragma in the body declarations.
3582 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3583 if No (Declarations (N)) then
3584 Set_Declarations (N, New_List);
3585 end if;
3587 -- The pragma is added before source declarations
3589 Prepend_To (Declarations (N), Aitem);
3591 -- When delay is not required and the context is not a compilation
3592 -- unit, we simply insert the pragma/attribute definition clause
3593 -- in sequence.
3595 elsif Present (Aitem) then
3596 Insert_After (Ins_Node, Aitem);
3597 Ins_Node := Aitem;
3598 end if;
3599 end Analyze_One_Aspect;
3601 <<Continue>>
3602 Next (Aspect);
3603 end loop Aspect_Loop;
3605 if Has_Delayed_Aspects (E) then
3606 Ensure_Freeze_Node (E);
3607 end if;
3608 end Analyze_Aspect_Specifications;
3610 ---------------------------------------------------
3611 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3612 ---------------------------------------------------
3614 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3615 Body_Id : constant Entity_Id := Defining_Entity (N);
3617 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3618 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3619 -- error message depending on the aspects involved. Spec_Id denotes the
3620 -- entity of the corresponding spec.
3622 --------------------------------
3623 -- Diagnose_Misplaced_Aspects --
3624 --------------------------------
3626 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3627 procedure Misplaced_Aspect_Error
3628 (Asp : Node_Id;
3629 Ref_Nam : Name_Id);
3630 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3631 -- the name of the refined version of the aspect.
3633 ----------------------------
3634 -- Misplaced_Aspect_Error --
3635 ----------------------------
3637 procedure Misplaced_Aspect_Error
3638 (Asp : Node_Id;
3639 Ref_Nam : Name_Id)
3641 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3642 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3644 begin
3645 -- The corresponding spec already contains the aspect in question
3646 -- and the one appearing on the body must be the refined form:
3648 -- procedure P with Global ...;
3649 -- procedure P with Global ... is ... end P;
3650 -- ^
3651 -- Refined_Global
3653 if Has_Aspect (Spec_Id, Asp_Id) then
3654 Error_Msg_Name_1 := Asp_Nam;
3656 -- Subunits cannot carry aspects that apply to a subprogram
3657 -- declaration.
3659 if Nkind (Parent (N)) = N_Subunit then
3660 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3662 -- Otherwise suggest the refined form
3664 else
3665 Error_Msg_Name_2 := Ref_Nam;
3666 Error_Msg_N ("aspect % should be %", Asp);
3667 end if;
3669 -- Otherwise the aspect must appear on the spec, not on the body
3671 -- procedure P;
3672 -- procedure P with Global ... is ... end P;
3674 else
3675 Error_Msg_N
3676 ("aspect specification must appear on initial declaration",
3677 Asp);
3678 end if;
3679 end Misplaced_Aspect_Error;
3681 -- Local variables
3683 Asp : Node_Id;
3684 Asp_Nam : Name_Id;
3686 -- Start of processing for Diagnose_Misplaced_Aspects
3688 begin
3689 -- Iterate over the aspect specifications and emit specific errors
3690 -- where applicable.
3692 Asp := First (Aspect_Specifications (N));
3693 while Present (Asp) loop
3694 Asp_Nam := Chars (Identifier (Asp));
3696 -- Do not emit errors on aspects that can appear on a subprogram
3697 -- body. This scenario occurs when the aspect specification list
3698 -- contains both misplaced and properly placed aspects.
3700 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3701 null;
3703 -- Special diagnostics for SPARK aspects
3705 elsif Asp_Nam = Name_Depends then
3706 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3708 elsif Asp_Nam = Name_Global then
3709 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3711 elsif Asp_Nam = Name_Post then
3712 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3714 -- Otherwise a language-defined aspect is misplaced
3716 else
3717 Error_Msg_N
3718 ("aspect specification must appear on initial declaration",
3719 Asp);
3720 end if;
3722 Next (Asp);
3723 end loop;
3724 end Diagnose_Misplaced_Aspects;
3726 -- Local variables
3728 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
3730 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3732 begin
3733 -- Language-defined aspects cannot be associated with a subprogram body
3734 -- [stub] if the subprogram has a spec. Certain implementation defined
3735 -- aspects are allowed to break this rule (for all applicable cases, see
3736 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3738 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
3739 Diagnose_Misplaced_Aspects (Spec_Id);
3740 else
3741 Analyze_Aspect_Specifications (N, Body_Id);
3742 end if;
3743 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3745 -----------------------
3746 -- Analyze_At_Clause --
3747 -----------------------
3749 -- An at clause is replaced by the corresponding Address attribute
3750 -- definition clause that is the preferred approach in Ada 95.
3752 procedure Analyze_At_Clause (N : Node_Id) is
3753 CS : constant Boolean := Comes_From_Source (N);
3755 begin
3756 -- This is an obsolescent feature
3758 Check_Restriction (No_Obsolescent_Features, N);
3760 if Warn_On_Obsolescent_Feature then
3761 Error_Msg_N
3762 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3763 Error_Msg_N
3764 ("\?j?use address attribute definition clause instead", N);
3765 end if;
3767 -- Rewrite as address clause
3769 Rewrite (N,
3770 Make_Attribute_Definition_Clause (Sloc (N),
3771 Name => Identifier (N),
3772 Chars => Name_Address,
3773 Expression => Expression (N)));
3775 -- We preserve Comes_From_Source, since logically the clause still comes
3776 -- from the source program even though it is changed in form.
3778 Set_Comes_From_Source (N, CS);
3780 -- Analyze rewritten clause
3782 Analyze_Attribute_Definition_Clause (N);
3783 end Analyze_At_Clause;
3785 -----------------------------------------
3786 -- Analyze_Attribute_Definition_Clause --
3787 -----------------------------------------
3789 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3790 Loc : constant Source_Ptr := Sloc (N);
3791 Nam : constant Node_Id := Name (N);
3792 Attr : constant Name_Id := Chars (N);
3793 Expr : constant Node_Id := Expression (N);
3794 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3796 Ent : Entity_Id;
3797 -- The entity of Nam after it is analyzed. In the case of an incomplete
3798 -- type, this is the underlying type.
3800 U_Ent : Entity_Id;
3801 -- The underlying entity to which the attribute applies. Generally this
3802 -- is the Underlying_Type of Ent, except in the case where the clause
3803 -- applies to full view of incomplete type or private type in which case
3804 -- U_Ent is just a copy of Ent.
3806 FOnly : Boolean := False;
3807 -- Reset to True for subtype specific attribute (Alignment, Size)
3808 -- and for stream attributes, i.e. those cases where in the call to
3809 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3810 -- are checked. Note that the case of stream attributes is not clear
3811 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3812 -- Storage_Size for derived task types, but that is also clearly
3813 -- unintentional.
3815 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3816 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3817 -- definition clauses.
3819 function Duplicate_Clause return Boolean;
3820 -- This routine checks if the aspect for U_Ent being given by attribute
3821 -- definition clause N is for an aspect that has already been specified,
3822 -- and if so gives an error message. If there is a duplicate, True is
3823 -- returned, otherwise if there is no error, False is returned.
3825 procedure Check_Indexing_Functions;
3826 -- Check that the function in Constant_Indexing or Variable_Indexing
3827 -- attribute has the proper type structure. If the name is overloaded,
3828 -- check that some interpretation is legal.
3830 procedure Check_Iterator_Functions;
3831 -- Check that there is a single function in Default_Iterator attribute
3832 -- has the proper type structure.
3834 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3835 -- Common legality check for the previous two
3837 -----------------------------------
3838 -- Analyze_Stream_TSS_Definition --
3839 -----------------------------------
3841 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3842 Subp : Entity_Id := Empty;
3843 I : Interp_Index;
3844 It : Interp;
3845 Pnam : Entity_Id;
3847 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3848 -- True for Read attribute, False for other attributes
3850 function Has_Good_Profile
3851 (Subp : Entity_Id;
3852 Report : Boolean := False) return Boolean;
3853 -- Return true if the entity is a subprogram with an appropriate
3854 -- profile for the attribute being defined. If result is False and
3855 -- Report is True, function emits appropriate error.
3857 ----------------------
3858 -- Has_Good_Profile --
3859 ----------------------
3861 function Has_Good_Profile
3862 (Subp : Entity_Id;
3863 Report : Boolean := False) return Boolean
3865 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3866 (False => E_Procedure, True => E_Function);
3867 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3868 F : Entity_Id;
3869 Typ : Entity_Id;
3871 begin
3872 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3873 return False;
3874 end if;
3876 F := First_Formal (Subp);
3878 if No (F)
3879 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3880 or else Designated_Type (Etype (F)) /=
3881 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3882 then
3883 return False;
3884 end if;
3886 if not Is_Function then
3887 Next_Formal (F);
3889 declare
3890 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3891 (False => E_In_Parameter,
3892 True => E_Out_Parameter);
3893 begin
3894 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3895 return False;
3896 end if;
3897 end;
3899 Typ := Etype (F);
3901 -- If the attribute specification comes from an aspect
3902 -- specification for a class-wide stream, the parameter must be
3903 -- a class-wide type of the entity to which the aspect applies.
3905 if From_Aspect_Specification (N)
3906 and then Class_Present (Parent (N))
3907 and then Is_Class_Wide_Type (Typ)
3908 then
3909 Typ := Etype (Typ);
3910 end if;
3912 else
3913 Typ := Etype (Subp);
3914 end if;
3916 -- Verify that the prefix of the attribute and the local name for
3917 -- the type of the formal match, or one is the class-wide of the
3918 -- other, in the case of a class-wide stream operation.
3920 if Base_Type (Typ) = Base_Type (Ent)
3921 or else (Is_Class_Wide_Type (Typ)
3922 and then Typ = Class_Wide_Type (Base_Type (Ent)))
3923 or else (Is_Class_Wide_Type (Ent)
3924 and then Ent = Class_Wide_Type (Base_Type (Typ)))
3925 then
3926 null;
3927 else
3928 return False;
3929 end if;
3931 if Present (Next_Formal (F)) then
3932 return False;
3934 elsif not Is_Scalar_Type (Typ)
3935 and then not Is_First_Subtype (Typ)
3936 and then not Is_Class_Wide_Type (Typ)
3937 then
3938 if Report and not Is_First_Subtype (Typ) then
3939 Error_Msg_N
3940 ("subtype of formal in stream operation must be a first "
3941 & "subtype", Parameter_Type (Parent (F)));
3942 end if;
3944 return False;
3946 else
3947 return True;
3948 end if;
3949 end Has_Good_Profile;
3951 -- Start of processing for Analyze_Stream_TSS_Definition
3953 begin
3954 FOnly := True;
3956 if not Is_Type (U_Ent) then
3957 Error_Msg_N ("local name must be a subtype", Nam);
3958 return;
3960 elsif not Is_First_Subtype (U_Ent) then
3961 Error_Msg_N ("local name must be a first subtype", Nam);
3962 return;
3963 end if;
3965 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3967 -- If Pnam is present, it can be either inherited from an ancestor
3968 -- type (in which case it is legal to redefine it for this type), or
3969 -- be a previous definition of the attribute for the same type (in
3970 -- which case it is illegal).
3972 -- In the first case, it will have been analyzed already, and we
3973 -- can check that its profile does not match the expected profile
3974 -- for a stream attribute of U_Ent. In the second case, either Pnam
3975 -- has been analyzed (and has the expected profile), or it has not
3976 -- been analyzed yet (case of a type that has not been frozen yet
3977 -- and for which the stream attribute has been set using Set_TSS).
3979 if Present (Pnam)
3980 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3981 then
3982 Error_Msg_Sloc := Sloc (Pnam);
3983 Error_Msg_Name_1 := Attr;
3984 Error_Msg_N ("% attribute already defined #", Nam);
3985 return;
3986 end if;
3988 Analyze (Expr);
3990 if Is_Entity_Name (Expr) then
3991 if not Is_Overloaded (Expr) then
3992 if Has_Good_Profile (Entity (Expr), Report => True) then
3993 Subp := Entity (Expr);
3994 end if;
3996 else
3997 Get_First_Interp (Expr, I, It);
3998 while Present (It.Nam) loop
3999 if Has_Good_Profile (It.Nam) then
4000 Subp := It.Nam;
4001 exit;
4002 end if;
4004 Get_Next_Interp (I, It);
4005 end loop;
4006 end if;
4007 end if;
4009 if Present (Subp) then
4010 if Is_Abstract_Subprogram (Subp) then
4011 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4012 return;
4014 -- A stream subprogram for an interface type must be a null
4015 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4016 -- of an interface is not an interface type (3.9.4 (6.b/2)).
4018 elsif Is_Interface (U_Ent)
4019 and then not Is_Class_Wide_Type (U_Ent)
4020 and then not Inside_A_Generic
4021 and then
4022 (Ekind (Subp) = E_Function
4023 or else
4024 not Null_Present
4025 (Specification
4026 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
4027 then
4028 Error_Msg_N
4029 ("stream subprogram for interface type must be null "
4030 & "procedure", Expr);
4031 end if;
4033 Set_Entity (Expr, Subp);
4034 Set_Etype (Expr, Etype (Subp));
4036 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
4038 else
4039 Error_Msg_Name_1 := Attr;
4040 Error_Msg_N ("incorrect expression for% attribute", Expr);
4041 end if;
4042 end Analyze_Stream_TSS_Definition;
4044 ------------------------------
4045 -- Check_Indexing_Functions --
4046 ------------------------------
4048 procedure Check_Indexing_Functions is
4049 Indexing_Found : Boolean := False;
4051 procedure Check_Inherited_Indexing;
4052 -- For a derived type, check that no indexing aspect is specified
4053 -- for the type if it is also inherited
4055 procedure Check_One_Function (Subp : Entity_Id);
4056 -- Check one possible interpretation. Sets Indexing_Found True if a
4057 -- legal indexing function is found.
4059 procedure Illegal_Indexing (Msg : String);
4060 -- Diagnose illegal indexing function if not overloaded. In the
4061 -- overloaded case indicate that no legal interpretation exists.
4063 ------------------------------
4064 -- Check_Inherited_Indexing --
4065 ------------------------------
4067 procedure Check_Inherited_Indexing is
4068 Inherited : Node_Id;
4070 begin
4071 if Attr = Name_Constant_Indexing then
4072 Inherited :=
4073 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4074 else pragma Assert (Attr = Name_Variable_Indexing);
4075 Inherited :=
4076 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4077 end if;
4079 if Present (Inherited) then
4080 if Debug_Flag_Dot_XX then
4081 null;
4083 -- OK if current attribute_definition_clause is expansion of
4084 -- inherited aspect.
4086 elsif Aspect_Rep_Item (Inherited) = N then
4087 null;
4089 -- Indicate the operation that must be overridden, rather than
4090 -- redefining the indexing aspect.
4092 else
4093 Illegal_Indexing
4094 ("indexing function already inherited from parent type");
4095 Error_Msg_NE
4096 ("!override & instead",
4097 N, Entity (Expression (Inherited)));
4098 end if;
4099 end if;
4100 end Check_Inherited_Indexing;
4102 ------------------------
4103 -- Check_One_Function --
4104 ------------------------
4106 procedure Check_One_Function (Subp : Entity_Id) is
4107 Default_Element : Node_Id;
4108 Ret_Type : constant Entity_Id := Etype (Subp);
4110 begin
4111 if not Is_Overloadable (Subp) then
4112 Illegal_Indexing ("illegal indexing function for type&");
4113 return;
4115 elsif Scope (Subp) /= Scope (Ent) then
4116 if Nkind (Expr) = N_Expanded_Name then
4118 -- Indexing function can't be declared elsewhere
4120 Illegal_Indexing
4121 ("indexing function must be declared in scope of type&");
4122 end if;
4124 return;
4126 elsif No (First_Formal (Subp)) then
4127 Illegal_Indexing
4128 ("Indexing requires a function that applies to type&");
4129 return;
4131 elsif No (Next_Formal (First_Formal (Subp))) then
4132 Illegal_Indexing
4133 ("indexing function must have at least two parameters");
4134 return;
4136 elsif Is_Derived_Type (Ent) then
4137 Check_Inherited_Indexing;
4138 end if;
4140 if not Check_Primitive_Function (Subp) then
4141 Illegal_Indexing
4142 ("Indexing aspect requires a function that applies to type&");
4143 return;
4144 end if;
4146 -- If partial declaration exists, verify that it is not tagged.
4148 if Ekind (Current_Scope) = E_Package
4149 and then Has_Private_Declaration (Ent)
4150 and then From_Aspect_Specification (N)
4151 and then
4152 List_Containing (Parent (Ent)) =
4153 Private_Declarations
4154 (Specification (Unit_Declaration_Node (Current_Scope)))
4155 and then Nkind (N) = N_Attribute_Definition_Clause
4156 then
4157 declare
4158 Decl : Node_Id;
4160 begin
4161 Decl :=
4162 First (Visible_Declarations
4163 (Specification
4164 (Unit_Declaration_Node (Current_Scope))));
4166 while Present (Decl) loop
4167 if Nkind (Decl) = N_Private_Type_Declaration
4168 and then Ent = Full_View (Defining_Identifier (Decl))
4169 and then Tagged_Present (Decl)
4170 and then No (Aspect_Specifications (Decl))
4171 then
4172 Illegal_Indexing
4173 ("Indexing aspect cannot be specified on full view "
4174 & "if partial view is tagged");
4175 return;
4176 end if;
4178 Next (Decl);
4179 end loop;
4180 end;
4181 end if;
4183 -- An indexing function must return either the default element of
4184 -- the container, or a reference type. For variable indexing it
4185 -- must be the latter.
4187 Default_Element :=
4188 Find_Value_Of_Aspect
4189 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4191 if Present (Default_Element) then
4192 Analyze (Default_Element);
4194 if Is_Entity_Name (Default_Element)
4195 and then not Covers (Entity (Default_Element), Ret_Type)
4196 and then False
4197 then
4198 Illegal_Indexing
4199 ("wrong return type for indexing function");
4200 return;
4201 end if;
4202 end if;
4204 -- For variable_indexing the return type must be a reference type
4206 if Attr = Name_Variable_Indexing then
4207 if not Has_Implicit_Dereference (Ret_Type) then
4208 Illegal_Indexing
4209 ("variable indexing must return a reference type");
4210 return;
4212 elsif Is_Access_Constant
4213 (Etype (First_Discriminant (Ret_Type)))
4214 then
4215 Illegal_Indexing
4216 ("variable indexing must return an access to variable");
4217 return;
4218 end if;
4220 else
4221 if Has_Implicit_Dereference (Ret_Type)
4222 and then not
4223 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4224 then
4225 Illegal_Indexing
4226 ("constant indexing must return an access to constant");
4227 return;
4229 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4230 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4231 then
4232 Illegal_Indexing
4233 ("constant indexing must apply to an access to constant");
4234 return;
4235 end if;
4236 end if;
4238 -- All checks succeeded.
4240 Indexing_Found := True;
4241 end Check_One_Function;
4243 -----------------------
4244 -- Illegal_Indexing --
4245 -----------------------
4247 procedure Illegal_Indexing (Msg : String) is
4248 begin
4249 Error_Msg_NE (Msg, N, Ent);
4250 end Illegal_Indexing;
4252 -- Start of processing for Check_Indexing_Functions
4254 begin
4255 if In_Instance then
4256 Check_Inherited_Indexing;
4257 end if;
4259 Analyze (Expr);
4261 if not Is_Overloaded (Expr) then
4262 Check_One_Function (Entity (Expr));
4264 else
4265 declare
4266 I : Interp_Index;
4267 It : Interp;
4269 begin
4270 Indexing_Found := False;
4271 Get_First_Interp (Expr, I, It);
4272 while Present (It.Nam) loop
4274 -- Note that analysis will have added the interpretation
4275 -- that corresponds to the dereference. We only check the
4276 -- subprogram itself.
4278 if Is_Overloadable (It.Nam) then
4279 Check_One_Function (It.Nam);
4280 end if;
4282 Get_Next_Interp (I, It);
4283 end loop;
4284 end;
4285 end if;
4287 if not Indexing_Found and then not Error_Posted (N) then
4288 Error_Msg_NE
4289 ("aspect Indexing requires a local function that "
4290 & "applies to type&", Expr, Ent);
4291 end if;
4292 end Check_Indexing_Functions;
4294 ------------------------------
4295 -- Check_Iterator_Functions --
4296 ------------------------------
4298 procedure Check_Iterator_Functions is
4299 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
4300 -- Check one possible interpretation for validity
4302 ----------------------------
4303 -- Valid_Default_Iterator --
4304 ----------------------------
4306 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4307 Formal : Entity_Id;
4309 begin
4310 if not Check_Primitive_Function (Subp) then
4311 return False;
4312 else
4313 Formal := First_Formal (Subp);
4314 end if;
4316 -- False if any subsequent formal has no default expression
4318 Formal := Next_Formal (Formal);
4319 while Present (Formal) loop
4320 if No (Expression (Parent (Formal))) then
4321 return False;
4322 end if;
4324 Next_Formal (Formal);
4325 end loop;
4327 -- True if all subsequent formals have default expressions
4329 return True;
4330 end Valid_Default_Iterator;
4332 -- Start of processing for Check_Iterator_Functions
4334 begin
4335 Analyze (Expr);
4337 if not Is_Entity_Name (Expr) then
4338 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4339 end if;
4341 if not Is_Overloaded (Expr) then
4342 if not Check_Primitive_Function (Entity (Expr)) then
4343 Error_Msg_NE
4344 ("aspect Indexing requires a function that applies to type&",
4345 Entity (Expr), Ent);
4346 end if;
4348 -- Flag the default_iterator as well as the denoted function.
4350 if not Valid_Default_Iterator (Entity (Expr)) then
4351 Error_Msg_N ("improper function for default iterator!", Expr);
4352 end if;
4354 else
4355 declare
4356 Default : Entity_Id := Empty;
4357 I : Interp_Index;
4358 It : Interp;
4360 begin
4361 Get_First_Interp (Expr, I, It);
4362 while Present (It.Nam) loop
4363 if not Check_Primitive_Function (It.Nam)
4364 or else not Valid_Default_Iterator (It.Nam)
4365 then
4366 Remove_Interp (I);
4368 elsif Present (Default) then
4370 -- An explicit one should override an implicit one
4372 if Comes_From_Source (Default) =
4373 Comes_From_Source (It.Nam)
4374 then
4375 Error_Msg_N ("default iterator must be unique", Expr);
4376 Error_Msg_Sloc := Sloc (Default);
4377 Error_Msg_N ("\\possible interpretation#", Expr);
4378 Error_Msg_Sloc := Sloc (It.Nam);
4379 Error_Msg_N ("\\possible interpretation#", Expr);
4381 elsif Comes_From_Source (It.Nam) then
4382 Default := It.Nam;
4383 end if;
4384 else
4385 Default := It.Nam;
4386 end if;
4388 Get_Next_Interp (I, It);
4389 end loop;
4391 if Present (Default) then
4392 Set_Entity (Expr, Default);
4393 Set_Is_Overloaded (Expr, False);
4394 end if;
4395 end;
4396 end if;
4397 end Check_Iterator_Functions;
4399 -------------------------------
4400 -- Check_Primitive_Function --
4401 -------------------------------
4403 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4404 Ctrl : Entity_Id;
4406 begin
4407 if Ekind (Subp) /= E_Function then
4408 return False;
4409 end if;
4411 if No (First_Formal (Subp)) then
4412 return False;
4413 else
4414 Ctrl := Etype (First_Formal (Subp));
4415 end if;
4417 -- To be a primitive operation subprogram has to be in same scope.
4419 if Scope (Ctrl) /= Scope (Subp) then
4420 return False;
4421 end if;
4423 -- Type of formal may be the class-wide type, an access to such,
4424 -- or an incomplete view.
4426 if Ctrl = Ent
4427 or else Ctrl = Class_Wide_Type (Ent)
4428 or else
4429 (Ekind (Ctrl) = E_Anonymous_Access_Type
4430 and then (Designated_Type (Ctrl) = Ent
4431 or else
4432 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4433 or else
4434 (Ekind (Ctrl) = E_Incomplete_Type
4435 and then Full_View (Ctrl) = Ent)
4436 then
4437 null;
4438 else
4439 return False;
4440 end if;
4442 return True;
4443 end Check_Primitive_Function;
4445 ----------------------
4446 -- Duplicate_Clause --
4447 ----------------------
4449 function Duplicate_Clause return Boolean is
4450 A : Node_Id;
4452 begin
4453 -- Nothing to do if this attribute definition clause comes from
4454 -- an aspect specification, since we could not be duplicating an
4455 -- explicit clause, and we dealt with the case of duplicated aspects
4456 -- in Analyze_Aspect_Specifications.
4458 if From_Aspect_Specification (N) then
4459 return False;
4460 end if;
4462 -- Otherwise current clause may duplicate previous clause, or a
4463 -- previously given pragma or aspect specification for the same
4464 -- aspect.
4466 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4468 if Present (A) then
4469 Error_Msg_Name_1 := Chars (N);
4470 Error_Msg_Sloc := Sloc (A);
4472 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4473 return True;
4474 end if;
4476 return False;
4477 end Duplicate_Clause;
4479 -- Start of processing for Analyze_Attribute_Definition_Clause
4481 begin
4482 -- The following code is a defense against recursion. Not clear that
4483 -- this can happen legitimately, but perhaps some error situations can
4484 -- cause it, and we did see this recursion during testing.
4486 if Analyzed (N) then
4487 return;
4488 else
4489 Set_Analyzed (N, True);
4490 end if;
4492 Check_Restriction_No_Use_Of_Attribute (N);
4494 -- Ignore some selected attributes in CodePeer mode since they are not
4495 -- relevant in this context.
4497 if CodePeer_Mode then
4498 case Id is
4500 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4501 -- internal representation of types by implicitly packing them.
4503 when Attribute_Component_Size =>
4504 Rewrite (N, Make_Null_Statement (Sloc (N)));
4505 return;
4507 when others =>
4508 null;
4509 end case;
4510 end if;
4512 -- Process Ignore_Rep_Clauses option
4514 if Ignore_Rep_Clauses then
4515 case Id is
4517 -- The following should be ignored. They do not affect legality
4518 -- and may be target dependent. The basic idea of -gnatI is to
4519 -- ignore any rep clauses that may be target dependent but do not
4520 -- affect legality (except possibly to be rejected because they
4521 -- are incompatible with the compilation target).
4523 when Attribute_Alignment |
4524 Attribute_Bit_Order |
4525 Attribute_Component_Size |
4526 Attribute_Machine_Radix |
4527 Attribute_Object_Size |
4528 Attribute_Size |
4529 Attribute_Small |
4530 Attribute_Stream_Size |
4531 Attribute_Value_Size =>
4532 Kill_Rep_Clause (N);
4533 return;
4535 -- The following should not be ignored, because in the first place
4536 -- they are reasonably portable, and should not cause problems
4537 -- in compiling code from another target, and also they do affect
4538 -- legality, e.g. failing to provide a stream attribute for a type
4539 -- may make a program illegal.
4541 when Attribute_External_Tag |
4542 Attribute_Input |
4543 Attribute_Output |
4544 Attribute_Read |
4545 Attribute_Simple_Storage_Pool |
4546 Attribute_Storage_Pool |
4547 Attribute_Storage_Size |
4548 Attribute_Write =>
4549 null;
4551 -- We do not do anything here with address clauses, they will be
4552 -- removed by Freeze later on, but for now, it works better to
4553 -- keep then in the tree.
4555 when Attribute_Address =>
4556 null;
4558 -- Other cases are errors ("attribute& cannot be set with
4559 -- definition clause"), which will be caught below.
4561 when others =>
4562 null;
4563 end case;
4564 end if;
4566 Analyze (Nam);
4567 Ent := Entity (Nam);
4569 if Rep_Item_Too_Early (Ent, N) then
4570 return;
4571 end if;
4573 -- Rep clause applies to full view of incomplete type or private type if
4574 -- we have one (if not, this is a premature use of the type). However,
4575 -- certain semantic checks need to be done on the specified entity (i.e.
4576 -- the private view), so we save it in Ent.
4578 if Is_Private_Type (Ent)
4579 and then Is_Derived_Type (Ent)
4580 and then not Is_Tagged_Type (Ent)
4581 and then No (Full_View (Ent))
4582 then
4583 -- If this is a private type whose completion is a derivation from
4584 -- another private type, there is no full view, and the attribute
4585 -- belongs to the type itself, not its underlying parent.
4587 U_Ent := Ent;
4589 elsif Ekind (Ent) = E_Incomplete_Type then
4591 -- The attribute applies to the full view, set the entity of the
4592 -- attribute definition accordingly.
4594 Ent := Underlying_Type (Ent);
4595 U_Ent := Ent;
4596 Set_Entity (Nam, Ent);
4598 else
4599 U_Ent := Underlying_Type (Ent);
4600 end if;
4602 -- Avoid cascaded error
4604 if Etype (Nam) = Any_Type then
4605 return;
4607 -- Must be declared in current scope or in case of an aspect
4608 -- specification, must be visible in current scope.
4610 elsif Scope (Ent) /= Current_Scope
4611 and then
4612 not (From_Aspect_Specification (N)
4613 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4614 then
4615 Error_Msg_N ("entity must be declared in this scope", Nam);
4616 return;
4618 -- Must not be a source renaming (we do have some cases where the
4619 -- expander generates a renaming, and those cases are OK, in such
4620 -- cases any attribute applies to the renamed object as well).
4622 elsif Is_Object (Ent)
4623 and then Present (Renamed_Object (Ent))
4624 then
4625 -- Case of renamed object from source, this is an error
4627 if Comes_From_Source (Renamed_Object (Ent)) then
4628 Get_Name_String (Chars (N));
4629 Error_Msg_Strlen := Name_Len;
4630 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4631 Error_Msg_N
4632 ("~ clause not allowed for a renaming declaration "
4633 & "(RM 13.1(6))", Nam);
4634 return;
4636 -- For the case of a compiler generated renaming, the attribute
4637 -- definition clause applies to the renamed object created by the
4638 -- expander. The easiest general way to handle this is to create a
4639 -- copy of the attribute definition clause for this object.
4641 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4642 Insert_Action (N,
4643 Make_Attribute_Definition_Clause (Loc,
4644 Name =>
4645 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4646 Chars => Chars (N),
4647 Expression => Duplicate_Subexpr (Expression (N))));
4649 -- If the renamed object is not an entity, it must be a dereference
4650 -- of an unconstrained function call, and we must introduce a new
4651 -- declaration to capture the expression. This is needed in the case
4652 -- of 'Alignment, where the original declaration must be rewritten.
4654 else
4655 pragma Assert
4656 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4657 null;
4658 end if;
4660 -- If no underlying entity, use entity itself, applies to some
4661 -- previously detected error cases ???
4663 elsif No (U_Ent) then
4664 U_Ent := Ent;
4666 -- Cannot specify for a subtype (exception Object/Value_Size)
4668 elsif Is_Type (U_Ent)
4669 and then not Is_First_Subtype (U_Ent)
4670 and then Id /= Attribute_Object_Size
4671 and then Id /= Attribute_Value_Size
4672 and then not From_At_Mod (N)
4673 then
4674 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4675 return;
4676 end if;
4678 Set_Entity (N, U_Ent);
4680 -- Switch on particular attribute
4682 case Id is
4684 -------------
4685 -- Address --
4686 -------------
4688 -- Address attribute definition clause
4690 when Attribute_Address => Address : begin
4692 -- A little error check, catch for X'Address use X'Address;
4694 if Nkind (Nam) = N_Identifier
4695 and then Nkind (Expr) = N_Attribute_Reference
4696 and then Attribute_Name (Expr) = Name_Address
4697 and then Nkind (Prefix (Expr)) = N_Identifier
4698 and then Chars (Nam) = Chars (Prefix (Expr))
4699 then
4700 Error_Msg_NE
4701 ("address for & is self-referencing", Prefix (Expr), Ent);
4702 return;
4703 end if;
4705 -- Not that special case, carry on with analysis of expression
4707 Analyze_And_Resolve (Expr, RTE (RE_Address));
4709 -- Even when ignoring rep clauses we need to indicate that the
4710 -- entity has an address clause and thus it is legal to declare
4711 -- it imported. Freeze will get rid of the address clause later.
4713 if Ignore_Rep_Clauses then
4714 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4715 Record_Rep_Item (U_Ent, N);
4716 end if;
4718 return;
4719 end if;
4721 if Duplicate_Clause then
4722 null;
4724 -- Case of address clause for subprogram
4726 elsif Is_Subprogram (U_Ent) then
4727 if Has_Homonym (U_Ent) then
4728 Error_Msg_N
4729 ("address clause cannot be given " &
4730 "for overloaded subprogram",
4731 Nam);
4732 return;
4733 end if;
4735 -- For subprograms, all address clauses are permitted, and we
4736 -- mark the subprogram as having a deferred freeze so that Gigi
4737 -- will not elaborate it too soon.
4739 -- Above needs more comments, what is too soon about???
4741 Set_Has_Delayed_Freeze (U_Ent);
4743 -- Case of address clause for entry
4745 elsif Ekind (U_Ent) = E_Entry then
4746 if Nkind (Parent (N)) = N_Task_Body then
4747 Error_Msg_N
4748 ("entry address must be specified in task spec", Nam);
4749 return;
4750 end if;
4752 -- For entries, we require a constant address
4754 Check_Constant_Address_Clause (Expr, U_Ent);
4756 -- Special checks for task types
4758 if Is_Task_Type (Scope (U_Ent))
4759 and then Comes_From_Source (Scope (U_Ent))
4760 then
4761 Error_Msg_N
4762 ("??entry address declared for entry in task type", N);
4763 Error_Msg_N
4764 ("\??only one task can be declared of this type", N);
4765 end if;
4767 -- Entry address clauses are obsolescent
4769 Check_Restriction (No_Obsolescent_Features, N);
4771 if Warn_On_Obsolescent_Feature then
4772 Error_Msg_N
4773 ("?j?attaching interrupt to task entry is an " &
4774 "obsolescent feature (RM J.7.1)", N);
4775 Error_Msg_N
4776 ("\?j?use interrupt procedure instead", N);
4777 end if;
4779 -- Case of an address clause for a controlled object which we
4780 -- consider to be erroneous.
4782 elsif Is_Controlled (Etype (U_Ent))
4783 or else Has_Controlled_Component (Etype (U_Ent))
4784 then
4785 Error_Msg_NE
4786 ("??controlled object& must not be overlaid", Nam, U_Ent);
4787 Error_Msg_N
4788 ("\??Program_Error will be raised at run time", Nam);
4789 Insert_Action (Declaration_Node (U_Ent),
4790 Make_Raise_Program_Error (Loc,
4791 Reason => PE_Overlaid_Controlled_Object));
4792 return;
4794 -- Case of address clause for a (non-controlled) object
4796 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4797 declare
4798 Expr : constant Node_Id := Expression (N);
4799 O_Ent : Entity_Id;
4800 Off : Boolean;
4802 begin
4803 -- Exported variables cannot have an address clause, because
4804 -- this cancels the effect of the pragma Export.
4806 if Is_Exported (U_Ent) then
4807 Error_Msg_N
4808 ("cannot export object with address clause", Nam);
4809 return;
4810 end if;
4812 Find_Overlaid_Entity (N, O_Ent, Off);
4814 if Present (O_Ent) then
4816 -- If the object overlays a constant object, mark it so
4818 if Is_Constant_Object (O_Ent) then
4819 Set_Overlays_Constant (U_Ent);
4820 end if;
4822 else
4823 -- If this is not an overlay, mark a variable as being
4824 -- volatile to prevent unwanted optimizations. It's a
4825 -- conservative interpretation of RM 13.3(19) for the
4826 -- cases where the compiler cannot detect potential
4827 -- aliasing issues easily and it also covers the case
4828 -- of an absolute address where the volatile aspect is
4829 -- kind of implicit.
4831 if Ekind (U_Ent) = E_Variable then
4832 Set_Treat_As_Volatile (U_Ent);
4833 end if;
4834 end if;
4836 -- Overlaying controlled objects is erroneous. Emit warning
4837 -- but continue analysis because program is itself legal,
4838 -- and back end must see address clause.
4840 if Present (O_Ent)
4841 and then (Has_Controlled_Component (Etype (O_Ent))
4842 or else Is_Controlled (Etype (O_Ent)))
4843 and then not Inside_A_Generic
4844 then
4845 Error_Msg_N
4846 ("??cannot use overlays with controlled objects", Expr);
4847 Error_Msg_N
4848 ("\??Program_Error will be raised at run time", Expr);
4849 Insert_Action (Declaration_Node (U_Ent),
4850 Make_Raise_Program_Error (Loc,
4851 Reason => PE_Overlaid_Controlled_Object));
4853 -- Issue an unconditional warning for a constant overlaying
4854 -- a variable. For the reverse case, we will issue it only
4855 -- if the variable is modified.
4857 elsif Ekind (U_Ent) = E_Constant
4858 and then Present (O_Ent)
4859 and then not Overlays_Constant (U_Ent)
4860 and then Address_Clause_Overlay_Warnings
4861 then
4862 Error_Msg_N ("??constant overlays a variable", Expr);
4864 -- Imported variables can have an address clause, but then
4865 -- the import is pretty meaningless except to suppress
4866 -- initializations, so we do not need such variables to
4867 -- be statically allocated (and in fact it causes trouble
4868 -- if the address clause is a local value).
4870 elsif Is_Imported (U_Ent) then
4871 Set_Is_Statically_Allocated (U_Ent, False);
4872 end if;
4874 -- We mark a possible modification of a variable with an
4875 -- address clause, since it is likely aliasing is occurring.
4877 Note_Possible_Modification (Nam, Sure => False);
4879 -- Legality checks on the address clause for initialized
4880 -- objects is deferred until the freeze point, because
4881 -- a subsequent pragma might indicate that the object
4882 -- is imported and thus not initialized. Also, the address
4883 -- clause might involve entities that have yet to be
4884 -- elaborated.
4886 Set_Has_Delayed_Freeze (U_Ent);
4888 -- If an initialization call has been generated for this
4889 -- object, it needs to be deferred to after the freeze node
4890 -- we have just now added, otherwise GIGI will see a
4891 -- reference to the variable (as actual to the IP call)
4892 -- before its definition.
4894 declare
4895 Init_Call : constant Node_Id :=
4896 Remove_Init_Call (U_Ent, N);
4898 begin
4899 if Present (Init_Call) then
4900 Append_Freeze_Action (U_Ent, Init_Call);
4902 -- Reset Initialization_Statements pointer so that
4903 -- if there is a pragma Import further down, it can
4904 -- clear any default initialization.
4906 Set_Initialization_Statements (U_Ent, Init_Call);
4907 end if;
4908 end;
4910 -- Entity has delayed freeze, so we will generate an
4911 -- alignment check at the freeze point unless suppressed.
4913 if not Range_Checks_Suppressed (U_Ent)
4914 and then not Alignment_Checks_Suppressed (U_Ent)
4915 then
4916 Set_Check_Address_Alignment (N);
4917 end if;
4919 -- Kill the size check code, since we are not allocating
4920 -- the variable, it is somewhere else.
4922 Kill_Size_Check_Code (U_Ent);
4924 -- If the address clause is of the form:
4926 -- for Y'Address use X'Address
4928 -- or
4930 -- Const : constant Address := X'Address;
4931 -- ...
4932 -- for Y'Address use Const;
4934 -- then we make an entry in the table for checking the size
4935 -- and alignment of the overlaying variable. We defer this
4936 -- check till after code generation to take full advantage
4937 -- of the annotation done by the back end.
4939 -- If the entity has a generic type, the check will be
4940 -- performed in the instance if the actual type justifies
4941 -- it, and we do not insert the clause in the table to
4942 -- prevent spurious warnings.
4944 -- Note: we used to test Comes_From_Source and only give
4945 -- this warning for source entities, but we have removed
4946 -- this test. It really seems bogus to generate overlays
4947 -- that would trigger this warning in generated code.
4948 -- Furthermore, by removing the test, we handle the
4949 -- aspect case properly.
4951 if Present (O_Ent)
4952 and then Is_Object (O_Ent)
4953 and then not Is_Generic_Type (Etype (U_Ent))
4954 and then Address_Clause_Overlay_Warnings
4955 then
4956 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4957 end if;
4958 end;
4960 -- Not a valid entity for an address clause
4962 else
4963 Error_Msg_N ("address cannot be given for &", Nam);
4964 end if;
4965 end Address;
4967 ---------------
4968 -- Alignment --
4969 ---------------
4971 -- Alignment attribute definition clause
4973 when Attribute_Alignment => Alignment : declare
4974 Align : constant Uint := Get_Alignment_Value (Expr);
4975 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4977 begin
4978 FOnly := True;
4980 if not Is_Type (U_Ent)
4981 and then Ekind (U_Ent) /= E_Variable
4982 and then Ekind (U_Ent) /= E_Constant
4983 then
4984 Error_Msg_N ("alignment cannot be given for &", Nam);
4986 elsif Duplicate_Clause then
4987 null;
4989 elsif Align /= No_Uint then
4990 Set_Has_Alignment_Clause (U_Ent);
4992 -- Tagged type case, check for attempt to set alignment to a
4993 -- value greater than Max_Align, and reset if so.
4995 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4996 Error_Msg_N
4997 ("alignment for & set to Maximum_Aligment??", Nam);
4998 Set_Alignment (U_Ent, Max_Align);
5000 -- All other cases
5002 else
5003 Set_Alignment (U_Ent, Align);
5004 end if;
5006 -- For an array type, U_Ent is the first subtype. In that case,
5007 -- also set the alignment of the anonymous base type so that
5008 -- other subtypes (such as the itypes for aggregates of the
5009 -- type) also receive the expected alignment.
5011 if Is_Array_Type (U_Ent) then
5012 Set_Alignment (Base_Type (U_Ent), Align);
5013 end if;
5014 end if;
5015 end Alignment;
5017 ---------------
5018 -- Bit_Order --
5019 ---------------
5021 -- Bit_Order attribute definition clause
5023 when Attribute_Bit_Order => Bit_Order : declare
5024 begin
5025 if not Is_Record_Type (U_Ent) then
5026 Error_Msg_N
5027 ("Bit_Order can only be defined for record type", Nam);
5029 elsif Duplicate_Clause then
5030 null;
5032 else
5033 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5035 if Etype (Expr) = Any_Type then
5036 return;
5038 elsif not Is_OK_Static_Expression (Expr) then
5039 Flag_Non_Static_Expr
5040 ("Bit_Order requires static expression!", Expr);
5042 else
5043 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5044 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
5045 end if;
5046 end if;
5047 end if;
5048 end Bit_Order;
5050 --------------------
5051 -- Component_Size --
5052 --------------------
5054 -- Component_Size attribute definition clause
5056 when Attribute_Component_Size => Component_Size_Case : declare
5057 Csize : constant Uint := Static_Integer (Expr);
5058 Ctyp : Entity_Id;
5059 Btype : Entity_Id;
5060 Biased : Boolean;
5061 New_Ctyp : Entity_Id;
5062 Decl : Node_Id;
5064 begin
5065 if not Is_Array_Type (U_Ent) then
5066 Error_Msg_N ("component size requires array type", Nam);
5067 return;
5068 end if;
5070 Btype := Base_Type (U_Ent);
5071 Ctyp := Component_Type (Btype);
5073 if Duplicate_Clause then
5074 null;
5076 elsif Rep_Item_Too_Early (Btype, N) then
5077 null;
5079 elsif Csize /= No_Uint then
5080 Check_Size (Expr, Ctyp, Csize, Biased);
5082 -- For the biased case, build a declaration for a subtype that
5083 -- will be used to represent the biased subtype that reflects
5084 -- the biased representation of components. We need the subtype
5085 -- to get proper conversions on referencing elements of the
5086 -- array.
5088 if Biased then
5089 New_Ctyp :=
5090 Make_Defining_Identifier (Loc,
5091 Chars =>
5092 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
5094 Decl :=
5095 Make_Subtype_Declaration (Loc,
5096 Defining_Identifier => New_Ctyp,
5097 Subtype_Indication =>
5098 New_Occurrence_Of (Component_Type (Btype), Loc));
5100 Set_Parent (Decl, N);
5101 Analyze (Decl, Suppress => All_Checks);
5103 Set_Has_Delayed_Freeze (New_Ctyp, False);
5104 Set_Esize (New_Ctyp, Csize);
5105 Set_RM_Size (New_Ctyp, Csize);
5106 Init_Alignment (New_Ctyp);
5107 Set_Is_Itype (New_Ctyp, True);
5108 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5110 Set_Component_Type (Btype, New_Ctyp);
5111 Set_Biased (New_Ctyp, N, "component size clause");
5112 end if;
5114 Set_Component_Size (Btype, Csize);
5116 -- Deal with warning on overridden size
5118 if Warn_On_Overridden_Size
5119 and then Has_Size_Clause (Ctyp)
5120 and then RM_Size (Ctyp) /= Csize
5121 then
5122 Error_Msg_NE
5123 ("component size overrides size clause for&?S?", N, Ctyp);
5124 end if;
5126 Set_Has_Component_Size_Clause (Btype, True);
5127 Set_Has_Non_Standard_Rep (Btype, True);
5128 end if;
5129 end Component_Size_Case;
5131 -----------------------
5132 -- Constant_Indexing --
5133 -----------------------
5135 when Attribute_Constant_Indexing =>
5136 Check_Indexing_Functions;
5138 ---------
5139 -- CPU --
5140 ---------
5142 when Attribute_CPU => CPU :
5143 begin
5144 -- CPU attribute definition clause not allowed except from aspect
5145 -- specification.
5147 if From_Aspect_Specification (N) then
5148 if not Is_Task_Type (U_Ent) then
5149 Error_Msg_N ("CPU can only be defined for task", Nam);
5151 elsif Duplicate_Clause then
5152 null;
5154 else
5155 -- The expression must be analyzed in the special manner
5156 -- described in "Handling of Default and Per-Object
5157 -- Expressions" in sem.ads.
5159 -- The visibility to the discriminants must be restored
5161 Push_Scope_And_Install_Discriminants (U_Ent);
5162 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5163 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5165 if not Is_OK_Static_Expression (Expr) then
5166 Check_Restriction (Static_Priorities, Expr);
5167 end if;
5168 end if;
5170 else
5171 Error_Msg_N
5172 ("attribute& cannot be set with definition clause", N);
5173 end if;
5174 end CPU;
5176 ----------------------
5177 -- Default_Iterator --
5178 ----------------------
5180 when Attribute_Default_Iterator => Default_Iterator : declare
5181 Func : Entity_Id;
5182 Typ : Entity_Id;
5184 begin
5185 -- If target type is untagged, further checks are irrelevant
5187 if not Is_Tagged_Type (U_Ent) then
5188 Error_Msg_N
5189 ("aspect Default_Iterator applies to tagged type", Nam);
5190 return;
5191 end if;
5193 Check_Iterator_Functions;
5195 Analyze (Expr);
5197 if not Is_Entity_Name (Expr)
5198 or else Ekind (Entity (Expr)) /= E_Function
5199 then
5200 Error_Msg_N ("aspect Iterator must be a function", Expr);
5201 return;
5202 else
5203 Func := Entity (Expr);
5204 end if;
5206 -- The type of the first parameter must be T, T'class, or a
5207 -- corresponding access type (5.5.1 (8/3). If function is
5208 -- parameterless label type accordingly.
5210 if No (First_Formal (Func)) then
5211 Typ := Any_Type;
5212 else
5213 Typ := Etype (First_Formal (Func));
5214 end if;
5216 if Typ = U_Ent
5217 or else Typ = Class_Wide_Type (U_Ent)
5218 or else (Is_Access_Type (Typ)
5219 and then Designated_Type (Typ) = U_Ent)
5220 or else (Is_Access_Type (Typ)
5221 and then Designated_Type (Typ) =
5222 Class_Wide_Type (U_Ent))
5223 then
5224 null;
5226 else
5227 Error_Msg_NE
5228 ("Default Iterator must be a primitive of&", Func, U_Ent);
5229 end if;
5230 end Default_Iterator;
5232 ------------------------
5233 -- Dispatching_Domain --
5234 ------------------------
5236 when Attribute_Dispatching_Domain => Dispatching_Domain :
5237 begin
5238 -- Dispatching_Domain attribute definition clause not allowed
5239 -- except from aspect specification.
5241 if From_Aspect_Specification (N) then
5242 if not Is_Task_Type (U_Ent) then
5243 Error_Msg_N
5244 ("Dispatching_Domain can only be defined for task", Nam);
5246 elsif Duplicate_Clause then
5247 null;
5249 else
5250 -- The expression must be analyzed in the special manner
5251 -- described in "Handling of Default and Per-Object
5252 -- Expressions" in sem.ads.
5254 -- The visibility to the discriminants must be restored
5256 Push_Scope_And_Install_Discriminants (U_Ent);
5258 Preanalyze_Spec_Expression
5259 (Expr, RTE (RE_Dispatching_Domain));
5261 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5262 end if;
5264 else
5265 Error_Msg_N
5266 ("attribute& cannot be set with definition clause", N);
5267 end if;
5268 end Dispatching_Domain;
5270 ------------------
5271 -- External_Tag --
5272 ------------------
5274 when Attribute_External_Tag => External_Tag :
5275 begin
5276 if not Is_Tagged_Type (U_Ent) then
5277 Error_Msg_N ("should be a tagged type", Nam);
5278 end if;
5280 if Duplicate_Clause then
5281 null;
5283 else
5284 Analyze_And_Resolve (Expr, Standard_String);
5286 if not Is_OK_Static_Expression (Expr) then
5287 Flag_Non_Static_Expr
5288 ("static string required for tag name!", Nam);
5289 end if;
5291 if not Is_Library_Level_Entity (U_Ent) then
5292 Error_Msg_NE
5293 ("??non-unique external tag supplied for &", N, U_Ent);
5294 Error_Msg_N
5295 ("\??same external tag applies to all "
5296 & "subprogram calls", N);
5297 Error_Msg_N
5298 ("\??corresponding internal tag cannot be obtained", N);
5299 end if;
5300 end if;
5301 end External_Tag;
5303 --------------------------
5304 -- Implicit_Dereference --
5305 --------------------------
5307 when Attribute_Implicit_Dereference =>
5309 -- Legality checks already performed at the point of the type
5310 -- declaration, aspect is not delayed.
5312 null;
5314 -----------
5315 -- Input --
5316 -----------
5318 when Attribute_Input =>
5319 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5320 Set_Has_Specified_Stream_Input (Ent);
5322 ------------------------
5323 -- Interrupt_Priority --
5324 ------------------------
5326 when Attribute_Interrupt_Priority => Interrupt_Priority :
5327 begin
5328 -- Interrupt_Priority attribute definition clause not allowed
5329 -- except from aspect specification.
5331 if From_Aspect_Specification (N) then
5332 if not Is_Concurrent_Type (U_Ent) then
5333 Error_Msg_N
5334 ("Interrupt_Priority can only be defined for task "
5335 & "and protected object", Nam);
5337 elsif Duplicate_Clause then
5338 null;
5340 else
5341 -- The expression must be analyzed in the special manner
5342 -- described in "Handling of Default and Per-Object
5343 -- Expressions" in sem.ads.
5345 -- The visibility to the discriminants must be restored
5347 Push_Scope_And_Install_Discriminants (U_Ent);
5349 Preanalyze_Spec_Expression
5350 (Expr, RTE (RE_Interrupt_Priority));
5352 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5354 -- Check the No_Task_At_Interrupt_Priority restriction
5356 if Is_Task_Type (U_Ent) then
5357 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5358 end if;
5359 end if;
5361 else
5362 Error_Msg_N
5363 ("attribute& cannot be set with definition clause", N);
5364 end if;
5365 end Interrupt_Priority;
5367 --------------
5368 -- Iterable --
5369 --------------
5371 when Attribute_Iterable =>
5372 Analyze (Expr);
5374 if Nkind (Expr) /= N_Aggregate then
5375 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5376 end if;
5378 declare
5379 Assoc : Node_Id;
5381 begin
5382 Assoc := First (Component_Associations (Expr));
5383 while Present (Assoc) loop
5384 if not Is_Entity_Name (Expression (Assoc)) then
5385 Error_Msg_N ("value must be a function", Assoc);
5386 end if;
5388 Next (Assoc);
5389 end loop;
5390 end;
5392 ----------------------
5393 -- Iterator_Element --
5394 ----------------------
5396 when Attribute_Iterator_Element =>
5397 Analyze (Expr);
5399 if not Is_Entity_Name (Expr)
5400 or else not Is_Type (Entity (Expr))
5401 then
5402 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5403 end if;
5405 -------------------
5406 -- Machine_Radix --
5407 -------------------
5409 -- Machine radix attribute definition clause
5411 when Attribute_Machine_Radix => Machine_Radix : declare
5412 Radix : constant Uint := Static_Integer (Expr);
5414 begin
5415 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5416 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5418 elsif Duplicate_Clause then
5419 null;
5421 elsif Radix /= No_Uint then
5422 Set_Has_Machine_Radix_Clause (U_Ent);
5423 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5425 if Radix = 2 then
5426 null;
5427 elsif Radix = 10 then
5428 Set_Machine_Radix_10 (U_Ent);
5429 else
5430 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5431 end if;
5432 end if;
5433 end Machine_Radix;
5435 -----------------
5436 -- Object_Size --
5437 -----------------
5439 -- Object_Size attribute definition clause
5441 when Attribute_Object_Size => Object_Size : declare
5442 Size : constant Uint := Static_Integer (Expr);
5444 Biased : Boolean;
5445 pragma Warnings (Off, Biased);
5447 begin
5448 if not Is_Type (U_Ent) then
5449 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5451 elsif Duplicate_Clause then
5452 null;
5454 else
5455 Check_Size (Expr, U_Ent, Size, Biased);
5457 if Is_Scalar_Type (U_Ent) then
5458 if Size /= 8 and then Size /= 16 and then Size /= 32
5459 and then UI_Mod (Size, 64) /= 0
5460 then
5461 Error_Msg_N
5462 ("Object_Size must be 8, 16, 32, or multiple of 64",
5463 Expr);
5464 end if;
5466 elsif Size mod 8 /= 0 then
5467 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5468 end if;
5470 Set_Esize (U_Ent, Size);
5471 Set_Has_Object_Size_Clause (U_Ent);
5472 Alignment_Check_For_Size_Change (U_Ent, Size);
5473 end if;
5474 end Object_Size;
5476 ------------
5477 -- Output --
5478 ------------
5480 when Attribute_Output =>
5481 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5482 Set_Has_Specified_Stream_Output (Ent);
5484 --------------
5485 -- Priority --
5486 --------------
5488 when Attribute_Priority => Priority :
5489 begin
5490 -- Priority attribute definition clause not allowed except from
5491 -- aspect specification.
5493 if From_Aspect_Specification (N) then
5494 if not (Is_Concurrent_Type (U_Ent)
5495 or else Ekind (U_Ent) = E_Procedure)
5496 then
5497 Error_Msg_N
5498 ("Priority can only be defined for task and protected "
5499 & "object", Nam);
5501 elsif Duplicate_Clause then
5502 null;
5504 else
5505 -- The expression must be analyzed in the special manner
5506 -- described in "Handling of Default and Per-Object
5507 -- Expressions" in sem.ads.
5509 -- The visibility to the discriminants must be restored
5511 Push_Scope_And_Install_Discriminants (U_Ent);
5512 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5513 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5515 if not Is_OK_Static_Expression (Expr) then
5516 Check_Restriction (Static_Priorities, Expr);
5517 end if;
5518 end if;
5520 else
5521 Error_Msg_N
5522 ("attribute& cannot be set with definition clause", N);
5523 end if;
5524 end Priority;
5526 ----------
5527 -- Read --
5528 ----------
5530 when Attribute_Read =>
5531 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5532 Set_Has_Specified_Stream_Read (Ent);
5534 --------------------------
5535 -- Scalar_Storage_Order --
5536 --------------------------
5538 -- Scalar_Storage_Order attribute definition clause
5540 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5541 begin
5542 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5543 Error_Msg_N
5544 ("Scalar_Storage_Order can only be defined for "
5545 & "record or array type", Nam);
5547 elsif Duplicate_Clause then
5548 null;
5550 else
5551 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5553 if Etype (Expr) = Any_Type then
5554 return;
5556 elsif not Is_OK_Static_Expression (Expr) then
5557 Flag_Non_Static_Expr
5558 ("Scalar_Storage_Order requires static expression!", Expr);
5560 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5562 -- Here for the case of a non-default (i.e. non-confirming)
5563 -- Scalar_Storage_Order attribute definition.
5565 if Support_Nondefault_SSO_On_Target then
5566 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5567 else
5568 Error_Msg_N
5569 ("non-default Scalar_Storage_Order "
5570 & "not supported on target", Expr);
5571 end if;
5572 end if;
5574 -- Clear SSO default indications since explicit setting of the
5575 -- order overrides the defaults.
5577 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5578 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5579 end if;
5580 end Scalar_Storage_Order;
5582 ----------
5583 -- Size --
5584 ----------
5586 -- Size attribute definition clause
5588 when Attribute_Size => Size : declare
5589 Size : constant Uint := Static_Integer (Expr);
5590 Etyp : Entity_Id;
5591 Biased : Boolean;
5593 begin
5594 FOnly := True;
5596 if Duplicate_Clause then
5597 null;
5599 elsif not Is_Type (U_Ent)
5600 and then Ekind (U_Ent) /= E_Variable
5601 and then Ekind (U_Ent) /= E_Constant
5602 then
5603 Error_Msg_N ("size cannot be given for &", Nam);
5605 elsif Is_Array_Type (U_Ent)
5606 and then not Is_Constrained (U_Ent)
5607 then
5608 Error_Msg_N
5609 ("size cannot be given for unconstrained array", Nam);
5611 elsif Size /= No_Uint then
5612 if Is_Type (U_Ent) then
5613 Etyp := U_Ent;
5614 else
5615 Etyp := Etype (U_Ent);
5616 end if;
5618 -- Check size, note that Gigi is in charge of checking that the
5619 -- size of an array or record type is OK. Also we do not check
5620 -- the size in the ordinary fixed-point case, since it is too
5621 -- early to do so (there may be subsequent small clause that
5622 -- affects the size). We can check the size if a small clause
5623 -- has already been given.
5625 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5626 or else Has_Small_Clause (U_Ent)
5627 then
5628 Check_Size (Expr, Etyp, Size, Biased);
5629 Set_Biased (U_Ent, N, "size clause", Biased);
5630 end if;
5632 -- For types set RM_Size and Esize if possible
5634 if Is_Type (U_Ent) then
5635 Set_RM_Size (U_Ent, Size);
5637 -- For elementary types, increase Object_Size to power of 2,
5638 -- but not less than a storage unit in any case (normally
5639 -- this means it will be byte addressable).
5641 -- For all other types, nothing else to do, we leave Esize
5642 -- (object size) unset, the back end will set it from the
5643 -- size and alignment in an appropriate manner.
5645 -- In both cases, we check whether the alignment must be
5646 -- reset in the wake of the size change.
5648 if Is_Elementary_Type (U_Ent) then
5649 if Size <= System_Storage_Unit then
5650 Init_Esize (U_Ent, System_Storage_Unit);
5651 elsif Size <= 16 then
5652 Init_Esize (U_Ent, 16);
5653 elsif Size <= 32 then
5654 Init_Esize (U_Ent, 32);
5655 else
5656 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5657 end if;
5659 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5660 else
5661 Alignment_Check_For_Size_Change (U_Ent, Size);
5662 end if;
5664 -- For objects, set Esize only
5666 else
5667 if Is_Elementary_Type (Etyp) then
5668 if Size /= System_Storage_Unit
5669 and then
5670 Size /= System_Storage_Unit * 2
5671 and then
5672 Size /= System_Storage_Unit * 4
5673 and then
5674 Size /= System_Storage_Unit * 8
5675 then
5676 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5677 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5678 Error_Msg_N
5679 ("size for primitive object must be a power of 2"
5680 & " in the range ^-^", N);
5681 end if;
5682 end if;
5684 Set_Esize (U_Ent, Size);
5685 end if;
5687 Set_Has_Size_Clause (U_Ent);
5688 end if;
5689 end Size;
5691 -----------
5692 -- Small --
5693 -----------
5695 -- Small attribute definition clause
5697 when Attribute_Small => Small : declare
5698 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5699 Small : Ureal;
5701 begin
5702 Analyze_And_Resolve (Expr, Any_Real);
5704 if Etype (Expr) = Any_Type then
5705 return;
5707 elsif not Is_OK_Static_Expression (Expr) then
5708 Flag_Non_Static_Expr
5709 ("small requires static expression!", Expr);
5710 return;
5712 else
5713 Small := Expr_Value_R (Expr);
5715 if Small <= Ureal_0 then
5716 Error_Msg_N ("small value must be greater than zero", Expr);
5717 return;
5718 end if;
5720 end if;
5722 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5723 Error_Msg_N
5724 ("small requires an ordinary fixed point type", Nam);
5726 elsif Has_Small_Clause (U_Ent) then
5727 Error_Msg_N ("small already given for &", Nam);
5729 elsif Small > Delta_Value (U_Ent) then
5730 Error_Msg_N
5731 ("small value must not be greater than delta value", Nam);
5733 else
5734 Set_Small_Value (U_Ent, Small);
5735 Set_Small_Value (Implicit_Base, Small);
5736 Set_Has_Small_Clause (U_Ent);
5737 Set_Has_Small_Clause (Implicit_Base);
5738 Set_Has_Non_Standard_Rep (Implicit_Base);
5739 end if;
5740 end Small;
5742 ------------------
5743 -- Storage_Pool --
5744 ------------------
5746 -- Storage_Pool attribute definition clause
5748 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5749 Pool : Entity_Id;
5750 T : Entity_Id;
5752 begin
5753 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5754 Error_Msg_N
5755 ("storage pool cannot be given for access-to-subprogram type",
5756 Nam);
5757 return;
5759 elsif not
5760 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5761 then
5762 Error_Msg_N
5763 ("storage pool can only be given for access types", Nam);
5764 return;
5766 elsif Is_Derived_Type (U_Ent) then
5767 Error_Msg_N
5768 ("storage pool cannot be given for a derived access type",
5769 Nam);
5771 elsif Duplicate_Clause then
5772 return;
5774 elsif Present (Associated_Storage_Pool (U_Ent)) then
5775 Error_Msg_N ("storage pool already given for &", Nam);
5776 return;
5777 end if;
5779 -- Check for Storage_Size previously given
5781 declare
5782 SS : constant Node_Id :=
5783 Get_Attribute_Definition_Clause
5784 (U_Ent, Attribute_Storage_Size);
5785 begin
5786 if Present (SS) then
5787 Check_Pool_Size_Clash (U_Ent, N, SS);
5788 end if;
5789 end;
5791 -- Storage_Pool case
5793 if Id = Attribute_Storage_Pool then
5794 Analyze_And_Resolve
5795 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5797 -- In the Simple_Storage_Pool case, we allow a variable of any
5798 -- simple storage pool type, so we Resolve without imposing an
5799 -- expected type.
5801 else
5802 Analyze_And_Resolve (Expr);
5804 if not Present (Get_Rep_Pragma
5805 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5806 then
5807 Error_Msg_N
5808 ("expression must be of a simple storage pool type", Expr);
5809 end if;
5810 end if;
5812 if not Denotes_Variable (Expr) then
5813 Error_Msg_N ("storage pool must be a variable", Expr);
5814 return;
5815 end if;
5817 if Nkind (Expr) = N_Type_Conversion then
5818 T := Etype (Expression (Expr));
5819 else
5820 T := Etype (Expr);
5821 end if;
5823 -- The Stack_Bounded_Pool is used internally for implementing
5824 -- access types with a Storage_Size. Since it only work properly
5825 -- when used on one specific type, we need to check that it is not
5826 -- hijacked improperly:
5828 -- type T is access Integer;
5829 -- for T'Storage_Size use n;
5830 -- type Q is access Float;
5831 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5833 if RTE_Available (RE_Stack_Bounded_Pool)
5834 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5835 then
5836 Error_Msg_N ("non-shareable internal Pool", Expr);
5837 return;
5838 end if;
5840 -- If the argument is a name that is not an entity name, then
5841 -- we construct a renaming operation to define an entity of
5842 -- type storage pool.
5844 if not Is_Entity_Name (Expr)
5845 and then Is_Object_Reference (Expr)
5846 then
5847 Pool := Make_Temporary (Loc, 'P', Expr);
5849 declare
5850 Rnode : constant Node_Id :=
5851 Make_Object_Renaming_Declaration (Loc,
5852 Defining_Identifier => Pool,
5853 Subtype_Mark =>
5854 New_Occurrence_Of (Etype (Expr), Loc),
5855 Name => Expr);
5857 begin
5858 -- If the attribute definition clause comes from an aspect
5859 -- clause, then insert the renaming before the associated
5860 -- entity's declaration, since the attribute clause has
5861 -- not yet been appended to the declaration list.
5863 if From_Aspect_Specification (N) then
5864 Insert_Before (Parent (Entity (N)), Rnode);
5865 else
5866 Insert_Before (N, Rnode);
5867 end if;
5869 Analyze (Rnode);
5870 Set_Associated_Storage_Pool (U_Ent, Pool);
5871 end;
5873 elsif Is_Entity_Name (Expr) then
5874 Pool := Entity (Expr);
5876 -- If pool is a renamed object, get original one. This can
5877 -- happen with an explicit renaming, and within instances.
5879 while Present (Renamed_Object (Pool))
5880 and then Is_Entity_Name (Renamed_Object (Pool))
5881 loop
5882 Pool := Entity (Renamed_Object (Pool));
5883 end loop;
5885 if Present (Renamed_Object (Pool))
5886 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5887 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5888 then
5889 Pool := Entity (Expression (Renamed_Object (Pool)));
5890 end if;
5892 Set_Associated_Storage_Pool (U_Ent, Pool);
5894 elsif Nkind (Expr) = N_Type_Conversion
5895 and then Is_Entity_Name (Expression (Expr))
5896 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5897 then
5898 Pool := Entity (Expression (Expr));
5899 Set_Associated_Storage_Pool (U_Ent, Pool);
5901 else
5902 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5903 return;
5904 end if;
5905 end;
5907 ------------------
5908 -- Storage_Size --
5909 ------------------
5911 -- Storage_Size attribute definition clause
5913 when Attribute_Storage_Size => Storage_Size : declare
5914 Btype : constant Entity_Id := Base_Type (U_Ent);
5916 begin
5917 if Is_Task_Type (U_Ent) then
5919 -- Check obsolescent (but never obsolescent if from aspect)
5921 if not From_Aspect_Specification (N) then
5922 Check_Restriction (No_Obsolescent_Features, N);
5924 if Warn_On_Obsolescent_Feature then
5925 Error_Msg_N
5926 ("?j?storage size clause for task is an " &
5927 "obsolescent feature (RM J.9)", N);
5928 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5929 end if;
5930 end if;
5932 FOnly := True;
5933 end if;
5935 if not Is_Access_Type (U_Ent)
5936 and then Ekind (U_Ent) /= E_Task_Type
5937 then
5938 Error_Msg_N ("storage size cannot be given for &", Nam);
5940 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5941 Error_Msg_N
5942 ("storage size cannot be given for a derived access type",
5943 Nam);
5945 elsif Duplicate_Clause then
5946 null;
5948 else
5949 Analyze_And_Resolve (Expr, Any_Integer);
5951 if Is_Access_Type (U_Ent) then
5953 -- Check for Storage_Pool previously given
5955 declare
5956 SP : constant Node_Id :=
5957 Get_Attribute_Definition_Clause
5958 (U_Ent, Attribute_Storage_Pool);
5960 begin
5961 if Present (SP) then
5962 Check_Pool_Size_Clash (U_Ent, SP, N);
5963 end if;
5964 end;
5966 -- Special case of for x'Storage_Size use 0
5968 if Is_OK_Static_Expression (Expr)
5969 and then Expr_Value (Expr) = 0
5970 then
5971 Set_No_Pool_Assigned (Btype);
5972 end if;
5973 end if;
5975 Set_Has_Storage_Size_Clause (Btype);
5976 end if;
5977 end Storage_Size;
5979 -----------------
5980 -- Stream_Size --
5981 -----------------
5983 when Attribute_Stream_Size => Stream_Size : declare
5984 Size : constant Uint := Static_Integer (Expr);
5986 begin
5987 if Ada_Version <= Ada_95 then
5988 Check_Restriction (No_Implementation_Attributes, N);
5989 end if;
5991 if Duplicate_Clause then
5992 null;
5994 elsif Is_Elementary_Type (U_Ent) then
5995 if Size /= System_Storage_Unit
5996 and then
5997 Size /= System_Storage_Unit * 2
5998 and then
5999 Size /= System_Storage_Unit * 4
6000 and then
6001 Size /= System_Storage_Unit * 8
6002 then
6003 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6004 Error_Msg_N
6005 ("stream size for elementary type must be a"
6006 & " power of 2 and at least ^", N);
6008 elsif RM_Size (U_Ent) > Size then
6009 Error_Msg_Uint_1 := RM_Size (U_Ent);
6010 Error_Msg_N
6011 ("stream size for elementary type must be a"
6012 & " power of 2 and at least ^", N);
6013 end if;
6015 Set_Has_Stream_Size_Clause (U_Ent);
6017 else
6018 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6019 end if;
6020 end Stream_Size;
6022 ----------------
6023 -- Value_Size --
6024 ----------------
6026 -- Value_Size attribute definition clause
6028 when Attribute_Value_Size => Value_Size : declare
6029 Size : constant Uint := Static_Integer (Expr);
6030 Biased : Boolean;
6032 begin
6033 if not Is_Type (U_Ent) then
6034 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6036 elsif Duplicate_Clause then
6037 null;
6039 elsif Is_Array_Type (U_Ent)
6040 and then not Is_Constrained (U_Ent)
6041 then
6042 Error_Msg_N
6043 ("Value_Size cannot be given for unconstrained array", Nam);
6045 else
6046 if Is_Elementary_Type (U_Ent) then
6047 Check_Size (Expr, U_Ent, Size, Biased);
6048 Set_Biased (U_Ent, N, "value size clause", Biased);
6049 end if;
6051 Set_RM_Size (U_Ent, Size);
6052 end if;
6053 end Value_Size;
6055 -----------------------
6056 -- Variable_Indexing --
6057 -----------------------
6059 when Attribute_Variable_Indexing =>
6060 Check_Indexing_Functions;
6062 -----------
6063 -- Write --
6064 -----------
6066 when Attribute_Write =>
6067 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6068 Set_Has_Specified_Stream_Write (Ent);
6070 -- All other attributes cannot be set
6072 when others =>
6073 Error_Msg_N
6074 ("attribute& cannot be set with definition clause", N);
6075 end case;
6077 -- The test for the type being frozen must be performed after any
6078 -- expression the clause has been analyzed since the expression itself
6079 -- might cause freezing that makes the clause illegal.
6081 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6082 return;
6083 end if;
6084 end Analyze_Attribute_Definition_Clause;
6086 ----------------------------
6087 -- Analyze_Code_Statement --
6088 ----------------------------
6090 procedure Analyze_Code_Statement (N : Node_Id) is
6091 HSS : constant Node_Id := Parent (N);
6092 SBody : constant Node_Id := Parent (HSS);
6093 Subp : constant Entity_Id := Current_Scope;
6094 Stmt : Node_Id;
6095 Decl : Node_Id;
6096 StmtO : Node_Id;
6097 DeclO : Node_Id;
6099 begin
6100 -- Accept foreign code statements for CodePeer. The analysis is skipped
6101 -- to avoid rejecting unrecognized constructs.
6103 if CodePeer_Mode then
6104 Set_Analyzed (N);
6105 return;
6106 end if;
6108 -- Analyze and check we get right type, note that this implements the
6109 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6110 -- the only way that Asm_Insn could possibly be visible.
6112 Analyze_And_Resolve (Expression (N));
6114 if Etype (Expression (N)) = Any_Type then
6115 return;
6116 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6117 Error_Msg_N ("incorrect type for code statement", N);
6118 return;
6119 end if;
6121 Check_Code_Statement (N);
6123 -- Make sure we appear in the handled statement sequence of a subprogram
6124 -- (RM 13.8(3)).
6126 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6127 or else Nkind (SBody) /= N_Subprogram_Body
6128 then
6129 Error_Msg_N
6130 ("code statement can only appear in body of subprogram", N);
6131 return;
6132 end if;
6134 -- Do remaining checks (RM 13.8(3)) if not already done
6136 if not Is_Machine_Code_Subprogram (Subp) then
6137 Set_Is_Machine_Code_Subprogram (Subp);
6139 -- No exception handlers allowed
6141 if Present (Exception_Handlers (HSS)) then
6142 Error_Msg_N
6143 ("exception handlers not permitted in machine code subprogram",
6144 First (Exception_Handlers (HSS)));
6145 end if;
6147 -- No declarations other than use clauses and pragmas (we allow
6148 -- certain internally generated declarations as well).
6150 Decl := First (Declarations (SBody));
6151 while Present (Decl) loop
6152 DeclO := Original_Node (Decl);
6153 if Comes_From_Source (DeclO)
6154 and not Nkind_In (DeclO, N_Pragma,
6155 N_Use_Package_Clause,
6156 N_Use_Type_Clause,
6157 N_Implicit_Label_Declaration)
6158 then
6159 Error_Msg_N
6160 ("this declaration not allowed in machine code subprogram",
6161 DeclO);
6162 end if;
6164 Next (Decl);
6165 end loop;
6167 -- No statements other than code statements, pragmas, and labels.
6168 -- Again we allow certain internally generated statements.
6170 -- In Ada 2012, qualified expressions are names, and the code
6171 -- statement is initially parsed as a procedure call.
6173 Stmt := First (Statements (HSS));
6174 while Present (Stmt) loop
6175 StmtO := Original_Node (Stmt);
6177 -- A procedure call transformed into a code statement is OK
6179 if Ada_Version >= Ada_2012
6180 and then Nkind (StmtO) = N_Procedure_Call_Statement
6181 and then Nkind (Name (StmtO)) = N_Qualified_Expression
6182 then
6183 null;
6185 elsif Comes_From_Source (StmtO)
6186 and then not Nkind_In (StmtO, N_Pragma,
6187 N_Label,
6188 N_Code_Statement)
6189 then
6190 Error_Msg_N
6191 ("this statement is not allowed in machine code subprogram",
6192 StmtO);
6193 end if;
6195 Next (Stmt);
6196 end loop;
6197 end if;
6198 end Analyze_Code_Statement;
6200 -----------------------------------------------
6201 -- Analyze_Enumeration_Representation_Clause --
6202 -----------------------------------------------
6204 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6205 Ident : constant Node_Id := Identifier (N);
6206 Aggr : constant Node_Id := Array_Aggregate (N);
6207 Enumtype : Entity_Id;
6208 Elit : Entity_Id;
6209 Expr : Node_Id;
6210 Assoc : Node_Id;
6211 Choice : Node_Id;
6212 Val : Uint;
6214 Err : Boolean := False;
6215 -- Set True to avoid cascade errors and crashes on incorrect source code
6217 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6218 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6219 -- Allowed range of universal integer (= allowed range of enum lit vals)
6221 Min : Uint;
6222 Max : Uint;
6223 -- Minimum and maximum values of entries
6225 Max_Node : Node_Id;
6226 -- Pointer to node for literal providing max value
6228 begin
6229 if Ignore_Rep_Clauses then
6230 Kill_Rep_Clause (N);
6231 return;
6232 end if;
6234 -- Ignore enumeration rep clauses by default in CodePeer mode,
6235 -- unless -gnatd.I is specified, as a work around for potential false
6236 -- positive messages.
6238 if CodePeer_Mode and not Debug_Flag_Dot_II then
6239 return;
6240 end if;
6242 -- First some basic error checks
6244 Find_Type (Ident);
6245 Enumtype := Entity (Ident);
6247 if Enumtype = Any_Type
6248 or else Rep_Item_Too_Early (Enumtype, N)
6249 then
6250 return;
6251 else
6252 Enumtype := Underlying_Type (Enumtype);
6253 end if;
6255 if not Is_Enumeration_Type (Enumtype) then
6256 Error_Msg_NE
6257 ("enumeration type required, found}",
6258 Ident, First_Subtype (Enumtype));
6259 return;
6260 end if;
6262 -- Ignore rep clause on generic actual type. This will already have
6263 -- been flagged on the template as an error, and this is the safest
6264 -- way to ensure we don't get a junk cascaded message in the instance.
6266 if Is_Generic_Actual_Type (Enumtype) then
6267 return;
6269 -- Type must be in current scope
6271 elsif Scope (Enumtype) /= Current_Scope then
6272 Error_Msg_N ("type must be declared in this scope", Ident);
6273 return;
6275 -- Type must be a first subtype
6277 elsif not Is_First_Subtype (Enumtype) then
6278 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6279 return;
6281 -- Ignore duplicate rep clause
6283 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6284 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6285 return;
6287 -- Don't allow rep clause for standard [wide_[wide_]]character
6289 elsif Is_Standard_Character_Type (Enumtype) then
6290 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
6291 return;
6293 -- Check that the expression is a proper aggregate (no parentheses)
6295 elsif Paren_Count (Aggr) /= 0 then
6296 Error_Msg
6297 ("extra parentheses surrounding aggregate not allowed",
6298 First_Sloc (Aggr));
6299 return;
6301 -- All tests passed, so set rep clause in place
6303 else
6304 Set_Has_Enumeration_Rep_Clause (Enumtype);
6305 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6306 end if;
6308 -- Now we process the aggregate. Note that we don't use the normal
6309 -- aggregate code for this purpose, because we don't want any of the
6310 -- normal expansion activities, and a number of special semantic
6311 -- rules apply (including the component type being any integer type)
6313 Elit := First_Literal (Enumtype);
6315 -- First the positional entries if any
6317 if Present (Expressions (Aggr)) then
6318 Expr := First (Expressions (Aggr));
6319 while Present (Expr) loop
6320 if No (Elit) then
6321 Error_Msg_N ("too many entries in aggregate", Expr);
6322 return;
6323 end if;
6325 Val := Static_Integer (Expr);
6327 -- Err signals that we found some incorrect entries processing
6328 -- the list. The final checks for completeness and ordering are
6329 -- skipped in this case.
6331 if Val = No_Uint then
6332 Err := True;
6334 elsif Val < Lo or else Hi < Val then
6335 Error_Msg_N ("value outside permitted range", Expr);
6336 Err := True;
6337 end if;
6339 Set_Enumeration_Rep (Elit, Val);
6340 Set_Enumeration_Rep_Expr (Elit, Expr);
6341 Next (Expr);
6342 Next (Elit);
6343 end loop;
6344 end if;
6346 -- Now process the named entries if present
6348 if Present (Component_Associations (Aggr)) then
6349 Assoc := First (Component_Associations (Aggr));
6350 while Present (Assoc) loop
6351 Choice := First (Choices (Assoc));
6353 if Present (Next (Choice)) then
6354 Error_Msg_N
6355 ("multiple choice not allowed here", Next (Choice));
6356 Err := True;
6357 end if;
6359 if Nkind (Choice) = N_Others_Choice then
6360 Error_Msg_N ("others choice not allowed here", Choice);
6361 Err := True;
6363 elsif Nkind (Choice) = N_Range then
6365 -- ??? should allow zero/one element range here
6367 Error_Msg_N ("range not allowed here", Choice);
6368 Err := True;
6370 else
6371 Analyze_And_Resolve (Choice, Enumtype);
6373 if Error_Posted (Choice) then
6374 Err := True;
6375 end if;
6377 if not Err then
6378 if Is_Entity_Name (Choice)
6379 and then Is_Type (Entity (Choice))
6380 then
6381 Error_Msg_N ("subtype name not allowed here", Choice);
6382 Err := True;
6384 -- ??? should allow static subtype with zero/one entry
6386 elsif Etype (Choice) = Base_Type (Enumtype) then
6387 if not Is_OK_Static_Expression (Choice) then
6388 Flag_Non_Static_Expr
6389 ("non-static expression used for choice!", Choice);
6390 Err := True;
6392 else
6393 Elit := Expr_Value_E (Choice);
6395 if Present (Enumeration_Rep_Expr (Elit)) then
6396 Error_Msg_Sloc :=
6397 Sloc (Enumeration_Rep_Expr (Elit));
6398 Error_Msg_NE
6399 ("representation for& previously given#",
6400 Choice, Elit);
6401 Err := True;
6402 end if;
6404 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6406 Expr := Expression (Assoc);
6407 Val := Static_Integer (Expr);
6409 if Val = No_Uint then
6410 Err := True;
6412 elsif Val < Lo or else Hi < Val then
6413 Error_Msg_N ("value outside permitted range", Expr);
6414 Err := True;
6415 end if;
6417 Set_Enumeration_Rep (Elit, Val);
6418 end if;
6419 end if;
6420 end if;
6421 end if;
6423 Next (Assoc);
6424 end loop;
6425 end if;
6427 -- Aggregate is fully processed. Now we check that a full set of
6428 -- representations was given, and that they are in range and in order.
6429 -- These checks are only done if no other errors occurred.
6431 if not Err then
6432 Min := No_Uint;
6433 Max := No_Uint;
6435 Elit := First_Literal (Enumtype);
6436 while Present (Elit) loop
6437 if No (Enumeration_Rep_Expr (Elit)) then
6438 Error_Msg_NE ("missing representation for&!", N, Elit);
6440 else
6441 Val := Enumeration_Rep (Elit);
6443 if Min = No_Uint then
6444 Min := Val;
6445 end if;
6447 if Val /= No_Uint then
6448 if Max /= No_Uint and then Val <= Max then
6449 Error_Msg_NE
6450 ("enumeration value for& not ordered!",
6451 Enumeration_Rep_Expr (Elit), Elit);
6452 end if;
6454 Max_Node := Enumeration_Rep_Expr (Elit);
6455 Max := Val;
6456 end if;
6458 -- If there is at least one literal whose representation is not
6459 -- equal to the Pos value, then note that this enumeration type
6460 -- has a non-standard representation.
6462 if Val /= Enumeration_Pos (Elit) then
6463 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6464 end if;
6465 end if;
6467 Next (Elit);
6468 end loop;
6470 -- Now set proper size information
6472 declare
6473 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6475 begin
6476 if Has_Size_Clause (Enumtype) then
6478 -- All OK, if size is OK now
6480 if RM_Size (Enumtype) >= Minsize then
6481 null;
6483 else
6484 -- Try if we can get by with biasing
6486 Minsize :=
6487 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6489 -- Error message if even biasing does not work
6491 if RM_Size (Enumtype) < Minsize then
6492 Error_Msg_Uint_1 := RM_Size (Enumtype);
6493 Error_Msg_Uint_2 := Max;
6494 Error_Msg_N
6495 ("previously given size (^) is too small "
6496 & "for this value (^)", Max_Node);
6498 -- If biasing worked, indicate that we now have biased rep
6500 else
6501 Set_Biased
6502 (Enumtype, Size_Clause (Enumtype), "size clause");
6503 end if;
6504 end if;
6506 else
6507 Set_RM_Size (Enumtype, Minsize);
6508 Set_Enum_Esize (Enumtype);
6509 end if;
6511 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6512 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6513 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6514 end;
6515 end if;
6517 -- We repeat the too late test in case it froze itself
6519 if Rep_Item_Too_Late (Enumtype, N) then
6520 null;
6521 end if;
6522 end Analyze_Enumeration_Representation_Clause;
6524 ----------------------------
6525 -- Analyze_Free_Statement --
6526 ----------------------------
6528 procedure Analyze_Free_Statement (N : Node_Id) is
6529 begin
6530 Analyze (Expression (N));
6531 end Analyze_Free_Statement;
6533 ---------------------------
6534 -- Analyze_Freeze_Entity --
6535 ---------------------------
6537 procedure Analyze_Freeze_Entity (N : Node_Id) is
6538 begin
6539 Freeze_Entity_Checks (N);
6540 end Analyze_Freeze_Entity;
6542 -----------------------------------
6543 -- Analyze_Freeze_Generic_Entity --
6544 -----------------------------------
6546 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6547 begin
6548 Freeze_Entity_Checks (N);
6549 end Analyze_Freeze_Generic_Entity;
6551 ------------------------------------------
6552 -- Analyze_Record_Representation_Clause --
6553 ------------------------------------------
6555 -- Note: we check as much as we can here, but we can't do any checks
6556 -- based on the position values (e.g. overlap checks) until freeze time
6557 -- because especially in Ada 2005 (machine scalar mode), the processing
6558 -- for non-standard bit order can substantially change the positions.
6559 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6560 -- for the remainder of this processing.
6562 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6563 Ident : constant Node_Id := Identifier (N);
6564 Biased : Boolean;
6565 CC : Node_Id;
6566 Comp : Entity_Id;
6567 Fbit : Uint;
6568 Hbit : Uint := Uint_0;
6569 Lbit : Uint;
6570 Ocomp : Entity_Id;
6571 Posit : Uint;
6572 Rectype : Entity_Id;
6573 Recdef : Node_Id;
6575 function Is_Inherited (Comp : Entity_Id) return Boolean;
6576 -- True if Comp is an inherited component in a record extension
6578 ------------------
6579 -- Is_Inherited --
6580 ------------------
6582 function Is_Inherited (Comp : Entity_Id) return Boolean is
6583 Comp_Base : Entity_Id;
6585 begin
6586 if Ekind (Rectype) = E_Record_Subtype then
6587 Comp_Base := Original_Record_Component (Comp);
6588 else
6589 Comp_Base := Comp;
6590 end if;
6592 return Comp_Base /= Original_Record_Component (Comp_Base);
6593 end Is_Inherited;
6595 -- Local variables
6597 Is_Record_Extension : Boolean;
6598 -- True if Rectype is a record extension
6600 CR_Pragma : Node_Id := Empty;
6601 -- Points to N_Pragma node if Complete_Representation pragma present
6603 -- Start of processing for Analyze_Record_Representation_Clause
6605 begin
6606 if Ignore_Rep_Clauses then
6607 Kill_Rep_Clause (N);
6608 return;
6609 end if;
6611 Find_Type (Ident);
6612 Rectype := Entity (Ident);
6614 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6615 return;
6616 else
6617 Rectype := Underlying_Type (Rectype);
6618 end if;
6620 -- First some basic error checks
6622 if not Is_Record_Type (Rectype) then
6623 Error_Msg_NE
6624 ("record type required, found}", Ident, First_Subtype (Rectype));
6625 return;
6627 elsif Scope (Rectype) /= Current_Scope then
6628 Error_Msg_N ("type must be declared in this scope", N);
6629 return;
6631 elsif not Is_First_Subtype (Rectype) then
6632 Error_Msg_N ("cannot give record rep clause for subtype", N);
6633 return;
6635 elsif Has_Record_Rep_Clause (Rectype) then
6636 Error_Msg_N ("duplicate record rep clause ignored", N);
6637 return;
6639 elsif Rep_Item_Too_Late (Rectype, N) then
6640 return;
6641 end if;
6643 -- We know we have a first subtype, now possibly go to the anonymous
6644 -- base type to determine whether Rectype is a record extension.
6646 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6647 Is_Record_Extension :=
6648 Nkind (Recdef) = N_Derived_Type_Definition
6649 and then Present (Record_Extension_Part (Recdef));
6651 if Present (Mod_Clause (N)) then
6652 declare
6653 Loc : constant Source_Ptr := Sloc (N);
6654 M : constant Node_Id := Mod_Clause (N);
6655 P : constant List_Id := Pragmas_Before (M);
6656 AtM_Nod : Node_Id;
6658 Mod_Val : Uint;
6659 pragma Warnings (Off, Mod_Val);
6661 begin
6662 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6664 if Warn_On_Obsolescent_Feature then
6665 Error_Msg_N
6666 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6667 Error_Msg_N
6668 ("\?j?use alignment attribute definition clause instead", N);
6669 end if;
6671 if Present (P) then
6672 Analyze_List (P);
6673 end if;
6675 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6676 -- the Mod clause into an alignment clause anyway, so that the
6677 -- back end can compute and back-annotate properly the size and
6678 -- alignment of types that may include this record.
6680 -- This seems dubious, this destroys the source tree in a manner
6681 -- not detectable by ASIS ???
6683 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6684 AtM_Nod :=
6685 Make_Attribute_Definition_Clause (Loc,
6686 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6687 Chars => Name_Alignment,
6688 Expression => Relocate_Node (Expression (M)));
6690 Set_From_At_Mod (AtM_Nod);
6691 Insert_After (N, AtM_Nod);
6692 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6693 Set_Mod_Clause (N, Empty);
6695 else
6696 -- Get the alignment value to perform error checking
6698 Mod_Val := Get_Alignment_Value (Expression (M));
6699 end if;
6700 end;
6701 end if;
6703 -- For untagged types, clear any existing component clauses for the
6704 -- type. If the type is derived, this is what allows us to override
6705 -- a rep clause for the parent. For type extensions, the representation
6706 -- of the inherited components is inherited, so we want to keep previous
6707 -- component clauses for completeness.
6709 if not Is_Tagged_Type (Rectype) then
6710 Comp := First_Component_Or_Discriminant (Rectype);
6711 while Present (Comp) loop
6712 Set_Component_Clause (Comp, Empty);
6713 Next_Component_Or_Discriminant (Comp);
6714 end loop;
6715 end if;
6717 -- All done if no component clauses
6719 CC := First (Component_Clauses (N));
6721 if No (CC) then
6722 return;
6723 end if;
6725 -- A representation like this applies to the base type
6727 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6728 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6729 Set_Has_Specified_Layout (Base_Type (Rectype));
6731 -- Process the component clauses
6733 while Present (CC) loop
6735 -- Pragma
6737 if Nkind (CC) = N_Pragma then
6738 Analyze (CC);
6740 -- The only pragma of interest is Complete_Representation
6742 if Pragma_Name (CC) = Name_Complete_Representation then
6743 CR_Pragma := CC;
6744 end if;
6746 -- Processing for real component clause
6748 else
6749 Posit := Static_Integer (Position (CC));
6750 Fbit := Static_Integer (First_Bit (CC));
6751 Lbit := Static_Integer (Last_Bit (CC));
6753 if Posit /= No_Uint
6754 and then Fbit /= No_Uint
6755 and then Lbit /= No_Uint
6756 then
6757 if Posit < 0 then
6758 Error_Msg_N
6759 ("position cannot be negative", Position (CC));
6761 elsif Fbit < 0 then
6762 Error_Msg_N
6763 ("first bit cannot be negative", First_Bit (CC));
6765 -- The Last_Bit specified in a component clause must not be
6766 -- less than the First_Bit minus one (RM-13.5.1(10)).
6768 elsif Lbit < Fbit - 1 then
6769 Error_Msg_N
6770 ("last bit cannot be less than first bit minus one",
6771 Last_Bit (CC));
6773 -- Values look OK, so find the corresponding record component
6774 -- Even though the syntax allows an attribute reference for
6775 -- implementation-defined components, GNAT does not allow the
6776 -- tag to get an explicit position.
6778 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6779 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6780 Error_Msg_N ("position of tag cannot be specified", CC);
6781 else
6782 Error_Msg_N ("illegal component name", CC);
6783 end if;
6785 else
6786 Comp := First_Entity (Rectype);
6787 while Present (Comp) loop
6788 exit when Chars (Comp) = Chars (Component_Name (CC));
6789 Next_Entity (Comp);
6790 end loop;
6792 if No (Comp) then
6794 -- Maybe component of base type that is absent from
6795 -- statically constrained first subtype.
6797 Comp := First_Entity (Base_Type (Rectype));
6798 while Present (Comp) loop
6799 exit when Chars (Comp) = Chars (Component_Name (CC));
6800 Next_Entity (Comp);
6801 end loop;
6802 end if;
6804 if No (Comp) then
6805 Error_Msg_N
6806 ("component clause is for non-existent field", CC);
6808 -- Ada 2012 (AI05-0026): Any name that denotes a
6809 -- discriminant of an object of an unchecked union type
6810 -- shall not occur within a record_representation_clause.
6812 -- The general restriction of using record rep clauses on
6813 -- Unchecked_Union types has now been lifted. Since it is
6814 -- possible to introduce a record rep clause which mentions
6815 -- the discriminant of an Unchecked_Union in non-Ada 2012
6816 -- code, this check is applied to all versions of the
6817 -- language.
6819 elsif Ekind (Comp) = E_Discriminant
6820 and then Is_Unchecked_Union (Rectype)
6821 then
6822 Error_Msg_N
6823 ("cannot reference discriminant of unchecked union",
6824 Component_Name (CC));
6826 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6827 Error_Msg_NE
6828 ("component clause not allowed for inherited "
6829 & "component&", CC, Comp);
6831 elsif Present (Component_Clause (Comp)) then
6833 -- Diagnose duplicate rep clause, or check consistency
6834 -- if this is an inherited component. In a double fault,
6835 -- there may be a duplicate inconsistent clause for an
6836 -- inherited component.
6838 if Scope (Original_Record_Component (Comp)) = Rectype
6839 or else Parent (Component_Clause (Comp)) = N
6840 then
6841 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6842 Error_Msg_N ("component clause previously given#", CC);
6844 else
6845 declare
6846 Rep1 : constant Node_Id := Component_Clause (Comp);
6847 begin
6848 if Intval (Position (Rep1)) /=
6849 Intval (Position (CC))
6850 or else Intval (First_Bit (Rep1)) /=
6851 Intval (First_Bit (CC))
6852 or else Intval (Last_Bit (Rep1)) /=
6853 Intval (Last_Bit (CC))
6854 then
6855 Error_Msg_N
6856 ("component clause inconsistent "
6857 & "with representation of ancestor", CC);
6859 elsif Warn_On_Redundant_Constructs then
6860 Error_Msg_N
6861 ("?r?redundant confirming component clause "
6862 & "for component!", CC);
6863 end if;
6864 end;
6865 end if;
6867 -- Normal case where this is the first component clause we
6868 -- have seen for this entity, so set it up properly.
6870 else
6871 -- Make reference for field in record rep clause and set
6872 -- appropriate entity field in the field identifier.
6874 Generate_Reference
6875 (Comp, Component_Name (CC), Set_Ref => False);
6876 Set_Entity (Component_Name (CC), Comp);
6878 -- Update Fbit and Lbit to the actual bit number
6880 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6881 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6883 if Has_Size_Clause (Rectype)
6884 and then RM_Size (Rectype) <= Lbit
6885 then
6886 Error_Msg_N
6887 ("bit number out of range of specified size",
6888 Last_Bit (CC));
6889 else
6890 Set_Component_Clause (Comp, CC);
6891 Set_Component_Bit_Offset (Comp, Fbit);
6892 Set_Esize (Comp, 1 + (Lbit - Fbit));
6893 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6894 Set_Normalized_Position (Comp, Fbit / SSU);
6896 if Warn_On_Overridden_Size
6897 and then Has_Size_Clause (Etype (Comp))
6898 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6899 then
6900 Error_Msg_NE
6901 ("?S?component size overrides size clause for&",
6902 Component_Name (CC), Etype (Comp));
6903 end if;
6905 -- This information is also set in the corresponding
6906 -- component of the base type, found by accessing the
6907 -- Original_Record_Component link if it is present.
6909 Ocomp := Original_Record_Component (Comp);
6911 if Hbit < Lbit then
6912 Hbit := Lbit;
6913 end if;
6915 Check_Size
6916 (Component_Name (CC),
6917 Etype (Comp),
6918 Esize (Comp),
6919 Biased);
6921 Set_Biased
6922 (Comp, First_Node (CC), "component clause", Biased);
6924 if Present (Ocomp) then
6925 Set_Component_Clause (Ocomp, CC);
6926 Set_Component_Bit_Offset (Ocomp, Fbit);
6927 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6928 Set_Normalized_Position (Ocomp, Fbit / SSU);
6929 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6931 Set_Normalized_Position_Max
6932 (Ocomp, Normalized_Position (Ocomp));
6934 -- Note: we don't use Set_Biased here, because we
6935 -- already gave a warning above if needed, and we
6936 -- would get a duplicate for the same name here.
6938 Set_Has_Biased_Representation
6939 (Ocomp, Has_Biased_Representation (Comp));
6940 end if;
6942 if Esize (Comp) < 0 then
6943 Error_Msg_N ("component size is negative", CC);
6944 end if;
6945 end if;
6946 end if;
6947 end if;
6948 end if;
6949 end if;
6951 Next (CC);
6952 end loop;
6954 -- Check missing components if Complete_Representation pragma appeared
6956 if Present (CR_Pragma) then
6957 Comp := First_Component_Or_Discriminant (Rectype);
6958 while Present (Comp) loop
6959 if No (Component_Clause (Comp)) then
6960 Error_Msg_NE
6961 ("missing component clause for &", CR_Pragma, Comp);
6962 end if;
6964 Next_Component_Or_Discriminant (Comp);
6965 end loop;
6967 -- Give missing components warning if required
6969 elsif Warn_On_Unrepped_Components then
6970 declare
6971 Num_Repped_Components : Nat := 0;
6972 Num_Unrepped_Components : Nat := 0;
6974 begin
6975 -- First count number of repped and unrepped components
6977 Comp := First_Component_Or_Discriminant (Rectype);
6978 while Present (Comp) loop
6979 if Present (Component_Clause (Comp)) then
6980 Num_Repped_Components := Num_Repped_Components + 1;
6981 else
6982 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6983 end if;
6985 Next_Component_Or_Discriminant (Comp);
6986 end loop;
6988 -- We are only interested in the case where there is at least one
6989 -- unrepped component, and at least half the components have rep
6990 -- clauses. We figure that if less than half have them, then the
6991 -- partial rep clause is really intentional. If the component
6992 -- type has no underlying type set at this point (as for a generic
6993 -- formal type), we don't know enough to give a warning on the
6994 -- component.
6996 if Num_Unrepped_Components > 0
6997 and then Num_Unrepped_Components < Num_Repped_Components
6998 then
6999 Comp := First_Component_Or_Discriminant (Rectype);
7000 while Present (Comp) loop
7001 if No (Component_Clause (Comp))
7002 and then Comes_From_Source (Comp)
7003 and then Present (Underlying_Type (Etype (Comp)))
7004 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
7005 or else Size_Known_At_Compile_Time
7006 (Underlying_Type (Etype (Comp))))
7007 and then not Has_Warnings_Off (Rectype)
7009 -- Ignore discriminant in unchecked union, since it is
7010 -- not there, and cannot have a component clause.
7012 and then (not Is_Unchecked_Union (Rectype)
7013 or else Ekind (Comp) /= E_Discriminant)
7014 then
7015 Error_Msg_Sloc := Sloc (Comp);
7016 Error_Msg_NE
7017 ("?C?no component clause given for & declared #",
7018 N, Comp);
7019 end if;
7021 Next_Component_Or_Discriminant (Comp);
7022 end loop;
7023 end if;
7024 end;
7025 end if;
7026 end Analyze_Record_Representation_Clause;
7028 -------------------------------------
7029 -- Build_Discrete_Static_Predicate --
7030 -------------------------------------
7032 procedure Build_Discrete_Static_Predicate
7033 (Typ : Entity_Id;
7034 Expr : Node_Id;
7035 Nam : Name_Id)
7037 Loc : constant Source_Ptr := Sloc (Expr);
7039 Non_Static : exception;
7040 -- Raised if something non-static is found
7042 Btyp : constant Entity_Id := Base_Type (Typ);
7044 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7045 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7046 -- Low bound and high bound value of base type of Typ
7048 TLo : Uint;
7049 THi : Uint;
7050 -- Bounds for constructing the static predicate. We use the bound of the
7051 -- subtype if it is static, otherwise the corresponding base type bound.
7052 -- Note: a non-static subtype can have a static predicate.
7054 type REnt is record
7055 Lo, Hi : Uint;
7056 end record;
7057 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7058 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7059 -- value.
7061 type RList is array (Nat range <>) of REnt;
7062 -- A list of ranges. The ranges are sorted in increasing order, and are
7063 -- disjoint (there is a gap of at least one value between each range in
7064 -- the table). A value is in the set of ranges in Rlist if it lies
7065 -- within one of these ranges.
7067 False_Range : constant RList :=
7068 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7069 -- An empty set of ranges represents a range list that can never be
7070 -- satisfied, since there are no ranges in which the value could lie,
7071 -- so it does not lie in any of them. False_Range is a canonical value
7072 -- for this empty set, but general processing should test for an Rlist
7073 -- with length zero (see Is_False predicate), since other null ranges
7074 -- may appear which must be treated as False.
7076 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7077 -- Range representing True, value must be in the base range
7079 function "and" (Left : RList; Right : RList) return RList;
7080 -- And's together two range lists, returning a range list. This is a set
7081 -- intersection operation.
7083 function "or" (Left : RList; Right : RList) return RList;
7084 -- Or's together two range lists, returning a range list. This is a set
7085 -- union operation.
7087 function "not" (Right : RList) return RList;
7088 -- Returns complement of a given range list, i.e. a range list
7089 -- representing all the values in TLo .. THi that are not in the input
7090 -- operand Right.
7092 function Build_Val (V : Uint) return Node_Id;
7093 -- Return an analyzed N_Identifier node referencing this value, suitable
7094 -- for use as an entry in the Static_Discrte_Predicate list. This node
7095 -- is typed with the base type.
7097 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7098 -- Return an analyzed N_Range node referencing this range, suitable for
7099 -- use as an entry in the Static_Discrete_Predicate list. This node is
7100 -- typed with the base type.
7102 function Get_RList (Exp : Node_Id) return RList;
7103 -- This is a recursive routine that converts the given expression into a
7104 -- list of ranges, suitable for use in building the static predicate.
7106 function Is_False (R : RList) return Boolean;
7107 pragma Inline (Is_False);
7108 -- Returns True if the given range list is empty, and thus represents a
7109 -- False list of ranges that can never be satisfied.
7111 function Is_True (R : RList) return Boolean;
7112 -- Returns True if R trivially represents the True predicate by having a
7113 -- single range from BLo to BHi.
7115 function Is_Type_Ref (N : Node_Id) return Boolean;
7116 pragma Inline (Is_Type_Ref);
7117 -- Returns if True if N is a reference to the type for the predicate in
7118 -- the expression (i.e. if it is an identifier whose Chars field matches
7119 -- the Nam given in the call). N must not be parenthesized, if the type
7120 -- name appears in parens, this routine will return False.
7122 function Lo_Val (N : Node_Id) return Uint;
7123 -- Given an entry from a Static_Discrete_Predicate list that is either
7124 -- a static expression or static range, gets either the expression value
7125 -- or the low bound of the range.
7127 function Hi_Val (N : Node_Id) return Uint;
7128 -- Given an entry from a Static_Discrete_Predicate list that is either
7129 -- a static expression or static range, gets either the expression value
7130 -- or the high bound of the range.
7132 function Membership_Entry (N : Node_Id) return RList;
7133 -- Given a single membership entry (range, value, or subtype), returns
7134 -- the corresponding range list. Raises Static_Error if not static.
7136 function Membership_Entries (N : Node_Id) return RList;
7137 -- Given an element on an alternatives list of a membership operation,
7138 -- returns the range list corresponding to this entry and all following
7139 -- entries (i.e. returns the "or" of this list of values).
7141 function Stat_Pred (Typ : Entity_Id) return RList;
7142 -- Given a type, if it has a static predicate, then return the predicate
7143 -- as a range list, otherwise raise Non_Static.
7145 -----------
7146 -- "and" --
7147 -----------
7149 function "and" (Left : RList; Right : RList) return RList is
7150 FEnt : REnt;
7151 -- First range of result
7153 SLeft : Nat := Left'First;
7154 -- Start of rest of left entries
7156 SRight : Nat := Right'First;
7157 -- Start of rest of right entries
7159 begin
7160 -- If either range is True, return the other
7162 if Is_True (Left) then
7163 return Right;
7164 elsif Is_True (Right) then
7165 return Left;
7166 end if;
7168 -- If either range is False, return False
7170 if Is_False (Left) or else Is_False (Right) then
7171 return False_Range;
7172 end if;
7174 -- Loop to remove entries at start that are disjoint, and thus just
7175 -- get discarded from the result entirely.
7177 loop
7178 -- If no operands left in either operand, result is false
7180 if SLeft > Left'Last or else SRight > Right'Last then
7181 return False_Range;
7183 -- Discard first left operand entry if disjoint with right
7185 elsif Left (SLeft).Hi < Right (SRight).Lo then
7186 SLeft := SLeft + 1;
7188 -- Discard first right operand entry if disjoint with left
7190 elsif Right (SRight).Hi < Left (SLeft).Lo then
7191 SRight := SRight + 1;
7193 -- Otherwise we have an overlapping entry
7195 else
7196 exit;
7197 end if;
7198 end loop;
7200 -- Now we have two non-null operands, and first entries overlap. The
7201 -- first entry in the result will be the overlapping part of these
7202 -- two entries.
7204 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7205 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7207 -- Now we can remove the entry that ended at a lower value, since its
7208 -- contribution is entirely contained in Fent.
7210 if Left (SLeft).Hi <= Right (SRight).Hi then
7211 SLeft := SLeft + 1;
7212 else
7213 SRight := SRight + 1;
7214 end if;
7216 -- Compute result by concatenating this first entry with the "and" of
7217 -- the remaining parts of the left and right operands. Note that if
7218 -- either of these is empty, "and" will yield empty, so that we will
7219 -- end up with just Fent, which is what we want in that case.
7221 return
7222 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7223 end "and";
7225 -----------
7226 -- "not" --
7227 -----------
7229 function "not" (Right : RList) return RList is
7230 begin
7231 -- Return True if False range
7233 if Is_False (Right) then
7234 return True_Range;
7235 end if;
7237 -- Return False if True range
7239 if Is_True (Right) then
7240 return False_Range;
7241 end if;
7243 -- Here if not trivial case
7245 declare
7246 Result : RList (1 .. Right'Length + 1);
7247 -- May need one more entry for gap at beginning and end
7249 Count : Nat := 0;
7250 -- Number of entries stored in Result
7252 begin
7253 -- Gap at start
7255 if Right (Right'First).Lo > TLo then
7256 Count := Count + 1;
7257 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7258 end if;
7260 -- Gaps between ranges
7262 for J in Right'First .. Right'Last - 1 loop
7263 Count := Count + 1;
7264 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7265 end loop;
7267 -- Gap at end
7269 if Right (Right'Last).Hi < THi then
7270 Count := Count + 1;
7271 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7272 end if;
7274 return Result (1 .. Count);
7275 end;
7276 end "not";
7278 ----------
7279 -- "or" --
7280 ----------
7282 function "or" (Left : RList; Right : RList) return RList is
7283 FEnt : REnt;
7284 -- First range of result
7286 SLeft : Nat := Left'First;
7287 -- Start of rest of left entries
7289 SRight : Nat := Right'First;
7290 -- Start of rest of right entries
7292 begin
7293 -- If either range is True, return True
7295 if Is_True (Left) or else Is_True (Right) then
7296 return True_Range;
7297 end if;
7299 -- If either range is False (empty), return the other
7301 if Is_False (Left) then
7302 return Right;
7303 elsif Is_False (Right) then
7304 return Left;
7305 end if;
7307 -- Initialize result first entry from left or right operand depending
7308 -- on which starts with the lower range.
7310 if Left (SLeft).Lo < Right (SRight).Lo then
7311 FEnt := Left (SLeft);
7312 SLeft := SLeft + 1;
7313 else
7314 FEnt := Right (SRight);
7315 SRight := SRight + 1;
7316 end if;
7318 -- This loop eats ranges from left and right operands that are
7319 -- contiguous with the first range we are gathering.
7321 loop
7322 -- Eat first entry in left operand if contiguous or overlapped by
7323 -- gathered first operand of result.
7325 if SLeft <= Left'Last
7326 and then Left (SLeft).Lo <= FEnt.Hi + 1
7327 then
7328 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7329 SLeft := SLeft + 1;
7331 -- Eat first entry in right operand if contiguous or overlapped by
7332 -- gathered right operand of result.
7334 elsif SRight <= Right'Last
7335 and then Right (SRight).Lo <= FEnt.Hi + 1
7336 then
7337 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7338 SRight := SRight + 1;
7340 -- All done if no more entries to eat
7342 else
7343 exit;
7344 end if;
7345 end loop;
7347 -- Obtain result as the first entry we just computed, concatenated
7348 -- to the "or" of the remaining results (if one operand is empty,
7349 -- this will just concatenate with the other
7351 return
7352 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7353 end "or";
7355 -----------------
7356 -- Build_Range --
7357 -----------------
7359 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7360 Result : Node_Id;
7361 begin
7362 Result :=
7363 Make_Range (Loc,
7364 Low_Bound => Build_Val (Lo),
7365 High_Bound => Build_Val (Hi));
7366 Set_Etype (Result, Btyp);
7367 Set_Analyzed (Result);
7368 return Result;
7369 end Build_Range;
7371 ---------------
7372 -- Build_Val --
7373 ---------------
7375 function Build_Val (V : Uint) return Node_Id is
7376 Result : Node_Id;
7378 begin
7379 if Is_Enumeration_Type (Typ) then
7380 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7381 else
7382 Result := Make_Integer_Literal (Loc, V);
7383 end if;
7385 Set_Etype (Result, Btyp);
7386 Set_Is_Static_Expression (Result);
7387 Set_Analyzed (Result);
7388 return Result;
7389 end Build_Val;
7391 ---------------
7392 -- Get_RList --
7393 ---------------
7395 function Get_RList (Exp : Node_Id) return RList is
7396 Op : Node_Kind;
7397 Val : Uint;
7399 begin
7400 -- Static expression can only be true or false
7402 if Is_OK_Static_Expression (Exp) then
7403 if Expr_Value (Exp) = 0 then
7404 return False_Range;
7405 else
7406 return True_Range;
7407 end if;
7408 end if;
7410 -- Otherwise test node type
7412 Op := Nkind (Exp);
7414 case Op is
7416 -- And
7418 when N_Op_And | N_And_Then =>
7419 return Get_RList (Left_Opnd (Exp))
7421 Get_RList (Right_Opnd (Exp));
7423 -- Or
7425 when N_Op_Or | N_Or_Else =>
7426 return Get_RList (Left_Opnd (Exp))
7428 Get_RList (Right_Opnd (Exp));
7430 -- Not
7432 when N_Op_Not =>
7433 return not Get_RList (Right_Opnd (Exp));
7435 -- Comparisons of type with static value
7437 when N_Op_Compare =>
7439 -- Type is left operand
7441 if Is_Type_Ref (Left_Opnd (Exp))
7442 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7443 then
7444 Val := Expr_Value (Right_Opnd (Exp));
7446 -- Typ is right operand
7448 elsif Is_Type_Ref (Right_Opnd (Exp))
7449 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7450 then
7451 Val := Expr_Value (Left_Opnd (Exp));
7453 -- Invert sense of comparison
7455 case Op is
7456 when N_Op_Gt => Op := N_Op_Lt;
7457 when N_Op_Lt => Op := N_Op_Gt;
7458 when N_Op_Ge => Op := N_Op_Le;
7459 when N_Op_Le => Op := N_Op_Ge;
7460 when others => null;
7461 end case;
7463 -- Other cases are non-static
7465 else
7466 raise Non_Static;
7467 end if;
7469 -- Construct range according to comparison operation
7471 case Op is
7472 when N_Op_Eq =>
7473 return RList'(1 => REnt'(Val, Val));
7475 when N_Op_Ge =>
7476 return RList'(1 => REnt'(Val, BHi));
7478 when N_Op_Gt =>
7479 return RList'(1 => REnt'(Val + 1, BHi));
7481 when N_Op_Le =>
7482 return RList'(1 => REnt'(BLo, Val));
7484 when N_Op_Lt =>
7485 return RList'(1 => REnt'(BLo, Val - 1));
7487 when N_Op_Ne =>
7488 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7490 when others =>
7491 raise Program_Error;
7492 end case;
7494 -- Membership (IN)
7496 when N_In =>
7497 if not Is_Type_Ref (Left_Opnd (Exp)) then
7498 raise Non_Static;
7499 end if;
7501 if Present (Right_Opnd (Exp)) then
7502 return Membership_Entry (Right_Opnd (Exp));
7503 else
7504 return Membership_Entries (First (Alternatives (Exp)));
7505 end if;
7507 -- Negative membership (NOT IN)
7509 when N_Not_In =>
7510 if not Is_Type_Ref (Left_Opnd (Exp)) then
7511 raise Non_Static;
7512 end if;
7514 if Present (Right_Opnd (Exp)) then
7515 return not Membership_Entry (Right_Opnd (Exp));
7516 else
7517 return not Membership_Entries (First (Alternatives (Exp)));
7518 end if;
7520 -- Function call, may be call to static predicate
7522 when N_Function_Call =>
7523 if Is_Entity_Name (Name (Exp)) then
7524 declare
7525 Ent : constant Entity_Id := Entity (Name (Exp));
7526 begin
7527 if Is_Predicate_Function (Ent)
7528 or else
7529 Is_Predicate_Function_M (Ent)
7530 then
7531 return Stat_Pred (Etype (First_Formal (Ent)));
7532 end if;
7533 end;
7534 end if;
7536 -- Other function call cases are non-static
7538 raise Non_Static;
7540 -- Qualified expression, dig out the expression
7542 when N_Qualified_Expression =>
7543 return Get_RList (Expression (Exp));
7545 when N_Case_Expression =>
7546 declare
7547 Alt : Node_Id;
7548 Choices : List_Id;
7549 Dep : Node_Id;
7551 begin
7552 if not Is_Entity_Name (Expression (Expr))
7553 or else Etype (Expression (Expr)) /= Typ
7554 then
7555 Error_Msg_N
7556 ("expression must denaote subtype", Expression (Expr));
7557 return False_Range;
7558 end if;
7560 -- Collect discrete choices in all True alternatives
7562 Choices := New_List;
7563 Alt := First (Alternatives (Exp));
7564 while Present (Alt) loop
7565 Dep := Expression (Alt);
7567 if not Is_OK_Static_Expression (Dep) then
7568 raise Non_Static;
7570 elsif Is_True (Expr_Value (Dep)) then
7571 Append_List_To (Choices,
7572 New_Copy_List (Discrete_Choices (Alt)));
7573 end if;
7575 Next (Alt);
7576 end loop;
7578 return Membership_Entries (First (Choices));
7579 end;
7581 -- Expression with actions: if no actions, dig out expression
7583 when N_Expression_With_Actions =>
7584 if Is_Empty_List (Actions (Exp)) then
7585 return Get_RList (Expression (Exp));
7586 else
7587 raise Non_Static;
7588 end if;
7590 -- Xor operator
7592 when N_Op_Xor =>
7593 return (Get_RList (Left_Opnd (Exp))
7594 and not Get_RList (Right_Opnd (Exp)))
7595 or (Get_RList (Right_Opnd (Exp))
7596 and not Get_RList (Left_Opnd (Exp)));
7598 -- Any other node type is non-static
7600 when others =>
7601 raise Non_Static;
7602 end case;
7603 end Get_RList;
7605 ------------
7606 -- Hi_Val --
7607 ------------
7609 function Hi_Val (N : Node_Id) return Uint is
7610 begin
7611 if Is_OK_Static_Expression (N) then
7612 return Expr_Value (N);
7613 else
7614 pragma Assert (Nkind (N) = N_Range);
7615 return Expr_Value (High_Bound (N));
7616 end if;
7617 end Hi_Val;
7619 --------------
7620 -- Is_False --
7621 --------------
7623 function Is_False (R : RList) return Boolean is
7624 begin
7625 return R'Length = 0;
7626 end Is_False;
7628 -------------
7629 -- Is_True --
7630 -------------
7632 function Is_True (R : RList) return Boolean is
7633 begin
7634 return R'Length = 1
7635 and then R (R'First).Lo = BLo
7636 and then R (R'First).Hi = BHi;
7637 end Is_True;
7639 -----------------
7640 -- Is_Type_Ref --
7641 -----------------
7643 function Is_Type_Ref (N : Node_Id) return Boolean is
7644 begin
7645 return Nkind (N) = N_Identifier
7646 and then Chars (N) = Nam
7647 and then Paren_Count (N) = 0;
7648 end Is_Type_Ref;
7650 ------------
7651 -- Lo_Val --
7652 ------------
7654 function Lo_Val (N : Node_Id) return Uint is
7655 begin
7656 if Is_OK_Static_Expression (N) then
7657 return Expr_Value (N);
7658 else
7659 pragma Assert (Nkind (N) = N_Range);
7660 return Expr_Value (Low_Bound (N));
7661 end if;
7662 end Lo_Val;
7664 ------------------------
7665 -- Membership_Entries --
7666 ------------------------
7668 function Membership_Entries (N : Node_Id) return RList is
7669 begin
7670 if No (Next (N)) then
7671 return Membership_Entry (N);
7672 else
7673 return Membership_Entry (N) or Membership_Entries (Next (N));
7674 end if;
7675 end Membership_Entries;
7677 ----------------------
7678 -- Membership_Entry --
7679 ----------------------
7681 function Membership_Entry (N : Node_Id) return RList is
7682 Val : Uint;
7683 SLo : Uint;
7684 SHi : Uint;
7686 begin
7687 -- Range case
7689 if Nkind (N) = N_Range then
7690 if not Is_OK_Static_Expression (Low_Bound (N))
7691 or else
7692 not Is_OK_Static_Expression (High_Bound (N))
7693 then
7694 raise Non_Static;
7695 else
7696 SLo := Expr_Value (Low_Bound (N));
7697 SHi := Expr_Value (High_Bound (N));
7698 return RList'(1 => REnt'(SLo, SHi));
7699 end if;
7701 -- Static expression case
7703 elsif Is_OK_Static_Expression (N) then
7704 Val := Expr_Value (N);
7705 return RList'(1 => REnt'(Val, Val));
7707 -- Identifier (other than static expression) case
7709 else pragma Assert (Nkind (N) = N_Identifier);
7711 -- Type case
7713 if Is_Type (Entity (N)) then
7715 -- If type has predicates, process them
7717 if Has_Predicates (Entity (N)) then
7718 return Stat_Pred (Entity (N));
7720 -- For static subtype without predicates, get range
7722 elsif Is_OK_Static_Subtype (Entity (N)) then
7723 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7724 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7725 return RList'(1 => REnt'(SLo, SHi));
7727 -- Any other type makes us non-static
7729 else
7730 raise Non_Static;
7731 end if;
7733 -- Any other kind of identifier in predicate (e.g. a non-static
7734 -- expression value) means this is not a static predicate.
7736 else
7737 raise Non_Static;
7738 end if;
7739 end if;
7740 end Membership_Entry;
7742 ---------------
7743 -- Stat_Pred --
7744 ---------------
7746 function Stat_Pred (Typ : Entity_Id) return RList is
7747 begin
7748 -- Not static if type does not have static predicates
7750 if not Has_Static_Predicate (Typ) then
7751 raise Non_Static;
7752 end if;
7754 -- Otherwise we convert the predicate list to a range list
7756 declare
7757 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7758 Result : RList (1 .. List_Length (Spred));
7759 P : Node_Id;
7761 begin
7762 P := First (Static_Discrete_Predicate (Typ));
7763 for J in Result'Range loop
7764 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7765 Next (P);
7766 end loop;
7768 return Result;
7769 end;
7770 end Stat_Pred;
7772 -- Start of processing for Build_Discrete_Static_Predicate
7774 begin
7775 -- Establish bounds for the predicate
7777 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7778 TLo := Expr_Value (Type_Low_Bound (Typ));
7779 else
7780 TLo := BLo;
7781 end if;
7783 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7784 THi := Expr_Value (Type_High_Bound (Typ));
7785 else
7786 THi := BHi;
7787 end if;
7789 -- Analyze the expression to see if it is a static predicate
7791 declare
7792 Ranges : constant RList := Get_RList (Expr);
7793 -- Range list from expression if it is static
7795 Plist : List_Id;
7797 begin
7798 -- Convert range list into a form for the static predicate. In the
7799 -- Ranges array, we just have raw ranges, these must be converted
7800 -- to properly typed and analyzed static expressions or range nodes.
7802 -- Note: here we limit ranges to the ranges of the subtype, so that
7803 -- a predicate is always false for values outside the subtype. That
7804 -- seems fine, such values are invalid anyway, and considering them
7805 -- to fail the predicate seems allowed and friendly, and furthermore
7806 -- simplifies processing for case statements and loops.
7808 Plist := New_List;
7810 for J in Ranges'Range loop
7811 declare
7812 Lo : Uint := Ranges (J).Lo;
7813 Hi : Uint := Ranges (J).Hi;
7815 begin
7816 -- Ignore completely out of range entry
7818 if Hi < TLo or else Lo > THi then
7819 null;
7821 -- Otherwise process entry
7823 else
7824 -- Adjust out of range value to subtype range
7826 if Lo < TLo then
7827 Lo := TLo;
7828 end if;
7830 if Hi > THi then
7831 Hi := THi;
7832 end if;
7834 -- Convert range into required form
7836 Append_To (Plist, Build_Range (Lo, Hi));
7837 end if;
7838 end;
7839 end loop;
7841 -- Processing was successful and all entries were static, so now we
7842 -- can store the result as the predicate list.
7844 Set_Static_Discrete_Predicate (Typ, Plist);
7846 -- The processing for static predicates put the expression into
7847 -- canonical form as a series of ranges. It also eliminated
7848 -- duplicates and collapsed and combined ranges. We might as well
7849 -- replace the alternatives list of the right operand of the
7850 -- membership test with the static predicate list, which will
7851 -- usually be more efficient.
7853 declare
7854 New_Alts : constant List_Id := New_List;
7855 Old_Node : Node_Id;
7856 New_Node : Node_Id;
7858 begin
7859 Old_Node := First (Plist);
7860 while Present (Old_Node) loop
7861 New_Node := New_Copy (Old_Node);
7863 if Nkind (New_Node) = N_Range then
7864 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7865 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7866 end if;
7868 Append_To (New_Alts, New_Node);
7869 Next (Old_Node);
7870 end loop;
7872 -- If empty list, replace by False
7874 if Is_Empty_List (New_Alts) then
7875 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7877 -- Else replace by set membership test
7879 else
7880 Rewrite (Expr,
7881 Make_In (Loc,
7882 Left_Opnd => Make_Identifier (Loc, Nam),
7883 Right_Opnd => Empty,
7884 Alternatives => New_Alts));
7886 -- Resolve new expression in function context
7888 Install_Formals (Predicate_Function (Typ));
7889 Push_Scope (Predicate_Function (Typ));
7890 Analyze_And_Resolve (Expr, Standard_Boolean);
7891 Pop_Scope;
7892 end if;
7893 end;
7894 end;
7896 -- If non-static, return doing nothing
7898 exception
7899 when Non_Static =>
7900 return;
7901 end Build_Discrete_Static_Predicate;
7903 --------------------------------
7904 -- Build_Export_Import_Pragma --
7905 --------------------------------
7907 function Build_Export_Import_Pragma
7908 (Asp : Node_Id;
7909 Id : Entity_Id) return Node_Id
7911 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
7912 Expr : constant Node_Id := Expression (Asp);
7913 Loc : constant Source_Ptr := Sloc (Asp);
7915 Args : List_Id;
7916 Conv : Node_Id;
7917 Conv_Arg : Node_Id;
7918 Dummy_1 : Node_Id;
7919 Dummy_2 : Node_Id;
7920 EN : Node_Id;
7921 LN : Node_Id;
7922 Prag : Node_Id;
7924 Create_Pragma : Boolean := False;
7925 -- This flag is set when the aspect form is such that it warrants the
7926 -- creation of a corresponding pragma.
7928 begin
7929 if Present (Expr) then
7930 if Error_Posted (Expr) then
7931 null;
7933 elsif Is_True (Expr_Value (Expr)) then
7934 Create_Pragma := True;
7935 end if;
7937 -- Otherwise the aspect defaults to True
7939 else
7940 Create_Pragma := True;
7941 end if;
7943 -- Nothing to do when the expression is False or is erroneous
7945 if not Create_Pragma then
7946 return Empty;
7947 end if;
7949 -- Obtain all interfacing aspects that apply to the related entity
7951 Get_Interfacing_Aspects
7952 (Iface_Asp => Asp,
7953 Conv_Asp => Conv,
7954 EN_Asp => EN,
7955 Expo_Asp => Dummy_1,
7956 Imp_Asp => Dummy_2,
7957 LN_Asp => LN);
7959 Args := New_List;
7961 -- Handle the convention argument
7963 if Present (Conv) then
7964 Conv_Arg := New_Copy_Tree (Expression (Conv));
7966 -- Assume convention "Ada' when aspect Convention is missing
7968 else
7969 Conv_Arg := Make_Identifier (Loc, Name_Ada);
7970 end if;
7972 Append_To (Args,
7973 Make_Pragma_Argument_Association (Loc,
7974 Chars => Name_Convention,
7975 Expression => Conv_Arg));
7977 -- Handle the entity argument
7979 Append_To (Args,
7980 Make_Pragma_Argument_Association (Loc,
7981 Chars => Name_Entity,
7982 Expression => New_Occurrence_Of (Id, Loc)));
7984 -- Handle the External_Name argument
7986 if Present (EN) then
7987 Append_To (Args,
7988 Make_Pragma_Argument_Association (Loc,
7989 Chars => Name_External_Name,
7990 Expression => New_Copy_Tree (Expression (EN))));
7991 end if;
7993 -- Handle the Link_Name argument
7995 if Present (LN) then
7996 Append_To (Args,
7997 Make_Pragma_Argument_Association (Loc,
7998 Chars => Name_Link_Name,
7999 Expression => New_Copy_Tree (Expression (LN))));
8000 end if;
8002 -- Generate:
8003 -- pragma Export/Import
8004 -- (Convention => <Conv>/Ada,
8005 -- Entity => <Id>,
8006 -- [External_Name => <EN>,]
8007 -- [Link_Name => <LN>]);
8009 Prag :=
8010 Make_Pragma (Loc,
8011 Pragma_Identifier =>
8012 Make_Identifier (Loc, Chars (Identifier (Asp))),
8013 Pragma_Argument_Associations => Args);
8015 -- Decorate the relevant aspect and the pragma
8017 Set_Aspect_Rep_Item (Asp, Prag);
8019 Set_Corresponding_Aspect (Prag, Asp);
8020 Set_From_Aspect_Specification (Prag);
8021 Set_Parent (Prag, Asp);
8023 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8024 Set_Import_Pragma (Id, Prag);
8025 end if;
8027 return Prag;
8028 end Build_Export_Import_Pragma;
8030 -------------------------------------------
8031 -- Build_Invariant_Procedure_Declaration --
8032 -------------------------------------------
8034 function Build_Invariant_Procedure_Declaration
8035 (Typ : Entity_Id) return Node_Id
8037 Loc : constant Source_Ptr := Sloc (Typ);
8038 Decl : Node_Id;
8039 Obj_Id : Entity_Id;
8040 SId : Entity_Id;
8042 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8044 begin
8045 -- Check for duplicate definitions
8047 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
8048 return Empty;
8049 end if;
8051 -- The related type may be subject to pragma Ghost. Set the mode now to
8052 -- ensure that the invariant procedure is properly marked as Ghost.
8054 Set_Ghost_Mode_From_Entity (Typ);
8056 SId :=
8057 Make_Defining_Identifier (Loc,
8058 Chars => New_External_Name (Chars (Typ), "Invariant"));
8059 Set_Has_Invariants (Typ);
8060 Set_Ekind (SId, E_Procedure);
8061 Set_Etype (SId, Standard_Void_Type);
8062 Set_Is_Invariant_Procedure (SId);
8063 Set_Invariant_Procedure (Typ, SId);
8065 -- Source Coverage Obligations might be attached to the invariant
8066 -- expression this procedure evaluates, and we need debug info to be
8067 -- able to assess the coverage achieved by evaluations.
8069 if Opt.Generate_SCO then
8070 Set_Needs_Debug_Info (SId);
8071 end if;
8073 -- Mark the invariant procedure explicitly as Ghost because it does not
8074 -- come from source.
8076 if Ghost_Mode > None then
8077 Set_Is_Ghost_Entity (SId);
8078 end if;
8080 Obj_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
8081 Set_Etype (Obj_Id, Typ);
8083 Decl :=
8084 Make_Subprogram_Declaration (Loc,
8085 Make_Procedure_Specification (Loc,
8086 Defining_Unit_Name => SId,
8087 Parameter_Specifications => New_List (
8088 Make_Parameter_Specification (Loc,
8089 Defining_Identifier => Obj_Id,
8090 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
8092 Ghost_Mode := Save_Ghost_Mode;
8094 return Decl;
8095 end Build_Invariant_Procedure_Declaration;
8097 -------------------------------
8098 -- Build_Invariant_Procedure --
8099 -------------------------------
8101 -- The procedure that is constructed here has the form
8103 -- procedure typInvariant (Ixxx : typ) is
8104 -- begin
8105 -- pragma Check (Invariant, exp, "failed invariant from xxx");
8106 -- pragma Check (Invariant, exp, "failed invariant from xxx");
8107 -- ...
8108 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
8109 -- ...
8110 -- end typInvariant;
8112 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
8113 procedure Add_Invariants
8114 (T : Entity_Id;
8115 Obj_Id : Entity_Id;
8116 Stmts : in out List_Id;
8117 Inherit : Boolean);
8118 -- Appends statements to Stmts for any invariants in the rep item chain
8119 -- of the given type. If Inherit is False, then we only process entries
8120 -- on the chain for the type Typ. If Inherit is True, then we ignore any
8121 -- Invariant aspects, but we process all Invariant'Class aspects, adding
8122 -- "inherited" to the exception message and generating an informational
8123 -- message about the inheritance of an invariant.
8125 --------------------
8126 -- Add_Invariants --
8127 --------------------
8129 procedure Add_Invariants
8130 (T : Entity_Id;
8131 Obj_Id : Entity_Id;
8132 Stmts : in out List_Id;
8133 Inherit : Boolean)
8135 procedure Add_Invariant (Prag : Node_Id);
8136 -- Create a runtime check to verify the exression of invariant pragma
8137 -- Prag. All generated code is added to list Stmts.
8139 -------------------
8140 -- Add_Invariant --
8141 -------------------
8143 procedure Add_Invariant (Prag : Node_Id) is
8144 procedure Replace_Type_Reference (N : Node_Id);
8145 -- Replace a single occurrence N of the subtype name with a
8146 -- reference to the formal of the predicate function. N can be an
8147 -- identifier referencing the subtype, or a selected component,
8148 -- representing an appropriately qualified occurrence of the
8149 -- subtype name.
8151 procedure Replace_Type_References is
8152 new Replace_Type_References_Generic (Replace_Type_Reference);
8153 -- Traverse an expression replacing all occurrences of the subtype
8154 -- name with appropriate references to the formal of the predicate
8155 -- function. Note that we must ensure that the type and entity
8156 -- information is properly set in the replacement node, since we
8157 -- will do a Preanalyze call of this expression without proper
8158 -- visibility of the procedure argument.
8160 ----------------------------
8161 -- Replace_Type_Reference --
8162 ----------------------------
8164 -- Note: See comments in Add_Predicates.Replace_Type_Reference
8165 -- regarding handling of Sloc and Comes_From_Source.
8167 procedure Replace_Type_Reference (N : Node_Id) is
8168 Nloc : constant Source_Ptr := Sloc (N);
8170 begin
8171 -- Add semantic information to node to be rewritten, for ASIS
8172 -- navigation needs.
8174 if Nkind (N) = N_Identifier then
8175 Set_Entity (N, T);
8176 Set_Etype (N, T);
8178 elsif Nkind (N) = N_Selected_Component then
8179 Analyze (Prefix (N));
8180 Set_Entity (Selector_Name (N), T);
8181 Set_Etype (Selector_Name (N), T);
8182 end if;
8184 -- Invariant'Class, replace with T'Class (obj)
8186 if Class_Present (Prag) then
8188 -- In ASIS mode, an inherited item is already analyzed,
8189 -- and the replacement has been done, so do not repeat
8190 -- the transformation to prevent a malformed tree.
8192 if ASIS_Mode
8193 and then Nkind (Parent (N)) = N_Attribute_Reference
8194 and then Attribute_Name (Parent (N)) = Name_Class
8195 then
8196 null;
8198 else
8199 Rewrite (N,
8200 Make_Type_Conversion (Nloc,
8201 Subtype_Mark =>
8202 Make_Attribute_Reference (Nloc,
8203 Prefix => New_Occurrence_Of (T, Nloc),
8204 Attribute_Name => Name_Class),
8205 Expression =>
8206 Make_Identifier (Nloc, Chars (Obj_Id))));
8208 Set_Entity (Expression (N), Obj_Id);
8209 Set_Etype (Expression (N), Typ);
8210 end if;
8212 -- Invariant, replace with obj
8214 else
8215 Rewrite (N, Make_Identifier (Nloc, Chars (Obj_Id)));
8216 Set_Entity (N, Obj_Id);
8217 Set_Etype (N, Typ);
8218 end if;
8220 Set_Comes_From_Source (N, True);
8221 end Replace_Type_Reference;
8223 -- Local variables
8225 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8226 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
8227 Ploc : constant Source_Ptr := Sloc (Prag);
8228 Arg1 : Node_Id;
8229 Arg2 : Node_Id;
8230 Arg3 : Node_Id;
8231 Assoc : List_Id;
8232 Expr : Node_Id;
8233 Str : String_Id;
8235 -- Start of processing for Add_Invariant
8237 begin
8238 -- Extract the arguments of the invariant pragma
8240 Arg1 := First (Pragma_Argument_Associations (Prag));
8241 Arg2 := Next (Arg1);
8242 Arg3 := Next (Arg2);
8244 Arg1 := Get_Pragma_Arg (Arg1);
8245 Arg2 := Get_Pragma_Arg (Arg2);
8247 -- The caller requests processing of all Invariant'Class pragmas,
8248 -- but the current pragma does not fall in this category. Return
8249 -- as there is nothing left to do.
8251 if Inherit then
8252 if not Class_Present (Prag) then
8253 return;
8254 end if;
8256 -- Otherwise the pragma must apply to the current type
8258 elsif Entity (Arg1) /= T then
8259 return;
8260 end if;
8262 Expr := New_Copy_Tree (Arg2);
8264 -- Replace all occurrences of the type's name with references to
8265 -- the formal parameter of the invariant procedure.
8267 Replace_Type_References (Expr, T);
8269 -- If the invariant pragma comes from an aspect, replace the saved
8270 -- expression because we need the subtype references replaced for
8271 -- the calls to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
8272 -- routines. This is not done for interited class-wide invariants
8273 -- because the original pragma of the parent type must remain
8274 -- unchanged.
8276 if not Inherit and then Present (Asp) then
8277 Set_Entity (Identifier (Asp), New_Copy_Tree (Expr));
8278 end if;
8280 -- Preanalyze the invariant expression to capture the visibility
8281 -- of the proper package part. In general the expression is not
8282 -- fully analyzed until the body of the invariant procedure is
8283 -- analyzed at the end of the private part, but that yields the
8284 -- wrong visibility.
8286 -- Historical note: we used to set N as the parent, but a package
8287 -- specification as the parent of an expression is bizarre.
8289 Set_Parent (Expr, Parent (Arg2));
8290 Preanalyze_Assert_Expression (Expr, Any_Boolean);
8292 -- Both modifications performed below are not done for inherited
8293 -- class-wide invariants because the origial aspect/pragma of the
8294 -- parent type must remain unchanged.
8296 if not Inherit then
8298 -- A class-wide invariant may be inherited in a separate unit,
8299 -- where the corresponding expression cannot be resolved by
8300 -- visibility, because it refers to a local function. Propagate
8301 -- semantic information to the original representation item, to
8302 -- be used when an invariant procedure for a derived type is
8303 -- constructed.
8305 -- ??? Unclear how to handle class-wide invariants that are not
8306 -- function calls.
8308 if Class_Present (Prag)
8309 and then Nkind (Expr) = N_Function_Call
8310 and then Nkind (Arg2) = N_Indexed_Component
8311 then
8312 Rewrite (Arg2,
8313 Make_Function_Call (Ploc,
8314 Name =>
8315 New_Occurrence_Of (Entity (Name (Expr)), Ploc),
8316 Parameter_Associations => Expressions (Arg2)));
8317 end if;
8319 -- In ASIS mode, even if assertions are not enabled, we must
8320 -- analyze the original expression in the aspect specification
8321 -- because it is part of the original tree.
8323 if ASIS_Mode and then Present (Asp) then
8324 declare
8325 Asp_Expr : constant Node_Id := Expression (Asp);
8327 begin
8328 Replace_Type_References (Asp_Expr, T);
8329 Preanalyze_Assert_Expression (Asp_Expr, Any_Boolean);
8330 end;
8331 end if;
8332 end if;
8334 -- An ignored invariant must not generate a runtime check. Add a
8335 -- null statement to ensure that the invariant procedure does get
8336 -- a completing body.
8338 if No (Stmts) then
8339 Stmts := Empty_List;
8340 end if;
8342 if Is_Ignored (Prag) then
8343 Append_To (Stmts, Make_Null_Statement (Ploc));
8345 -- Otherwise the invariant is checked. Build a Check pragma to
8346 -- verify the expression at runtime.
8348 else
8349 Assoc := New_List (
8350 Make_Pragma_Argument_Association (Ploc,
8351 Expression => Make_Identifier (Ploc, Nam)),
8352 Make_Pragma_Argument_Association (Ploc,
8353 Expression => Expr));
8355 -- Handle the String argument (if any)
8357 if Present (Arg3) then
8358 Str := Strval (Get_Pragma_Arg (Arg3));
8360 -- When inheriting an invariant, modify the message from
8361 -- "failed invariant" to "failed inherited invariant".
8363 if Inherit then
8364 String_To_Name_Buffer (Str);
8366 if Name_Buffer (1 .. 16) = "failed invariant" then
8367 Insert_Str_In_Name_Buffer ("inherited ", 8);
8368 Str := String_From_Name_Buffer;
8369 end if;
8370 end if;
8372 Append_To (Assoc,
8373 Make_Pragma_Argument_Association (Ploc,
8374 Expression => Make_String_Literal (Ploc, Str)));
8375 end if;
8377 -- Generate:
8378 -- pragma Check (Nam, Expr, Str);
8380 Append_To (Stmts,
8381 Make_Pragma (Ploc,
8382 Pragma_Identifier =>
8383 Make_Identifier (Ploc, Name_Check),
8384 Pragma_Argument_Associations => Assoc));
8385 end if;
8387 -- Output an info message when inheriting an invariant and the
8388 -- listing option is enabled.
8390 if Inherit and Opt.List_Inherited_Aspects then
8391 Error_Msg_Sloc := Sloc (Prag);
8392 Error_Msg_N
8393 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
8394 end if;
8395 end Add_Invariant;
8397 -- Local variables
8399 Ritem : Node_Id;
8401 -- Start of processing for Add_Invariants
8403 begin
8404 Ritem := First_Rep_Item (T);
8405 while Present (Ritem) loop
8406 if Nkind (Ritem) = N_Pragma
8407 and then Pragma_Name (Ritem) = Name_Invariant
8408 then
8409 Add_Invariant (Ritem);
8410 end if;
8412 Next_Rep_Item (Ritem);
8413 end loop;
8414 end Add_Invariants;
8416 -- Local variables
8418 Loc : constant Source_Ptr := Sloc (Typ);
8419 Priv_Decls : constant List_Id := Private_Declarations (N);
8420 Vis_Decls : constant List_Id := Visible_Declarations (N);
8422 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8424 PBody : Node_Id;
8425 PDecl : Node_Id;
8426 SId : Entity_Id;
8427 Spec : Node_Id;
8428 Stmts : List_Id;
8430 Obj_Id : Node_Id;
8431 -- The entity of the formal for the procedure
8433 -- Start of processing for Build_Invariant_Procedure
8435 begin
8436 -- The related type may be subject to pragma Ghost. Set the mode now to
8437 -- ensure that the invariant procedure is properly marked as Ghost.
8439 Set_Ghost_Mode_From_Entity (Typ);
8441 Stmts := No_List;
8442 PDecl := Empty;
8443 PBody := Empty;
8444 SId := Empty;
8446 -- If the aspect specification exists for some view of the type, the
8447 -- declaration for the procedure has been created.
8449 if Has_Invariants (Typ) then
8450 SId := Invariant_Procedure (Typ);
8451 end if;
8453 -- If the body is already present, nothing to do. This will occur when
8454 -- the type is already frozen, which is the case when the invariant
8455 -- appears in a private part, and the freezing takes place before the
8456 -- final pass over full declarations.
8458 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
8460 if Present (SId) then
8461 PDecl := Unit_Declaration_Node (SId);
8463 if Present (PDecl)
8464 and then Nkind (PDecl) = N_Subprogram_Declaration
8465 and then Present (Corresponding_Body (PDecl))
8466 then
8467 Ghost_Mode := Save_Ghost_Mode;
8468 return;
8469 end if;
8471 else
8472 PDecl := Build_Invariant_Procedure_Declaration (Typ);
8473 end if;
8475 -- Recover formal of procedure, for use in the calls to invariant
8476 -- functions (including inherited ones).
8478 Obj_Id :=
8479 Defining_Identifier
8480 (First (Parameter_Specifications (Specification (PDecl))));
8482 -- Add invariants for the current type
8484 Add_Invariants
8485 (T => Typ,
8486 Obj_Id => Obj_Id,
8487 Stmts => Stmts,
8488 Inherit => False);
8490 -- Add invariants for parent types
8492 declare
8493 Current_Typ : Entity_Id;
8494 Parent_Typ : Entity_Id;
8496 begin
8497 Current_Typ := Typ;
8498 loop
8499 Parent_Typ := Etype (Current_Typ);
8501 if Is_Private_Type (Parent_Typ)
8502 and then Present (Full_View (Base_Type (Parent_Typ)))
8503 then
8504 Parent_Typ := Full_View (Base_Type (Parent_Typ));
8505 end if;
8507 exit when Parent_Typ = Current_Typ;
8509 Current_Typ := Parent_Typ;
8510 Add_Invariants
8511 (T => Current_Typ,
8512 Obj_Id => Obj_Id,
8513 Stmts => Stmts,
8514 Inherit => True);
8515 end loop;
8516 end;
8518 -- Add invariants of progenitors
8520 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
8521 declare
8522 Ifaces_List : Elist_Id;
8523 AI : Elmt_Id;
8524 Iface : Entity_Id;
8526 begin
8527 Collect_Interfaces (Typ, Ifaces_List);
8529 AI := First_Elmt (Ifaces_List);
8530 while Present (AI) loop
8531 Iface := Node (AI);
8533 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8534 Add_Invariants
8535 (T => Iface,
8536 Obj_Id => Obj_Id,
8537 Stmts => Stmts,
8538 Inherit => True);
8539 end if;
8541 Next_Elmt (AI);
8542 end loop;
8543 end;
8544 end if;
8546 -- Build the procedure if we generated at least one Check pragma
8548 if Stmts /= No_List then
8549 Spec := Copy_Separate_Tree (Specification (PDecl));
8551 PBody :=
8552 Make_Subprogram_Body (Loc,
8553 Specification => Spec,
8554 Declarations => Empty_List,
8555 Handled_Statement_Sequence =>
8556 Make_Handled_Sequence_Of_Statements (Loc,
8557 Statements => Stmts));
8559 -- The processing of an invariant pragma immediately generates the
8560 -- invariant procedure spec, inserts it into the tree, and analyzes
8561 -- it. If the spec has not been analyzed, then the invariant pragma
8562 -- is being inherited and requires manual insertion and analysis.
8564 if not Analyzed (PDecl) then
8565 Append_To (Vis_Decls, PDecl);
8566 Analyze (PDecl);
8567 end if;
8569 -- The invariant procedure body is inserted at the end of the private
8570 -- declarations.
8572 if Present (Priv_Decls) then
8573 Append_To (Priv_Decls, PBody);
8575 -- If the invariant appears on the full view of a private type,
8576 -- then the analysis of the private part is already completed.
8577 -- Manually analyze the new body in this case, otherwise wait
8578 -- for the analysis of the private declarations to process the
8579 -- body.
8581 if In_Private_Part (Current_Scope) then
8582 Analyze (PBody);
8583 end if;
8585 -- Otherwise there are no private declarations. This is either an
8586 -- error or the related type is a private extension, in which case
8587 -- it does not need a completion in a private part. Insert the body
8588 -- at the end of the visible declarations and analyze immediately
8589 -- because the related type is about to be frozen.
8591 else
8592 Append_To (Vis_Decls, PBody);
8593 Analyze (PBody);
8594 end if;
8595 end if;
8597 Ghost_Mode := Save_Ghost_Mode;
8598 end Build_Invariant_Procedure;
8600 -------------------------------
8601 -- Build_Predicate_Functions --
8602 -------------------------------
8604 -- The procedures that are constructed here have the form:
8606 -- function typPredicate (Ixxx : typ) return Boolean is
8607 -- begin
8608 -- return
8609 -- typ1Predicate (typ1 (Ixxx))
8610 -- and then typ2Predicate (typ2 (Ixxx))
8611 -- and then ...;
8612 -- exp1 and then exp2 and then ...
8613 -- end typPredicate;
8615 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8616 -- this is the point at which these expressions get analyzed, providing the
8617 -- required delay, and typ1, typ2, are entities from which predicates are
8618 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8619 -- use this function even if checks are off, e.g. for membership tests.
8621 -- Note that the inherited predicates are evaluated first, as required by
8622 -- AI12-0071-1.
8624 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8625 -- the form of this return expression.
8627 -- If the expression has at least one Raise_Expression, then we also build
8628 -- the typPredicateM version of the function, in which any occurrence of a
8629 -- Raise_Expression is converted to "return False".
8631 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8632 Loc : constant Source_Ptr := Sloc (Typ);
8634 Expr : Node_Id;
8635 -- This is the expression for the result of the function. It is
8636 -- is build by connecting the component predicates with AND THEN.
8638 Expr_M : Node_Id;
8639 -- This is the corresponding return expression for the Predicate_M
8640 -- function. It differs in that raise expressions are marked for
8641 -- special expansion (see Process_REs).
8643 Object_Name : Name_Id;
8644 -- Name for argument of Predicate procedure. Note that we use the same
8645 -- name for both predicate functions. That way the reference within the
8646 -- predicate expression is the same in both functions.
8648 Object_Entity : Entity_Id;
8649 -- Entity for argument of Predicate procedure
8651 Object_Entity_M : Entity_Id;
8652 -- Entity for argument of separate Predicate procedure when exceptions
8653 -- are present in expression.
8655 FDecl : Node_Id;
8656 -- The function declaration
8658 SId : Entity_Id;
8659 -- Its entity
8661 Raise_Expression_Present : Boolean := False;
8662 -- Set True if Expr has at least one Raise_Expression
8664 procedure Add_Condition (Cond : Node_Id);
8665 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8666 -- Expr is empty).
8668 procedure Add_Predicates;
8669 -- Appends expressions for any Predicate pragmas in the rep item chain
8670 -- Typ to Expr. Note that we look only at items for this exact entity.
8671 -- Inheritance of predicates for the parent type is done by calling the
8672 -- Predicate_Function of the parent type, using Add_Call above.
8674 procedure Add_Call (T : Entity_Id);
8675 -- Includes a call to the predicate function for type T in Expr if T
8676 -- has predicates and Predicate_Function (T) is non-empty.
8678 function Process_RE (N : Node_Id) return Traverse_Result;
8679 -- Used in Process REs, tests if node N is a raise expression, and if
8680 -- so, marks it to be converted to return False.
8682 procedure Process_REs is new Traverse_Proc (Process_RE);
8683 -- Marks any raise expressions in Expr_M to return False
8685 function Test_RE (N : Node_Id) return Traverse_Result;
8686 -- Used in Test_REs, tests one node for being a raise expression, and if
8687 -- so sets Raise_Expression_Present True.
8689 procedure Test_REs is new Traverse_Proc (Test_RE);
8690 -- Tests to see if Expr contains any raise expressions
8692 --------------
8693 -- Add_Call --
8694 --------------
8696 procedure Add_Call (T : Entity_Id) is
8697 Exp : Node_Id;
8699 begin
8700 if Present (T) and then Present (Predicate_Function (T)) then
8701 Set_Has_Predicates (Typ);
8703 -- Build the call to the predicate function of T
8705 Exp :=
8706 Make_Predicate_Call
8707 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8709 -- "and"-in the call to evolving expression
8711 Add_Condition (Exp);
8713 -- Output info message on inheritance if required. Note we do not
8714 -- give this information for generic actual types, since it is
8715 -- unwelcome noise in that case in instantiations. We also
8716 -- generally suppress the message in instantiations, and also
8717 -- if it involves internal names.
8719 if Opt.List_Inherited_Aspects
8720 and then not Is_Generic_Actual_Type (Typ)
8721 and then Instantiation_Depth (Sloc (Typ)) = 0
8722 and then not Is_Internal_Name (Chars (T))
8723 and then not Is_Internal_Name (Chars (Typ))
8724 then
8725 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8726 Error_Msg_Node_2 := T;
8727 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8728 end if;
8729 end if;
8730 end Add_Call;
8732 -------------------
8733 -- Add_Condition --
8734 -------------------
8736 procedure Add_Condition (Cond : Node_Id) is
8737 begin
8738 -- This is the first predicate expression
8740 if No (Expr) then
8741 Expr := Cond;
8743 -- Otherwise concatenate to the existing predicate expressions by
8744 -- using "and then".
8746 else
8747 Expr :=
8748 Make_And_Then (Loc,
8749 Left_Opnd => Relocate_Node (Expr),
8750 Right_Opnd => Cond);
8751 end if;
8752 end Add_Condition;
8754 --------------------
8755 -- Add_Predicates --
8756 --------------------
8758 procedure Add_Predicates is
8759 procedure Add_Predicate (Prag : Node_Id);
8760 -- Concatenate the expression of predicate pragma Prag to Expr by
8761 -- using a short circuit "and then" operator.
8763 -------------------
8764 -- Add_Predicate --
8765 -------------------
8767 procedure Add_Predicate (Prag : Node_Id) is
8768 procedure Replace_Type_Reference (N : Node_Id);
8769 -- Replace a single occurrence N of the subtype name with a
8770 -- reference to the formal of the predicate function. N can be an
8771 -- identifier referencing the subtype, or a selected component,
8772 -- representing an appropriately qualified occurrence of the
8773 -- subtype name.
8775 procedure Replace_Type_References is
8776 new Replace_Type_References_Generic (Replace_Type_Reference);
8777 -- Traverse an expression changing every occurrence of an
8778 -- identifier whose name matches the name of the subtype with a
8779 -- reference to the formal parameter of the predicate function.
8781 ----------------------------
8782 -- Replace_Type_Reference --
8783 ----------------------------
8785 procedure Replace_Type_Reference (N : Node_Id) is
8786 begin
8787 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8788 -- Use the Sloc of the usage name, not the defining name
8790 Set_Etype (N, Typ);
8791 Set_Entity (N, Object_Entity);
8793 -- We want to treat the node as if it comes from source, so
8794 -- that ASIS will not ignore it.
8796 Set_Comes_From_Source (N, True);
8797 end Replace_Type_Reference;
8799 -- Local variables
8801 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8802 Arg1 : Node_Id;
8803 Arg2 : Node_Id;
8805 -- Start of processing for Add_Predicate
8807 begin
8808 -- Extract the arguments of the pragma. The expression itself
8809 -- is copied for use in the predicate function, to preserve the
8810 -- original version for ASIS use.
8812 Arg1 := First (Pragma_Argument_Associations (Prag));
8813 Arg2 := Next (Arg1);
8815 Arg1 := Get_Pragma_Arg (Arg1);
8816 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
8818 -- When the predicate pragma applies to the current type or its
8819 -- full view, replace all occurrences of the subtype name with
8820 -- references to the formal parameter of the predicate function.
8822 if Entity (Arg1) = Typ
8823 or else Full_View (Entity (Arg1)) = Typ
8824 then
8825 Replace_Type_References (Arg2, Typ);
8827 -- If the predicate pragma comes from an aspect, replace the
8828 -- saved expression because we need the subtype references
8829 -- replaced for the calls to Preanalyze_Spec_Expression in
8830 -- Check_Aspect_At_xxx routines.
8832 if Present (Asp) then
8833 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8834 end if;
8836 -- "and"-in the Arg2 condition to evolving expression
8838 Add_Condition (Relocate_Node (Arg2));
8839 end if;
8840 end Add_Predicate;
8842 -- Local variables
8844 Ritem : Node_Id;
8846 -- Start of processing for Add_Predicates
8848 begin
8849 Ritem := First_Rep_Item (Typ);
8850 while Present (Ritem) loop
8851 if Nkind (Ritem) = N_Pragma
8852 and then Pragma_Name (Ritem) = Name_Predicate
8853 then
8854 Add_Predicate (Ritem);
8856 -- If the type is declared in an inner package it may be frozen
8857 -- outside of the package, and the generated pragma has not been
8858 -- analyzed yet, so capture the expression for the predicate
8859 -- function at this point.
8861 elsif Nkind (Ritem) = N_Aspect_Specification
8862 and then Present (Aspect_Rep_Item (Ritem))
8863 and then Scope (Typ) /= Current_Scope
8864 then
8865 declare
8866 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8868 begin
8869 if Nkind (Prag) = N_Pragma
8870 and then Pragma_Name (Prag) = Name_Predicate
8871 then
8872 Add_Predicate (Prag);
8873 end if;
8874 end;
8875 end if;
8877 Next_Rep_Item (Ritem);
8878 end loop;
8879 end Add_Predicates;
8881 ----------------
8882 -- Process_RE --
8883 ----------------
8885 function Process_RE (N : Node_Id) return Traverse_Result is
8886 begin
8887 if Nkind (N) = N_Raise_Expression then
8888 Set_Convert_To_Return_False (N);
8889 return Skip;
8890 else
8891 return OK;
8892 end if;
8893 end Process_RE;
8895 -------------
8896 -- Test_RE --
8897 -------------
8899 function Test_RE (N : Node_Id) return Traverse_Result is
8900 begin
8901 if Nkind (N) = N_Raise_Expression then
8902 Raise_Expression_Present := True;
8903 return Abandon;
8904 else
8905 return OK;
8906 end if;
8907 end Test_RE;
8909 -- Local variables
8911 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8913 -- Start of processing for Build_Predicate_Functions
8915 begin
8916 -- Return if already built or if type does not have predicates
8918 SId := Predicate_Function (Typ);
8919 if not Has_Predicates (Typ)
8920 or else (Present (SId) and then Has_Completion (SId))
8921 then
8922 return;
8923 end if;
8925 -- The related type may be subject to pragma Ghost. Set the mode now to
8926 -- ensure that the predicate functions are properly marked as Ghost.
8928 Set_Ghost_Mode_From_Entity (Typ);
8930 -- Prepare to construct predicate expression
8932 Expr := Empty;
8934 if Present (SId) then
8935 FDecl := Unit_Declaration_Node (SId);
8937 else
8938 FDecl := Build_Predicate_Function_Declaration (Typ);
8939 SId := Defining_Entity (FDecl);
8940 end if;
8942 -- Recover name of formal parameter of function that replaces references
8943 -- to the type in predicate expressions.
8945 Object_Entity :=
8946 Defining_Identifier
8947 (First (Parameter_Specifications (Specification (FDecl))));
8949 Object_Name := Chars (Object_Entity);
8950 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8952 -- Add predicates for ancestor if present. These must come before the
8953 -- ones for the current type, as required by AI12-0071-1.
8955 declare
8956 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8957 begin
8958 if Present (Atyp) then
8959 Add_Call (Atyp);
8960 end if;
8961 end;
8963 -- Add Predicates for the current type
8965 Add_Predicates;
8967 -- Case where predicates are present
8969 if Present (Expr) then
8971 -- Test for raise expression present
8973 Test_REs (Expr);
8975 -- If raise expression is present, capture a copy of Expr for use
8976 -- in building the predicateM function version later on. For this
8977 -- copy we replace references to Object_Entity by Object_Entity_M.
8979 if Raise_Expression_Present then
8980 declare
8981 Map : constant Elist_Id := New_Elmt_List;
8982 New_V : Entity_Id := Empty;
8984 -- The unanalyzed expression will be copied and appear in
8985 -- both functions. Normally expressions do not declare new
8986 -- entities, but quantified expressions do, so we need to
8987 -- create new entities for their bound variables, to prevent
8988 -- multiple definitions in gigi.
8990 function Reset_Loop_Variable (N : Node_Id)
8991 return Traverse_Result;
8993 procedure Collect_Loop_Variables is
8994 new Traverse_Proc (Reset_Loop_Variable);
8996 ------------------------
8997 -- Reset_Loop_Variable --
8998 ------------------------
9000 function Reset_Loop_Variable (N : Node_Id)
9001 return Traverse_Result
9003 begin
9004 if Nkind (N) = N_Iterator_Specification then
9005 New_V := Make_Defining_Identifier
9006 (Sloc (N), Chars (Defining_Identifier (N)));
9008 Set_Defining_Identifier (N, New_V);
9009 end if;
9011 return OK;
9012 end Reset_Loop_Variable;
9014 begin
9015 Append_Elmt (Object_Entity, Map);
9016 Append_Elmt (Object_Entity_M, Map);
9017 Expr_M := New_Copy_Tree (Expr, Map => Map);
9018 Collect_Loop_Variables (Expr_M);
9019 end;
9020 end if;
9022 -- Build the main predicate function
9024 declare
9025 SIdB : constant Entity_Id :=
9026 Make_Defining_Identifier (Loc,
9027 Chars => New_External_Name (Chars (Typ), "Predicate"));
9028 -- The entity for the function body
9030 Spec : Node_Id;
9031 FBody : Node_Id;
9033 begin
9035 -- The predicate function is shared between views of a type
9037 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
9038 Set_Predicate_Function (Full_View (Typ), SId);
9039 end if;
9041 -- Mark the predicate function explicitly as Ghost because it does
9042 -- not come from source.
9044 if Ghost_Mode > None then
9045 Set_Is_Ghost_Entity (SId);
9046 end if;
9048 -- Build function body
9050 Spec :=
9051 Make_Function_Specification (Loc,
9052 Defining_Unit_Name => SIdB,
9053 Parameter_Specifications => New_List (
9054 Make_Parameter_Specification (Loc,
9055 Defining_Identifier =>
9056 Make_Defining_Identifier (Loc, Object_Name),
9057 Parameter_Type =>
9058 New_Occurrence_Of (Typ, Loc))),
9059 Result_Definition =>
9060 New_Occurrence_Of (Standard_Boolean, Loc));
9062 FBody :=
9063 Make_Subprogram_Body (Loc,
9064 Specification => Spec,
9065 Declarations => Empty_List,
9066 Handled_Statement_Sequence =>
9067 Make_Handled_Sequence_Of_Statements (Loc,
9068 Statements => New_List (
9069 Make_Simple_Return_Statement (Loc,
9070 Expression => Expr))));
9072 -- If declaration has not been analyzed yet, Insert declaration
9073 -- before freeze node.
9074 -- Insert body after freeze node.
9076 if not Analyzed (FDecl) then
9077 Insert_Before_And_Analyze (N, FDecl);
9078 end if;
9080 Insert_After_And_Analyze (N, FBody);
9082 -- Static predicate functions are always side-effect free, and
9083 -- in most cases dynamic predicate functions are as well. Mark
9084 -- them as such whenever possible, so redundant predicate checks
9085 -- can be optimized. If there is a variable reference within the
9086 -- expression, the function is not pure.
9088 if Expander_Active then
9089 Set_Is_Pure (SId,
9090 Side_Effect_Free (Expr, Variable_Ref => True));
9091 Set_Is_Inlined (SId);
9092 end if;
9093 end;
9095 -- Test for raise expressions present and if so build M version
9097 if Raise_Expression_Present then
9098 declare
9099 SId : constant Entity_Id :=
9100 Make_Defining_Identifier (Loc,
9101 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9102 -- The entity for the function spec
9104 SIdB : constant Entity_Id :=
9105 Make_Defining_Identifier (Loc,
9106 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9107 -- The entity for the function body
9109 Spec : Node_Id;
9110 FBody : Node_Id;
9111 FDecl : Node_Id;
9112 BTemp : Entity_Id;
9114 begin
9115 -- Mark any raise expressions for special expansion
9117 Process_REs (Expr_M);
9119 -- Build function declaration
9121 Set_Ekind (SId, E_Function);
9122 Set_Is_Predicate_Function_M (SId);
9123 Set_Predicate_Function_M (Typ, SId);
9125 -- The predicate function is shared between views of a type
9127 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
9128 Set_Predicate_Function_M (Full_View (Typ), SId);
9129 end if;
9131 -- Mark the predicate function explicitly as Ghost because it
9132 -- does not come from source.
9134 if Ghost_Mode > None then
9135 Set_Is_Ghost_Entity (SId);
9136 end if;
9138 Spec :=
9139 Make_Function_Specification (Loc,
9140 Defining_Unit_Name => SId,
9141 Parameter_Specifications => New_List (
9142 Make_Parameter_Specification (Loc,
9143 Defining_Identifier => Object_Entity_M,
9144 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9145 Result_Definition =>
9146 New_Occurrence_Of (Standard_Boolean, Loc));
9148 FDecl :=
9149 Make_Subprogram_Declaration (Loc,
9150 Specification => Spec);
9152 -- Build function body
9154 Spec :=
9155 Make_Function_Specification (Loc,
9156 Defining_Unit_Name => SIdB,
9157 Parameter_Specifications => New_List (
9158 Make_Parameter_Specification (Loc,
9159 Defining_Identifier =>
9160 Make_Defining_Identifier (Loc, Object_Name),
9161 Parameter_Type =>
9162 New_Occurrence_Of (Typ, Loc))),
9163 Result_Definition =>
9164 New_Occurrence_Of (Standard_Boolean, Loc));
9166 -- Build the body, we declare the boolean expression before
9167 -- doing the return, because we are not really confident of
9168 -- what happens if a return appears within a return.
9170 BTemp :=
9171 Make_Defining_Identifier (Loc,
9172 Chars => New_Internal_Name ('B'));
9174 FBody :=
9175 Make_Subprogram_Body (Loc,
9176 Specification => Spec,
9178 Declarations => New_List (
9179 Make_Object_Declaration (Loc,
9180 Defining_Identifier => BTemp,
9181 Constant_Present => True,
9182 Object_Definition =>
9183 New_Occurrence_Of (Standard_Boolean, Loc),
9184 Expression => Expr_M)),
9186 Handled_Statement_Sequence =>
9187 Make_Handled_Sequence_Of_Statements (Loc,
9188 Statements => New_List (
9189 Make_Simple_Return_Statement (Loc,
9190 Expression => New_Occurrence_Of (BTemp, Loc)))));
9192 -- Insert declaration before freeze node and body after
9194 Insert_Before_And_Analyze (N, FDecl);
9195 Insert_After_And_Analyze (N, FBody);
9196 end;
9197 end if;
9199 -- See if we have a static predicate. Note that the answer may be
9200 -- yes even if we have an explicit Dynamic_Predicate present.
9202 declare
9203 PS : Boolean;
9204 EN : Node_Id;
9206 begin
9207 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
9208 PS := False;
9209 else
9210 PS := Is_Predicate_Static (Expr, Object_Name);
9211 end if;
9213 -- Case where we have a predicate-static aspect
9215 if PS then
9217 -- We don't set Has_Static_Predicate_Aspect, since we can have
9218 -- any of the three cases (Predicate, Dynamic_Predicate, or
9219 -- Static_Predicate) generating a predicate with an expression
9220 -- that is predicate-static. We just indicate that we have a
9221 -- predicate that can be treated as static.
9223 Set_Has_Static_Predicate (Typ);
9225 -- For discrete subtype, build the static predicate list
9227 if Is_Discrete_Type (Typ) then
9228 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
9230 -- If we don't get a static predicate list, it means that we
9231 -- have a case where this is not possible, most typically in
9232 -- the case where we inherit a dynamic predicate. We do not
9233 -- consider this an error, we just leave the predicate as
9234 -- dynamic. But if we do succeed in building the list, then
9235 -- we mark the predicate as static.
9237 if No (Static_Discrete_Predicate (Typ)) then
9238 Set_Has_Static_Predicate (Typ, False);
9239 end if;
9241 -- For real or string subtype, save predicate expression
9243 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
9244 Set_Static_Real_Or_String_Predicate (Typ, Expr);
9245 end if;
9247 -- Case of dynamic predicate (expression is not predicate-static)
9249 else
9250 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
9251 -- is only set if we have an explicit Dynamic_Predicate aspect
9252 -- given. Here we may simply have a Predicate aspect where the
9253 -- expression happens not to be predicate-static.
9255 -- Emit an error when the predicate is categorized as static
9256 -- but its expression is not predicate-static.
9258 -- First a little fiddling to get a nice location for the
9259 -- message. If the expression is of the form (A and then B),
9260 -- where A is an inherited predicate, then use the right
9261 -- operand for the Sloc. This avoids getting confused by a call
9262 -- to an inherited predicate with a less convenient source
9263 -- location.
9265 EN := Expr;
9266 while Nkind (EN) = N_And_Then
9267 and then Nkind (Left_Opnd (EN)) = N_Function_Call
9268 and then Is_Predicate_Function
9269 (Entity (Name (Left_Opnd (EN))))
9270 loop
9271 EN := Right_Opnd (EN);
9272 end loop;
9274 -- Now post appropriate message
9276 if Has_Static_Predicate_Aspect (Typ) then
9277 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
9278 Error_Msg_F
9279 ("expression is not predicate-static (RM 3.2.4(16-22))",
9280 EN);
9281 else
9282 Error_Msg_F
9283 ("static predicate requires scalar or string type", EN);
9284 end if;
9285 end if;
9286 end if;
9287 end;
9288 end if;
9290 Ghost_Mode := Save_Ghost_Mode;
9291 end Build_Predicate_Functions;
9293 ------------------------------------------
9294 -- Build_Predicate_Function_Declaration --
9295 ------------------------------------------
9297 function Build_Predicate_Function_Declaration
9298 (Typ : Entity_Id) return Node_Id
9300 Loc : constant Source_Ptr := Sloc (Typ);
9302 Object_Entity : constant Entity_Id :=
9303 Make_Defining_Identifier (Loc,
9304 Chars => New_Internal_Name ('I'));
9306 -- The formal parameter of the function
9308 SId : constant Entity_Id :=
9309 Make_Defining_Identifier (Loc,
9310 Chars => New_External_Name (Chars (Typ), "Predicate"));
9312 -- The entity for the function spec
9314 FDecl : Node_Id;
9315 Spec : Node_Id;
9317 begin
9318 Spec :=
9319 Make_Function_Specification (Loc,
9320 Defining_Unit_Name => SId,
9321 Parameter_Specifications => New_List (
9322 Make_Parameter_Specification (Loc,
9323 Defining_Identifier => Object_Entity,
9324 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9325 Result_Definition =>
9326 New_Occurrence_Of (Standard_Boolean, Loc));
9328 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
9330 Set_Ekind (SId, E_Function);
9331 Set_Etype (SId, Standard_Boolean);
9332 Set_Is_Internal (SId);
9333 Set_Is_Predicate_Function (SId);
9334 Set_Predicate_Function (Typ, SId);
9336 if Comes_From_Source (Typ) then
9337 Insert_After (Parent (Typ), FDecl);
9338 else
9339 Insert_After (Parent (Base_Type (Typ)), FDecl);
9340 end if;
9342 Analyze (FDecl);
9344 return FDecl;
9345 end Build_Predicate_Function_Declaration;
9347 -----------------------------------------
9348 -- Check_Aspect_At_End_Of_Declarations --
9349 -----------------------------------------
9351 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
9352 Ent : constant Entity_Id := Entity (ASN);
9353 Ident : constant Node_Id := Identifier (ASN);
9354 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9356 End_Decl_Expr : constant Node_Id := Entity (Ident);
9357 -- Expression to be analyzed at end of declarations
9359 Freeze_Expr : constant Node_Id := Expression (ASN);
9360 -- Expression from call to Check_Aspect_At_Freeze_Point
9362 T : constant Entity_Id := Etype (Freeze_Expr);
9363 -- Type required for preanalyze call
9365 Err : Boolean;
9366 -- Set False if error
9368 -- On entry to this procedure, Entity (Ident) contains a copy of the
9369 -- original expression from the aspect, saved for this purpose, and
9370 -- but Expression (Ident) is a preanalyzed copy of the expression,
9371 -- preanalyzed just after the freeze point.
9373 procedure Check_Overloaded_Name;
9374 -- For aspects whose expression is simply a name, this routine checks if
9375 -- the name is overloaded or not. If so, it verifies there is an
9376 -- interpretation that matches the entity obtained at the freeze point,
9377 -- otherwise the compiler complains.
9379 ---------------------------
9380 -- Check_Overloaded_Name --
9381 ---------------------------
9383 procedure Check_Overloaded_Name is
9384 begin
9385 if not Is_Overloaded (End_Decl_Expr) then
9386 Err := not Is_Entity_Name (End_Decl_Expr)
9387 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
9389 else
9390 Err := True;
9392 declare
9393 Index : Interp_Index;
9394 It : Interp;
9396 begin
9397 Get_First_Interp (End_Decl_Expr, Index, It);
9398 while Present (It.Typ) loop
9399 if It.Nam = Entity (Freeze_Expr) then
9400 Err := False;
9401 exit;
9402 end if;
9404 Get_Next_Interp (Index, It);
9405 end loop;
9406 end;
9407 end if;
9408 end Check_Overloaded_Name;
9410 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9412 begin
9413 -- In an instance we do not perform the consistency check between freeze
9414 -- point and end of declarations, because it was done already in the
9415 -- analysis of the generic. Furthermore, the delayed analysis of an
9416 -- aspect of the instance may produce spurious errors when the generic
9417 -- is a child unit that references entities in the parent (which might
9418 -- not be in scope at the freeze point of the instance).
9420 if In_Instance then
9421 return;
9423 -- Case of aspects Dimension, Dimension_System and Synchronization
9425 elsif A_Id = Aspect_Synchronization then
9426 return;
9428 -- Case of stream attributes, just have to compare entities. However,
9429 -- the expression is just a name (possibly overloaded), and there may
9430 -- be stream operations declared for unrelated types, so we just need
9431 -- to verify that one of these interpretations is the one available at
9432 -- at the freeze point.
9434 elsif A_Id = Aspect_Input or else
9435 A_Id = Aspect_Output or else
9436 A_Id = Aspect_Read or else
9437 A_Id = Aspect_Write
9438 then
9439 Analyze (End_Decl_Expr);
9440 Check_Overloaded_Name;
9442 elsif A_Id = Aspect_Variable_Indexing or else
9443 A_Id = Aspect_Constant_Indexing or else
9444 A_Id = Aspect_Default_Iterator or else
9445 A_Id = Aspect_Iterator_Element
9446 then
9447 -- Make type unfrozen before analysis, to prevent spurious errors
9448 -- about late attributes.
9450 Set_Is_Frozen (Ent, False);
9451 Analyze (End_Decl_Expr);
9452 Set_Is_Frozen (Ent, True);
9454 -- If the end of declarations comes before any other freeze
9455 -- point, the Freeze_Expr is not analyzed: no check needed.
9457 if Analyzed (Freeze_Expr) and then not In_Instance then
9458 Check_Overloaded_Name;
9459 else
9460 Err := False;
9461 end if;
9463 -- All other cases
9465 else
9466 -- Indicate that the expression comes from an aspect specification,
9467 -- which is used in subsequent analysis even if expansion is off.
9469 Set_Parent (End_Decl_Expr, ASN);
9471 -- In a generic context the aspect expressions have not been
9472 -- preanalyzed, so do it now. There are no conformance checks
9473 -- to perform in this case.
9475 if No (T) then
9476 Check_Aspect_At_Freeze_Point (ASN);
9477 return;
9479 -- The default values attributes may be defined in the private part,
9480 -- and the analysis of the expression may take place when only the
9481 -- partial view is visible. The expression must be scalar, so use
9482 -- the full view to resolve.
9484 elsif (A_Id = Aspect_Default_Value
9485 or else
9486 A_Id = Aspect_Default_Component_Value)
9487 and then Is_Private_Type (T)
9488 then
9489 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
9491 else
9492 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9493 end if;
9495 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9496 end if;
9498 -- Output error message if error. Force error on aspect specification
9499 -- even if there is an error on the expression itself.
9501 if Err then
9502 Error_Msg_NE
9503 ("!visibility of aspect for& changes after freeze point",
9504 ASN, Ent);
9505 Error_Msg_NE
9506 ("info: & is frozen here, aspects evaluated at this point??",
9507 Freeze_Node (Ent), Ent);
9508 end if;
9509 end Check_Aspect_At_End_Of_Declarations;
9511 ----------------------------------
9512 -- Check_Aspect_At_Freeze_Point --
9513 ----------------------------------
9515 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9516 Ident : constant Node_Id := Identifier (ASN);
9517 -- Identifier (use Entity field to save expression)
9519 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9521 T : Entity_Id := Empty;
9522 -- Type required for preanalyze call
9524 begin
9525 -- On entry to this procedure, Entity (Ident) contains a copy of the
9526 -- original expression from the aspect, saved for this purpose.
9528 -- On exit from this procedure Entity (Ident) is unchanged, still
9529 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9530 -- of the expression, preanalyzed just after the freeze point.
9532 -- Make a copy of the expression to be preanalyzed
9534 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
9536 -- Find type for preanalyze call
9538 case A_Id is
9540 -- No_Aspect should be impossible
9542 when No_Aspect =>
9543 raise Program_Error;
9545 -- Aspects taking an optional boolean argument
9547 when Boolean_Aspects |
9548 Library_Unit_Aspects =>
9550 T := Standard_Boolean;
9552 -- Aspects corresponding to attribute definition clauses
9554 when Aspect_Address =>
9555 T := RTE (RE_Address);
9557 when Aspect_Attach_Handler =>
9558 T := RTE (RE_Interrupt_ID);
9560 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9561 T := RTE (RE_Bit_Order);
9563 when Aspect_Convention =>
9564 return;
9566 when Aspect_CPU =>
9567 T := RTE (RE_CPU_Range);
9569 -- Default_Component_Value is resolved with the component type
9571 when Aspect_Default_Component_Value =>
9572 T := Component_Type (Entity (ASN));
9574 when Aspect_Default_Storage_Pool =>
9575 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9577 -- Default_Value is resolved with the type entity in question
9579 when Aspect_Default_Value =>
9580 T := Entity (ASN);
9582 when Aspect_Dispatching_Domain =>
9583 T := RTE (RE_Dispatching_Domain);
9585 when Aspect_External_Tag =>
9586 T := Standard_String;
9588 when Aspect_External_Name =>
9589 T := Standard_String;
9591 when Aspect_Link_Name =>
9592 T := Standard_String;
9594 when Aspect_Priority | Aspect_Interrupt_Priority =>
9595 T := Standard_Integer;
9597 when Aspect_Relative_Deadline =>
9598 T := RTE (RE_Time_Span);
9600 when Aspect_Small =>
9601 T := Universal_Real;
9603 -- For a simple storage pool, we have to retrieve the type of the
9604 -- pool object associated with the aspect's corresponding attribute
9605 -- definition clause.
9607 when Aspect_Simple_Storage_Pool =>
9608 T := Etype (Expression (Aspect_Rep_Item (ASN)));
9610 when Aspect_Storage_Pool =>
9611 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9613 when Aspect_Alignment |
9614 Aspect_Component_Size |
9615 Aspect_Machine_Radix |
9616 Aspect_Object_Size |
9617 Aspect_Size |
9618 Aspect_Storage_Size |
9619 Aspect_Stream_Size |
9620 Aspect_Value_Size =>
9621 T := Any_Integer;
9623 when Aspect_Linker_Section =>
9624 T := Standard_String;
9626 when Aspect_Synchronization =>
9627 return;
9629 -- Special case, the expression of these aspects is just an entity
9630 -- that does not need any resolution, so just analyze.
9632 when Aspect_Input |
9633 Aspect_Output |
9634 Aspect_Read |
9635 Aspect_Suppress |
9636 Aspect_Unsuppress |
9637 Aspect_Warnings |
9638 Aspect_Write =>
9639 Analyze (Expression (ASN));
9640 return;
9642 -- Same for Iterator aspects, where the expression is a function
9643 -- name. Legality rules are checked separately.
9645 when Aspect_Constant_Indexing |
9646 Aspect_Default_Iterator |
9647 Aspect_Iterator_Element |
9648 Aspect_Variable_Indexing =>
9649 Analyze (Expression (ASN));
9650 return;
9652 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9654 when Aspect_Iterable =>
9655 T := Entity (ASN);
9657 declare
9658 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
9659 Assoc : Node_Id;
9660 Expr : Node_Id;
9662 begin
9663 if Cursor = Any_Type then
9664 return;
9665 end if;
9667 Assoc := First (Component_Associations (Expression (ASN)));
9668 while Present (Assoc) loop
9669 Expr := Expression (Assoc);
9670 Analyze (Expr);
9672 if not Error_Posted (Expr) then
9673 Resolve_Iterable_Operation
9674 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9675 end if;
9677 Next (Assoc);
9678 end loop;
9679 end;
9681 return;
9683 -- Invariant/Predicate take boolean expressions
9685 when Aspect_Dynamic_Predicate |
9686 Aspect_Invariant |
9687 Aspect_Predicate |
9688 Aspect_Static_Predicate |
9689 Aspect_Type_Invariant =>
9690 T := Standard_Boolean;
9692 when Aspect_Predicate_Failure =>
9693 T := Standard_String;
9695 -- Here is the list of aspects that don't require delay analysis
9697 when Aspect_Abstract_State |
9698 Aspect_Annotate |
9699 Aspect_Async_Readers |
9700 Aspect_Async_Writers |
9701 Aspect_Constant_After_Elaboration |
9702 Aspect_Contract_Cases |
9703 Aspect_Default_Initial_Condition |
9704 Aspect_Depends |
9705 Aspect_Dimension |
9706 Aspect_Dimension_System |
9707 Aspect_Effective_Reads |
9708 Aspect_Effective_Writes |
9709 Aspect_Extensions_Visible |
9710 Aspect_Ghost |
9711 Aspect_Global |
9712 Aspect_Implicit_Dereference |
9713 Aspect_Initial_Condition |
9714 Aspect_Initializes |
9715 Aspect_Obsolescent |
9716 Aspect_Part_Of |
9717 Aspect_Post |
9718 Aspect_Postcondition |
9719 Aspect_Pre |
9720 Aspect_Precondition |
9721 Aspect_Refined_Depends |
9722 Aspect_Refined_Global |
9723 Aspect_Refined_Post |
9724 Aspect_Refined_State |
9725 Aspect_SPARK_Mode |
9726 Aspect_Test_Case |
9727 Aspect_Unimplemented |
9728 Aspect_Volatile_Function =>
9729 raise Program_Error;
9731 end case;
9733 -- Do the preanalyze call
9735 Preanalyze_Spec_Expression (Expression (ASN), T);
9736 end Check_Aspect_At_Freeze_Point;
9738 -----------------------------------
9739 -- Check_Constant_Address_Clause --
9740 -----------------------------------
9742 procedure Check_Constant_Address_Clause
9743 (Expr : Node_Id;
9744 U_Ent : Entity_Id)
9746 procedure Check_At_Constant_Address (Nod : Node_Id);
9747 -- Checks that the given node N represents a name whose 'Address is
9748 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9749 -- address value is the same at the point of declaration of U_Ent and at
9750 -- the time of elaboration of the address clause.
9752 procedure Check_Expr_Constants (Nod : Node_Id);
9753 -- Checks that Nod meets the requirements for a constant address clause
9754 -- in the sense of the enclosing procedure.
9756 procedure Check_List_Constants (Lst : List_Id);
9757 -- Check that all elements of list Lst meet the requirements for a
9758 -- constant address clause in the sense of the enclosing procedure.
9760 -------------------------------
9761 -- Check_At_Constant_Address --
9762 -------------------------------
9764 procedure Check_At_Constant_Address (Nod : Node_Id) is
9765 begin
9766 if Is_Entity_Name (Nod) then
9767 if Present (Address_Clause (Entity ((Nod)))) then
9768 Error_Msg_NE
9769 ("invalid address clause for initialized object &!",
9770 Nod, U_Ent);
9771 Error_Msg_NE
9772 ("address for& cannot" &
9773 " depend on another address clause! (RM 13.1(22))!",
9774 Nod, U_Ent);
9776 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9777 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9778 then
9779 Error_Msg_NE
9780 ("invalid address clause for initialized object &!",
9781 Nod, U_Ent);
9782 Error_Msg_Node_2 := U_Ent;
9783 Error_Msg_NE
9784 ("\& must be defined before & (RM 13.1(22))!",
9785 Nod, Entity (Nod));
9786 end if;
9788 elsif Nkind (Nod) = N_Selected_Component then
9789 declare
9790 T : constant Entity_Id := Etype (Prefix (Nod));
9792 begin
9793 if (Is_Record_Type (T)
9794 and then Has_Discriminants (T))
9795 or else
9796 (Is_Access_Type (T)
9797 and then Is_Record_Type (Designated_Type (T))
9798 and then Has_Discriminants (Designated_Type (T)))
9799 then
9800 Error_Msg_NE
9801 ("invalid address clause for initialized object &!",
9802 Nod, U_Ent);
9803 Error_Msg_N
9804 ("\address cannot depend on component" &
9805 " of discriminated record (RM 13.1(22))!",
9806 Nod);
9807 else
9808 Check_At_Constant_Address (Prefix (Nod));
9809 end if;
9810 end;
9812 elsif Nkind (Nod) = N_Indexed_Component then
9813 Check_At_Constant_Address (Prefix (Nod));
9814 Check_List_Constants (Expressions (Nod));
9816 else
9817 Check_Expr_Constants (Nod);
9818 end if;
9819 end Check_At_Constant_Address;
9821 --------------------------
9822 -- Check_Expr_Constants --
9823 --------------------------
9825 procedure Check_Expr_Constants (Nod : Node_Id) is
9826 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9827 Ent : Entity_Id := Empty;
9829 begin
9830 if Nkind (Nod) in N_Has_Etype
9831 and then Etype (Nod) = Any_Type
9832 then
9833 return;
9834 end if;
9836 case Nkind (Nod) is
9837 when N_Empty | N_Error =>
9838 return;
9840 when N_Identifier | N_Expanded_Name =>
9841 Ent := Entity (Nod);
9843 -- We need to look at the original node if it is different
9844 -- from the node, since we may have rewritten things and
9845 -- substituted an identifier representing the rewrite.
9847 if Original_Node (Nod) /= Nod then
9848 Check_Expr_Constants (Original_Node (Nod));
9850 -- If the node is an object declaration without initial
9851 -- value, some code has been expanded, and the expression
9852 -- is not constant, even if the constituents might be
9853 -- acceptable, as in A'Address + offset.
9855 if Ekind (Ent) = E_Variable
9856 and then
9857 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9858 and then
9859 No (Expression (Declaration_Node (Ent)))
9860 then
9861 Error_Msg_NE
9862 ("invalid address clause for initialized object &!",
9863 Nod, U_Ent);
9865 -- If entity is constant, it may be the result of expanding
9866 -- a check. We must verify that its declaration appears
9867 -- before the object in question, else we also reject the
9868 -- address clause.
9870 elsif Ekind (Ent) = E_Constant
9871 and then In_Same_Source_Unit (Ent, U_Ent)
9872 and then Sloc (Ent) > Loc_U_Ent
9873 then
9874 Error_Msg_NE
9875 ("invalid address clause for initialized object &!",
9876 Nod, U_Ent);
9877 end if;
9879 return;
9880 end if;
9882 -- Otherwise look at the identifier and see if it is OK
9884 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9885 or else Is_Type (Ent)
9886 then
9887 return;
9889 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9891 -- This is the case where we must have Ent defined before
9892 -- U_Ent. Clearly if they are in different units this
9893 -- requirement is met since the unit containing Ent is
9894 -- already processed.
9896 if not In_Same_Source_Unit (Ent, U_Ent) then
9897 return;
9899 -- Otherwise location of Ent must be before the location
9900 -- of U_Ent, that's what prior defined means.
9902 elsif Sloc (Ent) < Loc_U_Ent then
9903 return;
9905 else
9906 Error_Msg_NE
9907 ("invalid address clause for initialized object &!",
9908 Nod, U_Ent);
9909 Error_Msg_Node_2 := U_Ent;
9910 Error_Msg_NE
9911 ("\& must be defined before & (RM 13.1(22))!",
9912 Nod, Ent);
9913 end if;
9915 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9916 Check_Expr_Constants (Original_Node (Nod));
9918 else
9919 Error_Msg_NE
9920 ("invalid address clause for initialized object &!",
9921 Nod, U_Ent);
9923 if Comes_From_Source (Ent) then
9924 Error_Msg_NE
9925 ("\reference to variable& not allowed"
9926 & " (RM 13.1(22))!", Nod, Ent);
9927 else
9928 Error_Msg_N
9929 ("non-static expression not allowed"
9930 & " (RM 13.1(22))!", Nod);
9931 end if;
9932 end if;
9934 when N_Integer_Literal =>
9936 -- If this is a rewritten unchecked conversion, in a system
9937 -- where Address is an integer type, always use the base type
9938 -- for a literal value. This is user-friendly and prevents
9939 -- order-of-elaboration issues with instances of unchecked
9940 -- conversion.
9942 if Nkind (Original_Node (Nod)) = N_Function_Call then
9943 Set_Etype (Nod, Base_Type (Etype (Nod)));
9944 end if;
9946 when N_Real_Literal |
9947 N_String_Literal |
9948 N_Character_Literal =>
9949 return;
9951 when N_Range =>
9952 Check_Expr_Constants (Low_Bound (Nod));
9953 Check_Expr_Constants (High_Bound (Nod));
9955 when N_Explicit_Dereference =>
9956 Check_Expr_Constants (Prefix (Nod));
9958 when N_Indexed_Component =>
9959 Check_Expr_Constants (Prefix (Nod));
9960 Check_List_Constants (Expressions (Nod));
9962 when N_Slice =>
9963 Check_Expr_Constants (Prefix (Nod));
9964 Check_Expr_Constants (Discrete_Range (Nod));
9966 when N_Selected_Component =>
9967 Check_Expr_Constants (Prefix (Nod));
9969 when N_Attribute_Reference =>
9970 if Nam_In (Attribute_Name (Nod), Name_Address,
9971 Name_Access,
9972 Name_Unchecked_Access,
9973 Name_Unrestricted_Access)
9974 then
9975 Check_At_Constant_Address (Prefix (Nod));
9977 else
9978 Check_Expr_Constants (Prefix (Nod));
9979 Check_List_Constants (Expressions (Nod));
9980 end if;
9982 when N_Aggregate =>
9983 Check_List_Constants (Component_Associations (Nod));
9984 Check_List_Constants (Expressions (Nod));
9986 when N_Component_Association =>
9987 Check_Expr_Constants (Expression (Nod));
9989 when N_Extension_Aggregate =>
9990 Check_Expr_Constants (Ancestor_Part (Nod));
9991 Check_List_Constants (Component_Associations (Nod));
9992 Check_List_Constants (Expressions (Nod));
9994 when N_Null =>
9995 return;
9997 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9998 Check_Expr_Constants (Left_Opnd (Nod));
9999 Check_Expr_Constants (Right_Opnd (Nod));
10001 when N_Unary_Op =>
10002 Check_Expr_Constants (Right_Opnd (Nod));
10004 when N_Type_Conversion |
10005 N_Qualified_Expression |
10006 N_Allocator |
10007 N_Unchecked_Type_Conversion =>
10008 Check_Expr_Constants (Expression (Nod));
10010 when N_Function_Call =>
10011 if not Is_Pure (Entity (Name (Nod))) then
10012 Error_Msg_NE
10013 ("invalid address clause for initialized object &!",
10014 Nod, U_Ent);
10016 Error_Msg_NE
10017 ("\function & is not pure (RM 13.1(22))!",
10018 Nod, Entity (Name (Nod)));
10020 else
10021 Check_List_Constants (Parameter_Associations (Nod));
10022 end if;
10024 when N_Parameter_Association =>
10025 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
10027 when others =>
10028 Error_Msg_NE
10029 ("invalid address clause for initialized object &!",
10030 Nod, U_Ent);
10031 Error_Msg_NE
10032 ("\must be constant defined before& (RM 13.1(22))!",
10033 Nod, U_Ent);
10034 end case;
10035 end Check_Expr_Constants;
10037 --------------------------
10038 -- Check_List_Constants --
10039 --------------------------
10041 procedure Check_List_Constants (Lst : List_Id) is
10042 Nod1 : Node_Id;
10044 begin
10045 if Present (Lst) then
10046 Nod1 := First (Lst);
10047 while Present (Nod1) loop
10048 Check_Expr_Constants (Nod1);
10049 Next (Nod1);
10050 end loop;
10051 end if;
10052 end Check_List_Constants;
10054 -- Start of processing for Check_Constant_Address_Clause
10056 begin
10057 -- If rep_clauses are to be ignored, no need for legality checks. In
10058 -- particular, no need to pester user about rep clauses that violate the
10059 -- rule on constant addresses, given that these clauses will be removed
10060 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
10061 -- we want to relax these checks.
10063 if not Ignore_Rep_Clauses and not CodePeer_Mode then
10064 Check_Expr_Constants (Expr);
10065 end if;
10066 end Check_Constant_Address_Clause;
10068 ---------------------------
10069 -- Check_Pool_Size_Clash --
10070 ---------------------------
10072 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
10073 Post : Node_Id;
10075 begin
10076 -- We need to find out which one came first. Note that in the case of
10077 -- aspects mixed with pragmas there are cases where the processing order
10078 -- is reversed, which is why we do the check here.
10080 if Sloc (SP) < Sloc (SS) then
10081 Error_Msg_Sloc := Sloc (SP);
10082 Post := SS;
10083 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
10085 else
10086 Error_Msg_Sloc := Sloc (SS);
10087 Post := SP;
10088 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
10089 end if;
10091 Error_Msg_N
10092 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
10093 end Check_Pool_Size_Clash;
10095 ----------------------------------------
10096 -- Check_Record_Representation_Clause --
10097 ----------------------------------------
10099 procedure Check_Record_Representation_Clause (N : Node_Id) is
10100 Loc : constant Source_Ptr := Sloc (N);
10101 Ident : constant Node_Id := Identifier (N);
10102 Rectype : Entity_Id;
10103 Fent : Entity_Id;
10104 CC : Node_Id;
10105 Fbit : Uint;
10106 Lbit : Uint;
10107 Hbit : Uint := Uint_0;
10108 Comp : Entity_Id;
10109 Pcomp : Entity_Id;
10111 Max_Bit_So_Far : Uint;
10112 -- Records the maximum bit position so far. If all field positions
10113 -- are monotonically increasing, then we can skip the circuit for
10114 -- checking for overlap, since no overlap is possible.
10116 Tagged_Parent : Entity_Id := Empty;
10117 -- This is set in the case of a derived tagged type for which we have
10118 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
10119 -- positioned by record representation clauses). In this case we must
10120 -- check for overlap between components of this tagged type, and the
10121 -- components of its parent. Tagged_Parent will point to this parent
10122 -- type. For all other cases Tagged_Parent is left set to Empty.
10124 Parent_Last_Bit : Uint;
10125 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
10126 -- last bit position for any field in the parent type. We only need to
10127 -- check overlap for fields starting below this point.
10129 Overlap_Check_Required : Boolean;
10130 -- Used to keep track of whether or not an overlap check is required
10132 Overlap_Detected : Boolean := False;
10133 -- Set True if an overlap is detected
10135 Ccount : Natural := 0;
10136 -- Number of component clauses in record rep clause
10138 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
10139 -- Given two entities for record components or discriminants, checks
10140 -- if they have overlapping component clauses and issues errors if so.
10142 procedure Find_Component;
10143 -- Finds component entity corresponding to current component clause (in
10144 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
10145 -- start/stop bits for the field. If there is no matching component or
10146 -- if the matching component does not have a component clause, then
10147 -- that's an error and Comp is set to Empty, but no error message is
10148 -- issued, since the message was already given. Comp is also set to
10149 -- Empty if the current "component clause" is in fact a pragma.
10151 -----------------------------
10152 -- Check_Component_Overlap --
10153 -----------------------------
10155 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
10156 CC1 : constant Node_Id := Component_Clause (C1_Ent);
10157 CC2 : constant Node_Id := Component_Clause (C2_Ent);
10159 begin
10160 if Present (CC1) and then Present (CC2) then
10162 -- Exclude odd case where we have two tag components in the same
10163 -- record, both at location zero. This seems a bit strange, but
10164 -- it seems to happen in some circumstances, perhaps on an error.
10166 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
10167 return;
10168 end if;
10170 -- Here we check if the two fields overlap
10172 declare
10173 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
10174 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
10175 E1 : constant Uint := S1 + Esize (C1_Ent);
10176 E2 : constant Uint := S2 + Esize (C2_Ent);
10178 begin
10179 if E2 <= S1 or else E1 <= S2 then
10180 null;
10181 else
10182 Error_Msg_Node_2 := Component_Name (CC2);
10183 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
10184 Error_Msg_Node_1 := Component_Name (CC1);
10185 Error_Msg_N
10186 ("component& overlaps & #", Component_Name (CC1));
10187 Overlap_Detected := True;
10188 end if;
10189 end;
10190 end if;
10191 end Check_Component_Overlap;
10193 --------------------
10194 -- Find_Component --
10195 --------------------
10197 procedure Find_Component is
10199 procedure Search_Component (R : Entity_Id);
10200 -- Search components of R for a match. If found, Comp is set
10202 ----------------------
10203 -- Search_Component --
10204 ----------------------
10206 procedure Search_Component (R : Entity_Id) is
10207 begin
10208 Comp := First_Component_Or_Discriminant (R);
10209 while Present (Comp) loop
10211 -- Ignore error of attribute name for component name (we
10212 -- already gave an error message for this, so no need to
10213 -- complain here)
10215 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
10216 null;
10217 else
10218 exit when Chars (Comp) = Chars (Component_Name (CC));
10219 end if;
10221 Next_Component_Or_Discriminant (Comp);
10222 end loop;
10223 end Search_Component;
10225 -- Start of processing for Find_Component
10227 begin
10228 -- Return with Comp set to Empty if we have a pragma
10230 if Nkind (CC) = N_Pragma then
10231 Comp := Empty;
10232 return;
10233 end if;
10235 -- Search current record for matching component
10237 Search_Component (Rectype);
10239 -- If not found, maybe component of base type discriminant that is
10240 -- absent from statically constrained first subtype.
10242 if No (Comp) then
10243 Search_Component (Base_Type (Rectype));
10244 end if;
10246 -- If no component, or the component does not reference the component
10247 -- clause in question, then there was some previous error for which
10248 -- we already gave a message, so just return with Comp Empty.
10250 if No (Comp) or else Component_Clause (Comp) /= CC then
10251 Check_Error_Detected;
10252 Comp := Empty;
10254 -- Normal case where we have a component clause
10256 else
10257 Fbit := Component_Bit_Offset (Comp);
10258 Lbit := Fbit + Esize (Comp) - 1;
10259 end if;
10260 end Find_Component;
10262 -- Start of processing for Check_Record_Representation_Clause
10264 begin
10265 Find_Type (Ident);
10266 Rectype := Entity (Ident);
10268 if Rectype = Any_Type then
10269 return;
10270 else
10271 Rectype := Underlying_Type (Rectype);
10272 end if;
10274 -- See if we have a fully repped derived tagged type
10276 declare
10277 PS : constant Entity_Id := Parent_Subtype (Rectype);
10279 begin
10280 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
10281 Tagged_Parent := PS;
10283 -- Find maximum bit of any component of the parent type
10285 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
10286 Pcomp := First_Entity (Tagged_Parent);
10287 while Present (Pcomp) loop
10288 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
10289 if Component_Bit_Offset (Pcomp) /= No_Uint
10290 and then Known_Static_Esize (Pcomp)
10291 then
10292 Parent_Last_Bit :=
10293 UI_Max
10294 (Parent_Last_Bit,
10295 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
10296 end if;
10297 else
10299 -- Skip anonymous types generated for constrained array
10300 -- or record components.
10302 null;
10303 end if;
10305 Next_Entity (Pcomp);
10306 end loop;
10307 end if;
10308 end;
10310 -- All done if no component clauses
10312 CC := First (Component_Clauses (N));
10314 if No (CC) then
10315 return;
10316 end if;
10318 -- If a tag is present, then create a component clause that places it
10319 -- at the start of the record (otherwise gigi may place it after other
10320 -- fields that have rep clauses).
10322 Fent := First_Entity (Rectype);
10324 if Nkind (Fent) = N_Defining_Identifier
10325 and then Chars (Fent) = Name_uTag
10326 then
10327 Set_Component_Bit_Offset (Fent, Uint_0);
10328 Set_Normalized_Position (Fent, Uint_0);
10329 Set_Normalized_First_Bit (Fent, Uint_0);
10330 Set_Normalized_Position_Max (Fent, Uint_0);
10331 Init_Esize (Fent, System_Address_Size);
10333 Set_Component_Clause (Fent,
10334 Make_Component_Clause (Loc,
10335 Component_Name => Make_Identifier (Loc, Name_uTag),
10337 Position => Make_Integer_Literal (Loc, Uint_0),
10338 First_Bit => Make_Integer_Literal (Loc, Uint_0),
10339 Last_Bit =>
10340 Make_Integer_Literal (Loc,
10341 UI_From_Int (System_Address_Size))));
10343 Ccount := Ccount + 1;
10344 end if;
10346 Max_Bit_So_Far := Uint_Minus_1;
10347 Overlap_Check_Required := False;
10349 -- Process the component clauses
10351 while Present (CC) loop
10352 Find_Component;
10354 if Present (Comp) then
10355 Ccount := Ccount + 1;
10357 -- We need a full overlap check if record positions non-monotonic
10359 if Fbit <= Max_Bit_So_Far then
10360 Overlap_Check_Required := True;
10361 end if;
10363 Max_Bit_So_Far := Lbit;
10365 -- Check bit position out of range of specified size
10367 if Has_Size_Clause (Rectype)
10368 and then RM_Size (Rectype) <= Lbit
10369 then
10370 Error_Msg_N
10371 ("bit number out of range of specified size",
10372 Last_Bit (CC));
10374 -- Check for overlap with tag component
10376 else
10377 if Is_Tagged_Type (Rectype)
10378 and then Fbit < System_Address_Size
10379 then
10380 Error_Msg_NE
10381 ("component overlaps tag field of&",
10382 Component_Name (CC), Rectype);
10383 Overlap_Detected := True;
10384 end if;
10386 if Hbit < Lbit then
10387 Hbit := Lbit;
10388 end if;
10389 end if;
10391 -- Check parent overlap if component might overlap parent field
10393 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
10394 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
10395 while Present (Pcomp) loop
10396 if not Is_Tag (Pcomp)
10397 and then Chars (Pcomp) /= Name_uParent
10398 then
10399 Check_Component_Overlap (Comp, Pcomp);
10400 end if;
10402 Next_Component_Or_Discriminant (Pcomp);
10403 end loop;
10404 end if;
10405 end if;
10407 Next (CC);
10408 end loop;
10410 -- Now that we have processed all the component clauses, check for
10411 -- overlap. We have to leave this till last, since the components can
10412 -- appear in any arbitrary order in the representation clause.
10414 -- We do not need this check if all specified ranges were monotonic,
10415 -- as recorded by Overlap_Check_Required being False at this stage.
10417 -- This first section checks if there are any overlapping entries at
10418 -- all. It does this by sorting all entries and then seeing if there are
10419 -- any overlaps. If there are none, then that is decisive, but if there
10420 -- are overlaps, they may still be OK (they may result from fields in
10421 -- different variants).
10423 if Overlap_Check_Required then
10424 Overlap_Check1 : declare
10426 OC_Fbit : array (0 .. Ccount) of Uint;
10427 -- First-bit values for component clauses, the value is the offset
10428 -- of the first bit of the field from start of record. The zero
10429 -- entry is for use in sorting.
10431 OC_Lbit : array (0 .. Ccount) of Uint;
10432 -- Last-bit values for component clauses, the value is the offset
10433 -- of the last bit of the field from start of record. The zero
10434 -- entry is for use in sorting.
10436 OC_Count : Natural := 0;
10437 -- Count of entries in OC_Fbit and OC_Lbit
10439 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10440 -- Compare routine for Sort
10442 procedure OC_Move (From : Natural; To : Natural);
10443 -- Move routine for Sort
10445 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
10447 -----------
10448 -- OC_Lt --
10449 -----------
10451 function OC_Lt (Op1, Op2 : Natural) return Boolean is
10452 begin
10453 return OC_Fbit (Op1) < OC_Fbit (Op2);
10454 end OC_Lt;
10456 -------------
10457 -- OC_Move --
10458 -------------
10460 procedure OC_Move (From : Natural; To : Natural) is
10461 begin
10462 OC_Fbit (To) := OC_Fbit (From);
10463 OC_Lbit (To) := OC_Lbit (From);
10464 end OC_Move;
10466 -- Start of processing for Overlap_Check
10468 begin
10469 CC := First (Component_Clauses (N));
10470 while Present (CC) loop
10472 -- Exclude component clause already marked in error
10474 if not Error_Posted (CC) then
10475 Find_Component;
10477 if Present (Comp) then
10478 OC_Count := OC_Count + 1;
10479 OC_Fbit (OC_Count) := Fbit;
10480 OC_Lbit (OC_Count) := Lbit;
10481 end if;
10482 end if;
10484 Next (CC);
10485 end loop;
10487 Sorting.Sort (OC_Count);
10489 Overlap_Check_Required := False;
10490 for J in 1 .. OC_Count - 1 loop
10491 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10492 Overlap_Check_Required := True;
10493 exit;
10494 end if;
10495 end loop;
10496 end Overlap_Check1;
10497 end if;
10499 -- If Overlap_Check_Required is still True, then we have to do the full
10500 -- scale overlap check, since we have at least two fields that do
10501 -- overlap, and we need to know if that is OK since they are in
10502 -- different variant, or whether we have a definite problem.
10504 if Overlap_Check_Required then
10505 Overlap_Check2 : declare
10506 C1_Ent, C2_Ent : Entity_Id;
10507 -- Entities of components being checked for overlap
10509 Clist : Node_Id;
10510 -- Component_List node whose Component_Items are being checked
10512 Citem : Node_Id;
10513 -- Component declaration for component being checked
10515 begin
10516 C1_Ent := First_Entity (Base_Type (Rectype));
10518 -- Loop through all components in record. For each component check
10519 -- for overlap with any of the preceding elements on the component
10520 -- list containing the component and also, if the component is in
10521 -- a variant, check against components outside the case structure.
10522 -- This latter test is repeated recursively up the variant tree.
10524 Main_Component_Loop : while Present (C1_Ent) loop
10525 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10526 goto Continue_Main_Component_Loop;
10527 end if;
10529 -- Skip overlap check if entity has no declaration node. This
10530 -- happens with discriminants in constrained derived types.
10531 -- Possibly we are missing some checks as a result, but that
10532 -- does not seem terribly serious.
10534 if No (Declaration_Node (C1_Ent)) then
10535 goto Continue_Main_Component_Loop;
10536 end if;
10538 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
10540 -- Loop through component lists that need checking. Check the
10541 -- current component list and all lists in variants above us.
10543 Component_List_Loop : loop
10545 -- If derived type definition, go to full declaration
10546 -- If at outer level, check discriminants if there are any.
10548 if Nkind (Clist) = N_Derived_Type_Definition then
10549 Clist := Parent (Clist);
10550 end if;
10552 -- Outer level of record definition, check discriminants
10554 if Nkind_In (Clist, N_Full_Type_Declaration,
10555 N_Private_Type_Declaration)
10556 then
10557 if Has_Discriminants (Defining_Identifier (Clist)) then
10558 C2_Ent :=
10559 First_Discriminant (Defining_Identifier (Clist));
10560 while Present (C2_Ent) loop
10561 exit when C1_Ent = C2_Ent;
10562 Check_Component_Overlap (C1_Ent, C2_Ent);
10563 Next_Discriminant (C2_Ent);
10564 end loop;
10565 end if;
10567 -- Record extension case
10569 elsif Nkind (Clist) = N_Derived_Type_Definition then
10570 Clist := Empty;
10572 -- Otherwise check one component list
10574 else
10575 Citem := First (Component_Items (Clist));
10576 while Present (Citem) loop
10577 if Nkind (Citem) = N_Component_Declaration then
10578 C2_Ent := Defining_Identifier (Citem);
10579 exit when C1_Ent = C2_Ent;
10580 Check_Component_Overlap (C1_Ent, C2_Ent);
10581 end if;
10583 Next (Citem);
10584 end loop;
10585 end if;
10587 -- Check for variants above us (the parent of the Clist can
10588 -- be a variant, in which case its parent is a variant part,
10589 -- and the parent of the variant part is a component list
10590 -- whose components must all be checked against the current
10591 -- component for overlap).
10593 if Nkind (Parent (Clist)) = N_Variant then
10594 Clist := Parent (Parent (Parent (Clist)));
10596 -- Check for possible discriminant part in record, this
10597 -- is treated essentially as another level in the
10598 -- recursion. For this case the parent of the component
10599 -- list is the record definition, and its parent is the
10600 -- full type declaration containing the discriminant
10601 -- specifications.
10603 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10604 Clist := Parent (Parent ((Clist)));
10606 -- If neither of these two cases, we are at the top of
10607 -- the tree.
10609 else
10610 exit Component_List_Loop;
10611 end if;
10612 end loop Component_List_Loop;
10614 <<Continue_Main_Component_Loop>>
10615 Next_Entity (C1_Ent);
10617 end loop Main_Component_Loop;
10618 end Overlap_Check2;
10619 end if;
10621 -- The following circuit deals with warning on record holes (gaps). We
10622 -- skip this check if overlap was detected, since it makes sense for the
10623 -- programmer to fix this illegality before worrying about warnings.
10625 if not Overlap_Detected and Warn_On_Record_Holes then
10626 Record_Hole_Check : declare
10627 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10628 -- Full declaration of record type
10630 procedure Check_Component_List
10631 (CL : Node_Id;
10632 Sbit : Uint;
10633 DS : List_Id);
10634 -- Check component list CL for holes. The starting bit should be
10635 -- Sbit. which is zero for the main record component list and set
10636 -- appropriately for recursive calls for variants. DS is set to
10637 -- a list of discriminant specifications to be included in the
10638 -- consideration of components. It is No_List if none to consider.
10640 --------------------------
10641 -- Check_Component_List --
10642 --------------------------
10644 procedure Check_Component_List
10645 (CL : Node_Id;
10646 Sbit : Uint;
10647 DS : List_Id)
10649 Compl : Integer;
10651 begin
10652 Compl := Integer (List_Length (Component_Items (CL)));
10654 if DS /= No_List then
10655 Compl := Compl + Integer (List_Length (DS));
10656 end if;
10658 declare
10659 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10660 -- Gather components (zero entry is for sort routine)
10662 Ncomps : Natural := 0;
10663 -- Number of entries stored in Comps (starting at Comps (1))
10665 Citem : Node_Id;
10666 -- One component item or discriminant specification
10668 Nbit : Uint;
10669 -- Starting bit for next component
10671 CEnt : Entity_Id;
10672 -- Component entity
10674 Variant : Node_Id;
10675 -- One variant
10677 function Lt (Op1, Op2 : Natural) return Boolean;
10678 -- Compare routine for Sort
10680 procedure Move (From : Natural; To : Natural);
10681 -- Move routine for Sort
10683 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
10685 --------
10686 -- Lt --
10687 --------
10689 function Lt (Op1, Op2 : Natural) return Boolean is
10690 begin
10691 return Component_Bit_Offset (Comps (Op1))
10693 Component_Bit_Offset (Comps (Op2));
10694 end Lt;
10696 ----------
10697 -- Move --
10698 ----------
10700 procedure Move (From : Natural; To : Natural) is
10701 begin
10702 Comps (To) := Comps (From);
10703 end Move;
10705 begin
10706 -- Gather discriminants into Comp
10708 if DS /= No_List then
10709 Citem := First (DS);
10710 while Present (Citem) loop
10711 if Nkind (Citem) = N_Discriminant_Specification then
10712 declare
10713 Ent : constant Entity_Id :=
10714 Defining_Identifier (Citem);
10715 begin
10716 if Ekind (Ent) = E_Discriminant then
10717 Ncomps := Ncomps + 1;
10718 Comps (Ncomps) := Ent;
10719 end if;
10720 end;
10721 end if;
10723 Next (Citem);
10724 end loop;
10725 end if;
10727 -- Gather component entities into Comp
10729 Citem := First (Component_Items (CL));
10730 while Present (Citem) loop
10731 if Nkind (Citem) = N_Component_Declaration then
10732 Ncomps := Ncomps + 1;
10733 Comps (Ncomps) := Defining_Identifier (Citem);
10734 end if;
10736 Next (Citem);
10737 end loop;
10739 -- Now sort the component entities based on the first bit.
10740 -- Note we already know there are no overlapping components.
10742 Sorting.Sort (Ncomps);
10744 -- Loop through entries checking for holes
10746 Nbit := Sbit;
10747 for J in 1 .. Ncomps loop
10748 CEnt := Comps (J);
10749 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
10751 if Error_Msg_Uint_1 > 0 then
10752 Error_Msg_NE
10753 ("?H?^-bit gap before component&",
10754 Component_Name (Component_Clause (CEnt)), CEnt);
10755 end if;
10757 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10758 end loop;
10760 -- Process variant parts recursively if present
10762 if Present (Variant_Part (CL)) then
10763 Variant := First (Variants (Variant_Part (CL)));
10764 while Present (Variant) loop
10765 Check_Component_List
10766 (Component_List (Variant), Nbit, No_List);
10767 Next (Variant);
10768 end loop;
10769 end if;
10770 end;
10771 end Check_Component_List;
10773 -- Start of processing for Record_Hole_Check
10775 begin
10776 declare
10777 Sbit : Uint;
10779 begin
10780 if Is_Tagged_Type (Rectype) then
10781 Sbit := UI_From_Int (System_Address_Size);
10782 else
10783 Sbit := Uint_0;
10784 end if;
10786 if Nkind (Decl) = N_Full_Type_Declaration
10787 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10788 then
10789 Check_Component_List
10790 (Component_List (Type_Definition (Decl)),
10791 Sbit,
10792 Discriminant_Specifications (Decl));
10793 end if;
10794 end;
10795 end Record_Hole_Check;
10796 end if;
10798 -- For records that have component clauses for all components, and whose
10799 -- size is less than or equal to 32, we need to know the size in the
10800 -- front end to activate possible packed array processing where the
10801 -- component type is a record.
10803 -- At this stage Hbit + 1 represents the first unused bit from all the
10804 -- component clauses processed, so if the component clauses are
10805 -- complete, then this is the length of the record.
10807 -- For records longer than System.Storage_Unit, and for those where not
10808 -- all components have component clauses, the back end determines the
10809 -- length (it may for example be appropriate to round up the size
10810 -- to some convenient boundary, based on alignment considerations, etc).
10812 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10814 -- Nothing to do if at least one component has no component clause
10816 Comp := First_Component_Or_Discriminant (Rectype);
10817 while Present (Comp) loop
10818 exit when No (Component_Clause (Comp));
10819 Next_Component_Or_Discriminant (Comp);
10820 end loop;
10822 -- If we fall out of loop, all components have component clauses
10823 -- and so we can set the size to the maximum value.
10825 if No (Comp) then
10826 Set_RM_Size (Rectype, Hbit + 1);
10827 end if;
10828 end if;
10829 end Check_Record_Representation_Clause;
10831 ----------------
10832 -- Check_Size --
10833 ----------------
10835 procedure Check_Size
10836 (N : Node_Id;
10837 T : Entity_Id;
10838 Siz : Uint;
10839 Biased : out Boolean)
10841 UT : constant Entity_Id := Underlying_Type (T);
10842 M : Uint;
10844 begin
10845 Biased := False;
10847 -- Reject patently improper size values.
10849 if Is_Elementary_Type (T)
10850 and then Siz > UI_From_Int (Int'Last)
10851 then
10852 Error_Msg_N ("Size value too large for elementary type", N);
10854 if Nkind (Original_Node (N)) = N_Op_Expon then
10855 Error_Msg_N
10856 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10857 end if;
10858 end if;
10860 -- Dismiss generic types
10862 if Is_Generic_Type (T)
10863 or else
10864 Is_Generic_Type (UT)
10865 or else
10866 Is_Generic_Type (Root_Type (UT))
10867 then
10868 return;
10870 -- Guard against previous errors
10872 elsif No (UT) or else UT = Any_Type then
10873 Check_Error_Detected;
10874 return;
10876 -- Check case of bit packed array
10878 elsif Is_Array_Type (UT)
10879 and then Known_Static_Component_Size (UT)
10880 and then Is_Bit_Packed_Array (UT)
10881 then
10882 declare
10883 Asiz : Uint;
10884 Indx : Node_Id;
10885 Ityp : Entity_Id;
10887 begin
10888 Asiz := Component_Size (UT);
10889 Indx := First_Index (UT);
10890 loop
10891 Ityp := Etype (Indx);
10893 -- If non-static bound, then we are not in the business of
10894 -- trying to check the length, and indeed an error will be
10895 -- issued elsewhere, since sizes of non-static array types
10896 -- cannot be set implicitly or explicitly.
10898 if not Is_OK_Static_Subtype (Ityp) then
10899 return;
10900 end if;
10902 -- Otherwise accumulate next dimension
10904 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10905 Expr_Value (Type_Low_Bound (Ityp)) +
10906 Uint_1);
10908 Next_Index (Indx);
10909 exit when No (Indx);
10910 end loop;
10912 if Asiz <= Siz then
10913 return;
10915 else
10916 Error_Msg_Uint_1 := Asiz;
10917 Error_Msg_NE
10918 ("size for& too small, minimum allowed is ^", N, T);
10919 Set_Esize (T, Asiz);
10920 Set_RM_Size (T, Asiz);
10921 end if;
10922 end;
10924 -- All other composite types are ignored
10926 elsif Is_Composite_Type (UT) then
10927 return;
10929 -- For fixed-point types, don't check minimum if type is not frozen,
10930 -- since we don't know all the characteristics of the type that can
10931 -- affect the size (e.g. a specified small) till freeze time.
10933 elsif Is_Fixed_Point_Type (UT)
10934 and then not Is_Frozen (UT)
10935 then
10936 null;
10938 -- Cases for which a minimum check is required
10940 else
10941 -- Ignore if specified size is correct for the type
10943 if Known_Esize (UT) and then Siz = Esize (UT) then
10944 return;
10945 end if;
10947 -- Otherwise get minimum size
10949 M := UI_From_Int (Minimum_Size (UT));
10951 if Siz < M then
10953 -- Size is less than minimum size, but one possibility remains
10954 -- that we can manage with the new size if we bias the type.
10956 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10958 if Siz < M then
10959 Error_Msg_Uint_1 := M;
10960 Error_Msg_NE
10961 ("size for& too small, minimum allowed is ^", N, T);
10962 Set_Esize (T, M);
10963 Set_RM_Size (T, M);
10964 else
10965 Biased := True;
10966 end if;
10967 end if;
10968 end if;
10969 end Check_Size;
10971 --------------------------
10972 -- Freeze_Entity_Checks --
10973 --------------------------
10975 procedure Freeze_Entity_Checks (N : Node_Id) is
10976 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10977 -- Inspect the primitive operations of type Typ and hide all pairs of
10978 -- implicitly declared non-overridden non-fully conformant homographs
10979 -- (Ada RM 8.3 12.3/2).
10981 -------------------------------------
10982 -- Hide_Non_Overridden_Subprograms --
10983 -------------------------------------
10985 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10986 procedure Hide_Matching_Homographs
10987 (Subp_Id : Entity_Id;
10988 Start_Elmt : Elmt_Id);
10989 -- Inspect a list of primitive operations starting with Start_Elmt
10990 -- and find matching implicitly declared non-overridden non-fully
10991 -- conformant homographs of Subp_Id. If found, all matches along
10992 -- with Subp_Id are hidden from all visibility.
10994 function Is_Non_Overridden_Or_Null_Procedure
10995 (Subp_Id : Entity_Id) return Boolean;
10996 -- Determine whether subprogram Subp_Id is implicitly declared non-
10997 -- overridden subprogram or an implicitly declared null procedure.
10999 ------------------------------
11000 -- Hide_Matching_Homographs --
11001 ------------------------------
11003 procedure Hide_Matching_Homographs
11004 (Subp_Id : Entity_Id;
11005 Start_Elmt : Elmt_Id)
11007 Prim : Entity_Id;
11008 Prim_Elmt : Elmt_Id;
11010 begin
11011 Prim_Elmt := Start_Elmt;
11012 while Present (Prim_Elmt) loop
11013 Prim := Node (Prim_Elmt);
11015 -- The current primitive is implicitly declared non-overridden
11016 -- non-fully conformant homograph of Subp_Id. Both subprograms
11017 -- must be hidden from visibility.
11019 if Chars (Prim) = Chars (Subp_Id)
11020 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
11021 and then not Fully_Conformant (Prim, Subp_Id)
11022 then
11023 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
11024 Set_Is_Immediately_Visible (Prim, False);
11025 Set_Is_Potentially_Use_Visible (Prim, False);
11027 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
11028 Set_Is_Immediately_Visible (Subp_Id, False);
11029 Set_Is_Potentially_Use_Visible (Subp_Id, False);
11030 end if;
11032 Next_Elmt (Prim_Elmt);
11033 end loop;
11034 end Hide_Matching_Homographs;
11036 -----------------------------------------
11037 -- Is_Non_Overridden_Or_Null_Procedure --
11038 -----------------------------------------
11040 function Is_Non_Overridden_Or_Null_Procedure
11041 (Subp_Id : Entity_Id) return Boolean
11043 Alias_Id : Entity_Id;
11045 begin
11046 -- The subprogram is inherited (implicitly declared), it does not
11047 -- override and does not cover a primitive of an interface.
11049 if Ekind_In (Subp_Id, E_Function, E_Procedure)
11050 and then Present (Alias (Subp_Id))
11051 and then No (Interface_Alias (Subp_Id))
11052 and then No (Overridden_Operation (Subp_Id))
11053 then
11054 Alias_Id := Alias (Subp_Id);
11056 if Requires_Overriding (Alias_Id) then
11057 return True;
11059 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
11060 and then Null_Present (Parent (Alias_Id))
11061 then
11062 return True;
11063 end if;
11064 end if;
11066 return False;
11067 end Is_Non_Overridden_Or_Null_Procedure;
11069 -- Local variables
11071 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
11072 Prim : Entity_Id;
11073 Prim_Elmt : Elmt_Id;
11075 -- Start of processing for Hide_Non_Overridden_Subprograms
11077 begin
11078 -- Inspect the list of primitives looking for non-overridden
11079 -- subprograms.
11081 if Present (Prim_Ops) then
11082 Prim_Elmt := First_Elmt (Prim_Ops);
11083 while Present (Prim_Elmt) loop
11084 Prim := Node (Prim_Elmt);
11085 Next_Elmt (Prim_Elmt);
11087 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
11088 Hide_Matching_Homographs
11089 (Subp_Id => Prim,
11090 Start_Elmt => Prim_Elmt);
11091 end if;
11092 end loop;
11093 end if;
11094 end Hide_Non_Overridden_Subprograms;
11096 ---------------------
11097 -- Local variables --
11098 ---------------------
11100 E : constant Entity_Id := Entity (N);
11102 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
11103 -- True in non-generic case. Some of the processing here is skipped
11104 -- for the generic case since it is not needed. Basically in the
11105 -- generic case, we only need to do stuff that might generate error
11106 -- messages or warnings.
11108 -- Start of processing for Freeze_Entity_Checks
11110 begin
11111 -- Remember that we are processing a freezing entity. Required to
11112 -- ensure correct decoration of internal entities associated with
11113 -- interfaces (see New_Overloaded_Entity).
11115 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
11117 -- For tagged types covering interfaces add internal entities that link
11118 -- the primitives of the interfaces with the primitives that cover them.
11119 -- Note: These entities were originally generated only when generating
11120 -- code because their main purpose was to provide support to initialize
11121 -- the secondary dispatch tables. They are now generated also when
11122 -- compiling with no code generation to provide ASIS the relationship
11123 -- between interface primitives and tagged type primitives. They are
11124 -- also used to locate primitives covering interfaces when processing
11125 -- generics (see Derive_Subprograms).
11127 -- This is not needed in the generic case
11129 if Ada_Version >= Ada_2005
11130 and then Non_Generic_Case
11131 and then Ekind (E) = E_Record_Type
11132 and then Is_Tagged_Type (E)
11133 and then not Is_Interface (E)
11134 and then Has_Interfaces (E)
11135 then
11136 -- This would be a good common place to call the routine that checks
11137 -- overriding of interface primitives (and thus factorize calls to
11138 -- Check_Abstract_Overriding located at different contexts in the
11139 -- compiler). However, this is not possible because it causes
11140 -- spurious errors in case of late overriding.
11142 Add_Internal_Interface_Entities (E);
11143 end if;
11145 -- After all forms of overriding have been resolved, a tagged type may
11146 -- be left with a set of implicitly declared and possibly erroneous
11147 -- abstract subprograms, null procedures and subprograms that require
11148 -- overriding. If this set contains fully conformant homographs, then
11149 -- one is chosen arbitrarily (already done during resolution), otherwise
11150 -- all remaining non-fully conformant homographs are hidden from
11151 -- visibility (Ada RM 8.3 12.3/2).
11153 if Is_Tagged_Type (E) then
11154 Hide_Non_Overridden_Subprograms (E);
11155 end if;
11157 -- Check CPP types
11159 if Ekind (E) = E_Record_Type
11160 and then Is_CPP_Class (E)
11161 and then Is_Tagged_Type (E)
11162 and then Tagged_Type_Expansion
11163 then
11164 if CPP_Num_Prims (E) = 0 then
11166 -- If the CPP type has user defined components then it must import
11167 -- primitives from C++. This is required because if the C++ class
11168 -- has no primitives then the C++ compiler does not added the _tag
11169 -- component to the type.
11171 if First_Entity (E) /= Last_Entity (E) then
11172 Error_Msg_N
11173 ("'C'P'P type must import at least one primitive from C++??",
11175 end if;
11176 end if;
11178 -- Check that all its primitives are abstract or imported from C++.
11179 -- Check also availability of the C++ constructor.
11181 declare
11182 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
11183 Elmt : Elmt_Id;
11184 Error_Reported : Boolean := False;
11185 Prim : Node_Id;
11187 begin
11188 Elmt := First_Elmt (Primitive_Operations (E));
11189 while Present (Elmt) loop
11190 Prim := Node (Elmt);
11192 if Comes_From_Source (Prim) then
11193 if Is_Abstract_Subprogram (Prim) then
11194 null;
11196 elsif not Is_Imported (Prim)
11197 or else Convention (Prim) /= Convention_CPP
11198 then
11199 Error_Msg_N
11200 ("primitives of 'C'P'P types must be imported from C++ "
11201 & "or abstract??", Prim);
11203 elsif not Has_Constructors
11204 and then not Error_Reported
11205 then
11206 Error_Msg_Name_1 := Chars (E);
11207 Error_Msg_N
11208 ("??'C'P'P constructor required for type %", Prim);
11209 Error_Reported := True;
11210 end if;
11211 end if;
11213 Next_Elmt (Elmt);
11214 end loop;
11215 end;
11216 end if;
11218 -- Check Ada derivation of CPP type
11220 if Expander_Active -- why? losing errors in -gnatc mode???
11221 and then Present (Etype (E)) -- defend against errors
11222 and then Tagged_Type_Expansion
11223 and then Ekind (E) = E_Record_Type
11224 and then Etype (E) /= E
11225 and then Is_CPP_Class (Etype (E))
11226 and then CPP_Num_Prims (Etype (E)) > 0
11227 and then not Is_CPP_Class (E)
11228 and then not Has_CPP_Constructors (Etype (E))
11229 then
11230 -- If the parent has C++ primitives but it has no constructor then
11231 -- check that all the primitives are overridden in this derivation;
11232 -- otherwise the constructor of the parent is needed to build the
11233 -- dispatch table.
11235 declare
11236 Elmt : Elmt_Id;
11237 Prim : Node_Id;
11239 begin
11240 Elmt := First_Elmt (Primitive_Operations (E));
11241 while Present (Elmt) loop
11242 Prim := Node (Elmt);
11244 if not Is_Abstract_Subprogram (Prim)
11245 and then No (Interface_Alias (Prim))
11246 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
11247 then
11248 Error_Msg_Name_1 := Chars (Etype (E));
11249 Error_Msg_N
11250 ("'C'P'P constructor required for parent type %", E);
11251 exit;
11252 end if;
11254 Next_Elmt (Elmt);
11255 end loop;
11256 end;
11257 end if;
11259 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
11261 -- If we have a type with predicates, build predicate function. This
11262 -- is not needed in the generic case, and is not needed within TSS
11263 -- subprograms and other predefined primitives.
11265 if Non_Generic_Case
11266 and then Is_Type (E)
11267 and then Has_Predicates (E)
11268 and then not Within_Internal_Subprogram
11269 then
11270 Build_Predicate_Functions (E, N);
11271 end if;
11273 -- If type has delayed aspects, this is where we do the preanalysis at
11274 -- the freeze point, as part of the consistent visibility check. Note
11275 -- that this must be done after calling Build_Predicate_Functions or
11276 -- Build_Invariant_Procedure since these subprograms fix occurrences of
11277 -- the subtype name in the saved expression so that they will not cause
11278 -- trouble in the preanalysis.
11280 -- This is also not needed in the generic case
11282 if Non_Generic_Case
11283 and then Has_Delayed_Aspects (E)
11284 and then Scope (E) = Current_Scope
11285 then
11286 -- Retrieve the visibility to the discriminants in order to properly
11287 -- analyze the aspects.
11289 Push_Scope_And_Install_Discriminants (E);
11291 declare
11292 Ritem : Node_Id;
11294 begin
11295 -- Look for aspect specification entries for this entity
11297 Ritem := First_Rep_Item (E);
11298 while Present (Ritem) loop
11299 if Nkind (Ritem) = N_Aspect_Specification
11300 and then Entity (Ritem) = E
11301 and then Is_Delayed_Aspect (Ritem)
11302 then
11303 Check_Aspect_At_Freeze_Point (Ritem);
11304 end if;
11306 Next_Rep_Item (Ritem);
11307 end loop;
11308 end;
11310 Uninstall_Discriminants_And_Pop_Scope (E);
11311 end if;
11313 -- For a record type, deal with variant parts. This has to be delayed
11314 -- to this point, because of the issue of statically predicated
11315 -- subtypes, which we have to ensure are frozen before checking
11316 -- choices, since we need to have the static choice list set.
11318 if Is_Record_Type (E) then
11319 Check_Variant_Part : declare
11320 D : constant Node_Id := Declaration_Node (E);
11321 T : Node_Id;
11322 C : Node_Id;
11323 VP : Node_Id;
11325 Others_Present : Boolean;
11326 pragma Warnings (Off, Others_Present);
11327 -- Indicates others present, not used in this case
11329 procedure Non_Static_Choice_Error (Choice : Node_Id);
11330 -- Error routine invoked by the generic instantiation below when
11331 -- the variant part has a non static choice.
11333 procedure Process_Declarations (Variant : Node_Id);
11334 -- Processes declarations associated with a variant. We analyzed
11335 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
11336 -- but we still need the recursive call to Check_Choices for any
11337 -- nested variant to get its choices properly processed. This is
11338 -- also where we expand out the choices if expansion is active.
11340 package Variant_Choices_Processing is new
11341 Generic_Check_Choices
11342 (Process_Empty_Choice => No_OP,
11343 Process_Non_Static_Choice => Non_Static_Choice_Error,
11344 Process_Associated_Node => Process_Declarations);
11345 use Variant_Choices_Processing;
11347 -----------------------------
11348 -- Non_Static_Choice_Error --
11349 -----------------------------
11351 procedure Non_Static_Choice_Error (Choice : Node_Id) is
11352 begin
11353 Flag_Non_Static_Expr
11354 ("choice given in variant part is not static!", Choice);
11355 end Non_Static_Choice_Error;
11357 --------------------------
11358 -- Process_Declarations --
11359 --------------------------
11361 procedure Process_Declarations (Variant : Node_Id) is
11362 CL : constant Node_Id := Component_List (Variant);
11363 VP : Node_Id;
11365 begin
11366 -- Check for static predicate present in this variant
11368 if Has_SP_Choice (Variant) then
11370 -- Here we expand. You might expect to find this call in
11371 -- Expand_N_Variant_Part, but that is called when we first
11372 -- see the variant part, and we cannot do this expansion
11373 -- earlier than the freeze point, since for statically
11374 -- predicated subtypes, the predicate is not known till
11375 -- the freeze point.
11377 -- Furthermore, we do this expansion even if the expander
11378 -- is not active, because other semantic processing, e.g.
11379 -- for aggregates, requires the expanded list of choices.
11381 -- If the expander is not active, then we can't just clobber
11382 -- the list since it would invalidate the ASIS -gnatct tree.
11383 -- So we have to rewrite the variant part with a Rewrite
11384 -- call that replaces it with a copy and clobber the copy.
11386 if not Expander_Active then
11387 declare
11388 NewV : constant Node_Id := New_Copy (Variant);
11389 begin
11390 Set_Discrete_Choices
11391 (NewV, New_Copy_List (Discrete_Choices (Variant)));
11392 Rewrite (Variant, NewV);
11393 end;
11394 end if;
11396 Expand_Static_Predicates_In_Choices (Variant);
11397 end if;
11399 -- We don't need to worry about the declarations in the variant
11400 -- (since they were analyzed by Analyze_Choices when we first
11401 -- encountered the variant), but we do need to take care of
11402 -- expansion of any nested variants.
11404 if not Null_Present (CL) then
11405 VP := Variant_Part (CL);
11407 if Present (VP) then
11408 Check_Choices
11409 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11410 end if;
11411 end if;
11412 end Process_Declarations;
11414 -- Start of processing for Check_Variant_Part
11416 begin
11417 -- Find component list
11419 C := Empty;
11421 if Nkind (D) = N_Full_Type_Declaration then
11422 T := Type_Definition (D);
11424 if Nkind (T) = N_Record_Definition then
11425 C := Component_List (T);
11427 elsif Nkind (T) = N_Derived_Type_Definition
11428 and then Present (Record_Extension_Part (T))
11429 then
11430 C := Component_List (Record_Extension_Part (T));
11431 end if;
11432 end if;
11434 -- Case of variant part present
11436 if Present (C) and then Present (Variant_Part (C)) then
11437 VP := Variant_Part (C);
11439 -- Check choices
11441 Check_Choices
11442 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11444 -- If the last variant does not contain the Others choice,
11445 -- replace it with an N_Others_Choice node since Gigi always
11446 -- wants an Others. Note that we do not bother to call Analyze
11447 -- on the modified variant part, since its only effect would be
11448 -- to compute the Others_Discrete_Choices node laboriously, and
11449 -- of course we already know the list of choices corresponding
11450 -- to the others choice (it's the list we're replacing).
11452 -- We only want to do this if the expander is active, since
11453 -- we do not want to clobber the ASIS tree.
11455 if Expander_Active then
11456 declare
11457 Last_Var : constant Node_Id :=
11458 Last_Non_Pragma (Variants (VP));
11460 Others_Node : Node_Id;
11462 begin
11463 if Nkind (First (Discrete_Choices (Last_Var))) /=
11464 N_Others_Choice
11465 then
11466 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11467 Set_Others_Discrete_Choices
11468 (Others_Node, Discrete_Choices (Last_Var));
11469 Set_Discrete_Choices
11470 (Last_Var, New_List (Others_Node));
11471 end if;
11472 end;
11473 end if;
11474 end if;
11475 end Check_Variant_Part;
11476 end if;
11477 end Freeze_Entity_Checks;
11479 -------------------------
11480 -- Get_Alignment_Value --
11481 -------------------------
11483 function Get_Alignment_Value (Expr : Node_Id) return Uint is
11484 Align : constant Uint := Static_Integer (Expr);
11486 begin
11487 if Align = No_Uint then
11488 return No_Uint;
11490 elsif Align <= 0 then
11491 Error_Msg_N ("alignment value must be positive", Expr);
11492 return No_Uint;
11494 else
11495 for J in Int range 0 .. 64 loop
11496 declare
11497 M : constant Uint := Uint_2 ** J;
11499 begin
11500 exit when M = Align;
11502 if M > Align then
11503 Error_Msg_N
11504 ("alignment value must be power of 2", Expr);
11505 return No_Uint;
11506 end if;
11507 end;
11508 end loop;
11510 return Align;
11511 end if;
11512 end Get_Alignment_Value;
11514 -----------------------------
11515 -- Get_Interfacing_Aspects --
11516 -----------------------------
11518 procedure Get_Interfacing_Aspects
11519 (Iface_Asp : Node_Id;
11520 Conv_Asp : out Node_Id;
11521 EN_Asp : out Node_Id;
11522 Expo_Asp : out Node_Id;
11523 Imp_Asp : out Node_Id;
11524 LN_Asp : out Node_Id;
11525 Do_Checks : Boolean := False)
11527 procedure Save_Or_Duplication_Error
11528 (Asp : Node_Id;
11529 To : in out Node_Id);
11530 -- Save the value of aspect Asp in node To. If To already has a value,
11531 -- then this is considered a duplicate use of aspect. Emit an error if
11532 -- flag Do_Checks is set.
11534 -------------------------------
11535 -- Save_Or_Duplication_Error --
11536 -------------------------------
11538 procedure Save_Or_Duplication_Error
11539 (Asp : Node_Id;
11540 To : in out Node_Id)
11542 begin
11543 -- Detect an extra aspect and issue an error
11545 if Present (To) then
11546 if Do_Checks then
11547 Error_Msg_Name_1 := Chars (Identifier (Asp));
11548 Error_Msg_Sloc := Sloc (To);
11549 Error_Msg_N ("aspect % previously given #", Asp);
11550 end if;
11552 -- Otherwise capture the aspect
11554 else
11555 To := Asp;
11556 end if;
11557 end Save_Or_Duplication_Error;
11559 -- Local variables
11561 Asp : Node_Id;
11562 Asp_Id : Aspect_Id;
11564 -- The following variables capture each individual aspect
11566 Conv : Node_Id := Empty;
11567 EN : Node_Id := Empty;
11568 Expo : Node_Id := Empty;
11569 Imp : Node_Id := Empty;
11570 LN : Node_Id := Empty;
11572 -- Start of processing for Get_Interfacing_Aspects
11574 begin
11575 -- The input interfacing aspect should reside in an aspect specification
11576 -- list.
11578 pragma Assert (Is_List_Member (Iface_Asp));
11580 -- Examine the aspect specifications of the related entity. Find and
11581 -- capture all interfacing aspects. Detect duplicates and emit errors
11582 -- if applicable.
11584 Asp := First (List_Containing (Iface_Asp));
11585 while Present (Asp) loop
11586 Asp_Id := Get_Aspect_Id (Asp);
11588 if Asp_Id = Aspect_Convention then
11589 Save_Or_Duplication_Error (Asp, Conv);
11591 elsif Asp_Id = Aspect_External_Name then
11592 Save_Or_Duplication_Error (Asp, EN);
11594 elsif Asp_Id = Aspect_Export then
11595 Save_Or_Duplication_Error (Asp, Expo);
11597 elsif Asp_Id = Aspect_Import then
11598 Save_Or_Duplication_Error (Asp, Imp);
11600 elsif Asp_Id = Aspect_Link_Name then
11601 Save_Or_Duplication_Error (Asp, LN);
11602 end if;
11604 Next (Asp);
11605 end loop;
11607 Conv_Asp := Conv;
11608 EN_Asp := EN;
11609 Expo_Asp := Expo;
11610 Imp_Asp := Imp;
11611 LN_Asp := LN;
11612 end Get_Interfacing_Aspects;
11614 -------------------------------------
11615 -- Inherit_Aspects_At_Freeze_Point --
11616 -------------------------------------
11618 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11619 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11620 (Rep_Item : Node_Id) return Boolean;
11621 -- This routine checks if Rep_Item is either a pragma or an aspect
11622 -- specification node whose correponding pragma (if any) is present in
11623 -- the Rep Item chain of the entity it has been specified to.
11625 --------------------------------------------------
11626 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11627 --------------------------------------------------
11629 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11630 (Rep_Item : Node_Id) return Boolean
11632 begin
11633 return
11634 Nkind (Rep_Item) = N_Pragma
11635 or else Present_In_Rep_Item
11636 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
11637 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11639 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11641 begin
11642 -- A representation item is either subtype-specific (Size and Alignment
11643 -- clauses) or type-related (all others). Subtype-specific aspects may
11644 -- differ for different subtypes of the same type (RM 13.1.8).
11646 -- A derived type inherits each type-related representation aspect of
11647 -- its parent type that was directly specified before the declaration of
11648 -- the derived type (RM 13.1.15).
11650 -- A derived subtype inherits each subtype-specific representation
11651 -- aspect of its parent subtype that was directly specified before the
11652 -- declaration of the derived type (RM 13.1.15).
11654 -- The general processing involves inheriting a representation aspect
11655 -- from a parent type whenever the first rep item (aspect specification,
11656 -- attribute definition clause, pragma) corresponding to the given
11657 -- representation aspect in the rep item chain of Typ, if any, isn't
11658 -- directly specified to Typ but to one of its parents.
11660 -- ??? Note that, for now, just a limited number of representation
11661 -- aspects have been inherited here so far. Many of them are
11662 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11663 -- a non- exhaustive list of aspects that likely also need to
11664 -- be moved to this routine: Alignment, Component_Alignment,
11665 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
11666 -- Preelaborable_Initialization, RM_Size and Small.
11668 -- In addition, Convention must be propagated from base type to subtype,
11669 -- because the subtype may have been declared on an incomplete view.
11671 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11672 return;
11673 end if;
11675 -- Ada_05/Ada_2005
11677 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11678 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11679 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11680 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11681 then
11682 Set_Is_Ada_2005_Only (Typ);
11683 end if;
11685 -- Ada_12/Ada_2012
11687 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11688 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11689 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11690 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11691 then
11692 Set_Is_Ada_2012_Only (Typ);
11693 end if;
11695 -- Atomic/Shared
11697 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11698 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11699 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11700 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11701 then
11702 Set_Is_Atomic (Typ);
11703 Set_Is_Volatile (Typ);
11704 Set_Treat_As_Volatile (Typ);
11705 end if;
11707 -- Convention
11709 if Is_Record_Type (Typ)
11710 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11711 then
11712 Set_Convention (Typ, Convention (Base_Type (Typ)));
11713 end if;
11715 -- Default_Component_Value
11717 -- Verify that there is no rep_item declared for the type, and there
11718 -- is one coming from an ancestor.
11720 if Is_Array_Type (Typ)
11721 and then Is_Base_Type (Typ)
11722 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
11723 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11724 then
11725 Set_Default_Aspect_Component_Value (Typ,
11726 Default_Aspect_Component_Value
11727 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11728 end if;
11730 -- Default_Value
11732 if Is_Scalar_Type (Typ)
11733 and then Is_Base_Type (Typ)
11734 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
11735 and then Has_Rep_Item (Typ, Name_Default_Value)
11736 then
11737 Set_Has_Default_Aspect (Typ);
11738 Set_Default_Aspect_Value (Typ,
11739 Default_Aspect_Value
11740 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11741 end if;
11743 -- Discard_Names
11745 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11746 and then Has_Rep_Item (Typ, Name_Discard_Names)
11747 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11748 (Get_Rep_Item (Typ, Name_Discard_Names))
11749 then
11750 Set_Discard_Names (Typ);
11751 end if;
11753 -- Invariants
11755 if not Has_Rep_Item (Typ, Name_Invariant, False)
11756 and then Has_Rep_Item (Typ, Name_Invariant)
11757 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11758 (Get_Rep_Item (Typ, Name_Invariant))
11759 then
11760 Set_Has_Invariants (Typ);
11762 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
11763 Set_Has_Inheritable_Invariants (Typ);
11764 end if;
11766 -- If we have a subtype with invariants, whose base type does not have
11767 -- invariants, copy these invariants to the base type. This happens for
11768 -- the case of implicit base types created for scalar and array types.
11770 elsif Has_Invariants (Typ)
11771 and then not Has_Invariants (Base_Type (Typ))
11772 then
11773 Set_Has_Invariants (Base_Type (Typ));
11774 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
11775 end if;
11777 -- Volatile
11779 if not Has_Rep_Item (Typ, Name_Volatile, False)
11780 and then Has_Rep_Item (Typ, Name_Volatile)
11781 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11782 (Get_Rep_Item (Typ, Name_Volatile))
11783 then
11784 Set_Is_Volatile (Typ);
11785 Set_Treat_As_Volatile (Typ);
11786 end if;
11788 -- Volatile_Full_Access
11790 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11791 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11792 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11793 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11794 then
11795 Set_Is_Volatile_Full_Access (Typ);
11796 Set_Is_Volatile (Typ);
11797 Set_Treat_As_Volatile (Typ);
11798 end if;
11800 -- Inheritance for derived types only
11802 if Is_Derived_Type (Typ) then
11803 declare
11804 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11805 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11807 begin
11808 -- Atomic_Components
11810 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11811 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11812 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11813 (Get_Rep_Item (Typ, Name_Atomic_Components))
11814 then
11815 Set_Has_Atomic_Components (Imp_Bas_Typ);
11816 end if;
11818 -- Volatile_Components
11820 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11821 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11822 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11823 (Get_Rep_Item (Typ, Name_Volatile_Components))
11824 then
11825 Set_Has_Volatile_Components (Imp_Bas_Typ);
11826 end if;
11828 -- Finalize_Storage_Only
11830 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11831 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11832 then
11833 Set_Finalize_Storage_Only (Bas_Typ);
11834 end if;
11836 -- Universal_Aliasing
11838 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11839 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11840 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11841 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11842 then
11843 Set_Universal_Aliasing (Imp_Bas_Typ);
11844 end if;
11846 -- Bit_Order
11848 if Is_Record_Type (Typ) then
11849 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11850 and then Has_Rep_Item (Typ, Name_Bit_Order)
11851 then
11852 Set_Reverse_Bit_Order (Bas_Typ,
11853 Reverse_Bit_Order (Entity (Name
11854 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11855 end if;
11856 end if;
11858 -- Scalar_Storage_Order
11860 -- Note: the aspect is specified on a first subtype, but recorded
11861 -- in a flag of the base type!
11863 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11864 and then Typ = Bas_Typ
11865 then
11866 -- For a type extension, always inherit from parent; otherwise
11867 -- inherit if no default applies. Note: we do not check for
11868 -- an explicit rep item on the parent type when inheriting,
11869 -- because the parent SSO may itself have been set by default.
11871 if not Has_Rep_Item (First_Subtype (Typ),
11872 Name_Scalar_Storage_Order, False)
11873 and then (Is_Tagged_Type (Bas_Typ)
11874 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11875 or else
11876 SSO_Set_High_By_Default (Bas_Typ)))
11877 then
11878 Set_Reverse_Storage_Order (Bas_Typ,
11879 Reverse_Storage_Order
11880 (Implementation_Base_Type (Etype (Bas_Typ))));
11882 -- Clear default SSO indications, since the inherited aspect
11883 -- which was set explicitly overrides the default.
11885 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11886 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11887 end if;
11888 end if;
11889 end;
11890 end if;
11891 end Inherit_Aspects_At_Freeze_Point;
11893 ----------------
11894 -- Initialize --
11895 ----------------
11897 procedure Initialize is
11898 begin
11899 Address_Clause_Checks.Init;
11900 Unchecked_Conversions.Init;
11902 if AAMP_On_Target then
11903 Independence_Checks.Init;
11904 end if;
11905 end Initialize;
11907 ---------------------------
11908 -- Install_Discriminants --
11909 ---------------------------
11911 procedure Install_Discriminants (E : Entity_Id) is
11912 Disc : Entity_Id;
11913 Prev : Entity_Id;
11914 begin
11915 Disc := First_Discriminant (E);
11916 while Present (Disc) loop
11917 Prev := Current_Entity (Disc);
11918 Set_Current_Entity (Disc);
11919 Set_Is_Immediately_Visible (Disc);
11920 Set_Homonym (Disc, Prev);
11921 Next_Discriminant (Disc);
11922 end loop;
11923 end Install_Discriminants;
11925 -------------------------
11926 -- Is_Operational_Item --
11927 -------------------------
11929 function Is_Operational_Item (N : Node_Id) return Boolean is
11930 begin
11931 if Nkind (N) /= N_Attribute_Definition_Clause then
11932 return False;
11934 else
11935 declare
11936 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11937 begin
11939 -- List of operational items is given in AARM 13.1(8.mm/1).
11940 -- It is clearly incomplete, as it does not include iterator
11941 -- aspects, among others.
11943 return Id = Attribute_Constant_Indexing
11944 or else Id = Attribute_Default_Iterator
11945 or else Id = Attribute_Implicit_Dereference
11946 or else Id = Attribute_Input
11947 or else Id = Attribute_Iterator_Element
11948 or else Id = Attribute_Iterable
11949 or else Id = Attribute_Output
11950 or else Id = Attribute_Read
11951 or else Id = Attribute_Variable_Indexing
11952 or else Id = Attribute_Write
11953 or else Id = Attribute_External_Tag;
11954 end;
11955 end if;
11956 end Is_Operational_Item;
11958 -------------------------
11959 -- Is_Predicate_Static --
11960 -------------------------
11962 -- Note: the basic legality of the expression has already been checked, so
11963 -- we don't need to worry about cases or ranges on strings for example.
11965 function Is_Predicate_Static
11966 (Expr : Node_Id;
11967 Nam : Name_Id) return Boolean
11969 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11970 -- Given a list of case expression alternatives, returns True if all
11971 -- the alternatives are static (have all static choices, and a static
11972 -- expression).
11974 function All_Static_Choices (L : List_Id) return Boolean;
11975 -- Returns true if all elements of the list are OK static choices
11976 -- as defined below for Is_Static_Choice. Used for case expression
11977 -- alternatives and for the right operand of a membership test. An
11978 -- others_choice is static if the corresponding expression is static.
11979 -- The staticness of the bounds is checked separately.
11981 function Is_Static_Choice (N : Node_Id) return Boolean;
11982 -- Returns True if N represents a static choice (static subtype, or
11983 -- static subtype indication, or static expression, or static range).
11985 -- Note that this is a bit more inclusive than we actually need
11986 -- (in particular membership tests do not allow the use of subtype
11987 -- indications). But that doesn't matter, we have already checked
11988 -- that the construct is legal to get this far.
11990 function Is_Type_Ref (N : Node_Id) return Boolean;
11991 pragma Inline (Is_Type_Ref);
11992 -- Returns True if N is a reference to the type for the predicate in the
11993 -- expression (i.e. if it is an identifier whose Chars field matches the
11994 -- Nam given in the call). N must not be parenthesized, if the type name
11995 -- appears in parens, this routine will return False.
11997 ----------------------------------
11998 -- All_Static_Case_Alternatives --
11999 ----------------------------------
12001 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
12002 N : Node_Id;
12004 begin
12005 N := First (L);
12006 while Present (N) loop
12007 if not (All_Static_Choices (Discrete_Choices (N))
12008 and then Is_OK_Static_Expression (Expression (N)))
12009 then
12010 return False;
12011 end if;
12013 Next (N);
12014 end loop;
12016 return True;
12017 end All_Static_Case_Alternatives;
12019 ------------------------
12020 -- All_Static_Choices --
12021 ------------------------
12023 function All_Static_Choices (L : List_Id) return Boolean is
12024 N : Node_Id;
12026 begin
12027 N := First (L);
12028 while Present (N) loop
12029 if not Is_Static_Choice (N) then
12030 return False;
12031 end if;
12033 Next (N);
12034 end loop;
12036 return True;
12037 end All_Static_Choices;
12039 ----------------------
12040 -- Is_Static_Choice --
12041 ----------------------
12043 function Is_Static_Choice (N : Node_Id) return Boolean is
12044 begin
12045 return Nkind (N) = N_Others_Choice
12046 or else Is_OK_Static_Expression (N)
12047 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
12048 and then Is_OK_Static_Subtype (Entity (N)))
12049 or else (Nkind (N) = N_Subtype_Indication
12050 and then Is_OK_Static_Subtype (Entity (N)))
12051 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
12052 end Is_Static_Choice;
12054 -----------------
12055 -- Is_Type_Ref --
12056 -----------------
12058 function Is_Type_Ref (N : Node_Id) return Boolean is
12059 begin
12060 return Nkind (N) = N_Identifier
12061 and then Chars (N) = Nam
12062 and then Paren_Count (N) = 0;
12063 end Is_Type_Ref;
12065 -- Start of processing for Is_Predicate_Static
12067 begin
12068 -- Predicate_Static means one of the following holds. Numbers are the
12069 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
12071 -- 16: A static expression
12073 if Is_OK_Static_Expression (Expr) then
12074 return True;
12076 -- 17: A membership test whose simple_expression is the current
12077 -- instance, and whose membership_choice_list meets the requirements
12078 -- for a static membership test.
12080 elsif Nkind (Expr) in N_Membership_Test
12081 and then ((Present (Right_Opnd (Expr))
12082 and then Is_Static_Choice (Right_Opnd (Expr)))
12083 or else
12084 (Present (Alternatives (Expr))
12085 and then All_Static_Choices (Alternatives (Expr))))
12086 then
12087 return True;
12089 -- 18. A case_expression whose selecting_expression is the current
12090 -- instance, and whose dependent expressions are static expressions.
12092 elsif Nkind (Expr) = N_Case_Expression
12093 and then Is_Type_Ref (Expression (Expr))
12094 and then All_Static_Case_Alternatives (Alternatives (Expr))
12095 then
12096 return True;
12098 -- 19. A call to a predefined equality or ordering operator, where one
12099 -- operand is the current instance, and the other is a static
12100 -- expression.
12102 -- Note: the RM is clearly wrong here in not excluding string types.
12103 -- Without this exclusion, we would allow expressions like X > "ABC"
12104 -- to be considered as predicate-static, which is clearly not intended,
12105 -- since the idea is for predicate-static to be a subset of normal
12106 -- static expressions (and "DEF" > "ABC" is not a static expression).
12108 -- However, we do allow internally generated (not from source) equality
12109 -- and inequality operations to be valid on strings (this helps deal
12110 -- with cases where we transform A in "ABC" to A = "ABC).
12112 elsif Nkind (Expr) in N_Op_Compare
12113 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
12114 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
12115 and then not Comes_From_Source (Expr)))
12116 and then ((Is_Type_Ref (Left_Opnd (Expr))
12117 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
12118 or else
12119 (Is_Type_Ref (Right_Opnd (Expr))
12120 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
12121 then
12122 return True;
12124 -- 20. A call to a predefined boolean logical operator, where each
12125 -- operand is predicate-static.
12127 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
12128 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
12129 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
12130 or else
12131 (Nkind (Expr) = N_Op_Not
12132 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
12133 then
12134 return True;
12136 -- 21. A short-circuit control form where both operands are
12137 -- predicate-static.
12139 elsif Nkind (Expr) in N_Short_Circuit
12140 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
12141 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
12142 then
12143 return True;
12145 -- 22. A parenthesized predicate-static expression. This does not
12146 -- require any special test, since we just ignore paren levels in
12147 -- all the cases above.
12149 -- One more test that is an implementation artifact caused by the fact
12150 -- that we are analyzing not the original expression, but the generated
12151 -- expression in the body of the predicate function. This can include
12152 -- references to inherited predicates, so that the expression we are
12153 -- processing looks like:
12155 -- xxPredicate (typ (Inns)) and then expression
12157 -- Where the call is to a Predicate function for an inherited predicate.
12158 -- We simply ignore such a call, which could be to either a dynamic or
12159 -- a static predicate. Note that if the parent predicate is dynamic then
12160 -- eventually this type will be marked as dynamic, but you are allowed
12161 -- to specify a static predicate for a subtype which is inheriting a
12162 -- dynamic predicate, so the static predicate validation here ignores
12163 -- the inherited predicate even if it is dynamic.
12165 elsif Nkind (Expr) = N_Function_Call
12166 and then Is_Predicate_Function (Entity (Name (Expr)))
12167 then
12168 return True;
12170 -- That's an exhaustive list of tests, all other cases are not
12171 -- predicate-static, so we return False.
12173 else
12174 return False;
12175 end if;
12176 end Is_Predicate_Static;
12178 ---------------------
12179 -- Kill_Rep_Clause --
12180 ---------------------
12182 procedure Kill_Rep_Clause (N : Node_Id) is
12183 begin
12184 pragma Assert (Ignore_Rep_Clauses);
12186 -- Note: we use Replace rather than Rewrite, because we don't want
12187 -- ASIS to be able to use Original_Node to dig out the (undecorated)
12188 -- rep clause that is being replaced.
12190 Replace (N, Make_Null_Statement (Sloc (N)));
12192 -- The null statement must be marked as not coming from source. This is
12193 -- so that ASIS ignores it, and also the back end does not expect bogus
12194 -- "from source" null statements in weird places (e.g. in declarative
12195 -- regions where such null statements are not allowed).
12197 Set_Comes_From_Source (N, False);
12198 end Kill_Rep_Clause;
12200 ------------------
12201 -- Minimum_Size --
12202 ------------------
12204 function Minimum_Size
12205 (T : Entity_Id;
12206 Biased : Boolean := False) return Nat
12208 Lo : Uint := No_Uint;
12209 Hi : Uint := No_Uint;
12210 LoR : Ureal := No_Ureal;
12211 HiR : Ureal := No_Ureal;
12212 LoSet : Boolean := False;
12213 HiSet : Boolean := False;
12214 B : Uint;
12215 S : Nat;
12216 Ancest : Entity_Id;
12217 R_Typ : constant Entity_Id := Root_Type (T);
12219 begin
12220 -- If bad type, return 0
12222 if T = Any_Type then
12223 return 0;
12225 -- For generic types, just return zero. There cannot be any legitimate
12226 -- need to know such a size, but this routine may be called with a
12227 -- generic type as part of normal processing.
12229 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
12230 return 0;
12232 -- Access types (cannot have size smaller than System.Address)
12234 elsif Is_Access_Type (T) then
12235 return System_Address_Size;
12237 -- Floating-point types
12239 elsif Is_Floating_Point_Type (T) then
12240 return UI_To_Int (Esize (R_Typ));
12242 -- Discrete types
12244 elsif Is_Discrete_Type (T) then
12246 -- The following loop is looking for the nearest compile time known
12247 -- bounds following the ancestor subtype chain. The idea is to find
12248 -- the most restrictive known bounds information.
12250 Ancest := T;
12251 loop
12252 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12253 return 0;
12254 end if;
12256 if not LoSet then
12257 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
12258 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
12259 LoSet := True;
12260 exit when HiSet;
12261 end if;
12262 end if;
12264 if not HiSet then
12265 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
12266 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
12267 HiSet := True;
12268 exit when LoSet;
12269 end if;
12270 end if;
12272 Ancest := Ancestor_Subtype (Ancest);
12274 if No (Ancest) then
12275 Ancest := Base_Type (T);
12277 if Is_Generic_Type (Ancest) then
12278 return 0;
12279 end if;
12280 end if;
12281 end loop;
12283 -- Fixed-point types. We can't simply use Expr_Value to get the
12284 -- Corresponding_Integer_Value values of the bounds, since these do not
12285 -- get set till the type is frozen, and this routine can be called
12286 -- before the type is frozen. Similarly the test for bounds being static
12287 -- needs to include the case where we have unanalyzed real literals for
12288 -- the same reason.
12290 elsif Is_Fixed_Point_Type (T) then
12292 -- The following loop is looking for the nearest compile time known
12293 -- bounds following the ancestor subtype chain. The idea is to find
12294 -- the most restrictive known bounds information.
12296 Ancest := T;
12297 loop
12298 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12299 return 0;
12300 end if;
12302 -- Note: In the following two tests for LoSet and HiSet, it may
12303 -- seem redundant to test for N_Real_Literal here since normally
12304 -- one would assume that the test for the value being known at
12305 -- compile time includes this case. However, there is a glitch.
12306 -- If the real literal comes from folding a non-static expression,
12307 -- then we don't consider any non- static expression to be known
12308 -- at compile time if we are in configurable run time mode (needed
12309 -- in some cases to give a clearer definition of what is and what
12310 -- is not accepted). So the test is indeed needed. Without it, we
12311 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
12313 if not LoSet then
12314 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
12315 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
12316 then
12317 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
12318 LoSet := True;
12319 exit when HiSet;
12320 end if;
12321 end if;
12323 if not HiSet then
12324 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
12325 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
12326 then
12327 HiR := Expr_Value_R (Type_High_Bound (Ancest));
12328 HiSet := True;
12329 exit when LoSet;
12330 end if;
12331 end if;
12333 Ancest := Ancestor_Subtype (Ancest);
12335 if No (Ancest) then
12336 Ancest := Base_Type (T);
12338 if Is_Generic_Type (Ancest) then
12339 return 0;
12340 end if;
12341 end if;
12342 end loop;
12344 Lo := UR_To_Uint (LoR / Small_Value (T));
12345 Hi := UR_To_Uint (HiR / Small_Value (T));
12347 -- No other types allowed
12349 else
12350 raise Program_Error;
12351 end if;
12353 -- Fall through with Hi and Lo set. Deal with biased case
12355 if (Biased
12356 and then not Is_Fixed_Point_Type (T)
12357 and then not (Is_Enumeration_Type (T)
12358 and then Has_Non_Standard_Rep (T)))
12359 or else Has_Biased_Representation (T)
12360 then
12361 Hi := Hi - Lo;
12362 Lo := Uint_0;
12363 end if;
12365 -- Null range case, size is always zero. We only do this in the discrete
12366 -- type case, since that's the odd case that came up. Probably we should
12367 -- also do this in the fixed-point case, but doing so causes peculiar
12368 -- gigi failures, and it is not worth worrying about this incredibly
12369 -- marginal case (explicit null-range fixed-point type declarations)???
12371 if Lo > Hi and then Is_Discrete_Type (T) then
12372 S := 0;
12374 -- Signed case. Note that we consider types like range 1 .. -1 to be
12375 -- signed for the purpose of computing the size, since the bounds have
12376 -- to be accommodated in the base type.
12378 elsif Lo < 0 or else Hi < 0 then
12379 S := 1;
12380 B := Uint_1;
12382 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
12383 -- Note that we accommodate the case where the bounds cross. This
12384 -- can happen either because of the way the bounds are declared
12385 -- or because of the algorithm in Freeze_Fixed_Point_Type.
12387 while Lo < -B
12388 or else Hi < -B
12389 or else Lo >= B
12390 or else Hi >= B
12391 loop
12392 B := Uint_2 ** S;
12393 S := S + 1;
12394 end loop;
12396 -- Unsigned case
12398 else
12399 -- If both bounds are positive, make sure that both are represen-
12400 -- table in the case where the bounds are crossed. This can happen
12401 -- either because of the way the bounds are declared, or because of
12402 -- the algorithm in Freeze_Fixed_Point_Type.
12404 if Lo > Hi then
12405 Hi := Lo;
12406 end if;
12408 -- S = size, (can accommodate 0 .. (2**size - 1))
12410 S := 0;
12411 while Hi >= Uint_2 ** S loop
12412 S := S + 1;
12413 end loop;
12414 end if;
12416 return S;
12417 end Minimum_Size;
12419 ---------------------------
12420 -- New_Stream_Subprogram --
12421 ---------------------------
12423 procedure New_Stream_Subprogram
12424 (N : Node_Id;
12425 Ent : Entity_Id;
12426 Subp : Entity_Id;
12427 Nam : TSS_Name_Type)
12429 Loc : constant Source_Ptr := Sloc (N);
12430 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
12431 Subp_Id : Entity_Id;
12432 Subp_Decl : Node_Id;
12433 F : Entity_Id;
12434 Etyp : Entity_Id;
12436 Defer_Declaration : constant Boolean :=
12437 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
12438 -- For a tagged type, there is a declaration for each stream attribute
12439 -- at the freeze point, and we must generate only a completion of this
12440 -- declaration. We do the same for private types, because the full view
12441 -- might be tagged. Otherwise we generate a declaration at the point of
12442 -- the attribute definition clause.
12444 function Build_Spec return Node_Id;
12445 -- Used for declaration and renaming declaration, so that this is
12446 -- treated as a renaming_as_body.
12448 ----------------
12449 -- Build_Spec --
12450 ----------------
12452 function Build_Spec return Node_Id is
12453 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12454 Formals : List_Id;
12455 Spec : Node_Id;
12456 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
12458 begin
12459 Subp_Id := Make_Defining_Identifier (Loc, Sname);
12461 -- S : access Root_Stream_Type'Class
12463 Formals := New_List (
12464 Make_Parameter_Specification (Loc,
12465 Defining_Identifier =>
12466 Make_Defining_Identifier (Loc, Name_S),
12467 Parameter_Type =>
12468 Make_Access_Definition (Loc,
12469 Subtype_Mark =>
12470 New_Occurrence_Of (
12471 Designated_Type (Etype (F)), Loc))));
12473 if Nam = TSS_Stream_Input then
12474 Spec :=
12475 Make_Function_Specification (Loc,
12476 Defining_Unit_Name => Subp_Id,
12477 Parameter_Specifications => Formals,
12478 Result_Definition => T_Ref);
12479 else
12480 -- V : [out] T
12482 Append_To (Formals,
12483 Make_Parameter_Specification (Loc,
12484 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12485 Out_Present => Out_P,
12486 Parameter_Type => T_Ref));
12488 Spec :=
12489 Make_Procedure_Specification (Loc,
12490 Defining_Unit_Name => Subp_Id,
12491 Parameter_Specifications => Formals);
12492 end if;
12494 return Spec;
12495 end Build_Spec;
12497 -- Start of processing for New_Stream_Subprogram
12499 begin
12500 F := First_Formal (Subp);
12502 if Ekind (Subp) = E_Procedure then
12503 Etyp := Etype (Next_Formal (F));
12504 else
12505 Etyp := Etype (Subp);
12506 end if;
12508 -- Prepare subprogram declaration and insert it as an action on the
12509 -- clause node. The visibility for this entity is used to test for
12510 -- visibility of the attribute definition clause (in the sense of
12511 -- 8.3(23) as amended by AI-195).
12513 if not Defer_Declaration then
12514 Subp_Decl :=
12515 Make_Subprogram_Declaration (Loc,
12516 Specification => Build_Spec);
12518 -- For a tagged type, there is always a visible declaration for each
12519 -- stream TSS (it is a predefined primitive operation), and the
12520 -- completion of this declaration occurs at the freeze point, which is
12521 -- not always visible at places where the attribute definition clause is
12522 -- visible. So, we create a dummy entity here for the purpose of
12523 -- tracking the visibility of the attribute definition clause itself.
12525 else
12526 Subp_Id :=
12527 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
12528 Subp_Decl :=
12529 Make_Object_Declaration (Loc,
12530 Defining_Identifier => Subp_Id,
12531 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
12532 end if;
12534 Insert_Action (N, Subp_Decl);
12535 Set_Entity (N, Subp_Id);
12537 Subp_Decl :=
12538 Make_Subprogram_Renaming_Declaration (Loc,
12539 Specification => Build_Spec,
12540 Name => New_Occurrence_Of (Subp, Loc));
12542 if Defer_Declaration then
12543 Set_TSS (Base_Type (Ent), Subp_Id);
12544 else
12545 Insert_Action (N, Subp_Decl);
12546 Copy_TSS (Subp_Id, Base_Type (Ent));
12547 end if;
12548 end New_Stream_Subprogram;
12550 ------------------------------------------
12551 -- Push_Scope_And_Install_Discriminants --
12552 ------------------------------------------
12554 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12555 begin
12556 if Has_Discriminants (E) then
12557 Push_Scope (E);
12559 -- Make discriminants visible for type declarations and protected
12560 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12562 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12563 Install_Discriminants (E);
12564 end if;
12565 end if;
12566 end Push_Scope_And_Install_Discriminants;
12568 ------------------------
12569 -- Rep_Item_Too_Early --
12570 ------------------------
12572 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
12573 begin
12574 -- Cannot apply non-operational rep items to generic types
12576 if Is_Operational_Item (N) then
12577 return False;
12579 elsif Is_Type (T)
12580 and then Is_Generic_Type (Root_Type (T))
12581 and then (Nkind (N) /= N_Pragma
12582 or else Get_Pragma_Id (N) /= Pragma_Convention)
12583 then
12584 Error_Msg_N ("representation item not allowed for generic type", N);
12585 return True;
12586 end if;
12588 -- Otherwise check for incomplete type
12590 if Is_Incomplete_Or_Private_Type (T)
12591 and then No (Underlying_Type (T))
12592 and then
12593 (Nkind (N) /= N_Pragma
12594 or else Get_Pragma_Id (N) /= Pragma_Import)
12595 then
12596 Error_Msg_N
12597 ("representation item must be after full type declaration", N);
12598 return True;
12600 -- If the type has incomplete components, a representation clause is
12601 -- illegal but stream attributes and Convention pragmas are correct.
12603 elsif Has_Private_Component (T) then
12604 if Nkind (N) = N_Pragma then
12605 return False;
12607 else
12608 Error_Msg_N
12609 ("representation item must appear after type is fully defined",
12611 return True;
12612 end if;
12613 else
12614 return False;
12615 end if;
12616 end Rep_Item_Too_Early;
12618 -----------------------
12619 -- Rep_Item_Too_Late --
12620 -----------------------
12622 function Rep_Item_Too_Late
12623 (T : Entity_Id;
12624 N : Node_Id;
12625 FOnly : Boolean := False) return Boolean
12627 S : Entity_Id;
12628 Parent_Type : Entity_Id;
12630 procedure No_Type_Rep_Item;
12631 -- Output message indicating that no type-related aspects can be
12632 -- specified due to some property of the parent type.
12634 procedure Too_Late;
12635 -- Output message for an aspect being specified too late
12637 -- Note that neither of the above errors is considered a serious one,
12638 -- since the effect is simply that we ignore the representation clause
12639 -- in these cases.
12640 -- Is this really true? In any case if we make this change we must
12641 -- document the requirement in the spec of Rep_Item_Too_Late that
12642 -- if True is returned, then the rep item must be completely ignored???
12644 ----------------------
12645 -- No_Type_Rep_Item --
12646 ----------------------
12648 procedure No_Type_Rep_Item is
12649 begin
12650 Error_Msg_N ("|type-related representation item not permitted!", N);
12651 end No_Type_Rep_Item;
12653 --------------
12654 -- Too_Late --
12655 --------------
12657 procedure Too_Late is
12658 begin
12659 -- Other compilers seem more relaxed about rep items appearing too
12660 -- late. Since analysis tools typically don't care about rep items
12661 -- anyway, no reason to be too strict about this.
12663 if not Relaxed_RM_Semantics then
12664 Error_Msg_N ("|representation item appears too late!", N);
12665 end if;
12666 end Too_Late;
12668 -- Start of processing for Rep_Item_Too_Late
12670 begin
12671 -- First make sure entity is not frozen (RM 13.1(9))
12673 if Is_Frozen (T)
12675 -- Exclude imported types, which may be frozen if they appear in a
12676 -- representation clause for a local type.
12678 and then not From_Limited_With (T)
12680 -- Exclude generated entities (not coming from source). The common
12681 -- case is when we generate a renaming which prematurely freezes the
12682 -- renamed internal entity, but we still want to be able to set copies
12683 -- of attribute values such as Size/Alignment.
12685 and then Comes_From_Source (T)
12686 then
12687 -- A self-referential aspect is illegal if it forces freezing the
12688 -- entity before the corresponding pragma has been analyzed.
12690 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12691 and then From_Aspect_Specification (N)
12692 then
12693 Error_Msg_NE
12694 ("aspect specification causes premature freezing of&", T, N);
12695 Set_Has_Delayed_Freeze (T, False);
12696 return True;
12697 end if;
12699 Too_Late;
12700 S := First_Subtype (T);
12702 if Present (Freeze_Node (S)) then
12703 if not Relaxed_RM_Semantics then
12704 Error_Msg_NE
12705 ("??no more representation items for }", Freeze_Node (S), S);
12706 end if;
12707 end if;
12709 return True;
12711 -- Check for case of untagged derived type whose parent either has
12712 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12713 -- this case we do not output a Too_Late message, since there is no
12714 -- earlier point where the rep item could be placed to make it legal.
12716 elsif Is_Type (T)
12717 and then not FOnly
12718 and then Is_Derived_Type (T)
12719 and then not Is_Tagged_Type (T)
12720 then
12721 Parent_Type := Etype (Base_Type (T));
12723 if Has_Primitive_Operations (Parent_Type) then
12724 No_Type_Rep_Item;
12726 if not Relaxed_RM_Semantics then
12727 Error_Msg_NE
12728 ("\parent type & has primitive operations!", N, Parent_Type);
12729 end if;
12731 return True;
12733 elsif Is_By_Reference_Type (Parent_Type) then
12734 No_Type_Rep_Item;
12736 if not Relaxed_RM_Semantics then
12737 Error_Msg_NE
12738 ("\parent type & is a by reference type!", N, Parent_Type);
12739 end if;
12741 return True;
12742 end if;
12743 end if;
12745 -- No error, but one more warning to consider. The RM (surprisingly)
12746 -- allows this pattern:
12748 -- type S is ...
12749 -- primitive operations for S
12750 -- type R is new S;
12751 -- rep clause for S
12753 -- Meaning that calls on the primitive operations of S for values of
12754 -- type R may require possibly expensive implicit conversion operations.
12755 -- This is not an error, but is worth a warning.
12757 if not Relaxed_RM_Semantics and then Is_Type (T) then
12758 declare
12759 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12761 begin
12762 if Present (DTL)
12763 and then Has_Primitive_Operations (Base_Type (T))
12765 -- For now, do not generate this warning for the case of aspect
12766 -- specification using Ada 2012 syntax, since we get wrong
12767 -- messages we do not understand. The whole business of derived
12768 -- types and rep items seems a bit confused when aspects are
12769 -- used, since the aspects are not evaluated till freeze time.
12771 and then not From_Aspect_Specification (N)
12772 then
12773 Error_Msg_Sloc := Sloc (DTL);
12774 Error_Msg_N
12775 ("representation item for& appears after derived type "
12776 & "declaration#??", N);
12777 Error_Msg_NE
12778 ("\may result in implicit conversions for primitive "
12779 & "operations of&??", N, T);
12780 Error_Msg_NE
12781 ("\to change representations when called with arguments "
12782 & "of type&??", N, DTL);
12783 end if;
12784 end;
12785 end if;
12787 -- No error, link item into head of chain of rep items for the entity,
12788 -- but avoid chaining if we have an overloadable entity, and the pragma
12789 -- is one that can apply to multiple overloaded entities.
12791 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
12792 declare
12793 Pname : constant Name_Id := Pragma_Name (N);
12794 begin
12795 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12796 Name_External, Name_Interface)
12797 then
12798 return False;
12799 end if;
12800 end;
12801 end if;
12803 Record_Rep_Item (T, N);
12804 return False;
12805 end Rep_Item_Too_Late;
12807 -------------------------------------
12808 -- Replace_Type_References_Generic --
12809 -------------------------------------
12811 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12812 TName : constant Name_Id := Chars (T);
12814 function Replace_Node (N : Node_Id) return Traverse_Result;
12815 -- Processes a single node in the traversal procedure below, checking
12816 -- if node N should be replaced, and if so, doing the replacement.
12818 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
12819 -- This instantiation provides the body of Replace_Type_References
12821 ------------------
12822 -- Replace_Node --
12823 ------------------
12825 function Replace_Node (N : Node_Id) return Traverse_Result is
12826 S : Entity_Id;
12827 P : Node_Id;
12829 begin
12830 -- Case of identifier
12832 if Nkind (N) = N_Identifier then
12834 -- If not the type name, check whether it is a reference to
12835 -- some other type, which must be frozen before the predicate
12836 -- function is analyzed, i.e. before the freeze node of the
12837 -- type to which the predicate applies.
12839 if Chars (N) /= TName then
12840 if Present (Current_Entity (N))
12841 and then Is_Type (Current_Entity (N))
12842 then
12843 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12844 end if;
12846 return Skip;
12848 -- Otherwise do the replacement and we are done with this node
12850 else
12851 Replace_Type_Reference (N);
12852 return Skip;
12853 end if;
12855 -- Case of selected component (which is what a qualification
12856 -- looks like in the unanalyzed tree, which is what we have.
12858 elsif Nkind (N) = N_Selected_Component then
12860 -- If selector name is not our type, keeping going (we might
12861 -- still have an occurrence of the type in the prefix).
12863 if Nkind (Selector_Name (N)) /= N_Identifier
12864 or else Chars (Selector_Name (N)) /= TName
12865 then
12866 return OK;
12868 -- Selector name is our type, check qualification
12870 else
12871 -- Loop through scopes and prefixes, doing comparison
12873 S := Current_Scope;
12874 P := Prefix (N);
12875 loop
12876 -- Continue if no more scopes or scope with no name
12878 if No (S) or else Nkind (S) not in N_Has_Chars then
12879 return OK;
12880 end if;
12882 -- Do replace if prefix is an identifier matching the
12883 -- scope that we are currently looking at.
12885 if Nkind (P) = N_Identifier
12886 and then Chars (P) = Chars (S)
12887 then
12888 Replace_Type_Reference (N);
12889 return Skip;
12890 end if;
12892 -- Go check scope above us if prefix is itself of the
12893 -- form of a selected component, whose selector matches
12894 -- the scope we are currently looking at.
12896 if Nkind (P) = N_Selected_Component
12897 and then Nkind (Selector_Name (P)) = N_Identifier
12898 and then Chars (Selector_Name (P)) = Chars (S)
12899 then
12900 S := Scope (S);
12901 P := Prefix (P);
12903 -- For anything else, we don't have a match, so keep on
12904 -- going, there are still some weird cases where we may
12905 -- still have a replacement within the prefix.
12907 else
12908 return OK;
12909 end if;
12910 end loop;
12911 end if;
12913 -- Continue for any other node kind
12915 else
12916 return OK;
12917 end if;
12918 end Replace_Node;
12920 begin
12921 Replace_Type_Refs (N);
12922 end Replace_Type_References_Generic;
12924 --------------------------------
12925 -- Resolve_Aspect_Expressions --
12926 --------------------------------
12928 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
12929 ASN : Node_Id;
12930 A_Id : Aspect_Id;
12931 Expr : Node_Id;
12933 function Resolve_Name (N : Node_Id) return Traverse_Result;
12934 -- Verify that all identifiers in the expression, with the exception
12935 -- of references to the current entity, denote visible entities. This
12936 -- is done only to detect visibility errors, as the expression will be
12937 -- properly analyzed/expanded during analysis of the predicate function
12938 -- body. We omit quantified expressions from this test, given that they
12939 -- introduce a local identifier that would require proper expansion to
12940 -- handle properly.
12942 ------------------
12943 -- Resolve_Name --
12944 ------------------
12946 function Resolve_Name (N : Node_Id) return Traverse_Result is
12947 begin
12948 if Nkind (N) = N_Selected_Component then
12949 if Nkind (Prefix (N)) = N_Identifier
12950 and then Chars (Prefix (N)) /= Chars (E)
12951 then
12952 Find_Selected_Component (N);
12953 end if;
12955 return Skip;
12957 elsif Nkind (N) = N_Identifier and then Chars (N) /= Chars (E) then
12958 Find_Direct_Name (N);
12959 Set_Entity (N, Empty);
12961 elsif Nkind (N) = N_Quantified_Expression then
12962 return Skip;
12963 end if;
12965 return OK;
12966 end Resolve_Name;
12968 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
12970 -- Start of processing for Resolve_Aspect_Expressions
12972 begin
12973 ASN := First_Rep_Item (E);
12974 while Present (ASN) loop
12975 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12976 A_Id := Get_Aspect_Id (ASN);
12977 Expr := Expression (ASN);
12979 case A_Id is
12980 -- For now we only deal with aspects that do not generate
12981 -- subprograms, or that may mention current instances of
12982 -- types. These will require special handling (???TBD).
12984 when Aspect_Predicate |
12985 Aspect_Predicate_Failure |
12986 Aspect_Invariant =>
12987 null;
12989 when Aspect_Static_Predicate |
12990 Aspect_Dynamic_Predicate =>
12992 -- Build predicate function specification and preanalyze
12993 -- expression after type replacement.
12995 if No (Predicate_Function (E)) then
12996 declare
12997 FDecl : constant Node_Id :=
12998 Build_Predicate_Function_Declaration (E);
12999 pragma Unreferenced (FDecl);
13000 begin
13001 Resolve_Aspect_Expression (Expr);
13002 end;
13003 end if;
13005 when Pre_Post_Aspects =>
13006 null;
13008 when Aspect_Iterable =>
13009 if Nkind (Expr) = N_Aggregate then
13010 declare
13011 Assoc : Node_Id;
13013 begin
13014 Assoc := First (Component_Associations (Expr));
13015 while Present (Assoc) loop
13016 Find_Direct_Name (Expression (Assoc));
13017 Next (Assoc);
13018 end loop;
13019 end;
13020 end if;
13022 when others =>
13023 if Present (Expr) then
13024 case Aspect_Argument (A_Id) is
13025 when Expression | Optional_Expression =>
13026 Analyze_And_Resolve (Expression (ASN));
13028 when Name | Optional_Name =>
13029 if Nkind (Expr) = N_Identifier then
13030 Find_Direct_Name (Expr);
13032 elsif Nkind (Expr) = N_Selected_Component then
13033 Find_Selected_Component (Expr);
13035 else
13036 null;
13037 end if;
13038 end case;
13039 end if;
13040 end case;
13041 end if;
13043 ASN := Next_Rep_Item (ASN);
13044 end loop;
13045 end Resolve_Aspect_Expressions;
13047 -------------------------
13048 -- Same_Representation --
13049 -------------------------
13051 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
13052 T1 : constant Entity_Id := Underlying_Type (Typ1);
13053 T2 : constant Entity_Id := Underlying_Type (Typ2);
13055 begin
13056 -- A quick check, if base types are the same, then we definitely have
13057 -- the same representation, because the subtype specific representation
13058 -- attributes (Size and Alignment) do not affect representation from
13059 -- the point of view of this test.
13061 if Base_Type (T1) = Base_Type (T2) then
13062 return True;
13064 elsif Is_Private_Type (Base_Type (T2))
13065 and then Base_Type (T1) = Full_View (Base_Type (T2))
13066 then
13067 return True;
13068 end if;
13070 -- Tagged types never have differing representations
13072 if Is_Tagged_Type (T1) then
13073 return True;
13074 end if;
13076 -- Representations are definitely different if conventions differ
13078 if Convention (T1) /= Convention (T2) then
13079 return False;
13080 end if;
13082 -- Representations are different if component alignments or scalar
13083 -- storage orders differ.
13085 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
13086 and then
13087 (Is_Record_Type (T2) or else Is_Array_Type (T2))
13088 and then
13089 (Component_Alignment (T1) /= Component_Alignment (T2)
13090 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
13091 then
13092 return False;
13093 end if;
13095 -- For arrays, the only real issue is component size. If we know the
13096 -- component size for both arrays, and it is the same, then that's
13097 -- good enough to know we don't have a change of representation.
13099 if Is_Array_Type (T1) then
13100 if Known_Component_Size (T1)
13101 and then Known_Component_Size (T2)
13102 and then Component_Size (T1) = Component_Size (T2)
13103 then
13104 return True;
13105 end if;
13106 end if;
13108 -- Types definitely have same representation if neither has non-standard
13109 -- representation since default representations are always consistent.
13110 -- If only one has non-standard representation, and the other does not,
13111 -- then we consider that they do not have the same representation. They
13112 -- might, but there is no way of telling early enough.
13114 if Has_Non_Standard_Rep (T1) then
13115 if not Has_Non_Standard_Rep (T2) then
13116 return False;
13117 end if;
13118 else
13119 return not Has_Non_Standard_Rep (T2);
13120 end if;
13122 -- Here the two types both have non-standard representation, and we need
13123 -- to determine if they have the same non-standard representation.
13125 -- For arrays, we simply need to test if the component sizes are the
13126 -- same. Pragma Pack is reflected in modified component sizes, so this
13127 -- check also deals with pragma Pack.
13129 if Is_Array_Type (T1) then
13130 return Component_Size (T1) = Component_Size (T2);
13132 -- Tagged types always have the same representation, because it is not
13133 -- possible to specify different representations for common fields.
13135 elsif Is_Tagged_Type (T1) then
13136 return True;
13138 -- Case of record types
13140 elsif Is_Record_Type (T1) then
13142 -- Packed status must conform
13144 if Is_Packed (T1) /= Is_Packed (T2) then
13145 return False;
13147 -- Otherwise we must check components. Typ2 maybe a constrained
13148 -- subtype with fewer components, so we compare the components
13149 -- of the base types.
13151 else
13152 Record_Case : declare
13153 CD1, CD2 : Entity_Id;
13155 function Same_Rep return Boolean;
13156 -- CD1 and CD2 are either components or discriminants. This
13157 -- function tests whether they have the same representation.
13159 --------------
13160 -- Same_Rep --
13161 --------------
13163 function Same_Rep return Boolean is
13164 begin
13165 if No (Component_Clause (CD1)) then
13166 return No (Component_Clause (CD2));
13167 else
13168 -- Note: at this point, component clauses have been
13169 -- normalized to the default bit order, so that the
13170 -- comparison of Component_Bit_Offsets is meaningful.
13172 return
13173 Present (Component_Clause (CD2))
13174 and then
13175 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
13176 and then
13177 Esize (CD1) = Esize (CD2);
13178 end if;
13179 end Same_Rep;
13181 -- Start of processing for Record_Case
13183 begin
13184 if Has_Discriminants (T1) then
13186 -- The number of discriminants may be different if the
13187 -- derived type has fewer (constrained by values). The
13188 -- invisible discriminants retain the representation of
13189 -- the original, so the discrepancy does not per se
13190 -- indicate a different representation.
13192 CD1 := First_Discriminant (T1);
13193 CD2 := First_Discriminant (T2);
13194 while Present (CD1) and then Present (CD2) loop
13195 if not Same_Rep then
13196 return False;
13197 else
13198 Next_Discriminant (CD1);
13199 Next_Discriminant (CD2);
13200 end if;
13201 end loop;
13202 end if;
13204 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
13205 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
13206 while Present (CD1) loop
13207 if not Same_Rep then
13208 return False;
13209 else
13210 Next_Component (CD1);
13211 Next_Component (CD2);
13212 end if;
13213 end loop;
13215 return True;
13216 end Record_Case;
13217 end if;
13219 -- For enumeration types, we must check each literal to see if the
13220 -- representation is the same. Note that we do not permit enumeration
13221 -- representation clauses for Character and Wide_Character, so these
13222 -- cases were already dealt with.
13224 elsif Is_Enumeration_Type (T1) then
13225 Enumeration_Case : declare
13226 L1, L2 : Entity_Id;
13228 begin
13229 L1 := First_Literal (T1);
13230 L2 := First_Literal (T2);
13231 while Present (L1) loop
13232 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
13233 return False;
13234 else
13235 Next_Literal (L1);
13236 Next_Literal (L2);
13237 end if;
13238 end loop;
13240 return True;
13241 end Enumeration_Case;
13243 -- Any other types have the same representation for these purposes
13245 else
13246 return True;
13247 end if;
13248 end Same_Representation;
13250 --------------------------------
13251 -- Resolve_Iterable_Operation --
13252 --------------------------------
13254 procedure Resolve_Iterable_Operation
13255 (N : Node_Id;
13256 Cursor : Entity_Id;
13257 Typ : Entity_Id;
13258 Nam : Name_Id)
13260 Ent : Entity_Id;
13261 F1 : Entity_Id;
13262 F2 : Entity_Id;
13264 begin
13265 if not Is_Overloaded (N) then
13266 if not Is_Entity_Name (N)
13267 or else Ekind (Entity (N)) /= E_Function
13268 or else Scope (Entity (N)) /= Scope (Typ)
13269 or else No (First_Formal (Entity (N)))
13270 or else Etype (First_Formal (Entity (N))) /= Typ
13271 then
13272 Error_Msg_N ("iterable primitive must be local function name "
13273 & "whose first formal is an iterable type", N);
13274 return;
13275 end if;
13277 Ent := Entity (N);
13278 F1 := First_Formal (Ent);
13279 if Nam = Name_First then
13281 -- First (Container) => Cursor
13283 if Etype (Ent) /= Cursor then
13284 Error_Msg_N ("primitive for First must yield a curosr", N);
13285 end if;
13287 elsif Nam = Name_Next then
13289 -- Next (Container, Cursor) => Cursor
13291 F2 := Next_Formal (F1);
13293 if Etype (F2) /= Cursor
13294 or else Etype (Ent) /= Cursor
13295 or else Present (Next_Formal (F2))
13296 then
13297 Error_Msg_N ("no match for Next iterable primitive", N);
13298 end if;
13300 elsif Nam = Name_Has_Element then
13302 -- Has_Element (Container, Cursor) => Boolean
13304 F2 := Next_Formal (F1);
13305 if Etype (F2) /= Cursor
13306 or else Etype (Ent) /= Standard_Boolean
13307 or else Present (Next_Formal (F2))
13308 then
13309 Error_Msg_N ("no match for Has_Element iterable primitive", N);
13310 end if;
13312 elsif Nam = Name_Element then
13313 F2 := Next_Formal (F1);
13315 if No (F2)
13316 or else Etype (F2) /= Cursor
13317 or else Present (Next_Formal (F2))
13318 then
13319 Error_Msg_N ("no match for Element iterable primitive", N);
13320 end if;
13321 null;
13323 else
13324 raise Program_Error;
13325 end if;
13327 else
13328 -- Overloaded case: find subprogram with proper signature.
13329 -- Caller will report error if no match is found.
13331 declare
13332 I : Interp_Index;
13333 It : Interp;
13335 begin
13336 Get_First_Interp (N, I, It);
13337 while Present (It.Typ) loop
13338 if Ekind (It.Nam) = E_Function
13339 and then Scope (It.Nam) = Scope (Typ)
13340 and then Etype (First_Formal (It.Nam)) = Typ
13341 then
13342 F1 := First_Formal (It.Nam);
13344 if Nam = Name_First then
13345 if Etype (It.Nam) = Cursor
13346 and then No (Next_Formal (F1))
13347 then
13348 Set_Entity (N, It.Nam);
13349 exit;
13350 end if;
13352 elsif Nam = Name_Next then
13353 F2 := Next_Formal (F1);
13355 if Present (F2)
13356 and then No (Next_Formal (F2))
13357 and then Etype (F2) = Cursor
13358 and then Etype (It.Nam) = Cursor
13359 then
13360 Set_Entity (N, It.Nam);
13361 exit;
13362 end if;
13364 elsif Nam = Name_Has_Element then
13365 F2 := Next_Formal (F1);
13367 if Present (F2)
13368 and then No (Next_Formal (F2))
13369 and then Etype (F2) = Cursor
13370 and then Etype (It.Nam) = Standard_Boolean
13371 then
13372 Set_Entity (N, It.Nam);
13373 F2 := Next_Formal (F1);
13374 exit;
13375 end if;
13377 elsif Nam = Name_Element then
13378 F2 := Next_Formal (F1);
13380 if Present (F2)
13381 and then No (Next_Formal (F2))
13382 and then Etype (F2) = Cursor
13383 then
13384 Set_Entity (N, It.Nam);
13385 exit;
13386 end if;
13387 end if;
13388 end if;
13390 Get_Next_Interp (I, It);
13391 end loop;
13392 end;
13393 end if;
13394 end Resolve_Iterable_Operation;
13396 ----------------
13397 -- Set_Biased --
13398 ----------------
13400 procedure Set_Biased
13401 (E : Entity_Id;
13402 N : Node_Id;
13403 Msg : String;
13404 Biased : Boolean := True)
13406 begin
13407 if Biased then
13408 Set_Has_Biased_Representation (E);
13410 if Warn_On_Biased_Representation then
13411 Error_Msg_NE
13412 ("?B?" & Msg & " forces biased representation for&", N, E);
13413 end if;
13414 end if;
13415 end Set_Biased;
13417 --------------------
13418 -- Set_Enum_Esize --
13419 --------------------
13421 procedure Set_Enum_Esize (T : Entity_Id) is
13422 Lo : Uint;
13423 Hi : Uint;
13424 Sz : Nat;
13426 begin
13427 Init_Alignment (T);
13429 -- Find the minimum standard size (8,16,32,64) that fits
13431 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13432 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13434 if Lo < 0 then
13435 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
13436 Sz := Standard_Character_Size; -- May be > 8 on some targets
13438 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13439 Sz := 16;
13441 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13442 Sz := 32;
13444 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13445 Sz := 64;
13446 end if;
13448 else
13449 if Hi < Uint_2**08 then
13450 Sz := Standard_Character_Size; -- May be > 8 on some targets
13452 elsif Hi < Uint_2**16 then
13453 Sz := 16;
13455 elsif Hi < Uint_2**32 then
13456 Sz := 32;
13458 else pragma Assert (Hi < Uint_2**63);
13459 Sz := 64;
13460 end if;
13461 end if;
13463 -- That minimum is the proper size unless we have a foreign convention
13464 -- and the size required is 32 or less, in which case we bump the size
13465 -- up to 32. This is required for C and C++ and seems reasonable for
13466 -- all other foreign conventions.
13468 if Has_Foreign_Convention (T)
13469 and then Esize (T) < Standard_Integer_Size
13471 -- Don't do this if Short_Enums on target
13473 and then not Target_Short_Enums
13474 then
13475 Init_Esize (T, Standard_Integer_Size);
13476 else
13477 Init_Esize (T, Sz);
13478 end if;
13479 end Set_Enum_Esize;
13481 -----------------------------
13482 -- Uninstall_Discriminants --
13483 -----------------------------
13485 procedure Uninstall_Discriminants (E : Entity_Id) is
13486 Disc : Entity_Id;
13487 Prev : Entity_Id;
13488 Outer : Entity_Id;
13490 begin
13491 -- Discriminants have been made visible for type declarations and
13492 -- protected type declarations, not for subtype declarations.
13494 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13495 Disc := First_Discriminant (E);
13496 while Present (Disc) loop
13497 if Disc /= Current_Entity (Disc) then
13498 Prev := Current_Entity (Disc);
13499 while Present (Prev)
13500 and then Present (Homonym (Prev))
13501 and then Homonym (Prev) /= Disc
13502 loop
13503 Prev := Homonym (Prev);
13504 end loop;
13505 else
13506 Prev := Empty;
13507 end if;
13509 Set_Is_Immediately_Visible (Disc, False);
13511 Outer := Homonym (Disc);
13512 while Present (Outer) and then Scope (Outer) = E loop
13513 Outer := Homonym (Outer);
13514 end loop;
13516 -- Reset homonym link of other entities, but do not modify link
13517 -- between entities in current scope, so that the back end can
13518 -- have a proper count of local overloadings.
13520 if No (Prev) then
13521 Set_Name_Entity_Id (Chars (Disc), Outer);
13523 elsif Scope (Prev) /= Scope (Disc) then
13524 Set_Homonym (Prev, Outer);
13525 end if;
13527 Next_Discriminant (Disc);
13528 end loop;
13529 end if;
13530 end Uninstall_Discriminants;
13532 -------------------------------------------
13533 -- Uninstall_Discriminants_And_Pop_Scope --
13534 -------------------------------------------
13536 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13537 begin
13538 if Has_Discriminants (E) then
13539 Uninstall_Discriminants (E);
13540 Pop_Scope;
13541 end if;
13542 end Uninstall_Discriminants_And_Pop_Scope;
13544 ------------------------------
13545 -- Validate_Address_Clauses --
13546 ------------------------------
13548 procedure Validate_Address_Clauses is
13549 begin
13550 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13551 declare
13552 ACCR : Address_Clause_Check_Record
13553 renames Address_Clause_Checks.Table (J);
13555 Expr : Node_Id;
13557 X_Alignment : Uint;
13558 Y_Alignment : Uint;
13560 X_Size : Uint;
13561 Y_Size : Uint;
13563 begin
13564 -- Skip processing of this entry if warning already posted
13566 if not Address_Warning_Posted (ACCR.N) then
13567 Expr := Original_Node (Expression (ACCR.N));
13569 -- Get alignments
13571 X_Alignment := Alignment (ACCR.X);
13572 Y_Alignment := Alignment (ACCR.Y);
13574 -- Similarly obtain sizes
13576 X_Size := Esize (ACCR.X);
13577 Y_Size := Esize (ACCR.Y);
13579 -- Check for large object overlaying smaller one
13581 if Y_Size > Uint_0
13582 and then X_Size > Uint_0
13583 and then X_Size > Y_Size
13584 then
13585 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
13586 Error_Msg_N
13587 ("\??program execution may be erroneous", ACCR.N);
13589 Error_Msg_Uint_1 := X_Size;
13590 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13592 Error_Msg_Uint_1 := Y_Size;
13593 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
13595 -- Check for inadequate alignment, both of the base object
13596 -- and of the offset, if any. We only do this check if the
13597 -- run-time Alignment_Check is active. No point in warning
13598 -- if this check has been suppressed (or is suppressed by
13599 -- default in the non-strict alignment machine case).
13601 -- Note: we do not check the alignment if we gave a size
13602 -- warning, since it would likely be redundant.
13604 elsif not Alignment_Checks_Suppressed (ACCR.Y)
13605 and then Y_Alignment /= Uint_0
13606 and then
13607 (Y_Alignment < X_Alignment
13608 or else
13609 (ACCR.Off
13610 and then Nkind (Expr) = N_Attribute_Reference
13611 and then Attribute_Name (Expr) = Name_Address
13612 and then Has_Compatible_Alignment
13613 (ACCR.X, Prefix (Expr), True) /=
13614 Known_Compatible))
13615 then
13616 Error_Msg_NE
13617 ("??specified address for& may be inconsistent with "
13618 & "alignment", ACCR.N, ACCR.X);
13619 Error_Msg_N
13620 ("\??program execution may be erroneous (RM 13.3(27))",
13621 ACCR.N);
13623 Error_Msg_Uint_1 := X_Alignment;
13624 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13626 Error_Msg_Uint_1 := Y_Alignment;
13627 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13629 if Y_Alignment >= X_Alignment then
13630 Error_Msg_N
13631 ("\??but offset is not multiple of alignment", ACCR.N);
13632 end if;
13633 end if;
13634 end if;
13635 end;
13636 end loop;
13637 end Validate_Address_Clauses;
13639 ---------------------------
13640 -- Validate_Independence --
13641 ---------------------------
13643 procedure Validate_Independence is
13644 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13645 N : Node_Id;
13646 E : Entity_Id;
13647 IC : Boolean;
13648 Comp : Entity_Id;
13649 Addr : Node_Id;
13650 P : Node_Id;
13652 procedure Check_Array_Type (Atyp : Entity_Id);
13653 -- Checks if the array type Atyp has independent components, and
13654 -- if not, outputs an appropriate set of error messages.
13656 procedure No_Independence;
13657 -- Output message that independence cannot be guaranteed
13659 function OK_Component (C : Entity_Id) return Boolean;
13660 -- Checks one component to see if it is independently accessible, and
13661 -- if so yields True, otherwise yields False if independent access
13662 -- cannot be guaranteed. This is a conservative routine, it only
13663 -- returns True if it knows for sure, it returns False if it knows
13664 -- there is a problem, or it cannot be sure there is no problem.
13666 procedure Reason_Bad_Component (C : Entity_Id);
13667 -- Outputs continuation message if a reason can be determined for
13668 -- the component C being bad.
13670 ----------------------
13671 -- Check_Array_Type --
13672 ----------------------
13674 procedure Check_Array_Type (Atyp : Entity_Id) is
13675 Ctyp : constant Entity_Id := Component_Type (Atyp);
13677 begin
13678 -- OK if no alignment clause, no pack, and no component size
13680 if not Has_Component_Size_Clause (Atyp)
13681 and then not Has_Alignment_Clause (Atyp)
13682 and then not Is_Packed (Atyp)
13683 then
13684 return;
13685 end if;
13687 -- Case of component size is greater than or equal to 64 and the
13688 -- alignment of the array is at least as large as the alignment
13689 -- of the component. We are definitely OK in this situation.
13691 if Known_Component_Size (Atyp)
13692 and then Component_Size (Atyp) >= 64
13693 and then Known_Alignment (Atyp)
13694 and then Known_Alignment (Ctyp)
13695 and then Alignment (Atyp) >= Alignment (Ctyp)
13696 then
13697 return;
13698 end if;
13700 -- Check actual component size
13702 if not Known_Component_Size (Atyp)
13703 or else not (Addressable (Component_Size (Atyp))
13704 and then Component_Size (Atyp) < 64)
13705 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13706 then
13707 No_Independence;
13709 -- Bad component size, check reason
13711 if Has_Component_Size_Clause (Atyp) then
13712 P := Get_Attribute_Definition_Clause
13713 (Atyp, Attribute_Component_Size);
13715 if Present (P) then
13716 Error_Msg_Sloc := Sloc (P);
13717 Error_Msg_N ("\because of Component_Size clause#", N);
13718 return;
13719 end if;
13720 end if;
13722 if Is_Packed (Atyp) then
13723 P := Get_Rep_Pragma (Atyp, Name_Pack);
13725 if Present (P) then
13726 Error_Msg_Sloc := Sloc (P);
13727 Error_Msg_N ("\because of pragma Pack#", N);
13728 return;
13729 end if;
13730 end if;
13732 -- No reason found, just return
13734 return;
13735 end if;
13737 -- Array type is OK independence-wise
13739 return;
13740 end Check_Array_Type;
13742 ---------------------
13743 -- No_Independence --
13744 ---------------------
13746 procedure No_Independence is
13747 begin
13748 if Pragma_Name (N) = Name_Independent then
13749 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
13750 else
13751 Error_Msg_NE
13752 ("independent components cannot be guaranteed for&", N, E);
13753 end if;
13754 end No_Independence;
13756 ------------------
13757 -- OK_Component --
13758 ------------------
13760 function OK_Component (C : Entity_Id) return Boolean is
13761 Rec : constant Entity_Id := Scope (C);
13762 Ctyp : constant Entity_Id := Etype (C);
13764 begin
13765 -- OK if no component clause, no Pack, and no alignment clause
13767 if No (Component_Clause (C))
13768 and then not Is_Packed (Rec)
13769 and then not Has_Alignment_Clause (Rec)
13770 then
13771 return True;
13772 end if;
13774 -- Here we look at the actual component layout. A component is
13775 -- addressable if its size is a multiple of the Esize of the
13776 -- component type, and its starting position in the record has
13777 -- appropriate alignment, and the record itself has appropriate
13778 -- alignment to guarantee the component alignment.
13780 -- Make sure sizes are static, always assume the worst for any
13781 -- cases where we cannot check static values.
13783 if not (Known_Static_Esize (C)
13784 and then
13785 Known_Static_Esize (Ctyp))
13786 then
13787 return False;
13788 end if;
13790 -- Size of component must be addressable or greater than 64 bits
13791 -- and a multiple of bytes.
13793 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
13794 return False;
13795 end if;
13797 -- Check size is proper multiple
13799 if Esize (C) mod Esize (Ctyp) /= 0 then
13800 return False;
13801 end if;
13803 -- Check alignment of component is OK
13805 if not Known_Component_Bit_Offset (C)
13806 or else Component_Bit_Offset (C) < Uint_0
13807 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13808 then
13809 return False;
13810 end if;
13812 -- Check alignment of record type is OK
13814 if not Known_Alignment (Rec)
13815 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13816 then
13817 return False;
13818 end if;
13820 -- All tests passed, component is addressable
13822 return True;
13823 end OK_Component;
13825 --------------------------
13826 -- Reason_Bad_Component --
13827 --------------------------
13829 procedure Reason_Bad_Component (C : Entity_Id) is
13830 Rec : constant Entity_Id := Scope (C);
13831 Ctyp : constant Entity_Id := Etype (C);
13833 begin
13834 -- If component clause present assume that's the problem
13836 if Present (Component_Clause (C)) then
13837 Error_Msg_Sloc := Sloc (Component_Clause (C));
13838 Error_Msg_N ("\because of Component_Clause#", N);
13839 return;
13840 end if;
13842 -- If pragma Pack clause present, assume that's the problem
13844 if Is_Packed (Rec) then
13845 P := Get_Rep_Pragma (Rec, Name_Pack);
13847 if Present (P) then
13848 Error_Msg_Sloc := Sloc (P);
13849 Error_Msg_N ("\because of pragma Pack#", N);
13850 return;
13851 end if;
13852 end if;
13854 -- See if record has bad alignment clause
13856 if Has_Alignment_Clause (Rec)
13857 and then Known_Alignment (Rec)
13858 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13859 then
13860 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13862 if Present (P) then
13863 Error_Msg_Sloc := Sloc (P);
13864 Error_Msg_N ("\because of Alignment clause#", N);
13865 end if;
13866 end if;
13868 -- Couldn't find a reason, so return without a message
13870 return;
13871 end Reason_Bad_Component;
13873 -- Start of processing for Validate_Independence
13875 begin
13876 for J in Independence_Checks.First .. Independence_Checks.Last loop
13877 N := Independence_Checks.Table (J).N;
13878 E := Independence_Checks.Table (J).E;
13879 IC := Pragma_Name (N) = Name_Independent_Components;
13881 -- Deal with component case
13883 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13884 if not OK_Component (E) then
13885 No_Independence;
13886 Reason_Bad_Component (E);
13887 goto Continue;
13888 end if;
13889 end if;
13891 -- Deal with record with Independent_Components
13893 if IC and then Is_Record_Type (E) then
13894 Comp := First_Component_Or_Discriminant (E);
13895 while Present (Comp) loop
13896 if not OK_Component (Comp) then
13897 No_Independence;
13898 Reason_Bad_Component (Comp);
13899 goto Continue;
13900 end if;
13902 Next_Component_Or_Discriminant (Comp);
13903 end loop;
13904 end if;
13906 -- Deal with address clause case
13908 if Is_Object (E) then
13909 Addr := Address_Clause (E);
13911 if Present (Addr) then
13912 No_Independence;
13913 Error_Msg_Sloc := Sloc (Addr);
13914 Error_Msg_N ("\because of Address clause#", N);
13915 goto Continue;
13916 end if;
13917 end if;
13919 -- Deal with independent components for array type
13921 if IC and then Is_Array_Type (E) then
13922 Check_Array_Type (E);
13923 end if;
13925 -- Deal with independent components for array object
13927 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13928 Check_Array_Type (Etype (E));
13929 end if;
13931 <<Continue>> null;
13932 end loop;
13933 end Validate_Independence;
13935 ------------------------------
13936 -- Validate_Iterable_Aspect --
13937 ------------------------------
13939 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
13940 Assoc : Node_Id;
13941 Expr : Node_Id;
13943 Prim : Node_Id;
13944 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
13946 First_Id : Entity_Id;
13947 Next_Id : Entity_Id;
13948 Has_Element_Id : Entity_Id;
13949 Element_Id : Entity_Id;
13951 begin
13952 -- If previous error aspect is unusable
13954 if Cursor = Any_Type then
13955 return;
13956 end if;
13958 First_Id := Empty;
13959 Next_Id := Empty;
13960 Has_Element_Id := Empty;
13961 Element_Id := Empty;
13963 -- Each expression must resolve to a function with the proper signature
13965 Assoc := First (Component_Associations (Expression (ASN)));
13966 while Present (Assoc) loop
13967 Expr := Expression (Assoc);
13968 Analyze (Expr);
13970 Prim := First (Choices (Assoc));
13972 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
13973 Error_Msg_N ("illegal name in association", Prim);
13975 elsif Chars (Prim) = Name_First then
13976 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
13977 First_Id := Entity (Expr);
13979 elsif Chars (Prim) = Name_Next then
13980 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
13981 Next_Id := Entity (Expr);
13983 elsif Chars (Prim) = Name_Has_Element then
13984 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
13985 Has_Element_Id := Entity (Expr);
13987 elsif Chars (Prim) = Name_Element then
13988 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
13989 Element_Id := Entity (Expr);
13991 else
13992 Error_Msg_N ("invalid name for iterable function", Prim);
13993 end if;
13995 Next (Assoc);
13996 end loop;
13998 if No (First_Id) then
13999 Error_Msg_N ("match for First primitive not found", ASN);
14001 elsif No (Next_Id) then
14002 Error_Msg_N ("match for Next primitive not found", ASN);
14004 elsif No (Has_Element_Id) then
14005 Error_Msg_N ("match for Has_Element primitive not found", ASN);
14007 elsif No (Element_Id) then
14008 null; -- Optional.
14009 end if;
14010 end Validate_Iterable_Aspect;
14012 -----------------------------------
14013 -- Validate_Unchecked_Conversion --
14014 -----------------------------------
14016 procedure Validate_Unchecked_Conversion
14017 (N : Node_Id;
14018 Act_Unit : Entity_Id)
14020 Source : Entity_Id;
14021 Target : Entity_Id;
14022 Vnode : Node_Id;
14024 begin
14025 -- Obtain source and target types. Note that we call Ancestor_Subtype
14026 -- here because the processing for generic instantiation always makes
14027 -- subtypes, and we want the original frozen actual types.
14029 -- If we are dealing with private types, then do the check on their
14030 -- fully declared counterparts if the full declarations have been
14031 -- encountered (they don't have to be visible, but they must exist).
14033 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
14035 if Is_Private_Type (Source)
14036 and then Present (Underlying_Type (Source))
14037 then
14038 Source := Underlying_Type (Source);
14039 end if;
14041 Target := Ancestor_Subtype (Etype (Act_Unit));
14043 -- If either type is generic, the instantiation happens within a generic
14044 -- unit, and there is nothing to check. The proper check will happen
14045 -- when the enclosing generic is instantiated.
14047 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
14048 return;
14049 end if;
14051 if Is_Private_Type (Target)
14052 and then Present (Underlying_Type (Target))
14053 then
14054 Target := Underlying_Type (Target);
14055 end if;
14057 -- Source may be unconstrained array, but not target, except in relaxed
14058 -- semantics mode.
14060 if Is_Array_Type (Target)
14061 and then not Is_Constrained (Target)
14062 and then not Relaxed_RM_Semantics
14063 then
14064 Error_Msg_N
14065 ("unchecked conversion to unconstrained array not allowed", N);
14066 return;
14067 end if;
14069 -- Warn if conversion between two different convention pointers
14071 if Is_Access_Type (Target)
14072 and then Is_Access_Type (Source)
14073 and then Convention (Target) /= Convention (Source)
14074 and then Warn_On_Unchecked_Conversion
14075 then
14076 -- Give warnings for subprogram pointers only on most targets
14078 if Is_Access_Subprogram_Type (Target)
14079 or else Is_Access_Subprogram_Type (Source)
14080 then
14081 Error_Msg_N
14082 ("?z?conversion between pointers with different conventions!",
14084 end if;
14085 end if;
14087 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
14088 -- warning when compiling GNAT-related sources.
14090 if Warn_On_Unchecked_Conversion
14091 and then not In_Predefined_Unit (N)
14092 and then RTU_Loaded (Ada_Calendar)
14093 and then (Chars (Source) = Name_Time
14094 or else
14095 Chars (Target) = Name_Time)
14096 then
14097 -- If Ada.Calendar is loaded and the name of one of the operands is
14098 -- Time, there is a good chance that this is Ada.Calendar.Time.
14100 declare
14101 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
14102 begin
14103 pragma Assert (Present (Calendar_Time));
14105 if Source = Calendar_Time or else Target = Calendar_Time then
14106 Error_Msg_N
14107 ("?z?representation of 'Time values may change between "
14108 & "'G'N'A'T versions", N);
14109 end if;
14110 end;
14111 end if;
14113 -- Make entry in unchecked conversion table for later processing by
14114 -- Validate_Unchecked_Conversions, which will check sizes and alignments
14115 -- (using values set by the back end where possible). This is only done
14116 -- if the appropriate warning is active.
14118 if Warn_On_Unchecked_Conversion then
14119 Unchecked_Conversions.Append
14120 (New_Val => UC_Entry'(Eloc => Sloc (N),
14121 Source => Source,
14122 Target => Target,
14123 Act_Unit => Act_Unit));
14125 -- If both sizes are known statically now, then back end annotation
14126 -- is not required to do a proper check but if either size is not
14127 -- known statically, then we need the annotation.
14129 if Known_Static_RM_Size (Source)
14130 and then
14131 Known_Static_RM_Size (Target)
14132 then
14133 null;
14134 else
14135 Back_Annotate_Rep_Info := True;
14136 end if;
14137 end if;
14139 -- If unchecked conversion to access type, and access type is declared
14140 -- in the same unit as the unchecked conversion, then set the flag
14141 -- No_Strict_Aliasing (no strict aliasing is implicit here)
14143 if Is_Access_Type (Target) and then
14144 In_Same_Source_Unit (Target, N)
14145 then
14146 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
14147 end if;
14149 -- Generate N_Validate_Unchecked_Conversion node for back end in case
14150 -- the back end needs to perform special validation checks.
14152 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
14153 -- have full expansion and the back end is called ???
14155 Vnode :=
14156 Make_Validate_Unchecked_Conversion (Sloc (N));
14157 Set_Source_Type (Vnode, Source);
14158 Set_Target_Type (Vnode, Target);
14160 -- If the unchecked conversion node is in a list, just insert before it.
14161 -- If not we have some strange case, not worth bothering about.
14163 if Is_List_Member (N) then
14164 Insert_After (N, Vnode);
14165 end if;
14166 end Validate_Unchecked_Conversion;
14168 ------------------------------------
14169 -- Validate_Unchecked_Conversions --
14170 ------------------------------------
14172 procedure Validate_Unchecked_Conversions is
14173 begin
14174 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
14175 declare
14176 T : UC_Entry renames Unchecked_Conversions.Table (N);
14178 Eloc : constant Source_Ptr := T.Eloc;
14179 Source : constant Entity_Id := T.Source;
14180 Target : constant Entity_Id := T.Target;
14181 Act_Unit : constant Entity_Id := T.Act_Unit;
14183 Source_Siz : Uint;
14184 Target_Siz : Uint;
14186 begin
14187 -- Skip if function marked as warnings off
14189 if Warnings_Off (Act_Unit) then
14190 goto Continue;
14191 end if;
14193 -- This validation check, which warns if we have unequal sizes for
14194 -- unchecked conversion, and thus potentially implementation
14195 -- dependent semantics, is one of the few occasions on which we
14196 -- use the official RM size instead of Esize. See description in
14197 -- Einfo "Handling of Type'Size Values" for details.
14199 if Serious_Errors_Detected = 0
14200 and then Known_Static_RM_Size (Source)
14201 and then Known_Static_RM_Size (Target)
14203 -- Don't do the check if warnings off for either type, note the
14204 -- deliberate use of OR here instead of OR ELSE to get the flag
14205 -- Warnings_Off_Used set for both types if appropriate.
14207 and then not (Has_Warnings_Off (Source)
14209 Has_Warnings_Off (Target))
14210 then
14211 Source_Siz := RM_Size (Source);
14212 Target_Siz := RM_Size (Target);
14214 if Source_Siz /= Target_Siz then
14215 Error_Msg
14216 ("?z?types for unchecked conversion have different sizes!",
14217 Eloc);
14219 if All_Errors_Mode then
14220 Error_Msg_Name_1 := Chars (Source);
14221 Error_Msg_Uint_1 := Source_Siz;
14222 Error_Msg_Name_2 := Chars (Target);
14223 Error_Msg_Uint_2 := Target_Siz;
14224 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
14226 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
14228 if Is_Discrete_Type (Source)
14229 and then
14230 Is_Discrete_Type (Target)
14231 then
14232 if Source_Siz > Target_Siz then
14233 Error_Msg
14234 ("\?z?^ high order bits of source will "
14235 & "be ignored!", Eloc);
14237 elsif Is_Unsigned_Type (Source) then
14238 Error_Msg
14239 ("\?z?source will be extended with ^ high order "
14240 & "zero bits!", Eloc);
14242 else
14243 Error_Msg
14244 ("\?z?source will be extended with ^ high order "
14245 & "sign bits!", Eloc);
14246 end if;
14248 elsif Source_Siz < Target_Siz then
14249 if Is_Discrete_Type (Target) then
14250 if Bytes_Big_Endian then
14251 Error_Msg
14252 ("\?z?target value will include ^ undefined "
14253 & "low order bits!", Eloc);
14254 else
14255 Error_Msg
14256 ("\?z?target value will include ^ undefined "
14257 & "high order bits!", Eloc);
14258 end if;
14260 else
14261 Error_Msg
14262 ("\?z?^ trailing bits of target value will be "
14263 & "undefined!", Eloc);
14264 end if;
14266 else pragma Assert (Source_Siz > Target_Siz);
14267 if Is_Discrete_Type (Source) then
14268 if Bytes_Big_Endian then
14269 Error_Msg
14270 ("\?z?^ low order bits of source will be "
14271 & "ignored!", Eloc);
14272 else
14273 Error_Msg
14274 ("\?z?^ high order bits of source will be "
14275 & "ignored!", Eloc);
14276 end if;
14278 else
14279 Error_Msg
14280 ("\?z?^ trailing bits of source will be "
14281 & "ignored!", Eloc);
14282 end if;
14283 end if;
14284 end if;
14285 end if;
14286 end if;
14288 -- If both types are access types, we need to check the alignment.
14289 -- If the alignment of both is specified, we can do it here.
14291 if Serious_Errors_Detected = 0
14292 and then Is_Access_Type (Source)
14293 and then Is_Access_Type (Target)
14294 and then Target_Strict_Alignment
14295 and then Present (Designated_Type (Source))
14296 and then Present (Designated_Type (Target))
14297 then
14298 declare
14299 D_Source : constant Entity_Id := Designated_Type (Source);
14300 D_Target : constant Entity_Id := Designated_Type (Target);
14302 begin
14303 if Known_Alignment (D_Source)
14304 and then
14305 Known_Alignment (D_Target)
14306 then
14307 declare
14308 Source_Align : constant Uint := Alignment (D_Source);
14309 Target_Align : constant Uint := Alignment (D_Target);
14311 begin
14312 if Source_Align < Target_Align
14313 and then not Is_Tagged_Type (D_Source)
14315 -- Suppress warning if warnings suppressed on either
14316 -- type or either designated type. Note the use of
14317 -- OR here instead of OR ELSE. That is intentional,
14318 -- we would like to set flag Warnings_Off_Used in
14319 -- all types for which warnings are suppressed.
14321 and then not (Has_Warnings_Off (D_Source)
14323 Has_Warnings_Off (D_Target)
14325 Has_Warnings_Off (Source)
14327 Has_Warnings_Off (Target))
14328 then
14329 Error_Msg_Uint_1 := Target_Align;
14330 Error_Msg_Uint_2 := Source_Align;
14331 Error_Msg_Node_1 := D_Target;
14332 Error_Msg_Node_2 := D_Source;
14333 Error_Msg
14334 ("?z?alignment of & (^) is stricter than "
14335 & "alignment of & (^)!", Eloc);
14336 Error_Msg
14337 ("\?z?resulting access value may have invalid "
14338 & "alignment!", Eloc);
14339 end if;
14340 end;
14341 end if;
14342 end;
14343 end if;
14344 end;
14346 <<Continue>>
14347 null;
14348 end loop;
14349 end Validate_Unchecked_Conversions;
14351 end Sem_Ch13;