]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch13.adb
ada: Move warnings switches
[gcc.git] / gcc / ada / sem_ch13.adb
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-2022, 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 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Freeze; use Freeze;
41 with Ghost; use Ghost;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Par_SCO; use Par_SCO;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Case; use Sem_Case;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Dim; use Sem_Dim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Sinfo; use Sinfo;
68 with Sinfo.Nodes; use Sinfo.Nodes;
69 with Sinfo.Utils; use Sinfo.Utils;
70 with Sinput; use Sinput;
71 with Snames; use Snames;
72 with Stand; use Stand;
73 with System.Case_Util; use System.Case_Util;
74 with Table;
75 with Targparm; use Targparm;
76 with Ttypes; use Ttypes;
77 with Tbuild; use Tbuild;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
80
81 with GNAT.Heap_Sort_G;
82
83 package body Sem_Ch13 is
84
85 SSU : constant Pos := System_Storage_Unit;
86 -- Convenient short hand for commonly used constant
87
88 -----------------------
89 -- Local Subprograms --
90 -----------------------
91
92 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id);
93 -- Helper routine providing the original (pre-AI95-0133) behavior for
94 -- Adjust_Record_For_Reverse_Bit_Order.
95
96 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
97 -- This routine is called after setting one of the sizes of type entity
98 -- Typ to Size. The purpose is to deal with the situation of a derived
99 -- type whose inherited alignment is no longer appropriate for the new
100 -- size value. In this case, we reset the Alignment to unknown.
101
102 function All_Static_Choices (L : List_Id) return Boolean;
103 -- Returns true if all elements of the list are OK static choices
104 -- as defined below for Is_Static_Choice. Used for case expression
105 -- alternatives and for the right operand of a membership test. An
106 -- others_choice is static if the corresponding expression is static.
107 -- The staticness of the bounds is checked separately.
108
109 procedure Build_Discrete_Static_Predicate
110 (Typ : Entity_Id;
111 Expr : Node_Id;
112 Nam : Name_Id);
113 -- Given a predicated type Typ, where Typ is a discrete static subtype,
114 -- whose predicate expression is Expr, tests if Expr is a static predicate,
115 -- and if so, builds the predicate range list. Nam is the name of the one
116 -- argument to the predicate function. Occurrences of the type name in the
117 -- predicate expression have been replaced by identifier references to this
118 -- name, which is unique, so any identifier with Chars matching Nam must be
119 -- a reference to the type. If the predicate is non-static, this procedure
120 -- returns doing nothing. If the predicate is static, then the predicate
121 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
122 -- rewritten as a canonicalized membership operation.
123
124 function Build_Export_Import_Pragma
125 (Asp : Node_Id;
126 Id : Entity_Id) return Node_Id;
127 -- Create the corresponding pragma for aspect Export or Import denoted by
128 -- Asp. Id is the related entity subject to the aspect. Return Empty when
129 -- the expression of aspect Asp evaluates to False or is erroneous.
130
131 function Build_Predicate_Function_Declaration
132 (Typ : Entity_Id) return Node_Id;
133 -- Build the declaration for a predicate function. The declaration is built
134 -- at the end of the declarative part containing the type definition, which
135 -- may be before the freeze point of the type. The predicate expression is
136 -- preanalyzed at this point, to catch visibility errors.
137
138 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id);
139 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
140 -- then either there are pragma Predicate entries on the rep chain for the
141 -- type (note that Predicate aspects are converted to pragma Predicate), or
142 -- there are inherited aspects from a parent type, or ancestor subtypes.
143 -- This procedure builds body for the Predicate function that tests these
144 -- predicates. N is the freeze node for the type. The spec of the function
145 -- is inserted before the freeze node, and the body of the function is
146 -- inserted after the freeze node.
147
148 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
149 -- Called if both Storage_Pool and Storage_Size attribute definition
150 -- clauses (SP and SS) are present for entity Ent. Issue error message.
151
152 procedure Freeze_Entity_Checks (N : Node_Id);
153 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
154 -- to generate appropriate semantic checks that are delayed until this
155 -- point (they had to be delayed this long for cases of delayed aspects,
156 -- e.g. analysis of statically predicated subtypes in choices, for which
157 -- we have to be sure the subtypes in question are frozen before checking).
158
159 function Get_Alignment_Value (Expr : Node_Id) return Uint;
160 -- Given the expression for an alignment value, returns the corresponding
161 -- Uint value. If the value is inappropriate, then error messages are
162 -- posted as required, and a value of No_Uint is returned.
163
164 function Is_Operational_Item (N : Node_Id) return Boolean;
165 -- A specification for a stream attribute is allowed before the full type
166 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
167 -- that do not specify a representation characteristic are operational
168 -- attributes.
169
170 function Is_Static_Choice (N : Node_Id) return Boolean;
171 -- Returns True if N represents a static choice (static subtype, or
172 -- static subtype indication, or static expression, or static range).
173 --
174 -- Note that this is a bit more inclusive than we actually need
175 -- (in particular membership tests do not allow the use of subtype
176 -- indications). But that doesn't matter, we have already checked
177 -- that the construct is legal to get this far.
178
179 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean;
180 -- Returns True for a representation clause/pragma that specifies a
181 -- type-related representation (as opposed to operational) aspect.
182
183 function Is_Predicate_Static
184 (Expr : Node_Id;
185 Nam : Name_Id;
186 Warn : Boolean := True) return Boolean;
187 -- Given predicate expression Expr, tests if Expr is predicate-static in
188 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
189 -- name in the predicate expression have been replaced by references to
190 -- an identifier whose Chars field is Nam. This name is unique, so any
191 -- identifier with Chars matching Nam must be a reference to the type.
192 -- Returns True if the expression is predicate-static and False otherwise,
193 -- but is not in the business of setting flags or issuing error messages.
194 --
195 -- Only scalar types can have static predicates, so False is always
196 -- returned for non-scalar types.
197 --
198 -- Note: the RM seems to suggest that string types can also have static
199 -- predicates. But that really makes little sense as very few useful
200 -- predicates can be constructed for strings. Remember that:
201 --
202 -- "ABC" < "DEF"
203 --
204 -- is not a static expression. So even though the clearly faulty RM wording
205 -- allows the following:
206 --
207 -- subtype S is String with Static_Predicate => S < "DEF"
208 --
209 -- We can't allow this, otherwise we have predicate-static applying to a
210 -- larger class than static expressions, which was never intended.
211 --
212 -- The Warn parameter is True iff this is not a recursive call. This
213 -- parameter is used to avoid generating warnings for subexpressions and
214 -- for cases where the predicate expression (as originally written by
215 -- the user, before any transformations) is a Boolean literal.
216
217 procedure New_Put_Image_Subprogram
218 (N : Node_Id;
219 Ent : Entity_Id;
220 Subp : Entity_Id);
221 -- Similar to New_Stream_Subprogram, but for the Put_Image attribute
222
223 procedure New_Stream_Subprogram
224 (N : Node_Id;
225 Ent : Entity_Id;
226 Subp : Entity_Id;
227 Nam : TSS_Name_Type);
228 -- Create a subprogram renaming of a given stream attribute to the
229 -- designated subprogram and then in the tagged case, provide this as a
230 -- primitive operation, or in the untagged case make an appropriate TSS
231 -- entry. This is more properly an expansion activity than just semantics,
232 -- but the presence of user-defined stream functions for limited types
233 -- is a legality check, which is why this takes place here rather than in
234 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
235 -- function to be generated.
236 --
237 -- To avoid elaboration anomalies with freeze nodes, for untagged types
238 -- we generate both a subprogram declaration and a subprogram renaming
239 -- declaration, so that the attribute specification is handled as a
240 -- renaming_as_body. For tagged types, the specification is one of the
241 -- primitive specs.
242
243 procedure No_Type_Rep_Item (N : Node_Id);
244 -- Output message indicating that no type-related aspects can be
245 -- specified due to some property of the parent type.
246
247 procedure Register_Address_Clause_Check
248 (N : Node_Id;
249 X : Entity_Id;
250 A : Uint;
251 Y : Entity_Id;
252 Off : Boolean);
253 -- Register a check for the address clause N. The rest of the parameters
254 -- are in keeping with the components of Address_Clause_Check_Record below.
255
256 procedure Validate_Aspect_Aggregate (N : Node_Id);
257 -- Check legality of operations given in the Ada 2022 Aggregate aspect for
258 -- containers.
259
260 procedure Resolve_Aspect_Aggregate
261 (Typ : Entity_Id;
262 Expr : Node_Id);
263 -- Resolve each one of the operations specified in the specification of
264 -- Aspect_Aggregate.
265
266 procedure Validate_Aspect_Stable_Properties
267 (E : Entity_Id; N : Node_Id; Class_Present : Boolean);
268 -- Check legality of functions given in the Ada 2022 Stable_Properties
269 -- (or Stable_Properties'Class) aspect.
270
271 procedure Validate_Storage_Model_Type_Aspect
272 (Typ : Entity_Id; ASN : Node_Id);
273 -- Check legality and completeness of the aggregate associations given in
274 -- the Storage_Model_Type aspect associated with Typ.
275
276 procedure Resolve_Storage_Model_Type_Argument
277 (N : Node_Id;
278 Typ : Entity_Id;
279 Addr_Type : in out Entity_Id;
280 Nam : Name_Id);
281 -- Resolve argument N to be of the proper kind (when a type or constant)
282 -- or to have the proper profile (when a subprogram).
283
284 procedure Resolve_Aspect_Stable_Properties
285 (Typ_Or_Subp : Entity_Id;
286 Expr : Node_Id;
287 Class_Present : Boolean);
288 -- Resolve each one of the functions specified in the specification of
289 -- aspect Stable_Properties (or Stable_Properties'Class).
290
291 procedure Resolve_Iterable_Operation
292 (N : Node_Id;
293 Cursor : Entity_Id;
294 Typ : Entity_Id;
295 Nam : Name_Id);
296 -- If the name of a primitive operation for an Iterable aspect is
297 -- overloaded, resolve according to required signature.
298
299 procedure Set_Biased
300 (E : Entity_Id;
301 N : Node_Id;
302 Msg : String;
303 Biased : Boolean := True);
304 -- If Biased is True, sets Has_Biased_Representation flag for E, and
305 -- outputs a warning message at node N if Warn_On_Biased_Representation is
306 -- is True. This warning inserts the string Msg to describe the construct
307 -- causing biasing.
308
309 -----------------------------------------------------------
310 -- Visibility of Discriminants in Aspect Specifications --
311 -----------------------------------------------------------
312
313 -- The discriminants of a type are visible when analyzing the aspect
314 -- specifications of a type declaration or protected type declaration,
315 -- but not when analyzing those of a subtype declaration. The following
316 -- routines enforce this distinction.
317
318 procedure Push_Type (E : Entity_Id);
319 -- Push scope E and make visible the discriminants of type entity E if E
320 -- has discriminants and is not a subtype.
321
322 procedure Pop_Type (E : Entity_Id);
323 -- Remove visibility to the discriminants of type entity E and pop the
324 -- scope stack if E has discriminants and is not a subtype.
325
326 ----------------------------------------------
327 -- Table for Validate_Unchecked_Conversions --
328 ----------------------------------------------
329
330 -- The following table collects unchecked conversions for validation.
331 -- Entries are made by Validate_Unchecked_Conversion and then the call
332 -- to Validate_Unchecked_Conversions does the actual error checking and
333 -- posting of warnings. The reason for this delayed processing is to take
334 -- advantage of back-annotations of size and alignment values performed by
335 -- the back end.
336
337 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
338 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
339 -- already have modified all Sloc values if the -gnatD option is set.
340
341 type UC_Entry is record
342 Eloc : Source_Ptr; -- node used for posting warnings
343 Source : Entity_Id; -- source type for unchecked conversion
344 Target : Entity_Id; -- target type for unchecked conversion
345 Act_Unit : Entity_Id; -- actual function instantiated
346 end record;
347
348 package Unchecked_Conversions is new Table.Table (
349 Table_Component_Type => UC_Entry,
350 Table_Index_Type => Int,
351 Table_Low_Bound => 1,
352 Table_Initial => 50,
353 Table_Increment => 200,
354 Table_Name => "Unchecked_Conversions");
355
356 ----------------------------------------
357 -- Table for Validate_Address_Clauses --
358 ----------------------------------------
359
360 -- If an address clause has the form
361
362 -- for X'Address use Expr
363
364 -- where Expr has a value known at compile time or is of the form Y'Address
365 -- or recursively is a reference to a constant initialized with either of
366 -- these forms, and the value of Expr is not a multiple of X's alignment,
367 -- or if Y has a smaller alignment than X, then that merits a warning about
368 -- possible bad alignment. The following table collects address clauses of
369 -- this kind. We put these in a table so that they can be checked after the
370 -- back end has completed annotation of the alignments of objects, since we
371 -- can catch more cases that way.
372
373 type Address_Clause_Check_Record is record
374 N : Node_Id;
375 -- The address clause
376
377 X : Entity_Id;
378 -- The entity of the object subject to the address clause
379
380 A : Uint;
381 -- The value of the address in the first case
382
383 Y : Entity_Id;
384 -- The entity of the object being overlaid in the second case
385
386 Off : Boolean;
387 -- Whether the address is offset within Y in the second case
388
389 Alignment_Checks_Suppressed : Boolean;
390 -- Whether alignment checks are suppressed by an active scope suppress
391 -- setting. We need to save the value in order to be able to reuse it
392 -- after the back end has been run.
393 end record;
394
395 package Address_Clause_Checks is new Table.Table (
396 Table_Component_Type => Address_Clause_Check_Record,
397 Table_Index_Type => Int,
398 Table_Low_Bound => 1,
399 Table_Initial => 20,
400 Table_Increment => 200,
401 Table_Name => "Address_Clause_Checks");
402
403 function Alignment_Checks_Suppressed
404 (ACCR : Address_Clause_Check_Record) return Boolean;
405 -- Return whether the alignment check generated for the address clause
406 -- is suppressed.
407
408 ---------------------------------
409 -- Alignment_Checks_Suppressed --
410 ---------------------------------
411
412 function Alignment_Checks_Suppressed
413 (ACCR : Address_Clause_Check_Record) return Boolean
414 is
415 begin
416 if Checks_May_Be_Suppressed (ACCR.X) then
417 return Is_Check_Suppressed (ACCR.X, Alignment_Check);
418 else
419 return ACCR.Alignment_Checks_Suppressed;
420 end if;
421 end Alignment_Checks_Suppressed;
422
423 -----------------------------------------
424 -- Adjust_Record_For_Reverse_Bit_Order --
425 -----------------------------------------
426
427 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
428 Max_Machine_Scalar_Size : constant Uint :=
429 UI_From_Int (System_Max_Integer_Size);
430 -- We use this as the maximum machine scalar size
431
432 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
433
434 CC : Node_Id;
435 Comp : Node_Id;
436 Num_CC : Natural;
437
438 begin
439 -- The processing done here used to depend on the Ada version, but the
440 -- behavior has been changed by AI95-0133. However this AI is a Binding
441 -- Interpretation, so we now implement it even in Ada 95 mode. But the
442 -- original behavior from unamended Ada 95 is available for the sake of
443 -- compatibility under the debugging switch -gnatd.p in Ada 95 mode.
444
445 if Ada_Version < Ada_2005 and then Debug_Flag_Dot_P then
446 Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R);
447 return;
448 end if;
449
450 -- For Ada 2005, we do machine scalar processing, as fully described In
451 -- AI-133. This involves gathering all components which start at the
452 -- same byte offset and processing them together. Same approach is still
453 -- valid in later versions including Ada 2012.
454
455 -- Note that component clauses found on record types may be inherited,
456 -- in which case the layout of the component with such a clause still
457 -- has to be done at this point. Therefore, the processing done here
458 -- must exclusively rely on the Component_Clause of the component.
459
460 -- This first loop through components does two things. First it deals
461 -- with the case of components with component clauses whose length is
462 -- greater than the maximum machine scalar size (either accepting them
463 -- or rejecting as needed). Second, it counts the number of components
464 -- with component clauses whose length does not exceed this maximum for
465 -- later processing.
466
467 Num_CC := 0;
468 Comp := First_Component_Or_Discriminant (R);
469 while Present (Comp) loop
470 CC := Component_Clause (Comp);
471
472 if Present (CC) then
473 declare
474 Fbit : constant Uint := Static_Integer (First_Bit (CC));
475 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
476
477 begin
478 -- Case of component with last bit >= max machine scalar
479
480 if Lbit >= Max_Machine_Scalar_Size then
481
482 -- This is allowed only if first bit is zero, and last bit
483 -- + 1 is a multiple of storage unit size.
484
485 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
486
487 -- This is the case to give a warning if enabled
488
489 if Warn_On_Reverse_Bit_Order then
490 Error_Msg_N
491 ("info: multi-byte field specified with "
492 & "non-standard Bit_Order?.v?", CC);
493
494 if Bytes_Big_Endian then
495 Error_Msg_N
496 ("\bytes are not reversed "
497 & "(component is big-endian)?.v?", CC);
498 else
499 Error_Msg_N
500 ("\bytes are not reversed "
501 & "(component is little-endian)?.v?", CC);
502 end if;
503 end if;
504
505 -- Give error message for RM 13.5.1(10) violation
506
507 else
508 Error_Msg_FE
509 ("machine scalar rules not followed for&",
510 First_Bit (CC), Comp);
511
512 Error_Msg_Uint_1 := Lbit + 1;
513 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
514 Error_Msg_F
515 ("\last bit + 1 (^) exceeds maximum machine scalar "
516 & "size (^)", First_Bit (CC));
517
518 if (Lbit + 1) mod SSU /= 0 then
519 Error_Msg_Uint_1 := SSU;
520 Error_Msg_F
521 ("\and is not a multiple of Storage_Unit (^) "
522 & "(RM 13.5.1(10))", First_Bit (CC));
523
524 else
525 Error_Msg_Uint_1 := Fbit;
526 Error_Msg_F
527 ("\and first bit (^) is non-zero "
528 & "(RM 13.4.1(10))", First_Bit (CC));
529 end if;
530 end if;
531
532 -- OK case of machine scalar related component clause. For now,
533 -- just count them.
534
535 else
536 Num_CC := Num_CC + 1;
537 end if;
538 end;
539 end if;
540
541 Next_Component_Or_Discriminant (Comp);
542 end loop;
543
544 -- We need to sort the component clauses on the basis of the Position
545 -- values in the clause, so we can group clauses with the same Position
546 -- together to determine the relevant machine scalar size.
547
548 Sort_CC : declare
549 Comps : array (0 .. Num_CC) of Entity_Id;
550 -- Array to collect component and discriminant entities. The data
551 -- starts at index 1, the 0'th entry is for the sort routine.
552
553 function CP_Lt (Op1, Op2 : Natural) return Boolean;
554 -- Compare routine for Sort
555
556 procedure CP_Move (From : Natural; To : Natural);
557 -- Move routine for Sort
558
559 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
560
561 MaxL : Uint;
562 -- Maximum last bit value of any component in this set
563
564 MSS : Uint;
565 -- Corresponding machine scalar size
566
567 Start : Natural;
568 Stop : Natural;
569 -- Start and stop positions in the component list of the set of
570 -- components with the same starting position (that constitute
571 -- components in a single machine scalar).
572
573 -----------
574 -- CP_Lt --
575 -----------
576
577 function CP_Lt (Op1, Op2 : Natural) return Boolean is
578 begin
579 return
580 Position (Component_Clause (Comps (Op1))) <
581 Position (Component_Clause (Comps (Op2)));
582 end CP_Lt;
583
584 -------------
585 -- CP_Move --
586 -------------
587
588 procedure CP_Move (From : Natural; To : Natural) is
589 begin
590 Comps (To) := Comps (From);
591 end CP_Move;
592
593 -- Start of processing for Sort_CC
594
595 begin
596 -- Collect the machine scalar relevant component clauses
597
598 Num_CC := 0;
599 Comp := First_Component_Or_Discriminant (R);
600 while Present (Comp) loop
601 declare
602 CC : constant Node_Id := Component_Clause (Comp);
603
604 begin
605 -- Collect only component clauses whose last bit is less than
606 -- machine scalar size. Any component clause whose last bit
607 -- exceeds this value does not take part in machine scalar
608 -- layout considerations. The test for Error_Posted makes sure
609 -- we exclude component clauses for which we already posted an
610 -- error.
611
612 if Present (CC)
613 and then not Error_Posted (Last_Bit (CC))
614 and then Static_Integer (Last_Bit (CC)) <
615 Max_Machine_Scalar_Size
616 then
617 Num_CC := Num_CC + 1;
618 Comps (Num_CC) := Comp;
619 end if;
620 end;
621
622 Next_Component_Or_Discriminant (Comp);
623 end loop;
624
625 -- Sort by ascending position number
626
627 Sorting.Sort (Num_CC);
628
629 -- We now have all the components whose size does not exceed the max
630 -- machine scalar value, sorted by starting position. In this loop we
631 -- gather groups of clauses starting at the same position, to process
632 -- them in accordance with AI-133.
633
634 Stop := 0;
635 while Stop < Num_CC loop
636 Start := Stop + 1;
637 Stop := Start;
638 MaxL :=
639 Static_Integer
640 (Last_Bit (Component_Clause (Comps (Start))));
641 while Stop < Num_CC loop
642 if Static_Integer
643 (Position (Component_Clause (Comps (Stop + 1)))) =
644 Static_Integer
645 (Position (Component_Clause (Comps (Stop))))
646 then
647 Stop := Stop + 1;
648 MaxL :=
649 UI_Max
650 (MaxL,
651 Static_Integer
652 (Last_Bit
653 (Component_Clause (Comps (Stop)))));
654 else
655 exit;
656 end if;
657 end loop;
658
659 -- Now we have a group of component clauses from Start to Stop
660 -- whose positions are identical, and MaxL is the maximum last
661 -- bit value of any of these components.
662
663 -- We need to determine the corresponding machine scalar size.
664 -- This loop assumes that machine scalar sizes are even, and that
665 -- each possible machine scalar has twice as many bits as the next
666 -- smaller one.
667
668 MSS := Max_Machine_Scalar_Size;
669 while MSS mod 2 = 0
670 and then (MSS / 2) >= SSU
671 and then (MSS / 2) > MaxL
672 loop
673 MSS := MSS / 2;
674 end loop;
675
676 -- Here is where we fix up the Component_Bit_Offset value to
677 -- account for the reverse bit order. Some examples of what needs
678 -- to be done for the case of a machine scalar size of 8 are:
679
680 -- First_Bit .. Last_Bit Component_Bit_Offset
681 -- old new old new
682
683 -- 0 .. 0 7 .. 7 0 7
684 -- 0 .. 1 6 .. 7 0 6
685 -- 0 .. 2 5 .. 7 0 5
686 -- 0 .. 7 0 .. 7 0 4
687
688 -- 1 .. 1 6 .. 6 1 6
689 -- 1 .. 4 3 .. 6 1 3
690 -- 4 .. 7 0 .. 3 4 0
691
692 -- The rule is that the first bit is obtained by subtracting the
693 -- old ending bit from machine scalar size - 1.
694
695 for C in Start .. Stop loop
696 declare
697 Comp : constant Entity_Id := Comps (C);
698 CC : constant Node_Id := Component_Clause (Comp);
699
700 FB : constant Uint := Static_Integer (First_Bit (CC));
701 LB : constant Uint := Static_Integer (Last_Bit (CC));
702 NFB : constant Uint := MSS - 1 - LB;
703 NLB : constant Uint := NFB + LB - FB;
704 Pos : constant Uint := Static_Integer (Position (CC));
705
706 begin
707 -- Do not warn for the artificial clause built for the tag
708 -- in Check_Record_Representation_Clause if it is inherited.
709
710 if Warn_On_Reverse_Bit_Order
711 and then Chars (Comp) /= Name_uTag
712 then
713 Error_Msg_Uint_1 := MSS;
714 Error_Msg_N
715 ("info: reverse bit order in machine scalar of "
716 & "length^?.v?", First_Bit (CC));
717 Error_Msg_Uint_1 := NFB;
718 Error_Msg_Uint_2 := NLB;
719
720 if Bytes_Big_Endian then
721 Error_Msg_NE
722 ("\big-endian range for component & is ^ .. ^?.v?",
723 First_Bit (CC), Comp);
724 else
725 Error_Msg_NE
726 ("\little-endian range for component " &
727 "& is ^ .. ^?.v?",
728 First_Bit (CC), Comp);
729 end if;
730 end if;
731
732 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
733 Set_Esize (Comp, 1 + (NLB - NFB));
734 Set_Normalized_First_Bit (Comp, NFB mod SSU);
735 Set_Normalized_Position (Comp, Pos + NFB / SSU);
736 end;
737 end loop;
738 end loop;
739 end Sort_CC;
740 end Adjust_Record_For_Reverse_Bit_Order;
741
742 ------------------------------------------------
743 -- Adjust_Record_For_Reverse_Bit_Order_Ada_95 --
744 ------------------------------------------------
745
746 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id) is
747 CC : Node_Id;
748 Comp : Node_Id;
749
750 begin
751 -- For Ada 95, we just renumber bits within a storage unit. We do the
752 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
753 -- Ada 83, and are free to add this extension.
754
755 Comp := First_Component_Or_Discriminant (R);
756 while Present (Comp) loop
757 CC := Component_Clause (Comp);
758
759 -- If component clause is present, then deal with the non-default
760 -- bit order case for Ada 95 mode.
761
762 -- We only do this processing for the base type, and in fact that
763 -- is important, since otherwise if there are record subtypes, we
764 -- could reverse the bits once for each subtype, which is wrong.
765
766 if Present (CC) and then Ekind (R) = E_Record_Type then
767 declare
768 CFB : constant Uint := Component_Bit_Offset (Comp);
769 CSZ : constant Uint := Esize (Comp);
770 CLC : constant Node_Id := Component_Clause (Comp);
771 Pos : constant Node_Id := Position (CLC);
772 FB : constant Node_Id := First_Bit (CLC);
773
774 Storage_Unit_Offset : constant Uint :=
775 CFB / System_Storage_Unit;
776
777 Start_Bit : constant Uint :=
778 CFB mod System_Storage_Unit;
779
780 begin
781 -- Cases where field goes over storage unit boundary
782
783 if Start_Bit + CSZ > System_Storage_Unit then
784
785 -- Allow multi-byte field but generate warning
786
787 if Start_Bit mod System_Storage_Unit = 0
788 and then CSZ mod System_Storage_Unit = 0
789 then
790 Error_Msg_N
791 ("info: multi-byte field specified with non-standard "
792 & "Bit_Order?.v?", CLC);
793
794 if Bytes_Big_Endian then
795 Error_Msg_N
796 ("\bytes are not reversed "
797 & "(component is big-endian)?.v?", CLC);
798 else
799 Error_Msg_N
800 ("\bytes are not reversed "
801 & "(component is little-endian)?.v?", CLC);
802 end if;
803
804 -- Do not allow non-contiguous field
805
806 else
807 Error_Msg_N
808 ("attempt to specify non-contiguous field not "
809 & "permitted", CLC);
810 Error_Msg_N
811 ("\caused by non-standard Bit_Order specified in "
812 & "legacy Ada 95 mode", CLC);
813 end if;
814
815 -- Case where field fits in one storage unit
816
817 else
818 -- Give warning if suspicious component clause
819
820 if Intval (FB) >= System_Storage_Unit
821 and then Warn_On_Reverse_Bit_Order
822 then
823 Error_Msg_N
824 ("info: Bit_Order clause does not affect byte "
825 & "ordering?.v?", Pos);
826 Error_Msg_Uint_1 :=
827 Intval (Pos) + Intval (FB) /
828 System_Storage_Unit;
829 Error_Msg_N
830 ("info: position normalized to ^ before bit order "
831 & "interpreted?.v?", Pos);
832 end if;
833
834 -- Here is where we fix up the Component_Bit_Offset value
835 -- to account for the reverse bit order. Some examples of
836 -- what needs to be done are:
837
838 -- First_Bit .. Last_Bit Component_Bit_Offset
839 -- old new old new
840
841 -- 0 .. 0 7 .. 7 0 7
842 -- 0 .. 1 6 .. 7 0 6
843 -- 0 .. 2 5 .. 7 0 5
844 -- 0 .. 7 0 .. 7 0 4
845
846 -- 1 .. 1 6 .. 6 1 6
847 -- 1 .. 4 3 .. 6 1 3
848 -- 4 .. 7 0 .. 3 4 0
849
850 -- The rule is that the first bit is obtained by subtracting
851 -- the old ending bit from storage_unit - 1.
852
853 Set_Component_Bit_Offset (Comp,
854 (Storage_Unit_Offset * System_Storage_Unit) +
855 (System_Storage_Unit - 1) -
856 (Start_Bit + CSZ - 1));
857
858 Set_Normalized_Position (Comp,
859 Component_Bit_Offset (Comp) / System_Storage_Unit);
860
861 Set_Normalized_First_Bit (Comp,
862 Component_Bit_Offset (Comp) mod System_Storage_Unit);
863 end if;
864 end;
865 end if;
866
867 Next_Component_Or_Discriminant (Comp);
868 end loop;
869 end Adjust_Record_For_Reverse_Bit_Order_Ada_95;
870
871 -------------------------------------
872 -- Alignment_Check_For_Size_Change --
873 -------------------------------------
874
875 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
876 begin
877 -- If the alignment is known, and not set by a rep clause, and is
878 -- inconsistent with the size being set, then reset it to unknown,
879 -- we assume in this case that the size overrides the inherited
880 -- alignment, and that the alignment must be recomputed.
881
882 if Known_Alignment (Typ)
883 and then not Has_Alignment_Clause (Typ)
884 and then Present (Size)
885 and then Size mod (Alignment (Typ) * SSU) /= 0
886 then
887 Reinit_Alignment (Typ);
888 end if;
889 end Alignment_Check_For_Size_Change;
890
891 -----------------------------------
892 -- All_Membership_Choices_Static --
893 -----------------------------------
894
895 function All_Membership_Choices_Static (Expr : Node_Id) return Boolean is
896 pragma Assert (Nkind (Expr) in N_Membership_Test);
897 begin
898 pragma Assert
899 (Present (Right_Opnd (Expr))
900 xor
901 Present (Alternatives (Expr)));
902
903 if Present (Right_Opnd (Expr)) then
904 return Is_Static_Choice (Right_Opnd (Expr));
905 else
906 return All_Static_Choices (Alternatives (Expr));
907 end if;
908 end All_Membership_Choices_Static;
909
910 ------------------------
911 -- All_Static_Choices --
912 ------------------------
913
914 function All_Static_Choices (L : List_Id) return Boolean is
915 N : Node_Id;
916
917 begin
918 N := First (L);
919 while Present (N) loop
920 if not Is_Static_Choice (N) then
921 return False;
922 end if;
923
924 Next (N);
925 end loop;
926
927 return True;
928 end All_Static_Choices;
929
930 -------------------------------------
931 -- Analyze_Aspects_At_Freeze_Point --
932 -------------------------------------
933
934 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
935 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
936 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
937 -- the aspect specification node ASN.
938
939 procedure Check_Aspect_Too_Late (N : Node_Id);
940 -- This procedure is similar to Rep_Item_Too_Late for representation
941 -- aspects that apply to type and that do not have a corresponding
942 -- pragma.
943 -- Used to check in particular that the expression associated with
944 -- aspect node N for the given type (entity) of the aspect does not
945 -- appear too late according to the rules in RM 13.1(9) and 13.1(10).
946
947 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
948 -- Given an aspect specification node ASN whose expression is an
949 -- optional Boolean, this routines creates the corresponding pragma
950 -- at the freezing point.
951
952 ----------------------------------
953 -- Analyze_Aspect_Default_Value --
954 ----------------------------------
955
956 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
957 Ent : constant Entity_Id := Entity (ASN);
958 Expr : constant Node_Id := Expression (ASN);
959
960 begin
961 Set_Has_Default_Aspect (Base_Type (Ent));
962
963 if Is_Scalar_Type (Ent) then
964 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
965 else
966 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
967 end if;
968
969 Check_Aspect_Too_Late (ASN);
970 end Analyze_Aspect_Default_Value;
971
972 ---------------------------
973 -- Check_Aspect_Too_Late --
974 ---------------------------
975
976 procedure Check_Aspect_Too_Late (N : Node_Id) is
977 Typ : constant Entity_Id := Entity (N);
978 Expr : constant Node_Id := Expression (N);
979
980 function Find_Type_Reference
981 (Typ : Entity_Id; Expr : Node_Id) return Boolean;
982 -- Return True if a reference to type Typ is found in the expression
983 -- Expr.
984
985 -------------------------
986 -- Find_Type_Reference --
987 -------------------------
988
989 function Find_Type_Reference
990 (Typ : Entity_Id; Expr : Node_Id) return Boolean
991 is
992 function Find_Type (N : Node_Id) return Traverse_Result;
993 -- Set Found to True if N refers to Typ
994
995 ---------------
996 -- Find_Type --
997 ---------------
998
999 function Find_Type (N : Node_Id) return Traverse_Result is
1000 begin
1001 if N = Typ
1002 or else (Nkind (N) in N_Identifier | N_Expanded_Name
1003 and then Present (Entity (N))
1004 and then Entity (N) = Typ)
1005 then
1006 return Abandon;
1007 else
1008 return OK;
1009 end if;
1010 end Find_Type;
1011
1012 function Search_Type_Reference is new Traverse_Func (Find_Type);
1013
1014 begin
1015 return Search_Type_Reference (Expr) = Abandon;
1016 end Find_Type_Reference;
1017
1018 Parent_Type : Entity_Id;
1019
1020 begin
1021 -- Ensure Expr is analyzed so that e.g. all types are properly
1022 -- resolved for Find_Type_Reference.
1023
1024 Analyze (Expr);
1025
1026 -- A self-referential aspect is illegal if it forces freezing the
1027 -- entity before the corresponding aspect has been analyzed.
1028
1029 if Find_Type_Reference (Typ, Expr) then
1030 Error_Msg_NE
1031 ("aspect specification causes premature freezing of&", N, Typ);
1032 end if;
1033
1034 -- For representation aspects, check for case of untagged derived
1035 -- type whose parent either has primitive operations (pre Ada 2022),
1036 -- or is a by-reference type (RM 13.1(10)).
1037 -- Strictly speaking the check also applies to Ada 2012 but it is
1038 -- really too constraining for existing code already, so relax it.
1039 -- ??? Confirming aspects should be allowed here.
1040
1041 if Is_Representation_Aspect (Get_Aspect_Id (N))
1042 and then Is_Derived_Type (Typ)
1043 and then not Is_Tagged_Type (Typ)
1044 then
1045 Parent_Type := Etype (Base_Type (Typ));
1046
1047 if Ada_Version <= Ada_2012
1048 and then Has_Primitive_Operations (Parent_Type)
1049 then
1050 Error_Msg_N
1051 ("|representation aspect not permitted before Ada 2022: " &
1052 "use -gnat2022!", N);
1053 Error_Msg_NE
1054 ("\parent type & has primitive operations!", N, Parent_Type);
1055
1056 elsif Is_By_Reference_Type (Parent_Type) then
1057 No_Type_Rep_Item (N);
1058 Error_Msg_NE
1059 ("\parent type & is a by-reference type!", N, Parent_Type);
1060 end if;
1061 end if;
1062 end Check_Aspect_Too_Late;
1063
1064 -------------------------------------
1065 -- Make_Pragma_From_Boolean_Aspect --
1066 -------------------------------------
1067
1068 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1069 Ident : constant Node_Id := Identifier (ASN);
1070 A_Name : constant Name_Id := Chars (Ident);
1071 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1072 Ent : constant Entity_Id := Entity (ASN);
1073 Expr : constant Node_Id := Expression (ASN);
1074 Loc : constant Source_Ptr := Sloc (ASN);
1075
1076 procedure Check_False_Aspect_For_Derived_Type;
1077 -- This procedure checks for the case of a false aspect for a derived
1078 -- type, which improperly tries to cancel an aspect inherited from
1079 -- the parent.
1080
1081 -----------------------------------------
1082 -- Check_False_Aspect_For_Derived_Type --
1083 -----------------------------------------
1084
1085 procedure Check_False_Aspect_For_Derived_Type is
1086 Par : Node_Id;
1087
1088 begin
1089 -- We are only checking derived types
1090
1091 if not Is_Derived_Type (E) then
1092 return;
1093 end if;
1094
1095 Par := Nearest_Ancestor (E);
1096
1097 case A_Id is
1098 when Aspect_Atomic
1099 | Aspect_Shared
1100 =>
1101 if not Is_Atomic (Par) then
1102 return;
1103 end if;
1104
1105 when Aspect_Atomic_Components =>
1106 if not Has_Atomic_Components (Par) then
1107 return;
1108 end if;
1109
1110 when Aspect_Discard_Names =>
1111 if not Discard_Names (Par) then
1112 return;
1113 end if;
1114
1115 when Aspect_Pack =>
1116 if not Is_Packed (Par) then
1117 return;
1118 end if;
1119
1120 when Aspect_Unchecked_Union =>
1121 if not Is_Unchecked_Union (Par) then
1122 return;
1123 end if;
1124
1125 when Aspect_Volatile =>
1126 if not Is_Volatile (Par) then
1127 return;
1128 end if;
1129
1130 when Aspect_Volatile_Components =>
1131 if not Has_Volatile_Components (Par) then
1132 return;
1133 end if;
1134
1135 when Aspect_Volatile_Full_Access
1136 | Aspect_Full_Access_Only
1137 =>
1138 if not Is_Volatile_Full_Access (Par) then
1139 return;
1140 end if;
1141
1142 when others =>
1143 return;
1144 end case;
1145
1146 -- Fall through means we are canceling an inherited aspect
1147
1148 Error_Msg_Name_1 := A_Name;
1149 Error_Msg_NE
1150 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1151 end Check_False_Aspect_For_Derived_Type;
1152
1153 -- Local variables
1154
1155 Prag : Node_Id;
1156 P_Name : Name_Id;
1157
1158 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1159
1160 begin
1161 if Present (Expr) and then Is_False (Static_Boolean (Expr)) then
1162 Check_False_Aspect_For_Derived_Type;
1163
1164 else
1165 -- There is no Full_Access_Only pragma so use VFA instead
1166
1167 if A_Name = Name_Full_Access_Only then
1168 P_Name := Name_Volatile_Full_Access;
1169 else
1170 P_Name := A_Name;
1171 end if;
1172
1173 Prag :=
1174 Make_Pragma (Loc,
1175 Pragma_Identifier =>
1176 Make_Identifier (Sloc (Ident), P_Name),
1177 Pragma_Argument_Associations => New_List (
1178 Make_Pragma_Argument_Association (Sloc (Ident),
1179 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
1180
1181 Set_From_Aspect_Specification (Prag, True);
1182 Set_Corresponding_Aspect (Prag, ASN);
1183 Set_Aspect_Rep_Item (ASN, Prag);
1184 Set_Is_Delayed_Aspect (Prag);
1185 Set_Parent (Prag, ASN);
1186 end if;
1187 end Make_Pragma_From_Boolean_Aspect;
1188
1189 -- Local variables
1190
1191 A_Id : Aspect_Id;
1192 ASN : Node_Id;
1193 Ritem : Node_Id;
1194
1195 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1196
1197 begin
1198 -- Must be visible in current scope, but if this is a type from a nested
1199 -- package it may be frozen from an object declaration in the enclosing
1200 -- scope, so install the package declarations to complete the analysis
1201 -- of the aspects, if any. If the package itself is frozen the type will
1202 -- have been frozen as well.
1203
1204 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1205 if Is_Type (E) and then From_Nested_Package (E) then
1206 declare
1207 Pack : constant Entity_Id := Scope (E);
1208
1209 begin
1210 Push_Scope (Pack);
1211 Install_Visible_Declarations (Pack);
1212 Install_Private_Declarations (Pack);
1213 Analyze_Aspects_At_Freeze_Point (E);
1214
1215 if Is_Private_Type (E)
1216 and then Present (Full_View (E))
1217 then
1218 Analyze_Aspects_At_Freeze_Point (Full_View (E));
1219 end if;
1220
1221 End_Package_Scope (Pack);
1222 return;
1223 end;
1224
1225 -- Aspects from other entities in different contexts are analyzed
1226 -- elsewhere.
1227
1228 else
1229 return;
1230 end if;
1231 end if;
1232
1233 -- Look for aspect specification entries for this entity
1234
1235 ASN := First_Rep_Item (E);
1236 while Present (ASN) loop
1237 if Nkind (ASN) = N_Aspect_Specification then
1238 exit when Entity (ASN) /= E;
1239
1240 if Is_Delayed_Aspect (ASN) then
1241 A_Id := Get_Aspect_Id (ASN);
1242
1243 case A_Id is
1244
1245 -- For aspects whose expression is an optional Boolean, make
1246 -- the corresponding pragma at the freeze point.
1247
1248 when Boolean_Aspects
1249 | Library_Unit_Aspects
1250 =>
1251 -- Aspects Export and Import require special handling.
1252 -- Both are by definition Boolean and may benefit from
1253 -- forward references, however their expressions are
1254 -- treated as static. In addition, the syntax of their
1255 -- corresponding pragmas requires extra "pieces" which
1256 -- may also contain forward references. To account for
1257 -- all of this, the corresponding pragma is created by
1258 -- Analyze_Aspect_Export_Import, but is not analyzed as
1259 -- the complete analysis must happen now.
1260
1261 -- Aspect Full_Access_Only must be analyzed last so that
1262 -- aspects Volatile and Atomic, if any, are analyzed.
1263
1264 -- Skip creation of pragma Preelaborable_Initialization
1265 -- in the case where the aspect has an expression,
1266 -- because the pragma is only needed for setting flag
1267 -- Known_To_Have_Preelab_Init, which is set by other
1268 -- means following resolution of the aspect expression.
1269
1270 if A_Id not in Aspect_Export
1271 | Aspect_Full_Access_Only
1272 | Aspect_Import
1273 and then (A_Id /= Aspect_Preelaborable_Initialization
1274 or else No (Expression (ASN)))
1275 then
1276 Make_Pragma_From_Boolean_Aspect (ASN);
1277 end if;
1278
1279 -- Special handling for aspects that don't correspond to
1280 -- pragmas/attributes.
1281
1282 when Aspect_Default_Value
1283 | Aspect_Default_Component_Value
1284 =>
1285 -- Do not inherit aspect for anonymous base type of a
1286 -- scalar or array type, because they apply to the first
1287 -- subtype of the type, and will be processed when that
1288 -- first subtype is frozen.
1289
1290 if Is_Derived_Type (E)
1291 and then not Comes_From_Source (E)
1292 and then E /= First_Subtype (E)
1293 then
1294 null;
1295 else
1296 Analyze_Aspect_Default_Value (ASN);
1297 end if;
1298
1299 -- Ditto for iterator aspects, because the corresponding
1300 -- attributes may not have been analyzed yet.
1301
1302 when Aspect_Constant_Indexing
1303 | Aspect_Default_Iterator
1304 | Aspect_Iterator_Element
1305 | Aspect_Variable_Indexing
1306 =>
1307 Analyze (Expression (ASN));
1308
1309 if Etype (Expression (ASN)) = Any_Type then
1310 Error_Msg_NE
1311 ("\aspect must be fully defined before & is frozen",
1312 ASN, E);
1313 end if;
1314
1315 when Aspect_Integer_Literal
1316 | Aspect_Real_Literal
1317 | Aspect_String_Literal
1318 =>
1319 Validate_Literal_Aspect (E, ASN);
1320
1321 when Aspect_Iterable =>
1322 Validate_Iterable_Aspect (E, ASN);
1323
1324 when Aspect_Designated_Storage_Model =>
1325 Analyze_And_Resolve (Expression (ASN));
1326
1327 if not Is_Entity_Name (Expression (ASN))
1328 or else not Is_Object (Entity (Expression (ASN)))
1329 or else
1330 No (Find_Aspect (Etype (Expression (ASN)),
1331 Aspect_Storage_Model_Type))
1332 then
1333 Error_Msg_N
1334 ("must specify name of stand-alone object of type "
1335 & "with aspect Storage_Model_Type",
1336 Expression (ASN));
1337
1338 -- Set access type's Associated_Storage_Pool to denote
1339 -- the Storage_Model_Type object given for the aspect
1340 -- (even though that isn't actually an Ada storage pool).
1341
1342 else
1343 Set_Associated_Storage_Pool
1344 (E, Entity (Expression (ASN)));
1345 end if;
1346
1347 when Aspect_Storage_Model_Type =>
1348 Validate_Storage_Model_Type_Aspect (E, ASN);
1349
1350 when Aspect_Aggregate =>
1351 null;
1352
1353 when others =>
1354 null;
1355 end case;
1356
1357 Ritem := Aspect_Rep_Item (ASN);
1358
1359 if Present (Ritem) then
1360 Analyze (Ritem);
1361 end if;
1362 end if;
1363 end if;
1364
1365 Next_Rep_Item (ASN);
1366 end loop;
1367
1368 -- Make a second pass for a Full_Access_Only entry
1369
1370 ASN := First_Rep_Item (E);
1371 while Present (ASN) loop
1372 if Nkind (ASN) = N_Aspect_Specification then
1373 exit when Entity (ASN) /= E;
1374
1375 if Get_Aspect_Id (ASN) = Aspect_Full_Access_Only then
1376 Make_Pragma_From_Boolean_Aspect (ASN);
1377 Ritem := Aspect_Rep_Item (ASN);
1378 if Present (Ritem) then
1379 Analyze (Ritem);
1380 end if;
1381 end if;
1382 end if;
1383
1384 Next_Rep_Item (ASN);
1385 end loop;
1386
1387 if In_Instance
1388 and then E /= Base_Type (E)
1389 and then Is_First_Subtype (E)
1390 then
1391 Inherit_Rep_Item_Chain (Base_Type (E), E);
1392 end if;
1393 end Analyze_Aspects_At_Freeze_Point;
1394
1395 -----------------------------------
1396 -- Analyze_Aspect_Specifications --
1397 -----------------------------------
1398
1399 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1400 pragma Assert (Present (E));
1401
1402 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1403 -- Establish linkages between an aspect and its corresponding pragma
1404
1405 procedure Insert_Pragma
1406 (Prag : Node_Id;
1407 Is_Instance : Boolean := False);
1408 -- Subsidiary to the analysis of aspects
1409 -- Abstract_State
1410 -- Attach_Handler
1411 -- Contract_Cases
1412 -- Depends
1413 -- Ghost
1414 -- Global
1415 -- Initial_Condition
1416 -- Initializes
1417 -- Post
1418 -- Pre
1419 -- Refined_Depends
1420 -- Refined_Global
1421 -- Refined_State
1422 -- SPARK_Mode
1423 -- Subprogram_Variant
1424 -- Warnings
1425 -- Insert pragma Prag such that it mimics the placement of a source
1426 -- pragma of the same kind. Flag Is_Generic should be set when the
1427 -- context denotes a generic instance.
1428
1429 function Relocate_Expression (Source : Node_Id) return Node_Id;
1430 -- Outside of a generic this function is equivalent to Relocate_Node.
1431 -- Inside a generic it is an identity function, because Relocate_Node
1432 -- would create a new node that is not associated with the generic
1433 -- template. This association is needed to save references to entities
1434 -- that are global to the generic (and might be not visible from where
1435 -- the generic is instantiated).
1436 --
1437 -- Inside a generic the original tree is shared between aspect and
1438 -- a corresponding pragma (or an attribute definition clause). This
1439 -- parallels what is done in sem_prag.adb (see Get_Argument).
1440
1441 --------------
1442 -- Decorate --
1443 --------------
1444
1445 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1446 begin
1447 Set_Aspect_Rep_Item (Asp, Prag);
1448 Set_Corresponding_Aspect (Prag, Asp);
1449 Set_From_Aspect_Specification (Prag);
1450 Set_Parent (Prag, Asp);
1451 end Decorate;
1452
1453 -------------------
1454 -- Insert_Pragma --
1455 -------------------
1456
1457 procedure Insert_Pragma
1458 (Prag : Node_Id;
1459 Is_Instance : Boolean := False)
1460 is
1461 Aux : Node_Id;
1462 Decl : Node_Id;
1463 Decls : List_Id;
1464 Def : Node_Id;
1465 Inserted : Boolean := False;
1466
1467 begin
1468 -- When the aspect appears on an entry, package, protected unit,
1469 -- subprogram, or task unit body, insert the generated pragma at the
1470 -- top of the body declarations to emulate the behavior of a source
1471 -- pragma.
1472
1473 -- package body Pack with Aspect is
1474
1475 -- package body Pack is
1476 -- pragma Prag;
1477
1478 if Nkind (N) in N_Entry_Body
1479 | N_Package_Body
1480 | N_Protected_Body
1481 | N_Subprogram_Body
1482 | N_Task_Body
1483 then
1484 Decls := Declarations (N);
1485
1486 if No (Decls) then
1487 Decls := New_List;
1488 Set_Declarations (N, Decls);
1489 end if;
1490
1491 Prepend_To (Decls, Prag);
1492
1493 -- When the aspect is associated with a [generic] package declaration
1494 -- insert the generated pragma at the top of the visible declarations
1495 -- to emulate the behavior of a source pragma.
1496
1497 -- package Pack with Aspect is
1498
1499 -- package Pack is
1500 -- pragma Prag;
1501
1502 elsif Nkind (N) in N_Generic_Package_Declaration
1503 | N_Package_Declaration
1504 then
1505 Decls := Visible_Declarations (Specification (N));
1506
1507 if No (Decls) then
1508 Decls := New_List;
1509 Set_Visible_Declarations (Specification (N), Decls);
1510 end if;
1511
1512 -- The visible declarations of a generic instance have the
1513 -- following structure:
1514
1515 -- <renamings of generic formals>
1516 -- <renamings of internally-generated spec and body>
1517 -- <first source declaration>
1518
1519 -- Insert the pragma before the first source declaration by
1520 -- skipping the instance "header" to ensure proper visibility of
1521 -- all formals.
1522
1523 if Is_Instance then
1524 Decl := First (Decls);
1525 while Present (Decl) loop
1526 if Comes_From_Source (Decl) then
1527 Insert_Before (Decl, Prag);
1528 Inserted := True;
1529 exit;
1530 else
1531 Next (Decl);
1532 end if;
1533 end loop;
1534
1535 -- The pragma is placed after the instance "header"
1536
1537 if not Inserted then
1538 Append_To (Decls, Prag);
1539 end if;
1540
1541 -- Otherwise this is not a generic instance
1542
1543 else
1544 Prepend_To (Decls, Prag);
1545 end if;
1546
1547 -- When the aspect is associated with a protected unit declaration,
1548 -- insert the generated pragma at the top of the visible declarations
1549 -- the emulate the behavior of a source pragma.
1550
1551 -- protected [type] Prot with Aspect is
1552
1553 -- protected [type] Prot is
1554 -- pragma Prag;
1555
1556 elsif Nkind (N) = N_Protected_Type_Declaration then
1557 Def := Protected_Definition (N);
1558
1559 if No (Def) then
1560 Def :=
1561 Make_Protected_Definition (Sloc (N),
1562 Visible_Declarations => New_List,
1563 End_Label => Empty);
1564
1565 Set_Protected_Definition (N, Def);
1566 end if;
1567
1568 Decls := Visible_Declarations (Def);
1569
1570 if No (Decls) then
1571 Decls := New_List;
1572 Set_Visible_Declarations (Def, Decls);
1573 end if;
1574
1575 Prepend_To (Decls, Prag);
1576
1577 -- When the aspect is associated with a task unit declaration, insert
1578 -- insert the generated pragma at the top of the visible declarations
1579 -- the emulate the behavior of a source pragma.
1580
1581 -- task [type] Prot with Aspect is
1582
1583 -- task [type] Prot is
1584 -- pragma Prag;
1585
1586 elsif Nkind (N) = N_Task_Type_Declaration then
1587 Def := Task_Definition (N);
1588
1589 if No (Def) then
1590 Def :=
1591 Make_Task_Definition (Sloc (N),
1592 Visible_Declarations => New_List,
1593 End_Label => Empty);
1594
1595 Set_Task_Definition (N, Def);
1596 end if;
1597
1598 Decls := Visible_Declarations (Def);
1599
1600 if No (Decls) then
1601 Decls := New_List;
1602 Set_Visible_Declarations (Def, Decls);
1603 end if;
1604
1605 Prepend_To (Decls, Prag);
1606
1607 -- When the context is a library unit, the pragma is added to the
1608 -- Pragmas_After list.
1609
1610 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1611 Aux := Aux_Decls_Node (Parent (N));
1612
1613 if No (Pragmas_After (Aux)) then
1614 Set_Pragmas_After (Aux, New_List);
1615 end if;
1616
1617 Prepend (Prag, Pragmas_After (Aux));
1618
1619 -- Default, the pragma is inserted after the context
1620
1621 else
1622 Insert_After (N, Prag);
1623 end if;
1624 end Insert_Pragma;
1625
1626 -------------------------
1627 -- Relocate_Expression --
1628 -------------------------
1629
1630 function Relocate_Expression (Source : Node_Id) return Node_Id is
1631 begin
1632 if Inside_A_Generic then
1633 return Source;
1634 else
1635 return Atree.Relocate_Node (Source);
1636 end if;
1637 end Relocate_Expression;
1638
1639 -- Local variables
1640
1641 Aspect : Node_Id;
1642 Aitem : Node_Id := Empty;
1643 Ent : Node_Id;
1644
1645 L : constant List_Id := Aspect_Specifications (N);
1646 pragma Assert (Present (L));
1647
1648 Ins_Node : Node_Id := N;
1649 -- Insert pragmas/attribute definition clause after this node when no
1650 -- delayed analysis is required.
1651
1652 -- Start of processing for Analyze_Aspect_Specifications
1653
1654 begin
1655 -- The general processing involves building an attribute definition
1656 -- clause or a pragma node that corresponds to the aspect. Then in order
1657 -- to delay the evaluation of this aspect to the freeze point, we attach
1658 -- the corresponding pragma/attribute definition clause to the aspect
1659 -- specification node, which is then placed in the Rep Item chain. In
1660 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1661 -- and we evaluate the rep item at the freeze point. When the aspect
1662 -- doesn't have a corresponding pragma/attribute definition clause, then
1663 -- its analysis is simply delayed at the freeze point.
1664
1665 -- Some special cases don't require delay analysis, thus the aspect is
1666 -- analyzed right now.
1667
1668 -- Note that there is a special handling for Pre, Post, Test_Case,
1669 -- Contract_Cases and Subprogram_Variant aspects. In these cases, we do
1670 -- not have to worry about delay issues, since the pragmas themselves
1671 -- deal with delay of visibility for the expression analysis. Thus, we
1672 -- just insert the pragma after the node N.
1673
1674 -- Loop through aspects
1675
1676 Aspect := First (L);
1677 Aspect_Loop : while Present (Aspect) loop
1678 Analyze_One_Aspect : declare
1679
1680 Aspect_Exit : exception;
1681 -- This exception is used to exit aspect processing completely. It
1682 -- is used when an error is detected, and no further processing is
1683 -- required. It is also used if an earlier error has left the tree
1684 -- in a state where the aspect should not be processed.
1685
1686 Expr : constant Node_Id := Expression (Aspect);
1687 Id : constant Node_Id := Identifier (Aspect);
1688 Loc : constant Source_Ptr := Sloc (Aspect);
1689 Nam : constant Name_Id := Chars (Id);
1690 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1691 Anod : Node_Id;
1692
1693 Delay_Required : Boolean;
1694 -- Set False if delay is not required
1695
1696 Eloc : Source_Ptr := No_Location;
1697 -- Source location of expression, modified when we split PPC's. It
1698 -- is set below when Expr is present.
1699
1700 procedure Analyze_Aspect_Convention;
1701 -- Perform analysis of aspect Convention
1702
1703 procedure Analyze_Aspect_Disable_Controlled;
1704 -- Perform analysis of aspect Disable_Controlled
1705
1706 procedure Analyze_Aspect_Export_Import;
1707 -- Perform analysis of aspects Export or Import
1708
1709 procedure Analyze_Aspect_External_Link_Name;
1710 -- Perform analysis of aspects External_Name or Link_Name
1711
1712 procedure Analyze_Aspect_Implicit_Dereference;
1713 -- Perform analysis of the Implicit_Dereference aspects
1714
1715 procedure Analyze_Aspect_Relaxed_Initialization;
1716 -- Perform analysis of aspect Relaxed_Initialization
1717
1718 procedure Analyze_Aspect_Yield;
1719 -- Perform analysis of aspect Yield
1720
1721 procedure Analyze_Aspect_Static;
1722 -- Ada 2022 (AI12-0075): Perform analysis of aspect Static
1723
1724 procedure Check_Expr_Is_OK_Static_Expression
1725 (Expr : Node_Id;
1726 Typ : Entity_Id := Empty);
1727 -- Check the specified expression Expr to make sure that it is a
1728 -- static expression of the given type (i.e. it will be analyzed
1729 -- and resolved using this type, which can be any valid argument
1730 -- to Resolve, e.g. Any_Integer is OK). If not, give an error
1731 -- and raise Aspect_Exit. If Typ is left Empty, then any static
1732 -- expression is allowed. Includes checking that the expression
1733 -- does not raise Constraint_Error.
1734
1735 function Directly_Specified
1736 (Id : Entity_Id; A : Aspect_Id) return Boolean;
1737 -- Returns True if the given aspect is directly (as opposed to
1738 -- via any form of inheritance) specified for the given entity.
1739
1740 function Make_Aitem_Pragma
1741 (Pragma_Argument_Associations : List_Id;
1742 Pragma_Name : Name_Id) return Node_Id;
1743 -- This is a wrapper for Make_Pragma used for converting aspects
1744 -- to pragmas. It takes care of Sloc (set from Loc) and building
1745 -- the pragma identifier from the given name. In addition the
1746 -- flags Class_Present and Split_PPC are set from the aspect
1747 -- node, as well as Is_Ignored. This routine also sets the
1748 -- From_Aspect_Specification in the resulting pragma node to
1749 -- True, and sets Corresponding_Aspect to point to the aspect.
1750 -- The resulting pragma is assigned to Aitem.
1751
1752 -------------------------------
1753 -- Analyze_Aspect_Convention --
1754 -------------------------------
1755
1756 procedure Analyze_Aspect_Convention is
1757 Conv : Node_Id;
1758 Dummy_1 : Node_Id;
1759 Dummy_2 : Node_Id;
1760 Dummy_3 : Node_Id;
1761 Expo : Node_Id;
1762 Imp : Node_Id;
1763
1764 begin
1765 -- Obtain all interfacing aspects that apply to the related
1766 -- entity.
1767
1768 Get_Interfacing_Aspects
1769 (Iface_Asp => Aspect,
1770 Conv_Asp => Dummy_1,
1771 EN_Asp => Dummy_2,
1772 Expo_Asp => Expo,
1773 Imp_Asp => Imp,
1774 LN_Asp => Dummy_3,
1775 Do_Checks => True);
1776
1777 -- The related entity is subject to aspect Export or Import.
1778 -- Do not process Convention now because it must be analysed
1779 -- as part of Export or Import.
1780
1781 if Present (Expo) or else Present (Imp) then
1782 return;
1783
1784 -- Otherwise Convention appears by itself
1785
1786 else
1787 -- The aspect specifies a particular convention
1788
1789 if Present (Expr) then
1790 Conv := New_Copy_Tree (Expr);
1791
1792 -- Otherwise assume convention Ada
1793
1794 else
1795 Conv := Make_Identifier (Loc, Name_Ada);
1796 end if;
1797
1798 -- Generate:
1799 -- pragma Convention (<Conv>, <E>);
1800
1801 Aitem := Make_Aitem_Pragma
1802 (Pragma_Name => Name_Convention,
1803 Pragma_Argument_Associations => New_List (
1804 Make_Pragma_Argument_Association (Loc,
1805 Expression => Conv),
1806 Make_Pragma_Argument_Association (Loc,
1807 Expression => New_Occurrence_Of (E, Loc))));
1808
1809 Decorate (Aspect, Aitem);
1810 Insert_Pragma (Aitem);
1811 end if;
1812 end Analyze_Aspect_Convention;
1813
1814 ---------------------------------------
1815 -- Analyze_Aspect_Disable_Controlled --
1816 ---------------------------------------
1817
1818 procedure Analyze_Aspect_Disable_Controlled is
1819 begin
1820 -- The aspect applies only to controlled records
1821
1822 if not (Ekind (E) = E_Record_Type
1823 and then Is_Controlled_Active (E))
1824 then
1825 Error_Msg_N
1826 ("aspect % requires controlled record type", Aspect);
1827 return;
1828 end if;
1829
1830 -- Preanalyze the expression (if any) when the aspect resides
1831 -- in a generic unit.
1832
1833 if Inside_A_Generic then
1834 if Present (Expr) then
1835 Preanalyze_And_Resolve (Expr, Any_Boolean);
1836 end if;
1837
1838 -- Otherwise the aspect resides in a nongeneric context
1839
1840 else
1841 -- A controlled record type loses its controlled semantics
1842 -- when the expression statically evaluates to True.
1843
1844 if Present (Expr) then
1845 Analyze_And_Resolve (Expr, Any_Boolean);
1846
1847 if Is_OK_Static_Expression (Expr) then
1848 if Is_True (Static_Boolean (Expr)) then
1849 Set_Disable_Controlled (E);
1850 end if;
1851
1852 -- Otherwise the expression is not static
1853
1854 else
1855 Error_Msg_N
1856 ("expression of aspect % must be static", Aspect);
1857 end if;
1858
1859 -- Otherwise the aspect appears without an expression and
1860 -- defaults to True.
1861
1862 else
1863 Set_Disable_Controlled (E);
1864 end if;
1865 end if;
1866 end Analyze_Aspect_Disable_Controlled;
1867
1868 ----------------------------------
1869 -- Analyze_Aspect_Export_Import --
1870 ----------------------------------
1871
1872 procedure Analyze_Aspect_Export_Import is
1873 Dummy_1 : Node_Id;
1874 Dummy_2 : Node_Id;
1875 Dummy_3 : Node_Id;
1876 Expo : Node_Id;
1877 Imp : Node_Id;
1878
1879 begin
1880 -- Obtain all interfacing aspects that apply to the related
1881 -- entity.
1882
1883 Get_Interfacing_Aspects
1884 (Iface_Asp => Aspect,
1885 Conv_Asp => Dummy_1,
1886 EN_Asp => Dummy_2,
1887 Expo_Asp => Expo,
1888 Imp_Asp => Imp,
1889 LN_Asp => Dummy_3,
1890 Do_Checks => True);
1891
1892 -- The related entity cannot be subject to both aspects Export
1893 -- and Import.
1894
1895 if Present (Expo) and then Present (Imp) then
1896 Error_Msg_N
1897 ("incompatible interfacing aspects given for &", E);
1898 Error_Msg_Sloc := Sloc (Expo);
1899 Error_Msg_N ("\aspect Export #", E);
1900 Error_Msg_Sloc := Sloc (Imp);
1901 Error_Msg_N ("\aspect Import #", E);
1902 end if;
1903
1904 -- A variable is most likely modified from the outside. Take
1905 -- the optimistic approach to avoid spurious errors.
1906
1907 if Ekind (E) = E_Variable then
1908 Set_Never_Set_In_Source (E, False);
1909 end if;
1910
1911 -- Resolve the expression of an Import or Export here, and
1912 -- require it to be of type Boolean and static. This is not
1913 -- quite right, because in general this should be delayed,
1914 -- but that seems tricky for these, because normally Boolean
1915 -- aspects are replaced with pragmas at the freeze point in
1916 -- Make_Pragma_From_Boolean_Aspect.
1917
1918 if No (Expr)
1919 or else Is_True (Static_Boolean (Expr))
1920 then
1921 if A_Id = Aspect_Import then
1922 Set_Has_Completion (E);
1923 Set_Is_Imported (E);
1924
1925 -- An imported object cannot be explicitly initialized
1926
1927 if Nkind (N) = N_Object_Declaration
1928 and then Present (Expression (N))
1929 then
1930 Error_Msg_N
1931 ("imported entities cannot be initialized "
1932 & "(RM B.1(24))", Expression (N));
1933 end if;
1934
1935 else
1936 pragma Assert (A_Id = Aspect_Export);
1937 Set_Is_Exported (E);
1938 end if;
1939
1940 -- Create the proper form of pragma Export or Import taking
1941 -- into account Conversion, External_Name, and Link_Name.
1942
1943 Aitem := Build_Export_Import_Pragma (Aspect, E);
1944
1945 -- Otherwise the expression is either False or erroneous. There
1946 -- is no corresponding pragma.
1947
1948 else
1949 Aitem := Empty;
1950 end if;
1951 end Analyze_Aspect_Export_Import;
1952
1953 ---------------------------------------
1954 -- Analyze_Aspect_External_Link_Name --
1955 ---------------------------------------
1956
1957 procedure Analyze_Aspect_External_Link_Name is
1958 Dummy_1 : Node_Id;
1959 Dummy_2 : Node_Id;
1960 Dummy_3 : Node_Id;
1961 Expo : Node_Id;
1962 Imp : Node_Id;
1963
1964 begin
1965 -- Obtain all interfacing aspects that apply to the related
1966 -- entity.
1967
1968 Get_Interfacing_Aspects
1969 (Iface_Asp => Aspect,
1970 Conv_Asp => Dummy_1,
1971 EN_Asp => Dummy_2,
1972 Expo_Asp => Expo,
1973 Imp_Asp => Imp,
1974 LN_Asp => Dummy_3,
1975 Do_Checks => True);
1976
1977 -- Ensure that aspect External_Name applies to aspect Export or
1978 -- Import.
1979
1980 if A_Id = Aspect_External_Name then
1981 if No (Expo) and then No (Imp) then
1982 Error_Msg_N
1983 ("aspect External_Name requires aspect Import or "
1984 & "Export", Aspect);
1985 end if;
1986
1987 -- Otherwise ensure that aspect Link_Name applies to aspect
1988 -- Export or Import.
1989
1990 else
1991 pragma Assert (A_Id = Aspect_Link_Name);
1992 if No (Expo) and then No (Imp) then
1993 Error_Msg_N
1994 ("aspect Link_Name requires aspect Import or Export",
1995 Aspect);
1996 end if;
1997 end if;
1998 end Analyze_Aspect_External_Link_Name;
1999
2000 -----------------------------------------
2001 -- Analyze_Aspect_Implicit_Dereference --
2002 -----------------------------------------
2003
2004 procedure Analyze_Aspect_Implicit_Dereference is
2005 begin
2006 if not Is_Type (E) or else not Has_Discriminants (E) then
2007 Error_Msg_N
2008 ("aspect must apply to a type with discriminants", Expr);
2009
2010 elsif not Is_Entity_Name (Expr) then
2011 Error_Msg_N
2012 ("aspect must name a discriminant of current type", Expr);
2013
2014 else
2015 -- Discriminant type be an anonymous access type or an
2016 -- anonymous access to subprogram.
2017
2018 -- Missing synchronized types???
2019
2020 declare
2021 Disc : Entity_Id := First_Discriminant (E);
2022 begin
2023 while Present (Disc) loop
2024 if Chars (Expr) = Chars (Disc)
2025 and then Ekind (Etype (Disc)) in
2026 E_Anonymous_Access_Subprogram_Type |
2027 E_Anonymous_Access_Type
2028 then
2029 Set_Has_Implicit_Dereference (E);
2030 Set_Has_Implicit_Dereference (Disc);
2031 exit;
2032 end if;
2033
2034 Next_Discriminant (Disc);
2035 end loop;
2036
2037 -- Error if no proper access discriminant
2038
2039 if Present (Disc) then
2040 -- For a type extension, check whether parent has
2041 -- a reference discriminant, to verify that use is
2042 -- proper.
2043
2044 if Is_Derived_Type (E)
2045 and then Has_Discriminants (Etype (E))
2046 then
2047 declare
2048 Parent_Disc : constant Entity_Id :=
2049 Get_Reference_Discriminant (Etype (E));
2050 begin
2051 if Present (Parent_Disc)
2052 and then Corresponding_Discriminant (Disc) /=
2053 Parent_Disc
2054 then
2055 Error_Msg_N
2056 ("reference discriminant does not match "
2057 & "discriminant of parent type", Expr);
2058 end if;
2059 end;
2060 end if;
2061
2062 else
2063 Error_Msg_NE
2064 ("not an access discriminant of&", Expr, E);
2065 end if;
2066 end;
2067 end if;
2068
2069 end Analyze_Aspect_Implicit_Dereference;
2070
2071 -------------------------------------------
2072 -- Analyze_Aspect_Relaxed_Initialization --
2073 -------------------------------------------
2074
2075 procedure Analyze_Aspect_Relaxed_Initialization is
2076 procedure Analyze_Relaxed_Parameter
2077 (Subp_Id : Entity_Id;
2078 Param : Node_Id;
2079 Seen : in out Elist_Id);
2080 -- Analyze parameter that appears in the expression of the
2081 -- aspect Relaxed_Initialization.
2082
2083 -------------------------------
2084 -- Analyze_Relaxed_Parameter --
2085 -------------------------------
2086
2087 procedure Analyze_Relaxed_Parameter
2088 (Subp_Id : Entity_Id;
2089 Param : Node_Id;
2090 Seen : in out Elist_Id)
2091 is
2092 begin
2093 -- Set name of the aspect for error messages
2094 Error_Msg_Name_1 := Nam;
2095
2096 -- The relaxed parameter is a formal parameter
2097
2098 if Nkind (Param) in N_Identifier | N_Expanded_Name then
2099 Analyze (Param);
2100
2101 declare
2102 Item : constant Entity_Id := Entity (Param);
2103 begin
2104 -- It must be a formal of the analyzed subprogram
2105
2106 if Scope (Item) = Subp_Id then
2107
2108 pragma Assert (Is_Formal (Item));
2109
2110 -- It must not have scalar or access type
2111
2112 if Is_Elementary_Type (Etype (Item)) then
2113 Error_Msg_N ("illegal aspect % item", Param);
2114 Error_Msg_N
2115 ("\item must not have elementary type", Param);
2116 end if;
2117
2118 -- Detect duplicated items
2119
2120 if Contains (Seen, Item) then
2121 Error_Msg_N ("duplicate aspect % item", Param);
2122 else
2123 Append_New_Elmt (Item, Seen);
2124 end if;
2125 else
2126 Error_Msg_N ("illegal aspect % item", Param);
2127 end if;
2128 end;
2129
2130 -- The relaxed parameter is the function's Result attribute
2131
2132 elsif Is_Attribute_Result (Param) then
2133 Analyze (Param);
2134
2135 declare
2136 Pref : constant Node_Id := Prefix (Param);
2137 begin
2138 if Present (Pref)
2139 and then
2140 Nkind (Pref) in N_Identifier | N_Expanded_Name
2141 and then
2142 Entity (Pref) = Subp_Id
2143 then
2144 -- Function result must not have scalar or access
2145 -- type.
2146
2147 if Is_Elementary_Type (Etype (Pref)) then
2148 Error_Msg_N ("illegal aspect % item", Param);
2149 Error_Msg_N
2150 ("\function result must not have elementary"
2151 & " type", Param);
2152 end if;
2153
2154 -- Detect duplicated items
2155
2156 if Contains (Seen, Subp_Id) then
2157 Error_Msg_N ("duplicate aspect % item", Param);
2158 else
2159 Append_New_Elmt (Entity (Pref), Seen);
2160 end if;
2161
2162 else
2163 Error_Msg_N ("illegal aspect % item", Param);
2164 end if;
2165 end;
2166 else
2167 Error_Msg_N ("illegal aspect % item", Param);
2168 end if;
2169 end Analyze_Relaxed_Parameter;
2170
2171 -- Local variables
2172
2173 Seen : Elist_Id := No_Elist;
2174 -- Items that appear in the relaxed initialization aspect
2175 -- expression of a subprogram; for detecting duplicates.
2176
2177 Restore_Scope : Boolean;
2178 -- Will be set to True if we need to restore the scope table
2179 -- after analyzing the aspect expression.
2180
2181 Prev_Id : Entity_Id;
2182
2183 -- Start of processing for Analyze_Aspect_Relaxed_Initialization
2184
2185 begin
2186 -- Set name of the aspect for error messages
2187 Error_Msg_Name_1 := Nam;
2188
2189 -- Annotation of a type; no aspect expression is allowed.
2190 -- For a private type, the aspect must be attached to the
2191 -- partial view.
2192 --
2193 -- ??? Once the exact rule for this aspect is ready, we will
2194 -- likely reject concurrent types, etc., so let's keep the code
2195 -- for types and variable separate.
2196
2197 if Is_First_Subtype (E) then
2198 Prev_Id := Incomplete_Or_Partial_View (E);
2199 if Present (Prev_Id) then
2200
2201 -- Aspect may appear on the full view of an incomplete
2202 -- type because the incomplete declaration cannot have
2203 -- any aspects.
2204
2205 if Ekind (Prev_Id) = E_Incomplete_Type then
2206 null;
2207 else
2208 Error_Msg_N ("aspect % must apply to partial view", N);
2209 end if;
2210
2211 elsif Present (Expr) then
2212 Error_Msg_N ("illegal aspect % expression", Expr);
2213 end if;
2214
2215 -- Annotation of a variable; no aspect expression is allowed
2216
2217 elsif Ekind (E) = E_Variable then
2218 if Present (Expr) then
2219 Error_Msg_N ("illegal aspect % expression", Expr);
2220 end if;
2221
2222 -- Annotation of a constant; no aspect expression is allowed.
2223 -- For a deferred constant, the aspect must be attached to the
2224 -- partial view.
2225
2226 elsif Ekind (E) = E_Constant then
2227 if Present (Incomplete_Or_Partial_View (E)) then
2228 Error_Msg_N
2229 ("aspect % must apply to deferred constant", N);
2230
2231 elsif Present (Expr) then
2232 Error_Msg_N ("illegal aspect % expression", Expr);
2233 end if;
2234
2235 -- Annotation of a subprogram; aspect expression is required
2236
2237 elsif Is_Subprogram_Or_Entry (E)
2238 or else Is_Generic_Subprogram (E)
2239 then
2240 if Present (Expr) then
2241
2242 -- If we analyze subprogram body that acts as its own
2243 -- spec, then the subprogram itself and its formals are
2244 -- already installed; otherwise, we need to install them,
2245 -- as they must be visible when analyzing the aspect
2246 -- expression.
2247
2248 if In_Open_Scopes (E) then
2249 Restore_Scope := False;
2250 else
2251 Restore_Scope := True;
2252 Push_Scope (E);
2253
2254 -- Only formals of the subprogram itself can appear
2255 -- in Relaxed_Initialization aspect expression, not
2256 -- formals of the enclosing generic unit. (This is
2257 -- different than in Precondition or Depends aspects,
2258 -- where both kinds of formals are allowed.)
2259
2260 Install_Formals (E);
2261 end if;
2262
2263 -- Aspect expression is either an aggregate with list of
2264 -- parameters (and possibly the Result attribute for a
2265 -- function).
2266
2267 if Nkind (Expr) = N_Aggregate then
2268
2269 -- Component associations in the aggregate must be a
2270 -- parameter name followed by a static boolean
2271 -- expression.
2272
2273 if Present (Component_Associations (Expr)) then
2274 declare
2275 Assoc : Node_Id :=
2276 First (Component_Associations (Expr));
2277 begin
2278 while Present (Assoc) loop
2279 if List_Length (Choices (Assoc)) = 1 then
2280 Analyze_Relaxed_Parameter
2281 (E, First (Choices (Assoc)), Seen);
2282
2283 if Inside_A_Generic then
2284 Preanalyze_And_Resolve
2285 (Expression (Assoc), Any_Boolean);
2286 else
2287 Analyze_And_Resolve
2288 (Expression (Assoc), Any_Boolean);
2289 end if;
2290
2291 if not Is_OK_Static_Expression
2292 (Expression (Assoc))
2293 then
2294 Error_Msg_Name_1 := Nam;
2295 Error_Msg_N
2296 ("expression of aspect % " &
2297 "must be static", Aspect);
2298 end if;
2299
2300 else
2301 Error_Msg_Name_1 := Nam;
2302 Error_Msg_N
2303 ("illegal aspect % expression", Expr);
2304 end if;
2305 Next (Assoc);
2306 end loop;
2307 end;
2308 end if;
2309
2310 -- Expressions of the aggregate are parameter names
2311
2312 if Present (Expressions (Expr)) then
2313 declare
2314 Param : Node_Id := First (Expressions (Expr));
2315
2316 begin
2317 while Present (Param) loop
2318 Analyze_Relaxed_Parameter (E, Param, Seen);
2319 Next (Param);
2320 end loop;
2321 end;
2322 end if;
2323
2324 -- Mark the aggregate expression itself as analyzed;
2325 -- its subexpressions were marked when they themselves
2326 -- were analyzed.
2327
2328 Set_Analyzed (Expr);
2329
2330 -- Otherwise, it is a single name of a subprogram
2331 -- parameter (or possibly the Result attribute for
2332 -- a function).
2333
2334 else
2335 Analyze_Relaxed_Parameter (E, Expr, Seen);
2336 end if;
2337
2338 if Restore_Scope then
2339 End_Scope;
2340 end if;
2341 else
2342 Error_Msg_N ("missing expression for aspect %", N);
2343 end if;
2344
2345 else
2346 Error_Msg_N ("inappropriate entity for aspect %", E);
2347 end if;
2348 end Analyze_Aspect_Relaxed_Initialization;
2349
2350 ---------------------------
2351 -- Analyze_Aspect_Static --
2352 ---------------------------
2353
2354 procedure Analyze_Aspect_Static is
2355 function Has_Convention_Intrinsic (L : List_Id) return Boolean;
2356 -- Return True if L contains a pragma argument association
2357 -- node representing a convention Intrinsic.
2358
2359 ------------------------------
2360 -- Has_Convention_Intrinsic --
2361 ------------------------------
2362
2363 function Has_Convention_Intrinsic
2364 (L : List_Id) return Boolean
2365 is
2366 Arg : Node_Id := First (L);
2367 begin
2368 while Present (Arg) loop
2369 if Nkind (Arg) = N_Pragma_Argument_Association
2370 and then Chars (Arg) = Name_Convention
2371 and then Chars (Expression (Arg)) = Name_Intrinsic
2372 then
2373 return True;
2374 end if;
2375
2376 Next (Arg);
2377 end loop;
2378
2379 return False;
2380 end Has_Convention_Intrinsic;
2381
2382 Is_Imported_Intrinsic : Boolean;
2383
2384 begin
2385 if Ada_Version < Ada_2022 then
2386 Error_Msg_Ada_2022_Feature ("aspect %", Sloc (Aspect));
2387 return;
2388 end if;
2389
2390 Is_Imported_Intrinsic := Is_Imported (E)
2391 and then
2392 Has_Convention_Intrinsic
2393 (Pragma_Argument_Associations (Import_Pragma (E)));
2394
2395 -- The aspect applies only to expression functions that
2396 -- statisfy the requirements for a static expression function
2397 -- (such as having an expression that is predicate-static) as
2398 -- well as Intrinsic imported functions as a -gnatX extension.
2399
2400 if not Is_Expression_Function (E)
2401 and then
2402 not (All_Extensions_Allowed and then Is_Imported_Intrinsic)
2403 then
2404 if All_Extensions_Allowed then
2405 Error_Msg_N
2406 ("aspect % requires intrinsic or expression function",
2407 Aspect);
2408
2409 elsif Is_Imported_Intrinsic then
2410 Error_Msg_GNAT_Extension
2411 ("aspect % on intrinsic function", Sloc (Aspect),
2412 Is_Core_Extension => True);
2413
2414 else
2415 Error_Msg_N
2416 ("aspect % requires expression function", Aspect);
2417 end if;
2418
2419 return;
2420
2421 -- Ada 2022 (AI12-0075): Check that the function satisfies
2422 -- several requirements of static functions as specified in
2423 -- RM 6.8(5.1-5.8). Note that some of the requirements given
2424 -- there are checked elsewhere.
2425
2426 else
2427 -- The expression of the expression function must be a
2428 -- potentially static expression (RM 2022 6.8(3.2-3.4)).
2429 -- That's checked in Sem_Ch6.Analyze_Expression_Function.
2430
2431 -- The function must not contain any calls to itself, which
2432 -- is checked in Sem_Res.Resolve_Call.
2433
2434 -- Each formal must be of mode in and have a static subtype
2435
2436 declare
2437 Formal : Entity_Id := First_Formal (E);
2438 begin
2439 while Present (Formal) loop
2440 if Ekind (Formal) /= E_In_Parameter then
2441 Error_Msg_N
2442 ("aspect % requires formals of mode IN",
2443 Aspect);
2444
2445 return;
2446 end if;
2447
2448 if not Is_Static_Subtype (Etype (Formal)) then
2449 Error_Msg_N
2450 ("aspect % requires formals with static subtypes",
2451 Aspect);
2452
2453 return;
2454 end if;
2455
2456 Next_Formal (Formal);
2457 end loop;
2458 end;
2459
2460 -- The function's result subtype must be a static subtype
2461
2462 if not Is_Static_Subtype (Etype (E)) then
2463 Error_Msg_N
2464 ("aspect % requires function with result of "
2465 & "a static subtype",
2466 Aspect);
2467
2468 return;
2469 end if;
2470
2471 -- Check that the function does not have any applicable
2472 -- precondition or postcondition expression.
2473
2474 for Asp in Pre_Post_Aspects loop
2475 if Has_Aspect (E, Asp) then
2476 Error_Msg_Name_1 := Aspect_Names (Asp);
2477 Error_Msg_N
2478 ("aspect % is not allowed for a static "
2479 & "expression function",
2480 Find_Aspect (E, Asp));
2481
2482 return;
2483 end if;
2484 end loop;
2485
2486 -- ??? Must check that "for result type R, if the
2487 -- function is a boundary entity for type R (see 7.3.2),
2488 -- no type invariant applies to type R; if R has a
2489 -- component type C, a similar rule applies to C."
2490 end if;
2491
2492 -- When the expression is present, it must be static. If it
2493 -- evaluates to True, the expression function is treated as
2494 -- a static function. Otherwise the aspect appears without
2495 -- an expression and defaults to True.
2496
2497 if Present (Expr) then
2498 -- Preanalyze the expression when the aspect resides in a
2499 -- generic unit. (Is this generic-related code necessary
2500 -- for this aspect? It's modeled on what's done for aspect
2501 -- Disable_Controlled. ???)
2502
2503 if Inside_A_Generic then
2504 Preanalyze_And_Resolve (Expr, Any_Boolean);
2505
2506 -- Otherwise the aspect resides in a nongeneric context
2507
2508 else
2509 Analyze_And_Resolve (Expr, Any_Boolean);
2510
2511 -- Error if the boolean expression is not static
2512
2513 if not Is_OK_Static_Expression (Expr) then
2514 Error_Msg_N
2515 ("expression of aspect % must be static", Aspect);
2516 end if;
2517 end if;
2518 end if;
2519 end Analyze_Aspect_Static;
2520
2521 --------------------------
2522 -- Analyze_Aspect_Yield --
2523 --------------------------
2524
2525 procedure Analyze_Aspect_Yield is
2526 Expr_Value : Boolean := False;
2527
2528 begin
2529 -- Check valid entity for 'Yield
2530
2531 if (Is_Subprogram (E)
2532 or else Is_Generic_Subprogram (E)
2533 or else Is_Entry (E))
2534 and then not Within_Protected_Type (E)
2535 then
2536 null;
2537
2538 elsif Within_Protected_Type (E) then
2539 Error_Msg_N
2540 ("aspect% not applicable to protected operation", Id);
2541 return;
2542
2543 else
2544 Error_Msg_N
2545 ("aspect% only applicable to subprogram and entry "
2546 & "declarations", Id);
2547 return;
2548 end if;
2549
2550 -- Evaluate its static expression (if available); otherwise it
2551 -- defaults to True.
2552
2553 if No (Expr) then
2554 Expr_Value := True;
2555
2556 -- Otherwise it must have a static boolean expression
2557
2558 else
2559 if Inside_A_Generic then
2560 Preanalyze_And_Resolve (Expr, Any_Boolean);
2561 else
2562 Analyze_And_Resolve (Expr, Any_Boolean);
2563 end if;
2564
2565 if Is_OK_Static_Expression (Expr) then
2566 if Is_True (Static_Boolean (Expr)) then
2567 Expr_Value := True;
2568 end if;
2569 else
2570 Error_Msg_N
2571 ("expression of aspect % must be static", Aspect);
2572 end if;
2573 end if;
2574
2575 if Expr_Value then
2576 Set_Has_Yield_Aspect (E);
2577 end if;
2578
2579 -- If the Yield aspect is specified for a dispatching
2580 -- subprogram that inherits the aspect, the specified
2581 -- value shall be confirming.
2582
2583 if Present (Expr)
2584 and then Is_Dispatching_Operation (E)
2585 and then Present (Overridden_Operation (E))
2586 and then Has_Yield_Aspect (Overridden_Operation (E))
2587 /= Is_True (Static_Boolean (Expr))
2588 then
2589 Error_Msg_N ("specification of inherited aspect% can only " &
2590 "confirm parent value", Id);
2591 end if;
2592 end Analyze_Aspect_Yield;
2593
2594 ----------------------------------------
2595 -- Check_Expr_Is_OK_Static_Expression --
2596 ----------------------------------------
2597
2598 procedure Check_Expr_Is_OK_Static_Expression
2599 (Expr : Node_Id;
2600 Typ : Entity_Id := Empty)
2601 is
2602 begin
2603 if Present (Typ) then
2604 Analyze_And_Resolve (Expr, Typ);
2605 else
2606 Analyze_And_Resolve (Expr);
2607 end if;
2608
2609 -- An expression cannot be considered static if its resolution
2610 -- failed or if it's erroneous. Stop the analysis of the
2611 -- related aspect.
2612
2613 if Etype (Expr) = Any_Type or else Error_Posted (Expr) then
2614 raise Aspect_Exit;
2615
2616 elsif Is_OK_Static_Expression (Expr) then
2617 return;
2618
2619 -- Finally, we have a real error
2620
2621 else
2622 Error_Msg_Name_1 := Nam;
2623 Flag_Non_Static_Expr
2624 ("entity for aspect% must be a static expression",
2625 Expr);
2626 raise Aspect_Exit;
2627 end if;
2628 end Check_Expr_Is_OK_Static_Expression;
2629
2630 ------------------------
2631 -- Directly_Specified --
2632 ------------------------
2633
2634 function Directly_Specified
2635 (Id : Entity_Id; A : Aspect_Id) return Boolean
2636 is
2637 Aspect_Spec : constant Node_Id := Find_Aspect (Id, A);
2638 begin
2639 return Present (Aspect_Spec) and then Entity (Aspect_Spec) = Id;
2640 end Directly_Specified;
2641
2642 -----------------------
2643 -- Make_Aitem_Pragma --
2644 -----------------------
2645
2646 function Make_Aitem_Pragma
2647 (Pragma_Argument_Associations : List_Id;
2648 Pragma_Name : Name_Id) return Node_Id
2649 is
2650 Args : List_Id := Pragma_Argument_Associations;
2651 Aitem : Node_Id;
2652
2653 begin
2654 -- We should never get here if aspect was disabled
2655
2656 pragma Assert (not Is_Disabled (Aspect));
2657
2658 -- Certain aspects allow for an optional name or expression. Do
2659 -- not generate a pragma with empty argument association list.
2660
2661 if No (Args) or else No (Expression (First (Args))) then
2662 Args := No_List;
2663 end if;
2664
2665 -- Build the pragma
2666
2667 Aitem :=
2668 Make_Pragma (Loc,
2669 Pragma_Argument_Associations => Args,
2670 Pragma_Identifier =>
2671 Make_Identifier (Sloc (Id), Pragma_Name),
2672 Class_Present => Class_Present (Aspect),
2673 Split_PPC => Split_PPC (Aspect));
2674
2675 -- Set additional semantic fields
2676
2677 if Is_Ignored (Aspect) then
2678 Set_Is_Ignored (Aitem);
2679 elsif Is_Checked (Aspect) then
2680 Set_Is_Checked (Aitem);
2681 end if;
2682
2683 Set_Corresponding_Aspect (Aitem, Aspect);
2684 Set_From_Aspect_Specification (Aitem);
2685
2686 return Aitem;
2687 end Make_Aitem_Pragma;
2688
2689 -- Start of processing for Analyze_One_Aspect
2690
2691 begin
2692 -- Skip aspect if already analyzed, to avoid looping in some cases
2693
2694 if Analyzed (Aspect) then
2695 goto Continue;
2696 end if;
2697
2698 -- Skip looking at aspect if it is totally disabled. Just mark it
2699 -- as such for later reference in the tree. This also sets the
2700 -- Is_Ignored and Is_Checked flags appropriately.
2701
2702 Check_Applicable_Policy (Aspect);
2703
2704 if Is_Disabled (Aspect) then
2705 goto Continue;
2706 end if;
2707
2708 -- Set the source location of expression, used in the case of
2709 -- a failed precondition/postcondition or invariant. Note that
2710 -- the source location of the expression is not usually the best
2711 -- choice here. For example, it gets located on the last AND
2712 -- keyword in a chain of boolean expressiond AND'ed together.
2713 -- It is best to put the message on the first character of the
2714 -- assertion, which is the effect of the First_Node call here.
2715
2716 if Present (Expr) then
2717 Eloc := Sloc (First_Node (Expr));
2718 end if;
2719
2720 -- Check restriction No_Implementation_Aspect_Specifications
2721
2722 if Implementation_Defined_Aspect (A_Id) then
2723 Check_Restriction
2724 (No_Implementation_Aspect_Specifications, Aspect);
2725 end if;
2726
2727 -- Check restriction No_Specification_Of_Aspect
2728
2729 Check_Restriction_No_Specification_Of_Aspect (Aspect);
2730
2731 -- Mark aspect analyzed (actual analysis is delayed till later)
2732
2733 Set_Analyzed (Aspect);
2734 Set_Entity (Aspect, E);
2735
2736 -- Build the reference to E that will be used in the built pragmas
2737
2738 Ent := New_Occurrence_Of (E, Sloc (Id));
2739
2740 if A_Id in Aspect_Attach_Handler | Aspect_Interrupt_Handler then
2741
2742 -- Treat the specification as a reference to the protected
2743 -- operation, which might otherwise appear unreferenced and
2744 -- generate spurious warnings.
2745
2746 Generate_Reference (E, Id);
2747 end if;
2748
2749 -- Check for duplicate aspect. Note that the Comes_From_Source
2750 -- test allows duplicate Pre/Post's that we generate internally
2751 -- to escape being flagged here.
2752
2753 if No_Duplicates_Allowed (A_Id) then
2754 Anod := First (L);
2755 while Anod /= Aspect loop
2756 if Comes_From_Source (Aspect)
2757 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
2758 then
2759 Error_Msg_Name_1 := Nam;
2760 Error_Msg_Sloc := Sloc (Anod);
2761
2762 -- Case of same aspect specified twice
2763
2764 if Class_Present (Anod) = Class_Present (Aspect) then
2765 if not Class_Present (Anod) then
2766 Error_Msg_NE
2767 ("aspect% for & previously given#",
2768 Id, E);
2769 else
2770 Error_Msg_NE
2771 ("aspect `%''Class` for & previously given#",
2772 Id, E);
2773 end if;
2774 end if;
2775 end if;
2776
2777 Next (Anod);
2778 end loop;
2779 end if;
2780
2781 -- Check some general restrictions on language defined aspects
2782
2783 if not Implementation_Defined_Aspect (A_Id)
2784 or else A_Id in Aspect_Async_Readers
2785 | Aspect_Async_Writers
2786 | Aspect_Effective_Reads
2787 | Aspect_Effective_Writes
2788 | Aspect_Preelaborable_Initialization
2789 then
2790 Error_Msg_Name_1 := Nam;
2791
2792 -- Not allowed for renaming declarations. Examine the original
2793 -- node because a subprogram renaming may have been rewritten
2794 -- as a body.
2795
2796 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
2797 Error_Msg_N
2798 ("aspect % not allowed for renaming declaration",
2799 Aspect);
2800 end if;
2801
2802 -- Not allowed for formal type declarations in previous
2803 -- versions of the language. Allowed for them only for
2804 -- shared variable control aspects.
2805
2806 -- Original node is used in case expansion rewrote the node -
2807 -- as is the case with generic derived types.
2808
2809 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
2810 if Ada_Version < Ada_2022 then
2811 Error_Msg_N
2812 ("aspect % not allowed for formal type declaration",
2813 Aspect);
2814
2815 elsif A_Id not in Aspect_Atomic
2816 | Aspect_Volatile
2817 | Aspect_Independent
2818 | Aspect_Atomic_Components
2819 | Aspect_Independent_Components
2820 | Aspect_Volatile_Components
2821 | Aspect_Async_Readers
2822 | Aspect_Async_Writers
2823 | Aspect_Effective_Reads
2824 | Aspect_Effective_Writes
2825 | Aspect_Preelaborable_Initialization
2826 then
2827 Error_Msg_N
2828 ("aspect % not allowed for formal type declaration",
2829 Aspect);
2830 end if;
2831 end if;
2832 end if;
2833
2834 -- Copy expression for later processing by the procedures
2835 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2836
2837 -- The expression may be a subprogram name, and can
2838 -- be an operator name that appears as a string, but
2839 -- requires its own analysis procedure (see sem_ch6).
2840
2841 if Nkind (Expr) = N_Operator_Symbol then
2842 Set_Entity (Id, Expr);
2843 else
2844 Set_Entity (Id, New_Copy_Tree (Expr));
2845 end if;
2846
2847 -- Set Delay_Required as appropriate to aspect
2848
2849 case Aspect_Delay (A_Id) is
2850 when Always_Delay =>
2851 -- For Boolean aspects, do not delay if no expression
2852
2853 if A_Id in Boolean_Aspects | Library_Unit_Aspects then
2854 Delay_Required := Present (Expr);
2855 else
2856 Delay_Required := True;
2857 end if;
2858
2859 when Never_Delay =>
2860 Delay_Required := False;
2861
2862 when Rep_Aspect =>
2863
2864 -- For Boolean aspects, do not delay if no expression except
2865 -- for Full_Access_Only because we need to process it after
2866 -- Volatile and Atomic, which can be independently delayed.
2867
2868 if A_Id in Boolean_Aspects
2869 and then A_Id /= Aspect_Full_Access_Only
2870 and then No (Expr)
2871 then
2872 Delay_Required := False;
2873
2874 -- For non-Boolean aspects, if the expression has the form
2875 -- of an integer literal, then do not delay, since we know
2876 -- the value cannot change. This optimization catches most
2877 -- rep clause cases.
2878
2879 elsif A_Id not in Boolean_Aspects
2880 and then Present (Expr)
2881 and then Nkind (Expr) = N_Integer_Literal
2882 then
2883 Delay_Required := False;
2884
2885 -- For Alignment and various Size aspects, do not delay for
2886 -- an attribute reference whose prefix is Standard, for
2887 -- example Standard'Maximum_Alignment or Standard'Word_Size.
2888
2889 elsif A_Id in Aspect_Alignment
2890 | Aspect_Component_Size
2891 | Aspect_Object_Size
2892 | Aspect_Size
2893 | Aspect_Value_Size
2894 and then Present (Expr)
2895 and then Nkind (Expr) = N_Attribute_Reference
2896 and then Nkind (Prefix (Expr)) = N_Identifier
2897 and then Chars (Prefix (Expr)) = Name_Standard
2898 then
2899 Delay_Required := False;
2900
2901 -- All other cases are delayed
2902
2903 else
2904 Delay_Required := True;
2905 Set_Has_Delayed_Rep_Aspects (E);
2906 end if;
2907 end case;
2908
2909 if Delay_Required
2910
2911 and then (A_Id = Aspect_Stable_Properties
2912 or else A_Id = Aspect_Designated_Storage_Model
2913 or else A_Id = Aspect_Storage_Model_Type)
2914 -- ??? It seems like we should do this for all aspects, not
2915 -- just these, but that causes as-yet-undiagnosed regressions.
2916
2917 then
2918 Set_Has_Delayed_Aspects (E);
2919 Set_Is_Delayed_Aspect (Aspect);
2920 end if;
2921
2922 -- Check 13.1(9.2/5): A representation aspect of a subtype or type
2923 -- shall not be specified (whether by a representation item or an
2924 -- aspect_specification) before the type is completely defined
2925 -- (see 3.11.1).
2926
2927 if Is_Representation_Aspect (A_Id)
2928 and then Rep_Item_Too_Early (E, N)
2929 then
2930 goto Continue;
2931 end if;
2932
2933 -- Processing based on specific aspect
2934
2935 case A_Id is
2936 when Aspect_Unimplemented =>
2937 null; -- ??? temp for now
2938
2939 -- No_Aspect should be impossible
2940
2941 when No_Aspect =>
2942 raise Program_Error;
2943
2944 -- Case 1: Aspects corresponding to attribute definition
2945 -- clauses.
2946
2947 when Aspect_Address
2948 | Aspect_Alignment
2949 | Aspect_Bit_Order
2950 | Aspect_Component_Size
2951 | Aspect_Constant_Indexing
2952 | Aspect_Default_Iterator
2953 | Aspect_Dispatching_Domain
2954 | Aspect_External_Tag
2955 | Aspect_Input
2956 | Aspect_Iterable
2957 | Aspect_Iterator_Element
2958 | Aspect_Machine_Radix
2959 | Aspect_Object_Size
2960 | Aspect_Output
2961 | Aspect_Put_Image
2962 | Aspect_Read
2963 | Aspect_Scalar_Storage_Order
2964 | Aspect_Simple_Storage_Pool
2965 | Aspect_Size
2966 | Aspect_Small
2967 | Aspect_Storage_Pool
2968 | Aspect_Stream_Size
2969 | Aspect_Value_Size
2970 | Aspect_Variable_Indexing
2971 | Aspect_Write
2972 =>
2973 -- Indexing aspects apply only to tagged type
2974
2975 if A_Id in Aspect_Constant_Indexing
2976 | Aspect_Variable_Indexing
2977 and then not (Is_Type (E)
2978 and then Is_Tagged_Type (E))
2979 then
2980 Error_Msg_N
2981 ("indexing aspect can only apply to a tagged type",
2982 Aspect);
2983 goto Continue;
2984 end if;
2985
2986 -- For the case of aspect Address, we don't consider that we
2987 -- know the entity is never set in the source, since it is
2988 -- is likely aliasing is occurring.
2989
2990 -- Note: one might think that the analysis of the resulting
2991 -- attribute definition clause would take care of that, but
2992 -- that's not the case since it won't be from source.
2993
2994 if A_Id = Aspect_Address then
2995 Set_Never_Set_In_Source (E, False);
2996 end if;
2997
2998 -- Correctness of the profile of a stream operation is
2999 -- verified at the freeze point, but we must detect the
3000 -- illegal specification of this aspect for a subtype now,
3001 -- to prevent malformed rep_item chains.
3002
3003 if A_Id in Aspect_Input
3004 | Aspect_Output
3005 | Aspect_Read
3006 | Aspect_Write
3007 then
3008 if not Is_First_Subtype (E) then
3009 Error_Msg_N
3010 ("local name must be a first subtype", Aspect);
3011 goto Continue;
3012
3013 -- If stream aspect applies to the class-wide type,
3014 -- the generated attribute definition applies to the
3015 -- class-wide type as well.
3016
3017 elsif Class_Present (Aspect) then
3018 Ent :=
3019 Make_Attribute_Reference (Loc,
3020 Prefix => Ent,
3021 Attribute_Name => Name_Class);
3022 end if;
3023 end if;
3024
3025 -- Construct the attribute_definition_clause. The expression
3026 -- in the aspect specification is simply shared with the
3027 -- constructed attribute, because it will be fully analyzed
3028 -- when the attribute is processed.
3029
3030 Aitem :=
3031 Make_Attribute_Definition_Clause (Loc,
3032 Name => Ent,
3033 Chars => Nam,
3034 Expression => Relocate_Expression (Expr));
3035
3036 -- If the address is specified, then we treat the entity as
3037 -- referenced, to avoid spurious warnings. This is analogous
3038 -- to what is done with an attribute definition clause, but
3039 -- here we don't want to generate a reference because this
3040 -- is the point of definition of the entity.
3041
3042 if A_Id = Aspect_Address then
3043 Set_Referenced (E);
3044 end if;
3045
3046 -- Case 2: Aspects corresponding to pragmas
3047
3048 -- Case 2a: Aspects corresponding to pragmas with two
3049 -- arguments, where the first argument is a local name
3050 -- referring to the entity, and the second argument is the
3051 -- aspect definition expression.
3052
3053 -- Linker_Section
3054
3055 when Aspect_Linker_Section =>
3056 Aitem := Make_Aitem_Pragma
3057 (Pragma_Argument_Associations => New_List (
3058 Make_Pragma_Argument_Association (Loc,
3059 Expression => New_Occurrence_Of (E, Loc)),
3060 Make_Pragma_Argument_Association (Sloc (Expr),
3061 Expression => Relocate_Node (Expr))),
3062 Pragma_Name => Name_Linker_Section);
3063
3064 -- Linker_Section does not need delaying, as its argument
3065 -- must be a static string. Furthermore, if applied to
3066 -- an object with an explicit initialization, the object
3067 -- must be frozen in order to elaborate the initialization
3068 -- code. (This is already done for types with implicit
3069 -- initialization, such as protected types.)
3070
3071 if Nkind (N) = N_Object_Declaration
3072 and then Has_Init_Expression (N)
3073 then
3074 Delay_Required := False;
3075 end if;
3076
3077 -- Synchronization
3078
3079 -- Corresponds to pragma Implemented, construct the pragma
3080
3081 when Aspect_Synchronization =>
3082 Aitem := Make_Aitem_Pragma
3083 (Pragma_Argument_Associations => New_List (
3084 Make_Pragma_Argument_Association (Loc,
3085 Expression => New_Occurrence_Of (E, Loc)),
3086 Make_Pragma_Argument_Association (Sloc (Expr),
3087 Expression => Relocate_Node (Expr))),
3088 Pragma_Name => Name_Implemented);
3089
3090 -- Attach_Handler
3091
3092 when Aspect_Attach_Handler =>
3093 Aitem := Make_Aitem_Pragma
3094 (Pragma_Argument_Associations => New_List (
3095 Make_Pragma_Argument_Association (Sloc (Ent),
3096 Expression => Ent),
3097 Make_Pragma_Argument_Association (Sloc (Expr),
3098 Expression => Relocate_Expression (Expr))),
3099 Pragma_Name => Name_Attach_Handler);
3100
3101 -- We need to insert this pragma into the tree to get proper
3102 -- processing and to look valid from a placement viewpoint.
3103
3104 Insert_Pragma (Aitem);
3105 goto Continue;
3106
3107 -- Dynamic_Predicate, Predicate, Static_Predicate
3108
3109 when Aspect_Dynamic_Predicate
3110 | Aspect_Predicate
3111 | Aspect_Static_Predicate
3112 =>
3113 -- These aspects apply only to subtypes
3114
3115 if not Is_Type (E) then
3116 Error_Msg_N
3117 ("predicate can only be specified for a subtype",
3118 Aspect);
3119 goto Continue;
3120
3121 elsif Is_Incomplete_Type (E) then
3122 Error_Msg_N
3123 ("predicate cannot apply to incomplete view", Aspect);
3124
3125 elsif Is_Generic_Type (E) then
3126 Error_Msg_N
3127 ("predicate cannot apply to formal type", Aspect);
3128 goto Continue;
3129 end if;
3130
3131 -- Construct the pragma (always a pragma Predicate, with
3132 -- flags recording whether it is static/dynamic). We also
3133 -- set flags recording this in the type itself.
3134
3135 Aitem := Make_Aitem_Pragma
3136 (Pragma_Argument_Associations => New_List (
3137 Make_Pragma_Argument_Association (Sloc (Ent),
3138 Expression => Ent),
3139 Make_Pragma_Argument_Association (Sloc (Expr),
3140 Expression => Relocate_Expression (Expr))),
3141 Pragma_Name => Name_Predicate);
3142
3143 -- Mark type has predicates, and remember what kind of
3144 -- aspect lead to this predicate (we need this to access
3145 -- the right set of check policies later on).
3146
3147 Set_Has_Predicates (E);
3148
3149 if A_Id = Aspect_Dynamic_Predicate then
3150 Set_Has_Dynamic_Predicate_Aspect (E);
3151
3152 -- If the entity has a dynamic predicate, any inherited
3153 -- static predicate becomes dynamic as well, and the
3154 -- predicate function includes the conjunction of both.
3155
3156 Set_Has_Static_Predicate_Aspect (E, False);
3157
3158 elsif A_Id = Aspect_Static_Predicate then
3159 Set_Has_Static_Predicate_Aspect (E);
3160 end if;
3161
3162 -- If the type is private, indicate that its completion
3163 -- has a freeze node, because that is the one that will
3164 -- be visible at freeze time.
3165
3166 if Is_Private_Type (E) and then Present (Full_View (E)) then
3167 Set_Has_Predicates (Full_View (E));
3168
3169 if A_Id = Aspect_Dynamic_Predicate then
3170 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
3171 elsif A_Id = Aspect_Static_Predicate then
3172 Set_Has_Static_Predicate_Aspect (Full_View (E));
3173 end if;
3174
3175 Set_Has_Delayed_Aspects (Full_View (E));
3176 Ensure_Freeze_Node (Full_View (E));
3177
3178 -- If there is an Underlying_Full_View, also create a
3179 -- freeze node for that one.
3180
3181 if Is_Private_Type (Full_View (E)) then
3182 declare
3183 U_Full : constant Entity_Id :=
3184 Underlying_Full_View (Full_View (E));
3185 begin
3186 if Present (U_Full) then
3187 Set_Has_Delayed_Aspects (U_Full);
3188 Ensure_Freeze_Node (U_Full);
3189 end if;
3190 end;
3191 end if;
3192 end if;
3193
3194 -- Predicate_Failure
3195
3196 when Aspect_Predicate_Failure =>
3197
3198 -- This aspect applies only to subtypes
3199
3200 if not Is_Type (E) then
3201 Error_Msg_N
3202 ("predicate can only be specified for a subtype",
3203 Aspect);
3204 goto Continue;
3205
3206 elsif Is_Incomplete_Type (E) then
3207 Error_Msg_N
3208 ("predicate cannot apply to incomplete view", Aspect);
3209 goto Continue;
3210
3211 elsif not Has_Predicates (E) then
3212 Error_Msg_N
3213 ("Predicate_Failure requires previous predicate" &
3214 " specification", Aspect);
3215 goto Continue;
3216
3217 elsif not (Directly_Specified (E, Aspect_Dynamic_Predicate)
3218 or else Directly_Specified (E, Aspect_Static_Predicate)
3219 or else Directly_Specified (E, Aspect_Predicate))
3220 then
3221 Error_Msg_N
3222 ("Predicate_Failure requires accompanying" &
3223 " noninherited predicate specification", Aspect);
3224 goto Continue;
3225 end if;
3226
3227 -- Construct the pragma
3228
3229 Aitem := Make_Aitem_Pragma
3230 (Pragma_Argument_Associations => New_List (
3231 Make_Pragma_Argument_Association (Sloc (Ent),
3232 Expression => Ent),
3233 Make_Pragma_Argument_Association (Sloc (Expr),
3234 Expression => Relocate_Node (Expr))),
3235 Pragma_Name => Name_Predicate_Failure);
3236
3237 -- Case 2b: Aspects corresponding to pragmas with two
3238 -- arguments, where the second argument is a local name
3239 -- referring to the entity, and the first argument is the
3240 -- aspect definition expression.
3241
3242 -- Convention
3243
3244 when Aspect_Convention =>
3245 Analyze_Aspect_Convention;
3246 goto Continue;
3247
3248 -- External_Name, Link_Name
3249
3250 when Aspect_External_Name
3251 | Aspect_Link_Name
3252 =>
3253 Analyze_Aspect_External_Link_Name;
3254 goto Continue;
3255
3256 -- CPU, Interrupt_Priority, Priority
3257
3258 -- These three aspects can be specified for a subprogram spec
3259 -- or body, in which case we analyze the expression and export
3260 -- the value of the aspect.
3261
3262 -- Previously, we generated an equivalent pragma for bodies
3263 -- (note that the specs cannot contain these pragmas). The
3264 -- pragma was inserted ahead of local declarations, rather than
3265 -- after the body. This leads to a certain duplication between
3266 -- the processing performed for the aspect and the pragma, but
3267 -- given the straightforward handling required it is simpler
3268 -- to duplicate than to translate the aspect in the spec into
3269 -- a pragma in the declarative part of the body.
3270
3271 when Aspect_CPU
3272 | Aspect_Interrupt_Priority
3273 | Aspect_Priority
3274 =>
3275 -- Verify the expression is static when Static_Priorities is
3276 -- enabled.
3277
3278 if not Is_OK_Static_Expression (Expr) then
3279 Check_Restriction (Static_Priorities, Expr);
3280 end if;
3281
3282 if Nkind (N) in N_Subprogram_Body | N_Subprogram_Declaration
3283 then
3284 -- Analyze the aspect expression
3285
3286 Analyze_And_Resolve (Expr, Standard_Integer);
3287
3288 -- Interrupt_Priority aspect not allowed for main
3289 -- subprograms. RM D.1 does not forbid this explicitly,
3290 -- but RM J.15.11(6/3) does not permit pragma
3291 -- Interrupt_Priority for subprograms.
3292
3293 if A_Id = Aspect_Interrupt_Priority then
3294 Error_Msg_N
3295 ("Interrupt_Priority aspect cannot apply to "
3296 & "subprogram", Expr);
3297
3298 -- The expression must be static
3299
3300 elsif not Is_OK_Static_Expression (Expr) then
3301 Flag_Non_Static_Expr
3302 ("aspect requires static expression!", Expr);
3303
3304 -- Check whether this is the main subprogram. Issue a
3305 -- warning only if it is obviously not a main program
3306 -- (when it has parameters or when the subprogram is
3307 -- within a package).
3308
3309 elsif Present (Parameter_Specifications
3310 (Specification (N)))
3311 or else not Is_Compilation_Unit (Defining_Entity (N))
3312 then
3313 -- See RM D.1(14/3) and D.16(12/3)
3314
3315 Error_Msg_N
3316 ("aspect applied to subprogram other than the "
3317 & "main subprogram has no effect??", Expr);
3318
3319 -- Otherwise check in range and export the value
3320
3321 -- For the CPU aspect
3322
3323 elsif A_Id = Aspect_CPU then
3324 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
3325
3326 -- Value is correct so we export the value to make
3327 -- it available at execution time.
3328
3329 Set_Main_CPU
3330 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3331
3332 else
3333 Error_Msg_N
3334 ("main subprogram 'C'P'U is out of range", Expr);
3335 end if;
3336
3337 -- For the Priority aspect
3338
3339 elsif A_Id = Aspect_Priority then
3340 if Is_In_Range (Expr, RTE (RE_Priority)) then
3341
3342 -- Value is correct so we export the value to make
3343 -- it available at execution time.
3344
3345 Set_Main_Priority
3346 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3347
3348 -- Ignore pragma if Relaxed_RM_Semantics to support
3349 -- other targets/non GNAT compilers.
3350
3351 elsif not Relaxed_RM_Semantics then
3352 Error_Msg_N
3353 ("main subprogram priority is out of range",
3354 Expr);
3355 end if;
3356 end if;
3357
3358 -- Load an arbitrary entity from System.Tasking.Stages
3359 -- or System.Tasking.Restricted.Stages (depending on
3360 -- the supported profile) to make sure that one of these
3361 -- packages is implicitly with'ed, since we need to have
3362 -- the tasking run time active for the pragma Priority to
3363 -- have any effect. Previously we with'ed the package
3364 -- System.Tasking, but this package does not trigger the
3365 -- required initialization of the run-time library.
3366
3367 if Restricted_Profile then
3368 Discard_Node (RTE (RE_Activate_Restricted_Tasks));
3369 else
3370 Discard_Node (RTE (RE_Activate_Tasks));
3371 end if;
3372
3373 -- Handling for these aspects in subprograms is complete
3374
3375 goto Continue;
3376
3377 -- For task and protected types pass the aspect as an
3378 -- attribute.
3379
3380 else
3381 Aitem :=
3382 Make_Attribute_Definition_Clause (Loc,
3383 Name => Ent,
3384 Chars => Nam,
3385 Expression => Relocate_Expression (Expr));
3386 end if;
3387
3388 -- Suppress/Unsuppress
3389
3390 when Aspect_Suppress
3391 | Aspect_Unsuppress
3392 =>
3393 Aitem := Make_Aitem_Pragma
3394 (Pragma_Argument_Associations => New_List (
3395 Make_Pragma_Argument_Association (Loc,
3396 Expression => Relocate_Node (Expr)),
3397 Make_Pragma_Argument_Association (Sloc (Expr),
3398 Expression => New_Occurrence_Of (E, Loc))),
3399 Pragma_Name => Nam);
3400
3401 Delay_Required := False;
3402
3403 -- Warnings
3404
3405 when Aspect_Warnings =>
3406 Aitem := Make_Aitem_Pragma
3407 (Pragma_Argument_Associations => New_List (
3408 Make_Pragma_Argument_Association (Sloc (Expr),
3409 Expression => Relocate_Node (Expr)),
3410 Make_Pragma_Argument_Association (Loc,
3411 Expression => New_Occurrence_Of (E, Loc))),
3412 Pragma_Name => Name_Warnings);
3413
3414 Decorate (Aspect, Aitem);
3415 Insert_Pragma (Aitem);
3416 goto Continue;
3417
3418 -- Case 2c: Aspects corresponding to pragmas with three
3419 -- arguments.
3420
3421 -- Invariant aspects have a first argument that references the
3422 -- entity, a second argument that is the expression and a third
3423 -- argument that is an appropriate message.
3424
3425 -- Invariant, Type_Invariant
3426
3427 when Aspect_Invariant
3428 | Aspect_Type_Invariant
3429 =>
3430 -- Analysis of the pragma will verify placement legality:
3431 -- an invariant must apply to a private type, or appear in
3432 -- the private part of a spec and apply to a completion.
3433
3434 Aitem := Make_Aitem_Pragma
3435 (Pragma_Argument_Associations => New_List (
3436 Make_Pragma_Argument_Association (Sloc (Ent),
3437 Expression => Ent),
3438 Make_Pragma_Argument_Association (Sloc (Expr),
3439 Expression => Relocate_Node (Expr))),
3440 Pragma_Name => Name_Invariant);
3441
3442 -- Add message unless exception messages are suppressed
3443
3444 if not Opt.Exception_Locations_Suppressed then
3445 Append_To (Pragma_Argument_Associations (Aitem),
3446 Make_Pragma_Argument_Association (Eloc,
3447 Chars => Name_Message,
3448 Expression =>
3449 Make_String_Literal (Eloc,
3450 Strval => "failed invariant from "
3451 & Build_Location_String (Eloc))));
3452 end if;
3453
3454 -- For Invariant case, insert immediately after the entity
3455 -- declaration. We do not have to worry about delay issues
3456 -- since the pragma processing takes care of this.
3457
3458 Delay_Required := False;
3459
3460 -- Case 2d : Aspects that correspond to a pragma with one
3461 -- argument.
3462
3463 -- Abstract_State
3464
3465 -- Aspect Abstract_State introduces implicit declarations for
3466 -- all state abstraction entities it defines. To emulate this
3467 -- behavior, insert the pragma at the beginning of the visible
3468 -- declarations of the related package so that it is analyzed
3469 -- immediately.
3470
3471 when Aspect_Abstract_State => Abstract_State : declare
3472 Context : Node_Id := N;
3473
3474 begin
3475 -- When aspect Abstract_State appears on a generic package,
3476 -- it is propagated to the package instance. The context in
3477 -- this case is the instance spec.
3478
3479 if Nkind (Context) = N_Package_Instantiation then
3480 Context := Instance_Spec (Context);
3481 end if;
3482
3483 if Nkind (Context) in N_Generic_Package_Declaration
3484 | N_Package_Declaration
3485 then
3486 Aitem := Make_Aitem_Pragma
3487 (Pragma_Argument_Associations => New_List (
3488 Make_Pragma_Argument_Association (Loc,
3489 Expression => Relocate_Node (Expr))),
3490 Pragma_Name => Name_Abstract_State);
3491
3492 Decorate (Aspect, Aitem);
3493 Insert_Pragma
3494 (Prag => Aitem,
3495 Is_Instance =>
3496 Is_Generic_Instance (Defining_Entity (Context)));
3497
3498 else
3499 Error_Msg_NE
3500 ("aspect & must apply to a package declaration",
3501 Aspect, Id);
3502 end if;
3503
3504 goto Continue;
3505 end Abstract_State;
3506
3507 -- Aspect Async_Readers is never delayed because it is
3508 -- equivalent to a source pragma which appears after the
3509 -- related object declaration.
3510
3511 when Aspect_Async_Readers =>
3512 Aitem := Make_Aitem_Pragma
3513 (Pragma_Argument_Associations => New_List (
3514 Make_Pragma_Argument_Association (Loc,
3515 Expression => Relocate_Node (Expr))),
3516 Pragma_Name => Name_Async_Readers);
3517
3518 Decorate (Aspect, Aitem);
3519 Insert_Pragma (Aitem);
3520 goto Continue;
3521
3522 -- Aspect Async_Writers is never delayed because it is
3523 -- equivalent to a source pragma which appears after the
3524 -- related object declaration.
3525
3526 when Aspect_Async_Writers =>
3527 Aitem := Make_Aitem_Pragma
3528 (Pragma_Argument_Associations => New_List (
3529 Make_Pragma_Argument_Association (Loc,
3530 Expression => Relocate_Node (Expr))),
3531 Pragma_Name => Name_Async_Writers);
3532
3533 Decorate (Aspect, Aitem);
3534 Insert_Pragma (Aitem);
3535 goto Continue;
3536
3537 -- Aspect Constant_After_Elaboration is never delayed because
3538 -- it is equivalent to a source pragma which appears after the
3539 -- related object declaration.
3540
3541 when Aspect_Constant_After_Elaboration =>
3542 Aitem := Make_Aitem_Pragma
3543 (Pragma_Argument_Associations => New_List (
3544 Make_Pragma_Argument_Association (Loc,
3545 Expression => Relocate_Node (Expr))),
3546 Pragma_Name =>
3547 Name_Constant_After_Elaboration);
3548
3549 Decorate (Aspect, Aitem);
3550 Insert_Pragma (Aitem);
3551 goto Continue;
3552
3553 -- Aspect Default_Internal_Condition is never delayed because
3554 -- it is equivalent to a source pragma which appears after the
3555 -- related private type. To deal with forward references, the
3556 -- generated pragma is stored in the rep chain of the related
3557 -- private type as types do not carry contracts. The pragma is
3558 -- wrapped inside of a procedure at the freeze point of the
3559 -- private type's full view.
3560
3561 -- A type entity argument is appended to facilitate inheriting
3562 -- the aspect from parent types (see Build_DIC_Procedure_Body),
3563 -- though that extra argument isn't documented for the pragma.
3564
3565 when Aspect_Default_Initial_Condition =>
3566 Aitem := Make_Aitem_Pragma
3567 (Pragma_Argument_Associations => New_List (
3568 Make_Pragma_Argument_Association (Loc,
3569 Expression => Relocate_Node (Expr)),
3570 Make_Pragma_Argument_Association (Sloc (Ent),
3571 Expression => Ent)),
3572 Pragma_Name =>
3573 Name_Default_Initial_Condition);
3574
3575 Decorate (Aspect, Aitem);
3576 Insert_Pragma (Aitem);
3577 goto Continue;
3578
3579 -- Default_Storage_Pool
3580
3581 when Aspect_Default_Storage_Pool =>
3582 Aitem := Make_Aitem_Pragma
3583 (Pragma_Argument_Associations => New_List (
3584 Make_Pragma_Argument_Association (Loc,
3585 Expression => Relocate_Node (Expr))),
3586 Pragma_Name =>
3587 Name_Default_Storage_Pool);
3588
3589 Decorate (Aspect, Aitem);
3590 Insert_Pragma (Aitem);
3591 goto Continue;
3592
3593 -- Depends
3594
3595 -- Aspect Depends is never delayed because it is equivalent to
3596 -- a source pragma which appears after the related subprogram.
3597 -- To deal with forward references, the generated pragma is
3598 -- stored in the contract of the related subprogram and later
3599 -- analyzed at the end of the declarative region. See routine
3600 -- Analyze_Depends_In_Decl_Part for details.
3601
3602 when Aspect_Depends =>
3603 Aitem := Make_Aitem_Pragma
3604 (Pragma_Argument_Associations => New_List (
3605 Make_Pragma_Argument_Association (Loc,
3606 Expression => Relocate_Node (Expr))),
3607 Pragma_Name => Name_Depends);
3608
3609 Decorate (Aspect, Aitem);
3610 Insert_Pragma (Aitem);
3611 goto Continue;
3612
3613 -- Aspect Effective_Reads is never delayed because it is
3614 -- equivalent to a source pragma which appears after the
3615 -- related object declaration.
3616
3617 when Aspect_Effective_Reads =>
3618 Aitem := Make_Aitem_Pragma
3619 (Pragma_Argument_Associations => New_List (
3620 Make_Pragma_Argument_Association (Loc,
3621 Expression => Relocate_Node (Expr))),
3622 Pragma_Name => Name_Effective_Reads);
3623
3624 Decorate (Aspect, Aitem);
3625 Insert_Pragma (Aitem);
3626 goto Continue;
3627
3628 -- Aspect Effective_Writes is never delayed because it is
3629 -- equivalent to a source pragma which appears after the
3630 -- related object declaration.
3631
3632 when Aspect_Effective_Writes =>
3633 Aitem := Make_Aitem_Pragma
3634 (Pragma_Argument_Associations => New_List (
3635 Make_Pragma_Argument_Association (Loc,
3636 Expression => Relocate_Node (Expr))),
3637 Pragma_Name => Name_Effective_Writes);
3638
3639 Decorate (Aspect, Aitem);
3640 Insert_Pragma (Aitem);
3641 goto Continue;
3642
3643 -- Aspect Extensions_Visible is never delayed because it is
3644 -- equivalent to a source pragma which appears after the
3645 -- related subprogram.
3646
3647 when Aspect_Extensions_Visible =>
3648 Aitem := Make_Aitem_Pragma
3649 (Pragma_Argument_Associations => New_List (
3650 Make_Pragma_Argument_Association (Loc,
3651 Expression => Relocate_Node (Expr))),
3652 Pragma_Name => Name_Extensions_Visible);
3653
3654 Decorate (Aspect, Aitem);
3655 Insert_Pragma (Aitem);
3656 goto Continue;
3657
3658 -- Aspect Ghost is never delayed because it is equivalent to a
3659 -- source pragma which appears at the top of [generic] package
3660 -- declarations or after an object, a [generic] subprogram, or
3661 -- a type declaration.
3662
3663 when Aspect_Ghost =>
3664 Aitem := Make_Aitem_Pragma
3665 (Pragma_Argument_Associations => New_List (
3666 Make_Pragma_Argument_Association (Loc,
3667 Expression => Relocate_Node (Expr))),
3668 Pragma_Name => Name_Ghost);
3669
3670 Decorate (Aspect, Aitem);
3671 Insert_Pragma (Aitem);
3672 goto Continue;
3673
3674 -- Global
3675
3676 -- Aspect Global is never delayed because it is equivalent to
3677 -- a source pragma which appears after the related subprogram.
3678 -- To deal with forward references, the generated pragma is
3679 -- stored in the contract of the related subprogram and later
3680 -- analyzed at the end of the declarative region. See routine
3681 -- Analyze_Global_In_Decl_Part for details.
3682
3683 when Aspect_Global =>
3684 Aitem := Make_Aitem_Pragma
3685 (Pragma_Argument_Associations => New_List (
3686 Make_Pragma_Argument_Association (Loc,
3687 Expression => Relocate_Node (Expr))),
3688 Pragma_Name => Name_Global);
3689
3690 Decorate (Aspect, Aitem);
3691 Insert_Pragma (Aitem);
3692 goto Continue;
3693
3694 -- Initial_Condition
3695
3696 -- Aspect Initial_Condition is never delayed because it is
3697 -- equivalent to a source pragma which appears after the
3698 -- related package. To deal with forward references, the
3699 -- generated pragma is stored in the contract of the related
3700 -- package and later analyzed at the end of the declarative
3701 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
3702 -- for details.
3703
3704 when Aspect_Initial_Condition => Initial_Condition : declare
3705 Context : Node_Id := N;
3706
3707 begin
3708 -- When aspect Initial_Condition appears on a generic
3709 -- package, it is propagated to the package instance. The
3710 -- context in this case is the instance spec.
3711
3712 if Nkind (Context) = N_Package_Instantiation then
3713 Context := Instance_Spec (Context);
3714 end if;
3715
3716 if Nkind (Context) in N_Generic_Package_Declaration
3717 | N_Package_Declaration
3718 then
3719 Aitem := Make_Aitem_Pragma
3720 (Pragma_Argument_Associations => New_List (
3721 Make_Pragma_Argument_Association (Loc,
3722 Expression => Relocate_Node (Expr))),
3723 Pragma_Name =>
3724 Name_Initial_Condition);
3725
3726 Decorate (Aspect, Aitem);
3727 Insert_Pragma
3728 (Prag => Aitem,
3729 Is_Instance =>
3730 Is_Generic_Instance (Defining_Entity (Context)));
3731
3732 -- Otherwise the context is illegal
3733
3734 else
3735 Error_Msg_NE
3736 ("aspect & must apply to a package declaration",
3737 Aspect, Id);
3738 end if;
3739
3740 goto Continue;
3741 end Initial_Condition;
3742
3743 -- Initializes
3744
3745 -- Aspect Initializes is never delayed because it is equivalent
3746 -- to a source pragma appearing after the related package. To
3747 -- deal with forward references, the generated pragma is stored
3748 -- in the contract of the related package and later analyzed at
3749 -- the end of the declarative region. For details, see routine
3750 -- Analyze_Initializes_In_Decl_Part.
3751
3752 when Aspect_Initializes => Initializes : declare
3753 Context : Node_Id := N;
3754
3755 begin
3756 -- When aspect Initializes appears on a generic package,
3757 -- it is propagated to the package instance. The context
3758 -- in this case is the instance spec.
3759
3760 if Nkind (Context) = N_Package_Instantiation then
3761 Context := Instance_Spec (Context);
3762 end if;
3763
3764 if Nkind (Context) in N_Generic_Package_Declaration
3765 | N_Package_Declaration
3766 then
3767 Aitem := Make_Aitem_Pragma
3768 (Pragma_Argument_Associations => New_List (
3769 Make_Pragma_Argument_Association (Loc,
3770 Expression => Relocate_Node (Expr))),
3771 Pragma_Name => Name_Initializes);
3772
3773 Decorate (Aspect, Aitem);
3774 Insert_Pragma
3775 (Prag => Aitem,
3776 Is_Instance =>
3777 Is_Generic_Instance (Defining_Entity (Context)));
3778
3779 -- Otherwise the context is illegal
3780
3781 else
3782 Error_Msg_NE
3783 ("aspect & must apply to a package declaration",
3784 Aspect, Id);
3785 end if;
3786
3787 goto Continue;
3788 end Initializes;
3789
3790 -- Max_Entry_Queue_Depth
3791
3792 when Aspect_Max_Entry_Queue_Depth =>
3793 Aitem := Make_Aitem_Pragma
3794 (Pragma_Argument_Associations => New_List (
3795 Make_Pragma_Argument_Association (Loc,
3796 Expression => Relocate_Node (Expr))),
3797 Pragma_Name => Name_Max_Entry_Queue_Depth);
3798
3799 Decorate (Aspect, Aitem);
3800 Insert_Pragma (Aitem);
3801 goto Continue;
3802
3803 -- Max_Entry_Queue_Length
3804
3805 when Aspect_Max_Entry_Queue_Length =>
3806 Aitem := Make_Aitem_Pragma
3807 (Pragma_Argument_Associations => New_List (
3808 Make_Pragma_Argument_Association (Loc,
3809 Expression => Relocate_Node (Expr))),
3810 Pragma_Name => Name_Max_Entry_Queue_Length);
3811
3812 Decorate (Aspect, Aitem);
3813 Insert_Pragma (Aitem);
3814 goto Continue;
3815
3816 -- Max_Queue_Length
3817
3818 when Aspect_Max_Queue_Length =>
3819 Aitem := Make_Aitem_Pragma
3820 (Pragma_Argument_Associations => New_List (
3821 Make_Pragma_Argument_Association (Loc,
3822 Expression => Relocate_Node (Expr))),
3823 Pragma_Name => Name_Max_Queue_Length);
3824
3825 Decorate (Aspect, Aitem);
3826 Insert_Pragma (Aitem);
3827 goto Continue;
3828
3829 -- Aspect No_Caching is never delayed because it is equivalent
3830 -- to a source pragma which appears after the related object
3831 -- declaration.
3832
3833 when Aspect_No_Caching =>
3834 Aitem := Make_Aitem_Pragma
3835 (Pragma_Argument_Associations => New_List (
3836 Make_Pragma_Argument_Association (Loc,
3837 Expression => Relocate_Node (Expr))),
3838 Pragma_Name => Name_No_Caching);
3839
3840 Decorate (Aspect, Aitem);
3841 Insert_Pragma (Aitem);
3842 goto Continue;
3843
3844 -- No_Controlled_Parts, No_Task_Parts
3845
3846 when Aspect_No_Controlled_Parts | Aspect_No_Task_Parts =>
3847
3848 -- Check appropriate type argument
3849
3850 if not Is_Type (E) then
3851 Error_Msg_N
3852 ("aspect % can only be applied to types", E);
3853 end if;
3854
3855 -- Disallow subtypes
3856
3857 if Nkind (Declaration_Node (E)) = N_Subtype_Declaration then
3858 Error_Msg_N
3859 ("aspect % cannot be applied to subtypes", E);
3860 end if;
3861
3862 -- Resolve the expression to a boolean
3863
3864 if Present (Expr) then
3865 Check_Expr_Is_OK_Static_Expression (Expr, Any_Boolean);
3866 end if;
3867
3868 goto Continue;
3869
3870 -- Obsolescent
3871
3872 when Aspect_Obsolescent => declare
3873 Args : List_Id;
3874
3875 begin
3876 if No (Expr) then
3877 Args := No_List;
3878 else
3879 Args := New_List (
3880 Make_Pragma_Argument_Association (Sloc (Expr),
3881 Expression => Relocate_Node (Expr)));
3882 end if;
3883
3884 Aitem := Make_Aitem_Pragma
3885 (Pragma_Argument_Associations => Args,
3886 Pragma_Name => Name_Obsolescent);
3887 end;
3888
3889 -- Part_Of
3890
3891 when Aspect_Part_Of =>
3892 if Nkind (N) in N_Object_Declaration
3893 | N_Package_Instantiation
3894 or else Is_Single_Concurrent_Type_Declaration (N)
3895 then
3896 Aitem := Make_Aitem_Pragma
3897 (Pragma_Argument_Associations => New_List (
3898 Make_Pragma_Argument_Association (Loc,
3899 Expression => Relocate_Node (Expr))),
3900 Pragma_Name => Name_Part_Of);
3901
3902 Decorate (Aspect, Aitem);
3903 Insert_Pragma (Aitem);
3904
3905 else
3906 Error_Msg_NE
3907 ("aspect & must apply to package instantiation, "
3908 & "object, single protected type or single task type",
3909 Aspect, Id);
3910 end if;
3911
3912 goto Continue;
3913
3914 -- SPARK_Mode
3915
3916 when Aspect_SPARK_Mode =>
3917 Aitem := Make_Aitem_Pragma
3918 (Pragma_Argument_Associations => New_List (
3919 Make_Pragma_Argument_Association (Loc,
3920 Expression => Relocate_Node (Expr))),
3921 Pragma_Name => Name_SPARK_Mode);
3922
3923 Decorate (Aspect, Aitem);
3924 Insert_Pragma (Aitem);
3925 goto Continue;
3926
3927 -- Refined_Depends
3928
3929 -- Aspect Refined_Depends is never delayed because it is
3930 -- equivalent to a source pragma which appears in the
3931 -- declarations of the related subprogram body. To deal with
3932 -- forward references, the generated pragma is stored in the
3933 -- contract of the related subprogram body and later analyzed
3934 -- at the end of the declarative region. For details, see
3935 -- routine Analyze_Refined_Depends_In_Decl_Part.
3936
3937 when Aspect_Refined_Depends =>
3938 Aitem := Make_Aitem_Pragma
3939 (Pragma_Argument_Associations => New_List (
3940 Make_Pragma_Argument_Association (Loc,
3941 Expression => Relocate_Node (Expr))),
3942 Pragma_Name => Name_Refined_Depends);
3943
3944 Decorate (Aspect, Aitem);
3945 Insert_Pragma (Aitem);
3946 goto Continue;
3947
3948 -- Refined_Global
3949
3950 -- Aspect Refined_Global is never delayed because it is
3951 -- equivalent to a source pragma which appears in the
3952 -- declarations of the related subprogram body. To deal with
3953 -- forward references, the generated pragma is stored in the
3954 -- contract of the related subprogram body and later analyzed
3955 -- at the end of the declarative region. For details, see
3956 -- routine Analyze_Refined_Global_In_Decl_Part.
3957
3958 when Aspect_Refined_Global =>
3959 Aitem := Make_Aitem_Pragma
3960 (Pragma_Argument_Associations => New_List (
3961 Make_Pragma_Argument_Association (Loc,
3962 Expression => Relocate_Node (Expr))),
3963 Pragma_Name => Name_Refined_Global);
3964
3965 Decorate (Aspect, Aitem);
3966 Insert_Pragma (Aitem);
3967 goto Continue;
3968
3969 -- Refined_Post
3970
3971 when Aspect_Refined_Post =>
3972 Aitem := Make_Aitem_Pragma
3973 (Pragma_Argument_Associations => New_List (
3974 Make_Pragma_Argument_Association (Loc,
3975 Expression => Relocate_Node (Expr))),
3976 Pragma_Name => Name_Refined_Post);
3977
3978 Decorate (Aspect, Aitem);
3979 Insert_Pragma (Aitem);
3980 goto Continue;
3981
3982 -- Refined_State
3983
3984 when Aspect_Refined_State =>
3985
3986 -- The corresponding pragma for Refined_State is inserted in
3987 -- the declarations of the related package body. This action
3988 -- synchronizes both the source and from-aspect versions of
3989 -- the pragma.
3990
3991 if Nkind (N) = N_Package_Body then
3992 Aitem := Make_Aitem_Pragma
3993 (Pragma_Argument_Associations => New_List (
3994 Make_Pragma_Argument_Association (Loc,
3995 Expression => Relocate_Node (Expr))),
3996 Pragma_Name => Name_Refined_State);
3997
3998 Decorate (Aspect, Aitem);
3999 Insert_Pragma (Aitem);
4000
4001 -- Otherwise the context is illegal
4002
4003 else
4004 Error_Msg_NE
4005 ("aspect & must apply to a package body", Aspect, Id);
4006 end if;
4007
4008 goto Continue;
4009
4010 -- Relative_Deadline
4011
4012 when Aspect_Relative_Deadline =>
4013 Aitem := Make_Aitem_Pragma
4014 (Pragma_Argument_Associations => New_List (
4015 Make_Pragma_Argument_Association (Loc,
4016 Expression => Relocate_Node (Expr))),
4017 Pragma_Name => Name_Relative_Deadline);
4018
4019 -- If the aspect applies to a task, the corresponding pragma
4020 -- must appear within its declarations, not after.
4021
4022 if Nkind (N) = N_Task_Type_Declaration then
4023 declare
4024 Def : Node_Id;
4025 V : List_Id;
4026
4027 begin
4028 if No (Task_Definition (N)) then
4029 Set_Task_Definition (N,
4030 Make_Task_Definition (Loc,
4031 Visible_Declarations => New_List,
4032 End_Label => Empty));
4033 end if;
4034
4035 Def := Task_Definition (N);
4036 V := Visible_Declarations (Def);
4037 if not Is_Empty_List (V) then
4038 Insert_Before (First (V), Aitem);
4039
4040 else
4041 Set_Visible_Declarations (Def, New_List (Aitem));
4042 end if;
4043
4044 goto Continue;
4045 end;
4046 end if;
4047
4048 -- Relaxed_Initialization
4049
4050 when Aspect_Relaxed_Initialization =>
4051 Analyze_Aspect_Relaxed_Initialization;
4052 goto Continue;
4053
4054 -- Secondary_Stack_Size
4055
4056 -- Aspect Secondary_Stack_Size needs to be converted into a
4057 -- pragma for two reasons: the attribute is not analyzed until
4058 -- after the expansion of the task type declaration and the
4059 -- attribute does not have visibility on the discriminant.
4060
4061 when Aspect_Secondary_Stack_Size =>
4062 Aitem := Make_Aitem_Pragma
4063 (Pragma_Argument_Associations => New_List (
4064 Make_Pragma_Argument_Association (Loc,
4065 Expression => Relocate_Node (Expr))),
4066 Pragma_Name =>
4067 Name_Secondary_Stack_Size);
4068
4069 Decorate (Aspect, Aitem);
4070 Insert_Pragma (Aitem);
4071 goto Continue;
4072
4073 -- Volatile_Function
4074
4075 -- Aspect Volatile_Function is never delayed because it is
4076 -- equivalent to a source pragma which appears after the
4077 -- related subprogram.
4078
4079 when Aspect_Volatile_Function =>
4080 Aitem := Make_Aitem_Pragma
4081 (Pragma_Argument_Associations => New_List (
4082 Make_Pragma_Argument_Association (Loc,
4083 Expression => Relocate_Node (Expr))),
4084 Pragma_Name => Name_Volatile_Function);
4085
4086 Decorate (Aspect, Aitem);
4087 Insert_Pragma (Aitem);
4088 goto Continue;
4089
4090 -- Case 2e: Annotate aspect
4091
4092 when Aspect_Annotate | Aspect_GNAT_Annotate =>
4093 declare
4094 Args : List_Id;
4095 Pargs : List_Id;
4096 Arg : Node_Id;
4097
4098 begin
4099 -- The argument can be a single identifier
4100
4101 if Nkind (Expr) = N_Identifier then
4102
4103 -- One level of parens is allowed
4104
4105 if Paren_Count (Expr) > 1 then
4106 Error_Msg_F ("extra parentheses ignored", Expr);
4107 end if;
4108
4109 Set_Paren_Count (Expr, 0);
4110
4111 -- Add the single item to the list
4112
4113 Args := New_List (Expr);
4114
4115 -- Otherwise we must have an aggregate
4116
4117 elsif Nkind (Expr) = N_Aggregate then
4118
4119 -- Must be positional
4120
4121 if Present (Component_Associations (Expr)) then
4122 Error_Msg_F
4123 ("purely positional aggregate required", Expr);
4124 goto Continue;
4125 end if;
4126
4127 -- Must not be parenthesized
4128
4129 if Paren_Count (Expr) /= 0 then
4130 Error_Msg_F -- CODEFIX
4131 ("redundant parentheses", Expr);
4132 end if;
4133
4134 -- List of arguments is list of aggregate expressions
4135
4136 Args := Expressions (Expr);
4137
4138 -- Anything else is illegal
4139
4140 else
4141 Error_Msg_F ("wrong form for Annotate aspect", Expr);
4142 goto Continue;
4143 end if;
4144
4145 -- Prepare pragma arguments
4146
4147 Pargs := New_List;
4148 Arg := First (Args);
4149 while Present (Arg) loop
4150 Append_To (Pargs,
4151 Make_Pragma_Argument_Association (Sloc (Arg),
4152 Expression => Relocate_Node (Arg)));
4153 Next (Arg);
4154 end loop;
4155
4156 Append_To (Pargs,
4157 Make_Pragma_Argument_Association (Sloc (Ent),
4158 Chars => Name_Entity,
4159 Expression => Ent));
4160
4161 Aitem := Make_Aitem_Pragma
4162 (Pragma_Argument_Associations => Pargs,
4163 Pragma_Name => Name_Annotate);
4164 end;
4165
4166 -- Case 3 : Aspects that don't correspond to pragma/attribute
4167 -- definition clause.
4168
4169 -- Case 3a: The aspects listed below don't correspond to
4170 -- pragmas/attributes but do require delayed analysis.
4171
4172 when Aspect_Default_Value | Aspect_Default_Component_Value =>
4173 Error_Msg_Name_1 := Nam;
4174
4175 if not Is_Type (E) then
4176 Error_Msg_N ("aspect% can only apply to a type", Id);
4177 goto Continue;
4178
4179 elsif not Is_First_Subtype (E) then
4180 Error_Msg_N ("aspect% cannot apply to subtype", Id);
4181 goto Continue;
4182
4183 elsif A_Id = Aspect_Default_Value
4184 and then not Is_Scalar_Type (E)
4185 then
4186 Error_Msg_N
4187 ("aspect% can only be applied to scalar type", Id);
4188 goto Continue;
4189
4190 elsif A_Id = Aspect_Default_Component_Value then
4191 if not Is_Array_Type (E) then
4192 Error_Msg_N
4193 ("aspect% can only be applied to array type", Id);
4194 goto Continue;
4195
4196 elsif not Is_Scalar_Type (Component_Type (E)) then
4197 Error_Msg_N ("aspect% requires scalar components", Id);
4198 goto Continue;
4199 end if;
4200 end if;
4201
4202 Aitem := Empty;
4203
4204 when Aspect_Aggregate =>
4205 Validate_Aspect_Aggregate (Expr);
4206 Record_Rep_Item (E, Aspect);
4207 goto Continue;
4208
4209 when Aspect_Stable_Properties =>
4210 Validate_Aspect_Stable_Properties
4211 (E, Expr, Class_Present => Class_Present (Aspect));
4212 Record_Rep_Item (E, Aspect);
4213 goto Continue;
4214
4215 when Aspect_Designated_Storage_Model =>
4216 if not All_Extensions_Allowed then
4217 Error_Msg_GNAT_Extension ("aspect %", Sloc (Aspect));
4218
4219 elsif not Is_Type (E)
4220 or else Ekind (E) /= E_Access_Type
4221 then
4222 Error_Msg_N
4223 ("can only be specified for pool-specific access type",
4224 Aspect);
4225 end if;
4226
4227 Record_Rep_Item (E, Aspect);
4228 goto Continue;
4229
4230 when Aspect_Storage_Model_Type =>
4231 if not All_Extensions_Allowed then
4232 Error_Msg_GNAT_Extension ("aspect %", Sloc (Aspect));
4233
4234 elsif not Is_Type (E)
4235 or else not Is_Immutably_Limited_Type (E)
4236 then
4237 Error_Msg_N
4238 ("can only be specified for immutably limited type",
4239 Aspect);
4240 end if;
4241
4242 Record_Rep_Item (E, Aspect);
4243 goto Continue;
4244
4245 when Aspect_Integer_Literal
4246 | Aspect_Real_Literal
4247 | Aspect_String_Literal
4248 =>
4249
4250 if not Is_First_Subtype (E) then
4251 Error_Msg_N
4252 ("may only be specified for a first subtype", Aspect);
4253 goto Continue;
4254 end if;
4255
4256 if Ada_Version < Ada_2022 then
4257 Check_Restriction
4258 (No_Implementation_Aspect_Specifications, N);
4259 end if;
4260
4261 Aitem := Empty;
4262
4263 -- Case 3b: The aspects listed below don't correspond to
4264 -- pragmas/attributes and don't need delayed analysis.
4265
4266 -- Implicit_Dereference
4267
4268 -- For Implicit_Dereference, External_Name and Link_Name, only
4269 -- the legality checks are done during the analysis, thus no
4270 -- delay is required.
4271
4272 when Aspect_Implicit_Dereference =>
4273 Analyze_Aspect_Implicit_Dereference;
4274 goto Continue;
4275
4276 -- Dimension
4277
4278 when Aspect_Dimension =>
4279 Analyze_Aspect_Dimension (N, Id, Expr);
4280 goto Continue;
4281
4282 -- Dimension_System
4283
4284 when Aspect_Dimension_System =>
4285 Analyze_Aspect_Dimension_System (N, Id, Expr);
4286 goto Continue;
4287
4288 -- Case 4: Aspects requiring special handling
4289
4290 -- Pre/Post/Test_Case/Contract_Cases/Subprogram_Variant whose
4291 -- corresponding pragmas take care of the delay.
4292
4293 -- Pre/Post
4294
4295 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
4296 -- with a first argument that is the expression, and a second
4297 -- argument that is an informative message if the test fails.
4298 -- This is inserted right after the declaration, to get the
4299 -- required pragma placement. The processing for the pragmas
4300 -- takes care of the required delay.
4301
4302 when Pre_Post_Aspects => Pre_Post : declare
4303 Pname : Name_Id;
4304
4305 begin
4306 if A_Id in Aspect_Pre | Aspect_Precondition then
4307 Pname := Name_Precondition;
4308 else
4309 Pname := Name_Postcondition;
4310 end if;
4311
4312 -- Check that the class-wide predicate cannot be applied to
4313 -- an operation of a synchronized type. AI12-0182 forbids
4314 -- these altogether, while earlier language semantics made
4315 -- them legal on tagged synchronized types.
4316
4317 -- Other legality checks are performed when analyzing the
4318 -- contract of the operation.
4319
4320 if Class_Present (Aspect)
4321 and then Is_Concurrent_Type (Current_Scope)
4322 and then Ekind (E) in E_Entry | E_Function | E_Procedure
4323 then
4324 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
4325 Error_Msg_N
4326 ("aspect % can only be specified for a primitive "
4327 & "operation of a tagged type", Aspect);
4328
4329 goto Continue;
4330 end if;
4331
4332 -- Remember class-wide conditions; they will be merged
4333 -- with inherited conditions.
4334
4335 if Class_Present (Aspect)
4336 and then A_Id in Aspect_Pre | Aspect_Post
4337 and then Is_Subprogram (E)
4338 and then not Is_Ignored_Ghost_Entity (E)
4339 then
4340 if A_Id = Aspect_Pre then
4341 if Is_Ignored (Aspect) then
4342 Set_Ignored_Class_Preconditions (E,
4343 New_Copy_Tree (Expr));
4344 else
4345 Set_Class_Preconditions (E, New_Copy_Tree (Expr));
4346 end if;
4347
4348 -- Postconditions may split into separate aspects, and we
4349 -- remember the expression before such split (i.e. when
4350 -- the first postcondition is processed).
4351
4352 elsif No (Class_Postconditions (E))
4353 and then No (Ignored_Class_Postconditions (E))
4354 then
4355 if Is_Ignored (Aspect) then
4356 Set_Ignored_Class_Postconditions (E,
4357 New_Copy_Tree (Expr));
4358 else
4359 Set_Class_Postconditions (E, New_Copy_Tree (Expr));
4360 end if;
4361 end if;
4362 end if;
4363
4364 -- If the expressions is of the form A and then B, then
4365 -- we generate separate Pre/Post aspects for the separate
4366 -- clauses. Since we allow multiple pragmas, there is no
4367 -- problem in allowing multiple Pre/Post aspects internally.
4368 -- These should be treated in reverse order (B first and
4369 -- A second) since they are later inserted just after N in
4370 -- the order they are treated. This way, the pragma for A
4371 -- ends up preceding the pragma for B, which may have an
4372 -- importance for the error raised (either constraint error
4373 -- or precondition error).
4374
4375 -- We do not do this for Pre'Class, since we have to put
4376 -- these conditions together in a complex OR expression.
4377
4378 -- We don't do this in GNATprove mode, because it brings no
4379 -- benefit for proof and causes annoyance for flow analysis,
4380 -- which prefers to be as close to the original source code
4381 -- as possible. Also we don't do this when analyzing generic
4382 -- units since it causes spurious visibility errors in the
4383 -- preanalysis of instantiations.
4384
4385 if not GNATprove_Mode
4386 and then (Pname = Name_Postcondition
4387 or else not Class_Present (Aspect))
4388 and then not Inside_A_Generic
4389 then
4390 while Nkind (Expr) = N_And_Then loop
4391 Insert_After (Aspect,
4392 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
4393 Identifier => Identifier (Aspect),
4394 Expression => Relocate_Node (Left_Opnd (Expr)),
4395 Class_Present => Class_Present (Aspect),
4396 Split_PPC => True));
4397 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
4398 Eloc := Sloc (Expr);
4399 end loop;
4400 end if;
4401
4402 -- Build the precondition/postcondition pragma
4403
4404 Aitem := Make_Aitem_Pragma
4405 (Pragma_Argument_Associations => New_List (
4406 Make_Pragma_Argument_Association (Eloc,
4407 Chars => Name_Check,
4408 Expression => Relocate_Expression (Expr))),
4409 Pragma_Name => Pname);
4410
4411 -- Add message unless exception messages are suppressed
4412
4413 if not Opt.Exception_Locations_Suppressed then
4414 Append_To (Pragma_Argument_Associations (Aitem),
4415 Make_Pragma_Argument_Association (Eloc,
4416 Chars => Name_Message,
4417 Expression =>
4418 Make_String_Literal (Eloc,
4419 Strval => "failed "
4420 & Get_Name_String (Pname)
4421 & " from "
4422 & Build_Location_String (Eloc))));
4423 end if;
4424
4425 Set_Is_Delayed_Aspect (Aspect);
4426
4427 -- For Pre/Post cases, insert immediately after the entity
4428 -- declaration, since that is the required pragma placement.
4429 -- Note that for these aspects, we do not have to worry
4430 -- about delay issues, since the pragmas themselves deal
4431 -- with delay of visibility for the expression analysis.
4432
4433 Insert_Pragma (Aitem);
4434
4435 goto Continue;
4436 end Pre_Post;
4437
4438 -- Test_Case
4439
4440 when Aspect_Test_Case => Test_Case : declare
4441 Args : List_Id;
4442 Comp_Expr : Node_Id;
4443 Comp_Assn : Node_Id;
4444
4445 begin
4446 Args := New_List;
4447
4448 if Nkind (Parent (N)) = N_Compilation_Unit then
4449 Error_Msg_Name_1 := Nam;
4450 Error_Msg_N ("incorrect placement of aspect %", E);
4451 goto Continue;
4452 end if;
4453
4454 if Nkind (Expr) /= N_Aggregate
4455 or else Null_Record_Present (Expr)
4456 then
4457 Error_Msg_Name_1 := Nam;
4458 Error_Msg_NE
4459 ("wrong syntax for aspect % for &", Id, E);
4460 goto Continue;
4461 end if;
4462
4463 -- Check that the expression is a proper aggregate (no
4464 -- parentheses).
4465
4466 if Paren_Count (Expr) /= 0 then
4467 Error_Msg_F -- CODEFIX
4468 ("redundant parentheses", Expr);
4469 goto Continue;
4470 end if;
4471
4472 -- Create the list of arguments for building the Test_Case
4473 -- pragma.
4474
4475 Comp_Expr := First (Expressions (Expr));
4476 while Present (Comp_Expr) loop
4477 Append_To (Args,
4478 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
4479 Expression => Relocate_Node (Comp_Expr)));
4480 Next (Comp_Expr);
4481 end loop;
4482
4483 Comp_Assn := First (Component_Associations (Expr));
4484 while Present (Comp_Assn) loop
4485 if List_Length (Choices (Comp_Assn)) /= 1
4486 or else
4487 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
4488 then
4489 Error_Msg_Name_1 := Nam;
4490 Error_Msg_NE
4491 ("wrong syntax for aspect % for &", Id, E);
4492 goto Continue;
4493 end if;
4494
4495 Append_To (Args,
4496 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
4497 Chars => Chars (First (Choices (Comp_Assn))),
4498 Expression =>
4499 Relocate_Node (Expression (Comp_Assn))));
4500 Next (Comp_Assn);
4501 end loop;
4502
4503 -- Build the test-case pragma
4504
4505 Aitem := Make_Aitem_Pragma
4506 (Pragma_Argument_Associations => Args,
4507 Pragma_Name => Name_Test_Case);
4508 end Test_Case;
4509
4510 -- Contract_Cases
4511
4512 when Aspect_Contract_Cases =>
4513 Aitem := Make_Aitem_Pragma
4514 (Pragma_Argument_Associations => New_List (
4515 Make_Pragma_Argument_Association (Loc,
4516 Expression => Relocate_Node (Expr))),
4517 Pragma_Name => Name_Contract_Cases);
4518
4519 Decorate (Aspect, Aitem);
4520 Insert_Pragma (Aitem);
4521 goto Continue;
4522
4523 -- Subprogram_Variant
4524
4525 when Aspect_Subprogram_Variant =>
4526 Aitem := Make_Aitem_Pragma
4527 (Pragma_Argument_Associations => New_List (
4528 Make_Pragma_Argument_Association (Loc,
4529 Expression => Relocate_Node (Expr))),
4530 Pragma_Name => Name_Subprogram_Variant);
4531
4532 Decorate (Aspect, Aitem);
4533 Insert_Pragma (Aitem);
4534 goto Continue;
4535
4536 -- Case 5: Special handling for aspects with an optional
4537 -- boolean argument.
4538
4539 -- In the delayed case, the corresponding pragma cannot be
4540 -- generated yet because the evaluation of the boolean needs
4541 -- to be delayed till the freeze point.
4542
4543 when Boolean_Aspects
4544 | Library_Unit_Aspects
4545 =>
4546 Set_Is_Boolean_Aspect (Aspect);
4547
4548 -- Lock_Free aspect only apply to protected objects
4549
4550 if A_Id = Aspect_Lock_Free then
4551 if Ekind (E) /= E_Protected_Type then
4552 Error_Msg_Name_1 := Nam;
4553 Error_Msg_N
4554 ("aspect % only applies to a protected type " &
4555 "or object",
4556 Aspect);
4557
4558 else
4559 -- Set the Uses_Lock_Free flag to True if there is no
4560 -- expression or if the expression is True. The
4561 -- evaluation of this aspect should be delayed to the
4562 -- freeze point if we wanted to handle the corner case
4563 -- of "true" or "false" being redefined.
4564
4565 if No (Expr)
4566 or else Is_True (Static_Boolean (Expr))
4567 then
4568 Set_Uses_Lock_Free (E);
4569 end if;
4570
4571 Record_Rep_Item (E, Aspect);
4572 end if;
4573
4574 goto Continue;
4575
4576 elsif A_Id in Aspect_Export | Aspect_Import then
4577 Analyze_Aspect_Export_Import;
4578
4579 -- Disable_Controlled
4580
4581 elsif A_Id = Aspect_Disable_Controlled then
4582 Analyze_Aspect_Disable_Controlled;
4583 goto Continue;
4584
4585 -- Ada 2022 (AI12-0129): Exclusive_Functions
4586
4587 elsif A_Id = Aspect_Exclusive_Functions then
4588 if Ekind (E) /= E_Protected_Type then
4589 Error_Msg_Name_1 := Nam;
4590 Error_Msg_N
4591 ("aspect % only applies to a protected type " &
4592 "or object",
4593 Aspect);
4594 end if;
4595
4596 goto Continue;
4597
4598 -- Ada 2022 (AI12-0363): Full_Access_Only
4599
4600 elsif A_Id = Aspect_Full_Access_Only then
4601 Error_Msg_Ada_2022_Feature ("aspect %", Sloc (Aspect));
4602
4603 -- Ada 2022 (AI12-0075): static expression functions
4604
4605 elsif A_Id = Aspect_Static then
4606 Analyze_Aspect_Static;
4607 goto Continue;
4608
4609 -- Ada 2022 (AI12-0279)
4610
4611 elsif A_Id = Aspect_Yield then
4612 Analyze_Aspect_Yield;
4613 goto Continue;
4614 end if;
4615
4616 -- Library unit aspects require special handling in the case
4617 -- of a package declaration, the pragma needs to be inserted
4618 -- in the list of declarations for the associated package.
4619 -- There is no issue of visibility delay for these aspects.
4620
4621 if A_Id in Library_Unit_Aspects
4622 and then
4623 Nkind (N) in N_Package_Declaration
4624 | N_Generic_Package_Declaration
4625 and then Nkind (Parent (N)) /= N_Compilation_Unit
4626
4627 -- Aspect is legal on a local instantiation of a library-
4628 -- level generic unit.
4629
4630 and then not Is_Generic_Instance (Defining_Entity (N))
4631 then
4632 Error_Msg_N
4633 ("incorrect context for library unit aspect&", Id);
4634 goto Continue;
4635 end if;
4636
4637 -- Cases where we do not delay
4638
4639 if not Delay_Required then
4640
4641 -- Exclude aspects Export and Import because their pragma
4642 -- syntax does not map directly to a Boolean aspect.
4643
4644 if A_Id not in Aspect_Export | Aspect_Import then
4645 Aitem := Make_Aitem_Pragma
4646 (Pragma_Argument_Associations => New_List (
4647 Make_Pragma_Argument_Association (Sloc (Ent),
4648 Expression => Ent)),
4649 Pragma_Name => Nam);
4650 end if;
4651
4652 -- In general cases, the corresponding pragma/attribute
4653 -- definition clause will be inserted later at the freezing
4654 -- point, and we do not need to build it now.
4655
4656 else
4657 Aitem := Empty;
4658 end if;
4659
4660 -- Storage_Size
4661
4662 -- This is special because for access types we need to generate
4663 -- an attribute definition clause. This also works for single
4664 -- task declarations, but it does not work for task type
4665 -- declarations, because we have the case where the expression
4666 -- references a discriminant of the task type. That can't use
4667 -- an attribute definition clause because we would not have
4668 -- visibility on the discriminant. For that case we must
4669 -- generate a pragma in the task definition.
4670
4671 when Aspect_Storage_Size =>
4672
4673 -- Task type case
4674
4675 if Ekind (E) = E_Task_Type then
4676 declare
4677 Decl : constant Node_Id := Declaration_Node (E);
4678
4679 begin
4680 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
4681
4682 -- If no task definition, create one
4683
4684 if No (Task_Definition (Decl)) then
4685 Set_Task_Definition (Decl,
4686 Make_Task_Definition (Loc,
4687 Visible_Declarations => Empty_List,
4688 End_Label => Empty));
4689 end if;
4690
4691 -- Create a pragma and put it at the start of the task
4692 -- definition for the task type declaration.
4693
4694 Aitem := Make_Aitem_Pragma
4695 (Pragma_Argument_Associations => New_List (
4696 Make_Pragma_Argument_Association (Loc,
4697 Expression => Relocate_Node (Expr))),
4698 Pragma_Name => Name_Storage_Size);
4699
4700 Prepend
4701 (Aitem,
4702 Visible_Declarations (Task_Definition (Decl)));
4703 goto Continue;
4704 end;
4705
4706 -- All other cases, generate attribute definition
4707
4708 else
4709 Aitem :=
4710 Make_Attribute_Definition_Clause (Loc,
4711 Name => Ent,
4712 Chars => Name_Storage_Size,
4713 Expression => Relocate_Node (Expr));
4714 end if;
4715 end case;
4716
4717 -- Attach the corresponding pragma/attribute definition clause to
4718 -- the aspect specification node.
4719
4720 if Present (Aitem) then
4721 Set_From_Aspect_Specification (Aitem);
4722 end if;
4723
4724 -- For an aspect that applies to a type, indicate whether it
4725 -- appears on a partial view of the type.
4726
4727 if Is_Type (E)
4728 and then Is_Private_Type (E)
4729 then
4730 Set_Aspect_On_Partial_View (Aspect);
4731 end if;
4732
4733 -- In the context of a compilation unit, we directly put the
4734 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
4735 -- node (no delay is required here) except for aspects on a
4736 -- subprogram body (see below) and a generic package, for which we
4737 -- need to introduce the pragma before building the generic copy
4738 -- (see sem_ch12), and for package instantiations, where the
4739 -- library unit pragmas are better handled early.
4740
4741 if Nkind (Parent (N)) = N_Compilation_Unit
4742 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
4743 then
4744 declare
4745 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
4746
4747 begin
4748 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
4749
4750 -- For a Boolean aspect, create the corresponding pragma if
4751 -- no expression or if the value is True.
4752
4753 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
4754 if Is_True (Static_Boolean (Expr)) then
4755 Aitem := Make_Aitem_Pragma
4756 (Pragma_Argument_Associations => New_List (
4757 Make_Pragma_Argument_Association (Sloc (Ent),
4758 Expression => Ent)),
4759 Pragma_Name => Nam);
4760
4761 Set_From_Aspect_Specification (Aitem, True);
4762 Set_Corresponding_Aspect (Aitem, Aspect);
4763
4764 else
4765 goto Continue;
4766 end if;
4767 end if;
4768
4769 -- If the aspect is on a subprogram body (relevant aspect
4770 -- is Inline), add the pragma in front of the declarations.
4771
4772 if Nkind (N) = N_Subprogram_Body then
4773 if No (Declarations (N)) then
4774 Set_Declarations (N, New_List);
4775 end if;
4776
4777 Prepend (Aitem, Declarations (N));
4778
4779 elsif Nkind (N) = N_Generic_Package_Declaration then
4780 if No (Visible_Declarations (Specification (N))) then
4781 Set_Visible_Declarations (Specification (N), New_List);
4782 end if;
4783
4784 Prepend (Aitem,
4785 Visible_Declarations (Specification (N)));
4786
4787 elsif Nkind (N) = N_Package_Instantiation then
4788 declare
4789 Spec : constant Node_Id :=
4790 Specification (Instance_Spec (N));
4791 begin
4792 if No (Visible_Declarations (Spec)) then
4793 Set_Visible_Declarations (Spec, New_List);
4794 end if;
4795
4796 Prepend (Aitem, Visible_Declarations (Spec));
4797 end;
4798
4799 else
4800 if No (Pragmas_After (Aux)) then
4801 Set_Pragmas_After (Aux, New_List);
4802 end if;
4803
4804 Append (Aitem, Pragmas_After (Aux));
4805 end if;
4806
4807 goto Continue;
4808 end;
4809 end if;
4810
4811 -- The evaluation of the aspect is delayed to the freezing point.
4812 -- The pragma or attribute clause if there is one is then attached
4813 -- to the aspect specification which is put in the rep item list.
4814
4815 if Delay_Required then
4816 if Present (Aitem) then
4817 Set_Is_Delayed_Aspect (Aitem);
4818 Set_Aspect_Rep_Item (Aspect, Aitem);
4819 Set_Parent (Aitem, Aspect);
4820 end if;
4821
4822 Set_Is_Delayed_Aspect (Aspect);
4823
4824 -- In the case of Default_Value, link the aspect to base type
4825 -- as well, even though it appears on a first subtype. This is
4826 -- mandated by the semantics of the aspect. Do not establish
4827 -- the link when processing the base type itself as this leads
4828 -- to a rep item circularity.
4829
4830 if A_Id = Aspect_Default_Value and then Base_Type (E) /= E then
4831 Set_Has_Delayed_Aspects (Base_Type (E));
4832 Record_Rep_Item (Base_Type (E), Aspect);
4833 end if;
4834
4835 Set_Has_Delayed_Aspects (E);
4836 Record_Rep_Item (E, Aspect);
4837
4838 -- When delay is not required and the context is a package or a
4839 -- subprogram body, insert the pragma in the body declarations.
4840
4841 elsif Nkind (N) in N_Package_Body | N_Subprogram_Body then
4842 if No (Declarations (N)) then
4843 Set_Declarations (N, New_List);
4844 end if;
4845
4846 -- The pragma is added before source declarations
4847
4848 Prepend_To (Declarations (N), Aitem);
4849
4850 -- When delay is not required and the context is not a compilation
4851 -- unit, we simply insert the pragma/attribute definition clause
4852 -- in sequence.
4853
4854 elsif Present (Aitem) then
4855 Insert_After (Ins_Node, Aitem);
4856 Ins_Node := Aitem;
4857 end if;
4858
4859 <<Continue>>
4860
4861 -- If a nonoverridable aspect is explicitly specified for a
4862 -- derived type, then check consistency with the parent type.
4863
4864 if A_Id in Nonoverridable_Aspect_Id
4865 and then Nkind (N) = N_Full_Type_Declaration
4866 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
4867 and then not In_Instance_Body
4868 then
4869 declare
4870 Parent_Type : constant Entity_Id := Etype (E);
4871 Inherited_Aspect : constant Node_Id :=
4872 Find_Aspect (Parent_Type, A_Id);
4873 begin
4874 if Present (Inherited_Aspect)
4875 and then not Is_Confirming
4876 (A_Id, Inherited_Aspect, Aspect)
4877 then
4878 Error_Msg_Name_1 := Aspect_Names (A_Id);
4879 Error_Msg_Sloc := Sloc (Inherited_Aspect);
4880
4881 Error_Msg_N
4882 ("overriding aspect specification for "
4883 & "nonoverridable aspect % does not confirm "
4884 & "aspect specification inherited from #",
4885 Aspect);
4886 end if;
4887 end;
4888 end if;
4889 exception
4890 when Aspect_Exit => null;
4891 end Analyze_One_Aspect;
4892
4893 Next (Aspect);
4894 end loop Aspect_Loop;
4895
4896 if Has_Delayed_Aspects (E) then
4897 Ensure_Freeze_Node (E);
4898 end if;
4899 end Analyze_Aspect_Specifications;
4900
4901 ------------------------------------------------
4902 -- Analyze_Aspects_On_Subprogram_Body_Or_Stub --
4903 ------------------------------------------------
4904
4905 procedure Analyze_Aspects_On_Subprogram_Body_Or_Stub (N : Node_Id) is
4906 Body_Id : constant Entity_Id := Defining_Entity (N);
4907
4908 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
4909 -- Body [stub] N has aspects, but they are not properly placed. Emit an
4910 -- error message depending on the aspects involved. Spec_Id denotes the
4911 -- entity of the corresponding spec.
4912
4913 --------------------------------
4914 -- Diagnose_Misplaced_Aspects --
4915 --------------------------------
4916
4917 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
4918 procedure Misplaced_Aspect_Error
4919 (Asp : Node_Id;
4920 Ref_Nam : Name_Id);
4921 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
4922 -- the name of the refined version of the aspect.
4923
4924 ----------------------------
4925 -- Misplaced_Aspect_Error --
4926 ----------------------------
4927
4928 procedure Misplaced_Aspect_Error
4929 (Asp : Node_Id;
4930 Ref_Nam : Name_Id)
4931 is
4932 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
4933 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
4934
4935 begin
4936 -- The corresponding spec already contains the aspect in question
4937 -- and the one appearing on the body must be the refined form:
4938
4939 -- procedure P with Global ...;
4940 -- procedure P with Global ... is ... end P;
4941 -- ^
4942 -- Refined_Global
4943
4944 if Has_Aspect (Spec_Id, Asp_Id) then
4945 Error_Msg_Name_1 := Asp_Nam;
4946
4947 -- Subunits cannot carry aspects that apply to a subprogram
4948 -- declaration.
4949
4950 if Nkind (Parent (N)) = N_Subunit then
4951 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
4952
4953 -- Otherwise suggest the refined form
4954
4955 else
4956 Error_Msg_Name_2 := Ref_Nam;
4957 Error_Msg_N ("aspect % should be %", Asp);
4958 end if;
4959
4960 -- Otherwise the aspect must appear on the spec, not on the body
4961
4962 -- procedure P;
4963 -- procedure P with Global ... is ... end P;
4964
4965 else
4966 Error_Msg_N
4967 ("aspect specification must appear on initial declaration",
4968 Asp);
4969 end if;
4970 end Misplaced_Aspect_Error;
4971
4972 -- Local variables
4973
4974 Asp : Node_Id;
4975 Asp_Nam : Name_Id;
4976
4977 -- Start of processing for Diagnose_Misplaced_Aspects
4978
4979 begin
4980 -- Iterate over the aspect specifications and emit specific errors
4981 -- where applicable.
4982
4983 Asp := First (Aspect_Specifications (N));
4984 while Present (Asp) loop
4985 Asp_Nam := Chars (Identifier (Asp));
4986
4987 -- Do not emit errors on aspects that can appear on a subprogram
4988 -- body. This scenario occurs when the aspect specification list
4989 -- contains both misplaced and properly placed aspects.
4990
4991 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
4992 null;
4993
4994 -- Special diagnostics for SPARK aspects
4995
4996 elsif Asp_Nam = Name_Depends then
4997 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
4998
4999 elsif Asp_Nam = Name_Global then
5000 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
5001
5002 elsif Asp_Nam = Name_Post then
5003 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
5004
5005 -- Otherwise a language-defined aspect is misplaced
5006
5007 else
5008 Error_Msg_N
5009 ("aspect specification must appear on initial declaration",
5010 Asp);
5011 end if;
5012
5013 Next (Asp);
5014 end loop;
5015 end Diagnose_Misplaced_Aspects;
5016
5017 -- Local variables
5018
5019 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
5020
5021 -- Start of processing for Analyze_Aspects_On_Subprogram_Body_Or_Stub
5022
5023 begin
5024 -- Language-defined aspects cannot be associated with a subprogram body
5025 -- [stub] if the subprogram has a spec. Certain implementation defined
5026 -- aspects are allowed to break this rule (for all applicable cases, see
5027 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
5028
5029 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
5030 Diagnose_Misplaced_Aspects (Spec_Id);
5031 else
5032 Analyze_Aspect_Specifications (N, Body_Id);
5033 end if;
5034 end Analyze_Aspects_On_Subprogram_Body_Or_Stub;
5035
5036 -----------------------
5037 -- Analyze_At_Clause --
5038 -----------------------
5039
5040 -- An at clause is replaced by the corresponding Address attribute
5041 -- definition clause that is the preferred approach in Ada 95.
5042
5043 procedure Analyze_At_Clause (N : Node_Id) is
5044 CS : constant Boolean := Comes_From_Source (N);
5045
5046 begin
5047 -- This is an obsolescent feature
5048
5049 Check_Restriction (No_Obsolescent_Features, N);
5050
5051 if Warn_On_Obsolescent_Feature then
5052 Error_Msg_N
5053 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
5054 Error_Msg_N
5055 ("\?j?use address attribute definition clause instead", N);
5056 end if;
5057
5058 -- Rewrite as address clause
5059
5060 Rewrite (N,
5061 Make_Attribute_Definition_Clause (Sloc (N),
5062 Name => Identifier (N),
5063 Chars => Name_Address,
5064 Expression => Expression (N)));
5065
5066 -- We preserve Comes_From_Source, since logically the clause still comes
5067 -- from the source program even though it is changed in form.
5068
5069 Set_Comes_From_Source (N, CS);
5070
5071 -- Analyze rewritten clause
5072
5073 Analyze_Attribute_Definition_Clause (N);
5074 end Analyze_At_Clause;
5075
5076 -----------------------------------------
5077 -- Analyze_Attribute_Definition_Clause --
5078 -----------------------------------------
5079
5080 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
5081 Loc : constant Source_Ptr := Sloc (N);
5082 Nam : constant Node_Id := Name (N);
5083 Attr : constant Name_Id := Chars (N);
5084 Expr : constant Node_Id := Expression (N);
5085 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
5086
5087 Ent : Entity_Id;
5088 -- The entity of Nam after it is analyzed. In the case of an incomplete
5089 -- type, this is the underlying type.
5090
5091 U_Ent : Entity_Id;
5092 -- The underlying entity to which the attribute applies. Generally this
5093 -- is the Underlying_Type of Ent, except in the case where the clause
5094 -- applies to the full view of an incomplete or private type, in which
5095 -- case U_Ent is just a copy of Ent.
5096
5097 FOnly : Boolean := False;
5098 -- Reset to True for subtype specific attribute (Alignment, Size)
5099 -- and for stream attributes, i.e. those cases where in the call to
5100 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
5101 -- are checked. Note that the case of stream attributes is not clear
5102 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
5103 -- Storage_Size for derived task types, but that is also clearly
5104 -- unintentional.
5105
5106 procedure Analyze_Put_Image_TSS_Definition;
5107
5108 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
5109 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
5110 -- definition clauses.
5111
5112 function Duplicate_Clause return Boolean;
5113 -- This routine checks if the aspect for U_Ent being given by attribute
5114 -- definition clause N is for an aspect that has already been specified,
5115 -- and if so gives an error message. If there is a duplicate, True is
5116 -- returned, otherwise there is no error, and False is returned. Size
5117 -- and Value_Size are considered to conflict, but for compatibility,
5118 -- this is merely a warning.
5119
5120 procedure Check_Indexing_Functions;
5121 -- Check that the function in Constant_Indexing or Variable_Indexing
5122 -- attribute has the proper type structure. If the name is overloaded,
5123 -- check that some interpretation is legal.
5124
5125 procedure Check_Iterator_Functions;
5126 -- Check that there is a single function in Default_Iterator attribute
5127 -- that has the proper type structure.
5128
5129 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
5130 -- Common legality check for the previous two
5131
5132 -----------------------------------
5133 -- Analyze_Put_Image_TSS_Definition --
5134 -----------------------------------
5135
5136 procedure Analyze_Put_Image_TSS_Definition is
5137 Subp : Entity_Id := Empty;
5138 I : Interp_Index;
5139 It : Interp;
5140 Pnam : Entity_Id;
5141
5142 function Has_Good_Profile
5143 (Subp : Entity_Id;
5144 Report : Boolean := False) return Boolean;
5145 -- Return true if the entity is a subprogram with an appropriate
5146 -- profile for the attribute being defined. If result is False and
5147 -- Report is True, function emits appropriate error.
5148
5149 ----------------------
5150 -- Has_Good_Profile --
5151 ----------------------
5152
5153 function Has_Good_Profile
5154 (Subp : Entity_Id;
5155 Report : Boolean := False) return Boolean
5156 is
5157 F : Entity_Id;
5158 Typ : Entity_Id;
5159
5160 begin
5161 if Ekind (Subp) /= E_Procedure then
5162 return False;
5163 end if;
5164
5165 F := First_Formal (Subp);
5166
5167 if No (F) then
5168 return False;
5169 end if;
5170
5171 if Base_Type (Etype (F))
5172 /= Class_Wide_Type (RTE (RE_Root_Buffer_Type))
5173 then
5174 if Report then
5175 Error_Msg_N
5176 ("wrong type for Put_Image procedure''s first parameter",
5177 Parameter_Type (Parent (F)));
5178 end if;
5179
5180 return False;
5181 end if;
5182
5183 if Parameter_Mode (F) /= E_In_Out_Parameter then
5184 if Report then
5185 Error_Msg_N
5186 ("wrong mode for Put_Image procedure''s first parameter",
5187 Parent (F));
5188 end if;
5189
5190 return False;
5191 end if;
5192
5193 Next_Formal (F);
5194
5195 Typ := Etype (F);
5196
5197 -- Verify that the prefix of the attribute and the local name for
5198 -- the type of the formal match.
5199
5200 if Base_Type (Typ) /= Base_Type (Ent) then
5201 if Report then
5202 Error_Msg_N
5203 ("wrong type for Put_Image procedure''s second parameter",
5204 Parameter_Type (Parent (F)));
5205 end if;
5206
5207 return False;
5208 end if;
5209
5210 if Parameter_Mode (F) /= E_In_Parameter then
5211 if Report then
5212 Error_Msg_N
5213 ("wrong mode for Put_Image procedure''s second parameter",
5214 Parent (F));
5215 end if;
5216
5217 return False;
5218 end if;
5219
5220 if Present (Next_Formal (F)) then
5221 return False;
5222 end if;
5223
5224 return True;
5225 end Has_Good_Profile;
5226
5227 -- Start of processing for Analyze_Put_Image_TSS_Definition
5228
5229 begin
5230 if not Is_Type (U_Ent) then
5231 Error_Msg_N ("local name must be a subtype", Nam);
5232 return;
5233
5234 elsif not Is_First_Subtype (U_Ent) then
5235 Error_Msg_N ("local name must be a first subtype", Nam);
5236 return;
5237 end if;
5238
5239 Pnam := TSS (Base_Type (U_Ent), TSS_Put_Image);
5240
5241 -- If Pnam is present, it can be either inherited from an ancestor
5242 -- type (in which case it is legal to redefine it for this type), or
5243 -- be a previous definition of the attribute for the same type (in
5244 -- which case it is illegal).
5245
5246 -- In the first case, it will have been analyzed already, and we can
5247 -- check that its profile does not match the expected profile for the
5248 -- Put_Image attribute of U_Ent. In the second case, either Pnam has
5249 -- been analyzed (and has the expected profile), or it has not been
5250 -- analyzed yet (case of a type that has not been frozen yet and for
5251 -- which Put_Image has been set using Set_TSS).
5252
5253 if Present (Pnam)
5254 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
5255 then
5256 Error_Msg_Sloc := Sloc (Pnam);
5257 Error_Msg_Name_1 := Attr;
5258 Error_Msg_N ("% attribute already defined #", Nam);
5259 return;
5260 end if;
5261
5262 Analyze (Expr);
5263
5264 if Is_Entity_Name (Expr) then
5265 if not Is_Overloaded (Expr) then
5266 if Has_Good_Profile (Entity (Expr), Report => True) then
5267 Subp := Entity (Expr);
5268 end if;
5269
5270 else
5271 Get_First_Interp (Expr, I, It);
5272 while Present (It.Nam) loop
5273 if Has_Good_Profile (It.Nam) then
5274 Subp := It.Nam;
5275 exit;
5276 end if;
5277
5278 Get_Next_Interp (I, It);
5279 end loop;
5280 end if;
5281 end if;
5282
5283 if Present (Subp) then
5284 if Is_Abstract_Subprogram (Subp) then
5285 Error_Msg_N ("Put_Image subprogram must not be abstract", Expr);
5286 return;
5287 end if;
5288
5289 Set_Entity (Expr, Subp);
5290 Set_Etype (Expr, Etype (Subp));
5291
5292 New_Put_Image_Subprogram (N, U_Ent, Subp);
5293
5294 else
5295 Error_Msg_Name_1 := Attr;
5296 Error_Msg_N ("incorrect expression for% attribute", Expr);
5297 end if;
5298 end Analyze_Put_Image_TSS_Definition;
5299
5300 -----------------------------------
5301 -- Analyze_Stream_TSS_Definition --
5302 -----------------------------------
5303
5304 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
5305 Subp : Entity_Id := Empty;
5306 I : Interp_Index;
5307 It : Interp;
5308 Pnam : Entity_Id;
5309
5310 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
5311 -- True for Read attribute, False for other attributes
5312
5313 function Has_Good_Profile
5314 (Subp : Entity_Id;
5315 Report : Boolean := False) return Boolean;
5316 -- Return true if the entity is a subprogram with an appropriate
5317 -- profile for the attribute being defined. If result is False and
5318 -- Report is True, function emits appropriate error.
5319
5320 ----------------------
5321 -- Has_Good_Profile --
5322 ----------------------
5323
5324 function Has_Good_Profile
5325 (Subp : Entity_Id;
5326 Report : Boolean := False) return Boolean
5327 is
5328 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
5329 (False => E_Procedure, True => E_Function);
5330 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
5331 F : Entity_Id;
5332 Typ : Entity_Id;
5333
5334 begin
5335 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
5336 return False;
5337 end if;
5338
5339 F := First_Formal (Subp);
5340
5341 if No (F)
5342 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
5343 or else Base_Type (Designated_Type (Etype (F))) /=
5344 Class_Wide_Type (RTE (RE_Root_Stream_Type))
5345 then
5346 return False;
5347 end if;
5348
5349 if not Is_Function then
5350 Next_Formal (F);
5351
5352 declare
5353 Expected_Mode : constant array (Boolean) of Entity_Kind :=
5354 (False => E_In_Parameter,
5355 True => E_Out_Parameter);
5356 begin
5357 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
5358 return False;
5359 end if;
5360 end;
5361
5362 Typ := Etype (F);
5363
5364 else
5365 Typ := Etype (Subp);
5366 end if;
5367
5368 -- Verify that the prefix of the attribute and the local name for
5369 -- the type of the formal match.
5370
5371 if Base_Type (Typ) /= Base_Type (Ent) then
5372 return False;
5373 end if;
5374
5375 if Present (Next_Formal (F)) then
5376 return False;
5377
5378 elsif not Is_Scalar_Type (Typ)
5379 and then not Is_First_Subtype (Typ)
5380 and then not Is_Class_Wide_Type (Typ)
5381 then
5382 if Report and not Is_First_Subtype (Typ) then
5383 Error_Msg_N
5384 ("subtype of formal in stream operation must be a first "
5385 & "subtype", Parameter_Type (Parent (F)));
5386 end if;
5387
5388 return False;
5389
5390 else
5391 return True;
5392 end if;
5393 end Has_Good_Profile;
5394
5395 -- Start of processing for Analyze_Stream_TSS_Definition
5396
5397 begin
5398 FOnly := True;
5399
5400 if not Is_Type (U_Ent) then
5401 Error_Msg_N ("local name must be a subtype", Nam);
5402 return;
5403
5404 elsif not Is_First_Subtype (U_Ent) then
5405 Error_Msg_N ("local name must be a first subtype", Nam);
5406 return;
5407 end if;
5408
5409 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
5410
5411 -- If Pnam is present, it can be either inherited from an ancestor
5412 -- type (in which case it is legal to redefine it for this type), or
5413 -- be a previous definition of the attribute for the same type (in
5414 -- which case it is illegal).
5415
5416 -- In the first case, it will have been analyzed already, and we
5417 -- can check that its profile does not match the expected profile
5418 -- for a stream attribute of U_Ent. In the second case, either Pnam
5419 -- has been analyzed (and has the expected profile), or it has not
5420 -- been analyzed yet (case of a type that has not been frozen yet
5421 -- and for which the stream attribute has been set using Set_TSS).
5422
5423 if Present (Pnam)
5424 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
5425 then
5426 Error_Msg_Sloc := Sloc (Pnam);
5427 Error_Msg_Name_1 := Attr;
5428 Error_Msg_N ("% attribute already defined #", Nam);
5429 return;
5430 end if;
5431
5432 Analyze (Expr);
5433
5434 if Is_Entity_Name (Expr) then
5435 if not Is_Overloaded (Expr) then
5436 if Has_Good_Profile (Entity (Expr), Report => True) then
5437 Subp := Entity (Expr);
5438 end if;
5439
5440 else
5441 Get_First_Interp (Expr, I, It);
5442 while Present (It.Nam) loop
5443 if Has_Good_Profile (It.Nam) then
5444 Subp := It.Nam;
5445 exit;
5446 end if;
5447
5448 Get_Next_Interp (I, It);
5449 end loop;
5450 end if;
5451 end if;
5452
5453 if Present (Subp) then
5454 if Is_Abstract_Subprogram (Subp) then
5455 Error_Msg_N ("stream subprogram must not be abstract", Expr);
5456 return;
5457
5458 -- A stream subprogram for an interface type must be a null
5459 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
5460 -- of an interface is not an interface type (3.9.4 (6.b/2)).
5461
5462 elsif Is_Interface (U_Ent)
5463 and then not Is_Class_Wide_Type (U_Ent)
5464 and then not Inside_A_Generic
5465 and then
5466 (Ekind (Subp) = E_Function
5467 or else
5468 not Null_Present
5469 (Specification
5470 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
5471 then
5472 Error_Msg_N
5473 ("stream subprogram for interface type must be null "
5474 & "procedure", Expr);
5475 end if;
5476
5477 Set_Entity (Expr, Subp);
5478 Set_Etype (Expr, Etype (Subp));
5479
5480 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
5481
5482 else
5483 Error_Msg_Name_1 := Attr;
5484
5485 if Is_Class_Wide_Type (Base_Type (Ent)) then
5486 Error_Msg_N
5487 ("incorrect expression for class-wide% attribute", Expr);
5488 else
5489 Error_Msg_N ("incorrect expression for% attribute", Expr);
5490 end if;
5491 end if;
5492 end Analyze_Stream_TSS_Definition;
5493
5494 ------------------------------
5495 -- Check_Indexing_Functions --
5496 ------------------------------
5497
5498 procedure Check_Indexing_Functions is
5499 Indexing_Found : Boolean := False;
5500
5501 procedure Check_Inherited_Indexing;
5502 -- For a derived type, check that for a derived type, a specification
5503 -- of an indexing aspect can only be confirming, i.e. uses the same
5504 -- name as in the parent type.
5505 -- AI12-0160: Verify that an indexing cannot be specified for
5506 -- a derived type unless it is specified for the parent.
5507
5508 procedure Check_One_Function (Subp : Entity_Id);
5509 -- Check one possible interpretation. Sets Indexing_Found True if a
5510 -- legal indexing function is found.
5511
5512 procedure Illegal_Indexing (Msg : String);
5513 -- Diagnose illegal indexing function if not overloaded. In the
5514 -- overloaded case indicate that no legal interpretation exists.
5515
5516 ------------------------------
5517 -- Check_Inherited_Indexing --
5518 ------------------------------
5519
5520 procedure Check_Inherited_Indexing is
5521 Inherited : Node_Id;
5522 Other_Indexing : Node_Id;
5523
5524 begin
5525 if Attr = Name_Constant_Indexing then
5526 Inherited :=
5527 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5528 Other_Indexing :=
5529 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5530
5531 else pragma Assert (Attr = Name_Variable_Indexing);
5532 Inherited :=
5533 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5534 Other_Indexing :=
5535 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5536 end if;
5537
5538 if Present (Inherited) then
5539 if Debug_Flag_Dot_XX then
5540 null;
5541
5542 -- OK if current attribute_definition_clause is expansion of
5543 -- inherited aspect.
5544
5545 elsif Aspect_Rep_Item (Inherited) = N then
5546 null;
5547
5548 -- Check if this is a confirming specification. The name
5549 -- may be overloaded between the parent operation and the
5550 -- inherited one, so we check that the Chars fields match.
5551
5552 elsif Is_Entity_Name (Expression (Inherited))
5553 and then Chars (Entity (Expression (Inherited))) =
5554 Chars (Entity (Expression (N)))
5555 then
5556 Indexing_Found := True;
5557
5558 -- Indicate the operation that must be overridden, rather than
5559 -- redefining the indexing aspect.
5560
5561 else
5562 Illegal_Indexing
5563 ("indexing function already inherited from parent type");
5564 Error_Msg_NE
5565 ("!override & instead",
5566 N, Entity (Expression (Inherited)));
5567 end if;
5568
5569 -- If not inherited and the parent has another indexing function
5570 -- this is illegal, because it leads to inconsistent results in
5571 -- class-wide calls.
5572
5573 elsif Present (Other_Indexing) then
5574 Error_Msg_N
5575 ("cannot specify indexing operation on derived type"
5576 & " if not specified for parent", N);
5577 end if;
5578 end Check_Inherited_Indexing;
5579
5580 ------------------------
5581 -- Check_One_Function --
5582 ------------------------
5583
5584 procedure Check_One_Function (Subp : Entity_Id) is
5585 Default_Element : Node_Id;
5586 Ret_Type : constant Entity_Id := Etype (Subp);
5587
5588 begin
5589 if not Is_Overloadable (Subp) then
5590 Illegal_Indexing ("illegal indexing function for type&");
5591 return;
5592
5593 elsif Scope (Subp) /= Scope (Ent) then
5594 if Nkind (Expr) = N_Expanded_Name then
5595
5596 -- Indexing function can't be declared elsewhere
5597
5598 Illegal_Indexing
5599 ("indexing function must be declared"
5600 & " in scope of type&");
5601 end if;
5602
5603 if Is_Derived_Type (Ent) then
5604 Check_Inherited_Indexing;
5605 end if;
5606
5607 return;
5608
5609 elsif No (First_Formal (Subp)) then
5610 Illegal_Indexing
5611 ("Indexing requires a function that applies to type&");
5612 return;
5613
5614 elsif No (Next_Formal (First_Formal (Subp))) then
5615 Illegal_Indexing
5616 ("indexing function must have at least two parameters");
5617 return;
5618
5619 elsif Is_Derived_Type (Ent) then
5620 Check_Inherited_Indexing;
5621 end if;
5622
5623 if not Check_Primitive_Function (Subp) then
5624 Illegal_Indexing
5625 ("Indexing aspect requires a function that applies to type&");
5626 return;
5627 end if;
5628
5629 -- If partial declaration exists, verify that it is not tagged.
5630
5631 if Ekind (Current_Scope) = E_Package
5632 and then Has_Private_Declaration (Ent)
5633 and then From_Aspect_Specification (N)
5634 and then
5635 List_Containing (Parent (Ent)) =
5636 Private_Declarations
5637 (Specification (Unit_Declaration_Node (Current_Scope)))
5638 and then Nkind (N) = N_Attribute_Definition_Clause
5639 then
5640 declare
5641 Decl : Node_Id;
5642
5643 begin
5644 Decl :=
5645 First (Visible_Declarations
5646 (Specification
5647 (Unit_Declaration_Node (Current_Scope))));
5648
5649 while Present (Decl) loop
5650 if Nkind (Decl) = N_Private_Type_Declaration
5651 and then Ent = Full_View (Defining_Identifier (Decl))
5652 and then Tagged_Present (Decl)
5653 and then No (Aspect_Specifications (Decl))
5654 then
5655 Illegal_Indexing
5656 ("Indexing aspect cannot be specified on full view "
5657 & "if partial view is tagged");
5658 return;
5659 end if;
5660
5661 Next (Decl);
5662 end loop;
5663 end;
5664 end if;
5665
5666 -- An indexing function must return either the default element of
5667 -- the container, or a reference type. For variable indexing it
5668 -- must be the latter.
5669
5670 Default_Element :=
5671 Find_Value_Of_Aspect
5672 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
5673
5674 if Present (Default_Element) then
5675 Analyze (Default_Element);
5676 end if;
5677
5678 -- For variable_indexing the return type must be a reference type
5679
5680 if Attr = Name_Variable_Indexing then
5681 if not Has_Implicit_Dereference (Ret_Type) then
5682 Illegal_Indexing
5683 ("variable indexing must return a reference type");
5684 return;
5685
5686 elsif Is_Access_Constant
5687 (Etype (First_Discriminant (Ret_Type)))
5688 then
5689 Illegal_Indexing
5690 ("variable indexing must return an access to variable");
5691 return;
5692 end if;
5693
5694 else
5695 if Has_Implicit_Dereference (Ret_Type)
5696 and then not
5697 Is_Access_Constant
5698 (Etype (Get_Reference_Discriminant (Ret_Type)))
5699 then
5700 Illegal_Indexing
5701 ("constant indexing must return an access to constant");
5702 return;
5703
5704 elsif Is_Access_Type (Etype (First_Formal (Subp)))
5705 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
5706 then
5707 Illegal_Indexing
5708 ("constant indexing must apply to an access to constant");
5709 return;
5710 end if;
5711 end if;
5712
5713 -- All checks succeeded
5714
5715 Indexing_Found := True;
5716 end Check_One_Function;
5717
5718 -----------------------
5719 -- Illegal_Indexing --
5720 -----------------------
5721
5722 procedure Illegal_Indexing (Msg : String) is
5723 begin
5724 Error_Msg_NE (Msg, N, Ent);
5725 end Illegal_Indexing;
5726
5727 -- Start of processing for Check_Indexing_Functions
5728
5729 begin
5730 if In_Instance then
5731 Check_Inherited_Indexing;
5732 end if;
5733
5734 Analyze (Expr);
5735
5736 if not Is_Overloaded (Expr) then
5737 Check_One_Function (Entity (Expr));
5738
5739 else
5740 declare
5741 I : Interp_Index;
5742 It : Interp;
5743
5744 begin
5745 Indexing_Found := False;
5746 Get_First_Interp (Expr, I, It);
5747 while Present (It.Nam) loop
5748
5749 -- Note that analysis will have added the interpretation
5750 -- that corresponds to the dereference. We only check the
5751 -- subprogram itself. Ignore homonyms that may come from
5752 -- derived types in the context.
5753
5754 if Is_Overloadable (It.Nam)
5755 and then Comes_From_Source (It.Nam)
5756 then
5757 Check_One_Function (It.Nam);
5758 end if;
5759
5760 Get_Next_Interp (I, It);
5761 end loop;
5762 end;
5763 end if;
5764
5765 if not Indexing_Found and then not Error_Posted (N) then
5766 Error_Msg_NE
5767 ("aspect Indexing requires a local function that applies to "
5768 & "type&", Expr, Ent);
5769 end if;
5770 end Check_Indexing_Functions;
5771
5772 ------------------------------
5773 -- Check_Iterator_Functions --
5774 ------------------------------
5775
5776 procedure Check_Iterator_Functions is
5777 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
5778 -- Check one possible interpretation for validity
5779
5780 ----------------------------
5781 -- Valid_Default_Iterator --
5782 ----------------------------
5783
5784 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
5785 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
5786 Formal : Entity_Id;
5787
5788 begin
5789 if not Check_Primitive_Function (Subp) then
5790 return False;
5791
5792 -- The return type must be derived from a type in an instance
5793 -- of Iterator.Interfaces, and thus its root type must have a
5794 -- predefined name.
5795
5796 elsif Chars (Root_T) /= Name_Forward_Iterator
5797 and then Chars (Root_T) /= Name_Reversible_Iterator
5798 then
5799 return False;
5800
5801 else
5802 Formal := First_Formal (Subp);
5803 end if;
5804
5805 -- False if any subsequent formal has no default expression
5806
5807 Next_Formal (Formal);
5808 while Present (Formal) loop
5809 if No (Expression (Parent (Formal))) then
5810 return False;
5811 end if;
5812
5813 Next_Formal (Formal);
5814 end loop;
5815
5816 -- True if all subsequent formals have default expressions
5817
5818 return True;
5819 end Valid_Default_Iterator;
5820
5821 -- Start of processing for Check_Iterator_Functions
5822
5823 begin
5824 Analyze (Expr);
5825
5826 if not Is_Entity_Name (Expr) then
5827 Error_Msg_N ("aspect Iterator must be a function name", Expr);
5828 end if;
5829
5830 if not Is_Overloaded (Expr) then
5831 if Entity (Expr) /= Any_Id
5832 and then not Check_Primitive_Function (Entity (Expr))
5833 then
5834 Error_Msg_NE
5835 ("aspect Indexing requires a function that applies to type&",
5836 Entity (Expr), Ent);
5837 end if;
5838
5839 -- Flag the default_iterator as well as the denoted function.
5840
5841 if not Valid_Default_Iterator (Entity (Expr)) then
5842 Error_Msg_N ("improper function for default iterator!", Expr);
5843 end if;
5844
5845 else
5846 declare
5847 Default : Entity_Id := Empty;
5848 I : Interp_Index;
5849 It : Interp;
5850
5851 begin
5852 Get_First_Interp (Expr, I, It);
5853 while Present (It.Nam) loop
5854 if not Check_Primitive_Function (It.Nam)
5855 or else not Valid_Default_Iterator (It.Nam)
5856 then
5857 Remove_Interp (I);
5858
5859 elsif Present (Default) then
5860
5861 -- An explicit one should override an implicit one
5862
5863 if Comes_From_Source (Default) =
5864 Comes_From_Source (It.Nam)
5865 then
5866 Error_Msg_N ("default iterator must be unique", Expr);
5867 Error_Msg_Sloc := Sloc (Default);
5868 Error_Msg_N ("\\possible interpretation#", Expr);
5869 Error_Msg_Sloc := Sloc (It.Nam);
5870 Error_Msg_N ("\\possible interpretation#", Expr);
5871
5872 elsif Comes_From_Source (It.Nam) then
5873 Default := It.Nam;
5874 end if;
5875 else
5876 Default := It.Nam;
5877 end if;
5878
5879 Get_Next_Interp (I, It);
5880 end loop;
5881
5882 if Present (Default) then
5883 Set_Entity (Expr, Default);
5884 Set_Is_Overloaded (Expr, False);
5885 else
5886 Error_Msg_N
5887 ("no interpretation is a valid default iterator!", Expr);
5888 end if;
5889 end;
5890 end if;
5891 end Check_Iterator_Functions;
5892
5893 -------------------------------
5894 -- Check_Primitive_Function --
5895 -------------------------------
5896
5897 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
5898 Ctrl : Entity_Id;
5899
5900 begin
5901 if Ekind (Subp) /= E_Function then
5902 return False;
5903 end if;
5904
5905 if No (First_Formal (Subp)) then
5906 return False;
5907 else
5908 Ctrl := Etype (First_Formal (Subp));
5909 end if;
5910
5911 -- To be a primitive operation subprogram has to be in same scope.
5912
5913 if Scope (Ctrl) /= Scope (Subp) then
5914 return False;
5915 end if;
5916
5917 -- Type of formal may be the class-wide type, an access to such,
5918 -- or an incomplete view.
5919
5920 if Ctrl = Ent
5921 or else Ctrl = Class_Wide_Type (Ent)
5922 or else
5923 (Ekind (Ctrl) = E_Anonymous_Access_Type
5924 and then (Designated_Type (Ctrl) = Ent
5925 or else
5926 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
5927 or else
5928 (Ekind (Ctrl) = E_Incomplete_Type
5929 and then Full_View (Ctrl) = Ent)
5930 then
5931 null;
5932 else
5933 return False;
5934 end if;
5935
5936 return True;
5937 end Check_Primitive_Function;
5938
5939 ----------------------
5940 -- Duplicate_Clause --
5941 ----------------------
5942
5943 function Duplicate_Clause return Boolean is
5944
5945 function Check_One_Attr (Attr_1, Attr_2 : Name_Id) return Boolean;
5946 -- Check for one attribute; Attr_1 is the attribute_designator we are
5947 -- looking for. Attr_2 is the attribute_designator of the current
5948 -- node. Normally, this is called just once by Duplicate_Clause, with
5949 -- Attr_1 = Attr_2. However, it needs to be called twice for Size and
5950 -- Value_Size, because these mean the same thing. For compatibility,
5951 -- we allow specifying both Size and Value_Size, but only if the two
5952 -- sizes are equal.
5953
5954 --------------------
5955 -- Check_One_Attr --
5956 --------------------
5957
5958 function Check_One_Attr (Attr_1, Attr_2 : Name_Id) return Boolean is
5959 A : constant Node_Id :=
5960 Get_Rep_Item (U_Ent, Attr_1, Check_Parents => False);
5961 begin
5962 if Present (A) then
5963 if Attr_1 = Attr_2 then
5964 Error_Msg_Name_1 := Attr_1;
5965 Error_Msg_Sloc := Sloc (A);
5966 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
5967
5968 else
5969 pragma Assert (Attr_1 in Name_Size | Name_Value_Size);
5970 pragma Assert (Attr_2 in Name_Size | Name_Value_Size);
5971
5972 Error_Msg_Name_1 := Attr_2;
5973 Error_Msg_Name_2 := Attr_1;
5974 Error_Msg_Sloc := Sloc (A);
5975 Error_Msg_NE ("?% for & conflicts with % #", N, U_Ent);
5976 end if;
5977
5978 return True;
5979 end if;
5980
5981 return False;
5982 end Check_One_Attr;
5983
5984 -- Start of processing for Duplicate_Clause
5985
5986 begin
5987 -- Nothing to do if this attribute definition clause comes from
5988 -- an aspect specification, since we could not be duplicating an
5989 -- explicit clause, and we dealt with the case of duplicated aspects
5990 -- in Analyze_Aspect_Specifications.
5991
5992 if From_Aspect_Specification (N) then
5993 return False;
5994 end if;
5995
5996 -- Special cases for Size and Value_Size
5997
5998 if (Chars (N) = Name_Size
5999 and then Check_One_Attr (Name_Value_Size, Name_Size))
6000 or else
6001 (Chars (N) = Name_Value_Size
6002 and then Check_One_Attr (Name_Size, Name_Value_Size))
6003 then
6004 return True;
6005 end if;
6006
6007 -- Normal case (including Size and Value_Size)
6008
6009 return Check_One_Attr (Chars (N), Chars (N));
6010 end Duplicate_Clause;
6011
6012 -- Start of processing for Analyze_Attribute_Definition_Clause
6013
6014 begin
6015 -- The following code is a defense against recursion. Not clear that
6016 -- this can happen legitimately, but perhaps some error situations can
6017 -- cause it, and we did see this recursion during testing.
6018
6019 if Analyzed (N) then
6020 return;
6021 else
6022 Set_Analyzed (N, True);
6023 end if;
6024
6025 Check_Restriction_No_Use_Of_Attribute (N);
6026
6027 if Is_Aspect_Id (Chars (N)) then
6028 -- 6.1/3 No_Specification_of_Aspect: Identifies an aspect for which
6029 -- no aspect_specification, attribute_definition_clause, or pragma
6030 -- is given.
6031 Check_Restriction_No_Specification_Of_Aspect (N);
6032 end if;
6033
6034 -- Ignore some selected attributes in CodePeer mode since they are not
6035 -- relevant in this context.
6036
6037 if CodePeer_Mode then
6038 case Id is
6039
6040 -- Ignore Component_Size in CodePeer mode, to avoid changing the
6041 -- internal representation of types by implicitly packing them.
6042
6043 when Attribute_Component_Size =>
6044 Rewrite (N, Make_Null_Statement (Sloc (N)));
6045 return;
6046
6047 when others =>
6048 null;
6049 end case;
6050 end if;
6051
6052 -- Process Ignore_Rep_Clauses option
6053
6054 if Ignore_Rep_Clauses then
6055 case Id is
6056
6057 -- The following should be ignored. They do not affect legality
6058 -- and may be target dependent. The basic idea of -gnatI is to
6059 -- ignore any rep clauses that may be target dependent but do not
6060 -- affect legality (except possibly to be rejected because they
6061 -- are incompatible with the compilation target).
6062
6063 when Attribute_Alignment
6064 | Attribute_Bit_Order
6065 | Attribute_Component_Size
6066 | Attribute_Default_Scalar_Storage_Order
6067 | Attribute_Machine_Radix
6068 | Attribute_Object_Size
6069 | Attribute_Scalar_Storage_Order
6070 | Attribute_Size
6071 | Attribute_Small
6072 | Attribute_Stream_Size
6073 | Attribute_Value_Size
6074 =>
6075 Kill_Rep_Clause (N);
6076 return;
6077
6078 -- The following should not be ignored, because in the first place
6079 -- they are reasonably portable, and should not cause problems
6080 -- in compiling code from another target, and also they do affect
6081 -- legality, e.g. failing to provide a stream attribute for a type
6082 -- may make a program illegal.
6083
6084 when Attribute_External_Tag
6085 | Attribute_Input
6086 | Attribute_Output
6087 | Attribute_Put_Image
6088 | Attribute_Read
6089 | Attribute_Simple_Storage_Pool
6090 | Attribute_Storage_Pool
6091 | Attribute_Storage_Size
6092 | Attribute_Write
6093 =>
6094 null;
6095
6096 -- We do not do anything here with address clauses, they will be
6097 -- removed by Freeze later on, but for now, it works better to
6098 -- keep them in the tree.
6099
6100 when Attribute_Address =>
6101 null;
6102
6103 -- Other cases are errors ("attribute& cannot be set with
6104 -- definition clause"), which will be caught below.
6105
6106 when others =>
6107 null;
6108 end case;
6109 end if;
6110
6111 Analyze (Nam);
6112 Ent := Entity (Nam);
6113
6114 if Rep_Item_Too_Early (Ent, N) then
6115 return;
6116 end if;
6117
6118 -- Rep clause applies to (underlying) full view of private or incomplete
6119 -- type if we have one (if not, this is a premature use of the type).
6120 -- However, some semantic checks need to be done on the specified entity
6121 -- i.e. the private view, so we save it in Ent.
6122
6123 if Is_Private_Type (Ent)
6124 and then Is_Derived_Type (Ent)
6125 and then not Is_Tagged_Type (Ent)
6126 and then No (Full_View (Ent))
6127 and then No (Underlying_Full_View (Ent))
6128 then
6129 U_Ent := Ent;
6130
6131 elsif Ekind (Ent) = E_Incomplete_Type then
6132
6133 -- The attribute applies to the full view, set the entity of the
6134 -- attribute definition accordingly.
6135
6136 Ent := Underlying_Type (Ent);
6137 U_Ent := Ent;
6138 Set_Entity (Nam, Ent);
6139
6140 else
6141 U_Ent := Underlying_Type (Ent);
6142 end if;
6143
6144 -- Avoid cascaded error
6145
6146 if Etype (Nam) = Any_Type then
6147 return;
6148
6149 -- Must be declared in current scope or in case of an aspect
6150 -- specification, must be visible in current scope.
6151
6152 elsif Scope (Ent) /= Current_Scope
6153 and then
6154 not (From_Aspect_Specification (N)
6155 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
6156 then
6157 Error_Msg_N ("entity must be declared in this scope", Nam);
6158 return;
6159
6160 -- Must not be a source renaming (we do have some cases where the
6161 -- expander generates a renaming, and those cases are OK, in such
6162 -- cases any attribute applies to the renamed object as well).
6163
6164 elsif Is_Object (Ent)
6165 and then Present (Renamed_Object (Ent))
6166 then
6167 -- In the case of a renamed object from source, this is an error
6168 -- unless the object is an aggregate and the renaming is created
6169 -- for an object declaration.
6170
6171 if Comes_From_Source (Renamed_Object (Ent))
6172 and then Nkind (Renamed_Object (Ent)) /= N_Aggregate
6173 then
6174 Get_Name_String (Chars (N));
6175 Error_Msg_Strlen := Name_Len;
6176 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
6177 Error_Msg_N
6178 ("~ clause not allowed for a renaming declaration "
6179 & "(RM 13.1(6))", Nam);
6180 return;
6181
6182 -- For the case of a compiler generated renaming, the attribute
6183 -- definition clause applies to the renamed object created by the
6184 -- expander. The easiest general way to handle this is to create a
6185 -- copy of the attribute definition clause for this object.
6186
6187 elsif Is_Entity_Name (Renamed_Object (Ent)) then
6188 Insert_Action (N,
6189 Make_Attribute_Definition_Clause (Loc,
6190 Name =>
6191 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
6192 Chars => Chars (N),
6193 Expression => Duplicate_Subexpr (Expression (N))));
6194
6195 -- If the renamed object is not an entity, it must be a dereference
6196 -- of an unconstrained function call, and we must introduce a new
6197 -- declaration to capture the expression. This is needed in the case
6198 -- of 'Alignment, where the original declaration must be rewritten.
6199
6200 else
6201 pragma Assert
6202 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
6203 null;
6204 end if;
6205
6206 -- If no underlying entity, use entity itself, applies to some
6207 -- previously detected error cases ???
6208
6209 elsif No (U_Ent) then
6210 U_Ent := Ent;
6211
6212 -- Cannot specify for a subtype (exception Object/Value_Size)
6213
6214 elsif Is_Type (U_Ent)
6215 and then not Is_First_Subtype (U_Ent)
6216 and then Id /= Attribute_Object_Size
6217 and then Id /= Attribute_Value_Size
6218 and then not From_At_Mod (N)
6219 then
6220 Error_Msg_N ("cannot specify attribute for subtype", Nam);
6221 return;
6222 end if;
6223
6224 Set_Entity (N, U_Ent);
6225
6226 -- Switch on particular attribute
6227
6228 case Id is
6229
6230 -------------
6231 -- Address --
6232 -------------
6233
6234 -- Address attribute definition clause
6235
6236 when Attribute_Address => Address : begin
6237
6238 -- A little error check, catch for X'Address use X'Address;
6239
6240 if Nkind (Nam) = N_Identifier
6241 and then Nkind (Expr) = N_Attribute_Reference
6242 and then Attribute_Name (Expr) = Name_Address
6243 and then Nkind (Prefix (Expr)) = N_Identifier
6244 and then Chars (Nam) = Chars (Prefix (Expr))
6245 then
6246 Error_Msg_NE
6247 ("address for & is self-referencing", Prefix (Expr), Ent);
6248 return;
6249 end if;
6250
6251 -- Not that special case, carry on with analysis of expression
6252
6253 Analyze_And_Resolve (Expr, RTE (RE_Address));
6254
6255 -- Even when ignoring rep clauses we need to indicate that the
6256 -- entity has an address clause and thus it is legal to declare
6257 -- it imported. Freeze will get rid of the address clause later.
6258 -- Also call Set_Address_Taken to indicate that an address clause
6259 -- was present, even if we are about to remove it.
6260
6261 if Ignore_Rep_Clauses then
6262 Set_Address_Taken (U_Ent);
6263
6264 if Ekind (U_Ent) in E_Variable | E_Constant then
6265 Record_Rep_Item (U_Ent, N);
6266 end if;
6267
6268 return;
6269 end if;
6270
6271 if Duplicate_Clause then
6272 null;
6273
6274 -- Case of address clause for subprogram
6275
6276 elsif Is_Subprogram (U_Ent) then
6277 if Has_Homonym (U_Ent) then
6278 Error_Msg_N
6279 ("address clause cannot be given for overloaded "
6280 & "subprogram", Nam);
6281 return;
6282 end if;
6283
6284 -- For subprograms, all address clauses are permitted, and we
6285 -- mark the subprogram as having a deferred freeze so that Gigi
6286 -- will not elaborate it too soon.
6287
6288 -- Above needs more comments, what is too soon about???
6289
6290 Set_Has_Delayed_Freeze (U_Ent);
6291
6292 -- Case of address clause for entry
6293
6294 elsif Ekind (U_Ent) = E_Entry then
6295 if Nkind (Parent (N)) = N_Task_Body then
6296 Error_Msg_N
6297 ("entry address must be specified in task spec", Nam);
6298 return;
6299 end if;
6300
6301 -- For entries, we require a constant address
6302
6303 Check_Constant_Address_Clause (Expr, U_Ent);
6304
6305 -- Special checks for task types
6306
6307 if Is_Task_Type (Scope (U_Ent))
6308 and then Comes_From_Source (Scope (U_Ent))
6309 then
6310 Error_Msg_N
6311 ("??entry address declared for entry in task type", N);
6312 Error_Msg_N
6313 ("\??only one task can be declared of this type", N);
6314 end if;
6315
6316 -- Entry address clauses are obsolescent
6317
6318 Check_Restriction (No_Obsolescent_Features, N);
6319
6320 if Warn_On_Obsolescent_Feature then
6321 Error_Msg_N
6322 ("?j?attaching interrupt to task entry is an obsolescent "
6323 & "feature (RM J.7.1)", N);
6324 Error_Msg_N
6325 ("\?j?use interrupt procedure instead", N);
6326 end if;
6327
6328 -- Case of address clause for an object
6329
6330 elsif Ekind (U_Ent) in E_Constant | E_Variable then
6331
6332 -- Disallow case of an address clause for an object of an
6333 -- indefinite subtype which takes its bounds/discriminant/tag
6334 -- from its initial value. Without this, we get a Gigi
6335 -- assertion failure for things like
6336 -- X : String := Some_Function (...) with Address => ...;
6337 -- where the result subtype of the function is unconstrained.
6338 --
6339 -- We want to reject two cases: the class-wide case, and the
6340 -- case where the FE conjures up a renaming declaration and
6341 -- would then otherwise generate an address specification for
6342 -- that renaming (which is a malformed tree, which is why Gigi
6343 -- complains).
6344
6345 if Is_Class_Wide_Type (Etype (U_Ent)) then
6346 Error_Msg_N
6347 ("address specification not supported for class-wide " &
6348 "object declaration", Nam);
6349 return;
6350 elsif Is_Constr_Subt_For_U_Nominal (Etype (U_Ent))
6351 and then
6352 Nkind (Parent (U_Ent)) = N_Object_Renaming_Declaration
6353 then
6354 -- Confirm accuracy of " and dynamic size" message text
6355 -- before including it. We want to include that text when
6356 -- it is correct because it may be useful to the reader.
6357 -- The case where we omit that part of the message text
6358 -- might be dead code, but let's not rely on that.
6359
6360 Error_Msg_N
6361 ("address specification not supported for object " &
6362 "declaration with indefinite nominal subtype" &
6363 (if Size_Known_At_Compile_Time (Etype (U_Ent))
6364 then ""
6365 else " and dynamic size"), Nam);
6366 return;
6367 end if;
6368
6369 declare
6370 Expr : constant Node_Id := Expression (N);
6371 O_Ent : Entity_Id;
6372 Off : Boolean;
6373
6374 begin
6375 -- Exported variables cannot have an address clause, because
6376 -- this cancels the effect of the pragma Export.
6377
6378 if Is_Exported (U_Ent) then
6379 Error_Msg_N
6380 ("cannot export object with address clause", Nam);
6381 return;
6382 end if;
6383
6384 Find_Overlaid_Entity (N, O_Ent, Off);
6385
6386 if Present (O_Ent) then
6387
6388 -- If the object overlays a constant object, mark it so
6389
6390 if Is_Constant_Object (O_Ent) then
6391 Set_Overlays_Constant (U_Ent);
6392 end if;
6393
6394 -- If the address clause is of the form:
6395
6396 -- for X'Address use Y'Address;
6397
6398 -- or
6399
6400 -- C : constant Address := Y'Address;
6401 -- ...
6402 -- for X'Address use C;
6403
6404 -- then we make an entry in the table to check the size
6405 -- and alignment of the overlaying variable. But we defer
6406 -- this check till after code generation to take full
6407 -- advantage of the annotation done by the back end.
6408
6409 -- If the entity has a generic type, the check will be
6410 -- performed in the instance if the actual type justifies
6411 -- it, and we do not insert the clause in the table to
6412 -- prevent spurious warnings.
6413
6414 -- Note: we used to test Comes_From_Source and only give
6415 -- this warning for source entities, but we have removed
6416 -- this test. It really seems bogus to generate overlays
6417 -- that would trigger this warning in generated code.
6418 -- Furthermore, by removing the test, we handle the
6419 -- aspect case properly.
6420
6421 if Is_Object (O_Ent)
6422 and then not Is_Generic_Formal (O_Ent)
6423 and then not Is_Generic_Type (Etype (U_Ent))
6424 and then Address_Clause_Overlay_Warnings
6425 then
6426 Register_Address_Clause_Check
6427 (N, U_Ent, No_Uint, O_Ent, Off);
6428 end if;
6429
6430 -- If the overlay changes the storage order, warn since
6431 -- the construct is not really supported by the back end.
6432 -- Also mark the entity as being volatile to block the
6433 -- optimizer, even if there is no warranty on the result.
6434
6435 if (Is_Record_Type (Etype (U_Ent))
6436 or else Is_Array_Type (Etype (U_Ent)))
6437 and then (Is_Record_Type (Etype (O_Ent))
6438 or else Is_Array_Type (Etype (O_Ent)))
6439 and then Reverse_Storage_Order (Etype (U_Ent)) /=
6440 Reverse_Storage_Order (Etype (O_Ent))
6441 then
6442 Error_Msg_N
6443 ("??overlay changes scalar storage order", Expr);
6444 Set_Treat_As_Volatile (U_Ent);
6445 end if;
6446
6447 else
6448 -- If this is not an overlay, mark a variable as being
6449 -- volatile to prevent unwanted optimizations. It's a
6450 -- conservative interpretation of RM 13.3(19) for the
6451 -- cases where the compiler cannot detect potential
6452 -- aliasing issues easily and it also covers the case
6453 -- of an absolute address where the volatile aspect is
6454 -- kind of implicit.
6455
6456 if Ekind (U_Ent) = E_Variable then
6457 Set_Treat_As_Volatile (U_Ent);
6458 end if;
6459
6460 -- Make an entry in the table for an absolute address as
6461 -- above to check that the value is compatible with the
6462 -- alignment of the object.
6463
6464 declare
6465 Addr : constant Node_Id := Address_Value (Expr);
6466 begin
6467 if Compile_Time_Known_Value (Addr)
6468 and then Address_Clause_Overlay_Warnings
6469 then
6470 Register_Address_Clause_Check
6471 (N, U_Ent, Expr_Value (Addr), Empty, False);
6472 end if;
6473 end;
6474 end if;
6475
6476 -- Issue an unconditional warning for a constant overlaying
6477 -- a variable. For the reverse case, we will issue it only
6478 -- if the variable is modified.
6479 -- Within a generic unit an In_Parameter is a constant.
6480 -- It can be instantiated with a variable, in which case
6481 -- there will be a warning on the instance.
6482
6483 if Ekind (U_Ent) = E_Constant
6484 and then Present (O_Ent)
6485 and then Ekind (O_Ent) /= E_Generic_In_Parameter
6486 and then not Overlays_Constant (U_Ent)
6487 and then Address_Clause_Overlay_Warnings
6488 then
6489 Error_Msg_N ("?o?constant overlays a variable", Expr);
6490
6491 -- Imported variables can have an address clause, but then
6492 -- the import is pretty meaningless except to suppress
6493 -- initializations, so we do not need such variables to
6494 -- be statically allocated (and in fact it causes trouble
6495 -- if the address clause is a local value).
6496
6497 elsif Is_Imported (U_Ent) then
6498 Set_Is_Statically_Allocated (U_Ent, False);
6499 end if;
6500
6501 -- We mark a possible modification of a variable with an
6502 -- address clause, since it is likely aliasing is occurring.
6503
6504 Note_Possible_Modification (Nam, Sure => False);
6505
6506 -- Legality checks on the address clause for initialized
6507 -- objects is deferred until the freeze point, because
6508 -- a subsequent pragma might indicate that the object
6509 -- is imported and thus not initialized. Also, the address
6510 -- clause might involve entities that have yet to be
6511 -- elaborated.
6512
6513 Set_Has_Delayed_Freeze (U_Ent);
6514
6515 -- If an initialization call has been generated for this
6516 -- object, it needs to be deferred to after the freeze node
6517 -- we have just now added, otherwise GIGI will see a
6518 -- reference to the variable (as actual to the IP call)
6519 -- before its definition.
6520
6521 declare
6522 Init_Call : constant Node_Id :=
6523 Remove_Init_Call (U_Ent, N);
6524
6525 begin
6526 if Present (Init_Call) then
6527 Append_Freeze_Action (U_Ent, Init_Call);
6528
6529 -- Reset Initialization_Statements pointer so that
6530 -- if there is a pragma Import further down, it can
6531 -- clear any default initialization.
6532
6533 Set_Initialization_Statements (U_Ent, Init_Call);
6534 end if;
6535 end;
6536
6537 -- Entity has delayed freeze, so we will generate an
6538 -- alignment check at the freeze point unless suppressed.
6539
6540 if not Range_Checks_Suppressed (U_Ent)
6541 and then not Alignment_Checks_Suppressed (U_Ent)
6542 then
6543 Set_Check_Address_Alignment (N);
6544 end if;
6545
6546 -- Kill the size check code, since we are not allocating
6547 -- the variable, it is somewhere else.
6548
6549 Kill_Size_Check_Code (U_Ent);
6550 end;
6551
6552 -- Not a valid entity for an address clause
6553
6554 else
6555 Error_Msg_N ("address cannot be given for &", Nam);
6556 end if;
6557 end Address;
6558
6559 ---------------
6560 -- Alignment --
6561 ---------------
6562
6563 -- Alignment attribute definition clause
6564
6565 when Attribute_Alignment => Alignment : declare
6566 Align : constant Uint := Get_Alignment_Value (Expr);
6567 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
6568
6569 begin
6570 FOnly := True;
6571
6572 if not Is_Type (U_Ent)
6573 and then Ekind (U_Ent) /= E_Variable
6574 and then Ekind (U_Ent) /= E_Constant
6575 then
6576 Error_Msg_N ("alignment cannot be given for &", Nam);
6577
6578 elsif Duplicate_Clause then
6579 null;
6580
6581 elsif Present (Align) then
6582 Set_Has_Alignment_Clause (U_Ent);
6583
6584 -- Tagged type case, check for attempt to set alignment to a
6585 -- value greater than Max_Align, and reset if so.
6586
6587 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
6588 Error_Msg_N
6589 ("alignment for & set to Maximum_Aligment??", Nam);
6590 Set_Alignment (U_Ent, Max_Align);
6591
6592 -- All other cases
6593
6594 else
6595 Set_Alignment (U_Ent, Align);
6596 end if;
6597
6598 -- For an array type, U_Ent is the first subtype. In that case,
6599 -- also set the alignment of the anonymous base type so that
6600 -- other subtypes (such as the itypes for aggregates of the
6601 -- type) also receive the expected alignment.
6602
6603 if Is_Array_Type (U_Ent) then
6604 Set_Alignment (Base_Type (U_Ent), Align);
6605 end if;
6606 end if;
6607 end Alignment;
6608
6609 ---------------
6610 -- Bit_Order --
6611 ---------------
6612
6613 -- Bit_Order attribute definition clause
6614
6615 when Attribute_Bit_Order =>
6616 if not Is_Record_Type (U_Ent) then
6617 Error_Msg_N
6618 ("Bit_Order can only be defined for record type", Nam);
6619
6620 elsif Is_Tagged_Type (U_Ent) and then Is_Derived_Type (U_Ent) then
6621 Error_Msg_N
6622 ("Bit_Order cannot be defined for record extensions", Nam);
6623
6624 elsif Duplicate_Clause then
6625 null;
6626
6627 else
6628 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
6629
6630 if Etype (Expr) = Any_Type then
6631 return;
6632
6633 elsif not Is_OK_Static_Expression (Expr) then
6634 Flag_Non_Static_Expr
6635 ("Bit_Order requires static expression!", Expr);
6636
6637 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
6638 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
6639 end if;
6640 end if;
6641
6642 --------------------
6643 -- Component_Size --
6644 --------------------
6645
6646 -- Component_Size attribute definition clause
6647
6648 when Attribute_Component_Size => Component_Size_Case : declare
6649 Csize : constant Uint := Static_Integer (Expr);
6650 Ctyp : Entity_Id;
6651 Btype : Entity_Id;
6652 Biased : Boolean;
6653 New_Ctyp : Entity_Id;
6654 Decl : Node_Id;
6655
6656 begin
6657 if not Is_Array_Type (U_Ent) then
6658 Error_Msg_N ("component size requires array type", Nam);
6659 return;
6660 end if;
6661
6662 Btype := Base_Type (U_Ent);
6663 Ctyp := Component_Type (Btype);
6664
6665 if Duplicate_Clause then
6666 null;
6667
6668 elsif Rep_Item_Too_Early (Btype, N) then
6669 null;
6670
6671 elsif Present (Csize) then
6672 Check_Size (Expr, Ctyp, Csize, Biased);
6673
6674 -- For the biased case, build a declaration for a subtype that
6675 -- will be used to represent the biased subtype that reflects
6676 -- the biased representation of components. We need the subtype
6677 -- to get proper conversions on referencing elements of the
6678 -- array.
6679
6680 if Biased then
6681 New_Ctyp :=
6682 Make_Defining_Identifier (Loc,
6683 Chars =>
6684 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
6685
6686 Decl :=
6687 Make_Subtype_Declaration (Loc,
6688 Defining_Identifier => New_Ctyp,
6689 Subtype_Indication =>
6690 New_Occurrence_Of (Component_Type (Btype), Loc));
6691
6692 Set_Parent (Decl, N);
6693 Analyze (Decl, Suppress => All_Checks);
6694
6695 Set_Has_Delayed_Freeze (New_Ctyp, False);
6696 Reinit_Esize (New_Ctyp);
6697 Set_RM_Size (New_Ctyp, Csize);
6698 Reinit_Alignment (New_Ctyp);
6699 Set_Is_Itype (New_Ctyp, True);
6700 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
6701
6702 Set_Component_Type (Btype, New_Ctyp);
6703 Set_Biased (New_Ctyp, N, "component size clause");
6704 end if;
6705
6706 Set_Component_Size (Btype, Csize);
6707
6708 -- Deal with warning on overridden size
6709
6710 if Warn_On_Overridden_Size
6711 and then Has_Size_Clause (Ctyp)
6712 and then RM_Size (Ctyp) /= Csize
6713 then
6714 Error_Msg_NE
6715 ("component size overrides size clause for&?.s?", N, Ctyp);
6716 end if;
6717
6718 Set_Has_Component_Size_Clause (Btype, True);
6719 Set_Has_Non_Standard_Rep (Btype, True);
6720 end if;
6721 end Component_Size_Case;
6722
6723 -----------------------
6724 -- Constant_Indexing --
6725 -----------------------
6726
6727 when Attribute_Constant_Indexing =>
6728 Check_Indexing_Functions;
6729
6730 ---------
6731 -- CPU --
6732 ---------
6733
6734 when Attribute_CPU =>
6735 pragma Assert (From_Aspect_Specification (N));
6736 -- The parser forbids this clause in source code, so it must have
6737 -- come from an aspect specification.
6738
6739 if not Is_Task_Type (U_Ent) then
6740 Error_Msg_N ("'C'P'U can only be defined for task", Nam);
6741
6742 elsif Duplicate_Clause then
6743 null;
6744
6745 else
6746 -- The expression must be analyzed in the special manner
6747 -- described in "Handling of Default and Per-Object
6748 -- Expressions" in sem.ads.
6749
6750 -- The visibility to the components must be established
6751 -- and restored before and after analysis.
6752
6753 Push_Type (U_Ent);
6754 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
6755 Pop_Type (U_Ent);
6756
6757 -- AI12-0117-1, "Restriction No_Tasks_Unassigned_To_CPU":
6758 -- If the expression is static, and its value is
6759 -- System.Multiprocessors.Not_A_Specific_CPU (i.e. zero) then
6760 -- that's a violation of No_Tasks_Unassigned_To_CPU. It might
6761 -- seem better to refer to Not_A_Specific_CPU here, but that
6762 -- involves a lot of horsing around with Rtsfind, and this
6763 -- value is not going to change, so it's better to hardwire
6764 -- Uint_0.
6765 --
6766 -- AI12-0055-1, "All properties of a usage profile are defined
6767 -- by pragmas": If the expression is nonstatic, that's a
6768 -- violation of No_Dynamic_CPU_Assignment.
6769
6770 if Is_OK_Static_Expression (Expr) then
6771 if Expr_Value (Expr) = Uint_0 then
6772 Check_Restriction (No_Tasks_Unassigned_To_CPU, Expr);
6773 end if;
6774 else
6775 Check_Restriction (No_Dynamic_CPU_Assignment, Expr);
6776 end if;
6777 end if;
6778
6779 ----------------------
6780 -- Default_Iterator --
6781 ----------------------
6782
6783 when Attribute_Default_Iterator => Default_Iterator : declare
6784 Func : Entity_Id;
6785 Typ : Entity_Id;
6786
6787 begin
6788 -- If target type is untagged, further checks are irrelevant
6789
6790 if not Is_Tagged_Type (U_Ent) then
6791 Error_Msg_N
6792 ("aspect Default_Iterator applies to tagged type", Nam);
6793 return;
6794 end if;
6795
6796 Check_Iterator_Functions;
6797
6798 Analyze (Expr);
6799
6800 if not Is_Entity_Name (Expr)
6801 or else Ekind (Entity (Expr)) /= E_Function
6802 then
6803 Error_Msg_N ("aspect Iterator must be a function", Expr);
6804 return;
6805 else
6806 Func := Entity (Expr);
6807 end if;
6808
6809 -- The type of the first parameter must be T, T'class, or a
6810 -- corresponding access type (5.5.1 (8/3). If function is
6811 -- parameterless label type accordingly.
6812
6813 if No (First_Formal (Func)) then
6814 Typ := Any_Type;
6815 else
6816 Typ := Etype (First_Formal (Func));
6817 end if;
6818
6819 if Typ = U_Ent
6820 or else Typ = Class_Wide_Type (U_Ent)
6821 or else (Is_Access_Type (Typ)
6822 and then Designated_Type (Typ) = U_Ent)
6823 or else (Is_Access_Type (Typ)
6824 and then Designated_Type (Typ) =
6825 Class_Wide_Type (U_Ent))
6826 then
6827 null;
6828
6829 else
6830 Error_Msg_NE
6831 ("Default_Iterator must be a primitive of&", Func, U_Ent);
6832 end if;
6833 end Default_Iterator;
6834
6835 ------------------------
6836 -- Dispatching_Domain --
6837 ------------------------
6838
6839 when Attribute_Dispatching_Domain =>
6840 pragma Assert (From_Aspect_Specification (N));
6841 -- The parser forbids this clause in source code, so it must have
6842 -- come from an aspect specification.
6843
6844 if not Is_Task_Type (U_Ent) then
6845 Error_Msg_N
6846 ("Dispatching_Domain can only be defined for task", Nam);
6847
6848 elsif Duplicate_Clause then
6849 null;
6850
6851 else
6852 -- The expression must be analyzed in the special manner
6853 -- described in "Handling of Default and Per-Object
6854 -- Expressions" in sem.ads.
6855
6856 -- The visibility to the components must be restored
6857
6858 Push_Type (U_Ent);
6859
6860 Preanalyze_Spec_Expression
6861 (Expr, RTE (RE_Dispatching_Domain));
6862
6863 Pop_Type (U_Ent);
6864 end if;
6865
6866 ------------------
6867 -- External_Tag --
6868 ------------------
6869
6870 when Attribute_External_Tag =>
6871 if not Is_Tagged_Type (U_Ent) then
6872 Error_Msg_N ("should be a tagged type", Nam);
6873 end if;
6874
6875 if Duplicate_Clause then
6876 null;
6877
6878 else
6879 Analyze_And_Resolve (Expr, Standard_String);
6880
6881 if not Is_OK_Static_Expression (Expr) then
6882 Flag_Non_Static_Expr
6883 ("static string required for tag name!", Nam);
6884 end if;
6885
6886 if not Is_Library_Level_Entity (U_Ent) then
6887 Error_Msg_NE
6888 ("??non-unique external tag supplied for &", N, U_Ent);
6889 Error_Msg_N
6890 ("\??same external tag applies to all subprogram calls",
6891 N);
6892 Error_Msg_N
6893 ("\??corresponding internal tag cannot be obtained", N);
6894 end if;
6895 end if;
6896
6897 --------------------------
6898 -- Implicit_Dereference --
6899 --------------------------
6900
6901 when Attribute_Implicit_Dereference =>
6902
6903 -- Legality checks already performed at the point of the type
6904 -- declaration, aspect is not delayed.
6905
6906 null;
6907
6908 -----------
6909 -- Input --
6910 -----------
6911
6912 when Attribute_Input =>
6913 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
6914 Set_Has_Specified_Stream_Input (Ent);
6915
6916 ------------------------
6917 -- Interrupt_Priority --
6918 ------------------------
6919
6920 when Attribute_Interrupt_Priority =>
6921 pragma Assert (From_Aspect_Specification (N));
6922 -- The parser forbids this clause in source code, so it must have
6923 -- come from an aspect specification.
6924
6925 if not Is_Concurrent_Type (U_Ent) then
6926 Error_Msg_N
6927 ("Interrupt_Priority can only be defined for task and "
6928 & "protected object", Nam);
6929
6930 elsif Duplicate_Clause then
6931 null;
6932
6933 else
6934 -- The expression must be analyzed in the special manner
6935 -- described in "Handling of Default and Per-Object
6936 -- Expressions" in sem.ads.
6937
6938 -- The visibility to the components must be restored
6939
6940 Push_Type (U_Ent);
6941
6942 Preanalyze_Spec_Expression
6943 (Expr, RTE (RE_Interrupt_Priority));
6944
6945 Pop_Type (U_Ent);
6946
6947 -- Check the No_Task_At_Interrupt_Priority restriction
6948
6949 if Is_Task_Type (U_Ent) then
6950 Check_Restriction (No_Task_At_Interrupt_Priority, N);
6951 end if;
6952 end if;
6953
6954 --------------
6955 -- Iterable --
6956 --------------
6957
6958 when Attribute_Iterable =>
6959 Analyze (Expr);
6960
6961 if Nkind (Expr) /= N_Aggregate then
6962 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
6963 return;
6964 end if;
6965
6966 declare
6967 Assoc : Node_Id;
6968
6969 begin
6970 Assoc := First (Component_Associations (Expr));
6971 while Present (Assoc) loop
6972 Analyze (Expression (Assoc));
6973
6974 if not Is_Entity_Name (Expression (Assoc))
6975 or else Ekind (Entity (Expression (Assoc))) /= E_Function
6976 then
6977 Error_Msg_N ("value must be a function", Assoc);
6978 end if;
6979
6980 Next (Assoc);
6981 end loop;
6982 end;
6983
6984 ----------------------
6985 -- Iterator_Element --
6986 ----------------------
6987
6988 when Attribute_Iterator_Element =>
6989 Analyze (Expr);
6990
6991 if not Is_Entity_Name (Expr)
6992 or else not Is_Type (Entity (Expr))
6993 then
6994 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
6995 return;
6996 end if;
6997
6998 -------------------
6999 -- Machine_Radix --
7000 -------------------
7001
7002 -- Machine radix attribute definition clause
7003
7004 when Attribute_Machine_Radix => Machine_Radix : declare
7005 Radix : constant Uint := Static_Integer (Expr);
7006
7007 begin
7008 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
7009 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
7010
7011 elsif Duplicate_Clause then
7012 null;
7013
7014 elsif Present (Radix) then
7015 Set_Has_Machine_Radix_Clause (U_Ent);
7016 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
7017
7018 if Radix = 2 then
7019 null;
7020
7021 elsif Radix = 10 then
7022 Set_Machine_Radix_10 (U_Ent);
7023
7024 else
7025 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
7026 end if;
7027 end if;
7028 end Machine_Radix;
7029
7030 -----------------
7031 -- Object_Size --
7032 -----------------
7033
7034 -- Object_Size attribute definition clause
7035
7036 when Attribute_Object_Size => Object_Size : declare
7037 Size : constant Uint := Static_Integer (Expr);
7038
7039 Biased : Boolean;
7040 pragma Warnings (Off, Biased);
7041
7042 begin
7043 if not Is_Type (U_Ent) then
7044 Error_Msg_N ("Object_Size cannot be given for &", Nam);
7045
7046 elsif Duplicate_Clause then
7047 null;
7048
7049 else
7050 Check_Size (Expr, U_Ent, Size, Biased);
7051
7052 if No (Size) or else Size <= 0 then
7053 Error_Msg_N ("Object_Size must be positive", Expr);
7054
7055 elsif Is_Scalar_Type (U_Ent) then
7056 if Size /= 8 and then Size /= 16 and then Size /= 32
7057 and then UI_Mod (Size, 64) /= 0
7058 then
7059 Error_Msg_N
7060 ("Object_Size must be 8, 16, 32, or multiple of 64",
7061 Expr);
7062 end if;
7063
7064 elsif Size mod 8 /= 0 then
7065 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
7066 end if;
7067
7068 Set_Esize (U_Ent, Size);
7069 Set_Has_Object_Size_Clause (U_Ent);
7070 Alignment_Check_For_Size_Change (U_Ent, Size);
7071 end if;
7072 end Object_Size;
7073
7074 ------------
7075 -- Output --
7076 ------------
7077
7078 when Attribute_Output =>
7079 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
7080 Set_Has_Specified_Stream_Output (Ent);
7081
7082 --------------
7083 -- Priority --
7084 --------------
7085
7086 when Attribute_Priority =>
7087
7088 -- Priority attribute definition clause not allowed except from
7089 -- aspect specification.
7090
7091 if From_Aspect_Specification (N) then
7092 if not (Is_Concurrent_Type (U_Ent)
7093 or else Ekind (U_Ent) = E_Procedure)
7094 then
7095 Error_Msg_N
7096 ("Priority can only be defined for task and protected "
7097 & "object", Nam);
7098
7099 elsif Duplicate_Clause then
7100 null;
7101
7102 else
7103 -- The expression must be analyzed in the special manner
7104 -- described in "Handling of Default and Per-Object
7105 -- Expressions" in sem.ads.
7106
7107 -- The visibility to the components must be restored
7108
7109 Push_Type (U_Ent);
7110 Preanalyze_Spec_Expression (Expr, Standard_Integer);
7111 Pop_Type (U_Ent);
7112
7113 if not Is_OK_Static_Expression (Expr) then
7114 Check_Restriction (Static_Priorities, Expr);
7115 end if;
7116 end if;
7117
7118 else
7119 Error_Msg_N
7120 ("attribute& cannot be set with definition clause", N);
7121 end if;
7122
7123 ---------------
7124 -- Put_Image --
7125 ---------------
7126
7127 when Attribute_Put_Image =>
7128 Analyze_Put_Image_TSS_Definition;
7129
7130 ----------
7131 -- Read --
7132 ----------
7133
7134 when Attribute_Read =>
7135 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
7136 Set_Has_Specified_Stream_Read (Ent);
7137
7138 --------------------------
7139 -- Scalar_Storage_Order --
7140 --------------------------
7141
7142 -- Scalar_Storage_Order attribute definition clause
7143
7144 when Attribute_Scalar_Storage_Order =>
7145 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
7146 Error_Msg_N
7147 ("Scalar_Storage_Order can only be defined for record or "
7148 & "array type", Nam);
7149
7150 elsif Duplicate_Clause then
7151 null;
7152
7153 else
7154 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
7155
7156 if Etype (Expr) = Any_Type then
7157 return;
7158
7159 elsif not Is_OK_Static_Expression (Expr) then
7160 Flag_Non_Static_Expr
7161 ("Scalar_Storage_Order requires static expression!", Expr);
7162
7163 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
7164
7165 -- Here for the case of a non-default (i.e. non-confirming)
7166 -- Scalar_Storage_Order attribute definition.
7167
7168 if Support_Nondefault_SSO_On_Target then
7169 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
7170 else
7171 Error_Msg_N
7172 ("non-default Scalar_Storage_Order not supported on "
7173 & "target", Expr);
7174 end if;
7175 end if;
7176
7177 -- Clear SSO default indications since explicit setting of the
7178 -- order overrides the defaults.
7179
7180 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
7181 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
7182 end if;
7183
7184 ------------------------
7185 -- Size or Value_Size --
7186 ------------------------
7187
7188 -- Size or Value_Size attribute definition clause. These are treated
7189 -- the same, except that Size is allowed on objects, and Value_Size
7190 -- is allowed on nonfirst subtypes. First subtypes allow both Size
7191 -- and Value_Size; the treatment is the same for both.
7192
7193 when Attribute_Size | Attribute_Value_Size => Size : declare
7194 Size : constant Uint := Static_Integer (Expr);
7195
7196 Attr_Name : constant String :=
7197 (if Id = Attribute_Size then "size"
7198 elsif Id = Attribute_Value_Size then "value size"
7199 else ""); -- can't happen
7200 -- Name of the attribute for printing in messages
7201
7202 OK_Prefix : constant Boolean :=
7203 (if Id = Attribute_Size then
7204 Ekind (U_Ent) in Type_Kind | Constant_Or_Variable_Kind
7205 elsif Id = Attribute_Value_Size then
7206 Ekind (U_Ent) in Type_Kind
7207 else False); -- can't happen
7208 -- For X'Size, X can be a type or object; for X'Value_Size,
7209 -- X can be a type. Note that we already checked that 'Size
7210 -- can be specified only for a first subtype.
7211
7212 begin
7213 FOnly := True;
7214
7215 if not OK_Prefix then
7216 Error_Msg_N (Attr_Name & " cannot be given for &", Nam);
7217
7218 elsif Duplicate_Clause then
7219 null;
7220
7221 elsif Is_Array_Type (U_Ent)
7222 and then not Is_Constrained (U_Ent)
7223 then
7224 Error_Msg_N
7225 (Attr_Name & " cannot be given for unconstrained array", Nam);
7226
7227 elsif Present (Size) then
7228 declare
7229 Etyp : constant Entity_Id :=
7230 (if Is_Type (U_Ent) then U_Ent else Etype (U_Ent));
7231
7232 begin
7233 -- Check size, note that Gigi is in charge of checking that
7234 -- the size of an array or record type is OK. Also we do not
7235 -- check the size in the ordinary fixed-point case, since
7236 -- it is too early to do so (there may be subsequent small
7237 -- clause that affects the size). We can check the size if
7238 -- a small clause has already been given.
7239
7240 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
7241 or else Has_Small_Clause (U_Ent)
7242 then
7243 declare
7244 Biased : Boolean;
7245 begin
7246 Check_Size (Expr, Etyp, Size, Biased);
7247 Set_Biased (U_Ent, N, Attr_Name & " clause", Biased);
7248 end;
7249 end if;
7250
7251 -- For types, set RM_Size and Esize if appropriate
7252
7253 if Is_Type (U_Ent) then
7254 Set_RM_Size (U_Ent, Size);
7255
7256 -- If we are specifying the Size or Value_Size of a
7257 -- first subtype, then for elementary types, increase
7258 -- Object_Size to power of 2, but not less than a storage
7259 -- unit in any case (normally this means it will be byte
7260 -- addressable).
7261
7262 -- For all other types, nothing else to do, we leave
7263 -- Esize (object size) unset; the back end will set it
7264 -- from the size and alignment in an appropriate manner.
7265
7266 -- In both cases, we check whether the alignment must be
7267 -- reset in the wake of the size change.
7268
7269 -- For nonfirst subtypes ('Value_Size only), we do
7270 -- nothing here.
7271
7272 if Is_First_Subtype (U_Ent) then
7273 if Is_Elementary_Type (U_Ent) then
7274 if Size <= System_Storage_Unit then
7275 Set_Esize
7276 (U_Ent, UI_From_Int (System_Storage_Unit));
7277 elsif Size <= 16 then
7278 Set_Esize (U_Ent, Uint_16);
7279 elsif Size <= 32 then
7280 Set_Esize (U_Ent, Uint_32);
7281 else
7282 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
7283 end if;
7284
7285 Alignment_Check_For_Size_Change
7286 (U_Ent, Esize (U_Ent));
7287 else
7288 Alignment_Check_For_Size_Change (U_Ent, Size);
7289 end if;
7290 end if;
7291
7292 -- For Object'Size, set Esize only
7293
7294 else
7295 if Is_Elementary_Type (Etyp)
7296 and then Size /= System_Storage_Unit
7297 and then Size /= 16
7298 and then Size /= 32
7299 and then Size /= 64
7300 and then Size /= System_Max_Integer_Size
7301 then
7302 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
7303 Error_Msg_Uint_2 :=
7304 UI_From_Int (System_Max_Integer_Size);
7305 Error_Msg_N
7306 ("size for primitive object must be a power of 2 in "
7307 & "the range ^-^", N);
7308 end if;
7309
7310 Set_Esize (U_Ent, Size);
7311 end if;
7312
7313 -- As of RM 13.1, only confirming size
7314 -- (i.e. (Size = Esize (Etyp))) for aliased object of
7315 -- elementary type must be supported.
7316 -- GNAT rejects nonconfirming size for such object.
7317
7318 if Is_Aliased (U_Ent)
7319 and then Is_Elementary_Type (Etyp)
7320 and then Known_Esize (U_Ent)
7321 and then Size /= Esize (Etyp)
7322 then
7323 Error_Msg_N
7324 ("nonconfirming Size for aliased object is not "
7325 & "supported", N);
7326 end if;
7327
7328 Set_Has_Size_Clause (U_Ent);
7329 end;
7330 end if;
7331 end Size;
7332
7333 -----------
7334 -- Small --
7335 -----------
7336
7337 -- Small attribute definition clause
7338
7339 when Attribute_Small => Small : declare
7340 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
7341 Small : Ureal;
7342
7343 begin
7344 Analyze_And_Resolve (Expr, Any_Real);
7345
7346 if Etype (Expr) = Any_Type then
7347 return;
7348
7349 elsif not Is_OK_Static_Expression (Expr) then
7350 Flag_Non_Static_Expr
7351 ("small requires static expression!", Expr);
7352 return;
7353
7354 else
7355 Small := Expr_Value_R (Expr);
7356
7357 if Small <= Ureal_0 then
7358 Error_Msg_N ("small value must be greater than zero", Expr);
7359 return;
7360 end if;
7361
7362 end if;
7363
7364 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
7365 Error_Msg_N
7366 ("small requires an ordinary fixed point type", Nam);
7367
7368 elsif Has_Small_Clause (U_Ent) then
7369 Error_Msg_N ("small already given for &", Nam);
7370
7371 elsif Small > Delta_Value (U_Ent) then
7372 Error_Msg_N
7373 ("small value must not be greater than delta value", Nam);
7374
7375 else
7376 Set_Small_Value (U_Ent, Small);
7377 Set_Small_Value (Implicit_Base, Small);
7378 Set_Has_Small_Clause (U_Ent);
7379 Set_Has_Small_Clause (Implicit_Base);
7380 Set_Has_Non_Standard_Rep (Implicit_Base);
7381 end if;
7382 end Small;
7383
7384 ------------------
7385 -- Storage_Pool --
7386 ------------------
7387
7388 -- Storage_Pool attribute definition clause
7389
7390 when Attribute_Simple_Storage_Pool
7391 | Attribute_Storage_Pool
7392 =>
7393 Storage_Pool : declare
7394 Pool : Entity_Id;
7395 T : Entity_Id;
7396
7397 procedure Associate_Storage_Pool
7398 (Ent : Entity_Id; Pool : Entity_Id);
7399 -- Associate Pool to Ent and perform legality checks on subpools
7400
7401 ----------------------------
7402 -- Associate_Storage_Pool --
7403 ----------------------------
7404
7405 procedure Associate_Storage_Pool
7406 (Ent : Entity_Id; Pool : Entity_Id)
7407 is
7408 function Object_From (Pool : Entity_Id) return Entity_Id;
7409 -- Return the entity of which Pool is a part of
7410
7411 -----------------
7412 -- Object_From --
7413 -----------------
7414
7415 function Object_From
7416 (Pool : Entity_Id) return Entity_Id
7417 is
7418 N : Node_Id := Pool;
7419 begin
7420 if Present (Renamed_Object (Pool)) then
7421 N := Renamed_Object (Pool);
7422 end if;
7423
7424 while Present (N) loop
7425 case Nkind (N) is
7426 when N_Defining_Identifier =>
7427 return N;
7428
7429 when N_Identifier | N_Expanded_Name =>
7430 return Entity (N);
7431
7432 when N_Indexed_Component | N_Selected_Component |
7433 N_Explicit_Dereference
7434 =>
7435 N := Prefix (N);
7436
7437 when N_Type_Conversion =>
7438 N := Expression (N);
7439
7440 when others =>
7441 -- ??? we probably should handle more cases but
7442 -- this is good enough in practice for this check
7443 -- on a corner case.
7444
7445 return Empty;
7446 end case;
7447 end loop;
7448
7449 return Empty;
7450 end Object_From;
7451
7452 Obj : Entity_Id;
7453
7454 begin
7455 Set_Associated_Storage_Pool (Ent, Pool);
7456
7457 -- Check RM 13.11.4(22-23/3): a specification of a storage pool
7458 -- is illegal if the storage pool supports subpools and:
7459 -- (A) The access type is a general access type.
7460 -- (B) The access type is statically deeper than the storage
7461 -- pool object;
7462 -- (C) The storage pool object is a part of a formal parameter;
7463 -- (D) The storage pool object is a part of the dereference of
7464 -- a non-library level general access type;
7465
7466 if Ada_Version >= Ada_2012
7467 and then RTU_Loaded (System_Storage_Pools_Subpools)
7468 and then
7469 Is_Ancestor (RTE (RE_Root_Storage_Pool_With_Subpools),
7470 Etype (Pool))
7471 then
7472 -- check (A)
7473
7474 if Ekind (Etype (Ent)) = E_General_Access_Type then
7475 Error_Msg_N
7476 ("subpool cannot be used on general access type", Ent);
7477 end if;
7478
7479 -- check (B)
7480
7481 if Type_Access_Level (Ent)
7482 > Static_Accessibility_Level
7483 (Pool, Object_Decl_Level)
7484 then
7485 Error_Msg_N
7486 ("subpool access type has deeper accessibility "
7487 & "level than pool", Ent);
7488 return;
7489 end if;
7490
7491 Obj := Object_From (Pool);
7492
7493 -- check (C)
7494
7495 if Present (Obj) and then Is_Formal (Obj) then
7496 Error_Msg_N
7497 ("subpool cannot be part of a parameter", Ent);
7498 return;
7499 end if;
7500
7501 -- check (D)
7502
7503 if Present (Obj)
7504 and then Ekind (Etype (Obj)) = E_General_Access_Type
7505 and then not Is_Library_Level_Entity (Etype (Obj))
7506 then
7507 Error_Msg_N
7508 ("subpool cannot be part of the dereference of a " &
7509 "nested general access type", Ent);
7510 return;
7511 end if;
7512 end if;
7513 end Associate_Storage_Pool;
7514
7515 begin
7516 if Ekind (U_Ent) = E_Access_Subprogram_Type then
7517 Error_Msg_N
7518 ("storage pool cannot be given for access-to-subprogram type",
7519 Nam);
7520 return;
7521
7522 elsif Ekind (U_Ent) not in E_Access_Type | E_General_Access_Type
7523 then
7524 Error_Msg_N
7525 ("storage pool can only be given for access types", Nam);
7526 return;
7527
7528 elsif Is_Derived_Type (U_Ent) then
7529 Error_Msg_N
7530 ("storage pool cannot be given for a derived access type",
7531 Nam);
7532
7533 elsif Duplicate_Clause then
7534 return;
7535
7536 elsif Present (Associated_Storage_Pool (U_Ent)) then
7537 Error_Msg_N ("storage pool already given for &", Nam);
7538 return;
7539 end if;
7540
7541 -- Check for Storage_Size previously given
7542
7543 declare
7544 SS : constant Node_Id :=
7545 Get_Attribute_Definition_Clause
7546 (U_Ent, Attribute_Storage_Size);
7547 begin
7548 if Present (SS) then
7549 Check_Pool_Size_Clash (U_Ent, N, SS);
7550 end if;
7551 end;
7552
7553 -- Storage_Pool case
7554
7555 if Id = Attribute_Storage_Pool then
7556 Analyze_And_Resolve
7557 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
7558
7559 -- In the Simple_Storage_Pool case, we allow a variable of any
7560 -- simple storage pool type, so we Resolve without imposing an
7561 -- expected type.
7562
7563 else
7564 Analyze_And_Resolve (Expr);
7565
7566 if No (Get_Rep_Pragma
7567 (Etype (Expr), Name_Simple_Storage_Pool_Type))
7568 then
7569 Error_Msg_N
7570 ("expression must be of a simple storage pool type", Expr);
7571 end if;
7572 end if;
7573
7574 if not Denotes_Variable (Expr) then
7575 Error_Msg_N ("storage pool must be a variable", Expr);
7576 return;
7577 end if;
7578
7579 if Nkind (Expr) = N_Type_Conversion then
7580 T := Etype (Expression (Expr));
7581 else
7582 T := Etype (Expr);
7583 end if;
7584
7585 -- The Stack_Bounded_Pool is used internally for implementing
7586 -- access types with a Storage_Size. Since it only work properly
7587 -- when used on one specific type, we need to check that it is not
7588 -- hijacked improperly:
7589
7590 -- type T is access Integer;
7591 -- for T'Storage_Size use n;
7592 -- type Q is access Float;
7593 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
7594
7595 if Is_RTE (Base_Type (T), RE_Stack_Bounded_Pool) then
7596 Error_Msg_N ("non-shareable internal Pool", Expr);
7597 return;
7598 end if;
7599
7600 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7601 -- Storage_Pool since this attribute cannot be defined for such
7602 -- types (RM E.2.2(17)).
7603
7604 Validate_Remote_Access_To_Class_Wide_Type (N);
7605
7606 -- If the argument is a name that is not an entity name, then
7607 -- we construct a renaming operation to define an entity of
7608 -- type storage pool.
7609
7610 if not Is_Entity_Name (Expr)
7611 and then Is_Object_Reference (Expr)
7612 then
7613 Pool := Make_Temporary (Loc, 'P', Expr);
7614
7615 declare
7616 Rnode : constant Node_Id :=
7617 Make_Object_Renaming_Declaration (Loc,
7618 Defining_Identifier => Pool,
7619 Subtype_Mark =>
7620 New_Occurrence_Of (Etype (Expr), Loc),
7621 Name => Expr);
7622
7623 begin
7624 -- If the attribute definition clause comes from an aspect
7625 -- clause, then insert the renaming before the associated
7626 -- entity's declaration, since the attribute clause has
7627 -- not yet been appended to the declaration list.
7628
7629 if From_Aspect_Specification (N) then
7630 Insert_Before (Parent (Entity (N)), Rnode);
7631 else
7632 Insert_Before (N, Rnode);
7633 end if;
7634
7635 Analyze (Rnode);
7636 Associate_Storage_Pool (U_Ent, Pool);
7637 end;
7638
7639 elsif Is_Entity_Name (Expr) then
7640 Pool := Entity (Expr);
7641
7642 -- If pool is a renamed object, get original one. This can
7643 -- happen with an explicit renaming, and within instances.
7644
7645 while Present (Renamed_Object (Pool))
7646 and then Is_Entity_Name (Renamed_Object (Pool))
7647 loop
7648 Pool := Entity (Renamed_Object (Pool));
7649 end loop;
7650
7651 if Present (Renamed_Object (Pool))
7652 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
7653 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
7654 then
7655 Pool := Entity (Expression (Renamed_Object (Pool)));
7656 end if;
7657
7658 Associate_Storage_Pool (U_Ent, Pool);
7659
7660 elsif Nkind (Expr) = N_Type_Conversion
7661 and then Is_Entity_Name (Expression (Expr))
7662 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
7663 then
7664 Pool := Entity (Expression (Expr));
7665 Associate_Storage_Pool (U_Ent, Pool);
7666
7667 else
7668 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
7669 return;
7670 end if;
7671 end Storage_Pool;
7672
7673 ------------------
7674 -- Storage_Size --
7675 ------------------
7676
7677 -- Storage_Size attribute definition clause
7678
7679 when Attribute_Storage_Size => Storage_Size : declare
7680 Btype : constant Entity_Id := Base_Type (U_Ent);
7681
7682 begin
7683 if Is_Task_Type (U_Ent) then
7684
7685 -- Check obsolescent (but never obsolescent if from aspect)
7686
7687 if not From_Aspect_Specification (N) then
7688 Check_Restriction (No_Obsolescent_Features, N);
7689
7690 if Warn_On_Obsolescent_Feature then
7691 Error_Msg_N
7692 ("?j?storage size clause for task is an obsolescent "
7693 & "feature (RM J.9)", N);
7694 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
7695 end if;
7696 end if;
7697
7698 FOnly := True;
7699 end if;
7700
7701 if not Is_Access_Type (U_Ent)
7702 and then Ekind (U_Ent) /= E_Task_Type
7703 then
7704 Error_Msg_N ("storage size cannot be given for &", Nam);
7705
7706 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
7707 Error_Msg_N
7708 ("storage size cannot be given for a derived access type",
7709 Nam);
7710
7711 elsif Duplicate_Clause then
7712 null;
7713
7714 else
7715 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7716 -- Storage_Size since this attribute cannot be defined for such
7717 -- types (RM E.2.2(17)).
7718
7719 Validate_Remote_Access_To_Class_Wide_Type (N);
7720
7721 Analyze_And_Resolve (Expr, Any_Integer);
7722
7723 if Is_Access_Type (U_Ent) then
7724
7725 -- Check for Storage_Pool previously given
7726
7727 declare
7728 SP : constant Node_Id :=
7729 Get_Attribute_Definition_Clause
7730 (U_Ent, Attribute_Storage_Pool);
7731
7732 begin
7733 if Present (SP) then
7734 Check_Pool_Size_Clash (U_Ent, SP, N);
7735 end if;
7736 end;
7737
7738 -- Special case of for x'Storage_Size use 0
7739
7740 if Is_OK_Static_Expression (Expr)
7741 and then Expr_Value (Expr) = 0
7742 then
7743 Set_No_Pool_Assigned (Btype);
7744 end if;
7745 end if;
7746
7747 Set_Has_Storage_Size_Clause (Btype);
7748 end if;
7749 end Storage_Size;
7750
7751 -----------------
7752 -- Stream_Size --
7753 -----------------
7754
7755 when Attribute_Stream_Size => Stream_Size : declare
7756 Size : constant Uint := Static_Integer (Expr);
7757
7758 begin
7759 if Ada_Version <= Ada_95 then
7760 Check_Restriction (No_Implementation_Attributes, N);
7761 end if;
7762
7763 if Duplicate_Clause then
7764 null;
7765
7766 elsif Is_Elementary_Type (U_Ent) then
7767 -- Size will be empty if we already detected an error
7768 -- (e.g. Expr is of the wrong type); we might as well
7769 -- give the useful hint below even in that case.
7770
7771 if No (Size) or else
7772 (Size /= System_Storage_Unit
7773 and then Size /= System_Storage_Unit * 2
7774 and then Size /= System_Storage_Unit * 3
7775 and then Size /= System_Storage_Unit * 4
7776 and then Size /= System_Storage_Unit * 8)
7777 then
7778 Error_Msg_N
7779 ("stream size for elementary type must be 8, 16, 24, " &
7780 "32 or 64", N);
7781
7782 elsif Known_RM_Size (U_Ent) and then RM_Size (U_Ent) > Size then
7783 Error_Msg_Uint_1 := RM_Size (U_Ent);
7784 Error_Msg_N
7785 ("stream size for elementary type must be 8, 16, 24, " &
7786 "32 or 64 and at least ^", N);
7787 end if;
7788
7789 Set_Has_Stream_Size_Clause (U_Ent);
7790
7791 else
7792 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
7793 end if;
7794 end Stream_Size;
7795
7796 -----------------------
7797 -- Variable_Indexing --
7798 -----------------------
7799
7800 when Attribute_Variable_Indexing =>
7801 Check_Indexing_Functions;
7802
7803 -----------
7804 -- Write --
7805 -----------
7806
7807 when Attribute_Write =>
7808 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
7809 Set_Has_Specified_Stream_Write (Ent);
7810
7811 -- All other attributes cannot be set
7812
7813 when others =>
7814 Error_Msg_N
7815 ("attribute& cannot be set with definition clause", N);
7816 end case;
7817
7818 -- The test for the type being frozen must be performed after any
7819 -- expression the clause has been analyzed since the expression itself
7820 -- might cause freezing that makes the clause illegal.
7821
7822 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
7823 return;
7824 end if;
7825 end Analyze_Attribute_Definition_Clause;
7826
7827 ----------------------------
7828 -- Analyze_Code_Statement --
7829 ----------------------------
7830
7831 procedure Analyze_Code_Statement (N : Node_Id) is
7832 HSS : constant Node_Id := Parent (N);
7833 SBody : constant Node_Id := Parent (HSS);
7834 Subp : constant Entity_Id := Current_Scope;
7835 Stmt : Node_Id;
7836 Decl : Node_Id;
7837 StmtO : Node_Id;
7838 DeclO : Node_Id;
7839
7840 begin
7841 -- Accept foreign code statements for CodePeer. The analysis is skipped
7842 -- to avoid rejecting unrecognized constructs.
7843
7844 if CodePeer_Mode then
7845 Set_Analyzed (N);
7846 return;
7847 end if;
7848
7849 -- Analyze and check we get right type, note that this implements the
7850 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
7851 -- the only way that Asm_Insn could possibly be visible.
7852
7853 Analyze_And_Resolve (Expression (N));
7854
7855 if Etype (Expression (N)) = Any_Type then
7856 return;
7857 elsif not Is_RTE (Etype (Expression (N)), RE_Asm_Insn) then
7858 Error_Msg_N ("incorrect type for code statement", N);
7859 return;
7860 end if;
7861
7862 Check_Code_Statement (N);
7863
7864 -- Make sure we appear in the handled statement sequence of a subprogram
7865 -- (RM 13.8(3)).
7866
7867 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
7868 or else Nkind (SBody) /= N_Subprogram_Body
7869 then
7870 Error_Msg_N
7871 ("code statement can only appear in body of subprogram", N);
7872 return;
7873 end if;
7874
7875 -- Do remaining checks (RM 13.8(3)) if not already done
7876
7877 if not Is_Machine_Code_Subprogram (Subp) then
7878 Set_Is_Machine_Code_Subprogram (Subp);
7879
7880 -- No exception handlers allowed
7881
7882 if Present (Exception_Handlers (HSS)) then
7883 Error_Msg_N
7884 ("exception handlers not permitted in machine code subprogram",
7885 First (Exception_Handlers (HSS)));
7886 end if;
7887
7888 -- No declarations other than use clauses and pragmas (we allow
7889 -- certain internally generated declarations as well).
7890
7891 Decl := First (Declarations (SBody));
7892 while Present (Decl) loop
7893 DeclO := Original_Node (Decl);
7894 if Comes_From_Source (DeclO)
7895 and Nkind (DeclO) not in N_Pragma
7896 | N_Use_Package_Clause
7897 | N_Use_Type_Clause
7898 | N_Implicit_Label_Declaration
7899 then
7900 Error_Msg_N
7901 ("this declaration is not allowed in machine code subprogram",
7902 DeclO);
7903 end if;
7904
7905 Next (Decl);
7906 end loop;
7907
7908 -- No statements other than code statements, pragmas, and labels.
7909 -- Again we allow certain internally generated statements.
7910
7911 -- In Ada 2012, qualified expressions are names, and the code
7912 -- statement is initially parsed as a procedure call.
7913
7914 Stmt := First (Statements (HSS));
7915 while Present (Stmt) loop
7916 StmtO := Original_Node (Stmt);
7917
7918 -- A procedure call transformed into a code statement is OK
7919
7920 if Ada_Version >= Ada_2012
7921 and then Nkind (StmtO) = N_Procedure_Call_Statement
7922 and then Nkind (Name (StmtO)) = N_Qualified_Expression
7923 then
7924 null;
7925
7926 elsif Comes_From_Source (StmtO)
7927 and then Nkind (StmtO) not in
7928 N_Pragma | N_Label | N_Code_Statement
7929 then
7930 Error_Msg_N
7931 ("this statement is not allowed in machine code subprogram",
7932 StmtO);
7933 end if;
7934
7935 Next (Stmt);
7936 end loop;
7937 end if;
7938 end Analyze_Code_Statement;
7939
7940 -----------------------------------------------
7941 -- Analyze_Enumeration_Representation_Clause --
7942 -----------------------------------------------
7943
7944 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
7945 Ident : constant Node_Id := Identifier (N);
7946 Aggr : constant Node_Id := Array_Aggregate (N);
7947 Enumtype : Entity_Id;
7948 Elit : Entity_Id;
7949 Expr : Node_Id;
7950 Assoc : Node_Id;
7951 Choice : Node_Id;
7952 Val : Uint;
7953
7954 Err : Boolean := False;
7955 -- Set True to avoid cascade errors and crashes on incorrect source code
7956
7957 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
7958 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
7959 -- Allowed range of universal integer (= allowed range of enum lit vals)
7960
7961 Min : Uint;
7962 Max : Uint;
7963 -- Minimum and maximum values of entries
7964
7965 Max_Node : Node_Id := Empty; -- init to avoid warning
7966 -- Pointer to node for literal providing max value
7967
7968 begin
7969 if Ignore_Rep_Clauses then
7970 Kill_Rep_Clause (N);
7971 return;
7972 end if;
7973
7974 -- Ignore enumeration rep clauses by default in CodePeer mode,
7975 -- unless -gnatd.I is specified, as a work around for potential false
7976 -- positive messages.
7977
7978 if CodePeer_Mode and not Debug_Flag_Dot_II then
7979 return;
7980 end if;
7981
7982 -- First some basic error checks
7983
7984 Find_Type (Ident);
7985 Enumtype := Entity (Ident);
7986
7987 if Enumtype = Any_Type
7988 or else Rep_Item_Too_Early (Enumtype, N)
7989 then
7990 return;
7991 else
7992 Enumtype := Underlying_Type (Enumtype);
7993 end if;
7994
7995 if not Is_Enumeration_Type (Enumtype) then
7996 Error_Msg_NE
7997 ("enumeration type required, found}",
7998 Ident, First_Subtype (Enumtype));
7999 return;
8000 end if;
8001
8002 -- Ignore rep clause on generic actual type. This will already have
8003 -- been flagged on the template as an error, and this is the safest
8004 -- way to ensure we don't get a junk cascaded message in the instance.
8005
8006 if Is_Generic_Actual_Type (Enumtype) then
8007 return;
8008
8009 -- Type must be in current scope
8010
8011 elsif Scope (Enumtype) /= Current_Scope then
8012 Error_Msg_N ("type must be declared in this scope", Ident);
8013 return;
8014
8015 -- Type must be a first subtype
8016
8017 elsif not Is_First_Subtype (Enumtype) then
8018 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
8019 return;
8020
8021 -- Ignore duplicate rep clause
8022
8023 elsif Has_Enumeration_Rep_Clause (Enumtype) then
8024 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
8025 return;
8026
8027 -- Don't allow rep clause for standard [wide_[wide_]]character
8028
8029 elsif Is_Standard_Character_Type (Enumtype) then
8030 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
8031 return;
8032
8033 -- Check that the expression is a proper aggregate (no parentheses)
8034
8035 elsif Paren_Count (Aggr) /= 0 then
8036 Error_Msg_F
8037 ("extra parentheses surrounding aggregate not allowed", Aggr);
8038 return;
8039
8040 -- Reject the mixing of named and positional entries in the aggregate
8041
8042 elsif Present (Expressions (Aggr))
8043 and then Present (Component_Associations (Aggr))
8044 then
8045 Error_Msg_N ("cannot mix positional and named entries in "
8046 & "enumeration rep clause", N);
8047 return;
8048
8049 -- All tests passed, so set rep clause in place
8050
8051 else
8052 Set_Has_Enumeration_Rep_Clause (Enumtype);
8053 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
8054 end if;
8055
8056 -- Now we process the aggregate. Note that we don't use the normal
8057 -- aggregate code for this purpose, because we don't want any of the
8058 -- normal expansion activities, and a number of special semantic
8059 -- rules apply (including the component type being any integer type)
8060
8061 Elit := First_Literal (Enumtype);
8062
8063 -- Process positional entries
8064
8065 if Present (Expressions (Aggr)) then
8066 Expr := First (Expressions (Aggr));
8067 while Present (Expr) loop
8068 if No (Elit) then
8069 Error_Msg_N ("too many entries in aggregate", Expr);
8070 return;
8071 end if;
8072
8073 Val := Static_Integer (Expr);
8074
8075 -- Err signals that we found some incorrect entries processing
8076 -- the list. The final checks for completeness and ordering are
8077 -- skipped in this case.
8078
8079 if No (Val) then
8080 Err := True;
8081
8082 elsif Val < Lo or else Hi < Val then
8083 Error_Msg_N ("value outside permitted range", Expr);
8084 Err := True;
8085
8086 else
8087 Set_Enumeration_Rep (Elit, Val);
8088 Set_Enumeration_Rep_Expr (Elit, Expr);
8089 end if;
8090
8091 Next (Expr);
8092 Next (Elit);
8093 end loop;
8094
8095 -- Process named entries
8096
8097 elsif Present (Component_Associations (Aggr)) then
8098 Assoc := First (Component_Associations (Aggr));
8099 while Present (Assoc) loop
8100 Choice := First (Choices (Assoc));
8101
8102 if Present (Next (Choice)) then
8103 Error_Msg_N
8104 ("multiple choice not allowed here", Next (Choice));
8105 Err := True;
8106 end if;
8107
8108 if Nkind (Choice) = N_Others_Choice then
8109 Error_Msg_N ("OTHERS choice not allowed here", Choice);
8110 Err := True;
8111
8112 elsif Nkind (Choice) = N_Range then
8113
8114 -- ??? should allow zero/one element range here
8115
8116 Error_Msg_N ("range not allowed here", Choice);
8117 Err := True;
8118
8119 else
8120 Analyze_And_Resolve (Choice, Enumtype);
8121
8122 if Error_Posted (Choice) then
8123 Err := True;
8124 end if;
8125
8126 if not Err then
8127 if Is_Entity_Name (Choice)
8128 and then Is_Type (Entity (Choice))
8129 then
8130 Error_Msg_N ("subtype name not allowed here", Choice);
8131 Err := True;
8132
8133 -- ??? should allow static subtype with zero/one entry
8134
8135 elsif Etype (Choice) = Base_Type (Enumtype) then
8136 if not Is_OK_Static_Expression (Choice) then
8137 Flag_Non_Static_Expr
8138 ("non-static expression used for choice!", Choice);
8139 Err := True;
8140
8141 else
8142 Elit := Expr_Value_E (Choice);
8143
8144 if Present (Enumeration_Rep_Expr (Elit)) then
8145 Error_Msg_Sloc :=
8146 Sloc (Enumeration_Rep_Expr (Elit));
8147 Error_Msg_NE
8148 ("representation for& previously given#",
8149 Choice, Elit);
8150 Err := True;
8151 end if;
8152
8153 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
8154
8155 Expr := Expression (Assoc);
8156 Val := Static_Integer (Expr);
8157
8158 if No (Val) then
8159 Err := True;
8160
8161 elsif Val < Lo or else Hi < Val then
8162 Error_Msg_N ("value outside permitted range", Expr);
8163 Err := True;
8164
8165 else
8166 Set_Enumeration_Rep (Elit, Val);
8167 end if;
8168 end if;
8169 end if;
8170 end if;
8171 end if;
8172
8173 Next (Assoc);
8174 end loop;
8175 end if;
8176
8177 -- Aggregate is fully processed. Now we check that a full set of
8178 -- representations was given, and that they are in range and in order.
8179 -- These checks are only done if no other errors occurred.
8180
8181 if not Err then
8182 Min := No_Uint;
8183 Max := No_Uint;
8184
8185 Elit := First_Literal (Enumtype);
8186 while Present (Elit) loop
8187 if No (Enumeration_Rep_Expr (Elit)) then
8188 Error_Msg_NE ("missing representation for&!", N, Elit);
8189
8190 else
8191 Val := Enumeration_Rep (Elit);
8192
8193 if No (Min) then
8194 Min := Val;
8195 end if;
8196
8197 if Present (Val) then
8198 if Present (Max) and then Val <= Max then
8199 Error_Msg_NE
8200 ("enumeration value for& not ordered!",
8201 Enumeration_Rep_Expr (Elit), Elit);
8202 end if;
8203
8204 Max_Node := Enumeration_Rep_Expr (Elit);
8205 Max := Val;
8206 end if;
8207
8208 -- If there is at least one literal whose representation is not
8209 -- equal to the Pos value, then note that this enumeration type
8210 -- has a non-standard representation.
8211
8212 if Val /= Enumeration_Pos (Elit) then
8213 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
8214 end if;
8215 end if;
8216
8217 Next (Elit);
8218 end loop;
8219
8220 -- Now set proper size information
8221
8222 declare
8223 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
8224
8225 begin
8226 if Has_Size_Clause (Enumtype) then
8227
8228 -- All OK, if size is OK now
8229
8230 if RM_Size (Enumtype) >= Minsize then
8231 null;
8232
8233 else
8234 -- Try if we can get by with biasing
8235
8236 Minsize :=
8237 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
8238
8239 -- Error message if even biasing does not work
8240
8241 if RM_Size (Enumtype) < Minsize then
8242 Error_Msg_Uint_1 := RM_Size (Enumtype);
8243 Error_Msg_Uint_2 := Max;
8244 Error_Msg_N
8245 ("previously given size (^) is too small "
8246 & "for this value (^)", Max_Node);
8247
8248 -- If biasing worked, indicate that we now have biased rep
8249
8250 else
8251 Set_Biased
8252 (Enumtype, Size_Clause (Enumtype), "size clause");
8253 end if;
8254 end if;
8255
8256 else
8257 Set_RM_Size (Enumtype, Minsize);
8258 Set_Enum_Esize (Enumtype);
8259 end if;
8260
8261 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
8262 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
8263
8264 Copy_Alignment (To => Base_Type (Enumtype), From => Enumtype);
8265 end;
8266 end if;
8267
8268 -- We repeat the too late test in case it froze itself
8269
8270 if Rep_Item_Too_Late (Enumtype, N) then
8271 null;
8272 end if;
8273 end Analyze_Enumeration_Representation_Clause;
8274
8275 ----------------------------
8276 -- Analyze_Free_Statement --
8277 ----------------------------
8278
8279 procedure Analyze_Free_Statement (N : Node_Id) is
8280 begin
8281 Analyze (Expression (N));
8282 end Analyze_Free_Statement;
8283
8284 ---------------------------
8285 -- Analyze_Freeze_Entity --
8286 ---------------------------
8287
8288 procedure Analyze_Freeze_Entity (N : Node_Id) is
8289 begin
8290 Freeze_Entity_Checks (N);
8291 end Analyze_Freeze_Entity;
8292
8293 -----------------------------------
8294 -- Analyze_Freeze_Generic_Entity --
8295 -----------------------------------
8296
8297 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
8298 E : constant Entity_Id := Entity (N);
8299
8300 begin
8301 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
8302 Analyze_Aspects_At_Freeze_Point (E);
8303 end if;
8304
8305 Freeze_Entity_Checks (N);
8306 end Analyze_Freeze_Generic_Entity;
8307
8308 ------------------------------------------
8309 -- Analyze_Record_Representation_Clause --
8310 ------------------------------------------
8311
8312 -- Note: we check as much as we can here, but we can't do any checks
8313 -- based on the position values (e.g. overlap checks) until freeze time
8314 -- because especially in Ada 2005 (machine scalar mode), the processing
8315 -- for non-standard bit order can substantially change the positions.
8316 -- See procedure Check_Record_Representation_Clause (called from Freeze)
8317 -- for the remainder of this processing.
8318
8319 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
8320 Ident : constant Node_Id := Identifier (N);
8321 Biased : Boolean;
8322 CC : Node_Id;
8323 Comp : Entity_Id;
8324 Fbit : Uint;
8325 Lbit : Uint;
8326 Ocomp : Entity_Id;
8327 Posit : Uint;
8328 Rectype : Entity_Id;
8329 Recdef : Node_Id;
8330
8331 function Is_Inherited (Comp : Entity_Id) return Boolean;
8332 -- True if Comp is an inherited component in a record extension
8333
8334 ------------------
8335 -- Is_Inherited --
8336 ------------------
8337
8338 function Is_Inherited (Comp : Entity_Id) return Boolean is
8339 Comp_Base : Entity_Id;
8340
8341 begin
8342 if Ekind (Rectype) = E_Record_Subtype then
8343 Comp_Base := Original_Record_Component (Comp);
8344 else
8345 Comp_Base := Comp;
8346 end if;
8347
8348 return Comp_Base /= Original_Record_Component (Comp_Base);
8349 end Is_Inherited;
8350
8351 -- Local variables
8352
8353 Is_Record_Extension : Boolean;
8354 -- True if Rectype is a record extension
8355
8356 CR_Pragma : Node_Id := Empty;
8357 -- Points to N_Pragma node if Complete_Representation pragma present
8358
8359 -- Start of processing for Analyze_Record_Representation_Clause
8360
8361 begin
8362 if Ignore_Rep_Clauses then
8363 Kill_Rep_Clause (N);
8364 return;
8365 end if;
8366
8367 Find_Type (Ident);
8368 Rectype := Entity (Ident);
8369
8370 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
8371 return;
8372 else
8373 Rectype := Underlying_Type (Rectype);
8374 end if;
8375
8376 -- First some basic error checks
8377
8378 if not Is_Record_Type (Rectype) then
8379 Error_Msg_NE
8380 ("record type required, found}", Ident, First_Subtype (Rectype));
8381 return;
8382
8383 elsif Scope (Rectype) /= Current_Scope then
8384 Error_Msg_N ("type must be declared in this scope", N);
8385 return;
8386
8387 elsif not Is_First_Subtype (Rectype) then
8388 Error_Msg_N ("cannot give record rep clause for subtype", N);
8389 return;
8390
8391 elsif Has_Record_Rep_Clause (Rectype) then
8392 Error_Msg_N ("duplicate record rep clause ignored", N);
8393 return;
8394
8395 elsif Rep_Item_Too_Late (Rectype, N) then
8396 return;
8397 end if;
8398
8399 -- We know we have a first subtype, now possibly go to the anonymous
8400 -- base type to determine whether Rectype is a record extension.
8401
8402 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
8403 Is_Record_Extension :=
8404 Nkind (Recdef) = N_Derived_Type_Definition
8405 and then Present (Record_Extension_Part (Recdef));
8406
8407 if Present (Mod_Clause (N)) then
8408 declare
8409 M : constant Node_Id := Mod_Clause (N);
8410 P : constant List_Id := Pragmas_Before (M);
8411 Ignore : Uint;
8412
8413 begin
8414 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
8415
8416 if Warn_On_Obsolescent_Feature then
8417 Error_Msg_N
8418 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
8419 Error_Msg_N
8420 ("\?j?use alignment attribute definition clause instead", N);
8421 end if;
8422
8423 if Present (P) then
8424 Analyze_List (P);
8425 end if;
8426
8427 -- Get the alignment value to perform error checking
8428
8429 Ignore := Get_Alignment_Value (Expression (M));
8430 end;
8431 end if;
8432
8433 -- For untagged types, clear any existing component clauses for the
8434 -- type. If the type is derived, this is what allows us to override
8435 -- a rep clause for the parent. For type extensions, the representation
8436 -- of the inherited components is inherited, so we want to keep previous
8437 -- component clauses for completeness.
8438
8439 if not Is_Tagged_Type (Rectype) then
8440 Comp := First_Component_Or_Discriminant (Rectype);
8441 while Present (Comp) loop
8442 Set_Component_Clause (Comp, Empty);
8443 Next_Component_Or_Discriminant (Comp);
8444 end loop;
8445 end if;
8446
8447 -- All done if no component clauses
8448
8449 CC := First (Component_Clauses (N));
8450
8451 if No (CC) then
8452 return;
8453 end if;
8454
8455 -- A representation like this applies to the base type
8456
8457 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
8458 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
8459 Set_Has_Specified_Layout (Base_Type (Rectype));
8460
8461 -- Process the component clauses
8462
8463 while Present (CC) loop
8464
8465 -- Pragma
8466
8467 if Nkind (CC) = N_Pragma then
8468 Analyze (CC);
8469
8470 -- The only pragma of interest is Complete_Representation
8471
8472 if Pragma_Name (CC) = Name_Complete_Representation then
8473 CR_Pragma := CC;
8474 end if;
8475
8476 -- Processing for real component clause
8477
8478 else
8479 Posit := Static_Integer (Position (CC));
8480 Fbit := Static_Integer (First_Bit (CC));
8481 Lbit := Static_Integer (Last_Bit (CC));
8482
8483 if Present (Posit)
8484 and then Present (Fbit)
8485 and then Present (Lbit)
8486 then
8487 if Posit < 0 then
8488 Error_Msg_N ("position cannot be negative", Position (CC));
8489
8490 elsif Fbit < 0 then
8491 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
8492
8493 -- The Last_Bit specified in a component clause must not be
8494 -- less than the First_Bit minus one (RM-13.5.1(10)).
8495
8496 elsif Lbit < Fbit - 1 then
8497 Error_Msg_N
8498 ("last bit cannot be less than first bit minus one",
8499 Last_Bit (CC));
8500
8501 -- Values look OK, so find the corresponding record component
8502 -- Even though the syntax allows an attribute reference for
8503 -- implementation-defined components, GNAT does not allow the
8504 -- tag to get an explicit position.
8505
8506 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
8507 if Attribute_Name (Component_Name (CC)) = Name_Tag then
8508 Error_Msg_N ("position of tag cannot be specified", CC);
8509 else
8510 Error_Msg_N ("illegal component name", CC);
8511 end if;
8512
8513 else
8514 Comp := First_Entity (Rectype);
8515 while Present (Comp) loop
8516 exit when Chars (Comp) = Chars (Component_Name (CC));
8517 Next_Entity (Comp);
8518 end loop;
8519
8520 if No (Comp) then
8521
8522 -- Maybe component of base type that is absent from
8523 -- statically constrained first subtype.
8524
8525 Comp := First_Entity (Base_Type (Rectype));
8526 while Present (Comp) loop
8527 exit when Chars (Comp) = Chars (Component_Name (CC));
8528 Next_Entity (Comp);
8529 end loop;
8530 end if;
8531
8532 if No (Comp) then
8533 Error_Msg_N
8534 ("component clause is for non-existent field", CC);
8535
8536 -- Ada 2012 (AI05-0026): Any name that denotes a
8537 -- discriminant of an object of an unchecked union type
8538 -- shall not occur within a record_representation_clause.
8539
8540 -- The general restriction of using record rep clauses on
8541 -- Unchecked_Union types has now been lifted. Since it is
8542 -- possible to introduce a record rep clause which mentions
8543 -- the discriminant of an Unchecked_Union in non-Ada 2012
8544 -- code, this check is applied to all versions of the
8545 -- language.
8546
8547 elsif Ekind (Comp) = E_Discriminant
8548 and then Is_Unchecked_Union (Rectype)
8549 then
8550 Error_Msg_N
8551 ("cannot reference discriminant of unchecked union",
8552 Component_Name (CC));
8553
8554 elsif Is_Record_Extension and then Is_Inherited (Comp) then
8555 Error_Msg_NE
8556 ("component clause not allowed for inherited "
8557 & "component&", CC, Comp);
8558
8559 elsif Present (Component_Clause (Comp)) then
8560
8561 -- Diagnose duplicate rep clause, or check consistency
8562 -- if this is an inherited component. In a double fault,
8563 -- there may be a duplicate inconsistent clause for an
8564 -- inherited component.
8565
8566 if Scope (Original_Record_Component (Comp)) = Rectype
8567 or else Parent (Component_Clause (Comp)) = N
8568 then
8569 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
8570 Error_Msg_N ("component clause previously given#", CC);
8571
8572 else
8573 declare
8574 Rep1 : constant Node_Id := Component_Clause (Comp);
8575 begin
8576 if Intval (Position (Rep1)) /=
8577 Intval (Position (CC))
8578 or else Intval (First_Bit (Rep1)) /=
8579 Intval (First_Bit (CC))
8580 or else Intval (Last_Bit (Rep1)) /=
8581 Intval (Last_Bit (CC))
8582 then
8583 Error_Msg_N
8584 ("component clause inconsistent with "
8585 & "representation of ancestor", CC);
8586
8587 elsif Warn_On_Redundant_Constructs then
8588 Error_Msg_N
8589 ("?r?redundant confirming component clause "
8590 & "for component!", CC);
8591 end if;
8592 end;
8593 end if;
8594
8595 -- Normal case where this is the first component clause we
8596 -- have seen for this entity, so set it up properly.
8597
8598 else
8599 -- Make reference for field in record rep clause and set
8600 -- appropriate entity field in the field identifier.
8601
8602 Generate_Reference
8603 (Comp, Component_Name (CC), Set_Ref => False);
8604 Set_Entity_With_Checks (Component_Name (CC), Comp);
8605
8606 -- Update Fbit and Lbit to the actual bit number
8607
8608 Fbit := Fbit + UI_From_Int (SSU) * Posit;
8609 Lbit := Lbit + UI_From_Int (SSU) * Posit;
8610
8611 if Has_Size_Clause (Rectype)
8612 and then RM_Size (Rectype) <= Lbit
8613 then
8614 Error_Msg_Uint_1 := RM_Size (Rectype);
8615 Error_Msg_Uint_2 := Lbit + 1;
8616 Error_Msg_N ("bit number out of range of specified "
8617 & "size (expected ^, got ^)",
8618 Last_Bit (CC));
8619 else
8620 Set_Component_Clause (Comp, CC);
8621 Set_Component_Bit_Offset (Comp, Fbit);
8622 Set_Esize (Comp, 1 + (Lbit - Fbit));
8623 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
8624 Set_Normalized_Position (Comp, Fbit / SSU);
8625
8626 if Warn_On_Overridden_Size
8627 and then Has_Size_Clause (Etype (Comp))
8628 and then RM_Size (Etype (Comp)) /= Esize (Comp)
8629 then
8630 Error_Msg_NE
8631 ("?.s?component size overrides size clause for&",
8632 Component_Name (CC), Etype (Comp));
8633 end if;
8634
8635 Check_Size
8636 (Component_Name (CC),
8637 Etype (Comp),
8638 Esize (Comp),
8639 Biased);
8640
8641 Set_Biased
8642 (Comp, First_Node (CC), "component clause", Biased);
8643
8644 -- This information is also set in the corresponding
8645 -- component of the base type, found by accessing the
8646 -- Original_Record_Component link if it is present.
8647
8648 Ocomp := Original_Record_Component (Comp);
8649
8650 if Present (Ocomp) and then Ocomp /= Comp then
8651 Set_Component_Clause (Ocomp, CC);
8652 Set_Component_Bit_Offset (Ocomp, Fbit);
8653 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
8654 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
8655 Set_Normalized_Position (Ocomp, Fbit / SSU);
8656
8657 -- Note: we don't use Set_Biased here, because we
8658 -- already gave a warning above if needed, and we
8659 -- would get a duplicate for the same name here.
8660
8661 Set_Has_Biased_Representation
8662 (Ocomp, Has_Biased_Representation (Comp));
8663 end if;
8664
8665 if Esize (Comp) < 0 then
8666 Error_Msg_N ("component size is negative", CC);
8667 end if;
8668 end if;
8669 end if;
8670 end if;
8671 end if;
8672 end if;
8673
8674 Next (CC);
8675 end loop;
8676
8677 -- Check missing components if Complete_Representation pragma appeared
8678
8679 if Present (CR_Pragma) then
8680 Comp := First_Component_Or_Discriminant (Rectype);
8681 while Present (Comp) loop
8682 if No (Component_Clause (Comp)) then
8683 Error_Msg_NE
8684 ("missing component clause for &", CR_Pragma, Comp);
8685 end if;
8686
8687 Next_Component_Or_Discriminant (Comp);
8688 end loop;
8689
8690 -- Give missing components warning if required
8691
8692 elsif Warn_On_Unrepped_Components then
8693 declare
8694 Num_Repped_Components : Nat := 0;
8695 Num_Unrepped_Components : Nat := 0;
8696
8697 begin
8698 -- First count number of repped and unrepped components
8699
8700 Comp := First_Component_Or_Discriminant (Rectype);
8701 while Present (Comp) loop
8702 if Present (Component_Clause (Comp)) then
8703 Num_Repped_Components := Num_Repped_Components + 1;
8704 else
8705 Num_Unrepped_Components := Num_Unrepped_Components + 1;
8706 end if;
8707
8708 Next_Component_Or_Discriminant (Comp);
8709 end loop;
8710
8711 -- We are only interested in the case where there is at least one
8712 -- unrepped component, and at least half the components have rep
8713 -- clauses. We figure that if less than half have them, then the
8714 -- partial rep clause is really intentional. If the component
8715 -- type has no underlying type set at this point (as for a generic
8716 -- formal type), we don't know enough to give a warning on the
8717 -- component.
8718
8719 if Num_Unrepped_Components > 0
8720 and then Num_Unrepped_Components < Num_Repped_Components
8721 then
8722 Comp := First_Component_Or_Discriminant (Rectype);
8723 while Present (Comp) loop
8724 if No (Component_Clause (Comp))
8725 and then Comes_From_Source (Comp)
8726 and then Present (Underlying_Type (Etype (Comp)))
8727 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
8728 or else Size_Known_At_Compile_Time
8729 (Underlying_Type (Etype (Comp))))
8730 and then not Has_Warnings_Off (Rectype)
8731
8732 -- Ignore discriminant in unchecked union, since it is
8733 -- not there, and cannot have a component clause.
8734
8735 and then (not Is_Unchecked_Union (Rectype)
8736 or else Ekind (Comp) /= E_Discriminant)
8737 then
8738 Error_Msg_Sloc := Sloc (Comp);
8739 Error_Msg_NE
8740 ("?.c?no component clause given for & declared #",
8741 N, Comp);
8742 end if;
8743
8744 Next_Component_Or_Discriminant (Comp);
8745 end loop;
8746 end if;
8747 end;
8748 end if;
8749 end Analyze_Record_Representation_Clause;
8750
8751 -------------------------------------
8752 -- Build_Discrete_Static_Predicate --
8753 -------------------------------------
8754
8755 procedure Build_Discrete_Static_Predicate
8756 (Typ : Entity_Id;
8757 Expr : Node_Id;
8758 Nam : Name_Id)
8759 is
8760 Loc : constant Source_Ptr := Sloc (Expr);
8761
8762 Btyp : constant Entity_Id := Base_Type (Typ);
8763
8764 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
8765 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
8766 -- Low bound and high bound value of base type of Typ
8767
8768 TLo : Uint;
8769 THi : Uint;
8770 -- Bounds for constructing the static predicate. We use the bound of the
8771 -- subtype if it is static, otherwise the corresponding base type bound.
8772 -- Note: a non-static subtype can have a static predicate.
8773
8774 type REnt is record
8775 Lo, Hi : Uint;
8776 end record;
8777 -- One entry in a Rlist value, a single REnt (range entry) value denotes
8778 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
8779 -- value.
8780
8781 type RList is array (Nat range <>) of REnt;
8782 -- A list of ranges. The ranges are sorted in increasing order, and are
8783 -- disjoint (there is a gap of at least one value between each range in
8784 -- the table). A value is in the set of ranges in Rlist if it lies
8785 -- within one of these ranges.
8786
8787 False_Range : constant RList :=
8788 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
8789 -- An empty set of ranges represents a range list that can never be
8790 -- satisfied, since there are no ranges in which the value could lie,
8791 -- so it does not lie in any of them. False_Range is a canonical value
8792 -- for this empty set, but general processing should test for an Rlist
8793 -- with length zero (see Is_False predicate), since other null ranges
8794 -- may appear which must be treated as False.
8795
8796 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
8797 -- Range representing True, value must be in the base range
8798
8799 function "and" (Left : RList; Right : RList) return RList;
8800 -- And's together two range lists, returning a range list. This is a set
8801 -- intersection operation.
8802
8803 function "or" (Left : RList; Right : RList) return RList;
8804 -- Or's together two range lists, returning a range list. This is a set
8805 -- union operation.
8806
8807 function "not" (Right : RList) return RList;
8808 -- Returns complement of a given range list, i.e. a range list
8809 -- representing all the values in TLo .. THi that are not in the input
8810 -- operand Right.
8811
8812 function Build_Val (V : Uint) return Node_Id;
8813 -- Return an analyzed N_Identifier node referencing this value, suitable
8814 -- for use as an entry in the Static_Discrete_Predicate list. This node
8815 -- is typed with the base type.
8816
8817 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
8818 -- Return an analyzed N_Range node referencing this range, suitable for
8819 -- use as an entry in the Static_Discrete_Predicate list. This node is
8820 -- typed with the base type.
8821
8822 function Get_RList
8823 (Exp : Node_Id;
8824 Static : access Boolean) return RList;
8825 -- This is a recursive routine that converts the given expression into a
8826 -- list of ranges, suitable for use in building the static predicate.
8827 -- Static.all will be set to False if the expression is found to be non
8828 -- static. Note that Static.all should be set to True by the caller.
8829
8830 function Is_False (R : RList) return Boolean;
8831 pragma Inline (Is_False);
8832 -- Returns True if the given range list is empty, and thus represents a
8833 -- False list of ranges that can never be satisfied.
8834
8835 function Is_True (R : RList) return Boolean;
8836 -- Returns True if R trivially represents the True predicate by having a
8837 -- single range from BLo to BHi.
8838
8839 function Is_Type_Ref (N : Node_Id) return Boolean;
8840 pragma Inline (Is_Type_Ref);
8841 -- Returns if True if N is a reference to the type for the predicate in
8842 -- the expression (i.e. if it is an identifier whose Chars field matches
8843 -- the Nam given in the call). N must not be parenthesized, if the type
8844 -- name appears in parens, this routine will return False.
8845
8846 function Lo_Val (N : Node_Id) return Uint;
8847 -- Given an entry from a Static_Discrete_Predicate list that is either
8848 -- a static expression or static range, gets either the expression value
8849 -- or the low bound of the range.
8850
8851 function Hi_Val (N : Node_Id) return Uint;
8852 -- Given an entry from a Static_Discrete_Predicate list that is either
8853 -- a static expression or static range, gets either the expression value
8854 -- or the high bound of the range.
8855
8856 function Membership_Entry
8857 (N : Node_Id; Static : access Boolean) return RList;
8858 -- Given a single membership entry (range, value, or subtype), returns
8859 -- the corresponding range list. Set Static.all to False if not static.
8860
8861 function Membership_Entries
8862 (N : Node_Id; Static : access Boolean) return RList;
8863 -- Given an element on an alternatives list of a membership operation,
8864 -- returns the range list corresponding to this entry and all following
8865 -- entries (i.e. returns the "or" of this list of values).
8866 -- Set Static.all to False if not static.
8867
8868 function Stat_Pred
8869 (Typ : Entity_Id;
8870 Static : access Boolean) return RList;
8871 -- Given a type, if it has a static predicate, then set Result to the
8872 -- predicate as a range list, otherwise set Static.all to False.
8873
8874 -----------
8875 -- "and" --
8876 -----------
8877
8878 function "and" (Left : RList; Right : RList) return RList is
8879 FEnt : REnt;
8880 -- First range of result
8881
8882 SLeft : Nat := Left'First;
8883 -- Start of rest of left entries
8884
8885 SRight : Nat := Right'First;
8886 -- Start of rest of right entries
8887
8888 begin
8889 -- If either range is True, return the other
8890
8891 if Is_True (Left) then
8892 return Right;
8893 elsif Is_True (Right) then
8894 return Left;
8895 end if;
8896
8897 -- If either range is False, return False
8898
8899 if Is_False (Left) or else Is_False (Right) then
8900 return False_Range;
8901 end if;
8902
8903 -- Loop to remove entries at start that are disjoint, and thus just
8904 -- get discarded from the result entirely.
8905
8906 loop
8907 -- If no operands left in either operand, result is false
8908
8909 if SLeft > Left'Last or else SRight > Right'Last then
8910 return False_Range;
8911
8912 -- Discard first left operand entry if disjoint with right
8913
8914 elsif Left (SLeft).Hi < Right (SRight).Lo then
8915 SLeft := SLeft + 1;
8916
8917 -- Discard first right operand entry if disjoint with left
8918
8919 elsif Right (SRight).Hi < Left (SLeft).Lo then
8920 SRight := SRight + 1;
8921
8922 -- Otherwise we have an overlapping entry
8923
8924 else
8925 exit;
8926 end if;
8927 end loop;
8928
8929 -- Now we have two non-null operands, and first entries overlap. The
8930 -- first entry in the result will be the overlapping part of these
8931 -- two entries.
8932
8933 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
8934 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
8935
8936 -- Now we can remove the entry that ended at a lower value, since its
8937 -- contribution is entirely contained in Fent.
8938
8939 if Left (SLeft).Hi <= Right (SRight).Hi then
8940 SLeft := SLeft + 1;
8941 else
8942 SRight := SRight + 1;
8943 end if;
8944
8945 -- Compute result by concatenating this first entry with the "and" of
8946 -- the remaining parts of the left and right operands. Note that if
8947 -- either of these is empty, "and" will yield empty, so that we will
8948 -- end up with just Fent, which is what we want in that case.
8949
8950 return
8951 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
8952 end "and";
8953
8954 -----------
8955 -- "not" --
8956 -----------
8957
8958 function "not" (Right : RList) return RList is
8959 begin
8960 -- Return True if False range
8961
8962 if Is_False (Right) then
8963 return True_Range;
8964 end if;
8965
8966 -- Return False if True range
8967
8968 if Is_True (Right) then
8969 return False_Range;
8970 end if;
8971
8972 -- Here if not trivial case
8973
8974 declare
8975 Result : RList (1 .. Right'Length + 1);
8976 -- May need one more entry for gap at beginning and end
8977
8978 Count : Nat := 0;
8979 -- Number of entries stored in Result
8980
8981 begin
8982 -- Gap at start
8983
8984 if Right (Right'First).Lo > TLo then
8985 Count := Count + 1;
8986 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
8987 end if;
8988
8989 -- Gaps between ranges
8990
8991 for J in Right'First .. Right'Last - 1 loop
8992 Count := Count + 1;
8993 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
8994 end loop;
8995
8996 -- Gap at end
8997
8998 if Right (Right'Last).Hi < THi then
8999 Count := Count + 1;
9000 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
9001 end if;
9002
9003 return Result (1 .. Count);
9004 end;
9005 end "not";
9006
9007 ----------
9008 -- "or" --
9009 ----------
9010
9011 function "or" (Left : RList; Right : RList) return RList is
9012 FEnt : REnt;
9013 -- First range of result
9014
9015 SLeft : Nat := Left'First;
9016 -- Start of rest of left entries
9017
9018 SRight : Nat := Right'First;
9019 -- Start of rest of right entries
9020
9021 begin
9022 -- If either range is True, return True
9023
9024 if Is_True (Left) or else Is_True (Right) then
9025 return True_Range;
9026 end if;
9027
9028 -- If either range is False (empty), return the other
9029
9030 if Is_False (Left) then
9031 return Right;
9032 elsif Is_False (Right) then
9033 return Left;
9034 end if;
9035
9036 -- Initialize result first entry from left or right operand depending
9037 -- on which starts with the lower range.
9038
9039 if Left (SLeft).Lo < Right (SRight).Lo then
9040 FEnt := Left (SLeft);
9041 SLeft := SLeft + 1;
9042 else
9043 FEnt := Right (SRight);
9044 SRight := SRight + 1;
9045 end if;
9046
9047 -- This loop eats ranges from left and right operands that are
9048 -- contiguous with the first range we are gathering.
9049
9050 loop
9051 -- Eat first entry in left operand if contiguous or overlapped by
9052 -- gathered first operand of result.
9053
9054 if SLeft <= Left'Last
9055 and then Left (SLeft).Lo <= FEnt.Hi + 1
9056 then
9057 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
9058 SLeft := SLeft + 1;
9059
9060 -- Eat first entry in right operand if contiguous or overlapped by
9061 -- gathered right operand of result.
9062
9063 elsif SRight <= Right'Last
9064 and then Right (SRight).Lo <= FEnt.Hi + 1
9065 then
9066 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
9067 SRight := SRight + 1;
9068
9069 -- All done if no more entries to eat
9070
9071 else
9072 exit;
9073 end if;
9074 end loop;
9075
9076 -- Obtain result as the first entry we just computed, concatenated
9077 -- to the "or" of the remaining results (if one operand is empty,
9078 -- this will just concatenate with the other
9079
9080 return
9081 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
9082 end "or";
9083
9084 -----------------
9085 -- Build_Range --
9086 -----------------
9087
9088 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
9089 Result : Node_Id;
9090 begin
9091 Result :=
9092 Make_Range (Loc,
9093 Low_Bound => Build_Val (Lo),
9094 High_Bound => Build_Val (Hi));
9095 Set_Etype (Result, Btyp);
9096 Set_Analyzed (Result);
9097 return Result;
9098 end Build_Range;
9099
9100 ---------------
9101 -- Build_Val --
9102 ---------------
9103
9104 function Build_Val (V : Uint) return Node_Id is
9105 Result : Node_Id;
9106
9107 begin
9108 if Is_Enumeration_Type (Typ) then
9109 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
9110 else
9111 Result := Make_Integer_Literal (Loc, V);
9112 end if;
9113
9114 Set_Etype (Result, Btyp);
9115 Set_Is_Static_Expression (Result);
9116 Set_Analyzed (Result);
9117 return Result;
9118 end Build_Val;
9119
9120 ---------------
9121 -- Get_RList --
9122 ---------------
9123
9124 function Get_RList
9125 (Exp : Node_Id;
9126 Static : access Boolean) return RList
9127 is
9128 Op : Node_Kind;
9129 Val : Uint;
9130
9131 begin
9132 -- Static expression can only be true or false
9133
9134 if Is_OK_Static_Expression (Exp) then
9135 if Expr_Value (Exp) = 0 then
9136 return False_Range;
9137 else
9138 return True_Range;
9139 end if;
9140 end if;
9141
9142 -- Otherwise test node type
9143
9144 Op := Nkind (Exp);
9145
9146 case Op is
9147
9148 -- And
9149
9150 when N_And_Then
9151 | N_Op_And
9152 =>
9153 return Get_RList (Left_Opnd (Exp), Static)
9154 and
9155 Get_RList (Right_Opnd (Exp), Static);
9156
9157 -- Or
9158
9159 when N_Op_Or
9160 | N_Or_Else
9161 =>
9162 return Get_RList (Left_Opnd (Exp), Static)
9163 or
9164 Get_RList (Right_Opnd (Exp), Static);
9165
9166 -- Not
9167
9168 when N_Op_Not =>
9169 return not Get_RList (Right_Opnd (Exp), Static);
9170
9171 -- Comparisons of type with static value
9172
9173 when N_Op_Compare =>
9174
9175 -- Type is left operand
9176
9177 if Is_Type_Ref (Left_Opnd (Exp))
9178 and then Is_OK_Static_Expression (Right_Opnd (Exp))
9179 then
9180 Val := Expr_Value (Right_Opnd (Exp));
9181
9182 -- Typ is right operand
9183
9184 elsif Is_Type_Ref (Right_Opnd (Exp))
9185 and then Is_OK_Static_Expression (Left_Opnd (Exp))
9186 then
9187 Val := Expr_Value (Left_Opnd (Exp));
9188
9189 -- Invert sense of comparison
9190
9191 case Op is
9192 when N_Op_Gt => Op := N_Op_Lt;
9193 when N_Op_Lt => Op := N_Op_Gt;
9194 when N_Op_Ge => Op := N_Op_Le;
9195 when N_Op_Le => Op := N_Op_Ge;
9196 when others => null;
9197 end case;
9198
9199 -- Other cases are non-static
9200
9201 else
9202 Static.all := False;
9203 return False_Range;
9204 end if;
9205
9206 -- Construct range according to comparison operation
9207
9208 case Op is
9209 when N_Op_Eq =>
9210 return RList'(1 => REnt'(Val, Val));
9211
9212 when N_Op_Ge =>
9213 return RList'(1 => REnt'(Val, BHi));
9214
9215 when N_Op_Gt =>
9216 return RList'(1 => REnt'(Val + 1, BHi));
9217
9218 when N_Op_Le =>
9219 return RList'(1 => REnt'(BLo, Val));
9220
9221 when N_Op_Lt =>
9222 return RList'(1 => REnt'(BLo, Val - 1));
9223
9224 when N_Op_Ne =>
9225 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
9226
9227 when others =>
9228 raise Program_Error;
9229 end case;
9230
9231 -- Membership (IN)
9232
9233 when N_In =>
9234 if not Is_Type_Ref (Left_Opnd (Exp)) then
9235 Static.all := False;
9236 return False_Range;
9237 end if;
9238
9239 if Present (Right_Opnd (Exp)) then
9240 return Membership_Entry (Right_Opnd (Exp), Static);
9241 else
9242 return Membership_Entries
9243 (First (Alternatives (Exp)), Static);
9244 end if;
9245
9246 -- Negative membership (NOT IN)
9247
9248 when N_Not_In =>
9249 if not Is_Type_Ref (Left_Opnd (Exp)) then
9250 Static.all := False;
9251 return False_Range;
9252 end if;
9253
9254 if Present (Right_Opnd (Exp)) then
9255 return not Membership_Entry (Right_Opnd (Exp), Static);
9256 else
9257 return not Membership_Entries
9258 (First (Alternatives (Exp)), Static);
9259 end if;
9260
9261 -- Function call, may be call to static predicate
9262
9263 when N_Function_Call =>
9264 if Is_Entity_Name (Name (Exp)) then
9265 declare
9266 Ent : constant Entity_Id := Entity (Name (Exp));
9267 begin
9268 if Is_Predicate_Function (Ent) then
9269 return Stat_Pred (Etype (First_Formal (Ent)), Static);
9270 end if;
9271 end;
9272 end if;
9273
9274 -- Other function call cases are non-static
9275
9276 Static.all := False;
9277 return False_Range;
9278
9279 -- Qualified expression, dig out the expression
9280
9281 when N_Qualified_Expression =>
9282 return Get_RList (Expression (Exp), Static);
9283
9284 when N_Case_Expression =>
9285 declare
9286 Alt : Node_Id;
9287 Choices : List_Id;
9288 Dep : Node_Id;
9289
9290 begin
9291 if not Is_Entity_Name (Expression (Expr))
9292 or else Etype (Expression (Expr)) /= Typ
9293 then
9294 Error_Msg_N
9295 ("expression must denote subtype", Expression (Expr));
9296 return False_Range;
9297 end if;
9298
9299 -- Collect discrete choices in all True alternatives
9300
9301 Choices := New_List;
9302 Alt := First (Alternatives (Exp));
9303 while Present (Alt) loop
9304 Dep := Expression (Alt);
9305
9306 if not Is_OK_Static_Expression (Dep) then
9307 Static.all := False;
9308 return False_Range;
9309
9310 elsif Is_True (Expr_Value (Dep)) then
9311 Append_List_To (Choices,
9312 New_Copy_List (Discrete_Choices (Alt)));
9313 end if;
9314
9315 Next (Alt);
9316 end loop;
9317
9318 return Membership_Entries (First (Choices), Static);
9319 end;
9320
9321 -- Expression with actions: if no actions, dig out expression
9322
9323 when N_Expression_With_Actions =>
9324 if Is_Empty_List (Actions (Exp)) then
9325 return Get_RList (Expression (Exp), Static);
9326 else
9327 Static.all := False;
9328 return False_Range;
9329 end if;
9330
9331 -- Xor operator
9332
9333 when N_Op_Xor =>
9334 return (Get_RList (Left_Opnd (Exp), Static)
9335 and not Get_RList (Right_Opnd (Exp), Static))
9336 or (Get_RList (Right_Opnd (Exp), Static)
9337 and not Get_RList (Left_Opnd (Exp), Static));
9338
9339 -- Any other node type is non-static
9340
9341 when others =>
9342 Static.all := False;
9343 return False_Range;
9344 end case;
9345 end Get_RList;
9346
9347 ------------
9348 -- Hi_Val --
9349 ------------
9350
9351 function Hi_Val (N : Node_Id) return Uint is
9352 begin
9353 if Is_OK_Static_Expression (N) then
9354 return Expr_Value (N);
9355 else
9356 pragma Assert (Nkind (N) = N_Range);
9357 return Expr_Value (High_Bound (N));
9358 end if;
9359 end Hi_Val;
9360
9361 --------------
9362 -- Is_False --
9363 --------------
9364
9365 function Is_False (R : RList) return Boolean is
9366 begin
9367 return R'Length = 0;
9368 end Is_False;
9369
9370 -------------
9371 -- Is_True --
9372 -------------
9373
9374 function Is_True (R : RList) return Boolean is
9375 begin
9376 return R'Length = 1
9377 and then R (R'First).Lo = BLo
9378 and then R (R'First).Hi = BHi;
9379 end Is_True;
9380
9381 -----------------
9382 -- Is_Type_Ref --
9383 -----------------
9384
9385 function Is_Type_Ref (N : Node_Id) return Boolean is
9386 begin
9387 return Nkind (N) = N_Identifier
9388 and then Chars (N) = Nam
9389 and then Paren_Count (N) = 0;
9390 end Is_Type_Ref;
9391
9392 ------------
9393 -- Lo_Val --
9394 ------------
9395
9396 function Lo_Val (N : Node_Id) return Uint is
9397 begin
9398 if Is_OK_Static_Expression (N) then
9399 return Expr_Value (N);
9400 else
9401 pragma Assert (Nkind (N) = N_Range);
9402 return Expr_Value (Low_Bound (N));
9403 end if;
9404 end Lo_Val;
9405
9406 ------------------------
9407 -- Membership_Entries --
9408 ------------------------
9409
9410 function Membership_Entries
9411 (N : Node_Id; Static : access Boolean) return RList is
9412 begin
9413 if No (Next (N)) then
9414 return Membership_Entry (N, Static);
9415 else
9416 return Membership_Entry (N, Static)
9417 or Membership_Entries (Next (N), Static);
9418 end if;
9419 end Membership_Entries;
9420
9421 ----------------------
9422 -- Membership_Entry --
9423 ----------------------
9424
9425 function Membership_Entry
9426 (N : Node_Id; Static : access Boolean) return RList
9427 is
9428 Val : Uint;
9429 SLo : Uint;
9430 SHi : Uint;
9431
9432 begin
9433 -- Range case
9434
9435 if Nkind (N) = N_Range then
9436 if not Is_OK_Static_Expression (Low_Bound (N))
9437 or else
9438 not Is_OK_Static_Expression (High_Bound (N))
9439 then
9440 Static.all := False;
9441 return False_Range;
9442 else
9443 SLo := Expr_Value (Low_Bound (N));
9444 SHi := Expr_Value (High_Bound (N));
9445 return RList'(1 => REnt'(SLo, SHi));
9446 end if;
9447
9448 -- Others case
9449
9450 elsif Nkind (N) = N_Others_Choice then
9451 declare
9452 Choices : constant List_Id := Others_Discrete_Choices (N);
9453 Choice : Node_Id;
9454 Range_List : RList (1 .. List_Length (Choices));
9455
9456 begin
9457 Choice := First (Choices);
9458
9459 for J in Range_List'Range loop
9460 Range_List (J) := REnt'(Lo_Val (Choice), Hi_Val (Choice));
9461 Next (Choice);
9462 end loop;
9463
9464 return Range_List;
9465 end;
9466
9467 -- Static expression case
9468
9469 elsif Is_OK_Static_Expression (N) then
9470 Val := Expr_Value (N);
9471 return RList'(1 => REnt'(Val, Val));
9472
9473 -- Identifier (other than static expression) case
9474
9475 else pragma Assert (Nkind (N) in N_Expanded_Name | N_Identifier);
9476
9477 -- Type case
9478
9479 if Is_Type (Entity (N)) then
9480
9481 -- If type has predicates, process them
9482
9483 if Has_Predicates (Entity (N)) then
9484 return Stat_Pred (Entity (N), Static);
9485
9486 -- For static subtype without predicates, get range
9487
9488 elsif Is_OK_Static_Subtype (Entity (N)) then
9489 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
9490 SHi := Expr_Value (Type_High_Bound (Entity (N)));
9491 return RList'(1 => REnt'(SLo, SHi));
9492
9493 -- Any other type makes us non-static
9494
9495 else
9496 Static.all := False;
9497 return False_Range;
9498 end if;
9499
9500 -- Any other kind of identifier in predicate (e.g. a non-static
9501 -- expression value) means this is not a static predicate.
9502
9503 else
9504 Static.all := False;
9505 return False_Range;
9506 end if;
9507 end if;
9508 end Membership_Entry;
9509
9510 ---------------
9511 -- Stat_Pred --
9512 ---------------
9513
9514 function Stat_Pred
9515 (Typ : Entity_Id;
9516 Static : access Boolean) return RList is
9517 begin
9518 -- Not static if type does not have static predicates
9519
9520 if not Has_Static_Predicate (Typ) then
9521 Static.all := False;
9522 return False_Range;
9523 end if;
9524
9525 -- Otherwise we convert the predicate list to a range list
9526
9527 declare
9528 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
9529 Result : RList (1 .. List_Length (Spred));
9530 P : Node_Id;
9531
9532 begin
9533 P := First (Static_Discrete_Predicate (Typ));
9534 for J in Result'Range loop
9535 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
9536 Next (P);
9537 end loop;
9538
9539 return Result;
9540 end;
9541 end Stat_Pred;
9542
9543 -- Start of processing for Build_Discrete_Static_Predicate
9544
9545 begin
9546 -- Establish bounds for the predicate
9547
9548 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9549 TLo := Expr_Value (Type_Low_Bound (Typ));
9550 else
9551 TLo := BLo;
9552 end if;
9553
9554 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9555 THi := Expr_Value (Type_High_Bound (Typ));
9556 else
9557 THi := BHi;
9558 end if;
9559
9560 -- Analyze the expression to see if it is a static predicate
9561
9562 declare
9563 Static : aliased Boolean := True;
9564 Ranges : constant RList := Get_RList (Expr, Static'Access);
9565 -- Range list from expression if it is static
9566
9567 Plist : List_Id;
9568
9569 begin
9570 -- If non-static, return doing nothing
9571
9572 if not Static then
9573 return;
9574 end if;
9575
9576 -- Convert range list into a form for the static predicate. In the
9577 -- Ranges array, we just have raw ranges, these must be converted
9578 -- to properly typed and analyzed static expressions or range nodes.
9579
9580 -- Note: here we limit ranges to the ranges of the subtype, so that
9581 -- a predicate is always false for values outside the subtype. That
9582 -- seems fine, such values are invalid anyway, and considering them
9583 -- to fail the predicate seems allowed and friendly, and furthermore
9584 -- simplifies processing for case statements and loops.
9585
9586 Plist := New_List;
9587
9588 for J in Ranges'Range loop
9589 declare
9590 Lo : Uint := Ranges (J).Lo;
9591 Hi : Uint := Ranges (J).Hi;
9592
9593 begin
9594 -- Ignore completely out of range entry
9595
9596 if Hi < TLo or else Lo > THi then
9597 null;
9598
9599 -- Otherwise process entry
9600
9601 else
9602 -- Adjust out of range value to subtype range
9603
9604 if Lo < TLo then
9605 Lo := TLo;
9606 end if;
9607
9608 if Hi > THi then
9609 Hi := THi;
9610 end if;
9611
9612 -- Convert range into required form
9613
9614 Append_To (Plist, Build_Range (Lo, Hi));
9615 end if;
9616 end;
9617 end loop;
9618
9619 -- Processing was successful and all entries were static, so now we
9620 -- can store the result as the predicate list.
9621
9622 Set_Static_Discrete_Predicate (Typ, Plist);
9623
9624 -- Within a generic the predicate functions themselves need not
9625 -- be constructed.
9626
9627 if Inside_A_Generic then
9628 return;
9629 end if;
9630
9631 -- The processing for static predicates put the expression into
9632 -- canonical form as a series of ranges. It also eliminated
9633 -- duplicates and collapsed and combined ranges. We might as well
9634 -- replace the alternatives list of the right operand of the
9635 -- membership test with the static predicate list, which will
9636 -- usually be more efficient.
9637
9638 declare
9639 New_Alts : constant List_Id := New_List;
9640 Old_Node : Node_Id;
9641 New_Node : Node_Id;
9642
9643 begin
9644 Old_Node := First (Plist);
9645 while Present (Old_Node) loop
9646 New_Node := New_Copy (Old_Node);
9647
9648 if Nkind (New_Node) = N_Range then
9649 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
9650 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
9651 end if;
9652
9653 Append_To (New_Alts, New_Node);
9654 Next (Old_Node);
9655 end loop;
9656
9657 -- If empty list, replace by False
9658
9659 if Is_Empty_List (New_Alts) then
9660 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
9661
9662 -- Else replace by set membership test
9663
9664 else
9665 Rewrite (Expr,
9666 Make_In (Loc,
9667 Left_Opnd => Make_Identifier (Loc, Nam),
9668 Right_Opnd => Empty,
9669 Alternatives => New_Alts));
9670
9671 -- Resolve new expression in function context
9672
9673 Install_Formals (Predicate_Function (Typ));
9674 Push_Scope (Predicate_Function (Typ));
9675 Analyze_And_Resolve (Expr, Standard_Boolean);
9676 Pop_Scope;
9677 end if;
9678 end;
9679 end;
9680 end Build_Discrete_Static_Predicate;
9681
9682 --------------------------------
9683 -- Build_Export_Import_Pragma --
9684 --------------------------------
9685
9686 function Build_Export_Import_Pragma
9687 (Asp : Node_Id;
9688 Id : Entity_Id) return Node_Id
9689 is
9690 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
9691 Expr : constant Node_Id := Expression (Asp);
9692 Loc : constant Source_Ptr := Sloc (Asp);
9693
9694 Args : List_Id;
9695 Conv : Node_Id;
9696 Conv_Arg : Node_Id;
9697 Dummy_1 : Node_Id;
9698 Dummy_2 : Node_Id;
9699 EN : Node_Id;
9700 LN : Node_Id;
9701 Prag : Node_Id;
9702
9703 Create_Pragma : Boolean := False;
9704 -- This flag is set when the aspect form is such that it warrants the
9705 -- creation of a corresponding pragma.
9706
9707 begin
9708 if Present (Expr) then
9709 if Error_Posted (Expr) then
9710 null;
9711
9712 elsif Is_True (Expr_Value (Expr)) then
9713 Create_Pragma := True;
9714 end if;
9715
9716 -- Otherwise the aspect defaults to True
9717
9718 else
9719 Create_Pragma := True;
9720 end if;
9721
9722 -- Nothing to do when the expression is False or is erroneous
9723
9724 if not Create_Pragma then
9725 return Empty;
9726 end if;
9727
9728 -- Obtain all interfacing aspects that apply to the related entity
9729
9730 Get_Interfacing_Aspects
9731 (Iface_Asp => Asp,
9732 Conv_Asp => Conv,
9733 EN_Asp => EN,
9734 Expo_Asp => Dummy_1,
9735 Imp_Asp => Dummy_2,
9736 LN_Asp => LN);
9737
9738 Args := New_List;
9739
9740 -- Handle the convention argument
9741
9742 if Present (Conv) then
9743 Conv_Arg := New_Copy_Tree (Expression (Conv));
9744
9745 -- Assume convention "Ada' when aspect Convention is missing
9746
9747 else
9748 Conv_Arg := Make_Identifier (Loc, Name_Ada);
9749 end if;
9750
9751 Append_To (Args,
9752 Make_Pragma_Argument_Association (Loc,
9753 Chars => Name_Convention,
9754 Expression => Conv_Arg));
9755
9756 -- Handle the entity argument
9757
9758 Append_To (Args,
9759 Make_Pragma_Argument_Association (Loc,
9760 Chars => Name_Entity,
9761 Expression => New_Occurrence_Of (Id, Loc)));
9762
9763 -- Handle the External_Name argument
9764
9765 if Present (EN) then
9766 Append_To (Args,
9767 Make_Pragma_Argument_Association (Loc,
9768 Chars => Name_External_Name,
9769 Expression => New_Copy_Tree (Expression (EN))));
9770 end if;
9771
9772 -- Handle the Link_Name argument
9773
9774 if Present (LN) then
9775 Append_To (Args,
9776 Make_Pragma_Argument_Association (Loc,
9777 Chars => Name_Link_Name,
9778 Expression => New_Copy_Tree (Expression (LN))));
9779 end if;
9780
9781 -- Generate:
9782 -- pragma Export/Import
9783 -- (Convention => <Conv>/Ada,
9784 -- Entity => <Id>,
9785 -- [External_Name => <EN>,]
9786 -- [Link_Name => <LN>]);
9787
9788 Prag :=
9789 Make_Pragma (Loc,
9790 Pragma_Identifier =>
9791 Make_Identifier (Loc, Chars (Identifier (Asp))),
9792 Pragma_Argument_Associations => Args);
9793
9794 -- Decorate the relevant aspect and the pragma
9795
9796 Set_Aspect_Rep_Item (Asp, Prag);
9797
9798 Set_Corresponding_Aspect (Prag, Asp);
9799 Set_From_Aspect_Specification (Prag);
9800 Set_Parent (Prag, Asp);
9801
9802 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
9803 Set_Import_Pragma (Id, Prag);
9804 end if;
9805
9806 return Prag;
9807 end Build_Export_Import_Pragma;
9808
9809 ------------------------------
9810 -- Build_Predicate_Function --
9811 ------------------------------
9812
9813 -- The function constructed here has the form:
9814
9815 -- function typPredicate (Ixxx : typ) return Boolean is
9816 -- begin
9817 -- return
9818 -- typ1Predicate (typ1 (Ixxx))
9819 -- and then typ2Predicate (typ2 (Ixxx))
9820 -- and then ...
9821 -- and then exp1 and then exp2 and then ...;
9822 -- end typPredicate;
9823
9824 -- If Predicate_Function_Needs_Membership_Parameter is true, then this
9825 -- function takes an additional boolean parameter; the parameter
9826 -- indicates whether the predicate evaluation is part of a membership
9827 -- test. This parameter is used in two cases: 1) It is passed along
9828 -- if another predicate function is called and that predicate function
9829 -- expects to be passed a boolean parameter. 2) If the Predicate_Failure
9830 -- aspect is directly specified for typ, then we replace the return
9831 -- expression described above with
9832 -- (if <expression described above> then True
9833 -- elsif For_Membership_Test then False
9834 -- else (raise Assertion_Error
9835 -- with <Predicate_Failure expression>))
9836 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
9837 -- this is the point at which these expressions get analyzed, providing the
9838 -- required delay, and typ1, typ2, are entities from which predicates are
9839 -- inherited. Note that we do NOT generate Check pragmas, that's because we
9840 -- use this function even if checks are off, e.g. for membership tests.
9841
9842 -- Note that the inherited predicates are evaluated first, as required by
9843 -- AI12-0071-1.
9844
9845 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
9846 -- the form of this return expression.
9847
9848 -- WARNING: This routine manages Ghost regions. Return statements must be
9849 -- replaced by gotos which jump to the end of the routine and restore the
9850 -- Ghost mode.
9851
9852 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
9853 Loc : constant Source_Ptr := Sloc (Typ);
9854
9855 Expr : Node_Id;
9856 -- This is the expression for the result of the function. It is
9857 -- is build by connecting the component predicates with AND THEN.
9858
9859 Object_Name : Name_Id;
9860 -- Name for argument of Predicate procedure. Note that we use the same
9861 -- name for both predicate functions. That way the reference within the
9862 -- predicate expression is the same in both functions.
9863
9864 Object_Entity : Entity_Id;
9865 -- Entity for argument of Predicate procedure
9866
9867 FDecl : Node_Id;
9868 -- The function declaration
9869
9870 SId : Entity_Id;
9871 -- Its entity
9872
9873 Ancestor_Predicate_Function_Called : Boolean := False;
9874 -- Does this predicate function include a call to the
9875 -- predication function of an ancestor subtype?
9876
9877 procedure Add_Condition (Cond : Node_Id);
9878 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
9879 -- Expr is empty).
9880
9881 procedure Add_Predicates;
9882 -- Appends expressions for any Predicate pragmas in the rep item chain
9883 -- Typ to Expr. Note that we look only at items for this exact entity.
9884 -- Inheritance of predicates for the parent type is done by calling the
9885 -- Predicate_Function of the parent type, using Add_Call above.
9886
9887 procedure Add_Call (T : Entity_Id);
9888 -- Includes a call to the predicate function for type T in Expr if
9889 -- Predicate_Function (T) is non-empty.
9890
9891 procedure Replace_Current_Instance_References
9892 (N : Node_Id; Typ, New_Entity : Entity_Id);
9893 -- Replace all references to Typ in the tree rooted at N with
9894 -- references to Param. [New_Entity will be a formal parameter of a
9895 -- predicate function.]
9896
9897 --------------
9898 -- Add_Call --
9899 --------------
9900
9901 procedure Add_Call (T : Entity_Id) is
9902 Exp : Node_Id;
9903
9904 begin
9905 if Present (Predicate_Function (T)) then
9906 pragma Assert (Has_Predicates (Typ));
9907
9908 -- Build the call to the predicate function of T. The type may be
9909 -- derived, so use an unchecked conversion for the actual.
9910
9911 declare
9912 Dynamic_Mem : Node_Id := Empty;
9913 Second_Formal : constant Entity_Id :=
9914 Next_Entity (Object_Entity);
9915 begin
9916 -- Some predicate functions require a second parameter;
9917 -- If one predicate function calls another and the second
9918 -- requires two parameters, then the first should also
9919 -- take two parameters (so that the first function has
9920 -- something to pass to the second function).
9921 if Predicate_Function_Needs_Membership_Parameter (T) then
9922 pragma Assert (Present (Second_Formal));
9923 Dynamic_Mem := New_Occurrence_Of (Second_Formal, Loc);
9924 end if;
9925
9926 Exp :=
9927 Make_Predicate_Call
9928 (Typ => T,
9929 Expr =>
9930 Unchecked_Convert_To (T,
9931 Make_Identifier (Loc, Object_Name)),
9932 Dynamic_Mem => Dynamic_Mem);
9933 end;
9934
9935 -- "and"-in the call to evolving expression
9936
9937 Add_Condition (Exp);
9938 Ancestor_Predicate_Function_Called := True;
9939
9940 -- Output info message on inheritance if required. Note we do not
9941 -- give this information for generic actual types, since it is
9942 -- unwelcome noise in that case in instantiations. We also
9943 -- generally suppress the message in instantiations, and also
9944 -- if it involves internal names.
9945
9946 if List_Inherited_Aspects
9947 and then not Is_Generic_Actual_Type (Typ)
9948 and then Instantiation_Location (Sloc (Typ)) = No_Location
9949 and then not Is_Internal_Name (Chars (T))
9950 and then not Is_Internal_Name (Chars (Typ))
9951 then
9952 Error_Msg_Sloc := Sloc (Predicate_Function (T));
9953 Error_Msg_Node_2 := T;
9954 Error_Msg_N ("info: & inherits predicate from & #?.l?", Typ);
9955 end if;
9956 end if;
9957 end Add_Call;
9958
9959 -------------------
9960 -- Add_Condition --
9961 -------------------
9962
9963 procedure Add_Condition (Cond : Node_Id) is
9964 begin
9965 -- This is the first predicate expression
9966
9967 if No (Expr) then
9968 Expr := Cond;
9969
9970 -- Otherwise concatenate to the existing predicate expressions by
9971 -- using "and then".
9972
9973 else
9974 Expr :=
9975 Make_And_Then (Loc,
9976 Left_Opnd => Relocate_Node (Expr),
9977 Right_Opnd => Cond);
9978 end if;
9979 end Add_Condition;
9980
9981 --------------------
9982 -- Add_Predicates --
9983 --------------------
9984
9985 procedure Add_Predicates is
9986 procedure Add_Predicate (Prag : Node_Id);
9987 -- Concatenate the expression of predicate pragma Prag to Expr by
9988 -- using a short circuit "and then" operator.
9989
9990 -------------------
9991 -- Add_Predicate --
9992 -------------------
9993
9994 procedure Add_Predicate (Prag : Node_Id) is
9995 -- Local variables
9996
9997 Asp : constant Node_Id := Corresponding_Aspect (Prag);
9998 Arg1 : Node_Id;
9999 Arg2 : Node_Id;
10000
10001 -- Start of processing for Add_Predicate
10002
10003 begin
10004 -- Mark corresponding SCO as enabled
10005
10006 Set_SCO_Pragma_Enabled (Sloc (Prag));
10007
10008 -- Extract the arguments of the pragma
10009
10010 Arg1 := First (Pragma_Argument_Associations (Prag));
10011 Arg2 := Next (Arg1);
10012
10013 Arg1 := Get_Pragma_Arg (Arg1);
10014 Arg2 := Get_Pragma_Arg (Arg2);
10015
10016 -- When the predicate pragma applies to the current type or its
10017 -- full view, replace all occurrences of the subtype name with
10018 -- references to the formal parameter of the predicate function.
10019
10020 if Entity (Arg1) = Typ
10021 or else Full_View (Entity (Arg1)) = Typ
10022 then
10023 declare
10024 Arg2_Copy : constant Node_Id := New_Copy_Tree (Arg2);
10025 begin
10026 Replace_Current_Instance_References
10027 (Arg2_Copy, Typ => Typ, New_Entity => Object_Entity);
10028
10029 -- If the predicate pragma comes from an aspect, replace the
10030 -- saved expression because we need the subtype references
10031 -- replaced for the calls to Preanalyze_Spec_Expression in
10032 -- Check_Aspect_At_xxx routines.
10033
10034 if Present (Asp) then
10035 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2_Copy));
10036 end if;
10037
10038 -- "and"-in the Arg2 condition to evolving expression
10039
10040 Add_Condition (Arg2_Copy);
10041 end;
10042 end if;
10043 end Add_Predicate;
10044
10045 -- Local variables
10046
10047 Ritem : Node_Id;
10048
10049 -- Start of processing for Add_Predicates
10050
10051 begin
10052 Ritem := First_Rep_Item (Typ);
10053
10054 -- If the type is private, check whether full view has inherited
10055 -- predicates.
10056
10057 if Is_Private_Type (Typ)
10058 and then No (Ritem)
10059 and then Present (Full_View (Typ))
10060 then
10061 Ritem := First_Rep_Item (Full_View (Typ));
10062 end if;
10063
10064 while Present (Ritem) loop
10065 if Nkind (Ritem) = N_Pragma
10066 and then Pragma_Name (Ritem) = Name_Predicate
10067 then
10068 Add_Predicate (Ritem);
10069
10070 -- If the type is declared in an inner package it may be frozen
10071 -- outside of the package, and the generated pragma has not been
10072 -- analyzed yet, so capture the expression for the predicate
10073 -- function at this point.
10074
10075 elsif Nkind (Ritem) = N_Aspect_Specification
10076 and then Present (Aspect_Rep_Item (Ritem))
10077 and then Scope_Depth (Scope (Typ)) > Scope_Depth (Current_Scope)
10078 then
10079 declare
10080 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
10081
10082 begin
10083 if Nkind (Prag) = N_Pragma
10084 and then Pragma_Name (Prag) = Name_Predicate
10085 then
10086 Add_Predicate (Prag);
10087 end if;
10088 end;
10089 end if;
10090
10091 Next_Rep_Item (Ritem);
10092 end loop;
10093 end Add_Predicates;
10094
10095 -----------------------------------------
10096 -- Replace_Current_Instance_References --
10097 -----------------------------------------
10098
10099 procedure Replace_Current_Instance_References
10100 (N : Node_Id; Typ, New_Entity : Entity_Id)
10101 is
10102 Root : Node_Id renames N;
10103
10104 procedure Replace_One_Reference (N : Node_Id);
10105 -- Actual parameter for Replace_Type_References_Generic instance
10106
10107 ---------------------------
10108 -- Replace_One_Reference --
10109 ---------------------------
10110
10111 procedure Replace_One_Reference (N : Node_Id) is
10112 pragma Assert (In_Subtree (N, Root => Root));
10113 begin
10114 Rewrite (N, New_Occurrence_Of (New_Entity, Sloc (N)));
10115 -- Use the Sloc of the usage name, not the defining name
10116 end Replace_One_Reference;
10117
10118 procedure Replace_Type_References is
10119 new Replace_Type_References_Generic (Replace_One_Reference);
10120 begin
10121 Replace_Type_References (N, Typ);
10122 end Replace_Current_Instance_References;
10123
10124 -- Local variables
10125
10126 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10127 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10128 -- Save the Ghost-related attributes to restore on exit
10129
10130 -- Start of processing for Build_Predicate_Function
10131
10132 begin
10133 -- Return if already built, if type does not have predicates,
10134 -- or if type is a constructed subtype that will inherit a
10135 -- predicate function from its ancestor. In a generic context
10136 -- the predicated parent may not have a predicate function yet
10137 -- but we don't want to build a new one for the subtype. This can
10138 -- happen in an instance body which is nested within a generic
10139 -- unit, in which case Within_A_Generic may be false, SId is
10140 -- Empty, but uses of Typ will receive a predicate check in a
10141 -- context where expansion and tests are enabled.
10142
10143 SId := Predicate_Function (Typ);
10144 if not Has_Predicates (Typ)
10145 or else (Present (SId) and then Has_Completion (SId))
10146 or else
10147 (Is_Itype (Typ)
10148 and then not Comes_From_Source (Typ)
10149 and then Ekind (Typ) in E_Array_Subtype
10150 | E_Record_Subtype
10151 | E_Record_Subtype_With_Private
10152 and then Present (Predicated_Parent (Typ)))
10153 then
10154 return;
10155
10156 -- Do not generate predicate bodies within a generic unit. The
10157 -- expressions have been analyzed already, and the bodies play no role
10158 -- if not within an executable unit. However, if a static predicate is
10159 -- present it must be processed for legality checks such as case
10160 -- coverage in an expression.
10161
10162 elsif Inside_A_Generic
10163 and then not Has_Static_Predicate_Aspect (Typ)
10164 then
10165 return;
10166 end if;
10167
10168 -- The related type may be subject to pragma Ghost. Set the mode now to
10169 -- ensure that the predicate functions are properly marked as Ghost.
10170
10171 Set_Ghost_Mode (Typ);
10172
10173 -- Prepare to construct predicate expression
10174
10175 Expr := Empty;
10176
10177 if Present (SId) then
10178 FDecl := Unit_Declaration_Node (SId);
10179
10180 else
10181 FDecl := Build_Predicate_Function_Declaration (Typ);
10182 SId := Defining_Entity (FDecl);
10183 end if;
10184
10185 -- Recover name of formal parameter of function that replaces references
10186 -- to the type in predicate expressions.
10187
10188 Object_Entity :=
10189 Defining_Identifier
10190 (First (Parameter_Specifications (Specification (FDecl))));
10191
10192 Object_Name := Chars (Object_Entity);
10193
10194 -- Add predicates for ancestor if present. These must come before the
10195 -- ones for the current type, as required by AI12-0071-1.
10196
10197 -- Looks like predicates aren't added for case of inheriting from
10198 -- multiple progenitors???
10199
10200 declare
10201 Atyp : Entity_Id;
10202 begin
10203 Atyp := Nearest_Ancestor (Typ);
10204
10205 -- The type may be private but the full view may inherit predicates
10206
10207 if No (Atyp) and then Is_Private_Type (Typ) then
10208 Atyp := Nearest_Ancestor (Full_View (Typ));
10209 end if;
10210
10211 if Present (Atyp) then
10212 Add_Call (Atyp);
10213 end if;
10214 end;
10215
10216 -- Add Predicates for the current type
10217
10218 Add_Predicates;
10219
10220 -- Case where predicates are present
10221
10222 if Present (Expr) then
10223
10224 -- Build the main predicate function
10225
10226 declare
10227 SIdB : constant Entity_Id :=
10228 Make_Defining_Identifier (Loc,
10229 Chars => New_External_Name (Chars (Typ), "Predicate"));
10230 -- The entity for the function body
10231
10232 Spec : Node_Id;
10233 FBody : Node_Id;
10234
10235 begin
10236 Mutate_Ekind (SIdB, E_Function);
10237 Set_Is_Predicate_Function (SIdB);
10238
10239 -- Build function body
10240
10241 declare
10242 Param_Specs : constant List_Id := New_List (
10243 Make_Parameter_Specification (Loc,
10244 Defining_Identifier =>
10245 Make_Defining_Identifier (Loc, Object_Name),
10246 Parameter_Type =>
10247 New_Occurrence_Of (Typ, Loc)));
10248 begin
10249 -- if Spec has 2 parameters, then body should too
10250 if Present (Next_Entity (Object_Entity)) then
10251 Append (Make_Parameter_Specification (Loc,
10252 Defining_Identifier =>
10253 Make_Defining_Identifier
10254 (Loc, Chars (Next_Entity (Object_Entity))),
10255 Parameter_Type =>
10256 New_Occurrence_Of (Standard_Boolean, Loc)),
10257 Param_Specs);
10258 end if;
10259
10260 Spec :=
10261 Make_Function_Specification (Loc,
10262 Defining_Unit_Name => SIdB,
10263 Parameter_Specifications => Param_Specs,
10264 Result_Definition =>
10265 New_Occurrence_Of (Standard_Boolean, Loc));
10266 end;
10267
10268 -- The Predicate_Expression attribute is used by SPARK.
10269 --
10270 -- If Ancestor_Predicate_Function_Called is True, then
10271 -- we try to exclude that call to the ancestor's
10272 -- predicate function by calling Right_Opnd.
10273 -- The call is not excluded in the case where
10274 -- it is not "and"ed with anything else (so we don't have
10275 -- an N_And_Then node). This exclusion is required if the
10276 -- Predicate_Failure aspect is specified for Typ because
10277 -- in that case we are going to drop the N_And_Then node
10278 -- on the floor. Otherwise, it is a question of what is
10279 -- most convenient for SPARK.
10280
10281 Set_Predicate_Expression
10282 (SId, (if Ancestor_Predicate_Function_Called
10283 and then Nkind (Expr) = N_And_Then
10284 then Right_Opnd (Expr)
10285 else Expr));
10286
10287 declare
10288 Result_Expr : Node_Id := Expr;
10289 PF_Expr : Node_Id := Predicate_Failure_Expression
10290 (Typ, Inherited_OK => False);
10291 PF_Expr_Copy : Node_Id;
10292 Second_Formal : constant Entity_Id :=
10293 Next_Entity (Object_Entity);
10294 begin
10295 -- In GNATprove mode we are only interested in the predicate
10296 -- expression itself and don't want a raise expression that
10297 -- comes from the Predicate_Failure. Ditto for CodePeer.
10298 -- And an illegal Predicate_Failure aspect can lead to cases
10299 -- we want to avoid.
10300
10301 if Present (PF_Expr)
10302 and then not GNATprove_Mode
10303 and then not CodePeer_Mode
10304 and then Serious_Errors_Detected = 0
10305 then
10306 pragma Assert (Present (Second_Formal));
10307
10308 -- This is an ugly hack to cope with an ugly situation.
10309 -- PF_Expr may have children whose Parent attribute
10310 -- does not point back to PF_Expr. If we pass such a
10311 -- tree to New_Copy_Tree, then it does not make a deep
10312 -- copy. But we need a deep copy. So we need to find a
10313 -- tree for which New_Copy_Tree *will* make a deep copy.
10314
10315 declare
10316 function Check_Node_Parent (Parent_Node, Node : Node_Id)
10317 return Traverse_Result;
10318 function Check_Node_Parent (Parent_Node, Node : Node_Id)
10319 return Traverse_Result is
10320 begin
10321 if Parent_Node = PF_Expr
10322 and then not Is_List_Member (Node)
10323 then
10324 pragma Assert
10325 (Nkind (PF_Expr) = Nkind (Parent (Node)));
10326
10327 -- We need PF_Expr to be a node for which
10328 -- New_Copy_Tree will make a deep copy.
10329 PF_Expr := Parent (Node);
10330 return Abandon;
10331 end if;
10332 return OK;
10333 end Check_Node_Parent;
10334 procedure Check_Parentage is
10335 new Traverse_Proc_With_Parent (Check_Node_Parent);
10336 begin
10337 Check_Parentage (PF_Expr);
10338 PF_Expr_Copy := New_Copy_Tree (PF_Expr);
10339 end;
10340
10341 -- Current instance uses need to have their Entity
10342 -- fields set so that Replace_Current_Instance_References
10343 -- can find them. So we preanalyze. Just for purposes of
10344 -- calls to Is_Current_Instance during this preanalysis,
10345 -- we set the Parent field.
10346 Set_Parent (PF_Expr_Copy, Parent (PF_Expr));
10347 Preanalyze (PF_Expr_Copy);
10348 Set_Parent (PF_Expr_Copy, Empty);
10349
10350 Replace_Current_Instance_References
10351 (PF_Expr_Copy, Typ => Typ, New_Entity => Object_Entity);
10352
10353 if Ancestor_Predicate_Function_Called then
10354 -- If the call to an ancestor predicate function
10355 -- returns False, we do not want to raise an
10356 -- exception here. Our Predicate_Failure aspect does
10357 -- not apply in that case. So we have to build a
10358 -- more complicated result expression:
10359 -- (if not Ancestor_Predicate_Function (...) then False
10360 -- elsif Noninherited_Predicates (...) then True
10361 -- elsif Is_Membership_Test then False
10362 -- else (raise Assertion_Error with PF text))
10363
10364 declare
10365 Ancestor_Call : constant Node_Id :=
10366 Left_Opnd (Result_Expr);
10367 Local_Preds : constant Node_Id :=
10368 Right_Opnd (Result_Expr);
10369 begin
10370 Result_Expr :=
10371 Make_If_Expression (Loc,
10372 Expressions => New_List (
10373 Make_Op_Not (Loc, Ancestor_Call),
10374 New_Occurrence_Of (Standard_False, Loc),
10375 Make_If_Expression (Loc,
10376 Is_Elsif => True,
10377 Expressions => New_List (
10378 Local_Preds,
10379 New_Occurrence_Of (Standard_True, Loc),
10380 Make_If_Expression (Loc,
10381 Is_Elsif => True,
10382 Expressions => New_List (
10383 New_Occurrence_Of (Second_Formal, Loc),
10384 New_Occurrence_Of (Standard_False, Loc),
10385 Make_Raise_Expression (Loc,
10386 New_Occurrence_Of (RTE
10387 (RE_Assert_Failure), Loc),
10388 PF_Expr_Copy)))))));
10389 end;
10390
10391 else
10392 -- Build a conditional expression:
10393 -- (if <predicate evaluates to True> then True
10394 -- elsif Is_Membership_Test then False
10395 -- else (raise Assertion_Error with PF text))
10396
10397 Result_Expr :=
10398 Make_If_Expression (Loc,
10399 Expressions => New_List (
10400 Result_Expr,
10401 New_Occurrence_Of (Standard_True, Loc),
10402 Make_If_Expression (Loc,
10403 Is_Elsif => True,
10404 Expressions => New_List (
10405 New_Occurrence_Of (Second_Formal, Loc),
10406 New_Occurrence_Of (Standard_False, Loc),
10407 Make_Raise_Expression (Loc,
10408 New_Occurrence_Of (RTE
10409 (RE_Assert_Failure), Loc),
10410 PF_Expr_Copy)))));
10411 end if;
10412 end if;
10413
10414 FBody :=
10415 Make_Subprogram_Body (Loc,
10416 Specification => Spec,
10417 Declarations => Empty_List,
10418 Handled_Statement_Sequence =>
10419 Make_Handled_Sequence_Of_Statements (Loc,
10420 Statements => New_List (
10421 Make_Simple_Return_Statement (Loc,
10422 Expression => Result_Expr))));
10423 end;
10424
10425 -- The declaration has been analyzed when created, and placed
10426 -- after type declaration. Insert body itself after freeze node,
10427 -- unless subprogram declaration is already there, in which case
10428 -- body better be placed afterwards.
10429
10430 if FDecl = Next (N) then
10431 Insert_After_And_Analyze (FDecl, FBody);
10432 else
10433 Insert_After_And_Analyze (N, FBody);
10434 end if;
10435
10436 -- The defining identifier of a quantified expression carries the
10437 -- scope in which the type appears, but when unnesting we need
10438 -- to indicate that its proper scope is the constructed predicate
10439 -- function. The quantified expressions have been converted into
10440 -- loops during analysis and expansion.
10441
10442 declare
10443 function Reset_Quantified_Variable_Scope
10444 (N : Node_Id) return Traverse_Result;
10445
10446 procedure Reset_Quantified_Variables_Scope is
10447 new Traverse_Proc (Reset_Quantified_Variable_Scope);
10448
10449 -------------------------------------
10450 -- Reset_Quantified_Variable_Scope --
10451 -------------------------------------
10452
10453 function Reset_Quantified_Variable_Scope
10454 (N : Node_Id) return Traverse_Result is
10455 begin
10456 if Nkind (N) in N_Iterator_Specification
10457 | N_Loop_Parameter_Specification
10458 then
10459 Set_Scope (Defining_Identifier (N),
10460 Predicate_Function (Typ));
10461 end if;
10462
10463 return OK;
10464 end Reset_Quantified_Variable_Scope;
10465
10466 begin
10467 if Unnest_Subprogram_Mode then
10468 Reset_Quantified_Variables_Scope (Expr);
10469 end if;
10470 end;
10471
10472 -- Within a generic unit, prevent a double analysis of the body
10473 -- which will not be marked analyzed yet. This will happen when
10474 -- the freeze node is created during the preanalysis of an
10475 -- expression function.
10476
10477 if Inside_A_Generic then
10478 Set_Analyzed (FBody);
10479 end if;
10480
10481 -- Static predicate functions are always side-effect free, and
10482 -- in most cases dynamic predicate functions are as well. Mark
10483 -- them as such whenever possible, so redundant predicate checks
10484 -- can be optimized. If there is a variable reference within the
10485 -- expression, the function is not pure.
10486
10487 if Expander_Active then
10488 Set_Is_Pure (SId,
10489 Side_Effect_Free (Expr, Variable_Ref => True));
10490 Set_Is_Inlined (SId);
10491 end if;
10492 end;
10493
10494 -- See if we have a static predicate. Note that the answer may be
10495 -- yes even if we have an explicit Dynamic_Predicate present.
10496
10497 declare
10498 PS : Boolean;
10499 EN : Node_Id;
10500
10501 begin
10502 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
10503 PS := False;
10504 else
10505 PS := Is_Predicate_Static (Expr, Object_Name);
10506 end if;
10507
10508 -- Case where we have a predicate-static aspect
10509
10510 if PS then
10511
10512 -- We don't set Has_Static_Predicate_Aspect, since we can have
10513 -- any of the three cases (Predicate, Dynamic_Predicate, or
10514 -- Static_Predicate) generating a predicate with an expression
10515 -- that is predicate-static. We just indicate that we have a
10516 -- predicate that can be treated as static.
10517
10518 Set_Has_Static_Predicate (Typ);
10519
10520 -- For discrete subtype, build the static predicate list
10521
10522 if Is_Discrete_Type (Typ) then
10523 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
10524
10525 -- If we don't get a static predicate list, it means that we
10526 -- have a case where this is not possible, most typically in
10527 -- the case where we inherit a dynamic predicate. We do not
10528 -- consider this an error, we just leave the predicate as
10529 -- dynamic. But if we do succeed in building the list, then
10530 -- we mark the predicate as static.
10531
10532 if No (Static_Discrete_Predicate (Typ)) then
10533 Set_Has_Static_Predicate (Typ, False);
10534 end if;
10535
10536 -- For real or string subtype, save predicate expression
10537
10538 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
10539 Set_Static_Real_Or_String_Predicate (Typ, Expr);
10540 end if;
10541
10542 -- Case of dynamic predicate (expression is not predicate-static)
10543
10544 else
10545 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
10546 -- is only set if we have an explicit Dynamic_Predicate aspect
10547 -- given. Here we may simply have a Predicate aspect where the
10548 -- expression happens not to be predicate-static.
10549
10550 -- Emit an error when the predicate is categorized as static
10551 -- but its expression is not predicate-static.
10552
10553 -- First a little fiddling to get a nice location for the
10554 -- message. If the expression is of the form (A and then B),
10555 -- where A is an inherited predicate, then use the right
10556 -- operand for the Sloc. This avoids getting confused by a call
10557 -- to an inherited predicate with a less convenient source
10558 -- location.
10559
10560 EN := Expr;
10561 while Nkind (EN) = N_And_Then
10562 and then Nkind (Left_Opnd (EN)) = N_Function_Call
10563 and then Is_Predicate_Function
10564 (Entity (Name (Left_Opnd (EN))))
10565 loop
10566 EN := Right_Opnd (EN);
10567 end loop;
10568
10569 -- Now post appropriate message
10570
10571 if Has_Static_Predicate_Aspect (Typ) then
10572 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
10573 Error_Msg_F
10574 ("expression is not predicate-static (RM 3.2.4(16-22))",
10575 EN);
10576 else
10577 Error_Msg_F
10578 ("static predicate requires scalar or string type", EN);
10579 end if;
10580 end if;
10581 end if;
10582 end;
10583 end if;
10584
10585 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10586 end Build_Predicate_Function;
10587
10588 ------------------------------------------
10589 -- Build_Predicate_Function_Declaration --
10590 ------------------------------------------
10591
10592 -- WARNING: This routine manages Ghost regions. Return statements must be
10593 -- replaced by gotos which jump to the end of the routine and restore the
10594 -- Ghost mode.
10595
10596 function Build_Predicate_Function_Declaration
10597 (Typ : Entity_Id) return Node_Id
10598 is
10599 Loc : constant Source_Ptr := Sloc (Typ);
10600
10601 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10602 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10603 -- Save the Ghost-related attributes to restore on exit
10604
10605 Func_Decl : Node_Id;
10606 Func_Id : Entity_Id;
10607 Spec : Node_Id;
10608
10609 CRec_Typ : Entity_Id;
10610 -- The corresponding record type of Full_Typ
10611
10612 Full_Typ : Entity_Id;
10613 -- The full view of Typ
10614
10615 Priv_Typ : Entity_Id;
10616 -- The partial view of Typ
10617
10618 UFull_Typ : Entity_Id;
10619 -- The underlying full view of Full_Typ
10620
10621 begin
10622 -- The related type may be subject to pragma Ghost. Set the mode now to
10623 -- ensure that the predicate functions are properly marked as Ghost.
10624
10625 Set_Ghost_Mode (Typ);
10626
10627 Func_Id :=
10628 Make_Defining_Identifier (Loc,
10629 Chars => New_External_Name (Chars (Typ), "Predicate"));
10630
10631 Mutate_Ekind (Func_Id, E_Function);
10632 Set_Etype (Func_Id, Standard_Boolean);
10633 Set_Is_Internal (Func_Id);
10634 Set_Is_Predicate_Function (Func_Id);
10635 Set_Predicate_Function (Typ, Func_Id);
10636
10637 -- The predicate function requires debug info when the predicates are
10638 -- subject to Source Coverage Obligations.
10639
10640 if Opt.Generate_SCO then
10641 Set_Debug_Info_Needed (Func_Id);
10642 end if;
10643
10644 -- Obtain all views of the input type
10645
10646 Get_Views (Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
10647
10648 -- Associate the predicate function and various flags with all views
10649
10650 Propagate_Predicate_Attributes (Priv_Typ, From_Typ => Typ);
10651 Propagate_Predicate_Attributes (Full_Typ, From_Typ => Typ);
10652 Propagate_Predicate_Attributes (UFull_Typ, From_Typ => Typ);
10653 Propagate_Predicate_Attributes (CRec_Typ, From_Typ => Typ);
10654
10655 declare
10656 Param_Specs : constant List_Id := New_List (
10657 Make_Parameter_Specification (Loc,
10658 Defining_Identifier => Make_Temporary (Loc, 'I'),
10659 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
10660 begin
10661 if Predicate_Function_Needs_Membership_Parameter (Typ) then
10662 -- Add Boolean-valued For_Membership_Test param
10663 Append (Make_Parameter_Specification (Loc,
10664 Defining_Identifier => Make_Temporary (Loc, 'M'),
10665 Parameter_Type =>
10666 New_Occurrence_Of (Standard_Boolean, Loc)),
10667 Param_Specs);
10668 end if;
10669
10670 Spec :=
10671 Make_Function_Specification (Loc,
10672 Defining_Unit_Name => Func_Id,
10673 Parameter_Specifications => Param_Specs,
10674 Result_Definition =>
10675 New_Occurrence_Of (Standard_Boolean, Loc));
10676 end;
10677
10678 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
10679
10680 Insert_After (Parent (Typ), Func_Decl);
10681 Analyze (Func_Decl);
10682
10683 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10684
10685 return Func_Decl;
10686 end Build_Predicate_Function_Declaration;
10687
10688 -----------------------------------------
10689 -- Check_Aspect_At_End_Of_Declarations --
10690 -----------------------------------------
10691
10692 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
10693 Ent : constant Entity_Id := Entity (ASN);
10694 Ident : constant Node_Id := Identifier (ASN);
10695 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10696
10697 End_Decl_Expr : constant Node_Id := Entity (Ident);
10698 -- Expression to be analyzed at end of declarations
10699
10700 Freeze_Expr : constant Node_Id := Expression (ASN);
10701 -- Expression from call to Check_Aspect_At_Freeze_Point.
10702
10703 T : constant Entity_Id :=
10704 (if Present (Freeze_Expr) and (A_Id /= Aspect_Stable_Properties)
10705 then Etype (Original_Node (Freeze_Expr))
10706 else Empty);
10707 -- Type required for preanalyze call. We use the original expression to
10708 -- get the proper type, to prevent cascaded errors when the expression
10709 -- is constant-folded. For Stable_Properties, the aspect value is
10710 -- not semantically an expression (although it is syntactically);
10711 -- in particular, it has no type.
10712
10713 Err : Boolean;
10714 -- Set True if error
10715
10716 -- On entry to this procedure, Entity (Ident) contains a copy of the
10717 -- original expression from the aspect, saved for this purpose, and
10718 -- but Expression (Ident) is a preanalyzed copy of the expression,
10719 -- preanalyzed just after the freeze point.
10720
10721 procedure Check_Overloaded_Name;
10722 -- For aspects whose expression is simply a name, this routine checks if
10723 -- the name is overloaded or not. If so, it verifies there is an
10724 -- interpretation that matches the entity obtained at the freeze point,
10725 -- otherwise the compiler complains.
10726
10727 ---------------------------
10728 -- Check_Overloaded_Name --
10729 ---------------------------
10730
10731 procedure Check_Overloaded_Name is
10732 begin
10733 if not Is_Overloaded (End_Decl_Expr) then
10734 Err := not Is_Entity_Name (End_Decl_Expr)
10735 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
10736
10737 else
10738 Err := True;
10739
10740 declare
10741 Index : Interp_Index;
10742 It : Interp;
10743
10744 begin
10745 Get_First_Interp (End_Decl_Expr, Index, It);
10746 while Present (It.Typ) loop
10747 if It.Nam = Entity (Freeze_Expr) then
10748 Err := False;
10749 exit;
10750 end if;
10751
10752 Get_Next_Interp (Index, It);
10753 end loop;
10754 end;
10755 end if;
10756 end Check_Overloaded_Name;
10757
10758 -- Start of processing for Check_Aspect_At_End_Of_Declarations
10759
10760 begin
10761 -- In an instance we do not perform the consistency check between freeze
10762 -- point and end of declarations, because it was done already in the
10763 -- analysis of the generic. Furthermore, the delayed analysis of an
10764 -- aspect of the instance may produce spurious errors when the generic
10765 -- is a child unit that references entities in the parent (which might
10766 -- not be in scope at the freeze point of the instance).
10767
10768 if In_Instance then
10769 return;
10770
10771 -- The enclosing scope may have been rewritten during expansion (.e.g. a
10772 -- task body is rewritten as a procedure) after this conformance check
10773 -- has been performed, so do not perform it again (it may not easily be
10774 -- done if full visibility of local entities is not available).
10775
10776 elsif not Comes_From_Source (Current_Scope) then
10777 return;
10778
10779 -- Case of aspects Dimension, Dimension_System and Synchronization
10780
10781 elsif A_Id = Aspect_Synchronization then
10782 return;
10783
10784 -- Case of stream attributes and Put_Image, just have to compare
10785 -- entities. However, the expression is just a possibly-overloaded
10786 -- name, so we need to verify that one of these interpretations is
10787 -- the one available at at the freeze point.
10788
10789 elsif A_Id in Aspect_Input
10790 | Aspect_Output
10791 | Aspect_Read
10792 | Aspect_Write
10793 | Aspect_Put_Image
10794 then
10795 Analyze (End_Decl_Expr);
10796 Check_Overloaded_Name;
10797
10798 elsif A_Id in Aspect_Variable_Indexing
10799 | Aspect_Constant_Indexing
10800 | Aspect_Default_Iterator
10801 | Aspect_Iterator_Element
10802 | Aspect_Integer_Literal
10803 | Aspect_Real_Literal
10804 | Aspect_String_Literal
10805 then
10806 -- Make type unfrozen before analysis, to prevent spurious errors
10807 -- about late attributes.
10808
10809 Set_Is_Frozen (Ent, False);
10810 Analyze (End_Decl_Expr);
10811 Set_Is_Frozen (Ent, True);
10812
10813 -- If the end of declarations comes before any other freeze point,
10814 -- the Freeze_Expr is not analyzed: no check needed.
10815
10816 if Analyzed (Freeze_Expr) and then not In_Instance then
10817 Check_Overloaded_Name;
10818 else
10819 Err := False;
10820 end if;
10821
10822 -- All other cases
10823
10824 else
10825 -- In a generic context freeze nodes are not always generated, so
10826 -- analyze the expression now. If the aspect is for a type, we must
10827 -- also make its potential components accessible.
10828
10829 if not Analyzed (Freeze_Expr) and then Inside_A_Generic then
10830 if A_Id in Aspect_Dynamic_Predicate | Aspect_Predicate |
10831 Aspect_Static_Predicate
10832 then
10833 Push_Type (Ent);
10834 Preanalyze_Spec_Expression (Freeze_Expr, Standard_Boolean);
10835 Pop_Type (Ent);
10836
10837 elsif A_Id = Aspect_Priority then
10838 Push_Type (Ent);
10839 Preanalyze_Spec_Expression (Freeze_Expr, Any_Integer);
10840 Pop_Type (Ent);
10841
10842 else
10843 Preanalyze (Freeze_Expr);
10844 end if;
10845 end if;
10846
10847 -- Indicate that the expression comes from an aspect specification,
10848 -- which is used in subsequent analysis even if expansion is off.
10849
10850 if Present (End_Decl_Expr) then
10851 Set_Parent (End_Decl_Expr, ASN);
10852 end if;
10853
10854 -- In a generic context the original aspect expressions have not
10855 -- been preanalyzed, so do it now. There are no conformance checks
10856 -- to perform in this case. As before, we have to make components
10857 -- visible for aspects that may reference them.
10858
10859 if Present (Freeze_Expr) and then No (T) then
10860 if A_Id in Aspect_Dynamic_Predicate
10861 | Aspect_Predicate
10862 | Aspect_Priority
10863 | Aspect_Static_Predicate
10864 then
10865 Push_Type (Ent);
10866 Check_Aspect_At_Freeze_Point (ASN);
10867 Pop_Type (Ent);
10868
10869 else
10870 Check_Aspect_At_Freeze_Point (ASN);
10871 end if;
10872 return;
10873
10874 -- The default values attributes may be defined in the private part,
10875 -- and the analysis of the expression may take place when only the
10876 -- partial view is visible. The expression must be scalar, so use
10877 -- the full view to resolve.
10878
10879 elsif A_Id in Aspect_Default_Component_Value | Aspect_Default_Value
10880 and then Is_Private_Type (T)
10881 then
10882 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
10883
10884 -- The following aspect expressions may contain references to
10885 -- components and discriminants of the type.
10886
10887 elsif A_Id in Aspect_CPU
10888 | Aspect_Dynamic_Predicate
10889 | Aspect_Predicate
10890 | Aspect_Priority
10891 | Aspect_Static_Predicate
10892 then
10893 Push_Type (Ent);
10894 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10895 Pop_Type (Ent);
10896
10897 elsif A_Id = Aspect_Predicate_Failure then
10898 Preanalyze_Spec_Expression (End_Decl_Expr, Standard_String);
10899 elsif Present (End_Decl_Expr) then
10900 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10901 end if;
10902
10903 Err :=
10904 not Fully_Conformant_Expressions
10905 (End_Decl_Expr, Freeze_Expr, Report => True);
10906 end if;
10907
10908 -- Output error message if error. Force error on aspect specification
10909 -- even if there is an error on the expression itself.
10910
10911 if Err then
10912 Error_Msg_NE
10913 ("!visibility of aspect for& changes after freeze point",
10914 ASN, Ent);
10915 Error_Msg_NE
10916 ("info: & is frozen here, (RM 13.1.1 (13/3))??",
10917 Freeze_Node (Ent), Ent);
10918 end if;
10919 end Check_Aspect_At_End_Of_Declarations;
10920
10921 ----------------------------------
10922 -- Check_Aspect_At_Freeze_Point --
10923 ----------------------------------
10924
10925 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
10926 Ident : constant Node_Id := Identifier (ASN);
10927 -- Identifier (use Entity field to save expression)
10928
10929 Expr : constant Node_Id := Expression (ASN);
10930 -- For cases where using Entity (Identifier) doesn't work
10931
10932 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10933
10934 T : Entity_Id := Empty;
10935 -- Type required for preanalyze call
10936
10937 begin
10938 -- On entry to this procedure, Entity (Ident) contains a copy of the
10939 -- original expression from the aspect, saved for this purpose.
10940
10941 -- On exit from this procedure Entity (Ident) is unchanged, still
10942 -- containing that copy, but Expression (Ident) is a preanalyzed copy
10943 -- of the expression, preanalyzed just after the freeze point.
10944
10945 -- Make a copy of the expression to be preanalyzed
10946
10947 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
10948
10949 -- Find type for preanalyze call
10950
10951 case A_Id is
10952
10953 -- No_Aspect should be impossible
10954
10955 when No_Aspect =>
10956 raise Program_Error;
10957
10958 -- Aspects taking an optional boolean argument
10959
10960 when Boolean_Aspects
10961 | Library_Unit_Aspects
10962 =>
10963 T := Standard_Boolean;
10964
10965 -- Aspects corresponding to attribute definition clauses
10966
10967 when Aspect_Address =>
10968 T := RTE (RE_Address);
10969
10970 when Aspect_Attach_Handler =>
10971 T := RTE (RE_Interrupt_ID);
10972
10973 when Aspect_Bit_Order
10974 | Aspect_Scalar_Storage_Order
10975 =>
10976 T := RTE (RE_Bit_Order);
10977
10978 when Aspect_Convention =>
10979 return;
10980
10981 when Aspect_CPU =>
10982 T := RTE (RE_CPU_Range);
10983
10984 -- Default_Component_Value is resolved with the component type
10985
10986 when Aspect_Default_Component_Value =>
10987 T := Component_Type (Entity (ASN));
10988
10989 when Aspect_Default_Storage_Pool =>
10990 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
10991
10992 -- Default_Value is resolved with the type entity in question
10993
10994 when Aspect_Default_Value =>
10995 T := Entity (ASN);
10996
10997 when Aspect_Dispatching_Domain =>
10998 T := RTE (RE_Dispatching_Domain);
10999
11000 when Aspect_External_Tag =>
11001 T := Standard_String;
11002
11003 when Aspect_External_Name =>
11004 T := Standard_String;
11005
11006 when Aspect_Link_Name =>
11007 T := Standard_String;
11008
11009 when Aspect_Interrupt_Priority
11010 | Aspect_Priority
11011 =>
11012 T := Standard_Integer;
11013
11014 when Aspect_Relative_Deadline =>
11015 T := RTE (RE_Time_Span);
11016
11017 when Aspect_Secondary_Stack_Size =>
11018 T := Standard_Integer;
11019
11020 when Aspect_Small =>
11021
11022 -- Note that the expression can be of any real type (not just a
11023 -- real universal literal) as long as it is a static constant.
11024
11025 T := Any_Real;
11026
11027 -- For a simple storage pool, we have to retrieve the type of the
11028 -- pool object associated with the aspect's corresponding attribute
11029 -- definition clause.
11030
11031 when Aspect_Simple_Storage_Pool =>
11032 T := Etype (Expression (Aspect_Rep_Item (ASN)));
11033
11034 when Aspect_Storage_Pool =>
11035 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
11036
11037 when Aspect_Alignment
11038 | Aspect_Component_Size
11039 | Aspect_Machine_Radix
11040 | Aspect_Object_Size
11041 | Aspect_Size
11042 | Aspect_Storage_Size
11043 | Aspect_Stream_Size
11044 | Aspect_Value_Size
11045 =>
11046 T := Any_Integer;
11047
11048 when Aspect_Linker_Section =>
11049 T := Standard_String;
11050
11051 when Aspect_Synchronization =>
11052 return;
11053
11054 -- Special case, the expression of these aspects is just an entity
11055 -- that does not need any resolution, so just analyze.
11056
11057 when Aspect_Input
11058 | Aspect_Output
11059 | Aspect_Put_Image
11060 | Aspect_Read
11061 | Aspect_Warnings
11062 | Aspect_Write
11063 =>
11064 Analyze (Expression (ASN));
11065 return;
11066
11067 -- Same for Iterator aspects, where the expression is a function
11068 -- name. Legality rules are checked separately.
11069
11070 when Aspect_Constant_Indexing
11071 | Aspect_Default_Iterator
11072 | Aspect_Iterator_Element
11073 | Aspect_Variable_Indexing
11074 =>
11075 Analyze (Expression (ASN));
11076 return;
11077
11078 -- Same for Literal aspects, where the expression is a function
11079 -- name. Legality rules are checked separately. Use Expr to avoid
11080 -- losing track of the previous resolution of Expression.
11081
11082 when Aspect_Integer_Literal
11083 | Aspect_Real_Literal
11084 | Aspect_String_Literal
11085 =>
11086 Set_Entity (Expression (ASN), Entity (Expr));
11087 Set_Etype (Expression (ASN), Etype (Expr));
11088 Set_Is_Overloaded (Expression (ASN), False);
11089 Analyze (Expression (ASN));
11090 return;
11091
11092 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
11093
11094 when Aspect_Iterable =>
11095 T := Entity (ASN);
11096
11097 declare
11098 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
11099 Assoc : Node_Id;
11100 Expr : Node_Id;
11101
11102 begin
11103 if Cursor = Any_Type then
11104 return;
11105 end if;
11106
11107 Assoc := First (Component_Associations (Expression (ASN)));
11108 while Present (Assoc) loop
11109 Expr := Expression (Assoc);
11110 Analyze (Expr);
11111
11112 if not Error_Posted (Expr) then
11113 Resolve_Iterable_Operation
11114 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
11115 end if;
11116
11117 Next (Assoc);
11118 end loop;
11119 end;
11120
11121 return;
11122
11123 when Aspect_Aggregate =>
11124 Resolve_Aspect_Aggregate (Entity (ASN), Expression (ASN));
11125 return;
11126
11127 when Aspect_Stable_Properties =>
11128 Resolve_Aspect_Stable_Properties
11129 (Entity (ASN), Expression (ASN),
11130 Class_Present => Class_Present (ASN));
11131 return;
11132
11133 -- Invariant/Predicate take boolean expressions
11134
11135 when Aspect_Dynamic_Predicate
11136 | Aspect_Invariant
11137 | Aspect_Predicate
11138 | Aspect_Static_Predicate
11139 | Aspect_Type_Invariant
11140 =>
11141 T := Standard_Boolean;
11142
11143 when Aspect_Predicate_Failure =>
11144 T := Standard_String;
11145
11146 -- As for some other aspects above, the expression of this aspect is
11147 -- just an entity that does not need any resolution, so just analyze.
11148
11149 when Aspect_Designated_Storage_Model =>
11150 Analyze (Expression (ASN));
11151 return;
11152
11153 when Aspect_Storage_Model_Type =>
11154
11155 -- The aggregate argument of Storage_Model_Type is optional, and
11156 -- when not present the aspect defaults to the native storage
11157 -- model (where the address type is System.Address, and other
11158 -- arguments default to corresponding native storage operations).
11159
11160 if No (Expression (ASN)) then
11161 return;
11162 end if;
11163
11164 T := Entity (ASN);
11165
11166 declare
11167 Assoc : Node_Id;
11168 Expr : Node_Id;
11169 Addr_Type : Entity_Id := Empty;
11170
11171 begin
11172 Assoc := First (Component_Associations (Expression (ASN)));
11173 while Present (Assoc) loop
11174 Expr := Expression (Assoc);
11175 Analyze (Expr);
11176
11177 if not Error_Posted (Expr) then
11178 Resolve_Storage_Model_Type_Argument
11179 (Expr, T, Addr_Type, Chars (First (Choices (Assoc))));
11180 end if;
11181
11182 Next (Assoc);
11183 end loop;
11184 end;
11185
11186 return;
11187
11188 -- Here is the list of aspects that don't require delay analysis
11189
11190 when Aspect_Abstract_State
11191 | Aspect_Annotate
11192 | Aspect_Async_Readers
11193 | Aspect_Async_Writers
11194 | Aspect_Constant_After_Elaboration
11195 | Aspect_Contract_Cases
11196 | Aspect_Default_Initial_Condition
11197 | Aspect_Depends
11198 | Aspect_Dimension
11199 | Aspect_Dimension_System
11200 | Aspect_Effective_Reads
11201 | Aspect_Effective_Writes
11202 | Aspect_Extensions_Visible
11203 | Aspect_Ghost
11204 | Aspect_Global
11205 | Aspect_GNAT_Annotate
11206 | Aspect_Implicit_Dereference
11207 | Aspect_Initial_Condition
11208 | Aspect_Initializes
11209 | Aspect_Max_Entry_Queue_Depth
11210 | Aspect_Max_Entry_Queue_Length
11211 | Aspect_Max_Queue_Length
11212 | Aspect_No_Caching
11213 | Aspect_No_Controlled_Parts
11214 | Aspect_No_Task_Parts
11215 | Aspect_Obsolescent
11216 | Aspect_Part_Of
11217 | Aspect_Post
11218 | Aspect_Postcondition
11219 | Aspect_Pre
11220 | Aspect_Precondition
11221 | Aspect_Refined_Depends
11222 | Aspect_Refined_Global
11223 | Aspect_Refined_Post
11224 | Aspect_Refined_State
11225 | Aspect_Relaxed_Initialization
11226 | Aspect_SPARK_Mode
11227 | Aspect_Subprogram_Variant
11228 | Aspect_Suppress
11229 | Aspect_Test_Case
11230 | Aspect_Unimplemented
11231 | Aspect_Unsuppress
11232 | Aspect_Volatile_Function
11233 =>
11234 raise Program_Error;
11235
11236 end case;
11237
11238 -- Do the preanalyze call
11239
11240 if Present (Expression (ASN)) then
11241 Preanalyze_Spec_Expression (Expression (ASN), T);
11242 end if;
11243 end Check_Aspect_At_Freeze_Point;
11244
11245 -----------------------------------
11246 -- Check_Constant_Address_Clause --
11247 -----------------------------------
11248
11249 procedure Check_Constant_Address_Clause
11250 (Expr : Node_Id;
11251 U_Ent : Entity_Id)
11252 is
11253 procedure Check_At_Constant_Address (Nod : Node_Id);
11254 -- Checks that the given node N represents a name whose 'Address is
11255 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
11256 -- address value is the same at the point of declaration of U_Ent and at
11257 -- the time of elaboration of the address clause.
11258
11259 procedure Check_Expr_Constants (Nod : Node_Id);
11260 -- Checks that Nod meets the requirements for a constant address clause
11261 -- in the sense of the enclosing procedure.
11262
11263 procedure Check_List_Constants (Lst : List_Id);
11264 -- Check that all elements of list Lst meet the requirements for a
11265 -- constant address clause in the sense of the enclosing procedure.
11266
11267 -------------------------------
11268 -- Check_At_Constant_Address --
11269 -------------------------------
11270
11271 procedure Check_At_Constant_Address (Nod : Node_Id) is
11272 begin
11273 if Is_Entity_Name (Nod) then
11274 if Present (Address_Clause (Entity ((Nod)))) then
11275 Error_Msg_NE
11276 ("invalid address clause for initialized object &!",
11277 Nod, U_Ent);
11278 Error_Msg_NE
11279 ("address for& cannot depend on another address clause! "
11280 & "(RM 13.1(22))!", Nod, U_Ent);
11281
11282 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
11283 and then Sloc (U_Ent) < Sloc (Entity (Nod))
11284 then
11285 Error_Msg_NE
11286 ("invalid address clause for initialized object &!",
11287 Nod, U_Ent);
11288 Error_Msg_Node_2 := U_Ent;
11289 Error_Msg_NE
11290 ("\& must be defined before & (RM 13.1(22))!",
11291 Nod, Entity (Nod));
11292 end if;
11293
11294 elsif Nkind (Nod) = N_Selected_Component then
11295 declare
11296 T : constant Entity_Id := Etype (Prefix (Nod));
11297
11298 begin
11299 if (Is_Record_Type (T)
11300 and then Has_Discriminants (T))
11301 or else
11302 (Is_Access_Type (T)
11303 and then Is_Record_Type (Designated_Type (T))
11304 and then Has_Discriminants (Designated_Type (T)))
11305 then
11306 Error_Msg_NE
11307 ("invalid address clause for initialized object &!",
11308 Nod, U_Ent);
11309 Error_Msg_N
11310 ("\address cannot depend on component of discriminated "
11311 & "record (RM 13.1(22))!", Nod);
11312 else
11313 Check_At_Constant_Address (Prefix (Nod));
11314 end if;
11315 end;
11316
11317 elsif Nkind (Nod) = N_Indexed_Component then
11318 Check_At_Constant_Address (Prefix (Nod));
11319 Check_List_Constants (Expressions (Nod));
11320
11321 else
11322 Check_Expr_Constants (Nod);
11323 end if;
11324 end Check_At_Constant_Address;
11325
11326 --------------------------
11327 -- Check_Expr_Constants --
11328 --------------------------
11329
11330 procedure Check_Expr_Constants (Nod : Node_Id) is
11331 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
11332 Ent : Entity_Id := Empty;
11333
11334 begin
11335 if Nkind (Nod) in N_Has_Etype
11336 and then Etype (Nod) = Any_Type
11337 then
11338 return;
11339 end if;
11340
11341 case Nkind (Nod) is
11342 when N_Empty
11343 | N_Error
11344 =>
11345 return;
11346
11347 when N_Expanded_Name
11348 | N_Identifier
11349 =>
11350 Ent := Entity (Nod);
11351
11352 -- We need to look at the original node if it is different
11353 -- from the node, since we may have rewritten things and
11354 -- substituted an identifier representing the rewrite.
11355
11356 if Is_Rewrite_Substitution (Nod) then
11357 Check_Expr_Constants (Original_Node (Nod));
11358
11359 -- If the node is an object declaration without initial
11360 -- value, some code has been expanded, and the expression
11361 -- is not constant, even if the constituents might be
11362 -- acceptable, as in A'Address + offset.
11363
11364 if Ekind (Ent) = E_Variable
11365 and then
11366 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
11367 and then
11368 No (Expression (Declaration_Node (Ent)))
11369 then
11370 Error_Msg_NE
11371 ("invalid address clause for initialized object &!",
11372 Nod, U_Ent);
11373
11374 -- If entity is constant, it may be the result of expanding
11375 -- a check. We must verify that its declaration appears
11376 -- before the object in question, else we also reject the
11377 -- address clause.
11378
11379 elsif Ekind (Ent) = E_Constant
11380 and then In_Same_Source_Unit (Ent, U_Ent)
11381 and then Sloc (Ent) > Loc_U_Ent
11382 then
11383 Error_Msg_NE
11384 ("invalid address clause for initialized object &!",
11385 Nod, U_Ent);
11386 end if;
11387
11388 return;
11389 end if;
11390
11391 -- Otherwise look at the identifier and see if it is OK
11392
11393 if Is_Named_Number (Ent) or else Is_Type (Ent) then
11394 return;
11395
11396 elsif Ekind (Ent) in E_Constant | E_In_Parameter then
11397
11398 -- This is the case where we must have Ent defined before
11399 -- U_Ent. Clearly if they are in different units this
11400 -- requirement is met since the unit containing Ent is
11401 -- already processed.
11402
11403 if not In_Same_Source_Unit (Ent, U_Ent) then
11404 return;
11405
11406 -- Otherwise location of Ent must be before the location
11407 -- of U_Ent, that's what prior defined means.
11408
11409 elsif Sloc (Ent) < Loc_U_Ent then
11410 return;
11411
11412 else
11413 Error_Msg_NE
11414 ("invalid address clause for initialized object &!",
11415 Nod, U_Ent);
11416 Error_Msg_Node_2 := U_Ent;
11417 Error_Msg_NE
11418 ("\& must be defined before & (RM 13.1(22))!",
11419 Nod, Ent);
11420 end if;
11421
11422 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
11423 Check_Expr_Constants (Original_Node (Nod));
11424
11425 else
11426 Error_Msg_NE
11427 ("invalid address clause for initialized object &!",
11428 Nod, U_Ent);
11429
11430 if Comes_From_Source (Ent) then
11431 Error_Msg_NE
11432 ("\reference to variable& not allowed"
11433 & " (RM 13.1(22))!", Nod, Ent);
11434 else
11435 Error_Msg_N
11436 ("non-static expression not allowed"
11437 & " (RM 13.1(22))!", Nod);
11438 end if;
11439 end if;
11440
11441 when N_Integer_Literal =>
11442
11443 -- If this is a rewritten unchecked conversion, in a system
11444 -- where Address is an integer type, always use the base type
11445 -- for a literal value. This is user-friendly and prevents
11446 -- order-of-elaboration issues with instances of unchecked
11447 -- conversion.
11448
11449 if Nkind (Original_Node (Nod)) = N_Function_Call then
11450 Set_Etype (Nod, Base_Type (Etype (Nod)));
11451 end if;
11452
11453 when N_Character_Literal
11454 | N_Real_Literal
11455 | N_String_Literal
11456 =>
11457 return;
11458
11459 when N_Range =>
11460 Check_Expr_Constants (Low_Bound (Nod));
11461 Check_Expr_Constants (High_Bound (Nod));
11462
11463 when N_Explicit_Dereference =>
11464 Check_Expr_Constants (Prefix (Nod));
11465
11466 when N_Indexed_Component =>
11467 Check_Expr_Constants (Prefix (Nod));
11468 Check_List_Constants (Expressions (Nod));
11469
11470 when N_Slice =>
11471 Check_Expr_Constants (Prefix (Nod));
11472 Check_Expr_Constants (Discrete_Range (Nod));
11473
11474 when N_Selected_Component =>
11475 Check_Expr_Constants (Prefix (Nod));
11476
11477 when N_Attribute_Reference =>
11478 if Attribute_Name (Nod) in Name_Address
11479 | Name_Access
11480 | Name_Unchecked_Access
11481 | Name_Unrestricted_Access
11482 then
11483 Check_At_Constant_Address (Prefix (Nod));
11484
11485 -- Normally, System'To_Address will have been transformed into
11486 -- an Unchecked_Conversion, but in -gnatc mode, it will not,
11487 -- and we don't want to give an error, because the whole point
11488 -- of 'To_Address is that it is static.
11489
11490 elsif Attribute_Name (Nod) = Name_To_Address then
11491 pragma Assert (Operating_Mode = Check_Semantics);
11492 null;
11493
11494 else
11495 Check_Expr_Constants (Prefix (Nod));
11496 Check_List_Constants (Expressions (Nod));
11497 end if;
11498
11499 when N_Aggregate =>
11500 Check_List_Constants (Component_Associations (Nod));
11501 Check_List_Constants (Expressions (Nod));
11502
11503 when N_Component_Association =>
11504 Check_Expr_Constants (Expression (Nod));
11505
11506 when N_Extension_Aggregate =>
11507 Check_Expr_Constants (Ancestor_Part (Nod));
11508 Check_List_Constants (Component_Associations (Nod));
11509 Check_List_Constants (Expressions (Nod));
11510
11511 when N_Null =>
11512 return;
11513
11514 when N_Binary_Op
11515 | N_Membership_Test
11516 | N_Short_Circuit
11517 =>
11518 Check_Expr_Constants (Left_Opnd (Nod));
11519 Check_Expr_Constants (Right_Opnd (Nod));
11520
11521 when N_Unary_Op =>
11522 Check_Expr_Constants (Right_Opnd (Nod));
11523
11524 when N_Allocator
11525 | N_Qualified_Expression
11526 | N_Type_Conversion
11527 | N_Unchecked_Type_Conversion
11528 =>
11529 Check_Expr_Constants (Expression (Nod));
11530
11531 when N_Function_Call =>
11532 if not Is_Pure (Entity (Name (Nod))) then
11533 Error_Msg_NE
11534 ("invalid address clause for initialized object &!",
11535 Nod, U_Ent);
11536
11537 Error_Msg_NE
11538 ("\function & is not pure (RM 13.1(22))!",
11539 Nod, Entity (Name (Nod)));
11540
11541 else
11542 Check_List_Constants (Parameter_Associations (Nod));
11543 end if;
11544
11545 when N_Parameter_Association =>
11546 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
11547
11548 when others =>
11549 Error_Msg_NE
11550 ("invalid address clause for initialized object &!",
11551 Nod, U_Ent);
11552 Error_Msg_NE
11553 ("\must be constant defined before& (RM 13.1(22))!",
11554 Nod, U_Ent);
11555 end case;
11556 end Check_Expr_Constants;
11557
11558 --------------------------
11559 -- Check_List_Constants --
11560 --------------------------
11561
11562 procedure Check_List_Constants (Lst : List_Id) is
11563 Nod1 : Node_Id;
11564
11565 begin
11566 Nod1 := First (Lst);
11567 while Present (Nod1) loop
11568 Check_Expr_Constants (Nod1);
11569 Next (Nod1);
11570 end loop;
11571 end Check_List_Constants;
11572
11573 -- Start of processing for Check_Constant_Address_Clause
11574
11575 begin
11576 -- If rep_clauses are to be ignored, no need for legality checks. In
11577 -- particular, no need to pester user about rep clauses that violate the
11578 -- rule on constant addresses, given that these clauses will be removed
11579 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
11580 -- we want to relax these checks.
11581
11582 if not Ignore_Rep_Clauses and not CodePeer_Mode then
11583 Check_Expr_Constants (Expr);
11584 end if;
11585 end Check_Constant_Address_Clause;
11586
11587 ---------------------------
11588 -- Check_Pool_Size_Clash --
11589 ---------------------------
11590
11591 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
11592 Post : Node_Id;
11593
11594 begin
11595 -- We need to find out which one came first. Note that in the case of
11596 -- aspects mixed with pragmas there are cases where the processing order
11597 -- is reversed, which is why we do the check here.
11598
11599 if Sloc (SP) < Sloc (SS) then
11600 Error_Msg_Sloc := Sloc (SP);
11601 Post := SS;
11602 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
11603
11604 else
11605 Error_Msg_Sloc := Sloc (SS);
11606 Post := SP;
11607 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
11608 end if;
11609
11610 Error_Msg_N
11611 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
11612 end Check_Pool_Size_Clash;
11613
11614 ----------------------------------------
11615 -- Check_Record_Representation_Clause --
11616 ----------------------------------------
11617
11618 procedure Check_Record_Representation_Clause (N : Node_Id) is
11619 Loc : constant Source_Ptr := Sloc (N);
11620 Ident : constant Node_Id := Identifier (N);
11621 Rectype : Entity_Id;
11622 Fent : Entity_Id;
11623 CC : Node_Id;
11624 Fbit : Uint := No_Uint;
11625 Lbit : Uint := No_Uint;
11626 Hbit : Uint := Uint_0;
11627 Comp : Entity_Id;
11628 Pcomp : Entity_Id;
11629
11630 Max_Bit_So_Far : Uint;
11631 -- Records the maximum bit position so far. If all field positions
11632 -- are monotonically increasing, then we can skip the circuit for
11633 -- checking for overlap, since no overlap is possible.
11634
11635 Tagged_Parent : Entity_Id := Empty;
11636 -- This is set in the case of an extension for which we have either a
11637 -- size clause or Is_Fully_Repped_Tagged_Type True (indicating that all
11638 -- components are positioned by record representation clauses) on the
11639 -- parent type. In this case we check for overlap between components of
11640 -- this tagged type and the parent component. Tagged_Parent will point
11641 -- to this parent type. For all other cases, Tagged_Parent is Empty.
11642
11643 Parent_Last_Bit : Uint := No_Uint; -- init to avoid warning
11644 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
11645 -- last bit position for any field in the parent type. We only need to
11646 -- check overlap for fields starting below this point.
11647
11648 Overlap_Check_Required : Boolean;
11649 -- Used to keep track of whether or not an overlap check is required
11650
11651 Overlap_Detected : Boolean := False;
11652 -- Set True if an overlap is detected
11653
11654 Ccount : Natural := 0;
11655 -- Number of component clauses in record rep clause
11656
11657 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
11658 -- Given two entities for record components or discriminants, checks
11659 -- if they have overlapping component clauses and issues errors if so.
11660
11661 procedure Find_Component;
11662 -- Finds component entity corresponding to current component clause (in
11663 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
11664 -- start/stop bits for the field. If there is no matching component or
11665 -- if the matching component does not have a component clause, then
11666 -- that's an error and Comp is set to Empty, but no error message is
11667 -- issued, since the message was already given. Comp is also set to
11668 -- Empty if the current "component clause" is in fact a pragma.
11669
11670 procedure Record_Hole_Check
11671 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean);
11672 -- Checks for gaps in the given Rectype. Compute After_Last, the bit
11673 -- number after the last component. Warn is True on the initial call,
11674 -- and warnings are given for gaps. For a type extension, this is called
11675 -- recursively to compute After_Last for the parent type; in this case
11676 -- Warn is False and the warnings are suppressed.
11677
11678 procedure Component_Order_Check (Rectype : Entity_Id);
11679 -- Check that the order of component clauses agrees with the order of
11680 -- component declarations, and that the component clauses are given in
11681 -- increasing order of bit offset.
11682
11683 -----------------------------
11684 -- Check_Component_Overlap --
11685 -----------------------------
11686
11687 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
11688 CC1 : constant Node_Id := Component_Clause (C1_Ent);
11689 CC2 : constant Node_Id := Component_Clause (C2_Ent);
11690
11691 begin
11692 if Present (CC1) and then Present (CC2) then
11693
11694 -- Exclude odd case where we have two tag components in the same
11695 -- record, both at location zero. This seems a bit strange, but
11696 -- it seems to happen in some circumstances, perhaps on an error.
11697
11698 if Chars (C1_Ent) = Name_uTag then
11699 return;
11700 end if;
11701
11702 -- Here we check if the two fields overlap
11703
11704 declare
11705 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
11706 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
11707 E1 : constant Uint := S1 + Esize (C1_Ent);
11708 E2 : constant Uint := S2 + Esize (C2_Ent);
11709
11710 begin
11711 if E2 <= S1 or else E1 <= S2 then
11712 null;
11713 else
11714 Error_Msg_Node_2 := Component_Name (CC2);
11715 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
11716 Error_Msg_Node_1 := Component_Name (CC1);
11717 Error_Msg_N
11718 ("component& overlaps & #", Component_Name (CC1));
11719 Overlap_Detected := True;
11720 end if;
11721 end;
11722 end if;
11723 end Check_Component_Overlap;
11724
11725 ---------------------------
11726 -- Component_Order_Check --
11727 ---------------------------
11728
11729 procedure Component_Order_Check (Rectype : Entity_Id) is
11730 Comp : Entity_Id := First_Component (Rectype);
11731 Clause : Node_Id := First (Component_Clauses (N));
11732 Prev_Bit_Offset : Uint := Uint_0;
11733 OOO : constant String :=
11734 "?_r?component clause out of order with respect to declaration";
11735
11736 begin
11737 -- Step Comp through components and Clause through component clauses,
11738 -- skipping pragmas. We ignore discriminants and variant parts,
11739 -- because we get most of the benefit from the plain vanilla
11740 -- component cases, without the extra complexity. If we find a Comp
11741 -- and Clause that don't match, give a warning on both and quit. If
11742 -- we find two subsequent clauses out of order by bit layout, give
11743 -- warning and quit. On each iteration, Prev_Bit_Offset is the one
11744 -- from the previous iteration (or 0 to start).
11745
11746 while Present (Comp) and then Present (Clause) loop
11747 if Nkind (Clause) = N_Component_Clause
11748 and then Ekind (Entity (Component_Name (Clause))) = E_Component
11749 then
11750 if Entity (Component_Name (Clause)) /= Comp then
11751 Error_Msg_N (OOO, Comp);
11752 Error_Msg_N (OOO, Clause);
11753 exit;
11754 end if;
11755
11756 if not Reverse_Bit_Order (Rectype)
11757 and then not Reverse_Storage_Order (Rectype)
11758 and then Component_Bit_Offset (Comp) < Prev_Bit_Offset
11759 then
11760 Error_Msg_N ("?_r?memory layout out of order", Clause);
11761 exit;
11762 end if;
11763
11764 Prev_Bit_Offset := Component_Bit_Offset (Comp);
11765 Next_Component (Comp);
11766 end if;
11767
11768 Next (Clause);
11769 end loop;
11770 end Component_Order_Check;
11771
11772 --------------------
11773 -- Find_Component --
11774 --------------------
11775
11776 procedure Find_Component is
11777
11778 procedure Search_Component (R : Entity_Id);
11779 -- Search components of R for a match. If found, Comp is set
11780
11781 ----------------------
11782 -- Search_Component --
11783 ----------------------
11784
11785 procedure Search_Component (R : Entity_Id) is
11786 begin
11787 Comp := First_Component_Or_Discriminant (R);
11788 while Present (Comp) loop
11789
11790 -- Ignore error of attribute name for component name (we
11791 -- already gave an error message for this, so no need to
11792 -- complain here)
11793
11794 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
11795 null;
11796 else
11797 exit when Chars (Comp) = Chars (Component_Name (CC));
11798 end if;
11799
11800 Next_Component_Or_Discriminant (Comp);
11801 end loop;
11802 end Search_Component;
11803
11804 -- Start of processing for Find_Component
11805
11806 begin
11807 -- Return with Comp set to Empty if we have a pragma
11808
11809 if Nkind (CC) = N_Pragma then
11810 Comp := Empty;
11811 return;
11812 end if;
11813
11814 -- Search current record for matching component
11815
11816 Search_Component (Rectype);
11817
11818 -- If not found, maybe component of base type discriminant that is
11819 -- absent from statically constrained first subtype.
11820
11821 if No (Comp) then
11822 Search_Component (Base_Type (Rectype));
11823 end if;
11824
11825 -- If no component, or the component does not reference the component
11826 -- clause in question, then there was some previous error for which
11827 -- we already gave a message, so just return with Comp Empty.
11828
11829 if No (Comp) or else Component_Clause (Comp) /= CC then
11830 Check_Error_Detected;
11831 Comp := Empty;
11832
11833 -- Normal case where we have a component clause
11834
11835 else
11836 Fbit := Component_Bit_Offset (Comp);
11837 Lbit := Fbit + Esize (Comp) - 1;
11838 end if;
11839 end Find_Component;
11840
11841 -----------------------
11842 -- Record_Hole_Check --
11843 -----------------------
11844
11845 procedure Record_Hole_Check
11846 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean)
11847 is
11848 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
11849 -- Full declaration of record type
11850
11851 procedure Check_Component_List
11852 (DS : List_Id;
11853 CL : Node_Id;
11854 Sbit : Uint;
11855 Abit : out Uint);
11856 -- Check component list CL for holes. DS is a list of discriminant
11857 -- specifications to be included in the consideration of components.
11858 -- Sbit is the starting bit, which is zero if there are no preceding
11859 -- components (before a variant part, or a parent type, or a tag
11860 -- field). If there are preceding components, Sbit is the bit just
11861 -- after the last such component. Abit is set to the bit just after
11862 -- the last component of DS and CL.
11863
11864 --------------------------
11865 -- Check_Component_List --
11866 --------------------------
11867
11868 procedure Check_Component_List
11869 (DS : List_Id;
11870 CL : Node_Id;
11871 Sbit : Uint;
11872 Abit : out Uint)
11873 is
11874 Compl : Integer;
11875
11876 begin
11877 Compl := Integer (List_Length (Component_Items (CL)));
11878
11879 if DS /= No_List then
11880 Compl := Compl + Integer (List_Length (DS));
11881 end if;
11882
11883 declare
11884 Comps : array (Natural range 0 .. Compl) of Entity_Id;
11885 -- Gather components (zero entry is for sort routine)
11886
11887 Ncomps : Natural := 0;
11888 -- Number of entries stored in Comps (starting at Comps (1))
11889
11890 Citem : Node_Id;
11891 -- One component item or discriminant specification
11892
11893 Nbit : Uint;
11894 -- Starting bit for next component
11895
11896 CEnt : Entity_Id;
11897 -- Component entity
11898
11899 Variant : Node_Id;
11900 -- One variant
11901
11902 function Lt (Op1, Op2 : Natural) return Boolean;
11903 -- Compare routine for Sort
11904
11905 procedure Move (From : Natural; To : Natural);
11906 -- Move routine for Sort
11907
11908 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
11909
11910 --------
11911 -- Lt --
11912 --------
11913
11914 function Lt (Op1, Op2 : Natural) return Boolean is
11915 K1 : constant Boolean :=
11916 Known_Component_Bit_Offset (Comps (Op1));
11917 K2 : constant Boolean :=
11918 Known_Component_Bit_Offset (Comps (Op2));
11919 -- Record representation clauses can be incomplete, so the
11920 -- Component_Bit_Offsets can be unknown.
11921 begin
11922 if K1 then
11923 if K2 then
11924 return Component_Bit_Offset (Comps (Op1))
11925 < Component_Bit_Offset (Comps (Op2));
11926 else
11927 return True;
11928 end if;
11929 else
11930 return K2;
11931 end if;
11932 end Lt;
11933
11934 ----------
11935 -- Move --
11936 ----------
11937
11938 procedure Move (From : Natural; To : Natural) is
11939 begin
11940 Comps (To) := Comps (From);
11941 end Move;
11942
11943 begin
11944 -- Gather discriminants into Comp
11945
11946 Citem := First (DS);
11947 while Present (Citem) loop
11948 if Nkind (Citem) = N_Discriminant_Specification then
11949 declare
11950 Ent : constant Entity_Id :=
11951 Defining_Identifier (Citem);
11952 begin
11953 if Ekind (Ent) = E_Discriminant then
11954 Ncomps := Ncomps + 1;
11955 Comps (Ncomps) := Ent;
11956 end if;
11957 end;
11958 end if;
11959
11960 Next (Citem);
11961 end loop;
11962
11963 -- Gather component entities into Comp
11964
11965 Citem := First (Component_Items (CL));
11966 while Present (Citem) loop
11967 if Nkind (Citem) = N_Component_Declaration then
11968 Ncomps := Ncomps + 1;
11969 Comps (Ncomps) := Defining_Identifier (Citem);
11970 end if;
11971
11972 Next (Citem);
11973 end loop;
11974
11975 -- Now sort the component entities based on the first bit.
11976 -- Note we already know there are no overlapping components.
11977
11978 Sorting.Sort (Ncomps);
11979
11980 -- Loop through entries checking for holes
11981
11982 Nbit := Sbit;
11983 for J in 1 .. Ncomps loop
11984 CEnt := Comps (J);
11985 pragma Annotate (CodePeer, Modified, CEnt);
11986
11987 declare
11988 CBO : constant Uint := Component_Bit_Offset (CEnt);
11989
11990 begin
11991 -- Skip components with unknown offsets
11992
11993 if Present (CBO) and then CBO >= 0 then
11994 Error_Msg_Uint_1 := CBO - Nbit;
11995
11996 if Warn and then Error_Msg_Uint_1 > 0 then
11997 Error_Msg_NE
11998 ("?.h?^-bit gap before component&",
11999 Component_Name (Component_Clause (CEnt)),
12000 CEnt);
12001 end if;
12002
12003 Nbit := CBO + Esize (CEnt);
12004 end if;
12005 end;
12006 end loop;
12007
12008 -- Set Abit to just after the last nonvariant component
12009
12010 Abit := Nbit;
12011
12012 -- Process variant parts recursively if present. Set Abit to
12013 -- the maximum for all variant parts.
12014
12015 if Present (Variant_Part (CL)) then
12016 declare
12017 Var_Start : constant Uint := Nbit;
12018 begin
12019 Variant := First (Variants (Variant_Part (CL)));
12020 while Present (Variant) loop
12021 Check_Component_List
12022 (No_List, Component_List (Variant), Var_Start, Nbit);
12023 Next (Variant);
12024 if Nbit > Abit then
12025 Abit := Nbit;
12026 end if;
12027 end loop;
12028 end;
12029 end if;
12030 end;
12031 end Check_Component_List;
12032
12033 -- Local variables
12034
12035 Sbit : Uint;
12036 -- Starting bit for call to Check_Component_List. Zero for an
12037 -- untagged type. The size of the Tag for a nonderived tagged
12038 -- type. Parent size for a type extension.
12039
12040 Record_Definition : Node_Id;
12041 -- Record_Definition containing Component_List to pass to
12042 -- Check_Component_List.
12043
12044 -- Start of processing for Record_Hole_Check
12045
12046 begin
12047 if Is_Tagged_Type (Rectype) then
12048 Sbit := UI_From_Int (System_Address_Size);
12049 else
12050 Sbit := Uint_0;
12051 end if;
12052
12053 After_Last := Uint_0;
12054
12055 if Nkind (Decl) = N_Full_Type_Declaration then
12056 Record_Definition := Type_Definition (Decl);
12057
12058 -- If we have a record extension, set Sbit to point after the last
12059 -- component of the parent type, by calling Record_Hole_Check
12060 -- recursively.
12061
12062 if Nkind (Record_Definition) = N_Derived_Type_Definition then
12063 Record_Definition := Record_Extension_Part (Record_Definition);
12064 Record_Hole_Check (Underlying_Type (Parent_Subtype (Rectype)),
12065 After_Last => Sbit, Warn => False);
12066 end if;
12067
12068 if Nkind (Record_Definition) = N_Record_Definition then
12069 Check_Component_List
12070 (Discriminant_Specifications (Decl),
12071 Component_List (Record_Definition),
12072 Sbit, After_Last);
12073 end if;
12074 end if;
12075 end Record_Hole_Check;
12076
12077 -- Start of processing for Check_Record_Representation_Clause
12078
12079 begin
12080 Find_Type (Ident);
12081 Rectype := Entity (Ident);
12082
12083 if Rectype = Any_Type then
12084 return;
12085 end if;
12086
12087 Rectype := Underlying_Type (Rectype);
12088
12089 -- See if we have a fully repped derived tagged type
12090
12091 declare
12092 PS : constant Entity_Id := Parent_Subtype (Rectype);
12093
12094 begin
12095 if Present (PS) and then Known_Static_RM_Size (PS) then
12096 Tagged_Parent := PS;
12097 Parent_Last_Bit := RM_Size (PS) - 1;
12098
12099 elsif Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
12100 Tagged_Parent := PS;
12101
12102 -- Find maximum bit of any component of the parent type
12103
12104 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
12105 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
12106 while Present (Pcomp) loop
12107 if Present (Component_Bit_Offset (Pcomp))
12108 and then Known_Static_Esize (Pcomp)
12109 then
12110 Parent_Last_Bit :=
12111 UI_Max
12112 (Parent_Last_Bit,
12113 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
12114 end if;
12115
12116 Next_Component_Or_Discriminant (Pcomp);
12117 end loop;
12118 end if;
12119 end;
12120
12121 -- All done if no component clauses
12122
12123 CC := First (Component_Clauses (N));
12124
12125 if No (CC) then
12126 return;
12127 end if;
12128
12129 -- If a tag is present, then create a component clause that places it
12130 -- at the start of the record (otherwise gigi may place it after other
12131 -- fields that have rep clauses).
12132
12133 Fent := First_Entity (Rectype);
12134
12135 if Nkind (Fent) = N_Defining_Identifier
12136 and then Chars (Fent) = Name_uTag
12137 then
12138 Set_Component_Bit_Offset (Fent, Uint_0);
12139 Set_Normalized_Position (Fent, Uint_0);
12140 Set_Normalized_First_Bit (Fent, Uint_0);
12141 Set_Esize (Fent, UI_From_Int (System_Address_Size));
12142
12143 Set_Component_Clause (Fent,
12144 Make_Component_Clause (Loc,
12145 Component_Name => Make_Identifier (Loc, Name_uTag),
12146
12147 Position => Make_Integer_Literal (Loc, Uint_0),
12148 First_Bit => Make_Integer_Literal (Loc, Uint_0),
12149 Last_Bit =>
12150 Make_Integer_Literal (Loc,
12151 UI_From_Int (System_Address_Size - 1))));
12152
12153 Ccount := Ccount + 1;
12154 end if;
12155
12156 Max_Bit_So_Far := Uint_Minus_1;
12157 Overlap_Check_Required := False;
12158
12159 -- Process the component clauses
12160
12161 while Present (CC) loop
12162 Find_Component;
12163
12164 if Present (Comp) then
12165 Ccount := Ccount + 1;
12166
12167 -- We need a full overlap check if record positions non-monotonic
12168
12169 if Fbit <= Max_Bit_So_Far then
12170 Overlap_Check_Required := True;
12171 end if;
12172
12173 Max_Bit_So_Far := Lbit;
12174
12175 -- Check bit position out of range of specified size
12176
12177 if Has_Size_Clause (Rectype)
12178 and then RM_Size (Rectype) <= Lbit
12179 then
12180 Error_Msg_Uint_1 := RM_Size (Rectype);
12181 Error_Msg_Uint_2 := Lbit + 1;
12182 Error_Msg_N ("bit number out of range of specified "
12183 & "size (expected ^, got ^)",
12184 Last_Bit (CC));
12185
12186 -- Check for overlap with tag or parent component
12187
12188 else
12189 if Is_Tagged_Type (Rectype)
12190 and then Fbit < System_Address_Size
12191 then
12192 Error_Msg_NE
12193 ("component overlaps tag field of&",
12194 Component_Name (CC), Rectype);
12195 Overlap_Detected := True;
12196
12197 elsif Present (Tagged_Parent)
12198 and then Fbit <= Parent_Last_Bit
12199 then
12200 Error_Msg_NE
12201 ("component overlaps parent field of&",
12202 Component_Name (CC), Rectype);
12203 Overlap_Detected := True;
12204 end if;
12205
12206 if Hbit < Lbit then
12207 Hbit := Lbit;
12208 end if;
12209 end if;
12210 end if;
12211
12212 Next (CC);
12213 end loop;
12214
12215 -- Now that we have processed all the component clauses, check for
12216 -- overlap. We have to leave this till last, since the components can
12217 -- appear in any arbitrary order in the representation clause.
12218
12219 -- We do not need this check if all specified ranges were monotonic,
12220 -- as recorded by Overlap_Check_Required being False at this stage.
12221
12222 -- This first section checks if there are any overlapping entries at
12223 -- all. It does this by sorting all entries and then seeing if there are
12224 -- any overlaps. If there are none, then that is decisive, but if there
12225 -- are overlaps, they may still be OK (they may result from fields in
12226 -- different variants).
12227
12228 if Overlap_Check_Required then
12229 Overlap_Check1 : declare
12230
12231 OC_Fbit : array (0 .. Ccount) of Uint;
12232 -- First-bit values for component clauses, the value is the offset
12233 -- of the first bit of the field from start of record. The zero
12234 -- entry is for use in sorting.
12235
12236 OC_Lbit : array (0 .. Ccount) of Uint;
12237 -- Last-bit values for component clauses, the value is the offset
12238 -- of the last bit of the field from start of record. The zero
12239 -- entry is for use in sorting.
12240
12241 OC_Count : Natural := 0;
12242 -- Count of entries in OC_Fbit and OC_Lbit
12243
12244 function OC_Lt (Op1, Op2 : Natural) return Boolean;
12245 -- Compare routine for Sort
12246
12247 procedure OC_Move (From : Natural; To : Natural);
12248 -- Move routine for Sort
12249
12250 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
12251
12252 -----------
12253 -- OC_Lt --
12254 -----------
12255
12256 function OC_Lt (Op1, Op2 : Natural) return Boolean is
12257 begin
12258 return OC_Fbit (Op1) < OC_Fbit (Op2);
12259 end OC_Lt;
12260
12261 -------------
12262 -- OC_Move --
12263 -------------
12264
12265 procedure OC_Move (From : Natural; To : Natural) is
12266 begin
12267 OC_Fbit (To) := OC_Fbit (From);
12268 OC_Lbit (To) := OC_Lbit (From);
12269 end OC_Move;
12270
12271 -- Start of processing for Overlap_Check
12272
12273 begin
12274 CC := First (Component_Clauses (N));
12275 while Present (CC) loop
12276
12277 -- Exclude component clause already marked in error
12278
12279 if not Error_Posted (CC) then
12280 Find_Component;
12281
12282 if Present (Comp) then
12283 OC_Count := OC_Count + 1;
12284 OC_Fbit (OC_Count) := Fbit;
12285 OC_Lbit (OC_Count) := Lbit;
12286 end if;
12287 end if;
12288
12289 Next (CC);
12290 end loop;
12291
12292 Sorting.Sort (OC_Count);
12293
12294 Overlap_Check_Required := False;
12295 for J in 1 .. OC_Count - 1 loop
12296 if OC_Lbit (J) >= OC_Fbit (J + 1) then
12297 Overlap_Check_Required := True;
12298 exit;
12299 end if;
12300 end loop;
12301 end Overlap_Check1;
12302 end if;
12303
12304 -- If Overlap_Check_Required is still True, then we have to do the full
12305 -- scale overlap check, since we have at least two fields that do
12306 -- overlap, and we need to know if that is OK since they are in
12307 -- different variant, or whether we have a definite problem.
12308
12309 if Overlap_Check_Required then
12310 Overlap_Check2 : declare
12311 C1_Ent, C2_Ent : Entity_Id;
12312 -- Entities of components being checked for overlap
12313
12314 Clist : Node_Id;
12315 -- Component_List node whose Component_Items are being checked
12316
12317 Citem : Node_Id;
12318 -- Component declaration for component being checked
12319
12320 begin
12321 C1_Ent := First_Entity (Base_Type (Rectype));
12322
12323 -- Loop through all components in record. For each component check
12324 -- for overlap with any of the preceding elements on the component
12325 -- list containing the component and also, if the component is in
12326 -- a variant, check against components outside the case structure.
12327 -- This latter test is repeated recursively up the variant tree.
12328
12329 Main_Component_Loop : while Present (C1_Ent) loop
12330 if Ekind (C1_Ent) not in E_Component | E_Discriminant then
12331 goto Continue_Main_Component_Loop;
12332 end if;
12333
12334 -- Skip overlap check if entity has no declaration node. This
12335 -- happens with discriminants in constrained derived types.
12336 -- Possibly we are missing some checks as a result, but that
12337 -- does not seem terribly serious.
12338
12339 if No (Declaration_Node (C1_Ent)) then
12340 goto Continue_Main_Component_Loop;
12341 end if;
12342
12343 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
12344
12345 -- Loop through component lists that need checking. Check the
12346 -- current component list and all lists in variants above us.
12347
12348 Component_List_Loop : loop
12349
12350 -- If derived type definition, go to full declaration
12351 -- If at outer level, check discriminants if there are any.
12352
12353 if Nkind (Clist) = N_Derived_Type_Definition then
12354 Clist := Parent (Clist);
12355 end if;
12356
12357 -- Outer level of record definition, check discriminants
12358 -- but be careful not to flag a non-stored discriminant
12359 -- and the stored discriminant it renames as overlapping.
12360
12361 if Nkind (Clist) in N_Full_Type_Declaration
12362 | N_Private_Type_Declaration
12363 then
12364 if Has_Discriminants (Defining_Identifier (Clist)) then
12365 C2_Ent :=
12366 First_Discriminant (Defining_Identifier (Clist));
12367 while Present (C2_Ent) loop
12368 exit when
12369 Original_Record_Component (C1_Ent) =
12370 Original_Record_Component (C2_Ent);
12371 Check_Component_Overlap (C1_Ent, C2_Ent);
12372 Next_Discriminant (C2_Ent);
12373 end loop;
12374 end if;
12375
12376 -- Record extension case
12377
12378 elsif Nkind (Clist) = N_Derived_Type_Definition then
12379 Clist := Empty;
12380
12381 -- Otherwise check one component list
12382
12383 else
12384 Citem := First (Component_Items (Clist));
12385 while Present (Citem) loop
12386 if Nkind (Citem) = N_Component_Declaration then
12387 C2_Ent := Defining_Identifier (Citem);
12388 exit when C1_Ent = C2_Ent;
12389 Check_Component_Overlap (C1_Ent, C2_Ent);
12390 end if;
12391
12392 Next (Citem);
12393 end loop;
12394 end if;
12395
12396 -- Check for variants above us (the parent of the Clist can
12397 -- be a variant, in which case its parent is a variant part,
12398 -- and the parent of the variant part is a component list
12399 -- whose components must all be checked against the current
12400 -- component for overlap).
12401
12402 if Nkind (Parent (Clist)) = N_Variant then
12403 Clist := Parent (Parent (Parent (Clist)));
12404
12405 -- Check for possible discriminant part in record, this
12406 -- is treated essentially as another level in the
12407 -- recursion. For this case the parent of the component
12408 -- list is the record definition, and its parent is the
12409 -- full type declaration containing the discriminant
12410 -- specifications.
12411
12412 elsif Nkind (Parent (Clist)) = N_Record_Definition then
12413 Clist := Parent (Parent ((Clist)));
12414
12415 -- If neither of these two cases, we are at the top of
12416 -- the tree.
12417
12418 else
12419 exit Component_List_Loop;
12420 end if;
12421 end loop Component_List_Loop;
12422
12423 <<Continue_Main_Component_Loop>>
12424 Next_Entity (C1_Ent);
12425
12426 end loop Main_Component_Loop;
12427 end Overlap_Check2;
12428 end if;
12429
12430 -- Skip the following warnings if overlap was detected; programmer
12431 -- should fix the errors first. Also skip the warnings for types in
12432 -- generics, because their representation information is not fully
12433 -- computed.
12434
12435 if not Overlap_Detected and then not In_Generic_Scope (Rectype) then
12436 -- Check for record holes (gaps)
12437
12438 if Warn_On_Record_Holes then
12439 declare
12440 Ignore : Uint;
12441 begin
12442 Record_Hole_Check (Rectype, After_Last => Ignore, Warn => True);
12443 end;
12444 end if;
12445
12446 -- Check for out-of-order component clauses
12447
12448 if Warn_On_Component_Order then
12449 Component_Order_Check (Rectype);
12450 end if;
12451 end if;
12452
12453 -- For records that have component clauses for all components, and whose
12454 -- size is less than or equal to 32, and which can be fully packed, we
12455 -- need to know the size in the front end to activate possible packed
12456 -- array processing where the component type is a record.
12457
12458 -- At this stage Hbit + 1 represents the first unused bit from all the
12459 -- component clauses processed, so if the component clauses are
12460 -- complete, then this is the length of the record.
12461
12462 -- For records longer than System.Storage_Unit, and for those where not
12463 -- all components have component clauses, the back end determines the
12464 -- length (it may for example be appropriate to round up the size
12465 -- to some convenient boundary, based on alignment considerations, etc).
12466
12467 if not Known_RM_Size (Rectype)
12468 and then Hbit + 1 <= 32
12469 and then not Strict_Alignment (Rectype)
12470 then
12471
12472 -- Nothing to do if at least one component has no component clause
12473
12474 Comp := First_Component_Or_Discriminant (Rectype);
12475 while Present (Comp) loop
12476 exit when No (Component_Clause (Comp));
12477 Next_Component_Or_Discriminant (Comp);
12478 end loop;
12479
12480 -- If we fall out of loop, all components have component clauses
12481 -- and so we can set the size to the maximum value.
12482
12483 if No (Comp) then
12484 Set_RM_Size (Rectype, Hbit + 1);
12485 end if;
12486 end if;
12487 end Check_Record_Representation_Clause;
12488
12489 ----------------
12490 -- Check_Size --
12491 ----------------
12492
12493 procedure Check_Size
12494 (N : Node_Id;
12495 T : Entity_Id;
12496 Siz : Uint;
12497 Biased : out Boolean)
12498 is
12499 procedure Size_Too_Small_Error (Min_Siz : Uint);
12500 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
12501 -- minimum size.
12502
12503 --------------------------
12504 -- Size_Too_Small_Error --
12505 --------------------------
12506
12507 procedure Size_Too_Small_Error (Min_Siz : Uint) is
12508 begin
12509 Error_Msg_Uint_1 := Min_Siz;
12510 Error_Msg_NE (Size_Too_Small_Message, N, T);
12511 end Size_Too_Small_Error;
12512
12513 -- Local variables
12514
12515 UT : constant Entity_Id := Underlying_Type (T);
12516 M : Uint;
12517
12518 -- Start of processing for Check_Size
12519
12520 begin
12521 Biased := False;
12522
12523 -- Reject patently improper size values
12524
12525 if Is_Elementary_Type (T)
12526 and then Siz > Int'Last
12527 then
12528 Error_Msg_N ("Size value too large for elementary type", N);
12529
12530 if Nkind (Original_Node (N)) = N_Op_Expon then
12531 Error_Msg_N
12532 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
12533 end if;
12534 end if;
12535
12536 -- Dismiss generic types
12537
12538 if Is_Generic_Type (T)
12539 or else
12540 Is_Generic_Type (UT)
12541 or else
12542 Is_Generic_Type (Root_Type (UT))
12543 then
12544 return;
12545
12546 -- Guard against previous errors
12547
12548 elsif No (UT) or else UT = Any_Type then
12549 Check_Error_Detected;
12550 return;
12551
12552 -- Check case of bit packed array
12553
12554 elsif Is_Array_Type (UT)
12555 and then Known_Static_Component_Size (UT)
12556 and then Is_Bit_Packed_Array (UT)
12557 then
12558 declare
12559 Asiz : Uint;
12560 Indx : Node_Id;
12561 Ityp : Entity_Id;
12562
12563 begin
12564 Asiz := Component_Size (UT);
12565 Indx := First_Index (UT);
12566 loop
12567 Ityp := Etype (Indx);
12568
12569 -- If non-static bound, then we are not in the business of
12570 -- trying to check the length, and indeed an error will be
12571 -- issued elsewhere, since sizes of non-static array types
12572 -- cannot be set implicitly or explicitly.
12573
12574 if not Is_OK_Static_Subtype (Ityp) then
12575 return;
12576 end if;
12577
12578 -- Otherwise accumulate next dimension
12579
12580 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
12581 Expr_Value (Type_Low_Bound (Ityp)) +
12582 Uint_1);
12583
12584 Next_Index (Indx);
12585 exit when No (Indx);
12586 end loop;
12587
12588 if Asiz <= Siz then
12589 return;
12590
12591 else
12592 Size_Too_Small_Error (Asiz);
12593 end if;
12594 end;
12595
12596 -- All other composite types are ignored
12597
12598 elsif Is_Composite_Type (UT) then
12599 return;
12600
12601 -- For fixed-point types, don't check minimum if type is not frozen,
12602 -- since we don't know all the characteristics of the type that can
12603 -- affect the size (e.g. a specified small) till freeze time.
12604
12605 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
12606 null;
12607
12608 -- Cases for which a minimum check is required
12609
12610 else
12611 -- Ignore if specified size is correct for the type
12612
12613 if Known_Esize (UT) and then Siz = Esize (UT) then
12614 return;
12615 end if;
12616
12617 -- Otherwise get minimum size
12618
12619 M := UI_From_Int (Minimum_Size (UT));
12620
12621 if Siz < M then
12622
12623 -- Size is less than minimum size, but one possibility remains
12624 -- that we can manage with the new size if we bias the type.
12625
12626 M := UI_From_Int (Minimum_Size (UT, Biased => True));
12627
12628 if Siz < M then
12629 Size_Too_Small_Error (M);
12630 else
12631 Biased := True;
12632 end if;
12633 end if;
12634 end if;
12635 end Check_Size;
12636
12637 --------------------------
12638 -- Freeze_Entity_Checks --
12639 --------------------------
12640
12641 procedure Freeze_Entity_Checks (N : Node_Id) is
12642 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
12643 -- Inspect the primitive operations of type Typ and hide all pairs of
12644 -- implicitly declared non-overridden non-fully conformant homographs
12645 -- (Ada RM 8.3 12.3/2).
12646
12647 -------------------------------------
12648 -- Hide_Non_Overridden_Subprograms --
12649 -------------------------------------
12650
12651 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
12652 procedure Hide_Matching_Homographs
12653 (Subp_Id : Entity_Id;
12654 Start_Elmt : Elmt_Id);
12655 -- Inspect a list of primitive operations starting with Start_Elmt
12656 -- and find matching implicitly declared non-overridden non-fully
12657 -- conformant homographs of Subp_Id. If found, all matches along
12658 -- with Subp_Id are hidden from all visibility.
12659
12660 function Is_Non_Overridden_Or_Null_Procedure
12661 (Subp_Id : Entity_Id) return Boolean;
12662 -- Determine whether subprogram Subp_Id is implicitly declared non-
12663 -- overridden subprogram or an implicitly declared null procedure.
12664
12665 ------------------------------
12666 -- Hide_Matching_Homographs --
12667 ------------------------------
12668
12669 procedure Hide_Matching_Homographs
12670 (Subp_Id : Entity_Id;
12671 Start_Elmt : Elmt_Id)
12672 is
12673 Prim : Entity_Id;
12674 Prim_Elmt : Elmt_Id;
12675
12676 begin
12677 Prim_Elmt := Start_Elmt;
12678 while Present (Prim_Elmt) loop
12679 Prim := Node (Prim_Elmt);
12680
12681 -- The current primitive is implicitly declared non-overridden
12682 -- non-fully conformant homograph of Subp_Id. Both subprograms
12683 -- must be hidden from visibility.
12684
12685 if Chars (Prim) = Chars (Subp_Id)
12686 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
12687 and then not Fully_Conformant (Prim, Subp_Id)
12688 then
12689 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
12690 Set_Is_Immediately_Visible (Prim, False);
12691 Set_Is_Potentially_Use_Visible (Prim, False);
12692
12693 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
12694 Set_Is_Immediately_Visible (Subp_Id, False);
12695 Set_Is_Potentially_Use_Visible (Subp_Id, False);
12696 end if;
12697
12698 Next_Elmt (Prim_Elmt);
12699 end loop;
12700 end Hide_Matching_Homographs;
12701
12702 -----------------------------------------
12703 -- Is_Non_Overridden_Or_Null_Procedure --
12704 -----------------------------------------
12705
12706 function Is_Non_Overridden_Or_Null_Procedure
12707 (Subp_Id : Entity_Id) return Boolean
12708 is
12709 Alias_Id : Entity_Id;
12710
12711 begin
12712 -- The subprogram is inherited (implicitly declared), it does not
12713 -- override and does not cover a primitive of an interface.
12714
12715 if Ekind (Subp_Id) in E_Function | E_Procedure
12716 and then Present (Alias (Subp_Id))
12717 and then No (Interface_Alias (Subp_Id))
12718 and then No (Overridden_Operation (Subp_Id))
12719 then
12720 Alias_Id := Alias (Subp_Id);
12721
12722 if Requires_Overriding (Alias_Id) then
12723 return True;
12724
12725 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
12726 and then Null_Present (Parent (Alias_Id))
12727 then
12728 return True;
12729 end if;
12730 end if;
12731
12732 return False;
12733 end Is_Non_Overridden_Or_Null_Procedure;
12734
12735 -- Local variables
12736
12737 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
12738 Prim : Entity_Id;
12739 Prim_Elmt : Elmt_Id;
12740
12741 -- Start of processing for Hide_Non_Overridden_Subprograms
12742
12743 begin
12744 -- Inspect the list of primitives looking for non-overridden
12745 -- subprograms.
12746
12747 if Present (Prim_Ops) then
12748 Prim_Elmt := First_Elmt (Prim_Ops);
12749 while Present (Prim_Elmt) loop
12750 Prim := Node (Prim_Elmt);
12751 Next_Elmt (Prim_Elmt);
12752
12753 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
12754 Hide_Matching_Homographs
12755 (Subp_Id => Prim,
12756 Start_Elmt => Prim_Elmt);
12757 end if;
12758 end loop;
12759 end if;
12760 end Hide_Non_Overridden_Subprograms;
12761
12762 -- Local variables
12763
12764 E : constant Entity_Id := Entity (N);
12765
12766 Nongeneric_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
12767 -- True in nongeneric case. Some of the processing here is skipped
12768 -- for the generic case since it is not needed. Basically in the
12769 -- generic case, we only need to do stuff that might generate error
12770 -- messages or warnings.
12771
12772 -- Start of processing for Freeze_Entity_Checks
12773
12774 begin
12775 -- Remember that we are processing a freezing entity. Required to
12776 -- ensure correct decoration of internal entities associated with
12777 -- interfaces (see New_Overloaded_Entity).
12778
12779 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
12780
12781 -- For tagged types covering interfaces add internal entities that link
12782 -- the primitives of the interfaces with the primitives that cover them.
12783 -- Note: These entities were originally generated only when generating
12784 -- code because their main purpose was to provide support to initialize
12785 -- the secondary dispatch tables. They are also used to locate
12786 -- primitives covering interfaces when processing generics (see
12787 -- Derive_Subprograms).
12788
12789 -- This is not needed in the generic case
12790
12791 if Ada_Version >= Ada_2005
12792 and then Nongeneric_Case
12793 and then Ekind (E) = E_Record_Type
12794 and then Is_Tagged_Type (E)
12795 and then not Is_Interface (E)
12796 and then Has_Interfaces (E)
12797 then
12798 -- This would be a good common place to call the routine that checks
12799 -- overriding of interface primitives (and thus factorize calls to
12800 -- Check_Abstract_Overriding located at different contexts in the
12801 -- compiler). However, this is not possible because it causes
12802 -- spurious errors in case of late overriding.
12803
12804 Add_Internal_Interface_Entities (E);
12805 end if;
12806
12807 -- After all forms of overriding have been resolved, a tagged type may
12808 -- be left with a set of implicitly declared and possibly erroneous
12809 -- abstract subprograms, null procedures and subprograms that require
12810 -- overriding. If this set contains fully conformant homographs, then
12811 -- one is chosen arbitrarily (already done during resolution), otherwise
12812 -- all remaining non-fully conformant homographs are hidden from
12813 -- visibility (Ada RM 8.3 12.3/2).
12814
12815 if Is_Tagged_Type (E) then
12816 Hide_Non_Overridden_Subprograms (E);
12817 end if;
12818
12819 -- Check CPP types
12820
12821 if Ekind (E) = E_Record_Type
12822 and then Is_CPP_Class (E)
12823 and then Is_Tagged_Type (E)
12824 and then Tagged_Type_Expansion
12825 then
12826 if CPP_Num_Prims (E) = 0 then
12827
12828 -- If the CPP type has user defined components then it must import
12829 -- primitives from C++. This is required because if the C++ class
12830 -- has no primitives then the C++ compiler does not added the _tag
12831 -- component to the type.
12832
12833 if First_Entity (E) /= Last_Entity (E) then
12834 Error_Msg_N
12835 ("'C'P'P type must import at least one primitive from C++??",
12836 E);
12837 end if;
12838 end if;
12839
12840 -- Check that all its primitives are abstract or imported from C++.
12841 -- Check also availability of the C++ constructor.
12842
12843 declare
12844 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
12845 Elmt : Elmt_Id;
12846 Error_Reported : Boolean := False;
12847 Prim : Node_Id;
12848
12849 begin
12850 Elmt := First_Elmt (Primitive_Operations (E));
12851 while Present (Elmt) loop
12852 Prim := Node (Elmt);
12853
12854 if Comes_From_Source (Prim) then
12855 if Is_Abstract_Subprogram (Prim) then
12856 null;
12857
12858 elsif not Is_Imported (Prim)
12859 or else Convention (Prim) /= Convention_CPP
12860 then
12861 Error_Msg_N
12862 ("primitives of 'C'P'P types must be imported from C++ "
12863 & "or abstract??", Prim);
12864
12865 elsif not Has_Constructors
12866 and then not Error_Reported
12867 then
12868 Error_Msg_Name_1 := Chars (E);
12869 Error_Msg_N
12870 ("??'C'P'P constructor required for type %", Prim);
12871 Error_Reported := True;
12872 end if;
12873 end if;
12874
12875 Next_Elmt (Elmt);
12876 end loop;
12877 end;
12878 end if;
12879
12880 -- Check Ada derivation of CPP type
12881
12882 if Expander_Active -- why? losing errors in -gnatc mode???
12883 and then Present (Etype (E)) -- defend against errors
12884 and then Tagged_Type_Expansion
12885 and then Ekind (E) = E_Record_Type
12886 and then Etype (E) /= E
12887 and then Is_CPP_Class (Etype (E))
12888 and then CPP_Num_Prims (Etype (E)) > 0
12889 and then not Is_CPP_Class (E)
12890 and then not Has_CPP_Constructors (Etype (E))
12891 then
12892 -- If the parent has C++ primitives but it has no constructor then
12893 -- check that all the primitives are overridden in this derivation;
12894 -- otherwise the constructor of the parent is needed to build the
12895 -- dispatch table.
12896
12897 declare
12898 Elmt : Elmt_Id;
12899 Prim : Node_Id;
12900
12901 begin
12902 Elmt := First_Elmt (Primitive_Operations (E));
12903 while Present (Elmt) loop
12904 Prim := Node (Elmt);
12905
12906 if not Is_Abstract_Subprogram (Prim)
12907 and then No (Interface_Alias (Prim))
12908 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
12909 then
12910 Error_Msg_Name_1 := Chars (Etype (E));
12911 Error_Msg_N
12912 ("'C'P'P constructor required for parent type %", E);
12913 exit;
12914 end if;
12915
12916 Next_Elmt (Elmt);
12917 end loop;
12918 end;
12919 end if;
12920
12921 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
12922
12923 -- For a record type, deal with variant parts. This has to be delayed to
12924 -- this point, because of the issue of statically predicated subtypes,
12925 -- which we have to ensure are frozen before checking choices, since we
12926 -- need to have the static choice list set.
12927
12928 if Is_Record_Type (E) then
12929 Check_Variant_Part : declare
12930 D : constant Node_Id := Declaration_Node (E);
12931 T : Node_Id;
12932 C : Node_Id;
12933 VP : Node_Id;
12934
12935 Others_Present : Boolean;
12936 pragma Warnings (Off, Others_Present);
12937 -- Indicates others present, not used in this case
12938
12939 procedure Non_Static_Choice_Error (Choice : Node_Id);
12940 -- Error routine invoked by the generic instantiation below when
12941 -- the variant part has a non static choice.
12942
12943 procedure Process_Declarations (Variant : Node_Id);
12944 -- Processes declarations associated with a variant. We analyzed
12945 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
12946 -- but we still need the recursive call to Check_Choices for any
12947 -- nested variant to get its choices properly processed. This is
12948 -- also where we expand out the choices if expansion is active.
12949
12950 package Variant_Choices_Processing is new
12951 Generic_Check_Choices
12952 (Process_Empty_Choice => No_OP,
12953 Process_Non_Static_Choice => Non_Static_Choice_Error,
12954 Process_Associated_Node => Process_Declarations);
12955 use Variant_Choices_Processing;
12956
12957 -----------------------------
12958 -- Non_Static_Choice_Error --
12959 -----------------------------
12960
12961 procedure Non_Static_Choice_Error (Choice : Node_Id) is
12962 begin
12963 Flag_Non_Static_Expr
12964 ("choice given in variant part is not static!", Choice);
12965 end Non_Static_Choice_Error;
12966
12967 --------------------------
12968 -- Process_Declarations --
12969 --------------------------
12970
12971 procedure Process_Declarations (Variant : Node_Id) is
12972 CL : constant Node_Id := Component_List (Variant);
12973 VP : Node_Id;
12974
12975 begin
12976 -- Check for static predicate present in this variant
12977
12978 if Has_SP_Choice (Variant) then
12979
12980 -- Here we expand. You might expect to find this call in
12981 -- Expand_N_Variant_Part, but that is called when we first
12982 -- see the variant part, and we cannot do this expansion
12983 -- earlier than the freeze point, since for statically
12984 -- predicated subtypes, the predicate is not known till
12985 -- the freeze point.
12986
12987 -- Furthermore, we do this expansion even if the expander
12988 -- is not active, because other semantic processing, e.g.
12989 -- for aggregates, requires the expanded list of choices.
12990
12991 -- If the expander is not active, then we can't just clobber
12992 -- the list since it would invalidate the tree.
12993 -- So we have to rewrite the variant part with a Rewrite
12994 -- call that replaces it with a copy and clobber the copy.
12995
12996 if not Expander_Active then
12997 declare
12998 NewV : constant Node_Id := New_Copy (Variant);
12999 begin
13000 Set_Discrete_Choices
13001 (NewV, New_Copy_List (Discrete_Choices (Variant)));
13002 Rewrite (Variant, NewV);
13003 end;
13004 end if;
13005
13006 Expand_Static_Predicates_In_Choices (Variant);
13007 end if;
13008
13009 -- We don't need to worry about the declarations in the variant
13010 -- (since they were analyzed by Analyze_Choices when we first
13011 -- encountered the variant), but we do need to take care of
13012 -- expansion of any nested variants.
13013
13014 if not Null_Present (CL) then
13015 VP := Variant_Part (CL);
13016
13017 if Present (VP) then
13018 Check_Choices
13019 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
13020 end if;
13021 end if;
13022 end Process_Declarations;
13023
13024 -- Start of processing for Check_Variant_Part
13025
13026 begin
13027 -- Find component list
13028
13029 C := Empty;
13030
13031 if Nkind (D) = N_Full_Type_Declaration then
13032 T := Type_Definition (D);
13033
13034 if Nkind (T) = N_Record_Definition then
13035 C := Component_List (T);
13036
13037 elsif Nkind (T) = N_Derived_Type_Definition
13038 and then Present (Record_Extension_Part (T))
13039 then
13040 C := Component_List (Record_Extension_Part (T));
13041 end if;
13042 end if;
13043
13044 -- Case of variant part present
13045
13046 if Present (C) and then Present (Variant_Part (C)) then
13047 VP := Variant_Part (C);
13048
13049 -- Check choices
13050
13051 Check_Choices
13052 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
13053
13054 -- If the last variant does not contain the Others choice,
13055 -- replace it with an N_Others_Choice node since Gigi always
13056 -- wants an Others. Note that we do not bother to call Analyze
13057 -- on the modified variant part, since its only effect would be
13058 -- to compute the Others_Discrete_Choices node laboriously, and
13059 -- of course we already know the list of choices corresponding
13060 -- to the others choice (it's the list we're replacing).
13061
13062 -- We only want to do this if the expander is active, since
13063 -- we do not want to clobber the tree.
13064
13065 if Expander_Active then
13066 declare
13067 Last_Var : constant Node_Id :=
13068 Last_Non_Pragma (Variants (VP));
13069
13070 Others_Node : Node_Id;
13071
13072 begin
13073 if Nkind (First (Discrete_Choices (Last_Var))) /=
13074 N_Others_Choice
13075 then
13076 Others_Node := Make_Others_Choice (Sloc (Last_Var));
13077 Set_Others_Discrete_Choices
13078 (Others_Node, Discrete_Choices (Last_Var));
13079 Set_Discrete_Choices
13080 (Last_Var, New_List (Others_Node));
13081 end if;
13082 end;
13083 end if;
13084 end if;
13085 end Check_Variant_Part;
13086 end if;
13087
13088 -- If we have a type with predicates, build predicate function. This is
13089 -- not needed in the generic case, nor within e.g. TSS subprograms and
13090 -- other predefined primitives. For a derived type, ensure that the
13091 -- parent type is already frozen so that its predicate function has been
13092 -- constructed already. This is necessary if the parent is declared
13093 -- in a nested package and its own freeze point has not been reached.
13094
13095 if Is_Type (E)
13096 and then Nongeneric_Case
13097 and then Has_Predicates (E)
13098 and then Predicate_Check_In_Scope (N)
13099 then
13100 declare
13101 Atyp : constant Entity_Id := Nearest_Ancestor (E);
13102
13103 begin
13104 if Present (Atyp)
13105 and then Has_Predicates (Atyp)
13106 and then not Is_Frozen (Atyp)
13107 then
13108 Freeze_Before (N, Atyp);
13109 end if;
13110 end;
13111
13112 -- Before we build a predicate function, ensure that discriminant
13113 -- checking functions are available. The predicate function might
13114 -- need to call these functions if the predicate references any
13115 -- components declared in a variant part.
13116
13117 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
13118 Build_Or_Copy_Discr_Checking_Funcs (Parent (E));
13119 end if;
13120
13121 Build_Predicate_Function (E, N);
13122 end if;
13123
13124 -- If type has delayed aspects, this is where we do the preanalysis at
13125 -- the freeze point, as part of the consistent visibility check. Note
13126 -- that this must be done after calling Build_Predicate_Function or
13127 -- Build_Invariant_Procedure since these subprograms fix occurrences of
13128 -- the subtype name in the saved expression so that they will not cause
13129 -- trouble in the preanalysis.
13130
13131 -- This is also not needed in the generic case
13132
13133 if Nongeneric_Case
13134 and then Has_Delayed_Aspects (E)
13135 and then Scope (E) = Current_Scope
13136 then
13137 declare
13138 Ritem : Node_Id;
13139
13140 begin
13141 -- Look for aspect specification entries for this entity
13142
13143 Ritem := First_Rep_Item (E);
13144 while Present (Ritem) loop
13145 if Nkind (Ritem) = N_Aspect_Specification
13146 and then Entity (Ritem) = E
13147 and then Is_Delayed_Aspect (Ritem)
13148 then
13149 if Get_Aspect_Id (Ritem) in Aspect_CPU
13150 | Aspect_Dynamic_Predicate
13151 | Aspect_Predicate
13152 | Aspect_Static_Predicate
13153 | Aspect_Priority
13154 then
13155 -- Retrieve the visibility to components and discriminants
13156 -- in order to properly analyze the aspects.
13157
13158 Push_Type (E);
13159 Check_Aspect_At_Freeze_Point (Ritem);
13160
13161 -- In the case of predicate aspects, there will be
13162 -- a corresponding Predicate pragma associated with
13163 -- the aspect, and the expression of the pragma also
13164 -- needs to be analyzed at this point, to ensure that
13165 -- Save_Global_References will capture global refs in
13166 -- expressions that occur in generic bodies, for proper
13167 -- later resolution of the pragma in instantiations.
13168
13169 if Is_Type (E)
13170 and then Inside_A_Generic
13171 and then Has_Predicates (E)
13172 and then Present (Aspect_Rep_Item (Ritem))
13173 then
13174 declare
13175 Pragma_Args : constant List_Id :=
13176 Pragma_Argument_Associations
13177 (Aspect_Rep_Item (Ritem));
13178 Pragma_Expr : constant Node_Id :=
13179 Expression (Next (First (Pragma_Args)));
13180 begin
13181 if Present (Pragma_Expr) then
13182 Analyze_And_Resolve
13183 (Pragma_Expr, Standard_Boolean);
13184 end if;
13185 end;
13186 end if;
13187
13188 Pop_Type (E);
13189
13190 else
13191 Check_Aspect_At_Freeze_Point (Ritem);
13192 end if;
13193
13194 -- A pragma Predicate should be checked like one of the
13195 -- corresponding aspects, wrt possible misuse of ghost
13196 -- entities.
13197
13198 elsif Nkind (Ritem) = N_Pragma
13199 and then No (Corresponding_Aspect (Ritem))
13200 and then
13201 Get_Pragma_Id (Pragma_Name (Ritem)) = Pragma_Predicate
13202 then
13203 -- Retrieve the visibility to components and discriminants
13204 -- in order to properly analyze the pragma.
13205
13206 declare
13207 Arg : constant Node_Id :=
13208 Next (First (Pragma_Argument_Associations (Ritem)));
13209 begin
13210 Push_Type (E);
13211 Preanalyze_Spec_Expression
13212 (Expression (Arg), Standard_Boolean);
13213 Pop_Type (E);
13214 end;
13215 end if;
13216
13217 Next_Rep_Item (Ritem);
13218 end loop;
13219 end;
13220 end if;
13221
13222 if not In_Generic_Scope (E)
13223 and then Ekind (E) = E_Record_Type
13224 and then Is_Tagged_Type (E)
13225 then
13226 Process_Class_Conditions_At_Freeze_Point (E);
13227 end if;
13228 end Freeze_Entity_Checks;
13229
13230 -------------------------
13231 -- Get_Alignment_Value --
13232 -------------------------
13233
13234 function Get_Alignment_Value (Expr : Node_Id) return Uint is
13235 Align : constant Uint := Static_Integer (Expr);
13236
13237 begin
13238 if No (Align) then
13239 return No_Uint;
13240
13241 elsif Align < 0 then
13242 Error_Msg_N ("alignment value must be positive", Expr);
13243 return No_Uint;
13244
13245 -- If Alignment is specified to be 0, we treat it the same as 1
13246
13247 elsif Align = 0 then
13248 return Uint_1;
13249
13250 else
13251 for J in Int range 0 .. 64 loop
13252 declare
13253 M : constant Uint := Uint_2 ** J;
13254
13255 begin
13256 exit when M = Align;
13257
13258 if M > Align then
13259 Error_Msg_N ("alignment value must be power of 2", Expr);
13260 return No_Uint;
13261 end if;
13262 end;
13263 end loop;
13264
13265 return Align;
13266 end if;
13267 end Get_Alignment_Value;
13268
13269 -----------------------------------
13270 -- Has_Compatible_Representation --
13271 -----------------------------------
13272
13273 function Has_Compatible_Representation
13274 (Target_Typ, Operand_Typ : Entity_Id) return Boolean
13275 is
13276 -- The subtype-specific representation attributes (Size and Alignment)
13277 -- do not affect representation from the point of view of this function.
13278
13279 T1 : constant Entity_Id := Implementation_Base_Type (Target_Typ);
13280 T2 : constant Entity_Id := Implementation_Base_Type (Operand_Typ);
13281
13282 begin
13283 -- Return true immediately for the same base type
13284
13285 if T1 = T2 then
13286 return True;
13287
13288 -- Tagged types always have the same representation, because it is not
13289 -- possible to specify different representations for common fields.
13290
13291 elsif Is_Tagged_Type (T1) then
13292 return True;
13293
13294 -- Representations are definitely different if conventions differ
13295
13296 elsif Convention (T1) /= Convention (T2) then
13297 return False;
13298
13299 -- Representations are different if component alignments or scalar
13300 -- storage orders differ.
13301
13302 elsif (Is_Record_Type (T1) or else Is_Array_Type (T1))
13303 and then
13304 (Is_Record_Type (T2) or else Is_Array_Type (T2))
13305 and then (Component_Alignment (T1) /= Component_Alignment (T2)
13306 or else
13307 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
13308 then
13309 return False;
13310 end if;
13311
13312 -- For arrays, the only real issue is component size. If we know the
13313 -- component size for both arrays, and it is the same, then that's
13314 -- good enough to know we don't have a change of representation.
13315
13316 if Is_Array_Type (T1) then
13317
13318 -- In a view conversion, if the target type is an array type having
13319 -- aliased components and the operand type is an array type having
13320 -- unaliased components, then a new object is created (4.6(58.3/4)).
13321
13322 if Has_Aliased_Components (T1)
13323 and then not Has_Aliased_Components (T2)
13324 then
13325 return False;
13326 end if;
13327
13328 if Known_Component_Size (T1)
13329 and then Known_Component_Size (T2)
13330 and then Component_Size (T1) = Component_Size (T2)
13331 then
13332 return True;
13333 end if;
13334
13335 -- For records, representations are different if reordering differs
13336
13337 elsif Is_Record_Type (T1)
13338 and then Is_Record_Type (T2)
13339 and then No_Reordering (T1) /= No_Reordering (T2)
13340 then
13341 return False;
13342 end if;
13343
13344 -- Types definitely have same representation if neither has non-standard
13345 -- representation since default representations are always consistent.
13346 -- If only one has non-standard representation, and the other does not,
13347 -- then we consider that they do not have the same representation. They
13348 -- might, but there is no way of telling early enough.
13349
13350 if Has_Non_Standard_Rep (T1) then
13351 if not Has_Non_Standard_Rep (T2) then
13352 return False;
13353 end if;
13354 else
13355 return not Has_Non_Standard_Rep (T2);
13356 end if;
13357
13358 -- Here the two types both have non-standard representation, and we need
13359 -- to determine if they have the same non-standard representation.
13360
13361 -- For arrays, we simply need to test if the component sizes are the
13362 -- same. Pragma Pack is reflected in modified component sizes, so this
13363 -- check also deals with pragma Pack.
13364
13365 if Is_Array_Type (T1) then
13366 return Component_Size (T1) = Component_Size (T2);
13367
13368 -- Case of record types
13369
13370 elsif Is_Record_Type (T1) then
13371
13372 -- Packed status must conform
13373
13374 if Is_Packed (T1) /= Is_Packed (T2) then
13375 return False;
13376
13377 -- If the operand type is derived from the target type and no clause
13378 -- has been given after the derivation, then the representations are
13379 -- the same since the derived type inherits that of the parent type.
13380
13381 elsif Is_Derived_Type (T2)
13382 and then Etype (T2) = T1
13383 and then not Has_Record_Rep_Clause (T2)
13384 then
13385 return True;
13386
13387 -- Otherwise we must check components. Typ2 maybe a constrained
13388 -- subtype with fewer components, so we compare the components
13389 -- of the base types.
13390
13391 else
13392 Record_Case : declare
13393 CD1, CD2 : Entity_Id;
13394
13395 function Same_Rep return Boolean;
13396 -- CD1 and CD2 are either components or discriminants. This
13397 -- function tests whether they have the same representation.
13398
13399 --------------
13400 -- Same_Rep --
13401 --------------
13402
13403 function Same_Rep return Boolean is
13404 begin
13405 if No (Component_Clause (CD1)) then
13406 return No (Component_Clause (CD2));
13407 else
13408 -- Note: at this point, component clauses have been
13409 -- normalized to the default bit order, so that the
13410 -- comparison of Component_Bit_Offsets is meaningful.
13411
13412 return
13413 Present (Component_Clause (CD2))
13414 and then
13415 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
13416 and then
13417 Esize (CD1) = Esize (CD2);
13418 end if;
13419 end Same_Rep;
13420
13421 -- Start of processing for Record_Case
13422
13423 begin
13424 if Has_Discriminants (T1) then
13425
13426 -- The number of discriminants may be different if the
13427 -- derived type has fewer (constrained by values). The
13428 -- invisible discriminants retain the representation of
13429 -- the original, so the discrepancy does not per se
13430 -- indicate a different representation.
13431
13432 CD1 := First_Discriminant (T1);
13433 CD2 := First_Discriminant (T2);
13434 while Present (CD1) and then Present (CD2) loop
13435 if not Same_Rep then
13436 return False;
13437 else
13438 Next_Discriminant (CD1);
13439 Next_Discriminant (CD2);
13440 end if;
13441 end loop;
13442 end if;
13443
13444 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
13445 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
13446 while Present (CD1) loop
13447 if not Same_Rep then
13448 return False;
13449 else
13450 Next_Component (CD1);
13451 Next_Component (CD2);
13452 end if;
13453 end loop;
13454
13455 return True;
13456 end Record_Case;
13457 end if;
13458
13459 -- For enumeration types, we must check each literal to see if the
13460 -- representation is the same. Note that we do not permit enumeration
13461 -- representation clauses for Character and Wide_Character, so these
13462 -- cases were already dealt with.
13463
13464 elsif Is_Enumeration_Type (T1) then
13465 Enumeration_Case : declare
13466 L1, L2 : Entity_Id;
13467
13468 begin
13469 L1 := First_Literal (T1);
13470 L2 := First_Literal (T2);
13471 while Present (L1) loop
13472 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
13473 return False;
13474 else
13475 Next_Literal (L1);
13476 Next_Literal (L2);
13477 end if;
13478 end loop;
13479
13480 return True;
13481 end Enumeration_Case;
13482
13483 -- Any other types have the same representation for these purposes
13484
13485 else
13486 return True;
13487 end if;
13488 end Has_Compatible_Representation;
13489
13490 -------------------------------------
13491 -- Inherit_Aspects_At_Freeze_Point --
13492 -------------------------------------
13493
13494 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
13495 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13496 (Rep_Item : Node_Id) return Boolean;
13497 -- This routine checks if Rep_Item is either a pragma or an aspect
13498 -- specification node whose corresponding pragma (if any) is present in
13499 -- the Rep Item chain of the entity it has been specified to.
13500
13501 --------------------------------------------------
13502 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
13503 --------------------------------------------------
13504
13505 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13506 (Rep_Item : Node_Id) return Boolean
13507 is
13508 begin
13509 return
13510 Nkind (Rep_Item) = N_Pragma
13511 or else
13512 Present_In_Rep_Item (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
13513 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
13514
13515 -- Start of processing for Inherit_Aspects_At_Freeze_Point
13516
13517 begin
13518 -- A representation item is either subtype-specific (Size and Alignment
13519 -- clauses) or type-related (all others). Subtype-specific aspects may
13520 -- differ for different subtypes of the same type (RM 13.1.8).
13521
13522 -- A derived type inherits each type-related representation aspect of
13523 -- its parent type that was directly specified before the declaration of
13524 -- the derived type (RM 13.1.15).
13525
13526 -- A derived subtype inherits each subtype-specific representation
13527 -- aspect of its parent subtype that was directly specified before the
13528 -- declaration of the derived type (RM 13.1.15).
13529
13530 -- The general processing involves inheriting a representation aspect
13531 -- from a parent type whenever the first rep item (aspect specification,
13532 -- attribute definition clause, pragma) corresponding to the given
13533 -- representation aspect in the rep item chain of Typ, if any, isn't
13534 -- directly specified to Typ but to one of its parents.
13535
13536 -- In addition, Convention must be propagated from base type to subtype,
13537 -- because the subtype may have been declared on an incomplete view.
13538
13539 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
13540 return;
13541 end if;
13542
13543 -- Ada_05/Ada_2005
13544
13545 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
13546 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
13547 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13548 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
13549 then
13550 Set_Is_Ada_2005_Only (Typ);
13551 end if;
13552
13553 -- Ada_12/Ada_2012
13554
13555 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
13556 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
13557 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13558 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
13559 then
13560 Set_Is_Ada_2012_Only (Typ);
13561 end if;
13562
13563 -- Ada_2022
13564
13565 if not Has_Rep_Item (Typ, Name_Ada_2022, False)
13566 and then Has_Rep_Item (Typ, Name_Ada_2022)
13567 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13568 (Get_Rep_Item (Typ, Name_Ada_2022))
13569 then
13570 Set_Is_Ada_2022_Only (Typ);
13571 end if;
13572
13573 -- Atomic/Shared
13574
13575 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
13576 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
13577 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13578 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
13579 then
13580 Set_Is_Atomic (Typ);
13581 Set_Is_Volatile (Typ);
13582 Set_Treat_As_Volatile (Typ);
13583 end if;
13584
13585 -- Convention
13586
13587 if Is_Record_Type (Typ)
13588 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
13589 then
13590 Set_Convention (Typ, Convention (Base_Type (Typ)));
13591 end if;
13592
13593 -- Default_Component_Value
13594
13595 -- Verify that there is no rep_item declared for the type, and there
13596 -- is one coming from an ancestor.
13597
13598 if Is_Array_Type (Typ)
13599 and then Is_Base_Type (Typ)
13600 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
13601 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
13602 then
13603 declare
13604 E : Entity_Id;
13605
13606 begin
13607 E := Entity (Get_Rep_Item (Typ, Name_Default_Component_Value));
13608
13609 -- Deal with private types
13610
13611 if Is_Private_Type (E) then
13612 E := Full_View (E);
13613 end if;
13614
13615 Set_Default_Aspect_Component_Value (Typ,
13616 Default_Aspect_Component_Value (E));
13617 end;
13618 end if;
13619
13620 -- Default_Value
13621
13622 if Is_Scalar_Type (Typ)
13623 and then Is_Base_Type (Typ)
13624 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
13625 and then Has_Rep_Item (Typ, Name_Default_Value)
13626 then
13627 Set_Has_Default_Aspect (Typ);
13628
13629 declare
13630 E : Entity_Id;
13631
13632 begin
13633 E := Entity (Get_Rep_Item (Typ, Name_Default_Value));
13634
13635 -- Deal with private types
13636
13637 if Is_Private_Type (E) then
13638 E := Full_View (E);
13639 end if;
13640
13641 Set_Default_Aspect_Value (Typ, Default_Aspect_Value (E));
13642 end;
13643 end if;
13644
13645 -- Discard_Names
13646
13647 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
13648 and then Has_Rep_Item (Typ, Name_Discard_Names)
13649 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13650 (Get_Rep_Item (Typ, Name_Discard_Names))
13651 then
13652 Set_Discard_Names (Typ);
13653 end if;
13654
13655 -- Volatile
13656
13657 if not Has_Rep_Item (Typ, Name_Volatile, False)
13658 and then Has_Rep_Item (Typ, Name_Volatile)
13659 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13660 (Get_Rep_Item (Typ, Name_Volatile))
13661 then
13662 Set_Is_Volatile (Typ);
13663 Set_Treat_As_Volatile (Typ);
13664 end if;
13665
13666 -- Volatile_Full_Access and Full_Access_Only
13667
13668 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
13669 and then not Has_Rep_Item (Typ, Name_Full_Access_Only, False)
13670 and then (Has_Rep_Item (Typ, Name_Volatile_Full_Access)
13671 or else Has_Rep_Item (Typ, Name_Full_Access_Only))
13672 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13673 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
13674 then
13675 Set_Is_Volatile_Full_Access (Typ);
13676 Set_Is_Volatile (Typ);
13677 Set_Treat_As_Volatile (Typ);
13678 end if;
13679
13680 -- Inheritance for derived types only
13681
13682 if Is_Derived_Type (Typ) then
13683 declare
13684 Bas_Typ : constant Entity_Id := Base_Type (Typ);
13685 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
13686
13687 begin
13688 -- Atomic_Components
13689
13690 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
13691 and then Has_Rep_Item (Typ, Name_Atomic_Components)
13692 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13693 (Get_Rep_Item (Typ, Name_Atomic_Components))
13694 then
13695 Set_Has_Atomic_Components (Imp_Bas_Typ);
13696 end if;
13697
13698 -- Volatile_Components
13699
13700 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
13701 and then Has_Rep_Item (Typ, Name_Volatile_Components)
13702 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13703 (Get_Rep_Item (Typ, Name_Volatile_Components))
13704 then
13705 Set_Has_Volatile_Components (Imp_Bas_Typ);
13706 end if;
13707
13708 -- Finalize_Storage_Only
13709
13710 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
13711 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
13712 then
13713 Set_Finalize_Storage_Only (Bas_Typ);
13714 end if;
13715
13716 -- Universal_Aliasing
13717
13718 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
13719 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
13720 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13721 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
13722 then
13723 Set_Universal_Aliasing (Imp_Bas_Typ);
13724 end if;
13725
13726 -- Bit_Order
13727
13728 if Is_Record_Type (Typ) and then Typ = Bas_Typ then
13729 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
13730 and then Has_Rep_Item (Typ, Name_Bit_Order)
13731 then
13732 Set_Reverse_Bit_Order (Bas_Typ,
13733 Reverse_Bit_Order
13734 (Implementation_Base_Type (Etype (Bas_Typ))));
13735 end if;
13736 end if;
13737
13738 -- Scalar_Storage_Order
13739
13740 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
13741 and then Typ = Bas_Typ
13742 then
13743 -- For a type extension, always inherit from parent; otherwise
13744 -- inherit if no default applies. Note: we do not check for
13745 -- an explicit rep item on the parent type when inheriting,
13746 -- because the parent SSO may itself have been set by default.
13747
13748 if not Has_Rep_Item (First_Subtype (Typ),
13749 Name_Scalar_Storage_Order, False)
13750 and then (Is_Tagged_Type (Bas_Typ)
13751 or else not (SSO_Set_Low_By_Default (Bas_Typ)
13752 or else
13753 SSO_Set_High_By_Default (Bas_Typ)))
13754 then
13755 Set_Reverse_Storage_Order (Bas_Typ,
13756 Reverse_Storage_Order
13757 (Implementation_Base_Type (Etype (Bas_Typ))));
13758
13759 -- Clear default SSO indications, since the inherited aspect
13760 -- which was set explicitly overrides the default.
13761
13762 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
13763 Set_SSO_Set_High_By_Default (Bas_Typ, False);
13764 end if;
13765 end if;
13766 end;
13767 end if;
13768 end Inherit_Aspects_At_Freeze_Point;
13769
13770 ---------------------------------
13771 -- Inherit_Delayed_Rep_Aspects --
13772 ---------------------------------
13773
13774 procedure Inherit_Delayed_Rep_Aspects (Typ : Entity_Id) is
13775 A : Aspect_Id;
13776 N : Node_Id;
13777 P : Entity_Id;
13778
13779 begin
13780 -- Find the first aspect that has been inherited
13781
13782 N := First_Rep_Item (Typ);
13783 while Present (N) loop
13784 if Nkind (N) = N_Aspect_Specification then
13785 exit when Entity (N) /= Typ;
13786 end if;
13787
13788 Next_Rep_Item (N);
13789 end loop;
13790
13791 -- There must be one if we reach here
13792
13793 pragma Assert (Present (N));
13794 P := Entity (N);
13795
13796 -- Loop through delayed aspects for the parent type
13797
13798 while Present (N) loop
13799 if Nkind (N) = N_Aspect_Specification then
13800 exit when Entity (N) /= P;
13801
13802 if Is_Delayed_Aspect (N) then
13803 A := Get_Aspect_Id (N);
13804
13805 -- Process delayed rep aspect. For Boolean attributes it is
13806 -- not possible to cancel an attribute once set (the attempt
13807 -- to use an aspect with xxx => False is an error) for a
13808 -- derived type. So for those cases, we do not have to check
13809 -- if a clause has been given for the derived type, since it
13810 -- is harmless to set it again if it is already set.
13811
13812 case A is
13813
13814 -- Alignment
13815
13816 when Aspect_Alignment =>
13817 if not Has_Alignment_Clause (Typ) then
13818 Set_Alignment (Typ, Alignment (P));
13819 end if;
13820
13821 -- Atomic
13822
13823 when Aspect_Atomic =>
13824 if Is_Atomic (P) then
13825 Set_Is_Atomic (Typ);
13826 end if;
13827
13828 -- Atomic_Components
13829
13830 when Aspect_Atomic_Components =>
13831 if Has_Atomic_Components (P) then
13832 Set_Has_Atomic_Components (Base_Type (Typ));
13833 end if;
13834
13835 -- Bit_Order
13836
13837 when Aspect_Bit_Order =>
13838 if Is_Record_Type (Typ)
13839 and then No (Get_Attribute_Definition_Clause
13840 (Typ, Attribute_Bit_Order))
13841 and then Reverse_Bit_Order (P)
13842 then
13843 Set_Reverse_Bit_Order (Base_Type (Typ));
13844 end if;
13845
13846 -- Component_Size
13847
13848 when Aspect_Component_Size =>
13849 if Is_Array_Type (Typ)
13850 and then not Has_Component_Size_Clause (Typ)
13851 then
13852 Set_Component_Size
13853 (Base_Type (Typ), Component_Size (P));
13854 end if;
13855
13856 -- Machine_Radix
13857
13858 when Aspect_Machine_Radix =>
13859 if Is_Decimal_Fixed_Point_Type (Typ)
13860 and then not Has_Machine_Radix_Clause (Typ)
13861 then
13862 Set_Machine_Radix_10 (Typ, Machine_Radix_10 (P));
13863 end if;
13864
13865 -- Object_Size (also Size which also sets Object_Size)
13866
13867 when Aspect_Object_Size
13868 | Aspect_Size
13869 =>
13870 if not Has_Size_Clause (Typ)
13871 and then
13872 No (Get_Attribute_Definition_Clause
13873 (Typ, Attribute_Object_Size))
13874 then
13875 Set_Esize (Typ, Esize (P));
13876 end if;
13877
13878 -- Pack
13879
13880 when Aspect_Pack =>
13881 if not Is_Packed (Typ) then
13882 Set_Is_Packed (Base_Type (Typ));
13883
13884 if Is_Bit_Packed_Array (P) then
13885 Set_Is_Bit_Packed_Array (Base_Type (Typ));
13886 Set_Packed_Array_Impl_Type
13887 (Typ, Packed_Array_Impl_Type (P));
13888 end if;
13889 end if;
13890
13891 -- Scalar_Storage_Order
13892
13893 when Aspect_Scalar_Storage_Order =>
13894 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
13895 and then No (Get_Attribute_Definition_Clause
13896 (Typ, Attribute_Scalar_Storage_Order))
13897 and then Reverse_Storage_Order (P)
13898 then
13899 Set_Reverse_Storage_Order (Base_Type (Typ));
13900
13901 -- Clear default SSO indications, since the aspect
13902 -- overrides the default.
13903
13904 Set_SSO_Set_Low_By_Default (Base_Type (Typ), False);
13905 Set_SSO_Set_High_By_Default (Base_Type (Typ), False);
13906 end if;
13907
13908 -- Small
13909
13910 when Aspect_Small =>
13911 if Is_Fixed_Point_Type (Typ)
13912 and then not Has_Small_Clause (Typ)
13913 then
13914 Set_Small_Value (Typ, Small_Value (P));
13915 end if;
13916
13917 -- Storage_Size
13918
13919 when Aspect_Storage_Size =>
13920 if (Is_Access_Type (Typ) or else Is_Task_Type (Typ))
13921 and then not Has_Storage_Size_Clause (Typ)
13922 then
13923 Set_Storage_Size_Variable
13924 (Base_Type (Typ), Storage_Size_Variable (P));
13925 end if;
13926
13927 -- Value_Size
13928
13929 when Aspect_Value_Size =>
13930
13931 -- Value_Size is never inherited, it is either set by
13932 -- default, or it is explicitly set for the derived
13933 -- type. So nothing to do here.
13934
13935 null;
13936
13937 -- Volatile
13938
13939 when Aspect_Volatile =>
13940 if Is_Volatile (P) then
13941 Set_Is_Volatile (Typ);
13942 end if;
13943
13944 -- Volatile_Full_Access (also Full_Access_Only)
13945
13946 when Aspect_Volatile_Full_Access
13947 | Aspect_Full_Access_Only
13948 =>
13949 if Is_Volatile_Full_Access (P) then
13950 Set_Is_Volatile_Full_Access (Typ);
13951 end if;
13952
13953 -- Volatile_Components
13954
13955 when Aspect_Volatile_Components =>
13956 if Has_Volatile_Components (P) then
13957 Set_Has_Volatile_Components (Base_Type (Typ));
13958 end if;
13959
13960 -- That should be all the Rep Aspects
13961
13962 when others =>
13963 pragma Assert (Aspect_Delay (A) /= Rep_Aspect);
13964 null;
13965 end case;
13966 end if;
13967 end if;
13968
13969 Next_Rep_Item (N);
13970 end loop;
13971 end Inherit_Delayed_Rep_Aspects;
13972
13973 ----------------
13974 -- Initialize --
13975 ----------------
13976
13977 procedure Initialize is
13978 begin
13979 Address_Clause_Checks.Init;
13980 Unchecked_Conversions.Init;
13981
13982 -- The following might be needed in the future for some non-GCC back
13983 -- ends:
13984 -- if AAMP_On_Target then
13985 -- Independence_Checks.Init;
13986 -- end if;
13987 end Initialize;
13988
13989 ---------------------------
13990 -- Install_Discriminants --
13991 ---------------------------
13992
13993 procedure Install_Discriminants (E : Entity_Id) is
13994 Disc : Entity_Id;
13995 Prev : Entity_Id;
13996 begin
13997 Disc := First_Discriminant (E);
13998 while Present (Disc) loop
13999 Prev := Current_Entity (Disc);
14000 Set_Current_Entity (Disc);
14001 Set_Is_Immediately_Visible (Disc);
14002 Set_Homonym (Disc, Prev);
14003 Next_Discriminant (Disc);
14004 end loop;
14005 end Install_Discriminants;
14006
14007 -------------------------
14008 -- Is_Operational_Item --
14009 -------------------------
14010
14011 function Is_Operational_Item (N : Node_Id) return Boolean is
14012 begin
14013 -- List of operational items is given in AARM 13.1(8.mm/1). It is
14014 -- clearly incomplete, as it does not include iterator aspects, among
14015 -- others.
14016
14017 return Nkind (N) = N_Attribute_Definition_Clause
14018 and then
14019 Get_Attribute_Id (Chars (N)) in Attribute_Constant_Indexing
14020 | Attribute_External_Tag
14021 | Attribute_Default_Iterator
14022 | Attribute_Implicit_Dereference
14023 | Attribute_Input
14024 | Attribute_Iterable
14025 | Attribute_Iterator_Element
14026 | Attribute_Output
14027 | Attribute_Put_Image
14028 | Attribute_Read
14029 | Attribute_Variable_Indexing
14030 | Attribute_Write;
14031 end Is_Operational_Item;
14032
14033 -------------------------
14034 -- Is_Predicate_Static --
14035 -------------------------
14036
14037 -- Note: the basic legality of the expression has already been checked, so
14038 -- we don't need to worry about cases or ranges on strings for example.
14039
14040 function Is_Predicate_Static
14041 (Expr : Node_Id;
14042 Nam : Name_Id;
14043 Warn : Boolean := True) return Boolean
14044 is
14045 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
14046 -- Given a list of case expression alternatives, returns True if all
14047 -- the alternatives are static (have all static choices, and a static
14048 -- expression).
14049
14050 function Is_Type_Ref (N : Node_Id) return Boolean;
14051 pragma Inline (Is_Type_Ref);
14052 -- Returns True if N is a reference to the type for the predicate in the
14053 -- expression (i.e. if it is an identifier whose Chars field matches the
14054 -- Nam given in the call). N must not be parenthesized, if the type name
14055 -- appears in parens, this routine will return False.
14056 --
14057 -- The routine also returns True for function calls generated during the
14058 -- expansion of comparison operators on strings, which are intended to
14059 -- be legal in static predicates, and are converted into calls to array
14060 -- comparison routines in the body of the corresponding predicate
14061 -- function.
14062
14063 ----------------------------------
14064 -- All_Static_Case_Alternatives --
14065 ----------------------------------
14066
14067 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
14068 N : Node_Id;
14069
14070 begin
14071 N := First (L);
14072 while Present (N) loop
14073 if not (All_Static_Choices (Discrete_Choices (N))
14074 and then Is_OK_Static_Expression (Expression (N)))
14075 then
14076 return False;
14077 end if;
14078
14079 Next (N);
14080 end loop;
14081
14082 return True;
14083 end All_Static_Case_Alternatives;
14084
14085 -----------------
14086 -- Is_Type_Ref --
14087 -----------------
14088
14089 function Is_Type_Ref (N : Node_Id) return Boolean is
14090 begin
14091 return (Nkind (N) = N_Identifier
14092 and then Chars (N) = Nam
14093 and then Paren_Count (N) = 0);
14094 end Is_Type_Ref;
14095
14096 -- helper function for recursive calls
14097 function Is_Predicate_Static_Aux (Expr : Node_Id) return Boolean is
14098 (Is_Predicate_Static (Expr, Nam, Warn => False));
14099
14100 -- Start of processing for Is_Predicate_Static
14101
14102 begin
14103 -- Handle cases like
14104 -- subtype S is Integer with Static_Predicate =>
14105 -- (Some_Integer_Variable in Integer) and then (S /= 0);
14106 -- where the predicate (which should be rejected) might have been
14107 -- transformed into just "(S /= 0)", which would appear to be
14108 -- a predicate-static expression (and therefore legal).
14109
14110 if Original_Node (Expr) /= Expr then
14111
14112 -- Emit warnings for predicates that are always True or always False
14113 -- and were not originally expressed as Boolean literals.
14114
14115 return Result : constant Boolean :=
14116 Is_Predicate_Static_Aux (Original_Node (Expr))
14117 do
14118 if Result and then Warn and then Is_Entity_Name (Expr) then
14119 if Entity (Expr) = Standard_True then
14120 Error_Msg_N ("predicate is redundant (always True)?", Expr);
14121 elsif Entity (Expr) = Standard_False then
14122 Error_Msg_N
14123 ("predicate is unsatisfiable (always False)?", Expr);
14124 end if;
14125 end if;
14126 end return;
14127 end if;
14128
14129 -- Predicate_Static means one of the following holds. Numbers are the
14130 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
14131
14132 -- 16: A static expression
14133
14134 if Is_OK_Static_Expression (Expr) then
14135 return True;
14136
14137 -- 17: A membership test whose simple_expression is the current
14138 -- instance, and whose membership_choice_list meets the requirements
14139 -- for a static membership test.
14140
14141 elsif Nkind (Expr) in N_Membership_Test
14142 and then Is_Type_Ref (Left_Opnd (Expr))
14143 and then All_Membership_Choices_Static (Expr)
14144 then
14145 return True;
14146
14147 -- 18. A case_expression whose selecting_expression is the current
14148 -- instance, and whose dependent expressions are static expressions.
14149
14150 elsif Nkind (Expr) = N_Case_Expression
14151 and then Is_Type_Ref (Expression (Expr))
14152 and then All_Static_Case_Alternatives (Alternatives (Expr))
14153 then
14154 return True;
14155
14156 -- 19. A call to a predefined equality or ordering operator, where one
14157 -- operand is the current instance, and the other is a static
14158 -- expression.
14159
14160 -- Note: the RM is clearly wrong here in not excluding string types.
14161 -- Without this exclusion, we would allow expressions like X > "ABC"
14162 -- to be considered as predicate-static, which is clearly not intended,
14163 -- since the idea is for predicate-static to be a subset of normal
14164 -- static expressions (and "DEF" > "ABC" is not a static expression).
14165
14166 -- However, we do allow internally generated (not from source) equality
14167 -- and inequality operations to be valid on strings (this helps deal
14168 -- with cases where we transform A in "ABC" to A = "ABC).
14169
14170 -- In fact, it appears that the intent of the ARG is to extend static
14171 -- predicates to strings, and that the extension should probably apply
14172 -- to static expressions themselves. The code below accepts comparison
14173 -- operators that apply to static strings.
14174
14175 elsif Nkind (Expr) in N_Op_Compare
14176 and then ((Is_Type_Ref (Left_Opnd (Expr))
14177 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
14178 or else
14179 (Is_Type_Ref (Right_Opnd (Expr))
14180 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
14181 then
14182 return True;
14183
14184 -- 20. A call to a predefined boolean logical operator, where each
14185 -- operand is predicate-static.
14186
14187 elsif (Nkind (Expr) in N_Op_And | N_Op_Or | N_Op_Xor
14188 and then Is_Predicate_Static_Aux (Left_Opnd (Expr))
14189 and then Is_Predicate_Static_Aux (Right_Opnd (Expr)))
14190 or else
14191 (Nkind (Expr) = N_Op_Not
14192 and then Is_Predicate_Static_Aux (Right_Opnd (Expr)))
14193 then
14194 return True;
14195
14196 -- 21. A short-circuit control form where both operands are
14197 -- predicate-static.
14198
14199 elsif Nkind (Expr) in N_Short_Circuit
14200 and then Is_Predicate_Static_Aux (Left_Opnd (Expr))
14201 and then Is_Predicate_Static_Aux (Right_Opnd (Expr))
14202 then
14203 return True;
14204
14205 -- 22. A parenthesized predicate-static expression. This does not
14206 -- require any special test, since we just ignore paren levels in
14207 -- all the cases above.
14208
14209 -- One more test that is an implementation artifact caused by the fact
14210 -- that we are analyzing not the original expression, but the generated
14211 -- expression in the body of the predicate function. This can include
14212 -- references to inherited predicates, so that the expression we are
14213 -- processing looks like:
14214
14215 -- xxPredicate (typ (Inns)) and then expression
14216
14217 -- Where the call is to a Predicate function for an inherited predicate.
14218 -- We simply ignore such a call, which could be to either a dynamic or
14219 -- a static predicate. Note that if the parent predicate is dynamic then
14220 -- eventually this type will be marked as dynamic, but you are allowed
14221 -- to specify a static predicate for a subtype which is inheriting a
14222 -- dynamic predicate, so the static predicate validation here ignores
14223 -- the inherited predicate even if it is dynamic.
14224 -- In all cases, a static predicate can only apply to a scalar type.
14225
14226 elsif Nkind (Expr) = N_Function_Call
14227 and then Is_Predicate_Function (Entity (Name (Expr)))
14228 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
14229 then
14230 return True;
14231
14232 -- That's an exhaustive list of tests, all other cases are not
14233 -- predicate-static, so we return False.
14234
14235 else
14236 return False;
14237 end if;
14238 end Is_Predicate_Static;
14239
14240 ----------------------
14241 -- Is_Static_Choice --
14242 ----------------------
14243
14244 function Is_Static_Choice (N : Node_Id) return Boolean is
14245 begin
14246 return Nkind (N) = N_Others_Choice
14247 or else Is_OK_Static_Expression (N)
14248 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
14249 and then Is_OK_Static_Subtype (Entity (N)))
14250 or else (Nkind (N) = N_Subtype_Indication
14251 and then Is_OK_Static_Subtype (Entity (N)))
14252 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
14253 end Is_Static_Choice;
14254
14255 ------------------------------
14256 -- Is_Type_Related_Rep_Item --
14257 ------------------------------
14258
14259 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean is
14260 begin
14261 case Nkind (N) is
14262 when N_Attribute_Definition_Clause =>
14263 -- See AARM 13.1(8.f-8.x) list items that end in "clause"
14264 -- ???: include any GNAT-defined attributes here?
14265 return Get_Attribute_Id (Chars (N)) in Attribute_Bit_Order
14266 | Attribute_Component_Size
14267 | Attribute_Machine_Radix
14268 | Attribute_Storage_Pool
14269 | Attribute_Stream_Size;
14270
14271 when N_Pragma =>
14272 case Get_Pragma_Id (N) is
14273 -- See AARM 13.1(8.f-8.x) list items that start with "pragma"
14274 -- ???: include any GNAT-defined pragmas here?
14275 when Pragma_Pack
14276 | Pragma_Import
14277 | Pragma_Export
14278 | Pragma_Convention
14279 | Pragma_Atomic
14280 | Pragma_Independent
14281 | Pragma_Volatile
14282 | Pragma_Atomic_Components
14283 | Pragma_Independent_Components
14284 | Pragma_Volatile_Components
14285 | Pragma_Discard_Names
14286 =>
14287 return True;
14288 when others =>
14289 null;
14290 end case;
14291
14292 when N_Enumeration_Representation_Clause
14293 | N_Record_Representation_Clause
14294 =>
14295 return True;
14296
14297 when others =>
14298 null;
14299 end case;
14300
14301 return False;
14302 end Is_Type_Related_Rep_Item;
14303
14304 ---------------------
14305 -- Kill_Rep_Clause --
14306 ---------------------
14307
14308 procedure Kill_Rep_Clause (N : Node_Id) is
14309 begin
14310 pragma Assert (Ignore_Rep_Clauses);
14311
14312 -- Note: we use Replace rather than Rewrite, because we don't want
14313 -- tools to be able to use Original_Node to dig out the (undecorated)
14314 -- rep clause that is being replaced.
14315
14316 Replace (N, Make_Null_Statement (Sloc (N)));
14317
14318 -- The null statement must be marked as not coming from source. This is
14319 -- so that tools ignore it, and also the back end does not expect bogus
14320 -- "from source" null statements in weird places (e.g. in declarative
14321 -- regions where such null statements are not allowed).
14322
14323 Set_Comes_From_Source (N, False);
14324 end Kill_Rep_Clause;
14325
14326 ------------------
14327 -- Minimum_Size --
14328 ------------------
14329
14330 function Minimum_Size
14331 (T : Entity_Id;
14332 Biased : Boolean := False) return Int
14333 is
14334 Lo : Uint := No_Uint;
14335 Hi : Uint := No_Uint;
14336 LoR : Ureal := No_Ureal;
14337 HiR : Ureal := No_Ureal;
14338 LoSet : Boolean := False;
14339 HiSet : Boolean := False;
14340 B : Uint;
14341 S : Nat;
14342 Ancest : Entity_Id;
14343 R_Typ : constant Entity_Id := Root_Type (T);
14344
14345 begin
14346 -- Bad type
14347
14348 if T = Any_Type then
14349 return Unknown_Minimum_Size;
14350
14351 -- For generic types, just return unknown. There cannot be any
14352 -- legitimate need to know such a size, but this routine may be
14353 -- called with a generic type as part of normal processing.
14354
14355 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
14356 return Unknown_Minimum_Size;
14357
14358 -- Access types (cannot have size smaller than System.Address)
14359
14360 elsif Is_Access_Type (T) then
14361 return System_Address_Size;
14362
14363 -- Floating-point types
14364
14365 elsif Is_Floating_Point_Type (T) then
14366 return UI_To_Int (Esize (R_Typ));
14367
14368 -- Discrete types
14369
14370 elsif Is_Discrete_Type (T) then
14371
14372 -- The following loop is looking for the nearest compile time known
14373 -- bounds following the ancestor subtype chain. The idea is to find
14374 -- the most restrictive known bounds information.
14375
14376 Ancest := T;
14377 loop
14378 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
14379 return Unknown_Minimum_Size;
14380 end if;
14381
14382 if not LoSet then
14383 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
14384 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
14385 LoSet := True;
14386 exit when HiSet;
14387 end if;
14388 end if;
14389
14390 if not HiSet then
14391 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
14392 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
14393 HiSet := True;
14394 exit when LoSet;
14395 end if;
14396 end if;
14397
14398 Ancest := Ancestor_Subtype (Ancest);
14399
14400 if No (Ancest) then
14401 Ancest := Base_Type (T);
14402
14403 if Is_Generic_Type (Ancest) then
14404 return Unknown_Minimum_Size;
14405 end if;
14406 end if;
14407 end loop;
14408
14409 -- Fixed-point types. We can't simply use Expr_Value to get the
14410 -- Corresponding_Integer_Value values of the bounds, since these do not
14411 -- get set till the type is frozen, and this routine can be called
14412 -- before the type is frozen. Similarly the test for bounds being static
14413 -- needs to include the case where we have unanalyzed real literals for
14414 -- the same reason.
14415
14416 elsif Is_Fixed_Point_Type (T) then
14417
14418 -- The following loop is looking for the nearest compile time known
14419 -- bounds following the ancestor subtype chain. The idea is to find
14420 -- the most restrictive known bounds information.
14421
14422 Ancest := T;
14423 loop
14424 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
14425 return Unknown_Minimum_Size;
14426 end if;
14427
14428 -- Note: In the following two tests for LoSet and HiSet, it may
14429 -- seem redundant to test for N_Real_Literal here since normally
14430 -- one would assume that the test for the value being known at
14431 -- compile time includes this case. However, there is a glitch.
14432 -- If the real literal comes from folding a non-static expression,
14433 -- then we don't consider any non- static expression to be known
14434 -- at compile time if we are in configurable run time mode (needed
14435 -- in some cases to give a clearer definition of what is and what
14436 -- is not accepted). So the test is indeed needed. Without it, we
14437 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
14438
14439 if not LoSet then
14440 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
14441 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
14442 then
14443 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
14444 LoSet := True;
14445 exit when HiSet;
14446 end if;
14447 end if;
14448
14449 if not HiSet then
14450 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
14451 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
14452 then
14453 HiR := Expr_Value_R (Type_High_Bound (Ancest));
14454 HiSet := True;
14455 exit when LoSet;
14456 end if;
14457 end if;
14458
14459 Ancest := Ancestor_Subtype (Ancest);
14460
14461 if No (Ancest) then
14462 Ancest := Base_Type (T);
14463
14464 if Is_Generic_Type (Ancest) then
14465 return Unknown_Minimum_Size;
14466 end if;
14467 end if;
14468 end loop;
14469
14470 Lo := UR_To_Uint (LoR / Small_Value (T));
14471 Hi := UR_To_Uint (HiR / Small_Value (T));
14472
14473 -- No other types allowed
14474
14475 else
14476 raise Program_Error;
14477 end if;
14478
14479 -- Fall through with Hi and Lo set. Deal with biased case
14480
14481 if (Biased
14482 and then not Is_Fixed_Point_Type (T)
14483 and then not (Is_Enumeration_Type (T)
14484 and then Has_Non_Standard_Rep (T)))
14485 or else Has_Biased_Representation (T)
14486 then
14487 Hi := Hi - Lo;
14488 Lo := Uint_0;
14489 end if;
14490
14491 -- Null range case, size is always zero. We only do this in the discrete
14492 -- type case, since that's the odd case that came up. Probably we should
14493 -- also do this in the fixed-point case, but doing so causes peculiar
14494 -- gigi failures, and it is not worth worrying about this incredibly
14495 -- marginal case (explicit null-range fixed-point type declarations).
14496
14497 if Lo > Hi and then Is_Discrete_Type (T) then
14498 S := 0;
14499
14500 -- Signed case. Note that we consider types like range 1 .. -1 to be
14501 -- signed for the purpose of computing the size, since the bounds have
14502 -- to be accommodated in the base type.
14503
14504 elsif Lo < 0 or else Hi < 0 then
14505 S := 1;
14506 B := Uint_1;
14507
14508 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
14509 -- Note that we accommodate the case where the bounds cross. This
14510 -- can happen either because of the way the bounds are declared
14511 -- or because of the algorithm in Freeze_Fixed_Point_Type.
14512
14513 while Lo < -B
14514 or else Hi < -B
14515 or else Lo >= B
14516 or else Hi >= B
14517 loop
14518 B := Uint_2 ** S;
14519 S := S + 1;
14520 end loop;
14521
14522 -- Unsigned case
14523
14524 else
14525 -- If both bounds are positive, make sure that both are represen-
14526 -- table in the case where the bounds are crossed. This can happen
14527 -- either because of the way the bounds are declared, or because of
14528 -- the algorithm in Freeze_Fixed_Point_Type.
14529
14530 if Lo > Hi then
14531 Hi := Lo;
14532 end if;
14533
14534 -- S = size, (can accommodate 0 .. (2**size - 1))
14535
14536 S := 0;
14537 while Hi >= Uint_2 ** S loop
14538 S := S + 1;
14539 end loop;
14540 end if;
14541
14542 return S;
14543 end Minimum_Size;
14544
14545 ------------------------------
14546 -- New_Put_Image_Subprogram --
14547 ------------------------------
14548
14549 procedure New_Put_Image_Subprogram
14550 (N : Node_Id;
14551 Ent : Entity_Id;
14552 Subp : Entity_Id)
14553 is
14554 Loc : constant Source_Ptr := Sloc (N);
14555 Sname : constant Name_Id :=
14556 Make_TSS_Name (Base_Type (Ent), TSS_Put_Image);
14557 Subp_Id : Entity_Id;
14558 Subp_Decl : Node_Id;
14559 F : Entity_Id;
14560 Etyp : Entity_Id;
14561
14562 Defer_Declaration : constant Boolean :=
14563 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
14564 -- For a tagged type, there is a declaration at the freeze point, and
14565 -- we must generate only a completion of this declaration. We do the
14566 -- same for private types, because the full view might be tagged.
14567 -- Otherwise we generate a declaration at the point of the attribute
14568 -- definition clause. If the attribute definition comes from an aspect
14569 -- specification the declaration is part of the freeze actions of the
14570 -- type.
14571
14572 function Build_Spec return Node_Id;
14573 -- Used for declaration and renaming declaration, so that this is
14574 -- treated as a renaming_as_body.
14575
14576 ----------------
14577 -- Build_Spec --
14578 ----------------
14579
14580 function Build_Spec return Node_Id is
14581 Formals : List_Id;
14582 Spec : Node_Id;
14583 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
14584
14585 begin
14586 Subp_Id := Make_Defining_Identifier (Loc, Sname);
14587
14588 -- S : Root_Buffer_Type'Class
14589
14590 Formals := New_List (
14591 Make_Parameter_Specification (Loc,
14592 Defining_Identifier =>
14593 Make_Defining_Identifier (Loc, Name_S),
14594 In_Present => True,
14595 Out_Present => True,
14596 Parameter_Type =>
14597 New_Occurrence_Of (Etype (F), Loc)));
14598
14599 -- V : T
14600
14601 Append_To (Formals,
14602 Make_Parameter_Specification (Loc,
14603 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
14604 Parameter_Type => T_Ref));
14605
14606 Spec :=
14607 Make_Procedure_Specification (Loc,
14608 Defining_Unit_Name => Subp_Id,
14609 Parameter_Specifications => Formals);
14610
14611 return Spec;
14612 end Build_Spec;
14613
14614 -- Start of processing for New_Put_Image_Subprogram
14615
14616 begin
14617 F := First_Formal (Subp);
14618
14619 Etyp := Etype (Next_Formal (F));
14620
14621 -- Prepare subprogram declaration and insert it as an action on the
14622 -- clause node. The visibility for this entity is used to test for
14623 -- visibility of the attribute definition clause (in the sense of
14624 -- 8.3(23) as amended by AI-195).
14625
14626 if not Defer_Declaration then
14627 Subp_Decl :=
14628 Make_Subprogram_Declaration (Loc,
14629 Specification => Build_Spec);
14630
14631 -- For a tagged type, there is always a visible declaration for the
14632 -- Put_Image TSS (it is a predefined primitive operation), and the
14633 -- completion of this declaration occurs at the freeze point, which is
14634 -- not always visible at places where the attribute definition clause is
14635 -- visible. So, we create a dummy entity here for the purpose of
14636 -- tracking the visibility of the attribute definition clause itself.
14637
14638 else
14639 Subp_Id :=
14640 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
14641 Subp_Decl :=
14642 Make_Object_Declaration (Loc,
14643 Defining_Identifier => Subp_Id,
14644 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
14645 end if;
14646
14647 if not Defer_Declaration
14648 and then From_Aspect_Specification (N)
14649 and then Has_Delayed_Freeze (Ent)
14650 then
14651 Append_Freeze_Action (Ent, Subp_Decl);
14652
14653 else
14654 Insert_Action (N, Subp_Decl);
14655 Set_Entity (N, Subp_Id);
14656 end if;
14657
14658 Subp_Decl :=
14659 Make_Subprogram_Renaming_Declaration (Loc,
14660 Specification => Build_Spec,
14661 Name => New_Occurrence_Of (Subp, Loc));
14662
14663 if Defer_Declaration then
14664 Set_TSS (Base_Type (Ent), Subp_Id);
14665
14666 else
14667 if From_Aspect_Specification (N) then
14668 Append_Freeze_Action (Ent, Subp_Decl);
14669 else
14670 Insert_Action (N, Subp_Decl);
14671 end if;
14672
14673 Copy_TSS (Subp_Id, Base_Type (Ent));
14674 end if;
14675 end New_Put_Image_Subprogram;
14676
14677 ---------------------------
14678 -- New_Stream_Subprogram --
14679 ---------------------------
14680
14681 procedure New_Stream_Subprogram
14682 (N : Node_Id;
14683 Ent : Entity_Id;
14684 Subp : Entity_Id;
14685 Nam : TSS_Name_Type)
14686 is
14687 Loc : constant Source_Ptr := Sloc (N);
14688 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
14689 Subp_Id : Entity_Id;
14690 Subp_Decl : Node_Id;
14691 F : Entity_Id;
14692 Etyp : Entity_Id;
14693
14694 Defer_Declaration : constant Boolean :=
14695 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
14696 -- For a tagged type, there is a declaration for each stream attribute
14697 -- at the freeze point, and we must generate only a completion of this
14698 -- declaration. We do the same for private types, because the full view
14699 -- might be tagged. Otherwise we generate a declaration at the point of
14700 -- the attribute definition clause. If the attribute definition comes
14701 -- from an aspect specification the declaration is part of the freeze
14702 -- actions of the type.
14703
14704 function Build_Spec return Node_Id;
14705 -- Used for declaration and renaming declaration, so that this is
14706 -- treated as a renaming_as_body.
14707
14708 ----------------
14709 -- Build_Spec --
14710 ----------------
14711
14712 function Build_Spec return Node_Id is
14713 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
14714 Formals : List_Id;
14715 Spec : Node_Id;
14716 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
14717
14718 begin
14719 Subp_Id := Make_Defining_Identifier (Loc, Sname);
14720
14721 -- S : access Root_Stream_Type'Class
14722
14723 Formals := New_List (
14724 Make_Parameter_Specification (Loc,
14725 Defining_Identifier =>
14726 Make_Defining_Identifier (Loc, Name_S),
14727 Parameter_Type =>
14728 Make_Access_Definition (Loc,
14729 Subtype_Mark =>
14730 New_Occurrence_Of (
14731 Designated_Type (Etype (F)), Loc))));
14732
14733 if Nam = TSS_Stream_Input then
14734 Spec :=
14735 Make_Function_Specification (Loc,
14736 Defining_Unit_Name => Subp_Id,
14737 Parameter_Specifications => Formals,
14738 Result_Definition => T_Ref);
14739 else
14740 -- V : [out] T
14741
14742 Append_To (Formals,
14743 Make_Parameter_Specification (Loc,
14744 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
14745 Out_Present => Out_P,
14746 Parameter_Type => T_Ref));
14747
14748 Spec :=
14749 Make_Procedure_Specification (Loc,
14750 Defining_Unit_Name => Subp_Id,
14751 Parameter_Specifications => Formals);
14752 end if;
14753
14754 return Spec;
14755 end Build_Spec;
14756
14757 -- Start of processing for New_Stream_Subprogram
14758
14759 begin
14760 F := First_Formal (Subp);
14761
14762 if Ekind (Subp) = E_Procedure then
14763 Etyp := Etype (Next_Formal (F));
14764 else
14765 Etyp := Etype (Subp);
14766 end if;
14767
14768 -- Prepare subprogram declaration and insert it as an action on the
14769 -- clause node. The visibility for this entity is used to test for
14770 -- visibility of the attribute definition clause (in the sense of
14771 -- 8.3(23) as amended by AI-195).
14772
14773 if not Defer_Declaration then
14774 Subp_Decl :=
14775 Make_Subprogram_Declaration (Loc,
14776 Specification => Build_Spec);
14777
14778 -- For a tagged type, there is always a visible declaration for each
14779 -- stream TSS (it is a predefined primitive operation), and the
14780 -- completion of this declaration occurs at the freeze point, which is
14781 -- not always visible at places where the attribute definition clause is
14782 -- visible. So, we create a dummy entity here for the purpose of
14783 -- tracking the visibility of the attribute definition clause itself.
14784
14785 else
14786 Subp_Id :=
14787 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
14788 Subp_Decl :=
14789 Make_Object_Declaration (Loc,
14790 Defining_Identifier => Subp_Id,
14791 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
14792 end if;
14793
14794 if not Defer_Declaration
14795 and then From_Aspect_Specification (N)
14796 and then Has_Delayed_Freeze (Ent)
14797 then
14798 Append_Freeze_Action (Ent, Subp_Decl);
14799
14800 else
14801 Insert_Action (N, Subp_Decl);
14802 Set_Entity (N, Subp_Id);
14803 end if;
14804
14805 Subp_Decl :=
14806 Make_Subprogram_Renaming_Declaration (Loc,
14807 Specification => Build_Spec,
14808 Name => New_Occurrence_Of (Subp, Loc));
14809
14810 if Defer_Declaration then
14811 Set_TSS (Base_Type (Ent), Subp_Id);
14812
14813 else
14814 if From_Aspect_Specification (N) then
14815 Append_Freeze_Action (Ent, Subp_Decl);
14816 else
14817 Insert_Action (N, Subp_Decl);
14818 end if;
14819
14820 Copy_TSS (Subp_Id, Base_Type (Ent));
14821 end if;
14822 end New_Stream_Subprogram;
14823
14824 ----------------------
14825 -- No_Type_Rep_Item --
14826 ----------------------
14827
14828 procedure No_Type_Rep_Item (N : Node_Id) is
14829 begin
14830 Error_Msg_N ("|type-related representation item not permitted!", N);
14831 end No_Type_Rep_Item;
14832
14833 --------------
14834 -- Pop_Type --
14835 --------------
14836
14837 procedure Pop_Type (E : Entity_Id) is
14838 begin
14839 if Ekind (E) = E_Record_Type and then E = Current_Scope then
14840 End_Scope;
14841
14842 elsif Is_Type (E)
14843 and then Has_Discriminants (E)
14844 and then Nkind (Parent (E)) /= N_Subtype_Declaration
14845 then
14846 Uninstall_Discriminants (E);
14847 Pop_Scope;
14848 end if;
14849 end Pop_Type;
14850
14851 ---------------
14852 -- Push_Type --
14853 ---------------
14854
14855 procedure Push_Type (E : Entity_Id) is
14856 Comp : Entity_Id;
14857
14858 begin
14859 if Ekind (E) = E_Record_Type then
14860 Push_Scope (E);
14861
14862 Comp := First_Component (E);
14863 while Present (Comp) loop
14864 Install_Entity (Comp);
14865 Next_Component (Comp);
14866 end loop;
14867
14868 if Has_Discriminants (E) then
14869 Install_Discriminants (E);
14870 end if;
14871
14872 elsif Is_Type (E)
14873 and then Has_Discriminants (E)
14874 and then Nkind (Parent (E)) /= N_Subtype_Declaration
14875 then
14876 Push_Scope (E);
14877 Install_Discriminants (E);
14878 end if;
14879 end Push_Type;
14880
14881 -----------------------------------
14882 -- Register_Address_Clause_Check --
14883 -----------------------------------
14884
14885 procedure Register_Address_Clause_Check
14886 (N : Node_Id;
14887 X : Entity_Id;
14888 A : Uint;
14889 Y : Entity_Id;
14890 Off : Boolean)
14891 is
14892 ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
14893 begin
14894 Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
14895 end Register_Address_Clause_Check;
14896
14897 ------------------------
14898 -- Rep_Item_Too_Early --
14899 ------------------------
14900
14901 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
14902 function Has_Generic_Parent (E : Entity_Id) return Boolean;
14903 -- Return True if R or any ancestor is a generic type
14904
14905 ------------------------
14906 -- Has_Generic_Parent --
14907 ------------------------
14908
14909 function Has_Generic_Parent (E : Entity_Id) return Boolean is
14910 Ancestor_Type : Entity_Id := Etype (E);
14911
14912 begin
14913 if Is_Generic_Type (E) then
14914 return True;
14915 end if;
14916
14917 while Present (Ancestor_Type)
14918 and then not Is_Generic_Type (Ancestor_Type)
14919 and then Etype (Ancestor_Type) /= Ancestor_Type
14920 loop
14921 Ancestor_Type := Etype (Ancestor_Type);
14922 end loop;
14923
14924 return
14925 Present (Ancestor_Type) and then Is_Generic_Type (Ancestor_Type);
14926 end Has_Generic_Parent;
14927
14928 -- Start of processing for Rep_Item_Too_Early
14929
14930 begin
14931 -- Cannot apply non-operational rep items to generic types
14932
14933 if Is_Operational_Item (N) then
14934 return False;
14935
14936 elsif Is_Type (T)
14937 and then Has_Generic_Parent (T)
14938 and then (Nkind (N) /= N_Pragma
14939 or else Get_Pragma_Id (N) /= Pragma_Convention)
14940 then
14941 if Ada_Version < Ada_2022 then
14942 Error_Msg_N
14943 ("representation item not allowed for generic type", N);
14944 return True;
14945 else
14946 return False;
14947 end if;
14948 end if;
14949
14950 -- Otherwise check for incomplete type
14951
14952 if Is_Incomplete_Or_Private_Type (T)
14953 and then No (Underlying_Type (T))
14954 and then
14955 (Nkind (N) /= N_Pragma
14956 or else Get_Pragma_Id (N) /= Pragma_Import)
14957 then
14958 Error_Msg_N
14959 ("representation item must be after full type declaration", N);
14960 return True;
14961
14962 -- If the type has incomplete components, a representation clause is
14963 -- illegal but stream attributes and Convention pragmas are correct.
14964
14965 elsif Has_Private_Component (T) then
14966 if Nkind (N) = N_Pragma then
14967 return False;
14968
14969 else
14970 Error_Msg_N
14971 ("representation item must appear after type is fully defined",
14972 N);
14973 return True;
14974 end if;
14975 else
14976 return False;
14977 end if;
14978 end Rep_Item_Too_Early;
14979
14980 -----------------------
14981 -- Rep_Item_Too_Late --
14982 -----------------------
14983
14984 function Rep_Item_Too_Late
14985 (T : Entity_Id;
14986 N : Node_Id;
14987 FOnly : Boolean := False) return Boolean
14988 is
14989 procedure Too_Late;
14990 -- Output message for an aspect being specified too late
14991
14992 -- Note that neither of the above errors is considered a serious one,
14993 -- since the effect is simply that we ignore the representation clause
14994 -- in these cases.
14995 -- Is this really true? In any case if we make this change we must
14996 -- document the requirement in the spec of Rep_Item_Too_Late that
14997 -- if True is returned, then the rep item must be completely ignored???
14998
14999 --------------
15000 -- Too_Late --
15001 --------------
15002
15003 procedure Too_Late is
15004 begin
15005 -- Other compilers seem more relaxed about rep items appearing too
15006 -- late. Since analysis tools typically don't care about rep items
15007 -- anyway, no reason to be too strict about this.
15008
15009 if not Relaxed_RM_Semantics then
15010 Error_Msg_N ("|representation item appears too late!", N);
15011 end if;
15012 end Too_Late;
15013
15014 -- Local variables
15015
15016 Parent_Type : Entity_Id;
15017 S : Entity_Id;
15018
15019 -- Start of processing for Rep_Item_Too_Late
15020
15021 begin
15022 -- First make sure entity is not frozen (RM 13.1(9))
15023
15024 if Is_Frozen (T)
15025
15026 -- Exclude imported types, which may be frozen if they appear in a
15027 -- representation clause for a local type.
15028
15029 and then not From_Limited_With (T)
15030
15031 -- Exclude generated entities (not coming from source). The common
15032 -- case is when we generate a renaming which prematurely freezes the
15033 -- renamed internal entity, but we still want to be able to set copies
15034 -- of attribute values such as Size/Alignment.
15035
15036 and then Comes_From_Source (T)
15037 then
15038 -- A self-referential aspect is illegal if it forces freezing the
15039 -- entity before the corresponding pragma has been analyzed.
15040
15041 if Nkind (N) in N_Attribute_Definition_Clause | N_Pragma
15042 and then From_Aspect_Specification (N)
15043 then
15044 Error_Msg_NE
15045 ("aspect specification causes premature freezing of&", N, T);
15046 Set_Has_Delayed_Freeze (T, False);
15047 return True;
15048 end if;
15049
15050 Too_Late;
15051 S := First_Subtype (T);
15052
15053 if Present (Freeze_Node (S)) then
15054 if not Relaxed_RM_Semantics then
15055 Error_Msg_NE
15056 ("??no more representation items for }", Freeze_Node (S), S);
15057 end if;
15058 end if;
15059
15060 return True;
15061
15062 -- Check for case of untagged derived type whose parent either has
15063 -- primitive operations (pre Ada 2022), or is a by-reference type (RM
15064 -- 13.1(10)). In this case we do not output a Too_Late message, since
15065 -- there is no earlier point where the rep item could be placed to make
15066 -- it legal.
15067 -- ??? Confirming representation clauses should be allowed here.
15068
15069 elsif Is_Type (T)
15070 and then not FOnly
15071 and then Is_Derived_Type (T)
15072 and then not Is_Tagged_Type (T)
15073 then
15074 Parent_Type := Etype (Base_Type (T));
15075
15076 if Relaxed_RM_Semantics then
15077 null;
15078
15079 elsif Ada_Version <= Ada_2012
15080 and then Has_Primitive_Operations (Parent_Type)
15081 then
15082 Error_Msg_N
15083 ("|representation item not permitted before Ada 2022!", N);
15084 Error_Msg_NE
15085 ("\parent type & has primitive operations!", N, Parent_Type);
15086 return True;
15087
15088 elsif Is_By_Reference_Type (Parent_Type) then
15089 No_Type_Rep_Item (N);
15090 Error_Msg_NE
15091 ("\parent type & is a by-reference type!", N, Parent_Type);
15092 return True;
15093 end if;
15094 end if;
15095
15096 -- No error, but one more warning to consider. The RM (surprisingly)
15097 -- allows this pattern in some cases:
15098
15099 -- type S is ...
15100 -- primitive operations for S
15101 -- type R is new S;
15102 -- rep clause for S
15103
15104 -- Meaning that calls on the primitive operations of S for values of
15105 -- type R may require possibly expensive implicit conversion operations.
15106 -- So even when this is not an error, it is still worth a warning.
15107
15108 if not Relaxed_RM_Semantics and then Is_Type (T) then
15109 declare
15110 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
15111
15112 begin
15113 if Present (DTL)
15114
15115 -- For now, do not generate this warning for the case of
15116 -- aspect specification using Ada 2012 syntax, since we get
15117 -- wrong messages we do not understand. The whole business
15118 -- of derived types and rep items seems a bit confused when
15119 -- aspects are used, since the aspects are not evaluated
15120 -- till freeze time. However, AI12-0109 confirms (in an AARM
15121 -- ramification) that inheritance in this case is required
15122 -- to work.
15123
15124 and then not From_Aspect_Specification (N)
15125 then
15126 if Is_By_Reference_Type (T)
15127 and then not Is_Tagged_Type (T)
15128 and then Is_Type_Related_Rep_Item (N)
15129 and then (Ada_Version >= Ada_2012
15130 or else Has_Primitive_Operations (Base_Type (T)))
15131 then
15132 -- Treat as hard error (AI12-0109, binding interpretation).
15133 -- Implementing a change of representation is not really
15134 -- an option in the case of a by-reference type, so we
15135 -- take this path for all Ada dialects if primitive
15136 -- operations are present.
15137 Error_Msg_Sloc := Sloc (DTL);
15138 Error_Msg_N
15139 ("representation item for& appears after derived type "
15140 & "declaration#", N);
15141
15142 elsif Has_Primitive_Operations (Base_Type (T)) then
15143 Error_Msg_Sloc := Sloc (DTL);
15144
15145 Error_Msg_N
15146 ("representation item for& appears after derived type "
15147 & "declaration#??", N);
15148 Error_Msg_NE
15149 ("\may result in implicit conversions for primitive "
15150 & "operations of&??", N, T);
15151 Error_Msg_NE
15152 ("\to change representations when called with arguments "
15153 & "of type&??", N, DTL);
15154 end if;
15155 end if;
15156 end;
15157 end if;
15158
15159 -- No error, link item into head of chain of rep items for the entity,
15160 -- but avoid chaining if we have an overloadable entity, and the pragma
15161 -- is one that can apply to multiple overloaded entities.
15162
15163 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
15164 declare
15165 Pname : constant Name_Id := Pragma_Name (N);
15166 begin
15167 if Pname in Name_Convention | Name_Import | Name_Export
15168 | Name_External | Name_Interface
15169 then
15170 return False;
15171 end if;
15172 end;
15173 end if;
15174
15175 Record_Rep_Item (T, N);
15176 return False;
15177 end Rep_Item_Too_Late;
15178
15179 -------------------------------------
15180 -- Replace_Type_References_Generic --
15181 -------------------------------------
15182
15183 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
15184 TName : constant Name_Id := Chars (T);
15185
15186 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
15187 -- Processes a single node in the traversal procedure below, checking
15188 -- if node N should be replaced, and if so, doing the replacement.
15189
15190 function Visible_Component (Comp : Name_Id) return Entity_Id;
15191 -- Given an identifier in the expression, check whether there is a
15192 -- discriminant, component, protected procedure, or entry of the type
15193 -- that is directy visible, and rewrite it as the corresponding selected
15194 -- component of the formal of the subprogram.
15195
15196 ----------------------
15197 -- Replace_Type_Ref --
15198 ----------------------
15199
15200 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
15201 Loc : constant Source_Ptr := Sloc (N);
15202
15203 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
15204 -- Add the proper prefix to a reference to a component of the type
15205 -- when it is not already a selected component.
15206
15207 ----------------
15208 -- Add_Prefix --
15209 ----------------
15210
15211 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
15212 begin
15213 Rewrite (Ref,
15214 Make_Selected_Component (Loc,
15215 Prefix => New_Occurrence_Of (T, Loc),
15216 Selector_Name => New_Occurrence_Of (Comp, Loc)));
15217 Replace_Type_Reference (Prefix (Ref));
15218 end Add_Prefix;
15219
15220 -- Local variables
15221
15222 Comp : Entity_Id;
15223 Pref : Node_Id;
15224 Scop : Entity_Id;
15225
15226 -- Start of processing for Replace_Type_Ref
15227
15228 begin
15229 if Nkind (N) = N_Identifier then
15230
15231 -- If not the type name, check whether it is a reference to some
15232 -- other type, which must be frozen before the predicate function
15233 -- is analyzed, i.e. before the freeze node of the type to which
15234 -- the predicate applies.
15235
15236 if Chars (N) /= TName then
15237 if Present (Current_Entity (N))
15238 and then Is_Type (Current_Entity (N))
15239 then
15240 Freeze_Before (Freeze_Node (T), Current_Entity (N));
15241 end if;
15242
15243 -- The components of the type are directly visible and can
15244 -- be referenced in the source code without a prefix.
15245 -- If a name denoting a component doesn't already have a
15246 -- prefix, then normalize it by adding a reference to the
15247 -- current instance of the type as a prefix.
15248 --
15249 -- This isn't right in the pathological corner case of an
15250 -- object-declaring expression (e.g., a quantified expression
15251 -- or a declare expression) that declares an object with the
15252 -- same name as a visible component declaration, thereby hiding
15253 -- the component within that expression. For example, given a
15254 -- record with a Boolean component "C" and a dynamic predicate
15255 -- "C = (for some C in Character => Some_Function (C))", only
15256 -- the first of the two uses of C should have a prefix added
15257 -- here; instead, both will get prefixes.
15258
15259 if Nkind (Parent (N)) /= N_Selected_Component
15260 or else N /= Selector_Name (Parent (N))
15261 then
15262 Comp := Visible_Component (Chars (N));
15263
15264 if Present (Comp) then
15265 Add_Prefix (N, Comp);
15266 end if;
15267 end if;
15268
15269 return Skip;
15270
15271 -- Otherwise do the replacement if this is not a qualified
15272 -- reference to a homograph of the type itself. Note that the
15273 -- current instance could not appear in such a context, e.g.
15274 -- the prefix of a type conversion.
15275
15276 else
15277 if Nkind (Parent (N)) /= N_Selected_Component
15278 or else N /= Selector_Name (Parent (N))
15279 then
15280 Replace_Type_Reference (N);
15281 end if;
15282
15283 return Skip;
15284 end if;
15285
15286 -- Case of selected component, which may be a subcomponent of the
15287 -- current instance, or an expanded name which is still unanalyzed.
15288
15289 elsif Nkind (N) = N_Selected_Component then
15290
15291 -- If selector name is not our type, keep going (we might still
15292 -- have an occurrence of the type in the prefix). If it is a
15293 -- subcomponent of the current entity, add prefix.
15294
15295 if Nkind (Selector_Name (N)) /= N_Identifier
15296 or else Chars (Selector_Name (N)) /= TName
15297 then
15298 if Nkind (Prefix (N)) = N_Identifier then
15299 Comp := Visible_Component (Chars (Prefix (N)));
15300
15301 if Present (Comp) then
15302 Add_Prefix (Prefix (N), Comp);
15303 end if;
15304 end if;
15305
15306 return OK;
15307
15308 -- Selector name is our type, check qualification
15309
15310 else
15311 -- Loop through scopes and prefixes, doing comparison
15312
15313 Scop := Current_Scope;
15314 Pref := Prefix (N);
15315 loop
15316 -- Continue if no more scopes or scope with no name
15317
15318 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
15319 return OK;
15320 end if;
15321
15322 -- Do replace if prefix is an identifier matching the scope
15323 -- that we are currently looking at.
15324
15325 if Nkind (Pref) = N_Identifier
15326 and then Chars (Pref) = Chars (Scop)
15327 then
15328 Replace_Type_Reference (N);
15329 return Skip;
15330 end if;
15331
15332 -- Go check scope above us if prefix is itself of the form
15333 -- of a selected component, whose selector matches the scope
15334 -- we are currently looking at.
15335
15336 if Nkind (Pref) = N_Selected_Component
15337 and then Nkind (Selector_Name (Pref)) = N_Identifier
15338 and then Chars (Selector_Name (Pref)) = Chars (Scop)
15339 then
15340 Scop := Scope (Scop);
15341 Pref := Prefix (Pref);
15342
15343 -- For anything else, we don't have a match, so keep on
15344 -- going, there are still some weird cases where we may
15345 -- still have a replacement within the prefix.
15346
15347 else
15348 return OK;
15349 end if;
15350 end loop;
15351 end if;
15352
15353 -- Continue for any other node kind
15354
15355 else
15356 return OK;
15357 end if;
15358 end Replace_Type_Ref;
15359
15360 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
15361
15362 -----------------------
15363 -- Visible_Component --
15364 -----------------------
15365
15366 function Visible_Component (Comp : Name_Id) return Entity_Id is
15367 E : Entity_Id;
15368 begin
15369 -- Types with nameable components are record, task, and protected
15370 -- types, and discriminated private types.
15371
15372 if Ekind (T) in E_Record_Type
15373 | E_Task_Type
15374 | E_Protected_Type
15375 or else (Is_Private_Type (T) and then Has_Discriminants (T))
15376 then
15377 -- This is a sequential search, which seems acceptable
15378 -- efficiency-wise, given the typical size of component
15379 -- lists, protected operation lists, task item lists, and
15380 -- check expressions.
15381
15382 E := First_Entity (T);
15383 while Present (E) loop
15384 if Comes_From_Source (E) and then Chars (E) = Comp then
15385 return E;
15386 end if;
15387
15388 Next_Entity (E);
15389 end loop;
15390 end if;
15391
15392 -- Nothing by that name
15393
15394 return Empty;
15395 end Visible_Component;
15396
15397 -- Start of processing for Replace_Type_References_Generic
15398
15399 begin
15400 Replace_Type_Refs (N);
15401 end Replace_Type_References_Generic;
15402
15403 --------------------------------
15404 -- Resolve_Aspect_Expressions --
15405 --------------------------------
15406
15407 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
15408 function Resolve_Name (N : Node_Id) return Traverse_Result;
15409 -- Verify that all identifiers in the expression, with the exception
15410 -- of references to the current entity, denote visible entities. This
15411 -- is done only to detect visibility errors, as the expression will be
15412 -- properly analyzed/expanded during analysis of the predicate function
15413 -- body. We omit quantified expressions from this test, given that they
15414 -- introduce a local identifier that would require proper expansion to
15415 -- handle properly.
15416
15417 ------------------
15418 -- Resolve_Name --
15419 ------------------
15420
15421 function Resolve_Name (N : Node_Id) return Traverse_Result is
15422 Dummy : Traverse_Result;
15423
15424 begin
15425 if Nkind (N) = N_Selected_Component then
15426 if Nkind (Prefix (N)) = N_Identifier
15427 and then Chars (Prefix (N)) /= Chars (E)
15428 then
15429 Find_Selected_Component (N);
15430 end if;
15431
15432 return Skip;
15433
15434 -- Resolve identifiers that are not selectors in parameter
15435 -- associations (these are never resolved by visibility).
15436
15437 elsif Nkind (N) = N_Identifier
15438 and then Chars (N) /= Chars (E)
15439 and then (Nkind (Parent (N)) /= N_Parameter_Association
15440 or else N /= Selector_Name (Parent (N)))
15441 then
15442 Find_Direct_Name (N);
15443
15444 -- Reset the Entity if N is overloaded since the entity may not
15445 -- be the correct one.
15446
15447 if Is_Overloaded (N) then
15448 Set_Entity (N, Empty);
15449 end if;
15450
15451 -- The name in a component association needs no resolution
15452
15453 elsif Nkind (N) = N_Component_Association then
15454 Dummy := Resolve_Name (Expression (N));
15455 return Skip;
15456
15457 elsif Nkind (N) = N_Quantified_Expression then
15458 return Skip;
15459 end if;
15460
15461 return OK;
15462 end Resolve_Name;
15463
15464 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
15465
15466 -- Local variables
15467
15468 ASN : Node_Id := First_Rep_Item (E);
15469
15470 -- Start of processing for Resolve_Aspect_Expressions
15471
15472 begin
15473 while Present (ASN) loop
15474 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
15475 declare
15476 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
15477 Expr : constant Node_Id := Expression (ASN);
15478
15479 begin
15480 case A_Id is
15481
15482 when Aspect_Aggregate =>
15483 Resolve_Aspect_Aggregate (Entity (ASN), Expr);
15484
15485 when Aspect_Stable_Properties =>
15486 Resolve_Aspect_Stable_Properties
15487 (Entity (ASN), Expr, Class_Present (ASN));
15488
15489 -- For now we only deal with aspects that do not generate
15490 -- subprograms, or that may mention current instances of
15491 -- types. These will require special handling???.
15492
15493 when Aspect_Invariant
15494 | Aspect_Predicate_Failure
15495 =>
15496 null;
15497
15498 when Aspect_Dynamic_Predicate
15499 | Aspect_Static_Predicate
15500 | Aspect_Predicate
15501 =>
15502 -- Preanalyze expression after type replacement to catch
15503 -- name resolution errors if the predicate function has
15504 -- not been built yet.
15505 -- Note that we cannot use Preanalyze_Spec_Expression
15506 -- because of the special handling required for
15507 -- quantifiers, see comments on Resolve_Aspect_Expression
15508 -- above.
15509
15510 if No (Predicate_Function (E)) then
15511 Push_Type (E);
15512 Resolve_Aspect_Expression (Expr);
15513 Pop_Type (E);
15514 end if;
15515
15516 when Pre_Post_Aspects =>
15517 null;
15518
15519 when Aspect_Iterable =>
15520 if Nkind (Expr) = N_Aggregate then
15521 declare
15522 Assoc : Node_Id;
15523
15524 begin
15525 Assoc := First (Component_Associations (Expr));
15526 while Present (Assoc) loop
15527 if Nkind (Expression (Assoc)) in N_Has_Entity
15528 then
15529 Find_Direct_Name (Expression (Assoc));
15530 end if;
15531
15532 Next (Assoc);
15533 end loop;
15534 end;
15535 end if;
15536
15537 -- The expression for Default_Value is a static expression
15538 -- of the type, but this expression does not freeze the
15539 -- type, so it can still appear in a representation clause
15540 -- before the actual freeze point.
15541
15542 when Aspect_Default_Value =>
15543 Set_Must_Not_Freeze (Expr);
15544 Preanalyze_Spec_Expression (Expr, E);
15545
15546 when Aspect_Priority =>
15547 Push_Type (E);
15548 Preanalyze_Spec_Expression (Expr, Any_Integer);
15549 Pop_Type (E);
15550
15551 -- Ditto for Storage_Size. Any other aspects that carry
15552 -- expressions that should not freeze ??? This is only
15553 -- relevant to the misuse of deferred constants.
15554
15555 when Aspect_Storage_Size =>
15556 Set_Must_Not_Freeze (Expr);
15557 Preanalyze_Spec_Expression (Expr, Any_Integer);
15558
15559 when others =>
15560 if Present (Expr) then
15561 case Aspect_Argument (A_Id) is
15562 when Expression
15563 | Optional_Expression
15564 =>
15565 Analyze_And_Resolve (Expr);
15566
15567 when Name
15568 | Optional_Name
15569 =>
15570 if Nkind (Expr) = N_Identifier then
15571 Find_Direct_Name (Expr);
15572
15573 elsif Nkind (Expr) = N_Selected_Component then
15574 Find_Selected_Component (Expr);
15575 end if;
15576 end case;
15577 end if;
15578 end case;
15579 end;
15580 end if;
15581
15582 Next_Rep_Item (ASN);
15583 end loop;
15584 end Resolve_Aspect_Expressions;
15585
15586 ----------------------------
15587 -- Parse_Aspect_Aggregate --
15588 ----------------------------
15589
15590 procedure Parse_Aspect_Aggregate
15591 (N : Node_Id;
15592 Empty_Subp : in out Node_Id;
15593 Add_Named_Subp : in out Node_Id;
15594 Add_Unnamed_Subp : in out Node_Id;
15595 New_Indexed_Subp : in out Node_Id;
15596 Assign_Indexed_Subp : in out Node_Id)
15597 is
15598 Assoc : Node_Id := First (Component_Associations (N));
15599 Op_Name : Name_Id;
15600 Subp : Node_Id;
15601
15602 begin
15603 while Present (Assoc) loop
15604 Subp := Expression (Assoc);
15605 Op_Name := Chars (First (Choices (Assoc)));
15606 if Op_Name = Name_Empty then
15607 Empty_Subp := Subp;
15608
15609 elsif Op_Name = Name_Add_Named then
15610 Add_Named_Subp := Subp;
15611
15612 elsif Op_Name = Name_Add_Unnamed then
15613 Add_Unnamed_Subp := Subp;
15614
15615 elsif Op_Name = Name_New_Indexed then
15616 New_Indexed_Subp := Subp;
15617
15618 elsif Op_Name = Name_Assign_Indexed then
15619 Assign_Indexed_Subp := Subp;
15620 end if;
15621
15622 Next (Assoc);
15623 end loop;
15624 end Parse_Aspect_Aggregate;
15625
15626 ------------------------------------
15627 -- Parse_Aspect_Stable_Properties --
15628 ------------------------------------
15629
15630 function Parse_Aspect_Stable_Properties
15631 (Aspect_Spec : Node_Id; Negated : out Boolean) return Subprogram_List
15632 is
15633 function Extract_Entity (Expr : Node_Id) return Entity_Id;
15634 -- Given an element of a Stable_Properties aspect spec, return the
15635 -- associated entity.
15636 -- This function updates the Negated flag as a side-effect.
15637
15638 --------------------
15639 -- Extract_Entity --
15640 --------------------
15641
15642 function Extract_Entity (Expr : Node_Id) return Entity_Id is
15643 Name : Node_Id;
15644 begin
15645 if Nkind (Expr) = N_Op_Not then
15646 Negated := True;
15647 Name := Right_Opnd (Expr);
15648 else
15649 Name := Expr;
15650 end if;
15651
15652 if Nkind (Name) in N_Has_Entity then
15653 return Entity (Name);
15654 else
15655 return Empty;
15656 end if;
15657 end Extract_Entity;
15658
15659 -- Local variables
15660
15661 L : List_Id;
15662 Id : Node_Id;
15663
15664 -- Start of processing for Parse_Aspect_Stable_Properties
15665
15666 begin
15667 Negated := False;
15668
15669 if Nkind (Aspect_Spec) /= N_Aggregate then
15670 return (1 => Extract_Entity (Aspect_Spec));
15671 else
15672 L := Expressions (Aspect_Spec);
15673 Id := First (L);
15674
15675 return Result : Subprogram_List (1 .. List_Length (L)) do
15676 for I in Result'Range loop
15677 Result (I) := Extract_Entity (Id);
15678
15679 if No (Result (I)) then
15680 pragma Assert (Serious_Errors_Detected > 0);
15681 goto Ignore_Aspect;
15682 end if;
15683
15684 Next (Id);
15685 end loop;
15686 end return;
15687 end if;
15688
15689 <<Ignore_Aspect>> return (1 .. 0 => <>);
15690 end Parse_Aspect_Stable_Properties;
15691
15692 -------------------------------
15693 -- Validate_Aspect_Aggregate --
15694 -------------------------------
15695
15696 procedure Validate_Aspect_Aggregate (N : Node_Id) is
15697 Empty_Subp : Node_Id := Empty;
15698 Add_Named_Subp : Node_Id := Empty;
15699 Add_Unnamed_Subp : Node_Id := Empty;
15700 New_Indexed_Subp : Node_Id := Empty;
15701 Assign_Indexed_Subp : Node_Id := Empty;
15702
15703 begin
15704 Error_Msg_Ada_2022_Feature ("aspect Aggregate", Sloc (N));
15705
15706 if Nkind (N) /= N_Aggregate
15707 or else Present (Expressions (N))
15708 or else No (Component_Associations (N))
15709 then
15710 Error_Msg_N ("aspect Aggregate requires an aggregate "
15711 & "with component associations", N);
15712 return;
15713 end if;
15714
15715 Parse_Aspect_Aggregate (N,
15716 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
15717 New_Indexed_Subp, Assign_Indexed_Subp);
15718
15719 if No (Empty_Subp) then
15720 Error_Msg_N ("missing specification for Empty in aggregate", N);
15721 end if;
15722
15723 if Present (Add_Named_Subp) then
15724 if Present (Add_Unnamed_Subp)
15725 or else Present (Assign_Indexed_Subp)
15726 then
15727 Error_Msg_N
15728 ("conflicting operations for aggregate (RM 4.3.5)", N);
15729 return;
15730 end if;
15731
15732 elsif Present (New_Indexed_Subp) /= Present (Assign_Indexed_Subp) then
15733 Error_Msg_N ("incomplete specification for indexed aggregate", N);
15734 end if;
15735 end Validate_Aspect_Aggregate;
15736
15737 -------------------------------
15738 -- Validate_Aspect_Stable_Properties --
15739 -------------------------------
15740
15741 procedure Validate_Aspect_Stable_Properties
15742 (E : Entity_Id; N : Node_Id; Class_Present : Boolean)
15743 is
15744 Is_Aspect_Of_Type : constant Boolean := Is_Type (E);
15745
15746 type Permission is (Forbidden, Optional, Required);
15747 Modifier_Permission : Permission :=
15748 (if Is_Aspect_Of_Type then Forbidden else Optional);
15749 Modifier_Error_Called : Boolean := False;
15750
15751 procedure Check_Property_Function_Arg (PF_Arg : Node_Id);
15752 -- Check syntax of a property function argument
15753
15754 ----------------------------------
15755 -- Check_Property_Function_Arg --
15756 ----------------------------------
15757
15758 procedure Check_Property_Function_Arg (PF_Arg : Node_Id) is
15759 procedure Modifier_Error;
15760 -- Generate message about bad "not" modifier if no message already
15761 -- generated. Errors include specifying "not" for an aspect of
15762 -- of a type and specifying "not" for some but not all of the
15763 -- names in a list.
15764
15765 --------------------
15766 -- Modifier_Error --
15767 --------------------
15768
15769 procedure Modifier_Error is
15770 begin
15771 if Modifier_Error_Called then
15772 return; -- error message already generated
15773 end if;
15774
15775 Modifier_Error_Called := True;
15776
15777 if Is_Aspect_Of_Type then
15778 Error_Msg_N
15779 ("NOT modifier not allowed for Stable_Properties aspect"
15780 & " of a type", PF_Arg);
15781 else
15782 Error_Msg_N ("mixed use of NOT modifiers", PF_Arg);
15783 end if;
15784 end Modifier_Error;
15785
15786 PF_Name : Node_Id := PF_Arg;
15787
15788 -- Start of processing for Check_Property_Function_Arg
15789
15790 begin
15791 if Nkind (PF_Arg) = N_Op_Not then
15792 PF_Name := Right_Opnd (PF_Arg);
15793
15794 case Modifier_Permission is
15795 when Forbidden =>
15796 Modifier_Error;
15797 when Optional =>
15798 Modifier_Permission := Required;
15799 when Required =>
15800 null;
15801 end case;
15802 else
15803 case Modifier_Permission is
15804 when Forbidden =>
15805 null;
15806 when Optional =>
15807 Modifier_Permission := Forbidden;
15808 when Required =>
15809 Modifier_Error;
15810 end case;
15811 end if;
15812
15813 if Nkind (PF_Name) not in
15814 N_Identifier | N_Operator_Symbol | N_Selected_Component
15815 then
15816 Error_Msg_N ("bad property function name", PF_Name);
15817 end if;
15818 end Check_Property_Function_Arg;
15819
15820 -- Start of processing for Validate_Aspect_Stable_Properties
15821
15822 begin
15823 Error_Msg_Ada_2022_Feature ("aspect Stable_Properties", Sloc (N));
15824
15825 if (not Is_Aspect_Of_Type) and then (not Is_Subprogram (E)) then
15826 Error_Msg_N ("Stable_Properties aspect can only be specified for "
15827 & "a type or a subprogram", N);
15828 elsif Class_Present then
15829 if Is_Aspect_Of_Type then
15830 if not Is_Tagged_Type (E) then
15831 Error_Msg_N
15832 ("Stable_Properties''Class aspect cannot be specified for "
15833 & "an untagged type", N);
15834 end if;
15835 else
15836 if not Is_Dispatching_Operation (E) then
15837 Error_Msg_N
15838 ("Stable_Properties''Class aspect cannot be specified for "
15839 & "a subprogram that is not a primitive subprogram "
15840 & "of a tagged type", N);
15841 end if;
15842 end if;
15843 end if;
15844
15845 if Nkind (N) = N_Aggregate then
15846 if Present (Component_Associations (N))
15847 or else Null_Record_Present (N)
15848 or else not Present (Expressions (N))
15849 then
15850 Error_Msg_N ("bad Stable_Properties aspect specification", N);
15851 return;
15852 end if;
15853
15854 declare
15855 PF_Arg : Node_Id := First (Expressions (N));
15856 begin
15857 while Present (PF_Arg) loop
15858 Check_Property_Function_Arg (PF_Arg);
15859 Next (PF_Arg);
15860 end loop;
15861 end;
15862 else
15863 Check_Property_Function_Arg (N);
15864 end if;
15865 end Validate_Aspect_Stable_Properties;
15866
15867 --------------------------------
15868 -- Resolve_Iterable_Operation --
15869 --------------------------------
15870
15871 procedure Resolve_Iterable_Operation
15872 (N : Node_Id;
15873 Cursor : Entity_Id;
15874 Typ : Entity_Id;
15875 Nam : Name_Id)
15876 is
15877 Ent : Entity_Id;
15878 F1 : Entity_Id;
15879 F2 : Entity_Id;
15880
15881 begin
15882 if not Is_Overloaded (N) then
15883 if not Is_Entity_Name (N)
15884 or else Ekind (Entity (N)) /= E_Function
15885 or else Scope (Entity (N)) /= Scope (Typ)
15886 or else No (First_Formal (Entity (N)))
15887 or else Etype (First_Formal (Entity (N))) /= Typ
15888 then
15889 Error_Msg_N
15890 ("iterable primitive must be local function name whose first "
15891 & "formal is an iterable type", N);
15892 return;
15893 end if;
15894
15895 Ent := Entity (N);
15896 F1 := First_Formal (Ent);
15897 F2 := Next_Formal (F1);
15898
15899 if Nam = Name_First then
15900
15901 -- First (Container) => Cursor
15902
15903 if Etype (Ent) /= Cursor then
15904 Error_Msg_N ("primitive for First must yield a cursor", N);
15905 elsif Present (F2) then
15906 Error_Msg_N ("no match for First iterable primitive", N);
15907 end if;
15908
15909 elsif Nam = Name_Last then
15910
15911 -- Last (Container) => Cursor
15912
15913 if Etype (Ent) /= Cursor then
15914 Error_Msg_N ("primitive for Last must yield a cursor", N);
15915 elsif Present (F2) then
15916 Error_Msg_N ("no match for Last iterable primitive", N);
15917 end if;
15918
15919 elsif Nam = Name_Next then
15920
15921 -- Next (Container, Cursor) => Cursor
15922
15923 if No (F2)
15924 or else Etype (F2) /= Cursor
15925 or else Etype (Ent) /= Cursor
15926 or else Present (Next_Formal (F2))
15927 then
15928 Error_Msg_N ("no match for Next iterable primitive", N);
15929 end if;
15930
15931 elsif Nam = Name_Previous then
15932
15933 -- Previous (Container, Cursor) => Cursor
15934
15935 if No (F2)
15936 or else Etype (F2) /= Cursor
15937 or else Etype (Ent) /= Cursor
15938 or else Present (Next_Formal (F2))
15939 then
15940 Error_Msg_N ("no match for Previous iterable primitive", N);
15941 end if;
15942
15943 elsif Nam = Name_Has_Element then
15944
15945 -- Has_Element (Container, Cursor) => Boolean
15946
15947 if No (F2)
15948 or else Etype (F2) /= Cursor
15949 or else Etype (Ent) /= Standard_Boolean
15950 or else Present (Next_Formal (F2))
15951 then
15952 Error_Msg_N ("no match for Has_Element iterable primitive", N);
15953 end if;
15954
15955 elsif Nam = Name_Element then
15956
15957 -- Element (Container, Cursor) => Element_Type;
15958
15959 if No (F2)
15960 or else Etype (F2) /= Cursor
15961 or else Present (Next_Formal (F2))
15962 then
15963 Error_Msg_N ("no match for Element iterable primitive", N);
15964 end if;
15965
15966 else
15967 raise Program_Error;
15968 end if;
15969
15970 else
15971 -- Overloaded case: find subprogram with proper signature. Caller
15972 -- will report error if no match is found.
15973
15974 declare
15975 I : Interp_Index;
15976 It : Interp;
15977
15978 begin
15979 Get_First_Interp (N, I, It);
15980 while Present (It.Typ) loop
15981 if Ekind (It.Nam) = E_Function
15982 and then Scope (It.Nam) = Scope (Typ)
15983 and then Present (First_Formal (It.Nam))
15984 and then Etype (First_Formal (It.Nam)) = Typ
15985 then
15986 F1 := First_Formal (It.Nam);
15987
15988 if Nam = Name_First then
15989 if Etype (It.Nam) = Cursor
15990 and then No (Next_Formal (F1))
15991 then
15992 Set_Entity (N, It.Nam);
15993 exit;
15994 end if;
15995
15996 elsif Nam = Name_Next then
15997 F2 := Next_Formal (F1);
15998
15999 if Present (F2)
16000 and then No (Next_Formal (F2))
16001 and then Etype (F2) = Cursor
16002 and then Etype (It.Nam) = Cursor
16003 then
16004 Set_Entity (N, It.Nam);
16005 exit;
16006 end if;
16007
16008 elsif Nam = Name_Has_Element then
16009 F2 := Next_Formal (F1);
16010
16011 if Present (F2)
16012 and then No (Next_Formal (F2))
16013 and then Etype (F2) = Cursor
16014 and then Etype (It.Nam) = Standard_Boolean
16015 then
16016 Set_Entity (N, It.Nam);
16017 F2 := Next_Formal (F1);
16018 exit;
16019 end if;
16020
16021 elsif Nam = Name_Element then
16022 F2 := Next_Formal (F1);
16023
16024 if Present (F2)
16025 and then No (Next_Formal (F2))
16026 and then Etype (F2) = Cursor
16027 then
16028 Set_Entity (N, It.Nam);
16029 exit;
16030 end if;
16031 end if;
16032 end if;
16033
16034 Get_Next_Interp (I, It);
16035 end loop;
16036 end;
16037 end if;
16038 end Resolve_Iterable_Operation;
16039
16040 ------------------------------
16041 -- Resolve_Aspect_Aggregate --
16042 ------------------------------
16043
16044 procedure Resolve_Aspect_Aggregate
16045 (Typ : Entity_Id;
16046 Expr : Node_Id)
16047 is
16048 function Valid_Empty (E : Entity_Id) return Boolean;
16049 function Valid_Add_Named (E : Entity_Id) return Boolean;
16050 function Valid_Add_Unnamed (E : Entity_Id) return Boolean;
16051 function Valid_New_Indexed (E : Entity_Id) return Boolean;
16052 function Valid_Assign_Indexed (E : Entity_Id) return Boolean;
16053 -- Predicates that establish the legality of each possible operation in
16054 -- an Aggregate aspect.
16055
16056 generic
16057 with function Pred (Id : Node_Id) return Boolean;
16058 procedure Resolve_Operation (Subp_Id : Node_Id);
16059 -- Common processing to resolve each aggregate operation.
16060
16061 ------------------------
16062 -- Valid_Assign_Index --
16063 ------------------------
16064
16065 function Valid_Assign_Indexed (E : Entity_Id) return Boolean is
16066 begin
16067 -- The profile must be the same as for Add_Named, with the added
16068 -- requirement that the key_type be a discrete type.
16069
16070 if Valid_Add_Named (E) then
16071 return Is_Discrete_Type (Etype (Next_Formal (First_Formal (E))));
16072 else
16073 return False;
16074 end if;
16075 end Valid_Assign_Indexed;
16076
16077 -----------------
16078 -- Valid_Empty --
16079 -----------------
16080
16081 function Valid_Empty (E : Entity_Id) return Boolean is
16082 begin
16083 if Etype (E) /= Typ or else Scope (E) /= Scope (Typ) then
16084 return False;
16085
16086 elsif Ekind (E) = E_Constant then
16087 return True;
16088
16089 elsif Ekind (E) = E_Function then
16090 return No (First_Formal (E))
16091 or else
16092 (Is_Integer_Type (Etype (First_Formal (E)))
16093 and then No (Next_Formal (First_Formal (E))));
16094 else
16095 return False;
16096 end if;
16097 end Valid_Empty;
16098
16099 ---------------------
16100 -- Valid_Add_Named --
16101 ---------------------
16102
16103 function Valid_Add_Named (E : Entity_Id) return Boolean is
16104 F2, F3 : Entity_Id;
16105 begin
16106 if Ekind (E) = E_Procedure
16107 and then Scope (E) = Scope (Typ)
16108 and then Number_Formals (E) = 3
16109 and then Etype (First_Formal (E)) = Typ
16110 and then Ekind (First_Formal (E)) = E_In_Out_Parameter
16111 then
16112 F2 := Next_Formal (First_Formal (E));
16113 F3 := Next_Formal (F2);
16114 return Ekind (F2) = E_In_Parameter
16115 and then Ekind (F3) = E_In_Parameter
16116 and then not Is_Limited_Type (Etype (F2))
16117 and then not Is_Limited_Type (Etype (F3));
16118 else
16119 return False;
16120 end if;
16121 end Valid_Add_Named;
16122
16123 -----------------------
16124 -- Valid_Add_Unnamed --
16125 -----------------------
16126
16127 function Valid_Add_Unnamed (E : Entity_Id) return Boolean is
16128 begin
16129 return Ekind (E) = E_Procedure
16130 and then Scope (E) = Scope (Typ)
16131 and then Number_Formals (E) = 2
16132 and then Etype (First_Formal (E)) = Typ
16133 and then Ekind (First_Formal (E)) = E_In_Out_Parameter
16134 and then
16135 not Is_Limited_Type (Etype (Next_Formal (First_Formal (E))));
16136 end Valid_Add_Unnamed;
16137
16138 -----------------------
16139 -- Valid_Nmw_Indexed --
16140 -----------------------
16141
16142 function Valid_New_Indexed (E : Entity_Id) return Boolean is
16143 begin
16144 return Ekind (E) = E_Function
16145 and then Scope (E) = Scope (Typ)
16146 and then Etype (E) = Typ
16147 and then Number_Formals (E) = 2
16148 and then Is_Discrete_Type (Etype (First_Formal (E)))
16149 and then Etype (First_Formal (E)) =
16150 Etype (Next_Formal (First_Formal (E)));
16151 end Valid_New_Indexed;
16152
16153 -----------------------
16154 -- Resolve_Operation --
16155 -----------------------
16156
16157 procedure Resolve_Operation (Subp_Id : Node_Id) is
16158 Subp : Entity_Id;
16159
16160 I : Interp_Index;
16161 It : Interp;
16162
16163 begin
16164 if not Is_Overloaded (Subp_Id) then
16165 Subp := Entity (Subp_Id);
16166 if not Pred (Subp) then
16167 Error_Msg_NE
16168 ("improper aggregate operation for&", Subp_Id, Typ);
16169 end if;
16170
16171 else
16172 Set_Entity (Subp_Id, Empty);
16173 Get_First_Interp (Subp_Id, I, It);
16174 while Present (It.Nam) loop
16175 if Pred (It.Nam) then
16176 Set_Is_Overloaded (Subp_Id, False);
16177 Set_Entity (Subp_Id, It.Nam);
16178 exit;
16179 end if;
16180
16181 Get_Next_Interp (I, It);
16182 end loop;
16183
16184 if No (Entity (Subp_Id)) then
16185 Error_Msg_NE
16186 ("improper aggregate operation for&", Subp_Id, Typ);
16187 end if;
16188 end if;
16189 end Resolve_Operation;
16190
16191 Assoc : Node_Id;
16192 Op_Name : Name_Id;
16193 Subp_Id : Node_Id;
16194
16195 procedure Resolve_Empty is new Resolve_Operation (Valid_Empty);
16196 procedure Resolve_Unnamed is new Resolve_Operation (Valid_Add_Unnamed);
16197 procedure Resolve_Named is new Resolve_Operation (Valid_Add_Named);
16198 procedure Resolve_Indexed is new Resolve_Operation (Valid_New_Indexed);
16199 procedure Resolve_Assign_Indexed
16200 is new Resolve_Operation
16201 (Valid_Assign_Indexed);
16202
16203 -- Start of processing for Resolve_Aspect_Aggregate
16204
16205 begin
16206 Assoc := First (Component_Associations (Expr));
16207
16208 while Present (Assoc) loop
16209 Op_Name := Chars (First (Choices (Assoc)));
16210
16211 -- When verifying the consistency of aspects between the freeze point
16212 -- and the end of declarqtions, we use a copy which is not analyzed
16213 -- yet, so do it now.
16214
16215 Subp_Id := Expression (Assoc);
16216 if No (Etype (Subp_Id)) then
16217 Analyze (Subp_Id);
16218 end if;
16219
16220 if Op_Name = Name_Empty then
16221 Resolve_Empty (Subp_Id);
16222
16223 elsif Op_Name = Name_Add_Named then
16224 Resolve_Named (Subp_Id);
16225
16226 elsif Op_Name = Name_Add_Unnamed then
16227 Resolve_Unnamed (Subp_Id);
16228
16229 elsif Op_Name = Name_New_Indexed then
16230 Resolve_Indexed (Subp_Id);
16231
16232 elsif Op_Name = Name_Assign_Indexed then
16233 Resolve_Assign_Indexed (Subp_Id);
16234 end if;
16235
16236 Next (Assoc);
16237 end loop;
16238 end Resolve_Aspect_Aggregate;
16239
16240 --------------------------------------
16241 -- Resolve_Aspect_Stable_Properties --
16242 --------------------------------------
16243
16244 procedure Resolve_Aspect_Stable_Properties
16245 (Typ_Or_Subp : Entity_Id; Expr : Node_Id; Class_Present : Boolean)
16246 is
16247 Is_Aspect_Of_Type : constant Boolean := Is_Type (Typ_Or_Subp);
16248
16249 Singleton : constant Boolean := Nkind (Expr) /= N_Aggregate;
16250 Subp_Name : Node_Id := (if Singleton
16251 then Expr
16252 else First (Expressions (Expr)));
16253 Has_Not : Boolean;
16254 begin
16255 if Is_Aspect_Of_Type
16256 and then Has_Private_Declaration (Typ_Or_Subp)
16257 and then not Is_Private_Type (Typ_Or_Subp)
16258 then
16259 Error_Msg_N
16260 ("Stable_Properties aspect cannot be specified " &
16261 "for the completion of a private type", Typ_Or_Subp);
16262 end if;
16263
16264 -- Analogous checks that the aspect is not specified for a completion
16265 -- in the subprogram case are not performed here because they are not
16266 -- specific to this particular aspect. Right ???
16267
16268 loop
16269 Has_Not := Nkind (Subp_Name) = N_Op_Not;
16270 if Has_Not then
16271 Set_Analyzed (Subp_Name); -- ???
16272 Subp_Name := Right_Opnd (Subp_Name);
16273 end if;
16274
16275 if No (Etype (Subp_Name)) then
16276 Analyze (Subp_Name);
16277 end if;
16278
16279 declare
16280 Subp : Entity_Id := Empty;
16281
16282 I : Interp_Index;
16283 It : Interp;
16284
16285 function Is_Property_Function (E : Entity_Id) return Boolean;
16286 -- Implements RM 7.3.4 definition of "property function".
16287
16288 function Is_Property_Function (E : Entity_Id) return Boolean is
16289 begin
16290 if Ekind (E) not in E_Function | E_Operator
16291 or else Number_Formals (E) /= 1
16292 then
16293 return False;
16294 end if;
16295
16296 declare
16297 Param_Type : constant Entity_Id :=
16298 Base_Type (Etype (First_Formal (E)));
16299
16300 function Matches_Param_Type (Typ : Entity_Id)
16301 return Boolean is
16302 ((Base_Type (Typ) = Param_Type)
16303 or else
16304 (Is_Class_Wide_Type (Param_Type)
16305 and then Is_Ancestor (Root_Type (Param_Type),
16306 Base_Type (Typ))));
16307 begin
16308 if Is_Aspect_Of_Type then
16309 if Matches_Param_Type (Typ_Or_Subp) then
16310 return True;
16311 end if;
16312 elsif Is_Primitive (Typ_Or_Subp) then
16313 declare
16314 Formal : Entity_Id := First_Formal (Typ_Or_Subp);
16315 begin
16316 while Present (Formal) loop
16317 if Matches_Param_Type (Etype (Formal)) then
16318
16319 -- Test whether Typ_Or_Subp (which is a subp
16320 -- in this case) is primitive op of the type
16321 -- of this parameter.
16322 if Scope (Typ_Or_Subp) = Scope (Param_Type) then
16323 return True;
16324 end if;
16325 end if;
16326 Next_Formal (Formal);
16327 end loop;
16328 end;
16329 end if;
16330 end;
16331
16332 return False;
16333 end Is_Property_Function;
16334 begin
16335 if not Is_Overloaded (Subp_Name) then
16336 Subp := Entity (Subp_Name);
16337 if not Is_Property_Function (Subp) then
16338 Error_Msg_NE ("improper property function for&",
16339 Subp_Name, Typ_Or_Subp);
16340 return;
16341 end if;
16342 else
16343 Set_Entity (Subp_Name, Empty);
16344 Get_First_Interp (Subp_Name, I, It);
16345 while Present (It.Nam) loop
16346 if Is_Property_Function (It.Nam) then
16347 if Present (Subp) then
16348 Error_Msg_NE
16349 ("ambiguous property function name for&",
16350 Subp_Name, Typ_Or_Subp);
16351 return;
16352 end if;
16353
16354 Subp := It.Nam;
16355 Set_Is_Overloaded (Subp_Name, False);
16356 Set_Entity (Subp_Name, Subp);
16357 end if;
16358
16359 Get_Next_Interp (I, It);
16360 end loop;
16361
16362 if No (Subp) then
16363 Error_Msg_NE ("improper property function for&",
16364 Subp_Name, Typ_Or_Subp);
16365 return;
16366 end if;
16367 end if;
16368
16369 -- perform legality (as opposed to name resolution) Subp checks
16370
16371 if Is_Limited_Type (Etype (Subp)) then
16372 Error_Msg_NE
16373 ("result type of property function for& is limited",
16374 Subp_Name, Typ_Or_Subp);
16375 end if;
16376
16377 if Ekind (First_Formal (Subp)) /= E_In_Parameter then
16378 Error_Msg_NE
16379 ("mode of parameter of property function for& is not IN",
16380 Subp_Name, Typ_Or_Subp);
16381 end if;
16382
16383 if Is_Class_Wide_Type (Etype (First_Formal (Subp))) then
16384 if not Covers (Etype (First_Formal (Subp)), Typ_Or_Subp) then
16385 Error_Msg_NE
16386 ("class-wide parameter type of property function " &
16387 "for& does not cover the type",
16388 Subp_Name, Typ_Or_Subp);
16389
16390 -- ??? This test is slightly stricter than 7.3.4(12/5);
16391 -- some legal corner cases may be incorrectly rejected.
16392 elsif Scope (Subp) /= Scope (Etype (First_Formal (Subp)))
16393 then
16394 Error_Msg_NE
16395 ("property function for& not declared in same scope " &
16396 "as parameter type",
16397 Subp_Name, Typ_Or_Subp);
16398 end if;
16399 elsif Is_Aspect_Of_Type and then
16400 Scope (Subp) /= Scope (Typ_Or_Subp) and then
16401 Scope (Subp) /= Standard_Standard -- e.g., derived type's "abs"
16402 then
16403 Error_Msg_NE
16404 ("property function for& " &
16405 "not a primitive function of the type",
16406 Subp_Name, Typ_Or_Subp);
16407 end if;
16408
16409 if Has_Not then
16410 -- check that Subp was mentioned in param type's aspect spec
16411 declare
16412 Param_Type : constant Entity_Id :=
16413 Base_Type (Etype (First_Formal (Subp)));
16414 Aspect_Spec : constant Node_Id :=
16415 Find_Value_Of_Aspect
16416 (Param_Type, Aspect_Stable_Properties,
16417 Class_Present => Class_Present);
16418 Found : Boolean := False;
16419 begin
16420 if Present (Aspect_Spec) then
16421 declare
16422 Ignored : Boolean;
16423 SPF_List : constant Subprogram_List :=
16424 Parse_Aspect_Stable_Properties
16425 (Aspect_Spec, Negated => Ignored);
16426 begin
16427 Found := (for some E of SPF_List => E = Subp);
16428 -- look through renamings ???
16429 end;
16430 end if;
16431 if not Found then
16432 declare
16433 CW_Modifier : constant String :=
16434 (if Class_Present then "class-wide " else "");
16435 begin
16436 Error_Msg_NE
16437 (CW_Modifier
16438 & "property function for& mentioned after NOT "
16439 & "but not a "
16440 & CW_Modifier
16441 & "stable property function of its parameter type",
16442 Subp_Name, Typ_Or_Subp);
16443 end;
16444 end if;
16445 end;
16446 end if;
16447 end;
16448
16449 exit when Singleton;
16450 Subp_Name :=
16451 Next ((if Has_Not then Parent (Subp_Name) else Subp_Name));
16452 exit when No (Subp_Name);
16453 end loop;
16454
16455 Set_Analyzed (Expr);
16456 end Resolve_Aspect_Stable_Properties;
16457
16458 -----------------------------------------
16459 -- Resolve_Storage_Model_Type_Argument --
16460 -----------------------------------------
16461
16462 procedure Resolve_Storage_Model_Type_Argument
16463 (N : Node_Id;
16464 Typ : Entity_Id;
16465 Addr_Type : in out Entity_Id;
16466 Nam : Name_Id)
16467 is
16468
16469 type Formal_Profile is record
16470 Subt : Entity_Id;
16471 Mode : Formal_Kind;
16472 end record;
16473
16474 type Formal_Profiles is array (Positive range <>) of Formal_Profile;
16475
16476 function Aspect_Argument_Profile_Matches
16477 (Subp : Entity_Id;
16478 Profiles : Formal_Profiles;
16479 Result_Subt : Entity_Id;
16480 Err_On_Mismatch : Boolean) return Boolean;
16481 -- Checks that the formal parameters of subprogram Subp conform to the
16482 -- subtypes and modes specified by Profiles, as well as to the result
16483 -- subtype Result_Subt when that is nonempty.
16484
16485 function Aspect_Argument_Profile_Matches
16486 (Subp : Entity_Id;
16487 Profiles : Formal_Profiles;
16488 Result_Subt : Entity_Id;
16489 Err_On_Mismatch : Boolean) return Boolean
16490 is
16491
16492 procedure Report_Argument_Error
16493 (Msg : String;
16494 Formal : Entity_Id := Empty;
16495 Subt : Entity_Id := Empty);
16496 -- If Err_On_Mismatch is True, reports an argument error given by Msg
16497 -- associated with Formal and/or Subt.
16498
16499 procedure Report_Argument_Error
16500 (Msg : String;
16501 Formal : Entity_Id := Empty;
16502 Subt : Entity_Id := Empty)
16503 is
16504 begin
16505 if Err_On_Mismatch then
16506 if Present (Formal) then
16507 if Present (Subt) then
16508 Error_Msg_Node_2 := Subt;
16509 end if;
16510 Error_Msg_NE (Msg, N, Formal);
16511
16512 elsif Present (Subt) then
16513 Error_Msg_NE (Msg, N, Subt);
16514
16515 else
16516 Error_Msg_N (Msg, N);
16517 end if;
16518 end if;
16519 end Report_Argument_Error;
16520
16521 -- Local variables
16522
16523 Formal : Entity_Id := First_Formal (Subp);
16524 Is_Error : Boolean := False;
16525
16526 -- Start of processing for Aspect_Argument_Profile_Matches
16527
16528 begin
16529 for FP of Profiles loop
16530 if No (Formal) then
16531 Is_Error := True;
16532 Report_Argument_Error ("missing formal of }", Subt => FP.Subt);
16533 exit;
16534
16535 elsif not Subtypes_Statically_Match
16536 (Etype (Formal), FP.Subt)
16537 then
16538 Is_Error := True;
16539 Report_Argument_Error
16540 ("formal& must be of subtype&",
16541 Formal => Formal, Subt => FP.Subt);
16542 exit;
16543
16544 elsif Ekind (Formal) /= FP.Mode then
16545 Is_Error := True;
16546 Report_Argument_Error
16547 ("formal& has wrong mode", Formal => Formal);
16548 exit;
16549 end if;
16550
16551 Formal := Next_Formal (Formal);
16552 end loop;
16553
16554 if not Is_Error
16555 and then Present (Formal)
16556 then
16557 Is_Error := True;
16558 Report_Argument_Error
16559 ("too many formals for subprogram in aspect");
16560 end if;
16561
16562 if not Is_Error
16563 and then Present (Result_Subt)
16564 and then not Subtypes_Statically_Match (Etype (Subp), Result_Subt)
16565 then
16566 Is_Error := True;
16567 Report_Argument_Error
16568 ("subprogram must have result}", Subt => Result_Subt);
16569 end if;
16570
16571 return not Is_Error;
16572 end Aspect_Argument_Profile_Matches;
16573
16574 -- Local variables
16575
16576 Ent : Entity_Id;
16577
16578 Storage_Count_Type : constant Entity_Id := RTE (RE_Storage_Count);
16579 System_Address_Type : constant Entity_Id := RTE (RE_Address);
16580
16581 -- Start of processing for Resolve_Storage_Model_Type_Argument
16582
16583 begin
16584 if Nam = Name_Address_Type then
16585 if not Is_Entity_Name (N)
16586 or else not Is_Type (Entity (N))
16587 or else (Root_Type (Entity (N)) /= System_Address_Type
16588 and then not Is_Integer_Type (Entity (N)))
16589 then
16590 Error_Msg_N ("named entity must be a descendant of System.Address "
16591 & "or an integer type", N);
16592 end if;
16593
16594 Addr_Type := Entity (N);
16595
16596 return;
16597
16598 -- If Addr_Type is not present as the first association, then we default
16599 -- it to System.Address.
16600
16601 elsif No (Addr_Type) then
16602 Addr_Type := RTE (RE_Address);
16603 end if;
16604
16605 if Nam = Name_Null_Address then
16606 if not Is_Entity_Name (N)
16607 or else not Is_Constant_Object (Entity (N))
16608 or else
16609 not Subtypes_Statically_Match (Etype (Entity (N)), Addr_Type)
16610 then
16611 Error_Msg_NE
16612 ("named entity must be constant of subtype}", N, Addr_Type);
16613 end if;
16614
16615 return;
16616
16617 elsif not Is_Overloaded (N) then
16618 if not Is_Entity_Name (N)
16619 or else Ekind (Entity (N)) not in E_Function | E_Procedure
16620 or else Scope (Entity (N)) /= Scope (Typ)
16621 then
16622 Error_Msg_N ("argument must be local subprogram name", N);
16623 return;
16624 end if;
16625
16626 Ent := Entity (N);
16627
16628 if Nam = Name_Allocate then
16629 if not Aspect_Argument_Profile_Matches
16630 (Ent,
16631 Profiles =>
16632 ((Typ, E_In_Out_Parameter),
16633 (Addr_Type, E_Out_Parameter),
16634 (Storage_Count_Type, E_In_Parameter),
16635 (Storage_Count_Type, E_In_Parameter)),
16636 Result_Subt => Empty,
16637 Err_On_Mismatch => True)
16638 then
16639 Error_Msg_N ("no match for Allocate operation", N);
16640 end if;
16641
16642 elsif Nam = Name_Deallocate then
16643 if not Aspect_Argument_Profile_Matches
16644 (Ent,
16645 Profiles =>
16646 ((Typ, E_In_Out_Parameter),
16647 (Addr_Type, E_In_Parameter),
16648 (Storage_Count_Type, E_In_Parameter),
16649 (Storage_Count_Type, E_In_Parameter)),
16650 Result_Subt => Empty,
16651 Err_On_Mismatch => True)
16652 then
16653 Error_Msg_N ("no match for Deallocate operation", N);
16654 end if;
16655
16656 elsif Nam = Name_Copy_From then
16657 if not Aspect_Argument_Profile_Matches
16658 (Ent,
16659 Profiles =>
16660 ((Typ, E_In_Out_Parameter),
16661 (System_Address_Type, E_In_Parameter),
16662 (Addr_Type, E_In_Parameter),
16663 (Storage_Count_Type, E_In_Parameter)),
16664 Result_Subt => Empty,
16665 Err_On_Mismatch => True)
16666 then
16667 Error_Msg_N ("no match for Copy_From operation", N);
16668 end if;
16669
16670 elsif Nam = Name_Copy_To then
16671 if not Aspect_Argument_Profile_Matches
16672 (Ent,
16673 Profiles =>
16674 ((Typ, E_In_Out_Parameter),
16675 (Addr_Type, E_In_Parameter),
16676 (System_Address_Type, E_In_Parameter),
16677 (Storage_Count_Type, E_In_Parameter)),
16678 Result_Subt => Empty,
16679 Err_On_Mismatch => True)
16680 then
16681 Error_Msg_N ("no match for Copy_To operation", N);
16682 end if;
16683
16684 elsif Nam = Name_Storage_Size then
16685 if not Aspect_Argument_Profile_Matches
16686 (Ent,
16687 Profiles => (1 => (Typ, E_In_Parameter)),
16688 Result_Subt => Storage_Count_Type,
16689 Err_On_Mismatch => True)
16690 then
16691 Error_Msg_N ("no match for Storage_Size operation", N);
16692 end if;
16693
16694 else
16695 null; -- Error will be caught in Validate_Storage_Model_Type_Aspect
16696 end if;
16697
16698 else
16699 -- Overloaded case: find subprogram with proper signature
16700
16701 declare
16702 I : Interp_Index;
16703 It : Interp;
16704 Found_Match : Boolean := False;
16705
16706 begin
16707 Get_First_Interp (N, I, It);
16708 while Present (It.Typ) loop
16709 if Ekind (It.Nam) in E_Function | E_Procedure
16710 and then Scope (It.Nam) = Scope (Typ)
16711 then
16712 if Nam = Name_Allocate then
16713 Found_Match :=
16714 Aspect_Argument_Profile_Matches
16715 (It.Nam,
16716 Profiles =>
16717 ((Typ, E_In_Out_Parameter),
16718 (Addr_Type, E_Out_Parameter),
16719 (Storage_Count_Type, E_In_Parameter),
16720 (Storage_Count_Type, E_In_Parameter)),
16721 Result_Subt => Empty,
16722 Err_On_Mismatch => False);
16723
16724 elsif Nam = Name_Deallocate then
16725 Found_Match :=
16726 Aspect_Argument_Profile_Matches
16727 (It.Nam,
16728 Profiles =>
16729 ((Typ, E_In_Out_Parameter),
16730 (Addr_Type, E_In_Parameter),
16731 (Storage_Count_Type, E_In_Parameter),
16732 (Storage_Count_Type, E_In_Parameter)),
16733 Result_Subt => Empty,
16734 Err_On_Mismatch => False);
16735
16736 elsif Nam = Name_Copy_From then
16737 Found_Match :=
16738 Aspect_Argument_Profile_Matches
16739 (It.Nam,
16740 Profiles =>
16741 ((Typ, E_In_Out_Parameter),
16742 (System_Address_Type, E_In_Parameter),
16743 (Addr_Type, E_In_Parameter),
16744 (Storage_Count_Type, E_In_Parameter),
16745 (Storage_Count_Type, E_In_Parameter)),
16746 Result_Subt => Empty,
16747 Err_On_Mismatch => False);
16748
16749 elsif Nam = Name_Copy_To then
16750 Found_Match :=
16751 Aspect_Argument_Profile_Matches
16752 (It.Nam,
16753 Profiles =>
16754 ((Typ, E_In_Out_Parameter),
16755 (Addr_Type, E_In_Parameter),
16756 (Storage_Count_Type, E_In_Parameter),
16757 (System_Address_Type, E_In_Parameter),
16758 (Storage_Count_Type, E_In_Parameter)),
16759 Result_Subt => Empty,
16760 Err_On_Mismatch => False);
16761
16762 elsif Nam = Name_Storage_Size then
16763 Found_Match :=
16764 Aspect_Argument_Profile_Matches
16765 (It.Nam,
16766 Profiles => (1 => (Typ, E_In_Parameter)),
16767 Result_Subt => Storage_Count_Type,
16768 Err_On_Mismatch => False);
16769 end if;
16770
16771 if Found_Match then
16772 Set_Entity (N, It.Nam);
16773 exit;
16774 end if;
16775 end if;
16776
16777 Get_Next_Interp (I, It);
16778 end loop;
16779
16780 if not Found_Match then
16781 Error_Msg_N
16782 ("no match found for Storage_Model_Type operation", N);
16783 end if;
16784 end;
16785 end if;
16786 end Resolve_Storage_Model_Type_Argument;
16787
16788 ----------------
16789 -- Set_Biased --
16790 ----------------
16791
16792 procedure Set_Biased
16793 (E : Entity_Id;
16794 N : Node_Id;
16795 Msg : String;
16796 Biased : Boolean := True)
16797 is
16798 begin
16799 if Biased then
16800 Set_Has_Biased_Representation (E);
16801
16802 if Warn_On_Biased_Representation then
16803 Error_Msg_NE
16804 ("?.b?" & Msg & " forces biased representation for&", N, E);
16805 end if;
16806 end if;
16807 end Set_Biased;
16808
16809 --------------------
16810 -- Set_Enum_Esize --
16811 --------------------
16812
16813 procedure Set_Enum_Esize (T : Entity_Id) is
16814 Lo : Uint;
16815 Hi : Uint;
16816 Sz : Unat;
16817
16818 begin
16819 Reinit_Alignment (T);
16820
16821 -- Find the minimum standard size (8,16,32,64,128) that fits
16822
16823 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
16824 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
16825
16826 if Lo < 0 then
16827 if Lo >= -Uint_2**7 and then Hi < Uint_2**7 then
16828 Sz := UI_From_Int (Standard_Character_Size);
16829 -- Might be > 8 on some targets
16830
16831 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
16832 Sz := Uint_16;
16833
16834 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
16835 Sz := Uint_32;
16836
16837 elsif Lo >= -Uint_2**63 and then Hi < Uint_2**63 then
16838 Sz := Uint_64;
16839
16840 else pragma Assert (Lo >= -Uint_2**127 and then Hi < Uint_2**127);
16841 Sz := Uint_128;
16842 end if;
16843
16844 else
16845 if Hi < Uint_2**8 then
16846 Sz := UI_From_Int (Standard_Character_Size);
16847
16848 elsif Hi < Uint_2**16 then
16849 Sz := Uint_16;
16850
16851 elsif Hi < Uint_2**32 then
16852 Sz := Uint_32;
16853
16854 elsif Hi < Uint_2**64 then
16855 Sz := Uint_64;
16856
16857 else pragma Assert (Hi < Uint_2**128);
16858 Sz := Uint_128;
16859 end if;
16860 end if;
16861
16862 -- That minimum is the proper size unless we have a foreign convention
16863 -- and the size required is 32 or less, in which case we bump the size
16864 -- up to 32. This is required for C and C++ and seems reasonable for
16865 -- all other foreign conventions.
16866
16867 if Has_Foreign_Convention (T)
16868 and then Esize (T) < Standard_Integer_Size
16869
16870 -- Don't do this if Short_Enums on target
16871
16872 and then not Target_Short_Enums
16873 then
16874 Set_Esize (T, UI_From_Int (Standard_Integer_Size));
16875 else
16876 Set_Esize (T, Sz);
16877 end if;
16878 end Set_Enum_Esize;
16879
16880 -----------------------------
16881 -- Uninstall_Discriminants --
16882 -----------------------------
16883
16884 procedure Uninstall_Discriminants (E : Entity_Id) is
16885 Disc : Entity_Id;
16886 Prev : Entity_Id;
16887 Outer : Entity_Id;
16888
16889 begin
16890 -- Discriminants have been made visible for type declarations and
16891 -- protected type declarations, not for subtype declarations.
16892
16893 if Nkind (Parent (E)) /= N_Subtype_Declaration then
16894 Disc := First_Discriminant (E);
16895 while Present (Disc) loop
16896 if Disc /= Current_Entity (Disc) then
16897 Prev := Current_Entity (Disc);
16898 while Present (Prev)
16899 and then Present (Homonym (Prev))
16900 and then Homonym (Prev) /= Disc
16901 loop
16902 Prev := Homonym (Prev);
16903 end loop;
16904 else
16905 Prev := Empty;
16906 end if;
16907
16908 Set_Is_Immediately_Visible (Disc, False);
16909
16910 Outer := Homonym (Disc);
16911 while Present (Outer) and then Scope (Outer) = E loop
16912 Outer := Homonym (Outer);
16913 end loop;
16914
16915 -- Reset homonym link of other entities, but do not modify link
16916 -- between entities in current scope, so that the back end can
16917 -- have a proper count of local overloadings.
16918
16919 if No (Prev) then
16920 Set_Name_Entity_Id (Chars (Disc), Outer);
16921
16922 elsif Scope (Prev) /= Scope (Disc) then
16923 Set_Homonym (Prev, Outer);
16924 end if;
16925
16926 Next_Discriminant (Disc);
16927 end loop;
16928 end if;
16929 end Uninstall_Discriminants;
16930
16931 ------------------------------
16932 -- Validate_Address_Clauses --
16933 ------------------------------
16934
16935 procedure Validate_Address_Clauses is
16936 function Offset_Value (Expr : Node_Id) return Uint;
16937 -- Given an Address attribute reference, return the value in bits of its
16938 -- offset from the first bit of the underlying entity, or 0 if it is not
16939 -- known at compile time.
16940
16941 ------------------
16942 -- Offset_Value --
16943 ------------------
16944
16945 function Offset_Value (Expr : Node_Id) return Uint is
16946 N : Node_Id := Prefix (Expr);
16947 Off : Uint;
16948 Val : Uint := Uint_0;
16949
16950 begin
16951 -- Climb the prefix chain and compute the cumulative offset
16952
16953 loop
16954 if Is_Entity_Name (N) then
16955 return Val;
16956
16957 elsif Nkind (N) = N_Selected_Component then
16958 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
16959 if Present (Off) and then Off >= Uint_0 then
16960 Val := Val + Off;
16961 N := Prefix (N);
16962 else
16963 return Uint_0;
16964 end if;
16965
16966 elsif Nkind (N) = N_Indexed_Component then
16967 Off := Indexed_Component_Bit_Offset (N);
16968 if Present (Off) then
16969 Val := Val + Off;
16970 N := Prefix (N);
16971 else
16972 return Uint_0;
16973 end if;
16974
16975 else
16976 return Uint_0;
16977 end if;
16978 end loop;
16979 end Offset_Value;
16980
16981 -- Start of processing for Validate_Address_Clauses
16982
16983 begin
16984 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
16985 declare
16986 ACCR : Address_Clause_Check_Record
16987 renames Address_Clause_Checks.Table (J);
16988
16989 Expr : Node_Id;
16990
16991 X_Alignment : Uint;
16992 Y_Alignment : Uint := Uint_0;
16993
16994 X_Size : Uint;
16995 Y_Size : Uint := Uint_0;
16996
16997 X_Offs : Uint;
16998
16999 begin
17000 -- Skip processing of this entry if warning already posted, or if
17001 -- alignments are not set.
17002
17003 if not Address_Warning_Posted (ACCR.N)
17004 and then Known_Alignment (ACCR.X)
17005 and then Known_Alignment (ACCR.Y)
17006 then
17007 Expr := Original_Node (Expression (ACCR.N));
17008
17009 -- Get alignments, sizes and offset, if any
17010
17011 X_Alignment := Alignment (ACCR.X);
17012 X_Size := Esize (ACCR.X);
17013
17014 if Present (ACCR.Y) then
17015 Y_Alignment := Alignment (ACCR.Y);
17016 Y_Size :=
17017 (if Known_Esize (ACCR.Y) then Esize (ACCR.Y) else Uint_0);
17018 end if;
17019
17020 if ACCR.Off
17021 and then Nkind (Expr) = N_Attribute_Reference
17022 and then Attribute_Name (Expr) = Name_Address
17023 then
17024 X_Offs := Offset_Value (Expr);
17025 else
17026 X_Offs := Uint_0;
17027 end if;
17028
17029 -- Check for known value not multiple of alignment
17030
17031 if No (ACCR.Y) then
17032 if not Alignment_Checks_Suppressed (ACCR)
17033 and then X_Alignment /= 0
17034 and then ACCR.A mod X_Alignment /= 0
17035 then
17036 Error_Msg_NE
17037 ("??specified address for& is inconsistent with "
17038 & "alignment", ACCR.N, ACCR.X);
17039 Error_Msg_N
17040 ("\??program execution may be erroneous (RM 13.3(27))",
17041 ACCR.N);
17042
17043 Error_Msg_Uint_1 := X_Alignment;
17044 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
17045 end if;
17046
17047 -- Check for large object overlaying smaller one
17048
17049 elsif Y_Size > Uint_0
17050 and then X_Size > Uint_0
17051 and then X_Offs + X_Size > Y_Size
17052 then
17053 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
17054 Error_Msg_N
17055 ("\??program execution may be erroneous", ACCR.N);
17056
17057 Error_Msg_Uint_1 := X_Size;
17058 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
17059
17060 Error_Msg_Uint_1 := Y_Size;
17061 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
17062
17063 if Y_Size >= X_Size then
17064 Error_Msg_Uint_1 := X_Offs;
17065 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
17066 end if;
17067
17068 -- Check for inadequate alignment, both of the base object
17069 -- and of the offset, if any. We only do this check if the
17070 -- run-time Alignment_Check is active. No point in warning
17071 -- if this check has been suppressed (or is suppressed by
17072 -- default in the non-strict alignment machine case).
17073
17074 -- Note: we do not check the alignment if we gave a size
17075 -- warning, since it would likely be redundant.
17076
17077 elsif not Alignment_Checks_Suppressed (ACCR)
17078 and then Y_Alignment /= Uint_0
17079 and then
17080 (Y_Alignment < X_Alignment
17081 or else
17082 (ACCR.Off
17083 and then Nkind (Expr) = N_Attribute_Reference
17084 and then Attribute_Name (Expr) = Name_Address
17085 and then Has_Compatible_Alignment
17086 (ACCR.X, Prefix (Expr), True) /=
17087 Known_Compatible))
17088 then
17089 Error_Msg_NE
17090 ("??specified address for& may be inconsistent with "
17091 & "alignment", ACCR.N, ACCR.X);
17092 Error_Msg_N
17093 ("\??program execution may be erroneous (RM 13.3(27))",
17094 ACCR.N);
17095
17096 Error_Msg_Uint_1 := X_Alignment;
17097 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
17098
17099 Error_Msg_Uint_1 := Y_Alignment;
17100 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
17101
17102 if Y_Alignment >= X_Alignment then
17103 Error_Msg_N
17104 ("\??but offset is not multiple of alignment", ACCR.N);
17105 end if;
17106 end if;
17107 end if;
17108 end;
17109 end loop;
17110 end Validate_Address_Clauses;
17111
17112 ------------------------------
17113 -- Validate_Iterable_Aspect --
17114 ------------------------------
17115
17116 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
17117 Aggr : constant Node_Id := Expression (ASN);
17118 Assoc : Node_Id;
17119 Expr : Node_Id;
17120
17121 Prim : Node_Id;
17122 Cursor : Entity_Id;
17123
17124 First_Id : Entity_Id := Empty;
17125 Last_Id : Entity_Id := Empty;
17126 Next_Id : Entity_Id := Empty;
17127 Has_Element_Id : Entity_Id := Empty;
17128 Element_Id : Entity_Id := Empty;
17129
17130 begin
17131 if Nkind (Aggr) /= N_Aggregate then
17132 Error_Msg_N ("aspect Iterable must be an aggregate", Aggr);
17133 return;
17134 end if;
17135
17136 Cursor := Get_Cursor_Type (ASN, Typ);
17137
17138 -- If previous error aspect is unusable
17139
17140 if Cursor = Any_Type then
17141 return;
17142 end if;
17143
17144 if not Is_Empty_List (Expressions (Aggr)) then
17145 Error_Msg_N
17146 ("illegal positional association", First (Expressions (Aggr)));
17147 end if;
17148
17149 -- Each expression must resolve to a function with the proper signature
17150
17151 Assoc := First (Component_Associations (Aggr));
17152 while Present (Assoc) loop
17153 Expr := Expression (Assoc);
17154 Analyze (Expr);
17155
17156 Prim := First (Choices (Assoc));
17157
17158 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
17159 Error_Msg_N ("illegal name in association", Prim);
17160
17161 elsif Chars (Prim) = Name_First then
17162 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
17163 First_Id := Entity (Expr);
17164
17165 elsif Chars (Prim) = Name_Last then
17166 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Last);
17167 Last_Id := Entity (Expr);
17168
17169 elsif Chars (Prim) = Name_Previous then
17170 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Previous);
17171 Last_Id := Entity (Expr);
17172
17173 elsif Chars (Prim) = Name_Next then
17174 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
17175 Next_Id := Entity (Expr);
17176
17177 elsif Chars (Prim) = Name_Has_Element then
17178 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
17179 Has_Element_Id := Entity (Expr);
17180
17181 elsif Chars (Prim) = Name_Element then
17182 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
17183 Element_Id := Entity (Expr);
17184
17185 else
17186 Error_Msg_N ("invalid name for iterable function", Prim);
17187 end if;
17188
17189 Next (Assoc);
17190 end loop;
17191
17192 if No (First_Id) then
17193 Error_Msg_N ("match for First primitive not found", ASN);
17194
17195 elsif No (Next_Id) then
17196 Error_Msg_N ("match for Next primitive not found", ASN);
17197
17198 elsif No (Has_Element_Id) then
17199 Error_Msg_N ("match for Has_Element primitive not found", ASN);
17200
17201 elsif No (Element_Id) or else No (Last_Id) then
17202 null; -- optional
17203 end if;
17204 end Validate_Iterable_Aspect;
17205
17206 ------------------------------
17207 -- Validate_Literal_Aspect --
17208 ------------------------------
17209
17210 procedure Validate_Literal_Aspect (Typ : Entity_Id; ASN : Node_Id) is
17211 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
17212 pragma Assert (A_Id in Aspect_Integer_Literal |
17213 Aspect_Real_Literal | Aspect_String_Literal);
17214 Func_Name : constant Node_Id := Expression (ASN);
17215 Overloaded : Boolean := Is_Overloaded (Func_Name);
17216
17217 I : Interp_Index := 0;
17218 It : Interp;
17219 Param_Type : Entity_Id;
17220 Match_Found : Boolean := False;
17221 Match2_Found : Boolean := False;
17222 Is_Match : Boolean;
17223 Match : Interp;
17224 Match2 : Entity_Id := Empty;
17225
17226 function Matching
17227 (Param_Id : Entity_Id; Param_Type : Entity_Id) return Boolean;
17228 -- Return True if Param_Id is a non aliased in parameter whose base type
17229 -- is Param_Type.
17230
17231 --------------
17232 -- Matching --
17233 --------------
17234
17235 function Matching
17236 (Param_Id : Entity_Id; Param_Type : Entity_Id) return Boolean is
17237 begin
17238 return Base_Type (Etype (Param_Id)) = Param_Type
17239 and then Ekind (Param_Id) = E_In_Parameter
17240 and then not Is_Aliased (Param_Id);
17241 end Matching;
17242
17243 begin
17244 if not Is_Type (Typ) then
17245 Error_Msg_N ("aspect can only be specified for a type", ASN);
17246 return;
17247
17248 elsif not Is_First_Subtype (Typ) then
17249 Error_Msg_N ("aspect cannot be specified for a subtype", ASN);
17250 return;
17251 end if;
17252
17253 if A_Id = Aspect_String_Literal then
17254 if Is_String_Type (Typ) then
17255 Error_Msg_N ("aspect cannot be specified for a string type", ASN);
17256 return;
17257 end if;
17258
17259 Param_Type := Standard_Wide_Wide_String;
17260
17261 else
17262 if Is_Numeric_Type (Typ) then
17263 Error_Msg_N ("aspect cannot be specified for a numeric type", ASN);
17264 return;
17265 end if;
17266
17267 Param_Type := Standard_String;
17268 end if;
17269
17270 if not Overloaded and then No (Entity (Func_Name)) then
17271 -- The aspect is specified by a subprogram name, which
17272 -- may be an operator name given originally by a string.
17273
17274 if Is_Operator_Name (Chars (Func_Name)) then
17275 Analyze_Operator_Symbol (Func_Name);
17276 else
17277 Analyze (Func_Name);
17278 end if;
17279
17280 Overloaded := Is_Overloaded (Func_Name);
17281 end if;
17282
17283 if Overloaded then
17284 Get_First_Interp (Func_Name, I => I, It => It);
17285 else
17286 -- only one possible interpretation
17287 It.Nam := Entity (Func_Name);
17288 pragma Assert (Present (It.Nam));
17289 end if;
17290
17291 while It.Nam /= Empty loop
17292 Is_Match := False;
17293
17294 if Ekind (It.Nam) = E_Function
17295 and then Base_Type (Etype (It.Nam)) = Base_Type (Typ)
17296 then
17297 declare
17298 Params : constant List_Id :=
17299 Parameter_Specifications (Parent (It.Nam));
17300 Param_Spec : Node_Id;
17301
17302 begin
17303 if List_Length (Params) = 1 then
17304 Param_Spec := First (Params);
17305 Is_Match :=
17306 Matching (Defining_Identifier (Param_Spec), Param_Type);
17307
17308 -- Look for the optional overloaded 2-param Real_Literal
17309
17310 elsif List_Length (Params) = 2
17311 and then A_Id = Aspect_Real_Literal
17312 then
17313 Param_Spec := First (Params);
17314
17315 if Matching (Defining_Identifier (Param_Spec), Param_Type)
17316 then
17317 Param_Spec := Next (Param_Spec);
17318
17319 if Matching (Defining_Identifier (Param_Spec), Param_Type)
17320 then
17321 if No (Match2) then
17322 Match2 := It.Nam;
17323 Match2_Found := True;
17324 else
17325 -- If we find more than one possible match then
17326 -- do not take any into account here: since the
17327 -- 2-parameter version of Real_Literal is optional
17328 -- we cannot generate an error here, so let
17329 -- standard resolution fail later if we do need to
17330 -- call this variant.
17331
17332 Match2_Found := False;
17333 end if;
17334 end if;
17335 end if;
17336 end if;
17337 end;
17338 end if;
17339
17340 if Is_Match then
17341 if Match_Found then
17342 Error_Msg_N ("aspect specification is ambiguous", ASN);
17343 return;
17344 end if;
17345
17346 Match_Found := True;
17347 Match := It;
17348 end if;
17349
17350 exit when not Overloaded;
17351
17352 if not Is_Match then
17353 Remove_Interp (I => I);
17354 end if;
17355
17356 Get_Next_Interp (I => I, It => It);
17357 end loop;
17358
17359 if not Match_Found then
17360 Error_Msg_N
17361 ("function name in aspect specification cannot be resolved", ASN);
17362 return;
17363 end if;
17364
17365 Set_Entity (Func_Name, Match.Nam);
17366 Set_Etype (Func_Name, Etype (Match.Nam));
17367 Set_Is_Overloaded (Func_Name, False);
17368
17369 -- Record the match for 2-parameter function if found
17370
17371 if Match2_Found then
17372 Set_Related_Expression (Match.Nam, Match2);
17373 end if;
17374 end Validate_Literal_Aspect;
17375
17376 ----------------------------------------
17377 -- Validate_Storage_Model_Type_Aspect --
17378 ----------------------------------------
17379
17380 procedure Validate_Storage_Model_Type_Aspect
17381 (Typ : Entity_Id; ASN : Node_Id)
17382 is
17383 Assoc : Node_Id;
17384 Choice : Entity_Id;
17385 Choice_Name : Name_Id;
17386 Expr : Node_Id;
17387
17388 Address_Type_Id : Entity_Id := Empty;
17389 Null_Address_Id : Entity_Id := Empty;
17390 Allocate_Id : Entity_Id := Empty;
17391 Deallocate_Id : Entity_Id := Empty;
17392 Copy_From_Id : Entity_Id := Empty;
17393 Copy_To_Id : Entity_Id := Empty;
17394 Storage_Size_Id : Entity_Id := Empty;
17395
17396 procedure Check_And_Resolve_Storage_Model_Type_Argument
17397 (Expr : Node_Id;
17398 Typ : Entity_Id;
17399 Argument_Id : in out Entity_Id;
17400 Nam : Name_Id);
17401 -- Checks that the subaspect for Nam has not already been specified for
17402 -- Typ's Storage_Model_Type aspect (i.e., checks Argument_Id = Empty),
17403 -- resolves Expr, and sets Argument_Id to the entity resolved for Expr.
17404
17405 procedure Check_And_Resolve_Storage_Model_Type_Argument
17406 (Expr : Node_Id;
17407 Typ : Entity_Id;
17408 Argument_Id : in out Entity_Id;
17409 Nam : Name_Id)
17410 is
17411 Name_String : String := Get_Name_String (Nam);
17412
17413 begin
17414 To_Mixed (Name_String);
17415
17416 if Present (Argument_Id) then
17417 Error_Msg_String (1 .. Name_String'Length) := Name_String;
17418 Error_Msg_Strlen := Name_String'Length;
17419
17420 Error_Msg_N ("~ already specified", Expr);
17421 end if;
17422
17423 Resolve_Storage_Model_Type_Argument (Expr, Typ, Address_Type_Id, Nam);
17424 Argument_Id := Entity (Expr);
17425 end Check_And_Resolve_Storage_Model_Type_Argument;
17426
17427 -- Start of processing for Validate_Storage_Model_Type_Aspect
17428
17429 begin
17430 -- The aggregate argument of Storage_Model_Type is optional, and when
17431 -- not present the aspect defaults to the native storage model (where
17432 -- the address type is System.Address, and other arguments default to
17433 -- the corresponding native storage operations).
17434
17435 if No (Expression (ASN)) then
17436 return;
17437 end if;
17438
17439 -- Each expression must resolve to an entity of the right kind or proper
17440 -- profile.
17441
17442 Assoc := First (Component_Associations (Expression (ASN)));
17443 while Present (Assoc) loop
17444 Expr := Expression (Assoc);
17445 Analyze (Expr);
17446
17447 Choice := First (Choices (Assoc));
17448
17449 Choice_Name := Chars (Choice);
17450
17451 if Nkind (Choice) /= N_Identifier or else Present (Next (Choice)) then
17452 Error_Msg_N ("illegal name in association", Choice);
17453
17454 elsif Choice_Name = Name_Address_Type then
17455 if Assoc /= First (Component_Associations (Expression (ASN))) then
17456 Error_Msg_N ("Address_Type must be first association", Choice);
17457 end if;
17458
17459 Check_And_Resolve_Storage_Model_Type_Argument
17460 (Expr, Typ, Address_Type_Id, Name_Address_Type);
17461
17462 else
17463 -- It's allowed to leave out the Address_Type argument, in which
17464 -- case the address type is defined to default to System.Address.
17465
17466 if No (Address_Type_Id) then
17467 Address_Type_Id := RTE (RE_Address);
17468 end if;
17469
17470 if Choice_Name = Name_Null_Address then
17471 Check_And_Resolve_Storage_Model_Type_Argument
17472 (Expr, Typ, Null_Address_Id, Name_Null_Address);
17473
17474 elsif Choice_Name = Name_Allocate then
17475 Check_And_Resolve_Storage_Model_Type_Argument
17476 (Expr, Typ, Allocate_Id, Name_Allocate);
17477
17478 elsif Choice_Name = Name_Deallocate then
17479 Check_And_Resolve_Storage_Model_Type_Argument
17480 (Expr, Typ, Deallocate_Id, Name_Deallocate);
17481
17482 elsif Choice_Name = Name_Copy_From then
17483 Check_And_Resolve_Storage_Model_Type_Argument
17484 (Expr, Typ, Copy_From_Id, Name_Copy_From);
17485
17486 elsif Choice_Name = Name_Copy_To then
17487 Check_And_Resolve_Storage_Model_Type_Argument
17488 (Expr, Typ, Copy_To_Id, Name_Copy_To);
17489
17490 elsif Choice_Name = Name_Storage_Size then
17491 Check_And_Resolve_Storage_Model_Type_Argument
17492 (Expr, Typ, Storage_Size_Id, Name_Storage_Size);
17493
17494 else
17495 Error_Msg_N
17496 ("invalid name for Storage_Model_Type argument", Choice);
17497 end if;
17498 end if;
17499
17500 Next (Assoc);
17501 end loop;
17502
17503 -- If Address_Type has been specified as or defaults to System.Address,
17504 -- then other "subaspect" arguments can be specified, but are optional.
17505 -- Otherwise, all other arguments are required and an error is flagged
17506 -- about any that are missing.
17507
17508 if Address_Type_Id = RTE (RE_Address) then
17509 return;
17510
17511 elsif No (Null_Address_Id) then
17512 Error_Msg_N ("match for Null_Address primitive not found", ASN);
17513
17514 elsif No (Allocate_Id) then
17515 Error_Msg_N ("match for Allocate primitive not found", ASN);
17516
17517 elsif No (Deallocate_Id) then
17518 Error_Msg_N ("match for Deallocate primitive not found", ASN);
17519
17520 elsif No (Copy_From_Id) then
17521 Error_Msg_N ("match for Copy_From primitive not found", ASN);
17522
17523 elsif No (Copy_To_Id) then
17524 Error_Msg_N ("match for Copy_To primitive not found", ASN);
17525
17526 elsif No (Storage_Size_Id) then
17527 Error_Msg_N ("match for Storage_Size primitive not found", ASN);
17528 end if;
17529 end Validate_Storage_Model_Type_Aspect;
17530
17531 -----------------------------------
17532 -- Validate_Unchecked_Conversion --
17533 -----------------------------------
17534
17535 procedure Validate_Unchecked_Conversion
17536 (N : Node_Id;
17537 Act_Unit : Entity_Id)
17538 is
17539 Source : Entity_Id;
17540 Target : Entity_Id;
17541
17542 procedure Warn_Nonportable (RE : RE_Id);
17543 -- Warn if either source or target of the conversion is a predefined
17544 -- private type, whose representation might differ between releases and
17545 -- targets of the compiler.
17546
17547 ----------------------
17548 -- Warn_Nonportable --
17549 ----------------------
17550
17551 procedure Warn_Nonportable (RE : RE_Id) is
17552 begin
17553 if Is_RTE (Source, RE) or else Is_RTE (Target, RE) then
17554 pragma Assert (Is_Private_Type (RTE (RE)));
17555 Error_Msg_NE
17556 ("?z?representation of & values may change between "
17557 & "'G'N'A'T versions", N, RTE (RE));
17558 end if;
17559 end Warn_Nonportable;
17560
17561 -- Local variables
17562
17563 Vnode : Node_Id;
17564
17565 -- Start of processing for Validate_Unchecked_Conversion
17566
17567 begin
17568 -- Obtain source and target types. Note that we call Ancestor_Subtype
17569 -- here because the processing for generic instantiation always makes
17570 -- subtypes, and we want the original frozen actual types.
17571
17572 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
17573 Target := Ancestor_Subtype (Etype (Act_Unit));
17574
17575 -- If either type is generic, the instantiation happens within a generic
17576 -- unit, and there is nothing to check. The proper check will happen
17577 -- when the enclosing generic is instantiated.
17578
17579 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
17580 return;
17581 end if;
17582
17583 -- Warn if one of the operands is a private type declared in
17584 -- Ada.Calendar or Ada.Real_Time. Do not emit a warning when compiling
17585 -- GNAT-related sources.
17586
17587 if Warn_On_Unchecked_Conversion
17588 and then not In_Predefined_Unit (N)
17589 then
17590 Warn_Nonportable (RO_CA_Time);
17591 Warn_Nonportable (RO_RT_Time);
17592 Warn_Nonportable (RE_Time_Span);
17593 end if;
17594
17595 -- If we are dealing with private types, then do the check on their
17596 -- fully declared counterparts if the full declarations have been
17597 -- encountered (they don't have to be visible, but they must exist).
17598
17599 if Is_Private_Type (Source)
17600 and then Present (Underlying_Type (Source))
17601 then
17602 Source := Underlying_Type (Source);
17603 end if;
17604
17605 if Is_Private_Type (Target)
17606 and then Present (Underlying_Type (Target))
17607 then
17608 Target := Underlying_Type (Target);
17609 end if;
17610
17611 -- Source may be unconstrained array, but not target, except in relaxed
17612 -- semantics mode.
17613
17614 if Is_Array_Type (Target)
17615 and then not Is_Constrained (Target)
17616 and then not Relaxed_RM_Semantics
17617 then
17618 Error_Msg_N
17619 ("unchecked conversion to unconstrained array not allowed", N);
17620 return;
17621 end if;
17622
17623 -- Warn if conversion between two different convention pointers
17624
17625 if Is_Access_Type (Target)
17626 and then Is_Access_Type (Source)
17627 and then Convention (Target) /= Convention (Source)
17628 and then Warn_On_Unchecked_Conversion
17629 then
17630 -- Give warnings for subprogram pointers only on most targets
17631
17632 if Is_Access_Subprogram_Type (Target)
17633 or else Is_Access_Subprogram_Type (Source)
17634 then
17635 Error_Msg_N
17636 ("?z?conversion between pointers with different conventions!",
17637 N);
17638 end if;
17639 end if;
17640
17641 -- Make entry in unchecked conversion table for later processing by
17642 -- Validate_Unchecked_Conversions, which will check sizes and alignments
17643 -- (using values set by the back end where possible). This is only done
17644 -- if the appropriate warning is active.
17645
17646 if Warn_On_Unchecked_Conversion then
17647 Unchecked_Conversions.Append
17648 (New_Val => UC_Entry'(Eloc => Sloc (N),
17649 Source => Source,
17650 Target => Target,
17651 Act_Unit => Act_Unit));
17652
17653 -- If both sizes are known statically now, then back-end annotation
17654 -- is not required to do a proper check but if either size is not
17655 -- known statically, then we need the annotation.
17656
17657 if Known_Static_RM_Size (Source)
17658 and then
17659 Known_Static_RM_Size (Target)
17660 then
17661 null;
17662 else
17663 Back_Annotate_Rep_Info := True;
17664 end if;
17665 end if;
17666
17667 -- If unchecked conversion to access type, and access type is declared
17668 -- in the same unit as the unchecked conversion, then set the flag
17669 -- No_Strict_Aliasing (no strict aliasing is implicit here)
17670
17671 if Is_Access_Type (Target)
17672 and then In_Same_Source_Unit (Target, N)
17673 then
17674 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
17675 end if;
17676
17677 -- If the unchecked conversion is between Address and an access
17678 -- subprogram type, show that we shouldn't use an internal
17679 -- representation for the access subprogram type.
17680
17681 if Is_Access_Subprogram_Type (Target)
17682 and then Is_Descendant_Of_Address (Source)
17683 and then In_Same_Source_Unit (Target, N)
17684 then
17685 Set_Can_Use_Internal_Rep (Target, False);
17686 elsif Is_Access_Subprogram_Type (Source)
17687 and then Is_Descendant_Of_Address (Target)
17688 and then In_Same_Source_Unit (Source, N)
17689 then
17690 Set_Can_Use_Internal_Rep (Source, False);
17691 end if;
17692
17693 -- Generate N_Validate_Unchecked_Conversion node for back end in case
17694 -- the back end needs to perform special validation checks.
17695
17696 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
17697 -- have full expansion and the back end is called ???
17698
17699 Vnode :=
17700 Make_Validate_Unchecked_Conversion (Sloc (N));
17701 Set_Source_Type (Vnode, Source);
17702 Set_Target_Type (Vnode, Target);
17703
17704 -- If the unchecked conversion node is in a list, just insert before it.
17705 -- If not we have some strange case, not worth bothering about.
17706
17707 if Is_List_Member (N) then
17708 Insert_After (N, Vnode);
17709 end if;
17710 end Validate_Unchecked_Conversion;
17711
17712 ------------------------------------
17713 -- Validate_Unchecked_Conversions --
17714 ------------------------------------
17715
17716 procedure Validate_Unchecked_Conversions is
17717 function Is_Null_Array (T : Entity_Id) return Boolean;
17718 -- We want to warn in the case of converting to a wrong-sized array of
17719 -- bytes, including the zero-size case. This returns True in that case,
17720 -- which is necessary because a size of 0 is used to indicate both an
17721 -- unknown size and a size of 0. It's OK for this to return True in
17722 -- other zero-size cases, but we don't go out of our way; for example,
17723 -- we don't bother with multidimensional arrays.
17724
17725 function Is_Null_Array (T : Entity_Id) return Boolean is
17726 begin
17727 if Is_Array_Type (T) and then Is_Constrained (T) then
17728 declare
17729 Index : constant Node_Id := First_Index (T);
17730 R : Node_Id; -- N_Range
17731 begin
17732 case Nkind (Index) is
17733 when N_Range =>
17734 R := Index;
17735 when N_Subtype_Indication =>
17736 R := Range_Expression (Constraint (Index));
17737 when N_Identifier | N_Expanded_Name =>
17738 R := Scalar_Range (Entity (Index));
17739 when others =>
17740 raise Program_Error;
17741 end case;
17742
17743 return Is_Null_Range (Low_Bound (R), High_Bound (R));
17744 end;
17745 end if;
17746
17747 return False;
17748 end Is_Null_Array;
17749
17750 begin
17751 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
17752 declare
17753 T : UC_Entry renames Unchecked_Conversions.Table (N);
17754
17755 Act_Unit : constant Entity_Id := T.Act_Unit;
17756 Eloc : constant Source_Ptr := T.Eloc;
17757 Source : constant Entity_Id := T.Source;
17758 Target : constant Entity_Id := T.Target;
17759
17760 Source_Siz : Uint;
17761 Target_Siz : Uint;
17762
17763 begin
17764 -- Skip if function marked as warnings off
17765
17766 if Has_Warnings_Off (Act_Unit)
17767 or else Serious_Errors_Detected > 0
17768 then
17769 goto Continue;
17770 end if;
17771
17772 -- Don't do the check if warnings off for either type, note the
17773 -- deliberate use of OR here instead of OR ELSE to get the flag
17774 -- Warnings_Off_Used set for both types if appropriate.
17775
17776 if Has_Warnings_Off (Source) or Has_Warnings_Off (Target) then
17777 goto Continue;
17778 end if;
17779
17780 if (Known_Static_RM_Size (Source)
17781 and then Known_Static_RM_Size (Target))
17782 or else Is_Null_Array (Target)
17783 then
17784 -- This validation check, which warns if we have unequal sizes
17785 -- for unchecked conversion, and thus implementation dependent
17786 -- semantics, is one of the few occasions on which we use the
17787 -- official RM size instead of Esize. See description in Einfo
17788 -- "Handling of Type'Size Values" for details.
17789
17790 Source_Siz := RM_Size (Source);
17791 Target_Siz := RM_Size (Target);
17792
17793 if Present (Source_Siz) and then Present (Target_Siz)
17794 and then Source_Siz /= Target_Siz
17795 then
17796 Error_Msg
17797 ("?z?types for unchecked conversion have different sizes!",
17798 Eloc, Act_Unit);
17799
17800 if All_Errors_Mode then
17801 Error_Msg_Name_1 := Chars (Source);
17802 Error_Msg_Uint_1 := Source_Siz;
17803 Error_Msg_Name_2 := Chars (Target);
17804 Error_Msg_Uint_2 := Target_Siz;
17805 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
17806
17807 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
17808
17809 if Is_Discrete_Type (Source)
17810 and then
17811 Is_Discrete_Type (Target)
17812 then
17813 if Source_Siz > Target_Siz then
17814 Error_Msg
17815 ("\?z?^ high order bits of source will "
17816 & "be ignored!", Eloc);
17817
17818 elsif Is_Unsigned_Type (Source) then
17819 Error_Msg
17820 ("\?z?source will be extended with ^ high order "
17821 & "zero bits!", Eloc);
17822
17823 else
17824 Error_Msg
17825 ("\?z?source will be extended with ^ high order "
17826 & "sign bits!", Eloc);
17827 end if;
17828
17829 elsif Source_Siz < Target_Siz then
17830 if Is_Discrete_Type (Target) then
17831 if Bytes_Big_Endian then
17832 Error_Msg
17833 ("\?z?target value will include ^ undefined "
17834 & "low order bits!", Eloc, Act_Unit);
17835 else
17836 Error_Msg
17837 ("\?z?target value will include ^ undefined "
17838 & "high order bits!", Eloc, Act_Unit);
17839 end if;
17840
17841 else
17842 Error_Msg
17843 ("\?z?^ trailing bits of target value will be "
17844 & "undefined!", Eloc, Act_Unit);
17845 end if;
17846
17847 else pragma Assert (Source_Siz > Target_Siz);
17848 if Is_Discrete_Type (Source) then
17849 if Bytes_Big_Endian then
17850 Error_Msg
17851 ("\?z?^ low order bits of source will be "
17852 & "ignored!", Eloc, Act_Unit);
17853 else
17854 Error_Msg
17855 ("\?z?^ high order bits of source will be "
17856 & "ignored!", Eloc, Act_Unit);
17857 end if;
17858
17859 else
17860 Error_Msg
17861 ("\?z?^ trailing bits of source will be "
17862 & "ignored!", Eloc, Act_Unit);
17863 end if;
17864 end if;
17865 end if;
17866 end if;
17867 end if;
17868
17869 -- If both types are access types, we need to check the alignment.
17870 -- If the alignment of both is specified, we can do it here.
17871
17872 if Serious_Errors_Detected = 0
17873 and then Is_Access_Type (Source)
17874 and then Is_Access_Type (Target)
17875 and then Target_Strict_Alignment
17876 and then Present (Designated_Type (Source))
17877 and then Present (Designated_Type (Target))
17878 then
17879 declare
17880 D_Source : constant Entity_Id := Designated_Type (Source);
17881 D_Target : constant Entity_Id := Designated_Type (Target);
17882
17883 begin
17884 if Known_Alignment (D_Source)
17885 and then
17886 Known_Alignment (D_Target)
17887 then
17888 declare
17889 Source_Align : constant Uint := Alignment (D_Source);
17890 Target_Align : constant Uint := Alignment (D_Target);
17891
17892 begin
17893 if Source_Align < Target_Align
17894 and then not Is_Tagged_Type (D_Source)
17895
17896 -- Suppress warning if warnings suppressed on either
17897 -- type or either designated type. Note the use of
17898 -- OR here instead of OR ELSE. That is intentional,
17899 -- we would like to set flag Warnings_Off_Used in
17900 -- all types for which warnings are suppressed.
17901
17902 and then not (Has_Warnings_Off (D_Source)
17903 or
17904 Has_Warnings_Off (D_Target)
17905 or
17906 Has_Warnings_Off (Source)
17907 or
17908 Has_Warnings_Off (Target))
17909 then
17910 Error_Msg_Uint_1 := Target_Align;
17911 Error_Msg_Uint_2 := Source_Align;
17912 Error_Msg_Node_1 := D_Target;
17913 Error_Msg_Node_2 := D_Source;
17914 Error_Msg
17915 ("?z?alignment of & (^) is stricter than "
17916 & "alignment of & (^)!", Eloc, Act_Unit);
17917 Error_Msg
17918 ("\?z?resulting access value may have invalid "
17919 & "alignment!", Eloc, Act_Unit);
17920 end if;
17921 end;
17922 end if;
17923 end;
17924 end if;
17925 end;
17926
17927 <<Continue>>
17928 null;
17929 end loop;
17930 end Validate_Unchecked_Conversions;
17931
17932 end Sem_Ch13;
This page took 0.836682 seconds and 5 git commands to generate.