[Ada] AI12-0394 Named Numbers and User-Defined Numeric Literals
[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-2020, 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 Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Par_SCO; use Par_SCO;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Case; use Sem_Case;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Res; use Sem_Res;
61 with Sem_Type; use Sem_Type;
62 with Sem_Util; use Sem_Util;
63 with Sem_Warn; use Sem_Warn;
64 with Sinfo; use Sinfo;
65 with Sinput; use Sinput;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with Targparm; use Targparm;
69 with Ttypes; use Ttypes;
70 with Tbuild; use Tbuild;
71 with Urealp; use Urealp;
72 with Warnsw; use Warnsw;
73
74 with GNAT.Heap_Sort_G;
75
76 package body Sem_Ch13 is
77
78 SSU : constant Pos := System_Storage_Unit;
79 -- Convenient short hand for commonly used constant
80
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
84
85 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id);
86 -- Helper routine providing the original (pre-AI95-0133) behavior for
87 -- Adjust_Record_For_Reverse_Bit_Order.
88
89 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
90 -- This routine is called after setting one of the sizes of type entity
91 -- Typ to Size. The purpose is to deal with the situation of a derived
92 -- type whose inherited alignment is no longer appropriate for the new
93 -- size value. In this case, we reset the Alignment to unknown.
94
95 function All_Static_Choices (L : List_Id) return Boolean;
96 -- Returns true if all elements of the list are OK static choices
97 -- as defined below for Is_Static_Choice. Used for case expression
98 -- alternatives and for the right operand of a membership test. An
99 -- others_choice is static if the corresponding expression is static.
100 -- The staticness of the bounds is checked separately.
101
102 procedure Build_Discrete_Static_Predicate
103 (Typ : Entity_Id;
104 Expr : Node_Id;
105 Nam : Name_Id);
106 -- Given a predicated type Typ, where Typ is a discrete static subtype,
107 -- whose predicate expression is Expr, tests if Expr is a static predicate,
108 -- and if so, builds the predicate range list. Nam is the name of the one
109 -- argument to the predicate function. Occurrences of the type name in the
110 -- predicate expression have been replaced by identifier references to this
111 -- name, which is unique, so any identifier with Chars matching Nam must be
112 -- a reference to the type. If the predicate is non-static, this procedure
113 -- returns doing nothing. If the predicate is static, then the predicate
114 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
115 -- rewritten as a canonicalized membership operation.
116
117 function Build_Export_Import_Pragma
118 (Asp : Node_Id;
119 Id : Entity_Id) return Node_Id;
120 -- Create the corresponding pragma for aspect Export or Import denoted by
121 -- Asp. Id is the related entity subject to the aspect. Return Empty when
122 -- the expression of aspect Asp evaluates to False or is erroneous.
123
124 function Build_Predicate_Function_Declaration
125 (Typ : Entity_Id) return Node_Id;
126 -- Build the declaration for a predicate function. The declaration is built
127 -- at the end of the declarative part containing the type definition, which
128 -- may be before the freeze point of the type. The predicate expression is
129 -- preanalyzed at this point, to catch visibility errors.
130
131 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
132 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
133 -- then either there are pragma Predicate entries on the rep chain for the
134 -- type (note that Predicate aspects are converted to pragma Predicate), or
135 -- there are inherited aspects from a parent type, or ancestor subtypes.
136 -- This procedure builds body for the Predicate function that tests these
137 -- predicates. N is the freeze node for the type. The spec of the function
138 -- is inserted before the freeze node, and the body of the function is
139 -- inserted after the freeze node. If the predicate expression has a least
140 -- one Raise_Expression, then this procedure also builds the M version of
141 -- the predicate function for use in membership tests.
142
143 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
144 -- Called if both Storage_Pool and Storage_Size attribute definition
145 -- clauses (SP and SS) are present for entity Ent. Issue error message.
146
147 procedure Freeze_Entity_Checks (N : Node_Id);
148 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
149 -- to generate appropriate semantic checks that are delayed until this
150 -- point (they had to be delayed this long for cases of delayed aspects,
151 -- e.g. analysis of statically predicated subtypes in choices, for which
152 -- we have to be sure the subtypes in question are frozen before checking).
153
154 function Get_Alignment_Value (Expr : Node_Id) return Uint;
155 -- Given the expression for an alignment value, returns the corresponding
156 -- Uint value. If the value is inappropriate, then error messages are
157 -- posted as required, and a value of No_Uint is returned.
158
159 function Is_Operational_Item (N : Node_Id) return Boolean;
160 -- A specification for a stream attribute is allowed before the full type
161 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
162 -- that do not specify a representation characteristic are operational
163 -- attributes.
164
165 function Is_Static_Choice (N : Node_Id) return Boolean;
166 -- Returns True if N represents a static choice (static subtype, or
167 -- static subtype indication, or static expression, or static range).
168 --
169 -- Note that this is a bit more inclusive than we actually need
170 -- (in particular membership tests do not allow the use of subtype
171 -- indications). But that doesn't matter, we have already checked
172 -- that the construct is legal to get this far.
173
174 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean;
175 -- Returns True for a representation clause/pragma that specifies a
176 -- type-related representation (as opposed to operational) aspect.
177
178 function Is_Predicate_Static
179 (Expr : Node_Id;
180 Nam : Name_Id) return Boolean;
181 -- Given predicate expression Expr, tests if Expr is predicate-static in
182 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
183 -- name in the predicate expression have been replaced by references to
184 -- an identifier whose Chars field is Nam. This name is unique, so any
185 -- identifier with Chars matching Nam must be a reference to the type.
186 -- Returns True if the expression is predicate-static and False otherwise,
187 -- but is not in the business of setting flags or issuing error messages.
188 --
189 -- Only scalar types can have static predicates, so False is always
190 -- returned for non-scalar types.
191 --
192 -- Note: the RM seems to suggest that string types can also have static
193 -- predicates. But that really makes lttle sense as very few useful
194 -- predicates can be constructed for strings. Remember that:
195 --
196 -- "ABC" < "DEF"
197 --
198 -- is not a static expression. So even though the clearly faulty RM wording
199 -- allows the following:
200 --
201 -- subtype S is String with Static_Predicate => S < "DEF"
202 --
203 -- We can't allow this, otherwise we have predicate-static applying to a
204 -- larger class than static expressions, which was never intended.
205
206 procedure New_Put_Image_Subprogram
207 (N : Node_Id;
208 Ent : Entity_Id;
209 Subp : Entity_Id);
210 -- Similar to New_Stream_Subprogram, but for the Put_Image attribute
211
212 procedure New_Stream_Subprogram
213 (N : Node_Id;
214 Ent : Entity_Id;
215 Subp : Entity_Id;
216 Nam : TSS_Name_Type);
217 -- Create a subprogram renaming of a given stream attribute to the
218 -- designated subprogram and then in the tagged case, provide this as a
219 -- primitive operation, or in the untagged case make an appropriate TSS
220 -- entry. This is more properly an expansion activity than just semantics,
221 -- but the presence of user-defined stream functions for limited types
222 -- is a legality check, which is why this takes place here rather than in
223 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
224 -- function to be generated.
225 --
226 -- To avoid elaboration anomalies with freeze nodes, for untagged types
227 -- we generate both a subprogram declaration and a subprogram renaming
228 -- declaration, so that the attribute specification is handled as a
229 -- renaming_as_body. For tagged types, the specification is one of the
230 -- primitive specs.
231
232 procedure No_Type_Rep_Item (N : Node_Id);
233 -- Output message indicating that no type-related aspects can be
234 -- specified due to some property of the parent type.
235
236 procedure Register_Address_Clause_Check
237 (N : Node_Id;
238 X : Entity_Id;
239 A : Uint;
240 Y : Entity_Id;
241 Off : Boolean);
242 -- Register a check for the address clause N. The rest of the parameters
243 -- are in keeping with the components of Address_Clause_Check_Record below.
244
245 procedure Validate_Aspect_Aggregate (N : Node_Id);
246 -- Check legality of operations given in the Ada 202x Aggregate aspect for
247 -- containers.
248
249 procedure Resolve_Aspect_Aggregate
250 (Typ : Entity_Id;
251 Expr : Node_Id);
252 -- Resolve each one of the operations specified in the specification of
253 -- Aspect_Aggregate.
254
255 procedure Resolve_Iterable_Operation
256 (N : Node_Id;
257 Cursor : Entity_Id;
258 Typ : Entity_Id;
259 Nam : Name_Id);
260 -- If the name of a primitive operation for an Iterable aspect is
261 -- overloaded, resolve according to required signature.
262
263 procedure Set_Biased
264 (E : Entity_Id;
265 N : Node_Id;
266 Msg : String;
267 Biased : Boolean := True);
268 -- If Biased is True, sets Has_Biased_Representation flag for E, and
269 -- outputs a warning message at node N if Warn_On_Biased_Representation is
270 -- is True. This warning inserts the string Msg to describe the construct
271 -- causing biasing.
272
273 -----------------------------------------------------------
274 -- Visibility of Discriminants in Aspect Specifications --
275 -----------------------------------------------------------
276
277 -- The discriminants of a type are visible when analyzing the aspect
278 -- specifications of a type declaration or protected type declaration,
279 -- but not when analyzing those of a subtype declaration. The following
280 -- routines enforce this distinction.
281
282 procedure Push_Type (E : Entity_Id);
283 -- Push scope E and make visible the discriminants of type entity E if E
284 -- has discriminants and is not a subtype.
285
286 procedure Pop_Type (E : Entity_Id);
287 -- Remove visibility to the discriminants of type entity E and pop the
288 -- scope stack if E has discriminants and is not a subtype.
289
290 ----------------------------------------------
291 -- Table for Validate_Unchecked_Conversions --
292 ----------------------------------------------
293
294 -- The following table collects unchecked conversions for validation.
295 -- Entries are made by Validate_Unchecked_Conversion and then the call
296 -- to Validate_Unchecked_Conversions does the actual error checking and
297 -- posting of warnings. The reason for this delayed processing is to take
298 -- advantage of back-annotations of size and alignment values performed by
299 -- the back end.
300
301 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
302 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
303 -- already have modified all Sloc values if the -gnatD option is set.
304
305 type UC_Entry is record
306 Eloc : Source_Ptr; -- node used for posting warnings
307 Source : Entity_Id; -- source type for unchecked conversion
308 Target : Entity_Id; -- target type for unchecked conversion
309 Act_Unit : Entity_Id; -- actual function instantiated
310 end record;
311
312 package Unchecked_Conversions is new Table.Table (
313 Table_Component_Type => UC_Entry,
314 Table_Index_Type => Int,
315 Table_Low_Bound => 1,
316 Table_Initial => 50,
317 Table_Increment => 200,
318 Table_Name => "Unchecked_Conversions");
319
320 ----------------------------------------
321 -- Table for Validate_Address_Clauses --
322 ----------------------------------------
323
324 -- If an address clause has the form
325
326 -- for X'Address use Expr
327
328 -- where Expr has a value known at compile time or is of the form Y'Address
329 -- or recursively is a reference to a constant initialized with either of
330 -- these forms, and the value of Expr is not a multiple of X's alignment,
331 -- or if Y has a smaller alignment than X, then that merits a warning about
332 -- possible bad alignment. The following table collects address clauses of
333 -- this kind. We put these in a table so that they can be checked after the
334 -- back end has completed annotation of the alignments of objects, since we
335 -- can catch more cases that way.
336
337 type Address_Clause_Check_Record is record
338 N : Node_Id;
339 -- The address clause
340
341 X : Entity_Id;
342 -- The entity of the object subject to the address clause
343
344 A : Uint;
345 -- The value of the address in the first case
346
347 Y : Entity_Id;
348 -- The entity of the object being overlaid in the second case
349
350 Off : Boolean;
351 -- Whether the address is offset within Y in the second case
352
353 Alignment_Checks_Suppressed : Boolean;
354 -- Whether alignment checks are suppressed by an active scope suppress
355 -- setting. We need to save the value in order to be able to reuse it
356 -- after the back end has been run.
357 end record;
358
359 package Address_Clause_Checks is new Table.Table (
360 Table_Component_Type => Address_Clause_Check_Record,
361 Table_Index_Type => Int,
362 Table_Low_Bound => 1,
363 Table_Initial => 20,
364 Table_Increment => 200,
365 Table_Name => "Address_Clause_Checks");
366
367 function Alignment_Checks_Suppressed
368 (ACCR : Address_Clause_Check_Record) return Boolean;
369 -- Return whether the alignment check generated for the address clause
370 -- is suppressed.
371
372 ---------------------------------
373 -- Alignment_Checks_Suppressed --
374 ---------------------------------
375
376 function Alignment_Checks_Suppressed
377 (ACCR : Address_Clause_Check_Record) return Boolean
378 is
379 begin
380 if Checks_May_Be_Suppressed (ACCR.X) then
381 return Is_Check_Suppressed (ACCR.X, Alignment_Check);
382 else
383 return ACCR.Alignment_Checks_Suppressed;
384 end if;
385 end Alignment_Checks_Suppressed;
386
387 -----------------------------------------
388 -- Adjust_Record_For_Reverse_Bit_Order --
389 -----------------------------------------
390
391 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
392 Max_Machine_Scalar_Size : constant Uint :=
393 UI_From_Int (System_Max_Integer_Size);
394 -- We use this as the maximum machine scalar size
395
396 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
397
398 CC : Node_Id;
399 Comp : Node_Id;
400 Num_CC : Natural;
401
402 begin
403 -- The processing done here used to depend on the Ada version, but the
404 -- behavior has been changed by AI95-0133. However this AI is a Binding
405 -- Interpretation, so we now implement it even in Ada 95 mode. But the
406 -- original behavior from unamended Ada 95 is available for the sake of
407 -- compatibility under the debugging switch -gnatd.p in Ada 95 mode.
408
409 if Ada_Version < Ada_2005 and then Debug_Flag_Dot_P then
410 Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R);
411 return;
412 end if;
413
414 -- For Ada 2005, we do machine scalar processing, as fully described In
415 -- AI-133. This involves gathering all components which start at the
416 -- same byte offset and processing them together. Same approach is still
417 -- valid in later versions including Ada 2012.
418
419 -- Note that component clauses found on record types may be inherited,
420 -- in which case the layout of the component with such a clause still
421 -- has to be done at this point. Therefore, the processing done here
422 -- must exclusively rely on the Component_Clause of the component.
423
424 -- This first loop through components does two things. First it deals
425 -- with the case of components with component clauses whose length is
426 -- greater than the maximum machine scalar size (either accepting them
427 -- or rejecting as needed). Second, it counts the number of components
428 -- with component clauses whose length does not exceed this maximum for
429 -- later processing.
430
431 Num_CC := 0;
432 Comp := First_Component_Or_Discriminant (R);
433 while Present (Comp) loop
434 CC := Component_Clause (Comp);
435
436 if Present (CC) then
437 declare
438 Fbit : constant Uint := Static_Integer (First_Bit (CC));
439 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
440
441 begin
442 -- Case of component with last bit >= max machine scalar
443
444 if Lbit >= Max_Machine_Scalar_Size then
445
446 -- This is allowed only if first bit is zero, and last bit
447 -- + 1 is a multiple of storage unit size.
448
449 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
450
451 -- This is the case to give a warning if enabled
452
453 if Warn_On_Reverse_Bit_Order then
454 Error_Msg_N
455 ("info: multi-byte field specified with "
456 & "non-standard Bit_Order?V?", CC);
457
458 if Bytes_Big_Endian then
459 Error_Msg_N
460 ("\bytes are not reversed "
461 & "(component is big-endian)?V?", CC);
462 else
463 Error_Msg_N
464 ("\bytes are not reversed "
465 & "(component is little-endian)?V?", CC);
466 end if;
467 end if;
468
469 -- Give error message for RM 13.5.1(10) violation
470
471 else
472 Error_Msg_FE
473 ("machine scalar rules not followed for&",
474 First_Bit (CC), Comp);
475
476 Error_Msg_Uint_1 := Lbit + 1;
477 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
478 Error_Msg_F
479 ("\last bit + 1 (^) exceeds maximum machine scalar "
480 & "size (^)", First_Bit (CC));
481
482 if (Lbit + 1) mod SSU /= 0 then
483 Error_Msg_Uint_1 := SSU;
484 Error_Msg_F
485 ("\and is not a multiple of Storage_Unit (^) "
486 & "(RM 13.5.1(10))", First_Bit (CC));
487
488 else
489 Error_Msg_Uint_1 := Fbit;
490 Error_Msg_F
491 ("\and first bit (^) is non-zero "
492 & "(RM 13.4.1(10))", First_Bit (CC));
493 end if;
494 end if;
495
496 -- OK case of machine scalar related component clause. For now,
497 -- just count them.
498
499 else
500 Num_CC := Num_CC + 1;
501 end if;
502 end;
503 end if;
504
505 Next_Component_Or_Discriminant (Comp);
506 end loop;
507
508 -- We need to sort the component clauses on the basis of the Position
509 -- values in the clause, so we can group clauses with the same Position
510 -- together to determine the relevant machine scalar size.
511
512 Sort_CC : declare
513 Comps : array (0 .. Num_CC) of Entity_Id;
514 -- Array to collect component and discriminant entities. The data
515 -- starts at index 1, the 0'th entry is for the sort routine.
516
517 function CP_Lt (Op1, Op2 : Natural) return Boolean;
518 -- Compare routine for Sort
519
520 procedure CP_Move (From : Natural; To : Natural);
521 -- Move routine for Sort
522
523 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
524
525 MaxL : Uint;
526 -- Maximum last bit value of any component in this set
527
528 MSS : Uint;
529 -- Corresponding machine scalar size
530
531 Start : Natural;
532 Stop : Natural;
533 -- Start and stop positions in the component list of the set of
534 -- components with the same starting position (that constitute
535 -- components in a single machine scalar).
536
537 -----------
538 -- CP_Lt --
539 -----------
540
541 function CP_Lt (Op1, Op2 : Natural) return Boolean is
542 begin
543 return
544 Position (Component_Clause (Comps (Op1))) <
545 Position (Component_Clause (Comps (Op2)));
546 end CP_Lt;
547
548 -------------
549 -- CP_Move --
550 -------------
551
552 procedure CP_Move (From : Natural; To : Natural) is
553 begin
554 Comps (To) := Comps (From);
555 end CP_Move;
556
557 -- Start of processing for Sort_CC
558
559 begin
560 -- Collect the machine scalar relevant component clauses
561
562 Num_CC := 0;
563 Comp := First_Component_Or_Discriminant (R);
564 while Present (Comp) loop
565 declare
566 CC : constant Node_Id := Component_Clause (Comp);
567
568 begin
569 -- Collect only component clauses whose last bit is less than
570 -- machine scalar size. Any component clause whose last bit
571 -- exceeds this value does not take part in machine scalar
572 -- layout considerations. The test for Error_Posted makes sure
573 -- we exclude component clauses for which we already posted an
574 -- error.
575
576 if Present (CC)
577 and then not Error_Posted (Last_Bit (CC))
578 and then Static_Integer (Last_Bit (CC)) <
579 Max_Machine_Scalar_Size
580 then
581 Num_CC := Num_CC + 1;
582 Comps (Num_CC) := Comp;
583 end if;
584 end;
585
586 Next_Component_Or_Discriminant (Comp);
587 end loop;
588
589 -- Sort by ascending position number
590
591 Sorting.Sort (Num_CC);
592
593 -- We now have all the components whose size does not exceed the max
594 -- machine scalar value, sorted by starting position. In this loop we
595 -- gather groups of clauses starting at the same position, to process
596 -- them in accordance with AI-133.
597
598 Stop := 0;
599 while Stop < Num_CC loop
600 Start := Stop + 1;
601 Stop := Start;
602 MaxL :=
603 Static_Integer
604 (Last_Bit (Component_Clause (Comps (Start))));
605 while Stop < Num_CC loop
606 if Static_Integer
607 (Position (Component_Clause (Comps (Stop + 1)))) =
608 Static_Integer
609 (Position (Component_Clause (Comps (Stop))))
610 then
611 Stop := Stop + 1;
612 MaxL :=
613 UI_Max
614 (MaxL,
615 Static_Integer
616 (Last_Bit
617 (Component_Clause (Comps (Stop)))));
618 else
619 exit;
620 end if;
621 end loop;
622
623 -- Now we have a group of component clauses from Start to Stop
624 -- whose positions are identical, and MaxL is the maximum last
625 -- bit value of any of these components.
626
627 -- We need to determine the corresponding machine scalar size.
628 -- This loop assumes that machine scalar sizes are even, and that
629 -- each possible machine scalar has twice as many bits as the next
630 -- smaller one.
631
632 MSS := Max_Machine_Scalar_Size;
633 while MSS mod 2 = 0
634 and then (MSS / 2) >= SSU
635 and then (MSS / 2) > MaxL
636 loop
637 MSS := MSS / 2;
638 end loop;
639
640 -- Here is where we fix up the Component_Bit_Offset value to
641 -- account for the reverse bit order. Some examples of what needs
642 -- to be done for the case of a machine scalar size of 8 are:
643
644 -- First_Bit .. Last_Bit Component_Bit_Offset
645 -- old new old new
646
647 -- 0 .. 0 7 .. 7 0 7
648 -- 0 .. 1 6 .. 7 0 6
649 -- 0 .. 2 5 .. 7 0 5
650 -- 0 .. 7 0 .. 7 0 4
651
652 -- 1 .. 1 6 .. 6 1 6
653 -- 1 .. 4 3 .. 6 1 3
654 -- 4 .. 7 0 .. 3 4 0
655
656 -- The rule is that the first bit is obtained by subtracting the
657 -- old ending bit from machine scalar size - 1.
658
659 for C in Start .. Stop loop
660 declare
661 Comp : constant Entity_Id := Comps (C);
662 CC : constant Node_Id := Component_Clause (Comp);
663
664 FB : constant Uint := Static_Integer (First_Bit (CC));
665 LB : constant Uint := Static_Integer (Last_Bit (CC));
666 NFB : constant Uint := MSS - 1 - LB;
667 NLB : constant Uint := NFB + LB - FB;
668 Pos : constant Uint := Static_Integer (Position (CC));
669
670 begin
671 -- Do not warn for the artificial clause built for the tag
672 -- in Check_Record_Representation_Clause if it is inherited.
673
674 if Warn_On_Reverse_Bit_Order
675 and then Chars (Comp) /= Name_uTag
676 then
677 Error_Msg_Uint_1 := MSS;
678 Error_Msg_N
679 ("info: reverse bit order in machine scalar of "
680 & "length^?V?", First_Bit (CC));
681 Error_Msg_Uint_1 := NFB;
682 Error_Msg_Uint_2 := NLB;
683
684 if Bytes_Big_Endian then
685 Error_Msg_NE
686 ("\big-endian range for component & is ^ .. ^?V?",
687 First_Bit (CC), Comp);
688 else
689 Error_Msg_NE
690 ("\little-endian range for component & is ^ .. ^?V?",
691 First_Bit (CC), Comp);
692 end if;
693 end if;
694
695 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
696 Set_Esize (Comp, 1 + (NLB - NFB));
697 Set_Normalized_First_Bit (Comp, NFB mod SSU);
698 Set_Normalized_Position (Comp, Pos + NFB / SSU);
699 end;
700 end loop;
701 end loop;
702 end Sort_CC;
703 end Adjust_Record_For_Reverse_Bit_Order;
704
705 ------------------------------------------------
706 -- Adjust_Record_For_Reverse_Bit_Order_Ada_95 --
707 ------------------------------------------------
708
709 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id) is
710 CC : Node_Id;
711 Comp : Node_Id;
712
713 begin
714 -- For Ada 95, we just renumber bits within a storage unit. We do the
715 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
716 -- Ada 83, and are free to add this extension.
717
718 Comp := First_Component_Or_Discriminant (R);
719 while Present (Comp) loop
720 CC := Component_Clause (Comp);
721
722 -- If component clause is present, then deal with the non-default
723 -- bit order case for Ada 95 mode.
724
725 -- We only do this processing for the base type, and in fact that
726 -- is important, since otherwise if there are record subtypes, we
727 -- could reverse the bits once for each subtype, which is wrong.
728
729 if Present (CC) and then Ekind (R) = E_Record_Type then
730 declare
731 CFB : constant Uint := Component_Bit_Offset (Comp);
732 CSZ : constant Uint := Esize (Comp);
733 CLC : constant Node_Id := Component_Clause (Comp);
734 Pos : constant Node_Id := Position (CLC);
735 FB : constant Node_Id := First_Bit (CLC);
736
737 Storage_Unit_Offset : constant Uint :=
738 CFB / System_Storage_Unit;
739
740 Start_Bit : constant Uint :=
741 CFB mod System_Storage_Unit;
742
743 begin
744 -- Cases where field goes over storage unit boundary
745
746 if Start_Bit + CSZ > System_Storage_Unit then
747
748 -- Allow multi-byte field but generate warning
749
750 if Start_Bit mod System_Storage_Unit = 0
751 and then CSZ mod System_Storage_Unit = 0
752 then
753 Error_Msg_N
754 ("info: multi-byte field specified with non-standard "
755 & "Bit_Order?V?", CLC);
756
757 if Bytes_Big_Endian then
758 Error_Msg_N
759 ("\bytes are not reversed "
760 & "(component is big-endian)?V?", CLC);
761 else
762 Error_Msg_N
763 ("\bytes are not reversed "
764 & "(component is little-endian)?V?", CLC);
765 end if;
766
767 -- Do not allow non-contiguous field
768
769 else
770 Error_Msg_N
771 ("attempt to specify non-contiguous field not "
772 & "permitted", CLC);
773 Error_Msg_N
774 ("\caused by non-standard Bit_Order specified in "
775 & "legacy Ada 95 mode", CLC);
776 end if;
777
778 -- Case where field fits in one storage unit
779
780 else
781 -- Give warning if suspicious component clause
782
783 if Intval (FB) >= System_Storage_Unit
784 and then Warn_On_Reverse_Bit_Order
785 then
786 Error_Msg_N
787 ("info: Bit_Order clause does not affect byte "
788 & "ordering?V?", Pos);
789 Error_Msg_Uint_1 :=
790 Intval (Pos) + Intval (FB) /
791 System_Storage_Unit;
792 Error_Msg_N
793 ("info: position normalized to ^ before bit order "
794 & "interpreted?V?", Pos);
795 end if;
796
797 -- Here is where we fix up the Component_Bit_Offset value
798 -- to account for the reverse bit order. Some examples of
799 -- what needs to be done are:
800
801 -- First_Bit .. Last_Bit Component_Bit_Offset
802 -- old new old new
803
804 -- 0 .. 0 7 .. 7 0 7
805 -- 0 .. 1 6 .. 7 0 6
806 -- 0 .. 2 5 .. 7 0 5
807 -- 0 .. 7 0 .. 7 0 4
808
809 -- 1 .. 1 6 .. 6 1 6
810 -- 1 .. 4 3 .. 6 1 3
811 -- 4 .. 7 0 .. 3 4 0
812
813 -- The rule is that the first bit is obtained by subtracting
814 -- the old ending bit from storage_unit - 1.
815
816 Set_Component_Bit_Offset (Comp,
817 (Storage_Unit_Offset * System_Storage_Unit) +
818 (System_Storage_Unit - 1) -
819 (Start_Bit + CSZ - 1));
820
821 Set_Normalized_Position (Comp,
822 Component_Bit_Offset (Comp) / System_Storage_Unit);
823
824 Set_Normalized_First_Bit (Comp,
825 Component_Bit_Offset (Comp) mod System_Storage_Unit);
826 end if;
827 end;
828 end if;
829
830 Next_Component_Or_Discriminant (Comp);
831 end loop;
832 end Adjust_Record_For_Reverse_Bit_Order_Ada_95;
833
834 -------------------------------------
835 -- Alignment_Check_For_Size_Change --
836 -------------------------------------
837
838 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
839 begin
840 -- If the alignment is known, and not set by a rep clause, and is
841 -- inconsistent with the size being set, then reset it to unknown,
842 -- we assume in this case that the size overrides the inherited
843 -- alignment, and that the alignment must be recomputed.
844
845 if Known_Alignment (Typ)
846 and then not Has_Alignment_Clause (Typ)
847 and then Size mod (Alignment (Typ) * SSU) /= 0
848 then
849 Init_Alignment (Typ);
850 end if;
851 end Alignment_Check_For_Size_Change;
852
853 -----------------------------------
854 -- All_Membership_Choices_Static --
855 -----------------------------------
856
857 function All_Membership_Choices_Static (Expr : Node_Id) return Boolean is
858 pragma Assert (Nkind (Expr) in N_Membership_Test);
859 begin
860 pragma Assert
861 (Present (Right_Opnd (Expr))
862 xor
863 Present (Alternatives (Expr)));
864
865 if Present (Right_Opnd (Expr)) then
866 return Is_Static_Choice (Right_Opnd (Expr));
867 else
868 return All_Static_Choices (Alternatives (Expr));
869 end if;
870 end All_Membership_Choices_Static;
871
872 ------------------------
873 -- All_Static_Choices --
874 ------------------------
875
876 function All_Static_Choices (L : List_Id) return Boolean is
877 N : Node_Id;
878
879 begin
880 N := First (L);
881 while Present (N) loop
882 if not Is_Static_Choice (N) then
883 return False;
884 end if;
885
886 Next (N);
887 end loop;
888
889 return True;
890 end All_Static_Choices;
891
892 -------------------------------------
893 -- Analyze_Aspects_At_Freeze_Point --
894 -------------------------------------
895
896 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
897 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
898 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
899 -- the aspect specification node ASN.
900
901 procedure Check_Aspect_Too_Late (N : Node_Id);
902 -- This procedure is similar to Rep_Item_Too_Late for representation
903 -- aspects that apply to type and that do not have a corresponding
904 -- pragma.
905 -- Used to check in particular that the expression associated with
906 -- aspect node N for the given type (entity) of the aspect does not
907 -- appear too late according to the rules in RM 13.1(9) and 13.1(10).
908
909 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
910 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
911 -- a derived type can inherit aspects from its parent which have been
912 -- specified at the time of the derivation using an aspect, as in:
913 --
914 -- type A is range 1 .. 10
915 -- with Size => Not_Defined_Yet;
916 -- ..
917 -- type B is new A;
918 -- ..
919 -- Not_Defined_Yet : constant := 64;
920 --
921 -- In this example, the Size of A is considered to be specified prior
922 -- to the derivation, and thus inherited, even though the value is not
923 -- known at the time of derivation. To deal with this, we use two entity
924 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
925 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
926 -- the derived type (B here). If this flag is set when the derived type
927 -- is frozen, then this procedure is called to ensure proper inheritance
928 -- of all delayed aspects from the parent type. The derived type is E,
929 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
930 -- aspect specification node in the Rep_Item chain for the parent type.
931
932 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
933 -- Given an aspect specification node ASN whose expression is an
934 -- optional Boolean, this routines creates the corresponding pragma
935 -- at the freezing point.
936
937 ----------------------------------
938 -- Analyze_Aspect_Default_Value --
939 ----------------------------------
940
941 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
942 Ent : constant Entity_Id := Entity (ASN);
943 Expr : constant Node_Id := Expression (ASN);
944
945 begin
946 Set_Has_Default_Aspect (Base_Type (Ent));
947
948 if Is_Scalar_Type (Ent) then
949 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
950 else
951 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
952 end if;
953
954 Check_Aspect_Too_Late (ASN);
955 end Analyze_Aspect_Default_Value;
956
957 ---------------------------
958 -- Check_Aspect_Too_Late --
959 ---------------------------
960
961 procedure Check_Aspect_Too_Late (N : Node_Id) is
962 Typ : constant Entity_Id := Entity (N);
963 Expr : constant Node_Id := Expression (N);
964
965 function Find_Type_Reference
966 (Typ : Entity_Id; Expr : Node_Id) return Boolean;
967 -- Return True if a reference to type Typ is found in the expression
968 -- Expr.
969
970 -------------------------
971 -- Find_Type_Reference --
972 -------------------------
973
974 function Find_Type_Reference
975 (Typ : Entity_Id; Expr : Node_Id) return Boolean
976 is
977 function Find_Type (N : Node_Id) return Traverse_Result;
978 -- Set Found to True if N refers to Typ
979
980 ---------------
981 -- Find_Type --
982 ---------------
983
984 function Find_Type (N : Node_Id) return Traverse_Result is
985 begin
986 if N = Typ
987 or else (Nkind (N) in N_Identifier | N_Expanded_Name
988 and then Present (Entity (N))
989 and then Entity (N) = Typ)
990 then
991 return Abandon;
992 else
993 return OK;
994 end if;
995 end Find_Type;
996
997 function Search_Type_Reference is new Traverse_Func (Find_Type);
998
999 begin
1000 return Search_Type_Reference (Expr) = Abandon;
1001 end Find_Type_Reference;
1002
1003 Parent_Type : Entity_Id;
1004
1005 begin
1006 -- Ensure Expr is analyzed so that e.g. all types are properly
1007 -- resolved for Find_Type_Reference.
1008
1009 Analyze (Expr);
1010
1011 -- A self-referential aspect is illegal if it forces freezing the
1012 -- entity before the corresponding aspect has been analyzed.
1013
1014 if Find_Type_Reference (Typ, Expr) then
1015 Error_Msg_NE
1016 ("aspect specification causes premature freezing of&", N, Typ);
1017 end if;
1018
1019 -- For representation aspects, check for case of untagged derived
1020 -- type whose parent either has primitive operations (pre Ada 202x),
1021 -- or is a by-reference type (RM 13.1(10)).
1022 -- Strictly speaking the check also applies to Ada 2012 but it is
1023 -- really too constraining for existing code already, so relax it.
1024 -- ??? Confirming aspects should be allowed here.
1025
1026 if Is_Representation_Aspect (Get_Aspect_Id (N))
1027 and then Is_Derived_Type (Typ)
1028 and then not Is_Tagged_Type (Typ)
1029 then
1030 Parent_Type := Etype (Base_Type (Typ));
1031
1032 if Ada_Version <= Ada_2012
1033 and then Has_Primitive_Operations (Parent_Type)
1034 then
1035 Error_Msg_N
1036 ("|representation aspect not permitted before Ada 202x: " &
1037 "use -gnat2020!", N);
1038 Error_Msg_NE
1039 ("\parent type & has primitive operations!", N, Parent_Type);
1040
1041 elsif Is_By_Reference_Type (Parent_Type) then
1042 No_Type_Rep_Item (N);
1043 Error_Msg_NE
1044 ("\parent type & is a by-reference type!", N, Parent_Type);
1045 end if;
1046 end if;
1047 end Check_Aspect_Too_Late;
1048
1049 ---------------------------------
1050 -- Inherit_Delayed_Rep_Aspects --
1051 ---------------------------------
1052
1053 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
1054 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
1055 P : constant Entity_Id := Entity (ASN);
1056 -- Entity for parent type
1057
1058 N : Node_Id;
1059 -- Item from Rep_Item chain
1060
1061 A : Aspect_Id;
1062
1063 begin
1064 -- Loop through delayed aspects for the parent type
1065
1066 N := ASN;
1067 while Present (N) loop
1068 if Nkind (N) = N_Aspect_Specification then
1069 exit when Entity (N) /= P;
1070
1071 if Is_Delayed_Aspect (N) then
1072 A := Get_Aspect_Id (Chars (Identifier (N)));
1073
1074 -- Process delayed rep aspect. For Boolean attributes it is
1075 -- not possible to cancel an attribute once set (the attempt
1076 -- to use an aspect with xxx => False is an error) for a
1077 -- derived type. So for those cases, we do not have to check
1078 -- if a clause has been given for the derived type, since it
1079 -- is harmless to set it again if it is already set.
1080
1081 case A is
1082
1083 -- Alignment
1084
1085 when Aspect_Alignment =>
1086 if not Has_Alignment_Clause (E) then
1087 Set_Alignment (E, Alignment (P));
1088 end if;
1089
1090 -- Atomic
1091
1092 when Aspect_Atomic =>
1093 if Is_Atomic (P) then
1094 Set_Is_Atomic (E);
1095 end if;
1096
1097 -- Atomic_Components
1098
1099 when Aspect_Atomic_Components =>
1100 if Has_Atomic_Components (P) then
1101 Set_Has_Atomic_Components (Base_Type (E));
1102 end if;
1103
1104 -- Bit_Order
1105
1106 when Aspect_Bit_Order =>
1107 if Is_Record_Type (E)
1108 and then No (Get_Attribute_Definition_Clause
1109 (E, Attribute_Bit_Order))
1110 and then Reverse_Bit_Order (P)
1111 then
1112 Set_Reverse_Bit_Order (Base_Type (E));
1113 end if;
1114
1115 -- Component_Size
1116
1117 when Aspect_Component_Size =>
1118 if Is_Array_Type (E)
1119 and then not Has_Component_Size_Clause (E)
1120 then
1121 Set_Component_Size
1122 (Base_Type (E), Component_Size (P));
1123 end if;
1124
1125 -- Machine_Radix
1126
1127 when Aspect_Machine_Radix =>
1128 if Is_Decimal_Fixed_Point_Type (E)
1129 and then not Has_Machine_Radix_Clause (E)
1130 then
1131 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
1132 end if;
1133
1134 -- Object_Size (also Size which also sets Object_Size)
1135
1136 when Aspect_Object_Size
1137 | Aspect_Size
1138 =>
1139 if not Has_Size_Clause (E)
1140 and then
1141 No (Get_Attribute_Definition_Clause
1142 (E, Attribute_Object_Size))
1143 then
1144 Set_Esize (E, Esize (P));
1145 end if;
1146
1147 -- Pack
1148
1149 when Aspect_Pack =>
1150 if not Is_Packed (E) then
1151 Set_Is_Packed (Base_Type (E));
1152
1153 if Is_Bit_Packed_Array (P) then
1154 Set_Is_Bit_Packed_Array (Base_Type (E));
1155 Set_Packed_Array_Impl_Type
1156 (E, Packed_Array_Impl_Type (P));
1157 end if;
1158 end if;
1159
1160 -- Scalar_Storage_Order
1161
1162 when Aspect_Scalar_Storage_Order =>
1163 if (Is_Record_Type (E) or else Is_Array_Type (E))
1164 and then No (Get_Attribute_Definition_Clause
1165 (E, Attribute_Scalar_Storage_Order))
1166 and then Reverse_Storage_Order (P)
1167 then
1168 Set_Reverse_Storage_Order (Base_Type (E));
1169
1170 -- Clear default SSO indications, since the aspect
1171 -- overrides the default.
1172
1173 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
1174 Set_SSO_Set_High_By_Default (Base_Type (E), False);
1175 end if;
1176
1177 -- Small
1178
1179 when Aspect_Small =>
1180 if Is_Fixed_Point_Type (E)
1181 and then not Has_Small_Clause (E)
1182 then
1183 Set_Small_Value (E, Small_Value (P));
1184 end if;
1185
1186 -- Storage_Size
1187
1188 when Aspect_Storage_Size =>
1189 if (Is_Access_Type (E) or else Is_Task_Type (E))
1190 and then not Has_Storage_Size_Clause (E)
1191 then
1192 Set_Storage_Size_Variable
1193 (Base_Type (E), Storage_Size_Variable (P));
1194 end if;
1195
1196 -- Value_Size
1197
1198 when Aspect_Value_Size =>
1199
1200 -- Value_Size is never inherited, it is either set by
1201 -- default, or it is explicitly set for the derived
1202 -- type. So nothing to do here.
1203
1204 null;
1205
1206 -- Volatile
1207
1208 when Aspect_Volatile =>
1209 if Is_Volatile (P) then
1210 Set_Is_Volatile (E);
1211 end if;
1212
1213 -- Volatile_Full_Access (also Full_Access_Only)
1214
1215 when Aspect_Volatile_Full_Access
1216 | Aspect_Full_Access_Only
1217 =>
1218 if Is_Volatile_Full_Access (P) then
1219 Set_Is_Volatile_Full_Access (E);
1220 end if;
1221
1222 -- Volatile_Components
1223
1224 when Aspect_Volatile_Components =>
1225 if Has_Volatile_Components (P) then
1226 Set_Has_Volatile_Components (Base_Type (E));
1227 end if;
1228
1229 -- That should be all the Rep Aspects
1230
1231 when others =>
1232 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1233 null;
1234 end case;
1235 end if;
1236 end if;
1237
1238 Next_Rep_Item (N);
1239 end loop;
1240 end Inherit_Delayed_Rep_Aspects;
1241
1242 -------------------------------------
1243 -- Make_Pragma_From_Boolean_Aspect --
1244 -------------------------------------
1245
1246 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1247 Ident : constant Node_Id := Identifier (ASN);
1248 A_Name : constant Name_Id := Chars (Ident);
1249 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1250 Ent : constant Entity_Id := Entity (ASN);
1251 Expr : constant Node_Id := Expression (ASN);
1252 Loc : constant Source_Ptr := Sloc (ASN);
1253
1254 procedure Check_False_Aspect_For_Derived_Type;
1255 -- This procedure checks for the case of a false aspect for a derived
1256 -- type, which improperly tries to cancel an aspect inherited from
1257 -- the parent.
1258
1259 -----------------------------------------
1260 -- Check_False_Aspect_For_Derived_Type --
1261 -----------------------------------------
1262
1263 procedure Check_False_Aspect_For_Derived_Type is
1264 Par : Node_Id;
1265
1266 begin
1267 -- We are only checking derived types
1268
1269 if not Is_Derived_Type (E) then
1270 return;
1271 end if;
1272
1273 Par := Nearest_Ancestor (E);
1274
1275 case A_Id is
1276 when Aspect_Atomic
1277 | Aspect_Shared
1278 =>
1279 if not Is_Atomic (Par) then
1280 return;
1281 end if;
1282
1283 when Aspect_Atomic_Components =>
1284 if not Has_Atomic_Components (Par) then
1285 return;
1286 end if;
1287
1288 when Aspect_Discard_Names =>
1289 if not Discard_Names (Par) then
1290 return;
1291 end if;
1292
1293 when Aspect_Pack =>
1294 if not Is_Packed (Par) then
1295 return;
1296 end if;
1297
1298 when Aspect_Unchecked_Union =>
1299 if not Is_Unchecked_Union (Par) then
1300 return;
1301 end if;
1302
1303 when Aspect_Volatile =>
1304 if not Is_Volatile (Par) then
1305 return;
1306 end if;
1307
1308 when Aspect_Volatile_Components =>
1309 if not Has_Volatile_Components (Par) then
1310 return;
1311 end if;
1312
1313 when Aspect_Volatile_Full_Access
1314 | Aspect_Full_Access_Only
1315 =>
1316 if not Is_Volatile_Full_Access (Par) then
1317 return;
1318 end if;
1319
1320 when others =>
1321 return;
1322 end case;
1323
1324 -- Fall through means we are canceling an inherited aspect
1325
1326 Error_Msg_Name_1 := A_Name;
1327 Error_Msg_NE
1328 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1329 end Check_False_Aspect_For_Derived_Type;
1330
1331 -- Local variables
1332
1333 Prag : Node_Id;
1334 P_Name : Name_Id;
1335
1336 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1337
1338 begin
1339 if Present (Expr) and then Is_False (Static_Boolean (Expr)) then
1340 Check_False_Aspect_For_Derived_Type;
1341
1342 else
1343 -- There is no Full_Access_Only pragma so use VFA instead
1344
1345 if A_Name = Name_Full_Access_Only then
1346 P_Name := Name_Volatile_Full_Access;
1347 else
1348 P_Name := A_Name;
1349 end if;
1350
1351 Prag :=
1352 Make_Pragma (Loc,
1353 Pragma_Identifier =>
1354 Make_Identifier (Sloc (Ident), P_Name),
1355 Pragma_Argument_Associations => New_List (
1356 Make_Pragma_Argument_Association (Sloc (Ident),
1357 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
1358
1359 Set_From_Aspect_Specification (Prag, True);
1360 Set_Corresponding_Aspect (Prag, ASN);
1361 Set_Aspect_Rep_Item (ASN, Prag);
1362 Set_Is_Delayed_Aspect (Prag);
1363 Set_Parent (Prag, ASN);
1364 end if;
1365 end Make_Pragma_From_Boolean_Aspect;
1366
1367 -- Local variables
1368
1369 A_Id : Aspect_Id;
1370 ASN : Node_Id;
1371 Ritem : Node_Id;
1372
1373 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1374
1375 begin
1376 -- Must be visible in current scope, but if this is a type from a nested
1377 -- package it may be frozen from an object declaration in the enclosing
1378 -- scope, so install the package declarations to complete the analysis
1379 -- of the aspects, if any. If the package itself is frozen the type will
1380 -- have been frozen as well.
1381
1382 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1383 if Is_Type (E) and then From_Nested_Package (E) then
1384 declare
1385 Pack : constant Entity_Id := Scope (E);
1386
1387 begin
1388 Push_Scope (Pack);
1389 Install_Visible_Declarations (Pack);
1390 Install_Private_Declarations (Pack);
1391 Analyze_Aspects_At_Freeze_Point (E);
1392
1393 if Is_Private_Type (E)
1394 and then Present (Full_View (E))
1395 then
1396 Analyze_Aspects_At_Freeze_Point (Full_View (E));
1397 end if;
1398
1399 End_Package_Scope (Pack);
1400 return;
1401 end;
1402
1403 -- Aspects from other entities in different contexts are analyzed
1404 -- elsewhere.
1405
1406 else
1407 return;
1408 end if;
1409 end if;
1410
1411 -- Look for aspect specification entries for this entity
1412
1413 ASN := First_Rep_Item (E);
1414 while Present (ASN) loop
1415 if Nkind (ASN) = N_Aspect_Specification then
1416 exit when Entity (ASN) /= E;
1417
1418 if Is_Delayed_Aspect (ASN) then
1419 A_Id := Get_Aspect_Id (ASN);
1420
1421 case A_Id is
1422
1423 -- For aspects whose expression is an optional Boolean, make
1424 -- the corresponding pragma at the freeze point.
1425
1426 when Boolean_Aspects
1427 | Library_Unit_Aspects
1428 =>
1429 -- Aspects Export and Import require special handling.
1430 -- Both are by definition Boolean and may benefit from
1431 -- forward references, however their expressions are
1432 -- treated as static. In addition, the syntax of their
1433 -- corresponding pragmas requires extra "pieces" which
1434 -- may also contain forward references. To account for
1435 -- all of this, the corresponding pragma is created by
1436 -- Analyze_Aspect_Export_Import, but is not analyzed as
1437 -- the complete analysis must happen now.
1438
1439 -- Aspect Full_Access_Only must be analyzed last so that
1440 -- aspects Volatile and Atomic, if any, are analyzed.
1441
1442 if A_Id /= Aspect_Export
1443 and then A_Id /= Aspect_Import
1444 and then A_Id /= Aspect_Full_Access_Only
1445 then
1446 Make_Pragma_From_Boolean_Aspect (ASN);
1447 end if;
1448
1449 -- Special handling for aspects that don't correspond to
1450 -- pragmas/attributes.
1451
1452 when Aspect_Default_Value
1453 | Aspect_Default_Component_Value
1454 =>
1455 -- Do not inherit aspect for anonymous base type of a
1456 -- scalar or array type, because they apply to the first
1457 -- subtype of the type, and will be processed when that
1458 -- first subtype is frozen.
1459
1460 if Is_Derived_Type (E)
1461 and then not Comes_From_Source (E)
1462 and then E /= First_Subtype (E)
1463 then
1464 null;
1465 else
1466 Analyze_Aspect_Default_Value (ASN);
1467 end if;
1468
1469 -- Ditto for iterator aspects, because the corresponding
1470 -- attributes may not have been analyzed yet.
1471
1472 when Aspect_Constant_Indexing
1473 | Aspect_Default_Iterator
1474 | Aspect_Iterator_Element
1475 | Aspect_Variable_Indexing
1476 =>
1477 Analyze (Expression (ASN));
1478
1479 if Etype (Expression (ASN)) = Any_Type then
1480 Error_Msg_NE
1481 ("\aspect must be fully defined before & is frozen",
1482 ASN, E);
1483 end if;
1484
1485 when Aspect_Integer_Literal
1486 | Aspect_Real_Literal
1487 | Aspect_String_Literal
1488 =>
1489 Validate_Literal_Aspect (E, ASN);
1490
1491 when Aspect_Iterable =>
1492 Validate_Iterable_Aspect (E, ASN);
1493
1494 when Aspect_Aggregate =>
1495 null;
1496
1497 when others =>
1498 null;
1499 end case;
1500
1501 Ritem := Aspect_Rep_Item (ASN);
1502
1503 if Present (Ritem) then
1504 Analyze (Ritem);
1505 end if;
1506 end if;
1507 end if;
1508
1509 Next_Rep_Item (ASN);
1510 end loop;
1511
1512 -- Make a second pass for a Full_Access_Only entry
1513
1514 ASN := First_Rep_Item (E);
1515 while Present (ASN) loop
1516 if Nkind (ASN) = N_Aspect_Specification then
1517 exit when Entity (ASN) /= E;
1518
1519 if Get_Aspect_Id (ASN) = Aspect_Full_Access_Only then
1520 Make_Pragma_From_Boolean_Aspect (ASN);
1521 Ritem := Aspect_Rep_Item (ASN);
1522 if Present (Ritem) then
1523 Analyze (Ritem);
1524 end if;
1525 end if;
1526 end if;
1527
1528 Next_Rep_Item (ASN);
1529 end loop;
1530
1531 -- This is where we inherit delayed rep aspects from our parent. Note
1532 -- that if we fell out of the above loop with ASN non-empty, it means
1533 -- we hit an aspect for an entity other than E, and it must be the
1534 -- type from which we were derived.
1535
1536 if May_Inherit_Delayed_Rep_Aspects (E) then
1537 Inherit_Delayed_Rep_Aspects (ASN);
1538 end if;
1539
1540 if In_Instance
1541 and then E /= Base_Type (E)
1542 and then Is_First_Subtype (E)
1543 then
1544 Inherit_Rep_Item_Chain (Base_Type (E), E);
1545 end if;
1546 end Analyze_Aspects_At_Freeze_Point;
1547
1548 -----------------------------------
1549 -- Analyze_Aspect_Specifications --
1550 -----------------------------------
1551
1552 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1553 pragma Assert (Present (E));
1554
1555 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1556 -- Establish linkages between an aspect and its corresponding pragma
1557
1558 procedure Insert_Pragma
1559 (Prag : Node_Id;
1560 Is_Instance : Boolean := False);
1561 -- Subsidiary to the analysis of aspects
1562 -- Abstract_State
1563 -- Attach_Handler
1564 -- Contract_Cases
1565 -- Depends
1566 -- Ghost
1567 -- Global
1568 -- Initial_Condition
1569 -- Initializes
1570 -- Post
1571 -- Pre
1572 -- Refined_Depends
1573 -- Refined_Global
1574 -- Refined_State
1575 -- SPARK_Mode
1576 -- Subprogram_Variant
1577 -- Warnings
1578 -- Insert pragma Prag such that it mimics the placement of a source
1579 -- pragma of the same kind. Flag Is_Generic should be set when the
1580 -- context denotes a generic instance.
1581
1582 --------------
1583 -- Decorate --
1584 --------------
1585
1586 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1587 begin
1588 Set_Aspect_Rep_Item (Asp, Prag);
1589 Set_Corresponding_Aspect (Prag, Asp);
1590 Set_From_Aspect_Specification (Prag);
1591 Set_Parent (Prag, Asp);
1592 end Decorate;
1593
1594 -------------------
1595 -- Insert_Pragma --
1596 -------------------
1597
1598 procedure Insert_Pragma
1599 (Prag : Node_Id;
1600 Is_Instance : Boolean := False)
1601 is
1602 Aux : Node_Id;
1603 Decl : Node_Id;
1604 Decls : List_Id;
1605 Def : Node_Id;
1606 Inserted : Boolean := False;
1607
1608 begin
1609 -- When the aspect appears on an entry, package, protected unit,
1610 -- subprogram, or task unit body, insert the generated pragma at the
1611 -- top of the body declarations to emulate the behavior of a source
1612 -- pragma.
1613
1614 -- package body Pack with Aspect is
1615
1616 -- package body Pack is
1617 -- pragma Prag;
1618
1619 if Nkind (N) in N_Entry_Body
1620 | N_Package_Body
1621 | N_Protected_Body
1622 | N_Subprogram_Body
1623 | N_Task_Body
1624 then
1625 Decls := Declarations (N);
1626
1627 if No (Decls) then
1628 Decls := New_List;
1629 Set_Declarations (N, Decls);
1630 end if;
1631
1632 Prepend_To (Decls, Prag);
1633
1634 -- When the aspect is associated with a [generic] package declaration
1635 -- insert the generated pragma at the top of the visible declarations
1636 -- to emulate the behavior of a source pragma.
1637
1638 -- package Pack with Aspect is
1639
1640 -- package Pack is
1641 -- pragma Prag;
1642
1643 elsif Nkind (N) in N_Generic_Package_Declaration
1644 | N_Package_Declaration
1645 then
1646 Decls := Visible_Declarations (Specification (N));
1647
1648 if No (Decls) then
1649 Decls := New_List;
1650 Set_Visible_Declarations (Specification (N), Decls);
1651 end if;
1652
1653 -- The visible declarations of a generic instance have the
1654 -- following structure:
1655
1656 -- <renamings of generic formals>
1657 -- <renamings of internally-generated spec and body>
1658 -- <first source declaration>
1659
1660 -- Insert the pragma before the first source declaration by
1661 -- skipping the instance "header" to ensure proper visibility of
1662 -- all formals.
1663
1664 if Is_Instance then
1665 Decl := First (Decls);
1666 while Present (Decl) loop
1667 if Comes_From_Source (Decl) then
1668 Insert_Before (Decl, Prag);
1669 Inserted := True;
1670 exit;
1671 else
1672 Next (Decl);
1673 end if;
1674 end loop;
1675
1676 -- The pragma is placed after the instance "header"
1677
1678 if not Inserted then
1679 Append_To (Decls, Prag);
1680 end if;
1681
1682 -- Otherwise this is not a generic instance
1683
1684 else
1685 Prepend_To (Decls, Prag);
1686 end if;
1687
1688 -- When the aspect is associated with a protected unit declaration,
1689 -- insert the generated pragma at the top of the visible declarations
1690 -- the emulate the behavior of a source pragma.
1691
1692 -- protected [type] Prot with Aspect is
1693
1694 -- protected [type] Prot is
1695 -- pragma Prag;
1696
1697 elsif Nkind (N) = N_Protected_Type_Declaration then
1698 Def := Protected_Definition (N);
1699
1700 if No (Def) then
1701 Def :=
1702 Make_Protected_Definition (Sloc (N),
1703 Visible_Declarations => New_List,
1704 End_Label => Empty);
1705
1706 Set_Protected_Definition (N, Def);
1707 end if;
1708
1709 Decls := Visible_Declarations (Def);
1710
1711 if No (Decls) then
1712 Decls := New_List;
1713 Set_Visible_Declarations (Def, Decls);
1714 end if;
1715
1716 Prepend_To (Decls, Prag);
1717
1718 -- When the aspect is associated with a task unit declaration, insert
1719 -- insert the generated pragma at the top of the visible declarations
1720 -- the emulate the behavior of a source pragma.
1721
1722 -- task [type] Prot with Aspect is
1723
1724 -- task [type] Prot is
1725 -- pragma Prag;
1726
1727 elsif Nkind (N) = N_Task_Type_Declaration then
1728 Def := Task_Definition (N);
1729
1730 if No (Def) then
1731 Def :=
1732 Make_Task_Definition (Sloc (N),
1733 Visible_Declarations => New_List,
1734 End_Label => Empty);
1735
1736 Set_Task_Definition (N, Def);
1737 end if;
1738
1739 Decls := Visible_Declarations (Def);
1740
1741 if No (Decls) then
1742 Decls := New_List;
1743 Set_Visible_Declarations (Def, Decls);
1744 end if;
1745
1746 Prepend_To (Decls, Prag);
1747
1748 -- When the context is a library unit, the pragma is added to the
1749 -- Pragmas_After list.
1750
1751 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1752 Aux := Aux_Decls_Node (Parent (N));
1753
1754 if No (Pragmas_After (Aux)) then
1755 Set_Pragmas_After (Aux, New_List);
1756 end if;
1757
1758 Prepend (Prag, Pragmas_After (Aux));
1759
1760 -- Default, the pragma is inserted after the context
1761
1762 else
1763 Insert_After (N, Prag);
1764 end if;
1765 end Insert_Pragma;
1766
1767 -- Local variables
1768
1769 Aspect : Node_Id;
1770 Aitem : Node_Id := Empty;
1771 Ent : Node_Id;
1772
1773 L : constant List_Id := Aspect_Specifications (N);
1774 pragma Assert (Present (L));
1775
1776 Ins_Node : Node_Id := N;
1777 -- Insert pragmas/attribute definition clause after this node when no
1778 -- delayed analysis is required.
1779
1780 -- Start of processing for Analyze_Aspect_Specifications
1781
1782 begin
1783 -- The general processing involves building an attribute definition
1784 -- clause or a pragma node that corresponds to the aspect. Then in order
1785 -- to delay the evaluation of this aspect to the freeze point, we attach
1786 -- the corresponding pragma/attribute definition clause to the aspect
1787 -- specification node, which is then placed in the Rep Item chain. In
1788 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1789 -- and we evaluate the rep item at the freeze point. When the aspect
1790 -- doesn't have a corresponding pragma/attribute definition clause, then
1791 -- its analysis is simply delayed at the freeze point.
1792
1793 -- Some special cases don't require delay analysis, thus the aspect is
1794 -- analyzed right now.
1795
1796 -- Note that there is a special handling for Pre, Post, Test_Case,
1797 -- Contract_Cases and Subprogram_Variant aspects. In these cases, we do
1798 -- not have to worry about delay issues, since the pragmas themselves
1799 -- deal with delay of visibility for the expression analysis. Thus, we
1800 -- just insert the pragma after the node N.
1801
1802 -- Loop through aspects
1803
1804 Aspect := First (L);
1805 Aspect_Loop : while Present (Aspect) loop
1806 Analyze_One_Aspect : declare
1807 Expr : constant Node_Id := Expression (Aspect);
1808 Id : constant Node_Id := Identifier (Aspect);
1809 Loc : constant Source_Ptr := Sloc (Aspect);
1810 Nam : constant Name_Id := Chars (Id);
1811 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1812 Anod : Node_Id;
1813
1814 Delay_Required : Boolean;
1815 -- Set False if delay is not required
1816
1817 Eloc : Source_Ptr := No_Location;
1818 -- Source location of expression, modified when we split PPC's. It
1819 -- is set below when Expr is present.
1820
1821 procedure Analyze_Aspect_Convention;
1822 -- Perform analysis of aspect Convention
1823
1824 procedure Analyze_Aspect_Disable_Controlled;
1825 -- Perform analysis of aspect Disable_Controlled
1826
1827 procedure Analyze_Aspect_Export_Import;
1828 -- Perform analysis of aspects Export or Import
1829
1830 procedure Analyze_Aspect_External_Link_Name;
1831 -- Perform analysis of aspects External_Name or Link_Name
1832
1833 procedure Analyze_Aspect_Implicit_Dereference;
1834 -- Perform analysis of the Implicit_Dereference aspects
1835
1836 procedure Analyze_Aspect_Relaxed_Initialization;
1837 -- Perform analysis of aspect Relaxed_Initialization
1838
1839 procedure Analyze_Aspect_Yield;
1840 -- Perform analysis of aspect Yield
1841
1842 procedure Analyze_Aspect_Static;
1843 -- Ada 202x (AI12-0075): Perform analysis of aspect Static
1844
1845 function Make_Aitem_Pragma
1846 (Pragma_Argument_Associations : List_Id;
1847 Pragma_Name : Name_Id) return Node_Id;
1848 -- This is a wrapper for Make_Pragma used for converting aspects
1849 -- to pragmas. It takes care of Sloc (set from Loc) and building
1850 -- the pragma identifier from the given name. In addition the
1851 -- flags Class_Present and Split_PPC are set from the aspect
1852 -- node, as well as Is_Ignored. This routine also sets the
1853 -- From_Aspect_Specification in the resulting pragma node to
1854 -- True, and sets Corresponding_Aspect to point to the aspect.
1855 -- The resulting pragma is assigned to Aitem.
1856
1857 -------------------------------
1858 -- Analyze_Aspect_Convention --
1859 -------------------------------
1860
1861 procedure Analyze_Aspect_Convention is
1862 Conv : Node_Id;
1863 Dummy_1 : Node_Id;
1864 Dummy_2 : Node_Id;
1865 Dummy_3 : Node_Id;
1866 Expo : Node_Id;
1867 Imp : Node_Id;
1868
1869 begin
1870 -- Obtain all interfacing aspects that apply to the related
1871 -- entity.
1872
1873 Get_Interfacing_Aspects
1874 (Iface_Asp => Aspect,
1875 Conv_Asp => Dummy_1,
1876 EN_Asp => Dummy_2,
1877 Expo_Asp => Expo,
1878 Imp_Asp => Imp,
1879 LN_Asp => Dummy_3,
1880 Do_Checks => True);
1881
1882 -- The related entity is subject to aspect Export or Import.
1883 -- Do not process Convention now because it must be analysed
1884 -- as part of Export or Import.
1885
1886 if Present (Expo) or else Present (Imp) then
1887 return;
1888
1889 -- Otherwise Convention appears by itself
1890
1891 else
1892 -- The aspect specifies a particular convention
1893
1894 if Present (Expr) then
1895 Conv := New_Copy_Tree (Expr);
1896
1897 -- Otherwise assume convention Ada
1898
1899 else
1900 Conv := Make_Identifier (Loc, Name_Ada);
1901 end if;
1902
1903 -- Generate:
1904 -- pragma Convention (<Conv>, <E>);
1905
1906 Aitem := Make_Aitem_Pragma
1907 (Pragma_Name => Name_Convention,
1908 Pragma_Argument_Associations => New_List (
1909 Make_Pragma_Argument_Association (Loc,
1910 Expression => Conv),
1911 Make_Pragma_Argument_Association (Loc,
1912 Expression => New_Occurrence_Of (E, Loc))));
1913
1914 Decorate (Aspect, Aitem);
1915 Insert_Pragma (Aitem);
1916 end if;
1917 end Analyze_Aspect_Convention;
1918
1919 ---------------------------------------
1920 -- Analyze_Aspect_Disable_Controlled --
1921 ---------------------------------------
1922
1923 procedure Analyze_Aspect_Disable_Controlled is
1924 begin
1925 -- The aspect applies only to controlled records
1926
1927 if not (Ekind (E) = E_Record_Type
1928 and then Is_Controlled_Active (E))
1929 then
1930 Error_Msg_N
1931 ("aspect % requires controlled record type", Aspect);
1932 return;
1933 end if;
1934
1935 -- Preanalyze the expression (if any) when the aspect resides
1936 -- in a generic unit.
1937
1938 if Inside_A_Generic then
1939 if Present (Expr) then
1940 Preanalyze_And_Resolve (Expr, Any_Boolean);
1941 end if;
1942
1943 -- Otherwise the aspect resides in a nongeneric context
1944
1945 else
1946 -- A controlled record type loses its controlled semantics
1947 -- when the expression statically evaluates to True.
1948
1949 if Present (Expr) then
1950 Analyze_And_Resolve (Expr, Any_Boolean);
1951
1952 if Is_OK_Static_Expression (Expr) then
1953 if Is_True (Static_Boolean (Expr)) then
1954 Set_Disable_Controlled (E);
1955 end if;
1956
1957 -- Otherwise the expression is not static
1958
1959 else
1960 Error_Msg_N
1961 ("expression of aspect % must be static", Aspect);
1962 end if;
1963
1964 -- Otherwise the aspect appears without an expression and
1965 -- defaults to True.
1966
1967 else
1968 Set_Disable_Controlled (E);
1969 end if;
1970 end if;
1971 end Analyze_Aspect_Disable_Controlled;
1972
1973 ----------------------------------
1974 -- Analyze_Aspect_Export_Import --
1975 ----------------------------------
1976
1977 procedure Analyze_Aspect_Export_Import is
1978 Dummy_1 : Node_Id;
1979 Dummy_2 : Node_Id;
1980 Dummy_3 : Node_Id;
1981 Expo : Node_Id;
1982 Imp : Node_Id;
1983
1984 begin
1985 -- Obtain all interfacing aspects that apply to the related
1986 -- entity.
1987
1988 Get_Interfacing_Aspects
1989 (Iface_Asp => Aspect,
1990 Conv_Asp => Dummy_1,
1991 EN_Asp => Dummy_2,
1992 Expo_Asp => Expo,
1993 Imp_Asp => Imp,
1994 LN_Asp => Dummy_3,
1995 Do_Checks => True);
1996
1997 -- The related entity cannot be subject to both aspects Export
1998 -- and Import.
1999
2000 if Present (Expo) and then Present (Imp) then
2001 Error_Msg_N
2002 ("incompatible interfacing aspects given for &", E);
2003 Error_Msg_Sloc := Sloc (Expo);
2004 Error_Msg_N ("\aspect `Export` #", E);
2005 Error_Msg_Sloc := Sloc (Imp);
2006 Error_Msg_N ("\aspect `Import` #", E);
2007 end if;
2008
2009 -- A variable is most likely modified from the outside. Take
2010 -- the optimistic approach to avoid spurious errors.
2011
2012 if Ekind (E) = E_Variable then
2013 Set_Never_Set_In_Source (E, False);
2014 end if;
2015
2016 -- Resolve the expression of an Import or Export here, and
2017 -- require it to be of type Boolean and static. This is not
2018 -- quite right, because in general this should be delayed,
2019 -- but that seems tricky for these, because normally Boolean
2020 -- aspects are replaced with pragmas at the freeze point in
2021 -- Make_Pragma_From_Boolean_Aspect.
2022
2023 if not Present (Expr)
2024 or else Is_True (Static_Boolean (Expr))
2025 then
2026 if A_Id = Aspect_Import then
2027 Set_Has_Completion (E);
2028 Set_Is_Imported (E);
2029
2030 -- An imported object cannot be explicitly initialized
2031
2032 if Nkind (N) = N_Object_Declaration
2033 and then Present (Expression (N))
2034 then
2035 Error_Msg_N
2036 ("imported entities cannot be initialized "
2037 & "(RM B.1(24))", Expression (N));
2038 end if;
2039
2040 else
2041 pragma Assert (A_Id = Aspect_Export);
2042 Set_Is_Exported (E);
2043 end if;
2044
2045 -- Create the proper form of pragma Export or Import taking
2046 -- into account Conversion, External_Name, and Link_Name.
2047
2048 Aitem := Build_Export_Import_Pragma (Aspect, E);
2049
2050 -- Otherwise the expression is either False or erroneous. There
2051 -- is no corresponding pragma.
2052
2053 else
2054 Aitem := Empty;
2055 end if;
2056 end Analyze_Aspect_Export_Import;
2057
2058 ---------------------------------------
2059 -- Analyze_Aspect_External_Link_Name --
2060 ---------------------------------------
2061
2062 procedure Analyze_Aspect_External_Link_Name is
2063 Dummy_1 : Node_Id;
2064 Dummy_2 : Node_Id;
2065 Dummy_3 : Node_Id;
2066 Expo : Node_Id;
2067 Imp : Node_Id;
2068
2069 begin
2070 -- Obtain all interfacing aspects that apply to the related
2071 -- entity.
2072
2073 Get_Interfacing_Aspects
2074 (Iface_Asp => Aspect,
2075 Conv_Asp => Dummy_1,
2076 EN_Asp => Dummy_2,
2077 Expo_Asp => Expo,
2078 Imp_Asp => Imp,
2079 LN_Asp => Dummy_3,
2080 Do_Checks => True);
2081
2082 -- Ensure that aspect External_Name applies to aspect Export or
2083 -- Import.
2084
2085 if A_Id = Aspect_External_Name then
2086 if No (Expo) and then No (Imp) then
2087 Error_Msg_N
2088 ("aspect `External_Name` requires aspect `Import` or "
2089 & "`Export`", Aspect);
2090 end if;
2091
2092 -- Otherwise ensure that aspect Link_Name applies to aspect
2093 -- Export or Import.
2094
2095 else
2096 pragma Assert (A_Id = Aspect_Link_Name);
2097 if No (Expo) and then No (Imp) then
2098 Error_Msg_N
2099 ("aspect `Link_Name` requires aspect `Import` or "
2100 & "`Export`", Aspect);
2101 end if;
2102 end if;
2103 end Analyze_Aspect_External_Link_Name;
2104
2105 -----------------------------------------
2106 -- Analyze_Aspect_Implicit_Dereference --
2107 -----------------------------------------
2108
2109 procedure Analyze_Aspect_Implicit_Dereference is
2110 begin
2111 if not Is_Type (E) or else not Has_Discriminants (E) then
2112 Error_Msg_N
2113 ("aspect must apply to a type with discriminants", Expr);
2114
2115 elsif not Is_Entity_Name (Expr) then
2116 Error_Msg_N
2117 ("aspect must name a discriminant of current type", Expr);
2118
2119 else
2120 -- Discriminant type be an anonymous access type or an
2121 -- anonymous access to subprogram.
2122
2123 -- Missing synchronized types???
2124
2125 declare
2126 Disc : Entity_Id := First_Discriminant (E);
2127 begin
2128 while Present (Disc) loop
2129 if Chars (Expr) = Chars (Disc)
2130 and then Ekind (Etype (Disc)) in
2131 E_Anonymous_Access_Subprogram_Type |
2132 E_Anonymous_Access_Type
2133 then
2134 Set_Has_Implicit_Dereference (E);
2135 Set_Has_Implicit_Dereference (Disc);
2136 exit;
2137 end if;
2138
2139 Next_Discriminant (Disc);
2140 end loop;
2141
2142 -- Error if no proper access discriminant
2143
2144 if Present (Disc) then
2145 -- For a type extension, check whether parent has
2146 -- a reference discriminant, to verify that use is
2147 -- proper.
2148
2149 if Is_Derived_Type (E)
2150 and then Has_Discriminants (Etype (E))
2151 then
2152 declare
2153 Parent_Disc : constant Entity_Id :=
2154 Get_Reference_Discriminant (Etype (E));
2155 begin
2156 if Present (Parent_Disc)
2157 and then Corresponding_Discriminant (Disc) /=
2158 Parent_Disc
2159 then
2160 Error_Msg_N
2161 ("reference discriminant does not match "
2162 & "discriminant of parent type", Expr);
2163 end if;
2164 end;
2165 end if;
2166
2167 else
2168 Error_Msg_NE
2169 ("not an access discriminant of&", Expr, E);
2170 end if;
2171 end;
2172 end if;
2173
2174 end Analyze_Aspect_Implicit_Dereference;
2175
2176 -------------------------------------------
2177 -- Analyze_Aspect_Relaxed_Initialization --
2178 -------------------------------------------
2179
2180 procedure Analyze_Aspect_Relaxed_Initialization is
2181 procedure Analyze_Relaxed_Parameter
2182 (Subp_Id : Entity_Id;
2183 Param : Node_Id;
2184 Seen : in out Elist_Id);
2185 -- Analyze parameter that appears in the expression of the
2186 -- aspect Relaxed_Initialization.
2187
2188 -------------------------------
2189 -- Analyze_Relaxed_Parameter --
2190 -------------------------------
2191
2192 procedure Analyze_Relaxed_Parameter
2193 (Subp_Id : Entity_Id;
2194 Param : Node_Id;
2195 Seen : in out Elist_Id)
2196 is
2197 begin
2198 -- Set name of the aspect for error messages
2199 Error_Msg_Name_1 := Nam;
2200
2201 -- The relaxed parameter is a formal parameter
2202
2203 if Nkind (Param) in N_Identifier | N_Expanded_Name then
2204 Analyze (Param);
2205
2206 declare
2207 Item : constant Entity_Id := Entity (Param);
2208 begin
2209 -- It must be a formal of the analyzed subprogram
2210
2211 if Scope (Item) = Subp_Id then
2212
2213 pragma Assert (Is_Formal (Item));
2214
2215 -- It must not have scalar or access type
2216
2217 if Is_Elementary_Type (Etype (Item)) then
2218 Error_Msg_N ("illegal aspect % item", Param);
2219 Error_Msg_N
2220 ("\item must not have elementary type", Param);
2221 end if;
2222
2223 -- Detect duplicated items
2224
2225 if Contains (Seen, Item) then
2226 Error_Msg_N ("duplicate aspect % item", Param);
2227 else
2228 Append_New_Elmt (Item, Seen);
2229 end if;
2230 else
2231 Error_Msg_N ("illegal aspect % item", Param);
2232 end if;
2233 end;
2234
2235 -- The relaxed parameter is the function's Result attribute
2236
2237 elsif Is_Attribute_Result (Param) then
2238 Analyze (Param);
2239
2240 declare
2241 Pref : constant Node_Id := Prefix (Param);
2242 begin
2243 if Present (Pref)
2244 and then
2245 Nkind (Pref) in N_Identifier | N_Expanded_Name
2246 and then
2247 Entity (Pref) = Subp_Id
2248 then
2249 -- Function result must not have scalar or access
2250 -- type.
2251
2252 if Is_Elementary_Type (Etype (Pref)) then
2253 Error_Msg_N ("illegal aspect % item", Param);
2254 Error_Msg_N
2255 ("\function result must not have elementary"
2256 & " type", Param);
2257 end if;
2258
2259 -- Detect duplicated items
2260
2261 if Contains (Seen, Subp_Id) then
2262 Error_Msg_N ("duplicate aspect % item", Param);
2263 else
2264 Append_New_Elmt (Entity (Pref), Seen);
2265 end if;
2266
2267 else
2268 Error_Msg_N ("illegal aspect % item", Param);
2269 end if;
2270 end;
2271 else
2272 Error_Msg_N ("illegal aspect % item", Param);
2273 end if;
2274 end Analyze_Relaxed_Parameter;
2275
2276 -- Local variables
2277
2278 Seen : Elist_Id := No_Elist;
2279 -- Items that appear in the relaxed initialization aspect
2280 -- expression of a subprogram; for detecting duplicates.
2281
2282 Restore_Scope : Boolean;
2283 -- Will be set to True if we need to restore the scope table
2284 -- after analyzing the aspect expression.
2285
2286 Prev_Id : Entity_Id;
2287
2288 -- Start of processing for Analyze_Aspect_Relaxed_Initialization
2289
2290 begin
2291 -- Set name of the aspect for error messages
2292 Error_Msg_Name_1 := Nam;
2293
2294 -- Annotation of a type; no aspect expression is allowed.
2295 -- For a private type, the aspect must be attached to the
2296 -- partial view.
2297 --
2298 -- ??? Once the exact rule for this aspect is ready, we will
2299 -- likely reject concurrent types, etc., so let's keep the code
2300 -- for types and variable separate.
2301
2302 if Is_First_Subtype (E) then
2303 Prev_Id := Incomplete_Or_Partial_View (E);
2304 if Present (Prev_Id) then
2305
2306 -- Aspect may appear on the full view of an incomplete
2307 -- type because the incomplete declaration cannot have
2308 -- any aspects.
2309
2310 if Ekind (Prev_Id) = E_Incomplete_Type then
2311 null;
2312 else
2313 Error_Msg_N ("aspect % must apply to partial view", N);
2314 end if;
2315
2316 elsif Present (Expr) then
2317 Error_Msg_N ("illegal aspect % expression", Expr);
2318 end if;
2319
2320 -- Annotation of a variable; no aspect expression is allowed
2321
2322 elsif Ekind (E) = E_Variable then
2323 if Present (Expr) then
2324 Error_Msg_N ("illegal aspect % expression", Expr);
2325 end if;
2326
2327 -- Annotation of a constant; no aspect expression is allowed.
2328 -- For a deferred constant, the aspect must be attached to the
2329 -- partial view.
2330
2331 elsif Ekind (E) = E_Constant then
2332 if Present (Incomplete_Or_Partial_View (E)) then
2333 Error_Msg_N
2334 ("aspect % must apply to deferred constant", N);
2335
2336 elsif Present (Expr) then
2337 Error_Msg_N ("illegal aspect % expression", Expr);
2338 end if;
2339
2340 -- Annotation of a subprogram; aspect expression is required
2341
2342 elsif Is_Subprogram_Or_Entry (E)
2343 or else Is_Generic_Subprogram (E)
2344 then
2345 if Present (Expr) then
2346
2347 -- If we analyze subprogram body that acts as its own
2348 -- spec, then the subprogram itself and its formals are
2349 -- already installed; otherwise, we need to install them,
2350 -- as they must be visible when analyzing the aspect
2351 -- expression.
2352
2353 if In_Open_Scopes (E) then
2354 Restore_Scope := False;
2355 else
2356 Restore_Scope := True;
2357 Push_Scope (E);
2358
2359 -- Only formals of the subprogram itself can appear
2360 -- in Relaxed_Initialization aspect expression, not
2361 -- formals of the enclosing generic unit. (This is
2362 -- different than in Precondition or Depends aspects,
2363 -- where both kinds of formals are allowed.)
2364
2365 Install_Formals (E);
2366 end if;
2367
2368 -- Aspect expression is either an aggregate with list of
2369 -- parameters (and possibly the Result attribute for a
2370 -- function).
2371
2372 if Nkind (Expr) = N_Aggregate then
2373
2374 -- Component associations in the aggregate must be a
2375 -- parameter name followed by a static boolean
2376 -- expression.
2377
2378 if Present (Component_Associations (Expr)) then
2379 declare
2380 Assoc : Node_Id :=
2381 First (Component_Associations (Expr));
2382 begin
2383 while Present (Assoc) loop
2384 if List_Length (Choices (Assoc)) = 1 then
2385 Analyze_Relaxed_Parameter
2386 (E, First (Choices (Assoc)), Seen);
2387
2388 if Inside_A_Generic then
2389 Preanalyze_And_Resolve
2390 (Expression (Assoc), Any_Boolean);
2391 else
2392 Analyze_And_Resolve
2393 (Expression (Assoc), Any_Boolean);
2394 end if;
2395
2396 if not Is_OK_Static_Expression
2397 (Expression (Assoc))
2398 then
2399 Error_Msg_Name_1 := Nam;
2400 Error_Msg_N
2401 ("expression of aspect %" &
2402 "must be static", Aspect);
2403 end if;
2404
2405 else
2406 Error_Msg_Name_1 := Nam;
2407 Error_Msg_N
2408 ("illegal aspect % expression", Expr);
2409 end if;
2410 Next (Assoc);
2411 end loop;
2412 end;
2413 end if;
2414
2415 -- Expressions of the aggregate are parameter names
2416
2417 if Present (Expressions (Expr)) then
2418 declare
2419 Param : Node_Id := First (Expressions (Expr));
2420
2421 begin
2422 while Present (Param) loop
2423 Analyze_Relaxed_Parameter (E, Param, Seen);
2424 Next (Param);
2425 end loop;
2426 end;
2427 end if;
2428
2429 -- Mark the aggregate expression itself as analyzed;
2430 -- its subexpressions were marked when they themselves
2431 -- were analyzed.
2432
2433 Set_Analyzed (Expr);
2434
2435 -- Otherwise, it is a single name of a subprogram
2436 -- parameter (or possibly the Result attribute for
2437 -- a function).
2438
2439 else
2440 Analyze_Relaxed_Parameter (E, Expr, Seen);
2441 end if;
2442
2443 if Restore_Scope then
2444 End_Scope;
2445 end if;
2446 else
2447 Error_Msg_N ("missing expression for aspect %", N);
2448 end if;
2449
2450 else
2451 Error_Msg_N ("inappropriate entity for aspect %", E);
2452 end if;
2453 end Analyze_Aspect_Relaxed_Initialization;
2454
2455 ---------------------------
2456 -- Analyze_Aspect_Static --
2457 ---------------------------
2458
2459 procedure Analyze_Aspect_Static is
2460 function Has_Convention_Intrinsic (L : List_Id) return Boolean;
2461 -- Return True if L contains a pragma argument association
2462 -- node representing a convention Intrinsic.
2463
2464 ------------------------------
2465 -- Has_Convention_Intrinsic --
2466 ------------------------------
2467
2468 function Has_Convention_Intrinsic
2469 (L : List_Id) return Boolean
2470 is
2471 Arg : Node_Id := First (L);
2472 begin
2473 while Present (Arg) loop
2474 if Nkind (Arg) = N_Pragma_Argument_Association
2475 and then Chars (Arg) = Name_Convention
2476 and then Chars (Expression (Arg)) = Name_Intrinsic
2477 then
2478 return True;
2479 end if;
2480
2481 Next (Arg);
2482 end loop;
2483
2484 return False;
2485 end Has_Convention_Intrinsic;
2486
2487 Is_Imported_Intrinsic : Boolean;
2488
2489 begin
2490 if Ada_Version < Ada_2020 then
2491 Error_Msg_N
2492 ("aspect % is an Ada 202x feature", Aspect);
2493 Error_Msg_N ("\compile with -gnat2020", Aspect);
2494
2495 return;
2496 end if;
2497
2498 Is_Imported_Intrinsic := Is_Imported (E)
2499 and then
2500 Has_Convention_Intrinsic
2501 (Pragma_Argument_Associations (Import_Pragma (E)));
2502
2503 -- The aspect applies only to expression functions that
2504 -- statisfy the requirements for a static expression function
2505 -- (such as having an expression that is predicate-static) as
2506 -- well as Intrinsic imported functions as a -gnatX extension.
2507
2508 if not Is_Expression_Function (E)
2509 and then
2510 not (Extensions_Allowed and then Is_Imported_Intrinsic)
2511 then
2512 if Extensions_Allowed then
2513 Error_Msg_N
2514 ("aspect % requires intrinsic or expression function",
2515 Aspect);
2516
2517 elsif Is_Imported_Intrinsic then
2518 Error_Msg_N
2519 ("aspect % on intrinsic function is an extension: " &
2520 "use -gnatX",
2521 Aspect);
2522
2523 else
2524 Error_Msg_N
2525 ("aspect % requires expression function", Aspect);
2526 end if;
2527
2528 return;
2529
2530 -- Ada 202x (AI12-0075): Check that the function satisfies
2531 -- several requirements of static functions as specified in
2532 -- RM 6.8(5.1-5.8). Note that some of the requirements given
2533 -- there are checked elsewhere.
2534
2535 else
2536 -- The expression of the expression function must be a
2537 -- potentially static expression (RM 202x 6.8(3.2-3.4)).
2538 -- That's checked in Sem_Ch6.Analyze_Expression_Function.
2539
2540 -- The function must not contain any calls to itself, which
2541 -- is checked in Sem_Res.Resolve_Call.
2542
2543 -- Each formal must be of mode in and have a static subtype
2544
2545 declare
2546 Formal : Entity_Id := First_Formal (E);
2547 begin
2548 while Present (Formal) loop
2549 if Ekind (Formal) /= E_In_Parameter then
2550 Error_Msg_N
2551 ("aspect % requires formals of mode IN",
2552 Aspect);
2553
2554 return;
2555 end if;
2556
2557 if not Is_Static_Subtype (Etype (Formal)) then
2558 Error_Msg_N
2559 ("aspect % requires formals with static subtypes",
2560 Aspect);
2561
2562 return;
2563 end if;
2564
2565 Next_Formal (Formal);
2566 end loop;
2567 end;
2568
2569 -- The function's result subtype must be a static subtype
2570
2571 if not Is_Static_Subtype (Etype (E)) then
2572 Error_Msg_N
2573 ("aspect % requires function with result of "
2574 & "a static subtype",
2575 Aspect);
2576
2577 return;
2578 end if;
2579
2580 -- Check that the function does not have any applicable
2581 -- precondition or postcondition expression.
2582
2583 for Asp in Pre_Post_Aspects loop
2584 if Has_Aspect (E, Asp) then
2585 Error_Msg_N
2586 ("this aspect not allowed for static expression "
2587 & "functions", Find_Aspect (E, Asp));
2588
2589 return;
2590 end if;
2591 end loop;
2592
2593 -- ??? TBD: Must check that "for result type R, if the
2594 -- function is a boundary entity for type R (see 7.3.2),
2595 -- no type invariant applies to type R; if R has a
2596 -- component type C, a similar rule applies to C."
2597 end if;
2598
2599 -- Preanalyze the expression (if any) when the aspect resides
2600 -- in a generic unit. (Is this generic-related code necessary
2601 -- for this aspect? It's modeled on what's done for aspect
2602 -- Disable_Controlled. ???)
2603
2604 if Inside_A_Generic then
2605 if Present (Expr) then
2606 Preanalyze_And_Resolve (Expr, Any_Boolean);
2607 end if;
2608
2609 -- Otherwise the aspect resides in a nongeneric context
2610
2611 else
2612 -- When the expression statically evaluates to True, the
2613 -- expression function is treated as a static function.
2614 -- Otherwise the aspect appears without an expression and
2615 -- defaults to True.
2616
2617 if Present (Expr) then
2618 Analyze_And_Resolve (Expr, Any_Boolean);
2619
2620 -- Error if the boolean expression is not static
2621
2622 if not Is_OK_Static_Expression (Expr) then
2623 Error_Msg_N
2624 ("expression of aspect % must be static", Aspect);
2625 end if;
2626 end if;
2627 end if;
2628 end Analyze_Aspect_Static;
2629
2630 --------------------------
2631 -- Analyze_Aspect_Yield --
2632 --------------------------
2633
2634 procedure Analyze_Aspect_Yield is
2635 Expr_Value : Boolean := False;
2636
2637 begin
2638 -- Check valid declarations for 'Yield
2639
2640 if Nkind (N) in N_Abstract_Subprogram_Declaration
2641 | N_Entry_Declaration
2642 | N_Generic_Subprogram_Declaration
2643 | N_Subprogram_Declaration
2644 | N_Formal_Subprogram_Declaration
2645 and then not Within_Protected_Type (E)
2646 then
2647 null;
2648
2649 elsif Within_Protected_Type (E) then
2650 Error_Msg_N
2651 ("aspect% not applicable to protected operations", Id);
2652 return;
2653
2654 else
2655 Error_Msg_N
2656 ("aspect% only applicable to subprogram and entry "
2657 & "declarations", Id);
2658 return;
2659 end if;
2660
2661 -- Evaluate its static expression (if available); otherwise it
2662 -- defaults to True.
2663
2664 if No (Expr) then
2665 Expr_Value := True;
2666
2667 -- Otherwise it must have a static boolean expression
2668
2669 else
2670 if Inside_A_Generic then
2671 Preanalyze_And_Resolve (Expr, Any_Boolean);
2672 else
2673 Analyze_And_Resolve (Expr, Any_Boolean);
2674 end if;
2675
2676 if Is_OK_Static_Expression (Expr) then
2677 if Is_True (Static_Boolean (Expr)) then
2678 Expr_Value := True;
2679 end if;
2680 else
2681 Error_Msg_N
2682 ("expression of aspect % must be static", Aspect);
2683 end if;
2684 end if;
2685
2686 if Expr_Value then
2687 Set_Has_Yield_Aspect (E);
2688 end if;
2689
2690 -- If the Yield aspect is specified for a dispatching
2691 -- subprogram that inherits the aspect, the specified
2692 -- value shall be confirming.
2693
2694 if Present (Expr)
2695 and then Is_Dispatching_Operation (E)
2696 and then Present (Overridden_Operation (E))
2697 and then Has_Yield_Aspect (Overridden_Operation (E))
2698 /= Is_True (Static_Boolean (Expr))
2699 then
2700 Error_Msg_N ("specification of inherited aspect% can only " &
2701 "confirm parent value", Id);
2702 end if;
2703 end Analyze_Aspect_Yield;
2704
2705 -----------------------
2706 -- Make_Aitem_Pragma --
2707 -----------------------
2708
2709 function Make_Aitem_Pragma
2710 (Pragma_Argument_Associations : List_Id;
2711 Pragma_Name : Name_Id) return Node_Id
2712 is
2713 Args : List_Id := Pragma_Argument_Associations;
2714 Aitem : Node_Id;
2715
2716 begin
2717 -- We should never get here if aspect was disabled
2718
2719 pragma Assert (not Is_Disabled (Aspect));
2720
2721 -- Certain aspects allow for an optional name or expression. Do
2722 -- not generate a pragma with empty argument association list.
2723
2724 if No (Args) or else No (Expression (First (Args))) then
2725 Args := No_List;
2726 end if;
2727
2728 -- Build the pragma
2729
2730 Aitem :=
2731 Make_Pragma (Loc,
2732 Pragma_Argument_Associations => Args,
2733 Pragma_Identifier =>
2734 Make_Identifier (Sloc (Id), Pragma_Name),
2735 Class_Present => Class_Present (Aspect),
2736 Split_PPC => Split_PPC (Aspect));
2737
2738 -- Set additional semantic fields
2739
2740 if Is_Ignored (Aspect) then
2741 Set_Is_Ignored (Aitem);
2742 elsif Is_Checked (Aspect) then
2743 Set_Is_Checked (Aitem);
2744 end if;
2745
2746 Set_Corresponding_Aspect (Aitem, Aspect);
2747 Set_From_Aspect_Specification (Aitem);
2748
2749 return Aitem;
2750 end Make_Aitem_Pragma;
2751
2752 -- Start of processing for Analyze_One_Aspect
2753
2754 begin
2755 -- Skip aspect if already analyzed, to avoid looping in some cases
2756
2757 if Analyzed (Aspect) then
2758 goto Continue;
2759 end if;
2760
2761 -- Skip looking at aspect if it is totally disabled. Just mark it
2762 -- as such for later reference in the tree. This also sets the
2763 -- Is_Ignored and Is_Checked flags appropriately.
2764
2765 Check_Applicable_Policy (Aspect);
2766
2767 if Is_Disabled (Aspect) then
2768 goto Continue;
2769 end if;
2770
2771 -- Set the source location of expression, used in the case of
2772 -- a failed precondition/postcondition or invariant. Note that
2773 -- the source location of the expression is not usually the best
2774 -- choice here. For example, it gets located on the last AND
2775 -- keyword in a chain of boolean expressiond AND'ed together.
2776 -- It is best to put the message on the first character of the
2777 -- assertion, which is the effect of the First_Node call here.
2778
2779 if Present (Expr) then
2780 Eloc := Sloc (First_Node (Expr));
2781 end if;
2782
2783 -- Check restriction No_Implementation_Aspect_Specifications
2784
2785 if Implementation_Defined_Aspect (A_Id) then
2786 Check_Restriction
2787 (No_Implementation_Aspect_Specifications, Aspect);
2788 end if;
2789
2790 -- Check restriction No_Specification_Of_Aspect
2791
2792 Check_Restriction_No_Specification_Of_Aspect (Aspect);
2793
2794 -- Mark aspect analyzed (actual analysis is delayed till later)
2795
2796 Set_Analyzed (Aspect);
2797 Set_Entity (Aspect, E);
2798
2799 -- Build the reference to E that will be used in the built pragmas
2800
2801 Ent := New_Occurrence_Of (E, Sloc (Id));
2802
2803 if A_Id = Aspect_Attach_Handler
2804 or else A_Id = Aspect_Interrupt_Handler
2805 then
2806
2807 -- Treat the specification as a reference to the protected
2808 -- operation, which might otherwise appear unreferenced and
2809 -- generate spurious warnings.
2810
2811 Generate_Reference (E, Id);
2812 end if;
2813
2814 -- Check for duplicate aspect. Note that the Comes_From_Source
2815 -- test allows duplicate Pre/Post's that we generate internally
2816 -- to escape being flagged here.
2817
2818 if No_Duplicates_Allowed (A_Id) then
2819 Anod := First (L);
2820 while Anod /= Aspect loop
2821 if Comes_From_Source (Aspect)
2822 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
2823 then
2824 Error_Msg_Name_1 := Nam;
2825 Error_Msg_Sloc := Sloc (Anod);
2826
2827 -- Case of same aspect specified twice
2828
2829 if Class_Present (Anod) = Class_Present (Aspect) then
2830 if not Class_Present (Anod) then
2831 Error_Msg_NE
2832 ("aspect% for & previously given#",
2833 Id, E);
2834 else
2835 Error_Msg_NE
2836 ("aspect `%''Class` for & previously given#",
2837 Id, E);
2838 end if;
2839 end if;
2840 end if;
2841
2842 Next (Anod);
2843 end loop;
2844 end if;
2845
2846 -- Check some general restrictions on language defined aspects
2847
2848 if not Implementation_Defined_Aspect (A_Id)
2849 or else A_Id = Aspect_Async_Readers
2850 or else A_Id = Aspect_Async_Writers
2851 or else A_Id = Aspect_Effective_Reads
2852 or else A_Id = Aspect_Effective_Reads
2853 then
2854 Error_Msg_Name_1 := Nam;
2855
2856 -- Not allowed for renaming declarations. Examine the original
2857 -- node because a subprogram renaming may have been rewritten
2858 -- as a body.
2859
2860 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
2861 Error_Msg_N
2862 ("aspect % not allowed for renaming declaration",
2863 Aspect);
2864 end if;
2865
2866 -- Not allowed for formal type declarations in previous
2867 -- versions of the language. Allowed for them only for
2868 -- shared variable control aspects.
2869
2870 if Nkind (N) = N_Formal_Type_Declaration then
2871 if Ada_Version < Ada_2020 then
2872 Error_Msg_N
2873 ("aspect % not allowed for formal type declaration",
2874 Aspect);
2875
2876 elsif A_Id /= Aspect_Atomic
2877 and then A_Id /= Aspect_Volatile
2878 and then A_Id /= Aspect_Independent
2879 and then A_Id /= Aspect_Atomic_Components
2880 and then A_Id /= Aspect_Independent_Components
2881 and then A_Id /= Aspect_Volatile_Components
2882 and then A_Id /= Aspect_Async_Readers
2883 and then A_Id /= Aspect_Async_Writers
2884 and then A_Id /= Aspect_Effective_Reads
2885 and then A_Id /= Aspect_Effective_Reads
2886 then
2887 Error_Msg_N
2888 ("aspect % not allowed for formal type declaration",
2889 Aspect);
2890 end if;
2891 end if;
2892 end if;
2893
2894 -- Copy expression for later processing by the procedures
2895 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2896
2897 Set_Entity (Id, New_Copy_Tree (Expr));
2898
2899 -- Set Delay_Required as appropriate to aspect
2900
2901 case Aspect_Delay (A_Id) is
2902 when Always_Delay =>
2903 -- For Boolean aspects, do not delay if no expression
2904
2905 if A_Id in Boolean_Aspects | Library_Unit_Aspects then
2906 Delay_Required := Present (Expr);
2907 else
2908 Delay_Required := True;
2909 end if;
2910
2911 when Never_Delay =>
2912 Delay_Required := False;
2913
2914 when Rep_Aspect =>
2915
2916 -- For Boolean aspects, do not delay if no expression except
2917 -- for Full_Access_Only because we need to process it after
2918 -- Volatile and Atomic, which can be independently delayed.
2919
2920 if A_Id in Boolean_Aspects
2921 and then A_Id /= Aspect_Full_Access_Only
2922 and then No (Expr)
2923 then
2924 Delay_Required := False;
2925
2926 -- For non-Boolean aspects, if the expression has the form
2927 -- of an integer literal, then do not delay, since we know
2928 -- the value cannot change. This optimization catches most
2929 -- rep clause cases.
2930
2931 elsif A_Id not in Boolean_Aspects
2932 and then Present (Expr)
2933 and then Nkind (Expr) = N_Integer_Literal
2934 then
2935 Delay_Required := False;
2936
2937 -- For Alignment and various Size aspects, do not delay for
2938 -- an attribute reference whose prefix is Standard, for
2939 -- example Standard'Maximum_Alignment or Standard'Word_Size.
2940
2941 elsif (A_Id = Aspect_Alignment
2942 or else A_Id = Aspect_Component_Size
2943 or else A_Id = Aspect_Object_Size
2944 or else A_Id = Aspect_Size
2945 or else A_Id = Aspect_Value_Size)
2946 and then Present (Expr)
2947 and then Nkind (Expr) = N_Attribute_Reference
2948 and then Nkind (Prefix (Expr)) = N_Identifier
2949 and then Chars (Prefix (Expr)) = Name_Standard
2950 then
2951 Delay_Required := False;
2952
2953 -- All other cases are delayed
2954
2955 else
2956 Delay_Required := True;
2957 Set_Has_Delayed_Rep_Aspects (E);
2958 end if;
2959 end case;
2960
2961 -- Check 13.1(9.2/5): A representation aspect of a subtype or type
2962 -- shall not be specified (whether by a representation item or an
2963 -- aspect_specification) before the type is completely defined
2964 -- (see 3.11.1).
2965
2966 if Is_Representation_Aspect (A_Id)
2967 and then Rep_Item_Too_Early (E, N)
2968 then
2969 goto Continue;
2970 end if;
2971
2972 -- Processing based on specific aspect
2973
2974 case A_Id is
2975 when Aspect_Unimplemented =>
2976 null; -- ??? temp for now
2977
2978 -- No_Aspect should be impossible
2979
2980 when No_Aspect =>
2981 raise Program_Error;
2982
2983 -- Case 1: Aspects corresponding to attribute definition
2984 -- clauses.
2985
2986 when Aspect_Address
2987 | Aspect_Alignment
2988 | Aspect_Bit_Order
2989 | Aspect_Component_Size
2990 | Aspect_Constant_Indexing
2991 | Aspect_Default_Iterator
2992 | Aspect_Dispatching_Domain
2993 | Aspect_External_Tag
2994 | Aspect_Input
2995 | Aspect_Iterable
2996 | Aspect_Iterator_Element
2997 | Aspect_Machine_Radix
2998 | Aspect_Object_Size
2999 | Aspect_Output
3000 | Aspect_Put_Image
3001 | Aspect_Read
3002 | Aspect_Scalar_Storage_Order
3003 | Aspect_Simple_Storage_Pool
3004 | Aspect_Size
3005 | Aspect_Small
3006 | Aspect_Storage_Pool
3007 | Aspect_Stream_Size
3008 | Aspect_Value_Size
3009 | Aspect_Variable_Indexing
3010 | Aspect_Write
3011 =>
3012 -- Indexing aspects apply only to tagged type
3013
3014 if (A_Id = Aspect_Constant_Indexing
3015 or else
3016 A_Id = Aspect_Variable_Indexing)
3017 and then not (Is_Type (E)
3018 and then Is_Tagged_Type (E))
3019 then
3020 Error_Msg_N
3021 ("indexing aspect can only apply to a tagged type",
3022 Aspect);
3023 goto Continue;
3024 end if;
3025
3026 -- For the case of aspect Address, we don't consider that we
3027 -- know the entity is never set in the source, since it is
3028 -- is likely aliasing is occurring.
3029
3030 -- Note: one might think that the analysis of the resulting
3031 -- attribute definition clause would take care of that, but
3032 -- that's not the case since it won't be from source.
3033
3034 if A_Id = Aspect_Address then
3035 Set_Never_Set_In_Source (E, False);
3036 end if;
3037
3038 -- Correctness of the profile of a stream operation is
3039 -- verified at the freeze point, but we must detect the
3040 -- illegal specification of this aspect for a subtype now,
3041 -- to prevent malformed rep_item chains.
3042
3043 if A_Id = Aspect_Input or else
3044 A_Id = Aspect_Output or else
3045 A_Id = Aspect_Read or else
3046 A_Id = Aspect_Write
3047 then
3048 if not Is_First_Subtype (E) then
3049 Error_Msg_N
3050 ("local name must be a first subtype", Aspect);
3051 goto Continue;
3052
3053 -- If stream aspect applies to the class-wide type,
3054 -- the generated attribute definition applies to the
3055 -- class-wide type as well.
3056
3057 elsif Class_Present (Aspect) then
3058 Ent :=
3059 Make_Attribute_Reference (Loc,
3060 Prefix => Ent,
3061 Attribute_Name => Name_Class);
3062 end if;
3063 end if;
3064
3065 -- Construct the attribute_definition_clause. The expression
3066 -- in the aspect specification is simply shared with the
3067 -- constructed attribute, because it will be fully analyzed
3068 -- when the attribute is processed.
3069
3070 Aitem :=
3071 Make_Attribute_Definition_Clause (Loc,
3072 Name => Ent,
3073 Chars => Chars (Id),
3074 Expression => Relocate_Node (Expr));
3075
3076 -- If the address is specified, then we treat the entity as
3077 -- referenced, to avoid spurious warnings. This is analogous
3078 -- to what is done with an attribute definition clause, but
3079 -- here we don't want to generate a reference because this
3080 -- is the point of definition of the entity.
3081
3082 if A_Id = Aspect_Address then
3083 Set_Referenced (E);
3084 end if;
3085
3086 -- Case 2: Aspects corresponding to pragmas
3087
3088 -- Case 2a: Aspects corresponding to pragmas with two
3089 -- arguments, where the first argument is a local name
3090 -- referring to the entity, and the second argument is the
3091 -- aspect definition expression.
3092
3093 -- Linker_Section
3094
3095 when Aspect_Linker_Section =>
3096 Aitem := Make_Aitem_Pragma
3097 (Pragma_Argument_Associations => New_List (
3098 Make_Pragma_Argument_Association (Loc,
3099 Expression => New_Occurrence_Of (E, Loc)),
3100 Make_Pragma_Argument_Association (Sloc (Expr),
3101 Expression => Relocate_Node (Expr))),
3102 Pragma_Name => Chars (Id));
3103
3104 -- Linker_Section does not need delaying, as its argument
3105 -- must be a static string. Furthermore, if applied to
3106 -- an object with an explicit initialization, the object
3107 -- must be frozen in order to elaborate the initialization
3108 -- code. (This is already done for types with implicit
3109 -- initialization, such as protected types.)
3110
3111 if Nkind (N) = N_Object_Declaration
3112 and then Has_Init_Expression (N)
3113 then
3114 Delay_Required := False;
3115 end if;
3116
3117 -- Synchronization
3118
3119 -- Corresponds to pragma Implemented, construct the pragma
3120
3121 when Aspect_Synchronization =>
3122 Aitem := Make_Aitem_Pragma
3123 (Pragma_Argument_Associations => New_List (
3124 Make_Pragma_Argument_Association (Loc,
3125 Expression => New_Occurrence_Of (E, Loc)),
3126 Make_Pragma_Argument_Association (Sloc (Expr),
3127 Expression => Relocate_Node (Expr))),
3128 Pragma_Name => Name_Implemented);
3129
3130 -- Attach_Handler
3131
3132 when Aspect_Attach_Handler =>
3133 Aitem := Make_Aitem_Pragma
3134 (Pragma_Argument_Associations => New_List (
3135 Make_Pragma_Argument_Association (Sloc (Ent),
3136 Expression => Ent),
3137 Make_Pragma_Argument_Association (Sloc (Expr),
3138 Expression => Relocate_Node (Expr))),
3139 Pragma_Name => Name_Attach_Handler);
3140
3141 -- We need to insert this pragma into the tree to get proper
3142 -- processing and to look valid from a placement viewpoint.
3143
3144 Insert_Pragma (Aitem);
3145 goto Continue;
3146
3147 -- Dynamic_Predicate, Predicate, Static_Predicate
3148
3149 when Aspect_Dynamic_Predicate
3150 | Aspect_Predicate
3151 | Aspect_Static_Predicate
3152 =>
3153 -- These aspects apply only to subtypes
3154
3155 if not Is_Type (E) then
3156 Error_Msg_N
3157 ("predicate can only be specified for a subtype",
3158 Aspect);
3159 goto Continue;
3160
3161 elsif Is_Incomplete_Type (E) then
3162 Error_Msg_N
3163 ("predicate cannot apply to incomplete view", Aspect);
3164
3165 elsif Is_Generic_Type (E) then
3166 Error_Msg_N
3167 ("predicate cannot apply to formal type", Aspect);
3168 goto Continue;
3169 end if;
3170
3171 -- Construct the pragma (always a pragma Predicate, with
3172 -- flags recording whether it is static/dynamic). We also
3173 -- set flags recording this in the type itself.
3174
3175 Aitem := Make_Aitem_Pragma
3176 (Pragma_Argument_Associations => New_List (
3177 Make_Pragma_Argument_Association (Sloc (Ent),
3178 Expression => Ent),
3179 Make_Pragma_Argument_Association (Sloc (Expr),
3180 Expression => Relocate_Node (Expr))),
3181 Pragma_Name => Name_Predicate);
3182
3183 -- Mark type has predicates, and remember what kind of
3184 -- aspect lead to this predicate (we need this to access
3185 -- the right set of check policies later on).
3186
3187 Set_Has_Predicates (E);
3188
3189 if A_Id = Aspect_Dynamic_Predicate then
3190 Set_Has_Dynamic_Predicate_Aspect (E);
3191
3192 -- If the entity has a dynamic predicate, any inherited
3193 -- static predicate becomes dynamic as well, and the
3194 -- predicate function includes the conjunction of both.
3195
3196 Set_Has_Static_Predicate_Aspect (E, False);
3197
3198 elsif A_Id = Aspect_Static_Predicate then
3199 Set_Has_Static_Predicate_Aspect (E);
3200 end if;
3201
3202 -- If the type is private, indicate that its completion
3203 -- has a freeze node, because that is the one that will
3204 -- be visible at freeze time.
3205
3206 if Is_Private_Type (E) and then Present (Full_View (E)) then
3207 Set_Has_Predicates (Full_View (E));
3208
3209 if A_Id = Aspect_Dynamic_Predicate then
3210 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
3211 elsif A_Id = Aspect_Static_Predicate then
3212 Set_Has_Static_Predicate_Aspect (Full_View (E));
3213 end if;
3214
3215 Set_Has_Delayed_Aspects (Full_View (E));
3216 Ensure_Freeze_Node (Full_View (E));
3217
3218 -- If there is an Underlying_Full_View, also create a
3219 -- freeze node for that one.
3220
3221 if Is_Private_Type (Full_View (E)) then
3222 declare
3223 U_Full : constant Entity_Id :=
3224 Underlying_Full_View (Full_View (E));
3225 begin
3226 if Present (U_Full) then
3227 Set_Has_Delayed_Aspects (U_Full);
3228 Ensure_Freeze_Node (U_Full);
3229 end if;
3230 end;
3231 end if;
3232 end if;
3233
3234 -- Predicate_Failure
3235
3236 when Aspect_Predicate_Failure =>
3237
3238 -- This aspect applies only to subtypes
3239
3240 if not Is_Type (E) then
3241 Error_Msg_N
3242 ("predicate can only be specified for a subtype",
3243 Aspect);
3244 goto Continue;
3245
3246 elsif Is_Incomplete_Type (E) then
3247 Error_Msg_N
3248 ("predicate cannot apply to incomplete view", Aspect);
3249 goto Continue;
3250
3251 elsif not Has_Predicates (E) then
3252 Error_Msg_N
3253 ("Predicate_Failure requires previous predicate" &
3254 " specification", Aspect);
3255 goto Continue;
3256 end if;
3257
3258 -- Construct the pragma
3259
3260 Aitem := Make_Aitem_Pragma
3261 (Pragma_Argument_Associations => New_List (
3262 Make_Pragma_Argument_Association (Sloc (Ent),
3263 Expression => Ent),
3264 Make_Pragma_Argument_Association (Sloc (Expr),
3265 Expression => Relocate_Node (Expr))),
3266 Pragma_Name => Name_Predicate_Failure);
3267
3268 -- Case 2b: Aspects corresponding to pragmas with two
3269 -- arguments, where the second argument is a local name
3270 -- referring to the entity, and the first argument is the
3271 -- aspect definition expression.
3272
3273 -- Convention
3274
3275 when Aspect_Convention =>
3276 Analyze_Aspect_Convention;
3277 goto Continue;
3278
3279 -- External_Name, Link_Name
3280
3281 when Aspect_External_Name
3282 | Aspect_Link_Name
3283 =>
3284 Analyze_Aspect_External_Link_Name;
3285 goto Continue;
3286
3287 -- CPU, Interrupt_Priority, Priority
3288
3289 -- These three aspects can be specified for a subprogram spec
3290 -- or body, in which case we analyze the expression and export
3291 -- the value of the aspect.
3292
3293 -- Previously, we generated an equivalent pragma for bodies
3294 -- (note that the specs cannot contain these pragmas). The
3295 -- pragma was inserted ahead of local declarations, rather than
3296 -- after the body. This leads to a certain duplication between
3297 -- the processing performed for the aspect and the pragma, but
3298 -- given the straightforward handling required it is simpler
3299 -- to duplicate than to translate the aspect in the spec into
3300 -- a pragma in the declarative part of the body.
3301
3302 when Aspect_CPU
3303 | Aspect_Interrupt_Priority
3304 | Aspect_Priority
3305 =>
3306 if Nkind (N) in N_Subprogram_Body | N_Subprogram_Declaration
3307 then
3308 -- Analyze the aspect expression
3309
3310 Analyze_And_Resolve (Expr, Standard_Integer);
3311
3312 -- Interrupt_Priority aspect not allowed for main
3313 -- subprograms. RM D.1 does not forbid this explicitly,
3314 -- but RM J.15.11(6/3) does not permit pragma
3315 -- Interrupt_Priority for subprograms.
3316
3317 if A_Id = Aspect_Interrupt_Priority then
3318 Error_Msg_N
3319 ("Interrupt_Priority aspect cannot apply to "
3320 & "subprogram", Expr);
3321
3322 -- The expression must be static
3323
3324 elsif not Is_OK_Static_Expression (Expr) then
3325 Flag_Non_Static_Expr
3326 ("aspect requires static expression!", Expr);
3327
3328 -- Check whether this is the main subprogram. Issue a
3329 -- warning only if it is obviously not a main program
3330 -- (when it has parameters or when the subprogram is
3331 -- within a package).
3332
3333 elsif Present (Parameter_Specifications
3334 (Specification (N)))
3335 or else not Is_Compilation_Unit (Defining_Entity (N))
3336 then
3337 -- See RM D.1(14/3) and D.16(12/3)
3338
3339 Error_Msg_N
3340 ("aspect applied to subprogram other than the "
3341 & "main subprogram has no effect??", Expr);
3342
3343 -- Otherwise check in range and export the value
3344
3345 -- For the CPU aspect
3346
3347 elsif A_Id = Aspect_CPU then
3348 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
3349
3350 -- Value is correct so we export the value to make
3351 -- it available at execution time.
3352
3353 Set_Main_CPU
3354 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3355
3356 else
3357 Error_Msg_N
3358 ("main subprogram CPU is out of range", Expr);
3359 end if;
3360
3361 -- For the Priority aspect
3362
3363 elsif A_Id = Aspect_Priority then
3364 if Is_In_Range (Expr, RTE (RE_Priority)) then
3365
3366 -- Value is correct so we export the value to make
3367 -- it available at execution time.
3368
3369 Set_Main_Priority
3370 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3371
3372 -- Ignore pragma if Relaxed_RM_Semantics to support
3373 -- other targets/non GNAT compilers.
3374
3375 elsif not Relaxed_RM_Semantics then
3376 Error_Msg_N
3377 ("main subprogram priority is out of range",
3378 Expr);
3379 end if;
3380 end if;
3381
3382 -- Load an arbitrary entity from System.Tasking.Stages
3383 -- or System.Tasking.Restricted.Stages (depending on
3384 -- the supported profile) to make sure that one of these
3385 -- packages is implicitly with'ed, since we need to have
3386 -- the tasking run time active for the pragma Priority to
3387 -- have any effect. Previously we with'ed the package
3388 -- System.Tasking, but this package does not trigger the
3389 -- required initialization of the run-time library.
3390
3391 declare
3392 Discard : Entity_Id;
3393 begin
3394 if Restricted_Profile then
3395 Discard := RTE (RE_Activate_Restricted_Tasks);
3396 else
3397 Discard := RTE (RE_Activate_Tasks);
3398 end if;
3399 end;
3400
3401 -- Handling for these aspects in subprograms is complete
3402
3403 goto Continue;
3404
3405 -- For task and protected types pass the aspect as an
3406 -- attribute.
3407
3408 else
3409 Aitem :=
3410 Make_Attribute_Definition_Clause (Loc,
3411 Name => Ent,
3412 Chars => Chars (Id),
3413 Expression => Relocate_Node (Expr));
3414 end if;
3415
3416 -- Suppress/Unsuppress
3417
3418 when Aspect_Suppress
3419 | Aspect_Unsuppress
3420 =>
3421 Aitem := Make_Aitem_Pragma
3422 (Pragma_Argument_Associations => New_List (
3423 Make_Pragma_Argument_Association (Loc,
3424 Expression => Relocate_Node (Expr)),
3425 Make_Pragma_Argument_Association (Sloc (Expr),
3426 Expression => New_Occurrence_Of (E, Loc))),
3427 Pragma_Name => Chars (Id));
3428
3429 Delay_Required := False;
3430
3431 -- Warnings
3432
3433 when Aspect_Warnings =>
3434 Aitem := Make_Aitem_Pragma
3435 (Pragma_Argument_Associations => New_List (
3436 Make_Pragma_Argument_Association (Sloc (Expr),
3437 Expression => Relocate_Node (Expr)),
3438 Make_Pragma_Argument_Association (Loc,
3439 Expression => New_Occurrence_Of (E, Loc))),
3440 Pragma_Name => Chars (Id));
3441
3442 Decorate (Aspect, Aitem);
3443 Insert_Pragma (Aitem);
3444 goto Continue;
3445
3446 -- Case 2c: Aspects corresponding to pragmas with three
3447 -- arguments.
3448
3449 -- Invariant aspects have a first argument that references the
3450 -- entity, a second argument that is the expression and a third
3451 -- argument that is an appropriate message.
3452
3453 -- Invariant, Type_Invariant
3454
3455 when Aspect_Invariant
3456 | Aspect_Type_Invariant
3457 =>
3458 -- Analysis of the pragma will verify placement legality:
3459 -- an invariant must apply to a private type, or appear in
3460 -- the private part of a spec and apply to a completion.
3461
3462 Aitem := Make_Aitem_Pragma
3463 (Pragma_Argument_Associations => New_List (
3464 Make_Pragma_Argument_Association (Sloc (Ent),
3465 Expression => Ent),
3466 Make_Pragma_Argument_Association (Sloc (Expr),
3467 Expression => Relocate_Node (Expr))),
3468 Pragma_Name => Name_Invariant);
3469
3470 -- Add message unless exception messages are suppressed
3471
3472 if not Opt.Exception_Locations_Suppressed then
3473 Append_To (Pragma_Argument_Associations (Aitem),
3474 Make_Pragma_Argument_Association (Eloc,
3475 Chars => Name_Message,
3476 Expression =>
3477 Make_String_Literal (Eloc,
3478 Strval => "failed invariant from "
3479 & Build_Location_String (Eloc))));
3480 end if;
3481
3482 -- For Invariant case, insert immediately after the entity
3483 -- declaration. We do not have to worry about delay issues
3484 -- since the pragma processing takes care of this.
3485
3486 Delay_Required := False;
3487
3488 -- Case 2d : Aspects that correspond to a pragma with one
3489 -- argument.
3490
3491 -- Abstract_State
3492
3493 -- Aspect Abstract_State introduces implicit declarations for
3494 -- all state abstraction entities it defines. To emulate this
3495 -- behavior, insert the pragma at the beginning of the visible
3496 -- declarations of the related package so that it is analyzed
3497 -- immediately.
3498
3499 when Aspect_Abstract_State => Abstract_State : declare
3500 Context : Node_Id := N;
3501
3502 begin
3503 -- When aspect Abstract_State appears on a generic package,
3504 -- it is propageted to the package instance. The context in
3505 -- this case is the instance spec.
3506
3507 if Nkind (Context) = N_Package_Instantiation then
3508 Context := Instance_Spec (Context);
3509 end if;
3510
3511 if Nkind (Context) in N_Generic_Package_Declaration
3512 | N_Package_Declaration
3513 then
3514 Aitem := Make_Aitem_Pragma
3515 (Pragma_Argument_Associations => New_List (
3516 Make_Pragma_Argument_Association (Loc,
3517 Expression => Relocate_Node (Expr))),
3518 Pragma_Name => Name_Abstract_State);
3519
3520 Decorate (Aspect, Aitem);
3521 Insert_Pragma
3522 (Prag => Aitem,
3523 Is_Instance =>
3524 Is_Generic_Instance (Defining_Entity (Context)));
3525
3526 else
3527 Error_Msg_NE
3528 ("aspect & must apply to a package declaration",
3529 Aspect, Id);
3530 end if;
3531
3532 goto Continue;
3533 end Abstract_State;
3534
3535 -- Aspect Async_Readers is never delayed because it is
3536 -- equivalent to a source pragma which appears after the
3537 -- related object declaration.
3538
3539 when Aspect_Async_Readers =>
3540 Aitem := Make_Aitem_Pragma
3541 (Pragma_Argument_Associations => New_List (
3542 Make_Pragma_Argument_Association (Loc,
3543 Expression => Relocate_Node (Expr))),
3544 Pragma_Name => Name_Async_Readers);
3545
3546 Decorate (Aspect, Aitem);
3547 Insert_Pragma (Aitem);
3548 goto Continue;
3549
3550 -- Aspect Async_Writers is never delayed because it is
3551 -- equivalent to a source pragma which appears after the
3552 -- related object declaration.
3553
3554 when Aspect_Async_Writers =>
3555 Aitem := Make_Aitem_Pragma
3556 (Pragma_Argument_Associations => New_List (
3557 Make_Pragma_Argument_Association (Loc,
3558 Expression => Relocate_Node (Expr))),
3559 Pragma_Name => Name_Async_Writers);
3560
3561 Decorate (Aspect, Aitem);
3562 Insert_Pragma (Aitem);
3563 goto Continue;
3564
3565 -- Aspect Constant_After_Elaboration is never delayed because
3566 -- it is equivalent to a source pragma which appears after the
3567 -- related object declaration.
3568
3569 when Aspect_Constant_After_Elaboration =>
3570 Aitem := Make_Aitem_Pragma
3571 (Pragma_Argument_Associations => New_List (
3572 Make_Pragma_Argument_Association (Loc,
3573 Expression => Relocate_Node (Expr))),
3574 Pragma_Name =>
3575 Name_Constant_After_Elaboration);
3576
3577 Decorate (Aspect, Aitem);
3578 Insert_Pragma (Aitem);
3579 goto Continue;
3580
3581 -- Aspect Default_Internal_Condition is never delayed because
3582 -- it is equivalent to a source pragma which appears after the
3583 -- related private type. To deal with forward references, the
3584 -- generated pragma is stored in the rep chain of the related
3585 -- private type as types do not carry contracts. The pragma is
3586 -- wrapped inside of a procedure at the freeze point of the
3587 -- private type's full view.
3588
3589 when Aspect_Default_Initial_Condition =>
3590 Aitem := Make_Aitem_Pragma
3591 (Pragma_Argument_Associations => New_List (
3592 Make_Pragma_Argument_Association (Loc,
3593 Expression => Relocate_Node (Expr))),
3594 Pragma_Name =>
3595 Name_Default_Initial_Condition);
3596
3597 Decorate (Aspect, Aitem);
3598 Insert_Pragma (Aitem);
3599 goto Continue;
3600
3601 -- Default_Storage_Pool
3602
3603 when Aspect_Default_Storage_Pool =>
3604 Aitem := Make_Aitem_Pragma
3605 (Pragma_Argument_Associations => New_List (
3606 Make_Pragma_Argument_Association (Loc,
3607 Expression => Relocate_Node (Expr))),
3608 Pragma_Name =>
3609 Name_Default_Storage_Pool);
3610
3611 Decorate (Aspect, Aitem);
3612 Insert_Pragma (Aitem);
3613 goto Continue;
3614
3615 -- Depends
3616
3617 -- Aspect Depends is never delayed because it is equivalent to
3618 -- a source pragma which appears after the related subprogram.
3619 -- To deal with forward references, the generated pragma is
3620 -- stored in the contract of the related subprogram and later
3621 -- analyzed at the end of the declarative region. See routine
3622 -- Analyze_Depends_In_Decl_Part for details.
3623
3624 when Aspect_Depends =>
3625 Aitem := Make_Aitem_Pragma
3626 (Pragma_Argument_Associations => New_List (
3627 Make_Pragma_Argument_Association (Loc,
3628 Expression => Relocate_Node (Expr))),
3629 Pragma_Name => Name_Depends);
3630
3631 Decorate (Aspect, Aitem);
3632 Insert_Pragma (Aitem);
3633 goto Continue;
3634
3635 -- Aspect Effective_Reads is never delayed because it is
3636 -- equivalent to a source pragma which appears after the
3637 -- related object declaration.
3638
3639 when Aspect_Effective_Reads =>
3640 Aitem := Make_Aitem_Pragma
3641 (Pragma_Argument_Associations => New_List (
3642 Make_Pragma_Argument_Association (Loc,
3643 Expression => Relocate_Node (Expr))),
3644 Pragma_Name => Name_Effective_Reads);
3645
3646 Decorate (Aspect, Aitem);
3647 Insert_Pragma (Aitem);
3648 goto Continue;
3649
3650 -- Aspect Effective_Writes is never delayed because it is
3651 -- equivalent to a source pragma which appears after the
3652 -- related object declaration.
3653
3654 when Aspect_Effective_Writes =>
3655 Aitem := Make_Aitem_Pragma
3656 (Pragma_Argument_Associations => New_List (
3657 Make_Pragma_Argument_Association (Loc,
3658 Expression => Relocate_Node (Expr))),
3659 Pragma_Name => Name_Effective_Writes);
3660
3661 Decorate (Aspect, Aitem);
3662 Insert_Pragma (Aitem);
3663 goto Continue;
3664
3665 -- Aspect Extensions_Visible is never delayed because it is
3666 -- equivalent to a source pragma which appears after the
3667 -- related subprogram.
3668
3669 when Aspect_Extensions_Visible =>
3670 Aitem := Make_Aitem_Pragma
3671 (Pragma_Argument_Associations => New_List (
3672 Make_Pragma_Argument_Association (Loc,
3673 Expression => Relocate_Node (Expr))),
3674 Pragma_Name => Name_Extensions_Visible);
3675
3676 Decorate (Aspect, Aitem);
3677 Insert_Pragma (Aitem);
3678 goto Continue;
3679
3680 -- Aspect Ghost is never delayed because it is equivalent to a
3681 -- source pragma which appears at the top of [generic] package
3682 -- declarations or after an object, a [generic] subprogram, or
3683 -- a type declaration.
3684
3685 when Aspect_Ghost =>
3686 Aitem := Make_Aitem_Pragma
3687 (Pragma_Argument_Associations => New_List (
3688 Make_Pragma_Argument_Association (Loc,
3689 Expression => Relocate_Node (Expr))),
3690 Pragma_Name => Name_Ghost);
3691
3692 Decorate (Aspect, Aitem);
3693 Insert_Pragma (Aitem);
3694 goto Continue;
3695
3696 -- Global
3697
3698 -- Aspect Global is never delayed because it is equivalent to
3699 -- a source pragma which appears after the related subprogram.
3700 -- To deal with forward references, the generated pragma is
3701 -- stored in the contract of the related subprogram and later
3702 -- analyzed at the end of the declarative region. See routine
3703 -- Analyze_Global_In_Decl_Part for details.
3704
3705 when Aspect_Global =>
3706 Aitem := Make_Aitem_Pragma
3707 (Pragma_Argument_Associations => New_List (
3708 Make_Pragma_Argument_Association (Loc,
3709 Expression => Relocate_Node (Expr))),
3710 Pragma_Name => Name_Global);
3711
3712 Decorate (Aspect, Aitem);
3713 Insert_Pragma (Aitem);
3714 goto Continue;
3715
3716 -- Initial_Condition
3717
3718 -- Aspect Initial_Condition is never delayed because it is
3719 -- equivalent to a source pragma which appears after the
3720 -- related package. To deal with forward references, the
3721 -- generated pragma is stored in the contract of the related
3722 -- package and later analyzed at the end of the declarative
3723 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
3724 -- for details.
3725
3726 when Aspect_Initial_Condition => Initial_Condition : declare
3727 Context : Node_Id := N;
3728
3729 begin
3730 -- When aspect Initial_Condition appears on a generic
3731 -- package, it is propageted to the package instance. The
3732 -- context in this case is the instance spec.
3733
3734 if Nkind (Context) = N_Package_Instantiation then
3735 Context := Instance_Spec (Context);
3736 end if;
3737
3738 if Nkind (Context) in N_Generic_Package_Declaration
3739 | N_Package_Declaration
3740 then
3741 Aitem := Make_Aitem_Pragma
3742 (Pragma_Argument_Associations => New_List (
3743 Make_Pragma_Argument_Association (Loc,
3744 Expression => Relocate_Node (Expr))),
3745 Pragma_Name =>
3746 Name_Initial_Condition);
3747
3748 Decorate (Aspect, Aitem);
3749 Insert_Pragma
3750 (Prag => Aitem,
3751 Is_Instance =>
3752 Is_Generic_Instance (Defining_Entity (Context)));
3753
3754 -- Otherwise the context is illegal
3755
3756 else
3757 Error_Msg_NE
3758 ("aspect & must apply to a package declaration",
3759 Aspect, Id);
3760 end if;
3761
3762 goto Continue;
3763 end Initial_Condition;
3764
3765 -- Initializes
3766
3767 -- Aspect Initializes is never delayed because it is equivalent
3768 -- to a source pragma appearing after the related package. To
3769 -- deal with forward references, the generated pragma is stored
3770 -- in the contract of the related package and later analyzed at
3771 -- the end of the declarative region. For details, see routine
3772 -- Analyze_Initializes_In_Decl_Part.
3773
3774 when Aspect_Initializes => Initializes : declare
3775 Context : Node_Id := N;
3776
3777 begin
3778 -- When aspect Initializes appears on a generic package,
3779 -- it is propageted to the package instance. The context
3780 -- in this case is the instance spec.
3781
3782 if Nkind (Context) = N_Package_Instantiation then
3783 Context := Instance_Spec (Context);
3784 end if;
3785
3786 if Nkind (Context) in N_Generic_Package_Declaration
3787 | N_Package_Declaration
3788 then
3789 Aitem := Make_Aitem_Pragma
3790 (Pragma_Argument_Associations => New_List (
3791 Make_Pragma_Argument_Association (Loc,
3792 Expression => Relocate_Node (Expr))),
3793 Pragma_Name => Name_Initializes);
3794
3795 Decorate (Aspect, Aitem);
3796 Insert_Pragma
3797 (Prag => Aitem,
3798 Is_Instance =>
3799 Is_Generic_Instance (Defining_Entity (Context)));
3800
3801 -- Otherwise the context is illegal
3802
3803 else
3804 Error_Msg_NE
3805 ("aspect & must apply to a package declaration",
3806 Aspect, Id);
3807 end if;
3808
3809 goto Continue;
3810 end Initializes;
3811
3812 -- Max_Entry_Queue_Depth
3813
3814 when Aspect_Max_Entry_Queue_Depth =>
3815 Aitem := Make_Aitem_Pragma
3816 (Pragma_Argument_Associations => New_List (
3817 Make_Pragma_Argument_Association (Loc,
3818 Expression => Relocate_Node (Expr))),
3819 Pragma_Name => Name_Max_Entry_Queue_Depth);
3820
3821 Decorate (Aspect, Aitem);
3822 Insert_Pragma (Aitem);
3823 goto Continue;
3824
3825 -- Max_Entry_Queue_Length
3826
3827 when Aspect_Max_Entry_Queue_Length =>
3828 Aitem := Make_Aitem_Pragma
3829 (Pragma_Argument_Associations => New_List (
3830 Make_Pragma_Argument_Association (Loc,
3831 Expression => Relocate_Node (Expr))),
3832 Pragma_Name => Name_Max_Entry_Queue_Length);
3833
3834 Decorate (Aspect, Aitem);
3835 Insert_Pragma (Aitem);
3836 goto Continue;
3837
3838 -- Max_Queue_Length
3839
3840 when Aspect_Max_Queue_Length =>
3841 Aitem := Make_Aitem_Pragma
3842 (Pragma_Argument_Associations => New_List (
3843 Make_Pragma_Argument_Association (Loc,
3844 Expression => Relocate_Node (Expr))),
3845 Pragma_Name => Name_Max_Queue_Length);
3846
3847 Decorate (Aspect, Aitem);
3848 Insert_Pragma (Aitem);
3849 goto Continue;
3850
3851 -- Aspect No_Caching is never delayed because it is equivalent
3852 -- to a source pragma which appears after the related object
3853 -- declaration.
3854
3855 when Aspect_No_Caching =>
3856 Aitem := Make_Aitem_Pragma
3857 (Pragma_Argument_Associations => New_List (
3858 Make_Pragma_Argument_Association (Loc,
3859 Expression => Relocate_Node (Expr))),
3860 Pragma_Name => Name_No_Caching);
3861
3862 Decorate (Aspect, Aitem);
3863 Insert_Pragma (Aitem);
3864 goto Continue;
3865
3866 -- Obsolescent
3867
3868 when Aspect_Obsolescent => declare
3869 Args : List_Id;
3870
3871 begin
3872 if No (Expr) then
3873 Args := No_List;
3874 else
3875 Args := New_List (
3876 Make_Pragma_Argument_Association (Sloc (Expr),
3877 Expression => Relocate_Node (Expr)));
3878 end if;
3879
3880 Aitem := Make_Aitem_Pragma
3881 (Pragma_Argument_Associations => Args,
3882 Pragma_Name => Chars (Id));
3883 end;
3884
3885 -- Part_Of
3886
3887 when Aspect_Part_Of =>
3888 if Nkind (N) in N_Object_Declaration
3889 | N_Package_Instantiation
3890 or else Is_Single_Concurrent_Type_Declaration (N)
3891 then
3892 Aitem := Make_Aitem_Pragma
3893 (Pragma_Argument_Associations => New_List (
3894 Make_Pragma_Argument_Association (Loc,
3895 Expression => Relocate_Node (Expr))),
3896 Pragma_Name => Name_Part_Of);
3897
3898 Decorate (Aspect, Aitem);
3899 Insert_Pragma (Aitem);
3900
3901 else
3902 Error_Msg_NE
3903 ("aspect & must apply to package instantiation, "
3904 & "object, single protected type or single task type",
3905 Aspect, Id);
3906 end if;
3907
3908 goto Continue;
3909
3910 -- SPARK_Mode
3911
3912 when Aspect_SPARK_Mode =>
3913 Aitem := Make_Aitem_Pragma
3914 (Pragma_Argument_Associations => New_List (
3915 Make_Pragma_Argument_Association (Loc,
3916 Expression => Relocate_Node (Expr))),
3917 Pragma_Name => Name_SPARK_Mode);
3918
3919 Decorate (Aspect, Aitem);
3920 Insert_Pragma (Aitem);
3921 goto Continue;
3922
3923 -- Refined_Depends
3924
3925 -- Aspect Refined_Depends is never delayed because it is
3926 -- equivalent to a source pragma which appears in the
3927 -- declarations of the related subprogram body. To deal with
3928 -- forward references, the generated pragma is stored in the
3929 -- contract of the related subprogram body and later analyzed
3930 -- at the end of the declarative region. For details, see
3931 -- routine Analyze_Refined_Depends_In_Decl_Part.
3932
3933 when Aspect_Refined_Depends =>
3934 Aitem := Make_Aitem_Pragma
3935 (Pragma_Argument_Associations => New_List (
3936 Make_Pragma_Argument_Association (Loc,
3937 Expression => Relocate_Node (Expr))),
3938 Pragma_Name => Name_Refined_Depends);
3939
3940 Decorate (Aspect, Aitem);
3941 Insert_Pragma (Aitem);
3942 goto Continue;
3943
3944 -- Refined_Global
3945
3946 -- Aspect Refined_Global is never delayed because it is
3947 -- equivalent to a source pragma which appears in the
3948 -- declarations of the related subprogram body. To deal with
3949 -- forward references, the generated pragma is stored in the
3950 -- contract of the related subprogram body and later analyzed
3951 -- at the end of the declarative region. For details, see
3952 -- routine Analyze_Refined_Global_In_Decl_Part.
3953
3954 when Aspect_Refined_Global =>
3955 Aitem := Make_Aitem_Pragma
3956 (Pragma_Argument_Associations => New_List (
3957 Make_Pragma_Argument_Association (Loc,
3958 Expression => Relocate_Node (Expr))),
3959 Pragma_Name => Name_Refined_Global);
3960
3961 Decorate (Aspect, Aitem);
3962 Insert_Pragma (Aitem);
3963 goto Continue;
3964
3965 -- Refined_Post
3966
3967 when Aspect_Refined_Post =>
3968 Aitem := Make_Aitem_Pragma
3969 (Pragma_Argument_Associations => New_List (
3970 Make_Pragma_Argument_Association (Loc,
3971 Expression => Relocate_Node (Expr))),
3972 Pragma_Name => Name_Refined_Post);
3973
3974 Decorate (Aspect, Aitem);
3975 Insert_Pragma (Aitem);
3976 goto Continue;
3977
3978 -- Refined_State
3979
3980 when Aspect_Refined_State =>
3981
3982 -- The corresponding pragma for Refined_State is inserted in
3983 -- the declarations of the related package body. This action
3984 -- synchronizes both the source and from-aspect versions of
3985 -- the pragma.
3986
3987 if Nkind (N) = N_Package_Body then
3988 Aitem := Make_Aitem_Pragma
3989 (Pragma_Argument_Associations => New_List (
3990 Make_Pragma_Argument_Association (Loc,
3991 Expression => Relocate_Node (Expr))),
3992 Pragma_Name => Name_Refined_State);
3993
3994 Decorate (Aspect, Aitem);
3995 Insert_Pragma (Aitem);
3996
3997 -- Otherwise the context is illegal
3998
3999 else
4000 Error_Msg_NE
4001 ("aspect & must apply to a package body", Aspect, Id);
4002 end if;
4003
4004 goto Continue;
4005
4006 -- Relative_Deadline
4007
4008 when Aspect_Relative_Deadline =>
4009 Aitem := Make_Aitem_Pragma
4010 (Pragma_Argument_Associations => New_List (
4011 Make_Pragma_Argument_Association (Loc,
4012 Expression => Relocate_Node (Expr))),
4013 Pragma_Name => Name_Relative_Deadline);
4014
4015 -- If the aspect applies to a task, the corresponding pragma
4016 -- must appear within its declarations, not after.
4017
4018 if Nkind (N) = N_Task_Type_Declaration then
4019 declare
4020 Def : Node_Id;
4021 V : List_Id;
4022
4023 begin
4024 if No (Task_Definition (N)) then
4025 Set_Task_Definition (N,
4026 Make_Task_Definition (Loc,
4027 Visible_Declarations => New_List,
4028 End_Label => Empty));
4029 end if;
4030
4031 Def := Task_Definition (N);
4032 V := Visible_Declarations (Def);
4033 if not Is_Empty_List (V) then
4034 Insert_Before (First (V), Aitem);
4035
4036 else
4037 Set_Visible_Declarations (Def, New_List (Aitem));
4038 end if;
4039
4040 goto Continue;
4041 end;
4042 end if;
4043
4044 -- Relaxed_Initialization
4045
4046 when Aspect_Relaxed_Initialization =>
4047 Analyze_Aspect_Relaxed_Initialization;
4048 goto Continue;
4049
4050 -- Secondary_Stack_Size
4051
4052 -- Aspect Secondary_Stack_Size needs to be converted into a
4053 -- pragma for two reasons: the attribute is not analyzed until
4054 -- after the expansion of the task type declaration and the
4055 -- attribute does not have visibility on the discriminant.
4056
4057 when Aspect_Secondary_Stack_Size =>
4058 Aitem := Make_Aitem_Pragma
4059 (Pragma_Argument_Associations => New_List (
4060 Make_Pragma_Argument_Association (Loc,
4061 Expression => Relocate_Node (Expr))),
4062 Pragma_Name =>
4063 Name_Secondary_Stack_Size);
4064
4065 Decorate (Aspect, Aitem);
4066 Insert_Pragma (Aitem);
4067 goto Continue;
4068
4069 -- Volatile_Function
4070
4071 -- Aspect Volatile_Function is never delayed because it is
4072 -- equivalent to a source pragma which appears after the
4073 -- related subprogram.
4074
4075 when Aspect_Volatile_Function =>
4076 Aitem := Make_Aitem_Pragma
4077 (Pragma_Argument_Associations => New_List (
4078 Make_Pragma_Argument_Association (Loc,
4079 Expression => Relocate_Node (Expr))),
4080 Pragma_Name => Name_Volatile_Function);
4081
4082 Decorate (Aspect, Aitem);
4083 Insert_Pragma (Aitem);
4084 goto Continue;
4085
4086 -- Case 2e: Annotate aspect
4087
4088 when Aspect_Annotate =>
4089 declare
4090 Args : List_Id;
4091 Pargs : List_Id;
4092 Arg : Node_Id;
4093
4094 begin
4095 -- The argument can be a single identifier
4096
4097 if Nkind (Expr) = N_Identifier then
4098
4099 -- One level of parens is allowed
4100
4101 if Paren_Count (Expr) > 1 then
4102 Error_Msg_F ("extra parentheses ignored", Expr);
4103 end if;
4104
4105 Set_Paren_Count (Expr, 0);
4106
4107 -- Add the single item to the list
4108
4109 Args := New_List (Expr);
4110
4111 -- Otherwise we must have an aggregate
4112
4113 elsif Nkind (Expr) = N_Aggregate then
4114
4115 -- Must be positional
4116
4117 if Present (Component_Associations (Expr)) then
4118 Error_Msg_F
4119 ("purely positional aggregate required", Expr);
4120 goto Continue;
4121 end if;
4122
4123 -- Must not be parenthesized
4124
4125 if Paren_Count (Expr) /= 0 then
4126 Error_Msg_F ("extra parentheses ignored", Expr);
4127 end if;
4128
4129 -- List of arguments is list of aggregate expressions
4130
4131 Args := Expressions (Expr);
4132
4133 -- Anything else is illegal
4134
4135 else
4136 Error_Msg_F ("wrong form for Annotate aspect", Expr);
4137 goto Continue;
4138 end if;
4139
4140 -- Prepare pragma arguments
4141
4142 Pargs := New_List;
4143 Arg := First (Args);
4144 while Present (Arg) loop
4145 Append_To (Pargs,
4146 Make_Pragma_Argument_Association (Sloc (Arg),
4147 Expression => Relocate_Node (Arg)));
4148 Next (Arg);
4149 end loop;
4150
4151 Append_To (Pargs,
4152 Make_Pragma_Argument_Association (Sloc (Ent),
4153 Chars => Name_Entity,
4154 Expression => Ent));
4155
4156 Aitem := Make_Aitem_Pragma
4157 (Pragma_Argument_Associations => Pargs,
4158 Pragma_Name => Name_Annotate);
4159 end;
4160
4161 -- Case 3 : Aspects that don't correspond to pragma/attribute
4162 -- definition clause.
4163
4164 -- Case 3a: The aspects listed below don't correspond to
4165 -- pragmas/attributes but do require delayed analysis.
4166
4167 when Aspect_Default_Value | Aspect_Default_Component_Value =>
4168 Error_Msg_Name_1 := Chars (Id);
4169
4170 if not Is_Type (E) then
4171 Error_Msg_N ("aspect% can only apply to a type", Id);
4172 goto Continue;
4173
4174 elsif not Is_First_Subtype (E) then
4175 Error_Msg_N ("aspect% cannot apply to subtype", Id);
4176 goto Continue;
4177
4178 elsif A_Id = Aspect_Default_Value
4179 and then not Is_Scalar_Type (E)
4180 then
4181 Error_Msg_N ("aspect% can only be applied to scalar type",
4182 Id);
4183 goto Continue;
4184
4185 elsif A_Id = Aspect_Default_Component_Value then
4186 if not Is_Array_Type (E) then
4187 Error_Msg_N ("aspect% can only be applied to array " &
4188 "type", Id);
4189 goto Continue;
4190
4191 elsif not Is_Scalar_Type (Component_Type (E)) then
4192 Error_Msg_N ("aspect% requires scalar components", Id);
4193 goto Continue;
4194 end if;
4195 end if;
4196
4197 Aitem := Empty;
4198
4199 when Aspect_Aggregate =>
4200 Validate_Aspect_Aggregate (Expr);
4201 Record_Rep_Item (E, Aspect);
4202 goto Continue;
4203
4204 when Aspect_Integer_Literal
4205 | Aspect_Real_Literal
4206 | Aspect_String_Literal
4207 =>
4208
4209 if not Is_First_Subtype (E) then
4210 Error_Msg_N
4211 ("may only be specified for a first subtype", Aspect);
4212 goto Continue;
4213 end if;
4214
4215 if Ada_Version < Ada_2020 then
4216 Check_Restriction
4217 (No_Implementation_Aspect_Specifications, N);
4218 end if;
4219
4220 Aitem := Empty;
4221
4222 -- Case 3b: The aspects listed below don't correspond to
4223 -- pragmas/attributes and don't need delayed analysis.
4224
4225 -- Implicit_Dereference
4226
4227 -- For Implicit_Dereference, External_Name and Link_Name, only
4228 -- the legality checks are done during the analysis, thus no
4229 -- delay is required.
4230
4231 when Aspect_Implicit_Dereference =>
4232 Analyze_Aspect_Implicit_Dereference;
4233 goto Continue;
4234
4235 -- Dimension
4236
4237 when Aspect_Dimension =>
4238 Analyze_Aspect_Dimension (N, Id, Expr);
4239 goto Continue;
4240
4241 -- Dimension_System
4242
4243 when Aspect_Dimension_System =>
4244 Analyze_Aspect_Dimension_System (N, Id, Expr);
4245 goto Continue;
4246
4247 -- Case 4: Aspects requiring special handling
4248
4249 -- Pre/Post/Test_Case/Contract_Cases/Subprogram_Variant whose
4250 -- corresponding pragmas take care of the delay.
4251
4252 -- Pre/Post
4253
4254 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
4255 -- with a first argument that is the expression, and a second
4256 -- argument that is an informative message if the test fails.
4257 -- This is inserted right after the declaration, to get the
4258 -- required pragma placement. The processing for the pragmas
4259 -- takes care of the required delay.
4260
4261 when Pre_Post_Aspects => Pre_Post : declare
4262 Pname : Name_Id;
4263
4264 begin
4265 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
4266 Pname := Name_Precondition;
4267 else
4268 Pname := Name_Postcondition;
4269 end if;
4270
4271 -- Check that the class-wide predicate cannot be applied to
4272 -- an operation of a synchronized type. AI12-0182 forbids
4273 -- these altogether, while earlier language semantics made
4274 -- them legal on tagged synchronized types.
4275
4276 -- Other legality checks are performed when analyzing the
4277 -- contract of the operation.
4278
4279 if Class_Present (Aspect)
4280 and then Is_Concurrent_Type (Current_Scope)
4281 and then Ekind (E) in E_Entry | E_Function | E_Procedure
4282 then
4283 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
4284 Error_Msg_N
4285 ("aspect % can only be specified for a primitive "
4286 & "operation of a tagged type", Aspect);
4287
4288 goto Continue;
4289 end if;
4290
4291 -- If the expressions is of the form A and then B, then
4292 -- we generate separate Pre/Post aspects for the separate
4293 -- clauses. Since we allow multiple pragmas, there is no
4294 -- problem in allowing multiple Pre/Post aspects internally.
4295 -- These should be treated in reverse order (B first and
4296 -- A second) since they are later inserted just after N in
4297 -- the order they are treated. This way, the pragma for A
4298 -- ends up preceding the pragma for B, which may have an
4299 -- importance for the error raised (either constraint error
4300 -- or precondition error).
4301
4302 -- We do not do this for Pre'Class, since we have to put
4303 -- these conditions together in a complex OR expression.
4304
4305 -- We don't do this in GNATprove mode, because it brings no
4306 -- benefit for proof and causes annoyance for flow analysis,
4307 -- which prefers to be as close to the original source code
4308 -- as possible. Also we don't do this when analyzing generic
4309 -- units since it causes spurious visibility errors in the
4310 -- preanalysis of instantiations.
4311
4312 if not GNATprove_Mode
4313 and then (Pname = Name_Postcondition
4314 or else not Class_Present (Aspect))
4315 and then not Inside_A_Generic
4316 then
4317 while Nkind (Expr) = N_And_Then loop
4318 Insert_After (Aspect,
4319 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
4320 Identifier => Identifier (Aspect),
4321 Expression => Relocate_Node (Left_Opnd (Expr)),
4322 Class_Present => Class_Present (Aspect),
4323 Split_PPC => True));
4324 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
4325 Eloc := Sloc (Expr);
4326 end loop;
4327 end if;
4328
4329 -- Build the precondition/postcondition pragma
4330
4331 -- We use Relocate_Node here rather than New_Copy_Tree
4332 -- because subsequent visibility analysis of the aspect
4333 -- depends on this sharing. This should be cleaned up???
4334
4335 -- If the context is generic, we want to preserve the
4336 -- original tree, and simply share it between aspect and
4337 -- generated attribute. This parallels what is done in
4338 -- sem_prag.adb (see Get_Argument).
4339
4340 declare
4341 New_Expr : Node_Id;
4342
4343 begin
4344 if Inside_A_Generic then
4345 New_Expr := Expr;
4346 else
4347 New_Expr := Relocate_Node (Expr);
4348 end if;
4349
4350 Aitem := Make_Aitem_Pragma
4351 (Pragma_Argument_Associations => New_List (
4352 Make_Pragma_Argument_Association (Eloc,
4353 Chars => Name_Check,
4354 Expression => New_Expr)),
4355 Pragma_Name => Pname);
4356 end;
4357
4358 -- Add message unless exception messages are suppressed
4359
4360 if not Opt.Exception_Locations_Suppressed then
4361 Append_To (Pragma_Argument_Associations (Aitem),
4362 Make_Pragma_Argument_Association (Eloc,
4363 Chars => Name_Message,
4364 Expression =>
4365 Make_String_Literal (Eloc,
4366 Strval => "failed "
4367 & Get_Name_String (Pname)
4368 & " from "
4369 & Build_Location_String (Eloc))));
4370 end if;
4371
4372 Set_Is_Delayed_Aspect (Aspect);
4373
4374 -- For Pre/Post cases, insert immediately after the entity
4375 -- declaration, since that is the required pragma placement.
4376 -- Note that for these aspects, we do not have to worry
4377 -- about delay issues, since the pragmas themselves deal
4378 -- with delay of visibility for the expression analysis.
4379
4380 Insert_Pragma (Aitem);
4381
4382 goto Continue;
4383 end Pre_Post;
4384
4385 -- Test_Case
4386
4387 when Aspect_Test_Case => Test_Case : declare
4388 Args : List_Id;
4389 Comp_Expr : Node_Id;
4390 Comp_Assn : Node_Id;
4391
4392 begin
4393 Args := New_List;
4394
4395 if Nkind (Parent (N)) = N_Compilation_Unit then
4396 Error_Msg_Name_1 := Nam;
4397 Error_Msg_N ("incorrect placement of aspect `%`", E);
4398 goto Continue;
4399 end if;
4400
4401 if Nkind (Expr) /= N_Aggregate then
4402 Error_Msg_Name_1 := Nam;
4403 Error_Msg_NE
4404 ("wrong syntax for aspect `%` for &", Id, E);
4405 goto Continue;
4406 end if;
4407
4408 -- Create the list of arguments for building the Test_Case
4409 -- pragma.
4410
4411 Comp_Expr := First (Expressions (Expr));
4412 while Present (Comp_Expr) loop
4413 Append_To (Args,
4414 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
4415 Expression => Relocate_Node (Comp_Expr)));
4416 Next (Comp_Expr);
4417 end loop;
4418
4419 Comp_Assn := First (Component_Associations (Expr));
4420 while Present (Comp_Assn) loop
4421 if List_Length (Choices (Comp_Assn)) /= 1
4422 or else
4423 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
4424 then
4425 Error_Msg_Name_1 := Nam;
4426 Error_Msg_NE
4427 ("wrong syntax for aspect `%` for &", Id, E);
4428 goto Continue;
4429 end if;
4430
4431 Append_To (Args,
4432 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
4433 Chars => Chars (First (Choices (Comp_Assn))),
4434 Expression =>
4435 Relocate_Node (Expression (Comp_Assn))));
4436 Next (Comp_Assn);
4437 end loop;
4438
4439 -- Build the test-case pragma
4440
4441 Aitem := Make_Aitem_Pragma
4442 (Pragma_Argument_Associations => Args,
4443 Pragma_Name => Nam);
4444 end Test_Case;
4445
4446 -- Contract_Cases
4447
4448 when Aspect_Contract_Cases =>
4449 Aitem := Make_Aitem_Pragma
4450 (Pragma_Argument_Associations => New_List (
4451 Make_Pragma_Argument_Association (Loc,
4452 Expression => Relocate_Node (Expr))),
4453 Pragma_Name => Nam);
4454
4455 Decorate (Aspect, Aitem);
4456 Insert_Pragma (Aitem);
4457 goto Continue;
4458
4459 -- Subprogram_Variant
4460
4461 when Aspect_Subprogram_Variant =>
4462 Aitem := Make_Aitem_Pragma
4463 (Pragma_Argument_Associations => New_List (
4464 Make_Pragma_Argument_Association (Loc,
4465 Expression => Relocate_Node (Expr))),
4466 Pragma_Name => Nam);
4467
4468 Decorate (Aspect, Aitem);
4469 Insert_Pragma (Aitem);
4470 goto Continue;
4471
4472 -- Case 5: Special handling for aspects with an optional
4473 -- boolean argument.
4474
4475 -- In the delayed case, the corresponding pragma cannot be
4476 -- generated yet because the evaluation of the boolean needs
4477 -- to be delayed till the freeze point.
4478
4479 when Boolean_Aspects
4480 | Library_Unit_Aspects
4481 =>
4482 Set_Is_Boolean_Aspect (Aspect);
4483
4484 -- Lock_Free aspect only apply to protected objects
4485
4486 if A_Id = Aspect_Lock_Free then
4487 if Ekind (E) /= E_Protected_Type then
4488 Error_Msg_Name_1 := Nam;
4489 Error_Msg_N
4490 ("aspect % only applies to a protected type " &
4491 "or object",
4492 Aspect);
4493
4494 else
4495 -- Set the Uses_Lock_Free flag to True if there is no
4496 -- expression or if the expression is True. The
4497 -- evaluation of this aspect should be delayed to the
4498 -- freeze point if we wanted to handle the corner case
4499 -- of "true" or "false" being redefined.
4500
4501 if No (Expr)
4502 or else Is_True (Static_Boolean (Expr))
4503 then
4504 Set_Uses_Lock_Free (E);
4505 end if;
4506
4507 Record_Rep_Item (E, Aspect);
4508 end if;
4509
4510 goto Continue;
4511
4512 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
4513 Analyze_Aspect_Export_Import;
4514
4515 -- Disable_Controlled
4516
4517 elsif A_Id = Aspect_Disable_Controlled then
4518 Analyze_Aspect_Disable_Controlled;
4519 goto Continue;
4520
4521 -- Ada 202x (AI12-0129): Exclusive_Functions
4522
4523 elsif A_Id = Aspect_Exclusive_Functions then
4524 if Ekind (E) /= E_Protected_Type then
4525 Error_Msg_Name_1 := Nam;
4526 Error_Msg_N
4527 ("aspect % only applies to a protected type " &
4528 "or object",
4529 Aspect);
4530 end if;
4531
4532 goto Continue;
4533
4534 -- Ada 202x (AI12-0363): Full_Access_Only
4535
4536 elsif A_Id = Aspect_Full_Access_Only then
4537 if Ada_Version < Ada_2020 then
4538 Error_Msg_N
4539 ("aspect % is an Ada 202x feature", Aspect);
4540 Error_Msg_N ("\compile with -gnat2020", Aspect);
4541 end if;
4542
4543 -- Ada 202x (AI12-0075): static expression functions
4544
4545 elsif A_Id = Aspect_Static then
4546 Analyze_Aspect_Static;
4547 goto Continue;
4548
4549 -- Ada 2020 (AI12-0279)
4550
4551 elsif A_Id = Aspect_Yield then
4552 Analyze_Aspect_Yield;
4553 goto Continue;
4554 end if;
4555
4556 -- Library unit aspects require special handling in the case
4557 -- of a package declaration, the pragma needs to be inserted
4558 -- in the list of declarations for the associated package.
4559 -- There is no issue of visibility delay for these aspects.
4560
4561 if A_Id in Library_Unit_Aspects
4562 and then
4563 Nkind (N) in N_Package_Declaration
4564 | N_Generic_Package_Declaration
4565 and then Nkind (Parent (N)) /= N_Compilation_Unit
4566
4567 -- Aspect is legal on a local instantiation of a library-
4568 -- level generic unit.
4569
4570 and then not Is_Generic_Instance (Defining_Entity (N))
4571 then
4572 Error_Msg_N
4573 ("incorrect context for library unit aspect&", Id);
4574 goto Continue;
4575 end if;
4576
4577 -- Cases where we do not delay
4578
4579 if not Delay_Required then
4580
4581 -- Exclude aspects Export and Import because their pragma
4582 -- syntax does not map directly to a Boolean aspect.
4583
4584 if A_Id /= Aspect_Export
4585 and then A_Id /= Aspect_Import
4586 then
4587 Aitem := Make_Aitem_Pragma
4588 (Pragma_Argument_Associations => New_List (
4589 Make_Pragma_Argument_Association (Sloc (Ent),
4590 Expression => Ent)),
4591 Pragma_Name => Chars (Id));
4592 end if;
4593
4594 -- In general cases, the corresponding pragma/attribute
4595 -- definition clause will be inserted later at the freezing
4596 -- point, and we do not need to build it now.
4597
4598 else
4599 Aitem := Empty;
4600 end if;
4601
4602 -- Storage_Size
4603
4604 -- This is special because for access types we need to generate
4605 -- an attribute definition clause. This also works for single
4606 -- task declarations, but it does not work for task type
4607 -- declarations, because we have the case where the expression
4608 -- references a discriminant of the task type. That can't use
4609 -- an attribute definition clause because we would not have
4610 -- visibility on the discriminant. For that case we must
4611 -- generate a pragma in the task definition.
4612
4613 when Aspect_Storage_Size =>
4614
4615 -- Task type case
4616
4617 if Ekind (E) = E_Task_Type then
4618 declare
4619 Decl : constant Node_Id := Declaration_Node (E);
4620
4621 begin
4622 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
4623
4624 -- If no task definition, create one
4625
4626 if No (Task_Definition (Decl)) then
4627 Set_Task_Definition (Decl,
4628 Make_Task_Definition (Loc,
4629 Visible_Declarations => Empty_List,
4630 End_Label => Empty));
4631 end if;
4632
4633 -- Create a pragma and put it at the start of the task
4634 -- definition for the task type declaration.
4635
4636 Aitem := Make_Aitem_Pragma
4637 (Pragma_Argument_Associations => New_List (
4638 Make_Pragma_Argument_Association (Loc,
4639 Expression => Relocate_Node (Expr))),
4640 Pragma_Name => Name_Storage_Size);
4641
4642 Prepend
4643 (Aitem,
4644 Visible_Declarations (Task_Definition (Decl)));
4645 goto Continue;
4646 end;
4647
4648 -- All other cases, generate attribute definition
4649
4650 else
4651 Aitem :=
4652 Make_Attribute_Definition_Clause (Loc,
4653 Name => Ent,
4654 Chars => Chars (Id),
4655 Expression => Relocate_Node (Expr));
4656 end if;
4657 end case;
4658
4659 -- Attach the corresponding pragma/attribute definition clause to
4660 -- the aspect specification node.
4661
4662 if Present (Aitem) then
4663 Set_From_Aspect_Specification (Aitem);
4664 end if;
4665
4666 -- For an aspect that applies to a type, indicate whether it
4667 -- appears on a partial view of the type.
4668
4669 if Is_Type (E)
4670 and then Is_Private_Type (E)
4671 then
4672 Set_Aspect_On_Partial_View (Aspect);
4673 end if;
4674
4675 -- In the context of a compilation unit, we directly put the
4676 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
4677 -- node (no delay is required here) except for aspects on a
4678 -- subprogram body (see below) and a generic package, for which we
4679 -- need to introduce the pragma before building the generic copy
4680 -- (see sem_ch12), and for package instantiations, where the
4681 -- library unit pragmas are better handled early.
4682
4683 if Nkind (Parent (N)) = N_Compilation_Unit
4684 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
4685 then
4686 declare
4687 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
4688
4689 begin
4690 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
4691
4692 -- For a Boolean aspect, create the corresponding pragma if
4693 -- no expression or if the value is True.
4694
4695 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
4696 if Is_True (Static_Boolean (Expr)) then
4697 Aitem := Make_Aitem_Pragma
4698 (Pragma_Argument_Associations => New_List (
4699 Make_Pragma_Argument_Association (Sloc (Ent),
4700 Expression => Ent)),
4701 Pragma_Name => Chars (Id));
4702
4703 Set_From_Aspect_Specification (Aitem, True);
4704 Set_Corresponding_Aspect (Aitem, Aspect);
4705
4706 else
4707 goto Continue;
4708 end if;
4709 end if;
4710
4711 -- If the aspect is on a subprogram body (relevant aspect
4712 -- is Inline), add the pragma in front of the declarations.
4713
4714 if Nkind (N) = N_Subprogram_Body then
4715 if No (Declarations (N)) then
4716 Set_Declarations (N, New_List);
4717 end if;
4718
4719 Prepend (Aitem, Declarations (N));
4720
4721 elsif Nkind (N) = N_Generic_Package_Declaration then
4722 if No (Visible_Declarations (Specification (N))) then
4723 Set_Visible_Declarations (Specification (N), New_List);
4724 end if;
4725
4726 Prepend (Aitem,
4727 Visible_Declarations (Specification (N)));
4728
4729 elsif Nkind (N) = N_Package_Instantiation then
4730 declare
4731 Spec : constant Node_Id :=
4732 Specification (Instance_Spec (N));
4733 begin
4734 if No (Visible_Declarations (Spec)) then
4735 Set_Visible_Declarations (Spec, New_List);
4736 end if;
4737
4738 Prepend (Aitem, Visible_Declarations (Spec));
4739 end;
4740
4741 else
4742 if No (Pragmas_After (Aux)) then
4743 Set_Pragmas_After (Aux, New_List);
4744 end if;
4745
4746 Append (Aitem, Pragmas_After (Aux));
4747 end if;
4748
4749 goto Continue;
4750 end;
4751 end if;
4752
4753 -- The evaluation of the aspect is delayed to the freezing point.
4754 -- The pragma or attribute clause if there is one is then attached
4755 -- to the aspect specification which is put in the rep item list.
4756
4757 if Delay_Required then
4758 if Present (Aitem) then
4759 Set_Is_Delayed_Aspect (Aitem);
4760 Set_Aspect_Rep_Item (Aspect, Aitem);
4761 Set_Parent (Aitem, Aspect);
4762 end if;
4763
4764 Set_Is_Delayed_Aspect (Aspect);
4765
4766 -- In the case of Default_Value, link the aspect to base type
4767 -- as well, even though it appears on a first subtype. This is
4768 -- mandated by the semantics of the aspect. Do not establish
4769 -- the link when processing the base type itself as this leads
4770 -- to a rep item circularity.
4771
4772 if A_Id = Aspect_Default_Value and then Base_Type (E) /= E then
4773 Set_Has_Delayed_Aspects (Base_Type (E));
4774 Record_Rep_Item (Base_Type (E), Aspect);
4775 end if;
4776
4777 Set_Has_Delayed_Aspects (E);
4778 Record_Rep_Item (E, Aspect);
4779
4780 -- When delay is not required and the context is a package or a
4781 -- subprogram body, insert the pragma in the body declarations.
4782
4783 elsif Nkind (N) in N_Package_Body | N_Subprogram_Body then
4784 if No (Declarations (N)) then
4785 Set_Declarations (N, New_List);
4786 end if;
4787
4788 -- The pragma is added before source declarations
4789
4790 Prepend_To (Declarations (N), Aitem);
4791
4792 -- When delay is not required and the context is not a compilation
4793 -- unit, we simply insert the pragma/attribute definition clause
4794 -- in sequence.
4795
4796 elsif Present (Aitem) then
4797 Insert_After (Ins_Node, Aitem);
4798 Ins_Node := Aitem;
4799 end if;
4800
4801 <<Continue>>
4802
4803 -- If a nonoverridable aspect is explicitly specified for a
4804 -- derived type, then check consistency with the parent type.
4805
4806 if A_Id in Nonoverridable_Aspect_Id
4807 and then Nkind (N) = N_Full_Type_Declaration
4808 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
4809 and then not In_Instance_Body
4810 then
4811 declare
4812 Parent_Type : constant Entity_Id := Etype (E);
4813 Inherited_Aspect : constant Node_Id :=
4814 Find_Aspect (Parent_Type, A_Id);
4815 begin
4816 if Present (Inherited_Aspect)
4817 and then not Is_Confirming
4818 (A_Id, Inherited_Aspect, Aspect)
4819 then
4820 Error_Msg_Name_1 := Aspect_Names (A_Id);
4821 Error_Msg_Sloc := Sloc (Inherited_Aspect);
4822
4823 Error_Msg
4824 ("overriding aspect specification for "
4825 & "nonoverridable aspect % does not confirm "
4826 & "aspect specification inherited from #",
4827 Sloc (Aspect));
4828 end if;
4829 end;
4830 end if;
4831 end Analyze_One_Aspect;
4832
4833 Next (Aspect);
4834 end loop Aspect_Loop;
4835
4836 if Has_Delayed_Aspects (E) then
4837 Ensure_Freeze_Node (E);
4838 end if;
4839 end Analyze_Aspect_Specifications;
4840
4841 ------------------------------------------------
4842 -- Analyze_Aspects_On_Subprogram_Body_Or_Stub --
4843 ------------------------------------------------
4844
4845 procedure Analyze_Aspects_On_Subprogram_Body_Or_Stub (N : Node_Id) is
4846 Body_Id : constant Entity_Id := Defining_Entity (N);
4847
4848 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
4849 -- Body [stub] N has aspects, but they are not properly placed. Emit an
4850 -- error message depending on the aspects involved. Spec_Id denotes the
4851 -- entity of the corresponding spec.
4852
4853 --------------------------------
4854 -- Diagnose_Misplaced_Aspects --
4855 --------------------------------
4856
4857 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
4858 procedure Misplaced_Aspect_Error
4859 (Asp : Node_Id;
4860 Ref_Nam : Name_Id);
4861 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
4862 -- the name of the refined version of the aspect.
4863
4864 ----------------------------
4865 -- Misplaced_Aspect_Error --
4866 ----------------------------
4867
4868 procedure Misplaced_Aspect_Error
4869 (Asp : Node_Id;
4870 Ref_Nam : Name_Id)
4871 is
4872 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
4873 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
4874
4875 begin
4876 -- The corresponding spec already contains the aspect in question
4877 -- and the one appearing on the body must be the refined form:
4878
4879 -- procedure P with Global ...;
4880 -- procedure P with Global ... is ... end P;
4881 -- ^
4882 -- Refined_Global
4883
4884 if Has_Aspect (Spec_Id, Asp_Id) then
4885 Error_Msg_Name_1 := Asp_Nam;
4886
4887 -- Subunits cannot carry aspects that apply to a subprogram
4888 -- declaration.
4889
4890 if Nkind (Parent (N)) = N_Subunit then
4891 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
4892
4893 -- Otherwise suggest the refined form
4894
4895 else
4896 Error_Msg_Name_2 := Ref_Nam;
4897 Error_Msg_N ("aspect % should be %", Asp);
4898 end if;
4899
4900 -- Otherwise the aspect must appear on the spec, not on the body
4901
4902 -- procedure P;
4903 -- procedure P with Global ... is ... end P;
4904
4905 else
4906 Error_Msg_N
4907 ("aspect specification must appear on initial declaration",
4908 Asp);
4909 end if;
4910 end Misplaced_Aspect_Error;
4911
4912 -- Local variables
4913
4914 Asp : Node_Id;
4915 Asp_Nam : Name_Id;
4916
4917 -- Start of processing for Diagnose_Misplaced_Aspects
4918
4919 begin
4920 -- Iterate over the aspect specifications and emit specific errors
4921 -- where applicable.
4922
4923 Asp := First (Aspect_Specifications (N));
4924 while Present (Asp) loop
4925 Asp_Nam := Chars (Identifier (Asp));
4926
4927 -- Do not emit errors on aspects that can appear on a subprogram
4928 -- body. This scenario occurs when the aspect specification list
4929 -- contains both misplaced and properly placed aspects.
4930
4931 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
4932 null;
4933
4934 -- Special diagnostics for SPARK aspects
4935
4936 elsif Asp_Nam = Name_Depends then
4937 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
4938
4939 elsif Asp_Nam = Name_Global then
4940 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
4941
4942 elsif Asp_Nam = Name_Post then
4943 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
4944
4945 -- Otherwise a language-defined aspect is misplaced
4946
4947 else
4948 Error_Msg_N
4949 ("aspect specification must appear on initial declaration",
4950 Asp);
4951 end if;
4952
4953 Next (Asp);
4954 end loop;
4955 end Diagnose_Misplaced_Aspects;
4956
4957 -- Local variables
4958
4959 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
4960
4961 -- Start of processing for Analyze_Aspects_On_Subprogram_Body_Or_Stub
4962
4963 begin
4964 -- Language-defined aspects cannot be associated with a subprogram body
4965 -- [stub] if the subprogram has a spec. Certain implementation defined
4966 -- aspects are allowed to break this rule (for all applicable cases, see
4967 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
4968
4969 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
4970 Diagnose_Misplaced_Aspects (Spec_Id);
4971 else
4972 Analyze_Aspect_Specifications (N, Body_Id);
4973 end if;
4974 end Analyze_Aspects_On_Subprogram_Body_Or_Stub;
4975
4976 -----------------------
4977 -- Analyze_At_Clause --
4978 -----------------------
4979
4980 -- An at clause is replaced by the corresponding Address attribute
4981 -- definition clause that is the preferred approach in Ada 95.
4982
4983 procedure Analyze_At_Clause (N : Node_Id) is
4984 CS : constant Boolean := Comes_From_Source (N);
4985
4986 begin
4987 -- This is an obsolescent feature
4988
4989 Check_Restriction (No_Obsolescent_Features, N);
4990
4991 if Warn_On_Obsolescent_Feature then
4992 Error_Msg_N
4993 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
4994 Error_Msg_N
4995 ("\?j?use address attribute definition clause instead", N);
4996 end if;
4997
4998 -- Rewrite as address clause
4999
5000 Rewrite (N,
5001 Make_Attribute_Definition_Clause (Sloc (N),
5002 Name => Identifier (N),
5003 Chars => Name_Address,
5004 Expression => Expression (N)));
5005
5006 -- We preserve Comes_From_Source, since logically the clause still comes
5007 -- from the source program even though it is changed in form.
5008
5009 Set_Comes_From_Source (N, CS);
5010
5011 -- Analyze rewritten clause
5012
5013 Analyze_Attribute_Definition_Clause (N);
5014 end Analyze_At_Clause;
5015
5016 -----------------------------------------
5017 -- Analyze_Attribute_Definition_Clause --
5018 -----------------------------------------
5019
5020 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
5021 Loc : constant Source_Ptr := Sloc (N);
5022 Nam : constant Node_Id := Name (N);
5023 Attr : constant Name_Id := Chars (N);
5024 Expr : constant Node_Id := Expression (N);
5025 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
5026
5027 Ent : Entity_Id;
5028 -- The entity of Nam after it is analyzed. In the case of an incomplete
5029 -- type, this is the underlying type.
5030
5031 U_Ent : Entity_Id;
5032 -- The underlying entity to which the attribute applies. Generally this
5033 -- is the Underlying_Type of Ent, except in the case where the clause
5034 -- applies to the full view of an incomplete or private type, in which
5035 -- case U_Ent is just a copy of Ent.
5036
5037 FOnly : Boolean := False;
5038 -- Reset to True for subtype specific attribute (Alignment, Size)
5039 -- and for stream attributes, i.e. those cases where in the call to
5040 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
5041 -- are checked. Note that the case of stream attributes is not clear
5042 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
5043 -- Storage_Size for derived task types, but that is also clearly
5044 -- unintentional.
5045
5046 procedure Analyze_Put_Image_TSS_Definition;
5047
5048 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
5049 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
5050 -- definition clauses.
5051
5052 function Duplicate_Clause return Boolean;
5053 -- This routine checks if the aspect for U_Ent being given by attribute
5054 -- definition clause N is for an aspect that has already been specified,
5055 -- and if so gives an error message. If there is a duplicate, True is
5056 -- returned, otherwise if there is no error, False is returned.
5057
5058 procedure Check_Indexing_Functions;
5059 -- Check that the function in Constant_Indexing or Variable_Indexing
5060 -- attribute has the proper type structure. If the name is overloaded,
5061 -- check that some interpretation is legal.
5062
5063 procedure Check_Iterator_Functions;
5064 -- Check that there is a single function in Default_Iterator attribute
5065 -- that has the proper type structure.
5066
5067 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
5068 -- Common legality check for the previous two
5069
5070 -----------------------------------
5071 -- Analyze_Put_Image_TSS_Definition --
5072 -----------------------------------
5073
5074 procedure Analyze_Put_Image_TSS_Definition is
5075 Subp : Entity_Id := Empty;
5076 I : Interp_Index;
5077 It : Interp;
5078 Pnam : Entity_Id;
5079
5080 function Has_Good_Profile
5081 (Subp : Entity_Id;
5082 Report : Boolean := False) return Boolean;
5083 -- Return true if the entity is a subprogram with an appropriate
5084 -- profile for the attribute being defined. If result is False and
5085 -- Report is True, function emits appropriate error.
5086
5087 ----------------------
5088 -- Has_Good_Profile --
5089 ----------------------
5090
5091 function Has_Good_Profile
5092 (Subp : Entity_Id;
5093 Report : Boolean := False) return Boolean
5094 is
5095 F : Entity_Id;
5096 Typ : Entity_Id;
5097
5098 begin
5099 if Ekind (Subp) /= E_Procedure then
5100 return False;
5101 end if;
5102
5103 F := First_Formal (Subp);
5104
5105 if No (F) or else Etype (F) /= Class_Wide_Type (RTE (RE_Sink)) then
5106 return False;
5107 end if;
5108
5109 Next_Formal (F);
5110
5111 if Parameter_Mode (F) /= E_In_Parameter then
5112 return False;
5113 end if;
5114
5115 Typ := Etype (F);
5116
5117 -- Verify that the prefix of the attribute and the local name for
5118 -- the type of the formal match.
5119
5120 if Typ /= Ent then
5121 return False;
5122 end if;
5123
5124 if Present (Next_Formal (F)) then
5125 return False;
5126
5127 elsif not Is_Scalar_Type (Typ)
5128 and then not Is_First_Subtype (Typ)
5129 then
5130 if Report and not Is_First_Subtype (Typ) then
5131 Error_Msg_N
5132 ("subtype of formal in Put_Image operation must be a "
5133 & "first subtype", Parameter_Type (Parent (F)));
5134 end if;
5135
5136 return False;
5137
5138 else
5139 return True;
5140 end if;
5141 end Has_Good_Profile;
5142
5143 -- Start of processing for Analyze_Put_Image_TSS_Definition
5144
5145 begin
5146 if not Is_Type (U_Ent) then
5147 Error_Msg_N ("local name must be a subtype", Nam);
5148 return;
5149
5150 elsif not Is_First_Subtype (U_Ent) then
5151 Error_Msg_N ("local name must be a first subtype", Nam);
5152 return;
5153 end if;
5154
5155 Pnam := TSS (Base_Type (U_Ent), TSS_Put_Image);
5156
5157 -- If Pnam is present, it can be either inherited from an ancestor
5158 -- type (in which case it is legal to redefine it for this type), or
5159 -- be a previous definition of the attribute for the same type (in
5160 -- which case it is illegal).
5161
5162 -- In the first case, it will have been analyzed already, and we can
5163 -- check that its profile does not match the expected profile for the
5164 -- Put_Image attribute of U_Ent. In the second case, either Pnam has
5165 -- been analyzed (and has the expected profile), or it has not been
5166 -- analyzed yet (case of a type that has not been frozen yet and for
5167 -- which Put_Image has been set using Set_TSS).
5168
5169 if Present (Pnam)
5170 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
5171 then
5172 Error_Msg_Sloc := Sloc (Pnam);
5173 Error_Msg_Name_1 := Attr;
5174 Error_Msg_N ("% attribute already defined #", Nam);
5175 return;
5176 end if;
5177
5178 Analyze (Expr);
5179
5180 if Is_Entity_Name (Expr) then
5181 if not Is_Overloaded (Expr) then
5182 if Has_Good_Profile (Entity (Expr), Report => True) then
5183 Subp := Entity (Expr);
5184 end if;
5185
5186 else
5187 Get_First_Interp (Expr, I, It);
5188 while Present (It.Nam) loop
5189 if Has_Good_Profile (It.Nam) then
5190 Subp := It.Nam;
5191 exit;
5192 end if;
5193
5194 Get_Next_Interp (I, It);
5195 end loop;
5196 end if;
5197 end if;
5198
5199 if Present (Subp) then
5200 if Is_Abstract_Subprogram (Subp) then
5201 Error_Msg_N ("Put_Image subprogram must not be abstract", Expr);
5202 return;
5203 end if;
5204
5205 Set_Entity (Expr, Subp);
5206 Set_Etype (Expr, Etype (Subp));
5207
5208 New_Put_Image_Subprogram (N, U_Ent, Subp);
5209
5210 else
5211 Error_Msg_Name_1 := Attr;
5212 Error_Msg_N ("incorrect expression for% attribute", Expr);
5213 end if;
5214 end Analyze_Put_Image_TSS_Definition;
5215
5216 -----------------------------------
5217 -- Analyze_Stream_TSS_Definition --
5218 -----------------------------------
5219
5220 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
5221 Subp : Entity_Id := Empty;
5222 I : Interp_Index;
5223 It : Interp;
5224 Pnam : Entity_Id;
5225
5226 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
5227 -- True for Read attribute, False for other attributes
5228
5229 function Has_Good_Profile
5230 (Subp : Entity_Id;
5231 Report : Boolean := False) return Boolean;
5232 -- Return true if the entity is a subprogram with an appropriate
5233 -- profile for the attribute being defined. If result is False and
5234 -- Report is True, function emits appropriate error.
5235
5236 ----------------------
5237 -- Has_Good_Profile --
5238 ----------------------
5239
5240 function Has_Good_Profile
5241 (Subp : Entity_Id;
5242 Report : Boolean := False) return Boolean
5243 is
5244 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
5245 (False => E_Procedure, True => E_Function);
5246 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
5247 F : Entity_Id;
5248 Typ : Entity_Id;
5249
5250 begin
5251 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
5252 return False;
5253 end if;
5254
5255 F := First_Formal (Subp);
5256
5257 if No (F)
5258 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
5259 or else Designated_Type (Etype (F)) /=
5260 Class_Wide_Type (RTE (RE_Root_Stream_Type))
5261 then
5262 return False;
5263 end if;
5264
5265 if not Is_Function then
5266 Next_Formal (F);
5267
5268 declare
5269 Expected_Mode : constant array (Boolean) of Entity_Kind :=
5270 (False => E_In_Parameter,
5271 True => E_Out_Parameter);
5272 begin
5273 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
5274 return False;
5275 end if;
5276 end;
5277
5278 Typ := Etype (F);
5279
5280 else
5281 Typ := Etype (Subp);
5282 end if;
5283
5284 -- Verify that the prefix of the attribute and the local name for
5285 -- the type of the formal match.
5286
5287 if Base_Type (Typ) /= Base_Type (Ent) then
5288 return False;
5289 end if;
5290
5291 if Present (Next_Formal (F)) then
5292 return False;
5293
5294 elsif not Is_Scalar_Type (Typ)
5295 and then not Is_First_Subtype (Typ)
5296 and then not Is_Class_Wide_Type (Typ)
5297 then
5298 if Report and not Is_First_Subtype (Typ) then
5299 Error_Msg_N
5300 ("subtype of formal in stream operation must be a first "
5301 & "subtype", Parameter_Type (Parent (F)));
5302 end if;
5303
5304 return False;
5305
5306 else
5307 return True;
5308 end if;
5309 end Has_Good_Profile;
5310
5311 -- Start of processing for Analyze_Stream_TSS_Definition
5312
5313 begin
5314 FOnly := True;
5315
5316 if not Is_Type (U_Ent) then
5317 Error_Msg_N ("local name must be a subtype", Nam);
5318 return;
5319
5320 elsif not Is_First_Subtype (U_Ent) then
5321 Error_Msg_N ("local name must be a first subtype", Nam);
5322 return;
5323 end if;
5324
5325 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
5326
5327 -- If Pnam is present, it can be either inherited from an ancestor
5328 -- type (in which case it is legal to redefine it for this type), or
5329 -- be a previous definition of the attribute for the same type (in
5330 -- which case it is illegal).
5331
5332 -- In the first case, it will have been analyzed already, and we
5333 -- can check that its profile does not match the expected profile
5334 -- for a stream attribute of U_Ent. In the second case, either Pnam
5335 -- has been analyzed (and has the expected profile), or it has not
5336 -- been analyzed yet (case of a type that has not been frozen yet
5337 -- and for which the stream attribute has been set using Set_TSS).
5338
5339 if Present (Pnam)
5340 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
5341 then
5342 Error_Msg_Sloc := Sloc (Pnam);
5343 Error_Msg_Name_1 := Attr;
5344 Error_Msg_N ("% attribute already defined #", Nam);
5345 return;
5346 end if;
5347
5348 Analyze (Expr);
5349
5350 if Is_Entity_Name (Expr) then
5351 if not Is_Overloaded (Expr) then
5352 if Has_Good_Profile (Entity (Expr), Report => True) then
5353 Subp := Entity (Expr);
5354 end if;
5355
5356 else
5357 Get_First_Interp (Expr, I, It);
5358 while Present (It.Nam) loop
5359 if Has_Good_Profile (It.Nam) then
5360 Subp := It.Nam;
5361 exit;
5362 end if;
5363
5364 Get_Next_Interp (I, It);
5365 end loop;
5366 end if;
5367 end if;
5368
5369 if Present (Subp) then
5370 if Is_Abstract_Subprogram (Subp) then
5371 Error_Msg_N ("stream subprogram must not be abstract", Expr);
5372 return;
5373
5374 -- A stream subprogram for an interface type must be a null
5375 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
5376 -- of an interface is not an interface type (3.9.4 (6.b/2)).
5377
5378 elsif Is_Interface (U_Ent)
5379 and then not Is_Class_Wide_Type (U_Ent)
5380 and then not Inside_A_Generic
5381 and then
5382 (Ekind (Subp) = E_Function
5383 or else
5384 not Null_Present
5385 (Specification
5386 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
5387 then
5388 Error_Msg_N
5389 ("stream subprogram for interface type must be null "
5390 & "procedure", Expr);
5391 end if;
5392
5393 Set_Entity (Expr, Subp);
5394 Set_Etype (Expr, Etype (Subp));
5395
5396 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
5397
5398 else
5399 Error_Msg_Name_1 := Attr;
5400
5401 if Is_Class_Wide_Type (Base_Type (Ent)) then
5402 Error_Msg_N
5403 ("incorrect expression for class-wide% attribute", Expr);
5404 else
5405 Error_Msg_N ("incorrect expression for% attribute", Expr);
5406 end if;
5407 end if;
5408 end Analyze_Stream_TSS_Definition;
5409
5410 ------------------------------
5411 -- Check_Indexing_Functions --
5412 ------------------------------
5413
5414 procedure Check_Indexing_Functions is
5415 Indexing_Found : Boolean := False;
5416
5417 procedure Check_Inherited_Indexing;
5418 -- For a derived type, check that for a derived type, a specification
5419 -- of an indexing aspect can only be confirming, i.e. uses the same
5420 -- name as in the parent type.
5421 -- AI12-0160: Verify that an indexing cannot be specified for
5422 -- a derived type unless it is specified for the parent.
5423
5424 procedure Check_One_Function (Subp : Entity_Id);
5425 -- Check one possible interpretation. Sets Indexing_Found True if a
5426 -- legal indexing function is found.
5427
5428 procedure Illegal_Indexing (Msg : String);
5429 -- Diagnose illegal indexing function if not overloaded. In the
5430 -- overloaded case indicate that no legal interpretation exists.
5431
5432 ------------------------------
5433 -- Check_Inherited_Indexing --
5434 ------------------------------
5435
5436 procedure Check_Inherited_Indexing is
5437 Inherited : Node_Id;
5438 Other_Indexing : Node_Id;
5439
5440 begin
5441 if Attr = Name_Constant_Indexing then
5442 Inherited :=
5443 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5444 Other_Indexing :=
5445 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5446
5447 else pragma Assert (Attr = Name_Variable_Indexing);
5448 Inherited :=
5449 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5450 Other_Indexing :=
5451 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5452 end if;
5453
5454 if Present (Inherited) then
5455 if Debug_Flag_Dot_XX then
5456 null;
5457
5458 -- OK if current attribute_definition_clause is expansion of
5459 -- inherited aspect.
5460
5461 elsif Aspect_Rep_Item (Inherited) = N then
5462 null;
5463
5464 -- Check if this is a confirming specification. The name
5465 -- may be overloaded between the parent operation and the
5466 -- inherited one, so we check that the Chars fields match.
5467
5468 elsif Is_Entity_Name (Expression (Inherited))
5469 and then Chars (Entity (Expression (Inherited))) =
5470 Chars (Entity (Expression (N)))
5471 then
5472 Indexing_Found := True;
5473
5474 -- Indicate the operation that must be overridden, rather than
5475 -- redefining the indexing aspect.
5476
5477 else
5478 Illegal_Indexing
5479 ("indexing function already inherited from parent type");
5480 Error_Msg_NE
5481 ("!override & instead",
5482 N, Entity (Expression (Inherited)));
5483 end if;
5484
5485 -- If not inherited and the parent has another indexing function
5486 -- this is illegal, because it leads to inconsistent results in
5487 -- class-wide calls.
5488
5489 elsif Present (Other_Indexing) then
5490 Error_Msg_N
5491 ("cannot specify indexing operation on derived type"
5492 & " if not specified for parent", N);
5493 end if;
5494 end Check_Inherited_Indexing;
5495
5496 ------------------------
5497 -- Check_One_Function --
5498 ------------------------
5499
5500 procedure Check_One_Function (Subp : Entity_Id) is
5501 Default_Element : Node_Id;
5502 Ret_Type : constant Entity_Id := Etype (Subp);
5503
5504 begin
5505 if not Is_Overloadable (Subp) then
5506 Illegal_Indexing ("illegal indexing function for type&");
5507 return;
5508
5509 elsif Scope (Subp) /= Scope (Ent) then
5510 if Nkind (Expr) = N_Expanded_Name then
5511
5512 -- Indexing function can't be declared elsewhere
5513
5514 Illegal_Indexing
5515 ("indexing function must be declared"
5516 & " in scope of type&");
5517 end if;
5518
5519 if Is_Derived_Type (Ent) then
5520 Check_Inherited_Indexing;
5521 end if;
5522
5523 return;
5524
5525 elsif No (First_Formal (Subp)) then
5526 Illegal_Indexing
5527 ("Indexing requires a function that applies to type&");
5528 return;
5529
5530 elsif No (Next_Formal (First_Formal (Subp))) then
5531 Illegal_Indexing
5532 ("indexing function must have at least two parameters");
5533 return;
5534
5535 elsif Is_Derived_Type (Ent) then
5536 Check_Inherited_Indexing;
5537 end if;
5538
5539 if not Check_Primitive_Function (Subp) then
5540 Illegal_Indexing
5541 ("Indexing aspect requires a function that applies to type&");
5542 return;
5543 end if;
5544
5545 -- If partial declaration exists, verify that it is not tagged.
5546
5547 if Ekind (Current_Scope) = E_Package
5548 and then Has_Private_Declaration (Ent)
5549 and then From_Aspect_Specification (N)
5550 and then
5551 List_Containing (Parent (Ent)) =
5552 Private_Declarations
5553 (Specification (Unit_Declaration_Node (Current_Scope)))
5554 and then Nkind (N) = N_Attribute_Definition_Clause
5555 then
5556 declare
5557 Decl : Node_Id;
5558
5559 begin
5560 Decl :=
5561 First (Visible_Declarations
5562 (Specification
5563 (Unit_Declaration_Node (Current_Scope))));
5564
5565 while Present (Decl) loop
5566 if Nkind (Decl) = N_Private_Type_Declaration
5567 and then Ent = Full_View (Defining_Identifier (Decl))
5568 and then Tagged_Present (Decl)
5569 and then No (Aspect_Specifications (Decl))
5570 then
5571 Illegal_Indexing
5572 ("Indexing aspect cannot be specified on full view "
5573 & "if partial view is tagged");
5574 return;
5575 end if;
5576
5577 Next (Decl);
5578 end loop;
5579 end;
5580 end if;
5581
5582 -- An indexing function must return either the default element of
5583 -- the container, or a reference type. For variable indexing it
5584 -- must be the latter.
5585
5586 Default_Element :=
5587 Find_Value_Of_Aspect
5588 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
5589
5590 if Present (Default_Element) then
5591 Analyze (Default_Element);
5592 end if;
5593
5594 -- For variable_indexing the return type must be a reference type
5595
5596 if Attr = Name_Variable_Indexing then
5597 if not Has_Implicit_Dereference (Ret_Type) then
5598 Illegal_Indexing
5599 ("variable indexing must return a reference type");
5600 return;
5601
5602 elsif Is_Access_Constant
5603 (Etype (First_Discriminant (Ret_Type)))
5604 then
5605 Illegal_Indexing
5606 ("variable indexing must return an access to variable");
5607 return;
5608 end if;
5609
5610 else
5611 if Has_Implicit_Dereference (Ret_Type)
5612 and then not
5613 Is_Access_Constant
5614 (Etype (Get_Reference_Discriminant (Ret_Type)))
5615 then
5616 Illegal_Indexing
5617 ("constant indexing must return an access to constant");
5618 return;
5619
5620 elsif Is_Access_Type (Etype (First_Formal (Subp)))
5621 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
5622 then
5623 Illegal_Indexing
5624 ("constant indexing must apply to an access to constant");
5625 return;
5626 end if;
5627 end if;
5628
5629 -- All checks succeeded
5630
5631 Indexing_Found := True;
5632 end Check_One_Function;
5633
5634 -----------------------
5635 -- Illegal_Indexing --
5636 -----------------------
5637
5638 procedure Illegal_Indexing (Msg : String) is
5639 begin
5640 Error_Msg_NE (Msg, N, Ent);
5641 end Illegal_Indexing;
5642
5643 -- Start of processing for Check_Indexing_Functions
5644
5645 begin
5646 if In_Instance then
5647 Check_Inherited_Indexing;
5648 end if;
5649
5650 Analyze (Expr);
5651
5652 if not Is_Overloaded (Expr) then
5653 Check_One_Function (Entity (Expr));
5654
5655 else
5656 declare
5657 I : Interp_Index;
5658 It : Interp;
5659
5660 begin
5661 Indexing_Found := False;
5662 Get_First_Interp (Expr, I, It);
5663 while Present (It.Nam) loop
5664
5665 -- Note that analysis will have added the interpretation
5666 -- that corresponds to the dereference. We only check the
5667 -- subprogram itself. Ignore homonyms that may come from
5668 -- derived types in the context.
5669
5670 if Is_Overloadable (It.Nam)
5671 and then Comes_From_Source (It.Nam)
5672 then
5673 Check_One_Function (It.Nam);
5674 end if;
5675
5676 Get_Next_Interp (I, It);
5677 end loop;
5678 end;
5679 end if;
5680
5681 if not Indexing_Found and then not Error_Posted (N) then
5682 Error_Msg_NE
5683 ("aspect Indexing requires a local function that applies to "
5684 & "type&", Expr, Ent);
5685 end if;
5686 end Check_Indexing_Functions;
5687
5688 ------------------------------
5689 -- Check_Iterator_Functions --
5690 ------------------------------
5691
5692 procedure Check_Iterator_Functions is
5693 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
5694 -- Check one possible interpretation for validity
5695
5696 ----------------------------
5697 -- Valid_Default_Iterator --
5698 ----------------------------
5699
5700 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
5701 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
5702 Formal : Entity_Id;
5703
5704 begin
5705 if not Check_Primitive_Function (Subp) then
5706 return False;
5707
5708 -- The return type must be derived from a type in an instance
5709 -- of Iterator.Interfaces, and thus its root type must have a
5710 -- predefined name.
5711
5712 elsif Chars (Root_T) /= Name_Forward_Iterator
5713 and then Chars (Root_T) /= Name_Reversible_Iterator
5714 then
5715 return False;
5716
5717 else
5718 Formal := First_Formal (Subp);
5719 end if;
5720
5721 -- False if any subsequent formal has no default expression
5722
5723 Next_Formal (Formal);
5724 while Present (Formal) loop
5725 if No (Expression (Parent (Formal))) then
5726 return False;
5727 end if;
5728
5729 Next_Formal (Formal);
5730 end loop;
5731
5732 -- True if all subsequent formals have default expressions
5733
5734 return True;
5735 end Valid_Default_Iterator;
5736
5737 -- Start of processing for Check_Iterator_Functions
5738
5739 begin
5740 Analyze (Expr);
5741
5742 if not Is_Entity_Name (Expr) then
5743 Error_Msg_N ("aspect Iterator must be a function name", Expr);
5744 end if;
5745
5746 if not Is_Overloaded (Expr) then
5747 if Entity (Expr) /= Any_Id
5748 and then not Check_Primitive_Function (Entity (Expr))
5749 then
5750 Error_Msg_NE
5751 ("aspect Indexing requires a function that applies to type&",
5752 Entity (Expr), Ent);
5753 end if;
5754
5755 -- Flag the default_iterator as well as the denoted function.
5756
5757 if not Valid_Default_Iterator (Entity (Expr)) then
5758 Error_Msg_N ("improper function for default iterator!", Expr);
5759 end if;
5760
5761 else
5762 declare
5763 Default : Entity_Id := Empty;
5764 I : Interp_Index;
5765 It : Interp;
5766
5767 begin
5768 Get_First_Interp (Expr, I, It);
5769 while Present (It.Nam) loop
5770 if not Check_Primitive_Function (It.Nam)
5771 or else not Valid_Default_Iterator (It.Nam)
5772 then
5773 Remove_Interp (I);
5774
5775 elsif Present (Default) then
5776
5777 -- An explicit one should override an implicit one
5778
5779 if Comes_From_Source (Default) =
5780 Comes_From_Source (It.Nam)
5781 then
5782 Error_Msg_N ("default iterator must be unique", Expr);
5783 Error_Msg_Sloc := Sloc (Default);
5784 Error_Msg_N ("\\possible interpretation#", Expr);
5785 Error_Msg_Sloc := Sloc (It.Nam);
5786 Error_Msg_N ("\\possible interpretation#", Expr);
5787
5788 elsif Comes_From_Source (It.Nam) then
5789 Default := It.Nam;
5790 end if;
5791 else
5792 Default := It.Nam;
5793 end if;
5794
5795 Get_Next_Interp (I, It);
5796 end loop;
5797
5798 if Present (Default) then
5799 Set_Entity (Expr, Default);
5800 Set_Is_Overloaded (Expr, False);
5801 else
5802 Error_Msg_N
5803 ("no interpretation is a valid default iterator!", Expr);
5804 end if;
5805 end;
5806 end if;
5807 end Check_Iterator_Functions;
5808
5809 -------------------------------
5810 -- Check_Primitive_Function --
5811 -------------------------------
5812
5813 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
5814 Ctrl : Entity_Id;
5815
5816 begin
5817 if Ekind (Subp) /= E_Function then
5818 return False;
5819 end if;
5820
5821 if No (First_Formal (Subp)) then
5822 return False;
5823 else
5824 Ctrl := Etype (First_Formal (Subp));
5825 end if;
5826
5827 -- To be a primitive operation subprogram has to be in same scope.
5828
5829 if Scope (Ctrl) /= Scope (Subp) then
5830 return False;
5831 end if;
5832
5833 -- Type of formal may be the class-wide type, an access to such,
5834 -- or an incomplete view.
5835
5836 if Ctrl = Ent
5837 or else Ctrl = Class_Wide_Type (Ent)
5838 or else
5839 (Ekind (Ctrl) = E_Anonymous_Access_Type
5840 and then (Designated_Type (Ctrl) = Ent
5841 or else
5842 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
5843 or else
5844 (Ekind (Ctrl) = E_Incomplete_Type
5845 and then Full_View (Ctrl) = Ent)
5846 then
5847 null;
5848 else
5849 return False;
5850 end if;
5851
5852 return True;
5853 end Check_Primitive_Function;
5854
5855 ----------------------
5856 -- Duplicate_Clause --
5857 ----------------------
5858
5859 function Duplicate_Clause return Boolean is
5860 A : Node_Id;
5861
5862 begin
5863 -- Nothing to do if this attribute definition clause comes from
5864 -- an aspect specification, since we could not be duplicating an
5865 -- explicit clause, and we dealt with the case of duplicated aspects
5866 -- in Analyze_Aspect_Specifications.
5867
5868 if From_Aspect_Specification (N) then
5869 return False;
5870 end if;
5871
5872 -- Otherwise current clause may duplicate previous clause, or a
5873 -- previously given pragma or aspect specification for the same
5874 -- aspect.
5875
5876 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
5877
5878 if Present (A) then
5879 Error_Msg_Name_1 := Chars (N);
5880 Error_Msg_Sloc := Sloc (A);
5881
5882 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
5883 return True;
5884 end if;
5885
5886 return False;
5887 end Duplicate_Clause;
5888
5889 -- Start of processing for Analyze_Attribute_Definition_Clause
5890
5891 begin
5892 -- The following code is a defense against recursion. Not clear that
5893 -- this can happen legitimately, but perhaps some error situations can
5894 -- cause it, and we did see this recursion during testing.
5895
5896 if Analyzed (N) then
5897 return;
5898 else
5899 Set_Analyzed (N, True);
5900 end if;
5901
5902 Check_Restriction_No_Use_Of_Attribute (N);
5903
5904 if Get_Aspect_Id (Chars (N)) /= No_Aspect then
5905 -- 6.1/3 No_Specification_of_Aspect: Identifies an aspect for which
5906 -- no aspect_specification, attribute_definition_clause, or pragma
5907 -- is given.
5908 Check_Restriction_No_Specification_Of_Aspect (N);
5909 end if;
5910
5911 -- Ignore some selected attributes in CodePeer mode since they are not
5912 -- relevant in this context.
5913
5914 if CodePeer_Mode then
5915 case Id is
5916
5917 -- Ignore Component_Size in CodePeer mode, to avoid changing the
5918 -- internal representation of types by implicitly packing them.
5919
5920 when Attribute_Component_Size =>
5921 Rewrite (N, Make_Null_Statement (Sloc (N)));
5922 return;
5923
5924 when others =>
5925 null;
5926 end case;
5927 end if;
5928
5929 -- Process Ignore_Rep_Clauses option
5930
5931 if Ignore_Rep_Clauses then
5932 case Id is
5933
5934 -- The following should be ignored. They do not affect legality
5935 -- and may be target dependent. The basic idea of -gnatI is to
5936 -- ignore any rep clauses that may be target dependent but do not
5937 -- affect legality (except possibly to be rejected because they
5938 -- are incompatible with the compilation target).
5939
5940 when Attribute_Alignment
5941 | Attribute_Bit_Order
5942 | Attribute_Component_Size
5943 | Attribute_Default_Scalar_Storage_Order
5944 | Attribute_Machine_Radix
5945 | Attribute_Object_Size
5946 | Attribute_Scalar_Storage_Order
5947 | Attribute_Size
5948 | Attribute_Small
5949 | Attribute_Stream_Size
5950 | Attribute_Value_Size
5951 =>
5952 Kill_Rep_Clause (N);
5953 return;
5954
5955 -- The following should not be ignored, because in the first place
5956 -- they are reasonably portable, and should not cause problems
5957 -- in compiling code from another target, and also they do affect
5958 -- legality, e.g. failing to provide a stream attribute for a type
5959 -- may make a program illegal.
5960
5961 when Attribute_External_Tag
5962 | Attribute_Input
5963 | Attribute_Output
5964 | Attribute_Put_Image
5965 | Attribute_Read
5966 | Attribute_Simple_Storage_Pool
5967 | Attribute_Storage_Pool
5968 | Attribute_Storage_Size
5969 | Attribute_Write
5970 =>
5971 null;
5972
5973 -- We do not do anything here with address clauses, they will be
5974 -- removed by Freeze later on, but for now, it works better to
5975 -- keep them in the tree.
5976
5977 when Attribute_Address =>
5978 null;
5979
5980 -- Other cases are errors ("attribute& cannot be set with
5981 -- definition clause"), which will be caught below.
5982
5983 when others =>
5984 null;
5985 end case;
5986 end if;
5987
5988 Analyze (Nam);
5989 Ent := Entity (Nam);
5990
5991 if Rep_Item_Too_Early (Ent, N) then
5992 return;
5993 end if;
5994
5995 -- Rep clause applies to (underlying) full view of private or incomplete
5996 -- type if we have one (if not, this is a premature use of the type).
5997 -- However, some semantic checks need to be done on the specified entity
5998 -- i.e. the private view, so we save it in Ent.
5999
6000 if Is_Private_Type (Ent)
6001 and then Is_Derived_Type (Ent)
6002 and then not Is_Tagged_Type (Ent)
6003 and then No (Full_View (Ent))
6004 and then No (Underlying_Full_View (Ent))
6005 then
6006 U_Ent := Ent;
6007
6008 elsif Ekind (Ent) = E_Incomplete_Type then
6009
6010 -- The attribute applies to the full view, set the entity of the
6011 -- attribute definition accordingly.
6012
6013 Ent := Underlying_Type (Ent);
6014 U_Ent := Ent;
6015 Set_Entity (Nam, Ent);
6016
6017 else
6018 U_Ent := Underlying_Type (Ent);
6019 end if;
6020
6021 -- Avoid cascaded error
6022
6023 if Etype (Nam) = Any_Type then
6024 return;
6025
6026 -- Must be declared in current scope or in case of an aspect
6027 -- specification, must be visible in current scope.
6028
6029 elsif Scope (Ent) /= Current_Scope
6030 and then
6031 not (From_Aspect_Specification (N)
6032 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
6033 then
6034 Error_Msg_N ("entity must be declared in this scope", Nam);
6035 return;
6036
6037 -- Must not be a source renaming (we do have some cases where the
6038 -- expander generates a renaming, and those cases are OK, in such
6039 -- cases any attribute applies to the renamed object as well).
6040
6041 elsif Is_Object (Ent)
6042 and then Present (Renamed_Object (Ent))
6043 then
6044 -- In the case of a renamed object from source, this is an error
6045 -- unless the object is an aggregate and the renaming is created
6046 -- for an object declaration.
6047
6048 if Comes_From_Source (Renamed_Object (Ent))
6049 and then Nkind (Renamed_Object (Ent)) /= N_Aggregate
6050 then
6051 Get_Name_String (Chars (N));
6052 Error_Msg_Strlen := Name_Len;
6053 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
6054 Error_Msg_N
6055 ("~ clause not allowed for a renaming declaration "
6056 & "(RM 13.1(6))", Nam);
6057 return;
6058
6059 -- For the case of a compiler generated renaming, the attribute
6060 -- definition clause applies to the renamed object created by the
6061 -- expander. The easiest general way to handle this is to create a
6062 -- copy of the attribute definition clause for this object.
6063
6064 elsif Is_Entity_Name (Renamed_Object (Ent)) then
6065 Insert_Action (N,
6066 Make_Attribute_Definition_Clause (Loc,
6067 Name =>
6068 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
6069 Chars => Chars (N),
6070 Expression => Duplicate_Subexpr (Expression (N))));
6071
6072 -- If the renamed object is not an entity, it must be a dereference
6073 -- of an unconstrained function call, and we must introduce a new
6074 -- declaration to capture the expression. This is needed in the case
6075 -- of 'Alignment, where the original declaration must be rewritten.
6076
6077 else
6078 pragma Assert
6079 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
6080 null;
6081 end if;
6082
6083 -- If no underlying entity, use entity itself, applies to some
6084 -- previously detected error cases ???
6085
6086 elsif No (U_Ent) then
6087 U_Ent := Ent;
6088
6089 -- Cannot specify for a subtype (exception Object/Value_Size)
6090
6091 elsif Is_Type (U_Ent)
6092 and then not Is_First_Subtype (U_Ent)
6093 and then Id /= Attribute_Object_Size
6094 and then Id /= Attribute_Value_Size
6095 and then not From_At_Mod (N)
6096 then
6097 Error_Msg_N ("cannot specify attribute for subtype", Nam);
6098 return;
6099 end if;
6100
6101 Set_Entity (N, U_Ent);
6102
6103 -- Switch on particular attribute
6104
6105 case Id is
6106
6107 -------------
6108 -- Address --
6109 -------------
6110
6111 -- Address attribute definition clause
6112
6113 when Attribute_Address => Address : begin
6114
6115 -- A little error check, catch for X'Address use X'Address;
6116
6117 if Nkind (Nam) = N_Identifier
6118 and then Nkind (Expr) = N_Attribute_Reference
6119 and then Attribute_Name (Expr) = Name_Address
6120 and then Nkind (Prefix (Expr)) = N_Identifier
6121 and then Chars (Nam) = Chars (Prefix (Expr))
6122 then
6123 Error_Msg_NE
6124 ("address for & is self-referencing", Prefix (Expr), Ent);
6125 return;
6126 end if;
6127
6128 -- Not that special case, carry on with analysis of expression
6129
6130 Analyze_And_Resolve (Expr, RTE (RE_Address));
6131
6132 -- Even when ignoring rep clauses we need to indicate that the
6133 -- entity has an address clause and thus it is legal to declare
6134 -- it imported. Freeze will get rid of the address clause later.
6135 -- Also call Set_Address_Taken to indicate that an address clause
6136 -- was present, even if we are about to remove it.
6137
6138 if Ignore_Rep_Clauses then
6139 Set_Address_Taken (U_Ent);
6140
6141 if Ekind (U_Ent) in E_Variable | E_Constant then
6142 Record_Rep_Item (U_Ent, N);
6143 end if;
6144
6145 return;
6146 end if;
6147
6148 if Duplicate_Clause then
6149 null;
6150
6151 -- Case of address clause for subprogram
6152
6153 elsif Is_Subprogram (U_Ent) then
6154 if Has_Homonym (U_Ent) then
6155 Error_Msg_N
6156 ("address clause cannot be given for overloaded "
6157 & "subprogram", Nam);
6158 return;
6159 end if;
6160
6161 -- For subprograms, all address clauses are permitted, and we
6162 -- mark the subprogram as having a deferred freeze so that Gigi
6163 -- will not elaborate it too soon.
6164
6165 -- Above needs more comments, what is too soon about???
6166
6167 Set_Has_Delayed_Freeze (U_Ent);
6168
6169 -- Case of address clause for entry
6170
6171 elsif Ekind (U_Ent) = E_Entry then
6172 if Nkind (Parent (N)) = N_Task_Body then
6173 Error_Msg_N
6174 ("entry address must be specified in task spec", Nam);
6175 return;
6176 end if;
6177
6178 -- For entries, we require a constant address
6179
6180 Check_Constant_Address_Clause (Expr, U_Ent);
6181
6182 -- Special checks for task types
6183
6184 if Is_Task_Type (Scope (U_Ent))
6185 and then Comes_From_Source (Scope (U_Ent))
6186 then
6187 Error_Msg_N
6188 ("??entry address declared for entry in task type", N);
6189 Error_Msg_N
6190 ("\??only one task can be declared of this type", N);
6191 end if;
6192
6193 -- Entry address clauses are obsolescent
6194
6195 Check_Restriction (No_Obsolescent_Features, N);
6196
6197 if Warn_On_Obsolescent_Feature then
6198 Error_Msg_N
6199 ("?j?attaching interrupt to task entry is an obsolescent "
6200 & "feature (RM J.7.1)", N);
6201 Error_Msg_N
6202 ("\?j?use interrupt procedure instead", N);
6203 end if;
6204
6205 -- Case of an address clause for a class-wide object, which is
6206 -- considered erroneous.
6207
6208 elsif Is_Class_Wide_Type (Etype (U_Ent)) then
6209 Error_Msg_NE
6210 ("??class-wide object & must not be overlaid", Nam, U_Ent);
6211 Error_Msg_N
6212 ("\??Program_Error will be raised at run time", Nam);
6213 Insert_Action (Declaration_Node (U_Ent),
6214 Make_Raise_Program_Error (Loc,
6215 Reason => PE_Overlaid_Controlled_Object));
6216 return;
6217
6218 -- Case of address clause for an object
6219
6220 elsif Ekind (U_Ent) in E_Constant | E_Variable then
6221 declare
6222 Expr : constant Node_Id := Expression (N);
6223 O_Ent : Entity_Id;
6224 Off : Boolean;
6225
6226 begin
6227 -- Exported variables cannot have an address clause, because
6228 -- this cancels the effect of the pragma Export.
6229
6230 if Is_Exported (U_Ent) then
6231 Error_Msg_N
6232 ("cannot export object with address clause", Nam);
6233 return;
6234 end if;
6235
6236 Find_Overlaid_Entity (N, O_Ent, Off);
6237
6238 if Present (O_Ent) then
6239
6240 -- If the object overlays a constant object, mark it so
6241
6242 if Is_Constant_Object (O_Ent) then
6243 Set_Overlays_Constant (U_Ent);
6244 end if;
6245
6246 -- If the address clause is of the form:
6247
6248 -- for X'Address use Y'Address;
6249
6250 -- or
6251
6252 -- C : constant Address := Y'Address;
6253 -- ...
6254 -- for X'Address use C;
6255
6256 -- then we make an entry in the table to check the size
6257 -- and alignment of the overlaying variable. But we defer
6258 -- this check till after code generation to take full
6259 -- advantage of the annotation done by the back end.
6260
6261 -- If the entity has a generic type, the check will be
6262 -- performed in the instance if the actual type justifies
6263 -- it, and we do not insert the clause in the table to
6264 -- prevent spurious warnings.
6265
6266 -- Note: we used to test Comes_From_Source and only give
6267 -- this warning for source entities, but we have removed
6268 -- this test. It really seems bogus to generate overlays
6269 -- that would trigger this warning in generated code.
6270 -- Furthermore, by removing the test, we handle the
6271 -- aspect case properly.
6272
6273 if Is_Object (O_Ent)
6274 and then not Is_Generic_Formal (O_Ent)
6275 and then not Is_Generic_Type (Etype (U_Ent))
6276 and then Address_Clause_Overlay_Warnings
6277 then
6278 Register_Address_Clause_Check
6279 (N, U_Ent, No_Uint, O_Ent, Off);
6280 end if;
6281
6282 -- If the overlay changes the storage order, warn since
6283 -- the construct is not really supported by the back end.
6284 -- Also mark the entity as being volatile to block the
6285 -- optimizer, even if there is no warranty on the result.
6286
6287 if (Is_Record_Type (Etype (U_Ent))
6288 or else Is_Array_Type (Etype (U_Ent)))
6289 and then (Is_Record_Type (Etype (O_Ent))
6290 or else Is_Array_Type (Etype (O_Ent)))
6291 and then Reverse_Storage_Order (Etype (U_Ent)) /=
6292 Reverse_Storage_Order (Etype (O_Ent))
6293 then
6294 Error_Msg_N
6295 ("??overlay changes scalar storage order", Expr);
6296 Set_Treat_As_Volatile (U_Ent);
6297 end if;
6298
6299 else
6300 -- If this is not an overlay, mark a variable as being
6301 -- volatile to prevent unwanted optimizations. It's a
6302 -- conservative interpretation of RM 13.3(19) for the
6303 -- cases where the compiler cannot detect potential
6304 -- aliasing issues easily and it also covers the case
6305 -- of an absolute address where the volatile aspect is
6306 -- kind of implicit.
6307
6308 if Ekind (U_Ent) = E_Variable then
6309 Set_Treat_As_Volatile (U_Ent);
6310 end if;
6311
6312 -- Make an entry in the table for an absolute address as
6313 -- above to check that the value is compatible with the
6314 -- alignment of the object.
6315
6316 declare
6317 Addr : constant Node_Id := Address_Value (Expr);
6318 begin
6319 if Compile_Time_Known_Value (Addr)
6320 and then Address_Clause_Overlay_Warnings
6321 then
6322 Register_Address_Clause_Check
6323 (N, U_Ent, Expr_Value (Addr), Empty, False);
6324 end if;
6325 end;
6326 end if;
6327
6328 -- Issue an unconditional warning for a constant overlaying
6329 -- a variable. For the reverse case, we will issue it only
6330 -- if the variable is modified.
6331 -- Within a generic unit an In_Parameter is a constant.
6332 -- It can be instantiated with a variable, in which case
6333 -- there will be a warning on the instance.
6334
6335 if Ekind (U_Ent) = E_Constant
6336 and then Present (O_Ent)
6337 and then Ekind (O_Ent) /= E_Generic_In_Parameter
6338 and then not Overlays_Constant (U_Ent)
6339 and then Address_Clause_Overlay_Warnings
6340 then
6341 Error_Msg_N ("??constant overlays a variable", Expr);
6342
6343 -- Imported variables can have an address clause, but then
6344 -- the import is pretty meaningless except to suppress
6345 -- initializations, so we do not need such variables to
6346 -- be statically allocated (and in fact it causes trouble
6347 -- if the address clause is a local value).
6348
6349 elsif Is_Imported (U_Ent) then
6350 Set_Is_Statically_Allocated (U_Ent, False);
6351 end if;
6352
6353 -- We mark a possible modification of a variable with an
6354 -- address clause, since it is likely aliasing is occurring.
6355
6356 Note_Possible_Modification (Nam, Sure => False);
6357
6358 -- Legality checks on the address clause for initialized
6359 -- objects is deferred until the freeze point, because
6360 -- a subsequent pragma might indicate that the object
6361 -- is imported and thus not initialized. Also, the address
6362 -- clause might involve entities that have yet to be
6363 -- elaborated.
6364
6365 Set_Has_Delayed_Freeze (U_Ent);
6366
6367 -- If an initialization call has been generated for this
6368 -- object, it needs to be deferred to after the freeze node
6369 -- we have just now added, otherwise GIGI will see a
6370 -- reference to the variable (as actual to the IP call)
6371 -- before its definition.
6372
6373 declare
6374 Init_Call : constant Node_Id :=
6375 Remove_Init_Call (U_Ent, N);
6376
6377 begin
6378 if Present (Init_Call) then
6379 Append_Freeze_Action (U_Ent, Init_Call);
6380
6381 -- Reset Initialization_Statements pointer so that
6382 -- if there is a pragma Import further down, it can
6383 -- clear any default initialization.
6384
6385 Set_Initialization_Statements (U_Ent, Init_Call);
6386 end if;
6387 end;
6388
6389 -- Entity has delayed freeze, so we will generate an
6390 -- alignment check at the freeze point unless suppressed.
6391
6392 if not Range_Checks_Suppressed (U_Ent)
6393 and then not Alignment_Checks_Suppressed (U_Ent)
6394 then
6395 Set_Check_Address_Alignment (N);
6396 end if;
6397
6398 -- Kill the size check code, since we are not allocating
6399 -- the variable, it is somewhere else.
6400
6401 Kill_Size_Check_Code (U_Ent);
6402 end;
6403
6404 -- Not a valid entity for an address clause
6405
6406 else
6407 Error_Msg_N ("address cannot be given for &", Nam);
6408 end if;
6409 end Address;
6410
6411 ---------------
6412 -- Alignment --
6413 ---------------
6414
6415 -- Alignment attribute definition clause
6416
6417 when Attribute_Alignment => Alignment : declare
6418 Align : constant Uint := Get_Alignment_Value (Expr);
6419 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
6420
6421 begin
6422 FOnly := True;
6423
6424 if not Is_Type (U_Ent)
6425 and then Ekind (U_Ent) /= E_Variable
6426 and then Ekind (U_Ent) /= E_Constant
6427 then
6428 Error_Msg_N ("alignment cannot be given for &", Nam);
6429
6430 elsif Duplicate_Clause then
6431 null;
6432
6433 elsif Align /= No_Uint then
6434 Set_Has_Alignment_Clause (U_Ent);
6435
6436 -- Tagged type case, check for attempt to set alignment to a
6437 -- value greater than Max_Align, and reset if so.
6438
6439 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
6440 Error_Msg_N
6441 ("alignment for & set to Maximum_Aligment??", Nam);
6442 Set_Alignment (U_Ent, Max_Align);
6443
6444 -- All other cases
6445
6446 else
6447 Set_Alignment (U_Ent, Align);
6448 end if;
6449
6450 -- For an array type, U_Ent is the first subtype. In that case,
6451 -- also set the alignment of the anonymous base type so that
6452 -- other subtypes (such as the itypes for aggregates of the
6453 -- type) also receive the expected alignment.
6454
6455 if Is_Array_Type (U_Ent) then
6456 Set_Alignment (Base_Type (U_Ent), Align);
6457 end if;
6458 end if;
6459 end Alignment;
6460
6461 ---------------
6462 -- Bit_Order --
6463 ---------------
6464
6465 -- Bit_Order attribute definition clause
6466
6467 when Attribute_Bit_Order =>
6468 if not Is_Record_Type (U_Ent) then
6469 Error_Msg_N
6470 ("Bit_Order can only be defined for record type", Nam);
6471
6472 elsif Is_Tagged_Type (U_Ent) and then Is_Derived_Type (U_Ent) then
6473 Error_Msg_N
6474 ("Bit_Order cannot be defined for record extensions", Nam);
6475
6476 elsif Duplicate_Clause then
6477 null;
6478
6479 else
6480 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
6481
6482 if Etype (Expr) = Any_Type then
6483 return;
6484
6485 elsif not Is_OK_Static_Expression (Expr) then
6486 Flag_Non_Static_Expr
6487 ("Bit_Order requires static expression!", Expr);
6488
6489 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
6490 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
6491 end if;
6492 end if;
6493
6494 --------------------
6495 -- Component_Size --
6496 --------------------
6497
6498 -- Component_Size attribute definition clause
6499
6500 when Attribute_Component_Size => Component_Size_Case : declare
6501 Csize : constant Uint := Static_Integer (Expr);
6502 Ctyp : Entity_Id;
6503 Btype : Entity_Id;
6504 Biased : Boolean;
6505 New_Ctyp : Entity_Id;
6506 Decl : Node_Id;
6507
6508 begin
6509 if not Is_Array_Type (U_Ent) then
6510 Error_Msg_N ("component size requires array type", Nam);
6511 return;
6512 end if;
6513
6514 Btype := Base_Type (U_Ent);
6515 Ctyp := Component_Type (Btype);
6516
6517 if Duplicate_Clause then
6518 null;
6519
6520 elsif Rep_Item_Too_Early (Btype, N) then
6521 null;
6522
6523 elsif Csize /= No_Uint then
6524 Check_Size (Expr, Ctyp, Csize, Biased);
6525
6526 -- For the biased case, build a declaration for a subtype that
6527 -- will be used to represent the biased subtype that reflects
6528 -- the biased representation of components. We need the subtype
6529 -- to get proper conversions on referencing elements of the
6530 -- array.
6531
6532 if Biased then
6533 New_Ctyp :=
6534 Make_Defining_Identifier (Loc,
6535 Chars =>
6536 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
6537
6538 Decl :=
6539 Make_Subtype_Declaration (Loc,
6540 Defining_Identifier => New_Ctyp,
6541 Subtype_Indication =>
6542 New_Occurrence_Of (Component_Type (Btype), Loc));
6543
6544 Set_Parent (Decl, N);
6545 Analyze (Decl, Suppress => All_Checks);
6546
6547 Set_Has_Delayed_Freeze (New_Ctyp, False);
6548 Init_Esize (New_Ctyp);
6549 Set_RM_Size (New_Ctyp, Csize);
6550 Init_Alignment (New_Ctyp);
6551 Set_Is_Itype (New_Ctyp, True);
6552 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
6553
6554 Set_Component_Type (Btype, New_Ctyp);
6555 Set_Biased (New_Ctyp, N, "component size clause");
6556 end if;
6557
6558 Set_Component_Size (Btype, Csize);
6559
6560 -- Deal with warning on overridden size
6561
6562 if Warn_On_Overridden_Size
6563 and then Has_Size_Clause (Ctyp)
6564 and then RM_Size (Ctyp) /= Csize
6565 then
6566 Error_Msg_NE
6567 ("component size overrides size clause for&?S?", N, Ctyp);
6568 end if;
6569
6570 Set_Has_Component_Size_Clause (Btype, True);
6571 Set_Has_Non_Standard_Rep (Btype, True);
6572 end if;
6573 end Component_Size_Case;
6574
6575 -----------------------
6576 -- Constant_Indexing --
6577 -----------------------
6578
6579 when Attribute_Constant_Indexing =>
6580 Check_Indexing_Functions;
6581
6582 ---------
6583 -- CPU --
6584 ---------
6585
6586 when Attribute_CPU =>
6587 pragma Assert (From_Aspect_Specification (N));
6588 -- The parser forbids this clause in source code, so it must have
6589 -- come from an aspect specification.
6590
6591 if not Is_Task_Type (U_Ent) then
6592 Error_Msg_N ("CPU can only be defined for task", Nam);
6593
6594 elsif Duplicate_Clause then
6595 null;
6596
6597 else
6598 -- The expression must be analyzed in the special manner
6599 -- described in "Handling of Default and Per-Object
6600 -- Expressions" in sem.ads.
6601
6602 -- The visibility to the components must be established
6603 -- and restored before and after analysis.
6604
6605 Push_Type (U_Ent);
6606 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
6607 Pop_Type (U_Ent);
6608
6609 -- AI12-0117-1, "Restriction No_Tasks_Unassigned_To_CPU":
6610 -- If the expression is static, and its value is
6611 -- System.Multiprocessors.Not_A_Specific_CPU (i.e. zero) then
6612 -- that's a violation of No_Tasks_Unassigned_To_CPU. It might
6613 -- seem better to refer to Not_A_Specific_CPU here, but that
6614 -- involves a lot of horsing around with Rtsfind, and this
6615 -- value is not going to change, so it's better to hardwire
6616 -- Uint_0.
6617 --
6618 -- AI12-0055-1, "All properties of a usage profile are defined
6619 -- by pragmas": If the expression is nonstatic, that's a
6620 -- violation of No_Dynamic_CPU_Assignment.
6621
6622 if Is_OK_Static_Expression (Expr) then
6623 if Expr_Value (Expr) = Uint_0 then
6624 Check_Restriction (No_Tasks_Unassigned_To_CPU, Expr);
6625 end if;
6626 else
6627 Check_Restriction (No_Dynamic_CPU_Assignment, Expr);
6628 end if;
6629 end if;
6630
6631 ----------------------
6632 -- Default_Iterator --
6633 ----------------------
6634
6635 when Attribute_Default_Iterator => Default_Iterator : declare
6636 Func : Entity_Id;
6637 Typ : Entity_Id;
6638
6639 begin
6640 -- If target type is untagged, further checks are irrelevant
6641
6642 if not Is_Tagged_Type (U_Ent) then
6643 Error_Msg_N
6644 ("aspect Default_Iterator applies to tagged type", Nam);
6645 return;
6646 end if;
6647
6648 Check_Iterator_Functions;
6649
6650 Analyze (Expr);
6651
6652 if not Is_Entity_Name (Expr)
6653 or else Ekind (Entity (Expr)) /= E_Function
6654 then
6655 Error_Msg_N ("aspect Iterator must be a function", Expr);
6656 return;
6657 else
6658 Func := Entity (Expr);
6659 end if;
6660
6661 -- The type of the first parameter must be T, T'class, or a
6662 -- corresponding access type (5.5.1 (8/3). If function is
6663 -- parameterless label type accordingly.
6664
6665 if No (First_Formal (Func)) then
6666 Typ := Any_Type;
6667 else
6668 Typ := Etype (First_Formal (Func));
6669 end if;
6670
6671 if Typ = U_Ent
6672 or else Typ = Class_Wide_Type (U_Ent)
6673 or else (Is_Access_Type (Typ)
6674 and then Designated_Type (Typ) = U_Ent)
6675 or else (Is_Access_Type (Typ)
6676 and then Designated_Type (Typ) =
6677 Class_Wide_Type (U_Ent))
6678 then
6679 null;
6680
6681 else
6682 Error_Msg_NE
6683 ("Default Iterator must be a primitive of&", Func, U_Ent);
6684 end if;
6685 end Default_Iterator;
6686
6687 ------------------------
6688 -- Dispatching_Domain --
6689 ------------------------
6690
6691 when Attribute_Dispatching_Domain =>
6692 pragma Assert (From_Aspect_Specification (N));
6693 -- The parser forbids this clause in source code, so it must have
6694 -- come from an aspect specification.
6695
6696 if not Is_Task_Type (U_Ent) then
6697 Error_Msg_N
6698 ("Dispatching_Domain can only be defined for task", Nam);
6699
6700 elsif Duplicate_Clause then
6701 null;
6702
6703 else
6704 -- The expression must be analyzed in the special manner
6705 -- described in "Handling of Default and Per-Object
6706 -- Expressions" in sem.ads.
6707
6708 -- The visibility to the components must be restored
6709
6710 Push_Type (U_Ent);
6711
6712 Preanalyze_Spec_Expression
6713 (Expr, RTE (RE_Dispatching_Domain));
6714
6715 Pop_Type (U_Ent);
6716 end if;
6717
6718 ------------------
6719 -- External_Tag --
6720 ------------------
6721
6722 when Attribute_External_Tag =>
6723 if not Is_Tagged_Type (U_Ent) then
6724 Error_Msg_N ("should be a tagged type", Nam);
6725 end if;
6726
6727 if Duplicate_Clause then
6728 null;
6729
6730 else
6731 Analyze_And_Resolve (Expr, Standard_String);
6732
6733 if not Is_OK_Static_Expression (Expr) then
6734 Flag_Non_Static_Expr
6735 ("static string required for tag name!", Nam);
6736 end if;
6737
6738 if not Is_Library_Level_Entity (U_Ent) then
6739 Error_Msg_NE
6740 ("??non-unique external tag supplied for &", N, U_Ent);
6741 Error_Msg_N
6742 ("\??same external tag applies to all subprogram calls",
6743 N);
6744 Error_Msg_N
6745 ("\??corresponding internal tag cannot be obtained", N);
6746 end if;
6747 end if;
6748
6749 --------------------------
6750 -- Implicit_Dereference --
6751 --------------------------
6752
6753 when Attribute_Implicit_Dereference =>
6754
6755 -- Legality checks already performed at the point of the type
6756 -- declaration, aspect is not delayed.
6757
6758 null;
6759
6760 -----------
6761 -- Input --
6762 -----------
6763
6764 when Attribute_Input =>
6765 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
6766 Set_Has_Specified_Stream_Input (Ent);
6767
6768 ------------------------
6769 -- Interrupt_Priority --
6770 ------------------------
6771
6772 when Attribute_Interrupt_Priority =>
6773 pragma Assert (From_Aspect_Specification (N));
6774 -- The parser forbids this clause in source code, so it must have
6775 -- come from an aspect specification.
6776
6777 if not Is_Concurrent_Type (U_Ent) then
6778 Error_Msg_N
6779 ("Interrupt_Priority can only be defined for task and "
6780 & "protected object", Nam);
6781
6782 elsif Duplicate_Clause then
6783 null;
6784
6785 else
6786 -- The expression must be analyzed in the special manner
6787 -- described in "Handling of Default and Per-Object
6788 -- Expressions" in sem.ads.
6789
6790 -- The visibility to the components must be restored
6791
6792 Push_Type (U_Ent);
6793
6794 Preanalyze_Spec_Expression
6795 (Expr, RTE (RE_Interrupt_Priority));
6796
6797 Pop_Type (U_Ent);
6798
6799 -- Check the No_Task_At_Interrupt_Priority restriction
6800
6801 if Is_Task_Type (U_Ent) then
6802 Check_Restriction (No_Task_At_Interrupt_Priority, N);
6803 end if;
6804 end if;
6805
6806 --------------
6807 -- Iterable --
6808 --------------
6809
6810 when Attribute_Iterable =>
6811 Analyze (Expr);
6812
6813 if Nkind (Expr) /= N_Aggregate then
6814 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
6815 end if;
6816
6817 declare
6818 Assoc : Node_Id;
6819
6820 begin
6821 Assoc := First (Component_Associations (Expr));
6822 while Present (Assoc) loop
6823 Analyze (Expression (Assoc));
6824
6825 if not Is_Entity_Name (Expression (Assoc)) then
6826 Error_Msg_N ("value must be a function", Assoc);
6827 end if;
6828
6829 Next (Assoc);
6830 end loop;
6831 end;
6832
6833 ----------------------
6834 -- Iterator_Element --
6835 ----------------------
6836
6837 when Attribute_Iterator_Element =>
6838 Analyze (Expr);
6839
6840 if not Is_Entity_Name (Expr)
6841 or else not Is_Type (Entity (Expr))
6842 then
6843 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
6844 return;
6845 end if;
6846
6847 -------------------
6848 -- Machine_Radix --
6849 -------------------
6850
6851 -- Machine radix attribute definition clause
6852
6853 when Attribute_Machine_Radix => Machine_Radix : declare
6854 Radix : constant Uint := Static_Integer (Expr);
6855
6856 begin
6857 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
6858 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
6859
6860 elsif Duplicate_Clause then
6861 null;
6862
6863 elsif Radix /= No_Uint then
6864 Set_Has_Machine_Radix_Clause (U_Ent);
6865 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
6866
6867 if Radix = 2 then
6868 null;
6869
6870 elsif Radix = 10 then
6871 Set_Machine_Radix_10 (U_Ent);
6872
6873 else
6874 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
6875 end if;
6876 end if;
6877 end Machine_Radix;
6878
6879 -----------------
6880 -- Object_Size --
6881 -----------------
6882
6883 -- Object_Size attribute definition clause
6884
6885 when Attribute_Object_Size => Object_Size : declare
6886 Size : constant Uint := Static_Integer (Expr);
6887
6888 Biased : Boolean;
6889 pragma Warnings (Off, Biased);
6890
6891 begin
6892 if not Is_Type (U_Ent) then
6893 Error_Msg_N ("Object_Size cannot be given for &", Nam);
6894
6895 elsif Duplicate_Clause then
6896 null;
6897
6898 else
6899 Check_Size (Expr, U_Ent, Size, Biased);
6900
6901 if Size <= 0 then
6902 Error_Msg_N ("Object_Size must be positive", Expr);
6903
6904 elsif Is_Scalar_Type (U_Ent) then
6905 if Size /= 8 and then Size /= 16 and then Size /= 32
6906 and then UI_Mod (Size, 64) /= 0
6907 then
6908 Error_Msg_N
6909 ("Object_Size must be 8, 16, 32, or multiple of 64",
6910 Expr);
6911 end if;
6912
6913 elsif Size mod 8 /= 0 then
6914 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
6915 end if;
6916
6917 Set_Esize (U_Ent, Size);
6918 Set_Has_Object_Size_Clause (U_Ent);
6919 Alignment_Check_For_Size_Change (U_Ent, Size);
6920 end if;
6921 end Object_Size;
6922
6923 ------------
6924 -- Output --
6925 ------------
6926
6927 when Attribute_Output =>
6928 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
6929 Set_Has_Specified_Stream_Output (Ent);
6930
6931 --------------
6932 -- Priority --
6933 --------------
6934
6935 when Attribute_Priority =>
6936
6937 -- Priority attribute definition clause not allowed except from
6938 -- aspect specification.
6939
6940 if From_Aspect_Specification (N) then
6941 if not (Is_Concurrent_Type (U_Ent)
6942 or else Ekind (U_Ent) = E_Procedure)
6943 then
6944 Error_Msg_N
6945 ("Priority can only be defined for task and protected "
6946 & "object", Nam);
6947
6948 elsif Duplicate_Clause then
6949 null;
6950
6951 else
6952 -- The expression must be analyzed in the special manner
6953 -- described in "Handling of Default and Per-Object
6954 -- Expressions" in sem.ads.
6955
6956 -- The visibility to the components must be restored
6957
6958 Push_Type (U_Ent);
6959 Preanalyze_Spec_Expression (Expr, Standard_Integer);
6960 Pop_Type (U_Ent);
6961
6962 if not Is_OK_Static_Expression (Expr) then
6963 Check_Restriction (Static_Priorities, Expr);
6964 end if;
6965 end if;
6966
6967 else
6968 Error_Msg_N
6969 ("attribute& cannot be set with definition clause", N);
6970 end if;
6971
6972 ---------------
6973 -- Put_Image --
6974 ---------------
6975
6976 when Attribute_Put_Image =>
6977 Analyze_Put_Image_TSS_Definition;
6978
6979 ----------
6980 -- Read --
6981 ----------
6982
6983 when Attribute_Read =>
6984 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
6985 Set_Has_Specified_Stream_Read (Ent);
6986
6987 --------------------------
6988 -- Scalar_Storage_Order --
6989 --------------------------
6990
6991 -- Scalar_Storage_Order attribute definition clause
6992
6993 when Attribute_Scalar_Storage_Order =>
6994 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
6995 Error_Msg_N
6996 ("Scalar_Storage_Order can only be defined for record or "
6997 & "array type", Nam);
6998
6999 elsif Duplicate_Clause then
7000 null;
7001
7002 else
7003 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
7004
7005 if Etype (Expr) = Any_Type then
7006 return;
7007
7008 elsif not Is_OK_Static_Expression (Expr) then
7009 Flag_Non_Static_Expr
7010 ("Scalar_Storage_Order requires static expression!", Expr);
7011
7012 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
7013
7014 -- Here for the case of a non-default (i.e. non-confirming)
7015 -- Scalar_Storage_Order attribute definition.
7016
7017 if Support_Nondefault_SSO_On_Target then
7018 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
7019 else
7020 Error_Msg_N
7021 ("non-default Scalar_Storage_Order not supported on "
7022 & "target", Expr);
7023 end if;
7024 end if;
7025
7026 -- Clear SSO default indications since explicit setting of the
7027 -- order overrides the defaults.
7028
7029 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
7030 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
7031 end if;
7032
7033 ----------
7034 -- Size --
7035 ----------
7036
7037 -- Size attribute definition clause
7038
7039 when Attribute_Size => Size : declare
7040 Size : constant Uint := Static_Integer (Expr);
7041 Etyp : Entity_Id;
7042 Biased : Boolean;
7043
7044 begin
7045 FOnly := True;
7046
7047 if Duplicate_Clause then
7048 null;
7049
7050 elsif not Is_Type (U_Ent)
7051 and then Ekind (U_Ent) /= E_Variable
7052 and then Ekind (U_Ent) /= E_Constant
7053 then
7054 Error_Msg_N ("size cannot be given for &", Nam);
7055
7056 elsif Is_Array_Type (U_Ent)
7057 and then not Is_Constrained (U_Ent)
7058 then
7059 Error_Msg_N
7060 ("size cannot be given for unconstrained array", Nam);
7061
7062 elsif Size /= No_Uint then
7063 if Is_Type (U_Ent) then
7064 Etyp := U_Ent;
7065 else
7066 Etyp := Etype (U_Ent);
7067 end if;
7068
7069 -- Check size, note that Gigi is in charge of checking that the
7070 -- size of an array or record type is OK. Also we do not check
7071 -- the size in the ordinary fixed-point case, since it is too
7072 -- early to do so (there may be subsequent small clause that
7073 -- affects the size). We can check the size if a small clause
7074 -- has already been given.
7075
7076 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
7077 or else Has_Small_Clause (U_Ent)
7078 then
7079 Check_Size (Expr, Etyp, Size, Biased);
7080 Set_Biased (U_Ent, N, "size clause", Biased);
7081 end if;
7082
7083 -- For types set RM_Size and Esize if possible
7084
7085 if Is_Type (U_Ent) then
7086 Set_RM_Size (U_Ent, Size);
7087
7088 -- For elementary types, increase Object_Size to power of 2,
7089 -- but not less than a storage unit in any case (normally
7090 -- this means it will be byte addressable).
7091
7092 -- For all other types, nothing else to do, we leave Esize
7093 -- (object size) unset, the back end will set it from the
7094 -- size and alignment in an appropriate manner.
7095
7096 -- In both cases, we check whether the alignment must be
7097 -- reset in the wake of the size change.
7098
7099 if Is_Elementary_Type (U_Ent) then
7100 if Size <= System_Storage_Unit then
7101 Init_Esize (U_Ent, System_Storage_Unit);
7102 elsif Size <= 16 then
7103 Init_Esize (U_Ent, 16);
7104 elsif Size <= 32 then
7105 Init_Esize (U_Ent, 32);
7106 else
7107 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
7108 end if;
7109
7110 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
7111 else
7112 Alignment_Check_For_Size_Change (U_Ent, Size);
7113 end if;
7114
7115 -- For objects, set Esize only
7116
7117 else
7118 if Is_Elementary_Type (Etyp)
7119 and then Size /= System_Storage_Unit
7120 and then Size /= 16
7121 and then Size /= 32
7122 and then Size /= 64
7123 and then Size /= System_Max_Integer_Size
7124 then
7125 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
7126 Error_Msg_Uint_2 := UI_From_Int (System_Max_Integer_Size);
7127 Error_Msg_N
7128 ("size for primitive object must be a power of 2 in "
7129 & "the range ^-^", N);
7130 end if;
7131
7132 Set_Esize (U_Ent, Size);
7133 end if;
7134
7135 Set_Has_Size_Clause (U_Ent);
7136 end if;
7137 end Size;
7138
7139 -----------
7140 -- Small --
7141 -----------
7142
7143 -- Small attribute definition clause
7144
7145 when Attribute_Small => Small : declare
7146 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
7147 Small : Ureal;
7148
7149 begin
7150 Analyze_And_Resolve (Expr, Any_Real);
7151
7152 if Etype (Expr) = Any_Type then
7153 return;
7154
7155 elsif not Is_OK_Static_Expression (Expr) then
7156 Flag_Non_Static_Expr
7157 ("small requires static expression!", Expr);
7158 return;
7159
7160 else
7161 Small := Expr_Value_R (Expr);
7162
7163 if Small <= Ureal_0 then
7164 Error_Msg_N ("small value must be greater than zero", Expr);
7165 return;
7166 end if;
7167
7168 end if;
7169
7170 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
7171 Error_Msg_N
7172 ("small requires an ordinary fixed point type", Nam);
7173
7174 elsif Has_Small_Clause (U_Ent) then
7175 Error_Msg_N ("small already given for &", Nam);
7176
7177 elsif Small > Delta_Value (U_Ent) then
7178 Error_Msg_N
7179 ("small value must not be greater than delta value", Nam);
7180
7181 else
7182 Set_Small_Value (U_Ent, Small);
7183 Set_Small_Value (Implicit_Base, Small);
7184 Set_Has_Small_Clause (U_Ent);
7185 Set_Has_Small_Clause (Implicit_Base);
7186 Set_Has_Non_Standard_Rep (Implicit_Base);
7187 end if;
7188 end Small;
7189
7190 ------------------
7191 -- Storage_Pool --
7192 ------------------
7193
7194 -- Storage_Pool attribute definition clause
7195
7196 when Attribute_Simple_Storage_Pool
7197 | Attribute_Storage_Pool
7198 =>
7199 Storage_Pool : declare
7200 Pool : Entity_Id;
7201 T : Entity_Id;
7202
7203 procedure Associate_Storage_Pool
7204 (Ent : Entity_Id; Pool : Entity_Id);
7205 -- Associate Pool to Ent and perform legality checks on subpools
7206
7207 ----------------------------
7208 -- Associate_Storage_Pool --
7209 ----------------------------
7210
7211 procedure Associate_Storage_Pool
7212 (Ent : Entity_Id; Pool : Entity_Id)
7213 is
7214 function Object_From (Pool : Entity_Id) return Entity_Id;
7215 -- Return the entity of which Pool is a part of
7216
7217 -----------------
7218 -- Object_From --
7219 -----------------
7220
7221 function Object_From
7222 (Pool : Entity_Id) return Entity_Id
7223 is
7224 N : Node_Id := Pool;
7225 begin
7226 if Present (Renamed_Object (Pool)) then
7227 N := Renamed_Object (Pool);
7228 end if;
7229
7230 while Present (N) loop
7231 case Nkind (N) is
7232 when N_Defining_Identifier =>
7233 return N;
7234
7235 when N_Identifier | N_Expanded_Name =>
7236 return Entity (N);
7237
7238 when N_Indexed_Component | N_Selected_Component |
7239 N_Explicit_Dereference
7240 =>
7241 N := Prefix (N);
7242
7243 when N_Type_Conversion =>
7244 N := Expression (N);
7245
7246 when others =>
7247 -- ??? we probably should handle more cases but
7248 -- this is good enough in practice for this check
7249 -- on a corner case.
7250
7251 return Empty;
7252 end case;
7253 end loop;
7254
7255 return Empty;
7256 end Object_From;
7257
7258 Obj : Entity_Id;
7259
7260 begin
7261 Set_Associated_Storage_Pool (Ent, Pool);
7262
7263 -- Check RM 13.11.4(22-23/3): a specification of a storage pool
7264 -- is illegal if the storage pool supports subpools and:
7265 -- (A) The access type is a general access type.
7266 -- (B) The access type is statically deeper than the storage
7267 -- pool object;
7268 -- (C) The storage pool object is a part of a formal parameter;
7269 -- (D) The storage pool object is a part of the dereference of
7270 -- a non-library level general access type;
7271
7272 if Ada_Version >= Ada_2012
7273 and then RTU_Loaded (System_Storage_Pools_Subpools)
7274 and then
7275 Is_Ancestor (RTE (RE_Root_Storage_Pool_With_Subpools),
7276 Etype (Pool))
7277 then
7278 -- check (A)
7279
7280 if Ekind (Etype (Ent)) = E_General_Access_Type then
7281 Error_Msg_N
7282 ("subpool cannot be used on general access type", Ent);
7283 end if;
7284
7285 -- check (B)
7286
7287 if Type_Access_Level (Ent)
7288 > Static_Accessibility_Level
7289 (Pool, Object_Decl_Level)
7290 then
7291 Error_Msg_N
7292 ("subpool access type has deeper accessibility "
7293 & "level than pool", Ent);
7294 return;
7295 end if;
7296
7297 Obj := Object_From (Pool);
7298
7299 -- check (C)
7300
7301 if Present (Obj) and then Is_Formal (Obj) then
7302 Error_Msg_N
7303 ("subpool cannot be part of a parameter", Ent);
7304 return;
7305 end if;
7306
7307 -- check (D)
7308
7309 if Present (Obj)
7310 and then Ekind (Etype (Obj)) = E_General_Access_Type
7311 and then not Is_Library_Level_Entity (Etype (Obj))
7312 then
7313 Error_Msg_N
7314 ("subpool cannot be part of the dereference of a " &
7315 "nested general access type", Ent);
7316 return;
7317 end if;
7318 end if;
7319 end Associate_Storage_Pool;
7320
7321 begin
7322 if Ekind (U_Ent) = E_Access_Subprogram_Type then
7323 Error_Msg_N
7324 ("storage pool cannot be given for access-to-subprogram type",
7325 Nam);
7326 return;
7327
7328 elsif Ekind (U_Ent) not in E_Access_Type | E_General_Access_Type
7329 then
7330 Error_Msg_N
7331 ("storage pool can only be given for access types", Nam);
7332 return;
7333
7334 elsif Is_Derived_Type (U_Ent) then
7335 Error_Msg_N
7336 ("storage pool cannot be given for a derived access type",
7337 Nam);
7338
7339 elsif Duplicate_Clause then
7340 return;
7341
7342 elsif Present (Associated_Storage_Pool (U_Ent)) then
7343 Error_Msg_N ("storage pool already given for &", Nam);
7344 return;
7345 end if;
7346
7347 -- Check for Storage_Size previously given
7348
7349 declare
7350 SS : constant Node_Id :=
7351 Get_Attribute_Definition_Clause
7352 (U_Ent, Attribute_Storage_Size);
7353 begin
7354 if Present (SS) then
7355 Check_Pool_Size_Clash (U_Ent, N, SS);
7356 end if;
7357 end;
7358
7359 -- Storage_Pool case
7360
7361 if Id = Attribute_Storage_Pool then
7362 Analyze_And_Resolve
7363 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
7364
7365 -- In the Simple_Storage_Pool case, we allow a variable of any
7366 -- simple storage pool type, so we Resolve without imposing an
7367 -- expected type.
7368
7369 else
7370 Analyze_And_Resolve (Expr);
7371
7372 if not Present (Get_Rep_Pragma
7373 (Etype (Expr), Name_Simple_Storage_Pool_Type))
7374 then
7375 Error_Msg_N
7376 ("expression must be of a simple storage pool type", Expr);
7377 end if;
7378 end if;
7379
7380 if not Denotes_Variable (Expr) then
7381 Error_Msg_N ("storage pool must be a variable", Expr);
7382 return;
7383 end if;
7384
7385 if Nkind (Expr) = N_Type_Conversion then
7386 T := Etype (Expression (Expr));
7387 else
7388 T := Etype (Expr);
7389 end if;
7390
7391 -- The Stack_Bounded_Pool is used internally for implementing
7392 -- access types with a Storage_Size. Since it only work properly
7393 -- when used on one specific type, we need to check that it is not
7394 -- hijacked improperly:
7395
7396 -- type T is access Integer;
7397 -- for T'Storage_Size use n;
7398 -- type Q is access Float;
7399 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
7400
7401 if RTE_Available (RE_Stack_Bounded_Pool)
7402 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
7403 then
7404 Error_Msg_N ("non-shareable internal Pool", Expr);
7405 return;
7406 end if;
7407
7408 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7409 -- Storage_Pool since this attribute cannot be defined for such
7410 -- types (RM E.2.2(17)).
7411
7412 Validate_Remote_Access_To_Class_Wide_Type (N);
7413
7414 -- If the argument is a name that is not an entity name, then
7415 -- we construct a renaming operation to define an entity of
7416 -- type storage pool.
7417
7418 if not Is_Entity_Name (Expr)
7419 and then Is_Object_Reference (Expr)
7420 then
7421 Pool := Make_Temporary (Loc, 'P', Expr);
7422
7423 declare
7424 Rnode : constant Node_Id :=
7425 Make_Object_Renaming_Declaration (Loc,
7426 Defining_Identifier => Pool,
7427 Subtype_Mark =>
7428 New_Occurrence_Of (Etype (Expr), Loc),
7429 Name => Expr);
7430
7431 begin
7432 -- If the attribute definition clause comes from an aspect
7433 -- clause, then insert the renaming before the associated
7434 -- entity's declaration, since the attribute clause has
7435 -- not yet been appended to the declaration list.
7436
7437 if From_Aspect_Specification (N) then
7438 Insert_Before (Parent (Entity (N)), Rnode);
7439 else
7440 Insert_Before (N, Rnode);
7441 end if;
7442
7443 Analyze (Rnode);
7444 Associate_Storage_Pool (U_Ent, Pool);
7445 end;
7446
7447 elsif Is_Entity_Name (Expr) then
7448 Pool := Entity (Expr);
7449
7450 -- If pool is a renamed object, get original one. This can
7451 -- happen with an explicit renaming, and within instances.
7452
7453 while Present (Renamed_Object (Pool))
7454 and then Is_Entity_Name (Renamed_Object (Pool))
7455 loop
7456 Pool := Entity (Renamed_Object (Pool));
7457 end loop;
7458
7459 if Present (Renamed_Object (Pool))
7460 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
7461 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
7462 then
7463 Pool := Entity (Expression (Renamed_Object (Pool)));
7464 end if;
7465
7466 Associate_Storage_Pool (U_Ent, Pool);
7467
7468 elsif Nkind (Expr) = N_Type_Conversion
7469 and then Is_Entity_Name (Expression (Expr))
7470 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
7471 then
7472 Pool := Entity (Expression (Expr));
7473 Associate_Storage_Pool (U_Ent, Pool);
7474
7475 else
7476 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
7477 return;
7478 end if;
7479 end Storage_Pool;
7480
7481 ------------------
7482 -- Storage_Size --
7483 ------------------
7484
7485 -- Storage_Size attribute definition clause
7486
7487 when Attribute_Storage_Size => Storage_Size : declare
7488 Btype : constant Entity_Id := Base_Type (U_Ent);
7489
7490 begin
7491 if Is_Task_Type (U_Ent) then
7492
7493 -- Check obsolescent (but never obsolescent if from aspect)
7494
7495 if not From_Aspect_Specification (N) then
7496 Check_Restriction (No_Obsolescent_Features, N);
7497
7498 if Warn_On_Obsolescent_Feature then
7499 Error_Msg_N
7500 ("?j?storage size clause for task is an obsolescent "
7501 & "feature (RM J.9)", N);
7502 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
7503 end if;
7504 end if;
7505
7506 FOnly := True;
7507 end if;
7508
7509 if not Is_Access_Type (U_Ent)
7510 and then Ekind (U_Ent) /= E_Task_Type
7511 then
7512 Error_Msg_N ("storage size cannot be given for &", Nam);
7513
7514 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
7515 Error_Msg_N
7516 ("storage size cannot be given for a derived access type",
7517 Nam);
7518
7519 elsif Duplicate_Clause then
7520 null;
7521
7522 else
7523 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7524 -- Storage_Size since this attribute cannot be defined for such
7525 -- types (RM E.2.2(17)).
7526
7527 Validate_Remote_Access_To_Class_Wide_Type (N);
7528
7529 Analyze_And_Resolve (Expr, Any_Integer);
7530
7531 if Is_Access_Type (U_Ent) then
7532
7533 -- Check for Storage_Pool previously given
7534
7535 declare
7536 SP : constant Node_Id :=
7537 Get_Attribute_Definition_Clause
7538 (U_Ent, Attribute_Storage_Pool);
7539
7540 begin
7541 if Present (SP) then
7542 Check_Pool_Size_Clash (U_Ent, SP, N);
7543 end if;
7544 end;
7545
7546 -- Special case of for x'Storage_Size use 0
7547
7548 if Is_OK_Static_Expression (Expr)
7549 and then Expr_Value (Expr) = 0
7550 then
7551 Set_No_Pool_Assigned (Btype);
7552 end if;
7553 end if;
7554
7555 Set_Has_Storage_Size_Clause (Btype);
7556 end if;
7557 end Storage_Size;
7558
7559 -----------------
7560 -- Stream_Size --
7561 -----------------
7562
7563 when Attribute_Stream_Size => Stream_Size : declare
7564 Size : constant Uint := Static_Integer (Expr);
7565
7566 begin
7567 if Ada_Version <= Ada_95 then
7568 Check_Restriction (No_Implementation_Attributes, N);
7569 end if;
7570
7571 if Duplicate_Clause then
7572 null;
7573
7574 elsif Is_Elementary_Type (U_Ent) then
7575 if Size /= System_Storage_Unit
7576 and then Size /= System_Storage_Unit * 2
7577 and then Size /= System_Storage_Unit * 3
7578 and then Size /= System_Storage_Unit * 4
7579 and then Size /= System_Storage_Unit * 8
7580 then
7581 Error_Msg_N
7582 ("stream size for elementary type must be 8, 16, 24, " &
7583 "32 or 64", N);
7584
7585 elsif RM_Size (U_Ent) > Size then
7586 Error_Msg_Uint_1 := RM_Size (U_Ent);
7587 Error_Msg_N
7588 ("stream size for elementary type must be 8, 16, 24, " &
7589 "32 or 64 and at least ^", N);
7590 end if;
7591
7592 Set_Has_Stream_Size_Clause (U_Ent);
7593
7594 else
7595 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
7596 end if;
7597 end Stream_Size;
7598
7599 ----------------
7600 -- Value_Size --
7601 ----------------
7602
7603 -- Value_Size attribute definition clause
7604
7605 when Attribute_Value_Size => Value_Size : declare
7606 Size : constant Uint := Static_Integer (Expr);
7607 Biased : Boolean;
7608
7609 begin
7610 if not Is_Type (U_Ent) then
7611 Error_Msg_N ("Value_Size cannot be given for &", Nam);
7612
7613 elsif Duplicate_Clause then
7614 null;
7615
7616 elsif Is_Array_Type (U_Ent)
7617 and then not Is_Constrained (U_Ent)
7618 then
7619 Error_Msg_N
7620 ("Value_Size cannot be given for unconstrained array", Nam);
7621
7622 else
7623 if Is_Elementary_Type (U_Ent) then
7624 Check_Size (Expr, U_Ent, Size, Biased);
7625 Set_Biased (U_Ent, N, "value size clause", Biased);
7626 end if;
7627
7628 Set_RM_Size (U_Ent, Size);
7629 end if;
7630 end Value_Size;
7631
7632 -----------------------
7633 -- Variable_Indexing --
7634 -----------------------
7635
7636 when Attribute_Variable_Indexing =>
7637 Check_Indexing_Functions;
7638
7639 -----------
7640 -- Write --
7641 -----------
7642
7643 when Attribute_Write =>
7644 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
7645 Set_Has_Specified_Stream_Write (Ent);
7646
7647 -- All other attributes cannot be set
7648
7649 when others =>
7650 Error_Msg_N
7651 ("attribute& cannot be set with definition clause", N);
7652 end case;
7653
7654 -- The test for the type being frozen must be performed after any
7655 -- expression the clause has been analyzed since the expression itself
7656 -- might cause freezing that makes the clause illegal.
7657
7658 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
7659 return;
7660 end if;
7661 end Analyze_Attribute_Definition_Clause;
7662
7663 ----------------------------
7664 -- Analyze_Code_Statement --
7665 ----------------------------
7666
7667 procedure Analyze_Code_Statement (N : Node_Id) is
7668 HSS : constant Node_Id := Parent (N);
7669 SBody : constant Node_Id := Parent (HSS);
7670 Subp : constant Entity_Id := Current_Scope;
7671 Stmt : Node_Id;
7672 Decl : Node_Id;
7673 StmtO : Node_Id;
7674 DeclO : Node_Id;
7675
7676 begin
7677 -- Accept foreign code statements for CodePeer. The analysis is skipped
7678 -- to avoid rejecting unrecognized constructs.
7679
7680 if CodePeer_Mode then
7681 Set_Analyzed (N);
7682 return;
7683 end if;
7684
7685 -- Analyze and check we get right type, note that this implements the
7686 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
7687 -- the only way that Asm_Insn could possibly be visible.
7688
7689 Analyze_And_Resolve (Expression (N));
7690
7691 if Etype (Expression (N)) = Any_Type then
7692 return;
7693 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
7694 Error_Msg_N ("incorrect type for code statement", N);
7695 return;
7696 end if;
7697
7698 Check_Code_Statement (N);
7699
7700 -- Make sure we appear in the handled statement sequence of a subprogram
7701 -- (RM 13.8(3)).
7702
7703 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
7704 or else Nkind (SBody) /= N_Subprogram_Body
7705 then
7706 Error_Msg_N
7707 ("code statement can only appear in body of subprogram", N);
7708 return;
7709 end if;
7710
7711 -- Do remaining checks (RM 13.8(3)) if not already done
7712
7713 if not Is_Machine_Code_Subprogram (Subp) then
7714 Set_Is_Machine_Code_Subprogram (Subp);
7715
7716 -- No exception handlers allowed
7717
7718 if Present (Exception_Handlers (HSS)) then
7719 Error_Msg_N
7720 ("exception handlers not permitted in machine code subprogram",
7721 First (Exception_Handlers (HSS)));
7722 end if;
7723
7724 -- No declarations other than use clauses and pragmas (we allow
7725 -- certain internally generated declarations as well).
7726
7727 Decl := First (Declarations (SBody));
7728 while Present (Decl) loop
7729 DeclO := Original_Node (Decl);
7730 if Comes_From_Source (DeclO)
7731 and Nkind (DeclO) not in N_Pragma
7732 | N_Use_Package_Clause
7733 | N_Use_Type_Clause
7734 | N_Implicit_Label_Declaration
7735 then
7736 Error_Msg_N
7737 ("this declaration not allowed in machine code subprogram",
7738 DeclO);
7739 end if;
7740
7741 Next (Decl);
7742 end loop;
7743
7744 -- No statements other than code statements, pragmas, and labels.
7745 -- Again we allow certain internally generated statements.
7746
7747 -- In Ada 2012, qualified expressions are names, and the code
7748 -- statement is initially parsed as a procedure call.
7749
7750 Stmt := First (Statements (HSS));
7751 while Present (Stmt) loop
7752 StmtO := Original_Node (Stmt);
7753
7754 -- A procedure call transformed into a code statement is OK
7755
7756 if Ada_Version >= Ada_2012
7757 and then Nkind (StmtO) = N_Procedure_Call_Statement
7758 and then Nkind (Name (StmtO)) = N_Qualified_Expression
7759 then
7760 null;
7761
7762 elsif Comes_From_Source (StmtO)
7763 and then Nkind (StmtO) not in
7764 N_Pragma | N_Label | N_Code_Statement
7765 then
7766 Error_Msg_N
7767 ("this statement is not allowed in machine code subprogram",
7768 StmtO);
7769 end if;
7770
7771 Next (Stmt);
7772 end loop;
7773 end if;
7774 end Analyze_Code_Statement;
7775
7776 -----------------------------------------------
7777 -- Analyze_Enumeration_Representation_Clause --
7778 -----------------------------------------------
7779
7780 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
7781 Ident : constant Node_Id := Identifier (N);
7782 Aggr : constant Node_Id := Array_Aggregate (N);
7783 Enumtype : Entity_Id;
7784 Elit : Entity_Id;
7785 Expr : Node_Id;
7786 Assoc : Node_Id;
7787 Choice : Node_Id;
7788 Val : Uint;
7789
7790 Err : Boolean := False;
7791 -- Set True to avoid cascade errors and crashes on incorrect source code
7792
7793 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
7794 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
7795 -- Allowed range of universal integer (= allowed range of enum lit vals)
7796
7797 Min : Uint;
7798 Max : Uint;
7799 -- Minimum and maximum values of entries
7800
7801 Max_Node : Node_Id := Empty; -- init to avoid warning
7802 -- Pointer to node for literal providing max value
7803
7804 begin
7805 if Ignore_Rep_Clauses then
7806 Kill_Rep_Clause (N);
7807 return;
7808 end if;
7809
7810 -- Ignore enumeration rep clauses by default in CodePeer mode,
7811 -- unless -gnatd.I is specified, as a work around for potential false
7812 -- positive messages.
7813
7814 if CodePeer_Mode and not Debug_Flag_Dot_II then
7815 return;
7816 end if;
7817
7818 -- First some basic error checks
7819
7820 Find_Type (Ident);
7821 Enumtype := Entity (Ident);
7822
7823 if Enumtype = Any_Type
7824 or else Rep_Item_Too_Early (Enumtype, N)
7825 then
7826 return;
7827 else
7828 Enumtype := Underlying_Type (Enumtype);
7829 end if;
7830
7831 if not Is_Enumeration_Type (Enumtype) then
7832 Error_Msg_NE
7833 ("enumeration type required, found}",
7834 Ident, First_Subtype (Enumtype));
7835 return;
7836 end if;
7837
7838 -- Ignore rep clause on generic actual type. This will already have
7839 -- been flagged on the template as an error, and this is the safest
7840 -- way to ensure we don't get a junk cascaded message in the instance.
7841
7842 if Is_Generic_Actual_Type (Enumtype) then
7843 return;
7844
7845 -- Type must be in current scope
7846
7847 elsif Scope (Enumtype) /= Current_Scope then
7848 Error_Msg_N ("type must be declared in this scope", Ident);
7849 return;
7850
7851 -- Type must be a first subtype
7852
7853 elsif not Is_First_Subtype (Enumtype) then
7854 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
7855 return;
7856
7857 -- Ignore duplicate rep clause
7858
7859 elsif Has_Enumeration_Rep_Clause (Enumtype) then
7860 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
7861 return;
7862
7863 -- Don't allow rep clause for standard [wide_[wide_]]character
7864
7865 elsif Is_Standard_Character_Type (Enumtype) then
7866 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
7867 return;
7868
7869 -- Check that the expression is a proper aggregate (no parentheses)
7870
7871 elsif Paren_Count (Aggr) /= 0 then
7872 Error_Msg
7873 ("extra parentheses surrounding aggregate not allowed",
7874 First_Sloc (Aggr));
7875 return;
7876
7877 -- All tests passed, so set rep clause in place
7878
7879 else
7880 Set_Has_Enumeration_Rep_Clause (Enumtype);
7881 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
7882 end if;
7883
7884 -- Now we process the aggregate. Note that we don't use the normal
7885 -- aggregate code for this purpose, because we don't want any of the
7886 -- normal expansion activities, and a number of special semantic
7887 -- rules apply (including the component type being any integer type)
7888
7889 Elit := First_Literal (Enumtype);
7890
7891 -- First the positional entries if any
7892
7893 if Present (Expressions (Aggr)) then
7894 Expr := First (Expressions (Aggr));
7895 while Present (Expr) loop
7896 if No (Elit) then
7897 Error_Msg_N ("too many entries in aggregate", Expr);
7898 return;
7899 end if;
7900
7901 Val := Static_Integer (Expr);
7902
7903 -- Err signals that we found some incorrect entries processing
7904 -- the list. The final checks for completeness and ordering are
7905 -- skipped in this case.
7906
7907 if Val = No_Uint then
7908 Err := True;
7909
7910 elsif Val < Lo or else Hi < Val then
7911 Error_Msg_N ("value outside permitted range", Expr);
7912 Err := True;
7913 end if;
7914
7915 Set_Enumeration_Rep (Elit, Val);
7916 Set_Enumeration_Rep_Expr (Elit, Expr);
7917 Next (Expr);
7918 Next (Elit);
7919 end loop;
7920 end if;
7921
7922 -- Now process the named entries if present
7923
7924 if Present (Component_Associations (Aggr)) then
7925 Assoc := First (Component_Associations (Aggr));
7926 while Present (Assoc) loop
7927 Choice := First (Choices (Assoc));
7928
7929 if Present (Next (Choice)) then
7930 Error_Msg_N
7931 ("multiple choice not allowed here", Next (Choice));
7932 Err := True;
7933 end if;
7934
7935 if Nkind (Choice) = N_Others_Choice then
7936 Error_Msg_N ("others choice not allowed here", Choice);
7937 Err := True;
7938
7939 elsif Nkind (Choice) = N_Range then
7940
7941 -- ??? should allow zero/one element range here
7942
7943 Error_Msg_N ("range not allowed here", Choice);
7944 Err := True;
7945
7946 else
7947 Analyze_And_Resolve (Choice, Enumtype);
7948
7949 if Error_Posted (Choice) then
7950 Err := True;
7951 end if;
7952
7953 if not Err then
7954 if Is_Entity_Name (Choice)
7955 and then Is_Type (Entity (Choice))
7956 then
7957 Error_Msg_N ("subtype name not allowed here", Choice);
7958 Err := True;
7959
7960 -- ??? should allow static subtype with zero/one entry
7961
7962 elsif Etype (Choice) = Base_Type (Enumtype) then
7963 if not Is_OK_Static_Expression (Choice) then
7964 Flag_Non_Static_Expr
7965 ("non-static expression used for choice!", Choice);
7966 Err := True;
7967
7968 else
7969 Elit := Expr_Value_E (Choice);
7970
7971 if Present (Enumeration_Rep_Expr (Elit)) then
7972 Error_Msg_Sloc :=
7973 Sloc (Enumeration_Rep_Expr (Elit));
7974 Error_Msg_NE
7975 ("representation for& previously given#",
7976 Choice, Elit);
7977 Err := True;
7978 end if;
7979
7980 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
7981
7982 Expr := Expression (Assoc);
7983 Val := Static_Integer (Expr);
7984
7985 if Val = No_Uint then
7986 Err := True;
7987
7988 elsif Val < Lo or else Hi < Val then
7989 Error_Msg_N ("value outside permitted range", Expr);
7990 Err := True;
7991 end if;
7992
7993 Set_Enumeration_Rep (Elit, Val);
7994 end if;
7995 end if;
7996 end if;
7997 end if;
7998
7999 Next (Assoc);
8000 end loop;
8001 end if;
8002
8003 -- Aggregate is fully processed. Now we check that a full set of
8004 -- representations was given, and that they are in range and in order.
8005 -- These checks are only done if no other errors occurred.
8006
8007 if not Err then
8008 Min := No_Uint;
8009 Max := No_Uint;
8010
8011 Elit := First_Literal (Enumtype);
8012 while Present (Elit) loop
8013 if No (Enumeration_Rep_Expr (Elit)) then
8014 Error_Msg_NE ("missing representation for&!", N, Elit);
8015
8016 else
8017 Val := Enumeration_Rep (Elit);
8018
8019 if Min = No_Uint then
8020 Min := Val;
8021 end if;
8022
8023 if Val /= No_Uint then
8024 if Max /= No_Uint and then Val <= Max then
8025 Error_Msg_NE
8026 ("enumeration value for& not ordered!",
8027 Enumeration_Rep_Expr (Elit), Elit);
8028 end if;
8029
8030 Max_Node := Enumeration_Rep_Expr (Elit);
8031 Max := Val;
8032 end if;
8033
8034 -- If there is at least one literal whose representation is not
8035 -- equal to the Pos value, then note that this enumeration type
8036 -- has a non-standard representation.
8037
8038 if Val /= Enumeration_Pos (Elit) then
8039 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
8040 end if;
8041 end if;
8042
8043 Next (Elit);
8044 end loop;
8045
8046 -- Now set proper size information
8047
8048 declare
8049 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
8050
8051 begin
8052 if Has_Size_Clause (Enumtype) then
8053
8054 -- All OK, if size is OK now
8055
8056 if RM_Size (Enumtype) >= Minsize then
8057 null;
8058
8059 else
8060 -- Try if we can get by with biasing
8061
8062 Minsize :=
8063 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
8064
8065 -- Error message if even biasing does not work
8066
8067 if RM_Size (Enumtype) < Minsize then
8068 Error_Msg_Uint_1 := RM_Size (Enumtype);
8069 Error_Msg_Uint_2 := Max;
8070 Error_Msg_N
8071 ("previously given size (^) is too small "
8072 & "for this value (^)", Max_Node);
8073
8074 -- If biasing worked, indicate that we now have biased rep
8075
8076 else
8077 Set_Biased
8078 (Enumtype, Size_Clause (Enumtype), "size clause");
8079 end if;
8080 end if;
8081
8082 else
8083 Set_RM_Size (Enumtype, Minsize);
8084 Set_Enum_Esize (Enumtype);
8085 end if;
8086
8087 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
8088 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
8089 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
8090 end;
8091 end if;
8092
8093 -- We repeat the too late test in case it froze itself
8094
8095 if Rep_Item_Too_Late (Enumtype, N) then
8096 null;
8097 end if;
8098 end Analyze_Enumeration_Representation_Clause;
8099
8100 ----------------------------
8101 -- Analyze_Free_Statement --
8102 ----------------------------
8103
8104 procedure Analyze_Free_Statement (N : Node_Id) is
8105 begin
8106 Analyze (Expression (N));
8107 end Analyze_Free_Statement;
8108
8109 ---------------------------
8110 -- Analyze_Freeze_Entity --
8111 ---------------------------
8112
8113 procedure Analyze_Freeze_Entity (N : Node_Id) is
8114 begin
8115 Freeze_Entity_Checks (N);
8116 end Analyze_Freeze_Entity;
8117
8118 -----------------------------------
8119 -- Analyze_Freeze_Generic_Entity --
8120 -----------------------------------
8121
8122 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
8123 E : constant Entity_Id := Entity (N);
8124
8125 begin
8126 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
8127 Analyze_Aspects_At_Freeze_Point (E);
8128 end if;
8129
8130 Freeze_Entity_Checks (N);
8131 end Analyze_Freeze_Generic_Entity;
8132
8133 ------------------------------------------
8134 -- Analyze_Record_Representation_Clause --
8135 ------------------------------------------
8136
8137 -- Note: we check as much as we can here, but we can't do any checks
8138 -- based on the position values (e.g. overlap checks) until freeze time
8139 -- because especially in Ada 2005 (machine scalar mode), the processing
8140 -- for non-standard bit order can substantially change the positions.
8141 -- See procedure Check_Record_Representation_Clause (called from Freeze)
8142 -- for the remainder of this processing.
8143
8144 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
8145 Ident : constant Node_Id := Identifier (N);
8146 Biased : Boolean;
8147 CC : Node_Id;
8148 Comp : Entity_Id;
8149 Fbit : Uint;
8150 Lbit : Uint;
8151 Ocomp : Entity_Id;
8152 Posit : Uint;
8153 Rectype : Entity_Id;
8154 Recdef : Node_Id;
8155
8156 function Is_Inherited (Comp : Entity_Id) return Boolean;
8157 -- True if Comp is an inherited component in a record extension
8158
8159 ------------------
8160 -- Is_Inherited --
8161 ------------------
8162
8163 function Is_Inherited (Comp : Entity_Id) return Boolean is
8164 Comp_Base : Entity_Id;
8165
8166 begin
8167 if Ekind (Rectype) = E_Record_Subtype then
8168 Comp_Base := Original_Record_Component (Comp);
8169 else
8170 Comp_Base := Comp;
8171 end if;
8172
8173 return Comp_Base /= Original_Record_Component (Comp_Base);
8174 end Is_Inherited;
8175
8176 -- Local variables
8177
8178 Is_Record_Extension : Boolean;
8179 -- True if Rectype is a record extension
8180
8181 CR_Pragma : Node_Id := Empty;
8182 -- Points to N_Pragma node if Complete_Representation pragma present
8183
8184 -- Start of processing for Analyze_Record_Representation_Clause
8185
8186 begin
8187 if Ignore_Rep_Clauses then
8188 Kill_Rep_Clause (N);
8189 return;
8190 end if;
8191
8192 Find_Type (Ident);
8193 Rectype := Entity (Ident);
8194
8195 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
8196 return;
8197 else
8198 Rectype := Underlying_Type (Rectype);
8199 end if;
8200
8201 -- First some basic error checks
8202
8203 if not Is_Record_Type (Rectype) then
8204 Error_Msg_NE
8205 ("record type required, found}", Ident, First_Subtype (Rectype));
8206 return;
8207
8208 elsif Scope (Rectype) /= Current_Scope then
8209 Error_Msg_N ("type must be declared in this scope", N);
8210 return;
8211
8212 elsif not Is_First_Subtype (Rectype) then
8213 Error_Msg_N ("cannot give record rep clause for subtype", N);
8214 return;
8215
8216 elsif Has_Record_Rep_Clause (Rectype) then
8217 Error_Msg_N ("duplicate record rep clause ignored", N);
8218 return;
8219
8220 elsif Rep_Item_Too_Late (Rectype, N) then
8221 return;
8222 end if;
8223
8224 -- We know we have a first subtype, now possibly go to the anonymous
8225 -- base type to determine whether Rectype is a record extension.
8226
8227 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
8228 Is_Record_Extension :=
8229 Nkind (Recdef) = N_Derived_Type_Definition
8230 and then Present (Record_Extension_Part (Recdef));
8231
8232 if Present (Mod_Clause (N)) then
8233 declare
8234 M : constant Node_Id := Mod_Clause (N);
8235 P : constant List_Id := Pragmas_Before (M);
8236 Ignore : Uint;
8237
8238 begin
8239 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
8240
8241 if Warn_On_Obsolescent_Feature then
8242 Error_Msg_N
8243 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
8244 Error_Msg_N
8245 ("\?j?use alignment attribute definition clause instead", N);
8246 end if;
8247
8248 if Present (P) then
8249 Analyze_List (P);
8250 end if;
8251
8252 -- Get the alignment value to perform error checking
8253
8254 Ignore := Get_Alignment_Value (Expression (M));
8255 end;
8256 end if;
8257
8258 -- For untagged types, clear any existing component clauses for the
8259 -- type. If the type is derived, this is what allows us to override
8260 -- a rep clause for the parent. For type extensions, the representation
8261 -- of the inherited components is inherited, so we want to keep previous
8262 -- component clauses for completeness.
8263
8264 if not Is_Tagged_Type (Rectype) then
8265 Comp := First_Component_Or_Discriminant (Rectype);
8266 while Present (Comp) loop
8267 Set_Component_Clause (Comp, Empty);
8268 Next_Component_Or_Discriminant (Comp);
8269 end loop;
8270 end if;
8271
8272 -- All done if no component clauses
8273
8274 CC := First (Component_Clauses (N));
8275
8276 if No (CC) then
8277 return;
8278 end if;
8279
8280 -- A representation like this applies to the base type
8281
8282 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
8283 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
8284 Set_Has_Specified_Layout (Base_Type (Rectype));
8285
8286 -- Process the component clauses
8287
8288 while Present (CC) loop
8289
8290 -- Pragma
8291
8292 if Nkind (CC) = N_Pragma then
8293 Analyze (CC);
8294
8295 -- The only pragma of interest is Complete_Representation
8296
8297 if Pragma_Name (CC) = Name_Complete_Representation then
8298 CR_Pragma := CC;
8299 end if;
8300
8301 -- Processing for real component clause
8302
8303 else
8304 Posit := Static_Integer (Position (CC));
8305 Fbit := Static_Integer (First_Bit (CC));
8306 Lbit := Static_Integer (Last_Bit (CC));
8307
8308 if Posit /= No_Uint
8309 and then Fbit /= No_Uint
8310 and then Lbit /= No_Uint
8311 then
8312 if Posit < 0 then
8313 Error_Msg_N ("position cannot be negative", Position (CC));
8314
8315 elsif Fbit < 0 then
8316 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
8317
8318 -- The Last_Bit specified in a component clause must not be
8319 -- less than the First_Bit minus one (RM-13.5.1(10)).
8320
8321 elsif Lbit < Fbit - 1 then
8322 Error_Msg_N
8323 ("last bit cannot be less than first bit minus one",
8324 Last_Bit (CC));
8325
8326 -- Values look OK, so find the corresponding record component
8327 -- Even though the syntax allows an attribute reference for
8328 -- implementation-defined components, GNAT does not allow the
8329 -- tag to get an explicit position.
8330
8331 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
8332 if Attribute_Name (Component_Name (CC)) = Name_Tag then
8333 Error_Msg_N ("position of tag cannot be specified", CC);
8334 else
8335 Error_Msg_N ("illegal component name", CC);
8336 end if;
8337
8338 else
8339 Comp := First_Entity (Rectype);
8340 while Present (Comp) loop
8341 exit when Chars (Comp) = Chars (Component_Name (CC));
8342 Next_Entity (Comp);
8343 end loop;
8344
8345 if No (Comp) then
8346
8347 -- Maybe component of base type that is absent from
8348 -- statically constrained first subtype.
8349
8350 Comp := First_Entity (Base_Type (Rectype));
8351 while Present (Comp) loop
8352 exit when Chars (Comp) = Chars (Component_Name (CC));
8353 Next_Entity (Comp);
8354 end loop;
8355 end if;
8356
8357 if No (Comp) then
8358 Error_Msg_N
8359 ("component clause is for non-existent field", CC);
8360
8361 -- Ada 2012 (AI05-0026): Any name that denotes a
8362 -- discriminant of an object of an unchecked union type
8363 -- shall not occur within a record_representation_clause.
8364
8365 -- The general restriction of using record rep clauses on
8366 -- Unchecked_Union types has now been lifted. Since it is
8367 -- possible to introduce a record rep clause which mentions
8368 -- the discriminant of an Unchecked_Union in non-Ada 2012
8369 -- code, this check is applied to all versions of the
8370 -- language.
8371
8372 elsif Ekind (Comp) = E_Discriminant
8373 and then Is_Unchecked_Union (Rectype)
8374 then
8375 Error_Msg_N
8376 ("cannot reference discriminant of unchecked union",
8377 Component_Name (CC));
8378
8379 elsif Is_Record_Extension and then Is_Inherited (Comp) then
8380 Error_Msg_NE
8381 ("component clause not allowed for inherited "
8382 & "component&", CC, Comp);
8383
8384 elsif Present (Component_Clause (Comp)) then
8385
8386 -- Diagnose duplicate rep clause, or check consistency
8387 -- if this is an inherited component. In a double fault,
8388 -- there may be a duplicate inconsistent clause for an
8389 -- inherited component.
8390
8391 if Scope (Original_Record_Component (Comp)) = Rectype
8392 or else Parent (Component_Clause (Comp)) = N
8393 then
8394 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
8395 Error_Msg_N ("component clause previously given#", CC);
8396
8397 else
8398 declare
8399 Rep1 : constant Node_Id := Component_Clause (Comp);
8400 begin
8401 if Intval (Position (Rep1)) /=
8402 Intval (Position (CC))
8403 or else Intval (First_Bit (Rep1)) /=
8404 Intval (First_Bit (CC))
8405 or else Intval (Last_Bit (Rep1)) /=
8406 Intval (Last_Bit (CC))
8407 then
8408 Error_Msg_N
8409 ("component clause inconsistent with "
8410 & "representation of ancestor", CC);
8411
8412 elsif Warn_On_Redundant_Constructs then
8413 Error_Msg_N
8414 ("?r?redundant confirming component clause "
8415 & "for component!", CC);
8416 end if;
8417 end;
8418 end if;
8419
8420 -- Normal case where this is the first component clause we
8421 -- have seen for this entity, so set it up properly.
8422
8423 else
8424 -- Make reference for field in record rep clause and set
8425 -- appropriate entity field in the field identifier.
8426
8427 Generate_Reference
8428 (Comp, Component_Name (CC), Set_Ref => False);
8429 Set_Entity (Component_Name (CC), Comp);
8430
8431 -- Update Fbit and Lbit to the actual bit number
8432
8433 Fbit := Fbit + UI_From_Int (SSU) * Posit;
8434 Lbit := Lbit + UI_From_Int (SSU) * Posit;
8435
8436 if Has_Size_Clause (Rectype)
8437 and then RM_Size (Rectype) <= Lbit
8438 then
8439 Error_Msg_Uint_1 := RM_Size (Rectype);
8440 Error_Msg_Uint_2 := Lbit + 1;
8441 Error_Msg_N ("bit number out of range of specified "
8442 & "size (expected ^, got ^)",
8443 Last_Bit (CC));
8444 else
8445 Set_Component_Clause (Comp, CC);
8446 Set_Component_Bit_Offset (Comp, Fbit);
8447 Set_Esize (Comp, 1 + (Lbit - Fbit));
8448 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
8449 Set_Normalized_Position (Comp, Fbit / SSU);
8450
8451 Set_Normalized_Position_Max
8452 (Comp, Normalized_Position (Comp));
8453
8454 if Warn_On_Overridden_Size
8455 and then Has_Size_Clause (Etype (Comp))
8456 and then RM_Size (Etype (Comp)) /= Esize (Comp)
8457 then
8458 Error_Msg_NE
8459 ("?S?component size overrides size clause for&",
8460 Component_Name (CC), Etype (Comp));
8461 end if;
8462
8463 Check_Size
8464 (Component_Name (CC),
8465 Etype (Comp),
8466 Esize (Comp),
8467 Biased);
8468
8469 Set_Biased
8470 (Comp, First_Node (CC), "component clause", Biased);
8471
8472 -- This information is also set in the corresponding
8473 -- component of the base type, found by accessing the
8474 -- Original_Record_Component link if it is present.
8475
8476 Ocomp := Original_Record_Component (Comp);
8477
8478 if Present (Ocomp) and then Ocomp /= Comp then
8479 Set_Component_Clause (Ocomp, CC);
8480 Set_Component_Bit_Offset (Ocomp, Fbit);
8481 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
8482 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
8483 Set_Normalized_Position (Ocomp, Fbit / SSU);
8484
8485 Set_Normalized_Position_Max
8486 (Ocomp, Normalized_Position (Ocomp));
8487
8488 -- Note: we don't use Set_Biased here, because we
8489 -- already gave a warning above if needed, and we
8490 -- would get a duplicate for the same name here.
8491
8492 Set_Has_Biased_Representation
8493 (Ocomp, Has_Biased_Representation (Comp));
8494 end if;
8495
8496 if Esize (Comp) < 0 then
8497 Error_Msg_N ("component size is negative", CC);
8498 end if;
8499 end if;
8500 end if;
8501 end if;
8502 end if;
8503 end if;
8504
8505 Next (CC);
8506 end loop;
8507
8508 -- Check missing components if Complete_Representation pragma appeared
8509
8510 if Present (CR_Pragma) then
8511 Comp := First_Component_Or_Discriminant (Rectype);
8512 while Present (Comp) loop
8513 if No (Component_Clause (Comp)) then
8514 Error_Msg_NE
8515 ("missing component clause for &", CR_Pragma, Comp);
8516 end if;
8517
8518 Next_Component_Or_Discriminant (Comp);
8519 end loop;
8520
8521 -- Give missing components warning if required
8522
8523 elsif Warn_On_Unrepped_Components then
8524 declare
8525 Num_Repped_Components : Nat := 0;
8526 Num_Unrepped_Components : Nat := 0;
8527
8528 begin
8529 -- First count number of repped and unrepped components
8530
8531 Comp := First_Component_Or_Discriminant (Rectype);
8532 while Present (Comp) loop
8533 if Present (Component_Clause (Comp)) then
8534 Num_Repped_Components := Num_Repped_Components + 1;
8535 else
8536 Num_Unrepped_Components := Num_Unrepped_Components + 1;
8537 end if;
8538
8539 Next_Component_Or_Discriminant (Comp);
8540 end loop;
8541
8542 -- We are only interested in the case where there is at least one
8543 -- unrepped component, and at least half the components have rep
8544 -- clauses. We figure that if less than half have them, then the
8545 -- partial rep clause is really intentional. If the component
8546 -- type has no underlying type set at this point (as for a generic
8547 -- formal type), we don't know enough to give a warning on the
8548 -- component.
8549
8550 if Num_Unrepped_Components > 0
8551 and then Num_Unrepped_Components < Num_Repped_Components
8552 then
8553 Comp := First_Component_Or_Discriminant (Rectype);
8554 while Present (Comp) loop
8555 if No (Component_Clause (Comp))
8556 and then Comes_From_Source (Comp)
8557 and then Present (Underlying_Type (Etype (Comp)))
8558 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
8559 or else Size_Known_At_Compile_Time
8560 (Underlying_Type (Etype (Comp))))
8561 and then not Has_Warnings_Off (Rectype)
8562
8563 -- Ignore discriminant in unchecked union, since it is
8564 -- not there, and cannot have a component clause.
8565
8566 and then (not Is_Unchecked_Union (Rectype)
8567 or else Ekind (Comp) /= E_Discriminant)
8568 then
8569 Error_Msg_Sloc := Sloc (Comp);
8570 Error_Msg_NE
8571 ("?C?no component clause given for & declared #",
8572 N, Comp);
8573 end if;
8574
8575 Next_Component_Or_Discriminant (Comp);
8576 end loop;
8577 end if;
8578 end;
8579 end if;
8580 end Analyze_Record_Representation_Clause;
8581
8582 -------------------------------------
8583 -- Build_Discrete_Static_Predicate --
8584 -------------------------------------
8585
8586 procedure Build_Discrete_Static_Predicate
8587 (Typ : Entity_Id;
8588 Expr : Node_Id;
8589 Nam : Name_Id)
8590 is
8591 Loc : constant Source_Ptr := Sloc (Expr);
8592
8593 Non_Static : exception;
8594 -- Raised if something non-static is found
8595
8596 Btyp : constant Entity_Id := Base_Type (Typ);
8597
8598 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
8599 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
8600 -- Low bound and high bound value of base type of Typ
8601
8602 TLo : Uint;
8603 THi : Uint;
8604 -- Bounds for constructing the static predicate. We use the bound of the
8605 -- subtype if it is static, otherwise the corresponding base type bound.
8606 -- Note: a non-static subtype can have a static predicate.
8607
8608 type REnt is record
8609 Lo, Hi : Uint;
8610 end record;
8611 -- One entry in a Rlist value, a single REnt (range entry) value denotes
8612 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
8613 -- value.
8614
8615 type RList is array (Nat range <>) of REnt;
8616 -- A list of ranges. The ranges are sorted in increasing order, and are
8617 -- disjoint (there is a gap of at least one value between each range in
8618 -- the table). A value is in the set of ranges in Rlist if it lies
8619 -- within one of these ranges.
8620
8621 False_Range : constant RList :=
8622 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
8623 -- An empty set of ranges represents a range list that can never be
8624 -- satisfied, since there are no ranges in which the value could lie,
8625 -- so it does not lie in any of them. False_Range is a canonical value
8626 -- for this empty set, but general processing should test for an Rlist
8627 -- with length zero (see Is_False predicate), since other null ranges
8628 -- may appear which must be treated as False.
8629
8630 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
8631 -- Range representing True, value must be in the base range
8632
8633 function "and" (Left : RList; Right : RList) return RList;
8634 -- And's together two range lists, returning a range list. This is a set
8635 -- intersection operation.
8636
8637 function "or" (Left : RList; Right : RList) return RList;
8638 -- Or's together two range lists, returning a range list. This is a set
8639 -- union operation.
8640
8641 function "not" (Right : RList) return RList;
8642 -- Returns complement of a given range list, i.e. a range list
8643 -- representing all the values in TLo .. THi that are not in the input
8644 -- operand Right.
8645
8646 function Build_Val (V : Uint) return Node_Id;
8647 -- Return an analyzed N_Identifier node referencing this value, suitable
8648 -- for use as an entry in the Static_Discrte_Predicate list. This node
8649 -- is typed with the base type.
8650
8651 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
8652 -- Return an analyzed N_Range node referencing this range, suitable for
8653 -- use as an entry in the Static_Discrete_Predicate list. This node is
8654 -- typed with the base type.
8655
8656 function Get_RList (Exp : Node_Id) return RList;
8657 -- This is a recursive routine that converts the given expression into a
8658 -- list of ranges, suitable for use in building the static predicate.
8659
8660 function Is_False (R : RList) return Boolean;
8661 pragma Inline (Is_False);
8662 -- Returns True if the given range list is empty, and thus represents a
8663 -- False list of ranges that can never be satisfied.
8664
8665 function Is_True (R : RList) return Boolean;
8666 -- Returns True if R trivially represents the True predicate by having a
8667 -- single range from BLo to BHi.
8668
8669 function Is_Type_Ref (N : Node_Id) return Boolean;
8670 pragma Inline (Is_Type_Ref);
8671 -- Returns if True if N is a reference to the type for the predicate in
8672 -- the expression (i.e. if it is an identifier whose Chars field matches
8673 -- the Nam given in the call). N must not be parenthesized, if the type
8674 -- name appears in parens, this routine will return False.
8675
8676 function Lo_Val (N : Node_Id) return Uint;
8677 -- Given an entry from a Static_Discrete_Predicate list that is either
8678 -- a static expression or static range, gets either the expression value
8679 -- or the low bound of the range.
8680
8681 function Hi_Val (N : Node_Id) return Uint;
8682 -- Given an entry from a Static_Discrete_Predicate list that is either
8683 -- a static expression or static range, gets either the expression value
8684 -- or the high bound of the range.
8685
8686 function Membership_Entry (N : Node_Id) return RList;
8687 -- Given a single membership entry (range, value, or subtype), returns
8688 -- the corresponding range list. Raises Static_Error if not static.
8689
8690 function Membership_Entries (N : Node_Id) return RList;
8691 -- Given an element on an alternatives list of a membership operation,
8692 -- returns the range list corresponding to this entry and all following
8693 -- entries (i.e. returns the "or" of this list of values).
8694
8695 function Stat_Pred (Typ : Entity_Id) return RList;
8696 -- Given a type, if it has a static predicate, then return the predicate
8697 -- as a range list, otherwise raise Non_Static.
8698
8699 -----------
8700 -- "and" --
8701 -----------
8702
8703 function "and" (Left : RList; Right : RList) return RList is
8704 FEnt : REnt;
8705 -- First range of result
8706
8707 SLeft : Nat := Left'First;
8708 -- Start of rest of left entries
8709
8710 SRight : Nat := Right'First;
8711 -- Start of rest of right entries
8712
8713 begin
8714 -- If either range is True, return the other
8715
8716 if Is_True (Left) then
8717 return Right;
8718 elsif Is_True (Right) then
8719 return Left;
8720 end if;
8721
8722 -- If either range is False, return False
8723
8724 if Is_False (Left) or else Is_False (Right) then
8725 return False_Range;
8726 end if;
8727
8728 -- Loop to remove entries at start that are disjoint, and thus just
8729 -- get discarded from the result entirely.
8730
8731 loop
8732 -- If no operands left in either operand, result is false
8733
8734 if SLeft > Left'Last or else SRight > Right'Last then
8735 return False_Range;
8736
8737 -- Discard first left operand entry if disjoint with right
8738
8739 elsif Left (SLeft).Hi < Right (SRight).Lo then
8740 SLeft := SLeft + 1;
8741
8742 -- Discard first right operand entry if disjoint with left
8743
8744 elsif Right (SRight).Hi < Left (SLeft).Lo then
8745 SRight := SRight + 1;
8746
8747 -- Otherwise we have an overlapping entry
8748
8749 else
8750 exit;
8751 end if;
8752 end loop;
8753
8754 -- Now we have two non-null operands, and first entries overlap. The
8755 -- first entry in the result will be the overlapping part of these
8756 -- two entries.
8757
8758 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
8759 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
8760
8761 -- Now we can remove the entry that ended at a lower value, since its
8762 -- contribution is entirely contained in Fent.
8763
8764 if Left (SLeft).Hi <= Right (SRight).Hi then
8765 SLeft := SLeft + 1;
8766 else
8767 SRight := SRight + 1;
8768 end if;
8769
8770 -- Compute result by concatenating this first entry with the "and" of
8771 -- the remaining parts of the left and right operands. Note that if
8772 -- either of these is empty, "and" will yield empty, so that we will
8773 -- end up with just Fent, which is what we want in that case.
8774
8775 return
8776 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
8777 end "and";
8778
8779 -----------
8780 -- "not" --
8781 -----------
8782
8783 function "not" (Right : RList) return RList is
8784 begin
8785 -- Return True if False range
8786
8787 if Is_False (Right) then
8788 return True_Range;
8789 end if;
8790
8791 -- Return False if True range
8792
8793 if Is_True (Right) then
8794 return False_Range;
8795 end if;
8796
8797 -- Here if not trivial case
8798
8799 declare
8800 Result : RList (1 .. Right'Length + 1);
8801 -- May need one more entry for gap at beginning and end
8802
8803 Count : Nat := 0;
8804 -- Number of entries stored in Result
8805
8806 begin
8807 -- Gap at start
8808
8809 if Right (Right'First).Lo > TLo then
8810 Count := Count + 1;
8811 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
8812 end if;
8813
8814 -- Gaps between ranges
8815
8816 for J in Right'First .. Right'Last - 1 loop
8817 Count := Count + 1;
8818 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
8819 end loop;
8820
8821 -- Gap at end
8822
8823 if Right (Right'Last).Hi < THi then
8824 Count := Count + 1;
8825 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
8826 end if;
8827
8828 return Result (1 .. Count);
8829 end;
8830 end "not";
8831
8832 ----------
8833 -- "or" --
8834 ----------
8835
8836 function "or" (Left : RList; Right : RList) return RList is
8837 FEnt : REnt;
8838 -- First range of result
8839
8840 SLeft : Nat := Left'First;
8841 -- Start of rest of left entries
8842
8843 SRight : Nat := Right'First;
8844 -- Start of rest of right entries
8845
8846 begin
8847 -- If either range is True, return True
8848
8849 if Is_True (Left) or else Is_True (Right) then
8850 return True_Range;
8851 end if;
8852
8853 -- If either range is False (empty), return the other
8854
8855 if Is_False (Left) then
8856 return Right;
8857 elsif Is_False (Right) then
8858 return Left;
8859 end if;
8860
8861 -- Initialize result first entry from left or right operand depending
8862 -- on which starts with the lower range.
8863
8864 if Left (SLeft).Lo < Right (SRight).Lo then
8865 FEnt := Left (SLeft);
8866 SLeft := SLeft + 1;
8867 else
8868 FEnt := Right (SRight);
8869 SRight := SRight + 1;
8870 end if;
8871
8872 -- This loop eats ranges from left and right operands that are
8873 -- contiguous with the first range we are gathering.
8874
8875 loop
8876 -- Eat first entry in left operand if contiguous or overlapped by
8877 -- gathered first operand of result.
8878
8879 if SLeft <= Left'Last
8880 and then Left (SLeft).Lo <= FEnt.Hi + 1
8881 then
8882 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
8883 SLeft := SLeft + 1;
8884
8885 -- Eat first entry in right operand if contiguous or overlapped by
8886 -- gathered right operand of result.
8887
8888 elsif SRight <= Right'Last
8889 and then Right (SRight).Lo <= FEnt.Hi + 1
8890 then
8891 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
8892 SRight := SRight + 1;
8893
8894 -- All done if no more entries to eat
8895
8896 else
8897 exit;
8898 end if;
8899 end loop;
8900
8901 -- Obtain result as the first entry we just computed, concatenated
8902 -- to the "or" of the remaining results (if one operand is empty,
8903 -- this will just concatenate with the other
8904
8905 return
8906 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
8907 end "or";
8908
8909 -----------------
8910 -- Build_Range --
8911 -----------------
8912
8913 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
8914 Result : Node_Id;
8915 begin
8916 Result :=
8917 Make_Range (Loc,
8918 Low_Bound => Build_Val (Lo),
8919 High_Bound => Build_Val (Hi));
8920 Set_Etype (Result, Btyp);
8921 Set_Analyzed (Result);
8922 return Result;
8923 end Build_Range;
8924
8925 ---------------
8926 -- Build_Val --
8927 ---------------
8928
8929 function Build_Val (V : Uint) return Node_Id is
8930 Result : Node_Id;
8931
8932 begin
8933 if Is_Enumeration_Type (Typ) then
8934 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
8935 else
8936 Result := Make_Integer_Literal (Loc, V);
8937 end if;
8938
8939 Set_Etype (Result, Btyp);
8940 Set_Is_Static_Expression (Result);
8941 Set_Analyzed (Result);
8942 return Result;
8943 end Build_Val;
8944
8945 ---------------
8946 -- Get_RList --
8947 ---------------
8948
8949 function Get_RList (Exp : Node_Id) return RList is
8950 Op : Node_Kind;
8951 Val : Uint;
8952
8953 begin
8954 -- Static expression can only be true or false
8955
8956 if Is_OK_Static_Expression (Exp) then
8957 if Expr_Value (Exp) = 0 then
8958 return False_Range;
8959 else
8960 return True_Range;
8961 end if;
8962 end if;
8963
8964 -- Otherwise test node type
8965
8966 Op := Nkind (Exp);
8967
8968 case Op is
8969
8970 -- And
8971
8972 when N_And_Then
8973 | N_Op_And
8974 =>
8975 return Get_RList (Left_Opnd (Exp))
8976 and
8977 Get_RList (Right_Opnd (Exp));
8978
8979 -- Or
8980
8981 when N_Op_Or
8982 | N_Or_Else
8983 =>
8984 return Get_RList (Left_Opnd (Exp))
8985 or
8986 Get_RList (Right_Opnd (Exp));
8987
8988 -- Not
8989
8990 when N_Op_Not =>
8991 return not Get_RList (Right_Opnd (Exp));
8992
8993 -- Comparisons of type with static value
8994
8995 when N_Op_Compare =>
8996
8997 -- Type is left operand
8998
8999 if Is_Type_Ref (Left_Opnd (Exp))
9000 and then Is_OK_Static_Expression (Right_Opnd (Exp))
9001 then
9002 Val := Expr_Value (Right_Opnd (Exp));
9003
9004 -- Typ is right operand
9005
9006 elsif Is_Type_Ref (Right_Opnd (Exp))
9007 and then Is_OK_Static_Expression (Left_Opnd (Exp))
9008 then
9009 Val := Expr_Value (Left_Opnd (Exp));
9010
9011 -- Invert sense of comparison
9012
9013 case Op is
9014 when N_Op_Gt => Op := N_Op_Lt;
9015 when N_Op_Lt => Op := N_Op_Gt;
9016 when N_Op_Ge => Op := N_Op_Le;
9017 when N_Op_Le => Op := N_Op_Ge;
9018 when others => null;
9019 end case;
9020
9021 -- Other cases are non-static
9022
9023 else
9024 raise Non_Static;
9025 end if;
9026
9027 -- Construct range according to comparison operation
9028
9029 case Op is
9030 when N_Op_Eq =>
9031 return RList'(1 => REnt'(Val, Val));
9032
9033 when N_Op_Ge =>
9034 return RList'(1 => REnt'(Val, BHi));
9035
9036 when N_Op_Gt =>
9037 return RList'(1 => REnt'(Val + 1, BHi));
9038
9039 when N_Op_Le =>
9040 return RList'(1 => REnt'(BLo, Val));
9041
9042 when N_Op_Lt =>
9043 return RList'(1 => REnt'(BLo, Val - 1));
9044
9045 when N_Op_Ne =>
9046 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
9047
9048 when others =>
9049 raise Program_Error;
9050 end case;
9051
9052 -- Membership (IN)
9053
9054 when N_In =>
9055 if not Is_Type_Ref (Left_Opnd (Exp)) then
9056 raise Non_Static;
9057 end if;
9058
9059 if Present (Right_Opnd (Exp)) then
9060 return Membership_Entry (Right_Opnd (Exp));
9061 else
9062 return Membership_Entries (First (Alternatives (Exp)));
9063 end if;
9064
9065 -- Negative membership (NOT IN)
9066
9067 when N_Not_In =>
9068 if not Is_Type_Ref (Left_Opnd (Exp)) then
9069 raise Non_Static;
9070 end if;
9071
9072 if Present (Right_Opnd (Exp)) then
9073 return not Membership_Entry (Right_Opnd (Exp));
9074 else
9075 return not Membership_Entries (First (Alternatives (Exp)));
9076 end if;
9077
9078 -- Function call, may be call to static predicate
9079
9080 when N_Function_Call =>
9081 if Is_Entity_Name (Name (Exp)) then
9082 declare
9083 Ent : constant Entity_Id := Entity (Name (Exp));
9084 begin
9085 if Is_Predicate_Function (Ent)
9086 or else
9087 Is_Predicate_Function_M (Ent)
9088 then
9089 return Stat_Pred (Etype (First_Formal (Ent)));
9090 end if;
9091 end;
9092 end if;
9093
9094 -- Other function call cases are non-static
9095
9096 raise Non_Static;
9097
9098 -- Qualified expression, dig out the expression
9099
9100 when N_Qualified_Expression =>
9101 return Get_RList (Expression (Exp));
9102
9103 when N_Case_Expression =>
9104 declare
9105 Alt : Node_Id;
9106 Choices : List_Id;
9107 Dep : Node_Id;
9108
9109 begin
9110 if not Is_Entity_Name (Expression (Expr))
9111 or else Etype (Expression (Expr)) /= Typ
9112 then
9113 Error_Msg_N
9114 ("expression must denaote subtype", Expression (Expr));
9115 return False_Range;
9116 end if;
9117
9118 -- Collect discrete choices in all True alternatives
9119
9120 Choices := New_List;
9121 Alt := First (Alternatives (Exp));
9122 while Present (Alt) loop
9123 Dep := Expression (Alt);
9124
9125 if not Is_OK_Static_Expression (Dep) then
9126 raise Non_Static;
9127
9128 elsif Is_True (Expr_Value (Dep)) then
9129 Append_List_To (Choices,
9130 New_Copy_List (Discrete_Choices (Alt)));
9131 end if;
9132
9133 Next (Alt);
9134 end loop;
9135
9136 return Membership_Entries (First (Choices));
9137 end;
9138
9139 -- Expression with actions: if no actions, dig out expression
9140
9141 when N_Expression_With_Actions =>
9142 if Is_Empty_List (Actions (Exp)) then
9143 return Get_RList (Expression (Exp));
9144 else
9145 raise Non_Static;
9146 end if;
9147
9148 -- Xor operator
9149
9150 when N_Op_Xor =>
9151 return (Get_RList (Left_Opnd (Exp))
9152 and not Get_RList (Right_Opnd (Exp)))
9153 or (Get_RList (Right_Opnd (Exp))
9154 and not Get_RList (Left_Opnd (Exp)));
9155
9156 -- Any other node type is non-static
9157
9158 when others =>
9159 raise Non_Static;
9160 end case;
9161 end Get_RList;
9162
9163 ------------
9164 -- Hi_Val --
9165 ------------
9166
9167 function Hi_Val (N : Node_Id) return Uint is
9168 begin
9169 if Is_OK_Static_Expression (N) then
9170 return Expr_Value (N);
9171 else
9172 pragma Assert (Nkind (N) = N_Range);
9173 return Expr_Value (High_Bound (N));
9174 end if;
9175 end Hi_Val;
9176
9177 --------------
9178 -- Is_False --
9179 --------------
9180
9181 function Is_False (R : RList) return Boolean is
9182 begin
9183 return R'Length = 0;
9184 end Is_False;
9185
9186 -------------
9187 -- Is_True --
9188 -------------
9189
9190 function Is_True (R : RList) return Boolean is
9191 begin
9192 return R'Length = 1
9193 and then R (R'First).Lo = BLo
9194 and then R (R'First).Hi = BHi;
9195 end Is_True;
9196
9197 -----------------
9198 -- Is_Type_Ref --
9199 -----------------
9200
9201 function Is_Type_Ref (N : Node_Id) return Boolean is
9202 begin
9203 return Nkind (N) = N_Identifier
9204 and then Chars (N) = Nam
9205 and then Paren_Count (N) = 0;
9206 end Is_Type_Ref;
9207
9208 ------------
9209 -- Lo_Val --
9210 ------------
9211
9212 function Lo_Val (N : Node_Id) return Uint is
9213 begin
9214 if Is_OK_Static_Expression (N) then
9215 return Expr_Value (N);
9216 else
9217 pragma Assert (Nkind (N) = N_Range);
9218 return Expr_Value (Low_Bound (N));
9219 end if;
9220 end Lo_Val;
9221
9222 ------------------------
9223 -- Membership_Entries --
9224 ------------------------
9225
9226 function Membership_Entries (N : Node_Id) return RList is
9227 begin
9228 if No (Next (N)) then
9229 return Membership_Entry (N);
9230 else
9231 return Membership_Entry (N) or Membership_Entries (Next (N));
9232 end if;
9233 end Membership_Entries;
9234
9235 ----------------------
9236 -- Membership_Entry --
9237 ----------------------
9238
9239 function Membership_Entry (N : Node_Id) return RList is
9240 Val : Uint;
9241 SLo : Uint;
9242 SHi : Uint;
9243
9244 begin
9245 -- Range case
9246
9247 if Nkind (N) = N_Range then
9248 if not Is_OK_Static_Expression (Low_Bound (N))
9249 or else
9250 not Is_OK_Static_Expression (High_Bound (N))
9251 then
9252 raise Non_Static;
9253 else
9254 SLo := Expr_Value (Low_Bound (N));
9255 SHi := Expr_Value (High_Bound (N));
9256 return RList'(1 => REnt'(SLo, SHi));
9257 end if;
9258
9259 -- Others case
9260
9261 elsif Nkind (N) = N_Others_Choice then
9262 declare
9263 Choices : constant List_Id := Others_Discrete_Choices (N);
9264 Choice : Node_Id;
9265 Range_List : RList (1 .. List_Length (Choices));
9266
9267 begin
9268 Choice := First (Choices);
9269
9270 for J in Range_List'Range loop
9271 Range_List (J) := REnt'(Lo_Val (Choice), Hi_Val (Choice));
9272 Next (Choice);
9273 end loop;
9274
9275 return Range_List;
9276 end;
9277
9278 -- Static expression case
9279
9280 elsif Is_OK_Static_Expression (N) then
9281 Val := Expr_Value (N);
9282 return RList'(1 => REnt'(Val, Val));
9283
9284 -- Identifier (other than static expression) case
9285
9286 else pragma Assert (Nkind (N) in N_Expanded_Name | N_Identifier);
9287
9288 -- Type case
9289
9290 if Is_Type (Entity (N)) then
9291
9292 -- If type has predicates, process them
9293
9294 if Has_Predicates (Entity (N)) then
9295 return Stat_Pred (Entity (N));
9296
9297 -- For static subtype without predicates, get range
9298
9299 elsif Is_OK_Static_Subtype (Entity (N)) then
9300 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
9301 SHi := Expr_Value (Type_High_Bound (Entity (N)));
9302 return RList'(1 => REnt'(SLo, SHi));
9303
9304 -- Any other type makes us non-static
9305
9306 else
9307 raise Non_Static;
9308 end if;
9309
9310 -- Any other kind of identifier in predicate (e.g. a non-static
9311 -- expression value) means this is not a static predicate.
9312
9313 else
9314 raise Non_Static;
9315 end if;
9316 end if;
9317 end Membership_Entry;
9318
9319 ---------------
9320 -- Stat_Pred --
9321 ---------------
9322
9323 function Stat_Pred (Typ : Entity_Id) return RList is
9324 begin
9325 -- Not static if type does not have static predicates
9326
9327 if not Has_Static_Predicate (Typ) then
9328 raise Non_Static;
9329 end if;
9330
9331 -- Otherwise we convert the predicate list to a range list
9332
9333 declare
9334 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
9335 Result : RList (1 .. List_Length (Spred));
9336 P : Node_Id;
9337
9338 begin
9339 P := First (Static_Discrete_Predicate (Typ));
9340 for J in Result'Range loop
9341 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
9342 Next (P);
9343 end loop;
9344
9345 return Result;
9346 end;
9347 end Stat_Pred;
9348
9349 -- Start of processing for Build_Discrete_Static_Predicate
9350
9351 begin
9352 -- Establish bounds for the predicate
9353
9354 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9355 TLo := Expr_Value (Type_Low_Bound (Typ));
9356 else
9357 TLo := BLo;
9358 end if;
9359
9360 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9361 THi := Expr_Value (Type_High_Bound (Typ));
9362 else
9363 THi := BHi;
9364 end if;
9365
9366 -- Analyze the expression to see if it is a static predicate
9367
9368 declare
9369 Ranges : constant RList := Get_RList (Expr);
9370 -- Range list from expression if it is static
9371
9372 Plist : List_Id;
9373
9374 begin
9375 -- Convert range list into a form for the static predicate. In the
9376 -- Ranges array, we just have raw ranges, these must be converted
9377 -- to properly typed and analyzed static expressions or range nodes.
9378
9379 -- Note: here we limit ranges to the ranges of the subtype, so that
9380 -- a predicate is always false for values outside the subtype. That
9381 -- seems fine, such values are invalid anyway, and considering them
9382 -- to fail the predicate seems allowed and friendly, and furthermore
9383 -- simplifies processing for case statements and loops.
9384
9385 Plist := New_List;
9386
9387 for J in Ranges'Range loop
9388 declare
9389 Lo : Uint := Ranges (J).Lo;
9390 Hi : Uint := Ranges (J).Hi;
9391
9392 begin
9393 -- Ignore completely out of range entry
9394
9395 if Hi < TLo or else Lo > THi then
9396 null;
9397
9398 -- Otherwise process entry
9399
9400 else
9401 -- Adjust out of range value to subtype range
9402
9403 if Lo < TLo then
9404 Lo := TLo;
9405 end if;
9406
9407 if Hi > THi then
9408 Hi := THi;
9409 end if;
9410
9411 -- Convert range into required form
9412
9413 Append_To (Plist, Build_Range (Lo, Hi));
9414 end if;
9415 end;
9416 end loop;
9417
9418 -- Processing was successful and all entries were static, so now we
9419 -- can store the result as the predicate list.
9420
9421 Set_Static_Discrete_Predicate (Typ, Plist);
9422
9423 -- Within a generic the predicate functions themselves need not
9424 -- be constructed.
9425
9426 if Inside_A_Generic then
9427 return;
9428 end if;
9429
9430 -- The processing for static predicates put the expression into
9431 -- canonical form as a series of ranges. It also eliminated
9432 -- duplicates and collapsed and combined ranges. We might as well
9433 -- replace the alternatives list of the right operand of the
9434 -- membership test with the static predicate list, which will
9435 -- usually be more efficient.
9436
9437 declare
9438 New_Alts : constant List_Id := New_List;
9439 Old_Node : Node_Id;
9440 New_Node : Node_Id;
9441
9442 begin
9443 Old_Node := First (Plist);
9444 while Present (Old_Node) loop
9445 New_Node := New_Copy (Old_Node);
9446
9447 if Nkind (New_Node) = N_Range then
9448 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
9449 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
9450 end if;
9451
9452 Append_To (New_Alts, New_Node);
9453 Next (Old_Node);
9454 end loop;
9455
9456 -- If empty list, replace by False
9457
9458 if Is_Empty_List (New_Alts) then
9459 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
9460
9461 -- Else replace by set membership test
9462
9463 else
9464 Rewrite (Expr,
9465 Make_In (Loc,
9466 Left_Opnd => Make_Identifier (Loc, Nam),
9467 Right_Opnd => Empty,
9468 Alternatives => New_Alts));
9469
9470 -- Resolve new expression in function context
9471
9472 Install_Formals (Predicate_Function (Typ));
9473 Push_Scope (Predicate_Function (Typ));
9474 Analyze_And_Resolve (Expr, Standard_Boolean);
9475 Pop_Scope;
9476 end if;
9477 end;
9478 end;
9479
9480 -- If non-static, return doing nothing
9481
9482 exception
9483 when Non_Static =>
9484 return;
9485 end Build_Discrete_Static_Predicate;
9486
9487 --------------------------------
9488 -- Build_Export_Import_Pragma --
9489 --------------------------------
9490
9491 function Build_Export_Import_Pragma
9492 (Asp : Node_Id;
9493 Id : Entity_Id) return Node_Id
9494 is
9495 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
9496 Expr : constant Node_Id := Expression (Asp);
9497 Loc : constant Source_Ptr := Sloc (Asp);
9498
9499 Args : List_Id;
9500 Conv : Node_Id;
9501 Conv_Arg : Node_Id;
9502 Dummy_1 : Node_Id;
9503 Dummy_2 : Node_Id;
9504 EN : Node_Id;
9505 LN : Node_Id;
9506 Prag : Node_Id;
9507
9508 Create_Pragma : Boolean := False;
9509 -- This flag is set when the aspect form is such that it warrants the
9510 -- creation of a corresponding pragma.
9511
9512 begin
9513 if Present (Expr) then
9514 if Error_Posted (Expr) then
9515 null;
9516
9517 elsif Is_True (Expr_Value (Expr)) then
9518 Create_Pragma := True;
9519 end if;
9520
9521 -- Otherwise the aspect defaults to True
9522
9523 else
9524 Create_Pragma := True;
9525 end if;
9526
9527 -- Nothing to do when the expression is False or is erroneous
9528
9529 if not Create_Pragma then
9530 return Empty;
9531 end if;
9532
9533 -- Obtain all interfacing aspects that apply to the related entity
9534
9535 Get_Interfacing_Aspects
9536 (Iface_Asp => Asp,
9537 Conv_Asp => Conv,
9538 EN_Asp => EN,
9539 Expo_Asp => Dummy_1,
9540 Imp_Asp => Dummy_2,
9541 LN_Asp => LN);
9542
9543 Args := New_List;
9544
9545 -- Handle the convention argument
9546
9547 if Present (Conv) then
9548 Conv_Arg := New_Copy_Tree (Expression (Conv));
9549
9550 -- Assume convention "Ada' when aspect Convention is missing
9551
9552 else
9553 Conv_Arg := Make_Identifier (Loc, Name_Ada);
9554 end if;
9555
9556 Append_To (Args,
9557 Make_Pragma_Argument_Association (Loc,
9558 Chars => Name_Convention,
9559 Expression => Conv_Arg));
9560
9561 -- Handle the entity argument
9562
9563 Append_To (Args,
9564 Make_Pragma_Argument_Association (Loc,
9565 Chars => Name_Entity,
9566 Expression => New_Occurrence_Of (Id, Loc)));
9567
9568 -- Handle the External_Name argument
9569
9570 if Present (EN) then
9571 Append_To (Args,
9572 Make_Pragma_Argument_Association (Loc,
9573 Chars => Name_External_Name,
9574 Expression => New_Copy_Tree (Expression (EN))));
9575 end if;
9576
9577 -- Handle the Link_Name argument
9578
9579 if Present (LN) then
9580 Append_To (Args,
9581 Make_Pragma_Argument_Association (Loc,
9582 Chars => Name_Link_Name,
9583 Expression => New_Copy_Tree (Expression (LN))));
9584 end if;
9585
9586 -- Generate:
9587 -- pragma Export/Import
9588 -- (Convention => <Conv>/Ada,
9589 -- Entity => <Id>,
9590 -- [External_Name => <EN>,]
9591 -- [Link_Name => <LN>]);
9592
9593 Prag :=
9594 Make_Pragma (Loc,
9595 Pragma_Identifier =>
9596 Make_Identifier (Loc, Chars (Identifier (Asp))),
9597 Pragma_Argument_Associations => Args);
9598
9599 -- Decorate the relevant aspect and the pragma
9600
9601 Set_Aspect_Rep_Item (Asp, Prag);
9602
9603 Set_Corresponding_Aspect (Prag, Asp);
9604 Set_From_Aspect_Specification (Prag);
9605 Set_Parent (Prag, Asp);
9606
9607 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
9608 Set_Import_Pragma (Id, Prag);
9609 end if;
9610
9611 return Prag;
9612 end Build_Export_Import_Pragma;
9613
9614 -------------------------------
9615 -- Build_Predicate_Functions --
9616 -------------------------------
9617
9618 -- The functions that are constructed here have the form:
9619
9620 -- function typPredicate (Ixxx : typ) return Boolean is
9621 -- begin
9622 -- return
9623 -- typ1Predicate (typ1 (Ixxx))
9624 -- and then typ2Predicate (typ2 (Ixxx))
9625 -- and then ...
9626 -- and then exp1 and then exp2 and then ...;
9627 -- end typPredicate;
9628
9629 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
9630 -- this is the point at which these expressions get analyzed, providing the
9631 -- required delay, and typ1, typ2, are entities from which predicates are
9632 -- inherited. Note that we do NOT generate Check pragmas, that's because we
9633 -- use this function even if checks are off, e.g. for membership tests.
9634
9635 -- Note that the inherited predicates are evaluated first, as required by
9636 -- AI12-0071-1.
9637
9638 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
9639 -- the form of this return expression.
9640
9641 -- If the expression has at least one Raise_Expression, then we also build
9642 -- the typPredicateM version of the function, in which any occurrence of a
9643 -- Raise_Expression is converted to "return False".
9644
9645 -- WARNING: This routine manages Ghost regions. Return statements must be
9646 -- replaced by gotos which jump to the end of the routine and restore the
9647 -- Ghost mode.
9648
9649 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
9650 Loc : constant Source_Ptr := Sloc (Typ);
9651
9652 Expr : Node_Id;
9653 -- This is the expression for the result of the function. It is
9654 -- is build by connecting the component predicates with AND THEN.
9655
9656 Expr_M : Node_Id := Empty; -- init to avoid warning
9657 -- This is the corresponding return expression for the Predicate_M
9658 -- function. It differs in that raise expressions are marked for
9659 -- special expansion (see Process_REs).
9660
9661 Object_Name : Name_Id;
9662 -- Name for argument of Predicate procedure. Note that we use the same
9663 -- name for both predicate functions. That way the reference within the
9664 -- predicate expression is the same in both functions.
9665
9666 Object_Entity : Entity_Id;
9667 -- Entity for argument of Predicate procedure
9668
9669 Object_Entity_M : Entity_Id;
9670 -- Entity for argument of separate Predicate procedure when exceptions
9671 -- are present in expression.
9672
9673 FDecl : Node_Id;
9674 -- The function declaration
9675
9676 SId : Entity_Id;
9677 -- Its entity
9678
9679 Raise_Expression_Present : Boolean := False;
9680 -- Set True if Expr has at least one Raise_Expression
9681
9682 procedure Add_Condition (Cond : Node_Id);
9683 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
9684 -- Expr is empty).
9685
9686 procedure Add_Predicates;
9687 -- Appends expressions for any Predicate pragmas in the rep item chain
9688 -- Typ to Expr. Note that we look only at items for this exact entity.
9689 -- Inheritance of predicates for the parent type is done by calling the
9690 -- Predicate_Function of the parent type, using Add_Call above.
9691
9692 procedure Add_Call (T : Entity_Id);
9693 -- Includes a call to the predicate function for type T in Expr if
9694 -- Predicate_Function (T) is non-empty.
9695
9696 function Process_RE (N : Node_Id) return Traverse_Result;
9697 -- Used in Process REs, tests if node N is a raise expression, and if
9698 -- so, marks it to be converted to return False.
9699
9700 procedure Process_REs is new Traverse_Proc (Process_RE);
9701 -- Marks any raise expressions in Expr_M to return False
9702
9703 function Test_RE (N : Node_Id) return Traverse_Result;
9704 -- Used in Test_REs, tests one node for being a raise expression, and if
9705 -- so sets Raise_Expression_Present True.
9706
9707 procedure Test_REs is new Traverse_Proc (Test_RE);
9708 -- Tests to see if Expr contains any raise expressions
9709
9710 --------------
9711 -- Add_Call --
9712 --------------
9713
9714 procedure Add_Call (T : Entity_Id) is
9715 Exp : Node_Id;
9716
9717 begin
9718 if Present (Predicate_Function (T)) then
9719 pragma Assert (Has_Predicates (Typ));
9720
9721 -- Build the call to the predicate function of T. The type may be
9722 -- derived, so use an unchecked conversion for the actual.
9723
9724 Exp :=
9725 Make_Predicate_Call
9726 (Typ => T,
9727 Expr =>
9728 Unchecked_Convert_To (T,
9729 Make_Identifier (Loc, Object_Name)));
9730
9731 -- "and"-in the call to evolving expression
9732
9733 Add_Condition (Exp);
9734
9735 -- Output info message on inheritance if required. Note we do not
9736 -- give this information for generic actual types, since it is
9737 -- unwelcome noise in that case in instantiations. We also
9738 -- generally suppress the message in instantiations, and also
9739 -- if it involves internal names.
9740
9741 if Opt.List_Inherited_Aspects
9742 and then not Is_Generic_Actual_Type (Typ)
9743 and then Instantiation_Depth (Sloc (Typ)) = 0
9744 and then not Is_Internal_Name (Chars (T))
9745 and then not Is_Internal_Name (Chars (Typ))
9746 then
9747 Error_Msg_Sloc := Sloc (Predicate_Function (T));
9748 Error_Msg_Node_2 := T;
9749 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
9750 end if;
9751 end if;
9752 end Add_Call;
9753
9754 -------------------
9755 -- Add_Condition --
9756 -------------------
9757
9758 procedure Add_Condition (Cond : Node_Id) is
9759 begin
9760 -- This is the first predicate expression
9761
9762 if No (Expr) then
9763 Expr := Cond;
9764
9765 -- Otherwise concatenate to the existing predicate expressions by
9766 -- using "and then".
9767
9768 else
9769 Expr :=
9770 Make_And_Then (Loc,
9771 Left_Opnd => Relocate_Node (Expr),
9772 Right_Opnd => Cond);
9773 end if;
9774 end Add_Condition;
9775
9776 --------------------
9777 -- Add_Predicates --
9778 --------------------
9779
9780 procedure Add_Predicates is
9781 procedure Add_Predicate (Prag : Node_Id);
9782 -- Concatenate the expression of predicate pragma Prag to Expr by
9783 -- using a short circuit "and then" operator.
9784
9785 -------------------
9786 -- Add_Predicate --
9787 -------------------
9788
9789 procedure Add_Predicate (Prag : Node_Id) is
9790 procedure Replace_Type_Reference (N : Node_Id);
9791 -- Replace a single occurrence N of the subtype name with a
9792 -- reference to the formal of the predicate function. N can be an
9793 -- identifier referencing the subtype, or a selected component,
9794 -- representing an appropriately qualified occurrence of the
9795 -- subtype name.
9796
9797 procedure Replace_Type_References is
9798 new Replace_Type_References_Generic (Replace_Type_Reference);
9799 -- Traverse an expression changing every occurrence of an
9800 -- identifier whose name matches the name of the subtype with a
9801 -- reference to the formal parameter of the predicate function.
9802
9803 ----------------------------
9804 -- Replace_Type_Reference --
9805 ----------------------------
9806
9807 procedure Replace_Type_Reference (N : Node_Id) is
9808 begin
9809 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
9810 -- Use the Sloc of the usage name, not the defining name
9811
9812 Set_Etype (N, Typ);
9813 Set_Entity (N, Object_Entity);
9814 end Replace_Type_Reference;
9815
9816 -- Local variables
9817
9818 Asp : constant Node_Id := Corresponding_Aspect (Prag);
9819 Arg1 : Node_Id;
9820 Arg2 : Node_Id;
9821
9822 -- Start of processing for Add_Predicate
9823
9824 begin
9825 -- Mark corresponding SCO as enabled
9826
9827 Set_SCO_Pragma_Enabled (Sloc (Prag));
9828
9829 -- Extract the arguments of the pragma. The expression itself
9830 -- is copied for use in the predicate function, to preserve the
9831 -- original version for ASIS use.
9832 -- Is this still needed???
9833
9834 Arg1 := First (Pragma_Argument_Associations (Prag));
9835 Arg2 := Next (Arg1);
9836
9837 Arg1 := Get_Pragma_Arg (Arg1);
9838 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
9839
9840 -- When the predicate pragma applies to the current type or its
9841 -- full view, replace all occurrences of the subtype name with
9842 -- references to the formal parameter of the predicate function.
9843
9844 if Entity (Arg1) = Typ
9845 or else Full_View (Entity (Arg1)) = Typ
9846 then
9847 Replace_Type_References (Arg2, Typ);
9848
9849 -- If the predicate pragma comes from an aspect, replace the
9850 -- saved expression because we need the subtype references
9851 -- replaced for the calls to Preanalyze_Spec_Expression in
9852 -- Check_Aspect_At_xxx routines.
9853
9854 if Present (Asp) then
9855 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
9856 end if;
9857
9858 -- "and"-in the Arg2 condition to evolving expression
9859
9860 Add_Condition (Relocate_Node (Arg2));
9861 end if;
9862 end Add_Predicate;
9863
9864 -- Local variables
9865
9866 Ritem : Node_Id;
9867
9868 -- Start of processing for Add_Predicates
9869
9870 begin
9871 Ritem := First_Rep_Item (Typ);
9872
9873 -- If the type is private, check whether full view has inherited
9874 -- predicates.
9875
9876 if Is_Private_Type (Typ) and then No (Ritem) then
9877 Ritem := First_Rep_Item (Full_View (Typ));
9878 end if;
9879
9880 while Present (Ritem) loop
9881 if Nkind (Ritem) = N_Pragma
9882 and then Pragma_Name (Ritem) = Name_Predicate
9883 then
9884 Add_Predicate (Ritem);
9885
9886 -- If the type is declared in an inner package it may be frozen
9887 -- outside of the package, and the generated pragma has not been
9888 -- analyzed yet, so capture the expression for the predicate
9889 -- function at this point.
9890
9891 elsif Nkind (Ritem) = N_Aspect_Specification
9892 and then Present (Aspect_Rep_Item (Ritem))
9893 and then Scope_Depth (Scope (Typ)) > Scope_Depth (Current_Scope)
9894 then
9895 declare
9896 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
9897
9898 begin
9899 if Nkind (Prag) = N_Pragma
9900 and then Pragma_Name (Prag) = Name_Predicate
9901 then
9902 Add_Predicate (Prag);
9903 end if;
9904 end;
9905 end if;
9906
9907 Next_Rep_Item (Ritem);
9908 end loop;
9909 end Add_Predicates;
9910
9911 ----------------
9912 -- Process_RE --
9913 ----------------
9914
9915 function Process_RE (N : Node_Id) return Traverse_Result is
9916 begin
9917 if Nkind (N) = N_Raise_Expression then
9918 Set_Convert_To_Return_False (N);
9919 return Skip;
9920 else
9921 return OK;
9922 end if;
9923 end Process_RE;
9924
9925 -------------
9926 -- Test_RE --
9927 -------------
9928
9929 function Test_RE (N : Node_Id) return Traverse_Result is
9930 begin
9931 if Nkind (N) = N_Raise_Expression then
9932 Raise_Expression_Present := True;
9933 return Abandon;
9934 else
9935 return OK;
9936 end if;
9937 end Test_RE;
9938
9939 -- Local variables
9940
9941 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9942 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9943 -- Save the Ghost-related attributes to restore on exit
9944
9945 -- Start of processing for Build_Predicate_Functions
9946
9947 begin
9948 -- Return if already built or if type does not have predicates
9949
9950 SId := Predicate_Function (Typ);
9951 if not Has_Predicates (Typ)
9952 or else (Present (SId) and then Has_Completion (SId))
9953 then
9954 return;
9955
9956 -- Do not generate predicate bodies within a generic unit. The
9957 -- expressions have been analyzed already, and the bodies play
9958 -- no role if not within an executable unit. However, if a statc
9959 -- predicate is present it must be processed for legality checks
9960 -- such as case coverage in an expression.
9961
9962 elsif Inside_A_Generic
9963 and then not Has_Static_Predicate_Aspect (Typ)
9964 then
9965 return;
9966 end if;
9967
9968 -- The related type may be subject to pragma Ghost. Set the mode now to
9969 -- ensure that the predicate functions are properly marked as Ghost.
9970
9971 Set_Ghost_Mode (Typ);
9972
9973 -- Prepare to construct predicate expression
9974
9975 Expr := Empty;
9976
9977 if Present (SId) then
9978 FDecl := Unit_Declaration_Node (SId);
9979
9980 else
9981 FDecl := Build_Predicate_Function_Declaration (Typ);
9982 SId := Defining_Entity (FDecl);
9983 end if;
9984
9985 -- Recover name of formal parameter of function that replaces references
9986 -- to the type in predicate expressions.
9987
9988 Object_Entity :=
9989 Defining_Identifier
9990 (First (Parameter_Specifications (Specification (FDecl))));
9991
9992 Object_Name := Chars (Object_Entity);
9993 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
9994
9995 -- Add predicates for ancestor if present. These must come before the
9996 -- ones for the current type, as required by AI12-0071-1.
9997
9998 -- Looks like predicates aren't added for case of inheriting from
9999 -- multiple progenitors???
10000
10001 declare
10002 Atyp : Entity_Id;
10003 begin
10004 Atyp := Nearest_Ancestor (Typ);
10005
10006 -- The type may be private but the full view may inherit predicates
10007
10008 if No (Atyp) and then Is_Private_Type (Typ) then
10009 Atyp := Nearest_Ancestor (Full_View (Typ));
10010 end if;
10011
10012 if Present (Atyp) then
10013 Add_Call (Atyp);
10014 end if;
10015 end;
10016
10017 -- Add Predicates for the current type
10018
10019 Add_Predicates;
10020
10021 -- Case where predicates are present
10022
10023 if Present (Expr) then
10024
10025 -- Test for raise expression present
10026
10027 Test_REs (Expr);
10028
10029 -- If raise expression is present, capture a copy of Expr for use
10030 -- in building the predicateM function version later on. For this
10031 -- copy we replace references to Object_Entity by Object_Entity_M.
10032
10033 if Raise_Expression_Present then
10034 declare
10035 function Reset_Loop_Variable
10036 (N : Node_Id) return Traverse_Result;
10037
10038 procedure Reset_Loop_Variables is
10039 new Traverse_Proc (Reset_Loop_Variable);
10040
10041 ------------------------
10042 -- Reset_Loop_Variable --
10043 ------------------------
10044
10045 function Reset_Loop_Variable
10046 (N : Node_Id) return Traverse_Result
10047 is
10048 begin
10049 if Nkind (N) = N_Iterator_Specification then
10050 Set_Defining_Identifier (N,
10051 Make_Defining_Identifier
10052 (Sloc (N), Chars (Defining_Identifier (N))));
10053 end if;
10054
10055 return OK;
10056 end Reset_Loop_Variable;
10057
10058 -- Local variables
10059
10060 Map : constant Elist_Id := New_Elmt_List;
10061
10062 begin
10063 Append_Elmt (Object_Entity, Map);
10064 Append_Elmt (Object_Entity_M, Map);
10065 Expr_M := New_Copy_Tree (Expr, Map => Map);
10066
10067 -- The unanalyzed expression will be copied and appear in
10068 -- both functions. Normally expressions do not declare new
10069 -- entities, but quantified expressions do, so we need to
10070 -- create new entities for their bound variables, to prevent
10071 -- multiple definitions in gigi.
10072
10073 Reset_Loop_Variables (Expr_M);
10074 end;
10075 end if;
10076
10077 -- Build the main predicate function
10078
10079 declare
10080 SIdB : constant Entity_Id :=
10081 Make_Defining_Identifier (Loc,
10082 Chars => New_External_Name (Chars (Typ), "Predicate"));
10083 -- The entity for the function body
10084
10085 Spec : Node_Id;
10086 FBody : Node_Id;
10087
10088 begin
10089 Set_Ekind (SIdB, E_Function);
10090 Set_Is_Predicate_Function (SIdB);
10091
10092 -- Build function body
10093
10094 Spec :=
10095 Make_Function_Specification (Loc,
10096 Defining_Unit_Name => SIdB,
10097 Parameter_Specifications => New_List (
10098 Make_Parameter_Specification (Loc,
10099 Defining_Identifier =>
10100 Make_Defining_Identifier (Loc, Object_Name),
10101 Parameter_Type =>
10102 New_Occurrence_Of (Typ, Loc))),
10103 Result_Definition =>
10104 New_Occurrence_Of (Standard_Boolean, Loc));
10105
10106 FBody :=
10107 Make_Subprogram_Body (Loc,
10108 Specification => Spec,
10109 Declarations => Empty_List,
10110 Handled_Statement_Sequence =>
10111 Make_Handled_Sequence_Of_Statements (Loc,
10112 Statements => New_List (
10113 Make_Simple_Return_Statement (Loc,
10114 Expression => Expr))));
10115
10116 -- The declaration has been analyzed when created, and placed
10117 -- after type declaration. Insert body itself after freeze node,
10118 -- unless subprogram declaration is already there, in which case
10119 -- body better be placed afterwards.
10120
10121 if FDecl = Next (N) then
10122 Insert_After_And_Analyze (FDecl, FBody);
10123 else
10124 Insert_After_And_Analyze (N, FBody);
10125 end if;
10126
10127 -- The defining identifier of a quantified expression carries the
10128 -- scope in which the type appears, but when unnesting we need
10129 -- to indicate that its proper scope is the constructed predicate
10130 -- function. The quantified expressions have been converted into
10131 -- loops during analysis and expansion.
10132
10133 declare
10134 function Reset_Quantified_Variable_Scope
10135 (N : Node_Id) return Traverse_Result;
10136
10137 procedure Reset_Quantified_Variables_Scope is
10138 new Traverse_Proc (Reset_Quantified_Variable_Scope);
10139
10140 -------------------------------------
10141 -- Reset_Quantified_Variable_Scope --
10142 -------------------------------------
10143
10144 function Reset_Quantified_Variable_Scope
10145 (N : Node_Id) return Traverse_Result is
10146 begin
10147 if Nkind (N) in N_Iterator_Specification
10148 | N_Loop_Parameter_Specification
10149 then
10150 Set_Scope (Defining_Identifier (N),
10151 Predicate_Function (Typ));
10152 end if;
10153
10154 return OK;
10155 end Reset_Quantified_Variable_Scope;
10156
10157 begin
10158 if Unnest_Subprogram_Mode then
10159 Reset_Quantified_Variables_Scope (Expr);
10160 end if;
10161 end;
10162
10163 -- Within a generic unit, prevent a double analysis of the body
10164 -- which will not be marked analyzed yet. This will happen when
10165 -- the freeze node is created during the preanalysis of an
10166 -- expression function.
10167
10168 if Inside_A_Generic then
10169 Set_Analyzed (FBody);
10170 end if;
10171
10172 -- Static predicate functions are always side-effect free, and
10173 -- in most cases dynamic predicate functions are as well. Mark
10174 -- them as such whenever possible, so redundant predicate checks
10175 -- can be optimized. If there is a variable reference within the
10176 -- expression, the function is not pure.
10177
10178 if Expander_Active then
10179 Set_Is_Pure (SId,
10180 Side_Effect_Free (Expr, Variable_Ref => True));
10181 Set_Is_Inlined (SId);
10182 end if;
10183 end;
10184
10185 -- Test for raise expressions present and if so build M version
10186
10187 if Raise_Expression_Present then
10188 declare
10189 SId : constant Entity_Id :=
10190 Make_Defining_Identifier (Loc,
10191 Chars => New_External_Name (Chars (Typ), "PredicateM"));
10192 -- The entity for the function spec
10193
10194 SIdB : constant Entity_Id :=
10195 Make_Defining_Identifier (Loc,
10196 Chars => New_External_Name (Chars (Typ), "PredicateM"));
10197 -- The entity for the function body
10198
10199 Spec : Node_Id;
10200 FBody : Node_Id;
10201 FDecl : Node_Id;
10202 BTemp : Entity_Id;
10203
10204 CRec_Typ : Entity_Id;
10205 -- The corresponding record type of Full_Typ
10206
10207 Full_Typ : Entity_Id;
10208 -- The full view of Typ
10209
10210 Priv_Typ : Entity_Id;
10211 -- The partial view of Typ
10212
10213 UFull_Typ : Entity_Id;
10214 -- The underlying full view of Full_Typ
10215
10216 begin
10217 -- Mark any raise expressions for special expansion
10218
10219 Process_REs (Expr_M);
10220
10221 -- Build function declaration
10222
10223 Set_Ekind (SId, E_Function);
10224 Set_Is_Predicate_Function_M (SId);
10225 Set_Predicate_Function_M (Typ, SId);
10226
10227 -- Obtain all views of the input type
10228
10229 Get_Views (Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
10230
10231 -- Associate the predicate function with all views
10232
10233 Propagate_Predicate_Attributes (Priv_Typ, From_Typ => Typ);
10234 Propagate_Predicate_Attributes (Full_Typ, From_Typ => Typ);
10235 Propagate_Predicate_Attributes (UFull_Typ, From_Typ => Typ);
10236 Propagate_Predicate_Attributes (CRec_Typ, From_Typ => Typ);
10237
10238 Spec :=
10239 Make_Function_Specification (Loc,
10240 Defining_Unit_Name => SId,
10241 Parameter_Specifications => New_List (
10242 Make_Parameter_Specification (Loc,
10243 Defining_Identifier => Object_Entity_M,
10244 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
10245 Result_Definition =>
10246 New_Occurrence_Of (Standard_Boolean, Loc));
10247
10248 FDecl :=
10249 Make_Subprogram_Declaration (Loc,
10250 Specification => Spec);
10251
10252 -- Build function body
10253
10254 Spec :=
10255 Make_Function_Specification (Loc,
10256 Defining_Unit_Name => SIdB,
10257 Parameter_Specifications => New_List (
10258 Make_Parameter_Specification (Loc,
10259 Defining_Identifier =>
10260 Make_Defining_Identifier (Loc, Object_Name),
10261 Parameter_Type =>
10262 New_Occurrence_Of (Typ, Loc))),
10263 Result_Definition =>
10264 New_Occurrence_Of (Standard_Boolean, Loc));
10265
10266 -- Build the body, we declare the boolean expression before
10267 -- doing the return, because we are not really confident of
10268 -- what happens if a return appears within a return.
10269
10270 BTemp :=
10271 Make_Defining_Identifier (Loc,
10272 Chars => New_Internal_Name ('B'));
10273
10274 FBody :=
10275 Make_Subprogram_Body (Loc,
10276 Specification => Spec,
10277
10278 Declarations => New_List (
10279 Make_Object_Declaration (Loc,
10280 Defining_Identifier => BTemp,
10281 Constant_Present => True,
10282 Object_Definition =>
10283 New_Occurrence_Of (Standard_Boolean, Loc),
10284 Expression => Expr_M)),
10285
10286 Handled_Statement_Sequence =>
10287 Make_Handled_Sequence_Of_Statements (Loc,
10288 Statements => New_List (
10289 Make_Simple_Return_Statement (Loc,
10290 Expression => New_Occurrence_Of (BTemp, Loc)))));
10291
10292 -- Insert declaration before freeze node and body after
10293
10294 Insert_Before_And_Analyze (N, FDecl);
10295 Insert_After_And_Analyze (N, FBody);
10296
10297 -- Should quantified expressions be handled here as well ???
10298 end;
10299 end if;
10300
10301 -- See if we have a static predicate. Note that the answer may be
10302 -- yes even if we have an explicit Dynamic_Predicate present.
10303
10304 declare
10305 PS : Boolean;
10306 EN : Node_Id;
10307
10308 begin
10309 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
10310 PS := False;
10311 else
10312 PS := Is_Predicate_Static (Expr, Object_Name);
10313 end if;
10314
10315 -- Case where we have a predicate-static aspect
10316
10317 if PS then
10318
10319 -- We don't set Has_Static_Predicate_Aspect, since we can have
10320 -- any of the three cases (Predicate, Dynamic_Predicate, or
10321 -- Static_Predicate) generating a predicate with an expression
10322 -- that is predicate-static. We just indicate that we have a
10323 -- predicate that can be treated as static.
10324
10325 Set_Has_Static_Predicate (Typ);
10326
10327 -- For discrete subtype, build the static predicate list
10328
10329 if Is_Discrete_Type (Typ) then
10330 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
10331
10332 -- If we don't get a static predicate list, it means that we
10333 -- have a case where this is not possible, most typically in
10334 -- the case where we inherit a dynamic predicate. We do not
10335 -- consider this an error, we just leave the predicate as
10336 -- dynamic. But if we do succeed in building the list, then
10337 -- we mark the predicate as static.
10338
10339 if No (Static_Discrete_Predicate (Typ)) then
10340 Set_Has_Static_Predicate (Typ, False);
10341 end if;
10342
10343 -- For real or string subtype, save predicate expression
10344
10345 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
10346 Set_Static_Real_Or_String_Predicate (Typ, Expr);
10347 end if;
10348
10349 -- Case of dynamic predicate (expression is not predicate-static)
10350
10351 else
10352 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
10353 -- is only set if we have an explicit Dynamic_Predicate aspect
10354 -- given. Here we may simply have a Predicate aspect where the
10355 -- expression happens not to be predicate-static.
10356
10357 -- Emit an error when the predicate is categorized as static
10358 -- but its expression is not predicate-static.
10359
10360 -- First a little fiddling to get a nice location for the
10361 -- message. If the expression is of the form (A and then B),
10362 -- where A is an inherited predicate, then use the right
10363 -- operand for the Sloc. This avoids getting confused by a call
10364 -- to an inherited predicate with a less convenient source
10365 -- location.
10366
10367 EN := Expr;
10368 while Nkind (EN) = N_And_Then
10369 and then Nkind (Left_Opnd (EN)) = N_Function_Call
10370 and then Is_Predicate_Function
10371 (Entity (Name (Left_Opnd (EN))))
10372 loop
10373 EN := Right_Opnd (EN);
10374 end loop;
10375
10376 -- Now post appropriate message
10377
10378 if Has_Static_Predicate_Aspect (Typ) then
10379 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
10380 Error_Msg_F
10381 ("expression is not predicate-static (RM 3.2.4(16-22))",
10382 EN);
10383 else
10384 Error_Msg_F
10385 ("static predicate requires scalar or string type", EN);
10386 end if;
10387 end if;
10388 end if;
10389 end;
10390 end if;
10391
10392 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10393 end Build_Predicate_Functions;
10394
10395 ------------------------------------------
10396 -- Build_Predicate_Function_Declaration --
10397 ------------------------------------------
10398
10399 -- WARNING: This routine manages Ghost regions. Return statements must be
10400 -- replaced by gotos which jump to the end of the routine and restore the
10401 -- Ghost mode.
10402
10403 function Build_Predicate_Function_Declaration
10404 (Typ : Entity_Id) return Node_Id
10405 is
10406 Loc : constant Source_Ptr := Sloc (Typ);
10407
10408 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10409 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10410 -- Save the Ghost-related attributes to restore on exit
10411
10412 Func_Decl : Node_Id;
10413 Func_Id : Entity_Id;
10414 Spec : Node_Id;
10415
10416 CRec_Typ : Entity_Id;
10417 -- The corresponding record type of Full_Typ
10418
10419 Full_Typ : Entity_Id;
10420 -- The full view of Typ
10421
10422 Priv_Typ : Entity_Id;
10423 -- The partial view of Typ
10424
10425 UFull_Typ : Entity_Id;
10426 -- The underlying full view of Full_Typ
10427
10428 begin
10429 -- The related type may be subject to pragma Ghost. Set the mode now to
10430 -- ensure that the predicate functions are properly marked as Ghost.
10431
10432 Set_Ghost_Mode (Typ);
10433
10434 Func_Id :=
10435 Make_Defining_Identifier (Loc,
10436 Chars => New_External_Name (Chars (Typ), "Predicate"));
10437
10438 Set_Ekind (Func_Id, E_Function);
10439 Set_Etype (Func_Id, Standard_Boolean);
10440 Set_Is_Internal (Func_Id);
10441 Set_Is_Predicate_Function (Func_Id);
10442 Set_Predicate_Function (Typ, Func_Id);
10443
10444 -- The predicate function requires debug info when the predicates are
10445 -- subject to Source Coverage Obligations.
10446
10447 if Opt.Generate_SCO then
10448 Set_Debug_Info_Needed (Func_Id);
10449 end if;
10450
10451 -- Obtain all views of the input type
10452
10453 Get_Views (Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
10454
10455 -- Associate the predicate function and various flags with all views
10456
10457 Propagate_Predicate_Attributes (Priv_Typ, From_Typ => Typ);
10458 Propagate_Predicate_Attributes (Full_Typ, From_Typ => Typ);
10459 Propagate_Predicate_Attributes (UFull_Typ, From_Typ => Typ);
10460 Propagate_Predicate_Attributes (CRec_Typ, From_Typ => Typ);
10461
10462 Spec :=
10463 Make_Function_Specification (Loc,
10464 Defining_Unit_Name => Func_Id,
10465 Parameter_Specifications => New_List (
10466 Make_Parameter_Specification (Loc,
10467 Defining_Identifier => Make_Temporary (Loc, 'I'),
10468 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
10469 Result_Definition =>
10470 New_Occurrence_Of (Standard_Boolean, Loc));
10471
10472 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
10473
10474 Insert_After (Parent (Typ), Func_Decl);
10475 Analyze (Func_Decl);
10476
10477 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10478
10479 return Func_Decl;
10480 end Build_Predicate_Function_Declaration;
10481
10482 -----------------------------------------
10483 -- Check_Aspect_At_End_Of_Declarations --
10484 -----------------------------------------
10485
10486 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
10487 Ent : constant Entity_Id := Entity (ASN);
10488 Ident : constant Node_Id := Identifier (ASN);
10489 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10490
10491 End_Decl_Expr : constant Node_Id := Entity (Ident);
10492 -- Expression to be analyzed at end of declarations
10493
10494 Freeze_Expr : constant Node_Id := Expression (ASN);
10495 -- Expression from call to Check_Aspect_At_Freeze_Point.
10496
10497 T : constant Entity_Id :=
10498 (if Present (Freeze_Expr)
10499 then Etype (Original_Node (Freeze_Expr))
10500 else Empty);
10501 -- Type required for preanalyze call. We use the original expression to
10502 -- get the proper type, to prevent cascaded errors when the expression
10503 -- is constant-folded.
10504
10505 Err : Boolean;
10506 -- Set False if error
10507
10508 -- On entry to this procedure, Entity (Ident) contains a copy of the
10509 -- original expression from the aspect, saved for this purpose, and
10510 -- but Expression (Ident) is a preanalyzed copy of the expression,
10511 -- preanalyzed just after the freeze point.
10512
10513 procedure Check_Overloaded_Name;
10514 -- For aspects whose expression is simply a name, this routine checks if
10515 -- the name is overloaded or not. If so, it verifies there is an
10516 -- interpretation that matches the entity obtained at the freeze point,
10517 -- otherwise the compiler complains.
10518
10519 ---------------------------
10520 -- Check_Overloaded_Name --
10521 ---------------------------
10522
10523 procedure Check_Overloaded_Name is
10524 begin
10525 if not Is_Overloaded (End_Decl_Expr) then
10526 Err := not Is_Entity_Name (End_Decl_Expr)
10527 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
10528
10529 else
10530 Err := True;
10531
10532 declare
10533 Index : Interp_Index;
10534 It : Interp;
10535
10536 begin
10537 Get_First_Interp (End_Decl_Expr, Index, It);
10538 while Present (It.Typ) loop
10539 if It.Nam = Entity (Freeze_Expr) then
10540 Err := False;
10541 exit;
10542 end if;
10543
10544 Get_Next_Interp (Index, It);
10545 end loop;
10546 end;
10547 end if;
10548 end Check_Overloaded_Name;
10549
10550 -- Start of processing for Check_Aspect_At_End_Of_Declarations
10551
10552 begin
10553 -- In an instance we do not perform the consistency check between freeze
10554 -- point and end of declarations, because it was done already in the
10555 -- analysis of the generic. Furthermore, the delayed analysis of an
10556 -- aspect of the instance may produce spurious errors when the generic
10557 -- is a child unit that references entities in the parent (which might
10558 -- not be in scope at the freeze point of the instance).
10559
10560 if In_Instance then
10561 return;
10562
10563 -- The enclosing scope may have been rewritten during expansion (.e.g. a
10564 -- task body is rewritten as a procedure) after this conformance check
10565 -- has been performed, so do not perform it again (it may not easily be
10566 -- done if full visibility of local entities is not available).
10567
10568 elsif not Comes_From_Source (Current_Scope) then
10569 return;
10570
10571 -- Case of aspects Dimension, Dimension_System and Synchronization
10572
10573 elsif A_Id = Aspect_Synchronization then
10574 return;
10575
10576 -- Case of stream attributes and Put_Image, just have to compare
10577 -- entities. However, the expression is just a possibly-overloaded
10578 -- name, so we need to verify that one of these interpretations is
10579 -- the one available at at the freeze point.
10580
10581 elsif A_Id = Aspect_Input or else
10582 A_Id = Aspect_Output or else
10583 A_Id = Aspect_Read or else
10584 A_Id = Aspect_Write or else
10585 A_Id = Aspect_Put_Image
10586 then
10587 Analyze (End_Decl_Expr);
10588 Check_Overloaded_Name;
10589
10590 elsif A_Id = Aspect_Variable_Indexing or else
10591 A_Id = Aspect_Constant_Indexing or else
10592 A_Id = Aspect_Default_Iterator or else
10593 A_Id = Aspect_Iterator_Element or else
10594 A_Id = Aspect_Integer_Literal or else
10595 A_Id = Aspect_Real_Literal or else
10596 A_Id = Aspect_String_Literal
10597 then
10598 -- Make type unfrozen before analysis, to prevent spurious errors
10599 -- about late attributes.
10600
10601 Set_Is_Frozen (Ent, False);
10602 Analyze (End_Decl_Expr);
10603 Set_Is_Frozen (Ent, True);
10604
10605 -- If the end of declarations comes before any other freeze point,
10606 -- the Freeze_Expr is not analyzed: no check needed.
10607
10608 if Analyzed (Freeze_Expr) and then not In_Instance then
10609 Check_Overloaded_Name;
10610 else
10611 Err := False;
10612 end if;
10613
10614 -- All other cases
10615
10616 else
10617 -- In a generic context freeze nodes are not always generated, so
10618 -- analyze the expression now. If the aspect is for a type, we must
10619 -- also make its potential components accessible.
10620
10621 if not Analyzed (Freeze_Expr) and then Inside_A_Generic then
10622 if A_Id = Aspect_Dynamic_Predicate
10623 or else A_Id = Aspect_Predicate
10624 then
10625 Push_Type (Ent);
10626 Preanalyze_Spec_Expression (Freeze_Expr, Standard_Boolean);
10627 Pop_Type (Ent);
10628
10629 elsif A_Id = Aspect_Priority then
10630 Push_Type (Ent);
10631 Preanalyze_Spec_Expression (Freeze_Expr, Any_Integer);
10632 Pop_Type (Ent);
10633
10634 else
10635 Preanalyze (Freeze_Expr);
10636 end if;
10637 end if;
10638
10639 -- Indicate that the expression comes from an aspect specification,
10640 -- which is used in subsequent analysis even if expansion is off.
10641
10642 Set_Parent (End_Decl_Expr, ASN);
10643
10644 -- In a generic context the original aspect expressions have not
10645 -- been preanalyzed, so do it now. There are no conformance checks
10646 -- to perform in this case. As before, we have to make components
10647 -- visible for aspects that may reference them.
10648
10649 if Present (Freeze_Expr) and then No (T) then
10650 if A_Id = Aspect_Dynamic_Predicate
10651 or else A_Id = Aspect_Predicate
10652 or else A_Id = Aspect_Priority
10653 then
10654 Push_Type (Ent);
10655 Check_Aspect_At_Freeze_Point (ASN);
10656 Pop_Type (Ent);
10657
10658 else
10659 Check_Aspect_At_Freeze_Point (ASN);
10660 end if;
10661 return;
10662
10663 -- The default values attributes may be defined in the private part,
10664 -- and the analysis of the expression may take place when only the
10665 -- partial view is visible. The expression must be scalar, so use
10666 -- the full view to resolve.
10667
10668 elsif (A_Id = Aspect_Default_Value
10669 or else
10670 A_Id = Aspect_Default_Component_Value)
10671 and then Is_Private_Type (T)
10672 then
10673 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
10674
10675 -- The following aspect expressions may contain references to
10676 -- components and discriminants of the type.
10677
10678 elsif A_Id = Aspect_Dynamic_Predicate
10679 or else A_Id = Aspect_Predicate
10680 or else A_Id = Aspect_Priority
10681 or else A_Id = Aspect_CPU
10682 then
10683 Push_Type (Ent);
10684 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10685 Pop_Type (Ent);
10686
10687 elsif A_Id = Aspect_Predicate_Failure then
10688 Preanalyze_Spec_Expression (End_Decl_Expr, Standard_String);
10689 elsif Present (End_Decl_Expr) then
10690 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10691 end if;
10692
10693 Err :=
10694 not Fully_Conformant_Expressions
10695 (End_Decl_Expr, Freeze_Expr, Report => True);
10696 end if;
10697
10698 -- Output error message if error. Force error on aspect specification
10699 -- even if there is an error on the expression itself.
10700
10701 if Err then
10702 Error_Msg_NE
10703 ("!visibility of aspect for& changes after freeze point",
10704 ASN, Ent);
10705 Error_Msg_NE
10706 ("info: & is frozen here, (RM 13.1.1 (13/3))??",
10707 Freeze_Node (Ent), Ent);
10708 end if;
10709 end Check_Aspect_At_End_Of_Declarations;
10710
10711 ----------------------------------
10712 -- Check_Aspect_At_Freeze_Point --
10713 ----------------------------------
10714
10715 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
10716 Ident : constant Node_Id := Identifier (ASN);
10717 -- Identifier (use Entity field to save expression)
10718
10719 Expr : constant Node_Id := Expression (ASN);
10720 -- For cases where using Entity (Identifier) doesn't work
10721
10722 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10723
10724 T : Entity_Id := Empty;
10725 -- Type required for preanalyze call
10726
10727 begin
10728 -- On entry to this procedure, Entity (Ident) contains a copy of the
10729 -- original expression from the aspect, saved for this purpose.
10730
10731 -- On exit from this procedure Entity (Ident) is unchanged, still
10732 -- containing that copy, but Expression (Ident) is a preanalyzed copy
10733 -- of the expression, preanalyzed just after the freeze point.
10734
10735 -- Make a copy of the expression to be preanalyzed
10736
10737 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
10738
10739 -- Find type for preanalyze call
10740
10741 case A_Id is
10742
10743 -- No_Aspect should be impossible
10744
10745 when No_Aspect =>
10746 raise Program_Error;
10747
10748 -- Aspects taking an optional boolean argument
10749
10750 when Boolean_Aspects
10751 | Library_Unit_Aspects
10752 =>
10753 T := Standard_Boolean;
10754
10755 -- Aspects corresponding to attribute definition clauses
10756
10757 when Aspect_Address =>
10758 T := RTE (RE_Address);
10759
10760 when Aspect_Attach_Handler =>
10761 T := RTE (RE_Interrupt_ID);
10762
10763 when Aspect_Bit_Order
10764 | Aspect_Scalar_Storage_Order
10765 =>
10766 T := RTE (RE_Bit_Order);
10767
10768 when Aspect_Convention =>
10769 return;
10770
10771 when Aspect_CPU =>
10772 T := RTE (RE_CPU_Range);
10773
10774 -- Default_Component_Value is resolved with the component type
10775
10776 when Aspect_Default_Component_Value =>
10777 T := Component_Type (Entity (ASN));
10778
10779 when Aspect_Default_Storage_Pool =>
10780 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
10781
10782 -- Default_Value is resolved with the type entity in question
10783
10784 when Aspect_Default_Value =>
10785 T := Entity (ASN);
10786
10787 when Aspect_Dispatching_Domain =>
10788 T := RTE (RE_Dispatching_Domain);
10789
10790 when Aspect_External_Tag =>
10791 T := Standard_String;
10792
10793 when Aspect_External_Name =>
10794 T := Standard_String;
10795
10796 when Aspect_Link_Name =>
10797 T := Standard_String;
10798
10799 when Aspect_Interrupt_Priority
10800 | Aspect_Priority
10801 =>
10802 T := Standard_Integer;
10803
10804 when Aspect_Relative_Deadline =>
10805 T := RTE (RE_Time_Span);
10806
10807 when Aspect_Secondary_Stack_Size =>
10808 T := Standard_Integer;
10809
10810 when Aspect_Small =>
10811
10812 -- Note that the expression can be of any real type (not just a
10813 -- real universal literal) as long as it is a static constant.
10814
10815 T := Any_Real;
10816
10817 -- For a simple storage pool, we have to retrieve the type of the
10818 -- pool object associated with the aspect's corresponding attribute
10819 -- definition clause.
10820
10821 when Aspect_Simple_Storage_Pool =>
10822 T := Etype (Expression (Aspect_Rep_Item (ASN)));
10823
10824 when Aspect_Storage_Pool =>
10825 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
10826
10827 when Aspect_Alignment
10828 | Aspect_Component_Size
10829 | Aspect_Machine_Radix
10830 | Aspect_Object_Size
10831 | Aspect_Size
10832 | Aspect_Storage_Size
10833 | Aspect_Stream_Size
10834 | Aspect_Value_Size
10835 =>
10836 T := Any_Integer;
10837
10838 when Aspect_Linker_Section =>
10839 T := Standard_String;
10840
10841 when Aspect_Synchronization =>
10842 return;
10843
10844 -- Special case, the expression of these aspects is just an entity
10845 -- that does not need any resolution, so just analyze.
10846
10847 when Aspect_Input
10848 | Aspect_Output
10849 | Aspect_Put_Image
10850 | Aspect_Read
10851 | Aspect_Warnings
10852 | Aspect_Write
10853 =>
10854 Analyze (Expression (ASN));
10855 return;
10856
10857 -- Same for Iterator aspects, where the expression is a function
10858 -- name. Legality rules are checked separately.
10859
10860 when Aspect_Constant_Indexing
10861 | Aspect_Default_Iterator
10862 | Aspect_Iterator_Element
10863 | Aspect_Variable_Indexing
10864 =>
10865 Analyze (Expression (ASN));
10866 return;
10867
10868 -- Same for Literal aspects, where the expression is a function
10869 -- name. Legality rules are checked separately. Use Expr to avoid
10870 -- losing track of the previous resolution of Expression.
10871
10872 when Aspect_Integer_Literal
10873 | Aspect_Real_Literal
10874 | Aspect_String_Literal
10875 =>
10876 Set_Entity (Expression (ASN), Entity (Expr));
10877 Set_Etype (Expression (ASN), Etype (Expr));
10878 Set_Is_Overloaded (Expression (ASN), False);
10879 Analyze (Expression (ASN));
10880 return;
10881
10882 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
10883
10884 when Aspect_Iterable =>
10885 T := Entity (ASN);
10886
10887 declare
10888 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
10889 Assoc : Node_Id;
10890 Expr : Node_Id;
10891
10892 begin
10893 if Cursor = Any_Type then
10894 return;
10895 end if;
10896
10897 Assoc := First (Component_Associations (Expression (ASN)));
10898 while Present (Assoc) loop
10899 Expr := Expression (Assoc);
10900 Analyze (Expr);
10901
10902 if not Error_Posted (Expr) then
10903 Resolve_Iterable_Operation
10904 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
10905 end if;
10906
10907 Next (Assoc);
10908 end loop;
10909 end;
10910
10911 return;
10912
10913 when Aspect_Aggregate =>
10914 Resolve_Aspect_Aggregate (Entity (ASN), Expr);
10915 return;
10916
10917 -- Invariant/Predicate take boolean expressions
10918
10919 when Aspect_Dynamic_Predicate
10920 | Aspect_Invariant
10921 | Aspect_Predicate
10922 | Aspect_Static_Predicate
10923 | Aspect_Type_Invariant
10924 =>
10925 T := Standard_Boolean;
10926
10927 when Aspect_Predicate_Failure =>
10928 T := Standard_String;
10929
10930 -- Here is the list of aspects that don't require delay analysis
10931
10932 when Aspect_Abstract_State
10933 | Aspect_Annotate
10934 | Aspect_Async_Readers
10935 | Aspect_Async_Writers
10936 | Aspect_Constant_After_Elaboration
10937 | Aspect_Contract_Cases
10938 | Aspect_Default_Initial_Condition
10939 | Aspect_Depends
10940 | Aspect_Dimension
10941 | Aspect_Dimension_System
10942 | Aspect_Effective_Reads
10943 | Aspect_Effective_Writes
10944 | Aspect_Extensions_Visible
10945 | Aspect_Ghost
10946 | Aspect_Global
10947 | Aspect_Implicit_Dereference
10948 | Aspect_Initial_Condition
10949 | Aspect_Initializes
10950 | Aspect_Max_Entry_Queue_Depth
10951 | Aspect_Max_Entry_Queue_Length
10952 | Aspect_Max_Queue_Length
10953 | Aspect_No_Caching
10954 | Aspect_Obsolescent
10955 | Aspect_Part_Of
10956 | Aspect_Post
10957 | Aspect_Postcondition
10958 | Aspect_Pre
10959 | Aspect_Precondition
10960 | Aspect_Refined_Depends
10961 | Aspect_Refined_Global
10962 | Aspect_Refined_Post
10963 | Aspect_Refined_State
10964 | Aspect_Relaxed_Initialization
10965 | Aspect_SPARK_Mode
10966 | Aspect_Subprogram_Variant
10967 | Aspect_Suppress
10968 | Aspect_Test_Case
10969 | Aspect_Unimplemented
10970 | Aspect_Unsuppress
10971 | Aspect_Volatile_Function
10972 =>
10973 raise Program_Error;
10974
10975 end case;
10976
10977 -- Do the preanalyze call
10978
10979 if Present (Expression (ASN)) then
10980 Preanalyze_Spec_Expression (Expression (ASN), T);
10981 end if;
10982 end Check_Aspect_At_Freeze_Point;
10983
10984 -----------------------------------
10985 -- Check_Constant_Address_Clause --
10986 -----------------------------------
10987
10988 procedure Check_Constant_Address_Clause
10989 (Expr : Node_Id;
10990 U_Ent : Entity_Id)
10991 is
10992 procedure Check_At_Constant_Address (Nod : Node_Id);
10993 -- Checks that the given node N represents a name whose 'Address is
10994 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
10995 -- address value is the same at the point of declaration of U_Ent and at
10996 -- the time of elaboration of the address clause.
10997
10998 procedure Check_Expr_Constants (Nod : Node_Id);
10999 -- Checks that Nod meets the requirements for a constant address clause
11000 -- in the sense of the enclosing procedure.
11001
11002 procedure Check_List_Constants (Lst : List_Id);
11003 -- Check that all elements of list Lst meet the requirements for a
11004 -- constant address clause in the sense of the enclosing procedure.
11005
11006 -------------------------------
11007 -- Check_At_Constant_Address --
11008 -------------------------------
11009
11010 procedure Check_At_Constant_Address (Nod : Node_Id) is
11011 begin
11012 if Is_Entity_Name (Nod) then
11013 if Present (Address_Clause (Entity ((Nod)))) then
11014 Error_Msg_NE
11015 ("invalid address clause for initialized object &!",
11016 Nod, U_Ent);
11017 Error_Msg_NE
11018 ("address for& cannot depend on another address clause! "
11019 & "(RM 13.1(22))!", Nod, U_Ent);
11020
11021 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
11022 and then Sloc (U_Ent) < Sloc (Entity (Nod))
11023 then
11024 Error_Msg_NE
11025 ("invalid address clause for initialized object &!",
11026 Nod, U_Ent);
11027 Error_Msg_Node_2 := U_Ent;
11028 Error_Msg_NE
11029 ("\& must be defined before & (RM 13.1(22))!",
11030 Nod, Entity (Nod));
11031 end if;
11032
11033 elsif Nkind (Nod) = N_Selected_Component then
11034 declare
11035 T : constant Entity_Id := Etype (Prefix (Nod));
11036
11037 begin
11038 if (Is_Record_Type (T)
11039 and then Has_Discriminants (T))
11040 or else
11041 (Is_Access_Type (T)
11042 and then Is_Record_Type (Designated_Type (T))
11043 and then Has_Discriminants (Designated_Type (T)))
11044 then
11045 Error_Msg_NE
11046 ("invalid address clause for initialized object &!",
11047 Nod, U_Ent);
11048 Error_Msg_N
11049 ("\address cannot depend on component of discriminated "
11050 & "record (RM 13.1(22))!", Nod);
11051 else
11052 Check_At_Constant_Address (Prefix (Nod));
11053 end if;
11054 end;
11055
11056 elsif Nkind (Nod) = N_Indexed_Component then
11057 Check_At_Constant_Address (Prefix (Nod));
11058 Check_List_Constants (Expressions (Nod));
11059
11060 else
11061 Check_Expr_Constants (Nod);
11062 end if;
11063 end Check_At_Constant_Address;
11064
11065 --------------------------
11066 -- Check_Expr_Constants --
11067 --------------------------
11068
11069 procedure Check_Expr_Constants (Nod : Node_Id) is
11070 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
11071 Ent : Entity_Id := Empty;
11072
11073 begin
11074 if Nkind (Nod) in N_Has_Etype
11075 and then Etype (Nod) = Any_Type
11076 then
11077 return;
11078 end if;
11079
11080 case Nkind (Nod) is
11081 when N_Empty
11082 | N_Error
11083 =>
11084 return;
11085
11086 when N_Expanded_Name
11087 | N_Identifier
11088 =>
11089 Ent := Entity (Nod);
11090
11091 -- We need to look at the original node if it is different
11092 -- from the node, since we may have rewritten things and
11093 -- substituted an identifier representing the rewrite.
11094
11095 if Is_Rewrite_Substitution (Nod) then
11096 Check_Expr_Constants (Original_Node (Nod));
11097
11098 -- If the node is an object declaration without initial
11099 -- value, some code has been expanded, and the expression
11100 -- is not constant, even if the constituents might be
11101 -- acceptable, as in A'Address + offset.
11102
11103 if Ekind (Ent) = E_Variable
11104 and then
11105 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
11106 and then
11107 No (Expression (Declaration_Node (Ent)))
11108 then
11109 Error_Msg_NE
11110 ("invalid address clause for initialized object &!",
11111 Nod, U_Ent);
11112
11113 -- If entity is constant, it may be the result of expanding
11114 -- a check. We must verify that its declaration appears
11115 -- before the object in question, else we also reject the
11116 -- address clause.
11117
11118 elsif Ekind (Ent) = E_Constant
11119 and then In_Same_Source_Unit (Ent, U_Ent)
11120 and then Sloc (Ent) > Loc_U_Ent
11121 then
11122 Error_Msg_NE
11123 ("invalid address clause for initialized object &!",
11124 Nod, U_Ent);
11125 end if;
11126
11127 return;
11128 end if;
11129
11130 -- Otherwise look at the identifier and see if it is OK
11131
11132 if Ekind (Ent) in E_Named_Integer | E_Named_Real
11133 or else Is_Type (Ent)
11134 then
11135 return;
11136
11137 elsif Ekind (Ent) in E_Constant | E_In_Parameter then
11138
11139 -- This is the case where we must have Ent defined before
11140 -- U_Ent. Clearly if they are in different units this
11141 -- requirement is met since the unit containing Ent is
11142 -- already processed.
11143
11144 if not In_Same_Source_Unit (Ent, U_Ent) then
11145 return;
11146
11147 -- Otherwise location of Ent must be before the location
11148 -- of U_Ent, that's what prior defined means.
11149
11150 elsif Sloc (Ent) < Loc_U_Ent then
11151 return;
11152
11153 else
11154 Error_Msg_NE
11155 ("invalid address clause for initialized object &!",
11156 Nod, U_Ent);
11157 Error_Msg_Node_2 := U_Ent;
11158 Error_Msg_NE
11159 ("\& must be defined before & (RM 13.1(22))!",
11160 Nod, Ent);
11161 end if;
11162
11163 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
11164 Check_Expr_Constants (Original_Node (Nod));
11165
11166 else
11167 Error_Msg_NE
11168 ("invalid address clause for initialized object &!",
11169 Nod, U_Ent);
11170
11171 if Comes_From_Source (Ent) then
11172 Error_Msg_NE
11173 ("\reference to variable& not allowed"
11174 & " (RM 13.1(22))!", Nod, Ent);
11175 else
11176 Error_Msg_N
11177 ("non-static expression not allowed"
11178 & " (RM 13.1(22))!", Nod);
11179 end if;
11180 end if;
11181
11182 when N_Integer_Literal =>
11183
11184 -- If this is a rewritten unchecked conversion, in a system
11185 -- where Address is an integer type, always use the base type
11186 -- for a literal value. This is user-friendly and prevents
11187 -- order-of-elaboration issues with instances of unchecked
11188 -- conversion.
11189
11190 if Nkind (Original_Node (Nod)) = N_Function_Call then
11191 Set_Etype (Nod, Base_Type (Etype (Nod)));
11192 end if;
11193
11194 when N_Character_Literal
11195 | N_Real_Literal
11196 | N_String_Literal
11197 =>
11198 return;
11199
11200 when N_Range =>
11201 Check_Expr_Constants (Low_Bound (Nod));
11202 Check_Expr_Constants (High_Bound (Nod));
11203
11204 when N_Explicit_Dereference =>
11205 Check_Expr_Constants (Prefix (Nod));
11206
11207 when N_Indexed_Component =>
11208 Check_Expr_Constants (Prefix (Nod));
11209 Check_List_Constants (Expressions (Nod));
11210
11211 when N_Slice =>
11212 Check_Expr_Constants (Prefix (Nod));
11213 Check_Expr_Constants (Discrete_Range (Nod));
11214
11215 when N_Selected_Component =>
11216 Check_Expr_Constants (Prefix (Nod));
11217
11218 when N_Attribute_Reference =>
11219 if Attribute_Name (Nod) in Name_Address
11220 | Name_Access
11221 | Name_Unchecked_Access
11222 | Name_Unrestricted_Access
11223 then
11224 Check_At_Constant_Address (Prefix (Nod));
11225
11226 -- Normally, System'To_Address will have been transformed into
11227 -- an Unchecked_Conversion, but in -gnatc mode, it will not,
11228 -- and we don't want to give an error, because the whole point
11229 -- of 'To_Address is that it is static.
11230
11231 elsif Attribute_Name (Nod) = Name_To_Address then
11232 pragma Assert (Operating_Mode = Check_Semantics);
11233 null;
11234
11235 else
11236 Check_Expr_Constants (Prefix (Nod));
11237 Check_List_Constants (Expressions (Nod));
11238 end if;
11239
11240 when N_Aggregate =>
11241 Check_List_Constants (Component_Associations (Nod));
11242 Check_List_Constants (Expressions (Nod));
11243
11244 when N_Component_Association =>
11245 Check_Expr_Constants (Expression (Nod));
11246
11247 when N_Extension_Aggregate =>
11248 Check_Expr_Constants (Ancestor_Part (Nod));
11249 Check_List_Constants (Component_Associations (Nod));
11250 Check_List_Constants (Expressions (Nod));
11251
11252 when N_Null =>
11253 return;
11254
11255 when N_Binary_Op
11256 | N_Membership_Test
11257 | N_Short_Circuit
11258 =>
11259 Check_Expr_Constants (Left_Opnd (Nod));
11260 Check_Expr_Constants (Right_Opnd (Nod));
11261
11262 when N_Unary_Op =>
11263 Check_Expr_Constants (Right_Opnd (Nod));
11264
11265 when N_Allocator
11266 | N_Qualified_Expression
11267 | N_Type_Conversion
11268 | N_Unchecked_Type_Conversion
11269 =>
11270 Check_Expr_Constants (Expression (Nod));
11271
11272 when N_Function_Call =>
11273 if not Is_Pure (Entity (Name (Nod))) then
11274 Error_Msg_NE
11275 ("invalid address clause for initialized object &!",
11276 Nod, U_Ent);
11277
11278 Error_Msg_NE
11279 ("\function & is not pure (RM 13.1(22))!",
11280 Nod, Entity (Name (Nod)));
11281
11282 else
11283 Check_List_Constants (Parameter_Associations (Nod));
11284 end if;
11285
11286 when N_Parameter_Association =>
11287 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
11288
11289 when others =>
11290 Error_Msg_NE
11291 ("invalid address clause for initialized object &!",
11292 Nod, U_Ent);
11293 Error_Msg_NE
11294 ("\must be constant defined before& (RM 13.1(22))!",
11295 Nod, U_Ent);
11296 end case;
11297 end Check_Expr_Constants;
11298
11299 --------------------------
11300 -- Check_List_Constants --
11301 --------------------------
11302
11303 procedure Check_List_Constants (Lst : List_Id) is
11304 Nod1 : Node_Id;
11305
11306 begin
11307 if Present (Lst) then
11308 Nod1 := First (Lst);
11309 while Present (Nod1) loop
11310 Check_Expr_Constants (Nod1);
11311 Next (Nod1);
11312 end loop;
11313 end if;
11314 end Check_List_Constants;
11315
11316 -- Start of processing for Check_Constant_Address_Clause
11317
11318 begin
11319 -- If rep_clauses are to be ignored, no need for legality checks. In
11320 -- particular, no need to pester user about rep clauses that violate the
11321 -- rule on constant addresses, given that these clauses will be removed
11322 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
11323 -- we want to relax these checks.
11324
11325 if not Ignore_Rep_Clauses and not CodePeer_Mode then
11326 Check_Expr_Constants (Expr);
11327 end if;
11328 end Check_Constant_Address_Clause;
11329
11330 ---------------------------
11331 -- Check_Pool_Size_Clash --
11332 ---------------------------
11333
11334 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
11335 Post : Node_Id;
11336
11337 begin
11338 -- We need to find out which one came first. Note that in the case of
11339 -- aspects mixed with pragmas there are cases where the processing order
11340 -- is reversed, which is why we do the check here.
11341
11342 if Sloc (SP) < Sloc (SS) then
11343 Error_Msg_Sloc := Sloc (SP);
11344 Post := SS;
11345 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
11346
11347 else
11348 Error_Msg_Sloc := Sloc (SS);
11349 Post := SP;
11350 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
11351 end if;
11352
11353 Error_Msg_N
11354 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
11355 end Check_Pool_Size_Clash;
11356
11357 ----------------------------------------
11358 -- Check_Record_Representation_Clause --
11359 ----------------------------------------
11360
11361 procedure Check_Record_Representation_Clause (N : Node_Id) is
11362 Loc : constant Source_Ptr := Sloc (N);
11363 Ident : constant Node_Id := Identifier (N);
11364 Rectype : Entity_Id;
11365 Fent : Entity_Id;
11366 CC : Node_Id;
11367 Fbit : Uint := No_Uint;
11368 Lbit : Uint := No_Uint;
11369 Hbit : Uint := Uint_0;
11370 Comp : Entity_Id;
11371 Pcomp : Entity_Id;
11372
11373 Max_Bit_So_Far : Uint;
11374 -- Records the maximum bit position so far. If all field positions
11375 -- are monotonically increasing, then we can skip the circuit for
11376 -- checking for overlap, since no overlap is possible.
11377
11378 Tagged_Parent : Entity_Id := Empty;
11379 -- This is set in the case of an extension for which we have either a
11380 -- size clause or Is_Fully_Repped_Tagged_Type True (indicating that all
11381 -- components are positioned by record representation clauses) on the
11382 -- parent type. In this case we check for overlap between components of
11383 -- this tagged type and the parent component. Tagged_Parent will point
11384 -- to this parent type. For all other cases, Tagged_Parent is Empty.
11385
11386 Parent_Last_Bit : Uint := No_Uint; -- init to avoid warning
11387 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
11388 -- last bit position for any field in the parent type. We only need to
11389 -- check overlap for fields starting below this point.
11390
11391 Overlap_Check_Required : Boolean;
11392 -- Used to keep track of whether or not an overlap check is required
11393
11394 Overlap_Detected : Boolean := False;
11395 -- Set True if an overlap is detected
11396
11397 Ccount : Natural := 0;
11398 -- Number of component clauses in record rep clause
11399
11400 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
11401 -- Given two entities for record components or discriminants, checks
11402 -- if they have overlapping component clauses and issues errors if so.
11403
11404 procedure Find_Component;
11405 -- Finds component entity corresponding to current component clause (in
11406 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
11407 -- start/stop bits for the field. If there is no matching component or
11408 -- if the matching component does not have a component clause, then
11409 -- that's an error and Comp is set to Empty, but no error message is
11410 -- issued, since the message was already given. Comp is also set to
11411 -- Empty if the current "component clause" is in fact a pragma.
11412
11413 procedure Record_Hole_Check
11414 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean);
11415 -- Checks for gaps in the given Rectype. Compute After_Last, the bit
11416 -- number after the last component. Warn is True on the initial call,
11417 -- and warnings are given for gaps. For a type extension, this is called
11418 -- recursively to compute After_Last for the parent type; in this case
11419 -- Warn is False and the warnings are suppressed.
11420
11421 procedure Component_Order_Check (Rectype : Entity_Id);
11422 -- Check that the order of component clauses agrees with the order of
11423 -- component declarations, and that the component clauses are given in
11424 -- increasing order of bit offset.
11425
11426 -----------------------------
11427 -- Check_Component_Overlap --
11428 -----------------------------
11429
11430 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
11431 CC1 : constant Node_Id := Component_Clause (C1_Ent);
11432 CC2 : constant Node_Id := Component_Clause (C2_Ent);
11433
11434 begin
11435 if Present (CC1) and then Present (CC2) then
11436
11437 -- Exclude odd case where we have two tag components in the same
11438 -- record, both at location zero. This seems a bit strange, but
11439 -- it seems to happen in some circumstances, perhaps on an error.
11440
11441 if Chars (C1_Ent) = Name_uTag then
11442 return;
11443 end if;
11444
11445 -- Here we check if the two fields overlap
11446
11447 declare
11448 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
11449 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
11450 E1 : constant Uint := S1 + Esize (C1_Ent);
11451 E2 : constant Uint := S2 + Esize (C2_Ent);
11452
11453 begin
11454 if E2 <= S1 or else E1 <= S2 then
11455 null;
11456 else
11457 Error_Msg_Node_2 := Component_Name (CC2);
11458 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
11459 Error_Msg_Node_1 := Component_Name (CC1);
11460 Error_Msg_N
11461 ("component& overlaps & #", Component_Name (CC1));
11462 Overlap_Detected := True;
11463 end if;
11464 end;
11465 end if;
11466 end Check_Component_Overlap;
11467
11468 ---------------------------
11469 -- Component_Order_Check --
11470 ---------------------------
11471
11472 procedure Component_Order_Check (Rectype : Entity_Id) is
11473 Comp : Entity_Id := First_Component (Rectype);
11474 Clause : Node_Id := First (Component_Clauses (N));
11475 Prev_Bit_Offset : Uint := Uint_0;
11476 OOO : constant String :=
11477 "?component clause out of order with respect to declaration";
11478
11479 begin
11480 -- Step Comp through components and Clause through component clauses,
11481 -- skipping pragmas. We ignore discriminants and variant parts,
11482 -- because we get most of the benefit from the plain vanilla
11483 -- component cases, without the extra complexity. If we find a Comp
11484 -- and Clause that don't match, give a warning on both and quit. If
11485 -- we find two subsequent clauses out of order by bit layout, give
11486 -- warning and quit. On each iteration, Prev_Bit_Offset is the one
11487 -- from the previous iteration (or 0 to start).
11488
11489 while Present (Comp) and then Present (Clause) loop
11490 if Nkind (Clause) = N_Component_Clause
11491 and then Ekind (Entity (Component_Name (Clause))) = E_Component
11492 then
11493 if Entity (Component_Name (Clause)) /= Comp then
11494 Error_Msg_N (OOO, Comp);
11495 Error_Msg_N (OOO, Clause);
11496 exit;
11497 end if;
11498
11499 if not Reverse_Bit_Order (Rectype)
11500 and then not Reverse_Storage_Order (Rectype)
11501 and then Component_Bit_Offset (Comp) < Prev_Bit_Offset
11502 then
11503 Error_Msg_N ("?memory layout out of order", Clause);
11504 exit;
11505 end if;
11506
11507 Prev_Bit_Offset := Component_Bit_Offset (Comp);
11508 Next_Component (Comp);
11509 end if;
11510
11511 Next (Clause);
11512 end loop;
11513 end Component_Order_Check;
11514
11515 --------------------
11516 -- Find_Component --
11517 --------------------
11518
11519 procedure Find_Component is
11520
11521 procedure Search_Component (R : Entity_Id);
11522 -- Search components of R for a match. If found, Comp is set
11523
11524 ----------------------
11525 -- Search_Component --
11526 ----------------------
11527
11528 procedure Search_Component (R : Entity_Id) is
11529 begin
11530 Comp := First_Component_Or_Discriminant (R);
11531 while Present (Comp) loop
11532
11533 -- Ignore error of attribute name for component name (we
11534 -- already gave an error message for this, so no need to
11535 -- complain here)
11536
11537 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
11538 null;
11539 else
11540 exit when Chars (Comp) = Chars (Component_Name (CC));
11541 end if;
11542
11543 Next_Component_Or_Discriminant (Comp);
11544 end loop;
11545 end Search_Component;
11546
11547 -- Start of processing for Find_Component
11548
11549 begin
11550 -- Return with Comp set to Empty if we have a pragma
11551
11552 if Nkind (CC) = N_Pragma then
11553 Comp := Empty;
11554 return;
11555 end if;
11556
11557 -- Search current record for matching component
11558
11559 Search_Component (Rectype);
11560
11561 -- If not found, maybe component of base type discriminant that is
11562 -- absent from statically constrained first subtype.
11563
11564 if No (Comp) then
11565 Search_Component (Base_Type (Rectype));
11566 end if;
11567
11568 -- If no component, or the component does not reference the component
11569 -- clause in question, then there was some previous error for which
11570 -- we already gave a message, so just return with Comp Empty.
11571
11572 if No (Comp) or else Component_Clause (Comp) /= CC then
11573 Check_Error_Detected;
11574 Comp := Empty;
11575
11576 -- Normal case where we have a component clause
11577
11578 else
11579 Fbit := Component_Bit_Offset (Comp);
11580 Lbit := Fbit + Esize (Comp) - 1;
11581 end if;
11582 end Find_Component;
11583
11584 -----------------------
11585 -- Record_Hole_Check --
11586 -----------------------
11587
11588 procedure Record_Hole_Check
11589 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean)
11590 is
11591 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
11592 -- Full declaration of record type
11593
11594 procedure Check_Component_List
11595 (DS : List_Id;
11596 CL : Node_Id;
11597 Sbit : Uint;
11598 Abit : out Uint);
11599 -- Check component list CL for holes. DS is a list of discriminant
11600 -- specifications to be included in the consideration of components.
11601 -- Sbit is the starting bit, which is zero if there are no preceding
11602 -- components (before a variant part, or a parent type, or a tag
11603 -- field). If there are preceding components, Sbit is the bit just
11604 -- after the last such component. Abit is set to the bit just after
11605 -- the last component of DS and CL.
11606
11607 --------------------------
11608 -- Check_Component_List --
11609 --------------------------
11610
11611 procedure Check_Component_List
11612 (DS : List_Id;
11613 CL : Node_Id;
11614 Sbit : Uint;
11615 Abit : out Uint)
11616 is
11617 Compl : Integer;
11618
11619 begin
11620 Compl := Integer (List_Length (Component_Items (CL)));
11621
11622 if DS /= No_List then
11623 Compl := Compl + Integer (List_Length (DS));
11624 end if;
11625
11626 declare
11627 Comps : array (Natural range 0 .. Compl) of Entity_Id;
11628 -- Gather components (zero entry is for sort routine)
11629
11630 Ncomps : Natural := 0;
11631 -- Number of entries stored in Comps (starting at Comps (1))
11632
11633 Citem : Node_Id;
11634 -- One component item or discriminant specification
11635
11636 Nbit : Uint;
11637 -- Starting bit for next component
11638
11639 CEnt : Entity_Id;
11640 -- Component entity
11641
11642 Variant : Node_Id;
11643 -- One variant
11644
11645 function Lt (Op1, Op2 : Natural) return Boolean;
11646 -- Compare routine for Sort
11647
11648 procedure Move (From : Natural; To : Natural);
11649 -- Move routine for Sort
11650
11651 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
11652
11653 --------
11654 -- Lt --
11655 --------
11656
11657 function Lt (Op1, Op2 : Natural) return Boolean is
11658 begin
11659 return Component_Bit_Offset (Comps (Op1))
11660 < Component_Bit_Offset (Comps (Op2));
11661 end Lt;
11662
11663 ----------
11664 -- Move --
11665 ----------
11666
11667 procedure Move (From : Natural; To : Natural) is
11668 begin
11669 Comps (To) := Comps (From);
11670 end Move;
11671
11672 begin
11673 -- Gather discriminants into Comp
11674
11675 if DS /= No_List then
11676 Citem := First (DS);
11677 while Present (Citem) loop
11678 if Nkind (Citem) = N_Discriminant_Specification then
11679 declare
11680 Ent : constant Entity_Id :=
11681 Defining_Identifier (Citem);
11682 begin
11683 if Ekind (Ent) = E_Discriminant then
11684 Ncomps := Ncomps + 1;
11685 Comps (Ncomps) := Ent;
11686 end if;
11687 end;
11688 end if;
11689
11690 Next (Citem);
11691 end loop;
11692 end if;
11693
11694 -- Gather component entities into Comp
11695
11696 Citem := First (Component_Items (CL));
11697 while Present (Citem) loop
11698 if Nkind (Citem) = N_Component_Declaration then
11699 Ncomps := Ncomps + 1;
11700 Comps (Ncomps) := Defining_Identifier (Citem);
11701 end if;
11702
11703 Next (Citem);
11704 end loop;
11705
11706 -- Now sort the component entities based on the first bit.
11707 -- Note we already know there are no overlapping components.
11708
11709 Sorting.Sort (Ncomps);
11710
11711 -- Loop through entries checking for holes
11712
11713 Nbit := Sbit;
11714 for J in 1 .. Ncomps loop
11715 CEnt := Comps (J);
11716 pragma Annotate (CodePeer, Modified, CEnt);
11717
11718 declare
11719 CBO : constant Uint := Component_Bit_Offset (CEnt);
11720
11721 begin
11722 -- Skip components with unknown offsets
11723
11724 if CBO /= No_Uint and then CBO >= 0 then
11725 Error_Msg_Uint_1 := CBO - Nbit;
11726
11727 if Warn and then Error_Msg_Uint_1 > 0 then
11728 Error_Msg_NE
11729 ("?H?^-bit gap before component&",
11730 Component_Name (Component_Clause (CEnt)),
11731 CEnt);
11732 end if;
11733
11734 Nbit := CBO + Esize (CEnt);
11735 end if;
11736 end;
11737 end loop;
11738
11739 -- Set Abit to just after the last nonvariant component
11740
11741 Abit := Nbit;
11742
11743 -- Process variant parts recursively if present. Set Abit to
11744 -- the maximum for all variant parts.
11745
11746 if Present (Variant_Part (CL)) then
11747 declare
11748 Var_Start : constant Uint := Nbit;
11749 begin
11750 Variant := First (Variants (Variant_Part (CL)));
11751 while Present (Variant) loop
11752 Check_Component_List
11753 (No_List, Component_List (Variant), Var_Start, Nbit);
11754 Next (Variant);
11755 if Nbit > Abit then
11756 Abit := Nbit;
11757 end if;
11758 end loop;
11759 end;
11760 end if;
11761 end;
11762 end Check_Component_List;
11763
11764 Sbit : Uint;
11765 -- Starting bit for call to Check_Component_List. Zero for an
11766 -- untagged type. The size of the Tag for a nonderived tagged
11767 -- type. Parent size for a type extension.
11768
11769 Record_Definition : Node_Id;
11770 -- Record_Definition containing Component_List to pass to
11771 -- Check_Component_List.
11772
11773 -- Start of processing for Record_Hole_Check
11774
11775 begin
11776 if Is_Tagged_Type (Rectype) then
11777 Sbit := UI_From_Int (System_Address_Size);
11778 else
11779 Sbit := Uint_0;
11780 end if;
11781
11782 After_Last := Uint_0;
11783
11784 if Nkind (Decl) = N_Full_Type_Declaration then
11785 Record_Definition := Type_Definition (Decl);
11786
11787 -- If we have a record extension, set Sbit to point after the last
11788 -- component of the parent type, by calling Record_Hole_Check
11789 -- recursively.
11790
11791 if Nkind (Record_Definition) = N_Derived_Type_Definition then
11792 Record_Definition := Record_Extension_Part (Record_Definition);
11793 Record_Hole_Check (Underlying_Type (Parent_Subtype (Rectype)),
11794 After_Last => Sbit, Warn => False);
11795 end if;
11796
11797 if Nkind (Record_Definition) = N_Record_Definition then
11798 Check_Component_List
11799 (Discriminant_Specifications (Decl),
11800 Component_List (Record_Definition),
11801 Sbit, After_Last);
11802 end if;
11803 end if;
11804 end Record_Hole_Check;
11805
11806 -- Start of processing for Check_Record_Representation_Clause
11807
11808 begin
11809 Find_Type (Ident);
11810 Rectype := Entity (Ident);
11811
11812 if Rectype = Any_Type then
11813 return;
11814 end if;
11815
11816 Rectype := Underlying_Type (Rectype);
11817
11818 -- See if we have a fully repped derived tagged type
11819
11820 declare
11821 PS : constant Entity_Id := Parent_Subtype (Rectype);
11822
11823 begin
11824 if Present (PS) and then Known_Static_RM_Size (PS) then
11825 Tagged_Parent := PS;
11826 Parent_Last_Bit := RM_Size (PS) - 1;
11827
11828 elsif Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
11829 Tagged_Parent := PS;
11830
11831 -- Find maximum bit of any component of the parent type
11832
11833 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
11834 Pcomp := First_Entity (Tagged_Parent);
11835 while Present (Pcomp) loop
11836 if Ekind (Pcomp) in E_Discriminant | E_Component then
11837 if Component_Bit_Offset (Pcomp) /= No_Uint
11838 and then Known_Static_Esize (Pcomp)
11839 then
11840 Parent_Last_Bit :=
11841 UI_Max
11842 (Parent_Last_Bit,
11843 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
11844 end if;
11845 else
11846
11847 -- Skip anonymous types generated for constrained array
11848 -- or record components.
11849
11850 null;
11851 end if;
11852
11853 Next_Entity (Pcomp);
11854 end loop;
11855 end if;
11856 end;
11857
11858 -- All done if no component clauses
11859
11860 CC := First (Component_Clauses (N));
11861
11862 if No (CC) then
11863 return;
11864 end if;
11865
11866 -- If a tag is present, then create a component clause that places it
11867 -- at the start of the record (otherwise gigi may place it after other
11868 -- fields that have rep clauses).
11869
11870 Fent := First_Entity (Rectype);
11871
11872 if Nkind (Fent) = N_Defining_Identifier
11873 and then Chars (Fent) = Name_uTag
11874 then
11875 Set_Component_Bit_Offset (Fent, Uint_0);
11876 Set_Normalized_Position (Fent, Uint_0);
11877 Set_Normalized_First_Bit (Fent, Uint_0);
11878 Set_Normalized_Position_Max (Fent, Uint_0);
11879 Init_Esize (Fent, System_Address_Size);
11880
11881 Set_Component_Clause (Fent,
11882 Make_Component_Clause (Loc,
11883 Component_Name => Make_Identifier (Loc, Name_uTag),
11884
11885 Position => Make_Integer_Literal (Loc, Uint_0),
11886 First_Bit => Make_Integer_Literal (Loc, Uint_0),
11887 Last_Bit =>
11888 Make_Integer_Literal (Loc,
11889 UI_From_Int (System_Address_Size - 1))));
11890
11891 Ccount := Ccount + 1;
11892 end if;
11893
11894 Max_Bit_So_Far := Uint_Minus_1;
11895 Overlap_Check_Required := False;
11896
11897 -- Process the component clauses
11898
11899 while Present (CC) loop
11900 Find_Component;
11901
11902 if Present (Comp) then
11903 Ccount := Ccount + 1;
11904
11905 -- We need a full overlap check if record positions non-monotonic
11906
11907 if Fbit <= Max_Bit_So_Far then
11908 Overlap_Check_Required := True;
11909 end if;
11910
11911 Max_Bit_So_Far := Lbit;
11912
11913 -- Check bit position out of range of specified size
11914
11915 if Has_Size_Clause (Rectype)
11916 and then RM_Size (Rectype) <= Lbit
11917 then
11918 Error_Msg_Uint_1 := RM_Size (Rectype);
11919 Error_Msg_Uint_2 := Lbit + 1;
11920 Error_Msg_N ("bit number out of range of specified "
11921 & "size (expected ^, got ^)",
11922 Last_Bit (CC));
11923
11924 -- Check for overlap with tag or parent component
11925
11926 else
11927 if Is_Tagged_Type (Rectype)
11928 and then Fbit < System_Address_Size
11929 then
11930 Error_Msg_NE
11931 ("component overlaps tag field of&",
11932 Component_Name (CC), Rectype);
11933 Overlap_Detected := True;
11934
11935 elsif Present (Tagged_Parent)
11936 and then Fbit <= Parent_Last_Bit
11937 then
11938 Error_Msg_NE
11939 ("component overlaps parent field of&",
11940 Component_Name (CC), Rectype);
11941 Overlap_Detected := True;
11942 end if;
11943
11944 if Hbit < Lbit then
11945 Hbit := Lbit;
11946 end if;
11947 end if;
11948 end if;
11949
11950 Next (CC);
11951 end loop;
11952
11953 -- Now that we have processed all the component clauses, check for
11954 -- overlap. We have to leave this till last, since the components can
11955 -- appear in any arbitrary order in the representation clause.
11956
11957 -- We do not need this check if all specified ranges were monotonic,
11958 -- as recorded by Overlap_Check_Required being False at this stage.
11959
11960 -- This first section checks if there are any overlapping entries at
11961 -- all. It does this by sorting all entries and then seeing if there are
11962 -- any overlaps. If there are none, then that is decisive, but if there
11963 -- are overlaps, they may still be OK (they may result from fields in
11964 -- different variants).
11965
11966 if Overlap_Check_Required then
11967 Overlap_Check1 : declare
11968
11969 OC_Fbit : array (0 .. Ccount) of Uint;
11970 -- First-bit values for component clauses, the value is the offset
11971 -- of the first bit of the field from start of record. The zero
11972 -- entry is for use in sorting.
11973
11974 OC_Lbit : array (0 .. Ccount) of Uint;
11975 -- Last-bit values for component clauses, the value is the offset
11976 -- of the last bit of the field from start of record. The zero
11977 -- entry is for use in sorting.
11978
11979 OC_Count : Natural := 0;
11980 -- Count of entries in OC_Fbit and OC_Lbit
11981
11982 function OC_Lt (Op1, Op2 : Natural) return Boolean;
11983 -- Compare routine for Sort
11984
11985 procedure OC_Move (From : Natural; To : Natural);
11986 -- Move routine for Sort
11987
11988 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
11989
11990 -----------
11991 -- OC_Lt --
11992 -----------
11993
11994 function OC_Lt (Op1, Op2 : Natural) return Boolean is
11995 begin
11996 return OC_Fbit (Op1) < OC_Fbit (Op2);
11997 end OC_Lt;
11998
11999 -------------
12000 -- OC_Move --
12001 -------------
12002
12003 procedure OC_Move (From : Natural; To : Natural) is
12004 begin
12005 OC_Fbit (To) := OC_Fbit (From);
12006 OC_Lbit (To) := OC_Lbit (From);
12007 end OC_Move;
12008
12009 -- Start of processing for Overlap_Check
12010
12011 begin
12012 CC := First (Component_Clauses (N));
12013 while Present (CC) loop
12014
12015 -- Exclude component clause already marked in error
12016
12017 if not Error_Posted (CC) then
12018 Find_Component;
12019
12020 if Present (Comp) then
12021 OC_Count := OC_Count + 1;
12022 OC_Fbit (OC_Count) := Fbit;
12023 OC_Lbit (OC_Count) := Lbit;
12024 end if;
12025 end if;
12026
12027 Next (CC);
12028 end loop;
12029
12030 Sorting.Sort (OC_Count);
12031
12032 Overlap_Check_Required := False;
12033 for J in 1 .. OC_Count - 1 loop
12034 if OC_Lbit (J) >= OC_Fbit (J + 1) then
12035 Overlap_Check_Required := True;
12036 exit;
12037 end if;
12038 end loop;
12039 end Overlap_Check1;
12040 end if;
12041
12042 -- If Overlap_Check_Required is still True, then we have to do the full
12043 -- scale overlap check, since we have at least two fields that do
12044 -- overlap, and we need to know if that is OK since they are in
12045 -- different variant, or whether we have a definite problem.
12046
12047 if Overlap_Check_Required then
12048 Overlap_Check2 : declare
12049 C1_Ent, C2_Ent : Entity_Id;
12050 -- Entities of components being checked for overlap
12051
12052 Clist : Node_Id;
12053 -- Component_List node whose Component_Items are being checked
12054
12055 Citem : Node_Id;
12056 -- Component declaration for component being checked
12057
12058 begin
12059 C1_Ent := First_Entity (Base_Type (Rectype));
12060
12061 -- Loop through all components in record. For each component check
12062 -- for overlap with any of the preceding elements on the component
12063 -- list containing the component and also, if the component is in
12064 -- a variant, check against components outside the case structure.
12065 -- This latter test is repeated recursively up the variant tree.
12066
12067 Main_Component_Loop : while Present (C1_Ent) loop
12068 if Ekind (C1_Ent) not in E_Component | E_Discriminant then
12069 goto Continue_Main_Component_Loop;
12070 end if;
12071
12072 -- Skip overlap check if entity has no declaration node. This
12073 -- happens with discriminants in constrained derived types.
12074 -- Possibly we are missing some checks as a result, but that
12075 -- does not seem terribly serious.
12076
12077 if No (Declaration_Node (C1_Ent)) then
12078 goto Continue_Main_Component_Loop;
12079 end if;
12080
12081 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
12082
12083 -- Loop through component lists that need checking. Check the
12084 -- current component list and all lists in variants above us.
12085
12086 Component_List_Loop : loop
12087
12088 -- If derived type definition, go to full declaration
12089 -- If at outer level, check discriminants if there are any.
12090
12091 if Nkind (Clist) = N_Derived_Type_Definition then
12092 Clist := Parent (Clist);
12093 end if;
12094
12095 -- Outer level of record definition, check discriminants
12096 -- but be careful not to flag a non-girder discriminant
12097 -- and the girder discriminant it renames as overlapping.
12098
12099 if Nkind (Clist) in N_Full_Type_Declaration
12100 | N_Private_Type_Declaration
12101 then
12102 if Has_Discriminants (Defining_Identifier (Clist)) then
12103 C2_Ent :=
12104 First_Discriminant (Defining_Identifier (Clist));
12105 while Present (C2_Ent) loop
12106 exit when
12107 Original_Record_Component (C1_Ent) =
12108 Original_Record_Component (C2_Ent);
12109 Check_Component_Overlap (C1_Ent, C2_Ent);
12110 Next_Discriminant (C2_Ent);
12111 end loop;
12112 end if;
12113
12114 -- Record extension case
12115
12116 elsif Nkind (Clist) = N_Derived_Type_Definition then
12117 Clist := Empty;
12118
12119 -- Otherwise check one component list
12120
12121 else
12122 Citem := First (Component_Items (Clist));
12123 while Present (Citem) loop
12124 if Nkind (Citem) = N_Component_Declaration then
12125 C2_Ent := Defining_Identifier (Citem);
12126 exit when C1_Ent = C2_Ent;
12127 Check_Component_Overlap (C1_Ent, C2_Ent);
12128 end if;
12129
12130 Next (Citem);
12131 end loop;
12132 end if;
12133
12134 -- Check for variants above us (the parent of the Clist can
12135 -- be a variant, in which case its parent is a variant part,
12136 -- and the parent of the variant part is a component list
12137 -- whose components must all be checked against the current
12138 -- component for overlap).
12139
12140 if Nkind (Parent (Clist)) = N_Variant then
12141 Clist := Parent (Parent (Parent (Clist)));
12142
12143 -- Check for possible discriminant part in record, this
12144 -- is treated essentially as another level in the
12145 -- recursion. For this case the parent of the component
12146 -- list is the record definition, and its parent is the
12147 -- full type declaration containing the discriminant
12148 -- specifications.
12149
12150 elsif Nkind (Parent (Clist)) = N_Record_Definition then
12151 Clist := Parent (Parent ((Clist)));
12152
12153 -- If neither of these two cases, we are at the top of
12154 -- the tree.
12155
12156 else
12157 exit Component_List_Loop;
12158 end if;
12159 end loop Component_List_Loop;
12160
12161 <<Continue_Main_Component_Loop>>
12162 Next_Entity (C1_Ent);
12163
12164 end loop Main_Component_Loop;
12165 end Overlap_Check2;
12166 end if;
12167
12168 -- Skip the following warnings if overlap was detected; programmer
12169 -- should fix the errors first.
12170
12171 if not Overlap_Detected then
12172 -- Check for record holes (gaps)
12173
12174 if Warn_On_Record_Holes then
12175 declare
12176 Ignore : Uint;
12177 begin
12178 Record_Hole_Check (Rectype, After_Last => Ignore, Warn => True);
12179 end;
12180 end if;
12181
12182 -- Check for out-of-order component clauses
12183
12184 if Warn_On_Component_Order then
12185 Component_Order_Check (Rectype);
12186 end if;
12187 end if;
12188
12189 -- For records that have component clauses for all components, and whose
12190 -- size is less than or equal to 32, and which can be fully packed, we
12191 -- need to know the size in the front end to activate possible packed
12192 -- array processing where the component type is a record.
12193
12194 -- At this stage Hbit + 1 represents the first unused bit from all the
12195 -- component clauses processed, so if the component clauses are
12196 -- complete, then this is the length of the record.
12197
12198 -- For records longer than System.Storage_Unit, and for those where not
12199 -- all components have component clauses, the back end determines the
12200 -- length (it may for example be appropriate to round up the size
12201 -- to some convenient boundary, based on alignment considerations, etc).
12202
12203 if Unknown_RM_Size (Rectype)
12204 and then Hbit + 1 <= 32
12205 and then not Strict_Alignment (Rectype)
12206 then
12207
12208 -- Nothing to do if at least one component has no component clause
12209
12210 Comp := First_Component_Or_Discriminant (Rectype);
12211 while Present (Comp) loop
12212 exit when No (Component_Clause (Comp));
12213 Next_Component_Or_Discriminant (Comp);
12214 end loop;
12215
12216 -- If we fall out of loop, all components have component clauses
12217 -- and so we can set the size to the maximum value.
12218
12219 if No (Comp) then
12220 Set_RM_Size (Rectype, Hbit + 1);
12221 end if;
12222 end if;
12223 end Check_Record_Representation_Clause;
12224
12225 ----------------
12226 -- Check_Size --
12227 ----------------
12228
12229 procedure Check_Size
12230 (N : Node_Id;
12231 T : Entity_Id;
12232 Siz : Uint;
12233 Biased : out Boolean)
12234 is
12235 procedure Size_Too_Small_Error (Min_Siz : Uint);
12236 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
12237 -- minimum size.
12238
12239 --------------------------
12240 -- Size_Too_Small_Error --
12241 --------------------------
12242
12243 procedure Size_Too_Small_Error (Min_Siz : Uint) is
12244 begin
12245 Error_Msg_Uint_1 := Min_Siz;
12246 Error_Msg_NE (Size_Too_Small_Message, N, T);
12247 end Size_Too_Small_Error;
12248
12249 -- Local variables
12250
12251 UT : constant Entity_Id := Underlying_Type (T);
12252 M : Uint;
12253
12254 -- Start of processing for Check_Size
12255
12256 begin
12257 Biased := False;
12258
12259 -- Reject patently improper size values
12260
12261 if Is_Elementary_Type (T)
12262 and then Siz > UI_From_Int (Int'Last)
12263 then
12264 Error_Msg_N ("Size value too large for elementary type", N);
12265
12266 if Nkind (Original_Node (N)) = N_Op_Expon then
12267 Error_Msg_N
12268 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
12269 end if;
12270 end if;
12271
12272 -- Dismiss generic types
12273
12274 if Is_Generic_Type (T)
12275 or else
12276 Is_Generic_Type (UT)
12277 or else
12278 Is_Generic_Type (Root_Type (UT))
12279 then
12280 return;
12281
12282 -- Guard against previous errors
12283
12284 elsif No (UT) or else UT = Any_Type then
12285 Check_Error_Detected;
12286 return;
12287
12288 -- Check case of bit packed array
12289
12290 elsif Is_Array_Type (UT)
12291 and then Known_Static_Component_Size (UT)
12292 and then Is_Bit_Packed_Array (UT)
12293 then
12294 declare
12295 Asiz : Uint;
12296 Indx : Node_Id;
12297 Ityp : Entity_Id;
12298
12299 begin
12300 Asiz := Component_Size (UT);
12301 Indx := First_Index (UT);
12302 loop
12303 Ityp := Etype (Indx);
12304
12305 -- If non-static bound, then we are not in the business of
12306 -- trying to check the length, and indeed an error will be
12307 -- issued elsewhere, since sizes of non-static array types
12308 -- cannot be set implicitly or explicitly.
12309
12310 if not Is_OK_Static_Subtype (Ityp) then
12311 return;
12312 end if;
12313
12314 -- Otherwise accumulate next dimension
12315
12316 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
12317 Expr_Value (Type_Low_Bound (Ityp)) +
12318 Uint_1);
12319
12320 Next_Index (Indx);
12321 exit when No (Indx);
12322 end loop;
12323
12324 if Asiz <= Siz then
12325 return;
12326
12327 else
12328 Size_Too_Small_Error (Asiz);
12329 Set_Esize (T, Asiz);
12330 Set_RM_Size (T, Asiz);
12331 end if;
12332 end;
12333
12334 -- All other composite types are ignored
12335
12336 elsif Is_Composite_Type (UT) then
12337 return;
12338
12339 -- For fixed-point types, don't check minimum if type is not frozen,
12340 -- since we don't know all the characteristics of the type that can
12341 -- affect the size (e.g. a specified small) till freeze time.
12342
12343 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
12344 null;
12345
12346 -- Cases for which a minimum check is required
12347
12348 else
12349 -- Ignore if specified size is correct for the type
12350
12351 if Known_Esize (UT) and then Siz = Esize (UT) then
12352 return;
12353 end if;
12354
12355 -- Otherwise get minimum size
12356
12357 M := UI_From_Int (Minimum_Size (UT));
12358
12359 if Siz < M then
12360
12361 -- Size is less than minimum size, but one possibility remains
12362 -- that we can manage with the new size if we bias the type.
12363
12364 M := UI_From_Int (Minimum_Size (UT, Biased => True));
12365
12366 if Siz < M then
12367 Size_Too_Small_Error (M);
12368 Set_Esize (T, M);
12369 Set_RM_Size (T, M);
12370 else
12371 Biased := True;
12372 end if;
12373 end if;
12374 end if;
12375 end Check_Size;
12376
12377 --------------------------
12378 -- Freeze_Entity_Checks --
12379 --------------------------
12380
12381 procedure Freeze_Entity_Checks (N : Node_Id) is
12382 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
12383 -- Inspect the primitive operations of type Typ and hide all pairs of
12384 -- implicitly declared non-overridden non-fully conformant homographs
12385 -- (Ada RM 8.3 12.3/2).
12386
12387 -------------------------------------
12388 -- Hide_Non_Overridden_Subprograms --
12389 -------------------------------------
12390
12391 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
12392 procedure Hide_Matching_Homographs
12393 (Subp_Id : Entity_Id;
12394 Start_Elmt : Elmt_Id);
12395 -- Inspect a list of primitive operations starting with Start_Elmt
12396 -- and find matching implicitly declared non-overridden non-fully
12397 -- conformant homographs of Subp_Id. If found, all matches along
12398 -- with Subp_Id are hidden from all visibility.
12399
12400 function Is_Non_Overridden_Or_Null_Procedure
12401 (Subp_Id : Entity_Id) return Boolean;
12402 -- Determine whether subprogram Subp_Id is implicitly declared non-
12403 -- overridden subprogram or an implicitly declared null procedure.
12404
12405 ------------------------------
12406 -- Hide_Matching_Homographs --
12407 ------------------------------
12408
12409 procedure Hide_Matching_Homographs
12410 (Subp_Id : Entity_Id;
12411 Start_Elmt : Elmt_Id)
12412 is
12413 Prim : Entity_Id;
12414 Prim_Elmt : Elmt_Id;
12415
12416 begin
12417 Prim_Elmt := Start_Elmt;
12418 while Present (Prim_Elmt) loop
12419 Prim := Node (Prim_Elmt);
12420
12421 -- The current primitive is implicitly declared non-overridden
12422 -- non-fully conformant homograph of Subp_Id. Both subprograms
12423 -- must be hidden from visibility.
12424
12425 if Chars (Prim) = Chars (Subp_Id)
12426 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
12427 and then not Fully_Conformant (Prim, Subp_Id)
12428 then
12429 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
12430 Set_Is_Immediately_Visible (Prim, False);
12431 Set_Is_Potentially_Use_Visible (Prim, False);
12432
12433 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
12434 Set_Is_Immediately_Visible (Subp_Id, False);
12435 Set_Is_Potentially_Use_Visible (Subp_Id, False);
12436 end if;
12437
12438 Next_Elmt (Prim_Elmt);
12439 end loop;
12440 end Hide_Matching_Homographs;
12441
12442 -----------------------------------------
12443 -- Is_Non_Overridden_Or_Null_Procedure --
12444 -----------------------------------------
12445
12446 function Is_Non_Overridden_Or_Null_Procedure
12447 (Subp_Id : Entity_Id) return Boolean
12448 is
12449 Alias_Id : Entity_Id;
12450
12451 begin
12452 -- The subprogram is inherited (implicitly declared), it does not
12453 -- override and does not cover a primitive of an interface.
12454
12455 if Ekind (Subp_Id) in E_Function | E_Procedure
12456 and then Present (Alias (Subp_Id))
12457 and then No (Interface_Alias (Subp_Id))
12458 and then No (Overridden_Operation (Subp_Id))
12459 then
12460 Alias_Id := Alias (Subp_Id);
12461
12462 if Requires_Overriding (Alias_Id) then
12463 return True;
12464
12465 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
12466 and then Null_Present (Parent (Alias_Id))
12467 then
12468 return True;
12469 end if;
12470 end if;
12471
12472 return False;
12473 end Is_Non_Overridden_Or_Null_Procedure;
12474
12475 -- Local variables
12476
12477 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
12478 Prim : Entity_Id;
12479 Prim_Elmt : Elmt_Id;
12480
12481 -- Start of processing for Hide_Non_Overridden_Subprograms
12482
12483 begin
12484 -- Inspect the list of primitives looking for non-overridden
12485 -- subprograms.
12486
12487 if Present (Prim_Ops) then
12488 Prim_Elmt := First_Elmt (Prim_Ops);
12489 while Present (Prim_Elmt) loop
12490 Prim := Node (Prim_Elmt);
12491 Next_Elmt (Prim_Elmt);
12492
12493 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
12494 Hide_Matching_Homographs
12495 (Subp_Id => Prim,
12496 Start_Elmt => Prim_Elmt);
12497 end if;
12498 end loop;
12499 end if;
12500 end Hide_Non_Overridden_Subprograms;
12501
12502 -- Local variables
12503
12504 E : constant Entity_Id := Entity (N);
12505
12506 Nongeneric_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
12507 -- True in nongeneric case. Some of the processing here is skipped
12508 -- for the generic case since it is not needed. Basically in the
12509 -- generic case, we only need to do stuff that might generate error
12510 -- messages or warnings.
12511
12512 -- Start of processing for Freeze_Entity_Checks
12513
12514 begin
12515 -- Remember that we are processing a freezing entity. Required to
12516 -- ensure correct decoration of internal entities associated with
12517 -- interfaces (see New_Overloaded_Entity).
12518
12519 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
12520
12521 -- For tagged types covering interfaces add internal entities that link
12522 -- the primitives of the interfaces with the primitives that cover them.
12523 -- Note: These entities were originally generated only when generating
12524 -- code because their main purpose was to provide support to initialize
12525 -- the secondary dispatch tables. They are also used to locate
12526 -- primitives covering interfaces when processing generics (see
12527 -- Derive_Subprograms).
12528
12529 -- This is not needed in the generic case
12530
12531 if Ada_Version >= Ada_2005
12532 and then Nongeneric_Case
12533 and then Ekind (E) = E_Record_Type
12534 and then Is_Tagged_Type (E)
12535 and then not Is_Interface (E)
12536 and then Has_Interfaces (E)
12537 then
12538 -- This would be a good common place to call the routine that checks
12539 -- overriding of interface primitives (and thus factorize calls to
12540 -- Check_Abstract_Overriding located at different contexts in the
12541 -- compiler). However, this is not possible because it causes
12542 -- spurious errors in case of late overriding.
12543
12544 Add_Internal_Interface_Entities (E);
12545 end if;
12546
12547 -- After all forms of overriding have been resolved, a tagged type may
12548 -- be left with a set of implicitly declared and possibly erroneous
12549 -- abstract subprograms, null procedures and subprograms that require
12550 -- overriding. If this set contains fully conformant homographs, then
12551 -- one is chosen arbitrarily (already done during resolution), otherwise
12552 -- all remaining non-fully conformant homographs are hidden from
12553 -- visibility (Ada RM 8.3 12.3/2).
12554
12555 if Is_Tagged_Type (E) then
12556 Hide_Non_Overridden_Subprograms (E);
12557 end if;
12558
12559 -- Check CPP types
12560
12561 if Ekind (E) = E_Record_Type
12562 and then Is_CPP_Class (E)
12563 and then Is_Tagged_Type (E)
12564 and then Tagged_Type_Expansion
12565 then
12566 if CPP_Num_Prims (E) = 0 then
12567
12568 -- If the CPP type has user defined components then it must import
12569 -- primitives from C++. This is required because if the C++ class
12570 -- has no primitives then the C++ compiler does not added the _tag
12571 -- component to the type.
12572
12573 if First_Entity (E) /= Last_Entity (E) then
12574 Error_Msg_N
12575 ("'C'P'P type must import at least one primitive from C++??",
12576 E);
12577 end if;
12578 end if;
12579
12580 -- Check that all its primitives are abstract or imported from C++.
12581 -- Check also availability of the C++ constructor.
12582
12583 declare
12584 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
12585 Elmt : Elmt_Id;
12586 Error_Reported : Boolean := False;
12587 Prim : Node_Id;
12588
12589 begin
12590 Elmt := First_Elmt (Primitive_Operations (E));
12591 while Present (Elmt) loop
12592 Prim := Node (Elmt);
12593
12594 if Comes_From_Source (Prim) then
12595 if Is_Abstract_Subprogram (Prim) then
12596 null;
12597
12598 elsif not Is_Imported (Prim)
12599 or else Convention (Prim) /= Convention_CPP
12600 then
12601 Error_Msg_N
12602 ("primitives of 'C'P'P types must be imported from C++ "
12603 & "or abstract??", Prim);
12604
12605 elsif not Has_Constructors
12606 and then not Error_Reported
12607 then
12608 Error_Msg_Name_1 := Chars (E);
12609 Error_Msg_N
12610 ("??'C'P'P constructor required for type %", Prim);
12611 Error_Reported := True;
12612 end if;
12613 end if;
12614
12615 Next_Elmt (Elmt);
12616 end loop;
12617 end;
12618 end if;
12619
12620 -- Check Ada derivation of CPP type
12621
12622 if Expander_Active -- why? losing errors in -gnatc mode???
12623 and then Present (Etype (E)) -- defend against errors
12624 and then Tagged_Type_Expansion
12625 and then Ekind (E) = E_Record_Type
12626 and then Etype (E) /= E
12627 and then Is_CPP_Class (Etype (E))
12628 and then CPP_Num_Prims (Etype (E)) > 0
12629 and then not Is_CPP_Class (E)
12630 and then not Has_CPP_Constructors (Etype (E))
12631 then
12632 -- If the parent has C++ primitives but it has no constructor then
12633 -- check that all the primitives are overridden in this derivation;
12634 -- otherwise the constructor of the parent is needed to build the
12635 -- dispatch table.
12636
12637 declare
12638 Elmt : Elmt_Id;
12639 Prim : Node_Id;
12640
12641 begin
12642 Elmt := First_Elmt (Primitive_Operations (E));
12643 while Present (Elmt) loop
12644 Prim := Node (Elmt);
12645
12646 if not Is_Abstract_Subprogram (Prim)
12647 and then No (Interface_Alias (Prim))
12648 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
12649 then
12650 Error_Msg_Name_1 := Chars (Etype (E));
12651 Error_Msg_N
12652 ("'C'P'P constructor required for parent type %", E);
12653 exit;
12654 end if;
12655
12656 Next_Elmt (Elmt);
12657 end loop;
12658 end;
12659 end if;
12660
12661 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
12662
12663 -- If we have a type with predicates, build predicate function. This is
12664 -- not needed in the generic case, nor within e.g. TSS subprograms and
12665 -- other predefined primitives. For a derived type, ensure that the
12666 -- parent type is already frozen so that its predicate function has been
12667 -- constructed already. This is necessary if the parent is declared
12668 -- in a nested package and its own freeze point has not been reached.
12669
12670 if Is_Type (E)
12671 and then Nongeneric_Case
12672 and then Has_Predicates (E)
12673 and then Predicate_Check_In_Scope (N)
12674 then
12675 declare
12676 Atyp : constant Entity_Id := Nearest_Ancestor (E);
12677 begin
12678 if Present (Atyp)
12679 and then Has_Predicates (Atyp)
12680 and then not Is_Frozen (Atyp)
12681 then
12682 Freeze_Before (N, Atyp);
12683 end if;
12684 end;
12685
12686 Build_Predicate_Functions (E, N);
12687 end if;
12688
12689 -- If type has delayed aspects, this is where we do the preanalysis at
12690 -- the freeze point, as part of the consistent visibility check. Note
12691 -- that this must be done after calling Build_Predicate_Functions or
12692 -- Build_Invariant_Procedure since these subprograms fix occurrences of
12693 -- the subtype name in the saved expression so that they will not cause
12694 -- trouble in the preanalysis.
12695
12696 -- This is also not needed in the generic case
12697
12698 if Nongeneric_Case
12699 and then Has_Delayed_Aspects (E)
12700 and then Scope (E) = Current_Scope
12701 then
12702 declare
12703 A_Id : Aspect_Id;
12704 Ritem : Node_Id;
12705
12706 begin
12707 -- Look for aspect specification entries for this entity
12708
12709 Ritem := First_Rep_Item (E);
12710 while Present (Ritem) loop
12711 if Nkind (Ritem) = N_Aspect_Specification
12712 and then Entity (Ritem) = E
12713 and then Is_Delayed_Aspect (Ritem)
12714 then
12715 A_Id := Get_Aspect_Id (Ritem);
12716
12717 if A_Id = Aspect_Dynamic_Predicate
12718 or else A_Id = Aspect_Predicate
12719 or else A_Id = Aspect_Priority
12720 or else A_Id = Aspect_CPU
12721 then
12722 -- Retrieve the visibility to components and discriminants
12723 -- in order to properly analyze the aspects.
12724
12725 Push_Type (E);
12726 Check_Aspect_At_Freeze_Point (Ritem);
12727 Pop_Type (E);
12728
12729 else
12730 Check_Aspect_At_Freeze_Point (Ritem);
12731 end if;
12732 end if;
12733
12734 Next_Rep_Item (Ritem);
12735 end loop;
12736 end;
12737
12738 end if;
12739
12740 -- For a record type, deal with variant parts. This has to be delayed to
12741 -- this point, because of the issue of statically predicated subtypes,
12742 -- which we have to ensure are frozen before checking choices, since we
12743 -- need to have the static choice list set.
12744
12745 if Is_Record_Type (E) then
12746 Check_Variant_Part : declare
12747 D : constant Node_Id := Declaration_Node (E);
12748 T : Node_Id;
12749 C : Node_Id;
12750 VP : Node_Id;
12751
12752 Others_Present : Boolean;
12753 pragma Warnings (Off, Others_Present);
12754 -- Indicates others present, not used in this case
12755
12756 procedure Non_Static_Choice_Error (Choice : Node_Id);
12757 -- Error routine invoked by the generic instantiation below when
12758 -- the variant part has a non static choice.
12759
12760 procedure Process_Declarations (Variant : Node_Id);
12761 -- Processes declarations associated with a variant. We analyzed
12762 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
12763 -- but we still need the recursive call to Check_Choices for any
12764 -- nested variant to get its choices properly processed. This is
12765 -- also where we expand out the choices if expansion is active.
12766
12767 package Variant_Choices_Processing is new
12768 Generic_Check_Choices
12769 (Process_Empty_Choice => No_OP,
12770 Process_Non_Static_Choice => Non_Static_Choice_Error,
12771 Process_Associated_Node => Process_Declarations);
12772 use Variant_Choices_Processing;
12773
12774 -----------------------------
12775 -- Non_Static_Choice_Error --
12776 -----------------------------
12777
12778 procedure Non_Static_Choice_Error (Choice : Node_Id) is
12779 begin
12780 Flag_Non_Static_Expr
12781 ("choice given in variant part is not static!", Choice);
12782 end Non_Static_Choice_Error;
12783
12784 --------------------------
12785 -- Process_Declarations --
12786 --------------------------
12787
12788 procedure Process_Declarations (Variant : Node_Id) is
12789 CL : constant Node_Id := Component_List (Variant);
12790 VP : Node_Id;
12791
12792 begin
12793 -- Check for static predicate present in this variant
12794
12795 if Has_SP_Choice (Variant) then
12796
12797 -- Here we expand. You might expect to find this call in
12798 -- Expand_N_Variant_Part, but that is called when we first
12799 -- see the variant part, and we cannot do this expansion
12800 -- earlier than the freeze point, since for statically
12801 -- predicated subtypes, the predicate is not known till
12802 -- the freeze point.
12803
12804 -- Furthermore, we do this expansion even if the expander
12805 -- is not active, because other semantic processing, e.g.
12806 -- for aggregates, requires the expanded list of choices.
12807
12808 -- If the expander is not active, then we can't just clobber
12809 -- the list since it would invalidate the tree.
12810 -- So we have to rewrite the variant part with a Rewrite
12811 -- call that replaces it with a copy and clobber the copy.
12812
12813 if not Expander_Active then
12814 declare
12815 NewV : constant Node_Id := New_Copy (Variant);
12816 begin
12817 Set_Discrete_Choices
12818 (NewV, New_Copy_List (Discrete_Choices (Variant)));
12819 Rewrite (Variant, NewV);
12820 end;
12821 end if;
12822
12823 Expand_Static_Predicates_In_Choices (Variant);
12824 end if;
12825
12826 -- We don't need to worry about the declarations in the variant
12827 -- (since they were analyzed by Analyze_Choices when we first
12828 -- encountered the variant), but we do need to take care of
12829 -- expansion of any nested variants.
12830
12831 if not Null_Present (CL) then
12832 VP := Variant_Part (CL);
12833
12834 if Present (VP) then
12835 Check_Choices
12836 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
12837 end if;
12838 end if;
12839 end Process_Declarations;
12840
12841 -- Start of processing for Check_Variant_Part
12842
12843 begin
12844 -- Find component list
12845
12846 C := Empty;
12847
12848 if Nkind (D) = N_Full_Type_Declaration then
12849 T := Type_Definition (D);
12850
12851 if Nkind (T) = N_Record_Definition then
12852 C := Component_List (T);
12853
12854 elsif Nkind (T) = N_Derived_Type_Definition
12855 and then Present (Record_Extension_Part (T))
12856 then
12857 C := Component_List (Record_Extension_Part (T));
12858 end if;
12859 end if;
12860
12861 -- Case of variant part present
12862
12863 if Present (C) and then Present (Variant_Part (C)) then
12864 VP := Variant_Part (C);
12865
12866 -- Check choices
12867
12868 Check_Choices
12869 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
12870
12871 -- If the last variant does not contain the Others choice,
12872 -- replace it with an N_Others_Choice node since Gigi always
12873 -- wants an Others. Note that we do not bother to call Analyze
12874 -- on the modified variant part, since its only effect would be
12875 -- to compute the Others_Discrete_Choices node laboriously, and
12876 -- of course we already know the list of choices corresponding
12877 -- to the others choice (it's the list we're replacing).
12878
12879 -- We only want to do this if the expander is active, since
12880 -- we do not want to clobber the tree.
12881
12882 if Expander_Active then
12883 declare
12884 Last_Var : constant Node_Id :=
12885 Last_Non_Pragma (Variants (VP));
12886
12887 Others_Node : Node_Id;
12888
12889 begin
12890 if Nkind (First (Discrete_Choices (Last_Var))) /=
12891 N_Others_Choice
12892 then
12893 Others_Node := Make_Others_Choice (Sloc (Last_Var));
12894 Set_Others_Discrete_Choices
12895 (Others_Node, Discrete_Choices (Last_Var));
12896 Set_Discrete_Choices
12897 (Last_Var, New_List (Others_Node));
12898 end if;
12899 end;
12900 end if;
12901 end if;
12902 end Check_Variant_Part;
12903 end if;
12904 end Freeze_Entity_Checks;
12905
12906 -------------------------
12907 -- Get_Alignment_Value --
12908 -------------------------
12909
12910 function Get_Alignment_Value (Expr : Node_Id) return Uint is
12911 Align : constant Uint := Static_Integer (Expr);
12912
12913 begin
12914 if Align = No_Uint then
12915 return No_Uint;
12916
12917 elsif Align < 0 then
12918 Error_Msg_N ("alignment value must be positive", Expr);
12919 return No_Uint;
12920
12921 -- If Alignment is specified to be 0, we treat it the same as 1
12922
12923 elsif Align = 0 then
12924 return Uint_1;
12925
12926 else
12927 for J in Int range 0 .. 64 loop
12928 declare
12929 M : constant Uint := Uint_2 ** J;
12930
12931 begin
12932 exit when M = Align;
12933
12934 if M > Align then
12935 Error_Msg_N ("alignment value must be power of 2", Expr);
12936 return No_Uint;
12937 end if;
12938 end;
12939 end loop;
12940
12941 return Align;
12942 end if;
12943 end Get_Alignment_Value;
12944
12945 -----------------------------------
12946 -- Has_Compatible_Representation --
12947 -----------------------------------
12948
12949 function Has_Compatible_Representation
12950 (Target_Type, Operand_Type : Entity_Id) return Boolean
12951 is
12952 T1 : constant Entity_Id := Underlying_Type (Target_Type);
12953 T2 : constant Entity_Id := Underlying_Type (Operand_Type);
12954
12955 begin
12956 -- A quick check, if base types are the same, then we definitely have
12957 -- the same representation, because the subtype specific representation
12958 -- attributes (Size and Alignment) do not affect representation from
12959 -- the point of view of this test.
12960
12961 if Base_Type (T1) = Base_Type (T2) then
12962 return True;
12963
12964 elsif Is_Private_Type (Base_Type (T2))
12965 and then Base_Type (T1) = Full_View (Base_Type (T2))
12966 then
12967 return True;
12968
12969 -- If T2 is a generic actual it is declared as a subtype, so
12970 -- check against its base type.
12971
12972 elsif Is_Generic_Actual_Type (T1)
12973 and then Has_Compatible_Representation (Base_Type (T1), T2)
12974 then
12975 return True;
12976 end if;
12977
12978 -- Tagged types always have the same representation, because it is not
12979 -- possible to specify different representations for common fields.
12980
12981 if Is_Tagged_Type (T1) then
12982 return True;
12983 end if;
12984
12985 -- Representations are definitely different if conventions differ
12986
12987 if Convention (T1) /= Convention (T2) then
12988 return False;
12989 end if;
12990
12991 -- Representations are different if component alignments or scalar
12992 -- storage orders differ.
12993
12994 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
12995 and then
12996 (Is_Record_Type (T2) or else Is_Array_Type (T2))
12997 and then
12998 (Component_Alignment (T1) /= Component_Alignment (T2)
12999 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
13000 then
13001 return False;
13002 end if;
13003
13004 -- For arrays, the only real issue is component size. If we know the
13005 -- component size for both arrays, and it is the same, then that's
13006 -- good enough to know we don't have a change of representation.
13007
13008 if Is_Array_Type (T1) then
13009
13010 -- In a view conversion, if the target type is an array type having
13011 -- aliased components and the operand type is an array type having
13012 -- unaliased components, then a new object is created (4.6(58.3/4)).
13013
13014 if Has_Aliased_Components (T1)
13015 and then not Has_Aliased_Components (T2)
13016 then
13017 return False;
13018 end if;
13019
13020 if Known_Component_Size (T1)
13021 and then Known_Component_Size (T2)
13022 and then Component_Size (T1) = Component_Size (T2)
13023 then
13024 return True;
13025 end if;
13026 end if;
13027
13028 -- For records, representations are different if reorderings differ
13029
13030 if Is_Record_Type (T1)
13031 and then Is_Record_Type (T2)
13032 and then No_Reordering (T1) /= No_Reordering (T2)
13033 then
13034 return False;
13035 end if;
13036
13037 -- Types definitely have same representation if neither has non-standard
13038 -- representation since default representations are always consistent.
13039 -- If only one has non-standard representation, and the other does not,
13040 -- then we consider that they do not have the same representation. They
13041 -- might, but there is no way of telling early enough.
13042
13043 if Has_Non_Standard_Rep (T1) then
13044 if not Has_Non_Standard_Rep (T2) then
13045 return False;
13046 end if;
13047 else
13048 return not Has_Non_Standard_Rep (T2);
13049 end if;
13050
13051 -- Here the two types both have non-standard representation, and we need
13052 -- to determine if they have the same non-standard representation.
13053
13054 -- For arrays, we simply need to test if the component sizes are the
13055 -- same. Pragma Pack is reflected in modified component sizes, so this
13056 -- check also deals with pragma Pack.
13057
13058 if Is_Array_Type (T1) then
13059 return Component_Size (T1) = Component_Size (T2);
13060
13061 -- Case of record types
13062
13063 elsif Is_Record_Type (T1) then
13064
13065 -- Packed status must conform
13066
13067 if Is_Packed (T1) /= Is_Packed (T2) then
13068 return False;
13069
13070 -- Otherwise we must check components. Typ2 maybe a constrained
13071 -- subtype with fewer components, so we compare the components
13072 -- of the base types.
13073
13074 else
13075 Record_Case : declare
13076 CD1, CD2 : Entity_Id;
13077
13078 function Same_Rep return Boolean;
13079 -- CD1 and CD2 are either components or discriminants. This
13080 -- function tests whether they have the same representation.
13081
13082 --------------
13083 -- Same_Rep --
13084 --------------
13085
13086 function Same_Rep return Boolean is
13087 begin
13088 if No (Component_Clause (CD1)) then
13089 return No (Component_Clause (CD2));
13090 else
13091 -- Note: at this point, component clauses have been
13092 -- normalized to the default bit order, so that the
13093 -- comparison of Component_Bit_Offsets is meaningful.
13094
13095 return
13096 Present (Component_Clause (CD2))
13097 and then
13098 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
13099 and then
13100 Esize (CD1) = Esize (CD2);
13101 end if;
13102 end Same_Rep;
13103
13104 -- Start of processing for Record_Case
13105
13106 begin
13107 if Has_Discriminants (T1) then
13108
13109 -- The number of discriminants may be different if the
13110 -- derived type has fewer (constrained by values). The
13111 -- invisible discriminants retain the representation of
13112 -- the original, so the discrepancy does not per se
13113 -- indicate a different representation.
13114
13115 CD1 := First_Discriminant (T1);
13116 CD2 := First_Discriminant (T2);
13117 while Present (CD1) and then Present (CD2) loop
13118 if not Same_Rep then
13119 return False;
13120 else
13121 Next_Discriminant (CD1);
13122 Next_Discriminant (CD2);
13123 end if;
13124 end loop;
13125 end if;
13126
13127 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
13128 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
13129 while Present (CD1) loop
13130 if not Same_Rep then
13131 return False;
13132 else
13133 Next_Component (CD1);
13134 Next_Component (CD2);
13135 end if;
13136 end loop;
13137
13138 return True;
13139 end Record_Case;
13140 end if;
13141
13142 -- For enumeration types, we must check each literal to see if the
13143 -- representation is the same. Note that we do not permit enumeration
13144 -- representation clauses for Character and Wide_Character, so these
13145 -- cases were already dealt with.
13146
13147 elsif Is_Enumeration_Type (T1) then
13148 Enumeration_Case : declare
13149 L1, L2 : Entity_Id;
13150
13151 begin
13152 L1 := First_Literal (T1);
13153 L2 := First_Literal (T2);
13154 while Present (L1) loop
13155 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
13156 return False;
13157 else
13158 Next_Literal (L1);
13159 Next_Literal (L2);
13160 end if;
13161 end loop;
13162
13163 return True;
13164 end Enumeration_Case;
13165
13166 -- Any other types have the same representation for these purposes
13167
13168 else
13169 return True;
13170 end if;
13171 end Has_Compatible_Representation;
13172
13173 -------------------------------------
13174 -- Inherit_Aspects_At_Freeze_Point --
13175 -------------------------------------
13176
13177 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
13178 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13179 (Rep_Item : Node_Id) return Boolean;
13180 -- This routine checks if Rep_Item is either a pragma or an aspect
13181 -- specification node whose correponding pragma (if any) is present in
13182 -- the Rep Item chain of the entity it has been specified to.
13183
13184 --------------------------------------------------
13185 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
13186 --------------------------------------------------
13187
13188 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13189 (Rep_Item : Node_Id) return Boolean
13190 is
13191 begin
13192 return
13193 Nkind (Rep_Item) = N_Pragma
13194 or else
13195 Present_In_Rep_Item (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
13196 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
13197
13198 -- Start of processing for Inherit_Aspects_At_Freeze_Point
13199
13200 begin
13201 -- A representation item is either subtype-specific (Size and Alignment
13202 -- clauses) or type-related (all others). Subtype-specific aspects may
13203 -- differ for different subtypes of the same type (RM 13.1.8).
13204
13205 -- A derived type inherits each type-related representation aspect of
13206 -- its parent type that was directly specified before the declaration of
13207 -- the derived type (RM 13.1.15).
13208
13209 -- A derived subtype inherits each subtype-specific representation
13210 -- aspect of its parent subtype that was directly specified before the
13211 -- declaration of the derived type (RM 13.1.15).
13212
13213 -- The general processing involves inheriting a representation aspect
13214 -- from a parent type whenever the first rep item (aspect specification,
13215 -- attribute definition clause, pragma) corresponding to the given
13216 -- representation aspect in the rep item chain of Typ, if any, isn't
13217 -- directly specified to Typ but to one of its parents.
13218
13219 -- ??? Note that, for now, just a limited number of representation
13220 -- aspects have been inherited here so far. Many of them are
13221 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
13222 -- a non- exhaustive list of aspects that likely also need to
13223 -- be moved to this routine: Alignment, Component_Alignment,
13224 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
13225 -- Preelaborable_Initialization, RM_Size and Small.
13226
13227 -- In addition, Convention must be propagated from base type to subtype,
13228 -- because the subtype may have been declared on an incomplete view.
13229
13230 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
13231 return;
13232 end if;
13233
13234 -- Ada_05/Ada_2005
13235
13236 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
13237 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
13238 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13239 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
13240 then
13241 Set_Is_Ada_2005_Only (Typ);
13242 end if;
13243
13244 -- Ada_12/Ada_2012
13245
13246 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
13247 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
13248 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13249 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
13250 then
13251 Set_Is_Ada_2012_Only (Typ);
13252 end if;
13253
13254 -- Atomic/Shared
13255
13256 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
13257 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
13258 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13259 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
13260 then
13261 Set_Is_Atomic (Typ);
13262 Set_Is_Volatile (Typ);
13263 Set_Treat_As_Volatile (Typ);
13264 end if;
13265
13266 -- Convention
13267
13268 if Is_Record_Type (Typ)
13269 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
13270 then
13271 Set_Convention (Typ, Convention (Base_Type (Typ)));
13272 end if;
13273
13274 -- Default_Component_Value
13275
13276 -- Verify that there is no rep_item declared for the type, and there
13277 -- is one coming from an ancestor.
13278
13279 if Is_Array_Type (Typ)
13280 and then Is_Base_Type (Typ)
13281 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
13282 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
13283 then
13284 Set_Default_Aspect_Component_Value (Typ,
13285 Default_Aspect_Component_Value
13286 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
13287 end if;
13288
13289 -- Default_Value
13290
13291 if Is_Scalar_Type (Typ)
13292 and then Is_Base_Type (Typ)
13293 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
13294 and then Has_Rep_Item (Typ, Name_Default_Value)
13295 then
13296 Set_Has_Default_Aspect (Typ);
13297 Set_Default_Aspect_Value (Typ,
13298 Default_Aspect_Value
13299 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
13300 end if;
13301
13302 -- Discard_Names
13303
13304 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
13305 and then Has_Rep_Item (Typ, Name_Discard_Names)
13306 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13307 (Get_Rep_Item (Typ, Name_Discard_Names))
13308 then
13309 Set_Discard_Names (Typ);
13310 end if;
13311
13312 -- Volatile
13313
13314 if not Has_Rep_Item (Typ, Name_Volatile, False)
13315 and then Has_Rep_Item (Typ, Name_Volatile)
13316 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13317 (Get_Rep_Item (Typ, Name_Volatile))
13318 then
13319 Set_Is_Volatile (Typ);
13320 Set_Treat_As_Volatile (Typ);
13321 end if;
13322
13323 -- Volatile_Full_Access and Full_Access_Only
13324
13325 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
13326 and then not Has_Rep_Item (Typ, Name_Full_Access_Only, False)
13327 and then (Has_Rep_Item (Typ, Name_Volatile_Full_Access)
13328 or else Has_Rep_Item (Typ, Name_Full_Access_Only))
13329 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13330 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
13331 then
13332 Set_Is_Volatile_Full_Access (Typ);
13333 Set_Is_Volatile (Typ);
13334 Set_Treat_As_Volatile (Typ);
13335 end if;
13336
13337 -- Inheritance for derived types only
13338
13339 if Is_Derived_Type (Typ) then
13340 declare
13341 Bas_Typ : constant Entity_Id := Base_Type (Typ);
13342 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
13343
13344 begin
13345 -- Atomic_Components
13346
13347 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
13348 and then Has_Rep_Item (Typ, Name_Atomic_Components)
13349 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13350 (Get_Rep_Item (Typ, Name_Atomic_Components))
13351 then
13352 Set_Has_Atomic_Components (Imp_Bas_Typ);
13353 end if;
13354
13355 -- Volatile_Components
13356
13357 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
13358 and then Has_Rep_Item (Typ, Name_Volatile_Components)
13359 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13360 (Get_Rep_Item (Typ, Name_Volatile_Components))
13361 then
13362 Set_Has_Volatile_Components (Imp_Bas_Typ);
13363 end if;
13364
13365 -- Finalize_Storage_Only
13366
13367 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
13368 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
13369 then
13370 Set_Finalize_Storage_Only (Bas_Typ);
13371 end if;
13372
13373 -- Universal_Aliasing
13374
13375 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
13376 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
13377 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13378 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
13379 then
13380 Set_Universal_Aliasing (Imp_Bas_Typ);
13381 end if;
13382
13383 -- Bit_Order
13384
13385 if Is_Record_Type (Typ) and then Typ = Bas_Typ then
13386 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
13387 and then Has_Rep_Item (Typ, Name_Bit_Order)
13388 then
13389 Set_Reverse_Bit_Order (Bas_Typ,
13390 Reverse_Bit_Order
13391 (Implementation_Base_Type (Etype (Bas_Typ))));
13392 end if;
13393 end if;
13394
13395 -- Scalar_Storage_Order
13396
13397 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
13398 and then Typ = Bas_Typ
13399 then
13400 -- For a type extension, always inherit from parent; otherwise
13401 -- inherit if no default applies. Note: we do not check for
13402 -- an explicit rep item on the parent type when inheriting,
13403 -- because the parent SSO may itself have been set by default.
13404
13405 if not Has_Rep_Item (First_Subtype (Typ),
13406 Name_Scalar_Storage_Order, False)
13407 and then (Is_Tagged_Type (Bas_Typ)
13408 or else not (SSO_Set_Low_By_Default (Bas_Typ)
13409 or else
13410 SSO_Set_High_By_Default (Bas_Typ)))
13411 then
13412 Set_Reverse_Storage_Order (Bas_Typ,
13413 Reverse_Storage_Order
13414 (Implementation_Base_Type (Etype (Bas_Typ))));
13415
13416 -- Clear default SSO indications, since the inherited aspect
13417 -- which was set explicitly overrides the default.
13418
13419 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
13420 Set_SSO_Set_High_By_Default (Bas_Typ, False);
13421 end if;
13422 end if;
13423 end;
13424 end if;
13425 end Inherit_Aspects_At_Freeze_Point;
13426
13427 ----------------
13428 -- Initialize --
13429 ----------------
13430
13431 procedure Initialize is
13432 begin
13433 Address_Clause_Checks.Init;
13434 Unchecked_Conversions.Init;
13435
13436 -- ??? Might be needed in the future for some non GCC back-ends
13437 -- if AAMP_On_Target then
13438 -- Independence_Checks.Init;
13439 -- end if;
13440 end Initialize;
13441
13442 ---------------------------
13443 -- Install_Discriminants --
13444 ---------------------------
13445
13446 procedure Install_Discriminants (E : Entity_Id) is
13447 Disc : Entity_Id;
13448 Prev : Entity_Id;
13449 begin
13450 Disc := First_Discriminant (E);
13451 while Present (Disc) loop
13452 Prev := Current_Entity (Disc);
13453 Set_Current_Entity (Disc);
13454 Set_Is_Immediately_Visible (Disc);
13455 Set_Homonym (Disc, Prev);
13456 Next_Discriminant (Disc);
13457 end loop;
13458 end Install_Discriminants;
13459
13460 -------------------------
13461 -- Is_Operational_Item --
13462 -------------------------
13463
13464 function Is_Operational_Item (N : Node_Id) return Boolean is
13465 begin
13466 if Nkind (N) /= N_Attribute_Definition_Clause then
13467 return False;
13468
13469 else
13470 declare
13471 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
13472 begin
13473
13474 -- List of operational items is given in AARM 13.1(8.mm/1).
13475 -- It is clearly incomplete, as it does not include iterator
13476 -- aspects, among others.
13477
13478 return Id = Attribute_Constant_Indexing
13479 or else Id = Attribute_Default_Iterator
13480 or else Id = Attribute_Implicit_Dereference
13481 or else Id = Attribute_Input
13482 or else Id = Attribute_Iterator_Element
13483 or else Id = Attribute_Iterable
13484 or else Id = Attribute_Output
13485 or else Id = Attribute_Read
13486 or else Id = Attribute_Variable_Indexing
13487 or else Id = Attribute_Write
13488 or else Id = Attribute_External_Tag;
13489 end;
13490 end if;
13491 end Is_Operational_Item;
13492
13493 -------------------------
13494 -- Is_Predicate_Static --
13495 -------------------------
13496
13497 -- Note: the basic legality of the expression has already been checked, so
13498 -- we don't need to worry about cases or ranges on strings for example.
13499
13500 function Is_Predicate_Static
13501 (Expr : Node_Id;
13502 Nam : Name_Id) return Boolean
13503 is
13504 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
13505 -- Given a list of case expression alternatives, returns True if all
13506 -- the alternatives are static (have all static choices, and a static
13507 -- expression).
13508
13509 function Is_Type_Ref (N : Node_Id) return Boolean;
13510 pragma Inline (Is_Type_Ref);
13511 -- Returns True if N is a reference to the type for the predicate in the
13512 -- expression (i.e. if it is an identifier whose Chars field matches the
13513 -- Nam given in the call). N must not be parenthesized, if the type name
13514 -- appears in parens, this routine will return False.
13515 --
13516 -- The routine also returns True for function calls generated during the
13517 -- expansion of comparison operators on strings, which are intended to
13518 -- be legal in static predicates, and are converted into calls to array
13519 -- comparison routines in the body of the corresponding predicate
13520 -- function.
13521
13522 ----------------------------------
13523 -- All_Static_Case_Alternatives --
13524 ----------------------------------
13525
13526 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
13527 N : Node_Id;
13528
13529 begin
13530 N := First (L);
13531 while Present (N) loop
13532 if not (All_Static_Choices (Discrete_Choices (N))
13533 and then Is_OK_Static_Expression (Expression (N)))
13534 then
13535 return False;
13536 end if;
13537
13538 Next (N);
13539 end loop;
13540
13541 return True;
13542 end All_Static_Case_Alternatives;
13543
13544 -----------------
13545 -- Is_Type_Ref --
13546 -----------------
13547
13548 function Is_Type_Ref (N : Node_Id) return Boolean is
13549 begin
13550 return (Nkind (N) = N_Identifier
13551 and then Chars (N) = Nam
13552 and then Paren_Count (N) = 0)
13553 or else Nkind (N) = N_Function_Call;
13554 end Is_Type_Ref;
13555
13556 -- Start of processing for Is_Predicate_Static
13557
13558 begin
13559 -- Predicate_Static means one of the following holds. Numbers are the
13560 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
13561
13562 -- 16: A static expression
13563
13564 if Is_OK_Static_Expression (Expr) then
13565 return True;
13566
13567 -- 17: A membership test whose simple_expression is the current
13568 -- instance, and whose membership_choice_list meets the requirements
13569 -- for a static membership test.
13570
13571 elsif Nkind (Expr) in N_Membership_Test
13572 and then All_Membership_Choices_Static (Expr)
13573 then
13574 return True;
13575
13576 -- 18. A case_expression whose selecting_expression is the current
13577 -- instance, and whose dependent expressions are static expressions.
13578
13579 elsif Nkind (Expr) = N_Case_Expression
13580 and then Is_Type_Ref (Expression (Expr))
13581 and then All_Static_Case_Alternatives (Alternatives (Expr))
13582 then
13583 return True;
13584
13585 -- 19. A call to a predefined equality or ordering operator, where one
13586 -- operand is the current instance, and the other is a static
13587 -- expression.
13588
13589 -- Note: the RM is clearly wrong here in not excluding string types.
13590 -- Without this exclusion, we would allow expressions like X > "ABC"
13591 -- to be considered as predicate-static, which is clearly not intended,
13592 -- since the idea is for predicate-static to be a subset of normal
13593 -- static expressions (and "DEF" > "ABC" is not a static expression).
13594
13595 -- However, we do allow internally generated (not from source) equality
13596 -- and inequality operations to be valid on strings (this helps deal
13597 -- with cases where we transform A in "ABC" to A = "ABC).
13598
13599 -- In fact, it appears that the intent of the ARG is to extend static
13600 -- predicates to strings, and that the extension should probably apply
13601 -- to static expressions themselves. The code below accepts comparison
13602 -- operators that apply to static strings.
13603
13604 elsif Nkind (Expr) in N_Op_Compare
13605 and then ((Is_Type_Ref (Left_Opnd (Expr))
13606 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
13607 or else
13608 (Is_Type_Ref (Right_Opnd (Expr))
13609 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
13610 then
13611 return True;
13612
13613 -- 20. A call to a predefined boolean logical operator, where each
13614 -- operand is predicate-static.
13615
13616 elsif (Nkind (Expr) in N_Op_And | N_Op_Or | N_Op_Xor
13617 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
13618 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
13619 or else
13620 (Nkind (Expr) = N_Op_Not
13621 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
13622 then
13623 return True;
13624
13625 -- 21. A short-circuit control form where both operands are
13626 -- predicate-static.
13627
13628 elsif Nkind (Expr) in N_Short_Circuit
13629 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
13630 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
13631 then
13632 return True;
13633
13634 -- 22. A parenthesized predicate-static expression. This does not
13635 -- require any special test, since we just ignore paren levels in
13636 -- all the cases above.
13637
13638 -- One more test that is an implementation artifact caused by the fact
13639 -- that we are analyzing not the original expression, but the generated
13640 -- expression in the body of the predicate function. This can include
13641 -- references to inherited predicates, so that the expression we are
13642 -- processing looks like:
13643
13644 -- xxPredicate (typ (Inns)) and then expression
13645
13646 -- Where the call is to a Predicate function for an inherited predicate.
13647 -- We simply ignore such a call, which could be to either a dynamic or
13648 -- a static predicate. Note that if the parent predicate is dynamic then
13649 -- eventually this type will be marked as dynamic, but you are allowed
13650 -- to specify a static predicate for a subtype which is inheriting a
13651 -- dynamic predicate, so the static predicate validation here ignores
13652 -- the inherited predicate even if it is dynamic.
13653 -- In all cases, a static predicate can only apply to a scalar type.
13654
13655 elsif Nkind (Expr) = N_Function_Call
13656 and then Is_Predicate_Function (Entity (Name (Expr)))
13657 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
13658 then
13659 return True;
13660
13661 elsif Is_Entity_Name (Expr)
13662 and then Entity (Expr) = Standard_True
13663 then
13664 Error_Msg_N ("predicate is redundant (always True)?", Expr);
13665 return True;
13666
13667 -- That's an exhaustive list of tests, all other cases are not
13668 -- predicate-static, so we return False.
13669
13670 else
13671 return False;
13672 end if;
13673 end Is_Predicate_Static;
13674
13675 ----------------------
13676 -- Is_Static_Choice --
13677 ----------------------
13678
13679 function Is_Static_Choice (N : Node_Id) return Boolean is
13680 begin
13681 return Nkind (N) = N_Others_Choice
13682 or else Is_OK_Static_Expression (N)
13683 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
13684 and then Is_OK_Static_Subtype (Entity (N)))
13685 or else (Nkind (N) = N_Subtype_Indication
13686 and then Is_OK_Static_Subtype (Entity (N)))
13687 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
13688 end Is_Static_Choice;
13689
13690 ------------------------------
13691 -- Is_Type_Related_Rep_Item --
13692 ------------------------------
13693
13694 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean is
13695 begin
13696 case Nkind (N) is
13697 when N_Attribute_Definition_Clause =>
13698 declare
13699 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
13700 -- See AARM 13.1(8.f-8.x) list items that end in "clause"
13701 -- ???: include any GNAT-defined attributes here?
13702 begin
13703 return Id = Attribute_Component_Size
13704 or else Id = Attribute_Bit_Order
13705 or else Id = Attribute_Storage_Pool
13706 or else Id = Attribute_Stream_Size
13707 or else Id = Attribute_Machine_Radix;
13708 end;
13709
13710 when N_Pragma =>
13711 case Get_Pragma_Id (N) is
13712 -- See AARM 13.1(8.f-8.x) list items that start with "pragma"
13713 -- ???: include any GNAT-defined pragmas here?
13714 when Pragma_Pack
13715 | Pragma_Import
13716 | Pragma_Export
13717 | Pragma_Convention
13718 | Pragma_Atomic
13719 | Pragma_Independent
13720 | Pragma_Volatile
13721 | Pragma_Atomic_Components
13722 | Pragma_Independent_Components
13723 | Pragma_Volatile_Components
13724 | Pragma_Discard_Names
13725 =>
13726 return True;
13727 when others =>
13728 null;
13729 end case;
13730
13731 when N_Enumeration_Representation_Clause
13732 | N_Record_Representation_Clause
13733 =>
13734 return True;
13735
13736 when others =>
13737 null;
13738 end case;
13739
13740 return False;
13741 end Is_Type_Related_Rep_Item;
13742
13743 ---------------------
13744 -- Kill_Rep_Clause --
13745 ---------------------
13746
13747 procedure Kill_Rep_Clause (N : Node_Id) is
13748 begin
13749 pragma Assert (Ignore_Rep_Clauses);
13750
13751 -- Note: we use Replace rather than Rewrite, because we don't want
13752 -- tools to be able to use Original_Node to dig out the (undecorated)
13753 -- rep clause that is being replaced.
13754
13755 Replace (N, Make_Null_Statement (Sloc (N)));
13756
13757 -- The null statement must be marked as not coming from source. This is
13758 -- so that tools ignore it, and also the back end does not expect bogus
13759 -- "from source" null statements in weird places (e.g. in declarative
13760 -- regions where such null statements are not allowed).
13761
13762 Set_Comes_From_Source (N, False);
13763 end Kill_Rep_Clause;
13764
13765 ------------------
13766 -- Minimum_Size --
13767 ------------------
13768
13769 function Minimum_Size
13770 (T : Entity_Id;
13771 Biased : Boolean := False) return Nat
13772 is
13773 Lo : Uint := No_Uint;
13774 Hi : Uint := No_Uint;
13775 LoR : Ureal := No_Ureal;
13776 HiR : Ureal := No_Ureal;
13777 LoSet : Boolean := False;
13778 HiSet : Boolean := False;
13779 B : Uint;
13780 S : Nat;
13781 Ancest : Entity_Id;
13782 R_Typ : constant Entity_Id := Root_Type (T);
13783
13784 begin
13785 -- If bad type, return 0
13786
13787 if T = Any_Type then
13788 return 0;
13789
13790 -- For generic types, just return zero. There cannot be any legitimate
13791 -- need to know such a size, but this routine may be called with a
13792 -- generic type as part of normal processing.
13793
13794 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
13795 return 0;
13796
13797 -- Access types (cannot have size smaller than System.Address)
13798
13799 elsif Is_Access_Type (T) then
13800 return System_Address_Size;
13801
13802 -- Floating-point types
13803
13804 elsif Is_Floating_Point_Type (T) then
13805 return UI_To_Int (Esize (R_Typ));
13806
13807 -- Discrete types
13808
13809 elsif Is_Discrete_Type (T) then
13810
13811 -- The following loop is looking for the nearest compile time known
13812 -- bounds following the ancestor subtype chain. The idea is to find
13813 -- the most restrictive known bounds information.
13814
13815 Ancest := T;
13816 loop
13817 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
13818 return 0;
13819 end if;
13820
13821 if not LoSet then
13822 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
13823 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
13824 LoSet := True;
13825 exit when HiSet;
13826 end if;
13827 end if;
13828
13829 if not HiSet then
13830 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
13831 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
13832 HiSet := True;
13833 exit when LoSet;
13834 end if;
13835 end if;
13836
13837 Ancest := Ancestor_Subtype (Ancest);
13838
13839 if No (Ancest) then
13840 Ancest := Base_Type (T);
13841
13842 if Is_Generic_Type (Ancest) then
13843 return 0;
13844 end if;
13845 end if;
13846 end loop;
13847
13848 -- Fixed-point types. We can't simply use Expr_Value to get the
13849 -- Corresponding_Integer_Value values of the bounds, since these do not
13850 -- get set till the type is frozen, and this routine can be called
13851 -- before the type is frozen. Similarly the test for bounds being static
13852 -- needs to include the case where we have unanalyzed real literals for
13853 -- the same reason.
13854
13855 elsif Is_Fixed_Point_Type (T) then
13856
13857 -- The following loop is looking for the nearest compile time known
13858 -- bounds following the ancestor subtype chain. The idea is to find
13859 -- the most restrictive known bounds information.
13860
13861 Ancest := T;
13862 loop
13863 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
13864 return 0;
13865 end if;
13866
13867 -- Note: In the following two tests for LoSet and HiSet, it may
13868 -- seem redundant to test for N_Real_Literal here since normally
13869 -- one would assume that the test for the value being known at
13870 -- compile time includes this case. However, there is a glitch.
13871 -- If the real literal comes from folding a non-static expression,
13872 -- then we don't consider any non- static expression to be known
13873 -- at compile time if we are in configurable run time mode (needed
13874 -- in some cases to give a clearer definition of what is and what
13875 -- is not accepted). So the test is indeed needed. Without it, we
13876 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
13877
13878 if not LoSet then
13879 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
13880 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
13881 then
13882 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
13883 LoSet := True;
13884 exit when HiSet;
13885 end if;
13886 end if;
13887
13888 if not HiSet then
13889 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
13890 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
13891 then
13892 HiR := Expr_Value_R (Type_High_Bound (Ancest));
13893 HiSet := True;
13894 exit when LoSet;
13895 end if;
13896 end if;
13897
13898 Ancest := Ancestor_Subtype (Ancest);
13899
13900 if No (Ancest) then
13901 Ancest := Base_Type (T);
13902
13903 if Is_Generic_Type (Ancest) then
13904 return 0;
13905 end if;
13906 end if;
13907 end loop;
13908
13909 Lo := UR_To_Uint (LoR / Small_Value (T));
13910 Hi := UR_To_Uint (HiR / Small_Value (T));
13911
13912 -- No other types allowed
13913
13914 else
13915 raise Program_Error;
13916 end if;
13917
13918 -- Fall through with Hi and Lo set. Deal with biased case
13919
13920 if (Biased
13921 and then not Is_Fixed_Point_Type (T)
13922 and then not (Is_Enumeration_Type (T)
13923 and then Has_Non_Standard_Rep (T)))
13924 or else Has_Biased_Representation (T)
13925 then
13926 Hi := Hi - Lo;
13927 Lo := Uint_0;
13928 end if;
13929
13930 -- Null range case, size is always zero. We only do this in the discrete
13931 -- type case, since that's the odd case that came up. Probably we should
13932 -- also do this in the fixed-point case, but doing so causes peculiar
13933 -- gigi failures, and it is not worth worrying about this incredibly
13934 -- marginal case (explicit null-range fixed-point type declarations)???
13935
13936 if Lo > Hi and then Is_Discrete_Type (T) then
13937 S := 0;
13938
13939 -- Signed case. Note that we consider types like range 1 .. -1 to be
13940 -- signed for the purpose of computing the size, since the bounds have
13941 -- to be accommodated in the base type.
13942
13943 elsif Lo < 0 or else Hi < 0 then
13944 S := 1;
13945 B := Uint_1;
13946
13947 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
13948 -- Note that we accommodate the case where the bounds cross. This
13949 -- can happen either because of the way the bounds are declared
13950 -- or because of the algorithm in Freeze_Fixed_Point_Type.
13951
13952 while Lo < -B
13953 or else Hi < -B
13954 or else Lo >= B
13955 or else Hi >= B
13956 loop
13957 B := Uint_2 ** S;
13958 S := S + 1;
13959 end loop;
13960
13961 -- Unsigned case
13962
13963 else
13964 -- If both bounds are positive, make sure that both are represen-
13965 -- table in the case where the bounds are crossed. This can happen
13966 -- either because of the way the bounds are declared, or because of
13967 -- the algorithm in Freeze_Fixed_Point_Type.
13968
13969 if Lo > Hi then
13970 Hi := Lo;
13971 end if;
13972
13973 -- S = size, (can accommodate 0 .. (2**size - 1))
13974
13975 S := 0;
13976 while Hi >= Uint_2 ** S loop
13977 S := S + 1;
13978 end loop;
13979 end if;
13980
13981 return S;
13982 end Minimum_Size;
13983
13984 ------------------------------
13985 -- New_Put_Image_Subprogram --
13986 ------------------------------
13987
13988 procedure New_Put_Image_Subprogram
13989 (N : Node_Id;
13990 Ent : Entity_Id;
13991 Subp : Entity_Id)
13992 is
13993 Loc : constant Source_Ptr := Sloc (N);
13994 Sname : constant Name_Id :=
13995 Make_TSS_Name (Base_Type (Ent), TSS_Put_Image);
13996 Subp_Id : Entity_Id;
13997 Subp_Decl : Node_Id;
13998 F : Entity_Id;
13999 Etyp : Entity_Id;
14000
14001 Defer_Declaration : constant Boolean :=
14002 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
14003 -- For a tagged type, there is a declaration at the freeze point, and
14004 -- we must generate only a completion of this declaration. We do the
14005 -- same for private types, because the full view might be tagged.
14006 -- Otherwise we generate a declaration at the point of the attribute
14007 -- definition clause. If the attribute definition comes from an aspect
14008 -- specification the declaration is part of the freeze actions of the
14009 -- type.
14010
14011 function Build_Spec return Node_Id;
14012 -- Used for declaration and renaming declaration, so that this is
14013 -- treated as a renaming_as_body.
14014
14015 ----------------
14016 -- Build_Spec --
14017 ----------------
14018
14019 function Build_Spec return Node_Id is
14020 Formals : List_Id;
14021 Spec : Node_Id;
14022 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
14023
14024 begin
14025 Subp_Id := Make_Defining_Identifier (Loc, Sname);
14026
14027 -- S : Sink'Class
14028
14029 Formals := New_List (
14030 Make_Parameter_Specification (Loc,
14031 Defining_Identifier =>
14032 Make_Defining_Identifier (Loc, Name_S),
14033 In_Present => True,
14034 Out_Present => True,
14035 Parameter_Type =>
14036 New_Occurrence_Of (Etype (F), Loc)));
14037
14038 -- V : T
14039
14040 Append_To (Formals,
14041 Make_Parameter_Specification (Loc,
14042 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
14043 Parameter_Type => T_Ref));
14044
14045 Spec :=
14046 Make_Procedure_Specification (Loc,
14047 Defining_Unit_Name => Subp_Id,
14048 Parameter_Specifications => Formals);
14049
14050 return Spec;
14051 end Build_Spec;
14052
14053 -- Start of processing for New_Put_Image_Subprogram
14054
14055 begin
14056 F := First_Formal (Subp);
14057
14058 Etyp := Etype (Next_Formal (F));
14059
14060 -- Prepare subprogram declaration and insert it as an action on the
14061 -- clause node. The visibility for this entity is used to test for
14062 -- visibility of the attribute definition clause (in the sense of
14063 -- 8.3(23) as amended by AI-195).
14064
14065 if not Defer_Declaration then
14066 Subp_Decl :=
14067 Make_Subprogram_Declaration (Loc,
14068 Specification => Build_Spec);
14069
14070 -- For a tagged type, there is always a visible declaration for the
14071 -- Put_Image TSS (it is a predefined primitive operation), and the
14072 -- completion of this declaration occurs at the freeze point, which is
14073 -- not always visible at places where the attribute definition clause is
14074 -- visible. So, we create a dummy entity here for the purpose of
14075 -- tracking the visibility of the attribute definition clause itself.
14076
14077 else
14078 Subp_Id :=
14079 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
14080 Subp_Decl :=
14081 Make_Object_Declaration (Loc,
14082 Defining_Identifier => Subp_Id,
14083 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
14084 end if;
14085
14086 if not Defer_Declaration
14087 and then From_Aspect_Specification (N)
14088 and then Has_Delayed_Freeze (Ent)
14089 then
14090 Append_Freeze_Action (Ent, Subp_Decl);
14091
14092 else
14093 Insert_Action (N, Subp_Decl);
14094 Set_Entity (N, Subp_Id);
14095 end if;
14096
14097 Subp_Decl :=
14098 Make_Subprogram_Renaming_Declaration (Loc,
14099 Specification => Build_Spec,
14100 Name => New_Occurrence_Of (Subp, Loc));
14101
14102 if Defer_Declaration then
14103 Set_TSS (Base_Type (Ent), Subp_Id);
14104
14105 else
14106 if From_Aspect_Specification (N) then
14107 Append_Freeze_Action (Ent, Subp_Decl);
14108 else
14109 Insert_Action (N, Subp_Decl);
14110 end if;
14111
14112 Copy_TSS (Subp_Id, Base_Type (Ent));
14113 end if;
14114 end New_Put_Image_Subprogram;
14115
14116 ---------------------------
14117 -- New_Stream_Subprogram --
14118 ---------------------------
14119
14120 procedure New_Stream_Subprogram
14121 (N : Node_Id;
14122 Ent : Entity_Id;
14123 Subp : Entity_Id;
14124 Nam : TSS_Name_Type)
14125 is
14126 Loc : constant Source_Ptr := Sloc (N);
14127 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
14128 Subp_Id : Entity_Id;
14129 Subp_Decl : Node_Id;
14130 F : Entity_Id;
14131 Etyp : Entity_Id;
14132
14133 Defer_Declaration : constant Boolean :=
14134 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
14135 -- For a tagged type, there is a declaration for each stream attribute
14136 -- at the freeze point, and we must generate only a completion of this
14137 -- declaration. We do the same for private types, because the full view
14138 -- might be tagged. Otherwise we generate a declaration at the point of
14139 -- the attribute definition clause. If the attribute definition comes
14140 -- from an aspect specification the declaration is part of the freeze
14141 -- actions of the type.
14142
14143 function Build_Spec return Node_Id;
14144 -- Used for declaration and renaming declaration, so that this is
14145 -- treated as a renaming_as_body.
14146
14147 ----------------
14148 -- Build_Spec --
14149 ----------------
14150
14151 function Build_Spec return Node_Id is
14152 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
14153 Formals : List_Id;
14154 Spec : Node_Id;
14155 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
14156
14157 begin
14158 Subp_Id := Make_Defining_Identifier (Loc, Sname);
14159
14160 -- S : access Root_Stream_Type'Class
14161
14162 Formals := New_List (
14163 Make_Parameter_Specification (Loc,
14164 Defining_Identifier =>
14165 Make_Defining_Identifier (Loc, Name_S),
14166 Parameter_Type =>
14167 Make_Access_Definition (Loc,
14168 Subtype_Mark =>
14169 New_Occurrence_Of (
14170 Designated_Type (Etype (F)), Loc))));
14171
14172 if Nam = TSS_Stream_Input then
14173 Spec :=
14174 Make_Function_Specification (Loc,
14175 Defining_Unit_Name => Subp_Id,
14176 Parameter_Specifications => Formals,
14177 Result_Definition => T_Ref);
14178 else
14179 -- V : [out] T
14180
14181 Append_To (Formals,
14182 Make_Parameter_Specification (Loc,
14183 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
14184 Out_Present => Out_P,
14185 Parameter_Type => T_Ref));
14186
14187 Spec :=
14188 Make_Procedure_Specification (Loc,
14189 Defining_Unit_Name => Subp_Id,
14190 Parameter_Specifications => Formals);
14191 end if;
14192
14193 return Spec;
14194 end Build_Spec;
14195
14196 -- Start of processing for New_Stream_Subprogram
14197
14198 begin
14199 F := First_Formal (Subp);
14200
14201 if Ekind (Subp) = E_Procedure then
14202 Etyp := Etype (Next_Formal (F));
14203 else
14204 Etyp := Etype (Subp);
14205 end if;
14206
14207 -- Prepare subprogram declaration and insert it as an action on the
14208 -- clause node. The visibility for this entity is used to test for
14209 -- visibility of the attribute definition clause (in the sense of
14210 -- 8.3(23) as amended by AI-195).
14211
14212 if not Defer_Declaration then
14213 Subp_Decl :=
14214 Make_Subprogram_Declaration (Loc,
14215 Specification => Build_Spec);
14216
14217 -- For a tagged type, there is always a visible declaration for each
14218 -- stream TSS (it is a predefined primitive operation), and the
14219 -- completion of this declaration occurs at the freeze point, which is
14220 -- not always visible at places where the attribute definition clause is
14221 -- visible. So, we create a dummy entity here for the purpose of
14222 -- tracking the visibility of the attribute definition clause itself.
14223
14224 else
14225 Subp_Id :=
14226 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
14227 Subp_Decl :=
14228 Make_Object_Declaration (Loc,
14229 Defining_Identifier => Subp_Id,
14230 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
14231 end if;
14232
14233 if not Defer_Declaration
14234 and then From_Aspect_Specification (N)
14235 and then Has_Delayed_Freeze (Ent)
14236 then
14237 Append_Freeze_Action (Ent, Subp_Decl);
14238
14239 else
14240 Insert_Action (N, Subp_Decl);
14241 Set_Entity (N, Subp_Id);
14242 end if;
14243
14244 Subp_Decl :=
14245 Make_Subprogram_Renaming_Declaration (Loc,
14246 Specification => Build_Spec,
14247 Name => New_Occurrence_Of (Subp, Loc));
14248
14249 if Defer_Declaration then
14250 Set_TSS (Base_Type (Ent), Subp_Id);
14251
14252 else
14253 if From_Aspect_Specification (N) then
14254 Append_Freeze_Action (Ent, Subp_Decl);
14255 else
14256 Insert_Action (N, Subp_Decl);
14257 end if;
14258
14259 Copy_TSS (Subp_Id, Base_Type (Ent));
14260 end if;
14261 end New_Stream_Subprogram;
14262
14263 ----------------------
14264 -- No_Type_Rep_Item --
14265 ----------------------
14266
14267 procedure No_Type_Rep_Item (N : Node_Id) is
14268 begin
14269 Error_Msg_N ("|type-related representation item not permitted!", N);
14270 end No_Type_Rep_Item;
14271
14272 --------------
14273 -- Pop_Type --
14274 --------------
14275
14276 procedure Pop_Type (E : Entity_Id) is
14277 begin
14278 if Ekind (E) = E_Record_Type and then E = Current_Scope then
14279 End_Scope;
14280
14281 elsif Is_Type (E)
14282 and then Has_Discriminants (E)
14283 and then Nkind (Parent (E)) /= N_Subtype_Declaration
14284 then
14285 Uninstall_Discriminants (E);
14286 Pop_Scope;
14287 end if;
14288 end Pop_Type;
14289
14290 ---------------
14291 -- Push_Type --
14292 ---------------
14293
14294 procedure Push_Type (E : Entity_Id) is
14295 Comp : Entity_Id;
14296
14297 begin
14298 if Ekind (E) = E_Record_Type then
14299 Push_Scope (E);
14300
14301 Comp := First_Component (E);
14302 while Present (Comp) loop
14303 Install_Entity (Comp);
14304 Next_Component (Comp);
14305 end loop;
14306
14307 if Has_Discriminants (E) then
14308 Install_Discriminants (E);
14309 end if;
14310
14311 elsif Is_Type (E)
14312 and then Has_Discriminants (E)
14313 and then Nkind (Parent (E)) /= N_Subtype_Declaration
14314 then
14315 Push_Scope (E);
14316 Install_Discriminants (E);
14317 end if;
14318 end Push_Type;
14319
14320 -----------------------------------
14321 -- Register_Address_Clause_Check --
14322 -----------------------------------
14323
14324 procedure Register_Address_Clause_Check
14325 (N : Node_Id;
14326 X : Entity_Id;
14327 A : Uint;
14328 Y : Entity_Id;
14329 Off : Boolean)
14330 is
14331 ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
14332 begin
14333 Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
14334 end Register_Address_Clause_Check;
14335
14336 ------------------------
14337 -- Rep_Item_Too_Early --
14338 ------------------------
14339
14340 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
14341 function Has_Generic_Parent (E : Entity_Id) return Boolean;
14342 -- Return True if R or any ancestor is a generic type
14343
14344 ------------------------
14345 -- Has_Generic_Parent --
14346 ------------------------
14347
14348 function Has_Generic_Parent (E : Entity_Id) return Boolean is
14349 Ancestor_Type : Entity_Id := Etype (E);
14350
14351 begin
14352 if Is_Generic_Type (E) then
14353 return True;
14354 end if;
14355
14356 while Present (Ancestor_Type)
14357 and then not Is_Generic_Type (Ancestor_Type)
14358 and then Etype (Ancestor_Type) /= Ancestor_Type
14359 loop
14360 Ancestor_Type := Etype (Ancestor_Type);
14361 end loop;
14362
14363 return
14364 Present (Ancestor_Type) and then Is_Generic_Type (Ancestor_Type);
14365 end Has_Generic_Parent;
14366
14367 -- Start of processing for Rep_Item_Too_Early
14368
14369 begin
14370 -- Cannot apply non-operational rep items to generic types
14371
14372 if Is_Operational_Item (N) then
14373 return False;
14374
14375 elsif Is_Type (T)
14376 and then Has_Generic_Parent (T)
14377 and then (Nkind (N) /= N_Pragma
14378 or else Get_Pragma_Id (N) /= Pragma_Convention)
14379 then
14380 if Ada_Version < Ada_2020 then
14381 Error_Msg_N
14382 ("representation item not allowed for generic type", N);
14383 return True;
14384 else
14385 return False;
14386 end if;
14387 end if;
14388
14389 -- Otherwise check for incomplete type
14390
14391 if Is_Incomplete_Or_Private_Type (T)
14392 and then No (Underlying_Type (T))
14393 and then
14394 (Nkind (N) /= N_Pragma
14395 or else Get_Pragma_Id (N) /= Pragma_Import)
14396 then
14397 Error_Msg_N
14398 ("representation item must be after full type declaration", N);
14399 return True;
14400
14401 -- If the type has incomplete components, a representation clause is
14402 -- illegal but stream attributes and Convention pragmas are correct.
14403
14404 elsif Has_Private_Component (T) then
14405 if Nkind (N) = N_Pragma then
14406 return False;
14407
14408 else
14409 Error_Msg_N
14410 ("representation item must appear after type is fully defined",
14411 N);
14412 return True;
14413 end if;
14414 else
14415 return False;
14416 end if;
14417 end Rep_Item_Too_Early;
14418
14419 -----------------------
14420 -- Rep_Item_Too_Late --
14421 -----------------------
14422
14423 function Rep_Item_Too_Late
14424 (T : Entity_Id;
14425 N : Node_Id;
14426 FOnly : Boolean := False) return Boolean
14427 is
14428 procedure Too_Late;
14429 -- Output message for an aspect being specified too late
14430
14431 -- Note that neither of the above errors is considered a serious one,
14432 -- since the effect is simply that we ignore the representation clause
14433 -- in these cases.
14434 -- Is this really true? In any case if we make this change we must
14435 -- document the requirement in the spec of Rep_Item_Too_Late that
14436 -- if True is returned, then the rep item must be completely ignored???
14437
14438 --------------
14439 -- Too_Late --
14440 --------------
14441
14442 procedure Too_Late is
14443 begin
14444 -- Other compilers seem more relaxed about rep items appearing too
14445 -- late. Since analysis tools typically don't care about rep items
14446 -- anyway, no reason to be too strict about this.
14447
14448 if not Relaxed_RM_Semantics then
14449 Error_Msg_N ("|representation item appears too late!", N);
14450 end if;
14451 end Too_Late;
14452
14453 -- Local variables
14454
14455 Parent_Type : Entity_Id;
14456 S : Entity_Id;
14457
14458 -- Start of processing for Rep_Item_Too_Late
14459
14460 begin
14461 -- First make sure entity is not frozen (RM 13.1(9))
14462
14463 if Is_Frozen (T)
14464
14465 -- Exclude imported types, which may be frozen if they appear in a
14466 -- representation clause for a local type.
14467
14468 and then not From_Limited_With (T)
14469
14470 -- Exclude generated entities (not coming from source). The common
14471 -- case is when we generate a renaming which prematurely freezes the
14472 -- renamed internal entity, but we still want to be able to set copies
14473 -- of attribute values such as Size/Alignment.
14474
14475 and then Comes_From_Source (T)
14476 then
14477 -- A self-referential aspect is illegal if it forces freezing the
14478 -- entity before the corresponding pragma has been analyzed.
14479
14480 if Nkind (N) in N_Attribute_Definition_Clause | N_Pragma
14481 and then From_Aspect_Specification (N)
14482 then
14483 Error_Msg_NE
14484 ("aspect specification causes premature freezing of&", N, T);
14485 Set_Has_Delayed_Freeze (T, False);
14486 return True;
14487 end if;
14488
14489 Too_Late;
14490 S := First_Subtype (T);
14491
14492 if Present (Freeze_Node (S)) then
14493 if not Relaxed_RM_Semantics then
14494 Error_Msg_NE
14495 ("??no more representation items for }", Freeze_Node (S), S);
14496 end if;
14497 end if;
14498
14499 return True;
14500
14501 -- Check for case of untagged derived type whose parent either has
14502 -- primitive operations (pre Ada 202x), or is a by-reference type (RM
14503 -- 13.1(10)). In this case we do not output a Too_Late message, since
14504 -- there is no earlier point where the rep item could be placed to make
14505 -- it legal.
14506 -- ??? Confirming representation clauses should be allowed here.
14507
14508 elsif Is_Type (T)
14509 and then not FOnly
14510 and then Is_Derived_Type (T)
14511 and then not Is_Tagged_Type (T)
14512 then
14513 Parent_Type := Etype (Base_Type (T));
14514
14515 if Relaxed_RM_Semantics then
14516 null;
14517
14518 elsif Ada_Version <= Ada_2012
14519 and then Has_Primitive_Operations (Parent_Type)
14520 then
14521 Error_Msg_N
14522 ("|representation item not permitted before Ada 202x!", N);
14523 Error_Msg_NE
14524 ("\parent type & has primitive operations!", N, Parent_Type);
14525 return True;
14526
14527 elsif Is_By_Reference_Type (Parent_Type) then
14528 No_Type_Rep_Item (N);
14529 Error_Msg_NE
14530 ("\parent type & is a by-reference type!", N, Parent_Type);
14531 return True;
14532 end if;
14533 end if;
14534
14535 -- No error, but one more warning to consider. The RM (surprisingly)
14536 -- allows this pattern in some cases:
14537
14538 -- type S is ...
14539 -- primitive operations for S
14540 -- type R is new S;
14541 -- rep clause for S
14542
14543 -- Meaning that calls on the primitive operations of S for values of
14544 -- type R may require possibly expensive implicit conversion operations.
14545 -- So even when this is not an error, it is still worth a warning.
14546
14547 if not Relaxed_RM_Semantics and then Is_Type (T) then
14548 declare
14549 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
14550
14551 begin
14552 if Present (DTL)
14553
14554 -- For now, do not generate this warning for the case of
14555 -- aspect specification using Ada 2012 syntax, since we get
14556 -- wrong messages we do not understand. The whole business
14557 -- of derived types and rep items seems a bit confused when
14558 -- aspects are used, since the aspects are not evaluated
14559 -- till freeze time. However, AI12-0109 confirms (in an AARM
14560 -- ramification) that inheritance in this case is required
14561 -- to work.
14562
14563 and then not From_Aspect_Specification (N)
14564 then
14565 if Is_By_Reference_Type (T)
14566 and then not Is_Tagged_Type (T)
14567 and then Is_Type_Related_Rep_Item (N)
14568 and then (Ada_Version >= Ada_2012
14569 or else Has_Primitive_Operations (Base_Type (T)))
14570 then
14571 -- Treat as hard error (AI12-0109, binding interpretation).
14572 -- Implementing a change of representation is not really
14573 -- an option in the case of a by-reference type, so we
14574 -- take this path for all Ada dialects if primitive
14575 -- operations are present.
14576 Error_Msg_Sloc := Sloc (DTL);
14577 Error_Msg_N
14578 ("representation item for& appears after derived type "
14579 & "declaration#", N);
14580
14581 elsif Has_Primitive_Operations (Base_Type (T)) then
14582 Error_Msg_Sloc := Sloc (DTL);
14583
14584 Error_Msg_N
14585 ("representation item for& appears after derived type "
14586 & "declaration#??", N);
14587 Error_Msg_NE
14588 ("\may result in implicit conversions for primitive "
14589 & "operations of&??", N, T);
14590 Error_Msg_NE
14591 ("\to change representations when called with arguments "
14592 & "of type&??", N, DTL);
14593 end if;
14594 end if;
14595 end;
14596 end if;
14597
14598 -- No error, link item into head of chain of rep items for the entity,
14599 -- but avoid chaining if we have an overloadable entity, and the pragma
14600 -- is one that can apply to multiple overloaded entities.
14601
14602 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
14603 declare
14604 Pname : constant Name_Id := Pragma_Name (N);
14605 begin
14606 if Pname in Name_Convention | Name_Import | Name_Export
14607 | Name_External | Name_Interface
14608 then
14609 return False;
14610 end if;
14611 end;
14612 end if;
14613
14614 Record_Rep_Item (T, N);
14615 return False;
14616 end Rep_Item_Too_Late;
14617
14618 -------------------------------------
14619 -- Replace_Type_References_Generic --
14620 -------------------------------------
14621
14622 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
14623 TName : constant Name_Id := Chars (T);
14624
14625 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
14626 -- Processes a single node in the traversal procedure below, checking
14627 -- if node N should be replaced, and if so, doing the replacement.
14628
14629 function Visible_Component (Comp : Name_Id) return Entity_Id;
14630 -- Given an identifier in the expression, check whether there is a
14631 -- discriminant, component, protected procedure, or entry of the type
14632 -- that is directy visible, and rewrite it as the corresponding selected
14633 -- component of the formal of the subprogram.
14634
14635 ----------------------
14636 -- Replace_Type_Ref --
14637 ----------------------
14638
14639 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
14640 Loc : constant Source_Ptr := Sloc (N);
14641
14642 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
14643 -- Add the proper prefix to a reference to a component of the type
14644 -- when it is not already a selected component.
14645
14646 ----------------
14647 -- Add_Prefix --
14648 ----------------
14649
14650 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
14651 begin
14652 Rewrite (Ref,
14653 Make_Selected_Component (Loc,
14654 Prefix => New_Occurrence_Of (T, Loc),
14655 Selector_Name => New_Occurrence_Of (Comp, Loc)));
14656 Replace_Type_Reference (Prefix (Ref));
14657 end Add_Prefix;
14658
14659 -- Local variables
14660
14661 Comp : Entity_Id;
14662 Pref : Node_Id;
14663 Scop : Entity_Id;
14664
14665 -- Start of processing for Replace_Type_Ref
14666
14667 begin
14668 if Nkind (N) = N_Identifier then
14669
14670 -- If not the type name, check whether it is a reference to some
14671 -- other type, which must be frozen before the predicate function
14672 -- is analyzed, i.e. before the freeze node of the type to which
14673 -- the predicate applies.
14674
14675 if Chars (N) /= TName then
14676 if Present (Current_Entity (N))
14677 and then Is_Type (Current_Entity (N))
14678 then
14679 Freeze_Before (Freeze_Node (T), Current_Entity (N));
14680 end if;
14681
14682 -- The components of the type are directly visible and can
14683 -- be referenced without a prefix.
14684
14685 if Nkind (Parent (N)) = N_Selected_Component then
14686 null;
14687
14688 -- In expression C (I), C may be a directly visible function
14689 -- or a visible component that has an array type. Disambiguate
14690 -- by examining the component type.
14691
14692 elsif Nkind (Parent (N)) = N_Indexed_Component
14693 and then N = Prefix (Parent (N))
14694 then
14695 Comp := Visible_Component (Chars (N));
14696
14697 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
14698 Add_Prefix (N, Comp);
14699 end if;
14700
14701 else
14702 Comp := Visible_Component (Chars (N));
14703
14704 if Present (Comp) then
14705 Add_Prefix (N, Comp);
14706 end if;
14707 end if;
14708
14709 return Skip;
14710
14711 -- Otherwise do the replacement if this is not a qualified
14712 -- reference to a homograph of the type itself. Note that the
14713 -- current instance could not appear in such a context, e.g.
14714 -- the prefix of a type conversion.
14715
14716 else
14717 if Nkind (Parent (N)) /= N_Selected_Component
14718 or else N /= Selector_Name (Parent (N))
14719 then
14720 Replace_Type_Reference (N);
14721 end if;
14722
14723 return Skip;
14724 end if;
14725
14726 -- Case of selected component, which may be a subcomponent of the
14727 -- current instance, or an expanded name which is still unanalyzed.
14728
14729 elsif Nkind (N) = N_Selected_Component then
14730
14731 -- If selector name is not our type, keep going (we might still
14732 -- have an occurrence of the type in the prefix). If it is a
14733 -- subcomponent of the current entity, add prefix.
14734
14735 if Nkind (Selector_Name (N)) /= N_Identifier
14736 or else Chars (Selector_Name (N)) /= TName
14737 then
14738 if Nkind (Prefix (N)) = N_Identifier then
14739 Comp := Visible_Component (Chars (Prefix (N)));
14740
14741 if Present (Comp) then
14742 Add_Prefix (Prefix (N), Comp);
14743 end if;
14744 end if;
14745
14746 return OK;
14747
14748 -- Selector name is our type, check qualification
14749
14750 else
14751 -- Loop through scopes and prefixes, doing comparison
14752
14753 Scop := Current_Scope;
14754 Pref := Prefix (N);
14755 loop
14756 -- Continue if no more scopes or scope with no name
14757
14758 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
14759 return OK;
14760 end if;
14761
14762 -- Do replace if prefix is an identifier matching the scope
14763 -- that we are currently looking at.
14764
14765 if Nkind (Pref) = N_Identifier
14766 and then Chars (Pref) = Chars (Scop)
14767 then
14768 Replace_Type_Reference (N);
14769 return Skip;
14770 end if;
14771
14772 -- Go check scope above us if prefix is itself of the form
14773 -- of a selected component, whose selector matches the scope
14774 -- we are currently looking at.
14775
14776 if Nkind (Pref) = N_Selected_Component
14777 and then Nkind (Selector_Name (Pref)) = N_Identifier
14778 and then Chars (Selector_Name (Pref)) = Chars (Scop)
14779 then
14780 Scop := Scope (Scop);
14781 Pref := Prefix (Pref);
14782
14783 -- For anything else, we don't have a match, so keep on
14784 -- going, there are still some weird cases where we may
14785 -- still have a replacement within the prefix.
14786
14787 else
14788 return OK;
14789 end if;
14790 end loop;
14791 end if;
14792
14793 -- Continue for any other node kind
14794
14795 else
14796 return OK;
14797 end if;
14798 end Replace_Type_Ref;
14799
14800 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
14801
14802 -----------------------
14803 -- Visible_Component --
14804 -----------------------
14805
14806 function Visible_Component (Comp : Name_Id) return Entity_Id is
14807 E : Entity_Id;
14808 begin
14809 -- Types with nameable components are record, task, and protected
14810 -- types, and discriminated private types.
14811
14812 if Ekind (T) in E_Record_Type
14813 | E_Task_Type
14814 | E_Protected_Type
14815 or else (Is_Private_Type (T) and then Has_Discriminants (T))
14816 then
14817 -- This is a sequential search, which seems acceptable
14818 -- efficiency-wise, given the typical size of component
14819 -- lists, protected operation lists, task item lists, and
14820 -- check expressions.
14821
14822 E := First_Entity (T);
14823 while Present (E) loop
14824 if Comes_From_Source (E) and then Chars (E) = Comp then
14825 return E;
14826 end if;
14827
14828 Next_Entity (E);
14829 end loop;
14830 end if;
14831
14832 -- Nothing by that name
14833
14834 return Empty;
14835 end Visible_Component;
14836
14837 -- Start of processing for Replace_Type_References_Generic
14838
14839 begin
14840 Replace_Type_Refs (N);
14841 end Replace_Type_References_Generic;
14842
14843 --------------------------------
14844 -- Resolve_Aspect_Expressions --
14845 --------------------------------
14846
14847 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
14848 function Resolve_Name (N : Node_Id) return Traverse_Result;
14849 -- Verify that all identifiers in the expression, with the exception
14850 -- of references to the current entity, denote visible entities. This
14851 -- is done only to detect visibility errors, as the expression will be
14852 -- properly analyzed/expanded during analysis of the predicate function
14853 -- body. We omit quantified expressions from this test, given that they
14854 -- introduce a local identifier that would require proper expansion to
14855 -- handle properly.
14856
14857 ------------------
14858 -- Resolve_Name --
14859 ------------------
14860
14861 function Resolve_Name (N : Node_Id) return Traverse_Result is
14862 Dummy : Traverse_Result;
14863
14864 begin
14865 if Nkind (N) = N_Selected_Component then
14866 if Nkind (Prefix (N)) = N_Identifier
14867 and then Chars (Prefix (N)) /= Chars (E)
14868 then
14869 Find_Selected_Component (N);
14870 end if;
14871
14872 return Skip;
14873
14874 -- Resolve identifiers that are not selectors in parameter
14875 -- associations (these are never resolved by visibility).
14876
14877 elsif Nkind (N) = N_Identifier
14878 and then Chars (N) /= Chars (E)
14879 and then (Nkind (Parent (N)) /= N_Parameter_Association
14880 or else N /= Selector_Name (Parent (N)))
14881 then
14882 Find_Direct_Name (N);
14883 Set_Entity (N, Empty);
14884
14885 -- The name is component association needs no resolution.
14886
14887 elsif Nkind (N) = N_Component_Association then
14888 Dummy := Resolve_Name (Expression (N));
14889 return Skip;
14890
14891 elsif Nkind (N) = N_Quantified_Expression then
14892 return Skip;
14893 end if;
14894
14895 return OK;
14896 end Resolve_Name;
14897
14898 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
14899
14900 -- Local variables
14901
14902 ASN : Node_Id := First_Rep_Item (E);
14903
14904 -- Start of processing for Resolve_Aspect_Expressions
14905
14906 begin
14907 if No (ASN) then
14908 return;
14909 end if;
14910
14911 while Present (ASN) loop
14912 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
14913 declare
14914 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
14915 Expr : constant Node_Id := Expression (ASN);
14916
14917 begin
14918 case A_Id is
14919
14920 when Aspect_Aggregate =>
14921 Resolve_Aspect_Aggregate (Entity (ASN), Expr);
14922
14923 -- For now we only deal with aspects that do not generate
14924 -- subprograms, or that may mention current instances of
14925 -- types. These will require special handling (???TBD).
14926
14927 when Aspect_Invariant
14928 | Aspect_Predicate
14929 | Aspect_Predicate_Failure
14930 =>
14931 null;
14932
14933 when Aspect_Dynamic_Predicate
14934 | Aspect_Static_Predicate
14935 =>
14936 -- Build predicate function specification and preanalyze
14937 -- expression after type replacement. The function
14938 -- declaration must be analyzed in the scope of the type,
14939 -- but the expression can reference components and
14940 -- discriminants of the type.
14941
14942 if No (Predicate_Function (E)) then
14943 declare
14944 FDecl : constant Node_Id :=
14945 Build_Predicate_Function_Declaration (E);
14946 pragma Unreferenced (FDecl);
14947
14948 begin
14949 Push_Type (E);
14950 Resolve_Aspect_Expression (Expr);
14951 Pop_Type (E);
14952 end;
14953 end if;
14954
14955 when Pre_Post_Aspects =>
14956 null;
14957
14958 when Aspect_Iterable =>
14959 if Nkind (Expr) = N_Aggregate then
14960 declare
14961 Assoc : Node_Id;
14962
14963 begin
14964 Assoc := First (Component_Associations (Expr));
14965 while Present (Assoc) loop
14966 Find_Direct_Name (Expression (Assoc));
14967 Next (Assoc);
14968 end loop;
14969 end;
14970 end if;
14971
14972 -- The expression for Default_Value is a static expression
14973 -- of the type, but this expression does not freeze the
14974 -- type, so it can still appear in a representation clause
14975 -- before the actual freeze point.
14976
14977 when Aspect_Default_Value =>
14978 Set_Must_Not_Freeze (Expr);
14979 Preanalyze_Spec_Expression (Expr, E);
14980
14981 when Aspect_Priority =>
14982 Push_Type (E);
14983 Preanalyze_Spec_Expression (Expr, Any_Integer);
14984 Pop_Type (E);
14985
14986 -- Ditto for Storage_Size. Any other aspects that carry
14987 -- expressions that should not freeze ??? This is only
14988 -- relevant to the misuse of deferred constants.
14989
14990 when Aspect_Storage_Size =>
14991 Set_Must_Not_Freeze (Expr);
14992 Preanalyze_Spec_Expression (Expr, Any_Integer);
14993
14994 when others =>
14995 if Present (Expr) then
14996 case Aspect_Argument (A_Id) is
14997 when Expression
14998 | Optional_Expression
14999 =>
15000 Analyze_And_Resolve (Expr);
15001
15002 when Name
15003 | Optional_Name
15004 =>
15005 if Nkind (Expr) = N_Identifier then
15006 Find_Direct_Name (Expr);
15007
15008 elsif Nkind (Expr) = N_Selected_Component then
15009 Find_Selected_Component (Expr);
15010 end if;
15011 end case;
15012 end if;
15013 end case;
15014 end;
15015 end if;
15016
15017 Next_Rep_Item (ASN);
15018 end loop;
15019 end Resolve_Aspect_Expressions;
15020
15021 ----------------------------
15022 -- Parse_Aspect_Aggregate --
15023 ----------------------------
15024
15025 procedure Parse_Aspect_Aggregate
15026 (N : Node_Id;
15027 Empty_Subp : in out Node_Id;
15028 Add_Named_Subp : in out Node_Id;
15029 Add_Unnamed_Subp : in out Node_Id;
15030 New_Indexed_Subp : in out Node_Id;
15031 Assign_Indexed_Subp : in out Node_Id)
15032 is
15033 Assoc : Node_Id := First (Component_Associations (N));
15034 Op_Name : Name_Id;
15035 Subp : Node_Id;
15036
15037 begin
15038 while Present (Assoc) loop
15039 Subp := Expression (Assoc);
15040 Op_Name := Chars (First (Choices (Assoc)));
15041 if Op_Name = Name_Empty then
15042 Empty_Subp := Subp;
15043
15044 elsif Op_Name = Name_Add_Named then
15045 Add_Named_Subp := Subp;
15046
15047 elsif Op_Name = Name_Add_Unnamed then
15048 Add_Unnamed_Subp := Subp;
15049
15050 elsif Op_Name = Name_New_Indexed then
15051 New_Indexed_Subp := Subp;
15052
15053 elsif Op_Name = Name_Assign_Indexed then
15054 Assign_Indexed_Subp := Subp;
15055 end if;
15056
15057 Next (Assoc);
15058 end loop;
15059 end Parse_Aspect_Aggregate;
15060
15061 -------------------------------
15062 -- Validate_Aspect_Aggregate --
15063 -------------------------------
15064
15065 procedure Validate_Aspect_Aggregate (N : Node_Id) is
15066 Empty_Subp : Node_Id := Empty;
15067 Add_Named_Subp : Node_Id := Empty;
15068 Add_Unnamed_Subp : Node_Id := Empty;
15069 New_Indexed_Subp : Node_Id := Empty;
15070 Assign_Indexed_Subp : Node_Id := Empty;
15071
15072 begin
15073 if Ada_Version < Ada_2020 then
15074 Error_Msg_N ("Aspect Aggregate is an Ada_2020 feature", N);
15075
15076 elsif Nkind (N) /= N_Aggregate
15077 or else Present (Expressions (N))
15078 or else No (Component_Associations (N))
15079 then
15080 Error_Msg_N ("Aspect Aggregate requires an aggregate "
15081 & "with component associations", N);
15082 return;
15083 end if;
15084
15085 Parse_Aspect_Aggregate (N,
15086 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
15087 New_Indexed_Subp, Assign_Indexed_Subp);
15088
15089 if No (Empty_Subp) then
15090 Error_Msg_N ("missing specification for Empty in aggregate", N);
15091 end if;
15092
15093 if Present (Add_Named_Subp) then
15094 if Present (Add_Unnamed_Subp)
15095 or else Present (Assign_Indexed_Subp)
15096 then
15097 Error_Msg_N
15098 ("conflicting operations for aggregate (RM 4.3.5)", N);
15099 return;
15100 end if;
15101
15102 elsif Present (New_Indexed_Subp) /= Present (Assign_Indexed_Subp) then
15103 Error_Msg_N ("incomplete specification for indexed aggregate", N);
15104 end if;
15105 end Validate_Aspect_Aggregate;
15106
15107 --------------------------------
15108 -- Resolve_Iterable_Operation --
15109 --------------------------------
15110
15111 procedure Resolve_Iterable_Operation
15112 (N : Node_Id;
15113 Cursor : Entity_Id;
15114 Typ : Entity_Id;
15115 Nam : Name_Id)
15116 is
15117 Ent : Entity_Id;
15118 F1 : Entity_Id;
15119 F2 : Entity_Id;
15120
15121 begin
15122 if not Is_Overloaded (N) then
15123 if not Is_Entity_Name (N)
15124 or else Ekind (Entity (N)) /= E_Function
15125 or else Scope (Entity (N)) /= Scope (Typ)
15126 or else No (First_Formal (Entity (N)))
15127 or else Etype (First_Formal (Entity (N))) /= Typ
15128 then
15129 Error_Msg_N
15130 ("iterable primitive must be local function name whose first "
15131 & "formal is an iterable type", N);
15132 return;
15133 end if;
15134
15135 Ent := Entity (N);
15136 F1 := First_Formal (Ent);
15137
15138 if Nam = Name_First or else Nam = Name_Last then
15139
15140 -- First or Last (Container) => Cursor
15141
15142 if Etype (Ent) /= Cursor then
15143 Error_Msg_N ("primitive for First must yield a curosr", N);
15144 end if;
15145
15146 elsif Nam = Name_Next then
15147
15148 -- Next (Container, Cursor) => Cursor
15149
15150 F2 := Next_Formal (F1);
15151
15152 if Etype (F2) /= Cursor
15153 or else Etype (Ent) /= Cursor
15154 or else Present (Next_Formal (F2))
15155 then
15156 Error_Msg_N ("no match for Next iterable primitive", N);
15157 end if;
15158
15159 elsif Nam = Name_Previous then
15160
15161 -- Previous (Container, Cursor) => Cursor
15162
15163 F2 := Next_Formal (F1);
15164
15165 if Etype (F2) /= Cursor
15166 or else Etype (Ent) /= Cursor
15167 or else Present (Next_Formal (F2))
15168 then
15169 Error_Msg_N ("no match for Previous iterable primitive", N);
15170 end if;
15171
15172 elsif Nam = Name_Has_Element then
15173
15174 -- Has_Element (Container, Cursor) => Boolean
15175
15176 F2 := Next_Formal (F1);
15177
15178 if Etype (F2) /= Cursor
15179 or else Etype (Ent) /= Standard_Boolean
15180 or else Present (Next_Formal (F2))
15181 then
15182 Error_Msg_N ("no match for Has_Element iterable primitive", N);
15183 end if;
15184
15185 elsif Nam = Name_Element then
15186 F2 := Next_Formal (F1);
15187
15188 if No (F2)
15189 or else Etype (F2) /= Cursor
15190 or else Present (Next_Formal (F2))
15191 then
15192 Error_Msg_N ("no match for Element iterable primitive", N);
15193 end if;
15194
15195 else
15196 raise Program_Error;
15197 end if;
15198
15199 else
15200 -- Overloaded case: find subprogram with proper signature. Caller
15201 -- will report error if no match is found.
15202
15203 declare
15204 I : Interp_Index;
15205 It : Interp;
15206
15207 begin
15208 Get_First_Interp (N, I, It);
15209 while Present (It.Typ) loop
15210 if Ekind (It.Nam) = E_Function
15211 and then Scope (It.Nam) = Scope (Typ)
15212 and then Etype (First_Formal (It.Nam)) = Typ
15213 then
15214 F1 := First_Formal (It.Nam);
15215
15216 if Nam = Name_First then
15217 if Etype (It.Nam) = Cursor
15218 and then No (Next_Formal (F1))
15219 then
15220 Set_Entity (N, It.Nam);
15221 exit;
15222 end if;
15223
15224 elsif Nam = Name_Next then
15225 F2 := Next_Formal (F1);
15226
15227 if Present (F2)
15228 and then No (Next_Formal (F2))
15229 and then Etype (F2) = Cursor
15230 and then Etype (It.Nam) = Cursor
15231 then
15232 Set_Entity (N, It.Nam);
15233 exit;
15234 end if;
15235
15236 elsif Nam = Name_Has_Element then
15237 F2 := Next_Formal (F1);
15238
15239 if Present (F2)
15240 and then No (Next_Formal (F2))
15241 and then Etype (F2) = Cursor
15242 and then Etype (It.Nam) = Standard_Boolean
15243 then
15244 Set_Entity (N, It.Nam);
15245 F2 := Next_Formal (F1);
15246 exit;
15247 end if;
15248
15249 elsif Nam = Name_Element then
15250 F2 := Next_Formal (F1);
15251
15252 if Present (F2)
15253 and then No (Next_Formal (F2))
15254 and then Etype (F2) = Cursor
15255 then
15256 Set_Entity (N, It.Nam);
15257 exit;
15258 end if;
15259 end if;
15260 end if;
15261
15262 Get_Next_Interp (I, It);
15263 end loop;
15264 end;
15265 end if;
15266 end Resolve_Iterable_Operation;
15267
15268 ------------------------------
15269 -- Resolve_Aspect_Aggregate --
15270 ------------------------------
15271
15272 procedure Resolve_Aspect_Aggregate
15273 (Typ : Entity_Id;
15274 Expr : Node_Id)
15275 is
15276 -- Predicates that establish the legality of each possible operation in
15277 -- an Aggregate aspect.
15278
15279 function Valid_Empty (E : Entity_Id) return Boolean;
15280 function Valid_Add_Named (E : Entity_Id) return Boolean;
15281 function Valid_Add_Unnamed (E : Entity_Id) return Boolean;
15282 function Valid_New_Indexed (E : Entity_Id) return Boolean;
15283 function Valid_Assign_Indexed (E : Entity_Id) return Boolean;
15284
15285 generic
15286 with function Pred (Id : Node_Id) return Boolean;
15287 procedure Resolve_Operation (Subp_Id : Node_Id);
15288 -- Common processing to resolve each aggregate operation.
15289
15290 ------------------------
15291 -- Valid_Assign_Index --
15292 ------------------------
15293
15294 function Valid_Assign_Indexed (E : Entity_Id) return Boolean is
15295 begin
15296 -- The profile must be the same as for Add_Named, with the added
15297 -- requirement that the key_type be a discrete type.
15298
15299 if Valid_Add_Named (E) then
15300 return Is_Discrete_Type (Etype (Next_Formal (First_Formal (E))));
15301 else
15302 return False;
15303 end if;
15304 end Valid_Assign_Indexed;
15305
15306 -----------------
15307 -- Valid_Emoty --
15308 -----------------
15309
15310 function Valid_Empty (E : Entity_Id) return Boolean is
15311 begin
15312 if Etype (E) /= Typ or else Scope (E) /= Scope (Typ) then
15313 return False;
15314
15315 elsif Ekind (E) = E_Constant then
15316 return True;
15317
15318 elsif Ekind (E) = E_Function then
15319 return No (First_Formal (E))
15320 or else
15321 (Is_Integer_Type (Etype (First_Formal (E)))
15322 and then No (Next_Formal (First_Formal (E))));
15323 else
15324 return False;
15325 end if;
15326 end Valid_Empty;
15327
15328 ---------------------
15329 -- Valid_Add_Named --
15330 ---------------------
15331
15332 function Valid_Add_Named (E : Entity_Id) return Boolean is
15333 F2, F3 : Entity_Id;
15334 begin
15335 if Ekind (E) = E_Procedure
15336 and then Scope (E) = Scope (Typ)
15337 and then Number_Formals (E) = 3
15338 and then Etype (First_Formal (E)) = Typ
15339 and then Ekind (First_Formal (E)) = E_In_Out_Parameter
15340 then
15341 F2 := Next_Formal (First_Formal (E));
15342 F3 := Next_Formal (F2);
15343 return Ekind (F2) = E_In_Parameter
15344 and then Ekind (F3) = E_In_Parameter
15345 and then not Is_Limited_Type (Etype (F2))
15346 and then not Is_Limited_Type (Etype (F3));
15347 else
15348 return False;
15349 end if;
15350 end Valid_Add_Named;
15351
15352 -----------------------
15353 -- Valid_Add_Unnamed --
15354 -----------------------
15355
15356 function Valid_Add_Unnamed (E : Entity_Id) return Boolean is
15357 begin
15358 return Ekind (E) = E_Procedure
15359 and then Scope (E) = Scope (Typ)
15360 and then Number_Formals (E) = 2
15361 and then Etype (First_Formal (E)) = Typ
15362 and then Ekind (First_Formal (E)) = E_In_Out_Parameter
15363 and then
15364 not Is_Limited_Type (Etype (Next_Formal (First_Formal (E))));
15365 end Valid_Add_Unnamed;
15366
15367 -----------------------
15368 -- Valid_Nmw_Indexed --
15369 -----------------------
15370
15371 function Valid_New_Indexed (E : Entity_Id) return Boolean is
15372 begin
15373 return Ekind (E) = E_Function
15374 and then Scope (E) = Scope (Typ)
15375 and then Etype (E) = Typ
15376 and then Number_Formals (E) = 2
15377 and then Is_Discrete_Type (Etype (First_Formal (E)))
15378 and then Etype (First_Formal (E)) =
15379 Etype (Next_Formal (First_Formal (E)));
15380 end Valid_New_Indexed;
15381
15382 -----------------------
15383 -- Resolve_Operation --
15384 -----------------------
15385
15386 procedure Resolve_Operation (Subp_Id : Node_Id) is
15387 Subp : Entity_Id;
15388
15389 I : Interp_Index;
15390 It : Interp;
15391
15392 begin
15393 if not Is_Overloaded (Subp_Id) then
15394 Subp := Entity (Subp_Id);
15395 if not Pred (Subp) then
15396 Error_Msg_NE
15397 ("improper aggregate operation for&", Subp_Id, Typ);
15398 end if;
15399
15400 else
15401 Set_Entity (Subp_Id, Empty);
15402 Get_First_Interp (Subp_Id, I, It);
15403 while Present (It.Nam) loop
15404 if Pred (It.Nam) then
15405 Set_Is_Overloaded (Subp_Id, False);
15406 Set_Entity (Subp_Id, It.Nam);
15407 exit;
15408 end if;
15409
15410 Get_Next_Interp (I, It);
15411 end loop;
15412
15413 if No (Entity (Subp_Id)) then
15414 Error_Msg_NE
15415 ("improper aggregate operation for&", Subp_Id, Typ);
15416 end if;
15417 end if;
15418 end Resolve_Operation;
15419
15420 Assoc : Node_Id;
15421 Op_Name : Name_Id;
15422 Subp_Id : Node_Id;
15423
15424 procedure Resolve_Empty is new Resolve_Operation (Valid_Empty);
15425 procedure Resolve_Unnamed is new Resolve_Operation (Valid_Add_Unnamed);
15426 procedure Resolve_Named is new Resolve_Operation (Valid_Add_Named);
15427 procedure Resolve_Indexed is new Resolve_Operation (Valid_New_Indexed);
15428 procedure Resolve_Assign_Indexed
15429 is new Resolve_Operation
15430 (Valid_Assign_Indexed);
15431 begin
15432 Assoc := First (Component_Associations (Expr));
15433
15434 while Present (Assoc) loop
15435 Op_Name := Chars (First (Choices (Assoc)));
15436
15437 -- When verifying the consistency of aspects between the freeze point
15438 -- and the end of declarqtions, we use a copy which is not analyzed
15439 -- yet, so do it now.
15440
15441 Subp_Id := Expression (Assoc);
15442 if No (Etype (Subp_Id)) then
15443 Analyze (Subp_Id);
15444 end if;
15445
15446 if Op_Name = Name_Empty then
15447 Resolve_Empty (Subp_Id);
15448
15449 elsif Op_Name = Name_Add_Named then
15450 Resolve_Named (Subp_Id);
15451
15452 elsif Op_Name = Name_Add_Unnamed then
15453 Resolve_Unnamed (Subp_Id);
15454
15455 elsif Op_Name = Name_New_Indexed then
15456 Resolve_Indexed (Subp_Id);
15457
15458 elsif Op_Name = Name_Assign_Indexed then
15459 Resolve_Assign_Indexed (Subp_Id);
15460 end if;
15461
15462 Next (Assoc);
15463 end loop;
15464 end Resolve_Aspect_Aggregate;
15465
15466 ----------------
15467 -- Set_Biased --
15468 ----------------
15469
15470 procedure Set_Biased
15471 (E : Entity_Id;
15472 N : Node_Id;
15473 Msg : String;
15474 Biased : Boolean := True)
15475 is
15476 begin
15477 if Biased then
15478 Set_Has_Biased_Representation (E);
15479
15480 if Warn_On_Biased_Representation then
15481 Error_Msg_NE
15482 ("?B?" & Msg & " forces biased representation for&", N, E);
15483 end if;
15484 end if;
15485 end Set_Biased;
15486
15487 --------------------
15488 -- Set_Enum_Esize --
15489 --------------------
15490
15491 procedure Set_Enum_Esize (T : Entity_Id) is
15492 Lo : Uint;
15493 Hi : Uint;
15494 Sz : Nat;
15495
15496 begin
15497 Init_Alignment (T);
15498
15499 -- Find the minimum standard size (8,16,32,64,128) that fits
15500
15501 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
15502 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
15503
15504 if Lo < 0 then
15505 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
15506 Sz := Standard_Character_Size; -- May be > 8 on some targets
15507
15508 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
15509 Sz := 16;
15510
15511 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
15512 Sz := 32;
15513
15514 elsif Lo >= -Uint_2**63 and then Hi < Uint_2**63 then
15515 Sz := 64;
15516
15517 else pragma Assert (Lo >= -Uint_2**127 and then Hi < Uint_2**127);
15518 Sz := 128;
15519 end if;
15520
15521 else
15522 if Hi < Uint_2**08 then
15523 Sz := Standard_Character_Size; -- May be > 8 on some targets
15524
15525 elsif Hi < Uint_2**16 then
15526 Sz := 16;
15527
15528 elsif Hi < Uint_2**32 then
15529 Sz := 32;
15530
15531 elsif Hi < Uint_2**64 then
15532 Sz := 64;
15533
15534 else pragma Assert (Hi < Uint_2**128);
15535 Sz := 128;
15536 end if;
15537 end if;
15538
15539 -- That minimum is the proper size unless we have a foreign convention
15540 -- and the size required is 32 or less, in which case we bump the size
15541 -- up to 32. This is required for C and C++ and seems reasonable for
15542 -- all other foreign conventions.
15543
15544 if Has_Foreign_Convention (T)
15545 and then Esize (T) < Standard_Integer_Size
15546
15547 -- Don't do this if Short_Enums on target
15548
15549 and then not Target_Short_Enums
15550 then
15551 Init_Esize (T, Standard_Integer_Size);
15552 else
15553 Init_Esize (T, Sz);
15554 end if;
15555 end Set_Enum_Esize;
15556
15557 -----------------------------
15558 -- Uninstall_Discriminants --
15559 -----------------------------
15560
15561 procedure Uninstall_Discriminants (E : Entity_Id) is
15562 Disc : Entity_Id;
15563 Prev : Entity_Id;
15564 Outer : Entity_Id;
15565
15566 begin
15567 -- Discriminants have been made visible for type declarations and
15568 -- protected type declarations, not for subtype declarations.
15569
15570 if Nkind (Parent (E)) /= N_Subtype_Declaration then
15571 Disc := First_Discriminant (E);
15572 while Present (Disc) loop
15573 if Disc /= Current_Entity (Disc) then
15574 Prev := Current_Entity (Disc);
15575 while Present (Prev)
15576 and then Present (Homonym (Prev))
15577 and then Homonym (Prev) /= Disc
15578 loop
15579 Prev := Homonym (Prev);
15580 end loop;
15581 else
15582 Prev := Empty;
15583 end if;
15584
15585 Set_Is_Immediately_Visible (Disc, False);
15586
15587 Outer := Homonym (Disc);
15588 while Present (Outer) and then Scope (Outer) = E loop
15589 Outer := Homonym (Outer);
15590 end loop;
15591
15592 -- Reset homonym link of other entities, but do not modify link
15593 -- between entities in current scope, so that the back end can
15594 -- have a proper count of local overloadings.
15595
15596 if No (Prev) then
15597 Set_Name_Entity_Id (Chars (Disc), Outer);
15598
15599 elsif Scope (Prev) /= Scope (Disc) then
15600 Set_Homonym (Prev, Outer);
15601 end if;
15602
15603 Next_Discriminant (Disc);
15604 end loop;
15605 end if;
15606 end Uninstall_Discriminants;
15607
15608 ------------------------------
15609 -- Validate_Address_Clauses --
15610 ------------------------------
15611
15612 procedure Validate_Address_Clauses is
15613 function Offset_Value (Expr : Node_Id) return Uint;
15614 -- Given an Address attribute reference, return the value in bits of its
15615 -- offset from the first bit of the underlying entity, or 0 if it is not
15616 -- known at compile time.
15617
15618 ------------------
15619 -- Offset_Value --
15620 ------------------
15621
15622 function Offset_Value (Expr : Node_Id) return Uint is
15623 N : Node_Id := Prefix (Expr);
15624 Off : Uint;
15625 Val : Uint := Uint_0;
15626
15627 begin
15628 -- Climb the prefix chain and compute the cumulative offset
15629
15630 loop
15631 if Is_Entity_Name (N) then
15632 return Val;
15633
15634 elsif Nkind (N) = N_Selected_Component then
15635 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
15636 if Off /= No_Uint and then Off >= Uint_0 then
15637 Val := Val + Off;
15638 N := Prefix (N);
15639 else
15640 return Uint_0;
15641 end if;
15642
15643 elsif Nkind (N) = N_Indexed_Component then
15644 Off := Indexed_Component_Bit_Offset (N);
15645 if Off /= No_Uint then
15646 Val := Val + Off;
15647 N := Prefix (N);
15648 else
15649 return Uint_0;
15650 end if;
15651
15652 else
15653 return Uint_0;
15654 end if;
15655 end loop;
15656 end Offset_Value;
15657
15658 -- Start of processing for Validate_Address_Clauses
15659
15660 begin
15661 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
15662 declare
15663 ACCR : Address_Clause_Check_Record
15664 renames Address_Clause_Checks.Table (J);
15665
15666 Expr : Node_Id;
15667
15668 X_Alignment : Uint;
15669 Y_Alignment : Uint := Uint_0;
15670
15671 X_Size : Uint;
15672 Y_Size : Uint := Uint_0;
15673
15674 X_Offs : Uint;
15675
15676 begin
15677 -- Skip processing of this entry if warning already posted
15678
15679 if not Address_Warning_Posted (ACCR.N) then
15680 Expr := Original_Node (Expression (ACCR.N));
15681
15682 -- Get alignments, sizes and offset, if any
15683
15684 X_Alignment := Alignment (ACCR.X);
15685 X_Size := Esize (ACCR.X);
15686
15687 if Present (ACCR.Y) then
15688 Y_Alignment := Alignment (ACCR.Y);
15689 Y_Size := Esize (ACCR.Y);
15690 end if;
15691
15692 if ACCR.Off
15693 and then Nkind (Expr) = N_Attribute_Reference
15694 and then Attribute_Name (Expr) = Name_Address
15695 then
15696 X_Offs := Offset_Value (Expr);
15697 else
15698 X_Offs := Uint_0;
15699 end if;
15700
15701 -- Check for known value not multiple of alignment
15702
15703 if No (ACCR.Y) then
15704 if not Alignment_Checks_Suppressed (ACCR)
15705 and then X_Alignment /= 0
15706 and then ACCR.A mod X_Alignment /= 0
15707 then
15708 Error_Msg_NE
15709 ("??specified address for& is inconsistent with "
15710 & "alignment", ACCR.N, ACCR.X);
15711 Error_Msg_N
15712 ("\??program execution may be erroneous (RM 13.3(27))",
15713 ACCR.N);
15714
15715 Error_Msg_Uint_1 := X_Alignment;
15716 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
15717 end if;
15718
15719 -- Check for large object overlaying smaller one
15720
15721 elsif Y_Size > Uint_0
15722 and then X_Size > Uint_0
15723 and then X_Offs + X_Size > Y_Size
15724 then
15725 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
15726 Error_Msg_N
15727 ("\??program execution may be erroneous", ACCR.N);
15728
15729 Error_Msg_Uint_1 := X_Size;
15730 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
15731
15732 Error_Msg_Uint_1 := Y_Size;
15733 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
15734
15735 if Y_Size >= X_Size then
15736 Error_Msg_Uint_1 := X_Offs;
15737 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
15738 end if;
15739
15740 -- Check for inadequate alignment, both of the base object
15741 -- and of the offset, if any. We only do this check if the
15742 -- run-time Alignment_Check is active. No point in warning
15743 -- if this check has been suppressed (or is suppressed by
15744 -- default in the non-strict alignment machine case).
15745
15746 -- Note: we do not check the alignment if we gave a size
15747 -- warning, since it would likely be redundant.
15748
15749 elsif not Alignment_Checks_Suppressed (ACCR)
15750 and then Y_Alignment /= Uint_0
15751 and then
15752 (Y_Alignment < X_Alignment
15753 or else
15754 (ACCR.Off
15755 and then Nkind (Expr) = N_Attribute_Reference
15756 and then Attribute_Name (Expr) = Name_Address
15757 and then Has_Compatible_Alignment
15758 (ACCR.X, Prefix (Expr), True) /=
15759 Known_Compatible))
15760 then
15761 Error_Msg_NE
15762 ("??specified address for& may be inconsistent with "
15763 & "alignment", ACCR.N, ACCR.X);
15764 Error_Msg_N
15765 ("\??program execution may be erroneous (RM 13.3(27))",
15766 ACCR.N);
15767
15768 Error_Msg_Uint_1 := X_Alignment;
15769 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
15770
15771 Error_Msg_Uint_1 := Y_Alignment;
15772 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
15773
15774 if Y_Alignment >= X_Alignment then
15775 Error_Msg_N
15776 ("\??but offset is not multiple of alignment", ACCR.N);
15777 end if;
15778 end if;
15779 end if;
15780 end;
15781 end loop;
15782 end Validate_Address_Clauses;
15783
15784 ---------------------------
15785 -- Validate_Independence --
15786 ---------------------------
15787
15788 procedure Validate_Independence is
15789 SU : constant Uint := UI_From_Int (System_Storage_Unit);
15790 N : Node_Id;
15791 E : Entity_Id;
15792 IC : Boolean;
15793 Comp : Entity_Id;
15794 Addr : Node_Id;
15795 P : Node_Id;
15796
15797 procedure Check_Array_Type (Atyp : Entity_Id);
15798 -- Checks if the array type Atyp has independent components, and
15799 -- if not, outputs an appropriate set of error messages.
15800
15801 procedure No_Independence;
15802 -- Output message that independence cannot be guaranteed
15803
15804 function OK_Component (C : Entity_Id) return Boolean;
15805 -- Checks one component to see if it is independently accessible, and
15806 -- if so yields True, otherwise yields False if independent access
15807 -- cannot be guaranteed. This is a conservative routine, it only
15808 -- returns True if it knows for sure, it returns False if it knows
15809 -- there is a problem, or it cannot be sure there is no problem.
15810
15811 procedure Reason_Bad_Component (C : Entity_Id);
15812 -- Outputs continuation message if a reason can be determined for
15813 -- the component C being bad.
15814
15815 ----------------------
15816 -- Check_Array_Type --
15817 ----------------------
15818
15819 procedure Check_Array_Type (Atyp : Entity_Id) is
15820 Ctyp : constant Entity_Id := Component_Type (Atyp);
15821
15822 begin
15823 -- OK if no alignment clause, no pack, and no component size
15824
15825 if not Has_Component_Size_Clause (Atyp)
15826 and then not Has_Alignment_Clause (Atyp)
15827 and then not Is_Packed (Atyp)
15828 then
15829 return;
15830 end if;
15831
15832 -- Case where component size is greater than or equal to the maximum
15833 -- integer size and the alignment of the array is at least as large
15834 -- as the alignment of the component. We are OK in this situation.
15835
15836 if Known_Component_Size (Atyp)
15837 and then Component_Size (Atyp) >= System_Max_Integer_Size
15838 and then Known_Alignment (Atyp)
15839 and then Known_Alignment (Ctyp)
15840 and then Alignment (Atyp) >= Alignment (Ctyp)
15841 then
15842 return;
15843 end if;
15844
15845 -- Check actual component size
15846
15847 if not Known_Component_Size (Atyp)
15848 or else not Addressable (Component_Size (Atyp))
15849 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
15850 then
15851 No_Independence;
15852
15853 -- Bad component size, check reason
15854
15855 if Has_Component_Size_Clause (Atyp) then
15856 P := Get_Attribute_Definition_Clause
15857 (Atyp, Attribute_Component_Size);
15858
15859 if Present (P) then
15860 Error_Msg_Sloc := Sloc (P);
15861 Error_Msg_N ("\because of Component_Size clause#", N);
15862 return;
15863 end if;
15864 end if;
15865
15866 if Is_Packed (Atyp) then
15867 P := Get_Rep_Pragma (Atyp, Name_Pack);
15868
15869 if Present (P) then
15870 Error_Msg_Sloc := Sloc (P);
15871 Error_Msg_N ("\because of pragma Pack#", N);
15872 return;
15873 end if;
15874 end if;
15875
15876 -- No reason found, just return
15877
15878 return;
15879 end if;
15880
15881 -- Array type is OK independence-wise
15882
15883 return;
15884 end Check_Array_Type;
15885
15886 ---------------------
15887 -- No_Independence --
15888 ---------------------
15889
15890 procedure No_Independence is
15891 begin
15892 if Pragma_Name (N) = Name_Independent then
15893 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
15894 else
15895 Error_Msg_NE
15896 ("independent components cannot be guaranteed for&", N, E);
15897 end if;
15898 end No_Independence;
15899
15900 ------------------
15901 -- OK_Component --
15902 ------------------
15903
15904 function OK_Component (C : Entity_Id) return Boolean is
15905 Rec : constant Entity_Id := Scope (C);
15906 Ctyp : constant Entity_Id := Etype (C);
15907
15908 begin
15909 -- OK if no component clause, no Pack, and no alignment clause
15910
15911 if No (Component_Clause (C))
15912 and then not Is_Packed (Rec)
15913 and then not Has_Alignment_Clause (Rec)
15914 then
15915 return True;
15916 end if;
15917
15918 -- Here we look at the actual component layout. A component is
15919 -- addressable if its size is a multiple of the Esize of the
15920 -- component type, and its starting position in the record has
15921 -- appropriate alignment, and the record itself has appropriate
15922 -- alignment to guarantee the component alignment.
15923
15924 -- Make sure sizes are static, always assume the worst for any
15925 -- cases where we cannot check static values.
15926
15927 if not (Known_Static_Esize (C)
15928 and then
15929 Known_Static_Esize (Ctyp))
15930 then
15931 return False;
15932 end if;
15933
15934 -- Size of component must be addressable or greater than the maximum
15935 -- integer size and a multiple of bytes.
15936
15937 if not Addressable (Esize (C))
15938 and then Esize (C) < System_Max_Integer_Size
15939 then
15940 return False;
15941 end if;
15942
15943 -- Check size is proper multiple
15944
15945 if Esize (C) mod Esize (Ctyp) /= 0 then
15946 return False;
15947 end if;
15948
15949 -- Check alignment of component is OK
15950
15951 if not Known_Component_Bit_Offset (C)
15952 or else Component_Bit_Offset (C) < Uint_0
15953 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
15954 then
15955 return False;
15956 end if;
15957
15958 -- Check alignment of record type is OK
15959
15960 if not Known_Alignment (Rec)
15961 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
15962 then
15963 return False;
15964 end if;
15965
15966 -- All tests passed, component is addressable
15967
15968 return True;
15969 end OK_Component;
15970
15971 --------------------------
15972 -- Reason_Bad_Component --
15973 --------------------------
15974
15975 procedure Reason_Bad_Component (C : Entity_Id) is
15976 Rec : constant Entity_Id := Scope (C);
15977 Ctyp : constant Entity_Id := Etype (C);
15978
15979 begin
15980 -- If component clause present assume that's the problem
15981
15982 if Present (Component_Clause (C)) then
15983 Error_Msg_Sloc := Sloc (Component_Clause (C));
15984 Error_Msg_N ("\because of Component_Clause#", N);
15985 return;
15986 end if;
15987
15988 -- If pragma Pack clause present, assume that's the problem
15989
15990 if Is_Packed (Rec) then
15991 P := Get_Rep_Pragma (Rec, Name_Pack);
15992
15993 if Present (P) then
15994 Error_Msg_Sloc := Sloc (P);
15995 Error_Msg_N ("\because of pragma Pack#", N);
15996 return;
15997 end if;
15998 end if;
15999
16000 -- See if record has bad alignment clause
16001
16002 if Has_Alignment_Clause (Rec)
16003 and then Known_Alignment (Rec)
16004 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
16005 then
16006 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
16007
16008 if Present (P) then
16009 Error_Msg_Sloc := Sloc (P);
16010 Error_Msg_N ("\because of Alignment clause#", N);
16011 end if;
16012 end if;
16013
16014 -- Couldn't find a reason, so return without a message
16015
16016 return;
16017 end Reason_Bad_Component;
16018
16019 -- Start of processing for Validate_Independence
16020
16021 begin
16022 for J in Independence_Checks.First .. Independence_Checks.Last loop
16023 N := Independence_Checks.Table (J).N;
16024 E := Independence_Checks.Table (J).E;
16025 IC := Pragma_Name (N) = Name_Independent_Components;
16026
16027 -- Deal with component case
16028
16029 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
16030 if not OK_Component (E) then
16031 No_Independence;
16032 Reason_Bad_Component (E);
16033 goto Continue;
16034 end if;
16035 end if;
16036
16037 -- Deal with record with Independent_Components
16038
16039 if IC and then Is_Record_Type (E) then
16040 Comp := First_Component_Or_Discriminant (E);
16041 while Present (Comp) loop
16042 if not OK_Component (Comp) then
16043 No_Independence;
16044 Reason_Bad_Component (Comp);
16045 goto Continue;
16046 end if;
16047
16048 Next_Component_Or_Discriminant (Comp);
16049 end loop;
16050 end if;
16051
16052 -- Deal with address clause case
16053
16054 if Is_Object (E) then
16055 Addr := Address_Clause (E);
16056
16057 if Present (Addr) then
16058 No_Independence;
16059 Error_Msg_Sloc := Sloc (Addr);
16060 Error_Msg_N ("\because of Address clause#", N);
16061 goto Continue;
16062 end if;
16063 end if;
16064
16065 -- Deal with independent components for array type
16066
16067 if IC and then Is_Array_Type (E) then
16068 Check_Array_Type (E);
16069 end if;
16070
16071 -- Deal with independent components for array object
16072
16073 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
16074 Check_Array_Type (Etype (E));
16075 end if;
16076
16077 <<Continue>> null;
16078 end loop;
16079 end Validate_Independence;
16080
16081 ------------------------------
16082 -- Validate_Iterable_Aspect --
16083 ------------------------------
16084
16085 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
16086 Assoc : Node_Id;
16087 Expr : Node_Id;
16088
16089 Prim : Node_Id;
16090 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
16091
16092 First_Id : Entity_Id;
16093 Last_Id : Entity_Id;
16094 Next_Id : Entity_Id;
16095 Has_Element_Id : Entity_Id;
16096 Element_Id : Entity_Id;
16097
16098 begin
16099 -- If previous error aspect is unusable
16100
16101 if Cursor = Any_Type then
16102 return;
16103 end if;
16104
16105 First_Id := Empty;
16106 Last_Id := Empty;
16107 Next_Id := Empty;
16108 Has_Element_Id := Empty;
16109 Element_Id := Empty;
16110
16111 -- Each expression must resolve to a function with the proper signature
16112
16113 Assoc := First (Component_Associations (Expression (ASN)));
16114 while Present (Assoc) loop
16115 Expr := Expression (Assoc);
16116 Analyze (Expr);
16117
16118 Prim := First (Choices (Assoc));
16119
16120 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
16121 Error_Msg_N ("illegal name in association", Prim);
16122
16123 elsif Chars (Prim) = Name_First then
16124 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
16125 First_Id := Entity (Expr);
16126
16127 elsif Chars (Prim) = Name_Last then
16128 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Last);
16129 Last_Id := Entity (Expr);
16130
16131 elsif Chars (Prim) = Name_Previous then
16132 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Previous);
16133 Last_Id := Entity (Expr);
16134
16135 elsif Chars (Prim) = Name_Next then
16136 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
16137 Next_Id := Entity (Expr);
16138
16139 elsif Chars (Prim) = Name_Has_Element then
16140 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
16141 Has_Element_Id := Entity (Expr);
16142
16143 elsif Chars (Prim) = Name_Element then
16144 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
16145 Element_Id := Entity (Expr);
16146
16147 else
16148 Error_Msg_N ("invalid name for iterable function", Prim);
16149 end if;
16150
16151 Next (Assoc);
16152 end loop;
16153
16154 if No (First_Id) then
16155 Error_Msg_N ("match for First primitive not found", ASN);
16156
16157 elsif No (Next_Id) then
16158 Error_Msg_N ("match for Next primitive not found", ASN);
16159
16160 elsif No (Has_Element_Id) then
16161 Error_Msg_N ("match for Has_Element primitive not found", ASN);
16162
16163 elsif No (Element_Id) or else No (Last_Id) then
16164 null; -- optional
16165 end if;
16166 end Validate_Iterable_Aspect;
16167
16168 ------------------------------
16169 -- Validate_Literal_Aspect --
16170 ------------------------------
16171
16172 procedure Validate_Literal_Aspect (Typ : Entity_Id; ASN : Node_Id) is
16173 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
16174 pragma Assert ((A_Id = Aspect_Integer_Literal) or
16175 (A_Id = Aspect_Real_Literal) or
16176 (A_Id = Aspect_String_Literal));
16177 Func_Name : constant Node_Id := Expression (ASN);
16178 Overloaded : Boolean := Is_Overloaded (Func_Name);
16179
16180 I : Interp_Index;
16181 It : Interp;
16182 Param_Type : Entity_Id;
16183 Match_Found : Boolean := False;
16184 Match2_Found : Boolean := False;
16185 Is_Match : Boolean;
16186 Match : Interp;
16187 Match2 : Entity_Id := Empty;
16188
16189 function Matching
16190 (Param_Id : Entity_Id; Param_Type : Entity_Id) return Boolean;
16191 -- Return True if Param_Id is a non aliased in parameter whose base type
16192 -- is Param_Type.
16193
16194 --------------
16195 -- Matching --
16196 --------------
16197
16198 function Matching
16199 (Param_Id : Entity_Id; Param_Type : Entity_Id) return Boolean is
16200 begin
16201 return Base_Type (Etype (Param_Id)) = Param_Type
16202 and then Ekind (Param_Id) = E_In_Parameter
16203 and then not Is_Aliased (Param_Id);
16204 end Matching;
16205
16206 begin
16207 if not Is_Type (Typ) then
16208 Error_Msg_N ("aspect can only be specified for a type", ASN);
16209 return;
16210
16211 elsif not Is_First_Subtype (Typ) then
16212 Error_Msg_N ("aspect cannot be specified for a subtype", ASN);
16213 return;
16214 end if;
16215
16216 if A_Id = Aspect_String_Literal then
16217 if Is_String_Type (Typ) then
16218 Error_Msg_N ("aspect cannot be specified for a string type", ASN);
16219 return;
16220 end if;
16221
16222 Param_Type := Standard_Wide_Wide_String;
16223
16224 else
16225 if Is_Numeric_Type (Typ) then
16226 Error_Msg_N ("aspect cannot be specified for a numeric type", ASN);
16227 return;
16228 end if;
16229
16230 Param_Type := Standard_String;
16231 end if;
16232
16233 if not Overloaded and then not Present (Entity (Func_Name)) then
16234 Analyze (Func_Name);
16235 Overloaded := Is_Overloaded (Func_Name);
16236 end if;
16237
16238 if Overloaded then
16239 Get_First_Interp (Func_Name, I => I, It => It);
16240 else
16241 -- only one possible interpretation
16242 It.Nam := Entity (Func_Name);
16243 pragma Assert (Present (It.Nam));
16244 end if;
16245
16246 while It.Nam /= Empty loop
16247 Is_Match := False;
16248
16249 if Ekind (It.Nam) = E_Function
16250 and then Base_Type (Etype (It.Nam)) = Base_Type (Typ)
16251 then
16252 declare
16253 Params : constant List_Id :=
16254 Parameter_Specifications (Parent (It.Nam));
16255 Param_Spec : Node_Id;
16256
16257 begin
16258 if List_Length (Params) = 1 then
16259 Param_Spec := First (Params);
16260 Is_Match :=
16261 Matching (Defining_Identifier (Param_Spec), Param_Type);
16262
16263 -- Look for the optional overloaded 2-param Real_Literal
16264
16265 elsif List_Length (Params) = 2
16266 and then A_Id = Aspect_Real_Literal
16267 then
16268 Param_Spec := First (Params);
16269
16270 if Matching (Defining_Identifier (Param_Spec), Param_Type)
16271 then
16272 Param_Spec := Next (Param_Spec);
16273
16274 if Matching (Defining_Identifier (Param_Spec), Param_Type)
16275 then
16276 if No (Match2) then
16277 Match2 := It.Nam;
16278 Match2_Found := True;
16279 else
16280 -- If we find more than one possible match then
16281 -- do not take any into account here: since the
16282 -- 2-parameter version of Real_Literal is optional
16283 -- we cannot generate an error here, so let
16284 -- standard resolution fail later if we do need to
16285 -- call this variant.
16286
16287 Match2_Found := False;
16288 end if;
16289 end if;
16290 end if;
16291 end if;
16292 end;
16293 end if;
16294
16295 if Is_Match then
16296 if Match_Found then
16297 Error_Msg_N ("aspect specification is ambiguous", ASN);
16298 return;
16299 end if;
16300
16301 Match_Found := True;
16302 Match := It;
16303 end if;
16304
16305 exit when not Overloaded;
16306
16307 if not Is_Match then
16308 Remove_Interp (I => I);
16309 end if;
16310
16311 Get_Next_Interp (I => I, It => It);
16312 end loop;
16313
16314 if not Match_Found then
16315 Error_Msg_N
16316 ("function name in aspect specification cannot be resolved", ASN);
16317 return;
16318 end if;
16319
16320 Set_Entity (Func_Name, Match.Nam);
16321 Set_Etype (Func_Name, Etype (Match.Nam));
16322 Set_Is_Overloaded (Func_Name, False);
16323
16324 -- Record the match for 2-parameter function if found
16325
16326 if Match2_Found then
16327 Set_Related_Expression (Match.Nam, Match2);
16328 end if;
16329 end Validate_Literal_Aspect;
16330
16331 -----------------------------------
16332 -- Validate_Unchecked_Conversion --
16333 -----------------------------------
16334
16335 procedure Validate_Unchecked_Conversion
16336 (N : Node_Id;
16337 Act_Unit : Entity_Id)
16338 is
16339 Source : Entity_Id;
16340 Target : Entity_Id;
16341 Vnode : Node_Id;
16342
16343 begin
16344 -- Obtain source and target types. Note that we call Ancestor_Subtype
16345 -- here because the processing for generic instantiation always makes
16346 -- subtypes, and we want the original frozen actual types.
16347
16348 -- If we are dealing with private types, then do the check on their
16349 -- fully declared counterparts if the full declarations have been
16350 -- encountered (they don't have to be visible, but they must exist).
16351
16352 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
16353
16354 if Is_Private_Type (Source)
16355 and then Present (Underlying_Type (Source))
16356 then
16357 Source := Underlying_Type (Source);
16358 end if;
16359
16360 Target := Ancestor_Subtype (Etype (Act_Unit));
16361
16362 -- If either type is generic, the instantiation happens within a generic
16363 -- unit, and there is nothing to check. The proper check will happen
16364 -- when the enclosing generic is instantiated.
16365
16366 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
16367 return;
16368 end if;
16369
16370 if Is_Private_Type (Target)
16371 and then Present (Underlying_Type (Target))
16372 then
16373 Target := Underlying_Type (Target);
16374 end if;
16375
16376 -- Source may be unconstrained array, but not target, except in relaxed
16377 -- semantics mode.
16378
16379 if Is_Array_Type (Target)
16380 and then not Is_Constrained (Target)
16381 and then not Relaxed_RM_Semantics
16382 then
16383 Error_Msg_N
16384 ("unchecked conversion to unconstrained array not allowed", N);
16385 return;
16386 end if;
16387
16388 -- Warn if conversion between two different convention pointers
16389
16390 if Is_Access_Type (Target)
16391 and then Is_Access_Type (Source)
16392 and then Convention (Target) /= Convention (Source)
16393 and then Warn_On_Unchecked_Conversion
16394 then
16395 -- Give warnings for subprogram pointers only on most targets
16396
16397 if Is_Access_Subprogram_Type (Target)
16398 or else Is_Access_Subprogram_Type (Source)
16399 then
16400 Error_Msg_N
16401 ("?z?conversion between pointers with different conventions!",
16402 N);
16403 end if;
16404 end if;
16405
16406 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
16407 -- warning when compiling GNAT-related sources.
16408
16409 if Warn_On_Unchecked_Conversion
16410 and then not In_Predefined_Unit (N)
16411 and then RTU_Loaded (Ada_Calendar)
16412 and then (Chars (Source) = Name_Time
16413 or else
16414 Chars (Target) = Name_Time)
16415 then
16416 -- If Ada.Calendar is loaded and the name of one of the operands is
16417 -- Time, there is a good chance that this is Ada.Calendar.Time.
16418
16419 declare
16420 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
16421 begin
16422 pragma Assert (Present (Calendar_Time));
16423
16424 if Source = Calendar_Time or else Target = Calendar_Time then
16425 Error_Msg_N
16426 ("?z?representation of 'Time values may change between "
16427 & "'G'N'A'T versions", N);
16428 end if;
16429 end;
16430 end if;
16431
16432 -- Make entry in unchecked conversion table for later processing by
16433 -- Validate_Unchecked_Conversions, which will check sizes and alignments
16434 -- (using values set by the back end where possible). This is only done
16435 -- if the appropriate warning is active.
16436
16437 if Warn_On_Unchecked_Conversion then
16438 Unchecked_Conversions.Append
16439 (New_Val => UC_Entry'(Eloc => Sloc (N),
16440 Source => Source,
16441 Target => Target,
16442 Act_Unit => Act_Unit));
16443
16444 -- If both sizes are known statically now, then back-end annotation
16445 -- is not required to do a proper check but if either size is not
16446 -- known statically, then we need the annotation.
16447
16448 if Known_Static_RM_Size (Source)
16449 and then
16450 Known_Static_RM_Size (Target)
16451 then
16452 null;
16453 else
16454 Back_Annotate_Rep_Info := True;
16455 end if;
16456 end if;
16457
16458 -- If unchecked conversion to access type, and access type is declared
16459 -- in the same unit as the unchecked conversion, then set the flag
16460 -- No_Strict_Aliasing (no strict aliasing is implicit here)
16461
16462 if Is_Access_Type (Target) and then
16463 In_Same_Source_Unit (Target, N)
16464 then
16465 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
16466 end if;
16467
16468 -- Generate N_Validate_Unchecked_Conversion node for back end in case
16469 -- the back end needs to perform special validation checks.
16470
16471 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
16472 -- have full expansion and the back end is called ???
16473
16474 Vnode :=
16475 Make_Validate_Unchecked_Conversion (Sloc (N));
16476 Set_Source_Type (Vnode, Source);
16477 Set_Target_Type (Vnode, Target);
16478
16479 -- If the unchecked conversion node is in a list, just insert before it.
16480 -- If not we have some strange case, not worth bothering about.
16481
16482 if Is_List_Member (N) then
16483 Insert_After (N, Vnode);
16484 end if;
16485 end Validate_Unchecked_Conversion;
16486
16487 ------------------------------------
16488 -- Validate_Unchecked_Conversions --
16489 ------------------------------------
16490
16491 procedure Validate_Unchecked_Conversions is
16492 function Is_Null_Array (T : Entity_Id) return Boolean;
16493 -- We want to warn in the case of converting to a wrong-sized array of
16494 -- bytes, including the zero-size case. This returns True in that case,
16495 -- which is necessary because a size of 0 is used to indicate both an
16496 -- unknown size and a size of 0. It's OK for this to return True in
16497 -- other zero-size cases, but we don't go out of our way; for example,
16498 -- we don't bother with multidimensional arrays.
16499
16500 function Is_Null_Array (T : Entity_Id) return Boolean is
16501 begin
16502 if Is_Array_Type (T) and then Is_Constrained (T) then
16503 declare
16504 Index : constant Node_Id := First_Index (T);
16505 R : Node_Id; -- N_Range
16506 begin
16507 case Nkind (Index) is
16508 when N_Range =>
16509 R := Index;
16510 when N_Subtype_Indication =>
16511 R := Range_Expression (Constraint (Index));
16512 when N_Identifier | N_Expanded_Name =>
16513 R := Scalar_Range (Entity (Index));
16514 when others =>
16515 raise Program_Error;
16516 end case;
16517
16518 return Is_Null_Range (Low_Bound (R), High_Bound (R));
16519 end;
16520 end if;
16521
16522 return False;
16523 end Is_Null_Array;
16524
16525 begin
16526 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
16527 declare
16528 T : UC_Entry renames Unchecked_Conversions.Table (N);
16529
16530 Act_Unit : constant Entity_Id := T.Act_Unit;
16531 Eloc : constant Source_Ptr := T.Eloc;
16532 Source : constant Entity_Id := T.Source;
16533 Target : constant Entity_Id := T.Target;
16534
16535 Source_Siz : Uint;
16536 Target_Siz : Uint;
16537
16538 begin
16539 -- Skip if function marked as warnings off
16540
16541 if Warnings_Off (Act_Unit) or else Serious_Errors_Detected > 0 then
16542 goto Continue;
16543 end if;
16544
16545 -- Don't do the check if warnings off for either type, note the
16546 -- deliberate use of OR here instead of OR ELSE to get the flag
16547 -- Warnings_Off_Used set for both types if appropriate.
16548
16549 if Has_Warnings_Off (Source) or Has_Warnings_Off (Target) then
16550 goto Continue;
16551 end if;
16552
16553 if (Known_Static_RM_Size (Source)
16554 and then Known_Static_RM_Size (Target))
16555 or else Is_Null_Array (Target)
16556 then
16557 -- This validation check, which warns if we have unequal sizes
16558 -- for unchecked conversion, and thus implementation dependent
16559 -- semantics, is one of the few occasions on which we use the
16560 -- official RM size instead of Esize. See description in Einfo
16561 -- "Handling of Type'Size Values" for details.
16562
16563 Source_Siz := RM_Size (Source);
16564 Target_Siz := RM_Size (Target);
16565
16566 if Source_Siz /= Target_Siz then
16567 Error_Msg
16568 ("?z?types for unchecked conversion have different sizes!",
16569 Eloc, Act_Unit);
16570
16571 if All_Errors_Mode then
16572 Error_Msg_Name_1 := Chars (Source);
16573 Error_Msg_Uint_1 := Source_Siz;
16574 Error_Msg_Name_2 := Chars (Target);
16575 Error_Msg_Uint_2 := Target_Siz;
16576 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
16577
16578 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
16579
16580 if Is_Discrete_Type (Source)
16581 and then
16582 Is_Discrete_Type (Target)
16583 then
16584 if Source_Siz > Target_Siz then
16585 Error_Msg
16586 ("\?z?^ high order bits of source will "
16587 & "be ignored!", Eloc);
16588
16589 elsif Is_Unsigned_Type (Source) then
16590 Error_Msg
16591 ("\?z?source will be extended with ^ high order "
16592 & "zero bits!", Eloc);
16593
16594 else
16595 Error_Msg
16596 ("\?z?source will be extended with ^ high order "
16597 & "sign bits!", Eloc);
16598 end if;
16599
16600 elsif Source_Siz < Target_Siz then
16601 if Is_Discrete_Type (Target) then
16602 if Bytes_Big_Endian then
16603 Error_Msg
16604 ("\?z?target value will include ^ undefined "
16605 & "low order bits!", Eloc, Act_Unit);
16606 else
16607 Error_Msg
16608 ("\?z?target value will include ^ undefined "
16609 & "high order bits!", Eloc, Act_Unit);
16610 end if;
16611
16612 else
16613 Error_Msg
16614 ("\?z?^ trailing bits of target value will be "
16615 & "undefined!", Eloc, Act_Unit);
16616 end if;
16617
16618 else pragma Assert (Source_Siz > Target_Siz);
16619 if Is_Discrete_Type (Source) then
16620 if Bytes_Big_Endian then
16621 Error_Msg
16622 ("\?z?^ low order bits of source will be "
16623 & "ignored!", Eloc, Act_Unit);
16624 else
16625 Error_Msg
16626 ("\?z?^ high order bits of source will be "
16627 & "ignored!", Eloc, Act_Unit);
16628 end if;
16629
16630 else
16631 Error_Msg
16632 ("\?z?^ trailing bits of source will be "
16633 & "ignored!", Eloc, Act_Unit);
16634 end if;
16635 end if;
16636 end if;
16637 end if;
16638 end if;
16639
16640 -- If both types are access types, we need to check the alignment.
16641 -- If the alignment of both is specified, we can do it here.
16642
16643 if Serious_Errors_Detected = 0
16644 and then Is_Access_Type (Source)
16645 and then Is_Access_Type (Target)
16646 and then Target_Strict_Alignment
16647 and then Present (Designated_Type (Source))
16648 and then Present (Designated_Type (Target))
16649 then
16650 declare
16651 D_Source : constant Entity_Id := Designated_Type (Source);
16652 D_Target : constant Entity_Id := Designated_Type (Target);
16653
16654 begin
16655 if Known_Alignment (D_Source)
16656 and then
16657 Known_Alignment (D_Target)
16658 then
16659 declare
16660 Source_Align : constant Uint := Alignment (D_Source);
16661 Target_Align : constant Uint := Alignment (D_Target);
16662
16663 begin
16664 if Source_Align < Target_Align
16665 and then not Is_Tagged_Type (D_Source)
16666
16667 -- Suppress warning if warnings suppressed on either
16668 -- type or either designated type. Note the use of
16669 -- OR here instead of OR ELSE. That is intentional,
16670 -- we would like to set flag Warnings_Off_Used in
16671 -- all types for which warnings are suppressed.
16672
16673 and then not (Has_Warnings_Off (D_Source)
16674 or
16675 Has_Warnings_Off (D_Target)
16676 or
16677 Has_Warnings_Off (Source)
16678 or
16679 Has_Warnings_Off (Target))
16680 then
16681 Error_Msg_Uint_1 := Target_Align;
16682 Error_Msg_Uint_2 := Source_Align;
16683 Error_Msg_Node_1 := D_Target;
16684 Error_Msg_Node_2 := D_Source;
16685 Error_Msg
16686 ("?z?alignment of & (^) is stricter than "
16687 & "alignment of & (^)!", Eloc, Act_Unit);
16688 Error_Msg
16689 ("\?z?resulting access value may have invalid "
16690 & "alignment!", Eloc, Act_Unit);
16691 end if;
16692 end;
16693 end if;
16694 end;
16695 end if;
16696 end;
16697
16698 <<Continue>>
16699 null;
16700 end loop;
16701 end Validate_Unchecked_Conversions;
16702
16703 end Sem_Ch13;